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 e7bf6af..ac35cb5 100644 --- a/block.c +++ b/block.c @@ -3101,6 +3101,35 @@ int bdrv_has_zero_init(BlockDriverState *bs) return 0; } +bool bdrv_has_discard_zeroes(BlockDriverState *bs) +{ + assert(bs->drv); + + if (bs->backing_hd) { + return false; + } + if (bs->drv->bdrv_has_discard_zeroes) { + return bs->drv->bdrv_has_discard_zeroes(bs); + } + + return false; +} + +bool bdrv_has_discard_write_zeroes(BlockDriverState *bs) +{ + assert(bs->drv); + + if (bs->backing_hd || !(bs->open_flags & BDRV_O_UNMAP)) { + return false; + } + + if (bs->drv->bdrv_has_discard_write_zeroes) { + return bs->drv->bdrv_has_discard_write_zeroes(bs); + } + + return false; +} + typedef struct BdrvCoGetBlockStatusData { BlockDriverState *bs; BlockDriverState *base; diff --git a/include/block/block.h b/include/block/block.h index b72c81a..2de226f 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -310,6 +310,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); +bool bdrv_has_discard_zeroes(BlockDriverState *bs); +bool 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 56e1994..cf78c3f 100644 --- a/include/block/block_int.h +++ b/include/block/block_int.h @@ -211,6 +211,19 @@ struct BlockDriver { */ int (*bdrv_has_zero_init)(BlockDriverState *bs); + /* + * Returns true if discarded blocks read back as zeroes. + */ + bool (*bdrv_has_discard_zeroes)(BlockDriverState *bs); + + /* + * Returns true 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. + */ + bool (*bdrv_has_discard_write_zeroes)(BlockDriverState *bs); + QLIST_ENTRY(BlockDriver) list; }; -- 1.7.9.5