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"
19 #include "fsdev/qemu-fsdev.h" /* local_ops */
20 #include <arpa/inet.h>
23 #include <sys/socket.h>
25 #include "qemu/xattr.h"
26 #include "qemu/cutils.h"
27 #include "qemu/error-report.h"
30 #ifdef CONFIG_LINUX_MAGIC_H
31 #include <linux/magic.h>
33 #include <sys/ioctl.h>
35 #ifndef XFS_SUPER_MAGIC
36 #define XFS_SUPER_MAGIC 0x58465342
38 #ifndef EXT2_SUPER_MAGIC
39 #define EXT2_SUPER_MAGIC 0xEF53
41 #ifndef REISERFS_SUPER_MAGIC
42 #define REISERFS_SUPER_MAGIC 0x52654973
44 #ifndef BTRFS_SUPER_MAGIC
45 #define BTRFS_SUPER_MAGIC 0x9123683E
52 int local_open_nofollow(FsContext *fs_ctx, const char *path, int flags,
55 LocalData *data = fs_ctx->private;
57 /* All paths are relative to the path data->mountfd points to */
58 while (*path == '/') {
62 return relative_openat_nofollow(data->mountfd, path, flags, mode);
65 int local_opendir_nofollow(FsContext *fs_ctx, const char *path)
67 return local_open_nofollow(fs_ctx, path, O_DIRECTORY | O_RDONLY, 0);
70 #define VIRTFS_META_DIR ".virtfs_metadata"
72 static char *local_mapped_attr_path(FsContext *ctx, const char *path)
75 const char *name = strrchr(path, '/');
83 return g_strdup_printf("%s/%.*s/%s/%s", ctx->fs_root,
84 dirlen, path, VIRTFS_META_DIR, name);
87 static FILE *local_fopen(const char *path, const char *mode)
91 int flags = O_NOFOLLOW;
93 * only supports two modes
97 } else if (mode[0] == 'w') {
98 flags |= O_WRONLY | O_TRUNC | O_CREAT;
99 o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
103 fd = open(path, flags, o_mode);
107 fp = fdopen(fd, mode);
114 static FILE *local_fopenat(int dirfd, const char *name, const char *mode)
120 * only supports two modes
122 if (mode[0] == 'r') {
124 } else if (mode[0] == 'w') {
125 flags = O_WRONLY | O_TRUNC | O_CREAT;
126 o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
130 fd = openat_file(dirfd, name, flags, o_mode);
134 fp = fdopen(fd, mode);
142 static void local_mapped_file_attr(int dirfd, const char *name,
149 map_dirfd = openat(dirfd, VIRTFS_META_DIR,
150 O_RDONLY | O_DIRECTORY | O_NOFOLLOW);
151 if (map_dirfd == -1) {
155 fp = local_fopenat(map_dirfd, name, "r");
156 close_preserve_errno(map_dirfd);
160 memset(buf, 0, ATTR_MAX);
161 while (fgets(buf, ATTR_MAX, fp)) {
162 if (!strncmp(buf, "virtfs.uid", 10)) {
163 stbuf->st_uid = atoi(buf+11);
164 } else if (!strncmp(buf, "virtfs.gid", 10)) {
165 stbuf->st_gid = atoi(buf+11);
166 } else if (!strncmp(buf, "virtfs.mode", 11)) {
167 stbuf->st_mode = atoi(buf+12);
168 } else if (!strncmp(buf, "virtfs.rdev", 11)) {
169 stbuf->st_rdev = atoi(buf+12);
171 memset(buf, 0, ATTR_MAX);
176 static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf)
179 char *dirpath = g_path_get_dirname(fs_path->data);
180 char *name = g_path_get_basename(fs_path->data);
183 dirfd = local_opendir_nofollow(fs_ctx, dirpath);
188 err = fstatat(dirfd, name, stbuf, AT_SYMLINK_NOFOLLOW);
192 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
193 /* Actual credentials are part of extended attrs */
199 if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.uid", &tmp_uid,
200 sizeof(uid_t)) > 0) {
201 stbuf->st_uid = le32_to_cpu(tmp_uid);
203 if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.gid", &tmp_gid,
204 sizeof(gid_t)) > 0) {
205 stbuf->st_gid = le32_to_cpu(tmp_gid);
207 if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.mode", &tmp_mode,
208 sizeof(mode_t)) > 0) {
209 stbuf->st_mode = le32_to_cpu(tmp_mode);
211 if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.rdev", &tmp_dev,
212 sizeof(dev_t)) > 0) {
213 stbuf->st_rdev = le64_to_cpu(tmp_dev);
215 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
216 local_mapped_file_attr(dirfd, name, stbuf);
220 close_preserve_errno(dirfd);
227 static int local_create_mapped_attr_dir(FsContext *ctx, const char *path)
231 char *tmp_path = g_strdup(path);
233 attr_dir = g_strdup_printf("%s/%s/%s",
234 ctx->fs_root, dirname(tmp_path), VIRTFS_META_DIR);
236 err = mkdir(attr_dir, 0700);
237 if (err < 0 && errno == EEXIST) {
245 static int local_set_mapped_file_attr(FsContext *ctx,
246 const char *path, FsCred *credp)
252 int uid = -1, gid = -1, mode = -1, rdev = -1;
254 attr_path = local_mapped_attr_path(ctx, path);
255 fp = local_fopen(attr_path, "r");
257 goto create_map_file;
259 memset(buf, 0, ATTR_MAX);
260 while (fgets(buf, ATTR_MAX, fp)) {
261 if (!strncmp(buf, "virtfs.uid", 10)) {
263 } else if (!strncmp(buf, "virtfs.gid", 10)) {
265 } else if (!strncmp(buf, "virtfs.mode", 11)) {
267 } else if (!strncmp(buf, "virtfs.rdev", 11)) {
270 memset(buf, 0, ATTR_MAX);
273 goto update_map_file;
276 ret = local_create_mapped_attr_dir(ctx, path);
282 fp = local_fopen(attr_path, "w");
288 if (credp->fc_uid != -1) {
291 if (credp->fc_gid != -1) {
294 if (credp->fc_mode != -1) {
295 mode = credp->fc_mode;
297 if (credp->fc_rdev != -1) {
298 rdev = credp->fc_rdev;
303 fprintf(fp, "virtfs.uid=%d\n", uid);
306 fprintf(fp, "virtfs.gid=%d\n", gid);
309 fprintf(fp, "virtfs.mode=%d\n", mode);
312 fprintf(fp, "virtfs.rdev=%d\n", rdev);
321 static int local_set_xattr(const char *path, FsCred *credp)
325 if (credp->fc_uid != -1) {
326 uint32_t tmp_uid = cpu_to_le32(credp->fc_uid);
327 err = setxattr(path, "user.virtfs.uid", &tmp_uid, sizeof(uid_t), 0);
332 if (credp->fc_gid != -1) {
333 uint32_t tmp_gid = cpu_to_le32(credp->fc_gid);
334 err = setxattr(path, "user.virtfs.gid", &tmp_gid, sizeof(gid_t), 0);
339 if (credp->fc_mode != -1) {
340 uint32_t tmp_mode = cpu_to_le32(credp->fc_mode);
341 err = setxattr(path, "user.virtfs.mode", &tmp_mode, sizeof(mode_t), 0);
346 if (credp->fc_rdev != -1) {
347 uint64_t tmp_rdev = cpu_to_le64(credp->fc_rdev);
348 err = setxattr(path, "user.virtfs.rdev", &tmp_rdev, sizeof(dev_t), 0);
356 static int local_post_create_passthrough(FsContext *fs_ctx, const char *path,
361 buffer = rpath(fs_ctx, path);
362 if (lchown(buffer, credp->fc_uid, credp->fc_gid) < 0) {
364 * If we fail to change ownership and if we are
365 * using security model none. Ignore the error
367 if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
372 if (chmod(buffer, credp->fc_mode & 07777) < 0) {
383 static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
384 char *buf, size_t bufsz)
388 if ((fs_ctx->export_flags & V9FS_SM_MAPPED) ||
389 (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
392 fd = local_open_nofollow(fs_ctx, fs_path->data, O_RDONLY, 0);
397 tsize = read(fd, (void *)buf, bufsz);
398 } while (tsize == -1 && errno == EINTR);
399 close_preserve_errno(fd);
400 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
401 (fs_ctx->export_flags & V9FS_SM_NONE)) {
402 char *dirpath = g_path_get_dirname(fs_path->data);
403 char *name = g_path_get_basename(fs_path->data);
406 dirfd = local_opendir_nofollow(fs_ctx, dirpath);
411 tsize = readlinkat(dirfd, name, buf, bufsz);
412 close_preserve_errno(dirfd);
420 static int local_close(FsContext *ctx, V9fsFidOpenState *fs)
422 return close(fs->fd);
425 static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs)
427 return closedir(fs->dir.stream);
430 static int local_open(FsContext *ctx, V9fsPath *fs_path,
431 int flags, V9fsFidOpenState *fs)
435 fd = local_open_nofollow(ctx, fs_path->data, flags, 0);
443 static int local_opendir(FsContext *ctx,
444 V9fsPath *fs_path, V9fsFidOpenState *fs)
449 dirfd = local_opendir_nofollow(ctx, fs_path->data);
454 stream = fdopendir(dirfd);
458 fs->dir.stream = stream;
462 static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
464 rewinddir(fs->dir.stream);
467 static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs)
469 return telldir(fs->dir.stream);
472 static struct dirent *local_readdir(FsContext *ctx, V9fsFidOpenState *fs)
474 struct dirent *entry;
477 entry = readdir(fs->dir.stream);
482 if (ctx->export_flags & V9FS_SM_MAPPED) {
483 entry->d_type = DT_UNKNOWN;
484 } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
485 if (!strcmp(entry->d_name, VIRTFS_META_DIR)) {
486 /* skp the meta data directory */
489 entry->d_type = DT_UNKNOWN;
495 static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
497 seekdir(fs->dir.stream, off);
500 static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs,
501 const struct iovec *iov,
502 int iovcnt, off_t offset)
505 return preadv(fs->fd, iov, iovcnt, offset);
507 int err = lseek(fs->fd, offset, SEEK_SET);
511 return readv(fs->fd, iov, iovcnt);
516 static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
517 const struct iovec *iov,
518 int iovcnt, off_t offset)
522 ret = pwritev(fs->fd, iov, iovcnt, offset);
524 int err = lseek(fs->fd, offset, SEEK_SET);
528 ret = writev(fs->fd, iov, iovcnt);
531 #ifdef CONFIG_SYNC_FILE_RANGE
532 if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
534 * Initiate a writeback. This is not a data integrity sync.
535 * We want to ensure that we don't leave dirty pages in the cache
536 * after write when writeout=immediate is sepcified.
538 sync_file_range(fs->fd, offset, ret,
539 SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
545 static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
549 char *path = fs_path->data;
551 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
552 buffer = rpath(fs_ctx, path);
553 ret = local_set_xattr(buffer, credp);
555 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
556 return local_set_mapped_file_attr(fs_ctx, path, credp);
557 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
558 (fs_ctx->export_flags & V9FS_SM_NONE)) {
559 buffer = rpath(fs_ctx, path);
560 ret = chmod(buffer, credp->fc_mode);
566 static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
567 const char *name, FsCred *credp)
575 v9fs_string_init(&fullname);
576 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
577 path = fullname.data;
579 /* Determine the security model */
580 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
581 buffer = rpath(fs_ctx, path);
582 err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0);
586 err = local_set_xattr(buffer, credp);
591 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
593 buffer = rpath(fs_ctx, path);
594 err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0);
598 err = local_set_mapped_file_attr(fs_ctx, path, credp);
603 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
604 (fs_ctx->export_flags & V9FS_SM_NONE)) {
605 buffer = rpath(fs_ctx, path);
606 err = mknod(buffer, credp->fc_mode, credp->fc_rdev);
610 err = local_post_create_passthrough(fs_ctx, path, credp);
623 v9fs_string_free(&fullname);
627 static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
628 const char *name, FsCred *credp)
636 v9fs_string_init(&fullname);
637 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
638 path = fullname.data;
640 /* Determine the security model */
641 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
642 buffer = rpath(fs_ctx, path);
643 err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
647 credp->fc_mode = credp->fc_mode|S_IFDIR;
648 err = local_set_xattr(buffer, credp);
653 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
654 buffer = rpath(fs_ctx, path);
655 err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
659 credp->fc_mode = credp->fc_mode|S_IFDIR;
660 err = local_set_mapped_file_attr(fs_ctx, path, credp);
665 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
666 (fs_ctx->export_flags & V9FS_SM_NONE)) {
667 buffer = rpath(fs_ctx, path);
668 err = mkdir(buffer, credp->fc_mode);
672 err = local_post_create_passthrough(fs_ctx, path, credp);
685 v9fs_string_free(&fullname);
689 static int local_fstat(FsContext *fs_ctx, int fid_type,
690 V9fsFidOpenState *fs, struct stat *stbuf)
694 if (fid_type == P9_FID_DIR) {
695 fd = dirfd(fs->dir.stream);
700 err = fstat(fd, stbuf);
704 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
705 /* Actual credentials are part of extended attrs */
711 if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) {
712 stbuf->st_uid = le32_to_cpu(tmp_uid);
714 if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
715 stbuf->st_gid = le32_to_cpu(tmp_gid);
717 if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) {
718 stbuf->st_mode = le32_to_cpu(tmp_mode);
720 if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
721 stbuf->st_rdev = le64_to_cpu(tmp_dev);
723 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
730 static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
731 int flags, FsCred *credp, V9fsFidOpenState *fs)
741 * Mark all the open to not follow symlinks
745 v9fs_string_init(&fullname);
746 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
747 path = fullname.data;
749 /* Determine the security model */
750 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
751 buffer = rpath(fs_ctx, path);
752 fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
757 credp->fc_mode = credp->fc_mode|S_IFREG;
758 /* Set cleint credentials in xattr */
759 err = local_set_xattr(buffer, credp);
764 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
765 buffer = rpath(fs_ctx, path);
766 fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
771 credp->fc_mode = credp->fc_mode|S_IFREG;
772 /* Set client credentials in .virtfs_metadata directory files */
773 err = local_set_mapped_file_attr(fs_ctx, path, credp);
778 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
779 (fs_ctx->export_flags & V9FS_SM_NONE)) {
780 buffer = rpath(fs_ctx, path);
781 fd = open(buffer, flags, credp->fc_mode);
786 err = local_post_create_passthrough(fs_ctx, path, credp);
802 v9fs_string_free(&fullname);
807 static int local_symlink(FsContext *fs_ctx, const char *oldpath,
808 V9fsPath *dir_path, const char *name, FsCred *credp)
816 v9fs_string_init(&fullname);
817 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
818 newpath = fullname.data;
820 /* Determine the security model */
821 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
823 ssize_t oldpath_size, write_size;
824 buffer = rpath(fs_ctx, newpath);
825 fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS);
830 /* Write the oldpath (target) to the file. */
831 oldpath_size = strlen(oldpath);
833 write_size = write(fd, (void *)oldpath, oldpath_size);
834 } while (write_size == -1 && errno == EINTR);
836 if (write_size != oldpath_size) {
843 /* Set cleint credentials in symlink's xattr */
844 credp->fc_mode = credp->fc_mode|S_IFLNK;
845 err = local_set_xattr(buffer, credp);
850 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
852 ssize_t oldpath_size, write_size;
853 buffer = rpath(fs_ctx, newpath);
854 fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS);
859 /* Write the oldpath (target) to the file. */
860 oldpath_size = strlen(oldpath);
862 write_size = write(fd, (void *)oldpath, oldpath_size);
863 } while (write_size == -1 && errno == EINTR);
865 if (write_size != oldpath_size) {
872 /* Set cleint credentials in symlink's xattr */
873 credp->fc_mode = credp->fc_mode|S_IFLNK;
874 err = local_set_mapped_file_attr(fs_ctx, newpath, credp);
879 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
880 (fs_ctx->export_flags & V9FS_SM_NONE)) {
881 buffer = rpath(fs_ctx, newpath);
882 err = symlink(oldpath, buffer);
886 err = lchown(buffer, credp->fc_uid, credp->fc_gid);
889 * If we fail to change ownership and if we are
890 * using security model none. Ignore the error
892 if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
906 v9fs_string_free(&fullname);
910 static int local_link(FsContext *ctx, V9fsPath *oldpath,
911 V9fsPath *dirpath, const char *name)
915 char *buffer, *buffer1;
917 v9fs_string_init(&newpath);
918 v9fs_string_sprintf(&newpath, "%s/%s", dirpath->data, name);
920 buffer = rpath(ctx, oldpath->data);
921 buffer1 = rpath(ctx, newpath.data);
922 ret = link(buffer, buffer1);
926 /* now link the virtfs_metadata files */
927 if (!ret && (ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
928 /* Link the .virtfs_metadata files. Create the metada directory */
929 ret = local_create_mapped_attr_dir(ctx, newpath.data);
933 buffer = local_mapped_attr_path(ctx, oldpath->data);
934 buffer1 = local_mapped_attr_path(ctx, newpath.data);
935 ret = link(buffer, buffer1);
938 if (ret < 0 && errno != ENOENT) {
943 v9fs_string_free(&newpath);
947 static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
951 fd = local_open_nofollow(ctx, fs_path->data, O_WRONLY, 0);
955 ret = ftruncate(fd, size);
956 close_preserve_errno(fd);
960 static int local_rename(FsContext *ctx, const char *oldpath,
964 char *buffer, *buffer1;
966 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
967 err = local_create_mapped_attr_dir(ctx, newpath);
971 /* rename the .virtfs_metadata files */
972 buffer = local_mapped_attr_path(ctx, oldpath);
973 buffer1 = local_mapped_attr_path(ctx, newpath);
974 err = rename(buffer, buffer1);
977 if (err < 0 && errno != ENOENT) {
982 buffer = rpath(ctx, oldpath);
983 buffer1 = rpath(ctx, newpath);
984 err = rename(buffer, buffer1);
990 static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
994 char *path = fs_path->data;
996 if ((credp->fc_uid == -1 && credp->fc_gid == -1) ||
997 (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
998 (fs_ctx->export_flags & V9FS_SM_NONE)) {
999 buffer = rpath(fs_ctx, path);
1000 ret = lchown(buffer, credp->fc_uid, credp->fc_gid);
1002 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
1003 buffer = rpath(fs_ctx, path);
1004 ret = local_set_xattr(buffer, credp);
1006 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1007 return local_set_mapped_file_attr(fs_ctx, path, credp);
1012 static int local_utimensat(FsContext *s, V9fsPath *fs_path,
1013 const struct timespec *buf)
1015 char *dirpath = g_path_get_dirname(fs_path->data);
1016 char *name = g_path_get_basename(fs_path->data);
1017 int dirfd, ret = -1;
1019 dirfd = local_opendir_nofollow(s, dirpath);
1024 ret = utimensat(dirfd, name, buf, AT_SYMLINK_NOFOLLOW);
1025 close_preserve_errno(dirfd);
1032 static int local_unlinkat_common(FsContext *ctx, int dirfd, const char *name,
1037 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1040 if (flags == AT_REMOVEDIR) {
1043 fd = openat(dirfd, name, O_RDONLY | O_DIRECTORY | O_PATH);
1048 * If directory remove .virtfs_metadata contained in the
1051 ret = unlinkat(fd, VIRTFS_META_DIR, AT_REMOVEDIR);
1052 close_preserve_errno(fd);
1053 if (ret < 0 && errno != ENOENT) {
1055 * We didn't had the .virtfs_metadata file. May be file created
1056 * in non-mapped mode ?. Ignore ENOENT.
1062 * Now remove the name from parent directory
1063 * .virtfs_metadata directory.
1065 map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
1066 ret = unlinkat(map_dirfd, name, 0);
1067 close_preserve_errno(map_dirfd);
1068 if (ret < 0 && errno != ENOENT) {
1070 * We didn't had the .virtfs_metadata file. May be file created
1071 * in non-mapped mode ?. Ignore ENOENT.
1077 ret = unlinkat(dirfd, name, flags);
1082 static int local_remove(FsContext *ctx, const char *path)
1085 char *dirpath = g_path_get_dirname(path);
1086 char *name = g_path_get_basename(path);
1091 dirfd = local_opendir_nofollow(ctx, dirpath);
1096 if (fstatat(dirfd, path, &stbuf, AT_SYMLINK_NOFOLLOW) < 0) {
1100 if (S_ISDIR(stbuf.st_mode)) {
1101 flags |= AT_REMOVEDIR;
1104 err = local_unlinkat_common(ctx, dirfd, name, flags);
1106 close_preserve_errno(dirfd);
1113 static int local_fsync(FsContext *ctx, int fid_type,
1114 V9fsFidOpenState *fs, int datasync)
1118 if (fid_type == P9_FID_DIR) {
1119 fd = dirfd(fs->dir.stream);
1125 return qemu_fdatasync(fd);
1131 static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
1135 fd = local_open_nofollow(s, fs_path->data, O_RDONLY, 0);
1136 ret = fstatfs(fd, stbuf);
1137 close_preserve_errno(fd);
1141 static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
1142 const char *name, void *value, size_t size)
1144 char *path = fs_path->data;
1146 return v9fs_get_xattr(ctx, path, name, value, size);
1149 static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path,
1150 void *value, size_t size)
1152 char *path = fs_path->data;
1154 return v9fs_list_xattr(ctx, path, value, size);
1157 static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
1158 void *value, size_t size, int flags)
1160 char *path = fs_path->data;
1162 return v9fs_set_xattr(ctx, path, name, value, size, flags);
1165 static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
1168 char *path = fs_path->data;
1170 return v9fs_remove_xattr(ctx, path, name);
1173 static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path,
1174 const char *name, V9fsPath *target)
1177 v9fs_path_sprintf(target, "%s/%s", dir_path->data, name);
1179 v9fs_path_sprintf(target, "%s", name);
1184 static int local_renameat(FsContext *ctx, V9fsPath *olddir,
1185 const char *old_name, V9fsPath *newdir,
1186 const char *new_name)
1189 V9fsString old_full_name, new_full_name;
1191 v9fs_string_init(&old_full_name);
1192 v9fs_string_init(&new_full_name);
1194 v9fs_string_sprintf(&old_full_name, "%s/%s", olddir->data, old_name);
1195 v9fs_string_sprintf(&new_full_name, "%s/%s", newdir->data, new_name);
1197 ret = local_rename(ctx, old_full_name.data, new_full_name.data);
1198 v9fs_string_free(&old_full_name);
1199 v9fs_string_free(&new_full_name);
1203 static int local_unlinkat(FsContext *ctx, V9fsPath *dir,
1204 const char *name, int flags)
1209 dirfd = local_opendir_nofollow(ctx, dir->data);
1214 ret = local_unlinkat_common(ctx, dirfd, name, flags);
1215 close_preserve_errno(dirfd);
1219 static int local_ioc_getversion(FsContext *ctx, V9fsPath *path,
1220 mode_t st_mode, uint64_t *st_gen)
1222 #ifdef FS_IOC_GETVERSION
1224 V9fsFidOpenState fid_open;
1227 * Do not try to open special files like device nodes, fifos etc
1228 * We can get fd for regular files and directories only
1230 if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
1234 err = local_open(ctx, path, O_RDONLY, &fid_open);
1238 err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
1239 local_close(ctx, &fid_open);
1247 static int local_init(FsContext *ctx)
1249 struct statfs stbuf;
1250 LocalData *data = g_malloc(sizeof(*data));
1252 data->mountfd = open(ctx->fs_root, O_DIRECTORY | O_RDONLY);
1253 if (data->mountfd == -1) {
1257 #ifdef FS_IOC_GETVERSION
1259 * use ioc_getversion only if the ioctl is definied
1261 if (fstatfs(data->mountfd, &stbuf) < 0) {
1262 close_preserve_errno(data->mountfd);
1265 switch (stbuf.f_type) {
1266 case EXT2_SUPER_MAGIC:
1267 case BTRFS_SUPER_MAGIC:
1268 case REISERFS_SUPER_MAGIC:
1269 case XFS_SUPER_MAGIC:
1270 ctx->exops.get_st_gen = local_ioc_getversion;
1275 if (ctx->export_flags & V9FS_SM_PASSTHROUGH) {
1276 ctx->xops = passthrough_xattr_ops;
1277 } else if (ctx->export_flags & V9FS_SM_MAPPED) {
1278 ctx->xops = mapped_xattr_ops;
1279 } else if (ctx->export_flags & V9FS_SM_NONE) {
1280 ctx->xops = none_xattr_ops;
1281 } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1283 * xattr operation for mapped-file and passthrough
1286 ctx->xops = passthrough_xattr_ops;
1288 ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT;
1290 ctx->private = data;
1298 static void local_cleanup(FsContext *ctx)
1300 LocalData *data = ctx->private;
1302 close(data->mountfd);
1306 static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse)
1308 const char *sec_model = qemu_opt_get(opts, "security_model");
1309 const char *path = qemu_opt_get(opts, "path");
1312 error_report("Security model not specified, local fs needs security model");
1313 error_printf("valid options are:"
1314 "\tsecurity_model=[passthrough|mapped-xattr|mapped-file|none]\n");
1318 if (!strcmp(sec_model, "passthrough")) {
1319 fse->export_flags |= V9FS_SM_PASSTHROUGH;
1320 } else if (!strcmp(sec_model, "mapped") ||
1321 !strcmp(sec_model, "mapped-xattr")) {
1322 fse->export_flags |= V9FS_SM_MAPPED;
1323 } else if (!strcmp(sec_model, "none")) {
1324 fse->export_flags |= V9FS_SM_NONE;
1325 } else if (!strcmp(sec_model, "mapped-file")) {
1326 fse->export_flags |= V9FS_SM_MAPPED_FILE;
1328 error_report("Invalid security model %s specified", sec_model);
1329 error_printf("valid options are:"
1330 "\t[passthrough|mapped-xattr|mapped-file|none]\n");
1335 error_report("fsdev: No path specified");
1338 fse->path = g_strdup(path);
1343 FileOperations local_ops = {
1344 .parse_opts = local_parse_opts,
1346 .cleanup = local_cleanup,
1347 .lstat = local_lstat,
1348 .readlink = local_readlink,
1349 .close = local_close,
1350 .closedir = local_closedir,
1352 .opendir = local_opendir,
1353 .rewinddir = local_rewinddir,
1354 .telldir = local_telldir,
1355 .readdir = local_readdir,
1356 .seekdir = local_seekdir,
1357 .preadv = local_preadv,
1358 .pwritev = local_pwritev,
1359 .chmod = local_chmod,
1360 .mknod = local_mknod,
1361 .mkdir = local_mkdir,
1362 .fstat = local_fstat,
1363 .open2 = local_open2,
1364 .symlink = local_symlink,
1366 .truncate = local_truncate,
1367 .rename = local_rename,
1368 .chown = local_chown,
1369 .utimensat = local_utimensat,
1370 .remove = local_remove,
1371 .fsync = local_fsync,
1372 .statfs = local_statfs,
1373 .lgetxattr = local_lgetxattr,
1374 .llistxattr = local_llistxattr,
1375 .lsetxattr = local_lsetxattr,
1376 .lremovexattr = local_lremovexattr,
1377 .name_to_path = local_name_to_path,
1378 .renameat = local_renameat,
1379 .unlinkat = local_unlinkat,