Patchwork [07/12] nbd: track clients into NBDExport

login
register
mail settings
Submitter Paolo Bonzini
Date Sept. 19, 2012, 1:49 p.m.
Message ID <1348062596-30446-8-git-send-email-pbonzini@redhat.com>
Download mbox | patch
Permalink /patch/185064/
State New
Headers show

Comments

Paolo Bonzini - Sept. 19, 2012, 1:49 p.m.
Track the NBDClients of each NBDExport, and use it to implement
nbd_export_close.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 nbd.c | 13 +++++++++++--
 1 file modificato, 11 inserzioni(+), 2 rimozioni(-)

Patch

diff --git a/nbd.c b/nbd.c
index 4922d38..c39692b 100644
--- a/nbd.c
+++ b/nbd.c
@@ -94,6 +94,7 @@  struct NBDExport {
     off_t dev_offset;
     off_t size;
     uint32_t nbdflags;
+    QTAILQ_HEAD(, NBDClient) clients;
     QSIMPLEQ_HEAD(, NBDRequest) requests;
 };
 
@@ -109,6 +110,7 @@  struct NBDClient {
     CoMutex send_lock;
     Coroutine *send_coroutine;
 
+    QTAILQ_ENTRY(NBDClient) next;
     int nb_requests;
     bool closing;
 };
@@ -665,6 +667,7 @@  void nbd_client_put(NBDClient *client)
         qemu_set_fd_handler2(client->sock, NULL, NULL, NULL, NULL);
         close(client->sock);
         client->sock = -1;
+        QTAILQ_REMOVE(&client->exp->clients, client, next);
         nbd_export_put(client->exp);
         g_free(client);
     }
@@ -725,6 +728,7 @@  NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset,
     NBDExport *exp = g_malloc0(sizeof(NBDExport));
     QSIMPLEQ_INIT(&exp->requests);
     exp->refcount = 1;
+    QTAILQ_INIT(&exp->clients);
     exp->bs = bs;
     exp->dev_offset = dev_offset;
     exp->nbdflags = nbdflags;
@@ -734,9 +738,13 @@  NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset,
 
 void nbd_export_close(NBDExport *exp)
 {
-    assert(exp->refcount == 1);
+    NBDClient *client, *next;
 
-    /* stub */
+    nbd_export_get(exp);
+    QTAILQ_FOREACH_SAFE(client, &exp->clients, next, next) {
+        nbd_client_close(client);
+    }
+    nbd_export_put(exp);
 }
 
 void nbd_export_get(NBDExport *exp)
@@ -1035,6 +1043,7 @@  NBDClient *nbd_client_new(NBDExport *exp, int csock,
     qemu_co_mutex_init(&client->send_lock);
     qemu_set_fd_handler2(csock, nbd_can_read, nbd_read, NULL, client);
 
+    QTAILQ_INSERT_TAIL(&exp->clients, client, next);
     nbd_export_get(exp);
     return client;
 }