All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kevin Wolf <kwolf@redhat.com>
To: qemu-block@nongnu.org
Cc: kwolf@redhat.com, richard.henderson@linaro.org, qemu-devel@nongnu.org
Subject: [PULL 18/21] qcow2: do not clear the dirty bit when reopening a read-only node
Date: Wed,  7 Oct 2026 18:32:59 +0200	[thread overview]
Message-ID: <20261007163302.332250-19-kwolf@redhat.com> (raw)
In-Reply-To: <20261007163302.332250-1-kwolf@redhat.com>

From: "Denis V. Lunev" <den@openvz.org>

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'

Where the file node below is writable the write is not refused early,
and bdrv_co_write_req_prepare() aborts on its BLK_PERM_WRITE assertion
instead.

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 <den@openvz.org>
Reviewed-by: Andrey Drobyshev <andrey.drobyshev@virtuozzo.com>
CC: Kevin Wolf <kwolf@redhat.com>
CC: Hanna Reitz <hreitz@redhat.com>
CC: Andrey Drobyshev <andrey.drobyshev@virtuozzo.com>
Cc: qemu-stable@nongnu.org
Message-ID: <20260824133729.1141990-2-den@openvz.org>
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 block/qcow2.c              |  8 +++---
 tests/qemu-iotests/039     | 50 ++++++++++++++++++++++++++++++++++++++
 tests/qemu-iotests/039.out | 20 +++++++++++++++
 3 files changed, 75 insertions(+), 3 deletions(-)

diff --git a/block/qcow2.c b/block/qcow2.c
index 7292dd036c9..1543255eba8 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -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;
+            }
         }
     }
 
diff --git a/tests/qemu-iotests/039 b/tests/qemu-iotests/039
index 8fe677ce731..686c6da50fd 100755
--- a/tests/qemu-iotests/039
+++ b/tests/qemu-iotests/039
@@ -93,6 +93,40 @@ $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 "== Read-only reopen must not write through a writable file node =="
+
+# The write the header update needs is refused by the permission system
+echo "{'execute': 'qmp_capabilities'}
+      {'execute': 'blockdev-reopen',
+       'arguments': {'options': [{'node-name': 'drive',
+                                  'driver': 'qcow2',
+                                  'read-only': true,
+                                  'file': 'prot'}]}}
+      {'execute': 'quit'}" \
+    | $QEMU -qmp stdio -nographic -nodefaults \
+            -blockdev "{'node-name': 'prot',
+                        'driver': 'file',
+                        'filename': '$TEST_IMG'}" \
+            -blockdev "{'node-name': 'drive',
+                        'driver': 'qcow2',
+                        'file': 'prot',
+                        'read-only': true}" \
+    | _filter_qmp
+
+# The dirty bit must still be set
+_qcow2_dump_header | grep incompatible_features
+
 echo
 echo "== Repairing the image file must succeed =="
 
@@ -106,6 +140,22 @@ echo "== Data should still be accessible after repair =="
 
 $QEMU_IO -c "read -P 0x5a 0 512" "$TEST_IMG" | _filter_qemu_io
 
+echo
+echo "== A read-write to read-only reopen must clear the dirty bit =="
+
+_make_test_img -o "compat=1.1,lazy_refcounts=on" $size
+
+# The kill keeps the close from clearing the bit, so the header shows what
+# the reopen did with it
+_NO_VALGRIND \
+$QEMU_IO -c "write -P 0x5a 0 512" \
+         -c "reopen -r" \
+         -c "sigraise $(kill -l KILL)" "$TEST_IMG" 2>&1 \
+    | _filter_qemu_io
+
+# The dirty bit must not be set
+_qcow2_dump_header | grep incompatible_features
+
 echo
 echo "== Opening a dirty image read/write should repair it =="
 
