9ae3a8
From 6c473bbd42becf52b10677500bb509fbfa077140 Mon Sep 17 00:00:00 2001
9ae3a8
From: Fam Zheng <famz@redhat.com>
9ae3a8
Date: Fri, 17 Jan 2014 03:07:47 +0100
9ae3a8
Subject: [PATCH 04/34] block: implement reference count for BlockDriverState
9ae3a8
9ae3a8
RH-Author: Fam Zheng <famz@redhat.com>
9ae3a8
Message-id: <1389928083-8921-3-git-send-email-famz@redhat.com>
9ae3a8
Patchwork-id: 56764
9ae3a8
O-Subject: [RHEL-7 qemu-kvm PATCH 02/18] block: implement reference count for BlockDriverState
9ae3a8
Bugzilla: 1041301
9ae3a8
RH-Acked-by: Jeffrey Cody <jcody@redhat.com>
9ae3a8
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
9ae3a8
Introduce bdrv_ref/bdrv_unref to manage the lifecycle of
9ae3a8
BlockDriverState. They are unused for now but will used to replace
9ae3a8
bdrv_delete() later.
9ae3a8
9ae3a8
Signed-off-by: Fam Zheng <famz@redhat.com>
9ae3a8
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
(cherry picked from commit 9fcb025146676ab376e6159b58f5a5ddb67bf03c)
9ae3a8
Signed-off-by: Fam Zheng <famz@redhat.com>
9ae3a8
9ae3a8
Conflicts:
9ae3a8
	block.c
9ae3a8
        Context conflict because throttling and before_write notifier
9ae3a8
        are not backported yet.
9ae3a8
9ae3a8
Signed-off-by: Fam Zheng <famz@redhat.com>
9ae3a8
---
9ae3a8
 block.c                   | 21 +++++++++++++++++++++
9ae3a8
 include/block/block.h     |  2 ++
9ae3a8
 include/block/block_int.h |  1 +
9ae3a8
 3 files changed, 24 insertions(+)
9ae3a8
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
---
9ae3a8
 block.c                   |   21 +++++++++++++++++++++
9ae3a8
 include/block/block.h     |    2 ++
9ae3a8
 include/block/block_int.h |    1 +
9ae3a8
 3 files changed, 24 insertions(+), 0 deletions(-)
9ae3a8
9ae3a8
diff --git a/block.c b/block.c
9ae3a8
index cc7afd4..ddf29d0 100644
9ae3a8
--- a/block.c
9ae3a8
+++ b/block.c
9ae3a8
@@ -301,6 +301,7 @@ BlockDriverState *bdrv_new(const char *device_name)
9ae3a8
     }
9ae3a8
     bdrv_iostatus_disable(bs);
9ae3a8
     notifier_list_init(&bs->close_notifiers);
9ae3a8
+    bs->refcnt = 1;
9ae3a8
 
9ae3a8
     return bs;
9ae3a8
 }
9ae3a8
@@ -1580,6 +1581,9 @@ static void bdrv_move_feature_fields(BlockDriverState *bs_dest,
9ae3a8
     /* dirty bitmap */
9ae3a8
     bs_dest->dirty_bitmap       = bs_src->dirty_bitmap;
9ae3a8
 
9ae3a8
+    /* reference count */
9ae3a8
+    bs_dest->refcnt             = bs_src->refcnt;
9ae3a8
+
9ae3a8
     /* job */
9ae3a8
     bs_dest->in_use             = bs_src->in_use;
9ae3a8
     bs_dest->job                = bs_src->job;
9ae3a8
@@ -4754,6 +4758,23 @@ int64_t bdrv_get_dirty_count(BlockDriverState *bs)
9ae3a8
     }
9ae3a8
 }
9ae3a8
 
9ae3a8
+/* Get a reference to bs */
9ae3a8
+void bdrv_ref(BlockDriverState *bs)
9ae3a8
+{
9ae3a8
+    bs->refcnt++;
9ae3a8
+}
9ae3a8
+
9ae3a8
+/* Release a previously grabbed reference to bs.
9ae3a8
+ * If after releasing, reference count is zero, the BlockDriverState is
9ae3a8
+ * deleted. */
9ae3a8
+void bdrv_unref(BlockDriverState *bs)
9ae3a8
+{
9ae3a8
+    assert(bs->refcnt > 0);
9ae3a8
+    if (--bs->refcnt == 0) {
9ae3a8
+        bdrv_delete(bs);
9ae3a8
+    }
9ae3a8
+}
9ae3a8
+
9ae3a8
 void bdrv_set_in_use(BlockDriverState *bs, int in_use)
9ae3a8
 {
9ae3a8
     assert(bs->in_use != in_use);
9ae3a8
diff --git a/include/block/block.h b/include/block/block.h
9ae3a8
index e7f718c..a3b7395 100644
9ae3a8
--- a/include/block/block.h
9ae3a8
+++ b/include/block/block.h
9ae3a8
@@ -421,6 +421,8 @@ int64_t bdrv_get_dirty_count(BlockDriverState *bs);
9ae3a8
 void bdrv_enable_copy_on_read(BlockDriverState *bs);
9ae3a8
 void bdrv_disable_copy_on_read(BlockDriverState *bs);
9ae3a8
 
9ae3a8
+void bdrv_ref(BlockDriverState *bs);
9ae3a8
+void bdrv_unref(BlockDriverState *bs);
9ae3a8
 void bdrv_set_in_use(BlockDriverState *bs, int in_use);
9ae3a8
 int bdrv_in_use(BlockDriverState *bs);
9ae3a8
 
9ae3a8
diff --git a/include/block/block_int.h b/include/block/block_int.h
9ae3a8
index d49a317..fbfdab7 100644
9ae3a8
--- a/include/block/block_int.h
9ae3a8
+++ b/include/block/block_int.h
9ae3a8
@@ -315,6 +315,7 @@ struct BlockDriverState {
9ae3a8
     BlockDeviceIoStatus iostatus;
9ae3a8
     char device_name[32];
9ae3a8
     HBitmap *dirty_bitmap;
9ae3a8
+    int refcnt;
9ae3a8
     int in_use; /* users other than guest access, eg. block migration */
9ae3a8
     QTAILQ_ENTRY(BlockDriverState) list;
9ae3a8
 
9ae3a8
-- 
9ae3a8
1.7.1
9ae3a8