From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([140.186.70.92]:46640) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RXZNj-0001Xp-U2 for qemu-devel@nongnu.org; Mon, 05 Dec 2011 09:19:01 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RXZNf-0000Nq-Ow for qemu-devel@nongnu.org; Mon, 05 Dec 2011 09:18:59 -0500 Received: from mx1.redhat.com ([209.132.183.28]:22468) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RXZNf-0000Nc-3r for qemu-devel@nongnu.org; Mon, 05 Dec 2011 09:18:55 -0500 From: Kevin Wolf Date: Mon, 5 Dec 2011 15:21:16 +0100 Message-Id: <1323094878-7967-40-git-send-email-kwolf@redhat.com> In-Reply-To: <1323094878-7967-1-git-send-email-kwolf@redhat.com> References: <1323094878-7967-1-git-send-email-kwolf@redhat.com> Subject: [Qemu-devel] [PATCH 39/41] block: implement bdrv_co_is_allocated() boundary cases List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: anthony@codemonkey.ws Cc: kwolf@redhat.com, qemu-devel@nongnu.org From: Stefan Hajnoczi Cases beyond the end of the disk image are only implemented for block drivers that do not provide .bdrv_co_is_allocated(). It's worth making these cases generic so that block drivers that do implement .bdrv_co_is_allocated() also get them for free. Suggested-by: Mark Wu Signed-off-by: Stefan Hajnoczi Signed-off-by: Kevin Wolf --- block.c | 26 ++++++++++++++++++-------- 1 files changed, 18 insertions(+), 8 deletions(-) diff --git a/block.c b/block.c index dd5d5ca..ecc5b44 100644 --- a/block.c +++ b/block.c @@ -2117,23 +2117,33 @@ typedef struct BdrvCoIsAllocatedData { * 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 + * 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. * - * 'nb_sectors' is the max value 'pnum' should be set to. + * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes + * beyond the end of the disk image it will be clamped. */ int coroutine_fn bdrv_co_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, int *pnum) { + int64_t n; + + if (sector_num >= bs->total_sectors) { + *pnum = 0; + return 0; + } + + n = bs->total_sectors - sector_num; + if (n < nb_sectors) { + nb_sectors = n; + } + if (!bs->drv->bdrv_co_is_allocated) { - int64_t n; - if (sector_num >= bs->total_sectors) { - *pnum = 0; - return 0; - } - n = bs->total_sectors - sector_num; - *pnum = (n < nb_sectors) ? (n) : (nb_sectors); + *pnum = nb_sectors; return 1; } -- 1.7.6.4