MAINTAINERS: mark megasas as maintained
[qemu/ar7.git] / hw / 9pfs / virtio-9p-local.c
blob56b302c122b682a00742b28e6fb9f54f007493c5
1 /*
2 * Virtio 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 "hw/virtio/virtio.h"
15 #include "virtio-9p.h"
16 #include "virtio-9p-xattr.h"
17 #include <arpa/inet.h>
18 #include <pwd.h>
19 #include <grp.h>
20 #include <sys/socket.h>
21 #include <sys/un.h>
22 #include "qemu/xattr.h"
23 #include <libgen.h>
24 #include <linux/fs.h>
25 #ifdef CONFIG_LINUX_MAGIC_H
26 #include <linux/magic.h>
27 #endif
28 #include <sys/ioctl.h>
30 #ifndef XFS_SUPER_MAGIC
31 #define XFS_SUPER_MAGIC 0x58465342
32 #endif
33 #ifndef EXT2_SUPER_MAGIC
34 #define EXT2_SUPER_MAGIC 0xEF53
35 #endif
36 #ifndef REISERFS_SUPER_MAGIC
37 #define REISERFS_SUPER_MAGIC 0x52654973
38 #endif
39 #ifndef BTRFS_SUPER_MAGIC
40 #define BTRFS_SUPER_MAGIC 0x9123683E
41 #endif
43 #define VIRTFS_META_DIR ".virtfs_metadata"
45 static char *local_mapped_attr_path(FsContext *ctx, const char *path)
47 char *dir_name;
48 char *tmp_path = g_strdup(path);
49 char *base_name = basename(tmp_path);
50 char *buffer;
52 /* NULL terminate the directory */
53 dir_name = tmp_path;
54 *(base_name - 1) = '\0';
56 buffer = g_strdup_printf("%s/%s/%s/%s",
57 ctx->fs_root, dir_name, VIRTFS_META_DIR, base_name);
58 g_free(tmp_path);
59 return buffer;
62 static FILE *local_fopen(const char *path, const char *mode)
64 int fd, o_mode = 0;
65 FILE *fp;
66 int flags = O_NOFOLLOW;
68 * only supports two modes
70 if (mode[0] == 'r') {
71 flags |= O_RDONLY;
72 } else if (mode[0] == 'w') {
73 flags |= O_WRONLY | O_TRUNC | O_CREAT;
74 o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
75 } else {
76 return NULL;
78 fd = open(path, flags, o_mode);
79 if (fd == -1) {
80 return NULL;
82 fp = fdopen(fd, mode);
83 if (!fp) {
84 close(fd);
86 return fp;
89 #define ATTR_MAX 100
90 static void local_mapped_file_attr(FsContext *ctx, const char *path,
91 struct stat *stbuf)
93 FILE *fp;
94 char buf[ATTR_MAX];
95 char *attr_path;
97 attr_path = local_mapped_attr_path(ctx, path);
98 fp = local_fopen(attr_path, "r");
99 g_free(attr_path);
100 if (!fp) {
101 return;
103 memset(buf, 0, ATTR_MAX);
104 while (fgets(buf, ATTR_MAX, fp)) {
105 if (!strncmp(buf, "virtfs.uid", 10)) {
106 stbuf->st_uid = atoi(buf+11);
107 } else if (!strncmp(buf, "virtfs.gid", 10)) {
108 stbuf->st_gid = atoi(buf+11);
109 } else if (!strncmp(buf, "virtfs.mode", 11)) {
110 stbuf->st_mode = atoi(buf+12);
111 } else if (!strncmp(buf, "virtfs.rdev", 11)) {
112 stbuf->st_rdev = atoi(buf+12);
114 memset(buf, 0, ATTR_MAX);
116 fclose(fp);
119 static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf)
121 int err;
122 char *buffer;
123 char *path = fs_path->data;
125 buffer = rpath(fs_ctx, path);
126 err = lstat(buffer, stbuf);
127 if (err) {
128 goto err_out;
130 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
131 /* Actual credentials are part of extended attrs */
132 uid_t tmp_uid;
133 gid_t tmp_gid;
134 mode_t tmp_mode;
135 dev_t tmp_dev;
136 if (getxattr(buffer, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) {
137 stbuf->st_uid = tmp_uid;
139 if (getxattr(buffer, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
140 stbuf->st_gid = tmp_gid;
142 if (getxattr(buffer, "user.virtfs.mode",
143 &tmp_mode, sizeof(mode_t)) > 0) {
144 stbuf->st_mode = tmp_mode;
146 if (getxattr(buffer, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
147 stbuf->st_rdev = tmp_dev;
149 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
150 local_mapped_file_attr(fs_ctx, path, stbuf);
153 err_out:
154 g_free(buffer);
155 return err;
158 static int local_create_mapped_attr_dir(FsContext *ctx, const char *path)
160 int err;
161 char *attr_dir;
162 char *tmp_path = g_strdup(path);
164 attr_dir = g_strdup_printf("%s/%s/%s",
165 ctx->fs_root, dirname(tmp_path), VIRTFS_META_DIR);
167 err = mkdir(attr_dir, 0700);
168 if (err < 0 && errno == EEXIST) {
169 err = 0;
171 g_free(attr_dir);
172 g_free(tmp_path);
173 return err;
176 static int local_set_mapped_file_attr(FsContext *ctx,
177 const char *path, FsCred *credp)
179 FILE *fp;
180 int ret = 0;
181 char buf[ATTR_MAX];
182 char *attr_path;
183 int uid = -1, gid = -1, mode = -1, rdev = -1;
185 attr_path = local_mapped_attr_path(ctx, path);
186 fp = local_fopen(attr_path, "r");
187 if (!fp) {
188 goto create_map_file;
190 memset(buf, 0, ATTR_MAX);
191 while (fgets(buf, ATTR_MAX, fp)) {
192 if (!strncmp(buf, "virtfs.uid", 10)) {
193 uid = atoi(buf+11);
194 } else if (!strncmp(buf, "virtfs.gid", 10)) {
195 gid = atoi(buf+11);
196 } else if (!strncmp(buf, "virtfs.mode", 11)) {
197 mode = atoi(buf+12);
198 } else if (!strncmp(buf, "virtfs.rdev", 11)) {
199 rdev = atoi(buf+12);
201 memset(buf, 0, ATTR_MAX);
203 fclose(fp);
204 goto update_map_file;
206 create_map_file:
207 ret = local_create_mapped_attr_dir(ctx, path);
208 if (ret < 0) {
209 goto err_out;
212 update_map_file:
213 fp = local_fopen(attr_path, "w");
214 if (!fp) {
215 ret = -1;
216 goto err_out;
219 if (credp->fc_uid != -1) {
220 uid = credp->fc_uid;
222 if (credp->fc_gid != -1) {
223 gid = credp->fc_gid;
225 if (credp->fc_mode != -1) {
226 mode = credp->fc_mode;
228 if (credp->fc_rdev != -1) {
229 rdev = credp->fc_rdev;
233 if (uid != -1) {
234 fprintf(fp, "virtfs.uid=%d\n", uid);
236 if (gid != -1) {
237 fprintf(fp, "virtfs.gid=%d\n", gid);
239 if (mode != -1) {
240 fprintf(fp, "virtfs.mode=%d\n", mode);
242 if (rdev != -1) {
243 fprintf(fp, "virtfs.rdev=%d\n", rdev);
245 fclose(fp);
247 err_out:
248 g_free(attr_path);
249 return ret;
252 static int local_set_xattr(const char *path, FsCred *credp)
254 int err;
256 if (credp->fc_uid != -1) {
257 err = setxattr(path, "user.virtfs.uid", &credp->fc_uid, sizeof(uid_t),
259 if (err) {
260 return err;
263 if (credp->fc_gid != -1) {
264 err = setxattr(path, "user.virtfs.gid", &credp->fc_gid, sizeof(gid_t),
266 if (err) {
267 return err;
270 if (credp->fc_mode != -1) {
271 err = setxattr(path, "user.virtfs.mode", &credp->fc_mode,
272 sizeof(mode_t), 0);
273 if (err) {
274 return err;
277 if (credp->fc_rdev != -1) {
278 err = setxattr(path, "user.virtfs.rdev", &credp->fc_rdev,
279 sizeof(dev_t), 0);
280 if (err) {
281 return err;
284 return 0;
287 static int local_post_create_passthrough(FsContext *fs_ctx, const char *path,
288 FsCred *credp)
290 char *buffer;
292 buffer = rpath(fs_ctx, path);
293 if (lchown(buffer, credp->fc_uid, credp->fc_gid) < 0) {
295 * If we fail to change ownership and if we are
296 * using security model none. Ignore the error
298 if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
299 goto err;
303 if (chmod(buffer, credp->fc_mode & 07777) < 0) {
304 goto err;
307 g_free(buffer);
308 return 0;
309 err:
310 g_free(buffer);
311 return -1;
314 static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
315 char *buf, size_t bufsz)
317 ssize_t tsize = -1;
318 char *buffer;
319 char *path = fs_path->data;
321 if ((fs_ctx->export_flags & V9FS_SM_MAPPED) ||
322 (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
323 int fd;
324 buffer = rpath(fs_ctx, path);
325 fd = open(buffer, O_RDONLY | O_NOFOLLOW);
326 g_free(buffer);
327 if (fd == -1) {
328 return -1;
330 do {
331 tsize = read(fd, (void *)buf, bufsz);
332 } while (tsize == -1 && errno == EINTR);
333 close(fd);
334 return tsize;
335 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
336 (fs_ctx->export_flags & V9FS_SM_NONE)) {
337 buffer = rpath(fs_ctx, path);
338 tsize = readlink(buffer, buf, bufsz);
339 g_free(buffer);
341 return tsize;
344 static int local_close(FsContext *ctx, V9fsFidOpenState *fs)
346 return close(fs->fd);
349 static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs)
351 return closedir(fs->dir);
354 static int local_open(FsContext *ctx, V9fsPath *fs_path,
355 int flags, V9fsFidOpenState *fs)
357 char *buffer;
358 char *path = fs_path->data;
360 buffer = rpath(ctx, path);
361 fs->fd = open(buffer, flags | O_NOFOLLOW);
362 g_free(buffer);
363 return fs->fd;
366 static int local_opendir(FsContext *ctx,
367 V9fsPath *fs_path, V9fsFidOpenState *fs)
369 char *buffer;
370 char *path = fs_path->data;
372 buffer = rpath(ctx, path);
373 fs->dir = opendir(buffer);
374 g_free(buffer);
375 if (!fs->dir) {
376 return -1;
378 return 0;
381 static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
383 return rewinddir(fs->dir);
386 static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs)
388 return telldir(fs->dir);
391 static int local_readdir_r(FsContext *ctx, V9fsFidOpenState *fs,
392 struct dirent *entry,
393 struct dirent **result)
395 int ret;
397 again:
398 ret = readdir_r(fs->dir, entry, result);
399 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
400 if (!ret && *result != NULL &&
401 !strcmp(entry->d_name, VIRTFS_META_DIR)) {
402 /* skp the meta data directory */
403 goto again;
406 return ret;
409 static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
411 return seekdir(fs->dir, off);
414 static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs,
415 const struct iovec *iov,
416 int iovcnt, off_t offset)
418 #ifdef CONFIG_PREADV
419 return preadv(fs->fd, iov, iovcnt, offset);
420 #else
421 int err = lseek(fs->fd, offset, SEEK_SET);
422 if (err == -1) {
423 return err;
424 } else {
425 return readv(fs->fd, iov, iovcnt);
427 #endif
430 static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
431 const struct iovec *iov,
432 int iovcnt, off_t offset)
434 ssize_t ret
436 #ifdef CONFIG_PREADV
437 ret = pwritev(fs->fd, iov, iovcnt, offset);
438 #else
439 int err = lseek(fs->fd, offset, SEEK_SET);
440 if (err == -1) {
441 return err;
442 } else {
443 ret = writev(fs->fd, iov, iovcnt);
445 #endif
446 #ifdef CONFIG_SYNC_FILE_RANGE
447 if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
449 * Initiate a writeback. This is not a data integrity sync.
450 * We want to ensure that we don't leave dirty pages in the cache
451 * after write when writeout=immediate is sepcified.
453 sync_file_range(fs->fd, offset, ret,
454 SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
456 #endif
457 return ret;
460 static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
462 char *buffer;
463 int ret = -1;
464 char *path = fs_path->data;
466 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
467 buffer = rpath(fs_ctx, path);
468 ret = local_set_xattr(buffer, credp);
469 g_free(buffer);
470 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
471 return local_set_mapped_file_attr(fs_ctx, path, credp);
472 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
473 (fs_ctx->export_flags & V9FS_SM_NONE)) {
474 buffer = rpath(fs_ctx, path);
475 ret = chmod(buffer, credp->fc_mode);
476 g_free(buffer);
478 return ret;
481 static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
482 const char *name, FsCred *credp)
484 char *path;
485 int err = -1;
486 int serrno = 0;
487 V9fsString fullname;
488 char *buffer;
490 v9fs_string_init(&fullname);
491 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
492 path = fullname.data;
494 /* Determine the security model */
495 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
496 buffer = rpath(fs_ctx, path);
497 err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0);
498 if (err == -1) {
499 g_free(buffer);
500 goto out;
502 err = local_set_xattr(buffer, credp);
503 if (err == -1) {
504 serrno = errno;
505 goto err_end;
507 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
509 buffer = rpath(fs_ctx, path);
510 err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0);
511 if (err == -1) {
512 g_free(buffer);
513 goto out;
515 err = local_set_mapped_file_attr(fs_ctx, path, credp);
516 if (err == -1) {
517 serrno = errno;
518 goto err_end;
520 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
521 (fs_ctx->export_flags & V9FS_SM_NONE)) {
522 buffer = rpath(fs_ctx, path);
523 err = mknod(buffer, credp->fc_mode, credp->fc_rdev);
524 if (err == -1) {
525 g_free(buffer);
526 goto out;
528 err = local_post_create_passthrough(fs_ctx, path, credp);
529 if (err == -1) {
530 serrno = errno;
531 goto err_end;
534 goto out;
536 err_end:
537 remove(buffer);
538 errno = serrno;
539 g_free(buffer);
540 out:
541 v9fs_string_free(&fullname);
542 return err;
545 static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
546 const char *name, FsCred *credp)
548 char *path;
549 int err = -1;
550 int serrno = 0;
551 V9fsString fullname;
552 char *buffer;
554 v9fs_string_init(&fullname);
555 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
556 path = fullname.data;
558 /* Determine the security model */
559 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
560 buffer = rpath(fs_ctx, path);
561 err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
562 if (err == -1) {
563 g_free(buffer);
564 goto out;
566 credp->fc_mode = credp->fc_mode|S_IFDIR;
567 err = local_set_xattr(buffer, credp);
568 if (err == -1) {
569 serrno = errno;
570 goto err_end;
572 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
573 buffer = rpath(fs_ctx, path);
574 err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
575 if (err == -1) {
576 g_free(buffer);
577 goto out;
579 credp->fc_mode = credp->fc_mode|S_IFDIR;
580 err = local_set_mapped_file_attr(fs_ctx, path, credp);
581 if (err == -1) {
582 serrno = errno;
583 goto err_end;
585 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
586 (fs_ctx->export_flags & V9FS_SM_NONE)) {
587 buffer = rpath(fs_ctx, path);
588 err = mkdir(buffer, credp->fc_mode);
589 if (err == -1) {
590 g_free(buffer);
591 goto out;
593 err = local_post_create_passthrough(fs_ctx, path, credp);
594 if (err == -1) {
595 serrno = errno;
596 goto err_end;
599 goto out;
601 err_end:
602 remove(buffer);
603 errno = serrno;
604 g_free(buffer);
605 out:
606 v9fs_string_free(&fullname);
607 return err;
610 static int local_fstat(FsContext *fs_ctx, int fid_type,
611 V9fsFidOpenState *fs, struct stat *stbuf)
613 int err, fd;
615 if (fid_type == P9_FID_DIR) {
616 fd = dirfd(fs->dir);
617 } else {
618 fd = fs->fd;
621 err = fstat(fd, stbuf);
622 if (err) {
623 return err;
625 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
626 /* Actual credentials are part of extended attrs */
627 uid_t tmp_uid;
628 gid_t tmp_gid;
629 mode_t tmp_mode;
630 dev_t tmp_dev;
632 if (fgetxattr(fd, "user.virtfs.uid",
633 &tmp_uid, sizeof(uid_t)) > 0) {
634 stbuf->st_uid = tmp_uid;
636 if (fgetxattr(fd, "user.virtfs.gid",
637 &tmp_gid, sizeof(gid_t)) > 0) {
638 stbuf->st_gid = tmp_gid;
640 if (fgetxattr(fd, "user.virtfs.mode",
641 &tmp_mode, sizeof(mode_t)) > 0) {
642 stbuf->st_mode = tmp_mode;
644 if (fgetxattr(fd, "user.virtfs.rdev",
645 &tmp_dev, sizeof(dev_t)) > 0) {
646 stbuf->st_rdev = tmp_dev;
648 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
649 errno = EOPNOTSUPP;
650 return -1;
652 return err;
655 static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
656 int flags, FsCred *credp, V9fsFidOpenState *fs)
658 char *path;
659 int fd = -1;
660 int err = -1;
661 int serrno = 0;
662 V9fsString fullname;
663 char *buffer;
666 * Mark all the open to not follow symlinks
668 flags |= O_NOFOLLOW;
670 v9fs_string_init(&fullname);
671 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
672 path = fullname.data;
674 /* Determine the security model */
675 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
676 buffer = rpath(fs_ctx, path);
677 fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
678 if (fd == -1) {
679 g_free(buffer);
680 err = fd;
681 goto out;
683 credp->fc_mode = credp->fc_mode|S_IFREG;
684 /* Set cleint credentials in xattr */
685 err = local_set_xattr(buffer, credp);
686 if (err == -1) {
687 serrno = errno;
688 goto err_end;
690 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
691 buffer = rpath(fs_ctx, path);
692 fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
693 if (fd == -1) {
694 g_free(buffer);
695 err = fd;
696 goto out;
698 credp->fc_mode = credp->fc_mode|S_IFREG;
699 /* Set client credentials in .virtfs_metadata directory files */
700 err = local_set_mapped_file_attr(fs_ctx, path, credp);
701 if (err == -1) {
702 serrno = errno;
703 goto err_end;
705 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
706 (fs_ctx->export_flags & V9FS_SM_NONE)) {
707 buffer = rpath(fs_ctx, path);
708 fd = open(buffer, flags, credp->fc_mode);
709 if (fd == -1) {
710 g_free(buffer);
711 err = fd;
712 goto out;
714 err = local_post_create_passthrough(fs_ctx, path, credp);
715 if (err == -1) {
716 serrno = errno;
717 goto err_end;
720 err = fd;
721 fs->fd = fd;
722 goto out;
724 err_end:
725 close(fd);
726 remove(buffer);
727 errno = serrno;
728 g_free(buffer);
729 out:
730 v9fs_string_free(&fullname);
731 return err;
735 static int local_symlink(FsContext *fs_ctx, const char *oldpath,
736 V9fsPath *dir_path, const char *name, FsCred *credp)
738 int err = -1;
739 int serrno = 0;
740 char *newpath;
741 V9fsString fullname;
742 char *buffer;
744 v9fs_string_init(&fullname);
745 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
746 newpath = fullname.data;
748 /* Determine the security model */
749 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
750 int fd;
751 ssize_t oldpath_size, write_size;
752 buffer = rpath(fs_ctx, newpath);
753 fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS);
754 if (fd == -1) {
755 g_free(buffer);
756 err = fd;
757 goto out;
759 /* Write the oldpath (target) to the file. */
760 oldpath_size = strlen(oldpath);
761 do {
762 write_size = write(fd, (void *)oldpath, oldpath_size);
763 } while (write_size == -1 && errno == EINTR);
765 if (write_size != oldpath_size) {
766 serrno = errno;
767 close(fd);
768 err = -1;
769 goto err_end;
771 close(fd);
772 /* Set cleint credentials in symlink's xattr */
773 credp->fc_mode = credp->fc_mode|S_IFLNK;
774 err = local_set_xattr(buffer, credp);
775 if (err == -1) {
776 serrno = errno;
777 goto err_end;
779 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
780 int fd;
781 ssize_t oldpath_size, write_size;
782 buffer = rpath(fs_ctx, newpath);
783 fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS);
784 if (fd == -1) {
785 g_free(buffer);
786 err = fd;
787 goto out;
789 /* Write the oldpath (target) to the file. */
790 oldpath_size = strlen(oldpath);
791 do {
792 write_size = write(fd, (void *)oldpath, oldpath_size);
793 } while (write_size == -1 && errno == EINTR);
795 if (write_size != oldpath_size) {
796 serrno = errno;
797 close(fd);
798 err = -1;
799 goto err_end;
801 close(fd);
802 /* Set cleint credentials in symlink's xattr */
803 credp->fc_mode = credp->fc_mode|S_IFLNK;
804 err = local_set_mapped_file_attr(fs_ctx, newpath, credp);
805 if (err == -1) {
806 serrno = errno;
807 goto err_end;
809 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
810 (fs_ctx->export_flags & V9FS_SM_NONE)) {
811 buffer = rpath(fs_ctx, newpath);
812 err = symlink(oldpath, buffer);
813 if (err) {
814 g_free(buffer);
815 goto out;
817 err = lchown(buffer, credp->fc_uid, credp->fc_gid);
818 if (err == -1) {
820 * If we fail to change ownership and if we are
821 * using security model none. Ignore the error
823 if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
824 serrno = errno;
825 goto err_end;
826 } else
827 err = 0;
830 goto out;
832 err_end:
833 remove(buffer);
834 errno = serrno;
835 g_free(buffer);
836 out:
837 v9fs_string_free(&fullname);
838 return err;
841 static int local_link(FsContext *ctx, V9fsPath *oldpath,
842 V9fsPath *dirpath, const char *name)
844 int ret;
845 V9fsString newpath;
846 char *buffer, *buffer1;
848 v9fs_string_init(&newpath);
849 v9fs_string_sprintf(&newpath, "%s/%s", dirpath->data, name);
851 buffer = rpath(ctx, oldpath->data);
852 buffer1 = rpath(ctx, newpath.data);
853 ret = link(buffer, buffer1);
854 g_free(buffer);
855 g_free(buffer1);
857 /* now link the virtfs_metadata files */
858 if (!ret && (ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
859 /* Link the .virtfs_metadata files. Create the metada directory */
860 ret = local_create_mapped_attr_dir(ctx, newpath.data);
861 if (ret < 0) {
862 goto err_out;
864 buffer = local_mapped_attr_path(ctx, oldpath->data);
865 buffer1 = local_mapped_attr_path(ctx, newpath.data);
866 ret = link(buffer, buffer1);
867 g_free(buffer);
868 g_free(buffer1);
869 if (ret < 0 && errno != ENOENT) {
870 goto err_out;
873 err_out:
874 v9fs_string_free(&newpath);
875 return ret;
878 static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
880 char *buffer;
881 int ret;
882 char *path = fs_path->data;
884 buffer = rpath(ctx, path);
885 ret = truncate(buffer, size);
886 g_free(buffer);
887 return ret;
890 static int local_rename(FsContext *ctx, const char *oldpath,
891 const char *newpath)
893 int err;
894 char *buffer, *buffer1;
896 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
897 err = local_create_mapped_attr_dir(ctx, newpath);
898 if (err < 0) {
899 return err;
901 /* rename the .virtfs_metadata files */
902 buffer = local_mapped_attr_path(ctx, oldpath);
903 buffer1 = local_mapped_attr_path(ctx, newpath);
904 err = rename(buffer, buffer1);
905 g_free(buffer);
906 g_free(buffer1);
907 if (err < 0 && errno != ENOENT) {
908 return err;
912 buffer = rpath(ctx, oldpath);
913 buffer1 = rpath(ctx, newpath);
914 err = rename(buffer, buffer1);
915 g_free(buffer);
916 g_free(buffer1);
917 return err;
920 static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
922 char *buffer;
923 int ret = -1;
924 char *path = fs_path->data;
926 if ((credp->fc_uid == -1 && credp->fc_gid == -1) ||
927 (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
928 (fs_ctx->export_flags & V9FS_SM_NONE)) {
929 buffer = rpath(fs_ctx, path);
930 ret = lchown(buffer, credp->fc_uid, credp->fc_gid);
931 g_free(buffer);
932 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
933 buffer = rpath(fs_ctx, path);
934 ret = local_set_xattr(buffer, credp);
935 g_free(buffer);
936 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
937 return local_set_mapped_file_attr(fs_ctx, path, credp);
939 return ret;
942 static int local_utimensat(FsContext *s, V9fsPath *fs_path,
943 const struct timespec *buf)
945 char *buffer;
946 int ret;
947 char *path = fs_path->data;
949 buffer = rpath(s, path);
950 ret = qemu_utimens(buffer, buf);
951 g_free(buffer);
952 return ret;
955 static int local_remove(FsContext *ctx, const char *path)
957 int err;
958 struct stat stbuf;
959 char *buffer;
961 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
962 buffer = rpath(ctx, path);
963 err = lstat(buffer, &stbuf);
964 g_free(buffer);
965 if (err) {
966 goto err_out;
969 * If directory remove .virtfs_metadata contained in the
970 * directory
972 if (S_ISDIR(stbuf.st_mode)) {
973 buffer = g_strdup_printf("%s/%s/%s", ctx->fs_root,
974 path, VIRTFS_META_DIR);
975 err = remove(buffer);
976 g_free(buffer);
977 if (err < 0 && errno != ENOENT) {
979 * We didn't had the .virtfs_metadata file. May be file created
980 * in non-mapped mode ?. Ignore ENOENT.
982 goto err_out;
986 * Now remove the name from parent directory
987 * .virtfs_metadata directory
989 buffer = local_mapped_attr_path(ctx, path);
990 err = remove(buffer);
991 g_free(buffer);
992 if (err < 0 && errno != ENOENT) {
994 * We didn't had the .virtfs_metadata file. May be file created
995 * in non-mapped mode ?. Ignore ENOENT.
997 goto err_out;
1001 buffer = rpath(ctx, path);
1002 err = remove(buffer);
1003 g_free(buffer);
1004 err_out:
1005 return err;
1008 static int local_fsync(FsContext *ctx, int fid_type,
1009 V9fsFidOpenState *fs, int datasync)
1011 int fd;
1013 if (fid_type == P9_FID_DIR) {
1014 fd = dirfd(fs->dir);
1015 } else {
1016 fd = fs->fd;
1019 if (datasync) {
1020 return qemu_fdatasync(fd);
1021 } else {
1022 return fsync(fd);
1026 static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
1028 char *buffer;
1029 int ret;
1030 char *path = fs_path->data;
1032 buffer = rpath(s, path);
1033 ret = statfs(buffer, stbuf);
1034 g_free(buffer);
1035 return ret;
1038 static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
1039 const char *name, void *value, size_t size)
1041 char *path = fs_path->data;
1043 return v9fs_get_xattr(ctx, path, name, value, size);
1046 static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path,
1047 void *value, size_t size)
1049 char *path = fs_path->data;
1051 return v9fs_list_xattr(ctx, path, value, size);
1054 static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
1055 void *value, size_t size, int flags)
1057 char *path = fs_path->data;
1059 return v9fs_set_xattr(ctx, path, name, value, size, flags);
1062 static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
1063 const char *name)
1065 char *path = fs_path->data;
1067 return v9fs_remove_xattr(ctx, path, name);
1070 static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path,
1071 const char *name, V9fsPath *target)
1073 if (dir_path) {
1074 v9fs_string_sprintf((V9fsString *)target, "%s/%s",
1075 dir_path->data, name);
1076 } else {
1077 v9fs_string_sprintf((V9fsString *)target, "%s", name);
1079 /* Bump the size for including terminating NULL */
1080 target->size++;
1081 return 0;
1084 static int local_renameat(FsContext *ctx, V9fsPath *olddir,
1085 const char *old_name, V9fsPath *newdir,
1086 const char *new_name)
1088 int ret;
1089 V9fsString old_full_name, new_full_name;
1091 v9fs_string_init(&old_full_name);
1092 v9fs_string_init(&new_full_name);
1094 v9fs_string_sprintf(&old_full_name, "%s/%s", olddir->data, old_name);
1095 v9fs_string_sprintf(&new_full_name, "%s/%s", newdir->data, new_name);
1097 ret = local_rename(ctx, old_full_name.data, new_full_name.data);
1098 v9fs_string_free(&old_full_name);
1099 v9fs_string_free(&new_full_name);
1100 return ret;
1103 static int local_unlinkat(FsContext *ctx, V9fsPath *dir,
1104 const char *name, int flags)
1106 int ret;
1107 V9fsString fullname;
1108 char *buffer;
1110 v9fs_string_init(&fullname);
1112 v9fs_string_sprintf(&fullname, "%s/%s", dir->data, name);
1113 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1114 if (flags == AT_REMOVEDIR) {
1116 * If directory remove .virtfs_metadata contained in the
1117 * directory
1119 buffer = g_strdup_printf("%s/%s/%s", ctx->fs_root,
1120 fullname.data, VIRTFS_META_DIR);
1121 ret = remove(buffer);
1122 g_free(buffer);
1123 if (ret < 0 && errno != ENOENT) {
1125 * We didn't had the .virtfs_metadata file. May be file created
1126 * in non-mapped mode ?. Ignore ENOENT.
1128 goto err_out;
1132 * Now remove the name from parent directory
1133 * .virtfs_metadata directory.
1135 buffer = local_mapped_attr_path(ctx, fullname.data);
1136 ret = remove(buffer);
1137 g_free(buffer);
1138 if (ret < 0 && errno != ENOENT) {
1140 * We didn't had the .virtfs_metadata file. May be file created
1141 * in non-mapped mode ?. Ignore ENOENT.
1143 goto err_out;
1146 /* Remove the name finally */
1147 buffer = rpath(ctx, fullname.data);
1148 ret = remove(buffer);
1149 g_free(buffer);
1151 err_out:
1152 v9fs_string_free(&fullname);
1153 return ret;
1156 static int local_ioc_getversion(FsContext *ctx, V9fsPath *path,
1157 mode_t st_mode, uint64_t *st_gen)
1159 #ifdef FS_IOC_GETVERSION
1160 int err;
1161 V9fsFidOpenState fid_open;
1164 * Do not try to open special files like device nodes, fifos etc
1165 * We can get fd for regular files and directories only
1167 if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
1168 errno = ENOTTY;
1169 return -1;
1171 err = local_open(ctx, path, O_RDONLY, &fid_open);
1172 if (err < 0) {
1173 return err;
1175 err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
1176 local_close(ctx, &fid_open);
1177 return err;
1178 #else
1179 errno = ENOTTY;
1180 return -1;
1181 #endif
1184 static int local_init(FsContext *ctx)
1186 int err = 0;
1187 struct statfs stbuf;
1189 if (ctx->export_flags & V9FS_SM_PASSTHROUGH) {
1190 ctx->xops = passthrough_xattr_ops;
1191 } else if (ctx->export_flags & V9FS_SM_MAPPED) {
1192 ctx->xops = mapped_xattr_ops;
1193 } else if (ctx->export_flags & V9FS_SM_NONE) {
1194 ctx->xops = none_xattr_ops;
1195 } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1197 * xattr operation for mapped-file and passthrough
1198 * remain same.
1200 ctx->xops = passthrough_xattr_ops;
1202 ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT;
1203 #ifdef FS_IOC_GETVERSION
1205 * use ioc_getversion only if the iocl is definied
1207 err = statfs(ctx->fs_root, &stbuf);
1208 if (!err) {
1209 switch (stbuf.f_type) {
1210 case EXT2_SUPER_MAGIC:
1211 case BTRFS_SUPER_MAGIC:
1212 case REISERFS_SUPER_MAGIC:
1213 case XFS_SUPER_MAGIC:
1214 ctx->exops.get_st_gen = local_ioc_getversion;
1215 break;
1218 #endif
1219 return err;
1222 static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse)
1224 const char *sec_model = qemu_opt_get(opts, "security_model");
1225 const char *path = qemu_opt_get(opts, "path");
1227 if (!sec_model) {
1228 fprintf(stderr, "security model not specified, "
1229 "local fs needs security model\nvalid options are:"
1230 "\tsecurity_model=[passthrough|mapped|none]\n");
1231 return -1;
1234 if (!strcmp(sec_model, "passthrough")) {
1235 fse->export_flags |= V9FS_SM_PASSTHROUGH;
1236 } else if (!strcmp(sec_model, "mapped") ||
1237 !strcmp(sec_model, "mapped-xattr")) {
1238 fse->export_flags |= V9FS_SM_MAPPED;
1239 } else if (!strcmp(sec_model, "none")) {
1240 fse->export_flags |= V9FS_SM_NONE;
1241 } else if (!strcmp(sec_model, "mapped-file")) {
1242 fse->export_flags |= V9FS_SM_MAPPED_FILE;
1243 } else {
1244 fprintf(stderr, "Invalid security model %s specified, valid options are"
1245 "\n\t [passthrough|mapped-xattr|mapped-file|none]\n",
1246 sec_model);
1247 return -1;
1250 if (!path) {
1251 fprintf(stderr, "fsdev: No path specified.\n");
1252 return -1;
1254 fse->path = g_strdup(path);
1256 return 0;
1259 FileOperations local_ops = {
1260 .parse_opts = local_parse_opts,
1261 .init = local_init,
1262 .lstat = local_lstat,
1263 .readlink = local_readlink,
1264 .close = local_close,
1265 .closedir = local_closedir,
1266 .open = local_open,
1267 .opendir = local_opendir,
1268 .rewinddir = local_rewinddir,
1269 .telldir = local_telldir,
1270 .readdir_r = local_readdir_r,
1271 .seekdir = local_seekdir,
1272 .preadv = local_preadv,
1273 .pwritev = local_pwritev,
1274 .chmod = local_chmod,
1275 .mknod = local_mknod,
1276 .mkdir = local_mkdir,
1277 .fstat = local_fstat,
1278 .open2 = local_open2,
1279 .symlink = local_symlink,
1280 .link = local_link,
1281 .truncate = local_truncate,
1282 .rename = local_rename,
1283 .chown = local_chown,
1284 .utimensat = local_utimensat,
1285 .remove = local_remove,
1286 .fsync = local_fsync,
1287 .statfs = local_statfs,
1288 .lgetxattr = local_lgetxattr,
1289 .llistxattr = local_llistxattr,
1290 .lsetxattr = local_lsetxattr,
1291 .lremovexattr = local_lremovexattr,
1292 .name_to_path = local_name_to_path,
1293 .renameat = local_renameat,
1294 .unlinkat = local_unlinkat,