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 "fsdev/qemu-fsdev.h" /* local_ops */
18 #include <arpa/inet.h>
21 #include <sys/socket.h>
23 #include "qemu/xattr.h"
26 #ifdef CONFIG_LINUX_MAGIC_H
27 #include <linux/magic.h>
29 #include <sys/ioctl.h>
31 #ifndef XFS_SUPER_MAGIC
32 #define XFS_SUPER_MAGIC 0x58465342
34 #ifndef EXT2_SUPER_MAGIC
35 #define EXT2_SUPER_MAGIC 0xEF53
37 #ifndef REISERFS_SUPER_MAGIC
38 #define REISERFS_SUPER_MAGIC 0x52654973
40 #ifndef BTRFS_SUPER_MAGIC
41 #define BTRFS_SUPER_MAGIC 0x9123683E
44 #define VIRTFS_META_DIR ".virtfs_metadata"
46 static char *local_mapped_attr_path(FsContext
*ctx
, const char *path
)
49 const char *name
= strrchr(path
, '/');
57 return g_strdup_printf("%s/%.*s/%s/%s", ctx
->fs_root
,
58 dirlen
, path
, VIRTFS_META_DIR
, name
);
61 static FILE *local_fopen(const char *path
, const char *mode
)
65 int flags
= O_NOFOLLOW
;
67 * only supports two modes
71 } else if (mode
[0] == 'w') {
72 flags
|= O_WRONLY
| O_TRUNC
| O_CREAT
;
73 o_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
;
77 fd
= open(path
, flags
, o_mode
);
81 fp
= fdopen(fd
, mode
);
89 static void local_mapped_file_attr(FsContext
*ctx
, const char *path
,
96 attr_path
= local_mapped_attr_path(ctx
, path
);
97 fp
= local_fopen(attr_path
, "r");
102 memset(buf
, 0, ATTR_MAX
);
103 while (fgets(buf
, ATTR_MAX
, fp
)) {
104 if (!strncmp(buf
, "virtfs.uid", 10)) {
105 stbuf
->st_uid
= atoi(buf
+11);
106 } else if (!strncmp(buf
, "virtfs.gid", 10)) {
107 stbuf
->st_gid
= atoi(buf
+11);
108 } else if (!strncmp(buf
, "virtfs.mode", 11)) {
109 stbuf
->st_mode
= atoi(buf
+12);
110 } else if (!strncmp(buf
, "virtfs.rdev", 11)) {
111 stbuf
->st_rdev
= atoi(buf
+12);
113 memset(buf
, 0, ATTR_MAX
);
118 static int local_lstat(FsContext
*fs_ctx
, V9fsPath
*fs_path
, struct stat
*stbuf
)
122 char *path
= fs_path
->data
;
124 buffer
= rpath(fs_ctx
, path
);
125 err
= lstat(buffer
, stbuf
);
129 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
130 /* Actual credentials are part of extended attrs */
135 if (getxattr(buffer
, "user.virtfs.uid", &tmp_uid
, sizeof(uid_t
)) > 0) {
136 stbuf
->st_uid
= le32_to_cpu(tmp_uid
);
138 if (getxattr(buffer
, "user.virtfs.gid", &tmp_gid
, sizeof(gid_t
)) > 0) {
139 stbuf
->st_gid
= le32_to_cpu(tmp_gid
);
141 if (getxattr(buffer
, "user.virtfs.mode",
142 &tmp_mode
, sizeof(mode_t
)) > 0) {
143 stbuf
->st_mode
= le32_to_cpu(tmp_mode
);
145 if (getxattr(buffer
, "user.virtfs.rdev", &tmp_dev
, sizeof(dev_t
)) > 0) {
146 stbuf
->st_rdev
= le64_to_cpu(tmp_dev
);
148 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
149 local_mapped_file_attr(fs_ctx
, path
, stbuf
);
157 static int local_create_mapped_attr_dir(FsContext
*ctx
, const char *path
)
161 char *tmp_path
= g_strdup(path
);
163 attr_dir
= g_strdup_printf("%s/%s/%s",
164 ctx
->fs_root
, dirname(tmp_path
), VIRTFS_META_DIR
);
166 err
= mkdir(attr_dir
, 0700);
167 if (err
< 0 && errno
== EEXIST
) {
175 static int local_set_mapped_file_attr(FsContext
*ctx
,
176 const char *path
, FsCred
*credp
)
182 int uid
= -1, gid
= -1, mode
= -1, rdev
= -1;
184 attr_path
= local_mapped_attr_path(ctx
, path
);
185 fp
= local_fopen(attr_path
, "r");
187 goto create_map_file
;
189 memset(buf
, 0, ATTR_MAX
);
190 while (fgets(buf
, ATTR_MAX
, fp
)) {
191 if (!strncmp(buf
, "virtfs.uid", 10)) {
193 } else if (!strncmp(buf
, "virtfs.gid", 10)) {
195 } else if (!strncmp(buf
, "virtfs.mode", 11)) {
197 } else if (!strncmp(buf
, "virtfs.rdev", 11)) {
200 memset(buf
, 0, ATTR_MAX
);
203 goto update_map_file
;
206 ret
= local_create_mapped_attr_dir(ctx
, path
);
212 fp
= local_fopen(attr_path
, "w");
218 if (credp
->fc_uid
!= -1) {
221 if (credp
->fc_gid
!= -1) {
224 if (credp
->fc_mode
!= -1) {
225 mode
= credp
->fc_mode
;
227 if (credp
->fc_rdev
!= -1) {
228 rdev
= credp
->fc_rdev
;
233 fprintf(fp
, "virtfs.uid=%d\n", uid
);
236 fprintf(fp
, "virtfs.gid=%d\n", gid
);
239 fprintf(fp
, "virtfs.mode=%d\n", mode
);
242 fprintf(fp
, "virtfs.rdev=%d\n", rdev
);
251 static int local_set_xattr(const char *path
, FsCred
*credp
)
255 if (credp
->fc_uid
!= -1) {
256 uint32_t tmp_uid
= cpu_to_le32(credp
->fc_uid
);
257 err
= setxattr(path
, "user.virtfs.uid", &tmp_uid
, sizeof(uid_t
), 0);
262 if (credp
->fc_gid
!= -1) {
263 uint32_t tmp_gid
= cpu_to_le32(credp
->fc_gid
);
264 err
= setxattr(path
, "user.virtfs.gid", &tmp_gid
, sizeof(gid_t
), 0);
269 if (credp
->fc_mode
!= -1) {
270 uint32_t tmp_mode
= cpu_to_le32(credp
->fc_mode
);
271 err
= setxattr(path
, "user.virtfs.mode", &tmp_mode
, sizeof(mode_t
), 0);
276 if (credp
->fc_rdev
!= -1) {
277 uint64_t tmp_rdev
= cpu_to_le64(credp
->fc_rdev
);
278 err
= setxattr(path
, "user.virtfs.rdev", &tmp_rdev
, sizeof(dev_t
), 0);
286 static int local_post_create_passthrough(FsContext
*fs_ctx
, const char *path
,
291 buffer
= rpath(fs_ctx
, path
);
292 if (lchown(buffer
, credp
->fc_uid
, credp
->fc_gid
) < 0) {
294 * If we fail to change ownership and if we are
295 * using security model none. Ignore the error
297 if ((fs_ctx
->export_flags
& V9FS_SEC_MASK
) != V9FS_SM_NONE
) {
302 if (chmod(buffer
, credp
->fc_mode
& 07777) < 0) {
313 static ssize_t
local_readlink(FsContext
*fs_ctx
, V9fsPath
*fs_path
,
314 char *buf
, size_t bufsz
)
318 char *path
= fs_path
->data
;
320 if ((fs_ctx
->export_flags
& V9FS_SM_MAPPED
) ||
321 (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
)) {
323 buffer
= rpath(fs_ctx
, path
);
324 fd
= open(buffer
, O_RDONLY
| O_NOFOLLOW
);
330 tsize
= read(fd
, (void *)buf
, bufsz
);
331 } while (tsize
== -1 && errno
== EINTR
);
333 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
334 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
335 buffer
= rpath(fs_ctx
, path
);
336 tsize
= readlink(buffer
, buf
, bufsz
);
342 static int local_close(FsContext
*ctx
, V9fsFidOpenState
*fs
)
344 return close(fs
->fd
);
347 static int local_closedir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
349 return closedir(fs
->dir
);
352 static int local_open(FsContext
*ctx
, V9fsPath
*fs_path
,
353 int flags
, V9fsFidOpenState
*fs
)
356 char *path
= fs_path
->data
;
358 buffer
= rpath(ctx
, path
);
359 fs
->fd
= open(buffer
, flags
| O_NOFOLLOW
);
364 static int local_opendir(FsContext
*ctx
,
365 V9fsPath
*fs_path
, V9fsFidOpenState
*fs
)
368 char *path
= fs_path
->data
;
370 buffer
= rpath(ctx
, path
);
371 fs
->dir
= opendir(buffer
);
379 static void local_rewinddir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
384 static off_t
local_telldir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
386 return telldir(fs
->dir
);
389 static int local_readdir_r(FsContext
*ctx
, V9fsFidOpenState
*fs
,
390 struct dirent
*entry
,
391 struct dirent
**result
)
396 ret
= readdir_r(fs
->dir
, entry
, result
);
397 if (ctx
->export_flags
& V9FS_SM_MAPPED
) {
398 entry
->d_type
= DT_UNKNOWN
;
399 } else 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 */
405 entry
->d_type
= DT_UNKNOWN
;
410 static void local_seekdir(FsContext
*ctx
, V9fsFidOpenState
*fs
, off_t off
)
412 seekdir(fs
->dir
, off
);
415 static ssize_t
local_preadv(FsContext
*ctx
, V9fsFidOpenState
*fs
,
416 const struct iovec
*iov
,
417 int iovcnt
, off_t offset
)
420 return preadv(fs
->fd
, iov
, iovcnt
, offset
);
422 int err
= lseek(fs
->fd
, offset
, SEEK_SET
);
426 return readv(fs
->fd
, iov
, iovcnt
);
431 static ssize_t
local_pwritev(FsContext
*ctx
, V9fsFidOpenState
*fs
,
432 const struct iovec
*iov
,
433 int iovcnt
, off_t offset
)
438 ret
= pwritev(fs
->fd
, iov
, iovcnt
, offset
);
440 int err
= lseek(fs
->fd
, offset
, SEEK_SET
);
444 ret
= writev(fs
->fd
, iov
, iovcnt
);
447 #ifdef CONFIG_SYNC_FILE_RANGE
448 if (ret
> 0 && ctx
->export_flags
& V9FS_IMMEDIATE_WRITEOUT
) {
450 * Initiate a writeback. This is not a data integrity sync.
451 * We want to ensure that we don't leave dirty pages in the cache
452 * after write when writeout=immediate is sepcified.
454 sync_file_range(fs
->fd
, offset
, ret
,
455 SYNC_FILE_RANGE_WAIT_BEFORE
| SYNC_FILE_RANGE_WRITE
);
461 static int local_chmod(FsContext
*fs_ctx
, V9fsPath
*fs_path
, FsCred
*credp
)
465 char *path
= fs_path
->data
;
467 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
468 buffer
= rpath(fs_ctx
, path
);
469 ret
= local_set_xattr(buffer
, credp
);
471 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
472 return local_set_mapped_file_attr(fs_ctx
, path
, credp
);
473 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
474 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
475 buffer
= rpath(fs_ctx
, path
);
476 ret
= chmod(buffer
, credp
->fc_mode
);
482 static int local_mknod(FsContext
*fs_ctx
, V9fsPath
*dir_path
,
483 const char *name
, FsCred
*credp
)
491 v9fs_string_init(&fullname
);
492 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
493 path
= fullname
.data
;
495 /* Determine the security model */
496 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
497 buffer
= rpath(fs_ctx
, path
);
498 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);
514 err
= local_set_mapped_file_attr(fs_ctx
, path
, credp
);
519 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
520 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
521 buffer
= rpath(fs_ctx
, path
);
522 err
= mknod(buffer
, credp
->fc_mode
, credp
->fc_rdev
);
526 err
= local_post_create_passthrough(fs_ctx
, path
, credp
);
539 v9fs_string_free(&fullname
);
543 static int local_mkdir(FsContext
*fs_ctx
, V9fsPath
*dir_path
,
544 const char *name
, FsCred
*credp
)
552 v9fs_string_init(&fullname
);
553 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
554 path
= fullname
.data
;
556 /* Determine the security model */
557 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
558 buffer
= rpath(fs_ctx
, path
);
559 err
= mkdir(buffer
, SM_LOCAL_DIR_MODE_BITS
);
563 credp
->fc_mode
= credp
->fc_mode
|S_IFDIR
;
564 err
= local_set_xattr(buffer
, credp
);
569 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
570 buffer
= rpath(fs_ctx
, path
);
571 err
= mkdir(buffer
, SM_LOCAL_DIR_MODE_BITS
);
575 credp
->fc_mode
= credp
->fc_mode
|S_IFDIR
;
576 err
= local_set_mapped_file_attr(fs_ctx
, path
, credp
);
581 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
582 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
583 buffer
= rpath(fs_ctx
, path
);
584 err
= mkdir(buffer
, credp
->fc_mode
);
588 err
= local_post_create_passthrough(fs_ctx
, path
, credp
);
601 v9fs_string_free(&fullname
);
605 static int local_fstat(FsContext
*fs_ctx
, int fid_type
,
606 V9fsFidOpenState
*fs
, struct stat
*stbuf
)
610 if (fid_type
== P9_FID_DIR
) {
616 err
= fstat(fd
, stbuf
);
620 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
621 /* Actual credentials are part of extended attrs */
627 if (fgetxattr(fd
, "user.virtfs.uid", &tmp_uid
, sizeof(uid_t
)) > 0) {
628 stbuf
->st_uid
= le32_to_cpu(tmp_uid
);
630 if (fgetxattr(fd
, "user.virtfs.gid", &tmp_gid
, sizeof(gid_t
)) > 0) {
631 stbuf
->st_gid
= le32_to_cpu(tmp_gid
);
633 if (fgetxattr(fd
, "user.virtfs.mode", &tmp_mode
, sizeof(mode_t
)) > 0) {
634 stbuf
->st_mode
= le32_to_cpu(tmp_mode
);
636 if (fgetxattr(fd
, "user.virtfs.rdev", &tmp_dev
, sizeof(dev_t
)) > 0) {
637 stbuf
->st_rdev
= le64_to_cpu(tmp_dev
);
639 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
646 static int local_open2(FsContext
*fs_ctx
, V9fsPath
*dir_path
, const char *name
,
647 int flags
, FsCred
*credp
, V9fsFidOpenState
*fs
)
657 * Mark all the open to not follow symlinks
661 v9fs_string_init(&fullname
);
662 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
663 path
= fullname
.data
;
665 /* Determine the security model */
666 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
667 buffer
= rpath(fs_ctx
, path
);
668 fd
= open(buffer
, flags
, SM_LOCAL_MODE_BITS
);
673 credp
->fc_mode
= credp
->fc_mode
|S_IFREG
;
674 /* Set cleint credentials in xattr */
675 err
= local_set_xattr(buffer
, credp
);
680 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
681 buffer
= rpath(fs_ctx
, path
);
682 fd
= open(buffer
, flags
, SM_LOCAL_MODE_BITS
);
687 credp
->fc_mode
= credp
->fc_mode
|S_IFREG
;
688 /* Set client credentials in .virtfs_metadata directory files */
689 err
= local_set_mapped_file_attr(fs_ctx
, path
, credp
);
694 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
695 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
696 buffer
= rpath(fs_ctx
, path
);
697 fd
= open(buffer
, flags
, credp
->fc_mode
);
702 err
= local_post_create_passthrough(fs_ctx
, path
, credp
);
718 v9fs_string_free(&fullname
);
723 static int local_symlink(FsContext
*fs_ctx
, const char *oldpath
,
724 V9fsPath
*dir_path
, const char *name
, FsCred
*credp
)
732 v9fs_string_init(&fullname
);
733 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
734 newpath
= fullname
.data
;
736 /* Determine the security model */
737 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
739 ssize_t oldpath_size
, write_size
;
740 buffer
= rpath(fs_ctx
, newpath
);
741 fd
= open(buffer
, O_CREAT
|O_EXCL
|O_RDWR
|O_NOFOLLOW
, SM_LOCAL_MODE_BITS
);
746 /* Write the oldpath (target) to the file. */
747 oldpath_size
= strlen(oldpath
);
749 write_size
= write(fd
, (void *)oldpath
, oldpath_size
);
750 } while (write_size
== -1 && errno
== EINTR
);
752 if (write_size
!= oldpath_size
) {
759 /* Set cleint credentials in symlink's xattr */
760 credp
->fc_mode
= credp
->fc_mode
|S_IFLNK
;
761 err
= local_set_xattr(buffer
, credp
);
766 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
768 ssize_t oldpath_size
, write_size
;
769 buffer
= rpath(fs_ctx
, newpath
);
770 fd
= open(buffer
, O_CREAT
|O_EXCL
|O_RDWR
|O_NOFOLLOW
, SM_LOCAL_MODE_BITS
);
775 /* Write the oldpath (target) to the file. */
776 oldpath_size
= strlen(oldpath
);
778 write_size
= write(fd
, (void *)oldpath
, oldpath_size
);
779 } while (write_size
== -1 && errno
== EINTR
);
781 if (write_size
!= oldpath_size
) {
788 /* Set cleint credentials in symlink's xattr */
789 credp
->fc_mode
= credp
->fc_mode
|S_IFLNK
;
790 err
= local_set_mapped_file_attr(fs_ctx
, newpath
, credp
);
795 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
796 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
797 buffer
= rpath(fs_ctx
, newpath
);
798 err
= symlink(oldpath
, buffer
);
802 err
= lchown(buffer
, credp
->fc_uid
, credp
->fc_gid
);
805 * If we fail to change ownership and if we are
806 * using security model none. Ignore the error
808 if ((fs_ctx
->export_flags
& V9FS_SEC_MASK
) != V9FS_SM_NONE
) {
822 v9fs_string_free(&fullname
);
826 static int local_link(FsContext
*ctx
, V9fsPath
*oldpath
,
827 V9fsPath
*dirpath
, const char *name
)
831 char *buffer
, *buffer1
;
833 v9fs_string_init(&newpath
);
834 v9fs_string_sprintf(&newpath
, "%s/%s", dirpath
->data
, name
);
836 buffer
= rpath(ctx
, oldpath
->data
);
837 buffer1
= rpath(ctx
, newpath
.data
);
838 ret
= link(buffer
, buffer1
);
842 /* now link the virtfs_metadata files */
843 if (!ret
&& (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
)) {
844 /* Link the .virtfs_metadata files. Create the metada directory */
845 ret
= local_create_mapped_attr_dir(ctx
, newpath
.data
);
849 buffer
= local_mapped_attr_path(ctx
, oldpath
->data
);
850 buffer1
= local_mapped_attr_path(ctx
, newpath
.data
);
851 ret
= link(buffer
, buffer1
);
854 if (ret
< 0 && errno
!= ENOENT
) {
859 v9fs_string_free(&newpath
);
863 static int local_truncate(FsContext
*ctx
, V9fsPath
*fs_path
, off_t size
)
867 char *path
= fs_path
->data
;
869 buffer
= rpath(ctx
, path
);
870 ret
= truncate(buffer
, size
);
875 static int local_rename(FsContext
*ctx
, const char *oldpath
,
879 char *buffer
, *buffer1
;
881 if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
882 err
= local_create_mapped_attr_dir(ctx
, newpath
);
886 /* rename the .virtfs_metadata files */
887 buffer
= local_mapped_attr_path(ctx
, oldpath
);
888 buffer1
= local_mapped_attr_path(ctx
, newpath
);
889 err
= rename(buffer
, buffer1
);
892 if (err
< 0 && errno
!= ENOENT
) {
897 buffer
= rpath(ctx
, oldpath
);
898 buffer1
= rpath(ctx
, newpath
);
899 err
= rename(buffer
, buffer1
);
905 static int local_chown(FsContext
*fs_ctx
, V9fsPath
*fs_path
, FsCred
*credp
)
909 char *path
= fs_path
->data
;
911 if ((credp
->fc_uid
== -1 && credp
->fc_gid
== -1) ||
912 (fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
913 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
914 buffer
= rpath(fs_ctx
, path
);
915 ret
= lchown(buffer
, credp
->fc_uid
, credp
->fc_gid
);
917 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
918 buffer
= rpath(fs_ctx
, path
);
919 ret
= local_set_xattr(buffer
, credp
);
921 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
922 return local_set_mapped_file_attr(fs_ctx
, path
, credp
);
927 static int local_utimensat(FsContext
*s
, V9fsPath
*fs_path
,
928 const struct timespec
*buf
)
932 char *path
= fs_path
->data
;
934 buffer
= rpath(s
, path
);
935 ret
= qemu_utimens(buffer
, buf
);
940 static int local_remove(FsContext
*ctx
, const char *path
)
946 if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
947 buffer
= rpath(ctx
, path
);
948 err
= lstat(buffer
, &stbuf
);
954 * If directory remove .virtfs_metadata contained in the
957 if (S_ISDIR(stbuf
.st_mode
)) {
958 buffer
= g_strdup_printf("%s/%s/%s", ctx
->fs_root
,
959 path
, VIRTFS_META_DIR
);
960 err
= remove(buffer
);
962 if (err
< 0 && errno
!= ENOENT
) {
964 * We didn't had the .virtfs_metadata file. May be file created
965 * in non-mapped mode ?. Ignore ENOENT.
971 * Now remove the name from parent directory
972 * .virtfs_metadata directory
974 buffer
= local_mapped_attr_path(ctx
, path
);
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 buffer
= rpath(ctx
, path
);
987 err
= remove(buffer
);
993 static int local_fsync(FsContext
*ctx
, int fid_type
,
994 V9fsFidOpenState
*fs
, int datasync
)
998 if (fid_type
== P9_FID_DIR
) {
1005 return qemu_fdatasync(fd
);
1011 static int local_statfs(FsContext
*s
, V9fsPath
*fs_path
, struct statfs
*stbuf
)
1015 char *path
= fs_path
->data
;
1017 buffer
= rpath(s
, path
);
1018 ret
= statfs(buffer
, stbuf
);
1023 static ssize_t
local_lgetxattr(FsContext
*ctx
, V9fsPath
*fs_path
,
1024 const char *name
, void *value
, size_t size
)
1026 char *path
= fs_path
->data
;
1028 return v9fs_get_xattr(ctx
, path
, name
, value
, size
);
1031 static ssize_t
local_llistxattr(FsContext
*ctx
, V9fsPath
*fs_path
,
1032 void *value
, size_t size
)
1034 char *path
= fs_path
->data
;
1036 return v9fs_list_xattr(ctx
, path
, value
, size
);
1039 static int local_lsetxattr(FsContext
*ctx
, V9fsPath
*fs_path
, const char *name
,
1040 void *value
, size_t size
, int flags
)
1042 char *path
= fs_path
->data
;
1044 return v9fs_set_xattr(ctx
, path
, name
, value
, size
, flags
);
1047 static int local_lremovexattr(FsContext
*ctx
, V9fsPath
*fs_path
,
1050 char *path
= fs_path
->data
;
1052 return v9fs_remove_xattr(ctx
, path
, name
);
1055 static int local_name_to_path(FsContext
*ctx
, V9fsPath
*dir_path
,
1056 const char *name
, V9fsPath
*target
)
1059 v9fs_string_sprintf((V9fsString
*)target
, "%s/%s",
1060 dir_path
->data
, name
);
1062 v9fs_string_sprintf((V9fsString
*)target
, "%s", name
);
1064 /* Bump the size for including terminating NULL */
1069 static int local_renameat(FsContext
*ctx
, V9fsPath
*olddir
,
1070 const char *old_name
, V9fsPath
*newdir
,
1071 const char *new_name
)
1074 V9fsString old_full_name
, new_full_name
;
1076 v9fs_string_init(&old_full_name
);
1077 v9fs_string_init(&new_full_name
);
1079 v9fs_string_sprintf(&old_full_name
, "%s/%s", olddir
->data
, old_name
);
1080 v9fs_string_sprintf(&new_full_name
, "%s/%s", newdir
->data
, new_name
);
1082 ret
= local_rename(ctx
, old_full_name
.data
, new_full_name
.data
);
1083 v9fs_string_free(&old_full_name
);
1084 v9fs_string_free(&new_full_name
);
1088 static int local_unlinkat(FsContext
*ctx
, V9fsPath
*dir
,
1089 const char *name
, int flags
)
1092 V9fsString fullname
;
1095 v9fs_string_init(&fullname
);
1097 v9fs_string_sprintf(&fullname
, "%s/%s", dir
->data
, name
);
1098 if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
1099 if (flags
== AT_REMOVEDIR
) {
1101 * If directory remove .virtfs_metadata contained in the
1104 buffer
= g_strdup_printf("%s/%s/%s", ctx
->fs_root
,
1105 fullname
.data
, VIRTFS_META_DIR
);
1106 ret
= remove(buffer
);
1108 if (ret
< 0 && errno
!= ENOENT
) {
1110 * We didn't had the .virtfs_metadata file. May be file created
1111 * in non-mapped mode ?. Ignore ENOENT.
1117 * Now remove the name from parent directory
1118 * .virtfs_metadata directory.
1120 buffer
= local_mapped_attr_path(ctx
, fullname
.data
);
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.
1131 /* Remove the name finally */
1132 buffer
= rpath(ctx
, fullname
.data
);
1133 ret
= remove(buffer
);
1137 v9fs_string_free(&fullname
);
1141 static int local_ioc_getversion(FsContext
*ctx
, V9fsPath
*path
,
1142 mode_t st_mode
, uint64_t *st_gen
)
1144 #ifdef FS_IOC_GETVERSION
1146 V9fsFidOpenState fid_open
;
1149 * Do not try to open special files like device nodes, fifos etc
1150 * We can get fd for regular files and directories only
1152 if (!S_ISREG(st_mode
) && !S_ISDIR(st_mode
)) {
1156 err
= local_open(ctx
, path
, O_RDONLY
, &fid_open
);
1160 err
= ioctl(fid_open
.fd
, FS_IOC_GETVERSION
, st_gen
);
1161 local_close(ctx
, &fid_open
);
1169 static int local_init(FsContext
*ctx
)
1172 struct statfs stbuf
;
1174 if (ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) {
1175 ctx
->xops
= passthrough_xattr_ops
;
1176 } else if (ctx
->export_flags
& V9FS_SM_MAPPED
) {
1177 ctx
->xops
= mapped_xattr_ops
;
1178 } else if (ctx
->export_flags
& V9FS_SM_NONE
) {
1179 ctx
->xops
= none_xattr_ops
;
1180 } else if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
1182 * xattr operation for mapped-file and passthrough
1185 ctx
->xops
= passthrough_xattr_ops
;
1187 ctx
->export_flags
|= V9FS_PATHNAME_FSCONTEXT
;
1188 #ifdef FS_IOC_GETVERSION
1190 * use ioc_getversion only if the iocl is definied
1192 err
= statfs(ctx
->fs_root
, &stbuf
);
1194 switch (stbuf
.f_type
) {
1195 case EXT2_SUPER_MAGIC
:
1196 case BTRFS_SUPER_MAGIC
:
1197 case REISERFS_SUPER_MAGIC
:
1198 case XFS_SUPER_MAGIC
:
1199 ctx
->exops
.get_st_gen
= local_ioc_getversion
;
1207 static int local_parse_opts(QemuOpts
*opts
, struct FsDriverEntry
*fse
)
1209 const char *sec_model
= qemu_opt_get(opts
, "security_model");
1210 const char *path
= qemu_opt_get(opts
, "path");
1213 fprintf(stderr
, "security model not specified, "
1214 "local fs needs security model\nvalid options are:"
1215 "\tsecurity_model=[passthrough|mapped|none]\n");
1219 if (!strcmp(sec_model
, "passthrough")) {
1220 fse
->export_flags
|= V9FS_SM_PASSTHROUGH
;
1221 } else if (!strcmp(sec_model
, "mapped") ||
1222 !strcmp(sec_model
, "mapped-xattr")) {
1223 fse
->export_flags
|= V9FS_SM_MAPPED
;
1224 } else if (!strcmp(sec_model
, "none")) {
1225 fse
->export_flags
|= V9FS_SM_NONE
;
1226 } else if (!strcmp(sec_model
, "mapped-file")) {
1227 fse
->export_flags
|= V9FS_SM_MAPPED_FILE
;
1229 fprintf(stderr
, "Invalid security model %s specified, valid options are"
1230 "\n\t [passthrough|mapped-xattr|mapped-file|none]\n",
1236 fprintf(stderr
, "fsdev: No path specified.\n");
1239 fse
->path
= g_strdup(path
);
1244 FileOperations local_ops
= {
1245 .parse_opts
= local_parse_opts
,
1247 .lstat
= local_lstat
,
1248 .readlink
= local_readlink
,
1249 .close
= local_close
,
1250 .closedir
= local_closedir
,
1252 .opendir
= local_opendir
,
1253 .rewinddir
= local_rewinddir
,
1254 .telldir
= local_telldir
,
1255 .readdir_r
= local_readdir_r
,
1256 .seekdir
= local_seekdir
,
1257 .preadv
= local_preadv
,
1258 .pwritev
= local_pwritev
,
1259 .chmod
= local_chmod
,
1260 .mknod
= local_mknod
,
1261 .mkdir
= local_mkdir
,
1262 .fstat
= local_fstat
,
1263 .open2
= local_open2
,
1264 .symlink
= local_symlink
,
1266 .truncate
= local_truncate
,
1267 .rename
= local_rename
,
1268 .chown
= local_chown
,
1269 .utimensat
= local_utimensat
,
1270 .remove
= local_remove
,
1271 .fsync
= local_fsync
,
1272 .statfs
= local_statfs
,
1273 .lgetxattr
= local_lgetxattr
,
1274 .llistxattr
= local_llistxattr
,
1275 .lsetxattr
= local_lsetxattr
,
1276 .lremovexattr
= local_lremovexattr
,
1277 .name_to_path
= local_name_to_path
,
1278 .renameat
= local_renameat
,
1279 .unlinkat
= local_unlinkat
,