9p-local.c 34.8 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
#define VIRTFS_META_DIR ".virtfs_metadata"

72
static char *local_mapped_attr_path(FsContext *ctx, const char *path)
73
{
74 75 76 77 78 79 80 81 82 83 84
    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);
85 86
}

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
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;
}

114 115 116 117 118 119
#define ATTR_MAX 100
static void local_mapped_file_attr(FsContext *ctx, const char *path,
                                   struct stat *stbuf)
{
    FILE *fp;
    char buf[ATTR_MAX];
120
    char *attr_path;
121

122
    attr_path = local_mapped_attr_path(ctx, path);
123
    fp = local_fopen(attr_path, "r");
124
    g_free(attr_path);
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
    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);
}

144
static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf)
145
{
146
    int err;
147
    char *buffer;
148 149
    char *path = fs_path->data;

150 151
    buffer = rpath(fs_ctx, path);
    err =  lstat(buffer, stbuf);
152
    if (err) {
153
        goto err_out;
154
    }
155
    if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
156 157 158 159 160
        /* Actual credentials are part of extended attrs */
        uid_t tmp_uid;
        gid_t tmp_gid;
        mode_t tmp_mode;
        dev_t tmp_dev;
161
        if (getxattr(buffer, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) {
162
            stbuf->st_uid = le32_to_cpu(tmp_uid);
163
        }
164
        if (getxattr(buffer, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
165
            stbuf->st_gid = le32_to_cpu(tmp_gid);
166
        }
167
        if (getxattr(buffer, "user.virtfs.mode",
168
                    &tmp_mode, sizeof(mode_t)) > 0) {
169
            stbuf->st_mode = le32_to_cpu(tmp_mode);
170
        }
171
        if (getxattr(buffer, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
172
            stbuf->st_rdev = le64_to_cpu(tmp_dev);
173
        }
174 175 176
    } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
        local_mapped_file_attr(fs_ctx, path, stbuf);
    }
177 178 179

err_out:
    g_free(buffer);
180 181 182 183 184 185
    return err;
}

static int local_create_mapped_attr_dir(FsContext *ctx, const char *path)
{
    int err;
186
    char *attr_dir;
187
    char *tmp_path = g_strdup(path);
188

189
    attr_dir = g_strdup_printf("%s/%s/%s",
190 191 192 193 194
             ctx->fs_root, dirname(tmp_path), VIRTFS_META_DIR);

    err = mkdir(attr_dir, 0700);
    if (err < 0 && errno == EEXIST) {
        err = 0;
195
    }
196
    g_free(attr_dir);
197
    g_free(tmp_path);
198
    return err;
199 200
}

201 202 203 204 205 206
static int local_set_mapped_file_attr(FsContext *ctx,
                                      const char *path, FsCred *credp)
{
    FILE *fp;
    int ret = 0;
    char buf[ATTR_MAX];
207
    char *attr_path;
208 209
    int uid = -1, gid = -1, mode = -1, rdev = -1;

210 211
    attr_path = local_mapped_attr_path(ctx, path);
    fp = local_fopen(attr_path, "r");
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
    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:
238
    fp = local_fopen(attr_path, "w");
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
    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:
273
    g_free(attr_path);
274 275 276
    return ret;
}

277
static int local_set_xattr(const char *path, FsCred *credp)
278
{
279
    int err;
280

281
    if (credp->fc_uid != -1) {
282 283
        uint32_t tmp_uid = cpu_to_le32(credp->fc_uid);
        err = setxattr(path, "user.virtfs.uid", &tmp_uid, sizeof(uid_t), 0);
284 285 286
        if (err) {
            return err;
        }
287
    }
288
    if (credp->fc_gid != -1) {
289 290
        uint32_t tmp_gid = cpu_to_le32(credp->fc_gid);
        err = setxattr(path, "user.virtfs.gid", &tmp_gid, sizeof(gid_t), 0);
291 292 293
        if (err) {
            return err;
        }
294
    }
295
    if (credp->fc_mode != -1) {
296 297
        uint32_t tmp_mode = cpu_to_le32(credp->fc_mode);
        err = setxattr(path, "user.virtfs.mode", &tmp_mode, sizeof(mode_t), 0);
298 299 300
        if (err) {
            return err;
        }
301
    }
302
    if (credp->fc_rdev != -1) {
303 304
        uint64_t tmp_rdev = cpu_to_le64(credp->fc_rdev);
        err = setxattr(path, "user.virtfs.rdev", &tmp_rdev, sizeof(dev_t), 0);
305 306 307
        if (err) {
            return err;
        }
308 309 310 311
    }
    return 0;
}

312
static int local_post_create_passthrough(FsContext *fs_ctx, const char *path,
313
                                         FsCred *credp)
314
{
315
    char *buffer;
316

317 318
    buffer = rpath(fs_ctx, path);
    if (lchown(buffer, credp->fc_uid, credp->fc_gid) < 0) {
319 320 321 322
        /*
         * If we fail to change ownership and if we are
         * using security model none. Ignore the error
         */
323
        if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
324
            goto err;
325
        }
326
    }
M
M. Mohan Kumar 已提交
327

328 329
    if (chmod(buffer, credp->fc_mode & 07777) < 0) {
        goto err;
M
M. Mohan Kumar 已提交
330
    }
331 332

    g_free(buffer);
333
    return 0;
334 335 336
err:
    g_free(buffer);
    return -1;
337 338
}

