virtio-9p-local.c 32.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * Virtio 9p Posix callback
 *
 * 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
Paolo Bonzini 已提交
14
#include "hw/virtio/virtio.h"
15
#include "virtio-9p.h"
16
#include "virtio-9p-xattr.h"
17
#include <arpa/inet.h>
18 19
#include <pwd.h>
#include <grp.h>
20 21
#include <sys/socket.h>
#include <sys/un.h>
22
#include "qemu/xattr.h"
23
#include <libgen.h>
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#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
42

43 44 45 46 47 48
#define VIRTFS_META_DIR ".virtfs_metadata"

static const char *local_mapped_attr_path(FsContext *ctx,
                                          const char *path, char *buffer)
{
    char *dir_name;
49
    char *tmp_path = g_strdup(path);
50 51 52 53 54 55 56 57
    char *base_name = basename(tmp_path);

    /* NULL terminate the directory */
    dir_name = tmp_path;
    *(base_name - 1) = '\0';

    snprintf(buffer, PATH_MAX, "%s/%s/%s/%s",
             ctx->fs_root, dir_name, VIRTFS_META_DIR, base_name);
58
    g_free(tmp_path);
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
    return buffer;
}

#define ATTR_MAX 100
static void local_mapped_file_attr(FsContext *ctx, const char *path,
                                   struct stat *stbuf)
{
    FILE *fp;
    char buf[ATTR_MAX];
    char attr_path[PATH_MAX];

    local_mapped_attr_path(ctx, path, attr_path);
    fp = fopen(attr_path, "r");
    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);
}

91
static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf)
92
{
93
    int err;
94
    char buffer[PATH_MAX];
95 96
    char *path = fs_path->data;

97
    err =  lstat(rpath(fs_ctx, path, buffer), stbuf);
98 99 100
    if (err) {
        return err;
    }
101
    if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
102 103 104 105 106
        /* Actual credentials are part of extended attrs */
        uid_t tmp_uid;
        gid_t tmp_gid;
        mode_t tmp_mode;
        dev_t tmp_dev;
107
        if (getxattr(rpath(fs_ctx, path, buffer), "user.virtfs.uid", &tmp_uid,
108 109 110
                    sizeof(uid_t)) > 0) {
            stbuf->st_uid = tmp_uid;
        }
111
        if (getxattr(rpath(fs_ctx, path, buffer), "user.virtfs.gid", &tmp_gid,
112 113 114
                    sizeof(gid_t)) > 0) {
            stbuf->st_gid = tmp_gid;
        }
115 116
        if (getxattr(rpath(fs_ctx, path, buffer), "user.virtfs.mode",
                    &tmp_mode, sizeof(mode_t)) > 0) {
117 118
            stbuf->st_mode = tmp_mode;
        }
119
        if (getxattr(rpath(fs_ctx, path, buffer), "user.virtfs.rdev", &tmp_dev,
120 121 122
                        sizeof(dev_t)) > 0) {
                stbuf->st_rdev = tmp_dev;
        }
123 124 125 126 127 128 129 130 131 132
    } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
        local_mapped_file_attr(fs_ctx, path, stbuf);
    }
    return err;
}

static int local_create_mapped_attr_dir(FsContext *ctx, const char *path)
{
    int err;
    char attr_dir[PATH_MAX];
133
    char *tmp_path = g_strdup(path);
134 135 136 137 138 139 140

    snprintf(attr_dir, PATH_MAX, "%s/%s/%s",
             ctx->fs_root, dirname(tmp_path), VIRTFS_META_DIR);

    err = mkdir(attr_dir, 0700);
    if (err < 0 && errno == EEXIST) {
        err = 0;
141
    }
142
    g_free(tmp_path);
143
    return err;
144 145
}

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
static int local_set_mapped_file_attr(FsContext *ctx,
                                      const char *path, FsCred *credp)
{
    FILE *fp;
    int ret = 0;
    char buf[ATTR_MAX];
    char attr_path[PATH_MAX];
    int uid = -1, gid = -1, mode = -1, rdev = -1;

    fp = fopen(local_mapped_attr_path(ctx, path, attr_path), "r");
    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:
    fp = fopen(attr_path, "w");
    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:
    return ret;
}

