qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Eric Blake <eblake@redhat.com>
To: qemu-devel@nongnu.org
Cc: kwolf@redhat.com, Fam Zheng <fam@euphon.net>,
	pkrempa@redhat.com, qemu-block@nongnu.org, armbru@redhat.com,
	rjones@redhat.com, vsementsov@virtuozzo.com, stefanha@redhat.com,
	Max Reitz <mreitz@redhat.com>, John Snow <jsnow@redhat.com>
Subject: [PATCH v6 08/11] block: Return depth level during bdrv_is_allocated_above
Date: Tue, 27 Oct 2020 00:05:53 -0500	[thread overview]
Message-ID: <20201027050556.269064-9-eblake@redhat.com> (raw)
In-Reply-To: <20201027050556.269064-1-eblake@redhat.com>

When checking for allocation across a chain, it's already easy to
count the depth within the chain at which the allocation is found.
Instead of throwing that information away, return it to the caller.
Existing callers only cared about allocated/non-allocated, but having
a depth available will be used by NBD in the next patch.

Signed-off-by: Eric Blake <eblake@redhat.com>
---
 block/coroutines.h |  6 ++++--
 block/io.c         | 29 ++++++++++++++++++++++-------
 block/commit.c     |  2 +-
 block/mirror.c     |  2 +-
 block/stream.c     |  2 +-
 5 files changed, 29 insertions(+), 12 deletions(-)

diff --git a/block/coroutines.h b/block/coroutines.h
index 1cb3128b942c..4cfb4946e65e 100644
--- a/block/coroutines.h
+++ b/block/coroutines.h
@@ -47,7 +47,8 @@ bdrv_co_common_block_status_above(BlockDriverState *bs,
                                   int64_t bytes,
                                   int64_t *pnum,
                                   int64_t *map,
-                                  BlockDriverState **file);
+                                  BlockDriverState **file,
+                                  int *depth);
 int generated_co_wrapper
 bdrv_common_block_status_above(BlockDriverState *bs,
                                BlockDriverState *base,
@@ -57,7 +58,8 @@ bdrv_common_block_status_above(BlockDriverState *bs,
                                int64_t bytes,
                                int64_t *pnum,
                                int64_t *map,
-                               BlockDriverState **file);
+                               BlockDriverState **file,
+                               int *depth);

 int coroutine_fn bdrv_co_readv_vmstate(BlockDriverState *bs,
                                        QEMUIOVector *qiov, int64_t pos);