339 340
static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
                              char *buf, size_t bufsz)
341
{
342
    ssize_t tsize = -1;
343
    char *buffer;
344 345
    char *path = fs_path->data;

346 347
    if ((fs_ctx->export_flags & V9FS_SM_MAPPED) ||
        (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
348
        int fd;
349 350 351
        buffer = rpath(fs_ctx, path);
        fd = open(buffer, O_RDONLY | O_NOFOLLOW);
        g_free(buffer);
352 353 354 355 356 357 358
        if (fd == -1) {
            return -1;
        }
        do {
            tsize = read(fd, (void *)buf, bufsz);
        } while (tsize == -1 && errno == EINTR);
        close(fd);
359 360
    } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
               (fs_ctx->export_flags & V9FS_SM_NONE)) {
361 362 363
        buffer = rpath(fs_ctx, path);
        tsize = readlink(buffer, buf, bufsz);
        g_free(buffer);
364 365
    }
    return tsize;
366 367
}

368
static int local_close(FsContext *ctx, V9fsFidOpenState *fs)
369
{
370
    return close(fs->fd);
371 372
}

373
static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs)
374
{
G
Greg Kurz 已提交
375
    return closedir(fs->dir.stream);
376
}
377

378 379
static int local_open(FsContext *ctx, V9fsPath *fs_path,
                      int flags, V9fsFidOpenState *fs)
380
{
381
    int fd;
382

383
    fd = local_open_nofollow(ctx, fs_path->data, flags, 0);
384 385 386 387
    if (fd == -1) {
        return -1;
    }
    fs->fd = fd;
388
    return fs->fd;
389 390
}

391 392
static int local_opendir(FsContext *ctx,
                         V9fsPath *fs_path, V9fsFidOpenState *fs)
393
{
394
    int dirfd;
395
    DIR *stream;
396

397 398 399 400 401 402
    dirfd = local_opendir_nofollow(ctx, fs_path->data);
    if (dirfd == -1) {
        return -1;
    }

    stream = fdopendir(dirfd);
403
    if (!stream) {
404 405
        return -1;
    }
406
    fs->dir.stream = stream;
407
    return 0;
408 409
}

410
static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
411
{
G
Greg Kurz 已提交
412
    rewinddir(fs->dir.stream);
413 414
}

415
static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs)
416
{
G
Greg Kurz 已提交
417
    return telldir(fs->dir.stream);
418 419
}

