2 * Virtio 9p backend for Maru
3 * Based on hw/9pfs/virtio-9p-local.c:
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
8 * Sooyoung Ha <yoosah.ha@samsung.com>
9 * YeongKyoon Lee <yeongkyoon.lee@samsung.com>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 #include "hw/virtio/virtio.h"
31 #include "virtio-9p.h"
33 #include "virtio-9p-xattr.h"
34 #include <arpa/inet.h>
37 #include <sys/socket.h>
39 #include "qemu/xattr.h"
45 #ifdef CONFIG_LINUX_MAGIC_H
46 #include <linux/magic.h>
49 #include <sys/ioctl.h>
52 #include "../../tizen/src/debug_ch.h"
53 MULTI_DEBUG_CHANNEL(tizen, qemu_9p_local);
55 #ifndef XFS_SUPER_MAGIC
56 #define XFS_SUPER_MAGIC 0x58465342
58 #ifndef EXT2_SUPER_MAGIC
59 #define EXT2_SUPER_MAGIC 0xEF53
61 #ifndef REISERFS_SUPER_MAGIC
62 #define REISERFS_SUPER_MAGIC 0x52654973
64 #ifndef BTRFS_SUPER_MAGIC
65 #define BTRFS_SUPER_MAGIC 0x9123683E
69 #ifndef MSDOS_SUPER_MAGIC
70 #define MSDOS_SUPER_MAGIC 0x4d44
72 #ifndef NTFS_SUPER_MAGIC
73 #define NTFS_SUPER_MAGIC 0x5346544e
76 uint64_t hostBytesPerSector = -1;
79 #define VIRTFS_META_DIR ".virtfs_metadata"
82 #define AT_REMOVEDIR 0x200
85 static char *local_mapped_attr_path(FsContext *ctx, const char *path)
88 char *tmp_path = g_strdup(path);
89 char *base_name = basename(tmp_path);
92 /* NULL terminate the directory */
94 *(base_name - 1) = '\0';
96 buffer = g_strdup_printf("%s/%s/%s/%s",
97 ctx->fs_root, dir_name, VIRTFS_META_DIR, base_name);
99 buffer = g_strdup_printf("%s\\%s\\%s\\%s",
100 ctx->fs_root, dir_name, VIRTFS_META_DIR, base_name);
101 while(buffer[strlen(buffer)-1] == '\\'){
102 buffer[strlen(buffer)-1] = '\0';
109 static FILE *local_fopen(const char *path, const char *mode)
111 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
117 int flags = O_NOFOLLOW;
119 * only supports two modes
121 if (mode[0] == 'r') {
123 } else if (mode[0] == 'w') {
124 flags |= O_WRONLY | O_TRUNC | O_CREAT;
126 o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
132 fd = open(path, flags, o_mode);
134 fd = open(path, flags | O_BINARY);
139 fp = fdopen(fd, mode);
148 static void local_mapped_file_attr(FsContext *ctx, const char *path,
151 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
156 attr_path = local_mapped_attr_path(ctx, path);
157 fp = local_fopen(attr_path, "r");
162 memset(buf, 0, ATTR_MAX);
163 while (fgets(buf, ATTR_MAX, fp)) {
164 if (!strncmp(buf, "virtfs.uid", 10)) {
165 stbuf->st_uid = atoi(buf+11);
166 } else if (!strncmp(buf, "virtfs.gid", 10)) {
167 stbuf->st_gid = atoi(buf+11);
168 } else if (!strncmp(buf, "virtfs.mode", 11)) {
169 stbuf->st_mode = atoi(buf+12);
170 } else if (!strncmp(buf, "virtfs.rdev", 11)) {
171 stbuf->st_rdev = atoi(buf+12);
173 memset(buf, 0, ATTR_MAX);
179 static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf)
181 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
184 char *path = fs_path->data;
185 buffer = rpath(fs_ctx, path);
188 err = lstat(buffer, stbuf);
192 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
193 /* Actual credentials are part of extended attrs */
199 if (getxattr(buffer, "user.virtfs.uid", &tmp_uid,
200 sizeof(uid_t)) > 0) {
201 stbuf->st_uid = tmp_uid;
203 if (getxattr(buffer, "user.virtfs.gid", &tmp_gid,
204 sizeof(gid_t)) > 0) {
205 stbuf->st_gid = tmp_gid;
207 if (getxattr(buffer, "user.virtfs.mode",
208 &tmp_mode, sizeof(mode_t)) > 0) {
209 stbuf->st_mode = tmp_mode;
211 if (getxattr(buffer, "user.virtfs.rdev", &tmp_dev,
212 sizeof(dev_t)) > 0) {
213 stbuf->st_rdev = tmp_dev;
217 * extra two parameters on Mac OS X.
218 * first one is position which specifies an offset within the extended attribute.
219 * i.e. extended attribute means "user.virtfs.uid". Currently, it is only used with
220 * resource fork attribute and all other extended attributes, it is reserved and should be zero.
221 * second one is options which specify option for retrieving extended attributes:
222 * (XATTR_NOFOLLOW, XATTR_SHOWCOMPRESSION)
224 if (getxattr(buffer, "user.virtfs.uid", &tmp_uid,
225 sizeof(uid_t), 0, 0) > 0) {
226 stbuf->st_uid = tmp_uid;
228 if (getxattr(buffer, "user.virtfs.gid", &tmp_gid,
229 sizeof(gid_t), 0, 0) > 0) {
230 stbuf->st_gid = tmp_gid;
232 if (getxattr(buffer, "user.virtfs.mode",
233 &tmp_mode, sizeof(mode_t), 0, 0) > 0) {
234 stbuf->st_mode = tmp_mode;
236 if (getxattr(buffer, "user.virtfs.rdev", &tmp_dev,
237 sizeof(dev_t), 0, 0) > 0) {
238 stbuf->st_rdev = tmp_dev;
241 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
242 local_mapped_file_attr(fs_ctx, path, stbuf);
245 const char * pathname = buffer;
246 if (hostBytesPerSector == -1) {
247 DWORD BytesPerSector;
248 TCHAR tmpName[PATH_MAX] = {0};
250 strncpy(tmpName, pathname, 3 > PATH_MAX ? PATH_MAX : 3);
251 tmpName[4 > PATH_MAX ? PATH_MAX : 4] = '\0';
252 LPCTSTR RootPathName = (LPCTSTR)tmpName;
254 GetDiskFreeSpace(RootPathName, NULL, &BytesPerSector, NULL, NULL);
255 hostBytesPerSector = BytesPerSector;
257 err = _stat(pathname, stbuf);
259 /* Modify the permission to 777 except the directories. */
260 stbuf->st_mode = stbuf->st_mode | 0777;
266 static int local_create_mapped_attr_dir(FsContext *ctx, const char *path)
268 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
270 char attr_dir[PATH_MAX];
271 char *tmp_path = g_strdup(path);
273 snprintf(attr_dir, PATH_MAX, "%s/%s/%s",
274 ctx->fs_root, dirname(tmp_path), VIRTFS_META_DIR);
275 err = mkdir(attr_dir, 0700);
277 snprintf(attr_dir, PATH_MAX, "%s\\%s\\%s",
278 ctx->fs_root, dirname(tmp_path), VIRTFS_META_DIR);
279 err = mkdir(attr_dir);
282 if (err < 0 && errno == EEXIST) {
289 static int local_set_mapped_file_attr(FsContext *ctx,
290 const char *path, FsCred *credp)
292 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
297 int uid = -1, gid = -1, mode = -1, rdev = -1;
299 attr_path = local_mapped_attr_path(ctx, path);
300 fp = local_fopen(attr_path, "r");
303 goto create_map_file;
305 memset(buf, 0, ATTR_MAX);
306 while (fgets(buf, ATTR_MAX, fp)) {
307 if (!strncmp(buf, "virtfs.uid", 10)) {
309 } else if (!strncmp(buf, "virtfs.gid", 10)) {
311 } else if (!strncmp(buf, "virtfs.mode", 11)) {
313 } else if (!strncmp(buf, "virtfs.rdev", 11)) {
316 memset(buf, 0, ATTR_MAX);
319 goto update_map_file;
322 ret = local_create_mapped_attr_dir(ctx, path);
328 fp = local_fopen(attr_path, "w");
334 if (credp->fc_uid != -1) {
337 if (credp->fc_gid != -1) {
340 if (credp->fc_mode != -1) {
341 mode = credp->fc_mode;
343 if (credp->fc_rdev != -1) {
344 rdev = credp->fc_rdev;
348 fprintf(fp, "virtfs.uid=%d\n", uid);
351 fprintf(fp, "virtfs.gid=%d\n", gid);
354 fprintf(fp, "virtfs.mode=%d\n", mode);
357 fprintf(fp, "virtfs.rdev=%d\n", rdev);
366 static int local_set_xattr(const char *path, FsCred *credp)
371 if (credp->fc_uid != -1) {
372 err = setxattr(path, "user.virtfs.uid", &credp->fc_uid, sizeof(uid_t),
378 if (credp->fc_gid != -1) {
379 err = setxattr(path, "user.virtfs.gid", &credp->fc_gid, sizeof(gid_t),
385 if (credp->fc_mode != -1) {
386 err = setxattr(path, "user.virtfs.mode", &credp->fc_mode,
392 if (credp->fc_rdev != -1) {
393 err = setxattr(path, "user.virtfs.rdev", &credp->fc_rdev,
401 * In case of setxattr on OS X, position parameter has been added.
402 * Its purpose is the same as getxattr. Last parameter options is the same as flags on Linux.
403 * XATTR_NOFOLLOW / XATTR_CREATE / XATTR_REPLACE
405 if (credp->fc_uid != -1) {
406 err = setxattr(path, "user.virtfs.uid", &credp->fc_uid, sizeof(uid_t),
412 if (credp->fc_gid != -1) {
413 err = setxattr(path, "user.virtfs.gid", &credp->fc_gid, sizeof(gid_t),
419 if (credp->fc_mode != -1) {
420 err = setxattr(path, "user.virtfs.mode", &credp->fc_mode,
421 sizeof(mode_t), 0, 0);
426 if (credp->fc_rdev != -1) {
427 err = setxattr(path, "user.virtfs.rdev", &credp->fc_rdev,
428 sizeof(dev_t), 0, 0);
439 static int local_post_create_passthrough(FsContext *fs_ctx, const char *path,
443 char *buffer = rpath(fs_ctx, path);
445 if (lchown(buffer, credp->fc_uid,
446 credp->fc_gid) < 0) {
448 * If we fail to change ownership and if we are
449 * using security model none. Ignore the error
451 if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
457 if (chmod(buffer, credp->fc_mode & 07777) < 0) {
465 static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
466 char *buf, size_t bufsz)
468 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
472 char *path = fs_path->data;
473 buffer = rpath(fs_ctx, path);
475 if ((fs_ctx->export_flags & V9FS_SM_MAPPED) ||
476 (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
478 fd = open(buffer, O_RDONLY | O_NOFOLLOW);
484 tsize = read(fd, (void *)buf, bufsz);
485 } while (tsize == -1 && errno == EINTR);
488 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
489 (fs_ctx->export_flags & V9FS_SM_NONE)) {
490 tsize = readlink(buffer, buf, bufsz);
497 static int local_close(FsContext *ctx, V9fsFidOpenState *fs)
499 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
500 return close(fs->fd);
503 static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs)
505 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
506 return closedir(fs->dir);
509 static int local_open(FsContext *ctx, V9fsPath *fs_path,
510 int flags, V9fsFidOpenState *fs)
512 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
514 char *path = fs_path->data;
515 buffer = rpath(ctx, path);
520 fs->fd = open(buffer, flags | O_NOFOLLOW);
526 static int local_opendir(FsContext *ctx,
527 V9fsPath *fs_path, V9fsFidOpenState *fs)
529 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
531 char *path = fs_path->data;
532 buffer = rpath(ctx, path);
534 fs->dir = opendir(buffer);
542 static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
544 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
545 return rewinddir(fs->dir);
548 static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs)
550 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
551 return telldir(fs->dir);
554 static int local_readdir_r(FsContext *ctx, V9fsFidOpenState *fs,
555 struct dirent *entry,
556 struct dirent **result)
558 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
563 ret = readdir_r(fs->dir, entry, result);
564 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
565 if (!ret && *result != NULL &&
566 !strcmp(entry->d_name, VIRTFS_META_DIR)) {
567 /* skp the meta data directory */
573 *result = readdir(fs->dir);
574 ret = (ret == errno ? 0 : errno);
576 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
577 if (!ret && *result != NULL &&
578 !strcmp(entry->d_name, VIRTFS_META_DIR)) {
579 /* skp the meta data directory */
587 static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
589 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
590 return seekdir(fs->dir, off);
593 static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs,
594 const struct iovec *iov,
595 int iovcnt, off_t offset)
598 return preadv(fs->fd, iov, iovcnt, offset);
600 int err = lseek(fs->fd, offset, SEEK_SET);
604 return readv(fs->fd, iov, iovcnt);
609 static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
610 const struct iovec *iov,
611 int iovcnt, off_t offset)
616 ret = pwritev(fs->fd, iov, iovcnt, offset);
618 int err = lseek(fs->fd, offset, SEEK_SET);
622 ret = writev(fs->fd, iov, iovcnt);
625 #ifdef CONFIG_SYNC_FILE_RANGE
626 if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
628 * Initiate a writeback. This is not a data integrity sync.
629 * We want to ensure that we don't leave dirty pages in the cache
630 * after write when writeout=immediate is sepcified.
632 sync_file_range(fs->fd, offset, ret,
633 SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
639 static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
641 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
645 char *path = fs_path->data;
646 buffer = rpath(fs_ctx, path);
648 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
649 ret = local_set_xattr(buffer, credp);
650 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
652 return local_set_mapped_file_attr(fs_ctx, path, credp);
653 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
654 (fs_ctx->export_flags & V9FS_SM_NONE)) {
655 ret = chmod(buffer, credp->fc_mode);
662 static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
663 const char *name, FsCred *credp)
665 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
673 v9fs_string_init(&fullname);
674 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
675 path = fullname.data;
676 buffer = rpath(fs_ctx, path);
678 /* Determine the security model */
679 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
681 SM_LOCAL_MODE_BITS|S_IFREG, 0);
685 err = local_set_xattr(buffer, credp);
690 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
693 SM_LOCAL_MODE_BITS|S_IFREG, 0);
697 err = local_set_mapped_file_attr(fs_ctx, path, credp);
702 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
703 (fs_ctx->export_flags & V9FS_SM_NONE)) {
704 err = mknod(buffer, credp->fc_mode,
709 err = local_post_create_passthrough(fs_ctx, path, credp);
722 v9fs_string_free(&fullname);
727 static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
728 const char *name, FsCred *credp)
730 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
737 v9fs_string_init(&fullname);
739 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
741 v9fs_string_sprintf(&fullname, "%s\\%s", dir_path->data, name);
742 while((fullname.data)[strlen(fullname.data)-1] == '\\'){
743 (fullname.data)[strlen(fullname.data)-1] = '\0';
746 path = fullname.data;
747 buffer = rpath(fs_ctx, path);
750 /* Determine the security model */
751 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
752 err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
756 credp->fc_mode = credp->fc_mode|S_IFDIR;
757 err = local_set_xattr(buffer, credp);
762 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
763 err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
767 credp->fc_mode = credp->fc_mode|S_IFDIR;
768 err = local_set_mapped_file_attr(fs_ctx, path, credp);
773 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
774 (fs_ctx->export_flags & V9FS_SM_NONE)) {
775 err = mkdir(buffer, credp->fc_mode);
779 err = local_post_create_passthrough(fs_ctx, path, credp);
795 v9fs_string_free(&fullname);
799 static int local_fstat(FsContext *fs_ctx, int fid_type,
800 V9fsFidOpenState *fs, struct stat *stbuf)
802 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
805 if (fid_type == P9_FID_DIR) {
815 err = fstat(fd, stbuf);
819 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
821 /* Actual credentials are part of extended attrs */
828 if (fgetxattr(fd, "user.virtfs.uid",
829 &tmp_uid, sizeof(uid_t)) > 0) {
830 stbuf->st_uid = tmp_uid;
832 if (fgetxattr(fd, "user.virtfs.gid",
833 &tmp_gid, sizeof(gid_t)) > 0) {
834 stbuf->st_gid = tmp_gid;
836 if (fgetxattr(fd, "user.virtfs.mode",
837 &tmp_mode, sizeof(mode_t)) > 0) {
838 stbuf->st_mode = tmp_mode;
840 if (fgetxattr(fd, "user.virtfs.rdev",
841 &tmp_dev, sizeof(dev_t)) > 0) {
842 stbuf->st_rdev = tmp_dev;
845 if (fgetxattr(fd, "user.virtfs.uid",
846 &tmp_uid, sizeof(uid_t), 0, 0) > 0) {
847 stbuf->st_uid = tmp_uid;
849 if (fgetxattr(fd, "user.virtfs.gid",
850 &tmp_gid, sizeof(gid_t), 0, 0) > 0) {
851 stbuf->st_gid = tmp_gid;
853 if (fgetxattr(fd, "user.virtfs.mode",
854 &tmp_mode, sizeof(mode_t), 0, 0) > 0) {
855 stbuf->st_mode = tmp_mode;
857 if (fgetxattr(fd, "user.virtfs.rdev",
858 &tmp_dev, sizeof(dev_t), 0, 0) > 0) {
859 stbuf->st_rdev = tmp_dev;
863 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
870 static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
871 int flags, FsCred *credp, V9fsFidOpenState *fs)
873 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
882 * Mark all the open to not follow symlinks
886 v9fs_string_init(&fullname);
888 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
890 v9fs_string_sprintf(&fullname, "%s\\%s", dir_path->data, name);
893 path = fullname.data;
894 buffer = rpath(fs_ctx, path);
896 /* Determine the security model */
897 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
898 fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
904 credp->fc_mode = credp->fc_mode|S_IFREG;
905 /* Set cleint credentials in xattr */
906 err = local_set_xattr(buffer, credp);
912 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
913 fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
918 credp->fc_mode = credp->fc_mode|S_IFREG;
919 /* Set client credentials in .virtfs_metadata directory files */
920 err = local_set_mapped_file_attr(fs_ctx, path, credp);
925 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
926 (fs_ctx->export_flags & V9FS_SM_NONE)) {
927 fd = open(buffer, flags, credp->fc_mode);
932 err = local_post_create_passthrough(fs_ctx, path, credp);
948 v9fs_string_free(&fullname);
952 static int local_symlink(FsContext *fs_ctx, const char *oldpath,
953 V9fsPath *dir_path, const char *name, FsCred *credp)
955 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
963 int flags = O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW;
965 v9fs_string_init(&fullname);
966 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
967 newpath = fullname.data;
968 buffer = rpath(fs_ctx, newpath);
970 /* Determine the security model */
971 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
973 ssize_t oldpath_size, write_size;
974 fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
979 /* Write the oldpath (target) to the file. */
980 oldpath_size = strlen(oldpath);
982 write_size = write(fd, (void *)oldpath, oldpath_size);
983 } while (write_size == -1 && errno == EINTR);
985 if (write_size != oldpath_size) {
992 /* Set cleint credentials in symlink's xattr */
993 credp->fc_mode = credp->fc_mode|S_IFLNK;
994 err = local_set_xattr(buffer, credp);
999 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1001 ssize_t oldpath_size, write_size;
1002 fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
1007 /* Write the oldpath (target) to the file. */
1008 oldpath_size = strlen(oldpath);
1010 write_size = write(fd, (void *)oldpath, oldpath_size);
1011 } while (write_size == -1 && errno == EINTR);
1013 if (write_size != oldpath_size) {
1020 /* Set cleint credentials in symlink's xattr */
1021 credp->fc_mode = credp->fc_mode|S_IFLNK;
1022 err = local_set_mapped_file_attr(fs_ctx, newpath, credp);
1027 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
1028 (fs_ctx->export_flags & V9FS_SM_NONE)) {
1029 err = symlink(oldpath, buffer);
1033 err = lchown(buffer, credp->fc_uid,
1037 * If we fail to change ownership and if we are
1038 * using security model none. Ignore the error
1040 if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
1054 v9fs_string_free(&fullname);
1059 static int local_link(FsContext *ctx, V9fsPath *oldpath,
1060 V9fsPath *dirpath, const char *name)
1062 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
1064 #ifndef CONFIG_WIN32
1066 char *buffer, *buffer1;
1068 v9fs_string_init(&newpath);
1069 v9fs_string_sprintf(&newpath, "%s/%s", dirpath->data, name);
1071 buffer = rpath(ctx, oldpath->data);
1072 buffer1 = rpath(ctx, newpath.data);
1073 ret = link(buffer, buffer1);
1077 /* now link the virtfs_metadata files */
1078 if (!ret && (ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
1079 /* Link the .virtfs_metadata files. Create the metada directory */
1080 ret = local_create_mapped_attr_dir(ctx, newpath.data);
1084 buffer = local_mapped_attr_path(ctx, oldpath->data);
1085 buffer1 = local_mapped_attr_path(ctx, newpath.data);
1086 ret = link(buffer, buffer1);
1089 if (ret < 0 && errno != ENOENT) {
1094 v9fs_string_free(&newpath);
1099 static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
1101 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
1104 char *path = fs_path->data;
1105 buffer = rpath(ctx, path);
1106 #ifndef CONFIG_WIN32
1107 ret = truncate(buffer, size);
1109 int fd = open(buffer, O_RDWR | O_BINARY | O_NOFOLLOW);
1113 ret = ftruncate(fd, size);
1121 static int local_rename(FsContext *ctx, const char *oldpath,
1122 const char *newpath)
1124 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
1126 char *buffer, *buffer1;
1128 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1129 err = local_create_mapped_attr_dir(ctx, newpath);
1133 /* rename the .virtfs_metadata files */
1134 buffer = local_mapped_attr_path(ctx, oldpath);
1135 buffer1 = local_mapped_attr_path(ctx, newpath);
1136 err = rename(buffer, buffer1);
1139 if (err < 0 && errno != ENOENT) {
1143 buffer = rpath(ctx, oldpath);
1144 buffer1 = rpath(ctx, newpath);
1145 #ifndef CONFIG_WIN32
1146 err = rename(buffer, buffer1);
1151 if (!MoveFileEx((LPCTSTR)buffer,
1152 (LPCTSTR)buffer1, MOVEFILE_REPLACE_EXISTING)){
1153 err = -(GetLastError());
1154 ERR("[%d][ >> %s] err: %d\n", __LINE__, __func__, err);
1164 static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
1166 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
1167 #ifndef CONFIG_WIN32
1169 char *path = fs_path->data;
1170 buffer = rpath(fs_ctx, path);
1172 if ((credp->fc_uid == -1 && credp->fc_gid == -1) ||
1173 (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
1174 (fs_ctx->export_flags & V9FS_SM_NONE)) {
1175 return lchown(buffer, credp->fc_uid, credp->fc_gid);
1176 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
1177 return local_set_xattr(buffer, credp);
1178 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1179 return local_set_mapped_file_attr(fs_ctx, path, credp);
1187 /* The function that change file last access and modification times for WIN32 */
1188 static int win_utimes (const char *filename, const struct timeval tv[2])
1190 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
1192 FILETIME LastAccessTime, LastWriteTime;
1194 ULARGE_INTEGER tmpTime;
1196 ULONGLONG WindowsUnixTimeGap = 11644473600; // between 1601.01.01 and 1970.01.01
1197 ULONGLONG WindowsSecond = 10000000; // 1sec / 100nanosec
1201 HANDLE hFile = CreateFile(filename, FILE_WRITE_ATTRIBUTES,
1202 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1203 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS, NULL);
1204 if (hFile == INVALID_HANDLE_VALUE) {
1205 if (GetFileAttributes(filename) & FILE_ATTRIBUTE_DIRECTORY) {
1212 if (!tv) { // case of NULL
1213 GetSystemTimeAsFileTime(&LastAccessTime);
1214 LastWriteTime = LastAccessTime;
1216 tmpTime.QuadPart = UInt32x32To64(WindowsUnixTimeGap + tv[0].tv_sec, WindowsSecond)
1217 + tv[0].tv_usec * 10;
1218 LastAccessTime.dwHighDateTime = tmpTime.HighPart;
1219 LastAccessTime.dwLowDateTime = tmpTime.LowPart;
1221 tmpTime.QuadPart = UInt32x32To64(WindowsUnixTimeGap + tv[1].tv_sec, WindowsSecond)
1222 + tv[1].tv_usec * 10;
1223 LastWriteTime.dwHighDateTime = tmpTime.HighPart;
1224 LastWriteTime.dwLowDateTime = tmpTime.LowPart;
1226 if (!SetFileTime(hFile, NULL, &LastAccessTime, &LastWriteTime)) {
1240 static int local_utimensat(FsContext *s, V9fsPath *fs_path,
1241 const struct timespec *buf)
1243 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
1246 char *path = fs_path->data;
1247 buffer = rpath(s, path);
1248 #ifndef CONFIG_WIN32
1249 ret = qemu_utimens(buffer, buf);
1253 const char * r_path = buffer;
1254 struct timeval tv[2], tv_now;
1258 /* happy if special cases */
1259 if (buf[0].tv_nsec == UTIME_OMIT && buf[1].tv_nsec == UTIME_OMIT) {
1262 if (buf[0].tv_nsec == UTIME_NOW && buf[1].tv_nsec == UTIME_NOW) {
1263 return win_utimes(r_path, NULL);
1266 /* prepare for hard cases */
1267 if (buf[0].tv_nsec == UTIME_NOW || buf[1].tv_nsec == UTIME_NOW) {
1268 gettimeofday(&tv_now, NULL);
1270 if (buf[0].tv_nsec == UTIME_OMIT || buf[1].tv_nsec == UTIME_OMIT) {
1274 for (i = 0; i < 2; i++) {
1275 if (buf[i].tv_nsec == UTIME_NOW) {
1276 tv[i].tv_sec = tv_now.tv_sec;
1277 tv[i].tv_usec = tv_now.tv_usec;
1278 } else if (buf[i].tv_nsec == UTIME_OMIT) {
1279 tv[i].tv_sec = (i == 0) ? st.st_atime : st.st_mtime;
1282 tv[i].tv_sec = buf[i].tv_sec;
1283 tv[i].tv_usec = buf[i].tv_nsec / 1000;
1287 return win_utimes(r_path, &tv[0]);
1291 static int local_remove(FsContext *ctx, const char *path)
1293 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
1296 char *buffer = rpath(ctx, path);
1298 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1299 #ifndef CONFIG_WIN32
1300 err = lstat(buffer, &stbuf);
1302 err = _stat(buffer, &stbuf);
1309 * If directory remove .virtfs_metadata contained in the
1312 if (S_ISDIR(stbuf.st_mode)) {
1313 #ifndef CONFIG_WIN32
1314 sprintf(buffer, "%s/%s/%s", ctx->fs_root, path, VIRTFS_META_DIR);
1316 sprintf(buffer, "%s\\%s\\%s", ctx->fs_root, path, VIRTFS_META_DIR);
1318 err = remove(buffer);
1319 if (err < 0 && errno != ENOENT) {
1321 * We didn't had the .virtfs_metadata file. May be file created
1322 * in non-mapped mode ?. Ignore ENOENT.
1328 * Now remove the name from parent directory
1329 * .virtfs_metadata directory
1331 buffer = local_mapped_attr_path(ctx, path);
1332 err = remove(buffer);
1334 if (err < 0 && errno != ENOENT) {
1336 * We didn't had the .virtfs_metadata file. May be file created
1337 * in non-mapped mode ?. Ignore ENOENT.
1342 buffer = rpath(ctx, path);
1343 err = remove(buffer);
1349 static int local_fsync(FsContext *ctx, int fid_type,
1350 V9fsFidOpenState *fs, int datasync)
1352 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
1355 if (fid_type == P9_FID_DIR) {
1356 #ifndef CONFIG_WIN32
1357 fd = dirfd(fs->dir);
1364 #ifndef CONFIG_WIN32
1366 return qemu_fdatasync(fd);
1375 static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
1377 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
1379 char *path = fs_path->data;
1380 buffer = rpath(s, path);
1382 #ifndef CONFIG_WIN32
1383 return statfs(buffer, stbuf);
1385 DWORD VolumeSerialNumber,
1386 MaximumComponentLength,
1389 DWORD SectorsPerCluster,
1391 NumberOfFreeClusters,
1392 TotalNumberOfClusters,
1395 ULARGE_INTEGER TotalNumberOfBytes,
1396 TotalNumberOfFreeBytes,
1397 FreeBytesAvailableToCaller;
1399 TCHAR tmpName[PATH_MAX] = {0};
1400 TCHAR VolumeNameBuffer[PATH_MAX] = {0};
1401 TCHAR FileSystemNameBuffer[PATH_MAX] = {0};
1403 strncpy(tmpName, buffer, 3 > PATH_MAX ? PATH_MAX : 3);
1404 tmpName[4 > PATH_MAX ? PATH_MAX : 4] = '\0';
1405 LPCTSTR RootPathName = (LPCTSTR)tmpName;
1407 if (RootPathName == NULL) {
1408 ERR("[%d][%s] >> err = %d\n", __LINE__, __func__, GetLastError());
1413 if (!GetVolumeInformation(RootPathName, (LPTSTR)&VolumeNameBuffer, PATH_MAX,
1414 &VolumeSerialNumber, &MaximumComponentLength, &FileSystemFlags,
1415 (LPTSTR)&FileSystemNameBuffer, PATH_MAX)) {
1416 ERR("[%d][%s] >> err = %d\n", __LINE__, __func__, GetLastError());
1420 if (!GetDiskFreeSpace(RootPathName, &SectorsPerCluster, &BytesPerSector,
1421 &NumberOfFreeClusters, &TotalNumberOfClusters)) {
1422 SectorsPerCluster = 1;
1424 NumberOfFreeClusters = 0;
1425 TotalNumberOfClusters = 0;
1428 BytesPerCluster = SectorsPerCluster * BytesPerSector;
1429 TotalNumberOfBytes.QuadPart = Int32x32To64(TotalNumberOfClusters, BytesPerCluster);
1430 TotalNumberOfFreeBytes.QuadPart = Int32x32To64(NumberOfFreeClusters, BytesPerCluster);
1431 FreeBytesAvailableToCaller=TotalNumberOfFreeBytes;
1433 if (!strcmp(FileSystemNameBuffer, "NTFS")) {
1434 stbuf->f_type = NTFS_SUPER_MAGIC;
1436 /* need to check more about other filesystems (ex CD) */
1438 stbuf->f_type = MSDOS_SUPER_MAGIC;
1440 stbuf->f_bsize = BytesPerSector;
1441 stbuf->f_blocks = TotalNumberOfBytes.QuadPart / BytesPerSector;
1442 stbuf->f_bfree = TotalNumberOfFreeBytes.QuadPart / BytesPerSector;
1443 stbuf->f_bavail = FreeBytesAvailableToCaller.QuadPart / BytesPerSector;
1444 stbuf->f_files = stbuf->f_blocks / SectorsPerCluster;
1445 stbuf->f_ffree = stbuf->f_bfree / SectorsPerCluster;
1446 stbuf->f_fsid.val[0] = HIWORD(VolumeSerialNumber);
1447 stbuf->f_fsid.val[1] = LOWORD(VolumeSerialNumber);
1448 stbuf->f_namelen = MaximumComponentLength;
1449 stbuf->f_spare[0] = 0;
1450 stbuf->f_spare[1] = 0;
1451 stbuf->f_spare[2] = 0;
1452 stbuf->f_spare[3] = 0;
1453 stbuf->f_spare[4] = 0;
1454 stbuf->f_spare[5] = 0;
1460 static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
1461 const char *name, void *value, size_t size)
1463 #ifndef CONFIG_WIN32
1464 char *path = fs_path->data;
1466 return v9fs_get_xattr(ctx, path, name, value, size);
1468 /* xattr doesn't support on Windows */
1473 static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path,
1474 void *value, size_t size)
1476 #ifndef CONFIG_WIN32
1477 char *path = fs_path->data;
1479 return v9fs_list_xattr(ctx, path, value, size);
1481 /* xattr doesn't support on Windows */
1486 static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
1487 void *value, size_t size, int flags)
1489 #ifndef CONFIG_WIN32
1490 char *path = fs_path->data;
1492 return v9fs_set_xattr(ctx, path, name, value, size, flags);
1494 /* xattr doesn't support on Windows */
1499 static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
1502 #ifndef CONFIG_WIN32
1503 char *path = fs_path->data;
1505 return v9fs_remove_xattr(ctx, path, name);
1507 /* xattr doesn't support on Windows */
1512 static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path,
1513 const char *name, V9fsPath *target)
1515 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
1517 #ifndef CONFIG_WIN32
1518 v9fs_string_sprintf((V9fsString *)target, "%s/%s",
1519 dir_path->data, name);
1521 v9fs_string_sprintf((V9fsString *)target, "%s\\%s",
1522 dir_path->data, name);
1523 while((target->data)[strlen(target->data)-1] == '\\'){
1524 (target->data)[strlen(target->data)-1] = '\0';
1528 v9fs_string_sprintf((V9fsString *)target, "%s", name);
1530 /* Bump the size for including terminating NULL */
1535 static int local_renameat(FsContext *ctx, V9fsPath *olddir,
1536 const char *old_name, V9fsPath *newdir,
1537 const char *new_name)
1539 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
1541 V9fsString old_full_name, new_full_name;
1543 v9fs_string_init(&old_full_name);
1544 v9fs_string_init(&new_full_name);
1546 #ifndef CONFIG_WIN32
1547 v9fs_string_sprintf(&old_full_name, "%s/%s", olddir->data, old_name);
1548 v9fs_string_sprintf(&new_full_name, "%s/%s", newdir->data, new_name);
1550 v9fs_string_sprintf(&old_full_name, "%s\\%s", olddir->data, old_name);
1551 v9fs_string_sprintf(&new_full_name, "%s\\%s", newdir->data, new_name);
1554 ret = local_rename(ctx, old_full_name.data, new_full_name.data);
1555 v9fs_string_free(&old_full_name);
1556 v9fs_string_free(&new_full_name);
1560 static int local_unlinkat(FsContext *ctx, V9fsPath *dir,
1561 const char *name, int flags)
1563 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
1565 V9fsString fullname;
1568 v9fs_string_init(&fullname);
1570 #ifndef CONFIG_WIN32
1571 v9fs_string_sprintf(&fullname, "%s/%s", dir->data, name);
1573 v9fs_string_sprintf(&fullname, "%s\\%s", dir->data, name);
1574 while(fullname.data[strlen(fullname.data)-1] == '\\'){
1575 fullname.data[strlen(fullname.data)-1] = '\0';
1578 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1579 #ifdef CONFIG_VIRTFS_DARWIN
1580 // TODO: find something to replace AT_REMOVEDIR feature.
1581 if (flags == AT_REMOVEDIR) {
1583 * If directory remove .virtfs_metadata contained in the
1586 #ifndef CONFIG_WIN32
1587 sprintf(buffer, "%s/%s/%s", ctx->fs_root,
1588 fullname.data, VIRTFS_META_DIR);
1590 sprintf(buffer, "%s\\%s\\%s", ctx->fs_root,
1591 fullname.data, VIRTFS_META_DIR);
1593 ret = remove(buffer);
1594 if (ret < 0 && errno != ENOENT) {
1596 * We didn't had the .virtfs_metadata file. May be file created
1597 * in non-mapped mode ?. Ignore ENOENT.
1604 * Now remove the name from parent directory
1605 * .virtfs_metadata directory.
1607 buffer = local_mapped_attr_path(ctx, fullname.data);
1608 ret = remove(buffer);
1610 if (ret < 0 && errno != ENOENT) {
1612 * We didn't had the .virtfs_metadata file. May be file created
1613 * in non-mapped mode ?. Ignore ENOENT.
1618 /* Remove the name finally */
1619 buffer = rpath(ctx, fullname.data);
1620 #ifndef CONFIG_WIN32
1621 ret = remove(buffer);
1623 if (flags == AT_REMOVEDIR) { // is dir
1624 ret = rmdir(buffer);
1626 ret = remove(buffer);
1630 v9fs_string_free(&fullname);
1636 #ifndef CONFIG_WIN32
1637 static int local_ioc_getversion(FsContext *ctx, V9fsPath *path,
1638 mode_t st_mode, uint64_t *st_gen)
1640 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
1643 #ifdef FS_IOC_GETVERSION
1644 V9fsFidOpenState fid_open;
1647 * Do not try to open special files like device nodes, fifos etc
1648 * We can get fd for regular files and directories only
1650 if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
1653 err = local_open(ctx, path, O_RDONLY, &fid_open);
1657 err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
1658 local_close(ctx, &fid_open);
1666 static int local_init(FsContext *ctx)
1668 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
1670 #ifdef FS_IOC_GETVERSION
1671 struct statfs stbuf;
1674 #ifndef CONFIG_WIN32
1675 if (ctx->export_flags & V9FS_SM_PASSTHROUGH) {
1676 ctx->xops = passthrough_xattr_ops;
1677 } else if (ctx->export_flags & V9FS_SM_MAPPED) {
1678 ctx->xops = mapped_xattr_ops;
1679 } else if (ctx->export_flags & V9FS_SM_NONE) {
1680 ctx->xops = none_xattr_ops;
1681 } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1683 * xattr operation for mapped-file and passthrough
1686 ctx->xops = passthrough_xattr_ops;
1691 ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT;
1692 #ifdef FS_IOC_GETVERSION
1694 * use ioc_getversion only if the iocl is definied
1696 err = statfs(ctx->fs_root, &stbuf);
1698 switch (stbuf.f_type) {
1699 case EXT2_SUPER_MAGIC:
1700 case BTRFS_SUPER_MAGIC:
1701 case REISERFS_SUPER_MAGIC:
1702 case XFS_SUPER_MAGIC:
1703 ctx->exops.get_st_gen = local_ioc_getversion;
1711 static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse)
1713 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
1714 const char *sec_model = qemu_opt_get(opts, "security_model");
1715 const char *path = qemu_opt_get(opts, "path");
1718 fprintf(stderr, "security model not specified, "
1719 "local fs needs security model\nvalid options are:"
1720 "\tsecurity_model=[passthrough|mapped|none]\n");
1724 if (!strcmp(sec_model, "passthrough")) {
1725 fse->export_flags |= V9FS_SM_PASSTHROUGH;
1726 } else if (!strcmp(sec_model, "mapped") ||
1727 !strcmp(sec_model, "mapped-xattr")) {
1728 fse->export_flags |= V9FS_SM_MAPPED;
1729 } else if (!strcmp(sec_model, "none")) {
1730 fse->export_flags |= V9FS_SM_NONE;
1731 } else if (!strcmp(sec_model, "mapped-file")) {
1732 fse->export_flags |= V9FS_SM_MAPPED_FILE;
1734 fprintf(stderr, "Invalid security model %s specified, valid options are"
1735 "\n\t [passthrough|mapped-xattr|mapped-file|none]\n",
1741 fprintf(stderr, "fsdev: No path specified.\n");
1744 fse->path = g_strdup(path);
1749 FileOperations local_ops = {
1750 .parse_opts = local_parse_opts,
1752 .lstat = local_lstat,
1753 .readlink = local_readlink,
1754 .close = local_close,
1755 .closedir = local_closedir,
1757 .opendir = local_opendir,
1758 .rewinddir = local_rewinddir,
1759 .telldir = local_telldir,
1760 .readdir_r = local_readdir_r,
1761 .seekdir = local_seekdir,
1762 .statfs = local_statfs,
1763 .preadv = local_preadv,
1764 .pwritev = local_pwritev,
1765 .truncate = local_truncate,
1766 .utimensat = local_utimensat,
1767 .open2 = local_open2,
1768 .lgetxattr = local_lgetxattr,
1769 .llistxattr = local_llistxattr,
1770 .lsetxattr = local_lsetxattr,
1771 .lremovexattr = local_lremovexattr,
1772 .mkdir = local_mkdir,
1773 .symlink = local_symlink,
1774 .remove = local_remove,
1775 .fstat = local_fstat,
1776 .chmod = local_chmod,
1777 .mknod = local_mknod,
1779 .rename = local_rename,
1780 .chown = local_chown,
1781 .fsync = local_fsync,
1782 .name_to_path = local_name_to_path,
1783 .unlinkat = local_unlinkat,
1784 .renameat = local_renameat,