[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PULL 06/17] nbd/server: refactor nbd_trip: split out nbd_h
From: |
Eric Blake |
Subject: |
[Qemu-devel] [PULL 06/17] nbd/server: refactor nbd_trip: split out nbd_handle_request |
Date: |
Tue, 13 Mar 2018 12:13:34 -0500 |
From: Vladimir Sementsov-Ogievskiy <address@hidden>
Split out request handling logic.
Signed-off-by: Vladimir Sementsov-Ogievskiy <address@hidden>
Message-Id: <address@hidden>
Reviewed-by: Eric Blake <address@hidden>
[eblake: touch up blank line placement]
Signed-off-by: Eric Blake <address@hidden>
---
nbd/server.c | 128 ++++++++++++++++++++++++++++++-----------------------------
1 file changed, 66 insertions(+), 62 deletions(-)
diff --git a/nbd/server.c b/nbd/server.c
index 297d314bd0b..83f21c55912 100644
--- a/nbd/server.c
+++ b/nbd/server.c
@@ -1587,17 +1587,78 @@ static coroutine_fn int nbd_do_cmd_read(NBDClient
*client, NBDRequest *request,
}
}
+/* Handle NBD request.
+ * Return -errno if sending fails. Other errors are reported directly to the
+ * client as an error reply. */
+static coroutine_fn int nbd_handle_request(NBDClient *client,
+ NBDRequest *request,
+ uint8_t *data, Error **errp)
+{
+ int ret;
+ int flags;
+ NBDExport *exp = client->exp;
+ char *msg;
+
+ switch (request->type) {
+ case NBD_CMD_READ:
+ return nbd_do_cmd_read(client, request, data, errp);
+
+ case NBD_CMD_WRITE:
+ flags = 0;
+ if (request->flags & NBD_CMD_FLAG_FUA) {
+ flags |= BDRV_REQ_FUA;
+ }
+ ret = blk_pwrite(exp->blk, request->from + exp->dev_offset,
+ data, request->len, flags);
+ return nbd_send_generic_reply(client, request->handle, ret,
+ "writing to file failed", errp);
+
+ case NBD_CMD_WRITE_ZEROES:
+ flags = 0;
+ if (request->flags & NBD_CMD_FLAG_FUA) {
+ flags |= BDRV_REQ_FUA;
+ }
+ if (!(request->flags & NBD_CMD_FLAG_NO_HOLE)) {
+ flags |= BDRV_REQ_MAY_UNMAP;
+ }
+ ret = blk_pwrite_zeroes(exp->blk, request->from + exp->dev_offset,
+ request->len, flags);
+ return nbd_send_generic_reply(client, request->handle, ret,
+ "writing to file failed", errp);
+
+ case NBD_CMD_DISC:
+ /* unreachable, thanks to special case in nbd_co_receive_request() */
+ abort();
+
+ case NBD_CMD_FLUSH:
+ ret = blk_co_flush(exp->blk);
+ return nbd_send_generic_reply(client, request->handle, ret,
+ "flush failed", errp);
+
+ case NBD_CMD_TRIM:
+ ret = blk_co_pdiscard(exp->blk, request->from + exp->dev_offset,
+ request->len);
+ return nbd_send_generic_reply(client, request->handle, ret,
+ "discard failed", errp);
+
+ default:
+ msg = g_strdup_printf("invalid request type (%" PRIu32 ") received",
+ request->type);
+ ret = nbd_send_generic_reply(client, request->handle, -EINVAL, msg,
+ errp);
+ g_free(msg);
+ return ret;
+ }
+}
+
/* Owns a reference to the NBDClient passed as opaque. */
static coroutine_fn void nbd_trip(void *opaque)
{
NBDClient *client = opaque;
- NBDExport *exp = client->exp;
NBDRequestData *req;
NBDRequest request = { 0 }; /* GCC thinks it can be used uninitialized
*/
int ret;
- int flags;
Error *local_err = NULL;
- char *msg = NULL;
trace_nbd_trip();
if (client->closing) {
@@ -1631,66 +1692,9 @@ static coroutine_fn void nbd_trip(void *opaque)
ret = nbd_send_generic_reply(client, request.handle, -EINVAL,
error_get_pretty(export_err), &local_err);
error_free(export_err);
-
- goto replied;
+ } else {
+ ret = nbd_handle_request(client, &request, req->data, &local_err);
}
-
- switch (request.type) {
- case NBD_CMD_READ:
- ret = nbd_do_cmd_read(client, &request, req->data, &local_err);
- break;
-
- case NBD_CMD_WRITE:
- flags = 0;
- if (request.flags & NBD_CMD_FLAG_FUA) {
- flags |= BDRV_REQ_FUA;
- }
- ret = blk_pwrite(exp->blk, request.from + exp->dev_offset,
- req->data, request.len, flags);
- ret = nbd_send_generic_reply(client, request.handle, ret,
- "writing to file failed", &local_err);
- break;
-
- case NBD_CMD_WRITE_ZEROES:
- flags = 0;
- if (request.flags & NBD_CMD_FLAG_FUA) {
- flags |= BDRV_REQ_FUA;
- }
- if (!(request.flags & NBD_CMD_FLAG_NO_HOLE)) {
- flags |= BDRV_REQ_MAY_UNMAP;
- }
- ret = blk_pwrite_zeroes(exp->blk, request.from + exp->dev_offset,
- request.len, flags);
- ret = nbd_send_generic_reply(client, request.handle, ret,
- "writing to file failed", &local_err);
- break;
-
- case NBD_CMD_DISC:
- /* unreachable, thanks to special case in nbd_co_receive_request() */
- abort();
-
- case NBD_CMD_FLUSH:
- ret = blk_co_flush(exp->blk);
- ret = nbd_send_generic_reply(client, request.handle, ret,
- "flush failed", &local_err);
- break;
-
- case NBD_CMD_TRIM:
- ret = blk_co_pdiscard(exp->blk, request.from + exp->dev_offset,
- request.len);
- ret = nbd_send_generic_reply(client, request.handle, ret,
- "discard failed", &local_err);
- break;
-
- default:
- msg = g_strdup_printf("invalid request type (%" PRIu32 ") received",
- request.type);
- ret = nbd_send_generic_reply(client, request.handle, -EINVAL, msg,
- &local_err);
- g_free(msg);
- }
-
-replied:
if (ret < 0) {
error_prepend(&local_err, "Failed to send reply: ");
goto disconnect;
--
2.14.3
- [Qemu-devel] [PULL 00/17] NBD patches for 2018-03-13 (2.12 softfreeze), Eric Blake, 2018/03/13
- [Qemu-devel] [PULL 04/17] nbd/server: fix: check client->closing before sending reply, Eric Blake, 2018/03/13
- [Qemu-devel] [PULL 08/17] block: let blk_add/remove_aio_context_notifier() tolerate BDS changes, Eric Blake, 2018/03/13
- [Qemu-devel] [PULL 02/17] nbd/server: move nbd_co_send_structured_error up, Eric Blake, 2018/03/13
- [Qemu-devel] [PULL 01/17] iotests: Fix stuck NBD process on 33, Eric Blake, 2018/03/13
- [Qemu-devel] [PULL 13/17] block/nbd-client: save first fatal error in nbd_iter_error, Eric Blake, 2018/03/13
- [Qemu-devel] [PULL 07/17] nbd/server: Honor FUA request on NBD_CMD_TRIM, Eric Blake, 2018/03/13
- [Qemu-devel] [PULL 14/17] nbd: BLOCK_STATUS for standard get_block_status function: client part, Eric Blake, 2018/03/13
- [Qemu-devel] [PULL 03/17] nbd/server: fix sparse read, Eric Blake, 2018/03/13
- [Qemu-devel] [PULL 06/17] nbd/server: refactor nbd_trip: split out nbd_handle_request,
Eric Blake <=
- [Qemu-devel] [PULL 05/17] nbd/server: refactor nbd_trip: cmd_read and generic reply, Eric Blake, 2018/03/13
- [Qemu-devel] [PULL 11/17] nbd/server: add nbd_read_opt_name helper, Eric Blake, 2018/03/13
- [Qemu-devel] [PULL 12/17] nbd: BLOCK_STATUS for standard get_block_status function: server part, Eric Blake, 2018/03/13
- [Qemu-devel] [PULL 10/17] nbd/server: add nbd_opt_invalid helper, Eric Blake, 2018/03/13
- [Qemu-devel] [PULL 17/17] iotests: new test 209 for NBD BLOCK_STATUS, Eric Blake, 2018/03/13
- [Qemu-devel] [PULL 09/17] iotests: add 208 nbd-server + blockdev-snapshot-sync test case, Eric Blake, 2018/03/13
- [Qemu-devel] [PULL 16/17] iotests: add file_path helper, Eric Blake, 2018/03/13
- [Qemu-devel] [PULL 15/17] iotests.py: tiny refactor: move system imports up, Eric Blake, 2018/03/13
- Re: [Qemu-devel] [PULL 00/17] NBD patches for 2018-03-13 (2.12 softfreeze), no-reply, 2018/03/13
- Re: [Qemu-devel] [PULL 00/17] NBD patches for 2018-03-13 (2.12 softfreeze), no-reply, 2018/03/13