G
Greg Kurz 已提交
420
static struct dirent *local_readdir(FsContext *ctx, V9fsFidOpenState *fs)
421
{
G
Greg Kurz 已提交
422
    struct dirent *entry;
423 424

again:
G
Greg Kurz 已提交
425 426 427 428 429
    entry = readdir(fs->dir.stream);
    if (!entry) {
        return NULL;
    }

430 431 432
    if (ctx->export_flags & V9FS_SM_MAPPED) {
        entry->d_type = DT_UNKNOWN;
    } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
G
Greg Kurz 已提交
433
        if (!strcmp(entry->d_name, VIRTFS_META_DIR)) {
434 435 436
            /* skp the meta data directory */
            goto again;
        }
437
        entry->d_type = DT_UNKNOWN;
438
    }
G
Greg Kurz 已提交
439 440

    return entry;
441 442
}

443
static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
444
{
G
Greg Kurz 已提交
445
    seekdir(fs->dir.stream, off);
446 447
}

448 449
static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs,
                            const struct iovec *iov,
450
                            int iovcnt, off_t offset)
451
{
452
#ifdef CONFIG_PREADV
453
    return preadv(fs->fd, iov, iovcnt, offset);
454
#else
455
    int err = lseek(fs->fd, offset, SEEK_SET);
456 457 458
    if (err == -1) {
        return err;
    } else {
459
        return readv(fs->fd, iov, iovcnt);
460 461
    }
#endif
462 463
}

464 465
static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
                             const struct iovec *iov,
466
                             int iovcnt, off_t offset)
467
{
468 469
    ssize_t ret
;
470
#ifdef CONFIG_PREADV
471
    ret = pwritev(fs->fd, iov, iovcnt, offset);
472
#else
473
    int err = lseek(fs->fd, offset, SEEK_SET);
474 475 476
    if (err == -1) {
        return err;
    } else {
477
        ret = writev(fs->fd, iov, iovcnt);
478 479
    }
#endif
480 481 482 483 484 485 486
#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.
         */
487
        sync_file_range(fs->fd, offset, ret,
488 489 490 491
                        SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
    }
#endif
    return ret;
492 493
}

494
static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
495
{
496 497
    char *buffer;
    int ret = -1;
498 499
    char *path = fs_path->data;

500
    if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
501 502 503
        buffer = rpath(fs_ctx, path);
        ret = local_set_xattr(buffer, credp);
        g_free(buffer);
504 505
    } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
        return local_set_mapped_file_attr(fs_ctx, path, credp);
506 507
    } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
               (fs_ctx->export_flags & V9FS_SM_NONE)) {
508 509 510
        buffer = rpath(fs_ctx, path);
        ret = chmod(buffer, credp->fc_mode);
        g_free(buffer);
511
    }
512
    return ret;
513 514
}

515 516
static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
                       const char *name, FsCred *credp)
517
{
518
    char *path;
519 520
    int err = -1;
    int serrno = 0;
521
    V9fsString fullname;
522
    char *buffer = NULL;
523

524 525 526 527
    v9fs_string_init(&fullname);
    v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
    path = fullname.data;

528
    /* Determine the security model */
529
    if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
530 531
        buffer = rpath(fs_ctx, path);
        err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0);
532
        if (err == -1) {
533
            goto out;
534
        }
535
        err = local_set_xattr(buffer, credp);
536 537 538 539
        if (err == -1) {
            serrno = errno;
            goto err_end;
        }
540 541
    } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {

542 543
        buffer = rpath(fs_ctx, path);
        err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0);
544 545 546 547 548 549 550 551
        if (err == -1) {
            goto out;
        }
        err = local_set_mapped_file_attr(fs_ctx, path, credp);
        if (err == -1) {
            serrno = errno;
            goto err_end;
        }
552 553
    } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
               (fs_ctx->export_flags & V9FS_SM_NONE)) {
554 555
        buffer = rpath(fs_ctx, path);
        err = mknod(buffer, credp->fc_mode, credp->fc_rdev);
556
        if (err == -1) {
557
            goto out;
558 559 560 561 562 563 564
        }
        err = local_post_create_passthrough(fs_ctx, path, credp);
        if (err == -1) {
            serrno = errno;
            goto err_end;
        }
    }
