9p-local.c 41.5 KB
Newer Older
1
/*
2
 * 9p Posix callback
3 4 5 6 7 8 9 10 11 12
 *
 * Copyright IBM, Corp. 2010
 *
 * Authors:
 *  Anthony Liguori   <aliguori@us.ibm.com>
 *
 * This work is licensed under the terms of the GNU GPL, version 2.  See
 * the COPYING file in the top-level directory.
 *
 */
13

P
Peter Maydell 已提交
14
#include "qemu/osdep.h"
W
Wei Liu 已提交
15
#include "9p.h"
16
#include "9p-local.h"
17
#include "9p-xattr.h"
18
#include "9p-util.h"
19
#include "fsdev/qemu-fsdev.h"   /* local_ops */
20
#include <arpa/inet.h>
21 22
#include <pwd.h>
#include <grp.h>
23 24
#include <sys/socket.h>
#include <sys/un.h>
25
#include "qemu/xattr.h"
26
#include "qemu/cutils.h"
27
#include "qemu/error-report.h"
28
#include <libgen.h>
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#include <linux/fs.h>
#ifdef CONFIG_LINUX_MAGIC_H
#include <linux/magic.h>
#endif
#include <sys/ioctl.h>

#ifndef XFS_SUPER_MAGIC
#define XFS_SUPER_MAGIC  0x58465342
#endif
#ifndef EXT2_SUPER_MAGIC
#define EXT2_SUPER_MAGIC 0xEF53
#endif
#ifndef REISERFS_SUPER_MAGIC
#define REISERFS_SUPER_MAGIC 0x52654973
#endif
#ifndef BTRFS_SUPER_MAGIC
#define BTRFS_SUPER_MAGIC 0x9123683E
#endif
47

48 49 50 51
typedef struct {
    int mountfd;
} LocalData;

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
int local_open_nofollow(FsContext *fs_ctx, const char *path, int flags,
                        mode_t mode)
{
    LocalData *data = fs_ctx->private;

    /* All paths are relative to the path data->mountfd points to */
    while (*path == '/') {
        path++;
    }

    return relative_openat_nofollow(data->mountfd, path, flags, mode);
}

int local_opendir_nofollow(FsContext *fs_ctx, const char *path)
{
    return local_open_nofollow(fs_ctx, path, O_DIRECTORY | O_RDONLY, 0);
}

70 71 72 73 74 75 76 77
static void renameat_preserve_errno(int odirfd, const char *opath, int ndirfd,
                                    const char *npath)
{
    int serrno = errno;
    renameat(odirfd, opath, ndirfd, npath);
    errno = serrno;
}

78 79 80 81 82 83 84
static void unlinkat_preserve_errno(int dirfd, const char *path, int flags)
{
    int serrno = errno;
    unlinkat(dirfd, path, flags);
    errno = serrno;
}

85 86
#define VIRTFS_META_DIR ".virtfs_metadata"

87
static char *local_mapped_attr_path(FsContext *ctx, const char *path)
88
{
89 90 91 92 93 94 95 96 97 98 99
    int dirlen;
    const char *name = strrchr(path, '/');
    if (name) {
        dirlen = name - path;
        ++name;
    } else {
        name = path;
        dirlen = 0;
    }
    return g_strdup_printf("%s/%.*s/%s/%s", ctx->fs_root,
                           dirlen, path, VIRTFS_META_DIR, name);
100 101
}

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
static FILE *local_fopen(const char *path, const char *mode)
{
    int fd, o_mode = 0;
    FILE *fp;
    int flags = O_NOFOLLOW;
    /*
     * only supports two modes
     */
    if (mode[0] == 'r') {
        flags |= O_RDONLY;
    } else if (mode[0] == 'w') {
        flags |= O_WRONLY | O_TRUNC | O_CREAT;
        o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
    } else {
        return NULL;
    }
    fd = open(path, flags, o_mode);
    if (fd == -1) {
        return NULL;
    }
    fp = fdopen(fd, mode);
    if (!fp) {
        close(fd);
    }
    return fp;
}

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
static FILE *local_fopenat(int dirfd, const char *name, const char *mode)
{
    int fd, o_mode = 0;
    FILE *fp;
    int flags;
    /*
     * only supports two modes
     */
    if (mode[0] == 'r') {
        flags = O_RDONLY;
    } else if (mode[0] == 'w') {
        flags = O_WRONLY | O_TRUNC | O_CREAT;
        o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
    } else {
        return NULL;
    }
    fd = openat_file(dirfd, name, flags, o_mode);
    if (fd == -1) {
        return NULL;
    }
    fp = fdopen(fd, mode);
    if (!fp) {
        close(fd);
    }
    return fp;
}

156
#define ATTR_MAX 100
157
static void local_mapped_file_attr(int dirfd, const char *name,
158 159 160 161
                                   struct stat *stbuf)
{
    FILE *fp;
    char buf[ATTR_MAX];
162 163
    int map_dirfd;

164
    map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
165 166 167
    if (map_dirfd == -1) {
        return;
    }
168

169 170
    fp = local_fopenat(map_dirfd, name, "r");
    close_preserve_errno(map_dirfd);
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
    if (!fp) {
        return;
    }
    memset(buf, 0, ATTR_MAX);
    while (fgets(buf, ATTR_MAX, fp)) {
        if (!strncmp(buf, "virtfs.uid", 10)) {
            stbuf->st_uid = atoi(buf+11);
        } else if (!strncmp(buf, "virtfs.gid", 10)) {
            stbuf->st_gid = atoi(buf+11);
        } else if (!strncmp(buf, "virtfs.mode", 11)) {
            stbuf->st_mode = atoi(buf+12);
        } else if (!strncmp(buf, "virtfs.rdev", 11)) {
            stbuf->st_rdev = atoi(buf+12);
        }
        memset(buf, 0, ATTR_MAX);
    }
    fclose(fp);
}

