Skip to contentSkip to conversation

qcow2: cluster space preallocation

PATCH Superseded Anton Nefedov 2 participants 19 messages Mar 12, 2018 – Mar 19, 2018 Block layer

v8 of 14 Superseded Latest is v14 →

lore
Share & tools
lore
#0 lore raw

v8 cover qcow2: cluster space preallocation

v8: - patch 1: flags setup moved to a proper place
- new patch 3 for quorum driver
- patch 4: expanded the flag description and minor relocations of sanity checks
- patch 5: function renamed
- patch 6: minor rebase-related changes
- patch 7: quorum added
- patch 8: use bdrv_is_allocated interface, which allows a less strict zero detection thanks to block_status 'want_zero' flag. Other minor changes (encrypted flag check moved).

v7: http://lists.nongnu.org/archive/html/qemu-devel/2018-01/msg04470.html

Anton Nefedov (9):
mirror: inherit supported write/zero flags
blkverify: set supported write/zero flags
quorum: set supported write/zero flags
block: introduce BDRV_REQ_ALLOCATE flag
block: treat BDRV_REQ_ALLOCATE as serialising
file-posix: support BDRV_REQ_ALLOCATE
block: support BDRV_REQ_ALLOCATE in passthrough drivers
qcow2: skip writing zero buffers to empty COW areas iotest 134: test cluster-misaligned encrypted write

 qapi/block-core.json       |  4 ++-
 block/qcow2.h              |  6 ++++
 include/block/block.h      |  8 ++++-
 include/block/block_int.h  |  2 +-
 block/blkdebug.c           |  3 +-
 block/blkverify.c          |  9 ++++++
 block/file-posix.c         | 10 +++++-
 block/io.c                 | 50 +++++++++++++++++++++++------
 block/mirror.c             | 11 +++++++
 block/qcow2-cluster.c      |  2 +-
 block/qcow2.c              | 79 ++++++++++++++++++++++++++++++++++++++++++++--
 block/quorum.c             | 21 ++++++++++++
 block/raw-format.c         |  3 +-
 block/trace-events         |  1 +
 tests/qemu-iotests/060     | 26 +++++++++------
 tests/qemu-iotests/060.out |  5 ++-
 tests/qemu-iotests/134     |  9 ++++++
 tests/qemu-iotests/134.out | 10 ++++++
 18 files changed, 230 insertions(+), 29 deletions(-)
Signature · 1 line
2.7.4
#1 lore raw

PATCH v8 1/9 mirror: inherit supported write/zero flags

---
 block/mirror.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
block/mirror.c +11 -0
index f5bf620..2fb786f 100644
@@ -1100,6 +1100,15 @@ static BlockDriver bdrv_mirror_top = {
     .bdrv_child_perm            = bdrv_mirror_top_child_perm,
 };
 
