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 char *tmp_path
= g_strdup(path
);
50 char *base_name
= basename(tmp_path
);
53 /* NULL terminate the directory */
55 *(base_name
- 1) = '\0';
57 buffer
= g_strdup_printf("%s/%s/%s/%s",
58 ctx
->fs_root
, dir_name
, VIRTFS_META_DIR
, base_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
);
336 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
337 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
338 buffer
= rpath(fs_ctx
, path
);
339 tsize
= readlink(buffer
, buf
, bufsz
);
345 static int local_close(FsContext
*ctx
, V9fsFidOpenState
*fs
)
347 return close(fs
->fd
);
350 static int local_closedir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
352 return closedir(fs
->dir
);
355 static int local_open(FsContext
*ctx
, V9fsPath
*fs_path
,
356 int flags
, V9fsFidOpenState
*fs
)
359 char *path
= fs_path
->data
;
361 buffer
= rpath(ctx
, path
);
362 fs
->fd
= open(buffer
, flags
| O_NOFOLLOW
);
367 static int local_opendir(FsContext
*ctx
,
368 V9fsPath
*fs_path
, V9fsFidOpenState
*fs
)
371 char *path
= fs_path
->data
;
373 buffer
= rpath(ctx
, path
);
374 fs
->dir
= opendir(buffer
);
382 static void local_rewinddir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
384 return rewinddir(fs
->dir
);
387 static off_t
local_telldir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
389 return telldir(fs
->dir
);
392 static int local_readdir_r(FsContext
*ctx
, V9fsFidOpenState
*fs
,
393 struct dirent
*entry
,
394 struct dirent
**result
)
399 ret
= readdir_r(fs
->dir
, entry
, result
);
400 if (ctx
->export_flags
& V9FS_SM_MAPPED
) {
401 entry
->d_type
= DT_UNKNOWN
;
402 } else if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
403 if (!ret
&& *result
!= NULL
&&
404 !strcmp(entry
->d_name
, VIRTFS_META_DIR
)) {
405 /* skp the meta data directory */
408 entry
->d_type
= DT_UNKNOWN
;
413 static void local_seekdir(FsContext
*ctx
, V9fsFidOpenState
*fs
, off_t off
)
415 return seekdir(fs
->dir
, off
);
418 static ssize_t
local_preadv(FsContext
*ctx
, V9fsFidOpenState
*fs
,
419 const struct iovec
*iov
,
420 int iovcnt
, off_t offset
)
423 return preadv(fs
->fd
, iov
, iovcnt
, offset
);
425 int err
= lseek(fs
->fd
, offset
, SEEK_SET
);
429 return readv(fs
->fd
, iov
, iovcnt
);
434 static ssize_t
local_pwritev(FsContext
*ctx
, V9fsFidOpenState
*fs
,
435 const struct iovec
*iov
,
436 int iovcnt
, off_t offset
)
441 ret
= pwritev(fs
->fd
, iov
, iovcnt
, offset
);
443 int err
= lseek(fs
->fd
, offset
, SEEK_SET
);
447 ret
= writev(fs
->fd
, iov
, iovcnt
);
450 #ifdef CONFIG_SYNC_FILE_RANGE
451 if (ret
> 0 && ctx
->export_flags
& V9FS_IMMEDIATE_WRITEOUT
) {
453 * Initiate a writeback. This is not a data integrity sync.
454 * We want to ensure that we don't leave dirty pages in the cache
455 * after write when writeout=immediate is sepcified.
457 sync_file_range(fs
->fd
, offset
, ret
,
458 SYNC_FILE_RANGE_WAIT_BEFORE
| SYNC_FILE_RANGE_WRITE
);
464 static int local_chmod(FsContext
*fs_ctx
, V9fsPath
*fs_path
, FsCred
*credp
)
468 char *path
= fs_path
->data
;
470 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
471 buffer
= rpath(fs_ctx
, path
);
472 ret
= local_set_xattr(buffer
, credp
);
474 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
475 return local_set_mapped_file_attr(fs_ctx
, path
, credp
);
476 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
477 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
478 buffer
= rpath(fs_ctx
, path
);
479 ret
= chmod(buffer
, credp
->fc_mode
);
485 static int local_mknod(FsContext
*fs_ctx
, V9fsPath
*dir_path
,
486 const char *name
, FsCred
*credp
)
494 v9fs_string_init(&fullname
);
495 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
496 path
= fullname
.data
;
498 /* Determine the security model */
499 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
500 buffer
= rpath(fs_ctx
, path
);
501 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);
519 err
= local_set_mapped_file_attr(fs_ctx
, path
, credp
);
524 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
525 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
526 buffer
= rpath(fs_ctx
, path
);
527 err
= mknod(buffer
, credp
->fc_mode
, credp
->fc_rdev
);
532 err
= local_post_create_passthrough(fs_ctx
, path
, credp
);
545 v9fs_string_free(&fullname
);
549 static int local_mkdir(FsContext
*fs_ctx
, V9fsPath
*dir_path
,
550 const char *name
, FsCred
*credp
)
558 v9fs_string_init(&fullname
);
559 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
560 path
= fullname
.data
;
562 /* Determine the security model */
563 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
564 buffer
= rpath(fs_ctx
, path
);
565 err
= mkdir(buffer
, SM_LOCAL_DIR_MODE_BITS
);
570 credp
->fc_mode
= credp
->fc_mode
|S_IFDIR
;
571 err
= local_set_xattr(buffer
, credp
);
576 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
577 buffer
= rpath(fs_ctx
, path
);
578 err
= mkdir(buffer
, SM_LOCAL_DIR_MODE_BITS
);
583 credp
->fc_mode
= credp
->fc_mode
|S_IFDIR
;
584 err
= local_set_mapped_file_attr(fs_ctx
, path
, credp
);
589 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
590 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
591 buffer
= rpath(fs_ctx
, path
);
592 err
= mkdir(buffer
, credp
->fc_mode
);
597 err
= local_post_create_passthrough(fs_ctx
, path
, credp
);
610 v9fs_string_free(&fullname
);
614 static int local_fstat(FsContext
*fs_ctx
, int fid_type
,
615 V9fsFidOpenState
*fs
, struct stat
*stbuf
)
619 if (fid_type
== P9_FID_DIR
) {
625 err
= fstat(fd
, stbuf
);
629 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
630 /* Actual credentials are part of extended attrs */
636 if (fgetxattr(fd
, "user.virtfs.uid", &tmp_uid
, sizeof(uid_t
)) > 0) {
637 stbuf
->st_uid
= le32_to_cpu(tmp_uid
);
639 if (fgetxattr(fd
, "user.virtfs.gid", &tmp_gid
, sizeof(gid_t
)) > 0) {
640 stbuf
->st_gid
= le32_to_cpu(tmp_gid
);
642 if (fgetxattr(fd
, "user.virtfs.mode", &tmp_mode
, sizeof(mode_t
)) > 0) {
643 stbuf
->st_mode
= le32_to_cpu(tmp_mode
);
645 if (fgetxattr(fd
, "user.virtfs.rdev", &tmp_dev
, sizeof(dev_t
)) > 0) {
646 stbuf
->st_rdev
= le64_to_cpu(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
,