220
static int local_set_xattr(const char *path, FsCred *credp)
221
{
222
    int err;
223

224 225 226 227 228 229
    if (credp->fc_uid != -1) {
        err = setxattr(path, "user.virtfs.uid", &credp->fc_uid, sizeof(uid_t),
                0);
        if (err) {
            return err;
        }
230
    }
231 232 233 234 235 236
    if (credp->fc_gid != -1) {
        err = setxattr(path, "user.virtfs.gid", &credp->fc_gid, sizeof(gid_t),
                0);
        if (err) {
            return err;
        }
237
    }
238 239 240 241 242 243
    if (credp->fc_mode != -1) {
        err = setxattr(path, "user.virtfs.mode", &credp->fc_mode,
                sizeof(mode_t), 0);
        if (err) {
            return err;
        }
244
    }
245 246 247 248 249 250
    if (credp->fc_rdev != -1) {
        err = setxattr(path, "user.virtfs.rdev", &credp->fc_rdev,
                sizeof(dev_t), 0);
        if (err) {
            return err;
        }
251 252 253 254
    }
    return 0;
}

255
static int local_post_create_passthrough(FsContext *fs_ctx, const char *path,
256
                                         FsCred *credp)
257
{
258
    char buffer[PATH_MAX];
259

260 261
    if (lchown(rpath(fs_ctx, path, buffer), credp->fc_uid,
                credp->fc_gid) < 0) {
262 263 264 265
        /*
         * If we fail to change ownership and if we are
         * using security model none. Ignore the error
         */
266
        if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
267 268
            return -1;
        }
269
    }
M
M. Mohan Kumar 已提交
270 271 272 273

    if (chmod(rpath(fs_ctx, path, buffer), credp->fc_mode & 07777) < 0) {
        return -1;
    }
274 275 276
    return 0;
}

277 278
static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
                              char *buf, size_t bufsz)
279
{
280
    ssize_t tsize = -1;
281
    char buffer[PATH_MAX];
282 283
    char *path = fs_path->data;

284 285
    if ((fs_ctx->export_flags & V9FS_SM_MAPPED) ||
        (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
286
        int fd;
287
        fd = open(rpath(fs_ctx, path, buffer), O_RDONLY);
288 289 290 291 292 293 294 295
        if (fd == -1) {
            return -1;
        }
        do {
            tsize = read(fd, (void *)buf, bufsz);
        } while (tsize == -1 && errno == EINTR);
        close(fd);
        return tsize;
296 297
    } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
               (fs_ctx->export_flags & V9FS_SM_NONE)) {
298
        tsize = readlink(rpath(fs_ctx, path, buffer), buf, bufsz);
299 300
    }
    return tsize;
301 302
}

303
static int local_close(FsContext *ctx, V9fsFidOpenState *fs)
304
{
305
    return close(fs->fd);
306 307
}

308
static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs)
309
{
310
    return closedir(fs->dir);
311
}
312

313 314
static int local_open(FsContext *ctx, V9fsPath *fs_path,
                      int flags, V9fsFidOpenState *fs)
315
{
316
    char buffer[PATH_MAX];
317 318
    char *path = fs_path->data;

319 320
    fs->fd = open(rpath(ctx, path, buffer), flags);
    return fs->fd;
321 322
}

323 324
static int local_opendir(FsContext *ctx,
                         V9fsPath *fs_path, V9fsFidOpenState *fs)
325
{
326
    char buffer[PATH_MAX];
327 328
    char *path = fs_path->data;

329 330 331 332 333
    fs->dir = opendir(rpath(ctx, path, buffer));
    if (!fs->dir) {
        return -1;
    }
    return 0;
334 335
}

336
static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
337
{
338
    return rewinddir(fs->dir);
339 340
}

341
static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs)
342
{
343
    return telldir(fs->dir);
344 345
}

346 347
static int local_readdir_r(FsContext *ctx, V9fsFidOpenState *fs,
                           struct dirent *entry,
348
                           struct dirent **result)
349
{
350 351 352 353 354 355 356 357 358 359 360 361
    int ret;

again:
    ret = readdir_r(fs->dir, entry, result);
    if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
        if (!ret && *result != NULL &&
            !strcmp(entry->d_name, VIRTFS_META_DIR)) {
            /* skp the meta data directory */
            goto again;
        }
    }
    return ret;
362 363
}

