diff options
author | Max Reitz <mreitz@redhat.com> | 2020-10-27 20:05:46 +0100 |
---|---|---|
committer | Kevin Wolf <kwolf@redhat.com> | 2020-12-11 17:52:40 +0100 |
commit | df4ea7091b744c8568e8bd9212a756ac504c43d4 (patch) | |
tree | 4bb9ba1819d02bb97343e03def3f6ca06ac54251 /block/export | |
parent | 4ca37a96a75aafe7a37ba51ab1912b09b7190a6b (diff) |
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>
Diffstat (limited to 'block/export')
-rw-r--r-- | block/export/fuse.c | 77 |
1 files changed, 77 insertions, 0 deletions
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 = { |