Patchwork [08/12] nbd: add notification for closing an NBDExport

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

Comments

Paolo Bonzini - Sept. 19, 2012, 1:49 p.m.
In order to exit cleanly from qemu-nbd, add a callback that triggers
when an NBDExport is closed.  In the case of qemu-nbd it will exit the
main loop.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 nbd.c      | 10 +++++++++-
 nbd.h      |  3 ++-
 qemu-nbd.c |  2 +-
 3 file modificati, 12 inserzioni(+), 3 rimozioni(-)

Patch

diff --git a/nbd.c b/nbd.c
index c39692b..2e9de70 100644
--- a/nbd.c
+++ b/nbd.c
@@ -90,6 +90,8 @@  struct NBDRequest {
 
 struct NBDExport {
     int refcount;
+    void (*close)(NBDExport *exp);
+
     BlockDriverState *bs;
     off_t dev_offset;
     off_t size;
@@ -723,7 +725,8 @@  static void nbd_request_put(NBDRequest *req)
 }
 
 NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset,
-                          off_t size, uint32_t nbdflags)
+                          off_t size, uint32_t nbdflags,
+                          void (*close)(NBDExport *))
 {
     NBDExport *exp = g_malloc0(sizeof(NBDExport));
     QSIMPLEQ_INIT(&exp->requests);
@@ -733,6 +736,7 @@  NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset,
     exp->dev_offset = dev_offset;
     exp->nbdflags = nbdflags;
     exp->size = size == -1 ? bdrv_getlength(bs) : size;
+    exp->close = close;
     return exp;
 }
 
@@ -761,6 +765,10 @@  void nbd_export_put(NBDExport *exp)
     }
 
     if (--exp->refcount == 0) {
+        if (exp->close) {
+            exp->close(exp);
+        }
+
         while (!QSIMPLEQ_EMPTY(&exp->requests)) {
             NBDRequest *first = QSIMPLEQ_FIRST(&exp->requests);
             QSIMPLEQ_REMOVE_HEAD(&exp->requests, entry);
diff --git a/nbd.h b/nbd.h
index 86921cd..895820b 100644
--- a/nbd.h
+++ b/nbd.h
@@ -79,7 +79,8 @@  typedef struct NBDExport NBDExport;
 typedef struct NBDClient NBDClient;
 
 NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset,
-                          off_t size, uint32_t nbdflags);
+                          off_t size, uint32_t nbdflags,
+                          void (*close)(NBDExport *));
 void nbd_export_close(NBDExport *exp);
 void nbd_export_get(NBDExport *exp);
 void nbd_export_put(NBDExport *exp);
diff --git a/qemu-nbd.c b/qemu-nbd.c
index 2a2cba3..8b87dea 100644
--- a/qemu-nbd.c
+++ b/qemu-nbd.c
@@ -546,7 +546,7 @@  int main(int argc, char **argv)
         }
     }
 
-    exp = nbd_export_new(bs, dev_offset, fd_size, nbdflags);
+    exp = nbd_export_new(bs, dev_offset, fd_size, nbdflags, NULL);
 
     if (sockpath) {
         fd = unix_socket_incoming(sockpath);