2 * Virtio 9p Posix callback
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 "hw/virtio/virtio.h"
15 #include "virtio-9p.h"
16 #include "virtio-9p-xattr.h"
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 char *tmp_path
= g_strdup(path
);
49 char *base_name
= basename(tmp_path
);
52 /* NULL terminate the directory */
54 *(base_name
- 1) = '\0';
56 buffer
= g_strdup_printf("%s/%s/%s/%s",
57 ctx
->fs_root
, dir_name
, VIRTFS_META_DIR
, base_name
);
62 static FILE *local_fopen(const char *path
, const char *mode
)
66 int flags
= O_NOFOLLOW
;
68 * only supports two modes
72 } else if (mode
[0] == 'w') {
73 flags
|= O_WRONLY
| O_TRUNC
| O_CREAT
;
74 o_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
;
78 fd
= open(path
, flags
, o_mode
);
82 fp
= fdopen(fd
, mode
);
90 static void local_mapped_file_attr(FsContext
*ctx
, const char *path
,
97 attr_path
= local_mapped_attr_path(ctx
, path
);
98 fp
= local_fopen(attr_path
, "r");
103 memset(buf
, 0, ATTR_MAX
);
104 while (fgets(buf
, ATTR_MAX
, fp
)) {
105 if (!strncmp(buf
, "virtfs.uid", 10)) {
106 stbuf
->st_uid
= atoi(buf
+11);
107 } else if (!strncmp(buf
, "virtfs.gid", 10)) {
108 stbuf
->st_gid
= atoi(buf
+11);
109 } else if (!strncmp(buf
, "virtfs.mode", 11)) {
110 stbuf
->st_mode
= atoi(buf
+12);
111 } else if (!strncmp(buf
, "virtfs.rdev", 11)) {
112 stbuf
->st_rdev
= atoi(buf
+12);
114 memset(buf
, 0, ATTR_MAX
);
119 static int local_lstat(FsContext
*fs_ctx
, V9fsPath
*fs_path
, struct stat
*stbuf
)
123 char *path
= fs_path
->data
;
125 buffer
= rpath(fs_ctx
, path
);
126 err
= lstat(buffer
, stbuf
);
130 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
131 /* Actual credentials are part of extended attrs */
136 if (getxattr(buffer
, "user.virtfs.uid", &tmp_uid
, sizeof(uid_t
)) > 0) {
137 stbuf
->st_uid
= tmp_uid
;
139 if (getxattr(buffer
, "user.virtfs.gid", &tmp_gid
, sizeof(gid_t
)) > 0) {
140 stbuf
->st_gid
= tmp_gid
;
142 if (getxattr(buffer
, "user.virtfs.mode",
143 &tmp_mode
, sizeof(mode_t
)) > 0) {
144 stbuf
->st_mode
= tmp_mode
;
146 if (getxattr(buffer
, "user.virtfs.rdev", &tmp_dev
, sizeof(dev_t
)) > 0) {
147 stbuf
->st_rdev
= tmp_dev
;
149 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
150 local_mapped_file_attr(fs_ctx
, path
, stbuf
);
158 static int local_create_mapped_attr_dir(FsContext
*ctx
, const char *path
)
162 char *tmp_path
= g_strdup(path
);
164 attr_dir
= g_strdup_printf("%s/%s/%s",
165 ctx
->fs_root
, dirname(tmp_path
), VIRTFS_META_DIR
);
167 err
= mkdir(attr_dir
, 0700);
168 if (err
< 0 && errno
== EEXIST
) {
176 static int local_set_mapped_file_attr(FsContext
*ctx
,
177 const char *path
, FsCred
*credp
)
183 int uid
= -1, gid
= -1, mode
= -1, rdev
= -1;
185 attr_path
= local_mapped_attr_path(ctx
, path
);
186 fp
= local_fopen(attr_path
, "r");
188 goto create_map_file
;
190 memset(buf
, 0, ATTR_MAX
);
191 while (fgets(buf
, ATTR_MAX
, fp
)) {
192 if (!strncmp(buf
, "virtfs.uid", 10)) {
194 } else if (!strncmp(buf
, "virtfs.gid", 10)) {
196 } else if (!strncmp(buf
, "virtfs.mode", 11)) {
198 } else if (!strncmp(buf
, "virtfs.rdev", 11)) {
201 memset(buf
, 0, ATTR_MAX
);
204 goto update_map_file
;
207 ret
= local_create_mapped_attr_dir(ctx
, path
);
213 fp
= local_fopen(attr_path
, "w");
219 if (credp
->fc_uid
!= -1) {
222 if (credp
->fc_gid
!= -1) {
225 if (credp
->fc_mode
!= -1) {
226 mode
= credp
->fc_mode
;
228 if (credp
->fc_rdev
!= -1) {
229 rdev
= credp
->fc_rdev
;
234 fprintf(fp
, "virtfs.uid=%d\n", uid
);
237 fprintf(fp
, "virtfs.gid=%d\n", gid
);
240 fprintf(fp
, "virtfs.mode=%d\n", mode
);
243 fprintf(fp
, "virtfs.rdev=%d\n", rdev
);
252 static int local_set_xattr(const char *path
, FsCred
*credp
)
256 if (credp
->fc_uid
!= -1) {
257 err
= setxattr(path
, "user.virtfs.uid", &credp
->fc_uid
, sizeof(uid_t
),
263 if (credp
->fc_gid
!= -1) {
264 err
= setxattr(path
, "user.virtfs.gid", &credp
->fc_gid
, sizeof(gid_t
),
270 if (credp
->fc_mode
!= -1) {
271 err
= setxattr(path
, "user.virtfs.mode", &credp
->fc_mode
,
277 if (credp
->fc_rdev
!= -1) {
278 err
= setxattr(path
, "user.virtfs.rdev", &credp
->fc_rdev
,
287 static int local_post_create_passthrough(FsContext
*fs_ctx
, const char *path
,
292 buffer
= rpath(fs_ctx
, path
);
293 if (lchown(buffer
, credp
->fc_uid
, credp
->fc_gid
) < 0) {
295 * If we fail to change ownership and if we are
296 * using security model none. Ignore the error
298 if ((fs_ctx
->export_flags
& V9FS_SEC_MASK
) != V9FS_SM_NONE
) {
303 if (chmod(buffer
, credp
->fc_mode
& 07777) < 0) {
314 static ssize_t
local_readlink(FsContext
*fs_ctx
, V9fsPath
*fs_path
,
315 char *buf
, size_t bufsz
)
319 char *path
= fs_path
->data
;
321 if ((fs_ctx
->export_flags
& V9FS_SM_MAPPED
) ||
322 (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
)) {
324 buffer
= rpath(fs_ctx
, path
);
325 fd
= open(buffer
, O_RDONLY
| O_NOFOLLOW
);
331 tsize
= read(fd
, (void *)buf
, bufsz
);
332 } 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
);
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
= opendir(buffer
);
381 static void local_rewinddir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
383 return rewinddir(fs
->dir
);
386 static off_t
local_telldir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
388 return telldir(fs
->dir
);
391 static int local_readdir_r(FsContext
*ctx
, V9fsFidOpenState
*fs
,
392 struct dirent
*entry
,
393 struct dirent
**result
)
398 ret
= readdir_r(fs
->dir
, entry
, result
);
399 if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
400 if (!ret
&& *result
!= NULL
&&
401 !strcmp(entry
->d_name
, VIRTFS_META_DIR
)) {
402 /* skp the meta data directory */
409 static void local_seekdir(FsContext
*ctx
, V9fsFidOpenState
*fs
, off_t off
)
411 return 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);
502 err
= local_set_xattr(buffer
, credp
);
507 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
509 buffer
= rpath(fs_ctx
, path
);
510 err
= mknod(buffer
, SM_LOCAL_MODE_BITS
|S_IFREG
, 0);
515 err
= local_set_mapped_file_attr(fs_ctx
, path
, credp
);
520 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
521 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
522 buffer
= rpath(fs_ctx
, path
);
523 err
= mknod(buffer
, credp
->fc_mode
, credp
->fc_rdev
);
528 err
= local_post_create_passthrough(fs_ctx
, path
, credp
);
541 v9fs_string_free(&fullname
);
545 static int local_mkdir(FsContext
*fs_ctx
, V9fsPath
*dir_path
,
546 const char *name
, FsCred
*credp
)
554 v9fs_string_init(&fullname
);
555 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
556 path
= fullname
.data
;
558 /* Determine the security model */
559 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
560 buffer
= rpath(fs_ctx
, path
);
561 err
= mkdir(buffer
, SM_LOCAL_DIR_MODE_BITS
);
566 credp
->fc_mode
= credp
->fc_mode
|S_IFDIR
;
567 err
= local_set_xattr(buffer
, credp
);
572 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
573 buffer
= rpath(fs_ctx
, path
);
574 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
);
593 err
= local_post_create_passthrough(fs_ctx
, path
, credp
);
606 v9fs_string_free(&fullname
);
610 static int local_fstat(FsContext
*fs_ctx
, int fid_type
,
611 V9fsFidOpenState
*fs
, struct stat
*stbuf
)
615 if (fid_type
== P9_FID_DIR
) {
621 err
= fstat(fd
, stbuf
);
625 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
626 /* Actual credentials are part of extended attrs */
632 if (fgetxattr(fd
, "user.virtfs.uid",
633 &tmp_uid
, sizeof(uid_t
)) > 0) {
634 stbuf
->st_uid
= tmp_uid
;
636 if (fgetxattr(fd
, "user.virtfs.gid",
637 &tmp_gid
, sizeof(gid_t
)) > 0) {
638 stbuf
->st_gid
= tmp_gid
;
640 if (fgetxattr(fd
, "user.virtfs.mode",
641 &tmp_mode
, sizeof(mode_t
)) > 0) {
642 stbuf
->st_mode
= tmp_mode
;
644 if (fgetxattr(fd
, "user.virtfs.rdev",
645 &tmp_dev
, sizeof(dev_t
)) > 0) {
646 stbuf
->st_rdev
= tmp_dev
;
648 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
655 static int local_open2(FsContext
*fs_ctx
, V9fsPath
*dir_path
, const char *name
,
656 int flags
, FsCred
*credp
, V9fsFidOpenState
*fs
)
666 * Mark all the open to not follow symlinks
670 v9fs_string_init(&fullname
);
671 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
672 path
= fullname
.data
;
674 /* Determine the security model */
675 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
676 buffer
= rpath(fs_ctx
, path
);
677 fd
= open(buffer
, flags
, SM_LOCAL_MODE_BITS
);
683 credp
->fc_mode
= credp
->fc_mode
|S_IFREG
;
684 /* Set cleint credentials in xattr */
685 err
= local_set_xattr(buffer
, credp
);
690 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
691 buffer
= rpath(fs_ctx
, path
);
692 fd
= open(buffer
, flags
, SM_LOCAL_MODE_BITS
);
698 credp
->fc_mode
= credp
->fc_mode
|S_IFREG
;
699 /* Set client credentials in .virtfs_metadata directory files */
700 err
= local_set_mapped_file_attr(fs_ctx
, path
, credp
);
705 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
706 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
707 buffer
= rpath(fs_ctx
, path
);
708 fd
= open(buffer
, flags
, credp
->fc_mode
);
714 err
= local_post_create_passthrough(fs_ctx
, path
, credp
);
730 v9fs_string_free(&fullname
);
735 static int local_symlink(FsContext
*fs_ctx
, const char *oldpath
,
736 V9fsPath
*dir_path
, const char *name
, FsCred
*credp
)
744 v9fs_string_init(&fullname
);
745 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
746 newpath
= fullname
.data
;
748 /* Determine the security model */
749 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
751 ssize_t oldpath_size
, write_size
;
752 buffer
= rpath(fs_ctx
, newpath
);
753 fd
= open(buffer
, O_CREAT
|O_EXCL
|O_RDWR
|O_NOFOLLOW
, SM_LOCAL_MODE_BITS
);
759 /* Write the oldpath (target) to the file. */
760 oldpath_size
= strlen(oldpath
);
762 write_size
= write(fd
, (void *)oldpath
, oldpath_size
);
763 } while (write_size
== -1 && errno
== EINTR
);
765 if (write_size
!= oldpath_size
) {
772 /* Set cleint credentials in symlink's xattr */
773 credp
->fc_mode
= credp
->fc_mode
|S_IFLNK
;
774 err
= local_set_xattr(buffer
, credp
);
779 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
781 ssize_t oldpath_size
, write_size
;
782 buffer
= rpath(fs_ctx
, newpath
);
783 fd
= open(buffer
, O_CREAT
|O_EXCL
|O_RDWR
|O_NOFOLLOW
, SM_LOCAL_MODE_BITS
);
789 /* Write the oldpath (target) to the file. */
790 oldpath_size
= strlen(oldpath
);
792 write_size
= write(fd
, (void *)oldpath
, oldpath_size
);
793 } while (write_size
== -1 && errno
== EINTR
);
795 if (write_size
!= oldpath_size
) {
802 /* Set cleint credentials in symlink's xattr */
803 credp
->fc_mode
= credp
->fc_mode
|S_IFLNK
;
804 err
= local_set_mapped_file_attr(fs_ctx
, newpath
, credp
);
809 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
810 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
811 buffer
= rpath(fs_ctx
, newpath
);
812 err
= symlink(oldpath
, buffer
);
817 err
= lchown(buffer
, credp
->fc_uid
, credp
->fc_gid
);
820 * If we fail to change ownership and if we are
821 * using security model none. Ignore the error
823 if ((fs_ctx
->export_flags
& V9FS_SEC_MASK
) != V9FS_SM_NONE
) {
837 v9fs_string_free(&fullname
);
841 static int local_link(FsContext
*ctx
, V9fsPath
*oldpath
,
842 V9fsPath
*dirpath
, const char *name
)
846 char *buffer
, *buffer1
;
848 v9fs_string_init(&newpath
);
849 v9fs_string_sprintf(&newpath
, "%s/%s", dirpath
->data
, name
);
851 buffer
= rpath(ctx
, oldpath
->data
);
852 buffer1
= rpath(ctx
, newpath
.data
);
853 ret
= link(buffer
, buffer1
);
857 /* now link the virtfs_metadata files */
858 if (!ret
&& (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
)) {
859 /* Link the .virtfs_metadata files. Create the metada directory */
860 ret
= local_create_mapped_attr_dir(ctx
, newpath
.data
);
864 buffer
= local_mapped_attr_path(ctx
, oldpath
->data
);
865 buffer1
= local_mapped_attr_path(ctx
, newpath
.data
);
866 ret
= link(buffer
, buffer1
);
869 if (ret
< 0 && errno
!= ENOENT
) {
874 v9fs_string_free(&newpath
);
878 static int local_truncate(FsContext
*ctx
, V9fsPath
*fs_path
, off_t size
)
882 char *path
= fs_path
->data
;
884 buffer
= rpath(ctx
, path
);
885 ret
= truncate(buffer
, size
);
890 static int local_rename(FsContext
*ctx
, const char *oldpath
,
894 char *buffer
, *buffer1
;
896 if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
897 err
= local_create_mapped_attr_dir(ctx
, newpath
);
901 /* rename the .virtfs_metadata files */
902 buffer
= local_mapped_attr_path(ctx
, oldpath
);
903 buffer1
= local_mapped_attr_path(ctx
, newpath
);
904 err
= rename(buffer
, buffer1
);
907 if (err
< 0 && errno
!= ENOENT
) {
912 buffer
= rpath(ctx
, oldpath
);
913 buffer1
= rpath(ctx
, newpath
);
914 err
= rename(buffer
, buffer1
);
920 static int local_chown(FsContext
*fs_ctx
, V9fsPath
*fs_path
, FsCred
*credp
)
924 char *path
= fs_path
->data
;
926 if ((credp
->fc_uid
== -1 && credp
->fc_gid
== -1) ||
927 (fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
928 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
929 buffer
= rpath(fs_ctx
, path
);
930 ret
= lchown(buffer
, credp
->fc_uid
, credp
->fc_gid
);
932 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
933 buffer
= rpath(fs_ctx
, path
);
934 ret
= local_set_xattr(buffer
, credp
);
936 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
937 return local_set_mapped_file_attr(fs_ctx
, path
, credp
);
942 static int local_utimensat(FsContext
*s
, V9fsPath
*fs_path
,
943 const struct timespec
*buf
)
947 char *path
= fs_path
->data
;
949 buffer
= rpath(s
, path
);
950 ret
= qemu_utimens(buffer
, buf
);
955 static int local_remove(FsContext
*ctx
, const char *path
)
961 if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
962 buffer
= rpath(ctx
, path
);
963 err
= lstat(buffer
, &stbuf
);
969 * If directory remove .virtfs_metadata contained in the
972 if (S_ISDIR(stbuf
.st_mode
)) {
973 buffer
= g_strdup_printf("%s/%s/%s", ctx
->fs_root
,
974 path
, VIRTFS_META_DIR
);
975 err
= remove(buffer
);
977 if (err
< 0 && errno
!= ENOENT
) {
979 * We didn't had the .virtfs_metadata file. May be file created
980 * in non-mapped mode ?. Ignore ENOENT.
986 * Now remove the name from parent directory
987 * .virtfs_metadata directory
989 buffer
= local_mapped_attr_path(ctx
, path
);
990 err
= remove(buffer
);
992 if (err
< 0 && errno
!= ENOENT
) {
994 * We didn't had the .virtfs_metadata file. May be file created
995 * in non-mapped mode ?. Ignore ENOENT.
1001 buffer
= rpath(ctx
, path
);
1002 err
= remove(buffer
);
1008 static int local_fsync(FsContext
*ctx
, int fid_type
,
1009 V9fsFidOpenState
*fs
, int datasync
)
1013 if (fid_type
== P9_FID_DIR
) {
1014 fd
= dirfd(fs
->dir
);
1020 return qemu_fdatasync(fd
);
1026 static int local_statfs(FsContext
*s
, V9fsPath
*fs_path
, struct statfs
*stbuf
)
1030 char *path
= fs_path
->data
;
1032 buffer
= rpath(s
, path
);
1033 ret
= statfs(buffer
, stbuf
);
1038 static ssize_t
local_lgetxattr(FsContext
*ctx
, V9fsPath
*fs_path
,
1039 const char *name
, void *value
, size_t size
)
1041 char *path
= fs_path
->data
;
1043 return v9fs_get_xattr(ctx
, path
, name
, value
, size
);
1046 static ssize_t
local_llistxattr(FsContext
*ctx
, V9fsPath
*fs_path
,
1047 void *value
, size_t size
)
1049 char *path
= fs_path
->data
;
1051 return v9fs_list_xattr(ctx
, path
, value
, size
);
1054 static int local_lsetxattr(FsContext
*ctx
, V9fsPath
*fs_path
, const char *name
,
1055 void *value
, size_t size
, int flags
)
1057 char *path
= fs_path
->data
;
1059 return v9fs_set_xattr(ctx
, path
, name
, value
, size
, flags
);
1062 static int local_lremovexattr(FsContext
*ctx
, V9fsPath
*fs_path
,
1065 char *path
= fs_path
->data
;
1067 return v9fs_remove_xattr(ctx
, path
, name
);
1070 static int local_name_to_path(FsContext
*ctx
, V9fsPath
*dir_path
,
1071 const char *name
, V9fsPath
*target
)
1074 v9fs_string_sprintf((V9fsString
*)target
, "%s/%s",
1075 dir_path
->data
, name
);
1077 v9fs_string_sprintf((V9fsString
*)target
, "%s", name
);
1079 /* Bump the size for including terminating NULL */
1084 static int local_renameat(FsContext
*ctx
, V9fsPath
*olddir
,
1085 const char *old_name
, V9fsPath
*newdir
,
1086 const char *new_name
)
1089 V9fsString old_full_name
, new_full_name
;
1091 v9fs_string_init(&old_full_name
);
1092 v9fs_string_init(&new_full_name
);
1094 v9fs_string_sprintf(&old_full_name
, "%s/%s", olddir
->data
, old_name
);
1095 v9fs_string_sprintf(&new_full_name
, "%s/%s", newdir
->data
, new_name
);
1097 ret
= local_rename(ctx
, old_full_name
.data
, new_full_name
.data
);
1098 v9fs_string_free(&old_full_name
);
1099 v9fs_string_free(&new_full_name
);
1103 static int local_unlinkat(FsContext
*ctx
, V9fsPath
*dir
,
1104 const char *name
, int flags
)
1107 V9fsString fullname
;
1110 v9fs_string_init(&fullname
);
1112 v9fs_string_sprintf(&fullname
, "%s/%s", dir
->data
, name
);
1113 if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
1114 if (flags
== AT_REMOVEDIR
) {
1116 * If directory remove .virtfs_metadata contained in the
1119 buffer
= g_strdup_printf("%s/%s/%s", ctx
->fs_root
,
1120 fullname
.data
, VIRTFS_META_DIR
);
1121 ret
= remove(buffer
);
1123 if (ret
< 0 && errno
!= ENOENT
) {
1125 * We didn't had the .virtfs_metadata file. May be file created
1126 * in non-mapped mode ?. Ignore ENOENT.
1132 * Now remove the name from parent directory
1133 * .virtfs_metadata directory.
1135 buffer
= local_mapped_attr_path(ctx
, fullname
.data
);
1136 ret
= remove(buffer
);
1138 if (ret
< 0 && errno
!= ENOENT
) {
1140 * We didn't had the .virtfs_metadata file. May be file created
1141 * in non-mapped mode ?. Ignore ENOENT.
1146 /* Remove the name finally */
1147 buffer
= rpath(ctx
, fullname
.data
);
1148 ret
= remove(buffer
);
1152 v9fs_string_free(&fullname
);
1156 static int local_ioc_getversion(FsContext
*ctx
, V9fsPath
*path
,
1157 mode_t st_mode
, uint64_t *st_gen
)
1159 #ifdef FS_IOC_GETVERSION
1161 V9fsFidOpenState fid_open
;
1164 * Do not try to open special files like device nodes, fifos etc
1165 * We can get fd for regular files and directories only
1167 if (!S_ISREG(st_mode
) && !S_ISDIR(st_mode
)) {
1171 err
= local_open(ctx
, path
, O_RDONLY
, &fid_open
);
1175 err
= ioctl(fid_open
.fd
, FS_IOC_GETVERSION
, st_gen
);
1176 local_close(ctx
, &fid_open
);
1184 static int local_init(FsContext
*ctx
)
1187 struct statfs stbuf
;
1189 if (ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) {
1190 ctx
->xops
= passthrough_xattr_ops
;
1191 } else if (ctx
->export_flags
& V9FS_SM_MAPPED
) {
1192 ctx
->xops
= mapped_xattr_ops
;
1193 } else if (ctx
->export_flags
& V9FS_SM_NONE
) {
1194 ctx
->xops
= none_xattr_ops
;
1195 } else if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
1197 * xattr operation for mapped-file and passthrough
1200 ctx
->xops
= passthrough_xattr_ops
;
1202 ctx
->export_flags
|= V9FS_PATHNAME_FSCONTEXT
;
1203 #ifdef FS_IOC_GETVERSION
1205 * use ioc_getversion only if the iocl is definied
1207 err
= statfs(ctx
->fs_root
, &stbuf
);
1209 switch (stbuf
.f_type
) {
1210 case EXT2_SUPER_MAGIC
:
1211 case BTRFS_SUPER_MAGIC
:
1212 case REISERFS_SUPER_MAGIC
:
1213 case XFS_SUPER_MAGIC
:
1214 ctx
->exops
.get_st_gen
= local_ioc_getversion
;
1222 static int local_parse_opts(QemuOpts
*opts
, struct FsDriverEntry
*fse
)
1224 const char *sec_model
= qemu_opt_get(opts
, "security_model");
1225 const char *path
= qemu_opt_get(opts
, "path");
1228 fprintf(stderr
, "security model not specified, "
1229 "local fs needs security model\nvalid options are:"
1230 "\tsecurity_model=[passthrough|mapped|none]\n");
1234 if (!strcmp(sec_model
, "passthrough")) {
1235 fse
->export_flags
|= V9FS_SM_PASSTHROUGH
;
1236 } else if (!strcmp(sec_model
, "mapped") ||
1237 !strcmp(sec_model
, "mapped-xattr")) {
1238 fse
->export_flags
|= V9FS_SM_MAPPED
;
1239 } else if (!strcmp(sec_model
, "none")) {
1240 fse
->export_flags
|= V9FS_SM_NONE
;
1241 } else if (!strcmp(sec_model
, "mapped-file")) {
1242 fse
->export_flags
|= V9FS_SM_MAPPED_FILE
;
1244 fprintf(stderr
, "Invalid security model %s specified, valid options are"
1245 "\n\t [passthrough|mapped-xattr|mapped-file|none]\n",
1251 fprintf(stderr
, "fsdev: No path specified.\n");
1254 fse
->path
= g_strdup(path
);
1259 FileOperations local_ops
= {
1260 .parse_opts
= local_parse_opts
,
1262 .lstat
= local_lstat
,
1263 .readlink
= local_readlink
,
1264 .close
= local_close
,
1265 .closedir
= local_closedir
,
1267 .opendir
= local_opendir
,
1268 .rewinddir
= local_rewinddir
,
1269 .telldir
= local_telldir
,
1270 .readdir_r
= local_readdir_r
,
1271 .seekdir
= local_seekdir
,
1272 .preadv
= local_preadv
,
1273 .pwritev
= local_pwritev
,
1274 .chmod
= local_chmod
,
1275 .mknod
= local_mknod
,
1276 .mkdir
= local_mkdir
,
1277 .fstat
= local_fstat
,
1278 .open2
= local_open2
,
1279 .symlink
= local_symlink
,
1281 .truncate
= local_truncate
,
1282 .rename
= local_rename
,
1283 .chown
= local_chown
,
1284 .utimensat
= local_utimensat
,
1285 .remove
= local_remove
,
1286 .fsync
= local_fsync
,
1287 .statfs
= local_statfs
,
1288 .lgetxattr
= local_lgetxattr
,
1289 .llistxattr
= local_llistxattr
,
1290 .lsetxattr
= local_lsetxattr
,
1291 .lremovexattr
= local_lremovexattr
,
1292 .name_to_path
= local_name_to_path
,
1293 .renameat
= local_renameat
,
1294 .unlinkat
= local_unlinkat
,