]>
Commit | Line | Data |
---|---|---|
4f1043b4 SH |
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 "trace.h" | |
15 | #include "block_int.h" | |
16 | ||
17 | enum { | |
18 | /* | |
19 | * Size of data buffer for populating the image file. This should be large | |
20 | * enough to process multiple clusters in a single call, so that populating | |
21 | * contiguous regions of the image is efficient. | |
22 | */ | |
23 | STREAM_BUFFER_SIZE = 512 * 1024, /* in bytes */ | |
24 | }; | |
25 | ||
26 | typedef struct StreamBlockJob { | |
27 | BlockJob common; | |
28 | BlockDriverState *base; | |
29 | } StreamBlockJob; | |
30 | ||
31 | static int coroutine_fn stream_populate(BlockDriverState *bs, | |
32 | int64_t sector_num, int nb_sectors, | |
33 | void *buf) | |
34 | { | |
35 | struct iovec iov = { | |
36 | .iov_base = buf, | |
37 | .iov_len = nb_sectors * BDRV_SECTOR_SIZE, | |
38 | }; | |
39 | QEMUIOVector qiov; | |
40 | ||
41 | qemu_iovec_init_external(&qiov, &iov, 1); | |
42 | ||
43 | /* Copy-on-read the unallocated clusters */ | |
44 | return bdrv_co_copy_on_readv(bs, sector_num, nb_sectors, &qiov); | |
45 | } | |
46 | ||
47 | static void coroutine_fn stream_run(void *opaque) | |
48 | { | |
49 | StreamBlockJob *s = opaque; | |
50 | BlockDriverState *bs = s->common.bs; | |
51 | int64_t sector_num, end; | |
52 | int ret = 0; | |
53 | int n; | |
54 | void *buf; | |
55 | ||
56 | s->common.len = bdrv_getlength(bs); | |
57 | if (s->common.len < 0) { | |
58 | block_job_complete(&s->common, s->common.len); | |
59 | return; | |
60 | } | |
61 | ||
62 | end = s->common.len >> BDRV_SECTOR_BITS; | |
63 | buf = qemu_blockalign(bs, STREAM_BUFFER_SIZE); | |
64 | ||
65 | /* Turn on copy-on-read for the whole block device so that guest read | |
66 | * requests help us make progress. Only do this when copying the entire | |
67 | * backing chain since the copy-on-read operation does not take base into | |
68 | * account. | |
69 | */ | |
70 | if (!base) { | |
71 | bdrv_enable_copy_on_read(bs); | |
72 | } | |
73 | ||
74 | for (sector_num = 0; sector_num < end; sector_num += n) { | |
75 | if (block_job_is_cancelled(&s->common)) { | |
76 | break; | |
77 | } | |
78 | ||
79 | /* TODO rate-limit */ | |
80 | /* Note that even when no rate limit is applied we need to yield with | |
81 | * no pending I/O here so that qemu_aio_flush() is able to return. | |
82 | */ | |
83 | co_sleep_ns(rt_clock, 0); | |
84 | ||
85 | ret = bdrv_co_is_allocated(bs, sector_num, | |
86 | STREAM_BUFFER_SIZE / BDRV_SECTOR_SIZE, &n); | |
87 | trace_stream_one_iteration(s, sector_num, n, ret); | |
88 | if (ret == 0) { | |
89 | ret = stream_populate(bs, sector_num, n, buf); | |
90 | } | |
91 | if (ret < 0) { | |
92 | break; | |
93 | } | |
94 | ||
95 | /* Publish progress */ | |
96 | s->common.offset += n * BDRV_SECTOR_SIZE; | |
97 | } | |
98 | ||
99 | if (!base) { | |
100 | bdrv_disable_copy_on_read(bs); | |
101 | } | |
102 | ||
103 | if (sector_num == end && ret == 0) { | |
104 | ret = bdrv_change_backing_file(bs, NULL, NULL); | |
105 | } | |
106 | ||
107 | qemu_vfree(buf); | |
108 | block_job_complete(&s->common, ret); | |
109 | } | |
110 | ||
111 | static BlockJobType stream_job_type = { | |
112 | .instance_size = sizeof(StreamBlockJob), | |
113 | .job_type = "stream", | |
114 | }; | |
115 | ||
116 | int stream_start(BlockDriverState *bs, BlockDriverState *base, | |
117 | BlockDriverCompletionFunc *cb, void *opaque) | |
118 | { | |
119 | StreamBlockJob *s; | |
120 | Coroutine *co; | |
121 | ||
122 | s = block_job_create(&stream_job_type, bs, cb, opaque); | |
123 | if (!s) { | |
124 | return -EBUSY; /* bs must already be in use */ | |
125 | } | |
126 | ||
127 | s->base = base; | |
128 | ||
129 | co = qemu_coroutine_create(stream_run); | |
130 | trace_stream_start(bs, base, s, co, opaque); | |
131 | qemu_coroutine_enter(co, s); | |
132 | return 0; | |
133 | } |