364
static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
365
{
366
    return seekdir(fs->dir, off);
367 368
}

369 370
static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs,
                            const struct iovec *iov,
371
                            int iovcnt, off_t offset)
372
{
373
#ifdef CONFIG_PREADV
374
    return preadv(fs->fd, iov, iovcnt, offset);
375
#else
376
    int err = lseek(fs->fd, offset, SEEK_SET);
377 378 379
    if (err == -1) {
        return err;
    } else {
380
        return readv(fs->fd, iov, iovcnt);
381 382
    }
#endif
383 384
}

385 386
static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
                             const struct iovec *iov,
387
                             int iovcnt, off_t offset)
388
{
389 390
    ssize_t ret
;
391
#ifdef CONFIG_PREADV
392
    ret = pwritev(fs->fd, iov, iovcnt, offset);
393
#else
394
    int err = lseek(fs->fd, offset, SEEK_SET);
395 396 397
    if (err == -1) {
        return err;
    } else {
398
        ret = writev(fs->fd, iov, iovcnt);
399 400
    }
#endif
401 402 403 404 405 406 407
#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.
         */
408
        sync_file_range(fs->fd, offset, ret,
409 410 411 412
                        SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
    }
#endif
    return ret;
413 414
}

415
static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
416
{
417
    char buffer[PATH_MAX];
418 419
    char *path = fs_path->data;

420
    if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
421
        return local_set_xattr(rpath(fs_ctx, path, buffer), credp);
422 423
    } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
        return local_set_mapped_file_attr(fs_ctx, path, credp);
424 425
    } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
               (fs_ctx->export_flags & V9FS_SM_NONE)) {
426
        return chmod(rpath(fs_ctx, path, buffer), credp->fc_mode);
427 428
    }
    return -1;
429 430
}

431 432
static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
                       const char *name, FsCred *credp)
433
{
434
    char *path;
435 436
    int err = -1;
    int serrno = 0;
437
    V9fsString fullname;
438
    char buffer[PATH_MAX];
439

440 441 442 443
    v9fs_string_init(&fullname);
    v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
    path = fullname.data;

444
    /* Determine the security model */
445
    if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
446 447
        err = mknod(rpath(fs_ctx, path, buffer),
                SM_LOCAL_MODE_BITS|S_IFREG, 0);
448
        if (err == -1) {
449
            goto out;
450
        }
451
        err = local_set_xattr(rpath(fs_ctx, path, buffer), credp);
452 453 454 455
        if (err == -1) {
            serrno = errno;
            goto err_end;
        }
456 457 458 459 460 461 462 463 464 465 466 467
    } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {

        err = mknod(rpath(fs_ctx, path, buffer),
                    SM_LOCAL_MODE_BITS|S_IFREG, 0);
        if (err == -1) {
            goto out;
        }
        err = local_set_mapped_file_attr(fs_ctx, path, credp);
        if (err == -1) {
            serrno = errno;
            goto err_end;
        }
468 469
    } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
               (fs_ctx->export_flags & V9FS_SM_NONE)) {
470 471
        err = mknod(rpath(fs_ctx, path, buffer), credp->fc_mode,
                credp->fc_rdev);
472
        if (err == -1) {
473
            goto out;
474 475 476 477 478 479 480
        }
        err = local_post_create_passthrough(fs_ctx, path, credp);
        if (err == -1) {
            serrno = errno;
            goto err_end;
        }
    }
481
    goto out;
482 483

err_end:
484
    remove(rpath(fs_ctx, path, buffer));
485
    errno = serrno;
486 487
out:
    v9fs_string_free(&fullname);
488
    return err;
489 490
}

491 492
static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
                       const char *name, FsCred *credp)
493
{
494
    char *path;
495 496
    int err = -1;
    int serrno = 0;
497
    V9fsString fullname;
498
    char buffer[PATH_MAX];
499

500 501 502 503
    v9fs_string_init(&fullname);
    v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
    path = fullname.data;

504
    /* Determine the security model */
505
    if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
506
        err = mkdir(rpath(fs_ctx, path, buffer), SM_LOCAL_DIR_MODE_BITS);
507
        if (err == -1) {
508
            goto out;
509 510
        }
        credp->fc_mode = credp->fc_mode|S_IFDIR;
511
        err = local_set_xattr(rpath(fs_ctx, path, buffer), credp);
512 513 514 515
        if (err == -1) {
            serrno = errno;
            goto err_end;
        }
516 517 518 519 520 521 522 523 524 525 526
    } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
        err = mkdir(rpath(fs_ctx, path, buffer), SM_LOCAL_DIR_MODE_BITS);
        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;
        }
