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 */ 12873c3213SStefan Weil 13fbc04127SPeter Maydell #include "qemu/osdep.h" 14ebe74f8bSWei Liu #include "9p.h" 15996a0d76SGreg Kurz #include "9p-local.h" 16267ae092SWei Liu #include "9p-xattr.h" 170e35a378SGreg Kurz #include "9p-util.h" 1869b15212SStefan Weil #include "fsdev/qemu-fsdev.h" /* local_ops */ 19c494dd6fSAnthony Liguori #include <arpa/inet.h> 20131dcb25SAnthony Liguori #include <pwd.h> 21131dcb25SAnthony Liguori #include <grp.h> 22c494dd6fSAnthony Liguori #include <sys/socket.h> 23c494dd6fSAnthony Liguori #include <sys/un.h> 241de7afc9SPaolo Bonzini #include "qemu/xattr.h" 25e688df6bSMarkus Armbruster #include "qapi/error.h" 26f348b6d1SVeronia Bahaa #include "qemu/cutils.h" 2763325b18SGreg Kurz #include "qemu/error-report.h" 28922a01a0SMarkus Armbruster #include "qemu/option.h" 292c30dd74SAneesh Kumar K.V #include <libgen.h> 30e06a765eSHarsh Prateek Bora #include <linux/fs.h> 31e06a765eSHarsh Prateek Bora #ifdef CONFIG_LINUX_MAGIC_H 32e06a765eSHarsh Prateek Bora #include <linux/magic.h> 33e06a765eSHarsh Prateek Bora #endif 34e06a765eSHarsh Prateek Bora #include <sys/ioctl.h> 35e06a765eSHarsh Prateek Bora 36e06a765eSHarsh Prateek Bora #ifndef XFS_SUPER_MAGIC 37e06a765eSHarsh Prateek Bora #define XFS_SUPER_MAGIC 0x58465342 38e06a765eSHarsh Prateek Bora #endif 39e06a765eSHarsh Prateek Bora #ifndef EXT2_SUPER_MAGIC 40e06a765eSHarsh Prateek Bora #define EXT2_SUPER_MAGIC 0xEF53 41e06a765eSHarsh Prateek Bora #endif 42e06a765eSHarsh Prateek Bora #ifndef REISERFS_SUPER_MAGIC 43e06a765eSHarsh Prateek Bora #define REISERFS_SUPER_MAGIC 0x52654973 44e06a765eSHarsh Prateek Bora #endif 45e06a765eSHarsh Prateek Bora #ifndef BTRFS_SUPER_MAGIC 46e06a765eSHarsh Prateek Bora #define BTRFS_SUPER_MAGIC 0x9123683E 47e06a765eSHarsh Prateek Bora #endif 48131dcb25SAnthony Liguori 490e35a378SGreg Kurz typedef struct { 500e35a378SGreg Kurz int mountfd; 510e35a378SGreg Kurz } LocalData; 520e35a378SGreg Kurz 53996a0d76SGreg Kurz int local_open_nofollow(FsContext *fs_ctx, const char *path, int flags, 54996a0d76SGreg Kurz mode_t mode) 55996a0d76SGreg Kurz { 56996a0d76SGreg Kurz LocalData *data = fs_ctx->private; 573dbcf273SGreg Kurz int fd = data->mountfd; 58996a0d76SGreg Kurz 593dbcf273SGreg Kurz while (*path && fd != -1) { 603dbcf273SGreg Kurz const char *c; 613dbcf273SGreg Kurz int next_fd; 623dbcf273SGreg Kurz char *head; 633dbcf273SGreg Kurz 643dbcf273SGreg Kurz /* Only relative paths without consecutive slashes */ 653dbcf273SGreg Kurz assert(*path != '/'); 663dbcf273SGreg Kurz 673dbcf273SGreg Kurz head = g_strdup(path); 68*5c99fa37SKeno Fischer c = qemu_strchrnul(path, '/'); 693dbcf273SGreg Kurz if (*c) { 703dbcf273SGreg Kurz /* Intermediate path element */ 713dbcf273SGreg Kurz head[c - path] = 0; 723dbcf273SGreg Kurz path = c + 1; 733dbcf273SGreg Kurz next_fd = openat_dir(fd, head); 743dbcf273SGreg Kurz } else { 753dbcf273SGreg Kurz /* Rightmost path element */ 763dbcf273SGreg Kurz next_fd = openat_file(fd, head, flags, mode); 773dbcf273SGreg Kurz path = c; 783dbcf273SGreg Kurz } 793dbcf273SGreg Kurz g_free(head); 803dbcf273SGreg Kurz if (fd != data->mountfd) { 813dbcf273SGreg Kurz close_preserve_errno(fd); 823dbcf273SGreg Kurz } 833dbcf273SGreg Kurz fd = next_fd; 84996a0d76SGreg Kurz } 85996a0d76SGreg Kurz 863dbcf273SGreg Kurz assert(fd != data->mountfd); 873dbcf273SGreg Kurz return fd; 88996a0d76SGreg Kurz } 89996a0d76SGreg Kurz 90996a0d76SGreg Kurz int local_opendir_nofollow(FsContext *fs_ctx, const char *path) 91996a0d76SGreg Kurz { 92996a0d76SGreg Kurz return local_open_nofollow(fs_ctx, path, O_DIRECTORY | O_RDONLY, 0); 93996a0d76SGreg Kurz } 94996a0d76SGreg Kurz 9599f2cf4bSGreg Kurz static void renameat_preserve_errno(int odirfd, const char *opath, int ndirfd, 9699f2cf4bSGreg Kurz const char *npath) 9799f2cf4bSGreg Kurz { 9899f2cf4bSGreg Kurz int serrno = errno; 9999f2cf4bSGreg Kurz renameat(odirfd, opath, ndirfd, npath); 10099f2cf4bSGreg Kurz errno = serrno; 10199f2cf4bSGreg Kurz } 10299f2cf4bSGreg Kurz 103ad0b46e6SGreg Kurz static void unlinkat_preserve_errno(int dirfd, const char *path, int flags) 104ad0b46e6SGreg Kurz { 105ad0b46e6SGreg Kurz int serrno = errno; 106ad0b46e6SGreg Kurz unlinkat(dirfd, path, flags); 107ad0b46e6SGreg Kurz errno = serrno; 108ad0b46e6SGreg Kurz } 109ad0b46e6SGreg Kurz 1102c30dd74SAneesh Kumar K.V #define VIRTFS_META_DIR ".virtfs_metadata" 11181ffbf5aSGreg Kurz #define VIRTFS_META_ROOT_FILE VIRTFS_META_DIR "_root" 1122c30dd74SAneesh Kumar K.V 113f9aef99bSGreg Kurz static FILE *local_fopenat(int dirfd, const char *name, const char *mode) 1140ceb092eSAneesh Kumar K.V { 1150ceb092eSAneesh Kumar K.V int fd, o_mode = 0; 1160ceb092eSAneesh Kumar K.V FILE *fp; 117f9aef99bSGreg Kurz int flags; 1180ceb092eSAneesh Kumar K.V /* 1190ceb092eSAneesh Kumar K.V * only supports two modes 1200ceb092eSAneesh Kumar K.V */ 1210ceb092eSAneesh Kumar K.V if (mode[0] == 'r') { 122f9aef99bSGreg Kurz flags = O_RDONLY; 1230ceb092eSAneesh Kumar K.V } else if (mode[0] == 'w') { 124f9aef99bSGreg Kurz flags = O_WRONLY | O_TRUNC | O_CREAT; 1250ceb092eSAneesh Kumar K.V o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; 1260ceb092eSAneesh Kumar K.V } else { 1270ceb092eSAneesh Kumar K.V return NULL; 1280ceb092eSAneesh Kumar K.V } 129f9aef99bSGreg Kurz fd = openat_file(dirfd, name, flags, o_mode); 1300ceb092eSAneesh Kumar K.V if (fd == -1) { 1310ceb092eSAneesh Kumar K.V return NULL; 1320ceb092eSAneesh Kumar K.V } 1330ceb092eSAneesh Kumar K.V fp = fdopen(fd, mode); 1340ceb092eSAneesh Kumar K.V if (!fp) { 1350ceb092eSAneesh Kumar K.V close(fd); 1360ceb092eSAneesh Kumar K.V } 1370ceb092eSAneesh Kumar K.V return fp; 1380ceb092eSAneesh Kumar K.V } 1390ceb092eSAneesh Kumar K.V 1402c30dd74SAneesh Kumar K.V #define ATTR_MAX 100 141f9aef99bSGreg Kurz static void local_mapped_file_attr(int dirfd, const char *name, 1422c30dd74SAneesh Kumar K.V struct stat *stbuf) 1432c30dd74SAneesh Kumar K.V { 1442c30dd74SAneesh Kumar K.V FILE *fp; 1452c30dd74SAneesh Kumar K.V char buf[ATTR_MAX]; 146f9aef99bSGreg Kurz int map_dirfd; 1472c30dd74SAneesh Kumar K.V 14881ffbf5aSGreg Kurz if (strcmp(name, ".")) { 14999f2cf4bSGreg Kurz map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR); 150f9aef99bSGreg Kurz if (map_dirfd == -1) { 151f9aef99bSGreg Kurz return; 152f9aef99bSGreg Kurz } 153f9aef99bSGreg Kurz 154f9aef99bSGreg Kurz fp = local_fopenat(map_dirfd, name, "r"); 155f9aef99bSGreg Kurz close_preserve_errno(map_dirfd); 15681ffbf5aSGreg Kurz } else { 15781ffbf5aSGreg Kurz fp = local_fopenat(dirfd, VIRTFS_META_ROOT_FILE, "r"); 15881ffbf5aSGreg Kurz } 1592c30dd74SAneesh Kumar K.V if (!fp) { 1602c30dd74SAneesh Kumar K.V return; 1612c30dd74SAneesh Kumar K.V } 1622c30dd74SAneesh Kumar K.V memset(buf, 0, ATTR_MAX); 1632c30dd74SAneesh Kumar K.V while (fgets(buf, ATTR_MAX, fp)) { 1642c30dd74SAneesh Kumar K.V if (!strncmp(buf, "virtfs.uid", 10)) { 1652c30dd74SAneesh Kumar K.V stbuf->st_uid = atoi(buf+11); 1662c30dd74SAneesh Kumar K.V } else if (!strncmp(buf, "virtfs.gid", 10)) { 1672c30dd74SAneesh Kumar K.V stbuf->st_gid = atoi(buf+11); 1682c30dd74SAneesh Kumar K.V } else if (!strncmp(buf, "virtfs.mode", 11)) { 1692c30dd74SAneesh Kumar K.V stbuf->st_mode = atoi(buf+12); 1702c30dd74SAneesh Kumar K.V } else if (!strncmp(buf, "virtfs.rdev", 11)) { 1712c30dd74SAneesh Kumar K.V stbuf->st_rdev = atoi(buf+12); 1722c30dd74SAneesh Kumar K.V } 1732c30dd74SAneesh Kumar K.V memset(buf, 0, ATTR_MAX); 1742c30dd74SAneesh Kumar K.V } 1752c30dd74SAneesh Kumar K.V fclose(fp); 1762c30dd74SAneesh Kumar K.V } 1772c30dd74SAneesh Kumar K.V 1782289be19SAneesh Kumar K.V static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf) 179131dcb25SAnthony Liguori { 180f9aef99bSGreg Kurz int err = -1; 181f9aef99bSGreg Kurz char *dirpath = g_path_get_dirname(fs_path->data); 182f9aef99bSGreg Kurz char *name = g_path_get_basename(fs_path->data); 183f9aef99bSGreg Kurz int dirfd; 1842289be19SAneesh Kumar K.V 185f9aef99bSGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dirpath); 186f9aef99bSGreg Kurz if (dirfd == -1) { 187f9aef99bSGreg Kurz goto out; 188f9aef99bSGreg Kurz } 189f9aef99bSGreg Kurz 190f9aef99bSGreg Kurz err = fstatat(dirfd, name, stbuf, AT_SYMLINK_NOFOLLOW); 1911237ad76SVenkateswararao Jujjuri (JV) if (err) { 1924fa4ce71SChen Gang goto err_out; 1931237ad76SVenkateswararao Jujjuri (JV) } 194b97400caSAneesh Kumar K.V if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 1951237ad76SVenkateswararao Jujjuri (JV) /* Actual credentials are part of extended attrs */ 1961237ad76SVenkateswararao Jujjuri (JV) uid_t tmp_uid; 1971237ad76SVenkateswararao Jujjuri (JV) gid_t tmp_gid; 1981237ad76SVenkateswararao Jujjuri (JV) mode_t tmp_mode; 1991237ad76SVenkateswararao Jujjuri (JV) dev_t tmp_dev; 200f9aef99bSGreg Kurz 201f9aef99bSGreg Kurz if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.uid", &tmp_uid, 202f9aef99bSGreg Kurz sizeof(uid_t)) > 0) { 203f8ad4a89SAneesh Kumar K.V stbuf->st_uid = le32_to_cpu(tmp_uid); 2041237ad76SVenkateswararao Jujjuri (JV) } 205f9aef99bSGreg Kurz if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.gid", &tmp_gid, 206f9aef99bSGreg Kurz sizeof(gid_t)) > 0) { 207f8ad4a89SAneesh Kumar K.V stbuf->st_gid = le32_to_cpu(tmp_gid); 2081237ad76SVenkateswararao Jujjuri (JV) } 209f9aef99bSGreg Kurz if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.mode", &tmp_mode, 210f9aef99bSGreg Kurz sizeof(mode_t)) > 0) { 211f8ad4a89SAneesh Kumar K.V stbuf->st_mode = le32_to_cpu(tmp_mode); 2121237ad76SVenkateswararao Jujjuri (JV) } 213f9aef99bSGreg Kurz if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.rdev", &tmp_dev, 214f9aef99bSGreg Kurz sizeof(dev_t)) > 0) { 215f8ad4a89SAneesh Kumar K.V stbuf->st_rdev = le64_to_cpu(tmp_dev); 2161237ad76SVenkateswararao Jujjuri (JV) } 2172c30dd74SAneesh Kumar K.V } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 218f9aef99bSGreg Kurz local_mapped_file_attr(dirfd, name, stbuf); 2191237ad76SVenkateswararao Jujjuri (JV) } 2204fa4ce71SChen Gang 2214fa4ce71SChen Gang err_out: 222f9aef99bSGreg Kurz close_preserve_errno(dirfd); 223f9aef99bSGreg Kurz out: 224f9aef99bSGreg Kurz g_free(name); 225f9aef99bSGreg Kurz g_free(dirpath); 2261237ad76SVenkateswararao Jujjuri (JV) return err; 227131dcb25SAnthony Liguori } 228131dcb25SAnthony Liguori 229e3187a45SGreg Kurz static int local_set_mapped_file_attrat(int dirfd, const char *name, 230e3187a45SGreg Kurz FsCred *credp) 2312c30dd74SAneesh Kumar K.V { 2322c30dd74SAneesh Kumar K.V FILE *fp; 233e3187a45SGreg Kurz int ret; 2342c30dd74SAneesh Kumar K.V char buf[ATTR_MAX]; 2352c30dd74SAneesh Kumar K.V int uid = -1, gid = -1, mode = -1, rdev = -1; 23681ffbf5aSGreg Kurz int map_dirfd = -1, map_fd; 23781ffbf5aSGreg Kurz bool is_root = !strcmp(name, "."); 2382c30dd74SAneesh Kumar K.V 23981ffbf5aSGreg Kurz if (is_root) { 24081ffbf5aSGreg Kurz fp = local_fopenat(dirfd, VIRTFS_META_ROOT_FILE, "r"); 24181ffbf5aSGreg Kurz if (!fp) { 24281ffbf5aSGreg Kurz if (errno == ENOENT) { 24381ffbf5aSGreg Kurz goto update_map_file; 24481ffbf5aSGreg Kurz } else { 24581ffbf5aSGreg Kurz return -1; 24681ffbf5aSGreg Kurz } 24781ffbf5aSGreg Kurz } 24881ffbf5aSGreg Kurz } else { 249e3187a45SGreg Kurz ret = mkdirat(dirfd, VIRTFS_META_DIR, 0700); 250e3187a45SGreg Kurz if (ret < 0 && errno != EEXIST) { 251e3187a45SGreg Kurz return -1; 252e3187a45SGreg Kurz } 253e3187a45SGreg Kurz 254e3187a45SGreg Kurz map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR); 255e3187a45SGreg Kurz if (map_dirfd == -1) { 256e3187a45SGreg Kurz return -1; 257e3187a45SGreg Kurz } 258e3187a45SGreg Kurz 259e3187a45SGreg Kurz fp = local_fopenat(map_dirfd, name, "r"); 2602c30dd74SAneesh Kumar K.V if (!fp) { 261e3187a45SGreg Kurz if (errno == ENOENT) { 262e3187a45SGreg Kurz goto update_map_file; 263e3187a45SGreg Kurz } else { 264e3187a45SGreg Kurz close_preserve_errno(map_dirfd); 265e3187a45SGreg Kurz return -1; 266e3187a45SGreg Kurz } 2672c30dd74SAneesh Kumar K.V } 26881ffbf5aSGreg Kurz } 2692c30dd74SAneesh Kumar K.V memset(buf, 0, ATTR_MAX); 2702c30dd74SAneesh Kumar K.V while (fgets(buf, ATTR_MAX, fp)) { 2712c30dd74SAneesh Kumar K.V if (!strncmp(buf, "virtfs.uid", 10)) { 2722c30dd74SAneesh Kumar K.V uid = atoi(buf + 11); 2732c30dd74SAneesh Kumar K.V } else if (!strncmp(buf, "virtfs.gid", 10)) { 2742c30dd74SAneesh Kumar K.V gid = atoi(buf + 11); 2752c30dd74SAneesh Kumar K.V } else if (!strncmp(buf, "virtfs.mode", 11)) { 2762c30dd74SAneesh Kumar K.V mode = atoi(buf + 12); 2772c30dd74SAneesh Kumar K.V } else if (!strncmp(buf, "virtfs.rdev", 11)) { 2782c30dd74SAneesh Kumar K.V rdev = atoi(buf + 12); 2792c30dd74SAneesh Kumar K.V } 2802c30dd74SAneesh Kumar K.V memset(buf, 0, ATTR_MAX); 2812c30dd74SAneesh Kumar K.V } 2822c30dd74SAneesh Kumar K.V fclose(fp); 2832c30dd74SAneesh Kumar K.V 2842c30dd74SAneesh Kumar K.V update_map_file: 28581ffbf5aSGreg Kurz if (is_root) { 28681ffbf5aSGreg Kurz fp = local_fopenat(dirfd, VIRTFS_META_ROOT_FILE, "w"); 28781ffbf5aSGreg Kurz } else { 288e3187a45SGreg Kurz fp = local_fopenat(map_dirfd, name, "w"); 28981ffbf5aSGreg Kurz /* We can't go this far with map_dirfd not being a valid file descriptor 29081ffbf5aSGreg Kurz * but some versions of gcc aren't smart enough to see it. 29181ffbf5aSGreg Kurz */ 29281ffbf5aSGreg Kurz if (map_dirfd != -1) { 293e3187a45SGreg Kurz close_preserve_errno(map_dirfd); 29481ffbf5aSGreg Kurz } 29581ffbf5aSGreg Kurz } 2962c30dd74SAneesh Kumar K.V if (!fp) { 297e3187a45SGreg Kurz return -1; 2982c30dd74SAneesh Kumar K.V } 2992c30dd74SAneesh Kumar K.V 30081ffbf5aSGreg Kurz map_fd = fileno(fp); 30181ffbf5aSGreg Kurz assert(map_fd != -1); 30281ffbf5aSGreg Kurz ret = fchmod(map_fd, 0600); 30381ffbf5aSGreg Kurz assert(ret == 0); 30481ffbf5aSGreg Kurz 3052c30dd74SAneesh Kumar K.V if (credp->fc_uid != -1) { 3062c30dd74SAneesh Kumar K.V uid = credp->fc_uid; 3072c30dd74SAneesh Kumar K.V } 3082c30dd74SAneesh Kumar K.V if (credp->fc_gid != -1) { 3092c30dd74SAneesh Kumar K.V gid = credp->fc_gid; 3102c30dd74SAneesh Kumar K.V } 3112c30dd74SAneesh Kumar K.V if (credp->fc_mode != -1) { 3122c30dd74SAneesh Kumar K.V mode = credp->fc_mode; 3132c30dd74SAneesh Kumar K.V } 3142c30dd74SAneesh Kumar K.V if (credp->fc_rdev != -1) { 3152c30dd74SAneesh Kumar K.V rdev = credp->fc_rdev; 3162c30dd74SAneesh Kumar K.V } 3172c30dd74SAneesh Kumar K.V 3182c30dd74SAneesh Kumar K.V if (uid != -1) { 3192c30dd74SAneesh Kumar K.V fprintf(fp, "virtfs.uid=%d\n", uid); 3202c30dd74SAneesh Kumar K.V } 3212c30dd74SAneesh Kumar K.V if (gid != -1) { 3222c30dd74SAneesh Kumar K.V fprintf(fp, "virtfs.gid=%d\n", gid); 3232c30dd74SAneesh Kumar K.V } 3242c30dd74SAneesh Kumar K.V if (mode != -1) { 3252c30dd74SAneesh Kumar K.V fprintf(fp, "virtfs.mode=%d\n", mode); 3262c30dd74SAneesh Kumar K.V } 3272c30dd74SAneesh Kumar K.V if (rdev != -1) { 3282c30dd74SAneesh Kumar K.V fprintf(fp, "virtfs.rdev=%d\n", rdev); 3292c30dd74SAneesh Kumar K.V } 3302c30dd74SAneesh Kumar K.V fclose(fp); 3312c30dd74SAneesh Kumar K.V 332e3187a45SGreg Kurz return 0; 333e3187a45SGreg Kurz } 334e3187a45SGreg Kurz 335e3187a45SGreg Kurz static int fchmodat_nofollow(int dirfd, const char *name, mode_t mode) 336e3187a45SGreg Kurz { 3374751fd53SGreg Kurz struct stat stbuf; 338e3187a45SGreg Kurz int fd, ret; 339e3187a45SGreg Kurz 340e3187a45SGreg Kurz /* FIXME: this should be handled with fchmodat(AT_SYMLINK_NOFOLLOW). 3414751fd53SGreg Kurz * Unfortunately, the linux kernel doesn't implement it yet. 342e3187a45SGreg Kurz */ 3434751fd53SGreg Kurz 3444751fd53SGreg Kurz /* First, we clear non-racing symlinks out of the way. */ 3454751fd53SGreg Kurz if (fstatat(dirfd, name, &stbuf, AT_SYMLINK_NOFOLLOW)) { 3464751fd53SGreg Kurz return -1; 3474751fd53SGreg Kurz } 3484751fd53SGreg Kurz if (S_ISLNK(stbuf.st_mode)) { 3494751fd53SGreg Kurz errno = ELOOP; 3504751fd53SGreg Kurz return -1; 3514751fd53SGreg Kurz } 3524751fd53SGreg Kurz 353aa5e85a1SGreg Kurz fd = openat_file(dirfd, name, O_RDONLY | O_PATH_9P_UTIL | O_NOFOLLOW, 0); 3544751fd53SGreg Kurz #if O_PATH_9P_UTIL == 0 355aa5e85a1SGreg Kurz /* Fallback for systems that don't support O_PATH: we depend on the file 356aa5e85a1SGreg Kurz * being readable or writable. 357aa5e85a1SGreg Kurz */ 358e3187a45SGreg Kurz if (fd == -1) { 359e3187a45SGreg Kurz /* In case the file is writable-only and isn't a directory. */ 360e3187a45SGreg Kurz if (errno == EACCES) { 361e3187a45SGreg Kurz fd = openat_file(dirfd, name, O_WRONLY, 0); 362e3187a45SGreg Kurz } 363e3187a45SGreg Kurz if (fd == -1 && errno == EISDIR) { 364e3187a45SGreg Kurz errno = EACCES; 365e3187a45SGreg Kurz } 366e3187a45SGreg Kurz } 367e3187a45SGreg Kurz if (fd == -1) { 368e3187a45SGreg Kurz return -1; 369e3187a45SGreg Kurz } 370e3187a45SGreg Kurz ret = fchmod(fd, mode); 3714751fd53SGreg Kurz #else 372aa5e85a1SGreg Kurz /* Access modes are ignored when O_PATH is supported. If name is a symbolic 373aa5e85a1SGreg Kurz * link, O_PATH | O_NOFOLLOW causes openat(2) to return a file descriptor 374aa5e85a1SGreg Kurz * referring to the symbolic link. 375aa5e85a1SGreg Kurz */ 3764751fd53SGreg Kurz if (fd == -1) { 3774751fd53SGreg Kurz return -1; 3784751fd53SGreg Kurz } 3794751fd53SGreg Kurz 3804751fd53SGreg Kurz /* Now we handle racing symlinks. */ 3814751fd53SGreg Kurz ret = fstat(fd, &stbuf); 3824751fd53SGreg Kurz if (!ret) { 3834751fd53SGreg Kurz if (S_ISLNK(stbuf.st_mode)) { 3844751fd53SGreg Kurz errno = ELOOP; 3854751fd53SGreg Kurz ret = -1; 3864751fd53SGreg Kurz } else { 3874751fd53SGreg Kurz char *proc_path = g_strdup_printf("/proc/self/fd/%d", fd); 3884751fd53SGreg Kurz ret = chmod(proc_path, mode); 3894751fd53SGreg Kurz g_free(proc_path); 3904751fd53SGreg Kurz } 3914751fd53SGreg Kurz } 3924751fd53SGreg Kurz #endif 393e3187a45SGreg Kurz close_preserve_errno(fd); 3942c30dd74SAneesh Kumar K.V return ret; 3952c30dd74SAneesh Kumar K.V } 3962c30dd74SAneesh Kumar K.V 397e3187a45SGreg Kurz static int local_set_xattrat(int dirfd, const char *path, FsCred *credp) 398131dcb25SAnthony Liguori { 399758e8e38SVenkateswararao Jujjuri (JV) int err; 4002289be19SAneesh Kumar K.V 401758e8e38SVenkateswararao Jujjuri (JV) if (credp->fc_uid != -1) { 402f8ad4a89SAneesh Kumar K.V uint32_t tmp_uid = cpu_to_le32(credp->fc_uid); 403e3187a45SGreg Kurz err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.uid", &tmp_uid, 404e3187a45SGreg Kurz sizeof(uid_t), 0); 405758e8e38SVenkateswararao Jujjuri (JV) if (err) { 406758e8e38SVenkateswararao Jujjuri (JV) return err; 407131dcb25SAnthony Liguori } 408131dcb25SAnthony Liguori } 409758e8e38SVenkateswararao Jujjuri (JV) if (credp->fc_gid != -1) { 410f8ad4a89SAneesh Kumar K.V uint32_t tmp_gid = cpu_to_le32(credp->fc_gid); 411e3187a45SGreg Kurz err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.gid", &tmp_gid, 412e3187a45SGreg Kurz sizeof(gid_t), 0); 413758e8e38SVenkateswararao Jujjuri (JV) if (err) { 414758e8e38SVenkateswararao Jujjuri (JV) return err; 415131dcb25SAnthony Liguori } 416131dcb25SAnthony Liguori } 417758e8e38SVenkateswararao Jujjuri (JV) if (credp->fc_mode != -1) { 418f8ad4a89SAneesh Kumar K.V uint32_t tmp_mode = cpu_to_le32(credp->fc_mode); 419e3187a45SGreg Kurz err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.mode", &tmp_mode, 420e3187a45SGreg Kurz sizeof(mode_t), 0); 421758e8e38SVenkateswararao Jujjuri (JV) if (err) { 422758e8e38SVenkateswararao Jujjuri (JV) return err; 423131dcb25SAnthony Liguori } 424131dcb25SAnthony Liguori } 425758e8e38SVenkateswararao Jujjuri (JV) if (credp->fc_rdev != -1) { 426f8ad4a89SAneesh Kumar K.V uint64_t tmp_rdev = cpu_to_le64(credp->fc_rdev); 427e3187a45SGreg Kurz err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.rdev", &tmp_rdev, 428e3187a45SGreg Kurz sizeof(dev_t), 0); 429758e8e38SVenkateswararao Jujjuri (JV) if (err) { 430758e8e38SVenkateswararao Jujjuri (JV) return err; 431131dcb25SAnthony Liguori } 432758e8e38SVenkateswararao Jujjuri (JV) } 433131dcb25SAnthony Liguori return 0; 434131dcb25SAnthony Liguori } 435131dcb25SAnthony Liguori 436d815e721SGreg Kurz static int local_set_cred_passthrough(FsContext *fs_ctx, int dirfd, 437d815e721SGreg Kurz const char *name, FsCred *credp) 4384750a96fSVenkateswararao Jujjuri (JV) { 439d815e721SGreg Kurz if (fchownat(dirfd, name, credp->fc_uid, credp->fc_gid, 440b314f6a0SGreg Kurz AT_SYMLINK_NOFOLLOW) < 0) { 44112848bfcSAneesh Kumar K.V /* 44212848bfcSAneesh Kumar K.V * If we fail to change ownership and if we are 44312848bfcSAneesh Kumar K.V * using security model none. Ignore the error 44412848bfcSAneesh Kumar K.V */ 445b97400caSAneesh Kumar K.V if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) { 4464fa4ce71SChen Gang return -1; 4474750a96fSVenkateswararao Jujjuri (JV) } 448d815e721SGreg Kurz } 449d815e721SGreg Kurz 450d815e721SGreg Kurz return fchmodat_nofollow(dirfd, name, credp->fc_mode & 07777); 451d815e721SGreg Kurz } 4524750a96fSVenkateswararao Jujjuri (JV) 4532289be19SAneesh Kumar K.V static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path, 454131dcb25SAnthony Liguori char *buf, size_t bufsz) 455131dcb25SAnthony Liguori { 456879c2813SVenkateswararao Jujjuri (JV) ssize_t tsize = -1; 4572289be19SAneesh Kumar K.V 4582c30dd74SAneesh Kumar K.V if ((fs_ctx->export_flags & V9FS_SM_MAPPED) || 4592c30dd74SAneesh Kumar K.V (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) { 460879c2813SVenkateswararao Jujjuri (JV) int fd; 461bec1e954SGreg Kurz 462bec1e954SGreg Kurz fd = local_open_nofollow(fs_ctx, fs_path->data, O_RDONLY, 0); 463879c2813SVenkateswararao Jujjuri (JV) if (fd == -1) { 464879c2813SVenkateswararao Jujjuri (JV) return -1; 465879c2813SVenkateswararao Jujjuri (JV) } 466879c2813SVenkateswararao Jujjuri (JV) do { 467879c2813SVenkateswararao Jujjuri (JV) tsize = read(fd, (void *)buf, bufsz); 468879c2813SVenkateswararao Jujjuri (JV) } while (tsize == -1 && errno == EINTR); 469bec1e954SGreg Kurz close_preserve_errno(fd); 470b97400caSAneesh Kumar K.V } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 471b97400caSAneesh Kumar K.V (fs_ctx->export_flags & V9FS_SM_NONE)) { 472bec1e954SGreg Kurz char *dirpath = g_path_get_dirname(fs_path->data); 473bec1e954SGreg Kurz char *name = g_path_get_basename(fs_path->data); 474bec1e954SGreg Kurz int dirfd; 475bec1e954SGreg Kurz 476bec1e954SGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dirpath); 477bec1e954SGreg Kurz if (dirfd == -1) { 478bec1e954SGreg Kurz goto out; 479bec1e954SGreg Kurz } 480bec1e954SGreg Kurz 481bec1e954SGreg Kurz tsize = readlinkat(dirfd, name, buf, bufsz); 482bec1e954SGreg Kurz close_preserve_errno(dirfd); 483bec1e954SGreg Kurz out: 484bec1e954SGreg Kurz g_free(name); 485bec1e954SGreg Kurz g_free(dirpath); 486879c2813SVenkateswararao Jujjuri (JV) } 487879c2813SVenkateswararao Jujjuri (JV) return tsize; 488131dcb25SAnthony Liguori } 489131dcb25SAnthony Liguori 490cc720ddbSAneesh Kumar K.V static int local_close(FsContext *ctx, V9fsFidOpenState *fs) 491131dcb25SAnthony Liguori { 492cc720ddbSAneesh Kumar K.V return close(fs->fd); 493131dcb25SAnthony Liguori } 494131dcb25SAnthony Liguori 495cc720ddbSAneesh Kumar K.V static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs) 496131dcb25SAnthony Liguori { 497f314ea4eSGreg Kurz return closedir(fs->dir.stream); 498131dcb25SAnthony Liguori } 4999f107513SAnthony Liguori 500cc720ddbSAneesh Kumar K.V static int local_open(FsContext *ctx, V9fsPath *fs_path, 501cc720ddbSAneesh Kumar K.V int flags, V9fsFidOpenState *fs) 502a6568fe2SAnthony Liguori { 50321328e1eSGreg Kurz int fd; 5042289be19SAneesh Kumar K.V 505996a0d76SGreg Kurz fd = local_open_nofollow(ctx, fs_path->data, flags, 0); 50621328e1eSGreg Kurz if (fd == -1) { 50721328e1eSGreg Kurz return -1; 50821328e1eSGreg Kurz } 50921328e1eSGreg Kurz fs->fd = fd; 510cc720ddbSAneesh Kumar K.V return fs->fd; 511a6568fe2SAnthony Liguori } 512a6568fe2SAnthony Liguori 513cc720ddbSAneesh Kumar K.V static int local_opendir(FsContext *ctx, 514cc720ddbSAneesh Kumar K.V V9fsPath *fs_path, V9fsFidOpenState *fs) 515a6568fe2SAnthony Liguori { 516996a0d76SGreg Kurz int dirfd; 51721328e1eSGreg Kurz DIR *stream; 5182289be19SAneesh Kumar K.V 519996a0d76SGreg Kurz dirfd = local_opendir_nofollow(ctx, fs_path->data); 520996a0d76SGreg Kurz if (dirfd == -1) { 521cc720ddbSAneesh Kumar K.V return -1; 522cc720ddbSAneesh Kumar K.V } 523996a0d76SGreg Kurz 524996a0d76SGreg Kurz stream = fdopendir(dirfd); 52521328e1eSGreg Kurz if (!stream) { 526faab207fSGreg Kurz close(dirfd); 527cc720ddbSAneesh Kumar K.V return -1; 528cc720ddbSAneesh Kumar K.V } 52921328e1eSGreg Kurz fs->dir.stream = stream; 530cc720ddbSAneesh Kumar K.V return 0; 531a6568fe2SAnthony Liguori } 532a6568fe2SAnthony Liguori 533cc720ddbSAneesh Kumar K.V static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs) 534a9231555SAnthony Liguori { 535f314ea4eSGreg Kurz rewinddir(fs->dir.stream); 536a9231555SAnthony Liguori } 537a9231555SAnthony Liguori 538cc720ddbSAneesh Kumar K.V static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs) 539a9231555SAnthony Liguori { 540f314ea4eSGreg Kurz return telldir(fs->dir.stream); 541a9231555SAnthony Liguori } 542a9231555SAnthony Liguori 5437a95434eSGreg Kurz static bool local_is_mapped_file_metadata(FsContext *fs_ctx, const char *name) 5447a95434eSGreg Kurz { 54581ffbf5aSGreg Kurz return 54681ffbf5aSGreg Kurz !strcmp(name, VIRTFS_META_DIR) || !strcmp(name, VIRTFS_META_ROOT_FILE); 5477a95434eSGreg Kurz } 5487a95434eSGreg Kurz 549635324e8SGreg Kurz static struct dirent *local_readdir(FsContext *ctx, V9fsFidOpenState *fs) 550a9231555SAnthony Liguori { 551635324e8SGreg Kurz struct dirent *entry; 5522c30dd74SAneesh Kumar K.V 5532c30dd74SAneesh Kumar K.V again: 554635324e8SGreg Kurz entry = readdir(fs->dir.stream); 555635324e8SGreg Kurz if (!entry) { 556635324e8SGreg Kurz return NULL; 557635324e8SGreg Kurz } 558635324e8SGreg Kurz 559840a1bf2SBastian Blank if (ctx->export_flags & V9FS_SM_MAPPED) { 560840a1bf2SBastian Blank entry->d_type = DT_UNKNOWN; 561840a1bf2SBastian Blank } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 5627a95434eSGreg Kurz if (local_is_mapped_file_metadata(ctx, entry->d_name)) { 56381ffbf5aSGreg Kurz /* skip the meta data */ 5642c30dd74SAneesh Kumar K.V goto again; 5652c30dd74SAneesh Kumar K.V } 566840a1bf2SBastian Blank entry->d_type = DT_UNKNOWN; 5672c30dd74SAneesh Kumar K.V } 568635324e8SGreg Kurz 569635324e8SGreg Kurz return entry; 570a9231555SAnthony Liguori } 571a9231555SAnthony Liguori 572cc720ddbSAneesh Kumar K.V static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off) 573a9231555SAnthony Liguori { 574f314ea4eSGreg Kurz seekdir(fs->dir.stream, off); 575a9231555SAnthony Liguori } 576a9231555SAnthony Liguori 577cc720ddbSAneesh Kumar K.V static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs, 578cc720ddbSAneesh Kumar K.V const struct iovec *iov, 57956d15a53SSanchit Garg int iovcnt, off_t offset) 580a9231555SAnthony Liguori { 58156d15a53SSanchit Garg #ifdef CONFIG_PREADV 582cc720ddbSAneesh Kumar K.V return preadv(fs->fd, iov, iovcnt, offset); 58356d15a53SSanchit Garg #else 584cc720ddbSAneesh Kumar K.V int err = lseek(fs->fd, offset, SEEK_SET); 58556d15a53SSanchit Garg if (err == -1) { 58656d15a53SSanchit Garg return err; 58756d15a53SSanchit Garg } else { 588cc720ddbSAneesh Kumar K.V return readv(fs->fd, iov, iovcnt); 589a9231555SAnthony Liguori } 59056d15a53SSanchit Garg #endif 591a9231555SAnthony Liguori } 592a9231555SAnthony Liguori 593cc720ddbSAneesh Kumar K.V static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs, 594cc720ddbSAneesh Kumar K.V const struct iovec *iov, 59556d15a53SSanchit Garg int iovcnt, off_t offset) 5968449360cSAnthony Liguori { 5976fe76accSGreg Kurz ssize_t ret; 59856d15a53SSanchit Garg #ifdef CONFIG_PREADV 599cc720ddbSAneesh Kumar K.V ret = pwritev(fs->fd, iov, iovcnt, offset); 60056d15a53SSanchit Garg #else 601cc720ddbSAneesh Kumar K.V int err = lseek(fs->fd, offset, SEEK_SET); 60256d15a53SSanchit Garg if (err == -1) { 60356d15a53SSanchit Garg return err; 60456d15a53SSanchit Garg } else { 605cc720ddbSAneesh Kumar K.V ret = writev(fs->fd, iov, iovcnt); 6068449360cSAnthony Liguori } 60756d15a53SSanchit Garg #endif 608d3ab98e6SAneesh Kumar K.V #ifdef CONFIG_SYNC_FILE_RANGE 609d3ab98e6SAneesh Kumar K.V if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) { 610d3ab98e6SAneesh Kumar K.V /* 611d3ab98e6SAneesh Kumar K.V * Initiate a writeback. This is not a data integrity sync. 612d3ab98e6SAneesh Kumar K.V * We want to ensure that we don't leave dirty pages in the cache 613d3ab98e6SAneesh Kumar K.V * after write when writeout=immediate is sepcified. 614d3ab98e6SAneesh Kumar K.V */ 615cc720ddbSAneesh Kumar K.V sync_file_range(fs->fd, offset, ret, 616d3ab98e6SAneesh Kumar K.V SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE); 617d3ab98e6SAneesh Kumar K.V } 618d3ab98e6SAneesh Kumar K.V #endif 619d3ab98e6SAneesh Kumar K.V return ret; 62056d15a53SSanchit Garg } 6218449360cSAnthony Liguori 6222289be19SAneesh Kumar K.V static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp) 623c494dd6fSAnthony Liguori { 624e3187a45SGreg Kurz char *dirpath = g_path_get_dirname(fs_path->data); 625e3187a45SGreg Kurz char *name = g_path_get_basename(fs_path->data); 6264fa4ce71SChen Gang int ret = -1; 627e3187a45SGreg Kurz int dirfd; 628e3187a45SGreg Kurz 629e3187a45SGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dirpath); 630e3187a45SGreg Kurz if (dirfd == -1) { 631e3187a45SGreg Kurz goto out; 632e3187a45SGreg Kurz } 6332289be19SAneesh Kumar K.V 634b97400caSAneesh Kumar K.V if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 635e3187a45SGreg Kurz ret = local_set_xattrat(dirfd, name, credp); 6362c30dd74SAneesh Kumar K.V } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 637e3187a45SGreg Kurz ret = local_set_mapped_file_attrat(dirfd, name, credp); 638e3187a45SGreg Kurz } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH || 639e3187a45SGreg Kurz fs_ctx->export_flags & V9FS_SM_NONE) { 640e3187a45SGreg Kurz ret = fchmodat_nofollow(dirfd, name, credp->fc_mode); 641e95ead32SVenkateswararao Jujjuri (JV) } 642e3187a45SGreg Kurz close_preserve_errno(dirfd); 643e3187a45SGreg Kurz 644e3187a45SGreg Kurz out: 645e3187a45SGreg Kurz g_free(dirpath); 646e3187a45SGreg Kurz g_free(name); 6474fa4ce71SChen Gang return ret; 648c494dd6fSAnthony Liguori } 649c494dd6fSAnthony Liguori 6502289be19SAneesh Kumar K.V static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path, 6512289be19SAneesh Kumar K.V const char *name, FsCred *credp) 652c494dd6fSAnthony Liguori { 6531c293312SVenkateswararao Jujjuri (JV) int err = -1; 654d815e721SGreg Kurz int dirfd; 6551c293312SVenkateswararao Jujjuri (JV) 6567a95434eSGreg Kurz if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE && 6577a95434eSGreg Kurz local_is_mapped_file_metadata(fs_ctx, name)) { 6587a95434eSGreg Kurz errno = EINVAL; 6597a95434eSGreg Kurz return -1; 6607a95434eSGreg Kurz } 6617a95434eSGreg Kurz 662d815e721SGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dir_path->data); 663d815e721SGreg Kurz if (dirfd == -1) { 664d815e721SGreg Kurz return -1; 665d815e721SGreg Kurz } 6662289be19SAneesh Kumar K.V 667d815e721SGreg Kurz if (fs_ctx->export_flags & V9FS_SM_MAPPED || 668d815e721SGreg Kurz fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 669b96feb2cSTobias Schramm err = mknodat(dirfd, name, fs_ctx->fmode | S_IFREG, 0); 670d815e721SGreg Kurz if (err == -1) { 671d815e721SGreg Kurz goto out; 672d815e721SGreg Kurz } 673d815e721SGreg Kurz 674b97400caSAneesh Kumar K.V if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 675d815e721SGreg Kurz err = local_set_xattrat(dirfd, name, credp); 676d815e721SGreg Kurz } else { 677d815e721SGreg Kurz err = local_set_mapped_file_attrat(dirfd, name, credp); 6781c293312SVenkateswararao Jujjuri (JV) } 6791c293312SVenkateswararao Jujjuri (JV) if (err == -1) { 6801c293312SVenkateswararao Jujjuri (JV) goto err_end; 6811c293312SVenkateswararao Jujjuri (JV) } 682d815e721SGreg Kurz } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH || 683d815e721SGreg Kurz fs_ctx->export_flags & V9FS_SM_NONE) { 684d815e721SGreg Kurz err = mknodat(dirfd, name, credp->fc_mode, credp->fc_rdev); 6852c30dd74SAneesh Kumar K.V if (err == -1) { 6862c30dd74SAneesh Kumar K.V goto out; 6872c30dd74SAneesh Kumar K.V } 688d815e721SGreg Kurz err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp); 6892c30dd74SAneesh Kumar K.V if (err == -1) { 6901c293312SVenkateswararao Jujjuri (JV) goto err_end; 6911c293312SVenkateswararao Jujjuri (JV) } 6921c293312SVenkateswararao Jujjuri (JV) } 6932289be19SAneesh Kumar K.V goto out; 6941c293312SVenkateswararao Jujjuri (JV) 6951c293312SVenkateswararao Jujjuri (JV) err_end: 696d815e721SGreg Kurz unlinkat_preserve_errno(dirfd, name, 0); 6972289be19SAneesh Kumar K.V out: 698d815e721SGreg Kurz close_preserve_errno(dirfd); 6991c293312SVenkateswararao Jujjuri (JV) return err; 700c494dd6fSAnthony Liguori } 701c494dd6fSAnthony Liguori 7022289be19SAneesh Kumar K.V static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path, 7032289be19SAneesh Kumar K.V const char *name, FsCred *credp) 704c494dd6fSAnthony Liguori { 70500ec5c37SVenkateswararao Jujjuri (JV) int err = -1; 7063f3a1699SGreg Kurz int dirfd; 70700ec5c37SVenkateswararao Jujjuri (JV) 7087a95434eSGreg Kurz if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE && 7097a95434eSGreg Kurz local_is_mapped_file_metadata(fs_ctx, name)) { 7107a95434eSGreg Kurz errno = EINVAL; 7117a95434eSGreg Kurz return -1; 7127a95434eSGreg Kurz } 7137a95434eSGreg Kurz 7143f3a1699SGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dir_path->data); 7153f3a1699SGreg Kurz if (dirfd == -1) { 7163f3a1699SGreg Kurz return -1; 7173f3a1699SGreg Kurz } 7182289be19SAneesh Kumar K.V 7193f3a1699SGreg Kurz if (fs_ctx->export_flags & V9FS_SM_MAPPED || 7203f3a1699SGreg Kurz fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 721b96feb2cSTobias Schramm err = mkdirat(dirfd, name, fs_ctx->dmode); 7223f3a1699SGreg Kurz if (err == -1) { 7233f3a1699SGreg Kurz goto out; 7243f3a1699SGreg Kurz } 7253f3a1699SGreg Kurz credp->fc_mode = credp->fc_mode | S_IFDIR; 7263f3a1699SGreg Kurz 727b97400caSAneesh Kumar K.V if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 7283f3a1699SGreg Kurz err = local_set_xattrat(dirfd, name, credp); 7293f3a1699SGreg Kurz } else { 7303f3a1699SGreg Kurz err = local_set_mapped_file_attrat(dirfd, name, credp); 73100ec5c37SVenkateswararao Jujjuri (JV) } 73200ec5c37SVenkateswararao Jujjuri (JV) if (err == -1) { 73300ec5c37SVenkateswararao Jujjuri (JV) goto err_end; 73400ec5c37SVenkateswararao Jujjuri (JV) } 7353f3a1699SGreg Kurz } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH || 7363f3a1699SGreg Kurz fs_ctx->export_flags & V9FS_SM_NONE) { 7373f3a1699SGreg Kurz err = mkdirat(dirfd, name, credp->fc_mode); 7382c30dd74SAneesh Kumar K.V if (err == -1) { 7392c30dd74SAneesh Kumar K.V goto out; 7402c30dd74SAneesh Kumar K.V } 7413f3a1699SGreg Kurz err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp); 7422c30dd74SAneesh Kumar K.V if (err == -1) { 74300ec5c37SVenkateswararao Jujjuri (JV) goto err_end; 74400ec5c37SVenkateswararao Jujjuri (JV) } 74500ec5c37SVenkateswararao Jujjuri (JV) } 7462289be19SAneesh Kumar K.V goto out; 74700ec5c37SVenkateswararao Jujjuri (JV) 74800ec5c37SVenkateswararao Jujjuri (JV) err_end: 7493f3a1699SGreg Kurz unlinkat_preserve_errno(dirfd, name, AT_REMOVEDIR); 7502289be19SAneesh Kumar K.V out: 7513f3a1699SGreg Kurz close_preserve_errno(dirfd); 75200ec5c37SVenkateswararao Jujjuri (JV) return err; 753c494dd6fSAnthony Liguori } 754c494dd6fSAnthony Liguori 7558b888272SAneesh Kumar K.V static int local_fstat(FsContext *fs_ctx, int fid_type, 756cc720ddbSAneesh Kumar K.V V9fsFidOpenState *fs, struct stat *stbuf) 757c494dd6fSAnthony Liguori { 7588b888272SAneesh Kumar K.V int err, fd; 7598b888272SAneesh Kumar K.V 7608b888272SAneesh Kumar K.V if (fid_type == P9_FID_DIR) { 761f314ea4eSGreg Kurz fd = dirfd(fs->dir.stream); 7628b888272SAneesh Kumar K.V } else { 7638b888272SAneesh Kumar K.V fd = fs->fd; 7648b888272SAneesh Kumar K.V } 7658b888272SAneesh Kumar K.V 7668b888272SAneesh Kumar K.V err = fstat(fd, stbuf); 7671237ad76SVenkateswararao Jujjuri (JV) if (err) { 7681237ad76SVenkateswararao Jujjuri (JV) return err; 7691237ad76SVenkateswararao Jujjuri (JV) } 770b97400caSAneesh Kumar K.V if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 7711237ad76SVenkateswararao Jujjuri (JV) /* Actual credentials are part of extended attrs */ 7721237ad76SVenkateswararao Jujjuri (JV) uid_t tmp_uid; 7731237ad76SVenkateswararao Jujjuri (JV) gid_t tmp_gid; 7741237ad76SVenkateswararao Jujjuri (JV) mode_t tmp_mode; 7751237ad76SVenkateswararao Jujjuri (JV) dev_t tmp_dev; 7761237ad76SVenkateswararao Jujjuri (JV) 777f8ad4a89SAneesh Kumar K.V if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) { 778f8ad4a89SAneesh Kumar K.V stbuf->st_uid = le32_to_cpu(tmp_uid); 7791237ad76SVenkateswararao Jujjuri (JV) } 780f8ad4a89SAneesh Kumar K.V if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) { 781f8ad4a89SAneesh Kumar K.V stbuf->st_gid = le32_to_cpu(tmp_gid); 7821237ad76SVenkateswararao Jujjuri (JV) } 783f8ad4a89SAneesh Kumar K.V if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) { 784f8ad4a89SAneesh Kumar K.V stbuf->st_mode = le32_to_cpu(tmp_mode); 7851237ad76SVenkateswararao Jujjuri (JV) } 786f8ad4a89SAneesh Kumar K.V if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) { 787f8ad4a89SAneesh Kumar K.V stbuf->st_rdev = le64_to_cpu(tmp_dev); 7881237ad76SVenkateswararao Jujjuri (JV) } 7892c30dd74SAneesh Kumar K.V } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 7902c30dd74SAneesh Kumar K.V errno = EOPNOTSUPP; 7912c30dd74SAneesh Kumar K.V return -1; 7921237ad76SVenkateswararao Jujjuri (JV) } 7931237ad76SVenkateswararao Jujjuri (JV) return err; 794c494dd6fSAnthony Liguori } 795c494dd6fSAnthony Liguori 7962289be19SAneesh Kumar K.V static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name, 797cc720ddbSAneesh Kumar K.V int flags, FsCred *credp, V9fsFidOpenState *fs) 798c494dd6fSAnthony Liguori { 7994750a96fSVenkateswararao Jujjuri (JV) int fd = -1; 8004750a96fSVenkateswararao Jujjuri (JV) int err = -1; 801a565fea5SGreg Kurz int dirfd; 8024750a96fSVenkateswararao Jujjuri (JV) 8037a95434eSGreg Kurz if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE && 8047a95434eSGreg Kurz local_is_mapped_file_metadata(fs_ctx, name)) { 8057a95434eSGreg Kurz errno = EINVAL; 8067a95434eSGreg Kurz return -1; 8077a95434eSGreg Kurz } 8087a95434eSGreg Kurz 8090ceb092eSAneesh Kumar K.V /* 8100ceb092eSAneesh Kumar K.V * Mark all the open to not follow symlinks 8110ceb092eSAneesh Kumar K.V */ 8120ceb092eSAneesh Kumar K.V flags |= O_NOFOLLOW; 8130ceb092eSAneesh Kumar K.V 814a565fea5SGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dir_path->data); 815a565fea5SGreg Kurz if (dirfd == -1) { 816a565fea5SGreg Kurz return -1; 817a565fea5SGreg Kurz } 8182289be19SAneesh Kumar K.V 8194750a96fSVenkateswararao Jujjuri (JV) /* Determine the security model */ 820a565fea5SGreg Kurz if (fs_ctx->export_flags & V9FS_SM_MAPPED || 821a565fea5SGreg Kurz fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 822b96feb2cSTobias Schramm fd = openat_file(dirfd, name, flags, fs_ctx->fmode); 823a565fea5SGreg Kurz if (fd == -1) { 824a565fea5SGreg Kurz goto out; 825a565fea5SGreg Kurz } 826a565fea5SGreg Kurz credp->fc_mode = credp->fc_mode|S_IFREG; 827b97400caSAneesh Kumar K.V if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 8284750a96fSVenkateswararao Jujjuri (JV) /* Set cleint credentials in xattr */ 829a565fea5SGreg Kurz err = local_set_xattrat(dirfd, name, credp); 830a565fea5SGreg Kurz } else { 831a565fea5SGreg Kurz err = local_set_mapped_file_attrat(dirfd, name, credp); 8324750a96fSVenkateswararao Jujjuri (JV) } 8332c30dd74SAneesh Kumar K.V if (err == -1) { 8342c30dd74SAneesh Kumar K.V goto err_end; 8352c30dd74SAneesh Kumar K.V } 836b97400caSAneesh Kumar K.V } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 837b97400caSAneesh Kumar K.V (fs_ctx->export_flags & V9FS_SM_NONE)) { 838a565fea5SGreg Kurz fd = openat_file(dirfd, name, flags, credp->fc_mode); 8394750a96fSVenkateswararao Jujjuri (JV) if (fd == -1) { 8402289be19SAneesh Kumar K.V goto out; 8414750a96fSVenkateswararao Jujjuri (JV) } 842a565fea5SGreg Kurz err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp); 8434750a96fSVenkateswararao Jujjuri (JV) if (err == -1) { 8444750a96fSVenkateswararao Jujjuri (JV) goto err_end; 8454750a96fSVenkateswararao Jujjuri (JV) } 8464750a96fSVenkateswararao Jujjuri (JV) } 8472289be19SAneesh Kumar K.V err = fd; 848cc720ddbSAneesh Kumar K.V fs->fd = fd; 8492289be19SAneesh Kumar K.V goto out; 8504750a96fSVenkateswararao Jujjuri (JV) 8514750a96fSVenkateswararao Jujjuri (JV) err_end: 852a565fea5SGreg Kurz unlinkat_preserve_errno(dirfd, name, 853a565fea5SGreg Kurz flags & O_DIRECTORY ? AT_REMOVEDIR : 0); 854a565fea5SGreg Kurz close_preserve_errno(fd); 8552289be19SAneesh Kumar K.V out: 856a565fea5SGreg Kurz close_preserve_errno(dirfd); 8574750a96fSVenkateswararao Jujjuri (JV) return err; 858c494dd6fSAnthony Liguori } 859c494dd6fSAnthony Liguori 860758e8e38SVenkateswararao Jujjuri (JV) 861879c2813SVenkateswararao Jujjuri (JV) static int local_symlink(FsContext *fs_ctx, const char *oldpath, 8622289be19SAneesh Kumar K.V V9fsPath *dir_path, const char *name, FsCred *credp) 863c494dd6fSAnthony Liguori { 864879c2813SVenkateswararao Jujjuri (JV) int err = -1; 86538771613SGreg Kurz int dirfd; 866879c2813SVenkateswararao Jujjuri (JV) 8677a95434eSGreg Kurz if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE && 8687a95434eSGreg Kurz local_is_mapped_file_metadata(fs_ctx, name)) { 8697a95434eSGreg Kurz errno = EINVAL; 8707a95434eSGreg Kurz return -1; 8717a95434eSGreg Kurz } 8727a95434eSGreg Kurz 87338771613SGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dir_path->data); 87438771613SGreg Kurz if (dirfd == -1) { 87538771613SGreg Kurz return -1; 87638771613SGreg Kurz } 8772289be19SAneesh Kumar K.V 878879c2813SVenkateswararao Jujjuri (JV) /* Determine the security model */ 87938771613SGreg Kurz if (fs_ctx->export_flags & V9FS_SM_MAPPED || 88038771613SGreg Kurz fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 88138771613SGreg Kurz int fd; 88238771613SGreg Kurz ssize_t oldpath_size, write_size; 88338771613SGreg Kurz 88438771613SGreg Kurz fd = openat_file(dirfd, name, O_CREAT | O_EXCL | O_RDWR, 885b96feb2cSTobias Schramm fs_ctx->fmode); 88638771613SGreg Kurz if (fd == -1) { 88738771613SGreg Kurz goto out; 88838771613SGreg Kurz } 88938771613SGreg Kurz /* Write the oldpath (target) to the file. */ 89038771613SGreg Kurz oldpath_size = strlen(oldpath); 89138771613SGreg Kurz do { 89238771613SGreg Kurz write_size = write(fd, (void *)oldpath, oldpath_size); 89338771613SGreg Kurz } while (write_size == -1 && errno == EINTR); 89438771613SGreg Kurz close_preserve_errno(fd); 89538771613SGreg Kurz 89638771613SGreg Kurz if (write_size != oldpath_size) { 89738771613SGreg Kurz goto err_end; 89838771613SGreg Kurz } 89938771613SGreg Kurz /* Set cleint credentials in symlink's xattr */ 90038771613SGreg Kurz credp->fc_mode = credp->fc_mode | S_IFLNK; 90138771613SGreg Kurz 902b97400caSAneesh Kumar K.V if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 90338771613SGreg Kurz err = local_set_xattrat(dirfd, name, credp); 90438771613SGreg Kurz } else { 90538771613SGreg Kurz err = local_set_mapped_file_attrat(dirfd, name, credp); 906879c2813SVenkateswararao Jujjuri (JV) } 907879c2813SVenkateswararao Jujjuri (JV) if (err == -1) { 908879c2813SVenkateswararao Jujjuri (JV) goto err_end; 909879c2813SVenkateswararao Jujjuri (JV) } 91038771613SGreg Kurz } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH || 91138771613SGreg Kurz fs_ctx->export_flags & V9FS_SM_NONE) { 91238771613SGreg Kurz err = symlinkat(oldpath, dirfd, name); 913879c2813SVenkateswararao Jujjuri (JV) if (err) { 9142289be19SAneesh Kumar K.V goto out; 915879c2813SVenkateswararao Jujjuri (JV) } 91638771613SGreg Kurz err = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid, 91738771613SGreg Kurz AT_SYMLINK_NOFOLLOW); 918879c2813SVenkateswararao Jujjuri (JV) if (err == -1) { 91912848bfcSAneesh Kumar K.V /* 92012848bfcSAneesh Kumar K.V * If we fail to change ownership and if we are 92112848bfcSAneesh Kumar K.V * using security model none. Ignore the error 92212848bfcSAneesh Kumar K.V */ 923b97400caSAneesh Kumar K.V if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) { 924879c2813SVenkateswararao Jujjuri (JV) goto err_end; 92538771613SGreg Kurz } else { 92612848bfcSAneesh Kumar K.V err = 0; 927879c2813SVenkateswararao Jujjuri (JV) } 928879c2813SVenkateswararao Jujjuri (JV) } 92938771613SGreg Kurz } 9302289be19SAneesh Kumar K.V goto out; 931879c2813SVenkateswararao Jujjuri (JV) 932879c2813SVenkateswararao Jujjuri (JV) err_end: 93338771613SGreg Kurz unlinkat_preserve_errno(dirfd, name, 0); 9342289be19SAneesh Kumar K.V out: 93538771613SGreg Kurz close_preserve_errno(dirfd); 936879c2813SVenkateswararao Jujjuri (JV) return err; 937c494dd6fSAnthony Liguori } 938c494dd6fSAnthony Liguori 9392289be19SAneesh Kumar K.V static int local_link(FsContext *ctx, V9fsPath *oldpath, 9402289be19SAneesh Kumar K.V V9fsPath *dirpath, const char *name) 941c494dd6fSAnthony Liguori { 942ad0b46e6SGreg Kurz char *odirpath = g_path_get_dirname(oldpath->data); 943ad0b46e6SGreg Kurz char *oname = g_path_get_basename(oldpath->data); 944ad0b46e6SGreg Kurz int ret = -1; 945ad0b46e6SGreg Kurz int odirfd, ndirfd; 946c494dd6fSAnthony Liguori 9477a95434eSGreg Kurz if (ctx->export_flags & V9FS_SM_MAPPED_FILE && 9487a95434eSGreg Kurz local_is_mapped_file_metadata(ctx, name)) { 9497a95434eSGreg Kurz errno = EINVAL; 9507a95434eSGreg Kurz return -1; 9517a95434eSGreg Kurz } 9527a95434eSGreg Kurz 953ad0b46e6SGreg Kurz odirfd = local_opendir_nofollow(ctx, odirpath); 954ad0b46e6SGreg Kurz if (odirfd == -1) { 9556dd4b1f1SGreg Kurz goto out; 9566dd4b1f1SGreg Kurz } 9572289be19SAneesh Kumar K.V 958ad0b46e6SGreg Kurz ndirfd = local_opendir_nofollow(ctx, dirpath->data); 959ad0b46e6SGreg Kurz if (ndirfd == -1) { 960ad0b46e6SGreg Kurz close_preserve_errno(odirfd); 961ad0b46e6SGreg Kurz goto out; 962ad0b46e6SGreg Kurz } 963ad0b46e6SGreg Kurz 964ad0b46e6SGreg Kurz ret = linkat(odirfd, oname, ndirfd, name, 0); 965ad0b46e6SGreg Kurz if (ret < 0) { 966ad0b46e6SGreg Kurz goto out_close; 967ad0b46e6SGreg Kurz } 9682c30dd74SAneesh Kumar K.V 9692c30dd74SAneesh Kumar K.V /* now link the virtfs_metadata files */ 9706dd4b1f1SGreg Kurz if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 971ad0b46e6SGreg Kurz int omap_dirfd, nmap_dirfd; 9726dd4b1f1SGreg Kurz 973ad0b46e6SGreg Kurz ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700); 974ad0b46e6SGreg Kurz if (ret < 0 && errno != EEXIST) { 975ad0b46e6SGreg Kurz goto err_undo_link; 9762c30dd74SAneesh Kumar K.V } 977ad0b46e6SGreg Kurz 978ad0b46e6SGreg Kurz omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR); 979ad0b46e6SGreg Kurz if (omap_dirfd == -1) { 980ad0b46e6SGreg Kurz goto err; 981ad0b46e6SGreg Kurz } 982ad0b46e6SGreg Kurz 983ad0b46e6SGreg Kurz nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR); 984ad0b46e6SGreg Kurz if (nmap_dirfd == -1) { 985ad0b46e6SGreg Kurz close_preserve_errno(omap_dirfd); 986ad0b46e6SGreg Kurz goto err; 987ad0b46e6SGreg Kurz } 988ad0b46e6SGreg Kurz 989ad0b46e6SGreg Kurz ret = linkat(omap_dirfd, oname, nmap_dirfd, name, 0); 990ad0b46e6SGreg Kurz close_preserve_errno(nmap_dirfd); 991ad0b46e6SGreg Kurz close_preserve_errno(omap_dirfd); 9922c30dd74SAneesh Kumar K.V if (ret < 0 && errno != ENOENT) { 993ad0b46e6SGreg Kurz goto err_undo_link; 9942c30dd74SAneesh Kumar K.V } 9956dd4b1f1SGreg Kurz 996ad0b46e6SGreg Kurz ret = 0; 9972c30dd74SAneesh Kumar K.V } 998ad0b46e6SGreg Kurz goto out_close; 999ad0b46e6SGreg Kurz 1000ad0b46e6SGreg Kurz err: 1001ad0b46e6SGreg Kurz ret = -1; 1002ad0b46e6SGreg Kurz err_undo_link: 1003ad0b46e6SGreg Kurz unlinkat_preserve_errno(ndirfd, name, 0); 1004ad0b46e6SGreg Kurz out_close: 1005ad0b46e6SGreg Kurz close_preserve_errno(ndirfd); 1006ad0b46e6SGreg Kurz close_preserve_errno(odirfd); 10076dd4b1f1SGreg Kurz out: 1008ad0b46e6SGreg Kurz g_free(oname); 1009ad0b46e6SGreg Kurz g_free(odirpath); 10102289be19SAneesh Kumar K.V return ret; 1011c494dd6fSAnthony Liguori } 1012c494dd6fSAnthony Liguori 10132289be19SAneesh Kumar K.V static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size) 10148cf89e00SAnthony Liguori { 1015ac125d99SGreg Kurz int fd, ret; 10162289be19SAneesh Kumar K.V 1017ac125d99SGreg Kurz fd = local_open_nofollow(ctx, fs_path->data, O_WRONLY, 0); 1018ac125d99SGreg Kurz if (fd == -1) { 1019ac125d99SGreg Kurz return -1; 1020ac125d99SGreg Kurz } 1021ac125d99SGreg Kurz ret = ftruncate(fd, size); 1022ac125d99SGreg Kurz close_preserve_errno(fd); 10234fa4ce71SChen Gang return ret; 10248cf89e00SAnthony Liguori } 10258cf89e00SAnthony Liguori 10262289be19SAneesh Kumar K.V static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp) 10278cf89e00SAnthony Liguori { 1028d369f207SGreg Kurz char *dirpath = g_path_get_dirname(fs_path->data); 1029d369f207SGreg Kurz char *name = g_path_get_basename(fs_path->data); 10304fa4ce71SChen Gang int ret = -1; 1031d369f207SGreg Kurz int dirfd; 1032d369f207SGreg Kurz 1033d369f207SGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dirpath); 1034d369f207SGreg Kurz if (dirfd == -1) { 1035d369f207SGreg Kurz goto out; 1036d369f207SGreg Kurz } 10372289be19SAneesh Kumar K.V 1038c79ce737SSripathi Kodi if ((credp->fc_uid == -1 && credp->fc_gid == -1) || 103917b1971fSAneesh Kumar K.V (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 104017b1971fSAneesh Kumar K.V (fs_ctx->export_flags & V9FS_SM_NONE)) { 1041d369f207SGreg Kurz ret = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid, 1042d369f207SGreg Kurz AT_SYMLINK_NOFOLLOW); 1043b97400caSAneesh Kumar K.V } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 1044d369f207SGreg Kurz ret = local_set_xattrat(dirfd, name, credp); 10452c30dd74SAneesh Kumar K.V } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 1046d369f207SGreg Kurz ret = local_set_mapped_file_attrat(dirfd, name, credp); 1047f7613beeSVenkateswararao Jujjuri (JV) } 1048d369f207SGreg Kurz 1049d369f207SGreg Kurz close_preserve_errno(dirfd); 1050d369f207SGreg Kurz out: 1051d369f207SGreg Kurz g_free(name); 1052d369f207SGreg Kurz g_free(dirpath); 10534fa4ce71SChen Gang return ret; 10548cf89e00SAnthony Liguori } 10558cf89e00SAnthony Liguori 10562289be19SAneesh Kumar K.V static int local_utimensat(FsContext *s, V9fsPath *fs_path, 105774bc02b2SM. Mohan Kumar const struct timespec *buf) 10588cf89e00SAnthony Liguori { 1059a33eda0dSGreg Kurz char *dirpath = g_path_get_dirname(fs_path->data); 1060a33eda0dSGreg Kurz char *name = g_path_get_basename(fs_path->data); 1061a33eda0dSGreg Kurz int dirfd, ret = -1; 10622289be19SAneesh Kumar K.V 1063a33eda0dSGreg Kurz dirfd = local_opendir_nofollow(s, dirpath); 1064a33eda0dSGreg Kurz if (dirfd == -1) { 1065a33eda0dSGreg Kurz goto out; 1066a33eda0dSGreg Kurz } 1067a33eda0dSGreg Kurz 1068a33eda0dSGreg Kurz ret = utimensat(dirfd, name, buf, AT_SYMLINK_NOFOLLOW); 1069a33eda0dSGreg Kurz close_preserve_errno(dirfd); 1070a33eda0dSGreg Kurz out: 1071a33eda0dSGreg Kurz g_free(dirpath); 1072a33eda0dSGreg Kurz g_free(name); 10734fa4ce71SChen Gang return ret; 10748cf89e00SAnthony Liguori } 10758cf89e00SAnthony Liguori 1076df4938a6SGreg Kurz static int local_unlinkat_common(FsContext *ctx, int dirfd, const char *name, 1077df4938a6SGreg Kurz int flags) 10785bae1900SAnthony Liguori { 1079df4938a6SGreg Kurz int ret = -1; 10802c30dd74SAneesh Kumar K.V 10812c30dd74SAneesh Kumar K.V if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 1082df4938a6SGreg Kurz int map_dirfd; 1083df4938a6SGreg Kurz 10846a87e792SGreg Kurz /* We need to remove the metadata as well: 10856a87e792SGreg Kurz * - the metadata directory if we're removing a directory 10866a87e792SGreg Kurz * - the metadata file in the parent's metadata directory 10876a87e792SGreg Kurz * 10886a87e792SGreg Kurz * If any of these are missing (ie, ENOENT) then we're probably 10896a87e792SGreg Kurz * trying to remove something that wasn't created in mapped-file 10906a87e792SGreg Kurz * mode. We just ignore the error. 10916a87e792SGreg Kurz */ 1092df4938a6SGreg Kurz if (flags == AT_REMOVEDIR) { 1093df4938a6SGreg Kurz int fd; 1094df4938a6SGreg Kurz 1095b003fc0dSGreg Kurz fd = openat_dir(dirfd, name); 1096df4938a6SGreg Kurz if (fd == -1) { 10972c30dd74SAneesh Kumar K.V goto err_out; 10982c30dd74SAneesh Kumar K.V } 1099df4938a6SGreg Kurz ret = unlinkat(fd, VIRTFS_META_DIR, AT_REMOVEDIR); 1100df4938a6SGreg Kurz close_preserve_errno(fd); 1101df4938a6SGreg Kurz if (ret < 0 && errno != ENOENT) { 11022c30dd74SAneesh Kumar K.V goto err_out; 11032c30dd74SAneesh Kumar K.V } 11042c30dd74SAneesh Kumar K.V } 1105df4938a6SGreg Kurz map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR); 11066a87e792SGreg Kurz if (map_dirfd != -1) { 1107df4938a6SGreg Kurz ret = unlinkat(map_dirfd, name, 0); 1108df4938a6SGreg Kurz close_preserve_errno(map_dirfd); 1109df4938a6SGreg Kurz if (ret < 0 && errno != ENOENT) { 11106a87e792SGreg Kurz goto err_out; 11116a87e792SGreg Kurz } 11126a87e792SGreg Kurz } else if (errno != ENOENT) { 11132c30dd74SAneesh Kumar K.V goto err_out; 11142c30dd74SAneesh Kumar K.V } 11152c30dd74SAneesh Kumar K.V } 11164fa4ce71SChen Gang 1117df4938a6SGreg Kurz ret = unlinkat(dirfd, name, flags); 11182c30dd74SAneesh Kumar K.V err_out: 1119df4938a6SGreg Kurz return ret; 1120df4938a6SGreg Kurz } 1121df4938a6SGreg Kurz 11228cf89e00SAnthony Liguori static int local_remove(FsContext *ctx, const char *path) 11238cf89e00SAnthony Liguori { 11248cf89e00SAnthony Liguori struct stat stbuf; 1125a0e640a8SGreg Kurz char *dirpath = g_path_get_dirname(path); 1126a0e640a8SGreg Kurz char *name = g_path_get_basename(path); 1127a0e640a8SGreg Kurz int flags = 0; 1128a0e640a8SGreg Kurz int dirfd; 1129a0e640a8SGreg Kurz int err = -1; 11308cf89e00SAnthony Liguori 1131a0e640a8SGreg Kurz dirfd = local_opendir_nofollow(ctx, dirpath); 1132b7361d46SGreg Kurz if (dirfd == -1) { 1133a0e640a8SGreg Kurz goto out; 1134a0e640a8SGreg Kurz } 1135a0e640a8SGreg Kurz 1136790db7efSBruce Rogers if (fstatat(dirfd, name, &stbuf, AT_SYMLINK_NOFOLLOW) < 0) { 11378cf89e00SAnthony Liguori goto err_out; 11388cf89e00SAnthony Liguori } 1139a0e640a8SGreg Kurz 11408cf89e00SAnthony Liguori if (S_ISDIR(stbuf.st_mode)) { 1141a0e640a8SGreg Kurz flags |= AT_REMOVEDIR; 11428cf89e00SAnthony Liguori } 11435bae1900SAnthony Liguori 1144a0e640a8SGreg Kurz err = local_unlinkat_common(ctx, dirfd, name, flags); 11452c30dd74SAneesh Kumar K.V err_out: 1146a0e640a8SGreg Kurz close_preserve_errno(dirfd); 1147a0e640a8SGreg Kurz out: 1148a0e640a8SGreg Kurz g_free(name); 1149a0e640a8SGreg Kurz g_free(dirpath); 11502c30dd74SAneesh Kumar K.V return err; 11515bae1900SAnthony Liguori } 11525bae1900SAnthony Liguori 11538b888272SAneesh Kumar K.V static int local_fsync(FsContext *ctx, int fid_type, 11548b888272SAneesh Kumar K.V V9fsFidOpenState *fs, int datasync) 11558cf89e00SAnthony Liguori { 11568b888272SAneesh Kumar K.V int fd; 11578b888272SAneesh Kumar K.V 11588b888272SAneesh Kumar K.V if (fid_type == P9_FID_DIR) { 1159f314ea4eSGreg Kurz fd = dirfd(fs->dir.stream); 116049594973SVenkateswararao Jujjuri (JV) } else { 11618b888272SAneesh Kumar K.V fd = fs->fd; 11628b888272SAneesh Kumar K.V } 11638b888272SAneesh Kumar K.V 11648b888272SAneesh Kumar K.V if (datasync) { 11658b888272SAneesh Kumar K.V return qemu_fdatasync(fd); 11668b888272SAneesh Kumar K.V } else { 11678b888272SAneesh Kumar K.V return fsync(fd); 11688cf89e00SAnthony Liguori } 116949594973SVenkateswararao Jujjuri (JV) } 11708cf89e00SAnthony Liguori 11712289be19SAneesh Kumar K.V static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf) 1172be940c87SM. Mohan Kumar { 117331e51d1cSGreg Kurz int fd, ret; 11742289be19SAneesh Kumar K.V 117531e51d1cSGreg Kurz fd = local_open_nofollow(s, fs_path->data, O_RDONLY, 0); 117623da0145SGreg Kurz if (fd == -1) { 117723da0145SGreg Kurz return -1; 117823da0145SGreg Kurz } 117931e51d1cSGreg Kurz ret = fstatfs(fd, stbuf); 118031e51d1cSGreg Kurz close_preserve_errno(fd); 11814fa4ce71SChen Gang return ret; 1182be940c87SM. Mohan Kumar } 1183be940c87SM. Mohan Kumar 11842289be19SAneesh Kumar K.V static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path, 1185fa32ef88SAneesh Kumar K.V const char *name, void *value, size_t size) 1186fa32ef88SAneesh Kumar K.V { 11872289be19SAneesh Kumar K.V char *path = fs_path->data; 11882289be19SAneesh Kumar K.V 1189fc22118dSAneesh Kumar K.V return v9fs_get_xattr(ctx, path, name, value, size); 1190fa32ef88SAneesh Kumar K.V } 1191fa32ef88SAneesh Kumar K.V 11922289be19SAneesh Kumar K.V static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path, 1193fa32ef88SAneesh Kumar K.V void *value, size_t size) 1194fa32ef88SAneesh Kumar K.V { 11952289be19SAneesh Kumar K.V char *path = fs_path->data; 11962289be19SAneesh Kumar K.V 1197fc22118dSAneesh Kumar K.V return v9fs_list_xattr(ctx, path, value, size); 119861b6c499SAneesh Kumar K.V } 119961b6c499SAneesh Kumar K.V 12002289be19SAneesh Kumar K.V static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name, 120110b468bdSAneesh Kumar K.V void *value, size_t size, int flags) 120210b468bdSAneesh Kumar K.V { 12032289be19SAneesh Kumar K.V char *path = fs_path->data; 12042289be19SAneesh Kumar K.V 1205fc22118dSAneesh Kumar K.V return v9fs_set_xattr(ctx, path, name, value, size, flags); 120610b468bdSAneesh Kumar K.V } 120710b468bdSAneesh Kumar K.V 12082289be19SAneesh Kumar K.V static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path, 12092289be19SAneesh Kumar K.V const char *name) 12109ed3ef26SAneesh Kumar K.V { 12112289be19SAneesh Kumar K.V char *path = fs_path->data; 12122289be19SAneesh Kumar K.V 1213fc22118dSAneesh Kumar K.V return v9fs_remove_xattr(ctx, path, name); 12149ed3ef26SAneesh Kumar K.V } 12159ed3ef26SAneesh Kumar K.V 12162289be19SAneesh Kumar K.V static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path, 12172289be19SAneesh Kumar K.V const char *name, V9fsPath *target) 12182289be19SAneesh Kumar K.V { 12197a95434eSGreg Kurz if (ctx->export_flags & V9FS_SM_MAPPED_FILE && 12207a95434eSGreg Kurz local_is_mapped_file_metadata(ctx, name)) { 12217a95434eSGreg Kurz errno = EINVAL; 12227a95434eSGreg Kurz return -1; 12237a95434eSGreg Kurz } 12247a95434eSGreg Kurz 12252289be19SAneesh Kumar K.V if (dir_path) { 1226f57f5878SGreg Kurz if (!strcmp(name, ".")) { 1227f57f5878SGreg Kurz /* "." relative to "foo/bar" is "foo/bar" */ 1228f57f5878SGreg Kurz v9fs_path_copy(target, dir_path); 1229f57f5878SGreg Kurz } else if (!strcmp(name, "..")) { 1230f57f5878SGreg Kurz if (!strcmp(dir_path->data, ".")) { 1231f57f5878SGreg Kurz /* ".." relative to the root is "." */ 1232f57f5878SGreg Kurz v9fs_path_sprintf(target, "."); 12339c6b899fSGreg Kurz } else { 1234f57f5878SGreg Kurz char *tmp = g_path_get_dirname(dir_path->data); 1235f57f5878SGreg Kurz /* Symbolic links are resolved by the client. We can assume 1236f57f5878SGreg Kurz * that ".." relative to "foo/bar" is equivalent to "foo" 12379c6b899fSGreg Kurz */ 1238f57f5878SGreg Kurz v9fs_path_sprintf(target, "%s", tmp); 1239f57f5878SGreg Kurz g_free(tmp); 1240f57f5878SGreg Kurz } 1241f57f5878SGreg Kurz } else { 1242f57f5878SGreg Kurz assert(!strchr(name, '/')); 1243f57f5878SGreg Kurz v9fs_path_sprintf(target, "%s/%s", dir_path->data, name); 1244f57f5878SGreg Kurz } 1245f57f5878SGreg Kurz } else if (!strcmp(name, "/") || !strcmp(name, ".") || 1246f57f5878SGreg Kurz !strcmp(name, "..")) { 1247f57f5878SGreg Kurz /* This is the root fid */ 1248f57f5878SGreg Kurz v9fs_path_sprintf(target, "."); 1249f57f5878SGreg Kurz } else { 1250f57f5878SGreg Kurz assert(!strchr(name, '/')); 1251f57f5878SGreg Kurz v9fs_path_sprintf(target, "./%s", name); 12522289be19SAneesh Kumar K.V } 12532289be19SAneesh Kumar K.V return 0; 12542289be19SAneesh Kumar K.V } 12552289be19SAneesh Kumar K.V 12562289be19SAneesh Kumar K.V static int local_renameat(FsContext *ctx, V9fsPath *olddir, 12572289be19SAneesh Kumar K.V const char *old_name, V9fsPath *newdir, 12582289be19SAneesh Kumar K.V const char *new_name) 12592289be19SAneesh Kumar K.V { 12602289be19SAneesh Kumar K.V int ret; 126199f2cf4bSGreg Kurz int odirfd, ndirfd; 12622289be19SAneesh Kumar K.V 12637a95434eSGreg Kurz if (ctx->export_flags & V9FS_SM_MAPPED_FILE && 12647a95434eSGreg Kurz (local_is_mapped_file_metadata(ctx, old_name) || 12657a95434eSGreg Kurz local_is_mapped_file_metadata(ctx, new_name))) { 12667a95434eSGreg Kurz errno = EINVAL; 12677a95434eSGreg Kurz return -1; 12687a95434eSGreg Kurz } 12697a95434eSGreg Kurz 127099f2cf4bSGreg Kurz odirfd = local_opendir_nofollow(ctx, olddir->data); 127199f2cf4bSGreg Kurz if (odirfd == -1) { 127299f2cf4bSGreg Kurz return -1; 127399f2cf4bSGreg Kurz } 12742289be19SAneesh Kumar K.V 127599f2cf4bSGreg Kurz ndirfd = local_opendir_nofollow(ctx, newdir->data); 127699f2cf4bSGreg Kurz if (ndirfd == -1) { 127799f2cf4bSGreg Kurz close_preserve_errno(odirfd); 127899f2cf4bSGreg Kurz return -1; 127999f2cf4bSGreg Kurz } 12802289be19SAneesh Kumar K.V 128199f2cf4bSGreg Kurz ret = renameat(odirfd, old_name, ndirfd, new_name); 128299f2cf4bSGreg Kurz if (ret < 0) { 128399f2cf4bSGreg Kurz goto out; 128499f2cf4bSGreg Kurz } 128599f2cf4bSGreg Kurz 128699f2cf4bSGreg Kurz if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 128799f2cf4bSGreg Kurz int omap_dirfd, nmap_dirfd; 128899f2cf4bSGreg Kurz 128999f2cf4bSGreg Kurz ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700); 129099f2cf4bSGreg Kurz if (ret < 0 && errno != EEXIST) { 129199f2cf4bSGreg Kurz goto err_undo_rename; 129299f2cf4bSGreg Kurz } 129399f2cf4bSGreg Kurz 12946dd4b1f1SGreg Kurz omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR); 129599f2cf4bSGreg Kurz if (omap_dirfd == -1) { 129699f2cf4bSGreg Kurz goto err; 129799f2cf4bSGreg Kurz } 129899f2cf4bSGreg Kurz 12996dd4b1f1SGreg Kurz nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR); 130099f2cf4bSGreg Kurz if (nmap_dirfd == -1) { 130199f2cf4bSGreg Kurz close_preserve_errno(omap_dirfd); 130299f2cf4bSGreg Kurz goto err; 130399f2cf4bSGreg Kurz } 130499f2cf4bSGreg Kurz 130599f2cf4bSGreg Kurz /* rename the .virtfs_metadata files */ 130699f2cf4bSGreg Kurz ret = renameat(omap_dirfd, old_name, nmap_dirfd, new_name); 130799f2cf4bSGreg Kurz close_preserve_errno(nmap_dirfd); 130899f2cf4bSGreg Kurz close_preserve_errno(omap_dirfd); 130999f2cf4bSGreg Kurz if (ret < 0 && errno != ENOENT) { 131099f2cf4bSGreg Kurz goto err_undo_rename; 131199f2cf4bSGreg Kurz } 131299f2cf4bSGreg Kurz 131399f2cf4bSGreg Kurz ret = 0; 131499f2cf4bSGreg Kurz } 131599f2cf4bSGreg Kurz goto out; 131699f2cf4bSGreg Kurz 131799f2cf4bSGreg Kurz err: 131899f2cf4bSGreg Kurz ret = -1; 131999f2cf4bSGreg Kurz err_undo_rename: 132099f2cf4bSGreg Kurz renameat_preserve_errno(ndirfd, new_name, odirfd, old_name); 132199f2cf4bSGreg Kurz out: 132299f2cf4bSGreg Kurz close_preserve_errno(ndirfd); 132399f2cf4bSGreg Kurz close_preserve_errno(odirfd); 13242289be19SAneesh Kumar K.V return ret; 13252289be19SAneesh Kumar K.V } 13262289be19SAneesh Kumar K.V 1327d2767edeSGreg Kurz static void v9fs_path_init_dirname(V9fsPath *path, const char *str) 1328d2767edeSGreg Kurz { 1329d2767edeSGreg Kurz path->data = g_path_get_dirname(str); 1330d2767edeSGreg Kurz path->size = strlen(path->data) + 1; 1331d2767edeSGreg Kurz } 1332d2767edeSGreg Kurz 1333d2767edeSGreg Kurz static int local_rename(FsContext *ctx, const char *oldpath, 1334d2767edeSGreg Kurz const char *newpath) 1335d2767edeSGreg Kurz { 1336d2767edeSGreg Kurz int err; 1337d2767edeSGreg Kurz char *oname = g_path_get_basename(oldpath); 1338d2767edeSGreg Kurz char *nname = g_path_get_basename(newpath); 1339d2767edeSGreg Kurz V9fsPath olddir, newdir; 1340d2767edeSGreg Kurz 1341d2767edeSGreg Kurz v9fs_path_init_dirname(&olddir, oldpath); 1342d2767edeSGreg Kurz v9fs_path_init_dirname(&newdir, newpath); 1343d2767edeSGreg Kurz 1344d2767edeSGreg Kurz err = local_renameat(ctx, &olddir, oname, &newdir, nname); 1345d2767edeSGreg Kurz 1346d2767edeSGreg Kurz v9fs_path_free(&newdir); 1347d2767edeSGreg Kurz v9fs_path_free(&olddir); 1348d2767edeSGreg Kurz g_free(nname); 1349d2767edeSGreg Kurz g_free(oname); 1350d2767edeSGreg Kurz 1351d2767edeSGreg Kurz return err; 1352d2767edeSGreg Kurz } 1353d2767edeSGreg Kurz 13542289be19SAneesh Kumar K.V static int local_unlinkat(FsContext *ctx, V9fsPath *dir, 13552289be19SAneesh Kumar K.V const char *name, int flags) 13562289be19SAneesh Kumar K.V { 13572289be19SAneesh Kumar K.V int ret; 1358df4938a6SGreg Kurz int dirfd; 13592c30dd74SAneesh Kumar K.V 13607a95434eSGreg Kurz if (ctx->export_flags & V9FS_SM_MAPPED_FILE && 13617a95434eSGreg Kurz local_is_mapped_file_metadata(ctx, name)) { 13627a95434eSGreg Kurz errno = EINVAL; 13637a95434eSGreg Kurz return -1; 13647a95434eSGreg Kurz } 13657a95434eSGreg Kurz 1366df4938a6SGreg Kurz dirfd = local_opendir_nofollow(ctx, dir->data); 1367df4938a6SGreg Kurz if (dirfd == -1) { 1368df4938a6SGreg Kurz return -1; 1369df4938a6SGreg Kurz } 13702289be19SAneesh Kumar K.V 1371df4938a6SGreg Kurz ret = local_unlinkat_common(ctx, dirfd, name, flags); 1372df4938a6SGreg Kurz close_preserve_errno(dirfd); 13732289be19SAneesh Kumar K.V return ret; 13742289be19SAneesh Kumar K.V } 13759ed3ef26SAneesh Kumar K.V 13765b7b2f9aSKeno Fischer #ifdef FS_IOC_GETVERSION 1377e06a765eSHarsh Prateek Bora static int local_ioc_getversion(FsContext *ctx, V9fsPath *path, 1378e06a765eSHarsh Prateek Bora mode_t st_mode, uint64_t *st_gen) 1379e06a765eSHarsh Prateek Bora { 13800e5fc994SKirill A. Shutemov int err; 1381cc720ddbSAneesh Kumar K.V V9fsFidOpenState fid_open; 1382cc720ddbSAneesh Kumar K.V 1383e06a765eSHarsh Prateek Bora /* 1384e06a765eSHarsh Prateek Bora * Do not try to open special files like device nodes, fifos etc 1385e06a765eSHarsh Prateek Bora * We can get fd for regular files and directories only 1386e06a765eSHarsh Prateek Bora */ 1387e06a765eSHarsh Prateek Bora if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) { 13881a9978a5SKirill A. Shutemov errno = ENOTTY; 13891a9978a5SKirill A. Shutemov return -1; 1390e06a765eSHarsh Prateek Bora } 1391cc720ddbSAneesh Kumar K.V err = local_open(ctx, path, O_RDONLY, &fid_open); 1392cc720ddbSAneesh Kumar K.V if (err < 0) { 1393cc720ddbSAneesh Kumar K.V return err; 1394e06a765eSHarsh Prateek Bora } 1395cc720ddbSAneesh Kumar K.V err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen); 1396cc720ddbSAneesh Kumar K.V local_close(ctx, &fid_open); 1397e06a765eSHarsh Prateek Bora return err; 13985b7b2f9aSKeno Fischer } 13990e5fc994SKirill A. Shutemov #endif 1400e06a765eSHarsh Prateek Bora 14015b7b2f9aSKeno Fischer static int local_ioc_getversion_init(FsContext *ctx, LocalData *data, Error **errp) 14020174fe73SAneesh Kumar K.V { 140300c90bd1SGreg Kurz #ifdef FS_IOC_GETVERSION 14045b7b2f9aSKeno Fischer struct statfs stbuf; 14055b7b2f9aSKeno Fischer 140600c90bd1SGreg Kurz /* 140700c90bd1SGreg Kurz * use ioc_getversion only if the ioctl is definied 140800c90bd1SGreg Kurz */ 14090e35a378SGreg Kurz if (fstatfs(data->mountfd, &stbuf) < 0) { 14102306271cSKeno Fischer error_setg_errno(errp, errno, 14112306271cSKeno Fischer "failed to stat file system at '%s'", ctx->fs_root); 14125b7b2f9aSKeno Fischer return -1; 141300c90bd1SGreg Kurz } 141400c90bd1SGreg Kurz switch (stbuf.f_type) { 141500c90bd1SGreg Kurz case EXT2_SUPER_MAGIC: 141600c90bd1SGreg Kurz case BTRFS_SUPER_MAGIC: 141700c90bd1SGreg Kurz case REISERFS_SUPER_MAGIC: 141800c90bd1SGreg Kurz case XFS_SUPER_MAGIC: 141900c90bd1SGreg Kurz ctx->exops.get_st_gen = local_ioc_getversion; 142000c90bd1SGreg Kurz break; 142100c90bd1SGreg Kurz } 142200c90bd1SGreg Kurz #endif 14235b7b2f9aSKeno Fischer return 0; 14245b7b2f9aSKeno Fischer } 14255b7b2f9aSKeno Fischer 14265b7b2f9aSKeno Fischer static int local_init(FsContext *ctx, Error **errp) 14275b7b2f9aSKeno Fischer { 14285b7b2f9aSKeno Fischer LocalData *data = g_malloc(sizeof(*data)); 14295b7b2f9aSKeno Fischer 14305b7b2f9aSKeno Fischer data->mountfd = open(ctx->fs_root, O_DIRECTORY | O_RDONLY); 14315b7b2f9aSKeno Fischer if (data->mountfd == -1) { 14325b7b2f9aSKeno Fischer error_setg_errno(errp, errno, "failed to open '%s'", ctx->fs_root); 14335b7b2f9aSKeno Fischer goto err; 14345b7b2f9aSKeno Fischer } 14355b7b2f9aSKeno Fischer 14365b7b2f9aSKeno Fischer if (local_ioc_getversion_init(ctx, data, errp) < 0) { 14375b7b2f9aSKeno Fischer close(data->mountfd); 14385b7b2f9aSKeno Fischer goto err; 14395b7b2f9aSKeno Fischer } 1440e06a765eSHarsh Prateek Bora 14412c30dd74SAneesh Kumar K.V if (ctx->export_flags & V9FS_SM_PASSTHROUGH) { 14422c30dd74SAneesh Kumar K.V ctx->xops = passthrough_xattr_ops; 14432c30dd74SAneesh Kumar K.V } else if (ctx->export_flags & V9FS_SM_MAPPED) { 14442c30dd74SAneesh Kumar K.V ctx->xops = mapped_xattr_ops; 14452c30dd74SAneesh Kumar K.V } else if (ctx->export_flags & V9FS_SM_NONE) { 14462c30dd74SAneesh Kumar K.V ctx->xops = none_xattr_ops; 14472c30dd74SAneesh Kumar K.V } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 14482c30dd74SAneesh Kumar K.V /* 14492c30dd74SAneesh Kumar K.V * xattr operation for mapped-file and passthrough 14502c30dd74SAneesh Kumar K.V * remain same. 14512c30dd74SAneesh Kumar K.V */ 14522c30dd74SAneesh Kumar K.V ctx->xops = passthrough_xattr_ops; 14532c30dd74SAneesh Kumar K.V } 1454c98f1d4aSAneesh Kumar K.V ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT; 145500c90bd1SGreg Kurz 14560e35a378SGreg Kurz ctx->private = data; 145700c90bd1SGreg Kurz return 0; 14580e35a378SGreg Kurz 14590e35a378SGreg Kurz err: 14600e35a378SGreg Kurz g_free(data); 14610e35a378SGreg Kurz return -1; 1462e06a765eSHarsh Prateek Bora } 14630e35a378SGreg Kurz 14640e35a378SGreg Kurz static void local_cleanup(FsContext *ctx) 14650e35a378SGreg Kurz { 14660e35a378SGreg Kurz LocalData *data = ctx->private; 14670e35a378SGreg Kurz 14680e35a378SGreg Kurz close(data->mountfd); 14690e35a378SGreg Kurz g_free(data); 14700174fe73SAneesh Kumar K.V } 14710174fe73SAneesh Kumar K.V 147291cda4e8SGreg Kurz static void error_append_security_model_hint(Error **errp) 147391cda4e8SGreg Kurz { 147491cda4e8SGreg Kurz error_append_hint(errp, "Valid options are: security_model=" 147591cda4e8SGreg Kurz "[passthrough|mapped-xattr|mapped-file|none]\n"); 147691cda4e8SGreg Kurz } 147791cda4e8SGreg Kurz 147891cda4e8SGreg Kurz static int local_parse_opts(QemuOpts *opts, FsDriverEntry *fse, Error **errp) 147999519f0aSAneesh Kumar K.V { 148099519f0aSAneesh Kumar K.V const char *sec_model = qemu_opt_get(opts, "security_model"); 148199519f0aSAneesh Kumar K.V const char *path = qemu_opt_get(opts, "path"); 148291cda4e8SGreg Kurz Error *local_err = NULL; 148399519f0aSAneesh Kumar K.V 148499519f0aSAneesh Kumar K.V if (!sec_model) { 148591cda4e8SGreg Kurz error_setg(errp, "security_model property not set"); 148691cda4e8SGreg Kurz error_append_security_model_hint(errp); 148799519f0aSAneesh Kumar K.V return -1; 148899519f0aSAneesh Kumar K.V } 148999519f0aSAneesh Kumar K.V 149099519f0aSAneesh Kumar K.V if (!strcmp(sec_model, "passthrough")) { 149199519f0aSAneesh Kumar K.V fse->export_flags |= V9FS_SM_PASSTHROUGH; 14922c30dd74SAneesh Kumar K.V } else if (!strcmp(sec_model, "mapped") || 14932c30dd74SAneesh Kumar K.V !strcmp(sec_model, "mapped-xattr")) { 149499519f0aSAneesh Kumar K.V fse->export_flags |= V9FS_SM_MAPPED; 149599519f0aSAneesh Kumar K.V } else if (!strcmp(sec_model, "none")) { 149699519f0aSAneesh Kumar K.V fse->export_flags |= V9FS_SM_NONE; 14972c30dd74SAneesh Kumar K.V } else if (!strcmp(sec_model, "mapped-file")) { 14982c30dd74SAneesh Kumar K.V fse->export_flags |= V9FS_SM_MAPPED_FILE; 149999519f0aSAneesh Kumar K.V } else { 150091cda4e8SGreg Kurz error_setg(errp, "invalid security_model property '%s'", sec_model); 150191cda4e8SGreg Kurz error_append_security_model_hint(errp); 150299519f0aSAneesh Kumar K.V return -1; 150399519f0aSAneesh Kumar K.V } 150499519f0aSAneesh Kumar K.V 150599519f0aSAneesh Kumar K.V if (!path) { 150691cda4e8SGreg Kurz error_setg(errp, "path property not set"); 150799519f0aSAneesh Kumar K.V return -1; 150899519f0aSAneesh Kumar K.V } 1509b8bbdb88SPradeep Jagadeesh 151091cda4e8SGreg Kurz fsdev_throttle_parse_opts(opts, &fse->fst, &local_err); 151191cda4e8SGreg Kurz if (local_err) { 151291cda4e8SGreg Kurz error_propagate(errp, local_err); 151391cda4e8SGreg Kurz error_prepend(errp, "invalid throttle configuration: "); 1514b8bbdb88SPradeep Jagadeesh return -1; 1515b8bbdb88SPradeep Jagadeesh } 1516b8bbdb88SPradeep Jagadeesh 1517b96feb2cSTobias Schramm if (fse->export_flags & V9FS_SM_MAPPED || 1518b96feb2cSTobias Schramm fse->export_flags & V9FS_SM_MAPPED_FILE) { 1519b96feb2cSTobias Schramm fse->fmode = 1520b96feb2cSTobias Schramm qemu_opt_get_number(opts, "fmode", SM_LOCAL_MODE_BITS) & 0777; 1521b96feb2cSTobias Schramm fse->dmode = 1522b96feb2cSTobias Schramm qemu_opt_get_number(opts, "dmode", SM_LOCAL_DIR_MODE_BITS) & 0777; 1523b96feb2cSTobias Schramm } else { 1524b96feb2cSTobias Schramm if (qemu_opt_find(opts, "fmode")) { 152591cda4e8SGreg Kurz error_setg(errp, "fmode is only valid for mapped security modes"); 1526b96feb2cSTobias Schramm return -1; 1527b96feb2cSTobias Schramm } 1528b96feb2cSTobias Schramm if (qemu_opt_find(opts, "dmode")) { 152991cda4e8SGreg Kurz error_setg(errp, "dmode is only valid for mapped security modes"); 1530b96feb2cSTobias Schramm return -1; 1531b96feb2cSTobias Schramm } 1532b96feb2cSTobias Schramm } 1533b96feb2cSTobias Schramm 153499519f0aSAneesh Kumar K.V fse->path = g_strdup(path); 153599519f0aSAneesh Kumar K.V 153699519f0aSAneesh Kumar K.V return 0; 153799519f0aSAneesh Kumar K.V } 153899519f0aSAneesh Kumar K.V 15399f107513SAnthony Liguori FileOperations local_ops = { 154099519f0aSAneesh Kumar K.V .parse_opts = local_parse_opts, 15410174fe73SAneesh Kumar K.V .init = local_init, 15420e35a378SGreg Kurz .cleanup = local_cleanup, 1543131dcb25SAnthony Liguori .lstat = local_lstat, 1544131dcb25SAnthony Liguori .readlink = local_readlink, 1545131dcb25SAnthony Liguori .close = local_close, 1546131dcb25SAnthony Liguori .closedir = local_closedir, 1547a6568fe2SAnthony Liguori .open = local_open, 1548a6568fe2SAnthony Liguori .opendir = local_opendir, 1549a9231555SAnthony Liguori .rewinddir = local_rewinddir, 1550a9231555SAnthony Liguori .telldir = local_telldir, 1551635324e8SGreg Kurz .readdir = local_readdir, 1552a9231555SAnthony Liguori .seekdir = local_seekdir, 155356d15a53SSanchit Garg .preadv = local_preadv, 155456d15a53SSanchit Garg .pwritev = local_pwritev, 1555c494dd6fSAnthony Liguori .chmod = local_chmod, 1556c494dd6fSAnthony Liguori .mknod = local_mknod, 1557c494dd6fSAnthony Liguori .mkdir = local_mkdir, 1558c494dd6fSAnthony Liguori .fstat = local_fstat, 1559c494dd6fSAnthony Liguori .open2 = local_open2, 1560c494dd6fSAnthony Liguori .symlink = local_symlink, 1561c494dd6fSAnthony Liguori .link = local_link, 15628cf89e00SAnthony Liguori .truncate = local_truncate, 15638cf89e00SAnthony Liguori .rename = local_rename, 15648cf89e00SAnthony Liguori .chown = local_chown, 156574bc02b2SM. Mohan Kumar .utimensat = local_utimensat, 15665bae1900SAnthony Liguori .remove = local_remove, 15678cf89e00SAnthony Liguori .fsync = local_fsync, 1568be940c87SM. Mohan Kumar .statfs = local_statfs, 1569fa32ef88SAneesh Kumar K.V .lgetxattr = local_lgetxattr, 1570fa32ef88SAneesh Kumar K.V .llistxattr = local_llistxattr, 157110b468bdSAneesh Kumar K.V .lsetxattr = local_lsetxattr, 15729ed3ef26SAneesh Kumar K.V .lremovexattr = local_lremovexattr, 15732289be19SAneesh Kumar K.V .name_to_path = local_name_to_path, 15742289be19SAneesh Kumar K.V .renameat = local_renameat, 15752289be19SAneesh Kumar K.V .unlinkat = local_unlinkat, 15769f107513SAnthony Liguori }; 1577