block: implement bdrv_co_is_allocated() boundary cases

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 <wudxw@linux.vnet.ibm.com>
Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
This commit is contained in:
Stefan Hajnoczi 2011-11-29 13:49:51 +00:00 committed by Kevin Wolf
parent c57c465800
commit bd9533e36e
1 changed files with 18 additions and 8 deletions

26
block.c
View File

@ -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;
}