qcow2: silent corruption when a dirty image becomes writable
v2 cover qcow2: silent corruption when a dirty image becomes writable
Today I have faced real data corrupt from our customer with the situation very close to the one addressed in the patch "qcow2: do not try to clear the dirty bit on a read-only node" and that is interesting. I was really unsure that design is correct but with today case I can say that correct thing was done.
The problem
-----------
qcow2_do_open() repairs an image carrying QCOW2_INCOMPAT_DIRTY, but only for a node that is writable from the start:
if (!(flags & BDRV_O_CHECK) && bdrv_is_writable(bs) &&
(s->incompatible_features & QCOW2_INCOMPAT_DIRTY)) {A node opened read-only skips it, correctly, since it resolves nothing and writes nothing. Nothing revisits the question when that same node later becomes writable, and bdrv_reopen() is not an exotic way to get
there: commit_active_start() and commit_start() both reopen the base read-write for the duration of the job, so an ordinary block-commit onto a read-only backing file is enough.
With lazy refcounts the refcount blocks are not written again once the dirty bit is set, so an image left behind by a killed QEMU has an on-disk refcount block that accounts for the metadata clusters and nothing else. Every data cluster reads as free. s->free_cluster_index starts at 0, so the first allocation after such a reopen starts at the front of the image and hands out clusters that L2 entries still point at.
The result is aliasing: two guest offsets mapped onto one host cluster, so the guest reads back data belonging to some other offset. Nothing about this fails. No I/O error is reported, the corrupt bit stays clear, and a clean close clears the dirty bit as well, so no later open will repair the image either. Afterwards qemu-img check reports
ERROR cluster N refcount=1 reference=2
ERROR cluster N refcount=0 reference=1
ERROR OFLAG_COPIED data cluster: l2_entry=<host>|COPIED refcount=0
and the only runtime witness, if something eventually frees one of those clusters, is a bare
qcow2_free_clusters failed: Invalid argument
on stderr, with the guest none the wiser.
How it looked in production
---------------------------
A VM was killed while its storage was unavailable, leaving a 100 GiB volume dirty. It came back with a snapshot-revert overlay on top, so the volume itself was now the read-only backing file, and the overlay was committed into it two and a half hours later. 8082 host clusters ended up referenced by two L2 entries each, roughly 8 GiB of guest data cross-mapped. The guest filesystem began failing metadata verification on buffers holding fragments of unrelated files.
Two properties of the damage are worth recording, because they are what told us this was not a race:
- aliasing is exactly two-way, never three or more, which is a single monotonic sweep of the allocator rather than a window hit repeatedly;
- it stops dead at the host cluster that was the image end at the moment the volume was made writable. Everything allocated after that point is intact.
qemu-img check -r all makes the metadata self-consistent again, and then honestly reports no errors, but it cannot un-alias anything. The guest data stays wrong.
Reproducer
----------
Under a second, no guest and no block job required:
qemu-img create -f qcow2 -o compat=1.1,lazy_refcounts=on base.qcow2 1G
qemu-io -f qcow2 -c "write -P 0xaa 0 100M" -c flush \
-c "sigraise 9" base.qcow2
qemu-io -r -f qcow2 base.qcow2 \
<<< $'reopen -w\nwrite -P 0xbb 900M 100M\nquit' qemu-io -r -f qcow2 -c "read -v 0 16" base.qcow2
# 00000000: bb bb bb bb bb bb bb bb bb bb bb bb bb bb bb bb
The flush is load-bearing: it writes out the L2 cache but not the refcount blocks, which is exactly the asymmetry the bug needs. Guest fsyncs supply it in production, so a long-running VM is the ideal victim. qemu-img commit of an overlay reaches the same state through commit_active_start().
v1:
https://lore.kernel.org/qemu-devel/[email protected]/
Changes in v2
-------------
- A failed repair no longer falls through to qcow2_reopen_bitmaps_rw(). qcow2_signal_corruption() has already cleared bs->drv at that point, so there is nothing left to reopen. bdrv_reopen() also reported success in that case and handed the caller a dead node, which needed a block layer change, patch 2. (Hanna)
- Repair only when the node has just become writable. With lazy refcounts the dirty bit stays set for the whole writable session, so repairing whenever the node is writable made every read-write to read-write reopen, blockdev-reopen changing l2-cache-size for instance, scan the metadata of a live image under bdrv_drain_all() and clear the dirty bit its session still owns. Patch 3 records the writability the reopen started from. (Andrey)
- Three new cases in iotests 039: the read-write reopen that must not check the image, the failed repair that must fail the reopen, and the read-only reopen that must keep the dirty bit.
Notes for review
----------------
- The repair still runs under bdrv_drain_all(), so a block-commit onto a large dirty base stalls guest I/O for the length of a full metadata scan. Rejecting the reopen instead would be cheap, but block-commit depends on it succeeding, so repairing in place is the only option.
- .bdrv_reopen_commit_post() runs after the transaction is committed and cannot roll anything back, so a negative return value reports an unusable node rather than rejecting the reopen. That is unusual, and it is the only channel available: there is no failable hook once the node holds BLK_PERM_WRITE.
Denis V. Lunev (4):
qcow2: do not clear the dirty bit when reopening a read-only node
block: let bdrv_reopen_commit_post() report a failure
block: remember the writability a reopen starts from
qcow2: repair a dirty image when it becomes writable
block.c | 19 +++++++-- block/qcow2.c | 29 +++++++++++-- include/block/block-common.h | 1 + include/block/block_int-common.h | 5 ++- tests/qemu-iotests/039 | 70 ++++++++++++++++++++++++++++++++ tests/qemu-iotests/039.out | 41 +++++++++++++++++++ 6 files changed, 156 insertions(+), 9 deletions(-)
base-commit: 3e3ccab106f879b1512f8e0d51a827dd4de30e22
Signature · 1 line
2.53.0
PATCH v2 1/4 qcow2: do not clear the dirty bit when reopening a read-only node
qcow2_reopen_prepare() clears the dirty bit whenever the node is reopened read-only, with an unguarded header write. A read-only node can still be dirty, inherited from an earlier writable session, and it holds no BLK_PERM_WRITE to resolve that. A read-only to read-only reopen of a dirty image therefore fails outright:
$ qemu-io -r -f qcow2 dirty.qcow2 <<< $'reopen -r\nquit' qemu-io: failed while preparing to reopen image 'dirty.qcow2'
Clear it only for a node that is writable now, the predicate qcow2_do_open() already uses for the repair. bdrv_is_writable() also excludes an inactive node, whose header must not be touched either.
--- block/qcow2.c | 8 +++++--- tests/qemu-iotests/039 | 10 ++++++++++ tests/qemu-iotests/039.out | 5 +++++ 3 files changed, 20 insertions(+), 3 deletions(-)
block/qcow2.c +5 -3
@@ -2102,9 +2102,11 @@ qcow2_reopen_prepare(BDRVReopenState *state,BlockReopenQueue *queue, goto fail; } - ret =qcow2_mark_clean (state->bs); -if (ret <0 ) { - goto fail; +if (bdrv_is_writable (state->bs)) { + ret =qcow2_mark_clean (state->bs); +if (ret <0 ) { + goto fail; + } } }
tests/qemu-iotests/039 +10 -0
@@ -95,6 +95,16 @@ $QEMU_IMG info --image-opts \ # The dirty bit must still be set: this open never wrote any guest data _qcow2_dump_header | grep incompatible_features +echo +echo "== Read-only reopen must not clear the dirty bit ==" + +# A read-only node cannot write the header, and must keep the dirty bit +$QEMU_IO -r -c "reopen -r" -c "read -P 0x5a 0 512" "$TEST_IMG" \ + | _filter_qemu_io + +# The dirty bit must still be set +_qcow2_dump_header | grep incompatible_features + echo echo "== Repairing the image file must succeed =="
tests/qemu-iotests/039.out +5 -0
@@ -27,6 +27,11 @@ incompatible_features [0] == Read-only open must not crash on close == incompatible_features [0] +== Read-only reopen must not clear the dirty bit == +read 512/512 bytes at offset 0 +512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +incompatible_features [0] + == Repairing the image file must succeed == ERROR cluster 5 refcount=0 reference=1 Rebuilding refcount structure
Signature · 1 line
2.53.0
PATCH v2 3/4 block: remember the writability a reopen starts from
In reply to Denis V. Lunevbdrv_reopen_commit() updates bs->open_flags, so by the time .bdrv_reopen_commit_post() runs a driver can no longer tell whether the node has just become writable or was writable all along. Only the transition is worth reacting to.
Record the value while the queue is built, next to the other pre-reopen state BDRVReopenState already keeps.
--- block.c | 1 + include/block/block-common.h | 1 + 2 files changed, 2 insertions(+)
block.c +1 -0
@@ -4473,6 +4473,7 @@ bdrv_reopen_queue_child(BlockReopenQueue *bs_queue, BlockDriverState *bs, bs_entry->state.options = options; bs_entry->state.explicit_options = explicit_options; bs_entry->state.flags = flags; + bs_entry->state.was_writable =bdrv_is_writable (bs);/* * If keep_old_opts is false then it means that unspecified
include/block/block-common.h +1 -0
@@ -360,6 +360,7 @@ typedef struct BDRVReopenState {int flags;BlockdevDetectZeroesOptions detect_zeroes;bool backing_missing; +bool was_writable;/* bs->open_flags is updated on commit */ BlockDriverState *old_backing_bs;/* keep pointer for permissions update */ BlockDriverState *old_file_bs;/* keep pointer for permissions update */ QDict *options;
Signature · 1 line
2.53.0
PATCH v2 4/4 qcow2: repair a dirty image when it becomes writable
In reply to Denis V. LunevA dirty image must be repaired before anything allocates a cluster in it. qcow2_do_open() does that, but only for a node that is writable from the start. A node opened read-only skips it, and nothing revisits the question once that node becomes writable, which block-commit does
routinely: commit_active_start() and commit_start() reopen the base read-write for the duration of the job.
With lazy refcounts the on-disk refcount block then still accounts for the metadata clusters only, so the allocator restarts at the front of the image and hands out clusters that L2 entries point at. Two guest offsets end up sharing one host cluster. Nothing fails, the corrupt bit stays clear, and a clean close clears the dirty bit, so no later open repairs the image either.
Do the repair in qcow2_reopen_commit_post(). bdrv_reopen_prepare() runs before bdrv_list_refresh_perms(), so it holds no BLK_PERM_WRITE and, with auto-read-only, bs->file may still have an O_RDONLY descriptor. commit_post cannot reject the reopen, so signal corruption if the repair fails rather than let writes alias live clusters. That drops bs->drv, so return the error and skip the bitmaps. An inactive node is
skipped: bdrv_activate() calls qcow2_do_open() again through qcow2_co_invalidate_cache().
With lazy refcounts the dirty bit stays set for the whole writable session, so a node that is merely writable says nothing. Repair only when it has just become writable, which BDRVReopenState remembers:
a read-write to read-write reopen must not scan a live image.
--- block/qcow2.c | 17 +++++++++++ tests/qemu-iotests/039 | 60 ++++++++++++++++++++++++++++++++++++++ tests/qemu-iotests/039.out | 36 +++++++++++++++++++++++ 3 files changed, 113 insertions(+)
block/qcow2.c +17 -0
@@ -2147,8 +2147,25 @@ static void qcow2_reopen_commit(BDRVReopenState *state)static int qcow2_reopen_commit_post (BDRVReopenState *state,Error **errp) { +BDRVQcow2State *s = state->bs->opaque; +GRAPH_RDLOCK_GUARD_MAINLOOP (); +if (!state->was_writable &&bdrv_is_writable (state->bs) && + (s->incompatible_features &QCOW2_INCOMPAT_DIRTY )) { +BdrvCheckResult result = {0 }; +int ret; + + ret =bdrv_check (state->bs, &result,BDRV_FIX_ERRORS |BDRV_FIX_LEAKS ); +if (ret <0 || result.check_errors) { +/* This clears bs->drv, there is nothing left to reopen */ +qcow2_signal_corruption (state->bs, true,-1 ,-1 , +"Could not repair dirty image" ); +error_setg (errp,"Could not repair dirty image" ); +return -EIO ; + } + } +if (state->flags &BDRV_O_RDWR ) {Error *local_err =NULL ;
tests/qemu-iotests/039 +60 -0
66 lines not on this page · raw message
tests/qemu-iotests/039.out +36 -0
@@ -64,6 +64,42 @@ wrote 512/512 bytes at offset 0 512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) incompatible_features [] +== Reopening a dirty image read/write should repair it == +Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=134217728 +wrote 512/512 bytes at offset 0 +512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +./common.rc: Killed ( VALGRIND_QEMU="${VALGRIND_QEMU_IO}" _qemu_proc_exec "${VALGRIND_LOGFILE}" "$QEMU_IO_PROG" $QEMU_IO_ARGS "$@" ) +incompatible_features [0] +ERROR cluster 5 refcount=0 reference=1 +Rebuilding refcount structure +Repairing cluster 1 refcount=1 reference=0 +Repairing cluster 2 refcount=1 reference=0 +wrote 512/512 bytes at offset 1048576 +512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +read 512/512 bytes at offset 0 +512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +incompatible_features [] +No errors were found on the image. + +== A read/write reopen must not check the image == +Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=134217728 +wrote 512/512 bytes at offset 0 +512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +./common.rc: Killed ( VALGRIND_QEMU="${VALGRIND_QEMU_IO}" _qemu_proc_exec "${VALGRIND_LOGFILE}" "$QEMU_IO_PROG" $QEMU_IO_ARGS "$@" ) +incompatible_features [0] + +== A failed repair must fail the reopen == +Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=134217728 +wrote 512/512 bytes at offset 0 +512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +./common.rc: Killed ( VALGRIND_QEMU="${VALGRIND_QEMU_IO}" _qemu_proc_exec "${VALGRIND_LOGFILE}" "$QEMU_IO_PROG" $QEMU_IO_ARGS "$@" ) +ERROR cluster 5 refcount=0 reference=1 +Rebuilding refcount structure +qemu-io: ERROR writing refblock: Input/output error +qcow2: Marking image as corrupt: Could not repair dirty image; further corruption events will be suppressed +qemu-io: Could not repair dirty image +read failed: No medium found + == Creating an image file with lazy_refcounts=off == Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=134217728 wrote 512/512 bytes at offset 0
Signature · 1 line
2.53.0
PATCH v2 2/4 block: let bdrv_reopen_commit_post() report a failure
In reply to Denis V. LunevThe callback runs after bdrv_reopen_multiple() has committed the transaction, so it cannot reject the reopen. It can still find that the node it has just made writable is unusable, and has no way to say
so: bdrv_reopen() returns success and the caller carries on.
Give it a return value and an Error argument. The reopen stays committed, the error only reports that the node is gone. Every queued node still gets its callback, the first error is the one reported. qcow2 is the only implementation and does not fail yet.
--- block.c | 18 +++++++++++++++--- block/qcow2.c | 4 +++- include/block/block_int-common.h | 5 +++-- 3 files changed, 21 insertions(+), 6 deletions(-)
block.c +15 -3
@@ -4653,15 +4653,27 @@ int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp)tran_commit (tran);bdrv_graph_wrunlock (); + ret =0 ; QTAILQ_FOREACH_REVERSE(bs_entry, bs_queue, entry) {BlockDriverState *bs = bs_entry->state.bs; +Error *local_err =NULL ; +int commit_ret; -if (bs->drv->bdrv_reopen_commit_post) { - bs->drv->bdrv_reopen_commit_post (&bs_entry->state); +if (!bs->drv->bdrv_reopen_commit_post) { +continue ; + } + + commit_ret = bs->drv->bdrv_reopen_commit_post (&bs_entry->state, + &local_err); +if (commit_ret <0 && ret ==0 ) { +/* Committed already, so report the first failure and go on */ +error_propagate (errp, local_err); + ret = commit_ret; + }else { +error_free (local_err); } } - ret =0 ; goto cleanup; abort:
block/qcow2.c +3 -1
@@ -2145,7 +2145,7 @@ static void qcow2_reopen_commit(BDRVReopenState *state)g_free (state->opaque); } -static void qcow2_reopen_commit_post (BDRVReopenState *state) +static int qcow2_reopen_commit_post (BDRVReopenState *state,Error **errp) {GRAPH_RDLOCK_GUARD_MAINLOOP ();
@@ -2163,6 +2163,8 @@ static void qcow2_reopen_commit_post(BDRVReopenState *state) bdrv_get_node_name(state->bs)); } } + +return 0 ; }static void qcow2_reopen_abort (BDRVReopenState *state)
include/block/block_int-common.h +3 -2
@@ -239,8 +239,9 @@ struct BlockDriver { BDRVReopenState *reopen_state, BlockReopenQueue *queue, Error **errp); void GRAPH_UNLOCKED_PTR (*bdrv_reopen_commit)(BDRVReopenState *reopen_state); - void GRAPH_UNLOCKED_PTR (*bdrv_reopen_commit_post)( -BDRVReopenState *reopen_state); +/* Cannot fail the reopen, an error only reports an unusable node */ + int GRAPH_UNLOCKED_PTR (*bdrv_reopen_commit_post)( +BDRVReopenState *reopen_state,Error **errp); void GRAPH_UNLOCKED_PTR (*bdrv_reopen_abort)(BDRVReopenState *reopen_state);void (*bdrv_join_options)(QDict *options,QDict *old_options);
Signature · 1 line
2.53.0
Re: [PATCH v2 1/4] qcow2: do not clear the dirty bit when reopening a read-only node
In reply to Denis V. LunevQuoted 70 lines · Denis V. Lunev
qcow2_reopen_prepare()clears the dirty bit whenever the node is reopened read-only, with an unguarded header write. A read-only node can still be dirty, inherited from an earlier writable session, and it holds noBLK_PERM_WRITEto resolve that. A read-only to read-only reopen of a dirty image therefore fails outright:$ qemu-io -r -f qcow2 dirty.qcow2 <<< $'reopen -r\nquit' qemu-io: failed while preparing to reopen image 'dirty.qcow2'Clear it only for a node that is writable now, the predicate
qcow2_do_open()already uses for the repair.bdrv_is_writable()also excludes an inactive node, whose header must not be touched either.Signed-off-by Denis V. Lunev <[email protected]>CC Kevin Wolf <[email protected]>CC Hanna Reitz <[email protected]>CC Andrey Drobyshev <[email protected]>--- block/qcow2.c | 8 +++++--- tests/qemu-iotests/039 | 10 ++++++++++ tests/qemu-iotests/039.out | 5 +++++ 3 files changed, 20 insertions(+), 3 deletions(-)block/qcow2.c +5
-3@@ -2102,9 +2102,11 @@ qcow2_reopen_prepare(BDRVReopenState *state,BlockReopenQueue *queue, goto fail; } - ret =qcow2_mark_clean (state->bs); -if (ret <0 ) { - goto fail; +if (bdrv_is_writable (state->bs)) { + ret =qcow2_mark_clean (state->bs); +if (ret <0 ) { + goto fail; + } } }tests/qemu-iotests/039 +10
-0@@ -95,6 +95,16 @@ $QEMU_IMG info --image-opts \ # The dirty bit must still be set: this open never wrote any guest data _qcow2_dump_header | grep incompatible_features +echo +echo "== Read-only reopen must not clear the dirty bit ==" + +# A read-only node cannot write the header, and must keep the dirty bit +$QEMU_IO -r -c "reopen -r" -c "read -P 0x5a 0 512" "$TEST_IMG" \ + | _filter_qemu_io + +# The dirty bit must still be set +_qcow2_dump_header | grep incompatible_features + echo echo "== Repairing the image file must succeed =="tests/qemu-iotests/039.out +0
-0@@ -27,6 +27,11 @@ incompatible_features [0] == Read-only open must not crash on close ==
tests/qemu-iotests/039.out incompatible_features [0] · line 37
Quoted 10 lines
tests/qemu-iotests/039.out +5
-0incompatible_features [0] +== Read-only reopen must not clear the dirty bit == +read 512/512 bytes at offset 0 +512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +incompatible_features [0] + == Repairing the image file must succeed == ERROR cluster 5 refcount=0 reference=1 Rebuilding refcount structure
bdrv_is_writable() checks pre-reopen state. So IMHO it'd also be nice to check RW->RO reopen and that it does clear the dirty bit. Smth like:
Quoted 3 lines
_make_test_img-o "compat=1.1,lazy_refcounts=on" $size
$QEMU_IO -c "write -P0x5a0 512" -c "reopen -r" "$TEST_IMG" |_filter_qemu_io_qcow2_dump_header| grepincompatible_features
A write operation with lazy refcounts must set dirty bit. And upon RW->RO reopen the dirty bit must be cleared.
Still, with or without this case covered:
Re: [PATCH v2 2/4] block: let bdrv_reopen_commit_post() report a failure
In reply to Denis V. LunevQuoted 84 lines · Denis V. Lunev
The callback runs after
bdrv_reopen_multiple()has committed the transaction, so it cannot reject the reopen. It can still find that the node it has just made writable is unusable, and has no way to say
so:bdrv_reopen()returns success and the caller carries on.Give it a return value and an Error argument. The reopen stays committed, the error only reports that the node is gone. Every queued node still gets its callback, the first error is the one reported. qcow2 is the only implementation and does not fail yet.
Signed-off-by Denis V. Lunev <[email protected]>CC Kevin Wolf <[email protected]>CC Hanna Reitz <[email protected]>CC Andrey Drobyshev <[email protected]>--- block.c | 18 +++++++++++++++--- block/qcow2.c | 4 +++- include/block/block_int-common.h | 5 +++-- 3 files changed, 21 insertions(+), 6 deletions(-)block.c +15
-3@@ -4653,15 +4653,27 @@ int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp)tran_commit (tran);bdrv_graph_wrunlock (); + ret =0 ; QTAILQ_FOREACH_REVERSE(bs_entry, bs_queue, entry) {BlockDriverState *bs = bs_entry->state.bs; +Error *local_err =NULL ; +int commit_ret; -if (bs->drv->bdrv_reopen_commit_post) { - bs->drv->bdrv_reopen_commit_post (&bs_entry->state); +if (!bs->drv->bdrv_reopen_commit_post) { +continue ; + } + + commit_ret = bs->drv->bdrv_reopen_commit_post (&bs_entry->state, + &local_err); +if (commit_ret <0 && ret ==0 ) { +/* Committed already, so report the first failure and go on */ +error_propagate (errp, local_err); + ret = commit_ret; + }else { +error_free (local_err); } } - ret =0 ; goto cleanup; abort:block/qcow2.c +3
-1@@ -2145,7 +2145,7 @@ static void qcow2_reopen_commit(BDRVReopenState *state)g_free (state->opaque); } -static void qcow2_reopen_commit_post (BDRVReopenState *state) +static int qcow2_reopen_commit_post (BDRVReopenState *state,Error **errp) {GRAPH_RDLOCK_GUARD_MAINLOOP ();@@ -2163,6 +2163,8 @@ static void qcow2_reopen_commit_post(BDRVReopenState *state) bdrv_get_node_name(state->bs)); } } + +return 0 ; }static void qcow2_reopen_abort (BDRVReopenState *state)include/block/block_int-common.h +0
-0@@ -239,8 +239,9 @@ struct BlockDriver { BDRVReopenState *reopen_state, BlockReopenQueue *queue, Error **errp);
include/block/block_int-common.h struct BlockDriver { · line 247
Quoted 10 lines
include/block/block_int-common.h +3
-2void GRAPH_UNLOCKED_PTR (*bdrv_reopen_commit)(BDRVReopenState *reopen_state); - void GRAPH_UNLOCKED_PTR (*bdrv_reopen_commit_post)( -BDRVReopenState *reopen_state); +/* Cannot fail the reopen, an error only reports an unusable node */ + int GRAPH_UNLOCKED_PTR (*bdrv_reopen_commit_post)( +BDRVReopenState *reopen_state,Error **errp); void GRAPH_UNLOCKED_PTR (*bdrv_reopen_abort)(BDRVReopenState *reopen_state);void (*bdrv_join_options)(QDict *options,QDict *old_options);
The overall commit logic is fine, but it changes the reopen logic. Failures up to and including permissions refresh abort the transaction, but commit_post failure (which can now potentially happen) takes place after actual reopen. So now we can end up in a situation where node is reopened, but unusable. I think this should be better documented.
Andrey
Re: [PATCH v2 3/4] block: remember the writability a reopen starts from
In reply to Denis V. LunevQuoted 35 lines · Denis V. Lunev
bdrv_reopen_commit()updatesbs->open_flags, so by the time .bdrv_reopen_commit_post() runs a driver can no longer tell whether the node has just become writable or was writable all along. Only the transition is worth reacting to.Record the value while the queue is built, next to the other pre-reopen state BDRVReopenState already keeps.
Signed-off-by Denis V. Lunev <[email protected]>CC Kevin Wolf <[email protected]>CC Hanna Reitz <[email protected]>CC Andrey Drobyshev <[email protected]>--- block.c | 1 + include/block/block-common.h | 1 + 2 files changed, 2 insertions(+)block.c +1
-0@@ -4473,6 +4473,7 @@ bdrv_reopen_queue_child(BlockReopenQueue *bs_queue, BlockDriverState *bs, bs_entry->state.options = options; bs_entry->state.explicit_options = explicit_options; bs_entry->state.flags = flags; + bs_entry->state.was_writable =bdrv_is_writable (bs);/* * If keep_old_opts is false then it means that unspecified include/block/block-common.h +0
-0
include/block/block-common.h typedef struct BDRVReopenState { · line 366
Quoted 7 lines
include/block/block-common.h +1
-0@@ -360,6 +360,7 @@ typedef struct BDRVReopenState {int flags;BlockdevDetectZeroesOptions detect_zeroes;bool backing_missing; +bool was_writable;/* bs->open_flags is updated on commit */ BlockDriverState *old_backing_bs;/* keep pointer for permissions update */ BlockDriverState *old_file_bs;/* keep pointer for permissions update */ QDict *options;
If we start tracking pre-reopen flags state, wouldn't it be better to just save 'int old_flags'? Otherwise, once we have an urge to track smth else besides writeability - we'll need to waste another struct field.
Andrey
Re: [PATCH v2 4/4] qcow2: repair a dirty image when it becomes writable
In reply to Denis V. LunevQuoted 44 lines · Denis V. Lunev
A dirty image must be repaired before anything allocates a cluster in it.
qcow2_do_open()does that, but only for a node that is writable from the start. A node opened read-only skips it, and nothing revisits the question once that node becomes writable, which block-commit does
routinely:commit_active_start()andcommit_start()reopen the base read-write for the duration of the job.With lazy refcounts the on-disk refcount block then still accounts for the metadata clusters only, so the allocator restarts at the front of the image and hands out clusters that L2 entries point at. Two guest offsets end up sharing one host cluster. Nothing fails, the corrupt bit stays clear, and a clean close clears the dirty bit, so no later open repairs the image either.
Do the repair in
qcow2_reopen_commit_post().bdrv_reopen_prepare()runs beforebdrv_list_refresh_perms(), so it holds noBLK_PERM_WRITEand, with auto-read-only,bs->filemay still have anO_RDONLYdescriptor.commit_postcannot reject the reopen, so signal corruption if the repair fails rather than let writes alias live clusters. That dropsbs->drv, so return the error and skip the bitmaps. An inactive node is
skipped:bdrv_activate()callsqcow2_do_open()again throughqcow2_co_invalidate_cache().With lazy refcounts the dirty bit stays set for the whole writable session, so a node that is merely writable says nothing. Repair only when it has just become writable, which BDRVReopenState remembers:
a read-write to read-write reopen must not scan a live image.Signed-off-by Denis V. Lunev <[email protected]>CC Kevin Wolf <[email protected]>CC Hanna Reitz <[email protected]>CC Andrey Drobyshev <[email protected]>--- block/qcow2.c | 17 +++++++++++ tests/qemu-iotests/039 | 60 ++++++++++++++++++++++++++++++++++++++ tests/qemu-iotests/039.out | 36 +++++++++++++++++++++++ 3 files changed, 113 insertions(+)block/qcow2.c +0
-0
block/qcow2.c static void qcow2_reopen_commit(BDRVReopenState *state) · line 2155
Quoted 9 lines
block/qcow2.c +4
-0@@ -2147,8 +2147,25 @@ static void qcow2_reopen_commit(BDRVReopenState *state)static int qcow2_reopen_commit_post (BDRVReopenState *state,Error **errp) { +BDRVQcow2State *s = state->bs->opaque; +GRAPH_RDLOCK_GUARD_MAINLOOP (); +if (!state->was_writable &&bdrv_is_writable (state->bs) && + (s->incompatible_features &QCOW2_INCOMPAT_DIRTY )) {
Nit: qcow2_do_open() also guards this with !(flags & BDRV_O_CHECK). AFAICT we can't reach it here since the only place we open with BDRV_O_CHECK is qemu-img check, and it never does reopen. Still, might consider adding the guard.
block/qcow2.c static void qcow2_reopen_commit(BDRVReopenState *state) · line 2164
Quoted 9 lines
block/qcow2.c +9
-0+BdrvCheckResult result = {0 }; +int ret; + + ret =bdrv_check (state->bs, &result,BDRV_FIX_ERRORS |BDRV_FIX_LEAKS ); +if (ret <0 || result.check_errors) { +/* This clears bs->drv, there is nothing left to reopen */ +qcow2_signal_corruption (state->bs, true,-1 ,-1 , +"Could not repair dirty image" ); +error_setg (errp,"Could not repair dirty image" );
Which image? blockdev-reopen can process multiple nodes. And we have no clue which one failed. How about adding bdrv_get_device_or_node_name(state->bs)?
block/qcow2.c static void qcow2_reopen_commit(BDRVReopenState *state) · line 2165
Quoted 1 line
block/qcow2.c +1
-0+return -EIO ;
Path qcow2_do_open() -> qcow2_co_check_locked() does preserve the check's error: ret < 0 ? ret : -EIO; error_setg_errno(-ret). Here a -ENOMEM from the check comes back as -EIO.
block/qcow2.c static void qcow2_reopen_commit(BDRVReopenState *state) · line 2168
Quoted 3 lines
block/qcow2.c +3
-0+ } + } +
There's also a logical issue with this check. qcow2_signal_corruption() does 'bs->drv = NULL', which explicitly makes the node unusable. Now, block commit does reopen the base node to RW, remembering its previous state. If reopen fails, it does rollback reopen to RO. And on this 2nd reopen we'll likely end up asserting on:
bdrv_reopen_set_read_only()
bdrv_reopen()
bdrv_reopen_multiple()
bdrv_reopen_prepare()
assert(reopen_state->bs->drv != NULL)on this unusable node.
Can we afford making bdrv_reopen_prepare() return an error instead of crashing? Or maybe guard this rollback in commit_start() by !bdrv_is_inserted(base). IMO this is either a separate commit, or part of 2nd commit.
And we should probably add a qmp_block_commit test which would cover exactly this problematic case.
Also the QAPI docs for blockdev-reopen say:
Quoted 1 line
... if one of them fails then the whole transaction is cancelled.
That no longer appears to be true. Probably should amend the docs as well.
Andrey
Quoted 121 lines
if (state->flags & BDRV_O_RDWR) { Error *local_err = NULL;tests/qemu-iotests/039 +60
-0@@ -136,6 +136,66 @@ $QEMU_IO -c "write 0 512" "$TEST_IMG" | _filter_qemu_io # The dirty bit must not be set _qcow2_dump_header | grep incompatible_features +echo +echo "== Reopening a dirty image read/write should repair it ==" + +_make_test_img -o "compat=1.1,lazy_refcounts=on" $size + +_NO_VALGRIND \ +$QEMU_IO -c "write -P 0x5a 0 512" \ + -c "sigraise $(kill -l KILL)" "$TEST_IMG" 2>&1 \ + | _filter_qemu_io + +# The dirty bit must be set +_qcow2_dump_header | grep incompatible_features + +# Without the repair this write would alias the cluster at offset 0 +$QEMU_IO -r -c "reopen -w" \ + -c "write -P 0xb1 1M 512" \ + -c "read -P 0x5a 0 512" "$TEST_IMG" | _filter_qemu_io + +# The dirty bit must not be set +_qcow2_dump_header | grep incompatible_features +_check_test_img + +echo +echo "== A read/write reopen must not check the image ==" + +_make_test_img -o "compat=1.1,lazy_refcounts=on" $size + +_NO_VALGRIND \ +$QEMU_IO -c "write -P 0x5a 0 512" \ + -c "reopen -o l2-cache-size=1M" \ + -c "sigraise $(kill -l KILL)" "$TEST_IMG" 2>&1 \ + | _filter_qemu_io + +# The dirty bit must still be set, it belongs to the running session +_qcow2_dump_header | grep incompatible_features + +echo +echo "== A failed repair must fail the reopen ==" + +_make_test_img -o "compat=1.1,lazy_refcounts=on" $size + +_NO_VALGRIND \ +$QEMU_IO -c "write -P 0x5a 0 512" \ + -c "sigraise $(kill -l KILL)" "$TEST_IMG" 2>&1 \ + | _filter_qemu_io + +cat > "$TEST_DIR/blkdebug.conf" <<EOF +[inject-error] +event = "none" +iotype = "write" +errno = "5" +EOF + +# The repair cannot write, so the reopen itself must report the failure +$QEMU_IO -r -c "reopen -w" -c "read -P 0x5a 0 512" \ + "blkdebug:$TEST_DIR/blkdebug.conf:$TEST_IMG" 2>&1 \ + | _filter_testdir | _filter_qemu_io + +rm -f "$TEST_DIR/blkdebug.conf" + echo echo "== Creating an image file with lazy_refcounts=off =="tests/qemu-iotests/039.out +36
-0@@ -64,6 +64,42 @@ wrote 512/512 bytes at offset 0 512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) incompatible_features [] +== Reopening a dirty image read/write should repair it == +Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=134217728 +wrote 512/512 bytes at offset 0 +512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +./common.rc: Killed ( VALGRIND_QEMU="${VALGRIND_QEMU_IO}" _qemu_proc_exec "${VALGRIND_LOGFILE}" "$QEMU_IO_PROG" $QEMU_IO_ARGS "$@" ) +incompatible_features [0] +ERROR cluster 5 refcount=0 reference=1 +Rebuilding refcount structure +Repairing cluster 1 refcount=1 reference=0 +Repairing cluster 2 refcount=1 reference=0 +wrote 512/512 bytes at offset 1048576 +512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +read 512/512 bytes at offset 0 +512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +incompatible_features [] +No errors were found on the image. + +== A read/write reopen must not check the image == +Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=134217728 +wrote 512/512 bytes at offset 0 +512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +./common.rc: Killed ( VALGRIND_QEMU="${VALGRIND_QEMU_IO}" _qemu_proc_exec "${VALGRIND_LOGFILE}" "$QEMU_IO_PROG" $QEMU_IO_ARGS "$@" ) +incompatible_features [0] + +== A failed repair must fail the reopen == +Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=134217728 +wrote 512/512 bytes at offset 0 +512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +./common.rc: Killed ( VALGRIND_QEMU="${VALGRIND_QEMU_IO}" _qemu_proc_exec "${VALGRIND_LOGFILE}" "$QEMU_IO_PROG" $QEMU_IO_ARGS "$@" ) +ERROR cluster 5 refcount=0 reference=1 +Rebuilding refcount structure +qemu-io: ERROR writing refblock: Input/output error +qcow2: Marking image as corrupt: Could not repair dirty image; further corruption events will be suppressed +qemu-io: Could not repair dirty image +read failed: No medium found + == Creating an image file with lazy_refcounts=off == Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=134217728 wrote 512/512 bytes at offset 0