master
c 1,664 lines 44.6 KB
Raw
1 /*
2 * 9p Posix callback
3 *
4 * Copyright IBM, Corp. 2010
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 */
12
13 /*
14 * Not so fast! You might want to read the 9p developer docs first:
15 * https://wiki.qemu.org/Documentation/9p
16 */
17
18 #include "qemu/osdep.h"
19 #include "9p.h"
20 #include "9p-local.h"
21 #include "9p-xattr.h"
22 #include "9p-util.h"
23 #include "fsdev/qemu-fsdev.h" /* local_ops */
24 #include <arpa/inet.h>
25 #include <pwd.h>
26 #include <grp.h>
27 #include <sys/socket.h>
28 #include <sys/un.h>
29 #include "qemu/xattr.h"
30 #include "qapi/error.h"
31 #include "qemu/bswap.h"
32 #include "qemu/cutils.h"
33 #include "qemu/error-report.h"
34 #include "qemu/option.h"
35 #include <libgen.h>
36 #ifdef CONFIG_LINUX
37 #include <linux/fs.h>
38 #ifdef CONFIG_LINUX_MAGIC_H
39 #include <linux/magic.h>
40 #endif
41 #endif
42 #include <sys/ioctl.h>
43
44 #ifndef XFS_SUPER_MAGIC
45 #define XFS_SUPER_MAGIC 0x58465342
46 #endif
47 #ifndef EXT2_SUPER_MAGIC
48 #define EXT2_SUPER_MAGIC 0xEF53
49 #endif
50 #ifndef REISERFS_SUPER_MAGIC
51 #define REISERFS_SUPER_MAGIC 0x52654973
52 #endif
53 #ifndef BTRFS_SUPER_MAGIC
54 #define BTRFS_SUPER_MAGIC 0x9123683E
55 #endif
56
57 typedef struct {
58 int mountfd;
59 } LocalData;
60
61 int local_open_nofollow(FsContext *fs_ctx, const char *path, int flags,
62 mode_t mode)
63 {
64 LocalData *data = fs_ctx->private;
65 int fd = data->mountfd;
66
67 while (*path && fd != -1) {
68 const char *c;
69 int next_fd;
70 char *head;
71
72 /* Only relative paths without consecutive slashes */
73 assert(*path != '/');
74
75 head = g_strdup(path);
76 c = qemu_strchrnul(path, '/');
77 if (*c) {
78 /* Intermediate path element */
79 head[c - path] = 0;
80 path = c + 1;
81 next_fd = openat_dir(fd, head);
82 } else {
83 /* Rightmost path element */
84 next_fd = openat_file(fd, head, flags, mode);
85 path = c;
86 }
87 g_free(head);
88 if (fd != data->mountfd) {
89 close_preserve_errno(fd);
90 }
91 fd = next_fd;
92 }
93
94 assert(fd != data->mountfd);
95 return fd;
96 }
97
98 int local_opendir_nofollow(FsContext *fs_ctx, const char *path)
99 {
100 return local_open_nofollow(fs_ctx, path, O_DIRECTORY | O_RDONLY, 0);
101 }
102
103 static void renameat_preserve_errno(int odirfd, const char *opath, int ndirfd,
104 const char *npath)
105 {
106 int serrno = errno;
107 qemu_renameat(odirfd, opath, ndirfd, npath);
108 errno = serrno;
109 }
110
111 static void unlinkat_preserve_errno(int dirfd, const char *path, int flags)
112 {
113 int serrno = errno;
114 qemu_unlinkat(dirfd, path, flags);
115 errno = serrno;
116 }
117
118 #define VIRTFS_META_DIR ".virtfs_metadata"
119 #define VIRTFS_META_ROOT_FILE VIRTFS_META_DIR "_root"
120
121 static FILE *local_fopenat(int dirfd, const char *name, const char *mode)
122 {
123 int fd, o_mode = 0;
124 FILE *fp;
125 int flags;
126 /*
127 * only supports two modes
128 */
129 if (mode[0] == 'r') {
130 flags = O_RDONLY;
131 } else if (mode[0] == 'w') {
132 flags = O_WRONLY | O_TRUNC | O_CREAT;
133 o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
134 } else {
135 return NULL;
136 }
137 fd = openat_file(dirfd, name, flags, o_mode);
138 if (fd == -1) {
139 return NULL;
140 }
141 fp = fdopen(fd, mode);
142 if (!fp) {
143 close(fd);
144 }
145 return fp;
146 }
147
148 #define ATTR_MAX 100
149 static void local_mapped_file_attr(int dirfd, const char *name,
150 struct stat *stbuf)
151 {
152 FILE *fp;
153 char buf[ATTR_MAX];
154 int map_dirfd;
155
156 if (strcmp(name, ".")) {
157 map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
158 if (map_dirfd == -1) {
159 return;
160 }
161
162 fp = local_fopenat(map_dirfd, name, "r");
163 close_preserve_errno(map_dirfd);
164 } else {
165 fp = local_fopenat(dirfd, VIRTFS_META_ROOT_FILE, "r");
166 }
167 if (!fp) {
168 return;
169 }
170 memset(buf, 0, ATTR_MAX);
171 while (fgets(buf, ATTR_MAX, fp)) {
172 if (!strncmp(buf, "virtfs.uid", 10)) {
173 stbuf->st_uid = atoi(buf + 11);
174 } else if (!strncmp(buf, "virtfs.gid", 10)) {
175 stbuf->st_gid = atoi(buf + 11);
176 } else if (!strncmp(buf, "virtfs.mode", 11)) {
177 stbuf->st_mode = atoi(buf + 12);
178 } else if (!strncmp(buf, "virtfs.rdev", 11)) {
179 stbuf->st_rdev = atoi(buf + 12);
180 }
181 memset(buf, 0, ATTR_MAX);
182 }
183 fclose(fp);
184 }
185
186 static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf)
187 {
188 int err = -1;
189 char *dirpath = g_path_get_dirname(fs_path->data);
190 char *name = g_path_get_basename(fs_path->data);
191 int dirfd;
192
193 dirfd = local_opendir_nofollow(fs_ctx, dirpath);
194 if (dirfd == -1) {
195 goto out;
196 }
197
198 err = qemu_fstatat(dirfd, name, stbuf, AT_SYMLINK_NOFOLLOW);
199 if (err) {
200 goto err_out;
201 }
202 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
203 /* Actual credentials are part of extended attrs */
204 uid_t tmp_uid;
205 gid_t tmp_gid;
206 mode_t tmp_mode;
207 dev_t tmp_dev;
208
209 if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.uid", &tmp_uid,
210 sizeof(uid_t)) > 0) {
211 stbuf->st_uid = le32_to_cpu(tmp_uid);
212 }
213 if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.gid", &tmp_gid,
214 sizeof(gid_t)) > 0) {
215 stbuf->st_gid = le32_to_cpu(tmp_gid);
216 }
217 if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.mode", &tmp_mode,
218 sizeof(mode_t)) > 0) {
219 stbuf->st_mode = le32_to_cpu(tmp_mode);
220 }
221 if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.rdev", &tmp_dev,
222 sizeof(dev_t)) > 0) {
223 stbuf->st_rdev = le64_to_cpu(tmp_dev);
224 }
225 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
226 local_mapped_file_attr(dirfd, name, stbuf);
227 }
228
229 err_out:
230 close_preserve_errno(dirfd);
231 out:
232 g_free(name);
233 g_free(dirpath);
234 return err;
235 }
236
237 static int local_set_mapped_file_attrat(int dirfd, const char *name,
238 FsCred *credp)
239 {
240 FILE *fp;
241 int ret;
242 char buf[ATTR_MAX];
243 int uid = -1, gid = -1, mode = -1, rdev = -1;
244 int map_dirfd = -1, map_fd;
245 bool is_root = !strcmp(name, ".");
246
247 if (is_root) {
248 fp = local_fopenat(dirfd, VIRTFS_META_ROOT_FILE, "r");
249 if (!fp) {
250 if (errno == ENOENT) {
251 goto update_map_file;
252 } else {
253 return -1;
254 }
255 }
256 } else {
257 ret = qemu_mkdirat(dirfd, VIRTFS_META_DIR, 0700);
258 if (ret < 0 && errno != EEXIST) {
259 return -1;
260 }
261
262 map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
263 if (map_dirfd == -1) {
264 return -1;
265 }
266
267 fp = local_fopenat(map_dirfd, name, "r");
268 if (!fp) {
269 if (errno == ENOENT) {
270 goto update_map_file;
271 } else {
272 close_preserve_errno(map_dirfd);
273 return -1;
274 }
275 }
276 }
277 memset(buf, 0, ATTR_MAX);
278 while (fgets(buf, ATTR_MAX, fp)) {
279 if (!strncmp(buf, "virtfs.uid", 10)) {
280 uid = atoi(buf + 11);
281 } else if (!strncmp(buf, "virtfs.gid", 10)) {
282 gid = atoi(buf + 11);
283 } else if (!strncmp(buf, "virtfs.mode", 11)) {
284 mode = atoi(buf + 12);
285 } else if (!strncmp(buf, "virtfs.rdev", 11)) {
286 rdev = atoi(buf + 12);
287 }
288 memset(buf, 0, ATTR_MAX);
289 }
290 fclose(fp);
291
292 update_map_file:
293 if (is_root) {
294 fp = local_fopenat(dirfd, VIRTFS_META_ROOT_FILE, "w");
295 } else {
296 fp = local_fopenat(map_dirfd, name, "w");
297 /* We can't go this far with map_dirfd not being a valid file descriptor
298 * but some versions of gcc aren't smart enough to see it.
299 */
300 if (map_dirfd != -1) {
301 close_preserve_errno(map_dirfd);
302 }
303 }
304 if (!fp) {
305 return -1;
306 }
307
308 map_fd = fileno(fp);
309 assert(map_fd != -1);
310 ret = fchmod(map_fd, 0600);
311 assert(ret == 0);
312
313 if (credp->fc_uid != -1) {
314 uid = credp->fc_uid;
315 }
316 if (credp->fc_gid != -1) {
317 gid = credp->fc_gid;
318 }
319 if (credp->fc_mode != (mode_t)-1) {
320 mode = credp->fc_mode;
321 }
322 if (credp->fc_rdev != -1) {
323 rdev = credp->fc_rdev;
324 }
325
326 if (uid != -1) {
327 fprintf(fp, "virtfs.uid=%d\n", uid);
328 }
329 if (gid != -1) {
330 fprintf(fp, "virtfs.gid=%d\n", gid);
331 }
332 if (mode != -1) {
333 fprintf(fp, "virtfs.mode=%d\n", mode);
334 }
335 if (rdev != -1) {
336 fprintf(fp, "virtfs.rdev=%d\n", rdev);
337 }
338 fclose(fp);
339
340 return 0;
341 }
342
343 static int fchmodat_nofollow(int dirfd, const char *name, mode_t mode)
344 {
345 struct stat stbuf;
346 int fd, ret;
347
348 /* FIXME: this should be handled with fchmodat(AT_SYMLINK_NOFOLLOW).
349 * Unfortunately, the linux kernel doesn't implement it yet.
350 */
351
352 /* First, we clear non-racing symlinks out of the way. */
353 if (qemu_fstatat(dirfd, name, &stbuf, AT_SYMLINK_NOFOLLOW)) {
354 return -1;
355 }
356 if (S_ISLNK(stbuf.st_mode)) {
357 errno = ELOOP;
358 return -1;
359 }
360
361 fd = openat_file(dirfd, name, O_RDONLY | O_PATH_9P_UTIL | O_NOFOLLOW, 0);
362 #if O_PATH_9P_UTIL == 0
363 /* Fallback for systems that don't support O_PATH: we depend on the file
364 * being readable or writable.
365 */
366 if (fd == -1) {
367 /* In case the file is writable-only and isn't a directory. */
368 if (errno == EACCES) {
369 fd = openat_file(dirfd, name, O_WRONLY, 0);
370 }
371 if (fd == -1 && errno == EISDIR) {
372 errno = EACCES;
373 }
374 }
375 if (fd == -1) {
376 return -1;
377 }
378 ret = fchmod(fd, mode);
379 #else
380 /* Access modes are ignored when O_PATH is supported. If name is a symbolic
381 * link, O_PATH | O_NOFOLLOW causes openat(2) to return a file descriptor
382 * referring to the symbolic link.
383 */
384 if (fd == -1) {
385 return -1;
386 }
387
388 /* Now we handle racing symlinks. */
389 ret = fstat(fd, &stbuf);
390 if (!ret) {
391 if (S_ISLNK(stbuf.st_mode)) {
392 errno = ELOOP;
393 ret = -1;
394 } else {
395 char *proc_path = g_strdup_printf("/proc/self/fd/%d", fd);
396 ret = chmod(proc_path, mode);
397 g_free(proc_path);
398 }
399 }
400 #endif
401 close_preserve_errno(fd);
402 return ret;
403 }
404
405 static int local_set_xattrat(int dirfd, const char *path, FsCred *credp)
406 {
407 int err;
408
409 if (credp->fc_uid != -1) {
410 uint32_t tmp_uid = cpu_to_le32(credp->fc_uid);
411 err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.uid", &tmp_uid,
412 sizeof(uid_t), 0);
413 if (err) {
414 return err;
415 }
416 }
417 if (credp->fc_gid != -1) {
418 uint32_t tmp_gid = cpu_to_le32(credp->fc_gid);
419 err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.gid", &tmp_gid,
420 sizeof(gid_t), 0);
421 if (err) {
422 return err;
423 }
424 }
425 if (credp->fc_mode != (mode_t)-1) {
426 uint32_t tmp_mode = cpu_to_le32(credp->fc_mode);
427 err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.mode", &tmp_mode,
428 sizeof(mode_t), 0);
429 if (err) {
430 return err;
431 }
432 }
433 if (credp->fc_rdev != -1) {
434 uint64_t tmp_rdev = cpu_to_le64(credp->fc_rdev);
435 err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.rdev", &tmp_rdev,
436 sizeof(dev_t), 0);
437 if (err) {
438 return err;
439 }
440 }
441 return 0;
442 }
443
444 static int local_set_cred_passthrough(FsContext *fs_ctx, int dirfd,
445 const char *name, FsCred *credp)
446 {
447 if (fchownat(dirfd, name, credp->fc_uid, credp->fc_gid,
448 AT_SYMLINK_NOFOLLOW) < 0) {
449 /*
450 * If we fail to change ownership and if we are
451 * using security model none. Ignore the error
452 */
453 if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
454 return -1;
455 }
456 }
457
458 return fchmodat_nofollow(dirfd, name, credp->fc_mode & 07777);
459 }
460
461 static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
462 char *buf, size_t bufsz)
463 {
464 ssize_t tsize = -1;
465
466 if ((fs_ctx->export_flags & V9FS_SM_MAPPED) ||
467 (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
468 int fd;
469
470 fd = local_open_nofollow(fs_ctx, fs_path->data, O_RDONLY, 0);
471 if (fd == -1) {
472 if (errno == ELOOP) {
473 goto native_symlink;
474 }
475 return -1;
476 }
477 tsize = RETRY_ON_EINTR(read(fd, (void *)buf, bufsz));
478 close_preserve_errno(fd);
479 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
480 (fs_ctx->export_flags & V9FS_SM_NONE)) {
481 native_symlink:;
482 char *dirpath = g_path_get_dirname(fs_path->data);
483 char *name = g_path_get_basename(fs_path->data);
484 int dirfd;
485
486 dirfd = local_opendir_nofollow(fs_ctx, dirpath);
487 if (dirfd == -1) {
488 goto out;
489 }
490
491 tsize = readlinkat(dirfd, name, buf, bufsz);
492 close_preserve_errno(dirfd);
493 out:
494 g_free(name);
495 g_free(dirpath);
496 }
497 return tsize;
498 }
499
500 static int local_close(FsContext *ctx, V9fsFidOpenState *fs)
501 {
502 return close(fs->fd);
503 }
504
505 static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs)
506 {
507 return closedir(fs->dir.stream);
508 }
509
510 static int local_open(FsContext *ctx, V9fsPath *fs_path,
511 int flags, V9fsFidOpenState *fs)
512 {
513 int fd;
514
515 fd = local_open_nofollow(ctx, fs_path->data, flags, 0);
516 if (fd == -1) {
517 return -1;
518 }
519 fs->fd = fd;
520 return fs->fd;
521 }
522
523 static int local_opendir(FsContext *ctx,
524 V9fsPath *fs_path, V9fsFidOpenState *fs)
525 {
526 int dirfd;
527 DIR *stream;
528
529 dirfd = local_opendir_nofollow(ctx, fs_path->data);
530 if (dirfd == -1) {
531 return -1;
532 }
533
534 stream = fdopendir(dirfd);
535 if (!stream) {
536 close(dirfd);
537 return -1;
538 }
539 fs->dir.stream = stream;
540 return 0;
541 }
542
543 static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
544 {
545 rewinddir(fs->dir.stream);
546 }
547
548 static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs)
549 {
550 return telldir(fs->dir.stream);
551 }
552
553 static bool local_is_mapped_file_metadata(FsContext *fs_ctx, const char *name)
554 {
555 return
556 !strcmp(name, VIRTFS_META_DIR) || !strcmp(name, VIRTFS_META_ROOT_FILE);
557 }
558
559 static struct dirent *local_readdir(FsContext *ctx, V9fsFidOpenState *fs)
560 {
561 struct dirent *entry;
562
563 again:
564 entry = readdir(fs->dir.stream);
565 if (!entry) {
566 return NULL;
567 }
568 #ifdef CONFIG_DARWIN
569 int off;
570 off = telldir(fs->dir.stream);
571 /* If telldir fails, fail the entire readdir call */
572 if (off < 0) {
573 return NULL;
574 }
575 entry->d_seekoff = off;
576 #endif
577
578 if (ctx->export_flags & V9FS_SM_MAPPED) {
579 entry->d_type = DT_UNKNOWN;
580 } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
581 if (local_is_mapped_file_metadata(ctx, entry->d_name)) {
582 /* skip the meta data */
583 goto again;
584 }
585 entry->d_type = DT_UNKNOWN;
586 }
587
588 return entry;
589 }
590
591 static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
592 {
593 seekdir(fs->dir.stream, off);
594 }
595
596 static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs,
597 const struct iovec *iov,
598 int iovcnt, off_t offset)
599 {
600 #ifdef CONFIG_PREADV
601 return preadv(fs->fd, iov, iovcnt, offset);
602 #else
603 int err = lseek(fs->fd, offset, SEEK_SET);
604 if (err == -1) {
605 return err;
606 } else {
607 return readv(fs->fd, iov, iovcnt);
608 }
609 #endif
610 }
611
612 static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
613 const struct iovec *iov,
614 int iovcnt, off_t offset)
615 {
616 ssize_t ret;
617 #ifdef CONFIG_PREADV
618 ret = pwritev(fs->fd, iov, iovcnt, offset);
619 #else
620 int err = lseek(fs->fd, offset, SEEK_SET);
621 if (err == -1) {
622 return err;
623 } else {
624 ret = writev(fs->fd, iov, iovcnt);
625 }
626 #endif
627 #ifdef CONFIG_SYNC_FILE_RANGE
628 if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
629 /*
630 * Initiate a writeback. This is not a data integrity sync.
631 * We want to ensure that we don't leave dirty pages in the cache
632 * after write when writeout=immediate is specified.
633 */
634 sync_file_range(fs->fd, offset, ret,
635 SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
636 }
637 #endif
638 return ret;
639 }
640
641 static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
642 {
643 char *dirpath = g_path_get_dirname(fs_path->data);
644 char *name = g_path_get_basename(fs_path->data);
645 int ret = -1;
646 int dirfd;
647
648 dirfd = local_opendir_nofollow(fs_ctx, dirpath);
649 if (dirfd == -1) {
650 goto out;
651 }
652
653 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
654 ret = local_set_xattrat(dirfd, name, credp);
655 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
656 ret = local_set_mapped_file_attrat(dirfd, name, credp);
657 } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
658 fs_ctx->export_flags & V9FS_SM_NONE) {
659 ret = fchmodat_nofollow(dirfd, name, credp->fc_mode);
660 }
661 close_preserve_errno(dirfd);
662
663 out:
664 g_free(dirpath);
665 g_free(name);
666 return ret;
667 }
668
669 static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
670 const char *name, FsCred *credp)
671 {
672 int err = -1;
673 int dirfd;
674
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;
679 }
680
681 dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
682 if (dirfd == -1) {
683 return -1;
684 }
685
686 if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
687 fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
688 err = qemu_mknodat(dirfd, name, fs_ctx->fmode | S_IFREG, 0);
689 if (err == -1) {
690 goto out;
691 }
692
693 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
694 err = local_set_xattrat(dirfd, name, credp);
695 } else {
696 err = local_set_mapped_file_attrat(dirfd, name, credp);
697 }
698 if (err == -1) {
699 goto err_end;
700 }
701 } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
702 fs_ctx->export_flags & V9FS_SM_NONE) {
703 err = qemu_mknodat(dirfd, name, credp->fc_mode, credp->fc_rdev);
704 if (err == -1) {
705 goto out;
706 }
707 err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp);
708 if (err == -1) {
709 goto err_end;
710 }
711 }
712 goto out;
713
714 err_end:
715 unlinkat_preserve_errno(dirfd, name, 0);
716 out:
717 close_preserve_errno(dirfd);
718 return err;
719 }
720
721 static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
722 const char *name, FsCred *credp)
723 {
724 int err = -1;
725 int dirfd;
726
727 if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE &&
728 local_is_mapped_file_metadata(fs_ctx, name)) {
729 errno = EINVAL;
730 return -1;
731 }
732
733 dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
734 if (dirfd == -1) {
735 return -1;
736 }
737
738 if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
739 fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
740 err = qemu_mkdirat(dirfd, name, fs_ctx->dmode);
741 if (err == -1) {
742 goto out;
743 }
744 credp->fc_mode = credp->fc_mode | S_IFDIR;
745
746 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
747 err = local_set_xattrat(dirfd, name, credp);
748 } else {
749 err = local_set_mapped_file_attrat(dirfd, name, credp);
750 }
751 if (err == -1) {
752 goto err_end;
753 }
754 } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
755 fs_ctx->export_flags & V9FS_SM_NONE) {
756 err = qemu_mkdirat(dirfd, name, credp->fc_mode);
757 if (err == -1) {
758 goto out;
759 }
760 err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp);
761 if (err == -1) {
762 goto err_end;
763 }
764 }
765 goto out;
766
767 err_end:
768 unlinkat_preserve_errno(dirfd, name, AT_REMOVEDIR);
769 out:
770 close_preserve_errno(dirfd);
771 return err;
772 }
773
774 static int local_fid_fd(int fid_type, V9fsFidOpenState *fs)
775 {
776 if (fid_type == P9_FID_DIR) {
777 return dirfd(fs->dir.stream);
778 } else if (fid_type == P9_FID_FILE) {
779 return fs->fd;
780 } else {
781 errno = EBADF;
782 return -1;
783 }
784 }
785
786 static int local_fstat(FsContext *fs_ctx, int fid_type,
787 V9fsFidOpenState *fs, struct stat *stbuf)
788 {
789 int err, fd = local_fid_fd(fid_type, fs);
790
791 err = fstat(fd, stbuf);
792 if (err) {
793 return err;
794 }
795 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
796 /* Actual credentials are part of extended attrs */
797 uid_t tmp_uid;
798 gid_t tmp_gid;
799 mode_t tmp_mode;
800 dev_t tmp_dev;
801
802 if (qemu_fgetxattr(fd, "user.virtfs.uid",
803 &tmp_uid, sizeof(uid_t)) > 0) {
804 stbuf->st_uid = le32_to_cpu(tmp_uid);
805 }
806 if (qemu_fgetxattr(fd, "user.virtfs.gid",
807 &tmp_gid, sizeof(gid_t)) > 0) {
808 stbuf->st_gid = le32_to_cpu(tmp_gid);
809 }
810 if (qemu_fgetxattr(fd, "user.virtfs.mode",
811 &tmp_mode, sizeof(mode_t)) > 0) {
812 stbuf->st_mode = le32_to_cpu(tmp_mode);
813 }
814 if (qemu_fgetxattr(fd, "user.virtfs.rdev",
815 &tmp_dev, sizeof(dev_t)) > 0) {
816 stbuf->st_rdev = le64_to_cpu(tmp_dev);
817 }
818 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
819 errno = EOPNOTSUPP;
820 return -1;
821 }
822 return err;
823 }
824
825 static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
826 int flags, FsCred *credp, V9fsFidOpenState *fs)
827 {
828 int fd = -1;
829 int err = -1;
830 int dirfd;
831
832 if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE &&
833 local_is_mapped_file_metadata(fs_ctx, name)) {
834 errno = EINVAL;
835 return -1;
836 }
837
838 /*
839 * Mark all the open to not follow symlinks
840 */
841 flags |= O_NOFOLLOW;
842
843 dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
844 if (dirfd == -1) {
845 return -1;
846 }
847
848 /* Determine the security model */
849 if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
850 fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
851 fd = openat_file(dirfd, name, flags, fs_ctx->fmode);
852 if (fd == -1) {
853 goto out;
854 }
855 credp->fc_mode = credp->fc_mode | S_IFREG;
856 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
857 /* Set client credentials in xattr */
858 err = local_set_xattrat(dirfd, name, credp);
859 } else {
860 err = local_set_mapped_file_attrat(dirfd, name, credp);
861 }
862 if (err == -1) {
863 goto err_end;
864 }
865 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
866 (fs_ctx->export_flags & V9FS_SM_NONE)) {
867 fd = openat_file(dirfd, name, flags, credp->fc_mode);
868 if (fd == -1) {
869 goto out;
870 }
871 err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp);
872 if (err == -1) {
873 goto err_end;
874 }
875 }
876 err = fd;
877 fs->fd = fd;
878 goto out;
879
880 err_end:
881 unlinkat_preserve_errno(dirfd, name,
882 flags & O_DIRECTORY ? AT_REMOVEDIR : 0);
883 close_preserve_errno(fd);
884 out:
885 close_preserve_errno(dirfd);
886 return err;
887 }
888
889
890 static int local_symlink(FsContext *fs_ctx, const char *oldpath,
891 V9fsPath *dir_path, const char *name, FsCred *credp)
892 {
893 int err = -1;
894 int dirfd;
895
896 if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE &&
897 local_is_mapped_file_metadata(fs_ctx, name)) {
898 errno = EINVAL;
899 return -1;
900 }
901
902 dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
903 if (dirfd == -1) {
904 return -1;
905 }
906
907 /* Determine the security model */
908 if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
909 fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
910 int fd;
911 ssize_t oldpath_size, write_size;
912
913 fd = openat_file(dirfd, name, O_CREAT | O_EXCL | O_RDWR,
914 fs_ctx->fmode);
915 if (fd == -1) {
916 goto out;
917 }
918 /* Write the oldpath (target) to the file. */
919 oldpath_size = strlen(oldpath);
920 write_size = RETRY_ON_EINTR(write(fd, (void *)oldpath, oldpath_size));
921 close_preserve_errno(fd);
922
923 if (write_size != oldpath_size) {
924 goto err_end;
925 }
926 /* Set client credentials in symlink's xattr */
927 credp->fc_mode = credp->fc_mode | S_IFLNK;
928
929 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
930 err = local_set_xattrat(dirfd, name, credp);
931 } else {
932 err = local_set_mapped_file_attrat(dirfd, name, credp);
933 }
934 if (err == -1) {
935 goto err_end;
936 }
937 } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
938 fs_ctx->export_flags & V9FS_SM_NONE) {
939 err = symlinkat(oldpath, dirfd, name);
940 if (err) {
941 goto out;
942 }
943 err = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid,
944 AT_SYMLINK_NOFOLLOW);
945 if (err == -1) {
946 /*
947 * If we fail to change ownership and if we are
948 * using security model none. Ignore the error
949 */
950 if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
951 goto err_end;
952 } else {
953 err = 0;
954 }
955 }
956 }
957 goto out;
958
959 err_end:
960 unlinkat_preserve_errno(dirfd, name, 0);
961 out:
962 close_preserve_errno(dirfd);
963 return err;
964 }
965
966 static int local_link(FsContext *ctx, V9fsPath *oldpath,
967 V9fsPath *dirpath, const char *name)
968 {
969 char *odirpath = g_path_get_dirname(oldpath->data);
970 char *oname = g_path_get_basename(oldpath->data);
971 int ret = -1;
972 int odirfd, ndirfd;
973
974 if (ctx->export_flags & V9FS_SM_MAPPED_FILE &&
975 local_is_mapped_file_metadata(ctx, name)) {
976 errno = EINVAL;
977 goto out;
978 }
979
980 odirfd = local_opendir_nofollow(ctx, odirpath);
981 if (odirfd == -1) {
982 goto out;
983 }
984
985 ndirfd = local_opendir_nofollow(ctx, dirpath->data);
986 if (ndirfd == -1) {
987 close_preserve_errno(odirfd);
988 goto out;
989 }
990
991 ret = linkat(odirfd, oname, ndirfd, name, 0);
992 if (ret < 0) {
993 goto out_close;
994 }
995
996 /* now link the virtfs_metadata files */
997 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
998 int omap_dirfd, nmap_dirfd;
999
1000 ret = qemu_mkdirat(ndirfd, VIRTFS_META_DIR, 0700);
1001 if (ret < 0 && errno != EEXIST) {
1002 goto err_undo_link;
1003 }
1004
1005 omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR);
1006 if (omap_dirfd == -1) {
1007 goto err;
1008 }
1009
1010 nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR);
1011 if (nmap_dirfd == -1) {
1012 close_preserve_errno(omap_dirfd);
1013 goto err;
1014 }
1015
1016 ret = linkat(omap_dirfd, oname, nmap_dirfd, name, 0);
1017 close_preserve_errno(nmap_dirfd);
1018 close_preserve_errno(omap_dirfd);
1019 if (ret < 0 && errno != ENOENT) {
1020 goto err_undo_link;
1021 }
1022
1023 ret = 0;
1024 }
1025 goto out_close;
1026
1027 err:
1028 ret = -1;
1029 err_undo_link:
1030 unlinkat_preserve_errno(ndirfd, name, 0);
1031 out_close:
1032 close_preserve_errno(ndirfd);
1033 close_preserve_errno(odirfd);
1034 out:
1035 g_free(oname);
1036 g_free(odirpath);
1037 return ret;
1038 }
1039
1040 static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
1041 {
1042 int fd, ret;
1043
1044 fd = local_open_nofollow(ctx, fs_path->data, O_WRONLY, 0);
1045 if (fd == -1) {
1046 return -1;
1047 }
1048 ret = ftruncate(fd, size);
1049 close_preserve_errno(fd);
1050 return ret;
1051 }
1052
1053 static int local_ftruncate(FsContext *ctx, int fid_type, V9fsFidOpenState *fs,
1054 off_t size)
1055 {
1056 int fd = local_fid_fd(fid_type, fs);
1057
1058 return ftruncate(fd, size);
1059 }
1060
1061 static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
1062 {
1063 char *dirpath = g_path_get_dirname(fs_path->data);
1064 char *name = g_path_get_basename(fs_path->data);
1065 int ret = -1;
1066 int dirfd;
1067
1068 dirfd = local_opendir_nofollow(fs_ctx, dirpath);
1069 if (dirfd == -1) {
1070 goto out;
1071 }
1072
1073 if ((credp->fc_uid == -1 && credp->fc_gid == -1) ||
1074 (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
1075 (fs_ctx->export_flags & V9FS_SM_NONE)) {
1076 ret = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid,
1077 AT_SYMLINK_NOFOLLOW);
1078 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
1079 ret = local_set_xattrat(dirfd, name, credp);
1080 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1081 ret = local_set_mapped_file_attrat(dirfd, name, credp);
1082 }
1083
1084 close_preserve_errno(dirfd);
1085 out:
1086 g_free(name);
1087 g_free(dirpath);
1088 return ret;
1089 }
1090
1091 static int local_utimensat(FsContext *s, V9fsPath *fs_path,
1092 const struct timespec *buf)
1093 {
1094 char *dirpath = g_path_get_dirname(fs_path->data);
1095 char *name = g_path_get_basename(fs_path->data);
1096 int dirfd, ret = -1;
1097
1098 dirfd = local_opendir_nofollow(s, dirpath);
1099 if (dirfd == -1) {
1100 goto out;
1101 }
1102
1103 ret = qemu_utimensat(dirfd, name, buf, AT_SYMLINK_NOFOLLOW);
1104 close_preserve_errno(dirfd);
1105 out:
1106 g_free(dirpath);
1107 g_free(name);
1108 return ret;
1109 }
1110
1111 static int local_futimens(FsContext *s, int fid_type, V9fsFidOpenState *fs,
1112 const struct timespec *times)
1113 {
1114 int fd = local_fid_fd(fid_type, fs);
1115
1116 return qemu_futimens(fd, times);
1117 }
1118
1119 static int local_unlinkat_common(FsContext *ctx, int dirfd, const char *name,
1120 int flags)
1121 {
1122 int ret;
1123
1124 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1125 int map_dirfd;
1126
1127 /* We need to remove the metadata as well:
1128 * - the metadata directory if we're removing a directory
1129 * - the metadata file in the parent's metadata directory
1130 *
1131 * If any of these are missing (ie, ENOENT) then we're probably
1132 * trying to remove something that wasn't created in mapped-file
1133 * mode. We just ignore the error.
1134 */
1135 if (flags == AT_REMOVEDIR) {
1136 int fd;
1137
1138 fd = openat_dir(dirfd, name);
1139 if (fd == -1) {
1140 return -1;
1141 }
1142 ret = qemu_unlinkat(fd, VIRTFS_META_DIR, AT_REMOVEDIR);
1143 close_preserve_errno(fd);
1144 if (ret < 0 && errno != ENOENT) {
1145 return -1;
1146 }
1147 }
1148 map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
1149 if (map_dirfd != -1) {
1150 ret = qemu_unlinkat(map_dirfd, name, 0);
1151 close_preserve_errno(map_dirfd);
1152 if (ret < 0 && errno != ENOENT) {
1153 return -1;
1154 }
1155 } else if (errno != ENOENT) {
1156 return -1;
1157 }
1158 }
1159
1160 return qemu_unlinkat(dirfd, name, flags);
1161 }
1162
1163 static int local_remove(FsContext *ctx, const char *path)
1164 {
1165 struct stat stbuf;
1166 char *dirpath = g_path_get_dirname(path);
1167 char *name = g_path_get_basename(path);
1168 int flags = 0;
1169 int dirfd;
1170 int err = -1;
1171
1172 dirfd = local_opendir_nofollow(ctx, dirpath);
1173 if (dirfd == -1) {
1174 goto out;
1175 }
1176
1177 if (qemu_fstatat(dirfd, name, &stbuf, AT_SYMLINK_NOFOLLOW) < 0) {
1178 goto err_out;
1179 }
1180
1181 if (S_ISDIR(stbuf.st_mode)) {
1182 flags |= AT_REMOVEDIR;
1183 }
1184
1185 err = local_unlinkat_common(ctx, dirfd, name, flags);
1186 err_out:
1187 close_preserve_errno(dirfd);
1188 out:
1189 g_free(name);
1190 g_free(dirpath);
1191 return err;
1192 }
1193
1194 static int local_fsync(FsContext *ctx, int fid_type,
1195 V9fsFidOpenState *fs, int datasync)
1196 {
1197 int fd = local_fid_fd(fid_type, fs);
1198
1199 if (datasync) {
1200 return qemu_fdatasync(fd);
1201 } else {
1202 return fsync(fd);
1203 }
1204 }
1205
1206 static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
1207 {
1208 int fd, ret;
1209
1210 fd = local_open_nofollow(s, fs_path->data, O_RDONLY, 0);
1211 if (fd == -1) {
1212 return -1;
1213 }
1214 ret = fstatfs(fd, stbuf);
1215 close_preserve_errno(fd);
1216 return ret;
1217 }
1218
1219 static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
1220 const char *name, void *value, size_t size)
1221 {
1222 char *path = fs_path->data;
1223
1224 return v9fs_get_xattr(ctx, path, name, value, size);
1225 }
1226
1227 static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path,
1228 void *value, size_t size)
1229 {
1230 char *path = fs_path->data;
1231
1232 return v9fs_list_xattr(ctx, path, value, size);
1233 }
1234
1235 static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
1236 void *value, size_t size, int flags)
1237 {
1238 char *path = fs_path->data;
1239
1240 return v9fs_set_xattr(ctx, path, name, value, size, flags);
1241 }
1242
1243 static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
1244 const char *name)
1245 {
1246 char *path = fs_path->data;
1247
1248 return v9fs_remove_xattr(ctx, path, name);
1249 }
1250
1251 static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path,
1252 const char *name, V9fsPath *target)
1253 {
1254 if (ctx->export_flags & V9FS_SM_MAPPED_FILE &&
1255 local_is_mapped_file_metadata(ctx, name)) {
1256 errno = EINVAL;
1257 return -1;
1258 }
1259
1260 if (dir_path) {
1261 if (!strcmp(name, ".")) {
1262 /* "." relative to "foo/bar" is "foo/bar" */
1263 v9fs_path_copy(target, dir_path);
1264 } else if (!strcmp(name, "..")) {
1265 if (!strcmp(dir_path->data, ".")) {
1266 /* ".." relative to the root is "." */
1267 if (v9fs_path_sprintf(target, ".") < 0) {
1268 return -1;
1269 }
1270 } else {
1271 g_autofree char *tmp = g_path_get_dirname(dir_path->data);
1272 /* Symbolic links are resolved by the client. We can assume
1273 * that ".." relative to "foo/bar" is equivalent to "foo"
1274 */
1275 if (v9fs_path_sprintf(target, "%s", tmp) < 0) {
1276 return -1;
1277 }
1278 }
1279 } else {
1280 assert(!strchr(name, '/'));
1281 if (v9fs_path_sprintf(target, "%s/%s", dir_path->data, name) < 0) {
1282 return -1;
1283 }
1284 }
1285 } else if (!strcmp(name, "/") || !strcmp(name, ".") ||
1286 !strcmp(name, "..")) {
1287 /* This is the root fid */
1288 if (v9fs_path_sprintf(target, ".") < 0) {
1289 return -1;
1290 }
1291 } else {
1292 assert(!strchr(name, '/'));
1293 if (v9fs_path_sprintf(target, "./%s", name) < 0) {
1294 return -1;
1295 }
1296 }
1297 return 0;
1298 }
1299
1300 static int local_renameat(FsContext *ctx, V9fsPath *olddir,
1301 const char *old_name, V9fsPath *newdir,
1302 const char *new_name)
1303 {
1304 int ret;
1305 int odirfd, ndirfd;
1306
1307 if (ctx->export_flags & V9FS_SM_MAPPED_FILE &&
1308 (local_is_mapped_file_metadata(ctx, old_name) ||
1309 local_is_mapped_file_metadata(ctx, new_name))) {
1310 errno = EINVAL;
1311 return -1;
1312 }
1313
1314 odirfd = local_opendir_nofollow(ctx, olddir->data);
1315 if (odirfd == -1) {
1316 return -1;
1317 }
1318
1319 ndirfd = local_opendir_nofollow(ctx, newdir->data);
1320 if (ndirfd == -1) {
1321 close_preserve_errno(odirfd);
1322 return -1;
1323 }
1324
1325 ret = qemu_renameat(odirfd, old_name, ndirfd, new_name);
1326 if (ret < 0) {
1327 goto out;
1328 }
1329
1330 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1331 int omap_dirfd, nmap_dirfd;
1332
1333 ret = qemu_mkdirat(ndirfd, VIRTFS_META_DIR, 0700);
1334 if (ret < 0 && errno != EEXIST) {
1335 goto err_undo_rename;
1336 }
1337
1338 omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR);
1339 if (omap_dirfd == -1) {
1340 goto err;
1341 }
1342
1343 nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR);
1344 if (nmap_dirfd == -1) {
1345 close_preserve_errno(omap_dirfd);
1346 goto err;
1347 }
1348
1349 /* rename the .virtfs_metadata files */
1350 ret = qemu_renameat(omap_dirfd, old_name, nmap_dirfd, new_name);
1351 close_preserve_errno(nmap_dirfd);
1352 close_preserve_errno(omap_dirfd);
1353 if (ret < 0 && errno != ENOENT) {
1354 goto err_undo_rename;
1355 }
1356
1357 ret = 0;
1358 }
1359 goto out;
1360
1361 err:
1362 ret = -1;
1363 err_undo_rename:
1364 renameat_preserve_errno(ndirfd, new_name, odirfd, old_name);
1365 out:
1366 close_preserve_errno(ndirfd);
1367 close_preserve_errno(odirfd);
1368 return ret;
1369 }
1370
1371 static void v9fs_path_init_dirname(V9fsPath *path, const char *str)
1372 {
1373 path->data = g_path_get_dirname(str);
1374 path->size = strlen(path->data) + 1;
1375 }
1376
1377 static int local_rename(FsContext *ctx, const char *oldpath,
1378 const char *newpath)
1379 {
1380 int err;
1381 char *oname = g_path_get_basename(oldpath);
1382 char *nname = g_path_get_basename(newpath);
1383 V9fsPath olddir, newdir;
1384
1385 v9fs_path_init_dirname(&olddir, oldpath);
1386 v9fs_path_init_dirname(&newdir, newpath);
1387
1388 err = local_renameat(ctx, &olddir, oname, &newdir, nname);
1389
1390 v9fs_path_free(&newdir);
1391 v9fs_path_free(&olddir);
1392 g_free(nname);
1393 g_free(oname);
1394
1395 return err;
1396 }
1397
1398 static int local_unlinkat(FsContext *ctx, V9fsPath *dir,
1399 const char *name, int flags)
1400 {
1401 int ret;
1402 int dirfd;
1403
1404 if (ctx->export_flags & V9FS_SM_MAPPED_FILE &&
1405 local_is_mapped_file_metadata(ctx, name)) {
1406 errno = EINVAL;
1407 return -1;
1408 }
1409
1410 dirfd = local_opendir_nofollow(ctx, dir->data);
1411 if (dirfd == -1) {
1412 return -1;
1413 }
1414
1415 ret = local_unlinkat_common(ctx, dirfd, name, flags);
1416 close_preserve_errno(dirfd);
1417 return ret;
1418 }
1419
1420 #ifdef FS_IOC_GETVERSION
1421 static int local_ioc_getversion(FsContext *ctx, V9fsPath *path,
1422 mode_t st_mode, uint64_t *st_gen)
1423 {
1424 int err;
1425 V9fsFidOpenState fid_open;
1426
1427 /*
1428 * Do not try to open special files like device nodes, fifos etc
1429 * We can get fd for regular files and directories only
1430 */
1431 if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
1432 errno = ENOTTY;
1433 return -1;
1434 }
1435 err = local_open(ctx, path, O_RDONLY, &fid_open);
1436 if (err < 0) {
1437 return err;
1438 }
1439 err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
1440 local_close(ctx, &fid_open);
1441 return err;
1442 }
1443 #endif
1444
1445 static int local_ioc_getversion_init(FsContext *ctx, LocalData *data, Error **errp)
1446 {
1447 #ifdef FS_IOC_GETVERSION
1448 struct statfs stbuf;
1449
1450 /*
1451 * use ioc_getversion only if the ioctl is defined
1452 */
1453 if (fstatfs(data->mountfd, &stbuf) < 0) {
1454 error_setg_errno(errp, errno,
1455 "failed to stat file system at '%s'", ctx->fs_root);
1456 return -1;
1457 }
1458 switch (stbuf.f_type) {
1459 case EXT2_SUPER_MAGIC:
1460 case BTRFS_SUPER_MAGIC:
1461 case REISERFS_SUPER_MAGIC:
1462 case XFS_SUPER_MAGIC:
1463 ctx->exops.get_st_gen = local_ioc_getversion;
1464 break;
1465 }
1466 #endif
1467 return 0;
1468 }
1469
1470 static int local_init(FsContext *ctx, Error **errp)
1471 {
1472 LocalData *data = g_malloc(sizeof(*data));
1473
1474 data->mountfd = open(ctx->fs_root, O_DIRECTORY | O_RDONLY);
1475 if (data->mountfd == -1) {
1476 error_setg_file_open(errp, errno, ctx->fs_root);
1477 goto err;
1478 }
1479
1480 if (local_ioc_getversion_init(ctx, data, errp) < 0) {
1481 close(data->mountfd);
1482 goto err;
1483 }
1484
1485 if (ctx->export_flags & V9FS_SM_PASSTHROUGH) {
1486 ctx->xops = passthrough_xattr_ops;
1487 } else if (ctx->export_flags & V9FS_SM_MAPPED) {
1488 ctx->xops = mapped_xattr_ops;
1489 } else if (ctx->export_flags & V9FS_SM_NONE) {
1490 ctx->xops = none_xattr_ops;
1491 } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1492 /*
1493 * xattr operation for mapped-file and passthrough
1494 * remain same.
1495 */
1496 ctx->xops = passthrough_xattr_ops;
1497 }
1498 ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT;
1499
1500 ctx->private = data;
1501 return 0;
1502
1503 err:
1504 g_free(data);
1505 return -1;
1506 }
1507
1508 static void local_cleanup(FsContext *ctx)
1509 {
1510 LocalData *data = ctx->private;
1511
1512 if (!data) {
1513 return;
1514 }
1515
1516 close(data->mountfd);
1517 g_free(data);
1518 }
1519
1520 static void error_append_security_model_hint(Error *const *errp)
1521 {
1522 error_append_hint(errp, "Valid options are: security_model="
1523 "[passthrough|mapped-xattr|mapped-file|none]\n");
1524 }
1525
1526 static int local_parse_opts(QemuOpts *opts, FsDriverEntry *fse, Error **errp)
1527 {
1528 ERRP_GUARD();
1529 const char *sec_model = qemu_opt_get(opts, "security_model");
1530 const char *path = qemu_opt_get(opts, "path");
1531 const char *multidevs = qemu_opt_get(opts, "multidevs");
1532
1533 uint64_t val = qemu_opt_get_number(opts, "max_xattr",
1534 V9FS_MAX_XATTR_DEFAULT);
1535 if (val > UINT32_MAX) {
1536 error_setg(errp, "max_xattr value '%s' too large",
1537 qemu_opt_get(opts, "max_xattr"));
1538 return -1;
1539 }
1540 fse->max_xattr = val;
1541
1542 if (!sec_model) {
1543 error_setg(errp, "security_model property not set");
1544 error_append_security_model_hint(errp);
1545 return -1;
1546 }
1547
1548 if (!strcmp(sec_model, "passthrough")) {
1549 fse->export_flags |= V9FS_SM_PASSTHROUGH;
1550 } else if (!strcmp(sec_model, "mapped") ||
1551 !strcmp(sec_model, "mapped-xattr")) {
1552 fse->export_flags |= V9FS_SM_MAPPED;
1553 } else if (!strcmp(sec_model, "none")) {
1554 fse->export_flags |= V9FS_SM_NONE;
1555 } else if (!strcmp(sec_model, "mapped-file")) {
1556 fse->export_flags |= V9FS_SM_MAPPED_FILE;
1557 } else {
1558 error_setg(errp, "invalid security_model property '%s'", sec_model);
1559 error_append_security_model_hint(errp);
1560 return -1;
1561 }
1562
1563 if (multidevs) {
1564 if (!strcmp(multidevs, "remap")) {
1565 fse->export_flags &= ~V9FS_FORBID_MULTIDEVS;
1566 fse->export_flags |= V9FS_REMAP_INODES;
1567 } else if (!strcmp(multidevs, "forbid")) {
1568 fse->export_flags &= ~V9FS_REMAP_INODES;
1569 fse->export_flags |= V9FS_FORBID_MULTIDEVS;
1570 } else if (!strcmp(multidevs, "warn")) {
1571 fse->export_flags &= ~V9FS_FORBID_MULTIDEVS;
1572 fse->export_flags &= ~V9FS_REMAP_INODES;
1573 } else {
1574 error_setg(errp, "invalid multidevs property '%s'",
1575 multidevs);
1576 error_append_hint(errp, "Valid options are: multidevs="
1577 "[remap|forbid|warn]\n");
1578 return -1;
1579 }
1580 } else {
1581 fse->export_flags &= ~V9FS_FORBID_MULTIDEVS;
1582 fse->export_flags |= V9FS_REMAP_INODES;
1583 }
1584
1585 if (!path) {
1586 error_setg(errp, "path property not set");
1587 return -1;
1588 }
1589
1590 if (fsdev_throttle_parse_opts(opts, &fse->fst, errp)) {
1591 error_prepend(errp, "invalid throttle configuration: ");
1592 return -1;
1593 }
1594
1595 if (fse->export_flags & V9FS_SM_MAPPED ||
1596 fse->export_flags & V9FS_SM_MAPPED_FILE) {
1597 fse->fmode =
1598 qemu_opt_get_number(opts, "fmode", SM_LOCAL_MODE_BITS) & 0777;
1599 fse->dmode =
1600 qemu_opt_get_number(opts, "dmode", SM_LOCAL_DIR_MODE_BITS) & 0777;
1601 } else {
1602 if (qemu_opt_find(opts, "fmode")) {
1603 error_setg(errp, "fmode is only valid for mapped security modes");
1604 return -1;
1605 }
1606 if (qemu_opt_find(opts, "dmode")) {
1607 error_setg(errp, "dmode is only valid for mapped security modes");
1608 return -1;
1609 }
1610 }
1611
1612 fse->path = g_strdup(path);
1613
1614 return 0;
1615 }
1616
1617 static bool local_has_valid_file_handle(int fid_type, V9fsFidOpenState *fs)
1618 {
1619 return
1620 (fid_type == P9_FID_FILE && fs->fd != -1) ||
1621 (fid_type == P9_FID_DIR && fs->dir.stream != NULL);
1622 }
1623
1624 FileOperations local_ops = {
1625 .parse_opts = local_parse_opts,
1626 .init = local_init,
1627 .cleanup = local_cleanup,
1628 .lstat = local_lstat,
1629 .readlink = local_readlink,
1630 .close = local_close,
1631 .closedir = local_closedir,
1632 .open = local_open,
1633 .opendir = local_opendir,
1634 .rewinddir = local_rewinddir,
1635 .telldir = local_telldir,
1636 .readdir = local_readdir,
1637 .seekdir = local_seekdir,
1638 .preadv = local_preadv,
1639 .pwritev = local_pwritev,
1640 .chmod = local_chmod,
1641 .mknod = local_mknod,
1642 .mkdir = local_mkdir,
1643 .fstat = local_fstat,
1644 .open2 = local_open2,
1645 .symlink = local_symlink,
1646 .link = local_link,
1647 .truncate = local_truncate,
1648 .rename = local_rename,
1649 .chown = local_chown,
1650 .utimensat = local_utimensat,
1651 .remove = local_remove,
1652 .fsync = local_fsync,
1653 .statfs = local_statfs,
1654 .lgetxattr = local_lgetxattr,
1655 .llistxattr = local_llistxattr,
1656 .lsetxattr = local_lsetxattr,
1657 .lremovexattr = local_lremovexattr,
1658 .name_to_path = local_name_to_path,
1659 .renameat = local_renameat,
1660 .unlinkat = local_unlinkat,
1661 .has_valid_file_handle = local_has_valid_file_handle,
1662 .ftruncate = local_ftruncate,
1663 .futimens = local_futimens,
1664 };