Signed-off-by: Peter Lieven <p...@kamp.de> --- block.c | 29 +++++++++++++++++++++++++++++ include/block/block.h | 2 ++ include/block/block_int.h | 13 +++++++++++++ 3 files changed, 44 insertions(+)
diff --git a/block.c b/block.c index 6f498fc..177720e 100644 --- a/block.c +++ b/block.c @@ -3045,6 +3045,35 @@ int bdrv_has_zero_init(BlockDriverState *bs) return 0; } +int bdrv_has_discard_zeroes(BlockDriverState *bs) +{ + assert(bs->drv); + + if (bs->backing_hd) { + return 0; + } + if (bs->drv->bdrv_has_discard_zeroes) { + return bs->drv->bdrv_has_discard_zeroes(bs); + } + + return 0; +} + +int bdrv_has_discard_write_zeroes(BlockDriverState *bs) +{ + assert(bs->drv); + + if (bs->backing_hd || !(bs->open_flags & BDRV_O_UNMAP)) { + return 0; + } + + if (bs->drv->bdrv_has_discard_write_zeroes) { + return bs->drv->bdrv_has_discard_write_zeroes(bs); + } + + return 0; +} + typedef struct BdrvCoGetBlockStatusData { BlockDriverState *bs; BlockDriverState *base; diff --git a/include/block/block.h b/include/block/block.h index 0328234..8aa325a 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -307,6 +307,8 @@ int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors); int bdrv_co_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors); int bdrv_has_zero_init_1(BlockDriverState *bs); int bdrv_has_zero_init(BlockDriverState *bs); +int bdrv_has_discard_zeroes(BlockDriverState *bs); +int bdrv_has_discard_write_zeroes(BlockDriverState *bs); int64_t bdrv_get_block_status(BlockDriverState *bs, int64_t sector_num, int nb_sectors, int *pnum); int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, diff --git a/include/block/block_int.h b/include/block/block_int.h index 26bad36..85c3474 100644 --- a/include/block/block_int.h +++ b/include/block/block_int.h @@ -202,6 +202,19 @@ struct BlockDriver { */ int (*bdrv_has_zero_init)(BlockDriverState *bs); + /* + * Returns 1 if discarded blocks read back as zeroes, 0 otherwise. + */ + int (*bdrv_has_discard_zeroes)(BlockDriverState *bs); + + /* + * Returns 1 if the driver can optimize writing zeroes by discarding + * sectors. It is additionally required that the block device is + * opened with BDRV_O_UNMAP and the that write zeroes request carries + * the BDRV_REQ_MAY_UNMAP flag for this to work. + */ + int (*bdrv_has_discard_write_zeroes)(BlockDriverState *bs); + QLIST_ENTRY(BlockDriver) list; }; -- 1.7.9.5