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 70*99f2cf4bSGreg Kurz static void renameat_preserve_errno(int odirfd, const char *opath, int ndirfd, 71*99f2cf4bSGreg Kurz const char *npath) 72*99f2cf4bSGreg Kurz { 73*99f2cf4bSGreg Kurz int serrno = errno; 74*99f2cf4bSGreg Kurz renameat(odirfd, opath, ndirfd, npath); 75*99f2cf4bSGreg Kurz errno = serrno; 76*99f2cf4bSGreg Kurz } 77*99f2cf4bSGreg Kurz 78f00d4f59SWei Liu #define VIRTFS_META_DIR ".virtfs_metadata" 79f00d4f59SWei Liu 80f00d4f59SWei Liu static char *local_mapped_attr_path(FsContext *ctx, const char *path) 81f00d4f59SWei Liu { 82f00d4f59SWei Liu int dirlen; 83f00d4f59SWei Liu const char *name = strrchr(path, '/'); 84f00d4f59SWei Liu if (name) { 85f00d4f59SWei Liu dirlen = name - path; 86f00d4f59SWei Liu ++name; 87f00d4f59SWei Liu } else { 88f00d4f59SWei Liu name = path; 89f00d4f59SWei Liu dirlen = 0; 90f00d4f59SWei Liu } 91f00d4f59SWei Liu return g_strdup_printf("%s/%.*s/%s/%s", ctx->fs_root, 92f00d4f59SWei Liu dirlen, path, VIRTFS_META_DIR, name); 93f00d4f59SWei Liu } 94f00d4f59SWei Liu 95f00d4f59SWei Liu static FILE *local_fopen(const char *path, const char *mode) 96f00d4f59SWei Liu { 97f00d4f59SWei Liu int fd, o_mode = 0; 98f00d4f59SWei Liu FILE *fp; 99f00d4f59SWei Liu int flags = O_NOFOLLOW; 100f00d4f59SWei Liu /* 101f00d4f59SWei Liu * only supports two modes 102f00d4f59SWei Liu */ 103f00d4f59SWei Liu if (mode[0] == 'r') { 104f00d4f59SWei Liu flags |= O_RDONLY; 105f00d4f59SWei Liu } else if (mode[0] == 'w') { 106f00d4f59SWei Liu flags |= O_WRONLY | O_TRUNC | O_CREAT; 107f00d4f59SWei Liu o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; 108f00d4f59SWei Liu } else { 109f00d4f59SWei Liu return NULL; 110f00d4f59SWei Liu } 111f00d4f59SWei Liu fd = open(path, flags, o_mode); 112f00d4f59SWei Liu if (fd == -1) { 113f00d4f59SWei Liu return NULL; 114f00d4f59SWei Liu } 115f00d4f59SWei Liu fp = fdopen(fd, mode); 116f00d4f59SWei Liu if (!fp) { 117f00d4f59SWei Liu close(fd); 118f00d4f59SWei Liu } 119f00d4f59SWei Liu return fp; 120f00d4f59SWei Liu } 121f00d4f59SWei Liu 122f9aef99bSGreg Kurz static FILE *local_fopenat(int dirfd, const char *name, const char *mode) 123f9aef99bSGreg Kurz { 124f9aef99bSGreg Kurz int fd, o_mode = 0; 125f9aef99bSGreg Kurz FILE *fp; 126f9aef99bSGreg Kurz int flags; 127f9aef99bSGreg Kurz /* 128f9aef99bSGreg Kurz * only supports two modes 129f9aef99bSGreg Kurz */ 130f9aef99bSGreg Kurz if (mode[0] == 'r') { 131f9aef99bSGreg Kurz flags = O_RDONLY; 132f9aef99bSGreg Kurz } else if (mode[0] == 'w') { 133f9aef99bSGreg Kurz flags = O_WRONLY | O_TRUNC | O_CREAT; 134f9aef99bSGreg Kurz o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; 135f9aef99bSGreg Kurz } else { 136f9aef99bSGreg Kurz return NULL; 137f9aef99bSGreg Kurz } 138f9aef99bSGreg Kurz fd = openat_file(dirfd, name, flags, o_mode); 139f9aef99bSGreg Kurz if (fd == -1) { 140f9aef99bSGreg Kurz return NULL; 141f9aef99bSGreg Kurz } 142f9aef99bSGreg Kurz fp = fdopen(fd, mode); 143f9aef99bSGreg Kurz if (!fp) { 144f9aef99bSGreg Kurz close(fd); 145f9aef99bSGreg Kurz } 146f9aef99bSGreg Kurz return fp; 147f9aef99bSGreg Kurz } 148f9aef99bSGreg Kurz 149f00d4f59SWei Liu #define ATTR_MAX 100 150f9aef99bSGreg Kurz static void local_mapped_file_attr(int dirfd, const char *name, 151f00d4f59SWei Liu struct stat *stbuf) 152f00d4f59SWei Liu { 153f00d4f59SWei Liu FILE *fp; 154f00d4f59SWei Liu char buf[ATTR_MAX]; 155f9aef99bSGreg Kurz int map_dirfd; 156f00d4f59SWei Liu 157*99f2cf4bSGreg Kurz map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR); 158f9aef99bSGreg Kurz if (map_dirfd == -1) { 159f9aef99bSGreg Kurz return; 160f9aef99bSGreg Kurz } 161f9aef99bSGreg Kurz 162f9aef99bSGreg Kurz fp = local_fopenat(map_dirfd, name, "r"); 163f9aef99bSGreg Kurz close_preserve_errno(map_dirfd); 164f00d4f59SWei Liu if (!fp) { 165f00d4f59SWei Liu return; 166f00d4f59SWei Liu } 167f00d4f59SWei Liu memset(buf, 0, ATTR_MAX); 168f00d4f59SWei Liu while (fgets(buf, ATTR_MAX, fp)) { 169f00d4f59SWei Liu if (!strncmp(buf, "virtfs.uid", 10)) { 170f00d4f59SWei Liu stbuf->st_uid = atoi(buf+11); 171f00d4f59SWei Liu } else if (!strncmp(buf, "virtfs.gid", 10)) { 172f00d4f59SWei Liu stbuf->st_gid = atoi(buf+11); 173f00d4f59SWei Liu } else if (!strncmp(buf, "virtfs.mode", 11)) { 174f00d4f59SWei Liu stbuf->st_mode = atoi(buf+12); 175f00d4f59SWei Liu } else if (!strncmp(buf, "virtfs.rdev", 11)) { 176f00d4f59SWei Liu stbuf->st_rdev = atoi(buf+12); 177f00d4f59SWei Liu } 178f00d4f59SWei Liu memset(buf, 0, ATTR_MAX); 179f00d4f59SWei Liu } 180f00d4f59SWei Liu fclose(fp); 181f00d4f59SWei Liu } 182f00d4f59SWei Liu 183f00d4f59SWei Liu static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf) 184f00d4f59SWei Liu { 185f9aef99bSGreg Kurz int err = -1; 186f9aef99bSGreg Kurz char *dirpath = g_path_get_dirname(fs_path->data); 187f9aef99bSGreg Kurz char *name = g_path_get_basename(fs_path->data); 188f9aef99bSGreg Kurz int dirfd; 189f00d4f59SWei Liu 190f9aef99bSGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dirpath); 191f9aef99bSGreg Kurz if (dirfd == -1) { 192f9aef99bSGreg Kurz goto out; 193f9aef99bSGreg Kurz } 194f9aef99bSGreg Kurz 195f9aef99bSGreg Kurz err = fstatat(dirfd, name, stbuf, AT_SYMLINK_NOFOLLOW); 196f00d4f59SWei Liu if (err) { 197f00d4f59SWei Liu goto err_out; 198f00d4f59SWei Liu } 199f00d4f59SWei Liu if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 200f00d4f59SWei Liu /* Actual credentials are part of extended attrs */ 201f00d4f59SWei Liu uid_t tmp_uid; 202f00d4f59SWei Liu gid_t tmp_gid; 203f00d4f59SWei Liu mode_t tmp_mode; 204f00d4f59SWei Liu dev_t tmp_dev; 205f9aef99bSGreg Kurz 206f9aef99bSGreg Kurz if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.uid", &tmp_uid, 207f9aef99bSGreg Kurz sizeof(uid_t)) > 0) { 208f00d4f59SWei Liu stbuf->st_uid = le32_to_cpu(tmp_uid); 209f00d4f59SWei Liu } 210f9aef99bSGreg Kurz if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.gid", &tmp_gid, 211f9aef99bSGreg Kurz sizeof(gid_t)) > 0) { 212f00d4f59SWei Liu stbuf->st_gid = le32_to_cpu(tmp_gid); 213f00d4f59SWei Liu } 214f9aef99bSGreg Kurz if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.mode", &tmp_mode, 215f9aef99bSGreg Kurz sizeof(mode_t)) > 0) { 216f00d4f59SWei Liu stbuf->st_mode = le32_to_cpu(tmp_mode); 217f00d4f59SWei Liu } 218f9aef99bSGreg Kurz if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.rdev", &tmp_dev, 219f9aef99bSGreg Kurz sizeof(dev_t)) > 0) { 220f00d4f59SWei Liu stbuf->st_rdev = le64_to_cpu(tmp_dev); 221f00d4f59SWei Liu } 222f00d4f59SWei Liu } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 223f9aef99bSGreg Kurz local_mapped_file_attr(dirfd, name, stbuf); 224f00d4f59SWei Liu } 225f00d4f59SWei Liu 226f00d4f59SWei Liu err_out: 227f9aef99bSGreg Kurz close_preserve_errno(dirfd); 228f9aef99bSGreg Kurz out: 229f9aef99bSGreg Kurz g_free(name); 230f9aef99bSGreg Kurz g_free(dirpath); 231f00d4f59SWei Liu return err; 232f00d4f59SWei Liu } 233f00d4f59SWei Liu 234f00d4f59SWei Liu static int local_create_mapped_attr_dir(FsContext *ctx, const char *path) 235f00d4f59SWei Liu { 236f00d4f59SWei Liu int err; 237f00d4f59SWei Liu char *attr_dir; 238f00d4f59SWei Liu char *tmp_path = g_strdup(path); 239f00d4f59SWei Liu 240f00d4f59SWei Liu attr_dir = g_strdup_printf("%s/%s/%s", 241f00d4f59SWei Liu ctx->fs_root, dirname(tmp_path), VIRTFS_META_DIR); 242f00d4f59SWei Liu 243f00d4f59SWei Liu err = mkdir(attr_dir, 0700); 244f00d4f59SWei Liu if (err < 0 && errno == EEXIST) { 245f00d4f59SWei Liu err = 0; 246f00d4f59SWei Liu } 247f00d4f59SWei Liu g_free(attr_dir); 248f00d4f59SWei Liu g_free(tmp_path); 249f00d4f59SWei Liu return err; 250f00d4f59SWei Liu } 251f00d4f59SWei Liu 252f00d4f59SWei Liu static int local_set_mapped_file_attr(FsContext *ctx, 253f00d4f59SWei Liu const char *path, FsCred *credp) 254f00d4f59SWei Liu { 255f00d4f59SWei Liu FILE *fp; 256f00d4f59SWei Liu int ret = 0; 257f00d4f59SWei Liu char buf[ATTR_MAX]; 258f00d4f59SWei Liu char *attr_path; 259f00d4f59SWei Liu int uid = -1, gid = -1, mode = -1, rdev = -1; 260f00d4f59SWei Liu 261f00d4f59SWei Liu attr_path = local_mapped_attr_path(ctx, path); 262f00d4f59SWei Liu fp = local_fopen(attr_path, "r"); 263f00d4f59SWei Liu if (!fp) { 264f00d4f59SWei Liu goto create_map_file; 265f00d4f59SWei Liu } 266f00d4f59SWei Liu memset(buf, 0, ATTR_MAX); 267f00d4f59SWei Liu while (fgets(buf, ATTR_MAX, fp)) { 268f00d4f59SWei Liu if (!strncmp(buf, "virtfs.uid", 10)) { 269f00d4f59SWei Liu uid = atoi(buf+11); 270f00d4f59SWei Liu } else if (!strncmp(buf, "virtfs.gid", 10)) { 271f00d4f59SWei Liu gid = atoi(buf+11); 272f00d4f59SWei Liu } else if (!strncmp(buf, "virtfs.mode", 11)) { 273f00d4f59SWei Liu mode = atoi(buf+12); 274f00d4f59SWei Liu } else if (!strncmp(buf, "virtfs.rdev", 11)) { 275f00d4f59SWei Liu rdev = atoi(buf+12); 276f00d4f59SWei Liu } 277f00d4f59SWei Liu memset(buf, 0, ATTR_MAX); 278f00d4f59SWei Liu } 279f00d4f59SWei Liu fclose(fp); 280f00d4f59SWei Liu goto update_map_file; 281f00d4f59SWei Liu 282f00d4f59SWei Liu create_map_file: 283f00d4f59SWei Liu ret = local_create_mapped_attr_dir(ctx, path); 284f00d4f59SWei Liu if (ret < 0) { 285f00d4f59SWei Liu goto err_out; 286f00d4f59SWei Liu } 287f00d4f59SWei Liu 288f00d4f59SWei Liu update_map_file: 289f00d4f59SWei Liu fp = local_fopen(attr_path, "w"); 290f00d4f59SWei Liu if (!fp) { 291f00d4f59SWei Liu ret = -1; 292f00d4f59SWei Liu goto err_out; 293f00d4f59SWei Liu } 294f00d4f59SWei Liu 295f00d4f59SWei Liu if (credp->fc_uid != -1) { 296f00d4f59SWei Liu uid = credp->fc_uid; 297f00d4f59SWei Liu } 298f00d4f59SWei Liu if (credp->fc_gid != -1) { 299f00d4f59SWei Liu gid = credp->fc_gid; 300f00d4f59SWei Liu } 301f00d4f59SWei Liu if (credp->fc_mode != -1) { 302f00d4f59SWei Liu mode = credp->fc_mode; 303f00d4f59SWei Liu } 304f00d4f59SWei Liu if (credp->fc_rdev != -1) { 305f00d4f59SWei Liu rdev = credp->fc_rdev; 306f00d4f59SWei Liu } 307f00d4f59SWei Liu 308f00d4f59SWei Liu 309f00d4f59SWei Liu if (uid != -1) { 310f00d4f59SWei Liu fprintf(fp, "virtfs.uid=%d\n", uid); 311f00d4f59SWei Liu } 312f00d4f59SWei Liu if (gid != -1) { 313f00d4f59SWei Liu fprintf(fp, "virtfs.gid=%d\n", gid); 314f00d4f59SWei Liu } 315f00d4f59SWei Liu if (mode != -1) { 316f00d4f59SWei Liu fprintf(fp, "virtfs.mode=%d\n", mode); 317f00d4f59SWei Liu } 318f00d4f59SWei Liu if (rdev != -1) { 319f00d4f59SWei Liu fprintf(fp, "virtfs.rdev=%d\n", rdev); 320f00d4f59SWei Liu } 321f00d4f59SWei Liu fclose(fp); 322f00d4f59SWei Liu 323f00d4f59SWei Liu err_out: 324f00d4f59SWei Liu g_free(attr_path); 325f00d4f59SWei Liu return ret; 326f00d4f59SWei Liu } 327f00d4f59SWei Liu 328f00d4f59SWei Liu static int local_set_xattr(const char *path, FsCred *credp) 329f00d4f59SWei Liu { 330f00d4f59SWei Liu int err; 331f00d4f59SWei Liu 332f00d4f59SWei Liu if (credp->fc_uid != -1) { 333f00d4f59SWei Liu uint32_t tmp_uid = cpu_to_le32(credp->fc_uid); 334f00d4f59SWei Liu err = setxattr(path, "user.virtfs.uid", &tmp_uid, sizeof(uid_t), 0); 335f00d4f59SWei Liu if (err) { 336f00d4f59SWei Liu return err; 337f00d4f59SWei Liu } 338f00d4f59SWei Liu } 339f00d4f59SWei Liu if (credp->fc_gid != -1) { 340f00d4f59SWei Liu uint32_t tmp_gid = cpu_to_le32(credp->fc_gid); 341f00d4f59SWei Liu err = setxattr(path, "user.virtfs.gid", &tmp_gid, sizeof(gid_t), 0); 342f00d4f59SWei Liu if (err) { 343f00d4f59SWei Liu return err; 344f00d4f59SWei Liu } 345f00d4f59SWei Liu } 346f00d4f59SWei Liu if (credp->fc_mode != -1) { 347f00d4f59SWei Liu uint32_t tmp_mode = cpu_to_le32(credp->fc_mode); 348f00d4f59SWei Liu err = setxattr(path, "user.virtfs.mode", &tmp_mode, sizeof(mode_t), 0); 349f00d4f59SWei Liu if (err) { 350f00d4f59SWei Liu return err; 351f00d4f59SWei Liu } 352f00d4f59SWei Liu } 353f00d4f59SWei Liu if (credp->fc_rdev != -1) { 354f00d4f59SWei Liu uint64_t tmp_rdev = cpu_to_le64(credp->fc_rdev); 355f00d4f59SWei Liu err = setxattr(path, "user.virtfs.rdev", &tmp_rdev, sizeof(dev_t), 0); 356f00d4f59SWei Liu if (err) { 357f00d4f59SWei Liu return err; 358f00d4f59SWei Liu } 359f00d4f59SWei Liu } 360f00d4f59SWei Liu return 0; 361f00d4f59SWei Liu } 362f00d4f59SWei Liu 363f00d4f59SWei Liu static int local_post_create_passthrough(FsContext *fs_ctx, const char *path, 364f00d4f59SWei Liu FsCred *credp) 365f00d4f59SWei Liu { 366f00d4f59SWei Liu char *buffer; 367f00d4f59SWei Liu 368f00d4f59SWei Liu buffer = rpath(fs_ctx, path); 369f00d4f59SWei Liu if (lchown(buffer, credp->fc_uid, credp->fc_gid) < 0) { 370f00d4f59SWei Liu /* 371f00d4f59SWei Liu * If we fail to change ownership and if we are 372f00d4f59SWei Liu * using security model none. Ignore the error 373f00d4f59SWei Liu */ 374f00d4f59SWei Liu if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) { 375f00d4f59SWei Liu goto err; 376f00d4f59SWei Liu } 377f00d4f59SWei Liu } 378f00d4f59SWei Liu 379f00d4f59SWei Liu if (chmod(buffer, credp->fc_mode & 07777) < 0) { 380f00d4f59SWei Liu goto err; 381f00d4f59SWei Liu } 382f00d4f59SWei Liu 383f00d4f59SWei Liu g_free(buffer); 384f00d4f59SWei Liu return 0; 385f00d4f59SWei Liu err: 386f00d4f59SWei Liu g_free(buffer); 387f00d4f59SWei Liu return -1; 388f00d4f59SWei Liu } 389f00d4f59SWei Liu 390f00d4f59SWei Liu static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path, 391f00d4f59SWei Liu char *buf, size_t bufsz) 392f00d4f59SWei Liu { 393f00d4f59SWei Liu ssize_t tsize = -1; 394f00d4f59SWei Liu 395f00d4f59SWei Liu if ((fs_ctx->export_flags & V9FS_SM_MAPPED) || 396f00d4f59SWei Liu (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) { 397f00d4f59SWei Liu int fd; 398bec1e954SGreg Kurz 399bec1e954SGreg Kurz fd = local_open_nofollow(fs_ctx, fs_path->data, O_RDONLY, 0); 400f00d4f59SWei Liu if (fd == -1) { 401f00d4f59SWei Liu return -1; 402f00d4f59SWei Liu } 403f00d4f59SWei Liu do { 404f00d4f59SWei Liu tsize = read(fd, (void *)buf, bufsz); 405f00d4f59SWei Liu } while (tsize == -1 && errno == EINTR); 406bec1e954SGreg Kurz close_preserve_errno(fd); 407f00d4f59SWei Liu } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 408f00d4f59SWei Liu (fs_ctx->export_flags & V9FS_SM_NONE)) { 409bec1e954SGreg Kurz char *dirpath = g_path_get_dirname(fs_path->data); 410bec1e954SGreg Kurz char *name = g_path_get_basename(fs_path->data); 411bec1e954SGreg Kurz int dirfd; 412bec1e954SGreg Kurz 413bec1e954SGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dirpath); 414bec1e954SGreg Kurz if (dirfd == -1) { 415bec1e954SGreg Kurz goto out; 416bec1e954SGreg Kurz } 417bec1e954SGreg Kurz 418bec1e954SGreg Kurz tsize = readlinkat(dirfd, name, buf, bufsz); 419bec1e954SGreg Kurz close_preserve_errno(dirfd); 420bec1e954SGreg Kurz out: 421bec1e954SGreg Kurz g_free(name); 422bec1e954SGreg Kurz g_free(dirpath); 423f00d4f59SWei Liu } 424f00d4f59SWei Liu return tsize; 425f00d4f59SWei Liu } 426f00d4f59SWei Liu 427f00d4f59SWei Liu static int local_close(FsContext *ctx, V9fsFidOpenState *fs) 428f00d4f59SWei Liu { 429f00d4f59SWei Liu return close(fs->fd); 430f00d4f59SWei Liu } 431f00d4f59SWei Liu 432f00d4f59SWei Liu static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs) 433f00d4f59SWei Liu { 434f314ea4eSGreg Kurz return closedir(fs->dir.stream); 435f00d4f59SWei Liu } 436f00d4f59SWei Liu 437f00d4f59SWei Liu static int local_open(FsContext *ctx, V9fsPath *fs_path, 438f00d4f59SWei Liu int flags, V9fsFidOpenState *fs) 439f00d4f59SWei Liu { 44021328e1eSGreg Kurz int fd; 441f00d4f59SWei Liu 442996a0d76SGreg Kurz fd = local_open_nofollow(ctx, fs_path->data, flags, 0); 44321328e1eSGreg Kurz if (fd == -1) { 44421328e1eSGreg Kurz return -1; 44521328e1eSGreg Kurz } 44621328e1eSGreg Kurz fs->fd = fd; 447f00d4f59SWei Liu return fs->fd; 448f00d4f59SWei Liu } 449f00d4f59SWei Liu 450f00d4f59SWei Liu static int local_opendir(FsContext *ctx, 451f00d4f59SWei Liu V9fsPath *fs_path, V9fsFidOpenState *fs) 452f00d4f59SWei Liu { 453996a0d76SGreg Kurz int dirfd; 45421328e1eSGreg Kurz DIR *stream; 455f00d4f59SWei Liu 456996a0d76SGreg Kurz dirfd = local_opendir_nofollow(ctx, fs_path->data); 457996a0d76SGreg Kurz if (dirfd == -1) { 458996a0d76SGreg Kurz return -1; 459996a0d76SGreg Kurz } 460996a0d76SGreg Kurz 461996a0d76SGreg Kurz stream = fdopendir(dirfd); 46221328e1eSGreg Kurz if (!stream) { 463f00d4f59SWei Liu return -1; 464f00d4f59SWei Liu } 46521328e1eSGreg Kurz fs->dir.stream = stream; 466f00d4f59SWei Liu return 0; 467f00d4f59SWei Liu } 468f00d4f59SWei Liu 469f00d4f59SWei Liu static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs) 470f00d4f59SWei Liu { 471f314ea4eSGreg Kurz rewinddir(fs->dir.stream); 472f00d4f59SWei Liu } 473f00d4f59SWei Liu 474f00d4f59SWei Liu static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs) 475f00d4f59SWei Liu { 476f314ea4eSGreg Kurz return telldir(fs->dir.stream); 477f00d4f59SWei Liu } 478f00d4f59SWei Liu 479635324e8SGreg Kurz static struct dirent *local_readdir(FsContext *ctx, V9fsFidOpenState *fs) 480f00d4f59SWei Liu { 481635324e8SGreg Kurz struct dirent *entry; 482f00d4f59SWei Liu 483f00d4f59SWei Liu again: 484635324e8SGreg Kurz entry = readdir(fs->dir.stream); 485635324e8SGreg Kurz if (!entry) { 486635324e8SGreg Kurz return NULL; 487635324e8SGreg Kurz } 488635324e8SGreg Kurz 489f00d4f59SWei Liu if (ctx->export_flags & V9FS_SM_MAPPED) { 490f00d4f59SWei Liu entry->d_type = DT_UNKNOWN; 491f00d4f59SWei Liu } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 492635324e8SGreg Kurz if (!strcmp(entry->d_name, VIRTFS_META_DIR)) { 493f00d4f59SWei Liu /* skp the meta data directory */ 494f00d4f59SWei Liu goto again; 495f00d4f59SWei Liu } 496f00d4f59SWei Liu entry->d_type = DT_UNKNOWN; 497f00d4f59SWei Liu } 498635324e8SGreg Kurz 499635324e8SGreg Kurz return entry; 500f00d4f59SWei Liu } 501f00d4f59SWei Liu 502f00d4f59SWei Liu static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off) 503f00d4f59SWei Liu { 504f314ea4eSGreg Kurz seekdir(fs->dir.stream, off); 505f00d4f59SWei Liu } 506f00d4f59SWei Liu 507f00d4f59SWei Liu static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs, 508f00d4f59SWei Liu const struct iovec *iov, 509f00d4f59SWei Liu int iovcnt, off_t offset) 510f00d4f59SWei Liu { 511f00d4f59SWei Liu #ifdef CONFIG_PREADV 512f00d4f59SWei Liu return preadv(fs->fd, iov, iovcnt, offset); 513f00d4f59SWei Liu #else 514f00d4f59SWei Liu int err = lseek(fs->fd, offset, SEEK_SET); 515f00d4f59SWei Liu if (err == -1) { 516f00d4f59SWei Liu return err; 517f00d4f59SWei Liu } else { 518f00d4f59SWei Liu return readv(fs->fd, iov, iovcnt); 519f00d4f59SWei Liu } 520f00d4f59SWei Liu #endif 521f00d4f59SWei Liu } 522f00d4f59SWei Liu 523f00d4f59SWei Liu static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs, 524f00d4f59SWei Liu const struct iovec *iov, 525f00d4f59SWei Liu int iovcnt, off_t offset) 526f00d4f59SWei Liu { 5276fe76accSGreg Kurz ssize_t ret; 528f00d4f59SWei Liu #ifdef CONFIG_PREADV 529f00d4f59SWei Liu ret = pwritev(fs->fd, iov, iovcnt, offset); 530f00d4f59SWei Liu #else 531f00d4f59SWei Liu int err = lseek(fs->fd, offset, SEEK_SET); 532f00d4f59SWei Liu if (err == -1) { 533f00d4f59SWei Liu return err; 534f00d4f59SWei Liu } else { 535f00d4f59SWei Liu ret = writev(fs->fd, iov, iovcnt); 536f00d4f59SWei Liu } 537f00d4f59SWei Liu #endif 538f00d4f59SWei Liu #ifdef CONFIG_SYNC_FILE_RANGE 539f00d4f59SWei Liu if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) { 540f00d4f59SWei Liu /* 541f00d4f59SWei Liu * Initiate a writeback. This is not a data integrity sync. 542f00d4f59SWei Liu * We want to ensure that we don't leave dirty pages in the cache 543f00d4f59SWei Liu * after write when writeout=immediate is sepcified. 544f00d4f59SWei Liu */ 545f00d4f59SWei Liu sync_file_range(fs->fd, offset, ret, 546f00d4f59SWei Liu SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE); 547f00d4f59SWei Liu } 548f00d4f59SWei Liu #endif 549f00d4f59SWei Liu return ret; 550f00d4f59SWei Liu } 551f00d4f59SWei Liu 552f00d4f59SWei Liu static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp) 553f00d4f59SWei Liu { 554f00d4f59SWei Liu char *buffer; 555f00d4f59SWei Liu int ret = -1; 556f00d4f59SWei Liu char *path = fs_path->data; 557f00d4f59SWei Liu 558f00d4f59SWei Liu if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 559f00d4f59SWei Liu buffer = rpath(fs_ctx, path); 560f00d4f59SWei Liu ret = local_set_xattr(buffer, credp); 561f00d4f59SWei Liu g_free(buffer); 562f00d4f59SWei Liu } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 563f00d4f59SWei Liu return local_set_mapped_file_attr(fs_ctx, path, credp); 564f00d4f59SWei Liu } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 565f00d4f59SWei Liu (fs_ctx->export_flags & V9FS_SM_NONE)) { 566f00d4f59SWei Liu buffer = rpath(fs_ctx, path); 567f00d4f59SWei Liu ret = chmod(buffer, credp->fc_mode); 568f00d4f59SWei Liu g_free(buffer); 569f00d4f59SWei Liu } 570f00d4f59SWei Liu return ret; 571f00d4f59SWei Liu } 572f00d4f59SWei Liu 573f00d4f59SWei Liu static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path, 574f00d4f59SWei Liu const char *name, FsCred *credp) 575f00d4f59SWei Liu { 576f00d4f59SWei Liu char *path; 577f00d4f59SWei Liu int err = -1; 578f00d4f59SWei Liu int serrno = 0; 579f00d4f59SWei Liu V9fsString fullname; 580f00d4f59SWei Liu char *buffer = NULL; 581f00d4f59SWei Liu 582f00d4f59SWei Liu v9fs_string_init(&fullname); 583f00d4f59SWei Liu v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name); 584f00d4f59SWei Liu path = fullname.data; 585f00d4f59SWei Liu 586f00d4f59SWei Liu /* Determine the security model */ 587f00d4f59SWei Liu if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 588f00d4f59SWei Liu buffer = rpath(fs_ctx, path); 589f00d4f59SWei Liu err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0); 590f00d4f59SWei Liu if (err == -1) { 591f00d4f59SWei Liu goto out; 592f00d4f59SWei Liu } 593f00d4f59SWei Liu err = local_set_xattr(buffer, credp); 594f00d4f59SWei Liu if (err == -1) { 595f00d4f59SWei Liu serrno = errno; 596f00d4f59SWei Liu goto err_end; 597f00d4f59SWei Liu } 598f00d4f59SWei Liu } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 599f00d4f59SWei Liu 600f00d4f59SWei Liu buffer = rpath(fs_ctx, path); 601f00d4f59SWei Liu err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0); 602f00d4f59SWei Liu if (err == -1) { 603f00d4f59SWei Liu goto out; 604f00d4f59SWei Liu } 605f00d4f59SWei Liu err = local_set_mapped_file_attr(fs_ctx, path, credp); 606f00d4f59SWei Liu if (err == -1) { 607f00d4f59SWei Liu serrno = errno; 608f00d4f59SWei Liu goto err_end; 609f00d4f59SWei Liu } 610f00d4f59SWei Liu } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 611f00d4f59SWei Liu (fs_ctx->export_flags & V9FS_SM_NONE)) { 612f00d4f59SWei Liu buffer = rpath(fs_ctx, path); 613f00d4f59SWei Liu err = mknod(buffer, credp->fc_mode, credp->fc_rdev); 614f00d4f59SWei Liu if (err == -1) { 615f00d4f59SWei Liu goto out; 616f00d4f59SWei Liu } 617f00d4f59SWei Liu err = local_post_create_passthrough(fs_ctx, path, credp); 618f00d4f59SWei Liu if (err == -1) { 619f00d4f59SWei Liu serrno = errno; 620f00d4f59SWei Liu goto err_end; 621f00d4f59SWei Liu } 622f00d4f59SWei Liu } 623f00d4f59SWei Liu goto out; 624f00d4f59SWei Liu 625f00d4f59SWei Liu err_end: 626f00d4f59SWei Liu remove(buffer); 627f00d4f59SWei Liu errno = serrno; 628f00d4f59SWei Liu out: 629f00d4f59SWei Liu g_free(buffer); 630f00d4f59SWei Liu v9fs_string_free(&fullname); 631f00d4f59SWei Liu return err; 632f00d4f59SWei Liu } 633f00d4f59SWei Liu 634f00d4f59SWei Liu static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path, 635f00d4f59SWei Liu const char *name, FsCred *credp) 636f00d4f59SWei Liu { 637f00d4f59SWei Liu char *path; 638f00d4f59SWei Liu int err = -1; 639f00d4f59SWei Liu int serrno = 0; 640f00d4f59SWei Liu V9fsString fullname; 641f00d4f59SWei Liu char *buffer = NULL; 642f00d4f59SWei Liu 643f00d4f59SWei Liu v9fs_string_init(&fullname); 644f00d4f59SWei Liu v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name); 645f00d4f59SWei Liu path = fullname.data; 646f00d4f59SWei Liu 647f00d4f59SWei Liu /* Determine the security model */ 648f00d4f59SWei Liu if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 649f00d4f59SWei Liu buffer = rpath(fs_ctx, path); 650f00d4f59SWei Liu err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS); 651f00d4f59SWei Liu if (err == -1) { 652f00d4f59SWei Liu goto out; 653f00d4f59SWei Liu } 654f00d4f59SWei Liu credp->fc_mode = credp->fc_mode|S_IFDIR; 655f00d4f59SWei Liu err = local_set_xattr(buffer, credp); 656f00d4f59SWei Liu if (err == -1) { 657f00d4f59SWei Liu serrno = errno; 658f00d4f59SWei Liu goto err_end; 659f00d4f59SWei Liu } 660f00d4f59SWei Liu } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 661f00d4f59SWei Liu buffer = rpath(fs_ctx, path); 662f00d4f59SWei Liu err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS); 663f00d4f59SWei Liu if (err == -1) { 664f00d4f59SWei Liu goto out; 665f00d4f59SWei Liu } 666f00d4f59SWei Liu credp->fc_mode = credp->fc_mode|S_IFDIR; 667f00d4f59SWei Liu err = local_set_mapped_file_attr(fs_ctx, path, credp); 668f00d4f59SWei Liu if (err == -1) { 669f00d4f59SWei Liu serrno = errno; 670f00d4f59SWei Liu goto err_end; 671f00d4f59SWei Liu } 672f00d4f59SWei Liu } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 673f00d4f59SWei Liu (fs_ctx->export_flags & V9FS_SM_NONE)) { 674f00d4f59SWei Liu buffer = rpath(fs_ctx, path); 675f00d4f59SWei Liu err = mkdir(buffer, credp->fc_mode); 676f00d4f59SWei Liu if (err == -1) { 677f00d4f59SWei Liu goto out; 678f00d4f59SWei Liu } 679f00d4f59SWei Liu err = local_post_create_passthrough(fs_ctx, path, credp); 680f00d4f59SWei Liu if (err == -1) { 681f00d4f59SWei Liu serrno = errno; 682f00d4f59SWei Liu goto err_end; 683f00d4f59SWei Liu } 684f00d4f59SWei Liu } 685f00d4f59SWei Liu goto out; 686f00d4f59SWei Liu 687f00d4f59SWei Liu err_end: 688f00d4f59SWei Liu remove(buffer); 689f00d4f59SWei Liu errno = serrno; 690f00d4f59SWei Liu out: 691f00d4f59SWei Liu g_free(buffer); 692f00d4f59SWei Liu v9fs_string_free(&fullname); 693f00d4f59SWei Liu return err; 694f00d4f59SWei Liu } 695f00d4f59SWei Liu 696f00d4f59SWei Liu static int local_fstat(FsContext *fs_ctx, int fid_type, 697f00d4f59SWei Liu V9fsFidOpenState *fs, struct stat *stbuf) 698f00d4f59SWei Liu { 699f00d4f59SWei Liu int err, fd; 700f00d4f59SWei Liu 701f00d4f59SWei Liu if (fid_type == P9_FID_DIR) { 702f314ea4eSGreg Kurz fd = dirfd(fs->dir.stream); 703f00d4f59SWei Liu } else { 704f00d4f59SWei Liu fd = fs->fd; 705f00d4f59SWei Liu } 706f00d4f59SWei Liu 707f00d4f59SWei Liu err = fstat(fd, stbuf); 708f00d4f59SWei Liu if (err) { 709f00d4f59SWei Liu return err; 710f00d4f59SWei Liu } 711f00d4f59SWei Liu if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 712f00d4f59SWei Liu /* Actual credentials are part of extended attrs */ 713f00d4f59SWei Liu uid_t tmp_uid; 714f00d4f59SWei Liu gid_t tmp_gid; 715f00d4f59SWei Liu mode_t tmp_mode; 716f00d4f59SWei Liu dev_t tmp_dev; 717f00d4f59SWei Liu 718f00d4f59SWei Liu if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) { 719f00d4f59SWei Liu stbuf->st_uid = le32_to_cpu(tmp_uid); 720f00d4f59SWei Liu } 721f00d4f59SWei Liu if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) { 722f00d4f59SWei Liu stbuf->st_gid = le32_to_cpu(tmp_gid); 723f00d4f59SWei Liu } 724f00d4f59SWei Liu if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) { 725f00d4f59SWei Liu stbuf->st_mode = le32_to_cpu(tmp_mode); 726f00d4f59SWei Liu } 727f00d4f59SWei Liu if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) { 728f00d4f59SWei Liu stbuf->st_rdev = le64_to_cpu(tmp_dev); 729f00d4f59SWei Liu } 730f00d4f59SWei Liu } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 731f00d4f59SWei Liu errno = EOPNOTSUPP; 732f00d4f59SWei Liu return -1; 733f00d4f59SWei Liu } 734f00d4f59SWei Liu return err; 735f00d4f59SWei Liu } 736f00d4f59SWei Liu 737f00d4f59SWei Liu static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name, 738f00d4f59SWei Liu int flags, FsCred *credp, V9fsFidOpenState *fs) 739f00d4f59SWei Liu { 740f00d4f59SWei Liu char *path; 741f00d4f59SWei Liu int fd = -1; 742f00d4f59SWei Liu int err = -1; 743f00d4f59SWei Liu int serrno = 0; 744f00d4f59SWei Liu V9fsString fullname; 745f00d4f59SWei Liu char *buffer = NULL; 746f00d4f59SWei Liu 747f00d4f59SWei Liu /* 748f00d4f59SWei Liu * Mark all the open to not follow symlinks 749f00d4f59SWei Liu */ 750f00d4f59SWei Liu flags |= O_NOFOLLOW; 751f00d4f59SWei Liu 752f00d4f59SWei Liu v9fs_string_init(&fullname); 753f00d4f59SWei Liu v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name); 754f00d4f59SWei Liu path = fullname.data; 755f00d4f59SWei Liu 756f00d4f59SWei Liu /* Determine the security model */ 757f00d4f59SWei Liu if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 758f00d4f59SWei Liu buffer = rpath(fs_ctx, path); 759f00d4f59SWei Liu fd = open(buffer, flags, SM_LOCAL_MODE_BITS); 760f00d4f59SWei Liu if (fd == -1) { 761f00d4f59SWei Liu err = fd; 762f00d4f59SWei Liu goto out; 763f00d4f59SWei Liu } 764f00d4f59SWei Liu credp->fc_mode = credp->fc_mode|S_IFREG; 765f00d4f59SWei Liu /* Set cleint credentials in xattr */ 766f00d4f59SWei Liu err = local_set_xattr(buffer, credp); 767f00d4f59SWei Liu if (err == -1) { 768f00d4f59SWei Liu serrno = errno; 769f00d4f59SWei Liu goto err_end; 770f00d4f59SWei Liu } 771f00d4f59SWei Liu } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 772f00d4f59SWei Liu buffer = rpath(fs_ctx, path); 773f00d4f59SWei Liu fd = open(buffer, flags, SM_LOCAL_MODE_BITS); 774f00d4f59SWei Liu if (fd == -1) { 775f00d4f59SWei Liu err = fd; 776f00d4f59SWei Liu goto out; 777f00d4f59SWei Liu } 778f00d4f59SWei Liu credp->fc_mode = credp->fc_mode|S_IFREG; 779f00d4f59SWei Liu /* Set client credentials in .virtfs_metadata directory files */ 780f00d4f59SWei Liu err = local_set_mapped_file_attr(fs_ctx, path, credp); 781f00d4f59SWei Liu if (err == -1) { 782f00d4f59SWei Liu serrno = errno; 783f00d4f59SWei Liu goto err_end; 784f00d4f59SWei Liu } 785f00d4f59SWei Liu } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 786f00d4f59SWei Liu (fs_ctx->export_flags & V9FS_SM_NONE)) { 787f00d4f59SWei Liu buffer = rpath(fs_ctx, path); 788f00d4f59SWei Liu fd = open(buffer, flags, credp->fc_mode); 789f00d4f59SWei Liu if (fd == -1) { 790f00d4f59SWei Liu err = fd; 791f00d4f59SWei Liu goto out; 792f00d4f59SWei Liu } 793f00d4f59SWei Liu err = local_post_create_passthrough(fs_ctx, path, credp); 794f00d4f59SWei Liu if (err == -1) { 795f00d4f59SWei Liu serrno = errno; 796f00d4f59SWei Liu goto err_end; 797f00d4f59SWei Liu } 798f00d4f59SWei Liu } 799f00d4f59SWei Liu err = fd; 800f00d4f59SWei Liu fs->fd = fd; 801f00d4f59SWei Liu goto out; 802f00d4f59SWei Liu 803f00d4f59SWei Liu err_end: 804f00d4f59SWei Liu close(fd); 805f00d4f59SWei Liu remove(buffer); 806f00d4f59SWei Liu errno = serrno; 807f00d4f59SWei Liu out: 808f00d4f59SWei Liu g_free(buffer); 809f00d4f59SWei Liu v9fs_string_free(&fullname); 810f00d4f59SWei Liu return err; 811f00d4f59SWei Liu } 812f00d4f59SWei Liu 813f00d4f59SWei Liu 814f00d4f59SWei Liu static int local_symlink(FsContext *fs_ctx, const char *oldpath, 815f00d4f59SWei Liu V9fsPath *dir_path, const char *name, FsCred *credp) 816f00d4f59SWei Liu { 817f00d4f59SWei Liu int err = -1; 818f00d4f59SWei Liu int serrno = 0; 819f00d4f59SWei Liu char *newpath; 820f00d4f59SWei Liu V9fsString fullname; 821f00d4f59SWei Liu char *buffer = NULL; 822f00d4f59SWei Liu 823f00d4f59SWei Liu v9fs_string_init(&fullname); 824f00d4f59SWei Liu v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name); 825f00d4f59SWei Liu newpath = fullname.data; 826f00d4f59SWei Liu 827f00d4f59SWei Liu /* Determine the security model */ 828f00d4f59SWei Liu if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 829f00d4f59SWei Liu int fd; 830f00d4f59SWei Liu ssize_t oldpath_size, write_size; 831f00d4f59SWei Liu buffer = rpath(fs_ctx, newpath); 832f00d4f59SWei Liu fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS); 833f00d4f59SWei Liu if (fd == -1) { 834f00d4f59SWei Liu err = fd; 835f00d4f59SWei Liu goto out; 836f00d4f59SWei Liu } 837f00d4f59SWei Liu /* Write the oldpath (target) to the file. */ 838f00d4f59SWei Liu oldpath_size = strlen(oldpath); 839f00d4f59SWei Liu do { 840f00d4f59SWei Liu write_size = write(fd, (void *)oldpath, oldpath_size); 841f00d4f59SWei Liu } while (write_size == -1 && errno == EINTR); 842f00d4f59SWei Liu 843f00d4f59SWei Liu if (write_size != oldpath_size) { 844f00d4f59SWei Liu serrno = errno; 845f00d4f59SWei Liu close(fd); 846f00d4f59SWei Liu err = -1; 847f00d4f59SWei Liu goto err_end; 848f00d4f59SWei Liu } 849f00d4f59SWei Liu close(fd); 850f00d4f59SWei Liu /* Set cleint credentials in symlink's xattr */ 851f00d4f59SWei Liu credp->fc_mode = credp->fc_mode|S_IFLNK; 852f00d4f59SWei Liu err = local_set_xattr(buffer, credp); 853f00d4f59SWei Liu if (err == -1) { 854f00d4f59SWei Liu serrno = errno; 855f00d4f59SWei Liu goto err_end; 856f00d4f59SWei Liu } 857f00d4f59SWei Liu } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 858f00d4f59SWei Liu int fd; 859f00d4f59SWei Liu ssize_t oldpath_size, write_size; 860f00d4f59SWei Liu buffer = rpath(fs_ctx, newpath); 861f00d4f59SWei Liu fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS); 862f00d4f59SWei Liu if (fd == -1) { 863f00d4f59SWei Liu err = fd; 864f00d4f59SWei Liu goto out; 865f00d4f59SWei Liu } 866f00d4f59SWei Liu /* Write the oldpath (target) to the file. */ 867f00d4f59SWei Liu oldpath_size = strlen(oldpath); 868f00d4f59SWei Liu do { 869f00d4f59SWei Liu write_size = write(fd, (void *)oldpath, oldpath_size); 870f00d4f59SWei Liu } while (write_size == -1 && errno == EINTR); 871f00d4f59SWei Liu 872f00d4f59SWei Liu if (write_size != oldpath_size) { 873f00d4f59SWei Liu serrno = errno; 874f00d4f59SWei Liu close(fd); 875f00d4f59SWei Liu err = -1; 876f00d4f59SWei Liu goto err_end; 877f00d4f59SWei Liu } 878f00d4f59SWei Liu close(fd); 879f00d4f59SWei Liu /* Set cleint credentials in symlink's xattr */ 880f00d4f59SWei Liu credp->fc_mode = credp->fc_mode|S_IFLNK; 881f00d4f59SWei Liu err = local_set_mapped_file_attr(fs_ctx, newpath, credp); 882f00d4f59SWei Liu if (err == -1) { 883f00d4f59SWei Liu serrno = errno; 884f00d4f59SWei Liu goto err_end; 885f00d4f59SWei Liu } 886f00d4f59SWei Liu } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 887f00d4f59SWei Liu (fs_ctx->export_flags & V9FS_SM_NONE)) { 888f00d4f59SWei Liu buffer = rpath(fs_ctx, newpath); 889f00d4f59SWei Liu err = symlink(oldpath, buffer); 890f00d4f59SWei Liu if (err) { 891f00d4f59SWei Liu goto out; 892f00d4f59SWei Liu } 893f00d4f59SWei Liu err = lchown(buffer, credp->fc_uid, credp->fc_gid); 894f00d4f59SWei Liu if (err == -1) { 895f00d4f59SWei Liu /* 896f00d4f59SWei Liu * If we fail to change ownership and if we are 897f00d4f59SWei Liu * using security model none. Ignore the error 898f00d4f59SWei Liu */ 899f00d4f59SWei Liu if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) { 900f00d4f59SWei Liu serrno = errno; 901f00d4f59SWei Liu goto err_end; 902f00d4f59SWei Liu } else 903f00d4f59SWei Liu err = 0; 904f00d4f59SWei Liu } 905f00d4f59SWei Liu } 906f00d4f59SWei Liu goto out; 907f00d4f59SWei Liu 908f00d4f59SWei Liu err_end: 909f00d4f59SWei Liu remove(buffer); 910f00d4f59SWei Liu errno = serrno; 911f00d4f59SWei Liu out: 912f00d4f59SWei Liu g_free(buffer); 913f00d4f59SWei Liu v9fs_string_free(&fullname); 914f00d4f59SWei Liu return err; 915f00d4f59SWei Liu } 916f00d4f59SWei Liu 917f00d4f59SWei Liu static int local_link(FsContext *ctx, V9fsPath *oldpath, 918f00d4f59SWei Liu V9fsPath *dirpath, const char *name) 919f00d4f59SWei Liu { 920f00d4f59SWei Liu int ret; 921f00d4f59SWei Liu V9fsString newpath; 922f00d4f59SWei Liu char *buffer, *buffer1; 923f00d4f59SWei Liu 924f00d4f59SWei Liu v9fs_string_init(&newpath); 925f00d4f59SWei Liu v9fs_string_sprintf(&newpath, "%s/%s", dirpath->data, name); 926f00d4f59SWei Liu 927f00d4f59SWei Liu buffer = rpath(ctx, oldpath->data); 928f00d4f59SWei Liu buffer1 = rpath(ctx, newpath.data); 929f00d4f59SWei Liu ret = link(buffer, buffer1); 930f00d4f59SWei Liu g_free(buffer); 931f00d4f59SWei Liu g_free(buffer1); 932f00d4f59SWei Liu 933f00d4f59SWei Liu /* now link the virtfs_metadata files */ 934f00d4f59SWei Liu if (!ret && (ctx->export_flags & V9FS_SM_MAPPED_FILE)) { 935f00d4f59SWei Liu /* Link the .virtfs_metadata files. Create the metada directory */ 936f00d4f59SWei Liu ret = local_create_mapped_attr_dir(ctx, newpath.data); 937f00d4f59SWei Liu if (ret < 0) { 938f00d4f59SWei Liu goto err_out; 939f00d4f59SWei Liu } 940f00d4f59SWei Liu buffer = local_mapped_attr_path(ctx, oldpath->data); 941f00d4f59SWei Liu buffer1 = local_mapped_attr_path(ctx, newpath.data); 942f00d4f59SWei Liu ret = link(buffer, buffer1); 943f00d4f59SWei Liu g_free(buffer); 944f00d4f59SWei Liu g_free(buffer1); 945f00d4f59SWei Liu if (ret < 0 && errno != ENOENT) { 946f00d4f59SWei Liu goto err_out; 947f00d4f59SWei Liu } 948f00d4f59SWei Liu } 949f00d4f59SWei Liu err_out: 950f00d4f59SWei Liu v9fs_string_free(&newpath); 951f00d4f59SWei Liu return ret; 952f00d4f59SWei Liu } 953f00d4f59SWei Liu 954f00d4f59SWei Liu static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size) 955f00d4f59SWei Liu { 956ac125d99SGreg Kurz int fd, ret; 957f00d4f59SWei Liu 958ac125d99SGreg Kurz fd = local_open_nofollow(ctx, fs_path->data, O_WRONLY, 0); 959ac125d99SGreg Kurz if (fd == -1) { 960ac125d99SGreg Kurz return -1; 961ac125d99SGreg Kurz } 962ac125d99SGreg Kurz ret = ftruncate(fd, size); 963ac125d99SGreg Kurz close_preserve_errno(fd); 964f00d4f59SWei Liu return ret; 965f00d4f59SWei Liu } 966f00d4f59SWei Liu 967f00d4f59SWei Liu static int local_rename(FsContext *ctx, const char *oldpath, 968f00d4f59SWei Liu const char *newpath) 969f00d4f59SWei Liu { 970f00d4f59SWei Liu int err; 971f00d4f59SWei Liu char *buffer, *buffer1; 972f00d4f59SWei Liu 973f00d4f59SWei Liu if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 974f00d4f59SWei Liu err = local_create_mapped_attr_dir(ctx, newpath); 975f00d4f59SWei Liu if (err < 0) { 976f00d4f59SWei Liu return err; 977f00d4f59SWei Liu } 978f00d4f59SWei Liu /* rename the .virtfs_metadata files */ 979f00d4f59SWei Liu buffer = local_mapped_attr_path(ctx, oldpath); 980f00d4f59SWei Liu buffer1 = local_mapped_attr_path(ctx, newpath); 981f00d4f59SWei Liu err = rename(buffer, buffer1); 982f00d4f59SWei Liu g_free(buffer); 983f00d4f59SWei Liu g_free(buffer1); 984f00d4f59SWei Liu if (err < 0 && errno != ENOENT) { 985f00d4f59SWei Liu return err; 986f00d4f59SWei Liu } 987f00d4f59SWei Liu } 988f00d4f59SWei Liu 989f00d4f59SWei Liu buffer = rpath(ctx, oldpath); 990f00d4f59SWei Liu buffer1 = rpath(ctx, newpath); 991f00d4f59SWei Liu err = rename(buffer, buffer1); 992f00d4f59SWei Liu g_free(buffer); 993f00d4f59SWei Liu g_free(buffer1); 994f00d4f59SWei Liu return err; 995f00d4f59SWei Liu } 996f00d4f59SWei Liu 997f00d4f59SWei Liu static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp) 998f00d4f59SWei Liu { 999f00d4f59SWei Liu char *buffer; 1000f00d4f59SWei Liu int ret = -1; 1001f00d4f59SWei Liu char *path = fs_path->data; 1002f00d4f59SWei Liu 1003f00d4f59SWei Liu if ((credp->fc_uid == -1 && credp->fc_gid == -1) || 1004f00d4f59SWei Liu (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 1005f00d4f59SWei Liu (fs_ctx->export_flags & V9FS_SM_NONE)) { 1006f00d4f59SWei Liu buffer = rpath(fs_ctx, path); 1007f00d4f59SWei Liu ret = lchown(buffer, credp->fc_uid, credp->fc_gid); 1008f00d4f59SWei Liu g_free(buffer); 1009f00d4f59SWei Liu } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 1010f00d4f59SWei Liu buffer = rpath(fs_ctx, path); 1011f00d4f59SWei Liu ret = local_set_xattr(buffer, credp); 1012f00d4f59SWei Liu g_free(buffer); 1013f00d4f59SWei Liu } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 1014f00d4f59SWei Liu return local_set_mapped_file_attr(fs_ctx, path, credp); 1015f00d4f59SWei Liu } 1016f00d4f59SWei Liu return ret; 1017f00d4f59SWei Liu } 1018f00d4f59SWei Liu 1019f00d4f59SWei Liu static int local_utimensat(FsContext *s, V9fsPath *fs_path, 1020f00d4f59SWei Liu const struct timespec *buf) 1021f00d4f59SWei Liu { 1022a33eda0dSGreg Kurz char *dirpath = g_path_get_dirname(fs_path->data); 1023a33eda0dSGreg Kurz char *name = g_path_get_basename(fs_path->data); 1024a33eda0dSGreg Kurz int dirfd, ret = -1; 1025f00d4f59SWei Liu 1026a33eda0dSGreg Kurz dirfd = local_opendir_nofollow(s, dirpath); 1027a33eda0dSGreg Kurz if (dirfd == -1) { 1028a33eda0dSGreg Kurz goto out; 1029a33eda0dSGreg Kurz } 1030a33eda0dSGreg Kurz 1031a33eda0dSGreg Kurz ret = utimensat(dirfd, name, buf, AT_SYMLINK_NOFOLLOW); 1032a33eda0dSGreg Kurz close_preserve_errno(dirfd); 1033a33eda0dSGreg Kurz out: 1034a33eda0dSGreg Kurz g_free(dirpath); 1035a33eda0dSGreg Kurz g_free(name); 1036f00d4f59SWei Liu return ret; 1037f00d4f59SWei Liu } 1038f00d4f59SWei Liu 1039df4938a6SGreg Kurz static int local_unlinkat_common(FsContext *ctx, int dirfd, const char *name, 1040df4938a6SGreg Kurz int flags) 1041df4938a6SGreg Kurz { 1042df4938a6SGreg Kurz int ret = -1; 1043df4938a6SGreg Kurz 1044df4938a6SGreg Kurz if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 1045df4938a6SGreg Kurz int map_dirfd; 1046df4938a6SGreg Kurz 1047df4938a6SGreg Kurz if (flags == AT_REMOVEDIR) { 1048df4938a6SGreg Kurz int fd; 1049df4938a6SGreg Kurz 1050df4938a6SGreg Kurz fd = openat(dirfd, name, O_RDONLY | O_DIRECTORY | O_PATH); 1051df4938a6SGreg Kurz if (fd == -1) { 1052df4938a6SGreg Kurz goto err_out; 1053df4938a6SGreg Kurz } 1054df4938a6SGreg Kurz /* 1055df4938a6SGreg Kurz * If directory remove .virtfs_metadata contained in the 1056df4938a6SGreg Kurz * directory 1057df4938a6SGreg Kurz */ 1058df4938a6SGreg Kurz ret = unlinkat(fd, VIRTFS_META_DIR, AT_REMOVEDIR); 1059df4938a6SGreg Kurz close_preserve_errno(fd); 1060df4938a6SGreg Kurz if (ret < 0 && errno != ENOENT) { 1061df4938a6SGreg Kurz /* 1062df4938a6SGreg Kurz * We didn't had the .virtfs_metadata file. May be file created 1063df4938a6SGreg Kurz * in non-mapped mode ?. Ignore ENOENT. 1064df4938a6SGreg Kurz */ 1065df4938a6SGreg Kurz goto err_out; 1066df4938a6SGreg Kurz } 1067df4938a6SGreg Kurz } 1068df4938a6SGreg Kurz /* 1069df4938a6SGreg Kurz * Now remove the name from parent directory 1070df4938a6SGreg Kurz * .virtfs_metadata directory. 1071df4938a6SGreg Kurz */ 1072df4938a6SGreg Kurz map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR); 1073df4938a6SGreg Kurz ret = unlinkat(map_dirfd, name, 0); 1074df4938a6SGreg Kurz close_preserve_errno(map_dirfd); 1075df4938a6SGreg Kurz if (ret < 0 && errno != ENOENT) { 1076df4938a6SGreg Kurz /* 1077df4938a6SGreg Kurz * We didn't had the .virtfs_metadata file. May be file created 1078df4938a6SGreg Kurz * in non-mapped mode ?. Ignore ENOENT. 1079df4938a6SGreg Kurz */ 1080df4938a6SGreg Kurz goto err_out; 1081df4938a6SGreg Kurz } 1082df4938a6SGreg Kurz } 1083df4938a6SGreg Kurz 1084df4938a6SGreg Kurz ret = unlinkat(dirfd, name, flags); 1085df4938a6SGreg Kurz err_out: 1086df4938a6SGreg Kurz return ret; 1087df4938a6SGreg Kurz } 1088df4938a6SGreg Kurz 1089f00d4f59SWei Liu static int local_remove(FsContext *ctx, const char *path) 1090f00d4f59SWei Liu { 1091f00d4f59SWei Liu struct stat stbuf; 1092a0e640a8SGreg Kurz char *dirpath = g_path_get_dirname(path); 1093a0e640a8SGreg Kurz char *name = g_path_get_basename(path); 1094a0e640a8SGreg Kurz int flags = 0; 1095a0e640a8SGreg Kurz int dirfd; 1096a0e640a8SGreg Kurz int err = -1; 1097f00d4f59SWei Liu 1098a0e640a8SGreg Kurz dirfd = local_opendir_nofollow(ctx, dirpath); 1099a0e640a8SGreg Kurz if (dirfd) { 1100a0e640a8SGreg Kurz goto out; 1101a0e640a8SGreg Kurz } 1102a0e640a8SGreg Kurz 1103a0e640a8SGreg Kurz if (fstatat(dirfd, path, &stbuf, AT_SYMLINK_NOFOLLOW) < 0) { 1104f00d4f59SWei Liu goto err_out; 1105f00d4f59SWei Liu } 1106a0e640a8SGreg Kurz 1107f00d4f59SWei Liu if (S_ISDIR(stbuf.st_mode)) { 1108a0e640a8SGreg Kurz flags |= AT_REMOVEDIR; 1109f00d4f59SWei Liu } 1110f00d4f59SWei Liu 1111a0e640a8SGreg Kurz err = local_unlinkat_common(ctx, dirfd, name, flags); 1112f00d4f59SWei Liu err_out: 1113a0e640a8SGreg Kurz close_preserve_errno(dirfd); 1114a0e640a8SGreg Kurz out: 1115a0e640a8SGreg Kurz g_free(name); 1116a0e640a8SGreg Kurz g_free(dirpath); 1117f00d4f59SWei Liu return err; 1118f00d4f59SWei Liu } 1119f00d4f59SWei Liu 1120f00d4f59SWei Liu static int local_fsync(FsContext *ctx, int fid_type, 1121f00d4f59SWei Liu V9fsFidOpenState *fs, int datasync) 1122f00d4f59SWei Liu { 1123f00d4f59SWei Liu int fd; 1124f00d4f59SWei Liu 1125f00d4f59SWei Liu if (fid_type == P9_FID_DIR) { 1126f314ea4eSGreg Kurz fd = dirfd(fs->dir.stream); 1127f00d4f59SWei Liu } else { 1128f00d4f59SWei Liu fd = fs->fd; 1129f00d4f59SWei Liu } 1130f00d4f59SWei Liu 1131f00d4f59SWei Liu if (datasync) { 1132f00d4f59SWei Liu return qemu_fdatasync(fd); 1133f00d4f59SWei Liu } else { 1134f00d4f59SWei Liu return fsync(fd); 1135f00d4f59SWei Liu } 1136f00d4f59SWei Liu } 1137f00d4f59SWei Liu 1138f00d4f59SWei Liu static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf) 1139f00d4f59SWei Liu { 114031e51d1cSGreg Kurz int fd, ret; 1141f00d4f59SWei Liu 114231e51d1cSGreg Kurz fd = local_open_nofollow(s, fs_path->data, O_RDONLY, 0); 114331e51d1cSGreg Kurz ret = fstatfs(fd, stbuf); 114431e51d1cSGreg Kurz close_preserve_errno(fd); 1145f00d4f59SWei Liu return ret; 1146f00d4f59SWei Liu } 1147f00d4f59SWei Liu 1148f00d4f59SWei Liu static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path, 1149f00d4f59SWei Liu const char *name, void *value, size_t size) 1150f00d4f59SWei Liu { 1151f00d4f59SWei Liu char *path = fs_path->data; 1152f00d4f59SWei Liu 1153f00d4f59SWei Liu return v9fs_get_xattr(ctx, path, name, value, size); 1154f00d4f59SWei Liu } 1155f00d4f59SWei Liu 1156f00d4f59SWei Liu static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path, 1157f00d4f59SWei Liu void *value, size_t size) 1158f00d4f59SWei Liu { 1159f00d4f59SWei Liu char *path = fs_path->data; 1160f00d4f59SWei Liu 1161f00d4f59SWei Liu return v9fs_list_xattr(ctx, path, value, size); 1162f00d4f59SWei Liu } 1163f00d4f59SWei Liu 1164f00d4f59SWei Liu static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name, 1165f00d4f59SWei Liu void *value, size_t size, int flags) 1166f00d4f59SWei Liu { 1167f00d4f59SWei Liu char *path = fs_path->data; 1168f00d4f59SWei Liu 1169f00d4f59SWei Liu return v9fs_set_xattr(ctx, path, name, value, size, flags); 1170f00d4f59SWei Liu } 1171f00d4f59SWei Liu 1172f00d4f59SWei Liu static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path, 1173f00d4f59SWei Liu const char *name) 1174f00d4f59SWei Liu { 1175f00d4f59SWei Liu char *path = fs_path->data; 1176f00d4f59SWei Liu 1177f00d4f59SWei Liu return v9fs_remove_xattr(ctx, path, name); 1178f00d4f59SWei Liu } 1179f00d4f59SWei Liu 1180f00d4f59SWei Liu static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path, 1181f00d4f59SWei Liu const char *name, V9fsPath *target) 1182f00d4f59SWei Liu { 1183f00d4f59SWei Liu if (dir_path) { 1184e3e83f2eSGreg Kurz v9fs_path_sprintf(target, "%s/%s", dir_path->data, name); 1185f00d4f59SWei Liu } else { 1186e3e83f2eSGreg Kurz v9fs_path_sprintf(target, "%s", name); 1187f00d4f59SWei Liu } 1188f00d4f59SWei Liu return 0; 1189f00d4f59SWei Liu } 1190f00d4f59SWei Liu 1191f00d4f59SWei Liu static int local_renameat(FsContext *ctx, V9fsPath *olddir, 1192f00d4f59SWei Liu const char *old_name, V9fsPath *newdir, 1193f00d4f59SWei Liu const char *new_name) 1194f00d4f59SWei Liu { 1195f00d4f59SWei Liu int ret; 1196*99f2cf4bSGreg Kurz int odirfd, ndirfd; 1197f00d4f59SWei Liu 1198*99f2cf4bSGreg Kurz odirfd = local_opendir_nofollow(ctx, olddir->data); 1199*99f2cf4bSGreg Kurz if (odirfd == -1) { 1200*99f2cf4bSGreg Kurz return -1; 1201*99f2cf4bSGreg Kurz } 1202f00d4f59SWei Liu 1203*99f2cf4bSGreg Kurz ndirfd = local_opendir_nofollow(ctx, newdir->data); 1204*99f2cf4bSGreg Kurz if (ndirfd == -1) { 1205*99f2cf4bSGreg Kurz close_preserve_errno(odirfd); 1206*99f2cf4bSGreg Kurz return -1; 1207*99f2cf4bSGreg Kurz } 1208f00d4f59SWei Liu 1209*99f2cf4bSGreg Kurz ret = renameat(odirfd, old_name, ndirfd, new_name); 1210*99f2cf4bSGreg Kurz if (ret < 0) { 1211*99f2cf4bSGreg Kurz goto out; 1212*99f2cf4bSGreg Kurz } 1213*99f2cf4bSGreg Kurz 1214*99f2cf4bSGreg Kurz if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 1215*99f2cf4bSGreg Kurz int omap_dirfd, nmap_dirfd; 1216*99f2cf4bSGreg Kurz 1217*99f2cf4bSGreg Kurz ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700); 1218*99f2cf4bSGreg Kurz if (ret < 0 && errno != EEXIST) { 1219*99f2cf4bSGreg Kurz goto err_undo_rename; 1220*99f2cf4bSGreg Kurz } 1221*99f2cf4bSGreg Kurz 1222*99f2cf4bSGreg Kurz omap_dirfd = openat(odirfd, VIRTFS_META_DIR, 1223*99f2cf4bSGreg Kurz O_RDONLY | O_DIRECTORY | O_NOFOLLOW); 1224*99f2cf4bSGreg Kurz if (omap_dirfd == -1) { 1225*99f2cf4bSGreg Kurz goto err; 1226*99f2cf4bSGreg Kurz } 1227*99f2cf4bSGreg Kurz 1228*99f2cf4bSGreg Kurz nmap_dirfd = openat(ndirfd, VIRTFS_META_DIR, 1229*99f2cf4bSGreg Kurz O_RDONLY | O_DIRECTORY | O_NOFOLLOW); 1230*99f2cf4bSGreg Kurz if (nmap_dirfd == -1) { 1231*99f2cf4bSGreg Kurz close_preserve_errno(omap_dirfd); 1232*99f2cf4bSGreg Kurz goto err; 1233*99f2cf4bSGreg Kurz } 1234*99f2cf4bSGreg Kurz 1235*99f2cf4bSGreg Kurz /* rename the .virtfs_metadata files */ 1236*99f2cf4bSGreg Kurz ret = renameat(omap_dirfd, old_name, nmap_dirfd, new_name); 1237*99f2cf4bSGreg Kurz close_preserve_errno(nmap_dirfd); 1238*99f2cf4bSGreg Kurz close_preserve_errno(omap_dirfd); 1239*99f2cf4bSGreg Kurz if (ret < 0 && errno != ENOENT) { 1240*99f2cf4bSGreg Kurz goto err_undo_rename; 1241*99f2cf4bSGreg Kurz } 1242*99f2cf4bSGreg Kurz 1243*99f2cf4bSGreg Kurz ret = 0; 1244*99f2cf4bSGreg Kurz } 1245*99f2cf4bSGreg Kurz goto out; 1246*99f2cf4bSGreg Kurz 1247*99f2cf4bSGreg Kurz err: 1248*99f2cf4bSGreg Kurz ret = -1; 1249*99f2cf4bSGreg Kurz err_undo_rename: 1250*99f2cf4bSGreg Kurz renameat_preserve_errno(ndirfd, new_name, odirfd, old_name); 1251*99f2cf4bSGreg Kurz out: 1252*99f2cf4bSGreg Kurz close_preserve_errno(ndirfd); 1253*99f2cf4bSGreg Kurz close_preserve_errno(odirfd); 1254f00d4f59SWei Liu return ret; 1255f00d4f59SWei Liu } 1256f00d4f59SWei Liu 1257f00d4f59SWei Liu static int local_unlinkat(FsContext *ctx, V9fsPath *dir, 1258f00d4f59SWei Liu const char *name, int flags) 1259f00d4f59SWei Liu { 1260f00d4f59SWei Liu int ret; 1261df4938a6SGreg Kurz int dirfd; 1262f00d4f59SWei Liu 1263df4938a6SGreg Kurz dirfd = local_opendir_nofollow(ctx, dir->data); 1264df4938a6SGreg Kurz if (dirfd == -1) { 1265df4938a6SGreg Kurz return -1; 1266df4938a6SGreg Kurz } 1267f00d4f59SWei Liu 1268df4938a6SGreg Kurz ret = local_unlinkat_common(ctx, dirfd, name, flags); 1269df4938a6SGreg Kurz close_preserve_errno(dirfd); 1270f00d4f59SWei Liu return ret; 1271f00d4f59SWei Liu } 1272f00d4f59SWei Liu 1273f00d4f59SWei Liu static int local_ioc_getversion(FsContext *ctx, V9fsPath *path, 1274f00d4f59SWei Liu mode_t st_mode, uint64_t *st_gen) 1275f00d4f59SWei Liu { 1276f00d4f59SWei Liu #ifdef FS_IOC_GETVERSION 1277f00d4f59SWei Liu int err; 1278f00d4f59SWei Liu V9fsFidOpenState fid_open; 1279f00d4f59SWei Liu 1280f00d4f59SWei Liu /* 1281f00d4f59SWei Liu * Do not try to open special files like device nodes, fifos etc 1282f00d4f59SWei Liu * We can get fd for regular files and directories only 1283f00d4f59SWei Liu */ 1284f00d4f59SWei Liu if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) { 1285f00d4f59SWei Liu errno = ENOTTY; 1286f00d4f59SWei Liu return -1; 1287f00d4f59SWei Liu } 1288f00d4f59SWei Liu err = local_open(ctx, path, O_RDONLY, &fid_open); 1289f00d4f59SWei Liu if (err < 0) { 1290f00d4f59SWei Liu return err; 1291f00d4f59SWei Liu } 1292f00d4f59SWei Liu err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen); 1293f00d4f59SWei Liu local_close(ctx, &fid_open); 1294f00d4f59SWei Liu return err; 1295f00d4f59SWei Liu #else 1296f00d4f59SWei Liu errno = ENOTTY; 1297f00d4f59SWei Liu return -1; 1298f00d4f59SWei Liu #endif 1299f00d4f59SWei Liu } 1300f00d4f59SWei Liu 1301f00d4f59SWei Liu static int local_init(FsContext *ctx) 1302f00d4f59SWei Liu { 1303f00d4f59SWei Liu struct statfs stbuf; 13040e35a378SGreg Kurz LocalData *data = g_malloc(sizeof(*data)); 13050e35a378SGreg Kurz 13060e35a378SGreg Kurz data->mountfd = open(ctx->fs_root, O_DIRECTORY | O_RDONLY); 13070e35a378SGreg Kurz if (data->mountfd == -1) { 13080e35a378SGreg Kurz goto err; 13090e35a378SGreg Kurz } 1310f00d4f59SWei Liu 131100c90bd1SGreg Kurz #ifdef FS_IOC_GETVERSION 131200c90bd1SGreg Kurz /* 131300c90bd1SGreg Kurz * use ioc_getversion only if the ioctl is definied 131400c90bd1SGreg Kurz */ 13150e35a378SGreg Kurz if (fstatfs(data->mountfd, &stbuf) < 0) { 13160e35a378SGreg Kurz close_preserve_errno(data->mountfd); 13170e35a378SGreg Kurz goto err; 131800c90bd1SGreg Kurz } 131900c90bd1SGreg Kurz switch (stbuf.f_type) { 132000c90bd1SGreg Kurz case EXT2_SUPER_MAGIC: 132100c90bd1SGreg Kurz case BTRFS_SUPER_MAGIC: 132200c90bd1SGreg Kurz case REISERFS_SUPER_MAGIC: 132300c90bd1SGreg Kurz case XFS_SUPER_MAGIC: 132400c90bd1SGreg Kurz ctx->exops.get_st_gen = local_ioc_getversion; 132500c90bd1SGreg Kurz break; 132600c90bd1SGreg Kurz } 132700c90bd1SGreg Kurz #endif 132800c90bd1SGreg Kurz 1329f00d4f59SWei Liu if (ctx->export_flags & V9FS_SM_PASSTHROUGH) { 1330f00d4f59SWei Liu ctx->xops = passthrough_xattr_ops; 1331f00d4f59SWei Liu } else if (ctx->export_flags & V9FS_SM_MAPPED) { 1332f00d4f59SWei Liu ctx->xops = mapped_xattr_ops; 1333f00d4f59SWei Liu } else if (ctx->export_flags & V9FS_SM_NONE) { 1334f00d4f59SWei Liu ctx->xops = none_xattr_ops; 1335f00d4f59SWei Liu } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 1336f00d4f59SWei Liu /* 1337f00d4f59SWei Liu * xattr operation for mapped-file and passthrough 1338f00d4f59SWei Liu * remain same. 1339f00d4f59SWei Liu */ 1340f00d4f59SWei Liu ctx->xops = passthrough_xattr_ops; 1341f00d4f59SWei Liu } 1342f00d4f59SWei Liu ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT; 134300c90bd1SGreg Kurz 13440e35a378SGreg Kurz ctx->private = data; 134500c90bd1SGreg Kurz return 0; 13460e35a378SGreg Kurz 13470e35a378SGreg Kurz err: 13480e35a378SGreg Kurz g_free(data); 13490e35a378SGreg Kurz return -1; 13500e35a378SGreg Kurz } 13510e35a378SGreg Kurz 13520e35a378SGreg Kurz static void local_cleanup(FsContext *ctx) 13530e35a378SGreg Kurz { 13540e35a378SGreg Kurz LocalData *data = ctx->private; 13550e35a378SGreg Kurz 13560e35a378SGreg Kurz close(data->mountfd); 13570e35a378SGreg Kurz g_free(data); 1358f00d4f59SWei Liu } 1359f00d4f59SWei Liu 1360f00d4f59SWei Liu static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse) 1361f00d4f59SWei Liu { 1362f00d4f59SWei Liu const char *sec_model = qemu_opt_get(opts, "security_model"); 1363f00d4f59SWei Liu const char *path = qemu_opt_get(opts, "path"); 1364f00d4f59SWei Liu 1365f00d4f59SWei Liu if (!sec_model) { 136663325b18SGreg Kurz error_report("Security model not specified, local fs needs security model"); 136763325b18SGreg Kurz error_printf("valid options are:" 136863325b18SGreg Kurz "\tsecurity_model=[passthrough|mapped-xattr|mapped-file|none]\n"); 1369f00d4f59SWei Liu return -1; 1370f00d4f59SWei Liu } 1371f00d4f59SWei Liu 1372f00d4f59SWei Liu if (!strcmp(sec_model, "passthrough")) { 1373f00d4f59SWei Liu fse->export_flags |= V9FS_SM_PASSTHROUGH; 1374f00d4f59SWei Liu } else if (!strcmp(sec_model, "mapped") || 1375f00d4f59SWei Liu !strcmp(sec_model, "mapped-xattr")) { 1376f00d4f59SWei Liu fse->export_flags |= V9FS_SM_MAPPED; 1377f00d4f59SWei Liu } else if (!strcmp(sec_model, "none")) { 1378f00d4f59SWei Liu fse->export_flags |= V9FS_SM_NONE; 1379f00d4f59SWei Liu } else if (!strcmp(sec_model, "mapped-file")) { 1380f00d4f59SWei Liu fse->export_flags |= V9FS_SM_MAPPED_FILE; 1381f00d4f59SWei Liu } else { 138263325b18SGreg Kurz error_report("Invalid security model %s specified", sec_model); 138363325b18SGreg Kurz error_printf("valid options are:" 138463325b18SGreg Kurz "\t[passthrough|mapped-xattr|mapped-file|none]\n"); 1385f00d4f59SWei Liu return -1; 1386f00d4f59SWei Liu } 1387f00d4f59SWei Liu 1388f00d4f59SWei Liu if (!path) { 138963325b18SGreg Kurz error_report("fsdev: No path specified"); 1390f00d4f59SWei Liu return -1; 1391f00d4f59SWei Liu } 1392f00d4f59SWei Liu fse->path = g_strdup(path); 1393f00d4f59SWei Liu 1394f00d4f59SWei Liu return 0; 1395f00d4f59SWei Liu } 1396f00d4f59SWei Liu 1397f00d4f59SWei Liu FileOperations local_ops = { 1398f00d4f59SWei Liu .parse_opts = local_parse_opts, 1399f00d4f59SWei Liu .init = local_init, 14000e35a378SGreg Kurz .cleanup = local_cleanup, 1401f00d4f59SWei Liu .lstat = local_lstat, 1402f00d4f59SWei Liu .readlink = local_readlink, 1403f00d4f59SWei Liu .close = local_close, 1404f00d4f59SWei Liu .closedir = local_closedir, 1405f00d4f59SWei Liu .open = local_open, 1406f00d4f59SWei Liu .opendir = local_opendir, 1407f00d4f59SWei Liu .rewinddir = local_rewinddir, 1408f00d4f59SWei Liu .telldir = local_telldir, 1409635324e8SGreg Kurz .readdir = local_readdir, 1410f00d4f59SWei Liu .seekdir = local_seekdir, 1411f00d4f59SWei Liu .preadv = local_preadv, 1412f00d4f59SWei Liu .pwritev = local_pwritev, 1413f00d4f59SWei Liu .chmod = local_chmod, 1414f00d4f59SWei Liu .mknod = local_mknod, 1415f00d4f59SWei Liu .mkdir = local_mkdir, 1416f00d4f59SWei Liu .fstat = local_fstat, 1417f00d4f59SWei Liu .open2 = local_open2, 1418f00d4f59SWei Liu .symlink = local_symlink, 1419f00d4f59SWei Liu .link = local_link, 1420f00d4f59SWei Liu .truncate = local_truncate, 1421f00d4f59SWei Liu .rename = local_rename, 1422f00d4f59SWei Liu .chown = local_chown, 1423f00d4f59SWei Liu .utimensat = local_utimensat, 1424f00d4f59SWei Liu .remove = local_remove, 1425f00d4f59SWei Liu .fsync = local_fsync, 1426f00d4f59SWei Liu .statfs = local_statfs, 1427f00d4f59SWei Liu .lgetxattr = local_lgetxattr, 1428f00d4f59SWei Liu .llistxattr = local_llistxattr, 1429f00d4f59SWei Liu .lsetxattr = local_lsetxattr, 1430f00d4f59SWei Liu .lremovexattr = local_lremovexattr, 1431f00d4f59SWei Liu .name_to_path = local_name_to_path, 1432f00d4f59SWei Liu .renameat = local_renameat, 1433f00d4f59SWei Liu .unlinkat = local_unlinkat, 1434f00d4f59SWei Liu }; 1435