527 528
    } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
               (fs_ctx->export_flags & V9FS_SM_NONE)) {
529
        err = mkdir(rpath(fs_ctx, path, buffer), credp->fc_mode);
530
        if (err == -1) {
531
            goto out;
532 533 534 535 536 537 538
        }
        err = local_post_create_passthrough(fs_ctx, path, credp);
        if (err == -1) {
            serrno = errno;
            goto err_end;
        }
    }
539
    goto out;
540 541

err_end:
542
    remove(rpath(fs_ctx, path, buffer));
543
    errno = serrno;
544 545
out:
    v9fs_string_free(&fullname);
546
    return err;
547 548
}

549
static int local_fstat(FsContext *fs_ctx, int fid_type,
550
                       V9fsFidOpenState *fs, struct stat *stbuf)
551
{
552 553 554 555 556 557 558 559 560
    int err, fd;

    if (fid_type == P9_FID_DIR) {
        fd = dirfd(fs->dir);
    } else {
        fd = fs->fd;
    }

    err = fstat(fd, stbuf);
561 562 563
    if (err) {
        return err;
    }
564
    if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
565 566 567 568 569 570
        /* Actual credentials are part of extended attrs */
        uid_t tmp_uid;
        gid_t tmp_gid;
        mode_t tmp_mode;
        dev_t tmp_dev;

571
        if (fgetxattr(fd, "user.virtfs.uid",
572
                      &tmp_uid, sizeof(uid_t)) > 0) {
573 574
            stbuf->st_uid = tmp_uid;
        }
575
        if (fgetxattr(fd, "user.virtfs.gid",
576
                      &tmp_gid, sizeof(gid_t)) > 0) {
577 578
            stbuf->st_gid = tmp_gid;
        }
579
        if (fgetxattr(fd, "user.virtfs.mode",
580
                      &tmp_mode, sizeof(mode_t)) > 0) {
581 582
            stbuf->st_mode = tmp_mode;
        }
583
        if (fgetxattr(fd, "user.virtfs.rdev",
584
                      &tmp_dev, sizeof(dev_t)) > 0) {
585 586
                stbuf->st_rdev = tmp_dev;
        }
587 588 589
    } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
        errno = EOPNOTSUPP;
        return -1;
590 591
    }
    return err;
592 593
}

594
static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
595
                       int flags, FsCred *credp, V9fsFidOpenState *fs)
596
{
597
    char *path;
598 599 600
    int fd = -1;
    int err = -1;
    int serrno = 0;
601
    V9fsString fullname;
602
    char buffer[PATH_MAX];
603

604 605 606 607
    v9fs_string_init(&fullname);
    v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
    path = fullname.data;

608
    /* Determine the security model */
609
    if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
610
        fd = open(rpath(fs_ctx, path, buffer), flags, SM_LOCAL_MODE_BITS);
611
        if (fd == -1) {
612 613
            err = fd;
            goto out;
614 615 616
        }
        credp->fc_mode = credp->fc_mode|S_IFREG;
        /* Set cleint credentials in xattr */
617
        err = local_set_xattr(rpath(fs_ctx, path, buffer), credp);
618 619 620 621
        if (err == -1) {
            serrno = errno;
            goto err_end;
        }
622 623 624 625 626 627 628 629 630 631 632 633 634
    } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
        fd = open(rpath(fs_ctx, path, buffer), flags, SM_LOCAL_MODE_BITS);
        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;
        }
635 636
    } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
               (fs_ctx->export_flags & V9FS_SM_NONE)) {
637
        fd = open(rpath(fs_ctx, path, buffer), flags, credp->fc_mode);
638
        if (fd == -1) {
639 640
            err = fd;
            goto out;
641 642 643 644 645 646 647
        }
        err = local_post_create_passthrough(fs_ctx, path, credp);
        if (err == -1) {
            serrno = errno;
            goto err_end;
        }
    }
648
    err = fd;
649
    fs->fd = fd;
650
    goto out;
651 652 653

err_end:
    close(fd);