565
    goto out;
566 567

err_end:
568
    remove(buffer);
569
    errno = serrno;
570
out:
571
    g_free(buffer);
572
    v9fs_string_free(&fullname);
573
    return err;
574 575
}

576 577
static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
                       const char *name, FsCred *credp)
578
{
579
    char *path;
580 581
    int err = -1;
    int serrno = 0;
582
    V9fsString fullname;
583
    char *buffer = NULL;
584

585 586 587 588
    v9fs_string_init(&fullname);
    v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
    path = fullname.data;

589
    /* Determine the security model */
590
    if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
591 592
        buffer = rpath(fs_ctx, path);
        err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
593
        if (err == -1) {
594
            goto out;
595 596
        }
        credp->fc_mode = credp->fc_mode|S_IFDIR;
597
        err = local_set_xattr(buffer, credp);
598 599 600 601
        if (err == -1) {
            serrno = errno;
            goto err_end;
        }
602
    } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
603 604
        buffer = rpath(fs_ctx, path);
        err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
605 606 607 608 609 610 611 612 613
        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;
        }
614 615
    } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
               (fs_ctx->export_flags & V9FS_SM_NONE)) {
616 617
        buffer = rpath(fs_ctx, path);
        err = mkdir(buffer, credp->fc_mode);
618
        if (err == -1) {
619
            goto out;
620 621 622 623 624 625 626
        }
        err = local_post_create_passthrough(fs_ctx, path, credp);
        if (err == -1) {
            serrno = errno;
            goto err_end;
        }
    }
627
    goto out;
628 629

err_end:
630
    remove(buffer);
631
    errno = serrno;
632
out:
633
    g_free(buffer);
634
    v9fs_string_free(&fullname);
635
    return err;
636 637
}

638
static int local_fstat(FsContext *fs_ctx, int fid_type,
639
                       V9fsFidOpenState *fs, struct stat *stbuf)
640
{
641 642 643
    int err, fd;

    if (fid_type == P9_FID_DIR) {
G
Greg Kurz 已提交
644
        fd = dirfd(fs->dir.stream);
645 646 647 648 649
    } else {
        fd = fs->fd;
    }

    err = fstat(fd, stbuf);
650 651 652
    if (err) {
        return err;
    }
653
    if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
654 655 656 657 658 659
        /* Actual credentials are part of extended attrs */
        uid_t tmp_uid;
        gid_t tmp_gid;
        mode_t tmp_mode;
        dev_t tmp_dev;

660 661
        if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) {
            stbuf->st_uid = le32_to_cpu(tmp_uid);
662
        }
663 664
        if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
            stbuf->st_gid = le32_to_cpu(tmp_gid);
665
        }
666 667
        if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) {
            stbuf->st_mode = le32_to_cpu(tmp_mode);
668
        }
669 670
        if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
            stbuf->st_rdev = le64_to_cpu(tmp_dev);
671
        }
672 673 674
    } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
        errno = EOPNOTSUPP;
        return -1;
675 676
    }
    return err;
677 678
}

679
static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
680
                       int flags, FsCred *credp, V9fsFidOpenState *fs)
681
{
682
    char *path;
683 684 685
    int fd = -1;
    int err = -1;
    int serrno = 0;
686
    V9fsString fullname;
687
    char *buffer = NULL;
688

689 690 691 692 693
    /*
     * Mark all the open to not follow symlinks
     */
    flags |= O_NOFOLLOW;

694 695 696 697
    v9fs_string_init(&fullname);
    v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
    path = fullname.data;

698
    /* Determine the security model */
699
    if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
700 701
        buffer = rpath(fs_ctx, path);
        fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
702
        if (fd == -1) {
703 704
            err = fd;
            goto out;
705 706 707
        }
        credp->fc_mode = credp->fc_mode|S_IFREG;
        /* Set cleint credentials in xattr */
708
        err = local_set_xattr(buffer, credp);
709 710 711 712
        if (err == -1) {
            serrno = errno;
            goto err_end;
        }
713
    } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
