Skip to contentSkip to conversation

block/backup-top: fix flags handling

PATCH Superseded Vladimir Sementsov-Ogievskiy 2 participants 3 messages Jan 31, 2020 – Feb 3, 2020 Block layer

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 block/backup-top: fix flags handling

backup-top "supports" write-unchanged, by skipping CBW operation in backup_top_co_pwritev. But it forgets to do the same in backup_top_co_pwrite_zeroes, as well as declare support for BDRV_REQ_WRITE_UNCHANGED.

Fix this, and, while being here, declare also support for flags supported by source child.

---
 block/backup-top.c | 28 ++++++++++++++++++----------
 1 file changed, 18 insertions(+), 10 deletions(-)
block/backup-top.c +18 -10
index 9aed2eb4c0..0e28fecc21 100644
@@ -48,11 +48,17 @@ static coroutine_fn int backup_top_co_preadv(
 }
 
 static coroutine_fn int backup_top_cbw(BlockDriverState *bs, uint64_t offset,
-                                       uint64_t bytes)
+                                       uint64_t bytes, BdrvRequestFlags flags)
 {
     BDRVBackupTopState *s = bs->opaque;
-    uint64_t end = QEMU_ALIGN_UP(offset + bytes, s->bcs->cluster_size);
-    uint64_t off = QEMU_ALIGN_DOWN(offset, s->bcs->cluster_size);
+    uint64_t off, end;
+
+    if (flags & BDRV_REQ_WRITE_UNCHANGED) {
+        return 0;
+    }
+
+    off = QEMU_ALIGN_DOWN(offset, s->bcs->cluster_size);
+    end = QEMU_ALIGN_UP(offset + bytes, s->bcs->cluster_size);
 
     return block_copy(s->bcs, off, end - off, NULL);
 }