190
static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf)
191
{
192 193 194 195 196 197 198 199 200
    int err = -1;
    char *dirpath = g_path_get_dirname(fs_path->data);
    char *name = g_path_get_basename(fs_path->data);
    int dirfd;

    dirfd = local_opendir_nofollow(fs_ctx, dirpath);
    if (dirfd == -1) {
        goto out;
    }
201

202
    err = fstatat(dirfd, name, stbuf, AT_SYMLINK_NOFOLLOW);
203
    if (err) {
204
        goto err_out;
205
    }
206
    if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
207 208 209 210 211
        /* Actual credentials are part of extended attrs */
        uid_t tmp_uid;
        gid_t tmp_gid;
        mode_t tmp_mode;
        dev_t tmp_dev;
212 213 214

        if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.uid", &tmp_uid,
                                 sizeof(uid_t)) > 0) {
215
            stbuf->st_uid = le32_to_cpu(tmp_uid);
216
        }
217 218
        if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.gid", &tmp_gid,
                                 sizeof(gid_t)) > 0) {
219
            stbuf->st_gid = le32_to_cpu(tmp_gid);
220
        }
221 222
        if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.mode", &tmp_mode,
                                 sizeof(mode_t)) > 0) {
223
            stbuf->st_mode = le32_to_cpu(tmp_mode);
224
        }
225 226
        if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.rdev", &tmp_dev,
                                 sizeof(dev_t)) > 0) {
227
            stbuf->st_rdev = le64_to_cpu(tmp_dev);
228
        }
229
    } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
230
        local_mapped_file_attr(dirfd, name, stbuf);
231
    }
232 233

err_out:
234 235 236 237
    close_preserve_errno(dirfd);
out:
    g_free(name);
    g_free(dirpath);
238 239 240 241 242 243
    return err;
}

static int local_create_mapped_attr_dir(FsContext *ctx, const char *path)
{
    int err;
244
    char *attr_dir;
245
    char *tmp_path = g_strdup(path);
246

247
    attr_dir = g_strdup_printf("%s/%s/%s",
248 249 250 251 252
             ctx->fs_root, dirname(tmp_path), VIRTFS_META_DIR);

    err = mkdir(attr_dir, 0700);
    if (err < 0 && errno == EEXIST) {
        err = 0;
253
    }
254
    g_free(attr_dir);
255
    g_free(tmp_path);
256
    return err;
257 258
}

259 260 261 262 263 264
static int local_set_mapped_file_attr(FsContext *ctx,
                                      const char *path, FsCred *credp)
{
    FILE *fp;
    int ret = 0;
    char buf[ATTR_MAX];
265
    char *attr_path;
266 267
    int uid = -1, gid = -1, mode = -1, rdev = -1;

268 269
    attr_path = local_mapped_attr_path(ctx, path);
    fp = local_fopen(attr_path, "r");
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
    if (!fp) {
        goto create_map_file;
    }
    memset(buf, 0, ATTR_MAX);
    while (fgets(buf, ATTR_MAX, fp)) {
        if (!strncmp(buf, "virtfs.uid", 10)) {
            uid = atoi(buf+11);
        } else if (!strncmp(buf, "virtfs.gid", 10)) {
            gid = atoi(buf+11);
        } else if (!strncmp(buf, "virtfs.mode", 11)) {
            mode = atoi(buf+12);
        } else if (!strncmp(buf, "virtfs.rdev", 11)) {
            rdev = atoi(buf+12);
        }
        memset(buf, 0, ATTR_MAX);
    }
    fclose(fp);
    goto update_map_file;

create_map_file:
    ret = local_create_mapped_attr_dir(ctx, path);
    if (ret < 0) {
        goto err_out;
    }

update_map_file:
296
    fp = local_fopen(attr_path, "w");
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
    if (!fp) {
        ret = -1;
        goto err_out;
    }

    if (credp->fc_uid != -1) {
        uid = credp->fc_uid;
    }
    if (credp->fc_gid != -1) {
        gid = credp->fc_gid;
    }
    if (credp->fc_mode != -1) {
        mode = credp->fc_mode;
    }
    if (credp->fc_rdev != -1) {
        rdev = credp->fc_rdev;
    }


    if (uid != -1) {
        fprintf(fp, "virtfs.uid=%d\n", uid);
    }
    if (gid != -1) {
        fprintf(fp, "virtfs.gid=%d\n", gid);
    }
    if (mode != -1) {
        fprintf(fp, "virtfs.mode=%d\n", mode);
    }
    if (rdev != -1) {
        fprintf(fp, "virtfs.rdev=%d\n", rdev);
    }
    fclose(fp);

err_out:
331
    g_free(attr_path);
332 333 334
    return ret;
}

335
static int local_set_xattr(const char *path, FsCred *credp)
336
{
337
    int err;
338

339
    if (credp->fc_uid != -1) {
340 341
        uint32_t tmp_uid = cpu_to_le32(credp->fc_uid);
        err = setxattr(path, "user.virtfs.uid", &tmp_uid, sizeof(uid_t), 0);
342 343 344
        if (err) {
            return err;
        }
345
    }
346
    if (credp->fc_gid != -1) {
347 348
        uint32_t tmp_gid = cpu_to_le32(credp->fc_gid);
        err = setxattr(path, "user.virtfs.gid", &tmp_gid, sizeof(gid_t), 0);
349 350 351
        if (err) {
            return err;
        }
352
    }
353
    if (credp->fc_mode != -1) {
354 355
        uint32_t tmp_mode = cpu_to_le32(credp->fc_mode);
        err = setxattr(path, "user.virtfs.mode", &tmp_mode, sizeof(mode_t), 0);
356 357 358
        if (err) {
            return err;
        }
359
    }
360
    if (credp->fc_rdev != -1) {
361 362
        uint64_t tmp_rdev = cpu_to_le64(credp->fc_rdev);
        err = setxattr(path, "user.virtfs.rdev", &tmp_rdev, sizeof(dev_t), 0);
363 364 365
        if (err) {
            return err;
        }
366 367 368 369
    }
    return 0;
}

370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
static int local_set_mapped_file_attrat(int dirfd, const char *name,
                                        FsCred *credp)
{
    FILE *fp;
    int ret;
    char buf[ATTR_MAX];
    int uid = -1, gid = -1, mode = -1, rdev = -1;
    int map_dirfd;

    ret = mkdirat(dirfd, VIRTFS_META_DIR, 0700);
    if (ret < 0 && errno != EEXIST) {
        return -1;
    }

    map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
    if (map_dirfd == -1) {
        return -1;
    }

    fp = local_fopenat(map_dirfd, name, "r");
    if (!fp) {
        if (errno == ENOENT) {
            goto update_map_file;
        } else {
            close_preserve_errno(map_dirfd);
            return -1;
        }
    }
    memset(buf, 0, ATTR_MAX);
    while (fgets(buf, ATTR_MAX, fp)) {
        if (!strncmp(buf, "virtfs.uid", 10)) {
            uid = atoi(buf + 11);
        } else if (!strncmp(buf, "virtfs.gid", 10)) {
            gid = atoi(buf + 11);
        } else if (!strncmp(buf, "virtfs.mode", 11)) {
            mode = atoi(buf + 12);
        } else if (!strncmp(buf, "virtfs.rdev", 11)) {
            rdev = atoi(buf + 12);
        }
        memset(buf, 0, ATTR_MAX);
    }
    fclose(fp);

update_map_file:
    fp = local_fopenat(map_dirfd, name, "w");
    close_preserve_errno(map_dirfd);
    if (!fp) {
        return -1;
    }

    if (credp->fc_uid != -1) {
        uid = credp->fc_uid;
    }
    if (credp->fc_gid != -1) {
        gid = credp->fc_gid;
    }
    if (credp->fc_mode != -1) {
        mode = credp->fc_mode;
    }
    if (credp->fc_rdev != -1) {
        rdev = credp->fc_rdev;
    }

    if (uid != -1) {
        fprintf(fp, "virtfs.uid=%d\n", uid);
    }
    if (gid != -1) {
        fprintf(fp, "virtfs.gid=%d\n", gid);
    }
    if (mode != -1) {
        fprintf(fp, "virtfs.mode=%d\n", mode);
    }
    if (rdev != -1) {
        fprintf(fp, "virtfs.rdev=%d\n", rdev);
    }
    fclose(fp);

    return 0;
}

static int fchmodat_nofollow(int dirfd, const char *name, mode_t mode)
{
    int fd, ret;

    /* FIXME: this should be handled with fchmodat(AT_SYMLINK_NOFOLLOW).
     * Unfortunately, the linux kernel doesn't implement it yet. As an
     * alternative, let's open the file and use fchmod() instead. This
     * may fail depending on the permissions of the file, but it is the
     * best we can do to avoid TOCTTOU. We first try to open read-only
     * in case name points to a directory. If that fails, we try write-only
     * in case name doesn't point to a directory.
     */
    fd = openat_file(dirfd, name, O_RDONLY, 0);
    if (fd == -1) {
        /* In case the file is writable-only and isn't a directory. */
        if (errno == EACCES) {
            fd = openat_file(dirfd, name, O_WRONLY, 0);
        }
        if (fd == -1 && errno == EISDIR) {
            errno = EACCES;
        }
    }
    if (fd == -1) {
        return -1;
    }
    ret = fchmod(fd, mode);
    close_preserve_errno(fd);
    return ret;
}

static int local_set_xattrat(int dirfd, const char *path, FsCred *credp)
{
    int err;

    if (credp->fc_uid != -1) {
        uint32_t tmp_uid = cpu_to_le32(credp->fc_uid);
        err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.uid", &tmp_uid,
                                   sizeof(uid_t), 0);
        if (err) {
            return err;
        }
    }
    if (credp->fc_gid != -1) {
        uint32_t tmp_gid = cpu_to_le32(credp->fc_gid);
        err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.gid", &tmp_gid,
                                   sizeof(gid_t), 0);
        if (err) {
            return err;
        }
    }
    if (credp->fc_mode != -1) {
        uint32_t tmp_mode = cpu_to_le32(credp->fc_mode);
        err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.mode", &tmp_mode,
                                   sizeof(mode_t), 0);
        if (err) {
            return err;
        }
    }
    if (credp->fc_rdev != -1) {
        uint64_t tmp_rdev = cpu_to_le64(credp->fc_rdev);
        err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.rdev", &tmp_rdev,
                                   sizeof(dev_t), 0);
        if (err) {
            return err;
        }
    }
    return 0;
}

519
static int local_post_create_passthrough(FsContext *fs_ctx, const char *path,
520
                                         FsCred *credp)
521
{
522
    char *buffer;
523

524 525
    buffer = rpath(fs_ctx, path);
    if (lchown(buffer, credp->fc_uid, credp->fc_gid) < 0) {
526 527 528 529
        /*
         * If we fail to change ownership and if we are
         * using security model none. Ignore the error
         */
530
        if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
531
            goto err;
532
        }
533
    }
M
M. Mohan Kumar 已提交
534

535 536
    if (chmod(buffer, credp->fc_mode & 07777) < 0) {
        goto err;
M
M. Mohan Kumar 已提交
537
    }
538 539

    g_free(buffer);
540
    return 0;
541 542 543
err:
    g_free(buffer);
    return -1;
544 545
}

546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
static int local_set_cred_passthrough(FsContext *fs_ctx, int dirfd,
                                      const char *name, FsCred *credp)
{
    if (fchownat(dirfd, name, credp->fc_uid, credp->fc_gid,
                 AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH) < 0) {
        /*
         * If we fail to change ownership and if we are
         * using security model none. Ignore the error
         */
        if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
            return -1;
        }
    }

    return fchmodat_nofollow(dirfd, name, credp->fc_mode & 07777);
}

563 564
static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
                              char *buf, size_t bufsz)
565
{
566
    ssize_t tsize = -1;
567

568 569
    if ((fs_ctx->export_flags & V9FS_SM_MAPPED) ||
        (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
570
        int fd;
571 572

        fd = local_open_nofollow(fs_ctx, fs_path->data, O_RDONLY, 0);
573 574 575 576 577 578
        if (fd == -1) {
            return -1;
        }
        do {
            tsize = read(fd, (void *)buf, bufsz);
        } while (tsize == -1 && errno == EINTR);
579
        close_preserve_errno(fd);
580 581
    } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
               (fs_ctx->export_flags & V9FS_SM_NONE)) {
582 583 584 585 586 587 588 589 590 591 592 593 594 595
        char *dirpath = g_path_get_dirname(fs_path->data);
        char *name = g_path_get_basename(fs_path->data);
        int dirfd;

        dirfd = local_opendir_nofollow(fs_ctx, dirpath);
        if (dirfd == -1) {
            goto out;
        }

        tsize = readlinkat(dirfd, name, buf, bufsz);
        close_preserve_errno(dirfd);
    out:
        g_free(name);
        g_free(dirpath);
596 597
    }
    return tsize;
598 599
}

