diff mbox

[PATCHv2,08/20] block: honour alignment and limit in bdrv_co_discard

Message ID 1379425736-11326-9-git-send-email-pl@kamp.de
State New
Headers show

Commit Message

Peter Lieven Sept. 17, 2013, 1:48 p.m. UTC
Signed-off-by: Peter Lieven <pl@kamp.de>
---
 block.c |   34 +++++++++++++++++++++++++++++++++-
 1 file changed, 33 insertions(+), 1 deletion(-)

Comments

Eric Blake Sept. 19, 2013, 8:09 p.m. UTC | #1
On 09/17/2013 07:48 AM, Peter Lieven wrote:
> Signed-off-by: Peter Lieven <pl@kamp.de>
> ---
>  block.c |   34 +++++++++++++++++++++++++++++++++-
>  1 file changed, 33 insertions(+), 1 deletion(-)
> 
> diff --git a/block.c b/block.c
> index 74ec342..ecc5be4 100644
> --- a/block.c
> +++ b/block.c
> @@ -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;

Worth having a named constant instead of a magic number?
diff mbox

Patch

diff --git a/block.c b/block.c
index 74ec342..ecc5be4 100644
--- a/block.c
+++ b/block.c
@@ -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 = {