diff --git a/tests/qemu-iotests/039.out b/tests/qemu-iotests/039.out
index c66361128f6..ce8ee577216 100644
--- a/tests/qemu-iotests/039.out
+++ b/tests/qemu-iotests/039.out
@@ -27,6 +27,19 @@ 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]
+
+== Read-only reopen must not write through a writable file node ==
+QMP_VERSION
+{"return": {}}
+{"return": {}}
+{"timestamp": {"seconds":  TIMESTAMP, "microseconds":  TIMESTAMP}, "event": "SHUTDOWN", "data": {"guest": false, "reason": "host-qmp-quit"}}
+{"return": {}}
+incompatible_features     [0]
+
 == Repairing the image file must succeed ==
 ERROR cluster 5 refcount=0 reference=1
 Rebuilding refcount structure
@@ -45,6 +58,13 @@ incompatible_features     []
 read 512/512 bytes at offset 0
 512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
 
+== A read-write to read-only reopen must clear the dirty bit ==
+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     []
+
 == Opening a dirty image read/write should repair it ==
 Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=134217728
 wrote 512/512 bytes at offset 0
-- 
2.56.0



  parent reply	other threads:[~2026-10-07 16:36 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-07 16:32 [PULL 00/21] Block layer patches Kevin Wolf
2026-10-07 16:32 ` [PULL 01/21] block-common: fix BDRV_WANT_PRECISE duplicating BDRV_WANT_OFFSET_VALID Kevin Wolf
2026-10-07 16:32 ` [PULL 02/21] block/throttle-groups: fix crash when enabling I/O limits on a busy disk Kevin Wolf
2026-10-07 16:32 ` [PULL 03/21] block: fix bdrv_next() skipping monitor-owned nodes Kevin Wolf
2026-10-07 16:32 ` [PULL 04/21] block: allocate aligned write buffer for 'truncate -m full' Kevin Wolf
2026-10-07 16:32 ` [PULL 05/21] scripts/qemugdb: coroutine: Fix selected frame leak on error path Kevin Wolf
2026-10-07 16:32 ` [PULL 06/21] scripts/qemugdb: coroutine: Get rid of fallback pattern when dumping backtrace Kevin Wolf
2026-10-07 16:32 ` [PULL 07/21] scripts/qemugdb: coroutine: Only attempt to restore regs in coredump Kevin Wolf
2026-10-07 16:32 ` [PULL 08/21] scripts/qemugdb: coroutine: Don't unwind past the outermost frame Kevin Wolf
2026-10-07 16:32 ` [PULL 09/21] scripts/qemugdb: coroutine: Fix patching pt_regs in the coredump Kevin Wolf
2026-10-07 16:32 ` [PULL 10/21] scripts/qemugdb: coroutine: Fix '--detailed' description Kevin Wolf
2026-10-07 16:32 ` [PULL 11/21] scripts/qemugdb: coroutine: Handle target absence gracefully Kevin Wolf
2026-10-07 16:32 ` [PULL 12/21] scripts/qemugdb: coroutine: Speed up coroutine lookup in a coredump Kevin Wolf
2026-10-07 16:32 ` [PULL 13/21] scripts/qemugdb: coroutine: Don't touch the coredump for a plain backtrace Kevin Wolf
2026-10-07 16:32 ` [PULL 14/21] iotests: remove unused 'owner=' variable assignment Kevin Wolf
2026-10-07 16:32 ` [PULL 15/21] hw/block/fdc: account for read and write requests Kevin Wolf
2026-10-07 16:32 ` [PULL 16/21] iotests: annotate TestRunner.shared_self for mypy 2.4 Kevin Wolf
2026-10-07 16:32 ` [PULL 17/21] file-posix: add statx(STATX_DIOALIGN) support Kevin Wolf
2026-10-07 16:32 ` Kevin Wolf [this message]
2026-10-07 16:33 ` [PULL 19/21] block: reject a reopen of an unusable node instead of crashing Kevin Wolf
2026-10-07 16:33 ` [PULL 20/21] block: Fix corruption from broken RMW atomicity Kevin Wolf
2026-10-07 16:33 ` [PULL 21/21] block: Remove now redundant bdrv_wait_serialising_requests() calls Kevin Wolf
2026-10-09 12:29 ` [PULL 00/21] Block layer patches Richard Henderson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261007163302.332250-19-kwolf@redhat.com \
    --to=kwolf@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=richard.henderson@linaro.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.