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 #include "qemu/osdep.h"
19 #include "fsdev/qemu-fsdev.h" /* local_ops */
20 #include <arpa/inet.h>
23 #include <sys/socket.h>
25 #include "qemu/xattr.h"
26 #include "qemu/cutils.h"
27 #include "qemu/error-report.h"
30 #ifdef CONFIG_LINUX_MAGIC_H
31 #include <linux/magic.h>
33 #include <sys/ioctl.h>
35 #ifndef XFS_SUPER_MAGIC
36 #define XFS_SUPER_MAGIC 0x58465342
38 #ifndef EXT2_SUPER_MAGIC
39 #define EXT2_SUPER_MAGIC 0xEF53
41 #ifndef REISERFS_SUPER_MAGIC
42 #define REISERFS_SUPER_MAGIC 0x52654973
44 #ifndef BTRFS_SUPER_MAGIC
45 #define BTRFS_SUPER_MAGIC 0x9123683E
52 int local_open_nofollow(FsContext
*fs_ctx
, const char *path
, int flags
,
55 LocalData
*data
= fs_ctx
->private;
57 /* All paths are relative to the path data->mountfd points to */
58 while (*path
== '/') {
62 return relative_openat_nofollow(data
->mountfd
, path
, flags
, mode
);
65 int local_opendir_nofollow(FsContext
*fs_ctx
, const char *path
)
67 return local_open_nofollow(fs_ctx
, path
, O_DIRECTORY
| O_RDONLY
, 0);
70 static void renameat_preserve_errno(int odirfd
, const char *opath
, int ndirfd
,
74 renameat(odirfd
, opath
, ndirfd
, npath
);
78 static void unlinkat_preserve_errno(int dirfd
, const char *path
, int flags
)
81 unlinkat(dirfd
, path
, flags
);
85 #define VIRTFS_META_DIR ".virtfs_metadata"
87 static FILE *local_fopenat(int dirfd
, const char *name
, const char *mode
)
93 * only supports two modes
97 } else if (mode
[0] == 'w') {
98 flags
= O_WRONLY
| O_TRUNC
| O_CREAT
;
99 o_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
;
103 fd
= openat_file(dirfd
, name
, flags
, o_mode
);
107 fp
= fdopen(fd
, mode
);
115 static void local_mapped_file_attr(int dirfd
, const char *name
,
122 map_dirfd
= openat_dir(dirfd
, VIRTFS_META_DIR
);
123 if (map_dirfd
== -1) {
127 fp
= local_fopenat(map_dirfd
, name
, "r");
128 close_preserve_errno(map_dirfd
);
132 memset(buf
, 0, ATTR_MAX
);
133 while (fgets(buf
, ATTR_MAX
, fp
)) {
134 if (!strncmp(buf
, "virtfs.uid", 10)) {
135 stbuf
->st_uid
= atoi(buf
+11);
136 } else if (!strncmp(buf
, "virtfs.gid", 10)) {
137 stbuf
->st_gid
= atoi(buf
+11);
138 } else if (!strncmp(buf
, "virtfs.mode", 11)) {
139 stbuf
->st_mode
= atoi(buf
+12);
140 } else if (!strncmp(buf
, "virtfs.rdev", 11)) {
141 stbuf
->st_rdev
= atoi(buf
+12);
143 memset(buf
, 0, ATTR_MAX
);
148 static int local_lstat(FsContext
*fs_ctx
, V9fsPath
*fs_path
, struct stat
*stbuf
)
151 char *dirpath
= g_path_get_dirname(fs_path
->data
);
152 char *name
= g_path_get_basename(fs_path
->data
);
155 dirfd
= local_opendir_nofollow(fs_ctx
, dirpath
);
160 err
= fstatat(dirfd
, name
, stbuf
, AT_SYMLINK_NOFOLLOW
);
164 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
165 /* Actual credentials are part of extended attrs */
171 if (fgetxattrat_nofollow(dirfd
, name
, "user.virtfs.uid", &tmp_uid
,
172 sizeof(uid_t
)) > 0) {
173 stbuf
->st_uid
= le32_to_cpu(tmp_uid
);
175 if (fgetxattrat_nofollow(dirfd
, name
, "user.virtfs.gid", &tmp_gid
,
176 sizeof(gid_t
)) > 0) {
177 stbuf
->st_gid
= le32_to_cpu(tmp_gid
);
179 if (fgetxattrat_nofollow(dirfd
, name
, "user.virtfs.mode", &tmp_mode
,
180 sizeof(mode_t
)) > 0) {
181 stbuf
->st_mode
= le32_to_cpu(tmp_mode
);
183 if (fgetxattrat_nofollow(dirfd
, name
, "user.virtfs.rdev", &tmp_dev
,
184 sizeof(dev_t
)) > 0) {
185 stbuf
->st_rdev
= le64_to_cpu(tmp_dev
);
187 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
188 local_mapped_file_attr(dirfd
, name
, stbuf
);
192 close_preserve_errno(dirfd
);
199 static int local_set_mapped_file_attrat(int dirfd
, const char *name
,
205 int uid
= -1, gid
= -1, mode
= -1, rdev
= -1;
208 ret
= mkdirat(dirfd
, VIRTFS_META_DIR
, 0700);
209 if (ret
< 0 && errno
!= EEXIST
) {
213 map_dirfd
= openat_dir(dirfd
, VIRTFS_META_DIR
);
214 if (map_dirfd
== -1) {
218 fp
= local_fopenat(map_dirfd
, name
, "r");
220 if (errno
== ENOENT
) {
221 goto update_map_file
;
223 close_preserve_errno(map_dirfd
);
227 memset(buf
, 0, ATTR_MAX
);
228 while (fgets(buf
, ATTR_MAX
, fp
)) {
229 if (!strncmp(buf
, "virtfs.uid", 10)) {
230 uid
= atoi(buf
+ 11);
231 } else if (!strncmp(buf
, "virtfs.gid", 10)) {
232 gid
= atoi(buf
+ 11);
233 } else if (!strncmp(buf
, "virtfs.mode", 11)) {
234 mode
= atoi(buf
+ 12);
235 } else if (!strncmp(buf
, "virtfs.rdev", 11)) {
236 rdev
= atoi(buf
+ 12);
238 memset(buf
, 0, ATTR_MAX
);
243 fp
= local_fopenat(map_dirfd
, name
, "w");
244 close_preserve_errno(map_dirfd
);
249 if (credp
->fc_uid
!= -1) {
252 if (credp
->fc_gid
!= -1) {
255 if (credp
->fc_mode
!= -1) {
256 mode
= credp
->fc_mode
;
258 if (credp
->fc_rdev
!= -1) {
259 rdev
= credp
->fc_rdev
;
263 fprintf(fp
, "virtfs.uid=%d\n", uid
);
266 fprintf(fp
, "virtfs.gid=%d\n", gid
);
269 fprintf(fp
, "virtfs.mode=%d\n", mode
);
272 fprintf(fp
, "virtfs.rdev=%d\n", rdev
);
279 static int fchmodat_nofollow(int dirfd
, const char *name
, mode_t mode
)
283 /* FIXME: this should be handled with fchmodat(AT_SYMLINK_NOFOLLOW).
284 * Unfortunately, the linux kernel doesn't implement it yet. As an
285 * alternative, let's open the file and use fchmod() instead. This
286 * may fail depending on the permissions of the file, but it is the
287 * best we can do to avoid TOCTTOU. We first try to open read-only
288 * in case name points to a directory. If that fails, we try write-only
289 * in case name doesn't point to a directory.
291 fd
= openat_file(dirfd
, name
, O_RDONLY
, 0);
293 /* In case the file is writable-only and isn't a directory. */
294 if (errno
== EACCES
) {
295 fd
= openat_file(dirfd
, name
, O_WRONLY
, 0);
297 if (fd
== -1 && errno
== EISDIR
) {
304 ret
= fchmod(fd
, mode
);
305 close_preserve_errno(fd
);
309 static int local_set_xattrat(int dirfd
, const char *path
, FsCred
*credp
)
313 if (credp
->fc_uid
!= -1) {
314 uint32_t tmp_uid
= cpu_to_le32(credp
->fc_uid
);
315 err
= fsetxattrat_nofollow(dirfd
, path
, "user.virtfs.uid", &tmp_uid
,
321 if (credp
->fc_gid
!= -1) {
322 uint32_t tmp_gid
= cpu_to_le32(credp
->fc_gid
);
323 err
= fsetxattrat_nofollow(dirfd
, path
, "user.virtfs.gid", &tmp_gid
,
329 if (credp
->fc_mode
!= -1) {
330 uint32_t tmp_mode
= cpu_to_le32(credp
->fc_mode
);
331 err
= fsetxattrat_nofollow(dirfd
, path
, "user.virtfs.mode", &tmp_mode
,
337 if (credp
->fc_rdev
!= -1) {
338 uint64_t tmp_rdev
= cpu_to_le64(credp
->fc_rdev
);
339 err
= fsetxattrat_nofollow(dirfd
, path
, "user.virtfs.rdev", &tmp_rdev
,
348 static int local_set_cred_passthrough(FsContext
*fs_ctx
, int dirfd
,
349 const char *name
, FsCred
*credp
)
351 if (fchownat(dirfd
, name
, credp
->fc_uid
, credp
->fc_gid
,
352 AT_SYMLINK_NOFOLLOW
) < 0) {
354 * If we fail to change ownership and if we are
355 * using security model none. Ignore the error
357 if ((fs_ctx
->export_flags
& V9FS_SEC_MASK
) != V9FS_SM_NONE
) {
362 return fchmodat_nofollow(dirfd
, name
, credp
->fc_mode
& 07777);
365 static ssize_t
local_readlink(FsContext
*fs_ctx
, V9fsPath
*fs_path
,
366 char *buf
, size_t bufsz
)
370 if ((fs_ctx
->export_flags
& V9FS_SM_MAPPED
) ||
371 (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
)) {
374 fd
= local_open_nofollow(fs_ctx
, fs_path
->data
, O_RDONLY
, 0);
379 tsize
= read(fd
, (void *)buf
, bufsz
);
380 } while (tsize
== -1 && errno
== EINTR
);
381 close_preserve_errno(fd
);
382 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
383 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
384 char *dirpath
= g_path_get_dirname(fs_path
->data
);
385 char *name
= g_path_get_basename(fs_path
->data
);
388 dirfd
= local_opendir_nofollow(fs_ctx
, dirpath
);
393 tsize
= readlinkat(dirfd
, name
, buf
, bufsz
);
394 close_preserve_errno(dirfd
);
402 static int local_close(FsContext
*ctx
, V9fsFidOpenState
*fs
)
404 return close(fs
->fd
);
407 static int local_closedir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
409 return closedir(fs
->dir
.stream
);
412 static int local_open(FsContext
*ctx
, V9fsPath
*fs_path
,
413 int flags
, V9fsFidOpenState
*fs
)
417 fd
= local_open_nofollow(ctx
, fs_path
->data
, flags
, 0);
425 static int local_opendir(FsContext
*ctx
,
426 V9fsPath
*fs_path
, V9fsFidOpenState
*fs
)
431 dirfd
= local_opendir_nofollow(ctx
, fs_path
->data
);
436 stream
= fdopendir(dirfd
);
441 fs
->dir
.stream
= stream
;
445 static void local_rewinddir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
447 rewinddir(fs
->dir
.stream
);
450 static off_t
local_telldir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
452 return telldir(fs
->dir
.stream
);
455 static struct dirent
*local_readdir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
457 struct dirent
*entry
;
460 entry
= readdir(fs
->dir
.stream
);
465 if (ctx
->export_flags
& V9FS_SM_MAPPED
) {
466 entry
->d_type
= DT_UNKNOWN
;
467 } else if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
468 if (!strcmp(entry
->d_name
, VIRTFS_META_DIR
)) {
469 /* skp the meta data directory */
472 entry
->d_type
= DT_UNKNOWN
;
478 static void local_seekdir(FsContext
*ctx
, V9fsFidOpenState
*fs
, off_t off
)
480 seekdir(fs
->dir
.stream
, off
);
483 static ssize_t
local_preadv(FsContext
*ctx
, V9fsFidOpenState
*fs
,
484 const struct iovec
*iov
,
485 int iovcnt
, off_t offset
)
488 return preadv(fs
->fd
, iov
, iovcnt
, offset
);
490 int err
= lseek(fs
->fd
, offset
, SEEK_SET
);
494 return readv(fs
->fd
, iov
, iovcnt
);
499 static ssize_t
local_pwritev(FsContext
*ctx
, V9fsFidOpenState
*fs
,
500 const struct iovec
*iov
,
501 int iovcnt
, off_t offset
)
505 ret
= pwritev(fs
->fd
, iov
, iovcnt
, offset
);
507 int err
= lseek(fs
->fd
, offset
, SEEK_SET
);
511 ret
= writev(fs
->fd
, iov
, iovcnt
);
514 #ifdef CONFIG_SYNC_FILE_RANGE
515 if (ret
> 0 && ctx
->export_flags
& V9FS_IMMEDIATE_WRITEOUT
) {
517 * Initiate a writeback. This is not a data integrity sync.
518 * We want to ensure that we don't leave dirty pages in the cache
519 * after write when writeout=immediate is sepcified.
521 sync_file_range(fs
->fd
, offset
, ret
,
522 SYNC_FILE_RANGE_WAIT_BEFORE
| SYNC_FILE_RANGE_WRITE
);
528 static int local_chmod(FsContext
*fs_ctx
, V9fsPath
*fs_path
, FsCred
*credp
)
530 char *dirpath
= g_path_get_dirname(fs_path
->data
);
531 char *name
= g_path_get_basename(fs_path
->data
);
535 dirfd
= local_opendir_nofollow(fs_ctx
, dirpath
);
540 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
541 ret
= local_set_xattrat(dirfd
, name
, credp
);
542 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
543 ret
= local_set_mapped_file_attrat(dirfd
, name
, credp
);
544 } else if (fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
||
545 fs_ctx
->export_flags
& V9FS_SM_NONE
) {
546 ret
= fchmodat_nofollow(dirfd
, name
, credp
->fc_mode
);
548 close_preserve_errno(dirfd
);
556 static int local_mknod(FsContext
*fs_ctx
, V9fsPath
*dir_path
,
557 const char *name
, FsCred
*credp
)
562 dirfd
= local_opendir_nofollow(fs_ctx
, dir_path
->data
);
567 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
||
568 fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
569 err
= mknodat(dirfd
, name
, SM_LOCAL_MODE_BITS
| S_IFREG
, 0);
574 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
575 err
= local_set_xattrat(dirfd
, name
, credp
);
577 err
= local_set_mapped_file_attrat(dirfd
, name
, credp
);
582 } else if (fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
||
583 fs_ctx
->export_flags
& V9FS_SM_NONE
) {
584 err
= mknodat(dirfd
, name
, credp
->fc_mode
, credp
->fc_rdev
);
588 err
= local_set_cred_passthrough(fs_ctx
, dirfd
, name
, credp
);
596 unlinkat_preserve_errno(dirfd
, name
, 0);
598 close_preserve_errno(dirfd
);
602 static int local_mkdir(FsContext
*fs_ctx
, V9fsPath
*dir_path
,
603 const char *name
, FsCred
*credp
)
608 dirfd
= local_opendir_nofollow(fs_ctx
, dir_path
->data
);
613 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
||
614 fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
615 err
= mkdirat(dirfd
, name
, SM_LOCAL_DIR_MODE_BITS
);
619 credp
->fc_mode
= credp
->fc_mode
| S_IFDIR
;
621 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
622 err
= local_set_xattrat(dirfd
, name
, credp
);
624 err
= local_set_mapped_file_attrat(dirfd
, name
, credp
);
629 } else if (fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
||
630 fs_ctx
->export_flags
& V9FS_SM_NONE
) {
631 err
= mkdirat(dirfd
, name
, credp
->fc_mode
);
635 err
= local_set_cred_passthrough(fs_ctx
, dirfd
, name
, credp
);
643 unlinkat_preserve_errno(dirfd
, name
, AT_REMOVEDIR
);
645 close_preserve_errno(dirfd
);
649 static int local_fstat(FsContext
*fs_ctx
, int fid_type
,
650 V9fsFidOpenState
*fs
, struct stat
*stbuf
)
654 if (fid_type
== P9_FID_DIR
) {
655 fd
= dirfd(fs
->dir
.stream
);
660 err
= fstat(fd
, stbuf
);
664 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
665 /* Actual credentials are part of extended attrs */
671 if (fgetxattr(fd
, "user.virtfs.uid", &tmp_uid
, sizeof(uid_t
)) > 0) {
672 stbuf
->st_uid
= le32_to_cpu(tmp_uid
);
674 if (fgetxattr(fd
, "user.virtfs.gid", &tmp_gid
, sizeof(gid_t
)) > 0) {
675 stbuf
->st_gid
= le32_to_cpu(tmp_gid
);
677 if (fgetxattr(fd
, "user.virtfs.mode", &tmp_mode
, sizeof(mode_t
)) > 0) {
678 stbuf
->st_mode
= le32_to_cpu(tmp_mode
);
680 if (fgetxattr(fd
, "user.virtfs.rdev", &tmp_dev
, sizeof(dev_t
)) > 0) {
681 stbuf
->st_rdev
= le64_to_cpu(tmp_dev
);
683 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
690 static int local_open2(FsContext
*fs_ctx
, V9fsPath
*dir_path
, const char *name
,
691 int flags
, FsCred
*credp
, V9fsFidOpenState
*fs
)
698 * Mark all the open to not follow symlinks
702 dirfd
= local_opendir_nofollow(fs_ctx
, dir_path
->data
);
707 /* Determine the security model */
708 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
||
709 fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
710 fd
= openat_file(dirfd
, name
, flags
, SM_LOCAL_MODE_BITS
);
714 credp
->fc_mode
= credp
->fc_mode
|S_IFREG
;
715 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
716 /* Set cleint credentials in xattr */
717 err
= local_set_xattrat(dirfd
, name
, credp
);
719 err
= local_set_mapped_file_attrat(dirfd
, name
, credp
);
724 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
725 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
726 fd
= openat_file(dirfd
, name
, flags
, credp
->fc_mode
);
730 err
= local_set_cred_passthrough(fs_ctx
, dirfd
, name
, credp
);
740 unlinkat_preserve_errno(dirfd
, name
,
741 flags
& O_DIRECTORY
? AT_REMOVEDIR
: 0);
742 close_preserve_errno(fd
);
744 close_preserve_errno(dirfd
);
749 static int local_symlink(FsContext
*fs_ctx
, const char *oldpath
,
750 V9fsPath
*dir_path
, const char *name
, FsCred
*credp
)
755 dirfd
= local_opendir_nofollow(fs_ctx
, dir_path
->data
);
760 /* Determine the security model */
761 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
||
762 fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
764 ssize_t oldpath_size
, write_size
;
766 fd
= openat_file(dirfd
, name
, O_CREAT
| O_EXCL
| O_RDWR
,
771 /* Write the oldpath (target) to the file. */
772 oldpath_size
= strlen(oldpath
);
774 write_size
= write(fd
, (void *)oldpath
, oldpath_size
);
775 } while (write_size
== -1 && errno
== EINTR
);
776 close_preserve_errno(fd
);
778 if (write_size
!= oldpath_size
) {
781 /* Set cleint credentials in symlink's xattr */
782 credp
->fc_mode
= credp
->fc_mode
| S_IFLNK
;
784 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
785 err
= local_set_xattrat(dirfd
, name
, credp
);
787 err
= local_set_mapped_file_attrat(dirfd
, name
, credp
);
792 } else if (fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
||
793 fs_ctx
->export_flags
& V9FS_SM_NONE
) {
794 err
= symlinkat(oldpath
, dirfd
, name
);
798 err
= fchownat(dirfd
, name
, credp
->fc_uid
, credp
->fc_gid
,
799 AT_SYMLINK_NOFOLLOW
);
802 * If we fail to change ownership and if we are
803 * using security model none. Ignore the error
805 if ((fs_ctx
->export_flags
& V9FS_SEC_MASK
) != V9FS_SM_NONE
) {
815 unlinkat_preserve_errno(dirfd
, name
, 0);
817 close_preserve_errno(dirfd
);
821 static int local_link(FsContext
*ctx
, V9fsPath
*oldpath
,
822 V9fsPath
*dirpath
, const char *name
)
824 char *odirpath
= g_path_get_dirname(oldpath
->data
);
825 char *oname
= g_path_get_basename(oldpath
->data
);
829 odirfd
= local_opendir_nofollow(ctx
, odirpath
);
834 ndirfd
= local_opendir_nofollow(ctx
, dirpath
->data
);
836 close_preserve_errno(odirfd
);
840 ret
= linkat(odirfd
, oname
, ndirfd
, name
, 0);
845 /* now link the virtfs_metadata files */
846 if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
847 int omap_dirfd
, nmap_dirfd
;
849 ret
= mkdirat(ndirfd
, VIRTFS_META_DIR
, 0700);
850 if (ret
< 0 && errno
!= EEXIST
) {
854 omap_dirfd
= openat_dir(odirfd
, VIRTFS_META_DIR
);
855 if (omap_dirfd
== -1) {
859 nmap_dirfd
= openat_dir(ndirfd
, VIRTFS_META_DIR
);
860 if (nmap_dirfd
== -1) {
861 close_preserve_errno(omap_dirfd
);
865 ret
= linkat(omap_dirfd
, oname
, nmap_dirfd
, name
, 0);
866 close_preserve_errno(nmap_dirfd
);
867 close_preserve_errno(omap_dirfd
);
868 if (ret
< 0 && errno
!= ENOENT
) {
879 unlinkat_preserve_errno(ndirfd
, name
, 0);
881 close_preserve_errno(ndirfd
);
882 close_preserve_errno(odirfd
);
889 static int local_truncate(FsContext
*ctx
, V9fsPath
*fs_path
, off_t size
)
893 fd
= local_open_nofollow(ctx
, fs_path
->data
, O_WRONLY
, 0);
897 ret
= ftruncate(fd
, size
);
898 close_preserve_errno(fd
);
902 static int local_chown(FsContext
*fs_ctx
, V9fsPath
*fs_path
, FsCred
*credp
)
904 char *dirpath
= g_path_get_dirname(fs_path
->data
);
905 char *name
= g_path_get_basename(fs_path
->data
);
909 dirfd
= local_opendir_nofollow(fs_ctx
, dirpath
);
914 if ((credp
->fc_uid
== -1 && credp
->fc_gid
== -1) ||
915 (fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
916 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
917 ret
= fchownat(dirfd
, name
, credp
->fc_uid
, credp
->fc_gid
,
918 AT_SYMLINK_NOFOLLOW
);
919 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
920 ret
= local_set_xattrat(dirfd
, name
, credp
);
921 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
922 ret
= local_set_mapped_file_attrat(dirfd
, name
, credp
);
925 close_preserve_errno(dirfd
);
932 static int local_utimensat(FsContext
*s
, V9fsPath
*fs_path
,
933 const struct timespec
*buf
)
935 char *dirpath
= g_path_get_dirname(fs_path
->data
);
936 char *name
= g_path_get_basename(fs_path
->data
);
939 dirfd
= local_opendir_nofollow(s
, dirpath
);
944 ret
= utimensat(dirfd
, name
, buf
, AT_SYMLINK_NOFOLLOW
);
945 close_preserve_errno(dirfd
);
952 static int local_unlinkat_common(FsContext
*ctx
, int dirfd
, const char *name
,
957 if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
960 if (flags
== AT_REMOVEDIR
) {
963 fd
= openat_dir(dirfd
, name
);
968 * If directory remove .virtfs_metadata contained in the
971 ret
= unlinkat(fd
, VIRTFS_META_DIR
, AT_REMOVEDIR
);
972 close_preserve_errno(fd
);
973 if (ret
< 0 && errno
!= ENOENT
) {
975 * We didn't had the .virtfs_metadata file. May be file created
976 * in non-mapped mode ?. Ignore ENOENT.
982 * Now remove the name from parent directory
983 * .virtfs_metadata directory.
985 map_dirfd
= openat_dir(dirfd
, VIRTFS_META_DIR
);
986 ret
= unlinkat(map_dirfd
, name
, 0);
987 close_preserve_errno(map_dirfd
);
988 if (ret
< 0 && errno
!= ENOENT
) {
990 * We didn't had the .virtfs_metadata file. May be file created
991 * in non-mapped mode ?. Ignore ENOENT.
997 ret
= unlinkat(dirfd
, name
, flags
);
1002 static int local_remove(FsContext
*ctx
, const char *path
)
1005 char *dirpath
= g_path_get_dirname(path
);
1006 char *name
= g_path_get_basename(path
);
1011 dirfd
= local_opendir_nofollow(ctx
, dirpath
);
1016 if (fstatat(dirfd
, path
, &stbuf
, AT_SYMLINK_NOFOLLOW
) < 0) {
1020 if (S_ISDIR(stbuf
.st_mode
)) {
1021 flags
|= AT_REMOVEDIR
;
1024 err
= local_unlinkat_common(ctx
, dirfd
, name
, flags
);
1026 close_preserve_errno(dirfd
);
1033 static int local_fsync(FsContext
*ctx
, int fid_type
,
1034 V9fsFidOpenState
*fs
, int datasync
)
1038 if (fid_type
== P9_FID_DIR
) {
1039 fd
= dirfd(fs
->dir
.stream
);
1045 return qemu_fdatasync(fd
);
1051 static int local_statfs(FsContext
*s
, V9fsPath
*fs_path
, struct statfs
*stbuf
)
1055 fd
= local_open_nofollow(s
, fs_path
->data
, O_RDONLY
, 0);
1059 ret
= fstatfs(fd
, stbuf
);
1060 close_preserve_errno(fd
);
1064 static ssize_t
local_lgetxattr(FsContext
*ctx
, V9fsPath
*fs_path
,
1065 const char *name
, void *value
, size_t size
)
1067 char *path
= fs_path
->data
;
1069 return v9fs_get_xattr(ctx
, path
, name
, value
, size
);
1072 static ssize_t
local_llistxattr(FsContext
*ctx
, V9fsPath
*fs_path
,
1073 void *value
, size_t size
)
1075 char *path
= fs_path
->data
;
1077 return v9fs_list_xattr(ctx
, path
, value
, size
);
1080 static int local_lsetxattr(FsContext
*ctx
, V9fsPath
*fs_path
, const char *name
,
1081 void *value
, size_t size
, int flags
)
1083 char *path
= fs_path
->data
;
1085 return v9fs_set_xattr(ctx
, path
, name
, value
, size
, flags
);
1088 static int local_lremovexattr(FsContext
*ctx
, V9fsPath
*fs_path
,
1091 char *path
= fs_path
->data
;
1093 return v9fs_remove_xattr(ctx
, path
, name
);
1096 static int local_name_to_path(FsContext
*ctx
, V9fsPath
*dir_path
,
1097 const char *name
, V9fsPath
*target
)
1100 v9fs_path_sprintf(target
, "%s/%s", dir_path
->data
, name
);
1102 v9fs_path_sprintf(target
, "%s", name
);
1107 static int local_renameat(FsContext
*ctx
, V9fsPath
*olddir
,
1108 const char *old_name
, V9fsPath
*newdir
,
1109 const char *new_name
)
1114 odirfd
= local_opendir_nofollow(ctx
, olddir
->data
);
1119 ndirfd
= local_opendir_nofollow(ctx
, newdir
->data
);
1121 close_preserve_errno(odirfd
);
1125 ret
= renameat(odirfd
, old_name
, ndirfd
, new_name
);
1130 if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
1131 int omap_dirfd
, nmap_dirfd
;
1133 ret
= mkdirat(ndirfd
, VIRTFS_META_DIR
, 0700);
1134 if (ret
< 0 && errno
!= EEXIST
) {
1135 goto err_undo_rename
;
1138 omap_dirfd
= openat_dir(odirfd
, VIRTFS_META_DIR
);
1139 if (omap_dirfd
== -1) {
1143 nmap_dirfd
= openat_dir(ndirfd
, VIRTFS_META_DIR
);
1144 if (nmap_dirfd
== -1) {
1145 close_preserve_errno(omap_dirfd
);
1149 /* rename the .virtfs_metadata files */
1150 ret
= renameat(omap_dirfd
, old_name
, nmap_dirfd
, new_name
);
1151 close_preserve_errno(nmap_dirfd
);
1152 close_preserve_errno(omap_dirfd
);
1153 if (ret
< 0 && errno
!= ENOENT
) {
1154 goto err_undo_rename
;
1164 renameat_preserve_errno(ndirfd
, new_name
, odirfd
, old_name
);
1166 close_preserve_errno(ndirfd
);
1167 close_preserve_errno(odirfd
);
1171 static void v9fs_path_init_dirname(V9fsPath
*path
, const char *str
)
1173 path
->data
= g_path_get_dirname(str
);
1174 path
->size
= strlen(path
->data
) + 1;
1177 static int local_rename(FsContext
*ctx
, const char *oldpath
,
1178 const char *newpath
)
1181 char *oname
= g_path_get_basename(oldpath
);
1182 char *nname
= g_path_get_basename(newpath
);
1183 V9fsPath olddir
, newdir
;
1185 v9fs_path_init_dirname(&olddir
, oldpath
);
1186 v9fs_path_init_dirname(&newdir
, newpath
);
1188 err
= local_renameat(ctx
, &olddir
, oname
, &newdir
, nname
);
1190 v9fs_path_free(&newdir
);
1191 v9fs_path_free(&olddir
);
1198 static int local_unlinkat(FsContext
*ctx
, V9fsPath
*dir
,
1199 const char *name
, int flags
)
1204 dirfd
= local_opendir_nofollow(ctx
, dir
->data
);
1209 ret
= local_unlinkat_common(ctx
, dirfd
, name
, flags
);
1210 close_preserve_errno(dirfd
);
1214 static int local_ioc_getversion(FsContext
*ctx
, V9fsPath
*path
,
1215 mode_t st_mode
, uint64_t *st_gen
)
1217 #ifdef FS_IOC_GETVERSION
1219 V9fsFidOpenState fid_open
;
1222 * Do not try to open special files like device nodes, fifos etc
1223 * We can get fd for regular files and directories only
1225 if (!S_ISREG(st_mode
) && !S_ISDIR(st_mode
)) {
1229 err
= local_open(ctx
, path
, O_RDONLY
, &fid_open
);
1233 err
= ioctl(fid_open
.fd
, FS_IOC_GETVERSION
, st_gen
);
1234 local_close(ctx
, &fid_open
);
1242 static int local_init(FsContext
*ctx
)
1244 struct statfs stbuf
;
1245 LocalData
*data
= g_malloc(sizeof(*data
));
1247 data
->mountfd
= open(ctx
->fs_root
, O_DIRECTORY
| O_RDONLY
);
1248 if (data
->mountfd
== -1) {
1252 #ifdef FS_IOC_GETVERSION
1254 * use ioc_getversion only if the ioctl is definied
1256 if (fstatfs(data
->mountfd
, &stbuf
) < 0) {
1257 close_preserve_errno(data
->mountfd
);
1260 switch (stbuf
.f_type
) {
1261 case EXT2_SUPER_MAGIC
:
1262 case BTRFS_SUPER_MAGIC
:
1263 case REISERFS_SUPER_MAGIC
:
1264 case XFS_SUPER_MAGIC
:
1265 ctx
->exops
.get_st_gen
= local_ioc_getversion
;
1270 if (ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) {
1271 ctx
->xops
= passthrough_xattr_ops
;
1272 } else if (ctx
->export_flags
& V9FS_SM_MAPPED
) {
1273 ctx
->xops
= mapped_xattr_ops
;
1274 } else if (ctx
->export_flags
& V9FS_SM_NONE
) {
1275 ctx
->xops
= none_xattr_ops
;
1276 } else if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
1278 * xattr operation for mapped-file and passthrough
1281 ctx
->xops
= passthrough_xattr_ops
;
1283 ctx
->export_flags
|= V9FS_PATHNAME_FSCONTEXT
;
1285 ctx
->private = data
;
1293 static void local_cleanup(FsContext
*ctx
)
1295 LocalData
*data
= ctx
->private;
1297 close(data
->mountfd
);
1301 static int local_parse_opts(QemuOpts
*opts
, struct FsDriverEntry
*fse
)
1303 const char *sec_model
= qemu_opt_get(opts
, "security_model");
1304 const char *path
= qemu_opt_get(opts
, "path");
1308 error_report("Security model not specified, local fs needs security model");
1309 error_printf("valid options are:"
1310 "\tsecurity_model=[passthrough|mapped-xattr|mapped-file|none]\n");
1314 if (!strcmp(sec_model
, "passthrough")) {
1315 fse
->export_flags
|= V9FS_SM_PASSTHROUGH
;
1316 } else if (!strcmp(sec_model
, "mapped") ||
1317 !strcmp(sec_model
, "mapped-xattr")) {
1318 fse
->export_flags
|= V9FS_SM_MAPPED
;
1319 } else if (!strcmp(sec_model
, "none")) {
1320 fse
->export_flags
|= V9FS_SM_NONE
;
1321 } else if (!strcmp(sec_model
, "mapped-file")) {
1322 fse
->export_flags
|= V9FS_SM_MAPPED_FILE
;
1324 error_report("Invalid security model %s specified", sec_model
);
1325 error_printf("valid options are:"
1326 "\t[passthrough|mapped-xattr|mapped-file|none]\n");
1331 error_report("fsdev: No path specified");
1335 fsdev_throttle_parse_opts(opts
, &fse
->fst
, &err
);
1337 error_reportf_err(err
, "Throttle configuration is not valid: ");
1341 fse
->path
= g_strdup(path
);
1346 FileOperations local_ops
= {
1347 .parse_opts
= local_parse_opts
,
1349 .cleanup
= local_cleanup
,
1350 .lstat
= local_lstat
,
1351 .readlink
= local_readlink
,
1352 .close
= local_close
,
1353 .closedir
= local_closedir
,
1355 .opendir
= local_opendir
,
1356 .rewinddir
= local_rewinddir
,
1357 .telldir
= local_telldir
,
1358 .readdir
= local_readdir
,
1359 .seekdir
= local_seekdir
,
1360 .preadv
= local_preadv
,
1361 .pwritev
= local_pwritev
,
1362 .chmod
= local_chmod
,
1363 .mknod
= local_mknod
,
1364 .mkdir
= local_mkdir
,
1365 .fstat
= local_fstat
,
1366 .open2
= local_open2
,
1367 .symlink
= local_symlink
,
1369 .truncate
= local_truncate
,
1370 .rename
= local_rename
,
1371 .chown
= local_chown
,
1372 .utimensat
= local_utimensat
,
1373 .remove
= local_remove
,
1374 .fsync
= local_fsync
,
1375 .statfs
= local_statfs
,
1376 .lgetxattr
= local_lgetxattr
,
1377 .llistxattr
= local_llistxattr
,
1378 .lsetxattr
= local_lsetxattr
,
1379 .lremovexattr
= local_lremovexattr
,
1380 .name_to_path
= local_name_to_path
,
1381 .renameat
= local_renameat
,
1382 .unlinkat
= local_unlinkat
,