+static void mirror_top_set_supported_flags(BlockDriverState *bs)
+{
+    bs->supported_write_flags = BDRV_REQ_FUA &
+        bs->backing->bs->supported_write_flags;
+    bs->supported_zero_flags =
+        (BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP) &
+        bs->backing->bs->supported_zero_flags;
+}
+
 static void mirror_start_job(const char *job_id, BlockDriverState *bs,
                              int creation_flags, BlockDriverState *target,
                              const char *replaces, int64_t speed,
@@ -1165,6 +1174,8 @@ static void mirror_start_job(const char *job_id, BlockDriverState *bs,
         return;
     }
 
+    mirror_top_set_supported_flags(mirror_top_bs);
+
     /* Make sure that the source is not resized while the job is running */
     s = block_job_create(job_id, driver, mirror_top_bs,
                          BLK_PERM_CONSISTENT_READ,
Signature · 1 line
2.7.4
Reviewed-by on v8 1/9
#2 lore raw

Re: [Qemu-devel] [PATCH v8 1/9] mirror: inherit supported write/zero flags

Quoted 12 lines · Anton Nefedov
---
 block/mirror.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
block/mirror.c +0 -0
index f5bf620..2fb786f 100644
@@ -1100,6 +1100,15 @@ static BlockDriver bdrv_mirror_top = {
     .bdrv_child_perm            = bdrv_mirror_top_child_perm,
 };

block/mirror.c static BlockDriver bdrv_mirror_top = { · line 1111

Quoted 10 lines
block/mirror.c +9 -0
 
+static void mirror_top_set_supported_flags(BlockDriverState *bs)
+{
+    bs->supported_write_flags = BDRV_REQ_FUA &
+        bs->backing->bs->supported_write_flags;
+    bs->supported_zero_flags =
+        (BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP) &
+        bs->backing->bs->supported_zero_flags;
+}
+

Berto

#3 lore raw

PATCH v8 2/9 blkverify: set supported write/zero flags

In reply to Anton Nefedov
---
 block/blkverify.c | 9 +++++++++
 1 file changed, 9 insertions(+)
block/blkverify.c +9 -0
index 331365b..de2fdc1 100644
@@ -141,6 +141,15 @@ static int blkverify_open(BlockDriverState *bs, QDict *options, int flags,
         goto fail;
     }
 
+    bs->supported_write_flags = BDRV_REQ_FUA &
+        bs->file->bs->supported_write_flags &
+        s->test_file->bs->supported_write_flags;
+
+    bs->supported_zero_flags =
+        (BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP) &
+        bs->file->bs->supported_zero_flags &
+        s->test_file->bs->supported_zero_flags;
+
     ret = 0;
 fail:
     qemu_opts_del(opts);
Signature · 1 line
2.7.4
#4 lore raw

PATCH v8 3/9 quorum: set supported write/zero flags

In reply to Anton Nefedov
---
 block/quorum.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
block/quorum.c +20 -0
index 14333c1..dc77a23 100644
@@ -867,6 +867,20 @@ static QemuOptsList quorum_runtime_opts = {
     },
 };
 
+static void quorum_set_supported_flags(BlockDriverState *bs)
+{
+    BDRVQuorumState *s = bs->opaque;
+    int i;
+
+    bs->supported_write_flags = BDRV_REQ_FUA;
+    bs->supported_zero_flags  = BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP;
+
+    for (i = 0; i < s->num_children; i++) {
+        bs->supported_write_flags &= s->children[i]->bs->supported_write_flags;
+        bs->supported_zero_flags  &= s->children[i]->bs->supported_zero_flags;
+    }
+}
+
 static int quorum_open(BlockDriverState *bs, QDict *options, int flags,
                        Error **errp)
 {
@@ -961,6 +975,8 @@ static int quorum_open(BlockDriverState *bs, QDict *options, int flags,
     }
     s->next_child_index = s->num_children;
 
+    quorum_set_supported_flags(bs);
+
     g_free(opened);
     goto exit;
 
@@ -1029,6 +1045,8 @@ static void quorum_add_child(BlockDriverState *bs, BlockDriverState *child_bs,
     s->children = g_renew(BdrvChild *, s->children, s->num_children + 1);
     s->children[s->num_children++] = child;
 
+    quorum_set_supported_flags(bs);
+
 out:
     bdrv_drained_end(bs);
 }
@@ -1064,6 +1082,8 @@ static void quorum_del_child(BlockDriverState *bs, BdrvChild *child,
     bdrv_unref_child(bs, child);
 
     bdrv_drained_end(bs);
+
+    quorum_set_supported_flags(bs);
 }
 
 static void quorum_refresh_filename(BlockDriverState *bs, QDict *options)
Signature · 1 line
2.7.4
#6 lore raw

PATCH v8 4/9 block: introduce BDRV_REQ_ALLOCATE flag

In reply to Anton Nefedov

The flag is supposed to indicate that the region of the disk image has to be sufficiently allocated so it reads as zeroes.

The call with the flag set must return -ENOTSUP if allocation cannot be done efficiently.
This has to be made sure of by both
- the drivers that support the flag
- and the common block layer (so it will not fall back to any slowpath (like writing zero buffers) in case the driver does not support the flag).

---
 include/block/block.h     |  8 +++++++-
 include/block/block_int.h |  2 +-
 block/io.c                | 18 ++++++++++++++++--
 3 files changed, 24 insertions(+), 4 deletions(-)
include/block/block.h +7 -1
index 8b6db95..9747632 100644
@@ -53,9 +53,15 @@ typedef enum {
     BDRV_REQ_NO_SERIALISING     = 0x8,
     BDRV_REQ_FUA                = 0x10,
     BDRV_REQ_WRITE_COMPRESSED   = 0x20,
+    /* The BDRV_REQ_ALLOCATE flag is used to indicate that the driver has to
+     * efficiently allocate the space so it reads as zeroes, or return an error.
+     * Must be used together with BDRV_REQ_ZERO_WRITE.
+     * Contradictory to BDRV_REQ_MAY_UNMAP so the two must not be used together.
+     */
+    BDRV_REQ_ALLOCATE           = 0x40,
 
     /* Mask of valid flags */
-    BDRV_REQ_MASK               = 0x3f,
+    BDRV_REQ_MASK               = 0x7f,
 } BdrvRequestFlags;
 
 typedef struct BlockSizes {
include/block/block_int.h +1 -1
index 64a5700..9495e35 100644
@@ -648,7 +648,7 @@ struct BlockDriverState {
     /* Flags honored during pwrite (so far: BDRV_REQ_FUA) */
     unsigned int supported_write_flags;
     /* Flags honored during pwrite_zeroes (so far: BDRV_REQ_FUA,
-     * BDRV_REQ_MAY_UNMAP) */
+     * BDRV_REQ_MAY_UNMAP, BDRV_REQ_ALLOCATE) */
     unsigned int supported_zero_flags;
 
     /* the following member gives a name to every node on the bs graph. */
block/io.c +16 -2
index 2b09c65..c4f2a07 100644
@@ -1418,7 +1418,7 @@ static int coroutine_fn bdrv_co_do_pwrite_zeroes(BlockDriverState *bs,
             assert(!bs->supported_zero_flags);
         }
 
-        if (ret == -ENOTSUP) {
+        if (ret == -ENOTSUP && !(flags & BDRV_REQ_ALLOCATE)) {
             /* Fall back to bounce buffer if write zeroes is unsupported */
             BdrvRequestFlags write_flags = flags & ~BDRV_REQ_ZERO_WRITE;
 
@@ -1511,7 +1511,9 @@ static int coroutine_fn bdrv_aligned_pwritev(BdrvChild *child,
         !(flags & BDRV_REQ_ZERO_WRITE) && drv->bdrv_co_pwrite_zeroes &&
         qemu_iovec_is_zero(qiov)) {
         flags |= BDRV_REQ_ZERO_WRITE;
-        if (bs->detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP) {
+        if (bs->detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
+            !(flags & BDRV_REQ_ALLOCATE))
+        {
             flags |= BDRV_REQ_MAY_UNMAP;
         }
     }
@@ -1587,6 +1589,9 @@ static int coroutine_fn bdrv_co_do_zero_pwritev(BdrvChild *child,
 
     assert(flags & BDRV_REQ_ZERO_WRITE);
     if (head_padding_bytes || tail_padding_bytes) {
+        if (flags & BDRV_REQ_ALLOCATE) {
+            return -ENOTSUP;
+        }
         buf = qemu_blockalign(bs, align);
         iov = (struct iovec) {
             .iov_base   = buf,
@@ -1672,6 +1677,9 @@ int coroutine_fn bdrv_co_pwritev(BdrvChild *child,
     bool use_local_qiov = false;
     int ret;
 
+    assert(!((flags & BDRV_REQ_ALLOCATE) && (flags & BDRV_REQ_MAY_UNMAP)));
+    assert(!((flags & BDRV_REQ_ALLOCATE) && !(flags & BDRV_REQ_ZERO_WRITE)));
+
     trace_bdrv_co_pwritev(child->bs, offset, bytes, flags);
 
     if (!bs->drv) {
@@ -1816,6 +1824,12 @@ int coroutine_fn bdrv_co_pwrite_zeroes(BdrvChild *child, int64_t offset,
 {
     trace_bdrv_co_pwrite_zeroes(child->bs, offset, bytes, flags);
 
+    if ((flags & BDRV_REQ_ALLOCATE) &&
+        !(child->bs->supported_zero_flags & BDRV_REQ_ALLOCATE))
+    {
+        return -ENOTSUP;
+    }
+
     if (!(child->bs->open_flags & BDRV_O_UNMAP)) {
         flags &= ~BDRV_REQ_MAY_UNMAP;
     }
Signature · 1 line
2.7.4
#7 lore raw

Re: [Qemu-devel] [PATCH v8 4/9] block: introduce BDRV_REQ_ALLOCATE flag

Quoted 11 lines · Anton Nefedov

The flag is supposed to indicate that the region of the disk image has to be sufficiently allocated so it reads as zeroes.

The call with the flag set must return -ENOTSUP if allocation cannot be done efficiently.
This has to be made sure of by both
- the drivers that support the flag
- and the common block layer (so it will not fall back to any slowpath (like writing zero buffers) in case the driver does not support the flag).

Quoted 3 lines

Hmm... this is not the patch that I reviewed, please remove the R-b lines when you submit a modified patch.

include/block/block.h typedef enum { · line 58

Quoted 3 lines
include/block/block.h +3 -0
+    /* The BDRV_REQ_ALLOCATE flag is used to indicate that the driver has to
+     * efficiently allocate the space so it reads as zeroes, or return an error.
+     * Must be used together with BDRV_REQ_ZERO_WRITE.

I guess it's understandable from the context but it's not immediately obvious that you can set BDRV_REQ_ZERO_WRITE but not this one.

Perhaps it could be rephrased as something like "If this is set then BDRV_REQ_ZERO_WRITE must also be set".

Quoted 1 line

+ * Contradictory to BDRV_REQ_MAY_UNMAP so the two must not be used together.

Or simply "This flag cannot be set together with BDRV_REQ_MAY_UNMAP".

include/block/block.h typedef enum { · line 61

Quoted 2 lines
include/block/block.h +2 -0
+     */
+    BDRV_REQ_ALLOCATE           = 0x40,

Berto

#8 lore raw

Re: [Qemu-devel] [PATCH v8 4/9] block: introduce BDRV_REQ_ALLOCATE flag

Quoted 12 lines · Alberto Garcia
Quoted 11 lines · Anton Nefedov

The flag is supposed to indicate that the region of the disk image has to be sufficiently allocated so it reads as zeroes.

The call with the flag set must return -ENOTSUP if allocation cannot be done efficiently.
This has to be made sure of by both
- the drivers that support the flag
- and the common block layer (so it will not fall back to any slowpath (like writing zero buffers) in case the driver does not support the flag).

Quoted 7 lines
Quoted 3 lines

Hmm... this is not the patch that I reviewed, please remove the R-b lines when you submit a modified patch.

Oops. sorry

Quoted 10 lines

include/block/block.h typedef enum { · line 58

Quoted 3 lines
include/block/block.h +3 -0
+    /* The BDRV_REQ_ALLOCATE flag is used to indicate that the driver has to
+     * efficiently allocate the space so it reads as zeroes, or return an error.
+     * Must be used together with BDRV_REQ_ZERO_WRITE.

I guess it's understandable from the context but it's not immediately obvious that you can set BDRV_REQ_ZERO_WRITE but not this one.

Perhaps it could be rephrased as something like "If this is set then BDRV_REQ_ZERO_WRITE must also be set".

Sound much better, fixed

Quoted 4 lines
Quoted 1 line

+ * Contradictory to BDRV_REQ_MAY_UNMAP so the two must not be used together.

Or simply "This flag cannot be set together with BDRV_REQ_MAY_UNMAP".

Done

Quoted 5 lines

include/block/block.h typedef enum { · line 61

Quoted 2 lines
include/block/block.h +2 -0
+     */
+    BDRV_REQ_ALLOCATE           = 0x40,

Berto

#9 lore raw

PATCH v8 5/9 block: treat BDRV_REQ_ALLOCATE as serialising

In reply to Anton Nefedov

The idea is that ALLOCATE requests may overlap with other requests. Reuse the existing block layer infrastructure for serialising requests. Use the following approach:
- mark ALLOCATE serialising, so subsequent requests to the area wait
- ALLOCATE request itself must never wait if another request is in flight already. Return EAGAIN, let the caller reconsider.

---
 block/io.c | 32 +++++++++++++++++++++++++-------
 1 file changed, 25 insertions(+), 7 deletions(-)
block/io.c +25 -7
index c4f2a07..5d74603 100644
@@ -599,12 +599,13 @@ void bdrv_dec_in_flight(BlockDriverState *bs)
     bdrv_wakeup(bs);
 }
 
-static bool coroutine_fn wait_serialising_requests(BdrvTrackedRequest *self)
+static bool coroutine_fn find_or_wait_serialising_requests(
+    BdrvTrackedRequest *self, bool wait)
 {
     BlockDriverState *bs = self->bs;
     BdrvTrackedRequest *req;
     bool retry;
-    bool waited = false;
+    bool found = false;
 
     if (!atomic_read(&bs->serialising_in_flight)) {
         return false;
@@ -630,11 +631,14 @@ static bool coroutine_fn wait_serialising_requests(BdrvTrackedRequest *self)
                  * will wait for us as soon as it wakes up, then just go on
                  * (instead of producing a deadlock in the former case). */
                 if (!req->waiting_for) {
+                    found = true;
+                    if (!wait) {
+                        break;
+                    }
                     self->waiting_for = req;
                     qemu_co_queue_wait(&req->wait_queue, &bs->reqs_lock);
                     self->waiting_for = NULL;
                     retry = true;
-                    waited = true;
                     break;
                 }
             }
@@ -642,7 +646,12 @@ static bool coroutine_fn wait_serialising_requests(BdrvTrackedRequest *self)
         qemu_co_mutex_unlock(&bs->reqs_lock);
     } while (retry);
 
-    return waited;
+    return found;
+}
+
+static bool coroutine_fn wait_serialising_requests(BdrvTrackedRequest *self)
+{
+    return find_or_wait_serialising_requests(self, true);
 }
 
 static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
@@ -1474,7 +1483,7 @@ static int coroutine_fn bdrv_aligned_pwritev(BdrvChild *child,
 {
     BlockDriverState *bs = child->bs;
     BlockDriver *drv = bs->drv;
-    bool waited;
+    bool found;
     int ret;
 
     int64_t end_sector = DIV_ROUND_UP(offset + bytes, BDRV_SECTOR_SIZE);
@@ -1498,8 +1507,13 @@ static int coroutine_fn bdrv_aligned_pwritev(BdrvChild *child,
     max_transfer = QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs->bl.max_transfer, INT_MAX),
                                    align);
 
-    waited = wait_serialising_requests(req);
-    assert(!waited || !req->serialising);
+    found = find_or_wait_serialising_requests(req,
+                                              !(flags & BDRV_REQ_ALLOCATE));
+    if (found && (flags & BDRV_REQ_ALLOCATE)) {
+        return -EAGAIN;
+    }
+
+    assert(!found || !req->serialising);
     assert(req->overlap_offset <= offset);
     assert(offset + bytes <= req->overlap_offset + req->overlap_bytes);
     assert(child->perm & BLK_PERM_WRITE);
@@ -1624,6 +1638,10 @@ static int coroutine_fn bdrv_co_do_zero_pwritev(BdrvChild *child,
         bytes -= zero_bytes;
     }
 
+    if (flags & BDRV_REQ_ALLOCATE) {
+        mark_request_serialising(req, align);
+    }
+
     assert(!bytes || (offset & (align - 1)) == 0);
     if (bytes >= align) {
         /* Write the aligned part in the middle. */
Signature · 1 line
2.7.4
Reviewed-by on v8 5/9
#10 lore raw

Re: [Qemu-devel] [PATCH v8 5/9] block: treat BDRV_REQ_ALLOCATE as serialising

Quoted 8 lines · Anton Nefedov

The idea is that ALLOCATE requests may overlap with other requests. Reuse the existing block layer infrastructure for serialising requests. Use the following approach:
- mark ALLOCATE serialising, so subsequent requests to the area wait
- ALLOCATE request itself must never wait if another request is in flight already. Return EAGAIN, let the caller reconsider.

block/io.c static int coroutine_fn bdrv_aligned_pwritev(BdrvChild *child, · line 1515

Quoted 12 lines
block/io.c +6 -2
@@ -1498,8 +1507,13 @@ static int coroutine_fn bdrv_aligned_pwritev(BdrvChild *child,
     max_transfer = QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs->bl.max_transfer, INT_MAX),
                                    align);
 
-    waited = wait_serialising_requests(req);
-    assert(!waited || !req->serialising);
+    found = find_or_wait_serialising_requests(req,
+                                              !(flags & BDRV_REQ_ALLOCATE));
+    if (found && (flags & BDRV_REQ_ALLOCATE)) {
+        return -EAGAIN;
+    }
+

Another alternative (perhaps a bit more readable):

    if (flags & BDRV_REQ_ALLOCATE) {
        if (find_or_wait_serialising_requests(req, false)) {
            return -EAGAIN;
        }
    } else {
        bool found = wait_serialising_requests(req);
        assert(!found || !req->serialising);
    }

but yours is fine, so keep it if you prefer.

Berto

#11 lore raw

PATCH v8 6/9 file-posix: support BDRV_REQ_ALLOCATE

In reply to Anton Nefedov

Current write_zeroes implementation is good enough to satisfy this flag too

---
 block/file-posix.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
block/file-posix.c +9 -1
index 7f2cc63..2136df9 100644
@@ -560,7 +560,10 @@ static int raw_open_common(BlockDriverState *bs, QDict *options,
     }
     if (S_ISREG(st.st_mode)) {
         s->discard_zeroes = true;
+#ifdef CONFIG_FALLOCATE
         s->has_fallocate = true;
+        bs->supported_zero_flags |= BDRV_REQ_ALLOCATE;
+#endif
     }
     if (S_ISBLK(st.st_mode)) {
 #ifdef BLKDISCARDZEROES
@@ -595,10 +598,11 @@ static int raw_open_common(BlockDriverState *bs, QDict *options,
 #ifdef CONFIG_XFS
     if (platform_test_xfs_fd(s->fd)) {
         s->is_xfs = true;
+        bs->supported_zero_flags |= BDRV_REQ_ALLOCATE;
     }
 #endif
 
-    bs->supported_zero_flags = s->discard_zeroes ? BDRV_REQ_MAY_UNMAP : 0;
+    bs->supported_zero_flags |= s->discard_zeroes ? BDRV_REQ_MAY_UNMAP : 0;
     ret = 0;
 fail:
     if (filename && (bdrv_flags & BDRV_O_TEMPORARY)) {
@@ -1416,6 +1420,10 @@ static ssize_t handle_aiocb_write_zeroes(RawPosixAIOData *aiocb)
         }
         s->has_fallocate = false;
     }
+
+    if (!s->has_fallocate) {
+        aiocb->bs->supported_zero_flags &= ~BDRV_REQ_ALLOCATE;
+    }
 #endif
 
     return -ENOTSUP;
Signature · 1 line
2.7.4
#12 lore raw

Re: [Qemu-devel] [PATCH v8 6/9] file-posix: support BDRV_REQ_ALLOCATE

Quoted 5 lines · Anton Nefedov

Current write_zeroes implementation is good enough to satisfy this flag too

strictly speaking this patch differs from v8 as well (rebase required an extra change, see below) so perhaps I should have stripped the review tags too

Quoted 20 lines
---
  block/file-posix.c | 10 +++++++++-
  1 file changed, 9 insertions(+), 1 deletion(-)
block/file-posix.c +3 -0
index 7f2cc63..2136df9 100644
@@ -560,7 +560,10 @@ static int raw_open_common(BlockDriverState *bs, QDict *options,
      }
      if (S_ISREG(st.st_mode)) {
          s->discard_zeroes = true;
+#ifdef CONFIG_FALLOCATE
          s->has_fallocate = true;
+        bs->supported_zero_flags |= BDRV_REQ_ALLOCATE;
+#endif
      }
      if (S_ISBLK(st.st_mode)) {
  #ifdef BLKDISCARDZEROES

block/file-posix.c static int raw_open_common(BlockDriverState *bs, QDict *options, · line 606

Quoted 10 lines
block/file-posix.c +2 -1
@@ -595,10 +598,11 @@ static int raw_open_common(BlockDriverState *bs, QDict *options,
  #ifdef CONFIG_XFS
      if (platform_test_xfs_fd(s->fd)) {
          s->is_xfs = true;
+        bs->supported_zero_flags |= BDRV_REQ_ALLOCATE;
      }
  #endif
 
-    bs->supported_zero_flags = s->discard_zeroes ? BDRV_REQ_MAY_UNMAP : 0;
+    bs->supported_zero_flags |= s->discard_zeroes ? BDRV_REQ_MAY_UNMAP : 0;
      ret = 0;

(this change is new in v9)

Quoted 14 lines
block/file-posix.c +4 -0
  fail:
      if (filename && (bdrv_flags & BDRV_O_TEMPORARY)) {
@@ -1416,6 +1420,10 @@ static ssize_t handle_aiocb_write_zeroes(RawPosixAIOData *aiocb)
          }
          s->has_fallocate = false;
      }
+
+    if (!s->has_fallocate) {
+        aiocb->bs->supported_zero_flags &= ~BDRV_REQ_ALLOCATE;
+    }
  #endif
 
      return -ENOTSUP;
#13 lore raw

PATCH v8 7/9 block: support BDRV_REQ_ALLOCATE in passthrough drivers

In reply to Anton Nefedov

Support the flag if the underlying BDS supports it

---
 block/blkdebug.c   | 3 ++-
 block/blkverify.c  | 2 +-
 block/mirror.c     | 2 +-
 block/quorum.c     | 3 ++-
 block/raw-format.c | 3 ++-
 5 files changed, 8 insertions(+), 5 deletions(-)
block/blkdebug.c +2 -1
index 5897124..d46f6c9 100644
@@ -400,7 +400,8 @@ static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags,
 
     bs->supported_write_flags = BDRV_REQ_FUA &
         bs->file->bs->supported_write_flags;
-    bs->supported_zero_flags = (BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP) &
+    bs->supported_zero_flags =
+        (BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP | BDRV_REQ_ALLOCATE) &
         bs->file->bs->supported_zero_flags;
     ret = -EINVAL;
 
block/blkverify.c +1 -1
index de2fdc1..03bec62 100644
@@ -146,7 +146,7 @@ static int blkverify_open(BlockDriverState *bs, QDict *options, int flags,
         s->test_file->bs->supported_write_flags;
 
     bs->supported_zero_flags =
-        (BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP) &
+        (BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP | BDRV_REQ_ALLOCATE) &
         bs->file->bs->supported_zero_flags &
         s->test_file->bs->supported_zero_flags;
 
block/mirror.c +1 -1
index 2fb786f..58f006f 100644
@@ -1105,7 +1105,7 @@ static void mirror_top_set_supported_flags(BlockDriverState *bs)
     bs->supported_write_flags = BDRV_REQ_FUA &
         bs->backing->bs->supported_write_flags;
     bs->supported_zero_flags =
-        (BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP) &
+        (BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP | BDRV_REQ_ALLOCATE) &
         bs->backing->bs->supported_zero_flags;
 }
 
block/quorum.c +2 -1
index dc77a23..e369e79 100644
@@ -873,7 +873,8 @@ static void quorum_set_supported_flags(BlockDriverState *bs)
     int i;
 
     bs->supported_write_flags = BDRV_REQ_FUA;
-    bs->supported_zero_flags  = BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP;
+    bs->supported_zero_flags  =
+        BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP | BDRV_REQ_ALLOCATE;
 
     for (i = 0; i < s->num_children; i++) {
         bs->supported_write_flags &= s->children[i]->bs->supported_write_flags;
block/raw-format.c +2 -1
index a378547..ca65f86 100644
@@ -417,7 +417,8 @@ static int raw_open(BlockDriverState *bs, QDict *options, int flags,
     bs->sg = bs->file->bs->sg;
     bs->supported_write_flags = BDRV_REQ_FUA &
         bs->file->bs->supported_write_flags;
-    bs->supported_zero_flags = (BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP) &
+    bs->supported_zero_flags =
+        (BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP | BDRV_REQ_ALLOCATE) &
         bs->file->bs->supported_zero_flags;
 
     if (bs->probed && !bdrv_is_read_only(bs)) {
Signature · 1 line
2.7.4
#14 lore raw

Re: [Qemu-devel] [PATCH v8 7/9] block: support BDRV_REQ_ALLOCATE in passthrough drivers

Quoted 4 lines · Anton Nefedov

Support the flag if the underlying BDS supports it

This is again not the patch that I reviewed (the quorum bits were not there), but I just check the changes and they look fine so you can keep my Reviewed-by line.

Berto

#15 lore raw

PATCH v8 8/9 qcow2: skip writing zero buffers to empty COW areas

In reply to Anton Nefedov

If COW areas of the newly allocated clusters are zeroes on the backing image, efficient bdrv_write_zeroes(flags=BDRV_REQ_ALLOCATE) can be used on the whole cluster instead of writing explicit zero buffers later in perform_cow().

iotest 060:
write to the discarded cluster does not trigger COW anymore. Use a backing image instead.

---
 qapi/block-core.json       |  4 ++-
 block/qcow2.h              |  6 ++++
 block/qcow2-cluster.c      |  2 +-
 block/qcow2.c              | 79 ++++++++++++++++++++++++++++++++++++++++++++--
 block/trace-events         |  1 +
 tests/qemu-iotests/060     | 26 +++++++++------
 tests/qemu-iotests/060.out |  5 ++-
 7 files changed, 108 insertions(+), 15 deletions(-)
qapi/block-core.json +3 -1
index 00475f0..da61a0d 100644
@@ -2584,6 +2584,8 @@
 #
 # @cor_write: a write due to copy-on-read (since 2.11)
 #
+# @cluster_alloc_space: an allocation of a cluster file space (since 2.12)
+#
 # Since: 2.9
 ##
 { 'enum': 'BlkdebugEvent', 'prefix': 'BLKDBG',
@@ -2602,7 +2604,7 @@
             'pwritev_rmw_tail', 'pwritev_rmw_after_tail', 'pwritev',
             'pwritev_zero', 'pwritev_done', 'empty_image_prepare',
             'l1_shrink_write_table', 'l1_shrink_free_l2_clusters',
-            'cor_write'] }
+            'cor_write', 'cluster_alloc_space'] }
 
 ##
 # @BlkdebugInjectErrorOptions:
block/qcow2.h +6 -0
index 1a84cc7..2cf7dfd 100644
@@ -379,6 +379,12 @@ typedef struct QCowL2Meta
     Qcow2COWRegion cow_end;
 
     /**
+     * Indicates that COW regions are already handled and do not require
+     * any more processing.
+     */
+    bool skip_cow;
+
+    /**
      * The I/O vector with the data from the actual guest write request.
      * If non-NULL, this is meant to be merged together with the data
      * from @cow_start and @cow_end into one single write operation.
block/qcow2-cluster.c +1 -1
index 98908c4..0624d65 100644
@@ -805,7 +805,7 @@ static int perform_cow(BlockDriverState *bs, QCowL2Meta *m)
     assert(start->offset + start->nb_bytes <= end->offset);
     assert(!m->data_qiov || m->data_qiov->size == data_bytes);
 
-    if (start->nb_bytes == 0 && end->nb_bytes == 0) {
+    if ((start->nb_bytes == 0 && end->nb_bytes == 0) || m->skip_cow) {
         return 0;
     }
 
block/qcow2.c +77 -2

113 lines not on this page · raw message

block/trace-events +1 -0
index 02dd80f..2045ac9 100644
@@ -61,6 +61,7 @@ qcow2_writev_done_part(void *co, int cur_bytes) "co %p cur_bytes %d"
 qcow2_writev_data(void *co, uint64_t offset) "co %p offset 0x%" PRIx64
 qcow2_pwrite_zeroes_start_req(void *co, int64_t offset, int count) "co %p offset 0x%" PRIx64 " count %d"
 qcow2_pwrite_zeroes(void *co, int64_t offset, int count) "co %p offset 0x%" PRIx64 " count %d"
+qcow2_skip_cow(void *co, uint64_t offset, int nb_clusters) "co %p offset 0x%" PRIx64 " nb_clusters %d"
 
 # block/qcow2-cluster.c
 qcow2_alloc_clusters_offset(void *co, uint64_t offset, int bytes) "co %p offset 0x%" PRIx64 " bytes %d"
tests/qemu-iotests/060 +16 -10
index 6c7407f..e6ed8b2 100755
@@ -143,27 +143,33 @@ $QEMU_IO -c "$OPEN_RO" -c "read -P 1 0 512" | _filter_qemu_io
 echo
 echo "=== Testing overlap while COW is in flight ==="
 echo
+BACKING_IMG=$TEST_IMG.base
+TEST_IMG=$BACKING_IMG _make_test_img 1G
+
+$QEMU_IO -c 'write 64k 64k' "$BACKING_IMG" | _filter_qemu_io
+
 # compat=0.10 is required in order to make the following discard actually
-# unallocate the sector rather than make it a zero sector - we want COW, after
-# all.
-IMGOPTS='compat=0.10' _make_test_img 1G
+# unallocate the sector rather than make it a zero sector as we would like
+# to reuse it for another guest offset
+IMGOPTS='compat=0.10' _make_test_img -b "$BACKING_IMG" 1G
 # Write two clusters, the second one enforces creation of an L2 table after
 # the first data cluster.
 $QEMU_IO -c 'write 0k 64k' -c 'write 512M 64k' "$TEST_IMG" | _filter_qemu_io
-# Discard the first cluster. This cluster will soon enough be reallocated and
-# used for COW.
+# Discard the first cluster. This cluster will soon enough be reallocated
 $QEMU_IO -c 'discard 0k 64k' "$TEST_IMG" | _filter_qemu_io
 # Now, corrupt the image by marking the second L2 table cluster as free.
 poke_file "$TEST_IMG" '131084' "\x00\x00" # 0x2000c
-# Start a write operation requiring COW on the image stopping it right before
-# doing the read; then, trigger the corruption prevention by writing anything to
-# any unallocated cluster, leading to an attempt to overwrite the second L2
+# Start a write operation requiring COW on the image;
+# this write will reuse the host offset released by a previous discard.
+# Stop it right before doing the read.
+# Then, trigger the corruption prevention by writing anything to
+# another unallocated cluster, leading to an attempt to overwrite the second L2
 # table. Finally, resume the COW write and see it fail (but not crash).
 echo "open -o file.driver=blkdebug $TEST_IMG
 break cow_read 0
-aio_write 0k 1k
+aio_write 64k 1k
 wait_break 0
-write 64k 64k
+write 128k 64k
 resume 0" | $QEMU_IO | _filter_qemu_io
 
 echo
tests/qemu-iotests/060.out +4 -1
index 25d5c39..00a6702 100644
@@ -97,7 +97,10 @@ read 512/512 bytes at offset 0
 
 === Testing overlap while COW is in flight ===
 
-Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=1073741824
+Formatting 'TEST_DIR/t.IMGFMT.base', fmt=IMGFMT size=1073741824
+wrote 65536/65536 bytes at offset 65536
+64 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=1073741824 backing_file=TEST_DIR/t.IMGFMT.base
 wrote 65536/65536 bytes at offset 0
 64 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
 wrote 65536/65536 bytes at offset 536870912
Signature · 1 line
2.7.4
#16 lore raw

Re: [Qemu-devel] [PATCH v8 8/9] qcow2: skip writing zero buffers to empty COW areas

qapi/block-core.json line 2587

Quoted 7 lines · Anton Nefedov
qapi/block-core.json +1 -0
@@ -2584,6 +2584,8 @@
 #
 # @cor_write: a write due to copy-on-read (since 2.11)
 #
+# @cluster_alloc_space: an allocation of a cluster file space (since 2.12)

That doesn't sound like correct English to me.

qapi/block-core.json

Quoted 7 lines
qapi/block-core.json +7 -0
+static bool is_unallocated(BlockDriverState *bs, int64_t offset, int64_t bytes)
+{
+    int64_t nr;
+    return !bytes ||
+        (!bdrv_is_allocated_above(bs, NULL, offset, bytes, &nr) && nr == bytes);
+}
+

Is this really more efficient than the previous is_zero() call ?

It seems that in both cases the code ends up calling bdrv_common_block_status_above().

qapi/block-core.json

Quoted 5 lines
qapi/block-core.json +5 -0
+static bool is_zero_cow(BlockDriverState *bs, QCowL2Meta *m)
+{
+    /* content with false negatives, giving is_allocated() is faster than
+     * a proper zero detection with possible actual image seeks, which is
+     * performed by is_zero() */

It took me a bit to understand this sentence, maybe some native English speaker can suggest an alternate wording?

"is_allocated() is not as accurate as is_zero() and can give us some
false negatives, but it is much more efficient so let's use it here"

qapi/block-core.json

Quoted 5 lines
qapi/block-core.json +5 -0
+    return is_unallocated(bs, m->offset + m->cow_start.offset,
+                          m->cow_start.nb_bytes) &&
+           is_unallocated(bs, m->offset + m->cow_end.offset,
+                          m->cow_end.nb_bytes);
+}

Berto

#17 lore raw

Re: [Qemu-devel] [PATCH v8 8/9] qcow2: skip writing zero buffers to empty COW areas

Quoted 8 lines · Alberto Garcia

qapi/block-core.json line 2587

Quoted 7 lines · Anton Nefedov
qapi/block-core.json +1 -0
@@ -2584,6 +2584,8 @@
  #
  # @cor_write: a write due to copy-on-read (since 2.11)
  #
+# @cluster_alloc_space: an allocation of a cluster file space (since 2.12)

now it's 2.13 I believe

Quoted 3 lines

That doesn't sound like correct English to me.

how about
"an allocation of file space for a cluster"

Quoted 10 lines

qapi/block-core.json

Quoted 7 lines
qapi/block-core.json +7 -0
+static bool is_unallocated(BlockDriverState *bs, int64_t offset, int64_t bytes)
+{
+    int64_t nr;
+    return !bytes ||
+        (!bdrv_is_allocated_above(bs, NULL, offset, bytes, &nr) && nr == bytes);
+}
+

Is this really more efficient than the previous is_zero() call ?

Quoted 3 lines

It seems that in both cases the code ends up calling bdrv_common_block_status_above().

the difference is that is_allocated passes 'want_zero = 0'. It hints drivers to skip thorough seeks for zeroes (e.g. file-posix returns (BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID) right away). Moreover, bdrv_co_block_status() even skips the check in local_file unless the protocol driver returned BDRV_BLOCK_RAW.

Quoted 9 lines

qapi/block-core.json

Quoted 5 lines
qapi/block-core.json +5 -0
+static bool is_zero_cow(BlockDriverState *bs, QCowL2Meta *m)
+{
+    /* content with false negatives, giving is_allocated() is faster than
+     * a proper zero detection with possible actual image seeks, which is
+     * performed by is_zero() */

It took me a bit to understand this sentence, maybe some native English speaker can suggest an alternate wording?

Quoted 3 lines

"is_allocated() is not as accurate as is_zero() and can give us some
false negatives, but it is much more efficient so let's use it here"

wording is the hardest part of it all :)
maybe:

/* This check is designed for optimization shortcut so it must be
* efficient.
* Instead of is_zero(), use is_unallocated() as it is faster (but not
* as accurate and can result in false negatives). */

Quoted 8 lines

qapi/block-core.json

Quoted 5 lines
qapi/block-core.json +5 -0
+    return is_unallocated(bs, m->offset + m->cow_start.offset,
+                          m->cow_start.nb_bytes) &&
+           is_unallocated(bs, m->offset + m->cow_end.offset,
+                          m->cow_end.nb_bytes);
+}

Berto

#18 lore raw

PATCH v8 9/9 iotest 134: test cluster-misaligned encrypted write

In reply to Anton Nefedov

COW (even empty/zero) areas require encryption too

---
 tests/qemu-iotests/134     |  9 +++++++++
 tests/qemu-iotests/134.out | 10 ++++++++++
 2 files changed, 19 insertions(+)
tests/qemu-iotests/134 +9 -0
index 9914415..6083ae4 100755
@@ -59,6 +59,15 @@ echo "== reading whole image =="
 $QEMU_IO --object $SECRET -c "read 0 $size" --image-opts $IMGSPEC | _filter_qemu_io | _filter_testdir
 
 echo
+echo "== rewriting cluster part =="
+$QEMU_IO --object $SECRET -c "write -P 0xb 512 512" --image-opts $IMGSPEC | _filter_qemu_io | _filter_testdir
+
+echo
+echo "== verify pattern =="
+$QEMU_IO --object $SECRET -c "read -P 0 0 512"  --image-opts $IMGSPEC | _filter_qemu_io | _filter_testdir
+$QEMU_IO --object $SECRET -c "read -P 0xb 512 512"  --image-opts $IMGSPEC | _filter_qemu_io | _filter_testdir
+
+echo
 echo "== rewriting whole image =="
 $QEMU_IO --object $SECRET -c "write -P 0xa 0 $size" --image-opts $IMGSPEC | _filter_qemu_io | _filter_testdir
 
tests/qemu-iotests/134.out +10 -0
index 972be49..09d46f6 100644
@@ -5,6 +5,16 @@ Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=134217728 encryption=on encrypt.
 read 134217728/134217728 bytes at offset 0
 128 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
 
+== rewriting cluster part ==
+wrote 512/512 bytes at offset 512
+512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+
+== verify pattern ==
+read 512/512 bytes at offset 0
+512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+read 512/512 bytes at offset 512
+512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+
 == rewriting whole image ==
 wrote 134217728/134217728 bytes at offset 0
 128 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
Signature · 1 line
2.7.4