diff mbox

[v2,1/4] blockjob: Allow nested pause

Message ID 1428069921-2957-2-git-send-email-famz@redhat.com
State New
Headers show

Commit Message

Fam Zheng April 3, 2015, 2:05 p.m. UTC
This patch changes block_job_pause to increase the pause counter and
block_job_resume to decrease it.

The counter will allow calling block_job_pause/block_job_resume
unconditionally on a job when we need to suspend the IO temporarily.

From now on, each block_job_resume must be paired with a block_job_pause
to keep the counter balanced.

The user pause from QMP or HMP will only trigger block_job_pause once
until it's resumed, this is achieved by adding a user_paused flag in
BlockJob.

One occurrence of block_job_resume in mirror_complete is replaced with
block_job_enter which does what is necessary.

In block_job_cancel, the cancel flag is good enough to instruct
coroutines to quit loop, so use block_job_enter to replace the unpaired
block_job_resume.

Upon block job IO error, user is notified about the entering to the
pause state, so this pause belongs to user pause, set the flag
accordingly and expect a matching QMP resume.

Signed-off-by: Fam Zheng <famz@redhat.com>
---
 block/mirror.c           |  2 +-
 blockdev.c               |  8 +++++---
 blockjob.c               | 23 +++++++++++++++++------
 include/block/blockjob.h | 20 ++++++++++++++++----
 4 files changed, 39 insertions(+), 14 deletions(-)

Comments

