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 <mreitz@redhat.com>
Message-Id: <20201027190600.192171-7-mreitz@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
diff --git a/block/export/fuse.c b/block/export/fuse.c
index 0b9d226..38f74c9 100644
--- a/block/export/fuse.c
+++ b/block/export/fuse.c
@@ -627,6 +627,80 @@
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 @@
.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 b2f96c0..3f823ed 100755
--- a/configure
+++ b/configure
@@ -450,6 +450,7 @@
skip_meson=no
gettext=""
fuse="auto"
+fuse_lseek="auto"
bogus_os="no"
malloc_trim="auto"
@@ -1530,6 +1531,10 @@
;;
--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 @@
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 @@
-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 6e8ef15..f344b25 100644
--- a/meson.build
+++ b/meson.build
@@ -773,10 +773,28 @@
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_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 += {'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 8f9f2e3..74ac853 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -68,6 +68,8 @@
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'],