600
static int local_close(FsContext *ctx, V9fsFidOpenState *fs)
601
{
602
    return close(fs->fd);
603 604
}

605
static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs)
606
{
G
Greg Kurz 已提交
607
    return closedir(fs->dir.stream);
608
}
609

610 611
static int local_open(FsContext *ctx, V9fsPath *fs_path,
                      int flags, V9fsFidOpenState *fs)
612
{
613
    int fd;
614

615
    fd = local_open_nofollow(ctx, fs_path->data, flags, 0);
616 617 618 619
    if (fd == -1) {
        return -1;
    }
    fs->fd = fd;
620
    return fs->fd;
621 622
}

623 624
static int local_opendir(FsContext *ctx,
                         V9fsPath *fs_path, V9fsFidOpenState *fs)
625
{
626
    int dirfd;
627
    DIR *stream;
628

629 630 631 632 633 634
    dirfd = local_opendir_nofollow(ctx, fs_path->data);
    if (dirfd == -1) {
        return -1;
    }

    stream = fdopendir(dirfd);
635
    if (!stream) {
636 637
        return -1;
    }
638
    fs->dir.stream = stream;
639
    return 0;
640 641
}

642
static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
643
{
G
Greg Kurz 已提交
644
    rewinddir(fs->dir.stream);
645 646
}

647
static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs)
648
{
G
Greg Kurz 已提交
649
    return telldir(fs->dir.stream);
650 651
}

G
Greg Kurz 已提交
652
static struct dirent *local_readdir(FsContext *ctx, V9fsFidOpenState *fs)
653
{
G
Greg Kurz 已提交
654
    struct dirent *entry;
655 656

again:
G
Greg Kurz 已提交
657 658 659 660 661
    entry = readdir(fs->dir.stream);
    if (!entry) {
        return NULL;
    }

662 663 664
    if (ctx->export_flags & V9FS_SM_MAPPED) {
        entry->d_type = DT_UNKNOWN;
    } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
G
Greg Kurz 已提交
665
        if (!strcmp(entry->d_name, VIRTFS_META_DIR)) {
666 667 668
            /* skp the meta data directory */
            goto again;
        }
669
        entry->d_type = DT_UNKNOWN;
670
    }
G
Greg Kurz 已提交
671 672

    return entry;
673 674
}

675
static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
676
{
G
Greg Kurz 已提交
677
    seekdir(fs->dir.stream, off);
678 679
}

680 681
static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs,
                            const struct iovec *iov,
682
                            int iovcnt, off_t offset)
683
{
684
#ifdef CONFIG_PREADV
685
    return preadv(fs->fd, iov, iovcnt, offset);
686
#else
687
    int err = lseek(fs->fd, offset, SEEK_SET);
688 689 690
    if (err == -1) {
        return err;
    } else {
691
        return readv(fs->fd, iov, iovcnt);
692 693
    }
#endif
694 695
}

696 697
static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
                             const struct iovec *iov,
698
                             int iovcnt, off_t offset)
699
{
700
    ssize_t ret;
701
#ifdef CONFIG_PREADV
702
    ret = pwritev(fs->fd, iov, iovcnt, offset);
703
#else
704
    int err = lseek(fs->fd, offset, SEEK_SET);
705 706 707
    if (err == -1) {
        return err;
    } else {
708
        ret = writev(fs->fd, iov, iovcnt);
709 710
    }
#endif
711 712 713 714 715 716 717
#ifdef CONFIG_SYNC_FILE_RANGE
    if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
        /*
         * Initiate a writeback. This is not a data integrity sync.
         * We want to ensure that we don't leave dirty pages in the cache
         * after write when writeout=immediate is sepcified.
         */
718
        sync_file_range(fs->fd, offset, ret,
719 720 721 722
                        SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
    }
#endif
    return ret;
723 724
}

725
static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
726
{
727 728
    char *dirpath = g_path_get_dirname(fs_path->data);
    char *name = g_path_get_basename(fs_path->data);
729
    int ret = -1;
730 731 732 733 734 735
    int dirfd;

    dirfd = local_opendir_nofollow(fs_ctx, dirpath);
    if (dirfd == -1) {
        goto out;
    }
736

737
    if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
738
        ret = local_set_xattrat(dirfd, name, credp);
739
    } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
740 741 742 743
        ret = local_set_mapped_file_attrat(dirfd, name, credp);
    } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
               fs_ctx->export_flags & V9FS_SM_NONE) {
        ret = fchmodat_nofollow(dirfd, name, credp->fc_mode);
744
    }
745 746 747 748 749
    close_preserve_errno(dirfd);

out:
    g_free(dirpath);
    g_free(name);
750
    return ret;
751 752
}

753 754
static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
                       const char *name, FsCred *credp)
755
{
756
    int err = -1;
757
    int dirfd;
758

759 760 761 762
    dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
    if (dirfd == -1) {
        return -1;
    }
763

764 765 766
    if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
        fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
        err = mknodat(dirfd, name, SM_LOCAL_MODE_BITS | S_IFREG, 0);
767
        if (err == -1) {
768
            goto out;
769
        }
770

771 772 773 774
        if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
            err = local_set_xattrat(dirfd, name, credp);
        } else {
            err = local_set_mapped_file_attrat(dirfd, name, credp);
775 776 777 778
        }
        if (err == -1) {
            goto err_end;
        }
779 780 781
    } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
               fs_ctx->export_flags & V9FS_SM_NONE) {
        err = mknodat(dirfd, name, credp->fc_mode, credp->fc_rdev);
782
        if (err == -1) {
783
            goto out;
784
        }
785
        err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp);
786 787 788 789
        if (err == -1) {
            goto err_end;
        }
    }
790
    goto out;
791 792

err_end:
793
    unlinkat_preserve_errno(dirfd, name, 0);
794
out:
795
    close_preserve_errno(dirfd);
796
    return err;
797 798
}

799 800
static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
                       const char *name, FsCred *credp)