714 715
        buffer = rpath(fs_ctx, path);
        fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
716 717 718 719 720 721 722 723 724 725 726
        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;
        }
727 728
    } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
               (fs_ctx->export_flags & V9FS_SM_NONE)) {
729 730
        buffer = rpath(fs_ctx, path);
        fd = open(buffer, flags, credp->fc_mode);
731
        if (fd == -1) {
732 733
            err = fd;
            goto out;
734 735 736 737 738 739 740
        }
        err = local_post_create_passthrough(fs_ctx, path, credp);
        if (err == -1) {
            serrno = errno;
            goto err_end;
        }
    }
741
    err = fd;
742
    fs->fd = fd;
743
    goto out;
744 745 746

err_end:
    close(fd);
747
    remove(buffer);
748
    errno = serrno;
749
out:
750
    g_free(buffer);
751
    v9fs_string_free(&fullname);
752
    return err;
753 754
}

755

756
static int local_symlink(FsContext *fs_ctx, const char *oldpath,
757
                         V9fsPath *dir_path, const char *name, FsCred *credp)
758
{
759 760
    int err = -1;
    int serrno = 0;
761 762
    char *newpath;
    V9fsString fullname;
763
    char *buffer = NULL;
764

765 766 767 768
    v9fs_string_init(&fullname);
    v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
    newpath = fullname.data;

769
    /* Determine the security model */
770
    if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
771 772
        int fd;
        ssize_t oldpath_size, write_size;
773 774
        buffer = rpath(fs_ctx, newpath);
        fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS);
775
        if (fd == -1) {
776 777
            err = fd;
            goto out;
778 779
        }
        /* Write the oldpath (target) to the file. */
780
        oldpath_size = strlen(oldpath);
781 782 783 784 785 786 787 788 789 790 791 792 793
        do {
            write_size = write(fd, (void *)oldpath, oldpath_size);
        } while (write_size == -1 && errno == EINTR);

        if (write_size != oldpath_size) {
            serrno = errno;
            close(fd);
            err = -1;
            goto err_end;
        }
        close(fd);
        /* Set cleint credentials in symlink's xattr */
        credp->fc_mode = credp->fc_mode|S_IFLNK;
794
        err = local_set_xattr(buffer, credp);
795 796 797 798
        if (err == -1) {
            serrno = errno;
            goto err_end;
        }
799 800 801
    } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
        int fd;
        ssize_t oldpath_size, write_size;
802 803
        buffer = rpath(fs_ctx, newpath);
        fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS);
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
        if (fd == -1) {
            err = fd;
            goto out;
        }
        /* Write the oldpath (target) to the file. */
        oldpath_size = strlen(oldpath);
        do {
            write_size = write(fd, (void *)oldpath, oldpath_size);
        } while (write_size == -1 && errno == EINTR);

        if (write_size != oldpath_size) {
            serrno = errno;
            close(fd);
            err = -1;
            goto err_end;
        }
        close(fd);
        /* Set cleint credentials in symlink's xattr */
        credp->fc_mode = credp->fc_mode|S_IFLNK;
        err = local_set_mapped_file_attr(fs_ctx, newpath, credp);
        if (err == -1) {
            serrno = errno;
            goto err_end;
        }
828 829
    } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
               (fs_ctx->export_flags & V9FS_SM_NONE)) {
830 831
        buffer = rpath(fs_ctx, newpath);
        err = symlink(oldpath, buffer);
832
        if (err) {
833
            goto out;
834
        }
835
        err = lchown(buffer, credp->fc_uid, credp->fc_gid);
836
        if (err == -1) {
837 838 839 840
            /*
             * If we fail to change ownership and if we are
             * using security model none. Ignore the error
             */
841
            if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
842 843 844 845
                serrno = errno;
                goto err_end;
            } else
                err = 0;
846 847
        }
    }
848
    goto out;
849 850

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

859 860
static int local_link(FsContext *ctx, V9fsPath *oldpath,
                      V9fsPath *dirpath, const char *name)
