Skip to contentSkip to conversation

qcow2: add very final sync on QCOW2 image closing

PATCH In review Denis V. Lunev via 2 participants 5 messages Dec 19, 2025 – Dec 29, 2025 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 qcow2: add very final sync on QCOW2 image closing

qcow2_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
index e29810d86a..abbc4e82ba 100644
@@ -3252,7 +3252,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.51.0
#1 lore raw
Quoted 25 lines · Denis V. Lunev

qcow2_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 +0 -0
index e29810d86a..abbc4e82ba 100644

block/qcow2.c int qcow2_update_header(BlockDriverState *bs) · line 3258

Quoted 8 lines
block/qcow2.c +1 -1
@@ -3252,7 +3252,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;
      }

Thanks a lot for Andrey Drobyshev who has attempted to review and test this patch.

In reality the patch is non working and the problem is even
more severe than it appears. BDRV_REQ_FUA processing is broken in mainstream QEMU. Generic write path ends in static int coroutine_fn GRAPH_RDLOCK bdrv_aligned_pwritev(BdrvChild *child, BdrvTrackedRequest *req, int64_t offset, int64_t bytes, int64_t align, QEMUIOVector *qiov, size_t qiov_offset, BdrvRequestFlags flags) { .... if (ret < 0) { /* Do nothing, write notifier decided to fail this request */ } else if (flags & BDRV_REQ_ZERO_WRITE) { bdrv_co_debug_event(bs, BLKDBG_PWRITEV_ZERO); ret = bdrv_co_do_pwrite_zeroes(bs, offset, bytes, flags); } else if (flags & BDRV_REQ_WRITE_COMPRESSED) { ret = bdrv_driver_pwritev_compressed(bs, offset, bytes, qiov, qiov_offset); } else if (bytes <= max_transfer) { bdrv_co_debug_event(bs, BLKDBG_PWRITEV); ret = bdrv_driver_pwritev(bs, offset, bytes, qiov, qiov_offset, flags); } else { bdrv_co_debug_event(bs, BLKDBG_PWRITEV); while (bytes_remaining) { int num = MIN(bytes_remaining, max_transfer); int local_flags = flags; assert(num); if (num < bytes_remaining && (flags & BDRV_REQ_FUA) && !(bs->supported_write_flags & BDRV_REQ_FUA)) { /* If FUA is going to be emulated by flush, we only * need to flush on the last iteration */ local_flags &= ~BDRV_REQ_FUA; } ret = bdrv_driver_pwritev(bs, offset + bytes - bytes_remaining, num, qiov, qiov_offset + bytes - bytes_remaining, local_flags); if (ret < 0) { break; } bytes_remaining -= num; } } bdrv_co_debug_event(bs, BLKDBG_PWRITEV_DONE); if (ret >= 0) { ret = 0; } bdrv_co_write_req_finish(child, offset, bytes, req, ret);

BDRV_REQ_FUA is processed insidebdrv_driver_pwritev() and resulted in bdrv_co_flush() is __NO OP (!)__ until bdrv_co_write_req_finish() is called which performs qatomic_inc(&bs->write_gen); Only next flush will really flush the data. This means that flush semantics is broken inside QEMU and this problem looks serious to me. I'll come up with a patch. Den

#2 lore raw

Re: [PATCH 1/1] qcow2: add very final sync on QCOW2 image closing - BDRV_REQ_FUA processing is broken

In reply to Denis V. Lunev via
Quoted 25 lines · Denis V. Lunev

qcow2_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 +0 -0
index e29810d86a..abbc4e82ba 100644

block/qcow2.c int qcow2_update_header(BlockDriverState *bs) · line 3258

Quoted 8 lines
block/qcow2.c +1 -1
@@ -3252,7 +3252,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;
      }

