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 87f9aef99bSGreg Kurz static FILE *local_fopenat(int dirfd, const char *name, const char *mode) 880ceb092eSAneesh Kumar K.V { 890ceb092eSAneesh Kumar K.V int fd, o_mode = 0; 900ceb092eSAneesh Kumar K.V FILE *fp; 91f9aef99bSGreg Kurz int flags; 920ceb092eSAneesh Kumar K.V /* 930ceb092eSAneesh Kumar K.V * only supports two modes 940ceb092eSAneesh Kumar K.V */ 950ceb092eSAneesh Kumar K.V if (mode[0] == 'r') { 96f9aef99bSGreg Kurz flags = O_RDONLY; 970ceb092eSAneesh Kumar K.V } else if (mode[0] == 'w') { 98f9aef99bSGreg Kurz flags = O_WRONLY | O_TRUNC | O_CREAT; 990ceb092eSAneesh Kumar K.V o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; 1000ceb092eSAneesh Kumar K.V } else { 1010ceb092eSAneesh Kumar K.V return NULL; 1020ceb092eSAneesh Kumar K.V } 103f9aef99bSGreg Kurz fd = openat_file(dirfd, name, flags, o_mode); 1040ceb092eSAneesh Kumar K.V if (fd == -1) { 1050ceb092eSAneesh Kumar K.V return NULL; 1060ceb092eSAneesh Kumar K.V } 1070ceb092eSAneesh Kumar K.V fp = fdopen(fd, mode); 1080ceb092eSAneesh Kumar K.V if (!fp) { 1090ceb092eSAneesh Kumar K.V close(fd); 1100ceb092eSAneesh Kumar K.V } 1110ceb092eSAneesh Kumar K.V return fp; 1120ceb092eSAneesh Kumar K.V } 1130ceb092eSAneesh Kumar K.V 1142c30dd74SAneesh Kumar K.V #define ATTR_MAX 100 115f9aef99bSGreg Kurz static void local_mapped_file_attr(int dirfd, const char *name, 1162c30dd74SAneesh Kumar K.V struct stat *stbuf) 1172c30dd74SAneesh Kumar K.V { 1182c30dd74SAneesh Kumar K.V FILE *fp; 1192c30dd74SAneesh Kumar K.V char buf[ATTR_MAX]; 120f9aef99bSGreg Kurz int map_dirfd; 1212c30dd74SAneesh Kumar K.V 12299f2cf4bSGreg Kurz map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR); 123f9aef99bSGreg Kurz if (map_dirfd == -1) { 124f9aef99bSGreg Kurz return; 125f9aef99bSGreg Kurz } 126f9aef99bSGreg Kurz 127f9aef99bSGreg Kurz fp = local_fopenat(map_dirfd, name, "r"); 128f9aef99bSGreg Kurz close_preserve_errno(map_dirfd); 1292c30dd74SAneesh Kumar K.V if (!fp) { 1302c30dd74SAneesh Kumar K.V return; 1312c30dd74SAneesh Kumar K.V } 1322c30dd74SAneesh Kumar K.V memset(buf, 0, ATTR_MAX); 1332c30dd74SAneesh Kumar K.V while (fgets(buf, ATTR_MAX, fp)) { 1342c30dd74SAneesh Kumar K.V if (!strncmp(buf, "virtfs.uid", 10)) { 1352c30dd74SAneesh Kumar K.V stbuf->st_uid = atoi(buf+11); 1362c30dd74SAneesh Kumar K.V } else if (!strncmp(buf, "virtfs.gid", 10)) { 1372c30dd74SAneesh Kumar K.V stbuf->st_gid = atoi(buf+11); 1382c30dd74SAneesh Kumar K.V } else if (!strncmp(buf, "virtfs.mode", 11)) { 1392c30dd74SAneesh Kumar K.V stbuf->st_mode = atoi(buf+12); 1402c30dd74SAneesh Kumar K.V } else if (!strncmp(buf, "virtfs.rdev", 11)) { 1412c30dd74SAneesh Kumar K.V stbuf->st_rdev = atoi(buf+12); 1422c30dd74SAneesh Kumar K.V } 1432c30dd74SAneesh Kumar K.V memset(buf, 0, ATTR_MAX); 1442c30dd74SAneesh Kumar K.V } 1452c30dd74SAneesh Kumar K.V fclose(fp); 1462c30dd74SAneesh Kumar K.V } 1472c30dd74SAneesh Kumar K.V 1482289be19SAneesh Kumar K.V static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf) 149131dcb25SAnthony Liguori { 150f9aef99bSGreg Kurz int err = -1; 151f9aef99bSGreg Kurz char *dirpath = g_path_get_dirname(fs_path->data); 152f9aef99bSGreg Kurz char *name = g_path_get_basename(fs_path->data); 153f9aef99bSGreg Kurz int dirfd; 1542289be19SAneesh Kumar K.V 155f9aef99bSGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dirpath); 156f9aef99bSGreg Kurz if (dirfd == -1) { 157f9aef99bSGreg Kurz goto out; 158f9aef99bSGreg Kurz } 159f9aef99bSGreg Kurz 160f9aef99bSGreg Kurz err = fstatat(dirfd, name, stbuf, AT_SYMLINK_NOFOLLOW); 1611237ad76SVenkateswararao Jujjuri (JV) if (err) { 1624fa4ce71SChen Gang goto err_out; 1631237ad76SVenkateswararao Jujjuri (JV) } 164b97400caSAneesh Kumar K.V if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 1651237ad76SVenkateswararao Jujjuri (JV) /* Actual credentials are part of extended attrs */ 1661237ad76SVenkateswararao Jujjuri (JV) uid_t tmp_uid; 1671237ad76SVenkateswararao Jujjuri (JV) gid_t tmp_gid; 1681237ad76SVenkateswararao Jujjuri (JV) mode_t tmp_mode; 1691237ad76SVenkateswararao Jujjuri (JV) dev_t tmp_dev; 170f9aef99bSGreg Kurz 171f9aef99bSGreg Kurz if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.uid", &tmp_uid, 172f9aef99bSGreg Kurz sizeof(uid_t)) > 0) { 173f8ad4a89SAneesh Kumar K.V stbuf->st_uid = le32_to_cpu(tmp_uid); 1741237ad76SVenkateswararao Jujjuri (JV) } 175f9aef99bSGreg Kurz if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.gid", &tmp_gid, 176f9aef99bSGreg Kurz sizeof(gid_t)) > 0) { 177f8ad4a89SAneesh Kumar K.V stbuf->st_gid = le32_to_cpu(tmp_gid); 1781237ad76SVenkateswararao Jujjuri (JV) } 179f9aef99bSGreg Kurz if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.mode", &tmp_mode, 180f9aef99bSGreg Kurz sizeof(mode_t)) > 0) { 181f8ad4a89SAneesh Kumar K.V stbuf->st_mode = le32_to_cpu(tmp_mode); 1821237ad76SVenkateswararao Jujjuri (JV) } 183f9aef99bSGreg Kurz if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.rdev", &tmp_dev, 184f9aef99bSGreg Kurz sizeof(dev_t)) > 0) { 185f8ad4a89SAneesh Kumar K.V stbuf->st_rdev = le64_to_cpu(tmp_dev); 1861237ad76SVenkateswararao Jujjuri (JV) } 1872c30dd74SAneesh Kumar K.V } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 188f9aef99bSGreg Kurz local_mapped_file_attr(dirfd, name, stbuf); 1891237ad76SVenkateswararao Jujjuri (JV) } 1904fa4ce71SChen Gang 1914fa4ce71SChen Gang err_out: 192f9aef99bSGreg Kurz close_preserve_errno(dirfd); 193f9aef99bSGreg Kurz out: 194f9aef99bSGreg Kurz g_free(name); 195f9aef99bSGreg Kurz g_free(dirpath); 1961237ad76SVenkateswararao Jujjuri (JV) return err; 197131dcb25SAnthony Liguori } 198131dcb25SAnthony Liguori 199e3187a45SGreg Kurz static int local_set_mapped_file_attrat(int dirfd, const char *name, 200e3187a45SGreg Kurz FsCred *credp) 2012c30dd74SAneesh Kumar K.V { 2022c30dd74SAneesh Kumar K.V FILE *fp; 203e3187a45SGreg Kurz int ret; 2042c30dd74SAneesh Kumar K.V char buf[ATTR_MAX]; 2052c30dd74SAneesh Kumar K.V int uid = -1, gid = -1, mode = -1, rdev = -1; 206e3187a45SGreg Kurz int map_dirfd; 2072c30dd74SAneesh Kumar K.V 208e3187a45SGreg Kurz ret = mkdirat(dirfd, VIRTFS_META_DIR, 0700); 209e3187a45SGreg Kurz if (ret < 0 && errno != EEXIST) { 210e3187a45SGreg Kurz return -1; 211e3187a45SGreg Kurz } 212e3187a45SGreg Kurz 213e3187a45SGreg Kurz map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR); 214e3187a45SGreg Kurz if (map_dirfd == -1) { 215e3187a45SGreg Kurz return -1; 216e3187a45SGreg Kurz } 217e3187a45SGreg Kurz 218e3187a45SGreg Kurz fp = local_fopenat(map_dirfd, name, "r"); 2192c30dd74SAneesh Kumar K.V if (!fp) { 220e3187a45SGreg Kurz if (errno == ENOENT) { 221e3187a45SGreg Kurz goto update_map_file; 222e3187a45SGreg Kurz } else { 223e3187a45SGreg Kurz close_preserve_errno(map_dirfd); 224e3187a45SGreg Kurz return -1; 225e3187a45SGreg Kurz } 2262c30dd74SAneesh Kumar K.V } 2272c30dd74SAneesh Kumar K.V memset(buf, 0, ATTR_MAX); 2282c30dd74SAneesh Kumar K.V while (fgets(buf, ATTR_MAX, fp)) { 2292c30dd74SAneesh Kumar K.V if (!strncmp(buf, "virtfs.uid", 10)) { 2302c30dd74SAneesh Kumar K.V uid = atoi(buf + 11); 2312c30dd74SAneesh Kumar K.V } else if (!strncmp(buf, "virtfs.gid", 10)) { 2322c30dd74SAneesh Kumar K.V gid = atoi(buf + 11); 2332c30dd74SAneesh Kumar K.V } else if (!strncmp(buf, "virtfs.mode", 11)) { 2342c30dd74SAneesh Kumar K.V mode = atoi(buf + 12); 2352c30dd74SAneesh Kumar K.V } else if (!strncmp(buf, "virtfs.rdev", 11)) { 2362c30dd74SAneesh Kumar K.V rdev = atoi(buf + 12); 2372c30dd74SAneesh Kumar K.V } 2382c30dd74SAneesh Kumar K.V memset(buf, 0, ATTR_MAX); 2392c30dd74SAneesh Kumar K.V } 2402c30dd74SAneesh Kumar K.V fclose(fp); 2412c30dd74SAneesh Kumar K.V 2422c30dd74SAneesh Kumar K.V update_map_file: 243e3187a45SGreg Kurz fp = local_fopenat(map_dirfd, name, "w"); 244e3187a45SGreg Kurz close_preserve_errno(map_dirfd); 2452c30dd74SAneesh Kumar K.V if (!fp) { 246e3187a45SGreg Kurz return -1; 2472c30dd74SAneesh Kumar K.V } 2482c30dd74SAneesh Kumar K.V 2492c30dd74SAneesh Kumar K.V if (credp->fc_uid != -1) { 2502c30dd74SAneesh Kumar K.V uid = credp->fc_uid; 2512c30dd74SAneesh Kumar K.V } 2522c30dd74SAneesh Kumar K.V if (credp->fc_gid != -1) { 2532c30dd74SAneesh Kumar K.V gid = credp->fc_gid; 2542c30dd74SAneesh Kumar K.V } 2552c30dd74SAneesh Kumar K.V if (credp->fc_mode != -1) { 2562c30dd74SAneesh Kumar K.V mode = credp->fc_mode; 2572c30dd74SAneesh Kumar K.V } 2582c30dd74SAneesh Kumar K.V if (credp->fc_rdev != -1) { 2592c30dd74SAneesh Kumar K.V rdev = credp->fc_rdev; 2602c30dd74SAneesh Kumar K.V } 2612c30dd74SAneesh Kumar K.V 2622c30dd74SAneesh Kumar K.V if (uid != -1) { 2632c30dd74SAneesh Kumar K.V fprintf(fp, "virtfs.uid=%d\n", uid); 2642c30dd74SAneesh Kumar K.V } 2652c30dd74SAneesh Kumar K.V if (gid != -1) { 2662c30dd74SAneesh Kumar K.V fprintf(fp, "virtfs.gid=%d\n", gid); 2672c30dd74SAneesh Kumar K.V } 2682c30dd74SAneesh Kumar K.V if (mode != -1) { 2692c30dd74SAneesh Kumar K.V fprintf(fp, "virtfs.mode=%d\n", mode); 2702c30dd74SAneesh Kumar K.V } 2712c30dd74SAneesh Kumar K.V if (rdev != -1) { 2722c30dd74SAneesh Kumar K.V fprintf(fp, "virtfs.rdev=%d\n", rdev); 2732c30dd74SAneesh Kumar K.V } 2742c30dd74SAneesh Kumar K.V fclose(fp); 2752c30dd74SAneesh Kumar K.V 276e3187a45SGreg Kurz return 0; 277e3187a45SGreg Kurz } 278e3187a45SGreg Kurz 279e3187a45SGreg Kurz static int fchmodat_nofollow(int dirfd, const char *name, mode_t mode) 280e3187a45SGreg Kurz { 281e3187a45SGreg Kurz int fd, ret; 282e3187a45SGreg Kurz 283e3187a45SGreg Kurz /* FIXME: this should be handled with fchmodat(AT_SYMLINK_NOFOLLOW). 284e3187a45SGreg Kurz * Unfortunately, the linux kernel doesn't implement it yet. As an 285e3187a45SGreg Kurz * alternative, let's open the file and use fchmod() instead. This 286e3187a45SGreg Kurz * may fail depending on the permissions of the file, but it is the 287e3187a45SGreg Kurz * best we can do to avoid TOCTTOU. We first try to open read-only 288e3187a45SGreg Kurz * in case name points to a directory. If that fails, we try write-only 289e3187a45SGreg Kurz * in case name doesn't point to a directory. 290e3187a45SGreg Kurz */ 291e3187a45SGreg Kurz fd = openat_file(dirfd, name, O_RDONLY, 0); 292e3187a45SGreg Kurz if (fd == -1) { 293e3187a45SGreg Kurz /* In case the file is writable-only and isn't a directory. */ 294e3187a45SGreg Kurz if (errno == EACCES) { 295e3187a45SGreg Kurz fd = openat_file(dirfd, name, O_WRONLY, 0); 296e3187a45SGreg Kurz } 297e3187a45SGreg Kurz if (fd == -1 && errno == EISDIR) { 298e3187a45SGreg Kurz errno = EACCES; 299e3187a45SGreg Kurz } 300e3187a45SGreg Kurz } 301e3187a45SGreg Kurz if (fd == -1) { 302e3187a45SGreg Kurz return -1; 303e3187a45SGreg Kurz } 304e3187a45SGreg Kurz ret = fchmod(fd, mode); 305e3187a45SGreg Kurz close_preserve_errno(fd); 3062c30dd74SAneesh Kumar K.V return ret; 3072c30dd74SAneesh Kumar K.V } 3082c30dd74SAneesh Kumar K.V 309e3187a45SGreg Kurz static int local_set_xattrat(int dirfd, const char *path, FsCred *credp) 310131dcb25SAnthony Liguori { 311758e8e38SVenkateswararao Jujjuri (JV) int err; 3122289be19SAneesh Kumar K.V 313758e8e38SVenkateswararao Jujjuri (JV) if (credp->fc_uid != -1) { 314f8ad4a89SAneesh Kumar K.V uint32_t tmp_uid = cpu_to_le32(credp->fc_uid); 315e3187a45SGreg Kurz err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.uid", &tmp_uid, 316e3187a45SGreg Kurz sizeof(uid_t), 0); 317758e8e38SVenkateswararao Jujjuri (JV) if (err) { 318758e8e38SVenkateswararao Jujjuri (JV) return err; 319131dcb25SAnthony Liguori } 320131dcb25SAnthony Liguori } 321758e8e38SVenkateswararao Jujjuri (JV) if (credp->fc_gid != -1) { 322f8ad4a89SAneesh Kumar K.V uint32_t tmp_gid = cpu_to_le32(credp->fc_gid); 323e3187a45SGreg Kurz err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.gid", &tmp_gid, 324e3187a45SGreg Kurz sizeof(gid_t), 0); 325758e8e38SVenkateswararao Jujjuri (JV) if (err) { 326758e8e38SVenkateswararao Jujjuri (JV) return err; 327131dcb25SAnthony Liguori } 328131dcb25SAnthony Liguori } 329758e8e38SVenkateswararao Jujjuri (JV) if (credp->fc_mode != -1) { 330f8ad4a89SAneesh Kumar K.V uint32_t tmp_mode = cpu_to_le32(credp->fc_mode); 331e3187a45SGreg Kurz err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.mode", &tmp_mode, 332e3187a45SGreg Kurz sizeof(mode_t), 0); 333758e8e38SVenkateswararao Jujjuri (JV) if (err) { 334758e8e38SVenkateswararao Jujjuri (JV) return err; 335131dcb25SAnthony Liguori } 336131dcb25SAnthony Liguori } 337758e8e38SVenkateswararao Jujjuri (JV) if (credp->fc_rdev != -1) { 338f8ad4a89SAneesh Kumar K.V uint64_t tmp_rdev = cpu_to_le64(credp->fc_rdev); 339e3187a45SGreg Kurz err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.rdev", &tmp_rdev, 340e3187a45SGreg Kurz sizeof(dev_t), 0); 341758e8e38SVenkateswararao Jujjuri (JV) if (err) { 342758e8e38SVenkateswararao Jujjuri (JV) return err; 343131dcb25SAnthony Liguori } 344758e8e38SVenkateswararao Jujjuri (JV) } 345131dcb25SAnthony Liguori return 0; 346131dcb25SAnthony Liguori } 347131dcb25SAnthony Liguori 348d815e721SGreg Kurz static int local_set_cred_passthrough(FsContext *fs_ctx, int dirfd, 349d815e721SGreg Kurz const char *name, FsCred *credp) 3504750a96fSVenkateswararao Jujjuri (JV) { 351d815e721SGreg Kurz if (fchownat(dirfd, name, credp->fc_uid, credp->fc_gid, 352d815e721SGreg Kurz AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH) < 0) { 35312848bfcSAneesh Kumar K.V /* 35412848bfcSAneesh Kumar K.V * If we fail to change ownership and if we are 35512848bfcSAneesh Kumar K.V * using security model none. Ignore the error 35612848bfcSAneesh Kumar K.V */ 357b97400caSAneesh Kumar K.V if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) { 3584fa4ce71SChen Gang return -1; 3594750a96fSVenkateswararao Jujjuri (JV) } 360d815e721SGreg Kurz } 361d815e721SGreg Kurz 362d815e721SGreg Kurz return fchmodat_nofollow(dirfd, name, credp->fc_mode & 07777); 363d815e721SGreg Kurz } 3644750a96fSVenkateswararao Jujjuri (JV) 3652289be19SAneesh Kumar K.V static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path, 366131dcb25SAnthony Liguori char *buf, size_t bufsz) 367131dcb25SAnthony Liguori { 368879c2813SVenkateswararao Jujjuri (JV) ssize_t tsize = -1; 3692289be19SAneesh Kumar K.V 3702c30dd74SAneesh Kumar K.V if ((fs_ctx->export_flags & V9FS_SM_MAPPED) || 3712c30dd74SAneesh Kumar K.V (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) { 372879c2813SVenkateswararao Jujjuri (JV) int fd; 373bec1e954SGreg Kurz 374bec1e954SGreg Kurz fd = local_open_nofollow(fs_ctx, fs_path->data, O_RDONLY, 0); 375879c2813SVenkateswararao Jujjuri (JV) if (fd == -1) { 376879c2813SVenkateswararao Jujjuri (JV) return -1; 377879c2813SVenkateswararao Jujjuri (JV) } 378879c2813SVenkateswararao Jujjuri (JV) do { 379879c2813SVenkateswararao Jujjuri (JV) tsize = read(fd, (void *)buf, bufsz); 380879c2813SVenkateswararao Jujjuri (JV) } while (tsize == -1 && errno == EINTR); 381bec1e954SGreg Kurz close_preserve_errno(fd); 382b97400caSAneesh Kumar K.V } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 383b97400caSAneesh Kumar K.V (fs_ctx->export_flags & V9FS_SM_NONE)) { 384bec1e954SGreg Kurz char *dirpath = g_path_get_dirname(fs_path->data); 385bec1e954SGreg Kurz char *name = g_path_get_basename(fs_path->data); 386bec1e954SGreg Kurz int dirfd; 387bec1e954SGreg Kurz 388bec1e954SGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dirpath); 389bec1e954SGreg Kurz if (dirfd == -1) { 390bec1e954SGreg Kurz goto out; 391bec1e954SGreg Kurz } 392bec1e954SGreg Kurz 393bec1e954SGreg Kurz tsize = readlinkat(dirfd, name, buf, bufsz); 394bec1e954SGreg Kurz close_preserve_errno(dirfd); 395bec1e954SGreg Kurz out: 396bec1e954SGreg Kurz g_free(name); 397bec1e954SGreg Kurz g_free(dirpath); 398879c2813SVenkateswararao Jujjuri (JV) } 399879c2813SVenkateswararao Jujjuri (JV) return tsize; 400131dcb25SAnthony Liguori } 401131dcb25SAnthony Liguori 402cc720ddbSAneesh Kumar K.V static int local_close(FsContext *ctx, V9fsFidOpenState *fs) 403131dcb25SAnthony Liguori { 404cc720ddbSAneesh Kumar K.V return close(fs->fd); 405131dcb25SAnthony Liguori } 406131dcb25SAnthony Liguori 407cc720ddbSAneesh Kumar K.V static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs) 408131dcb25SAnthony Liguori { 409f314ea4eSGreg Kurz return closedir(fs->dir.stream); 410131dcb25SAnthony Liguori } 4119f107513SAnthony Liguori 412cc720ddbSAneesh Kumar K.V static int local_open(FsContext *ctx, V9fsPath *fs_path, 413cc720ddbSAneesh Kumar K.V int flags, V9fsFidOpenState *fs) 414a6568fe2SAnthony Liguori { 41521328e1eSGreg Kurz int fd; 4162289be19SAneesh Kumar K.V 417996a0d76SGreg Kurz fd = local_open_nofollow(ctx, fs_path->data, flags, 0); 41821328e1eSGreg Kurz if (fd == -1) { 41921328e1eSGreg Kurz return -1; 42021328e1eSGreg Kurz } 42121328e1eSGreg Kurz fs->fd = fd; 422cc720ddbSAneesh Kumar K.V return fs->fd; 423a6568fe2SAnthony Liguori } 424a6568fe2SAnthony Liguori 425cc720ddbSAneesh Kumar K.V static int local_opendir(FsContext *ctx, 426cc720ddbSAneesh Kumar K.V V9fsPath *fs_path, V9fsFidOpenState *fs) 427a6568fe2SAnthony Liguori { 428996a0d76SGreg Kurz int dirfd; 42921328e1eSGreg Kurz DIR *stream; 4302289be19SAneesh Kumar K.V 431996a0d76SGreg Kurz dirfd = local_opendir_nofollow(ctx, fs_path->data); 432996a0d76SGreg Kurz if (dirfd == -1) { 433cc720ddbSAneesh Kumar K.V return -1; 434cc720ddbSAneesh Kumar K.V } 435996a0d76SGreg Kurz 436996a0d76SGreg Kurz stream = fdopendir(dirfd); 43721328e1eSGreg Kurz if (!stream) { 438faab207fSGreg Kurz close(dirfd); 439cc720ddbSAneesh Kumar K.V return -1; 440cc720ddbSAneesh Kumar K.V } 44121328e1eSGreg Kurz fs->dir.stream = stream; 442cc720ddbSAneesh Kumar K.V return 0; 443a6568fe2SAnthony Liguori } 444a6568fe2SAnthony Liguori 445cc720ddbSAneesh Kumar K.V static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs) 446a9231555SAnthony Liguori { 447f314ea4eSGreg Kurz rewinddir(fs->dir.stream); 448a9231555SAnthony Liguori } 449a9231555SAnthony Liguori 450cc720ddbSAneesh Kumar K.V static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs) 451a9231555SAnthony Liguori { 452f314ea4eSGreg Kurz return telldir(fs->dir.stream); 453a9231555SAnthony Liguori } 454a9231555SAnthony Liguori 455635324e8SGreg Kurz static struct dirent *local_readdir(FsContext *ctx, V9fsFidOpenState *fs) 456a9231555SAnthony Liguori { 457635324e8SGreg Kurz struct dirent *entry; 4582c30dd74SAneesh Kumar K.V 4592c30dd74SAneesh Kumar K.V again: 460635324e8SGreg Kurz entry = readdir(fs->dir.stream); 461635324e8SGreg Kurz if (!entry) { 462635324e8SGreg Kurz return NULL; 463635324e8SGreg Kurz } 464635324e8SGreg Kurz 465840a1bf2SBastian Blank if (ctx->export_flags & V9FS_SM_MAPPED) { 466840a1bf2SBastian Blank entry->d_type = DT_UNKNOWN; 467840a1bf2SBastian Blank } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 468635324e8SGreg Kurz if (!strcmp(entry->d_name, VIRTFS_META_DIR)) { 4692c30dd74SAneesh Kumar K.V /* skp the meta data directory */ 4702c30dd74SAneesh Kumar K.V goto again; 4712c30dd74SAneesh Kumar K.V } 472840a1bf2SBastian Blank entry->d_type = DT_UNKNOWN; 4732c30dd74SAneesh Kumar K.V } 474635324e8SGreg Kurz 475635324e8SGreg Kurz return entry; 476a9231555SAnthony Liguori } 477a9231555SAnthony Liguori 478cc720ddbSAneesh Kumar K.V static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off) 479a9231555SAnthony Liguori { 480f314ea4eSGreg Kurz seekdir(fs->dir.stream, off); 481a9231555SAnthony Liguori } 482a9231555SAnthony Liguori 483cc720ddbSAneesh Kumar K.V static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs, 484cc720ddbSAneesh Kumar K.V const struct iovec *iov, 48556d15a53SSanchit Garg int iovcnt, off_t offset) 486a9231555SAnthony Liguori { 48756d15a53SSanchit Garg #ifdef CONFIG_PREADV 488cc720ddbSAneesh Kumar K.V return preadv(fs->fd, iov, iovcnt, offset); 48956d15a53SSanchit Garg #else 490cc720ddbSAneesh Kumar K.V int err = lseek(fs->fd, offset, SEEK_SET); 49156d15a53SSanchit Garg if (err == -1) { 49256d15a53SSanchit Garg return err; 49356d15a53SSanchit Garg } else { 494cc720ddbSAneesh Kumar K.V return readv(fs->fd, iov, iovcnt); 495a9231555SAnthony Liguori } 49656d15a53SSanchit Garg #endif 497a9231555SAnthony Liguori } 498a9231555SAnthony Liguori 499cc720ddbSAneesh Kumar K.V static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs, 500cc720ddbSAneesh Kumar K.V const struct iovec *iov, 50156d15a53SSanchit Garg int iovcnt, off_t offset) 5028449360cSAnthony Liguori { 5036fe76accSGreg Kurz ssize_t ret; 50456d15a53SSanchit Garg #ifdef CONFIG_PREADV 505cc720ddbSAneesh Kumar K.V ret = pwritev(fs->fd, iov, iovcnt, offset); 50656d15a53SSanchit Garg #else 507cc720ddbSAneesh Kumar K.V int err = lseek(fs->fd, offset, SEEK_SET); 50856d15a53SSanchit Garg if (err == -1) { 50956d15a53SSanchit Garg return err; 51056d15a53SSanchit Garg } else { 511cc720ddbSAneesh Kumar K.V ret = writev(fs->fd, iov, iovcnt); 5128449360cSAnthony Liguori } 51356d15a53SSanchit Garg #endif 514d3ab98e6SAneesh Kumar K.V #ifdef CONFIG_SYNC_FILE_RANGE 515d3ab98e6SAneesh Kumar K.V if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) { 516d3ab98e6SAneesh Kumar K.V /* 517d3ab98e6SAneesh Kumar K.V * Initiate a writeback. This is not a data integrity sync. 518d3ab98e6SAneesh Kumar K.V * We want to ensure that we don't leave dirty pages in the cache 519d3ab98e6SAneesh Kumar K.V * after write when writeout=immediate is sepcified. 520d3ab98e6SAneesh Kumar K.V */ 521cc720ddbSAneesh Kumar K.V sync_file_range(fs->fd, offset, ret, 522d3ab98e6SAneesh Kumar K.V SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE); 523d3ab98e6SAneesh Kumar K.V } 524d3ab98e6SAneesh Kumar K.V #endif 525d3ab98e6SAneesh Kumar K.V return ret; 52656d15a53SSanchit Garg } 5278449360cSAnthony Liguori 5282289be19SAneesh Kumar K.V static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp) 529c494dd6fSAnthony Liguori { 530e3187a45SGreg Kurz char *dirpath = g_path_get_dirname(fs_path->data); 531e3187a45SGreg Kurz char *name = g_path_get_basename(fs_path->data); 5324fa4ce71SChen Gang int ret = -1; 533e3187a45SGreg Kurz int dirfd; 534e3187a45SGreg Kurz 535e3187a45SGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dirpath); 536e3187a45SGreg Kurz if (dirfd == -1) { 537e3187a45SGreg Kurz goto out; 538e3187a45SGreg Kurz } 5392289be19SAneesh Kumar K.V 540b97400caSAneesh Kumar K.V if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 541e3187a45SGreg Kurz ret = local_set_xattrat(dirfd, name, credp); 5422c30dd74SAneesh Kumar K.V } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 543e3187a45SGreg Kurz ret = local_set_mapped_file_attrat(dirfd, name, credp); 544e3187a45SGreg Kurz } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH || 545e3187a45SGreg Kurz fs_ctx->export_flags & V9FS_SM_NONE) { 546e3187a45SGreg Kurz ret = fchmodat_nofollow(dirfd, name, credp->fc_mode); 547e95ead32SVenkateswararao Jujjuri (JV) } 548e3187a45SGreg Kurz close_preserve_errno(dirfd); 549e3187a45SGreg Kurz 550e3187a45SGreg Kurz out: 551e3187a45SGreg Kurz g_free(dirpath); 552e3187a45SGreg Kurz g_free(name); 5534fa4ce71SChen Gang return ret; 554c494dd6fSAnthony Liguori } 555c494dd6fSAnthony Liguori 5562289be19SAneesh Kumar K.V static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path, 5572289be19SAneesh Kumar K.V const char *name, FsCred *credp) 558c494dd6fSAnthony Liguori { 5591c293312SVenkateswararao Jujjuri (JV) int err = -1; 560d815e721SGreg Kurz int dirfd; 5611c293312SVenkateswararao Jujjuri (JV) 562d815e721SGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dir_path->data); 563d815e721SGreg Kurz if (dirfd == -1) { 564d815e721SGreg Kurz return -1; 565d815e721SGreg Kurz } 5662289be19SAneesh Kumar K.V 567d815e721SGreg Kurz if (fs_ctx->export_flags & V9FS_SM_MAPPED || 568d815e721SGreg Kurz fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 569d815e721SGreg Kurz err = mknodat(dirfd, name, SM_LOCAL_MODE_BITS | S_IFREG, 0); 570d815e721SGreg Kurz if (err == -1) { 571d815e721SGreg Kurz goto out; 572d815e721SGreg Kurz } 573d815e721SGreg Kurz 574b97400caSAneesh Kumar K.V if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 575d815e721SGreg Kurz err = local_set_xattrat(dirfd, name, credp); 576d815e721SGreg Kurz } else { 577d815e721SGreg Kurz err = local_set_mapped_file_attrat(dirfd, name, credp); 5781c293312SVenkateswararao Jujjuri (JV) } 5791c293312SVenkateswararao Jujjuri (JV) if (err == -1) { 5801c293312SVenkateswararao Jujjuri (JV) goto err_end; 5811c293312SVenkateswararao Jujjuri (JV) } 582d815e721SGreg Kurz } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH || 583d815e721SGreg Kurz fs_ctx->export_flags & V9FS_SM_NONE) { 584d815e721SGreg Kurz err = mknodat(dirfd, name, credp->fc_mode, credp->fc_rdev); 5852c30dd74SAneesh Kumar K.V if (err == -1) { 5862c30dd74SAneesh Kumar K.V goto out; 5872c30dd74SAneesh Kumar K.V } 588d815e721SGreg Kurz err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp); 5892c30dd74SAneesh Kumar K.V if (err == -1) { 5901c293312SVenkateswararao Jujjuri (JV) goto err_end; 5911c293312SVenkateswararao Jujjuri (JV) } 5921c293312SVenkateswararao Jujjuri (JV) } 5932289be19SAneesh Kumar K.V goto out; 5941c293312SVenkateswararao Jujjuri (JV) 5951c293312SVenkateswararao Jujjuri (JV) err_end: 596d815e721SGreg Kurz unlinkat_preserve_errno(dirfd, name, 0); 5972289be19SAneesh Kumar K.V out: 598d815e721SGreg Kurz close_preserve_errno(dirfd); 5991c293312SVenkateswararao Jujjuri (JV) return err; 600c494dd6fSAnthony Liguori } 601c494dd6fSAnthony Liguori 6022289be19SAneesh Kumar K.V static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path, 6032289be19SAneesh Kumar K.V const char *name, FsCred *credp) 604c494dd6fSAnthony Liguori { 60500ec5c37SVenkateswararao Jujjuri (JV) int err = -1; 6063f3a1699SGreg Kurz int dirfd; 60700ec5c37SVenkateswararao Jujjuri (JV) 6083f3a1699SGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dir_path->data); 6093f3a1699SGreg Kurz if (dirfd == -1) { 6103f3a1699SGreg Kurz return -1; 6113f3a1699SGreg Kurz } 6122289be19SAneesh Kumar K.V 6133f3a1699SGreg Kurz if (fs_ctx->export_flags & V9FS_SM_MAPPED || 6143f3a1699SGreg Kurz fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 6153f3a1699SGreg Kurz err = mkdirat(dirfd, name, SM_LOCAL_DIR_MODE_BITS); 6163f3a1699SGreg Kurz if (err == -1) { 6173f3a1699SGreg Kurz goto out; 6183f3a1699SGreg Kurz } 6193f3a1699SGreg Kurz credp->fc_mode = credp->fc_mode | S_IFDIR; 6203f3a1699SGreg Kurz 621b97400caSAneesh Kumar K.V if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 6223f3a1699SGreg Kurz err = local_set_xattrat(dirfd, name, credp); 6233f3a1699SGreg Kurz } else { 6243f3a1699SGreg Kurz err = local_set_mapped_file_attrat(dirfd, name, credp); 62500ec5c37SVenkateswararao Jujjuri (JV) } 62600ec5c37SVenkateswararao Jujjuri (JV) if (err == -1) { 62700ec5c37SVenkateswararao Jujjuri (JV) goto err_end; 62800ec5c37SVenkateswararao Jujjuri (JV) } 6293f3a1699SGreg Kurz } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH || 6303f3a1699SGreg Kurz fs_ctx->export_flags & V9FS_SM_NONE) { 6313f3a1699SGreg Kurz err = mkdirat(dirfd, name, credp->fc_mode); 6322c30dd74SAneesh Kumar K.V if (err == -1) { 6332c30dd74SAneesh Kumar K.V goto out; 6342c30dd74SAneesh Kumar K.V } 6353f3a1699SGreg Kurz err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp); 6362c30dd74SAneesh Kumar K.V if (err == -1) { 63700ec5c37SVenkateswararao Jujjuri (JV) goto err_end; 63800ec5c37SVenkateswararao Jujjuri (JV) } 63900ec5c37SVenkateswararao Jujjuri (JV) } 6402289be19SAneesh Kumar K.V goto out; 64100ec5c37SVenkateswararao Jujjuri (JV) 64200ec5c37SVenkateswararao Jujjuri (JV) err_end: 6433f3a1699SGreg Kurz unlinkat_preserve_errno(dirfd, name, AT_REMOVEDIR); 6442289be19SAneesh Kumar K.V out: 6453f3a1699SGreg Kurz close_preserve_errno(dirfd); 64600ec5c37SVenkateswararao Jujjuri (JV) return err; 647c494dd6fSAnthony Liguori } 648c494dd6fSAnthony Liguori 6498b888272SAneesh Kumar K.V static int local_fstat(FsContext *fs_ctx, int fid_type, 650cc720ddbSAneesh Kumar K.V V9fsFidOpenState *fs, struct stat *stbuf) 651c494dd6fSAnthony Liguori { 6528b888272SAneesh Kumar K.V int err, fd; 6538b888272SAneesh Kumar K.V 6548b888272SAneesh Kumar K.V if (fid_type == P9_FID_DIR) { 655f314ea4eSGreg Kurz fd = dirfd(fs->dir.stream); 6568b888272SAneesh Kumar K.V } else { 6578b888272SAneesh Kumar K.V fd = fs->fd; 6588b888272SAneesh Kumar K.V } 6598b888272SAneesh Kumar K.V 6608b888272SAneesh Kumar K.V err = fstat(fd, stbuf); 6611237ad76SVenkateswararao Jujjuri (JV) if (err) { 6621237ad76SVenkateswararao Jujjuri (JV) return err; 6631237ad76SVenkateswararao Jujjuri (JV) } 664b97400caSAneesh Kumar K.V if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 6651237ad76SVenkateswararao Jujjuri (JV) /* Actual credentials are part of extended attrs */ 6661237ad76SVenkateswararao Jujjuri (JV) uid_t tmp_uid; 6671237ad76SVenkateswararao Jujjuri (JV) gid_t tmp_gid; 6681237ad76SVenkateswararao Jujjuri (JV) mode_t tmp_mode; 6691237ad76SVenkateswararao Jujjuri (JV) dev_t tmp_dev; 6701237ad76SVenkateswararao Jujjuri (JV) 671f8ad4a89SAneesh Kumar K.V if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) { 672f8ad4a89SAneesh Kumar K.V stbuf->st_uid = le32_to_cpu(tmp_uid); 6731237ad76SVenkateswararao Jujjuri (JV) } 674f8ad4a89SAneesh Kumar K.V if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) { 675f8ad4a89SAneesh Kumar K.V stbuf->st_gid = le32_to_cpu(tmp_gid); 6761237ad76SVenkateswararao Jujjuri (JV) } 677f8ad4a89SAneesh Kumar K.V if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) { 678f8ad4a89SAneesh Kumar K.V stbuf->st_mode = le32_to_cpu(tmp_mode); 6791237ad76SVenkateswararao Jujjuri (JV) } 680f8ad4a89SAneesh Kumar K.V if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) { 681f8ad4a89SAneesh Kumar K.V stbuf->st_rdev = le64_to_cpu(tmp_dev); 6821237ad76SVenkateswararao Jujjuri (JV) } 6832c30dd74SAneesh Kumar K.V } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 6842c30dd74SAneesh Kumar K.V errno = EOPNOTSUPP; 6852c30dd74SAneesh Kumar K.V return -1; 6861237ad76SVenkateswararao Jujjuri (JV) } 6871237ad76SVenkateswararao Jujjuri (JV) return err; 688c494dd6fSAnthony Liguori } 689c494dd6fSAnthony Liguori 6902289be19SAneesh Kumar K.V static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name, 691cc720ddbSAneesh Kumar K.V int flags, FsCred *credp, V9fsFidOpenState *fs) 692c494dd6fSAnthony Liguori { 6934750a96fSVenkateswararao Jujjuri (JV) int fd = -1; 6944750a96fSVenkateswararao Jujjuri (JV) int err = -1; 695a565fea5SGreg Kurz int dirfd; 6964750a96fSVenkateswararao Jujjuri (JV) 6970ceb092eSAneesh Kumar K.V /* 6980ceb092eSAneesh Kumar K.V * Mark all the open to not follow symlinks 6990ceb092eSAneesh Kumar K.V */ 7000ceb092eSAneesh Kumar K.V flags |= O_NOFOLLOW; 7010ceb092eSAneesh Kumar K.V 702a565fea5SGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dir_path->data); 703a565fea5SGreg Kurz if (dirfd == -1) { 704a565fea5SGreg Kurz return -1; 705a565fea5SGreg Kurz } 7062289be19SAneesh Kumar K.V 7074750a96fSVenkateswararao Jujjuri (JV) /* Determine the security model */ 708a565fea5SGreg Kurz if (fs_ctx->export_flags & V9FS_SM_MAPPED || 709a565fea5SGreg Kurz fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 710a565fea5SGreg Kurz fd = openat_file(dirfd, name, flags, SM_LOCAL_MODE_BITS); 711a565fea5SGreg Kurz if (fd == -1) { 712a565fea5SGreg Kurz goto out; 713a565fea5SGreg Kurz } 714a565fea5SGreg Kurz credp->fc_mode = credp->fc_mode|S_IFREG; 715b97400caSAneesh Kumar K.V if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 7164750a96fSVenkateswararao Jujjuri (JV) /* Set cleint credentials in xattr */ 717a565fea5SGreg Kurz err = local_set_xattrat(dirfd, name, credp); 718a565fea5SGreg Kurz } else { 719a565fea5SGreg Kurz err = local_set_mapped_file_attrat(dirfd, name, credp); 7204750a96fSVenkateswararao Jujjuri (JV) } 7212c30dd74SAneesh Kumar K.V if (err == -1) { 7222c30dd74SAneesh Kumar K.V goto err_end; 7232c30dd74SAneesh Kumar K.V } 724b97400caSAneesh Kumar K.V } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 725b97400caSAneesh Kumar K.V (fs_ctx->export_flags & V9FS_SM_NONE)) { 726a565fea5SGreg Kurz fd = openat_file(dirfd, name, flags, credp->fc_mode); 7274750a96fSVenkateswararao Jujjuri (JV) if (fd == -1) { 7282289be19SAneesh Kumar K.V goto out; 7294750a96fSVenkateswararao Jujjuri (JV) } 730a565fea5SGreg Kurz err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp); 7314750a96fSVenkateswararao Jujjuri (JV) if (err == -1) { 7324750a96fSVenkateswararao Jujjuri (JV) goto err_end; 7334750a96fSVenkateswararao Jujjuri (JV) } 7344750a96fSVenkateswararao Jujjuri (JV) } 7352289be19SAneesh Kumar K.V err = fd; 736cc720ddbSAneesh Kumar K.V fs->fd = fd; 7372289be19SAneesh Kumar K.V goto out; 7384750a96fSVenkateswararao Jujjuri (JV) 7394750a96fSVenkateswararao Jujjuri (JV) err_end: 740a565fea5SGreg Kurz unlinkat_preserve_errno(dirfd, name, 741a565fea5SGreg Kurz flags & O_DIRECTORY ? AT_REMOVEDIR : 0); 742a565fea5SGreg Kurz close_preserve_errno(fd); 7432289be19SAneesh Kumar K.V out: 744a565fea5SGreg Kurz close_preserve_errno(dirfd); 7454750a96fSVenkateswararao Jujjuri (JV) return err; 746c494dd6fSAnthony Liguori } 747c494dd6fSAnthony Liguori 748758e8e38SVenkateswararao Jujjuri (JV) 749879c2813SVenkateswararao Jujjuri (JV) static int local_symlink(FsContext *fs_ctx, const char *oldpath, 7502289be19SAneesh Kumar K.V V9fsPath *dir_path, const char *name, FsCred *credp) 751c494dd6fSAnthony Liguori { 752879c2813SVenkateswararao Jujjuri (JV) int err = -1; 75338771613SGreg Kurz int dirfd; 754879c2813SVenkateswararao Jujjuri (JV) 75538771613SGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dir_path->data); 75638771613SGreg Kurz if (dirfd == -1) { 75738771613SGreg Kurz return -1; 75838771613SGreg Kurz } 7592289be19SAneesh Kumar K.V 760879c2813SVenkateswararao Jujjuri (JV) /* Determine the security model */ 76138771613SGreg Kurz if (fs_ctx->export_flags & V9FS_SM_MAPPED || 76238771613SGreg Kurz fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 76338771613SGreg Kurz int fd; 76438771613SGreg Kurz ssize_t oldpath_size, write_size; 76538771613SGreg Kurz 76638771613SGreg Kurz fd = openat_file(dirfd, name, O_CREAT | O_EXCL | O_RDWR, 76738771613SGreg Kurz SM_LOCAL_MODE_BITS); 76838771613SGreg Kurz if (fd == -1) { 76938771613SGreg Kurz goto out; 77038771613SGreg Kurz } 77138771613SGreg Kurz /* Write the oldpath (target) to the file. */ 77238771613SGreg Kurz oldpath_size = strlen(oldpath); 77338771613SGreg Kurz do { 77438771613SGreg Kurz write_size = write(fd, (void *)oldpath, oldpath_size); 77538771613SGreg Kurz } while (write_size == -1 && errno == EINTR); 77638771613SGreg Kurz close_preserve_errno(fd); 77738771613SGreg Kurz 77838771613SGreg Kurz if (write_size != oldpath_size) { 77938771613SGreg Kurz goto err_end; 78038771613SGreg Kurz } 78138771613SGreg Kurz /* Set cleint credentials in symlink's xattr */ 78238771613SGreg Kurz credp->fc_mode = credp->fc_mode | S_IFLNK; 78338771613SGreg Kurz 784b97400caSAneesh Kumar K.V if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 78538771613SGreg Kurz err = local_set_xattrat(dirfd, name, credp); 78638771613SGreg Kurz } else { 78738771613SGreg Kurz err = local_set_mapped_file_attrat(dirfd, name, credp); 788879c2813SVenkateswararao Jujjuri (JV) } 789879c2813SVenkateswararao Jujjuri (JV) if (err == -1) { 790879c2813SVenkateswararao Jujjuri (JV) goto err_end; 791879c2813SVenkateswararao Jujjuri (JV) } 79238771613SGreg Kurz } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH || 79338771613SGreg Kurz fs_ctx->export_flags & V9FS_SM_NONE) { 79438771613SGreg Kurz err = symlinkat(oldpath, dirfd, name); 795879c2813SVenkateswararao Jujjuri (JV) if (err) { 7962289be19SAneesh Kumar K.V goto out; 797879c2813SVenkateswararao Jujjuri (JV) } 79838771613SGreg Kurz err = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid, 79938771613SGreg Kurz AT_SYMLINK_NOFOLLOW); 800879c2813SVenkateswararao Jujjuri (JV) if (err == -1) { 80112848bfcSAneesh Kumar K.V /* 80212848bfcSAneesh Kumar K.V * If we fail to change ownership and if we are 80312848bfcSAneesh Kumar K.V * using security model none. Ignore the error 80412848bfcSAneesh Kumar K.V */ 805b97400caSAneesh Kumar K.V if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) { 806879c2813SVenkateswararao Jujjuri (JV) goto err_end; 80738771613SGreg Kurz } else { 80812848bfcSAneesh Kumar K.V err = 0; 809879c2813SVenkateswararao Jujjuri (JV) } 810879c2813SVenkateswararao Jujjuri (JV) } 81138771613SGreg Kurz } 8122289be19SAneesh Kumar K.V goto out; 813879c2813SVenkateswararao Jujjuri (JV) 814879c2813SVenkateswararao Jujjuri (JV) err_end: 81538771613SGreg Kurz unlinkat_preserve_errno(dirfd, name, 0); 8162289be19SAneesh Kumar K.V out: 81738771613SGreg Kurz close_preserve_errno(dirfd); 818879c2813SVenkateswararao Jujjuri (JV) return err; 819c494dd6fSAnthony Liguori } 820c494dd6fSAnthony Liguori 8212289be19SAneesh Kumar K.V static int local_link(FsContext *ctx, V9fsPath *oldpath, 8222289be19SAneesh Kumar K.V V9fsPath *dirpath, const char *name) 823c494dd6fSAnthony Liguori { 824ad0b46e6SGreg Kurz char *odirpath = g_path_get_dirname(oldpath->data); 825ad0b46e6SGreg Kurz char *oname = g_path_get_basename(oldpath->data); 826ad0b46e6SGreg Kurz int ret = -1; 827ad0b46e6SGreg Kurz int odirfd, ndirfd; 828c494dd6fSAnthony Liguori 829ad0b46e6SGreg Kurz odirfd = local_opendir_nofollow(ctx, odirpath); 830ad0b46e6SGreg Kurz if (odirfd == -1) { 8316dd4b1f1SGreg Kurz goto out; 8326dd4b1f1SGreg Kurz } 8332289be19SAneesh Kumar K.V 834ad0b46e6SGreg Kurz ndirfd = local_opendir_nofollow(ctx, dirpath->data); 835ad0b46e6SGreg Kurz if (ndirfd == -1) { 836ad0b46e6SGreg Kurz close_preserve_errno(odirfd); 837ad0b46e6SGreg Kurz goto out; 838ad0b46e6SGreg Kurz } 839ad0b46e6SGreg Kurz 840ad0b46e6SGreg Kurz ret = linkat(odirfd, oname, ndirfd, name, 0); 841ad0b46e6SGreg Kurz if (ret < 0) { 842ad0b46e6SGreg Kurz goto out_close; 843ad0b46e6SGreg Kurz } 8442c30dd74SAneesh Kumar K.V 8452c30dd74SAneesh Kumar K.V /* now link the virtfs_metadata files */ 8466dd4b1f1SGreg Kurz if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 847ad0b46e6SGreg Kurz int omap_dirfd, nmap_dirfd; 8486dd4b1f1SGreg Kurz 849ad0b46e6SGreg Kurz ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700); 850ad0b46e6SGreg Kurz if (ret < 0 && errno != EEXIST) { 851ad0b46e6SGreg Kurz goto err_undo_link; 8522c30dd74SAneesh Kumar K.V } 853ad0b46e6SGreg Kurz 854ad0b46e6SGreg Kurz omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR); 855ad0b46e6SGreg Kurz if (omap_dirfd == -1) { 856ad0b46e6SGreg Kurz goto err; 857ad0b46e6SGreg Kurz } 858ad0b46e6SGreg Kurz 859ad0b46e6SGreg Kurz nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR); 860ad0b46e6SGreg Kurz if (nmap_dirfd == -1) { 861ad0b46e6SGreg Kurz close_preserve_errno(omap_dirfd); 862ad0b46e6SGreg Kurz goto err; 863ad0b46e6SGreg Kurz } 864ad0b46e6SGreg Kurz 865ad0b46e6SGreg Kurz ret = linkat(omap_dirfd, oname, nmap_dirfd, name, 0); 866ad0b46e6SGreg Kurz close_preserve_errno(nmap_dirfd); 867ad0b46e6SGreg Kurz close_preserve_errno(omap_dirfd); 8682c30dd74SAneesh Kumar K.V if (ret < 0 && errno != ENOENT) { 869ad0b46e6SGreg Kurz goto err_undo_link; 8702c30dd74SAneesh Kumar K.V } 8716dd4b1f1SGreg Kurz 872ad0b46e6SGreg Kurz ret = 0; 8732c30dd74SAneesh Kumar K.V } 874ad0b46e6SGreg Kurz goto out_close; 875ad0b46e6SGreg Kurz 876ad0b46e6SGreg Kurz err: 877ad0b46e6SGreg Kurz ret = -1; 878ad0b46e6SGreg Kurz err_undo_link: 879ad0b46e6SGreg Kurz unlinkat_preserve_errno(ndirfd, name, 0); 880ad0b46e6SGreg Kurz out_close: 881ad0b46e6SGreg Kurz close_preserve_errno(ndirfd); 882ad0b46e6SGreg Kurz close_preserve_errno(odirfd); 8836dd4b1f1SGreg Kurz out: 884ad0b46e6SGreg Kurz g_free(oname); 885ad0b46e6SGreg Kurz g_free(odirpath); 8862289be19SAneesh Kumar K.V return ret; 887c494dd6fSAnthony Liguori } 888c494dd6fSAnthony Liguori 8892289be19SAneesh Kumar K.V static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size) 8908cf89e00SAnthony Liguori { 891ac125d99SGreg Kurz int fd, ret; 8922289be19SAneesh Kumar K.V 893ac125d99SGreg Kurz fd = local_open_nofollow(ctx, fs_path->data, O_WRONLY, 0); 894ac125d99SGreg Kurz if (fd == -1) { 895ac125d99SGreg Kurz return -1; 896ac125d99SGreg Kurz } 897ac125d99SGreg Kurz ret = ftruncate(fd, size); 898ac125d99SGreg Kurz close_preserve_errno(fd); 8994fa4ce71SChen Gang return ret; 9008cf89e00SAnthony Liguori } 9018cf89e00SAnthony Liguori 9022289be19SAneesh Kumar K.V static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp) 9038cf89e00SAnthony Liguori { 904d369f207SGreg Kurz char *dirpath = g_path_get_dirname(fs_path->data); 905d369f207SGreg Kurz char *name = g_path_get_basename(fs_path->data); 9064fa4ce71SChen Gang int ret = -1; 907d369f207SGreg Kurz int dirfd; 908d369f207SGreg Kurz 909d369f207SGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dirpath); 910d369f207SGreg Kurz if (dirfd == -1) { 911d369f207SGreg Kurz goto out; 912d369f207SGreg Kurz } 9132289be19SAneesh Kumar K.V 914c79ce737SSripathi Kodi if ((credp->fc_uid == -1 && credp->fc_gid == -1) || 91517b1971fSAneesh Kumar K.V (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 91617b1971fSAneesh Kumar K.V (fs_ctx->export_flags & V9FS_SM_NONE)) { 917d369f207SGreg Kurz ret = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid, 918d369f207SGreg Kurz AT_SYMLINK_NOFOLLOW); 919b97400caSAneesh Kumar K.V } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 920d369f207SGreg Kurz ret = local_set_xattrat(dirfd, name, credp); 9212c30dd74SAneesh Kumar K.V } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 922d369f207SGreg Kurz ret = local_set_mapped_file_attrat(dirfd, name, credp); 923f7613beeSVenkateswararao Jujjuri (JV) } 924d369f207SGreg Kurz 925d369f207SGreg Kurz close_preserve_errno(dirfd); 926d369f207SGreg Kurz out: 927d369f207SGreg Kurz g_free(name); 928d369f207SGreg Kurz g_free(dirpath); 9294fa4ce71SChen Gang return ret; 9308cf89e00SAnthony Liguori } 9318cf89e00SAnthony Liguori 9322289be19SAneesh Kumar K.V static int local_utimensat(FsContext *s, V9fsPath *fs_path, 93374bc02b2SM. Mohan Kumar const struct timespec *buf) 9348cf89e00SAnthony Liguori { 935a33eda0dSGreg Kurz char *dirpath = g_path_get_dirname(fs_path->data); 936a33eda0dSGreg Kurz char *name = g_path_get_basename(fs_path->data); 937a33eda0dSGreg Kurz int dirfd, ret = -1; 9382289be19SAneesh Kumar K.V 939a33eda0dSGreg Kurz dirfd = local_opendir_nofollow(s, dirpath); 940a33eda0dSGreg Kurz if (dirfd == -1) { 941a33eda0dSGreg Kurz goto out; 942a33eda0dSGreg Kurz } 943a33eda0dSGreg Kurz 944a33eda0dSGreg Kurz ret = utimensat(dirfd, name, buf, AT_SYMLINK_NOFOLLOW); 945a33eda0dSGreg Kurz close_preserve_errno(dirfd); 946a33eda0dSGreg Kurz out: 947a33eda0dSGreg Kurz g_free(dirpath); 948a33eda0dSGreg Kurz g_free(name); 9494fa4ce71SChen Gang return ret; 9508cf89e00SAnthony Liguori } 9518cf89e00SAnthony Liguori 952df4938a6SGreg Kurz static int local_unlinkat_common(FsContext *ctx, int dirfd, const char *name, 953df4938a6SGreg Kurz int flags) 9545bae1900SAnthony Liguori { 955df4938a6SGreg Kurz int ret = -1; 9562c30dd74SAneesh Kumar K.V 9572c30dd74SAneesh Kumar K.V if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 958df4938a6SGreg Kurz int map_dirfd; 959df4938a6SGreg Kurz 960df4938a6SGreg Kurz if (flags == AT_REMOVEDIR) { 961df4938a6SGreg Kurz int fd; 962df4938a6SGreg Kurz 963df4938a6SGreg Kurz fd = openat(dirfd, name, O_RDONLY | O_DIRECTORY | O_PATH); 964df4938a6SGreg Kurz if (fd == -1) { 9652c30dd74SAneesh Kumar K.V goto err_out; 9662c30dd74SAneesh Kumar K.V } 9672c30dd74SAneesh Kumar K.V /* 9682c30dd74SAneesh Kumar K.V * If directory remove .virtfs_metadata contained in the 9692c30dd74SAneesh Kumar K.V * directory 9702c30dd74SAneesh Kumar K.V */ 971df4938a6SGreg Kurz ret = unlinkat(fd, VIRTFS_META_DIR, AT_REMOVEDIR); 972df4938a6SGreg Kurz close_preserve_errno(fd); 973df4938a6SGreg Kurz if (ret < 0 && errno != ENOENT) { 9742c30dd74SAneesh Kumar K.V /* 9752c30dd74SAneesh Kumar K.V * We didn't had the .virtfs_metadata file. May be file created 9762c30dd74SAneesh Kumar K.V * in non-mapped mode ?. Ignore ENOENT. 9772c30dd74SAneesh Kumar K.V */ 9782c30dd74SAneesh Kumar K.V goto err_out; 9792c30dd74SAneesh Kumar K.V } 9802c30dd74SAneesh Kumar K.V } 9812c30dd74SAneesh Kumar K.V /* 9822c30dd74SAneesh Kumar K.V * Now remove the name from parent directory 983df4938a6SGreg Kurz * .virtfs_metadata directory. 9842c30dd74SAneesh Kumar K.V */ 985df4938a6SGreg Kurz map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR); 986df4938a6SGreg Kurz ret = unlinkat(map_dirfd, name, 0); 987df4938a6SGreg Kurz close_preserve_errno(map_dirfd); 988df4938a6SGreg Kurz if (ret < 0 && errno != ENOENT) { 9892c30dd74SAneesh Kumar K.V /* 9902c30dd74SAneesh Kumar K.V * We didn't had the .virtfs_metadata file. May be file created 9912c30dd74SAneesh Kumar K.V * in non-mapped mode ?. Ignore ENOENT. 9922c30dd74SAneesh Kumar K.V */ 9932c30dd74SAneesh Kumar K.V goto err_out; 9942c30dd74SAneesh Kumar K.V } 9952c30dd74SAneesh Kumar K.V } 9964fa4ce71SChen Gang 997df4938a6SGreg Kurz ret = unlinkat(dirfd, name, flags); 9982c30dd74SAneesh Kumar K.V err_out: 999df4938a6SGreg Kurz return ret; 1000df4938a6SGreg Kurz } 1001df4938a6SGreg Kurz 10028cf89e00SAnthony Liguori static int local_remove(FsContext *ctx, const char *path) 10038cf89e00SAnthony Liguori { 10048cf89e00SAnthony Liguori struct stat stbuf; 1005a0e640a8SGreg Kurz char *dirpath = g_path_get_dirname(path); 1006a0e640a8SGreg Kurz char *name = g_path_get_basename(path); 1007a0e640a8SGreg Kurz int flags = 0; 1008a0e640a8SGreg Kurz int dirfd; 1009a0e640a8SGreg Kurz int err = -1; 10108cf89e00SAnthony Liguori 1011a0e640a8SGreg Kurz dirfd = local_opendir_nofollow(ctx, dirpath); 1012b7361d46SGreg Kurz if (dirfd == -1) { 1013a0e640a8SGreg Kurz goto out; 1014a0e640a8SGreg Kurz } 1015a0e640a8SGreg Kurz 1016a0e640a8SGreg Kurz if (fstatat(dirfd, path, &stbuf, AT_SYMLINK_NOFOLLOW) < 0) { 10178cf89e00SAnthony Liguori goto err_out; 10188cf89e00SAnthony Liguori } 1019a0e640a8SGreg Kurz 10208cf89e00SAnthony Liguori if (S_ISDIR(stbuf.st_mode)) { 1021a0e640a8SGreg Kurz flags |= AT_REMOVEDIR; 10228cf89e00SAnthony Liguori } 10235bae1900SAnthony Liguori 1024a0e640a8SGreg Kurz err = local_unlinkat_common(ctx, dirfd, name, flags); 10252c30dd74SAneesh Kumar K.V err_out: 1026a0e640a8SGreg Kurz close_preserve_errno(dirfd); 1027a0e640a8SGreg Kurz out: 1028a0e640a8SGreg Kurz g_free(name); 1029a0e640a8SGreg Kurz g_free(dirpath); 10302c30dd74SAneesh Kumar K.V return err; 10315bae1900SAnthony Liguori } 10325bae1900SAnthony Liguori 10338b888272SAneesh Kumar K.V static int local_fsync(FsContext *ctx, int fid_type, 10348b888272SAneesh Kumar K.V V9fsFidOpenState *fs, int datasync) 10358cf89e00SAnthony Liguori { 10368b888272SAneesh Kumar K.V int fd; 10378b888272SAneesh Kumar K.V 10388b888272SAneesh Kumar K.V if (fid_type == P9_FID_DIR) { 1039f314ea4eSGreg Kurz fd = dirfd(fs->dir.stream); 104049594973SVenkateswararao Jujjuri (JV) } else { 10418b888272SAneesh Kumar K.V fd = fs->fd; 10428b888272SAneesh Kumar K.V } 10438b888272SAneesh Kumar K.V 10448b888272SAneesh Kumar K.V if (datasync) { 10458b888272SAneesh Kumar K.V return qemu_fdatasync(fd); 10468b888272SAneesh Kumar K.V } else { 10478b888272SAneesh Kumar K.V return fsync(fd); 10488cf89e00SAnthony Liguori } 104949594973SVenkateswararao Jujjuri (JV) } 10508cf89e00SAnthony Liguori 10512289be19SAneesh Kumar K.V static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf) 1052be940c87SM. Mohan Kumar { 105331e51d1cSGreg Kurz int fd, ret; 10542289be19SAneesh Kumar K.V 105531e51d1cSGreg Kurz fd = local_open_nofollow(s, fs_path->data, O_RDONLY, 0); 1056*23da0145SGreg Kurz if (fd == -1) { 1057*23da0145SGreg Kurz return -1; 1058*23da0145SGreg Kurz } 105931e51d1cSGreg Kurz ret = fstatfs(fd, stbuf); 106031e51d1cSGreg Kurz close_preserve_errno(fd); 10614fa4ce71SChen Gang return ret; 1062be940c87SM. Mohan Kumar } 1063be940c87SM. Mohan Kumar 10642289be19SAneesh Kumar K.V static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path, 1065fa32ef88SAneesh Kumar K.V const char *name, void *value, size_t size) 1066fa32ef88SAneesh Kumar K.V { 10672289be19SAneesh Kumar K.V char *path = fs_path->data; 10682289be19SAneesh Kumar K.V 1069fc22118dSAneesh Kumar K.V return v9fs_get_xattr(ctx, path, name, value, size); 1070fa32ef88SAneesh Kumar K.V } 1071fa32ef88SAneesh Kumar K.V 10722289be19SAneesh Kumar K.V static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path, 1073fa32ef88SAneesh Kumar K.V void *value, size_t size) 1074fa32ef88SAneesh Kumar K.V { 10752289be19SAneesh Kumar K.V char *path = fs_path->data; 10762289be19SAneesh Kumar K.V 1077fc22118dSAneesh Kumar K.V return v9fs_list_xattr(ctx, path, value, size); 107861b6c499SAneesh Kumar K.V } 107961b6c499SAneesh Kumar K.V 10802289be19SAneesh Kumar K.V static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name, 108110b468bdSAneesh Kumar K.V void *value, size_t size, int flags) 108210b468bdSAneesh Kumar K.V { 10832289be19SAneesh Kumar K.V char *path = fs_path->data; 10842289be19SAneesh Kumar K.V 1085fc22118dSAneesh Kumar K.V return v9fs_set_xattr(ctx, path, name, value, size, flags); 108610b468bdSAneesh Kumar K.V } 108710b468bdSAneesh Kumar K.V 10882289be19SAneesh Kumar K.V static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path, 10892289be19SAneesh Kumar K.V const char *name) 10909ed3ef26SAneesh Kumar K.V { 10912289be19SAneesh Kumar K.V char *path = fs_path->data; 10922289be19SAneesh Kumar K.V 1093fc22118dSAneesh Kumar K.V return v9fs_remove_xattr(ctx, path, name); 10949ed3ef26SAneesh Kumar K.V } 10959ed3ef26SAneesh Kumar K.V 10962289be19SAneesh Kumar K.V static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path, 10972289be19SAneesh Kumar K.V const char *name, V9fsPath *target) 10982289be19SAneesh Kumar K.V { 10992289be19SAneesh Kumar K.V if (dir_path) { 1100e3e83f2eSGreg Kurz v9fs_path_sprintf(target, "%s/%s", dir_path->data, name); 11012289be19SAneesh Kumar K.V } else { 1102e3e83f2eSGreg Kurz v9fs_path_sprintf(target, "%s", name); 11032289be19SAneesh Kumar K.V } 11042289be19SAneesh Kumar K.V return 0; 11052289be19SAneesh Kumar K.V } 11062289be19SAneesh Kumar K.V 11072289be19SAneesh Kumar K.V static int local_renameat(FsContext *ctx, V9fsPath *olddir, 11082289be19SAneesh Kumar K.V const char *old_name, V9fsPath *newdir, 11092289be19SAneesh Kumar K.V const char *new_name) 11102289be19SAneesh Kumar K.V { 11112289be19SAneesh Kumar K.V int ret; 111299f2cf4bSGreg Kurz int odirfd, ndirfd; 11132289be19SAneesh Kumar K.V 111499f2cf4bSGreg Kurz odirfd = local_opendir_nofollow(ctx, olddir->data); 111599f2cf4bSGreg Kurz if (odirfd == -1) { 111699f2cf4bSGreg Kurz return -1; 111799f2cf4bSGreg Kurz } 11182289be19SAneesh Kumar K.V 111999f2cf4bSGreg Kurz ndirfd = local_opendir_nofollow(ctx, newdir->data); 112099f2cf4bSGreg Kurz if (ndirfd == -1) { 112199f2cf4bSGreg Kurz close_preserve_errno(odirfd); 112299f2cf4bSGreg Kurz return -1; 112399f2cf4bSGreg Kurz } 11242289be19SAneesh Kumar K.V 112599f2cf4bSGreg Kurz ret = renameat(odirfd, old_name, ndirfd, new_name); 112699f2cf4bSGreg Kurz if (ret < 0) { 112799f2cf4bSGreg Kurz goto out; 112899f2cf4bSGreg Kurz } 112999f2cf4bSGreg Kurz 113099f2cf4bSGreg Kurz if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 113199f2cf4bSGreg Kurz int omap_dirfd, nmap_dirfd; 113299f2cf4bSGreg Kurz 113399f2cf4bSGreg Kurz ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700); 113499f2cf4bSGreg Kurz if (ret < 0 && errno != EEXIST) { 113599f2cf4bSGreg Kurz goto err_undo_rename; 113699f2cf4bSGreg Kurz } 113799f2cf4bSGreg Kurz 11386dd4b1f1SGreg Kurz omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR); 113999f2cf4bSGreg Kurz if (omap_dirfd == -1) { 114099f2cf4bSGreg Kurz goto err; 114199f2cf4bSGreg Kurz } 114299f2cf4bSGreg Kurz 11436dd4b1f1SGreg Kurz nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR); 114499f2cf4bSGreg Kurz if (nmap_dirfd == -1) { 114599f2cf4bSGreg Kurz close_preserve_errno(omap_dirfd); 114699f2cf4bSGreg Kurz goto err; 114799f2cf4bSGreg Kurz } 114899f2cf4bSGreg Kurz 114999f2cf4bSGreg Kurz /* rename the .virtfs_metadata files */ 115099f2cf4bSGreg Kurz ret = renameat(omap_dirfd, old_name, nmap_dirfd, new_name); 115199f2cf4bSGreg Kurz close_preserve_errno(nmap_dirfd); 115299f2cf4bSGreg Kurz close_preserve_errno(omap_dirfd); 115399f2cf4bSGreg Kurz if (ret < 0 && errno != ENOENT) { 115499f2cf4bSGreg Kurz goto err_undo_rename; 115599f2cf4bSGreg Kurz } 115699f2cf4bSGreg Kurz 115799f2cf4bSGreg Kurz ret = 0; 115899f2cf4bSGreg Kurz } 115999f2cf4bSGreg Kurz goto out; 116099f2cf4bSGreg Kurz 116199f2cf4bSGreg Kurz err: 116299f2cf4bSGreg Kurz ret = -1; 116399f2cf4bSGreg Kurz err_undo_rename: 116499f2cf4bSGreg Kurz renameat_preserve_errno(ndirfd, new_name, odirfd, old_name); 116599f2cf4bSGreg Kurz out: 116699f2cf4bSGreg Kurz close_preserve_errno(ndirfd); 116799f2cf4bSGreg Kurz close_preserve_errno(odirfd); 11682289be19SAneesh Kumar K.V return ret; 11692289be19SAneesh Kumar K.V } 11702289be19SAneesh Kumar K.V 1171d2767edeSGreg Kurz static void v9fs_path_init_dirname(V9fsPath *path, const char *str) 1172d2767edeSGreg Kurz { 1173d2767edeSGreg Kurz path->data = g_path_get_dirname(str); 1174d2767edeSGreg Kurz path->size = strlen(path->data) + 1; 1175d2767edeSGreg Kurz } 1176d2767edeSGreg Kurz 1177d2767edeSGreg Kurz static int local_rename(FsContext *ctx, const char *oldpath, 1178d2767edeSGreg Kurz const char *newpath) 1179d2767edeSGreg Kurz { 1180d2767edeSGreg Kurz int err; 1181d2767edeSGreg Kurz char *oname = g_path_get_basename(oldpath); 1182d2767edeSGreg Kurz char *nname = g_path_get_basename(newpath); 1183d2767edeSGreg Kurz V9fsPath olddir, newdir; 1184d2767edeSGreg Kurz 1185d2767edeSGreg Kurz v9fs_path_init_dirname(&olddir, oldpath); 1186d2767edeSGreg Kurz v9fs_path_init_dirname(&newdir, newpath); 1187d2767edeSGreg Kurz 1188d2767edeSGreg Kurz err = local_renameat(ctx, &olddir, oname, &newdir, nname); 1189d2767edeSGreg Kurz 1190d2767edeSGreg Kurz v9fs_path_free(&newdir); 1191d2767edeSGreg Kurz v9fs_path_free(&olddir); 1192d2767edeSGreg Kurz g_free(nname); 1193d2767edeSGreg Kurz g_free(oname); 1194d2767edeSGreg Kurz 1195d2767edeSGreg Kurz return err; 1196d2767edeSGreg Kurz } 1197d2767edeSGreg Kurz 11982289be19SAneesh Kumar K.V static int local_unlinkat(FsContext *ctx, V9fsPath *dir, 11992289be19SAneesh Kumar K.V const char *name, int flags) 12002289be19SAneesh Kumar K.V { 12012289be19SAneesh Kumar K.V int ret; 1202df4938a6SGreg Kurz int dirfd; 12032c30dd74SAneesh Kumar K.V 1204df4938a6SGreg Kurz dirfd = local_opendir_nofollow(ctx, dir->data); 1205df4938a6SGreg Kurz if (dirfd == -1) { 1206df4938a6SGreg Kurz return -1; 1207df4938a6SGreg Kurz } 12082289be19SAneesh Kumar K.V 1209df4938a6SGreg Kurz ret = local_unlinkat_common(ctx, dirfd, name, flags); 1210df4938a6SGreg Kurz close_preserve_errno(dirfd); 12112289be19SAneesh Kumar K.V return ret; 12122289be19SAneesh Kumar K.V } 12139ed3ef26SAneesh Kumar K.V 1214e06a765eSHarsh Prateek Bora static int local_ioc_getversion(FsContext *ctx, V9fsPath *path, 1215e06a765eSHarsh Prateek Bora mode_t st_mode, uint64_t *st_gen) 1216e06a765eSHarsh Prateek Bora { 1217ae0f940eSPaolo Bonzini #ifdef FS_IOC_GETVERSION 12180e5fc994SKirill A. Shutemov int err; 1219cc720ddbSAneesh Kumar K.V V9fsFidOpenState fid_open; 1220cc720ddbSAneesh Kumar K.V 1221e06a765eSHarsh Prateek Bora /* 1222e06a765eSHarsh Prateek Bora * Do not try to open special files like device nodes, fifos etc 1223e06a765eSHarsh Prateek Bora * We can get fd for regular files and directories only 1224e06a765eSHarsh Prateek Bora */ 1225e06a765eSHarsh Prateek Bora if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) { 12261a9978a5SKirill A. Shutemov errno = ENOTTY; 12271a9978a5SKirill A. Shutemov return -1; 1228e06a765eSHarsh Prateek Bora } 1229cc720ddbSAneesh Kumar K.V err = local_open(ctx, path, O_RDONLY, &fid_open); 1230cc720ddbSAneesh Kumar K.V if (err < 0) { 1231cc720ddbSAneesh Kumar K.V return err; 1232e06a765eSHarsh Prateek Bora } 1233cc720ddbSAneesh Kumar K.V err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen); 1234cc720ddbSAneesh Kumar K.V local_close(ctx, &fid_open); 1235e06a765eSHarsh Prateek Bora return err; 12360e5fc994SKirill A. Shutemov #else 12370e5fc994SKirill A. Shutemov errno = ENOTTY; 12380e5fc994SKirill A. Shutemov return -1; 12390e5fc994SKirill A. Shutemov #endif 1240e06a765eSHarsh Prateek Bora } 1241e06a765eSHarsh Prateek Bora 12420174fe73SAneesh Kumar K.V static int local_init(FsContext *ctx) 12430174fe73SAneesh Kumar K.V { 1244e06a765eSHarsh Prateek Bora struct statfs stbuf; 12450e35a378SGreg Kurz LocalData *data = g_malloc(sizeof(*data)); 12460e35a378SGreg Kurz 12470e35a378SGreg Kurz data->mountfd = open(ctx->fs_root, O_DIRECTORY | O_RDONLY); 12480e35a378SGreg Kurz if (data->mountfd == -1) { 12490e35a378SGreg Kurz goto err; 12500e35a378SGreg Kurz } 1251e06a765eSHarsh Prateek Bora 125200c90bd1SGreg Kurz #ifdef FS_IOC_GETVERSION 125300c90bd1SGreg Kurz /* 125400c90bd1SGreg Kurz * use ioc_getversion only if the ioctl is definied 125500c90bd1SGreg Kurz */ 12560e35a378SGreg Kurz if (fstatfs(data->mountfd, &stbuf) < 0) { 12570e35a378SGreg Kurz close_preserve_errno(data->mountfd); 12580e35a378SGreg Kurz goto err; 125900c90bd1SGreg Kurz } 126000c90bd1SGreg Kurz switch (stbuf.f_type) { 126100c90bd1SGreg Kurz case EXT2_SUPER_MAGIC: 126200c90bd1SGreg Kurz case BTRFS_SUPER_MAGIC: 126300c90bd1SGreg Kurz case REISERFS_SUPER_MAGIC: 126400c90bd1SGreg Kurz case XFS_SUPER_MAGIC: 126500c90bd1SGreg Kurz ctx->exops.get_st_gen = local_ioc_getversion; 126600c90bd1SGreg Kurz break; 126700c90bd1SGreg Kurz } 126800c90bd1SGreg Kurz #endif 12690174fe73SAneesh Kumar K.V 12702c30dd74SAneesh Kumar K.V if (ctx->export_flags & V9FS_SM_PASSTHROUGH) { 12712c30dd74SAneesh Kumar K.V ctx->xops = passthrough_xattr_ops; 12722c30dd74SAneesh Kumar K.V } else if (ctx->export_flags & V9FS_SM_MAPPED) { 12732c30dd74SAneesh Kumar K.V ctx->xops = mapped_xattr_ops; 12742c30dd74SAneesh Kumar K.V } else if (ctx->export_flags & V9FS_SM_NONE) { 12752c30dd74SAneesh Kumar K.V ctx->xops = none_xattr_ops; 12762c30dd74SAneesh Kumar K.V } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 12772c30dd74SAneesh Kumar K.V /* 12782c30dd74SAneesh Kumar K.V * xattr operation for mapped-file and passthrough 12792c30dd74SAneesh Kumar K.V * remain same. 12802c30dd74SAneesh Kumar K.V */ 12812c30dd74SAneesh Kumar K.V ctx->xops = passthrough_xattr_ops; 12822c30dd74SAneesh Kumar K.V } 1283c98f1d4aSAneesh Kumar K.V ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT; 128400c90bd1SGreg Kurz 12850e35a378SGreg Kurz ctx->private = data; 128600c90bd1SGreg Kurz return 0; 12870e35a378SGreg Kurz 12880e35a378SGreg Kurz err: 12890e35a378SGreg Kurz g_free(data); 12900e35a378SGreg Kurz return -1; 1291e06a765eSHarsh Prateek Bora } 12920e35a378SGreg Kurz 12930e35a378SGreg Kurz static void local_cleanup(FsContext *ctx) 12940e35a378SGreg Kurz { 12950e35a378SGreg Kurz LocalData *data = ctx->private; 12960e35a378SGreg Kurz 12970e35a378SGreg Kurz close(data->mountfd); 12980e35a378SGreg Kurz g_free(data); 12990174fe73SAneesh Kumar K.V } 13000174fe73SAneesh Kumar K.V 130199519f0aSAneesh Kumar K.V static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse) 130299519f0aSAneesh Kumar K.V { 130399519f0aSAneesh Kumar K.V const char *sec_model = qemu_opt_get(opts, "security_model"); 130499519f0aSAneesh Kumar K.V const char *path = qemu_opt_get(opts, "path"); 1305b8bbdb88SPradeep Jagadeesh Error *err = NULL; 130699519f0aSAneesh Kumar K.V 130799519f0aSAneesh Kumar K.V if (!sec_model) { 130863325b18SGreg Kurz error_report("Security model not specified, local fs needs security model"); 130963325b18SGreg Kurz error_printf("valid options are:" 131063325b18SGreg Kurz "\tsecurity_model=[passthrough|mapped-xattr|mapped-file|none]\n"); 131199519f0aSAneesh Kumar K.V return -1; 131299519f0aSAneesh Kumar K.V } 131399519f0aSAneesh Kumar K.V 131499519f0aSAneesh Kumar K.V if (!strcmp(sec_model, "passthrough")) { 131599519f0aSAneesh Kumar K.V fse->export_flags |= V9FS_SM_PASSTHROUGH; 13162c30dd74SAneesh Kumar K.V } else if (!strcmp(sec_model, "mapped") || 13172c30dd74SAneesh Kumar K.V !strcmp(sec_model, "mapped-xattr")) { 131899519f0aSAneesh Kumar K.V fse->export_flags |= V9FS_SM_MAPPED; 131999519f0aSAneesh Kumar K.V } else if (!strcmp(sec_model, "none")) { 132099519f0aSAneesh Kumar K.V fse->export_flags |= V9FS_SM_NONE; 13212c30dd74SAneesh Kumar K.V } else if (!strcmp(sec_model, "mapped-file")) { 13222c30dd74SAneesh Kumar K.V fse->export_flags |= V9FS_SM_MAPPED_FILE; 132399519f0aSAneesh Kumar K.V } else { 132463325b18SGreg Kurz error_report("Invalid security model %s specified", sec_model); 132563325b18SGreg Kurz error_printf("valid options are:" 132663325b18SGreg Kurz "\t[passthrough|mapped-xattr|mapped-file|none]\n"); 132799519f0aSAneesh Kumar K.V return -1; 132899519f0aSAneesh Kumar K.V } 132999519f0aSAneesh Kumar K.V 133099519f0aSAneesh Kumar K.V if (!path) { 133163325b18SGreg Kurz error_report("fsdev: No path specified"); 133299519f0aSAneesh Kumar K.V return -1; 133399519f0aSAneesh Kumar K.V } 1334b8bbdb88SPradeep Jagadeesh 1335b8bbdb88SPradeep Jagadeesh fsdev_throttle_parse_opts(opts, &fse->fst, &err); 1336b8bbdb88SPradeep Jagadeesh if (err) { 1337b8bbdb88SPradeep Jagadeesh error_reportf_err(err, "Throttle configuration is not valid: "); 1338b8bbdb88SPradeep Jagadeesh return -1; 1339b8bbdb88SPradeep Jagadeesh } 1340b8bbdb88SPradeep Jagadeesh 134199519f0aSAneesh Kumar K.V fse->path = g_strdup(path); 134299519f0aSAneesh Kumar K.V 134399519f0aSAneesh Kumar K.V return 0; 134499519f0aSAneesh Kumar K.V } 134599519f0aSAneesh Kumar K.V 13469f107513SAnthony Liguori FileOperations local_ops = { 134799519f0aSAneesh Kumar K.V .parse_opts = local_parse_opts, 13480174fe73SAneesh Kumar K.V .init = local_init, 13490e35a378SGreg Kurz .cleanup = local_cleanup, 1350131dcb25SAnthony Liguori .lstat = local_lstat, 1351131dcb25SAnthony Liguori .readlink = local_readlink, 1352131dcb25SAnthony Liguori .close = local_close, 1353131dcb25SAnthony Liguori .closedir = local_closedir, 1354a6568fe2SAnthony Liguori .open = local_open, 1355a6568fe2SAnthony Liguori .opendir = local_opendir, 1356a9231555SAnthony Liguori .rewinddir = local_rewinddir, 1357a9231555SAnthony Liguori .telldir = local_telldir, 1358635324e8SGreg Kurz .readdir = local_readdir, 1359a9231555SAnthony Liguori .seekdir = local_seekdir, 136056d15a53SSanchit Garg .preadv = local_preadv, 136156d15a53SSanchit Garg .pwritev = local_pwritev, 1362c494dd6fSAnthony Liguori .chmod = local_chmod, 1363c494dd6fSAnthony Liguori .mknod = local_mknod, 1364c494dd6fSAnthony Liguori .mkdir = local_mkdir, 1365c494dd6fSAnthony Liguori .fstat = local_fstat, 1366c494dd6fSAnthony Liguori .open2 = local_open2, 1367c494dd6fSAnthony Liguori .symlink = local_symlink, 1368c494dd6fSAnthony Liguori .link = local_link, 13698cf89e00SAnthony Liguori .truncate = local_truncate, 13708cf89e00SAnthony Liguori .rename = local_rename, 13718cf89e00SAnthony Liguori .chown = local_chown, 137274bc02b2SM. Mohan Kumar .utimensat = local_utimensat, 13735bae1900SAnthony Liguori .remove = local_remove, 13748cf89e00SAnthony Liguori .fsync = local_fsync, 1375be940c87SM. Mohan Kumar .statfs = local_statfs, 1376fa32ef88SAneesh Kumar K.V .lgetxattr = local_lgetxattr, 1377fa32ef88SAneesh Kumar K.V .llistxattr = local_llistxattr, 137810b468bdSAneesh Kumar K.V .lsetxattr = local_lsetxattr, 13799ed3ef26SAneesh Kumar K.V .lremovexattr = local_lremovexattr, 13802289be19SAneesh Kumar K.V .name_to_path = local_name_to_path, 13812289be19SAneesh Kumar K.V .renameat = local_renameat, 13822289be19SAneesh Kumar K.V .unlinkat = local_unlinkat, 13839f107513SAnthony Liguori }; 1384