From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:38213) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1T60o8-0005ZF-6y for qemu-devel@nongnu.org; Mon, 27 Aug 2012 11:00:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1T60ny-0005cD-I0 for qemu-devel@nongnu.org; Mon, 27 Aug 2012 11:00:52 -0400 Received: from mail-ey0-f173.google.com ([209.85.215.173]:39158) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1T60ny-0005bG-B8 for qemu-devel@nongnu.org; Mon, 27 Aug 2012 11:00:42 -0400 Received: by mail-ey0-f173.google.com with SMTP id c13so1273715eaa.4 for ; Mon, 27 Aug 2012 08:00:41 -0700 (PDT) Sender: Paolo Bonzini From: Paolo Bonzini Date: Mon, 27 Aug 2012 17:00:17 +0200 Message-Id: <1346079626-16386-5-git-send-email-pbonzini@redhat.com> In-Reply-To: <1346079626-16386-1-git-send-email-pbonzini@redhat.com> References: <1346079626-16386-1-git-send-email-pbonzini@redhat.com> Subject: [Qemu-devel] [RFC PATCH 04/13] nbd: close all clients on deleting export List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: kwolf@redhat.com, stefanha@gmail.com Clients have a pointer to the NBDExport that they serve. Do not let a dangling pointer escape. Also flush all pending I/O so that coroutines are forced to exit. Signed-off-by: Paolo Bonzini --- nbd.c | 14 ++++++++++++++ 1 file modificato, 14 inserzioni(+) diff --git a/nbd.c b/nbd.c index cb4e8fe..f6eaea3 100644 --- a/nbd.c +++ b/nbd.c @@ -93,6 +93,7 @@ struct NBDExport { off_t dev_offset; off_t size; uint32_t nbdflags; + QTAILQ_HEAD(, NBDClient) clients; QSIMPLEQ_HEAD(, NBDRequest) requests; }; @@ -108,6 +109,7 @@ struct NBDClient { CoMutex send_lock; Coroutine *send_coroutine; + QTAILQ_ENTRY(NBDClient) next; int nb_requests; }; @@ -658,6 +660,9 @@ static void nbd_client_put(NBDClient *client) { if (--client->refcount == 0) { qemu_set_fd_handler2(client->sock, NULL, NULL, NULL, NULL); + if (client->exp) { + QTAILQ_REMOVE(&client->exp->clients, client, next); + } close(client->sock); client->sock = -1; if (client->close) { @@ -711,6 +716,7 @@ NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset, { NBDExport *exp = g_malloc0(sizeof(NBDExport)); QSIMPLEQ_INIT(&exp->requests); + QTAILQ_INIT(&exp->clients); exp->bs = bs; exp->dev_offset = dev_offset; exp->nbdflags = nbdflags; @@ -720,6 +726,11 @@ NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset, void nbd_export_close(NBDExport *exp) { + while (!QTAILQ_EMPTY(&exp->clients)) { + NBDClient *client = QTAILQ_FIRST(&exp->clients); + nbd_client_close(client); + } + while (!QSIMPLEQ_EMPTY(&exp->requests)) { NBDRequest *first = QSIMPLEQ_FIRST(&exp->requests); QSIMPLEQ_REMOVE_HEAD(&exp->requests, entry); @@ -995,6 +1006,9 @@ NBDClient *nbd_client_new(NBDExport *exp, int csock, return NULL; } client->close = close; + if (exp) { + QTAILQ_INSERT_TAIL(&exp->clients, client, next); + } qemu_co_mutex_init(&client->send_lock); qemu_set_fd_handler2(csock, nbd_can_read, nbd_read, NULL, client); return client; -- 1.7.11.2