From: Max Reitz <mreitz@redhat.com>
To: qemu-block@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>,
qemu-devel@nongnu.org, Max Reitz <mreitz@redhat.com>
Subject: [PATCH 03/18] fuse: Implement standard FUSE operations
Date: Thu, 19 Dec 2019 15:38:03 +0100 [thread overview]
Message-ID: <20191219143818.1646168-4-mreitz@redhat.com> (raw)
In-Reply-To: <20191219143818.1646168-1-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>
---
block/fuse.c | 222 +++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 222 insertions(+)
diff --git a/block/fuse.c b/block/fuse.c
index 3a22579dca..f18e8e7591 100644
--- a/block/fuse.c
+++ b/block/fuse.c
@@ -31,6 +31,10 @@
#include <fuse_lowlevel.h>
+/* Prevent overly long bounce buffer allocations */
+#define FUSE_MAX_BOUNCE_BYTES (MIN(BDRV_REQUEST_MAX_BYTES, 64 * 1024 * 1024))
+
+
typedef struct BdrvFuseSession {
struct fuse_session *fuse_session;
struct fuse_buf fuse_buf;
@@ -256,5 +260,223 @@ static bool is_regular_file(const char *path, Error **errp)
return true;
}
+
+/**
+ * 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);
+ ImageInfo *info;
+ BdrvFuseSession *session = fuse_req_userdata(req);
+ mode_t mode;
+ Error *local_error = NULL;
+
+ length = blk_getlength(session->blk);
+ if (length < 0) {
+ fuse_reply_err(req, -length);
+ return;
+ }
+
+ bdrv_query_image_info(blk_bs(session->blk), &info, &local_error);
+ if (local_error) {
+ error_free(local_error);
+ allocated_blocks = DIV_ROUND_UP(length, 512);
+ } else {
+ allocated_blocks = DIV_ROUND_UP(info->actual_size, 512);
+ qapi_free_ImageInfo(info);
+ }
+
+ mode = S_IFREG | 0400;
+ if (session->writable) {
+ mode |= 0200;
+ }
+
+ 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(session->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 BdrvFuseSession *session, int64_t size,
+ PreallocMode prealloc)
+{
+ int ret;
+
+ ret = blk_set_perm(session->blk, session->perm | BLK_PERM_RESIZE,
+ session->shared_perm, NULL);
+ if (ret < 0) {
+ return ret;
+ }
+
+ ret = blk_truncate(session->blk, size, true, prealloc, NULL);
+
+ /* Must succeed, because we are only giving up the RESIZE permission */
+ blk_set_perm(session->blk, session->perm, session->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)
+{
+ BdrvFuseSession *session = fuse_req_userdata(req);
+ int ret;
+
+ if (!session->writable) {
+ fuse_reply_err(req, EACCES);
+ return;
+ }
+
+ if (to_set & ~FUSE_SET_ATTR_SIZE) {
+ fuse_reply_err(req, ENOTSUP);
+ return;
+ }
+
+ ret = fuse_do_truncate(session, statbuf->st_size, 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)
+{
+ BdrvFuseSession *session = fuse_req_userdata(req);
+ int64_t length;
+ void *buf;
+ int ret;
+
+ /**
+ * Clients will expect short reads at EOF, so we have to limit
+ * offset+size to the image length.
+ */
+ length = blk_getlength(session->blk);
+ if (length < 0) {
+ fuse_reply_err(req, -length);
+ return;
+ }
+
+ size = MIN(size, FUSE_MAX_BOUNCE_BYTES);
+ if (offset + size > length) {
+ size = length - offset;
+ }
+
+ buf = qemu_try_blockalign(blk_bs(session->blk), size);
+ if (!buf) {
+ fuse_reply_err(req, ENOMEM);
+ return;
+ }
+
+ ret = blk_pread(session->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)
+{
+ BdrvFuseSession *session = fuse_req_userdata(req);
+ int64_t length;
+ int ret;
+
+ if (!session->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(session->blk);
+ if (length < 0) {
+ fuse_reply_err(req, -length);
+ return;
+ }
+
+ size = MIN(size, BDRV_REQUEST_MAX_BYTES);
+ if (offset + size > length) {
+ size = length - offset;
+ }
+
+ ret = blk_pwrite(session->blk, offset, buf, size, 0);
+ if (ret >= 0) {
+ fuse_reply_write(req, size);
+ } else {
+ fuse_reply_err(req, -ret);
+ }
+}
+
+/**
+ * Let clients flush the exported image.
+ */
+static void fuse_flush(fuse_req_t req, fuse_ino_t inode,
+ struct fuse_file_info *fi)
+{
+ BdrvFuseSession *session = fuse_req_userdata(req);
+ int ret;
+
+ ret = blk_flush(session->blk);
+ fuse_reply_err(req, ret < 0 ? -ret : 0);
+}
+
static const struct fuse_lowlevel_ops fuse_ops = {
+ .lookup = fuse_lookup,
+ .getattr = fuse_getattr,
+ .setattr = fuse_setattr,
+ .open = fuse_open,
+ .read = fuse_read,
+ .write = fuse_write,
+ .flush = fuse_flush,
};
--
2.23.0
next prev parent reply other threads:[~2019-12-19 14:45 UTC|newest]
Thread overview: 37+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-12-19 14:38 [PATCH 00/18] block: Allow exporting BDSs via FUSE Max Reitz
2019-12-19 14:38 ` [PATCH 01/18] configure: Detect libfuse Max Reitz
2019-12-19 14:38 ` [PATCH 02/18] fuse: Allow exporting BDSs via FUSE Max Reitz
2019-12-20 10:26 ` Kevin Wolf
2019-12-20 10:48 ` Max Reitz
2019-12-20 11:24 ` Kevin Wolf
2019-12-20 12:09 ` Max Reitz
2019-12-20 12:48 ` Markus Armbruster
2019-12-20 12:58 ` Kevin Wolf
2019-12-20 13:25 ` Markus Armbruster
2019-12-20 21:18 ` Eric Blake
2019-12-20 12:49 ` Markus Armbruster
2019-12-20 13:02 ` Kevin Wolf
2019-12-20 21:15 ` Eric Blake
2020-01-06 12:00 ` Max Reitz
2019-12-19 14:38 ` Max Reitz [this message]
2019-12-19 14:38 ` [PATCH 04/18] fuse: Add fuse-export-remove Max Reitz
2019-12-19 14:38 ` [PATCH 05/18] fuse: Allow growable exports Max Reitz
2019-12-19 14:38 ` [PATCH 06/18] fuse: (Partially) implement fallocate() Max Reitz
2019-12-19 14:38 ` [PATCH 07/18] fuse: Implement hole detection through lseek Max Reitz
2019-12-19 14:38 ` [PATCH 08/18] iotests: Do not needlessly filter _make_test_img Max Reitz
2019-12-19 14:38 ` [PATCH 09/18] iotests: Do not pipe _make_test_img Max Reitz
2019-12-19 14:38 ` [PATCH 10/18] iotests: Use convert -n in some cases Max Reitz
2019-12-19 14:38 ` [PATCH 11/18] iotests: Avoid renaming images Max Reitz
2019-12-19 14:38 ` [PATCH 12/18] iotests: Derive image names from $TEST_IMG Max Reitz
2019-12-19 14:38 ` [PATCH 13/18] iotests/091: Use _cleanup_qemu instad of "wait" Max Reitz
2019-12-19 14:38 ` [PATCH 14/18] iotests: Restrict some Python tests to file Max Reitz
2019-12-19 14:38 ` [PATCH 15/18] iotests: Let _make_test_img guess $TEST_IMG_FILE Max Reitz
2019-12-19 14:38 ` [PATCH 16/18] iotests: Allow testing FUSE exports Max Reitz
2019-12-19 14:38 ` [PATCH 17/18] iotests: Enable fuse for many tests Max Reitz
2019-12-19 14:38 ` [PATCH 18/18] iotests/281: Add test for FUSE exports Max Reitz
2019-12-19 19:05 ` [PATCH 00/18] block: Allow exporting BDSs via FUSE Max Reitz
2019-12-20 10:08 ` Stefan Hajnoczi
2019-12-20 10:30 ` Max Reitz
2019-12-20 12:50 ` Kevin Wolf
2019-12-20 21:20 ` Eric Blake
2020-01-02 11:22 ` Stefan Hajnoczi
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=20191219143818.1646168-4-mreitz@redhat.com \
--to=mreitz@redhat.com \
--cc=kwolf@redhat.com \
--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).