801
{
802
    char *path;
803 804
    int err = -1;
    int serrno = 0;
805
    V9fsString fullname;
806
    char *buffer = NULL;
807

808 809 810 811
    v9fs_string_init(&fullname);
    v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
    path = fullname.data;

812
    /* Determine the security model */
813
    if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
814 815
        buffer = rpath(fs_ctx, path);
        err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
816
        if (err == -1) {
817
            goto out;
818 819
        }
        credp->fc_mode = credp->fc_mode|S_IFDIR;
820
        err = local_set_xattr(buffer, credp);
821 822 823 824
        if (err == -1) {
            serrno = errno;
            goto err_end;
        }
825
    } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
826 827
        buffer = rpath(fs_ctx, path);
        err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
828 829 830 831 832 833 834 835 836
        if (err == -1) {
            goto out;
        }
        credp->fc_mode = credp->fc_mode|S_IFDIR;
        err = local_set_mapped_file_attr(fs_ctx, path, credp);
        if (err == -1) {
            serrno = errno;
            goto err_end;
        }
837 838
    } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
               (fs_ctx->export_flags & V9FS_SM_NONE)) {
839 840
        buffer = rpath(fs_ctx, path);
        err = mkdir(buffer, credp->fc_mode);
841
        if (err == -1) {
842
            goto out;
843 844 845 846 847 848 849
        }
        err = local_post_create_passthrough(fs_ctx, path, credp);
        if (err == -1) {
            serrno = errno;
            goto err_end;
        }
    }
850
    goto out;
851 852

err_end:
853
    remove(buffer);
854
    errno = serrno;
855
out:
856
    g_free(buffer);
857
    v9fs_string_free(&fullname);
858
    return err;
859 860
}

861
static int local_fstat(FsContext *fs_ctx, int fid_type,
862
                       V9fsFidOpenState *fs, struct stat *stbuf)
863
{
864 865 866
    int err, fd;

    if (fid_type == P9_FID_DIR) {
G
Greg Kurz 已提交
867
        fd = dirfd(fs->dir.stream);
868 869 870 871 872
    } else {
        fd = fs->fd;
    }

    err = fstat(fd, stbuf);
873 874 875
    if (err) {
        return err;
    }
876
    if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
877 878 879 880 881 882
        /* Actual credentials are part of extended attrs */
        uid_t tmp_uid;
        gid_t tmp_gid;
        mode_t tmp_mode;
        dev_t tmp_dev;

883 884
        if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) {
            stbuf->st_uid = le32_to_cpu(tmp_uid);
885
        }
886 887
        if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
            stbuf->st_gid = le32_to_cpu(tmp_gid);
888
        }
889 890
        if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) {
            stbuf->st_mode = le32_to_cpu(tmp_mode);
891
        }
892 893
        if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
            stbuf->st_rdev = le64_to_cpu(tmp_dev);
894
        }
895 896 897
    } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
        errno = EOPNOTSUPP;
        return -1;
898 899
    }
    return err;
900 901
}

902
static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
903
                       int flags, FsCred *credp, V9fsFidOpenState *fs)
904
{
905
    char *path;
906 907 908
    int fd = -1;
    int err = -1;
    int serrno = 0;
909
    V9fsString fullname;
910
    char *buffer = NULL;
911

912 913 914 915 916
    /*
     * Mark all the open to not follow symlinks
     */
    flags |= O_NOFOLLOW;

917 918 919 920
    v9fs_string_init(&fullname);
    v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
    path = fullname.data;

921
    /* Determine the security model */
922
    if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
923 924
        buffer = rpath(fs_ctx, path);
        fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
925
        if (fd == -1) {
926 927
            err = fd;
            goto out;
928 929 930
        }
        credp->fc_mode = credp->fc_mode|S_IFREG;
        /* Set cleint credentials in xattr */
931
        err = local_set_xattr(buffer, credp);
932 933 934 935
        if (err == -1) {
            serrno = errno;
            goto err_end;
        }
936
    } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
937 938
        buffer = rpath(fs_ctx, path);
        fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
939 940 941 942 943 944 945 946 947 948 949
        if (fd == -1) {
            err = fd;
            goto out;
        }
        credp->fc_mode = credp->fc_mode|S_IFREG;
        /* Set client credentials in .virtfs_metadata directory files */
        err = local_set_mapped_file_attr(fs_ctx, path, credp);
        if (err == -1) {
            serrno = errno;
            goto err_end;
        }
950 951
    } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
               (fs_ctx->export_flags & V9FS_SM_NONE)) {
952 953
        buffer = rpath(fs_ctx, path);
        fd = open(buffer, flags, credp->fc_mode);
954
        if (fd == -1) {
955 956
            err = fd;
            goto out;
957 958 959 960 961 962 963
        }
        err = local_post_create_passthrough(fs_ctx, path, credp);
        if (err == -1) {
            serrno = errno;
            goto err_end;
        }
    }
964
    err = fd;
965
    fs->fd = fd;
966
    goto out;
967 968 969

err_end:
    close(fd);
970
    remove(buffer);
971
    errno = serrno;
972
out:
973
    g_free(buffer);
974
    v9fs_string_free(&fullname);
975
    return err;
976 977
}

978

979
static int local_symlink(FsContext *fs_ctx, const char *oldpath,
980
                         V9fsPath *dir_path, const char *name, FsCred *credp)
981
{
982
    int err = -1;
983
    int dirfd;
984

985 986 987 988
    dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
    if (dirfd == -1) {
        return -1;
    }
989

990
    /* Determine the security model */
991 992
    if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
        fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
993 994
        int fd;
        ssize_t oldpath_size, write_size;
995 996 997

        fd = openat_file(dirfd, name, O_CREAT | O_EXCL | O_RDWR,
                         SM_LOCAL_MODE_BITS);
998
        if (fd == -1) {
999
            goto out;
1000 1001
        }
        /* Write the oldpath (target) to the file. */
1002
        oldpath_size = strlen(oldpath);
1003 1004 1005
        do {
            write_size = write(fd, (void *)oldpath, oldpath_size);
        } while (write_size == -1 && errno == EINTR);
1006
        close_preserve_errno(fd);
1007 1008 1009 1010 1011

        if (write_size != oldpath_size) {
            goto err_end;
        }
        /* Set cleint credentials in symlink's xattr */
1012
        credp->fc_mode = credp->fc_mode | S_IFLNK;
1013

1014 1015 1016 1017
        if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
            err = local_set_xattrat(dirfd, name, credp);
        } else {
            err = local_set_mapped_file_attrat(dirfd, name, credp);
1018 1019 1020 1021
        }
        if (err == -1) {
            goto err_end;
        }
