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"
17 #include "fsdev/qemu-fsdev.h" /* local_ops */
18 #include <arpa/inet.h>
21 #include <sys/socket.h>
23 #include "qemu/xattr.h"
24 #include "qemu/cutils.h"
25 #include "qemu/error-report.h"
28 #ifdef CONFIG_LINUX_MAGIC_H
29 #include <linux/magic.h>
31 #include <sys/ioctl.h>
33 #ifndef XFS_SUPER_MAGIC
34 #define XFS_SUPER_MAGIC 0x58465342
36 #ifndef EXT2_SUPER_MAGIC
37 #define EXT2_SUPER_MAGIC 0xEF53
39 #ifndef REISERFS_SUPER_MAGIC
40 #define REISERFS_SUPER_MAGIC 0x52654973
42 #ifndef BTRFS_SUPER_MAGIC
43 #define BTRFS_SUPER_MAGIC 0x9123683E
46 #define VIRTFS_META_DIR ".virtfs_metadata"
48 static char *local_mapped_attr_path(FsContext
*ctx
, const char *path
)
51 const char *name
= strrchr(path
, '/');
59 return g_strdup_printf("%s/%.*s/%s/%s", ctx
->fs_root
,
60 dirlen
, path
, VIRTFS_META_DIR
, name
);
63 static FILE *local_fopen(const char *path
, const char *mode
)
67 int flags
= O_NOFOLLOW
;
69 * only supports two modes
73 } else if (mode
[0] == 'w') {
74 flags
|= O_WRONLY
| O_TRUNC
| O_CREAT
;
75 o_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
;
79 fd
= open(path
, flags
, o_mode
);
83 fp
= fdopen(fd
, mode
);
91 static void local_mapped_file_attr(FsContext
*ctx
, const char *path
,
98 attr_path
= local_mapped_attr_path(ctx
, path
);
99 fp
= local_fopen(attr_path
, "r");
104 memset(buf
, 0, ATTR_MAX
);
105 while (fgets(buf
, ATTR_MAX
, fp
)) {
106 if (!strncmp(buf
, "virtfs.uid", 10)) {
107 stbuf
->st_uid
= atoi(buf
+11);
108 } else if (!strncmp(buf
, "virtfs.gid", 10)) {
109 stbuf
->st_gid
= atoi(buf
+11);
110 } else if (!strncmp(buf
, "virtfs.mode", 11)) {
111 stbuf
->st_mode
= atoi(buf
+12);
112 } else if (!strncmp(buf
, "virtfs.rdev", 11)) {
113 stbuf
->st_rdev
= atoi(buf
+12);
115 memset(buf
, 0, ATTR_MAX
);
120 static int local_lstat(FsContext
*fs_ctx
, V9fsPath
*fs_path
, struct stat
*stbuf
)
124 char *path
= fs_path
->data
;
126 buffer
= rpath(fs_ctx
, path
);
127 err
= lstat(buffer
, stbuf
);
131 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
132 /* Actual credentials are part of extended attrs */
137 if (getxattr(buffer
, "user.virtfs.uid", &tmp_uid
, sizeof(uid_t
)) > 0) {
138 stbuf
->st_uid
= le32_to_cpu(tmp_uid
);
140 if (getxattr(buffer
, "user.virtfs.gid", &tmp_gid
, sizeof(gid_t
)) > 0) {
141 stbuf
->st_gid
= le32_to_cpu(tmp_gid
);
143 if (getxattr(buffer
, "user.virtfs.mode",
144 &tmp_mode
, sizeof(mode_t
)) > 0) {
145 stbuf
->st_mode
= le32_to_cpu(tmp_mode
);
147 if (getxattr(buffer
, "user.virtfs.rdev", &tmp_dev
, sizeof(dev_t
)) > 0) {
148 stbuf
->st_rdev
= le64_to_cpu(tmp_dev
);
150 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
151 local_mapped_file_attr(fs_ctx
, path
, stbuf
);
159 static int local_create_mapped_attr_dir(FsContext
*ctx
, const char *path
)
163 char *tmp_path
= g_strdup(path
);
165 attr_dir
= g_strdup_printf("%s/%s/%s",
166 ctx
->fs_root
, dirname(tmp_path
), VIRTFS_META_DIR
);
168 err
= mkdir(attr_dir
, 0700);
169 if (err
< 0 && errno
== EEXIST
) {
177 static int local_set_mapped_file_attr(FsContext
*ctx
,
178 const char *path
, FsCred
*credp
)
184 int uid
= -1, gid
= -1, mode
= -1, rdev
= -1;
186 attr_path
= local_mapped_attr_path(ctx
, path
);
187 fp
= local_fopen(attr_path
, "r");
189 goto create_map_file
;
191 memset(buf
, 0, ATTR_MAX
);
192 while (fgets(buf
, ATTR_MAX
, fp
)) {
193 if (!strncmp(buf
, "virtfs.uid", 10)) {
195 } else if (!strncmp(buf
, "virtfs.gid", 10)) {
197 } else if (!strncmp(buf
, "virtfs.mode", 11)) {
199 } else if (!strncmp(buf
, "virtfs.rdev", 11)) {
202 memset(buf
, 0, ATTR_MAX
);
205 goto update_map_file
;
208 ret
= local_create_mapped_attr_dir(ctx
, path
);
214 fp
= local_fopen(attr_path
, "w");
220 if (credp
->fc_uid
!= -1) {
223 if (credp
->fc_gid
!= -1) {
226 if (credp
->fc_mode
!= -1) {
227 mode
= credp
->fc_mode
;
229 if (credp
->fc_rdev
!= -1) {
230 rdev
= credp
->fc_rdev
;
235 fprintf(fp
, "virtfs.uid=%d\n", uid
);
238 fprintf(fp
, "virtfs.gid=%d\n", gid
);
241 fprintf(fp
, "virtfs.mode=%d\n", mode
);
244 fprintf(fp
, "virtfs.rdev=%d\n", rdev
);
253 static int local_set_xattr(const char *path
, FsCred
*credp
)
257 if (credp
->fc_uid
!= -1) {
258 uint32_t tmp_uid
= cpu_to_le32(credp
->fc_uid
);
259 err
= setxattr(path
, "user.virtfs.uid", &tmp_uid
, sizeof(uid_t
), 0);
264 if (credp
->fc_gid
!= -1) {
265 uint32_t tmp_gid
= cpu_to_le32(credp
->fc_gid
);
266 err
= setxattr(path
, "user.virtfs.gid", &tmp_gid
, sizeof(gid_t
), 0);
271 if (credp
->fc_mode
!= -1) {
272 uint32_t tmp_mode
= cpu_to_le32(credp
->fc_mode
);
273 err
= setxattr(path
, "user.virtfs.mode", &tmp_mode
, sizeof(mode_t
), 0);
278 if (credp
->fc_rdev
!= -1) {
279 uint64_t tmp_rdev
= cpu_to_le64(credp
->fc_rdev
);
280 err
= setxattr(path
, "user.virtfs.rdev", &tmp_rdev
, sizeof(dev_t
), 0);
288 static int local_post_create_passthrough(FsContext
*fs_ctx
, const char *path
,
293 buffer
= rpath(fs_ctx
, path
);
294 if (lchown(buffer
, credp
->fc_uid
, credp
->fc_gid
) < 0) {
296 * If we fail to change ownership and if we are
297 * using security model none. Ignore the error
299 if ((fs_ctx
->export_flags
& V9FS_SEC_MASK
) != V9FS_SM_NONE
) {
304 if (chmod(buffer
, credp
->fc_mode
& 07777) < 0) {
315 static ssize_t
local_readlink(FsContext
*fs_ctx
, V9fsPath
*fs_path
,
316 char *buf
, size_t bufsz
)
320 char *path
= fs_path
->data
;
322 if ((fs_ctx
->export_flags
& V9FS_SM_MAPPED
) ||
323 (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
)) {
325 buffer
= rpath(fs_ctx
, path
);
326 fd
= open(buffer
, O_RDONLY
| O_NOFOLLOW
);
332 tsize
= read(fd
, (void *)buf
, bufsz
);
333 } while (tsize
== -1 && errno
== EINTR
);
335 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
336 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
337 buffer
= rpath(fs_ctx
, path
);
338 tsize
= readlink(buffer
, buf
, bufsz
);
344 static int local_close(FsContext
*ctx
, V9fsFidOpenState
*fs
)
346 return close(fs
->fd
);
349 static int local_closedir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
351 return closedir(fs
->dir
.stream
);
354 static int local_open(FsContext
*ctx
, V9fsPath
*fs_path
,
355 int flags
, V9fsFidOpenState
*fs
)
358 char *path
= fs_path
->data
;
360 buffer
= rpath(ctx
, path
);
361 fs
->fd
= open(buffer
, flags
| O_NOFOLLOW
);
366 static int local_opendir(FsContext
*ctx
,
367 V9fsPath
*fs_path
, V9fsFidOpenState
*fs
)
370 char *path
= fs_path
->data
;
372 buffer
= rpath(ctx
, path
);
373 fs
->dir
.stream
= opendir(buffer
);
375 if (!fs
->dir
.stream
) {
381 static void local_rewinddir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
383 rewinddir(fs
->dir
.stream
);
386 static off_t
local_telldir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
388 return telldir(fs
->dir
.stream
);
391 static struct dirent
*local_readdir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
393 struct dirent
*entry
;
396 entry
= readdir(fs
->dir
.stream
);
401 if (ctx
->export_flags
& V9FS_SM_MAPPED
) {
402 entry
->d_type
= DT_UNKNOWN
;
403 } else if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
404 if (!strcmp(entry
->d_name
, VIRTFS_META_DIR
)) {
405 /* skp the meta data directory */
408 entry
->d_type
= DT_UNKNOWN
;
414 static void local_seekdir(FsContext
*ctx
, V9fsFidOpenState
*fs
, off_t off
)
416 seekdir(fs
->dir
.stream
, off
);
419 static ssize_t
local_preadv(FsContext
*ctx
, V9fsFidOpenState
*fs
,
420 const struct iovec
*iov
,
421 int iovcnt
, off_t offset
)
424 return preadv(fs
->fd
, iov
, iovcnt
, offset
);
426 int err
= lseek(fs
->fd
, offset
, SEEK_SET
);
430 return readv(fs
->fd
, iov
, iovcnt
);
435 static ssize_t
local_pwritev(FsContext
*ctx
, V9fsFidOpenState
*fs
,
436 const struct iovec
*iov
,
437 int iovcnt
, off_t offset
)
442 ret
= pwritev(fs
->fd
, iov
, iovcnt
, offset
);
444 int err
= lseek(fs
->fd
, offset
, SEEK_SET
);
448 ret
= writev(fs
->fd
, iov
, iovcnt
);
451 #ifdef CONFIG_SYNC_FILE_RANGE
452 if (ret
> 0 && ctx
->export_flags
& V9FS_IMMEDIATE_WRITEOUT
) {
454 * Initiate a writeback. This is not a data integrity sync.
455 * We want to ensure that we don't leave dirty pages in the cache
456 * after write when writeout=immediate is sepcified.
458 sync_file_range(fs
->fd
, offset
, ret
,
459 SYNC_FILE_RANGE_WAIT_BEFORE
| SYNC_FILE_RANGE_WRITE
);
465 static int local_chmod(FsContext
*fs_ctx
, V9fsPath
*fs_path
, FsCred
*credp
)
469 char *path
= fs_path
->data
;
471 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
472 buffer
= rpath(fs_ctx
, path
);
473 ret
= local_set_xattr(buffer
, credp
);
475 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
476 return local_set_mapped_file_attr(fs_ctx
, path
, credp
);
477 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
478 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
479 buffer
= rpath(fs_ctx
, path
);
480 ret
= chmod(buffer
, credp
->fc_mode
);
486 static int local_mknod(FsContext
*fs_ctx
, V9fsPath
*dir_path
,
487 const char *name
, FsCred
*credp
)
495 v9fs_string_init(&fullname
);
496 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
497 path
= fullname
.data
;
499 /* Determine the security model */
500 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
501 buffer
= rpath(fs_ctx
, path
);
502 err
= mknod(buffer
, SM_LOCAL_MODE_BITS
|S_IFREG
, 0);
506 err
= local_set_xattr(buffer
, credp
);
511 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
513 buffer
= rpath(fs_ctx
, path
);
514 err
= mknod(buffer
, SM_LOCAL_MODE_BITS
|S_IFREG
, 0);
518 err
= local_set_mapped_file_attr(fs_ctx
, path
, credp
);
523 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
524 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
525 buffer
= rpath(fs_ctx
, path
);
526 err
= mknod(buffer
, credp
->fc_mode
, credp
->fc_rdev
);
530 err
= local_post_create_passthrough(fs_ctx
, path
, credp
);
543 v9fs_string_free(&fullname
);
547 static int local_mkdir(FsContext
*fs_ctx
, V9fsPath
*dir_path
,
548 const char *name
, FsCred
*credp
)
556 v9fs_string_init(&fullname
);
557 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
558 path
= fullname
.data
;
560 /* Determine the security model */
561 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
562 buffer
= rpath(fs_ctx
, path
);
563 err
= mkdir(buffer
, SM_LOCAL_DIR_MODE_BITS
);
567 credp
->fc_mode
= credp
->fc_mode
|S_IFDIR
;
568 err
= local_set_xattr(buffer
, credp
);
573 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
574 buffer
= rpath(fs_ctx
, path
);
575 err
= mkdir(buffer
, SM_LOCAL_DIR_MODE_BITS
);
579 credp
->fc_mode
= credp
->fc_mode
|S_IFDIR
;
580 err
= local_set_mapped_file_attr(fs_ctx
, path
, credp
);
585 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
586 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
587 buffer
= rpath(fs_ctx
, path
);
588 err
= mkdir(buffer
, credp
->fc_mode
);
592 err
= local_post_create_passthrough(fs_ctx
, path
, credp
);
605 v9fs_string_free(&fullname
);
609 static int local_fstat(FsContext
*fs_ctx
, int fid_type
,
610 V9fsFidOpenState
*fs
, struct stat
*stbuf
)
614 if (fid_type
== P9_FID_DIR
) {
615 fd
= dirfd(fs
->dir
.stream
);
620 err
= fstat(fd
, stbuf
);
624 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
625 /* Actual credentials are part of extended attrs */
631 if (fgetxattr(fd
, "user.virtfs.uid", &tmp_uid
, sizeof(uid_t
)) > 0) {
632 stbuf
->st_uid
= le32_to_cpu(tmp_uid
);
634 if (fgetxattr(fd
, "user.virtfs.gid", &tmp_gid
, sizeof(gid_t
)) > 0) {
635 stbuf
->st_gid
= le32_to_cpu(tmp_gid
);
637 if (fgetxattr(fd
, "user.virtfs.mode", &tmp_mode
, sizeof(mode_t
)) > 0) {
638 stbuf
->st_mode
= le32_to_cpu(tmp_mode
);
640 if (fgetxattr(fd
, "user.virtfs.rdev", &tmp_dev
, sizeof(dev_t
)) > 0) {
641 stbuf
->st_rdev
= le64_to_cpu(tmp_dev
);
643 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
650 static int local_open2(FsContext
*fs_ctx
, V9fsPath
*dir_path
, const char *name
,
651 int flags
, FsCred
*credp
, V9fsFidOpenState
*fs
)
661 * Mark all the open to not follow symlinks
665 v9fs_string_init(&fullname
);
666 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
667 path
= fullname
.data
;
669 /* Determine the security model */
670 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
671 buffer
= rpath(fs_ctx
, path
);
672 fd
= open(buffer
, flags
, SM_LOCAL_MODE_BITS
);
677 credp
->fc_mode
= credp
->fc_mode
|S_IFREG
;
678 /* Set cleint credentials in xattr */
679 err
= local_set_xattr(buffer
, credp
);
684 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
685 buffer
= rpath(fs_ctx
, path
);
686 fd
= open(buffer
, flags
, SM_LOCAL_MODE_BITS
);
691 credp
->fc_mode
= credp
->fc_mode
|S_IFREG
;
692 /* Set client credentials in .virtfs_metadata directory files */
693 err
= local_set_mapped_file_attr(fs_ctx
, path
, credp
);
698 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
699 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
700 buffer
= rpath(fs_ctx
, path
);
701 fd
= open(buffer
, flags
, credp
->fc_mode
);
706 err
= local_post_create_passthrough(fs_ctx
, path
, credp
);
722 v9fs_string_free(&fullname
);
727 static int local_symlink(FsContext
*fs_ctx
, const char *oldpath
,
728 V9fsPath
*dir_path
, const char *name
, FsCred
*credp
)
736 v9fs_string_init(&fullname
);
737 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
738 newpath
= fullname
.data
;
740 /* Determine the security model */
741 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
743 ssize_t oldpath_size
, write_size
;
744 buffer
= rpath(fs_ctx
, newpath
);
745 fd
= open(buffer
, O_CREAT
|O_EXCL
|O_RDWR
|O_NOFOLLOW
, SM_LOCAL_MODE_BITS
);
750 /* Write the oldpath (target) to the file. */
751 oldpath_size
= strlen(oldpath
);
753 write_size
= write(fd
, (void *)oldpath
, oldpath_size
);
754 } while (write_size
== -1 && errno
== EINTR
);
756 if (write_size
!= oldpath_size
) {
763 /* Set cleint credentials in symlink's xattr */
764 credp
->fc_mode
= credp
->fc_mode
|S_IFLNK
;
765 err
= local_set_xattr(buffer
, credp
);
770 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
772 ssize_t oldpath_size
, write_size
;
773 buffer
= rpath(fs_ctx
, newpath
);
774 fd
= open(buffer
, O_CREAT
|O_EXCL
|O_RDWR
|O_NOFOLLOW
, SM_LOCAL_MODE_BITS
);
779 /* Write the oldpath (target) to the file. */
780 oldpath_size
= strlen(oldpath
);
782 write_size
= write(fd
, (void *)oldpath
, oldpath_size
);
783 } while (write_size
== -1 && errno
== EINTR
);
785 if (write_size
!= oldpath_size
) {
792 /* Set cleint credentials in symlink's xattr */
793 credp
->fc_mode
= credp
->fc_mode
|S_IFLNK
;
794 err
= local_set_mapped_file_attr(fs_ctx
, newpath
, credp
);
799 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
800 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
801 buffer
= rpath(fs_ctx
, newpath
);
802 err
= symlink(oldpath
, buffer
);
806 err
= lchown(buffer
, credp
->fc_uid
, credp
->fc_gid
);
809 * If we fail to change ownership and if we are
810 * using security model none. Ignore the error
812 if ((fs_ctx
->export_flags
& V9FS_SEC_MASK
) != V9FS_SM_NONE
) {
826 v9fs_string_free(&fullname
);
830 static int local_link(FsContext
*ctx
, V9fsPath
*oldpath
,
831 V9fsPath
*dirpath
, const char *name
)
835 char *buffer
, *buffer1
;
837 v9fs_string_init(&newpath
);
838 v9fs_string_sprintf(&newpath
, "%s/%s", dirpath
->data
, name
);
840 buffer
= rpath(ctx
, oldpath
->data
);
841 buffer1
= rpath(ctx
, newpath
.data
);
842 ret
= link(buffer
, buffer1
);
846 /* now link the virtfs_metadata files */
847 if (!ret
&& (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
)) {
848 /* Link the .virtfs_metadata files. Create the metada directory */
849 ret
= local_create_mapped_attr_dir(ctx
, newpath
.data
);
853 buffer
= local_mapped_attr_path(ctx
, oldpath
->data
);
854 buffer1
= local_mapped_attr_path(ctx
, newpath
.data
);
855 ret
= link(buffer
, buffer1
);
858 if (ret
< 0 && errno
!= ENOENT
) {
863 v9fs_string_free(&newpath
);
867 static int local_truncate(FsContext
*ctx
, V9fsPath
*fs_path
, off_t size
)
871 char *path
= fs_path
->data
;
873 buffer
= rpath(ctx
, path
);
874 ret
= truncate(buffer
, size
);
879 static int local_rename(FsContext
*ctx
, const char *oldpath
,
883 char *buffer
, *buffer1
;
885 if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
886 err
= local_create_mapped_attr_dir(ctx
, newpath
);
890 /* rename the .virtfs_metadata files */
891 buffer
= local_mapped_attr_path(ctx
, oldpath
);
892 buffer1
= local_mapped_attr_path(ctx
, newpath
);
893 err
= rename(buffer
, buffer1
);
896 if (err
< 0 && errno
!= ENOENT
) {
901 buffer
= rpath(ctx
, oldpath
);
902 buffer1
= rpath(ctx
, newpath
);
903 err
= rename(buffer
, buffer1
);
909 static int local_chown(FsContext
*fs_ctx
, V9fsPath
*fs_path
, FsCred
*credp
)
913 char *path
= fs_path
->data
;
915 if ((credp
->fc_uid
== -1 && credp
->fc_gid
== -1) ||
916 (fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
917 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
918 buffer
= rpath(fs_ctx
, path
);
919 ret
= lchown(buffer
, credp
->fc_uid
, credp
->fc_gid
);
921 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
922 buffer
= rpath(fs_ctx
, path
);
923 ret
= local_set_xattr(buffer
, credp
);
925 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
926 return local_set_mapped_file_attr(fs_ctx
, path
, credp
);
931 static int local_utimensat(FsContext
*s
, V9fsPath
*fs_path
,
932 const struct timespec
*buf
)
936 char *path
= fs_path
->data
;
938 buffer
= rpath(s
, path
);
939 ret
= qemu_utimens(buffer
, buf
);
944 static int local_remove(FsContext
*ctx
, const char *path
)
950 if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
951 buffer
= rpath(ctx
, path
);
952 err
= lstat(buffer
, &stbuf
);
958 * If directory remove .virtfs_metadata contained in the
961 if (S_ISDIR(stbuf
.st_mode
)) {
962 buffer
= g_strdup_printf("%s/%s/%s", ctx
->fs_root
,
963 path
, VIRTFS_META_DIR
);
964 err
= remove(buffer
);
966 if (err
< 0 && errno
!= ENOENT
) {
968 * We didn't had the .virtfs_metadata file. May be file created
969 * in non-mapped mode ?. Ignore ENOENT.
975 * Now remove the name from parent directory
976 * .virtfs_metadata directory
978 buffer
= local_mapped_attr_path(ctx
, path
);
979 err
= remove(buffer
);
981 if (err
< 0 && errno
!= ENOENT
) {
983 * We didn't had the .virtfs_metadata file. May be file created
984 * in non-mapped mode ?. Ignore ENOENT.
990 buffer
= rpath(ctx
, path
);
991 err
= remove(buffer
);
997 static int local_fsync(FsContext
*ctx
, int fid_type
,
998 V9fsFidOpenState
*fs
, int datasync
)
1002 if (fid_type
== P9_FID_DIR
) {
1003 fd
= dirfd(fs
->dir
.stream
);
1009 return qemu_fdatasync(fd
);
1015 static int local_statfs(FsContext
*s
, V9fsPath
*fs_path
, struct statfs
*stbuf
)
1019 char *path
= fs_path
->data
;
1021 buffer
= rpath(s
, path
);
1022 ret
= statfs(buffer
, stbuf
);
1027 static ssize_t
local_lgetxattr(FsContext
*ctx
, V9fsPath
*fs_path
,
1028 const char *name
, void *value
, size_t size
)
1030 char *path
= fs_path
->data
;
1032 return v9fs_get_xattr(ctx
, path
, name
, value
, size
);
1035 static ssize_t
local_llistxattr(FsContext
*ctx
, V9fsPath
*fs_path
,
1036 void *value
, size_t size
)
1038 char *path
= fs_path
->data
;
1040 return v9fs_list_xattr(ctx
, path
, value
, size
);
1043 static int local_lsetxattr(FsContext
*ctx
, V9fsPath
*fs_path
, const char *name
,
1044 void *value
, size_t size
, int flags
)
1046 char *path
= fs_path
->data
;
1048 return v9fs_set_xattr(ctx
, path
, name
, value
, size
, flags
);
1051 static int local_lremovexattr(FsContext
*ctx
, V9fsPath
*fs_path
,
1054 char *path
= fs_path
->data
;
1056 return v9fs_remove_xattr(ctx
, path
, name
);
1059 static int local_name_to_path(FsContext
*ctx
, V9fsPath
*dir_path
,
1060 const char *name
, V9fsPath
*target
)
1063 v9fs_string_sprintf((V9fsString
*)target
, "%s/%s",
1064 dir_path
->data
, name
);
1066 v9fs_string_sprintf((V9fsString
*)target
, "%s", name
);
1068 /* Bump the size for including terminating NULL */
1073 static int local_renameat(FsContext
*ctx
, V9fsPath
*olddir
,
1074 const char *old_name
, V9fsPath
*newdir
,
1075 const char *new_name
)
1078 V9fsString old_full_name
, new_full_name
;
1080 v9fs_string_init(&old_full_name
);
1081 v9fs_string_init(&new_full_name
);
1083 v9fs_string_sprintf(&old_full_name
, "%s/%s", olddir
->data
, old_name
);
1084 v9fs_string_sprintf(&new_full_name
, "%s/%s", newdir
->data
, new_name
);
1086 ret
= local_rename(ctx
, old_full_name
.data
, new_full_name
.data
);
1087 v9fs_string_free(&old_full_name
);
1088 v9fs_string_free(&new_full_name
);
1092 static int local_unlinkat(FsContext
*ctx
, V9fsPath
*dir
,
1093 const char *name
, int flags
)
1096 V9fsString fullname
;
1099 v9fs_string_init(&fullname
);
1101 v9fs_string_sprintf(&fullname
, "%s/%s", dir
->data
, name
);
1102 if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
1103 if (flags
== AT_REMOVEDIR
) {
1105 * If directory remove .virtfs_metadata contained in the
1108 buffer
= g_strdup_printf("%s/%s/%s", ctx
->fs_root
,
1109 fullname
.data
, VIRTFS_META_DIR
);
1110 ret
= remove(buffer
);
1112 if (ret
< 0 && errno
!= ENOENT
) {
1114 * We didn't had the .virtfs_metadata file. May be file created
1115 * in non-mapped mode ?. Ignore ENOENT.
1121 * Now remove the name from parent directory
1122 * .virtfs_metadata directory.
1124 buffer
= local_mapped_attr_path(ctx
, fullname
.data
);
1125 ret
= remove(buffer
);
1127 if (ret
< 0 && errno
!= ENOENT
) {
1129 * We didn't had the .virtfs_metadata file. May be file created
1130 * in non-mapped mode ?. Ignore ENOENT.
1135 /* Remove the name finally */
1136 buffer
= rpath(ctx
, fullname
.data
);
1137 ret
= remove(buffer
);
1141 v9fs_string_free(&fullname
);
1145 static int local_ioc_getversion(FsContext
*ctx
, V9fsPath
*path
,
1146 mode_t st_mode
, uint64_t *st_gen
)
1148 #ifdef FS_IOC_GETVERSION
1150 V9fsFidOpenState fid_open
;
1153 * Do not try to open special files like device nodes, fifos etc
1154 * We can get fd for regular files and directories only
1156 if (!S_ISREG(st_mode
) && !S_ISDIR(st_mode
)) {
1160 err
= local_open(ctx
, path
, O_RDONLY
, &fid_open
);
1164 err
= ioctl(fid_open
.fd
, FS_IOC_GETVERSION
, st_gen
);
1165 local_close(ctx
, &fid_open
);
1173 static int local_init(FsContext
*ctx
)
1176 struct statfs stbuf
;
1178 if (ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) {
1179 ctx
->xops
= passthrough_xattr_ops
;
1180 } else if (ctx
->export_flags
& V9FS_SM_MAPPED
) {
1181 ctx
->xops
= mapped_xattr_ops
;
1182 } else if (ctx
->export_flags
& V9FS_SM_NONE
) {
1183 ctx
->xops
= none_xattr_ops
;
1184 } else if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
1186 * xattr operation for mapped-file and passthrough
1189 ctx
->xops
= passthrough_xattr_ops
;
1191 ctx
->export_flags
|= V9FS_PATHNAME_FSCONTEXT
;
1192 #ifdef FS_IOC_GETVERSION
1194 * use ioc_getversion only if the iocl is definied
1196 err
= statfs(ctx
->fs_root
, &stbuf
);
1198 switch (stbuf
.f_type
) {
1199 case EXT2_SUPER_MAGIC
:
1200 case BTRFS_SUPER_MAGIC
:
1201 case REISERFS_SUPER_MAGIC
:
1202 case XFS_SUPER_MAGIC
:
1203 ctx
->exops
.get_st_gen
= local_ioc_getversion
;
1211 static int local_parse_opts(QemuOpts
*opts
, struct FsDriverEntry
*fse
)
1213 const char *sec_model
= qemu_opt_get(opts
, "security_model");
1214 const char *path
= qemu_opt_get(opts
, "path");
1217 error_report("Security model not specified, local fs needs security model");
1218 error_printf("valid options are:"
1219 "\tsecurity_model=[passthrough|mapped-xattr|mapped-file|none]\n");
1223 if (!strcmp(sec_model
, "passthrough")) {
1224 fse
->export_flags
|= V9FS_SM_PASSTHROUGH
;
1225 } else if (!strcmp(sec_model
, "mapped") ||
1226 !strcmp(sec_model
, "mapped-xattr")) {
1227 fse
->export_flags
|= V9FS_SM_MAPPED
;
1228 } else if (!strcmp(sec_model
, "none")) {
1229 fse
->export_flags
|= V9FS_SM_NONE
;
1230 } else if (!strcmp(sec_model
, "mapped-file")) {
1231 fse
->export_flags
|= V9FS_SM_MAPPED_FILE
;
1233 error_report("Invalid security model %s specified", sec_model
);
1234 error_printf("valid options are:"
1235 "\t[passthrough|mapped-xattr|mapped-file|none]\n");
1240 error_report("fsdev: No path specified");
1243 fse
->path
= g_strdup(path
);
1248 FileOperations local_ops
= {
1249 .parse_opts
= local_parse_opts
,
1251 .lstat
= local_lstat
,
1252 .readlink
= local_readlink
,
1253 .close
= local_close
,
1254 .closedir
= local_closedir
,
1256 .opendir
= local_opendir
,
1257 .rewinddir
= local_rewinddir
,
1258 .telldir
= local_telldir
,
1259 .readdir
= local_readdir
,
1260 .seekdir
= local_seekdir
,
1261 .preadv
= local_preadv
,
1262 .pwritev
= local_pwritev
,
1263 .chmod
= local_chmod
,
1264 .mknod
= local_mknod
,
1265 .mkdir
= local_mkdir
,
1266 .fstat
= local_fstat
,
1267 .open2
= local_open2
,
1268 .symlink
= local_symlink
,
1270 .truncate
= local_truncate
,
1271 .rename
= local_rename
,
1272 .chown
= local_chown
,
1273 .utimensat
= local_utimensat
,
1274 .remove
= local_remove
,
1275 .fsync
= local_fsync
,
1276 .statfs
= local_statfs
,
1277 .lgetxattr
= local_lgetxattr
,
1278 .llistxattr
= local_llistxattr
,
1279 .lsetxattr
= local_lsetxattr
,
1280 .lremovexattr
= local_lremovexattr
,
1281 .name_to_path
= local_name_to_path
,
1282 .renameat
= local_renameat
,
1283 .unlinkat
= local_unlinkat
,