Merge remote-tracking branch 'remotes/thibault/tags/samuel-thibault' into staging
[qemu/ar7.git] / block / stream.c
blobe14579ff8091cf6e10701d459d39efd3807b3e2a
1 /*
2 * Image streaming
4 * Copyright IBM, Corp. 2011
6 * Authors:
7 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
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.
14 #include "qemu/osdep.h"
15 #include "trace.h"
16 #include "block/block_int.h"
17 #include "block/blockjob_int.h"
18 #include "qapi/error.h"
19 #include "qapi/qmp/qerror.h"
20 #include "qemu/ratelimit.h"
21 #include "sysemu/block-backend.h"
23 enum {
25 * Size of data buffer for populating the image file. This should be large
26 * enough to process multiple clusters in a single call, so that populating
27 * contiguous regions of the image is efficient.
29 STREAM_BUFFER_SIZE = 512 * 1024, /* in bytes */
32 typedef struct StreamBlockJob {
33 BlockJob common;
34 BlockDriverState *base;
35 BlockdevOnError on_error;
36 char *backing_file_str;
37 bool bs_read_only;
38 } StreamBlockJob;
40 static int coroutine_fn stream_populate(BlockBackend *blk,
41 int64_t offset, uint64_t bytes,
42 void *buf)
44 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, bytes);
46 assert(bytes < SIZE_MAX);
48 /* Copy-on-read the unallocated clusters */
49 return blk_co_preadv(blk, offset, qiov.size, &qiov, BDRV_REQ_COPY_ON_READ);
52 static int stream_prepare(Job *job)
54 StreamBlockJob *s = container_of(job, StreamBlockJob, common.job);
55 BlockJob *bjob = &s->common;
56 BlockDriverState *bs = blk_bs(bjob->blk);
57 BlockDriverState *base = s->base;
58 Error *local_err = NULL;
59 int ret = 0;
61 if (bs->backing) {
62 const char *base_id = NULL, *base_fmt = NULL;
63 if (base) {
64 base_id = s->backing_file_str;
65 if (base->drv) {
66 base_fmt = base->drv->format_name;
69 ret = bdrv_change_backing_file(bs, base_id, base_fmt);
70 bdrv_set_backing_hd(bs, base, &local_err);
71 if (local_err) {
72 error_report_err(local_err);
73 return -EPERM;
77 return ret;
80 static void stream_clean(Job *job)
82 StreamBlockJob *s = container_of(job, StreamBlockJob, common.job);
83 BlockJob *bjob = &s->common;
84 BlockDriverState *bs = blk_bs(bjob->blk);
86 /* Reopen the image back in read-only mode if necessary */
87 if (s->bs_read_only) {
88 /* Give up write permissions before making it read-only */
89 blk_set_perm(bjob->blk, 0, BLK_PERM_ALL, &error_abort);
90 bdrv_reopen_set_read_only(bs, true, NULL);
93 g_free(s->backing_file_str);
96 static int coroutine_fn stream_run(Job *job, Error **errp)
98 StreamBlockJob *s = container_of(job, StreamBlockJob, common.job);
99 BlockBackend *blk = s->common.blk;
100 BlockDriverState *bs = blk_bs(blk);
101 BlockDriverState *base = s->base;
102 int64_t len;
103 int64_t offset = 0;
104 uint64_t delay_ns = 0;
105 int error = 0;
106 int ret = 0;
107 int64_t n = 0; /* bytes */
108 void *buf;
110 if (!bs->backing) {
111 goto out;
114 len = bdrv_getlength(bs);
115 if (len < 0) {
116 ret = len;
117 goto out;
119 job_progress_set_remaining(&s->common.job, len);
121 buf = qemu_blockalign(bs, STREAM_BUFFER_SIZE);
123 /* Turn on copy-on-read for the whole block device so that guest read
124 * requests help us make progress. Only do this when copying the entire
125 * backing chain since the copy-on-read operation does not take base into
126 * account.
128 if (!base) {
129 bdrv_enable_copy_on_read(bs);
132 for ( ; offset < len; offset += n) {
133 bool copy;
135 /* Note that even when no rate limit is applied we need to yield
136 * with no pending I/O here so that bdrv_drain_all() returns.
138 job_sleep_ns(&s->common.job, delay_ns);
139 if (job_is_cancelled(&s->common.job)) {
140 break;
143 copy = false;
145 ret = bdrv_is_allocated(bs, offset, STREAM_BUFFER_SIZE, &n);
146 if (ret == 1) {
147 /* Allocated in the top, no need to copy. */
148 } else if (ret >= 0) {
149 /* Copy if allocated in the intermediate images. Limit to the
150 * known-unallocated area [offset, offset+n*BDRV_SECTOR_SIZE). */
151 ret = bdrv_is_allocated_above(backing_bs(bs), base,
152 offset, n, &n);
154 /* Finish early if end of backing file has been reached */
155 if (ret == 0 && n == 0) {
156 n = len - offset;
159 copy = (ret == 1);
161 trace_stream_one_iteration(s, offset, n, ret);
162 if (copy) {
163 ret = stream_populate(blk, offset, n, buf);
165 if (ret < 0) {
166 BlockErrorAction action =
167 block_job_error_action(&s->common, s->on_error, true, -ret);
168 if (action == BLOCK_ERROR_ACTION_STOP) {
169 n = 0;
170 continue;
172 if (error == 0) {
173 error = ret;
175 if (action == BLOCK_ERROR_ACTION_REPORT) {
176 break;
179 ret = 0;
181 /* Publish progress */
182 job_progress_update(&s->common.job, n);
183 if (copy) {
184 delay_ns = block_job_ratelimit_get_delay(&s->common, n);
185 } else {
186 delay_ns = 0;
190 if (!base) {
191 bdrv_disable_copy_on_read(bs);
194 /* Do not remove the backing file if an error was there but ignored. */
195 ret = error;
197 qemu_vfree(buf);
199 out:
200 /* Modify backing chain and close BDSes in main loop */
201 return ret;
204 static const BlockJobDriver stream_job_driver = {
205 .job_driver = {
206 .instance_size = sizeof(StreamBlockJob),
207 .job_type = JOB_TYPE_STREAM,
208 .free = block_job_free,
209 .run = stream_run,
210 .prepare = stream_prepare,
211 .clean = stream_clean,
212 .user_resume = block_job_user_resume,
213 .drain = block_job_drain,
217 void stream_start(const char *job_id, BlockDriverState *bs,
218 BlockDriverState *base, const char *backing_file_str,
219 int creation_flags, int64_t speed,
220 BlockdevOnError on_error, Error **errp)
222 StreamBlockJob *s;
223 BlockDriverState *iter;
224 bool bs_read_only;
226 /* Make sure that the image is opened in read-write mode */
227 bs_read_only = bdrv_is_read_only(bs);
228 if (bs_read_only) {
229 if (bdrv_reopen_set_read_only(bs, false, errp) != 0) {
230 return;
234 /* Prevent concurrent jobs trying to modify the graph structure here, we
235 * already have our own plans. Also don't allow resize as the image size is
236 * queried only at the job start and then cached. */
237 s = block_job_create(job_id, &stream_job_driver, NULL, bs,
238 BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED |
239 BLK_PERM_GRAPH_MOD,
240 BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED |
241 BLK_PERM_WRITE,
242 speed, creation_flags, NULL, NULL, errp);
243 if (!s) {
244 goto fail;
247 /* Block all intermediate nodes between bs and base, because they will
248 * disappear from the chain after this operation. The streaming job reads
249 * every block only once, assuming that it doesn't change, so block writes
250 * and resizes. */
251 for (iter = backing_bs(bs); iter && iter != base; iter = backing_bs(iter)) {
252 block_job_add_bdrv(&s->common, "intermediate node", iter, 0,
253 BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED,
254 &error_abort);
257 s->base = base;
258 s->backing_file_str = g_strdup(backing_file_str);
259 s->bs_read_only = bs_read_only;
261 s->on_error = on_error;
262 trace_stream_start(bs, base, s);
263 job_start(&s->common.job);
264 return;
266 fail:
267 if (bs_read_only) {
268 bdrv_reopen_set_read_only(bs, true, NULL);