diff --git a/block/io.c b/block/io.c
index 02528b3823fe..7751cdb81948 100644
--- a/block/io.c
+++ b/block/io.c
@@ -2349,20 +2349,28 @@ bdrv_co_common_block_status_above(BlockDriverState *bs,
                                   int64_t bytes,
                                   int64_t *pnum,
                                   int64_t *map,
-                                  BlockDriverState **file)
+                                  BlockDriverState **file,
+                                  int *depth)
 {
     int ret;
     BlockDriverState *p;
     int64_t eof = 0;
+    int dummy;

     assert(!include_base || base); /* Can't include NULL base */

+    if (!depth) {
+        depth = &dummy;
+    }
+    *depth = 0;
+
     if (!include_base && bs == base) {
         *pnum = bytes;
         return 0;
     }

     ret = bdrv_co_block_status(bs, want_zero, offset, bytes, pnum, map, file);
+    ++*depth;
     if (ret < 0 || *pnum == 0 || ret & BDRV_BLOCK_ALLOCATED || bs == base) {
         return ret;
     }
@@ -2379,6 +2387,7 @@ bdrv_co_common_block_status_above(BlockDriverState *bs,
     {
         ret = bdrv_co_block_status(p, want_zero, offset, bytes, pnum, map,
                                    file);
+        ++*depth;
         if (ret < 0) {
             return ret;
         }
@@ -2437,7 +2446,7 @@ int bdrv_block_status_above(BlockDriverState *bs, BlockDriverState *base,
                             int64_t *map, BlockDriverState **file)
 {
     return bdrv_common_block_status_above(bs, base, false, true, offset, bytes,
-                                          pnum, map, file);
+                                          pnum, map, file, NULL);
 }

 int bdrv_block_status(BlockDriverState *bs, int64_t offset, int64_t bytes,
@@ -2455,7 +2464,7 @@ int coroutine_fn bdrv_is_allocated(BlockDriverState *bs, int64_t offset,

     ret = bdrv_common_block_status_above(bs, bs, true, false, offset,
                                          bytes, pnum ? pnum : &dummy, NULL,
-                                         NULL);
+                                         NULL, NULL);
     if (ret < 0) {
         return ret;
     }
@@ -2465,8 +2474,9 @@ int coroutine_fn bdrv_is_allocated(BlockDriverState *bs, int64_t offset,
 /*
  * Given an image chain: ... -> [BASE] -> [INTER1] -> [INTER2] -> [TOP]
  *
- * Return 1 if (a prefix of) the given range is allocated in any image
- * between BASE and TOP (BASE is only included if include_base is set).
+ * Return a positive depth if (a prefix of) the given range is allocated
+ * in any image between BASE and TOP (BASE is only included if include_base
+ * is set).  Depth 1 is TOP, 2 is the first backing layer, and so forth.
  * BASE can be NULL to check if the given offset is allocated in any
  * image of the chain.  Return 0 otherwise, or negative errno on
  * failure.
@@ -2483,13 +2493,18 @@ int bdrv_is_allocated_above(BlockDriverState *top,
                             bool include_base, int64_t offset,
                             int64_t bytes, int64_t *pnum)
 {
+    int depth;
     int ret = bdrv_common_block_status_above(top, base, include_base, false,
-                                             offset, bytes, pnum, NULL, NULL);
+                                             offset, bytes, pnum, NULL, NULL,
+                                             &depth);
     if (ret < 0) {
         return ret;
     }

-    return !!(ret & BDRV_BLOCK_ALLOCATED);
+    if (ret & BDRV_BLOCK_ALLOCATED) {
+        return depth;
+    }
+    return 0;
 }

 int coroutine_fn
diff --git a/block/commit.c b/block/commit.c
index 1e85c306cc41..71db7ba7472e 100644
--- a/block/commit.c
+++ b/block/commit.c
@@ -156,7 +156,7 @@ static int coroutine_fn commit_run(Job *job, Error **errp)
         /* Copy if allocated above the base */
         ret = bdrv_is_allocated_above(blk_bs(s->top), s->base_overlay, true,
                                       offset, COMMIT_BUFFER_SIZE, &n);
-        copy = (ret == 1);
+        copy = (ret > 0);
         trace_commit_one_iteration(s, offset, n, ret);
         if (copy) {
             assert(n < SIZE_MAX);
diff --git a/block/mirror.c b/block/mirror.c
index 26acf4af6fb7..8e1ad6eceb57 100644
--- a/block/mirror.c
+++ b/block/mirror.c
@@ -846,7 +846,7 @@ static int coroutine_fn mirror_dirty_init(MirrorBlockJob *s)
         }

         assert(count);
-        if (ret == 1) {
+        if (ret > 0) {
             bdrv_set_dirty_bitmap(s->dirty_bitmap, offset, count);
         }
         offset += count;
diff --git a/block/stream.c b/block/stream.c
index 8ce6729a33da..236384f2f739 100644
--- a/block/stream.c
+++ b/block/stream.c
@@ -167,7 +167,7 @@ static int coroutine_fn stream_run(Job *job, Error **errp)
                 n = len - offset;
             }

-            copy = (ret == 1);
+            copy = (ret > 0);
         }
         trace_stream_one_iteration(s, offset, n, ret);
         if (copy) {
-- 
2.29.0



  parent reply	other threads:[~2020-10-27  5:12 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-27  5:05 [PATCH v6 00/11] Exposing backing-chain allocation over NBD Eric Blake
2020-10-27  5:05 ` [PATCH v6 01/11] block: Simplify QAPI_LIST_ADD Eric Blake
2020-10-27 10:06   ` Vladimir Sementsov-Ogievskiy
2020-10-27 12:58   ` Markus Armbruster
2020-10-27  5:05 ` [PATCH v6 02/11] qapi: Make QAPI_LIST_ADD() public Eric Blake
2020-10-27  5:05 ` [PATCH v6 03/11] nbd: Utilize QAPI_CLONE for type conversion Eric Blake
2020-10-27  5:05 ` [PATCH v6 04/11] nbd: Update qapi to support exporting multiple bitmaps Eric Blake
2020-10-27 10:29   ` Vladimir Sementsov-Ogievskiy
2020-10-27 12:37   ` Peter Krempa
2020-10-27  5:05 ` [PATCH v6 05/11] nbd: Simplify qemu bitmap context name Eric Blake
2020-10-27  5:05 ` [PATCH v6 06/11] nbd: Refactor counting of metadata contexts Eric Blake
2020-10-27 10:33   ` Vladimir Sementsov-Ogievskiy
2020-10-27  5:05 ` [PATCH v6 07/11] nbd: Allow export of multiple bitmaps for one device Eric Blake
2020-10-27  5:05 ` Eric Blake [this message]
2020-10-27 12:05   ` [PATCH v6 08/11] block: Return depth level during bdrv_is_allocated_above Vladimir Sementsov-Ogievskiy
2020-10-27  5:05 ` [PATCH v6 09/11] nbd: Add new qemu:allocation-depth metadata context Eric Blake
2020-10-27 10:53   ` Vladimir Sementsov-Ogievskiy
2020-10-27  5:05 ` [PATCH v6 10/11] nbd: Add 'qemu-nbd -A' to expose allocation depth Eric Blake
2020-10-27 11:03   ` Vladimir Sementsov-Ogievskiy
2020-10-27  5:05 ` [PATCH v6 11/11] qapi: Use QAPI_LIST_ADD() where possible Eric Blake
2020-10-27  5:53   ` Thomas Huth
2020-10-27  6:39   ` David Gibson
2020-10-27 10:09   ` Markus Armbruster
2020-10-27 12:28     ` Eric Blake
2020-10-27 15:36       ` Markus Armbruster
2020-10-27 18:44         ` Eric Blake
2020-10-27 11:26   ` Dr. David Alan Gilbert
2020-10-27 13:42   ` Vladimir Sementsov-Ogievskiy

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=20201027050556.269064-9-eblake@redhat.com \
    --to=eblake@redhat.com \
    --cc=armbru@redhat.com \
    --cc=fam@euphon.net \
    --cc=jsnow@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=pkrempa@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=rjones@redhat.com \
    --cc=stefanha@redhat.com \
    --cc=vsementsov@virtuozzo.com \
    /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).