From patchwork Tue Nov 29 13:49:51 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 128290 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id B1753B6F7F for ; Wed, 30 Nov 2011 00:50:24 +1100 (EST) Received: from localhost ([::1]:33265 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RVO4j-0001mZ-H3 for incoming@patchwork.ozlabs.org; Tue, 29 Nov 2011 08:50:21 -0500 Received: from eggs.gnu.org ([140.186.70.92]:50237) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RVO4d-0001mU-0n for qemu-devel@nongnu.org; Tue, 29 Nov 2011 08:50:16 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RVO4X-0003VC-72 for qemu-devel@nongnu.org; Tue, 29 Nov 2011 08:50:14 -0500 Received: from e06smtp11.uk.ibm.com ([195.75.94.107]:56046) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RVO4W-0003U0-Uw for qemu-devel@nongnu.org; Tue, 29 Nov 2011 08:50:09 -0500 Received: from /spool/local by e06smtp11.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 29 Nov 2011 13:50:07 -0000 Received: from d06nrmr1707.portsmouth.uk.ibm.com ([9.149.39.225]) by e06smtp11.uk.ibm.com ([192.168.101.141]) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Tue, 29 Nov 2011 13:50:00 -0000 Received: from d06av08.portsmouth.uk.ibm.com (d06av08.portsmouth.uk.ibm.com [9.149.37.249]) by d06nrmr1707.portsmouth.uk.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id pATDo0Cq2564100 for ; Tue, 29 Nov 2011 13:50:00 GMT Received: from d06av08.portsmouth.uk.ibm.com (loopback [127.0.0.1]) by d06av08.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id pATDnxUl027105 for ; Tue, 29 Nov 2011 13:50:00 GMT Received: from localhost (stefanha-thinkpad.manchester-maybrook.uk.ibm.com [9.174.219.31]) by d06av08.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id pATDnwBc027025; Tue, 29 Nov 2011 13:49:58 GMT From: Stefan Hajnoczi To: Date: Tue, 29 Nov 2011 13:49:51 +0000 Message-Id: <1322574591-28496-1-git-send-email-stefanha@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.7.3 x-cbid: 11112913-5024-0000-0000-000000E5970B X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 195.75.94.107 Cc: Kevin Wolf , Mark Wu , Stefan Hajnoczi Subject: [Qemu-devel] [PATCH] block: implement bdrv_co_is_allocated() boundary cases X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 --- block.c | 26 ++++++++++++++++++-------- 1 files changed, 18 insertions(+), 8 deletions(-) diff --git a/block.c b/block.c index 4310eb5..1e468a7 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; }