Signed-off-by: Changzhi Xie <s...@qq.com> This commit refactors the FUSE export to process read and write operations using coroutines, improving concurrency and avoiding blocking the main loop.
The main changes include: 1. Move read_from_fuse_export into coroutine 2. Move read/write processing into coroutine fuse_read_coroutine and fuse_write_coroutine 3. Remove fuse_buf from FuseExport, and have a separate struct fuse_buf for every request --- block/export/fuse.c | 188 +++++++++++++++++++++++++++++--------------- 1 file changed, 125 insertions(+), 63 deletions(-) diff --git a/block/export/fuse.c b/block/export/fuse.c index 465cc9891d..896cb55e3a 100644 --- a/block/export/fuse.c +++ b/block/export/fuse.c @@ -49,7 +49,6 @@ typedef struct FuseExport { BlockExport common; struct fuse_session *fuse_session; - struct fuse_buf fuse_buf; unsigned int in_flight; /* atomic */ bool mounted, fd_handler_set_up; @@ -64,6 +63,14 @@ typedef struct FuseExport { gid_t st_gid; } FuseExport; +typedef struct FuseIORequest { + fuse_req_t req; + size_t size; + off_t offset; + FuseExport *exp; + char *write_buf; +} FuseIORequest; + static GHashTable *exports; static const struct fuse_lowlevel_ops fuse_ops; @@ -281,13 +288,10 @@ fail: return ret; } -/** - * Callback to be invoked when the FUSE session FD can be read from. - * (This is basically the FUSE event loop.) - */ -static void read_from_fuse_export(void *opaque) +static void coroutine_fn read_from_fuse_export_coroutine(void *opaque) { FuseExport *exp = opaque; + struct fuse_buf buf = {}; int ret; blk_exp_ref(&exp->common); @@ -295,13 +299,13 @@ static void read_from_fuse_export(void *opaque) qatomic_inc(&exp->in_flight); do { - ret = fuse_session_receive_buf(exp->fuse_session, &exp->fuse_buf); + ret = fuse_session_receive_buf(exp->fuse_session, &buf); } while (ret == -EINTR); if (ret < 0) { goto out; } - fuse_session_process_buf(exp->fuse_session, &exp->fuse_buf); + fuse_session_process_buf(exp->fuse_session, &buf); out: if (qatomic_fetch_dec(&exp->in_flight) == 1) { @@ -309,6 +313,20 @@ out: } blk_exp_unref(&exp->common); + + free(buf.mem); + +} + +/** + * Callback to be invoked when the FUSE session FD can be read from. + * (This is basically the FUSE event loop.) + */ +static void read_from_fuse_export(void *opaque) +{ + Coroutine *co; + co = qemu_coroutine_create(read_from_fuse_export_coroutine, opaque); + qemu_coroutine_enter(co); } static void fuse_export_shutdown(BlockExport *blk_exp) @@ -347,7 +365,6 @@ static void fuse_export_delete(BlockExport *blk_exp) fuse_session_destroy(exp->fuse_session); } - free(exp->fuse_buf.mem); g_free(exp->mountpoint); } @@ -417,7 +434,7 @@ static void fuse_getattr(fuse_req_t req, fuse_ino_t inode, return; } - allocated_blocks = bdrv_get_allocated_file_size(blk_bs(exp->common.blk)); + allocated_blocks = bdrv_co_get_allocated_file_size(blk_bs(exp->common.blk)); if (allocated_blocks <= 0) { allocated_blocks = DIV_ROUND_UP(length, 512); } else { @@ -570,102 +587,147 @@ static void fuse_open(fuse_req_t req, fuse_ino_t inode, 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) +static void coroutine_fn fuse_read_coroutine(void *opaque) { - FuseExport *exp = fuse_req_userdata(req); + FuseIORequest *io_req = opaque; + FuseExport *exp = io_req->exp; int64_t length; - void *buf; + void *buffer; int ret; - /* Limited by max_read, should not happen */ - if (size > FUSE_MAX_BOUNCE_BYTES) { - fuse_reply_err(req, EINVAL); - return; + if (io_req->size > FUSE_MAX_BOUNCE_BYTES) { + fuse_reply_err(io_req->req, EINVAL); + goto cleanup; } - /** - * 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; + fuse_reply_err(io_req->req, -length); + goto cleanup; } - if (offset + size > length) { - size = length - offset; + if (io_req->offset + io_req->size > length) { + io_req->size = length - io_req->offset; } - buf = qemu_try_blockalign(blk_bs(exp->common.blk), size); - if (!buf) { - fuse_reply_err(req, ENOMEM); - return; + if (io_req->size == 0) { + fuse_reply_buf(io_req->req, NULL, 0); + goto cleanup; + } + + buffer = qemu_try_blockalign(blk_bs(exp->common.blk), io_req->size); + if (!buffer) { + fuse_reply_err(io_req->req, ENOMEM); + goto cleanup; } - ret = blk_pread(exp->common.blk, offset, size, buf, 0); + ret = blk_co_pread(exp->common.blk, io_req->offset, + io_req->size, buffer, 0); if (ret >= 0) { - fuse_reply_buf(req, buf, size); + fuse_reply_buf(io_req->req, buffer, io_req->size); } else { - fuse_reply_err(req, -ret); + fuse_reply_err(io_req->req, -ret); } - qemu_vfree(buf); + qemu_vfree(buffer); + +cleanup: + g_free(io_req); } -/** - * 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) +static void coroutine_fn fuse_write_coroutine(void *opaque) { - FuseExport *exp = fuse_req_userdata(req); + FuseIORequest *io_req = opaque; + FuseExport *exp = io_req->exp; 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 (io_req->size > BDRV_REQUEST_MAX_BYTES) { + fuse_reply_err(io_req->req, EINVAL); + goto cleanup; } if (!exp->writable) { - fuse_reply_err(req, EACCES); - return; + fuse_reply_err(io_req->req, EACCES); + goto cleanup; } - /** - * 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; + fuse_reply_err(io_req->req, -length); + goto cleanup; } - if (offset + size > length) { + if (io_req->offset + io_req->size > length) { if (exp->growable) { - ret = fuse_do_truncate(exp, offset + size, true, PREALLOC_MODE_OFF); + ret = fuse_do_truncate(exp, io_req->offset + io_req->size, + true, PREALLOC_MODE_OFF); if (ret < 0) { - fuse_reply_err(req, -ret); - return; + fuse_reply_err(io_req->req, -ret); + goto cleanup; } } else { - size = length - offset; + io_req->size = MAX(0, length - io_req->offset); + if (io_req->size == 0) { + fuse_reply_write(io_req->req, 0); + goto cleanup; + } } } - ret = blk_pwrite(exp->common.blk, offset, size, buf, 0); + ret = blk_co_pwrite(exp->common.blk, io_req->offset, io_req->size, + io_req->write_buf, 0); if (ret >= 0) { - fuse_reply_write(req, size); + fuse_reply_write(io_req->req, io_req->size); } else { - fuse_reply_err(req, -ret); + fuse_reply_err(io_req->req, -ret); } + +cleanup: + g_free(io_req->write_buf); + g_free(io_req); +} + +/** + * 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); + FuseIORequest *io_req = g_new(FuseIORequest, 1); + + *io_req = (FuseIORequest) { + .req = req, + .size = size, + .offset = offset, + .exp = exp, + }; + + Coroutine *co = qemu_coroutine_create(fuse_read_coroutine, io_req); + qemu_coroutine_enter(co); +} + + +/** + * 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); + FuseIORequest *io_req = g_new(FuseIORequest, 1); + + *io_req = (FuseIORequest) { + .req = req, + .size = size, + .offset = offset, + .exp = exp, + .write_buf = g_memdup2_qemu(buf, size), + }; + + Coroutine *co = qemu_coroutine_create(fuse_write_coroutine, io_req); + qemu_coroutine_enter(co); } /** -- 2.34.1