From patchwork Fri Jan 15 16:04:25 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 8042931 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id DA0E2BEEE5 for ; Fri, 15 Jan 2016 16:08:39 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 222922041A for ; Fri, 15 Jan 2016 16:08:39 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 6644D2040F for ; Fri, 15 Jan 2016 16:08:38 +0000 (UTC) Received: from localhost ([::1]:47809 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aK6vV-0006Eu-DG for patchwork-qemu-devel@patchwork.kernel.org; Fri, 15 Jan 2016 11:08:37 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59178) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aK6rw-0007wv-QY for qemu-devel@nongnu.org; Fri, 15 Jan 2016 11:04:57 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aK6ro-0003Je-GP for qemu-devel@nongnu.org; Fri, 15 Jan 2016 11:04:56 -0500 Received: from mail-wm0-x22d.google.com ([2a00:1450:400c:c09::22d]:35811) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aK6ro-0003JX-6O for qemu-devel@nongnu.org; Fri, 15 Jan 2016 11:04:48 -0500 Received: by mail-wm0-x22d.google.com with SMTP id f206so26185808wmf.0 for ; Fri, 15 Jan 2016 08:04:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=osGdy//RoXTitIfEpmqQ3jJoTZBzKoMCWOP/XrX6BxU=; b=Ag/mgLb2C0Nf88AxcBaQRmbYdkEIa3gZ7iFyNHdZiaVXFtXVaqizO0zrj2B3sxO5I+ YZ0vMNQbYoXC4DDKEhXW7YWKcynHMhF7ASM2rlKi04fnjAA83FujVlkjSrNKFTvNJ3Sg 0TXlfTQ177uL5IQzb5+6iO3cqYRujcMEzN5r6mkr//mjbky4tcpnQ3rITF2Qy+aX75rH EhmsGCAALIpORUFvemhdWpRFiJ47hFpFqm02kzgSBp53fTNB5CCgD2ewWItciVooBLn7 BDRtVyl+FMra3MhiBmjF9pp/gVydh34lqVw4Iqs06xObZ+0i0wBaFv0DDkmA7Axk0T+g xH5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=osGdy//RoXTitIfEpmqQ3jJoTZBzKoMCWOP/XrX6BxU=; b=iepqQTTU9npylFEm40jLJJRT23EJ3Nvm6xr+S3NMckD7T822QDOTLeH5rkN0ZPwUGA GDjOlftw/ieSjRHHl0DbFpwDBXfElWEGTfS5Ip4xDczVnag1RYFK+4nofACaLJ5AXNmt 7T4oWZrOiBvjH6Jswfyw4hk+P43vVizjrIgt40/UbVi6ib7vCMKxpq55z/QomG3GXDgu QYKRhPSDdGygvZx50HfFRh5AGe8t71GnvChP/hBpMmz3CqnxkfsTAMgo9DVqUZfOHQbq aBY+H3LWXaKfsrOOoG9lkf3qjPIXZbPFByAWCg3H/XDxzYFPtQy+U/wEoGROuHWtQr9A LJIw== X-Gm-Message-State: AG10YOS9x8e9z+xMyvAR95TqIHMKXxNzO52ZzvNL0TPZ19LJSTazPUOBllsAKk1CeaS0gg== X-Received: by 10.28.4.145 with SMTP id 139mr4433809wme.56.1452873887606; Fri, 15 Jan 2016 08:04:47 -0800 (PST) Received: from 640k.lan (94-39-195-126.adsl-ull.clienti.tiscali.it. [94.39.195.126]) by smtp.gmail.com with ESMTPSA id c15sm3103036wmd.19.2016.01.15.08.04.46 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 15 Jan 2016 08:04:46 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Fri, 15 Jan 2016 17:04:25 +0100 Message-Id: <1452873871-138914-11-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1452873871-138914-1-git-send-email-pbonzini@redhat.com> References: <1452873871-138914-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::22d Cc: Fam Zheng Subject: [Qemu-devel] [PULL 09/15] nbd: Always call "close_fn" in nbd_client_new X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Fam Zheng Rename the parameter "close" to "close_fn" to disambiguous with close(2). This unifies error handling paths of NBDClient allocation: nbd_client_new will shutdown the socket and call the "close_fn" callback if negotiation failed, so the caller don't need a different path than the normal close. The returned pointer is never used, make it void in preparation for the next patch. Signed-off-by: Fam Zheng Message-Id: <1452760863-25350-2-git-send-email-famz@redhat.com> Signed-off-by: Paolo Bonzini --- blockdev-nbd.c | 5 ++--- include/block/nbd.h | 3 +-- nbd.c | 11 +++++------ qemu-nbd.c | 10 +++------- 4 files changed, 11 insertions(+), 18 deletions(-) diff --git a/blockdev-nbd.c b/blockdev-nbd.c index bcdd18b..4a758ac 100644 --- a/blockdev-nbd.c +++ b/blockdev-nbd.c @@ -27,9 +27,8 @@ static void nbd_accept(void *opaque) socklen_t addr_len = sizeof(addr); int fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len); - if (fd >= 0 && !nbd_client_new(NULL, fd, nbd_client_put)) { - shutdown(fd, 2); - close(fd); + if (fd >= 0) { + nbd_client_new(NULL, fd, nbd_client_put); } } diff --git a/include/block/nbd.h b/include/block/nbd.h index 65f409d..7eccb41 100644 --- a/include/block/nbd.h +++ b/include/block/nbd.h @@ -98,8 +98,7 @@ NBDExport *nbd_export_find(const char *name); void nbd_export_set_name(NBDExport *exp, const char *name); void nbd_export_close_all(void); -NBDClient *nbd_client_new(NBDExport *exp, int csock, - void (*close)(NBDClient *)); +void nbd_client_new(NBDExport *exp, int csock, void (*close_fn)(NBDClient *)); void nbd_client_get(NBDClient *client); void nbd_client_put(NBDClient *client); diff --git a/nbd.c b/nbd.c index b3d9654..f8d0221 100644 --- a/nbd.c +++ b/nbd.c @@ -1475,8 +1475,7 @@ static void nbd_update_can_read(NBDClient *client) } } -NBDClient *nbd_client_new(NBDExport *exp, int csock, - void (*close)(NBDClient *)) +void nbd_client_new(NBDExport *exp, int csock, void (*close_fn)(NBDClient *)) { NBDClient *client; client = g_malloc0(sizeof(NBDClient)); @@ -1485,10 +1484,11 @@ NBDClient *nbd_client_new(NBDExport *exp, int csock, client->sock = csock; client->can_read = true; if (nbd_send_negotiate(client)) { - g_free(client); - return NULL; + shutdown(client->sock, 2); + close_fn(client); + return; } - client->close = close; + client->close = close_fn; qemu_co_mutex_init(&client->send_lock); nbd_set_handlers(client); @@ -1496,5 +1496,4 @@ NBDClient *nbd_client_new(NBDExport *exp, int csock, QTAILQ_INSERT_TAIL(&exp->clients, client, next); nbd_export_get(exp); } - return client; } diff --git a/qemu-nbd.c b/qemu-nbd.c index a4cf847..ede4a54 100644 --- a/qemu-nbd.c +++ b/qemu-nbd.c @@ -333,13 +333,9 @@ static void nbd_accept(void *opaque) return; } - if (nbd_client_new(exp, fd, nbd_client_closed)) { - nb_fds++; - nbd_update_server_fd_handler(server_fd); - } else { - shutdown(fd, 2); - close(fd); - } + nb_fds++; + nbd_update_server_fd_handler(server_fd); + nbd_client_new(exp, fd, nbd_client_closed); } static void nbd_update_server_fd_handler(int fd)