[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH for-2.7 04/15] block: Introduce image file locking i
From: |
Fam Zheng |
Subject: |
[Qemu-devel] [PATCH for-2.7 04/15] block: Introduce image file locking interface |
Date: |
Wed, 13 Apr 2016 17:09:53 +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 | 25 +++++++++++++++++++++++++
include/block/block.h | 8 ++++++++
include/block/block_int.h | 5 +++++
3 files changed, 38 insertions(+)
diff --git a/block.c b/block.c
index d4939b4..7f6e903 100644
--- a/block.c
+++ b/block.c
@@ -995,6 +995,15 @@ static int bdrv_open_common(BlockDriverState *bs,
BdrvChild *file,
goto free_and_fail;
}
+ if (!(open_flags & BDRV_O_NO_LOCK)) {
+ BdrvLockfCmd cmd = open_flags & BDRV_O_RDWR ? BDRV_LOCKF_RWLOCK :
+ BDRV_LOCKF_ROLOCK;
+ ret = bdrv_lockf(bs, cmd, errp);
+ if (ret != 0 && ret != -ENOTSUP) {
+ 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");
@@ -2141,6 +2150,10 @@ static void bdrv_close(BlockDriverState *bs)
blk_dev_change_media_cb(bs->blk, false);
}
+ if (bs->drv && !(bs->open_flags & BDRV_O_NO_LOCK)) {
+ bdrv_lockf(bs, BDRV_LOCKF_UNLOCK, &error_abort);
+ }
+
if (bs->drv) {
BdrvChild *child, *next;
@@ -3981,3 +3994,15 @@ void bdrv_refresh_filename(BlockDriverState *bs)
QDECREF(json);
}
}
+
+int bdrv_lockf(BlockDriverState *bs, BdrvLockfCmd cmd, Error **errp)
+{
+ if (!bs->drv) {
+ error_setg(errp, "No medium to lock");
+ return -ENOMEDIUM;
+ } else if (!bs->drv->bdrv_lockf) {
+ return -ENOTSUP;
+ } else {
+ return bs->drv->bdrv_lockf(bs, cmd, errp);
+ }
+}
diff --git a/include/block/block.h b/include/block/block.h
index b803597..36e1271 100644
--- a/include/block/block.h
+++ b/include/block/block.h
@@ -542,4 +542,12 @@ void bdrv_drained_begin(BlockDriverState *bs);
*/
void bdrv_drained_end(BlockDriverState *bs);
+typedef enum {
+ BDRV_LOCKF_RWLOCK,
+ BDRV_LOCKF_ROLOCK,
+ BDRV_LOCKF_UNLOCK,
+} BdrvLockfCmd;
+
+int bdrv_lockf(BlockDriverState *bs, BdrvLockfCmd cmd, Error **errp);
+
#endif
diff --git a/include/block/block_int.h b/include/block/block_int.h
index 10d8759..0a79eba 100644
--- a/include/block/block_int.h
+++ b/include/block/block_int.h
@@ -317,6 +317,11 @@ struct BlockDriver {
*/
void (*bdrv_drain)(BlockDriverState *bs);
+ /**
+ * Lock/unlock the image.
+ */
+ int (*bdrv_lockf)(BlockDriverState *bs, BdrvLockfCmd cmd, Error **errp);
+
QLIST_ENTRY(BlockDriver) list;
};
--
2.8.0
- [Qemu-devel] [PATCH for-2.7 00/15] block: Lock images when opening, Fam Zheng, 2016/04/13
- [Qemu-devel] [PATCH for-2.7 01/15] block: Add BDRV_O_NO_LOCK, Fam Zheng, 2016/04/13
- [Qemu-devel] [PATCH for-2.7 02/15] qapi: Add lock-image in blockdev-add options, Fam Zheng, 2016/04/13
- [Qemu-devel] [PATCH for-2.7 03/15] blockdev: Add and parse "lock-image" option for block devices, Fam Zheng, 2016/04/13
- [Qemu-devel] [PATCH for-2.7 04/15] block: Introduce image file locking interface,
Fam Zheng <=
- [Qemu-devel] [PATCH for-2.7 05/15] raw-posix: Implement .bdrv_lockf, Fam Zheng, 2016/04/13
- [Qemu-devel] [PATCH for-2.7 07/15] qemu-io: Add "-L" option for BDRV_O_NO_LOCK, Fam Zheng, 2016/04/13
- [Qemu-devel] [PATCH for-2.7 06/15] gluster: Implement .bdrv_lockf, Fam Zheng, 2016/04/13
- [Qemu-devel] [PATCH for-2.7 08/15] qemu-iotests: 140: Disable image lock for qemu-io access, Fam Zheng, 2016/04/13
- [Qemu-devel] [PATCH for-2.7 09/15] qemu-iotests: 046: Move version detection out from verify_io, Fam Zheng, 2016/04/13
- [Qemu-devel] [PATCH for-2.7 11/15] qemu-iotests: Disable image lock when checking test image, Fam Zheng, 2016/04/13