From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] [PATCH 06/12] nbd: add reference counting to NBDExport
Date: Wed, 19 Sep 2012 15:49:50 +0200 [thread overview]
Message-ID: <1348062596-30446-7-git-send-email-pbonzini@redhat.com> (raw)
In-Reply-To: <1348062596-30446-1-git-send-email-pbonzini@redhat.com>
We will use a similar two-phase destruction for NBDExport, so we need
each NBDClient to add a reference to NBDExport.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
nbd.c | 37 +++++++++++++++++++++++++++++++------
nbd.h | 2 ++
qemu-nbd.c | 2 ++
3 file modificati, 35 inserzioni(+), 6 rimozioni(-)
diff --git a/nbd.c b/nbd.c
index eb72f4a..4922d38 100644
--- a/nbd.c
+++ b/nbd.c
@@ -89,6 +89,7 @@ struct NBDRequest {
};
struct NBDExport {
+ int refcount;
BlockDriverState *bs;
off_t dev_offset;
off_t size;
@@ -664,6 +665,7 @@ void nbd_client_put(NBDClient *client)
qemu_set_fd_handler2(client->sock, NULL, NULL, NULL, NULL);
close(client->sock);
client->sock = -1;
+ nbd_export_put(client->exp);
g_free(client);
}
}
@@ -722,6 +724,7 @@ NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset,
{
NBDExport *exp = g_malloc0(sizeof(NBDExport));
QSIMPLEQ_INIT(&exp->requests);
+ exp->refcount = 1;
exp->bs = bs;
exp->dev_offset = dev_offset;
exp->nbdflags = nbdflags;
@@ -731,14 +734,34 @@ NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset,
void nbd_export_close(NBDExport *exp)
{
- while (!QSIMPLEQ_EMPTY(&exp->requests)) {
- NBDRequest *first = QSIMPLEQ_FIRST(&exp->requests);
- QSIMPLEQ_REMOVE_HEAD(&exp->requests, entry);
- qemu_vfree(first->data);
- g_free(first);
+ assert(exp->refcount == 1);
+
+ /* stub */
+}
+
+void nbd_export_get(NBDExport *exp)
+{
+ assert(exp->refcount > 0);
+ exp->refcount++;
+}
+
+void nbd_export_put(NBDExport *exp)
+{
+ assert(exp->refcount > 0);
+ if (exp->refcount == 1) {
+ nbd_export_close(exp);
}
- g_free(exp);
+ if (--exp->refcount == 0) {
+ while (!QSIMPLEQ_EMPTY(&exp->requests)) {
+ NBDRequest *first = QSIMPLEQ_FIRST(&exp->requests);
+ QSIMPLEQ_REMOVE_HEAD(&exp->requests, entry);
+ qemu_vfree(first->data);
+ g_free(first);
+ }
+
+ g_free(exp);
+ }
}
static int nbd_can_read(void *opaque);
@@ -1011,5 +1034,7 @@ NBDClient *nbd_client_new(NBDExport *exp, int csock,
client->close = close;
qemu_co_mutex_init(&client->send_lock);
qemu_set_fd_handler2(csock, nbd_can_read, nbd_read, NULL, client);
+
+ nbd_export_get(exp);
return client;
}
diff --git a/nbd.h b/nbd.h
index 8b84a50..86921cd 100644
--- a/nbd.h
+++ b/nbd.h
@@ -81,6 +81,8 @@ typedef struct NBDClient NBDClient;
NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset,
off_t size, uint32_t nbdflags);
void nbd_export_close(NBDExport *exp);
+void nbd_export_get(NBDExport *exp);
+void nbd_export_put(NBDExport *exp);
NBDClient *nbd_client_new(NBDExport *exp, int csock,
void (*close)(NBDClient *));
diff --git a/qemu-nbd.c b/qemu-nbd.c
index 23392e0..2a2cba3 100644
--- a/qemu-nbd.c
+++ b/qemu-nbd.c
@@ -586,7 +586,9 @@ int main(int argc, char **argv)
} while (!sigterm_reported && (persistent || !nbd_started || nb_fds > 0));
nbd_export_close(exp);
+ nbd_export_put(exp);
bdrv_close(bs);
+
if (sockpath) {
unlink(sockpath);
}
--
1.7.12
next prev parent reply other threads:[~2012-09-19 13:50 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-09-19 13:49 [Qemu-devel] [PULL 00/12] NBD patches for 2012-09-22 Paolo Bonzini
2012-09-19 13:49 ` [Qemu-devel] [PATCH 01/12] nbd: add more constants Paolo Bonzini
2012-09-19 13:49 ` [Qemu-devel] [PATCH 02/12] nbd: pass NBDClient to nbd_send_negotiate Paolo Bonzini
2012-09-19 13:49 ` [Qemu-devel] [PATCH 03/12] nbd: do not close BlockDriverState in nbd_export_close Paolo Bonzini
2012-09-19 13:49 ` [Qemu-devel] [PATCH 04/12] nbd: make refcount interface public Paolo Bonzini
2012-09-19 13:49 ` [Qemu-devel] [PATCH 05/12] nbd: do not leak nbd_trip coroutines when a connection is torn down Paolo Bonzini
2012-09-19 13:49 ` Paolo Bonzini [this message]
2012-09-19 13:49 ` [Qemu-devel] [PATCH 07/12] nbd: track clients into NBDExport Paolo Bonzini
2012-09-19 13:49 ` [Qemu-devel] [PATCH 08/12] nbd: add notification for closing an NBDExport Paolo Bonzini
2012-09-19 13:49 ` [Qemu-devel] [PATCH 09/12] qemu-nbd: rewrite termination conditions to use a state machine Paolo Bonzini
2012-09-19 13:49 ` [Qemu-devel] [PATCH 10/12] nbd: register named exports Paolo Bonzini
2012-09-19 13:49 ` [Qemu-devel] [PATCH 11/12] nbd: negotiate with " Paolo Bonzini
2012-09-19 13:49 ` [Qemu-devel] [PATCH 12/12] nbd: add nbd_export_get_blockdev Paolo Bonzini
2012-09-25 23:27 ` [Qemu-devel] [PULL 00/12] NBD patches for 2012-09-22 Anthony Liguori
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=1348062596-30446-7-git-send-email-pbonzini@redhat.com \
--to=pbonzini@redhat.com \
--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).