@@ -469,7 +469,11 @@ static void mirror_free_init(MirrorBlockJob *s)
469469 }
470470}
471471
472- static void mirror_drain (MirrorBlockJob * s )
472+ /* This is also used for the .pause callback. There is no matching
473+ * mirror_resume() because mirror_run() will begin iterating again
474+ * when the job is resumed.
475+ */
476+ static void mirror_wait_for_all_io (MirrorBlockJob * s )
473477{
474478 while (s -> in_flight > 0 ) {
475479 mirror_wait_for_io (s );
@@ -528,6 +532,7 @@ static void mirror_exit(BlockJob *job, void *opaque)
528532 g_free (s -> replaces );
529533 bdrv_op_unblock_all (target_bs , s -> common .blocker );
530534 blk_unref (s -> target );
535+ s -> target = NULL ;
531536 block_job_completed (& s -> common , data -> ret );
532537 g_free (data );
533538 bdrv_drained_end (src );
@@ -582,7 +587,7 @@ static int coroutine_fn mirror_dirty_init(MirrorBlockJob *s)
582587 sector_num += nb_sectors ;
583588 }
584589
585- mirror_drain (s );
590+ mirror_wait_for_all_io (s );
586591 }
587592
588593 /* First part, loop on the sectors and initialize the dirty bitmap. */
@@ -787,7 +792,7 @@ static void coroutine_fn mirror_run(void *opaque)
787792 * the target is a copy of the source.
788793 */
789794 assert (ret < 0 || (!s -> synced && block_job_is_cancelled (& s -> common )));
790- mirror_drain (s );
795+ mirror_wait_for_all_io (s );
791796 }
792797
793798 assert (s -> in_flight == 0 );
@@ -872,14 +877,11 @@ static void mirror_complete(BlockJob *job, Error **errp)
872877 block_job_enter (& s -> common );
873878}
874879
875- /* There is no matching mirror_resume() because mirror_run() will begin
876- * iterating again when the job is resumed.
877- */
878- static void coroutine_fn mirror_pause (BlockJob * job )
880+ static void mirror_pause (BlockJob * job )
879881{
880882 MirrorBlockJob * s = container_of (job , MirrorBlockJob , common );
881883
882- mirror_drain (s );
884+ mirror_wait_for_all_io (s );
883885}
884886
885887static void mirror_attached_aio_context (BlockJob * job , AioContext * new_context )
@@ -889,13 +891,29 @@ static void mirror_attached_aio_context(BlockJob *job, AioContext *new_context)
889891 blk_set_aio_context (s -> target , new_context );
890892}
891893
894+ static void mirror_drain (BlockJob * job )
895+ {
896+ MirrorBlockJob * s = container_of (job , MirrorBlockJob , common );
897+
898+ /* Need to keep a reference in case blk_drain triggers execution
899+ * of mirror_complete...
900+ */
901+ if (s -> target ) {
902+ BlockBackend * target = s -> target ;
903+ blk_ref (target );
904+ blk_drain (target );
905+ blk_unref (target );
906+ }
907+ }
908+
892909static const BlockJobDriver mirror_job_driver = {
893910 .instance_size = sizeof (MirrorBlockJob ),
894911 .job_type = BLOCK_JOB_TYPE_MIRROR ,
895912 .set_speed = mirror_set_speed ,
896913 .complete = mirror_complete ,
897914 .pause = mirror_pause ,
898915 .attached_aio_context = mirror_attached_aio_context ,
916+ .drain = mirror_drain ,
899917};
900918
901919static const BlockJobDriver commit_active_job_driver = {
@@ -905,6 +923,7 @@ static const BlockJobDriver commit_active_job_driver = {
905923 .complete = mirror_complete ,
906924 .pause = mirror_pause ,
907925 .attached_aio_context = mirror_attached_aio_context ,
926+ .drain = mirror_drain ,
908927};
909928
910929static void mirror_start_job (const char * job_id , BlockDriverState * bs ,
0 commit comments