@@ -60,7 +66,7 @@ static coroutine_fn int backup_top_cbw(BlockDriverState *bs, uint64_t offset,
 static int coroutine_fn backup_top_co_pdiscard(BlockDriverState *bs,
                                                int64_t offset, int bytes)
 {
-    int ret = backup_top_cbw(bs, offset, bytes);
+    int ret = backup_top_cbw(bs, offset, bytes, 0);
     if (ret < 0) {
         return ret;
     }
@@ -71,7 +77,7 @@ static int coroutine_fn backup_top_co_pdiscard(BlockDriverState *bs,
 static int coroutine_fn backup_top_co_pwrite_zeroes(BlockDriverState *bs,
         int64_t offset, int bytes, BdrvRequestFlags flags)
 {
-    int ret = backup_top_cbw(bs, offset, bytes);
+    int ret = backup_top_cbw(bs, offset, bytes, flags);
     if (ret < 0) {
         return ret;
     }
@@ -84,11 +90,9 @@ static coroutine_fn int backup_top_co_pwritev(BlockDriverState *bs,
                                               uint64_t bytes,
                                               QEMUIOVector *qiov, int flags)
 {
-    if (!(flags & BDRV_REQ_WRITE_UNCHANGED)) {
-        int ret = backup_top_cbw(bs, offset, bytes);
-        if (ret < 0) {
-            return ret;
-        }
+    int ret = backup_top_cbw(bs, offset, bytes, flags);
+    if (ret < 0) {
+        return ret;
     }
 
     return bdrv_co_pwritev(bs->backing, offset, bytes, qiov, flags);
@@ -197,6 +201,10 @@ BlockDriverState *bdrv_backup_top_append(BlockDriverState *source,
 
     top->total_sectors = source->total_sectors;
     state = top->opaque;
+    top->supported_write_flags =
+        BDRV_REQ_WRITE_UNCHANGED | source->supported_write_flags;
+    top->supported_zero_flags =
+        BDRV_REQ_WRITE_UNCHANGED | source->supported_zero_flags;
 
     bdrv_ref(target);
     state->target = bdrv_attach_child(top, target, "target", &child_file, errp);
Signature · 1 line
2.21.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 15 lines · Vladimir Sementsov-Ogievskiy

backup-top "supports" write-unchanged, by skipping CBW operation in backup_top_co_pwritev. But it forgets to do the same in backup_top_co_pwrite_zeroes, as well as declare support for BDRV_REQ_WRITE_UNCHANGED.

Fix this, and, while being here, declare also support for flags supported by source child.

---
  block/backup-top.c | 28 ++++++++++++++++++----------
  1 file changed, 18 insertions(+), 10 deletions(-)

block/backup-top.c BlockDriverState *bdrv_backup_top_append(BlockDriverState *source, · line 207

Quoted 7 lines
block/backup-top.c +4 -0
@@ -197,6 +201,10 @@ BlockDriverState *bdrv_backup_top_append(BlockDriverState *source,
 
      top->total_sectors = source->total_sectors;
      state = top->opaque;
+    top->supported_write_flags =
+        BDRV_REQ_WRITE_UNCHANGED | source->supported_write_flags;
+    top->supported_zero_flags =
+        BDRV_REQ_WRITE_UNCHANGED | source->supported_zero_flags;

Elsewhere, in block/filter-compress.c we do:

    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_NO_FALLBACK) &
            bs->file->bs->supported_zero_flags);

That's slightly more robust (if the block layer adds new BDRV_REQ_ bits, we don't have to revisit filter-compress.c to decide if blindly exposing those bits breaks for some reason, but rahter DO have to amend the line to opt-in to supporting the new bits). Whereas your code does NOT need editing if passing on the new bit is safe, but risks a subtle breakage if we forget to filter out the new bit when passing it on would be unsafe. I tend to lean towards safety and opt-in over blind pass-through that works with the current set of defined bits, but not enough to withhold:

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
#2 lore raw
Quoted 25 lines · Eric Blake
Quoted 15 lines · Vladimir Sementsov-Ogievskiy

backup-top "supports" write-unchanged, by skipping CBW operation in backup_top_co_pwritev. But it forgets to do the same in backup_top_co_pwrite_zeroes, as well as declare support for BDRV_REQ_WRITE_UNCHANGED.

Fix this, and, while being here, declare also support for flags supported by source child.

---
  block/backup-top.c | 28 ++++++++++++++++++----------

  1 file changed, 18 insertions(+), 10 deletions(-)

block/backup-top.c BlockDriverState *bdrv_backup_top_append(BlockDriverState *source, · line 206

Quoted 6 lines
block/backup-top.c +4 -0
@@ -197,6 +201,10 @@ BlockDriverState *bdrv_backup_top_append(BlockDriverState *source,
       top->total_sectors = source->total_sectors;
       state = top->opaque;
+    top->supported_write_flags =
+        BDRV_REQ_WRITE_UNCHANGED | source->supported_write_flags;
+    top->supported_zero_flags =
+        BDRV_REQ_WRITE_UNCHANGED | source->supported_zero_flags;

Elsewhere, in block/filter-compress.c we do:

Quoted 7 lines

    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_NO_FALLBACK) & bs->file->bs->supported_zero_flags);

I've looked at this too, but didn't understand, why we need it. Now with your description, I see that this make sense. I'll resend with same pattern, thanks.

Quoted 4 lines

That's slightly more robust (if the block layer adds new BDRV_REQ_ bits, we don't have to revisit filter-compress.c to decide if blindly exposing those bits breaks for some reason, but rahter DO have to amend the line to opt-in to supporting the new bits).  Whereas your code does NOT need editing if passing on the new bit is safe, but risks a subtle breakage if we forget to filter out the new bit when passing it on would be unsafe.  I tend to lean towards safety and opt-in over blind pass-through that works with the current set of defined bits, but not enough to withhold:

Signature · 2 lines
Best regards,
Vladimir