4 * Copyright IBM, Corp. 2010
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"
19 #include "fsdev/qemu-fsdev.h" /* local_ops */
20 #include <arpa/inet.h>
23 #include <sys/socket.h>
25 #include "qemu/xattr.h"
26 #include "qemu/cutils.h"
27 #include "qemu/error-report.h"
30 #ifdef CONFIG_LINUX_MAGIC_H
31 #include <linux/magic.h>
33 #include <sys/ioctl.h>
35 #ifndef XFS_SUPER_MAGIC
36 #define XFS_SUPER_MAGIC 0x58465342
38 #ifndef EXT2_SUPER_MAGIC
39 #define EXT2_SUPER_MAGIC 0xEF53
41 #ifndef REISERFS_SUPER_MAGIC
42 #define REISERFS_SUPER_MAGIC 0x52654973
44 #ifndef BTRFS_SUPER_MAGIC
45 #define BTRFS_SUPER_MAGIC 0x9123683E
52 int local_open_nofollow(FsContext
*fs_ctx
, const char *path
, int flags
,
55 LocalData
*data
= fs_ctx
->private;
57 /* All paths are relative to the path data->mountfd points to */
58 while (*path
== '/') {
62 return relative_openat_nofollow(data
->mountfd
, path
, flags
, mode
);
65 int local_opendir_nofollow(FsContext
*fs_ctx
, const char *path
)
67 return local_open_nofollow(fs_ctx
, path
, O_DIRECTORY
| O_RDONLY
, 0);
70 static void renameat_preserve_errno(int odirfd
, const char *opath
, int ndirfd
,
74 renameat(odirfd
, opath
, ndirfd
, npath
);
78 #define VIRTFS_META_DIR ".virtfs_metadata"
80 static char *local_mapped_attr_path(FsContext
*ctx
, const char *path
)
83 const char *name
= strrchr(path
, '/');
91 return g_strdup_printf("%s/%.*s/%s/%s", ctx
->fs_root
,
92 dirlen
, path
, VIRTFS_META_DIR
, name
);
95 static FILE *local_fopen(const char *path
, const char *mode
)
99 int flags
= O_NOFOLLOW
;
101 * only supports two modes
103 if (mode
[0] == 'r') {
105 } else if (mode
[0] == 'w') {
106 flags
|= O_WRONLY
| O_TRUNC
| O_CREAT
;
107 o_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
;
111 fd
= open(path
, flags
, o_mode
);
115 fp
= fdopen(fd
, mode
);
122 static FILE *local_fopenat(int dirfd
, const char *name
, const char *mode
)
128 * only supports two modes
130 if (mode
[0] == 'r') {
132 } else if (mode
[0] == 'w') {
133 flags
= O_WRONLY
| O_TRUNC
| O_CREAT
;
134 o_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
;
138 fd
= openat_file(dirfd
, name
, flags
, o_mode
);
142 fp
= fdopen(fd
, mode
);
150 static void local_mapped_file_attr(int dirfd
, const char *name
,
157 map_dirfd
= openat_dir(dirfd
, VIRTFS_META_DIR
);
158 if (map_dirfd
== -1) {
162 fp
= local_fopenat(map_dirfd
, name
, "r");
163 close_preserve_errno(map_dirfd
);
167 memset(buf
, 0, ATTR_MAX
);
168 while (fgets(buf
, ATTR_MAX
, fp
)) {
169 if (!strncmp(buf
, "virtfs.uid", 10)) {
170 stbuf
->st_uid
= atoi(buf
+11);
171 } else if (!strncmp(buf
, "virtfs.gid", 10)) {
172 stbuf
->st_gid
= atoi(buf
+11);
173 } else if (!strncmp(buf
, "virtfs.mode", 11)) {
174 stbuf
->st_mode
= atoi(buf
+12);
175 } else if (!strncmp(buf
, "virtfs.rdev", 11)) {
176 stbuf
->st_rdev
= atoi(buf
+12);
178 memset(buf
, 0, ATTR_MAX
);
183 static int local_lstat(FsContext
*fs_ctx
, V9fsPath
*fs_path
, struct stat
*stbuf
)
186 char *dirpath
= g_path_get_dirname(fs_path
->data
);
187 char *name
= g_path_get_basename(fs_path
->data
);
190 dirfd
= local_opendir_nofollow(fs_ctx
, dirpath
);
195 err
= fstatat(dirfd
, name
, stbuf
, AT_SYMLINK_NOFOLLOW
);
199 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
200 /* Actual credentials are part of extended attrs */
206 if (fgetxattrat_nofollow(dirfd
, name
, "user.virtfs.uid", &tmp_uid
,
207 sizeof(uid_t
)) > 0) {
208 stbuf
->st_uid
= le32_to_cpu(tmp_uid
);
210 if (fgetxattrat_nofollow(dirfd
, name
, "user.virtfs.gid", &tmp_gid
,
211 sizeof(gid_t
)) > 0) {
212 stbuf
->st_gid
= le32_to_cpu(tmp_gid
);
214 if (fgetxattrat_nofollow(dirfd
, name
, "user.virtfs.mode", &tmp_mode
,
215 sizeof(mode_t
)) > 0) {
216 stbuf
->st_mode
= le32_to_cpu(tmp_mode
);
218 if (fgetxattrat_nofollow(dirfd
, name
, "user.virtfs.rdev", &tmp_dev
,
219 sizeof(dev_t
)) > 0) {
220 stbuf
->st_rdev
= le64_to_cpu(tmp_dev
);
222 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
223 local_mapped_file_attr(dirfd
, name
, stbuf
);
227 close_preserve_errno(dirfd
);
234 static int local_create_mapped_attr_dir(FsContext
*ctx
, const char *path
)
238 char *tmp_path
= g_strdup(path
);
240 attr_dir
= g_strdup_printf("%s/%s/%s",
241 ctx
->fs_root
, dirname(tmp_path
), VIRTFS_META_DIR
);
243 err
= mkdir(attr_dir
, 0700);
244 if (err
< 0 && errno
== EEXIST
) {
252 static int local_set_mapped_file_attr(FsContext
*ctx
,
253 const char *path
, FsCred
*credp
)
259 int uid
= -1, gid
= -1, mode
= -1, rdev
= -1;
261 attr_path
= local_mapped_attr_path(ctx
, path
);
262 fp
= local_fopen(attr_path
, "r");
264 goto create_map_file
;
266 memset(buf
, 0, ATTR_MAX
);
267 while (fgets(buf
, ATTR_MAX
, fp
)) {
268 if (!strncmp(buf
, "virtfs.uid", 10)) {
270 } else if (!strncmp(buf
, "virtfs.gid", 10)) {
272 } else if (!strncmp(buf
, "virtfs.mode", 11)) {
274 } else if (!strncmp(buf
, "virtfs.rdev", 11)) {
277 memset(buf
, 0, ATTR_MAX
);
280 goto update_map_file
;
283 ret
= local_create_mapped_attr_dir(ctx
, path
);
289 fp
= local_fopen(attr_path
, "w");
295 if (credp
->fc_uid
!= -1) {
298 if (credp
->fc_gid
!= -1) {
301 if (credp
->fc_mode
!= -1) {
302 mode
= credp
->fc_mode
;
304 if (credp
->fc_rdev
!= -1) {
305 rdev
= credp
->fc_rdev
;
310 fprintf(fp
, "virtfs.uid=%d\n", uid
);
313 fprintf(fp
, "virtfs.gid=%d\n", gid
);
316 fprintf(fp
, "virtfs.mode=%d\n", mode
);
319 fprintf(fp
, "virtfs.rdev=%d\n", rdev
);
328 static int local_set_xattr(const char *path
, FsCred
*credp
)
332 if (credp
->fc_uid
!= -1) {
333 uint32_t tmp_uid
= cpu_to_le32(credp
->fc_uid
);
334 err
= setxattr(path
, "user.virtfs.uid", &tmp_uid
, sizeof(uid_t
), 0);
339 if (credp
->fc_gid
!= -1) {
340 uint32_t tmp_gid
= cpu_to_le32(credp
->fc_gid
);
341 err
= setxattr(path
, "user.virtfs.gid", &tmp_gid
, sizeof(gid_t
), 0);
346 if (credp
->fc_mode
!= -1) {
347 uint32_t tmp_mode
= cpu_to_le32(credp
->fc_mode
);
348 err
= setxattr(path
, "user.virtfs.mode", &tmp_mode
, sizeof(mode_t
), 0);
353 if (credp
->fc_rdev
!= -1) {
354 uint64_t tmp_rdev
= cpu_to_le64(credp
->fc_rdev
);
355 err
= setxattr(path
, "user.virtfs.rdev", &tmp_rdev
, sizeof(dev_t
), 0);
363 static int local_post_create_passthrough(FsContext
*fs_ctx
, const char *path
,
368 buffer
= rpath(fs_ctx
, path
);
369 if (lchown(buffer
, credp
->fc_uid
, credp
->fc_gid
) < 0) {
371 * If we fail to change ownership and if we are
372 * using security model none. Ignore the error
374 if ((fs_ctx
->export_flags
& V9FS_SEC_MASK
) != V9FS_SM_NONE
) {
379 if (chmod(buffer
, credp
->fc_mode
& 07777) < 0) {
390 static ssize_t
local_readlink(FsContext
*fs_ctx
, V9fsPath
*fs_path
,
391 char *buf
, size_t bufsz
)
395 if ((fs_ctx
->export_flags
& V9FS_SM_MAPPED
) ||
396 (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
)) {
399 fd
= local_open_nofollow(fs_ctx
, fs_path
->data
, O_RDONLY
, 0);
404 tsize
= read(fd
, (void *)buf
, bufsz
);
405 } while (tsize
== -1 && errno
== EINTR
);
406 close_preserve_errno(fd
);
407 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
408 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
409 char *dirpath
= g_path_get_dirname(fs_path
->data
);
410 char *name
= g_path_get_basename(fs_path
->data
);
413 dirfd
= local_opendir_nofollow(fs_ctx
, dirpath
);
418 tsize
= readlinkat(dirfd
, name
, buf
, bufsz
);
419 close_preserve_errno(dirfd
);
427 static int local_close(FsContext
*ctx
, V9fsFidOpenState
*fs
)
429 return close(fs
->fd
);
432 static int local_closedir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
434 return closedir(fs
->dir
.stream
);
437 static int local_open(FsContext
*ctx
, V9fsPath
*fs_path
,
438 int flags
, V9fsFidOpenState
*fs
)
442 fd
= local_open_nofollow(ctx
, fs_path
->data
, flags
, 0);
450 static int local_opendir(FsContext
*ctx
,
451 V9fsPath
*fs_path
, V9fsFidOpenState
*fs
)
456 dirfd
= local_opendir_nofollow(ctx
, fs_path
->data
);
461 stream
= fdopendir(dirfd
);
465 fs
->dir
.stream
= stream
;
469 static void local_rewinddir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
471 rewinddir(fs
->dir
.stream
);
474 static off_t
local_telldir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
476 return telldir(fs
->dir
.stream
);
479 static struct dirent
*local_readdir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
481 struct dirent
*entry
;
484 entry
= readdir(fs
->dir
.stream
);
489 if (ctx
->export_flags
& V9FS_SM_MAPPED
) {
490 entry
->d_type
= DT_UNKNOWN
;
491 } else if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
492 if (!strcmp(entry
->d_name
, VIRTFS_META_DIR
)) {
493 /* skp the meta data directory */
496 entry
->d_type
= DT_UNKNOWN
;
502 static void local_seekdir(FsContext
*ctx
, V9fsFidOpenState
*fs
, off_t off
)
504 seekdir(fs
->dir
.stream
, off
);
507 static ssize_t
local_preadv(FsContext
*ctx
, V9fsFidOpenState
*fs
,
508 const struct iovec
*iov
,
509 int iovcnt
, off_t offset
)
512 return preadv(fs
->fd
, iov
, iovcnt
, offset
);
514 int err
= lseek(fs
->fd
, offset
, SEEK_SET
);
518 return readv(fs
->fd
, iov
, iovcnt
);
523 static ssize_t
local_pwritev(FsContext
*ctx
, V9fsFidOpenState
*fs
,
524 const struct iovec
*iov
,
525 int iovcnt
, off_t offset
)
529 ret
= pwritev(fs
->fd
, iov
, iovcnt
, offset
);
531 int err
= lseek(fs
->fd
, offset
, SEEK_SET
);
535 ret
= writev(fs
->fd
, iov
, iovcnt
);
538 #ifdef CONFIG_SYNC_FILE_RANGE
539 if (ret
> 0 && ctx
->export_flags
& V9FS_IMMEDIATE_WRITEOUT
) {
541 * Initiate a writeback. This is not a data integrity sync.
542 * We want to ensure that we don't leave dirty pages in the cache
543 * after write when writeout=immediate is sepcified.
545 sync_file_range(fs
->fd
, offset
, ret
,
546 SYNC_FILE_RANGE_WAIT_BEFORE
| SYNC_FILE_RANGE_WRITE
);
552 static int local_chmod(FsContext
*fs_ctx
, V9fsPath
*fs_path
, FsCred
*credp
)
556 char *path
= fs_path
->data
;
558 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
559 buffer
= rpath(fs_ctx
, path
);
560 ret
= local_set_xattr(buffer
, credp
);
562 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
563 return local_set_mapped_file_attr(fs_ctx
, path
, credp
);
564 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
565 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
566 buffer
= rpath(fs_ctx
, path
);
567 ret
= chmod(buffer
, credp
->fc_mode
);
573 static int local_mknod(FsContext
*fs_ctx
, V9fsPath
*dir_path
,
574 const char *name
, FsCred
*credp
)
582 v9fs_string_init(&fullname
);
583 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
584 path
= fullname
.data
;
586 /* Determine the security model */
587 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
588 buffer
= rpath(fs_ctx
, path
);
589 err
= mknod(buffer
, SM_LOCAL_MODE_BITS
|S_IFREG
, 0);
593 err
= local_set_xattr(buffer
, credp
);
598 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
600 buffer
= rpath(fs_ctx
, path
);
601 err
= mknod(buffer
, SM_LOCAL_MODE_BITS
|S_IFREG
, 0);
605 err
= local_set_mapped_file_attr(fs_ctx
, path
, credp
);
610 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
611 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
612 buffer
= rpath(fs_ctx
, path
);
613 err
= mknod(buffer
, credp
->fc_mode
, credp
->fc_rdev
);
617 err
= local_post_create_passthrough(fs_ctx
, path
, credp
);
630 v9fs_string_free(&fullname
);
634 static int local_mkdir(FsContext
*fs_ctx
, V9fsPath
*dir_path
,
635 const char *name
, FsCred
*credp
)
643 v9fs_string_init(&fullname
);
644 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
645 path
= fullname
.data
;
647 /* Determine the security model */
648 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
649 buffer
= rpath(fs_ctx
, path
);
650 err
= mkdir(buffer
, SM_LOCAL_DIR_MODE_BITS
);
654 credp
->fc_mode
= credp
->fc_mode
|S_IFDIR
;
655 err
= local_set_xattr(buffer
, credp
);
660 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
661 buffer
= rpath(fs_ctx
, path
);
662 err
= mkdir(buffer
, SM_LOCAL_DIR_MODE_BITS
);
666 credp
->fc_mode
= credp
->fc_mode
|S_IFDIR
;
667 err
= local_set_mapped_file_attr(fs_ctx
, path
, credp
);
672 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
673 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
674 buffer
= rpath(fs_ctx
, path
);
675 err
= mkdir(buffer
, credp
->fc_mode
);
679 err
= local_post_create_passthrough(fs_ctx
, path
, credp
);
692 v9fs_string_free(&fullname
);
696 static int local_fstat(FsContext
*fs_ctx
, int fid_type
,
697 V9fsFidOpenState
*fs
, struct stat
*stbuf
)
701 if (fid_type
== P9_FID_DIR
) {
702 fd
= dirfd(fs
->dir
.stream
);
707 err
= fstat(fd
, stbuf
);
711 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
712 /* Actual credentials are part of extended attrs */
718 if (fgetxattr(fd
, "user.virtfs.uid", &tmp_uid
, sizeof(uid_t
)) > 0) {
719 stbuf
->st_uid
= le32_to_cpu(tmp_uid
);
721 if (fgetxattr(fd
, "user.virtfs.gid", &tmp_gid
, sizeof(gid_t
)) > 0) {
722 stbuf
->st_gid
= le32_to_cpu(tmp_gid
);
724 if (fgetxattr(fd
, "user.virtfs.mode", &tmp_mode
, sizeof(mode_t
)) > 0) {
725 stbuf
->st_mode
= le32_to_cpu(tmp_mode
);
727 if (fgetxattr(fd
, "user.virtfs.rdev", &tmp_dev
, sizeof(dev_t
)) > 0) {
728 stbuf
->st_rdev
= le64_to_cpu(tmp_dev
);
730 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
737 static int local_open2(FsContext
*fs_ctx
, V9fsPath
*dir_path
, const char *name
,
738 int flags
, FsCred
*credp
, V9fsFidOpenState
*fs
)
748 * Mark all the open to not follow symlinks
752 v9fs_string_init(&fullname
);
753 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
754 path
= fullname
.data
;
756 /* Determine the security model */
757 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
758 buffer
= rpath(fs_ctx
, path
);
759 fd
= open(buffer
, flags
, SM_LOCAL_MODE_BITS
);
764 credp
->fc_mode
= credp
->fc_mode
|S_IFREG
;
765 /* Set cleint credentials in xattr */
766 err
= local_set_xattr(buffer
, credp
);
771 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
772 buffer
= rpath(fs_ctx
, path
);
773 fd
= open(buffer
, flags
, SM_LOCAL_MODE_BITS
);
778 credp
->fc_mode
= credp
->fc_mode
|S_IFREG
;
779 /* Set client credentials in .virtfs_metadata directory files */
780 err
= local_set_mapped_file_attr(fs_ctx
, path
, credp
);
785 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
786 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
787 buffer
= rpath(fs_ctx
, path
);
788 fd
= open(buffer
, flags
, credp
->fc_mode
);
793 err
= local_post_create_passthrough(fs_ctx
, path
, credp
);
809 v9fs_string_free(&fullname
);
814 static int local_symlink(FsContext
*fs_ctx
, const char *oldpath
,
815 V9fsPath
*dir_path
, const char *name
, FsCred
*credp
)
823 v9fs_string_init(&fullname
);
824 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
825 newpath
= fullname
.data
;
827 /* Determine the security model */
828 if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
830 ssize_t oldpath_size
, write_size
;
831 buffer
= rpath(fs_ctx
, newpath
);
832 fd
= open(buffer
, O_CREAT
|O_EXCL
|O_RDWR
|O_NOFOLLOW
, SM_LOCAL_MODE_BITS
);
837 /* Write the oldpath (target) to the file. */
838 oldpath_size
= strlen(oldpath
);
840 write_size
= write(fd
, (void *)oldpath
, oldpath_size
);
841 } while (write_size
== -1 && errno
== EINTR
);
843 if (write_size
!= oldpath_size
) {
850 /* Set cleint credentials in symlink's xattr */
851 credp
->fc_mode
= credp
->fc_mode
|S_IFLNK
;
852 err
= local_set_xattr(buffer
, credp
);
857 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
859 ssize_t oldpath_size
, write_size
;
860 buffer
= rpath(fs_ctx
, newpath
);
861 fd
= open(buffer
, O_CREAT
|O_EXCL
|O_RDWR
|O_NOFOLLOW
, SM_LOCAL_MODE_BITS
);
866 /* Write the oldpath (target) to the file. */
867 oldpath_size
= strlen(oldpath
);
869 write_size
= write(fd
, (void *)oldpath
, oldpath_size
);
870 } while (write_size
== -1 && errno
== EINTR
);
872 if (write_size
!= oldpath_size
) {
879 /* Set cleint credentials in symlink's xattr */
880 credp
->fc_mode
= credp
->fc_mode
|S_IFLNK
;
881 err
= local_set_mapped_file_attr(fs_ctx
, newpath
, credp
);
886 } else if ((fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
887 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
888 buffer
= rpath(fs_ctx
, newpath
);
889 err
= symlink(oldpath
, buffer
);
893 err
= lchown(buffer
, credp
->fc_uid
, credp
->fc_gid
);
896 * If we fail to change ownership and if we are
897 * using security model none. Ignore the error
899 if ((fs_ctx
->export_flags
& V9FS_SEC_MASK
) != V9FS_SM_NONE
) {
913 v9fs_string_free(&fullname
);
917 static int local_link(FsContext
*ctx
, V9fsPath
*oldpath
,
918 V9fsPath
*dirpath
, const char *name
)
922 char *buffer
, *buffer1
;
925 v9fs_string_init(&newpath
);
926 v9fs_string_sprintf(&newpath
, "%s/%s", dirpath
->data
, name
);
928 buffer
= rpath(ctx
, oldpath
->data
);
929 buffer1
= rpath(ctx
, newpath
.data
);
930 ret
= link(buffer
, buffer1
);
936 /* now link the virtfs_metadata files */
937 if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
938 char *vbuffer
, *vbuffer1
;
940 /* Link the .virtfs_metadata files. Create the metada directory */
941 ret
= local_create_mapped_attr_dir(ctx
, newpath
.data
);
945 vbuffer
= local_mapped_attr_path(ctx
, oldpath
->data
);
946 vbuffer1
= local_mapped_attr_path(ctx
, newpath
.data
);
947 ret
= link(vbuffer
, vbuffer1
);
950 if (ret
< 0 && errno
!= ENOENT
) {
962 v9fs_string_free(&newpath
);
966 static int local_truncate(FsContext
*ctx
, V9fsPath
*fs_path
, off_t size
)
970 fd
= local_open_nofollow(ctx
, fs_path
->data
, O_WRONLY
, 0);
974 ret
= ftruncate(fd
, size
);
975 close_preserve_errno(fd
);
979 static int local_chown(FsContext
*fs_ctx
, V9fsPath
*fs_path
, FsCred
*credp
)
983 char *path
= fs_path
->data
;
985 if ((credp
->fc_uid
== -1 && credp
->fc_gid
== -1) ||
986 (fs_ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) ||
987 (fs_ctx
->export_flags
& V9FS_SM_NONE
)) {
988 buffer
= rpath(fs_ctx
, path
);
989 ret
= lchown(buffer
, credp
->fc_uid
, credp
->fc_gid
);
991 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED
) {
992 buffer
= rpath(fs_ctx
, path
);
993 ret
= local_set_xattr(buffer
, credp
);
995 } else if (fs_ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
996 return local_set_mapped_file_attr(fs_ctx
, path
, credp
);
1001 static int local_utimensat(FsContext
*s
, V9fsPath
*fs_path
,
1002 const struct timespec
*buf
)
1004 char *dirpath
= g_path_get_dirname(fs_path
->data
);
1005 char *name
= g_path_get_basename(fs_path
->data
);
1006 int dirfd
, ret
= -1;
1008 dirfd
= local_opendir_nofollow(s
, dirpath
);
1013 ret
= utimensat(dirfd
, name
, buf
, AT_SYMLINK_NOFOLLOW
);
1014 close_preserve_errno(dirfd
);
1021 static int local_unlinkat_common(FsContext
*ctx
, int dirfd
, const char *name
,
1026 if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
1029 if (flags
== AT_REMOVEDIR
) {
1032 fd
= openat(dirfd
, name
, O_RDONLY
| O_DIRECTORY
| O_PATH
);
1037 * If directory remove .virtfs_metadata contained in the
1040 ret
= unlinkat(fd
, VIRTFS_META_DIR
, AT_REMOVEDIR
);
1041 close_preserve_errno(fd
);
1042 if (ret
< 0 && errno
!= ENOENT
) {
1044 * We didn't had the .virtfs_metadata file. May be file created
1045 * in non-mapped mode ?. Ignore ENOENT.
1051 * Now remove the name from parent directory
1052 * .virtfs_metadata directory.
1054 map_dirfd
= openat_dir(dirfd
, VIRTFS_META_DIR
);
1055 ret
= unlinkat(map_dirfd
, name
, 0);
1056 close_preserve_errno(map_dirfd
);
1057 if (ret
< 0 && errno
!= ENOENT
) {
1059 * We didn't had the .virtfs_metadata file. May be file created
1060 * in non-mapped mode ?. Ignore ENOENT.
1066 ret
= unlinkat(dirfd
, name
, flags
);
1071 static int local_remove(FsContext
*ctx
, const char *path
)
1074 char *dirpath
= g_path_get_dirname(path
);
1075 char *name
= g_path_get_basename(path
);
1080 dirfd
= local_opendir_nofollow(ctx
, dirpath
);
1085 if (fstatat(dirfd
, path
, &stbuf
, AT_SYMLINK_NOFOLLOW
) < 0) {
1089 if (S_ISDIR(stbuf
.st_mode
)) {
1090 flags
|= AT_REMOVEDIR
;
1093 err
= local_unlinkat_common(ctx
, dirfd
, name
, flags
);
1095 close_preserve_errno(dirfd
);
1102 static int local_fsync(FsContext
*ctx
, int fid_type
,
1103 V9fsFidOpenState
*fs
, int datasync
)
1107 if (fid_type
== P9_FID_DIR
) {
1108 fd
= dirfd(fs
->dir
.stream
);
1114 return qemu_fdatasync(fd
);
1120 static int local_statfs(FsContext
*s
, V9fsPath
*fs_path
, struct statfs
*stbuf
)
1124 fd
= local_open_nofollow(s
, fs_path
->data
, O_RDONLY
, 0);
1125 ret
= fstatfs(fd
, stbuf
);
1126 close_preserve_errno(fd
);
1130 static ssize_t
local_lgetxattr(FsContext
*ctx
, V9fsPath
*fs_path
,
1131 const char *name
, void *value
, size_t size
)
1133 char *path
= fs_path
->data
;
1135 return v9fs_get_xattr(ctx
, path
, name
, value
, size
);
1138 static ssize_t
local_llistxattr(FsContext
*ctx
, V9fsPath
*fs_path
,
1139 void *value
, size_t size
)
1141 char *path
= fs_path
->data
;
1143 return v9fs_list_xattr(ctx
, path
, value
, size
);
1146 static int local_lsetxattr(FsContext
*ctx
, V9fsPath
*fs_path
, const char *name
,
1147 void *value
, size_t size
, int flags
)
1149 char *path
= fs_path
->data
;
1151 return v9fs_set_xattr(ctx
, path
, name
, value
, size
, flags
);
1154 static int local_lremovexattr(FsContext
*ctx
, V9fsPath
*fs_path
,
1157 char *path
= fs_path
->data
;
1159 return v9fs_remove_xattr(ctx
, path
, name
);
1162 static int local_name_to_path(FsContext
*ctx
, V9fsPath
*dir_path
,
1163 const char *name
, V9fsPath
*target
)
1166 v9fs_path_sprintf(target
, "%s/%s", dir_path
->data
, name
);
1168 v9fs_path_sprintf(target
, "%s", name
);
1173 static int local_renameat(FsContext
*ctx
, V9fsPath
*olddir
,
1174 const char *old_name
, V9fsPath
*newdir
,
1175 const char *new_name
)
1180 odirfd
= local_opendir_nofollow(ctx
, olddir
->data
);
1185 ndirfd
= local_opendir_nofollow(ctx
, newdir
->data
);
1187 close_preserve_errno(odirfd
);
1191 ret
= renameat(odirfd
, old_name
, ndirfd
, new_name
);
1196 if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
1197 int omap_dirfd
, nmap_dirfd
;
1199 ret
= mkdirat(ndirfd
, VIRTFS_META_DIR
, 0700);
1200 if (ret
< 0 && errno
!= EEXIST
) {
1201 goto err_undo_rename
;
1204 omap_dirfd
= openat_dir(odirfd
, VIRTFS_META_DIR
);
1205 if (omap_dirfd
== -1) {
1209 nmap_dirfd
= openat_dir(ndirfd
, VIRTFS_META_DIR
);
1210 if (nmap_dirfd
== -1) {
1211 close_preserve_errno(omap_dirfd
);
1215 /* rename the .virtfs_metadata files */
1216 ret
= renameat(omap_dirfd
, old_name
, nmap_dirfd
, new_name
);
1217 close_preserve_errno(nmap_dirfd
);
1218 close_preserve_errno(omap_dirfd
);
1219 if (ret
< 0 && errno
!= ENOENT
) {
1220 goto err_undo_rename
;
1230 renameat_preserve_errno(ndirfd
, new_name
, odirfd
, old_name
);
1232 close_preserve_errno(ndirfd
);
1233 close_preserve_errno(odirfd
);
1237 static void v9fs_path_init_dirname(V9fsPath
*path
, const char *str
)
1239 path
->data
= g_path_get_dirname(str
);
1240 path
->size
= strlen(path
->data
) + 1;
1243 static int local_rename(FsContext
*ctx
, const char *oldpath
,
1244 const char *newpath
)
1247 char *oname
= g_path_get_basename(oldpath
);
1248 char *nname
= g_path_get_basename(newpath
);
1249 V9fsPath olddir
, newdir
;
1251 v9fs_path_init_dirname(&olddir
, oldpath
);
1252 v9fs_path_init_dirname(&newdir
, newpath
);
1254 err
= local_renameat(ctx
, &olddir
, oname
, &newdir
, nname
);
1256 v9fs_path_free(&newdir
);
1257 v9fs_path_free(&olddir
);
1264 static int local_unlinkat(FsContext
*ctx
, V9fsPath
*dir
,
1265 const char *name
, int flags
)
1270 dirfd
= local_opendir_nofollow(ctx
, dir
->data
);
1275 ret
= local_unlinkat_common(ctx
, dirfd
, name
, flags
);
1276 close_preserve_errno(dirfd
);
1280 static int local_ioc_getversion(FsContext
*ctx
, V9fsPath
*path
,
1281 mode_t st_mode
, uint64_t *st_gen
)
1283 #ifdef FS_IOC_GETVERSION
1285 V9fsFidOpenState fid_open
;
1288 * Do not try to open special files like device nodes, fifos etc
1289 * We can get fd for regular files and directories only
1291 if (!S_ISREG(st_mode
) && !S_ISDIR(st_mode
)) {
1295 err
= local_open(ctx
, path
, O_RDONLY
, &fid_open
);
1299 err
= ioctl(fid_open
.fd
, FS_IOC_GETVERSION
, st_gen
);
1300 local_close(ctx
, &fid_open
);
1308 static int local_init(FsContext
*ctx
)
1310 struct statfs stbuf
;
1311 LocalData
*data
= g_malloc(sizeof(*data
));
1313 data
->mountfd
= open(ctx
->fs_root
, O_DIRECTORY
| O_RDONLY
);
1314 if (data
->mountfd
== -1) {
1318 #ifdef FS_IOC_GETVERSION
1320 * use ioc_getversion only if the ioctl is definied
1322 if (fstatfs(data
->mountfd
, &stbuf
) < 0) {
1323 close_preserve_errno(data
->mountfd
);
1326 switch (stbuf
.f_type
) {
1327 case EXT2_SUPER_MAGIC
:
1328 case BTRFS_SUPER_MAGIC
:
1329 case REISERFS_SUPER_MAGIC
:
1330 case XFS_SUPER_MAGIC
:
1331 ctx
->exops
.get_st_gen
= local_ioc_getversion
;
1336 if (ctx
->export_flags
& V9FS_SM_PASSTHROUGH
) {
1337 ctx
->xops
= passthrough_xattr_ops
;
1338 } else if (ctx
->export_flags
& V9FS_SM_MAPPED
) {
1339 ctx
->xops
= mapped_xattr_ops
;
1340 } else if (ctx
->export_flags
& V9FS_SM_NONE
) {
1341 ctx
->xops
= none_xattr_ops
;
1342 } else if (ctx
->export_flags
& V9FS_SM_MAPPED_FILE
) {
1344 * xattr operation for mapped-file and passthrough
1347 ctx
->xops
= passthrough_xattr_ops
;
1349 ctx
->export_flags
|= V9FS_PATHNAME_FSCONTEXT
;
1351 ctx
->private = data
;
1359 static void local_cleanup(FsContext
*ctx
)
1361 LocalData
*data
= ctx
->private;
1363 close(data
->mountfd
);
1367 static int local_parse_opts(QemuOpts
*opts
, struct FsDriverEntry
*fse
)
1369 const char *sec_model
= qemu_opt_get(opts
, "security_model");
1370 const char *path
= qemu_opt_get(opts
, "path");
1373 error_report("Security model not specified, local fs needs security model");
1374 error_printf("valid options are:"
1375 "\tsecurity_model=[passthrough|mapped-xattr|mapped-file|none]\n");
1379 if (!strcmp(sec_model
, "passthrough")) {
1380 fse
->export_flags
|= V9FS_SM_PASSTHROUGH
;
1381 } else if (!strcmp(sec_model
, "mapped") ||
1382 !strcmp(sec_model
, "mapped-xattr")) {
1383 fse
->export_flags
|= V9FS_SM_MAPPED
;
1384 } else if (!strcmp(sec_model
, "none")) {
1385 fse
->export_flags
|= V9FS_SM_NONE
;
1386 } else if (!strcmp(sec_model
, "mapped-file")) {
1387 fse
->export_flags
|= V9FS_SM_MAPPED_FILE
;
1389 error_report("Invalid security model %s specified", sec_model
);
1390 error_printf("valid options are:"
1391 "\t[passthrough|mapped-xattr|mapped-file|none]\n");
1396 error_report("fsdev: No path specified");
1399 fse
->path
= g_strdup(path
);
1404 FileOperations local_ops
= {
1405 .parse_opts
= local_parse_opts
,
1407 .cleanup
= local_cleanup
,
1408 .lstat
= local_lstat
,
1409 .readlink
= local_readlink
,
1410 .close
= local_close
,
1411 .closedir
= local_closedir
,
1413 .opendir
= local_opendir
,
1414 .rewinddir
= local_rewinddir
,
1415 .telldir
= local_telldir
,
1416 .readdir
= local_readdir
,
1417 .seekdir
= local_seekdir
,
1418 .preadv
= local_preadv
,
1419 .pwritev
= local_pwritev
,
1420 .chmod
= local_chmod
,
1421 .mknod
= local_mknod
,
1422 .mkdir
= local_mkdir
,
1423 .fstat
= local_fstat
,
1424 .open2
= local_open2
,
1425 .symlink
= local_symlink
,
1427 .truncate
= local_truncate
,
1428 .rename
= local_rename
,
1429 .chown
= local_chown
,
1430 .utimensat
= local_utimensat
,
1431 .remove
= local_remove
,
1432 .fsync
= local_fsync
,
1433 .statfs
= local_statfs
,
1434 .lgetxattr
= local_lgetxattr
,
1435 .llistxattr
= local_llistxattr
,
1436 .lsetxattr
= local_lsetxattr
,
1437 .lremovexattr
= local_lremovexattr
,
1438 .name_to_path
= local_name_to_path
,
1439 .renameat
= local_renameat
,
1440 .unlinkat
= local_unlinkat
,