qcow2: cluster space preallocation
v11 cover qcow2: cluster space preallocation
new in v11:
- patch 4, 9: fixed commentary format
- patch 4: removed one hunk with a dead check
- patch 5: added commentary to BDRV_REQ_ALLOCATE definition
- new auxiliary patch 6 for the following patch-7 change:
- patch 7: reset BDRV_REQ_ALLOCATE from supported flag if CONFIG_FALLOCATE is false
- patch 9: add commentary about missing qcow2_pre_write_overlap_check(). Omit redundant changes in the test 060.
v10: http://lists.nongnu.org/archive/html/qemu-devel/2018-12/msg00121.html
- patches 1-3,6,7: rebase after REQ_WRITE_UNCHANGED
- patch 3: drop supported_zero_flags. My bad, no write_zeroes in quorum.
- patch 4: almost trivial rebase. RB-tags not stripped. Choose another constant for BDRV_REQ_ALLOCATE
- patch 5: rebase. Instead of marking REQ_ALLOCATE serialising, accompany it with REQ_SERIALISING.
- patch 7: add symmetric copy-on-read change
- patch 8: trivial rebase. RB-tags not stripped.
----
This pull request is to start to improve a few performance points of qcow2 format:
1. non cluster-aligned write requests (to unallocated clusters) explicitly pad data with zeroes if there is no backing data.
Resulting increase in ops number and potential cluster fragmentation (on the host file) is already solved by:
ee22a9d qcow2: Merge the writing of the COW regions with the guest data
However, in case of zero COW regions, that can be avoided at all but the whole clusters are preallocated and zeroed in a single efficient write_zeroes() operation
2. moreover, efficient write_zeroes() operation can be used to preallocate space megabytes (*configurable number) ahead which gives noticeable improvement on some storage types (e.g. distributed storage) where the space allocation operation might be expensive) (Not included in this patchset since v6).
3. this will also allow to enable simultaneous writes to the same unallocated cluster after the space has been allocated & zeroed but before the first data is written and the cluster is linked to L2. (Not included in this patchset).
Efficient write_zeroes usually implies that the blocks are not actually written to but only reserved and marked as zeroed by the storage. In this patchset, file-posix driver is marked as supporting this operation if it supports (/configured to support) fallocate() operation.
Existing bdrv_write_zeroes() falls back to writing zero buffers if write_zeroes is not supported by the driver.
A new flag (BDRV_REQ_ALLOCATE) is introduced to avoid that but return ENOTSUP. Such allocate requests are also implemented to possibly overlap with the other requests. No wait is performed but an error returned in such case as well. So the operation should be considered advisory and a fallback scenario still handled by the caller (in this case, qcow2 driver).
simple perf test:
qemu-img create -f qcow2 test.img 4G && \ qemu-img bench -c $((1024*1024)) -f qcow2 -n -s 4k -t none -w test.img
test results (seconds):
+-----------+-------+------+-------+------+------+
| file | before | after | gain |
+-----------+-------+------+-------+------+------+
| ssd | 61.153 | 36.313 | 41% |
| hdd | 112.676 | 122.056 | -8% |
+-----------+--------------+--------------+------+Anton Nefedov (10):
mirror: inherit supported write/zero flags
blkverify: set supported write/zero flags
quorum: set supported write flags
block: introduce BDRV_REQ_ALLOCATE flag
block: treat BDRV_REQ_ALLOCATE as serialising
file-posix: reset fallocate-related flags without CONFIG_FALLOCATE*
file-posix: support BDRV_REQ_ALLOCATE
block: support BDRV_REQ_ALLOCATE in passthrough drivers
qcow2: skip writing zero buffers to empty COW areas iotest 134: test cluster-misaligned encrypted write
qapi/block-core.json | 4 +- block/qcow2.h | 6 +++ include/block/block.h | 13 +++++- include/block/block_int.h | 3 +- block/blkdebug.c | 2 +- block/blkverify.c | 10 ++++- block/copy-on-read.c | 4 +- block/file-posix.c | 16 +++++-- block/io.c | 45 +++++++++++++++---- block/mirror.c | 8 +++- block/qcow2-cluster.c | 2 +- block/qcow2.c | 89 +++++++++++++++++++++++++++++++++++++- block/quorum.c | 19 +++++++- block/raw-format.c | 2 +- block/trace-events | 1 + tests/qemu-iotests/060 | 7 ++- tests/qemu-iotests/060.out | 5 ++- tests/qemu-iotests/134 | 9 ++++ tests/qemu-iotests/134.out | 10 +++++ 19 files changed, 227 insertions(+), 28 deletions(-)
Signature · 1 line
2.17.1
PATCH v11 1/10 mirror: inherit supported write/zero flags
--- block/mirror.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
block/mirror.c +6 -2
@@ -1529,8 +1529,12 @@ static void mirror_start_job(const char *job_id, BlockDriverState *bs, mirror_top_bs->implicit = true; } mirror_top_bs->total_sectors = bs->total_sectors; - mirror_top_bs->supported_write_flags =BDRV_REQ_WRITE_UNCHANGED ; - mirror_top_bs->supported_zero_flags =BDRV_REQ_WRITE_UNCHANGED ; + mirror_top_bs->supported_write_flags =BDRV_REQ_WRITE_UNCHANGED | + (BDRV_REQ_FUA & bs->supported_write_flags); + mirror_top_bs->supported_zero_flags =BDRV_REQ_WRITE_UNCHANGED | + ((BDRV_REQ_FUA |BDRV_REQ_MAY_UNMAP ) + & bs->supported_zero_flags); + bs_opaque =g_new0 (MirrorBDSOpaque,1 ); mirror_top_bs->opaque = bs_opaque;bdrv_set_aio_context (mirror_top_bs,bdrv_get_aio_context (bs));
Signature · 1 line
2.17.1
PATCH v11 2/10 blkverify: set supported write/zero flags
In reply to Anton Nefedov--- block/blkverify.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
block/blkverify.c +8 -2
@@ -141,8 +141,14 @@ static int blkverify_open(BlockDriverState *bs, QDict *options, int flags, goto fail; } - bs->supported_write_flags =BDRV_REQ_WRITE_UNCHANGED ; - bs->supported_zero_flags =BDRV_REQ_WRITE_UNCHANGED ; + bs->supported_write_flags =BDRV_REQ_WRITE_UNCHANGED | + (BDRV_REQ_FUA & + bs->file->bs->supported_write_flags & + s->test_file->bs->supported_write_flags); + bs->supported_zero_flags =BDRV_REQ_WRITE_UNCHANGED | + ((BDRV_REQ_FUA |BDRV_REQ_MAY_UNMAP ) & + bs->file->bs->supported_zero_flags & + s->test_file->bs->supported_zero_flags); ret =0 ; fail:
Signature · 1 line
2.17.1
PATCH v11 3/10 quorum: set supported write flags
In reply to Anton Nefedov--- block/quorum.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-)
block/quorum.c +18 -1
@@ -857,6 +857,19 @@ static QemuOptsList quorum_runtime_opts = { }, }; +static void quorum_set_supported_flags (BlockDriverState *bs) +{ +BDRVQuorumState *s = bs->opaque; +int i; + + bs->supported_write_flags =BDRV_REQ_FUA ; +for (i =0 ; i < s->num_children; i++) { + bs->supported_write_flags &= s->children[i]->bs->supported_write_flags; + } + + bs->supported_write_flags |=BDRV_REQ_WRITE_UNCHANGED ; +} +static int quorum_open (BlockDriverState *bs,QDict *options,int flags,Error **errp) {
@@ -950,7 +963,7 @@ static int quorum_open(BlockDriverState *bs, QDict *options, int flags, } s->next_child_index = s->num_children; - bs->supported_write_flags =BDRV_REQ_WRITE_UNCHANGED ; +quorum_set_supported_flags (bs);g_free (opened); goto exit;
@@ -1025,6 +1038,8 @@ static void quorum_add_child(BlockDriverState *bs, BlockDriverState *child_bs, s->children = g_renew(BdrvChild *, s->children, s->num_children + 1); s->children[s->num_children++] = child; +quorum_set_supported_flags (bs); + out:bdrv_drained_end (bs); }
@@ -1063,6 +1078,8 @@ static void quorum_del_child(BlockDriverState *bs, BdrvChild *child,bdrv_unref_child (bs, child);bdrv_drained_end (bs); + +quorum_set_supported_flags (bs); }static void quorum_refresh_filename (BlockDriverState *bs,QDict *options)
Signature · 1 line
2.17.1
PATCH v11 4/10 block: introduce BDRV_REQ_ALLOCATE flag
In reply to Anton NefedovThe flag is supposed to indicate that the region of the disk image has to be sufficiently allocated so it reads as zeroes.
The call with the flag set must return -ENOTSUP if allocation cannot be done efficiently.
This has to be made sure of by both
- the drivers that support the flag
- and the common block layer (so it will not fall back to any slowpath (like writing zero buffers) in case the driver does not support the flag).
--- include/block/block.h | 10 +++++++++- include/block/block_int.h | 3 ++- block/io.c | 14 +++++++++++++- 3 files changed, 24 insertions(+), 3 deletions(-)
include/block/block.h +9 -1
@@ -83,8 +83,16 @@ typedef enum {*/ BDRV_REQ_SERIALISING =0x80 , +/* +* The BDRV_REQ_ALLOCATE flag is used to indicate that the driver has to +* efficiently allocate the space so it reads as zeroes, or return an error. +* If this flag is set then BDRV_REQ_ZERO_WRITE must also be set. +* This flag cannot be set together with BDRV_REQ_MAY_UNMAP. +*/ +BDRV_REQ_ALLOCATE =0x100 , +/* Mask of valid flags */ -BDRV_REQ_MASK =0xff , +BDRV_REQ_MASK =0x1ff , } BdrvRequestFlags; typedef struct BlockSizes {
include/block/block_int.h +2 -1
@@ -724,7 +724,8 @@ struct BlockDriverState {* their children. */ unsigned int supported_write_flags;/* Flags honored during pwrite_zeroes (so far: BDRV_REQ_FUA, -* BDRV_REQ_MAY_UNMAP, BDRV_REQ_WRITE_UNCHANGED) */ +* BDRV_REQ_MAY_UNMAP, BDRV_REQ_WRITE_UNCHANGED, BDRV_REQ_ALLOCATE) +*/ unsigned int supported_zero_flags;/* the following member gives a name to every node on the bs graph. */
block/io.c +13 -1
@@ -1534,7 +1534,7 @@ static int coroutine_fn bdrv_co_do_pwrite_zeroes(BlockDriverState *bs,assert (!bs->supported_zero_flags); } -if (ret == -ENOTSUP ) { +if (ret == -ENOTSUP && !(flags &BDRV_REQ_ALLOCATE )) {/* Fall back to bounce buffer if write zeroes is unsupported */ BdrvRequestFlags write_flags = flags & ~BDRV_REQ_ZERO_WRITE ;
@@ -1773,6 +1773,9 @@ static int coroutine_fn bdrv_co_do_zero_pwritev(BdrvChild *child,assert (flags &BDRV_REQ_ZERO_WRITE );if (head_padding_bytes || tail_padding_bytes) { +if (flags &BDRV_REQ_ALLOCATE ) { +return -ENOTSUP ; + } buf =qemu_blockalign (bs, align); iov = (struct iovec ) { .iov_base = buf,
@@ -1858,6 +1861,9 @@ int coroutine_fn bdrv_co_pwritev(BdrvChild *child,bool use_local_qiov = false;int ret; +assert (!((flags &BDRV_REQ_ALLOCATE ) && (flags &BDRV_REQ_MAY_UNMAP ))); +assert (!((flags &BDRV_REQ_ALLOCATE ) && !(flags &BDRV_REQ_ZERO_WRITE ))); +trace_bdrv_co_pwritev (child->bs, offset, bytes, flags);if (!bs->drv) {
@@ -1980,6 +1986,12 @@ int coroutine_fn bdrv_co_pwrite_zeroes(BdrvChild *child, int64_t offset, {trace_bdrv_co_pwrite_zeroes (child->bs, offset, bytes, flags); +if ((flags &BDRV_REQ_ALLOCATE ) && + !(child->bs->supported_zero_flags &BDRV_REQ_ALLOCATE )) + { +return -ENOTSUP ; + } +if (!(child->bs->open_flags &BDRV_O_UNMAP )) { flags &= ~BDRV_REQ_MAY_UNMAP ; }
Signature · 1 line
2.17.1
Re: [Qemu-devel] [PATCH v11 04/10] block: introduce BDRV_REQ_ALLOCATE flag
Quoted 88 lines · Anton Nefedov
The flag is supposed to indicate that the region of the disk image has to be sufficiently allocated so it reads as zeroes.
The call with the flag set must return
-ENOTSUPif allocation cannot be done efficiently.
This has to be made sure of by both
- the drivers that support the flag
- and the common block layer (so it will not fall back to any slowpath (like writing zero buffers) in case the driver does not support the flag).Signed-off-by Anton Nefedov <[email protected]>--- include/block/block.h | 10 +++++++++- include/block/block_int.h | 3 ++- block/io.c | 14 +++++++++++++- 3 files changed, 24 insertions(+), 3 deletions(-)include/block/block.h +9
-1@@ -83,8 +83,16 @@ typedef enum {*/ BDRV_REQ_SERIALISING =0x80 , +/* +* The BDRV_REQ_ALLOCATE flag is used to indicate that the driver has to +* efficiently allocate the space so it reads as zeroes, or return an error. +* If this flag is set then BDRV_REQ_ZERO_WRITE must also be set. +* This flag cannot be set together with BDRV_REQ_MAY_UNMAP. +*/ +BDRV_REQ_ALLOCATE =0x100 , +/* Mask of valid flags */ -BDRV_REQ_MASK =0xff , +BDRV_REQ_MASK =0x1ff , } BdrvRequestFlags; typedef struct BlockSizes {include/block/block_int.h +2
-1@@ -724,7 +724,8 @@ struct BlockDriverState {* their children. */ unsigned int supported_write_flags;/* Flags honored during pwrite_zeroes (so far: BDRV_REQ_FUA, -* BDRV_REQ_MAY_UNMAP, BDRV_REQ_WRITE_UNCHANGED) */ +* BDRV_REQ_MAY_UNMAP, BDRV_REQ_WRITE_UNCHANGED, BDRV_REQ_ALLOCATE) +*/ unsigned int supported_zero_flags;/* the following member gives a name to every node on the bs graph. */ block/io.c +7
-1@@ -1534,7 +1534,7 @@ static int coroutine_fn bdrv_co_do_pwrite_zeroes(BlockDriverState *bs,assert (!bs->supported_zero_flags); } -if (ret == -ENOTSUP ) { +if (ret == -ENOTSUP && !(flags &BDRV_REQ_ALLOCATE )) {/* Fall back to bounce buffer if write zeroes is unsupported */ BdrvRequestFlags write_flags = flags & ~BDRV_REQ_ZERO_WRITE ;@@ -1773,6 +1773,9 @@ static int coroutine_fn bdrv_co_do_zero_pwritev(BdrvChild *child,assert (flags &BDRV_REQ_ZERO_WRITE );if (head_padding_bytes || tail_padding_bytes) { +if (flags &BDRV_REQ_ALLOCATE ) { +return -ENOTSUP ; + } buf =qemu_blockalign (bs, align); iov = (struct iovec ) { .iov_base = buf,@@ -1858,6 +1861,9 @@ int coroutine_fn bdrv_co_pwritev(BdrvChild *child,bool use_local_qiov = false;int ret; +assert (!((flags &BDRV_REQ_ALLOCATE ) && (flags &BDRV_REQ_MAY_UNMAP ))); +assert (!((flags &BDRV_REQ_ALLOCATE ) && !(flags &BDRV_REQ_ZERO_WRITE ))); +trace_bdrv_co_pwritev (child->bs, offset, bytes, flags);if (!bs->drv) {
block/io.c int coroutine_fn bdrv_co_pwrite_zeroes(BdrvChild *child, int64_t offset, · line 1993
Quoted 8 lines
block/io.c +5
-0@@ -1980,6 +1986,12 @@ int coroutine_fn bdrv_co_pwrite_zeroes(BdrvChild *child, int64_t offset, {trace_bdrv_co_pwrite_zeroes (child->bs, offset, bytes, flags); +if ((flags &BDRV_REQ_ALLOCATE ) && + !(child->bs->supported_zero_flags &BDRV_REQ_ALLOCATE )) + { +return -ENOTSUP ; + }
Why don't we have this check in bdrv_co_pwritev? However, similar check about UNMAP is here too, so:
Quoted 5 lines
block/io.c +1
-0+if (!(child->bs->open_flags &BDRV_O_UNMAP )) { flags &= ~BDRV_REQ_MAY_UNMAP ; }
Signature · 2 lines
Best regards, Vladimir
Re: [Qemu-devel] [PATCH v11 04/10] block: introduce BDRV_REQ_ALLOCATE flag
In reply to Anton NefedovQuoted 11 lines · Anton Nefedov
The flag is supposed to indicate that the region of the disk image has to be sufficiently allocated so it reads as zeroes.
The call with the flag set must return
-ENOTSUPif allocation cannot be done efficiently.
This has to be made sure of by both
- the drivers that support the flag
- and the common block layer (so it will not fall back to any slowpath (like writing zero buffers) in case the driver does not support the flag).
Quoted 1 line
Signed-off-by Anton Nefedov <[email protected]>
Berto
PATCH v11 5/10 block: treat BDRV_REQ_ALLOCATE as serialising
In reply to Anton NefedovThe idea is that ALLOCATE requests may overlap with other requests. Reuse the existing block layer infrastructure for serialising requests. Use the following approach:
- mark ALLOCATE also SERIALISING, so subsequent requests to the area wait
- ALLOCATE request itself must never wait if another request is in flight already. Return EAGAIN, let the caller reconsider.
--- include/block/block.h | 3 +++ block/io.c | 31 ++++++++++++++++++++++++------- 2 files changed, 27 insertions(+), 7 deletions(-)
include/block/block.h +3 -0
@@ -88,6 +88,9 @@ typedef enum {* efficiently allocate the space so it reads as zeroes, or return an error. * If this flag is set then BDRV_REQ_ZERO_WRITE must also be set. * This flag cannot be set together with BDRV_REQ_MAY_UNMAP. +* This flag implicitly sets BDRV_REQ_SERIALISING meaning it is protected +* from conflicts with overlapping requests. If such conflict is detected, +* -EAGAIN is returned. */ BDRV_REQ_ALLOCATE = 0x100,
block/io.c +24 -7
@@ -720,12 +720,13 @@ void bdrv_dec_in_flight(BlockDriverState *bs)bdrv_wakeup (bs); } -static bool coroutine_fn wait_serialising_requests(BdrvTrackedRequest *self) +static bool coroutine_fn find_or_wait_serialising_requests( +BdrvTrackedRequest *self,bool wait) {BlockDriverState *bs = self->bs;BdrvTrackedRequest *req;bool retry; -bool waited = false; +bool found = false;if (!atomic_read (&bs->serialising_in_flight)) {return false;
@@ -751,11 +752,14 @@ static bool coroutine_fn wait_serialising_requests(BdrvTrackedRequest *self)* will wait for us as soon as it wakes up, then just go on * (instead of producing a deadlock in the former case). */ if (!req->waiting_for) { + found = true; +if (!wait) { +break ; + } self->waiting_for = req;qemu_co_queue_wait (&req->wait_queue, &bs->reqs_lock); self->waiting_for =NULL ; retry = true; - waited = true;break ; } }
@@ -763,7 +767,12 @@ static bool coroutine_fn wait_serialising_requests(BdrvTrackedRequest *self)qemu_co_mutex_unlock (&bs->reqs_lock); }while (retry); -return waited; +return found; +} + +static bool coroutine_fn wait_serialising_requests(BdrvTrackedRequest *self) +{ +return find_or_wait_serialising_requests (self, true); } static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
@@ -1585,7 +1594,7 @@ bdrv_co_write_req_prepare(BdrvChild *child, int64_t offset, uint64_t bytes, BdrvTrackedRequest *req, int flags) {BlockDriverState *bs = child->bs; -bool waited; +bool found;int64_t end_sector =DIV_ROUND_UP (offset + bytes,BDRV_SECTOR_SIZE );if (bs->read_only) {
@@ -1602,9 +1611,13 @@ bdrv_co_write_req_prepare(BdrvChild *child, int64_t offset, uint64_t bytes,mark_request_serialising (req,bdrv_get_cluster_size (bs)); } - waited =wait_serialising_requests (req); + found =find_or_wait_serialising_requests (req, + !(flags &BDRV_REQ_ALLOCATE )); +if (found && (flags &BDRV_REQ_ALLOCATE )) { +return -EAGAIN ; + } -assert (!waited || !req->serialising || +assert (!found || !req->serialising ||is_request_serialising_and_aligned (req));assert (req->overlap_offset <= offset);assert (offset + bytes <= req->overlap_offset + req->overlap_bytes);
@@ -1864,6 +1877,10 @@ int coroutine_fn bdrv_co_pwritev(BdrvChild *child,assert (!((flags &BDRV_REQ_ALLOCATE ) && (flags &BDRV_REQ_MAY_UNMAP )));assert (!((flags &BDRV_REQ_ALLOCATE ) && !(flags &BDRV_REQ_ZERO_WRITE ))); +if (flags &BDRV_REQ_ALLOCATE ) { + flags |=BDRV_REQ_SERIALISING ; + } +trace_bdrv_co_pwritev (child->bs, offset, bytes, flags);if (!bs->drv) {
Signature · 1 line
2.17.1
Re: [Qemu-devel] [PATCH v11 05/10] block: treat BDRV_REQ_ALLOCATE as serialising
Quoted 8 lines · Anton Nefedov
The idea is that ALLOCATE requests may overlap with other requests. Reuse the existing block layer infrastructure for serialising requests. Use the following approach:
- mark ALLOCATE also SERIALISING, so subsequent requests to the area wait
- ALLOCATE request itself must never wait if another request is in flight already. Return EAGAIN, let the caller reconsider.Signed-off-by Anton Nefedov <[email protected]>
Berto
PATCH v11 6/10 file-posix: reset fallocate-related flags without CONFIG_FALLOCATE*
In reply to Anton Nefedovthese flags currently affect nothing without CONFIG_FALLOCATE*, so it's not a bug. Fixing it makes possible to adjust supported zero flag BDRV_REQ_ALLOCATE regardless of configuration (in the following patch).
--- block/file-posix.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
block/file-posix.c +6 -2
@@ -1488,9 +1488,7 @@ static ssize_t handle_aiocb_write_zeroes_block(RawPosixAIOData *aiocb)static int handle_aiocb_write_zeroes (void *opaque) {RawPosixAIOData *aiocb = opaque; -#if defined(CONFIG_FALLOCATE ) || defined(CONFIG_XFS )BDRVRawState *s = aiocb->bs->opaque; -#endif #ifdef CONFIG_FALLOCATE int64_t len;#endif
@@ -1514,6 +1512,8 @@ static int handle_aiocb_write_zeroes(void *opaque) } s->has_write_zeroes = false; } +#else + s->has_write_zeroes = false;#endif #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
@@ -1533,6 +1533,8 @@ static int handle_aiocb_write_zeroes(void *opaque) s->has_discard = false; } } +#else + s->has_discard = false;#endif #ifdef CONFIG_FALLOCATE
@@ -1546,6 +1548,8 @@ static int handle_aiocb_write_zeroes(void *opaque) } s->has_fallocate = false; } +#else + s->has_fallocate = false;#endif return -ENOTSUP ;
Signature · 1 line
2.17.1
Re: [Qemu-devel] [PATCH v11 06/10] file-posix: reset fallocate-related flags without CONFIG_FALLOCATE*
Quoted 5 lines · Anton Nefedov
these flags currently affect nothing without CONFIG_FALLOCATE*, so it's not a bug. Fixing it makes possible to adjust supported zero flag
BDRV_REQ_ALLOCATEregardless of configuration (in the following patch).Signed-off-by Anton Nefedov <[email protected]>
I'd prefere to refactor things so that these variables are false from the beginning in these cases, but it's more difficult and may be done separately.
Quoted 46 lines
--- block/file-posix.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)block/file-posix.c +6
-2@@ -1488,9 +1488,7 @@ static ssize_t handle_aiocb_write_zeroes_block(RawPosixAIOData *aiocb)static int handle_aiocb_write_zeroes (void *opaque) {RawPosixAIOData *aiocb = opaque; -#if defined(CONFIG_FALLOCATE ) || defined(CONFIG_XFS )BDRVRawState *s = aiocb->bs->opaque; -#endif #ifdef CONFIG_FALLOCATE int64_t len;#endif @@ -1514,6 +1512,8 @@ static int handle_aiocb_write_zeroes(void *opaque) } s->has_write_zeroes = false; } +#else + s->has_write_zeroes = false;#endif #ifdef CONFIG_FALLOCATE_PUNCH_HOLE @@ -1533,6 +1533,8 @@ static int handle_aiocb_write_zeroes(void *opaque) s->has_discard = false; } } +#else + s->has_discard = false;#endif #ifdef CONFIG_FALLOCATE @@ -1546,6 +1548,8 @@ static int handle_aiocb_write_zeroes(void *opaque) } s->has_fallocate = false; } +#else + s->has_fallocate = false;#endif return -ENOTSUP ;
Signature · 2 lines
Best regards, Vladimir
Re: [Qemu-devel] [PATCH v11 06/10] file-posix: reset fallocate-related flags without CONFIG_FALLOCATE*
In reply to Anton NefedovQuoted 5 lines · Anton Nefedov
these flags currently affect nothing without CONFIG_FALLOCATE*, so it's not a bug. Fixing it makes possible to adjust supported zero flag
BDRV_REQ_ALLOCATEregardless of configuration (in the following patch).Signed-off-by Anton Nefedov <[email protected]>
Berto
PATCH v11 7/10 file-posix: support BDRV_REQ_ALLOCATE
In reply to Anton NefedovCurrent write_zeroes implementation is good enough to satisfy this flag too
--- block/file-posix.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
block/file-posix.c +7 -1
@@ -607,6 +607,7 @@ static int raw_open_common(BlockDriverState *bs, QDict *options, }else { s->discard_zeroes = true; s->has_fallocate = true; + bs->supported_zero_flags =BDRV_REQ_ALLOCATE ; } }else {if (!(S_ISCHR (st.st_mode) ||S_ISBLK (st.st_mode))) {
@@ -650,10 +651,11 @@ static int raw_open_common(BlockDriverState *bs, QDict *options,#ifdef CONFIG_XFS if (platform_test_xfs_fd (s->fd)) { s->is_xfs = true; + bs->supported_zero_flags =BDRV_REQ_ALLOCATE ; }#endif - bs->supported_zero_flags =BDRV_REQ_MAY_UNMAP ; + bs->supported_zero_flags |=BDRV_REQ_MAY_UNMAP ; ret =0 ; fail:if (filename && (bdrv_flags &BDRV_O_TEMPORARY )) {
@@ -1552,6 +1554,10 @@ static int handle_aiocb_write_zeroes(void *opaque) s->has_fallocate = false;#endif +if (!s->has_fallocate) { + aiocb->bs->supported_zero_flags &= ~BDRV_REQ_ALLOCATE ; + } +return -ENOTSUP ; }
Signature · 1 line
2.17.1
Re: [Qemu-devel] [PATCH v11 07/10] file-posix: support BDRV_REQ_ALLOCATE
Quoted 3 lines · Anton Nefedov
Current
write_zeroesimplementation is good enough to satisfy this flag tooSigned-off-by Anton Nefedov <[email protected]>
Quoted 41 lines
--- block/file-posix.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)block/file-posix.c +7
-1@@ -607,6 +607,7 @@ static int raw_open_common(BlockDriverState *bs, QDict *options, }else { s->discard_zeroes = true; s->has_fallocate = true; + bs->supported_zero_flags =BDRV_REQ_ALLOCATE ; } }else {if (!(S_ISCHR (st.st_mode) ||S_ISBLK (st.st_mode))) {@@ -650,10 +651,11 @@ static int raw_open_common(BlockDriverState *bs, QDict *options,#ifdef CONFIG_XFS if (platform_test_xfs_fd (s->fd)) { s->is_xfs = true; + bs->supported_zero_flags =BDRV_REQ_ALLOCATE ; }#endif - bs->supported_zero_flags =BDRV_REQ_MAY_UNMAP ; + bs->supported_zero_flags |=BDRV_REQ_MAY_UNMAP ; ret =0 ; fail:if (filename && (bdrv_flags &BDRV_O_TEMPORARY )) {@@ -1552,6 +1554,10 @@ static int handle_aiocb_write_zeroes(void *opaque) s->has_fallocate = false;#endif +if (!s->has_fallocate) { + aiocb->bs->supported_zero_flags &= ~BDRV_REQ_ALLOCATE ; + } +return -ENOTSUP ; }
Signature · 2 lines
Best regards, Vladimir
Re: [Qemu-devel] [PATCH v11 07/10] file-posix: support BDRV_REQ_ALLOCATE
In reply to Anton NefedovQuoted 3 lines · Anton Nefedov
Current
write_zeroesimplementation is good enough to satisfy this flag tooSigned-off-by Anton Nefedov <[email protected]>
Berto
PATCH v11 8/10 block: support BDRV_REQ_ALLOCATE in passthrough drivers
In reply to Anton NefedovSupport the flag if the underlying BDS supports it
--- block/blkdebug.c | 2 +- block/blkverify.c | 2 +- block/copy-on-read.c | 4 ++-- block/mirror.c | 2 +- block/raw-format.c | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-)
block/blkdebug.c +1 -1
@@ -401,7 +401,7 @@ static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags, bs->supported_write_flags =BDRV_REQ_WRITE_UNCHANGED | (BDRV_REQ_FUA & bs->file->bs->supported_write_flags); bs->supported_zero_flags =BDRV_REQ_WRITE_UNCHANGED | - ((BDRV_REQ_FUA |BDRV_REQ_MAY_UNMAP ) & + ((BDRV_REQ_FUA |BDRV_REQ_MAY_UNMAP |BDRV_REQ_ALLOCATE ) & bs->file->bs->supported_zero_flags); ret = -EINVAL ;
block/blkverify.c +1 -1
@@ -146,7 +146,7 @@ static int blkverify_open(BlockDriverState *bs, QDict *options, int flags, bs->file->bs->supported_write_flags & s->test_file->bs->supported_write_flags); bs->supported_zero_flags =BDRV_REQ_WRITE_UNCHANGED | - ((BDRV_REQ_FUA |BDRV_REQ_MAY_UNMAP ) & + ((BDRV_REQ_FUA |BDRV_REQ_MAY_UNMAP |BDRV_REQ_ALLOCATE ) & bs->file->bs->supported_zero_flags & s->test_file->bs->supported_zero_flags);
block/copy-on-read.c +2 -2
@@ -38,8 +38,8 @@ static int cor_open(BlockDriverState *bs, QDict *options, int flags, bs->file->bs->supported_write_flags); bs->supported_zero_flags =BDRV_REQ_WRITE_UNCHANGED | - ((BDRV_REQ_FUA |BDRV_REQ_MAY_UNMAP ) & - bs->file->bs->supported_zero_flags); + ((BDRV_REQ_FUA |BDRV_REQ_MAY_UNMAP |BDRV_REQ_ALLOCATE ) & + bs->file->bs->supported_zero_flags);return 0 ; }
block/mirror.c +1 -1
@@ -1532,7 +1532,7 @@ static void mirror_start_job(const char *job_id, BlockDriverState *bs, mirror_top_bs->supported_write_flags =BDRV_REQ_WRITE_UNCHANGED | (BDRV_REQ_FUA & bs->supported_write_flags); mirror_top_bs->supported_zero_flags =BDRV_REQ_WRITE_UNCHANGED | - ((BDRV_REQ_FUA |BDRV_REQ_MAY_UNMAP ) + ((BDRV_REQ_FUA |BDRV_REQ_MAY_UNMAP |BDRV_REQ_ALLOCATE ) & bs->supported_zero_flags); bs_opaque =g_new0 (MirrorBDSOpaque,1 );
block/raw-format.c +1 -1
@@ -432,7 +432,7 @@ static int raw_open(BlockDriverState *bs, QDict *options, int flags, bs->supported_write_flags =BDRV_REQ_WRITE_UNCHANGED | (BDRV_REQ_FUA & bs->file->bs->supported_write_flags); bs->supported_zero_flags =BDRV_REQ_WRITE_UNCHANGED | - ((BDRV_REQ_FUA |BDRV_REQ_MAY_UNMAP ) & + ((BDRV_REQ_FUA |BDRV_REQ_MAY_UNMAP |BDRV_REQ_ALLOCATE ) & bs->file->bs->supported_zero_flags);if (bs->probed && !bdrv_is_read_only (bs)) {
Signature · 1 line
2.17.1
PATCH v11 9/10 qcow2: skip writing zero buffers to empty COW areas
In reply to Anton NefedovIf COW areas of the newly allocated clusters are zeroes on the backing image, efficient bdrv_write_zeroes(flags=BDRV_REQ_ALLOCATE) can be used on the whole cluster instead of writing explicit zero buffers later in perform_cow().
iotest 060:
write to the discarded cluster does not trigger COW anymore. Use a backing image instead.
--- qapi/block-core.json | 4 +- block/qcow2.h | 6 +++ block/qcow2-cluster.c | 2 +- block/qcow2.c | 89 +++++++++++++++++++++++++++++++++++++- block/trace-events | 1 + tests/qemu-iotests/060 | 7 ++- tests/qemu-iotests/060.out | 5 ++- 7 files changed, 108 insertions(+), 6 deletions(-)
qapi/block-core.json +3 -1
@@ -3009,6 +3009,8 @@ # # @cor_write: a write due to copy-on-read (since 2.11) # +# @cluster_alloc_space: an allocation of file space for a cluster (since 4.0) +# # Since: 2.9 ## { 'enum': 'BlkdebugEvent', 'prefix': 'BLKDBG',
@@ -3027,7 +3029,7 @@ 'pwritev_rmw_tail', 'pwritev_rmw_after_tail', 'pwritev', 'pwritev_zero', 'pwritev_done', 'empty_image_prepare', 'l1_shrink_write_table', 'l1_shrink_free_l2_clusters', - 'cor_write'] } + 'cor_write', 'cluster_alloc_space'] } ## # @BlkdebugInjectErrorOptions:
block/qcow2.h +6 -0
@@ -386,6 +386,12 @@ typedef struct QCowL2Meta*/ Qcow2COWRegion cow_end; +/* +* Indicates that COW regions are already handled and do not require +* any more processing. +*/ +bool skip_cow; +/** * The I/O vector with the data from the actual guest write request. * If non-NULL, this is meant to be merged together with the data
block/qcow2-cluster.c +1 -1
@@ -806,7 +806,7 @@ static int perform_cow(BlockDriverState *bs, QCowL2Meta *m)assert (start->offset + start->nb_bytes <= end->offset);assert (!m->data_qiov || m->data_qiov->size == data_bytes); -if (start->nb_bytes ==0 && end->nb_bytes ==0 ) { +if ((start->nb_bytes ==0 && end->nb_bytes ==0 ) || m->skip_cow) {return 0 ; }
block/qcow2.c +87 -2
123 lines not on this page · raw message
block/trace-events +1 -0
@@ -69,6 +69,7 @@ qcow2_writev_done_part(void *co, int cur_bytes) "co %p cur_bytes %d" qcow2_writev_data(void *co, uint64_t offset) "co %p offset 0x%" PRIx64 qcow2_pwrite_zeroes_start_req(void *co, int64_t offset, int count) "co %p offset 0x%" PRIx64 " count %d" qcow2_pwrite_zeroes(void *co, int64_t offset, int count) "co %p offset 0x%" PRIx64 " count %d" +qcow2_skip_cow(void *co, uint64_t offset, int nb_clusters) "co %p offset 0x%" PRIx64 " nb_clusters %d" # block/qcow2-cluster.c qcow2_alloc_clusters_offset(void *co, uint64_t offset, int bytes) "co %p offset 0x%" PRIx64 " bytes %d"
tests/qemu-iotests/060 +6 -1
@@ -150,10 +150,15 @@ $QEMU_IO -c "$OPEN_RO" -c "read -P 1 0 512" | _filter_qemu_io echo echo "=== Testing overlap while COW is in flight ===" echo +BACKING_IMG=$TEST_IMG.base +TEST_IMG=$BACKING_IMG _make_test_img 1G + +$QEMU_IO -c 'write 0k 64k' "$BACKING_IMG" | _filter_qemu_io + # compat=0.10 is required in order to make the following discard actually # unallocate the sector rather than make it a zero sector - we want COW, after # all. -IMGOPTS='compat=0.10' _make_test_img 1G +IMGOPTS='compat=0.10' _make_test_img -b "$BACKING_IMG" 1G # Write two clusters, the second one enforces creation of an L2 table after # the first data cluster. $QEMU_IO -c 'write 0k 64k' -c 'write 512M 64k' "$TEST_IMG" | _filter_qemu_io
tests/qemu-iotests/060.out +4 -1
@@ -97,7 +97,10 @@ read 512/512 bytes at offset 0 === Testing overlap while COW is in flight === -Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=1073741824 +Formatting 'TEST_DIR/t.IMGFMT.base', fmt=IMGFMT size=1073741824 +wrote 65536/65536 bytes at offset 0 +64 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=1073741824 backing_file=TEST_DIR/t.IMGFMT.base wrote 65536/65536 bytes at offset 0 64 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) wrote 65536/65536 bytes at offset 536870912
Signature · 1 line
2.17.1
Re: [Qemu-devel] [PATCH v11 09/10] qcow2: skip writing zero buffers to empty COW areas
Quoted 46 lines · Anton Nefedov
If COW areas of the newly allocated clusters are zeroes on the backing image, efficient
bdrv_write_zeroes(flags=BDRV_REQ_ALLOCATE)can be used on the whole cluster instead of writing explicit zero buffers later inperform_cow().iotest 060:
write to the discarded cluster does not trigger COW anymore. Use a backing image instead.Signed-off-by Anton Nefedov <[email protected]>--- qapi/block-core.json | 4 +- block/qcow2.h | 6 +++ block/qcow2-cluster.c | 2 +- block/qcow2.c | 89 +++++++++++++++++++++++++++++++++++++- block/trace-events | 1 + tests/qemu-iotests/060 | 7 ++- tests/qemu-iotests/060.out | 5 ++- 7 files changed, 108 insertions(+), 6 deletions(-)qapi/block-core.json +3
-1@@ -3009,6 +3009,8 @@ # # @cor_write: a write due to copy-on-read (since 2.11) # +# @cluster_alloc_space: an allocation of file space for a cluster (since 4.0) +# # Since: 2.9 ## { 'enum': 'BlkdebugEvent', 'prefix': 'BLKDBG',@@ -3027,7 +3029,7 @@ 'pwritev_rmw_tail', 'pwritev_rmw_after_tail', 'pwritev', 'pwritev_zero', 'pwritev_done', 'empty_image_prepare', 'l1_shrink_write_table', 'l1_shrink_free_l2_clusters', - 'cor_write'] } + 'cor_write', 'cluster_alloc_space'] } ## # @BlkdebugInjectErrorOptions:block/qcow2.h +0
-0
block/qcow2.h typedef struct QCowL2Meta · line 395
Quoted 10 lines
block/qcow2.h +6
-0@@ -386,6 +386,12 @@ typedef struct QCowL2Meta*/ Qcow2COWRegion cow_end; +/* +* Indicates that COW regions are already handled and do not require +* any more processing. +*/ +bool skip_cow; +/**
hmm, around it, all comments starts from '/**', so, I think, yours should too. (this note doesn't touch your other comments)
Quoted 41 lines
block/qcow2.h +0
-0* The I/O vector with the data from the actual guest write request. * If non-NULL, this is meant to be merged together with the data block/qcow2-cluster.c +1
-1@@ -806,7 +806,7 @@ static int perform_cow(BlockDriverState *bs, QCowL2Meta *m)assert (start->offset + start->nb_bytes <= end->offset);assert (!m->data_qiov || m->data_qiov->size == data_bytes); -if (start->nb_bytes ==0 && end->nb_bytes ==0 ) { +if ((start->nb_bytes ==0 && end->nb_bytes ==0 ) || m->skip_cow) {return 0 ; }block/qcow2.c +11
-0@@ -2021,6 +2021,11 @@ static bool merge_cow(uint64_t offset, unsigned bytes,continue ; } +/* If COW regions are handled already, skip this too */ +if (m->skip_cow) { +continue ; + } +/* The data (middle) region must be immediately after the * start region */ if (l2meta_cow_start (m) + m->cow_start.nb_bytes != offset) {@@ -2046,6 +2051,77 @@ static bool merge_cow(uint64_t offset, unsigned bytes,return false; } +static bool is_unallocated (BlockDriverState *bs,int64_t offset,int64_t bytes) +{ +int64_t nr; +return !bytes || + (!bdrv_is_allocated_above (bs,NULL , offset, bytes, &nr) && nr == bytes); +}
block/qcow2.c static bool merge_cow(uint64_t offset, unsigned bytes, · line 2069
Quoted 10 lines
block/qcow2.c +10
-0+ +static bool is_zero_cow (BlockDriverState *bs,QCowL2Meta *m) +{ +/* +* This check is designed for optimization shortcut so it must be +* efficient. +* Instead of is_zero(), use is_unallocated() as it is faster (but not +* as accurate and can result in false negatives). +*/ + return is_unallocated(bs, m->offset + m->cow_start.offset,
here you add m->cow_start.offset ...[1]
Quoted 23 lines
block/qcow2.c +23
-0+ m->cow_start.nb_bytes) && +is_unallocated (bs, m->offset + m->cow_end.offset, + m->cow_end.nb_bytes); +} + +static int handle_alloc_space (BlockDriverState *bs,QCowL2Meta *l2meta) +{ +BDRVQcow2State *s = bs->opaque; +QCowL2Meta *m; + +if (!(bs->file->bs->supported_zero_flags &BDRV_REQ_ALLOCATE )) { +return 0 ; + } + +if (bs->encrypted) { +return 0 ; + } + +for (m = l2meta; m !=NULL ; m = m->next) { +int ret; + +if (!m->cow_start.nb_bytes && !m->cow_end.nb_bytes) { +continue ;
block/qcow2.c static bool merge_cow(uint64_t offset, unsigned bytes, · line 2102
Quoted 10 lines
block/qcow2.c +10
-0+ } + +if (!is_zero_cow (bs, m)) { +continue ; + } + +/* +* Conventional place for qcow2_pre_write_overlap_check() but in this +* case it is already done for these clusters +*/
to be honest, check is done for original request not, not for cow_regions, and, on the other hand,do_perform_cow_write _does_ the check.
So, I see two consistent ways:
1. you just add check here, like in do_perform_cow_write
2. improve check in qcow2_co_pwritev to cover the whole area, and drop checks from perform_cow.
I prefer 1, as it more helpful in case of bugs in the code (just check exactly what you are going to write in the next write).
block/qcow2.c static bool merge_cow(uint64_t offset, unsigned bytes, · line 2109
Quoted 7 lines
block/qcow2.c +7
-0+ +BLKDBG_EVENT (bs->file,BLKDBG_CLUSTER_ALLOC_SPACE ); +/* +* instead of writing zero COW buffers, +* efficiently zero out the whole clusters +*/ + ret = bdrv_co_pwrite_zeroes(bs->file, m->alloc_offset,
[1]... and here - not.
And perform_cow uses start too.
hmm..
On the other hand, here we just want to zero all allocated clusters, it is not related to cow regions. So, ok.
Quoted 5 lines
block/qcow2.c +5
-0+ m->nb_clusters * s->cluster_size, + BDRV_REQ_ALLOCATE); +if (ret <0 ) { +if (ret != -ENOTSUP && ret != -EAGAIN ) { +return ret;
block/qcow2.c static bool merge_cow(uint64_t offset, unsigned bytes, · line 2124
Quoted 10 lines
block/qcow2.c +10
-0+ } +continue ; + } + +trace_qcow2_skip_cow (qemu_coroutine_self (), m->offset, m->nb_clusters); + m->skip_cow = true; + } +return 0 ; +} +
[...]
with pre-write-check added:
Signature · 2 lines
Best regards, Vladimir
Re: [Qemu-devel] [PATCH v11 09/10] qcow2: skip writing zero buffers to empty COW areas
Quoted 50 lines · Vladimir Sementsov-Ogievskiy
Quoted 49 lines · Anton Nefedov
If COW areas of the newly allocated clusters are zeroes on the backing image, efficient
bdrv_write_zeroes(flags=BDRV_REQ_ALLOCATE)can be used on the whole cluster instead of writing explicit zero buffers later inperform_cow().iotest 060:
write to the discarded cluster does not trigger COW anymore. Use a backing image instead.Signed-off-by Anton Nefedov <[email protected]>--- qapi/block-core.json | 4 +- block/qcow2.h | 6 +++ block/qcow2-cluster.c | 2 +- block/qcow2.c | 89 +++++++++++++++++++++++++++++++++++++- block/trace-events | 1 + tests/qemu-iotests/060 | 7 ++- tests/qemu-iotests/060.out | 5 ++- 7 files changed, 108 insertions(+), 6 deletions(-)qapi/block-core.json +3
-1@@ -3009,6 +3009,8 @@ # # @cor_write: a write due to copy-on-read (since 2.11) # +# @cluster_alloc_space: an allocation of file space for a cluster (since 4.0) +# # Since: 2.9 ## { 'enum': 'BlkdebugEvent', 'prefix': 'BLKDBG',@@ -3027,7 +3029,7 @@ 'pwritev_rmw_tail', 'pwritev_rmw_after_tail', 'pwritev', 'pwritev_zero', 'pwritev_done', 'empty_image_prepare', 'l1_shrink_write_table', 'l1_shrink_free_l2_clusters', - 'cor_write'] } + 'cor_write', 'cluster_alloc_space'] } ## # @BlkdebugInjectErrorOptions:block/qcow2.h +0
-0@@ -386,6 +386,12 @@ typedef struct QCowL2Meta*/ Qcow2COWRegion cow_end;
Quoted 11 lines
block/qcow2.h typedef struct QCowL2Meta · line 395
Quoted 7 lines
block/qcow2.h +6
-0+/* +* Indicates that COW regions are already handled and do not require +* any more processing. +*/ +bool skip_cow; +/** hmm, around it, all comments starts from '/**', so, I think, yours should too. (this note doesn't touch your other comments)
that triggers a warning in checkpatch.pl
WARNING: Block comments use a leading /* on a separate line
Quoted 92 lines
Quoted 41 lines
block/qcow2.h +0
-0* The I/O vector with the data from the actual guest write request. * If non-NULL, this is meant to be merged together with the data block/qcow2-cluster.c +1
-1@@ -806,7 +806,7 @@ static int perform_cow(BlockDriverState *bs, QCowL2Meta *m)assert (start->offset + start->nb_bytes <= end->offset);assert (!m->data_qiov || m->data_qiov->size == data_bytes); -if (start->nb_bytes ==0 && end->nb_bytes ==0 ) { +if ((start->nb_bytes ==0 && end->nb_bytes ==0 ) || m->skip_cow) {return 0 ; }block/qcow2.c +11
-0@@ -2021,6 +2021,11 @@ static bool merge_cow(uint64_t offset, unsigned bytes,continue ; } +/* If COW regions are handled already, skip this too */ +if (m->skip_cow) { +continue ; + } +/* The data (middle) region must be immediately after the * start region */ if (l2meta_cow_start (m) + m->cow_start.nb_bytes != offset) {@@ -2046,6 +2051,77 @@ static bool merge_cow(uint64_t offset, unsigned bytes,return false; } +static bool is_unallocated (BlockDriverState *bs,int64_t offset,int64_t bytes) +{ +int64_t nr; +return !bytes || + (!bdrv_is_allocated_above (bs,NULL , offset, bytes, &nr) && nr == bytes); +}block/qcow2.c static bool merge_cow(uint64_t offset, unsigned bytes, · line 2069
Quoted 10 lines
block/qcow2.c +10
-0+ +static bool is_zero_cow (BlockDriverState *bs,QCowL2Meta *m) +{ +/* +* This check is designed for optimization shortcut so it must be +* efficient. +* Instead of is_zero(), use is_unallocated() as it is faster (but not +* as accurate and can result in false negatives). +*/ + return is_unallocated(bs, m->offset + m->cow_start.offset,here you add
m->cow_start.offset...[1]Quoted 23 lines
block/qcow2.c +23
-0+ m->cow_start.nb_bytes) && +is_unallocated (bs, m->offset + m->cow_end.offset, + m->cow_end.nb_bytes); +} + +static int handle_alloc_space (BlockDriverState *bs,QCowL2Meta *l2meta) +{ +BDRVQcow2State *s = bs->opaque; +QCowL2Meta *m; + +if (!(bs->file->bs->supported_zero_flags &BDRV_REQ_ALLOCATE )) { +return 0 ; + } + +if (bs->encrypted) { +return 0 ; + } + +for (m = l2meta; m !=NULL ; m = m->next) { +int ret; + +if (!m->cow_start.nb_bytes && !m->cow_end.nb_bytes) { +continue ;block/qcow2.c static bool merge_cow(uint64_t offset, unsigned bytes, · line 2102
Quoted 10 lines
block/qcow2.c +10
-0+ } + +if (!is_zero_cow (bs, m)) { +continue ; + } + +/* +* Conventional place for qcow2_pre_write_overlap_check() but in this +* case it is already done for these clusters +*/ to be honest, check is done for original request not, not for
cow_regions, and, on the other hand,do_perform_cow_write_does_ the check.
Quoted 8 lines
So, I see two consistent ways:
1. you just add check here, like indo_perform_cow_write
2. improve check inqcow2_co_pwritevto cover the whole area, and drop checks fromperform_cow.I prefer 1, as it more helpful in case of bugs in the code (just check exactly what you are going to write in the next write).
ok
block/qcow2.c +0 -0
@@ -2096,16 +2096,18 @@ static int handle_alloc_space(BlockDriverState
*bs, QCowL2Meta *l2meta)
continue;
}- /*
- * Conventional place for qcow2_pre_write_overlap_check() but
in this
- * case it is already done for these clusters
- */
-
- BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_ALLOC_SPACE);
/*
* instead of writing zero COW buffers,
* efficiently zero out the whole clusters */
+
+ ret = qcow2_pre_write_overlap_check(bs, 0, m->alloc_offset,
+ m->nb_clusters *s->cluster_size);
+ if (ret < 0) {
+ return ret;
+ }
+
+ BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_ALLOC_SPACE);
ret = bdrv_co_pwrite_zeroes(bs->file, m->alloc_offset,
m->nb_clusters * s->cluster_size,
BDRV_REQ_ALLOCATE);Quoted 39 lines
block/qcow2.c static bool merge_cow(uint64_t offset, unsigned bytes, · line 2109
Quoted 7 lines
block/qcow2.c +7
-0+ +BLKDBG_EVENT (bs->file,BLKDBG_CLUSTER_ALLOC_SPACE ); +/* +* instead of writing zero COW buffers, +* efficiently zero out the whole clusters +*/ + ret = bdrv_co_pwrite_zeroes(bs->file, m->alloc_offset,[1]... and here - not.
Andperform_cowuses start too.
hmm..On the other hand, here we just want to zero all allocated clusters, it is not related to cow regions. So, ok.
Quoted 5 lines
block/qcow2.c +5
-0+ m->nb_clusters * s->cluster_size, + BDRV_REQ_ALLOCATE); +if (ret <0 ) { +if (ret != -ENOTSUP && ret != -EAGAIN ) { +return ret;block/qcow2.c static bool merge_cow(uint64_t offset, unsigned bytes, · line 2124
Quoted 10 lines
block/qcow2.c +10
-0+ } +continue ; + } + +trace_qcow2_skip_cow (qemu_coroutine_self (), m->offset, m->nb_clusters); + m->skip_cow = true; + } +return 0 ; +} +[...]
with pre-write-check added:
Reviewed-by Vladimir Sementsov-Ogievskiy <[email protected]>
Re: [Qemu-devel] [PATCH v11 09/10] qcow2: skip writing zero buffers to empty COW areas
Quoted 63 lines · Anton Nefedov
Quoted 50 lines · Vladimir Sementsov-Ogievskiy
Quoted 49 lines · Anton Nefedov
If COW areas of the newly allocated clusters are zeroes on the backing image, efficient
bdrv_write_zeroes(flags=BDRV_REQ_ALLOCATE)can be used on the whole cluster instead of writing explicit zero buffers later inperform_cow().iotest 060:
write to the discarded cluster does not trigger COW anymore. Use a backing image instead.Signed-off-by Anton Nefedov <[email protected]>--- qapi/block-core.json | 4 +- block/qcow2.h | 6 +++ block/qcow2-cluster.c | 2 +- block/qcow2.c | 89 +++++++++++++++++++++++++++++++++++++- block/trace-events | 1 + tests/qemu-iotests/060 | 7 ++- tests/qemu-iotests/060.out | 5 ++- 7 files changed, 108 insertions(+), 6 deletions(-)qapi/block-core.json +3
-1@@ -3009,6 +3009,8 @@ # # @cor_write: a write due to copy-on-read (since 2.11) # +# @cluster_alloc_space: an allocation of file space for a cluster (since 4.0) +# # Since: 2.9 ## { 'enum': 'BlkdebugEvent', 'prefix': 'BLKDBG',@@ -3027,7 +3029,7 @@ 'pwritev_rmw_tail', 'pwritev_rmw_after_tail', 'pwritev', 'pwritev_zero', 'pwritev_done', 'empty_image_prepare', 'l1_shrink_write_table', 'l1_shrink_free_l2_clusters', - 'cor_write'] } + 'cor_write', 'cluster_alloc_space'] } ## # @BlkdebugInjectErrorOptions:block/qcow2.h +0
-0@@ -386,6 +386,12 @@ typedef struct QCowL2Meta*/ Qcow2COWRegion cow_end;Quoted 11 lines
block/qcow2.h typedef struct QCowL2Meta · line 395
Quoted 7 lines
block/qcow2.h +6
-0+/* +* Indicates that COW regions are already handled and do not require +* any more processing. +*/ +bool skip_cow; +/** hmm, around it, all comments starts from '/**', so, I think, yours should too. (this note doesn't touch your other comments)
Quoted 3 lines
that triggers a warning in checkpatch.pl
WARNING: Block comments use a leading /* on a separate line
I thing, it is the case, when we should ignore this warning.
Quoted 128 lines
Quoted 93 lines
Quoted 42 lines
block/qcow2.h +0
-0* The I/O vector with the data from the actual guest write request. * If non-NULL, this is meant to be merged together with the data block/qcow2-cluster.c +1
-1@@ -806,7 +806,7 @@ static int perform_cow(BlockDriverState *bs, QCowL2Meta *m)assert (start->offset + start->nb_bytes <= end->offset);assert (!m->data_qiov || m->data_qiov->size == data_bytes); -if (start->nb_bytes ==0 && end->nb_bytes ==0 ) { +if ((start->nb_bytes ==0 && end->nb_bytes ==0 ) || m->skip_cow) {return 0 ; }block/qcow2.c +11
-0@@ -2021,6 +2021,11 @@ static bool merge_cow(uint64_t offset, unsigned bytes,continue ; } +/* If COW regions are handled already, skip this too */ +if (m->skip_cow) { +continue ; + } +/* The data (middle) region must be immediately after the * start region */ if (l2meta_cow_start (m) + m->cow_start.nb_bytes != offset) {@@ -2046,6 +2051,77 @@ static bool merge_cow(uint64_t offset, unsigned bytes,return false; } +static bool is_unallocated (BlockDriverState *bs,int64_t offset,int64_t bytes) +{ +int64_t nr; +return !bytes || + (!bdrv_is_allocated_above (bs,NULL , offset, bytes, &nr) && nr == bytes); +}block/qcow2.c static bool merge_cow(uint64_t offset, unsigned bytes, · line 2069
Quoted 10 lines
block/qcow2.c +10
-0+ +static bool is_zero_cow (BlockDriverState *bs,QCowL2Meta *m) +{ +/* +* This check is designed for optimization shortcut so it must be +* efficient. +* Instead of is_zero(), use is_unallocated() as it is faster (but not +* as accurate and can result in false negatives). +*/ + return is_unallocated(bs, m->offset + m->cow_start.offset,here you add
m->cow_start.offset...[1]Quoted 23 lines
block/qcow2.c +23
-0+ m->cow_start.nb_bytes) && +is_unallocated (bs, m->offset + m->cow_end.offset, + m->cow_end.nb_bytes); +} + +static int handle_alloc_space (BlockDriverState *bs,QCowL2Meta *l2meta) +{ +BDRVQcow2State *s = bs->opaque; +QCowL2Meta *m; + +if (!(bs->file->bs->supported_zero_flags &BDRV_REQ_ALLOCATE )) { +return 0 ; + } + +if (bs->encrypted) { +return 0 ; + } + +for (m = l2meta; m !=NULL ; m = m->next) { +int ret; + +if (!m->cow_start.nb_bytes && !m->cow_end.nb_bytes) { +continue ;block/qcow2.c static bool merge_cow(uint64_t offset, unsigned bytes, · line 2102
Quoted 10 lines
block/qcow2.c +10
-0+ } + +if (!is_zero_cow (bs, m)) { +continue ; + } + +/* +* Conventional place for qcow2_pre_write_overlap_check() but in this +* case it is already done for these clusters +*/ to be honest, check is done for original request not, not for
cow_regions, and, on the other hand,do_perform_cow_write_does_ the check.Quoted 8 lines
So, I see two consistent ways:
1. you just add check here, like indo_perform_cow_write
2. improve check inqcow2_co_pwritevto cover the whole area, and drop checks fromperform_cow.I prefer 1, as it more helpful in case of bugs in the code (just check exactly what you are going to write in the next write).
ok
block/qcow2.c +0
-2@@ -2096,16 +2096,18 @@ static int handle_alloc_space(BlockDriverState *bs, QCowL2Meta *l2meta)continue ; } -/* -* Conventional place for qcow2_pre_write_overlap_check() but in this
block/qcow2.c +3
-4-* case it is already done for these clusters -*/ - -BLKDBG_EVENT (bs->file,BLKDBG_CLUSTER_ALLOC_SPACE );/* * instead of writing zero COW buffers, * efficiently zero out the whole clusters */ + + ret = qcow2_pre_write_overlap_check(bs, 0, m->alloc_offset, + m->nb_clusters *
s->cluster_size);
block/qcow2.c
Quoted 5 lines
block/qcow2.c +5
-0+if (ret <0 ) { +return ret; + } + +BLKDBG_EVENT (bs->file,BLKDBG_CLUSTER_ALLOC_SPACE );
ok
Quoted 43 lines
ret = bdrv_co_pwrite_zeroes(bs->file, m->alloc_offset, m->nb_clusters * s->cluster_size, BDRV_REQ_ALLOCATE);Quoted 39 lines
block/qcow2.c static bool merge_cow(uint64_t offset, unsigned bytes, · line 2109
Quoted 7 lines
block/qcow2.c +7
-0+ +BLKDBG_EVENT (bs->file,BLKDBG_CLUSTER_ALLOC_SPACE ); +/* +* instead of writing zero COW buffers, +* efficiently zero out the whole clusters +*/ + ret = bdrv_co_pwrite_zeroes(bs->file, m->alloc_offset,[1]... and here - not.
Andperform_cowuses start too.
hmm..On the other hand, here we just want to zero all allocated clusters, it is not related to cow regions. So, ok.
Quoted 5 lines
block/qcow2.c +5
-0+ m->nb_clusters * s->cluster_size, + BDRV_REQ_ALLOCATE); +if (ret <0 ) { +if (ret != -ENOTSUP && ret != -EAGAIN ) { +return ret;block/qcow2.c static bool merge_cow(uint64_t offset, unsigned bytes, · line 2124
Quoted 10 lines
block/qcow2.c +10
-0+ } +continue ; + } + +trace_qcow2_skip_cow (qemu_coroutine_self (), m->offset, m->nb_clusters); + m->skip_cow = true; + } +return 0 ; +} +[...]
with pre-write-check added:
Reviewed-by Vladimir Sementsov-Ogievskiy <[email protected]>
Signature · 2 lines
Best regards, Vladimir
Re: [Qemu-devel] [PATCH v11 09/10] qcow2: skip writing zero buffers to empty COW areas
In reply to Anton Nefedovblock/qcow2.c static coroutine_fn int qcow2_co_pwritev(BlockDriverState *bs, uint64_t offset, · line 2211
Quoted 11 lines · Anton Nefedov
block/qcow2.c +7
-0@@ -2126,24 +2202,33 @@ static coroutine_fn int qcow2_co_pwritev(BlockDriverState *bs, uint64_t offset, goto fail; } +qemu_co_mutex_unlock (&s->lock); + + ret =handle_alloc_space (bs, l2meta); +if (ret <0 ) { +qemu_co_mutex_lock (&s->lock); + goto fail; + }
I think you could add a brief comment before the handle_alloc_space() call to indicate what it does.
The patch looks good to me else, but since it seems you're changing it in the next revision I'll wait for it.
Berto
PATCH v11 10/10 iotest 134: test cluster-misaligned encrypted write
In reply to Anton NefedovCOW (even empty/zero) areas require encryption too
--- tests/qemu-iotests/134 | 9 +++++++++ tests/qemu-iotests/134.out | 10 ++++++++++ 2 files changed, 19 insertions(+)
tests/qemu-iotests/134 +9 -0
@@ -57,6 +57,15 @@ echo echo "== reading whole image ==" $QEMU_IO --object $SECRET -c "read 0 $size" --image-opts $IMGSPEC | _filter_qemu_io | _filter_testdir +echo +echo "== rewriting cluster part ==" +$QEMU_IO --object $SECRET -c "write -P 0xb 512 512" --image-opts $IMGSPEC | _filter_qemu_io | _filter_testdir + +echo +echo "== verify pattern ==" +$QEMU_IO --object $SECRET -c "read -P 0 0 512" --image-opts $IMGSPEC | _filter_qemu_io | _filter_testdir +$QEMU_IO --object $SECRET -c "read -P 0xb 512 512" --image-opts $IMGSPEC | _filter_qemu_io | _filter_testdir + echo echo "== rewriting whole image ==" $QEMU_IO --object $SECRET -c "write -P 0xa 0 $size" --image-opts $IMGSPEC | _filter_qemu_io | _filter_testdir
tests/qemu-iotests/134.out +10 -0
@@ -5,6 +5,16 @@ Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=134217728 encryption=on encrypt. read 134217728/134217728 bytes at offset 0 128 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +== rewriting cluster part == +wrote 512/512 bytes at offset 512 +512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) + +== verify pattern == +read 512/512 bytes at offset 0 +512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +read 512/512 bytes at offset 512 +512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) + == rewriting whole image == wrote 134217728/134217728 bytes at offset 0 128 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
Signature · 1 line
2.17.1