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 78*ad0b46e6SGreg Kurz static void unlinkat_preserve_errno(int dirfd, const char *path, int flags) 79*ad0b46e6SGreg Kurz { 80*ad0b46e6SGreg Kurz int serrno = errno; 81*ad0b46e6SGreg Kurz unlinkat(dirfd, path, flags); 82*ad0b46e6SGreg Kurz errno = serrno; 83*ad0b46e6SGreg Kurz } 84*ad0b46e6SGreg Kurz 85f00d4f59SWei Liu #define VIRTFS_META_DIR ".virtfs_metadata" 86f00d4f59SWei Liu 87f00d4f59SWei Liu static char *local_mapped_attr_path(FsContext *ctx, const char *path) 88f00d4f59SWei Liu { 89f00d4f59SWei Liu int dirlen; 90f00d4f59SWei Liu const char *name = strrchr(path, '/'); 91f00d4f59SWei Liu if (name) { 92f00d4f59SWei Liu dirlen = name - path; 93f00d4f59SWei Liu ++name; 94f00d4f59SWei Liu } else { 95f00d4f59SWei Liu name = path; 96f00d4f59SWei Liu dirlen = 0; 97f00d4f59SWei Liu } 98f00d4f59SWei Liu return g_strdup_printf("%s/%.*s/%s/%s", ctx->fs_root, 99f00d4f59SWei Liu dirlen, path, VIRTFS_META_DIR, name); 100f00d4f59SWei Liu } 101f00d4f59SWei Liu 102f00d4f59SWei Liu static FILE *local_fopen(const char *path, const char *mode) 103f00d4f59SWei Liu { 104f00d4f59SWei Liu int fd, o_mode = 0; 105f00d4f59SWei Liu FILE *fp; 106f00d4f59SWei Liu int flags = O_NOFOLLOW; 107f00d4f59SWei Liu /* 108f00d4f59SWei Liu * only supports two modes 109f00d4f59SWei Liu */ 110f00d4f59SWei Liu if (mode[0] == 'r') { 111f00d4f59SWei Liu flags |= O_RDONLY; 112f00d4f59SWei Liu } else if (mode[0] == 'w') { 113f00d4f59SWei Liu flags |= O_WRONLY | O_TRUNC | O_CREAT; 114f00d4f59SWei Liu o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; 115f00d4f59SWei Liu } else { 116f00d4f59SWei Liu return NULL; 117f00d4f59SWei Liu } 118f00d4f59SWei Liu fd = open(path, flags, o_mode); 119f00d4f59SWei Liu if (fd == -1) { 120f00d4f59SWei Liu return NULL; 121f00d4f59SWei Liu } 122f00d4f59SWei Liu fp = fdopen(fd, mode); 123f00d4f59SWei Liu if (!fp) { 124f00d4f59SWei Liu close(fd); 125f00d4f59SWei Liu } 126f00d4f59SWei Liu return fp; 127f00d4f59SWei Liu } 128f00d4f59SWei Liu 129f9aef99bSGreg Kurz static FILE *local_fopenat(int dirfd, const char *name, const char *mode) 130f9aef99bSGreg Kurz { 131f9aef99bSGreg Kurz int fd, o_mode = 0; 132f9aef99bSGreg Kurz FILE *fp; 133f9aef99bSGreg Kurz int flags; 134f9aef99bSGreg Kurz /* 135f9aef99bSGreg Kurz * only supports two modes 136f9aef99bSGreg Kurz */ 137f9aef99bSGreg Kurz if (mode[0] == 'r') { 138f9aef99bSGreg Kurz flags = O_RDONLY; 139f9aef99bSGreg Kurz } else if (mode[0] == 'w') { 140f9aef99bSGreg Kurz flags = O_WRONLY | O_TRUNC | O_CREAT; 141f9aef99bSGreg Kurz o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; 142f9aef99bSGreg Kurz } else { 143f9aef99bSGreg Kurz return NULL; 144f9aef99bSGreg Kurz } 145f9aef99bSGreg Kurz fd = openat_file(dirfd, name, flags, o_mode); 146f9aef99bSGreg Kurz if (fd == -1) { 147f9aef99bSGreg Kurz return NULL; 148f9aef99bSGreg Kurz } 149f9aef99bSGreg Kurz fp = fdopen(fd, mode); 150f9aef99bSGreg Kurz if (!fp) { 151f9aef99bSGreg Kurz close(fd); 152f9aef99bSGreg Kurz } 153f9aef99bSGreg Kurz return fp; 154f9aef99bSGreg Kurz } 155f9aef99bSGreg Kurz 156f00d4f59SWei Liu #define ATTR_MAX 100 157f9aef99bSGreg Kurz static void local_mapped_file_attr(int dirfd, const char *name, 158f00d4f59SWei Liu struct stat *stbuf) 159f00d4f59SWei Liu { 160f00d4f59SWei Liu FILE *fp; 161f00d4f59SWei Liu char buf[ATTR_MAX]; 162f9aef99bSGreg Kurz int map_dirfd; 163f00d4f59SWei Liu 16499f2cf4bSGreg Kurz map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR); 165f9aef99bSGreg Kurz if (map_dirfd == -1) { 166f9aef99bSGreg Kurz return; 167f9aef99bSGreg Kurz } 168f9aef99bSGreg Kurz 169f9aef99bSGreg Kurz fp = local_fopenat(map_dirfd, name, "r"); 170f9aef99bSGreg Kurz close_preserve_errno(map_dirfd); 171f00d4f59SWei Liu if (!fp) { 172f00d4f59SWei Liu return; 173f00d4f59SWei Liu } 174f00d4f59SWei Liu memset(buf, 0, ATTR_MAX); 175f00d4f59SWei Liu while (fgets(buf, ATTR_MAX, fp)) { 176f00d4f59SWei Liu if (!strncmp(buf, "virtfs.uid", 10)) { 177f00d4f59SWei Liu stbuf->st_uid = atoi(buf+11); 178f00d4f59SWei Liu } else if (!strncmp(buf, "virtfs.gid", 10)) { 179f00d4f59SWei Liu stbuf->st_gid = atoi(buf+11); 180f00d4f59SWei Liu } else if (!strncmp(buf, "virtfs.mode", 11)) { 181f00d4f59SWei Liu stbuf->st_mode = atoi(buf+12); 182f00d4f59SWei Liu } else if (!strncmp(buf, "virtfs.rdev", 11)) { 183f00d4f59SWei Liu stbuf->st_rdev = atoi(buf+12); 184f00d4f59SWei Liu } 185f00d4f59SWei Liu memset(buf, 0, ATTR_MAX); 186f00d4f59SWei Liu } 187f00d4f59SWei Liu fclose(fp); 188f00d4f59SWei Liu } 189f00d4f59SWei Liu 190f00d4f59SWei Liu static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf) 191f00d4f59SWei Liu { 192f9aef99bSGreg Kurz int err = -1; 193f9aef99bSGreg Kurz char *dirpath = g_path_get_dirname(fs_path->data); 194f9aef99bSGreg Kurz char *name = g_path_get_basename(fs_path->data); 195f9aef99bSGreg Kurz int dirfd; 196f00d4f59SWei Liu 197f9aef99bSGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dirpath); 198f9aef99bSGreg Kurz if (dirfd == -1) { 199f9aef99bSGreg Kurz goto out; 200f9aef99bSGreg Kurz } 201f9aef99bSGreg Kurz 202f9aef99bSGreg Kurz err = fstatat(dirfd, name, stbuf, AT_SYMLINK_NOFOLLOW); 203f00d4f59SWei Liu if (err) { 204f00d4f59SWei Liu goto err_out; 205f00d4f59SWei Liu } 206f00d4f59SWei Liu if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 207f00d4f59SWei Liu /* Actual credentials are part of extended attrs */ 208f00d4f59SWei Liu uid_t tmp_uid; 209f00d4f59SWei Liu gid_t tmp_gid; 210f00d4f59SWei Liu mode_t tmp_mode; 211f00d4f59SWei Liu dev_t tmp_dev; 212f9aef99bSGreg Kurz 213f9aef99bSGreg Kurz if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.uid", &tmp_uid, 214f9aef99bSGreg Kurz sizeof(uid_t)) > 0) { 215f00d4f59SWei Liu stbuf->st_uid = le32_to_cpu(tmp_uid); 216f00d4f59SWei Liu } 217f9aef99bSGreg Kurz if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.gid", &tmp_gid, 218f9aef99bSGreg Kurz sizeof(gid_t)) > 0) { 219f00d4f59SWei Liu stbuf->st_gid = le32_to_cpu(tmp_gid); 220f00d4f59SWei Liu } 221f9aef99bSGreg Kurz if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.mode", &tmp_mode, 222f9aef99bSGreg Kurz sizeof(mode_t)) > 0) { 223f00d4f59SWei Liu stbuf->st_mode = le32_to_cpu(tmp_mode); 224f00d4f59SWei Liu } 225f9aef99bSGreg Kurz if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.rdev", &tmp_dev, 226f9aef99bSGreg Kurz sizeof(dev_t)) > 0) { 227f00d4f59SWei Liu stbuf->st_rdev = le64_to_cpu(tmp_dev); 228f00d4f59SWei Liu } 229f00d4f59SWei Liu } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 230f9aef99bSGreg Kurz local_mapped_file_attr(dirfd, name, stbuf); 231f00d4f59SWei Liu } 232f00d4f59SWei Liu 233f00d4f59SWei Liu err_out: 234f9aef99bSGreg Kurz close_preserve_errno(dirfd); 235f9aef99bSGreg Kurz out: 236f9aef99bSGreg Kurz g_free(name); 237f9aef99bSGreg Kurz g_free(dirpath); 238f00d4f59SWei Liu return err; 239f00d4f59SWei Liu } 240f00d4f59SWei Liu 241f00d4f59SWei Liu static int local_create_mapped_attr_dir(FsContext *ctx, const char *path) 242f00d4f59SWei Liu { 243f00d4f59SWei Liu int err; 244f00d4f59SWei Liu char *attr_dir; 245f00d4f59SWei Liu char *tmp_path = g_strdup(path); 246f00d4f59SWei Liu 247f00d4f59SWei Liu attr_dir = g_strdup_printf("%s/%s/%s", 248f00d4f59SWei Liu ctx->fs_root, dirname(tmp_path), VIRTFS_META_DIR); 249f00d4f59SWei Liu 250f00d4f59SWei Liu err = mkdir(attr_dir, 0700); 251f00d4f59SWei Liu if (err < 0 && errno == EEXIST) { 252f00d4f59SWei Liu err = 0; 253f00d4f59SWei Liu } 254f00d4f59SWei Liu g_free(attr_dir); 255f00d4f59SWei Liu g_free(tmp_path); 256f00d4f59SWei Liu return err; 257f00d4f59SWei Liu } 258f00d4f59SWei Liu 259f00d4f59SWei Liu static int local_set_mapped_file_attr(FsContext *ctx, 260f00d4f59SWei Liu const char *path, FsCred *credp) 261f00d4f59SWei Liu { 262f00d4f59SWei Liu FILE *fp; 263f00d4f59SWei Liu int ret = 0; 264f00d4f59SWei Liu char buf[ATTR_MAX]; 265f00d4f59SWei Liu char *attr_path; 266f00d4f59SWei Liu int uid = -1, gid = -1, mode = -1, rdev = -1; 267f00d4f59SWei Liu 268f00d4f59SWei Liu attr_path = local_mapped_attr_path(ctx, path); 269f00d4f59SWei Liu fp = local_fopen(attr_path, "r"); 270f00d4f59SWei Liu if (!fp) { 271f00d4f59SWei Liu goto create_map_file; 272f00d4f59SWei Liu } 273f00d4f59SWei Liu memset(buf, 0, ATTR_MAX); 274f00d4f59SWei Liu while (fgets(buf, ATTR_MAX, fp)) { 275f00d4f59SWei Liu if (!strncmp(buf, "virtfs.uid", 10)) { 276f00d4f59SWei Liu uid = atoi(buf+11); 277f00d4f59SWei Liu } else if (!strncmp(buf, "virtfs.gid", 10)) { 278f00d4f59SWei Liu gid = atoi(buf+11); 279f00d4f59SWei Liu } else if (!strncmp(buf, "virtfs.mode", 11)) { 280f00d4f59SWei Liu mode = atoi(buf+12); 281f00d4f59SWei Liu } else if (!strncmp(buf, "virtfs.rdev", 11)) { 282f00d4f59SWei Liu rdev = atoi(buf+12); 283f00d4f59SWei Liu } 284f00d4f59SWei Liu memset(buf, 0, ATTR_MAX); 285f00d4f59SWei Liu } 286f00d4f59SWei Liu fclose(fp); 287f00d4f59SWei Liu goto update_map_file; 288f00d4f59SWei Liu 289f00d4f59SWei Liu create_map_file: 290f00d4f59SWei Liu ret = local_create_mapped_attr_dir(ctx, path); 291f00d4f59SWei Liu if (ret < 0) { 292f00d4f59SWei Liu goto err_out; 293f00d4f59SWei Liu } 294f00d4f59SWei Liu 295f00d4f59SWei Liu update_map_file: 296f00d4f59SWei Liu fp = local_fopen(attr_path, "w"); 297f00d4f59SWei Liu if (!fp) { 298f00d4f59SWei Liu ret = -1; 299f00d4f59SWei Liu goto err_out; 300f00d4f59SWei Liu } 301f00d4f59SWei Liu 302f00d4f59SWei Liu if (credp->fc_uid != -1) { 303f00d4f59SWei Liu uid = credp->fc_uid; 304f00d4f59SWei Liu } 305f00d4f59SWei Liu if (credp->fc_gid != -1) { 306f00d4f59SWei Liu gid = credp->fc_gid; 307f00d4f59SWei Liu } 308f00d4f59SWei Liu if (credp->fc_mode != -1) { 309f00d4f59SWei Liu mode = credp->fc_mode; 310f00d4f59SWei Liu } 311f00d4f59SWei Liu if (credp->fc_rdev != -1) { 312f00d4f59SWei Liu rdev = credp->fc_rdev; 313f00d4f59SWei Liu } 314f00d4f59SWei Liu 315f00d4f59SWei Liu 316f00d4f59SWei Liu if (uid != -1) { 317f00d4f59SWei Liu fprintf(fp, "virtfs.uid=%d\n", uid); 318f00d4f59SWei Liu } 319f00d4f59SWei Liu if (gid != -1) { 320f00d4f59SWei Liu fprintf(fp, "virtfs.gid=%d\n", gid); 321f00d4f59SWei Liu } 322f00d4f59SWei Liu if (mode != -1) { 323f00d4f59SWei Liu fprintf(fp, "virtfs.mode=%d\n", mode); 324f00d4f59SWei Liu } 325f00d4f59SWei Liu if (rdev != -1) { 326f00d4f59SWei Liu fprintf(fp, "virtfs.rdev=%d\n", rdev); 327f00d4f59SWei Liu } 328f00d4f59SWei Liu fclose(fp); 329f00d4f59SWei Liu 330f00d4f59SWei Liu err_out: 331f00d4f59SWei Liu g_free(attr_path); 332f00d4f59SWei Liu return ret; 333f00d4f59SWei Liu } 334f00d4f59SWei Liu 335f00d4f59SWei Liu static int local_set_xattr(const char *path, FsCred *credp) 336f00d4f59SWei Liu { 337f00d4f59SWei Liu int err; 338f00d4f59SWei Liu 339f00d4f59SWei Liu if (credp->fc_uid != -1) { 340f00d4f59SWei Liu uint32_t tmp_uid = cpu_to_le32(credp->fc_uid); 341f00d4f59SWei Liu err = setxattr(path, "user.virtfs.uid", &tmp_uid, sizeof(uid_t), 0); 342f00d4f59SWei Liu if (err) { 343f00d4f59SWei Liu return err; 344f00d4f59SWei Liu } 345f00d4f59SWei Liu } 346f00d4f59SWei Liu if (credp->fc_gid != -1) { 347f00d4f59SWei Liu uint32_t tmp_gid = cpu_to_le32(credp->fc_gid); 348f00d4f59SWei Liu err = setxattr(path, "user.virtfs.gid", &tmp_gid, sizeof(gid_t), 0); 349f00d4f59SWei Liu if (err) { 350f00d4f59SWei Liu return err; 351f00d4f59SWei Liu } 352f00d4f59SWei Liu } 353f00d4f59SWei Liu if (credp->fc_mode != -1) { 354f00d4f59SWei Liu uint32_t tmp_mode = cpu_to_le32(credp->fc_mode); 355f00d4f59SWei Liu err = setxattr(path, "user.virtfs.mode", &tmp_mode, sizeof(mode_t), 0); 356f00d4f59SWei Liu if (err) { 357f00d4f59SWei Liu return err; 358f00d4f59SWei Liu } 359f00d4f59SWei Liu } 360f00d4f59SWei Liu if (credp->fc_rdev != -1) { 361f00d4f59SWei Liu uint64_t tmp_rdev = cpu_to_le64(credp->fc_rdev); 362f00d4f59SWei Liu err = setxattr(path, "user.virtfs.rdev", &tmp_rdev, sizeof(dev_t), 0); 363f00d4f59SWei Liu if (err) { 364f00d4f59SWei Liu return err; 365f00d4f59SWei Liu } 366f00d4f59SWei Liu } 367f00d4f59SWei Liu return 0; 368f00d4f59SWei Liu } 369f00d4f59SWei Liu 370f00d4f59SWei Liu static int local_post_create_passthrough(FsContext *fs_ctx, const char *path, 371f00d4f59SWei Liu FsCred *credp) 372f00d4f59SWei Liu { 373f00d4f59SWei Liu char *buffer; 374f00d4f59SWei Liu 375f00d4f59SWei Liu buffer = rpath(fs_ctx, path); 376f00d4f59SWei Liu if (lchown(buffer, credp->fc_uid, credp->fc_gid) < 0) { 377f00d4f59SWei Liu /* 378f00d4f59SWei Liu * If we fail to change ownership and if we are 379f00d4f59SWei Liu * using security model none. Ignore the error 380f00d4f59SWei Liu */ 381f00d4f59SWei Liu if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) { 382f00d4f59SWei Liu goto err; 383f00d4f59SWei Liu } 384f00d4f59SWei Liu } 385f00d4f59SWei Liu 386f00d4f59SWei Liu if (chmod(buffer, credp->fc_mode & 07777) < 0) { 387f00d4f59SWei Liu goto err; 388f00d4f59SWei Liu } 389f00d4f59SWei Liu 390f00d4f59SWei Liu g_free(buffer); 391f00d4f59SWei Liu return 0; 392f00d4f59SWei Liu err: 393f00d4f59SWei Liu g_free(buffer); 394f00d4f59SWei Liu return -1; 395f00d4f59SWei Liu } 396f00d4f59SWei Liu 397f00d4f59SWei Liu static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path, 398f00d4f59SWei Liu char *buf, size_t bufsz) 399f00d4f59SWei Liu { 400f00d4f59SWei Liu ssize_t tsize = -1; 401f00d4f59SWei Liu 402f00d4f59SWei Liu if ((fs_ctx->export_flags & V9FS_SM_MAPPED) || 403f00d4f59SWei Liu (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) { 404f00d4f59SWei Liu int fd; 405bec1e954SGreg Kurz 406bec1e954SGreg Kurz fd = local_open_nofollow(fs_ctx, fs_path->data, O_RDONLY, 0); 407f00d4f59SWei Liu if (fd == -1) { 408f00d4f59SWei Liu return -1; 409f00d4f59SWei Liu } 410f00d4f59SWei Liu do { 411f00d4f59SWei Liu tsize = read(fd, (void *)buf, bufsz); 412f00d4f59SWei Liu } while (tsize == -1 && errno == EINTR); 413bec1e954SGreg Kurz close_preserve_errno(fd); 414f00d4f59SWei Liu } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 415f00d4f59SWei Liu (fs_ctx->export_flags & V9FS_SM_NONE)) { 416bec1e954SGreg Kurz char *dirpath = g_path_get_dirname(fs_path->data); 417bec1e954SGreg Kurz char *name = g_path_get_basename(fs_path->data); 418bec1e954SGreg Kurz int dirfd; 419bec1e954SGreg Kurz 420bec1e954SGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dirpath); 421bec1e954SGreg Kurz if (dirfd == -1) { 422bec1e954SGreg Kurz goto out; 423bec1e954SGreg Kurz } 424bec1e954SGreg Kurz 425bec1e954SGreg Kurz tsize = readlinkat(dirfd, name, buf, bufsz); 426bec1e954SGreg Kurz close_preserve_errno(dirfd); 427bec1e954SGreg Kurz out: 428bec1e954SGreg Kurz g_free(name); 429bec1e954SGreg Kurz g_free(dirpath); 430f00d4f59SWei Liu } 431f00d4f59SWei Liu return tsize; 432f00d4f59SWei Liu } 433f00d4f59SWei Liu 434f00d4f59SWei Liu static int local_close(FsContext *ctx, V9fsFidOpenState *fs) 435f00d4f59SWei Liu { 436f00d4f59SWei Liu return close(fs->fd); 437f00d4f59SWei Liu } 438f00d4f59SWei Liu 439f00d4f59SWei Liu static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs) 440f00d4f59SWei Liu { 441f314ea4eSGreg Kurz return closedir(fs->dir.stream); 442f00d4f59SWei Liu } 443f00d4f59SWei Liu 444f00d4f59SWei Liu static int local_open(FsContext *ctx, V9fsPath *fs_path, 445f00d4f59SWei Liu int flags, V9fsFidOpenState *fs) 446f00d4f59SWei Liu { 44721328e1eSGreg Kurz int fd; 448f00d4f59SWei Liu 449996a0d76SGreg Kurz fd = local_open_nofollow(ctx, fs_path->data, flags, 0); 45021328e1eSGreg Kurz if (fd == -1) { 45121328e1eSGreg Kurz return -1; 45221328e1eSGreg Kurz } 45321328e1eSGreg Kurz fs->fd = fd; 454f00d4f59SWei Liu return fs->fd; 455f00d4f59SWei Liu } 456f00d4f59SWei Liu 457f00d4f59SWei Liu static int local_opendir(FsContext *ctx, 458f00d4f59SWei Liu V9fsPath *fs_path, V9fsFidOpenState *fs) 459f00d4f59SWei Liu { 460996a0d76SGreg Kurz int dirfd; 46121328e1eSGreg Kurz DIR *stream; 462f00d4f59SWei Liu 463996a0d76SGreg Kurz dirfd = local_opendir_nofollow(ctx, fs_path->data); 464996a0d76SGreg Kurz if (dirfd == -1) { 465996a0d76SGreg Kurz return -1; 466996a0d76SGreg Kurz } 467996a0d76SGreg Kurz 468996a0d76SGreg Kurz stream = fdopendir(dirfd); 46921328e1eSGreg Kurz if (!stream) { 470f00d4f59SWei Liu return -1; 471f00d4f59SWei Liu } 47221328e1eSGreg Kurz fs->dir.stream = stream; 473f00d4f59SWei Liu return 0; 474f00d4f59SWei Liu } 475f00d4f59SWei Liu 476f00d4f59SWei Liu static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs) 477f00d4f59SWei Liu { 478f314ea4eSGreg Kurz rewinddir(fs->dir.stream); 479f00d4f59SWei Liu } 480f00d4f59SWei Liu 481f00d4f59SWei Liu static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs) 482f00d4f59SWei Liu { 483f314ea4eSGreg Kurz return telldir(fs->dir.stream); 484f00d4f59SWei Liu } 485f00d4f59SWei Liu 486635324e8SGreg Kurz static struct dirent *local_readdir(FsContext *ctx, V9fsFidOpenState *fs) 487f00d4f59SWei Liu { 488635324e8SGreg Kurz struct dirent *entry; 489f00d4f59SWei Liu 490f00d4f59SWei Liu again: 491635324e8SGreg Kurz entry = readdir(fs->dir.stream); 492635324e8SGreg Kurz if (!entry) { 493635324e8SGreg Kurz return NULL; 494635324e8SGreg Kurz } 495635324e8SGreg Kurz 496f00d4f59SWei Liu if (ctx->export_flags & V9FS_SM_MAPPED) { 497f00d4f59SWei Liu entry->d_type = DT_UNKNOWN; 498f00d4f59SWei Liu } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 499635324e8SGreg Kurz if (!strcmp(entry->d_name, VIRTFS_META_DIR)) { 500f00d4f59SWei Liu /* skp the meta data directory */ 501f00d4f59SWei Liu goto again; 502f00d4f59SWei Liu } 503f00d4f59SWei Liu entry->d_type = DT_UNKNOWN; 504f00d4f59SWei Liu } 505635324e8SGreg Kurz 506635324e8SGreg Kurz return entry; 507f00d4f59SWei Liu } 508f00d4f59SWei Liu 509f00d4f59SWei Liu static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off) 510f00d4f59SWei Liu { 511f314ea4eSGreg Kurz seekdir(fs->dir.stream, off); 512f00d4f59SWei Liu } 513f00d4f59SWei Liu 514f00d4f59SWei Liu static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs, 515f00d4f59SWei Liu const struct iovec *iov, 516f00d4f59SWei Liu int iovcnt, off_t offset) 517f00d4f59SWei Liu { 518f00d4f59SWei Liu #ifdef CONFIG_PREADV 519f00d4f59SWei Liu return preadv(fs->fd, iov, iovcnt, offset); 520f00d4f59SWei Liu #else 521f00d4f59SWei Liu int err = lseek(fs->fd, offset, SEEK_SET); 522f00d4f59SWei Liu if (err == -1) { 523f00d4f59SWei Liu return err; 524f00d4f59SWei Liu } else { 525f00d4f59SWei Liu return readv(fs->fd, iov, iovcnt); 526f00d4f59SWei Liu } 527f00d4f59SWei Liu #endif 528f00d4f59SWei Liu } 529f00d4f59SWei Liu 530f00d4f59SWei Liu static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs, 531f00d4f59SWei Liu const struct iovec *iov, 532f00d4f59SWei Liu int iovcnt, off_t offset) 533f00d4f59SWei Liu { 5346fe76accSGreg Kurz ssize_t ret; 535f00d4f59SWei Liu #ifdef CONFIG_PREADV 536f00d4f59SWei Liu ret = pwritev(fs->fd, iov, iovcnt, offset); 537f00d4f59SWei Liu #else 538f00d4f59SWei Liu int err = lseek(fs->fd, offset, SEEK_SET); 539f00d4f59SWei Liu if (err == -1) { 540f00d4f59SWei Liu return err; 541f00d4f59SWei Liu } else { 542f00d4f59SWei Liu ret = writev(fs->fd, iov, iovcnt); 543f00d4f59SWei Liu } 544f00d4f59SWei Liu #endif 545f00d4f59SWei Liu #ifdef CONFIG_SYNC_FILE_RANGE 546f00d4f59SWei Liu if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) { 547f00d4f59SWei Liu /* 548f00d4f59SWei Liu * Initiate a writeback. This is not a data integrity sync. 549f00d4f59SWei Liu * We want to ensure that we don't leave dirty pages in the cache 550f00d4f59SWei Liu * after write when writeout=immediate is sepcified. 551f00d4f59SWei Liu */ 552f00d4f59SWei Liu sync_file_range(fs->fd, offset, ret, 553f00d4f59SWei Liu SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE); 554f00d4f59SWei Liu } 555f00d4f59SWei Liu #endif 556f00d4f59SWei Liu return ret; 557f00d4f59SWei Liu } 558f00d4f59SWei Liu 559f00d4f59SWei Liu static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp) 560f00d4f59SWei Liu { 561f00d4f59SWei Liu char *buffer; 562f00d4f59SWei Liu int ret = -1; 563f00d4f59SWei Liu char *path = fs_path->data; 564f00d4f59SWei Liu 565f00d4f59SWei Liu if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 566f00d4f59SWei Liu buffer = rpath(fs_ctx, path); 567f00d4f59SWei Liu ret = local_set_xattr(buffer, credp); 568f00d4f59SWei Liu g_free(buffer); 569f00d4f59SWei Liu } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 570f00d4f59SWei Liu return local_set_mapped_file_attr(fs_ctx, path, credp); 571f00d4f59SWei Liu } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 572f00d4f59SWei Liu (fs_ctx->export_flags & V9FS_SM_NONE)) { 573f00d4f59SWei Liu buffer = rpath(fs_ctx, path); 574f00d4f59SWei Liu ret = chmod(buffer, credp->fc_mode); 575f00d4f59SWei Liu g_free(buffer); 576f00d4f59SWei Liu } 577f00d4f59SWei Liu return ret; 578f00d4f59SWei Liu } 579f00d4f59SWei Liu 580f00d4f59SWei Liu static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path, 581f00d4f59SWei Liu const char *name, FsCred *credp) 582f00d4f59SWei Liu { 583f00d4f59SWei Liu char *path; 584f00d4f59SWei Liu int err = -1; 585f00d4f59SWei Liu int serrno = 0; 586f00d4f59SWei Liu V9fsString fullname; 587f00d4f59SWei Liu char *buffer = NULL; 588f00d4f59SWei Liu 589f00d4f59SWei Liu v9fs_string_init(&fullname); 590f00d4f59SWei Liu v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name); 591f00d4f59SWei Liu path = fullname.data; 592f00d4f59SWei Liu 593f00d4f59SWei Liu /* Determine the security model */ 594f00d4f59SWei Liu if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 595f00d4f59SWei Liu buffer = rpath(fs_ctx, path); 596f00d4f59SWei Liu err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0); 597f00d4f59SWei Liu if (err == -1) { 598f00d4f59SWei Liu goto out; 599f00d4f59SWei Liu } 600f00d4f59SWei Liu err = local_set_xattr(buffer, credp); 601f00d4f59SWei Liu if (err == -1) { 602f00d4f59SWei Liu serrno = errno; 603f00d4f59SWei Liu goto err_end; 604f00d4f59SWei Liu } 605f00d4f59SWei Liu } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 606f00d4f59SWei Liu 607f00d4f59SWei Liu buffer = rpath(fs_ctx, path); 608f00d4f59SWei Liu err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0); 609f00d4f59SWei Liu if (err == -1) { 610f00d4f59SWei Liu goto out; 611f00d4f59SWei Liu } 612f00d4f59SWei Liu err = local_set_mapped_file_attr(fs_ctx, path, credp); 613f00d4f59SWei Liu if (err == -1) { 614f00d4f59SWei Liu serrno = errno; 615f00d4f59SWei Liu goto err_end; 616f00d4f59SWei Liu } 617f00d4f59SWei Liu } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 618f00d4f59SWei Liu (fs_ctx->export_flags & V9FS_SM_NONE)) { 619f00d4f59SWei Liu buffer = rpath(fs_ctx, path); 620f00d4f59SWei Liu err = mknod(buffer, credp->fc_mode, credp->fc_rdev); 621f00d4f59SWei Liu if (err == -1) { 622f00d4f59SWei Liu goto out; 623f00d4f59SWei Liu } 624f00d4f59SWei Liu err = local_post_create_passthrough(fs_ctx, path, credp); 625f00d4f59SWei Liu if (err == -1) { 626f00d4f59SWei Liu serrno = errno; 627f00d4f59SWei Liu goto err_end; 628f00d4f59SWei Liu } 629f00d4f59SWei Liu } 630f00d4f59SWei Liu goto out; 631f00d4f59SWei Liu 632f00d4f59SWei Liu err_end: 633f00d4f59SWei Liu remove(buffer); 634f00d4f59SWei Liu errno = serrno; 635f00d4f59SWei Liu out: 636f00d4f59SWei Liu g_free(buffer); 637f00d4f59SWei Liu v9fs_string_free(&fullname); 638f00d4f59SWei Liu return err; 639f00d4f59SWei Liu } 640f00d4f59SWei Liu 641f00d4f59SWei Liu static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path, 642f00d4f59SWei Liu const char *name, FsCred *credp) 643f00d4f59SWei Liu { 644f00d4f59SWei Liu char *path; 645f00d4f59SWei Liu int err = -1; 646f00d4f59SWei Liu int serrno = 0; 647f00d4f59SWei Liu V9fsString fullname; 648f00d4f59SWei Liu char *buffer = NULL; 649f00d4f59SWei Liu 650f00d4f59SWei Liu v9fs_string_init(&fullname); 651f00d4f59SWei Liu v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name); 652f00d4f59SWei Liu path = fullname.data; 653f00d4f59SWei Liu 654f00d4f59SWei Liu /* Determine the security model */ 655f00d4f59SWei Liu if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 656f00d4f59SWei Liu buffer = rpath(fs_ctx, path); 657f00d4f59SWei Liu err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS); 658f00d4f59SWei Liu if (err == -1) { 659f00d4f59SWei Liu goto out; 660f00d4f59SWei Liu } 661f00d4f59SWei Liu credp->fc_mode = credp->fc_mode|S_IFDIR; 662f00d4f59SWei Liu err = local_set_xattr(buffer, credp); 663f00d4f59SWei Liu if (err == -1) { 664f00d4f59SWei Liu serrno = errno; 665f00d4f59SWei Liu goto err_end; 666f00d4f59SWei Liu } 667f00d4f59SWei Liu } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 668f00d4f59SWei Liu buffer = rpath(fs_ctx, path); 669f00d4f59SWei Liu err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS); 670f00d4f59SWei Liu if (err == -1) { 671f00d4f59SWei Liu goto out; 672f00d4f59SWei Liu } 673f00d4f59SWei Liu credp->fc_mode = credp->fc_mode|S_IFDIR; 674f00d4f59SWei Liu err = local_set_mapped_file_attr(fs_ctx, path, credp); 675f00d4f59SWei Liu if (err == -1) { 676f00d4f59SWei Liu serrno = errno; 677f00d4f59SWei Liu goto err_end; 678f00d4f59SWei Liu } 679f00d4f59SWei Liu } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 680f00d4f59SWei Liu (fs_ctx->export_flags & V9FS_SM_NONE)) { 681f00d4f59SWei Liu buffer = rpath(fs_ctx, path); 682f00d4f59SWei Liu err = mkdir(buffer, credp->fc_mode); 683f00d4f59SWei Liu if (err == -1) { 684f00d4f59SWei Liu goto out; 685f00d4f59SWei Liu } 686f00d4f59SWei Liu err = local_post_create_passthrough(fs_ctx, path, credp); 687f00d4f59SWei Liu if (err == -1) { 688f00d4f59SWei Liu serrno = errno; 689f00d4f59SWei Liu goto err_end; 690f00d4f59SWei Liu } 691f00d4f59SWei Liu } 692f00d4f59SWei Liu goto out; 693f00d4f59SWei Liu 694f00d4f59SWei Liu err_end: 695f00d4f59SWei Liu remove(buffer); 696f00d4f59SWei Liu errno = serrno; 697f00d4f59SWei Liu out: 698f00d4f59SWei Liu g_free(buffer); 699f00d4f59SWei Liu v9fs_string_free(&fullname); 700f00d4f59SWei Liu return err; 701f00d4f59SWei Liu } 702f00d4f59SWei Liu 703f00d4f59SWei Liu static int local_fstat(FsContext *fs_ctx, int fid_type, 704f00d4f59SWei Liu V9fsFidOpenState *fs, struct stat *stbuf) 705f00d4f59SWei Liu { 706f00d4f59SWei Liu int err, fd; 707f00d4f59SWei Liu 708f00d4f59SWei Liu if (fid_type == P9_FID_DIR) { 709f314ea4eSGreg Kurz fd = dirfd(fs->dir.stream); 710f00d4f59SWei Liu } else { 711f00d4f59SWei Liu fd = fs->fd; 712f00d4f59SWei Liu } 713f00d4f59SWei Liu 714f00d4f59SWei Liu err = fstat(fd, stbuf); 715f00d4f59SWei Liu if (err) { 716f00d4f59SWei Liu return err; 717f00d4f59SWei Liu } 718f00d4f59SWei Liu if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 719f00d4f59SWei Liu /* Actual credentials are part of extended attrs */ 720f00d4f59SWei Liu uid_t tmp_uid; 721f00d4f59SWei Liu gid_t tmp_gid; 722f00d4f59SWei Liu mode_t tmp_mode; 723f00d4f59SWei Liu dev_t tmp_dev; 724f00d4f59SWei Liu 725f00d4f59SWei Liu if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) { 726f00d4f59SWei Liu stbuf->st_uid = le32_to_cpu(tmp_uid); 727f00d4f59SWei Liu } 728f00d4f59SWei Liu if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) { 729f00d4f59SWei Liu stbuf->st_gid = le32_to_cpu(tmp_gid); 730f00d4f59SWei Liu } 731f00d4f59SWei Liu if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) { 732f00d4f59SWei Liu stbuf->st_mode = le32_to_cpu(tmp_mode); 733f00d4f59SWei Liu } 734f00d4f59SWei Liu if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) { 735f00d4f59SWei Liu stbuf->st_rdev = le64_to_cpu(tmp_dev); 736f00d4f59SWei Liu } 737f00d4f59SWei Liu } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 738f00d4f59SWei Liu errno = EOPNOTSUPP; 739f00d4f59SWei Liu return -1; 740f00d4f59SWei Liu } 741f00d4f59SWei Liu return err; 742f00d4f59SWei Liu } 743f00d4f59SWei Liu 744f00d4f59SWei Liu static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name, 745f00d4f59SWei Liu int flags, FsCred *credp, V9fsFidOpenState *fs) 746f00d4f59SWei Liu { 747f00d4f59SWei Liu char *path; 748f00d4f59SWei Liu int fd = -1; 749f00d4f59SWei Liu int err = -1; 750f00d4f59SWei Liu int serrno = 0; 751f00d4f59SWei Liu V9fsString fullname; 752f00d4f59SWei Liu char *buffer = NULL; 753f00d4f59SWei Liu 754f00d4f59SWei Liu /* 755f00d4f59SWei Liu * Mark all the open to not follow symlinks 756f00d4f59SWei Liu */ 757f00d4f59SWei Liu flags |= O_NOFOLLOW; 758f00d4f59SWei Liu 759f00d4f59SWei Liu v9fs_string_init(&fullname); 760f00d4f59SWei Liu v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name); 761f00d4f59SWei Liu path = fullname.data; 762f00d4f59SWei Liu 763f00d4f59SWei Liu /* Determine the security model */ 764f00d4f59SWei Liu if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 765f00d4f59SWei Liu buffer = rpath(fs_ctx, path); 766f00d4f59SWei Liu fd = open(buffer, flags, SM_LOCAL_MODE_BITS); 767f00d4f59SWei Liu if (fd == -1) { 768f00d4f59SWei Liu err = fd; 769f00d4f59SWei Liu goto out; 770f00d4f59SWei Liu } 771f00d4f59SWei Liu credp->fc_mode = credp->fc_mode|S_IFREG; 772f00d4f59SWei Liu /* Set cleint credentials in xattr */ 773f00d4f59SWei Liu err = local_set_xattr(buffer, credp); 774f00d4f59SWei Liu if (err == -1) { 775f00d4f59SWei Liu serrno = errno; 776f00d4f59SWei Liu goto err_end; 777f00d4f59SWei Liu } 778f00d4f59SWei Liu } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 779f00d4f59SWei Liu buffer = rpath(fs_ctx, path); 780f00d4f59SWei Liu fd = open(buffer, flags, SM_LOCAL_MODE_BITS); 781f00d4f59SWei Liu if (fd == -1) { 782f00d4f59SWei Liu err = fd; 783f00d4f59SWei Liu goto out; 784f00d4f59SWei Liu } 785f00d4f59SWei Liu credp->fc_mode = credp->fc_mode|S_IFREG; 786f00d4f59SWei Liu /* Set client credentials in .virtfs_metadata directory files */ 787f00d4f59SWei Liu err = local_set_mapped_file_attr(fs_ctx, path, credp); 788f00d4f59SWei Liu if (err == -1) { 789f00d4f59SWei Liu serrno = errno; 790f00d4f59SWei Liu goto err_end; 791f00d4f59SWei Liu } 792f00d4f59SWei Liu } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 793f00d4f59SWei Liu (fs_ctx->export_flags & V9FS_SM_NONE)) { 794f00d4f59SWei Liu buffer = rpath(fs_ctx, path); 795f00d4f59SWei Liu fd = open(buffer, flags, credp->fc_mode); 796f00d4f59SWei Liu if (fd == -1) { 797f00d4f59SWei Liu err = fd; 798f00d4f59SWei Liu goto out; 799f00d4f59SWei Liu } 800f00d4f59SWei Liu err = local_post_create_passthrough(fs_ctx, path, credp); 801f00d4f59SWei Liu if (err == -1) { 802f00d4f59SWei Liu serrno = errno; 803f00d4f59SWei Liu goto err_end; 804f00d4f59SWei Liu } 805f00d4f59SWei Liu } 806f00d4f59SWei Liu err = fd; 807f00d4f59SWei Liu fs->fd = fd; 808f00d4f59SWei Liu goto out; 809f00d4f59SWei Liu 810f00d4f59SWei Liu err_end: 811f00d4f59SWei Liu close(fd); 812f00d4f59SWei Liu remove(buffer); 813f00d4f59SWei Liu errno = serrno; 814f00d4f59SWei Liu out: 815f00d4f59SWei Liu g_free(buffer); 816f00d4f59SWei Liu v9fs_string_free(&fullname); 817f00d4f59SWei Liu return err; 818f00d4f59SWei Liu } 819f00d4f59SWei Liu 820f00d4f59SWei Liu 821f00d4f59SWei Liu static int local_symlink(FsContext *fs_ctx, const char *oldpath, 822f00d4f59SWei Liu V9fsPath *dir_path, const char *name, FsCred *credp) 823f00d4f59SWei Liu { 824f00d4f59SWei Liu int err = -1; 825f00d4f59SWei Liu int serrno = 0; 826f00d4f59SWei Liu char *newpath; 827f00d4f59SWei Liu V9fsString fullname; 828f00d4f59SWei Liu char *buffer = NULL; 829f00d4f59SWei Liu 830f00d4f59SWei Liu v9fs_string_init(&fullname); 831f00d4f59SWei Liu v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name); 832f00d4f59SWei Liu newpath = fullname.data; 833f00d4f59SWei Liu 834f00d4f59SWei Liu /* Determine the security model */ 835f00d4f59SWei Liu if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 836f00d4f59SWei Liu int fd; 837f00d4f59SWei Liu ssize_t oldpath_size, write_size; 838f00d4f59SWei Liu buffer = rpath(fs_ctx, newpath); 839f00d4f59SWei Liu fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS); 840f00d4f59SWei Liu if (fd == -1) { 841f00d4f59SWei Liu err = fd; 842f00d4f59SWei Liu goto out; 843f00d4f59SWei Liu } 844f00d4f59SWei Liu /* Write the oldpath (target) to the file. */ 845f00d4f59SWei Liu oldpath_size = strlen(oldpath); 846f00d4f59SWei Liu do { 847f00d4f59SWei Liu write_size = write(fd, (void *)oldpath, oldpath_size); 848f00d4f59SWei Liu } while (write_size == -1 && errno == EINTR); 849f00d4f59SWei Liu 850f00d4f59SWei Liu if (write_size != oldpath_size) { 851f00d4f59SWei Liu serrno = errno; 852f00d4f59SWei Liu close(fd); 853f00d4f59SWei Liu err = -1; 854f00d4f59SWei Liu goto err_end; 855f00d4f59SWei Liu } 856f00d4f59SWei Liu close(fd); 857f00d4f59SWei Liu /* Set cleint credentials in symlink's xattr */ 858f00d4f59SWei Liu credp->fc_mode = credp->fc_mode|S_IFLNK; 859f00d4f59SWei Liu err = local_set_xattr(buffer, credp); 860f00d4f59SWei Liu if (err == -1) { 861f00d4f59SWei Liu serrno = errno; 862f00d4f59SWei Liu goto err_end; 863f00d4f59SWei Liu } 864f00d4f59SWei Liu } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 865f00d4f59SWei Liu int fd; 866f00d4f59SWei Liu ssize_t oldpath_size, write_size; 867f00d4f59SWei Liu buffer = rpath(fs_ctx, newpath); 868f00d4f59SWei Liu fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS); 869f00d4f59SWei Liu if (fd == -1) { 870f00d4f59SWei Liu err = fd; 871f00d4f59SWei Liu goto out; 872f00d4f59SWei Liu } 873f00d4f59SWei Liu /* Write the oldpath (target) to the file. */ 874f00d4f59SWei Liu oldpath_size = strlen(oldpath); 875f00d4f59SWei Liu do { 876f00d4f59SWei Liu write_size = write(fd, (void *)oldpath, oldpath_size); 877f00d4f59SWei Liu } while (write_size == -1 && errno == EINTR); 878f00d4f59SWei Liu 879f00d4f59SWei Liu if (write_size != oldpath_size) { 880f00d4f59SWei Liu serrno = errno; 881f00d4f59SWei Liu close(fd); 882f00d4f59SWei Liu err = -1; 883f00d4f59SWei Liu goto err_end; 884f00d4f59SWei Liu } 885f00d4f59SWei Liu close(fd); 886f00d4f59SWei Liu /* Set cleint credentials in symlink's xattr */ 887f00d4f59SWei Liu credp->fc_mode = credp->fc_mode|S_IFLNK; 888f00d4f59SWei Liu err = local_set_mapped_file_attr(fs_ctx, newpath, credp); 889f00d4f59SWei Liu if (err == -1) { 890f00d4f59SWei Liu serrno = errno; 891f00d4f59SWei Liu goto err_end; 892f00d4f59SWei Liu } 893f00d4f59SWei Liu } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 894f00d4f59SWei Liu (fs_ctx->export_flags & V9FS_SM_NONE)) { 895f00d4f59SWei Liu buffer = rpath(fs_ctx, newpath); 896f00d4f59SWei Liu err = symlink(oldpath, buffer); 897f00d4f59SWei Liu if (err) { 898f00d4f59SWei Liu goto out; 899f00d4f59SWei Liu } 900f00d4f59SWei Liu err = lchown(buffer, credp->fc_uid, credp->fc_gid); 901f00d4f59SWei Liu if (err == -1) { 902f00d4f59SWei Liu /* 903f00d4f59SWei Liu * If we fail to change ownership and if we are 904f00d4f59SWei Liu * using security model none. Ignore the error 905f00d4f59SWei Liu */ 906f00d4f59SWei Liu if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) { 907f00d4f59SWei Liu serrno = errno; 908f00d4f59SWei Liu goto err_end; 909f00d4f59SWei Liu } else 910f00d4f59SWei Liu err = 0; 911f00d4f59SWei Liu } 912f00d4f59SWei Liu } 913f00d4f59SWei Liu goto out; 914f00d4f59SWei Liu 915f00d4f59SWei Liu err_end: 916f00d4f59SWei Liu remove(buffer); 917f00d4f59SWei Liu errno = serrno; 918f00d4f59SWei Liu out: 919f00d4f59SWei Liu g_free(buffer); 920f00d4f59SWei Liu v9fs_string_free(&fullname); 921f00d4f59SWei Liu return err; 922f00d4f59SWei Liu } 923f00d4f59SWei Liu 924f00d4f59SWei Liu static int local_link(FsContext *ctx, V9fsPath *oldpath, 925f00d4f59SWei Liu V9fsPath *dirpath, const char *name) 926f00d4f59SWei Liu { 927*ad0b46e6SGreg Kurz char *odirpath = g_path_get_dirname(oldpath->data); 928*ad0b46e6SGreg Kurz char *oname = g_path_get_basename(oldpath->data); 929*ad0b46e6SGreg Kurz int ret = -1; 930*ad0b46e6SGreg Kurz int odirfd, ndirfd; 931f00d4f59SWei Liu 932*ad0b46e6SGreg Kurz odirfd = local_opendir_nofollow(ctx, odirpath); 933*ad0b46e6SGreg Kurz if (odirfd == -1) { 9346dd4b1f1SGreg Kurz goto out; 9356dd4b1f1SGreg Kurz } 936f00d4f59SWei Liu 937*ad0b46e6SGreg Kurz ndirfd = local_opendir_nofollow(ctx, dirpath->data); 938*ad0b46e6SGreg Kurz if (ndirfd == -1) { 939*ad0b46e6SGreg Kurz close_preserve_errno(odirfd); 940*ad0b46e6SGreg Kurz goto out; 941*ad0b46e6SGreg Kurz } 942*ad0b46e6SGreg Kurz 943*ad0b46e6SGreg Kurz ret = linkat(odirfd, oname, ndirfd, name, 0); 944*ad0b46e6SGreg Kurz if (ret < 0) { 945*ad0b46e6SGreg Kurz goto out_close; 946*ad0b46e6SGreg Kurz } 947*ad0b46e6SGreg Kurz 948f00d4f59SWei Liu /* now link the virtfs_metadata files */ 9496dd4b1f1SGreg Kurz if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 950*ad0b46e6SGreg Kurz int omap_dirfd, nmap_dirfd; 9516dd4b1f1SGreg Kurz 952*ad0b46e6SGreg Kurz ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700); 953*ad0b46e6SGreg Kurz if (ret < 0 && errno != EEXIST) { 954*ad0b46e6SGreg Kurz goto err_undo_link; 955f00d4f59SWei Liu } 956*ad0b46e6SGreg Kurz 957*ad0b46e6SGreg Kurz omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR); 958*ad0b46e6SGreg Kurz if (omap_dirfd == -1) { 959*ad0b46e6SGreg Kurz goto err; 960*ad0b46e6SGreg Kurz } 961*ad0b46e6SGreg Kurz 962*ad0b46e6SGreg Kurz nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR); 963*ad0b46e6SGreg Kurz if (nmap_dirfd == -1) { 964*ad0b46e6SGreg Kurz close_preserve_errno(omap_dirfd); 965*ad0b46e6SGreg Kurz goto err; 966*ad0b46e6SGreg Kurz } 967*ad0b46e6SGreg Kurz 968*ad0b46e6SGreg Kurz ret = linkat(omap_dirfd, oname, nmap_dirfd, name, 0); 969*ad0b46e6SGreg Kurz close_preserve_errno(nmap_dirfd); 970*ad0b46e6SGreg Kurz close_preserve_errno(omap_dirfd); 971f00d4f59SWei Liu if (ret < 0 && errno != ENOENT) { 972*ad0b46e6SGreg Kurz goto err_undo_link; 973f00d4f59SWei Liu } 9746dd4b1f1SGreg Kurz 975*ad0b46e6SGreg Kurz ret = 0; 976*ad0b46e6SGreg Kurz } 977*ad0b46e6SGreg Kurz goto out_close; 978*ad0b46e6SGreg Kurz 979*ad0b46e6SGreg Kurz err: 980*ad0b46e6SGreg Kurz ret = -1; 981*ad0b46e6SGreg Kurz err_undo_link: 982*ad0b46e6SGreg Kurz unlinkat_preserve_errno(ndirfd, name, 0); 983*ad0b46e6SGreg Kurz out_close: 984*ad0b46e6SGreg Kurz close_preserve_errno(ndirfd); 985*ad0b46e6SGreg Kurz close_preserve_errno(odirfd); 9866dd4b1f1SGreg Kurz out: 987*ad0b46e6SGreg Kurz g_free(oname); 988*ad0b46e6SGreg Kurz g_free(odirpath); 989f00d4f59SWei Liu return ret; 990f00d4f59SWei Liu } 991f00d4f59SWei Liu 992f00d4f59SWei Liu static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size) 993f00d4f59SWei Liu { 994ac125d99SGreg Kurz int fd, ret; 995f00d4f59SWei Liu 996ac125d99SGreg Kurz fd = local_open_nofollow(ctx, fs_path->data, O_WRONLY, 0); 997ac125d99SGreg Kurz if (fd == -1) { 998ac125d99SGreg Kurz return -1; 999ac125d99SGreg Kurz } 1000ac125d99SGreg Kurz ret = ftruncate(fd, size); 1001ac125d99SGreg Kurz close_preserve_errno(fd); 1002f00d4f59SWei Liu return ret; 1003f00d4f59SWei Liu } 1004f00d4f59SWei Liu 1005f00d4f59SWei Liu static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp) 1006f00d4f59SWei Liu { 1007f00d4f59SWei Liu char *buffer; 1008f00d4f59SWei Liu int ret = -1; 1009f00d4f59SWei Liu char *path = fs_path->data; 1010f00d4f59SWei Liu 1011f00d4f59SWei Liu if ((credp->fc_uid == -1 && credp->fc_gid == -1) || 1012f00d4f59SWei Liu (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 1013f00d4f59SWei Liu (fs_ctx->export_flags & V9FS_SM_NONE)) { 1014f00d4f59SWei Liu buffer = rpath(fs_ctx, path); 1015f00d4f59SWei Liu ret = lchown(buffer, credp->fc_uid, credp->fc_gid); 1016f00d4f59SWei Liu g_free(buffer); 1017f00d4f59SWei Liu } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 1018f00d4f59SWei Liu buffer = rpath(fs_ctx, path); 1019f00d4f59SWei Liu ret = local_set_xattr(buffer, credp); 1020f00d4f59SWei Liu g_free(buffer); 1021f00d4f59SWei Liu } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 1022f00d4f59SWei Liu return local_set_mapped_file_attr(fs_ctx, path, credp); 1023f00d4f59SWei Liu } 1024f00d4f59SWei Liu return ret; 1025f00d4f59SWei Liu } 1026f00d4f59SWei Liu 1027f00d4f59SWei Liu static int local_utimensat(FsContext *s, V9fsPath *fs_path, 1028f00d4f59SWei Liu const struct timespec *buf) 1029f00d4f59SWei Liu { 1030a33eda0dSGreg Kurz char *dirpath = g_path_get_dirname(fs_path->data); 1031a33eda0dSGreg Kurz char *name = g_path_get_basename(fs_path->data); 1032a33eda0dSGreg Kurz int dirfd, ret = -1; 1033f00d4f59SWei Liu 1034a33eda0dSGreg Kurz dirfd = local_opendir_nofollow(s, dirpath); 1035a33eda0dSGreg Kurz if (dirfd == -1) { 1036a33eda0dSGreg Kurz goto out; 1037a33eda0dSGreg Kurz } 1038a33eda0dSGreg Kurz 1039a33eda0dSGreg Kurz ret = utimensat(dirfd, name, buf, AT_SYMLINK_NOFOLLOW); 1040a33eda0dSGreg Kurz close_preserve_errno(dirfd); 1041a33eda0dSGreg Kurz out: 1042a33eda0dSGreg Kurz g_free(dirpath); 1043a33eda0dSGreg Kurz g_free(name); 1044f00d4f59SWei Liu return ret; 1045f00d4f59SWei Liu } 1046f00d4f59SWei Liu 1047df4938a6SGreg Kurz static int local_unlinkat_common(FsContext *ctx, int dirfd, const char *name, 1048df4938a6SGreg Kurz int flags) 1049df4938a6SGreg Kurz { 1050df4938a6SGreg Kurz int ret = -1; 1051df4938a6SGreg Kurz 1052df4938a6SGreg Kurz if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 1053df4938a6SGreg Kurz int map_dirfd; 1054df4938a6SGreg Kurz 1055df4938a6SGreg Kurz if (flags == AT_REMOVEDIR) { 1056df4938a6SGreg Kurz int fd; 1057df4938a6SGreg Kurz 1058df4938a6SGreg Kurz fd = openat(dirfd, name, O_RDONLY | O_DIRECTORY | O_PATH); 1059df4938a6SGreg Kurz if (fd == -1) { 1060df4938a6SGreg Kurz goto err_out; 1061df4938a6SGreg Kurz } 1062df4938a6SGreg Kurz /* 1063df4938a6SGreg Kurz * If directory remove .virtfs_metadata contained in the 1064df4938a6SGreg Kurz * directory 1065df4938a6SGreg Kurz */ 1066df4938a6SGreg Kurz ret = unlinkat(fd, VIRTFS_META_DIR, AT_REMOVEDIR); 1067df4938a6SGreg Kurz close_preserve_errno(fd); 1068df4938a6SGreg Kurz if (ret < 0 && errno != ENOENT) { 1069df4938a6SGreg Kurz /* 1070df4938a6SGreg Kurz * We didn't had the .virtfs_metadata file. May be file created 1071df4938a6SGreg Kurz * in non-mapped mode ?. Ignore ENOENT. 1072df4938a6SGreg Kurz */ 1073df4938a6SGreg Kurz goto err_out; 1074df4938a6SGreg Kurz } 1075df4938a6SGreg Kurz } 1076df4938a6SGreg Kurz /* 1077df4938a6SGreg Kurz * Now remove the name from parent directory 1078df4938a6SGreg Kurz * .virtfs_metadata directory. 1079df4938a6SGreg Kurz */ 1080df4938a6SGreg Kurz map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR); 1081df4938a6SGreg Kurz ret = unlinkat(map_dirfd, name, 0); 1082df4938a6SGreg Kurz close_preserve_errno(map_dirfd); 1083df4938a6SGreg Kurz if (ret < 0 && errno != ENOENT) { 1084df4938a6SGreg Kurz /* 1085df4938a6SGreg Kurz * We didn't had the .virtfs_metadata file. May be file created 1086df4938a6SGreg Kurz * in non-mapped mode ?. Ignore ENOENT. 1087df4938a6SGreg Kurz */ 1088df4938a6SGreg Kurz goto err_out; 1089df4938a6SGreg Kurz } 1090df4938a6SGreg Kurz } 1091df4938a6SGreg Kurz 1092df4938a6SGreg Kurz ret = unlinkat(dirfd, name, flags); 1093df4938a6SGreg Kurz err_out: 1094df4938a6SGreg Kurz return ret; 1095df4938a6SGreg Kurz } 1096df4938a6SGreg Kurz 1097f00d4f59SWei Liu static int local_remove(FsContext *ctx, const char *path) 1098f00d4f59SWei Liu { 1099f00d4f59SWei Liu struct stat stbuf; 1100a0e640a8SGreg Kurz char *dirpath = g_path_get_dirname(path); 1101a0e640a8SGreg Kurz char *name = g_path_get_basename(path); 1102a0e640a8SGreg Kurz int flags = 0; 1103a0e640a8SGreg Kurz int dirfd; 1104a0e640a8SGreg Kurz int err = -1; 1105f00d4f59SWei Liu 1106a0e640a8SGreg Kurz dirfd = local_opendir_nofollow(ctx, dirpath); 1107a0e640a8SGreg Kurz if (dirfd) { 1108a0e640a8SGreg Kurz goto out; 1109a0e640a8SGreg Kurz } 1110a0e640a8SGreg Kurz 1111a0e640a8SGreg Kurz if (fstatat(dirfd, path, &stbuf, AT_SYMLINK_NOFOLLOW) < 0) { 1112f00d4f59SWei Liu goto err_out; 1113f00d4f59SWei Liu } 1114a0e640a8SGreg Kurz 1115f00d4f59SWei Liu if (S_ISDIR(stbuf.st_mode)) { 1116a0e640a8SGreg Kurz flags |= AT_REMOVEDIR; 1117f00d4f59SWei Liu } 1118f00d4f59SWei Liu 1119a0e640a8SGreg Kurz err = local_unlinkat_common(ctx, dirfd, name, flags); 1120f00d4f59SWei Liu err_out: 1121a0e640a8SGreg Kurz close_preserve_errno(dirfd); 1122a0e640a8SGreg Kurz out: 1123a0e640a8SGreg Kurz g_free(name); 1124a0e640a8SGreg Kurz g_free(dirpath); 1125f00d4f59SWei Liu return err; 1126f00d4f59SWei Liu } 1127f00d4f59SWei Liu 1128f00d4f59SWei Liu static int local_fsync(FsContext *ctx, int fid_type, 1129f00d4f59SWei Liu V9fsFidOpenState *fs, int datasync) 1130f00d4f59SWei Liu { 1131f00d4f59SWei Liu int fd; 1132f00d4f59SWei Liu 1133f00d4f59SWei Liu if (fid_type == P9_FID_DIR) { 1134f314ea4eSGreg Kurz fd = dirfd(fs->dir.stream); 1135f00d4f59SWei Liu } else { 1136f00d4f59SWei Liu fd = fs->fd; 1137f00d4f59SWei Liu } 1138f00d4f59SWei Liu 1139f00d4f59SWei Liu if (datasync) { 1140f00d4f59SWei Liu return qemu_fdatasync(fd); 1141f00d4f59SWei Liu } else { 1142f00d4f59SWei Liu return fsync(fd); 1143f00d4f59SWei Liu } 1144f00d4f59SWei Liu } 1145f00d4f59SWei Liu 1146f00d4f59SWei Liu static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf) 1147f00d4f59SWei Liu { 114831e51d1cSGreg Kurz int fd, ret; 1149f00d4f59SWei Liu 115031e51d1cSGreg Kurz fd = local_open_nofollow(s, fs_path->data, O_RDONLY, 0); 115131e51d1cSGreg Kurz ret = fstatfs(fd, stbuf); 115231e51d1cSGreg Kurz close_preserve_errno(fd); 1153f00d4f59SWei Liu return ret; 1154f00d4f59SWei Liu } 1155f00d4f59SWei Liu 1156f00d4f59SWei Liu static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path, 1157f00d4f59SWei Liu const char *name, void *value, size_t size) 1158f00d4f59SWei Liu { 1159f00d4f59SWei Liu char *path = fs_path->data; 1160f00d4f59SWei Liu 1161f00d4f59SWei Liu return v9fs_get_xattr(ctx, path, name, value, size); 1162f00d4f59SWei Liu } 1163f00d4f59SWei Liu 1164f00d4f59SWei Liu static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path, 1165f00d4f59SWei Liu void *value, size_t size) 1166f00d4f59SWei Liu { 1167f00d4f59SWei Liu char *path = fs_path->data; 1168f00d4f59SWei Liu 1169f00d4f59SWei Liu return v9fs_list_xattr(ctx, path, value, size); 1170f00d4f59SWei Liu } 1171f00d4f59SWei Liu 1172f00d4f59SWei Liu static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name, 1173f00d4f59SWei Liu void *value, size_t size, int flags) 1174f00d4f59SWei Liu { 1175f00d4f59SWei Liu char *path = fs_path->data; 1176f00d4f59SWei Liu 1177f00d4f59SWei Liu return v9fs_set_xattr(ctx, path, name, value, size, flags); 1178f00d4f59SWei Liu } 1179f00d4f59SWei Liu 1180f00d4f59SWei Liu static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path, 1181f00d4f59SWei Liu const char *name) 1182f00d4f59SWei Liu { 1183f00d4f59SWei Liu char *path = fs_path->data; 1184f00d4f59SWei Liu 1185f00d4f59SWei Liu return v9fs_remove_xattr(ctx, path, name); 1186f00d4f59SWei Liu } 1187f00d4f59SWei Liu 1188f00d4f59SWei Liu static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path, 1189f00d4f59SWei Liu const char *name, V9fsPath *target) 1190f00d4f59SWei Liu { 1191f00d4f59SWei Liu if (dir_path) { 1192e3e83f2eSGreg Kurz v9fs_path_sprintf(target, "%s/%s", dir_path->data, name); 1193f00d4f59SWei Liu } else { 1194e3e83f2eSGreg Kurz v9fs_path_sprintf(target, "%s", name); 1195f00d4f59SWei Liu } 1196f00d4f59SWei Liu return 0; 1197f00d4f59SWei Liu } 1198f00d4f59SWei Liu 1199f00d4f59SWei Liu static int local_renameat(FsContext *ctx, V9fsPath *olddir, 1200f00d4f59SWei Liu const char *old_name, V9fsPath *newdir, 1201f00d4f59SWei Liu const char *new_name) 1202f00d4f59SWei Liu { 1203f00d4f59SWei Liu int ret; 120499f2cf4bSGreg Kurz int odirfd, ndirfd; 1205f00d4f59SWei Liu 120699f2cf4bSGreg Kurz odirfd = local_opendir_nofollow(ctx, olddir->data); 120799f2cf4bSGreg Kurz if (odirfd == -1) { 120899f2cf4bSGreg Kurz return -1; 120999f2cf4bSGreg Kurz } 1210f00d4f59SWei Liu 121199f2cf4bSGreg Kurz ndirfd = local_opendir_nofollow(ctx, newdir->data); 121299f2cf4bSGreg Kurz if (ndirfd == -1) { 121399f2cf4bSGreg Kurz close_preserve_errno(odirfd); 121499f2cf4bSGreg Kurz return -1; 121599f2cf4bSGreg Kurz } 1216f00d4f59SWei Liu 121799f2cf4bSGreg Kurz ret = renameat(odirfd, old_name, ndirfd, new_name); 121899f2cf4bSGreg Kurz if (ret < 0) { 121999f2cf4bSGreg Kurz goto out; 122099f2cf4bSGreg Kurz } 122199f2cf4bSGreg Kurz 122299f2cf4bSGreg Kurz if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 122399f2cf4bSGreg Kurz int omap_dirfd, nmap_dirfd; 122499f2cf4bSGreg Kurz 122599f2cf4bSGreg Kurz ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700); 122699f2cf4bSGreg Kurz if (ret < 0 && errno != EEXIST) { 122799f2cf4bSGreg Kurz goto err_undo_rename; 122899f2cf4bSGreg Kurz } 122999f2cf4bSGreg Kurz 12306dd4b1f1SGreg Kurz omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR); 123199f2cf4bSGreg Kurz if (omap_dirfd == -1) { 123299f2cf4bSGreg Kurz goto err; 123399f2cf4bSGreg Kurz } 123499f2cf4bSGreg Kurz 12356dd4b1f1SGreg Kurz nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR); 123699f2cf4bSGreg Kurz if (nmap_dirfd == -1) { 123799f2cf4bSGreg Kurz close_preserve_errno(omap_dirfd); 123899f2cf4bSGreg Kurz goto err; 123999f2cf4bSGreg Kurz } 124099f2cf4bSGreg Kurz 124199f2cf4bSGreg Kurz /* rename the .virtfs_metadata files */ 124299f2cf4bSGreg Kurz ret = renameat(omap_dirfd, old_name, nmap_dirfd, new_name); 124399f2cf4bSGreg Kurz close_preserve_errno(nmap_dirfd); 124499f2cf4bSGreg Kurz close_preserve_errno(omap_dirfd); 124599f2cf4bSGreg Kurz if (ret < 0 && errno != ENOENT) { 124699f2cf4bSGreg Kurz goto err_undo_rename; 124799f2cf4bSGreg Kurz } 124899f2cf4bSGreg Kurz 124999f2cf4bSGreg Kurz ret = 0; 125099f2cf4bSGreg Kurz } 125199f2cf4bSGreg Kurz goto out; 125299f2cf4bSGreg Kurz 125399f2cf4bSGreg Kurz err: 125499f2cf4bSGreg Kurz ret = -1; 125599f2cf4bSGreg Kurz err_undo_rename: 125699f2cf4bSGreg Kurz renameat_preserve_errno(ndirfd, new_name, odirfd, old_name); 125799f2cf4bSGreg Kurz out: 125899f2cf4bSGreg Kurz close_preserve_errno(ndirfd); 125999f2cf4bSGreg Kurz close_preserve_errno(odirfd); 1260f00d4f59SWei Liu return ret; 1261f00d4f59SWei Liu } 1262f00d4f59SWei Liu 1263d2767edeSGreg Kurz static void v9fs_path_init_dirname(V9fsPath *path, const char *str) 1264d2767edeSGreg Kurz { 1265d2767edeSGreg Kurz path->data = g_path_get_dirname(str); 1266d2767edeSGreg Kurz path->size = strlen(path->data) + 1; 1267d2767edeSGreg Kurz } 1268d2767edeSGreg Kurz 1269d2767edeSGreg Kurz static int local_rename(FsContext *ctx, const char *oldpath, 1270d2767edeSGreg Kurz const char *newpath) 1271d2767edeSGreg Kurz { 1272d2767edeSGreg Kurz int err; 1273d2767edeSGreg Kurz char *oname = g_path_get_basename(oldpath); 1274d2767edeSGreg Kurz char *nname = g_path_get_basename(newpath); 1275d2767edeSGreg Kurz V9fsPath olddir, newdir; 1276d2767edeSGreg Kurz 1277d2767edeSGreg Kurz v9fs_path_init_dirname(&olddir, oldpath); 1278d2767edeSGreg Kurz v9fs_path_init_dirname(&newdir, newpath); 1279d2767edeSGreg Kurz 1280d2767edeSGreg Kurz err = local_renameat(ctx, &olddir, oname, &newdir, nname); 1281d2767edeSGreg Kurz 1282d2767edeSGreg Kurz v9fs_path_free(&newdir); 1283d2767edeSGreg Kurz v9fs_path_free(&olddir); 1284d2767edeSGreg Kurz g_free(nname); 1285d2767edeSGreg Kurz g_free(oname); 1286d2767edeSGreg Kurz 1287d2767edeSGreg Kurz return err; 1288d2767edeSGreg Kurz } 1289d2767edeSGreg Kurz 1290f00d4f59SWei Liu static int local_unlinkat(FsContext *ctx, V9fsPath *dir, 1291f00d4f59SWei Liu const char *name, int flags) 1292f00d4f59SWei Liu { 1293f00d4f59SWei Liu int ret; 1294df4938a6SGreg Kurz int dirfd; 1295f00d4f59SWei Liu 1296df4938a6SGreg Kurz dirfd = local_opendir_nofollow(ctx, dir->data); 1297df4938a6SGreg Kurz if (dirfd == -1) { 1298df4938a6SGreg Kurz return -1; 1299df4938a6SGreg Kurz } 1300f00d4f59SWei Liu 1301df4938a6SGreg Kurz ret = local_unlinkat_common(ctx, dirfd, name, flags); 1302df4938a6SGreg Kurz close_preserve_errno(dirfd); 1303f00d4f59SWei Liu return ret; 1304f00d4f59SWei Liu } 1305f00d4f59SWei Liu 1306f00d4f59SWei Liu static int local_ioc_getversion(FsContext *ctx, V9fsPath *path, 1307f00d4f59SWei Liu mode_t st_mode, uint64_t *st_gen) 1308f00d4f59SWei Liu { 1309f00d4f59SWei Liu #ifdef FS_IOC_GETVERSION 1310f00d4f59SWei Liu int err; 1311f00d4f59SWei Liu V9fsFidOpenState fid_open; 1312f00d4f59SWei Liu 1313f00d4f59SWei Liu /* 1314f00d4f59SWei Liu * Do not try to open special files like device nodes, fifos etc 1315f00d4f59SWei Liu * We can get fd for regular files and directories only 1316f00d4f59SWei Liu */ 1317f00d4f59SWei Liu if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) { 1318f00d4f59SWei Liu errno = ENOTTY; 1319f00d4f59SWei Liu return -1; 1320f00d4f59SWei Liu } 1321f00d4f59SWei Liu err = local_open(ctx, path, O_RDONLY, &fid_open); 1322f00d4f59SWei Liu if (err < 0) { 1323f00d4f59SWei Liu return err; 1324f00d4f59SWei Liu } 1325f00d4f59SWei Liu err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen); 1326f00d4f59SWei Liu local_close(ctx, &fid_open); 1327f00d4f59SWei Liu return err; 1328f00d4f59SWei Liu #else 1329f00d4f59SWei Liu errno = ENOTTY; 1330f00d4f59SWei Liu return -1; 1331f00d4f59SWei Liu #endif 1332f00d4f59SWei Liu } 1333f00d4f59SWei Liu 1334f00d4f59SWei Liu static int local_init(FsContext *ctx) 1335f00d4f59SWei Liu { 1336f00d4f59SWei Liu struct statfs stbuf; 13370e35a378SGreg Kurz LocalData *data = g_malloc(sizeof(*data)); 13380e35a378SGreg Kurz 13390e35a378SGreg Kurz data->mountfd = open(ctx->fs_root, O_DIRECTORY | O_RDONLY); 13400e35a378SGreg Kurz if (data->mountfd == -1) { 13410e35a378SGreg Kurz goto err; 13420e35a378SGreg Kurz } 1343f00d4f59SWei Liu 134400c90bd1SGreg Kurz #ifdef FS_IOC_GETVERSION 134500c90bd1SGreg Kurz /* 134600c90bd1SGreg Kurz * use ioc_getversion only if the ioctl is definied 134700c90bd1SGreg Kurz */ 13480e35a378SGreg Kurz if (fstatfs(data->mountfd, &stbuf) < 0) { 13490e35a378SGreg Kurz close_preserve_errno(data->mountfd); 13500e35a378SGreg Kurz goto err; 135100c90bd1SGreg Kurz } 135200c90bd1SGreg Kurz switch (stbuf.f_type) { 135300c90bd1SGreg Kurz case EXT2_SUPER_MAGIC: 135400c90bd1SGreg Kurz case BTRFS_SUPER_MAGIC: 135500c90bd1SGreg Kurz case REISERFS_SUPER_MAGIC: 135600c90bd1SGreg Kurz case XFS_SUPER_MAGIC: 135700c90bd1SGreg Kurz ctx->exops.get_st_gen = local_ioc_getversion; 135800c90bd1SGreg Kurz break; 135900c90bd1SGreg Kurz } 136000c90bd1SGreg Kurz #endif 136100c90bd1SGreg Kurz 1362f00d4f59SWei Liu if (ctx->export_flags & V9FS_SM_PASSTHROUGH) { 1363f00d4f59SWei Liu ctx->xops = passthrough_xattr_ops; 1364f00d4f59SWei Liu } else if (ctx->export_flags & V9FS_SM_MAPPED) { 1365f00d4f59SWei Liu ctx->xops = mapped_xattr_ops; 1366f00d4f59SWei Liu } else if (ctx->export_flags & V9FS_SM_NONE) { 1367f00d4f59SWei Liu ctx->xops = none_xattr_ops; 1368f00d4f59SWei Liu } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 1369f00d4f59SWei Liu /* 1370f00d4f59SWei Liu * xattr operation for mapped-file and passthrough 1371f00d4f59SWei Liu * remain same. 1372f00d4f59SWei Liu */ 1373f00d4f59SWei Liu ctx->xops = passthrough_xattr_ops; 1374f00d4f59SWei Liu } 1375f00d4f59SWei Liu ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT; 137600c90bd1SGreg Kurz 13770e35a378SGreg Kurz ctx->private = data; 137800c90bd1SGreg Kurz return 0; 13790e35a378SGreg Kurz 13800e35a378SGreg Kurz err: 13810e35a378SGreg Kurz g_free(data); 13820e35a378SGreg Kurz return -1; 13830e35a378SGreg Kurz } 13840e35a378SGreg Kurz 13850e35a378SGreg Kurz static void local_cleanup(FsContext *ctx) 13860e35a378SGreg Kurz { 13870e35a378SGreg Kurz LocalData *data = ctx->private; 13880e35a378SGreg Kurz 13890e35a378SGreg Kurz close(data->mountfd); 13900e35a378SGreg Kurz g_free(data); 1391f00d4f59SWei Liu } 1392f00d4f59SWei Liu 1393f00d4f59SWei Liu static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse) 1394f00d4f59SWei Liu { 1395f00d4f59SWei Liu const char *sec_model = qemu_opt_get(opts, "security_model"); 1396f00d4f59SWei Liu const char *path = qemu_opt_get(opts, "path"); 1397f00d4f59SWei Liu 1398f00d4f59SWei Liu if (!sec_model) { 139963325b18SGreg Kurz error_report("Security model not specified, local fs needs security model"); 140063325b18SGreg Kurz error_printf("valid options are:" 140163325b18SGreg Kurz "\tsecurity_model=[passthrough|mapped-xattr|mapped-file|none]\n"); 1402f00d4f59SWei Liu return -1; 1403f00d4f59SWei Liu } 1404f00d4f59SWei Liu 1405f00d4f59SWei Liu if (!strcmp(sec_model, "passthrough")) { 1406f00d4f59SWei Liu fse->export_flags |= V9FS_SM_PASSTHROUGH; 1407f00d4f59SWei Liu } else if (!strcmp(sec_model, "mapped") || 1408f00d4f59SWei Liu !strcmp(sec_model, "mapped-xattr")) { 1409f00d4f59SWei Liu fse->export_flags |= V9FS_SM_MAPPED; 1410f00d4f59SWei Liu } else if (!strcmp(sec_model, "none")) { 1411f00d4f59SWei Liu fse->export_flags |= V9FS_SM_NONE; 1412f00d4f59SWei Liu } else if (!strcmp(sec_model, "mapped-file")) { 1413f00d4f59SWei Liu fse->export_flags |= V9FS_SM_MAPPED_FILE; 1414f00d4f59SWei Liu } else { 141563325b18SGreg Kurz error_report("Invalid security model %s specified", sec_model); 141663325b18SGreg Kurz error_printf("valid options are:" 141763325b18SGreg Kurz "\t[passthrough|mapped-xattr|mapped-file|none]\n"); 1418f00d4f59SWei Liu return -1; 1419f00d4f59SWei Liu } 1420f00d4f59SWei Liu 1421f00d4f59SWei Liu if (!path) { 142263325b18SGreg Kurz error_report("fsdev: No path specified"); 1423f00d4f59SWei Liu return -1; 1424f00d4f59SWei Liu } 1425f00d4f59SWei Liu fse->path = g_strdup(path); 1426f00d4f59SWei Liu 1427f00d4f59SWei Liu return 0; 1428f00d4f59SWei Liu } 1429f00d4f59SWei Liu 1430f00d4f59SWei Liu FileOperations local_ops = { 1431f00d4f59SWei Liu .parse_opts = local_parse_opts, 1432f00d4f59SWei Liu .init = local_init, 14330e35a378SGreg Kurz .cleanup = local_cleanup, 1434f00d4f59SWei Liu .lstat = local_lstat, 1435f00d4f59SWei Liu .readlink = local_readlink, 1436f00d4f59SWei Liu .close = local_close, 1437f00d4f59SWei Liu .closedir = local_closedir, 1438f00d4f59SWei Liu .open = local_open, 1439f00d4f59SWei Liu .opendir = local_opendir, 1440f00d4f59SWei Liu .rewinddir = local_rewinddir, 1441f00d4f59SWei Liu .telldir = local_telldir, 1442635324e8SGreg Kurz .readdir = local_readdir, 1443f00d4f59SWei Liu .seekdir = local_seekdir, 1444f00d4f59SWei Liu .preadv = local_preadv, 1445f00d4f59SWei Liu .pwritev = local_pwritev, 1446f00d4f59SWei Liu .chmod = local_chmod, 1447f00d4f59SWei Liu .mknod = local_mknod, 1448f00d4f59SWei Liu .mkdir = local_mkdir, 1449f00d4f59SWei Liu .fstat = local_fstat, 1450f00d4f59SWei Liu .open2 = local_open2, 1451f00d4f59SWei Liu .symlink = local_symlink, 1452f00d4f59SWei Liu .link = local_link, 1453f00d4f59SWei Liu .truncate = local_truncate, 1454f00d4f59SWei Liu .rename = local_rename, 1455f00d4f59SWei Liu .chown = local_chown, 1456f00d4f59SWei Liu .utimensat = local_utimensat, 1457f00d4f59SWei Liu .remove = local_remove, 1458f00d4f59SWei Liu .fsync = local_fsync, 1459f00d4f59SWei Liu .statfs = local_statfs, 1460f00d4f59SWei Liu .lgetxattr = local_lgetxattr, 1461f00d4f59SWei Liu .llistxattr = local_llistxattr, 1462f00d4f59SWei Liu .lsetxattr = local_lsetxattr, 1463f00d4f59SWei Liu .lremovexattr = local_lremovexattr, 1464f00d4f59SWei Liu .name_to_path = local_name_to_path, 1465f00d4f59SWei Liu .renameat = local_renameat, 1466f00d4f59SWei Liu .unlinkat = local_unlinkat, 1467f00d4f59SWei Liu }; 1468