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 renameat(odirfd
, opath
, ndirfd
, npath
);
110 static void unlinkat_preserve_errno(int dirfd
, const char *path
, int flags
)
113 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
= 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
= 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 (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);
474 tsize
= read(fd
, (void *)buf
, bufsz
);
475 } while (tsize
== -1 && errno
== EINTR
);
476 close_preserve_errno(fd
);
477 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
478 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
479 char *dirpath
= g_path_get_dirname(fs_path
->data
);
480 char *name
= g_path_get_basename(fs_path
->data
);
483 dirfd
= local_opendir_nofollow(fs_ctx
, dirpath
);
488 tsize
= readlinkat(dirfd
, name
, buf
, bufsz
);
489 close_preserve_errno(dirfd
);
497 static int local_close(FsContext
*ctx
, V9fsFidOpenState
*fs
)
499 return close(fs
->fd
);
502 static int local_closedir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
504 return closedir(fs
->dir
.stream
);
507 static int local_open(FsContext
*ctx
, V9fsPath
*fs_path
,
508 int flags
, V9fsFidOpenState
*fs
)
512 fd
= local_open_nofollow(ctx
, fs_path
->data
, flags
, 0);
520 static int local_opendir(FsContext
*ctx
,
521 V9fsPath
*fs_path
, V9fsFidOpenState
*fs
)
526 dirfd
= local_opendir_nofollow(ctx
, fs_path
->data
);
531 stream
= fdopendir(dirfd
);
536 fs
->dir
.stream
= stream
;
540 static void local_rewinddir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
542 rewinddir(fs
->dir
.stream
);
545 static off_t
local_telldir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
547 return telldir(fs
->dir
.stream
);
550 static bool local_is_mapped_file_metadata(FsContext
*fs_ctx
, const char *name
)
553 !strcmp(name
, VIRTFS_META_DIR
) || !strcmp(name
, VIRTFS_META_ROOT_FILE
);
556 static struct dirent
*local_readdir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
558 struct dirent
*entry
;
561 entry
= readdir(fs
->dir
.stream
);
567 off
= telldir(fs
->dir
.stream
);
568 /* If telldir fails, fail the entire readdir call */
572 entry
->d_seekoff
= off
;
575 if (ctx
->export_flags
& V9FS_SM_MAPPED
) {
576 entry
->d_type
= DT_UNKNOWN
;
577 } else if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
578 if (local_is_mapped_file_metadata(ctx
, entry
->d_name
)) {
579 /* skip the meta data */
582 entry
->d_type
= DT_UNKNOWN
;
588 static void local_seekdir(FsContext
*ctx
, V9fsFidOpenState
*fs
, off_t off
)
590 seekdir(fs
->dir
.stream
, off
);
593 static ssize_t
local_preadv(FsContext
*ctx
, V9fsFidOpenState
*fs
,
594 const struct iovec
*iov
,
595 int iovcnt
, off_t offset
)
598 return preadv(fs
->fd
, iov
, iovcnt
, offset
);
600 int err
= lseek(fs
->fd
, offset
, SEEK_SET
);
604 return readv(fs
->fd
, iov
, iovcnt
);
609 static ssize_t
local_pwritev(FsContext
*ctx
, V9fsFidOpenState
*fs
,
610 const struct iovec
*iov
,
611 int iovcnt
, off_t offset
)
615 ret
= pwritev(fs
->fd
, iov
, iovcnt
, offset
);
617 int err
= lseek(fs
->fd
, offset
, SEEK_SET
);
621 ret
= writev(fs
->fd
, iov
, iovcnt
);
624 #ifdef CONFIG_SYNC_FILE_RANGE
625 if (ret
> 0 && ctx
->export_flags
& V9FS_IMMEDIATE_WRITEOUT
) {
627 * Initiate a writeback. This is not a data integrity sync.
628 * We want to ensure that we don't leave dirty pages in the cache
629 * after write when writeout=immediate is sepcified.
631 sync_file_range(fs
->fd
, offset
, ret
,
632 SYNC_FILE_RANGE_WAIT_BEFORE
| SYNC_FILE_RANGE_WRITE
);
638 static int local_chmod(FsContext
*fs_ctx
, V9fsPath
*fs_path
, FsCred
*credp
)
640 char *dirpath
= g_path_get_dirname(fs_path
->data
);
641 char *name
= g_path_get_basename(fs_path
->data
);
645 dirfd
= local_opendir_nofollow(fs_ctx
, dirpath
);
650 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
651 ret
= local_set_xattrat(dirfd
, name
, credp
);
652 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
653 ret
= local_set_mapped_file_attrat(dirfd
, name
, credp
);
654 } else if (fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
||
655 fs_ctx
->export_flags
& V9FS_SM_NONE
) {
656 ret
= fchmodat_nofollow(dirfd
, name
, credp
->fc_mode
);
658 close_preserve_errno(dirfd
);
666 static int local_mknod(FsContext
*fs_ctx
, V9fsPath
*dir_path
,
667 const char *name
, FsCred
*credp
)
672 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
&&
673 local_is_mapped_file_metadata(fs_ctx
, name
)) {
678 dirfd
= local_opendir_nofollow(fs_ctx
, dir_path
->data
);
683 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
||
684 fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
685 err
= qemu_mknodat(dirfd
, name
, fs_ctx
->fmode
| S_IFREG
, 0);
690 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
691 err
= local_set_xattrat(dirfd
, name
, credp
);
693 err
= local_set_mapped_file_attrat(dirfd
, name
, credp
);
698 } else if (fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
||
699 fs_ctx
->export_flags
& V9FS_SM_NONE
) {
700 err
= qemu_mknodat(dirfd
, name
, credp
->fc_mode
, credp
->fc_rdev
);
704 err
= local_set_cred_passthrough(fs_ctx
, dirfd
, name
, credp
);
712 unlinkat_preserve_errno(dirfd
, name
, 0);
714 close_preserve_errno(dirfd
);
718 static int local_mkdir(FsContext
*fs_ctx
, V9fsPath
*dir_path
,
719 const char *name
, FsCred
*credp
)
724 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
&&
725 local_is_mapped_file_metadata(fs_ctx
, name
)) {
730 dirfd
= local_opendir_nofollow(fs_ctx
, dir_path
->data
);
735 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
||
736 fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
737 err
= mkdirat(dirfd
, name
, fs_ctx
->dmode
);
741 credp
->fc_mode
= credp
->fc_mode
| S_IFDIR
;
743 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
744 err
= local_set_xattrat(dirfd
, name
, credp
);
746 err
= local_set_mapped_file_attrat(dirfd
, name
, credp
);
751 } else if (fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
||
752 fs_ctx
->export_flags
& V9FS_SM_NONE
) {
753 err
= mkdirat(dirfd
, name
, credp
->fc_mode
);
757 err
= local_set_cred_passthrough(fs_ctx
, dirfd
, name
, credp
);
765 unlinkat_preserve_errno(dirfd
, name
, AT_REMOVEDIR
);
767 close_preserve_errno(dirfd
);
771 static int local_fstat(FsContext
*fs_ctx
, int fid_type
,
772 V9fsFidOpenState
*fs
, struct stat
*stbuf
)
776 if (fid_type
== P9_FID_DIR
) {
777 fd
= dirfd(fs
->dir
.stream
);
782 err
= fstat(fd
, stbuf
);
786 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
787 /* Actual credentials are part of extended attrs */
793 if (qemu_fgetxattr(fd
, "user.virtfs.uid",
794 &tmp_uid
, sizeof(uid_t
)) > 0) {
795 stbuf
->st_uid
= le32_to_cpu(tmp_uid
);
797 if (qemu_fgetxattr(fd
, "user.virtfs.gid",
798 &tmp_gid
, sizeof(gid_t
)) > 0) {
799 stbuf
->st_gid
= le32_to_cpu(tmp_gid
);
801 if (qemu_fgetxattr(fd
, "user.virtfs.mode",
802 &tmp_mode
, sizeof(mode_t
)) > 0) {
803 stbuf
->st_mode
= le32_to_cpu(tmp_mode
);
805 if (qemu_fgetxattr(fd
, "user.virtfs.rdev",
806 &tmp_dev
, sizeof(dev_t
)) > 0) {
807 stbuf
->st_rdev
= le64_to_cpu(tmp_dev
);
809 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
816 static int local_open2(FsContext
*fs_ctx
, V9fsPath
*dir_path
, const char *name
,
817 int flags
, FsCred
*credp
, V9fsFidOpenState
*fs
)
823 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
&&
824 local_is_mapped_file_metadata(fs_ctx
, name
)) {
830 * Mark all the open to not follow symlinks
834 dirfd
= local_opendir_nofollow(fs_ctx
, dir_path
->data
);
839 /* Determine the security model */
840 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
||
841 fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
842 fd
= openat_file(dirfd
, name
, flags
, fs_ctx
->fmode
);
846 credp
->fc_mode
= credp
->fc_mode
| S_IFREG
;
847 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
848 /* Set cleint credentials in xattr */
849 err
= local_set_xattrat(dirfd
, name
, credp
);
851 err
= local_set_mapped_file_attrat(dirfd
, name
, credp
);
856 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
857 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
858 fd
= openat_file(dirfd
, name
, flags
, credp
->fc_mode
);
862 err
= local_set_cred_passthrough(fs_ctx
, dirfd
, name
, credp
);
872 unlinkat_preserve_errno(dirfd
, name
,
873 flags
& O_DIRECTORY
? AT_REMOVEDIR
: 0);
874 close_preserve_errno(fd
);
876 close_preserve_errno(dirfd
);
881 static int local_symlink(FsContext
*fs_ctx
, const char *oldpath
,
882 V9fsPath
*dir_path
, const char *name
, FsCred
*credp
)
887 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
&&
888 local_is_mapped_file_metadata(fs_ctx
, name
)) {
893 dirfd
= local_opendir_nofollow(fs_ctx
, dir_path
->data
);
898 /* Determine the security model */
899 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
||
900 fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
902 ssize_t oldpath_size
, write_size
;
904 fd
= openat_file(dirfd
, name
, O_CREAT
| O_EXCL
| O_RDWR
,
909 /* Write the oldpath (target) to the file. */
910 oldpath_size
= strlen(oldpath
);
912 write_size
= write(fd
, (void *)oldpath
, oldpath_size
);
913 } while (write_size
== -1 && errno
== EINTR
);
914 close_preserve_errno(fd
);
916 if (write_size
!= oldpath_size
) {
919 /* Set cleint credentials in symlink's xattr */
920 credp
->fc_mode
= credp
->fc_mode
| S_IFLNK
;
922 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
923 err
= local_set_xattrat(dirfd
, name
, credp
);
925 err
= local_set_mapped_file_attrat(dirfd
, name
, credp
);
930 } else if (fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
||
931 fs_ctx
->export_flags
& V9FS_SM_NONE
) {
932 err
= symlinkat(oldpath
, dirfd
, name
);
936 err
= fchownat(dirfd
, name
, credp
->fc_uid
, credp
->fc_gid
,
937 AT_SYMLINK_NOFOLLOW
);
940 * If we fail to change ownership and if we are
941 * using security model none. Ignore the error
943 if ((fs_ctx
->export_flags
& V9FS_SEC_MASK
) != V9FS_SM_NONE
) {
953 unlinkat_preserve_errno(dirfd
, name
, 0);
955 close_preserve_errno(dirfd
);
959 static int local_link(FsContext
*ctx
, V9fsPath
*oldpath
,
960 V9fsPath
*dirpath
, const char *name
)
962 char *odirpath
= g_path_get_dirname(oldpath
->data
);
963 char *oname
= g_path_get_basename(oldpath
->data
);
967 if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
&&
968 local_is_mapped_file_metadata(ctx
, name
)) {
973 odirfd
= local_opendir_nofollow(ctx
, odirpath
);
978 ndirfd
= local_opendir_nofollow(ctx
, dirpath
->data
);
980 close_preserve_errno(odirfd
);
984 ret
= linkat(odirfd
, oname
, ndirfd
, name
, 0);
989 /* now link the virtfs_metadata files */
990 if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
991 int omap_dirfd
, nmap_dirfd
;
993 ret
= mkdirat(ndirfd
, VIRTFS_META_DIR
, 0700);
994 if (ret
< 0 && errno
!= EEXIST
) {
998 omap_dirfd
= openat_dir(odirfd
, VIRTFS_META_DIR
);
999 if (omap_dirfd
== -1) {
1003 nmap_dirfd
= openat_dir(ndirfd
, VIRTFS_META_DIR
);
1004 if (nmap_dirfd
== -1) {
1005 close_preserve_errno(omap_dirfd
);
1009 ret
= linkat(omap_dirfd
, oname
, nmap_dirfd
, name
, 0);
1010 close_preserve_errno(nmap_dirfd
);
1011 close_preserve_errno(omap_dirfd
);
1012 if (ret
< 0 && errno
!= ENOENT
) {
1023 unlinkat_preserve_errno(ndirfd
, name
, 0);
1025 close_preserve_errno(ndirfd
);
1026 close_preserve_errno(odirfd
);
1033 static int local_truncate(FsContext
*ctx
, V9fsPath
*fs_path
, off_t size
)
1037 fd
= local_open_nofollow(ctx
, fs_path
->data
, O_WRONLY
, 0);
1041 ret
= ftruncate(fd
, size
);
1042 close_preserve_errno(fd
);
1046 static int local_chown(FsContext
*fs_ctx
, V9fsPath
*fs_path
, FsCred
*credp
)
1048 char *dirpath
= g_path_get_dirname(fs_path
->data
);
1049 char *name
= g_path_get_basename(fs_path
->data
);
1053 dirfd
= local_opendir_nofollow(fs_ctx
, dirpath
);
1058 if ((credp
->fc_uid
== -1 && credp
->fc_gid
== -1) ||
1059 (fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
1060 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
1061 ret
= fchownat(dirfd
, name
, credp
->fc_uid
, credp
->fc_gid
,
1062 AT_SYMLINK_NOFOLLOW
);
1063 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
1064 ret
= local_set_xattrat(dirfd
, name
, credp
);
1065 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
1066 ret
= local_set_mapped_file_attrat(dirfd
, name
, credp
);
1069 close_preserve_errno(dirfd
);
1076 static int local_utimensat(FsContext
*s
, V9fsPath
*fs_path
,
1077 const struct timespec
*buf
)
1079 char *dirpath
= g_path_get_dirname(fs_path
->data
);
1080 char *name
= g_path_get_basename(fs_path
->data
);
1081 int dirfd
, ret
= -1;
1083 dirfd
= local_opendir_nofollow(s
, dirpath
);
1088 ret
= utimensat(dirfd
, name
, buf
, AT_SYMLINK_NOFOLLOW
);
1089 close_preserve_errno(dirfd
);
1096 static int local_unlinkat_common(FsContext
*ctx
, int dirfd
, const char *name
,
1101 if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
1104 /* We need to remove the metadata as well:
1105 * - the metadata directory if we're removing a directory
1106 * - the metadata file in the parent's metadata directory
1108 * If any of these are missing (ie, ENOENT) then we're probably
1109 * trying to remove something that wasn't created in mapped-file
1110 * mode. We just ignore the error.
1112 if (flags
== AT_REMOVEDIR
) {
1115 fd
= openat_dir(dirfd
, name
);
1119 ret
= unlinkat(fd
, VIRTFS_META_DIR
, AT_REMOVEDIR
);
1120 close_preserve_errno(fd
);
1121 if (ret
< 0 && errno
!= ENOENT
) {
1125 map_dirfd
= openat_dir(dirfd
, VIRTFS_META_DIR
);
1126 if (map_dirfd
!= -1) {
1127 ret
= unlinkat(map_dirfd
, name
, 0);
1128 close_preserve_errno(map_dirfd
);
1129 if (ret
< 0 && errno
!= ENOENT
) {
1132 } else if (errno
!= ENOENT
) {
1137 return unlinkat(dirfd
, name
, flags
);
1140 static int local_remove(FsContext
*ctx
, const char *path
)
1143 char *dirpath
= g_path_get_dirname(path
);
1144 char *name
= g_path_get_basename(path
);
1149 dirfd
= local_opendir_nofollow(ctx
, dirpath
);
1154 if (fstatat(dirfd
, name
, &stbuf
, AT_SYMLINK_NOFOLLOW
) < 0) {
1158 if (S_ISDIR(stbuf
.st_mode
)) {
1159 flags
|= AT_REMOVEDIR
;
1162 err
= local_unlinkat_common(ctx
, dirfd
, name
, flags
);
1164 close_preserve_errno(dirfd
);
1171 static int local_fsync(FsContext
*ctx
, int fid_type
,
1172 V9fsFidOpenState
*fs
, int datasync
)
1176 if (fid_type
== P9_FID_DIR
) {
1177 fd
= dirfd(fs
->dir
.stream
);
1183 return qemu_fdatasync(fd
);
1189 static int local_statfs(FsContext
*s
, V9fsPath
*fs_path
, struct statfs
*stbuf
)
1193 fd
= local_open_nofollow(s
, fs_path
->data
, O_RDONLY
, 0);
1197 ret
= fstatfs(fd
, stbuf
);
1198 close_preserve_errno(fd
);
1202 static ssize_t
local_lgetxattr(FsContext
*ctx
, V9fsPath
*fs_path
,
1203 const char *name
, void *value
, size_t size
)
1205 char *path
= fs_path
->data
;
1207 return v9fs_get_xattr(ctx
, path
, name
, value
, size
);
1210 static ssize_t
local_llistxattr(FsContext
*ctx
, V9fsPath
*fs_path
,
1211 void *value
, size_t size
)
1213 char *path
= fs_path
->data
;
1215 return v9fs_list_xattr(ctx
, path
, value
, size
);
1218 static int local_lsetxattr(FsContext
*ctx
, V9fsPath
*fs_path
, const char *name
,
1219 void *value
, size_t size
, int flags
)
1221 char *path
= fs_path
->data
;
1223 return v9fs_set_xattr(ctx
, path
, name
, value
, size
, flags
);
1226 static int local_lremovexattr(FsContext
*ctx
, V9fsPath
*fs_path
,
1229 char *path
= fs_path
->data
;
1231 return v9fs_remove_xattr(ctx
, path
, name
);
1234 static int local_name_to_path(FsContext
*ctx
, V9fsPath
*dir_path
,
1235 const char *name
, V9fsPath
*target
)
1237 if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
&&
1238 local_is_mapped_file_metadata(ctx
, name
)) {
1244 if (!strcmp(name
, ".")) {
1245 /* "." relative to "foo/bar" is "foo/bar" */
1246 v9fs_path_copy(target
, dir_path
);
1247 } else if (!strcmp(name
, "..")) {
1248 if (!strcmp(dir_path
->data
, ".")) {
1249 /* ".." relative to the root is "." */
1250 v9fs_path_sprintf(target
, ".");
1252 char *tmp
= g_path_get_dirname(dir_path
->data
);
1253 /* Symbolic links are resolved by the client. We can assume
1254 * that ".." relative to "foo/bar" is equivalent to "foo"
1256 v9fs_path_sprintf(target
, "%s", tmp
);
1260 assert(!strchr(name
, '/'));
1261 v9fs_path_sprintf(target
, "%s/%s", dir_path
->data
, name
);
1263 } else if (!strcmp(name
, "/") || !strcmp(name
, ".") ||
1264 !strcmp(name
, "..")) {
1265 /* This is the root fid */
1266 v9fs_path_sprintf(target
, ".");
1268 assert(!strchr(name
, '/'));
1269 v9fs_path_sprintf(target
, "./%s", name
);
1274 static int local_renameat(FsContext
*ctx
, V9fsPath
*olddir
,
1275 const char *old_name
, V9fsPath
*newdir
,
1276 const char *new_name
)
1281 if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
&&
1282 (local_is_mapped_file_metadata(ctx
, old_name
) ||
1283 local_is_mapped_file_metadata(ctx
, new_name
))) {
1288 odirfd
= local_opendir_nofollow(ctx
, olddir
->data
);
1293 ndirfd
= local_opendir_nofollow(ctx
, newdir
->data
);
1295 close_preserve_errno(odirfd
);
1299 ret
= renameat(odirfd
, old_name
, ndirfd
, new_name
);
1304 if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
1305 int omap_dirfd
, nmap_dirfd
;
1307 ret
= mkdirat(ndirfd
, VIRTFS_META_DIR
, 0700);
1308 if (ret
< 0 && errno
!= EEXIST
) {
1309 goto err_undo_rename
;
1312 omap_dirfd
= openat_dir(odirfd
, VIRTFS_META_DIR
);
1313 if (omap_dirfd
== -1) {
1317 nmap_dirfd
= openat_dir(ndirfd
, VIRTFS_META_DIR
);
1318 if (nmap_dirfd
== -1) {
1319 close_preserve_errno(omap_dirfd
);
1323 /* rename the .virtfs_metadata files */
1324 ret
= renameat(omap_dirfd
, old_name
, nmap_dirfd
, new_name
);
1325 close_preserve_errno(nmap_dirfd
);
1326 close_preserve_errno(omap_dirfd
);
1327 if (ret
< 0 && errno
!= ENOENT
) {
1328 goto err_undo_rename
;
1338 renameat_preserve_errno(ndirfd
, new_name
, odirfd
, old_name
);
1340 close_preserve_errno(ndirfd
);
1341 close_preserve_errno(odirfd
);
1345 static void v9fs_path_init_dirname(V9fsPath
*path
, const char *str
)
1347 path
->data
= g_path_get_dirname(str
);
1348 path
->size
= strlen(path
->data
) + 1;
1351 static int local_rename(FsContext
*ctx
, const char *oldpath
,
1352 const char *newpath
)
1355 char *oname
= g_path_get_basename(oldpath
);
1356 char *nname
= g_path_get_basename(newpath
);
1357 V9fsPath olddir
, newdir
;
1359 v9fs_path_init_dirname(&olddir
, oldpath
);
1360 v9fs_path_init_dirname(&newdir
, newpath
);
1362 err
= local_renameat(ctx
, &olddir
, oname
, &newdir
, nname
);
1364 v9fs_path_free(&newdir
);
1365 v9fs_path_free(&olddir
);
1372 static int local_unlinkat(FsContext
*ctx
, V9fsPath
*dir
,
1373 const char *name
, int flags
)
1378 if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
&&
1379 local_is_mapped_file_metadata(ctx
, name
)) {
1384 dirfd
= local_opendir_nofollow(ctx
, dir
->data
);
1389 ret
= local_unlinkat_common(ctx
, dirfd
, name
, flags
);
1390 close_preserve_errno(dirfd
);
1394 #ifdef FS_IOC_GETVERSION
1395 static int local_ioc_getversion(FsContext
*ctx
, V9fsPath
*path
,
1396 mode_t st_mode
, uint64_t *st_gen
)
1399 V9fsFidOpenState fid_open
;
1402 * Do not try to open special files like device nodes, fifos etc
1403 * We can get fd for regular files and directories only
1405 if (!S_ISREG(st_mode
) && !S_ISDIR(st_mode
)) {
1409 err
= local_open(ctx
, path
, O_RDONLY
, &fid_open
);
1413 err
= ioctl(fid_open
.fd
, FS_IOC_GETVERSION
, st_gen
);
1414 local_close(ctx
, &fid_open
);
1419 static int local_ioc_getversion_init(FsContext
*ctx
, LocalData
*data
, Error
**errp
)
1421 #ifdef FS_IOC_GETVERSION
1422 struct statfs stbuf
;
1425 * use ioc_getversion only if the ioctl is definied
1427 if (fstatfs(data
->mountfd
, &stbuf
) < 0) {
1428 error_setg_errno(errp
, errno
,
1429 "failed to stat file system at '%s'", ctx
->fs_root
);
1432 switch (stbuf
.f_type
) {
1433 case EXT2_SUPER_MAGIC
:
1434 case BTRFS_SUPER_MAGIC
:
1435 case REISERFS_SUPER_MAGIC
:
1436 case XFS_SUPER_MAGIC
:
1437 ctx
->exops
.get_st_gen
= local_ioc_getversion
;
1444 static int local_init(FsContext
*ctx
, Error
**errp
)
1446 LocalData
*data
= g_malloc(sizeof(*data
));
1448 data
->mountfd
= open(ctx
->fs_root
, O_DIRECTORY
| O_RDONLY
);
1449 if (data
->mountfd
== -1) {
1450 error_setg_errno(errp
, errno
, "failed to open '%s'", ctx
->fs_root
);
1454 if (local_ioc_getversion_init(ctx
, data
, errp
) < 0) {
1455 close(data
->mountfd
);
1459 if (ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) {
1460 ctx
->xops
= passthrough_xattr_ops
;
1461 } else if (ctx
->export_flags
& V9FS_SM_MAPPED
) {
1462 ctx
->xops
= mapped_xattr_ops
;
1463 } else if (ctx
->export_flags
& V9FS_SM_NONE
) {
1464 ctx
->xops
= none_xattr_ops
;
1465 } else if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
1467 * xattr operation for mapped-file and passthrough
1470 ctx
->xops
= passthrough_xattr_ops
;
1472 ctx
->export_flags
|= V9FS_PATHNAME_FSCONTEXT
;
1474 ctx
->private = data
;
1482 static void local_cleanup(FsContext
*ctx
)
1484 LocalData
*data
= ctx
->private;
1490 close(data
->mountfd
);
1494 static void error_append_security_model_hint(Error
*const *errp
)
1496 error_append_hint(errp
, "Valid options are: security_model="
1497 "[passthrough|mapped-xattr|mapped-file|none]\n");
1500 static int local_parse_opts(QemuOpts
*opts
, FsDriverEntry
*fse
, Error
**errp
)
1503 const char *sec_model
= qemu_opt_get(opts
, "security_model");
1504 const char *path
= qemu_opt_get(opts
, "path");
1505 const char *multidevs
= qemu_opt_get(opts
, "multidevs");
1508 error_setg(errp
, "security_model property not set");
1509 error_append_security_model_hint(errp
);
1513 if (!strcmp(sec_model
, "passthrough")) {
1514 fse
->export_flags
|= V9FS_SM_PASSTHROUGH
;
1515 } else if (!strcmp(sec_model
, "mapped") ||
1516 !strcmp(sec_model
, "mapped-xattr")) {
1517 fse
->export_flags
|= V9FS_SM_MAPPED
;
1518 } else if (!strcmp(sec_model
, "none")) {
1519 fse
->export_flags
|= V9FS_SM_NONE
;
1520 } else if (!strcmp(sec_model
, "mapped-file")) {
1521 fse
->export_flags
|= V9FS_SM_MAPPED_FILE
;
1523 error_setg(errp
, "invalid security_model property '%s'", sec_model
);
1524 error_append_security_model_hint(errp
);
1529 if (!strcmp(multidevs
, "remap")) {
1530 fse
->export_flags
&= ~V9FS_FORBID_MULTIDEVS
;
1531 fse
->export_flags
|= V9FS_REMAP_INODES
;
1532 } else if (!strcmp(multidevs
, "forbid")) {
1533 fse
->export_flags
&= ~V9FS_REMAP_INODES
;
1534 fse
->export_flags
|= V9FS_FORBID_MULTIDEVS
;
1535 } else if (!strcmp(multidevs
, "warn")) {
1536 fse
->export_flags
&= ~V9FS_FORBID_MULTIDEVS
;
1537 fse
->export_flags
&= ~V9FS_REMAP_INODES
;
1539 error_setg(errp
, "invalid multidevs property '%s'",
1541 error_append_hint(errp
, "Valid options are: multidevs="
1542 "[remap|forbid|warn]\n");
1548 error_setg(errp
, "path property not set");
1552 if (fsdev_throttle_parse_opts(opts
, &fse
->fst
, errp
)) {
1553 error_prepend(errp
, "invalid throttle configuration: ");
1557 if (fse
->export_flags
& V9FS_SM_MAPPED
||
1558 fse
->export_flags
& V9FS_SM_MAPPED_FILE
) {
1560 qemu_opt_get_number(opts
, "fmode", SM_LOCAL_MODE_BITS
) & 0777;
1562 qemu_opt_get_number(opts
, "dmode", SM_LOCAL_DIR_MODE_BITS
) & 0777;
1564 if (qemu_opt_find(opts
, "fmode")) {
1565 error_setg(errp
, "fmode is only valid for mapped security modes");
1568 if (qemu_opt_find(opts
, "dmode")) {
1569 error_setg(errp
, "dmode is only valid for mapped security modes");
1574 fse
->path
= g_strdup(path
);
1579 FileOperations local_ops
= {
1580 .parse_opts
= local_parse_opts
,
1582 .cleanup
= local_cleanup
,
1583 .lstat
= local_lstat
,
1584 .readlink
= local_readlink
,
1585 .close
= local_close
,
1586 .closedir
= local_closedir
,
1588 .opendir
= local_opendir
,
1589 .rewinddir
= local_rewinddir
,
1590 .telldir
= local_telldir
,
1591 .readdir
= local_readdir
,
1592 .seekdir
= local_seekdir
,
1593 .preadv
= local_preadv
,
1594 .pwritev
= local_pwritev
,
1595 .chmod
= local_chmod
,
1596 .mknod
= local_mknod
,
1597 .mkdir
= local_mkdir
,
1598 .fstat
= local_fstat
,
1599 .open2
= local_open2
,
1600 .symlink
= local_symlink
,
1602 .truncate
= local_truncate
,
1603 .rename
= local_rename
,
1604 .chown
= local_chown
,
1605 .utimensat
= local_utimensat
,
1606 .remove
= local_remove
,
1607 .fsync
= local_fsync
,
1608 .statfs
= local_statfs
,
1609 .lgetxattr
= local_lgetxattr
,
1610 .llistxattr
= local_llistxattr
,
1611 .lsetxattr
= local_lsetxattr
,
1612 .lremovexattr
= local_lremovexattr
,
1613 .name_to_path
= local_name_to_path
,
1614 .renameat
= local_renameat
,
1615 .unlinkat
= local_unlinkat
,