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