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 char *local_mapped_attr_path(FsContext
*ctx
, const char *path
)
90 const char *name
= strrchr(path
, '/');
98 return g_strdup_printf("%s/%.*s/%s/%s", ctx
->fs_root
,
99 dirlen
, path
, VIRTFS_META_DIR
, name
);
102 static FILE *local_fopen(const char *path
, const char *mode
)
106 int flags
= O_NOFOLLOW
;
108 * only supports two modes
110 if (mode
[0] == 'r') {
112 } else if (mode
[0] == 'w') {
113 flags
|= O_WRONLY
| O_TRUNC
| O_CREAT
;
114 o_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
;
118 fd
= open(path
, flags
, o_mode
);
122 fp
= fdopen(fd
, mode
);
129 static FILE *local_fopenat(int dirfd
, const char *name
, const char *mode
)
135 * only supports two modes
137 if (mode
[0] == 'r') {
139 } else if (mode
[0] == 'w') {
140 flags
= O_WRONLY
| O_TRUNC
| O_CREAT
;
141 o_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
;
145 fd
= openat_file(dirfd
, name
, flags
, o_mode
);
149 fp
= fdopen(fd
, mode
);
157 static void local_mapped_file_attr(int dirfd
, const char *name
,
164 map_dirfd
= openat_dir(dirfd
, VIRTFS_META_DIR
);
165 if (map_dirfd
== -1) {
169 fp
= local_fopenat(map_dirfd
, name
, "r");
170 close_preserve_errno(map_dirfd
);
174 memset(buf
, 0, ATTR_MAX
);
175 while (fgets(buf
, ATTR_MAX
, fp
)) {
176 if (!strncmp(buf
, "virtfs.uid", 10)) {
177 stbuf
->st_uid
= atoi(buf
+11);
178 } else if (!strncmp(buf
, "virtfs.gid", 10)) {
179 stbuf
->st_gid
= atoi(buf
+11);
180 } else if (!strncmp(buf
, "virtfs.mode", 11)) {
181 stbuf
->st_mode
= atoi(buf
+12);
182 } else if (!strncmp(buf
, "virtfs.rdev", 11)) {
183 stbuf
->st_rdev
= atoi(buf
+12);
185 memset(buf
, 0, ATTR_MAX
);
190 static int local_lstat(FsContext
*fs_ctx
, V9fsPath
*fs_path
, struct stat
*stbuf
)
193 char *dirpath
= g_path_get_dirname(fs_path
->data
);
194 char *name
= g_path_get_basename(fs_path
->data
);
197 dirfd
= local_opendir_nofollow(fs_ctx
, dirpath
);
202 err
= fstatat(dirfd
, name
, stbuf
, AT_SYMLINK_NOFOLLOW
);
206 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
207 /* Actual credentials are part of extended attrs */
213 if (fgetxattrat_nofollow(dirfd
, name
, "user.virtfs.uid", &tmp_uid
,
214 sizeof(uid_t
)) > 0) {
215 stbuf
->st_uid
= le32_to_cpu(tmp_uid
);
217 if (fgetxattrat_nofollow(dirfd
, name
, "user.virtfs.gid", &tmp_gid
,
218 sizeof(gid_t
)) > 0) {
219 stbuf
->st_gid
= le32_to_cpu(tmp_gid
);
221 if (fgetxattrat_nofollow(dirfd
, name
, "user.virtfs.mode", &tmp_mode
,
222 sizeof(mode_t
)) > 0) {
223 stbuf
->st_mode
= le32_to_cpu(tmp_mode
);
225 if (fgetxattrat_nofollow(dirfd
, name
, "user.virtfs.rdev", &tmp_dev
,
226 sizeof(dev_t
)) > 0) {
227 stbuf
->st_rdev
= le64_to_cpu(tmp_dev
);
229 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
230 local_mapped_file_attr(dirfd
, name
, stbuf
);
234 close_preserve_errno(dirfd
);
241 static int local_create_mapped_attr_dir(FsContext
*ctx
, const char *path
)
245 char *tmp_path
= g_strdup(path
);
247 attr_dir
= g_strdup_printf("%s/%s/%s",
248 ctx
->fs_root
, dirname(tmp_path
), VIRTFS_META_DIR
);
250 err
= mkdir(attr_dir
, 0700);
251 if (err
< 0 && errno
== EEXIST
) {
259 static int local_set_mapped_file_attr(FsContext
*ctx
,
260 const char *path
, FsCred
*credp
)
266 int uid
= -1, gid
= -1, mode
= -1, rdev
= -1;
268 attr_path
= local_mapped_attr_path(ctx
, path
);
269 fp
= local_fopen(attr_path
, "r");
271 goto create_map_file
;
273 memset(buf
, 0, ATTR_MAX
);
274 while (fgets(buf
, ATTR_MAX
, fp
)) {
275 if (!strncmp(buf
, "virtfs.uid", 10)) {
277 } else if (!strncmp(buf
, "virtfs.gid", 10)) {
279 } else if (!strncmp(buf
, "virtfs.mode", 11)) {
281 } else if (!strncmp(buf
, "virtfs.rdev", 11)) {
284 memset(buf
, 0, ATTR_MAX
);
287 goto update_map_file
;
290 ret
= local_create_mapped_attr_dir(ctx
, path
);
296 fp
= local_fopen(attr_path
, "w");
302 if (credp
->fc_uid
!= -1) {
305 if (credp
->fc_gid
!= -1) {
308 if (credp
->fc_mode
!= -1) {
309 mode
= credp
->fc_mode
;
311 if (credp
->fc_rdev
!= -1) {
312 rdev
= credp
->fc_rdev
;
317 fprintf(fp
, "virtfs.uid=%d\n", uid
);
320 fprintf(fp
, "virtfs.gid=%d\n", gid
);
323 fprintf(fp
, "virtfs.mode=%d\n", mode
);
326 fprintf(fp
, "virtfs.rdev=%d\n", rdev
);
335 static int local_set_xattr(const char *path
, FsCred
*credp
)
339 if (credp
->fc_uid
!= -1) {
340 uint32_t tmp_uid
= cpu_to_le32(credp
->fc_uid
);
341 err
= setxattr(path
, "user.virtfs.uid", &tmp_uid
, sizeof(uid_t
), 0);
346 if (credp
->fc_gid
!= -1) {
347 uint32_t tmp_gid
= cpu_to_le32(credp
->fc_gid
);
348 err
= setxattr(path
, "user.virtfs.gid", &tmp_gid
, sizeof(gid_t
), 0);
353 if (credp
->fc_mode
!= -1) {
354 uint32_t tmp_mode
= cpu_to_le32(credp
->fc_mode
);
355 err
= setxattr(path
, "user.virtfs.mode", &tmp_mode
, sizeof(mode_t
), 0);
360 if (credp
->fc_rdev
!= -1) {
361 uint64_t tmp_rdev
= cpu_to_le64(credp
->fc_rdev
);
362 err
= setxattr(path
, "user.virtfs.rdev", &tmp_rdev
, sizeof(dev_t
), 0);
370 static int local_post_create_passthrough(FsContext
*fs_ctx
, const char *path
,
375 buffer
= rpath(fs_ctx
, path
);
376 if (lchown(buffer
, credp
->fc_uid
, credp
->fc_gid
) < 0) {
378 * If we fail to change ownership and if we are
379 * using security model none. Ignore the error
381 if ((fs_ctx
->export_flags
& V9FS_SEC_MASK
) != V9FS_SM_NONE
) {
386 if (chmod(buffer
, credp
->fc_mode
& 07777) < 0) {
397 static ssize_t
local_readlink(FsContext
*fs_ctx
, V9fsPath
*fs_path
,
398 char *buf
, size_t bufsz
)
402 if ((fs_ctx
->export_flags
& V9FS_SM_MAPPED
) ||
403 (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
)) {
406 fd
= local_open_nofollow(fs_ctx
, fs_path
->data
, O_RDONLY
, 0);
411 tsize
= read(fd
, (void *)buf
, bufsz
);
412 } while (tsize
== -1 && errno
== EINTR
);
413 close_preserve_errno(fd
);
414 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
415 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
416 char *dirpath
= g_path_get_dirname(fs_path
->data
);
417 char *name
= g_path_get_basename(fs_path
->data
);
420 dirfd
= local_opendir_nofollow(fs_ctx
, dirpath
);
425 tsize
= readlinkat(dirfd
, name
, buf
, bufsz
);
426 close_preserve_errno(dirfd
);
434 static int local_close(FsContext
*ctx
, V9fsFidOpenState
*fs
)
436 return close(fs
->fd
);
439 static int local_closedir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
441 return closedir(fs
->dir
.stream
);
444 static int local_open(FsContext
*ctx
, V9fsPath
*fs_path
,
445 int flags
, V9fsFidOpenState
*fs
)
449 fd
= local_open_nofollow(ctx
, fs_path
->data
, flags
, 0);
457 static int local_opendir(FsContext
*ctx
,
458 V9fsPath
*fs_path
, V9fsFidOpenState
*fs
)
463 dirfd
= local_opendir_nofollow(ctx
, fs_path
->data
);
468 stream
= fdopendir(dirfd
);
472 fs
->dir
.stream
= stream
;
476 static void local_rewinddir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
478 rewinddir(fs
->dir
.stream
);
481 static off_t
local_telldir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
483 return telldir(fs
->dir
.stream
);
486 static struct dirent
*local_readdir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
488 struct dirent
*entry
;
491 entry
= readdir(fs
->dir
.stream
);
496 if (ctx
->export_flags
& V9FS_SM_MAPPED
) {
497 entry
->d_type
= DT_UNKNOWN
;
498 } else if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
499 if (!strcmp(entry
->d_name
, VIRTFS_META_DIR
)) {
500 /* skp the meta data directory */
503 entry
->d_type
= DT_UNKNOWN
;
509 static void local_seekdir(FsContext
*ctx
, V9fsFidOpenState
*fs
, off_t off
)
511 seekdir(fs
->dir
.stream
, off
);
514 static ssize_t
local_preadv(FsContext
*ctx
, V9fsFidOpenState
*fs
,
515 const struct iovec
*iov
,
516 int iovcnt
, off_t offset
)
519 return preadv(fs
->fd
, iov
, iovcnt
, offset
);
521 int err
= lseek(fs
->fd
, offset
, SEEK_SET
);
525 return readv(fs
->fd
, iov
, iovcnt
);
530 static ssize_t
local_pwritev(FsContext
*ctx
, V9fsFidOpenState
*fs
,
531 const struct iovec
*iov
,
532 int iovcnt
, off_t offset
)
536 ret
= pwritev(fs
->fd
, iov
, iovcnt
, offset
);
538 int err
= lseek(fs
->fd
, offset
, SEEK_SET
);
542 ret
= writev(fs
->fd
, iov
, iovcnt
);
545 #ifdef CONFIG_SYNC_FILE_RANGE
546 if (ret
> 0 && ctx
->export_flags
& V9FS_IMMEDIATE_WRITEOUT
) {
548 * Initiate a writeback. This is not a data integrity sync.
549 * We want to ensure that we don't leave dirty pages in the cache
550 * after write when writeout=immediate is sepcified.
552 sync_file_range(fs
->fd
, offset
, ret
,
553 SYNC_FILE_RANGE_WAIT_BEFORE
| SYNC_FILE_RANGE_WRITE
);
559 static int local_chmod(FsContext
*fs_ctx
, V9fsPath
*fs_path
, FsCred
*credp
)
563 char *path
= fs_path
->data
;
565 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
566 buffer
= rpath(fs_ctx
, path
);
567 ret
= local_set_xattr(buffer
, credp
);
569 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
570 return local_set_mapped_file_attr(fs_ctx
, path
, credp
);
571 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
572 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
573 buffer
= rpath(fs_ctx
, path
);
574 ret
= chmod(buffer
, credp
->fc_mode
);
580 static int local_mknod(FsContext
*fs_ctx
, V9fsPath
*dir_path
,
581 const char *name
, FsCred
*credp
)
589 v9fs_string_init(&fullname
);
590 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
591 path
= fullname
.data
;
593 /* Determine the security model */
594 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
595 buffer
= rpath(fs_ctx
, path
);
596 err
= mknod(buffer
, SM_LOCAL_MODE_BITS
|S_IFREG
, 0);
600 err
= local_set_xattr(buffer
, credp
);
605 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
607 buffer
= rpath(fs_ctx
, path
);
608 err
= mknod(buffer
, SM_LOCAL_MODE_BITS
|S_IFREG
, 0);
612 err
= local_set_mapped_file_attr(fs_ctx
, path
, credp
);
617 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
618 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
619 buffer
= rpath(fs_ctx
, path
);
620 err
= mknod(buffer
, credp
->fc_mode
, credp
->fc_rdev
);
624 err
= local_post_create_passthrough(fs_ctx
, path
, credp
);
637 v9fs_string_free(&fullname
);
641 static int local_mkdir(FsContext
*fs_ctx
, V9fsPath
*dir_path
,
642 const char *name
, FsCred
*credp
)
650 v9fs_string_init(&fullname
);
651 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
652 path
= fullname
.data
;
654 /* Determine the security model */
655 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
656 buffer
= rpath(fs_ctx
, path
);
657 err
= mkdir(buffer
, SM_LOCAL_DIR_MODE_BITS
);
661 credp
->fc_mode
= credp
->fc_mode
|S_IFDIR
;
662 err
= local_set_xattr(buffer
, credp
);
667 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
668 buffer
= rpath(fs_ctx
, path
);
669 err
= mkdir(buffer
, SM_LOCAL_DIR_MODE_BITS
);
673 credp
->fc_mode
= credp
->fc_mode
|S_IFDIR
;
674 err
= local_set_mapped_file_attr(fs_ctx
, path
, credp
);
679 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
680 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
681 buffer
= rpath(fs_ctx
, path
);
682 err
= mkdir(buffer
, credp
->fc_mode
);
686 err
= local_post_create_passthrough(fs_ctx
, path
, credp
);
699 v9fs_string_free(&fullname
);
703 static int local_fstat(FsContext
*fs_ctx
, int fid_type
,
704 V9fsFidOpenState
*fs
, struct stat
*stbuf
)
708 if (fid_type
== P9_FID_DIR
) {
709 fd
= dirfd(fs
->dir
.stream
);
714 err
= fstat(fd
, stbuf
);
718 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
719 /* Actual credentials are part of extended attrs */
725 if (fgetxattr(fd
, "user.virtfs.uid", &tmp_uid
, sizeof(uid_t
)) > 0) {
726 stbuf
->st_uid
= le32_to_cpu(tmp_uid
);
728 if (fgetxattr(fd
, "user.virtfs.gid", &tmp_gid
, sizeof(gid_t
)) > 0) {
729 stbuf
->st_gid
= le32_to_cpu(tmp_gid
);
731 if (fgetxattr(fd
, "user.virtfs.mode", &tmp_mode
, sizeof(mode_t
)) > 0) {
732 stbuf
->st_mode
= le32_to_cpu(tmp_mode
);
734 if (fgetxattr(fd
, "user.virtfs.rdev", &tmp_dev
, sizeof(dev_t
)) > 0) {
735 stbuf
->st_rdev
= le64_to_cpu(tmp_dev
);
737 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
744 static int local_open2(FsContext
*fs_ctx
, V9fsPath
*dir_path
, const char *name
,
745 int flags
, FsCred
*credp
, V9fsFidOpenState
*fs
)
755 * Mark all the open to not follow symlinks
759 v9fs_string_init(&fullname
);
760 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
761 path
= fullname
.data
;
763 /* Determine the security model */
764 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
765 buffer
= rpath(fs_ctx
, path
);
766 fd
= open(buffer
, flags
, SM_LOCAL_MODE_BITS
);
771 credp
->fc_mode
= credp
->fc_mode
|S_IFREG
;
772 /* Set cleint credentials in xattr */
773 err
= local_set_xattr(buffer
, credp
);
778 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
779 buffer
= rpath(fs_ctx
, path
);
780 fd
= open(buffer
, flags
, SM_LOCAL_MODE_BITS
);
785 credp
->fc_mode
= credp
->fc_mode
|S_IFREG
;
786 /* Set client credentials in .virtfs_metadata directory files */
787 err
= local_set_mapped_file_attr(fs_ctx
, path
, credp
);
792 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
793 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
794 buffer
= rpath(fs_ctx
, path
);
795 fd
= open(buffer
, flags
, credp
->fc_mode
);
800 err
= local_post_create_passthrough(fs_ctx
, path
, credp
);
816 v9fs_string_free(&fullname
);
821 static int local_symlink(FsContext
*fs_ctx
, const char *oldpath
,
822 V9fsPath
*dir_path
, const char *name
, FsCred
*credp
)
830 v9fs_string_init(&fullname
);
831 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
832 newpath
= fullname
.data
;
834 /* Determine the security model */
835 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
837 ssize_t oldpath_size
, write_size
;
838 buffer
= rpath(fs_ctx
, newpath
);
839 fd
= open(buffer
, O_CREAT
|O_EXCL
|O_RDWR
|O_NOFOLLOW
, SM_LOCAL_MODE_BITS
);
844 /* Write the oldpath (target) to the file. */
845 oldpath_size
= strlen(oldpath
);
847 write_size
= write(fd
, (void *)oldpath
, oldpath_size
);
848 } while (write_size
== -1 && errno
== EINTR
);
850 if (write_size
!= oldpath_size
) {
857 /* Set cleint credentials in symlink's xattr */
858 credp
->fc_mode
= credp
->fc_mode
|S_IFLNK
;
859 err
= local_set_xattr(buffer
, credp
);
864 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
866 ssize_t oldpath_size
, write_size
;
867 buffer
= rpath(fs_ctx
, newpath
);
868 fd
= open(buffer
, O_CREAT
|O_EXCL
|O_RDWR
|O_NOFOLLOW
, SM_LOCAL_MODE_BITS
);
873 /* Write the oldpath (target) to the file. */
874 oldpath_size
= strlen(oldpath
);
876 write_size
= write(fd
, (void *)oldpath
, oldpath_size
);
877 } while (write_size
== -1 && errno
== EINTR
);
879 if (write_size
!= oldpath_size
) {
886 /* Set cleint credentials in symlink's xattr */
887 credp
->fc_mode
= credp
->fc_mode
|S_IFLNK
;
888 err
= local_set_mapped_file_attr(fs_ctx
, newpath
, credp
);
893 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
894 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
895 buffer
= rpath(fs_ctx
, newpath
);
896 err
= symlink(oldpath
, buffer
);
900 err
= lchown(buffer
, credp
->fc_uid
, credp
->fc_gid
);
903 * If we fail to change ownership and if we are
904 * using security model none. Ignore the error
906 if ((fs_ctx
->export_flags
& V9FS_SEC_MASK
) != V9FS_SM_NONE
) {
920 v9fs_string_free(&fullname
);
924 static int local_link(FsContext
*ctx
, V9fsPath
*oldpath
,
925 V9fsPath
*dirpath
, const char *name
)
927 char *odirpath
= g_path_get_dirname(oldpath
->data
);
928 char *oname
= g_path_get_basename(oldpath
->data
);
932 odirfd
= local_opendir_nofollow(ctx
, odirpath
);
937 ndirfd
= local_opendir_nofollow(ctx
, dirpath
->data
);
939 close_preserve_errno(odirfd
);
943 ret
= linkat(odirfd
, oname
, ndirfd
, name
, 0);
948 /* now link the virtfs_metadata files */
949 if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
950 int omap_dirfd
, nmap_dirfd
;
952 ret
= mkdirat(ndirfd
, VIRTFS_META_DIR
, 0700);
953 if (ret
< 0 && errno
!= EEXIST
) {
957 omap_dirfd
= openat_dir(odirfd
, VIRTFS_META_DIR
);
958 if (omap_dirfd
== -1) {
962 nmap_dirfd
= openat_dir(ndirfd
, VIRTFS_META_DIR
);
963 if (nmap_dirfd
== -1) {
964 close_preserve_errno(omap_dirfd
);
968 ret
= linkat(omap_dirfd
, oname
, nmap_dirfd
, name
, 0);
969 close_preserve_errno(nmap_dirfd
);
970 close_preserve_errno(omap_dirfd
);
971 if (ret
< 0 && errno
!= ENOENT
) {
982 unlinkat_preserve_errno(ndirfd
, name
, 0);
984 close_preserve_errno(ndirfd
);
985 close_preserve_errno(odirfd
);
992 static int local_truncate(FsContext
*ctx
, V9fsPath
*fs_path
, off_t size
)
996 fd
= local_open_nofollow(ctx
, fs_path
->data
, O_WRONLY
, 0);
1000 ret
= ftruncate(fd
, size
);
1001 close_preserve_errno(fd
);
1005 static int local_chown(FsContext
*fs_ctx
, V9fsPath
*fs_path
, FsCred
*credp
)
1009 char *path
= fs_path
->data
;
1011 if ((credp
->fc_uid
== -1 && credp
->fc_gid
== -1) ||
1012 (fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
1013 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
1014 buffer
= rpath(fs_ctx
, path
);
1015 ret
= lchown(buffer
, credp
->fc_uid
, credp
->fc_gid
);
1017 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
1018 buffer
= rpath(fs_ctx
, path
);
1019 ret
= local_set_xattr(buffer
, credp
);
1021 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
1022 return local_set_mapped_file_attr(fs_ctx
, path
, credp
);
1027 static int local_utimensat(FsContext
*s
, V9fsPath
*fs_path
,
1028 const struct timespec
*buf
)
1030 char *dirpath
= g_path_get_dirname(fs_path
->data
);
1031 char *name
= g_path_get_basename(fs_path
->data
);
1032 int dirfd
, ret
= -1;
1034 dirfd
= local_opendir_nofollow(s
, dirpath
);
1039 ret
= utimensat(dirfd
, name
, buf
, AT_SYMLINK_NOFOLLOW
);
1040 close_preserve_errno(dirfd
);
1047 static int local_unlinkat_common(FsContext
*ctx
, int dirfd
, const char *name
,
1052 if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
1055 if (flags
== AT_REMOVEDIR
) {
1058 fd
= openat(dirfd
, name
, O_RDONLY
| O_DIRECTORY
| O_PATH
);
1063 * If directory remove .virtfs_metadata contained in the
1066 ret
= unlinkat(fd
, VIRTFS_META_DIR
, AT_REMOVEDIR
);
1067 close_preserve_errno(fd
);
1068 if (ret
< 0 && errno
!= ENOENT
) {
1070 * We didn't had the .virtfs_metadata file. May be file created
1071 * in non-mapped mode ?. Ignore ENOENT.
1077 * Now remove the name from parent directory
1078 * .virtfs_metadata directory.
1080 map_dirfd
= openat_dir(dirfd
, VIRTFS_META_DIR
);
1081 ret
= unlinkat(map_dirfd
, name
, 0);
1082 close_preserve_errno(map_dirfd
);
1083 if (ret
< 0 && errno
!= ENOENT
) {
1085 * We didn't had the .virtfs_metadata file. May be file created
1086 * in non-mapped mode ?. Ignore ENOENT.
1092 ret
= unlinkat(dirfd
, name
, flags
);
1097 static int local_remove(FsContext
*ctx
, const char *path
)
1100 char *dirpath
= g_path_get_dirname(path
);
1101 char *name
= g_path_get_basename(path
);
1106 dirfd
= local_opendir_nofollow(ctx
, dirpath
);
1111 if (fstatat(dirfd
, path
, &stbuf
, AT_SYMLINK_NOFOLLOW
) < 0) {
1115 if (S_ISDIR(stbuf
.st_mode
)) {
1116 flags
|= AT_REMOVEDIR
;
1119 err
= local_unlinkat_common(ctx
, dirfd
, name
, flags
);
1121 close_preserve_errno(dirfd
);
1128 static int local_fsync(FsContext
*ctx
, int fid_type
,
1129 V9fsFidOpenState
*fs
, int datasync
)
1133 if (fid_type
== P9_FID_DIR
) {
1134 fd
= dirfd(fs
->dir
.stream
);
1140 return qemu_fdatasync(fd
);
1146 static int local_statfs(FsContext
*s
, V9fsPath
*fs_path
, struct statfs
*stbuf
)
1150 fd
= local_open_nofollow(s
, fs_path
->data
, O_RDONLY
, 0);
1151 ret
= fstatfs(fd
, stbuf
);
1152 close_preserve_errno(fd
);
1156 static ssize_t
local_lgetxattr(FsContext
*ctx
, V9fsPath
*fs_path
,
1157 const char *name
, void *value
, size_t size
)
1159 char *path
= fs_path
->data
;
1161 return v9fs_get_xattr(ctx
, path
, name
, value
, size
);
1164 static ssize_t
local_llistxattr(FsContext
*ctx
, V9fsPath
*fs_path
,
1165 void *value
, size_t size
)
1167 char *path
= fs_path
->data
;
1169 return v9fs_list_xattr(ctx
, path
, value
, size
);
1172 static int local_lsetxattr(FsContext
*ctx
, V9fsPath
*fs_path
, const char *name
,
1173 void *value
, size_t size
, int flags
)
1175 char *path
= fs_path
->data
;
1177 return v9fs_set_xattr(ctx
, path
, name
, value
, size
, flags
);
1180 static int local_lremovexattr(FsContext
*ctx
, V9fsPath
*fs_path
,
1183 char *path
= fs_path
->data
;
1185 return v9fs_remove_xattr(ctx
, path
, name
);
1188 static int local_name_to_path(FsContext
*ctx
, V9fsPath
*dir_path
,
1189 const char *name
, V9fsPath
*target
)
1192 v9fs_path_sprintf(target
, "%s/%s", dir_path
->data
, name
);
1194 v9fs_path_sprintf(target
, "%s", name
);
1199 static int local_renameat(FsContext
*ctx
, V9fsPath
*olddir
,
1200 const char *old_name
, V9fsPath
*newdir
,
1201 const char *new_name
)
1206 odirfd
= local_opendir_nofollow(ctx
, olddir
->data
);
1211 ndirfd
= local_opendir_nofollow(ctx
, newdir
->data
);
1213 close_preserve_errno(odirfd
);
1217 ret
= renameat(odirfd
, old_name
, ndirfd
, new_name
);
1222 if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
1223 int omap_dirfd
, nmap_dirfd
;
1225 ret
= mkdirat(ndirfd
, VIRTFS_META_DIR
, 0700);
1226 if (ret
< 0 && errno
!= EEXIST
) {
1227 goto err_undo_rename
;
1230 omap_dirfd
= openat_dir(odirfd
, VIRTFS_META_DIR
);
1231 if (omap_dirfd
== -1) {
1235 nmap_dirfd
= openat_dir(ndirfd
, VIRTFS_META_DIR
);
1236 if (nmap_dirfd
== -1) {
1237 close_preserve_errno(omap_dirfd
);
1241 /* rename the .virtfs_metadata files */
1242 ret
= renameat(omap_dirfd
, old_name
, nmap_dirfd
, new_name
);
1243 close_preserve_errno(nmap_dirfd
);
1244 close_preserve_errno(omap_dirfd
);
1245 if (ret
< 0 && errno
!= ENOENT
) {
1246 goto err_undo_rename
;
1256 renameat_preserve_errno(ndirfd
, new_name
, odirfd
, old_name
);
1258 close_preserve_errno(ndirfd
);
1259 close_preserve_errno(odirfd
);
1263 static void v9fs_path_init_dirname(V9fsPath
*path
, const char *str
)
1265 path
->data
= g_path_get_dirname(str
);
1266 path
->size
= strlen(path
->data
) + 1;
1269 static int local_rename(FsContext
*ctx
, const char *oldpath
,
1270 const char *newpath
)
1273 char *oname
= g_path_get_basename(oldpath
);
1274 char *nname
= g_path_get_basename(newpath
);
1275 V9fsPath olddir
, newdir
;
1277 v9fs_path_init_dirname(&olddir
, oldpath
);
1278 v9fs_path_init_dirname(&newdir
, newpath
);
1280 err
= local_renameat(ctx
, &olddir
, oname
, &newdir
, nname
);
1282 v9fs_path_free(&newdir
);
1283 v9fs_path_free(&olddir
);
1290 static int local_unlinkat(FsContext
*ctx
, V9fsPath
*dir
,
1291 const char *name
, int flags
)
1296 dirfd
= local_opendir_nofollow(ctx
, dir
->data
);
1301 ret
= local_unlinkat_common(ctx
, dirfd
, name
, flags
);
1302 close_preserve_errno(dirfd
);
1306 static int local_ioc_getversion(FsContext
*ctx
, V9fsPath
*path
,
1307 mode_t st_mode
, uint64_t *st_gen
)
1309 #ifdef FS_IOC_GETVERSION
1311 V9fsFidOpenState fid_open
;
1314 * Do not try to open special files like device nodes, fifos etc
1315 * We can get fd for regular files and directories only
1317 if (!S_ISREG(st_mode
) && !S_ISDIR(st_mode
)) {
1321 err
= local_open(ctx
, path
, O_RDONLY
, &fid_open
);
1325 err
= ioctl(fid_open
.fd
, FS_IOC_GETVERSION
, st_gen
);
1326 local_close(ctx
, &fid_open
);
1334 static int local_init(FsContext
*ctx
)
1336 struct statfs stbuf
;
1337 LocalData
*data
= g_malloc(sizeof(*data
));
1339 data
->mountfd
= open(ctx
->fs_root
, O_DIRECTORY
| O_RDONLY
);
1340 if (data
->mountfd
== -1) {
1344 #ifdef FS_IOC_GETVERSION
1346 * use ioc_getversion only if the ioctl is definied
1348 if (fstatfs(data
->mountfd
, &stbuf
) < 0) {
1349 close_preserve_errno(data
->mountfd
);
1352 switch (stbuf
.f_type
) {
1353 case EXT2_SUPER_MAGIC
:
1354 case BTRFS_SUPER_MAGIC
:
1355 case REISERFS_SUPER_MAGIC
:
1356 case XFS_SUPER_MAGIC
:
1357 ctx
->exops
.get_st_gen
= local_ioc_getversion
;
1362 if (ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) {
1363 ctx
->xops
= passthrough_xattr_ops
;
1364 } else if (ctx
->export_flags
& V9FS_SM_MAPPED
) {
1365 ctx
->xops
= mapped_xattr_ops
;
1366 } else if (ctx
->export_flags
& V9FS_SM_NONE
) {
1367 ctx
->xops
= none_xattr_ops
;
1368 } else if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
1370 * xattr operation for mapped-file and passthrough
1373 ctx
->xops
= passthrough_xattr_ops
;
1375 ctx
->export_flags
|= V9FS_PATHNAME_FSCONTEXT
;
1377 ctx
->private = data
;
1385 static void local_cleanup(FsContext
*ctx
)
1387 LocalData
*data
= ctx
->private;
1389 close(data
->mountfd
);
1393 static int local_parse_opts(QemuOpts
*opts
, struct FsDriverEntry
*fse
)
1395 const char *sec_model
= qemu_opt_get(opts
, "security_model");
1396 const char *path
= qemu_opt_get(opts
, "path");
1399 error_report("Security model not specified, local fs needs security model");
1400 error_printf("valid options are:"
1401 "\tsecurity_model=[passthrough|mapped-xattr|mapped-file|none]\n");
1405 if (!strcmp(sec_model
, "passthrough")) {
1406 fse
->export_flags
|= V9FS_SM_PASSTHROUGH
;
1407 } else if (!strcmp(sec_model
, "mapped") ||
1408 !strcmp(sec_model
, "mapped-xattr")) {
1409 fse
->export_flags
|= V9FS_SM_MAPPED
;
1410 } else if (!strcmp(sec_model
, "none")) {
1411 fse
->export_flags
|= V9FS_SM_NONE
;
1412 } else if (!strcmp(sec_model
, "mapped-file")) {
1413 fse
->export_flags
|= V9FS_SM_MAPPED_FILE
;
1415 error_report("Invalid security model %s specified", sec_model
);
1416 error_printf("valid options are:"
1417 "\t[passthrough|mapped-xattr|mapped-file|none]\n");
1422 error_report("fsdev: No path specified");
1425 fse
->path
= g_strdup(path
);
1430 FileOperations local_ops
= {
1431 .parse_opts
= local_parse_opts
,
1433 .cleanup
= local_cleanup
,
1434 .lstat
= local_lstat
,
1435 .readlink
= local_readlink
,
1436 .close
= local_close
,
1437 .closedir
= local_closedir
,
1439 .opendir
= local_opendir
,
1440 .rewinddir
= local_rewinddir
,
1441 .telldir
= local_telldir
,
1442 .readdir
= local_readdir
,
1443 .seekdir
= local_seekdir
,
1444 .preadv
= local_preadv
,
1445 .pwritev
= local_pwritev
,
1446 .chmod
= local_chmod
,
1447 .mknod
= local_mknod
,
1448 .mkdir
= local_mkdir
,
1449 .fstat
= local_fstat
,
1450 .open2
= local_open2
,
1451 .symlink
= local_symlink
,
1453 .truncate
= local_truncate
,
1454 .rename
= local_rename
,
1455 .chown
= local_chown
,
1456 .utimensat
= local_utimensat
,
1457 .remove
= local_remove
,
1458 .fsync
= local_fsync
,
1459 .statfs
= local_statfs
,
1460 .lgetxattr
= local_lgetxattr
,
1461 .llistxattr
= local_llistxattr
,
1462 .lsetxattr
= local_lsetxattr
,
1463 .lremovexattr
= local_lremovexattr
,
1464 .name_to_path
= local_name_to_path
,
1465 .renameat
= local_renameat
,
1466 .unlinkat
= local_unlinkat
,