scsi: pvscsi: limit process IO loop to ring size
[qemu/ar7.git] / hw / 9pfs / 9p-local.c
blob3f271fcbd2c51599761e96cf1eac5835227ace08
1 /*
2 * 9p Posix callback
4 * Copyright IBM, Corp. 2010
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #include "qemu/osdep.h"
15 #include "9p.h"
16 #include "9p-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 "qemu/cutils.h"
25 #include "qemu/error-report.h"
26 #include <libgen.h>
27 #include <linux/fs.h>
28 #ifdef CONFIG_LINUX_MAGIC_H
29 #include <linux/magic.h>
30 #endif
31 #include <sys/ioctl.h>
33 #ifndef XFS_SUPER_MAGIC
34 #define XFS_SUPER_MAGIC 0x58465342
35 #endif
36 #ifndef EXT2_SUPER_MAGIC
37 #define EXT2_SUPER_MAGIC 0xEF53
38 #endif
39 #ifndef REISERFS_SUPER_MAGIC
40 #define REISERFS_SUPER_MAGIC 0x52654973
41 #endif
42 #ifndef BTRFS_SUPER_MAGIC
43 #define BTRFS_SUPER_MAGIC 0x9123683E
44 #endif
46 #define VIRTFS_META_DIR ".virtfs_metadata"
48 static char *local_mapped_attr_path(FsContext *ctx, const char *path)
50 int dirlen;
51 const char *name = strrchr(path, '/');
52 if (name) {
53 dirlen = name - path;
54 ++name;
55 } else {
56 name = path;
57 dirlen = 0;
59 return g_strdup_printf("%s/%.*s/%s/%s", ctx->fs_root,
60 dirlen, path, VIRTFS_META_DIR, name);
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 = le32_to_cpu(tmp_uid);
140 if (getxattr(buffer, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
141 stbuf->st_gid = le32_to_cpu(tmp_gid);
143 if (getxattr(buffer, "user.virtfs.mode",
144 &tmp_mode, sizeof(mode_t)) > 0) {
145 stbuf->st_mode = le32_to_cpu(tmp_mode);
147 if (getxattr(buffer, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
148 stbuf->st_rdev = le64_to_cpu(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 uint32_t tmp_uid = cpu_to_le32(credp->fc_uid);
259 err = setxattr(path, "user.virtfs.uid", &tmp_uid, sizeof(uid_t), 0);
260 if (err) {
261 return err;
264 if (credp->fc_gid != -1) {
265 uint32_t tmp_gid = cpu_to_le32(credp->fc_gid);
266 err = setxattr(path, "user.virtfs.gid", &tmp_gid, sizeof(gid_t), 0);
267 if (err) {
268 return err;
271 if (credp->fc_mode != -1) {
272 uint32_t tmp_mode = cpu_to_le32(credp->fc_mode);
273 err = setxattr(path, "user.virtfs.mode", &tmp_mode, sizeof(mode_t), 0);
274 if (err) {
275 return err;
278 if (credp->fc_rdev != -1) {
279 uint64_t tmp_rdev = cpu_to_le64(credp->fc_rdev);
280 err = setxattr(path, "user.virtfs.rdev", &tmp_rdev, 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 } 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.stream);
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.stream = opendir(buffer);
374 g_free(buffer);
375 if (!fs->dir.stream) {
376 return -1;
378 return 0;
381 static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
383 rewinddir(fs->dir.stream);
386 static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs)
388 return telldir(fs->dir.stream);
391 static struct dirent *local_readdir(FsContext *ctx, V9fsFidOpenState *fs)
393 struct dirent *entry;
395 again:
396 entry = readdir(fs->dir.stream);
397 if (!entry) {
398 return NULL;
401 if (ctx->export_flags & V9FS_SM_MAPPED) {
402 entry->d_type = DT_UNKNOWN;
403 } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
404 if (!strcmp(entry->d_name, VIRTFS_META_DIR)) {
405 /* skp the meta data directory */
406 goto again;
408 entry->d_type = DT_UNKNOWN;
411 return entry;
414 static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
416 seekdir(fs->dir.stream, off);
419 static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs,
420 const struct iovec *iov,
421 int iovcnt, off_t offset)
423 #ifdef CONFIG_PREADV
424 return preadv(fs->fd, iov, iovcnt, offset);
425 #else
426 int err = lseek(fs->fd, offset, SEEK_SET);
427 if (err == -1) {
428 return err;
429 } else {
430 return readv(fs->fd, iov, iovcnt);
432 #endif
435 static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
436 const struct iovec *iov,
437 int iovcnt, off_t offset)
439 ssize_t ret
441 #ifdef CONFIG_PREADV
442 ret = pwritev(fs->fd, iov, iovcnt, offset);
443 #else
444 int err = lseek(fs->fd, offset, SEEK_SET);
445 if (err == -1) {
446 return err;
447 } else {
448 ret = writev(fs->fd, iov, iovcnt);
450 #endif
451 #ifdef CONFIG_SYNC_FILE_RANGE
452 if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
454 * Initiate a writeback. This is not a data integrity sync.
455 * We want to ensure that we don't leave dirty pages in the cache
456 * after write when writeout=immediate is sepcified.
458 sync_file_range(fs->fd, offset, ret,
459 SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
461 #endif
462 return ret;
465 static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
467 char *buffer;
468 int ret = -1;
469 char *path = fs_path->data;
471 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
472 buffer = rpath(fs_ctx, path);
473 ret = local_set_xattr(buffer, credp);
474 g_free(buffer);
475 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
476 return local_set_mapped_file_attr(fs_ctx, path, credp);
477 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
478 (fs_ctx->export_flags & V9FS_SM_NONE)) {
479 buffer = rpath(fs_ctx, path);
480 ret = chmod(buffer, credp->fc_mode);
481 g_free(buffer);
483 return ret;
486 static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
487 const char *name, FsCred *credp)
489 char *path;
490 int err = -1;
491 int serrno = 0;
492 V9fsString fullname;
493 char *buffer = NULL;
495 v9fs_string_init(&fullname);
496 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
497 path = fullname.data;
499 /* Determine the security model */
500 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
501 buffer = rpath(fs_ctx, path);
502 err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0);
503 if (err == -1) {
504 goto out;
506 err = local_set_xattr(buffer, credp);
507 if (err == -1) {
508 serrno = errno;
509 goto err_end;
511 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
513 buffer = rpath(fs_ctx, path);
514 err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0);
515 if (err == -1) {
516 goto out;
518 err = local_set_mapped_file_attr(fs_ctx, path, credp);
519 if (err == -1) {
520 serrno = errno;
521 goto err_end;
523 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
524 (fs_ctx->export_flags & V9FS_SM_NONE)) {
525 buffer = rpath(fs_ctx, path);
526 err = mknod(buffer, credp->fc_mode, credp->fc_rdev);
527 if (err == -1) {
528 goto out;
530 err = local_post_create_passthrough(fs_ctx, path, credp);
531 if (err == -1) {
532 serrno = errno;
533 goto err_end;
536 goto out;
538 err_end:
539 remove(buffer);
540 errno = serrno;
541 out:
542 g_free(buffer);
543 v9fs_string_free(&fullname);
544 return err;
547 static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
548 const char *name, FsCred *credp)
550 char *path;
551 int err = -1;
552 int serrno = 0;
553 V9fsString fullname;
554 char *buffer = NULL;
556 v9fs_string_init(&fullname);
557 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
558 path = fullname.data;
560 /* Determine the security model */
561 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
562 buffer = rpath(fs_ctx, path);
563 err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
564 if (err == -1) {
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 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 goto out;
592 err = local_post_create_passthrough(fs_ctx, path, credp);
593 if (err == -1) {
594 serrno = errno;
595 goto err_end;
598 goto out;
600 err_end:
601 remove(buffer);
602 errno = serrno;
603 out:
604 g_free(buffer);
605 v9fs_string_free(&fullname);
606 return err;
609 static int local_fstat(FsContext *fs_ctx, int fid_type,
610 V9fsFidOpenState *fs, struct stat *stbuf)
612 int err, fd;
614 if (fid_type == P9_FID_DIR) {
615 fd = dirfd(fs->dir.stream);
616 } else {
617 fd = fs->fd;
620 err = fstat(fd, stbuf);
621 if (err) {
622 return err;
624 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
625 /* Actual credentials are part of extended attrs */
626 uid_t tmp_uid;
627 gid_t tmp_gid;
628 mode_t tmp_mode;
629 dev_t tmp_dev;
631 if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) {
632 stbuf->st_uid = le32_to_cpu(tmp_uid);
634 if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
635 stbuf->st_gid = le32_to_cpu(tmp_gid);
637 if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) {
638 stbuf->st_mode = le32_to_cpu(tmp_mode);
640 if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
641 stbuf->st_rdev = le64_to_cpu(tmp_dev);
643 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
644 errno = EOPNOTSUPP;
645 return -1;
647 return err;
650 static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
651 int flags, FsCred *credp, V9fsFidOpenState *fs)
653 char *path;
654 int fd = -1;
655 int err = -1;
656 int serrno = 0;
657 V9fsString fullname;
658 char *buffer = NULL;
661 * Mark all the open to not follow symlinks
663 flags |= O_NOFOLLOW;
665 v9fs_string_init(&fullname);
666 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
667 path = fullname.data;
669 /* Determine the security model */
670 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
671 buffer = rpath(fs_ctx, path);
672 fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
673 if (fd == -1) {
674 err = fd;
675 goto out;
677 credp->fc_mode = credp->fc_mode|S_IFREG;
678 /* Set cleint credentials in xattr */
679 err = local_set_xattr(buffer, credp);
680 if (err == -1) {
681 serrno = errno;
682 goto err_end;
684 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
685 buffer = rpath(fs_ctx, path);
686 fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
687 if (fd == -1) {
688 err = fd;
689 goto out;
691 credp->fc_mode = credp->fc_mode|S_IFREG;
692 /* Set client credentials in .virtfs_metadata directory files */
693 err = local_set_mapped_file_attr(fs_ctx, path, credp);
694 if (err == -1) {
695 serrno = errno;
696 goto err_end;
698 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
699 (fs_ctx->export_flags & V9FS_SM_NONE)) {
700 buffer = rpath(fs_ctx, path);
701 fd = open(buffer, flags, credp->fc_mode);
702 if (fd == -1) {
703 err = fd;
704 goto out;
706 err = local_post_create_passthrough(fs_ctx, path, credp);
707 if (err == -1) {
708 serrno = errno;
709 goto err_end;
712 err = fd;
713 fs->fd = fd;
714 goto out;
716 err_end:
717 close(fd);
718 remove(buffer);
719 errno = serrno;
720 out:
721 g_free(buffer);
722 v9fs_string_free(&fullname);
723 return err;
727 static int local_symlink(FsContext *fs_ctx, const char *oldpath,
728 V9fsPath *dir_path, const char *name, FsCred *credp)
730 int err = -1;
731 int serrno = 0;
732 char *newpath;
733 V9fsString fullname;
734 char *buffer = NULL;
736 v9fs_string_init(&fullname);
737 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
738 newpath = fullname.data;
740 /* Determine the security model */
741 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
742 int fd;
743 ssize_t oldpath_size, write_size;
744 buffer = rpath(fs_ctx, newpath);
745 fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS);
746 if (fd == -1) {
747 err = fd;
748 goto out;
750 /* Write the oldpath (target) to the file. */
751 oldpath_size = strlen(oldpath);
752 do {
753 write_size = write(fd, (void *)oldpath, oldpath_size);
754 } while (write_size == -1 && errno == EINTR);
756 if (write_size != oldpath_size) {
757 serrno = errno;
758 close(fd);
759 err = -1;
760 goto err_end;
762 close(fd);
763 /* Set cleint credentials in symlink's xattr */
764 credp->fc_mode = credp->fc_mode|S_IFLNK;
765 err = local_set_xattr(buffer, credp);
766 if (err == -1) {
767 serrno = errno;
768 goto err_end;
770 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
771 int fd;
772 ssize_t oldpath_size, write_size;
773 buffer = rpath(fs_ctx, newpath);
774 fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS);
775 if (fd == -1) {
776 err = fd;
777 goto out;
779 /* Write the oldpath (target) to the file. */
780 oldpath_size = strlen(oldpath);
781 do {
782 write_size = write(fd, (void *)oldpath, oldpath_size);
783 } while (write_size == -1 && errno == EINTR);
785 if (write_size != oldpath_size) {
786 serrno = errno;
787 close(fd);
788 err = -1;
789 goto err_end;
791 close(fd);
792 /* Set cleint credentials in symlink's xattr */
793 credp->fc_mode = credp->fc_mode|S_IFLNK;
794 err = local_set_mapped_file_attr(fs_ctx, newpath, credp);
795 if (err == -1) {
796 serrno = errno;
797 goto err_end;
799 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
800 (fs_ctx->export_flags & V9FS_SM_NONE)) {
801 buffer = rpath(fs_ctx, newpath);
802 err = symlink(oldpath, buffer);
803 if (err) {
804 goto out;
806 err = lchown(buffer, credp->fc_uid, credp->fc_gid);
807 if (err == -1) {
809 * If we fail to change ownership and if we are
810 * using security model none. Ignore the error
812 if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
813 serrno = errno;
814 goto err_end;
815 } else
816 err = 0;
819 goto out;
821 err_end:
822 remove(buffer);
823 errno = serrno;
824 out:
825 g_free(buffer);
826 v9fs_string_free(&fullname);
827 return err;
830 static int local_link(FsContext *ctx, V9fsPath *oldpath,
831 V9fsPath *dirpath, const char *name)
833 int ret;
834 V9fsString newpath;
835 char *buffer, *buffer1;
837 v9fs_string_init(&newpath);
838 v9fs_string_sprintf(&newpath, "%s/%s", dirpath->data, name);
840 buffer = rpath(ctx, oldpath->data);
841 buffer1 = rpath(ctx, newpath.data);
842 ret = link(buffer, buffer1);
843 g_free(buffer);
844 g_free(buffer1);
846 /* now link the virtfs_metadata files */
847 if (!ret && (ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
848 /* Link the .virtfs_metadata files. Create the metada directory */
849 ret = local_create_mapped_attr_dir(ctx, newpath.data);
850 if (ret < 0) {
851 goto err_out;
853 buffer = local_mapped_attr_path(ctx, oldpath->data);
854 buffer1 = local_mapped_attr_path(ctx, newpath.data);
855 ret = link(buffer, buffer1);
856 g_free(buffer);
857 g_free(buffer1);
858 if (ret < 0 && errno != ENOENT) {
859 goto err_out;
862 err_out:
863 v9fs_string_free(&newpath);
864 return ret;
867 static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
869 char *buffer;
870 int ret;
871 char *path = fs_path->data;
873 buffer = rpath(ctx, path);
874 ret = truncate(buffer, size);
875 g_free(buffer);
876 return ret;
879 static int local_rename(FsContext *ctx, const char *oldpath,
880 const char *newpath)
882 int err;
883 char *buffer, *buffer1;
885 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
886 err = local_create_mapped_attr_dir(ctx, newpath);
887 if (err < 0) {
888 return err;
890 /* rename the .virtfs_metadata files */
891 buffer = local_mapped_attr_path(ctx, oldpath);
892 buffer1 = local_mapped_attr_path(ctx, newpath);
893 err = rename(buffer, buffer1);
894 g_free(buffer);
895 g_free(buffer1);
896 if (err < 0 && errno != ENOENT) {
897 return err;
901 buffer = rpath(ctx, oldpath);
902 buffer1 = rpath(ctx, newpath);
903 err = rename(buffer, buffer1);
904 g_free(buffer);
905 g_free(buffer1);
906 return err;
909 static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
911 char *buffer;
912 int ret = -1;
913 char *path = fs_path->data;
915 if ((credp->fc_uid == -1 && credp->fc_gid == -1) ||
916 (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
917 (fs_ctx->export_flags & V9FS_SM_NONE)) {
918 buffer = rpath(fs_ctx, path);
919 ret = lchown(buffer, credp->fc_uid, credp->fc_gid);
920 g_free(buffer);
921 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
922 buffer = rpath(fs_ctx, path);
923 ret = local_set_xattr(buffer, credp);
924 g_free(buffer);
925 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
926 return local_set_mapped_file_attr(fs_ctx, path, credp);
928 return ret;
931 static int local_utimensat(FsContext *s, V9fsPath *fs_path,
932 const struct timespec *buf)
934 char *buffer;
935 int ret;
936 char *path = fs_path->data;
938 buffer = rpath(s, path);
939 ret = qemu_utimens(buffer, buf);
940 g_free(buffer);
941 return ret;
944 static int local_remove(FsContext *ctx, const char *path)
946 int err;
947 struct stat stbuf;
948 char *buffer;
950 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
951 buffer = rpath(ctx, path);
952 err = lstat(buffer, &stbuf);
953 g_free(buffer);
954 if (err) {
955 goto err_out;
958 * If directory remove .virtfs_metadata contained in the
959 * directory
961 if (S_ISDIR(stbuf.st_mode)) {
962 buffer = g_strdup_printf("%s/%s/%s", ctx->fs_root,
963 path, VIRTFS_META_DIR);
964 err = remove(buffer);
965 g_free(buffer);
966 if (err < 0 && errno != ENOENT) {
968 * We didn't had the .virtfs_metadata file. May be file created
969 * in non-mapped mode ?. Ignore ENOENT.
971 goto err_out;
975 * Now remove the name from parent directory
976 * .virtfs_metadata directory
978 buffer = local_mapped_attr_path(ctx, path);
979 err = remove(buffer);
980 g_free(buffer);
981 if (err < 0 && errno != ENOENT) {
983 * We didn't had the .virtfs_metadata file. May be file created
984 * in non-mapped mode ?. Ignore ENOENT.
986 goto err_out;
990 buffer = rpath(ctx, path);
991 err = remove(buffer);
992 g_free(buffer);
993 err_out:
994 return err;
997 static int local_fsync(FsContext *ctx, int fid_type,
998 V9fsFidOpenState *fs, int datasync)
1000 int fd;
1002 if (fid_type == P9_FID_DIR) {
1003 fd = dirfd(fs->dir.stream);
1004 } else {
1005 fd = fs->fd;
1008 if (datasync) {
1009 return qemu_fdatasync(fd);
1010 } else {
1011 return fsync(fd);
1015 static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
1017 char *buffer;
1018 int ret;
1019 char *path = fs_path->data;
1021 buffer = rpath(s, path);
1022 ret = statfs(buffer, stbuf);
1023 g_free(buffer);
1024 return ret;
1027 static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
1028 const char *name, void *value, size_t size)
1030 char *path = fs_path->data;
1032 return v9fs_get_xattr(ctx, path, name, value, size);
1035 static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path,
1036 void *value, size_t size)
1038 char *path = fs_path->data;
1040 return v9fs_list_xattr(ctx, path, value, size);
1043 static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
1044 void *value, size_t size, int flags)
1046 char *path = fs_path->data;
1048 return v9fs_set_xattr(ctx, path, name, value, size, flags);
1051 static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
1052 const char *name)
1054 char *path = fs_path->data;
1056 return v9fs_remove_xattr(ctx, path, name);
1059 static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path,
1060 const char *name, V9fsPath *target)
1062 if (dir_path) {
1063 v9fs_string_sprintf((V9fsString *)target, "%s/%s",
1064 dir_path->data, name);
1065 } else {
1066 v9fs_string_sprintf((V9fsString *)target, "%s", name);
1068 /* Bump the size for including terminating NULL */
1069 target->size++;
1070 return 0;
1073 static int local_renameat(FsContext *ctx, V9fsPath *olddir,
1074 const char *old_name, V9fsPath *newdir,
1075 const char *new_name)
1077 int ret;
1078 V9fsString old_full_name, new_full_name;
1080 v9fs_string_init(&old_full_name);
1081 v9fs_string_init(&new_full_name);
1083 v9fs_string_sprintf(&old_full_name, "%s/%s", olddir->data, old_name);
1084 v9fs_string_sprintf(&new_full_name, "%s/%s", newdir->data, new_name);
1086 ret = local_rename(ctx, old_full_name.data, new_full_name.data);
1087 v9fs_string_free(&old_full_name);
1088 v9fs_string_free(&new_full_name);
1089 return ret;
1092 static int local_unlinkat(FsContext *ctx, V9fsPath *dir,
1093 const char *name, int flags)
1095 int ret;
1096 V9fsString fullname;
1097 char *buffer;
1099 v9fs_string_init(&fullname);
1101 v9fs_string_sprintf(&fullname, "%s/%s", dir->data, name);
1102 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1103 if (flags == AT_REMOVEDIR) {
1105 * If directory remove .virtfs_metadata contained in the
1106 * directory
1108 buffer = g_strdup_printf("%s/%s/%s", ctx->fs_root,
1109 fullname.data, VIRTFS_META_DIR);
1110 ret = remove(buffer);
1111 g_free(buffer);
1112 if (ret < 0 && errno != ENOENT) {
1114 * We didn't had the .virtfs_metadata file. May be file created
1115 * in non-mapped mode ?. Ignore ENOENT.
1117 goto err_out;
1121 * Now remove the name from parent directory
1122 * .virtfs_metadata directory.
1124 buffer = local_mapped_attr_path(ctx, fullname.data);
1125 ret = remove(buffer);
1126 g_free(buffer);
1127 if (ret < 0 && errno != ENOENT) {
1129 * We didn't had the .virtfs_metadata file. May be file created
1130 * in non-mapped mode ?. Ignore ENOENT.
1132 goto err_out;
1135 /* Remove the name finally */
1136 buffer = rpath(ctx, fullname.data);
1137 ret = remove(buffer);
1138 g_free(buffer);
1140 err_out:
1141 v9fs_string_free(&fullname);
1142 return ret;
1145 static int local_ioc_getversion(FsContext *ctx, V9fsPath *path,
1146 mode_t st_mode, uint64_t *st_gen)
1148 #ifdef FS_IOC_GETVERSION
1149 int err;
1150 V9fsFidOpenState fid_open;
1153 * Do not try to open special files like device nodes, fifos etc
1154 * We can get fd for regular files and directories only
1156 if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
1157 errno = ENOTTY;
1158 return -1;
1160 err = local_open(ctx, path, O_RDONLY, &fid_open);
1161 if (err < 0) {
1162 return err;
1164 err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
1165 local_close(ctx, &fid_open);
1166 return err;
1167 #else
1168 errno = ENOTTY;
1169 return -1;
1170 #endif
1173 static int local_init(FsContext *ctx)
1175 int err = 0;
1176 struct statfs stbuf;
1178 if (ctx->export_flags & V9FS_SM_PASSTHROUGH) {
1179 ctx->xops = passthrough_xattr_ops;
1180 } else if (ctx->export_flags & V9FS_SM_MAPPED) {
1181 ctx->xops = mapped_xattr_ops;
1182 } else if (ctx->export_flags & V9FS_SM_NONE) {
1183 ctx->xops = none_xattr_ops;
1184 } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1186 * xattr operation for mapped-file and passthrough
1187 * remain same.
1189 ctx->xops = passthrough_xattr_ops;
1191 ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT;
1192 #ifdef FS_IOC_GETVERSION
1194 * use ioc_getversion only if the iocl is definied
1196 err = statfs(ctx->fs_root, &stbuf);
1197 if (!err) {
1198 switch (stbuf.f_type) {
1199 case EXT2_SUPER_MAGIC:
1200 case BTRFS_SUPER_MAGIC:
1201 case REISERFS_SUPER_MAGIC:
1202 case XFS_SUPER_MAGIC:
1203 ctx->exops.get_st_gen = local_ioc_getversion;
1204 break;
1207 #endif
1208 return err;
1211 static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse)
1213 const char *sec_model = qemu_opt_get(opts, "security_model");
1214 const char *path = qemu_opt_get(opts, "path");
1216 if (!sec_model) {
1217 error_report("Security model not specified, local fs needs security model");
1218 error_printf("valid options are:"
1219 "\tsecurity_model=[passthrough|mapped-xattr|mapped-file|none]\n");
1220 return -1;
1223 if (!strcmp(sec_model, "passthrough")) {
1224 fse->export_flags |= V9FS_SM_PASSTHROUGH;
1225 } else if (!strcmp(sec_model, "mapped") ||
1226 !strcmp(sec_model, "mapped-xattr")) {
1227 fse->export_flags |= V9FS_SM_MAPPED;
1228 } else if (!strcmp(sec_model, "none")) {
1229 fse->export_flags |= V9FS_SM_NONE;
1230 } else if (!strcmp(sec_model, "mapped-file")) {
1231 fse->export_flags |= V9FS_SM_MAPPED_FILE;
1232 } else {
1233 error_report("Invalid security model %s specified", sec_model);
1234 error_printf("valid options are:"
1235 "\t[passthrough|mapped-xattr|mapped-file|none]\n");
1236 return -1;
1239 if (!path) {
1240 error_report("fsdev: No path specified");
1241 return -1;
1243 fse->path = g_strdup(path);
1245 return 0;
1248 FileOperations local_ops = {
1249 .parse_opts = local_parse_opts,
1250 .init = local_init,
1251 .lstat = local_lstat,
1252 .readlink = local_readlink,
1253 .close = local_close,
1254 .closedir = local_closedir,
1255 .open = local_open,
1256 .opendir = local_opendir,
1257 .rewinddir = local_rewinddir,
1258 .telldir = local_telldir,
1259 .readdir = local_readdir,
1260 .seekdir = local_seekdir,
1261 .preadv = local_preadv,
1262 .pwritev = local_pwritev,
1263 .chmod = local_chmod,
1264 .mknod = local_mknod,
1265 .mkdir = local_mkdir,
1266 .fstat = local_fstat,
1267 .open2 = local_open2,
1268 .symlink = local_symlink,
1269 .link = local_link,
1270 .truncate = local_truncate,
1271 .rename = local_rename,
1272 .chown = local_chown,
1273 .utimensat = local_utimensat,
1274 .remove = local_remove,
1275 .fsync = local_fsync,
1276 .statfs = local_statfs,
1277 .lgetxattr = local_lgetxattr,
1278 .llistxattr = local_llistxattr,
1279 .lsetxattr = local_lsetxattr,
1280 .lremovexattr = local_lremovexattr,
1281 .name_to_path = local_name_to_path,
1282 .renameat = local_renameat,
1283 .unlinkat = local_unlinkat,