qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Eric Blake <eblake@redhat.com>
To: qemu-devel@nongnu.org
Cc: qemu-block@nongnu.org, kwolf@redhat.com, Max Reitz <mreitz@redhat.com>
Subject: [Qemu-devel] [PATCH 06/13] qcow2: Convert to bdrv_co_pwrite_zeroes()
Date: Tue, 24 May 2016 16:25:25 -0600	[thread overview]
Message-ID: <1464128732-12667-7-git-send-email-eblake@redhat.com> (raw)
In-Reply-To: <1464128732-12667-1-git-send-email-eblake@redhat.com>

Another step on our continuing quest to switch to byte-based
interfaces.

There are still opportunities to optimize the qcow2 handling
of zero clusters.  For example, if the backing file only has
non-zero data in the portion about to be overwritten, then
we could widen the request and make the entire cluster zero,
rather than falling back to -ENOTSUP.  But for this patch,
intentionally leave the semantics unchanged, even if not
optimal.

Signed-off-by: Eric Blake <eblake@redhat.com>
---
 block/qcow2.c | 46 +++++++++++++++++++++++++---------------------
 1 file changed, 25 insertions(+), 21 deletions(-)

diff --git a/block/qcow2.c b/block/qcow2.c
index 978694e..3522fc0 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -2428,43 +2428,47 @@ static bool is_zero_cluster_top_locked(BlockDriverState *bs, int64_t start)
     return ret == QCOW2_CLUSTER_UNALLOCATED || ret == QCOW2_CLUSTER_ZERO;
 }

