4 * Copyright IBM, Corp. 2010
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 * Not so fast! You might want to read the 9p developer docs first:
15 * https://wiki.qemu.org/Documentation/9p
18 #include "qemu/osdep.h"
23 #include "fsdev/qemu-fsdev.h" /* local_ops */
24 #include <arpa/inet.h>
27 #include <sys/socket.h>
29 #include "qemu/xattr.h"
30 #include "qapi/error.h"
31 #include "qemu/cutils.h"
32 #include "qemu/error-report.h"
33 #include "qemu/option.h"
37 #ifdef CONFIG_LINUX_MAGIC_H
38 #include <linux/magic.h>
41 #include <sys/ioctl.h>
43 #ifndef XFS_SUPER_MAGIC
44 #define XFS_SUPER_MAGIC 0x58465342
46 #ifndef EXT2_SUPER_MAGIC
47 #define EXT2_SUPER_MAGIC 0xEF53
49 #ifndef REISERFS_SUPER_MAGIC
50 #define REISERFS_SUPER_MAGIC 0x52654973
52 #ifndef BTRFS_SUPER_MAGIC
53 #define BTRFS_SUPER_MAGIC 0x9123683E
60 int local_open_nofollow(FsContext
*fs_ctx
, const char *path
, int flags
,
63 LocalData
*data
= fs_ctx
->private;
64 int fd
= data
->mountfd
;
66 while (*path
&& fd
!= -1) {
71 /* Only relative paths without consecutive slashes */
74 head
= g_strdup(path
);
75 c
= qemu_strchrnul(path
, '/');
77 /* Intermediate path element */
80 next_fd
= openat_dir(fd
, head
);
82 /* Rightmost path element */
83 next_fd
= openat_file(fd
, head
, flags
, mode
);
87 if (fd
!= data
->mountfd
) {
88 close_preserve_errno(fd
);
93 assert(fd
!= data
->mountfd
);
97 int local_opendir_nofollow(FsContext
*fs_ctx
, const char *path
)
99 return local_open_nofollow(fs_ctx
, path
, O_DIRECTORY
| O_RDONLY
, 0);
102 static void renameat_preserve_errno(int odirfd
, const char *opath
, int ndirfd
,
106 qemu_renameat(odirfd
, opath
, ndirfd
, npath
);
110 static void unlinkat_preserve_errno(int dirfd
, const char *path
, int flags
)
113 qemu_unlinkat(dirfd
, path
, flags
);
117 #define VIRTFS_META_DIR ".virtfs_metadata"
118 #define VIRTFS_META_ROOT_FILE VIRTFS_META_DIR "_root"
120 static FILE *local_fopenat(int dirfd
, const char *name
, const char *mode
)
126 * only supports two modes
128 if (mode
[0] == 'r') {
130 } else if (mode
[0] == 'w') {
131 flags
= O_WRONLY
| O_TRUNC
| O_CREAT
;
132 o_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
;
136 fd
= openat_file(dirfd
, name
, flags
, o_mode
);
140 fp
= fdopen(fd
, mode
);
148 static void local_mapped_file_attr(int dirfd
, const char *name
,
155 if (strcmp(name
, ".")) {
156 map_dirfd
= openat_dir(dirfd
, VIRTFS_META_DIR
);
157 if (map_dirfd
== -1) {
161 fp
= local_fopenat(map_dirfd
, name
, "r");
162 close_preserve_errno(map_dirfd
);
164 fp
= local_fopenat(dirfd
, VIRTFS_META_ROOT_FILE
, "r");
169 memset(buf
, 0, ATTR_MAX
);
170 while (fgets(buf
, ATTR_MAX
, fp
)) {
171 if (!strncmp(buf
, "virtfs.uid", 10)) {
172 stbuf
->st_uid
= atoi(buf
+ 11);
173 } else if (!strncmp(buf
, "virtfs.gid", 10)) {
174 stbuf
->st_gid
= atoi(buf
+ 11);
175 } else if (!strncmp(buf
, "virtfs.mode", 11)) {
176 stbuf
->st_mode
= atoi(buf
+ 12);
177 } else if (!strncmp(buf
, "virtfs.rdev", 11)) {
178 stbuf
->st_rdev
= atoi(buf
+ 12);
180 memset(buf
, 0, ATTR_MAX
);
185 static int local_lstat(FsContext
*fs_ctx
, V9fsPath
*fs_path
, struct stat
*stbuf
)
188 char *dirpath
= g_path_get_dirname(fs_path
->data
);
189 char *name
= g_path_get_basename(fs_path
->data
);
192 dirfd
= local_opendir_nofollow(fs_ctx
, dirpath
);
197 err
= qemu_fstatat(dirfd
, name
, stbuf
, AT_SYMLINK_NOFOLLOW
);
201 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
202 /* Actual credentials are part of extended attrs */
208 if (fgetxattrat_nofollow(dirfd
, name
, "user.virtfs.uid", &tmp_uid
,
209 sizeof(uid_t
)) > 0) {
210 stbuf
->st_uid
= le32_to_cpu(tmp_uid
);
212 if (fgetxattrat_nofollow(dirfd
, name
, "user.virtfs.gid", &tmp_gid
,
213 sizeof(gid_t
)) > 0) {
214 stbuf
->st_gid
= le32_to_cpu(tmp_gid
);
216 if (fgetxattrat_nofollow(dirfd
, name
, "user.virtfs.mode", &tmp_mode
,
217 sizeof(mode_t
)) > 0) {
218 stbuf
->st_mode
= le32_to_cpu(tmp_mode
);
220 if (fgetxattrat_nofollow(dirfd
, name
, "user.virtfs.rdev", &tmp_dev
,
221 sizeof(dev_t
)) > 0) {
222 stbuf
->st_rdev
= le64_to_cpu(tmp_dev
);
224 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
225 local_mapped_file_attr(dirfd
, name
, stbuf
);
229 close_preserve_errno(dirfd
);
236 static int local_set_mapped_file_attrat(int dirfd
, const char *name
,
242 int uid
= -1, gid
= -1, mode
= -1, rdev
= -1;
243 int map_dirfd
= -1, map_fd
;
244 bool is_root
= !strcmp(name
, ".");
247 fp
= local_fopenat(dirfd
, VIRTFS_META_ROOT_FILE
, "r");
249 if (errno
== ENOENT
) {
250 goto update_map_file
;
256 ret
= qemu_mkdirat(dirfd
, VIRTFS_META_DIR
, 0700);
257 if (ret
< 0 && errno
!= EEXIST
) {
261 map_dirfd
= openat_dir(dirfd
, VIRTFS_META_DIR
);
262 if (map_dirfd
== -1) {
266 fp
= local_fopenat(map_dirfd
, name
, "r");
268 if (errno
== ENOENT
) {
269 goto update_map_file
;
271 close_preserve_errno(map_dirfd
);
276 memset(buf
, 0, ATTR_MAX
);
277 while (fgets(buf
, ATTR_MAX
, fp
)) {
278 if (!strncmp(buf
, "virtfs.uid", 10)) {
279 uid
= atoi(buf
+ 11);
280 } else if (!strncmp(buf
, "virtfs.gid", 10)) {
281 gid
= atoi(buf
+ 11);
282 } else if (!strncmp(buf
, "virtfs.mode", 11)) {
283 mode
= atoi(buf
+ 12);
284 } else if (!strncmp(buf
, "virtfs.rdev", 11)) {
285 rdev
= atoi(buf
+ 12);
287 memset(buf
, 0, ATTR_MAX
);
293 fp
= local_fopenat(dirfd
, VIRTFS_META_ROOT_FILE
, "w");
295 fp
= local_fopenat(map_dirfd
, name
, "w");
296 /* We can't go this far with map_dirfd not being a valid file descriptor
297 * but some versions of gcc aren't smart enough to see it.
299 if (map_dirfd
!= -1) {
300 close_preserve_errno(map_dirfd
);
308 assert(map_fd
!= -1);
309 ret
= fchmod(map_fd
, 0600);
312 if (credp
->fc_uid
!= -1) {
315 if (credp
->fc_gid
!= -1) {
318 if (credp
->fc_mode
!= (mode_t
)-1) {
319 mode
= credp
->fc_mode
;
321 if (credp
->fc_rdev
!= -1) {
322 rdev
= credp
->fc_rdev
;
326 fprintf(fp
, "virtfs.uid=%d\n", uid
);
329 fprintf(fp
, "virtfs.gid=%d\n", gid
);
332 fprintf(fp
, "virtfs.mode=%d\n", mode
);
335 fprintf(fp
, "virtfs.rdev=%d\n", rdev
);
342 static int fchmodat_nofollow(int dirfd
, const char *name
, mode_t mode
)
347 /* FIXME: this should be handled with fchmodat(AT_SYMLINK_NOFOLLOW).
348 * Unfortunately, the linux kernel doesn't implement it yet.
351 /* First, we clear non-racing symlinks out of the way. */
352 if (qemu_fstatat(dirfd
, name
, &stbuf
, AT_SYMLINK_NOFOLLOW
)) {
355 if (S_ISLNK(stbuf
.st_mode
)) {
360 fd
= openat_file(dirfd
, name
, O_RDONLY
| O_PATH_9P_UTIL
| O_NOFOLLOW
, 0);
361 #if O_PATH_9P_UTIL == 0
362 /* Fallback for systems that don't support O_PATH: we depend on the file
363 * being readable or writable.
366 /* In case the file is writable-only and isn't a directory. */
367 if (errno
== EACCES
) {
368 fd
= openat_file(dirfd
, name
, O_WRONLY
, 0);
370 if (fd
== -1 && errno
== EISDIR
) {
377 ret
= fchmod(fd
, mode
);
379 /* Access modes are ignored when O_PATH is supported. If name is a symbolic
380 * link, O_PATH | O_NOFOLLOW causes openat(2) to return a file descriptor
381 * referring to the symbolic link.
387 /* Now we handle racing symlinks. */
388 ret
= fstat(fd
, &stbuf
);
390 if (S_ISLNK(stbuf
.st_mode
)) {
394 char *proc_path
= g_strdup_printf("/proc/self/fd/%d", fd
);
395 ret
= chmod(proc_path
, mode
);
400 close_preserve_errno(fd
);
404 static int local_set_xattrat(int dirfd
, const char *path
, FsCred
*credp
)
408 if (credp
->fc_uid
!= -1) {
409 uint32_t tmp_uid
= cpu_to_le32(credp
->fc_uid
);
410 err
= fsetxattrat_nofollow(dirfd
, path
, "user.virtfs.uid", &tmp_uid
,
416 if (credp
->fc_gid
!= -1) {
417 uint32_t tmp_gid
= cpu_to_le32(credp
->fc_gid
);
418 err
= fsetxattrat_nofollow(dirfd
, path
, "user.virtfs.gid", &tmp_gid
,
424 if (credp
->fc_mode
!= (mode_t
)-1) {
425 uint32_t tmp_mode
= cpu_to_le32(credp
->fc_mode
);
426 err
= fsetxattrat_nofollow(dirfd
, path
, "user.virtfs.mode", &tmp_mode
,
432 if (credp
->fc_rdev
!= -1) {
433 uint64_t tmp_rdev
= cpu_to_le64(credp
->fc_rdev
);
434 err
= fsetxattrat_nofollow(dirfd
, path
, "user.virtfs.rdev", &tmp_rdev
,
443 static int local_set_cred_passthrough(FsContext
*fs_ctx
, int dirfd
,
444 const char *name
, FsCred
*credp
)
446 if (fchownat(dirfd
, name
, credp
->fc_uid
, credp
->fc_gid
,
447 AT_SYMLINK_NOFOLLOW
) < 0) {
449 * If we fail to change ownership and if we are
450 * using security model none. Ignore the error
452 if ((fs_ctx
->export_flags
& V9FS_SEC_MASK
) != V9FS_SM_NONE
) {
457 return fchmodat_nofollow(dirfd
, name
, credp
->fc_mode
& 07777);
460 static ssize_t
local_readlink(FsContext
*fs_ctx
, V9fsPath
*fs_path
,
461 char *buf
, size_t bufsz
)
465 if ((fs_ctx
->export_flags
& V9FS_SM_MAPPED
) ||
466 (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
)) {
469 fd
= local_open_nofollow(fs_ctx
, fs_path
->data
, O_RDONLY
, 0);
473 tsize
= RETRY_ON_EINTR(read(fd
, (void *)buf
, bufsz
));
474 close_preserve_errno(fd
);
475 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
476 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
477 char *dirpath
= g_path_get_dirname(fs_path
->data
);
478 char *name
= g_path_get_basename(fs_path
->data
);
481 dirfd
= local_opendir_nofollow(fs_ctx
, dirpath
);
486 tsize
= readlinkat(dirfd
, name
, buf
, bufsz
);
487 close_preserve_errno(dirfd
);
495 static int local_close(FsContext
*ctx
, V9fsFidOpenState
*fs
)
497 return close(fs
->fd
);
500 static int local_closedir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
502 return closedir(fs
->dir
.stream
);
505 static int local_open(FsContext
*ctx
, V9fsPath
*fs_path
,
506 int flags
, V9fsFidOpenState
*fs
)
510 fd
= local_open_nofollow(ctx
, fs_path
->data
, flags
, 0);
518 static int local_opendir(FsContext
*ctx
,
519 V9fsPath
*fs_path
, V9fsFidOpenState
*fs
)
524 dirfd
= local_opendir_nofollow(ctx
, fs_path
->data
);
529 stream
= fdopendir(dirfd
);
534 fs
->dir
.stream
= stream
;
538 static void local_rewinddir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
540 rewinddir(fs
->dir
.stream
);
543 static off_t
local_telldir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
545 return telldir(fs
->dir
.stream
);
548 static bool local_is_mapped_file_metadata(FsContext
*fs_ctx
, const char *name
)
551 !strcmp(name
, VIRTFS_META_DIR
) || !strcmp(name
, VIRTFS_META_ROOT_FILE
);
554 static struct dirent
*local_readdir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
556 struct dirent
*entry
;
559 entry
= readdir(fs
->dir
.stream
);
565 off
= telldir(fs
->dir
.stream
);
566 /* If telldir fails, fail the entire readdir call */
570 entry
->d_seekoff
= off
;
573 if (ctx
->export_flags
& V9FS_SM_MAPPED
) {
574 entry
->d_type
= DT_UNKNOWN
;
575 } else if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
576 if (local_is_mapped_file_metadata(ctx
, entry
->d_name
)) {
577 /* skip the meta data */
580 entry
->d_type
= DT_UNKNOWN
;
586 static void local_seekdir(FsContext
*ctx
, V9fsFidOpenState
*fs
, off_t off
)
588 seekdir(fs
->dir
.stream
, off
);
591 static ssize_t
local_preadv(FsContext
*ctx
, V9fsFidOpenState
*fs
,
592 const struct iovec
*iov
,
593 int iovcnt
, off_t offset
)
596 return preadv(fs
->fd
, iov
, iovcnt
, offset
);
598 int err
= lseek(fs
->fd
, offset
, SEEK_SET
);
602 return readv(fs
->fd
, iov
, iovcnt
);
607 static ssize_t
local_pwritev(FsContext
*ctx
, V9fsFidOpenState
*fs
,
608 const struct iovec
*iov
,
609 int iovcnt
, off_t offset
)
613 ret
= pwritev(fs
->fd
, iov
, iovcnt
, offset
);
615 int err
= lseek(fs
->fd
, offset
, SEEK_SET
);
619 ret
= writev(fs
->fd
, iov
, iovcnt
);
622 #ifdef CONFIG_SYNC_FILE_RANGE
623 if (ret
> 0 && ctx
->export_flags
& V9FS_IMMEDIATE_WRITEOUT
) {
625 * Initiate a writeback. This is not a data integrity sync.
626 * We want to ensure that we don't leave dirty pages in the cache
627 * after write when writeout=immediate is specified.
629 sync_file_range(fs
->fd
, offset
, ret
,
630 SYNC_FILE_RANGE_WAIT_BEFORE
| SYNC_FILE_RANGE_WRITE
);
636 static int local_chmod(FsContext
*fs_ctx
, V9fsPath
*fs_path
, FsCred
*credp
)
638 char *dirpath
= g_path_get_dirname(fs_path
->data
);
639 char *name
= g_path_get_basename(fs_path
->data
);
643 dirfd
= local_opendir_nofollow(fs_ctx
, dirpath
);
648 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
649 ret
= local_set_xattrat(dirfd
, name
, credp
);
650 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
651 ret
= local_set_mapped_file_attrat(dirfd
, name
, credp
);
652 } else if (fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
||
653 fs_ctx
->export_flags
& V9FS_SM_NONE
) {
654 ret
= fchmodat_nofollow(dirfd
, name
, credp
->fc_mode
);
656 close_preserve_errno(dirfd
);
664 static int local_mknod(FsContext
*fs_ctx
, V9fsPath
*dir_path
,
665 const char *name
, FsCred
*credp
)
670 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
&&
671 local_is_mapped_file_metadata(fs_ctx
, name
)) {
676 dirfd
= local_opendir_nofollow(fs_ctx
, dir_path
->data
);
681 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
||
682 fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
683 err
= qemu_mknodat(dirfd
, name
, fs_ctx
->fmode
| S_IFREG
, 0);
688 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
689 err
= local_set_xattrat(dirfd
, name
, credp
);
691 err
= local_set_mapped_file_attrat(dirfd
, name
, credp
);
696 } else if (fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
||
697 fs_ctx
->export_flags
& V9FS_SM_NONE
) {
698 err
= qemu_mknodat(dirfd
, name
, credp
->fc_mode
, credp
->fc_rdev
);
702 err
= local_set_cred_passthrough(fs_ctx
, dirfd
, name
, credp
);
710 unlinkat_preserve_errno(dirfd
, name
, 0);
712 close_preserve_errno(dirfd
);
716 static int local_mkdir(FsContext
*fs_ctx
, V9fsPath
*dir_path
,
717 const char *name
, FsCred
*credp
)
722 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
&&
723 local_is_mapped_file_metadata(fs_ctx
, name
)) {
728 dirfd
= local_opendir_nofollow(fs_ctx
, dir_path
->data
);
733 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
||
734 fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
735 err
= qemu_mkdirat(dirfd
, name
, fs_ctx
->dmode
);
739 credp
->fc_mode
= credp
->fc_mode
| S_IFDIR
;
741 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
742 err
= local_set_xattrat(dirfd
, name
, credp
);
744 err
= local_set_mapped_file_attrat(dirfd
, name
, credp
);
749 } else if (fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
||
750 fs_ctx
->export_flags
& V9FS_SM_NONE
) {
751 err
= qemu_mkdirat(dirfd
, name
, credp
->fc_mode
);
755 err
= local_set_cred_passthrough(fs_ctx
, dirfd
, name
, credp
);
763 unlinkat_preserve_errno(dirfd
, name
, AT_REMOVEDIR
);
765 close_preserve_errno(dirfd
);
769 static int local_fstat(FsContext
*fs_ctx
, int fid_type
,
770 V9fsFidOpenState
*fs
, struct stat
*stbuf
)
774 if (fid_type
== P9_FID_DIR
) {
775 fd
= dirfd(fs
->dir
.stream
);
780 err
= fstat(fd
, stbuf
);
784 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
785 /* Actual credentials are part of extended attrs */
791 if (qemu_fgetxattr(fd
, "user.virtfs.uid",
792 &tmp_uid
, sizeof(uid_t
)) > 0) {
793 stbuf
->st_uid
= le32_to_cpu(tmp_uid
);
795 if (qemu_fgetxattr(fd
, "user.virtfs.gid",
796 &tmp_gid
, sizeof(gid_t
)) > 0) {
797 stbuf
->st_gid
= le32_to_cpu(tmp_gid
);
799 if (qemu_fgetxattr(fd
, "user.virtfs.mode",
800 &tmp_mode
, sizeof(mode_t
)) > 0) {
801 stbuf
->st_mode
= le32_to_cpu(tmp_mode
);
803 if (qemu_fgetxattr(fd
, "user.virtfs.rdev",
804 &tmp_dev
, sizeof(dev_t
)) > 0) {
805 stbuf
->st_rdev
= le64_to_cpu(tmp_dev
);
807 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
814 static int local_open2(FsContext
*fs_ctx
, V9fsPath
*dir_path
, const char *name
,
815 int flags
, FsCred
*credp
, V9fsFidOpenState
*fs
)
821 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
&&
822 local_is_mapped_file_metadata(fs_ctx
, name
)) {
828 * Mark all the open to not follow symlinks
832 dirfd
= local_opendir_nofollow(fs_ctx
, dir_path
->data
);
837 /* Determine the security model */
838 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
||
839 fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
840 fd
= openat_file(dirfd
, name
, flags
, fs_ctx
->fmode
);
844 credp
->fc_mode
= credp
->fc_mode
| S_IFREG
;
845 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
846 /* Set client credentials in xattr */
847 err
= local_set_xattrat(dirfd
, name
, credp
);
849 err
= local_set_mapped_file_attrat(dirfd
, name
, credp
);
854 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
855 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
856 fd
= openat_file(dirfd
, name
, flags
, credp
->fc_mode
);
860 err
= local_set_cred_passthrough(fs_ctx
, dirfd
, name
, credp
);
870 unlinkat_preserve_errno(dirfd
, name
,
871 flags
& O_DIRECTORY
? AT_REMOVEDIR
: 0);
872 close_preserve_errno(fd
);
874 close_preserve_errno(dirfd
);
879 static int local_symlink(FsContext
*fs_ctx
, const char *oldpath
,
880 V9fsPath
*dir_path
, const char *name
, FsCred
*credp
)
885 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
&&
886 local_is_mapped_file_metadata(fs_ctx
, name
)) {
891 dirfd
= local_opendir_nofollow(fs_ctx
, dir_path
->data
);
896 /* Determine the security model */
897 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
||
898 fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
900 ssize_t oldpath_size
, write_size
;
902 fd
= openat_file(dirfd
, name
, O_CREAT
| O_EXCL
| O_RDWR
,
907 /* Write the oldpath (target) to the file. */
908 oldpath_size
= strlen(oldpath
);
909 write_size
= RETRY_ON_EINTR(write(fd
, (void *)oldpath
, oldpath_size
));
910 close_preserve_errno(fd
);
912 if (write_size
!= oldpath_size
) {
915 /* Set client credentials in symlink's xattr */
916 credp
->fc_mode
= credp
->fc_mode
| S_IFLNK
;
918 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
919 err
= local_set_xattrat(dirfd
, name
, credp
);
921 err
= local_set_mapped_file_attrat(dirfd
, name
, credp
);
926 } else if (fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
||
927 fs_ctx
->export_flags
& V9FS_SM_NONE
) {
928 err
= symlinkat(oldpath
, dirfd
, name
);
932 err
= fchownat(dirfd
, name
, credp
->fc_uid
, credp
->fc_gid
,
933 AT_SYMLINK_NOFOLLOW
);
936 * If we fail to change ownership and if we are
937 * using security model none. Ignore the error
939 if ((fs_ctx
->export_flags
& V9FS_SEC_MASK
) != V9FS_SM_NONE
) {
949 unlinkat_preserve_errno(dirfd
, name
, 0);
951 close_preserve_errno(dirfd
);
955 static int local_link(FsContext
*ctx
, V9fsPath
*oldpath
,
956 V9fsPath
*dirpath
, const char *name
)
958 char *odirpath
= g_path_get_dirname(oldpath
->data
);
959 char *oname
= g_path_get_basename(oldpath
->data
);
963 if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
&&
964 local_is_mapped_file_metadata(ctx
, name
)) {
969 odirfd
= local_opendir_nofollow(ctx
, odirpath
);
974 ndirfd
= local_opendir_nofollow(ctx
, dirpath
->data
);
976 close_preserve_errno(odirfd
);
980 ret
= linkat(odirfd
, oname
, ndirfd
, name
, 0);
985 /* now link the virtfs_metadata files */
986 if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
987 int omap_dirfd
, nmap_dirfd
;
989 ret
= qemu_mkdirat(ndirfd
, VIRTFS_META_DIR
, 0700);
990 if (ret
< 0 && errno
!= EEXIST
) {
994 omap_dirfd
= openat_dir(odirfd
, VIRTFS_META_DIR
);
995 if (omap_dirfd
== -1) {
999 nmap_dirfd
= openat_dir(ndirfd
, VIRTFS_META_DIR
);
1000 if (nmap_dirfd
== -1) {
1001 close_preserve_errno(omap_dirfd
);
1005 ret
= linkat(omap_dirfd
, oname
, nmap_dirfd
, name
, 0);
1006 close_preserve_errno(nmap_dirfd
);
1007 close_preserve_errno(omap_dirfd
);
1008 if (ret
< 0 && errno
!= ENOENT
) {
1019 unlinkat_preserve_errno(ndirfd
, name
, 0);
1021 close_preserve_errno(ndirfd
);
1022 close_preserve_errno(odirfd
);
1029 static int local_truncate(FsContext
*ctx
, V9fsPath
*fs_path
, off_t size
)
1033 fd
= local_open_nofollow(ctx
, fs_path
->data
, O_WRONLY
, 0);
1037 ret
= ftruncate(fd
, size
);
1038 close_preserve_errno(fd
);
1042 static int local_chown(FsContext
*fs_ctx
, V9fsPath
*fs_path
, FsCred
*credp
)
1044 char *dirpath
= g_path_get_dirname(fs_path
->data
);
1045 char *name
= g_path_get_basename(fs_path
->data
);
1049 dirfd
= local_opendir_nofollow(fs_ctx
, dirpath
);
1054 if ((credp
->fc_uid
== -1 && credp
->fc_gid
== -1) ||
1055 (fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
1056 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
1057 ret
= fchownat(dirfd
, name
, credp
->fc_uid
, credp
->fc_gid
,
1058 AT_SYMLINK_NOFOLLOW
);
1059 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
1060 ret
= local_set_xattrat(dirfd
, name
, credp
);
1061 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
1062 ret
= local_set_mapped_file_attrat(dirfd
, name
, credp
);
1065 close_preserve_errno(dirfd
);
1072 static int local_utimensat(FsContext
*s
, V9fsPath
*fs_path
,
1073 const struct timespec
*buf
)
1075 char *dirpath
= g_path_get_dirname(fs_path
->data
);
1076 char *name
= g_path_get_basename(fs_path
->data
);
1077 int dirfd
, ret
= -1;
1079 dirfd
= local_opendir_nofollow(s
, dirpath
);
1084 ret
= qemu_utimensat(dirfd
, name
, buf
, AT_SYMLINK_NOFOLLOW
);
1085 close_preserve_errno(dirfd
);
1092 static int local_unlinkat_common(FsContext
*ctx
, int dirfd
, const char *name
,
1097 if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
1100 /* We need to remove the metadata as well:
1101 * - the metadata directory if we're removing a directory
1102 * - the metadata file in the parent's metadata directory
1104 * If any of these are missing (ie, ENOENT) then we're probably
1105 * trying to remove something that wasn't created in mapped-file
1106 * mode. We just ignore the error.
1108 if (flags
== AT_REMOVEDIR
) {
1111 fd
= openat_dir(dirfd
, name
);
1115 ret
= qemu_unlinkat(fd
, VIRTFS_META_DIR
, AT_REMOVEDIR
);
1116 close_preserve_errno(fd
);
1117 if (ret
< 0 && errno
!= ENOENT
) {
1121 map_dirfd
= openat_dir(dirfd
, VIRTFS_META_DIR
);
1122 if (map_dirfd
!= -1) {
1123 ret
= qemu_unlinkat(map_dirfd
, name
, 0);
1124 close_preserve_errno(map_dirfd
);
1125 if (ret
< 0 && errno
!= ENOENT
) {
1128 } else if (errno
!= ENOENT
) {
1133 return qemu_unlinkat(dirfd
, name
, flags
);
1136 static int local_remove(FsContext
*ctx
, const char *path
)
1139 char *dirpath
= g_path_get_dirname(path
);
1140 char *name
= g_path_get_basename(path
);
1145 dirfd
= local_opendir_nofollow(ctx
, dirpath
);
1150 if (qemu_fstatat(dirfd
, name
, &stbuf
, AT_SYMLINK_NOFOLLOW
) < 0) {
1154 if (S_ISDIR(stbuf
.st_mode
)) {
1155 flags
|= AT_REMOVEDIR
;
1158 err
= local_unlinkat_common(ctx
, dirfd
, name
, flags
);
1160 close_preserve_errno(dirfd
);
1167 static int local_fsync(FsContext
*ctx
, int fid_type
,
1168 V9fsFidOpenState
*fs
, int datasync
)
1172 if (fid_type
== P9_FID_DIR
) {
1173 fd
= dirfd(fs
->dir
.stream
);
1179 return qemu_fdatasync(fd
);
1185 static int local_statfs(FsContext
*s
, V9fsPath
*fs_path
, struct statfs
*stbuf
)
1189 fd
= local_open_nofollow(s
, fs_path
->data
, O_RDONLY
, 0);
1193 ret
= fstatfs(fd
, stbuf
);
1194 close_preserve_errno(fd
);
1198 static ssize_t
local_lgetxattr(FsContext
*ctx
, V9fsPath
*fs_path
,
1199 const char *name
, void *value
, size_t size
)
1201 char *path
= fs_path
->data
;
1203 return v9fs_get_xattr(ctx
, path
, name
, value
, size
);
1206 static ssize_t
local_llistxattr(FsContext
*ctx
, V9fsPath
*fs_path
,
1207 void *value
, size_t size
)
1209 char *path
= fs_path
->data
;
1211 return v9fs_list_xattr(ctx
, path
, value
, size
);
1214 static int local_lsetxattr(FsContext
*ctx
, V9fsPath
*fs_path
, const char *name
,
1215 void *value
, size_t size
, int flags
)
1217 char *path
= fs_path
->data
;
1219 return v9fs_set_xattr(ctx
, path
, name
, value
, size
, flags
);
1222 static int local_lremovexattr(FsContext
*ctx
, V9fsPath
*fs_path
,
1225 char *path
= fs_path
->data
;
1227 return v9fs_remove_xattr(ctx
, path
, name
);
1230 static int local_name_to_path(FsContext
*ctx
, V9fsPath
*dir_path
,
1231 const char *name
, V9fsPath
*target
)
1233 if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
&&
1234 local_is_mapped_file_metadata(ctx
, name
)) {
1240 if (!strcmp(name
, ".")) {
1241 /* "." relative to "foo/bar" is "foo/bar" */
1242 v9fs_path_copy(target
, dir_path
);
1243 } else if (!strcmp(name
, "..")) {
1244 if (!strcmp(dir_path
->data
, ".")) {
1245 /* ".." relative to the root is "." */
1246 v9fs_path_sprintf(target
, ".");
1248 char *tmp
= g_path_get_dirname(dir_path
->data
);
1249 /* Symbolic links are resolved by the client. We can assume
1250 * that ".." relative to "foo/bar" is equivalent to "foo"
1252 v9fs_path_sprintf(target
, "%s", tmp
);
1256 assert(!strchr(name
, '/'));
1257 v9fs_path_sprintf(target
, "%s/%s", dir_path
->data
, name
);
1259 } else if (!strcmp(name
, "/") || !strcmp(name
, ".") ||
1260 !strcmp(name
, "..")) {
1261 /* This is the root fid */
1262 v9fs_path_sprintf(target
, ".");
1264 assert(!strchr(name
, '/'));
1265 v9fs_path_sprintf(target
, "./%s", name
);
1270 static int local_renameat(FsContext
*ctx
, V9fsPath
*olddir
,
1271 const char *old_name
, V9fsPath
*newdir
,
1272 const char *new_name
)
1277 if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
&&
1278 (local_is_mapped_file_metadata(ctx
, old_name
) ||
1279 local_is_mapped_file_metadata(ctx
, new_name
))) {
1284 odirfd
= local_opendir_nofollow(ctx
, olddir
->data
);
1289 ndirfd
= local_opendir_nofollow(ctx
, newdir
->data
);
1291 close_preserve_errno(odirfd
);
1295 ret
= qemu_renameat(odirfd
, old_name
, ndirfd
, new_name
);
1300 if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
1301 int omap_dirfd
, nmap_dirfd
;
1303 ret
= qemu_mkdirat(ndirfd
, VIRTFS_META_DIR
, 0700);
1304 if (ret
< 0 && errno
!= EEXIST
) {
1305 goto err_undo_rename
;
1308 omap_dirfd
= openat_dir(odirfd
, VIRTFS_META_DIR
);
1309 if (omap_dirfd
== -1) {
1313 nmap_dirfd
= openat_dir(ndirfd
, VIRTFS_META_DIR
);
1314 if (nmap_dirfd
== -1) {
1315 close_preserve_errno(omap_dirfd
);
1319 /* rename the .virtfs_metadata files */
1320 ret
= qemu_renameat(omap_dirfd
, old_name
, nmap_dirfd
, new_name
);
1321 close_preserve_errno(nmap_dirfd
);
1322 close_preserve_errno(omap_dirfd
);
1323 if (ret
< 0 && errno
!= ENOENT
) {
1324 goto err_undo_rename
;
1334 renameat_preserve_errno(ndirfd
, new_name
, odirfd
, old_name
);
1336 close_preserve_errno(ndirfd
);
1337 close_preserve_errno(odirfd
);
1341 static void v9fs_path_init_dirname(V9fsPath
*path
, const char *str
)
1343 path
->data
= g_path_get_dirname(str
);
1344 path
->size
= strlen(path
->data
) + 1;
1347 static int local_rename(FsContext
*ctx
, const char *oldpath
,
1348 const char *newpath
)
1351 char *oname
= g_path_get_basename(oldpath
);
1352 char *nname
= g_path_get_basename(newpath
);
1353 V9fsPath olddir
, newdir
;
1355 v9fs_path_init_dirname(&olddir
, oldpath
);
1356 v9fs_path_init_dirname(&newdir
, newpath
);
1358 err
= local_renameat(ctx
, &olddir
, oname
, &newdir
, nname
);
1360 v9fs_path_free(&newdir
);
1361 v9fs_path_free(&olddir
);
1368 static int local_unlinkat(FsContext
*ctx
, V9fsPath
*dir
,
1369 const char *name
, int flags
)
1374 if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
&&
1375 local_is_mapped_file_metadata(ctx
, name
)) {
1380 dirfd
= local_opendir_nofollow(ctx
, dir
->data
);
1385 ret
= local_unlinkat_common(ctx
, dirfd
, name
, flags
);
1386 close_preserve_errno(dirfd
);
1390 #ifdef FS_IOC_GETVERSION
1391 static int local_ioc_getversion(FsContext
*ctx
, V9fsPath
*path
,
1392 mode_t st_mode
, uint64_t *st_gen
)
1395 V9fsFidOpenState fid_open
;
1398 * Do not try to open special files like device nodes, fifos etc
1399 * We can get fd for regular files and directories only
1401 if (!S_ISREG(st_mode
) && !S_ISDIR(st_mode
)) {
1405 err
= local_open(ctx
, path
, O_RDONLY
, &fid_open
);
1409 err
= ioctl(fid_open
.fd
, FS_IOC_GETVERSION
, st_gen
);
1410 local_close(ctx
, &fid_open
);
1415 static int local_ioc_getversion_init(FsContext
*ctx
, LocalData
*data
, Error
**errp
)
1417 #ifdef FS_IOC_GETVERSION
1418 struct statfs stbuf
;
1421 * use ioc_getversion only if the ioctl is defined
1423 if (fstatfs(data
->mountfd
, &stbuf
) < 0) {
1424 error_setg_errno(errp
, errno
,
1425 "failed to stat file system at '%s'", ctx
->fs_root
);
1428 switch (stbuf
.f_type
) {
1429 case EXT2_SUPER_MAGIC
:
1430 case BTRFS_SUPER_MAGIC
:
1431 case REISERFS_SUPER_MAGIC
:
1432 case XFS_SUPER_MAGIC
:
1433 ctx
->exops
.get_st_gen
= local_ioc_getversion
;
1440 static int local_init(FsContext
*ctx
, Error
**errp
)
1442 LocalData
*data
= g_malloc(sizeof(*data
));
1444 data
->mountfd
= open(ctx
->fs_root
, O_DIRECTORY
| O_RDONLY
);
1445 if (data
->mountfd
== -1) {
1446 error_setg_errno(errp
, errno
, "failed to open '%s'", ctx
->fs_root
);
1450 if (local_ioc_getversion_init(ctx
, data
, errp
) < 0) {
1451 close(data
->mountfd
);
1455 if (ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) {
1456 ctx
->xops
= passthrough_xattr_ops
;
1457 } else if (ctx
->export_flags
& V9FS_SM_MAPPED
) {
1458 ctx
->xops
= mapped_xattr_ops
;
1459 } else if (ctx
->export_flags
& V9FS_SM_NONE
) {
1460 ctx
->xops
= none_xattr_ops
;
1461 } else if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
1463 * xattr operation for mapped-file and passthrough
1466 ctx
->xops
= passthrough_xattr_ops
;
1468 ctx
->export_flags
|= V9FS_PATHNAME_FSCONTEXT
;
1470 ctx
->private = data
;
1478 static void local_cleanup(FsContext
*ctx
)
1480 LocalData
*data
= ctx
->private;
1486 close(data
->mountfd
);
1490 static void error_append_security_model_hint(Error
*const *errp
)
1492 error_append_hint(errp
, "Valid options are: security_model="
1493 "[passthrough|mapped-xattr|mapped-file|none]\n");
1496 static int local_parse_opts(QemuOpts
*opts
, FsDriverEntry
*fse
, Error
**errp
)
1499 const char *sec_model
= qemu_opt_get(opts
, "security_model");
1500 const char *path
= qemu_opt_get(opts
, "path");
1501 const char *multidevs
= qemu_opt_get(opts
, "multidevs");
1504 error_setg(errp
, "security_model property not set");
1505 error_append_security_model_hint(errp
);
1509 if (!strcmp(sec_model
, "passthrough")) {
1510 fse
->export_flags
|= V9FS_SM_PASSTHROUGH
;
1511 } else if (!strcmp(sec_model
, "mapped") ||
1512 !strcmp(sec_model
, "mapped-xattr")) {
1513 fse
->export_flags
|= V9FS_SM_MAPPED
;
1514 } else if (!strcmp(sec_model
, "none")) {
1515 fse
->export_flags
|= V9FS_SM_NONE
;
1516 } else if (!strcmp(sec_model
, "mapped-file")) {
1517 fse
->export_flags
|= V9FS_SM_MAPPED_FILE
;
1519 error_setg(errp
, "invalid security_model property '%s'", sec_model
);
1520 error_append_security_model_hint(errp
);
1525 if (!strcmp(multidevs
, "remap")) {
1526 fse
->export_flags
&= ~V9FS_FORBID_MULTIDEVS
;
1527 fse
->export_flags
|= V9FS_REMAP_INODES
;
1528 } else if (!strcmp(multidevs
, "forbid")) {
1529 fse
->export_flags
&= ~V9FS_REMAP_INODES
;
1530 fse
->export_flags
|= V9FS_FORBID_MULTIDEVS
;
1531 } else if (!strcmp(multidevs
, "warn")) {
1532 fse
->export_flags
&= ~V9FS_FORBID_MULTIDEVS
;
1533 fse
->export_flags
&= ~V9FS_REMAP_INODES
;
1535 error_setg(errp
, "invalid multidevs property '%s'",
1537 error_append_hint(errp
, "Valid options are: multidevs="
1538 "[remap|forbid|warn]\n");
1544 error_setg(errp
, "path property not set");
1548 if (fsdev_throttle_parse_opts(opts
, &fse
->fst
, errp
)) {
1549 error_prepend(errp
, "invalid throttle configuration: ");
1553 if (fse
->export_flags
& V9FS_SM_MAPPED
||
1554 fse
->export_flags
& V9FS_SM_MAPPED_FILE
) {
1556 qemu_opt_get_number(opts
, "fmode", SM_LOCAL_MODE_BITS
) & 0777;
1558 qemu_opt_get_number(opts
, "dmode", SM_LOCAL_DIR_MODE_BITS
) & 0777;
1560 if (qemu_opt_find(opts
, "fmode")) {
1561 error_setg(errp
, "fmode is only valid for mapped security modes");
1564 if (qemu_opt_find(opts
, "dmode")) {
1565 error_setg(errp
, "dmode is only valid for mapped security modes");
1570 fse
->path
= g_strdup(path
);
1575 FileOperations local_ops
= {
1576 .parse_opts
= local_parse_opts
,
1578 .cleanup
= local_cleanup
,
1579 .lstat
= local_lstat
,
1580 .readlink
= local_readlink
,
1581 .close
= local_close
,
1582 .closedir
= local_closedir
,
1584 .opendir
= local_opendir
,
1585 .rewinddir
= local_rewinddir
,
1586 .telldir
= local_telldir
,
1587 .readdir
= local_readdir
,
1588 .seekdir
= local_seekdir
,
1589 .preadv
= local_preadv
,
1590 .pwritev
= local_pwritev
,
1591 .chmod
= local_chmod
,
1592 .mknod
= local_mknod
,
1593 .mkdir
= local_mkdir
,
1594 .fstat
= local_fstat
,
1595 .open2
= local_open2
,
1596 .symlink
= local_symlink
,
1598 .truncate
= local_truncate
,
1599 .rename
= local_rename
,
1600 .chown
= local_chown
,
1601 .utimensat
= local_utimensat
,
1602 .remove
= local_remove
,
1603 .fsync
= local_fsync
,
1604 .statfs
= local_statfs
,
1605 .lgetxattr
= local_lgetxattr
,
1606 .llistxattr
= local_llistxattr
,
1607 .lsetxattr
= local_lsetxattr
,
1608 .lremovexattr
= local_lremovexattr
,
1609 .name_to_path
= local_name_to_path
,
1610 .renameat
= local_renameat
,
1611 .unlinkat
= local_unlinkat
,