861
{
862 863
    int ret;
    V9fsString newpath;
864
    char *buffer, *buffer1;
865

866 867 868
    v9fs_string_init(&newpath);
    v9fs_string_sprintf(&newpath, "%s/%s", dirpath->data, name);

869 870 871 872 873
    buffer = rpath(ctx, oldpath->data);
    buffer1 = rpath(ctx, newpath.data);
    ret = link(buffer, buffer1);
    g_free(buffer);
    g_free(buffer1);
874 875 876 877 878 879 880 881

    /* now link the virtfs_metadata files */
    if (!ret && (ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
        /* Link the .virtfs_metadata files. Create the metada directory */
        ret = local_create_mapped_attr_dir(ctx, newpath.data);
        if (ret < 0) {
            goto err_out;
        }
882 883 884 885 886
        buffer = local_mapped_attr_path(ctx, oldpath->data);
        buffer1 = local_mapped_attr_path(ctx, newpath.data);
        ret = link(buffer, buffer1);
        g_free(buffer);
        g_free(buffer1);
887 888 889 890 891
        if (ret < 0 && errno != ENOENT) {
            goto err_out;
        }
    }
err_out:
892 893
    v9fs_string_free(&newpath);
    return ret;
894 895
}

896
static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
897
{
898 899
    char *buffer;
    int ret;
900 901
    char *path = fs_path->data;

902 903 904 905
    buffer = rpath(ctx, path);
    ret = truncate(buffer, size);
    g_free(buffer);
    return ret;
906 907 908 909 910
}

static int local_rename(FsContext *ctx, const char *oldpath,
                        const char *newpath)
{
911
    int err;
912
    char *buffer, *buffer1;
913

914 915 916 917 918 919
    if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
        err = local_create_mapped_attr_dir(ctx, newpath);
        if (err < 0) {
            return err;
        }
        /* rename the .virtfs_metadata files */
920 921 922 923 924
        buffer = local_mapped_attr_path(ctx, oldpath);
        buffer1 = local_mapped_attr_path(ctx, newpath);
        err = rename(buffer, buffer1);
        g_free(buffer);
        g_free(buffer1);
925 926 927 928
        if (err < 0 && errno != ENOENT) {
            return err;
        }
    }
929 930 931 932 933 934 935

    buffer = rpath(ctx, oldpath);
    buffer1 = rpath(ctx, newpath);
    err = rename(buffer, buffer1);
    g_free(buffer);
    g_free(buffer1);
    return err;
936 937
}

938
static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
939
{
940 941
    char *buffer;
    int ret = -1;
942 943
    char *path = fs_path->data;

944
    if ((credp->fc_uid == -1 && credp->fc_gid == -1) ||
945 946
        (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
        (fs_ctx->export_flags & V9FS_SM_NONE)) {
947 948 949
        buffer = rpath(fs_ctx, path);
        ret = lchown(buffer, credp->fc_uid, credp->fc_gid);
        g_free(buffer);
950
    } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
951 952 953
        buffer = rpath(fs_ctx, path);
        ret = local_set_xattr(buffer, credp);
        g_free(buffer);
954 955
    } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
        return local_set_mapped_file_attr(fs_ctx, path, credp);
956
    }
957
    return ret;
958 959
}

960
static int local_utimensat(FsContext *s, V9fsPath *fs_path,
961
                           const struct timespec *buf)
962
{
963 964 965
    char *dirpath = g_path_get_dirname(fs_path->data);
    char *name = g_path_get_basename(fs_path->data);
    int dirfd, ret = -1;
966

967 968 969 970 971 972 973 974 975 976
    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);
977
    return ret;
978 979
}

980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
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;
}

1030 1031
static int local_remove(FsContext *ctx, const char *path)
{
1032
    struct stat stbuf;
1033 1034 1035 1036 1037
    char *dirpath = g_path_get_dirname(path);
    char *name = g_path_get_basename(path);
    int flags = 0;
    int dirfd;
    int err = -1;
1038

1039 1040 1041
    dirfd = local_opendir_nofollow(ctx, dirpath);
    if (dirfd) {
        goto out;
1042
    }
1043

1044 1045 1046 1047 1048 1049 1050 1051 1052
    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);
