xref: /qemu/block/stream.c (revision 72098a3a)
14f1043b4SStefan Hajnoczi /*
24f1043b4SStefan Hajnoczi  * Image streaming
34f1043b4SStefan Hajnoczi  *
44f1043b4SStefan Hajnoczi  * Copyright IBM, Corp. 2011
54f1043b4SStefan Hajnoczi  *
64f1043b4SStefan Hajnoczi  * Authors:
74f1043b4SStefan Hajnoczi  *  Stefan Hajnoczi   <stefanha@linux.vnet.ibm.com>
84f1043b4SStefan Hajnoczi  *
94f1043b4SStefan Hajnoczi  * This work is licensed under the terms of the GNU LGPL, version 2 or later.
104f1043b4SStefan Hajnoczi  * See the COPYING.LIB file in the top-level directory.
114f1043b4SStefan Hajnoczi  *
124f1043b4SStefan Hajnoczi  */
134f1043b4SStefan Hajnoczi 
1480c71a24SPeter Maydell #include "qemu/osdep.h"
154f1043b4SStefan Hajnoczi #include "trace.h"
16737e150eSPaolo Bonzini #include "block/block_int.h"
17c87621eaSJohn Snow #include "block/blockjob_int.h"
18da34e65cSMarkus Armbruster #include "qapi/error.h"
19205736f4SAndrey Shinkevich #include "qapi/qmp/qdict.h"
206ef228fcSPaolo Bonzini #include "qemu/ratelimit.h"
21373340b2SMax Reitz #include "sysemu/block-backend.h"
22205736f4SAndrey Shinkevich #include "block/copy-on-read.h"
234f1043b4SStefan Hajnoczi 
244f1043b4SStefan Hajnoczi enum {
254f1043b4SStefan Hajnoczi     /*
2699136607SVladimir Sementsov-Ogievskiy      * Maximum chunk size to feed to copy-on-read.  This should be
2799136607SVladimir Sementsov-Ogievskiy      * large enough to process multiple clusters in a single call, so
2899136607SVladimir Sementsov-Ogievskiy      * that populating contiguous regions of the image is efficient.
294f1043b4SStefan Hajnoczi      */
3099136607SVladimir Sementsov-Ogievskiy     STREAM_CHUNK = 512 * 1024, /* in bytes */
314f1043b4SStefan Hajnoczi };
324f1043b4SStefan Hajnoczi 
334f1043b4SStefan Hajnoczi typedef struct StreamBlockJob {
344f1043b4SStefan Hajnoczi     BlockJob common;
35048954e2SVladimir Sementsov-Ogievskiy     BlockBackend *blk;
3667acfd21SMax Reitz     BlockDriverState *base_overlay; /* COW overlay (stream from this) */
3767acfd21SMax Reitz     BlockDriverState *above_base;   /* Node directly above the base */
38205736f4SAndrey Shinkevich     BlockDriverState *cor_filter_bs;
390f6c9498SVladimir Sementsov-Ogievskiy     BlockDriverState *target_bs;
401d809098SPaolo Bonzini     BlockdevOnError on_error;
4113d8cc51SJeff Cody     char *backing_file_str;
4272098a3aSPeter Krempa     bool backing_mask_protocol;
43e7d22f8bSAlberto Garcia     bool bs_read_only;
444f1043b4SStefan Hajnoczi } StreamBlockJob;
454f1043b4SStefan Hajnoczi 
stream_populate(BlockBackend * blk,int64_t offset,uint64_t bytes)4603e35d82SKevin Wolf static int coroutine_fn stream_populate(BlockBackend *blk,
4799136607SVladimir Sementsov-Ogievskiy                                         int64_t offset, uint64_t bytes)
484f1043b4SStefan Hajnoczi {
498493211cSEric Blake     assert(bytes < SIZE_MAX);
504f1043b4SStefan Hajnoczi 
51205736f4SAndrey Shinkevich     return blk_co_preadv(blk, offset, bytes, NULL, BDRV_REQ_PREFETCH);
5265854933SAlberto Garcia }
5365854933SAlberto Garcia 
stream_prepare(Job * job)541b57488aSJohn Snow static int stream_prepare(Job *job)
55f3e69bebSStefan Hajnoczi {
561908a559SKevin Wolf     StreamBlockJob *s = container_of(job, StreamBlockJob, common.job);
57ad74751fSKevin Wolf     BlockDriverState *unfiltered_bs;
58ad74751fSKevin Wolf     BlockDriverState *unfiltered_bs_cow;
598d3dd037SHanna Reitz     BlockDriverState *base;
608d3dd037SHanna Reitz     BlockDriverState *unfiltered_base;
6112fa4af6SKevin Wolf     Error *local_err = NULL;
621b57488aSJohn Snow     int ret = 0;
63f3e69bebSStefan Hajnoczi 
64372b69f5SKevin Wolf     GLOBAL_STATE_CODE();
65372b69f5SKevin Wolf 
66ad74751fSKevin Wolf     bdrv_graph_rdlock_main_loop();
67ad74751fSKevin Wolf     unfiltered_bs = bdrv_skip_filters(s->target_bs);
68ad74751fSKevin Wolf     unfiltered_bs_cow = bdrv_cow_bs(unfiltered_bs);
69ad74751fSKevin Wolf     bdrv_graph_rdunlock_main_loop();
70ad74751fSKevin Wolf 
71205736f4SAndrey Shinkevich     /* We should drop filter at this point, as filter hold the backing chain */
72205736f4SAndrey Shinkevich     bdrv_cor_filter_drop(s->cor_filter_bs);
73205736f4SAndrey Shinkevich     s->cor_filter_bs = NULL;
7465854933SAlberto Garcia 
7592140b9fSKevin Wolf     /*
767d4ca9d2SKevin Wolf      * bdrv_set_backing_hd() requires that the unfiltered_bs and the COW child
777d4ca9d2SKevin Wolf      * of unfiltered_bs is drained. Drain already here and use
787d4ca9d2SKevin Wolf      * bdrv_set_backing_hd_drained() instead because the polling during
797d4ca9d2SKevin Wolf      * drained_begin() might change the graph, and if we do this only later, we
807d4ca9d2SKevin Wolf      * may end up working with the wrong base node (or it might even have gone
817d4ca9d2SKevin Wolf      * away by the time we want to use it).
8292140b9fSKevin Wolf      */
8392140b9fSKevin Wolf     bdrv_drained_begin(unfiltered_bs);
847d4ca9d2SKevin Wolf     if (unfiltered_bs_cow) {
857d4ca9d2SKevin Wolf         bdrv_ref(unfiltered_bs_cow);
867d4ca9d2SKevin Wolf         bdrv_drained_begin(unfiltered_bs_cow);
877d4ca9d2SKevin Wolf     }
88b1e1af39SHanna Reitz 
89372b69f5SKevin Wolf     bdrv_graph_rdlock_main_loop();
908d3dd037SHanna Reitz     base = bdrv_filter_or_cow_bs(s->above_base);
918d3dd037SHanna Reitz     unfiltered_base = bdrv_skip_filters(base);
92372b69f5SKevin Wolf     bdrv_graph_rdunlock_main_loop();
938d3dd037SHanna Reitz 
9478a9c76eSKevin Wolf     if (unfiltered_bs_cow) {
95f3e69bebSStefan Hajnoczi         const char *base_id = NULL, *base_fmt = NULL;
96000e5a1cSAndrey Shinkevich         if (unfiltered_base) {
97000e5a1cSAndrey Shinkevich             base_id = s->backing_file_str ?: unfiltered_base->filename;
98000e5a1cSAndrey Shinkevich             if (unfiltered_base->drv) {
9972098a3aSPeter Krempa                 if (s->backing_mask_protocol &&
10072098a3aSPeter Krempa                     unfiltered_base->drv->protocol_name) {
10172098a3aSPeter Krempa                     base_fmt = "raw";
10272098a3aSPeter Krempa                 } else {
103000e5a1cSAndrey Shinkevich                     base_fmt = unfiltered_base->drv->format_name;
104f3e69bebSStefan Hajnoczi                 }
105f3e69bebSStefan Hajnoczi             }
10672098a3aSPeter Krempa         }
107b1e1af39SHanna Reitz 
1086bc30f19SStefan Hajnoczi         bdrv_graph_wrlock();
10992140b9fSKevin Wolf         bdrv_set_backing_hd_drained(unfiltered_bs, base, &local_err);
1106bc30f19SStefan Hajnoczi         bdrv_graph_wrunlock();
11192140b9fSKevin Wolf 
11292140b9fSKevin Wolf         /*
11392140b9fSKevin Wolf          * This call will do I/O, so the graph can change again from here on.
11492140b9fSKevin Wolf          * We have already completed the graph change, so we are not in danger
11592140b9fSKevin Wolf          * of operating on the wrong node any more if this happens.
11692140b9fSKevin Wolf          */
11767acfd21SMax Reitz         ret = bdrv_change_backing_file(unfiltered_bs, base_id, base_fmt, false);
11812fa4af6SKevin Wolf         if (local_err) {
11912fa4af6SKevin Wolf             error_report_err(local_err);
120b1e1af39SHanna Reitz             ret = -EPERM;
121b1e1af39SHanna Reitz             goto out;
12212fa4af6SKevin Wolf         }
123f3e69bebSStefan Hajnoczi     }
124f3e69bebSStefan Hajnoczi 
125b1e1af39SHanna Reitz out:
1267d4ca9d2SKevin Wolf     if (unfiltered_bs_cow) {
1277d4ca9d2SKevin Wolf         bdrv_drained_end(unfiltered_bs_cow);
1287d4ca9d2SKevin Wolf         bdrv_unref(unfiltered_bs_cow);
1297d4ca9d2SKevin Wolf     }
13092140b9fSKevin Wolf     bdrv_drained_end(unfiltered_bs);
1311b57488aSJohn Snow     return ret;
1321b57488aSJohn Snow }
1331b57488aSJohn Snow 
stream_clean(Job * job)1341b57488aSJohn Snow static void stream_clean(Job *job)
1351b57488aSJohn Snow {
1361b57488aSJohn Snow     StreamBlockJob *s = container_of(job, StreamBlockJob, common.job);
1371b57488aSJohn Snow 
138205736f4SAndrey Shinkevich     if (s->cor_filter_bs) {
139205736f4SAndrey Shinkevich         bdrv_cor_filter_drop(s->cor_filter_bs);
140205736f4SAndrey Shinkevich         s->cor_filter_bs = NULL;
141205736f4SAndrey Shinkevich     }
142205736f4SAndrey Shinkevich 
143048954e2SVladimir Sementsov-Ogievskiy     blk_unref(s->blk);
144048954e2SVladimir Sementsov-Ogievskiy     s->blk = NULL;
145048954e2SVladimir Sementsov-Ogievskiy 
14661b49e48SAlberto Garcia     /* Reopen the image back in read-only mode if necessary */
147e7d22f8bSAlberto Garcia     if (s->bs_read_only) {
148a170a91fSKevin Wolf         /* Give up write permissions before making it read-only */
1490f6c9498SVladimir Sementsov-Ogievskiy         bdrv_reopen_set_read_only(s->target_bs, true, NULL);
15061b49e48SAlberto Garcia     }
15161b49e48SAlberto Garcia 
152f3e69bebSStefan Hajnoczi     g_free(s->backing_file_str);
153f3e69bebSStefan Hajnoczi }
154f3e69bebSStefan Hajnoczi 
stream_run(Job * job,Error ** errp)155f67432a2SJohn Snow static int coroutine_fn stream_run(Job *job, Error **errp)
1564f1043b4SStefan Hajnoczi {
157f67432a2SJohn Snow     StreamBlockJob *s = container_of(job, StreamBlockJob, common.job);
158ad74751fSKevin Wolf     BlockDriverState *unfiltered_bs;
15905df8a6aSKevin Wolf     int64_t len;
160d535435fSEric Blake     int64_t offset = 0;
1611d809098SPaolo Bonzini     int error = 0;
16251b0a488SEric Blake     int64_t n = 0; /* bytes */
1634f1043b4SStefan Hajnoczi 
164ad74751fSKevin Wolf     WITH_GRAPH_RDLOCK_GUARD() {
165ad74751fSKevin Wolf         unfiltered_bs = bdrv_skip_filters(s->target_bs);
16667acfd21SMax Reitz         if (unfiltered_bs == s->base_overlay) {
167c624b015SAndrey Shinkevich             /* Nothing to stream */
16896a07d5bSAndrey Shinkevich             return 0;
169f4a193e7SMax Reitz         }
170f4a193e7SMax Reitz 
1718ab8140aSKevin Wolf         len = bdrv_co_getlength(s->target_bs);
17205df8a6aSKevin Wolf         if (len < 0) {
17396a07d5bSAndrey Shinkevich             return len;
1744f1043b4SStefan Hajnoczi         }
1758ab8140aSKevin Wolf     }
17630a5c887SKevin Wolf     job_progress_set_remaining(&s->common.job, len);
1774f1043b4SStefan Hajnoczi 
17805df8a6aSKevin Wolf     for ( ; offset < len; offset += n) {
179f9749f28SPaolo Bonzini         bool copy;
18035c94535SChen Qun         int ret;
1814513eafeSPaolo Bonzini 
1824513eafeSPaolo Bonzini         /* Note that even when no rate limit is applied we need to yield
183c57b6656SKevin Wolf          * with no pending I/O here so that bdrv_drain_all() returns.
1844513eafeSPaolo Bonzini          */
185018e5987SKevin Wolf         block_job_ratelimit_sleep(&s->common);
186daa7f2f9SKevin Wolf         if (job_is_cancelled(&s->common.job)) {
1874f1043b4SStefan Hajnoczi             break;
1884f1043b4SStefan Hajnoczi         }
1894f1043b4SStefan Hajnoczi 
190c3e4f43aSStefan Weil         copy = false;
191c3e4f43aSStefan Weil 
1927ff9579eSKevin Wolf         WITH_GRAPH_RDLOCK_GUARD() {
193cc323997SPaolo Bonzini             ret = bdrv_co_is_allocated(unfiltered_bs, offset, STREAM_CHUNK, &n);
194f9749f28SPaolo Bonzini             if (ret == 1) {
195f9749f28SPaolo Bonzini                 /* Allocated in the top, no need to copy.  */
196d663640cSPaolo Bonzini             } else if (ret >= 0) {
1977ff9579eSKevin Wolf                 /*
1987ff9579eSKevin Wolf                  * Copy if allocated in the intermediate images.  Limit to the
1997ff9579eSKevin Wolf                  * known-unallocated area [offset, offset+n*BDRV_SECTOR_SIZE).
2007ff9579eSKevin Wolf                  */
201cc323997SPaolo Bonzini                 ret = bdrv_co_is_allocated_above(bdrv_cow_bs(unfiltered_bs),
20267acfd21SMax Reitz                                                  s->base_overlay, true,
20351b0a488SEric Blake                                                  offset, n, &n);
204571cd9dcSStefan Hajnoczi                 /* Finish early if end of backing file has been reached */
205571cd9dcSStefan Hajnoczi                 if (ret == 0 && n == 0) {
20605df8a6aSKevin Wolf                     n = len - offset;
207571cd9dcSStefan Hajnoczi                 }
208571cd9dcSStefan Hajnoczi 
209a92b1b06SEric Blake                 copy = (ret > 0);
210f9749f28SPaolo Bonzini             }
2117ff9579eSKevin Wolf         }
21251b0a488SEric Blake         trace_stream_one_iteration(s, offset, n, ret);
213c3e4f43aSStefan Weil         if (copy) {
214048954e2SVladimir Sementsov-Ogievskiy             ret = stream_populate(s->blk, offset, n);
2154f1043b4SStefan Hajnoczi         }
2164f1043b4SStefan Hajnoczi         if (ret < 0) {
2171d809098SPaolo Bonzini             BlockErrorAction action =
21881e254dcSKevin Wolf                 block_job_error_action(&s->common, s->on_error, true, -ret);
219a589569fSWenchao Xia             if (action == BLOCK_ERROR_ACTION_STOP) {
2201d809098SPaolo Bonzini                 n = 0;
2211d809098SPaolo Bonzini                 continue;
2221d809098SPaolo Bonzini             }
2231d809098SPaolo Bonzini             if (error == 0) {
2241d809098SPaolo Bonzini                 error = ret;
2251d809098SPaolo Bonzini             }
226a589569fSWenchao Xia             if (action == BLOCK_ERROR_ACTION_REPORT) {
2274f1043b4SStefan Hajnoczi                 break;
2284f1043b4SStefan Hajnoczi             }
2291d809098SPaolo Bonzini         }
2304f1043b4SStefan Hajnoczi 
2314f1043b4SStefan Hajnoczi         /* Publish progress */
23230a5c887SKevin Wolf         job_progress_update(&s->common.job, n);
233dee81d51SKevin Wolf         if (copy) {
234018e5987SKevin Wolf             block_job_ratelimit_processed_bytes(&s->common, n);
235f14a39ccSSascha Silbe         }
2364f1043b4SStefan Hajnoczi     }
2374f1043b4SStefan Hajnoczi 
23896a07d5bSAndrey Shinkevich     /* Do not remove the backing file if an error was there but ignored. */
23996a07d5bSAndrey Shinkevich     return error;
2404f1043b4SStefan Hajnoczi }
2414f1043b4SStefan Hajnoczi 
2423fc4b10aSFam Zheng static const BlockJobDriver stream_job_driver = {
24333e9e9bdSKevin Wolf     .job_driver = {
2444f1043b4SStefan Hajnoczi         .instance_size = sizeof(StreamBlockJob),
2458e4c8700SKevin Wolf         .job_type      = JOB_TYPE_STREAM,
24680fa2c75SKevin Wolf         .free          = block_job_free,
247f67432a2SJohn Snow         .run           = stream_run,
2481b57488aSJohn Snow         .prepare       = stream_prepare,
2491b57488aSJohn Snow         .clean         = stream_clean,
250b15de828SKevin Wolf         .user_resume   = block_job_user_resume,
251da01ff7fSKevin Wolf     },
2524f1043b4SStefan Hajnoczi };
2534f1043b4SStefan Hajnoczi 
stream_start(const char * job_id,BlockDriverState * bs,BlockDriverState * base,const char * backing_file_str,bool backing_mask_protocol,BlockDriverState * bottom,int creation_flags,int64_t speed,BlockdevOnError on_error,const char * filter_node_name,Error ** errp)2542323322eSAlberto Garcia void stream_start(const char *job_id, BlockDriverState *bs,
2552323322eSAlberto Garcia                   BlockDriverState *base, const char *backing_file_str,
25672098a3aSPeter Krempa                   bool backing_mask_protocol,
2577f4a396dSVladimir Sementsov-Ogievskiy                   BlockDriverState *bottom,
258cf6320dfSJohn Snow                   int creation_flags, int64_t speed,
259880747a8SAndrey Shinkevich                   BlockdevOnError on_error,
260880747a8SAndrey Shinkevich                   const char *filter_node_name,
261880747a8SAndrey Shinkevich                   Error **errp)
2624f1043b4SStefan Hajnoczi {
2631bf26076SKevin Wolf     StreamBlockJob *s = NULL;
26461b49e48SAlberto Garcia     BlockDriverState *iter;
265e7d22f8bSAlberto Garcia     bool bs_read_only;
266c624b015SAndrey Shinkevich     int basic_flags = BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED;
2677f4a396dSVladimir Sementsov-Ogievskiy     BlockDriverState *base_overlay;
268205736f4SAndrey Shinkevich     BlockDriverState *cor_filter_bs = NULL;
26967acfd21SMax Reitz     BlockDriverState *above_base;
270205736f4SAndrey Shinkevich     QDict *opts;
2711bf26076SKevin Wolf     int ret;
2724f1043b4SStefan Hajnoczi 
273b4ad82aaSEmanuele Giuseppe Esposito     GLOBAL_STATE_CODE();
274b4ad82aaSEmanuele Giuseppe Esposito 
2757f4a396dSVladimir Sementsov-Ogievskiy     assert(!(base && bottom));
2767f4a396dSVladimir Sementsov-Ogievskiy     assert(!(backing_file_str && bottom));
2777f4a396dSVladimir Sementsov-Ogievskiy 
2789275fc72SKevin Wolf     bdrv_graph_rdlock_main_loop();
2799275fc72SKevin Wolf 
2807f4a396dSVladimir Sementsov-Ogievskiy     if (bottom) {
2817f4a396dSVladimir Sementsov-Ogievskiy         /*
2827f4a396dSVladimir Sementsov-Ogievskiy          * New simple interface. The code is written in terms of old interface
2837f4a396dSVladimir Sementsov-Ogievskiy          * with @base parameter (still, it doesn't freeze link to base, so in
2847f4a396dSVladimir Sementsov-Ogievskiy          * this mean old code is correct for new interface). So, for now, just
2857f4a396dSVladimir Sementsov-Ogievskiy          * emulate base_overlay and above_base. Still, when old interface
2867f4a396dSVladimir Sementsov-Ogievskiy          * finally removed, we should refactor code to use only "bottom", but
2877f4a396dSVladimir Sementsov-Ogievskiy          * not "*base*" things.
2887f4a396dSVladimir Sementsov-Ogievskiy          */
2897f4a396dSVladimir Sementsov-Ogievskiy         assert(!bottom->drv->is_filter);
2907f4a396dSVladimir Sementsov-Ogievskiy         base_overlay = above_base = bottom;
2917f4a396dSVladimir Sementsov-Ogievskiy     } else {
2927f4a396dSVladimir Sementsov-Ogievskiy         base_overlay = bdrv_find_overlay(bs, base);
29367acfd21SMax Reitz         if (!base_overlay) {
29467acfd21SMax Reitz             error_setg(errp, "'%s' is not in the backing chain of '%s'",
29567acfd21SMax Reitz                        base->node_name, bs->node_name);
2969275fc72SKevin Wolf             goto out_rdlock;
29767acfd21SMax Reitz         }
29867acfd21SMax Reitz 
29967acfd21SMax Reitz         /*
3007f4a396dSVladimir Sementsov-Ogievskiy          * Find the node directly above @base.  @base_overlay is a COW overlay,
3017f4a396dSVladimir Sementsov-Ogievskiy          * so it must have a bdrv_cow_child(), but it is the immediate overlay
3027f4a396dSVladimir Sementsov-Ogievskiy          * of @base, so between the two there can only be filters.
30367acfd21SMax Reitz          */
30467acfd21SMax Reitz         above_base = base_overlay;
30567acfd21SMax Reitz         if (bdrv_cow_bs(above_base) != base) {
30667acfd21SMax Reitz             above_base = bdrv_cow_bs(above_base);
30767acfd21SMax Reitz             while (bdrv_filter_bs(above_base) != base) {
30867acfd21SMax Reitz                 above_base = bdrv_filter_bs(above_base);
30967acfd21SMax Reitz             }
31067acfd21SMax Reitz         }
3117f4a396dSVladimir Sementsov-Ogievskiy     }
31267acfd21SMax Reitz 
313205736f4SAndrey Shinkevich     /* Make sure that the image is opened in read-write mode */
314205736f4SAndrey Shinkevich     bs_read_only = bdrv_is_read_only(bs);
315205736f4SAndrey Shinkevich     if (bs_read_only) {
316205736f4SAndrey Shinkevich         /* Hold the chain during reopen */
31767acfd21SMax Reitz         if (bdrv_freeze_backing_chain(bs, above_base, errp) < 0) {
3189275fc72SKevin Wolf             goto out_rdlock;
31920509c4bSAlberto Garcia         }
32020509c4bSAlberto Garcia 
321205736f4SAndrey Shinkevich         ret = bdrv_reopen_set_read_only(bs, false, errp);
322205736f4SAndrey Shinkevich 
323205736f4SAndrey Shinkevich         /* failure, or cor-filter will hold the chain */
324205736f4SAndrey Shinkevich         bdrv_unfreeze_backing_chain(bs, above_base);
325205736f4SAndrey Shinkevich 
326205736f4SAndrey Shinkevich         if (ret < 0) {
3279275fc72SKevin Wolf             goto out_rdlock;
32861b49e48SAlberto Garcia         }
32961b49e48SAlberto Garcia     }
33061b49e48SAlberto Garcia 
3319275fc72SKevin Wolf     bdrv_graph_rdunlock_main_loop();
3329275fc72SKevin Wolf 
333205736f4SAndrey Shinkevich     opts = qdict_new();
334205736f4SAndrey Shinkevich 
335205736f4SAndrey Shinkevich     qdict_put_str(opts, "driver", "copy-on-read");
336205736f4SAndrey Shinkevich     qdict_put_str(opts, "file", bdrv_get_node_name(bs));
337205736f4SAndrey Shinkevich     /* Pass the base_overlay node name as 'bottom' to COR driver */
338205736f4SAndrey Shinkevich     qdict_put_str(opts, "bottom", base_overlay->node_name);
339205736f4SAndrey Shinkevich     if (filter_node_name) {
340205736f4SAndrey Shinkevich         qdict_put_str(opts, "node-name", filter_node_name);
341205736f4SAndrey Shinkevich     }
342205736f4SAndrey Shinkevich 
343205736f4SAndrey Shinkevich     cor_filter_bs = bdrv_insert_node(bs, opts, BDRV_O_RDWR, errp);
344205736f4SAndrey Shinkevich     if (!cor_filter_bs) {
345205736f4SAndrey Shinkevich         goto fail;
346205736f4SAndrey Shinkevich     }
347205736f4SAndrey Shinkevich 
348205736f4SAndrey Shinkevich     if (!filter_node_name) {
349205736f4SAndrey Shinkevich         cor_filter_bs->implicit = true;
350205736f4SAndrey Shinkevich     }
351205736f4SAndrey Shinkevich 
352205736f4SAndrey Shinkevich     s = block_job_create(job_id, &stream_job_driver, NULL, cor_filter_bs,
353048954e2SVladimir Sementsov-Ogievskiy                          0, BLK_PERM_ALL,
354cf6320dfSJohn Snow                          speed, creation_flags, NULL, NULL, errp);
355a170a91fSKevin Wolf     if (!s) {
356a170a91fSKevin Wolf         goto fail;
357a170a91fSKevin Wolf     }
358a170a91fSKevin Wolf 
359048954e2SVladimir Sementsov-Ogievskiy     s->blk = blk_new_with_bs(cor_filter_bs, BLK_PERM_CONSISTENT_READ,
360048954e2SVladimir Sementsov-Ogievskiy                              basic_flags | BLK_PERM_WRITE, errp);
361048954e2SVladimir Sementsov-Ogievskiy     if (!s->blk) {
362048954e2SVladimir Sementsov-Ogievskiy         goto fail;
363048954e2SVladimir Sementsov-Ogievskiy     }
364048954e2SVladimir Sementsov-Ogievskiy     /*
365048954e2SVladimir Sementsov-Ogievskiy      * Disable request queuing in the BlockBackend to avoid deadlocks on drain:
366048954e2SVladimir Sementsov-Ogievskiy      * The job reports that it's busy until it reaches a pause point.
367048954e2SVladimir Sementsov-Ogievskiy      */
368048954e2SVladimir Sementsov-Ogievskiy     blk_set_disable_request_queuing(s->blk, true);
369048954e2SVladimir Sementsov-Ogievskiy     blk_set_allow_aio_context_change(s->blk, true);
370048954e2SVladimir Sementsov-Ogievskiy 
371205736f4SAndrey Shinkevich     /*
372205736f4SAndrey Shinkevich      * Prevent concurrent jobs trying to modify the graph structure here, we
373205736f4SAndrey Shinkevich      * already have our own plans. Also don't allow resize as the image size is
374205736f4SAndrey Shinkevich      * queried only at the job start and then cached.
375205736f4SAndrey Shinkevich      */
3766bc30f19SStefan Hajnoczi     bdrv_graph_wrlock();
377205736f4SAndrey Shinkevich     if (block_job_add_bdrv(&s->common, "active node", bs, 0,
3781bf26076SKevin Wolf                            basic_flags | BLK_PERM_WRITE, errp)) {
3796bc30f19SStefan Hajnoczi         bdrv_graph_wrunlock();
380205736f4SAndrey Shinkevich         goto fail;
381205736f4SAndrey Shinkevich     }
382205736f4SAndrey Shinkevich 
383a170a91fSKevin Wolf     /* Block all intermediate nodes between bs and base, because they will
384a170a91fSKevin Wolf      * disappear from the chain after this operation. The streaming job reads
385c624b015SAndrey Shinkevich      * every block only once, assuming that it doesn't change, so forbid writes
386c624b015SAndrey Shinkevich      * and resizes. Reassign the base node pointer because the backing BS of the
387c624b015SAndrey Shinkevich      * bottom node might change after the call to bdrv_reopen_set_read_only()
388c624b015SAndrey Shinkevich      * due to parallel block jobs running.
38967acfd21SMax Reitz      * above_base node might change after the call to
39067acfd21SMax Reitz      * bdrv_reopen_set_read_only() due to parallel block jobs running.
391c624b015SAndrey Shinkevich      */
39267acfd21SMax Reitz     base = bdrv_filter_or_cow_bs(above_base);
39367acfd21SMax Reitz     for (iter = bdrv_filter_or_cow_bs(bs); iter != base;
39467acfd21SMax Reitz          iter = bdrv_filter_or_cow_bs(iter))
39567acfd21SMax Reitz     {
3961bf26076SKevin Wolf         ret = block_job_add_bdrv(&s->common, "intermediate node", iter, 0,
3971bf26076SKevin Wolf                                  basic_flags, errp);
3981bf26076SKevin Wolf         if (ret < 0) {
3996bc30f19SStefan Hajnoczi             bdrv_graph_wrunlock();
4001bf26076SKevin Wolf             goto fail;
4011bf26076SKevin Wolf         }
40261b49e48SAlberto Garcia     }
4036bc30f19SStefan Hajnoczi     bdrv_graph_wrunlock();
40461b49e48SAlberto Garcia 
40567acfd21SMax Reitz     s->base_overlay = base_overlay;
40667acfd21SMax Reitz     s->above_base = above_base;
40713d8cc51SJeff Cody     s->backing_file_str = g_strdup(backing_file_str);
40872098a3aSPeter Krempa     s->backing_mask_protocol = backing_mask_protocol;
409205736f4SAndrey Shinkevich     s->cor_filter_bs = cor_filter_bs;
4100f6c9498SVladimir Sementsov-Ogievskiy     s->target_bs = bs;
411e7d22f8bSAlberto Garcia     s->bs_read_only = bs_read_only;
4124f1043b4SStefan Hajnoczi 
4131d809098SPaolo Bonzini     s->on_error = on_error;
4145ccac6f1SJohn Snow     trace_stream_start(bs, base, s);
415da01ff7fSKevin Wolf     job_start(&s->common.job);
416a170a91fSKevin Wolf     return;
417a170a91fSKevin Wolf 
418a170a91fSKevin Wolf fail:
4191bf26076SKevin Wolf     if (s) {
4201bf26076SKevin Wolf         job_early_fail(&s->common.job);
4211bf26076SKevin Wolf     }
422205736f4SAndrey Shinkevich     if (cor_filter_bs) {
423205736f4SAndrey Shinkevich         bdrv_cor_filter_drop(cor_filter_bs);
424205736f4SAndrey Shinkevich     }
425e7d22f8bSAlberto Garcia     if (bs_read_only) {
426e7d22f8bSAlberto Garcia         bdrv_reopen_set_read_only(bs, true, NULL);
427a170a91fSKevin Wolf     }
4289275fc72SKevin Wolf     return;
4299275fc72SKevin Wolf 
4309275fc72SKevin Wolf out_rdlock:
4319275fc72SKevin Wolf     bdrv_graph_rdunlock_main_loop();
4324f1043b4SStefan Hajnoczi }
433