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/cutils.h"
25 #include "qemu/error-report.h"
28 #ifdef CONFIG_LINUX_MAGIC_H
29 #include <linux/magic.h>
31 #include <sys/ioctl.h>
33 #ifndef XFS_SUPER_MAGIC
34 #define XFS_SUPER_MAGIC 0x58465342
36 #ifndef EXT2_SUPER_MAGIC
37 #define EXT2_SUPER_MAGIC 0xEF53
39 #ifndef REISERFS_SUPER_MAGIC
40 #define REISERFS_SUPER_MAGIC 0x52654973
42 #ifndef BTRFS_SUPER_MAGIC
43 #define BTRFS_SUPER_MAGIC 0x9123683E
46 #define VIRTFS_META_DIR ".virtfs_metadata"
48 static char *local_mapped_attr_path(FsContext *ctx, const char *path)
51 const char *name = strrchr(path, '/');
59 return g_strdup_printf("%s/%.*s/%s/%s", ctx->fs_root,
60 dirlen, path, VIRTFS_META_DIR, name);
63 static FILE *local_fopen(const char *path, const char *mode)
67 int flags = O_NOFOLLOW;
69 * only supports two modes
73 } else if (mode[0] == 'w') {
74 flags |= O_WRONLY | O_TRUNC | O_CREAT;
75 o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
79 fd = open(path, flags, o_mode);
83 fp = fdopen(fd, mode);
91 static void local_mapped_file_attr(FsContext *ctx, const char *path,
98 attr_path = local_mapped_attr_path(ctx, path);
99 fp = local_fopen(attr_path, "r");
104 memset(buf, 0, ATTR_MAX);
105 while (fgets(buf, ATTR_MAX, fp)) {
106 if (!strncmp(buf, "virtfs.uid", 10)) {
107 stbuf->st_uid = atoi(buf+11);
108 } else if (!strncmp(buf, "virtfs.gid", 10)) {
109 stbuf->st_gid = atoi(buf+11);
110 } else if (!strncmp(buf, "virtfs.mode", 11)) {
111 stbuf->st_mode = atoi(buf+12);
112 } else if (!strncmp(buf, "virtfs.rdev", 11)) {
113 stbuf->st_rdev = atoi(buf+12);
115 memset(buf, 0, ATTR_MAX);
120 static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf)
124 char *path = fs_path->data;
126 buffer = rpath(fs_ctx, path);
127 err = lstat(buffer, stbuf);
131 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
132 /* Actual credentials are part of extended attrs */
137 if (getxattr(buffer, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) {
138 stbuf->st_uid = le32_to_cpu(tmp_uid);
140 if (getxattr(buffer, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
141 stbuf->st_gid = le32_to_cpu(tmp_gid);
143 if (getxattr(buffer, "user.virtfs.mode",
144 &tmp_mode, sizeof(mode_t)) > 0) {
145 stbuf->st_mode = le32_to_cpu(tmp_mode);
147 if (getxattr(buffer, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
148 stbuf->st_rdev = le64_to_cpu(tmp_dev);
150 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
151 local_mapped_file_attr(fs_ctx, path, stbuf);
159 static int local_create_mapped_attr_dir(FsContext *ctx, const char *path)
163 char *tmp_path = g_strdup(path);
165 attr_dir = g_strdup_printf("%s/%s/%s",
166 ctx->fs_root, dirname(tmp_path), VIRTFS_META_DIR);
168 err = mkdir(attr_dir, 0700);
169 if (err < 0 && errno == EEXIST) {
177 static int local_set_mapped_file_attr(FsContext *ctx,
178 const char *path, FsCred *credp)
184 int uid = -1, gid = -1, mode = -1, rdev = -1;
186 attr_path = local_mapped_attr_path(ctx, path);
187 fp = local_fopen(attr_path, "r");
189 goto create_map_file;
191 memset(buf, 0, ATTR_MAX);
192 while (fgets(buf, ATTR_MAX, fp)) {
193 if (!strncmp(buf, "virtfs.uid", 10)) {
195 } else if (!strncmp(buf, "virtfs.gid", 10)) {
197 } else if (!strncmp(buf, "virtfs.mode", 11)) {
199 } else if (!strncmp(buf, "virtfs.rdev", 11)) {
202 memset(buf, 0, ATTR_MAX);
205 goto update_map_file;
208 ret = local_create_mapped_attr_dir(ctx, path);
214 fp = local_fopen(attr_path, "w");
220 if (credp->fc_uid != -1) {
223 if (credp->fc_gid != -1) {
226 if (credp->fc_mode != -1) {
227 mode = credp->fc_mode;
229 if (credp->fc_rdev != -1) {
230 rdev = credp->fc_rdev;
235 fprintf(fp, "virtfs.uid=%d\n", uid);
238 fprintf(fp, "virtfs.gid=%d\n", gid);
241 fprintf(fp, "virtfs.mode=%d\n", mode);
244 fprintf(fp, "virtfs.rdev=%d\n", rdev);
253 static int local_set_xattr(const char *path, FsCred *credp)
257 if (credp->fc_uid != -1) {
258 uint32_t tmp_uid = cpu_to_le32(credp->fc_uid);
259 err = setxattr(path, "user.virtfs.uid", &tmp_uid, sizeof(uid_t), 0);
264 if (credp->fc_gid != -1) {
265 uint32_t tmp_gid = cpu_to_le32(credp->fc_gid);
266 err = setxattr(path, "user.virtfs.gid", &tmp_gid, sizeof(gid_t), 0);
271 if (credp->fc_mode != -1) {
272 uint32_t tmp_mode = cpu_to_le32(credp->fc_mode);
273 err = setxattr(path, "user.virtfs.mode", &tmp_mode, sizeof(mode_t), 0);
278 if (credp->fc_rdev != -1) {
279 uint64_t tmp_rdev = cpu_to_le64(credp->fc_rdev);
280 err = setxattr(path, "user.virtfs.rdev", &tmp_rdev, sizeof(dev_t), 0);
288 static int local_post_create_passthrough(FsContext *fs_ctx, const char *path,
293 buffer = rpath(fs_ctx, path);
294 if (lchown(buffer, credp->fc_uid, credp->fc_gid) < 0) {
296 * If we fail to change ownership and if we are
297 * using security model none. Ignore the error
299 if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
304 if (chmod(buffer, credp->fc_mode & 07777) < 0) {
315 static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
316 char *buf, size_t bufsz)
320 char *path = fs_path->data;
322 if ((fs_ctx->export_flags & V9FS_SM_MAPPED) ||
323 (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
325 buffer = rpath(fs_ctx, path);
326 fd = open(buffer, O_RDONLY | O_NOFOLLOW);
332 tsize = read(fd, (void *)buf, bufsz);
333 } while (tsize == -1 && errno == EINTR);
335 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
336 (fs_ctx->export_flags & V9FS_SM_NONE)) {
337 buffer = rpath(fs_ctx, path);
338 tsize = readlink(buffer, buf, bufsz);
344 static int local_close(FsContext *ctx, V9fsFidOpenState *fs)
346 return close(fs->fd);
349 static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs)
351 return closedir(fs->dir.stream);
354 static int local_open(FsContext *ctx, V9fsPath *fs_path,
355 int flags, V9fsFidOpenState *fs)
358 char *path = fs_path->data;
360 buffer = rpath(ctx, path);
361 fs->fd = open(buffer, flags | O_NOFOLLOW);
366 static int local_opendir(FsContext *ctx,
367 V9fsPath *fs_path, V9fsFidOpenState *fs)
370 char *path = fs_path->data;
372 buffer = rpath(ctx, path);
373 fs->dir.stream = opendir(buffer);
375 if (!fs->dir.stream) {
381 static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
383 rewinddir(fs->dir.stream);
386 static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs)
388 return telldir(fs->dir.stream);
391 static struct dirent *local_readdir(FsContext *ctx, V9fsFidOpenState *fs)
393 struct dirent *entry;
396 entry = readdir(fs->dir.stream);
401 if (ctx->export_flags & V9FS_SM_MAPPED) {
402 entry->d_type = DT_UNKNOWN;
403 } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
404 if (!strcmp(entry->d_name, VIRTFS_META_DIR)) {
405 /* skp the meta data directory */
408 entry->d_type = DT_UNKNOWN;
414 static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
416 seekdir(fs->dir.stream, off);
419 static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs,
420 const struct iovec *iov,
421 int iovcnt, off_t offset)
424 return preadv(fs->fd, iov, iovcnt, offset);
426 int err = lseek(fs->fd, offset, SEEK_SET);
430 return readv(fs->fd, iov, iovcnt);
435 static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
436 const struct iovec *iov,
437 int iovcnt, off_t offset)
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);
505 err = local_set_xattr(buffer, credp);
510 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
512 buffer = rpath(fs_ctx, path);
513 err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0);
517 err = local_set_mapped_file_attr(fs_ctx, path, credp);
522 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
523 (fs_ctx->export_flags & V9FS_SM_NONE)) {
524 buffer = rpath(fs_ctx, path);
525 err = mknod(buffer, credp->fc_mode, credp->fc_rdev);
529 err = local_post_create_passthrough(fs_ctx, path, credp);
542 v9fs_string_free(&fullname);
546 static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
547 const char *name, FsCred *credp)
555 v9fs_string_init(&fullname);
556 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
557 path = fullname.data;
559 /* Determine the security model */
560 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
561 buffer = rpath(fs_ctx, path);
562 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);
578 credp->fc_mode = credp->fc_mode|S_IFDIR;
579 err = local_set_mapped_file_attr(fs_ctx, path, credp);
584 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
585 (fs_ctx->export_flags & V9FS_SM_NONE)) {
586 buffer = rpath(fs_ctx, path);
587 err = mkdir(buffer, credp->fc_mode);
591 err = local_post_create_passthrough(fs_ctx, path, credp);
604 v9fs_string_free(&fullname);
608 static int local_fstat(FsContext *fs_ctx, int fid_type,
609 V9fsFidOpenState *fs, struct stat *stbuf)
613 if (fid_type == P9_FID_DIR) {
614 fd = dirfd(fs->dir.stream);
619 err = fstat(fd, stbuf);
623 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
624 /* Actual credentials are part of extended attrs */
630 if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) {
631 stbuf->st_uid = le32_to_cpu(tmp_uid);
633 if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
634 stbuf->st_gid = le32_to_cpu(tmp_gid);
636 if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) {
637 stbuf->st_mode = le32_to_cpu(tmp_mode);
639 if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
640 stbuf->st_rdev = le64_to_cpu(tmp_dev);
642 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
649 static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
650 int flags, FsCred *credp, V9fsFidOpenState *fs)
660 * Mark all the open to not follow symlinks
664 v9fs_string_init(&fullname);
665 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
666 path = fullname.data;
668 /* Determine the security model */
669 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
670 buffer = rpath(fs_ctx, path);
671 fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
676 credp->fc_mode = credp->fc_mode|S_IFREG;
677 /* Set cleint credentials in xattr */
678 err = local_set_xattr(buffer, credp);
683 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
684 buffer = rpath(fs_ctx, path);
685 fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
690 credp->fc_mode = credp->fc_mode|S_IFREG;
691 /* Set client credentials in .virtfs_metadata directory files */
692 err = local_set_mapped_file_attr(fs_ctx, path, credp);
697 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
698 (fs_ctx->export_flags & V9FS_SM_NONE)) {
699 buffer = rpath(fs_ctx, path);
700 fd = open(buffer, flags, credp->fc_mode);
705 err = local_post_create_passthrough(fs_ctx, path, credp);
721 v9fs_string_free(&fullname);
726 static int local_symlink(FsContext *fs_ctx, const char *oldpath,
727 V9fsPath *dir_path, const char *name, FsCred *credp)
735 v9fs_string_init(&fullname);
736 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
737 newpath = fullname.data;
739 /* Determine the security model */
740 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
742 ssize_t oldpath_size, write_size;
743 buffer = rpath(fs_ctx, newpath);
744 fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS);
749 /* Write the oldpath (target) to the file. */
750 oldpath_size = strlen(oldpath);
752 write_size = write(fd, (void *)oldpath, oldpath_size);
753 } while (write_size == -1 && errno == EINTR);
755 if (write_size != oldpath_size) {
762 /* Set cleint credentials in symlink's xattr */
763 credp->fc_mode = credp->fc_mode|S_IFLNK;
764 err = local_set_xattr(buffer, credp);
769 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
771 ssize_t oldpath_size, write_size;
772 buffer = rpath(fs_ctx, newpath);
773 fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS);
778 /* Write the oldpath (target) to the file. */
779 oldpath_size = strlen(oldpath);
781 write_size = write(fd, (void *)oldpath, oldpath_size);
782 } while (write_size == -1 && errno == EINTR);
784 if (write_size != oldpath_size) {
791 /* Set cleint credentials in symlink's xattr */
792 credp->fc_mode = credp->fc_mode|S_IFLNK;
793 err = local_set_mapped_file_attr(fs_ctx, newpath, credp);
798 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
799 (fs_ctx->export_flags & V9FS_SM_NONE)) {
800 buffer = rpath(fs_ctx, newpath);
801 err = symlink(oldpath, buffer);
805 err = lchown(buffer, credp->fc_uid, credp->fc_gid);
808 * If we fail to change ownership and if we are
809 * using security model none. Ignore the error
811 if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
825 v9fs_string_free(&fullname);
829 static int local_link(FsContext *ctx, V9fsPath *oldpath,
830 V9fsPath *dirpath, const char *name)
834 char *buffer, *buffer1;
836 v9fs_string_init(&newpath);
837 v9fs_string_sprintf(&newpath, "%s/%s", dirpath->data, name);
839 buffer = rpath(ctx, oldpath->data);
840 buffer1 = rpath(ctx, newpath.data);
841 ret = link(buffer, buffer1);
845 /* now link the virtfs_metadata files */
846 if (!ret && (ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
847 /* Link the .virtfs_metadata files. Create the metada directory */
848 ret = local_create_mapped_attr_dir(ctx, newpath.data);
852 buffer = local_mapped_attr_path(ctx, oldpath->data);
853 buffer1 = local_mapped_attr_path(ctx, newpath.data);
854 ret = link(buffer, buffer1);
857 if (ret < 0 && errno != ENOENT) {
862 v9fs_string_free(&newpath);
866 static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
870 char *path = fs_path->data;
872 buffer = rpath(ctx, path);
873 ret = truncate(buffer, size);
878 static int local_rename(FsContext *ctx, const char *oldpath,
882 char *buffer, *buffer1;
884 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
885 err = local_create_mapped_attr_dir(ctx, newpath);
889 /* rename the .virtfs_metadata files */
890 buffer = local_mapped_attr_path(ctx, oldpath);
891 buffer1 = local_mapped_attr_path(ctx, newpath);
892 err = rename(buffer, buffer1);
895 if (err < 0 && errno != ENOENT) {
900 buffer = rpath(ctx, oldpath);
901 buffer1 = rpath(ctx, newpath);
902 err = rename(buffer, buffer1);
908 static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
912 char *path = fs_path->data;
914 if ((credp->fc_uid == -1 && credp->fc_gid == -1) ||
915 (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
916 (fs_ctx->export_flags & V9FS_SM_NONE)) {
917 buffer = rpath(fs_ctx, path);
918 ret = lchown(buffer, credp->fc_uid, credp->fc_gid);
920 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
921 buffer = rpath(fs_ctx, path);
922 ret = local_set_xattr(buffer, credp);
924 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
925 return local_set_mapped_file_attr(fs_ctx, path, credp);
930 static int local_utimensat(FsContext *s, V9fsPath *fs_path,
931 const struct timespec *buf)
935 char *path = fs_path->data;
937 buffer = rpath(s, path);
938 ret = qemu_utimens(buffer, buf);
943 static int local_remove(FsContext *ctx, const char *path)
949 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
950 buffer = rpath(ctx, path);
951 err = lstat(buffer, &stbuf);
957 * If directory remove .virtfs_metadata contained in the
960 if (S_ISDIR(stbuf.st_mode)) {
961 buffer = g_strdup_printf("%s/%s/%s", ctx->fs_root,
962 path, VIRTFS_META_DIR);
963 err = remove(buffer);
965 if (err < 0 && errno != ENOENT) {
967 * We didn't had the .virtfs_metadata file. May be file created
968 * in non-mapped mode ?. Ignore ENOENT.
974 * Now remove the name from parent directory
975 * .virtfs_metadata directory
977 buffer = local_mapped_attr_path(ctx, path);
978 err = remove(buffer);
980 if (err < 0 && errno != ENOENT) {
982 * We didn't had the .virtfs_metadata file. May be file created
983 * in non-mapped mode ?. Ignore ENOENT.
989 buffer = rpath(ctx, path);
990 err = remove(buffer);
996 static int local_fsync(FsContext *ctx, int fid_type,
997 V9fsFidOpenState *fs, int datasync)
1001 if (fid_type == P9_FID_DIR) {
1002 fd = dirfd(fs->dir.stream);
1008 return qemu_fdatasync(fd);
1014 static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
1018 char *path = fs_path->data;
1020 buffer = rpath(s, path);
1021 ret = statfs(buffer, stbuf);
1026 static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
1027 const char *name, void *value, size_t size)
1029 char *path = fs_path->data;
1031 return v9fs_get_xattr(ctx, path, name, value, size);
1034 static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path,
1035 void *value, size_t size)
1037 char *path = fs_path->data;
1039 return v9fs_list_xattr(ctx, path, value, size);
1042 static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
1043 void *value, size_t size, int flags)
1045 char *path = fs_path->data;
1047 return v9fs_set_xattr(ctx, path, name, value, size, flags);
1050 static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
1053 char *path = fs_path->data;
1055 return v9fs_remove_xattr(ctx, path, name);
1058 static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path,
1059 const char *name, V9fsPath *target)
1062 v9fs_path_sprintf(target, "%s/%s", dir_path->data, name);
1064 v9fs_path_sprintf(target, "%s", name);
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 error_report("Security model not specified, local fs needs security model");
1214 error_printf("valid options are:"
1215 "\tsecurity_model=[passthrough|mapped-xattr|mapped-file|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 error_report("Invalid security model %s specified", sec_model);
1230 error_printf("valid options are:"
1231 "\t[passthrough|mapped-xattr|mapped-file|none]\n");
1236 error_report("fsdev: No path specified");
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 = local_readdir,
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,