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, 352d815e721SGreg Kurz AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH) < 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) { 438f00d4f59SWei Liu return -1; 439f00d4f59SWei Liu } 44021328e1eSGreg Kurz fs->dir.stream = stream; 441f00d4f59SWei Liu return 0; 442f00d4f59SWei Liu } 443f00d4f59SWei Liu 444f00d4f59SWei Liu static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs) 445f00d4f59SWei Liu { 446f314ea4eSGreg Kurz rewinddir(fs->dir.stream); 447f00d4f59SWei Liu } 448f00d4f59SWei Liu 449f00d4f59SWei Liu static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs) 450f00d4f59SWei Liu { 451f314ea4eSGreg Kurz return telldir(fs->dir.stream); 452f00d4f59SWei Liu } 453f00d4f59SWei Liu 454635324e8SGreg Kurz static struct dirent *local_readdir(FsContext *ctx, V9fsFidOpenState *fs) 455f00d4f59SWei Liu { 456635324e8SGreg Kurz struct dirent *entry; 457f00d4f59SWei Liu 458f00d4f59SWei Liu again: 459635324e8SGreg Kurz entry = readdir(fs->dir.stream); 460635324e8SGreg Kurz if (!entry) { 461635324e8SGreg Kurz return NULL; 462635324e8SGreg Kurz } 463635324e8SGreg Kurz 464f00d4f59SWei Liu if (ctx->export_flags & V9FS_SM_MAPPED) { 465f00d4f59SWei Liu entry->d_type = DT_UNKNOWN; 466f00d4f59SWei Liu } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 467635324e8SGreg Kurz if (!strcmp(entry->d_name, VIRTFS_META_DIR)) { 468f00d4f59SWei Liu /* skp the meta data directory */ 469f00d4f59SWei Liu goto again; 470f00d4f59SWei Liu } 471f00d4f59SWei Liu entry->d_type = DT_UNKNOWN; 472f00d4f59SWei Liu } 473635324e8SGreg Kurz 474635324e8SGreg Kurz return entry; 475f00d4f59SWei Liu } 476f00d4f59SWei Liu 477f00d4f59SWei Liu static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off) 478f00d4f59SWei Liu { 479f314ea4eSGreg Kurz seekdir(fs->dir.stream, off); 480f00d4f59SWei Liu } 481f00d4f59SWei Liu 482f00d4f59SWei Liu static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs, 483f00d4f59SWei Liu const struct iovec *iov, 484f00d4f59SWei Liu int iovcnt, off_t offset) 485f00d4f59SWei Liu { 486f00d4f59SWei Liu #ifdef CONFIG_PREADV 487f00d4f59SWei Liu return preadv(fs->fd, iov, iovcnt, offset); 488f00d4f59SWei Liu #else 489f00d4f59SWei Liu int err = lseek(fs->fd, offset, SEEK_SET); 490f00d4f59SWei Liu if (err == -1) { 491f00d4f59SWei Liu return err; 492f00d4f59SWei Liu } else { 493f00d4f59SWei Liu return readv(fs->fd, iov, iovcnt); 494f00d4f59SWei Liu } 495f00d4f59SWei Liu #endif 496f00d4f59SWei Liu } 497f00d4f59SWei Liu 498f00d4f59SWei Liu static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs, 499f00d4f59SWei Liu const struct iovec *iov, 500f00d4f59SWei Liu int iovcnt, off_t offset) 501f00d4f59SWei Liu { 5026fe76accSGreg Kurz ssize_t ret; 503f00d4f59SWei Liu #ifdef CONFIG_PREADV 504f00d4f59SWei Liu ret = pwritev(fs->fd, iov, iovcnt, offset); 505f00d4f59SWei Liu #else 506f00d4f59SWei Liu int err = lseek(fs->fd, offset, SEEK_SET); 507f00d4f59SWei Liu if (err == -1) { 508f00d4f59SWei Liu return err; 509f00d4f59SWei Liu } else { 510f00d4f59SWei Liu ret = writev(fs->fd, iov, iovcnt); 511f00d4f59SWei Liu } 512f00d4f59SWei Liu #endif 513f00d4f59SWei Liu #ifdef CONFIG_SYNC_FILE_RANGE 514f00d4f59SWei Liu if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) { 515f00d4f59SWei Liu /* 516f00d4f59SWei Liu * Initiate a writeback. This is not a data integrity sync. 517f00d4f59SWei Liu * We want to ensure that we don't leave dirty pages in the cache 518f00d4f59SWei Liu * after write when writeout=immediate is sepcified. 519f00d4f59SWei Liu */ 520f00d4f59SWei Liu sync_file_range(fs->fd, offset, ret, 521f00d4f59SWei Liu SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE); 522f00d4f59SWei Liu } 523f00d4f59SWei Liu #endif 524f00d4f59SWei Liu return ret; 525f00d4f59SWei Liu } 526f00d4f59SWei Liu 527f00d4f59SWei Liu static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp) 528f00d4f59SWei Liu { 529e3187a45SGreg Kurz char *dirpath = g_path_get_dirname(fs_path->data); 530e3187a45SGreg Kurz char *name = g_path_get_basename(fs_path->data); 531f00d4f59SWei Liu int ret = -1; 532e3187a45SGreg Kurz int dirfd; 533e3187a45SGreg Kurz 534e3187a45SGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dirpath); 535e3187a45SGreg Kurz if (dirfd == -1) { 536e3187a45SGreg Kurz goto out; 537e3187a45SGreg Kurz } 538f00d4f59SWei Liu 539f00d4f59SWei Liu if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 540e3187a45SGreg Kurz ret = local_set_xattrat(dirfd, name, credp); 541f00d4f59SWei Liu } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 542e3187a45SGreg Kurz ret = local_set_mapped_file_attrat(dirfd, name, credp); 543e3187a45SGreg Kurz } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH || 544e3187a45SGreg Kurz fs_ctx->export_flags & V9FS_SM_NONE) { 545e3187a45SGreg Kurz ret = fchmodat_nofollow(dirfd, name, credp->fc_mode); 546f00d4f59SWei Liu } 547e3187a45SGreg Kurz close_preserve_errno(dirfd); 548e3187a45SGreg Kurz 549e3187a45SGreg Kurz out: 550e3187a45SGreg Kurz g_free(dirpath); 551e3187a45SGreg Kurz g_free(name); 552f00d4f59SWei Liu return ret; 553f00d4f59SWei Liu } 554f00d4f59SWei Liu 555f00d4f59SWei Liu static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path, 556f00d4f59SWei Liu const char *name, FsCred *credp) 557f00d4f59SWei Liu { 558f00d4f59SWei Liu int err = -1; 559d815e721SGreg Kurz int dirfd; 560f00d4f59SWei Liu 561d815e721SGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dir_path->data); 562d815e721SGreg Kurz if (dirfd == -1) { 563d815e721SGreg Kurz return -1; 564d815e721SGreg Kurz } 565f00d4f59SWei Liu 566d815e721SGreg Kurz if (fs_ctx->export_flags & V9FS_SM_MAPPED || 567d815e721SGreg Kurz fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 568d815e721SGreg Kurz err = mknodat(dirfd, name, SM_LOCAL_MODE_BITS | S_IFREG, 0); 569d815e721SGreg Kurz if (err == -1) { 570d815e721SGreg Kurz goto out; 571d815e721SGreg Kurz } 572d815e721SGreg Kurz 573f00d4f59SWei Liu if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 574d815e721SGreg Kurz err = local_set_xattrat(dirfd, name, credp); 575d815e721SGreg Kurz } else { 576d815e721SGreg Kurz err = local_set_mapped_file_attrat(dirfd, name, credp); 577f00d4f59SWei Liu } 578f00d4f59SWei Liu if (err == -1) { 579f00d4f59SWei Liu goto err_end; 580f00d4f59SWei Liu } 581d815e721SGreg Kurz } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH || 582d815e721SGreg Kurz fs_ctx->export_flags & V9FS_SM_NONE) { 583d815e721SGreg Kurz err = mknodat(dirfd, name, credp->fc_mode, credp->fc_rdev); 584f00d4f59SWei Liu if (err == -1) { 585f00d4f59SWei Liu goto out; 586f00d4f59SWei Liu } 587d815e721SGreg Kurz err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp); 588f00d4f59SWei Liu if (err == -1) { 589f00d4f59SWei Liu goto err_end; 590f00d4f59SWei Liu } 591f00d4f59SWei Liu } 592f00d4f59SWei Liu goto out; 593f00d4f59SWei Liu 594f00d4f59SWei Liu err_end: 595d815e721SGreg Kurz unlinkat_preserve_errno(dirfd, name, 0); 596f00d4f59SWei Liu out: 597d815e721SGreg Kurz close_preserve_errno(dirfd); 598f00d4f59SWei Liu return err; 599f00d4f59SWei Liu } 600f00d4f59SWei Liu 601f00d4f59SWei Liu static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path, 602f00d4f59SWei Liu const char *name, FsCred *credp) 603f00d4f59SWei Liu { 604f00d4f59SWei Liu int err = -1; 6053f3a1699SGreg Kurz int dirfd; 606f00d4f59SWei Liu 6073f3a1699SGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dir_path->data); 6083f3a1699SGreg Kurz if (dirfd == -1) { 6093f3a1699SGreg Kurz return -1; 6103f3a1699SGreg Kurz } 611f00d4f59SWei Liu 6123f3a1699SGreg Kurz if (fs_ctx->export_flags & V9FS_SM_MAPPED || 6133f3a1699SGreg Kurz fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 6143f3a1699SGreg Kurz err = mkdirat(dirfd, name, SM_LOCAL_DIR_MODE_BITS); 6153f3a1699SGreg Kurz if (err == -1) { 6163f3a1699SGreg Kurz goto out; 6173f3a1699SGreg Kurz } 6183f3a1699SGreg Kurz credp->fc_mode = credp->fc_mode | S_IFDIR; 6193f3a1699SGreg Kurz 620f00d4f59SWei Liu if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 6213f3a1699SGreg Kurz err = local_set_xattrat(dirfd, name, credp); 6223f3a1699SGreg Kurz } else { 6233f3a1699SGreg Kurz err = local_set_mapped_file_attrat(dirfd, name, credp); 624f00d4f59SWei Liu } 625f00d4f59SWei Liu if (err == -1) { 626f00d4f59SWei Liu goto err_end; 627f00d4f59SWei Liu } 6283f3a1699SGreg Kurz } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH || 6293f3a1699SGreg Kurz fs_ctx->export_flags & V9FS_SM_NONE) { 6303f3a1699SGreg Kurz err = mkdirat(dirfd, name, credp->fc_mode); 631f00d4f59SWei Liu if (err == -1) { 632f00d4f59SWei Liu goto out; 633f00d4f59SWei Liu } 6343f3a1699SGreg Kurz err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp); 635f00d4f59SWei Liu if (err == -1) { 636f00d4f59SWei Liu goto err_end; 637f00d4f59SWei Liu } 638f00d4f59SWei Liu } 639f00d4f59SWei Liu goto out; 640f00d4f59SWei Liu 641f00d4f59SWei Liu err_end: 6423f3a1699SGreg Kurz unlinkat_preserve_errno(dirfd, name, AT_REMOVEDIR); 643f00d4f59SWei Liu out: 6443f3a1699SGreg Kurz close_preserve_errno(dirfd); 645f00d4f59SWei Liu return err; 646f00d4f59SWei Liu } 647f00d4f59SWei Liu 648f00d4f59SWei Liu static int local_fstat(FsContext *fs_ctx, int fid_type, 649f00d4f59SWei Liu V9fsFidOpenState *fs, struct stat *stbuf) 650f00d4f59SWei Liu { 651f00d4f59SWei Liu int err, fd; 652f00d4f59SWei Liu 653f00d4f59SWei Liu if (fid_type == P9_FID_DIR) { 654f314ea4eSGreg Kurz fd = dirfd(fs->dir.stream); 655f00d4f59SWei Liu } else { 656f00d4f59SWei Liu fd = fs->fd; 657f00d4f59SWei Liu } 658f00d4f59SWei Liu 659f00d4f59SWei Liu err = fstat(fd, stbuf); 660f00d4f59SWei Liu if (err) { 661f00d4f59SWei Liu return err; 662f00d4f59SWei Liu } 663f00d4f59SWei Liu if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 664f00d4f59SWei Liu /* Actual credentials are part of extended attrs */ 665f00d4f59SWei Liu uid_t tmp_uid; 666f00d4f59SWei Liu gid_t tmp_gid; 667f00d4f59SWei Liu mode_t tmp_mode; 668f00d4f59SWei Liu dev_t tmp_dev; 669f00d4f59SWei Liu 670f00d4f59SWei Liu if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) { 671f00d4f59SWei Liu stbuf->st_uid = le32_to_cpu(tmp_uid); 672f00d4f59SWei Liu } 673f00d4f59SWei Liu if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) { 674f00d4f59SWei Liu stbuf->st_gid = le32_to_cpu(tmp_gid); 675f00d4f59SWei Liu } 676f00d4f59SWei Liu if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) { 677f00d4f59SWei Liu stbuf->st_mode = le32_to_cpu(tmp_mode); 678f00d4f59SWei Liu } 679f00d4f59SWei Liu if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) { 680f00d4f59SWei Liu stbuf->st_rdev = le64_to_cpu(tmp_dev); 681f00d4f59SWei Liu } 682f00d4f59SWei Liu } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 683f00d4f59SWei Liu errno = EOPNOTSUPP; 684f00d4f59SWei Liu return -1; 685f00d4f59SWei Liu } 686f00d4f59SWei Liu return err; 687f00d4f59SWei Liu } 688f00d4f59SWei Liu 689f00d4f59SWei Liu static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name, 690f00d4f59SWei Liu int flags, FsCred *credp, V9fsFidOpenState *fs) 691f00d4f59SWei Liu { 692f00d4f59SWei Liu int fd = -1; 693f00d4f59SWei Liu int err = -1; 694a565fea5SGreg Kurz int dirfd; 695f00d4f59SWei Liu 696f00d4f59SWei Liu /* 697f00d4f59SWei Liu * Mark all the open to not follow symlinks 698f00d4f59SWei Liu */ 699f00d4f59SWei Liu flags |= O_NOFOLLOW; 700f00d4f59SWei Liu 701a565fea5SGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dir_path->data); 702a565fea5SGreg Kurz if (dirfd == -1) { 703a565fea5SGreg Kurz return -1; 704a565fea5SGreg Kurz } 705f00d4f59SWei Liu 706f00d4f59SWei Liu /* Determine the security model */ 707a565fea5SGreg Kurz if (fs_ctx->export_flags & V9FS_SM_MAPPED || 708a565fea5SGreg Kurz fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 709a565fea5SGreg Kurz fd = openat_file(dirfd, name, flags, SM_LOCAL_MODE_BITS); 710a565fea5SGreg Kurz if (fd == -1) { 711a565fea5SGreg Kurz goto out; 712a565fea5SGreg Kurz } 713a565fea5SGreg Kurz credp->fc_mode = credp->fc_mode|S_IFREG; 714f00d4f59SWei Liu if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 715f00d4f59SWei Liu /* Set cleint credentials in xattr */ 716a565fea5SGreg Kurz err = local_set_xattrat(dirfd, name, credp); 717a565fea5SGreg Kurz } else { 718a565fea5SGreg Kurz err = local_set_mapped_file_attrat(dirfd, name, credp); 719f00d4f59SWei Liu } 720f00d4f59SWei Liu if (err == -1) { 721f00d4f59SWei Liu goto err_end; 722f00d4f59SWei Liu } 723f00d4f59SWei Liu } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 724f00d4f59SWei Liu (fs_ctx->export_flags & V9FS_SM_NONE)) { 725a565fea5SGreg Kurz fd = openat_file(dirfd, name, flags, credp->fc_mode); 726f00d4f59SWei Liu if (fd == -1) { 727f00d4f59SWei Liu goto out; 728f00d4f59SWei Liu } 729a565fea5SGreg Kurz err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp); 730f00d4f59SWei Liu if (err == -1) { 731f00d4f59SWei Liu goto err_end; 732f00d4f59SWei Liu } 733f00d4f59SWei Liu } 734f00d4f59SWei Liu err = fd; 735f00d4f59SWei Liu fs->fd = fd; 736f00d4f59SWei Liu goto out; 737f00d4f59SWei Liu 738f00d4f59SWei Liu err_end: 739a565fea5SGreg Kurz unlinkat_preserve_errno(dirfd, name, 740a565fea5SGreg Kurz flags & O_DIRECTORY ? AT_REMOVEDIR : 0); 741a565fea5SGreg Kurz close_preserve_errno(fd); 742f00d4f59SWei Liu out: 743a565fea5SGreg Kurz close_preserve_errno(dirfd); 744f00d4f59SWei Liu return err; 745f00d4f59SWei Liu } 746f00d4f59SWei Liu 747f00d4f59SWei Liu 748f00d4f59SWei Liu static int local_symlink(FsContext *fs_ctx, const char *oldpath, 749f00d4f59SWei Liu V9fsPath *dir_path, const char *name, FsCred *credp) 750f00d4f59SWei Liu { 751f00d4f59SWei Liu int err = -1; 75238771613SGreg Kurz int dirfd; 753f00d4f59SWei Liu 75438771613SGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dir_path->data); 75538771613SGreg Kurz if (dirfd == -1) { 75638771613SGreg Kurz return -1; 75738771613SGreg Kurz } 758f00d4f59SWei Liu 759f00d4f59SWei Liu /* Determine the security model */ 76038771613SGreg Kurz if (fs_ctx->export_flags & V9FS_SM_MAPPED || 76138771613SGreg Kurz fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 76238771613SGreg Kurz int fd; 76338771613SGreg Kurz ssize_t oldpath_size, write_size; 76438771613SGreg Kurz 76538771613SGreg Kurz fd = openat_file(dirfd, name, O_CREAT | O_EXCL | O_RDWR, 76638771613SGreg Kurz SM_LOCAL_MODE_BITS); 76738771613SGreg Kurz if (fd == -1) { 76838771613SGreg Kurz goto out; 76938771613SGreg Kurz } 77038771613SGreg Kurz /* Write the oldpath (target) to the file. */ 77138771613SGreg Kurz oldpath_size = strlen(oldpath); 77238771613SGreg Kurz do { 77338771613SGreg Kurz write_size = write(fd, (void *)oldpath, oldpath_size); 77438771613SGreg Kurz } while (write_size == -1 && errno == EINTR); 77538771613SGreg Kurz close_preserve_errno(fd); 77638771613SGreg Kurz 77738771613SGreg Kurz if (write_size != oldpath_size) { 77838771613SGreg Kurz goto err_end; 77938771613SGreg Kurz } 78038771613SGreg Kurz /* Set cleint credentials in symlink's xattr */ 78138771613SGreg Kurz credp->fc_mode = credp->fc_mode | S_IFLNK; 78238771613SGreg Kurz 783f00d4f59SWei Liu if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 78438771613SGreg Kurz err = local_set_xattrat(dirfd, name, credp); 78538771613SGreg Kurz } else { 78638771613SGreg Kurz err = local_set_mapped_file_attrat(dirfd, name, credp); 787f00d4f59SWei Liu } 788f00d4f59SWei Liu if (err == -1) { 789f00d4f59SWei Liu goto err_end; 790f00d4f59SWei Liu } 79138771613SGreg Kurz } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH || 79238771613SGreg Kurz fs_ctx->export_flags & V9FS_SM_NONE) { 79338771613SGreg Kurz err = symlinkat(oldpath, dirfd, name); 794f00d4f59SWei Liu if (err) { 795f00d4f59SWei Liu goto out; 796f00d4f59SWei Liu } 79738771613SGreg Kurz err = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid, 79838771613SGreg Kurz AT_SYMLINK_NOFOLLOW); 799f00d4f59SWei Liu if (err == -1) { 800f00d4f59SWei Liu /* 801f00d4f59SWei Liu * If we fail to change ownership and if we are 802f00d4f59SWei Liu * using security model none. Ignore the error 803f00d4f59SWei Liu */ 804f00d4f59SWei Liu if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) { 805f00d4f59SWei Liu goto err_end; 80638771613SGreg Kurz } else { 807f00d4f59SWei Liu err = 0; 808f00d4f59SWei Liu } 809f00d4f59SWei Liu } 81038771613SGreg Kurz } 811f00d4f59SWei Liu goto out; 812f00d4f59SWei Liu 813f00d4f59SWei Liu err_end: 81438771613SGreg Kurz unlinkat_preserve_errno(dirfd, name, 0); 815f00d4f59SWei Liu out: 81638771613SGreg Kurz close_preserve_errno(dirfd); 817f00d4f59SWei Liu return err; 818f00d4f59SWei Liu } 819f00d4f59SWei Liu 820f00d4f59SWei Liu static int local_link(FsContext *ctx, V9fsPath *oldpath, 821f00d4f59SWei Liu V9fsPath *dirpath, const char *name) 822f00d4f59SWei Liu { 823ad0b46e6SGreg Kurz char *odirpath = g_path_get_dirname(oldpath->data); 824ad0b46e6SGreg Kurz char *oname = g_path_get_basename(oldpath->data); 825ad0b46e6SGreg Kurz int ret = -1; 826ad0b46e6SGreg Kurz int odirfd, ndirfd; 827f00d4f59SWei Liu 828ad0b46e6SGreg Kurz odirfd = local_opendir_nofollow(ctx, odirpath); 829ad0b46e6SGreg Kurz if (odirfd == -1) { 8306dd4b1f1SGreg Kurz goto out; 8316dd4b1f1SGreg Kurz } 832f00d4f59SWei Liu 833ad0b46e6SGreg Kurz ndirfd = local_opendir_nofollow(ctx, dirpath->data); 834ad0b46e6SGreg Kurz if (ndirfd == -1) { 835ad0b46e6SGreg Kurz close_preserve_errno(odirfd); 836ad0b46e6SGreg Kurz goto out; 837ad0b46e6SGreg Kurz } 838ad0b46e6SGreg Kurz 839ad0b46e6SGreg Kurz ret = linkat(odirfd, oname, ndirfd, name, 0); 840ad0b46e6SGreg Kurz if (ret < 0) { 841ad0b46e6SGreg Kurz goto out_close; 842ad0b46e6SGreg Kurz } 843f00d4f59SWei Liu 844f00d4f59SWei Liu /* now link the virtfs_metadata files */ 8456dd4b1f1SGreg Kurz if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 846ad0b46e6SGreg Kurz int omap_dirfd, nmap_dirfd; 8476dd4b1f1SGreg Kurz 848ad0b46e6SGreg Kurz ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700); 849ad0b46e6SGreg Kurz if (ret < 0 && errno != EEXIST) { 850ad0b46e6SGreg Kurz goto err_undo_link; 851f00d4f59SWei Liu } 852ad0b46e6SGreg Kurz 853ad0b46e6SGreg Kurz omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR); 854ad0b46e6SGreg Kurz if (omap_dirfd == -1) { 855ad0b46e6SGreg Kurz goto err; 856ad0b46e6SGreg Kurz } 857ad0b46e6SGreg Kurz 858ad0b46e6SGreg Kurz nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR); 859ad0b46e6SGreg Kurz if (nmap_dirfd == -1) { 860ad0b46e6SGreg Kurz close_preserve_errno(omap_dirfd); 861ad0b46e6SGreg Kurz goto err; 862ad0b46e6SGreg Kurz } 863ad0b46e6SGreg Kurz 864ad0b46e6SGreg Kurz ret = linkat(omap_dirfd, oname, nmap_dirfd, name, 0); 865ad0b46e6SGreg Kurz close_preserve_errno(nmap_dirfd); 866ad0b46e6SGreg Kurz close_preserve_errno(omap_dirfd); 867f00d4f59SWei Liu if (ret < 0 && errno != ENOENT) { 868ad0b46e6SGreg Kurz goto err_undo_link; 869f00d4f59SWei Liu } 8706dd4b1f1SGreg Kurz 871ad0b46e6SGreg Kurz ret = 0; 872f00d4f59SWei Liu } 873ad0b46e6SGreg Kurz goto out_close; 874ad0b46e6SGreg Kurz 875ad0b46e6SGreg Kurz err: 876ad0b46e6SGreg Kurz ret = -1; 877ad0b46e6SGreg Kurz err_undo_link: 878ad0b46e6SGreg Kurz unlinkat_preserve_errno(ndirfd, name, 0); 879ad0b46e6SGreg Kurz out_close: 880ad0b46e6SGreg Kurz close_preserve_errno(ndirfd); 881ad0b46e6SGreg Kurz close_preserve_errno(odirfd); 8826dd4b1f1SGreg Kurz out: 883ad0b46e6SGreg Kurz g_free(oname); 884ad0b46e6SGreg Kurz g_free(odirpath); 885f00d4f59SWei Liu return ret; 886f00d4f59SWei Liu } 887f00d4f59SWei Liu 888f00d4f59SWei Liu static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size) 889f00d4f59SWei Liu { 890ac125d99SGreg Kurz int fd, ret; 891f00d4f59SWei Liu 892ac125d99SGreg Kurz fd = local_open_nofollow(ctx, fs_path->data, O_WRONLY, 0); 893ac125d99SGreg Kurz if (fd == -1) { 894ac125d99SGreg Kurz return -1; 895ac125d99SGreg Kurz } 896ac125d99SGreg Kurz ret = ftruncate(fd, size); 897ac125d99SGreg Kurz close_preserve_errno(fd); 898f00d4f59SWei Liu return ret; 899f00d4f59SWei Liu } 900f00d4f59SWei Liu 901f00d4f59SWei Liu static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp) 902f00d4f59SWei Liu { 903d369f207SGreg Kurz char *dirpath = g_path_get_dirname(fs_path->data); 904d369f207SGreg Kurz char *name = g_path_get_basename(fs_path->data); 905f00d4f59SWei Liu int ret = -1; 906d369f207SGreg Kurz int dirfd; 907d369f207SGreg Kurz 908d369f207SGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dirpath); 909d369f207SGreg Kurz if (dirfd == -1) { 910d369f207SGreg Kurz goto out; 911d369f207SGreg Kurz } 912f00d4f59SWei Liu 913f00d4f59SWei Liu if ((credp->fc_uid == -1 && credp->fc_gid == -1) || 914f00d4f59SWei Liu (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 915f00d4f59SWei Liu (fs_ctx->export_flags & V9FS_SM_NONE)) { 916d369f207SGreg Kurz ret = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid, 917d369f207SGreg Kurz AT_SYMLINK_NOFOLLOW); 918f00d4f59SWei Liu } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 919d369f207SGreg Kurz ret = local_set_xattrat(dirfd, name, credp); 920f00d4f59SWei Liu } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 921d369f207SGreg Kurz ret = local_set_mapped_file_attrat(dirfd, name, credp); 922f00d4f59SWei Liu } 923d369f207SGreg Kurz 924d369f207SGreg Kurz close_preserve_errno(dirfd); 925d369f207SGreg Kurz out: 926d369f207SGreg Kurz g_free(name); 927d369f207SGreg Kurz g_free(dirpath); 928f00d4f59SWei Liu return ret; 929f00d4f59SWei Liu } 930f00d4f59SWei Liu 931f00d4f59SWei Liu static int local_utimensat(FsContext *s, V9fsPath *fs_path, 932f00d4f59SWei Liu const struct timespec *buf) 933f00d4f59SWei Liu { 934a33eda0dSGreg Kurz char *dirpath = g_path_get_dirname(fs_path->data); 935a33eda0dSGreg Kurz char *name = g_path_get_basename(fs_path->data); 936a33eda0dSGreg Kurz int dirfd, ret = -1; 937f00d4f59SWei Liu 938a33eda0dSGreg Kurz dirfd = local_opendir_nofollow(s, dirpath); 939a33eda0dSGreg Kurz if (dirfd == -1) { 940a33eda0dSGreg Kurz goto out; 941a33eda0dSGreg Kurz } 942a33eda0dSGreg Kurz 943a33eda0dSGreg Kurz ret = utimensat(dirfd, name, buf, AT_SYMLINK_NOFOLLOW); 944a33eda0dSGreg Kurz close_preserve_errno(dirfd); 945a33eda0dSGreg Kurz out: 946a33eda0dSGreg Kurz g_free(dirpath); 947a33eda0dSGreg Kurz g_free(name); 948f00d4f59SWei Liu return ret; 949f00d4f59SWei Liu } 950f00d4f59SWei Liu 951df4938a6SGreg Kurz static int local_unlinkat_common(FsContext *ctx, int dirfd, const char *name, 952df4938a6SGreg Kurz int flags) 953f00d4f59SWei Liu { 954df4938a6SGreg Kurz int ret = -1; 955f00d4f59SWei Liu 956f00d4f59SWei Liu if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 957df4938a6SGreg Kurz int map_dirfd; 958df4938a6SGreg Kurz 959df4938a6SGreg Kurz if (flags == AT_REMOVEDIR) { 960df4938a6SGreg Kurz int fd; 961df4938a6SGreg Kurz 962df4938a6SGreg Kurz fd = openat(dirfd, name, O_RDONLY | O_DIRECTORY | O_PATH); 963df4938a6SGreg Kurz if (fd == -1) { 964f00d4f59SWei Liu goto err_out; 965f00d4f59SWei Liu } 966f00d4f59SWei Liu /* 967f00d4f59SWei Liu * If directory remove .virtfs_metadata contained in the 968f00d4f59SWei Liu * directory 969f00d4f59SWei Liu */ 970df4938a6SGreg Kurz ret = unlinkat(fd, VIRTFS_META_DIR, AT_REMOVEDIR); 971df4938a6SGreg Kurz close_preserve_errno(fd); 972df4938a6SGreg Kurz if (ret < 0 && errno != ENOENT) { 973f00d4f59SWei Liu /* 974f00d4f59SWei Liu * We didn't had the .virtfs_metadata file. May be file created 975f00d4f59SWei Liu * in non-mapped mode ?. Ignore ENOENT. 976f00d4f59SWei Liu */ 977f00d4f59SWei Liu goto err_out; 978f00d4f59SWei Liu } 979f00d4f59SWei Liu } 980f00d4f59SWei Liu /* 981f00d4f59SWei Liu * Now remove the name from parent directory 982df4938a6SGreg Kurz * .virtfs_metadata directory. 983f00d4f59SWei Liu */ 984df4938a6SGreg Kurz map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR); 985df4938a6SGreg Kurz ret = unlinkat(map_dirfd, name, 0); 986df4938a6SGreg Kurz close_preserve_errno(map_dirfd); 987df4938a6SGreg Kurz if (ret < 0 && errno != ENOENT) { 988f00d4f59SWei Liu /* 989f00d4f59SWei Liu * We didn't had the .virtfs_metadata file. May be file created 990f00d4f59SWei Liu * in non-mapped mode ?. Ignore ENOENT. 991f00d4f59SWei Liu */ 992f00d4f59SWei Liu goto err_out; 993f00d4f59SWei Liu } 994f00d4f59SWei Liu } 995f00d4f59SWei Liu 996df4938a6SGreg Kurz ret = unlinkat(dirfd, name, flags); 997f00d4f59SWei Liu err_out: 998df4938a6SGreg Kurz return ret; 999df4938a6SGreg Kurz } 1000df4938a6SGreg Kurz 1001f00d4f59SWei Liu static int local_remove(FsContext *ctx, const char *path) 1002f00d4f59SWei Liu { 1003f00d4f59SWei Liu struct stat stbuf; 1004a0e640a8SGreg Kurz char *dirpath = g_path_get_dirname(path); 1005a0e640a8SGreg Kurz char *name = g_path_get_basename(path); 1006a0e640a8SGreg Kurz int flags = 0; 1007a0e640a8SGreg Kurz int dirfd; 1008a0e640a8SGreg Kurz int err = -1; 1009f00d4f59SWei Liu 1010a0e640a8SGreg Kurz dirfd = local_opendir_nofollow(ctx, dirpath); 1011*b7361d46SGreg Kurz if (dirfd == -1) { 1012a0e640a8SGreg Kurz goto out; 1013a0e640a8SGreg Kurz } 1014a0e640a8SGreg Kurz 1015a0e640a8SGreg Kurz if (fstatat(dirfd, path, &stbuf, AT_SYMLINK_NOFOLLOW) < 0) { 1016f00d4f59SWei Liu goto err_out; 1017f00d4f59SWei Liu } 1018a0e640a8SGreg Kurz 1019f00d4f59SWei Liu if (S_ISDIR(stbuf.st_mode)) { 1020a0e640a8SGreg Kurz flags |= AT_REMOVEDIR; 1021f00d4f59SWei Liu } 1022f00d4f59SWei Liu 1023a0e640a8SGreg Kurz err = local_unlinkat_common(ctx, dirfd, name, flags); 1024f00d4f59SWei Liu err_out: 1025a0e640a8SGreg Kurz close_preserve_errno(dirfd); 1026a0e640a8SGreg Kurz out: 1027a0e640a8SGreg Kurz g_free(name); 1028a0e640a8SGreg Kurz g_free(dirpath); 1029f00d4f59SWei Liu return err; 1030f00d4f59SWei Liu } 1031f00d4f59SWei Liu 1032f00d4f59SWei Liu static int local_fsync(FsContext *ctx, int fid_type, 1033f00d4f59SWei Liu V9fsFidOpenState *fs, int datasync) 1034f00d4f59SWei Liu { 1035f00d4f59SWei Liu int fd; 1036f00d4f59SWei Liu 1037f00d4f59SWei Liu if (fid_type == P9_FID_DIR) { 1038f314ea4eSGreg Kurz fd = dirfd(fs->dir.stream); 1039f00d4f59SWei Liu } else { 1040f00d4f59SWei Liu fd = fs->fd; 1041f00d4f59SWei Liu } 1042f00d4f59SWei Liu 1043f00d4f59SWei Liu if (datasync) { 1044f00d4f59SWei Liu return qemu_fdatasync(fd); 1045f00d4f59SWei Liu } else { 1046f00d4f59SWei Liu return fsync(fd); 1047f00d4f59SWei Liu } 1048f00d4f59SWei Liu } 1049f00d4f59SWei Liu 1050f00d4f59SWei Liu static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf) 1051f00d4f59SWei Liu { 105231e51d1cSGreg Kurz int fd, ret; 1053f00d4f59SWei Liu 105431e51d1cSGreg Kurz fd = local_open_nofollow(s, fs_path->data, O_RDONLY, 0); 105531e51d1cSGreg Kurz ret = fstatfs(fd, stbuf); 105631e51d1cSGreg Kurz close_preserve_errno(fd); 1057f00d4f59SWei Liu return ret; 1058f00d4f59SWei Liu } 1059f00d4f59SWei Liu 1060f00d4f59SWei Liu static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path, 1061f00d4f59SWei Liu const char *name, void *value, size_t size) 1062f00d4f59SWei Liu { 1063f00d4f59SWei Liu char *path = fs_path->data; 1064f00d4f59SWei Liu 1065f00d4f59SWei Liu return v9fs_get_xattr(ctx, path, name, value, size); 1066f00d4f59SWei Liu } 1067f00d4f59SWei Liu 1068f00d4f59SWei Liu static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path, 1069f00d4f59SWei Liu void *value, size_t size) 1070f00d4f59SWei Liu { 1071f00d4f59SWei Liu char *path = fs_path->data; 1072f00d4f59SWei Liu 1073f00d4f59SWei Liu return v9fs_list_xattr(ctx, path, value, size); 1074f00d4f59SWei Liu } 1075f00d4f59SWei Liu 1076f00d4f59SWei Liu static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name, 1077f00d4f59SWei Liu void *value, size_t size, int flags) 1078f00d4f59SWei Liu { 1079f00d4f59SWei Liu char *path = fs_path->data; 1080f00d4f59SWei Liu 1081f00d4f59SWei Liu return v9fs_set_xattr(ctx, path, name, value, size, flags); 1082f00d4f59SWei Liu } 1083f00d4f59SWei Liu 1084f00d4f59SWei Liu static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path, 1085f00d4f59SWei Liu const char *name) 1086f00d4f59SWei Liu { 1087f00d4f59SWei Liu char *path = fs_path->data; 1088f00d4f59SWei Liu 1089f00d4f59SWei Liu return v9fs_remove_xattr(ctx, path, name); 1090f00d4f59SWei Liu } 1091f00d4f59SWei Liu 1092f00d4f59SWei Liu static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path, 1093f00d4f59SWei Liu const char *name, V9fsPath *target) 1094f00d4f59SWei Liu { 1095f00d4f59SWei Liu if (dir_path) { 1096e3e83f2eSGreg Kurz v9fs_path_sprintf(target, "%s/%s", dir_path->data, name); 1097f00d4f59SWei Liu } else { 1098e3e83f2eSGreg Kurz v9fs_path_sprintf(target, "%s", name); 1099f00d4f59SWei Liu } 1100f00d4f59SWei Liu return 0; 1101f00d4f59SWei Liu } 1102f00d4f59SWei Liu 1103f00d4f59SWei Liu static int local_renameat(FsContext *ctx, V9fsPath *olddir, 1104f00d4f59SWei Liu const char *old_name, V9fsPath *newdir, 1105f00d4f59SWei Liu const char *new_name) 1106f00d4f59SWei Liu { 1107f00d4f59SWei Liu int ret; 110899f2cf4bSGreg Kurz int odirfd, ndirfd; 1109f00d4f59SWei Liu 111099f2cf4bSGreg Kurz odirfd = local_opendir_nofollow(ctx, olddir->data); 111199f2cf4bSGreg Kurz if (odirfd == -1) { 111299f2cf4bSGreg Kurz return -1; 111399f2cf4bSGreg Kurz } 1114f00d4f59SWei Liu 111599f2cf4bSGreg Kurz ndirfd = local_opendir_nofollow(ctx, newdir->data); 111699f2cf4bSGreg Kurz if (ndirfd == -1) { 111799f2cf4bSGreg Kurz close_preserve_errno(odirfd); 111899f2cf4bSGreg Kurz return -1; 111999f2cf4bSGreg Kurz } 1120f00d4f59SWei Liu 112199f2cf4bSGreg Kurz ret = renameat(odirfd, old_name, ndirfd, new_name); 112299f2cf4bSGreg Kurz if (ret < 0) { 112399f2cf4bSGreg Kurz goto out; 112499f2cf4bSGreg Kurz } 112599f2cf4bSGreg Kurz 112699f2cf4bSGreg Kurz if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 112799f2cf4bSGreg Kurz int omap_dirfd, nmap_dirfd; 112899f2cf4bSGreg Kurz 112999f2cf4bSGreg Kurz ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700); 113099f2cf4bSGreg Kurz if (ret < 0 && errno != EEXIST) { 113199f2cf4bSGreg Kurz goto err_undo_rename; 113299f2cf4bSGreg Kurz } 113399f2cf4bSGreg Kurz 11346dd4b1f1SGreg Kurz omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR); 113599f2cf4bSGreg Kurz if (omap_dirfd == -1) { 113699f2cf4bSGreg Kurz goto err; 113799f2cf4bSGreg Kurz } 113899f2cf4bSGreg Kurz 11396dd4b1f1SGreg Kurz nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR); 114099f2cf4bSGreg Kurz if (nmap_dirfd == -1) { 114199f2cf4bSGreg Kurz close_preserve_errno(omap_dirfd); 114299f2cf4bSGreg Kurz goto err; 114399f2cf4bSGreg Kurz } 114499f2cf4bSGreg Kurz 114599f2cf4bSGreg Kurz /* rename the .virtfs_metadata files */ 114699f2cf4bSGreg Kurz ret = renameat(omap_dirfd, old_name, nmap_dirfd, new_name); 114799f2cf4bSGreg Kurz close_preserve_errno(nmap_dirfd); 114899f2cf4bSGreg Kurz close_preserve_errno(omap_dirfd); 114999f2cf4bSGreg Kurz if (ret < 0 && errno != ENOENT) { 115099f2cf4bSGreg Kurz goto err_undo_rename; 115199f2cf4bSGreg Kurz } 115299f2cf4bSGreg Kurz 115399f2cf4bSGreg Kurz ret = 0; 115499f2cf4bSGreg Kurz } 115599f2cf4bSGreg Kurz goto out; 115699f2cf4bSGreg Kurz 115799f2cf4bSGreg Kurz err: 115899f2cf4bSGreg Kurz ret = -1; 115999f2cf4bSGreg Kurz err_undo_rename: 116099f2cf4bSGreg Kurz renameat_preserve_errno(ndirfd, new_name, odirfd, old_name); 116199f2cf4bSGreg Kurz out: 116299f2cf4bSGreg Kurz close_preserve_errno(ndirfd); 116399f2cf4bSGreg Kurz close_preserve_errno(odirfd); 1164f00d4f59SWei Liu return ret; 1165f00d4f59SWei Liu } 1166f00d4f59SWei Liu 1167d2767edeSGreg Kurz static void v9fs_path_init_dirname(V9fsPath *path, const char *str) 1168d2767edeSGreg Kurz { 1169d2767edeSGreg Kurz path->data = g_path_get_dirname(str); 1170d2767edeSGreg Kurz path->size = strlen(path->data) + 1; 1171d2767edeSGreg Kurz } 1172d2767edeSGreg Kurz 1173d2767edeSGreg Kurz static int local_rename(FsContext *ctx, const char *oldpath, 1174d2767edeSGreg Kurz const char *newpath) 1175d2767edeSGreg Kurz { 1176d2767edeSGreg Kurz int err; 1177d2767edeSGreg Kurz char *oname = g_path_get_basename(oldpath); 1178d2767edeSGreg Kurz char *nname = g_path_get_basename(newpath); 1179d2767edeSGreg Kurz V9fsPath olddir, newdir; 1180d2767edeSGreg Kurz 1181d2767edeSGreg Kurz v9fs_path_init_dirname(&olddir, oldpath); 1182d2767edeSGreg Kurz v9fs_path_init_dirname(&newdir, newpath); 1183d2767edeSGreg Kurz 1184d2767edeSGreg Kurz err = local_renameat(ctx, &olddir, oname, &newdir, nname); 1185d2767edeSGreg Kurz 1186d2767edeSGreg Kurz v9fs_path_free(&newdir); 1187d2767edeSGreg Kurz v9fs_path_free(&olddir); 1188d2767edeSGreg Kurz g_free(nname); 1189d2767edeSGreg Kurz g_free(oname); 1190d2767edeSGreg Kurz 1191d2767edeSGreg Kurz return err; 1192d2767edeSGreg Kurz } 1193d2767edeSGreg Kurz 1194f00d4f59SWei Liu static int local_unlinkat(FsContext *ctx, V9fsPath *dir, 1195f00d4f59SWei Liu const char *name, int flags) 1196f00d4f59SWei Liu { 1197f00d4f59SWei Liu int ret; 1198df4938a6SGreg Kurz int dirfd; 1199f00d4f59SWei Liu 1200df4938a6SGreg Kurz dirfd = local_opendir_nofollow(ctx, dir->data); 1201df4938a6SGreg Kurz if (dirfd == -1) { 1202df4938a6SGreg Kurz return -1; 1203df4938a6SGreg Kurz } 1204f00d4f59SWei Liu 1205df4938a6SGreg Kurz ret = local_unlinkat_common(ctx, dirfd, name, flags); 1206df4938a6SGreg Kurz close_preserve_errno(dirfd); 1207f00d4f59SWei Liu return ret; 1208f00d4f59SWei Liu } 1209f00d4f59SWei Liu 1210f00d4f59SWei Liu static int local_ioc_getversion(FsContext *ctx, V9fsPath *path, 1211f00d4f59SWei Liu mode_t st_mode, uint64_t *st_gen) 1212f00d4f59SWei Liu { 1213f00d4f59SWei Liu #ifdef FS_IOC_GETVERSION 1214f00d4f59SWei Liu int err; 1215f00d4f59SWei Liu V9fsFidOpenState fid_open; 1216f00d4f59SWei Liu 1217f00d4f59SWei Liu /* 1218f00d4f59SWei Liu * Do not try to open special files like device nodes, fifos etc 1219f00d4f59SWei Liu * We can get fd for regular files and directories only 1220f00d4f59SWei Liu */ 1221f00d4f59SWei Liu if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) { 1222f00d4f59SWei Liu errno = ENOTTY; 1223f00d4f59SWei Liu return -1; 1224f00d4f59SWei Liu } 1225f00d4f59SWei Liu err = local_open(ctx, path, O_RDONLY, &fid_open); 1226f00d4f59SWei Liu if (err < 0) { 1227f00d4f59SWei Liu return err; 1228f00d4f59SWei Liu } 1229f00d4f59SWei Liu err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen); 1230f00d4f59SWei Liu local_close(ctx, &fid_open); 1231f00d4f59SWei Liu return err; 1232f00d4f59SWei Liu #else 1233f00d4f59SWei Liu errno = ENOTTY; 1234f00d4f59SWei Liu return -1; 1235f00d4f59SWei Liu #endif 1236f00d4f59SWei Liu } 1237f00d4f59SWei Liu 1238f00d4f59SWei Liu static int local_init(FsContext *ctx) 1239f00d4f59SWei Liu { 1240f00d4f59SWei Liu struct statfs stbuf; 12410e35a378SGreg Kurz LocalData *data = g_malloc(sizeof(*data)); 12420e35a378SGreg Kurz 12430e35a378SGreg Kurz data->mountfd = open(ctx->fs_root, O_DIRECTORY | O_RDONLY); 12440e35a378SGreg Kurz if (data->mountfd == -1) { 12450e35a378SGreg Kurz goto err; 12460e35a378SGreg Kurz } 1247f00d4f59SWei Liu 124800c90bd1SGreg Kurz #ifdef FS_IOC_GETVERSION 124900c90bd1SGreg Kurz /* 125000c90bd1SGreg Kurz * use ioc_getversion only if the ioctl is definied 125100c90bd1SGreg Kurz */ 12520e35a378SGreg Kurz if (fstatfs(data->mountfd, &stbuf) < 0) { 12530e35a378SGreg Kurz close_preserve_errno(data->mountfd); 12540e35a378SGreg Kurz goto err; 125500c90bd1SGreg Kurz } 125600c90bd1SGreg Kurz switch (stbuf.f_type) { 125700c90bd1SGreg Kurz case EXT2_SUPER_MAGIC: 125800c90bd1SGreg Kurz case BTRFS_SUPER_MAGIC: 125900c90bd1SGreg Kurz case REISERFS_SUPER_MAGIC: 126000c90bd1SGreg Kurz case XFS_SUPER_MAGIC: 126100c90bd1SGreg Kurz ctx->exops.get_st_gen = local_ioc_getversion; 126200c90bd1SGreg Kurz break; 126300c90bd1SGreg Kurz } 126400c90bd1SGreg Kurz #endif 1265f00d4f59SWei Liu 1266f00d4f59SWei Liu if (ctx->export_flags & V9FS_SM_PASSTHROUGH) { 1267f00d4f59SWei Liu ctx->xops = passthrough_xattr_ops; 1268f00d4f59SWei Liu } else if (ctx->export_flags & V9FS_SM_MAPPED) { 1269f00d4f59SWei Liu ctx->xops = mapped_xattr_ops; 1270f00d4f59SWei Liu } else if (ctx->export_flags & V9FS_SM_NONE) { 1271f00d4f59SWei Liu ctx->xops = none_xattr_ops; 1272f00d4f59SWei Liu } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 1273f00d4f59SWei Liu /* 1274f00d4f59SWei Liu * xattr operation for mapped-file and passthrough 1275f00d4f59SWei Liu * remain same. 1276f00d4f59SWei Liu */ 1277f00d4f59SWei Liu ctx->xops = passthrough_xattr_ops; 1278f00d4f59SWei Liu } 1279f00d4f59SWei Liu ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT; 128000c90bd1SGreg Kurz 12810e35a378SGreg Kurz ctx->private = data; 128200c90bd1SGreg Kurz return 0; 12830e35a378SGreg Kurz 12840e35a378SGreg Kurz err: 12850e35a378SGreg Kurz g_free(data); 12860e35a378SGreg Kurz return -1; 1287f00d4f59SWei Liu } 12880e35a378SGreg Kurz 12890e35a378SGreg Kurz static void local_cleanup(FsContext *ctx) 12900e35a378SGreg Kurz { 12910e35a378SGreg Kurz LocalData *data = ctx->private; 12920e35a378SGreg Kurz 12930e35a378SGreg Kurz close(data->mountfd); 12940e35a378SGreg Kurz g_free(data); 1295f00d4f59SWei Liu } 1296f00d4f59SWei Liu 1297f00d4f59SWei Liu static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse) 1298f00d4f59SWei Liu { 1299f00d4f59SWei Liu const char *sec_model = qemu_opt_get(opts, "security_model"); 1300f00d4f59SWei Liu const char *path = qemu_opt_get(opts, "path"); 1301b8bbdb88SPradeep Jagadeesh Error *err = NULL; 1302f00d4f59SWei Liu 1303f00d4f59SWei Liu if (!sec_model) { 130463325b18SGreg Kurz error_report("Security model not specified, local fs needs security model"); 130563325b18SGreg Kurz error_printf("valid options are:" 130663325b18SGreg Kurz "\tsecurity_model=[passthrough|mapped-xattr|mapped-file|none]\n"); 1307f00d4f59SWei Liu return -1; 1308f00d4f59SWei Liu } 1309f00d4f59SWei Liu 1310f00d4f59SWei Liu if (!strcmp(sec_model, "passthrough")) { 1311f00d4f59SWei Liu fse->export_flags |= V9FS_SM_PASSTHROUGH; 1312f00d4f59SWei Liu } else if (!strcmp(sec_model, "mapped") || 1313f00d4f59SWei Liu !strcmp(sec_model, "mapped-xattr")) { 1314f00d4f59SWei Liu fse->export_flags |= V9FS_SM_MAPPED; 1315f00d4f59SWei Liu } else if (!strcmp(sec_model, "none")) { 1316f00d4f59SWei Liu fse->export_flags |= V9FS_SM_NONE; 1317f00d4f59SWei Liu } else if (!strcmp(sec_model, "mapped-file")) { 1318f00d4f59SWei Liu fse->export_flags |= V9FS_SM_MAPPED_FILE; 1319f00d4f59SWei Liu } else { 132063325b18SGreg Kurz error_report("Invalid security model %s specified", sec_model); 132163325b18SGreg Kurz error_printf("valid options are:" 132263325b18SGreg Kurz "\t[passthrough|mapped-xattr|mapped-file|none]\n"); 1323f00d4f59SWei Liu return -1; 1324f00d4f59SWei Liu } 1325f00d4f59SWei Liu 1326f00d4f59SWei Liu if (!path) { 132763325b18SGreg Kurz error_report("fsdev: No path specified"); 1328f00d4f59SWei Liu return -1; 1329f00d4f59SWei Liu } 1330b8bbdb88SPradeep Jagadeesh 1331b8bbdb88SPradeep Jagadeesh fsdev_throttle_parse_opts(opts, &fse->fst, &err); 1332b8bbdb88SPradeep Jagadeesh if (err) { 1333b8bbdb88SPradeep Jagadeesh error_reportf_err(err, "Throttle configuration is not valid: "); 1334b8bbdb88SPradeep Jagadeesh return -1; 1335b8bbdb88SPradeep Jagadeesh } 1336b8bbdb88SPradeep Jagadeesh 1337f00d4f59SWei Liu fse->path = g_strdup(path); 1338f00d4f59SWei Liu 1339f00d4f59SWei Liu return 0; 1340f00d4f59SWei Liu } 1341f00d4f59SWei Liu 1342f00d4f59SWei Liu FileOperations local_ops = { 1343f00d4f59SWei Liu .parse_opts = local_parse_opts, 1344f00d4f59SWei Liu .init = local_init, 13450e35a378SGreg Kurz .cleanup = local_cleanup, 1346f00d4f59SWei Liu .lstat = local_lstat, 1347f00d4f59SWei Liu .readlink = local_readlink, 1348f00d4f59SWei Liu .close = local_close, 1349f00d4f59SWei Liu .closedir = local_closedir, 1350f00d4f59SWei Liu .open = local_open, 1351f00d4f59SWei Liu .opendir = local_opendir, 1352f00d4f59SWei Liu .rewinddir = local_rewinddir, 1353f00d4f59SWei Liu .telldir = local_telldir, 1354635324e8SGreg Kurz .readdir = local_readdir, 1355f00d4f59SWei Liu .seekdir = local_seekdir, 1356f00d4f59SWei Liu .preadv = local_preadv, 1357f00d4f59SWei Liu .pwritev = local_pwritev, 1358f00d4f59SWei Liu .chmod = local_chmod, 1359f00d4f59SWei Liu .mknod = local_mknod, 1360f00d4f59SWei Liu .mkdir = local_mkdir, 1361f00d4f59SWei Liu .fstat = local_fstat, 1362f00d4f59SWei Liu .open2 = local_open2, 1363f00d4f59SWei Liu .symlink = local_symlink, 1364f00d4f59SWei Liu .link = local_link, 1365f00d4f59SWei Liu .truncate = local_truncate, 1366f00d4f59SWei Liu .rename = local_rename, 1367f00d4f59SWei Liu .chown = local_chown, 1368f00d4f59SWei Liu .utimensat = local_utimensat, 1369f00d4f59SWei Liu .remove = local_remove, 1370f00d4f59SWei Liu .fsync = local_fsync, 1371f00d4f59SWei Liu .statfs = local_statfs, 1372f00d4f59SWei Liu .lgetxattr = local_lgetxattr, 1373f00d4f59SWei Liu .llistxattr = local_llistxattr, 1374f00d4f59SWei Liu .lsetxattr = local_lsetxattr, 1375f00d4f59SWei Liu .lremovexattr = local_lremovexattr, 1376f00d4f59SWei Liu .name_to_path = local_name_to_path, 1377f00d4f59SWei Liu .renameat = local_renameat, 1378f00d4f59SWei Liu .unlinkat = local_unlinkat, 1379f00d4f59SWei Liu }; 1380