(attempt #2, mailer has rotten the mail. sorry).

Thanks a lot for Andrey Drobyshev who has attempted to review and test this patch.

The patch is non-working due to broken BDRV_REQ_FUA processing in QEMU code and this looks more severe than the original problem. bdrv_pwrite() endups in bdrv_aligned_pwritev() which internally calls bdrv_driver_pwritev() -> bdrv_co_flush() bdrv_co_write_req_finish() Please note, that bdrv_co_flush() is in reality noop until bs->write_gen is incremented, which happened only late inside bdrv_co_write_req_finish(). This means that BDRV_REQ_FUA request will be flushed only on the NEXT flush operation, which is definitely wrong. Den

#3 lore raw

Re: [PATCH 1/1] qcow2: add very final sync on QCOW2 image closing - BDRV_REQ_FUA processing is broken

Quoted 39 lines · Denis V. Lunev
Quoted 25 lines · Denis V. Lunev

qcow2_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 +0 -0
index e29810d86a..abbc4e82ba 100644

block/qcow2.c int qcow2_update_header(BlockDriverState *bs) · line 3258

Quoted 8 lines
block/qcow2.c +1 -1
@@ -3252,7 +3252,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;
      }

(attempt #2, mailer has rotten the mail. sorry).

Thanks a lot for Andrey Drobyshev who has attempted to review and test this patch.

Quoted 8 lines

The patch is non-working due to broken BDRV_REQ_FUA processing in QEMU code and this looks more severe than the original problem. bdrv_pwrite() endups in bdrv_aligned_pwritev() which internally calls bdrv_driver_pwritev() -> bdrv_co_flush() bdrv_co_write_req_finish() Please note, that bdrv_co_flush() is in reality noop until bs->write_gen is incremented, which happened only late inside bdrv_co_write_req_finish(). This means that BDRV_REQ_FUA request will be flushed only on the NEXT flush operation, which is definitely wrong. Den

(attempt #3, mailer has rotten the mail. sorry. Finally get it working)

Thanks a lot for Andrey Drobyshev who has attempted to review and test this patch.

The patch is non-working due to broken BDRV_REQ_FUA processing in QEMU code and this looks more severe than the original problem.

bdrv_pwrite() endups in bdrv_aligned_pwritev() which internally calls

	bdrv_driver_pwritev() -> bdrv_co_flush()
	bdrv_co_write_req_finish() 

Please note, that bdrv_co_flush() is in reality noop until bs->write_gen is incremented, which happened only late inside bdrv_co_write_req_finish().

This means that BDRV_REQ_FUA request will be flushed only on the NEXT flush operation, which is definitely wrong.

Den

#4 lore raw

Re: [PATCH 1/1] qcow2: add very final sync on QCOW2 image closing - BDRV_REQ_FUA processing is broken

Quoted 59 lines · Denis V. Lunev
Quoted 39 lines · Denis V. Lunev
Quoted 25 lines · Denis V. Lunev

qcow2_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 +0 -0
index e29810d86a..abbc4e82ba 100644

block/qcow2.c int qcow2_update_header(BlockDriverState *bs) · line 3258

Quoted 8 lines
block/qcow2.c +1 -1
@@ -3252,7 +3252,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;
      }

(attempt #2, mailer has rotten the mail. sorry).

Thanks a lot for Andrey Drobyshev who has attempted to review and test this patch.

Quoted 8 lines

The patch is non-working due to broken BDRV_REQ_FUA processing in QEMU code and this looks more severe than the original problem. bdrv_pwrite() endups in bdrv_aligned_pwritev() which internally calls bdrv_driver_pwritev() -> bdrv_co_flush() bdrv_co_write_req_finish() Please note, that bdrv_co_flush() is in reality noop until bs->write_gen is incremented, which happened only late inside bdrv_co_write_req_finish(). This means that BDRV_REQ_FUA request will be flushed only on the NEXT flush operation, which is definitely wrong. Den

(attempt #3, mailer has rotten the mail. sorry. Finally get it working)

Thanks a lot for Andrey Drobyshev who has attempted to review and test this patch.

The patch is non-working due to broken BDRV_REQ_FUA processing in QEMU code and this looks more severe than the original problem.

bdrv_pwrite() endups in bdrv_aligned_pwritev() which internally calls

	bdrv_driver_pwritev() -> bdrv_co_flush()
	bdrv_co_write_req_finish() 
Quoted 8 lines

Please note, that bdrv_co_flush() is in reality noop until bs->write_gen is incremented, which happened only late inside bdrv_co_write_req_finish().

This means that BDRV_REQ_FUA request will be flushed only on the NEXT flush operation, which is definitely wrong.

Den

please disregard this patch. Fixes have been sent.

Den