1053
err_out:
1054 1055 1056 1057
    close_preserve_errno(dirfd);
out:
    g_free(name);
    g_free(dirpath);
1058
    return err;
1059 1060
}

1061 1062
static int local_fsync(FsContext *ctx, int fid_type,
                       V9fsFidOpenState *fs, int datasync)
1063
{
1064 1065 1066
    int fd;

    if (fid_type == P9_FID_DIR) {
G
Greg Kurz 已提交
1067
        fd = dirfd(fs->dir.stream);
1068 1069 1070 1071
    } else {
        fd = fs->fd;
    }

1072
    if (datasync) {
1073
        return qemu_fdatasync(fd);
1074
    } else {
1075
        return fsync(fd);
1076
    }
1077 1078
}

1079
static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
1080
{
1081
    int fd, ret;
1082

1083 1084 1085
    fd = local_open_nofollow(s, fs_path->data, O_RDONLY, 0);
    ret = fstatfs(fd, stbuf);
    close_preserve_errno(fd);
1086
    return ret;
1087 1088
}

1089
static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
1090 1091
                               const char *name, void *value, size_t size)
{
1092 1093
    char *path = fs_path->data;

1094
    return v9fs_get_xattr(ctx, path, name, value, size);
1095 1096
}

1097
static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path,
1098 1099
                                void *value, size_t size)
{
1100 1101
    char *path = fs_path->data;

1102
    return v9fs_list_xattr(ctx, path, value, size);
1103 1104
}

1105
static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
1106 1107
                           void *value, size_t size, int flags)
{
1108 1109
    char *path = fs_path->data;

1110
    return v9fs_set_xattr(ctx, path, name, value, size, flags);
1111 1112
}

1113 1114
static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
                              const char *name)
1115
{
1116 1117
    char *path = fs_path->data;

1118
    return v9fs_remove_xattr(ctx, path, name);
1119 1120
}

1121 1122 1123 1124
static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path,
                              const char *name, V9fsPath *target)
{
    if (dir_path) {
1125
        v9fs_path_sprintf(target, "%s/%s", dir_path->data, name);
1126
    } else {
1127
        v9fs_path_sprintf(target, "%s", name);
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
    }
    return 0;
}

static int local_renameat(FsContext *ctx, V9fsPath *olddir,
                          const char *old_name, V9fsPath *newdir,
                          const char *new_name)
{
    int ret;
    V9fsString old_full_name, new_full_name;

    v9fs_string_init(&old_full_name);
    v9fs_string_init(&new_full_name);

    v9fs_string_sprintf(&old_full_name, "%s/%s", olddir->data, old_name);
    v9fs_string_sprintf(&new_full_name, "%s/%s", newdir->data, new_name);

    ret = local_rename(ctx, old_full_name.data, new_full_name.data);
    v9fs_string_free(&old_full_name);
    v9fs_string_free(&new_full_name);
    return ret;
}

static int local_unlinkat(FsContext *ctx, V9fsPath *dir,
                          const char *name, int flags)
{
    int ret;
1155
    int dirfd;
1156

1157 1158 1159
    dirfd = local_opendir_nofollow(ctx, dir->data);
    if (dirfd == -1) {
        return -1;
1160
    }
1161

1162 1163
    ret = local_unlinkat_common(ctx, dirfd, name, flags);
    close_preserve_errno(dirfd);
1164 1165
    return ret;
}
1166

