Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Image streaming |
| 3 | * |
| 4 | * Copyright IBM, Corp. 2011 |
| 5 | * |
| 6 | * Authors: |
| 7 | * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com> |
| 8 | * |
| 9 | * This work is licensed under the terms of the GNU LGPL, version 2 or later. |
| 10 | * See the COPYING.LIB file in the top-level directory. |
| 11 | * |
| 12 | */ |
| 13 | |
Peter Maydell | 80c71a2 | 2016-01-18 18:01:42 +0000 | [diff] [blame] | 14 | #include "qemu/osdep.h" |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 15 | #include "trace.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 16 | #include "block/block_int.h" |
John Snow | c87621e | 2016-10-27 12:07:00 -0400 | [diff] [blame] | 17 | #include "block/blockjob_int.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 18 | #include "qapi/error.h" |
Markus Armbruster | cc7a8ea | 2015-03-17 17:22:46 +0100 | [diff] [blame] | 19 | #include "qapi/qmp/qerror.h" |
Paolo Bonzini | 6ef228f | 2012-05-09 16:09:46 +0200 | [diff] [blame] | 20 | #include "qemu/ratelimit.h" |
Max Reitz | 373340b | 2015-10-19 17:53:22 +0200 | [diff] [blame] | 21 | #include "sysemu/block-backend.h" |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 22 | |
| 23 | enum { |
| 24 | /* |
Vladimir Sementsov-Ogievskiy | 9913660 | 2019-07-25 13:05:49 +0300 | [diff] [blame] | 25 | * Maximum chunk size to feed to copy-on-read. This should be |
| 26 | * large enough to process multiple clusters in a single call, so |
| 27 | * that populating contiguous regions of the image is efficient. |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 28 | */ |
Vladimir Sementsov-Ogievskiy | 9913660 | 2019-07-25 13:05:49 +0300 | [diff] [blame] | 29 | STREAM_CHUNK = 512 * 1024, /* in bytes */ |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | typedef struct StreamBlockJob { |
| 33 | BlockJob common; |
Andrey Shinkevich | c624b01 | 2019-05-29 20:56:16 +0300 | [diff] [blame] | 34 | BlockDriverState *bottom; |
Paolo Bonzini | 1d80909 | 2012-09-28 17:22:59 +0200 | [diff] [blame] | 35 | BlockdevOnError on_error; |
Jeff Cody | 13d8cc5 | 2014-06-25 15:40:11 -0400 | [diff] [blame] | 36 | char *backing_file_str; |
Alberto Garcia | e7d22f8 | 2018-11-12 16:00:37 +0200 | [diff] [blame] | 37 | bool bs_read_only; |
Alberto Garcia | 6585493 | 2019-03-12 18:48:43 +0200 | [diff] [blame] | 38 | bool chain_frozen; |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 39 | } StreamBlockJob; |
| 40 | |
Kevin Wolf | 03e35d8 | 2016-04-12 15:15:49 +0200 | [diff] [blame] | 41 | static int coroutine_fn stream_populate(BlockBackend *blk, |
Vladimir Sementsov-Ogievskiy | 9913660 | 2019-07-25 13:05:49 +0300 | [diff] [blame] | 42 | int64_t offset, uint64_t bytes) |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 43 | { |
Eric Blake | 8493211 | 2017-07-07 07:44:41 -0500 | [diff] [blame] | 44 | assert(bytes < SIZE_MAX); |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 45 | |
Vladimir Sementsov-Ogievskiy | 9913660 | 2019-07-25 13:05:49 +0300 | [diff] [blame] | 46 | return blk_co_preadv(blk, offset, bytes, NULL, |
| 47 | BDRV_REQ_COPY_ON_READ | BDRV_REQ_PREFETCH); |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Alberto Garcia | 6585493 | 2019-03-12 18:48:43 +0200 | [diff] [blame] | 50 | static void stream_abort(Job *job) |
| 51 | { |
| 52 | StreamBlockJob *s = container_of(job, StreamBlockJob, common.job); |
| 53 | |
| 54 | if (s->chain_frozen) { |
| 55 | BlockJob *bjob = &s->common; |
Andrey Shinkevich | c624b01 | 2019-05-29 20:56:16 +0300 | [diff] [blame] | 56 | bdrv_unfreeze_backing_chain(blk_bs(bjob->blk), s->bottom); |
Alberto Garcia | 6585493 | 2019-03-12 18:48:43 +0200 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
John Snow | 1b57488 | 2018-09-06 09:02:16 -0400 | [diff] [blame] | 60 | static int stream_prepare(Job *job) |
Stefan Hajnoczi | f3e69be | 2014-10-21 12:03:57 +0100 | [diff] [blame] | 61 | { |
Kevin Wolf | 1908a55 | 2018-04-17 16:41:17 +0200 | [diff] [blame] | 62 | StreamBlockJob *s = container_of(job, StreamBlockJob, common.job); |
| 63 | BlockJob *bjob = &s->common; |
Kevin Wolf | 1908a55 | 2018-04-17 16:41:17 +0200 | [diff] [blame] | 64 | BlockDriverState *bs = blk_bs(bjob->blk); |
Andrey Shinkevich | c624b01 | 2019-05-29 20:56:16 +0300 | [diff] [blame] | 65 | BlockDriverState *base = backing_bs(s->bottom); |
Kevin Wolf | 12fa4af | 2017-02-17 20:42:32 +0100 | [diff] [blame] | 66 | Error *local_err = NULL; |
John Snow | 1b57488 | 2018-09-06 09:02:16 -0400 | [diff] [blame] | 67 | int ret = 0; |
Stefan Hajnoczi | f3e69be | 2014-10-21 12:03:57 +0100 | [diff] [blame] | 68 | |
Andrey Shinkevich | c624b01 | 2019-05-29 20:56:16 +0300 | [diff] [blame] | 69 | bdrv_unfreeze_backing_chain(bs, s->bottom); |
Alberto Garcia | 6585493 | 2019-03-12 18:48:43 +0200 | [diff] [blame] | 70 | s->chain_frozen = false; |
| 71 | |
John Snow | 1b57488 | 2018-09-06 09:02:16 -0400 | [diff] [blame] | 72 | if (bs->backing) { |
Stefan Hajnoczi | f3e69be | 2014-10-21 12:03:57 +0100 | [diff] [blame] | 73 | const char *base_id = NULL, *base_fmt = NULL; |
| 74 | if (base) { |
| 75 | base_id = s->backing_file_str; |
| 76 | if (base->drv) { |
| 77 | base_fmt = base->drv->format_name; |
| 78 | } |
| 79 | } |
Kevin Wolf | 12fa4af | 2017-02-17 20:42:32 +0100 | [diff] [blame] | 80 | bdrv_set_backing_hd(bs, base, &local_err); |
Max Reitz | 8441d82 | 2019-07-03 19:28:04 +0200 | [diff] [blame] | 81 | ret = bdrv_change_backing_file(bs, base_id, base_fmt); |
Kevin Wolf | 12fa4af | 2017-02-17 20:42:32 +0100 | [diff] [blame] | 82 | if (local_err) { |
| 83 | error_report_err(local_err); |
John Snow | 1b57488 | 2018-09-06 09:02:16 -0400 | [diff] [blame] | 84 | return -EPERM; |
Kevin Wolf | 12fa4af | 2017-02-17 20:42:32 +0100 | [diff] [blame] | 85 | } |
Stefan Hajnoczi | f3e69be | 2014-10-21 12:03:57 +0100 | [diff] [blame] | 86 | } |
| 87 | |
John Snow | 1b57488 | 2018-09-06 09:02:16 -0400 | [diff] [blame] | 88 | return ret; |
| 89 | } |
| 90 | |
| 91 | static void stream_clean(Job *job) |
| 92 | { |
| 93 | StreamBlockJob *s = container_of(job, StreamBlockJob, common.job); |
| 94 | BlockJob *bjob = &s->common; |
| 95 | BlockDriverState *bs = blk_bs(bjob->blk); |
| 96 | |
Alberto Garcia | 61b49e4 | 2016-10-28 10:08:10 +0300 | [diff] [blame] | 97 | /* Reopen the image back in read-only mode if necessary */ |
Alberto Garcia | e7d22f8 | 2018-11-12 16:00:37 +0200 | [diff] [blame] | 98 | if (s->bs_read_only) { |
Kevin Wolf | a170a91 | 2017-02-09 13:34:18 +0100 | [diff] [blame] | 99 | /* Give up write permissions before making it read-only */ |
Kevin Wolf | 1908a55 | 2018-04-17 16:41:17 +0200 | [diff] [blame] | 100 | blk_set_perm(bjob->blk, 0, BLK_PERM_ALL, &error_abort); |
Alberto Garcia | e7d22f8 | 2018-11-12 16:00:37 +0200 | [diff] [blame] | 101 | bdrv_reopen_set_read_only(bs, true, NULL); |
Alberto Garcia | 61b49e4 | 2016-10-28 10:08:10 +0300 | [diff] [blame] | 102 | } |
| 103 | |
Stefan Hajnoczi | f3e69be | 2014-10-21 12:03:57 +0100 | [diff] [blame] | 104 | g_free(s->backing_file_str); |
Stefan Hajnoczi | f3e69be | 2014-10-21 12:03:57 +0100 | [diff] [blame] | 105 | } |
| 106 | |
John Snow | f67432a | 2018-08-29 21:57:26 -0400 | [diff] [blame] | 107 | static int coroutine_fn stream_run(Job *job, Error **errp) |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 108 | { |
John Snow | f67432a | 2018-08-29 21:57:26 -0400 | [diff] [blame] | 109 | StreamBlockJob *s = container_of(job, StreamBlockJob, common.job); |
Kevin Wolf | 03e35d8 | 2016-04-12 15:15:49 +0200 | [diff] [blame] | 110 | BlockBackend *blk = s->common.blk; |
| 111 | BlockDriverState *bs = blk_bs(blk); |
Andrey Shinkevich | c624b01 | 2019-05-29 20:56:16 +0300 | [diff] [blame] | 112 | bool enable_cor = !backing_bs(s->bottom); |
Kevin Wolf | 05df8a6 | 2018-01-18 18:08:22 +0100 | [diff] [blame] | 113 | int64_t len; |
Eric Blake | d535435 | 2017-07-07 07:44:43 -0500 | [diff] [blame] | 114 | int64_t offset = 0; |
Sascha Silbe | f14a39c | 2016-06-28 17:28:41 +0200 | [diff] [blame] | 115 | uint64_t delay_ns = 0; |
Paolo Bonzini | 1d80909 | 2012-09-28 17:22:59 +0200 | [diff] [blame] | 116 | int error = 0; |
Eric Blake | 51b0a48 | 2017-07-07 07:44:59 -0500 | [diff] [blame] | 117 | int64_t n = 0; /* bytes */ |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 118 | |
Andrey Shinkevich | c624b01 | 2019-05-29 20:56:16 +0300 | [diff] [blame] | 119 | if (bs == s->bottom) { |
| 120 | /* Nothing to stream */ |
Andrey Shinkevich | 96a07d5 | 2019-05-29 20:56:15 +0300 | [diff] [blame] | 121 | return 0; |
Max Reitz | f4a193e | 2013-11-13 20:37:58 +0100 | [diff] [blame] | 122 | } |
| 123 | |
Kevin Wolf | 05df8a6 | 2018-01-18 18:08:22 +0100 | [diff] [blame] | 124 | len = bdrv_getlength(bs); |
| 125 | if (len < 0) { |
Andrey Shinkevich | 96a07d5 | 2019-05-29 20:56:15 +0300 | [diff] [blame] | 126 | return len; |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 127 | } |
Kevin Wolf | 30a5c88 | 2018-05-04 12:17:20 +0200 | [diff] [blame] | 128 | job_progress_set_remaining(&s->common.job, len); |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 129 | |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 130 | /* Turn on copy-on-read for the whole block device so that guest read |
| 131 | * requests help us make progress. Only do this when copying the entire |
| 132 | * backing chain since the copy-on-read operation does not take base into |
| 133 | * account. |
| 134 | */ |
Andrey Shinkevich | c624b01 | 2019-05-29 20:56:16 +0300 | [diff] [blame] | 135 | if (enable_cor) { |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 136 | bdrv_enable_copy_on_read(bs); |
| 137 | } |
| 138 | |
Kevin Wolf | 05df8a6 | 2018-01-18 18:08:22 +0100 | [diff] [blame] | 139 | for ( ; offset < len; offset += n) { |
Paolo Bonzini | f9749f2 | 2012-05-08 16:52:00 +0200 | [diff] [blame] | 140 | bool copy; |
Chen Qun | 35c9453 | 2020-03-02 21:07:04 +0800 | [diff] [blame] | 141 | int ret; |
Paolo Bonzini | 4513eaf | 2012-05-08 16:51:45 +0200 | [diff] [blame] | 142 | |
Paolo Bonzini | 4513eaf | 2012-05-08 16:51:45 +0200 | [diff] [blame] | 143 | /* Note that even when no rate limit is applied we need to yield |
Kevin Wolf | c57b665 | 2012-11-13 16:35:13 +0100 | [diff] [blame] | 144 | * with no pending I/O here so that bdrv_drain_all() returns. |
Paolo Bonzini | 4513eaf | 2012-05-08 16:51:45 +0200 | [diff] [blame] | 145 | */ |
Kevin Wolf | 5d43e86 | 2018-04-18 16:32:20 +0200 | [diff] [blame] | 146 | job_sleep_ns(&s->common.job, delay_ns); |
Kevin Wolf | daa7f2f | 2018-04-17 12:56:07 +0200 | [diff] [blame] | 147 | if (job_is_cancelled(&s->common.job)) { |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 148 | break; |
| 149 | } |
| 150 | |
Stefan Weil | c3e4f43 | 2013-09-22 08:19:10 +0200 | [diff] [blame] | 151 | copy = false; |
| 152 | |
Vladimir Sementsov-Ogievskiy | 9913660 | 2019-07-25 13:05:49 +0300 | [diff] [blame] | 153 | ret = bdrv_is_allocated(bs, offset, STREAM_CHUNK, &n); |
Paolo Bonzini | f9749f2 | 2012-05-08 16:52:00 +0200 | [diff] [blame] | 154 | if (ret == 1) { |
| 155 | /* Allocated in the top, no need to copy. */ |
Paolo Bonzini | d663640 | 2013-09-04 19:00:25 +0200 | [diff] [blame] | 156 | } else if (ret >= 0) { |
Paolo Bonzini | f9749f2 | 2012-05-08 16:52:00 +0200 | [diff] [blame] | 157 | /* Copy if allocated in the intermediate images. Limit to the |
Eric Blake | d535435 | 2017-07-07 07:44:43 -0500 | [diff] [blame] | 158 | * known-unallocated area [offset, offset+n*BDRV_SECTOR_SIZE). */ |
Andrey Shinkevich | c624b01 | 2019-05-29 20:56:16 +0300 | [diff] [blame] | 159 | ret = bdrv_is_allocated_above(backing_bs(bs), s->bottom, true, |
Eric Blake | 51b0a48 | 2017-07-07 07:44:59 -0500 | [diff] [blame] | 160 | offset, n, &n); |
Stefan Hajnoczi | 571cd9d | 2012-08-28 15:26:48 +0100 | [diff] [blame] | 161 | /* Finish early if end of backing file has been reached */ |
| 162 | if (ret == 0 && n == 0) { |
Kevin Wolf | 05df8a6 | 2018-01-18 18:08:22 +0100 | [diff] [blame] | 163 | n = len - offset; |
Stefan Hajnoczi | 571cd9d | 2012-08-28 15:26:48 +0100 | [diff] [blame] | 164 | } |
| 165 | |
Paolo Bonzini | f9749f2 | 2012-05-08 16:52:00 +0200 | [diff] [blame] | 166 | copy = (ret == 1); |
| 167 | } |
Eric Blake | 51b0a48 | 2017-07-07 07:44:59 -0500 | [diff] [blame] | 168 | trace_stream_one_iteration(s, offset, n, ret); |
Stefan Weil | c3e4f43 | 2013-09-22 08:19:10 +0200 | [diff] [blame] | 169 | if (copy) { |
Vladimir Sementsov-Ogievskiy | 9913660 | 2019-07-25 13:05:49 +0300 | [diff] [blame] | 170 | ret = stream_populate(blk, offset, n); |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 171 | } |
| 172 | if (ret < 0) { |
Paolo Bonzini | 1d80909 | 2012-09-28 17:22:59 +0200 | [diff] [blame] | 173 | BlockErrorAction action = |
Kevin Wolf | 81e254d | 2016-04-18 11:36:38 +0200 | [diff] [blame] | 174 | block_job_error_action(&s->common, s->on_error, true, -ret); |
Wenchao Xia | a589569 | 2014-06-18 08:43:30 +0200 | [diff] [blame] | 175 | if (action == BLOCK_ERROR_ACTION_STOP) { |
Paolo Bonzini | 1d80909 | 2012-09-28 17:22:59 +0200 | [diff] [blame] | 176 | n = 0; |
| 177 | continue; |
| 178 | } |
| 179 | if (error == 0) { |
| 180 | error = ret; |
| 181 | } |
Wenchao Xia | a589569 | 2014-06-18 08:43:30 +0200 | [diff] [blame] | 182 | if (action == BLOCK_ERROR_ACTION_REPORT) { |
Paolo Bonzini | 1d80909 | 2012-09-28 17:22:59 +0200 | [diff] [blame] | 183 | break; |
| 184 | } |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | /* Publish progress */ |
Kevin Wolf | 30a5c88 | 2018-05-04 12:17:20 +0200 | [diff] [blame] | 188 | job_progress_update(&s->common.job, n); |
Kevin Wolf | dee81d5 | 2018-01-18 21:19:38 +0100 | [diff] [blame] | 189 | if (copy) { |
| 190 | delay_ns = block_job_ratelimit_get_delay(&s->common, n); |
Kevin Wolf | 2fe4bba | 2018-01-18 21:23:52 +0100 | [diff] [blame] | 191 | } else { |
| 192 | delay_ns = 0; |
Sascha Silbe | f14a39c | 2016-06-28 17:28:41 +0200 | [diff] [blame] | 193 | } |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Andrey Shinkevich | c624b01 | 2019-05-29 20:56:16 +0300 | [diff] [blame] | 196 | if (enable_cor) { |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 197 | bdrv_disable_copy_on_read(bs); |
| 198 | } |
| 199 | |
Andrey Shinkevich | 96a07d5 | 2019-05-29 20:56:15 +0300 | [diff] [blame] | 200 | /* Do not remove the backing file if an error was there but ignored. */ |
| 201 | return error; |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Fam Zheng | 3fc4b10 | 2013-10-08 17:29:38 +0800 | [diff] [blame] | 204 | static const BlockJobDriver stream_job_driver = { |
Kevin Wolf | 33e9e9b | 2018-04-12 17:29:59 +0200 | [diff] [blame] | 205 | .job_driver = { |
| 206 | .instance_size = sizeof(StreamBlockJob), |
Kevin Wolf | 252291e | 2018-04-12 17:57:08 +0200 | [diff] [blame] | 207 | .job_type = JOB_TYPE_STREAM, |
Kevin Wolf | 80fa2c7 | 2018-04-13 18:50:05 +0200 | [diff] [blame] | 208 | .free = block_job_free, |
John Snow | f67432a | 2018-08-29 21:57:26 -0400 | [diff] [blame] | 209 | .run = stream_run, |
John Snow | 1b57488 | 2018-09-06 09:02:16 -0400 | [diff] [blame] | 210 | .prepare = stream_prepare, |
Alberto Garcia | 6585493 | 2019-03-12 18:48:43 +0200 | [diff] [blame] | 211 | .abort = stream_abort, |
John Snow | 1b57488 | 2018-09-06 09:02:16 -0400 | [diff] [blame] | 212 | .clean = stream_clean, |
Kevin Wolf | b15de82 | 2018-04-18 17:10:26 +0200 | [diff] [blame] | 213 | .user_resume = block_job_user_resume, |
Kevin Wolf | 33e9e9b | 2018-04-12 17:29:59 +0200 | [diff] [blame] | 214 | }, |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 215 | }; |
| 216 | |
Alberto Garcia | 2323322 | 2016-07-05 17:28:59 +0300 | [diff] [blame] | 217 | void stream_start(const char *job_id, BlockDriverState *bs, |
| 218 | BlockDriverState *base, const char *backing_file_str, |
John Snow | cf6320d | 2018-09-06 09:02:12 -0400 | [diff] [blame] | 219 | int creation_flags, int64_t speed, |
| 220 | BlockdevOnError on_error, Error **errp) |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 221 | { |
| 222 | StreamBlockJob *s; |
Alberto Garcia | 61b49e4 | 2016-10-28 10:08:10 +0300 | [diff] [blame] | 223 | BlockDriverState *iter; |
Alberto Garcia | e7d22f8 | 2018-11-12 16:00:37 +0200 | [diff] [blame] | 224 | bool bs_read_only; |
Andrey Shinkevich | c624b01 | 2019-05-29 20:56:16 +0300 | [diff] [blame] | 225 | int basic_flags = BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED; |
| 226 | BlockDriverState *bottom = bdrv_find_overlay(bs, base); |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 227 | |
Andrey Shinkevich | c624b01 | 2019-05-29 20:56:16 +0300 | [diff] [blame] | 228 | if (bdrv_freeze_backing_chain(bs, bottom, errp) < 0) { |
Alberto Garcia | 20509c4 | 2019-03-28 18:25:10 +0200 | [diff] [blame] | 229 | return; |
| 230 | } |
| 231 | |
Alberto Garcia | 61b49e4 | 2016-10-28 10:08:10 +0300 | [diff] [blame] | 232 | /* Make sure that the image is opened in read-write mode */ |
Alberto Garcia | e7d22f8 | 2018-11-12 16:00:37 +0200 | [diff] [blame] | 233 | bs_read_only = bdrv_is_read_only(bs); |
| 234 | if (bs_read_only) { |
| 235 | if (bdrv_reopen_set_read_only(bs, false, errp) != 0) { |
Alberto Garcia | 20509c4 | 2019-03-28 18:25:10 +0200 | [diff] [blame] | 236 | bs_read_only = false; |
| 237 | goto fail; |
Alberto Garcia | 61b49e4 | 2016-10-28 10:08:10 +0300 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
Kevin Wolf | a170a91 | 2017-02-09 13:34:18 +0100 | [diff] [blame] | 241 | /* Prevent concurrent jobs trying to modify the graph structure here, we |
| 242 | * already have our own plans. Also don't allow resize as the image size is |
| 243 | * queried only at the job start and then cached. */ |
John Snow | 75859b9 | 2018-03-10 03:27:27 -0500 | [diff] [blame] | 244 | s = block_job_create(job_id, &stream_job_driver, NULL, bs, |
Andrey Shinkevich | c624b01 | 2019-05-29 20:56:16 +0300 | [diff] [blame] | 245 | basic_flags | BLK_PERM_GRAPH_MOD, |
| 246 | basic_flags | BLK_PERM_WRITE, |
John Snow | cf6320d | 2018-09-06 09:02:12 -0400 | [diff] [blame] | 247 | speed, creation_flags, NULL, NULL, errp); |
Kevin Wolf | a170a91 | 2017-02-09 13:34:18 +0100 | [diff] [blame] | 248 | if (!s) { |
| 249 | goto fail; |
| 250 | } |
| 251 | |
| 252 | /* Block all intermediate nodes between bs and base, because they will |
| 253 | * disappear from the chain after this operation. The streaming job reads |
Andrey Shinkevich | c624b01 | 2019-05-29 20:56:16 +0300 | [diff] [blame] | 254 | * every block only once, assuming that it doesn't change, so forbid writes |
| 255 | * and resizes. Reassign the base node pointer because the backing BS of the |
| 256 | * bottom node might change after the call to bdrv_reopen_set_read_only() |
| 257 | * due to parallel block jobs running. |
| 258 | */ |
| 259 | base = backing_bs(bottom); |
Alberto Garcia | 61b49e4 | 2016-10-28 10:08:10 +0300 | [diff] [blame] | 260 | for (iter = backing_bs(bs); iter && iter != base; iter = backing_bs(iter)) { |
Kevin Wolf | 76d554e | 2017-01-17 11:56:42 +0100 | [diff] [blame] | 261 | block_job_add_bdrv(&s->common, "intermediate node", iter, 0, |
Andrey Shinkevich | c624b01 | 2019-05-29 20:56:16 +0300 | [diff] [blame] | 262 | basic_flags, &error_abort); |
Alberto Garcia | 61b49e4 | 2016-10-28 10:08:10 +0300 | [diff] [blame] | 263 | } |
| 264 | |
Andrey Shinkevich | c624b01 | 2019-05-29 20:56:16 +0300 | [diff] [blame] | 265 | s->bottom = bottom; |
Jeff Cody | 13d8cc5 | 2014-06-25 15:40:11 -0400 | [diff] [blame] | 266 | s->backing_file_str = g_strdup(backing_file_str); |
Alberto Garcia | e7d22f8 | 2018-11-12 16:00:37 +0200 | [diff] [blame] | 267 | s->bs_read_only = bs_read_only; |
Alberto Garcia | 6585493 | 2019-03-12 18:48:43 +0200 | [diff] [blame] | 268 | s->chain_frozen = true; |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 269 | |
Paolo Bonzini | 1d80909 | 2012-09-28 17:22:59 +0200 | [diff] [blame] | 270 | s->on_error = on_error; |
John Snow | 5ccac6f | 2016-11-08 01:50:37 -0500 | [diff] [blame] | 271 | trace_stream_start(bs, base, s); |
Kevin Wolf | da01ff7 | 2018-04-13 17:31:02 +0200 | [diff] [blame] | 272 | job_start(&s->common.job); |
Kevin Wolf | a170a91 | 2017-02-09 13:34:18 +0100 | [diff] [blame] | 273 | return; |
| 274 | |
| 275 | fail: |
Alberto Garcia | e7d22f8 | 2018-11-12 16:00:37 +0200 | [diff] [blame] | 276 | if (bs_read_only) { |
| 277 | bdrv_reopen_set_read_only(bs, true, NULL); |
Kevin Wolf | a170a91 | 2017-02-09 13:34:18 +0100 | [diff] [blame] | 278 | } |
Max Reitz | 17a7c39 | 2019-07-03 19:28:03 +0200 | [diff] [blame] | 279 | bdrv_unfreeze_backing_chain(bs, bottom); |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 280 | } |