cachepc-qemu

Fork of AMDESE/qemu with changes for cachepc side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-qemu
Log | Files | Refs | Submodules | LICENSE | sfeed.txt

fuse.c (22519B)


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