qemu/block/export/fuse.c
<<
>>
Prefs
   1/*
   2 * Present a block device as a raw image through FUSE
   3 *
   4 * Copyright (c) 2020 Max Reitz <mreitz@redhat.com>
   5 *
   6 * This program is free software; you can redistribute it and/or modify
   7 * it under the terms of the GNU General Public License as published by
   8 * the Free Software Foundation; under version 2 or later of the License.
   9 *
  10 * This program is distributed in the hope that it will be useful,
  11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13 * GNU General Public License for more details.
  14 *
  15 * You should have received a copy of the GNU General Public License
  16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
  17 */
  18
  19#define FUSE_USE_VERSION 31
  20
  21#include "qemu/osdep.h"
  22#include "block/aio.h"
  23#include "block/block.h"
  24#include "block/export.h"
  25#include "block/fuse.h"
  26#include "block/qapi.h"
  27#include "qapi/error.h"
  28#include "qapi/qapi-commands-block.h"
  29#include "sysemu/block-backend.h"
  30
  31#include <fuse.h>
  32#include <fuse_lowlevel.h>
  33
  34
  35/* Prevent overly long bounce buffer allocations */
  36#define FUSE_MAX_BOUNCE_BYTES (MIN(BDRV_REQUEST_MAX_BYTES, 64 * 1024 * 1024))
  37
  38
  39typedef struct FuseExport {
  40    BlockExport common;
  41
  42    struct fuse_session *fuse_session;
  43    struct fuse_buf fuse_buf;
  44    bool mounted, fd_handler_set_up;
  45
  46    char *mountpoint;
  47    bool writable;
  48    bool growable;
  49    /* Whether allow_other was used as a mount option or not */
  50    bool allow_other;
  51
  52    mode_t st_mode;
  53    uid_t st_uid;
  54    gid_t st_gid;
  55} FuseExport;
  56
  57static GHashTable *exports;
  58static const struct fuse_lowlevel_ops fuse_ops;
  59
  60static void fuse_export_shutdown(BlockExport *exp);
  61static void fuse_export_delete(BlockExport *exp);
  62
  63static void init_exports_table(void);
  64
  65static int setup_fuse_export(FuseExport *exp, const char *mountpoint,
  66                             bool allow_other, Error **errp);
  67static void read_from_fuse_export(void *opaque);
  68
  69static bool is_regular_file(const char *path, Error **errp);
  70
  71
  72static int fuse_export_create(BlockExport *blk_exp,
  73                              BlockExportOptions *blk_exp_args,
  74                              Error **errp)
  75{
  76    FuseExport *exp = container_of(blk_exp, FuseExport, common);
  77    BlockExportOptionsFuse *args = &blk_exp_args->u.fuse;
  78    int ret;
  79
  80    assert(blk_exp_args->type == BLOCK_EXPORT_TYPE_FUSE);
  81
  82    /* For growable exports, take the RESIZE permission */
  83    if (args->growable) {
  84        uint64_t blk_perm, blk_shared_perm;
  85
  86        blk_get_perm(exp->common.blk, &blk_perm, &blk_shared_perm);
  87
  88        ret = blk_set_perm(exp->common.blk, blk_perm | BLK_PERM_RESIZE,
  89                           blk_shared_perm, errp);
  90        if (ret < 0) {
  91            return ret;
  92        }
  93    }
  94
  95    init_exports_table();
  96
  97    /*
  98     * It is important to do this check before calling is_regular_file() --
  99     * that function will do a stat(), which we would have to handle if we
 100     * already exported something on @mountpoint.  But we cannot, because
 101     * we are currently caught up here.
 102     * (Note that ideally we would want to resolve relative paths here,
 103     * but bdrv_make_absolute_filename() might do the wrong thing for
 104     * paths that contain colons, and realpath() would resolve symlinks,
 105     * which we do not want: The mount point is not going to be the
 106     * symlink's destination, but the link itself.)
 107     * So this will not catch all potential clashes, but hopefully at
 108     * least the most common one of specifying exactly the same path
 109     * string twice.
 110     */
 111    if (g_hash_table_contains(exports, args->mountpoint)) {
 112        error_setg(errp, "There already is a FUSE export on '%s'",
 113                   args->mountpoint);
 114        ret = -EEXIST;
 115        goto fail;
 116    }
 117
 118    if (!is_regular_file(args->mountpoint, errp)) {
 119        ret = -EINVAL;
 120        goto fail;
 121    }
 122
 123    exp->mountpoint = g_strdup(args->mountpoint);
 124    exp->writable = blk_exp_args->writable;
 125    exp->growable = args->growable;
 126
 127    /* set default */
 128    if (!args->has_allow_other) {
 129        args->allow_other = FUSE_EXPORT_ALLOW_OTHER_AUTO;
 130    }
 131
 132    exp->st_mode = S_IFREG | S_IRUSR;
 133    if (exp->writable) {
 134        exp->st_mode |= S_IWUSR;
 135    }
 136    exp->st_uid = getuid();
 137    exp->st_gid = getgid();
 138
 139    if (args->allow_other == FUSE_EXPORT_ALLOW_OTHER_AUTO) {
 140        /* Ignore errors on our first attempt */
 141        ret = setup_fuse_export(exp, args->mountpoint, true, NULL);
 142        exp->allow_other = ret == 0;
 143        if (ret < 0) {
 144            ret = setup_fuse_export(exp, args->mountpoint, false, errp);
 145        }
 146    } else {
 147        exp->allow_other = args->allow_other == FUSE_EXPORT_ALLOW_OTHER_ON;
 148        ret = setup_fuse_export(exp, args->mountpoint, exp->allow_other, errp);
 149    }
 150    if (ret < 0) {
 151        goto fail;
 152    }
 153
 154    return 0;
 155
 156fail:
 157    fuse_export_delete(blk_exp);
 158    return ret;
 159}
 160
 161/**
 162 * Allocates the global @exports hash table.
 163 */
 164static void init_exports_table(void)
 165{
 166    if (exports) {
 167        return;
 168    }
 169
 170    exports = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
 171}
 172
 173/**
 174 * Create exp->fuse_session and mount it.
 175 */
 176static int setup_fuse_export(FuseExport *exp, const char *mountpoint,
 177                             bool allow_other, Error **errp)
 178{
 179    const char *fuse_argv[4];
 180    char *mount_opts;
 181    struct fuse_args fuse_args;
 182    int ret;
 183
 184    /*
 185     * max_read needs to match what fuse_init() sets.
 186     * max_write need not be supplied.
 187     */
 188    mount_opts = g_strdup_printf("max_read=%zu,default_permissions%s",
 189                                 FUSE_MAX_BOUNCE_BYTES,
 190                                 allow_other ? ",allow_other" : "");
 191
 192    fuse_argv[0] = ""; /* Dummy program name */
 193    fuse_argv[1] = "-o";
 194    fuse_argv[2] = mount_opts;
 195    fuse_argv[3] = NULL;
 196    fuse_args = (struct fuse_args)FUSE_ARGS_INIT(3, (char **)fuse_argv);
 197
 198    exp->fuse_session = fuse_session_new(&fuse_args, &fuse_ops,
 199                                         sizeof(fuse_ops), exp);
 200    g_free(mount_opts);
 201    if (!exp->fuse_session) {
 202        error_setg(errp, "Failed to set up FUSE session");
 203        ret = -EIO;
 204        goto fail;
 205    }
 206
 207    ret = fuse_session_mount(exp->fuse_session, mountpoint);
 208    if (ret < 0) {
 209        error_setg(errp, "Failed to mount FUSE session to export");
 210        ret = -EIO;
 211        goto fail;
 212    }
 213    exp->mounted = true;
 214
 215    g_hash_table_insert(exports, g_strdup(mountpoint), NULL);
 216
 217    aio_set_fd_handler(exp->common.ctx,
 218                       fuse_session_fd(exp->fuse_session), true,
 219                       read_from_fuse_export, NULL, NULL, exp);
 220    exp->fd_handler_set_up = true;
 221
 222    return 0;
 223
 224fail:
 225    fuse_export_shutdown(&exp->common);
 226    return ret;
 227}
 228
 229/**
 230 * Callback to be invoked when the FUSE session FD can be read from.
 231 * (This is basically the FUSE event loop.)
 232 */
 233static void read_from_fuse_export(void *opaque)
 234{
 235    FuseExport *exp = opaque;
 236    int ret;
 237
 238    blk_exp_ref(&exp->common);
 239
 240    do {
 241        ret = fuse_session_receive_buf(exp->fuse_session, &exp->fuse_buf);
 242    } while (ret == -EINTR);
 243    if (ret < 0) {
 244        goto out;
 245    }
 246
 247    fuse_session_process_buf(exp->fuse_session, &exp->fuse_buf);
 248
 249out:
 250    blk_exp_unref(&exp->common);
 251}
 252
 253static void fuse_export_shutdown(BlockExport *blk_exp)
 254{
 255    FuseExport *exp = container_of(blk_exp, FuseExport, common);
 256
 257    if (exp->fuse_session) {
 258        fuse_session_exit(exp->fuse_session);
 259
 260        if (exp->fd_handler_set_up) {
 261            aio_set_fd_handler(exp->common.ctx,
 262                               fuse_session_fd(exp->fuse_session), true,
 263                               NULL, NULL, NULL, NULL);
 264            exp->fd_handler_set_up = false;
 265        }
 266    }
 267
 268    if (exp->mountpoint) {
 269        /*
 270         * Safe to drop now, because we will not handle any requests
 271         * for this export anymore anyway.
 272         */
 273        g_hash_table_remove(exports, exp->mountpoint);
 274    }
 275}
 276
 277static void fuse_export_delete(BlockExport *blk_exp)
 278{
 279    FuseExport *exp = container_of(blk_exp, FuseExport, common);
 280
 281    if (exp->fuse_session) {
 282        if (exp->mounted) {
 283            fuse_session_unmount(exp->fuse_session);
 284        }
 285
 286        fuse_session_destroy(exp->fuse_session);
 287    }
 288
 289    free(exp->fuse_buf.mem);
 290    g_free(exp->mountpoint);
 291}
 292
 293/**
 294 * Check whether @path points to a regular file.  If not, put an
 295 * appropriate message into *errp.
 296 */
 297static bool is_regular_file(const char *path, Error **errp)
 298{
 299    struct stat statbuf;
 300    int ret;
 301
 302    ret = stat(path, &statbuf);
 303    if (ret < 0) {
 304        error_setg_errno(errp, errno, "Failed to stat '%s'", path);
 305        return false;
 306    }
 307
 308    if (!S_ISREG(statbuf.st_mode)) {
 309        error_setg(errp, "'%s' is not a regular file", path);
 310        return false;
 311    }
 312
 313    return true;
 314}
 315
 316/**
 317 * A chance to set change some parameters supplied to FUSE_INIT.
 318 */
 319static void fuse_init(void *userdata, struct fuse_conn_info *conn)
 320{
 321    /*
 322     * MIN_NON_ZERO() would not be wrong here, but what we set here
 323     * must equal what has been passed to fuse_session_new().
 324     * Therefore, as long as max_read must be passed as a mount option
 325     * (which libfuse claims will be changed at some point), we have
 326     * to set max_read to a fixed value here.
 327     */
 328    conn->max_read = FUSE_MAX_BOUNCE_BYTES;
 329
 330    conn->max_write = MIN_NON_ZERO(BDRV_REQUEST_MAX_BYTES, conn->max_write);
 331}
 332
 333/**
 334 * Let clients look up files.  Always return ENOENT because we only
 335 * care about the mountpoint itself.
 336 */
 337static void fuse_lookup(fuse_req_t req, fuse_ino_t parent, const char *name)
 338{
 339    fuse_reply_err(req, ENOENT);
 340}
 341
 342/**
 343 * Let clients get file attributes (i.e., stat() the file).
 344 */
 345static void fuse_getattr(fuse_req_t req, fuse_ino_t inode,
 346                         struct fuse_file_info *fi)
 347{
 348    struct stat statbuf;
 349    int64_t length, allocated_blocks;
 350    time_t now = time(NULL);
 351    FuseExport *exp = fuse_req_userdata(req);
 352
 353    length = blk_getlength(exp->common.blk);
 354    if (length < 0) {
 355        fuse_reply_err(req, -length);
 356        return;
 357    }
 358
 359    allocated_blocks = bdrv_get_allocated_file_size(blk_bs(exp->common.blk));
 360    if (allocated_blocks <= 0) {
 361        allocated_blocks = DIV_ROUND_UP(length, 512);
 362    } else {
 363        allocated_blocks = DIV_ROUND_UP(allocated_blocks, 512);
 364    }
 365
 366    statbuf = (struct stat) {
 367        .st_ino     = inode,
 368        .st_mode    = exp->st_mode,
 369        .st_nlink   = 1,
 370        .st_uid     = exp->st_uid,
 371        .st_gid     = exp->st_gid,
 372        .st_size    = length,
 373        .st_blksize = blk_bs(exp->common.blk)->bl.request_alignment,
 374        .st_blocks  = allocated_blocks,
 375        .st_atime   = now,
 376        .st_mtime   = now,
 377        .st_ctime   = now,
 378    };
 379
 380    fuse_reply_attr(req, &statbuf, 1.);
 381}
 382
 383static int fuse_do_truncate(const FuseExport *exp, int64_t size,
 384                            bool req_zero_write, PreallocMode prealloc)
 385{
 386    uint64_t blk_perm, blk_shared_perm;
 387    BdrvRequestFlags truncate_flags = 0;
 388    int ret;
 389
 390    if (req_zero_write) {
 391        truncate_flags |= BDRV_REQ_ZERO_WRITE;
 392    }
 393
 394    /* Growable exports have a permanent RESIZE permission */
 395    if (!exp->growable) {
 396        blk_get_perm(exp->common.blk, &blk_perm, &blk_shared_perm);
 397
 398        ret = blk_set_perm(exp->common.blk, blk_perm | BLK_PERM_RESIZE,
 399                           blk_shared_perm, NULL);
 400        if (ret < 0) {
 401            return ret;
 402        }
 403    }
 404
 405    ret = blk_truncate(exp->common.blk, size, true, prealloc,
 406                       truncate_flags, NULL);
 407
 408    if (!exp->growable) {
 409        /* Must succeed, because we are only giving up the RESIZE permission */
 410        blk_set_perm(exp->common.blk, blk_perm, blk_shared_perm, &error_abort);
 411    }
 412
 413    return ret;
 414}
 415
 416/**
 417 * Let clients set file attributes.  Only resizing and changing
 418 * permissions (st_mode, st_uid, st_gid) is allowed.
 419 * Changing permissions is only allowed as far as it will actually
 420 * permit access: Read-only exports cannot be given +w, and exports
 421 * without allow_other cannot be given a different UID or GID, and
 422 * they cannot be given non-owner access.
 423 */
 424static void fuse_setattr(fuse_req_t req, fuse_ino_t inode, struct stat *statbuf,
 425                         int to_set, struct fuse_file_info *fi)
 426{
 427    FuseExport *exp = fuse_req_userdata(req);
 428    int supported_attrs;
 429    int ret;
 430
 431    supported_attrs = FUSE_SET_ATTR_SIZE | FUSE_SET_ATTR_MODE;
 432    if (exp->allow_other) {
 433        supported_attrs |= FUSE_SET_ATTR_UID | FUSE_SET_ATTR_GID;
 434    }
 435
 436    if (to_set & ~supported_attrs) {
 437        fuse_reply_err(req, ENOTSUP);
 438        return;
 439    }
 440
 441    /* Do some argument checks first before committing to anything */
 442    if (to_set & FUSE_SET_ATTR_MODE) {
 443        /*
 444         * Without allow_other, non-owners can never access the export, so do
 445         * not allow setting permissions for them
 446         */
 447        if (!exp->allow_other &&
 448            (statbuf->st_mode & (S_IRWXG | S_IRWXO)) != 0)
 449        {
 450            fuse_reply_err(req, EPERM);
 451            return;
 452        }
 453
 454        /* +w for read-only exports makes no sense, disallow it */
 455        if (!exp->writable &&
 456            (statbuf->st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)) != 0)
 457        {
 458            fuse_reply_err(req, EROFS);
 459            return;
 460        }
 461    }
 462
 463    if (to_set & FUSE_SET_ATTR_SIZE) {
 464        if (!exp->writable) {
 465            fuse_reply_err(req, EACCES);
 466            return;
 467        }
 468
 469        ret = fuse_do_truncate(exp, statbuf->st_size, true, PREALLOC_MODE_OFF);
 470        if (ret < 0) {
 471            fuse_reply_err(req, -ret);
 472            return;
 473        }
 474    }
 475
 476    if (to_set & FUSE_SET_ATTR_MODE) {
 477        /* Ignore FUSE-supplied file type, only change the mode */
 478        exp->st_mode = (statbuf->st_mode & 07777) | S_IFREG;
 479    }
 480
 481    if (to_set & FUSE_SET_ATTR_UID) {
 482        exp->st_uid = statbuf->st_uid;
 483    }
 484
 485    if (to_set & FUSE_SET_ATTR_GID) {
 486        exp->st_gid = statbuf->st_gid;
 487    }
 488
 489    fuse_getattr(req, inode, fi);
 490}
 491
 492/**
 493 * Let clients open a file (i.e., the exported image).
 494 */
 495static void fuse_open(fuse_req_t req, fuse_ino_t inode,
 496                      struct fuse_file_info *fi)
 497{
 498    fuse_reply_open(req, fi);
 499}
 500
 501/**
 502 * Handle client reads from the exported image.
 503 */
 504static void fuse_read(fuse_req_t req, fuse_ino_t inode,
 505                      size_t size, off_t offset, struct fuse_file_info *fi)
 506{
 507    FuseExport *exp = fuse_req_userdata(req);
 508    int64_t length;
 509    void *buf;
 510    int ret;
 511
 512    /* Limited by max_read, should not happen */
 513    if (size > FUSE_MAX_BOUNCE_BYTES) {
 514        fuse_reply_err(req, EINVAL);
 515        return;
 516    }
 517
 518    /**
 519     * Clients will expect short reads at EOF, so we have to limit
 520     * offset+size to the image length.
 521     */
 522    length = blk_getlength(exp->common.blk);
 523    if (length < 0) {
 524        fuse_reply_err(req, -length);
 525        return;
 526    }
 527
 528    if (offset + size > length) {
 529        size = length - offset;
 530    }
 531
 532    buf = qemu_try_blockalign(blk_bs(exp->common.blk), size);
 533    if (!buf) {
 534        fuse_reply_err(req, ENOMEM);
 535        return;
 536    }
 537
 538    ret = blk_pread(exp->common.blk, offset, buf, size);
 539    if (ret >= 0) {
 540        fuse_reply_buf(req, buf, size);
 541    } else {
 542        fuse_reply_err(req, -ret);
 543    }
 544
 545    qemu_vfree(buf);
 546}
 547
 548/**
 549 * Handle client writes to the exported image.
 550 */
 551static void fuse_write(fuse_req_t req, fuse_ino_t inode, const char *buf,
 552                       size_t size, off_t offset, struct fuse_file_info *fi)
 553{
 554    FuseExport *exp = fuse_req_userdata(req);
 555    int64_t length;
 556    int ret;
 557
 558    /* Limited by max_write, should not happen */
 559    if (size > BDRV_REQUEST_MAX_BYTES) {
 560        fuse_reply_err(req, EINVAL);
 561        return;
 562    }
 563
 564    if (!exp->writable) {
 565        fuse_reply_err(req, EACCES);
 566        return;
 567    }
 568
 569    /**
 570     * Clients will expect short writes at EOF, so we have to limit
 571     * offset+size to the image length.
 572     */
 573    length = blk_getlength(exp->common.blk);
 574    if (length < 0) {
 575        fuse_reply_err(req, -length);
 576        return;
 577    }
 578
 579    if (offset + size > length) {
 580        if (exp->growable) {
 581            ret = fuse_do_truncate(exp, offset + size, true, PREALLOC_MODE_OFF);
 582            if (ret < 0) {
 583                fuse_reply_err(req, -ret);
 584                return;
 585            }
 586        } else {
 587            size = length - offset;
 588        }
 589    }
 590
 591    ret = blk_pwrite(exp->common.blk, offset, buf, size, 0);
 592    if (ret >= 0) {
 593        fuse_reply_write(req, size);
 594    } else {
 595        fuse_reply_err(req, -ret);
 596    }
 597}
 598
 599/**
 600 * Let clients perform various fallocate() operations.
 601 */
 602static void fuse_fallocate(fuse_req_t req, fuse_ino_t inode, int mode,
 603                           off_t offset, off_t length,
 604                           struct fuse_file_info *fi)
 605{
 606    FuseExport *exp = fuse_req_userdata(req);
 607    int64_t blk_len;
 608    int ret;
 609
 610    if (!exp->writable) {
 611        fuse_reply_err(req, EACCES);
 612        return;
 613    }
 614
 615    blk_len = blk_getlength(exp->common.blk);
 616    if (blk_len < 0) {
 617        fuse_reply_err(req, -blk_len);
 618        return;
 619    }
 620
 621    if (mode & FALLOC_FL_KEEP_SIZE) {
 622        length = MIN(length, blk_len - offset);
 623    }
 624
 625    if (mode & FALLOC_FL_PUNCH_HOLE) {
 626        if (!(mode & FALLOC_FL_KEEP_SIZE)) {
 627            fuse_reply_err(req, EINVAL);
 628            return;
 629        }
 630
 631        do {
 632            int size = MIN(length, BDRV_REQUEST_MAX_BYTES);
 633
 634            ret = blk_pdiscard(exp->common.blk, offset, size);
 635            offset += size;
 636            length -= size;
 637        } while (ret == 0 && length > 0);
 638    }
 639#ifdef CONFIG_FALLOCATE_ZERO_RANGE
 640    else if (mode & FALLOC_FL_ZERO_RANGE) {
 641        if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + length > blk_len) {
 642            /* No need for zeroes, we are going to write them ourselves */
 643            ret = fuse_do_truncate(exp, offset + length, false,
 644                                   PREALLOC_MODE_OFF);
 645            if (ret < 0) {
 646                fuse_reply_err(req, -ret);
 647                return;
 648            }
 649        }
 650
 651        do {
 652            int size = MIN(length, BDRV_REQUEST_MAX_BYTES);
 653
 654            ret = blk_pwrite_zeroes(exp->common.blk,
 655                                    offset, size, 0);
 656            offset += size;
 657            length -= size;
 658        } while (ret == 0 && length > 0);
 659    }
 660#endif /* CONFIG_FALLOCATE_ZERO_RANGE */
 661    else if (!mode) {
 662        /* We can only fallocate at the EOF with a truncate */
 663        if (offset < blk_len) {
 664            fuse_reply_err(req, EOPNOTSUPP);
 665            return;
 666        }
 667
 668        if (offset > blk_len) {
 669            /* No preallocation needed here */
 670            ret = fuse_do_truncate(exp, offset, true, PREALLOC_MODE_OFF);
 671            if (ret < 0) {
 672                fuse_reply_err(req, -ret);
 673                return;
 674            }
 675        }
 676
 677        ret = fuse_do_truncate(exp, offset + length, true,
 678                               PREALLOC_MODE_FALLOC);
 679    } else {
 680        ret = -EOPNOTSUPP;
 681    }
 682
 683    fuse_reply_err(req, ret < 0 ? -ret : 0);
 684}
 685
 686/**
 687 * Let clients fsync the exported image.
 688 */
 689static void fuse_fsync(fuse_req_t req, fuse_ino_t inode, int datasync,
 690                       struct fuse_file_info *fi)
 691{
 692    FuseExport *exp = fuse_req_userdata(req);
 693    int ret;
 694
 695    ret = blk_flush(exp->common.blk);
 696    fuse_reply_err(req, ret < 0 ? -ret : 0);
 697}
 698
 699/**
 700 * Called before an FD to the exported image is closed.  (libfuse
 701 * notes this to be a way to return last-minute errors.)
 702 */
 703static void fuse_flush(fuse_req_t req, fuse_ino_t inode,
 704                        struct fuse_file_info *fi)
 705{
 706    fuse_fsync(req, inode, 1, fi);
 707}
 708
 709#ifdef CONFIG_FUSE_LSEEK
 710/**
 711 * Let clients inquire allocation status.
 712 */
 713static void fuse_lseek(fuse_req_t req, fuse_ino_t inode, off_t offset,
 714                       int whence, struct fuse_file_info *fi)
 715{
 716    FuseExport *exp = fuse_req_userdata(req);
 717
 718    if (whence != SEEK_HOLE && whence != SEEK_DATA) {
 719        fuse_reply_err(req, EINVAL);
 720        return;
 721    }
 722
 723    while (true) {
 724        int64_t pnum;
 725        int ret;
 726
 727        ret = bdrv_block_status_above(blk_bs(exp->common.blk), NULL,
 728                                      offset, INT64_MAX, &pnum, NULL, NULL);
 729        if (ret < 0) {
 730            fuse_reply_err(req, -ret);
 731            return;
 732        }
 733
 734        if (!pnum && (ret & BDRV_BLOCK_EOF)) {
 735            int64_t blk_len;
 736
 737            /*
 738             * If blk_getlength() rounds (e.g. by sectors), then the
 739             * export length will be rounded, too.  However,
 740             * bdrv_block_status_above() may return EOF at unaligned
 741             * offsets.  We must not let this become visible and thus
 742             * always simulate a hole between @offset (the real EOF)
 743             * and @blk_len (the client-visible EOF).
 744             */
 745
 746            blk_len = blk_getlength(exp->common.blk);
 747            if (blk_len < 0) {
 748                fuse_reply_err(req, -blk_len);
 749                return;
 750            }
 751
 752            if (offset > blk_len || whence == SEEK_DATA) {
 753                fuse_reply_err(req, ENXIO);
 754            } else {
 755                fuse_reply_lseek(req, offset);
 756            }
 757            return;
 758        }
 759
 760        if (ret & BDRV_BLOCK_DATA) {
 761            if (whence == SEEK_DATA) {
 762                fuse_reply_lseek(req, offset);
 763                return;
 764            }
 765        } else {
 766            if (whence == SEEK_HOLE) {
 767                fuse_reply_lseek(req, offset);
 768                return;
 769            }
 770        }
 771
 772        /* Safety check against infinite loops */
 773        if (!pnum) {
 774            fuse_reply_err(req, ENXIO);
 775            return;
 776        }
 777
 778        offset += pnum;
 779    }
 780}
 781#endif
 782
 783static const struct fuse_lowlevel_ops fuse_ops = {
 784    .init       = fuse_init,
 785    .lookup     = fuse_lookup,
 786    .getattr    = fuse_getattr,
 787    .setattr    = fuse_setattr,
 788    .open       = fuse_open,
 789    .read       = fuse_read,
 790    .write      = fuse_write,
 791    .fallocate  = fuse_fallocate,
 792    .flush      = fuse_flush,
 793    .fsync      = fuse_fsync,
 794#ifdef CONFIG_FUSE_LSEEK
 795    .lseek      = fuse_lseek,
 796#endif
 797};
 798
 799const BlockExportDriver blk_exp_fuse = {
 800    .type               = BLOCK_EXPORT_TYPE_FUSE,
 801    .instance_size      = sizeof(FuseExport),
 802    .create             = fuse_export_create,
 803    .delete             = fuse_export_delete,
 804    .request_shutdown   = fuse_export_shutdown,
 805};
 806