bdrv_max_size() provides a conservative maximum for the size of a new image. This information is handy if storage needs to be allocated (e.g. a SAN or an LVM volume) ahead of time.
Signed-off-by: Stefan Hajnoczi <stefa...@redhat.com> --- include/block/block.h | 2 ++ include/block/block_int.h | 2 ++ block.c | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/include/block/block.h b/include/block/block.h index c7c4a3a..12e1532 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -298,6 +298,8 @@ int bdrv_truncate(BdrvChild *child, int64_t offset); int64_t bdrv_nb_sectors(BlockDriverState *bs); int64_t bdrv_getlength(BlockDriverState *bs); int64_t bdrv_get_allocated_file_size(BlockDriverState *bs); +uint64_t bdrv_max_size(BlockDriver *drv, QemuOpts *opts, + BlockDriverState *in_bs, Error **errp); void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr); void bdrv_refresh_limits(BlockDriverState *bs, Error **errp); int bdrv_commit(BlockDriverState *bs); diff --git a/include/block/block_int.h b/include/block/block_int.h index a57c0bf..0354e22 100644 --- a/include/block/block_int.h +++ b/include/block/block_int.h @@ -201,6 +201,8 @@ struct BlockDriver { int64_t (*bdrv_getlength)(BlockDriverState *bs); bool has_variable_length; int64_t (*bdrv_get_allocated_file_size)(BlockDriverState *bs); + uint64_t (*bdrv_max_size)(QemuOpts *opts, BlockDriverState *in_bs, + Error **errp); int coroutine_fn (*bdrv_co_pwritev_compressed)(BlockDriverState *bs, uint64_t offset, uint64_t bytes, QEMUIOVector *qiov); diff --git a/block.c b/block.c index f293ccb..4a228f0 100644 --- a/block.c +++ b/block.c @@ -3188,6 +3188,43 @@ int64_t bdrv_get_allocated_file_size(BlockDriverState *bs) return -ENOTSUP; } +/* + * bdrv_max_size: + * @drv: Format driver + * @opts: Creation options + * @in_bs: Existing image containing data for new image (may be NULL) + * @errp: Error object + * + * Calculate maximum file size required by a new image. The value is + * conservative so actual creation of the image never requires more space than + * what is returned by this function. + * + * Note that this value is the file size including sparse regions, not the + * allocated file size. A new 5 GB raw file therefore has a maximum size of 5 + * GB, not 0! + * + * If @in_bs is given then space for allocated clusters and zero clusters + * from that image are included in the calculation. If @opts contains a + * backing file that is shared by @in_bs then backing clusters are omitted + * from the calculation. + * + * If @in_bs is NULL then the calculation includes no allocated clusters + * unless a preallocation option is given in @opts. + * + * Returns the maximum size for the new image or 0 on error. + */ +uint64_t bdrv_max_size(BlockDriver *drv, QemuOpts *opts, + BlockDriverState *in_bs, Error **errp) +{ + if (!drv->bdrv_max_size) { + error_setg(errp, "Block driver '%s' does not support max size " + "calculation", drv->format_name); + return 0; + } + + return drv->bdrv_max_size(opts, in_bs, errp); +} + /** * Return number of sectors on success, -errno on error. */ -- 2.9.3