hw/9pfs/virtio-9p-local.c: use snprintf() instead of sprintf()
[qemu/cris-port.git] / hw / 9pfs / virtio-9p-local.c
blobdc615a4d0fa4ddc8760a25f34aa80bfba8519627
1 /*
2 * Virtio 9p Posix callback
4 * Copyright IBM, Corp. 2010
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #include "hw/virtio/virtio.h"
15 #include "virtio-9p.h"
16 #include "virtio-9p-xattr.h"
17 #include <arpa/inet.h>
18 #include <pwd.h>
19 #include <grp.h>
20 #include <sys/socket.h>
21 #include <sys/un.h>
22 #include "qemu/xattr.h"
23 #include <libgen.h>
24 #include <linux/fs.h>
25 #ifdef CONFIG_LINUX_MAGIC_H
26 #include <linux/magic.h>
27 #endif
28 #include <sys/ioctl.h>
30 #ifndef XFS_SUPER_MAGIC
31 #define XFS_SUPER_MAGIC 0x58465342
32 #endif
33 #ifndef EXT2_SUPER_MAGIC
34 #define EXT2_SUPER_MAGIC 0xEF53
35 #endif
36 #ifndef REISERFS_SUPER_MAGIC
37 #define REISERFS_SUPER_MAGIC 0x52654973
38 #endif
39 #ifndef BTRFS_SUPER_MAGIC
40 #define BTRFS_SUPER_MAGIC 0x9123683E
41 #endif
43 #define VIRTFS_META_DIR ".virtfs_metadata"
45 static const char *local_mapped_attr_path(FsContext *ctx,
46 const char *path, char *buffer)
48 char *dir_name;
49 char *tmp_path = g_strdup(path);
50 char *base_name = basename(tmp_path);
52 /* NULL terminate the directory */
53 dir_name = tmp_path;
54 *(base_name - 1) = '\0';
56 snprintf(buffer, PATH_MAX, "%s/%s/%s/%s",
57 ctx->fs_root, dir_name, VIRTFS_META_DIR, base_name);
58 g_free(tmp_path);
59 return buffer;
62 static FILE *local_fopen(const char *path, const char *mode)
64 int fd, o_mode = 0;
65 FILE *fp;
66 int flags = O_NOFOLLOW;
68 * only supports two modes
70 if (mode[0] == 'r') {
71 flags |= O_RDONLY;
72 } else if (mode[0] == 'w') {
73 flags |= O_WRONLY | O_TRUNC | O_CREAT;
74 o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
75 } else {
76 return NULL;
78 fd = open(path, flags, o_mode);
79 if (fd == -1) {
80 return NULL;
82 fp = fdopen(fd, mode);
83 if (!fp) {
84 close(fd);
86 return fp;
89 #define ATTR_MAX 100
90 static void local_mapped_file_attr(FsContext *ctx, const char *path,
91 struct stat *stbuf)
93 FILE *fp;
94 char buf[ATTR_MAX];
95 char attr_path[PATH_MAX];
97 local_mapped_attr_path(ctx, path, attr_path);
98 fp = local_fopen(attr_path, "r");
99 if (!fp) {
100 return;
102 memset(buf, 0, ATTR_MAX);
103 while (fgets(buf, ATTR_MAX, fp)) {
104 if (!strncmp(buf, "virtfs.uid", 10)) {
105 stbuf->st_uid = atoi(buf+11);
106 } else if (!strncmp(buf, "virtfs.gid", 10)) {
107 stbuf->st_gid = atoi(buf+11);
108 } else if (!strncmp(buf, "virtfs.mode", 11)) {
109 stbuf->st_mode = atoi(buf+12);
110 } else if (!strncmp(buf, "virtfs.rdev", 11)) {
111 stbuf->st_rdev = atoi(buf+12);
113 memset(buf, 0, ATTR_MAX);
115 fclose(fp);
118 static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf)
120 int err;
121 char buffer[PATH_MAX];
122 char *path = fs_path->data;
124 err = lstat(rpath(fs_ctx, path, buffer), stbuf);
125 if (err) {
126 return err;
128 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
129 /* Actual credentials are part of extended attrs */
130 uid_t tmp_uid;
131 gid_t tmp_gid;
132 mode_t tmp_mode;
133 dev_t tmp_dev;
134 if (getxattr(rpath(fs_ctx, path, buffer), "user.virtfs.uid", &tmp_uid,
135 sizeof(uid_t)) > 0) {
136 stbuf->st_uid = tmp_uid;
138 if (getxattr(rpath(fs_ctx, path, buffer), "user.virtfs.gid", &tmp_gid,
139 sizeof(gid_t)) > 0) {
140 stbuf->st_gid = tmp_gid;
142 if (getxattr(rpath(fs_ctx, path, buffer), "user.virtfs.mode",
143 &tmp_mode, sizeof(mode_t)) > 0) {
144 stbuf->st_mode = tmp_mode;
146 if (getxattr(rpath(fs_ctx, path, buffer), "user.virtfs.rdev", &tmp_dev,
147 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);
153 return err;
156 static int local_create_mapped_attr_dir(FsContext *ctx, const char *path)
158 int err;
159 char attr_dir[PATH_MAX];
160 char *tmp_path = g_strdup(path);
162 snprintf(attr_dir, PATH_MAX, "%s/%s/%s",
163 ctx->fs_root, dirname(tmp_path), VIRTFS_META_DIR);
165 err = mkdir(attr_dir, 0700);
166 if (err < 0 && errno == EEXIST) {
167 err = 0;
169 g_free(tmp_path);
170 return err;
173 static int local_set_mapped_file_attr(FsContext *ctx,
174 const char *path, FsCred *credp)
176 FILE *fp;
177 int ret = 0;
178 char buf[ATTR_MAX];
179 char attr_path[PATH_MAX];
180 int uid = -1, gid = -1, mode = -1, rdev = -1;
182 fp = local_fopen(local_mapped_attr_path(ctx, path, attr_path), "r");
183 if (!fp) {
184 goto create_map_file;
186 memset(buf, 0, ATTR_MAX);
187 while (fgets(buf, ATTR_MAX, fp)) {
188 if (!strncmp(buf, "virtfs.uid", 10)) {
189 uid = atoi(buf+11);
190 } else if (!strncmp(buf, "virtfs.gid", 10)) {
191 gid = atoi(buf+11);
192 } else if (!strncmp(buf, "virtfs.mode", 11)) {
193 mode = atoi(buf+12);
194 } else if (!strncmp(buf, "virtfs.rdev", 11)) {
195 rdev = atoi(buf+12);
197 memset(buf, 0, ATTR_MAX);
199 fclose(fp);
200 goto update_map_file;
202 create_map_file:
203 ret = local_create_mapped_attr_dir(ctx, path);
204 if (ret < 0) {
205 goto err_out;
208 update_map_file:
209 fp = local_fopen(attr_path, "w");
210 if (!fp) {
211 ret = -1;
212 goto err_out;
215 if (credp->fc_uid != -1) {
216 uid = credp->fc_uid;
218 if (credp->fc_gid != -1) {
219 gid = credp->fc_gid;
221 if (credp->fc_mode != -1) {
222 mode = credp->fc_mode;
224 if (credp->fc_rdev != -1) {
225 rdev = credp->fc_rdev;
229 if (uid != -1) {
230 fprintf(fp, "virtfs.uid=%d\n", uid);
232 if (gid != -1) {
233 fprintf(fp, "virtfs.gid=%d\n", gid);
235 if (mode != -1) {
236 fprintf(fp, "virtfs.mode=%d\n", mode);
238 if (rdev != -1) {
239 fprintf(fp, "virtfs.rdev=%d\n", rdev);
241 fclose(fp);
243 err_out:
244 return ret;
247 static int local_set_xattr(const char *path, FsCred *credp)
249 int err;
251 if (credp->fc_uid != -1) {
252 err = setxattr(path, "user.virtfs.uid", &credp->fc_uid, sizeof(uid_t),
254 if (err) {
255 return err;
258 if (credp->fc_gid != -1) {
259 err = setxattr(path, "user.virtfs.gid", &credp->fc_gid, sizeof(gid_t),
261 if (err) {
262 return err;
265 if (credp->fc_mode != -1) {
266 err = setxattr(path, "user.virtfs.mode", &credp->fc_mode,
267 sizeof(mode_t), 0);
268 if (err) {
269 return err;
272 if (credp->fc_rdev != -1) {
273 err = setxattr(path, "user.virtfs.rdev", &credp->fc_rdev,
274 sizeof(dev_t), 0);
275 if (err) {
276 return err;
279 return 0;
282 static int local_post_create_passthrough(FsContext *fs_ctx, const char *path,
283 FsCred *credp)
285 char buffer[PATH_MAX];
287 if (lchown(rpath(fs_ctx, path, buffer), credp->fc_uid,
288 credp->fc_gid) < 0) {
290 * If we fail to change ownership and if we are
291 * using security model none. Ignore the error
293 if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
294 return -1;
298 if (chmod(rpath(fs_ctx, path, buffer), credp->fc_mode & 07777) < 0) {
299 return -1;
301 return 0;
304 static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
305 char *buf, size_t bufsz)
307 ssize_t tsize = -1;
308 char buffer[PATH_MAX];
309 char *path = fs_path->data;
311 if ((fs_ctx->export_flags & V9FS_SM_MAPPED) ||
312 (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
313 int fd;
314 fd = open(rpath(fs_ctx, path, buffer), O_RDONLY | O_NOFOLLOW);
315 if (fd == -1) {
316 return -1;
318 do {
319 tsize = read(fd, (void *)buf, bufsz);
320 } while (tsize == -1 && errno == EINTR);
321 close(fd);
322 return tsize;
323 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
324 (fs_ctx->export_flags & V9FS_SM_NONE)) {
325 tsize = readlink(rpath(fs_ctx, path, buffer), buf, bufsz);
327 return tsize;
330 static int local_close(FsContext *ctx, V9fsFidOpenState *fs)
332 return close(fs->fd);
335 static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs)
337 return closedir(fs->dir);
340 static int local_open(FsContext *ctx, V9fsPath *fs_path,
341 int flags, V9fsFidOpenState *fs)
343 char buffer[PATH_MAX];
344 char *path = fs_path->data;
346 fs->fd = open(rpath(ctx, path, buffer), flags | O_NOFOLLOW);
347 return fs->fd;
350 static int local_opendir(FsContext *ctx,
351 V9fsPath *fs_path, V9fsFidOpenState *fs)
353 char buffer[PATH_MAX];
354 char *path = fs_path->data;
356 fs->dir = opendir(rpath(ctx, path, buffer));
357 if (!fs->dir) {
358 return -1;
360 return 0;
363 static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
365 return rewinddir(fs->dir);
368 static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs)
370 return telldir(fs->dir);
373 static int local_readdir_r(FsContext *ctx, V9fsFidOpenState *fs,
374 struct dirent *entry,
375 struct dirent **result)
377 int ret;
379 again:
380 ret = readdir_r(fs->dir, entry, result);
381 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
382 if (!ret && *result != NULL &&
383 !strcmp(entry->d_name, VIRTFS_META_DIR)) {
384 /* skp the meta data directory */
385 goto again;
388 return ret;
391 static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
393 return seekdir(fs->dir, off);
396 static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs,
397 const struct iovec *iov,
398 int iovcnt, off_t offset)
400 #ifdef CONFIG_PREADV
401 return preadv(fs->fd, iov, iovcnt, offset);
402 #else
403 int err = lseek(fs->fd, offset, SEEK_SET);
404 if (err == -1) {
405 return err;
406 } else {
407 return readv(fs->fd, iov, iovcnt);
409 #endif
412 static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
413 const struct iovec *iov,
414 int iovcnt, off_t offset)
416 ssize_t ret
418 #ifdef CONFIG_PREADV
419 ret = pwritev(fs->fd, iov, iovcnt, offset);
420 #else
421 int err = lseek(fs->fd, offset, SEEK_SET);
422 if (err == -1) {
423 return err;
424 } else {
425 ret = writev(fs->fd, iov, iovcnt);
427 #endif
428 #ifdef CONFIG_SYNC_FILE_RANGE
429 if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
431 * Initiate a writeback. This is not a data integrity sync.
432 * We want to ensure that we don't leave dirty pages in the cache
433 * after write when writeout=immediate is sepcified.
435 sync_file_range(fs->fd, offset, ret,
436 SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
438 #endif
439 return ret;
442 static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
444 char buffer[PATH_MAX];
445 char *path = fs_path->data;
447 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
448 return local_set_xattr(rpath(fs_ctx, path, buffer), credp);
449 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
450 return local_set_mapped_file_attr(fs_ctx, path, credp);
451 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
452 (fs_ctx->export_flags & V9FS_SM_NONE)) {
453 return chmod(rpath(fs_ctx, path, buffer), credp->fc_mode);
455 return -1;
458 static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
459 const char *name, FsCred *credp)
461 char *path;
462 int err = -1;
463 int serrno = 0;
464 V9fsString fullname;
465 char buffer[PATH_MAX];
467 v9fs_string_init(&fullname);
468 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
469 path = fullname.data;
471 /* Determine the security model */
472 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
473 err = mknod(rpath(fs_ctx, path, buffer),
474 SM_LOCAL_MODE_BITS|S_IFREG, 0);
475 if (err == -1) {
476 goto out;
478 err = local_set_xattr(rpath(fs_ctx, path, buffer), credp);
479 if (err == -1) {
480 serrno = errno;
481 goto err_end;
483 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
485 err = mknod(rpath(fs_ctx, path, buffer),
486 SM_LOCAL_MODE_BITS|S_IFREG, 0);
487 if (err == -1) {
488 goto out;
490 err = local_set_mapped_file_attr(fs_ctx, path, credp);
491 if (err == -1) {
492 serrno = errno;
493 goto err_end;
495 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
496 (fs_ctx->export_flags & V9FS_SM_NONE)) {
497 err = mknod(rpath(fs_ctx, path, buffer), credp->fc_mode,
498 credp->fc_rdev);
499 if (err == -1) {
500 goto out;
502 err = local_post_create_passthrough(fs_ctx, path, credp);
503 if (err == -1) {
504 serrno = errno;
505 goto err_end;
508 goto out;
510 err_end:
511 remove(rpath(fs_ctx, path, buffer));
512 errno = serrno;
513 out:
514 v9fs_string_free(&fullname);
515 return err;
518 static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
519 const char *name, FsCred *credp)
521 char *path;
522 int err = -1;
523 int serrno = 0;
524 V9fsString fullname;
525 char buffer[PATH_MAX];
527 v9fs_string_init(&fullname);
528 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
529 path = fullname.data;
531 /* Determine the security model */
532 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
533 err = mkdir(rpath(fs_ctx, path, buffer), SM_LOCAL_DIR_MODE_BITS);
534 if (err == -1) {
535 goto out;
537 credp->fc_mode = credp->fc_mode|S_IFDIR;
538 err = local_set_xattr(rpath(fs_ctx, path, buffer), credp);
539 if (err == -1) {
540 serrno = errno;
541 goto err_end;
543 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
544 err = mkdir(rpath(fs_ctx, path, buffer), SM_LOCAL_DIR_MODE_BITS);
545 if (err == -1) {
546 goto out;
548 credp->fc_mode = credp->fc_mode|S_IFDIR;
549 err = local_set_mapped_file_attr(fs_ctx, path, credp);
550 if (err == -1) {
551 serrno = errno;
552 goto err_end;
554 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
555 (fs_ctx->export_flags & V9FS_SM_NONE)) {
556 err = mkdir(rpath(fs_ctx, path, buffer), credp->fc_mode);
557 if (err == -1) {
558 goto out;
560 err = local_post_create_passthrough(fs_ctx, path, credp);
561 if (err == -1) {
562 serrno = errno;
563 goto err_end;
566 goto out;
568 err_end:
569 remove(rpath(fs_ctx, path, buffer));
570 errno = serrno;
571 out:
572 v9fs_string_free(&fullname);
573 return err;
576 static int local_fstat(FsContext *fs_ctx, int fid_type,
577 V9fsFidOpenState *fs, struct stat *stbuf)
579 int err, fd;
581 if (fid_type == P9_FID_DIR) {
582 fd = dirfd(fs->dir);
583 } else {
584 fd = fs->fd;
587 err = fstat(fd, stbuf);
588 if (err) {
589 return err;
591 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
592 /* Actual credentials are part of extended attrs */
593 uid_t tmp_uid;
594 gid_t tmp_gid;
595 mode_t tmp_mode;
596 dev_t tmp_dev;
598 if (fgetxattr(fd, "user.virtfs.uid",
599 &tmp_uid, sizeof(uid_t)) > 0) {
600 stbuf->st_uid = tmp_uid;
602 if (fgetxattr(fd, "user.virtfs.gid",
603 &tmp_gid, sizeof(gid_t)) > 0) {
604 stbuf->st_gid = tmp_gid;
606 if (fgetxattr(fd, "user.virtfs.mode",
607 &tmp_mode, sizeof(mode_t)) > 0) {
608 stbuf->st_mode = tmp_mode;
610 if (fgetxattr(fd, "user.virtfs.rdev",
611 &tmp_dev, sizeof(dev_t)) > 0) {
612 stbuf->st_rdev = tmp_dev;
614 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
615 errno = EOPNOTSUPP;
616 return -1;
618 return err;
621 static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
622 int flags, FsCred *credp, V9fsFidOpenState *fs)
624 char *path;
625 int fd = -1;
626 int err = -1;
627 int serrno = 0;
628 V9fsString fullname;
629 char buffer[PATH_MAX];
632 * Mark all the open to not follow symlinks
634 flags |= O_NOFOLLOW;
636 v9fs_string_init(&fullname);
637 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
638 path = fullname.data;
640 /* Determine the security model */
641 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
642 fd = open(rpath(fs_ctx, path, buffer), flags, SM_LOCAL_MODE_BITS);
643 if (fd == -1) {
644 err = fd;
645 goto out;
647 credp->fc_mode = credp->fc_mode|S_IFREG;
648 /* Set cleint credentials in xattr */
649 err = local_set_xattr(rpath(fs_ctx, path, buffer), credp);
650 if (err == -1) {
651 serrno = errno;
652 goto err_end;
654 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
655 fd = open(rpath(fs_ctx, path, buffer), flags, SM_LOCAL_MODE_BITS);
656 if (fd == -1) {
657 err = fd;
658 goto out;
660 credp->fc_mode = credp->fc_mode|S_IFREG;
661 /* Set client credentials in .virtfs_metadata directory files */
662 err = local_set_mapped_file_attr(fs_ctx, path, credp);
663 if (err == -1) {
664 serrno = errno;
665 goto err_end;
667 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
668 (fs_ctx->export_flags & V9FS_SM_NONE)) {
669 fd = open(rpath(fs_ctx, path, buffer), flags, credp->fc_mode);
670 if (fd == -1) {
671 err = fd;
672 goto out;
674 err = local_post_create_passthrough(fs_ctx, path, credp);
675 if (err == -1) {
676 serrno = errno;
677 goto err_end;
680 err = fd;
681 fs->fd = fd;
682 goto out;
684 err_end:
685 close(fd);
686 remove(rpath(fs_ctx, path, buffer));
687 errno = serrno;
688 out:
689 v9fs_string_free(&fullname);
690 return err;
694 static int local_symlink(FsContext *fs_ctx, const char *oldpath,
695 V9fsPath *dir_path, const char *name, FsCred *credp)
697 int err = -1;
698 int serrno = 0;
699 char *newpath;
700 V9fsString fullname;
701 char buffer[PATH_MAX];
703 v9fs_string_init(&fullname);
704 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
705 newpath = fullname.data;
707 /* Determine the security model */
708 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
709 int fd;
710 ssize_t oldpath_size, write_size;
711 fd = open(rpath(fs_ctx, newpath, buffer),
712 O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW,
713 SM_LOCAL_MODE_BITS);
714 if (fd == -1) {
715 err = fd;
716 goto out;
718 /* Write the oldpath (target) to the file. */
719 oldpath_size = strlen(oldpath);
720 do {
721 write_size = write(fd, (void *)oldpath, oldpath_size);
722 } while (write_size == -1 && errno == EINTR);
724 if (write_size != oldpath_size) {
725 serrno = errno;
726 close(fd);
727 err = -1;
728 goto err_end;
730 close(fd);
731 /* Set cleint credentials in symlink's xattr */
732 credp->fc_mode = credp->fc_mode|S_IFLNK;
733 err = local_set_xattr(rpath(fs_ctx, newpath, buffer), credp);
734 if (err == -1) {
735 serrno = errno;
736 goto err_end;
738 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
739 int fd;
740 ssize_t oldpath_size, write_size;
741 fd = open(rpath(fs_ctx, newpath, buffer),
742 O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW,
743 SM_LOCAL_MODE_BITS);
744 if (fd == -1) {
745 err = fd;
746 goto out;
748 /* Write the oldpath (target) to the file. */
749 oldpath_size = strlen(oldpath);
750 do {
751 write_size = write(fd, (void *)oldpath, oldpath_size);
752 } while (write_size == -1 && errno == EINTR);
754 if (write_size != oldpath_size) {
755 serrno = errno;
756 close(fd);
757 err = -1;
758 goto err_end;
760 close(fd);
761 /* Set cleint credentials in symlink's xattr */
762 credp->fc_mode = credp->fc_mode|S_IFLNK;
763 err = local_set_mapped_file_attr(fs_ctx, newpath, credp);
764 if (err == -1) {
765 serrno = errno;
766 goto err_end;
768 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
769 (fs_ctx->export_flags & V9FS_SM_NONE)) {
770 err = symlink(oldpath, rpath(fs_ctx, newpath, buffer));
771 if (err) {
772 goto out;
774 err = lchown(rpath(fs_ctx, newpath, buffer), credp->fc_uid,
775 credp->fc_gid);
776 if (err == -1) {
778 * If we fail to change ownership and if we are
779 * using security model none. Ignore the error
781 if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
782 serrno = errno;
783 goto err_end;
784 } else
785 err = 0;
788 goto out;
790 err_end:
791 remove(rpath(fs_ctx, newpath, buffer));
792 errno = serrno;
793 out:
794 v9fs_string_free(&fullname);
795 return err;
798 static int local_link(FsContext *ctx, V9fsPath *oldpath,
799 V9fsPath *dirpath, const char *name)
801 int ret;
802 V9fsString newpath;
803 char buffer[PATH_MAX], buffer1[PATH_MAX];
805 v9fs_string_init(&newpath);
806 v9fs_string_sprintf(&newpath, "%s/%s", dirpath->data, name);
808 ret = link(rpath(ctx, oldpath->data, buffer),
809 rpath(ctx, newpath.data, buffer1));
811 /* now link the virtfs_metadata files */
812 if (!ret && (ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
813 /* Link the .virtfs_metadata files. Create the metada directory */
814 ret = local_create_mapped_attr_dir(ctx, newpath.data);
815 if (ret < 0) {
816 goto err_out;
818 ret = link(local_mapped_attr_path(ctx, oldpath->data, buffer),
819 local_mapped_attr_path(ctx, newpath.data, buffer1));
820 if (ret < 0 && errno != ENOENT) {
821 goto err_out;
824 err_out:
825 v9fs_string_free(&newpath);
826 return ret;
829 static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
831 char buffer[PATH_MAX];
832 char *path = fs_path->data;
834 return truncate(rpath(ctx, path, buffer), size);
837 static int local_rename(FsContext *ctx, const char *oldpath,
838 const char *newpath)
840 int err;
841 char buffer[PATH_MAX], buffer1[PATH_MAX];
843 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
844 err = local_create_mapped_attr_dir(ctx, newpath);
845 if (err < 0) {
846 return err;
848 /* rename the .virtfs_metadata files */
849 err = rename(local_mapped_attr_path(ctx, oldpath, buffer),
850 local_mapped_attr_path(ctx, newpath, buffer1));
851 if (err < 0 && errno != ENOENT) {
852 return err;
855 return rename(rpath(ctx, oldpath, buffer), rpath(ctx, newpath, buffer1));
858 static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
860 char buffer[PATH_MAX];
861 char *path = fs_path->data;
863 if ((credp->fc_uid == -1 && credp->fc_gid == -1) ||
864 (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
865 (fs_ctx->export_flags & V9FS_SM_NONE)) {
866 return lchown(rpath(fs_ctx, path, buffer),
867 credp->fc_uid, credp->fc_gid);
868 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
869 return local_set_xattr(rpath(fs_ctx, path, buffer), credp);
870 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
871 return local_set_mapped_file_attr(fs_ctx, path, credp);
873 return -1;
876 static int local_utimensat(FsContext *s, V9fsPath *fs_path,
877 const struct timespec *buf)
879 char buffer[PATH_MAX];
880 char *path = fs_path->data;
882 return qemu_utimens(rpath(s, path, buffer), buf);
885 static int local_remove(FsContext *ctx, const char *path)
887 int err;
888 struct stat stbuf;
889 char buffer[PATH_MAX];
891 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
892 err = lstat(rpath(ctx, path, buffer), &stbuf);
893 if (err) {
894 goto err_out;
897 * If directory remove .virtfs_metadata contained in the
898 * directory
900 if (S_ISDIR(stbuf.st_mode)) {
901 snprintf(buffer, ARRAY_SIZE(buffer), "%s/%s/%s",
902 ctx->fs_root, path, VIRTFS_META_DIR);
903 err = remove(buffer);
904 if (err < 0 && errno != ENOENT) {
906 * We didn't had the .virtfs_metadata file. May be file created
907 * in non-mapped mode ?. Ignore ENOENT.
909 goto err_out;
913 * Now remove the name from parent directory
914 * .virtfs_metadata directory
916 err = remove(local_mapped_attr_path(ctx, path, buffer));
917 if (err < 0 && errno != ENOENT) {
919 * We didn't had the .virtfs_metadata file. May be file created
920 * in non-mapped mode ?. Ignore ENOENT.
922 goto err_out;
925 return remove(rpath(ctx, path, buffer));
926 err_out:
927 return err;
930 static int local_fsync(FsContext *ctx, int fid_type,
931 V9fsFidOpenState *fs, int datasync)
933 int fd;
935 if (fid_type == P9_FID_DIR) {
936 fd = dirfd(fs->dir);
937 } else {
938 fd = fs->fd;
941 if (datasync) {
942 return qemu_fdatasync(fd);
943 } else {
944 return fsync(fd);
948 static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
950 char buffer[PATH_MAX];
951 char *path = fs_path->data;
953 return statfs(rpath(s, path, buffer), stbuf);
956 static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
957 const char *name, void *value, size_t size)
959 char *path = fs_path->data;
961 return v9fs_get_xattr(ctx, path, name, value, size);
964 static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path,
965 void *value, size_t size)
967 char *path = fs_path->data;
969 return v9fs_list_xattr(ctx, path, value, size);
972 static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
973 void *value, size_t size, int flags)
975 char *path = fs_path->data;
977 return v9fs_set_xattr(ctx, path, name, value, size, flags);
980 static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
981 const char *name)
983 char *path = fs_path->data;
985 return v9fs_remove_xattr(ctx, path, name);
988 static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path,
989 const char *name, V9fsPath *target)
991 if (dir_path) {
992 v9fs_string_sprintf((V9fsString *)target, "%s/%s",
993 dir_path->data, name);
994 } else {
995 v9fs_string_sprintf((V9fsString *)target, "%s", name);
997 /* Bump the size for including terminating NULL */
998 target->size++;
999 return 0;
1002 static int local_renameat(FsContext *ctx, V9fsPath *olddir,
1003 const char *old_name, V9fsPath *newdir,
1004 const char *new_name)
1006 int ret;
1007 V9fsString old_full_name, new_full_name;
1009 v9fs_string_init(&old_full_name);
1010 v9fs_string_init(&new_full_name);
1012 v9fs_string_sprintf(&old_full_name, "%s/%s", olddir->data, old_name);
1013 v9fs_string_sprintf(&new_full_name, "%s/%s", newdir->data, new_name);
1015 ret = local_rename(ctx, old_full_name.data, new_full_name.data);
1016 v9fs_string_free(&old_full_name);
1017 v9fs_string_free(&new_full_name);
1018 return ret;
1021 static int local_unlinkat(FsContext *ctx, V9fsPath *dir,
1022 const char *name, int flags)
1024 int ret;
1025 V9fsString fullname;
1026 char buffer[PATH_MAX];
1028 v9fs_string_init(&fullname);
1030 v9fs_string_sprintf(&fullname, "%s/%s", dir->data, name);
1031 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1032 if (flags == AT_REMOVEDIR) {
1034 * If directory remove .virtfs_metadata contained in the
1035 * directory
1037 snprintf(buffer, ARRAY_SIZE(buffer), "%s/%s/%s", ctx->fs_root,
1038 fullname.data, VIRTFS_META_DIR);
1039 ret = remove(buffer);
1040 if (ret < 0 && errno != ENOENT) {
1042 * We didn't had the .virtfs_metadata file. May be file created
1043 * in non-mapped mode ?. Ignore ENOENT.
1045 goto err_out;
1049 * Now remove the name from parent directory
1050 * .virtfs_metadata directory.
1052 ret = remove(local_mapped_attr_path(ctx, fullname.data, buffer));
1053 if (ret < 0 && errno != ENOENT) {
1055 * We didn't had the .virtfs_metadata file. May be file created
1056 * in non-mapped mode ?. Ignore ENOENT.
1058 goto err_out;
1061 /* Remove the name finally */
1062 ret = remove(rpath(ctx, fullname.data, buffer));
1064 err_out:
1065 v9fs_string_free(&fullname);
1066 return ret;
1069 static int local_ioc_getversion(FsContext *ctx, V9fsPath *path,
1070 mode_t st_mode, uint64_t *st_gen)
1072 #ifdef FS_IOC_GETVERSION
1073 int err;
1074 V9fsFidOpenState fid_open;
1077 * Do not try to open special files like device nodes, fifos etc
1078 * We can get fd for regular files and directories only
1080 if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
1081 errno = ENOTTY;
1082 return -1;
1084 err = local_open(ctx, path, O_RDONLY, &fid_open);
1085 if (err < 0) {
1086 return err;
1088 err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
1089 local_close(ctx, &fid_open);
1090 return err;
1091 #else
1092 errno = ENOTTY;
1093 return -1;
1094 #endif
1097 static int local_init(FsContext *ctx)
1099 int err = 0;
1100 struct statfs stbuf;
1102 if (ctx->export_flags & V9FS_SM_PASSTHROUGH) {
1103 ctx->xops = passthrough_xattr_ops;
1104 } else if (ctx->export_flags & V9FS_SM_MAPPED) {
1105 ctx->xops = mapped_xattr_ops;
1106 } else if (ctx->export_flags & V9FS_SM_NONE) {
1107 ctx->xops = none_xattr_ops;
1108 } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1110 * xattr operation for mapped-file and passthrough
1111 * remain same.
1113 ctx->xops = passthrough_xattr_ops;
1115 ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT;
1116 #ifdef FS_IOC_GETVERSION
1118 * use ioc_getversion only if the iocl is definied
1120 err = statfs(ctx->fs_root, &stbuf);
1121 if (!err) {
1122 switch (stbuf.f_type) {
1123 case EXT2_SUPER_MAGIC:
1124 case BTRFS_SUPER_MAGIC:
1125 case REISERFS_SUPER_MAGIC:
1126 case XFS_SUPER_MAGIC:
1127 ctx->exops.get_st_gen = local_ioc_getversion;
1128 break;
1131 #endif
1132 return err;
1135 static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse)
1137 const char *sec_model = qemu_opt_get(opts, "security_model");
1138 const char *path = qemu_opt_get(opts, "path");
1140 if (!sec_model) {
1141 fprintf(stderr, "security model not specified, "
1142 "local fs needs security model\nvalid options are:"
1143 "\tsecurity_model=[passthrough|mapped|none]\n");
1144 return -1;
1147 if (!strcmp(sec_model, "passthrough")) {
1148 fse->export_flags |= V9FS_SM_PASSTHROUGH;
1149 } else if (!strcmp(sec_model, "mapped") ||
1150 !strcmp(sec_model, "mapped-xattr")) {
1151 fse->export_flags |= V9FS_SM_MAPPED;
1152 } else if (!strcmp(sec_model, "none")) {
1153 fse->export_flags |= V9FS_SM_NONE;
1154 } else if (!strcmp(sec_model, "mapped-file")) {
1155 fse->export_flags |= V9FS_SM_MAPPED_FILE;
1156 } else {
1157 fprintf(stderr, "Invalid security model %s specified, valid options are"
1158 "\n\t [passthrough|mapped-xattr|mapped-file|none]\n",
1159 sec_model);
1160 return -1;
1163 if (!path) {
1164 fprintf(stderr, "fsdev: No path specified.\n");
1165 return -1;
1167 fse->path = g_strdup(path);
1169 return 0;
1172 FileOperations local_ops = {
1173 .parse_opts = local_parse_opts,
1174 .init = local_init,
1175 .lstat = local_lstat,
1176 .readlink = local_readlink,
1177 .close = local_close,
1178 .closedir = local_closedir,
1179 .open = local_open,
1180 .opendir = local_opendir,
1181 .rewinddir = local_rewinddir,
1182 .telldir = local_telldir,
1183 .readdir_r = local_readdir_r,
1184 .seekdir = local_seekdir,
1185 .preadv = local_preadv,
1186 .pwritev = local_pwritev,
1187 .chmod = local_chmod,
1188 .mknod = local_mknod,
1189 .mkdir = local_mkdir,
1190 .fstat = local_fstat,
1191 .open2 = local_open2,
1192 .symlink = local_symlink,
1193 .link = local_link,
1194 .truncate = local_truncate,
1195 .rename = local_rename,
1196 .chown = local_chown,
1197 .utimensat = local_utimensat,
1198 .remove = local_remove,
1199 .fsync = local_fsync,
1200 .statfs = local_statfs,
1201 .lgetxattr = local_lgetxattr,
1202 .llistxattr = local_llistxattr,
1203 .lsetxattr = local_lsetxattr,
1204 .lremovexattr = local_lremovexattr,
1205 .name_to_path = local_name_to_path,
1206 .renameat = local_renameat,
1207 .unlinkat = local_unlinkat,