diff mbox

[06/11] block: add bdrv_drain()

Message ID 1412182919-9550-7-git-send-email-stefanha@redhat.com
State New
Headers show

Commit Message

Stefan Hajnoczi Oct. 1, 2014, 5:01 p.m. UTC
Now that op blockers are in use, we can ensure that no other sources are
generating I/O on a BlockDriverState.  Therefore it is possible to drain
requests for a single BDS.

Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
---
 block.c               | 36 +++++++++++++++++++++++++++++-------
 include/block/block.h |  1 +
 2 files changed, 30 insertions(+), 7 deletions(-)

Comments

Max Reitz Oct. 1, 2014, 7:15 p.m. UTC | #1
On 01.10.2014 19:01, Stefan Hajnoczi wrote:
> Now that op blockers are in use, we can ensure that no other sources are
> generating I/O on a BlockDriverState.  Therefore it is possible to drain
> requests for a single BDS.
>
> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
> ---
>   block.c               | 36 +++++++++++++++++++++++++++++-------
>   include/block/block.h |  1 +
>   2 files changed, 30 insertions(+), 7 deletions(-)
>
> diff --git a/block.c b/block.c
> index c5a251c..48305c4 100644
> --- a/block.c
> +++ b/block.c
> @@ -1918,6 +1918,34 @@ static bool bdrv_requests_pending(BlockDriverState *bs)
>       return false;
>   }
>   
> +static bool bdrv_drain_one(BlockDriverState *bs)
> +{
> +    bool bs_busy;
> +
> +    bdrv_flush_io_queue(bs);
> +    bdrv_start_throttled_reqs(bs);
> +    bs_busy = bdrv_requests_pending(bs);
> +    bs_busy |= aio_poll(bdrv_get_aio_context(bs), bs_busy);
> +    return bs_busy;
> +}
> +
> +/*
> + * Wait for pending requests to complete on a single BlockDriverState subtree
> + *
> + * See the warning in bdrv_drain_all().  This function can only be called if
> + * you are sure nothing can generate I/O because you have op blockers
> + * installed.

Although that warning now sounds too harsh: "it is not possible to have 
a function to drain a single device's I/O queue." Apparently, under 
certain circumstances, it now *is* possible. ;-)

Reviewed-by: Max Reitz <mreitz@redhat.com>
diff mbox

Patch

diff --git a/block.c b/block.c
index c5a251c..48305c4 100644
--- a/block.c
+++ b/block.c
@@ -1918,6 +1918,34 @@  static bool bdrv_requests_pending(BlockDriverState *bs)
     return false;
 }
 
+static bool bdrv_drain_one(BlockDriverState *bs)
+{
+    bool bs_busy;
+
+    bdrv_flush_io_queue(bs);
+    bdrv_start_throttled_reqs(bs);
+    bs_busy = bdrv_requests_pending(bs);
+    bs_busy |= aio_poll(bdrv_get_aio_context(bs), bs_busy);
+    return bs_busy;
+}
+
+/*
+ * Wait for pending requests to complete on a single BlockDriverState subtree
+ *
+ * See the warning in bdrv_drain_all().  This function can only be called if
+ * you are sure nothing can generate I/O because you have op blockers
+ * installed.
+ *
+ * Note that unlike bdrv_drain_all(), the caller must hold the BlockDriverState
+ * AioContext.
+ */
+void bdrv_drain(BlockDriverState *bs)
+{
+    while (bdrv_drain_one(bs)) {
+        /* Keep iterating */
+    }
+}
+
 /*
  * Wait for pending requests to complete across all BlockDriverStates
  *
@@ -1941,16 +1969,10 @@  void bdrv_drain_all(void)
 
         QTAILQ_FOREACH(bs, &bdrv_states, device_list) {
             AioContext *aio_context = bdrv_get_aio_context(bs);
-            bool bs_busy;
 
             aio_context_acquire(aio_context);
-            bdrv_flush_io_queue(bs);
-            bdrv_start_throttled_reqs(bs);
-            bs_busy = bdrv_requests_pending(bs);
-            bs_busy |= aio_poll(aio_context, bs_busy);
+            busy |= bdrv_drain_one(bs);
             aio_context_release(aio_context);
-
-            busy |= bs_busy;
         }
     }
 }
diff --git a/include/block/block.h b/include/block/block.h
index 3318f0d..61df804 100644
--- a/include/block/block.h
+++ b/include/block/block.h
@@ -372,6 +372,7 @@  int bdrv_flush(BlockDriverState *bs);
 int coroutine_fn bdrv_co_flush(BlockDriverState *bs);
 int bdrv_flush_all(void);
 void bdrv_close_all(void);
+void bdrv_drain(BlockDriverState *bs);
 void bdrv_drain_all(void);
 
 int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors);