From nobody Sun Feb 8 22:34:03 2026 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 1496155684164728.4957127542631; Tue, 30 May 2017 07:48:04 -0700 (PDT) Received: from localhost ([::1]:54252 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dFiRG-0003sI-Ow for importer@patchew.org; Tue, 30 May 2017 10:48:02 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54320) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dFiAp-0005Qc-F2 for qemu-devel@nongnu.org; Tue, 30 May 2017 10:31:06 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dFiAn-0000QP-0B for qemu-devel@nongnu.org; Tue, 30 May 2017 10:31:03 -0400 Received: from mailhub.sw.ru ([195.214.232.25]:3338 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 1dFiAl-0000N8-TB for qemu-devel@nongnu.org; Tue, 30 May 2017 10:31: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 v4UEUqEH027829; Tue, 30 May 2017 17:30:54 +0300 (MSK) From: Vladimir Sementsov-Ogievskiy To: qemu-devel@nongnu.org Date: Tue, 30 May 2017 17:30:34 +0300 Message-Id: <20170530143052.165002-2-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20170530143052.165002-1-vsementsov@virtuozzo.com> References: <20170530143052.165002-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/19] 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_wr_syncv was working through qemu_co_sendv_recvv, which just yields, without setting any handlers. But now, 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 {read,write,drop}_sync functions. Signed-off-by: Vladimir Sementsov-Ogievskiy --- nbd/client.c | 26 ------------- nbd/common.c | 26 +++++++++++++ nbd/nbd-internal.h | 2 + nbd/server.c | 107 +++++++++++--------------------------------------= ---- 4 files changed, 50 insertions(+), 111 deletions(-) diff --git a/nbd/client.c b/nbd/client.c index f9e1d75be4..3d15596120 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 drop_sync(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 read_sync(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 bd81637ab9..e520aae741 100644 --- a/nbd/common.c +++ b/nbd/common.c @@ -69,6 +69,32 @@ ssize_t nbd_wr_syncv(QIOChannel *ioc, return done; } =20 +/* Discard length bytes from channel. Return -errno on failure and 0 on + * success*/ +int drop_sync(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 read_sync(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 d6071640a0..01d5778679 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 drop_sync(QIOChannel *ioc, size_t size, Error **errp); + #endif diff --git a/nbd/server.c b/nbd/server.c index ee59e5d234..26096f14e1 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 read_sync(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 write_sync(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 (write_sync(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 (write_sync(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 (write_sync(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 (write_sync(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 (write_sync(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 (write_sync(ioc, &len, sizeof(len), NULL) < 0) { LOG("write failed (name length)"); return -EINVAL; } - if (nbd_negotiate_write(ioc, name, name_len) < 0) { + if (write_sync(ioc, name, name_len, NULL) < 0) { LOG("write failed (name buffer)"); return -EINVAL; } - if (nbd_negotiate_write(ioc, desc, desc_len) < 0) { + if (write_sync(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 (drop_sync(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 (read_sync(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 (drop_sync(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 (read_sync(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 (read_sync(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 (read_sync(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 (read_sync(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 (drop_sync(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 (drop_sync(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 (drop_sync(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 (write_sync(client->ioc, buf, sizeof(buf), NULL) < 0) { LOG("write failed"); goto fail; } } else { - if (nbd_negotiate_write(client->ioc, buf, 18) < 0) { + if (write_sync(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 (write_sync(client->ioc, buf + 18, len, NULL) < 0) { LOG("write failed"); goto fail; } --=20 2.11.1