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 static void renameat_preserve_errno(int odirfd, const char *opath, int ndirfd,
74 renameat(odirfd, opath, ndirfd, npath);
78 static void unlinkat_preserve_errno(int dirfd, const char *path, int flags)
81 unlinkat(dirfd, path, flags);
85 #define VIRTFS_META_DIR ".virtfs_metadata"
87 static FILE *local_fopenat(int dirfd, const char *name, const char *mode)
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 = openat_file(dirfd, name, flags, o_mode);
107 fp = fdopen(fd, mode);
115 static void local_mapped_file_attr(int dirfd, const char *name,
122 map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
123 if (map_dirfd == -1) {
127 fp = local_fopenat(map_dirfd, name, "r");
128 close_preserve_errno(map_dirfd);
132 memset(buf, 0, ATTR_MAX);
133 while (fgets(buf, ATTR_MAX, fp)) {
134 if (!strncmp(buf, "virtfs.uid", 10)) {
135 stbuf->st_uid = atoi(buf+11);
136 } else if (!strncmp(buf, "virtfs.gid", 10)) {
137 stbuf->st_gid = atoi(buf+11);
138 } else if (!strncmp(buf, "virtfs.mode", 11)) {
139 stbuf->st_mode = atoi(buf+12);
140 } else if (!strncmp(buf, "virtfs.rdev", 11)) {
141 stbuf->st_rdev = atoi(buf+12);
143 memset(buf, 0, ATTR_MAX);
148 static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf)
151 char *dirpath = g_path_get_dirname(fs_path->data);
152 char *name = g_path_get_basename(fs_path->data);
155 dirfd = local_opendir_nofollow(fs_ctx, dirpath);
160 err = fstatat(dirfd, name, stbuf, AT_SYMLINK_NOFOLLOW);
164 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
165 /* Actual credentials are part of extended attrs */
171 if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.uid", &tmp_uid,
172 sizeof(uid_t)) > 0) {
173 stbuf->st_uid = le32_to_cpu(tmp_uid);
175 if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.gid", &tmp_gid,
176 sizeof(gid_t)) > 0) {
177 stbuf->st_gid = le32_to_cpu(tmp_gid);
179 if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.mode", &tmp_mode,
180 sizeof(mode_t)) > 0) {
181 stbuf->st_mode = le32_to_cpu(tmp_mode);
183 if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.rdev", &tmp_dev,
184 sizeof(dev_t)) > 0) {
185 stbuf->st_rdev = le64_to_cpu(tmp_dev);
187 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
188 local_mapped_file_attr(dirfd, name, stbuf);
192 close_preserve_errno(dirfd);
199 static int local_set_mapped_file_attrat(int dirfd, const char *name,
205 int uid = -1, gid = -1, mode = -1, rdev = -1;
208 ret = mkdirat(dirfd, VIRTFS_META_DIR, 0700);
209 if (ret < 0 && errno != EEXIST) {
213 map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
214 if (map_dirfd == -1) {
218 fp = local_fopenat(map_dirfd, name, "r");
220 if (errno == ENOENT) {
221 goto update_map_file;
223 close_preserve_errno(map_dirfd);
227 memset(buf, 0, ATTR_MAX);
228 while (fgets(buf, ATTR_MAX, fp)) {
229 if (!strncmp(buf, "virtfs.uid", 10)) {
230 uid = atoi(buf + 11);
231 } else if (!strncmp(buf, "virtfs.gid", 10)) {
232 gid = atoi(buf + 11);
233 } else if (!strncmp(buf, "virtfs.mode", 11)) {
234 mode = atoi(buf + 12);
235 } else if (!strncmp(buf, "virtfs.rdev", 11)) {
236 rdev = atoi(buf + 12);
238 memset(buf, 0, ATTR_MAX);
243 fp = local_fopenat(map_dirfd, name, "w");
244 close_preserve_errno(map_dirfd);
249 if (credp->fc_uid != -1) {
252 if (credp->fc_gid != -1) {
255 if (credp->fc_mode != -1) {
256 mode = credp->fc_mode;
258 if (credp->fc_rdev != -1) {
259 rdev = credp->fc_rdev;
263 fprintf(fp, "virtfs.uid=%d\n", uid);
266 fprintf(fp, "virtfs.gid=%d\n", gid);
269 fprintf(fp, "virtfs.mode=%d\n", mode);
272 fprintf(fp, "virtfs.rdev=%d\n", rdev);
279 static int fchmodat_nofollow(int dirfd, const char *name, mode_t mode)
283 /* FIXME: this should be handled with fchmodat(AT_SYMLINK_NOFOLLOW).
284 * Unfortunately, the linux kernel doesn't implement it yet. As an
285 * alternative, let's open the file and use fchmod() instead. This
286 * may fail depending on the permissions of the file, but it is the
287 * best we can do to avoid TOCTTOU. We first try to open read-only
288 * in case name points to a directory. If that fails, we try write-only
289 * in case name doesn't point to a directory.
291 fd = openat_file(dirfd, name, O_RDONLY, 0);
293 /* In case the file is writable-only and isn't a directory. */
294 if (errno == EACCES) {
295 fd = openat_file(dirfd, name, O_WRONLY, 0);
297 if (fd == -1 && errno == EISDIR) {
304 ret = fchmod(fd, mode);
305 close_preserve_errno(fd);
309 static int local_set_xattrat(int dirfd, const char *path, FsCred *credp)
313 if (credp->fc_uid != -1) {
314 uint32_t tmp_uid = cpu_to_le32(credp->fc_uid);
315 err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.uid", &tmp_uid,
321 if (credp->fc_gid != -1) {
322 uint32_t tmp_gid = cpu_to_le32(credp->fc_gid);
323 err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.gid", &tmp_gid,
329 if (credp->fc_mode != -1) {
330 uint32_t tmp_mode = cpu_to_le32(credp->fc_mode);
331 err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.mode", &tmp_mode,
337 if (credp->fc_rdev != -1) {
338 uint64_t tmp_rdev = cpu_to_le64(credp->fc_rdev);
339 err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.rdev", &tmp_rdev,
348 static int local_set_cred_passthrough(FsContext *fs_ctx, int dirfd,
349 const char *name, FsCred *credp)
351 if (fchownat(dirfd, name, credp->fc_uid, credp->fc_gid,
352 AT_SYMLINK_NOFOLLOW) < 0) {
354 * If we fail to change ownership and if we are
355 * using security model none. Ignore the error
357 if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
362 return fchmodat_nofollow(dirfd, name, credp->fc_mode & 07777);
365 static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
366 char *buf, size_t bufsz)
370 if ((fs_ctx->export_flags & V9FS_SM_MAPPED) ||
371 (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
374 fd = local_open_nofollow(fs_ctx, fs_path->data, O_RDONLY, 0);
379 tsize = read(fd, (void *)buf, bufsz);
380 } while (tsize == -1 && errno == EINTR);
381 close_preserve_errno(fd);
382 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
383 (fs_ctx->export_flags & V9FS_SM_NONE)) {
384 char *dirpath = g_path_get_dirname(fs_path->data);
385 char *name = g_path_get_basename(fs_path->data);
388 dirfd = local_opendir_nofollow(fs_ctx, dirpath);
393 tsize = readlinkat(dirfd, name, buf, bufsz);
394 close_preserve_errno(dirfd);
402 static int local_close(FsContext *ctx, V9fsFidOpenState *fs)
404 return close(fs->fd);
407 static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs)
409 return closedir(fs->dir.stream);
412 static int local_open(FsContext *ctx, V9fsPath *fs_path,
413 int flags, V9fsFidOpenState *fs)
417 fd = local_open_nofollow(ctx, fs_path->data, flags, 0);
425 static int local_opendir(FsContext *ctx,
426 V9fsPath *fs_path, V9fsFidOpenState *fs)
431 dirfd = local_opendir_nofollow(ctx, fs_path->data);
436 stream = fdopendir(dirfd);
441 fs->dir.stream = stream;
445 static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
447 rewinddir(fs->dir.stream);
450 static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs)
452 return telldir(fs->dir.stream);
455 static bool local_is_mapped_file_metadata(FsContext *fs_ctx, const char *name)
457 return !strcmp(name, VIRTFS_META_DIR);
460 static struct dirent *local_readdir(FsContext *ctx, V9fsFidOpenState *fs)
462 struct dirent *entry;
465 entry = readdir(fs->dir.stream);
470 if (ctx->export_flags & V9FS_SM_MAPPED) {
471 entry->d_type = DT_UNKNOWN;
472 } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
473 if (local_is_mapped_file_metadata(ctx, entry->d_name)) {
474 /* skip the meta data directory */
477 entry->d_type = DT_UNKNOWN;
483 static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
485 seekdir(fs->dir.stream, off);
488 static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs,
489 const struct iovec *iov,
490 int iovcnt, off_t offset)
493 return preadv(fs->fd, iov, iovcnt, offset);
495 int err = lseek(fs->fd, offset, SEEK_SET);
499 return readv(fs->fd, iov, iovcnt);
504 static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
505 const struct iovec *iov,
506 int iovcnt, off_t offset)
510 ret = pwritev(fs->fd, iov, iovcnt, offset);
512 int err = lseek(fs->fd, offset, SEEK_SET);
516 ret = writev(fs->fd, iov, iovcnt);
519 #ifdef CONFIG_SYNC_FILE_RANGE
520 if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
522 * Initiate a writeback. This is not a data integrity sync.
523 * We want to ensure that we don't leave dirty pages in the cache
524 * after write when writeout=immediate is sepcified.
526 sync_file_range(fs->fd, offset, ret,
527 SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
533 static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
535 char *dirpath = g_path_get_dirname(fs_path->data);
536 char *name = g_path_get_basename(fs_path->data);
540 dirfd = local_opendir_nofollow(fs_ctx, dirpath);
545 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
546 ret = local_set_xattrat(dirfd, name, credp);
547 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
548 ret = local_set_mapped_file_attrat(dirfd, name, credp);
549 } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
550 fs_ctx->export_flags & V9FS_SM_NONE) {
551 ret = fchmodat_nofollow(dirfd, name, credp->fc_mode);
553 close_preserve_errno(dirfd);
561 static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
562 const char *name, FsCred *credp)
567 if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE &&
568 local_is_mapped_file_metadata(fs_ctx, name)) {
573 dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
578 if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
579 fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
580 err = mknodat(dirfd, name, SM_LOCAL_MODE_BITS | S_IFREG, 0);
585 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
586 err = local_set_xattrat(dirfd, name, credp);
588 err = local_set_mapped_file_attrat(dirfd, name, credp);
593 } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
594 fs_ctx->export_flags & V9FS_SM_NONE) {
595 err = mknodat(dirfd, name, credp->fc_mode, credp->fc_rdev);
599 err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp);
607 unlinkat_preserve_errno(dirfd, name, 0);
609 close_preserve_errno(dirfd);
613 static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
614 const char *name, FsCred *credp)
619 if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE &&
620 local_is_mapped_file_metadata(fs_ctx, name)) {
625 dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
630 if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
631 fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
632 err = mkdirat(dirfd, name, SM_LOCAL_DIR_MODE_BITS);
636 credp->fc_mode = credp->fc_mode | S_IFDIR;
638 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
639 err = local_set_xattrat(dirfd, name, credp);
641 err = local_set_mapped_file_attrat(dirfd, name, credp);
646 } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
647 fs_ctx->export_flags & V9FS_SM_NONE) {
648 err = mkdirat(dirfd, name, credp->fc_mode);
652 err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp);
660 unlinkat_preserve_errno(dirfd, name, AT_REMOVEDIR);
662 close_preserve_errno(dirfd);
666 static int local_fstat(FsContext *fs_ctx, int fid_type,
667 V9fsFidOpenState *fs, struct stat *stbuf)
671 if (fid_type == P9_FID_DIR) {
672 fd = dirfd(fs->dir.stream);
677 err = fstat(fd, stbuf);
681 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
682 /* Actual credentials are part of extended attrs */
688 if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) {
689 stbuf->st_uid = le32_to_cpu(tmp_uid);
691 if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
692 stbuf->st_gid = le32_to_cpu(tmp_gid);
694 if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) {
695 stbuf->st_mode = le32_to_cpu(tmp_mode);
697 if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
698 stbuf->st_rdev = le64_to_cpu(tmp_dev);
700 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
707 static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
708 int flags, FsCred *credp, V9fsFidOpenState *fs)
714 if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE &&
715 local_is_mapped_file_metadata(fs_ctx, name)) {
721 * Mark all the open to not follow symlinks
725 dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
730 /* Determine the security model */
731 if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
732 fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
733 fd = openat_file(dirfd, name, flags, SM_LOCAL_MODE_BITS);
737 credp->fc_mode = credp->fc_mode|S_IFREG;
738 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
739 /* Set cleint credentials in xattr */
740 err = local_set_xattrat(dirfd, name, credp);
742 err = local_set_mapped_file_attrat(dirfd, name, credp);
747 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
748 (fs_ctx->export_flags & V9FS_SM_NONE)) {
749 fd = openat_file(dirfd, name, flags, credp->fc_mode);
753 err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp);
763 unlinkat_preserve_errno(dirfd, name,
764 flags & O_DIRECTORY ? AT_REMOVEDIR : 0);
765 close_preserve_errno(fd);
767 close_preserve_errno(dirfd);
772 static int local_symlink(FsContext *fs_ctx, const char *oldpath,
773 V9fsPath *dir_path, const char *name, FsCred *credp)
778 if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE &&
779 local_is_mapped_file_metadata(fs_ctx, name)) {
784 dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
789 /* Determine the security model */
790 if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
791 fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
793 ssize_t oldpath_size, write_size;
795 fd = openat_file(dirfd, name, O_CREAT | O_EXCL | O_RDWR,
800 /* Write the oldpath (target) to the file. */
801 oldpath_size = strlen(oldpath);
803 write_size = write(fd, (void *)oldpath, oldpath_size);
804 } while (write_size == -1 && errno == EINTR);
805 close_preserve_errno(fd);
807 if (write_size != oldpath_size) {
810 /* Set cleint credentials in symlink's xattr */
811 credp->fc_mode = credp->fc_mode | S_IFLNK;
813 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
814 err = local_set_xattrat(dirfd, name, credp);
816 err = local_set_mapped_file_attrat(dirfd, name, credp);
821 } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
822 fs_ctx->export_flags & V9FS_SM_NONE) {
823 err = symlinkat(oldpath, dirfd, name);
827 err = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid,
828 AT_SYMLINK_NOFOLLOW);
831 * If we fail to change ownership and if we are
832 * using security model none. Ignore the error
834 if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
844 unlinkat_preserve_errno(dirfd, name, 0);
846 close_preserve_errno(dirfd);
850 static int local_link(FsContext *ctx, V9fsPath *oldpath,
851 V9fsPath *dirpath, const char *name)
853 char *odirpath = g_path_get_dirname(oldpath->data);
854 char *oname = g_path_get_basename(oldpath->data);
858 if (ctx->export_flags & V9FS_SM_MAPPED_FILE &&
859 local_is_mapped_file_metadata(ctx, name)) {
864 odirfd = local_opendir_nofollow(ctx, odirpath);
869 ndirfd = local_opendir_nofollow(ctx, dirpath->data);
871 close_preserve_errno(odirfd);
875 ret = linkat(odirfd, oname, ndirfd, name, 0);
880 /* now link the virtfs_metadata files */
881 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
882 int omap_dirfd, nmap_dirfd;
884 ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700);
885 if (ret < 0 && errno != EEXIST) {
889 omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR);
890 if (omap_dirfd == -1) {
894 nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR);
895 if (nmap_dirfd == -1) {
896 close_preserve_errno(omap_dirfd);
900 ret = linkat(omap_dirfd, oname, nmap_dirfd, name, 0);
901 close_preserve_errno(nmap_dirfd);
902 close_preserve_errno(omap_dirfd);
903 if (ret < 0 && errno != ENOENT) {
914 unlinkat_preserve_errno(ndirfd, name, 0);
916 close_preserve_errno(ndirfd);
917 close_preserve_errno(odirfd);
924 static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
928 fd = local_open_nofollow(ctx, fs_path->data, O_WRONLY, 0);
932 ret = ftruncate(fd, size);
933 close_preserve_errno(fd);
937 static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
939 char *dirpath = g_path_get_dirname(fs_path->data);
940 char *name = g_path_get_basename(fs_path->data);
944 dirfd = local_opendir_nofollow(fs_ctx, dirpath);
949 if ((credp->fc_uid == -1 && credp->fc_gid == -1) ||
950 (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
951 (fs_ctx->export_flags & V9FS_SM_NONE)) {
952 ret = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid,
953 AT_SYMLINK_NOFOLLOW);
954 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
955 ret = local_set_xattrat(dirfd, name, credp);
956 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
957 ret = local_set_mapped_file_attrat(dirfd, name, credp);
960 close_preserve_errno(dirfd);
967 static int local_utimensat(FsContext *s, V9fsPath *fs_path,
968 const struct timespec *buf)
970 char *dirpath = g_path_get_dirname(fs_path->data);
971 char *name = g_path_get_basename(fs_path->data);
974 dirfd = local_opendir_nofollow(s, dirpath);
979 ret = utimensat(dirfd, name, buf, AT_SYMLINK_NOFOLLOW);
980 close_preserve_errno(dirfd);
987 static int local_unlinkat_common(FsContext *ctx, int dirfd, const char *name,
992 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
995 /* We need to remove the metadata as well:
996 * - the metadata directory if we're removing a directory
997 * - the metadata file in the parent's metadata directory
999 * If any of these are missing (ie, ENOENT) then we're probably
1000 * trying to remove something that wasn't created in mapped-file
1001 * mode. We just ignore the error.
1003 if (flags == AT_REMOVEDIR) {
1006 fd = openat_dir(dirfd, name);
1010 ret = unlinkat(fd, VIRTFS_META_DIR, AT_REMOVEDIR);
1011 close_preserve_errno(fd);
1012 if (ret < 0 && errno != ENOENT) {
1016 map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
1017 if (map_dirfd != -1) {
1018 ret = unlinkat(map_dirfd, name, 0);
1019 close_preserve_errno(map_dirfd);
1020 if (ret < 0 && errno != ENOENT) {
1023 } else if (errno != ENOENT) {
1028 ret = unlinkat(dirfd, name, flags);
1033 static int local_remove(FsContext *ctx, const char *path)
1036 char *dirpath = g_path_get_dirname(path);
1037 char *name = g_path_get_basename(path);
1042 dirfd = local_opendir_nofollow(ctx, dirpath);
1047 if (fstatat(dirfd, path, &stbuf, AT_SYMLINK_NOFOLLOW) < 0) {
1051 if (S_ISDIR(stbuf.st_mode)) {
1052 flags |= AT_REMOVEDIR;
1055 err = local_unlinkat_common(ctx, dirfd, name, flags);
1057 close_preserve_errno(dirfd);
1064 static int local_fsync(FsContext *ctx, int fid_type,
1065 V9fsFidOpenState *fs, int datasync)
1069 if (fid_type == P9_FID_DIR) {
1070 fd = dirfd(fs->dir.stream);
1076 return qemu_fdatasync(fd);
1082 static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
1086 fd = local_open_nofollow(s, fs_path->data, O_RDONLY, 0);
1090 ret = fstatfs(fd, stbuf);
1091 close_preserve_errno(fd);
1095 static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
1096 const char *name, void *value, size_t size)
1098 char *path = fs_path->data;
1100 return v9fs_get_xattr(ctx, path, name, value, size);
1103 static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path,
1104 void *value, size_t size)
1106 char *path = fs_path->data;
1108 return v9fs_list_xattr(ctx, path, value, size);
1111 static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
1112 void *value, size_t size, int flags)
1114 char *path = fs_path->data;
1116 return v9fs_set_xattr(ctx, path, name, value, size, flags);
1119 static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
1122 char *path = fs_path->data;
1124 return v9fs_remove_xattr(ctx, path, name);
1127 static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path,
1128 const char *name, V9fsPath *target)
1130 if (ctx->export_flags & V9FS_SM_MAPPED_FILE &&
1131 local_is_mapped_file_metadata(ctx, name)) {
1137 if (!strcmp(name, ".")) {
1138 /* "." relative to "foo/bar" is "foo/bar" */
1139 v9fs_path_copy(target, dir_path);
1140 } else if (!strcmp(name, "..")) {
1141 if (!strcmp(dir_path->data, ".")) {
1142 /* ".." relative to the root is "." */
1143 v9fs_path_sprintf(target, ".");
1145 char *tmp = g_path_get_dirname(dir_path->data);
1146 /* Symbolic links are resolved by the client. We can assume
1147 * that ".." relative to "foo/bar" is equivalent to "foo"
1149 v9fs_path_sprintf(target, "%s", tmp);
1153 assert(!strchr(name, '/'));
1154 v9fs_path_sprintf(target, "%s/%s", dir_path->data, name);
1156 } else if (!strcmp(name, "/") || !strcmp(name, ".") ||
1157 !strcmp(name, "..")) {
1158 /* This is the root fid */
1159 v9fs_path_sprintf(target, ".");
1161 assert(!strchr(name, '/'));
1162 v9fs_path_sprintf(target, "./%s", name);
1167 static int local_renameat(FsContext *ctx, V9fsPath *olddir,
1168 const char *old_name, V9fsPath *newdir,
1169 const char *new_name)
1174 if (ctx->export_flags & V9FS_SM_MAPPED_FILE &&
1175 (local_is_mapped_file_metadata(ctx, old_name) ||
1176 local_is_mapped_file_metadata(ctx, new_name))) {
1181 odirfd = local_opendir_nofollow(ctx, olddir->data);
1186 ndirfd = local_opendir_nofollow(ctx, newdir->data);
1188 close_preserve_errno(odirfd);
1192 ret = renameat(odirfd, old_name, ndirfd, new_name);
1197 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1198 int omap_dirfd, nmap_dirfd;
1200 ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700);
1201 if (ret < 0 && errno != EEXIST) {
1202 goto err_undo_rename;
1205 omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR);
1206 if (omap_dirfd == -1) {
1210 nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR);
1211 if (nmap_dirfd == -1) {
1212 close_preserve_errno(omap_dirfd);
1216 /* rename the .virtfs_metadata files */
1217 ret = renameat(omap_dirfd, old_name, nmap_dirfd, new_name);
1218 close_preserve_errno(nmap_dirfd);
1219 close_preserve_errno(omap_dirfd);
1220 if (ret < 0 && errno != ENOENT) {
1221 goto err_undo_rename;
1231 renameat_preserve_errno(ndirfd, new_name, odirfd, old_name);
1233 close_preserve_errno(ndirfd);
1234 close_preserve_errno(odirfd);
1238 static void v9fs_path_init_dirname(V9fsPath *path, const char *str)
1240 path->data = g_path_get_dirname(str);
1241 path->size = strlen(path->data) + 1;
1244 static int local_rename(FsContext *ctx, const char *oldpath,
1245 const char *newpath)
1248 char *oname = g_path_get_basename(oldpath);
1249 char *nname = g_path_get_basename(newpath);
1250 V9fsPath olddir, newdir;
1252 v9fs_path_init_dirname(&olddir, oldpath);
1253 v9fs_path_init_dirname(&newdir, newpath);
1255 err = local_renameat(ctx, &olddir, oname, &newdir, nname);
1257 v9fs_path_free(&newdir);
1258 v9fs_path_free(&olddir);
1265 static int local_unlinkat(FsContext *ctx, V9fsPath *dir,
1266 const char *name, int flags)
1271 if (ctx->export_flags & V9FS_SM_MAPPED_FILE &&
1272 local_is_mapped_file_metadata(ctx, name)) {
1277 dirfd = local_opendir_nofollow(ctx, dir->data);
1282 ret = local_unlinkat_common(ctx, dirfd, name, flags);
1283 close_preserve_errno(dirfd);
1287 static int local_ioc_getversion(FsContext *ctx, V9fsPath *path,
1288 mode_t st_mode, uint64_t *st_gen)
1290 #ifdef FS_IOC_GETVERSION
1292 V9fsFidOpenState fid_open;
1295 * Do not try to open special files like device nodes, fifos etc
1296 * We can get fd for regular files and directories only
1298 if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
1302 err = local_open(ctx, path, O_RDONLY, &fid_open);
1306 err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
1307 local_close(ctx, &fid_open);
1315 static int local_init(FsContext *ctx)
1317 struct statfs stbuf;
1318 LocalData *data = g_malloc(sizeof(*data));
1320 data->mountfd = open(ctx->fs_root, O_DIRECTORY | O_RDONLY);
1321 if (data->mountfd == -1) {
1325 #ifdef FS_IOC_GETVERSION
1327 * use ioc_getversion only if the ioctl is definied
1329 if (fstatfs(data->mountfd, &stbuf) < 0) {
1330 close_preserve_errno(data->mountfd);
1333 switch (stbuf.f_type) {
1334 case EXT2_SUPER_MAGIC:
1335 case BTRFS_SUPER_MAGIC:
1336 case REISERFS_SUPER_MAGIC:
1337 case XFS_SUPER_MAGIC:
1338 ctx->exops.get_st_gen = local_ioc_getversion;
1343 if (ctx->export_flags & V9FS_SM_PASSTHROUGH) {
1344 ctx->xops = passthrough_xattr_ops;
1345 } else if (ctx->export_flags & V9FS_SM_MAPPED) {
1346 ctx->xops = mapped_xattr_ops;
1347 } else if (ctx->export_flags & V9FS_SM_NONE) {
1348 ctx->xops = none_xattr_ops;
1349 } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1351 * xattr operation for mapped-file and passthrough
1354 ctx->xops = passthrough_xattr_ops;
1356 ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT;
1358 ctx->private = data;
1366 static void local_cleanup(FsContext *ctx)
1368 LocalData *data = ctx->private;
1370 close(data->mountfd);
1374 static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse)
1376 const char *sec_model = qemu_opt_get(opts, "security_model");
1377 const char *path = qemu_opt_get(opts, "path");
1381 error_report("Security model not specified, local fs needs security model");
1382 error_printf("valid options are:"
1383 "\tsecurity_model=[passthrough|mapped-xattr|mapped-file|none]\n");
1387 if (!strcmp(sec_model, "passthrough")) {
1388 fse->export_flags |= V9FS_SM_PASSTHROUGH;
1389 } else if (!strcmp(sec_model, "mapped") ||
1390 !strcmp(sec_model, "mapped-xattr")) {
1391 fse->export_flags |= V9FS_SM_MAPPED;
1392 } else if (!strcmp(sec_model, "none")) {
1393 fse->export_flags |= V9FS_SM_NONE;
1394 } else if (!strcmp(sec_model, "mapped-file")) {
1395 fse->export_flags |= V9FS_SM_MAPPED_FILE;
1397 error_report("Invalid security model %s specified", sec_model);
1398 error_printf("valid options are:"
1399 "\t[passthrough|mapped-xattr|mapped-file|none]\n");
1404 error_report("fsdev: No path specified");
1408 fsdev_throttle_parse_opts(opts, &fse->fst, &err);
1410 error_reportf_err(err, "Throttle configuration is not valid: ");
1414 fse->path = g_strdup(path);
1419 FileOperations local_ops = {
1420 .parse_opts = local_parse_opts,
1422 .cleanup = local_cleanup,
1423 .lstat = local_lstat,
1424 .readlink = local_readlink,
1425 .close = local_close,
1426 .closedir = local_closedir,
1428 .opendir = local_opendir,
1429 .rewinddir = local_rewinddir,
1430 .telldir = local_telldir,
1431 .readdir = local_readdir,
1432 .seekdir = local_seekdir,
1433 .preadv = local_preadv,
1434 .pwritev = local_pwritev,
1435 .chmod = local_chmod,
1436 .mknod = local_mknod,
1437 .mkdir = local_mkdir,
1438 .fstat = local_fstat,
1439 .open2 = local_open2,
1440 .symlink = local_symlink,
1442 .truncate = local_truncate,
1443 .rename = local_rename,
1444 .chown = local_chown,
1445 .utimensat = local_utimensat,
1446 .remove = local_remove,
1447 .fsync = local_fsync,
1448 .statfs = local_statfs,
1449 .lgetxattr = local_lgetxattr,
1450 .llistxattr = local_llistxattr,
1451 .lsetxattr = local_lsetxattr,
1452 .lremovexattr = local_lremovexattr,
1453 .name_to_path = local_name_to_path,
1454 .renameat = local_renameat,
1455 .unlinkat = local_unlinkat,