-static coroutine_fn int qcow2_co_write_zeroes(BlockDriverState *bs,
-    int64_t sector_num, int nb_sectors, BdrvRequestFlags flags)
+static coroutine_fn int qcow2_co_pwrite_zeroes(BlockDriverState *bs,
+    int64_t offset, int count, BdrvRequestFlags flags)
 {
     int ret;
     BDRVQcow2State *s = bs->opaque;

-    int head = sector_num % s->cluster_sectors;
-    int tail = (sector_num + nb_sectors) % s->cluster_sectors;
+    int head = offset % s->cluster_size;
+    int tail = (offset + count) % s->cluster_size;

+    /* Widen the write to a full cluster, if the cluster already reads
+     * as zero. */
     if (head != 0 || tail != 0) {
-        int64_t cl_end = -1;
+        int64_t tail_sector = 0;

-        sector_num -= head;
-        nb_sectors += head;
-
-        if (tail != 0) {
-            nb_sectors += s->cluster_sectors - tail;
+        offset -= head;
+        count += head;
+        if (tail) {
+            count += s->cluster_size - tail;
         }

-        if (!is_zero_cluster(bs, sector_num)) {
+        if (!is_zero_cluster(bs, offset >> BDRV_SECTOR_BITS)) {
             return -ENOTSUP;
         }

-        if (nb_sectors > s->cluster_sectors) {
-            /* Technically the request can cover 2 clusters, f.e. 4k write
-               at s->cluster_sectors - 2k offset. One of these cluster can
-               be zeroed, one unallocated */
-            cl_end = sector_num + nb_sectors - s->cluster_sectors;
-            if (!is_zero_cluster(bs, cl_end)) {
+        if (count > s->cluster_size) {
+            /* Technically the request can cover 2 clusters, f.e. 4k
+             * write at s->cluster_sectors - 2k offset. One of these
+             * cluster can be zeroed, one unallocated. Anything larger
+             * and the front end already split it to alignment
+             * boundaries. */
+            assert(count == 2 * s->cluster_size);
+            tail_sector = (offset >> BDRV_SECTOR_BITS) + s->cluster_sectors;
+            if (!is_zero_cluster(bs, tail_sector)) {
                 return -ENOTSUP;
             }
         }

         qemu_co_mutex_lock(&s->lock);
         /* We can have new write after previous check */
-        if (!is_zero_cluster_top_locked(bs, sector_num) ||
-                (cl_end > 0 && !is_zero_cluster_top_locked(bs, cl_end))) {
+        if (!is_zero_cluster_top_locked(bs, offset >> BDRV_SECTOR_BITS) ||
+            (tail_sector && !is_zero_cluster_top_locked(bs, tail_sector))) {
             qemu_co_mutex_unlock(&s->lock);
             return -ENOTSUP;
         }
@@ -2473,7 +2477,7 @@ static coroutine_fn int qcow2_co_write_zeroes(BlockDriverState *bs,
     }

     /* Whatever is left can use real zero clusters */
-    ret = qcow2_zero_clusters(bs, sector_num << BDRV_SECTOR_BITS, nb_sectors);
+    ret = qcow2_zero_clusters(bs, offset, count >> BDRV_SECTOR_BITS);
     qemu_co_mutex_unlock(&s->lock);

     return ret;
@@ -3380,7 +3384,7 @@ BlockDriver bdrv_qcow2 = {
     .bdrv_co_writev         = qcow2_co_writev,
     .bdrv_co_flush_to_os    = qcow2_co_flush_to_os,

-    .bdrv_co_write_zeroes   = qcow2_co_write_zeroes,
+    .bdrv_co_pwrite_zeroes  = qcow2_co_pwrite_zeroes,
     .bdrv_co_discard        = qcow2_co_discard,
     .bdrv_truncate          = qcow2_truncate,
     .bdrv_write_compressed  = qcow2_write_compressed,
-- 
2.5.5

  parent reply	other threads:[~2016-05-24 22:26 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-05-24 22:25 [Qemu-devel] [PATCH 00/13] Kill sector-based write_zeroes Eric Blake
2016-05-24 22:25 ` [Qemu-devel] [PATCH 01/13] block: Rename blk_write_zeroes() Eric Blake
2016-05-24 22:25 ` [Qemu-devel] [PATCH 02/13] block: Track write zero limits in bytes Eric Blake
2016-05-25 10:30   ` Kevin Wolf
2016-05-25 11:21     ` Eric Blake
2016-05-24 22:25 ` [Qemu-devel] [PATCH 03/13] block: Add .bdrv_co_pwrite_zeroes() Eric Blake
2016-05-25 13:02   ` Kevin Wolf
2016-05-24 22:25 ` [Qemu-devel] [PATCH 04/13] block: Switch bdrv_write_zeroes() to byte interface Eric Blake
2016-05-25 13:18   ` Kevin Wolf
2016-05-24 22:25 ` [Qemu-devel] [PATCH 05/13] iscsi: Convert to bdrv_co_pwrite_zeroes() Eric Blake
2016-05-25 13:34   ` Kevin Wolf
2016-06-01 16:33     ` Eric Blake
2016-05-24 22:25 ` Eric Blake [this message]
2016-05-25 13:53   ` [Qemu-devel] [PATCH 06/13] qcow2: " Kevin Wolf
2016-05-24 22:25 ` [Qemu-devel] [PATCH 07/13] blkreplay: " Eric Blake
2016-05-25 13:54   ` Kevin Wolf
2016-05-24 22:25 ` [Qemu-devel] [PATCH 08/13] gluster: " Eric Blake
2016-05-25 13:57   ` Kevin Wolf
2016-05-24 22:25 ` [Qemu-devel] [PATCH 09/13] qed: " Eric Blake
2016-05-25 14:07   ` Kevin Wolf
2016-05-25 14:28     ` Eric Blake
2016-05-25 15:06       ` Kevin Wolf
2016-05-24 22:25 ` [Qemu-devel] [PATCH 10/13] raw-posix: " Eric Blake
2016-05-25 14:20   ` Kevin Wolf
2016-05-24 22:25 ` [Qemu-devel] [PATCH 11/13] raw_bsd: " Eric Blake
2016-05-25 14:20   ` Kevin Wolf
2016-05-24 22:25 ` [Qemu-devel] [PATCH 12/13] vmdk: " Eric Blake
2016-05-25 14:23   ` Kevin Wolf
2016-05-25 14:35     ` Eric Blake
2016-05-24 22:25 ` [Qemu-devel] [PATCH 13/13] block: Kill bdrv_co_write_zeroes() Eric Blake
2016-05-25 14:24   ` Kevin Wolf
2016-05-25 11:02 ` [Qemu-devel] [PATCH 00/13] Kill sector-based write_zeroes Kevin Wolf
2016-06-01 15:35 ` Kevin Wolf
2016-06-01 15:38   ` Eric Blake

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=1464128732-12667-7-git-send-email-eblake@redhat.com \
    --to=eblake@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).