1022 1023 1024
    } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
               fs_ctx->export_flags & V9FS_SM_NONE) {
        err = symlinkat(oldpath, dirfd, name);
1025
        if (err) {
1026
            goto out;
1027
        }
1028 1029
        err = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid,
                       AT_SYMLINK_NOFOLLOW);
1030
        if (err == -1) {
1031 1032 1033 1034
            /*
             * If we fail to change ownership and if we are
             * using security model none. Ignore the error
             */
1035
            if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
1036
                goto err_end;
1037
            } else {
1038
                err = 0;
1039
            }
1040 1041
        }
    }
1042
    goto out;
1043 1044

err_end:
1045
    unlinkat_preserve_errno(dirfd, name, 0);
1046
out:
1047
    close_preserve_errno(dirfd);
1048
    return err;
1049 1050
}

1051 1052
static int local_link(FsContext *ctx, V9fsPath *oldpath,
                      V9fsPath *dirpath, const char *name)
1053
{
1054 1055 1056 1057 1058 1059 1060 1061 1062
    char *odirpath = g_path_get_dirname(oldpath->data);
    char *oname = g_path_get_basename(oldpath->data);
    int ret = -1;
    int odirfd, ndirfd;

    odirfd = local_opendir_nofollow(ctx, odirpath);
    if (odirfd == -1) {
        goto out;
    }
1063

1064 1065 1066 1067 1068
    ndirfd = local_opendir_nofollow(ctx, dirpath->data);
    if (ndirfd == -1) {
        close_preserve_errno(odirfd);
        goto out;
    }
1069

1070
    ret = linkat(odirfd, oname, ndirfd, name, 0);
1071
    if (ret < 0) {
1072
        goto out_close;
1073
    }
1074 1075

    /* now link the virtfs_metadata files */
1076
    if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
        int omap_dirfd, nmap_dirfd;

        ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700);
        if (ret < 0 && errno != EEXIST) {
            goto err_undo_link;
        }

        omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR);
        if (omap_dirfd == -1) {
            goto err;
        }

        nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR);
        if (nmap_dirfd == -1) {
            close_preserve_errno(omap_dirfd);
            goto err;
        }

        ret = linkat(omap_dirfd, oname, nmap_dirfd, name, 0);
        close_preserve_errno(nmap_dirfd);
        close_preserve_errno(omap_dirfd);
1098
        if (ret < 0 && errno != ENOENT) {
1099
            goto err_undo_link;
1100
        }
1101 1102

        ret = 0;
1103
    }
1104
    goto out_close;
1105

1106 1107 1108 1109 1110 1111 1112
err:
    ret = -1;
err_undo_link:
    unlinkat_preserve_errno(ndirfd, name, 0);
out_close:
    close_preserve_errno(ndirfd);
    close_preserve_errno(odirfd);
1113
out:
1114 1115
    g_free(oname);
    g_free(odirpath);
1116
    return ret;
1117 1118
}

1119
static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
1120
{
1121
    int fd, ret;
1122

1123 1124 1125 1126 1127 1128
    fd = local_open_nofollow(ctx, fs_path->data, O_WRONLY, 0);
    if (fd == -1) {
        return -1;
    }
    ret = ftruncate(fd, size);
    close_preserve_errno(fd);
1129
    return ret;
1130 1131
}

1132
static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
1133
{
1134 1135
    char *dirpath = g_path_get_dirname(fs_path->data);
    char *name = g_path_get_basename(fs_path->data);
1136
    int ret = -1;
1137 1138 1139 1140 1141 1142
    int dirfd;

    dirfd = local_opendir_nofollow(fs_ctx, dirpath);
    if (dirfd == -1) {
        goto out;
    }
1143

1144
    if ((credp->fc_uid == -1 && credp->fc_gid == -1) ||
1145 1146
        (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
        (fs_ctx->export_flags & V9FS_SM_NONE)) {
1147 1148
        ret = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid,
                       AT_SYMLINK_NOFOLLOW);
1149
    } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
1150
        ret = local_set_xattrat(dirfd, name, credp);
1151
    } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1152
        ret = local_set_mapped_file_attrat(dirfd, name, credp);
1153
    }
1154 1155 1156 1157 1158

    close_preserve_errno(dirfd);
out:
    g_free(name);
    g_free(dirpath);
1159
    return ret;
1160 1161
}

1162
static int local_utimensat(FsContext *s, V9fsPath *fs_path,
1163
                           const struct timespec *buf)
1164
{
1165 1166 1167
    char *dirpath = g_path_get_dirname(fs_path->data);
    char *name = g_path_get_basename(fs_path->data);
    int dirfd, ret = -1;
1168

1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
    dirfd = local_opendir_nofollow(s, dirpath);
    if (dirfd == -1) {
        goto out;
    }

    ret = utimensat(dirfd, name, buf, AT_SYMLINK_NOFOLLOW);
    close_preserve_errno(dirfd);
out:
    g_free(dirpath);
    g_free(name);
1179
    return ret;
1180 1181
}

1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
static int local_unlinkat_common(FsContext *ctx, int dirfd, const char *name,
                                 int flags)
{
    int ret = -1;

    if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
        int map_dirfd;

        if (flags == AT_REMOVEDIR) {
            int fd;

            fd = openat(dirfd, name, O_RDONLY | O_DIRECTORY | O_PATH);
            if (fd == -1) {
                goto err_out;
            }
            /*
             * If directory remove .virtfs_metadata contained in the
             * directory
             */
            ret = unlinkat(fd, VIRTFS_META_DIR, AT_REMOVEDIR);
            close_preserve_errno(fd);
            if (ret < 0 && errno != ENOENT) {
                /*
                 * We didn't had the .virtfs_metadata file. May be file created
                 * in non-mapped mode ?. Ignore ENOENT.
                 */
                goto err_out;
            }
        }
        /*
         * Now remove the name from parent directory
         * .virtfs_metadata directory.
         */
        map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
        ret = unlinkat(map_dirfd, name, 0);
        close_preserve_errno(map_dirfd);
        if (ret < 0 && errno != ENOENT) {
            /*
             * We didn't had the .virtfs_metadata file. May be file created
             * in non-mapped mode ?. Ignore ENOENT.
             */
            goto err_out;
        }
    }

    ret = unlinkat(dirfd, name, flags);
