From df4ea7091b744c8568e8bd9212a756ac504c43d4 Mon Sep 17 00:00:00 2001 From: Max Reitz Date: Tue, 27 Oct 2020 20:05:46 +0100 Subject: [PATCH] fuse: Implement hole detection through lseek This is a relatively new feature in libfuse (available since 3.8.0, which was released in November 2019), so we have to add a dedicated check whether it is available before making use of it. Signed-off-by: Max Reitz Message-Id: <20201027190600.192171-7-mreitz@redhat.com> Signed-off-by: Kevin Wolf --- block/export/fuse.c | 77 +++++++++++++++++++++++++++++++++++++++++++++ configure | 8 ++++- meson.build | 20 ++++++++++++ meson_options.txt | 2 ++ 4 files changed, 106 insertions(+), 1 deletion(-) diff --git a/block/export/fuse.c b/block/export/fuse.c index 0b9d226b2f..38f74c94da 100644 --- a/block/export/fuse.c +++ b/block/export/fuse.c @@ -627,6 +627,80 @@ static void fuse_flush(fuse_req_t req, fuse_ino_t inode, fuse_fsync(req, inode, 1, fi); } +#ifdef CONFIG_FUSE_LSEEK +/** + * Let clients inquire allocation status. + */ +static void fuse_lseek(fuse_req_t req, fuse_ino_t inode, off_t offset, + int whence, struct fuse_file_info *fi) +{ + FuseExport *exp = fuse_req_userdata(req); + + if (whence != SEEK_HOLE && whence != SEEK_DATA) { + fuse_reply_err(req, EINVAL); + return; + } + + while (true) { + int64_t pnum; + int ret; + + ret = bdrv_block_status_above(blk_bs(exp->common.blk), NULL, + offset, INT64_MAX, &pnum, NULL, NULL); + if (ret < 0) { + fuse_reply_err(req, -ret); + return; + } + + if (!pnum && (ret & BDRV_BLOCK_EOF)) { + int64_t blk_len; + + /* + * If blk_getlength() rounds (e.g. by sectors), then the + * export length will be rounded, too. However, + * bdrv_block_status_above() may return EOF at unaligned + * offsets. We must not let this become visible and thus + * always simulate a hole between @offset (the real EOF) + * and @blk_len (the client-visible EOF). + */ + + blk_len = blk_getlength(exp->common.blk); + if (blk_len < 0) { + fuse_reply_err(req, -blk_len); + return; + } + + if (offset > blk_len || whence == SEEK_DATA) { + fuse_reply_err(req, ENXIO); + } else { + fuse_reply_lseek(req, offset); + } + return; + } + + if (ret & BDRV_BLOCK_DATA) { + if (whence == SEEK_DATA) { + fuse_reply_lseek(req, offset); + return; + } + } else { + if (whence == SEEK_HOLE) { + fuse_reply_lseek(req, offset); + return; + } + } + + /* Safety check against infinite loops */ + if (!pnum) { + fuse_reply_err(req, ENXIO); + return; + } + + offset += pnum; + } +} +#endif + static const struct fuse_lowlevel_ops fuse_ops = { .init = fuse_init, .lookup = fuse_lookup, @@ -638,6 +712,9 @@ static const struct fuse_lowlevel_ops fuse_ops = { .fallocate = fuse_fallocate, .flush = fuse_flush, .fsync = fuse_fsync, +#ifdef CONFIG_FUSE_LSEEK + .lseek = fuse_lseek, +#endif }; const BlockExportDriver blk_exp_fuse = { diff --git a/configure b/configure index b2f96c0da2..3f823ed163 100755 --- a/configure +++ b/configure @@ -450,6 +450,7 @@ ninja="" skip_meson=no gettext="" fuse="auto" +fuse_lseek="auto" bogus_os="no" malloc_trim="auto" @@ -1530,6 +1531,10 @@ for opt do ;; --disable-fuse) fuse="disabled" ;; + --enable-fuse-lseek) fuse_lseek="enabled" + ;; + --disable-fuse-lseek) fuse_lseek="disabled" + ;; *) echo "ERROR: unknown option $opt" echo "Try '$0 --help' for more information" @@ -1856,6 +1861,7 @@ disabled with --disable-FEATURE, default is enabled if available: rng-none dummy RNG, avoid using /dev/(u)random and getrandom() libdaxctl libdaxctl support fuse FUSE block device export + fuse-lseek SEEK_HOLE/SEEK_DATA support for FUSE exports NOTE: The object files are built at the place where configure is launched EOF @@ -7020,7 +7026,7 @@ NINJA=$ninja $meson setup \ -Diconv=$iconv -Dcurses=$curses -Dlibudev=$libudev\ -Ddocs=$docs -Dsphinx_build=$sphinx_build -Dinstall_blobs=$blobs \ -Dvhost_user_blk_server=$vhost_user_blk_server \ - -Dfuse=$fuse \ + -Dfuse=$fuse -Dfuse_lseek=$fuse_lseek \ $cross_arg \ "$PWD" "$source_path" diff --git a/meson.build b/meson.build index 6e8ef151d8..f344b25955 100644 --- a/meson.build +++ b/meson.build @@ -773,10 +773,28 @@ elif get_option('vhost_user_blk_server').disabled() or not have_system have_vhost_user_blk_server = false endif +if get_option('fuse').disabled() and get_option('fuse_lseek').enabled() + error('Cannot enable fuse-lseek while fuse is disabled') +endif + fuse = dependency('fuse3', required: get_option('fuse'), version: '>=3.1', method: 'pkg-config', static: enable_static) +fuse_lseek = not_found +if not get_option('fuse_lseek').disabled() + if fuse.version().version_compare('>=3.8') + # Dummy dependency + fuse_lseek = declare_dependency() + elif get_option('fuse_lseek').enabled() + if fuse.found() + error('fuse-lseek requires libfuse >=3.8, found ' + fuse.version()) + else + error('fuse-lseek requires libfuse, which was not found') + endif + endif +endif + ################# # config-host.h # ################# @@ -812,6 +830,7 @@ config_host_data.set('CONFIG_GETTID', has_gettid) config_host_data.set('CONFIG_MALLOC_TRIM', has_malloc_trim) config_host_data.set('CONFIG_STATX', has_statx) config_host_data.set('CONFIG_FUSE', fuse.found()) +config_host_data.set('CONFIG_FUSE_LSEEK', fuse_lseek.found()) config_host_data.set('QEMU_VERSION', '"@0@"'.format(meson.project_version())) config_host_data.set('QEMU_VERSION_MAJOR', meson.project_version().split('.')[0]) config_host_data.set('QEMU_VERSION_MINOR', meson.project_version().split('.')[1]) @@ -2214,6 +2233,7 @@ summary_info += {'thread sanitizer': config_host.has_key('CONFIG_TSAN')} summary_info += {'rng-none': config_host.has_key('CONFIG_RNG_NONE')} summary_info += {'Linux keyring': config_host.has_key('CONFIG_SECRET_KEYRING')} summary_info += {'FUSE exports': fuse.found()} +summary_info += {'FUSE lseek': fuse_lseek.found()} summary(summary_info, bool_yn: true) if not supported_cpus.contains(cpu) diff --git a/meson_options.txt b/meson_options.txt index 8f9f2e3df6..74ac853548 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -68,6 +68,8 @@ option('vhost_user_blk_server', type: 'feature', value: 'auto', description: 'build vhost-user-blk server') option('fuse', type: 'feature', value: 'auto', description: 'FUSE block device export') +option('fuse_lseek', type : 'feature', value : 'auto', + description: 'SEEK_HOLE/SEEK_DATA support for FUSE exports') option('capstone', type: 'combo', value: 'auto', choices: ['disabled', 'enabled', 'auto', 'system', 'internal'],