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 #define VIRTFS_META_DIR ".virtfs_metadata"
72 static char *local_mapped_attr_path(FsContext
*ctx
, const char *path
)
75 const char *name
= strrchr(path
, '/');
83 return g_strdup_printf("%s/%.*s/%s/%s", ctx
->fs_root
,
84 dirlen
, path
, VIRTFS_META_DIR
, name
);
87 static FILE *local_fopen(const char *path
, const char *mode
)
91 int flags
= O_NOFOLLOW
;
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
= open(path
, flags
, o_mode
);
107 fp
= fdopen(fd
, mode
);
115 static void local_mapped_file_attr(FsContext
*ctx
, const char *path
,
122 attr_path
= local_mapped_attr_path(ctx
, path
);
123 fp
= local_fopen(attr_path
, "r");
128 memset(buf
, 0, ATTR_MAX
);
129 while (fgets(buf
, ATTR_MAX
, fp
)) {
130 if (!strncmp(buf
, "virtfs.uid", 10)) {
131 stbuf
->st_uid
= atoi(buf
+11);
132 } else if (!strncmp(buf
, "virtfs.gid", 10)) {
133 stbuf
->st_gid
= atoi(buf
+11);
134 } else if (!strncmp(buf
, "virtfs.mode", 11)) {
135 stbuf
->st_mode
= atoi(buf
+12);
136 } else if (!strncmp(buf
, "virtfs.rdev", 11)) {
137 stbuf
->st_rdev
= atoi(buf
+12);
139 memset(buf
, 0, ATTR_MAX
);
144 static int local_lstat(FsContext
*fs_ctx
, V9fsPath
*fs_path
, struct stat
*stbuf
)
148 char *path
= fs_path
->data
;
150 buffer
= rpath(fs_ctx
, path
);
151 err
= lstat(buffer
, stbuf
);
155 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
156 /* Actual credentials are part of extended attrs */
161 if (getxattr(buffer
, "user.virtfs.uid", &tmp_uid
, sizeof(uid_t
)) > 0) {
162 stbuf
->st_uid
= le32_to_cpu(tmp_uid
);
164 if (getxattr(buffer
, "user.virtfs.gid", &tmp_gid
, sizeof(gid_t
)) > 0) {
165 stbuf
->st_gid
= le32_to_cpu(tmp_gid
);
167 if (getxattr(buffer
, "user.virtfs.mode",
168 &tmp_mode
, sizeof(mode_t
)) > 0) {
169 stbuf
->st_mode
= le32_to_cpu(tmp_mode
);
171 if (getxattr(buffer
, "user.virtfs.rdev", &tmp_dev
, sizeof(dev_t
)) > 0) {
172 stbuf
->st_rdev
= le64_to_cpu(tmp_dev
);
174 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
175 local_mapped_file_attr(fs_ctx
, path
, stbuf
);
183 static int local_create_mapped_attr_dir(FsContext
*ctx
, const char *path
)
187 char *tmp_path
= g_strdup(path
);
189 attr_dir
= g_strdup_printf("%s/%s/%s",
190 ctx
->fs_root
, dirname(tmp_path
), VIRTFS_META_DIR
);
192 err
= mkdir(attr_dir
, 0700);
193 if (err
< 0 && errno
== EEXIST
) {
201 static int local_set_mapped_file_attr(FsContext
*ctx
,
202 const char *path
, FsCred
*credp
)
208 int uid
= -1, gid
= -1, mode
= -1, rdev
= -1;
210 attr_path
= local_mapped_attr_path(ctx
, path
);
211 fp
= local_fopen(attr_path
, "r");
213 goto create_map_file
;
215 memset(buf
, 0, ATTR_MAX
);
216 while (fgets(buf
, ATTR_MAX
, fp
)) {
217 if (!strncmp(buf
, "virtfs.uid", 10)) {
219 } else if (!strncmp(buf
, "virtfs.gid", 10)) {
221 } else if (!strncmp(buf
, "virtfs.mode", 11)) {
223 } else if (!strncmp(buf
, "virtfs.rdev", 11)) {
226 memset(buf
, 0, ATTR_MAX
);
229 goto update_map_file
;
232 ret
= local_create_mapped_attr_dir(ctx
, path
);
238 fp
= local_fopen(attr_path
, "w");
244 if (credp
->fc_uid
!= -1) {
247 if (credp
->fc_gid
!= -1) {
250 if (credp
->fc_mode
!= -1) {
251 mode
= credp
->fc_mode
;
253 if (credp
->fc_rdev
!= -1) {
254 rdev
= credp
->fc_rdev
;
259 fprintf(fp
, "virtfs.uid=%d\n", uid
);
262 fprintf(fp
, "virtfs.gid=%d\n", gid
);
265 fprintf(fp
, "virtfs.mode=%d\n", mode
);
268 fprintf(fp
, "virtfs.rdev=%d\n", rdev
);
277 static int local_set_xattr(const char *path
, FsCred
*credp
)
281 if (credp
->fc_uid
!= -1) {
282 uint32_t tmp_uid
= cpu_to_le32(credp
->fc_uid
);
283 err
= setxattr(path
, "user.virtfs.uid", &tmp_uid
, sizeof(uid_t
), 0);
288 if (credp
->fc_gid
!= -1) {
289 uint32_t tmp_gid
= cpu_to_le32(credp
->fc_gid
);
290 err
= setxattr(path
, "user.virtfs.gid", &tmp_gid
, sizeof(gid_t
), 0);
295 if (credp
->fc_mode
!= -1) {
296 uint32_t tmp_mode
= cpu_to_le32(credp
->fc_mode
);
297 err
= setxattr(path
, "user.virtfs.mode", &tmp_mode
, sizeof(mode_t
), 0);
302 if (credp
->fc_rdev
!= -1) {
303 uint64_t tmp_rdev
= cpu_to_le64(credp
->fc_rdev
);
304 err
= setxattr(path
, "user.virtfs.rdev", &tmp_rdev
, sizeof(dev_t
), 0);
312 static int local_post_create_passthrough(FsContext
*fs_ctx
, const char *path
,
317 buffer
= rpath(fs_ctx
, path
);
318 if (lchown(buffer
, credp
->fc_uid
, credp
->fc_gid
) < 0) {
320 * If we fail to change ownership and if we are
321 * using security model none. Ignore the error
323 if ((fs_ctx
->export_flags
& V9FS_SEC_MASK
) != V9FS_SM_NONE
) {
328 if (chmod(buffer
, credp
->fc_mode
& 07777) < 0) {
339 static ssize_t
local_readlink(FsContext
*fs_ctx
, V9fsPath
*fs_path
,
340 char *buf
, size_t bufsz
)
344 char *path
= fs_path
->data
;
346 if ((fs_ctx
->export_flags
& V9FS_SM_MAPPED
) ||
347 (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
)) {
349 buffer
= rpath(fs_ctx
, path
);
350 fd
= open(buffer
, O_RDONLY
| O_NOFOLLOW
);
356 tsize
= read(fd
, (void *)buf
, bufsz
);
357 } while (tsize
== -1 && errno
== EINTR
);
359 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
360 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
361 buffer
= rpath(fs_ctx
, path
);
362 tsize
= readlink(buffer
, buf
, bufsz
);
368 static int local_close(FsContext
*ctx
, V9fsFidOpenState
*fs
)
370 return close(fs
->fd
);
373 static int local_closedir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
375 return closedir(fs
->dir
.stream
);
378 static int local_open(FsContext
*ctx
, V9fsPath
*fs_path
,
379 int flags
, V9fsFidOpenState
*fs
)
383 fd
= local_open_nofollow(ctx
, fs_path
->data
, flags
, 0);
391 static int local_opendir(FsContext
*ctx
,
392 V9fsPath
*fs_path
, V9fsFidOpenState
*fs
)
397 dirfd
= local_opendir_nofollow(ctx
, fs_path
->data
);
402 stream
= fdopendir(dirfd
);
406 fs
->dir
.stream
= stream
;
410 static void local_rewinddir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
412 rewinddir(fs
->dir
.stream
);
415 static off_t
local_telldir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
417 return telldir(fs
->dir
.stream
);
420 static struct dirent
*local_readdir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
422 struct dirent
*entry
;
425 entry
= readdir(fs
->dir
.stream
);
430 if (ctx
->export_flags
& V9FS_SM_MAPPED
) {
431 entry
->d_type
= DT_UNKNOWN
;
432 } else if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
433 if (!strcmp(entry
->d_name
, VIRTFS_META_DIR
)) {
434 /* skp the meta data directory */
437 entry
->d_type
= DT_UNKNOWN
;
443 static void local_seekdir(FsContext
*ctx
, V9fsFidOpenState
*fs
, off_t off
)
445 seekdir(fs
->dir
.stream
, off
);
448 static ssize_t
local_preadv(FsContext
*ctx
, V9fsFidOpenState
*fs
,
449 const struct iovec
*iov
,
450 int iovcnt
, off_t offset
)
453 return preadv(fs
->fd
, iov
, iovcnt
, offset
);
455 int err
= lseek(fs
->fd
, offset
, SEEK_SET
);
459 return readv(fs
->fd
, iov
, iovcnt
);
464 static ssize_t
local_pwritev(FsContext
*ctx
, V9fsFidOpenState
*fs
,
465 const struct iovec
*iov
,
466 int iovcnt
, off_t offset
)
470 ret
= pwritev(fs
->fd
, iov
, iovcnt
, offset
);
472 int err
= lseek(fs
->fd
, offset
, SEEK_SET
);
476 ret
= writev(fs
->fd
, iov
, iovcnt
);
479 #ifdef CONFIG_SYNC_FILE_RANGE
480 if (ret
> 0 && ctx
->export_flags
& V9FS_IMMEDIATE_WRITEOUT
) {
482 * Initiate a writeback. This is not a data integrity sync.
483 * We want to ensure that we don't leave dirty pages in the cache
484 * after write when writeout=immediate is sepcified.
486 sync_file_range(fs
->fd
, offset
, ret
,
487 SYNC_FILE_RANGE_WAIT_BEFORE
| SYNC_FILE_RANGE_WRITE
);
493 static int local_chmod(FsContext
*fs_ctx
, V9fsPath
*fs_path
, FsCred
*credp
)
497 char *path
= fs_path
->data
;
499 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
500 buffer
= rpath(fs_ctx
, path
);
501 ret
= local_set_xattr(buffer
, credp
);
503 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
504 return local_set_mapped_file_attr(fs_ctx
, path
, credp
);
505 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
506 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
507 buffer
= rpath(fs_ctx
, path
);
508 ret
= chmod(buffer
, credp
->fc_mode
);
514 static int local_mknod(FsContext
*fs_ctx
, V9fsPath
*dir_path
,
515 const char *name
, FsCred
*credp
)
523 v9fs_string_init(&fullname
);
524 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
525 path
= fullname
.data
;
527 /* Determine the security model */
528 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
529 buffer
= rpath(fs_ctx
, path
);
530 err
= mknod(buffer
, SM_LOCAL_MODE_BITS
|S_IFREG
, 0);
534 err
= local_set_xattr(buffer
, credp
);
539 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
541 buffer
= rpath(fs_ctx
, path
);
542 err
= mknod(buffer
, SM_LOCAL_MODE_BITS
|S_IFREG
, 0);
546 err
= local_set_mapped_file_attr(fs_ctx
, path
, credp
);
551 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
552 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
553 buffer
= rpath(fs_ctx
, path
);
554 err
= mknod(buffer
, credp
->fc_mode
, credp
->fc_rdev
);
558 err
= local_post_create_passthrough(fs_ctx
, path
, credp
);
571 v9fs_string_free(&fullname
);
575 static int local_mkdir(FsContext
*fs_ctx
, V9fsPath
*dir_path
,
576 const char *name
, FsCred
*credp
)
584 v9fs_string_init(&fullname
);
585 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
586 path
= fullname
.data
;
588 /* Determine the security model */
589 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
590 buffer
= rpath(fs_ctx
, path
);
591 err
= mkdir(buffer
, SM_LOCAL_DIR_MODE_BITS
);
595 credp
->fc_mode
= credp
->fc_mode
|S_IFDIR
;
596 err
= local_set_xattr(buffer
, credp
);
601 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
602 buffer
= rpath(fs_ctx
, path
);
603 err
= mkdir(buffer
, SM_LOCAL_DIR_MODE_BITS
);
607 credp
->fc_mode
= credp
->fc_mode
|S_IFDIR
;
608 err
= local_set_mapped_file_attr(fs_ctx
, path
, credp
);
613 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
614 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
615 buffer
= rpath(fs_ctx
, path
);
616 err
= mkdir(buffer
, credp
->fc_mode
);
620 err
= local_post_create_passthrough(fs_ctx
, path
, credp
);
633 v9fs_string_free(&fullname
);
637 static int local_fstat(FsContext
*fs_ctx
, int fid_type
,
638 V9fsFidOpenState
*fs
, struct stat
*stbuf
)
642 if (fid_type
== P9_FID_DIR
) {
643 fd
= dirfd(fs
->dir
.stream
);
648 err
= fstat(fd
, stbuf
);
652 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
653 /* Actual credentials are part of extended attrs */
659 if (fgetxattr(fd
, "user.virtfs.uid", &tmp_uid
, sizeof(uid_t
)) > 0) {
660 stbuf
->st_uid
= le32_to_cpu(tmp_uid
);
662 if (fgetxattr(fd
, "user.virtfs.gid", &tmp_gid
, sizeof(gid_t
)) > 0) {
663 stbuf
->st_gid
= le32_to_cpu(tmp_gid
);
665 if (fgetxattr(fd
, "user.virtfs.mode", &tmp_mode
, sizeof(mode_t
)) > 0) {
666 stbuf
->st_mode
= le32_to_cpu(tmp_mode
);
668 if (fgetxattr(fd
, "user.virtfs.rdev", &tmp_dev
, sizeof(dev_t
)) > 0) {
669 stbuf
->st_rdev
= le64_to_cpu(tmp_dev
);
671 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
678 static int local_open2(FsContext
*fs_ctx
, V9fsPath
*dir_path
, const char *name
,
679 int flags
, FsCred
*credp
, V9fsFidOpenState
*fs
)
689 * Mark all the open to not follow symlinks
693 v9fs_string_init(&fullname
);
694 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
695 path
= fullname
.data
;
697 /* Determine the security model */
698 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
699 buffer
= rpath(fs_ctx
, path
);
700 fd
= open(buffer
, flags
, SM_LOCAL_MODE_BITS
);
705 credp
->fc_mode
= credp
->fc_mode
|S_IFREG
;
706 /* Set cleint credentials in xattr */
707 err
= local_set_xattr(buffer
, credp
);
712 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
713 buffer
= rpath(fs_ctx
, path
);
714 fd
= open(buffer
, flags
, SM_LOCAL_MODE_BITS
);
719 credp
->fc_mode
= credp
->fc_mode
|S_IFREG
;
720 /* Set client credentials in .virtfs_metadata directory files */
721 err
= local_set_mapped_file_attr(fs_ctx
, path
, credp
);
726 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
727 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
728 buffer
= rpath(fs_ctx
, path
);
729 fd
= open(buffer
, flags
, credp
->fc_mode
);
734 err
= local_post_create_passthrough(fs_ctx
, path
, credp
);
750 v9fs_string_free(&fullname
);
755 static int local_symlink(FsContext
*fs_ctx
, const char *oldpath
,
756 V9fsPath
*dir_path
, const char *name
, FsCred
*credp
)
764 v9fs_string_init(&fullname
);
765 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
766 newpath
= fullname
.data
;
768 /* Determine the security model */
769 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
771 ssize_t oldpath_size
, write_size
;
772 buffer
= rpath(fs_ctx
, newpath
);
773 fd
= open(buffer
, O_CREAT
|O_EXCL
|O_RDWR
|O_NOFOLLOW
, SM_LOCAL_MODE_BITS
);
778 /* Write the oldpath (target) to the file. */
779 oldpath_size
= strlen(oldpath
);
781 write_size
= write(fd
, (void *)oldpath
, oldpath_size
);
782 } while (write_size
== -1 && errno
== EINTR
);
784 if (write_size
!= oldpath_size
) {
791 /* Set cleint credentials in symlink's xattr */
792 credp
->fc_mode
= credp
->fc_mode
|S_IFLNK
;
793 err
= local_set_xattr(buffer
, credp
);
798 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
800 ssize_t oldpath_size
, write_size
;
801 buffer
= rpath(fs_ctx
, newpath
);
802 fd
= open(buffer
, O_CREAT
|O_EXCL
|O_RDWR
|O_NOFOLLOW
, SM_LOCAL_MODE_BITS
);
807 /* Write the oldpath (target) to the file. */
808 oldpath_size
= strlen(oldpath
);
810 write_size
= write(fd
, (void *)oldpath
, oldpath_size
);
811 } while (write_size
== -1 && errno
== EINTR
);
813 if (write_size
!= oldpath_size
) {
820 /* Set cleint credentials in symlink's xattr */
821 credp
->fc_mode
= credp
->fc_mode
|S_IFLNK
;
822 err
= local_set_mapped_file_attr(fs_ctx
, newpath
, credp
);
827 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
828 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
829 buffer
= rpath(fs_ctx
, newpath
);
830 err
= symlink(oldpath
, buffer
);
834 err
= lchown(buffer
, credp
->fc_uid
, credp
->fc_gid
);
837 * If we fail to change ownership and if we are
838 * using security model none. Ignore the error
840 if ((fs_ctx
->export_flags
& V9FS_SEC_MASK
) != V9FS_SM_NONE
) {
854 v9fs_string_free(&fullname
);
858 static int local_link(FsContext
*ctx
, V9fsPath
*oldpath
,
859 V9fsPath
*dirpath
, const char *name
)
863 char *buffer
, *buffer1
;
865 v9fs_string_init(&newpath
);
866 v9fs_string_sprintf(&newpath
, "%s/%s", dirpath
->data
, name
);
868 buffer
= rpath(ctx
, oldpath
->data
);
869 buffer1
= rpath(ctx
, newpath
.data
);
870 ret
= link(buffer
, buffer1
);
874 /* now link the virtfs_metadata files */
875 if (!ret
&& (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
)) {
876 /* Link the .virtfs_metadata files. Create the metada directory */
877 ret
= local_create_mapped_attr_dir(ctx
, newpath
.data
);
881 buffer
= local_mapped_attr_path(ctx
, oldpath
->data
);
882 buffer1
= local_mapped_attr_path(ctx
, newpath
.data
);
883 ret
= link(buffer
, buffer1
);
886 if (ret
< 0 && errno
!= ENOENT
) {
891 v9fs_string_free(&newpath
);
895 static int local_truncate(FsContext
*ctx
, V9fsPath
*fs_path
, off_t size
)
899 char *path
= fs_path
->data
;
901 buffer
= rpath(ctx
, path
);
902 ret
= truncate(buffer
, size
);
907 static int local_rename(FsContext
*ctx
, const char *oldpath
,
911 char *buffer
, *buffer1
;
913 if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
914 err
= local_create_mapped_attr_dir(ctx
, newpath
);
918 /* rename the .virtfs_metadata files */
919 buffer
= local_mapped_attr_path(ctx
, oldpath
);
920 buffer1
= local_mapped_attr_path(ctx
, newpath
);
921 err
= rename(buffer
, buffer1
);
924 if (err
< 0 && errno
!= ENOENT
) {
929 buffer
= rpath(ctx
, oldpath
);
930 buffer1
= rpath(ctx
, newpath
);
931 err
= rename(buffer
, buffer1
);
937 static int local_chown(FsContext
*fs_ctx
, V9fsPath
*fs_path
, FsCred
*credp
)
941 char *path
= fs_path
->data
;
943 if ((credp
->fc_uid
== -1 && credp
->fc_gid
== -1) ||
944 (fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
945 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
946 buffer
= rpath(fs_ctx
, path
);
947 ret
= lchown(buffer
, credp
->fc_uid
, credp
->fc_gid
);
949 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
950 buffer
= rpath(fs_ctx
, path
);
951 ret
= local_set_xattr(buffer
, credp
);
953 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
954 return local_set_mapped_file_attr(fs_ctx
, path
, credp
);
959 static int local_utimensat(FsContext
*s
, V9fsPath
*fs_path
,
960 const struct timespec
*buf
)
964 char *path
= fs_path
->data
;
966 buffer
= rpath(s
, path
);
967 ret
= qemu_utimens(buffer
, buf
);
972 static int local_unlinkat_common(FsContext
*ctx
, int dirfd
, const char *name
,
977 if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
980 if (flags
== AT_REMOVEDIR
) {
983 fd
= openat(dirfd
, name
, O_RDONLY
| O_DIRECTORY
| O_PATH
);
988 * If directory remove .virtfs_metadata contained in the
991 ret
= unlinkat(fd
, VIRTFS_META_DIR
, AT_REMOVEDIR
);
992 close_preserve_errno(fd
);
993 if (ret
< 0 && errno
!= ENOENT
) {
995 * We didn't had the .virtfs_metadata file. May be file created
996 * in non-mapped mode ?. Ignore ENOENT.
1002 * Now remove the name from parent directory
1003 * .virtfs_metadata directory.
1005 map_dirfd
= openat_dir(dirfd
, VIRTFS_META_DIR
);
1006 ret
= unlinkat(map_dirfd
, name
, 0);
1007 close_preserve_errno(map_dirfd
);
1008 if (ret
< 0 && errno
!= ENOENT
) {
1010 * We didn't had the .virtfs_metadata file. May be file created
1011 * in non-mapped mode ?. Ignore ENOENT.
1017 ret
= unlinkat(dirfd
, name
, flags
);
1022 static int local_remove(FsContext
*ctx
, const char *path
)
1025 char *dirpath
= g_path_get_dirname(path
);
1026 char *name
= g_path_get_basename(path
);
1031 dirfd
= local_opendir_nofollow(ctx
, dirpath
);
1036 if (fstatat(dirfd
, path
, &stbuf
, AT_SYMLINK_NOFOLLOW
) < 0) {
1040 if (S_ISDIR(stbuf
.st_mode
)) {
1041 flags
|= AT_REMOVEDIR
;
1044 err
= local_unlinkat_common(ctx
, dirfd
, name
, flags
);
1046 close_preserve_errno(dirfd
);
1053 static int local_fsync(FsContext
*ctx
, int fid_type
,
1054 V9fsFidOpenState
*fs
, int datasync
)
1058 if (fid_type
== P9_FID_DIR
) {
1059 fd
= dirfd(fs
->dir
.stream
);
1065 return qemu_fdatasync(fd
);
1071 static int local_statfs(FsContext
*s
, V9fsPath
*fs_path
, struct statfs
*stbuf
)
1075 char *path
= fs_path
->data
;
1077 buffer
= rpath(s
, path
);
1078 ret
= statfs(buffer
, stbuf
);
1083 static ssize_t
local_lgetxattr(FsContext
*ctx
, V9fsPath
*fs_path
,
1084 const char *name
, void *value
, size_t size
)
1086 char *path
= fs_path
->data
;
1088 return v9fs_get_xattr(ctx
, path
, name
, value
, size
);
1091 static ssize_t
local_llistxattr(FsContext
*ctx
, V9fsPath
*fs_path
,
1092 void *value
, size_t size
)
1094 char *path
= fs_path
->data
;
1096 return v9fs_list_xattr(ctx
, path
, value
, size
);
1099 static int local_lsetxattr(FsContext
*ctx
, V9fsPath
*fs_path
, const char *name
,
1100 void *value
, size_t size
, int flags
)
1102 char *path
= fs_path
->data
;
1104 return v9fs_set_xattr(ctx
, path
, name
, value
, size
, flags
);
1107 static int local_lremovexattr(FsContext
*ctx
, V9fsPath
*fs_path
,
1110 char *path
= fs_path
->data
;
1112 return v9fs_remove_xattr(ctx
, path
, name
);
1115 static int local_name_to_path(FsContext
*ctx
, V9fsPath
*dir_path
,
1116 const char *name
, V9fsPath
*target
)
1119 v9fs_path_sprintf(target
, "%s/%s", dir_path
->data
, name
);
1121 v9fs_path_sprintf(target
, "%s", name
);
1126 static int local_renameat(FsContext
*ctx
, V9fsPath
*olddir
,
1127 const char *old_name
, V9fsPath
*newdir
,
1128 const char *new_name
)
1131 V9fsString old_full_name
, new_full_name
;
1133 v9fs_string_init(&old_full_name
);
1134 v9fs_string_init(&new_full_name
);
1136 v9fs_string_sprintf(&old_full_name
, "%s/%s", olddir
->data
, old_name
);
1137 v9fs_string_sprintf(&new_full_name
, "%s/%s", newdir
->data
, new_name
);
1139 ret
= local_rename(ctx
, old_full_name
.data
, new_full_name
.data
);
1140 v9fs_string_free(&old_full_name
);
1141 v9fs_string_free(&new_full_name
);
1145 static int local_unlinkat(FsContext
*ctx
, V9fsPath
*dir
,
1146 const char *name
, int flags
)
1151 dirfd
= local_opendir_nofollow(ctx
, dir
->data
);
1156 ret
= local_unlinkat_common(ctx
, dirfd
, name
, flags
);
1157 close_preserve_errno(dirfd
);
1161 static int local_ioc_getversion(FsContext
*ctx
, V9fsPath
*path
,
1162 mode_t st_mode
, uint64_t *st_gen
)
1164 #ifdef FS_IOC_GETVERSION
1166 V9fsFidOpenState fid_open
;
1169 * Do not try to open special files like device nodes, fifos etc
1170 * We can get fd for regular files and directories only
1172 if (!S_ISREG(st_mode
) && !S_ISDIR(st_mode
)) {
1176 err
= local_open(ctx
, path
, O_RDONLY
, &fid_open
);
1180 err
= ioctl(fid_open
.fd
, FS_IOC_GETVERSION
, st_gen
);
1181 local_close(ctx
, &fid_open
);
1189 static int local_init(FsContext
*ctx
)
1191 struct statfs stbuf
;
1192 LocalData
*data
= g_malloc(sizeof(*data
));
1194 data
->mountfd
= open(ctx
->fs_root
, O_DIRECTORY
| O_RDONLY
);
1195 if (data
->mountfd
== -1) {
1199 #ifdef FS_IOC_GETVERSION
1201 * use ioc_getversion only if the ioctl is definied
1203 if (fstatfs(data
->mountfd
, &stbuf
) < 0) {
1204 close_preserve_errno(data
->mountfd
);
1207 switch (stbuf
.f_type
) {
1208 case EXT2_SUPER_MAGIC
:
1209 case BTRFS_SUPER_MAGIC
:
1210 case REISERFS_SUPER_MAGIC
:
1211 case XFS_SUPER_MAGIC
:
1212 ctx
->exops
.get_st_gen
= local_ioc_getversion
;
1217 if (ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) {
1218 ctx
->xops
= passthrough_xattr_ops
;
1219 } else if (ctx
->export_flags
& V9FS_SM_MAPPED
) {
1220 ctx
->xops
= mapped_xattr_ops
;
1221 } else if (ctx
->export_flags
& V9FS_SM_NONE
) {
1222 ctx
->xops
= none_xattr_ops
;
1223 } else if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
1225 * xattr operation for mapped-file and passthrough
1228 ctx
->xops
= passthrough_xattr_ops
;
1230 ctx
->export_flags
|= V9FS_PATHNAME_FSCONTEXT
;
1232 ctx
->private = data
;
1240 static void local_cleanup(FsContext
*ctx
)
1242 LocalData
*data
= ctx
->private;
1244 close(data
->mountfd
);
1248 static int local_parse_opts(QemuOpts
*opts
, struct FsDriverEntry
*fse
)
1250 const char *sec_model
= qemu_opt_get(opts
, "security_model");
1251 const char *path
= qemu_opt_get(opts
, "path");
1254 error_report("Security model not specified, local fs needs security model");
1255 error_printf("valid options are:"
1256 "\tsecurity_model=[passthrough|mapped-xattr|mapped-file|none]\n");
1260 if (!strcmp(sec_model
, "passthrough")) {
1261 fse
->export_flags
|= V9FS_SM_PASSTHROUGH
;
1262 } else if (!strcmp(sec_model
, "mapped") ||
1263 !strcmp(sec_model
, "mapped-xattr")) {
1264 fse
->export_flags
|= V9FS_SM_MAPPED
;
1265 } else if (!strcmp(sec_model
, "none")) {
1266 fse
->export_flags
|= V9FS_SM_NONE
;
1267 } else if (!strcmp(sec_model
, "mapped-file")) {
1268 fse
->export_flags
|= V9FS_SM_MAPPED_FILE
;
1270 error_report("Invalid security model %s specified", sec_model
);
1271 error_printf("valid options are:"
1272 "\t[passthrough|mapped-xattr|mapped-file|none]\n");
1277 error_report("fsdev: No path specified");
1280 fse
->path
= g_strdup(path
);
1285 FileOperations local_ops
= {
1286 .parse_opts
= local_parse_opts
,
1288 .cleanup
= local_cleanup
,
1289 .lstat
= local_lstat
,
1290 .readlink
= local_readlink
,
1291 .close
= local_close
,
1292 .closedir
= local_closedir
,
1294 .opendir
= local_opendir
,
1295 .rewinddir
= local_rewinddir
,
1296 .telldir
= local_telldir
,
1297 .readdir
= local_readdir
,
1298 .seekdir
= local_seekdir
,
1299 .preadv
= local_preadv
,
1300 .pwritev
= local_pwritev
,
1301 .chmod
= local_chmod
,
1302 .mknod
= local_mknod
,
1303 .mkdir
= local_mkdir
,
1304 .fstat
= local_fstat
,
1305 .open2
= local_open2
,
1306 .symlink
= local_symlink
,
1308 .truncate
= local_truncate
,
1309 .rename
= local_rename
,
1310 .chown
= local_chown
,
1311 .utimensat
= local_utimensat
,
1312 .remove
= local_remove
,
1313 .fsync
= local_fsync
,
1314 .statfs
= local_statfs
,
1315 .lgetxattr
= local_lgetxattr
,
1316 .llistxattr
= local_llistxattr
,
1317 .lsetxattr
= local_lsetxattr
,
1318 .lremovexattr
= local_lremovexattr
,
1319 .name_to_path
= local_name_to_path
,
1320 .renameat
= local_renameat
,
1321 .unlinkat
= local_unlinkat
,