qemu/hw/9pfs/9p-handle.c
<<
>>
Prefs
   1/*
   2 * 9p handle callback
   3 *
   4 * Copyright IBM, Corp. 2011
   5 *
   6 * Authors:
   7 *    Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
   8 *
   9 * This work is licensed under the terms of the GNU GPL, version 2.  See
  10 * the COPYING file in the top-level directory.
  11 *
  12 */
  13
  14#include "qemu/osdep.h"
  15#include "9p.h"
  16#include "9p-xattr.h"
  17#include <arpa/inet.h>
  18#include <pwd.h>
  19#include <grp.h>
  20#include <sys/socket.h>
  21#include <sys/un.h>
  22#include "qapi/error.h"
  23#include "qemu/xattr.h"
  24#include "qemu/cutils.h"
  25#include "qemu/error-report.h"
  26#include "qemu/option.h"
  27#include <linux/fs.h>
  28#ifdef CONFIG_LINUX_MAGIC_H
  29#include <linux/magic.h>
  30#endif
  31#include <sys/ioctl.h>
  32
  33#ifndef XFS_SUPER_MAGIC
  34#define XFS_SUPER_MAGIC  0x58465342
  35#endif
  36#ifndef EXT2_SUPER_MAGIC
  37#define EXT2_SUPER_MAGIC 0xEF53
  38#endif
  39#ifndef REISERFS_SUPER_MAGIC
  40#define REISERFS_SUPER_MAGIC 0x52654973
  41#endif
  42#ifndef BTRFS_SUPER_MAGIC
  43#define BTRFS_SUPER_MAGIC 0x9123683E
  44#endif
  45
  46typedef struct HandleData {
  47    int mountfd;
  48    int handle_bytes;
  49} HandleData;
  50
  51static inline int name_to_handle(int dirfd, const char *name,
  52                                 struct file_handle *fh, int *mnt_id, int flags)
  53{
  54    return name_to_handle_at(dirfd, name, fh, mnt_id, flags);
  55}
  56
  57static inline int open_by_handle(int mountfd, const char *fh, int flags)
  58{
  59    return open_by_handle_at(mountfd, (struct file_handle *)fh, flags);
  60}
  61
  62static int handle_update_file_cred(int dirfd, const char *name, FsCred *credp)
  63{
  64    int fd, ret;
  65    fd = openat(dirfd, name, O_NONBLOCK | O_NOFOLLOW);
  66    if (fd < 0) {
  67        return fd;
  68    }
  69    ret = fchownat(fd, "", credp->fc_uid, credp->fc_gid, AT_EMPTY_PATH);
  70    if (ret < 0) {
  71        goto err_out;
  72    }
  73    ret = fchmod(fd, credp->fc_mode & 07777);
  74err_out:
  75    close(fd);
  76    return ret;
  77}
  78
  79
  80static int handle_lstat(FsContext *fs_ctx, V9fsPath *fs_path,
  81                        struct stat *stbuf)
  82{
  83    int fd, ret;
  84    HandleData *data = (HandleData *) fs_ctx->private;
  85
  86    fd = open_by_handle(data->mountfd, fs_path->data, O_PATH);
  87    if (fd < 0) {
  88        return fd;
  89    }
  90    ret = fstatat(fd, "", stbuf, AT_EMPTY_PATH);
  91    close(fd);
  92    return ret;
  93}
  94
  95static ssize_t handle_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
  96                               char *buf, size_t bufsz)
  97{
  98    int fd, ret;
  99    HandleData *data = (HandleData *) fs_ctx->private;
 100
 101    fd = open_by_handle(data->mountfd, fs_path->data, O_PATH);
 102    if (fd < 0) {
 103        return fd;
 104    }
 105    ret = readlinkat(fd, "", buf, bufsz);
 106    close(fd);
 107    return ret;
 108}
 109
 110static int handle_close(FsContext *ctx, V9fsFidOpenState *fs)
 111{
 112    return close(fs->fd);
 113}
 114
 115static int handle_closedir(FsContext *ctx, V9fsFidOpenState *fs)
 116{
 117    return closedir(fs->dir.stream);
 118}
 119
 120static int handle_open(FsContext *ctx, V9fsPath *fs_path,
 121                       int flags, V9fsFidOpenState *fs)
 122{
 123    HandleData *data = (HandleData *) ctx->private;
 124
 125    fs->fd = open_by_handle(data->mountfd, fs_path->data, flags);
 126    return fs->fd;
 127}
 128
 129static int handle_opendir(FsContext *ctx,
 130                          V9fsPath *fs_path, V9fsFidOpenState *fs)
 131{
 132    int ret;
 133    ret = handle_open(ctx, fs_path, O_DIRECTORY, fs);
 134    if (ret < 0) {
 135        return -1;
 136    }
 137    fs->dir.stream = fdopendir(ret);
 138    if (!fs->dir.stream) {
 139        return -1;
 140    }
 141    return 0;
 142}
 143
 144static void handle_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
 145{
 146    rewinddir(fs->dir.stream);
 147}
 148
 149static off_t handle_telldir(FsContext *ctx, V9fsFidOpenState *fs)
 150{
 151    return telldir(fs->dir.stream);
 152}
 153
 154static struct dirent *handle_readdir(FsContext *ctx, V9fsFidOpenState *fs)
 155{
 156    return readdir(fs->dir.stream);
 157}
 158
 159static void handle_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
 160{
 161    seekdir(fs->dir.stream, off);
 162}
 163
 164static ssize_t handle_preadv(FsContext *ctx, V9fsFidOpenState *fs,
 165                             const struct iovec *iov,
 166                             int iovcnt, off_t offset)
 167{
 168#ifdef CONFIG_PREADV
 169    return preadv(fs->fd, iov, iovcnt, offset);
 170#else
 171    int err = lseek(fs->fd, offset, SEEK_SET);
 172    if (err == -1) {
 173        return err;
 174    } else {
 175        return readv(fs->fd, iov, iovcnt);
 176    }
 177#endif
 178}
 179
 180static ssize_t handle_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
 181                              const struct iovec *iov,
 182                              int iovcnt, off_t offset)
 183{
 184    ssize_t ret;
 185#ifdef CONFIG_PREADV
 186    ret = pwritev(fs->fd, iov, iovcnt, offset);
 187#else
 188    int err = lseek(fs->fd, offset, SEEK_SET);
 189    if (err == -1) {
 190        return err;
 191    } else {
 192        ret = writev(fs->fd, iov, iovcnt);
 193    }
 194#endif
 195#ifdef CONFIG_SYNC_FILE_RANGE
 196    if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
 197        /*
 198         * Initiate a writeback. This is not a data integrity sync.
 199         * We want to ensure that we don't leave dirty pages in the cache
 200         * after write when writeout=immediate is sepcified.
 201         */
 202        sync_file_range(fs->fd, offset, ret,
 203                        SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
 204    }
 205#endif
 206    return ret;
 207}
 208
 209static int handle_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
 210{
 211    int fd, ret;
 212    HandleData *data = (HandleData *) fs_ctx->private;
 213
 214    fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
 215    if (fd < 0) {
 216        return fd;
 217    }
 218    ret = fchmod(fd, credp->fc_mode);
 219    close(fd);
 220    return ret;
 221}
 222
 223static int handle_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
 224                       const char *name, FsCred *credp)
 225{
 226    int dirfd, ret;
 227    HandleData *data = (HandleData *) fs_ctx->private;
 228
 229    dirfd = open_by_handle(data->mountfd, dir_path->data, O_PATH);
 230    if (dirfd < 0) {
 231        return dirfd;
 232    }
 233    ret = mknodat(dirfd, name, credp->fc_mode, credp->fc_rdev);
 234    if (!ret) {
 235        ret = handle_update_file_cred(dirfd, name, credp);
 236    }
 237    close(dirfd);
 238    return ret;
 239}
 240
 241static int handle_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
 242                       const char *name, FsCred *credp)
 243{
 244    int dirfd, ret;
 245    HandleData *data = (HandleData *) fs_ctx->private;
 246
 247    dirfd = open_by_handle(data->mountfd, dir_path->data, O_PATH);
 248    if (dirfd < 0) {
 249        return dirfd;
 250    }
 251    ret = mkdirat(dirfd, name, credp->fc_mode);
 252    if (!ret) {
 253        ret = handle_update_file_cred(dirfd, name, credp);
 254    }
 255    close(dirfd);
 256    return ret;
 257}
 258
 259static int handle_fstat(FsContext *fs_ctx, int fid_type,
 260                        V9fsFidOpenState *fs, struct stat *stbuf)
 261{
 262    int fd;
 263
 264    if (fid_type == P9_FID_DIR) {
 265        fd = dirfd(fs->dir.stream);
 266    } else {
 267        fd = fs->fd;
 268    }
 269    return fstat(fd, stbuf);
 270}
 271
 272static int handle_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
 273                        int flags, FsCred *credp, V9fsFidOpenState *fs)
 274{
 275    int ret;
 276    int dirfd, fd;
 277    HandleData *data = (HandleData *) fs_ctx->private;
 278
 279    dirfd = open_by_handle(data->mountfd, dir_path->data, O_PATH);
 280    if (dirfd < 0) {
 281        return dirfd;
 282    }
 283    fd = openat(dirfd, name, flags | O_NOFOLLOW, credp->fc_mode);
 284    if (fd >= 0) {
 285        ret = handle_update_file_cred(dirfd, name, credp);
 286        if (ret < 0) {
 287            close(fd);
 288            fd = ret;
 289        } else {
 290            fs->fd = fd;
 291        }
 292    }
 293    close(dirfd);
 294    return fd;
 295}
 296
 297
 298static int handle_symlink(FsContext *fs_ctx, const char *oldpath,
 299                          V9fsPath *dir_path, const char *name, FsCred *credp)
 300{
 301    int fd, dirfd, ret;
 302    HandleData *data = (HandleData *) fs_ctx->private;
 303
 304    dirfd = open_by_handle(data->mountfd, dir_path->data, O_PATH);
 305    if (dirfd < 0) {
 306        return dirfd;
 307    }
 308    ret = symlinkat(oldpath, dirfd, name);
 309    if (!ret) {
 310        fd = openat(dirfd, name, O_PATH | O_NOFOLLOW);
 311        if (fd < 0) {
 312            ret = fd;
 313            goto err_out;
 314        }
 315        ret = fchownat(fd, "", credp->fc_uid, credp->fc_gid, AT_EMPTY_PATH);
 316        close(fd);
 317    }
 318err_out:
 319    close(dirfd);
 320    return ret;
 321}
 322
 323static int handle_link(FsContext *ctx, V9fsPath *oldpath,
 324                       V9fsPath *dirpath, const char *name)
 325{
 326    int oldfd, newdirfd, ret;
 327    HandleData *data = (HandleData *) ctx->private;
 328
 329    oldfd = open_by_handle(data->mountfd, oldpath->data, O_PATH);
 330    if (oldfd < 0) {
 331        return oldfd;
 332    }
 333    newdirfd = open_by_handle(data->mountfd, dirpath->data, O_PATH);
 334    if (newdirfd < 0) {
 335        close(oldfd);
 336        return newdirfd;
 337    }
 338    ret = linkat(oldfd, "", newdirfd, name, AT_EMPTY_PATH);
 339    close(newdirfd);
 340    close(oldfd);
 341    return ret;
 342}
 343
 344static int handle_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
 345{
 346    int fd, ret;
 347    HandleData *data = (HandleData *) ctx->private;
 348
 349    fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK | O_WRONLY);
 350    if (fd < 0) {
 351        return fd;
 352    }
 353    ret = ftruncate(fd, size);
 354    close(fd);
 355    return ret;
 356}
 357
 358static int handle_rename(FsContext *ctx, const char *oldpath,
 359                         const char *newpath)
 360{
 361    errno = EOPNOTSUPP;
 362    return -1;
 363}
 364
 365static int handle_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
 366{
 367    int fd, ret;
 368    HandleData *data = (HandleData *) fs_ctx->private;
 369
 370    fd = open_by_handle(data->mountfd, fs_path->data, O_PATH);
 371    if (fd < 0) {
 372        return fd;
 373    }
 374    ret = fchownat(fd, "", credp->fc_uid, credp->fc_gid, AT_EMPTY_PATH);
 375    close(fd);
 376    return ret;
 377}
 378
 379static int handle_utimensat(FsContext *ctx, V9fsPath *fs_path,
 380                            const struct timespec *buf)
 381{
 382    int ret;
 383    int fd;
 384    HandleData *data = (HandleData *) ctx->private;
 385
 386    fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
 387    if (fd < 0) {
 388        return fd;
 389    }
 390    ret = futimens(fd, buf);
 391    close(fd);
 392    return ret;
 393}
 394
 395static int handle_remove(FsContext *ctx, const char *path)
 396{
 397    errno = EOPNOTSUPP;
 398    return -1;
 399}
 400
 401static int handle_fsync(FsContext *ctx, int fid_type,
 402                        V9fsFidOpenState *fs, int datasync)
 403{
 404    int fd;
 405
 406    if (fid_type == P9_FID_DIR) {
 407        fd = dirfd(fs->dir.stream);
 408    } else {
 409        fd = fs->fd;
 410    }
 411
 412    if (datasync) {
 413        return qemu_fdatasync(fd);
 414    } else {
 415        return fsync(fd);
 416    }
 417}
 418
 419static int handle_statfs(FsContext *ctx, V9fsPath *fs_path,
 420                         struct statfs *stbuf)
 421{
 422    int fd, ret;
 423    HandleData *data = (HandleData *) ctx->private;
 424
 425    fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
 426    if (fd < 0) {
 427        return fd;
 428    }
 429    ret = fstatfs(fd, stbuf);
 430    close(fd);
 431    return ret;
 432}
 433
 434static ssize_t handle_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
 435                                const char *name, void *value, size_t size)
 436{
 437    int fd, ret;
 438    HandleData *data = (HandleData *) ctx->private;
 439
 440    fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
 441    if (fd < 0) {
 442        return fd;
 443    }
 444    ret = fgetxattr(fd, name, value, size);
 445    close(fd);
 446    return ret;
 447}
 448
 449static ssize_t handle_llistxattr(FsContext *ctx, V9fsPath *fs_path,
 450                                 void *value, size_t size)
 451{
 452    int fd, ret;
 453    HandleData *data = (HandleData *) ctx->private;
 454
 455    fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
 456    if (fd < 0) {
 457        return fd;
 458    }
 459    ret = flistxattr(fd, value, size);
 460    close(fd);
 461    return ret;
 462}
 463
 464static int handle_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
 465                            void *value, size_t size, int flags)
 466{
 467    int fd, ret;
 468    HandleData *data = (HandleData *) ctx->private;
 469
 470    fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
 471    if (fd < 0) {
 472        return fd;
 473    }
 474    ret = fsetxattr(fd, name, value, size, flags);
 475    close(fd);
 476    return ret;
 477}
 478
 479static int handle_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
 480                               const char *name)
 481{
 482    int fd, ret;
 483    HandleData *data = (HandleData *) ctx->private;
 484
 485    fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
 486    if (fd < 0) {
 487        return fd;
 488    }
 489    ret = fremovexattr(fd, name);
 490    close(fd);
 491    return ret;
 492}
 493
 494static int handle_name_to_path(FsContext *ctx, V9fsPath *dir_path,
 495                              const char *name, V9fsPath *target)
 496{
 497    char *buffer;
 498    struct file_handle *fh;
 499    int dirfd, ret, mnt_id;
 500    HandleData *data = (HandleData *) ctx->private;
 501
 502    /* "." and ".." are not allowed */
 503    if (!strcmp(name, ".") || !strcmp(name, "..")) {
 504        errno = EINVAL;
 505        return -1;
 506
 507    }
 508    if (dir_path) {
 509        dirfd = open_by_handle(data->mountfd, dir_path->data, O_PATH);
 510    } else {
 511        /* relative to export root */
 512        buffer = rpath(ctx, ".");
 513        dirfd = open(buffer, O_DIRECTORY);
 514        g_free(buffer);
 515    }
 516    if (dirfd < 0) {
 517        return dirfd;
 518    }
 519    fh = g_malloc(sizeof(struct file_handle) + data->handle_bytes);
 520    fh->handle_bytes = data->handle_bytes;
 521    /* add a "./" at the beginning of the path */
 522    buffer = g_strdup_printf("./%s", name);
 523    /* flag = 0 imply don't follow symlink */
 524    ret = name_to_handle(dirfd, buffer, fh, &mnt_id, 0);
 525    if (!ret) {
 526        target->data = (char *)fh;
 527        target->size = sizeof(struct file_handle) + data->handle_bytes;
 528    } else {
 529        g_free(fh);
 530    }
 531    close(dirfd);
 532    g_free(buffer);
 533    return ret;
 534}
 535
 536static int handle_renameat(FsContext *ctx, V9fsPath *olddir,
 537                           const char *old_name, V9fsPath *newdir,
 538                           const char *new_name)
 539{
 540    int olddirfd, newdirfd, ret;
 541    HandleData *data = (HandleData *) ctx->private;
 542
 543    olddirfd = open_by_handle(data->mountfd, olddir->data, O_PATH);
 544    if (olddirfd < 0) {
 545        return olddirfd;
 546    }
 547    newdirfd = open_by_handle(data->mountfd, newdir->data, O_PATH);
 548    if (newdirfd < 0) {
 549        close(olddirfd);
 550        return newdirfd;
 551    }
 552    ret = renameat(olddirfd, old_name, newdirfd, new_name);
 553    close(newdirfd);
 554    close(olddirfd);
 555    return ret;
 556}
 557
 558static int handle_unlinkat(FsContext *ctx, V9fsPath *dir,
 559                           const char *name, int flags)
 560{
 561    int dirfd, ret;
 562    HandleData *data = (HandleData *) ctx->private;
 563
 564    dirfd = open_by_handle(data->mountfd, dir->data, O_PATH);
 565    if (dirfd < 0) {
 566        return dirfd;
 567    }
 568
 569    ret = unlinkat(dirfd, name, flags);
 570
 571    close(dirfd);
 572    return ret;
 573}
 574
 575static int handle_ioc_getversion(FsContext *ctx, V9fsPath *path,
 576                                 mode_t st_mode, uint64_t *st_gen)
 577{
 578#ifdef FS_IOC_GETVERSION
 579    int err;
 580    V9fsFidOpenState fid_open;
 581
 582    /*
 583     * Do not try to open special files like device nodes, fifos etc
 584     * We can get fd for regular files and directories only
 585     */
 586    if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
 587        errno = ENOTTY;
 588        return -1;
 589    }
 590    err = handle_open(ctx, path, O_RDONLY, &fid_open);
 591    if (err < 0) {
 592        return err;
 593    }
 594    err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
 595    handle_close(ctx, &fid_open);
 596    return err;
 597#else
 598    errno = ENOTTY;
 599    return -1;
 600#endif
 601}
 602
 603static int handle_init(FsContext *ctx, Error **errp)
 604{
 605    int ret, mnt_id;
 606    struct statfs stbuf;
 607    struct file_handle fh;
 608    HandleData *data = g_malloc(sizeof(HandleData));
 609
 610    data->mountfd = open(ctx->fs_root, O_DIRECTORY);
 611    if (data->mountfd < 0) {
 612        ret = data->mountfd;
 613        goto err_out;
 614    }
 615    ret = statfs(ctx->fs_root, &stbuf);
 616    if (!ret) {
 617        switch (stbuf.f_type) {
 618        case EXT2_SUPER_MAGIC:
 619        case BTRFS_SUPER_MAGIC:
 620        case REISERFS_SUPER_MAGIC:
 621        case XFS_SUPER_MAGIC:
 622            ctx->exops.get_st_gen = handle_ioc_getversion;
 623            break;
 624        }
 625    }
 626    memset(&fh, 0, sizeof(struct file_handle));
 627    ret = name_to_handle(data->mountfd, ".", &fh, &mnt_id, 0);
 628    if (ret && errno == EOVERFLOW) {
 629        data->handle_bytes = fh.handle_bytes;
 630        ctx->private = data;
 631        ret = 0;
 632        goto out;
 633    }
 634    /* we got 0 byte handle ? */
 635    ret = -1;
 636    close(data->mountfd);
 637err_out:
 638    g_free(data);
 639out:
 640    return ret;
 641}
 642
 643static void handle_cleanup(FsContext *ctx)
 644{
 645    HandleData *data = ctx->private;
 646
 647    close(data->mountfd);
 648    g_free(data);
 649}
 650
 651static int handle_parse_opts(QemuOpts *opts, FsDriverEntry *fse, Error **errp)
 652{
 653    const char *sec_model = qemu_opt_get(opts, "security_model");
 654    const char *path = qemu_opt_get(opts, "path");
 655
 656    warn_report("handle backend is deprecated");
 657
 658    if (sec_model) {
 659        error_setg(errp,
 660                   "Invalid argument security_model specified with handle fsdriver");
 661        return -1;
 662    }
 663
 664    if (!path) {
 665        error_setg(errp, "fsdev: No path specified");
 666        return -1;
 667    }
 668    fse->path = g_strdup(path);
 669    return 0;
 670
 671}
 672
 673FileOperations handle_ops = {
 674    .parse_opts   = handle_parse_opts,
 675    .init         = handle_init,
 676    .cleanup      = handle_cleanup,
 677    .lstat        = handle_lstat,
 678    .readlink     = handle_readlink,
 679    .close        = handle_close,
 680    .closedir     = handle_closedir,
 681    .open         = handle_open,
 682    .opendir      = handle_opendir,
 683    .rewinddir    = handle_rewinddir,
 684    .telldir      = handle_telldir,
 685    .readdir      = handle_readdir,
 686    .seekdir      = handle_seekdir,
 687    .preadv       = handle_preadv,
 688    .pwritev      = handle_pwritev,
 689    .chmod        = handle_chmod,
 690    .mknod        = handle_mknod,
 691    .mkdir        = handle_mkdir,
 692    .fstat        = handle_fstat,
 693    .open2        = handle_open2,
 694    .symlink      = handle_symlink,
 695    .link         = handle_link,
 696    .truncate     = handle_truncate,
 697    .rename       = handle_rename,
 698    .chown        = handle_chown,
 699    .utimensat    = handle_utimensat,
 700    .remove       = handle_remove,
 701    .fsync        = handle_fsync,
 702    .statfs       = handle_statfs,
 703    .lgetxattr    = handle_lgetxattr,
 704    .llistxattr   = handle_llistxattr,
 705    .lsetxattr    = handle_lsetxattr,
 706    .lremovexattr = handle_lremovexattr,
 707    .name_to_path = handle_name_to_path,
 708    .renameat     = handle_renameat,
 709    .unlinkat     = handle_unlinkat,
 710};
 711