nbd/server: rename clientflags var in nbd_negotiate_options
[qemu/ar7.git] / hw / 9pfs / 9p-local.c
blob6e478f4765ef35416139049e72da1936d161695c
1 /*
2 * 9p Posix callback
4 * Copyright IBM, Corp. 2010
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
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"
15 #include "9p.h"
16 #include "9p-local.h"
17 #include "9p-xattr.h"
18 #include "9p-util.h"
19 #include "fsdev/qemu-fsdev.h" /* local_ops */
20 #include <arpa/inet.h>
21 #include <pwd.h>
22 #include <grp.h>
23 #include <sys/socket.h>
24 #include <sys/un.h>
25 #include "qemu/xattr.h"
26 #include "qemu/cutils.h"
27 #include "qemu/error-report.h"
28 #include <libgen.h>
29 #include <linux/fs.h>
30 #ifdef CONFIG_LINUX_MAGIC_H
31 #include <linux/magic.h>
32 #endif
33 #include <sys/ioctl.h>
35 #ifndef XFS_SUPER_MAGIC
36 #define XFS_SUPER_MAGIC 0x58465342
37 #endif
38 #ifndef EXT2_SUPER_MAGIC
39 #define EXT2_SUPER_MAGIC 0xEF53
40 #endif
41 #ifndef REISERFS_SUPER_MAGIC
42 #define REISERFS_SUPER_MAGIC 0x52654973
43 #endif
44 #ifndef BTRFS_SUPER_MAGIC
45 #define BTRFS_SUPER_MAGIC 0x9123683E
46 #endif
48 typedef struct {
49 int mountfd;
50 } LocalData;
52 int local_open_nofollow(FsContext *fs_ctx, const char *path, int flags,
53 mode_t mode)
55 LocalData *data = fs_ctx->private;
56 int fd = data->mountfd;
58 while (*path && fd != -1) {
59 const char *c;
60 int next_fd;
61 char *head;
63 /* Only relative paths without consecutive slashes */
64 assert(*path != '/');
66 head = g_strdup(path);
67 c = strchrnul(path, '/');
68 if (*c) {
69 /* Intermediate path element */
70 head[c - path] = 0;
71 path = c + 1;
72 next_fd = openat_dir(fd, head);
73 } else {
74 /* Rightmost path element */
75 next_fd = openat_file(fd, head, flags, mode);
76 path = c;
78 g_free(head);
79 if (fd != data->mountfd) {
80 close_preserve_errno(fd);
82 fd = next_fd;
85 assert(fd != data->mountfd);
86 return fd;
89 int local_opendir_nofollow(FsContext *fs_ctx, const char *path)
91 return local_open_nofollow(fs_ctx, path, O_DIRECTORY | O_RDONLY, 0);
94 static void renameat_preserve_errno(int odirfd, const char *opath, int ndirfd,
95 const char *npath)
97 int serrno = errno;
98 renameat(odirfd, opath, ndirfd, npath);
99 errno = serrno;
102 static void unlinkat_preserve_errno(int dirfd, const char *path, int flags)
104 int serrno = errno;
105 unlinkat(dirfd, path, flags);
106 errno = serrno;
109 #define VIRTFS_META_DIR ".virtfs_metadata"
110 #define VIRTFS_META_ROOT_FILE VIRTFS_META_DIR "_root"
112 static FILE *local_fopenat(int dirfd, const char *name, const char *mode)
114 int fd, o_mode = 0;
115 FILE *fp;
116 int flags;
118 * only supports two modes
120 if (mode[0] == 'r') {
121 flags = O_RDONLY;
122 } else if (mode[0] == 'w') {
123 flags = O_WRONLY | O_TRUNC | O_CREAT;
124 o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
125 } else {
126 return NULL;
128 fd = openat_file(dirfd, name, flags, o_mode);
129 if (fd == -1) {
130 return NULL;
132 fp = fdopen(fd, mode);
133 if (!fp) {
134 close(fd);
136 return fp;
139 #define ATTR_MAX 100
140 static void local_mapped_file_attr(int dirfd, const char *name,
141 struct stat *stbuf)
143 FILE *fp;
144 char buf[ATTR_MAX];
145 int map_dirfd;
147 if (strcmp(name, ".")) {
148 map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
149 if (map_dirfd == -1) {
150 return;
153 fp = local_fopenat(map_dirfd, name, "r");
154 close_preserve_errno(map_dirfd);
155 } else {
156 fp = local_fopenat(dirfd, VIRTFS_META_ROOT_FILE, "r");
158 if (!fp) {
159 return;
161 memset(buf, 0, ATTR_MAX);
162 while (fgets(buf, ATTR_MAX, fp)) {
163 if (!strncmp(buf, "virtfs.uid", 10)) {
164 stbuf->st_uid = atoi(buf+11);
165 } else if (!strncmp(buf, "virtfs.gid", 10)) {
166 stbuf->st_gid = atoi(buf+11);
167 } else if (!strncmp(buf, "virtfs.mode", 11)) {
168 stbuf->st_mode = atoi(buf+12);
169 } else if (!strncmp(buf, "virtfs.rdev", 11)) {
170 stbuf->st_rdev = atoi(buf+12);
172 memset(buf, 0, ATTR_MAX);
174 fclose(fp);
177 static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf)
179 int err = -1;
180 char *dirpath = g_path_get_dirname(fs_path->data);
181 char *name = g_path_get_basename(fs_path->data);
182 int dirfd;
184 dirfd = local_opendir_nofollow(fs_ctx, dirpath);
185 if (dirfd == -1) {
186 goto out;
189 err = fstatat(dirfd, name, stbuf, AT_SYMLINK_NOFOLLOW);
190 if (err) {
191 goto err_out;
193 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
194 /* Actual credentials are part of extended attrs */
195 uid_t tmp_uid;
196 gid_t tmp_gid;
197 mode_t tmp_mode;
198 dev_t tmp_dev;
200 if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.uid", &tmp_uid,
201 sizeof(uid_t)) > 0) {
202 stbuf->st_uid = le32_to_cpu(tmp_uid);
204 if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.gid", &tmp_gid,
205 sizeof(gid_t)) > 0) {
206 stbuf->st_gid = le32_to_cpu(tmp_gid);
208 if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.mode", &tmp_mode,
209 sizeof(mode_t)) > 0) {
210 stbuf->st_mode = le32_to_cpu(tmp_mode);
212 if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.rdev", &tmp_dev,
213 sizeof(dev_t)) > 0) {
214 stbuf->st_rdev = le64_to_cpu(tmp_dev);
216 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
217 local_mapped_file_attr(dirfd, name, stbuf);
220 err_out:
221 close_preserve_errno(dirfd);
222 out:
223 g_free(name);
224 g_free(dirpath);
225 return err;
228 static int local_set_mapped_file_attrat(int dirfd, const char *name,
229 FsCred *credp)
231 FILE *fp;
232 int ret;
233 char buf[ATTR_MAX];
234 int uid = -1, gid = -1, mode = -1, rdev = -1;
235 int map_dirfd = -1, map_fd;
236 bool is_root = !strcmp(name, ".");
238 if (is_root) {
239 fp = local_fopenat(dirfd, VIRTFS_META_ROOT_FILE, "r");
240 if (!fp) {
241 if (errno == ENOENT) {
242 goto update_map_file;
243 } else {
244 return -1;
247 } else {
248 ret = mkdirat(dirfd, VIRTFS_META_DIR, 0700);
249 if (ret < 0 && errno != EEXIST) {
250 return -1;
253 map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
254 if (map_dirfd == -1) {
255 return -1;
258 fp = local_fopenat(map_dirfd, name, "r");
259 if (!fp) {
260 if (errno == ENOENT) {
261 goto update_map_file;
262 } else {
263 close_preserve_errno(map_dirfd);
264 return -1;
268 memset(buf, 0, ATTR_MAX);
269 while (fgets(buf, ATTR_MAX, fp)) {
270 if (!strncmp(buf, "virtfs.uid", 10)) {
271 uid = atoi(buf + 11);
272 } else if (!strncmp(buf, "virtfs.gid", 10)) {
273 gid = atoi(buf + 11);
274 } else if (!strncmp(buf, "virtfs.mode", 11)) {
275 mode = atoi(buf + 12);
276 } else if (!strncmp(buf, "virtfs.rdev", 11)) {
277 rdev = atoi(buf + 12);
279 memset(buf, 0, ATTR_MAX);
281 fclose(fp);
283 update_map_file:
284 if (is_root) {
285 fp = local_fopenat(dirfd, VIRTFS_META_ROOT_FILE, "w");
286 } else {
287 fp = local_fopenat(map_dirfd, name, "w");
288 /* We can't go this far with map_dirfd not being a valid file descriptor
289 * but some versions of gcc aren't smart enough to see it.
291 if (map_dirfd != -1) {
292 close_preserve_errno(map_dirfd);
295 if (!fp) {
296 return -1;
299 map_fd = fileno(fp);
300 assert(map_fd != -1);
301 ret = fchmod(map_fd, 0600);
302 assert(ret == 0);
304 if (credp->fc_uid != -1) {
305 uid = credp->fc_uid;
307 if (credp->fc_gid != -1) {
308 gid = credp->fc_gid;
310 if (credp->fc_mode != -1) {
311 mode = credp->fc_mode;
313 if (credp->fc_rdev != -1) {
314 rdev = credp->fc_rdev;
317 if (uid != -1) {
318 fprintf(fp, "virtfs.uid=%d\n", uid);
320 if (gid != -1) {
321 fprintf(fp, "virtfs.gid=%d\n", gid);
323 if (mode != -1) {
324 fprintf(fp, "virtfs.mode=%d\n", mode);
326 if (rdev != -1) {
327 fprintf(fp, "virtfs.rdev=%d\n", rdev);
329 fclose(fp);
331 return 0;
334 static int fchmodat_nofollow(int dirfd, const char *name, mode_t mode)
336 int fd, ret;
338 /* FIXME: this should be handled with fchmodat(AT_SYMLINK_NOFOLLOW).
339 * Unfortunately, the linux kernel doesn't implement it yet. As an
340 * alternative, let's open the file and use fchmod() instead. This
341 * may fail depending on the permissions of the file, but it is the
342 * best we can do to avoid TOCTTOU. We first try to open read-only
343 * in case name points to a directory. If that fails, we try write-only
344 * in case name doesn't point to a directory.
346 fd = openat_file(dirfd, name, O_RDONLY, 0);
347 if (fd == -1) {
348 /* In case the file is writable-only and isn't a directory. */
349 if (errno == EACCES) {
350 fd = openat_file(dirfd, name, O_WRONLY, 0);
352 if (fd == -1 && errno == EISDIR) {
353 errno = EACCES;
356 if (fd == -1) {
357 return -1;
359 ret = fchmod(fd, mode);
360 close_preserve_errno(fd);
361 return ret;
364 static int local_set_xattrat(int dirfd, const char *path, FsCred *credp)
366 int err;
368 if (credp->fc_uid != -1) {
369 uint32_t tmp_uid = cpu_to_le32(credp->fc_uid);
370 err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.uid", &tmp_uid,
371 sizeof(uid_t), 0);
372 if (err) {
373 return err;
376 if (credp->fc_gid != -1) {
377 uint32_t tmp_gid = cpu_to_le32(credp->fc_gid);
378 err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.gid", &tmp_gid,
379 sizeof(gid_t), 0);
380 if (err) {
381 return err;
384 if (credp->fc_mode != -1) {
385 uint32_t tmp_mode = cpu_to_le32(credp->fc_mode);
386 err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.mode", &tmp_mode,
387 sizeof(mode_t), 0);
388 if (err) {
389 return err;
392 if (credp->fc_rdev != -1) {
393 uint64_t tmp_rdev = cpu_to_le64(credp->fc_rdev);
394 err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.rdev", &tmp_rdev,
395 sizeof(dev_t), 0);
396 if (err) {
397 return err;
400 return 0;
403 static int local_set_cred_passthrough(FsContext *fs_ctx, int dirfd,
404 const char *name, FsCred *credp)
406 if (fchownat(dirfd, name, credp->fc_uid, credp->fc_gid,
407 AT_SYMLINK_NOFOLLOW) < 0) {
409 * If we fail to change ownership and if we are
410 * using security model none. Ignore the error
412 if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
413 return -1;
417 return fchmodat_nofollow(dirfd, name, credp->fc_mode & 07777);
420 static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
421 char *buf, size_t bufsz)
423 ssize_t tsize = -1;
425 if ((fs_ctx->export_flags & V9FS_SM_MAPPED) ||
426 (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
427 int fd;
429 fd = local_open_nofollow(fs_ctx, fs_path->data, O_RDONLY, 0);
430 if (fd == -1) {
431 return -1;
433 do {
434 tsize = read(fd, (void *)buf, bufsz);
435 } while (tsize == -1 && errno == EINTR);
436 close_preserve_errno(fd);
437 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
438 (fs_ctx->export_flags & V9FS_SM_NONE)) {
439 char *dirpath = g_path_get_dirname(fs_path->data);
440 char *name = g_path_get_basename(fs_path->data);
441 int dirfd;
443 dirfd = local_opendir_nofollow(fs_ctx, dirpath);
444 if (dirfd == -1) {
445 goto out;
448 tsize = readlinkat(dirfd, name, buf, bufsz);
449 close_preserve_errno(dirfd);
450 out:
451 g_free(name);
452 g_free(dirpath);
454 return tsize;
457 static int local_close(FsContext *ctx, V9fsFidOpenState *fs)
459 return close(fs->fd);
462 static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs)
464 return closedir(fs->dir.stream);
467 static int local_open(FsContext *ctx, V9fsPath *fs_path,
468 int flags, V9fsFidOpenState *fs)
470 int fd;
472 fd = local_open_nofollow(ctx, fs_path->data, flags, 0);
473 if (fd == -1) {
474 return -1;
476 fs->fd = fd;
477 return fs->fd;
480 static int local_opendir(FsContext *ctx,
481 V9fsPath *fs_path, V9fsFidOpenState *fs)
483 int dirfd;
484 DIR *stream;
486 dirfd = local_opendir_nofollow(ctx, fs_path->data);
487 if (dirfd == -1) {
488 return -1;
491 stream = fdopendir(dirfd);
492 if (!stream) {
493 close(dirfd);
494 return -1;
496 fs->dir.stream = stream;
497 return 0;
500 static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
502 rewinddir(fs->dir.stream);
505 static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs)
507 return telldir(fs->dir.stream);
510 static bool local_is_mapped_file_metadata(FsContext *fs_ctx, const char *name)
512 return
513 !strcmp(name, VIRTFS_META_DIR) || !strcmp(name, VIRTFS_META_ROOT_FILE);
516 static struct dirent *local_readdir(FsContext *ctx, V9fsFidOpenState *fs)
518 struct dirent *entry;
520 again:
521 entry = readdir(fs->dir.stream);
522 if (!entry) {
523 return NULL;
526 if (ctx->export_flags & V9FS_SM_MAPPED) {
527 entry->d_type = DT_UNKNOWN;
528 } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
529 if (local_is_mapped_file_metadata(ctx, entry->d_name)) {
530 /* skip the meta data */
531 goto again;
533 entry->d_type = DT_UNKNOWN;
536 return entry;
539 static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
541 seekdir(fs->dir.stream, off);
544 static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs,
545 const struct iovec *iov,
546 int iovcnt, off_t offset)
548 #ifdef CONFIG_PREADV
549 return preadv(fs->fd, iov, iovcnt, offset);
550 #else
551 int err = lseek(fs->fd, offset, SEEK_SET);
552 if (err == -1) {
553 return err;
554 } else {
555 return readv(fs->fd, iov, iovcnt);
557 #endif
560 static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
561 const struct iovec *iov,
562 int iovcnt, off_t offset)
564 ssize_t ret;
565 #ifdef CONFIG_PREADV
566 ret = pwritev(fs->fd, iov, iovcnt, offset);
567 #else
568 int err = lseek(fs->fd, offset, SEEK_SET);
569 if (err == -1) {
570 return err;
571 } else {
572 ret = writev(fs->fd, iov, iovcnt);
574 #endif
575 #ifdef CONFIG_SYNC_FILE_RANGE
576 if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
578 * Initiate a writeback. This is not a data integrity sync.
579 * We want to ensure that we don't leave dirty pages in the cache
580 * after write when writeout=immediate is sepcified.
582 sync_file_range(fs->fd, offset, ret,
583 SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
585 #endif
586 return ret;
589 static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
591 char *dirpath = g_path_get_dirname(fs_path->data);
592 char *name = g_path_get_basename(fs_path->data);
593 int ret = -1;
594 int dirfd;
596 dirfd = local_opendir_nofollow(fs_ctx, dirpath);
597 if (dirfd == -1) {
598 goto out;
601 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
602 ret = local_set_xattrat(dirfd, name, credp);
603 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
604 ret = local_set_mapped_file_attrat(dirfd, name, credp);
605 } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
606 fs_ctx->export_flags & V9FS_SM_NONE) {
607 ret = fchmodat_nofollow(dirfd, name, credp->fc_mode);
609 close_preserve_errno(dirfd);
611 out:
612 g_free(dirpath);
613 g_free(name);
614 return ret;
617 static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
618 const char *name, FsCred *credp)
620 int err = -1;
621 int dirfd;
623 if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE &&
624 local_is_mapped_file_metadata(fs_ctx, name)) {
625 errno = EINVAL;
626 return -1;
629 dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
630 if (dirfd == -1) {
631 return -1;
634 if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
635 fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
636 err = mknodat(dirfd, name, fs_ctx->fmode | S_IFREG, 0);
637 if (err == -1) {
638 goto out;
641 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
642 err = local_set_xattrat(dirfd, name, credp);
643 } else {
644 err = local_set_mapped_file_attrat(dirfd, name, credp);
646 if (err == -1) {
647 goto err_end;
649 } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
650 fs_ctx->export_flags & V9FS_SM_NONE) {
651 err = mknodat(dirfd, name, credp->fc_mode, credp->fc_rdev);
652 if (err == -1) {
653 goto out;
655 err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp);
656 if (err == -1) {
657 goto err_end;
660 goto out;
662 err_end:
663 unlinkat_preserve_errno(dirfd, name, 0);
664 out:
665 close_preserve_errno(dirfd);
666 return err;
669 static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
670 const char *name, FsCred *credp)
672 int err = -1;
673 int dirfd;
675 if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE &&
676 local_is_mapped_file_metadata(fs_ctx, name)) {
677 errno = EINVAL;
678 return -1;
681 dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
682 if (dirfd == -1) {
683 return -1;
686 if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
687 fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
688 err = mkdirat(dirfd, name, fs_ctx->dmode);
689 if (err == -1) {
690 goto out;
692 credp->fc_mode = credp->fc_mode | S_IFDIR;
694 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
695 err = local_set_xattrat(dirfd, name, credp);
696 } else {
697 err = local_set_mapped_file_attrat(dirfd, name, credp);
699 if (err == -1) {
700 goto err_end;
702 } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
703 fs_ctx->export_flags & V9FS_SM_NONE) {
704 err = mkdirat(dirfd, name, credp->fc_mode);
705 if (err == -1) {
706 goto out;
708 err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp);
709 if (err == -1) {
710 goto err_end;
713 goto out;
715 err_end:
716 unlinkat_preserve_errno(dirfd, name, AT_REMOVEDIR);
717 out:
718 close_preserve_errno(dirfd);
719 return err;
722 static int local_fstat(FsContext *fs_ctx, int fid_type,
723 V9fsFidOpenState *fs, struct stat *stbuf)
725 int err, fd;
727 if (fid_type == P9_FID_DIR) {
728 fd = dirfd(fs->dir.stream);
729 } else {
730 fd = fs->fd;
733 err = fstat(fd, stbuf);
734 if (err) {
735 return err;
737 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
738 /* Actual credentials are part of extended attrs */
739 uid_t tmp_uid;
740 gid_t tmp_gid;
741 mode_t tmp_mode;
742 dev_t tmp_dev;
744 if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) {
745 stbuf->st_uid = le32_to_cpu(tmp_uid);
747 if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
748 stbuf->st_gid = le32_to_cpu(tmp_gid);
750 if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) {
751 stbuf->st_mode = le32_to_cpu(tmp_mode);
753 if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
754 stbuf->st_rdev = le64_to_cpu(tmp_dev);
756 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
757 errno = EOPNOTSUPP;
758 return -1;
760 return err;
763 static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
764 int flags, FsCred *credp, V9fsFidOpenState *fs)
766 int fd = -1;
767 int err = -1;
768 int dirfd;
770 if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE &&
771 local_is_mapped_file_metadata(fs_ctx, name)) {
772 errno = EINVAL;
773 return -1;
777 * Mark all the open to not follow symlinks
779 flags |= O_NOFOLLOW;
781 dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
782 if (dirfd == -1) {
783 return -1;
786 /* Determine the security model */
787 if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
788 fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
789 fd = openat_file(dirfd, name, flags, fs_ctx->fmode);
790 if (fd == -1) {
791 goto out;
793 credp->fc_mode = credp->fc_mode|S_IFREG;
794 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
795 /* Set cleint credentials in xattr */
796 err = local_set_xattrat(dirfd, name, credp);
797 } else {
798 err = local_set_mapped_file_attrat(dirfd, name, credp);
800 if (err == -1) {
801 goto err_end;
803 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
804 (fs_ctx->export_flags & V9FS_SM_NONE)) {
805 fd = openat_file(dirfd, name, flags, credp->fc_mode);
806 if (fd == -1) {
807 goto out;
809 err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp);
810 if (err == -1) {
811 goto err_end;
814 err = fd;
815 fs->fd = fd;
816 goto out;
818 err_end:
819 unlinkat_preserve_errno(dirfd, name,
820 flags & O_DIRECTORY ? AT_REMOVEDIR : 0);
821 close_preserve_errno(fd);
822 out:
823 close_preserve_errno(dirfd);
824 return err;
828 static int local_symlink(FsContext *fs_ctx, const char *oldpath,
829 V9fsPath *dir_path, const char *name, FsCred *credp)
831 int err = -1;
832 int dirfd;
834 if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE &&
835 local_is_mapped_file_metadata(fs_ctx, name)) {
836 errno = EINVAL;
837 return -1;
840 dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
841 if (dirfd == -1) {
842 return -1;
845 /* Determine the security model */
846 if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
847 fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
848 int fd;
849 ssize_t oldpath_size, write_size;
851 fd = openat_file(dirfd, name, O_CREAT | O_EXCL | O_RDWR,
852 fs_ctx->fmode);
853 if (fd == -1) {
854 goto out;
856 /* Write the oldpath (target) to the file. */
857 oldpath_size = strlen(oldpath);
858 do {
859 write_size = write(fd, (void *)oldpath, oldpath_size);
860 } while (write_size == -1 && errno == EINTR);
861 close_preserve_errno(fd);
863 if (write_size != oldpath_size) {
864 goto err_end;
866 /* Set cleint credentials in symlink's xattr */
867 credp->fc_mode = credp->fc_mode | S_IFLNK;
869 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
870 err = local_set_xattrat(dirfd, name, credp);
871 } else {
872 err = local_set_mapped_file_attrat(dirfd, name, credp);
874 if (err == -1) {
875 goto err_end;
877 } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
878 fs_ctx->export_flags & V9FS_SM_NONE) {
879 err = symlinkat(oldpath, dirfd, name);
880 if (err) {
881 goto out;
883 err = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid,
884 AT_SYMLINK_NOFOLLOW);
885 if (err == -1) {
887 * If we fail to change ownership and if we are
888 * using security model none. Ignore the error
890 if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
891 goto err_end;
892 } else {
893 err = 0;
897 goto out;
899 err_end:
900 unlinkat_preserve_errno(dirfd, name, 0);
901 out:
902 close_preserve_errno(dirfd);
903 return err;
906 static int local_link(FsContext *ctx, V9fsPath *oldpath,
907 V9fsPath *dirpath, const char *name)
909 char *odirpath = g_path_get_dirname(oldpath->data);
910 char *oname = g_path_get_basename(oldpath->data);
911 int ret = -1;
912 int odirfd, ndirfd;
914 if (ctx->export_flags & V9FS_SM_MAPPED_FILE &&
915 local_is_mapped_file_metadata(ctx, name)) {
916 errno = EINVAL;
917 return -1;
920 odirfd = local_opendir_nofollow(ctx, odirpath);
921 if (odirfd == -1) {
922 goto out;
925 ndirfd = local_opendir_nofollow(ctx, dirpath->data);
926 if (ndirfd == -1) {
927 close_preserve_errno(odirfd);
928 goto out;
931 ret = linkat(odirfd, oname, ndirfd, name, 0);
932 if (ret < 0) {
933 goto out_close;
936 /* now link the virtfs_metadata files */
937 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
938 int omap_dirfd, nmap_dirfd;
940 ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700);
941 if (ret < 0 && errno != EEXIST) {
942 goto err_undo_link;
945 omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR);
946 if (omap_dirfd == -1) {
947 goto err;
950 nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR);
951 if (nmap_dirfd == -1) {
952 close_preserve_errno(omap_dirfd);
953 goto err;
956 ret = linkat(omap_dirfd, oname, nmap_dirfd, name, 0);
957 close_preserve_errno(nmap_dirfd);
958 close_preserve_errno(omap_dirfd);
959 if (ret < 0 && errno != ENOENT) {
960 goto err_undo_link;
963 ret = 0;
965 goto out_close;
967 err:
968 ret = -1;
969 err_undo_link:
970 unlinkat_preserve_errno(ndirfd, name, 0);
971 out_close:
972 close_preserve_errno(ndirfd);
973 close_preserve_errno(odirfd);
974 out:
975 g_free(oname);
976 g_free(odirpath);
977 return ret;
980 static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
982 int fd, ret;
984 fd = local_open_nofollow(ctx, fs_path->data, O_WRONLY, 0);
985 if (fd == -1) {
986 return -1;
988 ret = ftruncate(fd, size);
989 close_preserve_errno(fd);
990 return ret;
993 static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
995 char *dirpath = g_path_get_dirname(fs_path->data);
996 char *name = g_path_get_basename(fs_path->data);
997 int ret = -1;
998 int dirfd;
1000 dirfd = local_opendir_nofollow(fs_ctx, dirpath);
1001 if (dirfd == -1) {
1002 goto out;
1005 if ((credp->fc_uid == -1 && credp->fc_gid == -1) ||
1006 (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
1007 (fs_ctx->export_flags & V9FS_SM_NONE)) {
1008 ret = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid,
1009 AT_SYMLINK_NOFOLLOW);
1010 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
1011 ret = local_set_xattrat(dirfd, name, credp);
1012 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1013 ret = local_set_mapped_file_attrat(dirfd, name, credp);
1016 close_preserve_errno(dirfd);
1017 out:
1018 g_free(name);
1019 g_free(dirpath);
1020 return ret;
1023 static int local_utimensat(FsContext *s, V9fsPath *fs_path,
1024 const struct timespec *buf)
1026 char *dirpath = g_path_get_dirname(fs_path->data);
1027 char *name = g_path_get_basename(fs_path->data);
1028 int dirfd, ret = -1;
1030 dirfd = local_opendir_nofollow(s, dirpath);
1031 if (dirfd == -1) {
1032 goto out;
1035 ret = utimensat(dirfd, name, buf, AT_SYMLINK_NOFOLLOW);
1036 close_preserve_errno(dirfd);
1037 out:
1038 g_free(dirpath);
1039 g_free(name);
1040 return ret;
1043 static int local_unlinkat_common(FsContext *ctx, int dirfd, const char *name,
1044 int flags)
1046 int ret = -1;
1048 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1049 int map_dirfd;
1051 /* We need to remove the metadata as well:
1052 * - the metadata directory if we're removing a directory
1053 * - the metadata file in the parent's metadata directory
1055 * If any of these are missing (ie, ENOENT) then we're probably
1056 * trying to remove something that wasn't created in mapped-file
1057 * mode. We just ignore the error.
1059 if (flags == AT_REMOVEDIR) {
1060 int fd;
1062 fd = openat_dir(dirfd, name);
1063 if (fd == -1) {
1064 goto err_out;
1066 ret = unlinkat(fd, VIRTFS_META_DIR, AT_REMOVEDIR);
1067 close_preserve_errno(fd);
1068 if (ret < 0 && errno != ENOENT) {
1069 goto err_out;
1072 map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
1073 if (map_dirfd != -1) {
1074 ret = unlinkat(map_dirfd, name, 0);
1075 close_preserve_errno(map_dirfd);
1076 if (ret < 0 && errno != ENOENT) {
1077 goto err_out;
1079 } else if (errno != ENOENT) {
1080 goto err_out;
1084 ret = unlinkat(dirfd, name, flags);
1085 err_out:
1086 return ret;
1089 static int local_remove(FsContext *ctx, const char *path)
1091 struct stat stbuf;
1092 char *dirpath = g_path_get_dirname(path);
1093 char *name = g_path_get_basename(path);
1094 int flags = 0;
1095 int dirfd;
1096 int err = -1;
1098 dirfd = local_opendir_nofollow(ctx, dirpath);
1099 if (dirfd == -1) {
1100 goto out;
1103 if (fstatat(dirfd, name, &stbuf, AT_SYMLINK_NOFOLLOW) < 0) {
1104 goto err_out;
1107 if (S_ISDIR(stbuf.st_mode)) {
1108 flags |= AT_REMOVEDIR;
1111 err = local_unlinkat_common(ctx, dirfd, name, flags);
1112 err_out:
1113 close_preserve_errno(dirfd);
1114 out:
1115 g_free(name);
1116 g_free(dirpath);
1117 return err;
1120 static int local_fsync(FsContext *ctx, int fid_type,
1121 V9fsFidOpenState *fs, int datasync)
1123 int fd;
1125 if (fid_type == P9_FID_DIR) {
1126 fd = dirfd(fs->dir.stream);
1127 } else {
1128 fd = fs->fd;
1131 if (datasync) {
1132 return qemu_fdatasync(fd);
1133 } else {
1134 return fsync(fd);
1138 static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
1140 int fd, ret;
1142 fd = local_open_nofollow(s, fs_path->data, O_RDONLY, 0);
1143 if (fd == -1) {
1144 return -1;
1146 ret = fstatfs(fd, stbuf);
1147 close_preserve_errno(fd);
1148 return ret;
1151 static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
1152 const char *name, void *value, size_t size)
1154 char *path = fs_path->data;
1156 return v9fs_get_xattr(ctx, path, name, value, size);
1159 static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path,
1160 void *value, size_t size)
1162 char *path = fs_path->data;
1164 return v9fs_list_xattr(ctx, path, value, size);
1167 static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
1168 void *value, size_t size, int flags)
1170 char *path = fs_path->data;
1172 return v9fs_set_xattr(ctx, path, name, value, size, flags);
1175 static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
1176 const char *name)
1178 char *path = fs_path->data;
1180 return v9fs_remove_xattr(ctx, path, name);
1183 static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path,
1184 const char *name, V9fsPath *target)
1186 if (ctx->export_flags & V9FS_SM_MAPPED_FILE &&
1187 local_is_mapped_file_metadata(ctx, name)) {
1188 errno = EINVAL;
1189 return -1;
1192 if (dir_path) {
1193 if (!strcmp(name, ".")) {
1194 /* "." relative to "foo/bar" is "foo/bar" */
1195 v9fs_path_copy(target, dir_path);
1196 } else if (!strcmp(name, "..")) {
1197 if (!strcmp(dir_path->data, ".")) {
1198 /* ".." relative to the root is "." */
1199 v9fs_path_sprintf(target, ".");
1200 } else {
1201 char *tmp = g_path_get_dirname(dir_path->data);
1202 /* Symbolic links are resolved by the client. We can assume
1203 * that ".." relative to "foo/bar" is equivalent to "foo"
1205 v9fs_path_sprintf(target, "%s", tmp);
1206 g_free(tmp);
1208 } else {
1209 assert(!strchr(name, '/'));
1210 v9fs_path_sprintf(target, "%s/%s", dir_path->data, name);
1212 } else if (!strcmp(name, "/") || !strcmp(name, ".") ||
1213 !strcmp(name, "..")) {
1214 /* This is the root fid */
1215 v9fs_path_sprintf(target, ".");
1216 } else {
1217 assert(!strchr(name, '/'));
1218 v9fs_path_sprintf(target, "./%s", name);
1220 return 0;
1223 static int local_renameat(FsContext *ctx, V9fsPath *olddir,
1224 const char *old_name, V9fsPath *newdir,
1225 const char *new_name)
1227 int ret;
1228 int odirfd, ndirfd;
1230 if (ctx->export_flags & V9FS_SM_MAPPED_FILE &&
1231 (local_is_mapped_file_metadata(ctx, old_name) ||
1232 local_is_mapped_file_metadata(ctx, new_name))) {
1233 errno = EINVAL;
1234 return -1;
1237 odirfd = local_opendir_nofollow(ctx, olddir->data);
1238 if (odirfd == -1) {
1239 return -1;
1242 ndirfd = local_opendir_nofollow(ctx, newdir->data);
1243 if (ndirfd == -1) {
1244 close_preserve_errno(odirfd);
1245 return -1;
1248 ret = renameat(odirfd, old_name, ndirfd, new_name);
1249 if (ret < 0) {
1250 goto out;
1253 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1254 int omap_dirfd, nmap_dirfd;
1256 ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700);
1257 if (ret < 0 && errno != EEXIST) {
1258 goto err_undo_rename;
1261 omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR);
1262 if (omap_dirfd == -1) {
1263 goto err;
1266 nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR);
1267 if (nmap_dirfd == -1) {
1268 close_preserve_errno(omap_dirfd);
1269 goto err;
1272 /* rename the .virtfs_metadata files */
1273 ret = renameat(omap_dirfd, old_name, nmap_dirfd, new_name);
1274 close_preserve_errno(nmap_dirfd);
1275 close_preserve_errno(omap_dirfd);
1276 if (ret < 0 && errno != ENOENT) {
1277 goto err_undo_rename;
1280 ret = 0;
1282 goto out;
1284 err:
1285 ret = -1;
1286 err_undo_rename:
1287 renameat_preserve_errno(ndirfd, new_name, odirfd, old_name);
1288 out:
1289 close_preserve_errno(ndirfd);
1290 close_preserve_errno(odirfd);
1291 return ret;
1294 static void v9fs_path_init_dirname(V9fsPath *path, const char *str)
1296 path->data = g_path_get_dirname(str);
1297 path->size = strlen(path->data) + 1;
1300 static int local_rename(FsContext *ctx, const char *oldpath,
1301 const char *newpath)
1303 int err;
1304 char *oname = g_path_get_basename(oldpath);
1305 char *nname = g_path_get_basename(newpath);
1306 V9fsPath olddir, newdir;
1308 v9fs_path_init_dirname(&olddir, oldpath);
1309 v9fs_path_init_dirname(&newdir, newpath);
1311 err = local_renameat(ctx, &olddir, oname, &newdir, nname);
1313 v9fs_path_free(&newdir);
1314 v9fs_path_free(&olddir);
1315 g_free(nname);
1316 g_free(oname);
1318 return err;
1321 static int local_unlinkat(FsContext *ctx, V9fsPath *dir,
1322 const char *name, int flags)
1324 int ret;
1325 int dirfd;
1327 if (ctx->export_flags & V9FS_SM_MAPPED_FILE &&
1328 local_is_mapped_file_metadata(ctx, name)) {
1329 errno = EINVAL;
1330 return -1;
1333 dirfd = local_opendir_nofollow(ctx, dir->data);
1334 if (dirfd == -1) {
1335 return -1;
1338 ret = local_unlinkat_common(ctx, dirfd, name, flags);
1339 close_preserve_errno(dirfd);
1340 return ret;
1343 static int local_ioc_getversion(FsContext *ctx, V9fsPath *path,
1344 mode_t st_mode, uint64_t *st_gen)
1346 #ifdef FS_IOC_GETVERSION
1347 int err;
1348 V9fsFidOpenState fid_open;
1351 * Do not try to open special files like device nodes, fifos etc
1352 * We can get fd for regular files and directories only
1354 if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
1355 errno = ENOTTY;
1356 return -1;
1358 err = local_open(ctx, path, O_RDONLY, &fid_open);
1359 if (err < 0) {
1360 return err;
1362 err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
1363 local_close(ctx, &fid_open);
1364 return err;
1365 #else
1366 errno = ENOTTY;
1367 return -1;
1368 #endif
1371 static int local_init(FsContext *ctx)
1373 struct statfs stbuf;
1374 LocalData *data = g_malloc(sizeof(*data));
1376 data->mountfd = open(ctx->fs_root, O_DIRECTORY | O_RDONLY);
1377 if (data->mountfd == -1) {
1378 goto err;
1381 #ifdef FS_IOC_GETVERSION
1383 * use ioc_getversion only if the ioctl is definied
1385 if (fstatfs(data->mountfd, &stbuf) < 0) {
1386 close_preserve_errno(data->mountfd);
1387 goto err;
1389 switch (stbuf.f_type) {
1390 case EXT2_SUPER_MAGIC:
1391 case BTRFS_SUPER_MAGIC:
1392 case REISERFS_SUPER_MAGIC:
1393 case XFS_SUPER_MAGIC:
1394 ctx->exops.get_st_gen = local_ioc_getversion;
1395 break;
1397 #endif
1399 if (ctx->export_flags & V9FS_SM_PASSTHROUGH) {
1400 ctx->xops = passthrough_xattr_ops;
1401 } else if (ctx->export_flags & V9FS_SM_MAPPED) {
1402 ctx->xops = mapped_xattr_ops;
1403 } else if (ctx->export_flags & V9FS_SM_NONE) {
1404 ctx->xops = none_xattr_ops;
1405 } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1407 * xattr operation for mapped-file and passthrough
1408 * remain same.
1410 ctx->xops = passthrough_xattr_ops;
1412 ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT;
1414 ctx->private = data;
1415 return 0;
1417 err:
1418 g_free(data);
1419 return -1;
1422 static void local_cleanup(FsContext *ctx)
1424 LocalData *data = ctx->private;
1426 close(data->mountfd);
1427 g_free(data);
1430 static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse)
1432 const char *sec_model = qemu_opt_get(opts, "security_model");
1433 const char *path = qemu_opt_get(opts, "path");
1434 Error *err = NULL;
1436 if (!sec_model) {
1437 error_report("Security model not specified, local fs needs security model");
1438 error_printf("valid options are:"
1439 "\tsecurity_model=[passthrough|mapped-xattr|mapped-file|none]\n");
1440 return -1;
1443 if (!strcmp(sec_model, "passthrough")) {
1444 fse->export_flags |= V9FS_SM_PASSTHROUGH;
1445 } else if (!strcmp(sec_model, "mapped") ||
1446 !strcmp(sec_model, "mapped-xattr")) {
1447 fse->export_flags |= V9FS_SM_MAPPED;
1448 } else if (!strcmp(sec_model, "none")) {
1449 fse->export_flags |= V9FS_SM_NONE;
1450 } else if (!strcmp(sec_model, "mapped-file")) {
1451 fse->export_flags |= V9FS_SM_MAPPED_FILE;
1452 } else {
1453 error_report("Invalid security model %s specified", sec_model);
1454 error_printf("valid options are:"
1455 "\t[passthrough|mapped-xattr|mapped-file|none]\n");
1456 return -1;
1459 if (!path) {
1460 error_report("fsdev: No path specified");
1461 return -1;
1464 fsdev_throttle_parse_opts(opts, &fse->fst, &err);
1465 if (err) {
1466 error_reportf_err(err, "Throttle configuration is not valid: ");
1467 return -1;
1470 if (fse->export_flags & V9FS_SM_MAPPED ||
1471 fse->export_flags & V9FS_SM_MAPPED_FILE) {
1472 fse->fmode =
1473 qemu_opt_get_number(opts, "fmode", SM_LOCAL_MODE_BITS) & 0777;
1474 fse->dmode =
1475 qemu_opt_get_number(opts, "dmode", SM_LOCAL_DIR_MODE_BITS) & 0777;
1476 } else {
1477 if (qemu_opt_find(opts, "fmode")) {
1478 error_report("fmode is only valid for mapped 9p modes");
1479 return -1;
1481 if (qemu_opt_find(opts, "dmode")) {
1482 error_report("dmode is only valid for mapped 9p modes");
1483 return -1;
1487 fse->path = g_strdup(path);
1489 return 0;
1492 FileOperations local_ops = {
1493 .parse_opts = local_parse_opts,
1494 .init = local_init,
1495 .cleanup = local_cleanup,
1496 .lstat = local_lstat,
1497 .readlink = local_readlink,
1498 .close = local_close,
1499 .closedir = local_closedir,
1500 .open = local_open,
1501 .opendir = local_opendir,
1502 .rewinddir = local_rewinddir,
1503 .telldir = local_telldir,
1504 .readdir = local_readdir,
1505 .seekdir = local_seekdir,
1506 .preadv = local_preadv,
1507 .pwritev = local_pwritev,
1508 .chmod = local_chmod,
1509 .mknod = local_mknod,
1510 .mkdir = local_mkdir,
1511 .fstat = local_fstat,
1512 .open2 = local_open2,
1513 .symlink = local_symlink,
1514 .link = local_link,
1515 .truncate = local_truncate,
1516 .rename = local_rename,
1517 .chown = local_chown,
1518 .utimensat = local_utimensat,
1519 .remove = local_remove,
1520 .fsync = local_fsync,
1521 .statfs = local_statfs,
1522 .lgetxattr = local_lgetxattr,
1523 .llistxattr = local_llistxattr,
1524 .lsetxattr = local_lsetxattr,
1525 .lremovexattr = local_lremovexattr,
1526 .name_to_path = local_name_to_path,
1527 .renameat = local_renameat,
1528 .unlinkat = local_unlinkat,