qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PATCH v2 08/13] block: Add bdrv_add_lock_unlock_notifier


From: Fam Zheng
Subject: [Qemu-devel] [PATCH v2 08/13] block: Add bdrv_add_lock_unlock_notifier
Date: Tue, 2 Jun 2015 11:21:57 +0800

Callers will be notified upon bdrv_lock if it was not locked, and
bdrv_unlock if it becomes unlocked.

It can be used to pause and resume devices.

Signed-off-by: Fam Zheng <address@hidden>
---
 block.c                   |  1 +
 block/io.c                | 19 +++++++++++++++++++
 include/block/block.h     | 12 ++++++++++++
 include/block/block_int.h |  1 +
 4 files changed, 33 insertions(+)

diff --git a/block.c b/block.c
index 0e25dbd..c0301d2 100644
--- a/block.c
+++ b/block.c
@@ -1853,6 +1853,7 @@ static void bdrv_move_feature_fields(BlockDriverState 
*bs_dest,
 
     /* lock */
     bs_dest->lock_level = bs_src->lock_level;
+    bs_dest->lock_notifiers = bs_src->lock_notifiers;
 
     memcpy(bs_dest->op_blockers, bs_src->op_blockers,
            sizeof(bs_dest->op_blockers));
diff --git a/block/io.c b/block/io.c
index 49060e5..8210236 100644
--- a/block/io.c
+++ b/block/io.c
@@ -2602,8 +2602,20 @@ void bdrv_flush_io_queue(BlockDriverState *bs)
     }
 }
 
+static void bdrv_lock_notify(BlockDriverState *bs, bool locking)
+{
+    BdrvLockEvent event = (BdrvLockEvent) {
+        .bs = bs,
+        .locking = locking,
+    };
+    notifier_list_notify(&bs->lock_notifiers, &event);
+}
+
 void bdrv_lock(BlockDriverState *bs)
 {
+    if (bs->lock_level == 0) {
+        bdrv_lock_notify(bs, true);
+    }
     bs->lock_level++;
     bdrv_drain(bs);
 }
@@ -2612,4 +2624,11 @@ void bdrv_unlock(BlockDriverState *bs)
 {
     bs->lock_level--;
     assert(bs->lock_level >= 0);
+    if (bs->lock_level == 0) {
+        bdrv_lock_notify(bs, false);
+    }
+}
+void bdrv_add_lock_unlock_notifier(BlockDriverState *bs, Notifier *notifier)
+{
+    notifier_list_add(&bs->lock_notifiers, notifier);
 }
diff --git a/include/block/block.h b/include/block/block.h
index b49194d..c17e3ad 100644
--- a/include/block/block.h
+++ b/include/block/block.h
@@ -606,6 +606,18 @@ void bdrv_lock(BlockDriverState *bs);
  */
 void bdrv_unlock(BlockDriverState *bs);
 
+typedef struct {
+    BlockDriverState *bs;
+    bool locking;
+} BdrvLockEvent;
+
+/**
+ * bdrv_add_lock_unlock_notifier:
+ *
+ * Add a notifier that will get notified when bs is locked or unlocked, with a
+ * BdrvLockEvent data.
+ */
+void bdrv_add_lock_unlock_notifier(BlockDriverState *bs, Notifier *notifier);
 BlockAcctStats *bdrv_get_stats(BlockDriverState *bs);
 
 #endif
diff --git a/include/block/block_int.h b/include/block/block_int.h
index 9f75d46..921b01a 100644
--- a/include/block/block_int.h
+++ b/include/block/block_int.h
@@ -435,6 +435,7 @@ struct BlockDriverState {
     NotifierWithReturn write_threshold_notifier;
 
     int lock_level;
+    NotifierList lock_notifiers;
 };
 
 
-- 
2.4.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]