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) 5462289be19SAneesh Kumar K.V static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path, 547131dcb25SAnthony Liguori char *buf, size_t bufsz) 548131dcb25SAnthony Liguori { 549879c2813SVenkateswararao Jujjuri (JV) ssize_t tsize = -1; 5502289be19SAneesh Kumar K.V 5512c30dd74SAneesh Kumar K.V if ((fs_ctx->export_flags & V9FS_SM_MAPPED) || 5522c30dd74SAneesh Kumar K.V (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) { 553879c2813SVenkateswararao Jujjuri (JV) int fd; 554bec1e954SGreg Kurz 555bec1e954SGreg Kurz fd = local_open_nofollow(fs_ctx, fs_path->data, O_RDONLY, 0); 556879c2813SVenkateswararao Jujjuri (JV) if (fd == -1) { 557879c2813SVenkateswararao Jujjuri (JV) return -1; 558879c2813SVenkateswararao Jujjuri (JV) } 559879c2813SVenkateswararao Jujjuri (JV) do { 560879c2813SVenkateswararao Jujjuri (JV) tsize = read(fd, (void *)buf, bufsz); 561879c2813SVenkateswararao Jujjuri (JV) } while (tsize == -1 && errno == EINTR); 562bec1e954SGreg Kurz close_preserve_errno(fd); 563b97400caSAneesh Kumar K.V } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 564b97400caSAneesh Kumar K.V (fs_ctx->export_flags & V9FS_SM_NONE)) { 565bec1e954SGreg Kurz char *dirpath = g_path_get_dirname(fs_path->data); 566bec1e954SGreg Kurz char *name = g_path_get_basename(fs_path->data); 567bec1e954SGreg Kurz int dirfd; 568bec1e954SGreg Kurz 569bec1e954SGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dirpath); 570bec1e954SGreg Kurz if (dirfd == -1) { 571bec1e954SGreg Kurz goto out; 572bec1e954SGreg Kurz } 573bec1e954SGreg Kurz 574bec1e954SGreg Kurz tsize = readlinkat(dirfd, name, buf, bufsz); 575bec1e954SGreg Kurz close_preserve_errno(dirfd); 576bec1e954SGreg Kurz out: 577bec1e954SGreg Kurz g_free(name); 578bec1e954SGreg Kurz g_free(dirpath); 579879c2813SVenkateswararao Jujjuri (JV) } 580879c2813SVenkateswararao Jujjuri (JV) return tsize; 581131dcb25SAnthony Liguori } 582131dcb25SAnthony Liguori 583cc720ddbSAneesh Kumar K.V static int local_close(FsContext *ctx, V9fsFidOpenState *fs) 584131dcb25SAnthony Liguori { 585cc720ddbSAneesh Kumar K.V return close(fs->fd); 586131dcb25SAnthony Liguori } 587131dcb25SAnthony Liguori 588cc720ddbSAneesh Kumar K.V static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs) 589131dcb25SAnthony Liguori { 590f314ea4eSGreg Kurz return closedir(fs->dir.stream); 591131dcb25SAnthony Liguori } 5929f107513SAnthony Liguori 593cc720ddbSAneesh Kumar K.V static int local_open(FsContext *ctx, V9fsPath *fs_path, 594cc720ddbSAneesh Kumar K.V int flags, V9fsFidOpenState *fs) 595a6568fe2SAnthony Liguori { 59621328e1eSGreg Kurz int fd; 5972289be19SAneesh Kumar K.V 598996a0d76SGreg Kurz fd = local_open_nofollow(ctx, fs_path->data, flags, 0); 59921328e1eSGreg Kurz if (fd == -1) { 60021328e1eSGreg Kurz return -1; 60121328e1eSGreg Kurz } 60221328e1eSGreg Kurz fs->fd = fd; 603cc720ddbSAneesh Kumar K.V return fs->fd; 604a6568fe2SAnthony Liguori } 605a6568fe2SAnthony Liguori 606cc720ddbSAneesh Kumar K.V static int local_opendir(FsContext *ctx, 607cc720ddbSAneesh Kumar K.V V9fsPath *fs_path, V9fsFidOpenState *fs) 608a6568fe2SAnthony Liguori { 609996a0d76SGreg Kurz int dirfd; 61021328e1eSGreg Kurz DIR *stream; 6112289be19SAneesh Kumar K.V 612996a0d76SGreg Kurz dirfd = local_opendir_nofollow(ctx, fs_path->data); 613996a0d76SGreg Kurz if (dirfd == -1) { 614996a0d76SGreg Kurz return -1; 615996a0d76SGreg Kurz } 616996a0d76SGreg Kurz 617996a0d76SGreg Kurz stream = fdopendir(dirfd); 61821328e1eSGreg Kurz if (!stream) { 619cc720ddbSAneesh Kumar K.V return -1; 620cc720ddbSAneesh Kumar K.V } 62121328e1eSGreg Kurz fs->dir.stream = stream; 622cc720ddbSAneesh Kumar K.V return 0; 623a6568fe2SAnthony Liguori } 624a6568fe2SAnthony Liguori 625cc720ddbSAneesh Kumar K.V static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs) 626a9231555SAnthony Liguori { 627f314ea4eSGreg Kurz rewinddir(fs->dir.stream); 628a9231555SAnthony Liguori } 629a9231555SAnthony Liguori 630cc720ddbSAneesh Kumar K.V static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs) 631a9231555SAnthony Liguori { 632f314ea4eSGreg Kurz return telldir(fs->dir.stream); 633a9231555SAnthony Liguori } 634a9231555SAnthony Liguori 635635324e8SGreg Kurz static struct dirent *local_readdir(FsContext *ctx, V9fsFidOpenState *fs) 636a9231555SAnthony Liguori { 637635324e8SGreg Kurz struct dirent *entry; 6382c30dd74SAneesh Kumar K.V 6392c30dd74SAneesh Kumar K.V again: 640635324e8SGreg Kurz entry = readdir(fs->dir.stream); 641635324e8SGreg Kurz if (!entry) { 642635324e8SGreg Kurz return NULL; 643635324e8SGreg Kurz } 644635324e8SGreg Kurz 645840a1bf2SBastian Blank if (ctx->export_flags & V9FS_SM_MAPPED) { 646840a1bf2SBastian Blank entry->d_type = DT_UNKNOWN; 647840a1bf2SBastian Blank } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 648635324e8SGreg Kurz if (!strcmp(entry->d_name, VIRTFS_META_DIR)) { 6492c30dd74SAneesh Kumar K.V /* skp the meta data directory */ 6502c30dd74SAneesh Kumar K.V goto again; 6512c30dd74SAneesh Kumar K.V } 652840a1bf2SBastian Blank entry->d_type = DT_UNKNOWN; 6532c30dd74SAneesh Kumar K.V } 654635324e8SGreg Kurz 655635324e8SGreg Kurz return entry; 656a9231555SAnthony Liguori } 657a9231555SAnthony Liguori 658cc720ddbSAneesh Kumar K.V static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off) 659a9231555SAnthony Liguori { 660f314ea4eSGreg Kurz seekdir(fs->dir.stream, off); 661a9231555SAnthony Liguori } 662a9231555SAnthony Liguori 663cc720ddbSAneesh Kumar K.V static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs, 664cc720ddbSAneesh Kumar K.V const struct iovec *iov, 66556d15a53SSanchit Garg int iovcnt, off_t offset) 666a9231555SAnthony Liguori { 66756d15a53SSanchit Garg #ifdef CONFIG_PREADV 668cc720ddbSAneesh Kumar K.V return preadv(fs->fd, iov, iovcnt, offset); 66956d15a53SSanchit Garg #else 670cc720ddbSAneesh Kumar K.V int err = lseek(fs->fd, offset, SEEK_SET); 67156d15a53SSanchit Garg if (err == -1) { 67256d15a53SSanchit Garg return err; 67356d15a53SSanchit Garg } else { 674cc720ddbSAneesh Kumar K.V return readv(fs->fd, iov, iovcnt); 675a9231555SAnthony Liguori } 67656d15a53SSanchit Garg #endif 677a9231555SAnthony Liguori } 678a9231555SAnthony Liguori 679cc720ddbSAneesh Kumar K.V static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs, 680cc720ddbSAneesh Kumar K.V const struct iovec *iov, 68156d15a53SSanchit Garg int iovcnt, off_t offset) 6828449360cSAnthony Liguori { 6836fe76accSGreg Kurz ssize_t ret; 68456d15a53SSanchit Garg #ifdef CONFIG_PREADV 685cc720ddbSAneesh Kumar K.V ret = pwritev(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 ret = writev(fs->fd, iov, iovcnt); 6928449360cSAnthony Liguori } 69356d15a53SSanchit Garg #endif 694d3ab98e6SAneesh Kumar K.V #ifdef CONFIG_SYNC_FILE_RANGE 695d3ab98e6SAneesh Kumar K.V if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) { 696d3ab98e6SAneesh Kumar K.V /* 697d3ab98e6SAneesh Kumar K.V * Initiate a writeback. This is not a data integrity sync. 698d3ab98e6SAneesh Kumar K.V * We want to ensure that we don't leave dirty pages in the cache 699d3ab98e6SAneesh Kumar K.V * after write when writeout=immediate is sepcified. 700d3ab98e6SAneesh Kumar K.V */ 701cc720ddbSAneesh Kumar K.V sync_file_range(fs->fd, offset, ret, 702d3ab98e6SAneesh Kumar K.V SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE); 703d3ab98e6SAneesh Kumar K.V } 704d3ab98e6SAneesh Kumar K.V #endif 705d3ab98e6SAneesh Kumar K.V return ret; 70656d15a53SSanchit Garg } 7078449360cSAnthony Liguori 7082289be19SAneesh Kumar K.V static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp) 709c494dd6fSAnthony Liguori { 710e3187a45SGreg Kurz char *dirpath = g_path_get_dirname(fs_path->data); 711e3187a45SGreg Kurz char *name = g_path_get_basename(fs_path->data); 7124fa4ce71SChen Gang int ret = -1; 713e3187a45SGreg Kurz int dirfd; 714e3187a45SGreg Kurz 715e3187a45SGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dirpath); 716e3187a45SGreg Kurz if (dirfd == -1) { 717e3187a45SGreg Kurz goto out; 718e3187a45SGreg Kurz } 7192289be19SAneesh Kumar K.V 720b97400caSAneesh Kumar K.V if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 721e3187a45SGreg Kurz ret = local_set_xattrat(dirfd, name, credp); 7222c30dd74SAneesh Kumar K.V } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 723e3187a45SGreg Kurz ret = local_set_mapped_file_attrat(dirfd, name, credp); 724e3187a45SGreg Kurz } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH || 725e3187a45SGreg Kurz fs_ctx->export_flags & V9FS_SM_NONE) { 726e3187a45SGreg Kurz ret = fchmodat_nofollow(dirfd, name, credp->fc_mode); 727e95ead32SVenkateswararao Jujjuri (JV) } 728e3187a45SGreg Kurz close_preserve_errno(dirfd); 729e3187a45SGreg Kurz 730e3187a45SGreg Kurz out: 731e3187a45SGreg Kurz g_free(dirpath); 732e3187a45SGreg Kurz g_free(name); 7334fa4ce71SChen Gang return ret; 734c494dd6fSAnthony Liguori } 735c494dd6fSAnthony Liguori 7362289be19SAneesh Kumar K.V static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path, 7372289be19SAneesh Kumar K.V const char *name, FsCred *credp) 738c494dd6fSAnthony Liguori { 7392289be19SAneesh Kumar K.V char *path; 7401c293312SVenkateswararao Jujjuri (JV) int err = -1; 7411c293312SVenkateswararao Jujjuri (JV) int serrno = 0; 7422289be19SAneesh Kumar K.V V9fsString fullname; 7434ed7b2c3SStefan Weil char *buffer = NULL; 7441c293312SVenkateswararao Jujjuri (JV) 7452289be19SAneesh Kumar K.V v9fs_string_init(&fullname); 7462289be19SAneesh Kumar K.V v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name); 7472289be19SAneesh Kumar K.V path = fullname.data; 7482289be19SAneesh Kumar K.V 7491c293312SVenkateswararao Jujjuri (JV) /* Determine the security model */ 750b97400caSAneesh Kumar K.V if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 7514fa4ce71SChen Gang buffer = rpath(fs_ctx, path); 7524fa4ce71SChen Gang err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0); 7531c293312SVenkateswararao Jujjuri (JV) if (err == -1) { 7542289be19SAneesh Kumar K.V goto out; 7551c293312SVenkateswararao Jujjuri (JV) } 7564fa4ce71SChen Gang err = local_set_xattr(buffer, credp); 7571c293312SVenkateswararao Jujjuri (JV) if (err == -1) { 7581c293312SVenkateswararao Jujjuri (JV) serrno = errno; 7591c293312SVenkateswararao Jujjuri (JV) goto err_end; 7601c293312SVenkateswararao Jujjuri (JV) } 7612c30dd74SAneesh Kumar K.V } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 7622c30dd74SAneesh Kumar K.V 7634fa4ce71SChen Gang buffer = rpath(fs_ctx, path); 7644fa4ce71SChen Gang err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0); 7652c30dd74SAneesh Kumar K.V if (err == -1) { 7662c30dd74SAneesh Kumar K.V goto out; 7672c30dd74SAneesh Kumar K.V } 7682c30dd74SAneesh Kumar K.V err = local_set_mapped_file_attr(fs_ctx, path, credp); 7692c30dd74SAneesh Kumar K.V if (err == -1) { 7702c30dd74SAneesh Kumar K.V serrno = errno; 7712c30dd74SAneesh Kumar K.V goto err_end; 7722c30dd74SAneesh Kumar K.V } 773b97400caSAneesh Kumar K.V } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 774b97400caSAneesh Kumar K.V (fs_ctx->export_flags & V9FS_SM_NONE)) { 7754fa4ce71SChen Gang buffer = rpath(fs_ctx, path); 7764fa4ce71SChen Gang err = mknod(buffer, credp->fc_mode, credp->fc_rdev); 7771c293312SVenkateswararao Jujjuri (JV) if (err == -1) { 7782289be19SAneesh Kumar K.V goto out; 7791c293312SVenkateswararao Jujjuri (JV) } 7801c293312SVenkateswararao Jujjuri (JV) err = local_post_create_passthrough(fs_ctx, path, credp); 7811c293312SVenkateswararao Jujjuri (JV) if (err == -1) { 7821c293312SVenkateswararao Jujjuri (JV) serrno = errno; 7831c293312SVenkateswararao Jujjuri (JV) goto err_end; 7841c293312SVenkateswararao Jujjuri (JV) } 7851c293312SVenkateswararao Jujjuri (JV) } 7862289be19SAneesh Kumar K.V goto out; 7871c293312SVenkateswararao Jujjuri (JV) 7881c293312SVenkateswararao Jujjuri (JV) err_end: 7894fa4ce71SChen Gang remove(buffer); 7901c293312SVenkateswararao Jujjuri (JV) errno = serrno; 7912289be19SAneesh Kumar K.V out: 7924ed7b2c3SStefan Weil g_free(buffer); 7932289be19SAneesh Kumar K.V v9fs_string_free(&fullname); 7941c293312SVenkateswararao Jujjuri (JV) return err; 795c494dd6fSAnthony Liguori } 796c494dd6fSAnthony Liguori 7972289be19SAneesh Kumar K.V static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path, 7982289be19SAneesh Kumar K.V const char *name, FsCred *credp) 799c494dd6fSAnthony Liguori { 8002289be19SAneesh Kumar K.V char *path; 80100ec5c37SVenkateswararao Jujjuri (JV) int err = -1; 80200ec5c37SVenkateswararao Jujjuri (JV) int serrno = 0; 8032289be19SAneesh Kumar K.V V9fsString fullname; 8044ed7b2c3SStefan Weil char *buffer = NULL; 80500ec5c37SVenkateswararao Jujjuri (JV) 8062289be19SAneesh Kumar K.V v9fs_string_init(&fullname); 8072289be19SAneesh Kumar K.V v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name); 8082289be19SAneesh Kumar K.V path = fullname.data; 8092289be19SAneesh Kumar K.V 81000ec5c37SVenkateswararao Jujjuri (JV) /* Determine the security model */ 811b97400caSAneesh Kumar K.V if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 8124fa4ce71SChen Gang buffer = rpath(fs_ctx, path); 8134fa4ce71SChen Gang err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS); 81400ec5c37SVenkateswararao Jujjuri (JV) if (err == -1) { 8152289be19SAneesh Kumar K.V goto out; 81600ec5c37SVenkateswararao Jujjuri (JV) } 81700ec5c37SVenkateswararao Jujjuri (JV) credp->fc_mode = credp->fc_mode|S_IFDIR; 8184fa4ce71SChen Gang err = local_set_xattr(buffer, credp); 81900ec5c37SVenkateswararao Jujjuri (JV) if (err == -1) { 82000ec5c37SVenkateswararao Jujjuri (JV) serrno = errno; 82100ec5c37SVenkateswararao Jujjuri (JV) goto err_end; 82200ec5c37SVenkateswararao Jujjuri (JV) } 8232c30dd74SAneesh Kumar K.V } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 8244fa4ce71SChen Gang buffer = rpath(fs_ctx, path); 8254fa4ce71SChen Gang err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS); 8262c30dd74SAneesh Kumar K.V if (err == -1) { 8272c30dd74SAneesh Kumar K.V goto out; 8282c30dd74SAneesh Kumar K.V } 8292c30dd74SAneesh Kumar K.V credp->fc_mode = credp->fc_mode|S_IFDIR; 8302c30dd74SAneesh Kumar K.V err = local_set_mapped_file_attr(fs_ctx, path, credp); 8312c30dd74SAneesh Kumar K.V if (err == -1) { 8322c30dd74SAneesh Kumar K.V serrno = errno; 8332c30dd74SAneesh Kumar K.V goto err_end; 8342c30dd74SAneesh Kumar K.V } 835b97400caSAneesh Kumar K.V } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 836b97400caSAneesh Kumar K.V (fs_ctx->export_flags & V9FS_SM_NONE)) { 8374fa4ce71SChen Gang buffer = rpath(fs_ctx, path); 8384fa4ce71SChen Gang err = mkdir(buffer, credp->fc_mode); 83900ec5c37SVenkateswararao Jujjuri (JV) if (err == -1) { 8402289be19SAneesh Kumar K.V goto out; 84100ec5c37SVenkateswararao Jujjuri (JV) } 84200ec5c37SVenkateswararao Jujjuri (JV) err = local_post_create_passthrough(fs_ctx, path, credp); 84300ec5c37SVenkateswararao Jujjuri (JV) if (err == -1) { 84400ec5c37SVenkateswararao Jujjuri (JV) serrno = errno; 84500ec5c37SVenkateswararao Jujjuri (JV) goto err_end; 84600ec5c37SVenkateswararao Jujjuri (JV) } 84700ec5c37SVenkateswararao Jujjuri (JV) } 8482289be19SAneesh Kumar K.V goto out; 84900ec5c37SVenkateswararao Jujjuri (JV) 85000ec5c37SVenkateswararao Jujjuri (JV) err_end: 8514fa4ce71SChen Gang remove(buffer); 85200ec5c37SVenkateswararao Jujjuri (JV) errno = serrno; 8532289be19SAneesh Kumar K.V out: 8544ed7b2c3SStefan Weil g_free(buffer); 8552289be19SAneesh Kumar K.V v9fs_string_free(&fullname); 85600ec5c37SVenkateswararao Jujjuri (JV) return err; 857c494dd6fSAnthony Liguori } 858c494dd6fSAnthony Liguori 8598b888272SAneesh Kumar K.V static int local_fstat(FsContext *fs_ctx, int fid_type, 860cc720ddbSAneesh Kumar K.V V9fsFidOpenState *fs, struct stat *stbuf) 861c494dd6fSAnthony Liguori { 8628b888272SAneesh Kumar K.V int err, fd; 8638b888272SAneesh Kumar K.V 8648b888272SAneesh Kumar K.V if (fid_type == P9_FID_DIR) { 865f314ea4eSGreg Kurz fd = dirfd(fs->dir.stream); 8668b888272SAneesh Kumar K.V } else { 8678b888272SAneesh Kumar K.V fd = fs->fd; 8688b888272SAneesh Kumar K.V } 8698b888272SAneesh Kumar K.V 8708b888272SAneesh Kumar K.V err = fstat(fd, stbuf); 8711237ad76SVenkateswararao Jujjuri (JV) if (err) { 8721237ad76SVenkateswararao Jujjuri (JV) return err; 8731237ad76SVenkateswararao Jujjuri (JV) } 874b97400caSAneesh Kumar K.V if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 8751237ad76SVenkateswararao Jujjuri (JV) /* Actual credentials are part of extended attrs */ 8761237ad76SVenkateswararao Jujjuri (JV) uid_t tmp_uid; 8771237ad76SVenkateswararao Jujjuri (JV) gid_t tmp_gid; 8781237ad76SVenkateswararao Jujjuri (JV) mode_t tmp_mode; 8791237ad76SVenkateswararao Jujjuri (JV) dev_t tmp_dev; 8801237ad76SVenkateswararao Jujjuri (JV) 881f8ad4a89SAneesh Kumar K.V if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) { 882f8ad4a89SAneesh Kumar K.V stbuf->st_uid = le32_to_cpu(tmp_uid); 8831237ad76SVenkateswararao Jujjuri (JV) } 884f8ad4a89SAneesh Kumar K.V if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) { 885f8ad4a89SAneesh Kumar K.V stbuf->st_gid = le32_to_cpu(tmp_gid); 8861237ad76SVenkateswararao Jujjuri (JV) } 887f8ad4a89SAneesh Kumar K.V if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) { 888f8ad4a89SAneesh Kumar K.V stbuf->st_mode = le32_to_cpu(tmp_mode); 8891237ad76SVenkateswararao Jujjuri (JV) } 890f8ad4a89SAneesh Kumar K.V if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) { 891f8ad4a89SAneesh Kumar K.V stbuf->st_rdev = le64_to_cpu(tmp_dev); 8921237ad76SVenkateswararao Jujjuri (JV) } 8932c30dd74SAneesh Kumar K.V } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 8942c30dd74SAneesh Kumar K.V errno = EOPNOTSUPP; 8952c30dd74SAneesh Kumar K.V return -1; 8961237ad76SVenkateswararao Jujjuri (JV) } 8971237ad76SVenkateswararao Jujjuri (JV) return err; 898c494dd6fSAnthony Liguori } 899c494dd6fSAnthony Liguori 9002289be19SAneesh Kumar K.V static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name, 901cc720ddbSAneesh Kumar K.V int flags, FsCred *credp, V9fsFidOpenState *fs) 902c494dd6fSAnthony Liguori { 9032289be19SAneesh Kumar K.V char *path; 9044750a96fSVenkateswararao Jujjuri (JV) int fd = -1; 9054750a96fSVenkateswararao Jujjuri (JV) int err = -1; 9064750a96fSVenkateswararao Jujjuri (JV) int serrno = 0; 9072289be19SAneesh Kumar K.V V9fsString fullname; 9084ed7b2c3SStefan Weil char *buffer = NULL; 9094750a96fSVenkateswararao Jujjuri (JV) 9100ceb092eSAneesh Kumar K.V /* 9110ceb092eSAneesh Kumar K.V * Mark all the open to not follow symlinks 9120ceb092eSAneesh Kumar K.V */ 9130ceb092eSAneesh Kumar K.V flags |= O_NOFOLLOW; 9140ceb092eSAneesh Kumar K.V 9152289be19SAneesh Kumar K.V v9fs_string_init(&fullname); 9162289be19SAneesh Kumar K.V v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name); 9172289be19SAneesh Kumar K.V path = fullname.data; 9182289be19SAneesh Kumar K.V 9194750a96fSVenkateswararao Jujjuri (JV) /* Determine the security model */ 920b97400caSAneesh Kumar K.V if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 9214fa4ce71SChen Gang buffer = rpath(fs_ctx, path); 9224fa4ce71SChen Gang fd = open(buffer, flags, SM_LOCAL_MODE_BITS); 9234750a96fSVenkateswararao Jujjuri (JV) if (fd == -1) { 9242289be19SAneesh Kumar K.V err = fd; 9252289be19SAneesh Kumar K.V goto out; 9264750a96fSVenkateswararao Jujjuri (JV) } 9274750a96fSVenkateswararao Jujjuri (JV) credp->fc_mode = credp->fc_mode|S_IFREG; 9284750a96fSVenkateswararao Jujjuri (JV) /* Set cleint credentials in xattr */ 9294fa4ce71SChen Gang err = local_set_xattr(buffer, credp); 9304750a96fSVenkateswararao Jujjuri (JV) if (err == -1) { 9314750a96fSVenkateswararao Jujjuri (JV) serrno = errno; 9324750a96fSVenkateswararao Jujjuri (JV) goto err_end; 9334750a96fSVenkateswararao Jujjuri (JV) } 9342c30dd74SAneesh Kumar K.V } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 9354fa4ce71SChen Gang buffer = rpath(fs_ctx, path); 9364fa4ce71SChen Gang fd = open(buffer, flags, SM_LOCAL_MODE_BITS); 9372c30dd74SAneesh Kumar K.V if (fd == -1) { 9382c30dd74SAneesh Kumar K.V err = fd; 9392c30dd74SAneesh Kumar K.V goto out; 9402c30dd74SAneesh Kumar K.V } 9412c30dd74SAneesh Kumar K.V credp->fc_mode = credp->fc_mode|S_IFREG; 9422c30dd74SAneesh Kumar K.V /* Set client credentials in .virtfs_metadata directory files */ 9432c30dd74SAneesh Kumar K.V err = local_set_mapped_file_attr(fs_ctx, path, credp); 9442c30dd74SAneesh Kumar K.V if (err == -1) { 9452c30dd74SAneesh Kumar K.V serrno = errno; 9462c30dd74SAneesh Kumar K.V goto err_end; 9472c30dd74SAneesh Kumar K.V } 948b97400caSAneesh Kumar K.V } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 949b97400caSAneesh Kumar K.V (fs_ctx->export_flags & V9FS_SM_NONE)) { 9504fa4ce71SChen Gang buffer = rpath(fs_ctx, path); 9514fa4ce71SChen Gang fd = open(buffer, flags, credp->fc_mode); 9524750a96fSVenkateswararao Jujjuri (JV) if (fd == -1) { 9532289be19SAneesh Kumar K.V err = fd; 9542289be19SAneesh Kumar K.V goto out; 9554750a96fSVenkateswararao Jujjuri (JV) } 9564750a96fSVenkateswararao Jujjuri (JV) err = local_post_create_passthrough(fs_ctx, path, credp); 9574750a96fSVenkateswararao Jujjuri (JV) if (err == -1) { 9584750a96fSVenkateswararao Jujjuri (JV) serrno = errno; 9594750a96fSVenkateswararao Jujjuri (JV) goto err_end; 9604750a96fSVenkateswararao Jujjuri (JV) } 9614750a96fSVenkateswararao Jujjuri (JV) } 9622289be19SAneesh Kumar K.V err = fd; 963cc720ddbSAneesh Kumar K.V fs->fd = fd; 9642289be19SAneesh Kumar K.V goto out; 9654750a96fSVenkateswararao Jujjuri (JV) 9664750a96fSVenkateswararao Jujjuri (JV) err_end: 9674750a96fSVenkateswararao Jujjuri (JV) close(fd); 9684fa4ce71SChen Gang remove(buffer); 9694750a96fSVenkateswararao Jujjuri (JV) errno = serrno; 9702289be19SAneesh Kumar K.V out: 9714ed7b2c3SStefan Weil g_free(buffer); 9722289be19SAneesh Kumar K.V v9fs_string_free(&fullname); 9734750a96fSVenkateswararao Jujjuri (JV) return err; 974c494dd6fSAnthony Liguori } 975c494dd6fSAnthony Liguori 976758e8e38SVenkateswararao Jujjuri (JV) 977879c2813SVenkateswararao Jujjuri (JV) static int local_symlink(FsContext *fs_ctx, const char *oldpath, 9782289be19SAneesh Kumar K.V V9fsPath *dir_path, const char *name, FsCred *credp) 979c494dd6fSAnthony Liguori { 980879c2813SVenkateswararao Jujjuri (JV) int err = -1; 981879c2813SVenkateswararao Jujjuri (JV) int serrno = 0; 9822289be19SAneesh Kumar K.V char *newpath; 9832289be19SAneesh Kumar K.V V9fsString fullname; 9844ed7b2c3SStefan Weil char *buffer = NULL; 985879c2813SVenkateswararao Jujjuri (JV) 9862289be19SAneesh Kumar K.V v9fs_string_init(&fullname); 9872289be19SAneesh Kumar K.V v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name); 9882289be19SAneesh Kumar K.V newpath = fullname.data; 9892289be19SAneesh Kumar K.V 990879c2813SVenkateswararao Jujjuri (JV) /* Determine the security model */ 991b97400caSAneesh Kumar K.V if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 992879c2813SVenkateswararao Jujjuri (JV) int fd; 993879c2813SVenkateswararao Jujjuri (JV) ssize_t oldpath_size, write_size; 9944fa4ce71SChen Gang buffer = rpath(fs_ctx, newpath); 9954fa4ce71SChen Gang fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS); 996879c2813SVenkateswararao Jujjuri (JV) if (fd == -1) { 9972289be19SAneesh Kumar K.V err = fd; 9982289be19SAneesh Kumar K.V goto out; 999879c2813SVenkateswararao Jujjuri (JV) } 1000879c2813SVenkateswararao Jujjuri (JV) /* Write the oldpath (target) to the file. */ 1001f35bde2fSHarsh Prateek Bora oldpath_size = strlen(oldpath); 1002879c2813SVenkateswararao Jujjuri (JV) do { 1003879c2813SVenkateswararao Jujjuri (JV) write_size = write(fd, (void *)oldpath, oldpath_size); 1004879c2813SVenkateswararao Jujjuri (JV) } while (write_size == -1 && errno == EINTR); 1005879c2813SVenkateswararao Jujjuri (JV) 1006879c2813SVenkateswararao Jujjuri (JV) if (write_size != oldpath_size) { 1007879c2813SVenkateswararao Jujjuri (JV) serrno = errno; 1008879c2813SVenkateswararao Jujjuri (JV) close(fd); 1009879c2813SVenkateswararao Jujjuri (JV) err = -1; 1010879c2813SVenkateswararao Jujjuri (JV) goto err_end; 1011879c2813SVenkateswararao Jujjuri (JV) } 1012879c2813SVenkateswararao Jujjuri (JV) close(fd); 1013879c2813SVenkateswararao Jujjuri (JV) /* Set cleint credentials in symlink's xattr */ 1014879c2813SVenkateswararao Jujjuri (JV) credp->fc_mode = credp->fc_mode|S_IFLNK; 10154fa4ce71SChen Gang err = local_set_xattr(buffer, credp); 1016879c2813SVenkateswararao Jujjuri (JV) if (err == -1) { 1017879c2813SVenkateswararao Jujjuri (JV) serrno = errno; 1018879c2813SVenkateswararao Jujjuri (JV) goto err_end; 1019879c2813SVenkateswararao Jujjuri (JV) } 10202c30dd74SAneesh Kumar K.V } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 10212c30dd74SAneesh Kumar K.V int fd; 10222c30dd74SAneesh Kumar K.V ssize_t oldpath_size, write_size; 10234fa4ce71SChen Gang buffer = rpath(fs_ctx, newpath); 10244fa4ce71SChen Gang fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS); 10252c30dd74SAneesh Kumar K.V if (fd == -1) { 10262c30dd74SAneesh Kumar K.V err = fd; 10272c30dd74SAneesh Kumar K.V goto out; 10282c30dd74SAneesh Kumar K.V } 10292c30dd74SAneesh Kumar K.V /* Write the oldpath (target) to the file. */ 10302c30dd74SAneesh Kumar K.V oldpath_size = strlen(oldpath); 10312c30dd74SAneesh Kumar K.V do { 10322c30dd74SAneesh Kumar K.V write_size = write(fd, (void *)oldpath, oldpath_size); 10332c30dd74SAneesh Kumar K.V } while (write_size == -1 && errno == EINTR); 10342c30dd74SAneesh Kumar K.V 10352c30dd74SAneesh Kumar K.V if (write_size != oldpath_size) { 10362c30dd74SAneesh Kumar K.V serrno = errno; 10372c30dd74SAneesh Kumar K.V close(fd); 10382c30dd74SAneesh Kumar K.V err = -1; 10392c30dd74SAneesh Kumar K.V goto err_end; 10402c30dd74SAneesh Kumar K.V } 10412c30dd74SAneesh Kumar K.V close(fd); 10422c30dd74SAneesh Kumar K.V /* Set cleint credentials in symlink's xattr */ 10432c30dd74SAneesh Kumar K.V credp->fc_mode = credp->fc_mode|S_IFLNK; 10442c30dd74SAneesh Kumar K.V err = local_set_mapped_file_attr(fs_ctx, newpath, credp); 10452c30dd74SAneesh Kumar K.V if (err == -1) { 10462c30dd74SAneesh Kumar K.V serrno = errno; 10472c30dd74SAneesh Kumar K.V goto err_end; 10482c30dd74SAneesh Kumar K.V } 1049b97400caSAneesh Kumar K.V } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 1050b97400caSAneesh Kumar K.V (fs_ctx->export_flags & V9FS_SM_NONE)) { 10514fa4ce71SChen Gang buffer = rpath(fs_ctx, newpath); 10524fa4ce71SChen Gang err = symlink(oldpath, buffer); 1053879c2813SVenkateswararao Jujjuri (JV) if (err) { 10542289be19SAneesh Kumar K.V goto out; 1055879c2813SVenkateswararao Jujjuri (JV) } 10564fa4ce71SChen Gang err = lchown(buffer, credp->fc_uid, credp->fc_gid); 1057879c2813SVenkateswararao Jujjuri (JV) if (err == -1) { 105812848bfcSAneesh Kumar K.V /* 105912848bfcSAneesh Kumar K.V * If we fail to change ownership and if we are 106012848bfcSAneesh Kumar K.V * using security model none. Ignore the error 106112848bfcSAneesh Kumar K.V */ 1062b97400caSAneesh Kumar K.V if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) { 1063879c2813SVenkateswararao Jujjuri (JV) serrno = errno; 1064879c2813SVenkateswararao Jujjuri (JV) goto err_end; 106512848bfcSAneesh Kumar K.V } else 106612848bfcSAneesh Kumar K.V err = 0; 1067879c2813SVenkateswararao Jujjuri (JV) } 1068879c2813SVenkateswararao Jujjuri (JV) } 10692289be19SAneesh Kumar K.V goto out; 1070879c2813SVenkateswararao Jujjuri (JV) 1071879c2813SVenkateswararao Jujjuri (JV) err_end: 10724fa4ce71SChen Gang remove(buffer); 1073879c2813SVenkateswararao Jujjuri (JV) errno = serrno; 10742289be19SAneesh Kumar K.V out: 10754ed7b2c3SStefan Weil g_free(buffer); 10762289be19SAneesh Kumar K.V v9fs_string_free(&fullname); 1077879c2813SVenkateswararao Jujjuri (JV) return err; 1078c494dd6fSAnthony Liguori } 1079c494dd6fSAnthony Liguori 10802289be19SAneesh Kumar K.V static int local_link(FsContext *ctx, V9fsPath *oldpath, 10812289be19SAneesh Kumar K.V V9fsPath *dirpath, const char *name) 1082c494dd6fSAnthony Liguori { 1083ad0b46e6SGreg Kurz char *odirpath = g_path_get_dirname(oldpath->data); 1084ad0b46e6SGreg Kurz char *oname = g_path_get_basename(oldpath->data); 1085ad0b46e6SGreg Kurz int ret = -1; 1086ad0b46e6SGreg Kurz int odirfd, ndirfd; 1087c494dd6fSAnthony Liguori 1088ad0b46e6SGreg Kurz odirfd = local_opendir_nofollow(ctx, odirpath); 1089ad0b46e6SGreg Kurz if (odirfd == -1) { 10906dd4b1f1SGreg Kurz goto out; 10916dd4b1f1SGreg Kurz } 10922c30dd74SAneesh Kumar K.V 1093ad0b46e6SGreg Kurz ndirfd = local_opendir_nofollow(ctx, dirpath->data); 1094ad0b46e6SGreg Kurz if (ndirfd == -1) { 1095ad0b46e6SGreg Kurz close_preserve_errno(odirfd); 1096ad0b46e6SGreg Kurz goto out; 1097ad0b46e6SGreg Kurz } 1098ad0b46e6SGreg Kurz 1099ad0b46e6SGreg Kurz ret = linkat(odirfd, oname, ndirfd, name, 0); 1100ad0b46e6SGreg Kurz if (ret < 0) { 1101ad0b46e6SGreg Kurz goto out_close; 1102ad0b46e6SGreg Kurz } 1103ad0b46e6SGreg Kurz 11042c30dd74SAneesh Kumar K.V /* now link the virtfs_metadata files */ 11056dd4b1f1SGreg Kurz if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 1106ad0b46e6SGreg Kurz int omap_dirfd, nmap_dirfd; 11076dd4b1f1SGreg Kurz 1108ad0b46e6SGreg Kurz ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700); 1109ad0b46e6SGreg Kurz if (ret < 0 && errno != EEXIST) { 1110ad0b46e6SGreg Kurz goto err_undo_link; 11112c30dd74SAneesh Kumar K.V } 1112ad0b46e6SGreg Kurz 1113ad0b46e6SGreg Kurz omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR); 1114ad0b46e6SGreg Kurz if (omap_dirfd == -1) { 1115ad0b46e6SGreg Kurz goto err; 1116ad0b46e6SGreg Kurz } 1117ad0b46e6SGreg Kurz 1118ad0b46e6SGreg Kurz nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR); 1119ad0b46e6SGreg Kurz if (nmap_dirfd == -1) { 1120ad0b46e6SGreg Kurz close_preserve_errno(omap_dirfd); 1121ad0b46e6SGreg Kurz goto err; 1122ad0b46e6SGreg Kurz } 1123ad0b46e6SGreg Kurz 1124ad0b46e6SGreg Kurz ret = linkat(omap_dirfd, oname, nmap_dirfd, name, 0); 1125ad0b46e6SGreg Kurz close_preserve_errno(nmap_dirfd); 1126ad0b46e6SGreg Kurz close_preserve_errno(omap_dirfd); 11272c30dd74SAneesh Kumar K.V if (ret < 0 && errno != ENOENT) { 1128ad0b46e6SGreg Kurz goto err_undo_link; 11292c30dd74SAneesh Kumar K.V } 11306dd4b1f1SGreg Kurz 1131ad0b46e6SGreg Kurz ret = 0; 1132ad0b46e6SGreg Kurz } 1133ad0b46e6SGreg Kurz goto out_close; 1134ad0b46e6SGreg Kurz 1135ad0b46e6SGreg Kurz err: 1136ad0b46e6SGreg Kurz ret = -1; 1137ad0b46e6SGreg Kurz err_undo_link: 1138ad0b46e6SGreg Kurz unlinkat_preserve_errno(ndirfd, name, 0); 1139ad0b46e6SGreg Kurz out_close: 1140ad0b46e6SGreg Kurz close_preserve_errno(ndirfd); 1141ad0b46e6SGreg Kurz close_preserve_errno(odirfd); 11426dd4b1f1SGreg Kurz out: 1143ad0b46e6SGreg Kurz g_free(oname); 1144ad0b46e6SGreg Kurz g_free(odirpath); 11452289be19SAneesh Kumar K.V return ret; 1146c494dd6fSAnthony Liguori } 1147c494dd6fSAnthony Liguori 11482289be19SAneesh Kumar K.V static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size) 11498cf89e00SAnthony Liguori { 1150ac125d99SGreg Kurz int fd, ret; 11512289be19SAneesh Kumar K.V 1152ac125d99SGreg Kurz fd = local_open_nofollow(ctx, fs_path->data, O_WRONLY, 0); 1153ac125d99SGreg Kurz if (fd == -1) { 1154ac125d99SGreg Kurz return -1; 1155ac125d99SGreg Kurz } 1156ac125d99SGreg Kurz ret = ftruncate(fd, size); 1157ac125d99SGreg Kurz close_preserve_errno(fd); 11584fa4ce71SChen Gang return ret; 11598cf89e00SAnthony Liguori } 11608cf89e00SAnthony Liguori 11612289be19SAneesh Kumar K.V static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp) 11628cf89e00SAnthony Liguori { 1163*d369f207SGreg Kurz char *dirpath = g_path_get_dirname(fs_path->data); 1164*d369f207SGreg Kurz char *name = g_path_get_basename(fs_path->data); 11654fa4ce71SChen Gang int ret = -1; 1166*d369f207SGreg Kurz int dirfd; 1167*d369f207SGreg Kurz 1168*d369f207SGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dirpath); 1169*d369f207SGreg Kurz if (dirfd == -1) { 1170*d369f207SGreg Kurz goto out; 1171*d369f207SGreg Kurz } 11722289be19SAneesh Kumar K.V 1173c79ce737SSripathi Kodi if ((credp->fc_uid == -1 && credp->fc_gid == -1) || 117417b1971fSAneesh Kumar K.V (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 117517b1971fSAneesh Kumar K.V (fs_ctx->export_flags & V9FS_SM_NONE)) { 1176*d369f207SGreg Kurz ret = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid, 1177*d369f207SGreg Kurz AT_SYMLINK_NOFOLLOW); 1178b97400caSAneesh Kumar K.V } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 1179*d369f207SGreg Kurz ret = local_set_xattrat(dirfd, name, credp); 11802c30dd74SAneesh Kumar K.V } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 1181*d369f207SGreg Kurz ret = local_set_mapped_file_attrat(dirfd, name, credp); 1182f7613beeSVenkateswararao Jujjuri (JV) } 1183*d369f207SGreg Kurz 1184*d369f207SGreg Kurz close_preserve_errno(dirfd); 1185*d369f207SGreg Kurz out: 1186*d369f207SGreg Kurz g_free(name); 1187*d369f207SGreg Kurz g_free(dirpath); 11884fa4ce71SChen Gang return ret; 11898cf89e00SAnthony Liguori } 11908cf89e00SAnthony Liguori 11912289be19SAneesh Kumar K.V static int local_utimensat(FsContext *s, V9fsPath *fs_path, 119274bc02b2SM. Mohan Kumar const struct timespec *buf) 11938cf89e00SAnthony Liguori { 1194a33eda0dSGreg Kurz char *dirpath = g_path_get_dirname(fs_path->data); 1195a33eda0dSGreg Kurz char *name = g_path_get_basename(fs_path->data); 1196a33eda0dSGreg Kurz int dirfd, ret = -1; 11972289be19SAneesh Kumar K.V 1198a33eda0dSGreg Kurz dirfd = local_opendir_nofollow(s, dirpath); 1199a33eda0dSGreg Kurz if (dirfd == -1) { 1200a33eda0dSGreg Kurz goto out; 1201a33eda0dSGreg Kurz } 1202a33eda0dSGreg Kurz 1203a33eda0dSGreg Kurz ret = utimensat(dirfd, name, buf, AT_SYMLINK_NOFOLLOW); 1204a33eda0dSGreg Kurz close_preserve_errno(dirfd); 1205a33eda0dSGreg Kurz out: 1206a33eda0dSGreg Kurz g_free(dirpath); 1207a33eda0dSGreg Kurz g_free(name); 12084fa4ce71SChen Gang return ret; 12098cf89e00SAnthony Liguori } 12108cf89e00SAnthony Liguori 1211df4938a6SGreg Kurz static int local_unlinkat_common(FsContext *ctx, int dirfd, const char *name, 1212df4938a6SGreg Kurz int flags) 1213df4938a6SGreg Kurz { 1214df4938a6SGreg Kurz int ret = -1; 1215df4938a6SGreg Kurz 1216df4938a6SGreg Kurz if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 1217df4938a6SGreg Kurz int map_dirfd; 1218df4938a6SGreg Kurz 1219df4938a6SGreg Kurz if (flags == AT_REMOVEDIR) { 1220df4938a6SGreg Kurz int fd; 1221df4938a6SGreg Kurz 1222df4938a6SGreg Kurz fd = openat(dirfd, name, O_RDONLY | O_DIRECTORY | O_PATH); 1223df4938a6SGreg Kurz if (fd == -1) { 1224df4938a6SGreg Kurz goto err_out; 1225df4938a6SGreg Kurz } 1226df4938a6SGreg Kurz /* 1227df4938a6SGreg Kurz * If directory remove .virtfs_metadata contained in the 1228df4938a6SGreg Kurz * directory 1229df4938a6SGreg Kurz */ 1230df4938a6SGreg Kurz ret = unlinkat(fd, VIRTFS_META_DIR, AT_REMOVEDIR); 1231df4938a6SGreg Kurz close_preserve_errno(fd); 1232df4938a6SGreg Kurz if (ret < 0 && errno != ENOENT) { 1233df4938a6SGreg Kurz /* 1234df4938a6SGreg Kurz * We didn't had the .virtfs_metadata file. May be file created 1235df4938a6SGreg Kurz * in non-mapped mode ?. Ignore ENOENT. 1236df4938a6SGreg Kurz */ 1237df4938a6SGreg Kurz goto err_out; 1238df4938a6SGreg Kurz } 1239df4938a6SGreg Kurz } 1240df4938a6SGreg Kurz /* 1241df4938a6SGreg Kurz * Now remove the name from parent directory 1242df4938a6SGreg Kurz * .virtfs_metadata directory. 1243df4938a6SGreg Kurz */ 1244df4938a6SGreg Kurz map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR); 1245df4938a6SGreg Kurz ret = unlinkat(map_dirfd, name, 0); 1246df4938a6SGreg Kurz close_preserve_errno(map_dirfd); 1247df4938a6SGreg Kurz if (ret < 0 && errno != ENOENT) { 1248df4938a6SGreg Kurz /* 1249df4938a6SGreg Kurz * We didn't had the .virtfs_metadata file. May be file created 1250df4938a6SGreg Kurz * in non-mapped mode ?. Ignore ENOENT. 1251df4938a6SGreg Kurz */ 1252df4938a6SGreg Kurz goto err_out; 1253df4938a6SGreg Kurz } 1254df4938a6SGreg Kurz } 1255df4938a6SGreg Kurz 1256df4938a6SGreg Kurz ret = unlinkat(dirfd, name, flags); 1257df4938a6SGreg Kurz err_out: 1258df4938a6SGreg Kurz return ret; 1259df4938a6SGreg Kurz } 1260df4938a6SGreg Kurz 12615bae1900SAnthony Liguori static int local_remove(FsContext *ctx, const char *path) 12625bae1900SAnthony Liguori { 12632c30dd74SAneesh Kumar K.V struct stat stbuf; 1264a0e640a8SGreg Kurz char *dirpath = g_path_get_dirname(path); 1265a0e640a8SGreg Kurz char *name = g_path_get_basename(path); 1266a0e640a8SGreg Kurz int flags = 0; 1267a0e640a8SGreg Kurz int dirfd; 1268a0e640a8SGreg Kurz int err = -1; 12692c30dd74SAneesh Kumar K.V 1270a0e640a8SGreg Kurz dirfd = local_opendir_nofollow(ctx, dirpath); 1271a0e640a8SGreg Kurz if (dirfd) { 1272a0e640a8SGreg Kurz goto out; 1273a0e640a8SGreg Kurz } 1274a0e640a8SGreg Kurz 1275a0e640a8SGreg Kurz if (fstatat(dirfd, path, &stbuf, AT_SYMLINK_NOFOLLOW) < 0) { 12762c30dd74SAneesh Kumar K.V goto err_out; 12772c30dd74SAneesh Kumar K.V } 1278a0e640a8SGreg Kurz 12792c30dd74SAneesh Kumar K.V if (S_ISDIR(stbuf.st_mode)) { 1280a0e640a8SGreg Kurz flags |= AT_REMOVEDIR; 12812c30dd74SAneesh Kumar K.V } 12824fa4ce71SChen Gang 1283a0e640a8SGreg Kurz err = local_unlinkat_common(ctx, dirfd, name, flags); 12842c30dd74SAneesh Kumar K.V err_out: 1285a0e640a8SGreg Kurz close_preserve_errno(dirfd); 1286a0e640a8SGreg Kurz out: 1287a0e640a8SGreg Kurz g_free(name); 1288a0e640a8SGreg Kurz g_free(dirpath); 12892c30dd74SAneesh Kumar K.V return err; 12905bae1900SAnthony Liguori } 12915bae1900SAnthony Liguori 12928b888272SAneesh Kumar K.V static int local_fsync(FsContext *ctx, int fid_type, 12938b888272SAneesh Kumar K.V V9fsFidOpenState *fs, int datasync) 12948cf89e00SAnthony Liguori { 12958b888272SAneesh Kumar K.V int fd; 12968b888272SAneesh Kumar K.V 12978b888272SAneesh Kumar K.V if (fid_type == P9_FID_DIR) { 1298f314ea4eSGreg Kurz fd = dirfd(fs->dir.stream); 129949594973SVenkateswararao Jujjuri (JV) } else { 13008b888272SAneesh Kumar K.V fd = fs->fd; 13018b888272SAneesh Kumar K.V } 13028b888272SAneesh Kumar K.V 13038b888272SAneesh Kumar K.V if (datasync) { 13048b888272SAneesh Kumar K.V return qemu_fdatasync(fd); 13058b888272SAneesh Kumar K.V } else { 13068b888272SAneesh Kumar K.V return fsync(fd); 13078cf89e00SAnthony Liguori } 130849594973SVenkateswararao Jujjuri (JV) } 13098cf89e00SAnthony Liguori 13102289be19SAneesh Kumar K.V static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf) 1311be940c87SM. Mohan Kumar { 131231e51d1cSGreg Kurz int fd, ret; 13132289be19SAneesh Kumar K.V 131431e51d1cSGreg Kurz fd = local_open_nofollow(s, fs_path->data, O_RDONLY, 0); 131531e51d1cSGreg Kurz ret = fstatfs(fd, stbuf); 131631e51d1cSGreg Kurz close_preserve_errno(fd); 13174fa4ce71SChen Gang return ret; 1318be940c87SM. Mohan Kumar } 1319be940c87SM. Mohan Kumar 13202289be19SAneesh Kumar K.V static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path, 1321fa32ef88SAneesh Kumar K.V const char *name, void *value, size_t size) 1322fa32ef88SAneesh Kumar K.V { 13232289be19SAneesh Kumar K.V char *path = fs_path->data; 13242289be19SAneesh Kumar K.V 1325fc22118dSAneesh Kumar K.V return v9fs_get_xattr(ctx, path, name, value, size); 1326fa32ef88SAneesh Kumar K.V } 1327fa32ef88SAneesh Kumar K.V 13282289be19SAneesh Kumar K.V static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path, 1329fa32ef88SAneesh Kumar K.V void *value, size_t size) 1330fa32ef88SAneesh Kumar K.V { 13312289be19SAneesh Kumar K.V char *path = fs_path->data; 13322289be19SAneesh Kumar K.V 1333fc22118dSAneesh Kumar K.V return v9fs_list_xattr(ctx, path, value, size); 133461b6c499SAneesh Kumar K.V } 133561b6c499SAneesh Kumar K.V 13362289be19SAneesh Kumar K.V static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name, 133710b468bdSAneesh Kumar K.V void *value, size_t size, int flags) 133810b468bdSAneesh Kumar K.V { 13392289be19SAneesh Kumar K.V char *path = fs_path->data; 13402289be19SAneesh Kumar K.V 1341fc22118dSAneesh Kumar K.V return v9fs_set_xattr(ctx, path, name, value, size, flags); 134210b468bdSAneesh Kumar K.V } 134310b468bdSAneesh Kumar K.V 13442289be19SAneesh Kumar K.V static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path, 13452289be19SAneesh Kumar K.V const char *name) 13469ed3ef26SAneesh Kumar K.V { 13472289be19SAneesh Kumar K.V char *path = fs_path->data; 13482289be19SAneesh Kumar K.V 1349fc22118dSAneesh Kumar K.V return v9fs_remove_xattr(ctx, path, name); 13509ed3ef26SAneesh Kumar K.V } 13519ed3ef26SAneesh Kumar K.V 13522289be19SAneesh Kumar K.V static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path, 13532289be19SAneesh Kumar K.V const char *name, V9fsPath *target) 13542289be19SAneesh Kumar K.V { 13552289be19SAneesh Kumar K.V if (dir_path) { 1356e3e83f2eSGreg Kurz v9fs_path_sprintf(target, "%s/%s", dir_path->data, name); 13572289be19SAneesh Kumar K.V } else { 1358e3e83f2eSGreg Kurz v9fs_path_sprintf(target, "%s", name); 13592289be19SAneesh Kumar K.V } 13602289be19SAneesh Kumar K.V return 0; 13612289be19SAneesh Kumar K.V } 13622289be19SAneesh Kumar K.V 13632289be19SAneesh Kumar K.V static int local_renameat(FsContext *ctx, V9fsPath *olddir, 13642289be19SAneesh Kumar K.V const char *old_name, V9fsPath *newdir, 13652289be19SAneesh Kumar K.V const char *new_name) 13662289be19SAneesh Kumar K.V { 13672289be19SAneesh Kumar K.V int ret; 136899f2cf4bSGreg Kurz int odirfd, ndirfd; 13692289be19SAneesh Kumar K.V 137099f2cf4bSGreg Kurz odirfd = local_opendir_nofollow(ctx, olddir->data); 137199f2cf4bSGreg Kurz if (odirfd == -1) { 137299f2cf4bSGreg Kurz return -1; 137399f2cf4bSGreg Kurz } 13742289be19SAneesh Kumar K.V 137599f2cf4bSGreg Kurz ndirfd = local_opendir_nofollow(ctx, newdir->data); 137699f2cf4bSGreg Kurz if (ndirfd == -1) { 137799f2cf4bSGreg Kurz close_preserve_errno(odirfd); 137899f2cf4bSGreg Kurz return -1; 137999f2cf4bSGreg Kurz } 13802289be19SAneesh Kumar K.V 138199f2cf4bSGreg Kurz ret = renameat(odirfd, old_name, ndirfd, new_name); 138299f2cf4bSGreg Kurz if (ret < 0) { 138399f2cf4bSGreg Kurz goto out; 138499f2cf4bSGreg Kurz } 138599f2cf4bSGreg Kurz 138699f2cf4bSGreg Kurz if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 138799f2cf4bSGreg Kurz int omap_dirfd, nmap_dirfd; 138899f2cf4bSGreg Kurz 138999f2cf4bSGreg Kurz ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700); 139099f2cf4bSGreg Kurz if (ret < 0 && errno != EEXIST) { 139199f2cf4bSGreg Kurz goto err_undo_rename; 139299f2cf4bSGreg Kurz } 139399f2cf4bSGreg Kurz 13946dd4b1f1SGreg Kurz omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR); 139599f2cf4bSGreg Kurz if (omap_dirfd == -1) { 139699f2cf4bSGreg Kurz goto err; 139799f2cf4bSGreg Kurz } 139899f2cf4bSGreg Kurz 13996dd4b1f1SGreg Kurz nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR); 140099f2cf4bSGreg Kurz if (nmap_dirfd == -1) { 140199f2cf4bSGreg Kurz close_preserve_errno(omap_dirfd); 140299f2cf4bSGreg Kurz goto err; 140399f2cf4bSGreg Kurz } 140499f2cf4bSGreg Kurz 140599f2cf4bSGreg Kurz /* rename the .virtfs_metadata files */ 140699f2cf4bSGreg Kurz ret = renameat(omap_dirfd, old_name, nmap_dirfd, new_name); 140799f2cf4bSGreg Kurz close_preserve_errno(nmap_dirfd); 140899f2cf4bSGreg Kurz close_preserve_errno(omap_dirfd); 140999f2cf4bSGreg Kurz if (ret < 0 && errno != ENOENT) { 141099f2cf4bSGreg Kurz goto err_undo_rename; 141199f2cf4bSGreg Kurz } 141299f2cf4bSGreg Kurz 141399f2cf4bSGreg Kurz ret = 0; 141499f2cf4bSGreg Kurz } 141599f2cf4bSGreg Kurz goto out; 141699f2cf4bSGreg Kurz 141799f2cf4bSGreg Kurz err: 141899f2cf4bSGreg Kurz ret = -1; 141999f2cf4bSGreg Kurz err_undo_rename: 142099f2cf4bSGreg Kurz renameat_preserve_errno(ndirfd, new_name, odirfd, old_name); 142199f2cf4bSGreg Kurz out: 142299f2cf4bSGreg Kurz close_preserve_errno(ndirfd); 142399f2cf4bSGreg Kurz close_preserve_errno(odirfd); 14242289be19SAneesh Kumar K.V return ret; 14252289be19SAneesh Kumar K.V } 14262289be19SAneesh Kumar K.V 1427d2767edeSGreg Kurz static void v9fs_path_init_dirname(V9fsPath *path, const char *str) 1428d2767edeSGreg Kurz { 1429d2767edeSGreg Kurz path->data = g_path_get_dirname(str); 1430d2767edeSGreg Kurz path->size = strlen(path->data) + 1; 1431d2767edeSGreg Kurz } 1432d2767edeSGreg Kurz 1433d2767edeSGreg Kurz static int local_rename(FsContext *ctx, const char *oldpath, 1434d2767edeSGreg Kurz const char *newpath) 1435d2767edeSGreg Kurz { 1436d2767edeSGreg Kurz int err; 1437d2767edeSGreg Kurz char *oname = g_path_get_basename(oldpath); 1438d2767edeSGreg Kurz char *nname = g_path_get_basename(newpath); 1439d2767edeSGreg Kurz V9fsPath olddir, newdir; 1440d2767edeSGreg Kurz 1441d2767edeSGreg Kurz v9fs_path_init_dirname(&olddir, oldpath); 1442d2767edeSGreg Kurz v9fs_path_init_dirname(&newdir, newpath); 1443d2767edeSGreg Kurz 1444d2767edeSGreg Kurz err = local_renameat(ctx, &olddir, oname, &newdir, nname); 1445d2767edeSGreg Kurz 1446d2767edeSGreg Kurz v9fs_path_free(&newdir); 1447d2767edeSGreg Kurz v9fs_path_free(&olddir); 1448d2767edeSGreg Kurz g_free(nname); 1449d2767edeSGreg Kurz g_free(oname); 1450d2767edeSGreg Kurz 1451d2767edeSGreg Kurz return err; 1452d2767edeSGreg Kurz } 1453d2767edeSGreg Kurz 14542289be19SAneesh Kumar K.V static int local_unlinkat(FsContext *ctx, V9fsPath *dir, 14552289be19SAneesh Kumar K.V const char *name, int flags) 14562289be19SAneesh Kumar K.V { 14572289be19SAneesh Kumar K.V int ret; 1458df4938a6SGreg Kurz int dirfd; 14592c30dd74SAneesh Kumar K.V 1460df4938a6SGreg Kurz dirfd = local_opendir_nofollow(ctx, dir->data); 1461df4938a6SGreg Kurz if (dirfd == -1) { 1462df4938a6SGreg Kurz return -1; 1463df4938a6SGreg Kurz } 14642289be19SAneesh Kumar K.V 1465df4938a6SGreg Kurz ret = local_unlinkat_common(ctx, dirfd, name, flags); 1466df4938a6SGreg Kurz close_preserve_errno(dirfd); 14672289be19SAneesh Kumar K.V return ret; 14682289be19SAneesh Kumar K.V } 14699ed3ef26SAneesh Kumar K.V 1470e06a765eSHarsh Prateek Bora static int local_ioc_getversion(FsContext *ctx, V9fsPath *path, 1471e06a765eSHarsh Prateek Bora mode_t st_mode, uint64_t *st_gen) 1472e06a765eSHarsh Prateek Bora { 1473ae0f940eSPaolo Bonzini #ifdef FS_IOC_GETVERSION 14740e5fc994SKirill A. Shutemov int err; 1475cc720ddbSAneesh Kumar K.V V9fsFidOpenState fid_open; 1476cc720ddbSAneesh Kumar K.V 1477e06a765eSHarsh Prateek Bora /* 1478e06a765eSHarsh Prateek Bora * Do not try to open special files like device nodes, fifos etc 1479e06a765eSHarsh Prateek Bora * We can get fd for regular files and directories only 1480e06a765eSHarsh Prateek Bora */ 1481e06a765eSHarsh Prateek Bora if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) { 14821a9978a5SKirill A. Shutemov errno = ENOTTY; 14831a9978a5SKirill A. Shutemov return -1; 1484e06a765eSHarsh Prateek Bora } 1485cc720ddbSAneesh Kumar K.V err = local_open(ctx, path, O_RDONLY, &fid_open); 1486cc720ddbSAneesh Kumar K.V if (err < 0) { 1487cc720ddbSAneesh Kumar K.V return err; 1488e06a765eSHarsh Prateek Bora } 1489cc720ddbSAneesh Kumar K.V err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen); 1490cc720ddbSAneesh Kumar K.V local_close(ctx, &fid_open); 1491e06a765eSHarsh Prateek Bora return err; 14920e5fc994SKirill A. Shutemov #else 14930e5fc994SKirill A. Shutemov errno = ENOTTY; 14940e5fc994SKirill A. Shutemov return -1; 14950e5fc994SKirill A. Shutemov #endif 1496e06a765eSHarsh Prateek Bora } 1497e06a765eSHarsh Prateek Bora 14980174fe73SAneesh Kumar K.V static int local_init(FsContext *ctx) 14990174fe73SAneesh Kumar K.V { 1500e06a765eSHarsh Prateek Bora struct statfs stbuf; 15010e35a378SGreg Kurz LocalData *data = g_malloc(sizeof(*data)); 15020e35a378SGreg Kurz 15030e35a378SGreg Kurz data->mountfd = open(ctx->fs_root, O_DIRECTORY | O_RDONLY); 15040e35a378SGreg Kurz if (data->mountfd == -1) { 15050e35a378SGreg Kurz goto err; 15060e35a378SGreg Kurz } 1507e06a765eSHarsh Prateek Bora 150800c90bd1SGreg Kurz #ifdef FS_IOC_GETVERSION 150900c90bd1SGreg Kurz /* 151000c90bd1SGreg Kurz * use ioc_getversion only if the ioctl is definied 151100c90bd1SGreg Kurz */ 15120e35a378SGreg Kurz if (fstatfs(data->mountfd, &stbuf) < 0) { 15130e35a378SGreg Kurz close_preserve_errno(data->mountfd); 15140e35a378SGreg Kurz goto err; 151500c90bd1SGreg Kurz } 151600c90bd1SGreg Kurz switch (stbuf.f_type) { 151700c90bd1SGreg Kurz case EXT2_SUPER_MAGIC: 151800c90bd1SGreg Kurz case BTRFS_SUPER_MAGIC: 151900c90bd1SGreg Kurz case REISERFS_SUPER_MAGIC: 152000c90bd1SGreg Kurz case XFS_SUPER_MAGIC: 152100c90bd1SGreg Kurz ctx->exops.get_st_gen = local_ioc_getversion; 152200c90bd1SGreg Kurz break; 152300c90bd1SGreg Kurz } 152400c90bd1SGreg Kurz #endif 152500c90bd1SGreg Kurz 15262c30dd74SAneesh Kumar K.V if (ctx->export_flags & V9FS_SM_PASSTHROUGH) { 15272c30dd74SAneesh Kumar K.V ctx->xops = passthrough_xattr_ops; 15282c30dd74SAneesh Kumar K.V } else if (ctx->export_flags & V9FS_SM_MAPPED) { 15292c30dd74SAneesh Kumar K.V ctx->xops = mapped_xattr_ops; 15302c30dd74SAneesh Kumar K.V } else if (ctx->export_flags & V9FS_SM_NONE) { 15312c30dd74SAneesh Kumar K.V ctx->xops = none_xattr_ops; 15322c30dd74SAneesh Kumar K.V } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 15332c30dd74SAneesh Kumar K.V /* 15342c30dd74SAneesh Kumar K.V * xattr operation for mapped-file and passthrough 15352c30dd74SAneesh Kumar K.V * remain same. 15362c30dd74SAneesh Kumar K.V */ 15372c30dd74SAneesh Kumar K.V ctx->xops = passthrough_xattr_ops; 15382c30dd74SAneesh Kumar K.V } 1539c98f1d4aSAneesh Kumar K.V ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT; 154000c90bd1SGreg Kurz 15410e35a378SGreg Kurz ctx->private = data; 154200c90bd1SGreg Kurz return 0; 15430e35a378SGreg Kurz 15440e35a378SGreg Kurz err: 15450e35a378SGreg Kurz g_free(data); 15460e35a378SGreg Kurz return -1; 15470e35a378SGreg Kurz } 15480e35a378SGreg Kurz 15490e35a378SGreg Kurz static void local_cleanup(FsContext *ctx) 15500e35a378SGreg Kurz { 15510e35a378SGreg Kurz LocalData *data = ctx->private; 15520e35a378SGreg Kurz 15530e35a378SGreg Kurz close(data->mountfd); 15540e35a378SGreg Kurz g_free(data); 15550174fe73SAneesh Kumar K.V } 15560174fe73SAneesh Kumar K.V 155799519f0aSAneesh Kumar K.V static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse) 155899519f0aSAneesh Kumar K.V { 155999519f0aSAneesh Kumar K.V const char *sec_model = qemu_opt_get(opts, "security_model"); 156099519f0aSAneesh Kumar K.V const char *path = qemu_opt_get(opts, "path"); 156199519f0aSAneesh Kumar K.V 156299519f0aSAneesh Kumar K.V if (!sec_model) { 156363325b18SGreg Kurz error_report("Security model not specified, local fs needs security model"); 156463325b18SGreg Kurz error_printf("valid options are:" 156563325b18SGreg Kurz "\tsecurity_model=[passthrough|mapped-xattr|mapped-file|none]\n"); 156699519f0aSAneesh Kumar K.V return -1; 156799519f0aSAneesh Kumar K.V } 156899519f0aSAneesh Kumar K.V 156999519f0aSAneesh Kumar K.V if (!strcmp(sec_model, "passthrough")) { 157099519f0aSAneesh Kumar K.V fse->export_flags |= V9FS_SM_PASSTHROUGH; 15712c30dd74SAneesh Kumar K.V } else if (!strcmp(sec_model, "mapped") || 15722c30dd74SAneesh Kumar K.V !strcmp(sec_model, "mapped-xattr")) { 157399519f0aSAneesh Kumar K.V fse->export_flags |= V9FS_SM_MAPPED; 157499519f0aSAneesh Kumar K.V } else if (!strcmp(sec_model, "none")) { 157599519f0aSAneesh Kumar K.V fse->export_flags |= V9FS_SM_NONE; 15762c30dd74SAneesh Kumar K.V } else if (!strcmp(sec_model, "mapped-file")) { 15772c30dd74SAneesh Kumar K.V fse->export_flags |= V9FS_SM_MAPPED_FILE; 157899519f0aSAneesh Kumar K.V } else { 157963325b18SGreg Kurz error_report("Invalid security model %s specified", sec_model); 158063325b18SGreg Kurz error_printf("valid options are:" 158163325b18SGreg Kurz "\t[passthrough|mapped-xattr|mapped-file|none]\n"); 158299519f0aSAneesh Kumar K.V return -1; 158399519f0aSAneesh Kumar K.V } 158499519f0aSAneesh Kumar K.V 158599519f0aSAneesh Kumar K.V if (!path) { 158663325b18SGreg Kurz error_report("fsdev: No path specified"); 158799519f0aSAneesh Kumar K.V return -1; 158899519f0aSAneesh Kumar K.V } 158999519f0aSAneesh Kumar K.V fse->path = g_strdup(path); 159099519f0aSAneesh Kumar K.V 159199519f0aSAneesh Kumar K.V return 0; 159299519f0aSAneesh Kumar K.V } 159399519f0aSAneesh Kumar K.V 15949f107513SAnthony Liguori FileOperations local_ops = { 159599519f0aSAneesh Kumar K.V .parse_opts = local_parse_opts, 15960174fe73SAneesh Kumar K.V .init = local_init, 15970e35a378SGreg Kurz .cleanup = local_cleanup, 1598131dcb25SAnthony Liguori .lstat = local_lstat, 1599131dcb25SAnthony Liguori .readlink = local_readlink, 1600131dcb25SAnthony Liguori .close = local_close, 1601131dcb25SAnthony Liguori .closedir = local_closedir, 1602a6568fe2SAnthony Liguori .open = local_open, 1603a6568fe2SAnthony Liguori .opendir = local_opendir, 1604a9231555SAnthony Liguori .rewinddir = local_rewinddir, 1605a9231555SAnthony Liguori .telldir = local_telldir, 1606635324e8SGreg Kurz .readdir = local_readdir, 1607a9231555SAnthony Liguori .seekdir = local_seekdir, 160856d15a53SSanchit Garg .preadv = local_preadv, 160956d15a53SSanchit Garg .pwritev = local_pwritev, 1610c494dd6fSAnthony Liguori .chmod = local_chmod, 1611c494dd6fSAnthony Liguori .mknod = local_mknod, 1612c494dd6fSAnthony Liguori .mkdir = local_mkdir, 1613c494dd6fSAnthony Liguori .fstat = local_fstat, 1614c494dd6fSAnthony Liguori .open2 = local_open2, 1615c494dd6fSAnthony Liguori .symlink = local_symlink, 1616c494dd6fSAnthony Liguori .link = local_link, 16178cf89e00SAnthony Liguori .truncate = local_truncate, 16188cf89e00SAnthony Liguori .rename = local_rename, 16198cf89e00SAnthony Liguori .chown = local_chown, 162074bc02b2SM. Mohan Kumar .utimensat = local_utimensat, 16215bae1900SAnthony Liguori .remove = local_remove, 16228cf89e00SAnthony Liguori .fsync = local_fsync, 1623be940c87SM. Mohan Kumar .statfs = local_statfs, 1624fa32ef88SAneesh Kumar K.V .lgetxattr = local_lgetxattr, 1625fa32ef88SAneesh Kumar K.V .llistxattr = local_llistxattr, 162610b468bdSAneesh Kumar K.V .lsetxattr = local_lsetxattr, 16279ed3ef26SAneesh Kumar K.V .lremovexattr = local_lremovexattr, 16282289be19SAneesh Kumar K.V .name_to_path = local_name_to_path, 16292289be19SAneesh Kumar K.V .renameat = local_renameat, 16302289be19SAneesh Kumar K.V .unlinkat = local_unlinkat, 16319f107513SAnthony Liguori }; 1632