fix potential data loss in QEMU (missed flush)
cover fix potential data loss in QEMU (missed flush)
This series is a followup to my previous patch sent under "qcow2: add very final sync on QCOW2 image closing". Could be considered as v2.
This series addresses two problems:
1. bdrv_co_flush() at the end of bdrv_driver_pwritev() and bdrv_co_do_pwrite_zeroes() could be effectively no op. The problem is that bdrv_co_flush() is optimized internally this way to track real requests which were making changes in the image and without such requests from previous flush() do nothing. Requests are tracked using write_gen on BDS. This field should be incremented after write is completed but before flush is called.
This could be considered as potential data loss if caller relies on BDRV_REQ_FUA flag which means that such requests are really lands on the underlying device, which could not due to missed flush().
2. qcow2_update_header() must actually write header with BDRV_REQ_FUA flag as image processing on open relies on the fact that header is actual and it is not possible to reorder header modifications and image content modifications.
Changes:
* added patches 1 & 2
PATCH 1/3 block: create bdrv_advance_flush_gen() helper
The patch creates helper to advance flush generation.
Right now there is the only place which handles bs->write_gen, but this is going to be changed in the next patches. Unfortunately we need to precisely control the moment of flush() when BDRV_REQ_FUA is passed as a request flag. Generic processing inside bdrv_co_write_req_finish() is too late.
--- block/io.c | 2 +- include/block/block_int-common.h | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-)
block/io.c +1 -1
@@ -2147,7 +2147,7 @@ bdrv_co_write_req_finish(BdrvChild *child, int64_t offset, int64_t bytes,bdrv_check_request (offset, bytes, &error_abort); -qatomic_inc (&bs->write_gen); +bdrv_advance_flush_gen (bs);/* * Discard cannot extend the image, but in error handling cases, such as
include/block/block_int-common.h +5 -0
@@ -1373,6 +1373,11 @@ int bdrv_check_qiov_request(int64_t offset, int64_t bytes, QEMUIOVector *qiov, size_t qiov_offset, Error **errp); +static inline void bdrv_advance_flush_gen (BlockDriverState *bs) +{ +qatomic_inc (&bs->write_gen); +} +bool cbw_filter_present (void );#ifdef _WIN32
Signature · 1 line
2.43.5
Re: [PATCH 1/3] block: create bdrv_advance_flush_gen() helper
Quoted 35 lines · Denis V. Lunev
The patch creates helper to advance flush generation.
Right now there is the only place which handles
bs->write_gen, but this is going to be changed in the next patches. Unfortunately we need to precisely control the moment offlush()whenBDRV_REQ_FUAis passed as a request flag. Generic processing insidebdrv_co_write_req_finish()is too late.Signed-off-by Denis V. Lunev <[email protected]>CC Andrey Drobyshev <[email protected]>CC Kevin Wolf <[email protected]>CC Hanna Reitz <[email protected]>--- block/io.c | 2 +- include/block/block_int-common.h | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-)block/io.c +1
-1@@ -2147,7 +2147,7 @@ bdrv_co_write_req_finish(BdrvChild *child, int64_t offset, int64_t bytes,bdrv_check_request (offset, bytes, &error_abort); -qatomic_inc (&bs->write_gen); +bdrv_advance_flush_gen (bs);/* * Discard cannot extend the image, but in error handling cases, such as include/block/block_int-common.h +0
-0
include/block/block_int-common.h int bdrv_check_qiov_request(int64_t offset, int64_t bytes, · line 1380
Quoted 8 lines
include/block/block_int-common.h +5
-0@@ -1373,6 +1373,11 @@ int bdrv_check_qiov_request(int64_t offset, int64_t bytes, QEMUIOVector *qiov, size_t qiov_offset, Error **errp); +static inline void bdrv_advance_flush_gen (BlockDriverState *bs) +{ +qatomic_inc (&bs->write_gen); +} +
The name is misleading. In bdrv_co_flush() we're comparing bs->flushed_gen with bs->write_gen. Here we're incrementing bs->write_gen. Did you mean bdrv_advance_write_gen() maybe?
include/block/block_int-common.h int bdrv_check_qiov_request(int64_t offset, int64_t bytes, · line 1382
Quoted 2 lines
include/block/block_int-common.h +0
-0bool cbw_filter_present (void );
Minor issue: patch doesn't apply cleanly because of the declaration above. Probably was committed on a downstream branch?
Quoted 1 line
include/block/block_int-common.h +0
-0#ifdef _WIN32
PATCH 2/3 block: move bdrv_advance_flush_gen() earlier from bdrv_co_write_req_finish
In reply to Denis V. Lunev viaThis helper is mandatory to be called before bdrv_co_flush(). In the other case bdrv_co_flush() will be noop. This helper should be called after actual write is completed for subsequent flush to perform some work.
Actually this change is important, without it BDRV_REQ_FUA semantics is broken completely. flush() is not called. This smells like potential data loss if somebody relies on BDRV_REQ_FUA.
--- block/io.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-)
block/io.c +13 -6
@@ -1163,8 +1163,11 @@ bdrv_driver_pwritev(BlockDriverState *bs, int64_t offset, int64_t bytes, ret = drv->bdrv_co_writev (bs, sector_num, nb_sectors, qiov, flags); emulate_flags: -if (ret ==0 && emulate_fua) { - ret =bdrv_co_flush (bs); +if (ret ==0 ) { +bdrv_advance_flush_gen (bs); +if (emulate_fua) { + ret =bdrv_co_flush (bs); + } }if (qiov == &local_qiov) {
@@ -2077,8 +2080,11 @@ bdrv_co_do_pwrite_zeroes(BlockDriverState *bs, int64_t offset, int64_t bytes, } fail: -if (ret ==0 && need_flush) { - ret =bdrv_co_flush (bs); +if (ret ==0 ) { +bdrv_advance_flush_gen (bs); +if (need_flush) { + ret =bdrv_co_flush (bs); + } }qemu_vfree (buf);return ret;
@@ -2147,8 +2153,6 @@ bdrv_co_write_req_finish(BdrvChild *child, int64_t offset, int64_t bytes,bdrv_check_request (offset, bytes, &error_abort); -bdrv_advance_flush_gen (bs); -/* * Discard cannot extend the image, but in error handling cases, such as * when reverting a qcow2 cluster allocation, the discarded range can pass
@@ -3720,6 +3724,7 @@ int coroutine_fn bdrv_co_pdiscard(BdrvChild *child, int64_t offset, } ret =0 ; out: +bdrv_advance_flush_gen (bs);bdrv_co_write_req_finish (child, req.offset, req.bytes, &req, ret);tracked_request_end (&req);bdrv_dec_in_flight (bs);
@@ -3994,6 +4000,7 @@ static int coroutine_fn GRAPH_RDLOCK bdrv_co_copy_range_internal( bytes, read_flags, write_flags); } +bdrv_advance_flush_gen (dst->bs);bdrv_co_write_req_finish (dst, dst_offset, bytes, &req, ret);tracked_request_end (&req);bdrv_dec_in_flight (dst->bs);
@@ -4187,6 +4194,7 @@ int coroutine_fn bdrv_co_truncate(BdrvChild *child, int64_t offset, bool exact, }else { offset = bs->total_sectors *BDRV_SECTOR_SIZE ; } +bdrv_advance_flush_gen (bs);/* * It's possible that truncation succeeded but bdrv_refresh_total_sectors * failed, but the latter doesn't affect how we should finish the request.
Signature · 1 line
2.43.5
Re: [PATCH 2/3] block: move bdrv_advance_flush_gen() earlier from bdrv_co_write_req_finish
Quoted 9 lines · Denis V. Lunev
This helper is mandatory to be called before
bdrv_co_flush(). In the other casebdrv_co_flush()will be noop. This helper should be called after actual write is completed for subsequent flush to perform some work.Actually this change is important, without it
BDRV_REQ_FUAsemantics is broken completely.flush()is not called. This smells like potential data loss if somebody relies onBDRV_REQ_FUA.
Quoted 7 lines
Signed-off-by Denis V. Lunev <[email protected]>CC Andrey Drobyshev <[email protected]>CC Kevin Wolf <[email protected]>CC Hanna Reitz <[email protected]>--- block/io.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-)
Definitely an improvement, but if possible, I would still prefer keeping the increment it in bdrv_co_write_req_finish(), because that is where all write requests eventually end up in, and that’s also where dirty bitmaps are handled. It seems slightly error-prone to manually have to call this every time the BDS data is modified.
Can we maybe instead pull the FUA emulation out into bdrv_aligned_pwritev(), after bdrv_co_write_req_finish()?
I think we can only pull out the flush, not the FUA emulation detection because for write-zeroes, we have to check either supported_zero_flags or supported_write_flags, depending on whether we fall back or not. But bdrv_driver_pwritev() and bdrv_co_do_pwrite_zeroes() could return an need_emulate_fua flag via pointer parameter, and if set, bdrv_aligned_pwritev() can then do the flush after bdrv_co_write_req_finish(). (This would also allow dropping both “only flush once at the end” blocks.)
Maybe it would then also make sense to move the bdrv_co_flush() into bdrv_co_write_req_finish(), and add a need_flush parameter to it, so that it can see whether the whole request (write + flush) was successful.
Hanna
Re: [PATCH 2/3] block: move bdrv_advance_flush_gen() earlier from bdrv_co_write_req_finish
Quoted 35 lines · Hanna Czenczek
Quoted 9 lines · Denis V. Lunev
This helper is mandatory to be called before
bdrv_co_flush(). In the other casebdrv_co_flush()will be noop. This helper should be called after actual write is completed for subsequent flush to perform some work.Actually this change is important, without it
BDRV_REQ_FUAsemantics is broken completely.flush()is not called. This smells like potential data loss if somebody relies onBDRV_REQ_FUA.Quoted 7 lines
Signed-off-by Denis V. Lunev <[email protected]>CC Andrey Drobyshev <[email protected]>CC Kevin Wolf <[email protected]>CC Hanna Reitz <[email protected]>--- block/io.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-)Definitely an improvement, but if possible, I would still prefer keeping the increment it in
bdrv_co_write_req_finish(), because that is where all write requests eventually end up in, and that’s also where dirty bitmaps are handled. It seems slightly error-prone to manually have to call this every time the BDS data is modified.Can we maybe instead pull the FUA emulation out into
bdrv_aligned_pwritev(), afterbdrv_co_write_req_finish()?I think we can only pull out the flush, not the FUA emulation detection because for write-zeroes, we have to check either
supported_zero_flagsorsupported_write_flags, depending on whether we fall back or not. Butbdrv_driver_pwritev()andbdrv_co_do_pwrite_zeroes()could return anneed_emulate_fuaflag via pointer parameter, and if set,bdrv_aligned_pwritev()can then do the flush after bdrv_co_write_req_finish(). (This would also allow dropping both “only flush once at the end” blocks.)
Quoted 6 lines
Maybe it would then also make sense to move the
bdrv_co_flush()intobdrv_co_write_req_finish(), and add aneed_flushparameter to it, so that it can see whether the whole request (write + flush) was successful.Hanna
I have tried to think about this problem in a this and that way and found yet another missed place in the code. This has frustrated me even more.
For the reference:
raw_co_prw(BlockDriverState *bs, int64_t *offset_ptr, uint64_t bytes, QEMUIOVector *qiov, int type, int flags)
{
...
assert(qiov->size == bytes);
ret = raw_thread_pool_submit(handle_aiocb_rw, &acb); if (ret == 0 && (flags & BDRV_REQ_FUA)) {
/* TODO Use pwritev2() instead if it's available */ ret = bdrv_co_flush(bs);
}
goto out; /* Avoid the compiler err of unused label */
This definitely states that the code is cumbersome and you right that I need to try harder to move FUA emulation out into generic code.
I'll invest more here.
Thank you for the point,
Den
PATCH 3/3 qcow2: add very final sync on QCOW2 image closing
In reply to Denis V. Lunev viaqcow2_header_updated() is the final call during image close. This means after this point we will have no IO operations on this file descriptor. This assumption has been validated via 'strace' which clearly confirms that this is very final write and there is no sync after this point.
There is almost no problem when the image is residing in local filesystem except that we will have image check if the chage will not reach disk before powerloss, but with a network or distributed filesystem we come to trouble. The change could be in flight and we can miss this data on other node like during migration.
The patch adds BDRV_REQ_FUA to the write request to do the trick.
--- block/qcow2.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
block/qcow2.c +1 -1
@@ -3255,7 +3255,7 @@ int qcow2_update_header(BlockDriverState *bs) }/* Write the new header */ - ret =bdrv_pwrite (bs->file,0 , s->cluster_size, header,0 ); + ret =bdrv_pwrite (bs->file,0 , s->cluster_size, header,BDRV_REQ_FUA );if (ret <0 ) { goto fail; }
Signature · 1 line
2.43.5
Quoted 22 lines · Denis V. Lunev
This series is a followup to my previous patch sent under "qcow2: add very final sync on QCOW2 image closing". Could be considered as v2.
This series addresses two problems:
1.bdrv_co_flush()at the end ofbdrv_driver_pwritev()andbdrv_co_do_pwrite_zeroes()could be effectively no op. The problem is thatbdrv_co_flush()is optimized internally this way to track real requests which were making changes in the image and without such requests from previousflush()do nothing. Requests are tracked usingwrite_genon BDS. This field should be incremented after write is completed but before flush is called.This could be considered as potential data loss if caller relies on
BDRV_REQ_FUAflag which means that such requests are really lands on the underlying device, which could not due to missedflush().2.
qcow2_update_header()must actually write header withBDRV_REQ_FUAflag as image processing on open relies on the fact that header is actual and it is not possible to reorder header modifications and image content modifications.
Quoted 8 lines
Changes:
* added patches 1 & 2Signed-off-by Denis V. Lunev <[email protected]>CC Andrey Drobyshev <[email protected]>CC Kevin Wolf <[email protected]>CC Hanna Reitz <[email protected]>
ping
Quoted 22 lines · Denis V. Lunev
This series is a followup to my previous patch sent under "qcow2: add very final sync on QCOW2 image closing". Could be considered as v2.
This series addresses two problems:
1.bdrv_co_flush()at the end ofbdrv_driver_pwritev()andbdrv_co_do_pwrite_zeroes()could be effectively no op. The problem is thatbdrv_co_flush()is optimized internally this way to track real requests which were making changes in the image and without such requests from previousflush()do nothing. Requests are tracked usingwrite_genon BDS. This field should be incremented after write is completed but before flush is called.This could be considered as potential data loss if caller relies on
BDRV_REQ_FUAflag which means that such requests are really lands on the underlying device, which could not due to missedflush().2.
qcow2_update_header()must actually write header withBDRV_REQ_FUAflag as image processing on open relies on the fact that header is actual and it is not possible to reorder header modifications and image content modifications.
Quoted 8 lines
Changes:
* added patches 1 & 2Signed-off-by Denis V. Lunev <[email protected]>CC Andrey Drobyshev <[email protected]>CC Kevin Wolf <[email protected]>CC Hanna Reitz <[email protected]>
ping
Quoted 22 lines · Denis V. Lunev
This series is a followup to my previous patch sent under "qcow2: add very final sync on QCOW2 image closing". Could be considered as v2.
This series addresses two problems:
1.bdrv_co_flush()at the end ofbdrv_driver_pwritev()andbdrv_co_do_pwrite_zeroes()could be effectively no op. The problem is thatbdrv_co_flush()is optimized internally this way to track real requests which were making changes in the image and without such requests from previousflush()do nothing. Requests are tracked usingwrite_genon BDS. This field should be incremented after write is completed but before flush is called.This could be considered as potential data loss if caller relies on
BDRV_REQ_FUAflag which means that such requests are really lands on the underlying device, which could not due to missedflush().2.
qcow2_update_header()must actually write header withBDRV_REQ_FUAflag as image processing on open relies on the fact that header is actual and it is not possible to reorder header modifications and image content modifications.
Quoted 8 lines
Changes:
* added patches 1 & 2Signed-off-by Denis V. Lunev <[email protected]>CC Andrey Drobyshev <[email protected]>CC Kevin Wolf <[email protected]>CC Hanna Reitz <[email protected]>
ping