19f107513SAnthony Liguori /* 2f00d4f59SWei Liu * 9p Posix callback 39f107513SAnthony Liguori * 49f107513SAnthony Liguori * Copyright IBM, Corp. 2010 59f107513SAnthony Liguori * 69f107513SAnthony Liguori * Authors: 79f107513SAnthony Liguori * Anthony Liguori <aliguori@us.ibm.com> 89f107513SAnthony Liguori * 99f107513SAnthony Liguori * This work is licensed under the terms of the GNU GPL, version 2. See 109f107513SAnthony Liguori * the COPYING file in the top-level directory. 119f107513SAnthony Liguori * 129f107513SAnthony Liguori */ 13873c3213SStefan Weil 14fbc04127SPeter Maydell #include "qemu/osdep.h" 15ebe74f8bSWei Liu #include "9p.h" 16996a0d76SGreg Kurz #include "9p-local.h" 17267ae092SWei Liu #include "9p-xattr.h" 180e35a378SGreg Kurz #include "9p-util.h" 1969b15212SStefan Weil #include "fsdev/qemu-fsdev.h" /* local_ops */ 20c494dd6fSAnthony Liguori #include <arpa/inet.h> 21131dcb25SAnthony Liguori #include <pwd.h> 22131dcb25SAnthony Liguori #include <grp.h> 23c494dd6fSAnthony Liguori #include <sys/socket.h> 24c494dd6fSAnthony Liguori #include <sys/un.h> 251de7afc9SPaolo Bonzini #include "qemu/xattr.h" 26f348b6d1SVeronia Bahaa #include "qemu/cutils.h" 2763325b18SGreg Kurz #include "qemu/error-report.h" 282c30dd74SAneesh Kumar K.V #include <libgen.h> 29e06a765eSHarsh Prateek Bora #include <linux/fs.h> 30e06a765eSHarsh Prateek Bora #ifdef CONFIG_LINUX_MAGIC_H 31e06a765eSHarsh Prateek Bora #include <linux/magic.h> 32e06a765eSHarsh Prateek Bora #endif 33e06a765eSHarsh Prateek Bora #include <sys/ioctl.h> 34e06a765eSHarsh Prateek Bora 35e06a765eSHarsh Prateek Bora #ifndef XFS_SUPER_MAGIC 36e06a765eSHarsh Prateek Bora #define XFS_SUPER_MAGIC 0x58465342 37e06a765eSHarsh Prateek Bora #endif 38e06a765eSHarsh Prateek Bora #ifndef EXT2_SUPER_MAGIC 39e06a765eSHarsh Prateek Bora #define EXT2_SUPER_MAGIC 0xEF53 40e06a765eSHarsh Prateek Bora #endif 41e06a765eSHarsh Prateek Bora #ifndef REISERFS_SUPER_MAGIC 42e06a765eSHarsh Prateek Bora #define REISERFS_SUPER_MAGIC 0x52654973 43e06a765eSHarsh Prateek Bora #endif 44e06a765eSHarsh Prateek Bora #ifndef BTRFS_SUPER_MAGIC 45e06a765eSHarsh Prateek Bora #define BTRFS_SUPER_MAGIC 0x9123683E 46e06a765eSHarsh Prateek Bora #endif 47131dcb25SAnthony Liguori 480e35a378SGreg Kurz typedef struct { 490e35a378SGreg Kurz int mountfd; 500e35a378SGreg Kurz } LocalData; 510e35a378SGreg Kurz 52996a0d76SGreg Kurz int local_open_nofollow(FsContext *fs_ctx, const char *path, int flags, 53996a0d76SGreg Kurz mode_t mode) 54996a0d76SGreg Kurz { 55996a0d76SGreg Kurz LocalData *data = fs_ctx->private; 563dbcf273SGreg Kurz int fd = data->mountfd; 57996a0d76SGreg Kurz 583dbcf273SGreg Kurz while (*path && fd != -1) { 593dbcf273SGreg Kurz const char *c; 603dbcf273SGreg Kurz int next_fd; 613dbcf273SGreg Kurz char *head; 623dbcf273SGreg Kurz 633dbcf273SGreg Kurz /* Only relative paths without consecutive slashes */ 643dbcf273SGreg Kurz assert(*path != '/'); 653dbcf273SGreg Kurz 663dbcf273SGreg Kurz head = g_strdup(path); 673dbcf273SGreg Kurz c = strchrnul(path, '/'); 683dbcf273SGreg Kurz if (*c) { 693dbcf273SGreg Kurz /* Intermediate path element */ 703dbcf273SGreg Kurz head[c - path] = 0; 713dbcf273SGreg Kurz path = c + 1; 723dbcf273SGreg Kurz next_fd = openat_dir(fd, head); 733dbcf273SGreg Kurz } else { 743dbcf273SGreg Kurz /* Rightmost path element */ 753dbcf273SGreg Kurz next_fd = openat_file(fd, head, flags, mode); 763dbcf273SGreg Kurz path = c; 773dbcf273SGreg Kurz } 783dbcf273SGreg Kurz g_free(head); 793dbcf273SGreg Kurz if (fd != data->mountfd) { 803dbcf273SGreg Kurz close_preserve_errno(fd); 813dbcf273SGreg Kurz } 823dbcf273SGreg Kurz fd = next_fd; 83996a0d76SGreg Kurz } 84996a0d76SGreg Kurz 853dbcf273SGreg Kurz assert(fd != data->mountfd); 863dbcf273SGreg Kurz return fd; 87996a0d76SGreg Kurz } 88996a0d76SGreg Kurz 89996a0d76SGreg Kurz int local_opendir_nofollow(FsContext *fs_ctx, const char *path) 90996a0d76SGreg Kurz { 91996a0d76SGreg Kurz return local_open_nofollow(fs_ctx, path, O_DIRECTORY | O_RDONLY, 0); 92996a0d76SGreg Kurz } 93996a0d76SGreg Kurz 9499f2cf4bSGreg Kurz static void renameat_preserve_errno(int odirfd, const char *opath, int ndirfd, 9599f2cf4bSGreg Kurz const char *npath) 9699f2cf4bSGreg Kurz { 9799f2cf4bSGreg Kurz int serrno = errno; 9899f2cf4bSGreg Kurz renameat(odirfd, opath, ndirfd, npath); 9999f2cf4bSGreg Kurz errno = serrno; 10099f2cf4bSGreg Kurz } 10199f2cf4bSGreg Kurz 102ad0b46e6SGreg Kurz static void unlinkat_preserve_errno(int dirfd, const char *path, int flags) 103ad0b46e6SGreg Kurz { 104ad0b46e6SGreg Kurz int serrno = errno; 105ad0b46e6SGreg Kurz unlinkat(dirfd, path, flags); 106ad0b46e6SGreg Kurz errno = serrno; 107ad0b46e6SGreg Kurz } 108ad0b46e6SGreg Kurz 1092c30dd74SAneesh Kumar K.V #define VIRTFS_META_DIR ".virtfs_metadata" 11081ffbf5aSGreg Kurz #define VIRTFS_META_ROOT_FILE VIRTFS_META_DIR "_root" 1112c30dd74SAneesh Kumar K.V 112f9aef99bSGreg Kurz static FILE *local_fopenat(int dirfd, const char *name, const char *mode) 1130ceb092eSAneesh Kumar K.V { 1140ceb092eSAneesh Kumar K.V int fd, o_mode = 0; 1150ceb092eSAneesh Kumar K.V FILE *fp; 116f9aef99bSGreg Kurz int flags; 1170ceb092eSAneesh Kumar K.V /* 1180ceb092eSAneesh Kumar K.V * only supports two modes 1190ceb092eSAneesh Kumar K.V */ 1200ceb092eSAneesh Kumar K.V if (mode[0] == 'r') { 121f9aef99bSGreg Kurz flags = O_RDONLY; 1220ceb092eSAneesh Kumar K.V } else if (mode[0] == 'w') { 123f9aef99bSGreg Kurz flags = O_WRONLY | O_TRUNC | O_CREAT; 1240ceb092eSAneesh Kumar K.V o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; 1250ceb092eSAneesh Kumar K.V } else { 1260ceb092eSAneesh Kumar K.V return NULL; 1270ceb092eSAneesh Kumar K.V } 128f9aef99bSGreg Kurz fd = openat_file(dirfd, name, flags, o_mode); 1290ceb092eSAneesh Kumar K.V if (fd == -1) { 1300ceb092eSAneesh Kumar K.V return NULL; 1310ceb092eSAneesh Kumar K.V } 1320ceb092eSAneesh Kumar K.V fp = fdopen(fd, mode); 1330ceb092eSAneesh Kumar K.V if (!fp) { 1340ceb092eSAneesh Kumar K.V close(fd); 1350ceb092eSAneesh Kumar K.V } 1360ceb092eSAneesh Kumar K.V return fp; 1370ceb092eSAneesh Kumar K.V } 1380ceb092eSAneesh Kumar K.V 1392c30dd74SAneesh Kumar K.V #define ATTR_MAX 100 140f9aef99bSGreg Kurz static void local_mapped_file_attr(int dirfd, const char *name, 1412c30dd74SAneesh Kumar K.V struct stat *stbuf) 1422c30dd74SAneesh Kumar K.V { 1432c30dd74SAneesh Kumar K.V FILE *fp; 1442c30dd74SAneesh Kumar K.V char buf[ATTR_MAX]; 145f9aef99bSGreg Kurz int map_dirfd; 1462c30dd74SAneesh Kumar K.V 14781ffbf5aSGreg Kurz if (strcmp(name, ".")) { 14899f2cf4bSGreg Kurz map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR); 149f9aef99bSGreg Kurz if (map_dirfd == -1) { 150f9aef99bSGreg Kurz return; 151f9aef99bSGreg Kurz } 152f9aef99bSGreg Kurz 153f9aef99bSGreg Kurz fp = local_fopenat(map_dirfd, name, "r"); 154f9aef99bSGreg Kurz close_preserve_errno(map_dirfd); 15581ffbf5aSGreg Kurz } else { 15681ffbf5aSGreg Kurz fp = local_fopenat(dirfd, VIRTFS_META_ROOT_FILE, "r"); 15781ffbf5aSGreg Kurz } 1582c30dd74SAneesh Kumar K.V if (!fp) { 1592c30dd74SAneesh Kumar K.V return; 1602c30dd74SAneesh Kumar K.V } 1612c30dd74SAneesh Kumar K.V memset(buf, 0, ATTR_MAX); 1622c30dd74SAneesh Kumar K.V while (fgets(buf, ATTR_MAX, fp)) { 1632c30dd74SAneesh Kumar K.V if (!strncmp(buf, "virtfs.uid", 10)) { 1642c30dd74SAneesh Kumar K.V stbuf->st_uid = atoi(buf+11); 1652c30dd74SAneesh Kumar K.V } else if (!strncmp(buf, "virtfs.gid", 10)) { 1662c30dd74SAneesh Kumar K.V stbuf->st_gid = atoi(buf+11); 1672c30dd74SAneesh Kumar K.V } else if (!strncmp(buf, "virtfs.mode", 11)) { 1682c30dd74SAneesh Kumar K.V stbuf->st_mode = atoi(buf+12); 1692c30dd74SAneesh Kumar K.V } else if (!strncmp(buf, "virtfs.rdev", 11)) { 1702c30dd74SAneesh Kumar K.V stbuf->st_rdev = atoi(buf+12); 1712c30dd74SAneesh Kumar K.V } 1722c30dd74SAneesh Kumar K.V memset(buf, 0, ATTR_MAX); 1732c30dd74SAneesh Kumar K.V } 1742c30dd74SAneesh Kumar K.V fclose(fp); 1752c30dd74SAneesh Kumar K.V } 1762c30dd74SAneesh Kumar K.V 1772289be19SAneesh Kumar K.V static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf) 178131dcb25SAnthony Liguori { 179f9aef99bSGreg Kurz int err = -1; 180f9aef99bSGreg Kurz char *dirpath = g_path_get_dirname(fs_path->data); 181f9aef99bSGreg Kurz char *name = g_path_get_basename(fs_path->data); 182f9aef99bSGreg Kurz int dirfd; 1832289be19SAneesh Kumar K.V 184f9aef99bSGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dirpath); 185f9aef99bSGreg Kurz if (dirfd == -1) { 186f9aef99bSGreg Kurz goto out; 187f9aef99bSGreg Kurz } 188f9aef99bSGreg Kurz 189f9aef99bSGreg Kurz err = fstatat(dirfd, name, stbuf, AT_SYMLINK_NOFOLLOW); 1901237ad76SVenkateswararao Jujjuri (JV) if (err) { 1914fa4ce71SChen Gang goto err_out; 1921237ad76SVenkateswararao Jujjuri (JV) } 193b97400caSAneesh Kumar K.V if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 1941237ad76SVenkateswararao Jujjuri (JV) /* Actual credentials are part of extended attrs */ 1951237ad76SVenkateswararao Jujjuri (JV) uid_t tmp_uid; 1961237ad76SVenkateswararao Jujjuri (JV) gid_t tmp_gid; 1971237ad76SVenkateswararao Jujjuri (JV) mode_t tmp_mode; 1981237ad76SVenkateswararao Jujjuri (JV) dev_t tmp_dev; 199f9aef99bSGreg Kurz 200f9aef99bSGreg Kurz if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.uid", &tmp_uid, 201f9aef99bSGreg Kurz sizeof(uid_t)) > 0) { 202f8ad4a89SAneesh Kumar K.V stbuf->st_uid = le32_to_cpu(tmp_uid); 2031237ad76SVenkateswararao Jujjuri (JV) } 204f9aef99bSGreg Kurz if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.gid", &tmp_gid, 205f9aef99bSGreg Kurz sizeof(gid_t)) > 0) { 206f8ad4a89SAneesh Kumar K.V stbuf->st_gid = le32_to_cpu(tmp_gid); 2071237ad76SVenkateswararao Jujjuri (JV) } 208f9aef99bSGreg Kurz if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.mode", &tmp_mode, 209f9aef99bSGreg Kurz sizeof(mode_t)) > 0) { 210f8ad4a89SAneesh Kumar K.V stbuf->st_mode = le32_to_cpu(tmp_mode); 2111237ad76SVenkateswararao Jujjuri (JV) } 212f9aef99bSGreg Kurz if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.rdev", &tmp_dev, 213f9aef99bSGreg Kurz sizeof(dev_t)) > 0) { 214f8ad4a89SAneesh Kumar K.V stbuf->st_rdev = le64_to_cpu(tmp_dev); 2151237ad76SVenkateswararao Jujjuri (JV) } 2162c30dd74SAneesh Kumar K.V } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 217f9aef99bSGreg Kurz local_mapped_file_attr(dirfd, name, stbuf); 2181237ad76SVenkateswararao Jujjuri (JV) } 2194fa4ce71SChen Gang 2204fa4ce71SChen Gang err_out: 221f9aef99bSGreg Kurz close_preserve_errno(dirfd); 222f9aef99bSGreg Kurz out: 223f9aef99bSGreg Kurz g_free(name); 224f9aef99bSGreg Kurz g_free(dirpath); 2251237ad76SVenkateswararao Jujjuri (JV) return err; 226131dcb25SAnthony Liguori } 227131dcb25SAnthony Liguori 228e3187a45SGreg Kurz static int local_set_mapped_file_attrat(int dirfd, const char *name, 229e3187a45SGreg Kurz FsCred *credp) 2302c30dd74SAneesh Kumar K.V { 2312c30dd74SAneesh Kumar K.V FILE *fp; 232e3187a45SGreg Kurz int ret; 2332c30dd74SAneesh Kumar K.V char buf[ATTR_MAX]; 2342c30dd74SAneesh Kumar K.V int uid = -1, gid = -1, mode = -1, rdev = -1; 23581ffbf5aSGreg Kurz int map_dirfd = -1, map_fd; 23681ffbf5aSGreg Kurz bool is_root = !strcmp(name, "."); 2372c30dd74SAneesh Kumar K.V 23881ffbf5aSGreg Kurz if (is_root) { 23981ffbf5aSGreg Kurz fp = local_fopenat(dirfd, VIRTFS_META_ROOT_FILE, "r"); 24081ffbf5aSGreg Kurz if (!fp) { 24181ffbf5aSGreg Kurz if (errno == ENOENT) { 24281ffbf5aSGreg Kurz goto update_map_file; 24381ffbf5aSGreg Kurz } else { 24481ffbf5aSGreg Kurz return -1; 24581ffbf5aSGreg Kurz } 24681ffbf5aSGreg Kurz } 24781ffbf5aSGreg Kurz } else { 248e3187a45SGreg Kurz ret = mkdirat(dirfd, VIRTFS_META_DIR, 0700); 249e3187a45SGreg Kurz if (ret < 0 && errno != EEXIST) { 250e3187a45SGreg Kurz return -1; 251e3187a45SGreg Kurz } 252e3187a45SGreg Kurz 253e3187a45SGreg Kurz map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR); 254e3187a45SGreg Kurz if (map_dirfd == -1) { 255e3187a45SGreg Kurz return -1; 256e3187a45SGreg Kurz } 257e3187a45SGreg Kurz 258e3187a45SGreg Kurz fp = local_fopenat(map_dirfd, name, "r"); 2592c30dd74SAneesh Kumar K.V if (!fp) { 260e3187a45SGreg Kurz if (errno == ENOENT) { 261e3187a45SGreg Kurz goto update_map_file; 262e3187a45SGreg Kurz } else { 263e3187a45SGreg Kurz close_preserve_errno(map_dirfd); 264e3187a45SGreg Kurz return -1; 265e3187a45SGreg Kurz } 2662c30dd74SAneesh Kumar K.V } 26781ffbf5aSGreg Kurz } 2682c30dd74SAneesh Kumar K.V memset(buf, 0, ATTR_MAX); 2692c30dd74SAneesh Kumar K.V while (fgets(buf, ATTR_MAX, fp)) { 2702c30dd74SAneesh Kumar K.V if (!strncmp(buf, "virtfs.uid", 10)) { 2712c30dd74SAneesh Kumar K.V uid = atoi(buf + 11); 2722c30dd74SAneesh Kumar K.V } else if (!strncmp(buf, "virtfs.gid", 10)) { 2732c30dd74SAneesh Kumar K.V gid = atoi(buf + 11); 2742c30dd74SAneesh Kumar K.V } else if (!strncmp(buf, "virtfs.mode", 11)) { 2752c30dd74SAneesh Kumar K.V mode = atoi(buf + 12); 2762c30dd74SAneesh Kumar K.V } else if (!strncmp(buf, "virtfs.rdev", 11)) { 2772c30dd74SAneesh Kumar K.V rdev = atoi(buf + 12); 2782c30dd74SAneesh Kumar K.V } 2792c30dd74SAneesh Kumar K.V memset(buf, 0, ATTR_MAX); 2802c30dd74SAneesh Kumar K.V } 2812c30dd74SAneesh Kumar K.V fclose(fp); 2822c30dd74SAneesh Kumar K.V 2832c30dd74SAneesh Kumar K.V update_map_file: 28481ffbf5aSGreg Kurz if (is_root) { 28581ffbf5aSGreg Kurz fp = local_fopenat(dirfd, VIRTFS_META_ROOT_FILE, "w"); 28681ffbf5aSGreg Kurz } else { 287e3187a45SGreg Kurz fp = local_fopenat(map_dirfd, name, "w"); 28881ffbf5aSGreg Kurz /* We can't go this far with map_dirfd not being a valid file descriptor 28981ffbf5aSGreg Kurz * but some versions of gcc aren't smart enough to see it. 29081ffbf5aSGreg Kurz */ 29181ffbf5aSGreg Kurz if (map_dirfd != -1) { 292e3187a45SGreg Kurz close_preserve_errno(map_dirfd); 29381ffbf5aSGreg Kurz } 29481ffbf5aSGreg Kurz } 2952c30dd74SAneesh Kumar K.V if (!fp) { 296e3187a45SGreg Kurz return -1; 2972c30dd74SAneesh Kumar K.V } 2982c30dd74SAneesh Kumar K.V 29981ffbf5aSGreg Kurz map_fd = fileno(fp); 30081ffbf5aSGreg Kurz assert(map_fd != -1); 30181ffbf5aSGreg Kurz ret = fchmod(map_fd, 0600); 30281ffbf5aSGreg Kurz assert(ret == 0); 30381ffbf5aSGreg Kurz 3042c30dd74SAneesh Kumar K.V if (credp->fc_uid != -1) { 3052c30dd74SAneesh Kumar K.V uid = credp->fc_uid; 3062c30dd74SAneesh Kumar K.V } 3072c30dd74SAneesh Kumar K.V if (credp->fc_gid != -1) { 3082c30dd74SAneesh Kumar K.V gid = credp->fc_gid; 3092c30dd74SAneesh Kumar K.V } 3102c30dd74SAneesh Kumar K.V if (credp->fc_mode != -1) { 3112c30dd74SAneesh Kumar K.V mode = credp->fc_mode; 3122c30dd74SAneesh Kumar K.V } 3132c30dd74SAneesh Kumar K.V if (credp->fc_rdev != -1) { 3142c30dd74SAneesh Kumar K.V rdev = credp->fc_rdev; 3152c30dd74SAneesh Kumar K.V } 3162c30dd74SAneesh Kumar K.V 3172c30dd74SAneesh Kumar K.V if (uid != -1) { 3182c30dd74SAneesh Kumar K.V fprintf(fp, "virtfs.uid=%d\n", uid); 3192c30dd74SAneesh Kumar K.V } 3202c30dd74SAneesh Kumar K.V if (gid != -1) { 3212c30dd74SAneesh Kumar K.V fprintf(fp, "virtfs.gid=%d\n", gid); 3222c30dd74SAneesh Kumar K.V } 3232c30dd74SAneesh Kumar K.V if (mode != -1) { 3242c30dd74SAneesh Kumar K.V fprintf(fp, "virtfs.mode=%d\n", mode); 3252c30dd74SAneesh Kumar K.V } 3262c30dd74SAneesh Kumar K.V if (rdev != -1) { 3272c30dd74SAneesh Kumar K.V fprintf(fp, "virtfs.rdev=%d\n", rdev); 3282c30dd74SAneesh Kumar K.V } 3292c30dd74SAneesh Kumar K.V fclose(fp); 3302c30dd74SAneesh Kumar K.V 331e3187a45SGreg Kurz return 0; 332e3187a45SGreg Kurz } 333e3187a45SGreg Kurz 334e3187a45SGreg Kurz static int fchmodat_nofollow(int dirfd, const char *name, mode_t mode) 335e3187a45SGreg Kurz { 336*4751fd53SGreg Kurz struct stat stbuf; 337e3187a45SGreg Kurz int fd, ret; 338e3187a45SGreg Kurz 339e3187a45SGreg Kurz /* FIXME: this should be handled with fchmodat(AT_SYMLINK_NOFOLLOW). 340*4751fd53SGreg Kurz * Unfortunately, the linux kernel doesn't implement it yet. 341e3187a45SGreg Kurz */ 342*4751fd53SGreg Kurz 343*4751fd53SGreg Kurz /* First, we clear non-racing symlinks out of the way. */ 344*4751fd53SGreg Kurz if (fstatat(dirfd, name, &stbuf, AT_SYMLINK_NOFOLLOW)) { 345*4751fd53SGreg Kurz return -1; 346*4751fd53SGreg Kurz } 347*4751fd53SGreg Kurz if (S_ISLNK(stbuf.st_mode)) { 348*4751fd53SGreg Kurz errno = ELOOP; 349*4751fd53SGreg Kurz return -1; 350*4751fd53SGreg Kurz } 351*4751fd53SGreg Kurz 352*4751fd53SGreg Kurz /* Access modes are ignored when O_PATH is supported. We try O_RDONLY and 353*4751fd53SGreg Kurz * O_WRONLY for old-systems that don't support O_PATH. 354*4751fd53SGreg Kurz */ 355*4751fd53SGreg Kurz fd = openat_file(dirfd, name, O_RDONLY | O_PATH_9P_UTIL, 0); 356*4751fd53SGreg Kurz #if O_PATH_9P_UTIL == 0 357e3187a45SGreg Kurz if (fd == -1) { 358e3187a45SGreg Kurz /* In case the file is writable-only and isn't a directory. */ 359e3187a45SGreg Kurz if (errno == EACCES) { 360e3187a45SGreg Kurz fd = openat_file(dirfd, name, O_WRONLY, 0); 361e3187a45SGreg Kurz } 362e3187a45SGreg Kurz if (fd == -1 && errno == EISDIR) { 363e3187a45SGreg Kurz errno = EACCES; 364e3187a45SGreg Kurz } 365e3187a45SGreg Kurz } 366e3187a45SGreg Kurz if (fd == -1) { 367e3187a45SGreg Kurz return -1; 368e3187a45SGreg Kurz } 369e3187a45SGreg Kurz ret = fchmod(fd, mode); 370*4751fd53SGreg Kurz #else 371*4751fd53SGreg Kurz if (fd == -1) { 372*4751fd53SGreg Kurz return -1; 373*4751fd53SGreg Kurz } 374*4751fd53SGreg Kurz 375*4751fd53SGreg Kurz /* Now we handle racing symlinks. */ 376*4751fd53SGreg Kurz ret = fstat(fd, &stbuf); 377*4751fd53SGreg Kurz if (!ret) { 378*4751fd53SGreg Kurz if (S_ISLNK(stbuf.st_mode)) { 379*4751fd53SGreg Kurz errno = ELOOP; 380*4751fd53SGreg Kurz ret = -1; 381*4751fd53SGreg Kurz } else { 382*4751fd53SGreg Kurz char *proc_path = g_strdup_printf("/proc/self/fd/%d", fd); 383*4751fd53SGreg Kurz ret = chmod(proc_path, mode); 384*4751fd53SGreg Kurz g_free(proc_path); 385*4751fd53SGreg Kurz } 386*4751fd53SGreg Kurz } 387*4751fd53SGreg Kurz #endif 388e3187a45SGreg Kurz close_preserve_errno(fd); 3892c30dd74SAneesh Kumar K.V return ret; 3902c30dd74SAneesh Kumar K.V } 3912c30dd74SAneesh Kumar K.V 392e3187a45SGreg Kurz static int local_set_xattrat(int dirfd, const char *path, FsCred *credp) 393131dcb25SAnthony Liguori { 394758e8e38SVenkateswararao Jujjuri (JV) int err; 3952289be19SAneesh Kumar K.V 396758e8e38SVenkateswararao Jujjuri (JV) if (credp->fc_uid != -1) { 397f8ad4a89SAneesh Kumar K.V uint32_t tmp_uid = cpu_to_le32(credp->fc_uid); 398e3187a45SGreg Kurz err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.uid", &tmp_uid, 399e3187a45SGreg Kurz sizeof(uid_t), 0); 400758e8e38SVenkateswararao Jujjuri (JV) if (err) { 401758e8e38SVenkateswararao Jujjuri (JV) return err; 402131dcb25SAnthony Liguori } 403131dcb25SAnthony Liguori } 404758e8e38SVenkateswararao Jujjuri (JV) if (credp->fc_gid != -1) { 405f8ad4a89SAneesh Kumar K.V uint32_t tmp_gid = cpu_to_le32(credp->fc_gid); 406e3187a45SGreg Kurz err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.gid", &tmp_gid, 407e3187a45SGreg Kurz sizeof(gid_t), 0); 408758e8e38SVenkateswararao Jujjuri (JV) if (err) { 409758e8e38SVenkateswararao Jujjuri (JV) return err; 410131dcb25SAnthony Liguori } 411131dcb25SAnthony Liguori } 412758e8e38SVenkateswararao Jujjuri (JV) if (credp->fc_mode != -1) { 413f8ad4a89SAneesh Kumar K.V uint32_t tmp_mode = cpu_to_le32(credp->fc_mode); 414e3187a45SGreg Kurz err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.mode", &tmp_mode, 415e3187a45SGreg Kurz sizeof(mode_t), 0); 416758e8e38SVenkateswararao Jujjuri (JV) if (err) { 417758e8e38SVenkateswararao Jujjuri (JV) return err; 418131dcb25SAnthony Liguori } 419131dcb25SAnthony Liguori } 420758e8e38SVenkateswararao Jujjuri (JV) if (credp->fc_rdev != -1) { 421f8ad4a89SAneesh Kumar K.V uint64_t tmp_rdev = cpu_to_le64(credp->fc_rdev); 422e3187a45SGreg Kurz err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.rdev", &tmp_rdev, 423e3187a45SGreg Kurz sizeof(dev_t), 0); 424758e8e38SVenkateswararao Jujjuri (JV) if (err) { 425758e8e38SVenkateswararao Jujjuri (JV) return err; 426131dcb25SAnthony Liguori } 427758e8e38SVenkateswararao Jujjuri (JV) } 428131dcb25SAnthony Liguori return 0; 429131dcb25SAnthony Liguori } 430131dcb25SAnthony Liguori 431d815e721SGreg Kurz static int local_set_cred_passthrough(FsContext *fs_ctx, int dirfd, 432d815e721SGreg Kurz const char *name, FsCred *credp) 4334750a96fSVenkateswararao Jujjuri (JV) { 434d815e721SGreg Kurz if (fchownat(dirfd, name, credp->fc_uid, credp->fc_gid, 435b314f6a0SGreg Kurz AT_SYMLINK_NOFOLLOW) < 0) { 43612848bfcSAneesh Kumar K.V /* 43712848bfcSAneesh Kumar K.V * If we fail to change ownership and if we are 43812848bfcSAneesh Kumar K.V * using security model none. Ignore the error 43912848bfcSAneesh Kumar K.V */ 440b97400caSAneesh Kumar K.V if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) { 4414fa4ce71SChen Gang return -1; 4424750a96fSVenkateswararao Jujjuri (JV) } 443d815e721SGreg Kurz } 444d815e721SGreg Kurz 445d815e721SGreg Kurz return fchmodat_nofollow(dirfd, name, credp->fc_mode & 07777); 446d815e721SGreg Kurz } 4474750a96fSVenkateswararao Jujjuri (JV) 4482289be19SAneesh Kumar K.V static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path, 449131dcb25SAnthony Liguori char *buf, size_t bufsz) 450131dcb25SAnthony Liguori { 451879c2813SVenkateswararao Jujjuri (JV) ssize_t tsize = -1; 4522289be19SAneesh Kumar K.V 4532c30dd74SAneesh Kumar K.V if ((fs_ctx->export_flags & V9FS_SM_MAPPED) || 4542c30dd74SAneesh Kumar K.V (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) { 455879c2813SVenkateswararao Jujjuri (JV) int fd; 456bec1e954SGreg Kurz 457bec1e954SGreg Kurz fd = local_open_nofollow(fs_ctx, fs_path->data, O_RDONLY, 0); 458879c2813SVenkateswararao Jujjuri (JV) if (fd == -1) { 459879c2813SVenkateswararao Jujjuri (JV) return -1; 460879c2813SVenkateswararao Jujjuri (JV) } 461879c2813SVenkateswararao Jujjuri (JV) do { 462879c2813SVenkateswararao Jujjuri (JV) tsize = read(fd, (void *)buf, bufsz); 463879c2813SVenkateswararao Jujjuri (JV) } while (tsize == -1 && errno == EINTR); 464bec1e954SGreg Kurz close_preserve_errno(fd); 465b97400caSAneesh Kumar K.V } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 466b97400caSAneesh Kumar K.V (fs_ctx->export_flags & V9FS_SM_NONE)) { 467bec1e954SGreg Kurz char *dirpath = g_path_get_dirname(fs_path->data); 468bec1e954SGreg Kurz char *name = g_path_get_basename(fs_path->data); 469bec1e954SGreg Kurz int dirfd; 470bec1e954SGreg Kurz 471bec1e954SGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dirpath); 472bec1e954SGreg Kurz if (dirfd == -1) { 473bec1e954SGreg Kurz goto out; 474bec1e954SGreg Kurz } 475bec1e954SGreg Kurz 476bec1e954SGreg Kurz tsize = readlinkat(dirfd, name, buf, bufsz); 477bec1e954SGreg Kurz close_preserve_errno(dirfd); 478bec1e954SGreg Kurz out: 479bec1e954SGreg Kurz g_free(name); 480bec1e954SGreg Kurz g_free(dirpath); 481879c2813SVenkateswararao Jujjuri (JV) } 482879c2813SVenkateswararao Jujjuri (JV) return tsize; 483131dcb25SAnthony Liguori } 484131dcb25SAnthony Liguori 485cc720ddbSAneesh Kumar K.V static int local_close(FsContext *ctx, V9fsFidOpenState *fs) 486131dcb25SAnthony Liguori { 487cc720ddbSAneesh Kumar K.V return close(fs->fd); 488131dcb25SAnthony Liguori } 489131dcb25SAnthony Liguori 490cc720ddbSAneesh Kumar K.V static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs) 491131dcb25SAnthony Liguori { 492f314ea4eSGreg Kurz return closedir(fs->dir.stream); 493131dcb25SAnthony Liguori } 4949f107513SAnthony Liguori 495cc720ddbSAneesh Kumar K.V static int local_open(FsContext *ctx, V9fsPath *fs_path, 496cc720ddbSAneesh Kumar K.V int flags, V9fsFidOpenState *fs) 497a6568fe2SAnthony Liguori { 49821328e1eSGreg Kurz int fd; 4992289be19SAneesh Kumar K.V 500996a0d76SGreg Kurz fd = local_open_nofollow(ctx, fs_path->data, flags, 0); 50121328e1eSGreg Kurz if (fd == -1) { 50221328e1eSGreg Kurz return -1; 50321328e1eSGreg Kurz } 50421328e1eSGreg Kurz fs->fd = fd; 505cc720ddbSAneesh Kumar K.V return fs->fd; 506a6568fe2SAnthony Liguori } 507a6568fe2SAnthony Liguori 508cc720ddbSAneesh Kumar K.V static int local_opendir(FsContext *ctx, 509cc720ddbSAneesh Kumar K.V V9fsPath *fs_path, V9fsFidOpenState *fs) 510a6568fe2SAnthony Liguori { 511996a0d76SGreg Kurz int dirfd; 51221328e1eSGreg Kurz DIR *stream; 5132289be19SAneesh Kumar K.V 514996a0d76SGreg Kurz dirfd = local_opendir_nofollow(ctx, fs_path->data); 515996a0d76SGreg Kurz if (dirfd == -1) { 516cc720ddbSAneesh Kumar K.V return -1; 517cc720ddbSAneesh Kumar K.V } 518996a0d76SGreg Kurz 519996a0d76SGreg Kurz stream = fdopendir(dirfd); 52021328e1eSGreg Kurz if (!stream) { 521faab207fSGreg Kurz close(dirfd); 522cc720ddbSAneesh Kumar K.V return -1; 523cc720ddbSAneesh Kumar K.V } 52421328e1eSGreg Kurz fs->dir.stream = stream; 525cc720ddbSAneesh Kumar K.V return 0; 526a6568fe2SAnthony Liguori } 527a6568fe2SAnthony Liguori 528cc720ddbSAneesh Kumar K.V static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs) 529a9231555SAnthony Liguori { 530f314ea4eSGreg Kurz rewinddir(fs->dir.stream); 531a9231555SAnthony Liguori } 532a9231555SAnthony Liguori 533cc720ddbSAneesh Kumar K.V static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs) 534a9231555SAnthony Liguori { 535f314ea4eSGreg Kurz return telldir(fs->dir.stream); 536a9231555SAnthony Liguori } 537a9231555SAnthony Liguori 5387a95434eSGreg Kurz static bool local_is_mapped_file_metadata(FsContext *fs_ctx, const char *name) 5397a95434eSGreg Kurz { 54081ffbf5aSGreg Kurz return 54181ffbf5aSGreg Kurz !strcmp(name, VIRTFS_META_DIR) || !strcmp(name, VIRTFS_META_ROOT_FILE); 5427a95434eSGreg Kurz } 5437a95434eSGreg Kurz 544635324e8SGreg Kurz static struct dirent *local_readdir(FsContext *ctx, V9fsFidOpenState *fs) 545a9231555SAnthony Liguori { 546635324e8SGreg Kurz struct dirent *entry; 5472c30dd74SAneesh Kumar K.V 5482c30dd74SAneesh Kumar K.V again: 549635324e8SGreg Kurz entry = readdir(fs->dir.stream); 550635324e8SGreg Kurz if (!entry) { 551635324e8SGreg Kurz return NULL; 552635324e8SGreg Kurz } 553635324e8SGreg Kurz 554840a1bf2SBastian Blank if (ctx->export_flags & V9FS_SM_MAPPED) { 555840a1bf2SBastian Blank entry->d_type = DT_UNKNOWN; 556840a1bf2SBastian Blank } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 5577a95434eSGreg Kurz if (local_is_mapped_file_metadata(ctx, entry->d_name)) { 55881ffbf5aSGreg Kurz /* skip the meta data */ 5592c30dd74SAneesh Kumar K.V goto again; 5602c30dd74SAneesh Kumar K.V } 561840a1bf2SBastian Blank entry->d_type = DT_UNKNOWN; 5622c30dd74SAneesh Kumar K.V } 563635324e8SGreg Kurz 564635324e8SGreg Kurz return entry; 565a9231555SAnthony Liguori } 566a9231555SAnthony Liguori 567cc720ddbSAneesh Kumar K.V static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off) 568a9231555SAnthony Liguori { 569f314ea4eSGreg Kurz seekdir(fs->dir.stream, off); 570a9231555SAnthony Liguori } 571a9231555SAnthony Liguori 572cc720ddbSAneesh Kumar K.V static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs, 573cc720ddbSAneesh Kumar K.V const struct iovec *iov, 57456d15a53SSanchit Garg int iovcnt, off_t offset) 575a9231555SAnthony Liguori { 57656d15a53SSanchit Garg #ifdef CONFIG_PREADV 577cc720ddbSAneesh Kumar K.V return preadv(fs->fd, iov, iovcnt, offset); 57856d15a53SSanchit Garg #else 579cc720ddbSAneesh Kumar K.V int err = lseek(fs->fd, offset, SEEK_SET); 58056d15a53SSanchit Garg if (err == -1) { 58156d15a53SSanchit Garg return err; 58256d15a53SSanchit Garg } else { 583cc720ddbSAneesh Kumar K.V return readv(fs->fd, iov, iovcnt); 584a9231555SAnthony Liguori } 58556d15a53SSanchit Garg #endif 586a9231555SAnthony Liguori } 587a9231555SAnthony Liguori 588cc720ddbSAneesh Kumar K.V static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs, 589cc720ddbSAneesh Kumar K.V const struct iovec *iov, 59056d15a53SSanchit Garg int iovcnt, off_t offset) 5918449360cSAnthony Liguori { 5926fe76accSGreg Kurz ssize_t ret; 59356d15a53SSanchit Garg #ifdef CONFIG_PREADV 594cc720ddbSAneesh Kumar K.V ret = pwritev(fs->fd, iov, iovcnt, offset); 59556d15a53SSanchit Garg #else 596cc720ddbSAneesh Kumar K.V int err = lseek(fs->fd, offset, SEEK_SET); 59756d15a53SSanchit Garg if (err == -1) { 59856d15a53SSanchit Garg return err; 59956d15a53SSanchit Garg } else { 600cc720ddbSAneesh Kumar K.V ret = writev(fs->fd, iov, iovcnt); 6018449360cSAnthony Liguori } 60256d15a53SSanchit Garg #endif 603d3ab98e6SAneesh Kumar K.V #ifdef CONFIG_SYNC_FILE_RANGE 604d3ab98e6SAneesh Kumar K.V if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) { 605d3ab98e6SAneesh Kumar K.V /* 606d3ab98e6SAneesh Kumar K.V * Initiate a writeback. This is not a data integrity sync. 607d3ab98e6SAneesh Kumar K.V * We want to ensure that we don't leave dirty pages in the cache 608d3ab98e6SAneesh Kumar K.V * after write when writeout=immediate is sepcified. 609d3ab98e6SAneesh Kumar K.V */ 610cc720ddbSAneesh Kumar K.V sync_file_range(fs->fd, offset, ret, 611d3ab98e6SAneesh Kumar K.V SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE); 612d3ab98e6SAneesh Kumar K.V } 613d3ab98e6SAneesh Kumar K.V #endif 614d3ab98e6SAneesh Kumar K.V return ret; 61556d15a53SSanchit Garg } 6168449360cSAnthony Liguori 6172289be19SAneesh Kumar K.V static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp) 618c494dd6fSAnthony Liguori { 619e3187a45SGreg Kurz char *dirpath = g_path_get_dirname(fs_path->data); 620e3187a45SGreg Kurz char *name = g_path_get_basename(fs_path->data); 6214fa4ce71SChen Gang int ret = -1; 622e3187a45SGreg Kurz int dirfd; 623e3187a45SGreg Kurz 624e3187a45SGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dirpath); 625e3187a45SGreg Kurz if (dirfd == -1) { 626e3187a45SGreg Kurz goto out; 627e3187a45SGreg Kurz } 6282289be19SAneesh Kumar K.V 629b97400caSAneesh Kumar K.V if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 630e3187a45SGreg Kurz ret = local_set_xattrat(dirfd, name, credp); 6312c30dd74SAneesh Kumar K.V } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 632e3187a45SGreg Kurz ret = local_set_mapped_file_attrat(dirfd, name, credp); 633e3187a45SGreg Kurz } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH || 634e3187a45SGreg Kurz fs_ctx->export_flags & V9FS_SM_NONE) { 635e3187a45SGreg Kurz ret = fchmodat_nofollow(dirfd, name, credp->fc_mode); 636e95ead32SVenkateswararao Jujjuri (JV) } 637e3187a45SGreg Kurz close_preserve_errno(dirfd); 638e3187a45SGreg Kurz 639e3187a45SGreg Kurz out: 640e3187a45SGreg Kurz g_free(dirpath); 641e3187a45SGreg Kurz g_free(name); 6424fa4ce71SChen Gang return ret; 643c494dd6fSAnthony Liguori } 644c494dd6fSAnthony Liguori 6452289be19SAneesh Kumar K.V static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path, 6462289be19SAneesh Kumar K.V const char *name, FsCred *credp) 647c494dd6fSAnthony Liguori { 6481c293312SVenkateswararao Jujjuri (JV) int err = -1; 649d815e721SGreg Kurz int dirfd; 6501c293312SVenkateswararao Jujjuri (JV) 6517a95434eSGreg Kurz if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE && 6527a95434eSGreg Kurz local_is_mapped_file_metadata(fs_ctx, name)) { 6537a95434eSGreg Kurz errno = EINVAL; 6547a95434eSGreg Kurz return -1; 6557a95434eSGreg Kurz } 6567a95434eSGreg Kurz 657d815e721SGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dir_path->data); 658d815e721SGreg Kurz if (dirfd == -1) { 659d815e721SGreg Kurz return -1; 660d815e721SGreg Kurz } 6612289be19SAneesh Kumar K.V 662d815e721SGreg Kurz if (fs_ctx->export_flags & V9FS_SM_MAPPED || 663d815e721SGreg Kurz fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 664b96feb2cSTobias Schramm err = mknodat(dirfd, name, fs_ctx->fmode | S_IFREG, 0); 665d815e721SGreg Kurz if (err == -1) { 666d815e721SGreg Kurz goto out; 667d815e721SGreg Kurz } 668d815e721SGreg Kurz 669b97400caSAneesh Kumar K.V if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 670d815e721SGreg Kurz err = local_set_xattrat(dirfd, name, credp); 671d815e721SGreg Kurz } else { 672d815e721SGreg Kurz err = local_set_mapped_file_attrat(dirfd, name, credp); 6731c293312SVenkateswararao Jujjuri (JV) } 6741c293312SVenkateswararao Jujjuri (JV) if (err == -1) { 6751c293312SVenkateswararao Jujjuri (JV) goto err_end; 6761c293312SVenkateswararao Jujjuri (JV) } 677d815e721SGreg Kurz } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH || 678d815e721SGreg Kurz fs_ctx->export_flags & V9FS_SM_NONE) { 679d815e721SGreg Kurz err = mknodat(dirfd, name, credp->fc_mode, credp->fc_rdev); 6802c30dd74SAneesh Kumar K.V if (err == -1) { 6812c30dd74SAneesh Kumar K.V goto out; 6822c30dd74SAneesh Kumar K.V } 683d815e721SGreg Kurz err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp); 6842c30dd74SAneesh Kumar K.V if (err == -1) { 6851c293312SVenkateswararao Jujjuri (JV) goto err_end; 6861c293312SVenkateswararao Jujjuri (JV) } 6871c293312SVenkateswararao Jujjuri (JV) } 6882289be19SAneesh Kumar K.V goto out; 6891c293312SVenkateswararao Jujjuri (JV) 6901c293312SVenkateswararao Jujjuri (JV) err_end: 691d815e721SGreg Kurz unlinkat_preserve_errno(dirfd, name, 0); 6922289be19SAneesh Kumar K.V out: 693d815e721SGreg Kurz close_preserve_errno(dirfd); 6941c293312SVenkateswararao Jujjuri (JV) return err; 695c494dd6fSAnthony Liguori } 696c494dd6fSAnthony Liguori 6972289be19SAneesh Kumar K.V static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path, 6982289be19SAneesh Kumar K.V const char *name, FsCred *credp) 699c494dd6fSAnthony Liguori { 70000ec5c37SVenkateswararao Jujjuri (JV) int err = -1; 7013f3a1699SGreg Kurz int dirfd; 70200ec5c37SVenkateswararao Jujjuri (JV) 7037a95434eSGreg Kurz if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE && 7047a95434eSGreg Kurz local_is_mapped_file_metadata(fs_ctx, name)) { 7057a95434eSGreg Kurz errno = EINVAL; 7067a95434eSGreg Kurz return -1; 7077a95434eSGreg Kurz } 7087a95434eSGreg Kurz 7093f3a1699SGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dir_path->data); 7103f3a1699SGreg Kurz if (dirfd == -1) { 7113f3a1699SGreg Kurz return -1; 7123f3a1699SGreg Kurz } 7132289be19SAneesh Kumar K.V 7143f3a1699SGreg Kurz if (fs_ctx->export_flags & V9FS_SM_MAPPED || 7153f3a1699SGreg Kurz fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 716b96feb2cSTobias Schramm err = mkdirat(dirfd, name, fs_ctx->dmode); 7173f3a1699SGreg Kurz if (err == -1) { 7183f3a1699SGreg Kurz goto out; 7193f3a1699SGreg Kurz } 7203f3a1699SGreg Kurz credp->fc_mode = credp->fc_mode | S_IFDIR; 7213f3a1699SGreg Kurz 722b97400caSAneesh Kumar K.V if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 7233f3a1699SGreg Kurz err = local_set_xattrat(dirfd, name, credp); 7243f3a1699SGreg Kurz } else { 7253f3a1699SGreg Kurz err = local_set_mapped_file_attrat(dirfd, name, credp); 72600ec5c37SVenkateswararao Jujjuri (JV) } 72700ec5c37SVenkateswararao Jujjuri (JV) if (err == -1) { 72800ec5c37SVenkateswararao Jujjuri (JV) goto err_end; 72900ec5c37SVenkateswararao Jujjuri (JV) } 7303f3a1699SGreg Kurz } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH || 7313f3a1699SGreg Kurz fs_ctx->export_flags & V9FS_SM_NONE) { 7323f3a1699SGreg Kurz err = mkdirat(dirfd, name, credp->fc_mode); 7332c30dd74SAneesh Kumar K.V if (err == -1) { 7342c30dd74SAneesh Kumar K.V goto out; 7352c30dd74SAneesh Kumar K.V } 7363f3a1699SGreg Kurz err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp); 7372c30dd74SAneesh Kumar K.V if (err == -1) { 73800ec5c37SVenkateswararao Jujjuri (JV) goto err_end; 73900ec5c37SVenkateswararao Jujjuri (JV) } 74000ec5c37SVenkateswararao Jujjuri (JV) } 7412289be19SAneesh Kumar K.V goto out; 74200ec5c37SVenkateswararao Jujjuri (JV) 74300ec5c37SVenkateswararao Jujjuri (JV) err_end: 7443f3a1699SGreg Kurz unlinkat_preserve_errno(dirfd, name, AT_REMOVEDIR); 7452289be19SAneesh Kumar K.V out: 7463f3a1699SGreg Kurz close_preserve_errno(dirfd); 74700ec5c37SVenkateswararao Jujjuri (JV) return err; 748c494dd6fSAnthony Liguori } 749c494dd6fSAnthony Liguori 7508b888272SAneesh Kumar K.V static int local_fstat(FsContext *fs_ctx, int fid_type, 751cc720ddbSAneesh Kumar K.V V9fsFidOpenState *fs, struct stat *stbuf) 752c494dd6fSAnthony Liguori { 7538b888272SAneesh Kumar K.V int err, fd; 7548b888272SAneesh Kumar K.V 7558b888272SAneesh Kumar K.V if (fid_type == P9_FID_DIR) { 756f314ea4eSGreg Kurz fd = dirfd(fs->dir.stream); 7578b888272SAneesh Kumar K.V } else { 7588b888272SAneesh Kumar K.V fd = fs->fd; 7598b888272SAneesh Kumar K.V } 7608b888272SAneesh Kumar K.V 7618b888272SAneesh Kumar K.V err = fstat(fd, stbuf); 7621237ad76SVenkateswararao Jujjuri (JV) if (err) { 7631237ad76SVenkateswararao Jujjuri (JV) return err; 7641237ad76SVenkateswararao Jujjuri (JV) } 765b97400caSAneesh Kumar K.V if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 7661237ad76SVenkateswararao Jujjuri (JV) /* Actual credentials are part of extended attrs */ 7671237ad76SVenkateswararao Jujjuri (JV) uid_t tmp_uid; 7681237ad76SVenkateswararao Jujjuri (JV) gid_t tmp_gid; 7691237ad76SVenkateswararao Jujjuri (JV) mode_t tmp_mode; 7701237ad76SVenkateswararao Jujjuri (JV) dev_t tmp_dev; 7711237ad76SVenkateswararao Jujjuri (JV) 772f8ad4a89SAneesh Kumar K.V if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) { 773f8ad4a89SAneesh Kumar K.V stbuf->st_uid = le32_to_cpu(tmp_uid); 7741237ad76SVenkateswararao Jujjuri (JV) } 775f8ad4a89SAneesh Kumar K.V if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) { 776f8ad4a89SAneesh Kumar K.V stbuf->st_gid = le32_to_cpu(tmp_gid); 7771237ad76SVenkateswararao Jujjuri (JV) } 778f8ad4a89SAneesh Kumar K.V if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) { 779f8ad4a89SAneesh Kumar K.V stbuf->st_mode = le32_to_cpu(tmp_mode); 7801237ad76SVenkateswararao Jujjuri (JV) } 781f8ad4a89SAneesh Kumar K.V if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) { 782f8ad4a89SAneesh Kumar K.V stbuf->st_rdev = le64_to_cpu(tmp_dev); 7831237ad76SVenkateswararao Jujjuri (JV) } 7842c30dd74SAneesh Kumar K.V } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 7852c30dd74SAneesh Kumar K.V errno = EOPNOTSUPP; 7862c30dd74SAneesh Kumar K.V return -1; 7871237ad76SVenkateswararao Jujjuri (JV) } 7881237ad76SVenkateswararao Jujjuri (JV) return err; 789c494dd6fSAnthony Liguori } 790c494dd6fSAnthony Liguori 7912289be19SAneesh Kumar K.V static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name, 792cc720ddbSAneesh Kumar K.V int flags, FsCred *credp, V9fsFidOpenState *fs) 793c494dd6fSAnthony Liguori { 7944750a96fSVenkateswararao Jujjuri (JV) int fd = -1; 7954750a96fSVenkateswararao Jujjuri (JV) int err = -1; 796a565fea5SGreg Kurz int dirfd; 7974750a96fSVenkateswararao Jujjuri (JV) 7987a95434eSGreg Kurz if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE && 7997a95434eSGreg Kurz local_is_mapped_file_metadata(fs_ctx, name)) { 8007a95434eSGreg Kurz errno = EINVAL; 8017a95434eSGreg Kurz return -1; 8027a95434eSGreg Kurz } 8037a95434eSGreg Kurz 8040ceb092eSAneesh Kumar K.V /* 8050ceb092eSAneesh Kumar K.V * Mark all the open to not follow symlinks 8060ceb092eSAneesh Kumar K.V */ 8070ceb092eSAneesh Kumar K.V flags |= O_NOFOLLOW; 8080ceb092eSAneesh Kumar K.V 809a565fea5SGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dir_path->data); 810a565fea5SGreg Kurz if (dirfd == -1) { 811a565fea5SGreg Kurz return -1; 812a565fea5SGreg Kurz } 8132289be19SAneesh Kumar K.V 8144750a96fSVenkateswararao Jujjuri (JV) /* Determine the security model */ 815a565fea5SGreg Kurz if (fs_ctx->export_flags & V9FS_SM_MAPPED || 816a565fea5SGreg Kurz fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 817b96feb2cSTobias Schramm fd = openat_file(dirfd, name, flags, fs_ctx->fmode); 818a565fea5SGreg Kurz if (fd == -1) { 819a565fea5SGreg Kurz goto out; 820a565fea5SGreg Kurz } 821a565fea5SGreg Kurz credp->fc_mode = credp->fc_mode|S_IFREG; 822b97400caSAneesh Kumar K.V if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 8234750a96fSVenkateswararao Jujjuri (JV) /* Set cleint credentials in xattr */ 824a565fea5SGreg Kurz err = local_set_xattrat(dirfd, name, credp); 825a565fea5SGreg Kurz } else { 826a565fea5SGreg Kurz err = local_set_mapped_file_attrat(dirfd, name, credp); 8274750a96fSVenkateswararao Jujjuri (JV) } 8282c30dd74SAneesh Kumar K.V if (err == -1) { 8292c30dd74SAneesh Kumar K.V goto err_end; 8302c30dd74SAneesh Kumar K.V } 831b97400caSAneesh Kumar K.V } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 832b97400caSAneesh Kumar K.V (fs_ctx->export_flags & V9FS_SM_NONE)) { 833a565fea5SGreg Kurz fd = openat_file(dirfd, name, flags, credp->fc_mode); 8344750a96fSVenkateswararao Jujjuri (JV) if (fd == -1) { 8352289be19SAneesh Kumar K.V goto out; 8364750a96fSVenkateswararao Jujjuri (JV) } 837a565fea5SGreg Kurz err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp); 8384750a96fSVenkateswararao Jujjuri (JV) if (err == -1) { 8394750a96fSVenkateswararao Jujjuri (JV) goto err_end; 8404750a96fSVenkateswararao Jujjuri (JV) } 8414750a96fSVenkateswararao Jujjuri (JV) } 8422289be19SAneesh Kumar K.V err = fd; 843cc720ddbSAneesh Kumar K.V fs->fd = fd; 8442289be19SAneesh Kumar K.V goto out; 8454750a96fSVenkateswararao Jujjuri (JV) 8464750a96fSVenkateswararao Jujjuri (JV) err_end: 847a565fea5SGreg Kurz unlinkat_preserve_errno(dirfd, name, 848a565fea5SGreg Kurz flags & O_DIRECTORY ? AT_REMOVEDIR : 0); 849a565fea5SGreg Kurz close_preserve_errno(fd); 8502289be19SAneesh Kumar K.V out: 851a565fea5SGreg Kurz close_preserve_errno(dirfd); 8524750a96fSVenkateswararao Jujjuri (JV) return err; 853c494dd6fSAnthony Liguori } 854c494dd6fSAnthony Liguori 855758e8e38SVenkateswararao Jujjuri (JV) 856879c2813SVenkateswararao Jujjuri (JV) static int local_symlink(FsContext *fs_ctx, const char *oldpath, 8572289be19SAneesh Kumar K.V V9fsPath *dir_path, const char *name, FsCred *credp) 858c494dd6fSAnthony Liguori { 859879c2813SVenkateswararao Jujjuri (JV) int err = -1; 86038771613SGreg Kurz int dirfd; 861879c2813SVenkateswararao Jujjuri (JV) 8627a95434eSGreg Kurz if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE && 8637a95434eSGreg Kurz local_is_mapped_file_metadata(fs_ctx, name)) { 8647a95434eSGreg Kurz errno = EINVAL; 8657a95434eSGreg Kurz return -1; 8667a95434eSGreg Kurz } 8677a95434eSGreg Kurz 86838771613SGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dir_path->data); 86938771613SGreg Kurz if (dirfd == -1) { 87038771613SGreg Kurz return -1; 87138771613SGreg Kurz } 8722289be19SAneesh Kumar K.V 873879c2813SVenkateswararao Jujjuri (JV) /* Determine the security model */ 87438771613SGreg Kurz if (fs_ctx->export_flags & V9FS_SM_MAPPED || 87538771613SGreg Kurz fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 87638771613SGreg Kurz int fd; 87738771613SGreg Kurz ssize_t oldpath_size, write_size; 87838771613SGreg Kurz 87938771613SGreg Kurz fd = openat_file(dirfd, name, O_CREAT | O_EXCL | O_RDWR, 880b96feb2cSTobias Schramm fs_ctx->fmode); 88138771613SGreg Kurz if (fd == -1) { 88238771613SGreg Kurz goto out; 88338771613SGreg Kurz } 88438771613SGreg Kurz /* Write the oldpath (target) to the file. */ 88538771613SGreg Kurz oldpath_size = strlen(oldpath); 88638771613SGreg Kurz do { 88738771613SGreg Kurz write_size = write(fd, (void *)oldpath, oldpath_size); 88838771613SGreg Kurz } while (write_size == -1 && errno == EINTR); 88938771613SGreg Kurz close_preserve_errno(fd); 89038771613SGreg Kurz 89138771613SGreg Kurz if (write_size != oldpath_size) { 89238771613SGreg Kurz goto err_end; 89338771613SGreg Kurz } 89438771613SGreg Kurz /* Set cleint credentials in symlink's xattr */ 89538771613SGreg Kurz credp->fc_mode = credp->fc_mode | S_IFLNK; 89638771613SGreg Kurz 897b97400caSAneesh Kumar K.V if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 89838771613SGreg Kurz err = local_set_xattrat(dirfd, name, credp); 89938771613SGreg Kurz } else { 90038771613SGreg Kurz err = local_set_mapped_file_attrat(dirfd, name, credp); 901879c2813SVenkateswararao Jujjuri (JV) } 902879c2813SVenkateswararao Jujjuri (JV) if (err == -1) { 903879c2813SVenkateswararao Jujjuri (JV) goto err_end; 904879c2813SVenkateswararao Jujjuri (JV) } 90538771613SGreg Kurz } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH || 90638771613SGreg Kurz fs_ctx->export_flags & V9FS_SM_NONE) { 90738771613SGreg Kurz err = symlinkat(oldpath, dirfd, name); 908879c2813SVenkateswararao Jujjuri (JV) if (err) { 9092289be19SAneesh Kumar K.V goto out; 910879c2813SVenkateswararao Jujjuri (JV) } 91138771613SGreg Kurz err = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid, 91238771613SGreg Kurz AT_SYMLINK_NOFOLLOW); 913879c2813SVenkateswararao Jujjuri (JV) if (err == -1) { 91412848bfcSAneesh Kumar K.V /* 91512848bfcSAneesh Kumar K.V * If we fail to change ownership and if we are 91612848bfcSAneesh Kumar K.V * using security model none. Ignore the error 91712848bfcSAneesh Kumar K.V */ 918b97400caSAneesh Kumar K.V if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) { 919879c2813SVenkateswararao Jujjuri (JV) goto err_end; 92038771613SGreg Kurz } else { 92112848bfcSAneesh Kumar K.V err = 0; 922879c2813SVenkateswararao Jujjuri (JV) } 923879c2813SVenkateswararao Jujjuri (JV) } 92438771613SGreg Kurz } 9252289be19SAneesh Kumar K.V goto out; 926879c2813SVenkateswararao Jujjuri (JV) 927879c2813SVenkateswararao Jujjuri (JV) err_end: 92838771613SGreg Kurz unlinkat_preserve_errno(dirfd, name, 0); 9292289be19SAneesh Kumar K.V out: 93038771613SGreg Kurz close_preserve_errno(dirfd); 931879c2813SVenkateswararao Jujjuri (JV) return err; 932c494dd6fSAnthony Liguori } 933c494dd6fSAnthony Liguori 9342289be19SAneesh Kumar K.V static int local_link(FsContext *ctx, V9fsPath *oldpath, 9352289be19SAneesh Kumar K.V V9fsPath *dirpath, const char *name) 936c494dd6fSAnthony Liguori { 937ad0b46e6SGreg Kurz char *odirpath = g_path_get_dirname(oldpath->data); 938ad0b46e6SGreg Kurz char *oname = g_path_get_basename(oldpath->data); 939ad0b46e6SGreg Kurz int ret = -1; 940ad0b46e6SGreg Kurz int odirfd, ndirfd; 941c494dd6fSAnthony Liguori 9427a95434eSGreg Kurz if (ctx->export_flags & V9FS_SM_MAPPED_FILE && 9437a95434eSGreg Kurz local_is_mapped_file_metadata(ctx, name)) { 9447a95434eSGreg Kurz errno = EINVAL; 9457a95434eSGreg Kurz return -1; 9467a95434eSGreg Kurz } 9477a95434eSGreg Kurz 948ad0b46e6SGreg Kurz odirfd = local_opendir_nofollow(ctx, odirpath); 949ad0b46e6SGreg Kurz if (odirfd == -1) { 9506dd4b1f1SGreg Kurz goto out; 9516dd4b1f1SGreg Kurz } 9522289be19SAneesh Kumar K.V 953ad0b46e6SGreg Kurz ndirfd = local_opendir_nofollow(ctx, dirpath->data); 954ad0b46e6SGreg Kurz if (ndirfd == -1) { 955ad0b46e6SGreg Kurz close_preserve_errno(odirfd); 956ad0b46e6SGreg Kurz goto out; 957ad0b46e6SGreg Kurz } 958ad0b46e6SGreg Kurz 959ad0b46e6SGreg Kurz ret = linkat(odirfd, oname, ndirfd, name, 0); 960ad0b46e6SGreg Kurz if (ret < 0) { 961ad0b46e6SGreg Kurz goto out_close; 962ad0b46e6SGreg Kurz } 9632c30dd74SAneesh Kumar K.V 9642c30dd74SAneesh Kumar K.V /* now link the virtfs_metadata files */ 9656dd4b1f1SGreg Kurz if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 966ad0b46e6SGreg Kurz int omap_dirfd, nmap_dirfd; 9676dd4b1f1SGreg Kurz 968ad0b46e6SGreg Kurz ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700); 969ad0b46e6SGreg Kurz if (ret < 0 && errno != EEXIST) { 970ad0b46e6SGreg Kurz goto err_undo_link; 9712c30dd74SAneesh Kumar K.V } 972ad0b46e6SGreg Kurz 973ad0b46e6SGreg Kurz omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR); 974ad0b46e6SGreg Kurz if (omap_dirfd == -1) { 975ad0b46e6SGreg Kurz goto err; 976ad0b46e6SGreg Kurz } 977ad0b46e6SGreg Kurz 978ad0b46e6SGreg Kurz nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR); 979ad0b46e6SGreg Kurz if (nmap_dirfd == -1) { 980ad0b46e6SGreg Kurz close_preserve_errno(omap_dirfd); 981ad0b46e6SGreg Kurz goto err; 982ad0b46e6SGreg Kurz } 983ad0b46e6SGreg Kurz 984ad0b46e6SGreg Kurz ret = linkat(omap_dirfd, oname, nmap_dirfd, name, 0); 985ad0b46e6SGreg Kurz close_preserve_errno(nmap_dirfd); 986ad0b46e6SGreg Kurz close_preserve_errno(omap_dirfd); 9872c30dd74SAneesh Kumar K.V if (ret < 0 && errno != ENOENT) { 988ad0b46e6SGreg Kurz goto err_undo_link; 9892c30dd74SAneesh Kumar K.V } 9906dd4b1f1SGreg Kurz 991ad0b46e6SGreg Kurz ret = 0; 9922c30dd74SAneesh Kumar K.V } 993ad0b46e6SGreg Kurz goto out_close; 994ad0b46e6SGreg Kurz 995ad0b46e6SGreg Kurz err: 996ad0b46e6SGreg Kurz ret = -1; 997ad0b46e6SGreg Kurz err_undo_link: 998ad0b46e6SGreg Kurz unlinkat_preserve_errno(ndirfd, name, 0); 999ad0b46e6SGreg Kurz out_close: 1000ad0b46e6SGreg Kurz close_preserve_errno(ndirfd); 1001ad0b46e6SGreg Kurz close_preserve_errno(odirfd); 10026dd4b1f1SGreg Kurz out: 1003ad0b46e6SGreg Kurz g_free(oname); 1004ad0b46e6SGreg Kurz g_free(odirpath); 10052289be19SAneesh Kumar K.V return ret; 1006c494dd6fSAnthony Liguori } 1007c494dd6fSAnthony Liguori 10082289be19SAneesh Kumar K.V static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size) 10098cf89e00SAnthony Liguori { 1010ac125d99SGreg Kurz int fd, ret; 10112289be19SAneesh Kumar K.V 1012ac125d99SGreg Kurz fd = local_open_nofollow(ctx, fs_path->data, O_WRONLY, 0); 1013ac125d99SGreg Kurz if (fd == -1) { 1014ac125d99SGreg Kurz return -1; 1015ac125d99SGreg Kurz } 1016ac125d99SGreg Kurz ret = ftruncate(fd, size); 1017ac125d99SGreg Kurz close_preserve_errno(fd); 10184fa4ce71SChen Gang return ret; 10198cf89e00SAnthony Liguori } 10208cf89e00SAnthony Liguori 10212289be19SAneesh Kumar K.V static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp) 10228cf89e00SAnthony Liguori { 1023d369f207SGreg Kurz char *dirpath = g_path_get_dirname(fs_path->data); 1024d369f207SGreg Kurz char *name = g_path_get_basename(fs_path->data); 10254fa4ce71SChen Gang int ret = -1; 1026d369f207SGreg Kurz int dirfd; 1027d369f207SGreg Kurz 1028d369f207SGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dirpath); 1029d369f207SGreg Kurz if (dirfd == -1) { 1030d369f207SGreg Kurz goto out; 1031d369f207SGreg Kurz } 10322289be19SAneesh Kumar K.V 1033c79ce737SSripathi Kodi if ((credp->fc_uid == -1 && credp->fc_gid == -1) || 103417b1971fSAneesh Kumar K.V (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 103517b1971fSAneesh Kumar K.V (fs_ctx->export_flags & V9FS_SM_NONE)) { 1036d369f207SGreg Kurz ret = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid, 1037d369f207SGreg Kurz AT_SYMLINK_NOFOLLOW); 1038b97400caSAneesh Kumar K.V } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 1039d369f207SGreg Kurz ret = local_set_xattrat(dirfd, name, credp); 10402c30dd74SAneesh Kumar K.V } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 1041d369f207SGreg Kurz ret = local_set_mapped_file_attrat(dirfd, name, credp); 1042f7613beeSVenkateswararao Jujjuri (JV) } 1043d369f207SGreg Kurz 1044d369f207SGreg Kurz close_preserve_errno(dirfd); 1045d369f207SGreg Kurz out: 1046d369f207SGreg Kurz g_free(name); 1047d369f207SGreg Kurz g_free(dirpath); 10484fa4ce71SChen Gang return ret; 10498cf89e00SAnthony Liguori } 10508cf89e00SAnthony Liguori 10512289be19SAneesh Kumar K.V static int local_utimensat(FsContext *s, V9fsPath *fs_path, 105274bc02b2SM. Mohan Kumar const struct timespec *buf) 10538cf89e00SAnthony Liguori { 1054a33eda0dSGreg Kurz char *dirpath = g_path_get_dirname(fs_path->data); 1055a33eda0dSGreg Kurz char *name = g_path_get_basename(fs_path->data); 1056a33eda0dSGreg Kurz int dirfd, ret = -1; 10572289be19SAneesh Kumar K.V 1058a33eda0dSGreg Kurz dirfd = local_opendir_nofollow(s, dirpath); 1059a33eda0dSGreg Kurz if (dirfd == -1) { 1060a33eda0dSGreg Kurz goto out; 1061a33eda0dSGreg Kurz } 1062a33eda0dSGreg Kurz 1063a33eda0dSGreg Kurz ret = utimensat(dirfd, name, buf, AT_SYMLINK_NOFOLLOW); 1064a33eda0dSGreg Kurz close_preserve_errno(dirfd); 1065a33eda0dSGreg Kurz out: 1066a33eda0dSGreg Kurz g_free(dirpath); 1067a33eda0dSGreg Kurz g_free(name); 10684fa4ce71SChen Gang return ret; 10698cf89e00SAnthony Liguori } 10708cf89e00SAnthony Liguori 1071df4938a6SGreg Kurz static int local_unlinkat_common(FsContext *ctx, int dirfd, const char *name, 1072df4938a6SGreg Kurz int flags) 10735bae1900SAnthony Liguori { 1074df4938a6SGreg Kurz int ret = -1; 10752c30dd74SAneesh Kumar K.V 10762c30dd74SAneesh Kumar K.V if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 1077df4938a6SGreg Kurz int map_dirfd; 1078df4938a6SGreg Kurz 10796a87e792SGreg Kurz /* We need to remove the metadata as well: 10806a87e792SGreg Kurz * - the metadata directory if we're removing a directory 10816a87e792SGreg Kurz * - the metadata file in the parent's metadata directory 10826a87e792SGreg Kurz * 10836a87e792SGreg Kurz * If any of these are missing (ie, ENOENT) then we're probably 10846a87e792SGreg Kurz * trying to remove something that wasn't created in mapped-file 10856a87e792SGreg Kurz * mode. We just ignore the error. 10866a87e792SGreg Kurz */ 1087df4938a6SGreg Kurz if (flags == AT_REMOVEDIR) { 1088df4938a6SGreg Kurz int fd; 1089df4938a6SGreg Kurz 1090b003fc0dSGreg Kurz fd = openat_dir(dirfd, name); 1091df4938a6SGreg Kurz if (fd == -1) { 10922c30dd74SAneesh Kumar K.V goto err_out; 10932c30dd74SAneesh Kumar K.V } 1094df4938a6SGreg Kurz ret = unlinkat(fd, VIRTFS_META_DIR, AT_REMOVEDIR); 1095df4938a6SGreg Kurz close_preserve_errno(fd); 1096df4938a6SGreg Kurz if (ret < 0 && errno != ENOENT) { 10972c30dd74SAneesh Kumar K.V goto err_out; 10982c30dd74SAneesh Kumar K.V } 10992c30dd74SAneesh Kumar K.V } 1100df4938a6SGreg Kurz map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR); 11016a87e792SGreg Kurz if (map_dirfd != -1) { 1102df4938a6SGreg Kurz ret = unlinkat(map_dirfd, name, 0); 1103df4938a6SGreg Kurz close_preserve_errno(map_dirfd); 1104df4938a6SGreg Kurz if (ret < 0 && errno != ENOENT) { 11056a87e792SGreg Kurz goto err_out; 11066a87e792SGreg Kurz } 11076a87e792SGreg Kurz } else if (errno != ENOENT) { 11082c30dd74SAneesh Kumar K.V goto err_out; 11092c30dd74SAneesh Kumar K.V } 11102c30dd74SAneesh Kumar K.V } 11114fa4ce71SChen Gang 1112df4938a6SGreg Kurz ret = unlinkat(dirfd, name, flags); 11132c30dd74SAneesh Kumar K.V err_out: 1114df4938a6SGreg Kurz return ret; 1115df4938a6SGreg Kurz } 1116df4938a6SGreg Kurz 11178cf89e00SAnthony Liguori static int local_remove(FsContext *ctx, const char *path) 11188cf89e00SAnthony Liguori { 11198cf89e00SAnthony Liguori struct stat stbuf; 1120a0e640a8SGreg Kurz char *dirpath = g_path_get_dirname(path); 1121a0e640a8SGreg Kurz char *name = g_path_get_basename(path); 1122a0e640a8SGreg Kurz int flags = 0; 1123a0e640a8SGreg Kurz int dirfd; 1124a0e640a8SGreg Kurz int err = -1; 11258cf89e00SAnthony Liguori 1126a0e640a8SGreg Kurz dirfd = local_opendir_nofollow(ctx, dirpath); 1127b7361d46SGreg Kurz if (dirfd == -1) { 1128a0e640a8SGreg Kurz goto out; 1129a0e640a8SGreg Kurz } 1130a0e640a8SGreg Kurz 1131790db7efSBruce Rogers if (fstatat(dirfd, name, &stbuf, AT_SYMLINK_NOFOLLOW) < 0) { 11328cf89e00SAnthony Liguori goto err_out; 11338cf89e00SAnthony Liguori } 1134a0e640a8SGreg Kurz 11358cf89e00SAnthony Liguori if (S_ISDIR(stbuf.st_mode)) { 1136a0e640a8SGreg Kurz flags |= AT_REMOVEDIR; 11378cf89e00SAnthony Liguori } 11385bae1900SAnthony Liguori 1139a0e640a8SGreg Kurz err = local_unlinkat_common(ctx, dirfd, name, flags); 11402c30dd74SAneesh Kumar K.V err_out: 1141a0e640a8SGreg Kurz close_preserve_errno(dirfd); 1142a0e640a8SGreg Kurz out: 1143a0e640a8SGreg Kurz g_free(name); 1144a0e640a8SGreg Kurz g_free(dirpath); 11452c30dd74SAneesh Kumar K.V return err; 11465bae1900SAnthony Liguori } 11475bae1900SAnthony Liguori 11488b888272SAneesh Kumar K.V static int local_fsync(FsContext *ctx, int fid_type, 11498b888272SAneesh Kumar K.V V9fsFidOpenState *fs, int datasync) 11508cf89e00SAnthony Liguori { 11518b888272SAneesh Kumar K.V int fd; 11528b888272SAneesh Kumar K.V 11538b888272SAneesh Kumar K.V if (fid_type == P9_FID_DIR) { 1154f314ea4eSGreg Kurz fd = dirfd(fs->dir.stream); 115549594973SVenkateswararao Jujjuri (JV) } else { 11568b888272SAneesh Kumar K.V fd = fs->fd; 11578b888272SAneesh Kumar K.V } 11588b888272SAneesh Kumar K.V 11598b888272SAneesh Kumar K.V if (datasync) { 11608b888272SAneesh Kumar K.V return qemu_fdatasync(fd); 11618b888272SAneesh Kumar K.V } else { 11628b888272SAneesh Kumar K.V return fsync(fd); 11638cf89e00SAnthony Liguori } 116449594973SVenkateswararao Jujjuri (JV) } 11658cf89e00SAnthony Liguori 11662289be19SAneesh Kumar K.V static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf) 1167be940c87SM. Mohan Kumar { 116831e51d1cSGreg Kurz int fd, ret; 11692289be19SAneesh Kumar K.V 117031e51d1cSGreg Kurz fd = local_open_nofollow(s, fs_path->data, O_RDONLY, 0); 117123da0145SGreg Kurz if (fd == -1) { 117223da0145SGreg Kurz return -1; 117323da0145SGreg Kurz } 117431e51d1cSGreg Kurz ret = fstatfs(fd, stbuf); 117531e51d1cSGreg Kurz close_preserve_errno(fd); 11764fa4ce71SChen Gang return ret; 1177be940c87SM. Mohan Kumar } 1178be940c87SM. Mohan Kumar 11792289be19SAneesh Kumar K.V static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path, 1180fa32ef88SAneesh Kumar K.V const char *name, void *value, size_t size) 1181fa32ef88SAneesh Kumar K.V { 11822289be19SAneesh Kumar K.V char *path = fs_path->data; 11832289be19SAneesh Kumar K.V 1184fc22118dSAneesh Kumar K.V return v9fs_get_xattr(ctx, path, name, value, size); 1185fa32ef88SAneesh Kumar K.V } 1186fa32ef88SAneesh Kumar K.V 11872289be19SAneesh Kumar K.V static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path, 1188fa32ef88SAneesh Kumar K.V void *value, size_t size) 1189fa32ef88SAneesh Kumar K.V { 11902289be19SAneesh Kumar K.V char *path = fs_path->data; 11912289be19SAneesh Kumar K.V 1192fc22118dSAneesh Kumar K.V return v9fs_list_xattr(ctx, path, value, size); 119361b6c499SAneesh Kumar K.V } 119461b6c499SAneesh Kumar K.V 11952289be19SAneesh Kumar K.V static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name, 119610b468bdSAneesh Kumar K.V void *value, size_t size, int flags) 119710b468bdSAneesh Kumar K.V { 11982289be19SAneesh Kumar K.V char *path = fs_path->data; 11992289be19SAneesh Kumar K.V 1200fc22118dSAneesh Kumar K.V return v9fs_set_xattr(ctx, path, name, value, size, flags); 120110b468bdSAneesh Kumar K.V } 120210b468bdSAneesh Kumar K.V 12032289be19SAneesh Kumar K.V static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path, 12042289be19SAneesh Kumar K.V const char *name) 12059ed3ef26SAneesh Kumar K.V { 12062289be19SAneesh Kumar K.V char *path = fs_path->data; 12072289be19SAneesh Kumar K.V 1208fc22118dSAneesh Kumar K.V return v9fs_remove_xattr(ctx, path, name); 12099ed3ef26SAneesh Kumar K.V } 12109ed3ef26SAneesh Kumar K.V 12112289be19SAneesh Kumar K.V static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path, 12122289be19SAneesh Kumar K.V const char *name, V9fsPath *target) 12132289be19SAneesh Kumar K.V { 12147a95434eSGreg Kurz if (ctx->export_flags & V9FS_SM_MAPPED_FILE && 12157a95434eSGreg Kurz local_is_mapped_file_metadata(ctx, name)) { 12167a95434eSGreg Kurz errno = EINVAL; 12177a95434eSGreg Kurz return -1; 12187a95434eSGreg Kurz } 12197a95434eSGreg Kurz 12202289be19SAneesh Kumar K.V if (dir_path) { 1221f57f5878SGreg Kurz if (!strcmp(name, ".")) { 1222f57f5878SGreg Kurz /* "." relative to "foo/bar" is "foo/bar" */ 1223f57f5878SGreg Kurz v9fs_path_copy(target, dir_path); 1224f57f5878SGreg Kurz } else if (!strcmp(name, "..")) { 1225f57f5878SGreg Kurz if (!strcmp(dir_path->data, ".")) { 1226f57f5878SGreg Kurz /* ".." relative to the root is "." */ 1227f57f5878SGreg Kurz v9fs_path_sprintf(target, "."); 12289c6b899fSGreg Kurz } else { 1229f57f5878SGreg Kurz char *tmp = g_path_get_dirname(dir_path->data); 1230f57f5878SGreg Kurz /* Symbolic links are resolved by the client. We can assume 1231f57f5878SGreg Kurz * that ".." relative to "foo/bar" is equivalent to "foo" 12329c6b899fSGreg Kurz */ 1233f57f5878SGreg Kurz v9fs_path_sprintf(target, "%s", tmp); 1234f57f5878SGreg Kurz g_free(tmp); 1235f57f5878SGreg Kurz } 1236f57f5878SGreg Kurz } else { 1237f57f5878SGreg Kurz assert(!strchr(name, '/')); 1238f57f5878SGreg Kurz v9fs_path_sprintf(target, "%s/%s", dir_path->data, name); 1239f57f5878SGreg Kurz } 1240f57f5878SGreg Kurz } else if (!strcmp(name, "/") || !strcmp(name, ".") || 1241f57f5878SGreg Kurz !strcmp(name, "..")) { 1242f57f5878SGreg Kurz /* This is the root fid */ 1243f57f5878SGreg Kurz v9fs_path_sprintf(target, "."); 1244f57f5878SGreg Kurz } else { 1245f57f5878SGreg Kurz assert(!strchr(name, '/')); 1246f57f5878SGreg Kurz v9fs_path_sprintf(target, "./%s", name); 12472289be19SAneesh Kumar K.V } 12482289be19SAneesh Kumar K.V return 0; 12492289be19SAneesh Kumar K.V } 12502289be19SAneesh Kumar K.V 12512289be19SAneesh Kumar K.V static int local_renameat(FsContext *ctx, V9fsPath *olddir, 12522289be19SAneesh Kumar K.V const char *old_name, V9fsPath *newdir, 12532289be19SAneesh Kumar K.V const char *new_name) 12542289be19SAneesh Kumar K.V { 12552289be19SAneesh Kumar K.V int ret; 125699f2cf4bSGreg Kurz int odirfd, ndirfd; 12572289be19SAneesh Kumar K.V 12587a95434eSGreg Kurz if (ctx->export_flags & V9FS_SM_MAPPED_FILE && 12597a95434eSGreg Kurz (local_is_mapped_file_metadata(ctx, old_name) || 12607a95434eSGreg Kurz local_is_mapped_file_metadata(ctx, new_name))) { 12617a95434eSGreg Kurz errno = EINVAL; 12627a95434eSGreg Kurz return -1; 12637a95434eSGreg Kurz } 12647a95434eSGreg Kurz 126599f2cf4bSGreg Kurz odirfd = local_opendir_nofollow(ctx, olddir->data); 126699f2cf4bSGreg Kurz if (odirfd == -1) { 126799f2cf4bSGreg Kurz return -1; 126899f2cf4bSGreg Kurz } 12692289be19SAneesh Kumar K.V 127099f2cf4bSGreg Kurz ndirfd = local_opendir_nofollow(ctx, newdir->data); 127199f2cf4bSGreg Kurz if (ndirfd == -1) { 127299f2cf4bSGreg Kurz close_preserve_errno(odirfd); 127399f2cf4bSGreg Kurz return -1; 127499f2cf4bSGreg Kurz } 12752289be19SAneesh Kumar K.V 127699f2cf4bSGreg Kurz ret = renameat(odirfd, old_name, ndirfd, new_name); 127799f2cf4bSGreg Kurz if (ret < 0) { 127899f2cf4bSGreg Kurz goto out; 127999f2cf4bSGreg Kurz } 128099f2cf4bSGreg Kurz 128199f2cf4bSGreg Kurz if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 128299f2cf4bSGreg Kurz int omap_dirfd, nmap_dirfd; 128399f2cf4bSGreg Kurz 128499f2cf4bSGreg Kurz ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700); 128599f2cf4bSGreg Kurz if (ret < 0 && errno != EEXIST) { 128699f2cf4bSGreg Kurz goto err_undo_rename; 128799f2cf4bSGreg Kurz } 128899f2cf4bSGreg Kurz 12896dd4b1f1SGreg Kurz omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR); 129099f2cf4bSGreg Kurz if (omap_dirfd == -1) { 129199f2cf4bSGreg Kurz goto err; 129299f2cf4bSGreg Kurz } 129399f2cf4bSGreg Kurz 12946dd4b1f1SGreg Kurz nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR); 129599f2cf4bSGreg Kurz if (nmap_dirfd == -1) { 129699f2cf4bSGreg Kurz close_preserve_errno(omap_dirfd); 129799f2cf4bSGreg Kurz goto err; 129899f2cf4bSGreg Kurz } 129999f2cf4bSGreg Kurz 130099f2cf4bSGreg Kurz /* rename the .virtfs_metadata files */ 130199f2cf4bSGreg Kurz ret = renameat(omap_dirfd, old_name, nmap_dirfd, new_name); 130299f2cf4bSGreg Kurz close_preserve_errno(nmap_dirfd); 130399f2cf4bSGreg Kurz close_preserve_errno(omap_dirfd); 130499f2cf4bSGreg Kurz if (ret < 0 && errno != ENOENT) { 130599f2cf4bSGreg Kurz goto err_undo_rename; 130699f2cf4bSGreg Kurz } 130799f2cf4bSGreg Kurz 130899f2cf4bSGreg Kurz ret = 0; 130999f2cf4bSGreg Kurz } 131099f2cf4bSGreg Kurz goto out; 131199f2cf4bSGreg Kurz 131299f2cf4bSGreg Kurz err: 131399f2cf4bSGreg Kurz ret = -1; 131499f2cf4bSGreg Kurz err_undo_rename: 131599f2cf4bSGreg Kurz renameat_preserve_errno(ndirfd, new_name, odirfd, old_name); 131699f2cf4bSGreg Kurz out: 131799f2cf4bSGreg Kurz close_preserve_errno(ndirfd); 131899f2cf4bSGreg Kurz close_preserve_errno(odirfd); 13192289be19SAneesh Kumar K.V return ret; 13202289be19SAneesh Kumar K.V } 13212289be19SAneesh Kumar K.V 1322d2767edeSGreg Kurz static void v9fs_path_init_dirname(V9fsPath *path, const char *str) 1323d2767edeSGreg Kurz { 1324d2767edeSGreg Kurz path->data = g_path_get_dirname(str); 1325d2767edeSGreg Kurz path->size = strlen(path->data) + 1; 1326d2767edeSGreg Kurz } 1327d2767edeSGreg Kurz 1328d2767edeSGreg Kurz static int local_rename(FsContext *ctx, const char *oldpath, 1329d2767edeSGreg Kurz const char *newpath) 1330d2767edeSGreg Kurz { 1331d2767edeSGreg Kurz int err; 1332d2767edeSGreg Kurz char *oname = g_path_get_basename(oldpath); 1333d2767edeSGreg Kurz char *nname = g_path_get_basename(newpath); 1334d2767edeSGreg Kurz V9fsPath olddir, newdir; 1335d2767edeSGreg Kurz 1336d2767edeSGreg Kurz v9fs_path_init_dirname(&olddir, oldpath); 1337d2767edeSGreg Kurz v9fs_path_init_dirname(&newdir, newpath); 1338d2767edeSGreg Kurz 1339d2767edeSGreg Kurz err = local_renameat(ctx, &olddir, oname, &newdir, nname); 1340d2767edeSGreg Kurz 1341d2767edeSGreg Kurz v9fs_path_free(&newdir); 1342d2767edeSGreg Kurz v9fs_path_free(&olddir); 1343d2767edeSGreg Kurz g_free(nname); 1344d2767edeSGreg Kurz g_free(oname); 1345d2767edeSGreg Kurz 1346d2767edeSGreg Kurz return err; 1347d2767edeSGreg Kurz } 1348d2767edeSGreg Kurz 13492289be19SAneesh Kumar K.V static int local_unlinkat(FsContext *ctx, V9fsPath *dir, 13502289be19SAneesh Kumar K.V const char *name, int flags) 13512289be19SAneesh Kumar K.V { 13522289be19SAneesh Kumar K.V int ret; 1353df4938a6SGreg Kurz int dirfd; 13542c30dd74SAneesh Kumar K.V 13557a95434eSGreg Kurz if (ctx->export_flags & V9FS_SM_MAPPED_FILE && 13567a95434eSGreg Kurz local_is_mapped_file_metadata(ctx, name)) { 13577a95434eSGreg Kurz errno = EINVAL; 13587a95434eSGreg Kurz return -1; 13597a95434eSGreg Kurz } 13607a95434eSGreg Kurz 1361df4938a6SGreg Kurz dirfd = local_opendir_nofollow(ctx, dir->data); 1362df4938a6SGreg Kurz if (dirfd == -1) { 1363df4938a6SGreg Kurz return -1; 1364df4938a6SGreg Kurz } 13652289be19SAneesh Kumar K.V 1366df4938a6SGreg Kurz ret = local_unlinkat_common(ctx, dirfd, name, flags); 1367df4938a6SGreg Kurz close_preserve_errno(dirfd); 13682289be19SAneesh Kumar K.V return ret; 13692289be19SAneesh Kumar K.V } 13709ed3ef26SAneesh Kumar K.V 1371e06a765eSHarsh Prateek Bora static int local_ioc_getversion(FsContext *ctx, V9fsPath *path, 1372e06a765eSHarsh Prateek Bora mode_t st_mode, uint64_t *st_gen) 1373e06a765eSHarsh Prateek Bora { 1374ae0f940eSPaolo Bonzini #ifdef FS_IOC_GETVERSION 13750e5fc994SKirill A. Shutemov int err; 1376cc720ddbSAneesh Kumar K.V V9fsFidOpenState fid_open; 1377cc720ddbSAneesh Kumar K.V 1378e06a765eSHarsh Prateek Bora /* 1379e06a765eSHarsh Prateek Bora * Do not try to open special files like device nodes, fifos etc 1380e06a765eSHarsh Prateek Bora * We can get fd for regular files and directories only 1381e06a765eSHarsh Prateek Bora */ 1382e06a765eSHarsh Prateek Bora if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) { 13831a9978a5SKirill A. Shutemov errno = ENOTTY; 13841a9978a5SKirill A. Shutemov return -1; 1385e06a765eSHarsh Prateek Bora } 1386cc720ddbSAneesh Kumar K.V err = local_open(ctx, path, O_RDONLY, &fid_open); 1387cc720ddbSAneesh Kumar K.V if (err < 0) { 1388cc720ddbSAneesh Kumar K.V return err; 1389e06a765eSHarsh Prateek Bora } 1390cc720ddbSAneesh Kumar K.V err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen); 1391cc720ddbSAneesh Kumar K.V local_close(ctx, &fid_open); 1392e06a765eSHarsh Prateek Bora return err; 13930e5fc994SKirill A. Shutemov #else 13940e5fc994SKirill A. Shutemov errno = ENOTTY; 13950e5fc994SKirill A. Shutemov return -1; 13960e5fc994SKirill A. Shutemov #endif 1397e06a765eSHarsh Prateek Bora } 1398e06a765eSHarsh Prateek Bora 13990174fe73SAneesh Kumar K.V static int local_init(FsContext *ctx) 14000174fe73SAneesh Kumar K.V { 1401e06a765eSHarsh Prateek Bora struct statfs stbuf; 14020e35a378SGreg Kurz LocalData *data = g_malloc(sizeof(*data)); 14030e35a378SGreg Kurz 14040e35a378SGreg Kurz data->mountfd = open(ctx->fs_root, O_DIRECTORY | O_RDONLY); 14050e35a378SGreg Kurz if (data->mountfd == -1) { 14060e35a378SGreg Kurz goto err; 14070e35a378SGreg Kurz } 1408e06a765eSHarsh Prateek Bora 140900c90bd1SGreg Kurz #ifdef FS_IOC_GETVERSION 141000c90bd1SGreg Kurz /* 141100c90bd1SGreg Kurz * use ioc_getversion only if the ioctl is definied 141200c90bd1SGreg Kurz */ 14130e35a378SGreg Kurz if (fstatfs(data->mountfd, &stbuf) < 0) { 14140e35a378SGreg Kurz close_preserve_errno(data->mountfd); 14150e35a378SGreg Kurz goto err; 141600c90bd1SGreg Kurz } 141700c90bd1SGreg Kurz switch (stbuf.f_type) { 141800c90bd1SGreg Kurz case EXT2_SUPER_MAGIC: 141900c90bd1SGreg Kurz case BTRFS_SUPER_MAGIC: 142000c90bd1SGreg Kurz case REISERFS_SUPER_MAGIC: 142100c90bd1SGreg Kurz case XFS_SUPER_MAGIC: 142200c90bd1SGreg Kurz ctx->exops.get_st_gen = local_ioc_getversion; 142300c90bd1SGreg Kurz break; 142400c90bd1SGreg Kurz } 142500c90bd1SGreg Kurz #endif 14260174fe73SAneesh Kumar K.V 14272c30dd74SAneesh Kumar K.V if (ctx->export_flags & V9FS_SM_PASSTHROUGH) { 14282c30dd74SAneesh Kumar K.V ctx->xops = passthrough_xattr_ops; 14292c30dd74SAneesh Kumar K.V } else if (ctx->export_flags & V9FS_SM_MAPPED) { 14302c30dd74SAneesh Kumar K.V ctx->xops = mapped_xattr_ops; 14312c30dd74SAneesh Kumar K.V } else if (ctx->export_flags & V9FS_SM_NONE) { 14322c30dd74SAneesh Kumar K.V ctx->xops = none_xattr_ops; 14332c30dd74SAneesh Kumar K.V } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 14342c30dd74SAneesh Kumar K.V /* 14352c30dd74SAneesh Kumar K.V * xattr operation for mapped-file and passthrough 14362c30dd74SAneesh Kumar K.V * remain same. 14372c30dd74SAneesh Kumar K.V */ 14382c30dd74SAneesh Kumar K.V ctx->xops = passthrough_xattr_ops; 14392c30dd74SAneesh Kumar K.V } 1440c98f1d4aSAneesh Kumar K.V ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT; 144100c90bd1SGreg Kurz 14420e35a378SGreg Kurz ctx->private = data; 144300c90bd1SGreg Kurz return 0; 14440e35a378SGreg Kurz 14450e35a378SGreg Kurz err: 14460e35a378SGreg Kurz g_free(data); 14470e35a378SGreg Kurz return -1; 1448e06a765eSHarsh Prateek Bora } 14490e35a378SGreg Kurz 14500e35a378SGreg Kurz static void local_cleanup(FsContext *ctx) 14510e35a378SGreg Kurz { 14520e35a378SGreg Kurz LocalData *data = ctx->private; 14530e35a378SGreg Kurz 14540e35a378SGreg Kurz close(data->mountfd); 14550e35a378SGreg Kurz g_free(data); 14560174fe73SAneesh Kumar K.V } 14570174fe73SAneesh Kumar K.V 145899519f0aSAneesh Kumar K.V static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse) 145999519f0aSAneesh Kumar K.V { 146099519f0aSAneesh Kumar K.V const char *sec_model = qemu_opt_get(opts, "security_model"); 146199519f0aSAneesh Kumar K.V const char *path = qemu_opt_get(opts, "path"); 1462b8bbdb88SPradeep Jagadeesh Error *err = NULL; 146399519f0aSAneesh Kumar K.V 146499519f0aSAneesh Kumar K.V if (!sec_model) { 146563325b18SGreg Kurz error_report("Security model not specified, local fs needs security model"); 146663325b18SGreg Kurz error_printf("valid options are:" 146763325b18SGreg Kurz "\tsecurity_model=[passthrough|mapped-xattr|mapped-file|none]\n"); 146899519f0aSAneesh Kumar K.V return -1; 146999519f0aSAneesh Kumar K.V } 147099519f0aSAneesh Kumar K.V 147199519f0aSAneesh Kumar K.V if (!strcmp(sec_model, "passthrough")) { 147299519f0aSAneesh Kumar K.V fse->export_flags |= V9FS_SM_PASSTHROUGH; 14732c30dd74SAneesh Kumar K.V } else if (!strcmp(sec_model, "mapped") || 14742c30dd74SAneesh Kumar K.V !strcmp(sec_model, "mapped-xattr")) { 147599519f0aSAneesh Kumar K.V fse->export_flags |= V9FS_SM_MAPPED; 147699519f0aSAneesh Kumar K.V } else if (!strcmp(sec_model, "none")) { 147799519f0aSAneesh Kumar K.V fse->export_flags |= V9FS_SM_NONE; 14782c30dd74SAneesh Kumar K.V } else if (!strcmp(sec_model, "mapped-file")) { 14792c30dd74SAneesh Kumar K.V fse->export_flags |= V9FS_SM_MAPPED_FILE; 148099519f0aSAneesh Kumar K.V } else { 148163325b18SGreg Kurz error_report("Invalid security model %s specified", sec_model); 148263325b18SGreg Kurz error_printf("valid options are:" 148363325b18SGreg Kurz "\t[passthrough|mapped-xattr|mapped-file|none]\n"); 148499519f0aSAneesh Kumar K.V return -1; 148599519f0aSAneesh Kumar K.V } 148699519f0aSAneesh Kumar K.V 148799519f0aSAneesh Kumar K.V if (!path) { 148863325b18SGreg Kurz error_report("fsdev: No path specified"); 148999519f0aSAneesh Kumar K.V return -1; 149099519f0aSAneesh Kumar K.V } 1491b8bbdb88SPradeep Jagadeesh 1492b8bbdb88SPradeep Jagadeesh fsdev_throttle_parse_opts(opts, &fse->fst, &err); 1493b8bbdb88SPradeep Jagadeesh if (err) { 1494b8bbdb88SPradeep Jagadeesh error_reportf_err(err, "Throttle configuration is not valid: "); 1495b8bbdb88SPradeep Jagadeesh return -1; 1496b8bbdb88SPradeep Jagadeesh } 1497b8bbdb88SPradeep Jagadeesh 1498b96feb2cSTobias Schramm if (fse->export_flags & V9FS_SM_MAPPED || 1499b96feb2cSTobias Schramm fse->export_flags & V9FS_SM_MAPPED_FILE) { 1500b96feb2cSTobias Schramm fse->fmode = 1501b96feb2cSTobias Schramm qemu_opt_get_number(opts, "fmode", SM_LOCAL_MODE_BITS) & 0777; 1502b96feb2cSTobias Schramm fse->dmode = 1503b96feb2cSTobias Schramm qemu_opt_get_number(opts, "dmode", SM_LOCAL_DIR_MODE_BITS) & 0777; 1504b96feb2cSTobias Schramm } else { 1505b96feb2cSTobias Schramm if (qemu_opt_find(opts, "fmode")) { 1506b96feb2cSTobias Schramm error_report("fmode is only valid for mapped 9p modes"); 1507b96feb2cSTobias Schramm return -1; 1508b96feb2cSTobias Schramm } 1509b96feb2cSTobias Schramm if (qemu_opt_find(opts, "dmode")) { 1510b96feb2cSTobias Schramm error_report("dmode is only valid for mapped 9p modes"); 1511b96feb2cSTobias Schramm return -1; 1512b96feb2cSTobias Schramm } 1513b96feb2cSTobias Schramm } 1514b96feb2cSTobias Schramm 151599519f0aSAneesh Kumar K.V fse->path = g_strdup(path); 151699519f0aSAneesh Kumar K.V 151799519f0aSAneesh Kumar K.V return 0; 151899519f0aSAneesh Kumar K.V } 151999519f0aSAneesh Kumar K.V 15209f107513SAnthony Liguori FileOperations local_ops = { 152199519f0aSAneesh Kumar K.V .parse_opts = local_parse_opts, 15220174fe73SAneesh Kumar K.V .init = local_init, 15230e35a378SGreg Kurz .cleanup = local_cleanup, 1524131dcb25SAnthony Liguori .lstat = local_lstat, 1525131dcb25SAnthony Liguori .readlink = local_readlink, 1526131dcb25SAnthony Liguori .close = local_close, 1527131dcb25SAnthony Liguori .closedir = local_closedir, 1528a6568fe2SAnthony Liguori .open = local_open, 1529a6568fe2SAnthony Liguori .opendir = local_opendir, 1530a9231555SAnthony Liguori .rewinddir = local_rewinddir, 1531a9231555SAnthony Liguori .telldir = local_telldir, 1532635324e8SGreg Kurz .readdir = local_readdir, 1533a9231555SAnthony Liguori .seekdir = local_seekdir, 153456d15a53SSanchit Garg .preadv = local_preadv, 153556d15a53SSanchit Garg .pwritev = local_pwritev, 1536c494dd6fSAnthony Liguori .chmod = local_chmod, 1537c494dd6fSAnthony Liguori .mknod = local_mknod, 1538c494dd6fSAnthony Liguori .mkdir = local_mkdir, 1539c494dd6fSAnthony Liguori .fstat = local_fstat, 1540c494dd6fSAnthony Liguori .open2 = local_open2, 1541c494dd6fSAnthony Liguori .symlink = local_symlink, 1542c494dd6fSAnthony Liguori .link = local_link, 15438cf89e00SAnthony Liguori .truncate = local_truncate, 15448cf89e00SAnthony Liguori .rename = local_rename, 15458cf89e00SAnthony Liguori .chown = local_chown, 154674bc02b2SM. Mohan Kumar .utimensat = local_utimensat, 15475bae1900SAnthony Liguori .remove = local_remove, 15488cf89e00SAnthony Liguori .fsync = local_fsync, 1549be940c87SM. Mohan Kumar .statfs = local_statfs, 1550fa32ef88SAneesh Kumar K.V .lgetxattr = local_lgetxattr, 1551fa32ef88SAneesh Kumar K.V .llistxattr = local_llistxattr, 155210b468bdSAneesh Kumar K.V .lsetxattr = local_lsetxattr, 15539ed3ef26SAneesh Kumar K.V .lremovexattr = local_lremovexattr, 15542289be19SAneesh Kumar K.V .name_to_path = local_name_to_path, 15552289be19SAneesh Kumar K.V .renameat = local_renameat, 15562289be19SAneesh Kumar K.V .unlinkat = local_unlinkat, 15579f107513SAnthony Liguori }; 1558