[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH v8 03/36] block: Introduce image file locking
From: |
Fam Zheng |
Subject: |
[Qemu-devel] [PATCH v8 03/36] block: Introduce image file locking |
Date: |
Fri, 30 Sep 2016 20:09:33 +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 | 52 +++++++++++++++++++++++++++++++++++++++++++++++
include/block/block.h | 4 +++-
include/block/block_int.h | 5 +++++
include/hw/block/block.h | 2 ++
4 files changed, 62 insertions(+), 1 deletion(-)
diff --git a/block.c b/block.c
index 493ecf3..9d600df 100644
--- a/block.c
+++ b/block.c
@@ -235,6 +235,7 @@ BlockDriverState *bdrv_new(void)
notifier_with_return_list_init(&bs->before_write_notifiers);
bs->refcnt = 1;
bs->aio_context = qemu_get_aio_context();
+ bs->cur_lock = IMAGE_LOCK_MODE__MAX;
qemu_co_queue_init(&bs->flush_queue);
@@ -925,6 +926,48 @@ out:
g_free(gen_node_name);
}
+ImageLockMode bdrv_lock_mode_from_flags(int flags)
+{
+ if (flags & BDRV_O_NO_LOCK) {
+ return IMAGE_LOCK_MODE_NOLOCK;
+ } else if (flags & BDRV_O_SHARED_LOCK) {
+ return IMAGE_LOCK_MODE_SHARED;
+ } else if (flags & BDRV_O_EXCLUSIVE_LOCK) {
+ return IMAGE_LOCK_MODE_EXCLUSIVE;
+ } else {
+ return IMAGE_LOCK_MODE_AUTO;
+ }
+}
+
+ImageLockMode bdrv_get_lock_mode(BlockDriverState *bs)
+{
+ return bs->cur_lock;
+}
+
+int bdrv_set_lock_mode(BlockDriverState *bs, ImageLockMode mode)
+{
+ int ret;
+
+ if (bs->cur_lock == mode) {
+ return 0;
+ } else if (!bs->drv) {
+ return -ENOMEDIUM;
+ } else if (!bs->drv->bdrv_lockf) {
+ if (bs->file) {
+ return bdrv_set_lock_mode(bs->file->bs, mode);
+ }
+ return 0;
+ }
+ ret = bs->drv->bdrv_lockf(bs, mode);
+ if (ret == -ENOTSUP) {
+ /* Handle it the same way as !bs->drv->bdrv_lockf */
+ ret = 0;
+ } else if (ret == 0) {
+ bs->cur_lock = mode;
+ }
+ return ret;
+}
+
static QemuOptsList bdrv_runtime_opts = {
.name = "bdrv_common",
.head = QTAILQ_HEAD_INITIALIZER(bdrv_runtime_opts.head),
@@ -1076,6 +1119,10 @@ static int bdrv_open_common(BlockDriverState *bs,
BdrvChild *file,
goto free_and_fail;
}
+ if (open_flags & BDRV_O_INACTIVE) {
+ open_flags = (open_flags & ~BDRV_O_LOCK_MASK) & BDRV_O_NO_LOCK;
+ }
+
ret = refresh_total_sectors(bs, bs->total_sectors);
if (ret < 0) {
error_setg_errno(errp, -ret, "Could not refresh total sector count");
@@ -2273,6 +2320,7 @@ static void bdrv_close(BlockDriverState *bs)
if (bs->drv) {
BdrvChild *child, *next;
+ bdrv_set_lock_mode(bs, IMAGE_LOCK_MODE_NOLOCK);
bs->drv->bdrv_close(bs);
bs->drv = NULL;
@@ -3188,6 +3236,9 @@ void bdrv_invalidate_cache(BlockDriverState *bs, Error
**errp)
error_setg_errno(errp, -ret, "Could not refresh total sector count");
return;
}
+ if (bs->cur_lock != IMAGE_LOCK_MODE__MAX) {
+ bdrv_set_lock_mode(bs, bs->cur_lock);
+ }
}
void bdrv_invalidate_cache_all(Error **errp)
@@ -3230,6 +3281,7 @@ static int bdrv_inactivate_recurse(BlockDriverState *bs,
}
if (setting_flag) {
+ ret = bdrv_set_lock_mode(bs, IMAGE_LOCK_MODE_NOLOCK);
bs->open_flags |= BDRV_O_INACTIVE;
}
return 0;
diff --git a/include/block/block.h b/include/block/block.h
index cecab7a..828ab14 100644
--- a/include/block/block.h
+++ b/include/block/block.h
@@ -272,7 +272,8 @@ int bdrv_drop_intermediate(BlockDriverState *active,
BlockDriverState *top,
BlockDriverState *bdrv_find_overlay(BlockDriverState *active,
BlockDriverState *bs);
BlockDriverState *bdrv_find_base(BlockDriverState *bs);
-
+ImageLockMode bdrv_lock_mode_from_flags(int flags);
+ImageLockMode bdrv_get_lock_mode(BlockDriverState *bs);
typedef struct BdrvCheckResult {
int corruptions;
@@ -529,5 +530,6 @@ void bdrv_drained_end(BlockDriverState *bs);
void bdrv_add_child(BlockDriverState *parent, BlockDriverState *child,
Error **errp);
void bdrv_del_child(BlockDriverState *parent, BdrvChild *child, Error **errp);
+int bdrv_set_lock_mode(BlockDriverState *bs, ImageLockMode mode);
#endif
diff --git a/include/block/block_int.h b/include/block/block_int.h
index 3e79228..486e1ea 100644
--- a/include/block/block_int.h
+++ b/include/block/block_int.h
@@ -319,6 +319,10 @@ struct BlockDriver {
Error **errp);
void (*bdrv_del_child)(BlockDriverState *parent, BdrvChild *child,
Error **errp);
+ /**
+ * Lock/unlock the image.
+ */
+ int (*bdrv_lockf)(BlockDriverState *bs, ImageLockMode mode);
QLIST_ENTRY(BlockDriver) list;
};
@@ -528,6 +532,7 @@ struct BlockDriverState {
unsigned io_plug_disabled;
int quiesce_counter;
+ ImageLockMode cur_lock;
};
struct BlockBackendRootState {
diff --git a/include/hw/block/block.h b/include/hw/block/block.h
index df9d207..8c04469 100644
--- a/include/hw/block/block.h
+++ b/include/hw/block/block.h
@@ -12,11 +12,13 @@
#define HW_BLOCK_H
#include "qemu-common.h"
+#include "block/block.h"
/* Configuration */
typedef struct BlockConf {
BlockBackend *blk;
+ ImageLockMode lock_mode;
uint16_t physical_block_size;
uint16_t logical_block_size;
uint16_t min_io_size;
--
2.7.4
- [Qemu-devel] [PATCH v8 00/36] block: Image locking series, Fam Zheng, 2016/09/30
- [Qemu-devel] [PATCH v8 01/36] block: Add flag bits for image locking, Fam Zheng, 2016/09/30
- [Qemu-devel] [PATCH v8 02/36] qapi: Add ImageLockMode, Fam Zheng, 2016/09/30
- [Qemu-devel] [PATCH v8 03/36] block: Introduce image file locking,
Fam Zheng <=
- [Qemu-devel] [PATCH v8 04/36] osdep: Add qemu_lock_fd and qemu_unlock_fd, Fam Zheng, 2016/09/30
- [Qemu-devel] [PATCH v8 06/36] qemu-io: Add "-L" option for BDRV_O_NO_LOCK, Fam Zheng, 2016/09/30
- [Qemu-devel] [PATCH v8 05/36] raw-posix: Add image locking support, Fam Zheng, 2016/09/30
- [Qemu-devel] [PATCH v8 08/36] qemu-img: Update documentation of "-L" option, Fam Zheng, 2016/09/30
- [Qemu-devel] [PATCH v8 07/36] qemu-img: Add "-L" option to sub commands, Fam Zheng, 2016/09/30
- [Qemu-devel] [PATCH v8 10/36] block: Don't lock drive-backup target image in none mode, Fam Zheng, 2016/09/30
- [Qemu-devel] [PATCH v8 09/36] qemu-nbd: Add "--no-lock/-L" option, Fam Zheng, 2016/09/30
- [Qemu-devel] [PATCH v8 11/36] block: Add blk_lock_image, Fam Zheng, 2016/09/30
- [Qemu-devel] [PATCH v8 12/36] virtio-blk: Apply lock-mode when realize, Fam Zheng, 2016/09/30
- [Qemu-devel] [PATCH v8 13/36] scsi-disk: Apply lock-mode when realize, Fam Zheng, 2016/09/30