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; 56996a0d76SGreg Kurz 57996a0d76SGreg Kurz /* All paths are relative to the path data->mountfd points to */ 58996a0d76SGreg Kurz while (*path == '/') { 59996a0d76SGreg Kurz path++; 60996a0d76SGreg Kurz } 61996a0d76SGreg Kurz 62996a0d76SGreg Kurz return relative_openat_nofollow(data->mountfd, path, flags, mode); 63996a0d76SGreg Kurz } 64996a0d76SGreg Kurz 65996a0d76SGreg Kurz int local_opendir_nofollow(FsContext *fs_ctx, const char *path) 66996a0d76SGreg Kurz { 67996a0d76SGreg Kurz return local_open_nofollow(fs_ctx, path, O_DIRECTORY | O_RDONLY, 0); 68996a0d76SGreg Kurz } 69996a0d76SGreg Kurz 7099f2cf4bSGreg Kurz static void renameat_preserve_errno(int odirfd, const char *opath, int ndirfd, 7199f2cf4bSGreg Kurz const char *npath) 7299f2cf4bSGreg Kurz { 7399f2cf4bSGreg Kurz int serrno = errno; 7499f2cf4bSGreg Kurz renameat(odirfd, opath, ndirfd, npath); 7599f2cf4bSGreg Kurz errno = serrno; 7699f2cf4bSGreg Kurz } 7799f2cf4bSGreg Kurz 78ad0b46e6SGreg Kurz static void unlinkat_preserve_errno(int dirfd, const char *path, int flags) 79ad0b46e6SGreg Kurz { 80ad0b46e6SGreg Kurz int serrno = errno; 81ad0b46e6SGreg Kurz unlinkat(dirfd, path, flags); 82ad0b46e6SGreg Kurz errno = serrno; 83ad0b46e6SGreg Kurz } 84ad0b46e6SGreg Kurz 852c30dd74SAneesh Kumar K.V #define VIRTFS_META_DIR ".virtfs_metadata" 862c30dd74SAneesh Kumar K.V 874fa4ce71SChen Gang static char *local_mapped_attr_path(FsContext *ctx, const char *path) 882c30dd74SAneesh Kumar K.V { 891b6f85e2SMichael Tokarev int dirlen; 901b6f85e2SMichael Tokarev const char *name = strrchr(path, '/'); 911b6f85e2SMichael Tokarev if (name) { 921b6f85e2SMichael Tokarev dirlen = name - path; 931b6f85e2SMichael Tokarev ++name; 941b6f85e2SMichael Tokarev } else { 951b6f85e2SMichael Tokarev name = path; 961b6f85e2SMichael Tokarev dirlen = 0; 971b6f85e2SMichael Tokarev } 981b6f85e2SMichael Tokarev return g_strdup_printf("%s/%.*s/%s/%s", ctx->fs_root, 991b6f85e2SMichael Tokarev dirlen, path, VIRTFS_META_DIR, name); 1002c30dd74SAneesh Kumar K.V } 1012c30dd74SAneesh Kumar K.V 1020ceb092eSAneesh Kumar K.V static FILE *local_fopen(const char *path, const char *mode) 1030ceb092eSAneesh Kumar K.V { 1040ceb092eSAneesh Kumar K.V int fd, o_mode = 0; 1050ceb092eSAneesh Kumar K.V FILE *fp; 1060ceb092eSAneesh Kumar K.V int flags = O_NOFOLLOW; 1070ceb092eSAneesh Kumar K.V /* 1080ceb092eSAneesh Kumar K.V * only supports two modes 1090ceb092eSAneesh Kumar K.V */ 1100ceb092eSAneesh Kumar K.V if (mode[0] == 'r') { 1110ceb092eSAneesh Kumar K.V flags |= O_RDONLY; 1120ceb092eSAneesh Kumar K.V } else if (mode[0] == 'w') { 1130ceb092eSAneesh Kumar K.V flags |= O_WRONLY | O_TRUNC | O_CREAT; 1140ceb092eSAneesh Kumar K.V o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; 1150ceb092eSAneesh Kumar K.V } else { 1160ceb092eSAneesh Kumar K.V return NULL; 1170ceb092eSAneesh Kumar K.V } 1180ceb092eSAneesh Kumar K.V fd = open(path, flags, o_mode); 1190ceb092eSAneesh Kumar K.V if (fd == -1) { 1200ceb092eSAneesh Kumar K.V return NULL; 1210ceb092eSAneesh Kumar K.V } 1220ceb092eSAneesh Kumar K.V fp = fdopen(fd, mode); 1230ceb092eSAneesh Kumar K.V if (!fp) { 1240ceb092eSAneesh Kumar K.V close(fd); 1250ceb092eSAneesh Kumar K.V } 1260ceb092eSAneesh Kumar K.V return fp; 1270ceb092eSAneesh Kumar K.V } 1280ceb092eSAneesh Kumar K.V 129f9aef99bSGreg Kurz static FILE *local_fopenat(int dirfd, const char *name, const char *mode) 130f9aef99bSGreg Kurz { 131f9aef99bSGreg Kurz int fd, o_mode = 0; 132f9aef99bSGreg Kurz FILE *fp; 133f9aef99bSGreg Kurz int flags; 134f9aef99bSGreg Kurz /* 135f9aef99bSGreg Kurz * only supports two modes 136f9aef99bSGreg Kurz */ 137f9aef99bSGreg Kurz if (mode[0] == 'r') { 138f9aef99bSGreg Kurz flags = O_RDONLY; 139f9aef99bSGreg Kurz } else if (mode[0] == 'w') { 140f9aef99bSGreg Kurz flags = O_WRONLY | O_TRUNC | O_CREAT; 141f9aef99bSGreg Kurz o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; 142f9aef99bSGreg Kurz } else { 143f9aef99bSGreg Kurz return NULL; 144f9aef99bSGreg Kurz } 145f9aef99bSGreg Kurz fd = openat_file(dirfd, name, flags, o_mode); 146f9aef99bSGreg Kurz if (fd == -1) { 147f9aef99bSGreg Kurz return NULL; 148f9aef99bSGreg Kurz } 149f9aef99bSGreg Kurz fp = fdopen(fd, mode); 150f9aef99bSGreg Kurz if (!fp) { 151f9aef99bSGreg Kurz close(fd); 152f9aef99bSGreg Kurz } 153f9aef99bSGreg Kurz return fp; 154f9aef99bSGreg Kurz } 155f9aef99bSGreg Kurz 1562c30dd74SAneesh Kumar K.V #define ATTR_MAX 100 157f9aef99bSGreg Kurz static void local_mapped_file_attr(int dirfd, const char *name, 1582c30dd74SAneesh Kumar K.V struct stat *stbuf) 1592c30dd74SAneesh Kumar K.V { 1602c30dd74SAneesh Kumar K.V FILE *fp; 1612c30dd74SAneesh Kumar K.V char buf[ATTR_MAX]; 162f9aef99bSGreg Kurz int map_dirfd; 1632c30dd74SAneesh Kumar K.V 16499f2cf4bSGreg Kurz map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR); 165f9aef99bSGreg Kurz if (map_dirfd == -1) { 166f9aef99bSGreg Kurz return; 167f9aef99bSGreg Kurz } 168f9aef99bSGreg Kurz 169f9aef99bSGreg Kurz fp = local_fopenat(map_dirfd, name, "r"); 170f9aef99bSGreg Kurz close_preserve_errno(map_dirfd); 1712c30dd74SAneesh Kumar K.V if (!fp) { 1722c30dd74SAneesh Kumar K.V return; 1732c30dd74SAneesh Kumar K.V } 1742c30dd74SAneesh Kumar K.V memset(buf, 0, ATTR_MAX); 1752c30dd74SAneesh Kumar K.V while (fgets(buf, ATTR_MAX, fp)) { 1762c30dd74SAneesh Kumar K.V if (!strncmp(buf, "virtfs.uid", 10)) { 1772c30dd74SAneesh Kumar K.V stbuf->st_uid = atoi(buf+11); 1782c30dd74SAneesh Kumar K.V } else if (!strncmp(buf, "virtfs.gid", 10)) { 1792c30dd74SAneesh Kumar K.V stbuf->st_gid = atoi(buf+11); 1802c30dd74SAneesh Kumar K.V } else if (!strncmp(buf, "virtfs.mode", 11)) { 1812c30dd74SAneesh Kumar K.V stbuf->st_mode = atoi(buf+12); 1822c30dd74SAneesh Kumar K.V } else if (!strncmp(buf, "virtfs.rdev", 11)) { 1832c30dd74SAneesh Kumar K.V stbuf->st_rdev = atoi(buf+12); 1842c30dd74SAneesh Kumar K.V } 1852c30dd74SAneesh Kumar K.V memset(buf, 0, ATTR_MAX); 1862c30dd74SAneesh Kumar K.V } 1872c30dd74SAneesh Kumar K.V fclose(fp); 1882c30dd74SAneesh Kumar K.V } 1892c30dd74SAneesh Kumar K.V 1902289be19SAneesh Kumar K.V static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf) 191131dcb25SAnthony Liguori { 192f9aef99bSGreg Kurz int err = -1; 193f9aef99bSGreg Kurz char *dirpath = g_path_get_dirname(fs_path->data); 194f9aef99bSGreg Kurz char *name = g_path_get_basename(fs_path->data); 195f9aef99bSGreg Kurz int dirfd; 1962289be19SAneesh Kumar K.V 197f9aef99bSGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dirpath); 198f9aef99bSGreg Kurz if (dirfd == -1) { 199f9aef99bSGreg Kurz goto out; 200f9aef99bSGreg Kurz } 201f9aef99bSGreg Kurz 202f9aef99bSGreg Kurz err = fstatat(dirfd, name, stbuf, AT_SYMLINK_NOFOLLOW); 2031237ad76SVenkateswararao Jujjuri (JV) if (err) { 2044fa4ce71SChen Gang goto err_out; 2051237ad76SVenkateswararao Jujjuri (JV) } 206b97400caSAneesh Kumar K.V if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 2071237ad76SVenkateswararao Jujjuri (JV) /* Actual credentials are part of extended attrs */ 2081237ad76SVenkateswararao Jujjuri (JV) uid_t tmp_uid; 2091237ad76SVenkateswararao Jujjuri (JV) gid_t tmp_gid; 2101237ad76SVenkateswararao Jujjuri (JV) mode_t tmp_mode; 2111237ad76SVenkateswararao Jujjuri (JV) dev_t tmp_dev; 212f9aef99bSGreg Kurz 213f9aef99bSGreg Kurz if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.uid", &tmp_uid, 214f9aef99bSGreg Kurz sizeof(uid_t)) > 0) { 215f8ad4a89SAneesh Kumar K.V stbuf->st_uid = le32_to_cpu(tmp_uid); 2161237ad76SVenkateswararao Jujjuri (JV) } 217f9aef99bSGreg Kurz if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.gid", &tmp_gid, 218f9aef99bSGreg Kurz sizeof(gid_t)) > 0) { 219f8ad4a89SAneesh Kumar K.V stbuf->st_gid = le32_to_cpu(tmp_gid); 2201237ad76SVenkateswararao Jujjuri (JV) } 221f9aef99bSGreg Kurz if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.mode", &tmp_mode, 222f9aef99bSGreg Kurz sizeof(mode_t)) > 0) { 223f8ad4a89SAneesh Kumar K.V stbuf->st_mode = le32_to_cpu(tmp_mode); 2241237ad76SVenkateswararao Jujjuri (JV) } 225f9aef99bSGreg Kurz if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.rdev", &tmp_dev, 226f9aef99bSGreg Kurz sizeof(dev_t)) > 0) { 227f8ad4a89SAneesh Kumar K.V stbuf->st_rdev = le64_to_cpu(tmp_dev); 2281237ad76SVenkateswararao Jujjuri (JV) } 2292c30dd74SAneesh Kumar K.V } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 230f9aef99bSGreg Kurz local_mapped_file_attr(dirfd, name, stbuf); 2311237ad76SVenkateswararao Jujjuri (JV) } 2324fa4ce71SChen Gang 2334fa4ce71SChen Gang err_out: 234f9aef99bSGreg Kurz close_preserve_errno(dirfd); 235f9aef99bSGreg Kurz out: 236f9aef99bSGreg Kurz g_free(name); 237f9aef99bSGreg Kurz g_free(dirpath); 2381237ad76SVenkateswararao Jujjuri (JV) return err; 239131dcb25SAnthony Liguori } 240131dcb25SAnthony Liguori 2412c30dd74SAneesh Kumar K.V static int local_create_mapped_attr_dir(FsContext *ctx, const char *path) 2422c30dd74SAneesh Kumar K.V { 2432c30dd74SAneesh Kumar K.V int err; 2444fa4ce71SChen Gang char *attr_dir; 245d3f8e138SMarkus Armbruster char *tmp_path = g_strdup(path); 2462c30dd74SAneesh Kumar K.V 2474fa4ce71SChen Gang attr_dir = g_strdup_printf("%s/%s/%s", 2482c30dd74SAneesh Kumar K.V ctx->fs_root, dirname(tmp_path), VIRTFS_META_DIR); 2492c30dd74SAneesh Kumar K.V 2502c30dd74SAneesh Kumar K.V err = mkdir(attr_dir, 0700); 2512c30dd74SAneesh Kumar K.V if (err < 0 && errno == EEXIST) { 2522c30dd74SAneesh Kumar K.V err = 0; 2532c30dd74SAneesh Kumar K.V } 2544fa4ce71SChen Gang g_free(attr_dir); 255d3f8e138SMarkus Armbruster g_free(tmp_path); 2562c30dd74SAneesh Kumar K.V return err; 2572c30dd74SAneesh Kumar K.V } 2582c30dd74SAneesh Kumar K.V 2592c30dd74SAneesh Kumar K.V static int local_set_mapped_file_attr(FsContext *ctx, 2602c30dd74SAneesh Kumar K.V const char *path, FsCred *credp) 2612c30dd74SAneesh Kumar K.V { 2622c30dd74SAneesh Kumar K.V FILE *fp; 2632c30dd74SAneesh Kumar K.V int ret = 0; 2642c30dd74SAneesh Kumar K.V char buf[ATTR_MAX]; 2654fa4ce71SChen Gang char *attr_path; 2662c30dd74SAneesh Kumar K.V int uid = -1, gid = -1, mode = -1, rdev = -1; 2672c30dd74SAneesh Kumar K.V 2684fa4ce71SChen Gang attr_path = local_mapped_attr_path(ctx, path); 2694fa4ce71SChen Gang fp = local_fopen(attr_path, "r"); 2702c30dd74SAneesh Kumar K.V if (!fp) { 2712c30dd74SAneesh Kumar K.V goto create_map_file; 2722c30dd74SAneesh Kumar K.V } 2732c30dd74SAneesh Kumar K.V memset(buf, 0, ATTR_MAX); 2742c30dd74SAneesh Kumar K.V while (fgets(buf, ATTR_MAX, fp)) { 2752c30dd74SAneesh Kumar K.V if (!strncmp(buf, "virtfs.uid", 10)) { 2762c30dd74SAneesh Kumar K.V uid = atoi(buf+11); 2772c30dd74SAneesh Kumar K.V } else if (!strncmp(buf, "virtfs.gid", 10)) { 2782c30dd74SAneesh Kumar K.V gid = atoi(buf+11); 2792c30dd74SAneesh Kumar K.V } else if (!strncmp(buf, "virtfs.mode", 11)) { 2802c30dd74SAneesh Kumar K.V mode = atoi(buf+12); 2812c30dd74SAneesh Kumar K.V } else if (!strncmp(buf, "virtfs.rdev", 11)) { 2822c30dd74SAneesh Kumar K.V rdev = atoi(buf+12); 2832c30dd74SAneesh Kumar K.V } 2842c30dd74SAneesh Kumar K.V memset(buf, 0, ATTR_MAX); 2852c30dd74SAneesh Kumar K.V } 2862c30dd74SAneesh Kumar K.V fclose(fp); 2872c30dd74SAneesh Kumar K.V goto update_map_file; 2882c30dd74SAneesh Kumar K.V 2892c30dd74SAneesh Kumar K.V create_map_file: 2902c30dd74SAneesh Kumar K.V ret = local_create_mapped_attr_dir(ctx, path); 2912c30dd74SAneesh Kumar K.V if (ret < 0) { 2922c30dd74SAneesh Kumar K.V goto err_out; 2932c30dd74SAneesh Kumar K.V } 2942c30dd74SAneesh Kumar K.V 2952c30dd74SAneesh Kumar K.V update_map_file: 2960ceb092eSAneesh Kumar K.V fp = local_fopen(attr_path, "w"); 2972c30dd74SAneesh Kumar K.V if (!fp) { 2982c30dd74SAneesh Kumar K.V ret = -1; 2992c30dd74SAneesh Kumar K.V goto err_out; 3002c30dd74SAneesh Kumar K.V } 3012c30dd74SAneesh Kumar K.V 3022c30dd74SAneesh Kumar K.V if (credp->fc_uid != -1) { 3032c30dd74SAneesh Kumar K.V uid = credp->fc_uid; 3042c30dd74SAneesh Kumar K.V } 3052c30dd74SAneesh Kumar K.V if (credp->fc_gid != -1) { 3062c30dd74SAneesh Kumar K.V gid = credp->fc_gid; 3072c30dd74SAneesh Kumar K.V } 3082c30dd74SAneesh Kumar K.V if (credp->fc_mode != -1) { 3092c30dd74SAneesh Kumar K.V mode = credp->fc_mode; 3102c30dd74SAneesh Kumar K.V } 3112c30dd74SAneesh Kumar K.V if (credp->fc_rdev != -1) { 3122c30dd74SAneesh Kumar K.V rdev = credp->fc_rdev; 3132c30dd74SAneesh Kumar K.V } 3142c30dd74SAneesh Kumar K.V 3152c30dd74SAneesh Kumar K.V 3162c30dd74SAneesh Kumar K.V if (uid != -1) { 3172c30dd74SAneesh Kumar K.V fprintf(fp, "virtfs.uid=%d\n", uid); 3182c30dd74SAneesh Kumar K.V } 3192c30dd74SAneesh Kumar K.V if (gid != -1) { 3202c30dd74SAneesh Kumar K.V fprintf(fp, "virtfs.gid=%d\n", gid); 3212c30dd74SAneesh Kumar K.V } 3222c30dd74SAneesh Kumar K.V if (mode != -1) { 3232c30dd74SAneesh Kumar K.V fprintf(fp, "virtfs.mode=%d\n", mode); 3242c30dd74SAneesh Kumar K.V } 3252c30dd74SAneesh Kumar K.V if (rdev != -1) { 3262c30dd74SAneesh Kumar K.V fprintf(fp, "virtfs.rdev=%d\n", rdev); 3272c30dd74SAneesh Kumar K.V } 3282c30dd74SAneesh Kumar K.V fclose(fp); 3292c30dd74SAneesh Kumar K.V 3302c30dd74SAneesh Kumar K.V err_out: 3314fa4ce71SChen Gang g_free(attr_path); 3322c30dd74SAneesh Kumar K.V return ret; 3332c30dd74SAneesh Kumar K.V } 3342c30dd74SAneesh Kumar K.V 335758e8e38SVenkateswararao Jujjuri (JV) static int local_set_xattr(const char *path, FsCred *credp) 336131dcb25SAnthony Liguori { 337758e8e38SVenkateswararao Jujjuri (JV) int err; 3382289be19SAneesh Kumar K.V 339758e8e38SVenkateswararao Jujjuri (JV) if (credp->fc_uid != -1) { 340f8ad4a89SAneesh Kumar K.V uint32_t tmp_uid = cpu_to_le32(credp->fc_uid); 341f8ad4a89SAneesh Kumar K.V err = setxattr(path, "user.virtfs.uid", &tmp_uid, sizeof(uid_t), 0); 342758e8e38SVenkateswararao Jujjuri (JV) if (err) { 343758e8e38SVenkateswararao Jujjuri (JV) return err; 344131dcb25SAnthony Liguori } 345131dcb25SAnthony Liguori } 346758e8e38SVenkateswararao Jujjuri (JV) if (credp->fc_gid != -1) { 347f8ad4a89SAneesh Kumar K.V uint32_t tmp_gid = cpu_to_le32(credp->fc_gid); 348f8ad4a89SAneesh Kumar K.V err = setxattr(path, "user.virtfs.gid", &tmp_gid, sizeof(gid_t), 0); 349758e8e38SVenkateswararao Jujjuri (JV) if (err) { 350758e8e38SVenkateswararao Jujjuri (JV) return err; 351131dcb25SAnthony Liguori } 352131dcb25SAnthony Liguori } 353758e8e38SVenkateswararao Jujjuri (JV) if (credp->fc_mode != -1) { 354f8ad4a89SAneesh Kumar K.V uint32_t tmp_mode = cpu_to_le32(credp->fc_mode); 355f8ad4a89SAneesh Kumar K.V err = setxattr(path, "user.virtfs.mode", &tmp_mode, sizeof(mode_t), 0); 356758e8e38SVenkateswararao Jujjuri (JV) if (err) { 357758e8e38SVenkateswararao Jujjuri (JV) return err; 358131dcb25SAnthony Liguori } 359131dcb25SAnthony Liguori } 360758e8e38SVenkateswararao Jujjuri (JV) if (credp->fc_rdev != -1) { 361f8ad4a89SAneesh Kumar K.V uint64_t tmp_rdev = cpu_to_le64(credp->fc_rdev); 362f8ad4a89SAneesh Kumar K.V err = setxattr(path, "user.virtfs.rdev", &tmp_rdev, sizeof(dev_t), 0); 363758e8e38SVenkateswararao Jujjuri (JV) if (err) { 364758e8e38SVenkateswararao Jujjuri (JV) return err; 365131dcb25SAnthony Liguori } 366758e8e38SVenkateswararao Jujjuri (JV) } 367131dcb25SAnthony Liguori return 0; 368131dcb25SAnthony Liguori } 369131dcb25SAnthony Liguori 370e3187a45SGreg Kurz static int local_set_mapped_file_attrat(int dirfd, const char *name, 371e3187a45SGreg Kurz FsCred *credp) 372e3187a45SGreg Kurz { 373e3187a45SGreg Kurz FILE *fp; 374e3187a45SGreg Kurz int ret; 375e3187a45SGreg Kurz char buf[ATTR_MAX]; 376e3187a45SGreg Kurz int uid = -1, gid = -1, mode = -1, rdev = -1; 377e3187a45SGreg Kurz int map_dirfd; 378e3187a45SGreg Kurz 379e3187a45SGreg Kurz ret = mkdirat(dirfd, VIRTFS_META_DIR, 0700); 380e3187a45SGreg Kurz if (ret < 0 && errno != EEXIST) { 381e3187a45SGreg Kurz return -1; 382e3187a45SGreg Kurz } 383e3187a45SGreg Kurz 384e3187a45SGreg Kurz map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR); 385e3187a45SGreg Kurz if (map_dirfd == -1) { 386e3187a45SGreg Kurz return -1; 387e3187a45SGreg Kurz } 388e3187a45SGreg Kurz 389e3187a45SGreg Kurz fp = local_fopenat(map_dirfd, name, "r"); 390e3187a45SGreg Kurz if (!fp) { 391e3187a45SGreg Kurz if (errno == ENOENT) { 392e3187a45SGreg Kurz goto update_map_file; 393e3187a45SGreg Kurz } else { 394e3187a45SGreg Kurz close_preserve_errno(map_dirfd); 395e3187a45SGreg Kurz return -1; 396e3187a45SGreg Kurz } 397e3187a45SGreg Kurz } 398e3187a45SGreg Kurz memset(buf, 0, ATTR_MAX); 399e3187a45SGreg Kurz while (fgets(buf, ATTR_MAX, fp)) { 400e3187a45SGreg Kurz if (!strncmp(buf, "virtfs.uid", 10)) { 401e3187a45SGreg Kurz uid = atoi(buf + 11); 402e3187a45SGreg Kurz } else if (!strncmp(buf, "virtfs.gid", 10)) { 403e3187a45SGreg Kurz gid = atoi(buf + 11); 404e3187a45SGreg Kurz } else if (!strncmp(buf, "virtfs.mode", 11)) { 405e3187a45SGreg Kurz mode = atoi(buf + 12); 406e3187a45SGreg Kurz } else if (!strncmp(buf, "virtfs.rdev", 11)) { 407e3187a45SGreg Kurz rdev = atoi(buf + 12); 408e3187a45SGreg Kurz } 409e3187a45SGreg Kurz memset(buf, 0, ATTR_MAX); 410e3187a45SGreg Kurz } 411e3187a45SGreg Kurz fclose(fp); 412e3187a45SGreg Kurz 413e3187a45SGreg Kurz update_map_file: 414e3187a45SGreg Kurz fp = local_fopenat(map_dirfd, name, "w"); 415e3187a45SGreg Kurz close_preserve_errno(map_dirfd); 416e3187a45SGreg Kurz if (!fp) { 417e3187a45SGreg Kurz return -1; 418e3187a45SGreg Kurz } 419e3187a45SGreg Kurz 420e3187a45SGreg Kurz if (credp->fc_uid != -1) { 421e3187a45SGreg Kurz uid = credp->fc_uid; 422e3187a45SGreg Kurz } 423e3187a45SGreg Kurz if (credp->fc_gid != -1) { 424e3187a45SGreg Kurz gid = credp->fc_gid; 425e3187a45SGreg Kurz } 426e3187a45SGreg Kurz if (credp->fc_mode != -1) { 427e3187a45SGreg Kurz mode = credp->fc_mode; 428e3187a45SGreg Kurz } 429e3187a45SGreg Kurz if (credp->fc_rdev != -1) { 430e3187a45SGreg Kurz rdev = credp->fc_rdev; 431e3187a45SGreg Kurz } 432e3187a45SGreg Kurz 433e3187a45SGreg Kurz if (uid != -1) { 434e3187a45SGreg Kurz fprintf(fp, "virtfs.uid=%d\n", uid); 435e3187a45SGreg Kurz } 436e3187a45SGreg Kurz if (gid != -1) { 437e3187a45SGreg Kurz fprintf(fp, "virtfs.gid=%d\n", gid); 438e3187a45SGreg Kurz } 439e3187a45SGreg Kurz if (mode != -1) { 440e3187a45SGreg Kurz fprintf(fp, "virtfs.mode=%d\n", mode); 441e3187a45SGreg Kurz } 442e3187a45SGreg Kurz if (rdev != -1) { 443e3187a45SGreg Kurz fprintf(fp, "virtfs.rdev=%d\n", rdev); 444e3187a45SGreg Kurz } 445e3187a45SGreg Kurz fclose(fp); 446e3187a45SGreg Kurz 447e3187a45SGreg Kurz return 0; 448e3187a45SGreg Kurz } 449e3187a45SGreg Kurz 450e3187a45SGreg Kurz static int fchmodat_nofollow(int dirfd, const char *name, mode_t mode) 451e3187a45SGreg Kurz { 452e3187a45SGreg Kurz int fd, ret; 453e3187a45SGreg Kurz 454e3187a45SGreg Kurz /* FIXME: this should be handled with fchmodat(AT_SYMLINK_NOFOLLOW). 455e3187a45SGreg Kurz * Unfortunately, the linux kernel doesn't implement it yet. As an 456e3187a45SGreg Kurz * alternative, let's open the file and use fchmod() instead. This 457e3187a45SGreg Kurz * may fail depending on the permissions of the file, but it is the 458e3187a45SGreg Kurz * best we can do to avoid TOCTTOU. We first try to open read-only 459e3187a45SGreg Kurz * in case name points to a directory. If that fails, we try write-only 460e3187a45SGreg Kurz * in case name doesn't point to a directory. 461e3187a45SGreg Kurz */ 462e3187a45SGreg Kurz fd = openat_file(dirfd, name, O_RDONLY, 0); 463e3187a45SGreg Kurz if (fd == -1) { 464e3187a45SGreg Kurz /* In case the file is writable-only and isn't a directory. */ 465e3187a45SGreg Kurz if (errno == EACCES) { 466e3187a45SGreg Kurz fd = openat_file(dirfd, name, O_WRONLY, 0); 467e3187a45SGreg Kurz } 468e3187a45SGreg Kurz if (fd == -1 && errno == EISDIR) { 469e3187a45SGreg Kurz errno = EACCES; 470e3187a45SGreg Kurz } 471e3187a45SGreg Kurz } 472e3187a45SGreg Kurz if (fd == -1) { 473e3187a45SGreg Kurz return -1; 474e3187a45SGreg Kurz } 475e3187a45SGreg Kurz ret = fchmod(fd, mode); 476e3187a45SGreg Kurz close_preserve_errno(fd); 477e3187a45SGreg Kurz return ret; 478e3187a45SGreg Kurz } 479e3187a45SGreg Kurz 480e3187a45SGreg Kurz static int local_set_xattrat(int dirfd, const char *path, FsCred *credp) 481e3187a45SGreg Kurz { 482e3187a45SGreg Kurz int err; 483e3187a45SGreg Kurz 484e3187a45SGreg Kurz if (credp->fc_uid != -1) { 485e3187a45SGreg Kurz uint32_t tmp_uid = cpu_to_le32(credp->fc_uid); 486e3187a45SGreg Kurz err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.uid", &tmp_uid, 487e3187a45SGreg Kurz sizeof(uid_t), 0); 488e3187a45SGreg Kurz if (err) { 489e3187a45SGreg Kurz return err; 490e3187a45SGreg Kurz } 491e3187a45SGreg Kurz } 492e3187a45SGreg Kurz if (credp->fc_gid != -1) { 493e3187a45SGreg Kurz uint32_t tmp_gid = cpu_to_le32(credp->fc_gid); 494e3187a45SGreg Kurz err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.gid", &tmp_gid, 495e3187a45SGreg Kurz sizeof(gid_t), 0); 496e3187a45SGreg Kurz if (err) { 497e3187a45SGreg Kurz return err; 498e3187a45SGreg Kurz } 499e3187a45SGreg Kurz } 500e3187a45SGreg Kurz if (credp->fc_mode != -1) { 501e3187a45SGreg Kurz uint32_t tmp_mode = cpu_to_le32(credp->fc_mode); 502e3187a45SGreg Kurz err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.mode", &tmp_mode, 503e3187a45SGreg Kurz sizeof(mode_t), 0); 504e3187a45SGreg Kurz if (err) { 505e3187a45SGreg Kurz return err; 506e3187a45SGreg Kurz } 507e3187a45SGreg Kurz } 508e3187a45SGreg Kurz if (credp->fc_rdev != -1) { 509e3187a45SGreg Kurz uint64_t tmp_rdev = cpu_to_le64(credp->fc_rdev); 510e3187a45SGreg Kurz err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.rdev", &tmp_rdev, 511e3187a45SGreg Kurz sizeof(dev_t), 0); 512e3187a45SGreg Kurz if (err) { 513e3187a45SGreg Kurz return err; 514e3187a45SGreg Kurz } 515e3187a45SGreg Kurz } 516e3187a45SGreg Kurz return 0; 517e3187a45SGreg Kurz } 518e3187a45SGreg Kurz 5194750a96fSVenkateswararao Jujjuri (JV) static int local_post_create_passthrough(FsContext *fs_ctx, const char *path, 5204750a96fSVenkateswararao Jujjuri (JV) FsCred *credp) 5214750a96fSVenkateswararao Jujjuri (JV) { 5224fa4ce71SChen Gang char *buffer; 5232289be19SAneesh Kumar K.V 5244fa4ce71SChen Gang buffer = rpath(fs_ctx, path); 5254fa4ce71SChen Gang if (lchown(buffer, credp->fc_uid, credp->fc_gid) < 0) { 52612848bfcSAneesh Kumar K.V /* 52712848bfcSAneesh Kumar K.V * If we fail to change ownership and if we are 52812848bfcSAneesh Kumar K.V * using security model none. Ignore the error 52912848bfcSAneesh Kumar K.V */ 530b97400caSAneesh Kumar K.V if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) { 5314fa4ce71SChen Gang goto err; 5324750a96fSVenkateswararao Jujjuri (JV) } 53312848bfcSAneesh Kumar K.V } 5342d40564aSM. Mohan Kumar 5354fa4ce71SChen Gang if (chmod(buffer, credp->fc_mode & 07777) < 0) { 5364fa4ce71SChen Gang goto err; 5372d40564aSM. Mohan Kumar } 5384fa4ce71SChen Gang 5394fa4ce71SChen Gang g_free(buffer); 5404750a96fSVenkateswararao Jujjuri (JV) return 0; 5414fa4ce71SChen Gang err: 5424fa4ce71SChen Gang g_free(buffer); 5434fa4ce71SChen Gang return -1; 5444750a96fSVenkateswararao Jujjuri (JV) } 5454750a96fSVenkateswararao Jujjuri (JV) 546d815e721SGreg Kurz static int local_set_cred_passthrough(FsContext *fs_ctx, int dirfd, 547d815e721SGreg Kurz const char *name, FsCred *credp) 548d815e721SGreg Kurz { 549d815e721SGreg Kurz if (fchownat(dirfd, name, credp->fc_uid, credp->fc_gid, 550d815e721SGreg Kurz AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH) < 0) { 551d815e721SGreg Kurz /* 552d815e721SGreg Kurz * If we fail to change ownership and if we are 553d815e721SGreg Kurz * using security model none. Ignore the error 554d815e721SGreg Kurz */ 555d815e721SGreg Kurz if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) { 556d815e721SGreg Kurz return -1; 557d815e721SGreg Kurz } 558d815e721SGreg Kurz } 559d815e721SGreg Kurz 560d815e721SGreg Kurz return fchmodat_nofollow(dirfd, name, credp->fc_mode & 07777); 561d815e721SGreg Kurz } 562d815e721SGreg Kurz 5632289be19SAneesh Kumar K.V static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path, 564131dcb25SAnthony Liguori char *buf, size_t bufsz) 565131dcb25SAnthony Liguori { 566879c2813SVenkateswararao Jujjuri (JV) ssize_t tsize = -1; 5672289be19SAneesh Kumar K.V 5682c30dd74SAneesh Kumar K.V if ((fs_ctx->export_flags & V9FS_SM_MAPPED) || 5692c30dd74SAneesh Kumar K.V (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) { 570879c2813SVenkateswararao Jujjuri (JV) int fd; 571bec1e954SGreg Kurz 572bec1e954SGreg Kurz fd = local_open_nofollow(fs_ctx, fs_path->data, O_RDONLY, 0); 573879c2813SVenkateswararao Jujjuri (JV) if (fd == -1) { 574879c2813SVenkateswararao Jujjuri (JV) return -1; 575879c2813SVenkateswararao Jujjuri (JV) } 576879c2813SVenkateswararao Jujjuri (JV) do { 577879c2813SVenkateswararao Jujjuri (JV) tsize = read(fd, (void *)buf, bufsz); 578879c2813SVenkateswararao Jujjuri (JV) } while (tsize == -1 && errno == EINTR); 579bec1e954SGreg Kurz close_preserve_errno(fd); 580b97400caSAneesh Kumar K.V } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 581b97400caSAneesh Kumar K.V (fs_ctx->export_flags & V9FS_SM_NONE)) { 582bec1e954SGreg Kurz char *dirpath = g_path_get_dirname(fs_path->data); 583bec1e954SGreg Kurz char *name = g_path_get_basename(fs_path->data); 584bec1e954SGreg Kurz int dirfd; 585bec1e954SGreg Kurz 586bec1e954SGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dirpath); 587bec1e954SGreg Kurz if (dirfd == -1) { 588bec1e954SGreg Kurz goto out; 589bec1e954SGreg Kurz } 590bec1e954SGreg Kurz 591bec1e954SGreg Kurz tsize = readlinkat(dirfd, name, buf, bufsz); 592bec1e954SGreg Kurz close_preserve_errno(dirfd); 593bec1e954SGreg Kurz out: 594bec1e954SGreg Kurz g_free(name); 595bec1e954SGreg Kurz g_free(dirpath); 596879c2813SVenkateswararao Jujjuri (JV) } 597879c2813SVenkateswararao Jujjuri (JV) return tsize; 598131dcb25SAnthony Liguori } 599131dcb25SAnthony Liguori 600cc720ddbSAneesh Kumar K.V static int local_close(FsContext *ctx, V9fsFidOpenState *fs) 601131dcb25SAnthony Liguori { 602cc720ddbSAneesh Kumar K.V return close(fs->fd); 603131dcb25SAnthony Liguori } 604131dcb25SAnthony Liguori 605cc720ddbSAneesh Kumar K.V static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs) 606131dcb25SAnthony Liguori { 607f314ea4eSGreg Kurz return closedir(fs->dir.stream); 608131dcb25SAnthony Liguori } 6099f107513SAnthony Liguori 610cc720ddbSAneesh Kumar K.V static int local_open(FsContext *ctx, V9fsPath *fs_path, 611cc720ddbSAneesh Kumar K.V int flags, V9fsFidOpenState *fs) 612a6568fe2SAnthony Liguori { 61321328e1eSGreg Kurz int fd; 6142289be19SAneesh Kumar K.V 615996a0d76SGreg Kurz fd = local_open_nofollow(ctx, fs_path->data, flags, 0); 61621328e1eSGreg Kurz if (fd == -1) { 61721328e1eSGreg Kurz return -1; 61821328e1eSGreg Kurz } 61921328e1eSGreg Kurz fs->fd = fd; 620cc720ddbSAneesh Kumar K.V return fs->fd; 621a6568fe2SAnthony Liguori } 622a6568fe2SAnthony Liguori 623cc720ddbSAneesh Kumar K.V static int local_opendir(FsContext *ctx, 624cc720ddbSAneesh Kumar K.V V9fsPath *fs_path, V9fsFidOpenState *fs) 625a6568fe2SAnthony Liguori { 626996a0d76SGreg Kurz int dirfd; 62721328e1eSGreg Kurz DIR *stream; 6282289be19SAneesh Kumar K.V 629996a0d76SGreg Kurz dirfd = local_opendir_nofollow(ctx, fs_path->data); 630996a0d76SGreg Kurz if (dirfd == -1) { 631996a0d76SGreg Kurz return -1; 632996a0d76SGreg Kurz } 633996a0d76SGreg Kurz 634996a0d76SGreg Kurz stream = fdopendir(dirfd); 63521328e1eSGreg Kurz if (!stream) { 636cc720ddbSAneesh Kumar K.V return -1; 637cc720ddbSAneesh Kumar K.V } 63821328e1eSGreg Kurz fs->dir.stream = stream; 639cc720ddbSAneesh Kumar K.V return 0; 640a6568fe2SAnthony Liguori } 641a6568fe2SAnthony Liguori 642cc720ddbSAneesh Kumar K.V static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs) 643a9231555SAnthony Liguori { 644f314ea4eSGreg Kurz rewinddir(fs->dir.stream); 645a9231555SAnthony Liguori } 646a9231555SAnthony Liguori 647cc720ddbSAneesh Kumar K.V static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs) 648a9231555SAnthony Liguori { 649f314ea4eSGreg Kurz return telldir(fs->dir.stream); 650a9231555SAnthony Liguori } 651a9231555SAnthony Liguori 652635324e8SGreg Kurz static struct dirent *local_readdir(FsContext *ctx, V9fsFidOpenState *fs) 653a9231555SAnthony Liguori { 654635324e8SGreg Kurz struct dirent *entry; 6552c30dd74SAneesh Kumar K.V 6562c30dd74SAneesh Kumar K.V again: 657635324e8SGreg Kurz entry = readdir(fs->dir.stream); 658635324e8SGreg Kurz if (!entry) { 659635324e8SGreg Kurz return NULL; 660635324e8SGreg Kurz } 661635324e8SGreg Kurz 662840a1bf2SBastian Blank if (ctx->export_flags & V9FS_SM_MAPPED) { 663840a1bf2SBastian Blank entry->d_type = DT_UNKNOWN; 664840a1bf2SBastian Blank } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 665635324e8SGreg Kurz if (!strcmp(entry->d_name, VIRTFS_META_DIR)) { 6662c30dd74SAneesh Kumar K.V /* skp the meta data directory */ 6672c30dd74SAneesh Kumar K.V goto again; 6682c30dd74SAneesh Kumar K.V } 669840a1bf2SBastian Blank entry->d_type = DT_UNKNOWN; 6702c30dd74SAneesh Kumar K.V } 671635324e8SGreg Kurz 672635324e8SGreg Kurz return entry; 673a9231555SAnthony Liguori } 674a9231555SAnthony Liguori 675cc720ddbSAneesh Kumar K.V static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off) 676a9231555SAnthony Liguori { 677f314ea4eSGreg Kurz seekdir(fs->dir.stream, off); 678a9231555SAnthony Liguori } 679a9231555SAnthony Liguori 680cc720ddbSAneesh Kumar K.V static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs, 681cc720ddbSAneesh Kumar K.V const struct iovec *iov, 68256d15a53SSanchit Garg int iovcnt, off_t offset) 683a9231555SAnthony Liguori { 68456d15a53SSanchit Garg #ifdef CONFIG_PREADV 685cc720ddbSAneesh Kumar K.V return preadv(fs->fd, iov, iovcnt, offset); 68656d15a53SSanchit Garg #else 687cc720ddbSAneesh Kumar K.V int err = lseek(fs->fd, offset, SEEK_SET); 68856d15a53SSanchit Garg if (err == -1) { 68956d15a53SSanchit Garg return err; 69056d15a53SSanchit Garg } else { 691cc720ddbSAneesh Kumar K.V return readv(fs->fd, iov, iovcnt); 692a9231555SAnthony Liguori } 69356d15a53SSanchit Garg #endif 694a9231555SAnthony Liguori } 695a9231555SAnthony Liguori 696cc720ddbSAneesh Kumar K.V static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs, 697cc720ddbSAneesh Kumar K.V const struct iovec *iov, 69856d15a53SSanchit Garg int iovcnt, off_t offset) 6998449360cSAnthony Liguori { 7006fe76accSGreg Kurz ssize_t ret; 70156d15a53SSanchit Garg #ifdef CONFIG_PREADV 702cc720ddbSAneesh Kumar K.V ret = pwritev(fs->fd, iov, iovcnt, offset); 70356d15a53SSanchit Garg #else 704cc720ddbSAneesh Kumar K.V int err = lseek(fs->fd, offset, SEEK_SET); 70556d15a53SSanchit Garg if (err == -1) { 70656d15a53SSanchit Garg return err; 70756d15a53SSanchit Garg } else { 708cc720ddbSAneesh Kumar K.V ret = writev(fs->fd, iov, iovcnt); 7098449360cSAnthony Liguori } 71056d15a53SSanchit Garg #endif 711d3ab98e6SAneesh Kumar K.V #ifdef CONFIG_SYNC_FILE_RANGE 712d3ab98e6SAneesh Kumar K.V if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) { 713d3ab98e6SAneesh Kumar K.V /* 714d3ab98e6SAneesh Kumar K.V * Initiate a writeback. This is not a data integrity sync. 715d3ab98e6SAneesh Kumar K.V * We want to ensure that we don't leave dirty pages in the cache 716d3ab98e6SAneesh Kumar K.V * after write when writeout=immediate is sepcified. 717d3ab98e6SAneesh Kumar K.V */ 718cc720ddbSAneesh Kumar K.V sync_file_range(fs->fd, offset, ret, 719d3ab98e6SAneesh Kumar K.V SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE); 720d3ab98e6SAneesh Kumar K.V } 721d3ab98e6SAneesh Kumar K.V #endif 722d3ab98e6SAneesh Kumar K.V return ret; 72356d15a53SSanchit Garg } 7248449360cSAnthony Liguori 7252289be19SAneesh Kumar K.V static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp) 726c494dd6fSAnthony Liguori { 727e3187a45SGreg Kurz char *dirpath = g_path_get_dirname(fs_path->data); 728e3187a45SGreg Kurz char *name = g_path_get_basename(fs_path->data); 7294fa4ce71SChen Gang int ret = -1; 730e3187a45SGreg Kurz int dirfd; 731e3187a45SGreg Kurz 732e3187a45SGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dirpath); 733e3187a45SGreg Kurz if (dirfd == -1) { 734e3187a45SGreg Kurz goto out; 735e3187a45SGreg Kurz } 7362289be19SAneesh Kumar K.V 737b97400caSAneesh Kumar K.V if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 738e3187a45SGreg Kurz ret = local_set_xattrat(dirfd, name, credp); 7392c30dd74SAneesh Kumar K.V } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 740e3187a45SGreg Kurz ret = local_set_mapped_file_attrat(dirfd, name, credp); 741e3187a45SGreg Kurz } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH || 742e3187a45SGreg Kurz fs_ctx->export_flags & V9FS_SM_NONE) { 743e3187a45SGreg Kurz ret = fchmodat_nofollow(dirfd, name, credp->fc_mode); 744e95ead32SVenkateswararao Jujjuri (JV) } 745e3187a45SGreg Kurz close_preserve_errno(dirfd); 746e3187a45SGreg Kurz 747e3187a45SGreg Kurz out: 748e3187a45SGreg Kurz g_free(dirpath); 749e3187a45SGreg Kurz g_free(name); 7504fa4ce71SChen Gang return ret; 751c494dd6fSAnthony Liguori } 752c494dd6fSAnthony Liguori 7532289be19SAneesh Kumar K.V static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path, 7542289be19SAneesh Kumar K.V const char *name, FsCred *credp) 755c494dd6fSAnthony Liguori { 7561c293312SVenkateswararao Jujjuri (JV) int err = -1; 757d815e721SGreg Kurz int dirfd; 7581c293312SVenkateswararao Jujjuri (JV) 759d815e721SGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dir_path->data); 760d815e721SGreg Kurz if (dirfd == -1) { 761d815e721SGreg Kurz return -1; 762d815e721SGreg Kurz } 7632289be19SAneesh Kumar K.V 764d815e721SGreg Kurz if (fs_ctx->export_flags & V9FS_SM_MAPPED || 765d815e721SGreg Kurz fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 766d815e721SGreg Kurz err = mknodat(dirfd, name, SM_LOCAL_MODE_BITS | S_IFREG, 0); 767d815e721SGreg Kurz if (err == -1) { 768d815e721SGreg Kurz goto out; 769d815e721SGreg Kurz } 770d815e721SGreg Kurz 771b97400caSAneesh Kumar K.V if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 772d815e721SGreg Kurz err = local_set_xattrat(dirfd, name, credp); 773d815e721SGreg Kurz } else { 774d815e721SGreg Kurz err = local_set_mapped_file_attrat(dirfd, name, credp); 7751c293312SVenkateswararao Jujjuri (JV) } 7761c293312SVenkateswararao Jujjuri (JV) if (err == -1) { 7771c293312SVenkateswararao Jujjuri (JV) goto err_end; 7781c293312SVenkateswararao Jujjuri (JV) } 779d815e721SGreg Kurz } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH || 780d815e721SGreg Kurz fs_ctx->export_flags & V9FS_SM_NONE) { 781d815e721SGreg Kurz err = mknodat(dirfd, name, credp->fc_mode, credp->fc_rdev); 7822c30dd74SAneesh Kumar K.V if (err == -1) { 7832c30dd74SAneesh Kumar K.V goto out; 7842c30dd74SAneesh Kumar K.V } 785d815e721SGreg Kurz err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp); 7862c30dd74SAneesh Kumar K.V if (err == -1) { 7871c293312SVenkateswararao Jujjuri (JV) goto err_end; 7881c293312SVenkateswararao Jujjuri (JV) } 7891c293312SVenkateswararao Jujjuri (JV) } 7902289be19SAneesh Kumar K.V goto out; 7911c293312SVenkateswararao Jujjuri (JV) 7921c293312SVenkateswararao Jujjuri (JV) err_end: 793d815e721SGreg Kurz unlinkat_preserve_errno(dirfd, name, 0); 7942289be19SAneesh Kumar K.V out: 795d815e721SGreg Kurz close_preserve_errno(dirfd); 7961c293312SVenkateswararao Jujjuri (JV) return err; 797c494dd6fSAnthony Liguori } 798c494dd6fSAnthony Liguori 7992289be19SAneesh Kumar K.V static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path, 8002289be19SAneesh Kumar K.V const char *name, FsCred *credp) 801c494dd6fSAnthony Liguori { 80200ec5c37SVenkateswararao Jujjuri (JV) int err = -1; 8033f3a1699SGreg Kurz int dirfd; 80400ec5c37SVenkateswararao Jujjuri (JV) 8053f3a1699SGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dir_path->data); 8063f3a1699SGreg Kurz if (dirfd == -1) { 8073f3a1699SGreg Kurz return -1; 8083f3a1699SGreg Kurz } 8092289be19SAneesh Kumar K.V 8103f3a1699SGreg Kurz if (fs_ctx->export_flags & V9FS_SM_MAPPED || 8113f3a1699SGreg Kurz fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 8123f3a1699SGreg Kurz err = mkdirat(dirfd, name, SM_LOCAL_DIR_MODE_BITS); 8133f3a1699SGreg Kurz if (err == -1) { 8143f3a1699SGreg Kurz goto out; 8153f3a1699SGreg Kurz } 8163f3a1699SGreg Kurz credp->fc_mode = credp->fc_mode | S_IFDIR; 8173f3a1699SGreg Kurz 818b97400caSAneesh Kumar K.V if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 8193f3a1699SGreg Kurz err = local_set_xattrat(dirfd, name, credp); 8203f3a1699SGreg Kurz } else { 8213f3a1699SGreg Kurz err = local_set_mapped_file_attrat(dirfd, name, credp); 82200ec5c37SVenkateswararao Jujjuri (JV) } 82300ec5c37SVenkateswararao Jujjuri (JV) if (err == -1) { 82400ec5c37SVenkateswararao Jujjuri (JV) goto err_end; 82500ec5c37SVenkateswararao Jujjuri (JV) } 8263f3a1699SGreg Kurz } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH || 8273f3a1699SGreg Kurz fs_ctx->export_flags & V9FS_SM_NONE) { 8283f3a1699SGreg Kurz err = mkdirat(dirfd, name, credp->fc_mode); 8292c30dd74SAneesh Kumar K.V if (err == -1) { 8302c30dd74SAneesh Kumar K.V goto out; 8312c30dd74SAneesh Kumar K.V } 8323f3a1699SGreg Kurz err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp); 8332c30dd74SAneesh Kumar K.V if (err == -1) { 83400ec5c37SVenkateswararao Jujjuri (JV) goto err_end; 83500ec5c37SVenkateswararao Jujjuri (JV) } 83600ec5c37SVenkateswararao Jujjuri (JV) } 8372289be19SAneesh Kumar K.V goto out; 83800ec5c37SVenkateswararao Jujjuri (JV) 83900ec5c37SVenkateswararao Jujjuri (JV) err_end: 8403f3a1699SGreg Kurz unlinkat_preserve_errno(dirfd, name, AT_REMOVEDIR); 8412289be19SAneesh Kumar K.V out: 8423f3a1699SGreg Kurz close_preserve_errno(dirfd); 84300ec5c37SVenkateswararao Jujjuri (JV) return err; 844c494dd6fSAnthony Liguori } 845c494dd6fSAnthony Liguori 8468b888272SAneesh Kumar K.V static int local_fstat(FsContext *fs_ctx, int fid_type, 847cc720ddbSAneesh Kumar K.V V9fsFidOpenState *fs, struct stat *stbuf) 848c494dd6fSAnthony Liguori { 8498b888272SAneesh Kumar K.V int err, fd; 8508b888272SAneesh Kumar K.V 8518b888272SAneesh Kumar K.V if (fid_type == P9_FID_DIR) { 852f314ea4eSGreg Kurz fd = dirfd(fs->dir.stream); 8538b888272SAneesh Kumar K.V } else { 8548b888272SAneesh Kumar K.V fd = fs->fd; 8558b888272SAneesh Kumar K.V } 8568b888272SAneesh Kumar K.V 8578b888272SAneesh Kumar K.V err = fstat(fd, stbuf); 8581237ad76SVenkateswararao Jujjuri (JV) if (err) { 8591237ad76SVenkateswararao Jujjuri (JV) return err; 8601237ad76SVenkateswararao Jujjuri (JV) } 861b97400caSAneesh Kumar K.V if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 8621237ad76SVenkateswararao Jujjuri (JV) /* Actual credentials are part of extended attrs */ 8631237ad76SVenkateswararao Jujjuri (JV) uid_t tmp_uid; 8641237ad76SVenkateswararao Jujjuri (JV) gid_t tmp_gid; 8651237ad76SVenkateswararao Jujjuri (JV) mode_t tmp_mode; 8661237ad76SVenkateswararao Jujjuri (JV) dev_t tmp_dev; 8671237ad76SVenkateswararao Jujjuri (JV) 868f8ad4a89SAneesh Kumar K.V if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) { 869f8ad4a89SAneesh Kumar K.V stbuf->st_uid = le32_to_cpu(tmp_uid); 8701237ad76SVenkateswararao Jujjuri (JV) } 871f8ad4a89SAneesh Kumar K.V if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) { 872f8ad4a89SAneesh Kumar K.V stbuf->st_gid = le32_to_cpu(tmp_gid); 8731237ad76SVenkateswararao Jujjuri (JV) } 874f8ad4a89SAneesh Kumar K.V if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) { 875f8ad4a89SAneesh Kumar K.V stbuf->st_mode = le32_to_cpu(tmp_mode); 8761237ad76SVenkateswararao Jujjuri (JV) } 877f8ad4a89SAneesh Kumar K.V if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) { 878f8ad4a89SAneesh Kumar K.V stbuf->st_rdev = le64_to_cpu(tmp_dev); 8791237ad76SVenkateswararao Jujjuri (JV) } 8802c30dd74SAneesh Kumar K.V } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 8812c30dd74SAneesh Kumar K.V errno = EOPNOTSUPP; 8822c30dd74SAneesh Kumar K.V return -1; 8831237ad76SVenkateswararao Jujjuri (JV) } 8841237ad76SVenkateswararao Jujjuri (JV) return err; 885c494dd6fSAnthony Liguori } 886c494dd6fSAnthony Liguori 8872289be19SAneesh Kumar K.V static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name, 888cc720ddbSAneesh Kumar K.V int flags, FsCred *credp, V9fsFidOpenState *fs) 889c494dd6fSAnthony Liguori { 8904750a96fSVenkateswararao Jujjuri (JV) int fd = -1; 8914750a96fSVenkateswararao Jujjuri (JV) int err = -1; 892*a565fea5SGreg Kurz int dirfd; 8934750a96fSVenkateswararao Jujjuri (JV) 8940ceb092eSAneesh Kumar K.V /* 8950ceb092eSAneesh Kumar K.V * Mark all the open to not follow symlinks 8960ceb092eSAneesh Kumar K.V */ 8970ceb092eSAneesh Kumar K.V flags |= O_NOFOLLOW; 8980ceb092eSAneesh Kumar K.V 899*a565fea5SGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dir_path->data); 900*a565fea5SGreg Kurz if (dirfd == -1) { 901*a565fea5SGreg Kurz return -1; 902*a565fea5SGreg Kurz } 9032289be19SAneesh Kumar K.V 9044750a96fSVenkateswararao Jujjuri (JV) /* Determine the security model */ 905*a565fea5SGreg Kurz if (fs_ctx->export_flags & V9FS_SM_MAPPED || 906*a565fea5SGreg Kurz fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 907*a565fea5SGreg Kurz fd = openat_file(dirfd, name, flags, SM_LOCAL_MODE_BITS); 908*a565fea5SGreg Kurz if (fd == -1) { 909*a565fea5SGreg Kurz goto out; 910*a565fea5SGreg Kurz } 911*a565fea5SGreg Kurz credp->fc_mode = credp->fc_mode|S_IFREG; 912b97400caSAneesh Kumar K.V if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 9134750a96fSVenkateswararao Jujjuri (JV) /* Set cleint credentials in xattr */ 914*a565fea5SGreg Kurz err = local_set_xattrat(dirfd, name, credp); 915*a565fea5SGreg Kurz } else { 916*a565fea5SGreg Kurz err = local_set_mapped_file_attrat(dirfd, name, credp); 9174750a96fSVenkateswararao Jujjuri (JV) } 9182c30dd74SAneesh Kumar K.V if (err == -1) { 9192c30dd74SAneesh Kumar K.V goto err_end; 9202c30dd74SAneesh Kumar K.V } 921b97400caSAneesh Kumar K.V } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 922b97400caSAneesh Kumar K.V (fs_ctx->export_flags & V9FS_SM_NONE)) { 923*a565fea5SGreg Kurz fd = openat_file(dirfd, name, flags, credp->fc_mode); 9244750a96fSVenkateswararao Jujjuri (JV) if (fd == -1) { 9252289be19SAneesh Kumar K.V goto out; 9264750a96fSVenkateswararao Jujjuri (JV) } 927*a565fea5SGreg Kurz err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp); 9284750a96fSVenkateswararao Jujjuri (JV) if (err == -1) { 9294750a96fSVenkateswararao Jujjuri (JV) goto err_end; 9304750a96fSVenkateswararao Jujjuri (JV) } 9314750a96fSVenkateswararao Jujjuri (JV) } 9322289be19SAneesh Kumar K.V err = fd; 933cc720ddbSAneesh Kumar K.V fs->fd = fd; 9342289be19SAneesh Kumar K.V goto out; 9354750a96fSVenkateswararao Jujjuri (JV) 9364750a96fSVenkateswararao Jujjuri (JV) err_end: 937*a565fea5SGreg Kurz unlinkat_preserve_errno(dirfd, name, 938*a565fea5SGreg Kurz flags & O_DIRECTORY ? AT_REMOVEDIR : 0); 939*a565fea5SGreg Kurz close_preserve_errno(fd); 9402289be19SAneesh Kumar K.V out: 941*a565fea5SGreg Kurz close_preserve_errno(dirfd); 9424750a96fSVenkateswararao Jujjuri (JV) return err; 943c494dd6fSAnthony Liguori } 944c494dd6fSAnthony Liguori 945758e8e38SVenkateswararao Jujjuri (JV) 946879c2813SVenkateswararao Jujjuri (JV) static int local_symlink(FsContext *fs_ctx, const char *oldpath, 9472289be19SAneesh Kumar K.V V9fsPath *dir_path, const char *name, FsCred *credp) 948c494dd6fSAnthony Liguori { 949879c2813SVenkateswararao Jujjuri (JV) int err = -1; 95038771613SGreg Kurz int dirfd; 951879c2813SVenkateswararao Jujjuri (JV) 95238771613SGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dir_path->data); 95338771613SGreg Kurz if (dirfd == -1) { 95438771613SGreg Kurz return -1; 95538771613SGreg Kurz } 9562289be19SAneesh Kumar K.V 957879c2813SVenkateswararao Jujjuri (JV) /* Determine the security model */ 95838771613SGreg Kurz if (fs_ctx->export_flags & V9FS_SM_MAPPED || 95938771613SGreg Kurz fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 96038771613SGreg Kurz int fd; 96138771613SGreg Kurz ssize_t oldpath_size, write_size; 96238771613SGreg Kurz 96338771613SGreg Kurz fd = openat_file(dirfd, name, O_CREAT | O_EXCL | O_RDWR, 96438771613SGreg Kurz SM_LOCAL_MODE_BITS); 96538771613SGreg Kurz if (fd == -1) { 96638771613SGreg Kurz goto out; 96738771613SGreg Kurz } 96838771613SGreg Kurz /* Write the oldpath (target) to the file. */ 96938771613SGreg Kurz oldpath_size = strlen(oldpath); 97038771613SGreg Kurz do { 97138771613SGreg Kurz write_size = write(fd, (void *)oldpath, oldpath_size); 97238771613SGreg Kurz } while (write_size == -1 && errno == EINTR); 97338771613SGreg Kurz close_preserve_errno(fd); 97438771613SGreg Kurz 97538771613SGreg Kurz if (write_size != oldpath_size) { 97638771613SGreg Kurz goto err_end; 97738771613SGreg Kurz } 97838771613SGreg Kurz /* Set cleint credentials in symlink's xattr */ 97938771613SGreg Kurz credp->fc_mode = credp->fc_mode | S_IFLNK; 98038771613SGreg Kurz 981b97400caSAneesh Kumar K.V if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 98238771613SGreg Kurz err = local_set_xattrat(dirfd, name, credp); 98338771613SGreg Kurz } else { 98438771613SGreg Kurz err = local_set_mapped_file_attrat(dirfd, name, credp); 985879c2813SVenkateswararao Jujjuri (JV) } 986879c2813SVenkateswararao Jujjuri (JV) if (err == -1) { 987879c2813SVenkateswararao Jujjuri (JV) goto err_end; 988879c2813SVenkateswararao Jujjuri (JV) } 98938771613SGreg Kurz } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH || 99038771613SGreg Kurz fs_ctx->export_flags & V9FS_SM_NONE) { 99138771613SGreg Kurz err = symlinkat(oldpath, dirfd, name); 992879c2813SVenkateswararao Jujjuri (JV) if (err) { 9932289be19SAneesh Kumar K.V goto out; 994879c2813SVenkateswararao Jujjuri (JV) } 99538771613SGreg Kurz err = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid, 99638771613SGreg Kurz AT_SYMLINK_NOFOLLOW); 997879c2813SVenkateswararao Jujjuri (JV) if (err == -1) { 99812848bfcSAneesh Kumar K.V /* 99912848bfcSAneesh Kumar K.V * If we fail to change ownership and if we are 100012848bfcSAneesh Kumar K.V * using security model none. Ignore the error 100112848bfcSAneesh Kumar K.V */ 1002b97400caSAneesh Kumar K.V if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) { 1003879c2813SVenkateswararao Jujjuri (JV) goto err_end; 100438771613SGreg Kurz } else { 100512848bfcSAneesh Kumar K.V err = 0; 1006879c2813SVenkateswararao Jujjuri (JV) } 1007879c2813SVenkateswararao Jujjuri (JV) } 100838771613SGreg Kurz } 10092289be19SAneesh Kumar K.V goto out; 1010879c2813SVenkateswararao Jujjuri (JV) 1011879c2813SVenkateswararao Jujjuri (JV) err_end: 101238771613SGreg Kurz unlinkat_preserve_errno(dirfd, name, 0); 10132289be19SAneesh Kumar K.V out: 101438771613SGreg Kurz close_preserve_errno(dirfd); 1015879c2813SVenkateswararao Jujjuri (JV) return err; 1016c494dd6fSAnthony Liguori } 1017c494dd6fSAnthony Liguori 10182289be19SAneesh Kumar K.V static int local_link(FsContext *ctx, V9fsPath *oldpath, 10192289be19SAneesh Kumar K.V V9fsPath *dirpath, const char *name) 1020c494dd6fSAnthony Liguori { 1021ad0b46e6SGreg Kurz char *odirpath = g_path_get_dirname(oldpath->data); 1022ad0b46e6SGreg Kurz char *oname = g_path_get_basename(oldpath->data); 1023ad0b46e6SGreg Kurz int ret = -1; 1024ad0b46e6SGreg Kurz int odirfd, ndirfd; 1025c494dd6fSAnthony Liguori 1026ad0b46e6SGreg Kurz odirfd = local_opendir_nofollow(ctx, odirpath); 1027ad0b46e6SGreg Kurz if (odirfd == -1) { 10286dd4b1f1SGreg Kurz goto out; 10296dd4b1f1SGreg Kurz } 10302c30dd74SAneesh Kumar K.V 1031ad0b46e6SGreg Kurz ndirfd = local_opendir_nofollow(ctx, dirpath->data); 1032ad0b46e6SGreg Kurz if (ndirfd == -1) { 1033ad0b46e6SGreg Kurz close_preserve_errno(odirfd); 1034ad0b46e6SGreg Kurz goto out; 1035ad0b46e6SGreg Kurz } 1036ad0b46e6SGreg Kurz 1037ad0b46e6SGreg Kurz ret = linkat(odirfd, oname, ndirfd, name, 0); 1038ad0b46e6SGreg Kurz if (ret < 0) { 1039ad0b46e6SGreg Kurz goto out_close; 1040ad0b46e6SGreg Kurz } 1041ad0b46e6SGreg Kurz 10422c30dd74SAneesh Kumar K.V /* now link the virtfs_metadata files */ 10436dd4b1f1SGreg Kurz if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 1044ad0b46e6SGreg Kurz int omap_dirfd, nmap_dirfd; 10456dd4b1f1SGreg Kurz 1046ad0b46e6SGreg Kurz ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700); 1047ad0b46e6SGreg Kurz if (ret < 0 && errno != EEXIST) { 1048ad0b46e6SGreg Kurz goto err_undo_link; 10492c30dd74SAneesh Kumar K.V } 1050ad0b46e6SGreg Kurz 1051ad0b46e6SGreg Kurz omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR); 1052ad0b46e6SGreg Kurz if (omap_dirfd == -1) { 1053ad0b46e6SGreg Kurz goto err; 1054ad0b46e6SGreg Kurz } 1055ad0b46e6SGreg Kurz 1056ad0b46e6SGreg Kurz nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR); 1057ad0b46e6SGreg Kurz if (nmap_dirfd == -1) { 1058ad0b46e6SGreg Kurz close_preserve_errno(omap_dirfd); 1059ad0b46e6SGreg Kurz goto err; 1060ad0b46e6SGreg Kurz } 1061ad0b46e6SGreg Kurz 1062ad0b46e6SGreg Kurz ret = linkat(omap_dirfd, oname, nmap_dirfd, name, 0); 1063ad0b46e6SGreg Kurz close_preserve_errno(nmap_dirfd); 1064ad0b46e6SGreg Kurz close_preserve_errno(omap_dirfd); 10652c30dd74SAneesh Kumar K.V if (ret < 0 && errno != ENOENT) { 1066ad0b46e6SGreg Kurz goto err_undo_link; 10672c30dd74SAneesh Kumar K.V } 10686dd4b1f1SGreg Kurz 1069ad0b46e6SGreg Kurz ret = 0; 1070ad0b46e6SGreg Kurz } 1071ad0b46e6SGreg Kurz goto out_close; 1072ad0b46e6SGreg Kurz 1073ad0b46e6SGreg Kurz err: 1074ad0b46e6SGreg Kurz ret = -1; 1075ad0b46e6SGreg Kurz err_undo_link: 1076ad0b46e6SGreg Kurz unlinkat_preserve_errno(ndirfd, name, 0); 1077ad0b46e6SGreg Kurz out_close: 1078ad0b46e6SGreg Kurz close_preserve_errno(ndirfd); 1079ad0b46e6SGreg Kurz close_preserve_errno(odirfd); 10806dd4b1f1SGreg Kurz out: 1081ad0b46e6SGreg Kurz g_free(oname); 1082ad0b46e6SGreg Kurz g_free(odirpath); 10832289be19SAneesh Kumar K.V return ret; 1084c494dd6fSAnthony Liguori } 1085c494dd6fSAnthony Liguori 10862289be19SAneesh Kumar K.V static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size) 10878cf89e00SAnthony Liguori { 1088ac125d99SGreg Kurz int fd, ret; 10892289be19SAneesh Kumar K.V 1090ac125d99SGreg Kurz fd = local_open_nofollow(ctx, fs_path->data, O_WRONLY, 0); 1091ac125d99SGreg Kurz if (fd == -1) { 1092ac125d99SGreg Kurz return -1; 1093ac125d99SGreg Kurz } 1094ac125d99SGreg Kurz ret = ftruncate(fd, size); 1095ac125d99SGreg Kurz close_preserve_errno(fd); 10964fa4ce71SChen Gang return ret; 10978cf89e00SAnthony Liguori } 10988cf89e00SAnthony Liguori 10992289be19SAneesh Kumar K.V static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp) 11008cf89e00SAnthony Liguori { 1101d369f207SGreg Kurz char *dirpath = g_path_get_dirname(fs_path->data); 1102d369f207SGreg Kurz char *name = g_path_get_basename(fs_path->data); 11034fa4ce71SChen Gang int ret = -1; 1104d369f207SGreg Kurz int dirfd; 1105d369f207SGreg Kurz 1106d369f207SGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dirpath); 1107d369f207SGreg Kurz if (dirfd == -1) { 1108d369f207SGreg Kurz goto out; 1109d369f207SGreg Kurz } 11102289be19SAneesh Kumar K.V 1111c79ce737SSripathi Kodi if ((credp->fc_uid == -1 && credp->fc_gid == -1) || 111217b1971fSAneesh Kumar K.V (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 111317b1971fSAneesh Kumar K.V (fs_ctx->export_flags & V9FS_SM_NONE)) { 1114d369f207SGreg Kurz ret = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid, 1115d369f207SGreg Kurz AT_SYMLINK_NOFOLLOW); 1116b97400caSAneesh Kumar K.V } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 1117d369f207SGreg Kurz ret = local_set_xattrat(dirfd, name, credp); 11182c30dd74SAneesh Kumar K.V } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 1119d369f207SGreg Kurz ret = local_set_mapped_file_attrat(dirfd, name, credp); 1120f7613beeSVenkateswararao Jujjuri (JV) } 1121d369f207SGreg Kurz 1122d369f207SGreg Kurz close_preserve_errno(dirfd); 1123d369f207SGreg Kurz out: 1124d369f207SGreg Kurz g_free(name); 1125d369f207SGreg Kurz g_free(dirpath); 11264fa4ce71SChen Gang return ret; 11278cf89e00SAnthony Liguori } 11288cf89e00SAnthony Liguori 11292289be19SAneesh Kumar K.V static int local_utimensat(FsContext *s, V9fsPath *fs_path, 113074bc02b2SM. Mohan Kumar const struct timespec *buf) 11318cf89e00SAnthony Liguori { 1132a33eda0dSGreg Kurz char *dirpath = g_path_get_dirname(fs_path->data); 1133a33eda0dSGreg Kurz char *name = g_path_get_basename(fs_path->data); 1134a33eda0dSGreg Kurz int dirfd, ret = -1; 11352289be19SAneesh Kumar K.V 1136a33eda0dSGreg Kurz dirfd = local_opendir_nofollow(s, dirpath); 1137a33eda0dSGreg Kurz if (dirfd == -1) { 1138a33eda0dSGreg Kurz goto out; 1139a33eda0dSGreg Kurz } 1140a33eda0dSGreg Kurz 1141a33eda0dSGreg Kurz ret = utimensat(dirfd, name, buf, AT_SYMLINK_NOFOLLOW); 1142a33eda0dSGreg Kurz close_preserve_errno(dirfd); 1143a33eda0dSGreg Kurz out: 1144a33eda0dSGreg Kurz g_free(dirpath); 1145a33eda0dSGreg Kurz g_free(name); 11464fa4ce71SChen Gang return ret; 11478cf89e00SAnthony Liguori } 11488cf89e00SAnthony Liguori 1149df4938a6SGreg Kurz static int local_unlinkat_common(FsContext *ctx, int dirfd, const char *name, 1150df4938a6SGreg Kurz int flags) 1151df4938a6SGreg Kurz { 1152df4938a6SGreg Kurz int ret = -1; 1153df4938a6SGreg Kurz 1154df4938a6SGreg Kurz if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 1155df4938a6SGreg Kurz int map_dirfd; 1156df4938a6SGreg Kurz 1157df4938a6SGreg Kurz if (flags == AT_REMOVEDIR) { 1158df4938a6SGreg Kurz int fd; 1159df4938a6SGreg Kurz 1160df4938a6SGreg Kurz fd = openat(dirfd, name, O_RDONLY | O_DIRECTORY | O_PATH); 1161df4938a6SGreg Kurz if (fd == -1) { 1162df4938a6SGreg Kurz goto err_out; 1163df4938a6SGreg Kurz } 1164df4938a6SGreg Kurz /* 1165df4938a6SGreg Kurz * If directory remove .virtfs_metadata contained in the 1166df4938a6SGreg Kurz * directory 1167df4938a6SGreg Kurz */ 1168df4938a6SGreg Kurz ret = unlinkat(fd, VIRTFS_META_DIR, AT_REMOVEDIR); 1169df4938a6SGreg Kurz close_preserve_errno(fd); 1170df4938a6SGreg Kurz if (ret < 0 && errno != ENOENT) { 1171df4938a6SGreg Kurz /* 1172df4938a6SGreg Kurz * We didn't had the .virtfs_metadata file. May be file created 1173df4938a6SGreg Kurz * in non-mapped mode ?. Ignore ENOENT. 1174df4938a6SGreg Kurz */ 1175df4938a6SGreg Kurz goto err_out; 1176df4938a6SGreg Kurz } 1177df4938a6SGreg Kurz } 1178df4938a6SGreg Kurz /* 1179df4938a6SGreg Kurz * Now remove the name from parent directory 1180df4938a6SGreg Kurz * .virtfs_metadata directory. 1181df4938a6SGreg Kurz */ 1182df4938a6SGreg Kurz map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR); 1183df4938a6SGreg Kurz ret = unlinkat(map_dirfd, name, 0); 1184df4938a6SGreg Kurz close_preserve_errno(map_dirfd); 1185df4938a6SGreg Kurz if (ret < 0 && errno != ENOENT) { 1186df4938a6SGreg Kurz /* 1187df4938a6SGreg Kurz * We didn't had the .virtfs_metadata file. May be file created 1188df4938a6SGreg Kurz * in non-mapped mode ?. Ignore ENOENT. 1189df4938a6SGreg Kurz */ 1190df4938a6SGreg Kurz goto err_out; 1191df4938a6SGreg Kurz } 1192df4938a6SGreg Kurz } 1193df4938a6SGreg Kurz 1194df4938a6SGreg Kurz ret = unlinkat(dirfd, name, flags); 1195df4938a6SGreg Kurz err_out: 1196df4938a6SGreg Kurz return ret; 1197df4938a6SGreg Kurz } 1198df4938a6SGreg Kurz 11995bae1900SAnthony Liguori static int local_remove(FsContext *ctx, const char *path) 12005bae1900SAnthony Liguori { 12012c30dd74SAneesh Kumar K.V struct stat stbuf; 1202a0e640a8SGreg Kurz char *dirpath = g_path_get_dirname(path); 1203a0e640a8SGreg Kurz char *name = g_path_get_basename(path); 1204a0e640a8SGreg Kurz int flags = 0; 1205a0e640a8SGreg Kurz int dirfd; 1206a0e640a8SGreg Kurz int err = -1; 12072c30dd74SAneesh Kumar K.V 1208a0e640a8SGreg Kurz dirfd = local_opendir_nofollow(ctx, dirpath); 1209a0e640a8SGreg Kurz if (dirfd) { 1210a0e640a8SGreg Kurz goto out; 1211a0e640a8SGreg Kurz } 1212a0e640a8SGreg Kurz 1213a0e640a8SGreg Kurz if (fstatat(dirfd, path, &stbuf, AT_SYMLINK_NOFOLLOW) < 0) { 12142c30dd74SAneesh Kumar K.V goto err_out; 12152c30dd74SAneesh Kumar K.V } 1216a0e640a8SGreg Kurz 12172c30dd74SAneesh Kumar K.V if (S_ISDIR(stbuf.st_mode)) { 1218a0e640a8SGreg Kurz flags |= AT_REMOVEDIR; 12192c30dd74SAneesh Kumar K.V } 12204fa4ce71SChen Gang 1221a0e640a8SGreg Kurz err = local_unlinkat_common(ctx, dirfd, name, flags); 12222c30dd74SAneesh Kumar K.V err_out: 1223a0e640a8SGreg Kurz close_preserve_errno(dirfd); 1224a0e640a8SGreg Kurz out: 1225a0e640a8SGreg Kurz g_free(name); 1226a0e640a8SGreg Kurz g_free(dirpath); 12272c30dd74SAneesh Kumar K.V return err; 12285bae1900SAnthony Liguori } 12295bae1900SAnthony Liguori 12308b888272SAneesh Kumar K.V static int local_fsync(FsContext *ctx, int fid_type, 12318b888272SAneesh Kumar K.V V9fsFidOpenState *fs, int datasync) 12328cf89e00SAnthony Liguori { 12338b888272SAneesh Kumar K.V int fd; 12348b888272SAneesh Kumar K.V 12358b888272SAneesh Kumar K.V if (fid_type == P9_FID_DIR) { 1236f314ea4eSGreg Kurz fd = dirfd(fs->dir.stream); 123749594973SVenkateswararao Jujjuri (JV) } else { 12388b888272SAneesh Kumar K.V fd = fs->fd; 12398b888272SAneesh Kumar K.V } 12408b888272SAneesh Kumar K.V 12418b888272SAneesh Kumar K.V if (datasync) { 12428b888272SAneesh Kumar K.V return qemu_fdatasync(fd); 12438b888272SAneesh Kumar K.V } else { 12448b888272SAneesh Kumar K.V return fsync(fd); 12458cf89e00SAnthony Liguori } 124649594973SVenkateswararao Jujjuri (JV) } 12478cf89e00SAnthony Liguori 12482289be19SAneesh Kumar K.V static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf) 1249be940c87SM. Mohan Kumar { 125031e51d1cSGreg Kurz int fd, ret; 12512289be19SAneesh Kumar K.V 125231e51d1cSGreg Kurz fd = local_open_nofollow(s, fs_path->data, O_RDONLY, 0); 125331e51d1cSGreg Kurz ret = fstatfs(fd, stbuf); 125431e51d1cSGreg Kurz close_preserve_errno(fd); 12554fa4ce71SChen Gang return ret; 1256be940c87SM. Mohan Kumar } 1257be940c87SM. Mohan Kumar 12582289be19SAneesh Kumar K.V static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path, 1259fa32ef88SAneesh Kumar K.V const char *name, void *value, size_t size) 1260fa32ef88SAneesh Kumar K.V { 12612289be19SAneesh Kumar K.V char *path = fs_path->data; 12622289be19SAneesh Kumar K.V 1263fc22118dSAneesh Kumar K.V return v9fs_get_xattr(ctx, path, name, value, size); 1264fa32ef88SAneesh Kumar K.V } 1265fa32ef88SAneesh Kumar K.V 12662289be19SAneesh Kumar K.V static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path, 1267fa32ef88SAneesh Kumar K.V void *value, size_t size) 1268fa32ef88SAneesh Kumar K.V { 12692289be19SAneesh Kumar K.V char *path = fs_path->data; 12702289be19SAneesh Kumar K.V 1271fc22118dSAneesh Kumar K.V return v9fs_list_xattr(ctx, path, value, size); 127261b6c499SAneesh Kumar K.V } 127361b6c499SAneesh Kumar K.V 12742289be19SAneesh Kumar K.V static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name, 127510b468bdSAneesh Kumar K.V void *value, size_t size, int flags) 127610b468bdSAneesh Kumar K.V { 12772289be19SAneesh Kumar K.V char *path = fs_path->data; 12782289be19SAneesh Kumar K.V 1279fc22118dSAneesh Kumar K.V return v9fs_set_xattr(ctx, path, name, value, size, flags); 128010b468bdSAneesh Kumar K.V } 128110b468bdSAneesh Kumar K.V 12822289be19SAneesh Kumar K.V static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path, 12832289be19SAneesh Kumar K.V const char *name) 12849ed3ef26SAneesh Kumar K.V { 12852289be19SAneesh Kumar K.V char *path = fs_path->data; 12862289be19SAneesh Kumar K.V 1287fc22118dSAneesh Kumar K.V return v9fs_remove_xattr(ctx, path, name); 12889ed3ef26SAneesh Kumar K.V } 12899ed3ef26SAneesh Kumar K.V 12902289be19SAneesh Kumar K.V static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path, 12912289be19SAneesh Kumar K.V const char *name, V9fsPath *target) 12922289be19SAneesh Kumar K.V { 12932289be19SAneesh Kumar K.V if (dir_path) { 1294e3e83f2eSGreg Kurz v9fs_path_sprintf(target, "%s/%s", dir_path->data, name); 12952289be19SAneesh Kumar K.V } else { 1296e3e83f2eSGreg Kurz v9fs_path_sprintf(target, "%s", name); 12972289be19SAneesh Kumar K.V } 12982289be19SAneesh Kumar K.V return 0; 12992289be19SAneesh Kumar K.V } 13002289be19SAneesh Kumar K.V 13012289be19SAneesh Kumar K.V static int local_renameat(FsContext *ctx, V9fsPath *olddir, 13022289be19SAneesh Kumar K.V const char *old_name, V9fsPath *newdir, 13032289be19SAneesh Kumar K.V const char *new_name) 13042289be19SAneesh Kumar K.V { 13052289be19SAneesh Kumar K.V int ret; 130699f2cf4bSGreg Kurz int odirfd, ndirfd; 13072289be19SAneesh Kumar K.V 130899f2cf4bSGreg Kurz odirfd = local_opendir_nofollow(ctx, olddir->data); 130999f2cf4bSGreg Kurz if (odirfd == -1) { 131099f2cf4bSGreg Kurz return -1; 131199f2cf4bSGreg Kurz } 13122289be19SAneesh Kumar K.V 131399f2cf4bSGreg Kurz ndirfd = local_opendir_nofollow(ctx, newdir->data); 131499f2cf4bSGreg Kurz if (ndirfd == -1) { 131599f2cf4bSGreg Kurz close_preserve_errno(odirfd); 131699f2cf4bSGreg Kurz return -1; 131799f2cf4bSGreg Kurz } 13182289be19SAneesh Kumar K.V 131999f2cf4bSGreg Kurz ret = renameat(odirfd, old_name, ndirfd, new_name); 132099f2cf4bSGreg Kurz if (ret < 0) { 132199f2cf4bSGreg Kurz goto out; 132299f2cf4bSGreg Kurz } 132399f2cf4bSGreg Kurz 132499f2cf4bSGreg Kurz if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 132599f2cf4bSGreg Kurz int omap_dirfd, nmap_dirfd; 132699f2cf4bSGreg Kurz 132799f2cf4bSGreg Kurz ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700); 132899f2cf4bSGreg Kurz if (ret < 0 && errno != EEXIST) { 132999f2cf4bSGreg Kurz goto err_undo_rename; 133099f2cf4bSGreg Kurz } 133199f2cf4bSGreg Kurz 13326dd4b1f1SGreg Kurz omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR); 133399f2cf4bSGreg Kurz if (omap_dirfd == -1) { 133499f2cf4bSGreg Kurz goto err; 133599f2cf4bSGreg Kurz } 133699f2cf4bSGreg Kurz 13376dd4b1f1SGreg Kurz nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR); 133899f2cf4bSGreg Kurz if (nmap_dirfd == -1) { 133999f2cf4bSGreg Kurz close_preserve_errno(omap_dirfd); 134099f2cf4bSGreg Kurz goto err; 134199f2cf4bSGreg Kurz } 134299f2cf4bSGreg Kurz 134399f2cf4bSGreg Kurz /* rename the .virtfs_metadata files */ 134499f2cf4bSGreg Kurz ret = renameat(omap_dirfd, old_name, nmap_dirfd, new_name); 134599f2cf4bSGreg Kurz close_preserve_errno(nmap_dirfd); 134699f2cf4bSGreg Kurz close_preserve_errno(omap_dirfd); 134799f2cf4bSGreg Kurz if (ret < 0 && errno != ENOENT) { 134899f2cf4bSGreg Kurz goto err_undo_rename; 134999f2cf4bSGreg Kurz } 135099f2cf4bSGreg Kurz 135199f2cf4bSGreg Kurz ret = 0; 135299f2cf4bSGreg Kurz } 135399f2cf4bSGreg Kurz goto out; 135499f2cf4bSGreg Kurz 135599f2cf4bSGreg Kurz err: 135699f2cf4bSGreg Kurz ret = -1; 135799f2cf4bSGreg Kurz err_undo_rename: 135899f2cf4bSGreg Kurz renameat_preserve_errno(ndirfd, new_name, odirfd, old_name); 135999f2cf4bSGreg Kurz out: 136099f2cf4bSGreg Kurz close_preserve_errno(ndirfd); 136199f2cf4bSGreg Kurz close_preserve_errno(odirfd); 13622289be19SAneesh Kumar K.V return ret; 13632289be19SAneesh Kumar K.V } 13642289be19SAneesh Kumar K.V 1365d2767edeSGreg Kurz static void v9fs_path_init_dirname(V9fsPath *path, const char *str) 1366d2767edeSGreg Kurz { 1367d2767edeSGreg Kurz path->data = g_path_get_dirname(str); 1368d2767edeSGreg Kurz path->size = strlen(path->data) + 1; 1369d2767edeSGreg Kurz } 1370d2767edeSGreg Kurz 1371d2767edeSGreg Kurz static int local_rename(FsContext *ctx, const char *oldpath, 1372d2767edeSGreg Kurz const char *newpath) 1373d2767edeSGreg Kurz { 1374d2767edeSGreg Kurz int err; 1375d2767edeSGreg Kurz char *oname = g_path_get_basename(oldpath); 1376d2767edeSGreg Kurz char *nname = g_path_get_basename(newpath); 1377d2767edeSGreg Kurz V9fsPath olddir, newdir; 1378d2767edeSGreg Kurz 1379d2767edeSGreg Kurz v9fs_path_init_dirname(&olddir, oldpath); 1380d2767edeSGreg Kurz v9fs_path_init_dirname(&newdir, newpath); 1381d2767edeSGreg Kurz 1382d2767edeSGreg Kurz err = local_renameat(ctx, &olddir, oname, &newdir, nname); 1383d2767edeSGreg Kurz 1384d2767edeSGreg Kurz v9fs_path_free(&newdir); 1385d2767edeSGreg Kurz v9fs_path_free(&olddir); 1386d2767edeSGreg Kurz g_free(nname); 1387d2767edeSGreg Kurz g_free(oname); 1388d2767edeSGreg Kurz 1389d2767edeSGreg Kurz return err; 1390d2767edeSGreg Kurz } 1391d2767edeSGreg Kurz 13922289be19SAneesh Kumar K.V static int local_unlinkat(FsContext *ctx, V9fsPath *dir, 13932289be19SAneesh Kumar K.V const char *name, int flags) 13942289be19SAneesh Kumar K.V { 13952289be19SAneesh Kumar K.V int ret; 1396df4938a6SGreg Kurz int dirfd; 13972c30dd74SAneesh Kumar K.V 1398df4938a6SGreg Kurz dirfd = local_opendir_nofollow(ctx, dir->data); 1399df4938a6SGreg Kurz if (dirfd == -1) { 1400df4938a6SGreg Kurz return -1; 1401df4938a6SGreg Kurz } 14022289be19SAneesh Kumar K.V 1403df4938a6SGreg Kurz ret = local_unlinkat_common(ctx, dirfd, name, flags); 1404df4938a6SGreg Kurz close_preserve_errno(dirfd); 14052289be19SAneesh Kumar K.V return ret; 14062289be19SAneesh Kumar K.V } 14079ed3ef26SAneesh Kumar K.V 1408e06a765eSHarsh Prateek Bora static int local_ioc_getversion(FsContext *ctx, V9fsPath *path, 1409e06a765eSHarsh Prateek Bora mode_t st_mode, uint64_t *st_gen) 1410e06a765eSHarsh Prateek Bora { 1411ae0f940eSPaolo Bonzini #ifdef FS_IOC_GETVERSION 14120e5fc994SKirill A. Shutemov int err; 1413cc720ddbSAneesh Kumar K.V V9fsFidOpenState fid_open; 1414cc720ddbSAneesh Kumar K.V 1415e06a765eSHarsh Prateek Bora /* 1416e06a765eSHarsh Prateek Bora * Do not try to open special files like device nodes, fifos etc 1417e06a765eSHarsh Prateek Bora * We can get fd for regular files and directories only 1418e06a765eSHarsh Prateek Bora */ 1419e06a765eSHarsh Prateek Bora if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) { 14201a9978a5SKirill A. Shutemov errno = ENOTTY; 14211a9978a5SKirill A. Shutemov return -1; 1422e06a765eSHarsh Prateek Bora } 1423cc720ddbSAneesh Kumar K.V err = local_open(ctx, path, O_RDONLY, &fid_open); 1424cc720ddbSAneesh Kumar K.V if (err < 0) { 1425cc720ddbSAneesh Kumar K.V return err; 1426e06a765eSHarsh Prateek Bora } 1427cc720ddbSAneesh Kumar K.V err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen); 1428cc720ddbSAneesh Kumar K.V local_close(ctx, &fid_open); 1429e06a765eSHarsh Prateek Bora return err; 14300e5fc994SKirill A. Shutemov #else 14310e5fc994SKirill A. Shutemov errno = ENOTTY; 14320e5fc994SKirill A. Shutemov return -1; 14330e5fc994SKirill A. Shutemov #endif 1434e06a765eSHarsh Prateek Bora } 1435e06a765eSHarsh Prateek Bora 14360174fe73SAneesh Kumar K.V static int local_init(FsContext *ctx) 14370174fe73SAneesh Kumar K.V { 1438e06a765eSHarsh Prateek Bora struct statfs stbuf; 14390e35a378SGreg Kurz LocalData *data = g_malloc(sizeof(*data)); 14400e35a378SGreg Kurz 14410e35a378SGreg Kurz data->mountfd = open(ctx->fs_root, O_DIRECTORY | O_RDONLY); 14420e35a378SGreg Kurz if (data->mountfd == -1) { 14430e35a378SGreg Kurz goto err; 14440e35a378SGreg Kurz } 1445e06a765eSHarsh Prateek Bora 144600c90bd1SGreg Kurz #ifdef FS_IOC_GETVERSION 144700c90bd1SGreg Kurz /* 144800c90bd1SGreg Kurz * use ioc_getversion only if the ioctl is definied 144900c90bd1SGreg Kurz */ 14500e35a378SGreg Kurz if (fstatfs(data->mountfd, &stbuf) < 0) { 14510e35a378SGreg Kurz close_preserve_errno(data->mountfd); 14520e35a378SGreg Kurz goto err; 145300c90bd1SGreg Kurz } 145400c90bd1SGreg Kurz switch (stbuf.f_type) { 145500c90bd1SGreg Kurz case EXT2_SUPER_MAGIC: 145600c90bd1SGreg Kurz case BTRFS_SUPER_MAGIC: 145700c90bd1SGreg Kurz case REISERFS_SUPER_MAGIC: 145800c90bd1SGreg Kurz case XFS_SUPER_MAGIC: 145900c90bd1SGreg Kurz ctx->exops.get_st_gen = local_ioc_getversion; 146000c90bd1SGreg Kurz break; 146100c90bd1SGreg Kurz } 146200c90bd1SGreg Kurz #endif 146300c90bd1SGreg Kurz 14642c30dd74SAneesh Kumar K.V if (ctx->export_flags & V9FS_SM_PASSTHROUGH) { 14652c30dd74SAneesh Kumar K.V ctx->xops = passthrough_xattr_ops; 14662c30dd74SAneesh Kumar K.V } else if (ctx->export_flags & V9FS_SM_MAPPED) { 14672c30dd74SAneesh Kumar K.V ctx->xops = mapped_xattr_ops; 14682c30dd74SAneesh Kumar K.V } else if (ctx->export_flags & V9FS_SM_NONE) { 14692c30dd74SAneesh Kumar K.V ctx->xops = none_xattr_ops; 14702c30dd74SAneesh Kumar K.V } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 14712c30dd74SAneesh Kumar K.V /* 14722c30dd74SAneesh Kumar K.V * xattr operation for mapped-file and passthrough 14732c30dd74SAneesh Kumar K.V * remain same. 14742c30dd74SAneesh Kumar K.V */ 14752c30dd74SAneesh Kumar K.V ctx->xops = passthrough_xattr_ops; 14762c30dd74SAneesh Kumar K.V } 1477c98f1d4aSAneesh Kumar K.V ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT; 147800c90bd1SGreg Kurz 14790e35a378SGreg Kurz ctx->private = data; 148000c90bd1SGreg Kurz return 0; 14810e35a378SGreg Kurz 14820e35a378SGreg Kurz err: 14830e35a378SGreg Kurz g_free(data); 14840e35a378SGreg Kurz return -1; 14850e35a378SGreg Kurz } 14860e35a378SGreg Kurz 14870e35a378SGreg Kurz static void local_cleanup(FsContext *ctx) 14880e35a378SGreg Kurz { 14890e35a378SGreg Kurz LocalData *data = ctx->private; 14900e35a378SGreg Kurz 14910e35a378SGreg Kurz close(data->mountfd); 14920e35a378SGreg Kurz g_free(data); 14930174fe73SAneesh Kumar K.V } 14940174fe73SAneesh Kumar K.V 149599519f0aSAneesh Kumar K.V static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse) 149699519f0aSAneesh Kumar K.V { 149799519f0aSAneesh Kumar K.V const char *sec_model = qemu_opt_get(opts, "security_model"); 149899519f0aSAneesh Kumar K.V const char *path = qemu_opt_get(opts, "path"); 149999519f0aSAneesh Kumar K.V 150099519f0aSAneesh Kumar K.V if (!sec_model) { 150163325b18SGreg Kurz error_report("Security model not specified, local fs needs security model"); 150263325b18SGreg Kurz error_printf("valid options are:" 150363325b18SGreg Kurz "\tsecurity_model=[passthrough|mapped-xattr|mapped-file|none]\n"); 150499519f0aSAneesh Kumar K.V return -1; 150599519f0aSAneesh Kumar K.V } 150699519f0aSAneesh Kumar K.V 150799519f0aSAneesh Kumar K.V if (!strcmp(sec_model, "passthrough")) { 150899519f0aSAneesh Kumar K.V fse->export_flags |= V9FS_SM_PASSTHROUGH; 15092c30dd74SAneesh Kumar K.V } else if (!strcmp(sec_model, "mapped") || 15102c30dd74SAneesh Kumar K.V !strcmp(sec_model, "mapped-xattr")) { 151199519f0aSAneesh Kumar K.V fse->export_flags |= V9FS_SM_MAPPED; 151299519f0aSAneesh Kumar K.V } else if (!strcmp(sec_model, "none")) { 151399519f0aSAneesh Kumar K.V fse->export_flags |= V9FS_SM_NONE; 15142c30dd74SAneesh Kumar K.V } else if (!strcmp(sec_model, "mapped-file")) { 15152c30dd74SAneesh Kumar K.V fse->export_flags |= V9FS_SM_MAPPED_FILE; 151699519f0aSAneesh Kumar K.V } else { 151763325b18SGreg Kurz error_report("Invalid security model %s specified", sec_model); 151863325b18SGreg Kurz error_printf("valid options are:" 151963325b18SGreg Kurz "\t[passthrough|mapped-xattr|mapped-file|none]\n"); 152099519f0aSAneesh Kumar K.V return -1; 152199519f0aSAneesh Kumar K.V } 152299519f0aSAneesh Kumar K.V 152399519f0aSAneesh Kumar K.V if (!path) { 152463325b18SGreg Kurz error_report("fsdev: No path specified"); 152599519f0aSAneesh Kumar K.V return -1; 152699519f0aSAneesh Kumar K.V } 152799519f0aSAneesh Kumar K.V fse->path = g_strdup(path); 152899519f0aSAneesh Kumar K.V 152999519f0aSAneesh Kumar K.V return 0; 153099519f0aSAneesh Kumar K.V } 153199519f0aSAneesh Kumar K.V 15329f107513SAnthony Liguori FileOperations local_ops = { 153399519f0aSAneesh Kumar K.V .parse_opts = local_parse_opts, 15340174fe73SAneesh Kumar K.V .init = local_init, 15350e35a378SGreg Kurz .cleanup = local_cleanup, 1536131dcb25SAnthony Liguori .lstat = local_lstat, 1537131dcb25SAnthony Liguori .readlink = local_readlink, 1538131dcb25SAnthony Liguori .close = local_close, 1539131dcb25SAnthony Liguori .closedir = local_closedir, 1540a6568fe2SAnthony Liguori .open = local_open, 1541a6568fe2SAnthony Liguori .opendir = local_opendir, 1542a9231555SAnthony Liguori .rewinddir = local_rewinddir, 1543a9231555SAnthony Liguori .telldir = local_telldir, 1544635324e8SGreg Kurz .readdir = local_readdir, 1545a9231555SAnthony Liguori .seekdir = local_seekdir, 154656d15a53SSanchit Garg .preadv = local_preadv, 154756d15a53SSanchit Garg .pwritev = local_pwritev, 1548c494dd6fSAnthony Liguori .chmod = local_chmod, 1549c494dd6fSAnthony Liguori .mknod = local_mknod, 1550c494dd6fSAnthony Liguori .mkdir = local_mkdir, 1551c494dd6fSAnthony Liguori .fstat = local_fstat, 1552c494dd6fSAnthony Liguori .open2 = local_open2, 1553c494dd6fSAnthony Liguori .symlink = local_symlink, 1554c494dd6fSAnthony Liguori .link = local_link, 15558cf89e00SAnthony Liguori .truncate = local_truncate, 15568cf89e00SAnthony Liguori .rename = local_rename, 15578cf89e00SAnthony Liguori .chown = local_chown, 155874bc02b2SM. Mohan Kumar .utimensat = local_utimensat, 15595bae1900SAnthony Liguori .remove = local_remove, 15608cf89e00SAnthony Liguori .fsync = local_fsync, 1561be940c87SM. Mohan Kumar .statfs = local_statfs, 1562fa32ef88SAneesh Kumar K.V .lgetxattr = local_lgetxattr, 1563fa32ef88SAneesh Kumar K.V .llistxattr = local_llistxattr, 156410b468bdSAneesh Kumar K.V .lsetxattr = local_lsetxattr, 15659ed3ef26SAneesh Kumar K.V .lremovexattr = local_lremovexattr, 15662289be19SAneesh Kumar K.V .name_to_path = local_name_to_path, 15672289be19SAneesh Kumar K.V .renameat = local_renameat, 15682289be19SAneesh Kumar K.V .unlinkat = local_unlinkat, 15699f107513SAnthony Liguori }; 1570