From patchwork Wed Sep 13 16:03:31 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Blake X-Patchwork-Id: 813523 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=208.118.235.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) 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 ozlabs.org (Postfix) with ESMTPS id 3xsn0z54v1z9s72 for ; Thu, 14 Sep 2017 02:19:15 +1000 (AEST) Received: from localhost ([::1]:43427 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dsANd-0000k1-Kb for incoming@patchwork.ozlabs.org; Wed, 13 Sep 2017 12:19:13 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46522) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dsAAT-00030y-RA for qemu-devel@nongnu.org; Wed, 13 Sep 2017 12:05:39 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dsAAS-0006zr-63 for qemu-devel@nongnu.org; Wed, 13 Sep 2017 12:05:37 -0400 Received: from mx1.redhat.com ([209.132.183.28]:39362) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dsAAM-0006xD-T0; Wed, 13 Sep 2017 12:05: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 F098881DE3; Wed, 13 Sep 2017 16:05:29 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com F098881DE3 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=fail smtp.mailfrom=eblake@redhat.com Received: from red.redhat.com (ovpn-120-201.rdu2.redhat.com [10.10.120.201]) by smtp.corp.redhat.com (Postfix) with ESMTP id B52AB69FAE; Wed, 13 Sep 2017 16:05:04 +0000 (UTC) From: Eric Blake To: qemu-devel@nongnu.org Date: Wed, 13 Sep 2017 11:03:31 -0500 Message-Id: <20170913160333.23622-22-eblake@redhat.com> In-Reply-To: <20170913160333.23622-1-eblake@redhat.com> References: <20170913160333.23622-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, 13 Sep 2017 16:05: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/23] block: Align block status requests 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, famz@redhat.com, qemu-block@nongnu.org, Max Reitz , Stefan Hajnoczi , jsnow@redhat.com Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" Any device that has request_alignment greater than 512 should be unable to report status at a finer granularity; it may also be simpler for such devices to be guaranteed that the block layer has rounded things out to the granularity boundary (the way the block layer already rounds all other I/O out). Besides, getting the code correct for super-sector alignment also benefits us for the fact that our public interface now has byte granularity, even though none of our drivers have byte-level callbacks. Add an assertion in blkdebug that proves that the block layer never requests status of unaligned sections, similar to what it does on other requests (while still keeping the generic helper in place for when future patches add a throttle driver). Note that iotest 177 already covers this (it would fail if you use just the blkdebug.c hunk without the io.c changes). Meanwhile, we can drop assertions in callers that no longer have to pass in sector-aligned addresses. There is a mid-function scope added for 'int count', for a couple of reasons: first, an upcoming patch will add an 'if' statement that checks whether a driver has an old- or new-style callback, and can conveniently use the same scope for less indentation churn at that time. Second, since we are trying to get rid of sector-based computations, wrapping things in a scope makes it easier to group and see what will be deleted in a final cleanup patch once all drivers have been converted to the new-style callback. Signed-off-by: Eric Blake --- v3: tweak commit message [Fam], rebase to context conflicts, ensure we don't exceed 32-bit limit, drop R-b v2: new patch --- include/block/block_int.h | 3 ++- block/io.c | 55 +++++++++++++++++++++++++++++++---------------- block/blkdebug.c | 13 ++++++++++- 3 files changed, 51 insertions(+), 20 deletions(-) diff --git a/include/block/block_int.h b/include/block/block_int.h index 7f71c585a0..b1ceffba78 100644 --- a/include/block/block_int.h +++ b/include/block/block_int.h @@ -207,7 +207,8 @@ struct BlockDriver { * according to the current layer, and should not set * BDRV_BLOCK_ALLOCATED, but may set BDRV_BLOCK_RAW. See block.h * for the meaning of _DATA, _ZERO, and _OFFSET_VALID. The block - * layer guarantees non-NULL pnum and file. + * layer guarantees input aligned to request_alignment, as well as + * non-NULL pnum and file. */ int64_t coroutine_fn (*bdrv_co_get_block_status)(BlockDriverState *bs, int64_t sector_num, int nb_sectors, int *pnum, diff --git a/block/io.c b/block/io.c index ea63d19480..c78201b8eb 100644 --- a/block/io.c +++ b/block/io.c @@ -1773,7 +1773,8 @@ static int64_t coroutine_fn bdrv_co_block_status(BlockDriverState *bs, int64_t n; /* bytes */ int64_t ret, ret2; BlockDriverState *local_file = NULL; - int count; /* sectors */ + int64_t aligned_offset, aligned_bytes; + uint32_t align; assert(pnum); total_size = bdrv_getlength(bs); @@ -1815,28 +1816,45 @@ static int64_t coroutine_fn bdrv_co_block_status(BlockDriverState *bs, } bdrv_inc_in_flight(bs); - /* - * 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. - */ - assert(QEMU_IS_ALIGNED(offset | bytes, BDRV_SECTOR_SIZE)); - bytes = MIN(bytes, BDRV_REQUEST_MAX_BYTES); - ret = bs->drv->bdrv_co_get_block_status(bs, offset >> BDRV_SECTOR_BITS, - bytes >> BDRV_SECTOR_BITS, &count, - &local_file); - if (ret < 0) { - *pnum = 0; - goto out; + + /* Round out to request_alignment boundaries */ + align = MAX(bs->bl.request_alignment, BDRV_SECTOR_SIZE); + aligned_offset = QEMU_ALIGN_DOWN(offset, align); + aligned_bytes = ROUND_UP(offset + bytes, align) - aligned_offset; + + { + int count; /* sectors */ + + assert(QEMU_IS_ALIGNED(aligned_offset | aligned_bytes, + BDRV_SECTOR_SIZE)); + ret = bs->drv->bdrv_co_get_block_status( + bs, aligned_offset >> BDRV_SECTOR_BITS, + MIN(INT_MAX, aligned_bytes) >> BDRV_SECTOR_BITS, &count, + &local_file); + if (ret < 0) { + *pnum = 0; + goto out; + } + *pnum = count * BDRV_SECTOR_SIZE; + } + + /* Clamp pnum and ret to original request */ + assert(QEMU_IS_ALIGNED(*pnum, align)); + *pnum -= offset - aligned_offset; + if (aligned_offset >> BDRV_SECTOR_BITS != offset >> BDRV_SECTOR_BITS && + ret & BDRV_BLOCK_OFFSET_VALID) { + ret += QEMU_ALIGN_DOWN(offset - aligned_offset, BDRV_SECTOR_SIZE); + } + if (*pnum > bytes) { + *pnum = bytes; } - *pnum = count * BDRV_SECTOR_SIZE; if (ret & BDRV_BLOCK_RAW) { assert(ret & BDRV_BLOCK_OFFSET_VALID && local_file); ret = bdrv_co_block_status(local_file, mapping, - ret & BDRV_BLOCK_OFFSET_MASK, + (ret & BDRV_BLOCK_OFFSET_MASK) | + (offset & ~BDRV_BLOCK_OFFSET_MASK), *pnum, pnum, &local_file); - assert(QEMU_IS_ALIGNED(*pnum, BDRV_SECTOR_SIZE)); goto out; } @@ -1860,7 +1878,8 @@ static int64_t coroutine_fn bdrv_co_block_status(BlockDriverState *bs, int64_t file_pnum; ret2 = bdrv_co_block_status(local_file, mapping, - ret & BDRV_BLOCK_OFFSET_MASK, + (ret & BDRV_BLOCK_OFFSET_MASK) | + (offset & ~BDRV_BLOCK_OFFSET_MASK), *pnum, &file_pnum, NULL); if (ret2 >= 0) { /* Ignore errors. This is just providing extra information, it diff --git a/block/blkdebug.c b/block/blkdebug.c index 46e53f2f09..f54fe33cae 100644 --- a/block/blkdebug.c +++ b/block/blkdebug.c @@ -628,6 +628,17 @@ static int coroutine_fn blkdebug_co_pdiscard(BlockDriverState *bs, return bdrv_co_pdiscard(bs->file->bs, offset, bytes); } +static int64_t coroutine_fn blkdebug_co_get_block_status( + BlockDriverState *bs, int64_t sector_num, int nb_sectors, int *pnum, + BlockDriverState **file) +{ + assert(QEMU_IS_ALIGNED(sector_num | nb_sectors, + DIV_ROUND_UP(bs->bl.request_alignment, + BDRV_SECTOR_SIZE))); + return bdrv_co_get_block_status_from_file(bs, sector_num, nb_sectors, + pnum, file); +} + static void blkdebug_close(BlockDriverState *bs) { BDRVBlkdebugState *s = bs->opaque; @@ -897,7 +908,7 @@ static BlockDriver bdrv_blkdebug = { .bdrv_co_flush_to_disk = blkdebug_co_flush, .bdrv_co_pwrite_zeroes = blkdebug_co_pwrite_zeroes, .bdrv_co_pdiscard = blkdebug_co_pdiscard, - .bdrv_co_get_block_status = bdrv_co_get_block_status_from_file, + .bdrv_co_get_block_status = blkdebug_co_get_block_status, .bdrv_debug_event = blkdebug_debug_event, .bdrv_debug_breakpoint = blkdebug_debug_breakpoint,