654
    remove(rpath(fs_ctx, path, buffer));
655
    errno = serrno;
656 657
out:
    v9fs_string_free(&fullname);
658
    return err;
659 660
}

661

662
static int local_symlink(FsContext *fs_ctx, const char *oldpath,
663
                         V9fsPath *dir_path, const char *name, FsCred *credp)
664
{
665 666
    int err = -1;
    int serrno = 0;
667 668
    char *newpath;
    V9fsString fullname;
669
    char buffer[PATH_MAX];
670

671 672 673 674
    v9fs_string_init(&fullname);
    v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
    newpath = fullname.data;

675
    /* Determine the security model */
676
    if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
677 678
        int fd;
        ssize_t oldpath_size, write_size;
679
        fd = open(rpath(fs_ctx, newpath, buffer), O_CREAT|O_EXCL|O_RDWR,
680 681
                SM_LOCAL_MODE_BITS);
        if (fd == -1) {
682 683
            err = fd;
            goto out;
684 685
        }
        /* Write the oldpath (target) to the file. */
686
        oldpath_size = strlen(oldpath);
687 688 689 690 691 692 693 694 695 696 697 698 699
        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;
700
        err = local_set_xattr(rpath(fs_ctx, newpath, buffer), credp);
701 702 703 704
        if (err == -1) {
            serrno = errno;
            goto err_end;
        }
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
    } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
        int fd;
        ssize_t oldpath_size, write_size;
        fd = open(rpath(fs_ctx, newpath, buffer), O_CREAT|O_EXCL|O_RDWR,
                  SM_LOCAL_MODE_BITS);
        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;
        }
734 735
    } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
               (fs_ctx->export_flags & V9FS_SM_NONE)) {
736
        err = symlink(oldpath, rpath(fs_ctx, newpath, buffer));
737
        if (err) {
738
            goto out;
739
        }
740
        err = lchown(rpath(fs_ctx, newpath, buffer), credp->fc_uid,
741
                     credp->fc_gid);
742
        if (err == -1) {
743 744 745 746
            /*
             * If we fail to change ownership and if we are
             * using security model none. Ignore the error
             */
747
            if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
748 749 750 751
                serrno = errno;
                goto err_end;
            } else
                err = 0;
752 753
        }
    }
754
    goto out;
755 756

err_end:
757
    remove(rpath(fs_ctx, newpath, buffer));
758
    errno = serrno;
759 760
out:
    v9fs_string_free(&fullname);
761
    return err;
762 763
}

764 765
static int local_link(FsContext *ctx, V9fsPath *oldpath,
                      V9fsPath *dirpath, const char *name)
766
{
767 768
    int ret;
    V9fsString newpath;
769
    char buffer[PATH_MAX], buffer1[PATH_MAX];
770

771 772 773 774 775
    v9fs_string_init(&newpath);
    v9fs_string_sprintf(&newpath, "%s/%s", dirpath->data, name);

    ret = link(rpath(ctx, oldpath->data, buffer),
               rpath(ctx, newpath.data, buffer1));
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790

    /* 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;
        }
        ret = link(local_mapped_attr_path(ctx, oldpath->data, buffer),
                   local_mapped_attr_path(ctx, newpath.data, buffer1));
        if (ret < 0 && errno != ENOENT) {
            goto err_out;
        }
    }
err_out:
791 792
    v9fs_string_free(&newpath);
    return ret;
793 794
}

795
static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
796
{
797
    char buffer[PATH_MAX];
798 799
    char *path = fs_path->data;

800
    return truncate(rpath(ctx, path, buffer), size);
801 802 803 804 805
}

static int local_rename(FsContext *ctx, const char *oldpath,
                        const char *newpath)
{
806
    int err;
807
    char buffer[PATH_MAX], buffer1[PATH_MAX];
808

809 810 811 812 813 814 815 816 817 818 819 820
    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 */
        err = rename(local_mapped_attr_path(ctx, oldpath, buffer),
                     local_mapped_attr_path(ctx, newpath, buffer1));
        if (err < 0 && errno != ENOENT) {
            return err;
        }
    }
821
    return rename(rpath(ctx, oldpath, buffer), rpath(ctx, newpath, buffer1));
822 823
}