err_out:
    return ret;
}

1232 1233
static int local_remove(FsContext *ctx, const char *path)
{
1234
    struct stat stbuf;
1235 1236 1237 1238 1239
    char *dirpath = g_path_get_dirname(path);
    char *name = g_path_get_basename(path);
    int flags = 0;
    int dirfd;
    int err = -1;
1240

1241 1242 1243
    dirfd = local_opendir_nofollow(ctx, dirpath);
    if (dirfd) {
        goto out;
1244
    }
1245

1246 1247 1248 1249 1250 1251 1252 1253 1254
    if (fstatat(dirfd, path, &stbuf, AT_SYMLINK_NOFOLLOW) < 0) {
        goto err_out;
    }

    if (S_ISDIR(stbuf.st_mode)) {
        flags |= AT_REMOVEDIR;
    }

    err = local_unlinkat_common(ctx, dirfd, name, flags);
1255
err_out:
1256 1257 1258 1259
    close_preserve_errno(dirfd);
out:
    g_free(name);
    g_free(dirpath);
1260
    return err;
1261 1262
}

1263 1264
static int local_fsync(FsContext *ctx, int fid_type,
                       V9fsFidOpenState *fs, int datasync)
1265
{
1266 1267 1268
    int fd;

    if (fid_type == P9_FID_DIR) {
G
Greg Kurz 已提交
1269
        fd = dirfd(fs->dir.stream);
1270 1271 1272 1273
    } else {
        fd = fs->fd;
    }

1274
    if (datasync) {
1275
        return qemu_fdatasync(fd);
1276
    } else {
1277
        return fsync(fd);
1278
    }
1279 1280
}

1281
static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
1282
{
1283
    int fd, ret;
1284

1285 1286 1287
    fd = local_open_nofollow(s, fs_path->data, O_RDONLY, 0);
    ret = fstatfs(fd, stbuf);
    close_preserve_errno(fd);
1288
    return ret;
1289 1290
}

1291
static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
1292 1293
                               const char *name, void *value, size_t size)
{
1294 1295
    char *path = fs_path->data;

1296
    return v9fs_get_xattr(ctx, path, name, value, size);
1297 1298
}

1299
static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path,
1300 1301
                                void *value, size_t size)
{
1302 1303
    char *path = fs_path->data;

1304
    return v9fs_list_xattr(ctx, path, value, size);
1305 1306
}

1307
static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
1308 1309
                           void *value, size_t size, int flags)
{
1310 1311
    char *path = fs_path->data;

1312
    return v9fs_set_xattr(ctx, path, name, value, size, flags);
1313 1314
}

1315 1316
static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
                              const char *name)
1317
{
1318 1319
    char *path = fs_path->data;

1320
    return v9fs_remove_xattr(ctx, path, name);
1321 1322
}

1323 1324 1325 1326
static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path,
                              const char *name, V9fsPath *target)
{
    if (dir_path) {
1327
        v9fs_path_sprintf(target, "%s/%s", dir_path->data, name);
1328
    } else {
1329
        v9fs_path_sprintf(target, "%s", name);
1330 1331 1332 1333 1334 1335 1336 1337 1338
    }
    return 0;
}

static int local_renameat(FsContext *ctx, V9fsPath *olddir,
                          const char *old_name, V9fsPath *newdir,
                          const char *new_name)
{
    int ret;
1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
    int odirfd, ndirfd;

    odirfd = local_opendir_nofollow(ctx, olddir->data);
    if (odirfd == -1) {
        return -1;
    }

    ndirfd = local_opendir_nofollow(ctx, newdir->data);
    if (ndirfd == -1) {
        close_preserve_errno(odirfd);
        return -1;
    }

    ret = renameat(odirfd, old_name, ndirfd, new_name);
    if (ret < 0) {
        goto out;
    }
1356

1357 1358
    if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
        int omap_dirfd, nmap_dirfd;
1359

1360 1361 1362 1363
        ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700);
        if (ret < 0 && errno != EEXIST) {
            goto err_undo_rename;
        }
1364

1365
        omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR);
1366 1367 1368 1369
        if (omap_dirfd == -1) {
            goto err;
        }

1370
        nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR);
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
        if (nmap_dirfd == -1) {
            close_preserve_errno(omap_dirfd);
            goto err;
        }

        /* rename the .virtfs_metadata files */
        ret = renameat(omap_dirfd, old_name, nmap_dirfd, new_name);
        close_preserve_errno(nmap_dirfd);
        close_preserve_errno(omap_dirfd);
        if (ret < 0 && errno != ENOENT) {
            goto err_undo_rename;
        }

        ret = 0;
    }
    goto out;

err:
    ret = -1;
err_undo_rename:
    renameat_preserve_errno(ndirfd, new_name, odirfd, old_name);
out:
    close_preserve_errno(ndirfd);
    close_preserve_errno(odirfd);
1395 1396 1397
    return ret;
}

G
Greg Kurz 已提交
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424
static void v9fs_path_init_dirname(V9fsPath *path, const char *str)
{
    path->data = g_path_get_dirname(str);
    path->size = strlen(path->data) + 1;
}

static int local_rename(FsContext *ctx, const char *oldpath,
                        const char *newpath)
{
    int err;
    char *oname = g_path_get_basename(oldpath);
    char *nname = g_path_get_basename(newpath);
    V9fsPath olddir, newdir;

    v9fs_path_init_dirname(&olddir, oldpath);
    v9fs_path_init_dirname(&newdir, newpath);

    err = local_renameat(ctx, &olddir, oname, &newdir, nname);

    v9fs_path_free(&newdir);
    v9fs_path_free(&olddir);
    g_free(nname);
    g_free(oname);

    return err;
}

