diff options
author | Paolo Bonzini <pbonzini@redhat.com> | 2011-09-19 14:25:30 +0200 |
---|---|---|
committer | Paolo Bonzini <pbonzini@redhat.com> | 2011-12-22 11:53:58 +0100 |
commit | 220455920384af77670e84953f3763153e39bcfa (patch) | |
tree | 014c5d59371f102277da6724b70aa143c5e22803 /nbd.c | |
parent | a478f6e595dab1801931d56d097623d65f5b6d1d (diff) |
qemu-nbd: introduce nbd_do_send_reply
Group the sending of a reply and the associated data into a new function.
Without corking, the caller would be forced to leave 12 free bytes at the
beginning of the data pointer. Not too ugly, but still ugly. :)
Using nbd_do_send_reply everywhere will help when the routine will set up
the write handler that re-enters the send coroutine.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Diffstat (limited to 'nbd.c')
-rw-r--r-- | nbd.c | 46 |
1 files changed, 32 insertions, 14 deletions
@@ -583,6 +583,34 @@ static int nbd_send_reply(int csock, struct nbd_reply *reply) return 0; } +static int nbd_do_send_reply(int csock, struct nbd_reply *reply, + uint8_t *data, int len) +{ + int rc, ret; + + if (!len) { + rc = nbd_send_reply(csock, reply); + if (rc == -1) { + rc = -errno; + } + } else { + socket_set_cork(csock, 1); + rc = nbd_send_reply(csock, reply); + if (rc != -1) { + ret = write_sync(csock, data, len); + if (ret != len) { + errno = EIO; + rc = -1; + } + } + if (rc == -1) { + rc = -errno; + } + socket_set_cork(csock, 0); + } + return rc; +} + int nbd_trip(BlockDriverState *bs, int csock, off_t size, uint64_t dev_offset, uint32_t nbdflags, uint8_t *data) @@ -637,18 +665,8 @@ int nbd_trip(BlockDriverState *bs, int csock, off_t size, } TRACE("Read %u byte(s)", request.len); - socket_set_cork(csock, 1); - if (nbd_send_reply(csock, &reply) == -1) + if (nbd_do_send_reply(csock, &reply, data, request.len) < 0) return -1; - - TRACE("Sending data to client"); - - if (write_sync(csock, data, request.len) != request.len) { - LOG("writing to socket failed"); - errno = EINVAL; - return -1; - } - socket_set_cork(csock, 0); break; case NBD_CMD_WRITE: TRACE("Request type is WRITE"); @@ -684,7 +702,7 @@ int nbd_trip(BlockDriverState *bs, int csock, off_t size, } } - if (nbd_send_reply(csock, &reply) == -1) + if (nbd_do_send_reply(csock, &reply, NULL, 0) < 0) return -1; break; case NBD_CMD_DISC: @@ -700,7 +718,7 @@ int nbd_trip(BlockDriverState *bs, int csock, off_t size, reply.error = -ret; } - if (nbd_send_reply(csock, &reply) == -1) + if (nbd_do_send_reply(csock, &reply, NULL, 0) < 0) return -1; break; case NBD_CMD_TRIM: @@ -711,7 +729,7 @@ int nbd_trip(BlockDriverState *bs, int csock, off_t size, LOG("discard failed"); reply.error = -ret; } - if (nbd_send_reply(csock, &reply) == -1) + if (nbd_do_send_reply(csock, &reply, NULL, 0) < 0) return -1; break; default: |