824
static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
825
{
826
    char buffer[PATH_MAX];
827 828
    char *path = fs_path->data;

829
    if ((credp->fc_uid == -1 && credp->fc_gid == -1) ||
830 831 832 833
        (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
        (fs_ctx->export_flags & V9FS_SM_NONE)) {
        return lchown(rpath(fs_ctx, path, buffer),
                      credp->fc_uid, credp->fc_gid);
834
    } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
835
        return local_set_xattr(rpath(fs_ctx, path, buffer), credp);
836 837
    } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
        return local_set_mapped_file_attr(fs_ctx, path, credp);
838 839
    }
    return -1;
840 841
}

842
static int local_utimensat(FsContext *s, V9fsPath *fs_path,
843
                           const struct timespec *buf)
844
{
845
    char buffer[PATH_MAX];
846 847
    char *path = fs_path->data;

848
    return qemu_utimens(rpath(s, path, buffer), buf);
849 850
}

851 852
static int local_remove(FsContext *ctx, const char *path)
{
853 854
    int err;
    struct stat stbuf;
855
    char buffer[PATH_MAX];
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880

    if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
        err =  lstat(rpath(ctx, path, buffer), &stbuf);
        if (err) {
            goto err_out;
        }
        /*
         * If directory remove .virtfs_metadata contained in the
         * directory
         */
        if (S_ISDIR(stbuf.st_mode)) {
            sprintf(buffer, "%s/%s/%s", ctx->fs_root, path, VIRTFS_META_DIR);
            err = remove(buffer);
            if (err < 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
         */
D
Dong Xu Wang 已提交
881
        err = remove(local_mapped_attr_path(ctx, path, buffer));
882 883 884 885 886 887 888 889
        if (err < 0 && errno != ENOENT) {
            /*
             * We didn't had the .virtfs_metadata file. May be file created
             * in non-mapped mode ?. Ignore ENOENT.
             */
            goto err_out;
        }
    }
890
    return remove(rpath(ctx, path, buffer));
891 892
err_out:
    return err;
893 894
}

895 896
static int local_fsync(FsContext *ctx, int fid_type,
                       V9fsFidOpenState *fs, int datasync)
897
{
898 899 900 901 902 903 904 905
    int fd;

    if (fid_type == P9_FID_DIR) {
        fd = dirfd(fs->dir);
    } else {
        fd = fs->fd;
    }

906
    if (datasync) {
907
        return qemu_fdatasync(fd);
908
    } else {
909
        return fsync(fd);
910
    }
911 912
}

913
static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
914
{
915
    char buffer[PATH_MAX];
916 917 918
    char *path = fs_path->data;

    return statfs(rpath(s, path, buffer), stbuf);
919 920
}

921
static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
922 923
                               const char *name, void *value, size_t size)
{
924 925
    char *path = fs_path->data;

926
    return v9fs_get_xattr(ctx, path, name, value, size);
927 928
}

929
static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path,
930 931
                                void *value, size_t size)
{
932 933
    char *path = fs_path->data;

934
    return v9fs_list_xattr(ctx, path, value, size);
935 936
}

937
static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
938 939
                           void *value, size_t size, int flags)
{
940 941
    char *path = fs_path->data;

942
    return v9fs_set_xattr(ctx, path, name, value, size, flags);
943 944
}

945 946
static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
                              const char *name)
947
{
948 949
    char *path = fs_path->data;

950
    return v9fs_remove_xattr(ctx, path, name);
951 952
}

953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path,
                              const char *name, V9fsPath *target)
{
    if (dir_path) {
        v9fs_string_sprintf((V9fsString *)target, "%s/%s",
                            dir_path->data, name);
    } else {
        v9fs_string_sprintf((V9fsString *)target, "%s", name);
    }
    /* Bump the size for including terminating NULL */
    target->size++;
    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;
    V9fsString fullname;
    char buffer[PATH_MAX];
992

993 994 995
    v9fs_string_init(&fullname);

    v9fs_string_sprintf(&fullname, "%s/%s", dir->data, name);
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
    if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
        if (flags == AT_REMOVEDIR) {
            /*
             * If directory remove .virtfs_metadata contained in the
             * directory
             */
            sprintf(buffer, "%s/%s/%s", ctx->fs_root,
                    fullname.data, VIRTFS_META_DIR);
            ret = remove(buffer);
            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.
         */
        ret = remove(local_mapped_attr_path(ctx, fullname.data, buffer));
        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;
        }
    }
    /* Remove the name finally */
