@@ -4181,7 +4181,39 @@ int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num,
}
if (bs->drv->bdrv_co_discard) {
- return bs->drv->bdrv_co_discard(bs, sector_num, nb_sectors);
+ /* if no limit is specified in the BlockDriverState use a default
+ * of 32768 512-byte sectors (16 MiB) per request.
+ */
+ int max_discard = bs->max_discard ? bs->max_discard : 32768;
+
+ while (nb_sectors > 0) {
+ int ret;
+ int num = nb_sectors;
+
+ /* align request */
+ if (bs->discard_alignment &&
+ num >= bs->discard_alignment &&
+ sector_num % bs->discard_alignment) {
+ if (num > bs->discard_alignment) {
+ num = bs->discard_alignment;
+ }
+ num -= sector_num % bs->discard_alignment;
+ }
+
+ /* limit request size */
+ if (num > max_discard) {
+ num = max_discard;
+ }
+
+ ret = bs->drv->bdrv_co_discard(bs, sector_num, num);
+ if (ret) {
+ return ret;
+ }
+
+ sector_num += num;
+ nb_sectors -= num;
+ }
+ return 0;
} else if (bs->drv->bdrv_aio_discard) {
BlockDriverAIOCB *acb;
CoroutineIOCompletion co = {
Signed-off-by: Peter Lieven <pl@kamp.de> --- block.c | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-)