hw/pcie: correct debug message
[qemu.git] / hw / 9pfs / virtio-9p-local.c
blob3b0b6a9b1d7d29260eab2ffcbbf5a308d530f4c7
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 "fsdev/qemu-fsdev.h" /* local_ops */
18 #include <arpa/inet.h>
19 #include <pwd.h>
20 #include <grp.h>
21 #include <sys/socket.h>
22 #include <sys/un.h>
23 #include "qemu/xattr.h"
24 #include <libgen.h>
25 #include <linux/fs.h>
26 #ifdef CONFIG_LINUX_MAGIC_H
27 #include <linux/magic.h>
28 #endif
29 #include <sys/ioctl.h>
31 #ifndef XFS_SUPER_MAGIC
32 #define XFS_SUPER_MAGIC 0x58465342
33 #endif
34 #ifndef EXT2_SUPER_MAGIC
35 #define EXT2_SUPER_MAGIC 0xEF53
36 #endif
37 #ifndef REISERFS_SUPER_MAGIC
38 #define REISERFS_SUPER_MAGIC 0x52654973
39 #endif
40 #ifndef BTRFS_SUPER_MAGIC
41 #define BTRFS_SUPER_MAGIC 0x9123683E
42 #endif
44 #define VIRTFS_META_DIR ".virtfs_metadata"
46 static char *local_mapped_attr_path(FsContext *ctx, const char *path)
48 char *dir_name;
49 char *tmp_path = g_strdup(path);
50 char *base_name = basename(tmp_path);
51 char *buffer;
53 /* NULL terminate the directory */
54 dir_name = tmp_path;
55 *(base_name - 1) = '\0';
57 buffer = g_strdup_printf("%s/%s/%s/%s",
58 ctx->fs_root, dir_name, VIRTFS_META_DIR, base_name);
59 g_free(tmp_path);
60 return buffer;
63 static FILE *local_fopen(const char *path, const char *mode)
65 int fd, o_mode = 0;
66 FILE *fp;
67 int flags = O_NOFOLLOW;
69 * only supports two modes
71 if (mode[0] == 'r') {
72 flags |= O_RDONLY;
73 } else if (mode[0] == 'w') {
74 flags |= O_WRONLY | O_TRUNC | O_CREAT;
75 o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
76 } else {
77 return NULL;
79 fd = open(path, flags, o_mode);
80 if (fd == -1) {
81 return NULL;
83 fp = fdopen(fd, mode);
84 if (!fp) {
85 close(fd);
87 return fp;
90 #define ATTR_MAX 100
91 static void local_mapped_file_attr(FsContext *ctx, const char *path,
92 struct stat *stbuf)
94 FILE *fp;
95 char buf[ATTR_MAX];
96 char *attr_path;
98 attr_path = local_mapped_attr_path(ctx, path);
99 fp = local_fopen(attr_path, "r");
100 g_free(attr_path);
101 if (!fp) {
102 return;
104 memset(buf, 0, ATTR_MAX);
105 while (fgets(buf, ATTR_MAX, fp)) {
106 if (!strncmp(buf, "virtfs.uid", 10)) {
107 stbuf->st_uid = atoi(buf+11);
108 } else if (!strncmp(buf, "virtfs.gid", 10)) {
109 stbuf->st_gid = atoi(buf+11);
110 } else if (!strncmp(buf, "virtfs.mode", 11)) {
111 stbuf->st_mode = atoi(buf+12);
112 } else if (!strncmp(buf, "virtfs.rdev", 11)) {
113 stbuf->st_rdev = atoi(buf+12);
115 memset(buf, 0, ATTR_MAX);
117 fclose(fp);
120 static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf)
122 int err;
123 char *buffer;
124 char *path = fs_path->data;
126 buffer = rpath(fs_ctx, path);
127 err = lstat(buffer, stbuf);
128 if (err) {
129 goto err_out;
131 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
132 /* Actual credentials are part of extended attrs */
133 uid_t tmp_uid;
134 gid_t tmp_gid;
135 mode_t tmp_mode;
136 dev_t tmp_dev;
137 if (getxattr(buffer, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) {
138 stbuf->st_uid = tmp_uid;
140 if (getxattr(buffer, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
141 stbuf->st_gid = tmp_gid;
143 if (getxattr(buffer, "user.virtfs.mode",
144 &tmp_mode, sizeof(mode_t)) > 0) {
145 stbuf->st_mode = tmp_mode;
147 if (getxattr(buffer, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
148 stbuf->st_rdev = tmp_dev;
150 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
151 local_mapped_file_attr(fs_ctx, path, stbuf);
154 err_out:
155 g_free(buffer);
156 return err;
159 static int local_create_mapped_attr_dir(FsContext *ctx, const char *path)
161 int err;
162 char *attr_dir;
163 char *tmp_path = g_strdup(path);
165 attr_dir = g_strdup_printf("%s/%s/%s",
166 ctx->fs_root, dirname(tmp_path), VIRTFS_META_DIR);
168 err = mkdir(attr_dir, 0700);
169 if (err < 0 && errno == EEXIST) {
170 err = 0;
172 g_free(attr_dir);
173 g_free(tmp_path);
174 return err;
177 static int local_set_mapped_file_attr(FsContext *ctx,
178 const char *path, FsCred *credp)
180 FILE *fp;
181 int ret = 0;
182 char buf[ATTR_MAX];
183 char *attr_path;
184 int uid = -1, gid = -1, mode = -1, rdev = -1;
186 attr_path = local_mapped_attr_path(ctx, path);
187 fp = local_fopen(attr_path, "r");
188 if (!fp) {
189 goto create_map_file;
191 memset(buf, 0, ATTR_MAX);
192 while (fgets(buf, ATTR_MAX, fp)) {
193 if (!strncmp(buf, "virtfs.uid", 10)) {
194 uid = atoi(buf+11);
195 } else if (!strncmp(buf, "virtfs.gid", 10)) {
196 gid = atoi(buf+11);
197 } else if (!strncmp(buf, "virtfs.mode", 11)) {
198 mode = atoi(buf+12);
199 } else if (!strncmp(buf, "virtfs.rdev", 11)) {
200 rdev = atoi(buf+12);
202 memset(buf, 0, ATTR_MAX);
204 fclose(fp);
205 goto update_map_file;
207 create_map_file:
208 ret = local_create_mapped_attr_dir(ctx, path);
209 if (ret < 0) {
210 goto err_out;
213 update_map_file:
214 fp = local_fopen(attr_path, "w");
215 if (!fp) {
216 ret = -1;
217 goto err_out;
220 if (credp->fc_uid != -1) {
221 uid = credp->fc_uid;
223 if (credp->fc_gid != -1) {
224 gid = credp->fc_gid;
226 if (credp->fc_mode != -1) {
227 mode = credp->fc_mode;
229 if (credp->fc_rdev != -1) {
230 rdev = credp->fc_rdev;
234 if (uid != -1) {
235 fprintf(fp, "virtfs.uid=%d\n", uid);
237 if (gid != -1) {
238 fprintf(fp, "virtfs.gid=%d\n", gid);
240 if (mode != -1) {
241 fprintf(fp, "virtfs.mode=%d\n", mode);
243 if (rdev != -1) {
244 fprintf(fp, "virtfs.rdev=%d\n", rdev);
246 fclose(fp);
248 err_out:
249 g_free(attr_path);
250 return ret;
253 static int local_set_xattr(const char *path, FsCred *credp)
255 int err;
257 if (credp->fc_uid != -1) {
258 err = setxattr(path, "user.virtfs.uid", &credp->fc_uid, sizeof(uid_t),
260 if (err) {
261 return err;
264 if (credp->fc_gid != -1) {
265 err = setxattr(path, "user.virtfs.gid", &credp->fc_gid, sizeof(gid_t),
267 if (err) {
268 return err;
271 if (credp->fc_mode != -1) {
272 err = setxattr(path, "user.virtfs.mode", &credp->fc_mode,
273 sizeof(mode_t), 0);
274 if (err) {
275 return err;
278 if (credp->fc_rdev != -1) {
279 err = setxattr(path, "user.virtfs.rdev", &credp->fc_rdev,
280 sizeof(dev_t), 0);
281 if (err) {
282 return err;
285 return 0;
288 static int local_post_create_passthrough(FsContext *fs_ctx, const char *path,
289 FsCred *credp)
291 char *buffer;
293 buffer = rpath(fs_ctx, path);
294 if (lchown(buffer, credp->fc_uid, credp->fc_gid) < 0) {
296 * If we fail to change ownership and if we are
297 * using security model none. Ignore the error
299 if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
300 goto err;
304 if (chmod(buffer, credp->fc_mode & 07777) < 0) {
305 goto err;
308 g_free(buffer);
309 return 0;
310 err:
311 g_free(buffer);
312 return -1;
315 static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
316 char *buf, size_t bufsz)
318 ssize_t tsize = -1;
319 char *buffer;
320 char *path = fs_path->data;
322 if ((fs_ctx->export_flags & V9FS_SM_MAPPED) ||
323 (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
324 int fd;
325 buffer = rpath(fs_ctx, path);
326 fd = open(buffer, O_RDONLY | O_NOFOLLOW);
327 g_free(buffer);
328 if (fd == -1) {
329 return -1;
331 do {
332 tsize = read(fd, (void *)buf, bufsz);
333 } while (tsize == -1 && errno == EINTR);
334 close(fd);
335 return tsize;
336 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
337 (fs_ctx->export_flags & V9FS_SM_NONE)) {
338 buffer = rpath(fs_ctx, path);
339 tsize = readlink(buffer, buf, bufsz);
340 g_free(buffer);
342 return tsize;
345 static int local_close(FsContext *ctx, V9fsFidOpenState *fs)
347 return close(fs->fd);
350 static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs)
352 return closedir(fs->dir);
355 static int local_open(FsContext *ctx, V9fsPath *fs_path,
356 int flags, V9fsFidOpenState *fs)
358 char *buffer;
359 char *path = fs_path->data;
361 buffer = rpath(ctx, path);
362 fs->fd = open(buffer, flags | O_NOFOLLOW);
363 g_free(buffer);
364 return fs->fd;
367 static int local_opendir(FsContext *ctx,
368 V9fsPath *fs_path, V9fsFidOpenState *fs)
370 char *buffer;
371 char *path = fs_path->data;
373 buffer = rpath(ctx, path);
374 fs->dir = opendir(buffer);
375 g_free(buffer);
376 if (!fs->dir) {
377 return -1;
379 return 0;
382 static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
384 return rewinddir(fs->dir);
387 static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs)
389 return telldir(fs->dir);
392 static int local_readdir_r(FsContext *ctx, V9fsFidOpenState *fs,
393 struct dirent *entry,
394 struct dirent **result)
396 int ret;
398 again:
399 ret = readdir_r(fs->dir, entry, result);
400 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
401 if (!ret && *result != NULL &&
402 !strcmp(entry->d_name, VIRTFS_META_DIR)) {
403 /* skp the meta data directory */
404 goto again;
407 return ret;
410 static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
412 return seekdir(fs->dir, off);
415 static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs,
416 const struct iovec *iov,
417 int iovcnt, off_t offset)
419 #ifdef CONFIG_PREADV
420 return preadv(fs->fd, iov, iovcnt, offset);
421 #else
422 int err = lseek(fs->fd, offset, SEEK_SET);
423 if (err == -1) {
424 return err;
425 } else {
426 return readv(fs->fd, iov, iovcnt);
428 #endif
431 static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
432 const struct iovec *iov,
433 int iovcnt, off_t offset)
435 ssize_t ret
437 #ifdef CONFIG_PREADV
438 ret = pwritev(fs->fd, iov, iovcnt, offset);
439 #else
440 int err = lseek(fs->fd, offset, SEEK_SET);
441 if (err == -1) {
442 return err;
443 } else {
444 ret = writev(fs->fd, iov, iovcnt);
446 #endif
447 #ifdef CONFIG_SYNC_FILE_RANGE
448 if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
450 * Initiate a writeback. This is not a data integrity sync.
451 * We want to ensure that we don't leave dirty pages in the cache
452 * after write when writeout=immediate is sepcified.
454 sync_file_range(fs->fd, offset, ret,
455 SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
457 #endif
458 return ret;
461 static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
463 char *buffer;
464 int ret = -1;
465 char *path = fs_path->data;
467 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
468 buffer = rpath(fs_ctx, path);
469 ret = local_set_xattr(buffer, credp);
470 g_free(buffer);
471 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
472 return local_set_mapped_file_attr(fs_ctx, path, credp);
473 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
474 (fs_ctx->export_flags & V9FS_SM_NONE)) {
475 buffer = rpath(fs_ctx, path);
476 ret = chmod(buffer, credp->fc_mode);
477 g_free(buffer);
479 return ret;
482 static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
483 const char *name, FsCred *credp)
485 char *path;
486 int err = -1;
487 int serrno = 0;
488 V9fsString fullname;
489 char *buffer;
491 v9fs_string_init(&fullname);
492 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
493 path = fullname.data;
495 /* Determine the security model */
496 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
497 buffer = rpath(fs_ctx, path);
498 err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0);
499 if (err == -1) {
500 g_free(buffer);
501 goto out;
503 err = local_set_xattr(buffer, credp);
504 if (err == -1) {
505 serrno = errno;
506 goto err_end;
508 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
510 buffer = rpath(fs_ctx, path);
511 err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0);
512 if (err == -1) {
513 g_free(buffer);
514 goto out;
516 err = local_set_mapped_file_attr(fs_ctx, path, credp);
517 if (err == -1) {
518 serrno = errno;
519 goto err_end;
521 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
522 (fs_ctx->export_flags & V9FS_SM_NONE)) {
523 buffer = rpath(fs_ctx, path);
524 err = mknod(buffer, credp->fc_mode, credp->fc_rdev);
525 if (err == -1) {
526 g_free(buffer);
527 goto out;
529 err = local_post_create_passthrough(fs_ctx, path, credp);
530 if (err == -1) {
531 serrno = errno;
532 goto err_end;
535 goto out;
537 err_end:
538 remove(buffer);
539 errno = serrno;
540 g_free(buffer);
541 out:
542 v9fs_string_free(&fullname);
543 return err;
546 static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
547 const char *name, FsCred *credp)
549 char *path;
550 int err = -1;
551 int serrno = 0;
552 V9fsString fullname;
553 char *buffer;
555 v9fs_string_init(&fullname);
556 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
557 path = fullname.data;
559 /* Determine the security model */
560 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
561 buffer = rpath(fs_ctx, path);
562 err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
563 if (err == -1) {
564 g_free(buffer);
565 goto out;
567 credp->fc_mode = credp->fc_mode|S_IFDIR;
568 err = local_set_xattr(buffer, credp);
569 if (err == -1) {
570 serrno = errno;
571 goto err_end;
573 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
574 buffer = rpath(fs_ctx, path);
575 err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
576 if (err == -1) {
577 g_free(buffer);
578 goto out;
580 credp->fc_mode = credp->fc_mode|S_IFDIR;
581 err = local_set_mapped_file_attr(fs_ctx, path, credp);
582 if (err == -1) {
583 serrno = errno;
584 goto err_end;
586 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
587 (fs_ctx->export_flags & V9FS_SM_NONE)) {
588 buffer = rpath(fs_ctx, path);
589 err = mkdir(buffer, credp->fc_mode);
590 if (err == -1) {
591 g_free(buffer);
592 goto out;
594 err = local_post_create_passthrough(fs_ctx, path, credp);
595 if (err == -1) {
596 serrno = errno;
597 goto err_end;
600 goto out;
602 err_end:
603 remove(buffer);
604 errno = serrno;
605 g_free(buffer);
606 out:
607 v9fs_string_free(&fullname);
608 return err;
611 static int local_fstat(FsContext *fs_ctx, int fid_type,
612 V9fsFidOpenState *fs, struct stat *stbuf)
614 int err, fd;
616 if (fid_type == P9_FID_DIR) {
617 fd = dirfd(fs->dir);
618 } else {
619 fd = fs->fd;
622 err = fstat(fd, stbuf);
623 if (err) {
624 return err;
626 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
627 /* Actual credentials are part of extended attrs */
628 uid_t tmp_uid;
629 gid_t tmp_gid;
630 mode_t tmp_mode;
631 dev_t tmp_dev;
633 if (fgetxattr(fd, "user.virtfs.uid",
634 &tmp_uid, sizeof(uid_t)) > 0) {
635 stbuf->st_uid = tmp_uid;
637 if (fgetxattr(fd, "user.virtfs.gid",
638 &tmp_gid, sizeof(gid_t)) > 0) {
639 stbuf->st_gid = tmp_gid;
641 if (fgetxattr(fd, "user.virtfs.mode",
642 &tmp_mode, sizeof(mode_t)) > 0) {
643 stbuf->st_mode = tmp_mode;
645 if (fgetxattr(fd, "user.virtfs.rdev",
646 &tmp_dev, sizeof(dev_t)) > 0) {
647 stbuf->st_rdev = tmp_dev;
649 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
650 errno = EOPNOTSUPP;
651 return -1;
653 return err;
656 static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
657 int flags, FsCred *credp, V9fsFidOpenState *fs)
659 char *path;
660 int fd = -1;
661 int err = -1;
662 int serrno = 0;
663 V9fsString fullname;
664 char *buffer;
667 * Mark all the open to not follow symlinks
669 flags |= O_NOFOLLOW;
671 v9fs_string_init(&fullname);
672 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
673 path = fullname.data;
675 /* Determine the security model */
676 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
677 buffer = rpath(fs_ctx, path);
678 fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
679 if (fd == -1) {
680 g_free(buffer);
681 err = fd;
682 goto out;
684 credp->fc_mode = credp->fc_mode|S_IFREG;
685 /* Set cleint credentials in xattr */
686 err = local_set_xattr(buffer, credp);
687 if (err == -1) {
688 serrno = errno;
689 goto err_end;
691 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
692 buffer = rpath(fs_ctx, path);
693 fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
694 if (fd == -1) {
695 g_free(buffer);
696 err = fd;
697 goto out;
699 credp->fc_mode = credp->fc_mode|S_IFREG;
700 /* Set client credentials in .virtfs_metadata directory files */
701 err = local_set_mapped_file_attr(fs_ctx, path, credp);
702 if (err == -1) {
703 serrno = errno;
704 goto err_end;
706 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
707 (fs_ctx->export_flags & V9FS_SM_NONE)) {
708 buffer = rpath(fs_ctx, path);
709 fd = open(buffer, flags, credp->fc_mode);
710 if (fd == -1) {
711 g_free(buffer);
712 err = fd;
713 goto out;
715 err = local_post_create_passthrough(fs_ctx, path, credp);
716 if (err == -1) {
717 serrno = errno;
718 goto err_end;
721 err = fd;
722 fs->fd = fd;
723 goto out;
725 err_end:
726 close(fd);
727 remove(buffer);
728 errno = serrno;
729 g_free(buffer);
730 out:
731 v9fs_string_free(&fullname);
732 return err;
736 static int local_symlink(FsContext *fs_ctx, const char *oldpath,
737 V9fsPath *dir_path, const char *name, FsCred *credp)
739 int err = -1;
740 int serrno = 0;
741 char *newpath;
742 V9fsString fullname;
743 char *buffer;
745 v9fs_string_init(&fullname);
746 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
747 newpath = fullname.data;
749 /* Determine the security model */
750 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
751 int fd;
752 ssize_t oldpath_size, write_size;
753 buffer = rpath(fs_ctx, newpath);
754 fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS);
755 if (fd == -1) {
756 g_free(buffer);
757 err = fd;
758 goto out;
760 /* Write the oldpath (target) to the file. */
761 oldpath_size = strlen(oldpath);
762 do {
763 write_size = write(fd, (void *)oldpath, oldpath_size);
764 } while (write_size == -1 && errno == EINTR);
766 if (write_size != oldpath_size) {
767 serrno = errno;
768 close(fd);
769 err = -1;
770 goto err_end;
772 close(fd);
773 /* Set cleint credentials in symlink's xattr */
774 credp->fc_mode = credp->fc_mode|S_IFLNK;
775 err = local_set_xattr(buffer, credp);
776 if (err == -1) {
777 serrno = errno;
778 goto err_end;
780 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
781 int fd;
782 ssize_t oldpath_size, write_size;
783 buffer = rpath(fs_ctx, newpath);
784 fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS);
785 if (fd == -1) {
786 g_free(buffer);
787 err = fd;
788 goto out;
790 /* Write the oldpath (target) to the file. */
791 oldpath_size = strlen(oldpath);
792 do {
793 write_size = write(fd, (void *)oldpath, oldpath_size);
794 } while (write_size == -1 && errno == EINTR);
796 if (write_size != oldpath_size) {
797 serrno = errno;
798 close(fd);
799 err = -1;
800 goto err_end;
802 close(fd);
803 /* Set cleint credentials in symlink's xattr */
804 credp->fc_mode = credp->fc_mode|S_IFLNK;
805 err = local_set_mapped_file_attr(fs_ctx, newpath, credp);
806 if (err == -1) {
807 serrno = errno;
808 goto err_end;
810 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
811 (fs_ctx->export_flags & V9FS_SM_NONE)) {
812 buffer = rpath(fs_ctx, newpath);
813 err = symlink(oldpath, buffer);
814 if (err) {
815 g_free(buffer);
816 goto out;
818 err = lchown(buffer, credp->fc_uid, credp->fc_gid);
819 if (err == -1) {
821 * If we fail to change ownership and if we are
822 * using security model none. Ignore the error
824 if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
825 serrno = errno;
826 goto err_end;
827 } else
828 err = 0;
831 goto out;
833 err_end:
834 remove(buffer);
835 errno = serrno;
836 g_free(buffer);
837 out:
838 v9fs_string_free(&fullname);
839 return err;
842 static int local_link(FsContext *ctx, V9fsPath *oldpath,
843 V9fsPath *dirpath, const char *name)
845 int ret;
846 V9fsString newpath;
847 char *buffer, *buffer1;
849 v9fs_string_init(&newpath);
850 v9fs_string_sprintf(&newpath, "%s/%s", dirpath->data, name);
852 buffer = rpath(ctx, oldpath->data);
853 buffer1 = rpath(ctx, newpath.data);
854 ret = link(buffer, buffer1);
855 g_free(buffer);
856 g_free(buffer1);
858 /* now link the virtfs_metadata files */
859 if (!ret && (ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
860 /* Link the .virtfs_metadata files. Create the metada directory */
861 ret = local_create_mapped_attr_dir(ctx, newpath.data);
862 if (ret < 0) {
863 goto err_out;
865 buffer = local_mapped_attr_path(ctx, oldpath->data);
866 buffer1 = local_mapped_attr_path(ctx, newpath.data);
867 ret = link(buffer, buffer1);
868 g_free(buffer);
869 g_free(buffer1);
870 if (ret < 0 && errno != ENOENT) {
871 goto err_out;
874 err_out:
875 v9fs_string_free(&newpath);
876 return ret;
879 static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
881 char *buffer;
882 int ret;
883 char *path = fs_path->data;
885 buffer = rpath(ctx, path);
886 ret = truncate(buffer, size);
887 g_free(buffer);
888 return ret;
891 static int local_rename(FsContext *ctx, const char *oldpath,
892 const char *newpath)
894 int err;
895 char *buffer, *buffer1;
897 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
898 err = local_create_mapped_attr_dir(ctx, newpath);
899 if (err < 0) {
900 return err;
902 /* rename the .virtfs_metadata files */
903 buffer = local_mapped_attr_path(ctx, oldpath);
904 buffer1 = local_mapped_attr_path(ctx, newpath);
905 err = rename(buffer, buffer1);
906 g_free(buffer);
907 g_free(buffer1);
908 if (err < 0 && errno != ENOENT) {
909 return err;
913 buffer = rpath(ctx, oldpath);
914 buffer1 = rpath(ctx, newpath);
915 err = rename(buffer, buffer1);
916 g_free(buffer);
917 g_free(buffer1);
918 return err;
921 static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
923 char *buffer;
924 int ret = -1;
925 char *path = fs_path->data;
927 if ((credp->fc_uid == -1 && credp->fc_gid == -1) ||
928 (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
929 (fs_ctx->export_flags & V9FS_SM_NONE)) {
930 buffer = rpath(fs_ctx, path);
931 ret = lchown(buffer, credp->fc_uid, credp->fc_gid);
932 g_free(buffer);
933 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
934 buffer = rpath(fs_ctx, path);
935 ret = local_set_xattr(buffer, credp);
936 g_free(buffer);
937 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
938 return local_set_mapped_file_attr(fs_ctx, path, credp);
940 return ret;
943 static int local_utimensat(FsContext *s, V9fsPath *fs_path,
944 const struct timespec *buf)
946 char *buffer;
947 int ret;
948 char *path = fs_path->data;
950 buffer = rpath(s, path);
951 ret = qemu_utimens(buffer, buf);
952 g_free(buffer);
953 return ret;
956 static int local_remove(FsContext *ctx, const char *path)
958 int err;
959 struct stat stbuf;
960 char *buffer;
962 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
963 buffer = rpath(ctx, path);
964 err = lstat(buffer, &stbuf);
965 g_free(buffer);
966 if (err) {
967 goto err_out;
970 * If directory remove .virtfs_metadata contained in the
971 * directory
973 if (S_ISDIR(stbuf.st_mode)) {
974 buffer = g_strdup_printf("%s/%s/%s", ctx->fs_root,
975 path, VIRTFS_META_DIR);
976 err = remove(buffer);
977 g_free(buffer);
978 if (err < 0 && errno != ENOENT) {
980 * We didn't had the .virtfs_metadata file. May be file created
981 * in non-mapped mode ?. Ignore ENOENT.
983 goto err_out;
987 * Now remove the name from parent directory
988 * .virtfs_metadata directory
990 buffer = local_mapped_attr_path(ctx, path);
991 err = remove(buffer);
992 g_free(buffer);
993 if (err < 0 && errno != ENOENT) {
995 * We didn't had the .virtfs_metadata file. May be file created
996 * in non-mapped mode ?. Ignore ENOENT.
998 goto err_out;
1002 buffer = rpath(ctx, path);
1003 err = remove(buffer);
1004 g_free(buffer);
1005 err_out:
1006 return err;
1009 static int local_fsync(FsContext *ctx, int fid_type,
1010 V9fsFidOpenState *fs, int datasync)
1012 int fd;
1014 if (fid_type == P9_FID_DIR) {
1015 fd = dirfd(fs->dir);
1016 } else {
1017 fd = fs->fd;
1020 if (datasync) {
1021 return qemu_fdatasync(fd);
1022 } else {
1023 return fsync(fd);
1027 static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
1029 char *buffer;
1030 int ret;
1031 char *path = fs_path->data;
1033 buffer = rpath(s, path);
1034 ret = statfs(buffer, stbuf);
1035 g_free(buffer);
1036 return ret;
1039 static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
1040 const char *name, void *value, size_t size)
1042 char *path = fs_path->data;
1044 return v9fs_get_xattr(ctx, path, name, value, size);
1047 static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path,
1048 void *value, size_t size)
1050 char *path = fs_path->data;
1052 return v9fs_list_xattr(ctx, path, value, size);
1055 static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
1056 void *value, size_t size, int flags)
1058 char *path = fs_path->data;
1060 return v9fs_set_xattr(ctx, path, name, value, size, flags);
1063 static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
1064 const char *name)
1066 char *path = fs_path->data;
1068 return v9fs_remove_xattr(ctx, path, name);
1071 static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path,
1072 const char *name, V9fsPath *target)
1074 if (dir_path) {
1075 v9fs_string_sprintf((V9fsString *)target, "%s/%s",
1076 dir_path->data, name);
1077 } else {
1078 v9fs_string_sprintf((V9fsString *)target, "%s", name);
1080 /* Bump the size for including terminating NULL */
1081 target->size++;
1082 return 0;
1085 static int local_renameat(FsContext *ctx, V9fsPath *olddir,
1086 const char *old_name, V9fsPath *newdir,
1087 const char *new_name)
1089 int ret;
1090 V9fsString old_full_name, new_full_name;
1092 v9fs_string_init(&old_full_name);
1093 v9fs_string_init(&new_full_name);
1095 v9fs_string_sprintf(&old_full_name, "%s/%s", olddir->data, old_name);
1096 v9fs_string_sprintf(&new_full_name, "%s/%s", newdir->data, new_name);
1098 ret = local_rename(ctx, old_full_name.data, new_full_name.data);
1099 v9fs_string_free(&old_full_name);
1100 v9fs_string_free(&new_full_name);
1101 return ret;
1104 static int local_unlinkat(FsContext *ctx, V9fsPath *dir,
1105 const char *name, int flags)
1107 int ret;
1108 V9fsString fullname;
1109 char *buffer;
1111 v9fs_string_init(&fullname);
1113 v9fs_string_sprintf(&fullname, "%s/%s", dir->data, name);
1114 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1115 if (flags == AT_REMOVEDIR) {
1117 * If directory remove .virtfs_metadata contained in the
1118 * directory
1120 buffer = g_strdup_printf("%s/%s/%s", ctx->fs_root,
1121 fullname.data, VIRTFS_META_DIR);
1122 ret = remove(buffer);
1123 g_free(buffer);
1124 if (ret < 0 && errno != ENOENT) {
1126 * We didn't had the .virtfs_metadata file. May be file created
1127 * in non-mapped mode ?. Ignore ENOENT.
1129 goto err_out;
1133 * Now remove the name from parent directory
1134 * .virtfs_metadata directory.
1136 buffer = local_mapped_attr_path(ctx, fullname.data);
1137 ret = remove(buffer);
1138 g_free(buffer);
1139 if (ret < 0 && errno != ENOENT) {
1141 * We didn't had the .virtfs_metadata file. May be file created
1142 * in non-mapped mode ?. Ignore ENOENT.
1144 goto err_out;
1147 /* Remove the name finally */
1148 buffer = rpath(ctx, fullname.data);
1149 ret = remove(buffer);
1150 g_free(buffer);
1152 err_out:
1153 v9fs_string_free(&fullname);
1154 return ret;
1157 static int local_ioc_getversion(FsContext *ctx, V9fsPath *path,
1158 mode_t st_mode, uint64_t *st_gen)
1160 #ifdef FS_IOC_GETVERSION
1161 int err;
1162 V9fsFidOpenState fid_open;
1165 * Do not try to open special files like device nodes, fifos etc
1166 * We can get fd for regular files and directories only
1168 if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
1169 errno = ENOTTY;
1170 return -1;
1172 err = local_open(ctx, path, O_RDONLY, &fid_open);
1173 if (err < 0) {
1174 return err;
1176 err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
1177 local_close(ctx, &fid_open);
1178 return err;
1179 #else
1180 errno = ENOTTY;
1181 return -1;
1182 #endif
1185 static int local_init(FsContext *ctx)
1187 int err = 0;
1188 struct statfs stbuf;
1190 if (ctx->export_flags & V9FS_SM_PASSTHROUGH) {
1191 ctx->xops = passthrough_xattr_ops;
1192 } else if (ctx->export_flags & V9FS_SM_MAPPED) {
1193 ctx->xops = mapped_xattr_ops;
1194 } else if (ctx->export_flags & V9FS_SM_NONE) {
1195 ctx->xops = none_xattr_ops;
1196 } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1198 * xattr operation for mapped-file and passthrough
1199 * remain same.
1201 ctx->xops = passthrough_xattr_ops;
1203 ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT;
1204 #ifdef FS_IOC_GETVERSION
1206 * use ioc_getversion only if the iocl is definied
1208 err = statfs(ctx->fs_root, &stbuf);
1209 if (!err) {
1210 switch (stbuf.f_type) {
1211 case EXT2_SUPER_MAGIC:
1212 case BTRFS_SUPER_MAGIC:
1213 case REISERFS_SUPER_MAGIC:
1214 case XFS_SUPER_MAGIC:
1215 ctx->exops.get_st_gen = local_ioc_getversion;
1216 break;
1219 #endif
1220 return err;
1223 static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse)
1225 const char *sec_model = qemu_opt_get(opts, "security_model");
1226 const char *path = qemu_opt_get(opts, "path");
1228 if (!sec_model) {
1229 fprintf(stderr, "security model not specified, "
1230 "local fs needs security model\nvalid options are:"
1231 "\tsecurity_model=[passthrough|mapped|none]\n");
1232 return -1;
1235 if (!strcmp(sec_model, "passthrough")) {
1236 fse->export_flags |= V9FS_SM_PASSTHROUGH;
1237 } else if (!strcmp(sec_model, "mapped") ||
1238 !strcmp(sec_model, "mapped-xattr")) {
1239 fse->export_flags |= V9FS_SM_MAPPED;
1240 } else if (!strcmp(sec_model, "none")) {
1241 fse->export_flags |= V9FS_SM_NONE;
1242 } else if (!strcmp(sec_model, "mapped-file")) {
1243 fse->export_flags |= V9FS_SM_MAPPED_FILE;
1244 } else {
1245 fprintf(stderr, "Invalid security model %s specified, valid options are"
1246 "\n\t [passthrough|mapped-xattr|mapped-file|none]\n",
1247 sec_model);
1248 return -1;
1251 if (!path) {
1252 fprintf(stderr, "fsdev: No path specified.\n");
1253 return -1;
1255 fse->path = g_strdup(path);
1257 return 0;
1260 FileOperations local_ops = {
1261 .parse_opts = local_parse_opts,
1262 .init = local_init,
1263 .lstat = local_lstat,
1264 .readlink = local_readlink,
1265 .close = local_close,
1266 .closedir = local_closedir,
1267 .open = local_open,
1268 .opendir = local_opendir,
1269 .rewinddir = local_rewinddir,
1270 .telldir = local_telldir,
1271 .readdir_r = local_readdir_r,
1272 .seekdir = local_seekdir,
1273 .preadv = local_preadv,
1274 .pwritev = local_pwritev,
1275 .chmod = local_chmod,
1276 .mknod = local_mknod,
1277 .mkdir = local_mkdir,
1278 .fstat = local_fstat,
1279 .open2 = local_open2,
1280 .symlink = local_symlink,
1281 .link = local_link,
1282 .truncate = local_truncate,
1283 .rename = local_rename,
1284 .chown = local_chown,
1285 .utimensat = local_utimensat,
1286 .remove = local_remove,
1287 .fsync = local_fsync,
1288 .statfs = local_statfs,
1289 .lgetxattr = local_lgetxattr,
1290 .llistxattr = local_llistxattr,
1291 .lsetxattr = local_lsetxattr,
1292 .lremovexattr = local_lremovexattr,
1293 .name_to_path = local_name_to_path,
1294 .renameat = local_renameat,
1295 .unlinkat = local_unlinkat,