From: Eric Blake <eblake@redhat.com>
To: qemu-devel@nongnu.org
Cc: kwolf@redhat.com, jsnow@redhat.com, qemu-block@nongnu.org,
el13635@mail.ntua.gr, Stefan Hajnoczi <stefanha@redhat.com>,
Fam Zheng <famz@redhat.com>, Max Reitz <mreitz@redhat.com>,
Jeff Cody <jcody@redhat.com>
Subject: [Qemu-devel] [PATCH v2 13/15] block: Convert bdrv_get_block_status_above() to bytes
Date: Mon, 3 Jul 2017 17:14:54 -0500 [thread overview]
Message-ID: <20170703221456.30817-14-eblake@redhat.com> (raw)
In-Reply-To: <20170703221456.30817-1-eblake@redhat.com>
We are gradually moving away from sector-based interfaces, towards
byte-based. In the common case, allocation is unlikely to ever use
values that are not naturally sector-aligned, but it is possible
that byte-based values will let us be more precise about allocation
at the end of an unaligned file that can do byte-based access.
Changing the name of the function from bdrv_get_block_status_above()
to bdrv_block_status_above() ensures that the compiler enforces that
all callers are updated. For now, the io.c layer still assert()s
that all callers are sector-aligned, but that can be relaxed when a
later patch implements byte-based block status in the drivers.
For the most part this patch is just the addition of scaling at the
callers followed by inverse scaling at bdrv_block_status(). But some
code, particularly bdrv_block_status(), gets a lot simpler because
it no longer has to mess with sectors.
For ease of review, bdrv_get_block_status() was tackled separately.
Signed-off-by: Eric Blake <eblake@redhat.com>
---
v2: rebase to earlier changes
---
include/block/block.h | 10 +++++-----
block/io.c | 39 ++++++++-------------------------------
block/mirror.c | 12 ++++--------
block/qcow2.c | 8 +++-----
qemu-img.c | 39 +++++++++++++++++++++++----------------
5 files changed, 43 insertions(+), 65 deletions(-)
diff --git a/include/block/block.h b/include/block/block.h
index e3e6582..ed2ea69 100644
--- a/include/block/block.h
+++ b/include/block/block.h
@@ -424,11 +424,11 @@ bool bdrv_can_write_zeroes_with_unmap(BlockDriverState *bs);
int64_t bdrv_block_status(BlockDriverState *bs, int64_t offset,
int64_t bytes, int64_t *pnum,
BlockDriverState **file);
-int64_t bdrv_get_block_status_above(BlockDriverState *bs,
- BlockDriverState *base,
- int64_t sector_num,
- int nb_sectors, int *pnum,
- BlockDriverState **file);
+int64_t bdrv_block_status_above(BlockDriverState *bs,
+ BlockDriverState *base,
+ int64_t offset,
+ int64_t bytes, int64_t *pnum,
+ BlockDriverState **file);
int bdrv_is_allocated(BlockDriverState *bs, int64_t offset, int64_t bytes,
int64_t *pnum);
int bdrv_is_allocated_above(BlockDriverState *top, BlockDriverState *base,
diff --git a/block/io.c b/block/io.c
index 85353fa..91d3e99 100644
--- a/block/io.c
+++ b/block/io.c
@@ -1903,7 +1903,7 @@ static int64_t coroutine_fn bdrv_co_block_status_above(BlockDriverState *bs,
return ret;
}
-/* Coroutine wrapper for bdrv_get_block_status_above() */
+/* Coroutine wrapper for bdrv_block_status_above() */
static void coroutine_fn bdrv_block_status_above_co_entry(void *opaque)
{
BdrvCoBlockStatusData *data = opaque;
@@ -1950,43 +1950,20 @@ static int64_t bdrv_common_block_status_above(BlockDriverState *bs,
return data.ret;
}
-int64_t bdrv_get_block_status_above(BlockDriverState *bs,
- BlockDriverState *base,
- int64_t sector_num,
- int nb_sectors, int *pnum,
- BlockDriverState **file)
+int64_t bdrv_block_status_above(BlockDriverState *bs, BlockDriverState *base,
+ int64_t offset, int64_t bytes, int64_t *pnum,
+ BlockDriverState **file)
{
- int64_t ret;
- int64_t n;
-
- ret = bdrv_common_block_status_above(bs, base, false,
- sector_num * BDRV_SECTOR_SIZE,
- nb_sectors * BDRV_SECTOR_SIZE,
- &n, file);
- if (ret < 0) {
- return ret;
- }
- assert(QEMU_IS_ALIGNED(n, BDRV_SECTOR_SIZE));
- *pnum = n >> BDRV_SECTOR_BITS;
- return ret;
+ return bdrv_common_block_status_above(bs, base, false, offset, bytes,
+ pnum, file);
}
int64_t bdrv_block_status(BlockDriverState *bs,
int64_t offset, int64_t bytes, int64_t *pnum,
BlockDriverState **file)
{
- int64_t ret;
- int n;
-
- assert(QEMU_IS_ALIGNED(offset | bytes, BDRV_SECTOR_SIZE));
- assert(bytes <= BDRV_REQUEST_MAX_BYTES);
- ret = bdrv_get_block_status_above(bs, backing_bs(bs),
- offset >> BDRV_SECTOR_BITS,
- bytes >> BDRV_SECTOR_BITS, &n, file);
- if (pnum) {
- *pnum = n * BDRV_SECTOR_SIZE;
- }
- return ret;
+ return bdrv_block_status_above(bs, backing_bs(bs),
+ offset, bytes, pnum, file);
}
int coroutine_fn bdrv_is_allocated(BlockDriverState *bs, int64_t offset,
diff --git a/block/mirror.c b/block/mirror.c
index 08c0c27..1d555bb 100644
--- a/block/mirror.c
+++ b/block/mirror.c
@@ -328,7 +328,6 @@ static uint64_t coroutine_fn mirror_iteration(MirrorBlockJob *s)
uint64_t delay_ns = 0;
/* At least the first dirty chunk is mirrored in one iteration. */
int nb_chunks = 1;
- int sectors_per_chunk = s->granularity >> BDRV_SECTOR_BITS;
bool write_zeroes_ok = bdrv_can_write_zeroes_with_unmap(blk_bs(s->target));
int max_io_bytes = MAX(s->buf_size / MAX_IN_FLIGHT, MAX_IO_BYTES);
@@ -377,7 +376,7 @@ static uint64_t coroutine_fn mirror_iteration(MirrorBlockJob *s)
}
/* Clear dirty bits before querying the block status, because
- * calling bdrv_get_block_status_above could yield - if some blocks are
+ * calling bdrv_block_status_above could yield - if some blocks are
* marked dirty in this window, we need to know.
*/
bdrv_reset_dirty_bitmap_locked(s->dirty_bitmap, offset,
@@ -387,7 +386,6 @@ static uint64_t coroutine_fn mirror_iteration(MirrorBlockJob *s)
bitmap_set(s->in_flight_bitmap, offset / s->granularity, nb_chunks);
while (nb_chunks > 0 && offset < s->bdev_length) {
int64_t ret;
- int io_sectors;
int64_t io_bytes;
int64_t io_bytes_acct;
enum MirrorMethod {
@@ -397,11 +395,9 @@ static uint64_t coroutine_fn mirror_iteration(MirrorBlockJob *s)
} mirror_method = MIRROR_METHOD_COPY;
assert(!(offset % s->granularity));
- ret = bdrv_get_block_status_above(source, NULL,
- offset >> BDRV_SECTOR_BITS,
- nb_chunks * sectors_per_chunk,
- &io_sectors, NULL);
- io_bytes = io_sectors * BDRV_SECTOR_SIZE;
+ ret = bdrv_block_status_above(source, NULL, offset,
+ nb_chunks * s->granularity,
+ &io_bytes, NULL);
if (ret < 0) {
io_bytes = MIN(nb_chunks * s->granularity, max_io_bytes);
} else if (ret & BDRV_BLOCK_DATA) {
diff --git a/block/qcow2.c b/block/qcow2.c
index 9754193..d251bf7 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -2907,7 +2907,7 @@ finish:
static bool is_zero(BlockDriverState *bs, int64_t offset, int64_t bytes)
{
- int nr;
+ int64_t nr;
int64_t res;
int64_t start;
@@ -2923,10 +2923,8 @@ static bool is_zero(BlockDriverState *bs, int64_t offset, int64_t bytes)
if (!bytes) {
return true;
}
- res = bdrv_get_block_status_above(bs, NULL, start >> BDRV_SECTOR_BITS,
- bytes >> BDRV_SECTOR_BITS, &nr, NULL);
- return res >= 0 && (res & BDRV_BLOCK_ZERO) &&
- nr * BDRV_SECTOR_SIZE == bytes;
+ res = bdrv_block_status_above(bs, NULL, start, bytes, &nr, NULL);
+ return res >= 0 && (res & BDRV_BLOCK_ZERO) && nr == bytes;
}
static coroutine_fn int qcow2_co_pwrite_zeroes(BlockDriverState *bs,
diff --git a/qemu-img.c b/qemu-img.c
index 2b1422a..39aff13 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -1224,7 +1224,7 @@ static int img_compare(int argc, char **argv)
BlockDriverState *bs1, *bs2;
int64_t total_sectors1, total_sectors2;
uint8_t *buf1 = NULL, *buf2 = NULL;
- int pnum1, pnum2;
+ int64_t pnum1, pnum2;
int allocated1, allocated2;
int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
bool progress = false, quiet = false, strict = false;
@@ -1378,9 +1378,11 @@ static int img_compare(int argc, char **argv)
if (nb_sectors <= 0) {
break;
}
- status1 = bdrv_get_block_status_above(bs1, NULL, sector_num,
- total_sectors1 - sector_num,
- &pnum1, NULL);
+ status1 = bdrv_block_status_above(bs1, NULL,
+ sector_num * BDRV_SECTOR_SIZE,
+ (total_sectors1 - sector_num) *
+ BDRV_SECTOR_SIZE,
+ &pnum1, NULL);
if (status1 < 0) {
ret = 3;
error_report("Sector allocation test failed for %s", filename1);
@@ -1388,9 +1390,11 @@ static int img_compare(int argc, char **argv)
}
allocated1 = status1 & BDRV_BLOCK_ALLOCATED;
- status2 = bdrv_get_block_status_above(bs2, NULL, sector_num,
- total_sectors2 - sector_num,
- &pnum2, NULL);
+ status2 = bdrv_block_status_above(bs2, NULL,
+ sector_num * BDRV_SECTOR_SIZE,
+ (total_sectors2 - sector_num) *
+ BDRV_SECTOR_SIZE,
+ &pnum2, NULL);
if (status2 < 0) {
ret = 3;
error_report("Sector allocation test failed for %s", filename2);
@@ -1398,10 +1402,12 @@ static int img_compare(int argc, char **argv)
}
allocated2 = status2 & BDRV_BLOCK_ALLOCATED;
if (pnum1) {
- nb_sectors = MIN(nb_sectors, pnum1);
+ nb_sectors = MIN(nb_sectors,
+ DIV_ROUND_UP(pnum1, BDRV_SECTOR_SIZE));
}
if (pnum2) {
- nb_sectors = MIN(nb_sectors, pnum2);
+ nb_sectors = MIN(nb_sectors,
+ DIV_ROUND_UP(pnum2, BDRV_SECTOR_SIZE));
}
if (strict) {
@@ -1415,7 +1421,7 @@ static int img_compare(int argc, char **argv)
}
}
if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) {
- nb_sectors = MIN(pnum1, pnum2);
+ nb_sectors = DIV_ROUND_UP(MIN(pnum1, pnum2), BDRV_SECTOR_SIZE);
} else if (allocated1 == allocated2) {
if (allocated1) {
ret = blk_pread(blk1, sector_num << BDRV_SECTOR_BITS, buf1,
@@ -1593,23 +1599,24 @@ static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
if (s->sector_next_status <= sector_num) {
+ int64_t count = n * BDRV_SECTOR_SIZE;
+
if (s->target_has_backing) {
- int64_t count = n * BDRV_SECTOR_SIZE;
ret = bdrv_block_status(blk_bs(s->src[src_cur]),
(sector_num - src_cur_offset) *
BDRV_SECTOR_SIZE,
count, &count, NULL);
- assert(ret < 0 || QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE));
- n = count >> BDRV_SECTOR_BITS;
} else {
- ret = bdrv_get_block_status_above(blk_bs(s->src[src_cur]), NULL,
- sector_num - src_cur_offset,
- n, &n, NULL);
+ ret = bdrv_block_status_above(blk_bs(s->src[src_cur]), NULL,
+ (sector_num - src_cur_offset) *
+ BDRV_SECTOR_SIZE,
+ count, &count, NULL);
}
if (ret < 0) {
return ret;
}
+ n = DIV_ROUND_UP(count, BDRV_SECTOR_SIZE);
if (ret & BDRV_BLOCK_ZERO) {
s->status = BLK_ZERO;
--
2.9.4
next prev parent reply other threads:[~2017-07-03 22:15 UTC|newest]
Thread overview: 41+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-07-03 22:14 [Qemu-devel] [PATCH v2 00/15] make bdrv_get_block_status byte-based Eric Blake
2017-07-03 22:14 ` [Qemu-devel] [PATCH v2 01/15] block: add default implementations for bdrv_co_get_block_status() Eric Blake
2017-07-04 7:33 ` Fam Zheng
2017-07-05 22:00 ` Eric Blake
2017-07-03 22:14 ` [Qemu-devel] [PATCH v2 02/15] block: Allow NULL file for bdrv_get_block_status() Eric Blake
2017-07-04 7:33 ` Fam Zheng
2017-07-03 22:14 ` [Qemu-devel] [PATCH v2 03/15] block: Add flag to avoid wasted work in bdrv_is_allocated() Eric Blake
2017-07-04 7:06 ` Fam Zheng
2017-07-05 11:56 ` Eric Blake
2017-07-05 12:07 ` Fam Zheng
2017-07-05 14:01 ` Eric Blake
2017-07-05 14:21 ` Fam Zheng
2017-07-06 2:35 ` Eric Blake
2017-07-03 22:14 ` [Qemu-devel] [PATCH v2 04/15] block: Make bdrv_round_to_clusters() signature more useful Eric Blake
2017-07-04 7:34 ` Fam Zheng
2017-07-03 22:14 ` [Qemu-devel] [PATCH v2 05/15] qcow2: Switch is_zero_sectors() to byte-based Eric Blake
2017-07-04 7:34 ` Fam Zheng
2017-07-03 22:14 ` [Qemu-devel] [PATCH v2 06/15] block: Switch bdrv_make_zero() " Eric Blake
2017-07-04 7:35 ` Fam Zheng
2017-07-03 22:14 ` [Qemu-devel] [PATCH v2 07/15] qemu-img: Switch get_block_status() " Eric Blake
2017-07-04 7:40 ` Fam Zheng
2017-07-03 22:14 ` [Qemu-devel] [PATCH v2 08/15] block: Convert bdrv_get_block_status() to bytes Eric Blake
2017-07-04 8:40 ` Fam Zheng
2017-07-03 22:14 ` [Qemu-devel] [PATCH v2 09/15] block: Switch bdrv_co_get_block_status() to byte-based Eric Blake
2017-07-04 8:59 ` Fam Zheng
2017-07-03 22:14 ` [Qemu-devel] [PATCH v2 10/15] block: Switch BdrvCoGetBlockStatusData " Eric Blake
2017-07-04 9:00 ` Fam Zheng
2017-07-03 22:14 ` [Qemu-devel] [PATCH v2 11/15] block: Switch bdrv_common_block_status_above() " Eric Blake
2017-07-04 9:02 ` Fam Zheng
2017-07-03 22:14 ` [Qemu-devel] [PATCH v2 12/15] block: Switch bdrv_co_get_block_status_above() " Eric Blake
2017-07-04 9:28 ` Fam Zheng
2017-07-03 22:14 ` Eric Blake [this message]
2017-07-04 9:32 ` [Qemu-devel] [PATCH v2 13/15] block: Convert bdrv_get_block_status_above() to bytes Fam Zheng
2017-07-03 22:14 ` [Qemu-devel] [PATCH v2 14/15] block: Align block status requests Eric Blake
2017-07-04 9:44 ` Fam Zheng
2017-07-05 12:01 ` Eric Blake
2017-07-05 12:12 ` Fam Zheng
2017-07-03 22:14 ` [Qemu-devel] [PATCH v2 15/15] qemu-io: Relax 'alloc' now that block-status doesn't assert Eric Blake
2017-07-04 9:50 ` Fam Zheng
2017-07-03 22:20 ` [Qemu-devel] [RFC PATCH v2 16/15] block: Add .bdrv_co_block_status() callback Eric Blake
2017-07-04 11:30 ` Fam Zheng
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=20170703221456.30817-14-eblake@redhat.com \
--to=eblake@redhat.com \
--cc=el13635@mail.ntua.gr \
--cc=famz@redhat.com \
--cc=jcody@redhat.com \
--cc=jsnow@redhat.com \
--cc=kwolf@redhat.com \
--cc=mreitz@redhat.com \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@redhat.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).