From nobody Tue May 7 02:07:57 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 1494928092490537.5294968446487; Tue, 16 May 2017 02:48:12 -0700 (PDT) Received: from localhost ([::1]:40954 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dAZ5O-0002pU-P9 for importer@patchew.org; Tue, 16 May 2017 05:48:10 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54406) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dAZ3I-0001VF-UU for qemu-devel@nongnu.org; Tue, 16 May 2017 05:46:01 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dAZ3F-0004wD-9T for qemu-devel@nongnu.org; Tue, 16 May 2017 05:46:00 -0400 Received: from mailhub.sw.ru ([195.214.232.25]:44396 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 1dAZ3E-0004u4-Rc for qemu-devel@nongnu.org; Tue, 16 May 2017 05:45:57 -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 v4G9jXBa020558; Tue, 16 May 2017 12:45:34 +0300 (MSK) From: Vladimir Sementsov-Ogievskiy To: qemu-devel@nongnu.org, qemu-block@nongnu.org Date: Tue, 16 May 2017 12:45:29 +0300 Message-Id: <20170516094533.6160-2-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20170516094533.6160-1-vsementsov@virtuozzo.com> References: <20170516094533.6160-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 1/5] nbd: strict nbd_wr_syncv 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: kwolf@redhat.com, pbonzini@redhat.com, den@openvz.org, vsementsov@virtuozzo.com, mreitz@redhat.com 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" nbd_wr_syncv is called either from coroutine or from client negotiation code, when socket is in blocking mode. So, -EAGAIN is impossible. Furthermore, EAGAIN is confusing, as, what to read/write again? With EAGAIN as a return code we don't know how much data is already read or written by the function, so in case of EAGAIN the whole communication is broken. Signed-off-by: Vladimir Sementsov-Ogievskiy --- nbd/common.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/nbd/common.c b/nbd/common.c index dccbb8e9de..4db45b3ede 100644 --- a/nbd/common.c +++ b/nbd/common.c @@ -20,6 +20,10 @@ #include "qapi/error.h" #include "nbd-internal.h" =20 +/* nbd_wr_syncv + * 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, @@ -42,11 +46,8 @@ ssize_t nbd_wr_syncv(QIOChannel *ioc, len =3D qio_channel_writev(ioc, local_iov, nlocal_iov, &local_= err); } if (len =3D=3D QIO_CHANNEL_ERR_BLOCK) { - if (qemu_in_coroutine()) { - qio_channel_yield(ioc, do_read ? G_IO_IN : G_IO_OUT); - } else { - return -EAGAIN; - } + assert(qemu_in_coroutine()); + qio_channel_yield(ioc, do_read ? G_IO_IN : G_IO_OUT); continue; } if (len < 0) { --=20 2.11.1 From nobody Tue May 7 02:07:57 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 1494928245574685.3005456142855; Tue, 16 May 2017 02:50:45 -0700 (PDT) Received: from localhost ([::1]:40968 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dAZ7s-00050D-4b for importer@patchew.org; Tue, 16 May 2017 05:50:44 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54454) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dAZ3M-0001WJ-4p for qemu-devel@nongnu.org; Tue, 16 May 2017 05:46:06 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dAZ3F-0004wO-Me for qemu-devel@nongnu.org; Tue, 16 May 2017 05:46:04 -0400 Received: from mailhub.sw.ru ([195.214.232.25]:10562 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 1dAZ3E-0004ui-Vn for qemu-devel@nongnu.org; Tue, 16 May 2017 05:45:57 -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 v4G9jXBb020558; Tue, 16 May 2017 12:45:34 +0300 (MSK) From: Vladimir Sementsov-Ogievskiy To: qemu-devel@nongnu.org, qemu-block@nongnu.org Date: Tue, 16 May 2017 12:45:30 +0300 Message-Id: <20170516094533.6160-3-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20170516094533.6160-1-vsementsov@virtuozzo.com> References: <20170516094533.6160-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 2/5] nbd: read_sync and friends: return 0 on success 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: kwolf@redhat.com, pbonzini@redhat.com, den@openvz.org, vsementsov@virtuozzo.com, mreitz@redhat.com 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 read_sync, drop_sync, write_sync, and also nbd_negotiate_write, nbd_negotiate_read, nbd_negotiate_drop_sync returns number of processed bytes. But what this number can be, except requested number of bytes? Actually, underlying nbd_wr_syncv function returns a value >=3D 0 and !=3D requested_bytes only on eof on read operation. So, firstly, it is impossible on write (let's add an assert) and on read it actually means, that communication is broken (except nbd_receive_reply, see below). Most of callers operate like this: if (func(..., size) !=3D size) { /* error path */ } , i.e.: 1. They are not interested in partial success 2. Extra duplications in code (especially bad are duplications of magic numbers) 3. User doesn't see actual error message, as return code is lost. (this patch doesn't fix this point, but it makes fixing easier) Several callers handles ret >=3D 0 and !=3D requested-size separately, by just returning EINVAL in this case. This patch makes read_sync and friends return EINVAL in this case, so final behavior is the same. And only one caller - nbd_receive_reply() does something not so obvious. It returns EINVAL for ret > 0 and !=3D requested-size, like previous group, but for ret =3D=3D 0 it returns 0. The only caller of nbd_receive_reply() - nbd_read_reply_entry() handles ret =3D=3D 0 in the same way as ret < 0, so for now it doesn't matter. However, in following commits error path handling will be improved and we'll need to distinguish success from fail in this case too. So, this patch adds separate helper for this case - read_sync_eof. Signed-off-by: Vladimir Sementsov-Ogievskiy --- nbd/client.c | 63 ++++++++++++++++------------------------ nbd/nbd-internal.h | 34 +++++++++++++++++++--- nbd/server.c | 84 +++++++++++++++++++++-----------------------------= ---- 3 files changed, 88 insertions(+), 93 deletions(-) diff --git a/nbd/client.c b/nbd/client.c index a58fb02cb4..6b74a628f1 100644 --- a/nbd/client.c +++ b/nbd/client.c @@ -86,9 +86,9 @@ static QTAILQ_HEAD(, NBDExport) exports =3D QTAILQ_HEAD_I= NITIALIZER(exports); =20 */ =20 -/* Discard length bytes from channel. Return -errno on failure, or - * the amount of bytes consumed. */ -static ssize_t drop_sync(QIOChannel *ioc, size_t size) +/* Discard length bytes from channel. Return -errno on failure and 0 on + * success*/ +static int drop_sync(QIOChannel *ioc, size_t size) { ssize_t ret =3D 0; char small[1024]; @@ -96,14 +96,13 @@ static ssize_t drop_sync(QIOChannel *ioc, size_t size) =20 buffer =3D sizeof(small) >=3D size ? small : g_malloc(MIN(65536, size)= ); while (size > 0) { - ssize_t count =3D read_sync(ioc, buffer, MIN(65536, size)); + ssize_t count =3D MIN(65536, size); + ret =3D read_sync(ioc, buffer, MIN(65536, size)); =20 - if (count <=3D 0) { + if (ret < 0) { goto cleanup; } - assert(count <=3D size); size -=3D count; - ret +=3D count; } =20 cleanup: @@ -136,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)) !=3D sizeof(req)) { + if (write_sync(ioc, &req, sizeof(req)) < 0) { error_setg(errp, "Failed to send option request header"); return -1; } =20 - if (len && write_sync(ioc, (char *) data, len) !=3D len) { + if (len && write_sync(ioc, (char *) data, len) < 0) { error_setg(errp, "Failed to send option request data"); return -1; } @@ -170,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)) !=3D sizeof(*reply)) { + if (read_sync(ioc, reply, sizeof(*reply)) < 0) { error_setg(errp, "failed to read option reply"); nbd_send_opt_abort(ioc); return -1; @@ -219,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) !=3D reply->length) { + if (read_sync(ioc, msg, reply->length) < 0) { error_setg(errp, "failed to read option error message"); goto cleanup; } @@ -321,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)) !=3D sizeof(namelen)) { + if (read_sync(ioc, &namelen, sizeof(namelen)) < 0) { error_setg(errp, "failed to read option name length"); nbd_send_opt_abort(ioc); return -1; @@ -334,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) !=3D len) { + if (drop_sync(ioc, len) < 0) { error_setg(errp, "failed to skip export name with wrong length= "); nbd_send_opt_abort(ioc); return -1; @@ -343,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) !=3D namelen) { + if (read_sync(ioc, name, namelen) < 0) { error_setg(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) !=3D len) { + if (drop_sync(ioc, len) < 0) { error_setg(errp, "failed to read export description"); nbd_send_opt_abort(ioc); return -1; @@ -477,7 +476,7 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *= name, uint16_t *flags, goto fail; } =20 - if (read_sync(ioc, buf, 8) !=3D 8) { + if (read_sync(ioc, buf, 8) < 0) { error_setg(errp, "Failed to read data"); goto fail; } @@ -503,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)) !=3D sizeof(magic)) { + if (read_sync(ioc, &magic, sizeof(magic)) < 0) { error_setg(errp, "Failed to read magic"); goto fail; } @@ -515,8 +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)) !=3D - sizeof(globalflags)) { + if (read_sync(ioc, &globalflags, sizeof(globalflags)) < 0) { error_setg(errp, "Failed to read server flags"); goto fail; } @@ -534,8 +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)) !=3D - sizeof(clientflags)) { + if (write_sync(ioc, &clientflags, sizeof(clientflags)) < 0) { error_setg(errp, "Failed to send clientflags field"); goto fail; } @@ -573,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)) !=3D sizeof(s)) { + if (read_sync(ioc, &s, sizeof(s)) < 0) { error_setg(errp, "Failed to read export length"); goto fail; } *size =3D be64_to_cpu(s); =20 - if (read_sync(ioc, flags, sizeof(*flags)) !=3D sizeof(*flags)) { + if (read_sync(ioc, flags, sizeof(*flags)) < 0) { error_setg(errp, "Failed to read export flags"); goto fail; } @@ -596,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)) !=3D sizeof(s)) { + if (read_sync(ioc, &s, sizeof(s)) < 0) { error_setg(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)) !=3D sizeof(oldfla= gs)) { + if (read_sync(ioc, &oldflags, sizeof(oldflags)) < 0) { error_setg(errp, "Failed to read export flags"); goto fail; } @@ -619,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) !=3D 124) { + if (zeroes && drop_sync(ioc, 124) < 0) { error_setg(errp, "Failed to read reserved block"); goto fail; } @@ -744,7 +741,6 @@ int nbd_disconnect(int fd) ssize_t nbd_send_request(QIOChannel *ioc, NBDRequest *request) { uint8_t buf[NBD_REQUEST_SIZE]; - ssize_t ret; =20 TRACE("Sending request to server: " "{ .from =3D %" PRIu64", .len =3D %" PRIu32 ", .handle =3D %" PR= Iu64 @@ -759,16 +755,7 @@ ssize_t nbd_send_request(QIOChannel *ioc, NBDRequest *= request) stq_be_p(buf + 16, request->from); stl_be_p(buf + 24, request->len); =20 - ret =3D write_sync(ioc, buf, sizeof(buf)); - if (ret < 0) { - return ret; - } - - if (ret !=3D sizeof(buf)) { - LOG("writing to socket failed"); - return -EINVAL; - } - return 0; + return write_sync(ioc, buf, sizeof(buf)); } =20 ssize_t nbd_receive_reply(QIOChannel *ioc, NBDReply *reply) @@ -777,7 +764,7 @@ ssize_t nbd_receive_reply(QIOChannel *ioc, NBDReply *re= ply) uint32_t magic; ssize_t ret; =20 - ret =3D read_sync(ioc, buf, sizeof(buf)); + ret =3D read_sync_eof(ioc, buf, sizeof(buf)); if (ret <=3D 0) { return ret; } diff --git a/nbd/nbd-internal.h b/nbd/nbd-internal.h index f43d990a05..e6bbc7c4b4 100644 --- a/nbd/nbd-internal.h +++ b/nbd/nbd-internal.h @@ -94,7 +94,13 @@ #define NBD_ENOSPC 28 #define NBD_ESHUTDOWN 108 =20 -static inline ssize_t read_sync(QIOChannel *ioc, void *buffer, size_t size) +/* read_sync_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 + * iteratively. + */ +static inline ssize_t read_sync_eof(QIOChannel *ioc, void *buffer, size_t = size) { struct iovec iov =3D { .iov_base =3D buffer, .iov_len =3D size }; /* Sockets are kept in blocking mode in the negotiation phase. After @@ -105,12 +111,32 @@ static inline ssize_t read_sync(QIOChannel *ioc, void= *buffer, size_t size) return nbd_wr_syncv(ioc, &iov, 1, size, true); } =20 -static inline ssize_t write_sync(QIOChannel *ioc, const void *buffer, - size_t size) +/* read_sync + * Reads @size bytes from @ioc. Returns 0 on success. + */ +static inline int read_sync(QIOChannel *ioc, void *buffer, size_t size) +{ + ssize_t ret =3D read_sync_eof(ioc, buffer, size); + + if (ret >=3D 0 && ret !=3D size) { + ret =3D -EINVAL; + } + + return ret < 0 ? ret : 0; +} + +/* write_sync + * Writes @size bytes to @ioc. Returns 0 on success. + */ +static inline int write_sync(QIOChannel *ioc, const void *buffer, size_t s= ize) { struct iovec iov =3D { .iov_base =3D (void *) buffer, .iov_len =3D siz= e }; =20 - return nbd_wr_syncv(ioc, &iov, 1, size, false); + ssize_t ret =3D nbd_wr_syncv(ioc, &iov, 1, size, false); + + assert(ret < 0 || ret =3D=3D size); + + return ret < 0 ? ret : 0; } =20 struct NBDTLSHandshakeData { diff --git a/nbd/server.c b/nbd/server.c index 924a1fe2db..1e1096c762 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -112,7 +112,7 @@ static gboolean nbd_negotiate_continue(QIOChannel *ioc, return TRUE; } =20 -static ssize_t nbd_negotiate_read(QIOChannel *ioc, void *buffer, size_t si= ze) +static int nbd_negotiate_read(QIOChannel *ioc, void *buffer, size_t size) { ssize_t ret; guint watch; @@ -130,8 +130,7 @@ static ssize_t nbd_negotiate_read(QIOChannel *ioc, void= *buffer, size_t size) =20 } =20 -static ssize_t nbd_negotiate_write(QIOChannel *ioc, const void *buffer, - size_t size) +static int nbd_negotiate_write(QIOChannel *ioc, const void *buffer, size_t= size) { ssize_t ret; guint watch; @@ -148,24 +147,24 @@ static ssize_t nbd_negotiate_write(QIOChannel *ioc, c= onst void *buffer, return ret; } =20 -static ssize_t nbd_negotiate_drop_sync(QIOChannel *ioc, size_t size) +static int nbd_negotiate_drop_sync(QIOChannel *ioc, size_t size) { - ssize_t ret, dropped =3D size; + ssize_t ret; uint8_t *buffer =3D g_malloc(MIN(65536, size)); =20 while (size > 0) { - ret =3D nbd_negotiate_read(ioc, buffer, MIN(65536, size)); + size_t count =3D MIN(65536, size); + ret =3D nbd_negotiate_read(ioc, buffer, count); if (ret < 0) { g_free(buffer); return ret; } =20 - assert(ret <=3D size); - size -=3D ret; + size -=3D count; } =20 g_free(buffer); - return dropped; + return 0; } =20 /* Basic flow for negotiation @@ -206,22 +205,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)) !=3D sizeof(magic)= ) { + if (nbd_negotiate_write(ioc, &magic, sizeof(magic)) < 0) { LOG("write failed (rep magic)"); return -EINVAL; } opt =3D cpu_to_be32(opt); - if (nbd_negotiate_write(ioc, &opt, sizeof(opt)) !=3D sizeof(opt)) { + if (nbd_negotiate_write(ioc, &opt, sizeof(opt)) < 0) { LOG("write failed (rep opt)"); return -EINVAL; } type =3D cpu_to_be32(type); - if (nbd_negotiate_write(ioc, &type, sizeof(type)) !=3D sizeof(type)) { + if (nbd_negotiate_write(ioc, &type, sizeof(type)) < 0) { LOG("write failed (rep type)"); return -EINVAL; } len =3D cpu_to_be32(len); - if (nbd_negotiate_write(ioc, &len, sizeof(len)) !=3D sizeof(len)) { + if (nbd_negotiate_write(ioc, &len, sizeof(len)) < 0) { LOG("write failed (rep data length)"); return -EINVAL; } @@ -256,7 +255,7 @@ nbd_negotiate_send_rep_err(QIOChannel *ioc, uint32_t ty= pe, if (ret < 0) { goto out; } - if (nbd_negotiate_write(ioc, msg, len) !=3D len) { + if (nbd_negotiate_write(ioc, msg, len) < 0) { LOG("write failed (error message)"); ret =3D -EIO; } else { @@ -287,15 +286,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)) !=3D sizeof(len)) { + if (nbd_negotiate_write(ioc, &len, sizeof(len)) < 0) { LOG("write failed (name length)"); return -EINVAL; } - if (nbd_negotiate_write(ioc, name, name_len) !=3D name_len) { + if (nbd_negotiate_write(ioc, name, name_len) < 0) { LOG("write failed (name buffer)"); return -EINVAL; } - if (nbd_negotiate_write(ioc, desc, desc_len) !=3D desc_len) { + if (nbd_negotiate_write(ioc, desc, desc_len) < 0) { LOG("write failed (description buffer)"); return -EINVAL; } @@ -309,7 +308,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) !=3D length) { + if (nbd_negotiate_drop_sync(client->ioc, length) < 0) { return -EIO; } return nbd_negotiate_send_rep_err(client->ioc, @@ -340,7 +339,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) !=3D length) { + if (nbd_negotiate_read(client->ioc, name, length) < 0) { LOG("read failed"); goto fail; } @@ -373,7 +372,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) !=3D length) { + if (nbd_negotiate_drop_sync(ioc, length) < 0) { return NULL; } nbd_negotiate_send_rep_err(ioc, NBD_REP_ERR_INVALID, NBD_OPT_START= TLS, @@ -437,8 +436,7 @@ static int nbd_negotiate_options(NBDClient *client) ... Rest of request */ =20 - if (nbd_negotiate_read(client->ioc, &flags, sizeof(flags)) !=3D - sizeof(flags)) { + if (nbd_negotiate_read(client->ioc, &flags, sizeof(flags)) < 0) { LOG("read failed"); return -EIO; } @@ -464,8 +462,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)) !=3D - sizeof(magic)) { + if (nbd_negotiate_read(client->ioc, &magic, sizeof(magic)) < 0) { LOG("read failed"); return -EINVAL; } @@ -476,14 +473,14 @@ static int nbd_negotiate_options(NBDClient *client) } =20 if (nbd_negotiate_read(client->ioc, &clientflags, - sizeof(clientflags)) !=3D sizeof(clientflag= s)) { + sizeof(clientflags)) < 0) + { LOG("read failed"); return -EINVAL; } clientflags =3D be32_to_cpu(clientflags); =20 - if (nbd_negotiate_read(client->ioc, &length, sizeof(length)) !=3D - sizeof(length)) { + if (nbd_negotiate_read(client->ioc, &length, sizeof(length)) < 0) { LOG("read failed"); return -EINVAL; } @@ -513,7 +510,7 @@ static int nbd_negotiate_options(NBDClient *client) return -EINVAL; =20 default: - if (nbd_negotiate_drop_sync(client->ioc, length) !=3D leng= th) { + if (nbd_negotiate_drop_sync(client->ioc, length) < 0) { return -EIO; } ret =3D nbd_negotiate_send_rep_err(client->ioc, @@ -551,7 +548,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) !=3D leng= th) { + if (nbd_negotiate_drop_sync(client->ioc, length) < 0) { return -EIO; } if (client->tlscreds) { @@ -570,7 +567,7 @@ static int nbd_negotiate_options(NBDClient *client) } break; default: - if (nbd_negotiate_drop_sync(client->ioc, length) !=3D leng= th) { + if (nbd_negotiate_drop_sync(client->ioc, length) < 0) { return -EIO; } ret =3D nbd_negotiate_send_rep_err(client->ioc, @@ -659,12 +656,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)) !=3D sizeof= (buf)) { + if (nbd_negotiate_write(client->ioc, buf, sizeof(buf)) < 0) { LOG("write failed"); goto fail; } } else { - if (nbd_negotiate_write(client->ioc, buf, 18) !=3D 18) { + if (nbd_negotiate_write(client->ioc, buf, 18) < 0) { LOG("write failed"); goto fail; } @@ -679,7 +676,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) !=3D len) { + if (nbd_negotiate_write(client->ioc, buf + 18, len) < 0) { LOG("write failed"); goto fail; } @@ -702,11 +699,6 @@ static ssize_t nbd_receive_request(QIOChannel *ioc, NB= DRequest *request) return ret; } =20 - if (ret !=3D sizeof(buf)) { - LOG("read failed"); - return -EINVAL; - } - /* Request [ 0 .. 3] magic (NBD_REQUEST_MAGIC) [ 4 .. 5] flags (NBD_CMD_FLAG_FUA, ...) @@ -737,7 +729,6 @@ static ssize_t nbd_receive_request(QIOChannel *ioc, NBD= Request *request) static ssize_t nbd_send_reply(QIOChannel *ioc, NBDReply *reply) { uint8_t buf[NBD_REPLY_SIZE]; - ssize_t ret; =20 reply->error =3D system_errno_to_nbd_errno(reply->error); =20 @@ -754,16 +745,7 @@ static ssize_t nbd_send_reply(QIOChannel *ioc, NBDRepl= y *reply) stl_be_p(buf + 4, reply->error); stq_be_p(buf + 8, reply->handle); =20 - ret =3D write_sync(ioc, buf, sizeof(buf)); - if (ret < 0) { - return ret; - } - - if (ret !=3D sizeof(buf)) { - LOG("writing to socket failed"); - return -EINVAL; - } - return 0; + return write_sync(ioc, buf, sizeof(buf)); } =20 #define MAX_NBD_REQUESTS 16 @@ -1067,7 +1049,7 @@ static ssize_t nbd_co_send_reply(NBDRequestData *req,= NBDReply *reply, rc =3D nbd_send_reply(client->ioc, reply); if (rc >=3D 0) { ret =3D write_sync(client->ioc, req->data, len); - if (ret !=3D len) { + if (ret < 0) { rc =3D -EIO; } } @@ -1141,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) !=3D request->= len) { + if (read_sync(client->ioc, req->data, request->len) < 0) { LOG("reading from socket failed"); rc =3D -EIO; goto out; --=20 2.11.1 From nobody Tue May 7 02:07:57 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 14949280890251020.4765427381747; Tue, 16 May 2017 02:48:09 -0700 (PDT) Received: from localhost ([::1]:40952 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dAZ5I-0002kY-4C for importer@patchew.org; Tue, 16 May 2017 05:48:04 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54426) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dAZ3J-0001VM-Mx for qemu-devel@nongnu.org; Tue, 16 May 2017 05:46:02 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dAZ3I-0004xJ-Rn for qemu-devel@nongnu.org; Tue, 16 May 2017 05:46:01 -0400 Received: from mailhub.sw.ru ([195.214.232.25]:14468 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 1dAZ3I-0004vf-EU for qemu-devel@nongnu.org; Tue, 16 May 2017 05:46:00 -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 v4G9jXBc020558; Tue, 16 May 2017 12:45:34 +0300 (MSK) From: Vladimir Sementsov-Ogievskiy To: qemu-devel@nongnu.org, qemu-block@nongnu.org Date: Tue, 16 May 2017 12:45:31 +0300 Message-Id: <20170516094533.6160-4-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20170516094533.6160-1-vsementsov@virtuozzo.com> References: <20170516094533.6160-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 3/5] nbd: add errp parameter to nbd_wr_syncv() 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: kwolf@redhat.com, pbonzini@redhat.com, den@openvz.org, vsementsov@virtuozzo.com, mreitz@redhat.com 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" Will be used in following patch to provide actual error message in some cases. Signed-off-by: Vladimir Sementsov-Ogievskiy --- block/nbd-client.c | 4 ++-- include/block/nbd.h | 3 ++- nbd/common.c | 12 +++++------- nbd/nbd-internal.h | 4 ++-- 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/block/nbd-client.c b/block/nbd-client.c index 1e2952fdae..538d95e031 100644 --- a/block/nbd-client.c +++ b/block/nbd-client.c @@ -136,7 +136,7 @@ static int nbd_co_send_request(BlockDriverState *bs, 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); + false, NULL); if (ret !=3D request->len) { rc =3D -EIO; } @@ -165,7 +165,7 @@ static void nbd_co_receive_reply(NBDClientSession *s, } else { if (qiov && reply->error =3D=3D 0) { ret =3D nbd_wr_syncv(s->ioc, qiov->iov, qiov->niov, request->l= en, - true); + true, NULL); if (ret !=3D request->len) { reply->error =3D EIO; } diff --git a/include/block/nbd.h b/include/block/nbd.h index 3e373f0498..a96fb5fceb 100644 --- a/include/block/nbd.h +++ b/include/block/nbd.h @@ -127,7 +127,8 @@ ssize_t nbd_wr_syncv(QIOChannel *ioc, struct iovec *iov, size_t niov, size_t length, - bool do_read); + 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/common.c b/nbd/common.c index 4db45b3ede..bd81637ab9 100644 --- a/nbd/common.c +++ b/nbd/common.c @@ -28,10 +28,10 @@ ssize_t nbd_wr_syncv(QIOChannel *ioc, struct iovec *iov, size_t niov, size_t length, - bool do_read) + bool do_read, + Error **errp) { ssize_t done =3D 0; - Error *local_err =3D NULL; struct iovec *local_iov =3D g_new(struct iovec, niov); struct iovec *local_iov_head =3D local_iov; unsigned int nlocal_iov =3D niov; @@ -41,19 +41,17 @@ ssize_t nbd_wr_syncv(QIOChannel *ioc, while (nlocal_iov > 0) { ssize_t len; if (do_read) { - len =3D qio_channel_readv(ioc, local_iov, nlocal_iov, &local_e= rr); + len =3D qio_channel_readv(ioc, local_iov, nlocal_iov, errp); } else { - len =3D qio_channel_writev(ioc, local_iov, nlocal_iov, &local_= err); + len =3D qio_channel_writev(ioc, local_iov, nlocal_iov, errp); } if (len =3D=3D QIO_CHANNEL_ERR_BLOCK) { + /* errp should not be set */ assert(qemu_in_coroutine()); qio_channel_yield(ioc, do_read ? G_IO_IN : G_IO_OUT); continue; } if (len < 0) { - TRACE("I/O error: %s", error_get_pretty(local_err)); - error_free(local_err); - /* XXX handle Error objects */ done =3D -EIO; goto cleanup; } diff --git a/nbd/nbd-internal.h b/nbd/nbd-internal.h index e6bbc7c4b4..1d479fe135 100644 --- a/nbd/nbd-internal.h +++ b/nbd/nbd-internal.h @@ -108,7 +108,7 @@ static inline ssize_t read_sync_eof(QIOChannel *ioc, vo= id *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); + return nbd_wr_syncv(ioc, &iov, 1, size, true, NULL); } =20 /* read_sync @@ -132,7 +132,7 @@ static inline int write_sync(QIOChannel *ioc, const voi= d *buffer, size_t size) { 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); + ssize_t ret =3D nbd_wr_syncv(ioc, &iov, 1, size, false, NULL); =20 assert(ret < 0 || ret =3D=3D size); =20 --=20 2.11.1 From nobody Tue May 7 02:07:57 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 1494928230376439.4802409646601; Tue, 16 May 2017 02:50:30 -0700 (PDT) Received: from localhost ([::1]:40964 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dAZ7d-0004oQ-3B for importer@patchew.org; Tue, 16 May 2017 05:50:29 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54409) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dAZ3I-0001VH-VA for qemu-devel@nongnu.org; Tue, 16 May 2017 05:46:02 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dAZ3F-0004w2-73 for qemu-devel@nongnu.org; Tue, 16 May 2017 05:46:00 -0400 Received: from mailhub.sw.ru ([195.214.232.25]:37408 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 1dAZ3E-0004ud-CX for qemu-devel@nongnu.org; Tue, 16 May 2017 05:45:57 -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 v4G9jXBd020558; Tue, 16 May 2017 12:45:35 +0300 (MSK) From: Vladimir Sementsov-Ogievskiy To: qemu-devel@nongnu.org, qemu-block@nongnu.org Date: Tue, 16 May 2017 12:45:32 +0300 Message-Id: <20170516094533.6160-5-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20170516094533.6160-1-vsementsov@virtuozzo.com> References: <20170516094533.6160-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 4/5] nbd: add errp to read_sync, write_sync and drop_sync 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: kwolf@redhat.com, pbonzini@redhat.com, den@openvz.org, vsementsov@virtuozzo.com, mreitz@redhat.com 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" There a lot of calls of these functions, which already have errp, which they are filling themselves. On the other hand, nbd_wr_syncv has errp parameter too, so it would be great to connect them. Signed-off-by: Vladimir Sementsov-Ogievskiy --- nbd/client.c | 76 +++++++++++++++++++++++++++-----------------------= ---- nbd/nbd-internal.h | 16 +++++++----- nbd/server.c | 12 ++++----- 3 files changed, 54 insertions(+), 50 deletions(-) diff --git a/nbd/client.c b/nbd/client.c index 6b74a628f1..f102375504 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) +static int drop_sync(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) 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)); + ret =3D read_sync(ioc, buffer, MIN(65536, size), errp); =20 if (ret < 0) { goto cleanup; @@ -135,13 +135,13 @@ 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)) < 0) { - error_setg(errp, "Failed to send option request header"); + if (write_sync(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) < 0) { - error_setg(errp, "Failed to send option request data"); + if (len && write_sync(ioc, (char *) data, len, errp) < 0) { + error_prepend(errp, "Failed to send option request data"); return -1; } =20 @@ -169,8 +169,8 @@ 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)) < 0) { - error_setg(errp, "failed to read option reply"); + if (read_sync(ioc, reply, sizeof(*reply), errp) < 0) { + error_prepend(errp, "failed to read option reply"); nbd_send_opt_abort(ioc); return -1; } @@ -218,8 +218,8 @@ 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) < 0) { - error_setg(errp, "failed to read option error message"); + if (read_sync(ioc, msg, reply->length, errp) < 0) { + error_prepend(errp, "failed to read option error message"); goto cleanup; } msg[reply->length] =3D '\0'; @@ -320,8 +320,8 @@ 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)) < 0) { - error_setg(errp, "failed to read option name length"); + if (read_sync(ioc, &namelen, sizeof(namelen), errp) < 0) { + error_prepend(errp, "failed to read option name length"); nbd_send_opt_abort(ioc); return -1; } @@ -333,8 +333,8 @@ static int nbd_receive_list(QIOChannel *ioc, const char= *want, bool *match, return -1; } if (namelen !=3D strlen(want)) { - if (drop_sync(ioc, len) < 0) { - error_setg(errp, "failed to skip export name with wrong length= "); + if (drop_sync(ioc, len, errp) < 0) { + error_prepend(errp, "failed to skip export name with wrong len= gth"); nbd_send_opt_abort(ioc); return -1; } @@ -342,15 +342,15 @@ static int nbd_receive_list(QIOChannel *ioc, const ch= ar *want, bool *match, } =20 assert(namelen < sizeof(name)); - if (read_sync(ioc, name, namelen) < 0) { - error_setg(errp, "failed to read export name"); + if (read_sync(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) < 0) { - error_setg(errp, "failed to read export description"); + if (drop_sync(ioc, len, errp) < 0) { + error_prepend(errp, "failed to read export description"); nbd_send_opt_abort(ioc); return -1; } @@ -476,8 +476,8 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *= name, uint16_t *flags, goto fail; } =20 - if (read_sync(ioc, buf, 8) < 0) { - error_setg(errp, "Failed to read data"); + if (read_sync(ioc, buf, 8, errp) < 0) { + error_prepend(errp, "Failed to read data"); goto fail; } =20 @@ -502,8 +502,8 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *= name, uint16_t *flags, goto fail; } =20 - if (read_sync(ioc, &magic, sizeof(magic)) < 0) { - error_setg(errp, "Failed to read magic"); + if (read_sync(ioc, &magic, sizeof(magic), errp) < 0) { + error_prepend(errp, "Failed to read magic"); goto fail; } magic =3D be64_to_cpu(magic); @@ -514,8 +514,8 @@ 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)) < 0) { - error_setg(errp, "Failed to read server flags"); + if (read_sync(ioc, &globalflags, sizeof(globalflags), errp) < 0) { + error_prepend(errp, "Failed to read server flags"); goto fail; } globalflags =3D be16_to_cpu(globalflags); @@ -532,8 +532,8 @@ 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)) < 0) { - error_setg(errp, "Failed to send clientflags field"); + if (write_sync(ioc, &clientflags, sizeof(clientflags), errp) < 0) { + error_prepend(errp, "Failed to send clientflags field"); goto fail; } if (tlscreds) { @@ -570,14 +570,14 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char= *name, uint16_t *flags, } =20 /* Read the response */ - if (read_sync(ioc, &s, sizeof(s)) < 0) { - error_setg(errp, "Failed to read export length"); + if (read_sync(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)) < 0) { - error_setg(errp, "Failed to read export flags"); + if (read_sync(ioc, flags, sizeof(*flags), errp) < 0) { + error_prepend(errp, "Failed to read export flags"); goto fail; } be16_to_cpus(flags); @@ -593,15 +593,15 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char= *name, uint16_t *flags, goto fail; } =20 - if (read_sync(ioc, &s, sizeof(s)) < 0) { - error_setg(errp, "Failed to read export length"); + if (read_sync(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)) < 0) { - error_setg(errp, "Failed to read export flags"); + if (read_sync(ioc, &oldflags, sizeof(oldflags), errp) < 0) { + error_prepend(errp, "Failed to read export flags"); goto fail; } be32_to_cpus(&oldflags); @@ -616,8 +616,8 @@ 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) < 0) { - error_setg(errp, "Failed to read reserved block"); + if (zeroes && drop_sync(ioc, 124, errp) < 0) { + error_prepend(errp, "Failed to read reserved block"); goto fail; } rc =3D 0; @@ -755,7 +755,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)); + return write_sync(ioc, buf, sizeof(buf), NULL); } =20 ssize_t nbd_receive_reply(QIOChannel *ioc, NBDReply *reply) @@ -764,7 +764,7 @@ ssize_t nbd_receive_reply(QIOChannel *ioc, NBDReply *re= ply) uint32_t magic; ssize_t ret; =20 - ret =3D read_sync_eof(ioc, buf, sizeof(buf)); + ret =3D read_sync_eof(ioc, buf, sizeof(buf), NULL); if (ret <=3D 0) { return ret; } diff --git a/nbd/nbd-internal.h b/nbd/nbd-internal.h index 1d479fe135..d6071640a0 100644 --- a/nbd/nbd-internal.h +++ b/nbd/nbd-internal.h @@ -100,7 +100,8 @@ * qio_channel_readv() returns 0. So, there are no needs to call read_sync= _eof * iteratively. */ -static inline ssize_t read_sync_eof(QIOChannel *ioc, void *buffer, size_t = size) +static inline ssize_t read_sync_eof(QIOChannel *ioc, void *buffer, size_t = size, + 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 @@ -108,18 +109,20 @@ 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, NULL); + return nbd_wr_syncv(ioc, &iov, 1, size, true, errp); } =20 /* read_sync * Reads @size bytes from @ioc. Returns 0 on success. */ -static inline int read_sync(QIOChannel *ioc, void *buffer, size_t size) +static inline int read_sync(QIOChannel *ioc, void *buffer, size_t size, + Error **errp) { - ssize_t ret =3D read_sync_eof(ioc, buffer, size); + ssize_t ret =3D read_sync_eof(ioc, buffer, size, errp); =20 if (ret >=3D 0 && ret !=3D size) { ret =3D -EINVAL; + error_setg(errp, "End of file"); } =20 return ret < 0 ? ret : 0; @@ -128,11 +131,12 @@ static inline int read_sync(QIOChannel *ioc, void *bu= ffer, size_t size) /* write_sync * Writes @size bytes to @ioc. Returns 0 on success. */ -static inline int write_sync(QIOChannel *ioc, const void *buffer, size_t s= ize) +static inline int write_sync(QIOChannel *ioc, const void *buffer, size_t s= ize, + 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, NULL); + ssize_t ret =3D nbd_wr_syncv(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 1e1096c762..ee59e5d234 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); + ret =3D read_sync(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); + ret =3D write_sync(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)); + ret =3D read_sync(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)); + return write_sync(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); + ret =3D write_sync(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) < 0) { + if (read_sync(client->ioc, req->data, request->len, NULL) < 0) { LOG("reading from socket failed"); rc =3D -EIO; goto out; --=20 2.11.1 From nobody Tue May 7 02:07:57 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 1494928113077721.6195561400243; Tue, 16 May 2017 02:48:33 -0700 (PDT) Received: from localhost ([::1]:40955 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dAZ5j-00037l-KL for importer@patchew.org; Tue, 16 May 2017 05:48:31 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54507) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dAZ3c-0001hQ-Nq for qemu-devel@nongnu.org; Tue, 16 May 2017 05:46:22 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dAZ3a-00050e-LG for qemu-devel@nongnu.org; Tue, 16 May 2017 05:46:20 -0400 Received: from mailhub.sw.ru ([195.214.232.25]:43988 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 1dAZ3a-0004zv-AJ for qemu-devel@nongnu.org; Tue, 16 May 2017 05:46:18 -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 v4G9jXBe020558; Tue, 16 May 2017 12:45:35 +0300 (MSK) From: Vladimir Sementsov-Ogievskiy To: qemu-devel@nongnu.org, qemu-block@nongnu.org Date: Tue, 16 May 2017 12:45:33 +0300 Message-Id: <20170516094533.6160-6-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20170516094533.6160-1-vsementsov@virtuozzo.com> References: <20170516094533.6160-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 5/5] nbd/client.c: use errp instead of LOG 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: kwolf@redhat.com, pbonzini@redhat.com, den@openvz.org, vsementsov@virtuozzo.com, mreitz@redhat.com 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 to modern errp scheme from just LOGging errors. Signed-off-by: Vladimir Sementsov-Ogievskiy --- block/nbd-client.c | 7 ++++++- include/block/nbd.h | 5 +++-- nbd/client.c | 26 ++++++++++++++------------ qemu-nbd.c | 3 ++- 4 files changed, 25 insertions(+), 16 deletions(-) diff --git a/block/nbd-client.c b/block/nbd-client.c index 538d95e031..073032bac5 100644 --- a/block/nbd-client.c +++ b/block/nbd-client.c @@ -28,6 +28,7 @@ */ =20 #include "qemu/osdep.h" +#include "qapi/error.h" #include "nbd-client.h" =20 #define HANDLE_TO_INDEX(bs, handle) ((handle) ^ ((uint64_t)(intptr_t)bs)) @@ -70,10 +71,14 @@ static coroutine_fn void nbd_read_reply_entry(void *opa= que) NBDClientSession *s =3D opaque; uint64_t i; int ret; + Error *local_err; =20 for (;;) { assert(s->reply.handle =3D=3D 0); - ret =3D nbd_receive_reply(s->ioc, &s->reply); + ret =3D nbd_receive_reply(s->ioc, &s->reply, &local_err); + if (ret < 0) { + error_report_err(local_err); + } if (ret <=3D 0) { break; } diff --git a/include/block/nbd.h b/include/block/nbd.h index a96fb5fceb..e0c64e4981 100644 --- a/include/block/nbd.h +++ b/include/block/nbd.h @@ -133,9 +133,10 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char = *name, uint16_t *flags, QCryptoTLSCreds *tlscreds, const char *hostname, QIOChannel **outioc, off_t *size, Error **errp); -int nbd_init(int fd, QIOChannelSocket *sioc, uint16_t flags, off_t size); +int nbd_init(int fd, QIOChannelSocket *sioc, uint16_t flags, off_t size, + Error **errp); ssize_t nbd_send_request(QIOChannel *ioc, NBDRequest *request); -ssize_t nbd_receive_reply(QIOChannel *ioc, NBDReply *reply); +ssize_t nbd_receive_reply(QIOChannel *ioc, NBDReply *reply, Error **errp); int nbd_client(int fd); int nbd_disconnect(int fd); =20 diff --git a/nbd/client.c b/nbd/client.c index f102375504..e41b4fd935 100644 --- a/nbd/client.c +++ b/nbd/client.c @@ -627,11 +627,13 @@ fail: } =20 #ifdef __linux__ -int nbd_init(int fd, QIOChannelSocket *sioc, uint16_t flags, off_t size) +int nbd_init(int fd, QIOChannelSocket *sioc, uint16_t flags, off_t size, + Error **errp) { unsigned long sectors =3D size / BDRV_SECTOR_SIZE; if (size / BDRV_SECTOR_SIZE !=3D sectors) { - LOG("Export size %lld too large for 32-bit kernel", (long long) si= ze); + error_setg(errp, "Export size %lld too large for 32-bit kernel", + (long long) size); return -E2BIG; } =20 @@ -639,7 +641,7 @@ int nbd_init(int fd, QIOChannelSocket *sioc, uint16_t f= lags, off_t size) =20 if (ioctl(fd, NBD_SET_SOCK, (unsigned long) sioc->fd) < 0) { int serrno =3D errno; - LOG("Failed to set NBD socket"); + error_setg(errp, "Failed to set NBD socket"); return -serrno; } =20 @@ -647,7 +649,7 @@ int nbd_init(int fd, QIOChannelSocket *sioc, uint16_t f= lags, off_t size) =20 if (ioctl(fd, NBD_SET_BLKSIZE, (unsigned long)BDRV_SECTOR_SIZE) < 0) { int serrno =3D errno; - LOG("Failed setting NBD block size"); + error_setg(errp, "Failed setting NBD block size"); return -serrno; } =20 @@ -659,7 +661,7 @@ int nbd_init(int fd, QIOChannelSocket *sioc, uint16_t f= lags, off_t size) =20 if (ioctl(fd, NBD_SET_SIZE_BLOCKS, sectors) < 0) { int serrno =3D errno; - LOG("Failed setting size (in blocks)"); + error_setg(errp, "Failed setting size (in blocks)"); return -serrno; } =20 @@ -670,12 +672,12 @@ int nbd_init(int fd, QIOChannelSocket *sioc, uint16_t= flags, off_t size) =20 if (ioctl(fd, BLKROSET, (unsigned long) &read_only) < 0) { int serrno =3D errno; - LOG("Failed setting read-only attribute"); + error_setg(errp, "Failed setting read-only attribute"); return -serrno; } } else { int serrno =3D errno; - LOG("Failed setting flags"); + error_setg(errp, "Failed setting flags"); return -serrno; } } @@ -758,19 +760,19 @@ ssize_t nbd_send_request(QIOChannel *ioc, NBDRequest = *request) return write_sync(ioc, buf, sizeof(buf), NULL); } =20 -ssize_t nbd_receive_reply(QIOChannel *ioc, NBDReply *reply) +ssize_t nbd_receive_reply(QIOChannel *ioc, NBDReply *reply, Error **errp) { uint8_t buf[NBD_REPLY_SIZE]; uint32_t magic; ssize_t ret; =20 - ret =3D read_sync_eof(ioc, buf, sizeof(buf), NULL); + ret =3D read_sync_eof(ioc, buf, sizeof(buf), errp); if (ret <=3D 0) { return ret; } =20 if (ret !=3D sizeof(buf)) { - LOG("read failed"); + error_setg(errp, "read failed"); return -EINVAL; } =20 @@ -788,7 +790,7 @@ ssize_t nbd_receive_reply(QIOChannel *ioc, NBDReply *re= ply) =20 if (reply->error =3D=3D ESHUTDOWN) { /* This works even on mingw which lacks a native ESHUTDOWN */ - LOG("server shutting down"); + error_setg(errp, "server shutting down"); return -EINVAL; } TRACE("Got reply: { magic =3D 0x%" PRIx32 ", .error =3D % " PRId32 @@ -796,7 +798,7 @@ ssize_t nbd_receive_reply(QIOChannel *ioc, NBDReply *re= ply) magic, reply->error, reply->handle); =20 if (magic !=3D NBD_REPLY_MAGIC) { - LOG("invalid magic (got 0x%" PRIx32 ")", magic); + error_setg(errp, "invalid magic (got 0x%" PRIx32 ")", magic); return -EINVAL; } return sizeof(buf); diff --git a/qemu-nbd.c b/qemu-nbd.c index e080fb7c75..83b8b3e1d9 100644 --- a/qemu-nbd.c +++ b/qemu-nbd.c @@ -288,8 +288,9 @@ static void *nbd_client_thread(void *arg) goto out_socket; } =20 - ret =3D nbd_init(fd, sioc, nbdflags, size); + ret =3D nbd_init(fd, sioc, nbdflags, size, &local_error); if (ret < 0) { + error_report_err(local_error); goto out_fd; } =20 --=20 2.11.1