hw/9pfs: Add 9pfs support for Windows
PATCH v4 4/16 hw/9pfs: Implement Windows specific xxxdir() APIs
In reply to Bin MengFrom: Guohuai Shi <[email protected]>
This commit implements Windows specific xxxdir() APIs for safety directory access.
---
hw/9pfs/9p-util.h | 6 + hw/9pfs/9p-util-win32.c | 296 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 302 insertions(+)
hw/9pfs/9p-util.h +6 -0
@@ -141,6 +141,12 @@ int unlinkat_win32(int dirfd, const char *pathname, int flags);int statfs_win32 (const char *root_path,struct statfs *stbuf);int openat_dir (int dirfd,const char *name);int openat_file (int dirfd,const char *name,int flags,mode_t mode); +DIR *opendir_win32 (const char *full_file_name); +int closedir_win32 (DIR *pDir); +struct dirent *readdir_win32 (DIR *pDir); +void rewinddir_win32 (DIR *pDir); +void seekdir_win32 (DIR *pDir,long pos); +long telldir_win32 (DIR *pDir);#endif static inline void close_preserve_errno (int fd)
hw/9pfs/9p-util-win32.c +296 -0
311 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH v4 9/16 hw/9pfs: Disable unsupported flags and features for Windows
In reply to Bin MengFrom: Guohuai Shi <[email protected]>
Some flags and features are not supported on Windows, like mknod, readlink, file mode, etc. Update the codes for Windows.
---
hw/9pfs/9p.c | 45 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 7 deletions(-)
hw/9pfs/9p.c +38 -7
@@ -39,6 +39,11 @@#include "qemu/xxhash.h" #include <math.h> +#ifdef CONFIG_WIN32 +#define UTIME_NOW ((1l << 30) - 1l) +#define UTIME_OMIT ((1l << 30) - 2l) +#endif +int open_fd_hw;int total_open_fd;static int open_fd_rc;
@@ -132,13 +137,17 @@ static int dotl_to_open_flags(int flags)DotlOpenflagMap dotl_oflag_map[] = { {P9_DOTL_CREATE ,O_CREAT }, {P9_DOTL_EXCL ,O_EXCL }, +#ifndef CONFIG_WIN32 {P9_DOTL_NOCTTY ,O_NOCTTY }, +#endif {P9_DOTL_TRUNC ,O_TRUNC }, {P9_DOTL_APPEND ,O_APPEND }, +#ifndef CONFIG_WIN32 {P9_DOTL_NONBLOCK ,O_NONBLOCK } , {P9_DOTL_DSYNC ,O_DSYNC }, {P9_DOTL_FASYNC ,FASYNC }, -#ifndef CONFIG_DARWIN +#endif +#if !defined(CONFIG_DARWIN) && !defined(CONFIG_WIN32) {P9_DOTL_NOATIME ,O_NOATIME },/* * On Darwin, we could map to F_NOCACHE, which is
@@ -151,8 +160,10 @@ static int dotl_to_open_flags(int flags)#endif { P9_DOTL_LARGEFILE, O_LARGEFILE }, { P9_DOTL_DIRECTORY, O_DIRECTORY }, +#ifndef CONFIG_WIN32 { P9_DOTL_NOFOLLOW, O_NOFOLLOW }, { P9_DOTL_SYNC, O_SYNC }, +#endif };for (i =0 ; i <ARRAY_SIZE (dotl_oflag_map); i++) {
@@ -179,8 +190,11 @@ static int get_dotl_openflags(V9fsState *s, int oflags)* Filter the client open flags */ flags =dotl_to_open_flags (oflags); - flags &= ~(O_NOCTTY |O_ASYNC |O_CREAT ); -#ifndef CONFIG_DARWIN + flags &= ~(O_CREAT ); +#ifndef CONFIG_WIN32 + flags &= ~(O_NOCTTY |O_ASYNC ); +#endif +#if !defined(CONFIG_DARWIN ) && !defined(CONFIG_WIN32 )/* * Ignore direct disk access hint until the server supports it. */
@@ -1117,12 +1131,14 @@ static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)if (mode &P9_STAT_MODE_SYMLINK ) { ret |=S_IFLNK ; } +#ifndef CONFIG_WIN32 if (mode &P9_STAT_MODE_SOCKET ) { ret |=S_IFSOCK ; }if (mode &P9_STAT_MODE_NAMED_PIPE ) { ret |=S_IFIFO ; } +#endif if (mode &P9_STAT_MODE_DEVICE ) {if (extension->size && extension->data[0 ] =='c' ) { ret |=S_IFCHR ;
@@ -1203,6 +1219,7 @@ static uint32_t stat_to_v9mode(const struct stat *stbuf) mode |=P9_STAT_MODE_SYMLINK ; } +#ifndef CONFIG_WIN32 if (S_ISSOCK (stbuf->st_mode)) { mode |=P9_STAT_MODE_SOCKET ; }
@@ -1210,6 +1227,7 @@ static uint32_t stat_to_v9mode(const struct stat *stbuf)if (S_ISFIFO (stbuf->st_mode)) { mode |=P9_STAT_MODE_NAMED_PIPE ; } +#endif if (S_ISBLK (stbuf->st_mode) ||S_ISCHR (stbuf->st_mode)) { mode |=P9_STAT_MODE_DEVICE ;
@@ -1369,7 +1387,8 @@ static int stat_to_v9stat_dotl(V9fsPDU *pdu, const struct stat *stbuf, v9lstat->st_atime_nsec = stbuf->st_atimespec.tv_nsec; v9lstat->st_mtime_nsec = stbuf->st_mtimespec.tv_nsec; v9lstat->st_ctime_nsec = stbuf->st_ctimespec.tv_nsec; -#else +#endif +#ifdef CONFIG_LINUX v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec; v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec; v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec;
@@ -2492,6 +2511,7 @@ static int coroutine_fn v9fs_do_readdir(V9fsPDU *pdu, V9fsFidState *fidp,struct dirent *dent;struct stat *st;struct V9fsDirEnt *entries =NULL ; +unsigned char d_type =0 ;/* * inode remapping requires the device id, which in turn might be
@@ -2553,10 +2573,13 @@ static int coroutine_fn v9fs_do_readdir(V9fsPDU *pdu, V9fsFidState *fidp,v9fs_string_init (&name);v9fs_string_sprintf (&name,"%s" , dent->d_name); +#ifndef CONFIG_WIN32 + d_type = dent->d_type; +#endif /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */ len =pdu_marshal (pdu,11 + count,"Qqbs" , &qid, off, - dent->d_type, &name); + d_type, &name);v9fs_string_free (&name);
@@ -2912,8 +2935,12 @@ static void coroutine_fn v9fs_create(void *opaque)v9fs_path_copy (&fidp->path, &path);v9fs_path_unlock (s); }else if (perm &P9_STAT_MODE_SOCKET ) { +#ifndef CONFIG_WIN32 err =v9fs_co_mknod (pdu, fidp, &name, fidp->uid,-1 ,0 ,S_IFSOCK | (perm &0777 ), &stbuf); +#else + err = -ENOTSUP ; +#endif if (err <0 ) { goto out; }
@@ -3983,7 +4010,7 @@ out_nofid:#if defined(CONFIG_LINUX )/* Currently, only Linux has XATTR_SIZE_MAX */ #define P9_XATTR_SIZE_MAX XATTR_SIZE_MAX -#elif defined(CONFIG_DARWIN ) +#elif defined(CONFIG_DARWIN ) || defined(CONFIG_WIN32 )/* * Darwin doesn't seem to define a maximum xattr size in its user * space header, so manually configure it across platforms as 64k.
@@ -4000,6 +4027,8 @@ out_nofid: static void coroutine_fn v9fs_xattrcreate(void *opaque) { +V9fsPDU *pdu = opaque; +#ifndef CONFIG_WIN32 int flags, rflags =0 ;int32_t fid;uint64_t size;
@@ -4008,7 +4037,6 @@ static void coroutine_fn v9fs_xattrcreate(void *opaque)size_t offset =7 ;V9fsFidState *file_fidp;V9fsFidState *xattr_fidp; -V9fsPDU *pdu = opaque;v9fs_string_init (&name); err =pdu_unmarshal (pdu, offset,"dsqd" , &fid, &name, &size, &flags);
@@ -4061,6 +4089,9 @@ out_put_fid: out_nofid:pdu_complete (pdu, err);v9fs_string_free (&name); +#else +pdu_complete (pdu,-1 ); +#endif } static void coroutine_fn v9fs_readlink(void *opaque)
Signature · 1 line
2.25.1
PATCH v4 13/16 fsdev: Disable proxy fs driver on Windows
In reply to Bin MengFrom: Guohuai Shi <[email protected]>
We don't plan to support 'proxy' file system driver for 9pfs on Windows. Disable it for Windows build.
---
fsdev/qemu-fsdev.c | 2 ++ 1 file changed, 2 insertions(+)
fsdev/qemu-fsdev.c +2 -0
@@ -89,6 +89,7 @@ static FsDriverTable FsDrivers[] = { NULL }, }, +#ifndef CONFIG_WIN32 { .name = "proxy", .ops = &proxy_ops,
@@ -100,6 +101,7 @@ static FsDriverTable FsDrivers[] = {NULL }, }, +#endif }; static int validate_opt(void *opaque, const char *name, const char *value,
Signature · 1 line
2.25.1
PATCH v4 16/16 meson.build: Turn on virtfs for Windows
In reply to Bin MengFrom: Guohuai Shi <[email protected]>
Enable virtfs configuration option for Windows host.
---
meson.build | 10 +++++----- fsdev/meson.build | 1 + hw/9pfs/meson.build | 8 +++++--- 3 files changed, 11 insertions(+), 8 deletions(-)
meson.build +5 -5
@@ -1751,16 +1751,16 @@ dbus_display = get_option('dbus_display') \ .allowed() have_virtfs = get_option('virtfs') \ - .require(targetos == 'linux' or targetos == 'darwin', - error_message: 'virtio-9p (virtfs) requires Linux or macOS') \ - .require(targetos == 'linux' or cc.has_function('pthread_fchdir_np'), + .require(targetos == 'linux' or targetos == 'darwin' or targetos == 'windows', + error_message: 'virtio-9p (virtfs) requires Linux or macOS or Windows') \ + .require(targetos == 'linux' or targetos == 'windows' or cc.has_function('pthread_fchdir_np'), error_message: 'virtio-9p (virtfs) on macOS requires the presence of pthread_fchdir_np') \ - .require(targetos == 'darwin' or (libattr.found() and libcap_ng.found()), + .require(targetos == 'darwin' or targetos == 'windows' or (libattr.found() and libcap_ng.found()), error_message: 'virtio-9p (virtfs) on Linux requires libcap-ng-devel and libattr-devel') \ .disable_auto_if(not have_tools and not have_system) \ .allowed() -have_virtfs_proxy_helper = targetos != 'darwin' and have_virtfs and have_tools +have_virtfs_proxy_helper = targetos != 'darwin' and targetos != 'windows' and have_virtfs and have_tools if get_option('block_drv_ro_whitelist') == '' config_host_data.set('CONFIG_BDRV_RO_WHITELIST', '')
fsdev/meson.build +1 -0
@@ -8,6 +8,7 @@ fsdev_ss.add(when: ['CONFIG_FSDEV_9P'], if_true: files( ), if_false: files('qemu-fsdev-dummy.c')) softmmu_ss.add_all(when: 'CONFIG_LINUX', if_true: fsdev_ss) softmmu_ss.add_all(when: 'CONFIG_DARWIN', if_true: fsdev_ss) +softmmu_ss.add_all(when: 'CONFIG_WIN32', if_true: fsdev_ss) if have_virtfs_proxy_helper executable('virtfs-proxy-helper',
hw/9pfs/meson.build +5 -3
@@ -2,7 +2,6 @@ fs_ss = ss.source_set() fs_ss.add(files( '9p-local.c', '9p-posix-acl.c', - '9p-proxy.c', '9p-synth.c', '9p-xattr-user.c', '9p-xattr.c',
@@ -13,8 +12,11 @@ fs_ss.add(files( 'coth.c', 'coxattr.c', )) -fs_ss.add(when: 'CONFIG_LINUX', if_true: files('9p-util-linux.c')) -fs_ss.add(when: 'CONFIG_DARWIN', if_true: files('9p-util-darwin.c')) +fs_ss.add(when: 'CONFIG_LINUX', if_true: files('9p-proxy.c', + '9p-util-linux.c')) +fs_ss.add(when: 'CONFIG_DARWIN', if_true: files('9p-proxy.c', + '9p-util-darwin.c')) +fs_ss.add(when: 'CONFIG_WIN32', if_true: files('9p-util-win32.c')) fs_ss.add(when: 'CONFIG_XEN', if_true: files('xen-9p-backend.c')) softmmu_ss.add_all(when: 'CONFIG_FSDEV_9P', if_true: fs_ss)
Signature · 1 line
2.25.1
PATCH v4 14/16 hw/9pfs: Update synth fs driver for Windows
In reply to Bin MengFrom: Guohuai Shi <[email protected]>
Adapt synth fs driver for Windows in preparation to running qtest 9p testing on Windows.
---
hw/9pfs/9p-synth.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
hw/9pfs/9p-synth.c +4 -1
@@ -146,8 +146,10 @@ static void synth_fill_statbuf(V9fsSynthNode *node, struct stat *stbuf) stbuf->st_gid =0 ; stbuf->st_rdev =0 ; stbuf->st_size =0 ; +#ifndef CONFIG_WIN32 stbuf->st_blksize =0 ; stbuf->st_blocks =0 ; +#endif stbuf->st_atime =0 ; stbuf->st_mtime =0 ; stbuf->st_ctime =0 ;
@@ -230,7 +232,8 @@ static void synth_direntry(V9fsSynthNode *node, entry->d_ino = node->attr->inode;#ifdef CONFIG_DARWIN entry->d_seekoff = off +1 ; -#else +#endif +#ifdef CONFIG_LINUX entry->d_off = off +1 ;#endif }
Signature · 1 line
2.25.1
PATCH v4 7/16 hw/9pfs: Update helper qemu_stat_rdev()
In reply to Bin MengAs Windows host does not have stat->st_rdev field, we use the first 3 characters of the root path to build a device id.
---
hw/9pfs/9p-util.h | 22 +++++++++++++++++++--- hw/9pfs/9p-util-win32.c | 18 ++++++++++++++++++ hw/9pfs/9p.c | 5 +++-- 3 files changed, 40 insertions(+), 5 deletions(-)
hw/9pfs/9p-util.h +19 -3
@@ -22,8 +22,9 @@/* forward declaration */ union V9fsFidOpenState ;struct V9fsState ; +struct FsContext ; -#if !defined(CONFIG_LINUX ) +#ifdef CONFIG_DARWIN /* * Generates a Linux device number (a.k.a. dev_t) for given device major
@@ -55,10 +56,12 @@ static inline uint64_t makedev_dotl(uint32_t dev_major, uint32_t dev_minor)*/ static inline uint64_t host_dev_to_dotl_dev (dev_t dev) { -#ifdef CONFIG_LINUX +#if defined(CONFIG_LINUX ) || defined(CONFIG_WIN32 )return dev; -#else +#elif defined(CONFIG_DARWIN )return makedev_dotl (major (dev),minor (dev)); +#else +#error Missing host_dev_to_dotl_dev() implementation for this host system#endif }
@@ -152,6 +155,7 @@ void rewinddir_win32(DIR *pDir);void seekdir_win32 (DIR *pDir,long pos);long telldir_win32 (DIR *pDir);off_t qemu_dirent_off_win32 (struct V9fsState *s,union V9fsFidOpenState *fs); +uint64_t qemu_stat_rdev_win32 (struct FsContext *fs_ctx);#endif static inline void close_preserve_errno (int fd)
@@ -269,6 +273,18 @@ static inline struct dirent *qemu_dirent_dup(struct dirent *dent)return g_memdup (dent, sz); } +static inline uint64_t qemu_stat_rdev (const struct stat *stbuf, +struct FsContext *fs_ctx) +{ +#if defined(CONFIG_LINUX ) || defined(CONFIG_DARWIN ) +return stbuf->st_rdev; +#elif defined(CONFIG_WIN32 ) +return qemu_stat_rdev_win32 (fs_ctx); +#else +#error Missing qemu_stat_rdev() implementation for this host system +#endif +} +/* * As long as mknodat is not available on macOS, this workaround * using pthread_fchdir_np is needed. qemu_mknodat is defined in
hw/9pfs/9p-util-win32.c +18 -0
@@ -1278,3 +1278,21 @@ off_t qemu_dirent_off_win32(struct V9fsState *s, union V9fsFidOpenState *fs) {return s->ops->telldir (&s->ctx, fs); } + +uint64_t qemu_stat_rdev_win32 (struct FsContext *fs_ctx) +{ +uint64_t rdev =0 ; +LocalData *data = fs_ctx->private; + +/* +* As Windows host does not have stat->st_rdev field, we use the first +* 3 characters of the root path to build a device id. +* +* (Windows root path always starts from a driver letter like "C:\") +*/ +if (data) { +memcpy (&rdev, data->root_path,3 ); + } + +return rdev; +}
hw/9pfs/9p.c +3 -2
@@ -1266,7 +1266,8 @@ static int coroutine_fn stat_to_v9stat(V9fsPDU *pdu, V9fsPath *path, }else if (v9stat->mode &P9_STAT_MODE_DEVICE ) {v9fs_string_sprintf (&v9stat->extension,"%c %u %u" ,S_ISCHR (stbuf->st_mode) ?'c' :'b' , -major (stbuf->st_rdev),minor (stbuf->st_rdev)); +major (qemu_stat_rdev (stbuf, &pdu->s->ctx)), +minor (qemu_stat_rdev (stbuf, &pdu->s->ctx))); }else if (S_ISDIR (stbuf->st_mode) ||S_ISREG (stbuf->st_mode)) {v9fs_string_sprintf (&v9stat->extension,"%s %lu" ,"HARDLINKCOUNT" , (unsigned long )stbuf->st_nlink);
@@ -1346,7 +1347,7 @@ static int stat_to_v9stat_dotl(V9fsPDU *pdu, const struct stat *stbuf, v9lstat->st_nlink = stbuf->st_nlink; v9lstat->st_uid = stbuf->st_uid; v9lstat->st_gid = stbuf->st_gid; - v9lstat->st_rdev =host_dev_to_dotl_dev (stbuf->st_rdev); + v9lstat->st_rdev =host_dev_to_dotl_dev (rdev); v9lstat->st_size = stbuf->st_size; v9lstat->st_blksize =stat_to_iounit (pdu, stbuf); v9lstat->st_blocks = stbuf->st_blocks;
Signature · 1 line
2.25.1
PATCH v4 8/16 hw/9pfs: Add a helper qemu_stat_blksize()
In reply to Bin MengAs Windows host does not have stat->st_blksize field, we use the one we calculated in init_win32_root_directory().
Add a helper qemu_stat_blksize() and use it to avoid direct access to stat->st_blksize.
---
hw/9pfs/9p-util.h | 13 +++++++++++++ hw/9pfs/9p-util-win32.c | 7 +++++++ hw/9pfs/9p.c | 13 ++++++++++++- 3 files changed, 32 insertions(+), 1 deletion(-)
hw/9pfs/9p-util.h +13 -0
@@ -156,6 +156,7 @@ void seekdir_win32(DIR *pDir, long pos);long telldir_win32 (DIR *pDir);off_t qemu_dirent_off_win32 (struct V9fsState *s,union V9fsFidOpenState *fs);uint64_t qemu_stat_rdev_win32 (struct FsContext *fs_ctx); +uint64_t qemu_stat_blksize_win32 (struct FsContext *fs_ctx);#endif static inline void close_preserve_errno (int fd)
@@ -285,6 +286,18 @@ static inline uint64_t qemu_stat_rdev(const struct stat *stbuf,#endif } +static inline uint64_t qemu_stat_blksize (const struct stat *stbuf, +struct FsContext *fs_ctx) +{ +#if defined(CONFIG_LINUX ) || defined(CONFIG_DARWIN ) +return stbuf->st_blksize; +#elif defined(CONFIG_WIN32 ) +return qemu_stat_blksize_win32 (fs_ctx); +#else +#error Missing qemu_stat_blksize() implementation for this host system +#endif +} +/* * As long as mknodat is not available on macOS, this workaround * using pthread_fchdir_np is needed. qemu_mknodat is defined in
hw/9pfs/9p-util-win32.c +7 -0
@@ -1296,3 +1296,10 @@ uint64_t qemu_stat_rdev_win32(struct FsContext *fs_ctx)return rdev; } + +uint64_t qemu_stat_blksize_win32 (struct FsContext *fs_ctx) +{ +LocalData *data = fs_ctx->private; + +return data ? (uint64_t )data->block_size :0 ; +}
hw/9pfs/9p.c +12 -1
@@ -1335,12 +1335,14 @@ static int32_t blksize_to_iounit(const V9fsPDU *pdu, int32_t blksize)static int32_t stat_to_iounit (const V9fsPDU *pdu,const struct stat *stbuf) { -return blksize_to_iounit (pdu, stbuf->st_blksize); +return blksize_to_iounit (pdu,qemu_stat_blksize (stbuf, &pdu->s->ctx)); }static int stat_to_v9stat_dotl (V9fsPDU *pdu,const struct stat *stbuf,V9fsStatDotl *v9lstat) { +dev_t rdev =qemu_stat_rdev (stbuf, &pdu->s->ctx); +memset (v9lstat,0 ,sizeof (*v9lstat)); v9lstat->st_mode = stbuf->st_mode;
@@ -1350,7 +1352,16 @@ static int stat_to_v9stat_dotl(V9fsPDU *pdu, const struct stat *stbuf, v9lstat->st_rdev =host_dev_to_dotl_dev (rdev); v9lstat->st_size = stbuf->st_size; v9lstat->st_blksize =stat_to_iounit (pdu, stbuf); +#if defined(CONFIG_LINUX ) || defined(CONFIG_DARWIN ) v9lstat->st_blocks = stbuf->st_blocks; +#elif defined(CONFIG_WIN32 ) +if (v9lstat->st_blksize ==0 ) { + v9lstat->st_blocks =0 ; + }else { + v9lstat->st_blocks =ROUND_UP (v9lstat->st_size / v9lstat->st_blksize, + v9lstat->st_blksize); + } +#endif v9lstat->st_atime_sec = stbuf->st_atime; v9lstat->st_mtime_sec = stbuf->st_mtime; v9lstat->st_ctime_sec = stbuf->st_ctime;
Signature · 1 line
2.25.1
PATCH v4 12/16 hw/9pfs: Translate Windows errno to Linux value
In reply to Bin MengFrom: Guohuai Shi <[email protected]>
Some of Windows error numbers have different value from Linux ones. For example, ENOTEMPTY is defined to 39 in Linux, but is defined to 41 in Windows. So deleting a directory from a Linux guest on top of QEMU from a Windows host complains:
# rmdir tmp rmdir: 'tmp': Unknown error 41
This commit provides error number translation from Windows to Linux. It can make Linux guest OS happy with the error number when running on top of QEMU from a Windows host.
---
hw/9pfs/9p-util.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
hw/9pfs/9p-util.h +10 -2
@@ -72,9 +72,9 @@ static inline int errno_to_dotl(int err) {#if defined(CONFIG_LINUX )/* nothing to translate (Linux -> Linux) */ -#elif defined(CONFIG_DARWIN ) +#elif defined(CONFIG_DARWIN ) || defined(CONFIG_WIN32 )/* -* translation mandatory for macOS hosts +* translation mandatory for different hosts * * FIXME: Only most important errnos translated here yet, this should be * extended to as many errnos being translated as possible in future.
@@ -83,9 +83,17 @@ static inline int errno_to_dotl(int err)case ENAMETOOLONG :return L_ENAMETOOLONG ;case ENOTEMPTY :return L_ENOTEMPTY ;case ELOOP :return L_ELOOP ; +#ifdef CONFIG_DARWIN case ENOATTR :return L_ENODATA ; case ENOTSUP return L_EOPNOTSUPP;case EOPNOTSUPP :return L_EOPNOTSUPP ; +#endif +#ifdef CONFIG_WIN32 +case EDEADLK :return L_EDEADLK ; +case ENOLCK :return L_ENOLCK ; +case ENOSYS :return L_ENOSYS ; +case EILSEQ :return L_EILSEQ ; +#endif }#else #error Missing errno translation to Linux for this host system
Signature · 1 line
2.25.1
PATCH v4 10/16 hw/9pfs: Update v9fs_set_fd_limit() for Windows
In reply to Bin MengFrom: Guohuai Shi <[email protected]>
Use _getmaxstdio() to set the fd limit on Windows.
---
hw/9pfs/9p.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-)
hw/9pfs/9p.c +20 -3
@@ -4396,11 +4396,28 @@ void v9fs_reset(V9fsState *s)static void __attribute__((__constructor__))v9fs_set_fd_limit (void ) { +int rlim_cur; +int ret; + +#ifndef CONFIG_WIN32 struct rlimit rlim; -if (getrlimit (RLIMIT_NOFILE , &rlim) <0 ) { + ret =getrlimit (RLIMIT_NOFILE , &rlim); + rlim_cur = rlim.rlim_cur; +#else +/* +* On Windows host, _getmaxstdio() actually returns the number of max +* open files at the stdio level. It *may* be smaller than the number +* of open files by open() or CreateFile(). +*/ + ret =_getmaxstdio (); + rlim_cur = ret; +#endif + +if (ret <0 ) {error_report ("Failed to get the resource limit" );exit (1 ); } - open_fd_hw = rlim.rlim_cur -MIN (400 , rlim.rlim_cur /3 ); - open_fd_rc = rlim.rlim_cur /2 ; + + open_fd_hw = rlim_cur -MIN (400 , rlim_cur /3 ); + open_fd_rc = rlim_cur /2 ; }
Signature · 1 line
2.25.1
PATCH v4 2/16 hw/9pfs: Implement Windows specific utilities functions for 9pfs
In reply to Bin MengFrom: Guohuai Shi <[email protected]>
Windows POSIX API and MinGW library do not provide the NO_FOLLOW flag, and do not allow opening a directory by POSIX open(). This causes all xxx_at() functions cannot work directly. However, we can provide Windows handle based functions to emulate xxx_at() functions (e.g.: openat_win32, utimensat_win32, etc.).
NTFS ADS (Alternate Data Streams) is used to emulate 9pfs extended attributes on Windows. Symbolic link is only supported when security model is "mapped-xattr" or "mapped-file".
---
hw/9pfs/9p-local.h | 7 + hw/9pfs/9p-util.h | 32 +- hw/9pfs/9p-local.c | 4 - hw/9pfs/9p-util-win32.c | 979 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 1017 insertions(+), 5 deletions(-) create mode 100644 hw/9pfs/9p-util-win32.c
hw/9pfs/9p-local.h +7 -0
@@ -13,6 +13,13 @@#ifndef QEMU_9P_LOCAL_H #define QEMU_9P_LOCAL_H +typedef struct { +int mountfd; +#ifdef CONFIG_WIN32 +char *root_path; +#endif +}LocalData ; +int local_open_nofollow (FsContext *fs_ctx,const char *path,int flags,mode_t mode);int local_opendir_nofollow (FsContext *fs_ctx,const char *path);
hw/9pfs/9p-util.h +31 -1
@@ -88,18 +88,46 @@ static inline int errno_to_dotl(int err) {return err; } -#ifdef CONFIG_DARWIN +#if defined(CONFIG_DARWIN )#define qemu_fgetxattr (...) fgetxattr(__VA_ARGS__, 0, 0) +#elif defined(CONFIG_WIN32 ) +#define qemu_fgetxattr fgetxattr_win32#else #define qemu_fgetxattr fgetxattr#endif +#ifdef CONFIG_WIN32 +#define qemu_openat openat_win32 +#define qemu_fstatat fstatat_win32 +#define qemu_mkdirat mkdirat_win32 +#define qemu_renameat renameat_win32 +#define qemu_utimensat utimensat_win32 +#define qemu_unlinkat unlinkat_win32 +#else #define qemu_openat openat#define qemu_fstatat fstatat#define qemu_mkdirat mkdirat#define qemu_renameat renameat#define qemu_utimensat utimensat#define qemu_unlinkat unlinkat +#endif + +#ifdef CONFIG_WIN32 +char *get_full_path_win32 (HANDLE hDir,const char *name); +ssize_t fgetxattr_win32 (int fd,const char *name,void *value,size_t size); +int openat_win32 (int dirfd,const char *pathname,int flags,mode_t mode); +int fstatat_win32 (int dirfd,const char *pathname, +struct stat *statbuf,int flags); +int mkdirat_win32 (int dirfd,const char *pathname,mode_t mode); +int renameat_win32 (int olddirfd,const char *oldpath, +int newdirfd,const char *newpath); +int utimensat_win32 (int dirfd,const char *pathname, +const struct timespec times[2 ],int flags); +int unlinkat_win32 (int dirfd,const char *pathname,int flags); +int statfs_win32 (const char *root_path,struct statfs *stbuf); +int openat_dir (int dirfd,const char *name); +int openat_file (int dirfd,const char *name,int flags,mode_t mode); +#endif static inline void close_preserve_errno (int fd) {
@@ -108,6 +136,7 @@ static inline void close_preserve_errno(int fd) errno = serrno; } +#ifndef CONFIG_WIN32 static inline int openat_dir (int dirfd,const char *name) { return qemu_openat(dirfd, name,
@@ -154,6 +183,7 @@ again: errno = serrno; return fd; } +#endif ssize_t fgetxattrat_nofollow (int dirfd,const char *path,const char *name,void *value,size_t size);
hw/9pfs/9p-local.c +0 -4
@@ -53,10 +53,6 @@#define BTRFS_SUPER_MAGIC 0x9123683E#endif -typedef struct { -int mountfd; -}LocalData ; -int local_open_nofollow (FsContext *fs_ctx,const char *path,int flags,mode_t mode) {
hw/9pfs/9p-util-win32.c new file +979 -0
979 lines not on this page · raw message
Signature · 1 line
2.25.1
v4 cover hw/9pfs: Add 9pfs support for Windows
At present there is no Windows support for 9p file system. This series adds initial Windows support for 9p file system.
'local' file system backend driver is supported on Windows, including open, read, write, close, rename, remove, etc. All security models are supported. The mapped (mapped-xattr) security model is implemented using NTFS Alternate Data Stream (ADS) so the 9p export path shall be on an NTFS partition.
'synth' driver is adapted for Windows too so that we can now run qtests on Windows for 9p related regression testing.
Example command line to test:
"-fsdev local,path=c:\msys64,security_model=mapped,id=p9 -device virtio-9p-pci,fsdev=p9,mount_tag=p9fs"
Base-commit: 13356edb87506c148b163b8c7eb0695647d00c2a
Changes in v4:
- Fixed 9pfs mounted as read-only issue on Windows host, adding a win32_error_to_posix() to translate Windows native API error to POSIX one.
- Fixed errors of handling symbolic links
- Added forward declaration to avoid using 'void *'
- Implemented Windows specific xxxdir() APIs for safe directory access
Bin Meng (2):
hw/9pfs: Update helper qemu_stat_rdev()
hw/9pfs: Add a helper qemu_stat_blksize()
Guohuai Shi (14):
hw/9pfs: Add missing definitions for Windows
hw/9pfs: Implement Windows specific utilities functions for 9pfs hw/9pfs: Replace the direct call to xxxdir() APIs with a wrapper hw/9pfs: Implement Windows specific xxxdir() APIs hw/9pfs: Update the local fs driver to support Windows hw/9pfs: Support getting current directory offset for Windows hw/9pfs: Disable unsupported flags and features for Windows hw/9pfs: Update v9fs_set_fd_limit() for Windows hw/9pfs: Add Linux error number definition
hw/9pfs: Translate Windows errno to Linux value
fsdev: Disable proxy fs driver on Windows
hw/9pfs: Update synth fs driver for Windows tests/qtest: virtio-9p-test: Adapt the case for win32 meson.build: Turn on virtfs for Windows
meson.build | 10 +- fsdev/file-op-9p.h | 33 + hw/9pfs/9p-linux-errno.h | 151 +++ hw/9pfs/9p-local.h | 8 + hw/9pfs/9p-util.h | 139 ++- hw/9pfs/9p.h | 43 + tests/qtest/libqos/virtio-9p-client.h | 7 + fsdev/qemu-fsdev.c | 2 + hw/9pfs/9p-local.c | 269 ++++- hw/9pfs/9p-synth.c | 5 +- hw/9pfs/9p-util-win32.c | 1305 +++++++++++++++++++++++++ hw/9pfs/9p.c | 90 +- hw/9pfs/codir.c | 2 +- fsdev/meson.build | 1 + hw/9pfs/meson.build | 8 +- 15 files changed, 2008 insertions(+), 65 deletions(-) create mode 100644 hw/9pfs/9p-linux-errno.h create mode 100644 hw/9pfs/9p-util-win32.c
Signature · 1 line
2.25.1
PATCH v4 1/16 hw/9pfs: Add missing definitions for Windows
From: Guohuai Shi <[email protected]>
Some definitions currently used by the 9pfs codes are only available on POSIX platforms. Let's add our own ones in preparation to adding 9pfs support for Windows.
---
fsdev/file-op-9p.h | 33 +++++++++++++++++++++++++++++++++ hw/9pfs/9p.h | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+)
fsdev/file-op-9p.h +33 -0
@@ -27,6 +27,39 @@# include <sys/mount.h> #endif +#ifdef CONFIG_WIN32 + +/* POSIX structure not defined in Windows */ + +typedef uint32_t uid_t ; +typedef uint32_t gid_t ; + +/* from http://man7.org/linux/man-pages/man2/statfs.2.html */ +typedef uint32_t __fsword_t ; +typedef uint32_t fsblkcnt_t ; +typedef uint32_t fsfilcnt_t ; + +/* from linux/include/uapi/asm-generic/posix_types.h */ +typedef struct { +long __val[2 ]; +}fsid_t ; + +struct statfs { +__fsword_t f_type; +__fsword_t f_bsize; +fsblkcnt_t f_blocks; +fsblkcnt_t f_bfree; +fsblkcnt_t f_bavail; +fsfilcnt_t f_files; +fsfilcnt_t f_ffree; +fsid_t f_fsid; +__fsword_t f_namelen; +__fsword_t f_frsize; +__fsword_t f_flags; +}; + +#endif /* CONFIG_WIN32 */ +#define SM_LOCAL_MODE_BITS 0600#define SM_LOCAL_DIR_MODE_BITS 0700
hw/9pfs/9p.h +43 -0
56 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH v4 6/16 hw/9pfs: Support getting current directory offset for Windows
In reply to Bin MengFrom: Guohuai Shi <[email protected]>
On Windows 'struct dirent' does not have current directory offset. Update qemu_dirent_off() to support Windows.
While we are here, add a build time check to error out if a new host does not implement this helper.
---
hw/9pfs/9p-util.h | 16 +++++++++++++--- hw/9pfs/9p-util-win32.c | 5 +++++ hw/9pfs/9p.c | 4 ++-- hw/9pfs/codir.c | 2 +- 4 files changed, 21 insertions(+), 6 deletions(-)
hw/9pfs/9p-util.h +13 -3
@@ -19,6 +19,10 @@#define O_PATH_9P_UTIL 0#endif +/* forward declaration */ +union V9fsFidOpenState ; +struct V9fsState ; +#if !defined(CONFIG_LINUX )/*
@@ -147,6 +151,7 @@ struct dirent *readdir_win32(DIR *pDir);void rewinddir_win32 (DIR *pDir);void seekdir_win32 (DIR *pDir,long pos);long telldir_win32 (DIR *pDir); +off_t qemu_dirent_off_win32 (struct V9fsState *s,union V9fsFidOpenState *fs);#endif static inline void close_preserve_errno (int fd)
@@ -220,12 +225,17 @@ ssize_t fremovexattrat_nofollow(int dirfd, const char *filename,* so ensure it is manually injected earlier and call here when * needed. */ -static inline off_t qemu_dirent_off (struct dirent *dent) +static inline off_t qemu_dirent_off (struct dirent *dent,struct V9fsState *s, +union V9fsFidOpenState *fs) { -#ifdef CONFIG_DARWIN +#if defined(CONFIG_DARWIN )return dent->d_seekoff; -#else +#elif defined(CONFIG_LINUX )return dent->d_off; +#elif defined(CONFIG_WIN32 ) +return qemu_dirent_off_win32 (s, fs); +#else +#error Missing qemu_dirent_off() implementation for this host system#endif }
hw/9pfs/9p-util-win32.c +5 -0
@@ -1273,3 +1273,8 @@ long telldir_win32(DIR *pDir)return (long )stream->offset; } + +off_t qemu_dirent_off_win32 (struct V9fsState *s,union V9fsFidOpenState *fs) +{ +return s->ops->telldir (&s->ctx, fs); +}
hw/9pfs/9p.c +2 -2
@@ -2336,7 +2336,7 @@ static int coroutine_fn v9fs_do_readdir_with_stat(V9fsPDU *pdu, count += len;v9fs_stat_free (&v9stat);v9fs_path_free (&path); - saved_dir_pos =qemu_dirent_off (dent); + saved_dir_pos =qemu_dirent_off (dent, pdu->s, &fidp->fs); }v9fs_readdir_unlock (&fidp->fs.dir);
@@ -2537,7 +2537,7 @@ static int coroutine_fn v9fs_do_readdir(V9fsPDU *pdu, V9fsFidState *fidp, qid.version =0 ; } - off =qemu_dirent_off (dent); + off =qemu_dirent_off (dent, pdu->s, &fidp->fs);v9fs_string_init (&name);v9fs_string_sprintf (&name,"%s" , dent->d_name);
hw/9pfs/codir.c +1 -1
@@ -167,7 +167,7 @@ static int do_readdir_many(V9fsPDU *pdu, V9fsFidState *fidp, } size += len; - saved_dir_pos =qemu_dirent_off (dent); + saved_dir_pos =qemu_dirent_off (dent, s, &fidp->fs); }/* restore (last) saved position */
Signature · 1 line
2.25.1
PATCH v4 11/16 hw/9pfs: Add Linux error number definition
In reply to Bin MengFrom: Guohuai Shi <[email protected]>
When using 9p2000.L protocol, the errno should use the Linux errno. Currently magic numbers with comments are used. Replace these with macros for future expansion.
---
hw/9pfs/9p-linux-errno.h | 151 +++++++++++++++++++++++++++++++++++++++ hw/9pfs/9p-util.h | 24 +++---- 2 files changed, 162 insertions(+), 13 deletions(-) create mode 100644 hw/9pfs/9p-linux-errno.h
hw/9pfs/9p-linux-errno.h new file +151 -0
@@ -0,0 +1,151 @@ +/* +* 9p Linux errno translation definition +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +*/ + +#include <errno.h> + +#ifndef QEMU_9P_LINUX_ERRNO_H +#define QEMU_9P_LINUX_ERRNO_H + +/* +* This file contains the Linux errno definitions to translate errnos set by +* the 9P server (running on non-Linux hosts) to a corresponding errno value. +* +* This list should be periodically reviewed and updated; particularly for +* errnos that might be set as a result of a file system operation. +*/ + +#define L_EPERM 1/* Operation not permitted */ +#define L_ENOENT 2/* No such file or directory */ +#define L_ESRCH 3/* No such process */ +#define L_EINTR 4/* Interrupted system call */ +#define L_EIO 5/* I/O error */ +#define L_ENXIO 6/* No such device or address */ +#define L_E2BIG 7/* Argument list too long */ +#define L_ENOEXEC 8/* Exec format error */ +#define L_EBADF 9/* Bad file number */ +#define L_ECHILD 10/* No child processes */ +#define L_EAGAIN 11/* Try again */ +#define L_ENOMEM 12/* Out of memory */ +#define L_EACCES 13/* Permission denied */ +#define L_EFAULT 14/* Bad address */ +#define L_ENOTBLK 15/* Block device required */ +#define L_EBUSY 16/* Device or resource busy */ +#define L_EEXIST 17/* File exists */ +#define L_EXDEV 18/* Cross-device link */ +#define L_ENODEV 19/* No such device */ +#define L_ENOTDIR 20/* Not a directory */ +#define L_EISDIR 21/* Is a directory */ +#define L_EINVAL 22/* Invalid argument */ +#define L_ENFILE 23/* File table overflow */ +#define L_EMFILE 24/* Too many open files */ +#define L_ENOTTY 25/* Not a typewriter */ +#define L_ETXTBSY 26/* Text file busy */ +#define L_EFBIG 27/* File too large */ +#define L_ENOSPC 28/* No space left on device */ +#define L_ESPIPE 29/* Illegal seek */ +#define L_EROFS 30/* Read-only file system */ +#define L_EMLINK 31/* Too many links */ +#define L_EPIPE 32/* Broken pipe */ +#define L_EDOM 33/* Math argument out of domain of func */ +#define L_ERANGE 34/* Math result not representable */ +#define L_EDEADLK 35/* Resource deadlock would occur */ +#define L_ENAMETOOLONG 36/* File name too long */ +#define L_ENOLCK 37/* No record locks available */ +#define L_ENOSYS 38/* Function not implemented */ +#define L_ENOTEMPTY 39/* Directory not empty */ +#define L_ELOOP 40/* Too many symbolic links encountered */ +#define L_ENOMSG 42/* No message of desired type */ +#define L_EIDRM 43/* Identifier removed */ +#define L_ECHRNG 44/* Channel number out of range */ +#define L_EL2NSYNC 45/* Level 2 not synchronized */ +#define L_EL3HLT 46/* Level 3 halted */ +#define L_EL3RST 47/* Level 3 reset */ +#define L_ELNRNG 48/* Link number out of range */ +#define L_EUNATCH 49/* Protocol driver not attached */ +#define L_ENOCSI 50/* No CSI structure available */ +#define L_EL2HLT 51/* Level 2 halted */ +#define L_EBADE 52/* Invalid exchange */ +#define L_EBADR 53/* Invalid request descriptor */ +#define L_EXFULL 54/* Exchange full */ +#define L_ENOANO 55/* No anode */ +#define L_EBADRQC 56/* Invalid request code */ +#define L_EBADSLT 57/* Invalid slot */ +#define L_EBFONT 58/* Bad font file format */ +#define L_ENOSTR 59/* Device not a stream */ +#define L_ENODATA 61/* No data available */ +#define L_ETIME 62/* Timer expired */ +#define L_ENOSR 63/* Out of streams resources */ +#define L_ENONET 64/* Machine is not on the network */ +#define L_ENOPKG 65/* Package not installed */ +#define L_EREMOTE 66/* Object is remote */ +#define L_ENOLINK 67/* Link has been severed */ +#define L_EADV 68/* Advertise error */ +#define L_ESRMNT 69/* Srmount error */ +#define L_ECOMM 70/* Communication error on send */ +#define L_EPROTO 71/* Protocol error */ +#define L_EMULTIHOP 72/* Multihop attempted */ +#define L_EDOTDOT 73/* RFS specific error */ +#define L_EBADMSG 74/* Not a data message */ +#define L_EOVERFLOW 75/* Value too large for defined data type */ +#define L_ENOTUNIQ 76/* Name not unique on network */ +#define L_EBADFD 77/* File descriptor in bad state */ +#define L_EREMCHG 78/* Remote address changed */ +#define L_ELIBACC 79/* Can not access a needed shared library */ +#define L_ELIBBAD 80/* Accessing a corrupted shared library */ +#define L_ELIBSCN 81/* .lib section in a.out corrupted */ +#define L_ELIBMAX 82/* Attempting to link in too many shared libs */ +#define L_ELIBEXEC 83/* Cannot exec a shared library directly */ +#define L_EILSEQ 84/* Illegal byte sequence */ +#define L_ERESTART 85/* Interrupted system call should be restarted */ +#define L_ESTRPIPE 86/* Streams pipe error */ +#define L_EUSERS 87/* Too many users */ +#define L_ENOTSOCK 88/* Socket operation on non-socket */ +#define L_EDESTADDRREQ 89/* Destination address required */ +#define L_EMSGSIZE 90/* Message too long */ +#define L_EPROTOTYPE 91/* Protocol wrong type for socket */ +#define L_ENOPROTOOPT 92/* Protocol not available */ +#define L_EPROTONOSUPPORT 93/* Protocol not supported */ +#define L_ESOCKTNOSUPPORT 94/* Socket type not supported */ +#define L_EOPNOTSUPP 95/* Operation not supported on transport endpoint */ +#define L_EPFNOSUPPORT 96/* Protocol family not supported */ +#define L_EAFNOSUPPORT 97/* Address family not supported by protocol */ +#define L_EADDRINUSE 98/* Address already in use */ +#define L_EADDRNOTAVAIL 99/* Cannot assign requested address */ +#define L_ENETDOWN 100/* Network is down */ +#define L_ENETUNREACH 101/* Network is unreachable */ +#define L_ENETRESET 102/* Network dropped connection because of reset */ +#define L_ECONNABORTED 103/* Software caused connection abort */ +#define L_ECONNRESET 104/* Connection reset by peer */ +#define L_ENOBUFS 105/* No buffer space available */ +#define L_EISCONN 106/* Transport endpoint is already connected */ +#define L_ENOTCONN 107/* Transport endpoint is not connected */ +#define L_ESHUTDOWN 108/* Cannot send after transport endpoint shutdown */ +#define L_ETOOMANYREFS 109/* Too many references: cannot splice */ +#define L_ETIMEDOUT 110/* Connection timed out */ +#define L_ECONNREFUSED 111/* Connection refused */ +#define L_EHOSTDOWN 112/* Host is down */ +#define L_EHOSTUNREACH 113/* No route to host */ +#define L_EALREADY 114/* Operation already in progress */ +#define L_EINPROGRESS 115/* Operation now in progress */ +#define L_ESTALE 116/* Stale NFS file handle */ +#define L_EUCLEAN 117/* Structure needs cleaning */ +#define L_ENOTNAM 118/* Not a XENIX named type file */ +#define L_ENAVAIL 119/* No XENIX semaphores available */ +#define L_EISNAM 120/* Is a named type file */ +#define L_EREMOTEIO 121/* Remote I/O error */ +#define L_EDQUOT 122/* Quota exceeded */ +#define L_ENOMEDIUM 123/* No medium found */ +#define L_EMEDIUMTYPE 124/* Wrong medium type */ +#define L_ECANCELED 125/* Operation Canceled */ +#define L_ENOKEY 126/* Required key not available */ +#define L_EKEYEXPIRED 127/* Key has expired */ +#define L_EKEYREVOKED 128/* Key has been revoked */ +#define L_EKEYREJECTED 129/* Key was rejected by service */ +#define L_EOWNERDEAD 130/* Owner died */ +#define L_ENOTRECOVERABLE 131/* State not recoverable */ + +#endif /* QEMU_9P_LINUX_ERRNO_H */
hw/9pfs/9p-util.h +11 -13
@@ -65,8 +65,11 @@ static inline uint64_t host_dev_to_dotl_dev(dev_t dev)#endif } +#include "9p-linux-errno.h" +/* Translates errno from host -> Linux if needed */ -static inline int errno_to_dotl (int err) { +static inline int errno_to_dotl (int err) +{#if defined(CONFIG_LINUX )/* nothing to translate (Linux -> Linux) */ #elif defined(CONFIG_DARWIN )
@@ -76,18 +79,13 @@ static inline int errno_to_dotl(int err) {* FIXME: Only most important errnos translated here yet, this should be * extended to as many errnos being translated as possible in future. */ -if (err ==ENAMETOOLONG ) { - err =36 ;/* ==ENAMETOOLONG on Linux */ - }else if (err ==ENOTEMPTY ) { - err =39 ;/* ==ENOTEMPTY on Linux */ - }else if (err ==ELOOP ) { - err =40 ;/* ==ELOOP on Linux */ - }else if (err ==ENOATTR ) { - err =61 ;/* ==ENODATA on Linux */ - }else if (err ==ENOTSUP ) { - err =95 ;/* ==EOPNOTSUPP on Linux */ - }else if (err ==EOPNOTSUPP ) { - err =95 ;/* ==EOPNOTSUPP on Linux */ +switch (err) { +case ENAMETOOLONG :return L_ENAMETOOLONG ; +case ENOTEMPTY :return L_ENOTEMPTY ; +case ELOOP :return L_ELOOP ; +case ENOATTR :return L_ENODATA ; + case ENOTSUP return L_EOPNOTSUPP; +case EOPNOTSUPP :return L_EOPNOTSUPP ; }#else #error Missing errno translation to Linux for this host system
Signature · 1 line
2.25.1
PATCH v4 3/16 hw/9pfs: Replace the direct call to xxxdir() APIs with a wrapper
In reply to Bin MengFrom: Guohuai Shi <[email protected]>
xxxdir() APIs are not safe on Windows host. For future extension to Windows, let's replace the direct call to xxxdir() APIs with a wrapper.
---
hw/9pfs/9p-util.h | 14 ++++++++++++++ hw/9pfs/9p-local.c | 12 ++++++------ 2 files changed, 20 insertions(+), 6 deletions(-)
hw/9pfs/9p-util.h +14 -0
@@ -103,6 +103,13 @@ static inline int errno_to_dotl(int err) {#define qemu_renameat renameat_win32#define qemu_utimensat utimensat_win32#define qemu_unlinkat unlinkat_win32 + +#define qemu_opendir opendir_win32 +#define qemu_closedir closedir_win32 +#define qemu_readdir readdir_win32 +#define qeme_rewinddir rewinddir_win32 +#define qemu_seekdir seekdir_win32 +#define qemu_telldir telldir_win32#else #define qemu_openat openat#define qemu_fstatat fstatat
@@ -110,6 +117,13 @@ static inline int errno_to_dotl(int err) {#define qemu_renameat renameat#define qemu_utimensat utimensat#define qemu_unlinkat unlinkat + +#define qemu_opendir opendir +#define qemu_closedir closedir +#define qemu_readdir readdir +#define qeme_rewinddir rewinddir +#define qemu_seekdir seekdir +#define qemu_telldir telldir#endif #ifdef CONFIG_WIN32
hw/9pfs/9p-local.c +6 -6
@@ -495,7 +495,7 @@ static int local_close(FsContext *ctx, V9fsFidOpenState *fs)static int local_closedir (FsContext *ctx,V9fsFidOpenState *fs) { -return closedir (fs->dir.stream); +return qemu_closedir (fs->dir.stream); } static int local_open(FsContext *ctx, V9fsPath *fs_path,
@@ -533,12 +533,12 @@ static int local_opendir(FsContext *ctx,static void local_rewinddir (FsContext *ctx,V9fsFidOpenState *fs) { -rewinddir (fs->dir.stream); +qeme_rewinddir (fs->dir.stream); }static off_t local_telldir (FsContext *ctx,V9fsFidOpenState *fs) { -return telldir (fs->dir.stream); +return qemu_telldir (fs->dir.stream); }static bool local_is_mapped_file_metadata (FsContext *fs_ctx,const char *name)
@@ -552,13 +552,13 @@ static struct dirent *local_readdir(FsContext *ctx, V9fsFidOpenState *fs)struct dirent *entry; again: - entry =readdir (fs->dir.stream); + entry =qemu_readdir (fs->dir.stream);if (!entry) {return NULL ; }#ifdef CONFIG_DARWIN int off; - off =telldir (fs->dir.stream); + off =qemu_telldir (fs->dir.stream);/* If telldir fails, fail the entire readdir call */ if (off <0 ) {return NULL ;
@@ -581,7 +581,7 @@ again:static void local_seekdir (FsContext *ctx,V9fsFidOpenState *fs,off_t off) { -seekdir (fs->dir.stream, off); +qemu_seekdir (fs->dir.stream, off); } static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs,
Signature · 1 line
2.25.1
PATCH v4 5/16 hw/9pfs: Update the local fs driver to support Windows
In reply to Bin MengFrom: Guohuai Shi <[email protected]>
Update the 9p 'local' file system driver to support Windows, including open, read, write, close, rename, remove, etc.
All security models are supported. The mapped (mapped-xattr) security model is implemented using NTFS Alternate Data Stream (ADS) so the 9p export path shall be on an NTFS partition.
Symbolic link and hard link are not supported when security model is "passthrough" or "none", because Windows NTFS does not fully support them with POSIX compatibility. Symbolic link is enabled when security model is "mapped-file" or "mapped-xattr".
inode remap is always enabled because Windows file system does not provide a compatible inode number.
mknod() is not supported because Windows does not support it. chown() and chmod() are not supported when 9pfs is configured with security mode to 'none' or 'passthrough' because Windows host does not support such type request.
---
hw/9pfs/9p-local.h | 1 + hw/9pfs/9p-local.c | 253 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 246 insertions(+), 8 deletions(-)
hw/9pfs/9p-local.h +1 -0
@@ -17,6 +17,7 @@ typedef struct {int mountfd;#ifdef CONFIG_WIN32 char *root_path; +DWORD block_size;#endif } LocalData;
hw/9pfs/9p-local.c +245 -8
528 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH v4 15/16 tests/qtest: virtio-9p-test: Adapt the case for win32
In reply to Bin MengFrom: Guohuai Shi <[email protected]>
Windows does not provide the getuid() API. Let's create a local one and return a fixed value 0 as the uid for testing.
---
tests/qtest/libqos/virtio-9p-client.h | 7 +++++++ 1 file changed, 7 insertions(+)
tests/qtest/libqos/virtio-9p-client.h +7 -0
@@ -491,4 +491,11 @@ void v9fs_rlink(P9Req *req);TunlinkatRes v9fs_tunlinkat (TunlinkatOpt );void v9fs_runlinkat (P9Req *req); +#ifdef CONFIG_WIN32 +static inline uint32_t getuid (void ) +{ +return 0 ; +} +#endif +#endif
Signature · 1 line
2.25.1
Hi
Quoted 19 lines · Bin Meng
At present there is no Windows support for 9p file system. This series adds initial Windows support for 9p file system.
'local' file system backend driver is supported on Windows, including open, read, write, close, rename, remove, etc. All security models are supported. The mapped (mapped-xattr) security model is implemented using NTFS Alternate Data Stream (ADS) so the 9p export path shall be on an NTFS partition.
'synth' driver is adapted for Windows too so that we can now run qtests on Windows for 9p related regression testing.
Example command line to test:
"-fsdev local,path=c:\msys64,security_model=mapped,id=p9 -device
virtio-9p-pci,fsdev=p9,mount_tag=p9fs"Base-commit:
13356edb87506c148b163b8c7eb0695647d00c2a
Quoted 9 lines
Changes in v4:
- Fixed 9pfs mounted as read-only issue on Windows host, adding awin32_error_to_posix()to translate Windows native API error to POSIX one.
- Fixed errors of handling symbolic links
- Added forward declaration to avoid using 'void *'
- Implemented Windows specificxxxdir()APIs for safe directory access
Sorry to look a bit late at this series, I don't know what was discussed previously.
My general feeling is that a lot of this FS portability work would be better handled by using GIO (even though this may add some extra dependency). GIO lacks some features on win32 (for example xattributes on win32), but they could have been proposed there too and benefiting other apps.
Btw, I would not count on mingw adding support for flags/API (S_IFLNK etc), that do not make sense on win32. Did you request them?
I suppose the 9pfs maintainers (Greg, Christian) will have to decide. I can take a deeper look if the overall approach is approved, and as needed.
Bin Meng (2):
Quoted 42 lines
hw/9pfs: Update helper
qemu_stat_rdev()
hw/9pfs: Add a helperqemu_stat_blksize()Guohuai Shi (14):
hw/9pfs: Add missing definitions for Windows
hw/9pfs: Implement Windows specific utilities functions for 9pfs hw/9pfs: Replace the direct call toxxxdir()APIs with a wrapper hw/9pfs: Implement Windows specificxxxdir()APIs hw/9pfs: Update the local fs driver to support Windows hw/9pfs: Support getting current directory offset for Windows hw/9pfs: Disable unsupported flags and features for Windows hw/9pfs: Updatev9fs_set_fd_limit()for Windows hw/9pfs: Add Linux error number definition
hw/9pfs: Translate Windows errno to Linux value
fsdev: Disable proxy fs driver on Windows
hw/9pfs: Update synth fs driver for Windows tests/qtest: virtio-9p-test: Adapt the case for win32 meson.build: Turn on virtfs for Windowsmeson.build | 10 +- fsdev/file-op-9p.h | 33 + hw/9pfs/9p-linux-errno.h | 151 +++ hw/9pfs/9p-local.h | 8 + hw/9pfs/9p-util.h | 139 ++- hw/9pfs/9p.h | 43 + tests/qtest/libqos/virtio-9p-client.h | 7 + fsdev/qemu-fsdev.c | 2 + hw/9pfs/9p-local.c | 269 ++++- hw/9pfs/9p-synth.c | 5 +- hw/9pfs/9p-util-win32.c | 1305 +++++++++++++++++++++++++ hw/9pfs/9p.c | 90 +- hw/9pfs/codir.c | 2 +- fsdev/meson.build | 1 + hw/9pfs/meson.build | 8 +- 15 files changed, 2008 insertions(+), 65 deletions(-) create mode 100644 hw/9pfs/9p-linux-errno.h create mode 100644 hw/9pfs/9p-util-win32.c--
2.25.1
Quoted 32 lines · Marc-André Lureau
Hi
Quoted 19 lines · Bin Meng
At present there is no Windows support for 9p file system. This series adds initial Windows support for 9p file system.
'local' file system backend driver is supported on Windows, including open, read, write, close, rename, remove, etc. All security models are supported. The mapped (mapped-xattr) security model is implemented using NTFS Alternate Data Stream (ADS) so the 9p export path shall be on an NTFS partition.
'synth' driver is adapted for Windows too so that we can now run qtests on Windows for 9p related regression testing.
Example command line to test:
"-fsdev local,path=c:\msys64,security_model=mapped,id=p9 -device
virtio-9p-pci,fsdev=p9,mount_tag=p9fs"Base-commit:
13356edb87506c148b163b8c7eb0695647d00c2aQuoted 9 lines
Changes in v4:
- Fixed 9pfs mounted as read-only issue on Windows host, adding awin32_error_to_posix()to translate Windows native API error to POSIX one.
- Fixed errors of handling symbolic links
- Added forward declaration to avoid using 'void *'
- Implemented Windows specificxxxdir()APIs for safe directory access
Quoted 8 lines
Sorry to look a bit late at this series, I don't know what was discussed previously.
My general feeling is that a lot of this FS portability work would be better handled by using GIO (even though this may add some extra dependency). GIO lacks some features on win32 (for example xattributes on win32), but they could have been proposed there too and benefiting other apps.
The currently impl relies on the openat, fstatat, mkdirat, renameat, utimensat, unlinkat functions. IIRC this was in order to deal with various security vulnerabilities that exist due to race conditions. AFAIK, there's no way to achieve the same with GIO as its a higher level API which doesn't expose this kind of functionality
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Hi
Quoted 42 lines · Daniel P. Berrangé
Quoted 32 lines · Marc-André Lureau
Hi
Quoted 19 lines · Bin Meng
At present there is no Windows support for 9p file system. This series adds initial Windows support for 9p file system.
'local' file system backend driver is supported on Windows, including open, read, write, close, rename, remove, etc. All security models are supported. The mapped (mapped-xattr) security model is implemented using NTFS Alternate Data Stream (ADS) so the 9p export path shall be on an NTFS partition.
'synth' driver is adapted for Windows too so that we can now run qtests on Windows for 9p related regression testing.
Example command line to test:
"-fsdev local,path=c:\msys64,security_model=mapped,id=p9 -device
virtio-9p-pci,fsdev=p9,mount_tag=p9fs"Base-commit:
13356edb87506c148b163b8c7eb0695647d00c2aQuoted 9 lines
Changes in v4:
- Fixed 9pfs mounted as read-only issue on Windows host, adding awin32_error_to_posix()to translate Windows native API error to POSIX one.
- Fixed errors of handling symbolic links
- Added forward declaration to avoid using 'void *'
- Implemented Windows specificxxxdir()APIs for safe directory accessQuoted 8 lines
Sorry to look a bit late at this series, I don't know what was discussed previously.
My general feeling is that a lot of this FS portability work would be better handled by using GIO (even though this may add some extra dependency). GIO lacks some features on win32 (for example xattributes on win32), but they could have been proposed there too and benefiting other apps.
Quoted 7 lines
The currently impl relies on the openat, fstatat, mkdirat, renameat, utimensat, unlinkat functions. IIRC this was in order to deal with various security vulnerabilities that exist due to race conditions. AFAIK, there's no way to achieve the same with GIO as its a higher level API which doesn't expose this kind of functionality
Correct me if I am wrong, but that doesn't seem to hold much since the protocol doesn't keep a context (with associated fds) around. But perhaps GIO API alone can't provide safe implementations of the FileOperations callbacks?
Also a lot of 9p-unix specific details may not map easily to the GIO API. How they can be ported to win32 is certainly a challenge, mostly duplicating the effort done in GIO to me.
Quoted 8 lines
From: Marc-André Lureau <[email protected]>
Sent: Tuesday, January 31, 2023 23:07
To: Daniel P. Berrangé <[email protected]>
Cc: Meng, Bin <[email protected]>; Greg Kurz <[email protected]>; Christian Schoenebeck <[email protected]>; [email protected]; Shi, Guohuai <[email protected]>; Laurent > Vivier <[email protected]>; Paolo Bonzini <[email protected]>; Philippe Mathieu-Daudé <[email protected]>; Thomas Huth <[email protected]>
Subject: Re: [PATCH v4 00/16] hw/9pfs: Add 9pfs support for Windows
Quoted 46 lines
CAUTION: This email comes from a non Wind River email account!
Do not click links or open attachments unless you recognize the sender and know the content is safe. HiOn Tue, Jan 31, 2023 at 6:39 PM Daniel P. Berrangé <mailto:[email protected]> wrote:
Quoted 40 lines · Marc-André Lureau
Hi
Quoted 19 lines · Bin Meng
At present there is no Windows support for 9p file system. This series adds initial Windows support for 9p file system.
'local' file system backend driver is supported on Windows, including open, read, write, close, rename, remove, etc. All security models are supported. The mapped (mapped-xattr) security model is implemented using NTFS Alternate Data Stream (ADS) so the 9p export path shall be on an NTFS partition.
'synth' driver is adapted for Windows too so that we can now run qtests on Windows for 9p related regression testing.
Example command line to test:
"-fsdev local,path=c:\msys64,security_model=mapped,id=p9 -device virtio-9p-pci,fsdev=p9,mount_tag=p9fs"
Base-commit:
13356edb87506c148b163b8c7eb0695647d00c2aQuoted 9 lines
Changes in v4:
- Fixed 9pfs mounted as read-only issue on Windows host, adding awin32_error_to_posix()to translate Windows native API error to POSIX one.
- Fixed errors of handling symbolic links
- Added forward declaration to avoid using 'void *'
- Implemented Windows specificxxxdir()APIs for safe directory accessSorry to look a bit late at this series, I don't know what was discussed previously.
My general feeling is that a lot of this FS portability work would be better handled by using GIO (even though this may add some extra dependency). GIO lacks some features on win32 (for example xattributes on win32), but they could have been proposed there too and benefiting other apps.
GIO function is actually same as MinGW APIs, which is not safety as MinGW (discussed in previous versions).
https://gitlab.gnome.org/GNOME/glib/-/blob/main/glib/dirent/dirent.c#L61
https://github.com/Alexpux/mingw-w64/blob/master/mingw-w64-crt/misc/dirent.c#L42
GIO function also does not handle symbolic links on Windows host, this may cause security issues. GIO functions also use Windows POSIX APIs without extra security checks (does not provide NO_FOLLOW flags):
https://gitlab.gnome.org/GNOME/glib/-/blob/main/glib/gstdio.c#L1050
9pfs need functions like openat() to make sure that the sub-sequence operation is working in the expected parent.
So using GIO will still have security issues.
Quoted 10 lines
The currently impl relies on the openat, fstatat, mkdirat, renameat, utimensat, unlinkat functions. IIRC this was in order to deal with various security vulnerabilities that exist due to race conditions. AFAIK, there's no way to achieve the same with GIO as its a higher level API which doesn't expose this kind of functionality
Correct me if I am wrong, but that doesn't seem to hold much since the protocol doesn't keep a context (with associated fds) around. But perhaps GIO API alone can't provide safe implementations of the FileOperations callbacks?
Also a lot of 9p-unix specific details may not map easily to the GIO API. How they can be ported to win32 is certainly a challenge, mostly duplicating the effort done in GIO to me.
Thanks
Guohuai
Hi
Quoted 61 lines · Guohuai
Quoted 9 lines
From: Marc-André Lureau <[email protected]>
Sent: Tuesday, January 31, 2023 23:07
To: Daniel P. Berrangé <[email protected]>
Cc: Meng, Bin <[email protected]>; Greg Kurz <[email protected]>; Christian Schoenebeck <[email protected]>; [email protected]; Shi, Guohuai <[email protected]>; Laurent > Vivier <[email protected]>; Paolo Bonzini <[email protected]>; Philippe Mathieu-Daudé <[email protected]>; Thomas Huth <[email protected]>
Subject: Re: [PATCH v4 00/16] hw/9pfs: Add 9pfs support for WindowsQuoted 46 lines
CAUTION: This email comes from a non Wind River email account!
Do not click links or open attachments unless you recognize the sender and know the content is safe. HiOn Tue, Jan 31, 2023 at 6:39 PM Daniel P. Berrangé <mailto:[email protected]> wrote:
Quoted 40 lines · Marc-André Lureau
Hi
Quoted 19 lines · Bin Meng
At present there is no Windows support for 9p file system. This series adds initial Windows support for 9p file system.
'local' file system backend driver is supported on Windows, including open, read, write, close, rename, remove, etc. All security models are supported. The mapped (mapped-xattr) security model is implemented using NTFS Alternate Data Stream (ADS) so the 9p export path shall be on an NTFS partition.
'synth' driver is adapted for Windows too so that we can now run qtests on Windows for 9p related regression testing.
Example command line to test:
"-fsdev local,path=c:\msys64,security_model=mapped,id=p9 -device
virtio-9p-pci,fsdev=p9,mount_tag=p9fs"Base-commit:
13356edb87506c148b163b8c7eb0695647d00c2aQuoted 9 lines
Changes in v4:
- Fixed 9pfs mounted as read-only issue on Windows host, adding awin32_error_to_posix()to translate Windows native API error to POSIX one.
- Fixed errors of handling symbolic links
- Added forward declaration to avoid using 'void *'
- Implemented Windows specificxxxdir()APIs for safe directory accessSorry to look a bit late at this series, I don't know what was discussed previously.
My general feeling is that a lot of this FS portability work would be better handled by using GIO (even though this may add some extra dependency). GIO lacks some features on win32 (for example xattributes on win32), but they could have been proposed there too and benefiting other apps.
GIO function is actually same as MinGW APIs, which is not safety as MinGW (discussed in previous versions).
https://gitlab.gnome.org/GNOME/glib/-/blob/main/glib/dirent/dirent.c#L61
https://github.com/Alexpux/mingw-w64/blob/master/mingw-w64-crt/misc/dirent.c#L42
Quoted 7 lines
GIO function also does not handle symbolic links on Windows host, this may cause security issues. GIO functions also use Windows POSIX APIs without extra security checks (does not provide
NO_FOLLOWflags):
https://gitlab.gnome.org/GNOME/glib/-/blob/main/glib/gstdio.c#L10509pfs need functions like
openat()to make sure that the sub-sequence operation is working in the expected parent.So using GIO will still have security issues.
Fair enough, it's a bit of a shame it's not easy to sandbox a process and not have to worry about those links..
Re: [PATCH v4 04/16] hw/9pfs: Implement Windows specific xxxdir() APIs
In reply to Bin MengQuoted 5 lines · CET Bin Meng
From: Guohuai Shi <[email protected]>
This commit implements Windows specific
xxxdir()APIs for safety directory access.
This issue deserves a link to either the previous discussion
and/or a link to this continuation of the discussion here, as it's not a trivial issue, with pros and cons been discussed for the individual, possible solutions.
Quoted 30 lines
Signed-off-by Guohuai Shi <[email protected]>Signed-off-by Bin Meng <[email protected]>---
hw/9pfs/9p-util.h | 6 + hw/9pfs/9p-util-win32.c | 296 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 302 insertions(+)hw/9pfs/9p-util.h +6
-0@@ -141,6 +141,12 @@ int unlinkat_win32(int dirfd, const char *pathname, int flags);int statfs_win32 (const char *root_path,struct statfs *stbuf);int openat_dir (int dirfd,const char *name);int openat_file (int dirfd,const char *name,int flags,mode_t mode); +DIR *opendir_win32 (const char *full_file_name); +int closedir_win32 (DIR *pDir); +struct dirent *readdir_win32 (DIR *pDir); +void rewinddir_win32 (DIR *pDir); +void seekdir_win32 (DIR *pDir,long pos); +long telldir_win32 (DIR *pDir);#endif static inline void close_preserve_errno (int fd)hw/9pfs/9p-util-win32.c +0
-0
hw/9pfs/9p-util-win32.c line 43
Quoted 7 lines
hw/9pfs/9p-util-win32.c +4
-0@@ -37,6 +37,13 @@* Windows does not support opendir, the directory fd is created by * CreateFile and convert to fd by _open_osfhandle(). Keep the fd open will * lock and protect the directory (can not be modified or replaced) +* +* 5. Windows and MinGW does not provide safety directory accessing functions. +* readdir(), seekdir() and telldir() may get or set wrong value because +* directory entry data is not protected.
I would rephrase that sentence, as it doesn't cover the root problem adequately. Maybe something like this:
5. Neither Windows native APIs, nor MinGW provide a POSIX compatible API for acquiring directory entries in a safe way. Calling those APIs (native _findfirst() and _findnext() or MinGW's readdir(), seekdir() and telldir()) directly can lead to an inconsistent state if directory is modified in between, e.g. the same directory appearing more than once in output, or directories not appearing at all in output even though they were neither newly created nor deleted. POSIX does not define what happens with deleted or newly created directories in between, but it guarantees a consistent state.
Quoted 111 lines
hw/9pfs/9p-util-win32.c +97
-0+* +* This file re-write POSIX directory accessing functions and cache all +* directory entries during opening. */ #include "qemu/osdep.h" @@ -51,6 +58,27 @@#define V9FS_MAGIC 0x53465039/* string "9PFS" */ +/* +* MinGW and Windows does not provide safety way to seek directory while other +* thread is modifying same directory. +* +* The two structures are used to cache all directory entries when opening it. +* Cached entries are always returned for read or seek. +*/ +struct dir_win32_entry { +QSLIST_ENTRY (dir_win32_entry) node; +struct _finddata_t dd_data; +}; + +struct dir_win32 { +struct dirent dd_dir; +uint32_t offset; +uint32_t total_entries; + QSLIST_HEAD(, dir_win32_entry) head; +struct dir_win32_entry *current; +char dd_name[1 ]; +}; +/* * win32_error_to_posix - convert Win32 error to POSIX error number * @@ -977,3 +1005,271 @@ int qemu_mknodat(int dirfd, const char *filename, mode_t mode, dev_t dev) errno =ENOTSUP ;return -1 ; } + +/* +* opendir_win32 - open a directory +* +* This function opens a directory and caches all directory entries. +*/ +DIR *opendir_win32 (const char *full_file_name) +{ +HANDLE hDir =INVALID_HANDLE_VALUE ; +DWORD attribute; +intptr_t dd_handle =-1 ; +struct _finddata_t dd_data; + +struct dir_win32 *stream =NULL ; +struct dir_win32_entry *dir_entry; +struct dir_win32_entry *prev; +struct dir_win32_entry *next; + +int err =0 ; +int find_status; +uint32_t index; + +/* open directory to prevent it being removed */ + + hDir =CreateFile (full_file_name,GENERIC_READ , +FILE_SHARE_READ |FILE_SHARE_WRITE |FILE_SHARE_DELETE , +NULL , +OPEN_EXISTING ,FILE_FLAG_BACKUP_SEMANTICS ,NULL ); + +if (hDir ==INVALID_HANDLE_VALUE ) { + err =win32_error_to_posix (GetLastError ()); + goto out; + } + + attribute =GetFileAttributes (full_file_name); + +/* symlink is not allow */ +if (attribute ==INVALID_FILE_ATTRIBUTES + || (attribute &FILE_ATTRIBUTE_REPARSE_POINT ) !=0 ) { + err =EACCES ; + goto out; + } + +/* check if it is a directory */ +if ((attribute &FILE_ATTRIBUTE_DIRECTORY ) ==0 ) { + err =ENOTDIR ; + goto out; + } + +/* +* findfirst() need suffix format name like "\dir1\dir2\*", allocate more +* buffer to store suffix. +*/ + stream =g_malloc0 (sizeof (struct dir_win32 ) +strlen (full_file_name) +3 ); +QSLIST_INIT (&stream->head); + +strcpy (stream->dd_name, full_file_name); +strcat (stream->dd_name,"\\*" ); + + dd_handle =_findfirst (stream->dd_name, &dd_data); + +if (dd_handle ==-1 ) { + err = errno; + goto out; + } + + index =0 ; + +/* read all entries to link list */ + do { + dir_entry = g_malloc0(sizeof(struct dir_win32_entry)); + memcpy(&dir_entry->dd_data, &dd_data, sizeof(dd_data)); + if (index == 0) {
hw/9pfs/9p-util-win32.c int qemu_mknodat(int dirfd, const char *filename, mode_t mode, dev_t dev) · line 1090
Quoted 10 lines
hw/9pfs/9p-util-win32.c +10
-0+QSLIST_INSERT_HEAD (&stream->head, dir_entry, node); + }else { +QSLIST_INSERT_AFTER (prev, dir_entry, node); + } + + prev = dir_entry; + find_status =_findnext (dd_handle, &dd_data); + + index++; + }while (find_status ==0 );
So you decided to go for the solution that caches all entries of a directory in RAM.
So don't you think my last suggested solution that would call native _findfirst() and _findnext() directly, but without any chaching and instead picking the relevent entry simply by inode number, might be a better candidate as a starting point for landing Windows support? Link to that previous suggestion:
https://lore.kernel.org/qemu-devel/2468168.SvRIHAoRfs@silver/
Quoted 186 lines
hw/9pfs/9p-util-win32.c +185
-0+ +if (errno ==ENOENT ) { +/* No more matching files could be found, clean errno */ + errno =0 ; + }else { + err = errno; + goto out; + } + + stream->total_entries = index; + stream->current =QSLIST_FIRST (&stream->head); + +out: +if (err !=0 ) { + errno = err; +/* free whole list */ +if (stream !=NULL ) { + QSLIST_FOREACH_SAFE(dir_entry, &stream->head, node, next) { +QSLIST_REMOVE (&stream->head, dir_entry, dir_win32_entry, node); +g_free (dir_entry); + } +g_free (stream); + stream =NULL ; + } + } + +/* after cached all entries, this handle is useless */ +if (dd_handle !=-1 ) { +_findclose (dd_handle); + } + +if (hDir !=INVALID_HANDLE_VALUE ) { +CloseHandle (hDir); + } + +return (DIR *)stream; +} + +/* +* closedir_win32 - close a directory +* +* This function closes directory and free all cached resources. +*/ +int closedir_win32 (DIR *pDir) +{ +struct dir_win32 *stream = (struct dir_win32 *)pDir; +struct dir_win32_entry *dir_entry; +struct dir_win32_entry *next; + +if (stream ==NULL ) { + errno =EBADF ; +return -1 ; + } + +/* free all resources */ + + QSLIST_FOREACH_SAFE(dir_entry, &stream->head, node, next) { +QSLIST_REMOVE (&stream->head, dir_entry, dir_win32_entry, node); +g_free (dir_entry); + } + +g_free (stream); + +return 0 ; +} + +/* +* readdir_win32 - read a directory +* +* This function reads a directory entry from cached entry list. +*/ +struct dirent *readdir_win32 (DIR *pDir) +{ +struct dir_win32 *stream = (struct dir_win32 *)pDir; + +if (stream ==NULL ) { + errno =EBADF ; +return NULL ; + } + +if (stream->offset >= stream->total_entries) { +/* reach to the end, return NULL without set errno */ +return NULL ; + } + +memcpy (stream->dd_dir.d_name, + stream->current->dd_data.name, +sizeof (stream->dd_dir.d_name)); + +/* Windows does not provide inode number */ + stream->dd_dir.d_ino =0 ; + stream->dd_dir.d_reclen =0 ; + stream->dd_dir.d_namlen =strlen (stream->dd_dir.d_name); + + stream->offset++; + stream->current =QSLIST_NEXT (stream->current, node); + +return &stream->dd_dir; +} + +/* +* rewinddir_win32 - reset directory stream +* +* This function resets the position of the directory stream to the +* beginning of the directory. +*/ +void rewinddir_win32 (DIR *pDir) +{ +struct dir_win32 *stream = (struct dir_win32 *)pDir; + +if (stream ==NULL ) { + errno =EBADF ; +return ; + } + + stream->offset =0 ; + stream->current =QSLIST_FIRST (&stream->head); + +return ; +} + +/* +* seekdir_win32 - set the position of the next readdir() call in the directory +* +* This function sets the position of the next readdir() call in the directory +* from which the next readdir() call will start. +*/ +void seekdir_win32 (DIR *pDir,long pos) +{ +struct dir_win32 *stream = (struct dir_win32 *)pDir; +uint32_t index; + +if (stream ==NULL ) { + errno =EBADF ; +return ; + } + +if (pos <-1 ) { + errno =EINVAL ; +return ; + } + +if (pos ==-1 || pos >= (long )stream->total_entries) { +/* seek to the end */ + stream->offset = stream->total_entries; +return ; + } + +if (pos - (long )stream->offset ==0 ) { +/* no need to seek */ +return ; + } + +/* seek position from list head */ + + stream->current =QSLIST_FIRST (&stream->head); + +for (index =0 ; index < (uint32_t )pos; index++) { + stream->current =QSLIST_NEXT (stream->current, node); + } + stream->offset = index; + +return ; +} + +/* +* telldir_win32 - return current location in directory +* +* This function returns current location in directory. +*/ +long telldir_win32 (DIR *pDir) +{ +struct dir_win32 *stream = (struct dir_win32 *)pDir; + +if (stream ==NULL ) { + errno =EBADF ; +return -1 ; + } + +if (stream->offset > stream->total_entries) { +return -1 ; + } + +return (long )stream->offset; +}
RE: [PATCH v4 04/16] hw/9pfs: Implement Windows specific xxxdir() APIs
Quoted 214 lines
-----Original Message-----
From: Christian Schoenebeck <[email protected]>
Sent: Friday, February 3, 2023 20:25
To: Greg Kurz <[email protected]>; [email protected]
Cc: Shi, Guohuai <[email protected]>; Meng, Bin <[email protected]>; Marc-André Lureau <[email protected]>; Daniel P. Berrangé <[email protected]>
Subject: Re: [PATCH v4 04/16] hw/9pfs: Implement Windows specificxxxdir()APIsCAUTION: This email comes from a non Wind River email account! Do not click links or open attachments unless you recognize the sender and know the content is safe.
Quoted 5 lines · CET Bin Meng
From: Guohuai Shi <[email protected]>
This commit implements Windows specific
xxxdir()APIs for safety directory access.This issue deserves a link to either the previous discussion
and/or a link to this continuation of the discussion here, as it's not a trivial issue, with pros and cons been discussed for the individual, possible solutions.
Quoted 29 lines
Signed-off-by Guohuai Shi <[email protected]>Signed-off-by Bin Meng <[email protected]>---
hw/9pfs/9p-util.h | 6 + hw/9pfs/9p-util-win32.c | 296 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 302 insertions(+)hw/9pfs/9p-util.h index renamed from hw/9pfs/9p-util.h +0
-0
0f159fb4ce..c1c251fbd1100644hw/9pfs/9p-util.h +0
-0@@ -141,6 +141,12 @@ int unlinkat_win32(int dirfd, const char *pathname, int flags); int statfs_win32(const char *root_path, structstatfs *stbuf); int
openat_dir(int dirfd, const char *name); intopenat_file(int dirfd, const char *name, int flags, mode_t mode);hw/9pfs/9p-util.h +4
-0+DIR *opendir_win32 (const char *full_file_name);int +closedir_win32 (DIR *pDir);struct dirent *readdir_win32 (DIR *pDir); +void rewinddir_win32 (DIR *pDir);void seekdir_win32 (DIR *pDir,long +pos);long telldir_win32 (DIR *pDir);#endif static inline void close_preserve_errno(int fd) diff --git
a/hw/9pfs/9p-util-win32.cb/hw/9pfs/9p-util-win32.cindex a99d579a06..5503199300 100644hw/9pfs/9p-util-win32.c line 38
Quoted 2 lines
hw/9pfs/9p-util-win32.c +0
-0@@ -37,6 +37,13 @@* Windows does not support opendir, the directory fd is created by * CreateFile and convert to fd by _open_osfhandle(). Keep the fd open will
hw/9pfs/9p-util-win32.c
Quoted 3 lines
hw/9pfs/9p-util-win32.c +2
-0* lock and protect the directory (can not be modified or replaced) +* +* 5. Windows and MinGW does not provide safety directory accessing functions.
hw/9pfs/9p-util-win32.c line 42
Quoted 1 line
hw/9pfs/9p-util-win32.c +1
-0+* readdir(), seekdir() and telldir() may get or set wrong value because
hw/9pfs/9p-util-win32.c line 43
Quoted 1 line
hw/9pfs/9p-util-win32.c +1
-0+* directory entry data is not protected. I would rephrase that sentence, as it doesn't cover the root problem adequately. Maybe something like this:
5. Neither Windows native APIs, nor MinGW provide a POSIX compatible API for acquiring directory entries in a safe way. Calling those APIs (native
_findfirst()and_findnext()or MinGW'sreaddir(),seekdir()andtelldir()) directly can lead to an inconsistent state if directory is modified in between, e.g. the same directory appearing more than once in output, or directories not appearing at all in output even though they were neither newly created nor deleted. POSIX does not define what happens with deleted or newly created directories in between, but it guarantees a consistent state.Quoted 7 lines
hw/9pfs/9p-util-win32.c +3
-0+* +* This file re-write POSIX directory accessing functions and cache all +* directory entries during opening. */ #include "qemu/osdep.h" hw/9pfs/9p-util-win32.c line 66
Quoted 9 lines
hw/9pfs/9p-util-win32.c +6
-0@@ -51,6 +58,27 @@#define V9FS_MAGIC 0x53465039/* string "9PFS" */ +/* +* MinGW and Windows does not provide safety way to seek directory +while other +* thread is modifying same directory. +* +* The two structures are used to cache all directory entries when opening it.
hw/9pfs/9p-util-win32.c int qemu_mknodat(int dirfd, const char *filename,
Quoted 20 lines
hw/9pfs/9p-util-win32.c +16
-0+* Cached entries are always returned for read or seek. +*/ +struct dir_win32_entry { +QSLIST_ENTRY (dir_win32_entry) node; +struct _finddata_t dd_data; +}; + +struct dir_win32 { +struct dirent dd_dir; +uint32_t offset; +uint32_t total_entries; + QSLIST_HEAD(, dir_win32_entry) head; +struct dir_win32_entry *current; +char dd_name[1 ]; +}; +/* * win32_error_to_posix - convert Win32 error to POSIX error number * @@ -977,3 +1005,271 @@ int qemu_mknodat(int dirfd, const char *filename,
mode_tmode,dev_tdev)Quoted 18 lines
hw/9pfs/9p-util-win32.c +15
-0errno =ENOTSUP ;return -1 ; } + +/* +* opendir_win32 - open a directory +* +* This function opens a directory and caches all directory entries. +*/ +DIR *opendir_win32 (const char *full_file_name) { +HANDLE hDir =INVALID_HANDLE_VALUE ; +DWORD attribute; +intptr_t dd_handle =-1 ; +struct _finddata_t dd_data; + +struct dir_win32 *stream =NULL ; +struct dir_win32_entry *dir_entry; +struct dir_win32_entry *prev;hw/9pfs/9p-util-win32.c int qemu_mknodat(int dirfd, const char *filename, mode_t mode, dev_t dev)
Quoted 10 lines
hw/9pfs/9p-util-win32.c +10
-0+struct dir_win32_entry *next; + +int err =0 ; +int find_status; +uint32_t index; + +/* open directory to prevent it being removed */ + + hDir = CreateFile(full_file_name, GENERIC_READ, + FILE_SHARE_READ | FILE_SHARE_WRITE |
FILE_SHARE_DELETE,Quoted 16 lines
hw/9pfs/9p-util-win32.c +16
-0+NULL , +OPEN_EXISTING ,FILE_FLAG_BACKUP_SEMANTICS , + NULL); + +if (hDir ==INVALID_HANDLE_VALUE ) { + err =win32_error_to_posix (GetLastError ()); + goto out; + } + + attribute =GetFileAttributes (full_file_name); + +/* symlink is not allow */ +if (attribute ==INVALID_FILE_ATTRIBUTES + || (attribute &FILE_ATTRIBUTE_REPARSE_POINT ) !=0 ) { + err =EACCES ; + goto out;hw/9pfs/9p-util-win32.c int qemu_mknodat(int dirfd, const char *filename, mode_t mode, dev_t dev)
Quoted 10 lines
hw/9pfs/9p-util-win32.c +10
-0+ } + +/* check if it is a directory */ +if ((attribute &FILE_ATTRIBUTE_DIRECTORY ) ==0 ) { + err =ENOTDIR ; + goto out; + } + +/* +* findfirst() need suffix format name like "\dir1\dir2\*", allocate more
hw/9pfs/9p-util-win32.c int qemu_mknodat(int dirfd, const char *filename, mode_t mode, dev_t dev)
Quoted 3 lines
hw/9pfs/9p-util-win32.c +3
-0+* buffer to store suffix. +*/ + stream = g_malloc0(sizeof(struct dir_win32) + strlen(full_file_name) +3);
Quoted 19 lines
hw/9pfs/9p-util-win32.c +19
-0+QSLIST_INIT (&stream->head); + +strcpy (stream->dd_name, full_file_name); +strcat (stream->dd_name,"\\*" ); + + dd_handle =_findfirst (stream->dd_name, &dd_data); + +if (dd_handle ==-1 ) { + err = errno; + goto out; + } + + index =0 ; + +/* read all entries to link list */ + do { + dir_entry = g_malloc0(sizeof(struct dir_win32_entry)); + memcpy(&dir_entry->dd_data, &dd_data, sizeof(dd_data)); + if (index == 0) {hw/9pfs/9p-util-win32.c int qemu_mknodat(int dirfd, const char *filename, mode_t mode, dev_t dev) · line 1090
Quoted 10 lines
hw/9pfs/9p-util-win32.c +10
-0+QSLIST_INSERT_HEAD (&stream->head, dir_entry, node); + }else { +QSLIST_INSERT_AFTER (prev, dir_entry, node); + } + + prev = dir_entry; + find_status =_findnext (dd_handle, &dd_data); + + index++; + }while (find_status ==0 );So you decided to go for the solution that caches all entries of a directory in RAM.
Quoted 9 lines
So don't you think my last suggested solution that would call native
_findfirst()and_findnext()directly, but without any chaching and instead picking the relevent entry simply by inode number, might be a better candidate as a starting point for landing Windows support? Link to that previous
suggestion:https://lore.kernel.org/qemu-devel/2468168.SvRIHAoRfs@silver/
I did a quick test for caching data without name entry, but it failed for reading + deleting directory on Windows host (like "rm -rf" for a directory). The root cause is: Windows's directory entry is not cached.
If there is 100 files in a directory:
File1
File2
...
File100
When "rm -rf" is working:
It read first 10 entries, and remove them. 9pfs may seek and re-seek to offset 10 to read next 10 entries.
But Windows and MinGW does not provide rewinddir.
If we using findfirst() and findnext to seek to offset 10, then we will not get File11 but get File 21 (because we skipped 10 entries by seekdir()).
If we removed some entries in directory, inode number is useless because we can not find it again.
Thanks
Guohuai
Quoted 188 lines
Quoted 9 lines
hw/9pfs/9p-util-win32.c +9
-0+ +if (errno ==ENOENT ) { +/* No more matching files could be found, clean errno */ + errno =0 ; + }else { + err = errno; + goto out; + } +hw/9pfs/9p-util-win32.c int qemu_mknodat(int dirfd, const char *filename, mode_t mode, dev_t dev)
Quoted 10 lines
hw/9pfs/9p-util-win32.c +10
-0+ stream->total_entries = index; + stream->current =QSLIST_FIRST (&stream->head); + +out: +if (err !=0 ) { + errno = err; +/* free whole list */ +if (stream !=NULL ) { + QSLIST_FOREACH_SAFE(dir_entry, &stream->head, node, next) { + QSLIST_REMOVE(&stream->head, dir_entry, dir_win32_entry,node);
Quoted 167 lines
hw/9pfs/9p-util-win32.c +166
-0+g_free (dir_entry); + } +g_free (stream); + stream =NULL ; + } + } + +/* after cached all entries, this handle is useless */ +if (dd_handle !=-1 ) { +_findclose (dd_handle); + } + +if (hDir !=INVALID_HANDLE_VALUE ) { +CloseHandle (hDir); + } + +return (DIR *)stream; +} + +/* +* closedir_win32 - close a directory +* +* This function closes directory and free all cached resources. +*/ +int closedir_win32 (DIR *pDir) +{ +struct dir_win32 *stream = (struct dir_win32 *)pDir; +struct dir_win32_entry *dir_entry; +struct dir_win32_entry *next; + +if (stream ==NULL ) { + errno =EBADF ; +return -1 ; + } + +/* free all resources */ + + QSLIST_FOREACH_SAFE(dir_entry, &stream->head, node, next) { +QSLIST_REMOVE (&stream->head, dir_entry, dir_win32_entry, node); +g_free (dir_entry); + } + +g_free (stream); + +return 0 ; +} + +/* +* readdir_win32 - read a directory +* +* This function reads a directory entry from cached entry list. +*/ +struct dirent *readdir_win32 (DIR *pDir) { +struct dir_win32 *stream = (struct dir_win32 *)pDir; + +if (stream ==NULL ) { + errno =EBADF ; +return NULL ; + } + +if (stream->offset >= stream->total_entries) { +/* reach to the end, return NULL without set errno */ +return NULL ; + } + +memcpy (stream->dd_dir.d_name, + stream->current->dd_data.name, +sizeof (stream->dd_dir.d_name)); + +/* Windows does not provide inode number */ + stream->dd_dir.d_ino =0 ; + stream->dd_dir.d_reclen =0 ; + stream->dd_dir.d_namlen =strlen (stream->dd_dir.d_name); + + stream->offset++; + stream->current =QSLIST_NEXT (stream->current, node); + +return &stream->dd_dir; +} + +/* +* rewinddir_win32 - reset directory stream +* +* This function resets the position of the directory stream to the +* beginning of the directory. +*/ +void rewinddir_win32 (DIR *pDir) +{ +struct dir_win32 *stream = (struct dir_win32 *)pDir; + +if (stream ==NULL ) { + errno =EBADF ; +return ; + } + + stream->offset =0 ; + stream->current =QSLIST_FIRST (&stream->head); + +return ; +} + +/* +* seekdir_win32 - set the position of the next readdir() call in the +directory +* +* This function sets the position of the next readdir() call in the +directory +* from which the next readdir() call will start. +*/ +void seekdir_win32 (DIR *pDir,long pos) { +struct dir_win32 *stream = (struct dir_win32 *)pDir; +uint32_t index; + +if (stream ==NULL ) { + errno =EBADF ; +return ; + } + +if (pos <-1 ) { + errno =EINVAL ; +return ; + } + +if (pos ==-1 || pos >= (long )stream->total_entries) { +/* seek to the end */ + stream->offset = stream->total_entries; +return ; + } + +if (pos - (long )stream->offset ==0 ) { +/* no need to seek */ +return ; + } + +/* seek position from list head */ + + stream->current =QSLIST_FIRST (&stream->head); + +for (index =0 ; index < (uint32_t )pos; index++) { + stream->current =QSLIST_NEXT (stream->current, node); + } + stream->offset = index; + +return ; +} + +/* +* telldir_win32 - return current location in directory +* +* This function returns current location in directory. +*/ +long telldir_win32 (DIR *pDir) +{ +struct dir_win32 *stream = (struct dir_win32 *)pDir; + +if (stream ==NULL ) { + errno =EBADF ; +return -1 ; + } + +if (stream->offset > stream->total_entries) { +return -1 ; + } + +return (long )stream->offset; +}
Re: [PATCH v4 04/16] hw/9pfs: Implement Windows specific xxxdir() APIs
Quoted 232 lines · Guohuai
Quoted 213 lines
-----Original Message-----
From: Christian Schoenebeck <[email protected]>
Sent: Friday, February 3, 2023 20:25
To: Greg Kurz <[email protected]>; [email protected]
Cc: Shi, Guohuai <[email protected]>; Meng, Bin <[email protected]>; Marc-André Lureau <[email protected]>; Daniel P. Berrangé <[email protected]>
Subject: Re: [PATCH v4 04/16] hw/9pfs: Implement Windows specificxxxdir()APIsCAUTION: This email comes from a non Wind River email account! Do not click links or open attachments unless you recognize the sender and know the content is safe.
Quoted 5 lines · CET Bin Meng
From: Guohuai Shi <[email protected]>
This commit implements Windows specific
xxxdir()APIs for safety directory access.This issue deserves a link to either the previous discussion
and/or a link to this continuation of the discussion here, as it's not a trivial issue, with pros and cons been discussed for the individual, possible solutions.
Quoted 29 lines
Signed-off-by Guohuai Shi <[email protected]>Signed-off-by Bin Meng <[email protected]>---
hw/9pfs/9p-util.h | 6 + hw/9pfs/9p-util-win32.c | 296 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 302 insertions(+)hw/9pfs/9p-util.h index renamed from hw/9pfs/9p-util.h +0
-0
0f159fb4ce..c1c251fbd1100644hw/9pfs/9p-util.h +0
-0@@ -141,6 +141,12 @@ int unlinkat_win32(int dirfd, const char *pathname, int flags); int statfs_win32(const char *root_path, structstatfs *stbuf); int
openat_dir(int dirfd, const char *name); intopenat_file(int dirfd, const char *name, int flags, mode_t mode);hw/9pfs/9p-util.h +4
-0+DIR *opendir_win32 (const char *full_file_name);int +closedir_win32 (DIR *pDir);struct dirent *readdir_win32 (DIR *pDir); +void rewinddir_win32 (DIR *pDir);void seekdir_win32 (DIR *pDir,long +pos);long telldir_win32 (DIR *pDir);#endif static inline void close_preserve_errno(int fd) diff --git
a/hw/9pfs/9p-util-win32.cb/hw/9pfs/9p-util-win32.cindex a99d579a06..5503199300 100644hw/9pfs/9p-util-win32.c line 38
Quoted 2 lines
hw/9pfs/9p-util-win32.c +0
-0@@ -37,6 +37,13 @@* Windows does not support opendir, the directory fd is created by * CreateFile and convert to fd by _open_osfhandle(). Keep the fd open will
hw/9pfs/9p-util-win32.c
Quoted 3 lines
hw/9pfs/9p-util-win32.c +2
-0* lock and protect the directory (can not be modified or replaced) +* +* 5. Windows and MinGW does not provide safety directory accessing functions.
hw/9pfs/9p-util-win32.c line 42
Quoted 1 line
hw/9pfs/9p-util-win32.c +1
-0+* readdir(), seekdir() and telldir() may get or set wrong value because
hw/9pfs/9p-util-win32.c line 43
Quoted 1 line
hw/9pfs/9p-util-win32.c +1
-0+* directory entry data is not protected. I would rephrase that sentence, as it doesn't cover the root problem adequately. Maybe something like this:
5. Neither Windows native APIs, nor MinGW provide a POSIX compatible API for acquiring directory entries in a safe way. Calling those APIs (native
_findfirst()and_findnext()or MinGW'sreaddir(),seekdir()andtelldir()) directly can lead to an inconsistent state if directory is modified in between, e.g. the same directory appearing more than once in output, or directories not appearing at all in output even though they were neither newly created nor deleted. POSIX does not define what happens with deleted or newly created directories in between, but it guarantees a consistent state.Quoted 7 lines
hw/9pfs/9p-util-win32.c +3
-0+* +* This file re-write POSIX directory accessing functions and cache all +* directory entries during opening. */ #include "qemu/osdep.h" hw/9pfs/9p-util-win32.c line 66
Quoted 9 lines
hw/9pfs/9p-util-win32.c +6
-0@@ -51,6 +58,27 @@#define V9FS_MAGIC 0x53465039/* string "9PFS" */ +/* +* MinGW and Windows does not provide safety way to seek directory +while other +* thread is modifying same directory. +* +* The two structures are used to cache all directory entries when opening it.
hw/9pfs/9p-util-win32.c int qemu_mknodat(int dirfd, const char *filename,
Quoted 20 lines
hw/9pfs/9p-util-win32.c +16
-0+* Cached entries are always returned for read or seek. +*/ +struct dir_win32_entry { +QSLIST_ENTRY (dir_win32_entry) node; +struct _finddata_t dd_data; +}; + +struct dir_win32 { +struct dirent dd_dir; +uint32_t offset; +uint32_t total_entries; + QSLIST_HEAD(, dir_win32_entry) head; +struct dir_win32_entry *current; +char dd_name[1 ]; +}; +/* * win32_error_to_posix - convert Win32 error to POSIX error number * @@ -977,3 +1005,271 @@ int qemu_mknodat(int dirfd, const char *filename,
mode_tmode,dev_tdev)Quoted 18 lines
hw/9pfs/9p-util-win32.c +15
-0errno =ENOTSUP ;return -1 ; } + +/* +* opendir_win32 - open a directory +* +* This function opens a directory and caches all directory entries. +*/ +DIR *opendir_win32 (const char *full_file_name) { +HANDLE hDir =INVALID_HANDLE_VALUE ; +DWORD attribute; +intptr_t dd_handle =-1 ; +struct _finddata_t dd_data; + +struct dir_win32 *stream =NULL ; +struct dir_win32_entry *dir_entry; +struct dir_win32_entry *prev;hw/9pfs/9p-util-win32.c int qemu_mknodat(int dirfd, const char *filename, mode_t mode, dev_t dev)
Quoted 10 lines
hw/9pfs/9p-util-win32.c +10
-0+struct dir_win32_entry *next; + +int err =0 ; +int find_status; +uint32_t index; + +/* open directory to prevent it being removed */ + + hDir = CreateFile(full_file_name, GENERIC_READ, + FILE_SHARE_READ | FILE_SHARE_WRITE |
FILE_SHARE_DELETE,Quoted 16 lines
hw/9pfs/9p-util-win32.c +16
-0+NULL , +OPEN_EXISTING ,FILE_FLAG_BACKUP_SEMANTICS , + NULL); + +if (hDir ==INVALID_HANDLE_VALUE ) { + err =win32_error_to_posix (GetLastError ()); + goto out; + } + + attribute =GetFileAttributes (full_file_name); + +/* symlink is not allow */ +if (attribute ==INVALID_FILE_ATTRIBUTES + || (attribute &FILE_ATTRIBUTE_REPARSE_POINT ) !=0 ) { + err =EACCES ; + goto out;hw/9pfs/9p-util-win32.c int qemu_mknodat(int dirfd, const char *filename, mode_t mode, dev_t dev)
Quoted 10 lines
hw/9pfs/9p-util-win32.c +10
-0+ } + +/* check if it is a directory */ +if ((attribute &FILE_ATTRIBUTE_DIRECTORY ) ==0 ) { + err =ENOTDIR ; + goto out; + } + +/* +* findfirst() need suffix format name like "\dir1\dir2\*", allocate more
hw/9pfs/9p-util-win32.c int qemu_mknodat(int dirfd, const char *filename, mode_t mode, dev_t dev)
Quoted 3 lines
hw/9pfs/9p-util-win32.c +3
-0+* buffer to store suffix. +*/ + stream = g_malloc0(sizeof(struct dir_win32) + strlen(full_file_name) +3);
Quoted 19 lines
hw/9pfs/9p-util-win32.c +19
-0+QSLIST_INIT (&stream->head); + +strcpy (stream->dd_name, full_file_name); +strcat (stream->dd_name,"\\*" ); + + dd_handle =_findfirst (stream->dd_name, &dd_data); + +if (dd_handle ==-1 ) { + err = errno; + goto out; + } + + index =0 ; + +/* read all entries to link list */ + do { + dir_entry = g_malloc0(sizeof(struct dir_win32_entry)); + memcpy(&dir_entry->dd_data, &dd_data, sizeof(dd_data)); + if (index == 0) {hw/9pfs/9p-util-win32.c int qemu_mknodat(int dirfd, const char *filename, mode_t mode, dev_t dev) · line 1090
Quoted 10 lines
hw/9pfs/9p-util-win32.c +10
-0+QSLIST_INSERT_HEAD (&stream->head, dir_entry, node); + }else { +QSLIST_INSERT_AFTER (prev, dir_entry, node); + } + + prev = dir_entry; + find_status =_findnext (dd_handle, &dd_data); + + index++; + }while (find_status ==0 );So you decided to go for the solution that caches all entries of a directory in RAM.
Quoted 9 lines
So don't you think my last suggested solution that would call native
_findfirst()and_findnext()directly, but without any chaching and instead picking the relevent entry simply by inode number, might be a better candidate as a starting point for landing Windows support? Link to that previous
suggestion:https://lore.kernel.org/qemu-devel/2468168.SvRIHAoRfs@silver/
I did a quick test for caching data without name entry, but it failed for reading + deleting directory on Windows host (like "rm -rf" for a directory). The root cause is: Windows's directory entry is not cached.
If there is 100 files in a directory:File1
File2
...
File100
Quoted 5 lines
When "rm -rf" is working:
It read first 10 entries, and remove them. 9pfs may seek and re-seek to offset 10 to read next 10 entries.
But Windows and MinGW does not provide rewinddir.
If we usingfindfirst()and findnext to seek to offset 10, then we will not get File11 but get File 21 (because we skipped 10 entries byseekdir()).
I assume you are referring to a simple solution like MinGW does, i.e. a consecutive dense index (0,1,2,3,...n-1 where n is the current total amount of directory entries). That would not work, yes. But that's not what I suggested.
With an inode number based lookup you would not seek to an incorrect entry ...
Quoted 1 line
If we removed some entries in directory, inode number is useless because we can not find it again.
You *can* recover from the previous inode number, even if any directory entry has been deleted in the meantime: you would lookup the entry with the next higher inode number.
Example, say initial directory state on host is:
name inode-nr aaa 8 bbb 3 ccc 4 ddd 2 eee 9
Say client is looking up exactly 2 entries, you would return to client in this order (by inode-nr):
1. ddd
2. bbb
Now say "bbb" (a.k.a. previous) and "ccc" (a.k.a next) are removed. Directory state on host is now:
name inode-nr aaa 8 ddd 2 eee 9
Subsequently the last directory entries are requested by client. Previous inode number (stored in RAM) was 3, which no longer exists, so you lookup the entry with the next higher inode number than 3, which is now 8 in this example. Hence you would eventually return to client (in this order):
3. aaa
4. eee
Quoted 197 lines
Thanks
GuohuaiQuoted 188 lines
Quoted 9 lines
hw/9pfs/9p-util-win32.c +9
-0+ +if (errno ==ENOENT ) { +/* No more matching files could be found, clean errno */ + errno =0 ; + }else { + err = errno; + goto out; + } +hw/9pfs/9p-util-win32.c int qemu_mknodat(int dirfd, const char *filename, mode_t mode, dev_t dev)
Quoted 10 lines
hw/9pfs/9p-util-win32.c +10
-0+ stream->total_entries = index; + stream->current =QSLIST_FIRST (&stream->head); + +out: +if (err !=0 ) { + errno = err; +/* free whole list */ +if (stream !=NULL ) { + QSLIST_FOREACH_SAFE(dir_entry, &stream->head, node, next) { + QSLIST_REMOVE(&stream->head, dir_entry, dir_win32_entry,node);
Quoted 167 lines
hw/9pfs/9p-util-win32.c +166
-0+g_free (dir_entry); + } +g_free (stream); + stream =NULL ; + } + } + +/* after cached all entries, this handle is useless */ +if (dd_handle !=-1 ) { +_findclose (dd_handle); + } + +if (hDir !=INVALID_HANDLE_VALUE ) { +CloseHandle (hDir); + } + +return (DIR *)stream; +} + +/* +* closedir_win32 - close a directory +* +* This function closes directory and free all cached resources. +*/ +int closedir_win32 (DIR *pDir) +{ +struct dir_win32 *stream = (struct dir_win32 *)pDir; +struct dir_win32_entry *dir_entry; +struct dir_win32_entry *next; + +if (stream ==NULL ) { + errno =EBADF ; +return -1 ; + } + +/* free all resources */ + + QSLIST_FOREACH_SAFE(dir_entry, &stream->head, node, next) { +QSLIST_REMOVE (&stream->head, dir_entry, dir_win32_entry, node); +g_free (dir_entry); + } + +g_free (stream); + +return 0 ; +} + +/* +* readdir_win32 - read a directory +* +* This function reads a directory entry from cached entry list. +*/ +struct dirent *readdir_win32 (DIR *pDir) { +struct dir_win32 *stream = (struct dir_win32 *)pDir; + +if (stream ==NULL ) { + errno =EBADF ; +return NULL ; + } + +if (stream->offset >= stream->total_entries) { +/* reach to the end, return NULL without set errno */ +return NULL ; + } + +memcpy (stream->dd_dir.d_name, + stream->current->dd_data.name, +sizeof (stream->dd_dir.d_name)); + +/* Windows does not provide inode number */ + stream->dd_dir.d_ino =0 ; + stream->dd_dir.d_reclen =0 ; + stream->dd_dir.d_namlen =strlen (stream->dd_dir.d_name); + + stream->offset++; + stream->current =QSLIST_NEXT (stream->current, node); + +return &stream->dd_dir; +} + +/* +* rewinddir_win32 - reset directory stream +* +* This function resets the position of the directory stream to the +* beginning of the directory. +*/ +void rewinddir_win32 (DIR *pDir) +{ +struct dir_win32 *stream = (struct dir_win32 *)pDir; + +if (stream ==NULL ) { + errno =EBADF ; +return ; + } + + stream->offset =0 ; + stream->current =QSLIST_FIRST (&stream->head); + +return ; +} + +/* +* seekdir_win32 - set the position of the next readdir() call in the +directory +* +* This function sets the position of the next readdir() call in the +directory +* from which the next readdir() call will start. +*/ +void seekdir_win32 (DIR *pDir,long pos) { +struct dir_win32 *stream = (struct dir_win32 *)pDir; +uint32_t index; + +if (stream ==NULL ) { + errno =EBADF ; +return ; + } + +if (pos <-1 ) { + errno =EINVAL ; +return ; + } + +if (pos ==-1 || pos >= (long )stream->total_entries) { +/* seek to the end */ + stream->offset = stream->total_entries; +return ; + } + +if (pos - (long )stream->offset ==0 ) { +/* no need to seek */ +return ; + } + +/* seek position from list head */ + + stream->current =QSLIST_FIRST (&stream->head); + +for (index =0 ; index < (uint32_t )pos; index++) { + stream->current =QSLIST_NEXT (stream->current, node); + } + stream->offset = index; + +return ; +} + +/* +* telldir_win32 - return current location in directory +* +* This function returns current location in directory. +*/ +long telldir_win32 (DIR *pDir) +{ +struct dir_win32 *stream = (struct dir_win32 *)pDir; + +if (stream ==NULL ) { + errno =EBADF ; +return -1 ; + } + +if (stream->offset > stream->total_entries) { +return -1 ; + } + +return (long )stream->offset; +}
RE: [PATCH v4 04/16] hw/9pfs: Implement Windows specific xxxdir() APIs
Quoted 307 lines
-----Original Message-----
From: Christian Schoenebeck <[email protected]>
Sent: Friday, February 3, 2023 22:41
To: Greg Kurz <[email protected]>; [email protected]
Cc: Meng, Bin <[email protected]>; Marc-André Lureau <[email protected]>; Daniel P. Berrangé <[email protected]>; Shi, Guohuai <[email protected]>
Subject: Re: [PATCH v4 04/16] hw/9pfs: Implement Windows specificxxxdir()APIsCAUTION: This email comes from a non Wind River email account! Do not click links or open attachments unless you recognize the sender and know the content is safe.
Quoted 55 lines · Guohuai
Quoted 55 lines
-----Original Message-----
From: Christian Schoenebeck <[email protected]>
Sent: Friday, February 3, 2023 20:25
To: Greg Kurz <[email protected]>; [email protected]
Cc: Shi, Guohuai <[email protected]>; Meng, Bin <[email protected]>; Marc-André Lureau <[email protected]>; Daniel P. Berrangé <[email protected]>
Subject: Re: [PATCH v4 04/16] hw/9pfs: Implement Windows specificxxxdir()APIsCAUTION: This email comes from a non Wind River email account! Do not click links or open attachments unless you recognize the sender and know the content is safe.
Quoted 5 lines · CET Bin Meng
From: Guohuai Shi <[email protected]>
This commit implements Windows specific
xxxdir()APIs for safety directory access.This issue deserves a link to either the previous discussion
and/or a link to this continuation of the discussion here, as it's not a trivial issue, with pros and cons been discussed for the individual, possible solutions.
Quoted 24 lines
Signed-off-by Guohuai Shi <[email protected]>Signed-off-by Bin Meng <[email protected]>---
hw/9pfs/9p-util.h | 6 + hw/9pfs/9p-util-win32.c | 296 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 302 insertions(+)hw/9pfs/9p-util.h index renamed from hw/9pfs/9p-util.h +0
-0
0f159fb4ce..c1c251fbd1100644hw/9pfs/9p-util.h +0
-0@@ -141,6 +141,12 @@ int unlinkat_win32(int dirfd, const char *pathname, int flags); int statfs_win32(const char *root_path,struct statfs *stbuf); int
openat_dir(int dirfd, const char *name); intopenat_file(int dirfd, const char *name, int flags, mode_t mode);hw/9pfs/9p-util.h +4
-0+DIR *opendir_win32 (const char *full_file_name);int +closedir_win32 (DIR *pDir);struct dirent *readdir_win32 (DIR +*pDir);void rewinddir_win32 (DIR *pDir);void seekdir_win32 (DIR +*pDir,long pos);long telldir_win32 (DIR *pDir);#endif Quoted 8 lines
Quoted 8 lines
hw/9pfs/9p-util-win32.c line 38
Quoted 8 lines
static inline void
close_preserve_errno(int fd)diff --gita/hw/9pfs/9p-util-win32.cb/hw/9pfs/9p-util-win32.cindex a99d579a06..5503199300 100644hw/9pfs/9p-util-win32.c +0
-0@@ -37,6 +37,13 @@* Windows does not support opendir, the directory fd is created by * CreateFile and convert to fd by _open_osfhandle(). Keep the fd open
Quoted 13 lines
Quoted 13 lines
will
hw/9pfs/9p-util-win32.c
Quoted 4 lines
hw/9pfs/9p-util-win32.c +3
-0* lock and protect the directory (can not be modified or replaced) +* +* 5. Windows and MinGW does not provide safety directory +accessing functions.
hw/9pfs/9p-util-win32.c line 43
Quoted 1 line
hw/9pfs/9p-util-win32.c +1
-0+* readdir(), seekdir() and telldir() may get or set wrong value because
hw/9pfs/9p-util-win32.c line 43
Quoted 1 line
hw/9pfs/9p-util-win32.c +1
-0+* directory entry data is not protected. I would rephrase that sentence, as it doesn't cover the root problem adequately. Maybe something like this:
Quoted 9 lines
Quoted 9 lines
5. Neither Windows native APIs, nor MinGW provide a POSIX compatible API for acquiring directory entries in a safe way. Calling those APIs (native
_findfirst()and_findnext()or MinGW'sreaddir(),seekdir()andtelldir()) directly can lead to an inconsistent state if directory is modified in between, e.g. the same directory appearing more than once in output, or directories not appearing at all in output even though they were neither newly created nor deleted. POSIX does not define what happens with deleted or newly created directories in between,but it guarantees a consistent state.
Quoted 3 lines
Quoted 3 lines
hw/9pfs/9p-util-win32.c line 46
Quoted 3 lines
hw/9pfs/9p-util-win32.c +2
-0+* +* This file re-write POSIX directory accessing functions and cache all
Quoted 140 lines
Quoted 140 lines
Quoted 5 lines
hw/9pfs/9p-util-win32.c +1
-0+* directory entries during opening. */ #include "qemu/osdep.h" hw/9pfs/9p-util-win32.c line 67
Quoted 10 lines
hw/9pfs/9p-util-win32.c +7
-0@@ -51,6 +58,27 @@#define V9FS_MAGIC 0x53465039/* string "9PFS" */ +/* +* MinGW and Windows does not provide safety way to seek +directory while other +* thread is modifying same directory. +* +* The two structures are used to cache all directory entries +when opening it.
Quoted 20 lines
hw/9pfs/9p-util-win32.c +16
-0+* Cached entries are always returned for read or seek. +*/ +struct dir_win32_entry { +QSLIST_ENTRY (dir_win32_entry) node; +struct _finddata_t dd_data; +}; + +struct dir_win32 { +struct dirent dd_dir; +uint32_t offset; +uint32_t total_entries; + QSLIST_HEAD(, dir_win32_entry) head; +struct dir_win32_entry *current; +char dd_name[1 ]; +}; +/* * win32_error_to_posix - convert Win32 error to POSIX error number * hw/9pfs/9p-util-win32.c int qemu_mknodat(int dirfd, const char · line 1005
Quoted 1 line
hw/9pfs/9p-util-win32.c +0
-0@@ -977,3 +1005,271 @@ int qemu_mknodat(int dirfd, const char *filename,
mode_tmode,dev_tdev)Quoted 18 lines
hw/9pfs/9p-util-win32.c +15
-0errno =ENOTSUP ;return -1 ; } + +/* +* opendir_win32 - open a directory +* +* This function opens a directory and caches all directory entries. +*/ +DIR *opendir_win32 (const char *full_file_name) { +HANDLE hDir =INVALID_HANDLE_VALUE ; +DWORD attribute; +intptr_t dd_handle =-1 ; +struct _finddata_t dd_data; + +struct dir_win32 *stream =NULL ; +struct dir_win32_entry *dir_entry; +struct dir_win32_entry *prev;hw/9pfs/9p-util-win32.c int qemu_mknodat(int dirfd, const char *filename, mode_t mode, dev_t dev)
Quoted 10 lines
hw/9pfs/9p-util-win32.c +10
-0+struct dir_win32_entry *next; + +int err =0 ; +int find_status; +uint32_t index; + +/* open directory to prevent it being removed */ + + hDir = CreateFile(full_file_name, GENERIC_READ, + FILE_SHARE_READ | FILE_SHARE_WRITE |
FILE_SHARE_DELETE,Quoted 17 lines
hw/9pfs/9p-util-win32.c +17
-0+NULL , +OPEN_EXISTING ,FILE_FLAG_BACKUP_SEMANTICS , + NULL); + +if (hDir ==INVALID_HANDLE_VALUE ) { + err =win32_error_to_posix (GetLastError ()); + goto out; + } + + attribute =GetFileAttributes (full_file_name); + +/* symlink is not allow */ +if (attribute ==INVALID_FILE_ATTRIBUTES + || (attribute &FILE_ATTRIBUTE_REPARSE_POINT ) !=0 ) { + err =EACCES ; + goto out; + }hw/9pfs/9p-util-win32.c int qemu_mknodat(int dirfd, const char *filename, mode_t mode, dev_t dev)
Quoted 10 lines
hw/9pfs/9p-util-win32.c +10
-0+ +/* check if it is a directory */ +if ((attribute &FILE_ATTRIBUTE_DIRECTORY ) ==0 ) { + err =ENOTDIR ; + goto out; + } + +/* +* findfirst() need suffix format name like "\dir1\dir2\*", +allocate more
hw/9pfs/9p-util-win32.c int qemu_mknodat(int dirfd, const char *filename, mode_t mode, dev_t dev)
Quoted 4 lines
hw/9pfs/9p-util-win32.c +4
-0+* buffer to store suffix. +*/ + stream = g_malloc0(sizeof(struct dir_win32) + + strlen(full_file_name) +3);
Quoted 19 lines
hw/9pfs/9p-util-win32.c +19
-0+QSLIST_INIT (&stream->head); + +strcpy (stream->dd_name, full_file_name); +strcat (stream->dd_name,"\\*" ); + + dd_handle =_findfirst (stream->dd_name, &dd_data); + +if (dd_handle ==-1 ) { + err = errno; + goto out; + } + + index =0 ; + +/* read all entries to link list */ + do { + dir_entry = g_malloc0(sizeof(struct dir_win32_entry)); + memcpy(&dir_entry->dd_data, &dd_data, sizeof(dd_data)); + if (index == 0) {hw/9pfs/9p-util-win32.c int qemu_mknodat(int dirfd, const char *filename, mode_t mode, dev_t dev) · line 1090
Quoted 10 lines
hw/9pfs/9p-util-win32.c +10
-0+QSLIST_INSERT_HEAD (&stream->head, dir_entry, node); + }else { +QSLIST_INSERT_AFTER (prev, dir_entry, node); + } + + prev = dir_entry; + find_status =_findnext (dd_handle, &dd_data); + + index++; + }while (find_status ==0 );So you decided to go for the solution that caches all entries of a directory in RAM.
So don't you think my last suggested solution that would call native
_findfirst()and_findnext()directly, but without any chaching and instead picking the relevent entry simply by inode number, might be a better candidate as a starting point for landing Windows support? Link to that previous
suggestion:Quoted 4 lines
I did a quick test for caching data without name entry, but it failed for
reading + deleting directory on Windows host (like "rm -rf" for a directory).
Quoted 11 lines
The root cause is: Windows's directory entry is not cached. If there is 100 files in a directory:
File1
File2
...
File100When "rm -rf" is working:
It read first 10 entries, and remove them. 9pfs may seek and re-seek to
offset 10 to read next 10 entries.
Quoted 2 lines
But Windows and MinGW does not provide rewinddir.
If we usingfindfirst()and findnext to seek to offset 10, then we will notget File11 but get File 21 (because we skipped 10 entries by
seekdir()).I assume you are referring to a simple solution like MinGW does, i.e. a consecutive dense index (0,1,2,3,...n-1 where n is the current total amount of directory entries). That would not work, yes. But that's not what I suggested.
With an inode number based lookup you would not seek to an incorrect entry
...Quoted 1 line
If we removed some entries in directory, inode number is useless because we
can not find it again.
You *can* recover from the previous inode number, even if any directory entry has been deleted in the meantime: you would lookup the entry with the next higher inode number.
Example, say initial directory state on host is:
name inode-nr aaa 8 bbb 3 ccc 4 ddd 2 eee 9Say client is looking up exactly 2 entries, you would return to client in this order (by inode-nr):
1. ddd
2. bbbNow say "bbb" (a.k.a. previous) and "ccc" (a.k.a next) are removed. Directory state on host is now:
name inode-nr aaa 8 ddd 2 eee 9
Quoted 8 lines
Subsequently the last directory entries are requested by client. Previous inode number (stored in RAM) was 3, which no longer exists, so you lookup the entry with the next higher inode number than 3, which is now 8 in this example. Hence you would eventually return to client (in this order):
3. aaa
4. eee
Yes, it can work by using inode number (called File ID on Windows host: https://learn.microsoft.com/en-us/windows/win32/api/winbase/ns-winbase-file_id_info).
However, Windows does not provide a function to get file information by file ID.
That means, for anytime of seeking directory, 9pfs need to do the following sequence work to locate a name entry:
1. findfirst
2. CreateFile to get file handle
3. GetFileInformationByHandleEx to get file ID (https://learn.microsoft.com/en-us/windows/win32/api/minwinbase/ne-minwinbase-file_info_by_handle_class)
4. Close file handle and return if the file ID is match
5. findnext
6. repeat to step #2
Windows does not short file name entry by file ID and the file ID is 128-bit integer. When there are many entries in directory, seeking directory will cause a very bad performance.
So I think store all name entries would be better than store all file ID.
Quoted 203 lines
Quoted 16 lines
Thanks
GuohuaiQuoted 10 lines
Quoted 10 lines
hw/9pfs/9p-util-win32.c +10
-0+ +if (errno ==ENOENT ) { +/* No more matching files could be found, clean errno */ + errno =0 ; + }else { + err = errno; + goto out; + } + + stream->total_entries = index;Quoted 8 lines
Quoted 8 lines
hw/9pfs/9p-util-win32.c int qemu_mknodat(int dirfd, const char *filename, mode_t mode, dev_t dev)
Quoted 8 lines
hw/9pfs/9p-util-win32.c +8
-0+ stream->current =QSLIST_FIRST (&stream->head); + +out: +if (err !=0 ) { + errno = err; +/* free whole list */ +if (stream !=NULL ) { +QSLIST_FOREACH_SAFE (dir_entry, &stream->head, node, next){
Quoted 34 lines
Quoted 34 lines
hw/9pfs/9p-util-win32.c
Quoted 2 lines
hw/9pfs/9p-util-win32.c +2
-0+ QSLIST_REMOVE(&stream->head, dir_entry, +dir_win32_entry,node);
Quoted 31 lines
hw/9pfs/9p-util-win32.c +31
-0+g_free (dir_entry); + } +g_free (stream); + stream =NULL ; + } + } + +/* after cached all entries, this handle is useless */ +if (dd_handle !=-1 ) { +_findclose (dd_handle); + } + +if (hDir !=INVALID_HANDLE_VALUE ) { +CloseHandle (hDir); + } + +return (DIR *)stream; +} + +/* +* closedir_win32 - close a directory +* +* This function closes directory and free all cached resources. +*/ +int closedir_win32 (DIR *pDir) +{ +struct dir_win32 *stream = (struct dir_win32 *)pDir; +struct dir_win32_entry *dir_entry; +struct dir_win32_entry *next; + +if (stream ==NULL ) {Quoted 8 lines
Quoted 8 lines
hw/9pfs/9p-util-win32.c int qemu_mknodat(int dirfd, const char *filename, mode_t mode, dev_t dev)
Quoted 8 lines
hw/9pfs/9p-util-win32.c +8
-0+ errno =EBADF ; +return -1 ; + } + +/* free all resources */ + + QSLIST_FOREACH_SAFE(dir_entry, &stream->head, node, next) { + QSLIST_REMOVE(&stream->head, dir_entry, dir_win32_entry,node);
Quoted 133 lines
Quoted 130 lines
Quoted 129 lines
hw/9pfs/9p-util-win32.c +128
-0+g_free (dir_entry); + } + +g_free (stream); + +return 0 ; +} + +/* +* readdir_win32 - read a directory +* +* This function reads a directory entry from cached entry list. +*/ +struct dirent *readdir_win32 (DIR *pDir) { +struct dir_win32 *stream = (struct dir_win32 *)pDir; + +if (stream ==NULL ) { + errno =EBADF ; +return NULL ; + } + +if (stream->offset >= stream->total_entries) { +/* reach to the end, return NULL without set errno */ +return NULL ; + } + +memcpy (stream->dd_dir.d_name, + stream->current->dd_data.name, +sizeof (stream->dd_dir.d_name)); + +/* Windows does not provide inode number */ + stream->dd_dir.d_ino =0 ; + stream->dd_dir.d_reclen =0 ; + stream->dd_dir.d_namlen =strlen (stream->dd_dir.d_name); + + stream->offset++; + stream->current =QSLIST_NEXT (stream->current, node); + +return &stream->dd_dir; +} + +/* +* rewinddir_win32 - reset directory stream +* +* This function resets the position of the directory stream to +the +* beginning of the directory. +*/ +void rewinddir_win32 (DIR *pDir) +{ +struct dir_win32 *stream = (struct dir_win32 *)pDir; + +if (stream ==NULL ) { + errno =EBADF ; +return ; + } + + stream->offset =0 ; + stream->current =QSLIST_FIRST (&stream->head); + +return ; +} + +/* +* seekdir_win32 - set the position of the next readdir() call in +the directory +* +* This function sets the position of the next readdir() call in +the directory +* from which the next readdir() call will start. +*/ +void seekdir_win32 (DIR *pDir,long pos) { +struct dir_win32 *stream = (struct dir_win32 *)pDir; +uint32_t index; + +if (stream ==NULL ) { + errno =EBADF ; +return ; + } + +if (pos <-1 ) { + errno =EINVAL ; +return ; + } + +if (pos ==-1 || pos >= (long )stream->total_entries) { +/* seek to the end */ + stream->offset = stream->total_entries; +return ; + } + +if (pos - (long )stream->offset ==0 ) { +/* no need to seek */ +return ; + } + +/* seek position from list head */ + + stream->current =QSLIST_FIRST (&stream->head); + +for (index =0 ; index < (uint32_t )pos; index++) { + stream->current =QSLIST_NEXT (stream->current, node); + } + stream->offset = index; + +return ; +} + +/* +* telldir_win32 - return current location in directory +* +* This function returns current location in directory. +*/ +long telldir_win32 (DIR *pDir) +{ +struct dir_win32 *stream = (struct dir_win32 *)pDir; + +if (stream ==NULL ) { + errno =EBADF ; +return -1 ; + } + +if (stream->offset > stream->total_entries) { +return -1 ; + } + +return (long )stream->offset; +}
Re: [PATCH v4 04/16] hw/9pfs: Implement Windows specific xxxdir() APIs
Quoted 326 lines · Guohuai
Quoted 306 lines
-----Original Message-----
From: Christian Schoenebeck <[email protected]>
Sent: Friday, February 3, 2023 22:41
To: Greg Kurz <[email protected]>; [email protected]
Cc: Meng, Bin <[email protected]>; Marc-André Lureau <[email protected]>; Daniel P. Berrangé <[email protected]>; Shi, Guohuai <[email protected]>
Subject: Re: [PATCH v4 04/16] hw/9pfs: Implement Windows specificxxxdir()APIsCAUTION: This email comes from a non Wind River email account! Do not click links or open attachments unless you recognize the sender and know the content is safe.
Quoted 55 lines · Guohuai
Quoted 55 lines
-----Original Message-----
From: Christian Schoenebeck <[email protected]>
Sent: Friday, February 3, 2023 20:25
To: Greg Kurz <[email protected]>; [email protected]
Cc: Shi, Guohuai <[email protected]>; Meng, Bin <[email protected]>; Marc-André Lureau <[email protected]>; Daniel P. Berrangé <[email protected]>
Subject: Re: [PATCH v4 04/16] hw/9pfs: Implement Windows specificxxxdir()APIsCAUTION: This email comes from a non Wind River email account! Do not click links or open attachments unless you recognize the sender and know the content is safe.
Quoted 5 lines · CET Bin Meng
From: Guohuai Shi <[email protected]>
This commit implements Windows specific
xxxdir()APIs for safety directory access.This issue deserves a link to either the previous discussion
and/or a link to this continuation of the discussion here, as it's not a trivial issue, with pros and cons been discussed for the individual, possible solutions.
Quoted 24 lines
Signed-off-by Guohuai Shi <[email protected]>Signed-off-by Bin Meng <[email protected]>---
hw/9pfs/9p-util.h | 6 + hw/9pfs/9p-util-win32.c | 296 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 302 insertions(+)hw/9pfs/9p-util.h index renamed from hw/9pfs/9p-util.h +0
-0
0f159fb4ce..c1c251fbd1100644hw/9pfs/9p-util.h +0
-0@@ -141,6 +141,12 @@ int unlinkat_win32(int dirfd, const char *pathname, int flags); int statfs_win32(const char *root_path,struct statfs *stbuf); int
openat_dir(int dirfd, const char *name); intopenat_file(int dirfd, const char *name, int flags, mode_t mode);hw/9pfs/9p-util.h +4
-0+DIR *opendir_win32 (const char *full_file_name);int +closedir_win32 (DIR *pDir);struct dirent *readdir_win32 (DIR +*pDir);void rewinddir_win32 (DIR *pDir);void seekdir_win32 (DIR +*pDir,long pos);long telldir_win32 (DIR *pDir);#endif Quoted 8 lines
Quoted 8 lines
hw/9pfs/9p-util-win32.c line 38
Quoted 8 lines
static inline void
close_preserve_errno(int fd)diff --gita/hw/9pfs/9p-util-win32.cb/hw/9pfs/9p-util-win32.cindex a99d579a06..5503199300 100644hw/9pfs/9p-util-win32.c +0
-0@@ -37,6 +37,13 @@* Windows does not support opendir, the directory fd is created by * CreateFile and convert to fd by _open_osfhandle(). Keep the fd open
Quoted 13 lines
Quoted 13 lines
will
hw/9pfs/9p-util-win32.c
Quoted 4 lines
hw/9pfs/9p-util-win32.c +3
-0* lock and protect the directory (can not be modified or replaced) +* +* 5. Windows and MinGW does not provide safety directory +accessing functions.
hw/9pfs/9p-util-win32.c line 43
Quoted 1 line
hw/9pfs/9p-util-win32.c +1
-0+* readdir(), seekdir() and telldir() may get or set wrong value because
hw/9pfs/9p-util-win32.c line 43
Quoted 1 line
hw/9pfs/9p-util-win32.c +1
-0+* directory entry data is not protected. I would rephrase that sentence, as it doesn't cover the root problem adequately. Maybe something like this:
Quoted 9 lines
Quoted 9 lines
5. Neither Windows native APIs, nor MinGW provide a POSIX compatible API for acquiring directory entries in a safe way. Calling those APIs (native
_findfirst()and_findnext()or MinGW'sreaddir(),seekdir()andtelldir()) directly can lead to an inconsistent state if directory is modified in between, e.g. the same directory appearing more than once in output, or directories not appearing at all in output even though they were neither newly created nor deleted. POSIX does not define what happens with deleted or newly created directories in between,but it guarantees a consistent state.
Quoted 3 lines
Quoted 3 lines
hw/9pfs/9p-util-win32.c line 46
Quoted 3 lines
hw/9pfs/9p-util-win32.c +2
-0+* +* This file re-write POSIX directory accessing functions and cache all
Quoted 140 lines
Quoted 140 lines
Quoted 5 lines
hw/9pfs/9p-util-win32.c +1
-0+* directory entries during opening. */ #include "qemu/osdep.h" hw/9pfs/9p-util-win32.c line 67
Quoted 10 lines
hw/9pfs/9p-util-win32.c +7
-0@@ -51,6 +58,27 @@#define V9FS_MAGIC 0x53465039/* string "9PFS" */ +/* +* MinGW and Windows does not provide safety way to seek +directory while other +* thread is modifying same directory. +* +* The two structures are used to cache all directory entries +when opening it.
Quoted 20 lines
hw/9pfs/9p-util-win32.c +16
-0+* Cached entries are always returned for read or seek. +*/ +struct dir_win32_entry { +QSLIST_ENTRY (dir_win32_entry) node; +struct _finddata_t dd_data; +}; + +struct dir_win32 { +struct dirent dd_dir; +uint32_t offset; +uint32_t total_entries; + QSLIST_HEAD(, dir_win32_entry) head; +struct dir_win32_entry *current; +char dd_name[1 ]; +}; +/* * win32_error_to_posix - convert Win32 error to POSIX error number * hw/9pfs/9p-util-win32.c int qemu_mknodat(int dirfd, const char · line 1005
Quoted 1 line
hw/9pfs/9p-util-win32.c +0
-0@@ -977,3 +1005,271 @@ int qemu_mknodat(int dirfd, const char *filename,
mode_tmode,dev_tdev)Quoted 18 lines
hw/9pfs/9p-util-win32.c +15
-0errno =ENOTSUP ;return -1 ; } + +/* +* opendir_win32 - open a directory +* +* This function opens a directory and caches all directory entries. +*/ +DIR *opendir_win32 (const char *full_file_name) { +HANDLE hDir =INVALID_HANDLE_VALUE ; +DWORD attribute; +intptr_t dd_handle =-1 ; +struct _finddata_t dd_data; + +struct dir_win32 *stream =NULL ; +struct dir_win32_entry *dir_entry; +struct dir_win32_entry *prev;hw/9pfs/9p-util-win32.c int qemu_mknodat(int dirfd, const char *filename, mode_t mode, dev_t dev)
Quoted 10 lines
hw/9pfs/9p-util-win32.c +10
-0+struct dir_win32_entry *next; + +int err =0 ; +int find_status; +uint32_t index; + +/* open directory to prevent it being removed */ + + hDir = CreateFile(full_file_name, GENERIC_READ, + FILE_SHARE_READ | FILE_SHARE_WRITE |
FILE_SHARE_DELETE,Quoted 17 lines
hw/9pfs/9p-util-win32.c +17
-0+NULL , +OPEN_EXISTING ,FILE_FLAG_BACKUP_SEMANTICS , + NULL); + +if (hDir ==INVALID_HANDLE_VALUE ) { + err =win32_error_to_posix (GetLastError ()); + goto out; + } + + attribute =GetFileAttributes (full_file_name); + +/* symlink is not allow */ +if (attribute ==INVALID_FILE_ATTRIBUTES + || (attribute &FILE_ATTRIBUTE_REPARSE_POINT ) !=0 ) { + err =EACCES ; + goto out; + }hw/9pfs/9p-util-win32.c int qemu_mknodat(int dirfd, const char *filename, mode_t mode, dev_t dev)
Quoted 10 lines
hw/9pfs/9p-util-win32.c +10
-0+ +/* check if it is a directory */ +if ((attribute &FILE_ATTRIBUTE_DIRECTORY ) ==0 ) { + err =ENOTDIR ; + goto out; + } + +/* +* findfirst() need suffix format name like "\dir1\dir2\*", +allocate more
hw/9pfs/9p-util-win32.c int qemu_mknodat(int dirfd, const char *filename, mode_t mode, dev_t dev)
Quoted 4 lines
hw/9pfs/9p-util-win32.c +4
-0+* buffer to store suffix. +*/ + stream = g_malloc0(sizeof(struct dir_win32) + + strlen(full_file_name) +3);
Quoted 19 lines
hw/9pfs/9p-util-win32.c +19
-0+QSLIST_INIT (&stream->head); + +strcpy (stream->dd_name, full_file_name); +strcat (stream->dd_name,"\\*" ); + + dd_handle =_findfirst (stream->dd_name, &dd_data); + +if (dd_handle ==-1 ) { + err = errno; + goto out; + } + + index =0 ; + +/* read all entries to link list */ + do { + dir_entry = g_malloc0(sizeof(struct dir_win32_entry)); + memcpy(&dir_entry->dd_data, &dd_data, sizeof(dd_data)); + if (index == 0) {hw/9pfs/9p-util-win32.c int qemu_mknodat(int dirfd, const char *filename, mode_t mode, dev_t dev) · line 1090
Quoted 10 lines
hw/9pfs/9p-util-win32.c +10
-0+QSLIST_INSERT_HEAD (&stream->head, dir_entry, node); + }else { +QSLIST_INSERT_AFTER (prev, dir_entry, node); + } + + prev = dir_entry; + find_status =_findnext (dd_handle, &dd_data); + + index++; + }while (find_status ==0 );So you decided to go for the solution that caches all entries of a directory in RAM.
So don't you think my last suggested solution that would call native
_findfirst()and_findnext()directly, but without any chaching and instead picking the relevent entry simply by inode number, might be a better candidate as a starting point for landing Windows support? Link to that previous
suggestion:Quoted 4 lines
I did a quick test for caching data without name entry, but it failed for
reading + deleting directory on Windows host (like "rm -rf" for a directory).
Quoted 11 lines
The root cause is: Windows's directory entry is not cached. If there is 100 files in a directory:
File1
File2
...
File100When "rm -rf" is working:
It read first 10 entries, and remove them. 9pfs may seek and re-seek to
offset 10 to read next 10 entries.
Quoted 2 lines
But Windows and MinGW does not provide rewinddir.
If we usingfindfirst()and findnext to seek to offset 10, then we will notget File11 but get File 21 (because we skipped 10 entries by
seekdir()).I assume you are referring to a simple solution like MinGW does, i.e. a consecutive dense index (0,1,2,3,...n-1 where n is the current total amount of directory entries). That would not work, yes. But that's not what I suggested.
With an inode number based lookup you would not seek to an incorrect entry
...Quoted 1 line
If we removed some entries in directory, inode number is useless because we
can not find it again.
You *can* recover from the previous inode number, even if any directory entry has been deleted in the meantime: you would lookup the entry with the next higher inode number.
Example, say initial directory state on host is:
name inode-nr aaa 8 bbb 3 ccc 4 ddd 2 eee 9Say client is looking up exactly 2 entries, you would return to client in this order (by inode-nr):
1. ddd
2. bbbNow say "bbb" (a.k.a. previous) and "ccc" (a.k.a next) are removed. Directory state on host is now:
name inode-nr aaa 8 ddd 2 eee 9Quoted 8 lines
Subsequently the last directory entries are requested by client. Previous inode number (stored in RAM) was 3, which no longer exists, so you lookup the entry with the next higher inode number than 3, which is now 8 in this example. Hence you would eventually return to client (in this order):
3. aaa
4. eeeYes, it can work by using inode number (called File ID on Windows host: https://learn.microsoft.com/en-us/windows/win32/api/winbase/ns-winbase-file_id_info).
However, Windows does not provide a function to get file information by file ID.
That means, for anytime of seeking directory, 9pfs need to do the following sequence work to locate a name entry:1. findfirst
2. CreateFile to get file handle
3. GetFileInformationByHandleEx to get file ID (https://learn.microsoft.com/en-us/windows/win32/api/minwinbase/ne-minwinbase-file_info_by_handle_class)
4. Close file handle and return if the file ID is match
5. findnext
6. repeat to step #2
Quoted 2 lines
Windows does not short file name entry by file ID and the file ID is 128-bit integer. When there are many entries in directory, seeking directory will cause a very bad performance.
I know, it's an n-square performance issue and what I already wrote in the summary of the linked original suggestion [1] in v3 before, quote:
+ Relatively straight-forward to implement.
+ No (major) changes in 9pfs code base required.
- Still n-square performance issue (neglectable to land Windows host support
IMO).
o Consistency assured for "most" cases, except one: if hardlinks are inserted in between then it might fail
[1] https://lore.kernel.org/qemu-devel/2468168.SvRIHAoRfs@silver/
The idea was to use that just as a starting point to land Windows host support ASAP, slower on large dirs compared to other solutions, yes, but with guaranteed correct and deterministic behaviour. And then on the long run we would of course replace that with a more performant solution.
I mean, this is really simple to implement, so I would at least test it. If it really runs horribly slow we could still discuss faster solutions, which are however all much more tricky.
Quoted 1 line
So I think store all name entries would be better than store all file ID.
As already discussed, NTFS allows up to (2^32 - 1) = 4,294,967,295 entries per directory. So caching only one directory (entirely) in RAM can already exceed the available RAM, which would crash QEMU. Multiplied by an expected amount of directory lookups by client and we even get into much higher categories, even with much smaller individual directory sizes.
Quoted 208 lines
Quoted 205 lines
Quoted 18 lines
Thanks
GuohuaiQuoted 10 lines
Quoted 10 lines
hw/9pfs/9p-util-win32.c +10
-0+ +if (errno ==ENOENT ) { +/* No more matching files could be found, clean errno */ + errno =0 ; + }else { + err = errno; + goto out; + } + + stream->total_entries = index;Quoted 8 lines
Quoted 8 lines
hw/9pfs/9p-util-win32.c int qemu_mknodat(int dirfd, const char *filename, mode_t mode, dev_t dev)
Quoted 8 lines
hw/9pfs/9p-util-win32.c +8
-0+ stream->current =QSLIST_FIRST (&stream->head); + +out: +if (err !=0 ) { + errno = err; +/* free whole list */ +if (stream !=NULL ) { +QSLIST_FOREACH_SAFE (dir_entry, &stream->head, node, next){
Quoted 34 lines
Quoted 34 lines
hw/9pfs/9p-util-win32.c
Quoted 2 lines
hw/9pfs/9p-util-win32.c +2
-0+ QSLIST_REMOVE(&stream->head, dir_entry, +dir_win32_entry,node);
Quoted 31 lines
hw/9pfs/9p-util-win32.c +31
-0+g_free (dir_entry); + } +g_free (stream); + stream =NULL ; + } + } + +/* after cached all entries, this handle is useless */ +if (dd_handle !=-1 ) { +_findclose (dd_handle); + } + +if (hDir !=INVALID_HANDLE_VALUE ) { +CloseHandle (hDir); + } + +return (DIR *)stream; +} + +/* +* closedir_win32 - close a directory +* +* This function closes directory and free all cached resources. +*/ +int closedir_win32 (DIR *pDir) +{ +struct dir_win32 *stream = (struct dir_win32 *)pDir; +struct dir_win32_entry *dir_entry; +struct dir_win32_entry *next; + +if (stream ==NULL ) {Quoted 8 lines
Quoted 8 lines
hw/9pfs/9p-util-win32.c int qemu_mknodat(int dirfd, const char *filename, mode_t mode, dev_t dev)
Quoted 8 lines
hw/9pfs/9p-util-win32.c +8
-0+ errno =EBADF ; +return -1 ; + } + +/* free all resources */ + + QSLIST_FOREACH_SAFE(dir_entry, &stream->head, node, next) { + QSLIST_REMOVE(&stream->head, dir_entry, dir_win32_entry,node);
Quoted 133 lines
Quoted 130 lines
Quoted 129 lines
hw/9pfs/9p-util-win32.c +128
-0+g_free (dir_entry); + } + +g_free (stream); + +return 0 ; +} + +/* +* readdir_win32 - read a directory +* +* This function reads a directory entry from cached entry list. +*/ +struct dirent *readdir_win32 (DIR *pDir) { +struct dir_win32 *stream = (struct dir_win32 *)pDir; + +if (stream ==NULL ) { + errno =EBADF ; +return NULL ; + } + +if (stream->offset >= stream->total_entries) { +/* reach to the end, return NULL without set errno */ +return NULL ; + } + +memcpy (stream->dd_dir.d_name, + stream->current->dd_data.name, +sizeof (stream->dd_dir.d_name)); + +/* Windows does not provide inode number */ + stream->dd_dir.d_ino =0 ; + stream->dd_dir.d_reclen =0 ; + stream->dd_dir.d_namlen =strlen (stream->dd_dir.d_name); + + stream->offset++; + stream->current =QSLIST_NEXT (stream->current, node); + +return &stream->dd_dir; +} + +/* +* rewinddir_win32 - reset directory stream +* +* This function resets the position of the directory stream to +the +* beginning of the directory. +*/ +void rewinddir_win32 (DIR *pDir) +{ +struct dir_win32 *stream = (struct dir_win32 *)pDir; + +if (stream ==NULL ) { + errno =EBADF ; +return ; + } + + stream->offset =0 ; + stream->current =QSLIST_FIRST (&stream->head); + +return ; +} + +/* +* seekdir_win32 - set the position of the next readdir() call in +the directory +* +* This function sets the position of the next readdir() call in +the directory +* from which the next readdir() call will start. +*/ +void seekdir_win32 (DIR *pDir,long pos) { +struct dir_win32 *stream = (struct dir_win32 *)pDir; +uint32_t index; + +if (stream ==NULL ) { + errno =EBADF ; +return ; + } + +if (pos <-1 ) { + errno =EINVAL ; +return ; + } + +if (pos ==-1 || pos >= (long )stream->total_entries) { +/* seek to the end */ + stream->offset = stream->total_entries; +return ; + } + +if (pos - (long )stream->offset ==0 ) { +/* no need to seek */ +return ; + } + +/* seek position from list head */ + + stream->current =QSLIST_FIRST (&stream->head); + +for (index =0 ; index < (uint32_t )pos; index++) { + stream->current =QSLIST_NEXT (stream->current, node); + } + stream->offset = index; + +return ; +} + +/* +* telldir_win32 - return current location in directory +* +* This function returns current location in directory. +*/ +long telldir_win32 (DIR *pDir) +{ +struct dir_win32 *stream = (struct dir_win32 *)pDir; + +if (stream ==NULL ) { + errno =EBADF ; +return -1 ; + } + +if (stream->offset > stream->total_entries) { +return -1 ; + } + +return (long )stream->offset; +}
RE: [PATCH v4 04/16] hw/9pfs: Implement Windows specific xxxdir() APIs
Quoted 372 lines
-----Original Message-----
From: Christian Schoenebeck <[email protected]>
Sent: Saturday, February 4, 2023 01:55
To: Greg Kurz <[email protected]>; [email protected]
Cc: Meng, Bin <[email protected]>; Marc-André Lureau <[email protected]>; Daniel P. Berrangé <[email protected]>; Shi, Guohuai <[email protected]>
Subject: Re: [PATCH v4 04/16] hw/9pfs: Implement Windows specificxxxdir()APIsCAUTION: This email comes from a non Wind River email account! Do not click links or open attachments unless you recognize the sender and know the content is safe.
Quoted 74 lines · Guohuai
Quoted 74 lines
-----Original Message-----
From: Christian Schoenebeck <[email protected]>
Sent: Friday, February 3, 2023 22:41
To: Greg Kurz <[email protected]>; [email protected]
Cc: Meng, Bin <[email protected]>; Marc-André Lureau <[email protected]>; Daniel P. Berrangé <[email protected]>; Shi, Guohuai <[email protected]>
Subject: Re: [PATCH v4 04/16] hw/9pfs: Implement Windows specificxxxdir()APIsCAUTION: This email comes from a non Wind River email account! Do not click links or open attachments unless you recognize the sender and know the content is safe.
Quoted 58 lines · Guohuai
Quoted 58 lines
-----Original Message-----
From: Christian Schoenebeck <[email protected]>
Sent: Friday, February 3, 2023 20:25
To: Greg Kurz <[email protected]>; [email protected]
Cc: Shi, Guohuai <[email protected]>; Meng, Bin <[email protected]>; Marc-André Lureau <[email protected]>; Daniel P. Berrangé <[email protected]>
Subject: Re: [PATCH v4 04/16] hw/9pfs: Implement Windows specificxxxdir()APIsCAUTION: This email comes from a non Wind River email account! Do not click links or open attachments unless you recognize the sender and know the content is safe.
Quoted 5 lines · CET Bin Meng
From: Guohuai Shi <[email protected]>
This commit implements Windows specific
xxxdir()APIs for safety directory access.This issue deserves a link to either the previous discussion
Link:
https://lore.kernel.org/qemu-devel/2830993.GtbaR8S6b6@silver/and/or a link to this continuation of the discussion here, as it's not a trivial issue, with pros and cons been discussed for the individual, possible solutions.
Quoted 25 lines
Signed-off-by Guohuai Shi <[email protected]>Signed-off-by Bin Meng <[email protected]>---
hw/9pfs/9p-util.h | 6 + hw/9pfs/9p-util-win32.c | 296 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 302 insertions(+)hw/9pfs/9p-util.h index renamed from hw/9pfs/9p-util.h +0
-0
0f159fb4ce..c1c251fbd1100644hw/9pfs/9p-util.h +0
-0@@ -141,6 +141,12 @@ int unlinkat_win32(int dirfd, const char *pathname, int flags); int statfs_win32(const char *root_path, struct statfs *stbuf); int openat_dir(int dirfd,const char *name); int
openat_file(int dirfd, const char *name, int flags, mode_t mode);hw/9pfs/9p-util.h +5
-0+DIR *opendir_win32 (const char *full_file_name);int +closedir_win32 (DIR *pDir);struct dirent *readdir_win32 (DIR +*pDir);void rewinddir_win32 (DIR *pDir);void +seekdir_win32 (DIR *pDir,long pos);long telldir_win32 (DIR +*pDir);#endif Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
hw/9pfs/9p-util-win32.c line 37
Quoted 7 lines
static inline void
close_preserve_errno(int fd)diff --gita/hw/9pfs/9p-util-win32.cb/hw/9pfs/9p-util-win32.cindex a99d579a06..5503199300 100644hw/9pfs/9p-util-win32.c +0
-0@@ -37,6 +37,13 @@* Windows does not support opendir, the directory fd is created by
Quoted 55 lines
Quoted 55 lines
Quoted 1 line
Quoted 1 line
hw/9pfs/9p-util-win32.c line 38
Quoted 1 line
hw/9pfs/9p-util-win32.c +0
-0* CreateFile and convert to fd by _open_osfhandle(). Keep the fd open
Quoted 13 lines
Quoted 13 lines
will
hw/9pfs/9p-util-win32.c
Quoted 4 lines
hw/9pfs/9p-util-win32.c +3
-0* lock and protect the directory (can not be modified or replaced) +* +* 5. Windows and MinGW does not provide safety directory +accessing functions.
hw/9pfs/9p-util-win32.c line 43
Quoted 1 line
hw/9pfs/9p-util-win32.c +1
-0+* readdir(), seekdir() and telldir() may get or set wrong value because
hw/9pfs/9p-util-win32.c line 43
Quoted 1 line
hw/9pfs/9p-util-win32.c +1
-0+* directory entry data is not protected. I would rephrase that sentence, as it doesn't cover the root problem adequately. Maybe something like this:
Quoted 10 lines
Quoted 10 lines
5. Neither Windows native APIs, nor MinGW provide a POSIX compatible API for acquiring directory entries in a safe way. Calling those APIs (native
_findfirst()and_findnext()or MinGW'sreaddir(),seekdir()andtelldir()) directly can lead to an inconsistent state if directory is modified in between, e.g. the same directory appearing more than once in output, or directories not appearing at all in output even though they were neither newly created nor deleted. POSIX does not define what happens with deleted or newly created directories in between,but it guarantees a consistent state.
Quoted 3 lines
Quoted 3 lines
hw/9pfs/9p-util-win32.c line 46
Quoted 3 lines
hw/9pfs/9p-util-win32.c +2
-0+* +* This file re-write POSIX directory accessing functions and cache all
Quoted 25 lines
Quoted 25 lines
Quoted 5 lines
hw/9pfs/9p-util-win32.c +1
-0+* directory entries during opening. */ #include "qemu/osdep.h" hw/9pfs/9p-util-win32.c line 67
Quoted 10 lines
hw/9pfs/9p-util-win32.c +7
-0@@ -51,6 +58,27 @@#define V9FS_MAGIC 0x53465039/* string "9PFS" */ +/* +* MinGW and Windows does not provide safety way to seek +directory while other +* thread is modifying same directory. +* +* The two structures are used to cache all directory entries +when opening it.
Quoted 9 lines
hw/9pfs/9p-util-win32.c +9
-0+* Cached entries are always returned for read or seek. +*/ +struct dir_win32_entry { +QSLIST_ENTRY (dir_win32_entry) node; +struct _finddata_t dd_data; }; + +struct dir_win32 { +struct dirent dd_dir; +uint32_t offset;Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
hw/9pfs/9p-util-win32.c
Quoted 8 lines
hw/9pfs/9p-util-win32.c +6
-0+uint32_t total_entries; + QSLIST_HEAD(, dir_win32_entry) head; +struct dir_win32_entry *current; +char dd_name[1 ]; +}; +/* * win32_error_to_posix - convert Win32 error to POSIX error number
Quoted 99 lines
Quoted 99 lines
Quoted 99 lines
Quoted 99 lines
hw/9pfs/9p-util-win32.c int qemu_mknodat(int dirfd, const char · line 1005
Quoted 3 lines
hw/9pfs/9p-util-win32.c +0
-0* @@ -977,3 +1005,271 @@ int qemu_mknodat(int dirfd, const char *filename,
mode_tmode,dev_tdev)Quoted 18 lines
hw/9pfs/9p-util-win32.c +15
-0errno =ENOTSUP ;return -1 ; } + +/* +* opendir_win32 - open a directory +* +* This function opens a directory and caches all directory entries. +*/ +DIR *opendir_win32 (const char *full_file_name) { +HANDLE hDir =INVALID_HANDLE_VALUE ; +DWORD attribute; +intptr_t dd_handle =-1 ; +struct _finddata_t dd_data; + +struct dir_win32 *stream =NULL ; +struct dir_win32_entry *dir_entry; +struct dir_win32_entry *prev;hw/9pfs/9p-util-win32.c int qemu_mknodat(int dirfd, const char *filename, mode_t mode, dev_t dev)
Quoted 10 lines
hw/9pfs/9p-util-win32.c +10
-0+struct dir_win32_entry *next; + +int err =0 ; +int find_status; +uint32_t index; + +/* open directory to prevent it being removed */ + + hDir = CreateFile(full_file_name, GENERIC_READ, + FILE_SHARE_READ | FILE_SHARE_WRITE |
FILE_SHARE_DELETE,Quoted 17 lines
hw/9pfs/9p-util-win32.c +17
-0+NULL , +OPEN_EXISTING , + FILE_FLAG_BACKUP_SEMANTICS, NULL); + +if (hDir ==INVALID_HANDLE_VALUE ) { + err =win32_error_to_posix (GetLastError ()); + goto out; + } + + attribute =GetFileAttributes (full_file_name); + +/* symlink is not allow */ +if (attribute ==INVALID_FILE_ATTRIBUTES + || (attribute &FILE_ATTRIBUTE_REPARSE_POINT ) !=0 ) { + err =EACCES ; + goto out; + }hw/9pfs/9p-util-win32.c int qemu_mknodat(int dirfd, const char *filename, mode_t mode, dev_t dev)
Quoted 10 lines
hw/9pfs/9p-util-win32.c +10
-0+ +/* check if it is a directory */ +if ((attribute &FILE_ATTRIBUTE_DIRECTORY ) ==0 ) { + err =ENOTDIR ; + goto out; + } + +/* +* findfirst() need suffix format name like +"\dir1\dir2\*", allocate more
hw/9pfs/9p-util-win32.c int qemu_mknodat(int dirfd, const char *filename, mode_t mode, dev_t dev)
Quoted 4 lines
hw/9pfs/9p-util-win32.c +4
-0+* buffer to store suffix. +*/ + stream = g_malloc0(sizeof(struct dir_win32) + + strlen(full_file_name) +3);
Quoted 19 lines
hw/9pfs/9p-util-win32.c +19
-0+QSLIST_INIT (&stream->head); + +strcpy (stream->dd_name, full_file_name); +strcat (stream->dd_name,"\\*" ); + + dd_handle =_findfirst (stream->dd_name, &dd_data); + +if (dd_handle ==-1 ) { + err = errno; + goto out; + } + + index =0 ; + +/* read all entries to link list */ + do { + dir_entry = g_malloc0(sizeof(struct dir_win32_entry)); + memcpy(&dir_entry->dd_data, &dd_data, sizeof(dd_data)); + if (index == 0) {hw/9pfs/9p-util-win32.c int qemu_mknodat(int dirfd, const char *filename, mode_t mode, dev_t dev) · line 1090
Quoted 10 lines
hw/9pfs/9p-util-win32.c +10
-0+QSLIST_INSERT_HEAD (&stream->head, dir_entry, node); + }else { +QSLIST_INSERT_AFTER (prev, dir_entry, node); + } + + prev = dir_entry; + find_status =_findnext (dd_handle, &dd_data); + + index++; + }while (find_status ==0 );So you decided to go for the solution that caches all entries of a directory in RAM.
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
So don't you think my last suggested solution that would call native
_findfirst()and_findnext()directly, but without any chaching and instead picking the relevent entry simply by inode number, might be a better candidate as a starting point for landing Windowssupport?
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 5 lines
Link to that previous
suggestion:https://lore.kernel.org/qemu-devel/2468168.SvRIHAoRfs@silver/
I did a quick test for caching data without name entry, but it failed for
reading + deleting directory on Windows host (like "rm -rf" for a
directory).
Quoted 57 lines
Quoted 57 lines
Quoted 8 lines
The root cause is: Windows's directory entry is not cached. If there is 100 files in a directory:
File1
File2
...
File100Quoted 4 lines
When "rm -rf" is working:
It read first 10 entries, and remove them. 9pfs may seek and re-seek to
offset 10 to read next 10 entries.
Quoted 3 lines
But Windows and MinGW does not provide rewinddir.
If we usingfindfirst()and findnext to seek to offset 10, then we will notget File11 but get File 21 (because we skipped 10 entries by
seekdir()).I assume you are referring to a simple solution like MinGW does, i.e. a consecutive dense index (0,1,2,3,...n-1 where n is the current total amount of directory entries). That would not work, yes. But that's not what I suggested.
With an inode number based lookup you would not seek to an incorrect entry ...
Quoted 2 lines
If we removed some entries in directory, inode number is useless because we
can not find it again.
You *can* recover from the previous inode number, even if any directory entry has been deleted in the meantime: you would lookup the entry with the next higher inode number.
Example, say initial directory state on host is:
name inode-nr aaa 8 bbb 3 ccc 4 ddd 2 eee 9Say client is looking up exactly 2 entries, you would return to client in this order (by inode-nr):
1. ddd
2. bbbNow say "bbb" (a.k.a. previous) and "ccc" (a.k.a next) are removed. Directory state on host is now:
name inode-nr aaa 8 ddd 2 eee 9Quoted 4 lines
Quoted 4 lines
Subsequently the last directory entries are requested by client. Previous inode number (stored in RAM) was 3, which no longer exists, so you lookup the entry with the next higher inode number than 3, which is now 8 in this example. Hence you would eventually return to
client (in this order):
Quoted 6 lines
Quoted 4 lines
3. aaa
4. eeeYes, it can work by using inode number (called File ID on Windows host:
https://learn.microsoft.com/en-us/windows/win32/api/winbase/ns-
winbase-file_id_info).Quoted 1 line
However, Windows does not provide a function to get file information by
file ID.
Quoted 1 line
That means, for anytime of seeking directory, 9pfs need to do the following
sequence work to locate a name entry:
Quoted 5 lines
1. findfirst
2. CreateFile to get file handle
3. GetFileInformationByHandleEx to get file ID
(https://learn.microsoft.com/en-us/windows/win32/api/minwinbase/ne-min
Quoted 5 lines
winbase-file_info_by_handle_class)
4. Close file handle and return if the file ID is match 5. findnext 6. repeat to step #2Windows does not short file name entry by file ID and the file ID is 128-bit
integer.
Quoted 1 line
When there are many entries in directory, seeking directory will cause a
very bad performance.
I know, it's an n-square performance issue and what I already wrote in the summary of the linked original suggestion [1] in v3 before, quote:
+ Relatively straight-forward to implement.
Quoted 8 lines
+ No (major) changes in 9pfs code base required.
- Still n-square performance issue (neglectable to land Windows host
supportIMO).o Consistency assured for "most" cases, except one: if hardlinks are inserted in between then it might fail
readdir() on Linux host may also return the deleted entries. And POSIX specification does not mention about the consistency issue.
NTFS file id is the $MFT index id. It will keen unique until file is deleted. But the index id may be reuse if delete and re-create many files.
Saving file id instead of name will make consistency better, but may not cover all status. Because read directory is not a "atomic" operation.
Quoted 12 lines
[1] https://lore.kernel.org/qemu-devel/2468168.SvRIHAoRfs@silver/
The idea was to use that just as a starting point to land Windows host support ASAP, slower on large dirs compared to other solutions, yes, but with guaranteed correct and deterministic behaviour. And then on the long run we would of course replace that with a more performant solution.
I mean, this is really simple to implement, so I would at least test it. If it really runs horribly slow we could still discuss faster solutions, which are however all much more tricky.
I did a basic test on Windows host, here is the code:
st = clock();
pDir = opendir_win32(TEST_DIR);
if (pDir == NULL)
return -1;
while ((pEnt = readdir_win32(pDir)) != NULL)
{
totals++;
}
closedir_win32(pDir);
ed = clock();
printf("total = %d clocks = %d %d\n", totals, ed - st, CLOCKS_PER_SEC);My local storage is SSD disk.
Run this test for 100, 1000, 10000 entries.
For file name cache solution, the time cost is: 2, 9, 44 (in ms).
For file id cache solution, the time cost: 3, 438, 4338 (in ms).
I already used OpenFileById() to make it faster instead of CreateFile(). If I use CreateFile, it need more than 80 seconds.
The performance looks like not good.
And actually, it would be worse in 9pfs.
Because in current design, 9pfs may seek forward and seek back several times during reading directory, which may cause the performance worse.
Quoted 8 lines
Quoted 1 line
So I think store all name entries would be better than store all file ID.
As already discussed, NTFS allows up to (2^32 - 1) = 4,294,967,295 entries per directory. So caching only one directory (entirely) in RAM can already exceed the available RAM, which would crash QEMU. Multiplied by an expected amount of directory lookups by client and we even get into much higher categories, even with much smaller individual directory sizes.
Windows file id structure is 24 bytes, which is not a small structure. If you think the performance is acceptable, I can rework this commit based on file id.
Quoted 209 lines
Quoted 60 lines
Quoted 60 lines
Quoted 19 lines
Thanks
GuohuaiQuoted 11 lines
Quoted 11 lines
hw/9pfs/9p-util-win32.c +11
-0+ +if (errno ==ENOENT ) { +/* No more matching files could be found, clean errno */ + errno =0 ; + }else { + err = errno; + goto out; + } + + stream->total_entries = index; + stream->current =QSLIST_FIRST (&stream->head);Quoted 8 lines
Quoted 8 lines
hw/9pfs/9p-util-win32.c int qemu_mknodat(int dirfd, const char *filename, mode_t mode, dev_t dev)
Quoted 8 lines
hw/9pfs/9p-util-win32.c +8
-0+ +out: +if (err !=0 ) { + errno = err; +/* free whole list */ +if (stream !=NULL ) { +QSLIST_FOREACH_SAFE (dir_entry, &stream->head, +node, next){
Quoted 32 lines
Quoted 32 lines
hw/9pfs/9p-util-win32.c
Quoted 2 lines
hw/9pfs/9p-util-win32.c +2
-0+ QSLIST_REMOVE(&stream->head, dir_entry, +dir_win32_entry,node);
Quoted 29 lines
hw/9pfs/9p-util-win32.c +29
-0+g_free (dir_entry); + } +g_free (stream); + stream =NULL ; + } + } + +/* after cached all entries, this handle is useless */ +if (dd_handle !=-1 ) { +_findclose (dd_handle); + } + +if (hDir !=INVALID_HANDLE_VALUE ) { +CloseHandle (hDir); + } + +return (DIR *)stream; +} + +/* +* closedir_win32 - close a directory +* +* This function closes directory and free all cached resources. +*/ +int closedir_win32 (DIR *pDir) { +struct dir_win32 *stream = (struct dir_win32 *)pDir; +struct dir_win32_entry *dir_entry; +struct dir_win32_entry *next; +Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
hw/9pfs/9p-util-win32.c int qemu_mknodat(int dirfd, const char *filename, mode_t mode, dev_t dev)
Quoted 8 lines
hw/9pfs/9p-util-win32.c +8
-0+if (stream ==NULL ) { + errno =EBADF ; +return -1 ; + } + +/* free all resources */ + +QSLIST_FOREACH_SAFE (dir_entry, &stream->head, node, next){
Quoted 138 lines
Quoted 135 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
+ QSLIST_REMOVE(&stream->head,
dir_entry,
+dir_win32_entry,node);
Quoted 130 lines
Quoted 127 lines
Quoted 126 lines
hw/9pfs/9p-util-win32.c +125
-0+g_free (dir_entry); + } + +g_free (stream); + +return 0 ; +} + +/* +* readdir_win32 - read a directory +* +* This function reads a directory entry from cached entry list. +*/ +struct dirent *readdir_win32 (DIR *pDir) { +struct dir_win32 *stream = (struct dir_win32 *)pDir; + +if (stream ==NULL ) { + errno =EBADF ; +return NULL ; + } + +if (stream->offset >= stream->total_entries) { +/* reach to the end, return NULL without set errno */ +return NULL ; + } + +memcpy (stream->dd_dir.d_name, + stream->current->dd_data.name, +sizeof (stream->dd_dir.d_name)); + +/* Windows does not provide inode number */ + stream->dd_dir.d_ino =0 ; + stream->dd_dir.d_reclen =0 ; + stream->dd_dir.d_namlen =strlen (stream->dd_dir.d_name); + + stream->offset++; + stream->current =QSLIST_NEXT (stream->current, node); + +return &stream->dd_dir; +} + +/* +* rewinddir_win32 - reset directory stream +* +* This function resets the position of the directory stream +to the +* beginning of the directory. +*/ +void rewinddir_win32 (DIR *pDir) { +struct dir_win32 *stream = (struct dir_win32 *)pDir; + +if (stream ==NULL ) { + errno =EBADF ; +return ; + } + + stream->offset =0 ; + stream->current =QSLIST_FIRST (&stream->head); + +return ; +} + +/* +* seekdir_win32 - set the position of the next readdir() +call in the directory +* +* This function sets the position of the next readdir() call +in the directory +* from which the next readdir() call will start. +*/ +void seekdir_win32 (DIR *pDir,long pos) { +struct dir_win32 *stream = (struct dir_win32 *)pDir; +uint32_t index; + +if (stream ==NULL ) { + errno =EBADF ; +return ; + } + +if (pos <-1 ) { + errno =EINVAL ; +return ; + } + +if (pos ==-1 || pos >= (long )stream->total_entries) { +/* seek to the end */ + stream->offset = stream->total_entries; +return ; + } + +if (pos - (long )stream->offset ==0 ) { +/* no need to seek */ +return ; + } + +/* seek position from list head */ + + stream->current =QSLIST_FIRST (&stream->head); + +for (index =0 ; index < (uint32_t )pos; index++) { + stream->current =QSLIST_NEXT (stream->current, node); + } + stream->offset = index; + +return ; +} + +/* +* telldir_win32 - return current location in directory +* +* This function returns current location in directory. +*/ +long telldir_win32 (DIR *pDir) { +struct dir_win32 *stream = (struct dir_win32 *)pDir; + +if (stream ==NULL ) { + errno =EBADF ; +return -1 ; + } + +if (stream->offset > stream->total_entries) { +return -1 ; + } + +return (long )stream->offset; }
Re: [PATCH v4 04/16] hw/9pfs: Implement Windows specific xxxdir() APIs
On Monday, February 6, 2023 6:37:16 AM CET Shi, Guohuai wrote:
[...]
Quoted 11 lines
Quoted 11 lines
I know, it's an n-square performance issue and what I already wrote in the summary of the linked original suggestion [1] in v3 before, quote:
+ Relatively straight-forward to implement.
+ No (major) changes in 9pfs code base required.
- Still n-square performance issue (neglectable to land Windows host
supportIMO).
Quoted 5 lines
Quoted 2 lines
o Consistency assured for "most" cases, except one: if hardlinks are inserted in between then it might fail
readdir()on Linux host may also return the deleted entries. And POSIX specification does not mention about the consistency issue.
POSIX explicitly specifies that 1. new and 2. deleted entries may or may not appear in result and leaves that implementation specific. That was never our concern.
And yes, POSIX does not explicitly discuss consistency concerning entries that have neither been added or removed, but this expectation is implied. In practice double entries are probably less of an issue, client might be able to handle that without misbehaviour (haven't checked this at all yet), but if the implementation would lead to chances that entries may *never* appear to clients at all, even after refreshing periodically, I mean how could you work with a file system like that?
Quoted 5 lines
NTFS file id is the $MFT index id. It will keen unique until file is deleted. But the index id may be reuse if delete and re-create many files.
Saving file id instead of name will make consistency better, but may not cover all status. Because read directory is not a "atomic" operation.
I don't see an issue with that, because these are entries that were either added or removed, we don't care about them. And their file IDs would not affect fetching the other directory entries that have not been touched in between.
And we are also not questioning atomicity here, but consistency.
Quoted 31 lines
Quoted 11 lines
[1] https://lore.kernel.org/qemu-devel/2468168.SvRIHAoRfs@silver/
The idea was to use that just as a starting point to land Windows host support ASAP, slower on large dirs compared to other solutions, yes, but with guaranteed correct and deterministic behaviour. And then on the long run we would of course replace that with a more performant solution.
I mean, this is really simple to implement, so I would at least test it. If it really runs horribly slow we could still discuss faster solutions, which are however all much more tricky.
I did a basic test on Windows host, here is the code:
st = clock(); pDir = opendir_win32(TEST_DIR); if (pDir == NULL) return -1; while ((pEnt = readdir_win32(pDir)) != NULL) { totals++; } closedir_win32(pDir); ed = clock(); printf("total = %d clocks = %d %d\n", totals, ed - st, CLOCKS_PER_SEC);My local storage is SSD disk.
Quoted 8 lines
Run this test for 100, 1000, 10000 entries.
For file name cache solution, the time cost is: 2, 9, 44 (in ms).
For file id cache solution, the time cost: 3, 438, 4338 (in ms).
I already usedOpenFileById()to make it faster instead ofCreateFile(). If I use CreateFile, it need more than 80 seconds.The performance looks like not good.
And actually, it would be worse in 9pfs.
Because in current design, 9pfs may seek forward and seek back several times during reading directory, which may cause the performance worse.
Poor performance, yes, probably a bit worse than I would have expected.
So it is about choosing your poison (potential crash vs. poor performance).
I mean, I am not keen into suggesting any kind of bike shredding for you on this issue, but if this is merged, then people expect it to behave reliably and not allowing a guest to crash QEMU host process by simply creating a large number of directory entries on guest.
I was also considering to make it a QEMU option, but OTOH, this is a temporary situation and those options would be wiped once we have an oppropriate solution a bit later.
I am open for suggestions. Could we probably just mark Windows host support as experimental for now, is that even allowed by QEMU policies?
Quoted 11 lines
Quoted 8 lines
Quoted 1 line
So I think store all name entries would be better than store all file ID.
As already discussed, NTFS allows up to (2^32 - 1) = 4,294,967,295 entries per directory. So caching only one directory (entirely) in RAM can already exceed the available RAM, which would crash QEMU. Multiplied by an expected amount of directory lookups by client and we even get into much higher categories, even with much smaller individual directory sizes.
Windows file id structure is 24 bytes, which is not a small structure. If you think the performance is acceptable, I can rework this commit based on file id.
RE: [PATCH v4 04/16] hw/9pfs: Implement Windows specific xxxdir() APIs
Quoted 115 lines
-----Original Message-----
From: Christian Schoenebeck <[email protected]>
Sent: Tuesday, February 7, 2023 18:12
To: Greg Kurz <[email protected]>; [email protected]
Cc: Meng, Bin <[email protected]>; Marc-André Lureau <[email protected]>; Daniel P. Berrangé <[email protected]>; Shi, Guohuai <[email protected]>
Subject: Re: [PATCH v4 04/16] hw/9pfs: Implement Windows specificxxxdir()APIsCAUTION: This email comes from a non Wind River email account! Do not click links or open attachments unless you recognize the sender and know the content is safe.
On Monday, February 6, 2023 6:37:16 AM CET Shi, Guohuai wrote:
[...]Quoted 11 lines
Quoted 11 lines
I know, it's an n-square performance issue and what I already wrote in the summary of the linked original suggestion [1] in v3 before, quote:
+ Relatively straight-forward to implement.
+ No (major) changes in 9pfs code base required.
- Still n-square performance issue (neglectable to land Windows
host supportIMO).Quoted 5 lines
Quoted 2 lines
o Consistency assured for "most" cases, except one: if hardlinks are inserted in between then it might fail
readdir()on Linux host may also return the deleted entries. And POSIX specification does not mention about the consistency issue.POSIX explicitly specifies that 1. new and 2. deleted entries may or may not appear in result and leaves that implementation specific. That was never our concern.
And yes, POSIX does not explicitly discuss consistency concerning entries that have neither been added or removed, but this expectation is implied. In practice double entries are probably less of an issue, client might be able to handle that without misbehaviour (haven't checked this at all yet), but if the implementation would lead to chances that entries may *never* appear to clients at all, even after refreshing periodically, I mean how could you work with a file system like that?
Quoted 1 line
NTFS file id is the $MFT index id. It will keen unique until file is
deleted.
Quoted 3 lines
But the index id may be reuse if delete and re-create many files.
Saving file id instead of name will make consistency better, but may not
cover all status.
Quoted 1 line
Because read directory is not a "atomic" operation.
I don't see an issue with that, because these are entries that were either added or removed, we don't care about them. And their file IDs would not affect fetching the other directory entries that have not been touched in between.
And we are also not questioning atomicity here, but consistency.
Quoted 6 lines
Quoted 6 lines
[1] https://lore.kernel.org/qemu-devel/2468168.SvRIHAoRfs@silver/
The idea was to use that just as a starting point to land Windows host support ASAP, slower on large dirs compared to other solutions, yes, but with guaranteed correct and deterministic behaviour. And then on the long run we would of course replace that with a more
performant solution.
Quoted 22 lines
Quoted 5 lines
I mean, this is really simple to implement, so I would at least test it. If it really runs horribly slow we could still discuss faster solutions, which are however all much more tricky.
I did a basic test on Windows host, here is the code:
st = clock(); pDir = opendir_win32(TEST_DIR); if (pDir == NULL) return -1; while ((pEnt = readdir_win32(pDir)) != NULL) { totals++; } closedir_win32(pDir); ed = clock(); printf("total = %d clocks = %d %d\n", totals, ed - st,Quoted 8 lines
CLOCKS_PER_SEC);My local storage is SSD disk.
Run this test for 100, 1000, 10000 entries.
For file name cache solution, the time cost is: 2, 9, 44 (in ms). For file id cache solution, the time cost: 3, 438, 4338 (in ms). I already usedOpenFileById()to make it faster instead ofCreateFile(). IfI use CreateFile, it need more than 80 seconds.
Quoted 4 lines
The performance looks like not good.
And actually, it would be worse in 9pfs.
Because in current design, 9pfs may seek forward and seek back severaltimes during reading directory, which may cause the performance worse.
Poor performance, yes, probably a bit worse than I would have expected.
So it is about choosing your poison (potential crash vs. poor performance).
I mean, I am not keen into suggesting any kind of bike shredding for you on this issue, but if this is merged, then people expect it to behave reliably and not allowing a guest to crash QEMU host process by simply creating a large number of directory entries on guest.
Quoted 6 lines
I was also considering to make it a QEMU option, but OTOH, this is a temporary situation and those options would be wiped once we have an oppropriate solution a bit later.
I am open for suggestions. Could we probably just mark Windows host support as experimental for now, is that even allowed by QEMU policies?
Yes, it is hard to choose:
a) 1 file id entry is 24 bytes, to reduce memory fragment, I used an array to store the file ids.
b) 1 file name entry is ~300 bytes, by using link list.
If there are 1-million files in one directory, a) need 24 MB continues memory buffer, b) need 300 MB memory (no need continues). If there are 10-million files in one directory, a) need 240 MB continues memory buffer, b) need 3 GB memory (no need continues).
Both #a and #b are need more and more memory buffer. If there no more free memory, opendir() will be failed. However, is it a normal status that a directory contains more than 1-million files?
I will prepare an new version solution just for this commit with storing file id. The new patch would be ready tomorrow.
Thanks
Guohuai
Quoted 16 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
So I think store all name entries would be better than store all file
ID.
Quoted 6 lines
Quoted 6 lines
As already discussed, NTFS allows up to (2^32 - 1) = 4,294,967,295 entries per directory. So caching only one directory (entirely) in RAM can already exceed the available RAM, which would crash QEMU. Multiplied by an expected amount of directory lookups by client and we even get into much higher categories, even with much smaller
individual directory sizes.
Quoted 4 lines
Quoted 1 line
Windows file id structure is 24 bytes, which is not a small structure. If you think the performance is acceptable, I can rework this commit based
on file id.