target-s390x: support OC and NC in the EX instruction
[qemu/qmp-unstable.git] / hw / 9pfs / virtio-9p-local.c
bloba183eee662b62b514b6d97f3644287ec93ed899f
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 = 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 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) {
401 entry->d_type = DT_UNKNOWN;
402 } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
403 if (!ret && *result != NULL &&
404 !strcmp(entry->d_name, VIRTFS_META_DIR)) {
405 /* skp the meta data directory */
406 goto again;
408 entry->d_type = DT_UNKNOWN;
410 return ret;
413 static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
415 return seekdir(fs->dir, off);
418 static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs,
419 const struct iovec *iov,
420 int iovcnt, off_t offset)
422 #ifdef CONFIG_PREADV
423 return preadv(fs->fd, iov, iovcnt, offset);
424 #else
425 int err = lseek(fs->fd, offset, SEEK_SET);
426 if (err == -1) {
427 return err;
428 } else {
429 return readv(fs->fd, iov, iovcnt);
431 #endif
434 static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
435 const struct iovec *iov,
436 int iovcnt, off_t offset)
438 ssize_t ret
440 #ifdef CONFIG_PREADV
441 ret = pwritev(fs->fd, iov, iovcnt, offset);
442 #else
443 int err = lseek(fs->fd, offset, SEEK_SET);
444 if (err == -1) {
445 return err;
446 } else {
447 ret = writev(fs->fd, iov, iovcnt);
449 #endif
450 #ifdef CONFIG_SYNC_FILE_RANGE
451 if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
453 * Initiate a writeback. This is not a data integrity sync.
454 * We want to ensure that we don't leave dirty pages in the cache
455 * after write when writeout=immediate is sepcified.
457 sync_file_range(fs->fd, offset, ret,
458 SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
460 #endif
461 return ret;
464 static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
466 char *buffer;
467 int ret = -1;
468 char *path = fs_path->data;
470 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
471 buffer = rpath(fs_ctx, path);
472 ret = local_set_xattr(buffer, credp);
473 g_free(buffer);
474 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
475 return local_set_mapped_file_attr(fs_ctx, path, credp);
476 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
477 (fs_ctx->export_flags & V9FS_SM_NONE)) {
478 buffer = rpath(fs_ctx, path);
479 ret = chmod(buffer, credp->fc_mode);
480 g_free(buffer);
482 return ret;
485 static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
486 const char *name, FsCred *credp)
488 char *path;
489 int err = -1;
490 int serrno = 0;
491 V9fsString fullname;
492 char *buffer;
494 v9fs_string_init(&fullname);
495 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
496 path = fullname.data;
498 /* Determine the security model */
499 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
500 buffer = rpath(fs_ctx, path);
501 err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0);
502 if (err == -1) {
503 g_free(buffer);
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 g_free(buffer);
517 goto out;
519 err = local_set_mapped_file_attr(fs_ctx, path, credp);
520 if (err == -1) {
521 serrno = errno;
522 goto err_end;
524 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
525 (fs_ctx->export_flags & V9FS_SM_NONE)) {
526 buffer = rpath(fs_ctx, path);
527 err = mknod(buffer, credp->fc_mode, credp->fc_rdev);
528 if (err == -1) {
529 g_free(buffer);
530 goto out;
532 err = local_post_create_passthrough(fs_ctx, path, credp);
533 if (err == -1) {
534 serrno = errno;
535 goto err_end;
538 goto out;
540 err_end:
541 remove(buffer);
542 errno = serrno;
543 g_free(buffer);
544 out:
545 v9fs_string_free(&fullname);
546 return err;
549 static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
550 const char *name, FsCred *credp)
552 char *path;
553 int err = -1;
554 int serrno = 0;
555 V9fsString fullname;
556 char *buffer;
558 v9fs_string_init(&fullname);
559 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
560 path = fullname.data;
562 /* Determine the security model */
563 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
564 buffer = rpath(fs_ctx, path);
565 err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
566 if (err == -1) {
567 g_free(buffer);
568 goto out;
570 credp->fc_mode = credp->fc_mode|S_IFDIR;
571 err = local_set_xattr(buffer, credp);
572 if (err == -1) {
573 serrno = errno;
574 goto err_end;
576 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
577 buffer = rpath(fs_ctx, path);
578 err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
579 if (err == -1) {
580 g_free(buffer);
581 goto out;
583 credp->fc_mode = credp->fc_mode|S_IFDIR;
584 err = local_set_mapped_file_attr(fs_ctx, path, credp);
585 if (err == -1) {
586 serrno = errno;
587 goto err_end;
589 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
590 (fs_ctx->export_flags & V9FS_SM_NONE)) {
591 buffer = rpath(fs_ctx, path);
592 err = mkdir(buffer, credp->fc_mode);
593 if (err == -1) {
594 g_free(buffer);
595 goto out;
597 err = local_post_create_passthrough(fs_ctx, path, credp);
598 if (err == -1) {
599 serrno = errno;
600 goto err_end;
603 goto out;
605 err_end:
606 remove(buffer);
607 errno = serrno;
608 g_free(buffer);
609 out:
610 v9fs_string_free(&fullname);
611 return err;
614 static int local_fstat(FsContext *fs_ctx, int fid_type,
615 V9fsFidOpenState *fs, struct stat *stbuf)
617 int err, fd;
619 if (fid_type == P9_FID_DIR) {
620 fd = dirfd(fs->dir);
621 } else {
622 fd = fs->fd;
625 err = fstat(fd, stbuf);
626 if (err) {
627 return err;
629 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
630 /* Actual credentials are part of extended attrs */
631 uid_t tmp_uid;
632 gid_t tmp_gid;
633 mode_t tmp_mode;
634 dev_t tmp_dev;
636 if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) {
637 stbuf->st_uid = le32_to_cpu(tmp_uid);
639 if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
640 stbuf->st_gid = le32_to_cpu(tmp_gid);
642 if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) {
643 stbuf->st_mode = le32_to_cpu(tmp_mode);
645 if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
646 stbuf->st_rdev = le64_to_cpu(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,