From nobody Tue Feb 10 05:17:22 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.zohomail.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 1511346303524576.8075080811625; Wed, 22 Nov 2017 02:25:03 -0800 (PST) Received: from localhost ([::1]:38706 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eHSDC-0004vb-Cy for importer@patchew.org; Wed, 22 Nov 2017 05:24:58 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33931) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eHS8Z-0001N6-6w for qemu-devel@nongnu.org; Wed, 22 Nov 2017 05:20:16 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eHS8U-0004mI-7q for qemu-devel@nongnu.org; Wed, 22 Nov 2017 05:20:11 -0500 Received: from mailhub.sw.ru ([195.214.232.25]:36975 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 1eHS8T-0004k1-MR for qemu-devel@nongnu.org; Wed, 22 Nov 2017 05:20:06 -0500 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 vAMAJwhI016254; Wed, 22 Nov 2017 13:19:59 +0300 (MSK) From: Vladimir Sementsov-Ogievskiy To: qemu-block@nongnu.org, qemu-devel@nongnu.org Date: Wed, 22 Nov 2017 13:19:56 +0300 Message-Id: <20171122101958.17065-4-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20171122101958.17065-1-vsementsov@virtuozzo.com> References: <20171122101958.17065-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/server: add helper nbd_opt_invalid 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, vsementsov@virtuozzo.com, mreitz@redhat.com, den@openvz.org, pbonzini@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" Signed-off-by: Vladimir Sementsov-Ogievskiy --- nbd/server.c | 74 +++++++++++++++++++++++++++++++++++++-------------------= ---- 1 file changed, 46 insertions(+), 28 deletions(-) diff --git a/nbd/server.c b/nbd/server.c index c9445a89e9..79b937d88f 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -199,20 +199,15 @@ static int nbd_negotiate_send_rep(NBDClient *client, = uint32_t type, return nbd_negotiate_send_rep_len(client, type, 0, errp); } =20 -/* Send an error reply. - * Return -errno on error, 0 on success. */ -static int GCC_FMT_ATTR(4, 5) -nbd_negotiate_send_rep_err(NBDClient *client, uint32_t type, - Error **errp, const char *fmt, ...) +static int GCC_FMT_ATTR(3, 0) +nbd_negotiate_send_rep_err_v(NBDClient *client, uint32_t type, + const char *fmt, va_list va, Error **errp) { - va_list va; char *msg; int ret; size_t len; =20 - va_start(va, fmt); msg =3D g_strdup_vprintf(fmt, va); - va_end(va); len =3D strlen(msg); assert(len < 4096); trace_nbd_negotiate_send_rep_err(msg); @@ -232,6 +227,43 @@ out: return ret; } =20 +static int GCC_FMT_ATTR(4, 5) +nbd_negotiate_send_rep_err(NBDClient *client, uint32_t type, + Error **errp, const char *fmt, ...) +{ + va_list va; + int ret; + + va_start(va, fmt); + ret =3D nbd_negotiate_send_rep_err_v(client, type, fmt, va, errp); + va_end(va); + + return ret; +} + +/* nbd_opt_invalid + * Drop reminded option data and reply with NBD_REP_ERR_INVALID + */ +static int nbd_opt_invalid(NBDClient *client, + Error **errp, const char *fmt, ...) +{ + int ret; + va_list va; + + if (client->optlen > 0) { + if (nbd_opt_drop(client, client->optlen, errp) < 0) { + return -EIO; + } + } + + va_start(va, fmt); + ret =3D nbd_negotiate_send_rep_err_v(client, NBD_REP_ERR_INVALID, + fmt, va, errp); + va_end(va); + + return ret; +} + /* Send a single NBD_REP_SERVER reply to NBD_OPT_LIST, including payload. * Return -errno on error, 0 on success. */ static int nbd_negotiate_send_rep_list(NBDClient *client, NBDExport *exp, @@ -384,7 +416,6 @@ static int nbd_negotiate_handle_info(NBDClient *client,= uint16_t myflags, bool blocksize =3D false; uint32_t sizes[3]; char buf[sizeof(uint64_t) + sizeof(uint16_t)]; - const char *msg; =20 /* Client sends: 4 bytes: L, name length (can be 0) @@ -393,8 +424,7 @@ static int nbd_negotiate_handle_info(NBDClient *client,= uint16_t myflags, N * 2 bytes: N requests */ if (client->optlen < sizeof(namelen) + sizeof(requests)) { - msg =3D "overall request too short"; - goto invalid; + return nbd_opt_invalid(client, errp, "overall request too short"); } if (nbd_opt_read(client, &namelen, sizeof(namelen), errp) < 0) { return -EIO; @@ -403,8 +433,7 @@ static int nbd_negotiate_handle_info(NBDClient *client,= uint16_t myflags, if (namelen > client->optlen - sizeof(requests) || (client->optlen - namelen) % 2) { - msg =3D "name length is incorrect"; - goto invalid; + return nbd_opt_invalid(client, errp, "name length is incorrect"); } if (nbd_opt_read(client, name, namelen, errp) < 0) { return -EIO; @@ -418,8 +447,8 @@ static int nbd_negotiate_handle_info(NBDClient *client,= uint16_t myflags, be16_to_cpus(&requests); trace_nbd_negotiate_handle_info_requests(requests); if (requests !=3D client->optlen / sizeof(request)) { - msg =3D "incorrect number of requests for overall length"; - goto invalid; + return nbd_opt_invalid( + client, errp, "incorrect number of requests for overall length= "); } while (requests--) { if (nbd_opt_read(client, &request, sizeof(request), errp) < 0) { @@ -528,13 +557,6 @@ static int nbd_negotiate_handle_info(NBDClient *client= , uint16_t myflags, rc =3D 1; } return rc; - - invalid: - if (nbd_opt_drop(client, client->optlen, errp) < 0) { - return -EIO; - } - return nbd_negotiate_send_rep_err(client, NBD_REP_ERR_INVALID, - errp, "%s", msg); } =20 =20 @@ -597,12 +619,8 @@ static int nbd_reject_length(NBDClient *client, bool f= atal, Error **errp) int ret; =20 assert(client->optlen); - if (nbd_drop(client->ioc, client->optlen, errp) < 0) { - return -EIO; - } - ret =3D nbd_negotiate_send_rep_err(client, NBD_REP_ERR_INVALID, errp, - "option '%s' should have zero length", - nbd_opt_lookup(client->opt)); + ret =3D nbd_opt_invalid(client, errp, "option '%s' should have zero le= ngth", + nbd_opt_lookup(client->opt)); if (fatal && !ret) { error_setg(errp, "option '%s' should have zero length", nbd_opt_lookup(client->opt)); --=20 2.11.1