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 114f00d4f59SWei Liu #define ATTR_MAX 100 115f00d4f59SWei Liu static void local_mapped_file_attr(FsContext *ctx, const char *path, 116f00d4f59SWei Liu struct stat *stbuf) 117f00d4f59SWei Liu { 118f00d4f59SWei Liu FILE *fp; 119f00d4f59SWei Liu char buf[ATTR_MAX]; 120f00d4f59SWei Liu char *attr_path; 121f00d4f59SWei Liu 122f00d4f59SWei Liu attr_path = local_mapped_attr_path(ctx, path); 123f00d4f59SWei Liu fp = local_fopen(attr_path, "r"); 124f00d4f59SWei Liu g_free(attr_path); 125f00d4f59SWei Liu if (!fp) { 126f00d4f59SWei Liu return; 127f00d4f59SWei Liu } 128f00d4f59SWei Liu memset(buf, 0, ATTR_MAX); 129f00d4f59SWei Liu while (fgets(buf, ATTR_MAX, fp)) { 130f00d4f59SWei Liu if (!strncmp(buf, "virtfs.uid", 10)) { 131f00d4f59SWei Liu stbuf->st_uid = atoi(buf+11); 132f00d4f59SWei Liu } else if (!strncmp(buf, "virtfs.gid", 10)) { 133f00d4f59SWei Liu stbuf->st_gid = atoi(buf+11); 134f00d4f59SWei Liu } else if (!strncmp(buf, "virtfs.mode", 11)) { 135f00d4f59SWei Liu stbuf->st_mode = atoi(buf+12); 136f00d4f59SWei Liu } else if (!strncmp(buf, "virtfs.rdev", 11)) { 137f00d4f59SWei Liu stbuf->st_rdev = atoi(buf+12); 138f00d4f59SWei Liu } 139f00d4f59SWei Liu memset(buf, 0, ATTR_MAX); 140f00d4f59SWei Liu } 141f00d4f59SWei Liu fclose(fp); 142f00d4f59SWei Liu } 143f00d4f59SWei Liu 144f00d4f59SWei Liu static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf) 145f00d4f59SWei Liu { 146f00d4f59SWei Liu int err; 147f00d4f59SWei Liu char *buffer; 148f00d4f59SWei Liu char *path = fs_path->data; 149f00d4f59SWei Liu 150f00d4f59SWei Liu buffer = rpath(fs_ctx, path); 151f00d4f59SWei Liu err = lstat(buffer, stbuf); 152f00d4f59SWei Liu if (err) { 153f00d4f59SWei Liu goto err_out; 154f00d4f59SWei Liu } 155f00d4f59SWei Liu if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 156f00d4f59SWei Liu /* Actual credentials are part of extended attrs */ 157f00d4f59SWei Liu uid_t tmp_uid; 158f00d4f59SWei Liu gid_t tmp_gid; 159f00d4f59SWei Liu mode_t tmp_mode; 160f00d4f59SWei Liu dev_t tmp_dev; 161f00d4f59SWei Liu if (getxattr(buffer, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) { 162f00d4f59SWei Liu stbuf->st_uid = le32_to_cpu(tmp_uid); 163f00d4f59SWei Liu } 164f00d4f59SWei Liu if (getxattr(buffer, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) { 165f00d4f59SWei Liu stbuf->st_gid = le32_to_cpu(tmp_gid); 166f00d4f59SWei Liu } 167f00d4f59SWei Liu if (getxattr(buffer, "user.virtfs.mode", 168f00d4f59SWei Liu &tmp_mode, sizeof(mode_t)) > 0) { 169f00d4f59SWei Liu stbuf->st_mode = le32_to_cpu(tmp_mode); 170f00d4f59SWei Liu } 171f00d4f59SWei Liu if (getxattr(buffer, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) { 172f00d4f59SWei Liu stbuf->st_rdev = le64_to_cpu(tmp_dev); 173f00d4f59SWei Liu } 174f00d4f59SWei Liu } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 175f00d4f59SWei Liu local_mapped_file_attr(fs_ctx, path, stbuf); 176f00d4f59SWei Liu } 177f00d4f59SWei Liu 178f00d4f59SWei Liu err_out: 179f00d4f59SWei Liu g_free(buffer); 180f00d4f59SWei Liu return err; 181f00d4f59SWei Liu } 182f00d4f59SWei Liu 183f00d4f59SWei Liu static int local_create_mapped_attr_dir(FsContext *ctx, const char *path) 184f00d4f59SWei Liu { 185f00d4f59SWei Liu int err; 186f00d4f59SWei Liu char *attr_dir; 187f00d4f59SWei Liu char *tmp_path = g_strdup(path); 188f00d4f59SWei Liu 189f00d4f59SWei Liu attr_dir = g_strdup_printf("%s/%s/%s", 190f00d4f59SWei Liu ctx->fs_root, dirname(tmp_path), VIRTFS_META_DIR); 191f00d4f59SWei Liu 192f00d4f59SWei Liu err = mkdir(attr_dir, 0700); 193f00d4f59SWei Liu if (err < 0 && errno == EEXIST) { 194f00d4f59SWei Liu err = 0; 195f00d4f59SWei Liu } 196f00d4f59SWei Liu g_free(attr_dir); 197f00d4f59SWei Liu g_free(tmp_path); 198f00d4f59SWei Liu return err; 199f00d4f59SWei Liu } 200f00d4f59SWei Liu 201f00d4f59SWei Liu static int local_set_mapped_file_attr(FsContext *ctx, 202f00d4f59SWei Liu const char *path, FsCred *credp) 203f00d4f59SWei Liu { 204f00d4f59SWei Liu FILE *fp; 205f00d4f59SWei Liu int ret = 0; 206f00d4f59SWei Liu char buf[ATTR_MAX]; 207f00d4f59SWei Liu char *attr_path; 208f00d4f59SWei Liu int uid = -1, gid = -1, mode = -1, rdev = -1; 209f00d4f59SWei Liu 210f00d4f59SWei Liu attr_path = local_mapped_attr_path(ctx, path); 211f00d4f59SWei Liu fp = local_fopen(attr_path, "r"); 212f00d4f59SWei Liu if (!fp) { 213f00d4f59SWei Liu goto create_map_file; 214f00d4f59SWei Liu } 215f00d4f59SWei Liu memset(buf, 0, ATTR_MAX); 216f00d4f59SWei Liu while (fgets(buf, ATTR_MAX, fp)) { 217f00d4f59SWei Liu if (!strncmp(buf, "virtfs.uid", 10)) { 218f00d4f59SWei Liu uid = atoi(buf+11); 219f00d4f59SWei Liu } else if (!strncmp(buf, "virtfs.gid", 10)) { 220f00d4f59SWei Liu gid = atoi(buf+11); 221f00d4f59SWei Liu } else if (!strncmp(buf, "virtfs.mode", 11)) { 222f00d4f59SWei Liu mode = atoi(buf+12); 223f00d4f59SWei Liu } else if (!strncmp(buf, "virtfs.rdev", 11)) { 224f00d4f59SWei Liu rdev = atoi(buf+12); 225f00d4f59SWei Liu } 226f00d4f59SWei Liu memset(buf, 0, ATTR_MAX); 227f00d4f59SWei Liu } 228f00d4f59SWei Liu fclose(fp); 229f00d4f59SWei Liu goto update_map_file; 230f00d4f59SWei Liu 231f00d4f59SWei Liu create_map_file: 232f00d4f59SWei Liu ret = local_create_mapped_attr_dir(ctx, path); 233f00d4f59SWei Liu if (ret < 0) { 234f00d4f59SWei Liu goto err_out; 235f00d4f59SWei Liu } 236f00d4f59SWei Liu 237f00d4f59SWei Liu update_map_file: 238f00d4f59SWei Liu fp = local_fopen(attr_path, "w"); 239f00d4f59SWei Liu if (!fp) { 240f00d4f59SWei Liu ret = -1; 241f00d4f59SWei Liu goto err_out; 242f00d4f59SWei Liu } 243f00d4f59SWei Liu 244f00d4f59SWei Liu if (credp->fc_uid != -1) { 245f00d4f59SWei Liu uid = credp->fc_uid; 246f00d4f59SWei Liu } 247f00d4f59SWei Liu if (credp->fc_gid != -1) { 248f00d4f59SWei Liu gid = credp->fc_gid; 249f00d4f59SWei Liu } 250f00d4f59SWei Liu if (credp->fc_mode != -1) { 251f00d4f59SWei Liu mode = credp->fc_mode; 252f00d4f59SWei Liu } 253f00d4f59SWei Liu if (credp->fc_rdev != -1) { 254f00d4f59SWei Liu rdev = credp->fc_rdev; 255f00d4f59SWei Liu } 256f00d4f59SWei Liu 257f00d4f59SWei Liu 258f00d4f59SWei Liu if (uid != -1) { 259f00d4f59SWei Liu fprintf(fp, "virtfs.uid=%d\n", uid); 260f00d4f59SWei Liu } 261f00d4f59SWei Liu if (gid != -1) { 262f00d4f59SWei Liu fprintf(fp, "virtfs.gid=%d\n", gid); 263f00d4f59SWei Liu } 264f00d4f59SWei Liu if (mode != -1) { 265f00d4f59SWei Liu fprintf(fp, "virtfs.mode=%d\n", mode); 266f00d4f59SWei Liu } 267f00d4f59SWei Liu if (rdev != -1) { 268f00d4f59SWei Liu fprintf(fp, "virtfs.rdev=%d\n", rdev); 269f00d4f59SWei Liu } 270f00d4f59SWei Liu fclose(fp); 271f00d4f59SWei Liu 272f00d4f59SWei Liu err_out: 273f00d4f59SWei Liu g_free(attr_path); 274f00d4f59SWei Liu return ret; 275f00d4f59SWei Liu } 276f00d4f59SWei Liu 277f00d4f59SWei Liu static int local_set_xattr(const char *path, FsCred *credp) 278f00d4f59SWei Liu { 279f00d4f59SWei Liu int err; 280f00d4f59SWei Liu 281f00d4f59SWei Liu if (credp->fc_uid != -1) { 282f00d4f59SWei Liu uint32_t tmp_uid = cpu_to_le32(credp->fc_uid); 283f00d4f59SWei Liu err = setxattr(path, "user.virtfs.uid", &tmp_uid, sizeof(uid_t), 0); 284f00d4f59SWei Liu if (err) { 285f00d4f59SWei Liu return err; 286f00d4f59SWei Liu } 287f00d4f59SWei Liu } 288f00d4f59SWei Liu if (credp->fc_gid != -1) { 289f00d4f59SWei Liu uint32_t tmp_gid = cpu_to_le32(credp->fc_gid); 290f00d4f59SWei Liu err = setxattr(path, "user.virtfs.gid", &tmp_gid, sizeof(gid_t), 0); 291f00d4f59SWei Liu if (err) { 292f00d4f59SWei Liu return err; 293f00d4f59SWei Liu } 294f00d4f59SWei Liu } 295f00d4f59SWei Liu if (credp->fc_mode != -1) { 296f00d4f59SWei Liu uint32_t tmp_mode = cpu_to_le32(credp->fc_mode); 297f00d4f59SWei Liu err = setxattr(path, "user.virtfs.mode", &tmp_mode, sizeof(mode_t), 0); 298f00d4f59SWei Liu if (err) { 299f00d4f59SWei Liu return err; 300f00d4f59SWei Liu } 301f00d4f59SWei Liu } 302f00d4f59SWei Liu if (credp->fc_rdev != -1) { 303f00d4f59SWei Liu uint64_t tmp_rdev = cpu_to_le64(credp->fc_rdev); 304f00d4f59SWei Liu err = setxattr(path, "user.virtfs.rdev", &tmp_rdev, sizeof(dev_t), 0); 305f00d4f59SWei Liu if (err) { 306f00d4f59SWei Liu return err; 307f00d4f59SWei Liu } 308f00d4f59SWei Liu } 309f00d4f59SWei Liu return 0; 310f00d4f59SWei Liu } 311f00d4f59SWei Liu 312f00d4f59SWei Liu static int local_post_create_passthrough(FsContext *fs_ctx, const char *path, 313f00d4f59SWei Liu FsCred *credp) 314f00d4f59SWei Liu { 315f00d4f59SWei Liu char *buffer; 316f00d4f59SWei Liu 317f00d4f59SWei Liu buffer = rpath(fs_ctx, path); 318f00d4f59SWei Liu if (lchown(buffer, credp->fc_uid, credp->fc_gid) < 0) { 319f00d4f59SWei Liu /* 320f00d4f59SWei Liu * If we fail to change ownership and if we are 321f00d4f59SWei Liu * using security model none. Ignore the error 322f00d4f59SWei Liu */ 323f00d4f59SWei Liu if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) { 324f00d4f59SWei Liu goto err; 325f00d4f59SWei Liu } 326f00d4f59SWei Liu } 327f00d4f59SWei Liu 328f00d4f59SWei Liu if (chmod(buffer, credp->fc_mode & 07777) < 0) { 329f00d4f59SWei Liu goto err; 330f00d4f59SWei Liu } 331f00d4f59SWei Liu 332f00d4f59SWei Liu g_free(buffer); 333f00d4f59SWei Liu return 0; 334f00d4f59SWei Liu err: 335f00d4f59SWei Liu g_free(buffer); 336f00d4f59SWei Liu return -1; 337f00d4f59SWei Liu } 338f00d4f59SWei Liu 339f00d4f59SWei Liu static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path, 340f00d4f59SWei Liu char *buf, size_t bufsz) 341f00d4f59SWei Liu { 342f00d4f59SWei Liu ssize_t tsize = -1; 343f00d4f59SWei Liu 344f00d4f59SWei Liu if ((fs_ctx->export_flags & V9FS_SM_MAPPED) || 345f00d4f59SWei Liu (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) { 346f00d4f59SWei Liu int fd; 347*bec1e954SGreg Kurz 348*bec1e954SGreg Kurz fd = local_open_nofollow(fs_ctx, fs_path->data, O_RDONLY, 0); 349f00d4f59SWei Liu if (fd == -1) { 350f00d4f59SWei Liu return -1; 351f00d4f59SWei Liu } 352f00d4f59SWei Liu do { 353f00d4f59SWei Liu tsize = read(fd, (void *)buf, bufsz); 354f00d4f59SWei Liu } while (tsize == -1 && errno == EINTR); 355*bec1e954SGreg Kurz close_preserve_errno(fd); 356f00d4f59SWei Liu } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 357f00d4f59SWei Liu (fs_ctx->export_flags & V9FS_SM_NONE)) { 358*bec1e954SGreg Kurz char *dirpath = g_path_get_dirname(fs_path->data); 359*bec1e954SGreg Kurz char *name = g_path_get_basename(fs_path->data); 360*bec1e954SGreg Kurz int dirfd; 361*bec1e954SGreg Kurz 362*bec1e954SGreg Kurz dirfd = local_opendir_nofollow(fs_ctx, dirpath); 363*bec1e954SGreg Kurz if (dirfd == -1) { 364*bec1e954SGreg Kurz goto out; 365*bec1e954SGreg Kurz } 366*bec1e954SGreg Kurz 367*bec1e954SGreg Kurz tsize = readlinkat(dirfd, name, buf, bufsz); 368*bec1e954SGreg Kurz close_preserve_errno(dirfd); 369*bec1e954SGreg Kurz out: 370*bec1e954SGreg Kurz g_free(name); 371*bec1e954SGreg Kurz g_free(dirpath); 372f00d4f59SWei Liu } 373f00d4f59SWei Liu return tsize; 374f00d4f59SWei Liu } 375f00d4f59SWei Liu 376f00d4f59SWei Liu static int local_close(FsContext *ctx, V9fsFidOpenState *fs) 377f00d4f59SWei Liu { 378f00d4f59SWei Liu return close(fs->fd); 379f00d4f59SWei Liu } 380f00d4f59SWei Liu 381f00d4f59SWei Liu static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs) 382f00d4f59SWei Liu { 383f314ea4eSGreg Kurz return closedir(fs->dir.stream); 384f00d4f59SWei Liu } 385f00d4f59SWei Liu 386f00d4f59SWei Liu static int local_open(FsContext *ctx, V9fsPath *fs_path, 387f00d4f59SWei Liu int flags, V9fsFidOpenState *fs) 388f00d4f59SWei Liu { 38921328e1eSGreg Kurz int fd; 390f00d4f59SWei Liu 391996a0d76SGreg Kurz fd = local_open_nofollow(ctx, fs_path->data, flags, 0); 39221328e1eSGreg Kurz if (fd == -1) { 39321328e1eSGreg Kurz return -1; 39421328e1eSGreg Kurz } 39521328e1eSGreg Kurz fs->fd = fd; 396f00d4f59SWei Liu return fs->fd; 397f00d4f59SWei Liu } 398f00d4f59SWei Liu 399f00d4f59SWei Liu static int local_opendir(FsContext *ctx, 400f00d4f59SWei Liu V9fsPath *fs_path, V9fsFidOpenState *fs) 401f00d4f59SWei Liu { 402996a0d76SGreg Kurz int dirfd; 40321328e1eSGreg Kurz DIR *stream; 404f00d4f59SWei Liu 405996a0d76SGreg Kurz dirfd = local_opendir_nofollow(ctx, fs_path->data); 406996a0d76SGreg Kurz if (dirfd == -1) { 407996a0d76SGreg Kurz return -1; 408996a0d76SGreg Kurz } 409996a0d76SGreg Kurz 410996a0d76SGreg Kurz stream = fdopendir(dirfd); 41121328e1eSGreg Kurz if (!stream) { 412f00d4f59SWei Liu return -1; 413f00d4f59SWei Liu } 41421328e1eSGreg Kurz fs->dir.stream = stream; 415f00d4f59SWei Liu return 0; 416f00d4f59SWei Liu } 417f00d4f59SWei Liu 418f00d4f59SWei Liu static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs) 419f00d4f59SWei Liu { 420f314ea4eSGreg Kurz rewinddir(fs->dir.stream); 421f00d4f59SWei Liu } 422f00d4f59SWei Liu 423f00d4f59SWei Liu static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs) 424f00d4f59SWei Liu { 425f314ea4eSGreg Kurz return telldir(fs->dir.stream); 426f00d4f59SWei Liu } 427f00d4f59SWei Liu 428635324e8SGreg Kurz static struct dirent *local_readdir(FsContext *ctx, V9fsFidOpenState *fs) 429f00d4f59SWei Liu { 430635324e8SGreg Kurz struct dirent *entry; 431f00d4f59SWei Liu 432f00d4f59SWei Liu again: 433635324e8SGreg Kurz entry = readdir(fs->dir.stream); 434635324e8SGreg Kurz if (!entry) { 435635324e8SGreg Kurz return NULL; 436635324e8SGreg Kurz } 437635324e8SGreg Kurz 438f00d4f59SWei Liu if (ctx->export_flags & V9FS_SM_MAPPED) { 439f00d4f59SWei Liu entry->d_type = DT_UNKNOWN; 440f00d4f59SWei Liu } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 441635324e8SGreg Kurz if (!strcmp(entry->d_name, VIRTFS_META_DIR)) { 442f00d4f59SWei Liu /* skp the meta data directory */ 443f00d4f59SWei Liu goto again; 444f00d4f59SWei Liu } 445f00d4f59SWei Liu entry->d_type = DT_UNKNOWN; 446f00d4f59SWei Liu } 447635324e8SGreg Kurz 448635324e8SGreg Kurz return entry; 449f00d4f59SWei Liu } 450f00d4f59SWei Liu 451f00d4f59SWei Liu static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off) 452f00d4f59SWei Liu { 453f314ea4eSGreg Kurz seekdir(fs->dir.stream, off); 454f00d4f59SWei Liu } 455f00d4f59SWei Liu 456f00d4f59SWei Liu static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs, 457f00d4f59SWei Liu const struct iovec *iov, 458f00d4f59SWei Liu int iovcnt, off_t offset) 459f00d4f59SWei Liu { 460f00d4f59SWei Liu #ifdef CONFIG_PREADV 461f00d4f59SWei Liu return preadv(fs->fd, iov, iovcnt, offset); 462f00d4f59SWei Liu #else 463f00d4f59SWei Liu int err = lseek(fs->fd, offset, SEEK_SET); 464f00d4f59SWei Liu if (err == -1) { 465f00d4f59SWei Liu return err; 466f00d4f59SWei Liu } else { 467f00d4f59SWei Liu return readv(fs->fd, iov, iovcnt); 468f00d4f59SWei Liu } 469f00d4f59SWei Liu #endif 470f00d4f59SWei Liu } 471f00d4f59SWei Liu 472f00d4f59SWei Liu static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs, 473f00d4f59SWei Liu const struct iovec *iov, 474f00d4f59SWei Liu int iovcnt, off_t offset) 475f00d4f59SWei Liu { 4766fe76accSGreg Kurz ssize_t ret; 477f00d4f59SWei Liu #ifdef CONFIG_PREADV 478f00d4f59SWei Liu ret = pwritev(fs->fd, iov, iovcnt, offset); 479f00d4f59SWei Liu #else 480f00d4f59SWei Liu int err = lseek(fs->fd, offset, SEEK_SET); 481f00d4f59SWei Liu if (err == -1) { 482f00d4f59SWei Liu return err; 483f00d4f59SWei Liu } else { 484f00d4f59SWei Liu ret = writev(fs->fd, iov, iovcnt); 485f00d4f59SWei Liu } 486f00d4f59SWei Liu #endif 487f00d4f59SWei Liu #ifdef CONFIG_SYNC_FILE_RANGE 488f00d4f59SWei Liu if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) { 489f00d4f59SWei Liu /* 490f00d4f59SWei Liu * Initiate a writeback. This is not a data integrity sync. 491f00d4f59SWei Liu * We want to ensure that we don't leave dirty pages in the cache 492f00d4f59SWei Liu * after write when writeout=immediate is sepcified. 493f00d4f59SWei Liu */ 494f00d4f59SWei Liu sync_file_range(fs->fd, offset, ret, 495f00d4f59SWei Liu SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE); 496f00d4f59SWei Liu } 497f00d4f59SWei Liu #endif 498f00d4f59SWei Liu return ret; 499f00d4f59SWei Liu } 500f00d4f59SWei Liu 501f00d4f59SWei Liu static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp) 502f00d4f59SWei Liu { 503f00d4f59SWei Liu char *buffer; 504f00d4f59SWei Liu int ret = -1; 505f00d4f59SWei Liu char *path = fs_path->data; 506f00d4f59SWei Liu 507f00d4f59SWei Liu if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 508f00d4f59SWei Liu buffer = rpath(fs_ctx, path); 509f00d4f59SWei Liu ret = local_set_xattr(buffer, credp); 510f00d4f59SWei Liu g_free(buffer); 511f00d4f59SWei Liu } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 512f00d4f59SWei Liu return local_set_mapped_file_attr(fs_ctx, path, credp); 513f00d4f59SWei Liu } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 514f00d4f59SWei Liu (fs_ctx->export_flags & V9FS_SM_NONE)) { 515f00d4f59SWei Liu buffer = rpath(fs_ctx, path); 516f00d4f59SWei Liu ret = chmod(buffer, credp->fc_mode); 517f00d4f59SWei Liu g_free(buffer); 518f00d4f59SWei Liu } 519f00d4f59SWei Liu return ret; 520f00d4f59SWei Liu } 521f00d4f59SWei Liu 522f00d4f59SWei Liu static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path, 523f00d4f59SWei Liu const char *name, FsCred *credp) 524f00d4f59SWei Liu { 525f00d4f59SWei Liu char *path; 526f00d4f59SWei Liu int err = -1; 527f00d4f59SWei Liu int serrno = 0; 528f00d4f59SWei Liu V9fsString fullname; 529f00d4f59SWei Liu char *buffer = NULL; 530f00d4f59SWei Liu 531f00d4f59SWei Liu v9fs_string_init(&fullname); 532f00d4f59SWei Liu v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name); 533f00d4f59SWei Liu path = fullname.data; 534f00d4f59SWei Liu 535f00d4f59SWei Liu /* Determine the security model */ 536f00d4f59SWei Liu if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 537f00d4f59SWei Liu buffer = rpath(fs_ctx, path); 538f00d4f59SWei Liu err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0); 539f00d4f59SWei Liu if (err == -1) { 540f00d4f59SWei Liu goto out; 541f00d4f59SWei Liu } 542f00d4f59SWei Liu err = local_set_xattr(buffer, credp); 543f00d4f59SWei Liu if (err == -1) { 544f00d4f59SWei Liu serrno = errno; 545f00d4f59SWei Liu goto err_end; 546f00d4f59SWei Liu } 547f00d4f59SWei Liu } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 548f00d4f59SWei Liu 549f00d4f59SWei Liu buffer = rpath(fs_ctx, path); 550f00d4f59SWei Liu err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0); 551f00d4f59SWei Liu if (err == -1) { 552f00d4f59SWei Liu goto out; 553f00d4f59SWei Liu } 554f00d4f59SWei Liu err = local_set_mapped_file_attr(fs_ctx, path, credp); 555f00d4f59SWei Liu if (err == -1) { 556f00d4f59SWei Liu serrno = errno; 557f00d4f59SWei Liu goto err_end; 558f00d4f59SWei Liu } 559f00d4f59SWei Liu } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 560f00d4f59SWei Liu (fs_ctx->export_flags & V9FS_SM_NONE)) { 561f00d4f59SWei Liu buffer = rpath(fs_ctx, path); 562f00d4f59SWei Liu err = mknod(buffer, credp->fc_mode, credp->fc_rdev); 563f00d4f59SWei Liu if (err == -1) { 564f00d4f59SWei Liu goto out; 565f00d4f59SWei Liu } 566f00d4f59SWei Liu err = local_post_create_passthrough(fs_ctx, path, credp); 567f00d4f59SWei Liu if (err == -1) { 568f00d4f59SWei Liu serrno = errno; 569f00d4f59SWei Liu goto err_end; 570f00d4f59SWei Liu } 571f00d4f59SWei Liu } 572f00d4f59SWei Liu goto out; 573f00d4f59SWei Liu 574f00d4f59SWei Liu err_end: 575f00d4f59SWei Liu remove(buffer); 576f00d4f59SWei Liu errno = serrno; 577f00d4f59SWei Liu out: 578f00d4f59SWei Liu g_free(buffer); 579f00d4f59SWei Liu v9fs_string_free(&fullname); 580f00d4f59SWei Liu return err; 581f00d4f59SWei Liu } 582f00d4f59SWei Liu 583f00d4f59SWei Liu static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path, 584f00d4f59SWei Liu const char *name, FsCred *credp) 585f00d4f59SWei Liu { 586f00d4f59SWei Liu char *path; 587f00d4f59SWei Liu int err = -1; 588f00d4f59SWei Liu int serrno = 0; 589f00d4f59SWei Liu V9fsString fullname; 590f00d4f59SWei Liu char *buffer = NULL; 591f00d4f59SWei Liu 592f00d4f59SWei Liu v9fs_string_init(&fullname); 593f00d4f59SWei Liu v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name); 594f00d4f59SWei Liu path = fullname.data; 595f00d4f59SWei Liu 596f00d4f59SWei Liu /* Determine the security model */ 597f00d4f59SWei Liu if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 598f00d4f59SWei Liu buffer = rpath(fs_ctx, path); 599f00d4f59SWei Liu err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS); 600f00d4f59SWei Liu if (err == -1) { 601f00d4f59SWei Liu goto out; 602f00d4f59SWei Liu } 603f00d4f59SWei Liu credp->fc_mode = credp->fc_mode|S_IFDIR; 604f00d4f59SWei Liu err = local_set_xattr(buffer, credp); 605f00d4f59SWei Liu if (err == -1) { 606f00d4f59SWei Liu serrno = errno; 607f00d4f59SWei Liu goto err_end; 608f00d4f59SWei Liu } 609f00d4f59SWei Liu } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 610f00d4f59SWei Liu buffer = rpath(fs_ctx, path); 611f00d4f59SWei Liu err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS); 612f00d4f59SWei Liu if (err == -1) { 613f00d4f59SWei Liu goto out; 614f00d4f59SWei Liu } 615f00d4f59SWei Liu credp->fc_mode = credp->fc_mode|S_IFDIR; 616f00d4f59SWei Liu err = local_set_mapped_file_attr(fs_ctx, path, credp); 617f00d4f59SWei Liu if (err == -1) { 618f00d4f59SWei Liu serrno = errno; 619f00d4f59SWei Liu goto err_end; 620f00d4f59SWei Liu } 621f00d4f59SWei Liu } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 622f00d4f59SWei Liu (fs_ctx->export_flags & V9FS_SM_NONE)) { 623f00d4f59SWei Liu buffer = rpath(fs_ctx, path); 624f00d4f59SWei Liu err = mkdir(buffer, credp->fc_mode); 625f00d4f59SWei Liu if (err == -1) { 626f00d4f59SWei Liu goto out; 627f00d4f59SWei Liu } 628f00d4f59SWei Liu err = local_post_create_passthrough(fs_ctx, path, credp); 629f00d4f59SWei Liu if (err == -1) { 630f00d4f59SWei Liu serrno = errno; 631f00d4f59SWei Liu goto err_end; 632f00d4f59SWei Liu } 633f00d4f59SWei Liu } 634f00d4f59SWei Liu goto out; 635f00d4f59SWei Liu 636f00d4f59SWei Liu err_end: 637f00d4f59SWei Liu remove(buffer); 638f00d4f59SWei Liu errno = serrno; 639f00d4f59SWei Liu out: 640f00d4f59SWei Liu g_free(buffer); 641f00d4f59SWei Liu v9fs_string_free(&fullname); 642f00d4f59SWei Liu return err; 643f00d4f59SWei Liu } 644f00d4f59SWei Liu 645f00d4f59SWei Liu static int local_fstat(FsContext *fs_ctx, int fid_type, 646f00d4f59SWei Liu V9fsFidOpenState *fs, struct stat *stbuf) 647f00d4f59SWei Liu { 648f00d4f59SWei Liu int err, fd; 649f00d4f59SWei Liu 650f00d4f59SWei Liu if (fid_type == P9_FID_DIR) { 651f314ea4eSGreg Kurz fd = dirfd(fs->dir.stream); 652f00d4f59SWei Liu } else { 653f00d4f59SWei Liu fd = fs->fd; 654f00d4f59SWei Liu } 655f00d4f59SWei Liu 656f00d4f59SWei Liu err = fstat(fd, stbuf); 657f00d4f59SWei Liu if (err) { 658f00d4f59SWei Liu return err; 659f00d4f59SWei Liu } 660f00d4f59SWei Liu if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 661f00d4f59SWei Liu /* Actual credentials are part of extended attrs */ 662f00d4f59SWei Liu uid_t tmp_uid; 663f00d4f59SWei Liu gid_t tmp_gid; 664f00d4f59SWei Liu mode_t tmp_mode; 665f00d4f59SWei Liu dev_t tmp_dev; 666f00d4f59SWei Liu 667f00d4f59SWei Liu if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) { 668f00d4f59SWei Liu stbuf->st_uid = le32_to_cpu(tmp_uid); 669f00d4f59SWei Liu } 670f00d4f59SWei Liu if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) { 671f00d4f59SWei Liu stbuf->st_gid = le32_to_cpu(tmp_gid); 672f00d4f59SWei Liu } 673f00d4f59SWei Liu if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) { 674f00d4f59SWei Liu stbuf->st_mode = le32_to_cpu(tmp_mode); 675f00d4f59SWei Liu } 676f00d4f59SWei Liu if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) { 677f00d4f59SWei Liu stbuf->st_rdev = le64_to_cpu(tmp_dev); 678f00d4f59SWei Liu } 679f00d4f59SWei Liu } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 680f00d4f59SWei Liu errno = EOPNOTSUPP; 681f00d4f59SWei Liu return -1; 682f00d4f59SWei Liu } 683f00d4f59SWei Liu return err; 684f00d4f59SWei Liu } 685f00d4f59SWei Liu 686f00d4f59SWei Liu static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name, 687f00d4f59SWei Liu int flags, FsCred *credp, V9fsFidOpenState *fs) 688f00d4f59SWei Liu { 689f00d4f59SWei Liu char *path; 690f00d4f59SWei Liu int fd = -1; 691f00d4f59SWei Liu int err = -1; 692f00d4f59SWei Liu int serrno = 0; 693f00d4f59SWei Liu V9fsString fullname; 694f00d4f59SWei Liu char *buffer = NULL; 695f00d4f59SWei Liu 696f00d4f59SWei Liu /* 697f00d4f59SWei Liu * Mark all the open to not follow symlinks 698f00d4f59SWei Liu */ 699f00d4f59SWei Liu flags |= O_NOFOLLOW; 700f00d4f59SWei Liu 701f00d4f59SWei Liu v9fs_string_init(&fullname); 702f00d4f59SWei Liu v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name); 703f00d4f59SWei Liu path = fullname.data; 704f00d4f59SWei Liu 705f00d4f59SWei Liu /* Determine the security model */ 706f00d4f59SWei Liu if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 707f00d4f59SWei Liu buffer = rpath(fs_ctx, path); 708f00d4f59SWei Liu fd = open(buffer, flags, SM_LOCAL_MODE_BITS); 709f00d4f59SWei Liu if (fd == -1) { 710f00d4f59SWei Liu err = fd; 711f00d4f59SWei Liu goto out; 712f00d4f59SWei Liu } 713f00d4f59SWei Liu credp->fc_mode = credp->fc_mode|S_IFREG; 714f00d4f59SWei Liu /* Set cleint credentials in xattr */ 715f00d4f59SWei Liu err = local_set_xattr(buffer, credp); 716f00d4f59SWei Liu if (err == -1) { 717f00d4f59SWei Liu serrno = errno; 718f00d4f59SWei Liu goto err_end; 719f00d4f59SWei Liu } 720f00d4f59SWei Liu } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 721f00d4f59SWei Liu buffer = rpath(fs_ctx, path); 722f00d4f59SWei Liu fd = open(buffer, flags, SM_LOCAL_MODE_BITS); 723f00d4f59SWei Liu if (fd == -1) { 724f00d4f59SWei Liu err = fd; 725f00d4f59SWei Liu goto out; 726f00d4f59SWei Liu } 727f00d4f59SWei Liu credp->fc_mode = credp->fc_mode|S_IFREG; 728f00d4f59SWei Liu /* Set client credentials in .virtfs_metadata directory files */ 729f00d4f59SWei Liu err = local_set_mapped_file_attr(fs_ctx, path, credp); 730f00d4f59SWei Liu if (err == -1) { 731f00d4f59SWei Liu serrno = errno; 732f00d4f59SWei Liu goto err_end; 733f00d4f59SWei Liu } 734f00d4f59SWei Liu } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 735f00d4f59SWei Liu (fs_ctx->export_flags & V9FS_SM_NONE)) { 736f00d4f59SWei Liu buffer = rpath(fs_ctx, path); 737f00d4f59SWei Liu fd = open(buffer, flags, credp->fc_mode); 738f00d4f59SWei Liu if (fd == -1) { 739f00d4f59SWei Liu err = fd; 740f00d4f59SWei Liu goto out; 741f00d4f59SWei Liu } 742f00d4f59SWei Liu err = local_post_create_passthrough(fs_ctx, path, credp); 743f00d4f59SWei Liu if (err == -1) { 744f00d4f59SWei Liu serrno = errno; 745f00d4f59SWei Liu goto err_end; 746f00d4f59SWei Liu } 747f00d4f59SWei Liu } 748f00d4f59SWei Liu err = fd; 749f00d4f59SWei Liu fs->fd = fd; 750f00d4f59SWei Liu goto out; 751f00d4f59SWei Liu 752f00d4f59SWei Liu err_end: 753f00d4f59SWei Liu close(fd); 754f00d4f59SWei Liu remove(buffer); 755f00d4f59SWei Liu errno = serrno; 756f00d4f59SWei Liu out: 757f00d4f59SWei Liu g_free(buffer); 758f00d4f59SWei Liu v9fs_string_free(&fullname); 759f00d4f59SWei Liu return err; 760f00d4f59SWei Liu } 761f00d4f59SWei Liu 762f00d4f59SWei Liu 763f00d4f59SWei Liu static int local_symlink(FsContext *fs_ctx, const char *oldpath, 764f00d4f59SWei Liu V9fsPath *dir_path, const char *name, FsCred *credp) 765f00d4f59SWei Liu { 766f00d4f59SWei Liu int err = -1; 767f00d4f59SWei Liu int serrno = 0; 768f00d4f59SWei Liu char *newpath; 769f00d4f59SWei Liu V9fsString fullname; 770f00d4f59SWei Liu char *buffer = NULL; 771f00d4f59SWei Liu 772f00d4f59SWei Liu v9fs_string_init(&fullname); 773f00d4f59SWei Liu v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name); 774f00d4f59SWei Liu newpath = fullname.data; 775f00d4f59SWei Liu 776f00d4f59SWei Liu /* Determine the security model */ 777f00d4f59SWei Liu if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 778f00d4f59SWei Liu int fd; 779f00d4f59SWei Liu ssize_t oldpath_size, write_size; 780f00d4f59SWei Liu buffer = rpath(fs_ctx, newpath); 781f00d4f59SWei Liu fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS); 782f00d4f59SWei Liu if (fd == -1) { 783f00d4f59SWei Liu err = fd; 784f00d4f59SWei Liu goto out; 785f00d4f59SWei Liu } 786f00d4f59SWei Liu /* Write the oldpath (target) to the file. */ 787f00d4f59SWei Liu oldpath_size = strlen(oldpath); 788f00d4f59SWei Liu do { 789f00d4f59SWei Liu write_size = write(fd, (void *)oldpath, oldpath_size); 790f00d4f59SWei Liu } while (write_size == -1 && errno == EINTR); 791f00d4f59SWei Liu 792f00d4f59SWei Liu if (write_size != oldpath_size) { 793f00d4f59SWei Liu serrno = errno; 794f00d4f59SWei Liu close(fd); 795f00d4f59SWei Liu err = -1; 796f00d4f59SWei Liu goto err_end; 797f00d4f59SWei Liu } 798f00d4f59SWei Liu close(fd); 799f00d4f59SWei Liu /* Set cleint credentials in symlink's xattr */ 800f00d4f59SWei Liu credp->fc_mode = credp->fc_mode|S_IFLNK; 801f00d4f59SWei Liu err = local_set_xattr(buffer, credp); 802f00d4f59SWei Liu if (err == -1) { 803f00d4f59SWei Liu serrno = errno; 804f00d4f59SWei Liu goto err_end; 805f00d4f59SWei Liu } 806f00d4f59SWei Liu } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 807f00d4f59SWei Liu int fd; 808f00d4f59SWei Liu ssize_t oldpath_size, write_size; 809f00d4f59SWei Liu buffer = rpath(fs_ctx, newpath); 810f00d4f59SWei Liu fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS); 811f00d4f59SWei Liu if (fd == -1) { 812f00d4f59SWei Liu err = fd; 813f00d4f59SWei Liu goto out; 814f00d4f59SWei Liu } 815f00d4f59SWei Liu /* Write the oldpath (target) to the file. */ 816f00d4f59SWei Liu oldpath_size = strlen(oldpath); 817f00d4f59SWei Liu do { 818f00d4f59SWei Liu write_size = write(fd, (void *)oldpath, oldpath_size); 819f00d4f59SWei Liu } while (write_size == -1 && errno == EINTR); 820f00d4f59SWei Liu 821f00d4f59SWei Liu if (write_size != oldpath_size) { 822f00d4f59SWei Liu serrno = errno; 823f00d4f59SWei Liu close(fd); 824f00d4f59SWei Liu err = -1; 825f00d4f59SWei Liu goto err_end; 826f00d4f59SWei Liu } 827f00d4f59SWei Liu close(fd); 828f00d4f59SWei Liu /* Set cleint credentials in symlink's xattr */ 829f00d4f59SWei Liu credp->fc_mode = credp->fc_mode|S_IFLNK; 830f00d4f59SWei Liu err = local_set_mapped_file_attr(fs_ctx, newpath, credp); 831f00d4f59SWei Liu if (err == -1) { 832f00d4f59SWei Liu serrno = errno; 833f00d4f59SWei Liu goto err_end; 834f00d4f59SWei Liu } 835f00d4f59SWei Liu } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 836f00d4f59SWei Liu (fs_ctx->export_flags & V9FS_SM_NONE)) { 837f00d4f59SWei Liu buffer = rpath(fs_ctx, newpath); 838f00d4f59SWei Liu err = symlink(oldpath, buffer); 839f00d4f59SWei Liu if (err) { 840f00d4f59SWei Liu goto out; 841f00d4f59SWei Liu } 842f00d4f59SWei Liu err = lchown(buffer, credp->fc_uid, credp->fc_gid); 843f00d4f59SWei Liu if (err == -1) { 844f00d4f59SWei Liu /* 845f00d4f59SWei Liu * If we fail to change ownership and if we are 846f00d4f59SWei Liu * using security model none. Ignore the error 847f00d4f59SWei Liu */ 848f00d4f59SWei Liu if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) { 849f00d4f59SWei Liu serrno = errno; 850f00d4f59SWei Liu goto err_end; 851f00d4f59SWei Liu } else 852f00d4f59SWei Liu err = 0; 853f00d4f59SWei Liu } 854f00d4f59SWei Liu } 855f00d4f59SWei Liu goto out; 856f00d4f59SWei Liu 857f00d4f59SWei Liu err_end: 858f00d4f59SWei Liu remove(buffer); 859f00d4f59SWei Liu errno = serrno; 860f00d4f59SWei Liu out: 861f00d4f59SWei Liu g_free(buffer); 862f00d4f59SWei Liu v9fs_string_free(&fullname); 863f00d4f59SWei Liu return err; 864f00d4f59SWei Liu } 865f00d4f59SWei Liu 866f00d4f59SWei Liu static int local_link(FsContext *ctx, V9fsPath *oldpath, 867f00d4f59SWei Liu V9fsPath *dirpath, const char *name) 868f00d4f59SWei Liu { 869f00d4f59SWei Liu int ret; 870f00d4f59SWei Liu V9fsString newpath; 871f00d4f59SWei Liu char *buffer, *buffer1; 872f00d4f59SWei Liu 873f00d4f59SWei Liu v9fs_string_init(&newpath); 874f00d4f59SWei Liu v9fs_string_sprintf(&newpath, "%s/%s", dirpath->data, name); 875f00d4f59SWei Liu 876f00d4f59SWei Liu buffer = rpath(ctx, oldpath->data); 877f00d4f59SWei Liu buffer1 = rpath(ctx, newpath.data); 878f00d4f59SWei Liu ret = link(buffer, buffer1); 879f00d4f59SWei Liu g_free(buffer); 880f00d4f59SWei Liu g_free(buffer1); 881f00d4f59SWei Liu 882f00d4f59SWei Liu /* now link the virtfs_metadata files */ 883f00d4f59SWei Liu if (!ret && (ctx->export_flags & V9FS_SM_MAPPED_FILE)) { 884f00d4f59SWei Liu /* Link the .virtfs_metadata files. Create the metada directory */ 885f00d4f59SWei Liu ret = local_create_mapped_attr_dir(ctx, newpath.data); 886f00d4f59SWei Liu if (ret < 0) { 887f00d4f59SWei Liu goto err_out; 888f00d4f59SWei Liu } 889f00d4f59SWei Liu buffer = local_mapped_attr_path(ctx, oldpath->data); 890f00d4f59SWei Liu buffer1 = local_mapped_attr_path(ctx, newpath.data); 891f00d4f59SWei Liu ret = link(buffer, buffer1); 892f00d4f59SWei Liu g_free(buffer); 893f00d4f59SWei Liu g_free(buffer1); 894f00d4f59SWei Liu if (ret < 0 && errno != ENOENT) { 895f00d4f59SWei Liu goto err_out; 896f00d4f59SWei Liu } 897f00d4f59SWei Liu } 898f00d4f59SWei Liu err_out: 899f00d4f59SWei Liu v9fs_string_free(&newpath); 900f00d4f59SWei Liu return ret; 901f00d4f59SWei Liu } 902f00d4f59SWei Liu 903f00d4f59SWei Liu static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size) 904f00d4f59SWei Liu { 905ac125d99SGreg Kurz int fd, ret; 906f00d4f59SWei Liu 907ac125d99SGreg Kurz fd = local_open_nofollow(ctx, fs_path->data, O_WRONLY, 0); 908ac125d99SGreg Kurz if (fd == -1) { 909ac125d99SGreg Kurz return -1; 910ac125d99SGreg Kurz } 911ac125d99SGreg Kurz ret = ftruncate(fd, size); 912ac125d99SGreg Kurz close_preserve_errno(fd); 913f00d4f59SWei Liu return ret; 914f00d4f59SWei Liu } 915f00d4f59SWei Liu 916f00d4f59SWei Liu static int local_rename(FsContext *ctx, const char *oldpath, 917f00d4f59SWei Liu const char *newpath) 918f00d4f59SWei Liu { 919f00d4f59SWei Liu int err; 920f00d4f59SWei Liu char *buffer, *buffer1; 921f00d4f59SWei Liu 922f00d4f59SWei Liu if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 923f00d4f59SWei Liu err = local_create_mapped_attr_dir(ctx, newpath); 924f00d4f59SWei Liu if (err < 0) { 925f00d4f59SWei Liu return err; 926f00d4f59SWei Liu } 927f00d4f59SWei Liu /* rename the .virtfs_metadata files */ 928f00d4f59SWei Liu buffer = local_mapped_attr_path(ctx, oldpath); 929f00d4f59SWei Liu buffer1 = local_mapped_attr_path(ctx, newpath); 930f00d4f59SWei Liu err = rename(buffer, buffer1); 931f00d4f59SWei Liu g_free(buffer); 932f00d4f59SWei Liu g_free(buffer1); 933f00d4f59SWei Liu if (err < 0 && errno != ENOENT) { 934f00d4f59SWei Liu return err; 935f00d4f59SWei Liu } 936f00d4f59SWei Liu } 937f00d4f59SWei Liu 938f00d4f59SWei Liu buffer = rpath(ctx, oldpath); 939f00d4f59SWei Liu buffer1 = rpath(ctx, newpath); 940f00d4f59SWei Liu err = rename(buffer, buffer1); 941f00d4f59SWei Liu g_free(buffer); 942f00d4f59SWei Liu g_free(buffer1); 943f00d4f59SWei Liu return err; 944f00d4f59SWei Liu } 945f00d4f59SWei Liu 946f00d4f59SWei Liu static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp) 947f00d4f59SWei Liu { 948f00d4f59SWei Liu char *buffer; 949f00d4f59SWei Liu int ret = -1; 950f00d4f59SWei Liu char *path = fs_path->data; 951f00d4f59SWei Liu 952f00d4f59SWei Liu if ((credp->fc_uid == -1 && credp->fc_gid == -1) || 953f00d4f59SWei Liu (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 954f00d4f59SWei Liu (fs_ctx->export_flags & V9FS_SM_NONE)) { 955f00d4f59SWei Liu buffer = rpath(fs_ctx, path); 956f00d4f59SWei Liu ret = lchown(buffer, credp->fc_uid, credp->fc_gid); 957f00d4f59SWei Liu g_free(buffer); 958f00d4f59SWei Liu } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 959f00d4f59SWei Liu buffer = rpath(fs_ctx, path); 960f00d4f59SWei Liu ret = local_set_xattr(buffer, credp); 961f00d4f59SWei Liu g_free(buffer); 962f00d4f59SWei Liu } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 963f00d4f59SWei Liu return local_set_mapped_file_attr(fs_ctx, path, credp); 964f00d4f59SWei Liu } 965f00d4f59SWei Liu return ret; 966f00d4f59SWei Liu } 967f00d4f59SWei Liu 968f00d4f59SWei Liu static int local_utimensat(FsContext *s, V9fsPath *fs_path, 969f00d4f59SWei Liu const struct timespec *buf) 970f00d4f59SWei Liu { 971a33eda0dSGreg Kurz char *dirpath = g_path_get_dirname(fs_path->data); 972a33eda0dSGreg Kurz char *name = g_path_get_basename(fs_path->data); 973a33eda0dSGreg Kurz int dirfd, ret = -1; 974f00d4f59SWei Liu 975a33eda0dSGreg Kurz dirfd = local_opendir_nofollow(s, dirpath); 976a33eda0dSGreg Kurz if (dirfd == -1) { 977a33eda0dSGreg Kurz goto out; 978a33eda0dSGreg Kurz } 979a33eda0dSGreg Kurz 980a33eda0dSGreg Kurz ret = utimensat(dirfd, name, buf, AT_SYMLINK_NOFOLLOW); 981a33eda0dSGreg Kurz close_preserve_errno(dirfd); 982a33eda0dSGreg Kurz out: 983a33eda0dSGreg Kurz g_free(dirpath); 984a33eda0dSGreg Kurz g_free(name); 985f00d4f59SWei Liu return ret; 986f00d4f59SWei Liu } 987f00d4f59SWei Liu 988df4938a6SGreg Kurz static int local_unlinkat_common(FsContext *ctx, int dirfd, const char *name, 989df4938a6SGreg Kurz int flags) 990df4938a6SGreg Kurz { 991df4938a6SGreg Kurz int ret = -1; 992df4938a6SGreg Kurz 993df4938a6SGreg Kurz if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 994df4938a6SGreg Kurz int map_dirfd; 995df4938a6SGreg Kurz 996df4938a6SGreg Kurz if (flags == AT_REMOVEDIR) { 997df4938a6SGreg Kurz int fd; 998df4938a6SGreg Kurz 999df4938a6SGreg Kurz fd = openat(dirfd, name, O_RDONLY | O_DIRECTORY | O_PATH); 1000df4938a6SGreg Kurz if (fd == -1) { 1001df4938a6SGreg Kurz goto err_out; 1002df4938a6SGreg Kurz } 1003df4938a6SGreg Kurz /* 1004df4938a6SGreg Kurz * If directory remove .virtfs_metadata contained in the 1005df4938a6SGreg Kurz * directory 1006df4938a6SGreg Kurz */ 1007df4938a6SGreg Kurz ret = unlinkat(fd, VIRTFS_META_DIR, AT_REMOVEDIR); 1008df4938a6SGreg Kurz close_preserve_errno(fd); 1009df4938a6SGreg Kurz if (ret < 0 && errno != ENOENT) { 1010df4938a6SGreg Kurz /* 1011df4938a6SGreg Kurz * We didn't had the .virtfs_metadata file. May be file created 1012df4938a6SGreg Kurz * in non-mapped mode ?. Ignore ENOENT. 1013df4938a6SGreg Kurz */ 1014df4938a6SGreg Kurz goto err_out; 1015df4938a6SGreg Kurz } 1016df4938a6SGreg Kurz } 1017df4938a6SGreg Kurz /* 1018df4938a6SGreg Kurz * Now remove the name from parent directory 1019df4938a6SGreg Kurz * .virtfs_metadata directory. 1020df4938a6SGreg Kurz */ 1021df4938a6SGreg Kurz map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR); 1022df4938a6SGreg Kurz ret = unlinkat(map_dirfd, name, 0); 1023df4938a6SGreg Kurz close_preserve_errno(map_dirfd); 1024df4938a6SGreg Kurz if (ret < 0 && errno != ENOENT) { 1025df4938a6SGreg Kurz /* 1026df4938a6SGreg Kurz * We didn't had the .virtfs_metadata file. May be file created 1027df4938a6SGreg Kurz * in non-mapped mode ?. Ignore ENOENT. 1028df4938a6SGreg Kurz */ 1029df4938a6SGreg Kurz goto err_out; 1030df4938a6SGreg Kurz } 1031df4938a6SGreg Kurz } 1032df4938a6SGreg Kurz 1033df4938a6SGreg Kurz ret = unlinkat(dirfd, name, flags); 1034df4938a6SGreg Kurz err_out: 1035df4938a6SGreg Kurz return ret; 1036df4938a6SGreg Kurz } 1037df4938a6SGreg Kurz 1038f00d4f59SWei Liu static int local_remove(FsContext *ctx, const char *path) 1039f00d4f59SWei Liu { 1040f00d4f59SWei Liu struct stat stbuf; 1041a0e640a8SGreg Kurz char *dirpath = g_path_get_dirname(path); 1042a0e640a8SGreg Kurz char *name = g_path_get_basename(path); 1043a0e640a8SGreg Kurz int flags = 0; 1044a0e640a8SGreg Kurz int dirfd; 1045a0e640a8SGreg Kurz int err = -1; 1046f00d4f59SWei Liu 1047a0e640a8SGreg Kurz dirfd = local_opendir_nofollow(ctx, dirpath); 1048a0e640a8SGreg Kurz if (dirfd) { 1049a0e640a8SGreg Kurz goto out; 1050a0e640a8SGreg Kurz } 1051a0e640a8SGreg Kurz 1052a0e640a8SGreg Kurz if (fstatat(dirfd, path, &stbuf, AT_SYMLINK_NOFOLLOW) < 0) { 1053f00d4f59SWei Liu goto err_out; 1054f00d4f59SWei Liu } 1055a0e640a8SGreg Kurz 1056f00d4f59SWei Liu if (S_ISDIR(stbuf.st_mode)) { 1057a0e640a8SGreg Kurz flags |= AT_REMOVEDIR; 1058f00d4f59SWei Liu } 1059f00d4f59SWei Liu 1060a0e640a8SGreg Kurz err = local_unlinkat_common(ctx, dirfd, name, flags); 1061f00d4f59SWei Liu err_out: 1062a0e640a8SGreg Kurz close_preserve_errno(dirfd); 1063a0e640a8SGreg Kurz out: 1064a0e640a8SGreg Kurz g_free(name); 1065a0e640a8SGreg Kurz g_free(dirpath); 1066f00d4f59SWei Liu return err; 1067f00d4f59SWei Liu } 1068f00d4f59SWei Liu 1069f00d4f59SWei Liu static int local_fsync(FsContext *ctx, int fid_type, 1070f00d4f59SWei Liu V9fsFidOpenState *fs, int datasync) 1071f00d4f59SWei Liu { 1072f00d4f59SWei Liu int fd; 1073f00d4f59SWei Liu 1074f00d4f59SWei Liu if (fid_type == P9_FID_DIR) { 1075f314ea4eSGreg Kurz fd = dirfd(fs->dir.stream); 1076f00d4f59SWei Liu } else { 1077f00d4f59SWei Liu fd = fs->fd; 1078f00d4f59SWei Liu } 1079f00d4f59SWei Liu 1080f00d4f59SWei Liu if (datasync) { 1081f00d4f59SWei Liu return qemu_fdatasync(fd); 1082f00d4f59SWei Liu } else { 1083f00d4f59SWei Liu return fsync(fd); 1084f00d4f59SWei Liu } 1085f00d4f59SWei Liu } 1086f00d4f59SWei Liu 1087f00d4f59SWei Liu static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf) 1088f00d4f59SWei Liu { 108931e51d1cSGreg Kurz int fd, ret; 1090f00d4f59SWei Liu 109131e51d1cSGreg Kurz fd = local_open_nofollow(s, fs_path->data, O_RDONLY, 0); 109231e51d1cSGreg Kurz ret = fstatfs(fd, stbuf); 109331e51d1cSGreg Kurz close_preserve_errno(fd); 1094f00d4f59SWei Liu return ret; 1095f00d4f59SWei Liu } 1096f00d4f59SWei Liu 1097f00d4f59SWei Liu static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path, 1098f00d4f59SWei Liu const char *name, void *value, size_t size) 1099f00d4f59SWei Liu { 1100f00d4f59SWei Liu char *path = fs_path->data; 1101f00d4f59SWei Liu 1102f00d4f59SWei Liu return v9fs_get_xattr(ctx, path, name, value, size); 1103f00d4f59SWei Liu } 1104f00d4f59SWei Liu 1105f00d4f59SWei Liu static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path, 1106f00d4f59SWei Liu void *value, size_t size) 1107f00d4f59SWei Liu { 1108f00d4f59SWei Liu char *path = fs_path->data; 1109f00d4f59SWei Liu 1110f00d4f59SWei Liu return v9fs_list_xattr(ctx, path, value, size); 1111f00d4f59SWei Liu } 1112f00d4f59SWei Liu 1113f00d4f59SWei Liu static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name, 1114f00d4f59SWei Liu void *value, size_t size, int flags) 1115f00d4f59SWei Liu { 1116f00d4f59SWei Liu char *path = fs_path->data; 1117f00d4f59SWei Liu 1118f00d4f59SWei Liu return v9fs_set_xattr(ctx, path, name, value, size, flags); 1119f00d4f59SWei Liu } 1120f00d4f59SWei Liu 1121f00d4f59SWei Liu static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path, 1122f00d4f59SWei Liu const char *name) 1123f00d4f59SWei Liu { 1124f00d4f59SWei Liu char *path = fs_path->data; 1125f00d4f59SWei Liu 1126f00d4f59SWei Liu return v9fs_remove_xattr(ctx, path, name); 1127f00d4f59SWei Liu } 1128f00d4f59SWei Liu 1129f00d4f59SWei Liu static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path, 1130f00d4f59SWei Liu const char *name, V9fsPath *target) 1131f00d4f59SWei Liu { 1132f00d4f59SWei Liu if (dir_path) { 1133e3e83f2eSGreg Kurz v9fs_path_sprintf(target, "%s/%s", dir_path->data, name); 1134f00d4f59SWei Liu } else { 1135e3e83f2eSGreg Kurz v9fs_path_sprintf(target, "%s", name); 1136f00d4f59SWei Liu } 1137f00d4f59SWei Liu return 0; 1138f00d4f59SWei Liu } 1139f00d4f59SWei Liu 1140f00d4f59SWei Liu static int local_renameat(FsContext *ctx, V9fsPath *olddir, 1141f00d4f59SWei Liu const char *old_name, V9fsPath *newdir, 1142f00d4f59SWei Liu const char *new_name) 1143f00d4f59SWei Liu { 1144f00d4f59SWei Liu int ret; 1145f00d4f59SWei Liu V9fsString old_full_name, new_full_name; 1146f00d4f59SWei Liu 1147f00d4f59SWei Liu v9fs_string_init(&old_full_name); 1148f00d4f59SWei Liu v9fs_string_init(&new_full_name); 1149f00d4f59SWei Liu 1150f00d4f59SWei Liu v9fs_string_sprintf(&old_full_name, "%s/%s", olddir->data, old_name); 1151f00d4f59SWei Liu v9fs_string_sprintf(&new_full_name, "%s/%s", newdir->data, new_name); 1152f00d4f59SWei Liu 1153f00d4f59SWei Liu ret = local_rename(ctx, old_full_name.data, new_full_name.data); 1154f00d4f59SWei Liu v9fs_string_free(&old_full_name); 1155f00d4f59SWei Liu v9fs_string_free(&new_full_name); 1156f00d4f59SWei Liu return ret; 1157f00d4f59SWei Liu } 1158f00d4f59SWei Liu 1159f00d4f59SWei Liu static int local_unlinkat(FsContext *ctx, V9fsPath *dir, 1160f00d4f59SWei Liu const char *name, int flags) 1161f00d4f59SWei Liu { 1162f00d4f59SWei Liu int ret; 1163df4938a6SGreg Kurz int dirfd; 1164f00d4f59SWei Liu 1165df4938a6SGreg Kurz dirfd = local_opendir_nofollow(ctx, dir->data); 1166df4938a6SGreg Kurz if (dirfd == -1) { 1167df4938a6SGreg Kurz return -1; 1168df4938a6SGreg Kurz } 1169f00d4f59SWei Liu 1170df4938a6SGreg Kurz ret = local_unlinkat_common(ctx, dirfd, name, flags); 1171df4938a6SGreg Kurz close_preserve_errno(dirfd); 1172f00d4f59SWei Liu return ret; 1173f00d4f59SWei Liu } 1174f00d4f59SWei Liu 1175f00d4f59SWei Liu static int local_ioc_getversion(FsContext *ctx, V9fsPath *path, 1176f00d4f59SWei Liu mode_t st_mode, uint64_t *st_gen) 1177f00d4f59SWei Liu { 1178f00d4f59SWei Liu #ifdef FS_IOC_GETVERSION 1179f00d4f59SWei Liu int err; 1180f00d4f59SWei Liu V9fsFidOpenState fid_open; 1181f00d4f59SWei Liu 1182f00d4f59SWei Liu /* 1183f00d4f59SWei Liu * Do not try to open special files like device nodes, fifos etc 1184f00d4f59SWei Liu * We can get fd for regular files and directories only 1185f00d4f59SWei Liu */ 1186f00d4f59SWei Liu if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) { 1187f00d4f59SWei Liu errno = ENOTTY; 1188f00d4f59SWei Liu return -1; 1189f00d4f59SWei Liu } 1190f00d4f59SWei Liu err = local_open(ctx, path, O_RDONLY, &fid_open); 1191f00d4f59SWei Liu if (err < 0) { 1192f00d4f59SWei Liu return err; 1193f00d4f59SWei Liu } 1194f00d4f59SWei Liu err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen); 1195f00d4f59SWei Liu local_close(ctx, &fid_open); 1196f00d4f59SWei Liu return err; 1197f00d4f59SWei Liu #else 1198f00d4f59SWei Liu errno = ENOTTY; 1199f00d4f59SWei Liu return -1; 1200f00d4f59SWei Liu #endif 1201f00d4f59SWei Liu } 1202f00d4f59SWei Liu 1203f00d4f59SWei Liu static int local_init(FsContext *ctx) 1204f00d4f59SWei Liu { 1205f00d4f59SWei Liu struct statfs stbuf; 12060e35a378SGreg Kurz LocalData *data = g_malloc(sizeof(*data)); 12070e35a378SGreg Kurz 12080e35a378SGreg Kurz data->mountfd = open(ctx->fs_root, O_DIRECTORY | O_RDONLY); 12090e35a378SGreg Kurz if (data->mountfd == -1) { 12100e35a378SGreg Kurz goto err; 12110e35a378SGreg Kurz } 1212f00d4f59SWei Liu 121300c90bd1SGreg Kurz #ifdef FS_IOC_GETVERSION 121400c90bd1SGreg Kurz /* 121500c90bd1SGreg Kurz * use ioc_getversion only if the ioctl is definied 121600c90bd1SGreg Kurz */ 12170e35a378SGreg Kurz if (fstatfs(data->mountfd, &stbuf) < 0) { 12180e35a378SGreg Kurz close_preserve_errno(data->mountfd); 12190e35a378SGreg Kurz goto err; 122000c90bd1SGreg Kurz } 122100c90bd1SGreg Kurz switch (stbuf.f_type) { 122200c90bd1SGreg Kurz case EXT2_SUPER_MAGIC: 122300c90bd1SGreg Kurz case BTRFS_SUPER_MAGIC: 122400c90bd1SGreg Kurz case REISERFS_SUPER_MAGIC: 122500c90bd1SGreg Kurz case XFS_SUPER_MAGIC: 122600c90bd1SGreg Kurz ctx->exops.get_st_gen = local_ioc_getversion; 122700c90bd1SGreg Kurz break; 122800c90bd1SGreg Kurz } 122900c90bd1SGreg Kurz #endif 123000c90bd1SGreg Kurz 1231f00d4f59SWei Liu if (ctx->export_flags & V9FS_SM_PASSTHROUGH) { 1232f00d4f59SWei Liu ctx->xops = passthrough_xattr_ops; 1233f00d4f59SWei Liu } else if (ctx->export_flags & V9FS_SM_MAPPED) { 1234f00d4f59SWei Liu ctx->xops = mapped_xattr_ops; 1235f00d4f59SWei Liu } else if (ctx->export_flags & V9FS_SM_NONE) { 1236f00d4f59SWei Liu ctx->xops = none_xattr_ops; 1237f00d4f59SWei Liu } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 1238f00d4f59SWei Liu /* 1239f00d4f59SWei Liu * xattr operation for mapped-file and passthrough 1240f00d4f59SWei Liu * remain same. 1241f00d4f59SWei Liu */ 1242f00d4f59SWei Liu ctx->xops = passthrough_xattr_ops; 1243f00d4f59SWei Liu } 1244f00d4f59SWei Liu ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT; 124500c90bd1SGreg Kurz 12460e35a378SGreg Kurz ctx->private = data; 124700c90bd1SGreg Kurz return 0; 12480e35a378SGreg Kurz 12490e35a378SGreg Kurz err: 12500e35a378SGreg Kurz g_free(data); 12510e35a378SGreg Kurz return -1; 12520e35a378SGreg Kurz } 12530e35a378SGreg Kurz 12540e35a378SGreg Kurz static void local_cleanup(FsContext *ctx) 12550e35a378SGreg Kurz { 12560e35a378SGreg Kurz LocalData *data = ctx->private; 12570e35a378SGreg Kurz 12580e35a378SGreg Kurz close(data->mountfd); 12590e35a378SGreg Kurz g_free(data); 1260f00d4f59SWei Liu } 1261f00d4f59SWei Liu 1262f00d4f59SWei Liu static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse) 1263f00d4f59SWei Liu { 1264f00d4f59SWei Liu const char *sec_model = qemu_opt_get(opts, "security_model"); 1265f00d4f59SWei Liu const char *path = qemu_opt_get(opts, "path"); 1266f00d4f59SWei Liu 1267f00d4f59SWei Liu if (!sec_model) { 126863325b18SGreg Kurz error_report("Security model not specified, local fs needs security model"); 126963325b18SGreg Kurz error_printf("valid options are:" 127063325b18SGreg Kurz "\tsecurity_model=[passthrough|mapped-xattr|mapped-file|none]\n"); 1271f00d4f59SWei Liu return -1; 1272f00d4f59SWei Liu } 1273f00d4f59SWei Liu 1274f00d4f59SWei Liu if (!strcmp(sec_model, "passthrough")) { 1275f00d4f59SWei Liu fse->export_flags |= V9FS_SM_PASSTHROUGH; 1276f00d4f59SWei Liu } else if (!strcmp(sec_model, "mapped") || 1277f00d4f59SWei Liu !strcmp(sec_model, "mapped-xattr")) { 1278f00d4f59SWei Liu fse->export_flags |= V9FS_SM_MAPPED; 1279f00d4f59SWei Liu } else if (!strcmp(sec_model, "none")) { 1280f00d4f59SWei Liu fse->export_flags |= V9FS_SM_NONE; 1281f00d4f59SWei Liu } else if (!strcmp(sec_model, "mapped-file")) { 1282f00d4f59SWei Liu fse->export_flags |= V9FS_SM_MAPPED_FILE; 1283f00d4f59SWei Liu } else { 128463325b18SGreg Kurz error_report("Invalid security model %s specified", sec_model); 128563325b18SGreg Kurz error_printf("valid options are:" 128663325b18SGreg Kurz "\t[passthrough|mapped-xattr|mapped-file|none]\n"); 1287f00d4f59SWei Liu return -1; 1288f00d4f59SWei Liu } 1289f00d4f59SWei Liu 1290f00d4f59SWei Liu if (!path) { 129163325b18SGreg Kurz error_report("fsdev: No path specified"); 1292f00d4f59SWei Liu return -1; 1293f00d4f59SWei Liu } 1294f00d4f59SWei Liu fse->path = g_strdup(path); 1295f00d4f59SWei Liu 1296f00d4f59SWei Liu return 0; 1297f00d4f59SWei Liu } 1298f00d4f59SWei Liu 1299f00d4f59SWei Liu FileOperations local_ops = { 1300f00d4f59SWei Liu .parse_opts = local_parse_opts, 1301f00d4f59SWei Liu .init = local_init, 13020e35a378SGreg Kurz .cleanup = local_cleanup, 1303f00d4f59SWei Liu .lstat = local_lstat, 1304f00d4f59SWei Liu .readlink = local_readlink, 1305f00d4f59SWei Liu .close = local_close, 1306f00d4f59SWei Liu .closedir = local_closedir, 1307f00d4f59SWei Liu .open = local_open, 1308f00d4f59SWei Liu .opendir = local_opendir, 1309f00d4f59SWei Liu .rewinddir = local_rewinddir, 1310f00d4f59SWei Liu .telldir = local_telldir, 1311635324e8SGreg Kurz .readdir = local_readdir, 1312f00d4f59SWei Liu .seekdir = local_seekdir, 1313f00d4f59SWei Liu .preadv = local_preadv, 1314f00d4f59SWei Liu .pwritev = local_pwritev, 1315f00d4f59SWei Liu .chmod = local_chmod, 1316f00d4f59SWei Liu .mknod = local_mknod, 1317f00d4f59SWei Liu .mkdir = local_mkdir, 1318f00d4f59SWei Liu .fstat = local_fstat, 1319f00d4f59SWei Liu .open2 = local_open2, 1320f00d4f59SWei Liu .symlink = local_symlink, 1321f00d4f59SWei Liu .link = local_link, 1322f00d4f59SWei Liu .truncate = local_truncate, 1323f00d4f59SWei Liu .rename = local_rename, 1324f00d4f59SWei Liu .chown = local_chown, 1325f00d4f59SWei Liu .utimensat = local_utimensat, 1326f00d4f59SWei Liu .remove = local_remove, 1327f00d4f59SWei Liu .fsync = local_fsync, 1328f00d4f59SWei Liu .statfs = local_statfs, 1329f00d4f59SWei Liu .lgetxattr = local_lgetxattr, 1330f00d4f59SWei Liu .llistxattr = local_llistxattr, 1331f00d4f59SWei Liu .lsetxattr = local_lsetxattr, 1332f00d4f59SWei Liu .lremovexattr = local_lremovexattr, 1333f00d4f59SWei Liu .name_to_path = local_name_to_path, 1334f00d4f59SWei Liu .renameat = local_renameat, 1335f00d4f59SWei Liu .unlinkat = local_unlinkat, 1336f00d4f59SWei Liu }; 1337