Skip to content

Commit

Permalink
client: Port CephFS client to Windows
Browse files Browse the repository at this point in the history
This patch provides a few changes required in order to use the
CephFS client on Windows.

A subsequent patch will make use of it along with Dokan, an
interface similar to Fuse.

Signed-off-by: Lucian Petrut <[email protected]>
  • Loading branch information
petrutlucian94 committed Jan 13, 2021
1 parent ae8d603 commit 073b81c
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 7 deletions.
39 changes: 37 additions & 2 deletions src/client/Client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@
#include <sys/param.h>
#include <fcntl.h>
#include <sys/file.h>
#ifndef _WIN32
#include <sys/utsname.h>
#endif
#include <sys/uio.h>

#include <boost/lexical_cast.hpp>
#include <boost/fusion/include/std_pair.hpp>

#include "common/async/waiter.h"

#if defined(__FreeBSD__)
#if defined(__FreeBSD__) || defined(_WIN32)
#define XATTR_CREATE 0x1
#define XATTR_REPLACE 0x2
#else
Expand Down Expand Up @@ -127,6 +129,20 @@
#define O_DIRECT 0x0
#endif

// Windows doesn't define those values. While the Posix compatibilty layer
// doesn't support those values, the Windows native functions do provide
// similar flags. Special care should be taken if we're going to use those
// flags in ceph-dokan. The current values are no-ops, while propagating
// them to the rest of the code might cause the Windows functions to reject
// them as invalid.
#ifndef O_NOFOLLOW
#define O_NOFOLLOW 0x0
#endif

#ifndef O_SYNC
#define O_SYNC 0x0
#endif

#define DEBUG_GETATTR_CAPS (CEPH_CAP_XATTR_SHARED)