1425 1426 1427 1428
static int local_unlinkat(FsContext *ctx, V9fsPath *dir,
                          const char *name, int flags)
{
    int ret;
1429
    int dirfd;
1430

1431 1432 1433
    dirfd = local_opendir_nofollow(ctx, dir->data);
    if (dirfd == -1) {
        return -1;
1434
    }
1435

1436 1437
    ret = local_unlinkat_common(ctx, dirfd, name, flags);
    close_preserve_errno(dirfd);
1438 1439
    return ret;
}
1440

1441 1442 1443
static int local_ioc_getversion(FsContext *ctx, V9fsPath *path,
                                mode_t st_mode, uint64_t *st_gen)
{
1444
#ifdef FS_IOC_GETVERSION
1445
    int err;
1446 1447
    V9fsFidOpenState fid_open;

1448 1449 1450 1451 1452
    /*
     * Do not try to open special files like device nodes, fifos etc
     * We can get fd for regular files and directories only
     */
    if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
1453 1454
        errno = ENOTTY;
        return -1;
1455
    }
1456 1457 1458
    err = local_open(ctx, path, O_RDONLY, &fid_open);
    if (err < 0) {
        return err;
1459
    }
1460 1461
    err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
    local_close(ctx, &fid_open);
1462
    return err;
1463
#else
1464 1465
    errno = ENOTTY;
    return -1;
1466
#endif
1467 1468
}

1469 1470
static int local_init(FsContext *ctx)
{
1471
    struct statfs stbuf;
1472 1473 1474 1475 1476 1477
    LocalData *data = g_malloc(sizeof(*data));

    data->mountfd = open(ctx->fs_root, O_DIRECTORY | O_RDONLY);
    if (data->mountfd == -1) {
        goto err;
    }
1478

1479 1480 1481 1482
#ifdef FS_IOC_GETVERSION
    /*
     * use ioc_getversion only if the ioctl is definied
     */
1483 1484 1485
    if (fstatfs(data->mountfd, &stbuf) < 0) {
        close_preserve_errno(data->mountfd);
        goto err;
1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496
    }
    switch (stbuf.f_type) {
    case EXT2_SUPER_MAGIC:
    case BTRFS_SUPER_MAGIC:
    case REISERFS_SUPER_MAGIC:
    case XFS_SUPER_MAGIC:
        ctx->exops.get_st_gen = local_ioc_getversion;
        break;
    }
#endif

1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509
    if (ctx->export_flags & V9FS_SM_PASSTHROUGH) {
        ctx->xops = passthrough_xattr_ops;
    } else if (ctx->export_flags & V9FS_SM_MAPPED) {
        ctx->xops = mapped_xattr_ops;
    } else if (ctx->export_flags & V9FS_SM_NONE) {
        ctx->xops = none_xattr_ops;
    } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
        /*
         * xattr operation for mapped-file and passthrough
         * remain same.
         */
        ctx->xops = passthrough_xattr_ops;
    }
1510
    ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT;
1511

1512
    ctx->private = data;
1513
    return 0;
1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525

err:
    g_free(data);
    return -1;
}

static void local_cleanup(FsContext *ctx)
{
    LocalData *data = ctx->private;

    close(data->mountfd);
    g_free(data);
1526 1527
}

1528 1529 1530 1531 1532 1533
static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse)
{
    const char *sec_model = qemu_opt_get(opts, "security_model");
    const char *path = qemu_opt_get(opts, "path");

    if (!sec_model) {
1534 1535 1536
        error_report("Security model not specified, local fs needs security model");
        error_printf("valid options are:"
                     "\tsecurity_model=[passthrough|mapped-xattr|mapped-file|none]\n");
1537 1538 1539 1540 1541
        return -1;
    }

    if (!strcmp(sec_model, "passthrough")) {
        fse->export_flags |= V9FS_SM_PASSTHROUGH;
1542 1543
    } else if (!strcmp(sec_model, "mapped") ||
               !strcmp(sec_model, "mapped-xattr")) {
1544 1545 1546
        fse->export_flags |= V9FS_SM_MAPPED;
    } else if (!strcmp(sec_model, "none")) {
        fse->export_flags |= V9FS_SM_NONE;
1547 1548
    } else if (!strcmp(sec_model, "mapped-file")) {
        fse->export_flags |= V9FS_SM_MAPPED_FILE;
1549
    } else {
1550 1551 1552
        error_report("Invalid security model %s specified", sec_model);
        error_printf("valid options are:"
                     "\t[passthrough|mapped-xattr|mapped-file|none]\n");
1553 1554 1555 1556
        return -1;
    }

    if (!path) {
1557
        error_report("fsdev: No path specified");
1558 1559 1560 1561 1562 1563 1564
        return -1;
    }
    fse->path = g_strdup(path);

    return 0;
}

1565
FileOperations local_ops = {
1566
    .parse_opts = local_parse_opts,
1567
    .init  = local_init,
1568
    .cleanup = local_cleanup,
1569 1570 1571 1572
    .lstat = local_lstat,
    .readlink = local_readlink,
    .close = local_close,
    .closedir = local_closedir,
1573 1574
    .open = local_open,
    .opendir = local_opendir,
1575 1576
    .rewinddir = local_rewinddir,
    .telldir = local_telldir,
G
Greg Kurz 已提交
1577
    .readdir = local_readdir,
1578
    .seekdir = local_seekdir,
1579 1580
    .preadv = local_preadv,
    .pwritev = local_pwritev,
1581 1582 1583 1584 1585 1586 1587
    .chmod = local_chmod,
    .mknod = local_mknod,
    .mkdir = local_mkdir,
    .fstat = local_fstat,
    .open2 = local_open2,
    .symlink = local_symlink,
    .link = local_link,
1588 1589 1590
    .truncate = local_truncate,
    .rename = local_rename,
    .chown = local_chown,
M
M. Mohan Kumar 已提交
1591
    .utimensat = local_utimensat,
1592
    .remove = local_remove,
1593
    .fsync = local_fsync,
1594
    .statfs = local_statfs,
1595 1596
    .lgetxattr = local_lgetxattr,
    .llistxattr = local_llistxattr,
1597
    .lsetxattr = local_lsetxattr,
1598
    .lremovexattr = local_lremovexattr,
1599 1600 1601
    .name_to_path = local_name_to_path,
    .renameat  = local_renameat,
    .unlinkat = local_unlinkat,
1602
};