Paolo Bonzini April 3, 2015, 2:13 p.m. UTC | #1
On 03/04/2015 16:05, Fam Zheng wrote:
> This patch changes block_job_pause to increase the pause counter and
> block_job_resume to decrease it.
> 
> The counter will allow calling block_job_pause/block_job_resume
> unconditionally on a job when we need to suspend the IO temporarily.
> 
> From now on, each block_job_resume must be paired with a block_job_pause
> to keep the counter balanced.
> 
> The user pause from QMP or HMP will only trigger block_job_pause once
> until it's resumed, this is achieved by adding a user_paused flag in
> BlockJob.
> 
> One occurrence of block_job_resume in mirror_complete is replaced with
> block_job_enter which does what is necessary.
> 
> In block_job_cancel, the cancel flag is good enough to instruct
> coroutines to quit loop, so use block_job_enter to replace the unpaired
> block_job_resume.
> 
> Upon block job IO error, user is notified about the entering to the
> pause state, so this pause belongs to user pause, set the flag
> accordingly and expect a matching QMP resume.
> 
> Signed-off-by: Fam Zheng <famz@redhat.com>
> ---
>  block/mirror.c           |  2 +-
>  blockdev.c               |  8 +++++---
>  blockjob.c               | 23 +++++++++++++++++------
>  include/block/blockjob.h | 20 ++++++++++++++++----
>  4 files changed, 39 insertions(+), 14 deletions(-)
> 
> diff --git a/block/mirror.c b/block/mirror.c
> index 4056164..65b1718 100644
> --- a/block/mirror.c
> +++ b/block/mirror.c
> @@ -634,7 +634,7 @@ static void mirror_complete(BlockJob *job, Error **errp)
>      }
>  
>      s->should_complete = true;
> -    block_job_resume(job);
> +    block_job_enter(&s->common);
>  }
>  
>  static const BlockJobDriver mirror_job_driver = {
> diff --git a/blockdev.c b/blockdev.c
> index fbb3a79..9132d69 100644
> --- a/blockdev.c
> +++ b/blockdev.c
> @@ -2699,7 +2699,7 @@ void qmp_block_job_cancel(const char *device,
>          force = false;
>      }
>  
> -    if (job->paused && !force) {
> +    if (job->user_paused && !force) {
>          error_setg(errp, "The block job for device '%s' is currently paused",
>                     device);
>          goto out;
> @@ -2716,10 +2716,11 @@ void qmp_block_job_pause(const char *device, Error **errp)
>      AioContext *aio_context;
>      BlockJob *job = find_block_job(device, &aio_context, errp);
>  
> -    if (!job) {
> +    if (!job || job->user_paused) {
>          return;
>      }
>  
> +    job->user_paused = true;
>      trace_qmp_block_job_pause(job);
>      block_job_pause(job);
>      aio_context_release(aio_context);
> @@ -2730,10 +2731,11 @@ void qmp_block_job_resume(const char *device, Error **errp)
>      AioContext *aio_context;
>      BlockJob *job = find_block_job(device, &aio_context, errp);
>  
> -    if (!job) {
> +    if (!job || !job->user_paused) {
>          return;
>      }
>  
> +    job->user_paused = false;
>      trace_qmp_block_job_resume(job);
>      block_job_resume(job);
>      aio_context_release(aio_context);
> diff --git a/blockjob.c b/blockjob.c
> index ba2255d..2755465 100644
> --- a/blockjob.c
> +++ b/blockjob.c
> @@ -107,7 +107,7 @@ void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp)
>  
>  void block_job_complete(BlockJob *job, Error **errp)
>  {
> -    if (job->paused || job->cancelled || !job->driver->complete) {
> +    if (job->pause_count || job->cancelled || !job->driver->complete) {
>          error_set(errp, QERR_BLOCK_JOB_NOT_READY,
>                    bdrv_get_device_name(job->bs));
>          return;
> @@ -118,17 +118,26 @@ void block_job_complete(BlockJob *job, Error **errp)
>  
>  void block_job_pause(BlockJob *job)
>  {
> -    job->paused = true;
> +    job->pause_count++;
>  }
>  
>  bool block_job_is_paused(BlockJob *job)
>  {
> -    return job->paused;
> +    return job->pause_count > 0;
>  }
>  
>  void block_job_resume(BlockJob *job)
>  {
> -    job->paused = false;
> +    assert(job->pause_count > 0);
> +    job->pause_count--;
> +    if (job->pause_count) {
> +        return;
> +    }
> +    block_job_enter(job);
> +}
> +
> +void block_job_enter(BlockJob *job)
> +{
>      block_job_iostatus_reset(job);
>      if (job->co && !job->busy) {
>          qemu_coroutine_enter(job->co, NULL);
> @@ -138,7 +147,7 @@ void block_job_resume(BlockJob *job)
>  void block_job_cancel(BlockJob *job)
>  {
>      job->cancelled = true;
> -    block_job_resume(job);
> +    block_job_enter(job);
>  }
>  
>  bool block_job_is_cancelled(BlockJob *job)
> @@ -258,7 +267,7 @@ BlockJobInfo *block_job_query(BlockJob *job)
>      info->device    = g_strdup(bdrv_get_device_name(job->bs));
>      info->len       = job->len;
>      info->busy      = job->busy;
> -    info->paused    = job->paused;
> +    info->paused    = job->pause_count > 0;
>      info->offset    = job->offset;
>      info->speed     = job->speed;
>      info->io_status = job->iostatus;
> @@ -335,6 +344,8 @@ BlockErrorAction block_job_error_action(BlockJob *job, BlockDriverState *bs,
>                                      IO_OPERATION_TYPE_WRITE,
>                                      action, &error_abort);
>      if (action == BLOCK_ERROR_ACTION_STOP) {
> +        /* make the pause user visible, which will be resumed from QMP. */
> +        job->user_paused = true;
>          block_job_pause(job);
>          block_job_iostatus_set_err(job, error);
>          if (bs != job->bs) {
> diff --git a/include/block/blockjob.h b/include/block/blockjob.h
> index b6d4ebb..9f4b20d 100644
> --- a/include/block/blockjob.h
> +++ b/include/block/blockjob.h
> @@ -79,10 +79,14 @@ struct BlockJob {
>      bool cancelled;
>  
>      /**
> -     * Set to true if the job is either paused, or will pause itself
> -     * as soon as possible (if busy == true).
> +     * Counter for pause request. If non-zero, the block job will pause.

Why not keep the more complete comment ("If non-zero, the block job is
either paused, or if busy == true will pause itself as soon as possible")?

>       */
> -    bool paused;
> +    int pause_count;
> +
> +    /**
> +     * Set to true if the job is paused by user.

... and can be unpaused with the block-job-resume QMP command.

> +     */
> +    bool user_paused;
>  
>      /**
>       * Set to false by the job while it is in a quiescent state, where
> @@ -225,11 +229,19 @@ void block_job_pause(BlockJob *job);
>   * block_job_resume:
>   * @job: The job to be resumed.
>   *
> - * Resume the specified job.
> + * Resume the specified job.  Must be paired with a preceding block_job_pause.
>   */
>  void block_job_resume(BlockJob *job);
>  
>  /**
> + * block_job_enter:
> + * @job: The job to enter.
> + *
> + * Continue the specified job by entering the coroutine.
> + */
> +void block_job_enter(BlockJob *job);
> +
> +/**
>   * block_job_event_cancelled:
>   * @job: The job whose information is requested.
>   *
> 

Apart from this,

Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Alberto Garcia April 8, 2015, 7:25 a.m. UTC | #2
On Fri, Apr 03, 2015 at 10:05:18PM +0800, Fam Zheng wrote:

> Signed-off-by: Fam Zheng <famz@redhat.com>
> ---
>  block/mirror.c           |  2 +-
>  blockdev.c               |  8 +++++---
>  blockjob.c               | 23 +++++++++++++++++------
>  include/block/blockjob.h | 20 ++++++++++++++++----
>  4 files changed, 39 insertions(+), 14 deletions(-)

Reviewed-by: Alberto Garcia <berto@igalia.com>

Berto
Stefan Hajnoczi April 8, 2015, 10:31 a.m. UTC | #3
On Fri, Apr 03, 2015 at 10:05:18PM +0800, Fam Zheng wrote:
> This patch changes block_job_pause to increase the pause counter and
> block_job_resume to decrease it.
> 
> The counter will allow calling block_job_pause/block_job_resume
> unconditionally on a job when we need to suspend the IO temporarily.
> 
> From now on, each block_job_resume must be paired with a block_job_pause
> to keep the counter balanced.
> 
> The user pause from QMP or HMP will only trigger block_job_pause once
> until it's resumed, this is achieved by adding a user_paused flag in
> BlockJob.
> 
> One occurrence of block_job_resume in mirror_complete is replaced with
> block_job_enter which does what is necessary.
> 
> In block_job_cancel, the cancel flag is good enough to instruct
> coroutines to quit loop, so use block_job_enter to replace the unpaired
> block_job_resume.
> 
> Upon block job IO error, user is notified about the entering to the
> pause state, so this pause belongs to user pause, set the flag
> accordingly and expect a matching QMP resume.
> 
> Signed-off-by: Fam Zheng <famz@redhat.com>
> ---
>  block/mirror.c           |  2 +-
>  blockdev.c               |  8 +++++---
>  blockjob.c               | 23 +++++++++++++++++------
>  include/block/blockjob.h | 20 ++++++++++++++++----
>  4 files changed, 39 insertions(+), 14 deletions(-)

Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Stefan Hajnoczi April 20, 2015, 5:11 p.m. UTC | #4
On Fri, Apr 03, 2015 at 04:13:55PM +0200, Paolo Bonzini wrote:
> 
> 
> On 03/04/2015 16:05, Fam Zheng wrote:
> > This patch changes block_job_pause to increase the pause counter and
> > block_job_resume to decrease it.
> > 
> > The counter will allow calling block_job_pause/block_job_resume
> > unconditionally on a job when we need to suspend the IO temporarily.
> > 
> > From now on, each block_job_resume must be paired with a block_job_pause
> > to keep the counter balanced.
> > 
> > The user pause from QMP or HMP will only trigger block_job_pause once
> > until it's resumed, this is achieved by adding a user_paused flag in
> > BlockJob.
> > 
> > One occurrence of block_job_resume in mirror_complete is replaced with
> > block_job_enter which does what is necessary.
> > 
> > In block_job_cancel, the cancel flag is good enough to instruct
> > coroutines to quit loop, so use block_job_enter to replace the unpaired
> > block_job_resume.
> > 
> > Upon block job IO error, user is notified about the entering to the
> > pause state, so this pause belongs to user pause, set the flag
> > accordingly and expect a matching QMP resume.
> > 
> > Signed-off-by: Fam Zheng <famz@redhat.com>
> > ---
> >  block/mirror.c           |  2 +-
> >  blockdev.c               |  8 +++++---
> >  blockjob.c               | 23 +++++++++++++++++------
> >  include/block/blockjob.h | 20 ++++++++++++++++----
> >  4 files changed, 39 insertions(+), 14 deletions(-)
> > 
> > diff --git a/block/mirror.c b/block/mirror.c
> > index 4056164..65b1718 100644
> > --- a/block/mirror.c
> > +++ b/block/mirror.c
> > @@ -634,7 +634,7 @@ static void mirror_complete(BlockJob *job, Error **errp)
> >      }
> >  
> >      s->should_complete = true;
> > -    block_job_resume(job);
> > +    block_job_enter(&s->common);
> >  }
> >  
> >  static const BlockJobDriver mirror_job_driver = {
> > diff --git a/blockdev.c b/blockdev.c
> > index fbb3a79..9132d69 100644
> > --- a/blockdev.c
> > +++ b/blockdev.c
> > @@ -2699,7 +2699,7 @@ void qmp_block_job_cancel(const char *device,
> >          force = false;
> >      }
> >  
> > -    if (job->paused && !force) {
> > +    if (job->user_paused && !force) {
> >          error_setg(errp, "The block job for device '%s' is currently paused",
> >                     device);
> >          goto out;
> > @@ -2716,10 +2716,11 @@ void qmp_block_job_pause(const char *device, Error **errp)
> >      AioContext *aio_context;
> >      BlockJob *job = find_block_job(device, &aio_context, errp);
> >  
> > -    if (!job) {
> > +    if (!job || job->user_paused) {
> >          return;
> >      }
> >  
> > +    job->user_paused = true;
> >      trace_qmp_block_job_pause(job);
> >      block_job_pause(job);
> >      aio_context_release(aio_context);
> > @@ -2730,10 +2731,11 @@ void qmp_block_job_resume(const char *device, Error **errp)
> >      AioContext *aio_context;
> >      BlockJob *job = find_block_job(device, &aio_context, errp);
> >  
> > -    if (!job) {
> > +    if (!job || !job->user_paused) {
> >          return;
> >      }
> >  
> > +    job->user_paused = false;
> >      trace_qmp_block_job_resume(job);
> >      block_job_resume(job);
> >      aio_context_release(aio_context);
> > diff --git a/blockjob.c b/blockjob.c
> > index ba2255d..2755465 100644
> > --- a/blockjob.c
> > +++ b/blockjob.c
> > @@ -107,7 +107,7 @@ void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp)
> >  
> >  void block_job_complete(BlockJob *job, Error **errp)
> >  {
> > -    if (job->paused || job->cancelled || !job->driver->complete) {
> > +    if (job->pause_count || job->cancelled || !job->driver->complete) {
> >          error_set(errp, QERR_BLOCK_JOB_NOT_READY,
> >                    bdrv_get_device_name(job->bs));
> >          return;
> > @@ -118,17 +118,26 @@ void block_job_complete(BlockJob *job, Error **errp)
> >  
> >  void block_job_pause(BlockJob *job)
> >  {
> > -    job->paused = true;
> > +    job->pause_count++;
> >  }
> >  
> >  bool block_job_is_paused(BlockJob *job)
> >  {
> > -    return job->paused;
> > +    return job->pause_count > 0;
> >  }
> >  
> >  void block_job_resume(BlockJob *job)
> >  {
> > -    job->paused = false;
> > +    assert(job->pause_count > 0);
> > +    job->pause_count--;
> > +    if (job->pause_count) {
> > +        return;
> > +    }
> > +    block_job_enter(job);
> > +}
> > +
> > +void block_job_enter(BlockJob *job)
> > +{
> >      block_job_iostatus_reset(job);
> >      if (job->co && !job->busy) {
> >          qemu_coroutine_enter(job->co, NULL);
> > @@ -138,7 +147,7 @@ void block_job_resume(BlockJob *job)
> >  void block_job_cancel(BlockJob *job)
> >  {
> >      job->cancelled = true;
> > -    block_job_resume(job);
> > +    block_job_enter(job);
> >  }
> >  
> >  bool block_job_is_cancelled(BlockJob *job)
> > @@ -258,7 +267,7 @@ BlockJobInfo *block_job_query(BlockJob *job)
> >      info->device    = g_strdup(bdrv_get_device_name(job->bs));
> >      info->len       = job->len;
> >      info->busy      = job->busy;
> > -    info->paused    = job->paused;
> > +    info->paused    = job->pause_count > 0;
> >      info->offset    = job->offset;
> >      info->speed     = job->speed;
> >      info->io_status = job->iostatus;
> > @@ -335,6 +344,8 @@ BlockErrorAction block_job_error_action(BlockJob *job, BlockDriverState *bs,
> >                                      IO_OPERATION_TYPE_WRITE,
> >                                      action, &error_abort);
> >      if (action == BLOCK_ERROR_ACTION_STOP) {
> > +        /* make the pause user visible, which will be resumed from QMP. */
> > +        job->user_paused = true;
> >          block_job_pause(job);
> >          block_job_iostatus_set_err(job, error);
> >          if (bs != job->bs) {
> > diff --git a/include/block/blockjob.h b/include/block/blockjob.h
> > index b6d4ebb..9f4b20d 100644
> > --- a/include/block/blockjob.h
> > +++ b/include/block/blockjob.h
> > @@ -79,10 +79,14 @@ struct BlockJob {
> >      bool cancelled;
> >  
> >      /**
> > -     * Set to true if the job is either paused, or will pause itself
> > -     * as soon as possible (if busy == true).
> > +     * Counter for pause request. If non-zero, the block job will pause.
> 
> Why not keep the more complete comment ("If non-zero, the block job is
> either paused, or if busy == true will pause itself as soon as possible")?
> 
> >       */
> > -    bool paused;
> > +    int pause_count;
> > +
> > +    /**
> > +     * Set to true if the job is paused by user.
> 
> ... and can be unpaused with the block-job-resume QMP command.

I squashed in your suggestions.
diff mbox

Patch

diff --git a/block/mirror.c b/block/mirror.c
index 4056164..65b1718 100644
--- a/block/mirror.c
+++ b/block/mirror.c
@@ -634,7 +634,7 @@  static void mirror_complete(BlockJob *job, Error **errp)
     }
 
     s->should_complete = true;
-    block_job_resume(job);
+    block_job_enter(&s->common);
 }
 
 static const BlockJobDriver mirror_job_driver = {
diff --git a/blockdev.c b/blockdev.c
index fbb3a79..9132d69 100644
--- a/blockdev.c
+++ b/blockdev.c
@@ -2699,7 +2699,7 @@  void qmp_block_job_cancel(const char *device,
         force = false;
     }
 
-    if (job->paused && !force) {
+    if (job->user_paused && !force) {
         error_setg(errp, "The block job for device '%s' is currently paused",
                    device);
         goto out;
@@ -2716,10 +2716,11 @@  void qmp_block_job_pause(const char *device, Error **errp)
     AioContext *aio_context;
     BlockJob *job = find_block_job(device, &aio_context, errp);
 
-    if (!job) {
+    if (!job || job->user_paused) {
         return;
     }
 
+    job->user_paused = true;
     trace_qmp_block_job_pause(job);
     block_job_pause(job);
     aio_context_release(aio_context);
@@ -2730,10 +2731,11 @@  void qmp_block_job_resume(const char *device, Error **errp)
     AioContext *aio_context;
     BlockJob *job = find_block_job(device, &aio_context, errp);
 
-    if (!job) {
+    if (!job || !job->user_paused) {
         return;
     }
 
+    job->user_paused = false;
     trace_qmp_block_job_resume(job);
     block_job_resume(job);
     aio_context_release(aio_context);
diff --git a/blockjob.c b/blockjob.c
index ba2255d..2755465 100644
--- a/blockjob.c
+++ b/blockjob.c
@@ -107,7 +107,7 @@  void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp)
 
 void block_job_complete(BlockJob *job, Error **errp)
 {
-    if (job->paused || job->cancelled || !job->driver->complete) {
+    if (job->pause_count || job->cancelled || !job->driver->complete) {
         error_set(errp, QERR_BLOCK_JOB_NOT_READY,
                   bdrv_get_device_name(job->bs));
         return;
@@ -118,17 +118,26 @@  void block_job_complete(BlockJob *job, Error **errp)
 
 void block_job_pause(BlockJob *job)
 {
-    job->paused = true;
+    job->pause_count++;
 }
 
 bool block_job_is_paused(BlockJob *job)
 {
-    return job->paused;
+    return job->pause_count > 0;
 }
 
 void block_job_resume(BlockJob *job)
 {
-    job->paused = false;
+    assert(job->pause_count > 0);
+    job->pause_count--;
+    if (job->pause_count) {
+        return;
+    }
+    block_job_enter(job);
+}
+
+void block_job_enter(BlockJob *job)
+{
     block_job_iostatus_reset(job);
     if (job->co && !job->busy) {
         qemu_coroutine_enter(job->co, NULL);
@@ -138,7 +147,7 @@  void block_job_resume(BlockJob *job)
 void block_job_cancel(BlockJob *job)
 {
     job->cancelled = true;
-    block_job_resume(job);
+    block_job_enter(job);
 }
 
 bool block_job_is_cancelled(BlockJob *job)
@@ -258,7 +267,7 @@  BlockJobInfo *block_job_query(BlockJob *job)
     info->device    = g_strdup(bdrv_get_device_name(job->bs));
     info->len       = job->len;
     info->busy      = job->busy;
-    info->paused    = job->paused;
+    info->paused    = job->pause_count > 0;
     info->offset    = job->offset;
     info->speed     = job->speed;
     info->io_status = job->iostatus;
@@ -335,6 +344,8 @@  BlockErrorAction block_job_error_action(BlockJob *job, BlockDriverState *bs,
                                     IO_OPERATION_TYPE_WRITE,
                                     action, &error_abort);
     if (action == BLOCK_ERROR_ACTION_STOP) {
+        /* make the pause user visible, which will be resumed from QMP. */
+        job->user_paused = true;
         block_job_pause(job);
         block_job_iostatus_set_err(job, error);
         if (bs != job->bs) {
diff --git a/include/block/blockjob.h b/include/block/blockjob.h
index b6d4ebb..9f4b20d 100644
--- a/include/block/blockjob.h
+++ b/include/block/blockjob.h
@@ -79,10 +79,14 @@  struct BlockJob {
     bool cancelled;
 
     /**
-     * Set to true if the job is either paused, or will pause itself
-     * as soon as possible (if busy == true).
+     * Counter for pause request. If non-zero, the block job will pause.
      */
-    bool paused;
+    int pause_count;
+
+    /**
+     * Set to true if the job is paused by user.
+     */
+    bool user_paused;
 
     /**
      * Set to false by the job while it is in a quiescent state, where
@@ -225,11 +229,19 @@  void block_job_pause(BlockJob *job);
  * block_job_resume:
  * @job: The job to be resumed.
  *
- * Resume the specified job.
+ * Resume the specified job.  Must be paired with a preceding block_job_pause.
  */
 void block_job_resume(BlockJob *job);
 
 /**
+ * block_job_enter:
+ * @job: The job to enter.
+ *
+ * Continue the specified job by entering the coroutine.
+ */
+void block_job_enter(BlockJob *job);
+
+/**
  * block_job_event_cancelled:
  * @job: The job whose information is requested.
  *