From nobody Sat Apr 27 10:36:39 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.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 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1566667799; cv=none; d=zoho.com; s=zohoarc; b=MLNux+2Su2Se5Tw6EVAdBqSPOkn5CHhOHgu/VKaBcOP2gVeGNcgU0sST5fnkmBnGKWTg7WOpCyqRh2hS276RDHe7iMdbQOOwx97ByT6RoO+RH/Fea4MQqJxXz2YgP2ISFMpNz6WXmy51zRb5oF+NMs0hy/+ie6r+B+jM0F5DoP0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1566667799; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=DkJwcOXJNm1N2nwret2NcqStOkAnAXiVj2Rv3uGYY7k=; b=GN6lBpWs0FcmN08ljsYrKnIjHPMoZW7iw2leYJuhikc9dmTsOdWJmN7MkZ0DKSpiqT5xGOGQiMhruiWpoWYIWuXCPwaDeG4XhXkdKBsA0LFsmsZJGgvKv5ZVrQ7XR/7n1a7GJiQK3+aW2wKjdQgpzjpFuVXgcjyQxoqjiL5bIxA= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1566667799287681.4260622705417; Sat, 24 Aug 2019 10:29:59 -0700 (PDT) Received: from localhost ([::1]:38818 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i1ZrS-0008JL-2r for importer@patchew.org; Sat, 24 Aug 2019 13:29:58 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:42410) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i1Zps-0006uk-Uu for qemu-devel@nongnu.org; Sat, 24 Aug 2019 13:28:22 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i1Zpr-0008RQ-RU for qemu-devel@nongnu.org; Sat, 24 Aug 2019 13:28:20 -0400 Received: from mx1.redhat.com ([209.132.183.28]:62221) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1i1Zpp-0008Pr-97; Sat, 24 Aug 2019 13:28:17 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 94D623086246; Sat, 24 Aug 2019 17:28:16 +0000 (UTC) Received: from blue.redhat.com (ovpn-116-234.phx2.redhat.com [10.3.116.234]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1738860933; Sat, 24 Aug 2019 17:28:16 +0000 (UTC) From: Eric Blake To: qemu-devel@nongnu.org Date: Sat, 24 Aug 2019 12:28:12 -0500 Message-Id: <20190824172813.29720-2-eblake@redhat.com> In-Reply-To: <20190824172813.29720-1-eblake@redhat.com> References: <20190824172813.29720-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.49]); Sat, 24 Aug 2019 17:28:16 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v2 1/2] nbd: Use g_autofree in a few places X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , vsementsov@virtuozzo.com, rjones@redhat.com, "open list:Network Block Dev..." , Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" Thanks to our recent move to use glib's g_autofree, I can join the bandwagon. Getting rid of gotos is fun ;) There are probably more places where we could register cleanup functions and get rid of more gotos; this patch just focuses on the labels that existed merely to call g_free. Signed-off-by: Eric Blake Reviewed-by: Daniel P. Berrang=C3=A9 Reviewed-by: Vladimir Sementsov-Ogievskiy --- block/nbd.c | 11 ++++------- nbd/client.c | 22 +++++++--------------- nbd/server.c | 12 ++++-------- 3 files changed, 15 insertions(+), 30 deletions(-) diff --git a/block/nbd.c b/block/nbd.c index beed46fb3414..c4c91a158602 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -1374,7 +1374,7 @@ static bool nbd_has_filename_options_conflict(QDict *= options, Error **errp) static void nbd_parse_filename(const char *filename, QDict *options, Error **errp) { - char *file; + g_autofree char *file =3D NULL; char *export_name; const char *host_spec; const char *unixpath; @@ -1396,7 +1396,7 @@ static void nbd_parse_filename(const char *filename, = QDict *options, export_name =3D strstr(file, EN_OPTSTR); if (export_name) { if (export_name[strlen(EN_OPTSTR)] =3D=3D 0) { - goto out; + return; } export_name[0] =3D 0; /* truncate 'file' */ export_name +=3D strlen(EN_OPTSTR); @@ -1407,11 +1407,11 @@ static void nbd_parse_filename(const char *filename= , QDict *options, /* extract the host_spec - fail if it's not nbd:... */ if (!strstart(file, "nbd:", &host_spec)) { error_setg(errp, "File name string for NBD must start with 'nbd:'"= ); - goto out; + return; } if (!*host_spec) { - goto out; + return; } /* are we a UNIX or TCP socket? */ @@ -1431,9 +1431,6 @@ static void nbd_parse_filename(const char *filename, = QDict *options, out_inet: qapi_free_InetSocketAddress(addr); } - -out: - g_free(file); } static bool nbd_process_legacy_socket_options(QDict *output_options, diff --git a/nbd/client.c b/nbd/client.c index 49bf9906f94b..a9d8d32feff7 100644 --- a/nbd/client.c +++ b/nbd/client.c @@ -247,12 +247,11 @@ static int nbd_handle_reply_err(QIOChannel *ioc, NBDO= ptionReply *reply, static int nbd_receive_list(QIOChannel *ioc, char **name, char **descripti= on, Error **errp) { - int ret =3D -1; NBDOptionReply reply; uint32_t len; uint32_t namelen; - char *local_name =3D NULL; - char *local_desc =3D NULL; + g_autofree char *local_name =3D NULL; + g_autofree char *local_desc =3D NULL; int error; if (nbd_receive_option_reply(ioc, NBD_OPT_LIST, &reply, errp) < 0) { @@ -298,7 +297,7 @@ static int nbd_receive_list(QIOChannel *ioc, char **nam= e, char **description, local_name =3D g_malloc(namelen + 1); if (nbd_read(ioc, local_name, namelen, "export name", errp) < 0) { nbd_send_opt_abort(ioc); - goto out; + return -1; } local_name[namelen] =3D '\0'; len -=3D namelen; @@ -306,24 +305,17 @@ static int nbd_receive_list(QIOChannel *ioc, char **n= ame, char **description, local_desc =3D g_malloc(len + 1); if (nbd_read(ioc, local_desc, len, "export description", errp) < 0= ) { nbd_send_opt_abort(ioc); - goto out; + return -1; } local_desc[len] =3D '\0'; } trace_nbd_receive_list(local_name, local_desc ?: ""); - *name =3D local_name; - local_name =3D NULL; + *name =3D g_steal_pointer(&local_name); if (description) { - *description =3D local_desc; - local_desc =3D NULL; + *description =3D g_steal_pointer(&local_desc); } - ret =3D 1; - - out: - g_free(local_name); - g_free(local_desc); - return ret; + return 1; } diff --git a/nbd/server.c b/nbd/server.c index 0fb41c6c50ea..74d205812fee 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -206,7 +206,7 @@ static int GCC_FMT_ATTR(4, 0) nbd_negotiate_send_rep_verr(NBDClient *client, uint32_t type, Error **errp, const char *fmt, va_list va) { - char *msg; + g_autofree char *msg =3D NULL; int ret; size_t len; @@ -216,18 +216,14 @@ nbd_negotiate_send_rep_verr(NBDClient *client, uint32= _t type, trace_nbd_negotiate_send_rep_err(msg); ret =3D nbd_negotiate_send_rep_len(client, type, len, errp); if (ret < 0) { - goto out; + return ret; } if (nbd_write(client->ioc, msg, len, errp) < 0) { error_prepend(errp, "write failed (error message): "); - ret =3D -EIO; - } else { - ret =3D 0; + return -EIO; } -out: - g_free(msg); - return ret; + return 0; } /* Send an error reply. --=20 2.21.0 From nobody Sat Apr 27 10:36:39 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.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 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1566667907; cv=none; d=zoho.com; s=zohoarc; b=RxVBmiCjyiK9BYc97RvK4uQejLHeGBZOq5y6QTtCeuAgDEOMOGVbzIzkntgoj+RfzDQjm2WicmAs7PfjRFQJx+P46fPb5yQatFmfXqIW8zxyOgskDJxcgCQMZwOSjeL2nA1NDoWUgkmTynCBCo1nMuM8qKqdUbjTIpL7Vxn0K/k= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1566667907; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=Ay0eMbgbEjSdWJAV97WiqJ6PqQPlKw9vpVIMmA0W1j0=; b=SlKG2TNPUzFJyh6XH/Mdg3urocTc72PqeknVh8vgJLe6xVI2YnO5jcCRm27G8H5ZAxon9DXZhTa51Ttd7LhjwCJNU3aP/VcAd/B4XHOj/7S8TRNWBYuWH/CfWu3GwitfLwfXmGRvVyhA2LLWLVJk54OW9j6tltI8P9cyStNP9+I= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1566667907540282.5939961606331; Sat, 24 Aug 2019 10:31:47 -0700 (PDT) Received: from localhost ([::1]:38832 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i1ZtA-0001F4-E4 for importer@patchew.org; Sat, 24 Aug 2019 13:31:44 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:42421) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i1Zpu-0006wH-Mp for qemu-devel@nongnu.org; Sat, 24 Aug 2019 13:28:24 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i1Zpt-0008Rn-2G for qemu-devel@nongnu.org; Sat, 24 Aug 2019 13:28:22 -0400 Received: from mx1.redhat.com ([209.132.183.28]:41616) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1i1Zpp-0008Q2-Qp; Sat, 24 Aug 2019 13:28:17 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2D9E28980EE; Sat, 24 Aug 2019 17:28:17 +0000 (UTC) Received: from blue.redhat.com (ovpn-116-234.phx2.redhat.com [10.3.116.234]) by smtp.corp.redhat.com (Postfix) with ESMTP id C33F660606; Sat, 24 Aug 2019 17:28:16 +0000 (UTC) From: Eric Blake To: qemu-devel@nongnu.org Date: Sat, 24 Aug 2019 12:28:13 -0500 Message-Id: <20190824172813.29720-3-eblake@redhat.com> In-Reply-To: <20190824172813.29720-1-eblake@redhat.com> References: <20190824172813.29720-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.6.2 (mx1.redhat.com [10.5.110.67]); Sat, 24 Aug 2019 17:28:17 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v2 2/2] nbd: Tolerate more errors to structured reply request X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: vsementsov@virtuozzo.com, rjones@redhat.com, "open list:Network Block Dev..." Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" A server may have a reason to reject a request for structured replies, beyond just not recognizing them as a valid request; similarly, it may have a reason for rejecting a request for a meta context. It doesn't hurt us to continue talking to such a server; otherwise 'qemu-nbd --list' of such a server fails to display all available details about the export. Encountered when temporarily tweaking nbdkit to reply with NBD_REP_ERR_POLICY. Present since structured reply support was first added (commit d795299b reused starttls handling, but starttls is different in that we can't fall back to other behavior on any error). Signed-off-by: Eric Blake Reviewed-by: Daniel P. Berrang=C3=A9 --- nbd/client.c | 63 +++++++++++++++++++++++++----------------------- nbd/trace-events | 2 +- 2 files changed, 34 insertions(+), 31 deletions(-) diff --git a/nbd/client.c b/nbd/client.c index a9d8d32feff7..b9dc829175f9 100644 --- a/nbd/client.c +++ b/nbd/client.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016-2018 Red Hat, Inc. + * Copyright (C) 2016-2019 Red Hat, Inc. * Copyright (C) 2005 Anthony Liguori * * Network Block Device Client Side @@ -142,17 +142,18 @@ static int nbd_receive_option_reply(QIOChannel *ioc, = uint32_t opt, return 0; } -/* If reply represents success, return 1 without further action. - * If reply represents an error, consume the optional payload of - * the packet on ioc. Then return 0 for unsupported (so the client - * can fall back to other approaches), or -1 with errp set for other - * errors. +/* + * If reply represents success, return 1 without further action. If + * reply represents an error, consume the optional payload of the + * packet on ioc. Then return 0 for unsupported (so the client can + * fall back to other approaches), where @strict determines if only + * ERR_UNSUP or all errors fit that category, or -1 with errp set for + * other errors. */ static int nbd_handle_reply_err(QIOChannel *ioc, NBDOptionReply *reply, - Error **errp) + bool strict, Error **errp) { - char *msg =3D NULL; - int result =3D -1; + g_autofree char *msg =3D NULL; if (!(reply->type & (1 << 31))) { return 1; @@ -163,26 +164,28 @@ static int nbd_handle_reply_err(QIOChannel *ioc, NBDO= ptionReply *reply, error_setg(errp, "server error %" PRIu32 " (%s) message is too long", reply->type, nbd_rep_lookup(reply->type)); - goto cleanup; + goto err; } msg =3D g_malloc(reply->length + 1); if (nbd_read(ioc, msg, reply->length, NULL, errp) < 0) { error_prepend(errp, "Failed to read option error %" PRIu32 " (%s) message: ", reply->type, nbd_rep_lookup(reply->type)); - goto cleanup; + goto err; } msg[reply->length] =3D '\0'; trace_nbd_server_error_msg(reply->type, nbd_reply_type_lookup(reply->type), msg= ); } + if (reply->type =3D=3D NBD_REP_ERR_UNSUP || !strict) { + trace_nbd_reply_err_ignored(reply->option, + nbd_opt_lookup(reply->option), + reply->type, nbd_rep_lookup(reply->typ= e)); + return 0; + } + switch (reply->type) { - case NBD_REP_ERR_UNSUP: - trace_nbd_reply_err_unsup(reply->option, nbd_opt_lookup(reply->opt= ion)); - result =3D 0; - goto cleanup; - case NBD_REP_ERR_POLICY: error_setg(errp, "Denied by server for option %" PRIu32 " (%s)", reply->option, nbd_opt_lookup(reply->option)); @@ -227,12 +230,9 @@ static int nbd_handle_reply_err(QIOChannel *ioc, NBDOp= tionReply *reply, error_append_hint(errp, "server reported: %s\n", msg); } - cleanup: - g_free(msg); - if (result < 0) { - nbd_send_opt_abort(ioc); - } - return result; + err: + nbd_send_opt_abort(ioc); + return -1; } /* nbd_receive_list: @@ -257,7 +257,7 @@ static int nbd_receive_list(QIOChannel *ioc, char **nam= e, char **description, if (nbd_receive_option_reply(ioc, NBD_OPT_LIST, &reply, errp) < 0) { return -1; } - error =3D nbd_handle_reply_err(ioc, &reply, errp); + error =3D nbd_handle_reply_err(ioc, &reply, true, errp); if (error <=3D 0) { return error; } @@ -363,7 +363,7 @@ static int nbd_opt_info_or_go(QIOChannel *ioc, uint32_t= opt, if (nbd_receive_option_reply(ioc, opt, &reply, errp) < 0) { return -1; } - error =3D nbd_handle_reply_err(ioc, &reply, errp); + error =3D nbd_handle_reply_err(ioc, &reply, true, errp); if (error <=3D 0) { return error; } @@ -538,12 +538,15 @@ static int nbd_receive_query_exports(QIOChannel *ioc, } } -/* nbd_request_simple_option: Send an option request, and parse the reply +/* + * nbd_request_simple_option: Send an option request, and parse the reply. + * @strict controls whether ERR_UNSUP or all errors produce 0 status. * return 1 for successful negotiation, * 0 if operation is unsupported, * -1 with errp set for any other error */ -static int nbd_request_simple_option(QIOChannel *ioc, int opt, Error **err= p) +static int nbd_request_simple_option(QIOChannel *ioc, int opt, bool strict, + Error **errp) { NBDOptionReply reply; int error; @@ -555,7 +558,7 @@ static int nbd_request_simple_option(QIOChannel *ioc, i= nt opt, Error **errp) if (nbd_receive_option_reply(ioc, opt, &reply, errp) < 0) { return -1; } - error =3D nbd_handle_reply_err(ioc, &reply, errp); + error =3D nbd_handle_reply_err(ioc, &reply, strict, errp); if (error <=3D 0) { return error; } @@ -587,7 +590,7 @@ static QIOChannel *nbd_receive_starttls(QIOChannel *ioc, QIOChannelTLS *tioc; struct NBDTLSHandshakeData data =3D { 0 }; - ret =3D nbd_request_simple_option(ioc, NBD_OPT_STARTTLS, errp); + ret =3D nbd_request_simple_option(ioc, NBD_OPT_STARTTLS, true, errp); if (ret <=3D 0) { if (ret =3D=3D 0) { error_setg(errp, "Server don't support STARTTLS option"); @@ -687,7 +690,7 @@ static int nbd_receive_one_meta_context(QIOChannel *ioc, return -1; } - ret =3D nbd_handle_reply_err(ioc, &reply, errp); + ret =3D nbd_handle_reply_err(ioc, &reply, false, errp); if (ret <=3D 0) { return ret; } @@ -943,7 +946,7 @@ static int nbd_start_negotiate(AioContext *aio_context,= QIOChannel *ioc, if (structured_reply) { result =3D nbd_request_simple_option(ioc, NBD_OPT_STRUCTURED_REPL= Y, - errp); + false, errp); if (result < 0) { return -EINVAL; } diff --git a/nbd/trace-events b/nbd/trace-events index 7ab6b3788cb2..f6cde967903a 100644 --- a/nbd/trace-events +++ b/nbd/trace-events @@ -4,7 +4,7 @@ nbd_send_option_request(uint32_t opt, const char *name, uint32_t len) "Sen= ding option request %" PRIu32" (%s), len %" PRIu32 nbd_receive_option_reply(uint32_t option, const char *optname, uint32_t ty= pe, const char *typename, uint32_t length) "Received option reply %" PRIu32= " (%s), type %" PRIu32" (%s), len %" PRIu32 nbd_server_error_msg(uint32_t err, const char *type, const char *msg) "ser= ver reported error 0x%" PRIx32 " (%s) with additional message: %s" -nbd_reply_err_unsup(uint32_t option, const char *name) "server doesn't und= erstand request %" PRIu32 " (%s), attempting fallback" +nbd_reply_err_ignored(uint32_t option, const char *name, uint32_t reply, c= onst char *reply_name) "server failed request %" PRIu32 " (%s) with error 0= x%" PRIx32 " (%s), attempting fallback" nbd_receive_list(const char *name, const char *desc) "export list includes= '%s', description '%s'" nbd_opt_info_go_start(const char *opt, const char *name) "Attempting %s fo= r export '%s'" nbd_opt_info_go_success(const char *opt) "Export is ready after %s request" --=20 2.21.0