1f00d4f59SWei Liu /* 2f00d4f59SWei Liu * 9p Posix callback 3f00d4f59SWei Liu * 4f00d4f59SWei Liu * Copyright IBM, Corp. 2010 5f00d4f59SWei Liu * 6f00d4f59SWei Liu * Authors: 7f00d4f59SWei Liu * Anthony Liguori <aliguori@us.ibm.com> 8f00d4f59SWei Liu * 9f00d4f59SWei Liu * This work is licensed under the terms of the GNU GPL, version 2. See 10f00d4f59SWei Liu * the COPYING file in the top-level directory. 11f00d4f59SWei Liu * 12f00d4f59SWei Liu */ 13f00d4f59SWei Liu 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" 19f00d4f59SWei Liu #include "fsdev/qemu-fsdev.h" /* local_ops */ 20f00d4f59SWei Liu #include <arpa/inet.h> 21f00d4f59SWei Liu #include <pwd.h> 22f00d4f59SWei Liu #include <grp.h> 23f00d4f59SWei Liu #include <sys/socket.h> 24f00d4f59SWei Liu #include <sys/un.h> 25f00d4f59SWei Liu #include "qemu/xattr.h" 26f348b6d1SVeronia Bahaa #include "qemu/cutils.h" 2763325b18SGreg Kurz #include "qemu/error-report.h" 28f00d4f59SWei Liu #include <libgen.h> 29f00d4f59SWei Liu #include <linux/fs.h> 30f00d4f59SWei Liu #ifdef CONFIG_LINUX_MAGIC_H 31f00d4f59SWei Liu #include <linux/magic.h> 32f00d4f59SWei Liu #endif 33f00d4f59SWei Liu #include <sys/ioctl.h> 34f00d4f59SWei Liu 35f00d4f59SWei Liu #ifndef XFS_SUPER_MAGIC 36f00d4f59SWei Liu #define XFS_SUPER_MAGIC 0x58465342 37f00d4f59SWei Liu #endif 38f00d4f59SWei Liu #ifndef EXT2_SUPER_MAGIC 39f00d4f59SWei Liu #define EXT2_SUPER_MAGIC 0xEF53 40f00d4f59SWei Liu #endif 41f00d4f59SWei Liu #ifndef REISERFS_SUPER_MAGIC 42f00d4f59SWei Liu #define REISERFS_SUPER_MAGIC 0x52654973 43f00d4f59SWei Liu #endif 44f00d4f59SWei Liu #ifndef BTRFS_SUPER_MAGIC 45f00d4f59SWei Liu #define BTRFS_SUPER_MAGIC 0x9123683E 46f00d4f59SWei Liu #endif 47f00d4f59SWei Liu 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 85f00d4f59SWei Liu #define VIRTFS_META_DIR ".virtfs_metadata" 86f00d4f59SWei Liu 87f9aef99bSGreg Kurz static FILE *local_fopenat(int dirfd, const char *name, const char *mode) 88f00d4f59SWei Liu { 89f00d4f59SWei Liu int fd, o_mode = 0; 90f00d4f59SWei Liu FILE *fp; 91f9aef99bSGreg Kurz int flags; 92f00d4f59SWei Liu /* 93f00d4f59SWei Liu * only supports two modes 94f00d4f59SWei Liu */ 95f00d4f59SWei Liu if (mode[0] == 'r') { 96f9aef99bSGreg Kurz flags = O_RDONLY; 97f00d4f59SWei Liu } else if (mode[0] == 'w') { 98f9aef99bSGreg Kurz flags = O_WRONLY | O_TRUNC | O_CREAT; 99f00d4f59SWei Liu o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; 100f00d4f59SWei Liu } else { 101f00d4f59SWei Liu return NULL; 102f00d4f59SWei Liu } 103f9aef99bSGreg Kurz fd = openat_file(dirfd, name, flags, o_mode); 104f00d4f59SWei Liu if (fd == -1) { 105f00d4f59SWei Liu return NULL; 106f00d4f59SWei Liu } 107f00d4f59SWei Liu fp = fdopen(fd, mode); 108f00d4f59SWei Liu if (!fp) { 109f00d4f59SWei Liu close(fd); 110f00d4f59SWei Liu } 111f00d4f59SWei Liu return fp; 112f00d4f59SWei Liu } 113f00d4f59SWei Liu 114f00d4f59SWei Liu #define ATTR_MAX 100 115f9aef99bSGreg Kurz static void local_mapped_file_attr(int dirfd, const char *name, 116f00d4f59SWei Liu struct stat *stbuf) 117f00d4f59SWei Liu { 118f00d4f59SWei Liu FILE *fp; 119f00d4f59SWei Liu char buf[ATTR_MAX]; 120f9aef99bSGreg Kurz int map_dirfd; 121f00d4f59SWei Liu 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); 129f00d4f59SWei Liu if (!fp) { 130f00d4f59SWei Liu return; 131f00d4f59SWei Liu } 132f00d4f59SWei Liu memset(buf, 0, ATTR_MAX); 133f00d4f59SWei Liu while (fgets(buf, ATTR_MAX, fp)) { 134f00d4f59SWei Liu if (!strncmp(buf, "virtfs.uid", 10)) { 135f00d4f59SWei Liu stbuf->st_uid = atoi(buf+11); 136f00d4f59SWei Liu } else if (!strncmp(buf, "virtfs.gid", 10)) { 137f00d4f59SWei Liu stbuf->st_gid = atoi(buf+11); 138f00d4f59SWei Liu } else if (!strncmp(buf, "virtfs.mode", 11)) { 139f00d4f59SWei Liu stbuf->st_mode = atoi(buf+12); 140f00d4f59SWei Liu } else if (!strncmp(buf, "virtfs.rdev", 11)) { 141f00d4f59SWei Liu stbuf->st_rdev = atoi(buf+12); 142f00d4f59SWei Liu } 143f00d4f59SWei Liu memset(buf, 0, ATTR_MAX); 144f00d4f59SWei Liu } 145f00d4f59SWei Liu fclose(fp); 146f00d4f59SWei Liu } 147f00d4f59SWei Liu 148f00d4f59SWei Liu static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf) 149f00d4f59SWei Liu { 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; 154f00d4f59SWei Liu 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); 161f00d4f59SWei Liu if (err) { 162f00d4f59SWei Liu goto err_out; 163f00d4f59SWei Liu } 164f00d4f59SWei Liu if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 165f00d4f59SWei Liu /* Actual credentials are part of extended attrs */ 166f00d4f59SWei Liu uid_t tmp_uid; 167f00d4f59SWei Liu gid_t tmp_gid; 168f00d4f59SWei Liu mode_t tmp_mode; 169f00d4f59SWei Liu dev_t tmp_dev; 170f9aef99bSGreg Kurz 171f9aef99bSGreg Kurz if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.uid", &tmp_uid, 172f9aef99bSGreg Kurz sizeof(uid_t)) > 0) { 173f00d4f59SWei Liu stbuf->st_uid = le32_to_cpu(tmp_uid); 174f00d4f59SWei Liu } 175f9aef99bSGreg Kurz if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.gid", &tmp_gid, 176f9aef99bSGreg Kurz sizeof(gid_t)) > 0) { 177f00d4f59SWei Liu stbuf->st_gid = le32_to_cpu(tmp_gid); 178f00d4f59SWei Liu } 179f9aef99bSGreg Kurz if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.mode", &tmp_mode, 180f9aef99bSGreg Kurz sizeof(mode_t)) > 0) { 181f00d4f59SWei Liu stbuf->st_mode = le32_to_cpu(tmp_mode); 182f00d4f59SWei Liu } 183f9aef99bSGreg Kurz if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.rdev", &tmp_dev, 184f9aef99bSGreg Kurz sizeof(dev_t)) > 0) { 185f00d4f59SWei Liu stbuf->st_rdev = le64_to_cpu(tmp_dev); 186f00d4f59SWei Liu } 187f00d4f59SWei Liu } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 188f9aef99bSGreg Kurz local_mapped_file_attr(dirfd, name, stbuf); 189f00d4f59SWei Liu } 190f00d4f59SWei Liu 191f00d4f59SWei Liu err_out: 192f9aef99bSGreg Kurz close_preserve_errno(dirfd); 193f9aef99bSGreg Kurz out: 194f9aef99bSGreg Kurz g_free(name); 195f9aef99bSGreg Kurz g_free(dirpath); 196f00d4f59SWei Liu return err; 197f00d4f59SWei Liu } 198f00d4f59SWei Liu 199e3187a45SGreg Kurz static int local_set_mapped_file_attrat(int dirfd, const char *name, 200e3187a45SGreg Kurz FsCred *credp) 201f00d4f59SWei Liu { 202f00d4f59SWei Liu FILE *fp; 203e3187a45SGreg Kurz int ret; 204f00d4f59SWei Liu char buf[ATTR_MAX]; 205f00d4f59SWei Liu int uid = -1, gid = -1, mode = -1, rdev = -1; 206e3187a45SGreg Kurz int map_dirfd; 207f00d4f59SWei Liu 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"); 219f00d4f59SWei Liu 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 } 226f00d4f59SWei Liu } 227f00d4f59SWei Liu memset(buf, 0, ATTR_MAX); 228f00d4f59SWei Liu while (fgets(buf, ATTR_MAX, fp)) { 229f00d4f59SWei Liu if (!strncmp(buf, "virtfs.uid", 10)) { 230f00d4f59SWei Liu uid = atoi(buf + 11); 231f00d4f59SWei Liu } else if (!strncmp(buf, "virtfs.gid", 10)) { 232f00d4f59SWei Liu gid = atoi(buf + 11); 233f00d4f59SWei Liu } else if (!strncmp(buf, "virtfs.mode", 11)) { 234f00d4f59SWei Liu mode = atoi(buf + 12); 235f00d4f59SWei Liu } else if (!strncmp(buf, "virtfs.rdev", 11)) { 236f00d4f59SWei Liu rdev = atoi(buf + 12); 237f00d4f59SWei Liu } 238f00d4f59SWei Liu memset(buf, 0, ATTR_MAX); 239f00d4f59SWei Liu } 240f00d4f59SWei Liu fclose(fp); 241f00d4f59SWei Liu 242f00d4f59SWei Liu update_map_file: 243e3187a45SGreg Kurz fp = local_fopenat(map_dirfd, name, "w"); 244e3187a45SGreg Kurz close_preserve_errno(map_dirfd); 245f00d4f59SWei Liu if (!fp) { 246e3187a45SGreg Kurz return -1; 247f00d4f59SWei Liu } 248f00d4f59SWei Liu 249f00d4f59SWei Liu if (credp->fc_uid != -1) { 250f00d4f59SWei Liu uid = credp->fc_uid; 251f00d4f59SWei Liu } 252f00d4f59SWei Liu if (credp->fc_gid != -1) { 253f00d4f59SWei Liu gid = credp->fc_gid; 254f00d4f59SWei Liu } 255f00d4f59SWei Liu if (credp->fc_mode != -1) { 256f00d4f59SWei Liu mode = credp->fc_mode; 257f00d4f59SWei Liu } 258f00d4f59SWei Liu if (credp->fc_rdev != -1) { 259f00d4f59SWei Liu rdev = credp->fc_rdev; 260f00d4f59SWei Liu } 261f00d4f59SWei Liu 262f00d4f59SWei Liu if (uid != -1) { 263f00d4f59SWei Liu fprintf(fp, "virtfs.uid=%d\n", uid); 264f00d4f59SWei Liu } 265f00d4f59SWei Liu if (gid != -1) { 266f00d4f59SWei Liu fprintf(fp, "virtfs.gid=%d\n", gid); 267f00d4f59SWei Liu } 268f00d4f59SWei Liu if (mode != -1) { 269f00d4f59SWei Liu fprintf(fp, "virtfs.mode=%d\n", mode); 270f00d4f59SWei Liu } 271f00d4f59SWei Liu if (rdev != -1) { 272f00d4f59SWei Liu fprintf(fp, "virtfs.rdev=%d\n", rdev); 273f00d4f59SWei Liu } 274f00d4f59SWei Liu fclose(fp); 275f00d4f59SWei Liu 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); 306f00d4f59SWei Liu return ret; 307f00d4f59SWei Liu } 308f00d4f59SWei Liu 309e3187a45SGreg Kurz static int local_set_xattrat(int dirfd, const char *path, FsCred *credp) 310f00d4f59SWei Liu { 311f00d4f59SWei Liu int err; 312f00d4f59SWei Liu 313f00d4f59SWei Liu if (credp->fc_uid != -1) { 314f00d4f59SWei Liu 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); 317f00d4f59SWei Liu if (err) { 318f00d4f59SWei Liu return err; 319f00d4f59SWei Liu } 320f00d4f59SWei Liu } 321f00d4f59SWei Liu if (credp->fc_gid != -1) { 322f00d4f59SWei Liu 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); 325f00d4f59SWei Liu if (err) { 326f00d4f59SWei Liu return err; 327f00d4f59SWei Liu } 328f00d4f59SWei Liu } 329f00d4f59SWei Liu if (credp->fc_mode != -1) { 330f00d4f59SWei Liu 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); 333f00d4f59SWei Liu if (err) { 334f00d4f59SWei Liu return err; 335f00d4f59SWei Liu } 336f00d4f59SWei Liu } 337f00d4f59SWei Liu if (credp->fc_rdev != -1) { 338f00d4f59SWei Liu 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); 341f00d4f59SWei Liu if (err) { 342f00d4f59SWei Liu return err; 343f00d4f59SWei Liu } 344f00d4f59SWei Liu } 345f00d4f59SWei Liu return 0; 346f00d4f59SWei Liu } 347f00d4f59SWei Liu 348d815e721SGreg Kurz static int local_set_cred_passthrough(FsContext *fs_ctx, int dirfd, 349d815e721SGreg Kurz const char *name, FsCred *credp) 350f00d4f59SWei Liu { 351d815e721SGreg Kurz if (fchownat(dirfd, name, credp->fc_uid, credp->fc_gid, 352b314f6a0SGreg Kurz AT_SYMLINK_NOFOLLOW) < 0) { 353f00d4f59SWei Liu /* 354f00d4f59SWei Liu * If we fail to change ownership and if we are 355f00d4f59SWei Liu * using security model none. Ignore the error 356f00d4f59SWei Liu */ 357f00d4f59SWei Liu if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) { 358f00d4f59SWei Liu return -1; 359f00d4f59SWei Liu } 360d815e721SGreg Kurz } 361d815e721SGreg Kurz 362d815e721SGreg Kurz return fchmodat_nofollow(dirfd, name, credp->fc_mode & 07777); 363d815e721SGreg Kurz } 364f00d4f59SWei Liu 365f00d4f59SWei Liu static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path, 366f00d4f59SWei Liu char *buf, size_t bufsz) 367f00d4f59SWei Liu { 368f00d4f59SWei Liu ssize_t tsize = -1; 369f00d4f59SWei Liu 370f00d4f59SWei Liu if ((fs_ctx->export_flags & V9FS_SM_MAPPED) || 371f00d4f59SWei Liu (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) { 372f00d4f59SWei Liu int fd; 373bec1e954SGreg Kurz 374bec1e954SGreg Kurz fd = local_open_nofollow(fs_ctx, fs_path->data, O_RDONLY, 0); 375f00d4f59SWei Liu if (fd == -1) { 376f00d4f59SWei Liu return -1; 377f00d4f59SWei Liu } 378f00d4f59SWei Liu do { 379f00d4f59SWei Liu tsize = read(fd, (void *)buf, bufsz); 380f00d4f59SWei Liu } while (tsize == -1 && errno == EINTR); 381bec1e954SGreg Kurz close_preserve_errno(fd); 382f00d4f59SWei Liu } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 383f00d4f59SWei Liu (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); 398f00d4f59SWei Liu } 399f00d4f59SWei Liu return tsize; 400f00d4f59SWei Liu } 401f00d4f59SWei Liu 402f00d4f59SWei Liu static int local_close(FsContext *ctx, V9fsFidOpenState *fs) 403f00d4f59SWei Liu { 404f00d4f59SWei Liu return close(fs->fd); 405f00d4f59SWei Liu } 406f00d4f59SWei Liu 407f00d4f59SWei Liu static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs) 408f00d4f59SWei Liu { 409f314ea4eSGreg Kurz return closedir(fs->dir.stream); 410f00d4f59SWei Liu } 411f00d4f59SWei Liu 412f00d4f59SWei Liu static int local_open(FsContext *ctx, V9fsPath *fs_path, 413f00d4f59SWei Liu int flags, V9fsFidOpenState *fs) 414f00d4f59SWei Liu { 41521328e1eSGreg Kurz int fd; 416f00d4f59SWei Liu 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; 422f00d4f59SWei Liu return fs->fd; 423f00d4f59SWei Liu } 424f00d4f59SWei Liu 425f00d4f59SWei Liu static int local_opendir(FsContext *ctx, 426f00d4f59SWei Liu V9fsPath *fs_path, V9fsFidOpenState *fs) 427f00d4f59SWei Liu { 428996a0d76SGreg Kurz int dirfd; 42921328e1eSGreg Kurz DIR *stream; 430f00d4f59SWei Liu 431996a0d76SGreg Kurz dirfd = local_opendir_nofollow(ctx, fs_path->data); 432996a0d76SGreg Kurz if (dirfd == -1) { 433f00d4f59SWei Liu return -1; 434f00d4f59SWei Liu } 435996a0d76SGreg Kurz 436996a0d76SGreg Kurz stream = fdopendir(dirfd); 43721328e1eSGreg Kurz if (!stream) { 438faab207fSGreg Kurz close(dirfd); 439f00d4f59SWei Liu return -1; 440f00d4f59SWei Liu } 44121328e1eSGreg Kurz fs->dir.stream = stream; 442f00d4f59SWei Liu return 0; 443f00d4f59SWei Liu } 444f00d4f59SWei Liu 445f00d4f59SWei Liu static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs) 446f00d4f59SWei Liu { 447f314ea4eSGreg Kurz rewinddir(fs->dir.stream); 448f00d4f59SWei Liu } 449f00d4f59SWei Liu 450f00d4f59SWei Liu static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs) 451f00d4f59SWei Liu { 452f314ea4eSGreg Kurz return telldir(fs->dir.stream); 453f00d4f59SWei Liu } 454f00d4f59SWei Liu 455635324e8SGreg Kurz static struct dirent *local_readdir(FsContext *ctx, V9fsFidOpenState *fs) 456f00d4f59SWei Liu { 457635324e8SGreg Kurz struct dirent *entry; 458f00d4f59SWei Liu 459f00d4f59SWei Liu again: 460635324e8SGreg Kurz entry = readdir(fs->dir.stream); 461635324e8SGreg Kurz if (!entry) { 462635324e8SGreg Kurz return NULL; 463635324e8SGreg Kurz } 464635324e8SGreg Kurz 465f00d4f59SWei Liu if (ctx->export_flags & V9FS_SM_MAPPED) { 466f00d4f59SWei Liu entry->d_type = DT_UNKNOWN; 467f00d4f59SWei Liu } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 468635324e8SGreg Kurz if (!strcmp(entry->d_name, VIRTFS_META_DIR)) { 469f00d4f59SWei Liu /* skp the meta data directory */ 470f00d4f59SWei Liu goto again; 471f00d4f59SWei Liu } 472f00d4f59SWei Liu entry->d_type = DT_UNKNOWN; 473f00d4f59SWei Liu } 474635324e8SGreg Kurz 475635324e8SGreg Kurz return entry; 476f00d4f59SWei Liu } 477f00d4f59SWei Liu 478f00d4f59SWei Liu static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off) 479f00d4f59SWei Liu { 480f314ea4eSGreg Kurz seekdir(fs->dir.stream, off); 481f00d4f59SWei Liu } 482f00d4f59SWei Liu 483f00d4f59SWei Liu static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs, 484f00d4f59SWei Liu const struct iovec *iov, 485f00d4f59SWei Liu int iovcnt, off_t offset) 486f00d4f59SWei Liu { 487f00d4f59SWei Liu #ifdef CONFIG_PREADV 488f00d4f59SWei Liu return preadv(fs->fd, iov, iovcnt, offset); 489f00d4f59SWei Liu #else 490f00d4f59SWei Liu int err = lseek(fs->fd, offset, SEEK_SET); 491f00d4f59SWei Liu if (err == -1) { 492f00d4f59SWei Liu return err; 493f00d4f59SWei Liu } else { 494f00d4f59SWei Liu return readv(fs->fd, iov, iovcnt); 495f00d4f59SWei Liu } 496f00d4f59SWei Liu #endif 497f00d4f59SWei Liu } 498f00d4f59SWei Liu 499f00d4f59SWei Liu static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs, 500f00d4f59SWei Liu const struct iovec *iov, 501f00d4f59SWei Liu int iovcnt, off_t offset) 502f00d4f59SWei Liu { 5036fe76accSGreg Kurz ssize_t ret; 504f00d4f59SWei Liu #ifdef CONFIG_PREADV 505f00d4f59SWei Liu ret = pwritev(fs->fd, iov, iovcnt, offset); 506f00d4f59SWei Liu #else 507f00d4f59SWei Liu int err = lseek(fs->fd, offset, SEEK_SET); 508f00d4f59SWei Liu if (err == -1) { 509f00d4f59SWei Liu return err; 510f00d4f59SWei Liu } else { 511f00d4f59SWei Liu ret = writev(fs->fd, iov, iovcnt); 512f00d4f59SWei Liu } 513f00d4f59SWei Liu #endif 514f00d4f59SWei Liu #ifdef CONFIG_SYNC_FILE_RANGE 515f00d4f59SWei Liu if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) { 516f00d4f59SWei Liu /* 517f00d4f59SWei Liu * Initiate a writeback. This is not a data integrity sync. 518f00d4f59SWei Liu * We want to ensure that we don't leave dirty pages in the cache 519f00d4f59SWei Liu * after write when writeout=immediate is sepcified. 520f00d4f59SWei Liu */ 521f00d4f59SWei Liu sync_file_range(fs->fd, offset, ret, 522f00d4f59SWei Liu SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE); 523f00d4f59SWei Liu } 524f00d4f59SWei Liu #endif 525f00d4f59SWei Liu return ret; 526f00d4f59SWei Liu } 527f00d4f59SWei Liu 528f00d4f59SWei Liu static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp) 529f00d4f59SWei Liu { 530e3187a45SGreg Kurz char *dirpath = g_path_get_dirname(fs_path->data); 531e3187a45SGreg Kurz char *name = g_path_get_basename(fs_path->data); 532f00d4f59SWei Liu 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 } 539f00d4f59SWei Liu 540f00d4f59SWei Liu if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 541e3187a45SGreg Kurz ret = local_set_xattrat(dirfd, name, credp); 542f00d4f59SWei Liu } 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); 547f00d4f59SWei Liu } 548e3187a45SGreg Kurz close_preserve_errno(dirfd); 549e3187a45SGreg Kurz 550e3187a45SGreg Kurz out: 551e3187a45SGreg Kurz g_free(dirpath); 552e3187a45SGreg Kurz g_free(name); 553f00d4f59SWei Liu return ret; 554f00d4f59SWei Liu } 555f00d4f59SWei Liu 556f00d4f59SWei Liu static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path, 557f00d4f59SWei Liu const char *name, FsCred *credp) 558f00d4f59SWei Liu { 559f00d4f59SWei Liu int err = -1; 560d815e721SGreg Kurz int dirfd; 561f00d4f59SWei Liu 562d815e721SGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dir_path->data); 563d815e721SGreg Kurz if (dirfd == -1) { 564d815e721SGreg Kurz return -1; 565d815e721SGreg Kurz } 566f00d4f59SWei Liu 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 574f00d4f59SWei Liu 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); 578f00d4f59SWei Liu } 579f00d4f59SWei Liu if (err == -1) { 580f00d4f59SWei Liu goto err_end; 581f00d4f59SWei Liu } 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); 585f00d4f59SWei Liu if (err == -1) { 586f00d4f59SWei Liu goto out; 587f00d4f59SWei Liu } 588d815e721SGreg Kurz err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp); 589f00d4f59SWei Liu if (err == -1) { 590f00d4f59SWei Liu goto err_end; 591f00d4f59SWei Liu } 592f00d4f59SWei Liu } 593f00d4f59SWei Liu goto out; 594f00d4f59SWei Liu 595f00d4f59SWei Liu err_end: 596d815e721SGreg Kurz unlinkat_preserve_errno(dirfd, name, 0); 597f00d4f59SWei Liu out: 598d815e721SGreg Kurz close_preserve_errno(dirfd); 599f00d4f59SWei Liu return err; 600f00d4f59SWei Liu } 601f00d4f59SWei Liu 602f00d4f59SWei Liu static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path, 603f00d4f59SWei Liu const char *name, FsCred *credp) 604f00d4f59SWei Liu { 605f00d4f59SWei Liu int err = -1; 6063f3a1699SGreg Kurz int dirfd; 607f00d4f59SWei Liu 6083f3a1699SGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dir_path->data); 6093f3a1699SGreg Kurz if (dirfd == -1) { 6103f3a1699SGreg Kurz return -1; 6113f3a1699SGreg Kurz } 612f00d4f59SWei Liu 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 621f00d4f59SWei Liu 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); 625f00d4f59SWei Liu } 626f00d4f59SWei Liu if (err == -1) { 627f00d4f59SWei Liu goto err_end; 628f00d4f59SWei Liu } 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); 632f00d4f59SWei Liu if (err == -1) { 633f00d4f59SWei Liu goto out; 634f00d4f59SWei Liu } 6353f3a1699SGreg Kurz err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp); 636f00d4f59SWei Liu if (err == -1) { 637f00d4f59SWei Liu goto err_end; 638f00d4f59SWei Liu } 639f00d4f59SWei Liu } 640f00d4f59SWei Liu goto out; 641f00d4f59SWei Liu 642f00d4f59SWei Liu err_end: 6433f3a1699SGreg Kurz unlinkat_preserve_errno(dirfd, name, AT_REMOVEDIR); 644f00d4f59SWei Liu out: 6453f3a1699SGreg Kurz close_preserve_errno(dirfd); 646f00d4f59SWei Liu return err; 647f00d4f59SWei Liu } 648f00d4f59SWei Liu 649f00d4f59SWei Liu static int local_fstat(FsContext *fs_ctx, int fid_type, 650f00d4f59SWei Liu V9fsFidOpenState *fs, struct stat *stbuf) 651f00d4f59SWei Liu { 652f00d4f59SWei Liu int err, fd; 653f00d4f59SWei Liu 654f00d4f59SWei Liu if (fid_type == P9_FID_DIR) { 655f314ea4eSGreg Kurz fd = dirfd(fs->dir.stream); 656f00d4f59SWei Liu } else { 657f00d4f59SWei Liu fd = fs->fd; 658f00d4f59SWei Liu } 659f00d4f59SWei Liu 660f00d4f59SWei Liu err = fstat(fd, stbuf); 661f00d4f59SWei Liu if (err) { 662f00d4f59SWei Liu return err; 663f00d4f59SWei Liu } 664f00d4f59SWei Liu if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 665f00d4f59SWei Liu /* Actual credentials are part of extended attrs */ 666f00d4f59SWei Liu uid_t tmp_uid; 667f00d4f59SWei Liu gid_t tmp_gid; 668f00d4f59SWei Liu mode_t tmp_mode; 669f00d4f59SWei Liu dev_t tmp_dev; 670f00d4f59SWei Liu 671f00d4f59SWei Liu if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) { 672f00d4f59SWei Liu stbuf->st_uid = le32_to_cpu(tmp_uid); 673f00d4f59SWei Liu } 674f00d4f59SWei Liu if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) { 675f00d4f59SWei Liu stbuf->st_gid = le32_to_cpu(tmp_gid); 676f00d4f59SWei Liu } 677f00d4f59SWei Liu if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) { 678f00d4f59SWei Liu stbuf->st_mode = le32_to_cpu(tmp_mode); 679f00d4f59SWei Liu } 680f00d4f59SWei Liu if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) { 681f00d4f59SWei Liu stbuf->st_rdev = le64_to_cpu(tmp_dev); 682f00d4f59SWei Liu } 683f00d4f59SWei Liu } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 684f00d4f59SWei Liu errno = EOPNOTSUPP; 685f00d4f59SWei Liu return -1; 686f00d4f59SWei Liu } 687f00d4f59SWei Liu return err; 688f00d4f59SWei Liu } 689f00d4f59SWei Liu 690f00d4f59SWei Liu static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name, 691f00d4f59SWei Liu int flags, FsCred *credp, V9fsFidOpenState *fs) 692f00d4f59SWei Liu { 693f00d4f59SWei Liu int fd = -1; 694f00d4f59SWei Liu int err = -1; 695a565fea5SGreg Kurz int dirfd; 696f00d4f59SWei Liu 697f00d4f59SWei Liu /* 698f00d4f59SWei Liu * Mark all the open to not follow symlinks 699f00d4f59SWei Liu */ 700f00d4f59SWei Liu flags |= O_NOFOLLOW; 701f00d4f59SWei Liu 702a565fea5SGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dir_path->data); 703a565fea5SGreg Kurz if (dirfd == -1) { 704a565fea5SGreg Kurz return -1; 705a565fea5SGreg Kurz } 706f00d4f59SWei Liu 707f00d4f59SWei Liu /* 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; 715f00d4f59SWei Liu if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 716f00d4f59SWei Liu /* 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); 720f00d4f59SWei Liu } 721f00d4f59SWei Liu if (err == -1) { 722f00d4f59SWei Liu goto err_end; 723f00d4f59SWei Liu } 724f00d4f59SWei Liu } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 725f00d4f59SWei Liu (fs_ctx->export_flags & V9FS_SM_NONE)) { 726a565fea5SGreg Kurz fd = openat_file(dirfd, name, flags, credp->fc_mode); 727f00d4f59SWei Liu if (fd == -1) { 728f00d4f59SWei Liu goto out; 729f00d4f59SWei Liu } 730a565fea5SGreg Kurz err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp); 731f00d4f59SWei Liu if (err == -1) { 732f00d4f59SWei Liu goto err_end; 733f00d4f59SWei Liu } 734f00d4f59SWei Liu } 735f00d4f59SWei Liu err = fd; 736f00d4f59SWei Liu fs->fd = fd; 737f00d4f59SWei Liu goto out; 738f00d4f59SWei Liu 739f00d4f59SWei Liu err_end: 740a565fea5SGreg Kurz unlinkat_preserve_errno(dirfd, name, 741a565fea5SGreg Kurz flags & O_DIRECTORY ? AT_REMOVEDIR : 0); 742a565fea5SGreg Kurz close_preserve_errno(fd); 743f00d4f59SWei Liu out: 744a565fea5SGreg Kurz close_preserve_errno(dirfd); 745f00d4f59SWei Liu return err; 746f00d4f59SWei Liu } 747f00d4f59SWei Liu 748f00d4f59SWei Liu 749f00d4f59SWei Liu static int local_symlink(FsContext *fs_ctx, const char *oldpath, 750f00d4f59SWei Liu V9fsPath *dir_path, const char *name, FsCred *credp) 751f00d4f59SWei Liu { 752f00d4f59SWei Liu int err = -1; 75338771613SGreg Kurz int dirfd; 754f00d4f59SWei Liu 75538771613SGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dir_path->data); 75638771613SGreg Kurz if (dirfd == -1) { 75738771613SGreg Kurz return -1; 75838771613SGreg Kurz } 759f00d4f59SWei Liu 760f00d4f59SWei Liu /* 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 784f00d4f59SWei Liu 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); 788f00d4f59SWei Liu } 789f00d4f59SWei Liu if (err == -1) { 790f00d4f59SWei Liu goto err_end; 791f00d4f59SWei Liu } 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); 795f00d4f59SWei Liu if (err) { 796f00d4f59SWei Liu goto out; 797f00d4f59SWei Liu } 79838771613SGreg Kurz err = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid, 79938771613SGreg Kurz AT_SYMLINK_NOFOLLOW); 800f00d4f59SWei Liu if (err == -1) { 801f00d4f59SWei Liu /* 802f00d4f59SWei Liu * If we fail to change ownership and if we are 803f00d4f59SWei Liu * using security model none. Ignore the error 804f00d4f59SWei Liu */ 805f00d4f59SWei Liu if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) { 806f00d4f59SWei Liu goto err_end; 80738771613SGreg Kurz } else { 808f00d4f59SWei Liu err = 0; 809f00d4f59SWei Liu } 810f00d4f59SWei Liu } 81138771613SGreg Kurz } 812f00d4f59SWei Liu goto out; 813f00d4f59SWei Liu 814f00d4f59SWei Liu err_end: 81538771613SGreg Kurz unlinkat_preserve_errno(dirfd, name, 0); 816f00d4f59SWei Liu out: 81738771613SGreg Kurz close_preserve_errno(dirfd); 818f00d4f59SWei Liu return err; 819f00d4f59SWei Liu } 820f00d4f59SWei Liu 821f00d4f59SWei Liu static int local_link(FsContext *ctx, V9fsPath *oldpath, 822f00d4f59SWei Liu V9fsPath *dirpath, const char *name) 823f00d4f59SWei Liu { 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; 828f00d4f59SWei Liu 829ad0b46e6SGreg Kurz odirfd = local_opendir_nofollow(ctx, odirpath); 830ad0b46e6SGreg Kurz if (odirfd == -1) { 8316dd4b1f1SGreg Kurz goto out; 8326dd4b1f1SGreg Kurz } 833f00d4f59SWei Liu 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 } 844f00d4f59SWei Liu 845f00d4f59SWei Liu /* 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; 852f00d4f59SWei Liu } 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); 868f00d4f59SWei Liu if (ret < 0 && errno != ENOENT) { 869ad0b46e6SGreg Kurz goto err_undo_link; 870f00d4f59SWei Liu } 8716dd4b1f1SGreg Kurz 872ad0b46e6SGreg Kurz ret = 0; 873f00d4f59SWei Liu } 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); 886f00d4f59SWei Liu return ret; 887f00d4f59SWei Liu } 888f00d4f59SWei Liu 889f00d4f59SWei Liu static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size) 890f00d4f59SWei Liu { 891ac125d99SGreg Kurz int fd, ret; 892f00d4f59SWei Liu 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); 899f00d4f59SWei Liu return ret; 900f00d4f59SWei Liu } 901f00d4f59SWei Liu 902f00d4f59SWei Liu static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp) 903f00d4f59SWei Liu { 904d369f207SGreg Kurz char *dirpath = g_path_get_dirname(fs_path->data); 905d369f207SGreg Kurz char *name = g_path_get_basename(fs_path->data); 906f00d4f59SWei Liu 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 } 913f00d4f59SWei Liu 914f00d4f59SWei Liu if ((credp->fc_uid == -1 && credp->fc_gid == -1) || 915f00d4f59SWei Liu (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 916f00d4f59SWei Liu (fs_ctx->export_flags & V9FS_SM_NONE)) { 917d369f207SGreg Kurz ret = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid, 918d369f207SGreg Kurz AT_SYMLINK_NOFOLLOW); 919f00d4f59SWei Liu } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 920d369f207SGreg Kurz ret = local_set_xattrat(dirfd, name, credp); 921f00d4f59SWei Liu } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 922d369f207SGreg Kurz ret = local_set_mapped_file_attrat(dirfd, name, credp); 923f00d4f59SWei Liu } 924d369f207SGreg Kurz 925d369f207SGreg Kurz close_preserve_errno(dirfd); 926d369f207SGreg Kurz out: 927d369f207SGreg Kurz g_free(name); 928d369f207SGreg Kurz g_free(dirpath); 929f00d4f59SWei Liu return ret; 930f00d4f59SWei Liu } 931f00d4f59SWei Liu 932f00d4f59SWei Liu static int local_utimensat(FsContext *s, V9fsPath *fs_path, 933f00d4f59SWei Liu const struct timespec *buf) 934f00d4f59SWei Liu { 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; 938f00d4f59SWei Liu 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); 949f00d4f59SWei Liu return ret; 950f00d4f59SWei Liu } 951f00d4f59SWei Liu 952df4938a6SGreg Kurz static int local_unlinkat_common(FsContext *ctx, int dirfd, const char *name, 953df4938a6SGreg Kurz int flags) 954f00d4f59SWei Liu { 955df4938a6SGreg Kurz int ret = -1; 956f00d4f59SWei Liu 957f00d4f59SWei Liu 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 963b003fc0dSGreg Kurz fd = openat_dir(dirfd, name); 964df4938a6SGreg Kurz if (fd == -1) { 965f00d4f59SWei Liu goto err_out; 966f00d4f59SWei Liu } 967f00d4f59SWei Liu /* 968f00d4f59SWei Liu * If directory remove .virtfs_metadata contained in the 969f00d4f59SWei Liu * directory 970f00d4f59SWei Liu */ 971df4938a6SGreg Kurz ret = unlinkat(fd, VIRTFS_META_DIR, AT_REMOVEDIR); 972df4938a6SGreg Kurz close_preserve_errno(fd); 973df4938a6SGreg Kurz if (ret < 0 && errno != ENOENT) { 974f00d4f59SWei Liu /* 975f00d4f59SWei Liu * We didn't had the .virtfs_metadata file. May be file created 976f00d4f59SWei Liu * in non-mapped mode ?. Ignore ENOENT. 977f00d4f59SWei Liu */ 978f00d4f59SWei Liu goto err_out; 979f00d4f59SWei Liu } 980f00d4f59SWei Liu } 981f00d4f59SWei Liu /* 982f00d4f59SWei Liu * Now remove the name from parent directory 983df4938a6SGreg Kurz * .virtfs_metadata directory. 984f00d4f59SWei Liu */ 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) { 989f00d4f59SWei Liu /* 990f00d4f59SWei Liu * We didn't had the .virtfs_metadata file. May be file created 991f00d4f59SWei Liu * in non-mapped mode ?. Ignore ENOENT. 992f00d4f59SWei Liu */ 993f00d4f59SWei Liu goto err_out; 994f00d4f59SWei Liu } 995f00d4f59SWei Liu } 996f00d4f59SWei Liu 997df4938a6SGreg Kurz ret = unlinkat(dirfd, name, flags); 998f00d4f59SWei Liu err_out: 999df4938a6SGreg Kurz return ret; 1000df4938a6SGreg Kurz } 1001df4938a6SGreg Kurz 1002f00d4f59SWei Liu static int local_remove(FsContext *ctx, const char *path) 1003f00d4f59SWei Liu { 1004f00d4f59SWei Liu 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; 1010f00d4f59SWei Liu 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) { 1017f00d4f59SWei Liu goto err_out; 1018f00d4f59SWei Liu } 1019a0e640a8SGreg Kurz 1020f00d4f59SWei Liu if (S_ISDIR(stbuf.st_mode)) { 1021a0e640a8SGreg Kurz flags |= AT_REMOVEDIR; 1022f00d4f59SWei Liu } 1023f00d4f59SWei Liu 1024a0e640a8SGreg Kurz err = local_unlinkat_common(ctx, dirfd, name, flags); 1025f00d4f59SWei Liu err_out: 1026a0e640a8SGreg Kurz close_preserve_errno(dirfd); 1027a0e640a8SGreg Kurz out: 1028a0e640a8SGreg Kurz g_free(name); 1029a0e640a8SGreg Kurz g_free(dirpath); 1030f00d4f59SWei Liu return err; 1031f00d4f59SWei Liu } 1032f00d4f59SWei Liu 1033f00d4f59SWei Liu static int local_fsync(FsContext *ctx, int fid_type, 1034f00d4f59SWei Liu V9fsFidOpenState *fs, int datasync) 1035f00d4f59SWei Liu { 1036f00d4f59SWei Liu int fd; 1037f00d4f59SWei Liu 1038f00d4f59SWei Liu if (fid_type == P9_FID_DIR) { 1039f314ea4eSGreg Kurz fd = dirfd(fs->dir.stream); 1040f00d4f59SWei Liu } else { 1041f00d4f59SWei Liu fd = fs->fd; 1042f00d4f59SWei Liu } 1043f00d4f59SWei Liu 1044f00d4f59SWei Liu if (datasync) { 1045f00d4f59SWei Liu return qemu_fdatasync(fd); 1046f00d4f59SWei Liu } else { 1047f00d4f59SWei Liu return fsync(fd); 1048f00d4f59SWei Liu } 1049f00d4f59SWei Liu } 1050f00d4f59SWei Liu 1051f00d4f59SWei Liu static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf) 1052f00d4f59SWei Liu { 105331e51d1cSGreg Kurz int fd, ret; 1054f00d4f59SWei Liu 105531e51d1cSGreg Kurz fd = local_open_nofollow(s, fs_path->data, O_RDONLY, 0); 105623da0145SGreg Kurz if (fd == -1) { 105723da0145SGreg Kurz return -1; 105823da0145SGreg Kurz } 105931e51d1cSGreg Kurz ret = fstatfs(fd, stbuf); 106031e51d1cSGreg Kurz close_preserve_errno(fd); 1061f00d4f59SWei Liu return ret; 1062f00d4f59SWei Liu } 1063f00d4f59SWei Liu 1064f00d4f59SWei Liu static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path, 1065f00d4f59SWei Liu const char *name, void *value, size_t size) 1066f00d4f59SWei Liu { 1067f00d4f59SWei Liu char *path = fs_path->data; 1068f00d4f59SWei Liu 1069f00d4f59SWei Liu return v9fs_get_xattr(ctx, path, name, value, size); 1070f00d4f59SWei Liu } 1071f00d4f59SWei Liu 1072f00d4f59SWei Liu static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path, 1073f00d4f59SWei Liu void *value, size_t size) 1074f00d4f59SWei Liu { 1075f00d4f59SWei Liu char *path = fs_path->data; 1076f00d4f59SWei Liu 1077f00d4f59SWei Liu return v9fs_list_xattr(ctx, path, value, size); 1078f00d4f59SWei Liu } 1079f00d4f59SWei Liu 1080f00d4f59SWei Liu static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name, 1081f00d4f59SWei Liu void *value, size_t size, int flags) 1082f00d4f59SWei Liu { 1083f00d4f59SWei Liu char *path = fs_path->data; 1084f00d4f59SWei Liu 1085f00d4f59SWei Liu return v9fs_set_xattr(ctx, path, name, value, size, flags); 1086f00d4f59SWei Liu } 1087f00d4f59SWei Liu 1088f00d4f59SWei Liu static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path, 1089f00d4f59SWei Liu const char *name) 1090f00d4f59SWei Liu { 1091f00d4f59SWei Liu char *path = fs_path->data; 1092f00d4f59SWei Liu 1093f00d4f59SWei Liu return v9fs_remove_xattr(ctx, path, name); 1094f00d4f59SWei Liu } 1095f00d4f59SWei Liu 1096f00d4f59SWei Liu static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path, 1097f00d4f59SWei Liu const char *name, V9fsPath *target) 1098f00d4f59SWei Liu { 1099f00d4f59SWei Liu if (dir_path) { 1100e3e83f2eSGreg Kurz v9fs_path_sprintf(target, "%s/%s", dir_path->data, name); 1101*9c6b899fSGreg Kurz } else if (strcmp(name, "/")) { 1102e3e83f2eSGreg Kurz v9fs_path_sprintf(target, "%s", name); 1103*9c6b899fSGreg Kurz } else { 1104*9c6b899fSGreg Kurz /* We want the path of the export root to be relative, otherwise 1105*9c6b899fSGreg Kurz * "*at()" syscalls would treat it as "/" in the host. 1106*9c6b899fSGreg Kurz */ 1107*9c6b899fSGreg Kurz v9fs_path_sprintf(target, "%s", "."); 1108f00d4f59SWei Liu } 1109f00d4f59SWei Liu return 0; 1110f00d4f59SWei Liu } 1111f00d4f59SWei Liu 1112f00d4f59SWei Liu static int local_renameat(FsContext *ctx, V9fsPath *olddir, 1113f00d4f59SWei Liu const char *old_name, V9fsPath *newdir, 1114f00d4f59SWei Liu const char *new_name) 1115f00d4f59SWei Liu { 1116f00d4f59SWei Liu int ret; 111799f2cf4bSGreg Kurz int odirfd, ndirfd; 1118f00d4f59SWei Liu 111999f2cf4bSGreg Kurz odirfd = local_opendir_nofollow(ctx, olddir->data); 112099f2cf4bSGreg Kurz if (odirfd == -1) { 112199f2cf4bSGreg Kurz return -1; 112299f2cf4bSGreg Kurz } 1123f00d4f59SWei Liu 112499f2cf4bSGreg Kurz ndirfd = local_opendir_nofollow(ctx, newdir->data); 112599f2cf4bSGreg Kurz if (ndirfd == -1) { 112699f2cf4bSGreg Kurz close_preserve_errno(odirfd); 112799f2cf4bSGreg Kurz return -1; 112899f2cf4bSGreg Kurz } 1129f00d4f59SWei Liu 113099f2cf4bSGreg Kurz ret = renameat(odirfd, old_name, ndirfd, new_name); 113199f2cf4bSGreg Kurz if (ret < 0) { 113299f2cf4bSGreg Kurz goto out; 113399f2cf4bSGreg Kurz } 113499f2cf4bSGreg Kurz 113599f2cf4bSGreg Kurz if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 113699f2cf4bSGreg Kurz int omap_dirfd, nmap_dirfd; 113799f2cf4bSGreg Kurz 113899f2cf4bSGreg Kurz ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700); 113999f2cf4bSGreg Kurz if (ret < 0 && errno != EEXIST) { 114099f2cf4bSGreg Kurz goto err_undo_rename; 114199f2cf4bSGreg Kurz } 114299f2cf4bSGreg Kurz 11436dd4b1f1SGreg Kurz omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR); 114499f2cf4bSGreg Kurz if (omap_dirfd == -1) { 114599f2cf4bSGreg Kurz goto err; 114699f2cf4bSGreg Kurz } 114799f2cf4bSGreg Kurz 11486dd4b1f1SGreg Kurz nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR); 114999f2cf4bSGreg Kurz if (nmap_dirfd == -1) { 115099f2cf4bSGreg Kurz close_preserve_errno(omap_dirfd); 115199f2cf4bSGreg Kurz goto err; 115299f2cf4bSGreg Kurz } 115399f2cf4bSGreg Kurz 115499f2cf4bSGreg Kurz /* rename the .virtfs_metadata files */ 115599f2cf4bSGreg Kurz ret = renameat(omap_dirfd, old_name, nmap_dirfd, new_name); 115699f2cf4bSGreg Kurz close_preserve_errno(nmap_dirfd); 115799f2cf4bSGreg Kurz close_preserve_errno(omap_dirfd); 115899f2cf4bSGreg Kurz if (ret < 0 && errno != ENOENT) { 115999f2cf4bSGreg Kurz goto err_undo_rename; 116099f2cf4bSGreg Kurz } 116199f2cf4bSGreg Kurz 116299f2cf4bSGreg Kurz ret = 0; 116399f2cf4bSGreg Kurz } 116499f2cf4bSGreg Kurz goto out; 116599f2cf4bSGreg Kurz 116699f2cf4bSGreg Kurz err: 116799f2cf4bSGreg Kurz ret = -1; 116899f2cf4bSGreg Kurz err_undo_rename: 116999f2cf4bSGreg Kurz renameat_preserve_errno(ndirfd, new_name, odirfd, old_name); 117099f2cf4bSGreg Kurz out: 117199f2cf4bSGreg Kurz close_preserve_errno(ndirfd); 117299f2cf4bSGreg Kurz close_preserve_errno(odirfd); 1173f00d4f59SWei Liu return ret; 1174f00d4f59SWei Liu } 1175f00d4f59SWei Liu 1176d2767edeSGreg Kurz static void v9fs_path_init_dirname(V9fsPath *path, const char *str) 1177d2767edeSGreg Kurz { 1178d2767edeSGreg Kurz path->data = g_path_get_dirname(str); 1179d2767edeSGreg Kurz path->size = strlen(path->data) + 1; 1180d2767edeSGreg Kurz } 1181d2767edeSGreg Kurz 1182d2767edeSGreg Kurz static int local_rename(FsContext *ctx, const char *oldpath, 1183d2767edeSGreg Kurz const char *newpath) 1184d2767edeSGreg Kurz { 1185d2767edeSGreg Kurz int err; 1186d2767edeSGreg Kurz char *oname = g_path_get_basename(oldpath); 1187d2767edeSGreg Kurz char *nname = g_path_get_basename(newpath); 1188d2767edeSGreg Kurz V9fsPath olddir, newdir; 1189d2767edeSGreg Kurz 1190d2767edeSGreg Kurz v9fs_path_init_dirname(&olddir, oldpath); 1191d2767edeSGreg Kurz v9fs_path_init_dirname(&newdir, newpath); 1192d2767edeSGreg Kurz 1193d2767edeSGreg Kurz err = local_renameat(ctx, &olddir, oname, &newdir, nname); 1194d2767edeSGreg Kurz 1195d2767edeSGreg Kurz v9fs_path_free(&newdir); 1196d2767edeSGreg Kurz v9fs_path_free(&olddir); 1197d2767edeSGreg Kurz g_free(nname); 1198d2767edeSGreg Kurz g_free(oname); 1199d2767edeSGreg Kurz 1200d2767edeSGreg Kurz return err; 1201d2767edeSGreg Kurz } 1202d2767edeSGreg Kurz 1203f00d4f59SWei Liu static int local_unlinkat(FsContext *ctx, V9fsPath *dir, 1204f00d4f59SWei Liu const char *name, int flags) 1205f00d4f59SWei Liu { 1206f00d4f59SWei Liu int ret; 1207df4938a6SGreg Kurz int dirfd; 1208f00d4f59SWei Liu 1209df4938a6SGreg Kurz dirfd = local_opendir_nofollow(ctx, dir->data); 1210df4938a6SGreg Kurz if (dirfd == -1) { 1211df4938a6SGreg Kurz return -1; 1212df4938a6SGreg Kurz } 1213f00d4f59SWei Liu 1214df4938a6SGreg Kurz ret = local_unlinkat_common(ctx, dirfd, name, flags); 1215df4938a6SGreg Kurz close_preserve_errno(dirfd); 1216f00d4f59SWei Liu return ret; 1217f00d4f59SWei Liu } 1218f00d4f59SWei Liu 1219f00d4f59SWei Liu static int local_ioc_getversion(FsContext *ctx, V9fsPath *path, 1220f00d4f59SWei Liu mode_t st_mode, uint64_t *st_gen) 1221f00d4f59SWei Liu { 1222f00d4f59SWei Liu #ifdef FS_IOC_GETVERSION 1223f00d4f59SWei Liu int err; 1224f00d4f59SWei Liu V9fsFidOpenState fid_open; 1225f00d4f59SWei Liu 1226f00d4f59SWei Liu /* 1227f00d4f59SWei Liu * Do not try to open special files like device nodes, fifos etc 1228f00d4f59SWei Liu * We can get fd for regular files and directories only 1229f00d4f59SWei Liu */ 1230f00d4f59SWei Liu if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) { 1231f00d4f59SWei Liu errno = ENOTTY; 1232f00d4f59SWei Liu return -1; 1233f00d4f59SWei Liu } 1234f00d4f59SWei Liu err = local_open(ctx, path, O_RDONLY, &fid_open); 1235f00d4f59SWei Liu if (err < 0) { 1236f00d4f59SWei Liu return err; 1237f00d4f59SWei Liu } 1238f00d4f59SWei Liu err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen); 1239f00d4f59SWei Liu local_close(ctx, &fid_open); 1240f00d4f59SWei Liu return err; 1241f00d4f59SWei Liu #else 1242f00d4f59SWei Liu errno = ENOTTY; 1243f00d4f59SWei Liu return -1; 1244f00d4f59SWei Liu #endif 1245f00d4f59SWei Liu } 1246f00d4f59SWei Liu 1247f00d4f59SWei Liu static int local_init(FsContext *ctx) 1248f00d4f59SWei Liu { 1249f00d4f59SWei Liu struct statfs stbuf; 12500e35a378SGreg Kurz LocalData *data = g_malloc(sizeof(*data)); 12510e35a378SGreg Kurz 12520e35a378SGreg Kurz data->mountfd = open(ctx->fs_root, O_DIRECTORY | O_RDONLY); 12530e35a378SGreg Kurz if (data->mountfd == -1) { 12540e35a378SGreg Kurz goto err; 12550e35a378SGreg Kurz } 1256f00d4f59SWei Liu 125700c90bd1SGreg Kurz #ifdef FS_IOC_GETVERSION 125800c90bd1SGreg Kurz /* 125900c90bd1SGreg Kurz * use ioc_getversion only if the ioctl is definied 126000c90bd1SGreg Kurz */ 12610e35a378SGreg Kurz if (fstatfs(data->mountfd, &stbuf) < 0) { 12620e35a378SGreg Kurz close_preserve_errno(data->mountfd); 12630e35a378SGreg Kurz goto err; 126400c90bd1SGreg Kurz } 126500c90bd1SGreg Kurz switch (stbuf.f_type) { 126600c90bd1SGreg Kurz case EXT2_SUPER_MAGIC: 126700c90bd1SGreg Kurz case BTRFS_SUPER_MAGIC: 126800c90bd1SGreg Kurz case REISERFS_SUPER_MAGIC: 126900c90bd1SGreg Kurz case XFS_SUPER_MAGIC: 127000c90bd1SGreg Kurz ctx->exops.get_st_gen = local_ioc_getversion; 127100c90bd1SGreg Kurz break; 127200c90bd1SGreg Kurz } 127300c90bd1SGreg Kurz #endif 1274f00d4f59SWei Liu 1275f00d4f59SWei Liu if (ctx->export_flags & V9FS_SM_PASSTHROUGH) { 1276f00d4f59SWei Liu ctx->xops = passthrough_xattr_ops; 1277f00d4f59SWei Liu } else if (ctx->export_flags & V9FS_SM_MAPPED) { 1278f00d4f59SWei Liu ctx->xops = mapped_xattr_ops; 1279f00d4f59SWei Liu } else if (ctx->export_flags & V9FS_SM_NONE) { 1280f00d4f59SWei Liu ctx->xops = none_xattr_ops; 1281f00d4f59SWei Liu } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 1282f00d4f59SWei Liu /* 1283f00d4f59SWei Liu * xattr operation for mapped-file and passthrough 1284f00d4f59SWei Liu * remain same. 1285f00d4f59SWei Liu */ 1286f00d4f59SWei Liu ctx->xops = passthrough_xattr_ops; 1287f00d4f59SWei Liu } 1288f00d4f59SWei Liu ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT; 128900c90bd1SGreg Kurz 12900e35a378SGreg Kurz ctx->private = data; 129100c90bd1SGreg Kurz return 0; 12920e35a378SGreg Kurz 12930e35a378SGreg Kurz err: 12940e35a378SGreg Kurz g_free(data); 12950e35a378SGreg Kurz return -1; 1296f00d4f59SWei Liu } 12970e35a378SGreg Kurz 12980e35a378SGreg Kurz static void local_cleanup(FsContext *ctx) 12990e35a378SGreg Kurz { 13000e35a378SGreg Kurz LocalData *data = ctx->private; 13010e35a378SGreg Kurz 13020e35a378SGreg Kurz close(data->mountfd); 13030e35a378SGreg Kurz g_free(data); 1304f00d4f59SWei Liu } 1305f00d4f59SWei Liu 1306f00d4f59SWei Liu static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse) 1307f00d4f59SWei Liu { 1308f00d4f59SWei Liu const char *sec_model = qemu_opt_get(opts, "security_model"); 1309f00d4f59SWei Liu const char *path = qemu_opt_get(opts, "path"); 1310b8bbdb88SPradeep Jagadeesh Error *err = NULL; 1311f00d4f59SWei Liu 1312f00d4f59SWei Liu if (!sec_model) { 131363325b18SGreg Kurz error_report("Security model not specified, local fs needs security model"); 131463325b18SGreg Kurz error_printf("valid options are:" 131563325b18SGreg Kurz "\tsecurity_model=[passthrough|mapped-xattr|mapped-file|none]\n"); 1316f00d4f59SWei Liu return -1; 1317f00d4f59SWei Liu } 1318f00d4f59SWei Liu 1319f00d4f59SWei Liu if (!strcmp(sec_model, "passthrough")) { 1320f00d4f59SWei Liu fse->export_flags |= V9FS_SM_PASSTHROUGH; 1321f00d4f59SWei Liu } else if (!strcmp(sec_model, "mapped") || 1322f00d4f59SWei Liu !strcmp(sec_model, "mapped-xattr")) { 1323f00d4f59SWei Liu fse->export_flags |= V9FS_SM_MAPPED; 1324f00d4f59SWei Liu } else if (!strcmp(sec_model, "none")) { 1325f00d4f59SWei Liu fse->export_flags |= V9FS_SM_NONE; 1326f00d4f59SWei Liu } else if (!strcmp(sec_model, "mapped-file")) { 1327f00d4f59SWei Liu fse->export_flags |= V9FS_SM_MAPPED_FILE; 1328f00d4f59SWei Liu } else { 132963325b18SGreg Kurz error_report("Invalid security model %s specified", sec_model); 133063325b18SGreg Kurz error_printf("valid options are:" 133163325b18SGreg Kurz "\t[passthrough|mapped-xattr|mapped-file|none]\n"); 1332f00d4f59SWei Liu return -1; 1333f00d4f59SWei Liu } 1334f00d4f59SWei Liu 1335f00d4f59SWei Liu if (!path) { 133663325b18SGreg Kurz error_report("fsdev: No path specified"); 1337f00d4f59SWei Liu return -1; 1338f00d4f59SWei Liu } 1339b8bbdb88SPradeep Jagadeesh 1340b8bbdb88SPradeep Jagadeesh fsdev_throttle_parse_opts(opts, &fse->fst, &err); 1341b8bbdb88SPradeep Jagadeesh if (err) { 1342b8bbdb88SPradeep Jagadeesh error_reportf_err(err, "Throttle configuration is not valid: "); 1343b8bbdb88SPradeep Jagadeesh return -1; 1344b8bbdb88SPradeep Jagadeesh } 1345b8bbdb88SPradeep Jagadeesh 1346f00d4f59SWei Liu fse->path = g_strdup(path); 1347f00d4f59SWei Liu 1348f00d4f59SWei Liu return 0; 1349f00d4f59SWei Liu } 1350f00d4f59SWei Liu 1351f00d4f59SWei Liu FileOperations local_ops = { 1352f00d4f59SWei Liu .parse_opts = local_parse_opts, 1353f00d4f59SWei Liu .init = local_init, 13540e35a378SGreg Kurz .cleanup = local_cleanup, 1355f00d4f59SWei Liu .lstat = local_lstat, 1356f00d4f59SWei Liu .readlink = local_readlink, 1357f00d4f59SWei Liu .close = local_close, 1358f00d4f59SWei Liu .closedir = local_closedir, 1359f00d4f59SWei Liu .open = local_open, 1360f00d4f59SWei Liu .opendir = local_opendir, 1361f00d4f59SWei Liu .rewinddir = local_rewinddir, 1362f00d4f59SWei Liu .telldir = local_telldir, 1363635324e8SGreg Kurz .readdir = local_readdir, 1364f00d4f59SWei Liu .seekdir = local_seekdir, 1365f00d4f59SWei Liu .preadv = local_preadv, 1366f00d4f59SWei Liu .pwritev = local_pwritev, 1367f00d4f59SWei Liu .chmod = local_chmod, 1368f00d4f59SWei Liu .mknod = local_mknod, 1369f00d4f59SWei Liu .mkdir = local_mkdir, 1370f00d4f59SWei Liu .fstat = local_fstat, 1371f00d4f59SWei Liu .open2 = local_open2, 1372f00d4f59SWei Liu .symlink = local_symlink, 1373f00d4f59SWei Liu .link = local_link, 1374f00d4f59SWei Liu .truncate = local_truncate, 1375f00d4f59SWei Liu .rename = local_rename, 1376f00d4f59SWei Liu .chown = local_chown, 1377f00d4f59SWei Liu .utimensat = local_utimensat, 1378f00d4f59SWei Liu .remove = local_remove, 1379f00d4f59SWei Liu .fsync = local_fsync, 1380f00d4f59SWei Liu .statfs = local_statfs, 1381f00d4f59SWei Liu .lgetxattr = local_lgetxattr, 1382f00d4f59SWei Liu .llistxattr = local_llistxattr, 1383f00d4f59SWei Liu .lsetxattr = local_lsetxattr, 1384f00d4f59SWei Liu .lremovexattr = local_lremovexattr, 1385f00d4f59SWei Liu .name_to_path = local_name_to_path, 1386f00d4f59SWei Liu .renameat = local_renameat, 1387f00d4f59SWei Liu .unlinkat = local_unlinkat, 1388f00d4f59SWei Liu }; 1389