From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:36063) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aE9dh-0003pi-Eb for qemu-devel@nongnu.org; Wed, 30 Dec 2015 00:49:38 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aE9dg-0004lE-AN for qemu-devel@nongnu.org; Wed, 30 Dec 2015 00:49:37 -0500 From: Fam Zheng Date: Wed, 30 Dec 2015 13:49:25 +0800 Message-Id: <1451454566-15005-2-git-send-email-famz@redhat.com> In-Reply-To: <1451454566-15005-1-git-send-email-famz@redhat.com> References: <1451454566-15005-1-git-send-email-famz@redhat.com> Subject: [Qemu-devel] [PATCH 1/2] nbd: Interface tweak of nbd_client_new List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Kevin Wolf , Paolo Bonzini , qemu-block@nongnu.org In preparation for an async implementation, introduce a callback and move the shutdown/close to the function. Signed-off-by: Fam Zheng --- blockdev-nbd.c | 5 ++--- include/block/nbd.h | 6 ++++-- nbd.c | 20 +++++++++++++++----- qemu-nbd.c | 16 +++++++++------- 4 files changed, 30 insertions(+), 17 deletions(-) diff --git a/blockdev-nbd.c b/blockdev-nbd.c index bcdd18b..f708e0f 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, NULL); } } diff --git a/include/block/nbd.h b/include/block/nbd.h index 65f409d..11194e0 100644 --- a/include/block/nbd.h +++ b/include/block/nbd.h @@ -98,8 +98,10 @@ 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 *)); +typedef void (*NBDClientNewCB)(NBDExport *exp, int csock, int ret); +void nbd_client_new(NBDExport *exp, int csock, + void (*close_fn)(NBDClient *), + NBDClientNewCB cb); void nbd_client_get(NBDClient *client); void nbd_client_put(NBDClient *client); diff --git a/nbd.c b/nbd.c index b3d9654..bcb79d4 100644 --- a/nbd.c +++ b/nbd.c @@ -1475,9 +1475,13 @@ static void nbd_update_can_read(NBDClient *client) } } -NBDClient *nbd_client_new(NBDExport *exp, int csock, - void (*close)(NBDClient *)) +/* Create and initialize a new client. If it fails, @csock is closed. + * cb will be called with the status code when done. */ +void nbd_client_new(NBDExport *exp, int csock, + void (*close_fn)(NBDClient *), + NBDClientNewCB cb) { + int ret = 0; NBDClient *client; client = g_malloc0(sizeof(NBDClient)); client->refcount = 1; @@ -1485,10 +1489,13 @@ NBDClient *nbd_client_new(NBDExport *exp, int csock, client->sock = csock; client->can_read = true; if (nbd_send_negotiate(client)) { + shutdown(csock, 2); + close(csock); g_free(client); - return NULL; + ret = -1; + goto out; } - client->close = close; + client->close = close_fn; qemu_co_mutex_init(&client->send_lock); nbd_set_handlers(client); @@ -1496,5 +1503,8 @@ NBDClient *nbd_client_new(NBDExport *exp, int csock, QTAILQ_INSERT_TAIL(&exp->clients, client, next); nbd_export_get(exp); } - return client; +out: + if (cb) { + cb(exp, csock, ret); + } } diff --git a/qemu-nbd.c b/qemu-nbd.c index 65dc30c..bdec228 100644 --- a/qemu-nbd.c +++ b/qemu-nbd.c @@ -319,6 +319,14 @@ static void nbd_client_closed(NBDClient *client) nbd_client_put(client); } +static void nbd_client_new_cb(NBDExport *exp, int fd, int ret) +{ + if (ret == 0) { + nb_fds++; + nbd_update_server_fd_handler(server_fd); + } +} + static void nbd_accept(void *opaque) { struct sockaddr_in addr; @@ -335,13 +343,7 @@ 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); - } + nbd_client_new(exp, fd, nbd_client_closed, nbd_client_new_cb); } static void nbd_update_server_fd_handler(int fd) -- 2.4.3