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);
354 static int local_open(FsContext *ctx, V9fsPath *fs_path,
355 int flags, V9fsFidOpenState *fs)
358 char *path = fs_path->data;
360 buffer = rpath(ctx, path);
361 fs->fd = open(buffer, flags | O_NOFOLLOW);
366 static int local_opendir(FsContext *ctx,
367 V9fsPath *fs_path, V9fsFidOpenState *fs)
370 char *path = fs_path->data;
372 buffer = rpath(ctx, path);
373 fs->dir = opendir(buffer);
381 static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
386 static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs)
388 return telldir(fs->dir);
391 static int local_readdir_r(FsContext *ctx, V9fsFidOpenState *fs,
392 struct dirent *entry,
393 struct dirent **result)
398 ret = readdir_r(fs->dir, entry, result);
399 if (ctx->export_flags & V9FS_SM_MAPPED) {
400 entry->d_type = DT_UNKNOWN;
401 } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
402 if (!ret && *result != NULL &&
403 !strcmp(entry->d_name, VIRTFS_META_DIR)) {
404 /* skp the meta data directory */
407 entry->d_type = DT_UNKNOWN;
412 static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
414 seekdir(fs->dir, off);
417 static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs,
418 const struct iovec *iov,
419 int iovcnt, off_t offset)
422 return preadv(fs->fd, iov, iovcnt, offset);
424 int err = lseek(fs->fd, offset, SEEK_SET);
428 return readv(fs->fd, iov, iovcnt);
433 static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
434 const struct iovec *iov,
435 int iovcnt, off_t offset)
440 ret = pwritev(fs->fd, iov, iovcnt, offset);
442 int err = lseek(fs->fd, offset, SEEK_SET);
446 ret = writev(fs->fd, iov, iovcnt);
449 #ifdef CONFIG_SYNC_FILE_RANGE
450 if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
452 * Initiate a writeback. This is not a data integrity sync.
453 * We want to ensure that we don't leave dirty pages in the cache
454 * after write when writeout=immediate is sepcified.
456 sync_file_range(fs->fd, offset, ret,
457 SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
463 static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
467 char *path = fs_path->data;
469 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
470 buffer = rpath(fs_ctx, path);
471 ret = local_set_xattr(buffer, credp);
473 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
474 return local_set_mapped_file_attr(fs_ctx, path, credp);
475 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
476 (fs_ctx->export_flags & V9FS_SM_NONE)) {
477 buffer = rpath(fs_ctx, path);
478 ret = chmod(buffer, credp->fc_mode);
484 static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
485 const char *name, FsCred *credp)
493 v9fs_string_init(&fullname);
494 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
495 path = fullname.data;
497 /* Determine the security model */
498 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
499 buffer = rpath(fs_ctx, path);
500 err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0);
504 err = local_set_xattr(buffer, credp);
509 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
511 buffer = rpath(fs_ctx, path);
512 err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0);
516 err = local_set_mapped_file_attr(fs_ctx, path, credp);
521 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
522 (fs_ctx->export_flags & V9FS_SM_NONE)) {
523 buffer = rpath(fs_ctx, path);
524 err = mknod(buffer, credp->fc_mode, credp->fc_rdev);
528 err = local_post_create_passthrough(fs_ctx, path, credp);
541 v9fs_string_free(&fullname);
545 static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
546 const char *name, FsCred *credp)
554 v9fs_string_init(&fullname);
555 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
556 path = fullname.data;
558 /* Determine the security model */
559 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
560 buffer = rpath(fs_ctx, path);
561 err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
565 credp->fc_mode = credp->fc_mode|S_IFDIR;
566 err = local_set_xattr(buffer, credp);
571 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
572 buffer = rpath(fs_ctx, path);
573 err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
577 credp->fc_mode = credp->fc_mode|S_IFDIR;
578 err = local_set_mapped_file_attr(fs_ctx, path, credp);
583 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
584 (fs_ctx->export_flags & V9FS_SM_NONE)) {
585 buffer = rpath(fs_ctx, path);
586 err = mkdir(buffer, credp->fc_mode);
590 err = local_post_create_passthrough(fs_ctx, path, credp);
603 v9fs_string_free(&fullname);
607 static int local_fstat(FsContext *fs_ctx, int fid_type,
608 V9fsFidOpenState *fs, struct stat *stbuf)
612 if (fid_type == P9_FID_DIR) {
618 err = fstat(fd, stbuf);
622 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
623 /* Actual credentials are part of extended attrs */
629 if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) {
630 stbuf->st_uid = le32_to_cpu(tmp_uid);
632 if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
633 stbuf->st_gid = le32_to_cpu(tmp_gid);
635 if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) {
636 stbuf->st_mode = le32_to_cpu(tmp_mode);
638 if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
639 stbuf->st_rdev = le64_to_cpu(tmp_dev);
641 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
648 static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
649 int flags, FsCred *credp, V9fsFidOpenState *fs)
659 * Mark all the open to not follow symlinks
663 v9fs_string_init(&fullname);
664 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
665 path = fullname.data;
667 /* Determine the security model */
668 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
669 buffer = rpath(fs_ctx, path);
670 fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
675 credp->fc_mode = credp->fc_mode|S_IFREG;
676 /* Set cleint credentials in xattr */
677 err = local_set_xattr(buffer, credp);
682 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
683 buffer = rpath(fs_ctx, path);
684 fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
689 credp->fc_mode = credp->fc_mode|S_IFREG;
690 /* Set client credentials in .virtfs_metadata directory files */
691 err = local_set_mapped_file_attr(fs_ctx, path, credp);
696 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
697 (fs_ctx->export_flags & V9FS_SM_NONE)) {
698 buffer = rpath(fs_ctx, path);
699 fd = open(buffer, flags, credp->fc_mode);
704 err = local_post_create_passthrough(fs_ctx, path, credp);
720 v9fs_string_free(&fullname);
725 static int local_symlink(FsContext *fs_ctx, const char *oldpath,
726 V9fsPath *dir_path, const char *name, FsCred *credp)
734 v9fs_string_init(&fullname);
735 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
736 newpath = fullname.data;
738 /* Determine the security model */
739 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
741 ssize_t oldpath_size, write_size;
742 buffer = rpath(fs_ctx, newpath);
743 fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS);
748 /* Write the oldpath (target) to the file. */
749 oldpath_size = strlen(oldpath);
751 write_size = write(fd, (void *)oldpath, oldpath_size);
752 } while (write_size == -1 && errno == EINTR);
754 if (write_size != oldpath_size) {
761 /* Set cleint credentials in symlink's xattr */
762 credp->fc_mode = credp->fc_mode|S_IFLNK;
763 err = local_set_xattr(buffer, credp);
768 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
770 ssize_t oldpath_size, write_size;
771 buffer = rpath(fs_ctx, newpath);
772 fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS);
777 /* Write the oldpath (target) to the file. */
778 oldpath_size = strlen(oldpath);
780 write_size = write(fd, (void *)oldpath, oldpath_size);
781 } while (write_size == -1 && errno == EINTR);
783 if (write_size != oldpath_size) {
790 /* Set cleint credentials in symlink's xattr */
791 credp->fc_mode = credp->fc_mode|S_IFLNK;
792 err = local_set_mapped_file_attr(fs_ctx, newpath, credp);
797 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
798 (fs_ctx->export_flags & V9FS_SM_NONE)) {
799 buffer = rpath(fs_ctx, newpath);
800 err = symlink(oldpath, buffer);
804 err = lchown(buffer, credp->fc_uid, credp->fc_gid);
807 * If we fail to change ownership and if we are
808 * using security model none. Ignore the error
810 if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
824 v9fs_string_free(&fullname);
828 static int local_link(FsContext *ctx, V9fsPath *oldpath,
829 V9fsPath *dirpath, const char *name)
833 char *buffer, *buffer1;
835 v9fs_string_init(&newpath);
836 v9fs_string_sprintf(&newpath, "%s/%s", dirpath->data, name);
838 buffer = rpath(ctx, oldpath->data);
839 buffer1 = rpath(ctx, newpath.data);
840 ret = link(buffer, buffer1);
844 /* now link the virtfs_metadata files */
845 if (!ret && (ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
846 /* Link the .virtfs_metadata files. Create the metada directory */
847 ret = local_create_mapped_attr_dir(ctx, newpath.data);
851 buffer = local_mapped_attr_path(ctx, oldpath->data);
852 buffer1 = local_mapped_attr_path(ctx, newpath.data);
853 ret = link(buffer, buffer1);
856 if (ret < 0 && errno != ENOENT) {
861 v9fs_string_free(&newpath);
865 static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
869 char *path = fs_path->data;
871 buffer = rpath(ctx, path);
872 ret = truncate(buffer, size);
877 static int local_rename(FsContext *ctx, const char *oldpath,
881 char *buffer, *buffer1;
883 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
884 err = local_create_mapped_attr_dir(ctx, newpath);
888 /* rename the .virtfs_metadata files */
889 buffer = local_mapped_attr_path(ctx, oldpath);
890 buffer1 = local_mapped_attr_path(ctx, newpath);
891 err = rename(buffer, buffer1);
894 if (err < 0 && errno != ENOENT) {
899 buffer = rpath(ctx, oldpath);
900 buffer1 = rpath(ctx, newpath);
901 err = rename(buffer, buffer1);
907 static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
911 char *path = fs_path->data;
913 if ((credp->fc_uid == -1 && credp->fc_gid == -1) ||
914 (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
915 (fs_ctx->export_flags & V9FS_SM_NONE)) {
916 buffer = rpath(fs_ctx, path);
917 ret = lchown(buffer, credp->fc_uid, credp->fc_gid);
919 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
920 buffer = rpath(fs_ctx, path);
921 ret = local_set_xattr(buffer, credp);
923 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
924 return local_set_mapped_file_attr(fs_ctx, path, credp);
929 static int local_utimensat(FsContext *s, V9fsPath *fs_path,
930 const struct timespec *buf)
934 char *path = fs_path->data;
936 buffer = rpath(s, path);
937 ret = qemu_utimens(buffer, buf);
942 static int local_remove(FsContext *ctx, const char *path)
948 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
949 buffer = rpath(ctx, path);
950 err = lstat(buffer, &stbuf);
956 * If directory remove .virtfs_metadata contained in the
959 if (S_ISDIR(stbuf.st_mode)) {
960 buffer = g_strdup_printf("%s/%s/%s", ctx->fs_root,
961 path, VIRTFS_META_DIR);
962 err = remove(buffer);
964 if (err < 0 && errno != ENOENT) {
966 * We didn't had the .virtfs_metadata file. May be file created
967 * in non-mapped mode ?. Ignore ENOENT.
973 * Now remove the name from parent directory
974 * .virtfs_metadata directory
976 buffer = local_mapped_attr_path(ctx, path);
977 err = remove(buffer);
979 if (err < 0 && errno != ENOENT) {
981 * We didn't had the .virtfs_metadata file. May be file created
982 * in non-mapped mode ?. Ignore ENOENT.
988 buffer = rpath(ctx, path);
989 err = remove(buffer);
995 static int local_fsync(FsContext *ctx, int fid_type,
996 V9fsFidOpenState *fs, int datasync)
1000 if (fid_type == P9_FID_DIR) {
1001 fd = dirfd(fs->dir);
1007 return qemu_fdatasync(fd);
1013 static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
1017 char *path = fs_path->data;
1019 buffer = rpath(s, path);
1020 ret = statfs(buffer, stbuf);
1025 static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
1026 const char *name, void *value, size_t size)
1028 char *path = fs_path->data;
1030 return v9fs_get_xattr(ctx, path, name, value, size);
1033 static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path,
1034 void *value, size_t size)
1036 char *path = fs_path->data;
1038 return v9fs_list_xattr(ctx, path, value, size);
1041 static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
1042 void *value, size_t size, int flags)
1044 char *path = fs_path->data;
1046 return v9fs_set_xattr(ctx, path, name, value, size, flags);
1049 static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
1052 char *path = fs_path->data;
1054 return v9fs_remove_xattr(ctx, path, name);
1057 static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path,
1058 const char *name, V9fsPath *target)
1061 v9fs_string_sprintf((V9fsString *)target, "%s/%s",
1062 dir_path->data, name);
1064 v9fs_string_sprintf((V9fsString *)target, "%s", name);
1066 /* Bump the size for including terminating NULL */
1071 static int local_renameat(FsContext *ctx, V9fsPath *olddir,
1072 const char *old_name, V9fsPath *newdir,
1073 const char *new_name)
1076 V9fsString old_full_name, new_full_name;
1078 v9fs_string_init(&old_full_name);
1079 v9fs_string_init(&new_full_name);
1081 v9fs_string_sprintf(&old_full_name, "%s/%s", olddir->data, old_name);
1082 v9fs_string_sprintf(&new_full_name, "%s/%s", newdir->data, new_name);
1084 ret = local_rename(ctx, old_full_name.data, new_full_name.data);
1085 v9fs_string_free(&old_full_name);
1086 v9fs_string_free(&new_full_name);
1090 static int local_unlinkat(FsContext *ctx, V9fsPath *dir,
1091 const char *name, int flags)
1094 V9fsString fullname;
1097 v9fs_string_init(&fullname);
1099 v9fs_string_sprintf(&fullname, "%s/%s", dir->data, name);
1100 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1101 if (flags == AT_REMOVEDIR) {
1103 * If directory remove .virtfs_metadata contained in the
1106 buffer = g_strdup_printf("%s/%s/%s", ctx->fs_root,
1107 fullname.data, VIRTFS_META_DIR);
1108 ret = remove(buffer);
1110 if (ret < 0 && errno != ENOENT) {
1112 * We didn't had the .virtfs_metadata file. May be file created
1113 * in non-mapped mode ?. Ignore ENOENT.
1119 * Now remove the name from parent directory
1120 * .virtfs_metadata directory.
1122 buffer = local_mapped_attr_path(ctx, fullname.data);
1123 ret = remove(buffer);
1125 if (ret < 0 && errno != ENOENT) {
1127 * We didn't had the .virtfs_metadata file. May be file created
1128 * in non-mapped mode ?. Ignore ENOENT.
1133 /* Remove the name finally */
1134 buffer = rpath(ctx, fullname.data);
1135 ret = remove(buffer);
1139 v9fs_string_free(&fullname);
1143 static int local_ioc_getversion(FsContext *ctx, V9fsPath *path,
1144 mode_t st_mode, uint64_t *st_gen)
1146 #ifdef FS_IOC_GETVERSION
1148 V9fsFidOpenState fid_open;
1151 * Do not try to open special files like device nodes, fifos etc
1152 * We can get fd for regular files and directories only
1154 if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
1158 err = local_open(ctx, path, O_RDONLY, &fid_open);
1162 err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
1163 local_close(ctx, &fid_open);
1171 static int local_init(FsContext *ctx)
1174 struct statfs stbuf;
1176 if (ctx->export_flags & V9FS_SM_PASSTHROUGH) {
1177 ctx->xops = passthrough_xattr_ops;
1178 } else if (ctx->export_flags & V9FS_SM_MAPPED) {
1179 ctx->xops = mapped_xattr_ops;
1180 } else if (ctx->export_flags & V9FS_SM_NONE) {
1181 ctx->xops = none_xattr_ops;
1182 } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1184 * xattr operation for mapped-file and passthrough
1187 ctx->xops = passthrough_xattr_ops;
1189 ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT;
1190 #ifdef FS_IOC_GETVERSION
1192 * use ioc_getversion only if the iocl is definied
1194 err = statfs(ctx->fs_root, &stbuf);
1196 switch (stbuf.f_type) {
1197 case EXT2_SUPER_MAGIC:
1198 case BTRFS_SUPER_MAGIC:
1199 case REISERFS_SUPER_MAGIC:
1200 case XFS_SUPER_MAGIC:
1201 ctx->exops.get_st_gen = local_ioc_getversion;
1209 static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse)
1211 const char *sec_model = qemu_opt_get(opts, "security_model");
1212 const char *path = qemu_opt_get(opts, "path");
1215 error_report("Security model not specified, local fs needs security model");
1216 error_printf("valid options are:"
1217 "\tsecurity_model=[passthrough|mapped-xattr|mapped-file|none]\n");
1221 if (!strcmp(sec_model, "passthrough")) {
1222 fse->export_flags |= V9FS_SM_PASSTHROUGH;
1223 } else if (!strcmp(sec_model, "mapped") ||
1224 !strcmp(sec_model, "mapped-xattr")) {
1225 fse->export_flags |= V9FS_SM_MAPPED;
1226 } else if (!strcmp(sec_model, "none")) {
1227 fse->export_flags |= V9FS_SM_NONE;
1228 } else if (!strcmp(sec_model, "mapped-file")) {
1229 fse->export_flags |= V9FS_SM_MAPPED_FILE;
1231 error_report("Invalid security model %s specified", sec_model);
1232 error_printf("valid options are:"
1233 "\t[passthrough|mapped-xattr|mapped-file|none]\n");
1238 error_report("fsdev: No path specified");
1241 fse->path = g_strdup(path);
1246 FileOperations local_ops = {
1247 .parse_opts = local_parse_opts,
1249 .lstat = local_lstat,
1250 .readlink = local_readlink,
1251 .close = local_close,
1252 .closedir = local_closedir,
1254 .opendir = local_opendir,
1255 .rewinddir = local_rewinddir,
1256 .telldir = local_telldir,
1257 .readdir_r = local_readdir_r,
1258 .seekdir = local_seekdir,
1259 .preadv = local_preadv,
1260 .pwritev = local_pwritev,
1261 .chmod = local_chmod,
1262 .mknod = local_mknod,
1263 .mkdir = local_mkdir,
1264 .fstat = local_fstat,
1265 .open2 = local_open2,
1266 .symlink = local_symlink,
1268 .truncate = local_truncate,
1269 .rename = local_rename,
1270 .chown = local_chown,
1271 .utimensat = local_utimensat,
1272 .remove = local_remove,
1273 .fsync = local_fsync,
1274 .statfs = local_statfs,
1275 .lgetxattr = local_lgetxattr,
1276 .llistxattr = local_llistxattr,
1277 .lsetxattr = local_lsetxattr,
1278 .lremovexattr = local_lremovexattr,
1279 .name_to_path = local_name_to_path,
1280 .renameat = local_renameat,
1281 .unlinkat = local_unlinkat,