using namespace TOPNSPC::common;
Expand Down Expand Up @@ -2034,6 +2050,13 @@ MetaSession *Client::_get_or_open_mds_session(mds_rank_t mds)
void Client::populate_metadata(const std::string &mount_root)
{
// Hostname
#ifdef _WIN32
// TODO: move this to compat.h
char hostname[64];
DWORD hostname_sz = 64;
GetComputerNameA(hostname, &hostname_sz);
metadata["hostname"] = hostname;
#else
struct utsname u;
int r = uname(&u);
if (r >= 0) {
Expand All @@ -2042,6 +2065,7 @@ void Client::populate_metadata(const std::string &mount_root)
} else {
ldout(cct, 1) << __func__ << " failed to read hostname (" << cpp_strerror(r) << ")" << dendl;
}
#endif

metadata["pid"] = stringify(getpid());

Expand Down Expand Up @@ -7439,6 +7463,9 @@ void Client::stat_to_statx(struct stat *st, struct ceph_statx *stx)
#ifdef __APPLE__
stx->stx_mtime = st->st_mtimespec;
stx->stx_atime = st->st_atimespec;
#elif __WIN32
stx->stx_mtime.tv_sec = st->st_mtime;
stx->stx_atime.tv_sec = st->st_atime;
#else
stx->stx_mtime = st->st_mtim;
stx->stx_atime = st->st_atim;
Expand Down Expand Up @@ -7732,12 +7759,20 @@ int Client::fill_stat(Inode *in, struct stat *st, frag_info_t *dirstat, nest_inf
st->st_size = in->rstat.rbytes;
else
st->st_size = in->dirstat.size();
// The Windows "stat" structure provides just a subset of the fields that are
// available on Linux.
#ifndef _WIN32
st->st_blocks = 1;
#endif
} else {
st->st_size = in->size;
#ifndef _WIN32
st->st_blocks = (in->size + 511) >> 9;
#endif
}
#ifndef _WIN32
st->st_blksize = std::max<uint32_t>(in->layout.stripe_unit, 4096);
#endif

if (dirstat)
*dirstat = in->dirstat;
Expand Down Expand Up @@ -8281,7 +8316,7 @@ void Client::fill_dirent(struct dirent *de, const char *name, int type, uint64_t
{
strncpy(de->d_name, name, 255);
de->d_name[255] = '\0';
#ifndef __CYGWIN__
#if !defined(__CYGWIN__) && !(defined(_WIN32))
de->d_ino = ino;
#if !defined(__APPLE__) && !defined(__FreeBSD__)
de->d_off = next_off;
Expand Down
1 change: 1 addition & 0 deletions src/client/Inode.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <numeric>

#include "include/compat.h"
#include "include/ceph_assert.h"
#include "include/types.h"
#include "include/xlist.h"
Expand Down
20 changes: 20 additions & 0 deletions src/common/compat.cc
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,26 @@ ssize_t get_self_exe_path(char* path, int buff_length) {
return GetModuleFileName(NULL, path, buff_length - 1);
}

int geteuid()
{
return 0;
}

int getegid()
{
return 0;
}

int getuid()
{
return 0;
}

int getgid()
{
return 0;
}

#else

unsigned get_page_size() {
Expand Down
4 changes: 4 additions & 0 deletions src/include/cephfs/ceph_ll_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
#define CEPH_CEPH_LL_CLIENT_H
#include <stdint.h>

#ifdef _WIN32
#include "include/win32/fs_compat.h"
#endif

#ifdef __cplusplus
extern "C" {

Expand Down
20 changes: 15 additions & 5 deletions src/include/compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ int sched_setaffinity(pid_t pid, size_t cpusetsize,
#define LOG_AUTHPRIV (10<<3)
#define LOG_FTP (11<<3)
#define __STRING(x) "x"
#endif

#if defined(__sun) || defined(_AIX) || defined(_WIN32)
#define IFTODT(mode) (((mode) & 0170000) >> 12)
#endif

Expand Down Expand Up @@ -220,6 +223,7 @@ int ceph_memzero_s(void *dest, size_t destsz, size_t count);
#include <time.h>

#include "include/win32/win32_errno.h"
#include "include/win32/fs_compat.h"

// There are a few name collisions between Windows headers and Ceph.
// Updating Ceph definitions would be the prefferable fix in order to avoid
Expand All @@ -237,12 +241,12 @@ typedef unsigned int uint;

typedef _sigset_t sigset_t;

typedef int uid_t;
typedef int gid_t;
typedef unsigned int uid_t;
typedef unsigned int gid_t;

typedef long blksize_t;
typedef long blkcnt_t;
typedef long nlink_t;
typedef unsigned int blksize_t;
typedef unsigned __int64 blkcnt_t;
typedef unsigned short nlink_t;

typedef long long loff_t;

Expand Down Expand Up @@ -297,6 +301,12 @@ int chown(const char *path, uid_t owner, gid_t group);
int fchown(int fd, uid_t owner, gid_t group);
int lchown(const char *path, uid_t owner, gid_t group);
int setenv(const char *name, const char *value, int overwrite);

int geteuid();
int getegid();
int getuid();
int getgid();

#define unsetenv(name) _putenv_s(name, "")

int win_socketpair(int socks[2]);
Expand Down
35 changes: 35 additions & 0 deletions src/include/win32/fs_compat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2021 SUSE LINUX GmbH
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software
* Foundation. See file COPYING.
*
*/

// Those definitions allow handling information coming from Ceph and should
// not be passed to Windows functions.

#define S_IFLNK 0120000

#define S_ISTYPE(m, TYPE) ((m & S_IFMT) == TYPE)
#define S_ISLNK(m) S_ISTYPE(m, S_IFLNK)
#define S_ISUID 04000
#define S_ISGID 02000
#define S_ISVTX 01000

#define LOCK_SH 1
#define LOCK_EX 2
#define LOCK_NB 4
#define LOCK_UN 8
#define LOCK_MAND 32
#define LOCK_READ 64
#define LOCK_WRITE 128
#define LOCK_RW 192

#define AT_SYMLINK_NOFOLLOW 0x100

#define MAXSYMLINKS 65000
36 changes: 36 additions & 0 deletions src/include/win32/sys/statvfs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef _SYS_STATVFS_H
#define _SYS_STATVFS_H 1

typedef unsigned __int64 fsfilcnt64_t;
typedef unsigned __int64 fsblkcnt64_t;
typedef unsigned __int64 fsblkcnt_t;

struct statvfs
{
unsigned long int f_bsize;
unsigned long int f_frsize;
fsblkcnt64_t f_blocks;
fsblkcnt64_t f_bfree;
fsblkcnt64_t f_bavail;
fsfilcnt64_t f_files;
fsfilcnt64_t f_ffree;
fsfilcnt64_t f_favail;
unsigned long int f_fsid;
unsigned long int f_flag;
unsigned long int f_namemax;
int __f_spare[6];
};
struct flock {
short l_type;
short l_whence;
off_t l_start;
off_t l_len;
pid_t l_pid;
};

#define F_RDLCK 0
#define F_WRLCK 1
#define F_UNLCK 2
#define F_SETLK 6

#endif /* _SYS_STATVFS_H */
1 change: 1 addition & 0 deletions src/mds/CInode.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "common/config.h"
#include "common/RefCountedObj.h"
#include "include/compat.h"
#include "include/counter.h"
#include "include/elist.h"
#include "include/types.h"
Expand Down
1 change: 1 addition & 0 deletions src/mds/mdstypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "common/StackStringStream.h"
#include "common/entity_name.h"

#include "include/compat.h"
#include "include/Context.h"
#include "include/frag.h"
#include "include/xlist.h"
Expand Down

0 comments on commit 073b81c

Please sign in to comment.