From patchwork Tue Apr 18 01:33:32 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Blake X-Patchwork-Id: 9684817 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 3776D602C2 for ; Tue, 18 Apr 2017 01:43:13 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1D76B25223 for ; Tue, 18 Apr 2017 01:43:13 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0EFBC27C0B; Tue, 18 Apr 2017 01:43:13 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 4740425223 for ; Tue, 18 Apr 2017 01:43:12 +0000 (UTC) Received: from localhost ([::1]:39519 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d0IAh-0003E3-Gi for patchwork-qemu-devel@patchwork.kernel.org; Mon, 17 Apr 2017 21:43:11 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33960) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d0I27-00059W-79 for qemu-devel@nongnu.org; Mon, 17 Apr 2017 21:34:20 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d0I26-0003fV-10 for qemu-devel@nongnu.org; Mon, 17 Apr 2017 21:34:19 -0400 Received: from mx1.redhat.com ([209.132.183.28]:40130) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1d0I23-0003aG-5O; Mon, 17 Apr 2017 21:34:15 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 205FD7F3F5; Tue, 18 Apr 2017 01:34:14 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 205FD7F3F5 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 205FD7F3F5 Received: from red.redhat.com (ovpn-121-102.rdu2.redhat.com [10.10.121.102]) by smtp.corp.redhat.com (Postfix) with ESMTP id E69687835E; Tue, 18 Apr 2017 01:34:12 +0000 (UTC) From: Eric Blake To: qemu-devel@nongnu.org Date: Mon, 17 Apr 2017 20:33:32 -0500 Message-Id: <20170418013356.3578-8-eblake@redhat.com> In-Reply-To: <20170418013356.3578-1-eblake@redhat.com> References: <20170418013356.3578-1-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Tue, 18 Apr 2017 01:34:14 +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 07/31] block: Switch bdrv_co_get_block_status() 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: Kevin Wolf , Fam Zheng , qemu-block@nongnu.org, Max Reitz , Stefan Hajnoczi , kwolf@nongnu.org, jsnow@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP 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 as with its public counterpart, rename to bdrv_co_block_status() to make the compiler enforce that we catch all uses. For now, we assert that callers still pass aligned data, but ultimately, this will be the function where we hand off to a byte-based driver callback, and will eventually need to add logic to ensure we round calls according to the driver's request_alignment then touch up the result handed back to the caller, if we start permitting a caller to pass unaligned offsets. While at it, adjust the function to accept NULL for pnum or file, while still guaranteeing the driver callback has a non-NULL pointer to write into. Signed-off-by: Eric Blake --- block/io.c | 88 +++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 53 insertions(+), 35 deletions(-) diff --git a/block/io.c b/block/io.c index 5cdb1f0..e804bdd 100644 --- a/block/io.c +++ b/block/io.c @@ -1696,69 +1696,83 @@ typedef struct BdrvCoGetBlockStatusData { * Drivers not implementing the functionality are assumed to not support * backing files, hence all their sectors are reported as allocated. * - * If 'sector_num' is beyond the end of the disk image the return value is 0 + * If 'offset' is beyond the end of the disk image the return value is 0 * and 'pnum' is set to 0. * - * 'pnum' is set to the number of sectors (including and immediately following - * 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. It may be NULL. * - * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes + * 'bytes' is the max value 'pnum' should be set to. If bytes goes * beyond the end of the disk image it will be clamped. * - * If returned value is positive and BDRV_BLOCK_OFFSET_VALID bit is set, 'file' - * points to the BDS which the sector range is allocated in. + * If returned value is positive and BDRV_BLOCK_OFFSET_VALID bit is set, and + * 'file' is not NULL, then *file points to the BDS which owns the allocated + * sector that contains offset. */ -static int64_t coroutine_fn bdrv_co_get_block_status(BlockDriverState *bs, - int64_t sector_num, - int nb_sectors, int *pnum, - BlockDriverState **file) +static int64_t coroutine_fn bdrv_co_block_status(BlockDriverState *bs, + int64_t offset, int64_t bytes, + int64_t *pnum, + BlockDriverState **file) { - int64_t total_sectors; - int64_t n; + int64_t total_size; + int64_t n; /* bytes */ int64_t ret, ret2; + int count; /* sectors */ + BlockDriverState *tmp_file; - total_sectors = bdrv_nb_sectors(bs); - if (total_sectors < 0) { - return total_sectors; + total_size = bdrv_getlength(bs); + if (total_size < 0) { + return total_size; } - if (sector_num >= total_sectors) { + if (!pnum) { + pnum = &n; + } + if (offset >= total_size) { *pnum = 0; return 0; } - n = total_sectors - sector_num; - if (n < nb_sectors) { - nb_sectors = n; + n = total_size - offset; + if (n < bytes) { + bytes = n; } if (!bs->drv->bdrv_co_get_block_status) { - *pnum = nb_sectors; + *pnum = bytes; ret = BDRV_BLOCK_DATA | BDRV_BLOCK_ALLOCATED; if (bs->drv->protocol_name) { - ret |= BDRV_BLOCK_OFFSET_VALID | (sector_num * BDRV_SECTOR_SIZE); + ret |= BDRV_BLOCK_OFFSET_VALID | (offset & BDRV_BLOCK_OFFSET_MASK); } return ret; } + if (!file) { + file = &tmp_file; + } *file = NULL; bdrv_inc_in_flight(bs); - ret = bs->drv->bdrv_co_get_block_status(bs, sector_num, nb_sectors, pnum, + /* TODO: Rather than require aligned offsets, we could instead + * round to the driver's request_alignment here, then touch up + * count afterwards back to the caller's expectations. But first + * we want to switch the driver callback to likewise be + * byte-based. */ + assert(QEMU_IS_ALIGNED(offset | bytes, BDRV_SECTOR_SIZE)); + ret = bs->drv->bdrv_co_get_block_status(bs, offset >> BDRV_SECTOR_BITS, + bytes >> BDRV_SECTOR_BITS, &count, file); if (ret < 0) { *pnum = 0; goto out; } + *pnum = count * BDRV_SECTOR_SIZE; if (ret & BDRV_BLOCK_RAW) { - int64_t bytes = *pnum * BDRV_SECTOR_SIZE; - assert(ret & BDRV_BLOCK_OFFSET_VALID); ret = bdrv_block_status(*file, ret & BDRV_BLOCK_OFFSET_MASK, - bytes, &bytes, file); - assert(QEMU_IS_ALIGNED(bytes, BDRV_SECTOR_SIZE)); - *pnum = bytes >> BDRV_SECTOR_BITS; + *pnum, pnum, file); + assert(QEMU_IS_ALIGNED(*pnum, BDRV_SECTOR_SIZE)); goto out; } @@ -1769,8 +1783,8 @@ static int64_t coroutine_fn bdrv_co_get_block_status(BlockDriverState *bs, ret |= BDRV_BLOCK_ZERO; } else if (bs->backing) { BlockDriverState *bs2 = bs->backing->bs; - int64_t nb_sectors2 = bdrv_nb_sectors(bs2); - if (nb_sectors2 >= 0 && sector_num >= nb_sectors2) { + int64_t size2 = bdrv_getlength(bs2); + if (size2 >= 0 && offset >= size2) { ret |= BDRV_BLOCK_ZERO; } } @@ -1779,11 +1793,10 @@ static int64_t coroutine_fn bdrv_co_get_block_status(BlockDriverState *bs, if (*file && *file != bs && (ret & BDRV_BLOCK_DATA) && !(ret & BDRV_BLOCK_ZERO) && (ret & BDRV_BLOCK_OFFSET_VALID)) { - BlockDriverState *file2; - int file_pnum; + int64_t file_pnum; - ret2 = bdrv_co_get_block_status(*file, ret >> BDRV_SECTOR_BITS, - *pnum, &file_pnum, &file2); + ret2 = bdrv_co_block_status(*file, ret & BDRV_BLOCK_OFFSET_MASK, + *pnum, &file_pnum, NULL); if (ret2 >= 0) { /* Ignore errors. This is just providing extra information, it * is useful but not necessary. @@ -1818,7 +1831,12 @@ static int64_t coroutine_fn bdrv_co_get_block_status_above(BlockDriverState *bs, assert(bs != base); for (p = bs; p != base; p = backing_bs(p)) { - ret = bdrv_co_get_block_status(p, sector_num, nb_sectors, pnum, file); + int64_t count; + + ret = bdrv_co_block_status(p, sector_num * BDRV_SECTOR_SIZE, + nb_sectors * BDRV_SECTOR_SIZE, &count, + file); + *pnum = count >> BDRV_SECTOR_BITS; if (ret < 0 || ret & BDRV_BLOCK_ALLOCATED) { break; }