From nobody Sun Oct 5 19:15:27 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1499289074889737.0601325468641; Wed, 5 Jul 2017 14:11:14 -0700 (PDT) Received: from localhost ([::1]:48060 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSrZl-0006G8-Pi for importer@patchew.org; Wed, 05 Jul 2017 17:11:09 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37792) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSrXk-0004oO-49 for qemu-devel@nongnu.org; Wed, 05 Jul 2017 17:09:07 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dSrXi-0002cj-Pc for qemu-devel@nongnu.org; Wed, 05 Jul 2017 17:09:04 -0400 Received: from mx1.redhat.com ([209.132.183.28]:53426) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dSrXc-0002YJ-RY; Wed, 05 Jul 2017 17:08:57 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D82A17CE04; Wed, 5 Jul 2017 21:08:55 +0000 (UTC) Received: from red.redhat.com (ovpn-120-36.rdu2.redhat.com [10.10.120.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id C158B600CC; Wed, 5 Jul 2017 21:08:54 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com D82A17CE04 Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=eblake@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com D82A17CE04 From: Eric Blake To: qemu-devel@nongnu.org Date: Wed, 5 Jul 2017 16:08:22 -0500 Message-Id: <20170705210842.960-2-eblake@redhat.com> In-Reply-To: <20170705210842.960-1-eblake@redhat.com> References: <20170705210842.960-1-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Wed, 05 Jul 2017 21:08:56 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v4 01/21] blockjob: Track job ratelimits via bytes, not sectors X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, jcody@redhat.com, jsnow@redhat.com, qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The user interface specifies job rate limits in bytes/second. It's pointless to have our internal representation track things in sectors/second, particularly since we want to move away from sector-based interfaces. Fix up a doc typo found while verifying that the ratelimit code handles the scaling difference. Repetition of expressions like 'n * BDRV_SECTOR_SIZE' will be cleaned up later when functions are converted to iterate over images by bytes rather than by sectors. Signed-off-by: Eric Blake Reviewed-by: John Snow Reviewed-by: Jeff Cody Reviewed-by: Kevin Wolf --- v2: adjust commit message based on review; no code change --- include/qemu/ratelimit.h | 3 ++- block/backup.c | 5 +++-- block/commit.c | 5 +++-- block/mirror.c | 13 +++++++------ block/stream.c | 5 +++-- 5 files changed, 18 insertions(+), 13 deletions(-) diff --git a/include/qemu/ratelimit.h b/include/qemu/ratelimit.h index 8da1232..8dece48 100644 --- a/include/qemu/ratelimit.h +++ b/include/qemu/ratelimit.h @@ -24,7 +24,8 @@ typedef struct { /** Calculate and return delay for next request in ns * - * Record that we sent @p n data units. If we may send more data units + * Record that we sent @n data units (where @n matches the scale chosen + * during ratelimit_set_speed). If we may send more data units * in the current time slice, return 0 (i.e. no delay). Otherwise * return the amount of time (in ns) until the start of the next time * slice that will permit sending the next chunk of data. diff --git a/block/backup.c b/block/backup.c index 5387fbd..9ca1d8e 100644 --- a/block/backup.c +++ b/block/backup.c @@ -208,7 +208,7 @@ static void backup_set_speed(BlockJob *job, int64_t spe= ed, Error **errp) error_setg(errp, QERR_INVALID_PARAMETER, "speed"); return; } - ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME); + ratelimit_set_speed(&s->limit, speed, SLICE_TIME); } static void backup_cleanup_sync_bitmap(BackupBlockJob *job, int ret) @@ -359,7 +359,8 @@ static bool coroutine_fn yield_and_check(BackupBlockJob= *job) */ if (job->common.speed) { uint64_t delay_ns =3D ratelimit_calculate_delay(&job->limit, - job->sectors_read); + job->sectors_read * + BDRV_SECTOR_SIZE); job->sectors_read =3D 0; block_job_sleep_ns(&job->common, QEMU_CLOCK_REALTIME, delay_ns); } else { diff --git a/block/commit.c b/block/commit.c index 524bd54..6993994 100644 --- a/block/commit.c +++ b/block/commit.c @@ -209,7 +209,8 @@ static void coroutine_fn commit_run(void *opaque) s->common.offset +=3D n * BDRV_SECTOR_SIZE; if (copy && s->common.speed) { - delay_ns =3D ratelimit_calculate_delay(&s->limit, n); + delay_ns =3D ratelimit_calculate_delay(&s->limit, + n * BDRV_SECTOR_SIZE); } } @@ -231,7 +232,7 @@ static void commit_set_speed(BlockJob *job, int64_t spe= ed, Error **errp) error_setg(errp, QERR_INVALID_PARAMETER, "speed"); return; } - ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME); + ratelimit_set_speed(&s->limit, speed, SLICE_TIME); } static const BlockJobDriver commit_job_driver =3D { diff --git a/block/mirror.c b/block/mirror.c index 61a862d..eb27efc 100644 --- a/block/mirror.c +++ b/block/mirror.c @@ -396,7 +396,8 @@ static uint64_t coroutine_fn mirror_iteration(MirrorBlo= ckJob *s) bitmap_set(s->in_flight_bitmap, sector_num / sectors_per_chunk, nb_chu= nks); while (nb_chunks > 0 && sector_num < end) { int64_t ret; - int io_sectors, io_sectors_acct; + int io_sectors; + int64_t io_bytes_acct; BlockDriverState *file; enum MirrorMethod { MIRROR_METHOD_COPY, @@ -444,16 +445,16 @@ static uint64_t coroutine_fn mirror_iteration(MirrorB= lockJob *s) switch (mirror_method) { case MIRROR_METHOD_COPY: io_sectors =3D mirror_do_read(s, sector_num, io_sectors); - io_sectors_acct =3D io_sectors; + io_bytes_acct =3D io_sectors * BDRV_SECTOR_SIZE; break; case MIRROR_METHOD_ZERO: case MIRROR_METHOD_DISCARD: mirror_do_zero_or_discard(s, sector_num, io_sectors, mirror_method =3D=3D MIRROR_METHOD_D= ISCARD); if (write_zeroes_ok) { - io_sectors_acct =3D 0; + io_bytes_acct =3D 0; } else { - io_sectors_acct =3D io_sectors; + io_bytes_acct =3D io_sectors * BDRV_SECTOR_SIZE; } break; default: @@ -463,7 +464,7 @@ static uint64_t coroutine_fn mirror_iteration(MirrorBlo= ckJob *s) sector_num +=3D io_sectors; nb_chunks -=3D DIV_ROUND_UP(io_sectors, sectors_per_chunk); if (s->common.speed) { - delay_ns =3D ratelimit_calculate_delay(&s->limit, io_sectors_a= cct); + delay_ns =3D ratelimit_calculate_delay(&s->limit, io_bytes_acc= t); } } return delay_ns; @@ -929,7 +930,7 @@ static void mirror_set_speed(BlockJob *job, int64_t spe= ed, Error **errp) error_setg(errp, QERR_INVALID_PARAMETER, "speed"); return; } - ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME); + ratelimit_set_speed(&s->limit, speed, SLICE_TIME); } static void mirror_complete(BlockJob *job, Error **errp) diff --git a/block/stream.c b/block/stream.c index 52d329f..29273a5 100644 --- a/block/stream.c +++ b/block/stream.c @@ -191,7 +191,8 @@ static void coroutine_fn stream_run(void *opaque) /* Publish progress */ s->common.offset +=3D n * BDRV_SECTOR_SIZE; if (copy && s->common.speed) { - delay_ns =3D ratelimit_calculate_delay(&s->limit, n); + delay_ns =3D ratelimit_calculate_delay(&s->limit, + n * BDRV_SECTOR_SIZE); } } @@ -220,7 +221,7 @@ static void stream_set_speed(BlockJob *job, int64_t spe= ed, Error **errp) error_setg(errp, QERR_INVALID_PARAMETER, "speed"); return; } - ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME); + ratelimit_set_speed(&s->limit, speed, SLICE_TIME); } static const BlockJobDriver stream_job_driver =3D { --=20 2.9.4 From nobody Sun Oct 5 19:15:27 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1499289264962490.83434034557627; Wed, 5 Jul 2017 14:14:24 -0700 (PDT) Received: from localhost ([::1]:48075 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSrct-00010D-Nw for importer@patchew.org; Wed, 05 Jul 2017 17:14:23 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37900) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSrXs-0004v7-Dd for qemu-devel@nongnu.org; Wed, 05 Jul 2017 17:09:14 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dSrXn-0002f9-I1 for qemu-devel@nongnu.org; Wed, 05 Jul 2017 17:09:12 -0400 Received: from mx1.redhat.com ([209.132.183.28]:44752) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dSrXe-0002Yo-6d; Wed, 05 Jul 2017 17:08:58 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 25E1B80C14; Wed, 5 Jul 2017 21:08:57 +0000 (UTC) Received: from red.redhat.com (ovpn-120-36.rdu2.redhat.com [10.10.120.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id 23C00600CC; Wed, 5 Jul 2017 21:08:56 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 25E1B80C14 Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=eblake@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 25E1B80C14 From: Eric Blake To: qemu-devel@nongnu.org Date: Wed, 5 Jul 2017 16:08:23 -0500 Message-Id: <20170705210842.960-3-eblake@redhat.com> In-Reply-To: <20170705210842.960-1-eblake@redhat.com> References: <20170705210842.960-1-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Wed, 05 Jul 2017 21:08:57 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v4 02/21] trace: Show blockjob actions via bytes, not sectors X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, jcody@redhat.com, jsnow@redhat.com, qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Upcoming patches are going to switch to byte-based interfaces instead of sector-based. Even worse, trace_backup_do_cow_enter() had a weird mix of cluster and sector indices. The trace interface is low enough that there are no stability guarantees, and therefore nothing wrong with changing our units, even in cases like trace_backup_do_cow_skip() where we are not changing the trace output. So make the tracing uniformly use bytes. Signed-off-by: Eric Blake Reviewed-by: John Snow Reviewed-by: Jeff Cody Reviewed-by: Kevin Wolf --- v2: improve commit message, no code change --- block/backup.c | 16 ++++++++++------ block/commit.c | 3 ++- block/mirror.c | 26 +++++++++++++++++--------- block/stream.c | 3 ++- block/trace-events | 14 +++++++------- 5 files changed, 38 insertions(+), 24 deletions(-) diff --git a/block/backup.c b/block/backup.c index 9ca1d8e..06431ac 100644 --- a/block/backup.c +++ b/block/backup.c @@ -102,6 +102,7 @@ static int coroutine_fn backup_do_cow(BackupBlockJob *j= ob, void *bounce_buffer =3D NULL; int ret =3D 0; int64_t sectors_per_cluster =3D cluster_size_sectors(job); + int64_t bytes_per_cluster =3D sectors_per_cluster * BDRV_SECTOR_SIZE; int64_t start, end; int n; @@ -110,18 +111,20 @@ static int coroutine_fn backup_do_cow(BackupBlockJob = *job, start =3D sector_num / sectors_per_cluster; end =3D DIV_ROUND_UP(sector_num + nb_sectors, sectors_per_cluster); - trace_backup_do_cow_enter(job, start, sector_num, nb_sectors); + trace_backup_do_cow_enter(job, start * bytes_per_cluster, + sector_num * BDRV_SECTOR_SIZE, + nb_sectors * BDRV_SECTOR_SIZE); wait_for_overlapping_requests(job, start, end); cow_request_begin(&cow_request, job, start, end); for (; start < end; start++) { if (test_bit(start, job->done_bitmap)) { - trace_backup_do_cow_skip(job, start); + trace_backup_do_cow_skip(job, start * bytes_per_cluster); continue; /* already copied */ } - trace_backup_do_cow_process(job, start); + trace_backup_do_cow_process(job, start * bytes_per_cluster); n =3D MIN(sectors_per_cluster, job->common.len / BDRV_SECTOR_SIZE - @@ -138,7 +141,7 @@ static int coroutine_fn backup_do_cow(BackupBlockJob *j= ob, bounce_qiov.size, &bounce_qiov, is_write_notifier ? BDRV_REQ_NO_SERIALISING : = 0); if (ret < 0) { - trace_backup_do_cow_read_fail(job, start, ret); + trace_backup_do_cow_read_fail(job, start * bytes_per_cluster, = ret); if (error_is_read) { *error_is_read =3D true; } @@ -154,7 +157,7 @@ static int coroutine_fn backup_do_cow(BackupBlockJob *j= ob, job->compress ? BDRV_REQ_WRITE_COMPRESSED= : 0); } if (ret < 0) { - trace_backup_do_cow_write_fail(job, start, ret); + trace_backup_do_cow_write_fail(job, start * bytes_per_cluster,= ret); if (error_is_read) { *error_is_read =3D false; } @@ -177,7 +180,8 @@ out: cow_request_end(&cow_request); - trace_backup_do_cow_return(job, sector_num, nb_sectors, ret); + trace_backup_do_cow_return(job, sector_num * BDRV_SECTOR_SIZE, + nb_sectors * BDRV_SECTOR_SIZE, ret); qemu_co_rwlock_unlock(&job->flush_rwlock); diff --git a/block/commit.c b/block/commit.c index 6993994..4cda7f2 100644 --- a/block/commit.c +++ b/block/commit.c @@ -190,7 +190,8 @@ static void coroutine_fn commit_run(void *opaque) COMMIT_BUFFER_SIZE / BDRV_SECTOR_SIZ= E, &n); copy =3D (ret =3D=3D 1); - trace_commit_one_iteration(s, sector_num, n, ret); + trace_commit_one_iteration(s, sector_num * BDRV_SECTOR_SIZE, + n * BDRV_SECTOR_SIZE, ret); if (copy) { ret =3D commit_populate(s->top, s->base, sector_num, n, buf); bytes_written +=3D n * BDRV_SECTOR_SIZE; diff --git a/block/mirror.c b/block/mirror.c index eb27efc..b4dfe95 100644 --- a/block/mirror.c +++ b/block/mirror.c @@ -103,7 +103,8 @@ static void mirror_iteration_done(MirrorOp *op, int ret) int64_t chunk_num; int i, nb_chunks, sectors_per_chunk; - trace_mirror_iteration_done(s, op->sector_num, op->nb_sectors, ret); + trace_mirror_iteration_done(s, op->sector_num * BDRV_SECTOR_SIZE, + op->nb_sectors * BDRV_SECTOR_SIZE, ret); s->in_flight--; s->sectors_in_flight -=3D op->nb_sectors; @@ -268,7 +269,8 @@ static int mirror_do_read(MirrorBlockJob *s, int64_t se= ctor_num, nb_chunks =3D DIV_ROUND_UP(nb_sectors, sectors_per_chunk); while (s->buf_free_count < nb_chunks) { - trace_mirror_yield_in_flight(s, sector_num, s->in_flight); + trace_mirror_yield_in_flight(s, sector_num * BDRV_SECTOR_SIZE, + s->in_flight); mirror_wait_for_io(s); } @@ -294,7 +296,8 @@ static int mirror_do_read(MirrorBlockJob *s, int64_t se= ctor_num, /* Copy the dirty cluster. */ s->in_flight++; s->sectors_in_flight +=3D nb_sectors; - trace_mirror_one_iteration(s, sector_num, nb_sectors); + trace_mirror_one_iteration(s, sector_num * BDRV_SECTOR_SIZE, + nb_sectors * BDRV_SECTOR_SIZE); blk_aio_preadv(source, sector_num * BDRV_SECTOR_SIZE, &op->qiov, 0, mirror_read_complete, op); @@ -347,14 +350,16 @@ static uint64_t coroutine_fn mirror_iteration(MirrorB= lockJob *s) if (sector_num < 0) { bdrv_set_dirty_iter(s->dbi, 0); sector_num =3D bdrv_dirty_iter_next(s->dbi); - trace_mirror_restart_iter(s, bdrv_get_dirty_count(s->dirty_bitmap)= ); + trace_mirror_restart_iter(s, bdrv_get_dirty_count(s->dirty_bitmap)= * + BDRV_SECTOR_SIZE); assert(sector_num >=3D 0); } bdrv_dirty_bitmap_unlock(s->dirty_bitmap); first_chunk =3D sector_num / sectors_per_chunk; while (test_bit(first_chunk, s->in_flight_bitmap)) { - trace_mirror_yield_in_flight(s, sector_num, s->in_flight); + trace_mirror_yield_in_flight(s, sector_num * BDRV_SECTOR_SIZE, + s->in_flight); mirror_wait_for_io(s); } @@ -433,7 +438,8 @@ static uint64_t coroutine_fn mirror_iteration(MirrorBlo= ckJob *s) } while (s->in_flight >=3D MAX_IN_FLIGHT) { - trace_mirror_yield_in_flight(s, sector_num, s->in_flight); + trace_mirror_yield_in_flight(s, sector_num * BDRV_SECTOR_SIZE, + s->in_flight); mirror_wait_for_io(s); } @@ -823,7 +829,8 @@ static void coroutine_fn mirror_run(void *opaque) s->common.iostatus =3D=3D BLOCK_DEVICE_IO_STATUS_OK) { if (s->in_flight >=3D MAX_IN_FLIGHT || s->buf_free_count =3D= =3D 0 || (cnt =3D=3D 0 && s->in_flight > 0)) { - trace_mirror_yield(s, cnt, s->buf_free_count, s->in_flight= ); + trace_mirror_yield(s, cnt * BDRV_SECTOR_SIZE, + s->buf_free_count, s->in_flight); mirror_wait_for_io(s); continue; } else if (cnt !=3D 0) { @@ -864,7 +871,7 @@ static void coroutine_fn mirror_run(void *opaque) * whether to switch to target check one last time if I/O has * come in the meanwhile, and if not flush the data to disk. */ - trace_mirror_before_drain(s, cnt); + trace_mirror_before_drain(s, cnt * BDRV_SECTOR_SIZE); bdrv_drained_begin(bs); cnt =3D bdrv_get_dirty_count(s->dirty_bitmap); @@ -883,7 +890,8 @@ static void coroutine_fn mirror_run(void *opaque) } ret =3D 0; - trace_mirror_before_sleep(s, cnt, s->synced, delay_ns); + trace_mirror_before_sleep(s, cnt * BDRV_SECTOR_SIZE, + s->synced, delay_ns); if (!s->synced) { block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, delay_ns); if (block_job_is_cancelled(&s->common)) { diff --git a/block/stream.c b/block/stream.c index 29273a5..6cb3939 100644 --- a/block/stream.c +++ b/block/stream.c @@ -168,7 +168,8 @@ static void coroutine_fn stream_run(void *opaque) copy =3D (ret =3D=3D 1); } - trace_stream_one_iteration(s, sector_num, n, ret); + trace_stream_one_iteration(s, sector_num * BDRV_SECTOR_SIZE, + n * BDRV_SECTOR_SIZE, ret); if (copy) { ret =3D stream_populate(blk, sector_num, n, buf); } diff --git a/block/trace-events b/block/trace-events index 752de6a..4a4df25 100644 --- a/block/trace-events +++ b/block/trace-events @@ -15,11 +15,11 @@ bdrv_co_pwrite_zeroes(void *bs, int64_t offset, int cou= nt, int flags) "bs %p off bdrv_co_do_copy_on_readv(void *bs, int64_t offset, unsigned int bytes, int= 64_t cluster_offset, unsigned int cluster_bytes) "bs %p offset %"PRId64" by= tes %u cluster_offset %"PRId64" cluster_bytes %u" # block/stream.c -stream_one_iteration(void *s, int64_t sector_num, int nb_sectors, int is_a= llocated) "s %p sector_num %"PRId64" nb_sectors %d is_allocated %d" +stream_one_iteration(void *s, int64_t offset, uint64_t bytes, int is_alloc= ated) "s %p offset %" PRId64 " bytes %" PRIu64 " is_allocated %d" stream_start(void *bs, void *base, void *s) "bs %p base %p s %p" # block/commit.c -commit_one_iteration(void *s, int64_t sector_num, int nb_sectors, int is_a= llocated) "s %p sector_num %"PRId64" nb_sectors %d is_allocated %d" +commit_one_iteration(void *s, int64_t offset, uint64_t bytes, int is_alloc= ated) "s %p offset %" PRId64 " bytes %" PRIu64 " is_allocated %d" commit_start(void *bs, void *base, void *top, void *s) "bs %p base %p top = %p s %p" # block/mirror.c @@ -28,14 +28,14 @@ mirror_restart_iter(void *s, int64_t cnt) "s %p dirty c= ount %"PRId64 mirror_before_flush(void *s) "s %p" mirror_before_drain(void *s, int64_t cnt) "s %p dirty count %"PRId64 mirror_before_sleep(void *s, int64_t cnt, int synced, uint64_t delay_ns) "= s %p dirty count %"PRId64" synced %d delay %"PRIu64"ns" -mirror_one_iteration(void *s, int64_t sector_num, int nb_sectors) "s %p se= ctor_num %"PRId64" nb_sectors %d" -mirror_iteration_done(void *s, int64_t sector_num, int nb_sectors, int ret= ) "s %p sector_num %"PRId64" nb_sectors %d ret %d" +mirror_one_iteration(void *s, int64_t offset, uint64_t bytes) "s %p offset= %" PRId64 " bytes %" PRIu64 +mirror_iteration_done(void *s, int64_t offset, uint64_t bytes, int ret) "s= %p offset %" PRId64 " bytes %" PRIu64 " ret %d" mirror_yield(void *s, int64_t cnt, int buf_free_count, int in_flight) "s %= p dirty count %"PRId64" free buffers %d in_flight %d" -mirror_yield_in_flight(void *s, int64_t sector_num, int in_flight) "s %p s= ector_num %"PRId64" in_flight %d" +mirror_yield_in_flight(void *s, int64_t offset, int in_flight) "s %p offse= t %" PRId64 " in_flight %d" # block/backup.c -backup_do_cow_enter(void *job, int64_t start, int64_t sector_num, int nb_s= ectors) "job %p start %"PRId64" sector_num %"PRId64" nb_sectors %d" -backup_do_cow_return(void *job, int64_t sector_num, int nb_sectors, int re= t) "job %p sector_num %"PRId64" nb_sectors %d ret %d" +backup_do_cow_enter(void *job, int64_t start, int64_t offset, uint64_t byt= es) "job %p start %" PRId64 " offset %" PRId64 " bytes %" PRIu64 +backup_do_cow_return(void *job, int64_t offset, uint64_t bytes, int ret) "= job %p offset %" PRId64 " bytes %" PRIu64 " ret %d" backup_do_cow_skip(void *job, int64_t start) "job %p start %"PRId64 backup_do_cow_process(void *job, int64_t start) "job %p start %"PRId64 backup_do_cow_read_fail(void *job, int64_t start, int ret) "job %p start %= "PRId64" ret %d" --=20 2.9.4 From nobody Sun Oct 5 19:15:27 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1499289244329119.26405743858425; Wed, 5 Jul 2017 14:14:04 -0700 (PDT) Received: from localhost ([::1]:48074 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSrcY-0000Uy-3U for importer@patchew.org; Wed, 05 Jul 2017 17:14:02 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37780) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSrXj-0004nv-K8 for qemu-devel@nongnu.org; Wed, 05 Jul 2017 17:09:07 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dSrXi-0002cY-Hu for qemu-devel@nongnu.org; Wed, 05 Jul 2017 17:09:03 -0400 Received: from mx1.redhat.com ([209.132.183.28]:47392) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dSrXf-0002Zs-CU; Wed, 05 Jul 2017 17:08:59 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 66E8E80F9D; Wed, 5 Jul 2017 21:08:58 +0000 (UTC) Received: from red.redhat.com (ovpn-120-36.rdu2.redhat.com [10.10.120.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4B3D95C7A9; Wed, 5 Jul 2017 21:08:57 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 66E8E80F9D Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=eblake@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 66E8E80F9D From: Eric Blake To: qemu-devel@nongnu.org Date: Wed, 5 Jul 2017 16:08:24 -0500 Message-Id: <20170705210842.960-4-eblake@redhat.com> In-Reply-To: <20170705210842.960-1-eblake@redhat.com> References: <20170705210842.960-1-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Wed, 05 Jul 2017 21:08:58 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v4 03/21] stream: Switch stream_populate() to byte-based X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, jcody@redhat.com, jsnow@redhat.com, qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" We are gradually converting to byte-based interfaces, as they are easier to reason about than sector-based. Start by converting an internal function (no semantic change). Signed-off-by: Eric Blake Reviewed-by: John Snow Reviewed-by: Jeff Cody Reviewed-by: Kevin Wolf --- v2: no change --- block/stream.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/block/stream.c b/block/stream.c index 6cb3939..746d525 100644 --- a/block/stream.c +++ b/block/stream.c @@ -41,20 +41,20 @@ typedef struct StreamBlockJob { } StreamBlockJob; static int coroutine_fn stream_populate(BlockBackend *blk, - int64_t sector_num, int nb_sectors, + int64_t offset, uint64_t bytes, void *buf) { struct iovec iov =3D { .iov_base =3D buf, - .iov_len =3D nb_sectors * BDRV_SECTOR_SIZE, + .iov_len =3D bytes, }; QEMUIOVector qiov; + assert(bytes < SIZE_MAX); qemu_iovec_init_external(&qiov, &iov, 1); /* Copy-on-read the unallocated clusters */ - return blk_co_preadv(blk, sector_num * BDRV_SECTOR_SIZE, qiov.size, &q= iov, - BDRV_REQ_COPY_ON_READ); + return blk_co_preadv(blk, offset, qiov.size, &qiov, BDRV_REQ_COPY_ON_R= EAD); } typedef struct { @@ -171,7 +171,8 @@ static void coroutine_fn stream_run(void *opaque) trace_stream_one_iteration(s, sector_num * BDRV_SECTOR_SIZE, n * BDRV_SECTOR_SIZE, ret); if (copy) { - ret =3D stream_populate(blk, sector_num, n, buf); + ret =3D stream_populate(blk, sector_num * BDRV_SECTOR_SIZE, + n * BDRV_SECTOR_SIZE, buf); } if (ret < 0) { BlockErrorAction action =3D --=20 2.9.4 From nobody Sun Oct 5 19:15:27 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1499289083310613.6261517264228; Wed, 5 Jul 2017 14:11:23 -0700 (PDT) Received: from localhost ([::1]:48062 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSrZw-0006Mh-Vj for importer@patchew.org; Wed, 05 Jul 2017 17:11:21 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37818) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSrXl-0004ol-2E for qemu-devel@nongnu.org; Wed, 05 Jul 2017 17:09:07 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dSrXi-0002co-RO for qemu-devel@nongnu.org; Wed, 05 Jul 2017 17:09:05 -0400 Received: from mx1.redhat.com ([209.132.183.28]:33158) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dSrXg-0002as-Qk; Wed, 05 Jul 2017 17:09:00 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id DFDB6C0587E6; Wed, 5 Jul 2017 21:08:59 +0000 (UTC) Received: from red.redhat.com (ovpn-120-36.rdu2.redhat.com [10.10.120.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9B62E600CC; Wed, 5 Jul 2017 21:08:58 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com DFDB6C0587E6 Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=eblake@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com DFDB6C0587E6 From: Eric Blake To: qemu-devel@nongnu.org Date: Wed, 5 Jul 2017 16:08:25 -0500 Message-Id: <20170705210842.960-5-eblake@redhat.com> In-Reply-To: <20170705210842.960-1-eblake@redhat.com> References: <20170705210842.960-1-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Wed, 05 Jul 2017 21:09:00 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v4 04/21] stream: Drop reached_end for stream_complete() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, jcody@redhat.com, jsnow@redhat.com, qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" stream_complete() skips the work of rewriting the backing file if the job was cancelled, if data->reached_end is false, or if there was an error detected (non-zero data->ret) during the streaming. But note that in stream_run(), data->reached_end is only set if the loop ran to completion, and data->ret is only 0 in two cases: either the loop ran to completion (possibly by cancellation, but stream_complete checks for that), or we took an early goto out because there is no bs->backing. Thus, we can preserve the same semantics without the use of reached_end, by merely checking for bs->backing (and logically, if there was no backing file, streaming is a no-op, so there is no backing file to rewrite). Suggested-by: Kevin Wolf Signed-off-by: Eric Blake Reviewed-by: John Snow Reviewed-by: Kevin Wolf --- v4: new patch --- block/stream.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/block/stream.c b/block/stream.c index 746d525..12f1659 100644 --- a/block/stream.c +++ b/block/stream.c @@ -59,7 +59,6 @@ static int coroutine_fn stream_populate(BlockBackend *blk, typedef struct { int ret; - bool reached_end; } StreamCompleteData; static void stream_complete(BlockJob *job, void *opaque) @@ -70,7 +69,7 @@ static void stream_complete(BlockJob *job, void *opaque) BlockDriverState *base =3D s->base; Error *local_err =3D NULL; - if (!block_job_is_cancelled(&s->common) && data->reached_end && + if (!block_job_is_cancelled(&s->common) && bs->backing && data->ret =3D=3D 0) { const char *base_id =3D NULL, *base_fmt =3D NULL; if (base) { @@ -211,7 +210,6 @@ out: /* Modify backing chain and close BDSes in main loop */ data =3D g_malloc(sizeof(*data)); data->ret =3D ret; - data->reached_end =3D sector_num =3D=3D end; block_job_defer_to_main_loop(&s->common, stream_complete, data); } --=20 2.9.4 From nobody Sun Oct 5 19:15:27 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1499289382803275.58468296905005; Wed, 5 Jul 2017 14:16:22 -0700 (PDT) Received: from localhost ([::1]:48091 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSren-0002kj-5Q for importer@patchew.org; Wed, 05 Jul 2017 17:16:21 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37898) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSrXs-0004v1-D3 for qemu-devel@nongnu.org; Wed, 05 Jul 2017 17:09:13 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dSrXn-0002ez-8g for qemu-devel@nongnu.org; Wed, 05 Jul 2017 17:09:12 -0400 Received: from mx1.redhat.com ([209.132.183.28]:34918) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dSrXi-0002bg-5z; Wed, 05 Jul 2017 17:09:02 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 410A2C04D28B; Wed, 5 Jul 2017 21:09:01 +0000 (UTC) Received: from red.redhat.com (ovpn-120-36.rdu2.redhat.com [10.10.120.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id 219AE600CC; Wed, 5 Jul 2017 21:09:00 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 410A2C04D28B Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=eblake@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 410A2C04D28B From: Eric Blake To: qemu-devel@nongnu.org Date: Wed, 5 Jul 2017 16:08:26 -0500 Message-Id: <20170705210842.960-6-eblake@redhat.com> In-Reply-To: <20170705210842.960-1-eblake@redhat.com> References: <20170705210842.960-1-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Wed, 05 Jul 2017 21:09:01 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v4 05/21] stream: Switch stream_run() to byte-based X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, jcody@redhat.com, jsnow@redhat.com, qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" We are gradually converting to byte-based interfaces, as they are easier to reason about than sector-based. Change the internal loop iteration of streaming to track by bytes instead of sectors (although we are still guaranteed that we iterate by steps that are sector-aligned). Signed-off-by: Eric Blake Reviewed-by: John Snow Reviewed-by: Jeff Cody Reviewed-by: Kevin Wolf --- v4: avoid reached_end change by rebasing to earlier changes [Kevin], R-b ke= pt v2-v3: no change --- block/stream.c | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/block/stream.c b/block/stream.c index 12f1659..e3dd2ac 100644 --- a/block/stream.c +++ b/block/stream.c @@ -107,12 +107,11 @@ static void coroutine_fn stream_run(void *opaque) BlockBackend *blk =3D s->common.blk; BlockDriverState *bs =3D blk_bs(blk); BlockDriverState *base =3D s->base; - int64_t sector_num =3D 0; - int64_t end =3D -1; + int64_t offset =3D 0; uint64_t delay_ns =3D 0; int error =3D 0; int ret =3D 0; - int n =3D 0; + int n =3D 0; /* sectors */ void *buf; if (!bs->backing) { @@ -125,7 +124,6 @@ static void coroutine_fn stream_run(void *opaque) goto out; } - end =3D s->common.len >> BDRV_SECTOR_BITS; buf =3D qemu_blockalign(bs, STREAM_BUFFER_SIZE); /* Turn on copy-on-read for the whole block device so that guest read @@ -137,7 +135,7 @@ static void coroutine_fn stream_run(void *opaque) bdrv_enable_copy_on_read(bs); } - for (sector_num =3D 0; sector_num < end; sector_num +=3D n) { + for ( ; offset < s->common.len; offset +=3D n * BDRV_SECTOR_SIZE) { bool copy; /* Note that even when no rate limit is applied we need to yield @@ -150,28 +148,26 @@ static void coroutine_fn stream_run(void *opaque) copy =3D false; - ret =3D bdrv_is_allocated(bs, sector_num, + ret =3D bdrv_is_allocated(bs, offset / BDRV_SECTOR_SIZE, STREAM_BUFFER_SIZE / BDRV_SECTOR_SIZE, &n); if (ret =3D=3D 1) { /* Allocated in the top, no need to copy. */ } else if (ret >=3D 0) { /* Copy if allocated in the intermediate images. Limit to the - * known-unallocated area [sector_num, sector_num+n). */ + * known-unallocated area [offset, offset+n*BDRV_SECTOR_SIZE).= */ ret =3D bdrv_is_allocated_above(backing_bs(bs), base, - sector_num, n, &n); + offset / BDRV_SECTOR_SIZE, n, &n= ); /* Finish early if end of backing file has been reached */ if (ret =3D=3D 0 && n =3D=3D 0) { - n =3D end - sector_num; + n =3D (s->common.len - offset) / BDRV_SECTOR_SIZE; } copy =3D (ret =3D=3D 1); } - trace_stream_one_iteration(s, sector_num * BDRV_SECTOR_SIZE, - n * BDRV_SECTOR_SIZE, ret); + trace_stream_one_iteration(s, offset, n * BDRV_SECTOR_SIZE, ret); if (copy) { - ret =3D stream_populate(blk, sector_num * BDRV_SECTOR_SIZE, - n * BDRV_SECTOR_SIZE, buf); + ret =3D stream_populate(blk, offset, n * BDRV_SECTOR_SIZE, buf= ); } if (ret < 0) { BlockErrorAction action =3D --=20 2.9.4 From nobody Sun Oct 5 19:15:27 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1499289237091776.3210656134155; Wed, 5 Jul 2017 14:13:57 -0700 (PDT) Received: from localhost ([::1]:48073 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSrcQ-00007f-QN for importer@patchew.org; Wed, 05 Jul 2017 17:13:54 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37896) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSrXs-0004uw-Cw for qemu-devel@nongnu.org; Wed, 05 Jul 2017 17:09:13 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dSrXn-0002fH-IU for qemu-devel@nongnu.org; Wed, 05 Jul 2017 17:09:12 -0400 Received: from mx1.redhat.com ([209.132.183.28]:35852) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dSrXj-0002cg-CM; Wed, 05 Jul 2017 17:09:03 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 666A45D689; Wed, 5 Jul 2017 21:09:02 +0000 (UTC) Received: from red.redhat.com (ovpn-120-36.rdu2.redhat.com [10.10.120.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id 76F9B600CC; Wed, 5 Jul 2017 21:09:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 666A45D689 Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=eblake@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 666A45D689 From: Eric Blake To: qemu-devel@nongnu.org Date: Wed, 5 Jul 2017 16:08:27 -0500 Message-Id: <20170705210842.960-7-eblake@redhat.com> In-Reply-To: <20170705210842.960-1-eblake@redhat.com> References: <20170705210842.960-1-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Wed, 05 Jul 2017 21:09:02 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v4 06/21] commit: Switch commit_populate() to byte-based X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, jcody@redhat.com, jsnow@redhat.com, qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" We are gradually converting to byte-based interfaces, as they are easier to reason about than sector-based. Start by converting an internal function (no semantic change). Signed-off-by: Eric Blake Reviewed-by: John Snow Reviewed-by: Jeff Cody Reviewed-by: Kevin Wolf --- v2: no change --- block/commit.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/block/commit.c b/block/commit.c index 4cda7f2..6f67d78 100644 --- a/block/commit.c +++ b/block/commit.c @@ -47,26 +47,25 @@ typedef struct CommitBlockJob { } CommitBlockJob; static int coroutine_fn commit_populate(BlockBackend *bs, BlockBackend *ba= se, - int64_t sector_num, int nb_sectors, + int64_t offset, uint64_t bytes, void *buf) { int ret =3D 0; QEMUIOVector qiov; struct iovec iov =3D { .iov_base =3D buf, - .iov_len =3D nb_sectors * BDRV_SECTOR_SIZE, + .iov_len =3D bytes, }; + assert(bytes < SIZE_MAX); qemu_iovec_init_external(&qiov, &iov, 1); - ret =3D blk_co_preadv(bs, sector_num * BDRV_SECTOR_SIZE, - qiov.size, &qiov, 0); + ret =3D blk_co_preadv(bs, offset, qiov.size, &qiov, 0); if (ret < 0) { return ret; } - ret =3D blk_co_pwritev(base, sector_num * BDRV_SECTOR_SIZE, - qiov.size, &qiov, 0); + ret =3D blk_co_pwritev(base, offset, qiov.size, &qiov, 0); if (ret < 0) { return ret; } @@ -193,7 +192,9 @@ static void coroutine_fn commit_run(void *opaque) trace_commit_one_iteration(s, sector_num * BDRV_SECTOR_SIZE, n * BDRV_SECTOR_SIZE, ret); if (copy) { - ret =3D commit_populate(s->top, s->base, sector_num, n, buf); + ret =3D commit_populate(s->top, s->base, + sector_num * BDRV_SECTOR_SIZE, + n * BDRV_SECTOR_SIZE, buf); bytes_written +=3D n * BDRV_SECTOR_SIZE; } if (ret < 0) { --=20 2.9.4 From nobody Sun Oct 5 19:15:27 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1499289093460453.2827981815544; Wed, 5 Jul 2017 14:11:33 -0700 (PDT) Received: from localhost ([::1]:48063 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSra8-0006WM-7X for importer@patchew.org; Wed, 05 Jul 2017 17:11:32 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37897) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSrXs-0004v0-D2 for qemu-devel@nongnu.org; Wed, 05 Jul 2017 17:09:13 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dSrXn-0002f8-HV for qemu-devel@nongnu.org; Wed, 05 Jul 2017 17:09:12 -0400 Received: from mx1.redhat.com ([209.132.183.28]:47524) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dSrXk-0002db-LX; Wed, 05 Jul 2017 17:09:04 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B56077EBD6; Wed, 5 Jul 2017 21:09:03 +0000 (UTC) Received: from red.redhat.com (ovpn-120-36.rdu2.redhat.com [10.10.120.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9BEB2600CC; Wed, 5 Jul 2017 21:09:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com B56077EBD6 Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=eblake@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com B56077EBD6 From: Eric Blake To: qemu-devel@nongnu.org Date: Wed, 5 Jul 2017 16:08:28 -0500 Message-Id: <20170705210842.960-8-eblake@redhat.com> In-Reply-To: <20170705210842.960-1-eblake@redhat.com> References: <20170705210842.960-1-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Wed, 05 Jul 2017 21:09:03 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v4 07/21] commit: Switch commit_run() to byte-based X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, jcody@redhat.com, jsnow@redhat.com, qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" We are gradually converting to byte-based interfaces, as they are easier to reason about than sector-based. Change the internal loop iteration of committing to track by bytes instead of sectors (although we are still guaranteed that we iterate by steps that are sector-aligned). Signed-off-by: Eric Blake Reviewed-by: John Snow Reviewed-by: Jeff Cody Reviewed-by: Kevin Wolf --- v2: no change --- block/commit.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/block/commit.c b/block/commit.c index 6f67d78..c3a7bca 100644 --- a/block/commit.c +++ b/block/commit.c @@ -143,17 +143,16 @@ static void coroutine_fn commit_run(void *opaque) { CommitBlockJob *s =3D opaque; CommitCompleteData *data; - int64_t sector_num, end; + int64_t offset; uint64_t delay_ns =3D 0; int ret =3D 0; - int n =3D 0; + int n =3D 0; /* sectors */ void *buf =3D NULL; int bytes_written =3D 0; int64_t base_len; ret =3D s->common.len =3D blk_getlength(s->top); - if (s->common.len < 0) { goto out; } @@ -170,10 +169,9 @@ static void coroutine_fn commit_run(void *opaque) } } - end =3D s->common.len >> BDRV_SECTOR_BITS; buf =3D blk_blockalign(s->top, COMMIT_BUFFER_SIZE); - for (sector_num =3D 0; sector_num < end; sector_num +=3D n) { + for (offset =3D 0; offset < s->common.len; offset +=3D n * BDRV_SECTOR= _SIZE) { bool copy; /* Note that even when no rate limit is applied we need to yield @@ -185,15 +183,13 @@ static void coroutine_fn commit_run(void *opaque) } /* Copy if allocated above the base */ ret =3D bdrv_is_allocated_above(blk_bs(s->top), blk_bs(s->base), - sector_num, + offset / BDRV_SECTOR_SIZE, COMMIT_BUFFER_SIZE / BDRV_SECTOR_SIZ= E, &n); copy =3D (ret =3D=3D 1); - trace_commit_one_iteration(s, sector_num * BDRV_SECTOR_SIZE, - n * BDRV_SECTOR_SIZE, ret); + trace_commit_one_iteration(s, offset, n * BDRV_SECTOR_SIZE, ret); if (copy) { - ret =3D commit_populate(s->top, s->base, - sector_num * BDRV_SECTOR_SIZE, + ret =3D commit_populate(s->top, s->base, offset, n * BDRV_SECTOR_SIZE, buf); bytes_written +=3D n * BDRV_SECTOR_SIZE; } --=20 2.9.4 From nobody Sun Oct 5 19:15:27 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1499289538919434.53360847747024; Wed, 5 Jul 2017 14:18:58 -0700 (PDT) Received: from localhost ([::1]:48105 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSrhJ-0004v6-FK for importer@patchew.org; Wed, 05 Jul 2017 17:18:57 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37960) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSrXu-0004xG-CV for qemu-devel@nongnu.org; Wed, 05 Jul 2017 17:09:17 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dSrXs-0002i1-F6 for qemu-devel@nongnu.org; Wed, 05 Jul 2017 17:09:14 -0400 Received: from mx1.redhat.com ([209.132.183.28]:35012) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dSrXm-0002eI-1S; Wed, 05 Jul 2017 17:09:06 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 1AC5FC04D28D; Wed, 5 Jul 2017 21:09:05 +0000 (UTC) Received: from red.redhat.com (ovpn-120-36.rdu2.redhat.com [10.10.120.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id EB172600CC; Wed, 5 Jul 2017 21:09:03 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 1AC5FC04D28D Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=eblake@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 1AC5FC04D28D From: Eric Blake To: qemu-devel@nongnu.org Date: Wed, 5 Jul 2017 16:08:29 -0500 Message-Id: <20170705210842.960-9-eblake@redhat.com> In-Reply-To: <20170705210842.960-1-eblake@redhat.com> References: <20170705210842.960-1-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Wed, 05 Jul 2017 21:09:05 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v4 08/21] mirror: Switch MirrorBlockJob to byte-based X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, jcody@redhat.com, jsnow@redhat.com, qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" We are gradually converting to byte-based interfaces, as they are easier to reason about than sector-based. Continue by converting an internal structure (no semantic change), and all references to the buffer size. Add an assertion that our use of s->granularity >> BDRV_SECTOR_BITS (necessary for interaction with sector-based dirty bitmaps, until a later patch converts those to be byte-based) does not suffer from truncation problems. [checkpatch has a false positive on use of MIN() in this patch] Signed-off-by: Eric Blake Reviewed-by: John Snow Reviewed-by: Kevin Wolf --- v4: add assertion and formatting tweak [Kevin], R-b dropped v2-v3: no change --- block/mirror.c | 82 +++++++++++++++++++++++++++++-------------------------= ---- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/block/mirror.c b/block/mirror.c index b4dfe95..9aca0cb 100644 --- a/block/mirror.c +++ b/block/mirror.c @@ -24,9 +24,8 @@ #define SLICE_TIME 100000000ULL /* ns */ #define MAX_IN_FLIGHT 16 -#define MAX_IO_SECTORS ((1 << 20) >> BDRV_SECTOR_BITS) /* 1 Mb */ -#define DEFAULT_MIRROR_BUF_SIZE \ - (MAX_IN_FLIGHT * MAX_IO_SECTORS * BDRV_SECTOR_SIZE) +#define MAX_IO_BYTES (1 << 20) /* 1 Mb */ +#define DEFAULT_MIRROR_BUF_SIZE (MAX_IN_FLIGHT * MAX_IO_BYTES) /* The mirroring buffer is a list of granularity-sized chunks. * Free chunks are organized in a list. @@ -67,11 +66,11 @@ typedef struct MirrorBlockJob { uint64_t last_pause_ns; unsigned long *in_flight_bitmap; int in_flight; - int64_t sectors_in_flight; + int64_t bytes_in_flight; int ret; bool unmap; bool waiting_for_io; - int target_cluster_sectors; + int target_cluster_size; int max_iov; bool initial_zeroing_ongoing; } MirrorBlockJob; @@ -79,8 +78,8 @@ typedef struct MirrorBlockJob { typedef struct MirrorOp { MirrorBlockJob *s; QEMUIOVector qiov; - int64_t sector_num; - int nb_sectors; + int64_t offset; + uint64_t bytes; } MirrorOp; static BlockErrorAction mirror_error_action(MirrorBlockJob *s, bool read, @@ -101,13 +100,12 @@ static void mirror_iteration_done(MirrorOp *op, int r= et) MirrorBlockJob *s =3D op->s; struct iovec *iov; int64_t chunk_num; - int i, nb_chunks, sectors_per_chunk; + int i, nb_chunks; - trace_mirror_iteration_done(s, op->sector_num * BDRV_SECTOR_SIZE, - op->nb_sectors * BDRV_SECTOR_SIZE, ret); + trace_mirror_iteration_done(s, op->offset, op->bytes, ret); s->in_flight--; - s->sectors_in_flight -=3D op->nb_sectors; + s->bytes_in_flight -=3D op->bytes; iov =3D op->qiov.iov; for (i =3D 0; i < op->qiov.niov; i++) { MirrorBuffer *buf =3D (MirrorBuffer *) iov[i].iov_base; @@ -115,16 +113,15 @@ static void mirror_iteration_done(MirrorOp *op, int r= et) s->buf_free_count++; } - sectors_per_chunk =3D s->granularity >> BDRV_SECTOR_BITS; - chunk_num =3D op->sector_num / sectors_per_chunk; - nb_chunks =3D DIV_ROUND_UP(op->nb_sectors, sectors_per_chunk); + chunk_num =3D op->offset / s->granularity; + nb_chunks =3D DIV_ROUND_UP(op->bytes, s->granularity); bitmap_clear(s->in_flight_bitmap, chunk_num, nb_chunks); if (ret >=3D 0) { if (s->cow_bitmap) { bitmap_set(s->cow_bitmap, chunk_num, nb_chunks); } if (!s->initial_zeroing_ongoing) { - s->common.offset +=3D (uint64_t)op->nb_sectors * BDRV_SECTOR_S= IZE; + s->common.offset +=3D op->bytes; } } qemu_iovec_destroy(&op->qiov); @@ -144,7 +141,8 @@ static void mirror_write_complete(void *opaque, int ret) if (ret < 0) { BlockErrorAction action; - bdrv_set_dirty_bitmap(s->dirty_bitmap, op->sector_num, op->nb_sect= ors); + bdrv_set_dirty_bitmap(s->dirty_bitmap, op->offset >> BDRV_SECTOR_B= ITS, + op->bytes >> BDRV_SECTOR_BITS); action =3D mirror_error_action(s, false, -ret); if (action =3D=3D BLOCK_ERROR_ACTION_REPORT && s->ret >=3D 0) { s->ret =3D ret; @@ -163,7 +161,8 @@ static void mirror_read_complete(void *opaque, int ret) if (ret < 0) { BlockErrorAction action; - bdrv_set_dirty_bitmap(s->dirty_bitmap, op->sector_num, op->nb_sect= ors); + bdrv_set_dirty_bitmap(s->dirty_bitmap, op->offset >> BDRV_SECTOR_B= ITS, + op->bytes >> BDRV_SECTOR_BITS); action =3D mirror_error_action(s, true, -ret); if (action =3D=3D BLOCK_ERROR_ACTION_REPORT && s->ret >=3D 0) { s->ret =3D ret; @@ -171,7 +170,7 @@ static void mirror_read_complete(void *opaque, int ret) mirror_iteration_done(op, ret); } else { - blk_aio_pwritev(s->target, op->sector_num * BDRV_SECTOR_SIZE, &op-= >qiov, + blk_aio_pwritev(s->target, op->offset, &op->qiov, 0, mirror_write_complete, op); } aio_context_release(blk_get_aio_context(s->common.blk)); @@ -211,7 +210,8 @@ static int mirror_cow_align(MirrorBlockJob *s, align_nb_sectors =3D max_sectors; if (need_cow) { align_nb_sectors =3D QEMU_ALIGN_DOWN(align_nb_sectors, - s->target_cluster_sectors); + s->target_cluster_size >> + BDRV_SECTOR_BITS); } } /* Clipping may result in align_nb_sectors unaligned to chunk boundary= , but @@ -277,8 +277,8 @@ static int mirror_do_read(MirrorBlockJob *s, int64_t se= ctor_num, /* Allocate a MirrorOp that is used as an AIO callback. */ op =3D g_new(MirrorOp, 1); op->s =3D s; - op->sector_num =3D sector_num; - op->nb_sectors =3D nb_sectors; + op->offset =3D sector_num * BDRV_SECTOR_SIZE; + op->bytes =3D nb_sectors * BDRV_SECTOR_SIZE; /* Now make a QEMUIOVector taking enough granularity-sized chunks * from s->buf_free. @@ -295,7 +295,7 @@ static int mirror_do_read(MirrorBlockJob *s, int64_t se= ctor_num, /* Copy the dirty cluster. */ s->in_flight++; - s->sectors_in_flight +=3D nb_sectors; + s->bytes_in_flight +=3D nb_sectors * BDRV_SECTOR_SIZE; trace_mirror_one_iteration(s, sector_num * BDRV_SECTOR_SIZE, nb_sectors * BDRV_SECTOR_SIZE); @@ -315,19 +315,17 @@ static void mirror_do_zero_or_discard(MirrorBlockJob = *s, * so the freeing in mirror_iteration_done is nop. */ op =3D g_new0(MirrorOp, 1); op->s =3D s; - op->sector_num =3D sector_num; - op->nb_sectors =3D nb_sectors; + op->offset =3D sector_num * BDRV_SECTOR_SIZE; + op->bytes =3D nb_sectors * BDRV_SECTOR_SIZE; s->in_flight++; - s->sectors_in_flight +=3D nb_sectors; + s->bytes_in_flight +=3D nb_sectors * BDRV_SECTOR_SIZE; if (is_discard) { blk_aio_pdiscard(s->target, sector_num << BDRV_SECTOR_BITS, - op->nb_sectors << BDRV_SECTOR_BITS, - mirror_write_complete, op); + op->bytes, mirror_write_complete, op); } else { blk_aio_pwrite_zeroes(s->target, sector_num * BDRV_SECTOR_SIZE, - op->nb_sectors * BDRV_SECTOR_SIZE, - s->unmap ? BDRV_REQ_MAY_UNMAP : 0, + op->bytes, s->unmap ? BDRV_REQ_MAY_UNMAP : 0, mirror_write_complete, op); } } @@ -342,8 +340,7 @@ static uint64_t coroutine_fn mirror_iteration(MirrorBlo= ckJob *s) int64_t end =3D s->bdev_length / BDRV_SECTOR_SIZE; int sectors_per_chunk =3D s->granularity >> BDRV_SECTOR_BITS; bool write_zeroes_ok =3D bdrv_can_write_zeroes_with_unmap(blk_bs(s->ta= rget)); - int max_io_sectors =3D MAX((s->buf_size >> BDRV_SECTOR_BITS) / MAX_IN_= FLIGHT, - MAX_IO_SECTORS); + int max_io_bytes =3D MAX(s->buf_size / MAX_IN_FLIGHT, MAX_IO_BYTES); bdrv_dirty_bitmap_lock(s->dirty_bitmap); sector_num =3D bdrv_dirty_iter_next(s->dbi); @@ -415,9 +412,10 @@ static uint64_t coroutine_fn mirror_iteration(MirrorBl= ockJob *s) nb_chunks * sectors_per_chunk, &io_sectors, &file); if (ret < 0) { - io_sectors =3D MIN(nb_chunks * sectors_per_chunk, max_io_secto= rs); + io_sectors =3D MIN(nb_chunks * sectors_per_chunk, + max_io_bytes >> BDRV_SECTOR_BITS); } else if (ret & BDRV_BLOCK_DATA) { - io_sectors =3D MIN(io_sectors, max_io_sectors); + io_sectors =3D MIN(io_sectors, max_io_bytes >> BDRV_SECTOR_BIT= S); } io_sectors -=3D io_sectors % sectors_per_chunk; @@ -719,7 +717,6 @@ static void coroutine_fn mirror_run(void *opaque) char backing_filename[2]; /* we only need 2 characters because we are = only checking for a NULL string */ int ret =3D 0; - int target_cluster_size =3D BDRV_SECTOR_SIZE; if (block_job_is_cancelled(&s->common)) { goto immediate_exit; @@ -771,14 +768,15 @@ static void coroutine_fn mirror_run(void *opaque) bdrv_get_backing_filename(target_bs, backing_filename, sizeof(backing_filename)); if (!bdrv_get_info(target_bs, &bdi) && bdi.cluster_size) { - target_cluster_size =3D bdi.cluster_size; + s->target_cluster_size =3D bdi.cluster_size; + } else { + s->target_cluster_size =3D BDRV_SECTOR_SIZE; } if (backing_filename[0] && !target_bs->backing - && s->granularity < target_cluster_size) { - s->buf_size =3D MAX(s->buf_size, target_cluster_size); + && s->granularity < s->target_cluster_size) { + s->buf_size =3D MAX(s->buf_size, s->target_cluster_size); s->cow_bitmap =3D bitmap_new(length); } - s->target_cluster_sectors =3D target_cluster_size >> BDRV_SECTOR_BITS; s->max_iov =3D MIN(bs->bl.max_iov, target_bs->bl.max_iov); s->buf =3D qemu_try_blockalign(bs, s->buf_size); @@ -814,10 +812,10 @@ static void coroutine_fn mirror_run(void *opaque) cnt =3D bdrv_get_dirty_count(s->dirty_bitmap); /* s->common.offset contains the number of bytes already processed= so * far, cnt is the number of dirty sectors remaining and - * s->sectors_in_flight is the number of sectors currently being + * s->bytes_in_flight is the number of bytes currently being * processed; together those are the current total operation lengt= h */ - s->common.len =3D s->common.offset + - (cnt + s->sectors_in_flight) * BDRV_SECTOR_SIZE; + s->common.len =3D s->common.offset + s->bytes_in_flight + + cnt * BDRV_SECTOR_SIZE; /* Note that even when no rate limit is applied we need to yield * periodically with no pending I/O so that bdrv_drain_all() retur= ns. @@ -1150,6 +1148,8 @@ static void mirror_start_job(const char *job_id, Bloc= kDriverState *bs, } assert ((granularity & (granularity - 1)) =3D=3D 0); + /* Granularity must be large enough for sector-based dirty bitmap */ + assert(granularity >=3D BDRV_SECTOR_SIZE); if (buf_size < 0) { error_setg(errp, "Invalid parameter 'buf-size'"); --=20 2.9.4 From nobody Sun Oct 5 19:15:27 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1499289074088802.9297527269583; Wed, 5 Jul 2017 14:11:14 -0700 (PDT) Received: from localhost ([::1]:48061 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSrZo-0006Hi-EW for importer@patchew.org; Wed, 05 Jul 2017 17:11:12 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37968) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSrXu-0004y0-HJ for qemu-devel@nongnu.org; Wed, 05 Jul 2017 17:09:17 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dSrXt-0002ia-EM for qemu-devel@nongnu.org; Wed, 05 Jul 2017 17:09:14 -0400 Received: from mx1.redhat.com ([209.132.183.28]:52352) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dSrXn-0002em-CG; Wed, 05 Jul 2017 17:09:07 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 63A694E4D1; Wed, 5 Jul 2017 21:09:06 +0000 (UTC) Received: from red.redhat.com (ovpn-120-36.rdu2.redhat.com [10.10.120.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id 513DA600CC; Wed, 5 Jul 2017 21:09:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 63A694E4D1 Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=eblake@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 63A694E4D1 From: Eric Blake To: qemu-devel@nongnu.org Date: Wed, 5 Jul 2017 16:08:30 -0500 Message-Id: <20170705210842.960-10-eblake@redhat.com> In-Reply-To: <20170705210842.960-1-eblake@redhat.com> References: <20170705210842.960-1-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Wed, 05 Jul 2017 21:09:06 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v4 09/21] mirror: Switch mirror_do_zero_or_discard() to byte-based X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, jcody@redhat.com, jsnow@redhat.com, qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" We are gradually converting to byte-based interfaces, as they are easier to reason about than sector-based. Convert another internal function (no semantic change). Signed-off-by: Eric Blake Reviewed-by: John Snow Reviewed-by: Jeff Cody Reviewed-by: Kevin Wolf --- v2-v4: no change --- block/mirror.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/block/mirror.c b/block/mirror.c index 9aca0cb..2736d0b 100644 --- a/block/mirror.c +++ b/block/mirror.c @@ -305,8 +305,8 @@ static int mirror_do_read(MirrorBlockJob *s, int64_t se= ctor_num, } static void mirror_do_zero_or_discard(MirrorBlockJob *s, - int64_t sector_num, - int nb_sectors, + int64_t offset, + uint64_t bytes, bool is_discard) { MirrorOp *op; @@ -315,16 +315,16 @@ static void mirror_do_zero_or_discard(MirrorBlockJob = *s, * so the freeing in mirror_iteration_done is nop. */ op =3D g_new0(MirrorOp, 1); op->s =3D s; - op->offset =3D sector_num * BDRV_SECTOR_SIZE; - op->bytes =3D nb_sectors * BDRV_SECTOR_SIZE; + op->offset =3D offset; + op->bytes =3D bytes; s->in_flight++; - s->bytes_in_flight +=3D nb_sectors * BDRV_SECTOR_SIZE; + s->bytes_in_flight +=3D bytes; if (is_discard) { - blk_aio_pdiscard(s->target, sector_num << BDRV_SECTOR_BITS, + blk_aio_pdiscard(s->target, offset, op->bytes, mirror_write_complete, op); } else { - blk_aio_pwrite_zeroes(s->target, sector_num * BDRV_SECTOR_SIZE, + blk_aio_pwrite_zeroes(s->target, offset, op->bytes, s->unmap ? BDRV_REQ_MAY_UNMAP : 0, mirror_write_complete, op); } @@ -453,7 +453,8 @@ static uint64_t coroutine_fn mirror_iteration(MirrorBlo= ckJob *s) break; case MIRROR_METHOD_ZERO: case MIRROR_METHOD_DISCARD: - mirror_do_zero_or_discard(s, sector_num, io_sectors, + mirror_do_zero_or_discard(s, sector_num * BDRV_SECTOR_SIZE, + io_sectors * BDRV_SECTOR_SIZE, mirror_method =3D=3D MIRROR_METHOD_D= ISCARD); if (write_zeroes_ok) { io_bytes_acct =3D 0; @@ -657,7 +658,8 @@ static int coroutine_fn mirror_dirty_init(MirrorBlockJo= b *s) continue; } - mirror_do_zero_or_discard(s, sector_num, nb_sectors, false); + mirror_do_zero_or_discard(s, sector_num * BDRV_SECTOR_SIZE, + nb_sectors * BDRV_SECTOR_SIZE, false= ); sector_num +=3D nb_sectors; } --=20 2.9.4 From nobody Sun Oct 5 19:15:27 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1499289230126219.29858998953603; Wed, 5 Jul 2017 14:13:50 -0700 (PDT) Received: from localhost ([::1]:48072 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSrcK-0008Lj-OJ for importer@patchew.org; Wed, 05 Jul 2017 17:13:48 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37966) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSrXu-0004xr-GG for qemu-devel@nongnu.org; Wed, 05 Jul 2017 17:09:17 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dSrXt-0002ii-Id for qemu-devel@nongnu.org; Wed, 05 Jul 2017 17:09:14 -0400 Received: from mx1.redhat.com ([209.132.183.28]:47620) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dSrXo-0002fZ-Hj; Wed, 05 Jul 2017 17:09:08 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 9A6AD7EBD6; Wed, 5 Jul 2017 21:09:07 +0000 (UTC) Received: from red.redhat.com (ovpn-120-36.rdu2.redhat.com [10.10.120.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9D7CB600CC; Wed, 5 Jul 2017 21:09:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 9A6AD7EBD6 Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=eblake@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 9A6AD7EBD6 From: Eric Blake To: qemu-devel@nongnu.org Date: Wed, 5 Jul 2017 16:08:31 -0500 Message-Id: <20170705210842.960-11-eblake@redhat.com> In-Reply-To: <20170705210842.960-1-eblake@redhat.com> References: <20170705210842.960-1-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Wed, 05 Jul 2017 21:09:07 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v4 10/21] mirror: Update signature of mirror_clip_sectors() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, jcody@redhat.com, jsnow@redhat.com, qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Rather than having a void function that modifies its input in-place as the output, change the signature to reduce a layer of indirection and return the result. Suggested-by: John Snow Signed-off-by: Eric Blake Reviewed-by: John Snow Reviewed-by: Jeff Cody Reviewed-by: Kevin Wolf --- v3-v4: no change v2: new patch --- block/mirror.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/block/mirror.c b/block/mirror.c index 2736d0b..70682b6 100644 --- a/block/mirror.c +++ b/block/mirror.c @@ -176,12 +176,12 @@ static void mirror_read_complete(void *opaque, int re= t) aio_context_release(blk_get_aio_context(s->common.blk)); } -static inline void mirror_clip_sectors(MirrorBlockJob *s, - int64_t sector_num, - int *nb_sectors) +static inline int mirror_clip_sectors(MirrorBlockJob *s, + int64_t sector_num, + int nb_sectors) { - *nb_sectors =3D MIN(*nb_sectors, - s->bdev_length / BDRV_SECTOR_SIZE - sector_num); + return MIN(nb_sectors, + s->bdev_length / BDRV_SECTOR_SIZE - sector_num); } /* Round sector_num and/or nb_sectors to target cluster if COW is needed, = and @@ -216,7 +216,8 @@ static int mirror_cow_align(MirrorBlockJob *s, } /* Clipping may result in align_nb_sectors unaligned to chunk boundary= , but * that doesn't matter because it's already the end of source image. */ - mirror_clip_sectors(s, align_sector_num, &align_nb_sectors); + align_nb_sectors =3D mirror_clip_sectors(s, align_sector_num, + align_nb_sectors); ret =3D align_sector_num + align_nb_sectors - (*sector_num + *nb_secto= rs); *sector_num =3D align_sector_num; @@ -445,7 +446,7 @@ static uint64_t coroutine_fn mirror_iteration(MirrorBlo= ckJob *s) return 0; } - mirror_clip_sectors(s, sector_num, &io_sectors); + io_sectors =3D mirror_clip_sectors(s, sector_num, io_sectors); switch (mirror_method) { case MIRROR_METHOD_COPY: io_sectors =3D mirror_do_read(s, sector_num, io_sectors); --=20 2.9.4 From nobody Sun Oct 5 19:15:27 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1499289693679233.73409004235714; Wed, 5 Jul 2017 14:21:33 -0700 (PDT) Received: from localhost ([::1]:48122 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSrjn-0007Zp-Au for importer@patchew.org; Wed, 05 Jul 2017 17:21:31 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37980) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSrXu-0004yz-W3 for qemu-devel@nongnu.org; Wed, 05 Jul 2017 17:09:17 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dSrXt-0002iq-SZ for qemu-devel@nongnu.org; Wed, 05 Jul 2017 17:09:14 -0400 Received: from mx1.redhat.com ([209.132.183.28]:33332) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dSrXq-0002gQ-1B; Wed, 05 Jul 2017 17:09:10 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 180C7C0587D8; Wed, 5 Jul 2017 21:09:09 +0000 (UTC) Received: from red.redhat.com (ovpn-120-36.rdu2.redhat.com [10.10.120.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id D0326600CC; Wed, 5 Jul 2017 21:09:07 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 180C7C0587D8 Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=eblake@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 180C7C0587D8 From: Eric Blake To: qemu-devel@nongnu.org Date: Wed, 5 Jul 2017 16:08:32 -0500 Message-Id: <20170705210842.960-12-eblake@redhat.com> In-Reply-To: <20170705210842.960-1-eblake@redhat.com> References: <20170705210842.960-1-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Wed, 05 Jul 2017 21:09:09 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v4 11/21] mirror: Switch mirror_cow_align() to byte-based X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, jcody@redhat.com, jsnow@redhat.com, qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" We are gradually converting to byte-based interfaces, as they are easier to reason about than sector-based. Convert another internal function (no semantic change), and add mirror_clip_bytes() as a counterpart to mirror_clip_sectors(). Some of the conversion is a bit tricky, requiring temporaries to convert between units; it will be cleared up in a following patch. Signed-off-by: Eric Blake Reviewed-by: John Snow Reviewed-by: Jeff Cody Reviewed-by: Kevin Wolf --- v3-v4: no change v2: tweak mirror_clip_bytes() signature to match previous patch --- block/mirror.c | 64 ++++++++++++++++++++++++++++++++++--------------------= ---- 1 file changed, 38 insertions(+), 26 deletions(-) diff --git a/block/mirror.c b/block/mirror.c index 70682b6..374cefd 100644 --- a/block/mirror.c +++ b/block/mirror.c @@ -176,6 +176,15 @@ static void mirror_read_complete(void *opaque, int ret) aio_context_release(blk_get_aio_context(s->common.blk)); } +/* Clip bytes relative to offset to not exceed end-of-file */ +static inline int64_t mirror_clip_bytes(MirrorBlockJob *s, + int64_t offset, + int64_t bytes) +{ + return MIN(bytes, s->bdev_length - offset); +} + +/* Clip nb_sectors relative to sector_num to not exceed end-of-file */ static inline int mirror_clip_sectors(MirrorBlockJob *s, int64_t sector_num, int nb_sectors) @@ -184,44 +193,39 @@ static inline int mirror_clip_sectors(MirrorBlockJob = *s, s->bdev_length / BDRV_SECTOR_SIZE - sector_num); } -/* Round sector_num and/or nb_sectors to target cluster if COW is needed, = and - * return the offset of the adjusted tail sector against original. */ -static int mirror_cow_align(MirrorBlockJob *s, - int64_t *sector_num, - int *nb_sectors) +/* Round offset and/or bytes to target cluster if COW is needed, and + * return the offset of the adjusted tail against original. */ +static int mirror_cow_align(MirrorBlockJob *s, int64_t *offset, + unsigned int *bytes) { bool need_cow; int ret =3D 0; - int chunk_sectors =3D s->granularity >> BDRV_SECTOR_BITS; - int64_t align_sector_num =3D *sector_num; - int align_nb_sectors =3D *nb_sectors; - int max_sectors =3D chunk_sectors * s->max_iov; + int64_t align_offset =3D *offset; + unsigned int align_bytes =3D *bytes; + int max_bytes =3D s->granularity * s->max_iov; - need_cow =3D !test_bit(*sector_num / chunk_sectors, s->cow_bitmap); - need_cow |=3D !test_bit((*sector_num + *nb_sectors - 1) / chunk_sector= s, + need_cow =3D !test_bit(*offset / s->granularity, s->cow_bitmap); + need_cow |=3D !test_bit((*offset + *bytes - 1) / s->granularity, s->cow_bitmap); if (need_cow) { - bdrv_round_sectors_to_clusters(blk_bs(s->target), *sector_num, - *nb_sectors, &align_sector_num, - &align_nb_sectors); + bdrv_round_to_clusters(blk_bs(s->target), *offset, *bytes, + &align_offset, &align_bytes); } - if (align_nb_sectors > max_sectors) { - align_nb_sectors =3D max_sectors; + if (align_bytes > max_bytes) { + align_bytes =3D max_bytes; if (need_cow) { - align_nb_sectors =3D QEMU_ALIGN_DOWN(align_nb_sectors, - s->target_cluster_size >> - BDRV_SECTOR_BITS); + align_bytes =3D QEMU_ALIGN_DOWN(align_bytes, + s->target_cluster_size); } } - /* Clipping may result in align_nb_sectors unaligned to chunk boundary= , but + /* Clipping may result in align_bytes unaligned to chunk boundary, but * that doesn't matter because it's already the end of source image. */ - align_nb_sectors =3D mirror_clip_sectors(s, align_sector_num, - align_nb_sectors); + align_bytes =3D mirror_clip_bytes(s, align_offset, align_bytes); - ret =3D align_sector_num + align_nb_sectors - (*sector_num + *nb_secto= rs); - *sector_num =3D align_sector_num; - *nb_sectors =3D align_nb_sectors; + ret =3D align_offset + align_bytes - (*offset + *bytes); + *offset =3D align_offset; + *bytes =3D align_bytes; assert(ret >=3D 0); return ret; } @@ -257,10 +261,18 @@ static int mirror_do_read(MirrorBlockJob *s, int64_t = sector_num, nb_sectors =3D MIN(s->buf_size >> BDRV_SECTOR_BITS, nb_sectors); nb_sectors =3D MIN(max_sectors, nb_sectors); assert(nb_sectors); + assert(nb_sectors < BDRV_REQUEST_MAX_SECTORS); ret =3D nb_sectors; if (s->cow_bitmap) { - ret +=3D mirror_cow_align(s, §or_num, &nb_sectors); + int64_t offset =3D sector_num * BDRV_SECTOR_SIZE; + unsigned int bytes =3D nb_sectors * BDRV_SECTOR_SIZE; + int gap; + + gap =3D mirror_cow_align(s, &offset, &bytes); + sector_num =3D offset / BDRV_SECTOR_SIZE; + nb_sectors =3D bytes / BDRV_SECTOR_SIZE; + ret +=3D gap / BDRV_SECTOR_SIZE; } assert(nb_sectors << BDRV_SECTOR_BITS <=3D s->buf_size); /* The sector range must meet granularity because: --=20 2.9.4 From nobody Sun Oct 5 19:15:27 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1499289564368999.2256101058742; Wed, 5 Jul 2017 14:19:24 -0700 (PDT) Received: from localhost ([::1]:48108 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSrhj-0005kd-07 for importer@patchew.org; Wed, 05 Jul 2017 17:19:23 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38018) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSrXy-00051J-Nn for qemu-devel@nongnu.org; Wed, 05 Jul 2017 17:09:23 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dSrXx-0002kf-BV for qemu-devel@nongnu.org; Wed, 05 Jul 2017 17:09:18 -0400 Received: from mx1.redhat.com ([209.132.183.28]:52494) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dSrXt-0002he-0E; Wed, 05 Jul 2017 17:09:13 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 0E7224E4D1; Wed, 5 Jul 2017 21:09:12 +0000 (UTC) Received: from red.redhat.com (ovpn-120-36.rdu2.redhat.com [10.10.120.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4FC0E600CC; Wed, 5 Jul 2017 21:09:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 0E7224E4D1 Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=eblake@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 0E7224E4D1 From: Eric Blake To: qemu-devel@nongnu.org Date: Wed, 5 Jul 2017 16:08:33 -0500 Message-Id: <20170705210842.960-13-eblake@redhat.com> In-Reply-To: <20170705210842.960-1-eblake@redhat.com> References: <20170705210842.960-1-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Wed, 05 Jul 2017 21:09:12 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v4 12/21] mirror: Switch mirror_do_read() to byte-based X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, jcody@redhat.com, jsnow@redhat.com, qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" We are gradually converting to byte-based interfaces, as they are easier to reason about than sector-based. Convert another internal function (no semantic change). Signed-off-by: Eric Blake Reviewed-by: John Snow Reviewed-by: Jeff Cody --- v3-v4: no change v2: rebase to earlier changes --- block/mirror.c | 75 ++++++++++++++++++++++++++----------------------------= ---- 1 file changed, 33 insertions(+), 42 deletions(-) diff --git a/block/mirror.c b/block/mirror.c index 374cefd..d3325d0 100644 --- a/block/mirror.c +++ b/block/mirror.c @@ -196,7 +196,7 @@ static inline int mirror_clip_sectors(MirrorBlockJob *s, /* Round offset and/or bytes to target cluster if COW is needed, and * return the offset of the adjusted tail against original. */ static int mirror_cow_align(MirrorBlockJob *s, int64_t *offset, - unsigned int *bytes) + uint64_t *bytes) { bool need_cow; int ret =3D 0; @@ -204,6 +204,7 @@ static int mirror_cow_align(MirrorBlockJob *s, int64_t = *offset, unsigned int align_bytes =3D *bytes; int max_bytes =3D s->granularity * s->max_iov; + assert(*bytes < INT_MAX); need_cow =3D !test_bit(*offset / s->granularity, s->cow_bitmap); need_cow |=3D !test_bit((*offset + *bytes - 1) / s->granularity, s->cow_bitmap); @@ -239,59 +240,50 @@ static inline void mirror_wait_for_io(MirrorBlockJob = *s) } /* Submit async read while handling COW. - * Returns: The number of sectors copied after and including sector_num, - * excluding any sectors copied prior to sector_num due to alignm= ent. - * This will be nb_sectors if no alignment is necessary, or - * (new_end - sector_num) if tail is rounded up or down due to + * Returns: The number of bytes copied after and including offset, + * excluding any bytes copied prior to offset due to alignment. + * This will be @bytes if no alignment is necessary, or + * (new_end - offset) if tail is rounded up or down due to * alignment or buffer limit. */ -static int mirror_do_read(MirrorBlockJob *s, int64_t sector_num, - int nb_sectors) +static uint64_t mirror_do_read(MirrorBlockJob *s, int64_t offset, + uint64_t bytes) { BlockBackend *source =3D s->common.blk; - int sectors_per_chunk, nb_chunks; - int ret; + int nb_chunks; + uint64_t ret; MirrorOp *op; - int max_sectors; + uint64_t max_bytes; - sectors_per_chunk =3D s->granularity >> BDRV_SECTOR_BITS; - max_sectors =3D sectors_per_chunk * s->max_iov; + max_bytes =3D s->granularity * s->max_iov; /* We can only handle as much as buf_size at a time. */ - nb_sectors =3D MIN(s->buf_size >> BDRV_SECTOR_BITS, nb_sectors); - nb_sectors =3D MIN(max_sectors, nb_sectors); - assert(nb_sectors); - assert(nb_sectors < BDRV_REQUEST_MAX_SECTORS); - ret =3D nb_sectors; + bytes =3D MIN(s->buf_size, MIN(max_bytes, bytes)); + assert(bytes); + assert(bytes < BDRV_REQUEST_MAX_BYTES); + ret =3D bytes; if (s->cow_bitmap) { - int64_t offset =3D sector_num * BDRV_SECTOR_SIZE; - unsigned int bytes =3D nb_sectors * BDRV_SECTOR_SIZE; - int gap; - - gap =3D mirror_cow_align(s, &offset, &bytes); - sector_num =3D offset / BDRV_SECTOR_SIZE; - nb_sectors =3D bytes / BDRV_SECTOR_SIZE; - ret +=3D gap / BDRV_SECTOR_SIZE; + ret +=3D mirror_cow_align(s, &offset, &bytes); } - assert(nb_sectors << BDRV_SECTOR_BITS <=3D s->buf_size); - /* The sector range must meet granularity because: + assert(bytes <=3D s->buf_size); + /* The range will be sector-aligned because: * 1) Caller passes in aligned values; - * 2) mirror_cow_align is used only when target cluster is larger. */ - assert(!(sector_num % sectors_per_chunk)); - nb_chunks =3D DIV_ROUND_UP(nb_sectors, sectors_per_chunk); + * 2) mirror_cow_align is used only when target cluster is larger. + * But it might not be cluster-aligned at end-of-file. */ + assert(QEMU_IS_ALIGNED(bytes, BDRV_SECTOR_SIZE)); + nb_chunks =3D DIV_ROUND_UP(bytes, s->granularity); while (s->buf_free_count < nb_chunks) { - trace_mirror_yield_in_flight(s, sector_num * BDRV_SECTOR_SIZE, - s->in_flight); + trace_mirror_yield_in_flight(s, offset, s->in_flight); mirror_wait_for_io(s); } /* Allocate a MirrorOp that is used as an AIO callback. */ op =3D g_new(MirrorOp, 1); op->s =3D s; - op->offset =3D sector_num * BDRV_SECTOR_SIZE; - op->bytes =3D nb_sectors * BDRV_SECTOR_SIZE; + op->offset =3D offset; + op->bytes =3D bytes; /* Now make a QEMUIOVector taking enough granularity-sized chunks * from s->buf_free. @@ -299,7 +291,7 @@ static int mirror_do_read(MirrorBlockJob *s, int64_t se= ctor_num, qemu_iovec_init(&op->qiov, nb_chunks); while (nb_chunks-- > 0) { MirrorBuffer *buf =3D QSIMPLEQ_FIRST(&s->buf_free); - size_t remaining =3D nb_sectors * BDRV_SECTOR_SIZE - op->qiov.size; + size_t remaining =3D bytes - op->qiov.size; QSIMPLEQ_REMOVE_HEAD(&s->buf_free, next); s->buf_free_count--; @@ -308,12 +300,10 @@ static int mirror_do_read(MirrorBlockJob *s, int64_t = sector_num, /* Copy the dirty cluster. */ s->in_flight++; - s->bytes_in_flight +=3D nb_sectors * BDRV_SECTOR_SIZE; - trace_mirror_one_iteration(s, sector_num * BDRV_SECTOR_SIZE, - nb_sectors * BDRV_SECTOR_SIZE); + s->bytes_in_flight +=3D bytes; + trace_mirror_one_iteration(s, offset, bytes); - blk_aio_preadv(source, sector_num * BDRV_SECTOR_SIZE, &op->qiov, 0, - mirror_read_complete, op); + blk_aio_preadv(source, offset, &op->qiov, 0, mirror_read_complete, op); return ret; } @@ -461,8 +451,9 @@ static uint64_t coroutine_fn mirror_iteration(MirrorBlo= ckJob *s) io_sectors =3D mirror_clip_sectors(s, sector_num, io_sectors); switch (mirror_method) { case MIRROR_METHOD_COPY: - io_sectors =3D mirror_do_read(s, sector_num, io_sectors); - io_bytes_acct =3D io_sectors * BDRV_SECTOR_SIZE; + io_bytes_acct =3D mirror_do_read(s, sector_num * BDRV_SECTOR_S= IZE, + io_sectors * BDRV_SECTOR_SIZE); + io_sectors =3D io_bytes_acct / BDRV_SECTOR_SIZE; break; case MIRROR_METHOD_ZERO: case MIRROR_METHOD_DISCARD: --=20 2.9.4 From nobody Sun Oct 5 19:15:27 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1499289432375134.76482478208015; Wed, 5 Jul 2017 14:17:12 -0700 (PDT) Received: from localhost ([::1]:48094 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSrfb-0003QG-0j for importer@patchew.org; Wed, 05 Jul 2017 17:17:11 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38059) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSrY3-00055z-2j for qemu-devel@nongnu.org; Wed, 05 Jul 2017 17:09:24 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dSrXx-0002ky-SV for qemu-devel@nongnu.org; Wed, 05 Jul 2017 17:09:23 -0400 Received: from mx1.redhat.com ([209.132.183.28]:52554) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dSrXu-0002in-9Q; Wed, 05 Jul 2017 17:09:14 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 59E694E4D1; Wed, 5 Jul 2017 21:09:13 +0000 (UTC) Received: from red.redhat.com (ovpn-120-36.rdu2.redhat.com [10.10.120.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id 486F5600CC; Wed, 5 Jul 2017 21:09:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 59E694E4D1 Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=eblake@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 59E694E4D1 From: Eric Blake To: qemu-devel@nongnu.org Date: Wed, 5 Jul 2017 16:08:34 -0500 Message-Id: <20170705210842.960-14-eblake@redhat.com> In-Reply-To: <20170705210842.960-1-eblake@redhat.com> References: <20170705210842.960-1-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Wed, 05 Jul 2017 21:09:13 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v4 13/21] mirror: Switch mirror_iteration() to byte-based X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, jcody@redhat.com, jsnow@redhat.com, qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" We are gradually converting to byte-based interfaces, as they are easier to reason about than sector-based. Change the internal loop iteration of mirroring to track by bytes instead of sectors (although we are still guaranteed that we iterate by steps that are both sector-aligned and multiples of the granularity). Drop the now-unused mirror_clip_sectors(). Signed-off-by: Eric Blake Reviewed-by: John Snow Reviewed-by: Jeff Cody Reviewed-by: Kevin Wolf --- v4: no change v3: rebase to Paolo's thread-safety changes, R-b kept v2: straightforward rebase to earlier mirror_clip_bytes() change, R-b kept --- block/mirror.c | 105 +++++++++++++++++++++++++----------------------------= ---- 1 file changed, 46 insertions(+), 59 deletions(-) diff --git a/block/mirror.c b/block/mirror.c index d3325d0..f54a8d7 100644 --- a/block/mirror.c +++ b/block/mirror.c @@ -184,15 +184,6 @@ static inline int64_t mirror_clip_bytes(MirrorBlockJob= *s, return MIN(bytes, s->bdev_length - offset); } -/* Clip nb_sectors relative to sector_num to not exceed end-of-file */ -static inline int mirror_clip_sectors(MirrorBlockJob *s, - int64_t sector_num, - int nb_sectors) -{ - return MIN(nb_sectors, - s->bdev_length / BDRV_SECTOR_SIZE - sector_num); -} - /* Round offset and/or bytes to target cluster if COW is needed, and * return the offset of the adjusted tail against original. */ static int mirror_cow_align(MirrorBlockJob *s, int64_t *offset, @@ -336,30 +327,28 @@ static void mirror_do_zero_or_discard(MirrorBlockJob = *s, static uint64_t coroutine_fn mirror_iteration(MirrorBlockJob *s) { BlockDriverState *source =3D s->source; - int64_t sector_num, first_chunk; + int64_t offset, first_chunk; uint64_t delay_ns =3D 0; /* At least the first dirty chunk is mirrored in one iteration. */ int nb_chunks =3D 1; - int64_t end =3D s->bdev_length / BDRV_SECTOR_SIZE; int sectors_per_chunk =3D s->granularity >> BDRV_SECTOR_BITS; bool write_zeroes_ok =3D bdrv_can_write_zeroes_with_unmap(blk_bs(s->ta= rget)); int max_io_bytes =3D MAX(s->buf_size / MAX_IN_FLIGHT, MAX_IO_BYTES); bdrv_dirty_bitmap_lock(s->dirty_bitmap); - sector_num =3D bdrv_dirty_iter_next(s->dbi); - if (sector_num < 0) { + offset =3D bdrv_dirty_iter_next(s->dbi) * BDRV_SECTOR_SIZE; + if (offset < 0) { bdrv_set_dirty_iter(s->dbi, 0); - sector_num =3D bdrv_dirty_iter_next(s->dbi); + offset =3D bdrv_dirty_iter_next(s->dbi) * BDRV_SECTOR_SIZE; trace_mirror_restart_iter(s, bdrv_get_dirty_count(s->dirty_bitmap)= * BDRV_SECTOR_SIZE); - assert(sector_num >=3D 0); + assert(offset >=3D 0); } bdrv_dirty_bitmap_unlock(s->dirty_bitmap); - first_chunk =3D sector_num / sectors_per_chunk; + first_chunk =3D offset / s->granularity; while (test_bit(first_chunk, s->in_flight_bitmap)) { - trace_mirror_yield_in_flight(s, sector_num * BDRV_SECTOR_SIZE, - s->in_flight); + trace_mirror_yield_in_flight(s, offset, s->in_flight); mirror_wait_for_io(s); } @@ -368,25 +357,26 @@ static uint64_t coroutine_fn mirror_iteration(MirrorB= lockJob *s) /* Find the number of consective dirty chunks following the first dirty * one, and wait for in flight requests in them. */ bdrv_dirty_bitmap_lock(s->dirty_bitmap); - while (nb_chunks * sectors_per_chunk < (s->buf_size >> BDRV_SECTOR_BIT= S)) { + while (nb_chunks * s->granularity < s->buf_size) { int64_t next_dirty; - int64_t next_sector =3D sector_num + nb_chunks * sectors_per_chunk; - int64_t next_chunk =3D next_sector / sectors_per_chunk; - if (next_sector >=3D end || - !bdrv_get_dirty_locked(source, s->dirty_bitmap, next_sector)) { + int64_t next_offset =3D offset + nb_chunks * s->granularity; + int64_t next_chunk =3D next_offset / s->granularity; + if (next_offset >=3D s->bdev_length || + !bdrv_get_dirty_locked(source, s->dirty_bitmap, + next_offset >> BDRV_SECTOR_BITS)) { break; } if (test_bit(next_chunk, s->in_flight_bitmap)) { break; } - next_dirty =3D bdrv_dirty_iter_next(s->dbi); - if (next_dirty > next_sector || next_dirty < 0) { + next_dirty =3D bdrv_dirty_iter_next(s->dbi) * BDRV_SECTOR_SIZE; + if (next_dirty > next_offset || next_dirty < 0) { /* The bitmap iterator's cache is stale, refresh it */ - bdrv_set_dirty_iter(s->dbi, next_sector); - next_dirty =3D bdrv_dirty_iter_next(s->dbi); + bdrv_set_dirty_iter(s->dbi, next_offset >> BDRV_SECTOR_BITS); + next_dirty =3D bdrv_dirty_iter_next(s->dbi) * BDRV_SECTOR_SIZE; } - assert(next_dirty =3D=3D next_sector); + assert(next_dirty =3D=3D next_offset); nb_chunks++; } @@ -394,14 +384,15 @@ static uint64_t coroutine_fn mirror_iteration(MirrorB= lockJob *s) * calling bdrv_get_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, sector_num, - nb_chunks * sectors_per_chunk); + bdrv_reset_dirty_bitmap_locked(s->dirty_bitmap, offset >> BDRV_SECTOR_= BITS, + nb_chunks * sectors_per_chunk); bdrv_dirty_bitmap_unlock(s->dirty_bitmap); - bitmap_set(s->in_flight_bitmap, sector_num / sectors_per_chunk, nb_chu= nks); - while (nb_chunks > 0 && sector_num < end) { + 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; + unsigned int io_bytes; int64_t io_bytes_acct; BlockDriverState *file; enum MirrorMethod { @@ -410,28 +401,28 @@ static uint64_t coroutine_fn mirror_iteration(MirrorB= lockJob *s) MIRROR_METHOD_DISCARD } mirror_method =3D MIRROR_METHOD_COPY; - assert(!(sector_num % sectors_per_chunk)); - ret =3D bdrv_get_block_status_above(source, NULL, sector_num, + assert(!(offset % s->granularity)); + ret =3D bdrv_get_block_status_above(source, NULL, + offset >> BDRV_SECTOR_BITS, nb_chunks * sectors_per_chunk, &io_sectors, &file); + io_bytes =3D io_sectors * BDRV_SECTOR_SIZE; if (ret < 0) { - io_sectors =3D MIN(nb_chunks * sectors_per_chunk, - max_io_bytes >> BDRV_SECTOR_BITS); + io_bytes =3D MIN(nb_chunks * s->granularity, max_io_bytes); } else if (ret & BDRV_BLOCK_DATA) { - io_sectors =3D MIN(io_sectors, max_io_bytes >> BDRV_SECTOR_BIT= S); + io_bytes =3D MIN(io_bytes, max_io_bytes); } - io_sectors -=3D io_sectors % sectors_per_chunk; - if (io_sectors < sectors_per_chunk) { - io_sectors =3D sectors_per_chunk; + io_bytes -=3D io_bytes % s->granularity; + if (io_bytes < s->granularity) { + io_bytes =3D s->granularity; } else if (ret >=3D 0 && !(ret & BDRV_BLOCK_DATA)) { - int64_t target_sector_num; - int target_nb_sectors; - bdrv_round_sectors_to_clusters(blk_bs(s->target), sector_num, - io_sectors, &target_sector_num, - &target_nb_sectors); - if (target_sector_num =3D=3D sector_num && - target_nb_sectors =3D=3D io_sectors) { + int64_t target_offset; + unsigned int target_bytes; + bdrv_round_to_clusters(blk_bs(s->target), offset, io_bytes, + &target_offset, &target_bytes); + if (target_offset =3D=3D offset && + target_bytes =3D=3D io_bytes) { mirror_method =3D ret & BDRV_BLOCK_ZERO ? MIRROR_METHOD_ZERO : MIRROR_METHOD_DISCARD; @@ -439,8 +430,7 @@ static uint64_t coroutine_fn mirror_iteration(MirrorBlo= ckJob *s) } while (s->in_flight >=3D MAX_IN_FLIGHT) { - trace_mirror_yield_in_flight(s, sector_num * BDRV_SECTOR_SIZE, - s->in_flight); + trace_mirror_yield_in_flight(s, offset, s->in_flight); mirror_wait_for_io(s); } @@ -448,30 +438,27 @@ static uint64_t coroutine_fn mirror_iteration(MirrorB= lockJob *s) return 0; } - io_sectors =3D mirror_clip_sectors(s, sector_num, io_sectors); + io_bytes =3D mirror_clip_bytes(s, offset, io_bytes); switch (mirror_method) { case MIRROR_METHOD_COPY: - io_bytes_acct =3D mirror_do_read(s, sector_num * BDRV_SECTOR_S= IZE, - io_sectors * BDRV_SECTOR_SIZE); - io_sectors =3D io_bytes_acct / BDRV_SECTOR_SIZE; + io_bytes =3D io_bytes_acct =3D mirror_do_read(s, offset, io_by= tes); break; case MIRROR_METHOD_ZERO: case MIRROR_METHOD_DISCARD: - mirror_do_zero_or_discard(s, sector_num * BDRV_SECTOR_SIZE, - io_sectors * BDRV_SECTOR_SIZE, + mirror_do_zero_or_discard(s, offset, io_bytes, mirror_method =3D=3D MIRROR_METHOD_D= ISCARD); if (write_zeroes_ok) { io_bytes_acct =3D 0; } else { - io_bytes_acct =3D io_sectors * BDRV_SECTOR_SIZE; + io_bytes_acct =3D io_bytes; } break; default: abort(); } - assert(io_sectors); - sector_num +=3D io_sectors; - nb_chunks -=3D DIV_ROUND_UP(io_sectors, sectors_per_chunk); + assert(io_bytes); + offset +=3D io_bytes; + nb_chunks -=3D DIV_ROUND_UP(io_bytes, s->granularity); if (s->common.speed) { delay_ns =3D ratelimit_calculate_delay(&s->limit, io_bytes_acc= t); } --=20 2.9.4 From nobody Sun Oct 5 19:15:27 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1499289716838980.8600253217395; Wed, 5 Jul 2017 14:21:56 -0700 (PDT) Received: from localhost ([::1]:48124 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSrkB-0007wY-Hr for importer@patchew.org; Wed, 05 Jul 2017 17:21:55 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38109) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSrY5-00058h-4E for qemu-devel@nongnu.org; Wed, 05 Jul 2017 17:09:26 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dSrY4-0002no-5k for qemu-devel@nongnu.org; Wed, 05 Jul 2017 17:09:25 -0400 Received: from mx1.redhat.com ([209.132.183.28]:52856) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dSrXz-0002lA-1X; Wed, 05 Jul 2017 17:09:19 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 1D5CE4E024; Wed, 5 Jul 2017 21:09:18 +0000 (UTC) Received: from red.redhat.com (ovpn-120-36.rdu2.redhat.com [10.10.120.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id 917CF600CC; Wed, 5 Jul 2017 21:09:13 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 1D5CE4E024 Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=eblake@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 1D5CE4E024 From: Eric Blake To: qemu-devel@nongnu.org Date: Wed, 5 Jul 2017 16:08:35 -0500 Message-Id: <20170705210842.960-15-eblake@redhat.com> In-Reply-To: <20170705210842.960-1-eblake@redhat.com> References: <20170705210842.960-1-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Wed, 05 Jul 2017 21:09:18 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v4 14/21] block: Drop unused bdrv_round_sectors_to_clusters() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, Fam Zheng , qemu-block@nongnu.org, jcody@redhat.com, Max Reitz , Stefan Hajnoczi , jsnow@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Now that the last user [mirror_iteration()] has converted to using bytes, we no longer need a function to round sectors to clusters. Signed-off-by: Eric Blake Reviewed-by: John Snow Reviewed-by: Jeff Cody Reviewed-by: Kevin Wolf --- v3-v4: no change v2: hoist to earlier series, no change --- include/block/block.h | 4 ---- block/io.c | 21 --------------------- 2 files changed, 25 deletions(-) diff --git a/include/block/block.h b/include/block/block.h index f432ea6..a9dc753 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -472,10 +472,6 @@ const char *bdrv_get_device_or_node_name(const BlockDr= iverState *bs); int bdrv_get_flags(BlockDriverState *bs); int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi); ImageInfoSpecific *bdrv_get_specific_info(BlockDriverState *bs); -void bdrv_round_sectors_to_clusters(BlockDriverState *bs, - int64_t sector_num, int nb_sectors, - int64_t *cluster_sector_num, - int *cluster_nb_sectors); void bdrv_round_to_clusters(BlockDriverState *bs, int64_t offset, unsigned int bytes, int64_t *cluster_offset, diff --git a/block/io.c b/block/io.c index 9186c5a..f662c97 100644 --- a/block/io.c +++ b/block/io.c @@ -419,27 +419,6 @@ static void mark_request_serialising(BdrvTrackedReques= t *req, uint64_t align) } /** - * Round a region to cluster boundaries (sector-based) - */ -void bdrv_round_sectors_to_clusters(BlockDriverState *bs, - int64_t sector_num, int nb_sectors, - int64_t *cluster_sector_num, - int *cluster_nb_sectors) -{ - BlockDriverInfo bdi; - - if (bdrv_get_info(bs, &bdi) < 0 || bdi.cluster_size =3D=3D 0) { - *cluster_sector_num =3D sector_num; - *cluster_nb_sectors =3D nb_sectors; - } else { - int64_t c =3D bdi.cluster_size / BDRV_SECTOR_SIZE; - *cluster_sector_num =3D QEMU_ALIGN_DOWN(sector_num, c); - *cluster_nb_sectors =3D QEMU_ALIGN_UP(sector_num - *cluster_sector= _num + - nb_sectors, c); - } -} - -/** * Round a region to cluster boundaries */ void bdrv_round_to_clusters(BlockDriverState *bs, --=20 2.9.4 From nobody Sun Oct 5 19:15:27 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1499289914810391.5258927625898; Wed, 5 Jul 2017 14:25:14 -0700 (PDT) Received: from localhost ([::1]:48141 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSrnM-0002yA-OP for importer@patchew.org; Wed, 05 Jul 2017 17:25:12 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38123) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSrY5-00059L-JL for qemu-devel@nongnu.org; Wed, 05 Jul 2017 17:09:26 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dSrY4-0002nv-BW for qemu-devel@nongnu.org; Wed, 05 Jul 2017 17:09:25 -0400 Received: from mx1.redhat.com ([209.132.183.28]:36494) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dSrY0-0002ld-6w; Wed, 05 Jul 2017 17:09:20 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 4254861B83; Wed, 5 Jul 2017 21:09:19 +0000 (UTC) Received: from red.redhat.com (ovpn-120-36.rdu2.redhat.com [10.10.120.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id 54F41600CC; Wed, 5 Jul 2017 21:09:18 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 4254861B83 Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=eblake@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 4254861B83 From: Eric Blake To: qemu-devel@nongnu.org Date: Wed, 5 Jul 2017 16:08:36 -0500 Message-Id: <20170705210842.960-16-eblake@redhat.com> In-Reply-To: <20170705210842.960-1-eblake@redhat.com> References: <20170705210842.960-1-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Wed, 05 Jul 2017 21:09:19 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v4 15/21] backup: Switch BackupBlockJob to byte-based X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, jcody@redhat.com, jsnow@redhat.com, qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" We are gradually converting to byte-based interfaces, as they are easier to reason about than sector-based. Continue by converting an internal structure (no semantic change), and all references to tracking progress. Drop a redundant local variable bytes_per_cluster. Signed-off-by: Eric Blake Reviewed-by: John Snow Reviewed-by: Jeff Cody Reviewed-by: Kevin Wolf --- v2-v4: no change --- block/backup.c | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/block/backup.c b/block/backup.c index 06431ac..4e64710 100644 --- a/block/backup.c +++ b/block/backup.c @@ -39,7 +39,7 @@ typedef struct BackupBlockJob { BlockdevOnError on_source_error; BlockdevOnError on_target_error; CoRwlock flush_rwlock; - uint64_t sectors_read; + uint64_t bytes_read; unsigned long *done_bitmap; int64_t cluster_size; bool compress; @@ -102,16 +102,15 @@ static int coroutine_fn backup_do_cow(BackupBlockJob = *job, void *bounce_buffer =3D NULL; int ret =3D 0; int64_t sectors_per_cluster =3D cluster_size_sectors(job); - int64_t bytes_per_cluster =3D sectors_per_cluster * BDRV_SECTOR_SIZE; - int64_t start, end; - int n; + int64_t start, end; /* clusters */ + int n; /* bytes */ qemu_co_rwlock_rdlock(&job->flush_rwlock); start =3D sector_num / sectors_per_cluster; end =3D DIV_ROUND_UP(sector_num + nb_sectors, sectors_per_cluster); - trace_backup_do_cow_enter(job, start * bytes_per_cluster, + trace_backup_do_cow_enter(job, start * job->cluster_size, sector_num * BDRV_SECTOR_SIZE, nb_sectors * BDRV_SECTOR_SIZE); @@ -120,28 +119,27 @@ static int coroutine_fn backup_do_cow(BackupBlockJob = *job, for (; start < end; start++) { if (test_bit(start, job->done_bitmap)) { - trace_backup_do_cow_skip(job, start * bytes_per_cluster); + trace_backup_do_cow_skip(job, start * job->cluster_size); continue; /* already copied */ } - trace_backup_do_cow_process(job, start * bytes_per_cluster); + trace_backup_do_cow_process(job, start * job->cluster_size); - n =3D MIN(sectors_per_cluster, - job->common.len / BDRV_SECTOR_SIZE - - start * sectors_per_cluster); + n =3D MIN(job->cluster_size, + job->common.len - start * job->cluster_size); if (!bounce_buffer) { bounce_buffer =3D blk_blockalign(blk, job->cluster_size); } iov.iov_base =3D bounce_buffer; - iov.iov_len =3D n * BDRV_SECTOR_SIZE; + iov.iov_len =3D n; qemu_iovec_init_external(&bounce_qiov, &iov, 1); ret =3D blk_co_preadv(blk, start * job->cluster_size, bounce_qiov.size, &bounce_qiov, is_write_notifier ? BDRV_REQ_NO_SERIALISING : = 0); if (ret < 0) { - trace_backup_do_cow_read_fail(job, start * bytes_per_cluster, = ret); + trace_backup_do_cow_read_fail(job, start * job->cluster_size, = ret); if (error_is_read) { *error_is_read =3D true; } @@ -157,7 +155,7 @@ static int coroutine_fn backup_do_cow(BackupBlockJob *j= ob, job->compress ? BDRV_REQ_WRITE_COMPRESSED= : 0); } if (ret < 0) { - trace_backup_do_cow_write_fail(job, start * bytes_per_cluster,= ret); + trace_backup_do_cow_write_fail(job, start * job->cluster_size,= ret); if (error_is_read) { *error_is_read =3D false; } @@ -169,8 +167,8 @@ static int coroutine_fn backup_do_cow(BackupBlockJob *j= ob, /* Publish progress, guest I/O counts as progress too. Note that = the * offset field is an opaque progress value, it is not a disk offs= et. */ - job->sectors_read +=3D n; - job->common.offset +=3D n * BDRV_SECTOR_SIZE; + job->bytes_read +=3D n; + job->common.offset +=3D n; } out: @@ -363,9 +361,8 @@ static bool coroutine_fn yield_and_check(BackupBlockJob= *job) */ if (job->common.speed) { uint64_t delay_ns =3D ratelimit_calculate_delay(&job->limit, - job->sectors_read * - BDRV_SECTOR_SIZE); - job->sectors_read =3D 0; + job->bytes_read); + job->bytes_read =3D 0; block_job_sleep_ns(&job->common, QEMU_CLOCK_REALTIME, delay_ns); } else { block_job_sleep_ns(&job->common, QEMU_CLOCK_REALTIME, 0); --=20 2.9.4 From nobody Sun Oct 5 19:15:27 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1499289599024740.7982340481608; Wed, 5 Jul 2017 14:19:59 -0700 (PDT) Received: from localhost ([::1]:48109 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSriH-0006Ed-Ib for importer@patchew.org; Wed, 05 Jul 2017 17:19:57 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38144) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSrY6-0005AT-FY for qemu-devel@nongnu.org; Wed, 05 Jul 2017 17:09:27 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dSrY5-0002oV-6l for qemu-devel@nongnu.org; Wed, 05 Jul 2017 17:09:26 -0400 Received: from mx1.redhat.com ([209.132.183.28]:36574) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dSrY1-0002mM-UI; Wed, 05 Jul 2017 17:09:22 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id CC2BA61E53; Wed, 5 Jul 2017 21:09:20 +0000 (UTC) Received: from red.redhat.com (ovpn-120-36.rdu2.redhat.com [10.10.120.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7BD79600CC; Wed, 5 Jul 2017 21:09:19 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com CC2BA61E53 Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=eblake@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com CC2BA61E53 From: Eric Blake To: qemu-devel@nongnu.org Date: Wed, 5 Jul 2017 16:08:37 -0500 Message-Id: <20170705210842.960-17-eblake@redhat.com> In-Reply-To: <20170705210842.960-1-eblake@redhat.com> References: <20170705210842.960-1-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Wed, 05 Jul 2017 21:09:21 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v4 16/21] backup: Switch block_backup.h to byte-based X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, qemu-block@nongnu.org, Wen Congyang , jcody@redhat.com, Max Reitz , jsnow@redhat.com, Xie Changlong Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" We are gradually converting to byte-based interfaces, as they are easier to reason about than sector-based. Continue by converting the public interface to backup jobs (no semantic change), including a change to CowRequest to track by bytes instead of cluster indices. Note that this does not change the difference between the public interface (starting point, and size of the subsequent range) and the internal interface (starting and end points). Signed-off-by: Eric Blake Reviewed-by: John Snow Reviewed-by: Xie Changlong Reviewed-by: Jeff Cody Reviewed-by: Kevin Wolf --- v3-v4: no change v2: change a couple more parameter names --- include/block/block_backup.h | 11 +++++------ block/backup.c | 33 ++++++++++++++++----------------- block/replication.c | 12 ++++++++---- 3 files changed, 29 insertions(+), 27 deletions(-) diff --git a/include/block/block_backup.h b/include/block/block_backup.h index 8a75947..994a3bd 100644 --- a/include/block/block_backup.h +++ b/include/block/block_backup.h @@ -21,17 +21,16 @@ #include "block/block_int.h" typedef struct CowRequest { - int64_t start; - int64_t end; + int64_t start_byte; + int64_t end_byte; QLIST_ENTRY(CowRequest) list; CoQueue wait_queue; /* coroutines blocked on this request */ } CowRequest; -void backup_wait_for_overlapping_requests(BlockJob *job, int64_t sector_nu= m, - int nb_sectors); +void backup_wait_for_overlapping_requests(BlockJob *job, int64_t offset, + uint64_t bytes); void backup_cow_request_begin(CowRequest *req, BlockJob *job, - int64_t sector_num, - int nb_sectors); + int64_t offset, uint64_t bytes); void backup_cow_request_end(CowRequest *req); void backup_do_checkpoint(BlockJob *job, Error **errp); diff --git a/block/backup.c b/block/backup.c index 4e64710..cfbd921 100644 --- a/block/backup.c +++ b/block/backup.c @@ -55,7 +55,7 @@ static inline int64_t cluster_size_sectors(BackupBlockJob= *job) /* See if in-flight requests overlap and wait for them to complete */ static void coroutine_fn wait_for_overlapping_requests(BackupBlockJob *job, - int64_t start, + int64_t offset, int64_t end) { CowRequest *req; @@ -64,7 +64,7 @@ static void coroutine_fn wait_for_overlapping_requests(Ba= ckupBlockJob *job, do { retry =3D false; QLIST_FOREACH(req, &job->inflight_reqs, list) { - if (end > req->start && start < req->end) { + if (end > req->start_byte && offset < req->end_byte) { qemu_co_queue_wait(&req->wait_queue, NULL); retry =3D true; break; @@ -75,10 +75,10 @@ static void coroutine_fn wait_for_overlapping_requests(= BackupBlockJob *job, /* Keep track of an in-flight request */ static void cow_request_begin(CowRequest *req, BackupBlockJob *job, - int64_t start, int64_t end) + int64_t offset, int64_t end) { - req->start =3D start; - req->end =3D end; + req->start_byte =3D offset; + req->end_byte =3D end; qemu_co_queue_init(&req->wait_queue); QLIST_INSERT_HEAD(&job->inflight_reqs, req, list); } @@ -114,8 +114,10 @@ static int coroutine_fn backup_do_cow(BackupBlockJob *= job, sector_num * BDRV_SECTOR_SIZE, nb_sectors * BDRV_SECTOR_SIZE); - wait_for_overlapping_requests(job, start, end); - cow_request_begin(&cow_request, job, start, end); + wait_for_overlapping_requests(job, start * job->cluster_size, + end * job->cluster_size); + cow_request_begin(&cow_request, job, start * job->cluster_size, + end * job->cluster_size); for (; start < end; start++) { if (test_bit(start, job->done_bitmap)) { @@ -277,32 +279,29 @@ void backup_do_checkpoint(BlockJob *job, Error **errp) bitmap_zero(backup_job->done_bitmap, len); } -void backup_wait_for_overlapping_requests(BlockJob *job, int64_t sector_nu= m, - int nb_sectors) +void backup_wait_for_overlapping_requests(BlockJob *job, int64_t offset, + uint64_t bytes) { BackupBlockJob *backup_job =3D container_of(job, BackupBlockJob, commo= n); - int64_t sectors_per_cluster =3D cluster_size_sectors(backup_job); int64_t start, end; assert(job->driver->job_type =3D=3D BLOCK_JOB_TYPE_BACKUP); - start =3D sector_num / sectors_per_cluster; - end =3D DIV_ROUND_UP(sector_num + nb_sectors, sectors_per_cluster); + start =3D QEMU_ALIGN_DOWN(offset, backup_job->cluster_size); + end =3D QEMU_ALIGN_UP(offset + bytes, backup_job->cluster_size); wait_for_overlapping_requests(backup_job, start, end); } void backup_cow_request_begin(CowRequest *req, BlockJob *job, - int64_t sector_num, - int nb_sectors) + int64_t offset, uint64_t bytes) { BackupBlockJob *backup_job =3D container_of(job, BackupBlockJob, commo= n); - int64_t sectors_per_cluster =3D cluster_size_sectors(backup_job); int64_t start, end; assert(job->driver->job_type =3D=3D BLOCK_JOB_TYPE_BACKUP); - start =3D sector_num / sectors_per_cluster; - end =3D DIV_ROUND_UP(sector_num + nb_sectors, sectors_per_cluster); + start =3D QEMU_ALIGN_DOWN(offset, backup_job->cluster_size); + end =3D QEMU_ALIGN_UP(offset + bytes, backup_job->cluster_size); cow_request_begin(req, backup_job, start, end); } diff --git a/block/replication.c b/block/replication.c index 3885f04..8f3aba7 100644 --- a/block/replication.c +++ b/block/replication.c @@ -234,10 +234,14 @@ static coroutine_fn int replication_co_readv(BlockDri= verState *bs, } if (job) { - backup_wait_for_overlapping_requests(child->bs->job, sector_num, - remaining_sectors); - backup_cow_request_begin(&req, child->bs->job, sector_num, - remaining_sectors); + uint64_t remaining_bytes =3D remaining_sectors * BDRV_SECTOR_SIZE; + + backup_wait_for_overlapping_requests(child->bs->job, + sector_num * BDRV_SECTOR_SIZE, + remaining_bytes); + backup_cow_request_begin(&req, child->bs->job, + sector_num * BDRV_SECTOR_SIZE, + remaining_bytes); ret =3D bdrv_co_readv(bs->file, sector_num, remaining_sectors, qiov); backup_cow_request_end(&req); --=20 2.9.4 From nobody Sun Oct 5 19:15:27 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1499289748902451.9226633898073; Wed, 5 Jul 2017 14:22:28 -0700 (PDT) Received: from localhost ([::1]:48125 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSrkh-0008Oq-II for importer@patchew.org; Wed, 05 Jul 2017 17:22:27 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38163) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSrY7-0005BR-96 for qemu-devel@nongnu.org; Wed, 05 Jul 2017 17:09:28 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dSrY5-0002p3-V4 for qemu-devel@nongnu.org; Wed, 05 Jul 2017 17:09:27 -0400 Received: from mx1.redhat.com ([209.132.183.28]:33704) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dSrY2-0002mk-Uk; Wed, 05 Jul 2017 17:09:23 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 013EFC0587E6; Wed, 5 Jul 2017 21:09:22 +0000 (UTC) Received: from red.redhat.com (ovpn-120-36.rdu2.redhat.com [10.10.120.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id 104956046B; Wed, 5 Jul 2017 21:09:20 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 013EFC0587E6 Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=eblake@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 013EFC0587E6 From: Eric Blake To: qemu-devel@nongnu.org Date: Wed, 5 Jul 2017 16:08:38 -0500 Message-Id: <20170705210842.960-18-eblake@redhat.com> In-Reply-To: <20170705210842.960-1-eblake@redhat.com> References: <20170705210842.960-1-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Wed, 05 Jul 2017 21:09:22 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v4 17/21] backup: Switch backup_do_cow() to byte-based X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, jcody@redhat.com, jsnow@redhat.com, qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" We are gradually converting to byte-based interfaces, as they are easier to reason about than sector-based. Convert another internal function (no semantic change). Signed-off-by: Eric Blake Reviewed-by: John Snow Reviewed-by: Jeff Cody Reviewed-by: Kevin Wolf --- v2-v4: no change --- block/backup.c | 62 ++++++++++++++++++++++++------------------------------= ---- 1 file changed, 26 insertions(+), 36 deletions(-) diff --git a/block/backup.c b/block/backup.c index cfbd921..c029d44 100644 --- a/block/backup.c +++ b/block/backup.c @@ -91,7 +91,7 @@ static void cow_request_end(CowRequest *req) } static int coroutine_fn backup_do_cow(BackupBlockJob *job, - int64_t sector_num, int nb_sectors, + int64_t offset, uint64_t bytes, bool *error_is_read, bool is_write_notifier) { @@ -101,34 +101,28 @@ static int coroutine_fn backup_do_cow(BackupBlockJob = *job, QEMUIOVector bounce_qiov; void *bounce_buffer =3D NULL; int ret =3D 0; - int64_t sectors_per_cluster =3D cluster_size_sectors(job); - int64_t start, end; /* clusters */ + int64_t start, end; /* bytes */ int n; /* bytes */ qemu_co_rwlock_rdlock(&job->flush_rwlock); - start =3D sector_num / sectors_per_cluster; - end =3D DIV_ROUND_UP(sector_num + nb_sectors, sectors_per_cluster); + start =3D QEMU_ALIGN_DOWN(offset, job->cluster_size); + end =3D QEMU_ALIGN_UP(bytes + offset, job->cluster_size); - trace_backup_do_cow_enter(job, start * job->cluster_size, - sector_num * BDRV_SECTOR_SIZE, - nb_sectors * BDRV_SECTOR_SIZE); + trace_backup_do_cow_enter(job, start, offset, bytes); - wait_for_overlapping_requests(job, start * job->cluster_size, - end * job->cluster_size); - cow_request_begin(&cow_request, job, start * job->cluster_size, - end * job->cluster_size); + wait_for_overlapping_requests(job, start, end); + cow_request_begin(&cow_request, job, start, end); - for (; start < end; start++) { - if (test_bit(start, job->done_bitmap)) { - trace_backup_do_cow_skip(job, start * job->cluster_size); + for (; start < end; start +=3D job->cluster_size) { + if (test_bit(start / job->cluster_size, job->done_bitmap)) { + trace_backup_do_cow_skip(job, start); continue; /* already copied */ } - trace_backup_do_cow_process(job, start * job->cluster_size); + trace_backup_do_cow_process(job, start); - n =3D MIN(job->cluster_size, - job->common.len - start * job->cluster_size); + n =3D MIN(job->cluster_size, job->common.len - start); if (!bounce_buffer) { bounce_buffer =3D blk_blockalign(blk, job->cluster_size); @@ -137,11 +131,10 @@ static int coroutine_fn backup_do_cow(BackupBlockJob = *job, iov.iov_len =3D n; qemu_iovec_init_external(&bounce_qiov, &iov, 1); - ret =3D blk_co_preadv(blk, start * job->cluster_size, - bounce_qiov.size, &bounce_qiov, + ret =3D blk_co_preadv(blk, start, bounce_qiov.size, &bounce_qiov, is_write_notifier ? BDRV_REQ_NO_SERIALISING : = 0); if (ret < 0) { - trace_backup_do_cow_read_fail(job, start * job->cluster_size, = ret); + trace_backup_do_cow_read_fail(job, start, ret); if (error_is_read) { *error_is_read =3D true; } @@ -149,22 +142,22 @@ static int coroutine_fn backup_do_cow(BackupBlockJob = *job, } if (buffer_is_zero(iov.iov_base, iov.iov_len)) { - ret =3D blk_co_pwrite_zeroes(job->target, start * job->cluster= _size, + ret =3D blk_co_pwrite_zeroes(job->target, start, bounce_qiov.size, BDRV_REQ_MAY_UNMA= P); } else { - ret =3D blk_co_pwritev(job->target, start * job->cluster_size, + ret =3D blk_co_pwritev(job->target, start, bounce_qiov.size, &bounce_qiov, job->compress ? BDRV_REQ_WRITE_COMPRESSED= : 0); } if (ret < 0) { - trace_backup_do_cow_write_fail(job, start * job->cluster_size,= ret); + trace_backup_do_cow_write_fail(job, start, ret); if (error_is_read) { *error_is_read =3D false; } goto out; } - set_bit(start, job->done_bitmap); + set_bit(start / job->cluster_size, job->done_bitmap); /* Publish progress, guest I/O counts as progress too. Note that = the * offset field is an opaque progress value, it is not a disk offs= et. @@ -180,8 +173,7 @@ out: cow_request_end(&cow_request); - trace_backup_do_cow_return(job, sector_num * BDRV_SECTOR_SIZE, - nb_sectors * BDRV_SECTOR_SIZE, ret); + trace_backup_do_cow_return(job, offset, bytes, ret); qemu_co_rwlock_unlock(&job->flush_rwlock); @@ -194,14 +186,12 @@ static int coroutine_fn backup_before_write_notify( { BackupBlockJob *job =3D container_of(notifier, BackupBlockJob, before_= write); BdrvTrackedRequest *req =3D opaque; - int64_t sector_num =3D req->offset >> BDRV_SECTOR_BITS; - int nb_sectors =3D req->bytes >> BDRV_SECTOR_BITS; assert(req->bs =3D=3D blk_bs(job->common.blk)); - assert((req->offset & (BDRV_SECTOR_SIZE - 1)) =3D=3D 0); - assert((req->bytes & (BDRV_SECTOR_SIZE - 1)) =3D=3D 0); + assert(QEMU_IS_ALIGNED(req->offset, BDRV_SECTOR_SIZE)); + assert(QEMU_IS_ALIGNED(req->bytes, BDRV_SECTOR_SIZE)); - return backup_do_cow(job, sector_num, nb_sectors, NULL, true); + return backup_do_cow(job, req->offset, req->bytes, NULL, true); } static void backup_set_speed(BlockJob *job, int64_t speed, Error **errp) @@ -406,8 +396,8 @@ static int coroutine_fn backup_run_incremental(BackupBl= ockJob *job) if (yield_and_check(job)) { goto out; } - ret =3D backup_do_cow(job, cluster * sectors_per_cluster, - sectors_per_cluster, &error_is_read, + ret =3D backup_do_cow(job, cluster * job->cluster_size, + job->cluster_size, &error_is_read, false); if ((ret < 0) && backup_error_action(job, error_is_read, -ret) =3D=3D @@ -509,8 +499,8 @@ static void coroutine_fn backup_run(void *opaque) if (alloced < 0) { ret =3D alloced; } else { - ret =3D backup_do_cow(job, start * sectors_per_cluster, - sectors_per_cluster, &error_is_read, + ret =3D backup_do_cow(job, start * job->cluster_size, + job->cluster_size, &error_is_read, false); } if (ret < 0) { --=20 2.9.4 From nobody Sun Oct 5 19:15:27 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1499290040491357.5636487583604; Wed, 5 Jul 2017 14:27:20 -0700 (PDT) Received: from localhost ([::1]:48158 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSrpO-0004q7-U3 for importer@patchew.org; Wed, 05 Jul 2017 17:27:18 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38180) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSrY8-0005Ca-1d for qemu-devel@nongnu.org; Wed, 05 Jul 2017 17:09:34 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dSrY6-0002pq-QF for qemu-devel@nongnu.org; Wed, 05 Jul 2017 17:09:27 -0400 Received: from mx1.redhat.com ([209.132.183.28]:3381) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dSrY4-0002nR-80; Wed, 05 Jul 2017 17:09:24 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 46DBB8047E; Wed, 5 Jul 2017 21:09:23 +0000 (UTC) Received: from red.redhat.com (ovpn-120-36.rdu2.redhat.com [10.10.120.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id 39396600CC; Wed, 5 Jul 2017 21:09:22 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 46DBB8047E Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=eblake@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 46DBB8047E From: Eric Blake To: qemu-devel@nongnu.org Date: Wed, 5 Jul 2017 16:08:39 -0500 Message-Id: <20170705210842.960-19-eblake@redhat.com> In-Reply-To: <20170705210842.960-1-eblake@redhat.com> References: <20170705210842.960-1-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Wed, 05 Jul 2017 21:09:23 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v4 18/21] backup: Switch backup_run() to byte-based X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, jcody@redhat.com, jsnow@redhat.com, qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" We are gradually converting to byte-based interfaces, as they are easier to reason about than sector-based. Change the internal loop iteration of backups to track by bytes instead of sectors (although we are still guaranteed that we iterate by steps that are cluster-aligned). Signed-off-by: Eric Blake Reviewed-by: John Snow Reviewed-by: Jeff Cody Reviewed-by: Kevin Wolf --- v2-v4: no change --- block/backup.c | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/block/backup.c b/block/backup.c index c029d44..04def91 100644 --- a/block/backup.c +++ b/block/backup.c @@ -370,11 +370,10 @@ static int coroutine_fn backup_run_incremental(Backup= BlockJob *job) int ret =3D 0; int clusters_per_iter; uint32_t granularity; - int64_t sector; + int64_t offset; int64_t cluster; int64_t end; int64_t last_cluster =3D -1; - int64_t sectors_per_cluster =3D cluster_size_sectors(job); BdrvDirtyBitmapIter *dbi; granularity =3D bdrv_dirty_bitmap_granularity(job->sync_bitmap); @@ -382,8 +381,8 @@ static int coroutine_fn backup_run_incremental(BackupBl= ockJob *job) dbi =3D bdrv_dirty_iter_new(job->sync_bitmap, 0); /* Find the next dirty sector(s) */ - while ((sector =3D bdrv_dirty_iter_next(dbi)) !=3D -1) { - cluster =3D sector / sectors_per_cluster; + while ((offset =3D bdrv_dirty_iter_next(dbi) * BDRV_SECTOR_SIZE) >=3D = 0) { + cluster =3D offset / job->cluster_size; /* Fake progress updates for any clusters we skipped */ if (cluster !=3D last_cluster + 1) { @@ -410,7 +409,8 @@ static int coroutine_fn backup_run_incremental(BackupBl= ockJob *job) /* If the bitmap granularity is smaller than the backup granularit= y, * we need to advance the iterator pointer to the next cluster. */ if (granularity < job->cluster_size) { - bdrv_set_dirty_iter(dbi, cluster * sectors_per_cluster); + bdrv_set_dirty_iter(dbi, + cluster * job->cluster_size / BDRV_SECTOR_= SIZE); } last_cluster =3D cluster - 1; @@ -432,17 +432,15 @@ static void coroutine_fn backup_run(void *opaque) BackupBlockJob *job =3D opaque; BackupCompleteData *data; BlockDriverState *bs =3D blk_bs(job->common.blk); - int64_t start, end; + int64_t offset; int64_t sectors_per_cluster =3D cluster_size_sectors(job); int ret =3D 0; QLIST_INIT(&job->inflight_reqs); qemu_co_rwlock_init(&job->flush_rwlock); - start =3D 0; - end =3D DIV_ROUND_UP(job->common.len, job->cluster_size); - - job->done_bitmap =3D bitmap_new(end); + job->done_bitmap =3D bitmap_new(DIV_ROUND_UP(job->common.len, + job->cluster_size)); job->before_write.notify =3D backup_before_write_notify; bdrv_add_before_write_notifier(bs, &job->before_write); @@ -457,7 +455,8 @@ static void coroutine_fn backup_run(void *opaque) ret =3D backup_run_incremental(job); } else { /* Both FULL and TOP SYNC_MODE's require copying.. */ - for (; start < end; start++) { + for (offset =3D 0; offset < job->common.len; + offset +=3D job->cluster_size) { bool error_is_read; int alloced =3D 0; @@ -480,8 +479,8 @@ static void coroutine_fn backup_run(void *opaque) * needed but at some point that is always the case. */ alloced =3D bdrv_is_allocated(bs, - start * sectors_per_cluster + i, - sectors_per_cluster - i, &n); + (offset >> BDRV_SECTOR_BITS) + i, + sectors_per_cluster - i, &n); i +=3D n; if (alloced || n =3D=3D 0) { @@ -499,9 +498,8 @@ static void coroutine_fn backup_run(void *opaque) if (alloced < 0) { ret =3D alloced; } else { - ret =3D backup_do_cow(job, start * job->cluster_size, - job->cluster_size, &error_is_read, - false); + ret =3D backup_do_cow(job, offset, job->cluster_size, + &error_is_read, false); } if (ret < 0) { /* Depending on error action, fail now or retry cluster */ @@ -510,7 +508,7 @@ static void coroutine_fn backup_run(void *opaque) if (action =3D=3D BLOCK_ERROR_ACTION_REPORT) { break; } else { - start--; + offset -=3D job->cluster_size; continue; } } --=20 2.9.4 From nobody Sun Oct 5 19:15:27 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1499289560431203.95925952247853; Wed, 5 Jul 2017 14:19:20 -0700 (PDT) Received: from localhost ([::1]:48106 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSrhf-0005gK-4D for importer@patchew.org; Wed, 05 Jul 2017 17:19:19 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38248) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSrYH-0005J8-0E for qemu-devel@nongnu.org; Wed, 05 Jul 2017 17:09:39 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dSrYE-0002se-He for qemu-devel@nongnu.org; Wed, 05 Jul 2017 17:09:36 -0400 Received: from mx1.redhat.com ([209.132.183.28]:39658) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dSrY6-0002ox-8k; Wed, 05 Jul 2017 17:09:26 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 580D27F3ED; Wed, 5 Jul 2017 21:09:25 +0000 (UTC) Received: from red.redhat.com (ovpn-120-36.rdu2.redhat.com [10.10.120.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id 89BB5600CC; Wed, 5 Jul 2017 21:09:23 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 580D27F3ED Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=eblake@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 580D27F3ED From: Eric Blake To: qemu-devel@nongnu.org Date: Wed, 5 Jul 2017 16:08:40 -0500 Message-Id: <20170705210842.960-20-eblake@redhat.com> In-Reply-To: <20170705210842.960-1-eblake@redhat.com> References: <20170705210842.960-1-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Wed, 05 Jul 2017 21:09:25 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v4 19/21] block: Make bdrv_is_allocated() byte-based X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, Fam Zheng , qemu-block@nongnu.org, Juan Quintela , jcody@redhat.com, "Dr. David Alan Gilbert" , Max Reitz , Stefan Hajnoczi , jsnow@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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 signature of the function to use int64_t *pnum 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 on input and that *pnum is sector-aligned on return to the caller, but that can be relaxed when a later patch implements byte-based block status. Therefore, this code adds usages like DIV_ROUND_UP(,BDRV_SECTOR_SIZE) to callers that still want aligned values, where the call might reasonbly give non-aligned results in the future; on the other hand, no rounding is needed for callers that should just continue to work with byte alignment. For the most part this patch is just the addition of scaling at the callers followed by inverse scaling at bdrv_is_allocated(). But some code, particularly bdrv_commit(), gets a lot simpler because it no longer has to mess with sectors; also, it is now possible to pass NULL if the caller does not care how much of the image is allocated beyond the initial offset. For ease of review, bdrv_is_allocated_above() will be tackled separately. Signed-off-by: Eric Blake Reviewed-by: John Snow Reviewed-by: Juan Quintela Reviewed-by: Jeff Cody --- v4: rebase to vvfat TAB cleanup, R-b kept v3: no change v2: rebase to earlier changes, tweak commit message --- include/block/block.h | 4 +-- block/backup.c | 17 ++++--------- block/commit.c | 21 +++++++--------- block/io.c | 49 +++++++++++++++++++++++++----------- block/stream.c | 5 ++-- block/vvfat.c | 34 ++++++++++++++----------- migration/block.c | 9 ++++--- qemu-img.c | 5 +++- qemu-io-cmds.c | 70 +++++++++++++++++++++++------------------------= ---- 9 files changed, 114 insertions(+), 100 deletions(-) diff --git a/include/block/block.h b/include/block/block.h index a9dc753..d3e01fb 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -427,8 +427,8 @@ int64_t bdrv_get_block_status_above(BlockDriverState *b= s, int64_t sector_num, int nb_sectors, int *pnum, BlockDriverState **file); -int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sec= tors, - int *pnum); +int bdrv_is_allocated(BlockDriverState *bs, int64_t offset, int64_t bytes, + int64_t *pnum); int bdrv_is_allocated_above(BlockDriverState *top, BlockDriverState *base, int64_t sector_num, int nb_sectors, int *pnum); diff --git a/block/backup.c b/block/backup.c index 04def91..b2048bf 100644 --- a/block/backup.c +++ b/block/backup.c @@ -47,12 +47,6 @@ typedef struct BackupBlockJob { QLIST_HEAD(, CowRequest) inflight_reqs; } BackupBlockJob; -/* Size of a cluster in sectors, instead of bytes. */ -static inline int64_t cluster_size_sectors(BackupBlockJob *job) -{ - return job->cluster_size / BDRV_SECTOR_SIZE; -} - /* See if in-flight requests overlap and wait for them to complete */ static void coroutine_fn wait_for_overlapping_requests(BackupBlockJob *job, int64_t offset, @@ -433,7 +427,6 @@ static void coroutine_fn backup_run(void *opaque) BackupCompleteData *data; BlockDriverState *bs =3D blk_bs(job->common.blk); int64_t offset; - int64_t sectors_per_cluster =3D cluster_size_sectors(job); int ret =3D 0; QLIST_INIT(&job->inflight_reqs); @@ -465,12 +458,13 @@ static void coroutine_fn backup_run(void *opaque) } if (job->sync_mode =3D=3D MIRROR_SYNC_MODE_TOP) { - int i, n; + int i; + int64_t n; /* Check to see if these blocks are already in the * backing file. */ - for (i =3D 0; i < sectors_per_cluster;) { + for (i =3D 0; i < job->cluster_size;) { /* bdrv_is_allocated() only returns true/false based * on the first set of sectors it comes across that * are are all in the same state. @@ -478,9 +472,8 @@ static void coroutine_fn backup_run(void *opaque) * backup cluster length. We end up copying more than * needed but at some point that is always the case. */ alloced =3D - bdrv_is_allocated(bs, - (offset >> BDRV_SECTOR_BITS) + i, - sectors_per_cluster - i, &n); + bdrv_is_allocated(bs, offset + i, + job->cluster_size - i, &n); i +=3D n; if (alloced || n =3D=3D 0) { diff --git a/block/commit.c b/block/commit.c index c3a7bca..241aa95 100644 --- a/block/commit.c +++ b/block/commit.c @@ -443,7 +443,7 @@ fail: } -#define COMMIT_BUF_SECTORS 2048 +#define COMMIT_BUF_SIZE (2048 * BDRV_SECTOR_SIZE) /* commit COW file into the raw image */ int bdrv_commit(BlockDriverState *bs) @@ -452,8 +452,9 @@ int bdrv_commit(BlockDriverState *bs) BlockDriverState *backing_file_bs =3D NULL; BlockDriverState *commit_top_bs =3D NULL; BlockDriver *drv =3D bs->drv; - int64_t sector, total_sectors, length, backing_length; - int n, ro, open_flags; + int64_t offset, length, backing_length; + int ro, open_flags; + int64_t n; int ret =3D 0; uint8_t *buf =3D NULL; Error *local_err =3D NULL; @@ -531,30 +532,26 @@ int bdrv_commit(BlockDriverState *bs) } } - total_sectors =3D length >> BDRV_SECTOR_BITS; - /* blk_try_blockalign() for src will choose an alignment that works for * backing as well, so no need to compare the alignment manually. */ - buf =3D blk_try_blockalign(src, COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE); + buf =3D blk_try_blockalign(src, COMMIT_BUF_SIZE); if (buf =3D=3D NULL) { ret =3D -ENOMEM; goto ro_cleanup; } - for (sector =3D 0; sector < total_sectors; sector +=3D n) { - ret =3D bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n); + for (offset =3D 0; offset < length; offset +=3D n) { + ret =3D bdrv_is_allocated(bs, offset, COMMIT_BUF_SIZE, &n); if (ret < 0) { goto ro_cleanup; } if (ret) { - ret =3D blk_pread(src, sector * BDRV_SECTOR_SIZE, buf, - n * BDRV_SECTOR_SIZE); + ret =3D blk_pread(src, offset, buf, n); if (ret < 0) { goto ro_cleanup; } - ret =3D blk_pwrite(backing, sector * BDRV_SECTOR_SIZE, buf, - n * BDRV_SECTOR_SIZE, 0); + ret =3D blk_pwrite(backing, offset, buf, n, 0); if (ret < 0) { goto ro_cleanup; } diff --git a/block/io.c b/block/io.c index f662c97..cb40069 100644 --- a/block/io.c +++ b/block/io.c @@ -1036,14 +1036,15 @@ static int coroutine_fn bdrv_aligned_preadv(BdrvChi= ld *child, int64_t start_sector =3D offset >> BDRV_SECTOR_BITS; int64_t end_sector =3D DIV_ROUND_UP(offset + bytes, BDRV_SECTOR_SI= ZE); unsigned int nb_sectors =3D end_sector - start_sector; - int pnum; + int64_t pnum; - ret =3D bdrv_is_allocated(bs, start_sector, nb_sectors, &pnum); + ret =3D bdrv_is_allocated(bs, start_sector << BDRV_SECTOR_BITS, + nb_sectors << BDRV_SECTOR_BITS, &pnum); if (ret < 0) { goto out; } - if (!ret || pnum !=3D nb_sectors) { + if (!ret || pnum !=3D nb_sectors << BDRV_SECTOR_BITS) { ret =3D bdrv_co_do_copy_on_readv(child, offset, bytes, qiov); goto out; } @@ -1904,15 +1905,26 @@ int64_t bdrv_get_block_status(BlockDriverState *bs, sector_num, nb_sectors, pnum, file); } -int coroutine_fn bdrv_is_allocated(BlockDriverState *bs, int64_t sector_nu= m, - int nb_sectors, int *pnum) +int coroutine_fn bdrv_is_allocated(BlockDriverState *bs, int64_t offset, + int64_t bytes, int64_t *pnum) { BlockDriverState *file; - int64_t ret =3D bdrv_get_block_status(bs, sector_num, nb_sectors, pnum, - &file); + int64_t sector_num =3D offset >> BDRV_SECTOR_BITS; + int nb_sectors =3D bytes >> BDRV_SECTOR_BITS; + int64_t ret; + int psectors; + + assert(QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE)); + assert(QEMU_IS_ALIGNED(bytes, BDRV_SECTOR_SIZE) && + bytes < INT_MAX * BDRV_SECTOR_SIZE); + ret =3D bdrv_get_block_status(bs, sector_num, nb_sectors, &psectors, + &file); if (ret < 0) { return ret; } + if (pnum) { + *pnum =3D psectors * BDRV_SECTOR_SIZE; + } return !!(ret & BDRV_BLOCK_ALLOCATED); } @@ -1921,7 +1933,8 @@ int coroutine_fn bdrv_is_allocated(BlockDriverState *= bs, int64_t sector_num, * * Return true if the given sector is allocated in any image between * BASE and TOP (inclusive). BASE can be NULL to check if the given - * sector is allocated in any image of the chain. Return false otherwise. + * sector is allocated in any image of the chain. Return false otherwise, + * or negative errno on failure. * * 'pnum' is set to the number of sectors (including and immediately follo= wing * the specified sector) that are known to be in the same @@ -1938,13 +1951,19 @@ int bdrv_is_allocated_above(BlockDriverState *top, intermediate =3D top; while (intermediate && intermediate !=3D base) { - int pnum_inter; - ret =3D bdrv_is_allocated(intermediate, sector_num, nb_sectors, + int64_t pnum_inter; + int psectors_inter; + + ret =3D bdrv_is_allocated(intermediate, sector_num * BDRV_SECTOR_S= IZE, + nb_sectors * BDRV_SECTOR_SIZE, &pnum_inter); if (ret < 0) { return ret; - } else if (ret) { - *pnum =3D pnum_inter; + } + assert(pnum_inter < INT_MAX * BDRV_SECTOR_SIZE); + psectors_inter =3D pnum_inter >> BDRV_SECTOR_BITS; + if (ret) { + *pnum =3D psectors_inter; return 1; } @@ -1954,10 +1973,10 @@ int bdrv_is_allocated_above(BlockDriverState *top, * * [sector_num+x, nr_sectors] allocated. */ - if (n > pnum_inter && + if (n > psectors_inter && (intermediate =3D=3D top || - sector_num + pnum_inter < intermediate->total_sectors)) { - n =3D pnum_inter; + sector_num + psectors_inter < intermediate->total_sectors)) { + n =3D psectors_inter; } intermediate =3D backing_bs(intermediate); diff --git a/block/stream.c b/block/stream.c index e3dd2ac..e5f2a08 100644 --- a/block/stream.c +++ b/block/stream.c @@ -137,6 +137,7 @@ static void coroutine_fn stream_run(void *opaque) for ( ; offset < s->common.len; offset +=3D n * BDRV_SECTOR_SIZE) { bool copy; + int64_t count =3D 0; /* Note that even when no rate limit is applied we need to yield * with no pending I/O here so that bdrv_drain_all() returns. @@ -148,8 +149,8 @@ static void coroutine_fn stream_run(void *opaque) copy =3D false; - ret =3D bdrv_is_allocated(bs, offset / BDRV_SECTOR_SIZE, - STREAM_BUFFER_SIZE / BDRV_SECTOR_SIZE, &n); + ret =3D bdrv_is_allocated(bs, offset, STREAM_BUFFER_SIZE, &count); + n =3D DIV_ROUND_UP(count, BDRV_SECTOR_SIZE); if (ret =3D=3D 1) { /* Allocated in the top, no need to copy. */ } else if (ret >=3D 0) { diff --git a/block/vvfat.c b/block/vvfat.c index f55104c..4fd28e1 100644 --- a/block/vvfat.c +++ b/block/vvfat.c @@ -1482,24 +1482,27 @@ static int vvfat_read(BlockDriverState *bs, int64_t= sector_num, if (sector_num >=3D bs->total_sectors) return -1; if (s->qcow) { - int n; + int64_t n; int ret; - ret =3D bdrv_is_allocated(s->qcow->bs, sector_num, - nb_sectors - i, &n); + ret =3D bdrv_is_allocated(s->qcow->bs, sector_num * BDRV_SECTO= R_SIZE, + (nb_sectors - i) * BDRV_SECTOR_SIZE, &= n); if (ret < 0) { return ret; } if (ret) { - DLOG(fprintf(stderr, "sectors %d+%d allocated\n", - (int)sector_num, n)); - if (bdrv_read(s->qcow, sector_num, buf + i * 0x200, n)) { + DLOG(fprintf(stderr, "sectors %" PRId64 "+%" PRId64 + " allocated\n", sector_num, + n >> BDRV_SECTOR_BITS)); + if (bdrv_read(s->qcow, sector_num, buf + i * 0x200, + n >> BDRV_SECTOR_BITS)) { return -1; } - i +=3D n - 1; - sector_num +=3D n - 1; + i +=3D (n >> BDRV_SECTOR_BITS) - 1; + sector_num +=3D (n >> BDRV_SECTOR_BITS) - 1; continue; } -DLOG(fprintf(stderr, "sector %d not allocated\n", (int)sector_num)); + DLOG(fprintf(stderr, "sector %" PRId64 " not allocated\n", + sector_num)); } if (sector_num < s->offset_to_root_dir) { if (sector_num < s->offset_to_fat) { @@ -1779,7 +1782,7 @@ static inline bool cluster_was_modified(BDRVVVFATStat= e *s, uint32_t cluster_num) { int was_modified =3D 0; - int i, dummy; + int i; if (s->qcow =3D=3D NULL) { return 0; @@ -1787,8 +1790,9 @@ static inline bool cluster_was_modified(BDRVVVFATStat= e *s, for (i =3D 0; !was_modified && i < s->sectors_per_cluster; i++) { was_modified =3D bdrv_is_allocated(s->qcow->bs, - cluster2sector(s, cluster_num) + = i, - 1, &dummy); + (cluster2sector(s, cluster_num) + + i) * BDRV_SECTOR_SIZE, + BDRV_SECTOR_SIZE, NULL); } /* @@ -1935,7 +1939,7 @@ static uint32_t get_cluster_count_for_direntry(BDRVVV= FATState* s, } if (copy_it) { - int i, dummy; + int i; /* * This is horribly inefficient, but that is okay, since * it is rarely executed, if at all. @@ -1946,7 +1950,9 @@ static uint32_t get_cluster_count_for_direntry(BDRVVV= FATState* s, for (i =3D 0; i < s->sectors_per_cluster; i++) { int res; - res =3D bdrv_is_allocated(s->qcow->bs, offset + i, 1, = &dummy); + res =3D bdrv_is_allocated(s->qcow->bs, + (offset + i) * BDRV_SECTOR_SIZ= E, + BDRV_SECTOR_SIZE, NULL); if (res < 0) { return -1; } diff --git a/migration/block.c b/migration/block.c index 7674ae1..4a48e5c 100644 --- a/migration/block.c +++ b/migration/block.c @@ -34,7 +34,7 @@ #define BLK_MIG_FLAG_PROGRESS 0x04 #define BLK_MIG_FLAG_ZERO_BLOCK 0x08 -#define MAX_IS_ALLOCATED_SEARCH 65536 +#define MAX_IS_ALLOCATED_SEARCH (65536 * BDRV_SECTOR_SIZE) #define MAX_INFLIGHT_IO 512 @@ -267,6 +267,7 @@ static int mig_save_device_bulk(QEMUFile *f, BlkMigDevS= tate *bmds) BlockBackend *bb =3D bmds->blk; BlkMigBlock *blk; int nr_sectors; + int64_t count; if (bmds->shared_base) { qemu_mutex_lock_iothread(); @@ -274,9 +275,9 @@ static int mig_save_device_bulk(QEMUFile *f, BlkMigDevS= tate *bmds) /* Skip unallocated sectors; intentionally treats failure as * an allocated sector */ while (cur_sector < total_sectors && - !bdrv_is_allocated(blk_bs(bb), cur_sector, - MAX_IS_ALLOCATED_SEARCH, &nr_sectors)) { - cur_sector +=3D nr_sectors; + !bdrv_is_allocated(blk_bs(bb), cur_sector * BDRV_SECTOR_SIZ= E, + MAX_IS_ALLOCATED_SEARCH, &count)) { + cur_sector +=3D DIV_ROUND_UP(count, BDRV_SECTOR_SIZE); } aio_context_release(blk_get_aio_context(bb)); qemu_mutex_unlock_iothread(); diff --git a/qemu-img.c b/qemu-img.c index d5a1560..5271b41 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -3229,6 +3229,7 @@ static int img_rebase(int argc, char **argv) int64_t new_backing_num_sectors =3D 0; uint64_t sector; int n; + int64_t count; float local_progress =3D 0; buf_old =3D blk_blockalign(blk, IO_BUF_SIZE); @@ -3276,12 +3277,14 @@ static int img_rebase(int argc, char **argv) } /* If the cluster is allocated, we don't need to take action */ - ret =3D bdrv_is_allocated(bs, sector, n, &n); + ret =3D bdrv_is_allocated(bs, sector << BDRV_SECTOR_BITS, + n << BDRV_SECTOR_BITS, &count); if (ret < 0) { error_report("error while reading image metadata: %s", strerror(-ret)); goto out; } + n =3D DIV_ROUND_UP(count, BDRV_SECTOR_SIZE); if (ret) { continue; } diff --git a/qemu-io-cmds.c b/qemu-io-cmds.c index b0ea327..e529b8f 100644 --- a/qemu-io-cmds.c +++ b/qemu-io-cmds.c @@ -1760,12 +1760,12 @@ out: static int alloc_f(BlockBackend *blk, int argc, char **argv) { BlockDriverState *bs =3D blk_bs(blk); - int64_t offset, sector_num, nb_sectors, remaining, count; + int64_t offset, start, remaining, count; char s1[64]; - int num, ret; - int64_t sum_alloc; + int ret; + int64_t num, sum_alloc; - offset =3D cvtnum(argv[1]); + start =3D offset =3D cvtnum(argv[1]); if (offset < 0) { print_cvtnum_err(offset, argv[1]); return 0; @@ -1793,32 +1793,30 @@ static int alloc_f(BlockBackend *blk, int argc, cha= r **argv) count); return 0; } - nb_sectors =3D count >> BDRV_SECTOR_BITS; - remaining =3D nb_sectors; + remaining =3D count; sum_alloc =3D 0; - sector_num =3D offset >> 9; while (remaining) { - ret =3D bdrv_is_allocated(bs, sector_num, remaining, &num); + ret =3D bdrv_is_allocated(bs, offset, remaining, &num); if (ret < 0) { printf("is_allocated failed: %s\n", strerror(-ret)); return 0; } - sector_num +=3D num; + offset +=3D num; remaining -=3D num; if (ret) { sum_alloc +=3D num; } if (num =3D=3D 0) { - nb_sectors -=3D remaining; + count -=3D remaining; remaining =3D 0; } } - cvtstr(offset, s1, sizeof(s1)); + cvtstr(start, s1, sizeof(s1)); printf("%"PRId64"/%"PRId64" bytes allocated at offset %s\n", - sum_alloc << BDRV_SECTOR_BITS, nb_sectors << BDRV_SECTOR_BITS, = s1); + sum_alloc, count, s1); return 0; } @@ -1833,14 +1831,15 @@ static const cmdinfo_t alloc_cmd =3D { }; -static int map_is_allocated(BlockDriverState *bs, int64_t sector_num, - int64_t nb_sectors, int64_t *pnum) +static int map_is_allocated(BlockDriverState *bs, int64_t offset, + int64_t bytes, int64_t *pnum) { - int num, num_checked; + int64_t num; + int num_checked; int ret, firstret; - num_checked =3D MIN(nb_sectors, INT_MAX); - ret =3D bdrv_is_allocated(bs, sector_num, num_checked, &num); + num_checked =3D MIN(bytes, BDRV_REQUEST_MAX_BYTES); + ret =3D bdrv_is_allocated(bs, offset, num_checked, &num); if (ret < 0) { return ret; } @@ -1848,12 +1847,12 @@ static int map_is_allocated(BlockDriverState *bs, i= nt64_t sector_num, firstret =3D ret; *pnum =3D num; - while (nb_sectors > 0 && ret =3D=3D firstret) { - sector_num +=3D num; - nb_sectors -=3D num; + while (bytes > 0 && ret =3D=3D firstret) { + offset +=3D num; + bytes -=3D num; - num_checked =3D MIN(nb_sectors, INT_MAX); - ret =3D bdrv_is_allocated(bs, sector_num, num_checked, &num); + num_checked =3D MIN(bytes, BDRV_REQUEST_MAX_BYTES); + ret =3D bdrv_is_allocated(bs, offset, num_checked, &num); if (ret =3D=3D firstret && num) { *pnum +=3D num; } else { @@ -1866,25 +1865,21 @@ static int map_is_allocated(BlockDriverState *bs, i= nt64_t sector_num, static int map_f(BlockBackend *blk, int argc, char **argv) { - int64_t offset; - int64_t nb_sectors, total_sectors; + int64_t offset, bytes; char s1[64], s2[64]; int64_t num; int ret; const char *retstr; offset =3D 0; - total_sectors =3D blk_nb_sectors(blk); - if (total_sectors < 0) { - error_report("Failed to query image length: %s", - strerror(-total_sectors)); + bytes =3D blk_getlength(blk); + if (bytes < 0) { + error_report("Failed to query image length: %s", strerror(-bytes)); return 0; } - nb_sectors =3D total_sectors; - - do { - ret =3D map_is_allocated(blk_bs(blk), offset, nb_sectors, &num); + while (bytes) { + ret =3D map_is_allocated(blk_bs(blk), offset, bytes, &num); if (ret < 0) { error_report("Failed to get allocation status: %s", strerror(-= ret)); return 0; @@ -1894,15 +1889,14 @@ static int map_f(BlockBackend *blk, int argc, char = **argv) } retstr =3D ret ? " allocated" : "not allocated"; - cvtstr(num << BDRV_SECTOR_BITS, s1, sizeof(s1)); - cvtstr(offset << BDRV_SECTOR_BITS, s2, sizeof(s2)); + cvtstr(num, s1, sizeof(s1)); + cvtstr(offset, s2, sizeof(s2)); printf("%s (0x%" PRIx64 ") bytes %s at offset %s (0x%" PRIx64 ")\n= ", - s1, num << BDRV_SECTOR_BITS, retstr, - s2, offset << BDRV_SECTOR_BITS); + s1, num, retstr, s2, offset); offset +=3D num; - nb_sectors -=3D num; - } while (offset < total_sectors); + bytes -=3D num; + }; return 0; } --=20 2.9.4 From nobody Sun Oct 5 19:15:27 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1499290147583786.219987576407; Wed, 5 Jul 2017 14:29:07 -0700 (PDT) Received: from localhost ([::1]:48166 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSrr7-0006h0-9D for importer@patchew.org; Wed, 05 Jul 2017 17:29:05 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38234) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSrYF-0005HN-Gq for qemu-devel@nongnu.org; Wed, 05 Jul 2017 17:09:36 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dSrYE-0002sj-II for qemu-devel@nongnu.org; Wed, 05 Jul 2017 17:09:35 -0400 Received: from mx1.redhat.com ([209.132.183.28]:49386) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dSrY7-0002q5-MO; Wed, 05 Jul 2017 17:09:27 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B81BB267C9; Wed, 5 Jul 2017 21:09:26 +0000 (UTC) Received: from red.redhat.com (ovpn-120-36.rdu2.redhat.com [10.10.120.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8EB82600CC; Wed, 5 Jul 2017 21:09:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com B81BB267C9 Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=eblake@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com B81BB267C9 From: Eric Blake To: qemu-devel@nongnu.org Date: Wed, 5 Jul 2017 16:08:41 -0500 Message-Id: <20170705210842.960-21-eblake@redhat.com> In-Reply-To: <20170705210842.960-1-eblake@redhat.com> References: <20170705210842.960-1-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Wed, 05 Jul 2017 21:09:26 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v4 20/21] block: Minimize raw use of bds->total_sectors X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, Fam Zheng , qemu-block@nongnu.org, jcody@redhat.com, Max Reitz , Stefan Hajnoczi , jsnow@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" bdrv_is_allocated_above() was relying on intermediate->total_sectors, which is a field that can have stale contents depending on the value of intermediate->has_variable_length. An audit shows that we are safe (we were first calling through bdrv_co_get_block_status() which in turn calls bdrv_nb_sectors() and therefore just refreshed the current length), but it's nicer to favor our accessor functions to avoid having to repeat such an audit, even if it means refresh_total_sectors() is called more frequently. Suggested-by: John Snow Signed-off-by: Eric Blake Reviewed-by: Manos Pitsidianakis Reviewed-by: Jeff Cody Reviewed-by: John Snow --- v3-v4: no change v2: new patch --- block/io.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/block/io.c b/block/io.c index cb40069..fb8d1c7 100644 --- a/block/io.c +++ b/block/io.c @@ -1952,6 +1952,7 @@ int bdrv_is_allocated_above(BlockDriverState *top, intermediate =3D top; while (intermediate && intermediate !=3D base) { int64_t pnum_inter; + int64_t size_inter; int psectors_inter; ret =3D bdrv_is_allocated(intermediate, sector_num * BDRV_SECTOR_S= IZE, @@ -1969,13 +1970,14 @@ int bdrv_is_allocated_above(BlockDriverState *top, /* * [sector_num, nb_sectors] is unallocated on top but intermediate - * might have - * - * [sector_num+x, nr_sectors] allocated. + * might have [sector_num+x, nb_sectors-x] allocated. */ + size_inter =3D bdrv_nb_sectors(intermediate); + if (size_inter < 0) { + return size_inter; + } if (n > psectors_inter && - (intermediate =3D=3D top || - sector_num + psectors_inter < intermediate->total_sectors)) { + (intermediate =3D=3D top || sector_num + psectors_inter < size= _inter)) { n =3D psectors_inter; } --=20 2.9.4 From nobody Sun Oct 5 19:15:27 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1499289402314322.2772290815011; Wed, 5 Jul 2017 14:16:42 -0700 (PDT) Received: from localhost ([::1]:48093 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSrf6-00032P-TE for importer@patchew.org; Wed, 05 Jul 2017 17:16:40 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38250) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSrYH-0005JD-3E for qemu-devel@nongnu.org; Wed, 05 Jul 2017 17:09:38 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dSrYF-0002sy-7a for qemu-devel@nongnu.org; Wed, 05 Jul 2017 17:09:37 -0400 Received: from mx1.redhat.com ([209.132.183.28]:36914) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dSrYB-0002rG-4T; Wed, 05 Jul 2017 17:09:31 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2C63561B83; Wed, 5 Jul 2017 21:09:30 +0000 (UTC) Received: from red.redhat.com (ovpn-120-36.rdu2.redhat.com [10.10.120.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id 033F1600CC; Wed, 5 Jul 2017 21:09:26 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 2C63561B83 Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=eblake@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 2C63561B83 From: Eric Blake To: qemu-devel@nongnu.org Date: Wed, 5 Jul 2017 16:08:42 -0500 Message-Id: <20170705210842.960-22-eblake@redhat.com> In-Reply-To: <20170705210842.960-1-eblake@redhat.com> References: <20170705210842.960-1-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Wed, 05 Jul 2017 21:09:30 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v4 21/21] block: Make bdrv_is_allocated_above() byte-based X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, Fam Zheng , qemu-block@nongnu.org, Wen Congyang , jcody@redhat.com, Max Reitz , Stefan Hajnoczi , jsnow@redhat.com, Xie Changlong Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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 signature of the function to use int64_t *pnum 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. Therefore, for the most part this patch is just the addition of scaling at the callers followed by inverse scaling at bdrv_is_allocated(). But some code, particularly stream_run(), gets a lot simpler because it no longer has to mess with sectors. For ease of review, bdrv_is_allocated() was tackled separately. Signed-off-by: Eric Blake Reviewed-by: John Snow Reviewed-by: Xie Changlong [replication= part] Reviewed-by: Jeff Cody --- v3-v4: no change v2: tweak function comments, favor bdrv_getlength() over ->total_sectors --- include/block/block.h | 2 +- block/commit.c | 20 ++++++++------------ block/io.c | 42 ++++++++++++++++++++---------------------- block/mirror.c | 5 ++++- block/replication.c | 17 ++++++++++++----- block/stream.c | 21 +++++++++------------ qemu-img.c | 10 +++++++--- 7 files changed, 61 insertions(+), 56 deletions(-) diff --git a/include/block/block.h b/include/block/block.h index d3e01fb..f0fdbe8 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -430,7 +430,7 @@ int64_t bdrv_get_block_status_above(BlockDriverState *b= s, int bdrv_is_allocated(BlockDriverState *bs, int64_t offset, int64_t bytes, int64_t *pnum); int bdrv_is_allocated_above(BlockDriverState *top, BlockDriverState *base, - int64_t sector_num, int nb_sectors, int *pnum); + int64_t offset, int64_t bytes, int64_t *pnum); bool bdrv_is_read_only(BlockDriverState *bs); bool bdrv_is_writable(BlockDriverState *bs); diff --git a/block/commit.c b/block/commit.c index 241aa95..774a8a5 100644 --- a/block/commit.c +++ b/block/commit.c @@ -146,7 +146,7 @@ static void coroutine_fn commit_run(void *opaque) int64_t offset; uint64_t delay_ns =3D 0; int ret =3D 0; - int n =3D 0; /* sectors */ + int64_t n =3D 0; /* bytes */ void *buf =3D NULL; int bytes_written =3D 0; int64_t base_len; @@ -171,7 +171,7 @@ static void coroutine_fn commit_run(void *opaque) buf =3D blk_blockalign(s->top, COMMIT_BUFFER_SIZE); - for (offset =3D 0; offset < s->common.len; offset +=3D n * BDRV_SECTOR= _SIZE) { + for (offset =3D 0; offset < s->common.len; offset +=3D n) { bool copy; /* Note that even when no rate limit is applied we need to yield @@ -183,15 +183,12 @@ static void coroutine_fn commit_run(void *opaque) } /* Copy if allocated above the base */ ret =3D bdrv_is_allocated_above(blk_bs(s->top), blk_bs(s->base), - offset / BDRV_SECTOR_SIZE, - COMMIT_BUFFER_SIZE / BDRV_SECTOR_SIZ= E, - &n); + offset, COMMIT_BUFFER_SIZE, &n); copy =3D (ret =3D=3D 1); - trace_commit_one_iteration(s, offset, n * BDRV_SECTOR_SIZE, ret); + trace_commit_one_iteration(s, offset, n, ret); if (copy) { - ret =3D commit_populate(s->top, s->base, offset, - n * BDRV_SECTOR_SIZE, buf); - bytes_written +=3D n * BDRV_SECTOR_SIZE; + ret =3D commit_populate(s->top, s->base, offset, n, buf); + bytes_written +=3D n; } if (ret < 0) { BlockErrorAction action =3D @@ -204,11 +201,10 @@ static void coroutine_fn commit_run(void *opaque) } } /* Publish progress */ - s->common.offset +=3D n * BDRV_SECTOR_SIZE; + s->common.offset +=3D n; if (copy && s->common.speed) { - delay_ns =3D ratelimit_calculate_delay(&s->limit, - n * BDRV_SECTOR_SIZE); + delay_ns =3D ratelimit_calculate_delay(&s->limit, n); } } diff --git a/block/io.c b/block/io.c index fb8d1c7..569c503 100644 --- a/block/io.c +++ b/block/io.c @@ -1931,54 +1931,52 @@ int coroutine_fn bdrv_is_allocated(BlockDriverState= *bs, int64_t offset, /* * Given an image chain: ... -> [BASE] -> [INTER1] -> [INTER2] -> [TOP] * - * Return true if the given sector is allocated in any image between - * BASE and TOP (inclusive). BASE can be NULL to check if the given - * sector is allocated in any image of the chain. Return false otherwise, + * Return true if the (prefix of the) given range is allocated in any image + * between BASE and TOP (inclusive). BASE can be NULL to check if the giv= en + * offset is allocated in any image of the chain. Return false otherwise, * or negative errno on failure. * - * 'pnum' is set to the number of sectors (including and immediately follo= wing - * the specified sector) that are known to be in the same - * allocated/unallocated state. + * 'pnum' is set to the number of bytes (including and immediately + * following the specified offset) that are known to be in the same + * allocated/unallocated state. Note that a subsequent call starting + * at 'offset + *pnum' may return the same allocation status (in other + * words, the result is not necessarily the maximum possible range); + * but 'pnum' will only be 0 when end of file is reached. * */ int bdrv_is_allocated_above(BlockDriverState *top, BlockDriverState *base, - int64_t sector_num, - int nb_sectors, int *pnum) + int64_t offset, int64_t bytes, int64_t *pnum) { BlockDriverState *intermediate; - int ret, n =3D nb_sectors; + int ret; + int64_t n =3D bytes; intermediate =3D top; while (intermediate && intermediate !=3D base) { int64_t pnum_inter; int64_t size_inter; - int psectors_inter; - ret =3D bdrv_is_allocated(intermediate, sector_num * BDRV_SECTOR_S= IZE, - nb_sectors * BDRV_SECTOR_SIZE, - &pnum_inter); + ret =3D bdrv_is_allocated(intermediate, offset, bytes, &pnum_inter= ); if (ret < 0) { return ret; } - assert(pnum_inter < INT_MAX * BDRV_SECTOR_SIZE); - psectors_inter =3D pnum_inter >> BDRV_SECTOR_BITS; if (ret) { - *pnum =3D psectors_inter; + *pnum =3D pnum_inter; return 1; } /* - * [sector_num, nb_sectors] is unallocated on top but intermediate - * might have [sector_num+x, nb_sectors-x] allocated. + * [offset, bytes] is unallocated on top but intermediate + * might have [offset+x, bytes-x] allocated. */ - size_inter =3D bdrv_nb_sectors(intermediate); + size_inter =3D bdrv_getlength(intermediate); if (size_inter < 0) { return size_inter; } - if (n > psectors_inter && - (intermediate =3D=3D top || sector_num + psectors_inter < size= _inter)) { - n =3D psectors_inter; + if (n > pnum_inter && + (intermediate =3D=3D top || offset + pnum_inter < size_inter))= { + n =3D pnum_inter; } intermediate =3D backing_bs(intermediate); diff --git a/block/mirror.c b/block/mirror.c index f54a8d7..c717f60 100644 --- a/block/mirror.c +++ b/block/mirror.c @@ -621,6 +621,7 @@ static int coroutine_fn mirror_dirty_init(MirrorBlockJo= b *s) BlockDriverState *bs =3D s->source; BlockDriverState *target_bs =3D blk_bs(s->target); int ret, n; + int64_t count; end =3D s->bdev_length / BDRV_SECTOR_SIZE; @@ -670,11 +671,13 @@ static int coroutine_fn mirror_dirty_init(MirrorBlock= Job *s) return 0; } - ret =3D bdrv_is_allocated_above(bs, base, sector_num, nb_sectors, = &n); + ret =3D bdrv_is_allocated_above(bs, base, sector_num * BDRV_SECTOR= _SIZE, + nb_sectors * BDRV_SECTOR_SIZE, &coun= t); if (ret < 0) { return ret; } + n =3D DIV_ROUND_UP(count, BDRV_SECTOR_SIZE); assert(n > 0); if (ret =3D=3D 1) { bdrv_set_dirty_bitmap(s->dirty_bitmap, sector_num, n); diff --git a/block/replication.c b/block/replication.c index 8f3aba7..bf4462c 100644 --- a/block/replication.c +++ b/block/replication.c @@ -264,7 +264,8 @@ static coroutine_fn int replication_co_writev(BlockDriv= erState *bs, BdrvChild *top =3D bs->file; BdrvChild *base =3D s->secondary_disk; BdrvChild *target; - int ret, n; + int ret; + int64_t n; ret =3D replication_get_io_status(s); if (ret < 0) { @@ -283,14 +284,20 @@ static coroutine_fn int replication_co_writev(BlockDr= iverState *bs, */ qemu_iovec_init(&hd_qiov, qiov->niov); while (remaining_sectors > 0) { - ret =3D bdrv_is_allocated_above(top->bs, base->bs, sector_num, - remaining_sectors, &n); + int64_t count; + + ret =3D bdrv_is_allocated_above(top->bs, base->bs, + sector_num * BDRV_SECTOR_SIZE, + remaining_sectors * BDRV_SECTOR_SIZE, + &count); if (ret < 0) { goto out1; } + assert(QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE)); + n =3D count >> BDRV_SECTOR_BITS; qemu_iovec_reset(&hd_qiov); - qemu_iovec_concat(&hd_qiov, qiov, bytes_done, n * BDRV_SECTOR_SIZE= ); + qemu_iovec_concat(&hd_qiov, qiov, bytes_done, count); target =3D ret ? top : base; ret =3D bdrv_co_writev(target, sector_num, n, &hd_qiov); @@ -300,7 +307,7 @@ static coroutine_fn int replication_co_writev(BlockDriv= erState *bs, remaining_sectors -=3D n; sector_num +=3D n; - bytes_done +=3D n * BDRV_SECTOR_SIZE; + bytes_done +=3D count; } out1: diff --git a/block/stream.c b/block/stream.c index e5f2a08..e6f7234 100644 --- a/block/stream.c +++ b/block/stream.c @@ -111,7 +111,7 @@ static void coroutine_fn stream_run(void *opaque) uint64_t delay_ns =3D 0; int error =3D 0; int ret =3D 0; - int n =3D 0; /* sectors */ + int64_t n =3D 0; /* bytes */ void *buf; if (!bs->backing) { @@ -135,9 +135,8 @@ static void coroutine_fn stream_run(void *opaque) bdrv_enable_copy_on_read(bs); } - for ( ; offset < s->common.len; offset +=3D n * BDRV_SECTOR_SIZE) { + for ( ; offset < s->common.len; offset +=3D n) { bool copy; - int64_t count =3D 0; /* Note that even when no rate limit is applied we need to yield * with no pending I/O here so that bdrv_drain_all() returns. @@ -149,26 +148,25 @@ static void coroutine_fn stream_run(void *opaque) copy =3D false; - ret =3D bdrv_is_allocated(bs, offset, STREAM_BUFFER_SIZE, &count); - n =3D DIV_ROUND_UP(count, BDRV_SECTOR_SIZE); + ret =3D bdrv_is_allocated(bs, offset, STREAM_BUFFER_SIZE, &n); if (ret =3D=3D 1) { /* Allocated in the top, no need to copy. */ } else if (ret >=3D 0) { /* Copy if allocated in the intermediate images. Limit to the * known-unallocated area [offset, offset+n*BDRV_SECTOR_SIZE).= */ ret =3D bdrv_is_allocated_above(backing_bs(bs), base, - offset / BDRV_SECTOR_SIZE, n, &n= ); + offset, n, &n); /* Finish early if end of backing file has been reached */ if (ret =3D=3D 0 && n =3D=3D 0) { - n =3D (s->common.len - offset) / BDRV_SECTOR_SIZE; + n =3D s->common.len - offset; } copy =3D (ret =3D=3D 1); } - trace_stream_one_iteration(s, offset, n * BDRV_SECTOR_SIZE, ret); + trace_stream_one_iteration(s, offset, n, ret); if (copy) { - ret =3D stream_populate(blk, offset, n * BDRV_SECTOR_SIZE, buf= ); + ret =3D stream_populate(blk, offset, n, buf); } if (ret < 0) { BlockErrorAction action =3D @@ -187,10 +185,9 @@ static void coroutine_fn stream_run(void *opaque) ret =3D 0; /* Publish progress */ - s->common.offset +=3D n * BDRV_SECTOR_SIZE; + s->common.offset +=3D n; if (copy && s->common.speed) { - delay_ns =3D ratelimit_calculate_delay(&s->limit, - n * BDRV_SECTOR_SIZE); + delay_ns =3D ratelimit_calculate_delay(&s->limit, n); } } diff --git a/qemu-img.c b/qemu-img.c index 5271b41..960f42a 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -1477,12 +1477,16 @@ static int img_compare(int argc, char **argv) } for (;;) { + int64_t count; + nb_sectors =3D sectors_to_process(total_sectors_over, sector_n= um); if (nb_sectors <=3D 0) { break; } - ret =3D bdrv_is_allocated_above(blk_bs(blk_over), NULL, sector= _num, - nb_sectors, &pnum); + ret =3D bdrv_is_allocated_above(blk_bs(blk_over), NULL, + sector_num * BDRV_SECTOR_SIZE, + nb_sectors * BDRV_SECTOR_SIZE, + &count); if (ret < 0) { ret =3D 3; error_report("Sector allocation test failed for %s", @@ -1490,7 +1494,7 @@ static int img_compare(int argc, char **argv) goto out; } - nb_sectors =3D pnum; + nb_sectors =3D DIV_ROUND_UP(count, BDRV_SECTOR_SIZE); if (ret) { ret =3D check_empty_sectors(blk_over, sector_num, nb_secto= rs, filename_over, buf1, quiet); --=20 2.9.4