OSDN Git Service

block: Mark bdrv_co_refresh_total_sectors() and callers GRAPH_RDLOCK
[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 "qapi/qmp/qdict.h"
21 #include "qemu/ratelimit.h"
22 #include "sysemu/block-backend.h"
23 #include "block/copy-on-read.h"
24
25 enum {
26     /*
27      * Maximum chunk size to feed to copy-on-read.  This should be
28      * large enough to process multiple clusters in a single call, so
29      * that populating contiguous regions of the image is efficient.
30      */
31     STREAM_CHUNK = 512 * 1024, /* in bytes */
32 };
33
34 typedef struct StreamBlockJob {
35     BlockJob common;
36     BlockBackend *blk;
37     BlockDriverState *base_overlay; /* COW overlay (stream from this) */
38     BlockDriverState *above_base;   /* Node directly above the base */
39     BlockDriverState *cor_filter_bs;
40     BlockDriverState *target_bs;
41     BlockdevOnError on_error;
42     char *backing_file_str;
43     bool bs_read_only;
44 } StreamBlockJob;
45
46 static int coroutine_fn stream_populate(BlockBackend *blk,
47                                         int64_t offset, uint64_t bytes)
48 {
49     assert(bytes < SIZE_MAX);
50
51     return blk_co_preadv(blk, offset, bytes, NULL, BDRV_REQ_PREFETCH);
52 }
53
54 static int stream_prepare(Job *job)
55 {
56     StreamBlockJob *s = container_of(job, StreamBlockJob, common.job);
57     BlockDriverState *unfiltered_bs = bdrv_skip_filters(s->target_bs);
58     BlockDriverState *base;
59     BlockDriverState *unfiltered_base;
60     Error *local_err = NULL;
61     int ret = 0;
62
63     /* We should drop filter at this point, as filter hold the backing chain */
64     bdrv_cor_filter_drop(s->cor_filter_bs);
65     s->cor_filter_bs = NULL;
66
67     /*
68      * bdrv_set_backing_hd() requires that unfiltered_bs is drained. Drain
69      * already here and use bdrv_set_backing_hd_drained() instead because
70      * the polling during drained_begin() might change the graph, and if we do
71      * this only later, we may end up working with the wrong base node (or it
72      * might even have gone away by the time we want to use it).
73      */
74     bdrv_drained_begin(unfiltered_bs);
75
76     base = bdrv_filter_or_cow_bs(s->above_base);
77     unfiltered_base = bdrv_skip_filters(base);
78
79     if (bdrv_cow_child(unfiltered_bs)) {
80         const char *base_id = NULL, *base_fmt = NULL;
81         if (unfiltered_base) {
82             base_id = s->backing_file_str ?: unfiltered_base->filename;
83             if (unfiltered_base->drv) {
84                 base_fmt = unfiltered_base->drv->format_name;
85             }
86         }
87
88         bdrv_set_backing_hd_drained(unfiltered_bs, base, &local_err);
89
90         /*
91          * This call will do I/O, so the graph can change again from here on.
92          * We have already completed the graph change, so we are not in danger
93          * of operating on the wrong node any more if this happens.
94          */
95         ret = bdrv_change_backing_file(unfiltered_bs, base_id, base_fmt, false);
96         if (local_err) {
97             error_report_err(local_err);
98             ret = -EPERM;
99             goto out;
100         }
101     }
102
103 out:
104     bdrv_drained_end(unfiltered_bs);
105     return ret;
106 }
107
108 static void stream_clean(Job *job)
109 {
110     StreamBlockJob *s = container_of(job, StreamBlockJob, common.job);
111
112     if (s->cor_filter_bs) {
113         bdrv_cor_filter_drop(s->cor_filter_bs);
114         s->cor_filter_bs = NULL;
115     }
116
117     blk_unref(s->blk);
118     s->blk = NULL;
119
120     /* Reopen the image back in read-only mode if necessary */
121     if (s->bs_read_only) {
122         /* Give up write permissions before making it read-only */
123         bdrv_reopen_set_read_only(s->target_bs, true, NULL);
124     }
125
126     g_free(s->backing_file_str);
127 }
128
129 static int coroutine_fn stream_run(Job *job, Error **errp)
130 {
131     StreamBlockJob *s = container_of(job, StreamBlockJob, common.job);
132     BlockDriverState *unfiltered_bs = bdrv_skip_filters(s->target_bs);
133     int64_t len;
134     int64_t offset = 0;
135     uint64_t delay_ns = 0;
136     int error = 0;
137     int64_t n = 0; /* bytes */
138
139     if (unfiltered_bs == s->base_overlay) {
140         /* Nothing to stream */
141         return 0;
142     }
143
144     WITH_GRAPH_RDLOCK_GUARD() {
145         len = bdrv_co_getlength(s->target_bs);
146         if (len < 0) {
147             return len;
148         }
149     }
150     job_progress_set_remaining(&s->common.job, len);
151
152     for ( ; offset < len; offset += n) {
153         bool copy;
154         int ret;
155
156         /* Note that even when no rate limit is applied we need to yield
157          * with no pending I/O here so that bdrv_drain_all() returns.
158          */
159         job_sleep_ns(&s->common.job, delay_ns);
160         if (job_is_cancelled(&s->common.job)) {
161             break;
162         }
163
164         copy = false;
165
166         WITH_GRAPH_RDLOCK_GUARD() {
167             ret = bdrv_is_allocated(unfiltered_bs, offset, STREAM_CHUNK, &n);
168             if (ret == 1) {
169                 /* Allocated in the top, no need to copy.  */
170             } else if (ret >= 0) {
171                 /*
172                  * Copy if allocated in the intermediate images.  Limit to the
173                  * known-unallocated area [offset, offset+n*BDRV_SECTOR_SIZE).
174                  */
175                 ret = bdrv_is_allocated_above(bdrv_cow_bs(unfiltered_bs),
176                                             s->base_overlay, true,
177                                             offset, n, &n);
178                 /* Finish early if end of backing file has been reached */
179                 if (ret == 0 && n == 0) {
180                     n = len - offset;
181                 }
182
183                 copy = (ret > 0);
184             }
185         }
186         trace_stream_one_iteration(s, offset, n, ret);
187         if (copy) {
188             ret = stream_populate(s->blk, offset, n);
189         }
190         if (ret < 0) {
191             BlockErrorAction action =
192                 block_job_error_action(&s->common, s->on_error, true, -ret);
193             if (action == BLOCK_ERROR_ACTION_STOP) {
194                 n = 0;
195                 continue;
196             }
197             if (error == 0) {
198                 error = ret;
199             }
200             if (action == BLOCK_ERROR_ACTION_REPORT) {
201                 break;
202             }
203         }
204
205         /* Publish progress */
206         job_progress_update(&s->common.job, n);
207         if (copy) {
208             delay_ns = block_job_ratelimit_get_delay(&s->common, n);
209         } else {
210             delay_ns = 0;
211         }
212     }
213
214     /* Do not remove the backing file if an error was there but ignored. */
215     return error;
216 }
217
218 static const BlockJobDriver stream_job_driver = {
219     .job_driver = {
220         .instance_size = sizeof(StreamBlockJob),
221         .job_type      = JOB_TYPE_STREAM,
222         .free          = block_job_free,
223         .run           = stream_run,
224         .prepare       = stream_prepare,
225         .clean         = stream_clean,
226         .user_resume   = block_job_user_resume,
227     },
228 };
229
230 void stream_start(const char *job_id, BlockDriverState *bs,
231                   BlockDriverState *base, const char *backing_file_str,
232                   BlockDriverState *bottom,
233                   int creation_flags, int64_t speed,
234                   BlockdevOnError on_error,
235                   const char *filter_node_name,
236                   Error **errp)
237 {
238     StreamBlockJob *s = NULL;
239     BlockDriverState *iter;
240     bool bs_read_only;
241     int basic_flags = BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED;
242     BlockDriverState *base_overlay;
243     BlockDriverState *cor_filter_bs = NULL;
244     BlockDriverState *above_base;
245     QDict *opts;
246     int ret;
247
248     GLOBAL_STATE_CODE();
249
250     assert(!(base && bottom));
251     assert(!(backing_file_str && bottom));
252
253     if (bottom) {
254         /*
255          * New simple interface. The code is written in terms of old interface
256          * with @base parameter (still, it doesn't freeze link to base, so in
257          * this mean old code is correct for new interface). So, for now, just
258          * emulate base_overlay and above_base. Still, when old interface
259          * finally removed, we should refactor code to use only "bottom", but
260          * not "*base*" things.
261          */
262         assert(!bottom->drv->is_filter);
263         base_overlay = above_base = bottom;
264     } else {
265         base_overlay = bdrv_find_overlay(bs, base);
266         if (!base_overlay) {
267             error_setg(errp, "'%s' is not in the backing chain of '%s'",
268                        base->node_name, bs->node_name);
269             return;
270         }
271
272         /*
273          * Find the node directly above @base.  @base_overlay is a COW overlay,
274          * so it must have a bdrv_cow_child(), but it is the immediate overlay
275          * of @base, so between the two there can only be filters.
276          */
277         above_base = base_overlay;
278         if (bdrv_cow_bs(above_base) != base) {
279             above_base = bdrv_cow_bs(above_base);
280             while (bdrv_filter_bs(above_base) != base) {
281                 above_base = bdrv_filter_bs(above_base);
282             }
283         }
284     }
285
286     /* Make sure that the image is opened in read-write mode */
287     bs_read_only = bdrv_is_read_only(bs);
288     if (bs_read_only) {
289         int ret;
290         /* Hold the chain during reopen */
291         if (bdrv_freeze_backing_chain(bs, above_base, errp) < 0) {
292             return;
293         }
294
295         ret = bdrv_reopen_set_read_only(bs, false, errp);
296
297         /* failure, or cor-filter will hold the chain */
298         bdrv_unfreeze_backing_chain(bs, above_base);
299
300         if (ret < 0) {
301             return;
302         }
303     }
304
305     opts = qdict_new();
306
307     qdict_put_str(opts, "driver", "copy-on-read");
308     qdict_put_str(opts, "file", bdrv_get_node_name(bs));
309     /* Pass the base_overlay node name as 'bottom' to COR driver */
310     qdict_put_str(opts, "bottom", base_overlay->node_name);
311     if (filter_node_name) {
312         qdict_put_str(opts, "node-name", filter_node_name);
313     }
314
315     cor_filter_bs = bdrv_insert_node(bs, opts, BDRV_O_RDWR, errp);
316     if (!cor_filter_bs) {
317         goto fail;
318     }
319
320     if (!filter_node_name) {
321         cor_filter_bs->implicit = true;
322     }
323
324     s = block_job_create(job_id, &stream_job_driver, NULL, cor_filter_bs,
325                          0, BLK_PERM_ALL,
326                          speed, creation_flags, NULL, NULL, errp);
327     if (!s) {
328         goto fail;
329     }
330
331     s->blk = blk_new_with_bs(cor_filter_bs, BLK_PERM_CONSISTENT_READ,
332                              basic_flags | BLK_PERM_WRITE, errp);
333     if (!s->blk) {
334         goto fail;
335     }
336     /*
337      * Disable request queuing in the BlockBackend to avoid deadlocks on drain:
338      * The job reports that it's busy until it reaches a pause point.
339      */
340     blk_set_disable_request_queuing(s->blk, true);
341     blk_set_allow_aio_context_change(s->blk, true);
342
343     /*
344      * Prevent concurrent jobs trying to modify the graph structure here, we
345      * already have our own plans. Also don't allow resize as the image size is
346      * queried only at the job start and then cached.
347      */
348     if (block_job_add_bdrv(&s->common, "active node", bs, 0,
349                            basic_flags | BLK_PERM_WRITE, errp)) {
350         goto fail;
351     }
352
353     /* Block all intermediate nodes between bs and base, because they will
354      * disappear from the chain after this operation. The streaming job reads
355      * every block only once, assuming that it doesn't change, so forbid writes
356      * and resizes. Reassign the base node pointer because the backing BS of the
357      * bottom node might change after the call to bdrv_reopen_set_read_only()
358      * due to parallel block jobs running.
359      * above_base node might change after the call to
360      * bdrv_reopen_set_read_only() due to parallel block jobs running.
361      */
362     base = bdrv_filter_or_cow_bs(above_base);
363     for (iter = bdrv_filter_or_cow_bs(bs); iter != base;
364          iter = bdrv_filter_or_cow_bs(iter))
365     {
366         ret = block_job_add_bdrv(&s->common, "intermediate node", iter, 0,
367                                  basic_flags, errp);
368         if (ret < 0) {
369             goto fail;
370         }
371     }
372
373     s->base_overlay = base_overlay;
374     s->above_base = above_base;
375     s->backing_file_str = g_strdup(backing_file_str);
376     s->cor_filter_bs = cor_filter_bs;
377     s->target_bs = bs;
378     s->bs_read_only = bs_read_only;
379
380     s->on_error = on_error;
381     trace_stream_start(bs, base, s);
382     job_start(&s->common.job);
383     return;
384
385 fail:
386     if (s) {
387         job_early_fail(&s->common.job);
388     }
389     if (cor_filter_bs) {
390         bdrv_cor_filter_drop(cor_filter_bs);
391     }
392     if (bs_read_only) {
393         bdrv_reopen_set_read_only(bs, true, NULL);
394     }
395 }