From nobody Sun May 5 12:18:29 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1496250265779471.72000070809406; Wed, 31 May 2017 10:04:25 -0700 (PDT) Received: from localhost ([::1]:32946 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dG72k-0006f1-81 for importer@patchew.org; Wed, 31 May 2017 13:04:22 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57086) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dG6uZ-0007r9-4x for qemu-devel@nongnu.org; Wed, 31 May 2017 12:55:57 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dG6uS-00041r-3D for qemu-devel@nongnu.org; Wed, 31 May 2017 12:55:55 -0400 Received: from mailhub.sw.ru ([195.214.232.25]:45237 helo=relay.sw.ru) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dG6uR-000401-Gt for qemu-devel@nongnu.org; Wed, 31 May 2017 12:55:47 -0400 Received: from kvm.sw.ru (msk-vpn.virtuozzo.com [195.214.232.6]) by relay.sw.ru (8.13.4/8.13.4) with ESMTP id v4VGtfWh011112; Wed, 31 May 2017 19:55:42 +0300 (MSK) From: Vladimir Sementsov-Ogievskiy To: qemu-devel@nongnu.org Date: Wed, 31 May 2017 19:55:30 +0300 Message-Id: <20170531165541.47338-2-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20170531165541.47338-1-vsementsov@virtuozzo.com> References: <20170531165541.47338-1-vsementsov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: OpenBSD 3.x [fuzzy] X-Received-From: 195.214.232.25 Subject: [Qemu-devel] [PATCH 01/12] nbd: rename read_sync and friends X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: pbonzini@redhat.com, vsementsov@virtuozzo.com, den@openvz.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Rename nbd_wr_syncv -> nbd_rwv read_sync -> nbd_read read_sync_eof -> nbd_read_eof write_sync -> nbd_write drop_sync -> nbd_drop 1. nbd_ prefix read_sync and write_sync are already shared, so it is good to have a namespace prefix. drop_sync will be shared, and read_sync_eof is related to read_sync, so let's rename them all. 2. _sync suffix _sync is related to the fact, that nbd_wr_syncv doesn't return if write to socket returns EAGAIN. In first implementation nbd_wr_syncv just loops while getting EAGAIN, current implementation yields in this case. Why to get rid of it: - it is normal for r/w functions to be synchronous, so having additional suffix for it looks redundant (contrariwise, we have _aio suffix for async functions) - _sync suffix in block layer is used when function does flush (so using it for other thing is confusing a bit) - keep function names short after adding nbd_ prefix 3. for nbd_wr_syncv let's use more common notation 'rw' Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Eric Blake --- block/nbd-client.c | 8 ++++---- include/block/nbd.h | 8 ++------ nbd/client.c | 42 +++++++++++++++++++++--------------------- nbd/common.c | 8 ++------ nbd/nbd-internal.h | 26 +++++++++++++------------- nbd/server.c | 12 ++++++------ 6 files changed, 48 insertions(+), 56 deletions(-) diff --git a/block/nbd-client.c b/block/nbd-client.c index 09d955bc4d..3e080ca7f0 100644 --- a/block/nbd-client.c +++ b/block/nbd-client.c @@ -140,8 +140,8 @@ static int nbd_co_send_request(BlockDriverState *bs, qio_channel_set_cork(s->ioc, true); rc =3D nbd_send_request(s->ioc, request); if (rc >=3D 0) { - ret =3D nbd_wr_syncv(s->ioc, qiov->iov, qiov->niov, request->l= en, - false, NULL); + ret =3D nbd_rwv(s->ioc, qiov->iov, qiov->niov, request->len, f= alse, + NULL); if (ret !=3D request->len) { rc =3D -EIO; } @@ -169,8 +169,8 @@ static void nbd_co_receive_reply(NBDClientSession *s, reply->error =3D EIO; } else { if (qiov && reply->error =3D=3D 0) { - ret =3D nbd_wr_syncv(s->ioc, qiov->iov, qiov->niov, request->l= en, - true, NULL); + ret =3D nbd_rwv(s->ioc, qiov->iov, qiov->niov, request->len, t= rue, + NULL); if (ret !=3D request->len) { reply->error =3D EIO; } diff --git a/include/block/nbd.h b/include/block/nbd.h index e0c64e4981..0a5ecd0e5c 100644 --- a/include/block/nbd.h +++ b/include/block/nbd.h @@ -123,12 +123,8 @@ enum { * aren't overflowing some other buffer. */ #define NBD_MAX_NAME_SIZE 256 =20 -ssize_t nbd_wr_syncv(QIOChannel *ioc, - struct iovec *iov, - size_t niov, - size_t length, - bool do_read, - Error **errp); +ssize_t nbd_rwv(QIOChannel *ioc, struct iovec *iov, size_t niov, size_t le= ngth, + bool do_read, Error **errp); int nbd_receive_negotiate(QIOChannel *ioc, const char *name, uint16_t *fla= gs, QCryptoTLSCreds *tlscreds, const char *hostname, QIOChannel **outioc, diff --git a/nbd/client.c b/nbd/client.c index f9e1d75be4..08050c39b3 100644 --- a/nbd/client.c +++ b/nbd/client.c @@ -88,7 +88,7 @@ static QTAILQ_HEAD(, NBDExport) exports =3D QTAILQ_HEAD_I= NITIALIZER(exports); =20 /* Discard length bytes from channel. Return -errno on failure and 0 on * success*/ -static int drop_sync(QIOChannel *ioc, size_t size, Error **errp) +static int nbd_drop(QIOChannel *ioc, size_t size, Error **errp) { ssize_t ret =3D 0; char small[1024]; @@ -97,7 +97,7 @@ static int drop_sync(QIOChannel *ioc, size_t size, Error = **errp) buffer =3D sizeof(small) >=3D size ? small : g_malloc(MIN(65536, size)= ); while (size > 0) { ssize_t count =3D MIN(65536, size); - ret =3D read_sync(ioc, buffer, MIN(65536, size), errp); + ret =3D nbd_read(ioc, buffer, MIN(65536, size), errp); =20 if (ret < 0) { goto cleanup; @@ -135,12 +135,12 @@ static int nbd_send_option_request(QIOChannel *ioc, u= int32_t opt, stl_be_p(&req.option, opt); stl_be_p(&req.length, len); =20 - if (write_sync(ioc, &req, sizeof(req), errp) < 0) { + if (nbd_write(ioc, &req, sizeof(req), errp) < 0) { error_prepend(errp, "Failed to send option request header"); return -1; } =20 - if (len && write_sync(ioc, (char *) data, len, errp) < 0) { + if (len && nbd_write(ioc, (char *) data, len, errp) < 0) { error_prepend(errp, "Failed to send option request data"); return -1; } @@ -169,7 +169,7 @@ static int nbd_receive_option_reply(QIOChannel *ioc, ui= nt32_t opt, nbd_opt_reply *reply, Error **errp) { QEMU_BUILD_BUG_ON(sizeof(*reply) !=3D 20); - if (read_sync(ioc, reply, sizeof(*reply), errp) < 0) { + if (nbd_read(ioc, reply, sizeof(*reply), errp) < 0) { error_prepend(errp, "failed to read option reply"); nbd_send_opt_abort(ioc); return -1; @@ -218,7 +218,7 @@ static int nbd_handle_reply_err(QIOChannel *ioc, nbd_op= t_reply *reply, goto cleanup; } msg =3D g_malloc(reply->length + 1); - if (read_sync(ioc, msg, reply->length, errp) < 0) { + if (nbd_read(ioc, msg, reply->length, errp) < 0) { error_prepend(errp, "failed to read option error message"); goto cleanup; } @@ -320,7 +320,7 @@ static int nbd_receive_list(QIOChannel *ioc, const char= *want, bool *match, nbd_send_opt_abort(ioc); return -1; } - if (read_sync(ioc, &namelen, sizeof(namelen), errp) < 0) { + if (nbd_read(ioc, &namelen, sizeof(namelen), errp) < 0) { error_prepend(errp, "failed to read option name length"); nbd_send_opt_abort(ioc); return -1; @@ -333,7 +333,7 @@ static int nbd_receive_list(QIOChannel *ioc, const char= *want, bool *match, return -1; } if (namelen !=3D strlen(want)) { - if (drop_sync(ioc, len, errp) < 0) { + if (nbd_drop(ioc, len, errp) < 0) { error_prepend(errp, "failed to skip export name with wrong len= gth"); nbd_send_opt_abort(ioc); return -1; @@ -342,14 +342,14 @@ static int nbd_receive_list(QIOChannel *ioc, const ch= ar *want, bool *match, } =20 assert(namelen < sizeof(name)); - if (read_sync(ioc, name, namelen, errp) < 0) { + if (nbd_read(ioc, name, namelen, errp) < 0) { error_prepend(errp, "failed to read export name"); nbd_send_opt_abort(ioc); return -1; } name[namelen] =3D '\0'; len -=3D namelen; - if (drop_sync(ioc, len, errp) < 0) { + if (nbd_drop(ioc, len, errp) < 0) { error_prepend(errp, "failed to read export description"); nbd_send_opt_abort(ioc); return -1; @@ -476,7 +476,7 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *= name, uint16_t *flags, goto fail; } =20 - if (read_sync(ioc, buf, 8, errp) < 0) { + if (nbd_read(ioc, buf, 8, errp) < 0) { error_prepend(errp, "Failed to read data"); goto fail; } @@ -502,7 +502,7 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *= name, uint16_t *flags, goto fail; } =20 - if (read_sync(ioc, &magic, sizeof(magic), errp) < 0) { + if (nbd_read(ioc, &magic, sizeof(magic), errp) < 0) { error_prepend(errp, "Failed to read magic"); goto fail; } @@ -514,7 +514,7 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *= name, uint16_t *flags, uint16_t globalflags; bool fixedNewStyle =3D false; =20 - if (read_sync(ioc, &globalflags, sizeof(globalflags), errp) < 0) { + if (nbd_read(ioc, &globalflags, sizeof(globalflags), errp) < 0) { error_prepend(errp, "Failed to read server flags"); goto fail; } @@ -532,7 +532,7 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *= name, uint16_t *flags, } /* client requested flags */ clientflags =3D cpu_to_be32(clientflags); - if (write_sync(ioc, &clientflags, sizeof(clientflags), errp) < 0) { + if (nbd_write(ioc, &clientflags, sizeof(clientflags), errp) < 0) { error_prepend(errp, "Failed to send clientflags field"); goto fail; } @@ -570,13 +570,13 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char= *name, uint16_t *flags, } =20 /* Read the response */ - if (read_sync(ioc, &s, sizeof(s), errp) < 0) { + if (nbd_read(ioc, &s, sizeof(s), errp) < 0) { error_prepend(errp, "Failed to read export length"); goto fail; } *size =3D be64_to_cpu(s); =20 - if (read_sync(ioc, flags, sizeof(*flags), errp) < 0) { + if (nbd_read(ioc, flags, sizeof(*flags), errp) < 0) { error_prepend(errp, "Failed to read export flags"); goto fail; } @@ -593,14 +593,14 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char= *name, uint16_t *flags, goto fail; } =20 - if (read_sync(ioc, &s, sizeof(s), errp) < 0) { + if (nbd_read(ioc, &s, sizeof(s), errp) < 0) { error_prepend(errp, "Failed to read export length"); goto fail; } *size =3D be64_to_cpu(s); TRACE("Size is %" PRIu64, *size); =20 - if (read_sync(ioc, &oldflags, sizeof(oldflags), errp) < 0) { + if (nbd_read(ioc, &oldflags, sizeof(oldflags), errp) < 0) { error_prepend(errp, "Failed to read export flags"); goto fail; } @@ -616,7 +616,7 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *= name, uint16_t *flags, } =20 TRACE("Size is %" PRIu64 ", export flags %" PRIx16, *size, *flags); - if (zeroes && drop_sync(ioc, 124, errp) < 0) { + if (zeroes && nbd_drop(ioc, 124, errp) < 0) { error_prepend(errp, "Failed to read reserved block"); goto fail; } @@ -759,7 +759,7 @@ ssize_t nbd_send_request(QIOChannel *ioc, NBDRequest *r= equest) stq_be_p(buf + 16, request->from); stl_be_p(buf + 24, request->len); =20 - return write_sync(ioc, buf, sizeof(buf), NULL); + return nbd_write(ioc, buf, sizeof(buf), NULL); } =20 ssize_t nbd_receive_reply(QIOChannel *ioc, NBDReply *reply, Error **errp) @@ -768,7 +768,7 @@ ssize_t nbd_receive_reply(QIOChannel *ioc, NBDReply *re= ply, Error **errp) uint32_t magic; ssize_t ret; =20 - ret =3D read_sync_eof(ioc, buf, sizeof(buf), errp); + ret =3D nbd_read_eof(ioc, buf, sizeof(buf), errp); if (ret <=3D 0) { return ret; } diff --git a/nbd/common.c b/nbd/common.c index bd81637ab9..d6b719ddaa 100644 --- a/nbd/common.c +++ b/nbd/common.c @@ -24,12 +24,8 @@ * The function may be called from coroutine or from non-coroutine context. * When called from non-coroutine context @ioc must be in blocking mode. */ -ssize_t nbd_wr_syncv(QIOChannel *ioc, - struct iovec *iov, - size_t niov, - size_t length, - bool do_read, - Error **errp) +ssize_t nbd_rwv(QIOChannel *ioc, struct iovec *iov, size_t niov, size_t le= ngth, + bool do_read, Error **errp) { ssize_t done =3D 0; struct iovec *local_iov =3D g_new(struct iovec, niov); diff --git a/nbd/nbd-internal.h b/nbd/nbd-internal.h index d6071640a0..630f553134 100644 --- a/nbd/nbd-internal.h +++ b/nbd/nbd-internal.h @@ -94,14 +94,14 @@ #define NBD_ENOSPC 28 #define NBD_ESHUTDOWN 108 =20 -/* read_sync_eof +/* nbd_read_eof * Tries to read @size bytes from @ioc. Returns number of bytes actually r= ead. * May return a value >=3D 0 and < size only on EOF, i.e. when iteratively= called - * qio_channel_readv() returns 0. So, there are no needs to call read_sync= _eof + * qio_channel_readv() returns 0. So, there are no needs to call nbd_read_= eof * iteratively. */ -static inline ssize_t read_sync_eof(QIOChannel *ioc, void *buffer, size_t = size, - Error **errp) +static inline ssize_t nbd_read_eof(QIOChannel *ioc, void *buffer, size_t s= ize, + Error **errp) { struct iovec iov =3D { .iov_base =3D buffer, .iov_len =3D size }; /* Sockets are kept in blocking mode in the negotiation phase. After @@ -109,16 +109,16 @@ static inline ssize_t read_sync_eof(QIOChannel *ioc, = void *buffer, size_t size, * our request/reply. Synchronization is done with recv_coroutine, so * that this is coroutine-safe. */ - return nbd_wr_syncv(ioc, &iov, 1, size, true, errp); + return nbd_rwv(ioc, &iov, 1, size, true, errp); } =20 -/* read_sync +/* nbd_read * Reads @size bytes from @ioc. Returns 0 on success. */ -static inline int read_sync(QIOChannel *ioc, void *buffer, size_t size, - Error **errp) +static inline int nbd_read(QIOChannel *ioc, void *buffer, size_t size, + Error **errp) { - ssize_t ret =3D read_sync_eof(ioc, buffer, size, errp); + ssize_t ret =3D nbd_read_eof(ioc, buffer, size, errp); =20 if (ret >=3D 0 && ret !=3D size) { ret =3D -EINVAL; @@ -128,15 +128,15 @@ static inline int read_sync(QIOChannel *ioc, void *bu= ffer, size_t size, return ret < 0 ? ret : 0; } =20 -/* write_sync +/* nbd_write * Writes @size bytes to @ioc. Returns 0 on success. */ -static inline int write_sync(QIOChannel *ioc, const void *buffer, size_t s= ize, - Error **errp) +static inline int nbd_write(QIOChannel *ioc, const void *buffer, size_t si= ze, + Error **errp) { struct iovec iov =3D { .iov_base =3D (void *) buffer, .iov_len =3D siz= e }; =20 - ssize_t ret =3D nbd_wr_syncv(ioc, &iov, 1, size, false, errp); + ssize_t ret =3D nbd_rwv(ioc, &iov, 1, size, false, errp); =20 assert(ret < 0 || ret =3D=3D size); =20 diff --git a/nbd/server.c b/nbd/server.c index ee59e5d234..abaf9fb890 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -124,7 +124,7 @@ static int nbd_negotiate_read(QIOChannel *ioc, void *bu= ffer, size_t size) nbd_negotiate_continue, qemu_coroutine_self(), NULL); - ret =3D read_sync(ioc, buffer, size, NULL); + ret =3D nbd_read(ioc, buffer, size, NULL); g_source_remove(watch); return ret; =20 @@ -142,7 +142,7 @@ static int nbd_negotiate_write(QIOChannel *ioc, const v= oid *buffer, size_t size) nbd_negotiate_continue, qemu_coroutine_self(), NULL); - ret =3D write_sync(ioc, buffer, size, NULL); + ret =3D nbd_write(ioc, buffer, size, NULL); g_source_remove(watch); return ret; } @@ -694,7 +694,7 @@ static ssize_t nbd_receive_request(QIOChannel *ioc, NBD= Request *request) uint32_t magic; ssize_t ret; =20 - ret =3D read_sync(ioc, buf, sizeof(buf), NULL); + ret =3D nbd_read(ioc, buf, sizeof(buf), NULL); if (ret < 0) { return ret; } @@ -745,7 +745,7 @@ static ssize_t nbd_send_reply(QIOChannel *ioc, NBDReply= *reply) stl_be_p(buf + 4, reply->error); stq_be_p(buf + 8, reply->handle); =20 - return write_sync(ioc, buf, sizeof(buf), NULL); + return nbd_write(ioc, buf, sizeof(buf), NULL); } =20 #define MAX_NBD_REQUESTS 16 @@ -1048,7 +1048,7 @@ static ssize_t nbd_co_send_reply(NBDRequestData *req,= NBDReply *reply, qio_channel_set_cork(client->ioc, true); rc =3D nbd_send_reply(client->ioc, reply); if (rc >=3D 0) { - ret =3D write_sync(client->ioc, req->data, len, NULL); + ret =3D nbd_write(client->ioc, req->data, len, NULL); if (ret < 0) { rc =3D -EIO; } @@ -1123,7 +1123,7 @@ static ssize_t nbd_co_receive_request(NBDRequestData = *req, if (request->type =3D=3D NBD_CMD_WRITE) { TRACE("Reading %" PRIu32 " byte(s)", request->len); =20 - if (read_sync(client->ioc, req->data, request->len, NULL) < 0) { + if (nbd_read(client->ioc, req->data, request->len, NULL) < 0) { LOG("reading from socket failed"); rc =3D -EIO; goto out; --=20 2.11.1 From nobody Sun May 5 12:18:29 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1496249985863778.1239153401198; Wed, 31 May 2017 09:59:45 -0700 (PDT) Received: from localhost ([::1]:32917 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dG6yG-0002mf-EE for importer@patchew.org; Wed, 31 May 2017 12:59:44 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57001) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dG6uV-0007q0-RH for qemu-devel@nongnu.org; Wed, 31 May 2017 12:55:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dG6uR-000410-TC for qemu-devel@nongnu.org; Wed, 31 May 2017 12:55:51 -0400 Received: from mailhub.sw.ru ([195.214.232.25]:12885 helo=relay.sw.ru) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dG6uR-00040B-Gp for qemu-devel@nongnu.org; Wed, 31 May 2017 12:55:47 -0400 Received: from kvm.sw.ru (msk-vpn.virtuozzo.com [195.214.232.6]) by relay.sw.ru (8.13.4/8.13.4) with ESMTP id v4VGtfWi011112; Wed, 31 May 2017 19:55:42 +0300 (MSK) From: Vladimir Sementsov-Ogievskiy To: qemu-devel@nongnu.org Date: Wed, 31 May 2017 19:55:31 +0300 Message-Id: <20170531165541.47338-3-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20170531165541.47338-1-vsementsov@virtuozzo.com> References: <20170531165541.47338-1-vsementsov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: OpenBSD 3.x [fuzzy] X-Received-From: 195.214.232.25 Subject: [Qemu-devel] [PATCH 02/12] nbd: make nbd_drop public X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: pbonzini@redhat.com, vsementsov@virtuozzo.com, den@openvz.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Following commit will reuse it for nbd server too. Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Eric Blake --- nbd/client.c | 26 -------------------------- nbd/common.c | 26 ++++++++++++++++++++++++++ nbd/nbd-internal.h | 2 ++ 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/nbd/client.c b/nbd/client.c index 08050c39b3..215857f65d 100644 --- a/nbd/client.c +++ b/nbd/client.c @@ -86,32 +86,6 @@ static QTAILQ_HEAD(, NBDExport) exports =3D QTAILQ_HEAD_= INITIALIZER(exports); =20 */ =20 -/* Discard length bytes from channel. Return -errno on failure and 0 on - * success*/ -static int nbd_drop(QIOChannel *ioc, size_t size, Error **errp) -{ - ssize_t ret =3D 0; - char small[1024]; - char *buffer; - - buffer =3D sizeof(small) >=3D size ? small : g_malloc(MIN(65536, size)= ); - while (size > 0) { - ssize_t count =3D MIN(65536, size); - ret =3D nbd_read(ioc, buffer, MIN(65536, size), errp); - - if (ret < 0) { - goto cleanup; - } - size -=3D count; - } - - cleanup: - if (buffer !=3D small) { - g_free(buffer); - } - return ret; -} - /* Send an option request. * * The request is for option @opt, with @data containing @len bytes of diff --git a/nbd/common.c b/nbd/common.c index d6b719ddaa..6b5c1b7b02 100644 --- a/nbd/common.c +++ b/nbd/common.c @@ -65,6 +65,32 @@ ssize_t nbd_rwv(QIOChannel *ioc, struct iovec *iov, size= _t niov, size_t length, return done; } =20 +/* Discard length bytes from channel. Return -errno on failure and 0 on + * success */ +int nbd_drop(QIOChannel *ioc, size_t size, Error **errp) +{ + ssize_t ret =3D 0; + char small[1024]; + char *buffer; + + buffer =3D sizeof(small) >=3D size ? small : g_malloc(MIN(65536, size)= ); + while (size > 0) { + ssize_t count =3D MIN(65536, size); + ret =3D nbd_read(ioc, buffer, MIN(65536, size), errp); + + if (ret < 0) { + goto cleanup; + } + size -=3D count; + } + + cleanup: + if (buffer !=3D small) { + g_free(buffer); + } + return ret; +} + =20 void nbd_tls_handshake(QIOTask *task, void *opaque) diff --git a/nbd/nbd-internal.h b/nbd/nbd-internal.h index 630f553134..d1c2bed471 100644 --- a/nbd/nbd-internal.h +++ b/nbd/nbd-internal.h @@ -153,4 +153,6 @@ struct NBDTLSHandshakeData { void nbd_tls_handshake(QIOTask *task, void *opaque); =20 +int nbd_drop(QIOChannel *ioc, size_t size, Error **errp); + #endif --=20 2.11.1 From nobody Sun May 5 12:18:29 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 149625026705551.17201944538397; Wed, 31 May 2017 10:04:27 -0700 (PDT) Received: from localhost ([::1]:32945 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dG72j-0006eY-V3 for importer@patchew.org; Wed, 31 May 2017 13:04:22 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57076) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dG6uY-0007qe-21 for qemu-devel@nongnu.org; Wed, 31 May 2017 12:55:55 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dG6uS-00041v-3R for qemu-devel@nongnu.org; Wed, 31 May 2017 12:55:54 -0400 Received: from mailhub.sw.ru ([195.214.232.25]:16738 helo=relay.sw.ru) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dG6uR-000402-HC for qemu-devel@nongnu.org; Wed, 31 May 2017 12:55:47 -0400 Received: from kvm.sw.ru (msk-vpn.virtuozzo.com [195.214.232.6]) by relay.sw.ru (8.13.4/8.13.4) with ESMTP id v4VGtfWj011112; Wed, 31 May 2017 19:55:42 +0300 (MSK) From: Vladimir Sementsov-Ogievskiy To: qemu-devel@nongnu.org Date: Wed, 31 May 2017 19:55:32 +0300 Message-Id: <20170531165541.47338-4-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20170531165541.47338-1-vsementsov@virtuozzo.com> References: <20170531165541.47338-1-vsementsov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: OpenBSD 3.x [fuzzy] X-Received-From: 195.214.232.25 Subject: [Qemu-devel] [PATCH 03/12] nbd/server: get rid of nbd_negotiate_read and friends X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: pbonzini@redhat.com, vsementsov@virtuozzo.com, den@openvz.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Functions nbd_negotiate_{read,write,drop_sync} were introduced in 1a6245a5b, when nbd_rwv (was nbd_wr_sync) was working through qemu_co_sendv_recvv (the path is nbd_wr_sync -> qemu_co_{recv/send} -> qemu_co_send_recv -> qemu_co_sendv_recvv), which just yields, without setting any handlers. But starting from ff82911cd nbd_rwv (was nbd_wr_syncv) works through qio_channel_yield() which sets handlers, so watchers are redundant in nbd_negotiate_{read,write,drop_sync}, then, let's just use nbd_{read,write,drop} functions. Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Eric Blake --- nbd/server.c | 107 ++++++++++++-------------------------------------------= ---- 1 file changed, 22 insertions(+), 85 deletions(-) diff --git a/nbd/server.c b/nbd/server.c index abaf9fb890..b830cd23d3 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -104,69 +104,6 @@ struct NBDClient { =20 static void nbd_client_receive_next_request(NBDClient *client); =20 -static gboolean nbd_negotiate_continue(QIOChannel *ioc, - GIOCondition condition, - void *opaque) -{ - qemu_coroutine_enter(opaque); - return TRUE; -} - -static int nbd_negotiate_read(QIOChannel *ioc, void *buffer, size_t size) -{ - ssize_t ret; - guint watch; - - assert(qemu_in_coroutine()); - /* Negotiation are always in main loop. */ - watch =3D qio_channel_add_watch(ioc, - G_IO_IN, - nbd_negotiate_continue, - qemu_coroutine_self(), - NULL); - ret =3D nbd_read(ioc, buffer, size, NULL); - g_source_remove(watch); - return ret; - -} - -static int nbd_negotiate_write(QIOChannel *ioc, const void *buffer, size_t= size) -{ - ssize_t ret; - guint watch; - - assert(qemu_in_coroutine()); - /* Negotiation are always in main loop. */ - watch =3D qio_channel_add_watch(ioc, - G_IO_OUT, - nbd_negotiate_continue, - qemu_coroutine_self(), - NULL); - ret =3D nbd_write(ioc, buffer, size, NULL); - g_source_remove(watch); - return ret; -} - -static int nbd_negotiate_drop_sync(QIOChannel *ioc, size_t size) -{ - ssize_t ret; - uint8_t *buffer =3D g_malloc(MIN(65536, size)); - - while (size > 0) { - size_t count =3D MIN(65536, size); - ret =3D nbd_negotiate_read(ioc, buffer, count); - if (ret < 0) { - g_free(buffer); - return ret; - } - - size -=3D count; - } - - g_free(buffer); - return 0; -} - /* Basic flow for negotiation =20 Server Client @@ -205,22 +142,22 @@ static int nbd_negotiate_send_rep_len(QIOChannel *ioc= , uint32_t type, type, opt, len); =20 magic =3D cpu_to_be64(NBD_REP_MAGIC); - if (nbd_negotiate_write(ioc, &magic, sizeof(magic)) < 0) { + if (nbd_write(ioc, &magic, sizeof(magic), NULL) < 0) { LOG("write failed (rep magic)"); return -EINVAL; } opt =3D cpu_to_be32(opt); - if (nbd_negotiate_write(ioc, &opt, sizeof(opt)) < 0) { + if (nbd_write(ioc, &opt, sizeof(opt), NULL) < 0) { LOG("write failed (rep opt)"); return -EINVAL; } type =3D cpu_to_be32(type); - if (nbd_negotiate_write(ioc, &type, sizeof(type)) < 0) { + if (nbd_write(ioc, &type, sizeof(type), NULL) < 0) { LOG("write failed (rep type)"); return -EINVAL; } len =3D cpu_to_be32(len); - if (nbd_negotiate_write(ioc, &len, sizeof(len)) < 0) { + if (nbd_write(ioc, &len, sizeof(len), NULL) < 0) { LOG("write failed (rep data length)"); return -EINVAL; } @@ -255,7 +192,7 @@ nbd_negotiate_send_rep_err(QIOChannel *ioc, uint32_t ty= pe, if (ret < 0) { goto out; } - if (nbd_negotiate_write(ioc, msg, len) < 0) { + if (nbd_write(ioc, msg, len, NULL) < 0) { LOG("write failed (error message)"); ret =3D -EIO; } else { @@ -286,15 +223,15 @@ static int nbd_negotiate_send_rep_list(QIOChannel *io= c, NBDExport *exp) } =20 len =3D cpu_to_be32(name_len); - if (nbd_negotiate_write(ioc, &len, sizeof(len)) < 0) { + if (nbd_write(ioc, &len, sizeof(len), NULL) < 0) { LOG("write failed (name length)"); return -EINVAL; } - if (nbd_negotiate_write(ioc, name, name_len) < 0) { + if (nbd_write(ioc, name, name_len, NULL) < 0) { LOG("write failed (name buffer)"); return -EINVAL; } - if (nbd_negotiate_write(ioc, desc, desc_len) < 0) { + if (nbd_write(ioc, desc, desc_len, NULL) < 0) { LOG("write failed (description buffer)"); return -EINVAL; } @@ -308,7 +245,7 @@ static int nbd_negotiate_handle_list(NBDClient *client,= uint32_t length) NBDExport *exp; =20 if (length) { - if (nbd_negotiate_drop_sync(client->ioc, length) < 0) { + if (nbd_drop(client->ioc, length, NULL) < 0) { return -EIO; } return nbd_negotiate_send_rep_err(client->ioc, @@ -339,7 +276,7 @@ static int nbd_negotiate_handle_export_name(NBDClient *= client, uint32_t length) LOG("Bad length received"); goto fail; } - if (nbd_negotiate_read(client->ioc, name, length) < 0) { + if (nbd_read(client->ioc, name, length, NULL) < 0) { LOG("read failed"); goto fail; } @@ -372,7 +309,7 @@ static QIOChannel *nbd_negotiate_handle_starttls(NBDCli= ent *client, TRACE("Setting up TLS"); ioc =3D client->ioc; if (length) { - if (nbd_negotiate_drop_sync(ioc, length) < 0) { + if (nbd_drop(ioc, length, NULL) < 0) { return NULL; } nbd_negotiate_send_rep_err(ioc, NBD_REP_ERR_INVALID, NBD_OPT_START= TLS, @@ -436,7 +373,7 @@ static int nbd_negotiate_options(NBDClient *client) ... Rest of request */ =20 - if (nbd_negotiate_read(client->ioc, &flags, sizeof(flags)) < 0) { + if (nbd_read(client->ioc, &flags, sizeof(flags), NULL) < 0) { LOG("read failed"); return -EIO; } @@ -462,7 +399,7 @@ static int nbd_negotiate_options(NBDClient *client) uint32_t clientflags, length; uint64_t magic; =20 - if (nbd_negotiate_read(client->ioc, &magic, sizeof(magic)) < 0) { + if (nbd_read(client->ioc, &magic, sizeof(magic), NULL) < 0) { LOG("read failed"); return -EINVAL; } @@ -472,15 +409,15 @@ static int nbd_negotiate_options(NBDClient *client) return -EINVAL; } =20 - if (nbd_negotiate_read(client->ioc, &clientflags, - sizeof(clientflags)) < 0) + if (nbd_read(client->ioc, &clientflags, + sizeof(clientflags), NULL) < 0) { LOG("read failed"); return -EINVAL; } clientflags =3D be32_to_cpu(clientflags); =20 - if (nbd_negotiate_read(client->ioc, &length, sizeof(length)) < 0) { + if (nbd_read(client->ioc, &length, sizeof(length), NULL) < 0) { LOG("read failed"); return -EINVAL; } @@ -510,7 +447,7 @@ static int nbd_negotiate_options(NBDClient *client) return -EINVAL; =20 default: - if (nbd_negotiate_drop_sync(client->ioc, length) < 0) { + if (nbd_drop(client->ioc, length, NULL) < 0) { return -EIO; } ret =3D nbd_negotiate_send_rep_err(client->ioc, @@ -548,7 +485,7 @@ static int nbd_negotiate_options(NBDClient *client) return nbd_negotiate_handle_export_name(client, length); =20 case NBD_OPT_STARTTLS: - if (nbd_negotiate_drop_sync(client->ioc, length) < 0) { + if (nbd_drop(client->ioc, length, NULL) < 0) { return -EIO; } if (client->tlscreds) { @@ -567,7 +504,7 @@ static int nbd_negotiate_options(NBDClient *client) } break; default: - if (nbd_negotiate_drop_sync(client->ioc, length) < 0) { + if (nbd_drop(client->ioc, length, NULL) < 0) { return -EIO; } ret =3D nbd_negotiate_send_rep_err(client->ioc, @@ -656,12 +593,12 @@ static coroutine_fn int nbd_negotiate(NBDClientNewDat= a *data) TRACE("TLS cannot be enabled with oldstyle protocol"); goto fail; } - if (nbd_negotiate_write(client->ioc, buf, sizeof(buf)) < 0) { + if (nbd_write(client->ioc, buf, sizeof(buf), NULL) < 0) { LOG("write failed"); goto fail; } } else { - if (nbd_negotiate_write(client->ioc, buf, 18) < 0) { + if (nbd_write(client->ioc, buf, 18, NULL) < 0) { LOG("write failed"); goto fail; } @@ -676,7 +613,7 @@ static coroutine_fn int nbd_negotiate(NBDClientNewData = *data) stq_be_p(buf + 18, client->exp->size); stw_be_p(buf + 26, client->exp->nbdflags | myflags); len =3D client->no_zeroes ? 10 : sizeof(buf) - 18; - if (nbd_negotiate_write(client->ioc, buf + 18, len) < 0) { + if (nbd_write(client->ioc, buf + 18, len, NULL) < 0) { LOG("write failed"); goto fail; } --=20 2.11.1 From nobody Sun May 5 12:18:29 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1496249984536558.7347251272316; Wed, 31 May 2017 09:59:44 -0700 (PDT) Received: from localhost ([::1]:32915 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dG6yF-0002lL-1p for importer@patchew.org; Wed, 31 May 2017 12:59:43 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56998) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dG6uV-0007py-Pv for qemu-devel@nongnu.org; Wed, 31 May 2017 12:55:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dG6uR-00041H-TM for qemu-devel@nongnu.org; Wed, 31 May 2017 12:55:51 -0400 Received: from mailhub.sw.ru ([195.214.232.25]:28999 helo=relay.sw.ru) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dG6uR-000404-HG for qemu-devel@nongnu.org; Wed, 31 May 2017 12:55:47 -0400 Received: from kvm.sw.ru (msk-vpn.virtuozzo.com [195.214.232.6]) by relay.sw.ru (8.13.4/8.13.4) with ESMTP id v4VGtfWk011112; Wed, 31 May 2017 19:55:42 +0300 (MSK) From: Vladimir Sementsov-Ogievskiy To: qemu-devel@nongnu.org Date: Wed, 31 May 2017 19:55:33 +0300 Message-Id: <20170531165541.47338-5-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20170531165541.47338-1-vsementsov@virtuozzo.com> References: <20170531165541.47338-1-vsementsov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: OpenBSD 3.x [fuzzy] X-Received-From: 195.214.232.25 Subject: [Qemu-devel] [PATCH 04/12] nbd/server: get rid of ssize_t X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: pbonzini@redhat.com, vsementsov@virtuozzo.com, den@openvz.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Now nbd_read and friends return int, so get rid of ssize_t. Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Eric Blake --- nbd/server.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/nbd/server.c b/nbd/server.c index b830cd23d3..8985df86b4 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -625,11 +625,11 @@ fail: return rc; } =20 -static ssize_t nbd_receive_request(QIOChannel *ioc, NBDRequest *request) +static int nbd_receive_request(QIOChannel *ioc, NBDRequest *request) { uint8_t buf[NBD_REQUEST_SIZE]; uint32_t magic; - ssize_t ret; + int ret; =20 ret =3D nbd_read(ioc, buf, sizeof(buf), NULL); if (ret < 0) { @@ -663,7 +663,7 @@ static ssize_t nbd_receive_request(QIOChannel *ioc, NBD= Request *request) return 0; } =20 -static ssize_t nbd_send_reply(QIOChannel *ioc, NBDReply *reply) +static int nbd_send_reply(QIOChannel *ioc, NBDReply *reply) { uint8_t buf[NBD_REPLY_SIZE]; =20 @@ -969,11 +969,10 @@ void nbd_export_close_all(void) } } =20 -static ssize_t nbd_co_send_reply(NBDRequestData *req, NBDReply *reply, - int len) +static int nbd_co_send_reply(NBDRequestData *req, NBDReply *reply, int len) { NBDClient *client =3D req->client; - ssize_t rc, ret; + int rc, ret; =20 g_assert(qemu_in_coroutine()); qemu_co_mutex_lock(&client->send_lock); @@ -1003,11 +1002,10 @@ static ssize_t nbd_co_send_reply(NBDRequestData *re= q, NBDReply *reply, * and any other negative value to report an error to the client * (although the caller may still need to disconnect after reporting * the error). */ -static ssize_t nbd_co_receive_request(NBDRequestData *req, - NBDRequest *request) +static int nbd_co_receive_request(NBDRequestData *req, NBDRequest *request) { NBDClient *client =3D req->client; - ssize_t rc; + int rc; =20 g_assert(qemu_in_coroutine()); assert(client->recv_coroutine =3D=3D qemu_coroutine_self()); @@ -1105,7 +1103,7 @@ static coroutine_fn void nbd_trip(void *opaque) NBDRequestData *req; NBDRequest request =3D { 0 }; /* GCC thinks it can be used uninitia= lized */ NBDReply reply; - ssize_t ret; + int ret; int flags; =20 TRACE("Reading request."); --=20 2.11.1 From nobody Sun May 5 12:18:30 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 149624986056033.55833423224783; Wed, 31 May 2017 09:57:40 -0700 (PDT) Received: from localhost ([::1]:32907 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dG6wC-0000ZQ-OB for importer@patchew.org; Wed, 31 May 2017 12:57:36 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56997) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dG6uV-0007px-Q2 for qemu-devel@nongnu.org; Wed, 31 May 2017 12:55:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dG6uR-00040x-SX for qemu-devel@nongnu.org; Wed, 31 May 2017 12:55:51 -0400 Received: from mailhub.sw.ru ([195.214.232.25]:28841 helo=relay.sw.ru) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dG6uR-000408-H1 for qemu-devel@nongnu.org; Wed, 31 May 2017 12:55:47 -0400 Received: from kvm.sw.ru (msk-vpn.virtuozzo.com [195.214.232.6]) by relay.sw.ru (8.13.4/8.13.4) with ESMTP id v4VGtfWl011112; Wed, 31 May 2017 19:55:42 +0300 (MSK) From: Vladimir Sementsov-Ogievskiy To: qemu-devel@nongnu.org Date: Wed, 31 May 2017 19:55:34 +0300 Message-Id: <20170531165541.47338-6-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20170531165541.47338-1-vsementsov@virtuozzo.com> References: <20170531165541.47338-1-vsementsov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: OpenBSD 3.x [fuzzy] X-Received-From: 195.214.232.25 Subject: [Qemu-devel] [PATCH 05/12] nbd/server: refactor nbd_co_send_reply X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: pbonzini@redhat.com, vsementsov@virtuozzo.com, den@openvz.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" As nbd_write never returns value > 0, we can get rid of extra ret. Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Eric Blake --- nbd/server.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/nbd/server.c b/nbd/server.c index 8985df86b4..dd6b241761 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -972,7 +972,7 @@ void nbd_export_close_all(void) static int nbd_co_send_reply(NBDRequestData *req, NBDReply *reply, int len) { NBDClient *client =3D req->client; - int rc, ret; + int rc; =20 g_assert(qemu_in_coroutine()); qemu_co_mutex_lock(&client->send_lock); @@ -983,9 +983,9 @@ static int nbd_co_send_reply(NBDRequestData *req, NBDRe= ply *reply, int len) } else { qio_channel_set_cork(client->ioc, true); rc =3D nbd_send_reply(client->ioc, reply); - if (rc >=3D 0) { - ret =3D nbd_write(client->ioc, req->data, len, NULL); - if (ret < 0) { + if (rc =3D=3D 0) { + rc =3D nbd_write(client->ioc, req->data, len, NULL); + if (rc < 0) { rc =3D -EIO; } } --=20 2.11.1 From nobody Sun May 5 12:18:30 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1496249862616900.2252357708574; Wed, 31 May 2017 09:57:42 -0700 (PDT) Received: from localhost ([::1]:32908 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dG6wD-0000a9-7r for importer@patchew.org; Wed, 31 May 2017 12:57:37 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56993) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dG6uV-0007pt-PQ for qemu-devel@nongnu.org; Wed, 31 May 2017 12:55:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dG6uR-000417-St for qemu-devel@nongnu.org; Wed, 31 May 2017 12:55:51 -0400 Received: from mailhub.sw.ru ([195.214.232.25]:27998 helo=relay.sw.ru) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dG6uR-000403-H5 for qemu-devel@nongnu.org; Wed, 31 May 2017 12:55:47 -0400 Received: from kvm.sw.ru (msk-vpn.virtuozzo.com [195.214.232.6]) by relay.sw.ru (8.13.4/8.13.4) with ESMTP id v4VGtfWm011112; Wed, 31 May 2017 19:55:42 +0300 (MSK) From: Vladimir Sementsov-Ogievskiy To: qemu-devel@nongnu.org Date: Wed, 31 May 2017 19:55:35 +0300 Message-Id: <20170531165541.47338-7-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20170531165541.47338-1-vsementsov@virtuozzo.com> References: <20170531165541.47338-1-vsementsov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: OpenBSD 3.x [fuzzy] X-Received-From: 195.214.232.25 Subject: [Qemu-devel] [PATCH 06/12] nbd/server: get rid of EAGAIN dead code X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: pbonzini@redhat.com, vsementsov@virtuozzo.com, den@openvz.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" For now nbd_read never returns EAGAIN. So, don't handle it. Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Eric Blake --- nbd/server.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/nbd/server.c b/nbd/server.c index dd6b241761..b0bb263596 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -997,11 +997,12 @@ static int nbd_co_send_reply(NBDRequestData *req, NBD= Reply *reply, int len) return rc; } =20 -/* Collect a client request. Return 0 if request looks valid, -EAGAIN - * to keep trying the collection, -EIO to drop connection right away, - * and any other negative value to report an error to the client - * (although the caller may still need to disconnect after reporting - * the error). */ +/* nbd_co_receive_request + * Collect a client request. Return 0 if request looks valid, -EIO to drop + * connection right away, and any other negative value to report an error = to + * the client (although the caller may still need to disconnect after repo= rting + * the error). + */ static int nbd_co_receive_request(NBDRequestData *req, NBDRequest *request) { NBDClient *client =3D req->client; @@ -1011,9 +1012,7 @@ static int nbd_co_receive_request(NBDRequestData *req= , NBDRequest *request) assert(client->recv_coroutine =3D=3D qemu_coroutine_self()); rc =3D nbd_receive_request(client->ioc, request); if (rc < 0) { - if (rc !=3D -EAGAIN) { - rc =3D -EIO; - } + rc =3D -EIO; goto out; } =20 @@ -1114,9 +1113,6 @@ static coroutine_fn void nbd_trip(void *opaque) =20 req =3D nbd_request_get(client); ret =3D nbd_co_receive_request(req, &request); - if (ret =3D=3D -EAGAIN) { - goto done; - } if (ret =3D=3D -EIO) { goto out; } --=20 2.11.1 From nobody Sun May 5 12:18:30 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1496249859562216.127767137153; Wed, 31 May 2017 09:57:39 -0700 (PDT) Received: from localhost ([::1]:32910 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dG6wE-0000an-0Z for importer@patchew.org; Wed, 31 May 2017 12:57:38 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56992) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dG6uV-0007ps-Pz for qemu-devel@nongnu.org; Wed, 31 May 2017 12:55:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dG6uR-00041M-Tj for qemu-devel@nongnu.org; Wed, 31 May 2017 12:55:51 -0400 Received: from mailhub.sw.ru ([195.214.232.25]:40435 helo=relay.sw.ru) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dG6uR-00040A-HO for qemu-devel@nongnu.org; Wed, 31 May 2017 12:55:47 -0400 Received: from kvm.sw.ru (msk-vpn.virtuozzo.com [195.214.232.6]) by relay.sw.ru (8.13.4/8.13.4) with ESMTP id v4VGtfWn011112; Wed, 31 May 2017 19:55:42 +0300 (MSK) From: Vladimir Sementsov-Ogievskiy To: qemu-devel@nongnu.org Date: Wed, 31 May 2017 19:55:36 +0300 Message-Id: <20170531165541.47338-8-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20170531165541.47338-1-vsementsov@virtuozzo.com> References: <20170531165541.47338-1-vsementsov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: OpenBSD 3.x [fuzzy] X-Received-From: 195.214.232.25 Subject: [Qemu-devel] [PATCH 07/12] nbd/server: refactor nbd_co_receive_request X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: pbonzini@redhat.com, vsementsov@virtuozzo.com, den@openvz.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Move function tail, about receiving next request out of the function. Error path is simplified and nbd_co_receive_request becomes more corresponding to its name. Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Eric Blake --- nbd/server.c | 41 +++++++++++++---------------------------- 1 file changed, 13 insertions(+), 28 deletions(-) diff --git a/nbd/server.c b/nbd/server.c index b0bb263596..8ac095d6bc 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -1006,14 +1006,11 @@ static int nbd_co_send_reply(NBDRequestData *req, N= BDReply *reply, int len) static int nbd_co_receive_request(NBDRequestData *req, NBDRequest *request) { NBDClient *client =3D req->client; - int rc; =20 g_assert(qemu_in_coroutine()); assert(client->recv_coroutine =3D=3D qemu_coroutine_self()); - rc =3D nbd_receive_request(client->ioc, request); - if (rc < 0) { - rc =3D -EIO; - goto out; + if (nbd_receive_request(client->ioc, request) < 0) { + return -EIO; } =20 TRACE("Decoding type"); @@ -1027,8 +1024,7 @@ static int nbd_co_receive_request(NBDRequestData *req= , NBDRequest *request) /* Special case: we're going to disconnect without a reply, * whether or not flags, from, or len are bogus */ TRACE("Request type is DISCONNECT"); - rc =3D -EIO; - goto out; + return -EIO; } =20 /* Check for sanity in the parameters, part 1. Defer as many @@ -1036,22 +1032,19 @@ static int nbd_co_receive_request(NBDRequestData *r= eq, NBDRequest *request) * payload, so we can try and keep the connection alive. */ if ((request->from + request->len) < request->from) { LOG("integer overflow detected, you're probably being attacked"); - rc =3D -EINVAL; - goto out; + return -EINVAL; } =20 if (request->type =3D=3D NBD_CMD_READ || request->type =3D=3D NBD_CMD_= WRITE) { if (request->len > NBD_MAX_BUFFER_SIZE) { LOG("len (%" PRIu32" ) is larger than max len (%u)", request->len, NBD_MAX_BUFFER_SIZE); - rc =3D -EINVAL; - goto out; + return -EINVAL; } =20 req->data =3D blk_try_blockalign(client->exp->blk, request->len); if (req->data =3D=3D NULL) { - rc =3D -ENOMEM; - goto out; + return -ENOMEM; } } if (request->type =3D=3D NBD_CMD_WRITE) { @@ -1059,8 +1052,7 @@ static int nbd_co_receive_request(NBDRequestData *req= , NBDRequest *request) =20 if (nbd_read(client->ioc, req->data, request->len, NULL) < 0) { LOG("reading from socket failed"); - rc =3D -EIO; - goto out; + return -EIO; } req->complete =3D true; } @@ -1070,28 +1062,19 @@ static int nbd_co_receive_request(NBDRequestData *r= eq, NBDRequest *request) LOG("operation past EOF; From: %" PRIu64 ", Len: %" PRIu32 ", Size: %" PRIu64, request->from, request->len, (uint64_t)client->exp->size); - rc =3D request->type =3D=3D NBD_CMD_WRITE ? -ENOSPC : -EINVAL; - goto out; + return request->type =3D=3D NBD_CMD_WRITE ? -ENOSPC : -EINVAL; } if (request->flags & ~(NBD_CMD_FLAG_FUA | NBD_CMD_FLAG_NO_HOLE)) { LOG("unsupported flags (got 0x%x)", request->flags); - rc =3D -EINVAL; - goto out; + return -EINVAL; } if (request->type !=3D NBD_CMD_WRITE_ZEROES && (request->flags & NBD_CMD_FLAG_NO_HOLE)) { LOG("unexpected flags (got 0x%x)", request->flags); - rc =3D -EINVAL; - goto out; + return -EINVAL; } =20 - rc =3D 0; - -out: - client->recv_coroutine =3D NULL; - nbd_client_receive_next_request(client); - - return rc; + return 0; } =20 /* Owns a reference to the NBDClient passed as opaque. */ @@ -1113,6 +1096,8 @@ static coroutine_fn void nbd_trip(void *opaque) =20 req =3D nbd_request_get(client); ret =3D nbd_co_receive_request(req, &request); + client->recv_coroutine =3D NULL; + nbd_client_receive_next_request(client); if (ret =3D=3D -EIO) { goto out; } --=20 2.11.1 From nobody Sun May 5 12:18:30 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 149625010977262.66334021577529; Wed, 31 May 2017 10:01:49 -0700 (PDT) Received: from localhost ([::1]:32935 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dG70G-0004U7-Cp for importer@patchew.org; Wed, 31 May 2017 13:01:48 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56994) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dG6uV-0007pu-PR for qemu-devel@nongnu.org; Wed, 31 May 2017 12:55:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dG6uS-00041W-0a for qemu-devel@nongnu.org; Wed, 31 May 2017 12:55:51 -0400 Received: from mailhub.sw.ru ([195.214.232.25]:16430 helo=relay.sw.ru) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dG6uR-000405-HK for qemu-devel@nongnu.org; Wed, 31 May 2017 12:55:47 -0400 Received: from kvm.sw.ru (msk-vpn.virtuozzo.com [195.214.232.6]) by relay.sw.ru (8.13.4/8.13.4) with ESMTP id v4VGtfWo011112; Wed, 31 May 2017 19:55:42 +0300 (MSK) From: Vladimir Sementsov-Ogievskiy To: qemu-devel@nongnu.org Date: Wed, 31 May 2017 19:55:37 +0300 Message-Id: <20170531165541.47338-9-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20170531165541.47338-1-vsementsov@virtuozzo.com> References: <20170531165541.47338-1-vsementsov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: OpenBSD 3.x [fuzzy] X-Received-From: 195.214.232.25 Subject: [Qemu-devel] [PATCH 08/12] nbd/server: remove NBDClientNewData X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: pbonzini@redhat.com, vsementsov@virtuozzo.com, den@openvz.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" "co" field of NBDClientNewData has never been used, all the way back to its declaration in commit 1a6245a5. So let's just use client pointer instead of extra structure. Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Eric Blake --- nbd/server.c | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/nbd/server.c b/nbd/server.c index 8ac095d6bc..d376563527 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -535,14 +535,8 @@ static int nbd_negotiate_options(NBDClient *client) } } =20 -typedef struct { - NBDClient *client; - Coroutine *co; -} NBDClientNewData; - -static coroutine_fn int nbd_negotiate(NBDClientNewData *data) +static coroutine_fn int nbd_negotiate(NBDClient *client) { - NBDClient *client =3D data->client; char buf[8 + 8 + 8 + 128]; int rc; const uint16_t myflags =3D (NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_TRIM | @@ -1268,16 +1262,15 @@ static void nbd_client_receive_next_request(NBDClie= nt *client) =20 static coroutine_fn void nbd_co_client_start(void *opaque) { - NBDClientNewData *data =3D opaque; - NBDClient *client =3D data->client; + NBDClient *client =3D opaque; NBDExport *exp =3D client->exp; =20 if (exp) { nbd_export_get(exp); } - if (nbd_negotiate(data)) { + if (nbd_negotiate(client)) { client_close(client); - goto out; + return; } qemu_co_mutex_init(&client->send_lock); =20 @@ -1286,9 +1279,6 @@ static coroutine_fn void nbd_co_client_start(void *op= aque) } =20 nbd_client_receive_next_request(client); - -out: - g_free(data); } =20 void nbd_client_new(NBDExport *exp, @@ -1298,7 +1288,7 @@ void nbd_client_new(NBDExport *exp, void (*close_fn)(NBDClient *)) { NBDClient *client; - NBDClientNewData *data =3D g_new(NBDClientNewData, 1); + Coroutine *co; =20 client =3D g_malloc0(sizeof(NBDClient)); client->refcount =3D 1; @@ -1314,7 +1304,6 @@ void nbd_client_new(NBDExport *exp, object_ref(OBJECT(client->ioc)); client->close =3D close_fn; =20 - data->client =3D client; - data->co =3D qemu_coroutine_create(nbd_co_client_start, data); - qemu_coroutine_enter(data->co); + co =3D qemu_coroutine_create(nbd_co_client_start, client); + qemu_coroutine_enter(co); } --=20 2.11.1 From nobody Sun May 5 12:18:30 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1496249985567609.474444279708; Wed, 31 May 2017 09:59:45 -0700 (PDT) Received: from localhost ([::1]:32916 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dG6yF-0002mM-VU for importer@patchew.org; Wed, 31 May 2017 12:59:44 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56995) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dG6uV-0007pv-PT for qemu-devel@nongnu.org; Wed, 31 May 2017 12:55:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dG6uR-00041R-VN for qemu-devel@nongnu.org; Wed, 31 May 2017 12:55:51 -0400 Received: from mailhub.sw.ru ([195.214.232.25]:27536 helo=relay.sw.ru) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dG6uR-00040C-Ia for qemu-devel@nongnu.org; Wed, 31 May 2017 12:55:47 -0400 Received: from kvm.sw.ru (msk-vpn.virtuozzo.com [195.214.232.6]) by relay.sw.ru (8.13.4/8.13.4) with ESMTP id v4VGtfWp011112; Wed, 31 May 2017 19:55:42 +0300 (MSK) From: Vladimir Sementsov-Ogievskiy To: qemu-devel@nongnu.org Date: Wed, 31 May 2017 19:55:38 +0300 Message-Id: <20170531165541.47338-10-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20170531165541.47338-1-vsementsov@virtuozzo.com> References: <20170531165541.47338-1-vsementsov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: OpenBSD 3.x [fuzzy] X-Received-From: 195.214.232.25 Subject: [Qemu-devel] [PATCH 09/12] nbd/server: nbd_negotiate: fix error path X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: pbonzini@redhat.com, vsementsov@virtuozzo.com, den@openvz.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Current code will return 0 on this nbd_write fail, as rc is 0 after successful nbd_negotiate_options. Fix this. Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Eric Blake --- nbd/server.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nbd/server.c b/nbd/server.c index d376563527..ec163ad829 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -607,7 +607,8 @@ static coroutine_fn int nbd_negotiate(NBDClient *client) stq_be_p(buf + 18, client->exp->size); stw_be_p(buf + 26, client->exp->nbdflags | myflags); len =3D client->no_zeroes ? 10 : sizeof(buf) - 18; - if (nbd_write(client->ioc, buf + 18, len, NULL) < 0) { + rc =3D nbd_write(client->ioc, buf + 18, len, NULL); + if (rc < 0) { LOG("write failed"); goto fail; } --=20 2.11.1 From nobody Sun May 5 12:18:30 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1496250267974734.3913215433724; Wed, 31 May 2017 10:04:27 -0700 (PDT) Received: from localhost ([::1]:32947 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dG72l-0006gS-Oj for importer@patchew.org; Wed, 31 May 2017 13:04:23 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57059) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dG6uX-0007qF-9Y for qemu-devel@nongnu.org; Wed, 31 May 2017 12:55:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dG6uS-000420-47 for qemu-devel@nongnu.org; Wed, 31 May 2017 12:55:53 -0400 Received: from mailhub.sw.ru ([195.214.232.25]:8996 helo=relay.sw.ru) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dG6uR-00040D-Oa for qemu-devel@nongnu.org; Wed, 31 May 2017 12:55:48 -0400 Received: from kvm.sw.ru (msk-vpn.virtuozzo.com [195.214.232.6]) by relay.sw.ru (8.13.4/8.13.4) with ESMTP id v4VGtfWq011112; Wed, 31 May 2017 19:55:42 +0300 (MSK) From: Vladimir Sementsov-Ogievskiy To: qemu-devel@nongnu.org Date: Wed, 31 May 2017 19:55:39 +0300 Message-Id: <20170531165541.47338-11-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20170531165541.47338-1-vsementsov@virtuozzo.com> References: <20170531165541.47338-1-vsementsov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: OpenBSD 3.x [fuzzy] X-Received-From: 195.214.232.25 Subject: [Qemu-devel] [PATCH 10/12] nbd/server: get rid of fail: return rc X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: pbonzini@redhat.com, vsementsov@virtuozzo.com, den@openvz.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" "goto fail" error handling scheme is not needed for just returning error code. Better is return it immediately. Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Eric Blake --- nbd/server.c | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/nbd/server.c b/nbd/server.c index ec163ad829..2f1c5b0a5b 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -265,7 +265,6 @@ static int nbd_negotiate_handle_list(NBDClient *client,= uint32_t length) =20 static int nbd_negotiate_handle_export_name(NBDClient *client, uint32_t le= ngth) { - int rc =3D -EINVAL; char name[NBD_MAX_NAME_SIZE + 1]; =20 /* Client sends: @@ -274,11 +273,11 @@ static int nbd_negotiate_handle_export_name(NBDClient= *client, uint32_t length) TRACE("Checking length"); if (length >=3D sizeof(name)) { LOG("Bad length received"); - goto fail; + return -EINVAL; } if (nbd_read(client->ioc, name, length, NULL) < 0) { LOG("read failed"); - goto fail; + return -EINVAL; } name[length] =3D '\0'; =20 @@ -287,14 +286,13 @@ static int nbd_negotiate_handle_export_name(NBDClient= *client, uint32_t length) client->exp =3D nbd_export_find(name); if (!client->exp) { LOG("export not found"); - goto fail; + return -EINVAL; } =20 QTAILQ_INSERT_TAIL(&client->exp->clients, client, next); nbd_export_get(client->exp); - rc =3D 0; -fail: - return rc; + + return 0; } =20 /* Handle NBD_OPT_STARTTLS. Return NULL to drop connection, or else the @@ -564,7 +562,6 @@ static coroutine_fn int nbd_negotiate(NBDClient *client) */ =20 qio_channel_set_blocking(client->ioc, false, NULL); - rc =3D -EINVAL; =20 TRACE("Beginning negotiation."); memset(buf, 0, sizeof(buf)); @@ -585,21 +582,21 @@ static coroutine_fn int nbd_negotiate(NBDClient *clie= nt) if (oldStyle) { if (client->tlscreds) { TRACE("TLS cannot be enabled with oldstyle protocol"); - goto fail; + return -EINVAL; } if (nbd_write(client->ioc, buf, sizeof(buf), NULL) < 0) { LOG("write failed"); - goto fail; + return -EINVAL; } } else { if (nbd_write(client->ioc, buf, 18, NULL) < 0) { LOG("write failed"); - goto fail; + return -EINVAL; } rc =3D nbd_negotiate_options(client); if (rc !=3D 0) { LOG("option negotiation failed"); - goto fail; + return rc; } =20 TRACE("advertising size %" PRIu64 " and flags %x", @@ -610,14 +607,13 @@ static coroutine_fn int nbd_negotiate(NBDClient *clie= nt) rc =3D nbd_write(client->ioc, buf + 18, len, NULL); if (rc < 0) { LOG("write failed"); - goto fail; + return rc; } } =20 TRACE("Negotiation succeeded."); - rc =3D 0; -fail: - return rc; + + return 0; } =20 static int nbd_receive_request(QIOChannel *ioc, NBDRequest *request) --=20 2.11.1 From nobody Sun May 5 12:18:30 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1496250109834811.8554333042273; Wed, 31 May 2017 10:01:49 -0700 (PDT) Received: from localhost ([::1]:32934 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dG70F-0004Tk-Ab for importer@patchew.org; Wed, 31 May 2017 13:01:47 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56996) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dG6uV-0007pw-Q1 for qemu-devel@nongnu.org; Wed, 31 May 2017 12:55:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dG6uS-00041g-32 for qemu-devel@nongnu.org; Wed, 31 May 2017 12:55:51 -0400 Received: from mailhub.sw.ru ([195.214.232.25]:35247 helo=relay.sw.ru) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dG6uR-000409-MA for qemu-devel@nongnu.org; Wed, 31 May 2017 12:55:47 -0400 Received: from kvm.sw.ru (msk-vpn.virtuozzo.com [195.214.232.6]) by relay.sw.ru (8.13.4/8.13.4) with ESMTP id v4VGtfWr011112; Wed, 31 May 2017 19:55:43 +0300 (MSK) From: Vladimir Sementsov-Ogievskiy To: qemu-devel@nongnu.org Date: Wed, 31 May 2017 19:55:40 +0300 Message-Id: <20170531165541.47338-12-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20170531165541.47338-1-vsementsov@virtuozzo.com> References: <20170531165541.47338-1-vsementsov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: OpenBSD 3.x [fuzzy] X-Received-From: 195.214.232.25 Subject: [Qemu-devel] [PATCH 11/12] nbd/server: rename rc to ret X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: pbonzini@redhat.com, vsementsov@virtuozzo.com, den@openvz.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" For consistency use 'ret' name for saving return code everywhere in the file. Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Eric Blake --- nbd/server.c | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/nbd/server.c b/nbd/server.c index 2f1c5b0a5b..209a3d4e1e 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -211,15 +211,15 @@ static int nbd_negotiate_send_rep_list(QIOChannel *io= c, NBDExport *exp) uint32_t len; const char *name =3D exp->name ? exp->name : ""; const char *desc =3D exp->description ? exp->description : ""; - int rc; + int ret; =20 TRACE("Advertising export name '%s' description '%s'", name, desc); name_len =3D strlen(name); desc_len =3D strlen(desc); len =3D name_len + desc_len + sizeof(len); - rc =3D nbd_negotiate_send_rep_len(ioc, NBD_REP_SERVER, NBD_OPT_LIST, l= en); - if (rc < 0) { - return rc; + ret =3D nbd_negotiate_send_rep_len(ioc, NBD_REP_SERVER, NBD_OPT_LIST, = len); + if (ret < 0) { + return ret; } =20 len =3D cpu_to_be32(name_len); @@ -536,7 +536,7 @@ static int nbd_negotiate_options(NBDClient *client) static coroutine_fn int nbd_negotiate(NBDClient *client) { char buf[8 + 8 + 8 + 128]; - int rc; + int ret; const uint16_t myflags =3D (NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_TRIM | NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA | NBD_FLAG_SEND_WRITE_ZEROES); @@ -593,10 +593,10 @@ static coroutine_fn int nbd_negotiate(NBDClient *clie= nt) LOG("write failed"); return -EINVAL; } - rc =3D nbd_negotiate_options(client); - if (rc !=3D 0) { + ret =3D nbd_negotiate_options(client); + if (ret !=3D 0) { LOG("option negotiation failed"); - return rc; + return ret; } =20 TRACE("advertising size %" PRIu64 " and flags %x", @@ -604,10 +604,10 @@ static coroutine_fn int nbd_negotiate(NBDClient *clie= nt) stq_be_p(buf + 18, client->exp->size); stw_be_p(buf + 26, client->exp->nbdflags | myflags); len =3D client->no_zeroes ? 10 : sizeof(buf) - 18; - rc =3D nbd_write(client->ioc, buf + 18, len, NULL); - if (rc < 0) { + ret =3D nbd_write(client->ioc, buf + 18, len, NULL); + if (ret < 0) { LOG("write failed"); - return rc; + return ret; } } =20 @@ -963,21 +963,21 @@ void nbd_export_close_all(void) static int nbd_co_send_reply(NBDRequestData *req, NBDReply *reply, int len) { NBDClient *client =3D req->client; - int rc; + int ret; =20 g_assert(qemu_in_coroutine()); qemu_co_mutex_lock(&client->send_lock); client->send_coroutine =3D qemu_coroutine_self(); =20 if (!len) { - rc =3D nbd_send_reply(client->ioc, reply); + ret =3D nbd_send_reply(client->ioc, reply); } else { qio_channel_set_cork(client->ioc, true); - rc =3D nbd_send_reply(client->ioc, reply); - if (rc =3D=3D 0) { - rc =3D nbd_write(client->ioc, req->data, len, NULL); - if (rc < 0) { - rc =3D -EIO; + ret =3D nbd_send_reply(client->ioc, reply); + if (ret =3D=3D 0) { + ret =3D nbd_write(client->ioc, req->data, len, NULL); + if (ret < 0) { + ret =3D -EIO; } } qio_channel_set_cork(client->ioc, false); @@ -985,7 +985,7 @@ static int nbd_co_send_reply(NBDRequestData *req, NBDRe= ply *reply, int len) =20 client->send_coroutine =3D NULL; qemu_co_mutex_unlock(&client->send_lock); - return rc; + return ret; } =20 /* nbd_co_receive_request --=20 2.11.1 From nobody Sun May 5 12:18:30 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1496250107886362.77961165370436; Wed, 31 May 2017 10:01:47 -0700 (PDT) Received: from localhost ([::1]:32932 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dG70D-0004Sd-At for importer@patchew.org; Wed, 31 May 2017 13:01:45 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57045) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dG6uX-0007q2-1s for qemu-devel@nongnu.org; Wed, 31 May 2017 12:55:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dG6uS-00041m-2m for qemu-devel@nongnu.org; Wed, 31 May 2017 12:55:53 -0400 Received: from mailhub.sw.ru ([195.214.232.25]:41104 helo=relay.sw.ru) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dG6uR-000406-Kq for qemu-devel@nongnu.org; Wed, 31 May 2017 12:55:47 -0400 Received: from kvm.sw.ru (msk-vpn.virtuozzo.com [195.214.232.6]) by relay.sw.ru (8.13.4/8.13.4) with ESMTP id v4VGtfWs011112; Wed, 31 May 2017 19:55:43 +0300 (MSK) From: Vladimir Sementsov-Ogievskiy To: qemu-devel@nongnu.org Date: Wed, 31 May 2017 19:55:41 +0300 Message-Id: <20170531165541.47338-13-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20170531165541.47338-1-vsementsov@virtuozzo.com> References: <20170531165541.47338-1-vsementsov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: OpenBSD 3.x [fuzzy] X-Received-From: 195.214.232.25 Subject: [Qemu-devel] [PATCH 12/12] nbd/server: refactor nbd_trip X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: pbonzini@redhat.com, vsementsov@virtuozzo.com, den@openvz.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" - do not use 'goto error_reply' outside a switch to jump into the middle of the switch's default case label - reduce code duplication Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Eric Blake --- nbd/server.c | 53 ++++++++++++++++++++--------------------------------- 1 file changed, 20 insertions(+), 33 deletions(-) diff --git a/nbd/server.c b/nbd/server.c index 209a3d4e1e..198eabe338 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -1078,6 +1078,7 @@ static coroutine_fn void nbd_trip(void *opaque) NBDReply reply; int ret; int flags; + int reply_data_len =3D 0; =20 TRACE("Reading request."); if (client->closing) { @@ -1090,7 +1091,7 @@ static coroutine_fn void nbd_trip(void *opaque) client->recv_coroutine =3D NULL; nbd_client_receive_next_request(client); if (ret =3D=3D -EIO) { - goto out; + goto disconnect; } =20 reply.handle =3D request.handle; @@ -1098,7 +1099,7 @@ static coroutine_fn void nbd_trip(void *opaque) =20 if (ret < 0) { reply.error =3D -ret; - goto error_reply; + goto reply; } =20 if (client->closing) { @@ -1119,7 +1120,7 @@ static coroutine_fn void nbd_trip(void *opaque) if (ret < 0) { LOG("flush failed"); reply.error =3D -ret; - goto error_reply; + break; } } =20 @@ -1128,12 +1129,12 @@ static coroutine_fn void nbd_trip(void *opaque) if (ret < 0) { LOG("reading from file failed"); reply.error =3D -ret; - goto error_reply; + break; } =20 + reply_data_len =3D request.len; TRACE("Read %" PRIu32" byte(s)", request.len); - if (nbd_co_send_reply(req, &reply, request.len) < 0) - goto out; + break; case NBD_CMD_WRITE: TRACE("Request type is WRITE"); @@ -1141,7 +1142,7 @@ static coroutine_fn void nbd_trip(void *opaque) if (exp->nbdflags & NBD_FLAG_READ_ONLY) { TRACE("Server is read-only, return error"); reply.error =3D EROFS; - goto error_reply; + break; } =20 TRACE("Writing to device"); @@ -1155,21 +1156,16 @@ static coroutine_fn void nbd_trip(void *opaque) if (ret < 0) { LOG("writing to file failed"); reply.error =3D -ret; - goto error_reply; } =20 - if (nbd_co_send_reply(req, &reply, 0) < 0) { - goto out; - } break; - case NBD_CMD_WRITE_ZEROES: TRACE("Request type is WRITE_ZEROES"); =20 if (exp->nbdflags & NBD_FLAG_READ_ONLY) { TRACE("Server is read-only, return error"); reply.error =3D EROFS; - goto error_reply; + break; } =20 TRACE("Writing to device"); @@ -1186,14 +1182,9 @@ static coroutine_fn void nbd_trip(void *opaque) if (ret < 0) { LOG("writing to file failed"); reply.error =3D -ret; - goto error_reply; } =20 - if (nbd_co_send_reply(req, &reply, 0) < 0) { - goto out; - } break; - case NBD_CMD_DISC: /* unreachable, thanks to special case in nbd_co_receive_request()= */ abort(); @@ -1206,9 +1197,7 @@ static coroutine_fn void nbd_trip(void *opaque) LOG("flush failed"); reply.error =3D -ret; } - if (nbd_co_send_reply(req, &reply, 0) < 0) { - goto out; - } + break; case NBD_CMD_TRIM: TRACE("Request type is TRIM"); @@ -1218,21 +1207,19 @@ static coroutine_fn void nbd_trip(void *opaque) LOG("discard failed"); reply.error =3D -ret; } - if (nbd_co_send_reply(req, &reply, 0) < 0) { - goto out; - } + break; default: LOG("invalid request type (%" PRIu32 ") received", request.type); reply.error =3D EINVAL; - error_reply: - /* We must disconnect after NBD_CMD_WRITE if we did not - * read the payload. - */ - if (nbd_co_send_reply(req, &reply, 0) < 0 || !req->complete) { - goto out; - } - break; + } + +reply: + /* We must disconnect after NBD_CMD_WRITE if we did not + * read the payload. + */ + if (nbd_co_send_reply(req, &reply, reply_data_len) < 0 || !req->comple= te) { + goto disconnect; } =20 TRACE("Request/Reply complete"); @@ -1242,7 +1229,7 @@ done: nbd_client_put(client); return; =20 -out: +disconnect: nbd_request_put(req); client_close(client); nbd_client_put(client); --=20 2.11.1