OSDN Git Service

qapi: block-stream: add "bottom" argument
[qmiga/qemu.git] / block / stream.c
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
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"
22
23 enum {
24     /*
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.
28      */
29     STREAM_CHUNK = 512 * 1024, /* in bytes */
30 };
31
32 typedef struct StreamBlockJob {
33     BlockJob common;
34     BlockDriverState *base_overlay; /* COW overlay (stream from this) */
35     BlockDriverState *above_base;   /* Node directly above the base */
36     BlockdevOnError on_error;
37     char *backing_file_str;
38     bool bs_read_only;
39     bool chain_frozen;
40 } StreamBlockJob;
41
42 static int coroutine_fn stream_populate(BlockBackend *blk,
43                                         int64_t offset, uint64_t bytes)
44 {
45     assert(bytes < SIZE_MAX);
46
47     return blk_co_preadv(blk, offset, bytes, NULL,
48                          BDRV_REQ_COPY_ON_READ | BDRV_REQ_PREFETCH);
49 }
50
51 static void stream_abort(Job *job)
52 {
53     StreamBlockJob *s = container_of(job, StreamBlockJob, common.job);
54
55     if (s->chain_frozen) {
56         BlockJob *bjob = &s->common;
57         bdrv_unfreeze_backing_chain(blk_bs(bjob->blk), s->above_base);
58     }
59 }
60
61 static int stream_prepare(Job *job)
62 {
63     StreamBlockJob *s = container_of(job, StreamBlockJob, common.job);
64     BlockJob *bjob = &s->common;
65     BlockDriverState *bs = blk_bs(bjob->blk);
66     BlockDriverState *unfiltered_bs = bdrv_skip_filters(bs);
67     BlockDriverState *base = bdrv_filter_or_cow_bs(s->above_base);
68     BlockDriverState *unfiltered_base = bdrv_skip_filters(base);
69     Error *local_err = NULL;
70     int ret = 0;
71
72     bdrv_unfreeze_backing_chain(bs, s->above_base);
73     s->chain_frozen = false;
74
75     if (bdrv_cow_child(unfiltered_bs)) {
76         const char *base_id = NULL, *base_fmt = NULL;
77         if (unfiltered_base) {
78             base_id = s->backing_file_str ?: unfiltered_base->filename;
79             if (unfiltered_base->drv) {
80                 base_fmt = unfiltered_base->drv->format_name;
81             }
82         }
83         bdrv_set_backing_hd(unfiltered_bs, base, &local_err);
84         ret = bdrv_change_backing_file(unfiltered_bs, base_id, base_fmt, false);
85         if (local_err) {
86             error_report_err(local_err);
87             return -EPERM;
88         }
89     }
90
91     return ret;
92 }
93
94 static void stream_clean(Job *job)
95 {
96     StreamBlockJob *s = container_of(job, StreamBlockJob, common.job);
97     BlockJob *bjob = &s->common;
98     BlockDriverState *bs = blk_bs(bjob->blk);
99
100     /* Reopen the image back in read-only mode if necessary */
101     if (s->bs_read_only) {
102         /* Give up write permissions before making it read-only */
103         blk_set_perm(bjob->blk, 0, BLK_PERM_ALL, &error_abort);
104         bdrv_reopen_set_read_only(bs, true, NULL);
105     }
106
107     g_free(s->backing_file_str);
108 }
109
110 static int coroutine_fn stream_run(Job *job, Error **errp)
111 {
112     StreamBlockJob *s = container_of(job, StreamBlockJob, common.job);
113     BlockBackend *blk = s->common.blk;
114     BlockDriverState *bs = blk_bs(blk);
115     BlockDriverState *unfiltered_bs = bdrv_skip_filters(bs);
116     bool enable_cor = !bdrv_cow_child(s->base_overlay);
117     int64_t len;
118     int64_t offset = 0;
119     uint64_t delay_ns = 0;
120     int error = 0;
121     int64_t n = 0; /* bytes */
122
123     if (unfiltered_bs == s->base_overlay) {
124         /* Nothing to stream */
125         return 0;
126     }
127
128     len = bdrv_getlength(bs);
129     if (len < 0) {
130         return len;
131     }
132     job_progress_set_remaining(&s->common.job, len);
133
134     /* Turn on copy-on-read for the whole block device so that guest read
135      * requests help us make progress.  Only do this when copying the entire
136      * backing chain since the copy-on-read operation does not take base into
137      * account.
138      */
139     if (enable_cor) {
140         bdrv_enable_copy_on_read(bs);
141     }
142
143     for ( ; offset < len; offset += n) {
144         bool copy;
145         int ret;
146
147         /* Note that even when no rate limit is applied we need to yield
148          * with no pending I/O here so that bdrv_drain_all() returns.
149          */
150         job_sleep_ns(&s->common.job, delay_ns);
151         if (job_is_cancelled(&s->common.job)) {
152             break;
153         }
154
155         copy = false;
156
157         ret = bdrv_is_allocated(unfiltered_bs, offset, STREAM_CHUNK, &n);
158         if (ret == 1) {
159             /* Allocated in the top, no need to copy.  */
160         } else if (ret >= 0) {
161             /* Copy if allocated in the intermediate images.  Limit to the
162              * known-unallocated area [offset, offset+n*BDRV_SECTOR_SIZE).  */
163             ret = bdrv_is_allocated_above(bdrv_cow_bs(unfiltered_bs),
164                                           s->base_overlay, true,
165                                           offset, n, &n);
166             /* Finish early if end of backing file has been reached */
167             if (ret == 0 && n == 0) {
168                 n = len - offset;
169             }
170
171             copy = (ret > 0);
172         }
173         trace_stream_one_iteration(s, offset, n, ret);
174         if (copy) {
175             ret = stream_populate(blk, offset, n);
176         }
177         if (ret < 0) {
178             BlockErrorAction action =
179                 block_job_error_action(&s->common, s->on_error, true, -ret);
180             if (action == BLOCK_ERROR_ACTION_STOP) {
181                 n = 0;
182                 continue;
183             }
184             if (error == 0) {
185                 error = ret;
186             }
187             if (action == BLOCK_ERROR_ACTION_REPORT) {
188                 break;
189             }
190         }
191
192         /* Publish progress */
193         job_progress_update(&s->common.job, n);
194         if (copy) {
195             delay_ns = block_job_ratelimit_get_delay(&s->common, n);
196         } else {
197             delay_ns = 0;
198         }
199     }
200
201     if (enable_cor) {
202         bdrv_disable_copy_on_read(bs);
203     }
204
205     /* Do not remove the backing file if an error was there but ignored. */
206     return error;
207 }
208
209 static const BlockJobDriver stream_job_driver = {
210     .job_driver = {
211         .instance_size = sizeof(StreamBlockJob),
212         .job_type      = JOB_TYPE_STREAM,
213         .free          = block_job_free,
214         .run           = stream_run,
215         .prepare       = stream_prepare,
216         .abort         = stream_abort,
217         .clean         = stream_clean,
218         .user_resume   = block_job_user_resume,
219     },
220 };
221
222 void stream_start(const char *job_id, BlockDriverState *bs,
223                   BlockDriverState *base, const char *backing_file_str,
224                   BlockDriverState *bottom,
225                   int creation_flags, int64_t speed,
226                   BlockdevOnError on_error,
227                   const char *filter_node_name,
228                   Error **errp)
229 {
230     StreamBlockJob *s;
231     BlockDriverState *iter;
232     bool bs_read_only;
233     int basic_flags = BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED;
234     BlockDriverState *base_overlay;
235     BlockDriverState *above_base;
236
237     assert(!(base && bottom));
238     assert(!(backing_file_str && bottom));
239
240     if (bottom) {
241         /*
242          * New simple interface. The code is written in terms of old interface
243          * with @base parameter (still, it doesn't freeze link to base, so in
244          * this mean old code is correct for new interface). So, for now, just
245          * emulate base_overlay and above_base. Still, when old interface
246          * finally removed, we should refactor code to use only "bottom", but
247          * not "*base*" things.
248          */
249         assert(!bottom->drv->is_filter);
250         base_overlay = above_base = bottom;
251     } else {
252         base_overlay = bdrv_find_overlay(bs, base);
253         if (!base_overlay) {
254             error_setg(errp, "'%s' is not in the backing chain of '%s'",
255                        base->node_name, bs->node_name);
256             return;
257         }
258
259         /*
260          * Find the node directly above @base.  @base_overlay is a COW overlay,
261          * so it must have a bdrv_cow_child(), but it is the immediate overlay
262          * of @base, so between the two there can only be filters.
263          */
264         above_base = base_overlay;
265         if (bdrv_cow_bs(above_base) != base) {
266             above_base = bdrv_cow_bs(above_base);
267             while (bdrv_filter_bs(above_base) != base) {
268                 above_base = bdrv_filter_bs(above_base);
269             }
270         }
271     }
272
273     if (bdrv_freeze_backing_chain(bs, above_base, errp) < 0) {
274         return;
275     }
276
277     /* Make sure that the image is opened in read-write mode */
278     bs_read_only = bdrv_is_read_only(bs);
279     if (bs_read_only) {
280         if (bdrv_reopen_set_read_only(bs, false, errp) != 0) {
281             bs_read_only = false;
282             goto fail;
283         }
284     }
285
286     /* Prevent concurrent jobs trying to modify the graph structure here, we
287      * already have our own plans. Also don't allow resize as the image size is
288      * queried only at the job start and then cached. */
289     s = block_job_create(job_id, &stream_job_driver, NULL, bs,
290                          basic_flags | BLK_PERM_GRAPH_MOD,
291                          basic_flags | BLK_PERM_WRITE,
292                          speed, creation_flags, NULL, NULL, errp);
293     if (!s) {
294         goto fail;
295     }
296
297     /* Block all intermediate nodes between bs and base, because they will
298      * disappear from the chain after this operation. The streaming job reads
299      * every block only once, assuming that it doesn't change, so forbid writes
300      * and resizes. Reassign the base node pointer because the backing BS of the
301      * bottom node might change after the call to bdrv_reopen_set_read_only()
302      * due to parallel block jobs running.
303      * above_base node might change after the call to
304      * bdrv_reopen_set_read_only() due to parallel block jobs running.
305      */
306     base = bdrv_filter_or_cow_bs(above_base);
307     for (iter = bdrv_filter_or_cow_bs(bs); iter != base;
308          iter = bdrv_filter_or_cow_bs(iter))
309     {
310         block_job_add_bdrv(&s->common, "intermediate node", iter, 0,
311                            basic_flags, &error_abort);
312     }
313
314     s->base_overlay = base_overlay;
315     s->above_base = above_base;
316     s->backing_file_str = g_strdup(backing_file_str);
317     s->bs_read_only = bs_read_only;
318     s->chain_frozen = true;
319
320     s->on_error = on_error;
321     trace_stream_start(bs, base, s);
322     job_start(&s->common.job);
323     return;
324
325 fail:
326     if (bs_read_only) {
327         bdrv_reopen_set_read_only(bs, true, NULL);
328     }
329     bdrv_unfreeze_backing_chain(bs, above_base);
330 }