[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-block] [PATCH v4 04/27] block: Introduce image file locking
From: |
Fam Zheng |
Subject: |
[Qemu-block] [PATCH v4 04/27] block: Introduce image file locking |
Date: |
Tue, 10 May 2016 10:50:36 +0800 |
Block drivers can implement this new operation .bdrv_lockf to actually lock the
image in the protocol specific way.
Signed-off-by: Fam Zheng <address@hidden>
---
block.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
include/block/block_int.h | 12 ++++++++++++
2 files changed, 59 insertions(+)
diff --git a/block.c b/block.c
index d4939b4..16c7d58 100644
--- a/block.c
+++ b/block.c
@@ -846,6 +846,40 @@ out:
g_free(gen_node_name);
}
+static int bdrv_lock_unlock_image_do(BlockDriverState *bs, bool lock_image)
+{
+ int cmd = BDRV_LOCKF_UNLOCK;
+ int ret;
+
+ if (bs->image_locked == lock_image) {
+ return 0;
+ } else if (!bs->drv) {
+ return -ENOMEDIUM;
+ } else if (!bs->drv->bdrv_lockf) {
+ return 0;
+ }
+ if (lock_image) {
+ cmd = bs->open_flags & BDRV_O_RDWR ? BDRV_LOCKF_RWLOCK :
+ BDRV_LOCKF_ROLOCK;
+ }
+ ret = bs->drv->bdrv_lockf(bs, cmd);
+ if (ret == -ENOTSUP) {
+ /* Handle it the same way as !bs->drv->bdrv_lockf */
+ ret = 0;
+ }
+ return ret;
+}
+
+static int bdrv_lock_image(BlockDriverState *bs)
+{
+ return bdrv_lock_unlock_image_do(bs, true);
+}
+
+static int bdrv_unlock_image(BlockDriverState *bs)
+{
+ return bdrv_lock_unlock_image_do(bs, false);
+}
+
static QemuOptsList bdrv_runtime_opts = {
.name = "bdrv_common",
.head = QTAILQ_HEAD_INITIALIZER(bdrv_runtime_opts.head),
@@ -995,6 +1029,14 @@ static int bdrv_open_common(BlockDriverState *bs,
BdrvChild *file,
goto free_and_fail;
}
+ if (!(open_flags & (BDRV_O_NO_LOCK | BDRV_O_INACTIVE))) {
+ ret = bdrv_lock_image(bs);
+ if (ret) {
+ error_setg(errp, "Failed to lock image");
+ goto free_and_fail;
+ }
+ }
+
ret = refresh_total_sectors(bs, bs->total_sectors);
if (ret < 0) {
error_setg_errno(errp, -ret, "Could not refresh total sector count");
@@ -2144,6 +2186,7 @@ static void bdrv_close(BlockDriverState *bs)
if (bs->drv) {
BdrvChild *child, *next;
+ bdrv_unlock_image(bs);
bs->drv->bdrv_close(bs);
bs->drv = NULL;
@@ -3230,6 +3273,9 @@ void bdrv_invalidate_cache(BlockDriverState *bs, Error
**errp)
error_setg_errno(errp, -ret, "Could not refresh total sector count");
return;
}
+ if (!(bs->open_flags & BDRV_O_NO_LOCK)) {
+ bdrv_lock_image(bs);
+ }
}
void bdrv_invalidate_cache_all(Error **errp)
@@ -3261,6 +3307,7 @@ static int bdrv_inactivate(BlockDriverState *bs)
}
}
+ ret = bdrv_unlock_image(bs);
bs->open_flags |= BDRV_O_INACTIVE;
return 0;
}
diff --git a/include/block/block_int.h b/include/block/block_int.h
index 10d8759..ffa30b0 100644
--- a/include/block/block_int.h
+++ b/include/block/block_int.h
@@ -85,6 +85,12 @@ typedef struct BdrvTrackedRequest {
struct BdrvTrackedRequest *waiting_for;
} BdrvTrackedRequest;
+typedef enum {
+ BDRV_LOCKF_RWLOCK,
+ BDRV_LOCKF_ROLOCK,
+ BDRV_LOCKF_UNLOCK,
+} BdrvLockfCmd;
+
struct BlockDriver {
const char *format_name;
int instance_size;
@@ -317,6 +323,11 @@ struct BlockDriver {
*/
void (*bdrv_drain)(BlockDriverState *bs);
+ /**
+ * Lock/unlock the image.
+ */
+ int (*bdrv_lockf)(BlockDriverState *bs, BdrvLockfCmd cmd);
+
QLIST_ENTRY(BlockDriver) list;
};
@@ -485,6 +496,7 @@ struct BlockDriverState {
NotifierWithReturn write_threshold_notifier;
int quiesce_counter;
+ bool image_locked;
};
struct BlockBackendRootState {
--
2.8.2
- [Qemu-block] [PATCH v4 00/27] block: Lock images when opening, Fam Zheng, 2016/05/09
- [Qemu-block] [PATCH v4 01/27] block: Add BDRV_O_NO_LOCK, Fam Zheng, 2016/05/09
- [Qemu-block] [PATCH v4 02/27] qapi: Add lock-image in blockdev-add options, Fam Zheng, 2016/05/09
- [Qemu-block] [PATCH v4 03/27] blockdev: Add and parse "lock-image" option for block devices, Fam Zheng, 2016/05/09
- [Qemu-block] [PATCH v4 04/27] block: Introduce image file locking,
Fam Zheng <=
- [Qemu-block] [PATCH v4 05/27] block: Add bdrv_image_locked, Fam Zheng, 2016/05/09
- [Qemu-block] [PATCH v4 06/27] block: Make bdrv_reopen_{commit, abort} private functions, Fam Zheng, 2016/05/09
- [Qemu-block] [PATCH v4 07/27] block: Handle image locking during reopen, Fam Zheng, 2016/05/09
- [Qemu-block] [PATCH v4 08/27] osdep: Add qemu_lock_fd and qemu_unlock_fd, Fam Zheng, 2016/05/09