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