Make image fleecing more usable
Subsystems (2)
cover Make image fleecing more usable
Hi all!
That's an untested draft. I'll be on vocation 05-22, so no reason for this just lay in my hard drive. Any comments are welcome (mostly about general design), but don't waste time on careful reviewing.
What this series brings to image-fleecing:
1. support for bitmap (patch 04). So, we can do incremental external backups and not do extra copy-before-write operations for non-dirty regions.
2. fleecing block driver - see lat patch for details and list of benefits.
Based-on: <[email protected]>
([PATCH v7 00/33] block: publish backup-top filter)
Vladimir Sementsov-Ogievskiy (11):
block/block-copy: move copy_bitmap initialization to
block_copy_state_new()
block/dirty-bitmap: bdrv_merge_dirty_bitmap(): add return value block/block-copy: block_copy_state_new(): add bitmap parameter block/copy-before-write: add bitmap open parameter block/block-copy: add block_copy_reset()
block: intoduce reqlist
block/dirty-bitmap: introduce bdrv_dirty_bitmap_status() block/reqlist: add reqlist_wait_all() block/copy-before-write: add cbw_snapshot_read_{lock,unlock}() block/copy-before-write: add cbw_snapshot_discard()
block: introduce fleecing block driver
qapi/block-core.json | 10 +- block/copy-before-write.h | 6 ++ include/block/block-copy.h | 3 +- include/block/dirty-bitmap.h | 4 +- include/block/reqlist.h | 47 ++++++++ include/qemu/hbitmap.h | 11 ++ block/block-copy.c | 152 +++++++++++--------------- block/copy-before-write.c | 143 ++++++++++++++++++++++++- block/dirty-bitmap.c | 14 ++- block/fleecing.c | 183 ++++++++++++++++++++++++++++++++ block/monitor/bitmap-qmp-cmds.c | 5 +- block/reqlist.c | 100 +++++++++++++++++ util/hbitmap.c | 36 +++++++ block/meson.build | 2 + 14 files changed, 613 insertions(+), 103 deletions(-) create mode 100644 include/block/reqlist.h create mode 100644 block/fleecing.c create mode 100644 block/reqlist.c
Signature · 1 line
2.29.2
PATCH 1/11 block/block-copy: move copy_bitmap initialization to block_copy_state_new()
We are going to complicate bitmap initialization in the following commit. And in future, backup job will be able to work without filter (when source is immutable), so we'll need same bitmap initialization in copy-before-write filter and in backup job. So, it's reasonable to do it in block-copy.
--- block/block-copy.c | 1 + block/copy-before-write.c | 4 ---- 2 files changed, 1 insertion(+), 4 deletions(-)
block/block-copy.c +1 -0
@@ -418,6 +418,7 @@ BlockCopyState *block_copy_state_new(BdrvChild *source, BdrvChild *target,return NULL ; }bdrv_disable_dirty_bitmap (copy_bitmap); +bdrv_set_dirty_bitmap (copy_bitmap,0 ,bdrv_dirty_bitmap_size (copy_bitmap)); s =g_new (BlockCopyState,1 ); *s = (BlockCopyState ) {
block/copy-before-write.c +0 -4
@@ -148,7 +148,6 @@ static int cbw_open(BlockDriverState *bs, QDict *options, int flags, Error **errp) {BDRVCopyBeforeWriteState *s = bs->opaque; -BdrvDirtyBitmap *copy_bitmap; bs->file = bdrv_open_child(NULL, options, "file", bs, &child_of_bds, BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY,
@@ -176,9 +175,6 @@ static int cbw_open(BlockDriverState *bs, QDict *options, int flags,return -EINVAL ; } - copy_bitmap =block_copy_dirty_bitmap (s->bcs); -bdrv_set_dirty_bitmap (copy_bitmap,0 ,bdrv_dirty_bitmap_size (copy_bitmap)); -return 0 ; }
Signature · 1 line
2.29.2
PATCH 2/11 block/dirty-bitmap: bdrv_merge_dirty_bitmap(): add return value
In reply to Vladimir Sementsov-OgievskiyThat simplifies handling failure in existing code and in further new usage of bdrv_merge_dirty_bitmap().
--- include/block/dirty-bitmap.h | 2 +- block/dirty-bitmap.c | 8 ++++++-- block/monitor/bitmap-qmp-cmds.c | 5 +---- 3 files changed, 8 insertions(+), 7 deletions(-)
include/block/dirty-bitmap.h +1 -1
@@ -77,7 +77,7 @@ void bdrv_dirty_bitmap_set_persistence(BdrvDirtyBitmap *bitmap, bool persistent);void bdrv_dirty_bitmap_set_inconsistent (BdrvDirtyBitmap *bitmap);void bdrv_dirty_bitmap_set_busy (BdrvDirtyBitmap *bitmap,bool busy); -void bdrv_merge_dirty_bitmap (BdrvDirtyBitmap *dest,const BdrvDirtyBitmap *src, +bool bdrv_merge_dirty_bitmap (BdrvDirtyBitmap *dest,const BdrvDirtyBitmap *src,HBitmap **backup,Error **errp);void bdrv_dirty_bitmap_skip_store (BdrvDirtyBitmap *bitmap,bool skip);bool bdrv_dirty_bitmap_get (BdrvDirtyBitmap *bitmap,int64_t offset);
block/dirty-bitmap.c +6 -2
@@ -881,10 +881,10 @@ bool bdrv_dirty_bitmap_next_dirty_area(BdrvDirtyBitmap *bitmap,* * @backup: If provided, make a copy of dest here prior to merge. */ -void bdrv_merge_dirty_bitmap (BdrvDirtyBitmap *dest,const BdrvDirtyBitmap *src, +bool bdrv_merge_dirty_bitmap (BdrvDirtyBitmap *dest,const BdrvDirtyBitmap *src,HBitmap **backup,Error **errp) { -bool ret; +bool ret = false;bdrv_dirty_bitmaps_lock (dest->bs);if (src->bs != dest->bs) {
@@ -907,11 +907,15 @@ void bdrv_merge_dirty_bitmap(BdrvDirtyBitmap *dest, const BdrvDirtyBitmap *src, ret =bdrv_dirty_bitmap_merge_internal (dest, src, backup, false);assert (ret); + ret = true; + out:bdrv_dirty_bitmaps_unlock (dest->bs);if (src->bs != dest->bs) {bdrv_dirty_bitmaps_unlock (src->bs); } + +return ret; }/**
block/monitor/bitmap-qmp-cmds.c +1 -4
@@ -259,7 +259,6 @@ BdrvDirtyBitmap *block_dirty_bitmap_merge(const char *node, const char *target,BlockDriverState *bs;BdrvDirtyBitmap *dst, *src, *anon;BlockDirtyBitmapMergeSourceList *lst; -Error *local_err =NULL ; dst =block_dirty_bitmap_lookup (node, target, &bs, errp);if (!dst) {
@@ -297,9 +296,7 @@ BdrvDirtyBitmap *block_dirty_bitmap_merge(const char *node, const char *target,abort (); } -bdrv_merge_dirty_bitmap (anon, src,NULL , &local_err); -if (local_err) { -error_propagate (errp, local_err); +if (!bdrv_merge_dirty_bitmap (anon, src,NULL , errp)) { dst =NULL ; goto out; }
Signature · 1 line
2.29.2
PATCH 3/11 block/block-copy: block_copy_state_new(): add bitmap parameter
In reply to Vladimir Sementsov-OgievskiyThis will be used in the following commit to bring "incremental" mode to copy-before-write filter.
--- include/block/block-copy.h | 2 +- block/block-copy.c | 16 +++++++++++++--- block/copy-before-write.c | 3 ++- 3 files changed, 16 insertions(+), 5 deletions(-)
include/block/block-copy.h +1 -1
@@ -26,7 +26,7 @@ typedef struct BlockCopyCallState BlockCopyCallState;BlockCopyState *block_copy_state_new (BdrvChild *source,BdrvChild *target,bool use_copy_range,bool compress, -Error **errp); +BdrvDirtyBitmap *bitmap,Error **errp);void block_copy_set_copy_opts (BlockCopyState *s,bool use_copy_range,bool compress);
block/block-copy.c +13 -3
@@ -400,9 +400,10 @@ static int64_t block_copy_calculate_cluster_size(BlockDriverState *target, }BlockCopyState *block_copy_state_new (BdrvChild *source,BdrvChild *target, -bool use_copy_range, -bool compress,Error **errp) +bool use_copy_range,bool compress, +BdrvDirtyBitmap *bitmap,Error **errp) { +ERRP_GUARD ();BlockCopyState *s;int64_t cluster_size;BdrvDirtyBitmap *copy_bitmap;
@@ -418,7 +419,16 @@ BlockCopyState *block_copy_state_new(BdrvChild *source, BdrvChild *target,return NULL ; }bdrv_disable_dirty_bitmap (copy_bitmap); -bdrv_set_dirty_bitmap (copy_bitmap,0 ,bdrv_dirty_bitmap_size (copy_bitmap)); +if (bitmap) { +if (!bdrv_merge_dirty_bitmap (copy_bitmap, bitmap,NULL , errp)) { +error_prepend (errp,"Failed to merge bitmap '%s' to internal " +"copy-bitmap: " ,bdrv_dirty_bitmap_name (bitmap)); +return NULL ; + } + }else { +bdrv_set_dirty_bitmap (copy_bitmap,0 , +bdrv_dirty_bitmap_size (copy_bitmap)); + } s =g_new (BlockCopyState,1 ); *s = (BlockCopyState ) {
block/copy-before-write.c +2 -1
@@ -169,7 +169,8 @@ static int cbw_open(BlockDriverState *bs, QDict *options, int flags, ((BDRV_REQ_FUA |BDRV_REQ_MAY_UNMAP |BDRV_REQ_NO_FALLBACK ) & bs->file->bs->supported_zero_flags); - s->bcs =block_copy_state_new (bs->file, s->target, false, false, errp); + s->bcs =block_copy_state_new (bs->file, s->target, false, false,NULL , + errp);if (!s->bcs) {error_prepend (errp,"Cannot create block-copy-state: " );return -EINVAL ;
Signature · 1 line
2.29.2
PATCH 4/11 block/copy-before-write: add bitmap open parameter
In reply to Vladimir Sementsov-OgievskiyThis brings "incremental" mode to copy-before-write filter: user can specify bitmap so that filter will copy only "dirty" areas.
--- qapi/block-core.json | 10 +++++++++- block/copy-before-write.c | 28 +++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-)
qapi/block-core.json +9 -1
@@ -4063,11 +4063,19 @@ # # @target: The target for copy-before-write operations. # +# @bitmap: If specified, copy-before-write filter will do +# copy-before-write operations only for dirty regions of the +# bitmap. Bitmap size must be equal to length of file and +# target child of the filter. Note also, that bitmap is used +# only to initialize internal bitmap of the process, so further +# modifications (or removing) of specified bitmap doesn't +# influence the filter. +# # Since: 6.1 ## { 'struct': 'BlockdevOptionsCbw', 'base': 'BlockdevOptionsGenericFormat', - 'data': { 'target': 'BlockdevRef' } } + 'data': { 'target': 'BlockdevRef', '*bitmap': 'BlockDirtyBitmap' } } ## # @BlockdevOptions:
block/copy-before-write.c +27 -1
@@ -148,6 +148,7 @@ static int cbw_open(BlockDriverState *bs, QDict *options, int flags, Error **errp) {BDRVCopyBeforeWriteState *s = bs->opaque; +BdrvDirtyBitmap *bitmap =NULL ; bs->file = bdrv_open_child(NULL, options, "file", bs, &child_of_bds, BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY,
@@ -162,6 +163,31 @@ static int cbw_open(BlockDriverState *bs, QDict *options, int flags,return -EINVAL ; } +if (qdict_haskey (options,"bitmap.node" ) || +qdict_haskey (options,"bitmap.name" )) + { +const char *bitmap_node, *bitmap_name; + +if (!qdict_haskey (options,"bitmap.node" )) { +error_setg (errp,"bitmap.node is not specified" ); +return -EINVAL ; + } + +if (!qdict_haskey (options,"bitmap.name" )) { +error_setg (errp,"bitmap.name is not specified" ); +return -EINVAL ; + } + + bitmap_node =qdict_get_str (options,"bitmap.node" ); + bitmap_name =qdict_get_str (options,"bitmap.name" ); + + bitmap =block_dirty_bitmap_lookup (bitmap_node, bitmap_name,NULL , + errp); +if (!bitmap) { +return -EINVAL ; + } + } + bs->total_sectors = bs->file->bs->total_sectors; bs->supported_write_flags =BDRV_REQ_WRITE_UNCHANGED | (BDRV_REQ_FUA & bs->file->bs->supported_write_flags);
@@ -169,7 +195,7 @@ static int cbw_open(BlockDriverState *bs, QDict *options, int flags, ((BDRV_REQ_FUA |BDRV_REQ_MAY_UNMAP |BDRV_REQ_NO_FALLBACK ) & bs->file->bs->supported_zero_flags); - s->bcs =block_copy_state_new (bs->file, s->target, false, false,NULL , + s->bcs =block_copy_state_new (bs->file, s->target, false, false, bitmap, errp);if (!s->bcs) {error_prepend (errp,"Cannot create block-copy-state: " );
Signature · 1 line
2.29.2
PATCH 5/11 block/block-copy: add block_copy_reset()
In reply to Vladimir Sementsov-OgievskiySplit block_copy_reset() out of block_copy_reset_unallocated() to be used in separate later.
--- include/block/block-copy.h | 1 + block/block-copy.c | 21 +++++++++++++-------- 2 files changed, 14 insertions(+), 8 deletions(-)
include/block/block-copy.h +1 -0
@@ -34,6 +34,7 @@ void block_copy_set_progress_meter(BlockCopyState *s, ProgressMeter *pm);void block_copy_state_free (BlockCopyState *s); +void block_copy_reset (BlockCopyState *s,int64_t offset,int64_t bytes);int64_t block_copy_reset_unallocated (BlockCopyState *s,int64_t offset,int64_t *count);
block/block-copy.c +13 -8
@@ -689,6 +689,18 @@ static int block_copy_is_cluster_allocated(BlockCopyState *s, int64_t offset, } } +void block_copy_reset (BlockCopyState *s,int64_t offset,int64_t bytes) +{ +QEMU_LOCK_GUARD (&s->lock); + +bdrv_reset_dirty_bitmap (s->copy_bitmap, offset, bytes); +if (s->progress) { +progress_set_remaining (s->progress, +bdrv_get_dirty_count (s->copy_bitmap) + + s->in_flight_bytes); + } +} +/* * Reset bits in copy_bitmap starting at offset if they represent unallocated * data in the image. May reset subsequent contiguous bits.
@@ -709,14 +721,7 @@ int64_t block_copy_reset_unallocated(BlockCopyState *s, bytes = clusters * s->cluster_size;if (!ret) { -qemu_co_mutex_lock (&s->lock); -bdrv_reset_dirty_bitmap (s->copy_bitmap, offset, bytes); -if (s->progress) { -progress_set_remaining (s->progress, -bdrv_get_dirty_count (s->copy_bitmap) + - s->in_flight_bytes); - } -qemu_co_mutex_unlock (&s->lock); +block_copy_reset (s, offset, bytes); } *count = bytes;
Signature · 1 line
2.29.2
PATCH 6/11 block: intoduce reqlist
In reply to Vladimir Sementsov-OgievskiySplit intersecting-requests functionality out of block-copy to be reused in copy-before-write filter.
--- include/block/reqlist.h | 45 ++++++++++++++++ block/block-copy.c | 116 +++++++++++++--------------------------- block/reqlist.c | 94 ++++++++++++++++++++++++++++++++ block/meson.build | 1 + 4 files changed, 177 insertions(+), 79 deletions(-) create mode 100644 include/block/reqlist.h create mode 100644 block/reqlist.c
include/block/reqlist.h new file +45 -0
@@ -0,0 +1,45 @@ +/* +* block_copy API +* +* Copyright (C) 2013 Proxmox Server Solutions +* Copyright (c) 2021 Virtuozzo International GmbH. +* +* Authors: +* Dietmar Maurer ([email protected]) +* Vladimir Sementsov-Ogievskiy <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +*/ + +#ifndef REQLIST_H +#define REQLIST_H + +#include "qemu/coroutine.h" + +/* +* The API is not thread-safe and shouldn't be. The struct is public to be part +* of other structures and protected by third-party locks, see +* block/block-copy.c for example. +*/ + +typedef struct BlockReq { +int64_t offset; +int64_t bytes; + +CoQueue wait_queue;/* coroutines blocked on this req */ +QLIST_ENTRY (BlockReq) list; +}BlockReq ; + +typedef QLIST_HEAD(, BlockReq) BlockReqList; + +void reqlist_init_req (BlockReqList *reqs,BlockReq *req,int64_t offset, +int64_t bytes); +BlockReq *reqlist_find_conflict (BlockReqList *reqs,int64_t offset, +int64_t bytes); +bool coroutine_fn reqlist_wait_one(BlockReqList *reqs, int64_t offset, +int64_t bytes,CoMutex *lock); +void coroutine_fn reqlist_shrink_req(BlockReq *req, int64_t new_bytes); +void coroutine_fn reqlist_remove_req(BlockReq *req); + +#endif /* REQLIST_H */
block/block-copy.c +37 -79
252 lines not on this page · raw message
block/reqlist.c new file +94 -0
94 lines not on this page · raw message
block/meson.build +1 -0
@@ -32,6 +32,7 @@ block_ss.add(files( 'qcow2.c', 'quorum.c', 'raw-format.c', + 'reqlist.c', 'snapshot.c', 'throttle-groups.c', 'throttle.c',
Signature · 1 line
2.29.2
PATCH 7/11 block/dirty-bitmap: introduce bdrv_dirty_bitmap_status()
In reply to Vladimir Sementsov-OgievskiyAdd a convenient function similar with bdrv_block_status() to get status of dirty bitmap.
--- include/block/dirty-bitmap.h | 2 ++ include/qemu/hbitmap.h | 11 +++++++++++ block/dirty-bitmap.c | 6 ++++++ util/hbitmap.c | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+)
include/block/dirty-bitmap.h +2 -0
@@ -115,6 +115,8 @@ int64_t bdrv_dirty_bitmap_next_zero(BdrvDirtyBitmap *bitmap, int64_t offset,bool bdrv_dirty_bitmap_next_dirty_area (BdrvDirtyBitmap *bitmap,int64_t start,int64_t end,int64_t max_dirty_count,int64_t *dirty_start,int64_t *dirty_count); +void bdrv_dirty_bitmap_status (BdrvDirtyBitmap *bitmap,int64_t offset, +int64_t bytes,bool *is_dirty,int64_t *count);BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap_locked (BdrvDirtyBitmap *bitmap,Error **errp);
include/qemu/hbitmap.h +11 -0
@@ -340,6 +340,17 @@ bool hbitmap_next_dirty_area(const HBitmap *hb, int64_t start, int64_t end,int64_t max_dirty_count, int64_t *dirty_start, int64_t *dirty_count); +/* +* bdrv_dirty_bitmap_status: +* @hb: The HBitmap to operate on +* @start: the offset to start from +* @end: end of requested area +* @is_dirty: is bitmap dirty at @offset +* @pnum: how many bits has same value starting from @offset +*/ +void hbitmap_status (const HBitmap *hb,int64_t offset,int64_t bytes, +bool *is_dirty,int64_t *pnum); +/** * hbitmap_iter_next: * @hbi: HBitmapIter to operate on.
block/dirty-bitmap.c +6 -0
@@ -875,6 +875,12 @@ bool bdrv_dirty_bitmap_next_dirty_area(BdrvDirtyBitmap *bitmap, dirty_start, dirty_count); } +void bdrv_dirty_bitmap_status (BdrvDirtyBitmap *bitmap,int64_t offset, +int64_t bytes,bool *is_dirty,int64_t *count) +{ +hbitmap_status (bitmap->bitmap, offset, bytes, is_dirty, count); +} +/** * bdrv_merge_dirty_bitmap: merge src into dest. * Ensures permissions on bitmaps are reasonable; use for public API.
util/hbitmap.c +36 -0
@@ -301,6 +301,42 @@ bool hbitmap_next_dirty_area(const HBitmap *hb, int64_t start, int64_t end,return true; } +void hbitmap_status (const HBitmap *hb,int64_t start,int64_t count, +bool *is_dirty,int64_t *pnum) +{ +int64_t next_dirty, next_zero; + +assert (start >=0 ); +assert (count >0 ); +assert (start + count <= hb->orig_size); + + next_dirty =hbitmap_next_dirty (hb, start, count); +if (next_dirty ==-1 ) { + *pnum = count; + *is_dirty = false; +return ; + } + +if (next_dirty > start) { + *pnum = next_dirty - start; + *is_dirty = false; +return ; + } + +assert (next_dirty == start); + + next_zero =hbitmap_next_zero (hb, start, count); +if (next_zero ==-1 ) { + *pnum = next_zero - start; + *is_dirty = true; +return ; + } + +assert (next_zero > start); + *pnum = next_zero - start; + *is_dirty = false; +} +bool hbitmap_empty (const HBitmap *hb) {return hb->count ==0 ;
Signature · 1 line
2.29.2
PATCH 8/11 block/reqlist: add reqlist_wait_all()
In reply to Vladimir Sementsov-OgievskiyTo be used in the next commit.
--- include/block/reqlist.h | 2 ++ block/reqlist.c | 6 ++++++ 2 files changed, 8 insertions(+)
include/block/reqlist.h +2 -0
@@ -39,6 +39,8 @@ BlockReq *reqlist_find_conflict(BlockReqList *reqs, int64_t offset, int64_t bytes); bool coroutine_fn reqlist_wait_one(BlockReqList *reqs, int64_t offset,int64_t bytes,CoMutex *lock); +void coroutine_fn reqlist_wait_all(BlockReqList *reqs, int64_t offset, +int64_t bytes,CoMutex *lock); void coroutine_fn reqlist_shrink_req(BlockReq *req, int64_t new_bytes); void coroutine_fn reqlist_remove_req(BlockReq *req);
block/reqlist.c +6 -0
@@ -68,6 +68,12 @@ bool coroutine_fn reqlist_wait_one(BlockReqList *reqs, int64_t offset,return true; } +void coroutine_fn reqlist_wait_all(BlockReqList *reqs, int64_t offset, +int64_t bytes,CoMutex *lock) +{ +while (reqlist_wait_one (reqs, offset, bytes, lock)) { } +} +/* * Shrink request and wake all waiting coroutines (may be some of them are not * intersecting with shrunk request).
Signature · 1 line
2.29.2
PATCH 10/11 block/copy-before-write: add cbw_snapshot_discard()
In reply to Vladimir Sementsov-OgievskiyTo be used soon.
--- block/copy-before-write.h | 1 + block/copy-before-write.c | 11 +++++++++++ 2 files changed, 12 insertions(+)
block/copy-before-write.h +1 -0
@@ -40,5 +40,6 @@ void bdrv_cbw_drop(BlockDriverState *bs);int cbw_snapshot_read_lock (BlockDriverState *bs,int64_t offset,int64_t bytes,const BlockReq **req,int64_t *pnum);void cbw_snapshot_read_unlock (BlockDriverState *bs,const BlockReq *req); +void cbw_snapshot_discard (BlockDriverState *bs,int64_t offset,int64_t bytes);#endif /* COPY_BEFORE_WRITE_H */
block/copy-before-write.c +11 -0
@@ -103,6 +103,17 @@ void cbw_snapshot_read_unlock(BlockDriverState *bs, const BlockReq *req)drop_read_req (s, (BlockReq *)req); } +void cbw_snapshot_discard (BlockDriverState *bs,int64_t offset,int64_t bytes) +{ +BDRVCopyBeforeWriteState *s = bs->opaque; + + WITH_QEMU_LOCK_GUARD(&s->lock) { +bdrv_reset_dirty_bitmap (s->access_bitmap, offset, bytes); + } + +block_copy_reset (s->bcs, offset, bytes); +} + static coroutine_fn int cbw_co_preadv(BlockDriverState *bs,uint64_t offset,uint64_t bytes,QEMUIOVector *qiov,int flags)
Signature · 1 line
2.29.2
PATCH 11/11 block: introduce fleecing block driver
In reply to Vladimir Sementsov-OgievskiyIntroduce a new driver, that works in pair with copy-before-write to improve fleecing.
Without fleecing driver, fleecing scheme may look as follows:
[guest]
|
|root
v
[copy-before-write] -----> [temp.qcow2] <--- [nbd export]
| target | |file |backing v |
[active disk] <-------------+
With fleecing driver, new scheme is:
bdrv_ref() [guest]<~~~~~~~~~~~~~~~~~~~[fleecing] <--- [nbd export] | | | |root +--source------+ |file v v v
[copy-before-write] -----> [temp.img]
| target
|file
v
[active disk]
Benefits of new scheme:
1. Access control: if remote client try to read data that not covered by original dirty bitmap used on copy-before-write open, client gets -EACCES.
2. Discard support: if remote client do DISCARD, this additionally to discarding data in temp.img informs block-copy process to not copy these clusters. Next read from discarded area will return -EACCES.
3. Synchronisation between client reads and block-copy write is more
efficient: it doesn't block intersecting block-copy write during client read (hmm, we still needlesly block it, as block-copy always serialize writes, it's a TODO to stop doing so).
4. We don't rely on backing feature: active disk should not be backing of temp image, so we avoid some permission-related difficulties (cleaning them up in copy-before-write filter is a TODO) and temp image now is not required to support backing, it may be simple raw image.
--- block/fleecing.c | 183 ++++++++++++++++++++++++++++++++++++++++++++++ block/meson.build | 1 + 2 files changed, 184 insertions(+) create mode 100644 block/fleecing.c
block/fleecing.c new file +183 -0
@@ -0,0 +1,183 @@ +/* +* copy-before-write filter driver +* +* The driver performs Copy-Before-Write (CBW) operation: it is injected above +* some node, and before each write it copies _old_ data to the target node. +* +* Copyright (c) 2018-2021 Virtuozzo International GmbH. +* +* Author: +* Sementsov-Ogievskiy Vladimir <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include "qemu/osdep.h" + +#include "sysemu/block-backend.h" +#include "qemu/cutils.h" +#include "qapi/error.h" +#include "block/block_int.h" +#include "block/qdict.h" +#include "block/block-copy.h" + +#include "block/copy-before-write.h" + +typedef struct BDRVFleecingState { +BlockDriverState *cbw; +BdrvChild *source; +}BDRVFleecingState ; + +static coroutine_fn int fleecing_co_preadv_part( +BlockDriverState *bs,uint64_t offset,uint64_t bytes, +QEMUIOVector *qiov,size_t qiov_offset,int flags) +{ +BDRVFleecingState *s = bs->opaque; +const BlockReq *req; +int ret; + +/* TODO: upgrade to async loop using AioTask */ +while (bytes) { +int64_t cur_bytes; + + ret =cbw_snapshot_read_lock (s->cbw, offset, bytes, &req, &cur_bytes); +if (ret <0 ) { +return ret; + } + +if (req) { + ret =bdrv_co_preadv_part (s->source, offset, cur_bytes, + qiov, qiov_offset, flags); +cbw_snapshot_read_unlock (s->cbw, req); + }else { + ret =bdrv_co_preadv_part (bs->file, offset, cur_bytes, + qiov, qiov_offset, flags); + } +if (ret <0 ) { +return ret; + } + + bytes -= cur_bytes; + offset += cur_bytes; + qiov_offset += cur_bytes; + } + +return 0 ; +} + +static int coroutine_fn fleecing_co_pdiscard(BlockDriverState *bs, +int64_t offset,int bytes) +{ +BDRVFleecingState *s = bs->opaque; + +cbw_snapshot_discard (s->cbw, offset, bytes); + +bdrv_co_pdiscard (bs->file, offset, bytes); + +/* +* Ignore bdrv_co_pdiscard() result: cbw_snapshot_discard() succeeded, that +* means that next read from this area will fail with -EACCES. More correct +* to report success now. +*/ +return 0 ; +} + +static int coroutine_fn fleecing_co_pwrite_zeroes(BlockDriverState *bs, +int64_t offset,int bytes,BdrvRequestFlags flags) +{ +return -EACCES ; +} + +static coroutine_fn int fleecing_co_pwritev(BlockDriverState *bs, +uint64_t offset, +uint64_t bytes, +QEMUIOVector *qiov,int flags) +{ +return -EACCES ; +} + + +static void fleecing_refresh_filename (BlockDriverState *bs) +{ +pstrcpy (bs->exact_filename,sizeof (bs->exact_filename), + bs->file->bs->filename); +} + +static int fleecing_open (BlockDriverState *bs,QDict *options,int flags, +Error **errp) +{ +BDRVFleecingState *s = bs->opaque; +const char *cbw_node_name =qdict_get_str (options, +"copy-before-write-node" ); +BlockDriverState *cbw; + + cbw =bdrv_find_node (cbw_node_name); +if (!cbw) { +error_setg (errp,"Node '%s' not found" , cbw_node_name); +return -EINVAL ; + } + + bs->file =bdrv_open_child (NULL , options,"file" , bs, &child_of_bds, +BDRV_CHILD_DATA |BDRV_CHILD_PRIMARY , + false, errp); +if (!bs->file) { +return -EINVAL ; + } + + s->source =bdrv_open_child (NULL , options,"source" , bs, &child_of_bds, +BDRV_CHILD_DATA , false, errp); +if (!s->source) { +return -EINVAL ; + } + + bs->total_sectors = bs->file->bs->total_sectors; + + s->cbw = cbw; +bdrv_ref (cbw); + +return 0 ; +} + +static void fleecing_close (BlockDriverState *bs) +{ +BDRVFleecingState *s = bs->opaque; + +bdrv_unref (s->cbw); +} + +BlockDriver bdrv_fleecing_filter = { + .format_name ="fleecing" , + .instance_size =sizeof (BDRVFleecingState), + + .bdrv_open = fleecing_open, + .bdrv_close = fleecing_close, + + .bdrv_co_preadv_part = fleecing_co_preadv_part, + .bdrv_co_pwritev = fleecing_co_pwritev, + .bdrv_co_pwrite_zeroes = fleecing_co_pwrite_zeroes, + .bdrv_co_pdiscard = fleecing_co_pdiscard, + + .bdrv_refresh_filename = fleecing_refresh_filename, + + .bdrv_child_perm = bdrv_default_perms, + + .is_filter = true, +}; + +static void fleecing_init (void ) +{ +bdrv_register (&bdrv_fleecing_filter); +} + +block_init (fleecing_init);
block/meson.build +1 -0
@@ -18,6 +18,7 @@ block_ss.add(files( 'crypto.c', 'dirty-bitmap.c', 'filter-compress.c', + 'fleecing.c', 'io.c', 'mirror.c', 'nbd.c',
Signature · 1 line
2.29.2
PATCH 9/11 block/copy-before-write: add cbw_snapshot_read_{lock, unlock}()
In reply to Vladimir Sementsov-OgievskiyAdd interface which help to do fleecing read. To be used in the next commit.
--- block/copy-before-write.h | 5 ++ block/copy-before-write.c | 103 +++++++++++++++++++++++++++++++++++++- 2 files changed, 106 insertions(+), 2 deletions(-)
block/copy-before-write.h +5 -0
@@ -28,6 +28,7 @@#include "block/block_int.h" #include "block/block-copy.h" +#include "block/reqlist.h" BlockDriverState *bdrv_cbw_append(BlockDriverState *source, BlockDriverState *target,
@@ -36,4 +37,8 @@ BlockDriverState *bdrv_cbw_append(BlockDriverState *source, Error **errp);void bdrv_cbw_drop (BlockDriverState *bs); +int cbw_snapshot_read_lock (BlockDriverState *bs,int64_t offset, +int64_t bytes,const BlockReq **req,int64_t *pnum); +void cbw_snapshot_read_unlock (BlockDriverState *bs,const BlockReq *req); +#endif /* COPY_BEFORE_WRITE_H */
block/copy-before-write.c +101 -2
147 lines not on this page · raw message
Signature · 1 line
2.29.2