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.
16 #include "fsdev/qemu-fsdev.h" /* local_ops */
17 #include <arpa/inet.h>
20 #include <sys/socket.h>
22 #include "qemu/xattr.h"
25 #ifdef CONFIG_LINUX_MAGIC_H
26 #include <linux/magic.h>
28 #include <sys/ioctl.h>
30 #ifndef XFS_SUPER_MAGIC
31 #define XFS_SUPER_MAGIC 0x58465342
33 #ifndef EXT2_SUPER_MAGIC
34 #define EXT2_SUPER_MAGIC 0xEF53
36 #ifndef REISERFS_SUPER_MAGIC
37 #define REISERFS_SUPER_MAGIC 0x52654973
39 #ifndef BTRFS_SUPER_MAGIC
40 #define BTRFS_SUPER_MAGIC 0x9123683E
43 #define VIRTFS_META_DIR ".virtfs_metadata"
45 static char *local_mapped_attr_path(FsContext
*ctx
, const char *path
)
48 const char *name
= strrchr(path
, '/');
56 return g_strdup_printf("%s/%.*s/%s/%s", ctx
->fs_root
,
57 dirlen
, path
, VIRTFS_META_DIR
, name
);
60 static FILE *local_fopen(const char *path
, const char *mode
)
64 int flags
= O_NOFOLLOW
;
66 * only supports two modes
70 } else if (mode
[0] == 'w') {
71 flags
|= O_WRONLY
| O_TRUNC
| O_CREAT
;
72 o_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
;
76 fd
= open(path
, flags
, o_mode
);
80 fp
= fdopen(fd
, mode
);
88 static void local_mapped_file_attr(FsContext
*ctx
, const char *path
,
95 attr_path
= local_mapped_attr_path(ctx
, path
);
96 fp
= local_fopen(attr_path
, "r");
101 memset(buf
, 0, ATTR_MAX
);
102 while (fgets(buf
, ATTR_MAX
, fp
)) {
103 if (!strncmp(buf
, "virtfs.uid", 10)) {
104 stbuf
->st_uid
= atoi(buf
+11);
105 } else if (!strncmp(buf
, "virtfs.gid", 10)) {
106 stbuf
->st_gid
= atoi(buf
+11);
107 } else if (!strncmp(buf
, "virtfs.mode", 11)) {
108 stbuf
->st_mode
= atoi(buf
+12);
109 } else if (!strncmp(buf
, "virtfs.rdev", 11)) {
110 stbuf
->st_rdev
= atoi(buf
+12);
112 memset(buf
, 0, ATTR_MAX
);
117 static int local_lstat(FsContext
*fs_ctx
, V9fsPath
*fs_path
, struct stat
*stbuf
)
121 char *path
= fs_path
->data
;
123 buffer
= rpath(fs_ctx
, path
);
124 err
= lstat(buffer
, stbuf
);
128 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
129 /* Actual credentials are part of extended attrs */
134 if (getxattr(buffer
, "user.virtfs.uid", &tmp_uid
, sizeof(uid_t
)) > 0) {
135 stbuf
->st_uid
= le32_to_cpu(tmp_uid
);
137 if (getxattr(buffer
, "user.virtfs.gid", &tmp_gid
, sizeof(gid_t
)) > 0) {
138 stbuf
->st_gid
= le32_to_cpu(tmp_gid
);
140 if (getxattr(buffer
, "user.virtfs.mode",
141 &tmp_mode
, sizeof(mode_t
)) > 0) {
142 stbuf
->st_mode
= le32_to_cpu(tmp_mode
);
144 if (getxattr(buffer
, "user.virtfs.rdev", &tmp_dev
, sizeof(dev_t
)) > 0) {
145 stbuf
->st_rdev
= le64_to_cpu(tmp_dev
);
147 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
148 local_mapped_file_attr(fs_ctx
, path
, stbuf
);
156 static int local_create_mapped_attr_dir(FsContext
*ctx
, const char *path
)
160 char *tmp_path
= g_strdup(path
);
162 attr_dir
= g_strdup_printf("%s/%s/%s",
163 ctx
->fs_root
, dirname(tmp_path
), VIRTFS_META_DIR
);
165 err
= mkdir(attr_dir
, 0700);
166 if (err
< 0 && errno
== EEXIST
) {
174 static int local_set_mapped_file_attr(FsContext
*ctx
,
175 const char *path
, FsCred
*credp
)
181 int uid
= -1, gid
= -1, mode
= -1, rdev
= -1;
183 attr_path
= local_mapped_attr_path(ctx
, path
);
184 fp
= local_fopen(attr_path
, "r");
186 goto create_map_file
;
188 memset(buf
, 0, ATTR_MAX
);
189 while (fgets(buf
, ATTR_MAX
, fp
)) {
190 if (!strncmp(buf
, "virtfs.uid", 10)) {
192 } else if (!strncmp(buf
, "virtfs.gid", 10)) {
194 } else if (!strncmp(buf
, "virtfs.mode", 11)) {
196 } else if (!strncmp(buf
, "virtfs.rdev", 11)) {
199 memset(buf
, 0, ATTR_MAX
);
202 goto update_map_file
;
205 ret
= local_create_mapped_attr_dir(ctx
, path
);
211 fp
= local_fopen(attr_path
, "w");
217 if (credp
->fc_uid
!= -1) {
220 if (credp
->fc_gid
!= -1) {
223 if (credp
->fc_mode
!= -1) {
224 mode
= credp
->fc_mode
;
226 if (credp
->fc_rdev
!= -1) {
227 rdev
= credp
->fc_rdev
;
232 fprintf(fp
, "virtfs.uid=%d\n", uid
);
235 fprintf(fp
, "virtfs.gid=%d\n", gid
);
238 fprintf(fp
, "virtfs.mode=%d\n", mode
);
241 fprintf(fp
, "virtfs.rdev=%d\n", rdev
);
250 static int local_set_xattr(const char *path
, FsCred
*credp
)
254 if (credp
->fc_uid
!= -1) {
255 uint32_t tmp_uid
= cpu_to_le32(credp
->fc_uid
);
256 err
= setxattr(path
, "user.virtfs.uid", &tmp_uid
, sizeof(uid_t
), 0);
261 if (credp
->fc_gid
!= -1) {
262 uint32_t tmp_gid
= cpu_to_le32(credp
->fc_gid
);
263 err
= setxattr(path
, "user.virtfs.gid", &tmp_gid
, sizeof(gid_t
), 0);
268 if (credp
->fc_mode
!= -1) {
269 uint32_t tmp_mode
= cpu_to_le32(credp
->fc_mode
);
270 err
= setxattr(path
, "user.virtfs.mode", &tmp_mode
, sizeof(mode_t
), 0);
275 if (credp
->fc_rdev
!= -1) {
276 uint64_t tmp_rdev
= cpu_to_le64(credp
->fc_rdev
);
277 err
= setxattr(path
, "user.virtfs.rdev", &tmp_rdev
, sizeof(dev_t
), 0);
285 static int local_post_create_passthrough(FsContext
*fs_ctx
, const char *path
,
290 buffer
= rpath(fs_ctx
, path
);
291 if (lchown(buffer
, credp
->fc_uid
, credp
->fc_gid
) < 0) {
293 * If we fail to change ownership and if we are
294 * using security model none. Ignore the error
296 if ((fs_ctx
->export_flags
& V9FS_SEC_MASK
) != V9FS_SM_NONE
) {
301 if (chmod(buffer
, credp
->fc_mode
& 07777) < 0) {
312 static ssize_t
local_readlink(FsContext
*fs_ctx
, V9fsPath
*fs_path
,
313 char *buf
, size_t bufsz
)
317 char *path
= fs_path
->data
;
319 if ((fs_ctx
->export_flags
& V9FS_SM_MAPPED
) ||
320 (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
)) {
322 buffer
= rpath(fs_ctx
, path
);
323 fd
= open(buffer
, O_RDONLY
| O_NOFOLLOW
);
329 tsize
= read(fd
, (void *)buf
, bufsz
);
330 } while (tsize
== -1 && errno
== EINTR
);
332 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
333 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
334 buffer
= rpath(fs_ctx
, path
);
335 tsize
= readlink(buffer
, buf
, bufsz
);
341 static int local_close(FsContext
*ctx
, V9fsFidOpenState
*fs
)
343 return close(fs
->fd
);
346 static int local_closedir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
348 return closedir(fs
->dir
);
351 static int local_open(FsContext
*ctx
, V9fsPath
*fs_path
,
352 int flags
, V9fsFidOpenState
*fs
)
355 char *path
= fs_path
->data
;
357 buffer
= rpath(ctx
, path
);
358 fs
->fd
= open(buffer
, flags
| O_NOFOLLOW
);
363 static int local_opendir(FsContext
*ctx
,
364 V9fsPath
*fs_path
, V9fsFidOpenState
*fs
)
367 char *path
= fs_path
->data
;
369 buffer
= rpath(ctx
, path
);
370 fs
->dir
= opendir(buffer
);
378 static void local_rewinddir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
383 static off_t
local_telldir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
385 return telldir(fs
->dir
);
388 static int local_readdir_r(FsContext
*ctx
, V9fsFidOpenState
*fs
,
389 struct dirent
*entry
,
390 struct dirent
**result
)
395 ret
= readdir_r(fs
->dir
, entry
, result
);
396 if (ctx
->export_flags
& V9FS_SM_MAPPED
) {
397 entry
->d_type
= DT_UNKNOWN
;
398 } else if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
399 if (!ret
&& *result
!= NULL
&&
400 !strcmp(entry
->d_name
, VIRTFS_META_DIR
)) {
401 /* skp the meta data directory */
404 entry
->d_type
= DT_UNKNOWN
;
409 static void local_seekdir(FsContext
*ctx
, V9fsFidOpenState
*fs
, off_t off
)
411 seekdir(fs
->dir
, off
);
414 static ssize_t
local_preadv(FsContext
*ctx
, V9fsFidOpenState
*fs
,
415 const struct iovec
*iov
,
416 int iovcnt
, off_t offset
)
419 return preadv(fs
->fd
, iov
, iovcnt
, offset
);
421 int err
= lseek(fs
->fd
, offset
, SEEK_SET
);
425 return readv(fs
->fd
, iov
, iovcnt
);
430 static ssize_t
local_pwritev(FsContext
*ctx
, V9fsFidOpenState
*fs
,
431 const struct iovec
*iov
,
432 int iovcnt
, off_t offset
)
437 ret
= pwritev(fs
->fd
, iov
, iovcnt
, offset
);
439 int err
= lseek(fs
->fd
, offset
, SEEK_SET
);
443 ret
= writev(fs
->fd
, iov
, iovcnt
);
446 #ifdef CONFIG_SYNC_FILE_RANGE
447 if (ret
> 0 && ctx
->export_flags
& V9FS_IMMEDIATE_WRITEOUT
) {
449 * Initiate a writeback. This is not a data integrity sync.
450 * We want to ensure that we don't leave dirty pages in the cache
451 * after write when writeout=immediate is sepcified.
453 sync_file_range(fs
->fd
, offset
, ret
,
454 SYNC_FILE_RANGE_WAIT_BEFORE
| SYNC_FILE_RANGE_WRITE
);
460 static int local_chmod(FsContext
*fs_ctx
, V9fsPath
*fs_path
, FsCred
*credp
)
464 char *path
= fs_path
->data
;
466 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
467 buffer
= rpath(fs_ctx
, path
);
468 ret
= local_set_xattr(buffer
, credp
);
470 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
471 return local_set_mapped_file_attr(fs_ctx
, path
, credp
);
472 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
473 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
474 buffer
= rpath(fs_ctx
, path
);
475 ret
= chmod(buffer
, credp
->fc_mode
);
481 static int local_mknod(FsContext
*fs_ctx
, V9fsPath
*dir_path
,
482 const char *name
, FsCred
*credp
)
490 v9fs_string_init(&fullname
);
491 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
492 path
= fullname
.data
;
494 /* Determine the security model */
495 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
496 buffer
= rpath(fs_ctx
, path
);
497 err
= mknod(buffer
, SM_LOCAL_MODE_BITS
|S_IFREG
, 0);
501 err
= local_set_xattr(buffer
, credp
);
506 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
508 buffer
= rpath(fs_ctx
, path
);
509 err
= mknod(buffer
, SM_LOCAL_MODE_BITS
|S_IFREG
, 0);
513 err
= local_set_mapped_file_attr(fs_ctx
, path
, credp
);
518 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
519 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
520 buffer
= rpath(fs_ctx
, path
);
521 err
= mknod(buffer
, credp
->fc_mode
, credp
->fc_rdev
);
525 err
= local_post_create_passthrough(fs_ctx
, path
, credp
);
538 v9fs_string_free(&fullname
);
542 static int local_mkdir(FsContext
*fs_ctx
, V9fsPath
*dir_path
,
543 const char *name
, FsCred
*credp
)
551 v9fs_string_init(&fullname
);
552 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
553 path
= fullname
.data
;
555 /* Determine the security model */
556 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
557 buffer
= rpath(fs_ctx
, path
);
558 err
= mkdir(buffer
, SM_LOCAL_DIR_MODE_BITS
);
562 credp
->fc_mode
= credp
->fc_mode
|S_IFDIR
;
563 err
= local_set_xattr(buffer
, credp
);
568 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
569 buffer
= rpath(fs_ctx
, path
);
570 err
= mkdir(buffer
, SM_LOCAL_DIR_MODE_BITS
);
574 credp
->fc_mode
= credp
->fc_mode
|S_IFDIR
;
575 err
= local_set_mapped_file_attr(fs_ctx
, path
, credp
);
580 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
581 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
582 buffer
= rpath(fs_ctx
, path
);
583 err
= mkdir(buffer
, credp
->fc_mode
);
587 err
= local_post_create_passthrough(fs_ctx
, path
, credp
);
600 v9fs_string_free(&fullname
);
604 static int local_fstat(FsContext
*fs_ctx
, int fid_type
,
605 V9fsFidOpenState
*fs
, struct stat
*stbuf
)
609 if (fid_type
== P9_FID_DIR
) {
615 err
= fstat(fd
, stbuf
);
619 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
620 /* Actual credentials are part of extended attrs */
626 if (fgetxattr(fd
, "user.virtfs.uid", &tmp_uid
, sizeof(uid_t
)) > 0) {
627 stbuf
->st_uid
= le32_to_cpu(tmp_uid
);
629 if (fgetxattr(fd
, "user.virtfs.gid", &tmp_gid
, sizeof(gid_t
)) > 0) {
630 stbuf
->st_gid
= le32_to_cpu(tmp_gid
);
632 if (fgetxattr(fd
, "user.virtfs.mode", &tmp_mode
, sizeof(mode_t
)) > 0) {
633 stbuf
->st_mode
= le32_to_cpu(tmp_mode
);
635 if (fgetxattr(fd
, "user.virtfs.rdev", &tmp_dev
, sizeof(dev_t
)) > 0) {
636 stbuf
->st_rdev
= le64_to_cpu(tmp_dev
);
638 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
645 static int local_open2(FsContext
*fs_ctx
, V9fsPath
*dir_path
, const char *name
,
646 int flags
, FsCred
*credp
, V9fsFidOpenState
*fs
)
656 * Mark all the open to not follow symlinks
660 v9fs_string_init(&fullname
);
661 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
662 path
= fullname
.data
;
664 /* Determine the security model */
665 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
666 buffer
= rpath(fs_ctx
, path
);
667 fd
= open(buffer
, flags
, SM_LOCAL_MODE_BITS
);
672 credp
->fc_mode
= credp
->fc_mode
|S_IFREG
;
673 /* Set cleint credentials in xattr */
674 err
= local_set_xattr(buffer
, credp
);
679 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
680 buffer
= rpath(fs_ctx
, path
);
681 fd
= open(buffer
, flags
, SM_LOCAL_MODE_BITS
);
686 credp
->fc_mode
= credp
->fc_mode
|S_IFREG
;
687 /* Set client credentials in .virtfs_metadata directory files */
688 err
= local_set_mapped_file_attr(fs_ctx
, path
, credp
);
693 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
694 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
695 buffer
= rpath(fs_ctx
, path
);
696 fd
= open(buffer
, flags
, credp
->fc_mode
);
701 err
= local_post_create_passthrough(fs_ctx
, path
, credp
);
717 v9fs_string_free(&fullname
);
722 static int local_symlink(FsContext
*fs_ctx
, const char *oldpath
,
723 V9fsPath
*dir_path
, const char *name
, FsCred
*credp
)
731 v9fs_string_init(&fullname
);
732 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
733 newpath
= fullname
.data
;
735 /* Determine the security model */
736 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
738 ssize_t oldpath_size
, write_size
;
739 buffer
= rpath(fs_ctx
, newpath
);
740 fd
= open(buffer
, O_CREAT
|O_EXCL
|O_RDWR
|O_NOFOLLOW
, SM_LOCAL_MODE_BITS
);
745 /* Write the oldpath (target) to the file. */
746 oldpath_size
= strlen(oldpath
);
748 write_size
= write(fd
, (void *)oldpath
, oldpath_size
);
749 } while (write_size
== -1 && errno
== EINTR
);
751 if (write_size
!= oldpath_size
) {
758 /* Set cleint credentials in symlink's xattr */
759 credp
->fc_mode
= credp
->fc_mode
|S_IFLNK
;
760 err
= local_set_xattr(buffer
, credp
);
765 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
767 ssize_t oldpath_size
, write_size
;
768 buffer
= rpath(fs_ctx
, newpath
);
769 fd
= open(buffer
, O_CREAT
|O_EXCL
|O_RDWR
|O_NOFOLLOW
, SM_LOCAL_MODE_BITS
);
774 /* Write the oldpath (target) to the file. */
775 oldpath_size
= strlen(oldpath
);
777 write_size
= write(fd
, (void *)oldpath
, oldpath_size
);
778 } while (write_size
== -1 && errno
== EINTR
);
780 if (write_size
!= oldpath_size
) {
787 /* Set cleint credentials in symlink's xattr */
788 credp
->fc_mode
= credp
->fc_mode
|S_IFLNK
;
789 err
= local_set_mapped_file_attr(fs_ctx
, newpath
, credp
);
794 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
795 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
796 buffer
= rpath(fs_ctx
, newpath
);
797 err
= symlink(oldpath
, buffer
);
801 err
= lchown(buffer
, credp
->fc_uid
, credp
->fc_gid
);
804 * If we fail to change ownership and if we are
805 * using security model none. Ignore the error
807 if ((fs_ctx
->export_flags
& V9FS_SEC_MASK
) != V9FS_SM_NONE
) {
821 v9fs_string_free(&fullname
);
825 static int local_link(FsContext
*ctx
, V9fsPath
*oldpath
,
826 V9fsPath
*dirpath
, const char *name
)
830 char *buffer
, *buffer1
;
832 v9fs_string_init(&newpath
);
833 v9fs_string_sprintf(&newpath
, "%s/%s", dirpath
->data
, name
);
835 buffer
= rpath(ctx
, oldpath
->data
);
836 buffer1
= rpath(ctx
, newpath
.data
);
837 ret
= link(buffer
, buffer1
);
841 /* now link the virtfs_metadata files */
842 if (!ret
&& (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
)) {
843 /* Link the .virtfs_metadata files. Create the metada directory */
844 ret
= local_create_mapped_attr_dir(ctx
, newpath
.data
);
848 buffer
= local_mapped_attr_path(ctx
, oldpath
->data
);
849 buffer1
= local_mapped_attr_path(ctx
, newpath
.data
);
850 ret
= link(buffer
, buffer1
);
853 if (ret
< 0 && errno
!= ENOENT
) {
858 v9fs_string_free(&newpath
);
862 static int local_truncate(FsContext
*ctx
, V9fsPath
*fs_path
, off_t size
)
866 char *path
= fs_path
->data
;
868 buffer
= rpath(ctx
, path
);
869 ret
= truncate(buffer
, size
);
874 static int local_rename(FsContext
*ctx
, const char *oldpath
,
878 char *buffer
, *buffer1
;
880 if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
881 err
= local_create_mapped_attr_dir(ctx
, newpath
);
885 /* rename the .virtfs_metadata files */
886 buffer
= local_mapped_attr_path(ctx
, oldpath
);
887 buffer1
= local_mapped_attr_path(ctx
, newpath
);
888 err
= rename(buffer
, buffer1
);
891 if (err
< 0 && errno
!= ENOENT
) {
896 buffer
= rpath(ctx
, oldpath
);
897 buffer1
= rpath(ctx
, newpath
);
898 err
= rename(buffer
, buffer1
);
904 static int local_chown(FsContext
*fs_ctx
, V9fsPath
*fs_path
, FsCred
*credp
)
908 char *path
= fs_path
->data
;
910 if ((credp
->fc_uid
== -1 && credp
->fc_gid
== -1) ||
911 (fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
912 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
913 buffer
= rpath(fs_ctx
, path
);
914 ret
= lchown(buffer
, credp
->fc_uid
, credp
->fc_gid
);
916 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
917 buffer
= rpath(fs_ctx
, path
);
918 ret
= local_set_xattr(buffer
, credp
);
920 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
921 return local_set_mapped_file_attr(fs_ctx
, path
, credp
);
926 static int local_utimensat(FsContext
*s
, V9fsPath
*fs_path
,
927 const struct timespec
*buf
)
931 char *path
= fs_path
->data
;
933 buffer
= rpath(s
, path
);
934 ret
= qemu_utimens(buffer
, buf
);
939 static int local_remove(FsContext
*ctx
, const char *path
)
945 if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
946 buffer
= rpath(ctx
, path
);
947 err
= lstat(buffer
, &stbuf
);
953 * If directory remove .virtfs_metadata contained in the
956 if (S_ISDIR(stbuf
.st_mode
)) {
957 buffer
= g_strdup_printf("%s/%s/%s", ctx
->fs_root
,
958 path
, VIRTFS_META_DIR
);
959 err
= remove(buffer
);
961 if (err
< 0 && errno
!= ENOENT
) {
963 * We didn't had the .virtfs_metadata file. May be file created
964 * in non-mapped mode ?. Ignore ENOENT.
970 * Now remove the name from parent directory
971 * .virtfs_metadata directory
973 buffer
= local_mapped_attr_path(ctx
, path
);
974 err
= remove(buffer
);
976 if (err
< 0 && errno
!= ENOENT
) {
978 * We didn't had the .virtfs_metadata file. May be file created
979 * in non-mapped mode ?. Ignore ENOENT.
985 buffer
= rpath(ctx
, path
);
986 err
= remove(buffer
);
992 static int local_fsync(FsContext
*ctx
, int fid_type
,
993 V9fsFidOpenState
*fs
, int datasync
)
997 if (fid_type
== P9_FID_DIR
) {
1004 return qemu_fdatasync(fd
);
1010 static int local_statfs(FsContext
*s
, V9fsPath
*fs_path
, struct statfs
*stbuf
)
1014 char *path
= fs_path
->data
;
1016 buffer
= rpath(s
, path
);
1017 ret
= statfs(buffer
, stbuf
);
1022 static ssize_t
local_lgetxattr(FsContext
*ctx
, V9fsPath
*fs_path
,
1023 const char *name
, void *value
, size_t size
)
1025 char *path
= fs_path
->data
;
1027 return v9fs_get_xattr(ctx
, path
, name
, value
, size
);
1030 static ssize_t
local_llistxattr(FsContext
*ctx
, V9fsPath
*fs_path
,
1031 void *value
, size_t size
)
1033 char *path
= fs_path
->data
;
1035 return v9fs_list_xattr(ctx
, path
, value
, size
);
1038 static int local_lsetxattr(FsContext
*ctx
, V9fsPath
*fs_path
, const char *name
,
1039 void *value
, size_t size
, int flags
)
1041 char *path
= fs_path
->data
;
1043 return v9fs_set_xattr(ctx
, path
, name
, value
, size
, flags
);
1046 static int local_lremovexattr(FsContext
*ctx
, V9fsPath
*fs_path
,
1049 char *path
= fs_path
->data
;
1051 return v9fs_remove_xattr(ctx
, path
, name
);
1054 static int local_name_to_path(FsContext
*ctx
, V9fsPath
*dir_path
,
1055 const char *name
, V9fsPath
*target
)
1058 v9fs_string_sprintf((V9fsString
*)target
, "%s/%s",
1059 dir_path
->data
, name
);
1061 v9fs_string_sprintf((V9fsString
*)target
, "%s", name
);
1063 /* Bump the size for including terminating NULL */
1068 static int local_renameat(FsContext
*ctx
, V9fsPath
*olddir
,
1069 const char *old_name
, V9fsPath
*newdir
,
1070 const char *new_name
)
1073 V9fsString old_full_name
, new_full_name
;
1075 v9fs_string_init(&old_full_name
);
1076 v9fs_string_init(&new_full_name
);
1078 v9fs_string_sprintf(&old_full_name
, "%s/%s", olddir
->data
, old_name
);
1079 v9fs_string_sprintf(&new_full_name
, "%s/%s", newdir
->data
, new_name
);
1081 ret
= local_rename(ctx
, old_full_name
.data
, new_full_name
.data
);
1082 v9fs_string_free(&old_full_name
);
1083 v9fs_string_free(&new_full_name
);
1087 static int local_unlinkat(FsContext
*ctx
, V9fsPath
*dir
,
1088 const char *name
, int flags
)
1091 V9fsString fullname
;
1094 v9fs_string_init(&fullname
);
1096 v9fs_string_sprintf(&fullname
, "%s/%s", dir
->data
, name
);
1097 if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
1098 if (flags
== AT_REMOVEDIR
) {
1100 * If directory remove .virtfs_metadata contained in the
1103 buffer
= g_strdup_printf("%s/%s/%s", ctx
->fs_root
,
1104 fullname
.data
, VIRTFS_META_DIR
);
1105 ret
= remove(buffer
);
1107 if (ret
< 0 && errno
!= ENOENT
) {
1109 * We didn't had the .virtfs_metadata file. May be file created
1110 * in non-mapped mode ?. Ignore ENOENT.
1116 * Now remove the name from parent directory
1117 * .virtfs_metadata directory.
1119 buffer
= local_mapped_attr_path(ctx
, fullname
.data
);
1120 ret
= remove(buffer
);
1122 if (ret
< 0 && errno
!= ENOENT
) {
1124 * We didn't had the .virtfs_metadata file. May be file created
1125 * in non-mapped mode ?. Ignore ENOENT.
1130 /* Remove the name finally */
1131 buffer
= rpath(ctx
, fullname
.data
);
1132 ret
= remove(buffer
);
1136 v9fs_string_free(&fullname
);
1140 static int local_ioc_getversion(FsContext
*ctx
, V9fsPath
*path
,
1141 mode_t st_mode
, uint64_t *st_gen
)
1143 #ifdef FS_IOC_GETVERSION
1145 V9fsFidOpenState fid_open
;
1148 * Do not try to open special files like device nodes, fifos etc
1149 * We can get fd for regular files and directories only
1151 if (!S_ISREG(st_mode
) && !S_ISDIR(st_mode
)) {
1155 err
= local_open(ctx
, path
, O_RDONLY
, &fid_open
);
1159 err
= ioctl(fid_open
.fd
, FS_IOC_GETVERSION
, st_gen
);
1160 local_close(ctx
, &fid_open
);
1168 static int local_init(FsContext
*ctx
)
1171 struct statfs stbuf
;
1173 if (ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) {
1174 ctx
->xops
= passthrough_xattr_ops
;
1175 } else if (ctx
->export_flags
& V9FS_SM_MAPPED
) {
1176 ctx
->xops
= mapped_xattr_ops
;
1177 } else if (ctx
->export_flags
& V9FS_SM_NONE
) {
1178 ctx
->xops
= none_xattr_ops
;
1179 } else if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
1181 * xattr operation for mapped-file and passthrough
1184 ctx
->xops
= passthrough_xattr_ops
;
1186 ctx
->export_flags
|= V9FS_PATHNAME_FSCONTEXT
;
1187 #ifdef FS_IOC_GETVERSION
1189 * use ioc_getversion only if the iocl is definied
1191 err
= statfs(ctx
->fs_root
, &stbuf
);
1193 switch (stbuf
.f_type
) {
1194 case EXT2_SUPER_MAGIC
:
1195 case BTRFS_SUPER_MAGIC
:
1196 case REISERFS_SUPER_MAGIC
:
1197 case XFS_SUPER_MAGIC
:
1198 ctx
->exops
.get_st_gen
= local_ioc_getversion
;
1206 static int local_parse_opts(QemuOpts
*opts
, struct FsDriverEntry
*fse
)
1208 const char *sec_model
= qemu_opt_get(opts
, "security_model");
1209 const char *path
= qemu_opt_get(opts
, "path");
1212 fprintf(stderr
, "security model not specified, "
1213 "local fs needs security model\nvalid options are:"
1214 "\tsecurity_model=[passthrough|mapped|none]\n");
1218 if (!strcmp(sec_model
, "passthrough")) {
1219 fse
->export_flags
|= V9FS_SM_PASSTHROUGH
;
1220 } else if (!strcmp(sec_model
, "mapped") ||
1221 !strcmp(sec_model
, "mapped-xattr")) {
1222 fse
->export_flags
|= V9FS_SM_MAPPED
;
1223 } else if (!strcmp(sec_model
, "none")) {
1224 fse
->export_flags
|= V9FS_SM_NONE
;
1225 } else if (!strcmp(sec_model
, "mapped-file")) {
1226 fse
->export_flags
|= V9FS_SM_MAPPED_FILE
;
1228 fprintf(stderr
, "Invalid security model %s specified, valid options are"
1229 "\n\t [passthrough|mapped-xattr|mapped-file|none]\n",
1235 fprintf(stderr
, "fsdev: No path specified.\n");
1238 fse
->path
= g_strdup(path
);
1243 FileOperations local_ops
= {
1244 .parse_opts
= local_parse_opts
,
1246 .lstat
= local_lstat
,
1247 .readlink
= local_readlink
,
1248 .close
= local_close
,
1249 .closedir
= local_closedir
,
1251 .opendir
= local_opendir
,
1252 .rewinddir
= local_rewinddir
,
1253 .telldir
= local_telldir
,
1254 .readdir_r
= local_readdir_r
,
1255 .seekdir
= local_seekdir
,
1256 .preadv
= local_preadv
,
1257 .pwritev
= local_pwritev
,
1258 .chmod
= local_chmod
,
1259 .mknod
= local_mknod
,
1260 .mkdir
= local_mkdir
,
1261 .fstat
= local_fstat
,
1262 .open2
= local_open2
,
1263 .symlink
= local_symlink
,
1265 .truncate
= local_truncate
,
1266 .rename
= local_rename
,
1267 .chown
= local_chown
,
1268 .utimensat
= local_utimensat
,
1269 .remove
= local_remove
,
1270 .fsync
= local_fsync
,
1271 .statfs
= local_statfs
,
1272 .lgetxattr
= local_lgetxattr
,
1273 .llistxattr
= local_llistxattr
,
1274 .lsetxattr
= local_lsetxattr
,
1275 .lremovexattr
= local_lremovexattr
,
1276 .name_to_path
= local_name_to_path
,
1277 .renameat
= local_renameat
,
1278 .unlinkat
= local_unlinkat
,