1167 1168 1169
static int local_ioc_getversion(FsContext *ctx, V9fsPath *path,
                                mode_t st_mode, uint64_t *st_gen)
{
1170
#ifdef FS_IOC_GETVERSION
1171
    int err;
1172 1173
    V9fsFidOpenState fid_open;

1174 1175 1176 1177 1178
    /*
     * 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)) {
1179 1180
        errno = ENOTTY;
        return -1;
1181
    }
1182 1183 1184
    err = local_open(ctx, path, O_RDONLY, &fid_open);
    if (err < 0) {
        return err;
1185
    }
1186 1187
    err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
    local_close(ctx, &fid_open);
1188
    return err;
1189
#else
1190 1191
    errno = ENOTTY;
    return -1;
1192
#endif
1193 1194
}

1195 1196
static int local_init(FsContext *ctx)
{
1197
    struct statfs stbuf;
1198 1199 1200 1201 1202 1203
    LocalData *data = g_malloc(sizeof(*data));

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

1205 1206 1207 1208
#ifdef FS_IOC_GETVERSION
    /*
     * use ioc_getversion only if the ioctl is definied
     */
1209 1210 1211
    if (fstatfs(data->mountfd, &stbuf) < 0) {
        close_preserve_errno(data->mountfd);
        goto err;
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
    }
    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

1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
    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;
    }
1236
    ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT;
1237

1238
    ctx->private = data;
1239
    return 0;
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251

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

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

    close(data->mountfd);
    g_free(data);
1252 1253
}

1254 1255 1256 1257 1258 1259
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) {
1260 1261 1262
        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");
1263 1264 1265 1266 1267
        return -1;
    }

    if (!strcmp(sec_model, "passthrough")) {
        fse->export_flags |= V9FS_SM_PASSTHROUGH;
1268 1269
    } else if (!strcmp(sec_model, "mapped") ||
               !strcmp(sec_model, "mapped-xattr")) {
1270 1271 1272
        fse->export_flags |= V9FS_SM_MAPPED;
    } else if (!strcmp(sec_model, "none")) {
        fse->export_flags |= V9FS_SM_NONE;
1273 1274
    } else if (!strcmp(sec_model, "mapped-file")) {
        fse->export_flags |= V9FS_SM_MAPPED_FILE;
1275
    } else {
1276 1277 1278
        error_report("Invalid security model %s specified", sec_model);
        error_printf("valid options are:"
                     "\t[passthrough|mapped-xattr|mapped-file|none]\n");
1279 1280 1281 1282
        return -1;
    }

    if (!path) {
1283
        error_report("fsdev: No path specified");
1284 1285 1286 1287 1288 1289 1290
        return -1;
    }
    fse->path = g_strdup(path);

    return 0;
}

1291
FileOperations local_ops = {
1292
    .parse_opts = local_parse_opts,
1293
    .init  = local_init,
1294
    .cleanup = local_cleanup,
1295 1296 1297 1298
    .lstat = local_lstat,
    .readlink = local_readlink,
    .close = local_close,
    .closedir = local_closedir,
1299 1300
    .open = local_open,
    .opendir = local_opendir,
1301 1302
    .rewinddir = local_rewinddir,
    .telldir = local_telldir,
G
Greg Kurz 已提交
1303
    .readdir = local_readdir,
1304
    .seekdir = local_seekdir,
1305 1306
    .preadv = local_preadv,
    .pwritev = local_pwritev,
1307 1308 1309 1310 1311 1312 1313
    .chmod = local_chmod,
    .mknod = local_mknod,
    .mkdir = local_mkdir,
    .fstat = local_fstat,
    .open2 = local_open2,
    .symlink = local_symlink,
    .link = local_link,
1314 1315 1316
    .truncate = local_truncate,
    .rename = local_rename,
    .chown = local_chown,
M
M. Mohan Kumar 已提交
1317
    .utimensat = local_utimensat,
1318
    .remove = local_remove,
1319
    .fsync = local_fsync,
1320
    .statfs = local_statfs,
1321 1322
    .lgetxattr = local_lgetxattr,
    .llistxattr = local_llistxattr,
1323
    .lsetxattr = local_lsetxattr,
1324
    .lremovexattr = local_lremovexattr,
1325 1326 1327
    .name_to_path = local_name_to_path,
    .renameat  = local_renameat,
    .unlinkat = local_unlinkat,
1328
};