qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Fam Zheng <famz@redhat.com>
To: qemu-devel@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>,
	Paolo Bonzini <pbonzini@redhat.com>,
	qemu-block@nongnu.org
Subject: [Qemu-devel] [PATCH 1/2] nbd: Interface tweak of nbd_client_new
Date: Wed, 30 Dec 2015 13:49:25 +0800	[thread overview]
Message-ID: <1451454566-15005-2-git-send-email-famz@redhat.com> (raw)
In-Reply-To: <1451454566-15005-1-git-send-email-famz@redhat.com>

In preparation for an async implementation, introduce a callback and
move the shutdown/close to the function.

Signed-off-by: Fam Zheng <famz@redhat.com>
---
 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

  reply	other threads:[~2015-12-30  5:49 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-12-30  5:49 [Qemu-devel] [PATCH 0/2] nbd: Async built-in server negotiation Fam Zheng
2015-12-30  5:49 ` Fam Zheng [this message]
2016-01-08 16:24   ` [Qemu-devel] [PATCH 1/2] nbd: Interface tweak of nbd_client_new Daniel P. Berrange
2016-01-08 18:24     ` Paolo Bonzini
2016-01-11  3:20       ` Fam Zheng
2015-12-30  5:49 ` [Qemu-devel] [PATCH 2/2] nbd: Coroutine based nbd_send_negotiate Fam Zheng

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1451454566-15005-2-git-send-email-famz@redhat.com \
    --to=famz@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).