1027 1028 1029
    ret = remove(rpath(ctx, fullname.data, buffer));
    v9fs_string_free(&fullname);

1030
err_out:
1031 1032
    return ret;
}
1033

1034 1035 1036
static int local_ioc_getversion(FsContext *ctx, V9fsPath *path,
                                mode_t st_mode, uint64_t *st_gen)
{
1037
    int err;
1038
#ifdef FS_IOC_GETVERSION
1039 1040
    V9fsFidOpenState fid_open;

1041 1042 1043 1044 1045 1046 1047
    /*
     * 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)) {
            return 0;
    }
1048 1049 1050
    err = local_open(ctx, path, O_RDONLY, &fid_open);
    if (err < 0) {
        return err;
1051
    }
1052 1053
    err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
    local_close(ctx, &fid_open);
1054 1055 1056
#else
    err = -ENOTTY;
#endif
1057 1058 1059
    return err;
}

1060 1061
static int local_init(FsContext *ctx)
{
1062
    int err = 0;
1063 1064
    struct statfs stbuf;

1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
    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;
    }
1078
    ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT;
1079 1080 1081 1082
#ifdef FS_IOC_GETVERSION
    /*
     * use ioc_getversion only if the iocl is definied
     */
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
    err = statfs(ctx->fs_root, &stbuf);
    if (!err) {
        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;
        }
    }
1094
#endif
1095
    return err;
1096 1097
}

1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
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) {
        fprintf(stderr, "security model not specified, "
                "local fs needs security model\nvalid options are:"
                "\tsecurity_model=[passthrough|mapped|none]\n");
        return -1;
    }

    if (!strcmp(sec_model, "passthrough")) {
        fse->export_flags |= V9FS_SM_PASSTHROUGH;
1112 1113
    } else if (!strcmp(sec_model, "mapped") ||
               !strcmp(sec_model, "mapped-xattr")) {
1114 1115 1116
        fse->export_flags |= V9FS_SM_MAPPED;
    } else if (!strcmp(sec_model, "none")) {
        fse->export_flags |= V9FS_SM_NONE;
1117 1118
    } else if (!strcmp(sec_model, "mapped-file")) {
        fse->export_flags |= V9FS_SM_MAPPED_FILE;
1119 1120
    } else {
        fprintf(stderr, "Invalid security model %s specified, valid options are"
1121 1122
                "\n\t [passthrough|mapped-xattr|mapped-file|none]\n",
                sec_model);
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
        return -1;
    }

    if (!path) {
        fprintf(stderr, "fsdev: No path specified.\n");
        return -1;
    }
    fse->path = g_strdup(path);

    return 0;
}

1135
FileOperations local_ops = {
1136
    .parse_opts = local_parse_opts,
1137
    .init  = local_init,
1138 1139 1140 1141
    .lstat = local_lstat,
    .readlink = local_readlink,
    .close = local_close,
    .closedir = local_closedir,
1142 1143
    .open = local_open,
    .opendir = local_opendir,
1144 1145
    .rewinddir = local_rewinddir,
    .telldir = local_telldir,
1146
    .readdir_r = local_readdir_r,
1147
    .seekdir = local_seekdir,
1148 1149
    .preadv = local_preadv,
    .pwritev = local_pwritev,
1150 1151 1152 1153 1154 1155 1156
    .chmod = local_chmod,
    .mknod = local_mknod,
    .mkdir = local_mkdir,
    .fstat = local_fstat,
    .open2 = local_open2,
    .symlink = local_symlink,
    .link = local_link,
1157 1158 1159
    .truncate = local_truncate,
    .rename = local_rename,
    .chown = local_chown,
M
M. Mohan Kumar 已提交
1160
    .utimensat = local_utimensat,
1161
    .remove = local_remove,
1162
    .fsync = local_fsync,
1163
    .statfs = local_statfs,
1164 1165
    .lgetxattr = local_lgetxattr,
    .llistxattr = local_llistxattr,
1166
    .lsetxattr = local_lsetxattr,
1167
    .lremovexattr = local_lremovexattr,
1168 1169 1170
    .name_to_path = local_name_to_path,
    .renameat  = local_renameat,
    .unlinkat = local_unlinkat,
1171
};