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