4 * Copyright IBM, Corp. 2010
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/error-report.h"
27 #ifdef CONFIG_LINUX_MAGIC_H
28 #include <linux/magic.h>
30 #include <sys/ioctl.h>
32 #ifndef XFS_SUPER_MAGIC
33 #define XFS_SUPER_MAGIC 0x58465342
35 #ifndef EXT2_SUPER_MAGIC
36 #define EXT2_SUPER_MAGIC 0xEF53
38 #ifndef REISERFS_SUPER_MAGIC
39 #define REISERFS_SUPER_MAGIC 0x52654973
41 #ifndef BTRFS_SUPER_MAGIC
42 #define BTRFS_SUPER_MAGIC 0x9123683E
45 #define VIRTFS_META_DIR ".virtfs_metadata"
47 static char *local_mapped_attr_path(FsContext *ctx, const char *path)
50 const char *name = strrchr(path, '/');
58 return g_strdup_printf("%s/%.*s/%s/%s", ctx->fs_root,
59 dirlen, path, VIRTFS_META_DIR, 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 = le32_to_cpu(tmp_uid);
139 if (getxattr(buffer, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
140 stbuf->st_gid = le32_to_cpu(tmp_gid);
142 if (getxattr(buffer, "user.virtfs.mode",
143 &tmp_mode, sizeof(mode_t)) > 0) {
144 stbuf->st_mode = le32_to_cpu(tmp_mode);
146 if (getxattr(buffer, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
147 stbuf->st_rdev = le64_to_cpu(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 uint32_t tmp_uid = cpu_to_le32(credp->fc_uid);
258 err = setxattr(path, "user.virtfs.uid", &tmp_uid, sizeof(uid_t), 0);
263 if (credp->fc_gid != -1) {
264 uint32_t tmp_gid = cpu_to_le32(credp->fc_gid);
265 err = setxattr(path, "user.virtfs.gid", &tmp_gid, sizeof(gid_t), 0);
270 if (credp->fc_mode != -1) {
271 uint32_t tmp_mode = cpu_to_le32(credp->fc_mode);
272 err = setxattr(path, "user.virtfs.mode", &tmp_mode, sizeof(mode_t), 0);
277 if (credp->fc_rdev != -1) {
278 uint64_t tmp_rdev = cpu_to_le64(credp->fc_rdev);
279 err = setxattr(path, "user.virtfs.rdev", &tmp_rdev, sizeof(dev_t), 0);
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);
334 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
335 (fs_ctx->export_flags & V9FS_SM_NONE)) {
336 buffer = rpath(fs_ctx, path);
337 tsize = readlink(buffer, buf, bufsz);
343 static int local_close(FsContext *ctx, V9fsFidOpenState *fs)
345 return close(fs->fd);
348 static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs)
350 return closedir(fs->dir);
353 static int local_open(FsContext *ctx, V9fsPath *fs_path,
354 int flags, V9fsFidOpenState *fs)
357 char *path = fs_path->data;
359 buffer = rpath(ctx, path);
360 fs->fd = open(buffer, flags | O_NOFOLLOW);
365 static int local_opendir(FsContext *ctx,
366 V9fsPath *fs_path, V9fsFidOpenState *fs)
369 char *path = fs_path->data;
371 buffer = rpath(ctx, path);
372 fs->dir = opendir(buffer);
380 static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
385 static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs)
387 return telldir(fs->dir);
390 static int local_readdir_r(FsContext *ctx, V9fsFidOpenState *fs,
391 struct dirent *entry,
392 struct dirent **result)
397 ret = readdir_r(fs->dir, entry, result);
398 if (ctx->export_flags & V9FS_SM_MAPPED) {
399 entry->d_type = DT_UNKNOWN;
400 } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
401 if (!ret && *result != NULL &&
402 !strcmp(entry->d_name, VIRTFS_META_DIR)) {
403 /* skp the meta data directory */
406 entry->d_type = DT_UNKNOWN;
411 static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
413 seekdir(fs->dir, off);
416 static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs,
417 const struct iovec *iov,
418 int iovcnt, off_t offset)
421 return preadv(fs->fd, iov, iovcnt, offset);
423 int err = lseek(fs->fd, offset, SEEK_SET);
427 return readv(fs->fd, iov, iovcnt);
432 static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
433 const struct iovec *iov,
434 int iovcnt, off_t offset)
439 ret = pwritev(fs->fd, iov, iovcnt, offset);
441 int err = lseek(fs->fd, offset, SEEK_SET);
445 ret = writev(fs->fd, iov, iovcnt);
448 #ifdef CONFIG_SYNC_FILE_RANGE
449 if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
451 * Initiate a writeback. This is not a data integrity sync.
452 * We want to ensure that we don't leave dirty pages in the cache
453 * after write when writeout=immediate is sepcified.
455 sync_file_range(fs->fd, offset, ret,
456 SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
462 static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
466 char *path = fs_path->data;
468 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
469 buffer = rpath(fs_ctx, path);
470 ret = local_set_xattr(buffer, credp);
472 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
473 return local_set_mapped_file_attr(fs_ctx, path, credp);
474 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
475 (fs_ctx->export_flags & V9FS_SM_NONE)) {
476 buffer = rpath(fs_ctx, path);
477 ret = chmod(buffer, credp->fc_mode);
483 static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
484 const char *name, FsCred *credp)
492 v9fs_string_init(&fullname);
493 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
494 path = fullname.data;
496 /* Determine the security model */
497 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
498 buffer = rpath(fs_ctx, path);
499 err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0);
503 err = local_set_xattr(buffer, credp);
508 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
510 buffer = rpath(fs_ctx, path);
511 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);
527 err = local_post_create_passthrough(fs_ctx, path, credp);
540 v9fs_string_free(&fullname);
544 static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
545 const char *name, FsCred *credp)
553 v9fs_string_init(&fullname);
554 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
555 path = fullname.data;
557 /* Determine the security model */
558 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
559 buffer = rpath(fs_ctx, path);
560 err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
564 credp->fc_mode = credp->fc_mode|S_IFDIR;
565 err = local_set_xattr(buffer, credp);
570 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
571 buffer = rpath(fs_ctx, path);
572 err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
576 credp->fc_mode = credp->fc_mode|S_IFDIR;
577 err = local_set_mapped_file_attr(fs_ctx, path, credp);
582 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
583 (fs_ctx->export_flags & V9FS_SM_NONE)) {
584 buffer = rpath(fs_ctx, path);
585 err = mkdir(buffer, credp->fc_mode);
589 err = local_post_create_passthrough(fs_ctx, path, credp);
602 v9fs_string_free(&fullname);
606 static int local_fstat(FsContext *fs_ctx, int fid_type,
607 V9fsFidOpenState *fs, struct stat *stbuf)
611 if (fid_type == P9_FID_DIR) {
617 err = fstat(fd, stbuf);
621 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
622 /* Actual credentials are part of extended attrs */
628 if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) {
629 stbuf->st_uid = le32_to_cpu(tmp_uid);
631 if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
632 stbuf->st_gid = le32_to_cpu(tmp_gid);
634 if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) {
635 stbuf->st_mode = le32_to_cpu(tmp_mode);
637 if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
638 stbuf->st_rdev = le64_to_cpu(tmp_dev);
640 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
647 static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
648 int flags, FsCred *credp, V9fsFidOpenState *fs)
658 * Mark all the open to not follow symlinks
662 v9fs_string_init(&fullname);
663 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
664 path = fullname.data;
666 /* Determine the security model */
667 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
668 buffer = rpath(fs_ctx, path);
669 fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
674 credp->fc_mode = credp->fc_mode|S_IFREG;
675 /* Set cleint credentials in xattr */
676 err = local_set_xattr(buffer, credp);
681 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
682 buffer = rpath(fs_ctx, path);
683 fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
688 credp->fc_mode = credp->fc_mode|S_IFREG;
689 /* Set client credentials in .virtfs_metadata directory files */
690 err = local_set_mapped_file_attr(fs_ctx, path, credp);
695 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
696 (fs_ctx->export_flags & V9FS_SM_NONE)) {
697 buffer = rpath(fs_ctx, path);
698 fd = open(buffer, flags, credp->fc_mode);
703 err = local_post_create_passthrough(fs_ctx, path, credp);
719 v9fs_string_free(&fullname);
724 static int local_symlink(FsContext *fs_ctx, const char *oldpath,
725 V9fsPath *dir_path, const char *name, FsCred *credp)
733 v9fs_string_init(&fullname);
734 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
735 newpath = fullname.data;
737 /* Determine the security model */
738 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
740 ssize_t oldpath_size, write_size;
741 buffer = rpath(fs_ctx, newpath);
742 fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS);
747 /* Write the oldpath (target) to the file. */
748 oldpath_size = strlen(oldpath);
750 write_size = write(fd, (void *)oldpath, oldpath_size);
751 } while (write_size == -1 && errno == EINTR);
753 if (write_size != oldpath_size) {
760 /* Set cleint credentials in symlink's xattr */
761 credp->fc_mode = credp->fc_mode|S_IFLNK;
762 err = local_set_xattr(buffer, credp);
767 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
769 ssize_t oldpath_size, write_size;
770 buffer = rpath(fs_ctx, newpath);
771 fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS);
776 /* Write the oldpath (target) to the file. */
777 oldpath_size = strlen(oldpath);
779 write_size = write(fd, (void *)oldpath, oldpath_size);
780 } while (write_size == -1 && errno == EINTR);
782 if (write_size != oldpath_size) {
789 /* Set cleint credentials in symlink's xattr */
790 credp->fc_mode = credp->fc_mode|S_IFLNK;
791 err = local_set_mapped_file_attr(fs_ctx, newpath, credp);
796 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
797 (fs_ctx->export_flags & V9FS_SM_NONE)) {
798 buffer = rpath(fs_ctx, newpath);
799 err = symlink(oldpath, buffer);
803 err = lchown(buffer, credp->fc_uid, credp->fc_gid);
806 * If we fail to change ownership and if we are
807 * using security model none. Ignore the error
809 if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
823 v9fs_string_free(&fullname);
827 static int local_link(FsContext *ctx, V9fsPath *oldpath,
828 V9fsPath *dirpath, const char *name)
832 char *buffer, *buffer1;
834 v9fs_string_init(&newpath);
835 v9fs_string_sprintf(&newpath, "%s/%s", dirpath->data, name);
837 buffer = rpath(ctx, oldpath->data);
838 buffer1 = rpath(ctx, newpath.data);
839 ret = link(buffer, buffer1);
843 /* now link the virtfs_metadata files */
844 if (!ret && (ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
845 /* Link the .virtfs_metadata files. Create the metada directory */
846 ret = local_create_mapped_attr_dir(ctx, newpath.data);
850 buffer = local_mapped_attr_path(ctx, oldpath->data);
851 buffer1 = local_mapped_attr_path(ctx, newpath.data);
852 ret = link(buffer, buffer1);
855 if (ret < 0 && errno != ENOENT) {
860 v9fs_string_free(&newpath);
864 static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
868 char *path = fs_path->data;
870 buffer = rpath(ctx, path);
871 ret = truncate(buffer, size);
876 static int local_rename(FsContext *ctx, const char *oldpath,
880 char *buffer, *buffer1;
882 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
883 err = local_create_mapped_attr_dir(ctx, newpath);
887 /* rename the .virtfs_metadata files */
888 buffer = local_mapped_attr_path(ctx, oldpath);
889 buffer1 = local_mapped_attr_path(ctx, newpath);
890 err = rename(buffer, buffer1);
893 if (err < 0 && errno != ENOENT) {
898 buffer = rpath(ctx, oldpath);
899 buffer1 = rpath(ctx, newpath);
900 err = rename(buffer, buffer1);
906 static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
910 char *path = fs_path->data;
912 if ((credp->fc_uid == -1 && credp->fc_gid == -1) ||
913 (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
914 (fs_ctx->export_flags & V9FS_SM_NONE)) {
915 buffer = rpath(fs_ctx, path);
916 ret = lchown(buffer, credp->fc_uid, credp->fc_gid);
918 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
919 buffer = rpath(fs_ctx, path);
920 ret = local_set_xattr(buffer, credp);
922 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
923 return local_set_mapped_file_attr(fs_ctx, path, credp);
928 static int local_utimensat(FsContext *s, V9fsPath *fs_path,
929 const struct timespec *buf)
933 char *path = fs_path->data;
935 buffer = rpath(s, path);
936 ret = qemu_utimens(buffer, buf);
941 static int local_remove(FsContext *ctx, const char *path)
947 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
948 buffer = rpath(ctx, path);
949 err = lstat(buffer, &stbuf);
955 * If directory remove .virtfs_metadata contained in the
958 if (S_ISDIR(stbuf.st_mode)) {
959 buffer = g_strdup_printf("%s/%s/%s", ctx->fs_root,
960 path, VIRTFS_META_DIR);
961 err = remove(buffer);
963 if (err < 0 && errno != ENOENT) {
965 * We didn't had the .virtfs_metadata file. May be file created
966 * in non-mapped mode ?. Ignore ENOENT.
972 * Now remove the name from parent directory
973 * .virtfs_metadata directory
975 buffer = local_mapped_attr_path(ctx, path);
976 err = remove(buffer);
978 if (err < 0 && errno != ENOENT) {
980 * We didn't had the .virtfs_metadata file. May be file created
981 * in non-mapped mode ?. Ignore ENOENT.
987 buffer = rpath(ctx, path);
988 err = remove(buffer);
994 static int local_fsync(FsContext *ctx, int fid_type,
995 V9fsFidOpenState *fs, int datasync)
999 if (fid_type == P9_FID_DIR) {
1000 fd = dirfd(fs->dir);
1006 return qemu_fdatasync(fd);
1012 static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
1016 char *path = fs_path->data;
1018 buffer = rpath(s, path);
1019 ret = statfs(buffer, stbuf);
1024 static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
1025 const char *name, void *value, size_t size)
1027 char *path = fs_path->data;
1029 return v9fs_get_xattr(ctx, path, name, value, size);
1032 static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path,
1033 void *value, size_t size)
1035 char *path = fs_path->data;
1037 return v9fs_list_xattr(ctx, path, value, size);
1040 static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
1041 void *value, size_t size, int flags)
1043 char *path = fs_path->data;
1045 return v9fs_set_xattr(ctx, path, name, value, size, flags);
1048 static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
1051 char *path = fs_path->data;
1053 return v9fs_remove_xattr(ctx, path, name);
1056 static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path,
1057 const char *name, V9fsPath *target)
1060 v9fs_string_sprintf((V9fsString *)target, "%s/%s",
1061 dir_path->data, name);
1063 v9fs_string_sprintf((V9fsString *)target, "%s", name);
1065 /* Bump the size for including terminating NULL */
1070 static int local_renameat(FsContext *ctx, V9fsPath *olddir,
1071 const char *old_name, V9fsPath *newdir,
1072 const char *new_name)
1075 V9fsString old_full_name, new_full_name;
1077 v9fs_string_init(&old_full_name);
1078 v9fs_string_init(&new_full_name);
1080 v9fs_string_sprintf(&old_full_name, "%s/%s", olddir->data, old_name);
1081 v9fs_string_sprintf(&new_full_name, "%s/%s", newdir->data, new_name);
1083 ret = local_rename(ctx, old_full_name.data, new_full_name.data);
1084 v9fs_string_free(&old_full_name);
1085 v9fs_string_free(&new_full_name);
1089 static int local_unlinkat(FsContext *ctx, V9fsPath *dir,
1090 const char *name, int flags)
1093 V9fsString fullname;
1096 v9fs_string_init(&fullname);
1098 v9fs_string_sprintf(&fullname, "%s/%s", dir->data, name);
1099 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1100 if (flags == AT_REMOVEDIR) {
1102 * If directory remove .virtfs_metadata contained in the
1105 buffer = g_strdup_printf("%s/%s/%s", ctx->fs_root,
1106 fullname.data, VIRTFS_META_DIR);
1107 ret = remove(buffer);
1109 if (ret < 0 && errno != ENOENT) {
1111 * We didn't had the .virtfs_metadata file. May be file created
1112 * in non-mapped mode ?. Ignore ENOENT.
1118 * Now remove the name from parent directory
1119 * .virtfs_metadata directory.
1121 buffer = local_mapped_attr_path(ctx, fullname.data);
1122 ret = remove(buffer);
1124 if (ret < 0 && errno != ENOENT) {
1126 * We didn't had the .virtfs_metadata file. May be file created
1127 * in non-mapped mode ?. Ignore ENOENT.
1132 /* Remove the name finally */
1133 buffer = rpath(ctx, fullname.data);
1134 ret = remove(buffer);
1138 v9fs_string_free(&fullname);
1142 static int local_ioc_getversion(FsContext *ctx, V9fsPath *path,
1143 mode_t st_mode, uint64_t *st_gen)
1145 #ifdef FS_IOC_GETVERSION
1147 V9fsFidOpenState fid_open;
1150 * Do not try to open special files like device nodes, fifos etc
1151 * We can get fd for regular files and directories only
1153 if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
1157 err = local_open(ctx, path, O_RDONLY, &fid_open);
1161 err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
1162 local_close(ctx, &fid_open);
1170 static int local_init(FsContext *ctx)
1173 struct statfs stbuf;
1175 if (ctx->export_flags & V9FS_SM_PASSTHROUGH) {
1176 ctx->xops = passthrough_xattr_ops;
1177 } else if (ctx->export_flags & V9FS_SM_MAPPED) {
1178 ctx->xops = mapped_xattr_ops;
1179 } else if (ctx->export_flags & V9FS_SM_NONE) {
1180 ctx->xops = none_xattr_ops;
1181 } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1183 * xattr operation for mapped-file and passthrough
1186 ctx->xops = passthrough_xattr_ops;
1188 ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT;
1189 #ifdef FS_IOC_GETVERSION
1191 * use ioc_getversion only if the iocl is definied
1193 err = statfs(ctx->fs_root, &stbuf);
1195 switch (stbuf.f_type) {
1196 case EXT2_SUPER_MAGIC:
1197 case BTRFS_SUPER_MAGIC:
1198 case REISERFS_SUPER_MAGIC:
1199 case XFS_SUPER_MAGIC:
1200 ctx->exops.get_st_gen = local_ioc_getversion;
1208 static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse)
1210 const char *sec_model = qemu_opt_get(opts, "security_model");
1211 const char *path = qemu_opt_get(opts, "path");
1214 error_report("Security model not specified, local fs needs security model");
1215 error_printf("valid options are:"
1216 "\tsecurity_model=[passthrough|mapped-xattr|mapped-file|none]\n");
1220 if (!strcmp(sec_model, "passthrough")) {
1221 fse->export_flags |= V9FS_SM_PASSTHROUGH;
1222 } else if (!strcmp(sec_model, "mapped") ||
1223 !strcmp(sec_model, "mapped-xattr")) {
1224 fse->export_flags |= V9FS_SM_MAPPED;
1225 } else if (!strcmp(sec_model, "none")) {
1226 fse->export_flags |= V9FS_SM_NONE;
1227 } else if (!strcmp(sec_model, "mapped-file")) {
1228 fse->export_flags |= V9FS_SM_MAPPED_FILE;
1230 error_report("Invalid security model %s specified", sec_model);
1231 error_printf("valid options are:"
1232 "\t[passthrough|mapped-xattr|mapped-file|none]\n");
1237 error_report("fsdev: No path specified");
1240 fse->path = g_strdup(path);
1245 FileOperations local_ops = {
1246 .parse_opts = local_parse_opts,
1248 .lstat = local_lstat,
1249 .readlink = local_readlink,
1250 .close = local_close,
1251 .closedir = local_closedir,
1253 .opendir = local_opendir,
1254 .rewinddir = local_rewinddir,
1255 .telldir = local_telldir,
1256 .readdir_r = local_readdir_r,
1257 .seekdir = local_seekdir,
1258 .preadv = local_preadv,
1259 .pwritev = local_pwritev,
1260 .chmod = local_chmod,
1261 .mknod = local_mknod,
1262 .mkdir = local_mkdir,
1263 .fstat = local_fstat,
1264 .open2 = local_open2,
1265 .symlink = local_symlink,
1267 .truncate = local_truncate,
1268 .rename = local_rename,
1269 .chown = local_chown,
1270 .utimensat = local_utimensat,
1271 .remove = local_remove,
1272 .fsync = local_fsync,
1273 .statfs = local_statfs,
1274 .lgetxattr = local_lgetxattr,
1275 .llistxattr = local_llistxattr,
1276 .lsetxattr = local_lsetxattr,
1277 .lremovexattr = local_lremovexattr,
1278 .name_to_path = local_name_to_path,
1279 .renameat = local_renameat,
1280 .unlinkat = local_unlinkat,