From: Kevin Wolf <kwolf@redhat.com>
To: qemu-block@nongnu.org
Cc: kwolf@redhat.com, peter.maydell@linaro.org, qemu-devel@nongnu.org
Subject: [PULL 07/34] fuse: Implement standard FUSE operations
Date: Fri, 11 Dec 2020 18:07:45 +0100 [thread overview]
Message-ID: <20201211170812.228643-8-kwolf@redhat.com> (raw)
In-Reply-To: <20201211170812.228643-1-kwolf@redhat.com>
From: Max Reitz <mreitz@redhat.com>
This makes the export actually useful instead of only producing errors
whenever it is accessed.
Signed-off-by: Max Reitz <mreitz@redhat.com>
Message-Id: <20201027190600.192171-4-mreitz@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
block/export/fuse.c | 242 ++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 242 insertions(+)
diff --git a/block/export/fuse.c b/block/export/fuse.c
index 0553bcd630..d995829ab7 100644
--- a/block/export/fuse.c
+++ b/block/export/fuse.c
@@ -282,8 +282,250 @@ static void fuse_init(void *userdata, struct fuse_conn_info *conn)
conn->max_write = MIN_NON_ZERO(BDRV_REQUEST_MAX_BYTES, conn->max_write);
}
+/**
+ * Let clients look up files. Always return ENOENT because we only
+ * care about the mountpoint itself.
+ */
+static void fuse_lookup(fuse_req_t req, fuse_ino_t parent, const char *name)
+{
+ fuse_reply_err(req, ENOENT);
+}
+
+/**
+ * Let clients get file attributes (i.e., stat() the file).
+ */
+static void fuse_getattr(fuse_req_t req, fuse_ino_t inode,
+ struct fuse_file_info *fi)
+{
+ struct stat statbuf;
+ int64_t length, allocated_blocks;
+ time_t now = time(NULL);
+ FuseExport *exp = fuse_req_userdata(req);
+ mode_t mode;
+
+ length = blk_getlength(exp->common.blk);
+ if (length < 0) {
+ fuse_reply_err(req, -length);
+ return;
+ }
+
+ allocated_blocks = bdrv_get_allocated_file_size(blk_bs(exp->common.blk));
+ if (allocated_blocks <= 0) {
+ allocated_blocks = DIV_ROUND_UP(length, 512);
+ } else {
+ allocated_blocks = DIV_ROUND_UP(allocated_blocks, 512);
+ }
+
+ mode = S_IFREG | S_IRUSR;
+ if (exp->writable) {
+ mode |= S_IWUSR;
+ }
+
+ statbuf = (struct stat) {
+ .st_ino = inode,
+ .st_mode = mode,
+ .st_nlink = 1,
+ .st_uid = getuid(),
+ .st_gid = getgid(),
+ .st_size = length,
+ .st_blksize = blk_bs(exp->common.blk)->bl.request_alignment,
+ .st_blocks = allocated_blocks,
+ .st_atime = now,
+ .st_mtime = now,
+ .st_ctime = now,
+ };
+
+ fuse_reply_attr(req, &statbuf, 1.);
+}
+
+static int fuse_do_truncate(const FuseExport *exp, int64_t size,
+ bool req_zero_write, PreallocMode prealloc)
+{
+ uint64_t blk_perm, blk_shared_perm;
+ BdrvRequestFlags truncate_flags = 0;
+ int ret;
+
+ if (req_zero_write) {
+ truncate_flags |= BDRV_REQ_ZERO_WRITE;
+ }
+
+ blk_get_perm(exp->common.blk, &blk_perm, &blk_shared_perm);
+
+ ret = blk_set_perm(exp->common.blk, blk_perm | BLK_PERM_RESIZE,
+ blk_shared_perm, NULL);
+ if (ret < 0) {
+ return ret;
+ }
+
+ ret = blk_truncate(exp->common.blk, size, true, prealloc,
+ truncate_flags, NULL);
+
+ /* Must succeed, because we are only giving up the RESIZE permission */
+ blk_set_perm(exp->common.blk, blk_perm, blk_shared_perm, &error_abort);
+
+ return ret;
+}
+
+/**
+ * Let clients set file attributes. Only resizing is supported.
+ */
+static void fuse_setattr(fuse_req_t req, fuse_ino_t inode, struct stat *statbuf,
+ int to_set, struct fuse_file_info *fi)
+{
+ FuseExport *exp = fuse_req_userdata(req);
+ int ret;
+
+ if (!exp->writable) {
+ fuse_reply_err(req, EACCES);
+ return;
+ }
+
+ if (to_set & ~FUSE_SET_ATTR_SIZE) {
+ fuse_reply_err(req, ENOTSUP);
+ return;
+ }
+
+ ret = fuse_do_truncate(exp, statbuf->st_size, true, PREALLOC_MODE_OFF);
+ if (ret < 0) {
+ fuse_reply_err(req, -ret);
+ return;
+ }
+
+ fuse_getattr(req, inode, fi);
+}
+
+/**
+ * Let clients open a file (i.e., the exported image).
+ */
+static void fuse_open(fuse_req_t req, fuse_ino_t inode,
+ struct fuse_file_info *fi)
+{
+ fuse_reply_open(req, fi);
+}
+
+/**
+ * Handle client reads from the exported image.
+ */
+static void fuse_read(fuse_req_t req, fuse_ino_t inode,
+ size_t size, off_t offset, struct fuse_file_info *fi)
+{
+ FuseExport *exp = fuse_req_userdata(req);
+ int64_t length;
+ void *buf;
+ int ret;
+
+ /* Limited by max_read, should not happen */
+ if (size > FUSE_MAX_BOUNCE_BYTES) {
+ fuse_reply_err(req, EINVAL);
+ return;
+ }
+
+ /**
+ * Clients will expect short reads at EOF, so we have to limit
+ * offset+size to the image length.
+ */
+ length = blk_getlength(exp->common.blk);
+ if (length < 0) {
+ fuse_reply_err(req, -length);
+ return;
+ }
+
+ if (offset + size > length) {
+ size = length - offset;
+ }
+
+ buf = qemu_try_blockalign(blk_bs(exp->common.blk), size);
+ if (!buf) {
+ fuse_reply_err(req, ENOMEM);
+ return;
+ }
+
+ ret = blk_pread(exp->common.blk, offset, buf, size);
+ if (ret >= 0) {
+ fuse_reply_buf(req, buf, size);
+ } else {
+ fuse_reply_err(req, -ret);
+ }
+
+ qemu_vfree(buf);
+}
+
+/**
+ * Handle client writes to the exported image.
+ */
+static void fuse_write(fuse_req_t req, fuse_ino_t inode, const char *buf,
+ size_t size, off_t offset, struct fuse_file_info *fi)
+{
+ FuseExport *exp = fuse_req_userdata(req);
+ int64_t length;
+ int ret;
+
+ /* Limited by max_write, should not happen */
+ if (size > BDRV_REQUEST_MAX_BYTES) {
+ fuse_reply_err(req, EINVAL);
+ return;
+ }
+
+ if (!exp->writable) {
+ fuse_reply_err(req, EACCES);
+ return;
+ }
+
+ /**
+ * Clients will expect short writes at EOF, so we have to limit
+ * offset+size to the image length.
+ */
+ length = blk_getlength(exp->common.blk);
+ if (length < 0) {
+ fuse_reply_err(req, -length);
+ return;
+ }
+
+ if (offset + size > length) {
+ size = length - offset;
+ }
+
+ ret = blk_pwrite(exp->common.blk, offset, buf, size, 0);
+ if (ret >= 0) {
+ fuse_reply_write(req, size);
+ } else {
+ fuse_reply_err(req, -ret);
+ }
+}
+
+/**
+ * Let clients fsync the exported image.
+ */
+static void fuse_fsync(fuse_req_t req, fuse_ino_t inode, int datasync,
+ struct fuse_file_info *fi)
+{
+ FuseExport *exp = fuse_req_userdata(req);
+ int ret;
+
+ ret = blk_flush(exp->common.blk);
+ fuse_reply_err(req, ret < 0 ? -ret : 0);
+}
+
+/**
+ * Called before an FD to the exported image is closed. (libfuse
+ * notes this to be a way to return last-minute errors.)
+ */
+static void fuse_flush(fuse_req_t req, fuse_ino_t inode,
+ struct fuse_file_info *fi)
+{
+ fuse_fsync(req, inode, 1, fi);
+}
+
static const struct fuse_lowlevel_ops fuse_ops = {
.init = fuse_init,
+ .lookup = fuse_lookup,
+ .getattr = fuse_getattr,
+ .setattr = fuse_setattr,
+ .open = fuse_open,
+ .read = fuse_read,
+ .write = fuse_write,
+ .flush = fuse_flush,
+ .fsync = fuse_fsync,
};
const BlockExportDriver blk_exp_fuse = {
--
2.29.2
next prev parent reply other threads:[~2020-12-11 17:22 UTC|newest]
Thread overview: 36+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-12-11 17:07 [PULL 00/34] Block layer patches Kevin Wolf
2020-12-11 17:07 ` [PULL 01/34] block/accounting: Use lock guard macros Kevin Wolf
2020-12-11 17:07 ` [PULL 02/34] block/curl: " Kevin Wolf
2020-12-11 17:07 ` [PULL 03/34] block/throttle-groups: " Kevin Wolf
2020-12-11 17:07 ` [PULL 04/34] block/iscsi: " Kevin Wolf
2020-12-11 17:07 ` [PULL 05/34] meson: Detect libfuse Kevin Wolf
2020-12-11 17:07 ` [PULL 06/34] fuse: Allow exporting BDSs via FUSE Kevin Wolf
2020-12-11 17:07 ` Kevin Wolf [this message]
2020-12-11 17:07 ` [PULL 08/34] fuse: Allow growable exports Kevin Wolf
2020-12-11 17:07 ` [PULL 09/34] fuse: (Partially) implement fallocate() Kevin Wolf
2020-12-11 17:07 ` [PULL 10/34] fuse: Implement hole detection through lseek Kevin Wolf
2020-12-11 17:07 ` [PULL 11/34] iotests: Do not needlessly filter _make_test_img Kevin Wolf
2020-12-11 17:07 ` [PULL 12/34] iotests: Do not pipe _make_test_img Kevin Wolf
2020-12-11 17:07 ` [PULL 13/34] iotests: Use convert -n in some cases Kevin Wolf
2020-12-11 17:07 ` [PULL 14/34] iotests/046: Avoid renaming images Kevin Wolf
2020-12-11 17:07 ` [PULL 15/34] iotests: Derive image names from $TEST_IMG Kevin Wolf
2020-12-11 17:07 ` [PULL 16/34] iotests/091: Use _cleanup_qemu instad of "wait" Kevin Wolf
2020-12-11 17:07 ` [PULL 17/34] iotests: Restrict some Python tests to file Kevin Wolf
2020-12-11 17:07 ` [PULL 18/34] iotests: Let _make_test_img guess $TEST_IMG_FILE Kevin Wolf
2020-12-11 17:07 ` [PULL 19/34] iotests/287: Clean up subshell test image Kevin Wolf
2020-12-11 17:07 ` [PULL 20/34] storage-daemon: Call bdrv_close_all() on exit Kevin Wolf
2020-12-11 17:07 ` [PULL 21/34] iotests: Give access to the qemu-storage-daemon Kevin Wolf
2020-12-11 17:08 ` [PULL 22/34] iotests: Allow testing FUSE exports Kevin Wolf
2020-12-11 17:08 ` [PULL 23/34] iotests: Enable fuse for many tests Kevin Wolf
2020-12-11 17:08 ` [PULL 24/34] iotests/308: Add test for FUSE exports Kevin Wolf
2020-12-11 17:08 ` [PULL 25/34] file-posix: check the use_lock before setting the file lock Kevin Wolf
2020-12-11 17:08 ` [PULL 26/34] iotests/221: Discard image before qemu-img map Kevin Wolf
2020-12-11 17:08 ` [PULL 27/34] can-host: Fix crash when 'canbus' property is not set Kevin Wolf
2020-12-11 17:08 ` [PULL 28/34] block/file-posix: fix workaround in raw_do_pwrite_zeroes() Kevin Wolf
2020-12-11 17:08 ` [PULL 29/34] block/io: bdrv_refresh_limits(): use ERRP_GUARD Kevin Wolf
2020-12-11 17:08 ` [PULL 30/34] block/io: bdrv_check_byte_request(): drop bdrv_is_inserted() Kevin Wolf
2020-12-11 17:08 ` [PULL 31/34] block: introduce BDRV_MAX_LENGTH Kevin Wolf
2020-12-11 17:08 ` [PULL 32/34] block: Simplify qmp_block_resize() error paths Kevin Wolf
2020-12-11 17:08 ` [PULL 33/34] block: Fix locking in qmp_block_resize() Kevin Wolf
2020-12-11 17:08 ` [PULL 34/34] block: Fix deadlock in bdrv_co_yield_to_drain() Kevin Wolf
2020-12-12 16:06 ` [PULL 00/34] Block layer patches Peter Maydell
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20201211170812.228643-8-kwolf@redhat.com \
--to=kwolf@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).