root / block.c @ 13ef70f6
History | View | Annotate | Download (113.5 kB)
1 | fc01f7e7 | bellard | /*
|
---|---|---|---|
2 | fc01f7e7 | bellard | * QEMU System Emulator block driver
|
3 | 5fafdf24 | ths | *
|
4 | fc01f7e7 | bellard | * Copyright (c) 2003 Fabrice Bellard
|
5 | 5fafdf24 | ths | *
|
6 | fc01f7e7 | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | fc01f7e7 | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | fc01f7e7 | bellard | * in the Software without restriction, including without limitation the rights
|
9 | fc01f7e7 | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | fc01f7e7 | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | fc01f7e7 | bellard | * furnished to do so, subject to the following conditions:
|
12 | fc01f7e7 | bellard | *
|
13 | fc01f7e7 | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | fc01f7e7 | bellard | * all copies or substantial portions of the Software.
|
15 | fc01f7e7 | bellard | *
|
16 | fc01f7e7 | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | fc01f7e7 | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | fc01f7e7 | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | fc01f7e7 | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | fc01f7e7 | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | fc01f7e7 | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | fc01f7e7 | bellard | * THE SOFTWARE.
|
23 | fc01f7e7 | bellard | */
|
24 | 3990d09a | blueswir1 | #include "config-host.h" |
25 | faf07963 | pbrook | #include "qemu-common.h" |
26 | 6d519a5f | Stefan Hajnoczi | #include "trace.h" |
27 | 376253ec | aliguori | #include "monitor.h" |
28 | ea2384d3 | bellard | #include "block_int.h" |
29 | 5efa9d5a | Anthony Liguori | #include "module.h" |
30 | f795e743 | Luiz Capitulino | #include "qjson.h" |
31 | 68485420 | Kevin Wolf | #include "qemu-coroutine.h" |
32 | b2023818 | Luiz Capitulino | #include "qmp-commands.h" |
33 | 0563e191 | Zhi Yong Wu | #include "qemu-timer.h" |
34 | fc01f7e7 | bellard | |
35 | 71e72a19 | Juan Quintela | #ifdef CONFIG_BSD
|
36 | 7674e7bf | bellard | #include <sys/types.h> |
37 | 7674e7bf | bellard | #include <sys/stat.h> |
38 | 7674e7bf | bellard | #include <sys/ioctl.h> |
39 | 72cf2d4f | Blue Swirl | #include <sys/queue.h> |
40 | c5e97233 | blueswir1 | #ifndef __DragonFly__
|
41 | 7674e7bf | bellard | #include <sys/disk.h> |
42 | 7674e7bf | bellard | #endif
|
43 | c5e97233 | blueswir1 | #endif
|
44 | 7674e7bf | bellard | |
45 | 49dc768d | aliguori | #ifdef _WIN32
|
46 | 49dc768d | aliguori | #include <windows.h> |
47 | 49dc768d | aliguori | #endif
|
48 | 49dc768d | aliguori | |
49 | 1c9805a3 | Stefan Hajnoczi | #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */ |
50 | 1c9805a3 | Stefan Hajnoczi | |
51 | 470c0504 | Stefan Hajnoczi | typedef enum { |
52 | 470c0504 | Stefan Hajnoczi | BDRV_REQ_COPY_ON_READ = 0x1,
|
53 | f08f2dda | Stefan Hajnoczi | BDRV_REQ_ZERO_WRITE = 0x2,
|
54 | 470c0504 | Stefan Hajnoczi | } BdrvRequestFlags; |
55 | 470c0504 | Stefan Hajnoczi | |
56 | 7d4b4ba5 | Markus Armbruster | static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load); |
57 | f141eafe | aliguori | static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
|
58 | f141eafe | aliguori | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
59 | c87c0672 | aliguori | BlockDriverCompletionFunc *cb, void *opaque);
|
60 | f141eafe | aliguori | static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
|
61 | f141eafe | aliguori | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
62 | ce1a14dc | pbrook | BlockDriverCompletionFunc *cb, void *opaque);
|
63 | f9f05dc5 | Kevin Wolf | static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs, |
64 | f9f05dc5 | Kevin Wolf | int64_t sector_num, int nb_sectors,
|
65 | f9f05dc5 | Kevin Wolf | QEMUIOVector *iov); |
66 | f9f05dc5 | Kevin Wolf | static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs, |
67 | f9f05dc5 | Kevin Wolf | int64_t sector_num, int nb_sectors,
|
68 | f9f05dc5 | Kevin Wolf | QEMUIOVector *iov); |
69 | c5fbe571 | Stefan Hajnoczi | static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs, |
70 | 470c0504 | Stefan Hajnoczi | int64_t sector_num, int nb_sectors, QEMUIOVector *qiov,
|
71 | 470c0504 | Stefan Hajnoczi | BdrvRequestFlags flags); |
72 | 1c9805a3 | Stefan Hajnoczi | static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs, |
73 | f08f2dda | Stefan Hajnoczi | int64_t sector_num, int nb_sectors, QEMUIOVector *qiov,
|
74 | f08f2dda | Stefan Hajnoczi | BdrvRequestFlags flags); |
75 | b2a61371 | Stefan Hajnoczi | static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs,
|
76 | b2a61371 | Stefan Hajnoczi | int64_t sector_num, |
77 | b2a61371 | Stefan Hajnoczi | QEMUIOVector *qiov, |
78 | b2a61371 | Stefan Hajnoczi | int nb_sectors,
|
79 | b2a61371 | Stefan Hajnoczi | BlockDriverCompletionFunc *cb, |
80 | b2a61371 | Stefan Hajnoczi | void *opaque,
|
81 | 8c5873d6 | Stefan Hajnoczi | bool is_write);
|
82 | b2a61371 | Stefan Hajnoczi | static void coroutine_fn bdrv_co_do_rw(void *opaque); |
83 | 621f0589 | Kevin Wolf | static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs, |
84 | 621f0589 | Kevin Wolf | int64_t sector_num, int nb_sectors);
|
85 | ec530c81 | bellard | |
86 | 98f90dba | Zhi Yong Wu | static bool bdrv_exceed_bps_limits(BlockDriverState *bs, int nb_sectors, |
87 | 98f90dba | Zhi Yong Wu | bool is_write, double elapsed_time, uint64_t *wait); |
88 | 98f90dba | Zhi Yong Wu | static bool bdrv_exceed_iops_limits(BlockDriverState *bs, bool is_write, |
89 | 98f90dba | Zhi Yong Wu | double elapsed_time, uint64_t *wait);
|
90 | 98f90dba | Zhi Yong Wu | static bool bdrv_exceed_io_limits(BlockDriverState *bs, int nb_sectors, |
91 | 98f90dba | Zhi Yong Wu | bool is_write, int64_t *wait);
|
92 | 98f90dba | Zhi Yong Wu | |
93 | 1b7bdbc1 | Stefan Hajnoczi | static QTAILQ_HEAD(, BlockDriverState) bdrv_states =
|
94 | 1b7bdbc1 | Stefan Hajnoczi | QTAILQ_HEAD_INITIALIZER(bdrv_states); |
95 | 7ee930d0 | blueswir1 | |
96 | 8a22f02a | Stefan Hajnoczi | static QLIST_HEAD(, BlockDriver) bdrv_drivers =
|
97 | 8a22f02a | Stefan Hajnoczi | QLIST_HEAD_INITIALIZER(bdrv_drivers); |
98 | ea2384d3 | bellard | |
99 | f9092b10 | Markus Armbruster | /* The device to use for VM snapshots */
|
100 | f9092b10 | Markus Armbruster | static BlockDriverState *bs_snapshots;
|
101 | f9092b10 | Markus Armbruster | |
102 | eb852011 | Markus Armbruster | /* If non-zero, use only whitelisted block drivers */
|
103 | eb852011 | Markus Armbruster | static int use_bdrv_whitelist; |
104 | eb852011 | Markus Armbruster | |
105 | 9e0b22f4 | Stefan Hajnoczi | #ifdef _WIN32
|
106 | 9e0b22f4 | Stefan Hajnoczi | static int is_windows_drive_prefix(const char *filename) |
107 | 9e0b22f4 | Stefan Hajnoczi | { |
108 | 9e0b22f4 | Stefan Hajnoczi | return (((filename[0] >= 'a' && filename[0] <= 'z') || |
109 | 9e0b22f4 | Stefan Hajnoczi | (filename[0] >= 'A' && filename[0] <= 'Z')) && |
110 | 9e0b22f4 | Stefan Hajnoczi | filename[1] == ':'); |
111 | 9e0b22f4 | Stefan Hajnoczi | } |
112 | 9e0b22f4 | Stefan Hajnoczi | |
113 | 9e0b22f4 | Stefan Hajnoczi | int is_windows_drive(const char *filename) |
114 | 9e0b22f4 | Stefan Hajnoczi | { |
115 | 9e0b22f4 | Stefan Hajnoczi | if (is_windows_drive_prefix(filename) &&
|
116 | 9e0b22f4 | Stefan Hajnoczi | filename[2] == '\0') |
117 | 9e0b22f4 | Stefan Hajnoczi | return 1; |
118 | 9e0b22f4 | Stefan Hajnoczi | if (strstart(filename, "\\\\.\\", NULL) || |
119 | 9e0b22f4 | Stefan Hajnoczi | strstart(filename, "//./", NULL)) |
120 | 9e0b22f4 | Stefan Hajnoczi | return 1; |
121 | 9e0b22f4 | Stefan Hajnoczi | return 0; |
122 | 9e0b22f4 | Stefan Hajnoczi | } |
123 | 9e0b22f4 | Stefan Hajnoczi | #endif
|
124 | 9e0b22f4 | Stefan Hajnoczi | |
125 | 0563e191 | Zhi Yong Wu | /* throttling disk I/O limits */
|
126 | 98f90dba | Zhi Yong Wu | void bdrv_io_limits_disable(BlockDriverState *bs)
|
127 | 98f90dba | Zhi Yong Wu | { |
128 | 98f90dba | Zhi Yong Wu | bs->io_limits_enabled = false;
|
129 | 98f90dba | Zhi Yong Wu | |
130 | 98f90dba | Zhi Yong Wu | while (qemu_co_queue_next(&bs->throttled_reqs));
|
131 | 98f90dba | Zhi Yong Wu | |
132 | 98f90dba | Zhi Yong Wu | if (bs->block_timer) {
|
133 | 98f90dba | Zhi Yong Wu | qemu_del_timer(bs->block_timer); |
134 | 98f90dba | Zhi Yong Wu | qemu_free_timer(bs->block_timer); |
135 | 98f90dba | Zhi Yong Wu | bs->block_timer = NULL;
|
136 | 98f90dba | Zhi Yong Wu | } |
137 | 98f90dba | Zhi Yong Wu | |
138 | 98f90dba | Zhi Yong Wu | bs->slice_start = 0;
|
139 | 98f90dba | Zhi Yong Wu | bs->slice_end = 0;
|
140 | 98f90dba | Zhi Yong Wu | bs->slice_time = 0;
|
141 | 98f90dba | Zhi Yong Wu | memset(&bs->io_base, 0, sizeof(bs->io_base)); |
142 | 98f90dba | Zhi Yong Wu | } |
143 | 98f90dba | Zhi Yong Wu | |
144 | 0563e191 | Zhi Yong Wu | static void bdrv_block_timer(void *opaque) |
145 | 0563e191 | Zhi Yong Wu | { |
146 | 0563e191 | Zhi Yong Wu | BlockDriverState *bs = opaque; |
147 | 0563e191 | Zhi Yong Wu | |
148 | 0563e191 | Zhi Yong Wu | qemu_co_queue_next(&bs->throttled_reqs); |
149 | 0563e191 | Zhi Yong Wu | } |
150 | 0563e191 | Zhi Yong Wu | |
151 | 0563e191 | Zhi Yong Wu | void bdrv_io_limits_enable(BlockDriverState *bs)
|
152 | 0563e191 | Zhi Yong Wu | { |
153 | 0563e191 | Zhi Yong Wu | qemu_co_queue_init(&bs->throttled_reqs); |
154 | 0563e191 | Zhi Yong Wu | bs->block_timer = qemu_new_timer_ns(vm_clock, bdrv_block_timer, bs); |
155 | 0563e191 | Zhi Yong Wu | bs->slice_time = 5 * BLOCK_IO_SLICE_TIME;
|
156 | 0563e191 | Zhi Yong Wu | bs->slice_start = qemu_get_clock_ns(vm_clock); |
157 | 0563e191 | Zhi Yong Wu | bs->slice_end = bs->slice_start + bs->slice_time; |
158 | 0563e191 | Zhi Yong Wu | memset(&bs->io_base, 0, sizeof(bs->io_base)); |
159 | 0563e191 | Zhi Yong Wu | bs->io_limits_enabled = true;
|
160 | 0563e191 | Zhi Yong Wu | } |
161 | 0563e191 | Zhi Yong Wu | |
162 | 0563e191 | Zhi Yong Wu | bool bdrv_io_limits_enabled(BlockDriverState *bs)
|
163 | 0563e191 | Zhi Yong Wu | { |
164 | 0563e191 | Zhi Yong Wu | BlockIOLimit *io_limits = &bs->io_limits; |
165 | 0563e191 | Zhi Yong Wu | return io_limits->bps[BLOCK_IO_LIMIT_READ]
|
166 | 0563e191 | Zhi Yong Wu | || io_limits->bps[BLOCK_IO_LIMIT_WRITE] |
167 | 0563e191 | Zhi Yong Wu | || io_limits->bps[BLOCK_IO_LIMIT_TOTAL] |
168 | 0563e191 | Zhi Yong Wu | || io_limits->iops[BLOCK_IO_LIMIT_READ] |
169 | 0563e191 | Zhi Yong Wu | || io_limits->iops[BLOCK_IO_LIMIT_WRITE] |
170 | 0563e191 | Zhi Yong Wu | || io_limits->iops[BLOCK_IO_LIMIT_TOTAL]; |
171 | 0563e191 | Zhi Yong Wu | } |
172 | 0563e191 | Zhi Yong Wu | |
173 | 98f90dba | Zhi Yong Wu | static void bdrv_io_limits_intercept(BlockDriverState *bs, |
174 | 98f90dba | Zhi Yong Wu | bool is_write, int nb_sectors) |
175 | 98f90dba | Zhi Yong Wu | { |
176 | 98f90dba | Zhi Yong Wu | int64_t wait_time = -1;
|
177 | 98f90dba | Zhi Yong Wu | |
178 | 98f90dba | Zhi Yong Wu | if (!qemu_co_queue_empty(&bs->throttled_reqs)) {
|
179 | 98f90dba | Zhi Yong Wu | qemu_co_queue_wait(&bs->throttled_reqs); |
180 | 98f90dba | Zhi Yong Wu | } |
181 | 98f90dba | Zhi Yong Wu | |
182 | 98f90dba | Zhi Yong Wu | /* In fact, we hope to keep each request's timing, in FIFO mode. The next
|
183 | 98f90dba | Zhi Yong Wu | * throttled requests will not be dequeued until the current request is
|
184 | 98f90dba | Zhi Yong Wu | * allowed to be serviced. So if the current request still exceeds the
|
185 | 98f90dba | Zhi Yong Wu | * limits, it will be inserted to the head. All requests followed it will
|
186 | 98f90dba | Zhi Yong Wu | * be still in throttled_reqs queue.
|
187 | 98f90dba | Zhi Yong Wu | */
|
188 | 98f90dba | Zhi Yong Wu | |
189 | 98f90dba | Zhi Yong Wu | while (bdrv_exceed_io_limits(bs, nb_sectors, is_write, &wait_time)) {
|
190 | 98f90dba | Zhi Yong Wu | qemu_mod_timer(bs->block_timer, |
191 | 98f90dba | Zhi Yong Wu | wait_time + qemu_get_clock_ns(vm_clock)); |
192 | 98f90dba | Zhi Yong Wu | qemu_co_queue_wait_insert_head(&bs->throttled_reqs); |
193 | 98f90dba | Zhi Yong Wu | } |
194 | 98f90dba | Zhi Yong Wu | |
195 | 98f90dba | Zhi Yong Wu | qemu_co_queue_next(&bs->throttled_reqs); |
196 | 98f90dba | Zhi Yong Wu | } |
197 | 98f90dba | Zhi Yong Wu | |
198 | 9e0b22f4 | Stefan Hajnoczi | /* check if the path starts with "<protocol>:" */
|
199 | 9e0b22f4 | Stefan Hajnoczi | static int path_has_protocol(const char *path) |
200 | 9e0b22f4 | Stefan Hajnoczi | { |
201 | 947995c0 | Paolo Bonzini | const char *p; |
202 | 947995c0 | Paolo Bonzini | |
203 | 9e0b22f4 | Stefan Hajnoczi | #ifdef _WIN32
|
204 | 9e0b22f4 | Stefan Hajnoczi | if (is_windows_drive(path) ||
|
205 | 9e0b22f4 | Stefan Hajnoczi | is_windows_drive_prefix(path)) { |
206 | 9e0b22f4 | Stefan Hajnoczi | return 0; |
207 | 9e0b22f4 | Stefan Hajnoczi | } |
208 | 947995c0 | Paolo Bonzini | p = path + strcspn(path, ":/\\");
|
209 | 947995c0 | Paolo Bonzini | #else
|
210 | 947995c0 | Paolo Bonzini | p = path + strcspn(path, ":/");
|
211 | 9e0b22f4 | Stefan Hajnoczi | #endif
|
212 | 9e0b22f4 | Stefan Hajnoczi | |
213 | 947995c0 | Paolo Bonzini | return *p == ':'; |
214 | 9e0b22f4 | Stefan Hajnoczi | } |
215 | 9e0b22f4 | Stefan Hajnoczi | |
216 | 83f64091 | bellard | int path_is_absolute(const char *path) |
217 | 3b0d4f61 | bellard | { |
218 | 21664424 | bellard | #ifdef _WIN32
|
219 | 21664424 | bellard | /* specific case for names like: "\\.\d:" */
|
220 | f53f4da9 | Paolo Bonzini | if (is_windows_drive(path) || is_windows_drive_prefix(path)) {
|
221 | 21664424 | bellard | return 1; |
222 | f53f4da9 | Paolo Bonzini | } |
223 | f53f4da9 | Paolo Bonzini | return (*path == '/' || *path == '\\'); |
224 | 3b9f94e1 | bellard | #else
|
225 | f53f4da9 | Paolo Bonzini | return (*path == '/'); |
226 | 3b9f94e1 | bellard | #endif
|
227 | 3b0d4f61 | bellard | } |
228 | 3b0d4f61 | bellard | |
229 | 83f64091 | bellard | /* if filename is absolute, just copy it to dest. Otherwise, build a
|
230 | 83f64091 | bellard | path to it by considering it is relative to base_path. URL are
|
231 | 83f64091 | bellard | supported. */
|
232 | 83f64091 | bellard | void path_combine(char *dest, int dest_size, |
233 | 83f64091 | bellard | const char *base_path, |
234 | 83f64091 | bellard | const char *filename) |
235 | 3b0d4f61 | bellard | { |
236 | 83f64091 | bellard | const char *p, *p1; |
237 | 83f64091 | bellard | int len;
|
238 | 83f64091 | bellard | |
239 | 83f64091 | bellard | if (dest_size <= 0) |
240 | 83f64091 | bellard | return;
|
241 | 83f64091 | bellard | if (path_is_absolute(filename)) {
|
242 | 83f64091 | bellard | pstrcpy(dest, dest_size, filename); |
243 | 83f64091 | bellard | } else {
|
244 | 83f64091 | bellard | p = strchr(base_path, ':');
|
245 | 83f64091 | bellard | if (p)
|
246 | 83f64091 | bellard | p++; |
247 | 83f64091 | bellard | else
|
248 | 83f64091 | bellard | p = base_path; |
249 | 3b9f94e1 | bellard | p1 = strrchr(base_path, '/');
|
250 | 3b9f94e1 | bellard | #ifdef _WIN32
|
251 | 3b9f94e1 | bellard | { |
252 | 3b9f94e1 | bellard | const char *p2; |
253 | 3b9f94e1 | bellard | p2 = strrchr(base_path, '\\');
|
254 | 3b9f94e1 | bellard | if (!p1 || p2 > p1)
|
255 | 3b9f94e1 | bellard | p1 = p2; |
256 | 3b9f94e1 | bellard | } |
257 | 3b9f94e1 | bellard | #endif
|
258 | 83f64091 | bellard | if (p1)
|
259 | 83f64091 | bellard | p1++; |
260 | 83f64091 | bellard | else
|
261 | 83f64091 | bellard | p1 = base_path; |
262 | 83f64091 | bellard | if (p1 > p)
|
263 | 83f64091 | bellard | p = p1; |
264 | 83f64091 | bellard | len = p - base_path; |
265 | 83f64091 | bellard | if (len > dest_size - 1) |
266 | 83f64091 | bellard | len = dest_size - 1;
|
267 | 83f64091 | bellard | memcpy(dest, base_path, len); |
268 | 83f64091 | bellard | dest[len] = '\0';
|
269 | 83f64091 | bellard | pstrcat(dest, dest_size, filename); |
270 | 3b0d4f61 | bellard | } |
271 | 3b0d4f61 | bellard | } |
272 | 3b0d4f61 | bellard | |
273 | dc5a1371 | Paolo Bonzini | void bdrv_get_full_backing_filename(BlockDriverState *bs, char *dest, size_t sz) |
274 | dc5a1371 | Paolo Bonzini | { |
275 | dc5a1371 | Paolo Bonzini | if (bs->backing_file[0] == '\0' || path_has_protocol(bs->backing_file)) { |
276 | dc5a1371 | Paolo Bonzini | pstrcpy(dest, sz, bs->backing_file); |
277 | dc5a1371 | Paolo Bonzini | } else {
|
278 | dc5a1371 | Paolo Bonzini | path_combine(dest, sz, bs->filename, bs->backing_file); |
279 | dc5a1371 | Paolo Bonzini | } |
280 | dc5a1371 | Paolo Bonzini | } |
281 | dc5a1371 | Paolo Bonzini | |
282 | 5efa9d5a | Anthony Liguori | void bdrv_register(BlockDriver *bdrv)
|
283 | ea2384d3 | bellard | { |
284 | 8c5873d6 | Stefan Hajnoczi | /* Block drivers without coroutine functions need emulation */
|
285 | 8c5873d6 | Stefan Hajnoczi | if (!bdrv->bdrv_co_readv) {
|
286 | f9f05dc5 | Kevin Wolf | bdrv->bdrv_co_readv = bdrv_co_readv_em; |
287 | f9f05dc5 | Kevin Wolf | bdrv->bdrv_co_writev = bdrv_co_writev_em; |
288 | f9f05dc5 | Kevin Wolf | |
289 | f8c35c1d | Stefan Hajnoczi | /* bdrv_co_readv_em()/brdv_co_writev_em() work in terms of aio, so if
|
290 | f8c35c1d | Stefan Hajnoczi | * the block driver lacks aio we need to emulate that too.
|
291 | f8c35c1d | Stefan Hajnoczi | */
|
292 | f9f05dc5 | Kevin Wolf | if (!bdrv->bdrv_aio_readv) {
|
293 | f9f05dc5 | Kevin Wolf | /* add AIO emulation layer */
|
294 | f9f05dc5 | Kevin Wolf | bdrv->bdrv_aio_readv = bdrv_aio_readv_em; |
295 | f9f05dc5 | Kevin Wolf | bdrv->bdrv_aio_writev = bdrv_aio_writev_em; |
296 | f9f05dc5 | Kevin Wolf | } |
297 | 83f64091 | bellard | } |
298 | b2e12bc6 | Christoph Hellwig | |
299 | 8a22f02a | Stefan Hajnoczi | QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list); |
300 | ea2384d3 | bellard | } |
301 | b338082b | bellard | |
302 | b338082b | bellard | /* create a new block device (by default it is empty) */
|
303 | b338082b | bellard | BlockDriverState *bdrv_new(const char *device_name) |
304 | b338082b | bellard | { |
305 | 1b7bdbc1 | Stefan Hajnoczi | BlockDriverState *bs; |
306 | b338082b | bellard | |
307 | 7267c094 | Anthony Liguori | bs = g_malloc0(sizeof(BlockDriverState));
|
308 | b338082b | bellard | pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
|
309 | ea2384d3 | bellard | if (device_name[0] != '\0') { |
310 | 1b7bdbc1 | Stefan Hajnoczi | QTAILQ_INSERT_TAIL(&bdrv_states, bs, list); |
311 | ea2384d3 | bellard | } |
312 | 28a7282a | Luiz Capitulino | bdrv_iostatus_disable(bs); |
313 | b338082b | bellard | return bs;
|
314 | b338082b | bellard | } |
315 | b338082b | bellard | |
316 | ea2384d3 | bellard | BlockDriver *bdrv_find_format(const char *format_name) |
317 | ea2384d3 | bellard | { |
318 | ea2384d3 | bellard | BlockDriver *drv1; |
319 | 8a22f02a | Stefan Hajnoczi | QLIST_FOREACH(drv1, &bdrv_drivers, list) { |
320 | 8a22f02a | Stefan Hajnoczi | if (!strcmp(drv1->format_name, format_name)) {
|
321 | ea2384d3 | bellard | return drv1;
|
322 | 8a22f02a | Stefan Hajnoczi | } |
323 | ea2384d3 | bellard | } |
324 | ea2384d3 | bellard | return NULL; |
325 | ea2384d3 | bellard | } |
326 | ea2384d3 | bellard | |
327 | eb852011 | Markus Armbruster | static int bdrv_is_whitelisted(BlockDriver *drv) |
328 | eb852011 | Markus Armbruster | { |
329 | eb852011 | Markus Armbruster | static const char *whitelist[] = { |
330 | eb852011 | Markus Armbruster | CONFIG_BDRV_WHITELIST |
331 | eb852011 | Markus Armbruster | }; |
332 | eb852011 | Markus Armbruster | const char **p; |
333 | eb852011 | Markus Armbruster | |
334 | eb852011 | Markus Armbruster | if (!whitelist[0]) |
335 | eb852011 | Markus Armbruster | return 1; /* no whitelist, anything goes */ |
336 | eb852011 | Markus Armbruster | |
337 | eb852011 | Markus Armbruster | for (p = whitelist; *p; p++) {
|
338 | eb852011 | Markus Armbruster | if (!strcmp(drv->format_name, *p)) {
|
339 | eb852011 | Markus Armbruster | return 1; |
340 | eb852011 | Markus Armbruster | } |
341 | eb852011 | Markus Armbruster | } |
342 | eb852011 | Markus Armbruster | return 0; |
343 | eb852011 | Markus Armbruster | } |
344 | eb852011 | Markus Armbruster | |
345 | eb852011 | Markus Armbruster | BlockDriver *bdrv_find_whitelisted_format(const char *format_name) |
346 | eb852011 | Markus Armbruster | { |
347 | eb852011 | Markus Armbruster | BlockDriver *drv = bdrv_find_format(format_name); |
348 | eb852011 | Markus Armbruster | return drv && bdrv_is_whitelisted(drv) ? drv : NULL; |
349 | eb852011 | Markus Armbruster | } |
350 | eb852011 | Markus Armbruster | |
351 | 5b7e1542 | Zhi Yong Wu | typedef struct CreateCo { |
352 | 5b7e1542 | Zhi Yong Wu | BlockDriver *drv; |
353 | 5b7e1542 | Zhi Yong Wu | char *filename;
|
354 | 5b7e1542 | Zhi Yong Wu | QEMUOptionParameter *options; |
355 | 5b7e1542 | Zhi Yong Wu | int ret;
|
356 | 5b7e1542 | Zhi Yong Wu | } CreateCo; |
357 | 5b7e1542 | Zhi Yong Wu | |
358 | 5b7e1542 | Zhi Yong Wu | static void coroutine_fn bdrv_create_co_entry(void *opaque) |
359 | 5b7e1542 | Zhi Yong Wu | { |
360 | 5b7e1542 | Zhi Yong Wu | CreateCo *cco = opaque; |
361 | 5b7e1542 | Zhi Yong Wu | assert(cco->drv); |
362 | 5b7e1542 | Zhi Yong Wu | |
363 | 5b7e1542 | Zhi Yong Wu | cco->ret = cco->drv->bdrv_create(cco->filename, cco->options); |
364 | 5b7e1542 | Zhi Yong Wu | } |
365 | 5b7e1542 | Zhi Yong Wu | |
366 | 0e7e1989 | Kevin Wolf | int bdrv_create(BlockDriver *drv, const char* filename, |
367 | 0e7e1989 | Kevin Wolf | QEMUOptionParameter *options) |
368 | ea2384d3 | bellard | { |
369 | 5b7e1542 | Zhi Yong Wu | int ret;
|
370 | 5b7e1542 | Zhi Yong Wu | |
371 | 5b7e1542 | Zhi Yong Wu | Coroutine *co; |
372 | 5b7e1542 | Zhi Yong Wu | CreateCo cco = { |
373 | 5b7e1542 | Zhi Yong Wu | .drv = drv, |
374 | 5b7e1542 | Zhi Yong Wu | .filename = g_strdup(filename), |
375 | 5b7e1542 | Zhi Yong Wu | .options = options, |
376 | 5b7e1542 | Zhi Yong Wu | .ret = NOT_DONE, |
377 | 5b7e1542 | Zhi Yong Wu | }; |
378 | 5b7e1542 | Zhi Yong Wu | |
379 | 5b7e1542 | Zhi Yong Wu | if (!drv->bdrv_create) {
|
380 | ea2384d3 | bellard | return -ENOTSUP;
|
381 | 5b7e1542 | Zhi Yong Wu | } |
382 | 5b7e1542 | Zhi Yong Wu | |
383 | 5b7e1542 | Zhi Yong Wu | if (qemu_in_coroutine()) {
|
384 | 5b7e1542 | Zhi Yong Wu | /* Fast-path if already in coroutine context */
|
385 | 5b7e1542 | Zhi Yong Wu | bdrv_create_co_entry(&cco); |
386 | 5b7e1542 | Zhi Yong Wu | } else {
|
387 | 5b7e1542 | Zhi Yong Wu | co = qemu_coroutine_create(bdrv_create_co_entry); |
388 | 5b7e1542 | Zhi Yong Wu | qemu_coroutine_enter(co, &cco); |
389 | 5b7e1542 | Zhi Yong Wu | while (cco.ret == NOT_DONE) {
|
390 | 5b7e1542 | Zhi Yong Wu | qemu_aio_wait(); |
391 | 5b7e1542 | Zhi Yong Wu | } |
392 | 5b7e1542 | Zhi Yong Wu | } |
393 | 5b7e1542 | Zhi Yong Wu | |
394 | 5b7e1542 | Zhi Yong Wu | ret = cco.ret; |
395 | 5b7e1542 | Zhi Yong Wu | g_free(cco.filename); |
396 | 0e7e1989 | Kevin Wolf | |
397 | 5b7e1542 | Zhi Yong Wu | return ret;
|
398 | ea2384d3 | bellard | } |
399 | ea2384d3 | bellard | |
400 | 84a12e66 | Christoph Hellwig | int bdrv_create_file(const char* filename, QEMUOptionParameter *options) |
401 | 84a12e66 | Christoph Hellwig | { |
402 | 84a12e66 | Christoph Hellwig | BlockDriver *drv; |
403 | 84a12e66 | Christoph Hellwig | |
404 | b50cbabc | MORITA Kazutaka | drv = bdrv_find_protocol(filename); |
405 | 84a12e66 | Christoph Hellwig | if (drv == NULL) { |
406 | 16905d71 | Stefan Hajnoczi | return -ENOENT;
|
407 | 84a12e66 | Christoph Hellwig | } |
408 | 84a12e66 | Christoph Hellwig | |
409 | 84a12e66 | Christoph Hellwig | return bdrv_create(drv, filename, options);
|
410 | 84a12e66 | Christoph Hellwig | } |
411 | 84a12e66 | Christoph Hellwig | |
412 | eba25057 | Jim Meyering | /*
|
413 | eba25057 | Jim Meyering | * Create a uniquely-named empty temporary file.
|
414 | eba25057 | Jim Meyering | * Return 0 upon success, otherwise a negative errno value.
|
415 | eba25057 | Jim Meyering | */
|
416 | eba25057 | Jim Meyering | int get_tmp_filename(char *filename, int size) |
417 | d5249393 | bellard | { |
418 | eba25057 | Jim Meyering | #ifdef _WIN32
|
419 | 3b9f94e1 | bellard | char temp_dir[MAX_PATH];
|
420 | eba25057 | Jim Meyering | /* GetTempFileName requires that its output buffer (4th param)
|
421 | eba25057 | Jim Meyering | have length MAX_PATH or greater. */
|
422 | eba25057 | Jim Meyering | assert(size >= MAX_PATH); |
423 | eba25057 | Jim Meyering | return (GetTempPath(MAX_PATH, temp_dir)
|
424 | eba25057 | Jim Meyering | && GetTempFileName(temp_dir, "qem", 0, filename) |
425 | eba25057 | Jim Meyering | ? 0 : -GetLastError());
|
426 | d5249393 | bellard | #else
|
427 | 67b915a5 | bellard | int fd;
|
428 | 7ccfb2eb | blueswir1 | const char *tmpdir; |
429 | 0badc1ee | aurel32 | tmpdir = getenv("TMPDIR");
|
430 | 0badc1ee | aurel32 | if (!tmpdir)
|
431 | 0badc1ee | aurel32 | tmpdir = "/tmp";
|
432 | eba25057 | Jim Meyering | if (snprintf(filename, size, "%s/vl.XXXXXX", tmpdir) >= size) { |
433 | eba25057 | Jim Meyering | return -EOVERFLOW;
|
434 | eba25057 | Jim Meyering | } |
435 | ea2384d3 | bellard | fd = mkstemp(filename); |
436 | eba25057 | Jim Meyering | if (fd < 0 || close(fd)) { |
437 | eba25057 | Jim Meyering | return -errno;
|
438 | eba25057 | Jim Meyering | } |
439 | eba25057 | Jim Meyering | return 0; |
440 | d5249393 | bellard | #endif
|
441 | eba25057 | Jim Meyering | } |
442 | fc01f7e7 | bellard | |
443 | 84a12e66 | Christoph Hellwig | /*
|
444 | 84a12e66 | Christoph Hellwig | * Detect host devices. By convention, /dev/cdrom[N] is always
|
445 | 84a12e66 | Christoph Hellwig | * recognized as a host CDROM.
|
446 | 84a12e66 | Christoph Hellwig | */
|
447 | 84a12e66 | Christoph Hellwig | static BlockDriver *find_hdev_driver(const char *filename) |
448 | 84a12e66 | Christoph Hellwig | { |
449 | 84a12e66 | Christoph Hellwig | int score_max = 0, score; |
450 | 84a12e66 | Christoph Hellwig | BlockDriver *drv = NULL, *d;
|
451 | 84a12e66 | Christoph Hellwig | |
452 | 84a12e66 | Christoph Hellwig | QLIST_FOREACH(d, &bdrv_drivers, list) { |
453 | 84a12e66 | Christoph Hellwig | if (d->bdrv_probe_device) {
|
454 | 84a12e66 | Christoph Hellwig | score = d->bdrv_probe_device(filename); |
455 | 84a12e66 | Christoph Hellwig | if (score > score_max) {
|
456 | 84a12e66 | Christoph Hellwig | score_max = score; |
457 | 84a12e66 | Christoph Hellwig | drv = d; |
458 | 84a12e66 | Christoph Hellwig | } |
459 | 84a12e66 | Christoph Hellwig | } |
460 | 84a12e66 | Christoph Hellwig | } |
461 | 84a12e66 | Christoph Hellwig | |
462 | 84a12e66 | Christoph Hellwig | return drv;
|
463 | 84a12e66 | Christoph Hellwig | } |
464 | 84a12e66 | Christoph Hellwig | |
465 | b50cbabc | MORITA Kazutaka | BlockDriver *bdrv_find_protocol(const char *filename) |
466 | 83f64091 | bellard | { |
467 | 83f64091 | bellard | BlockDriver *drv1; |
468 | 83f64091 | bellard | char protocol[128]; |
469 | 1cec71e3 | Anthony Liguori | int len;
|
470 | 83f64091 | bellard | const char *p; |
471 | 19cb3738 | bellard | |
472 | 66f82cee | Kevin Wolf | /* TODO Drivers without bdrv_file_open must be specified explicitly */
|
473 | 66f82cee | Kevin Wolf | |
474 | 39508e7a | Christoph Hellwig | /*
|
475 | 39508e7a | Christoph Hellwig | * XXX(hch): we really should not let host device detection
|
476 | 39508e7a | Christoph Hellwig | * override an explicit protocol specification, but moving this
|
477 | 39508e7a | Christoph Hellwig | * later breaks access to device names with colons in them.
|
478 | 39508e7a | Christoph Hellwig | * Thanks to the brain-dead persistent naming schemes on udev-
|
479 | 39508e7a | Christoph Hellwig | * based Linux systems those actually are quite common.
|
480 | 39508e7a | Christoph Hellwig | */
|
481 | 39508e7a | Christoph Hellwig | drv1 = find_hdev_driver(filename); |
482 | 39508e7a | Christoph Hellwig | if (drv1) {
|
483 | 39508e7a | Christoph Hellwig | return drv1;
|
484 | 39508e7a | Christoph Hellwig | } |
485 | 39508e7a | Christoph Hellwig | |
486 | 9e0b22f4 | Stefan Hajnoczi | if (!path_has_protocol(filename)) {
|
487 | 39508e7a | Christoph Hellwig | return bdrv_find_format("file"); |
488 | 84a12e66 | Christoph Hellwig | } |
489 | 9e0b22f4 | Stefan Hajnoczi | p = strchr(filename, ':');
|
490 | 9e0b22f4 | Stefan Hajnoczi | assert(p != NULL);
|
491 | 1cec71e3 | Anthony Liguori | len = p - filename; |
492 | 1cec71e3 | Anthony Liguori | if (len > sizeof(protocol) - 1) |
493 | 1cec71e3 | Anthony Liguori | len = sizeof(protocol) - 1; |
494 | 1cec71e3 | Anthony Liguori | memcpy(protocol, filename, len); |
495 | 1cec71e3 | Anthony Liguori | protocol[len] = '\0';
|
496 | 8a22f02a | Stefan Hajnoczi | QLIST_FOREACH(drv1, &bdrv_drivers, list) { |
497 | 5fafdf24 | ths | if (drv1->protocol_name &&
|
498 | 8a22f02a | Stefan Hajnoczi | !strcmp(drv1->protocol_name, protocol)) { |
499 | 83f64091 | bellard | return drv1;
|
500 | 8a22f02a | Stefan Hajnoczi | } |
501 | 83f64091 | bellard | } |
502 | 83f64091 | bellard | return NULL; |
503 | 83f64091 | bellard | } |
504 | 83f64091 | bellard | |
505 | c98ac35d | Stefan Weil | static int find_image_format(const char *filename, BlockDriver **pdrv) |
506 | f3a5d3f8 | Christoph Hellwig | { |
507 | f3a5d3f8 | Christoph Hellwig | int ret, score, score_max;
|
508 | f3a5d3f8 | Christoph Hellwig | BlockDriver *drv1, *drv; |
509 | f3a5d3f8 | Christoph Hellwig | uint8_t buf[2048];
|
510 | f3a5d3f8 | Christoph Hellwig | BlockDriverState *bs; |
511 | f3a5d3f8 | Christoph Hellwig | |
512 | f5edb014 | Naphtali Sprei | ret = bdrv_file_open(&bs, filename, 0);
|
513 | c98ac35d | Stefan Weil | if (ret < 0) { |
514 | c98ac35d | Stefan Weil | *pdrv = NULL;
|
515 | c98ac35d | Stefan Weil | return ret;
|
516 | c98ac35d | Stefan Weil | } |
517 | f8ea0b00 | Nicholas Bellinger | |
518 | 08a00559 | Kevin Wolf | /* Return the raw BlockDriver * to scsi-generic devices or empty drives */
|
519 | 08a00559 | Kevin Wolf | if (bs->sg || !bdrv_is_inserted(bs)) {
|
520 | 1a396859 | Nicholas A. Bellinger | bdrv_delete(bs); |
521 | c98ac35d | Stefan Weil | drv = bdrv_find_format("raw");
|
522 | c98ac35d | Stefan Weil | if (!drv) {
|
523 | c98ac35d | Stefan Weil | ret = -ENOENT; |
524 | c98ac35d | Stefan Weil | } |
525 | c98ac35d | Stefan Weil | *pdrv = drv; |
526 | c98ac35d | Stefan Weil | return ret;
|
527 | 1a396859 | Nicholas A. Bellinger | } |
528 | f8ea0b00 | Nicholas Bellinger | |
529 | 83f64091 | bellard | ret = bdrv_pread(bs, 0, buf, sizeof(buf)); |
530 | 83f64091 | bellard | bdrv_delete(bs); |
531 | 83f64091 | bellard | if (ret < 0) { |
532 | c98ac35d | Stefan Weil | *pdrv = NULL;
|
533 | c98ac35d | Stefan Weil | return ret;
|
534 | 83f64091 | bellard | } |
535 | 83f64091 | bellard | |
536 | ea2384d3 | bellard | score_max = 0;
|
537 | 84a12e66 | Christoph Hellwig | drv = NULL;
|
538 | 8a22f02a | Stefan Hajnoczi | QLIST_FOREACH(drv1, &bdrv_drivers, list) { |
539 | 83f64091 | bellard | if (drv1->bdrv_probe) {
|
540 | 83f64091 | bellard | score = drv1->bdrv_probe(buf, ret, filename); |
541 | 83f64091 | bellard | if (score > score_max) {
|
542 | 83f64091 | bellard | score_max = score; |
543 | 83f64091 | bellard | drv = drv1; |
544 | 83f64091 | bellard | } |
545 | 0849bf08 | bellard | } |
546 | fc01f7e7 | bellard | } |
547 | c98ac35d | Stefan Weil | if (!drv) {
|
548 | c98ac35d | Stefan Weil | ret = -ENOENT; |
549 | c98ac35d | Stefan Weil | } |
550 | c98ac35d | Stefan Weil | *pdrv = drv; |
551 | c98ac35d | Stefan Weil | return ret;
|
552 | ea2384d3 | bellard | } |
553 | ea2384d3 | bellard | |
554 | 51762288 | Stefan Hajnoczi | /**
|
555 | 51762288 | Stefan Hajnoczi | * Set the current 'total_sectors' value
|
556 | 51762288 | Stefan Hajnoczi | */
|
557 | 51762288 | Stefan Hajnoczi | static int refresh_total_sectors(BlockDriverState *bs, int64_t hint) |
558 | 51762288 | Stefan Hajnoczi | { |
559 | 51762288 | Stefan Hajnoczi | BlockDriver *drv = bs->drv; |
560 | 51762288 | Stefan Hajnoczi | |
561 | 396759ad | Nicholas Bellinger | /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */
|
562 | 396759ad | Nicholas Bellinger | if (bs->sg)
|
563 | 396759ad | Nicholas Bellinger | return 0; |
564 | 396759ad | Nicholas Bellinger | |
565 | 51762288 | Stefan Hajnoczi | /* query actual device if possible, otherwise just trust the hint */
|
566 | 51762288 | Stefan Hajnoczi | if (drv->bdrv_getlength) {
|
567 | 51762288 | Stefan Hajnoczi | int64_t length = drv->bdrv_getlength(bs); |
568 | 51762288 | Stefan Hajnoczi | if (length < 0) { |
569 | 51762288 | Stefan Hajnoczi | return length;
|
570 | 51762288 | Stefan Hajnoczi | } |
571 | 51762288 | Stefan Hajnoczi | hint = length >> BDRV_SECTOR_BITS; |
572 | 51762288 | Stefan Hajnoczi | } |
573 | 51762288 | Stefan Hajnoczi | |
574 | 51762288 | Stefan Hajnoczi | bs->total_sectors = hint; |
575 | 51762288 | Stefan Hajnoczi | return 0; |
576 | 51762288 | Stefan Hajnoczi | } |
577 | 51762288 | Stefan Hajnoczi | |
578 | c3993cdc | Stefan Hajnoczi | /**
|
579 | c3993cdc | Stefan Hajnoczi | * Set open flags for a given cache mode
|
580 | c3993cdc | Stefan Hajnoczi | *
|
581 | c3993cdc | Stefan Hajnoczi | * Return 0 on success, -1 if the cache mode was invalid.
|
582 | c3993cdc | Stefan Hajnoczi | */
|
583 | c3993cdc | Stefan Hajnoczi | int bdrv_parse_cache_flags(const char *mode, int *flags) |
584 | c3993cdc | Stefan Hajnoczi | { |
585 | c3993cdc | Stefan Hajnoczi | *flags &= ~BDRV_O_CACHE_MASK; |
586 | c3993cdc | Stefan Hajnoczi | |
587 | c3993cdc | Stefan Hajnoczi | if (!strcmp(mode, "off") || !strcmp(mode, "none")) { |
588 | c3993cdc | Stefan Hajnoczi | *flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB; |
589 | 92196b2f | Stefan Hajnoczi | } else if (!strcmp(mode, "directsync")) { |
590 | 92196b2f | Stefan Hajnoczi | *flags |= BDRV_O_NOCACHE; |
591 | c3993cdc | Stefan Hajnoczi | } else if (!strcmp(mode, "writeback")) { |
592 | c3993cdc | Stefan Hajnoczi | *flags |= BDRV_O_CACHE_WB; |
593 | c3993cdc | Stefan Hajnoczi | } else if (!strcmp(mode, "unsafe")) { |
594 | c3993cdc | Stefan Hajnoczi | *flags |= BDRV_O_CACHE_WB; |
595 | c3993cdc | Stefan Hajnoczi | *flags |= BDRV_O_NO_FLUSH; |
596 | c3993cdc | Stefan Hajnoczi | } else if (!strcmp(mode, "writethrough")) { |
597 | c3993cdc | Stefan Hajnoczi | /* this is the default */
|
598 | c3993cdc | Stefan Hajnoczi | } else {
|
599 | c3993cdc | Stefan Hajnoczi | return -1; |
600 | c3993cdc | Stefan Hajnoczi | } |
601 | c3993cdc | Stefan Hajnoczi | |
602 | c3993cdc | Stefan Hajnoczi | return 0; |
603 | c3993cdc | Stefan Hajnoczi | } |
604 | c3993cdc | Stefan Hajnoczi | |
605 | 53fec9d3 | Stefan Hajnoczi | /**
|
606 | 53fec9d3 | Stefan Hajnoczi | * The copy-on-read flag is actually a reference count so multiple users may
|
607 | 53fec9d3 | Stefan Hajnoczi | * use the feature without worrying about clobbering its previous state.
|
608 | 53fec9d3 | Stefan Hajnoczi | * Copy-on-read stays enabled until all users have called to disable it.
|
609 | 53fec9d3 | Stefan Hajnoczi | */
|
610 | 53fec9d3 | Stefan Hajnoczi | void bdrv_enable_copy_on_read(BlockDriverState *bs)
|
611 | 53fec9d3 | Stefan Hajnoczi | { |
612 | 53fec9d3 | Stefan Hajnoczi | bs->copy_on_read++; |
613 | 53fec9d3 | Stefan Hajnoczi | } |
614 | 53fec9d3 | Stefan Hajnoczi | |
615 | 53fec9d3 | Stefan Hajnoczi | void bdrv_disable_copy_on_read(BlockDriverState *bs)
|
616 | 53fec9d3 | Stefan Hajnoczi | { |
617 | 53fec9d3 | Stefan Hajnoczi | assert(bs->copy_on_read > 0);
|
618 | 53fec9d3 | Stefan Hajnoczi | bs->copy_on_read--; |
619 | 53fec9d3 | Stefan Hajnoczi | } |
620 | 53fec9d3 | Stefan Hajnoczi | |
621 | b6ce07aa | Kevin Wolf | /*
|
622 | 57915332 | Kevin Wolf | * Common part for opening disk images and files
|
623 | 57915332 | Kevin Wolf | */
|
624 | 57915332 | Kevin Wolf | static int bdrv_open_common(BlockDriverState *bs, const char *filename, |
625 | 57915332 | Kevin Wolf | int flags, BlockDriver *drv)
|
626 | 57915332 | Kevin Wolf | { |
627 | 57915332 | Kevin Wolf | int ret, open_flags;
|
628 | 57915332 | Kevin Wolf | |
629 | 57915332 | Kevin Wolf | assert(drv != NULL);
|
630 | 6405875c | Paolo Bonzini | assert(bs->file == NULL);
|
631 | 57915332 | Kevin Wolf | |
632 | 28dcee10 | Stefan Hajnoczi | trace_bdrv_open_common(bs, filename, flags, drv->format_name); |
633 | 28dcee10 | Stefan Hajnoczi | |
634 | 57915332 | Kevin Wolf | bs->open_flags = flags; |
635 | 57915332 | Kevin Wolf | bs->buffer_alignment = 512;
|
636 | 57915332 | Kevin Wolf | |
637 | 53fec9d3 | Stefan Hajnoczi | assert(bs->copy_on_read == 0); /* bdrv_new() and bdrv_close() make it so */ |
638 | 53fec9d3 | Stefan Hajnoczi | if ((flags & BDRV_O_RDWR) && (flags & BDRV_O_COPY_ON_READ)) {
|
639 | 53fec9d3 | Stefan Hajnoczi | bdrv_enable_copy_on_read(bs); |
640 | 53fec9d3 | Stefan Hajnoczi | } |
641 | 53fec9d3 | Stefan Hajnoczi | |
642 | 57915332 | Kevin Wolf | pstrcpy(bs->filename, sizeof(bs->filename), filename);
|
643 | 57915332 | Kevin Wolf | |
644 | 57915332 | Kevin Wolf | if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) {
|
645 | 57915332 | Kevin Wolf | return -ENOTSUP;
|
646 | 57915332 | Kevin Wolf | } |
647 | 57915332 | Kevin Wolf | |
648 | 57915332 | Kevin Wolf | bs->drv = drv; |
649 | 7267c094 | Anthony Liguori | bs->opaque = g_malloc0(drv->instance_size); |
650 | 57915332 | Kevin Wolf | |
651 | 03f541bd | Stefan Hajnoczi | bs->enable_write_cache = !!(flags & BDRV_O_CACHE_WB); |
652 | e1e9b0ac | Paolo Bonzini | open_flags = flags | BDRV_O_CACHE_WB; |
653 | 57915332 | Kevin Wolf | |
654 | 57915332 | Kevin Wolf | /*
|
655 | 57915332 | Kevin Wolf | * Clear flags that are internal to the block layer before opening the
|
656 | 57915332 | Kevin Wolf | * image.
|
657 | 57915332 | Kevin Wolf | */
|
658 | e1e9b0ac | Paolo Bonzini | open_flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); |
659 | 57915332 | Kevin Wolf | |
660 | 57915332 | Kevin Wolf | /*
|
661 | ebabb67a | Stefan Weil | * Snapshots should be writable.
|
662 | 57915332 | Kevin Wolf | */
|
663 | 57915332 | Kevin Wolf | if (bs->is_temporary) {
|
664 | 57915332 | Kevin Wolf | open_flags |= BDRV_O_RDWR; |
665 | 57915332 | Kevin Wolf | } |
666 | 57915332 | Kevin Wolf | |
667 | e7c63796 | Stefan Hajnoczi | bs->keep_read_only = bs->read_only = !(open_flags & BDRV_O_RDWR); |
668 | e7c63796 | Stefan Hajnoczi | |
669 | 66f82cee | Kevin Wolf | /* Open the image, either directly or using a protocol */
|
670 | 66f82cee | Kevin Wolf | if (drv->bdrv_file_open) {
|
671 | 66f82cee | Kevin Wolf | ret = drv->bdrv_file_open(bs, filename, open_flags); |
672 | 66f82cee | Kevin Wolf | } else {
|
673 | 66f82cee | Kevin Wolf | ret = bdrv_file_open(&bs->file, filename, open_flags); |
674 | 66f82cee | Kevin Wolf | if (ret >= 0) { |
675 | 66f82cee | Kevin Wolf | ret = drv->bdrv_open(bs, open_flags); |
676 | 66f82cee | Kevin Wolf | } |
677 | 66f82cee | Kevin Wolf | } |
678 | 66f82cee | Kevin Wolf | |
679 | 57915332 | Kevin Wolf | if (ret < 0) { |
680 | 57915332 | Kevin Wolf | goto free_and_fail;
|
681 | 57915332 | Kevin Wolf | } |
682 | 57915332 | Kevin Wolf | |
683 | 51762288 | Stefan Hajnoczi | ret = refresh_total_sectors(bs, bs->total_sectors); |
684 | 51762288 | Stefan Hajnoczi | if (ret < 0) { |
685 | 51762288 | Stefan Hajnoczi | goto free_and_fail;
|
686 | 57915332 | Kevin Wolf | } |
687 | 51762288 | Stefan Hajnoczi | |
688 | 57915332 | Kevin Wolf | #ifndef _WIN32
|
689 | 57915332 | Kevin Wolf | if (bs->is_temporary) {
|
690 | 57915332 | Kevin Wolf | unlink(filename); |
691 | 57915332 | Kevin Wolf | } |
692 | 57915332 | Kevin Wolf | #endif
|
693 | 57915332 | Kevin Wolf | return 0; |
694 | 57915332 | Kevin Wolf | |
695 | 57915332 | Kevin Wolf | free_and_fail:
|
696 | 66f82cee | Kevin Wolf | if (bs->file) {
|
697 | 66f82cee | Kevin Wolf | bdrv_delete(bs->file); |
698 | 66f82cee | Kevin Wolf | bs->file = NULL;
|
699 | 66f82cee | Kevin Wolf | } |
700 | 7267c094 | Anthony Liguori | g_free(bs->opaque); |
701 | 57915332 | Kevin Wolf | bs->opaque = NULL;
|
702 | 57915332 | Kevin Wolf | bs->drv = NULL;
|
703 | 57915332 | Kevin Wolf | return ret;
|
704 | 57915332 | Kevin Wolf | } |
705 | 57915332 | Kevin Wolf | |
706 | 57915332 | Kevin Wolf | /*
|
707 | b6ce07aa | Kevin Wolf | * Opens a file using a protocol (file, host_device, nbd, ...)
|
708 | b6ce07aa | Kevin Wolf | */
|
709 | 83f64091 | bellard | int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags) |
710 | ea2384d3 | bellard | { |
711 | 83f64091 | bellard | BlockDriverState *bs; |
712 | 6db95603 | Christoph Hellwig | BlockDriver *drv; |
713 | 83f64091 | bellard | int ret;
|
714 | 83f64091 | bellard | |
715 | b50cbabc | MORITA Kazutaka | drv = bdrv_find_protocol(filename); |
716 | 6db95603 | Christoph Hellwig | if (!drv) {
|
717 | 6db95603 | Christoph Hellwig | return -ENOENT;
|
718 | 6db95603 | Christoph Hellwig | } |
719 | 6db95603 | Christoph Hellwig | |
720 | 83f64091 | bellard | bs = bdrv_new("");
|
721 | b6ce07aa | Kevin Wolf | ret = bdrv_open_common(bs, filename, flags, drv); |
722 | 83f64091 | bellard | if (ret < 0) { |
723 | 83f64091 | bellard | bdrv_delete(bs); |
724 | 83f64091 | bellard | return ret;
|
725 | 3b0d4f61 | bellard | } |
726 | 71d0770c | aliguori | bs->growable = 1;
|
727 | 83f64091 | bellard | *pbs = bs; |
728 | 83f64091 | bellard | return 0; |
729 | 83f64091 | bellard | } |
730 | 83f64091 | bellard | |
731 | b6ce07aa | Kevin Wolf | /*
|
732 | b6ce07aa | Kevin Wolf | * Opens a disk image (raw, qcow2, vmdk, ...)
|
733 | b6ce07aa | Kevin Wolf | */
|
734 | d6e9098e | Kevin Wolf | int bdrv_open(BlockDriverState *bs, const char *filename, int flags, |
735 | d6e9098e | Kevin Wolf | BlockDriver *drv) |
736 | ea2384d3 | bellard | { |
737 | b6ce07aa | Kevin Wolf | int ret;
|
738 | 2b572816 | Kevin Wolf | char tmp_filename[PATH_MAX];
|
739 | 712e7874 | bellard | |
740 | 83f64091 | bellard | if (flags & BDRV_O_SNAPSHOT) {
|
741 | ea2384d3 | bellard | BlockDriverState *bs1; |
742 | ea2384d3 | bellard | int64_t total_size; |
743 | 7c96d46e | aliguori | int is_protocol = 0; |
744 | 91a073a9 | Kevin Wolf | BlockDriver *bdrv_qcow2; |
745 | 91a073a9 | Kevin Wolf | QEMUOptionParameter *options; |
746 | b6ce07aa | Kevin Wolf | char backing_filename[PATH_MAX];
|
747 | 3b46e624 | ths | |
748 | ea2384d3 | bellard | /* if snapshot, we create a temporary backing file and open it
|
749 | ea2384d3 | bellard | instead of opening 'filename' directly */
|
750 | 33e3963e | bellard | |
751 | ea2384d3 | bellard | /* if there is a backing file, use it */
|
752 | ea2384d3 | bellard | bs1 = bdrv_new("");
|
753 | d6e9098e | Kevin Wolf | ret = bdrv_open(bs1, filename, 0, drv);
|
754 | 51d7c00c | aliguori | if (ret < 0) { |
755 | ea2384d3 | bellard | bdrv_delete(bs1); |
756 | 51d7c00c | aliguori | return ret;
|
757 | ea2384d3 | bellard | } |
758 | 3e82990b | Jes Sorensen | total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK; |
759 | 7c96d46e | aliguori | |
760 | 7c96d46e | aliguori | if (bs1->drv && bs1->drv->protocol_name)
|
761 | 7c96d46e | aliguori | is_protocol = 1;
|
762 | 7c96d46e | aliguori | |
763 | ea2384d3 | bellard | bdrv_delete(bs1); |
764 | 3b46e624 | ths | |
765 | eba25057 | Jim Meyering | ret = get_tmp_filename(tmp_filename, sizeof(tmp_filename));
|
766 | eba25057 | Jim Meyering | if (ret < 0) { |
767 | eba25057 | Jim Meyering | return ret;
|
768 | eba25057 | Jim Meyering | } |
769 | 7c96d46e | aliguori | |
770 | 7c96d46e | aliguori | /* Real path is meaningless for protocols */
|
771 | 7c96d46e | aliguori | if (is_protocol)
|
772 | 7c96d46e | aliguori | snprintf(backing_filename, sizeof(backing_filename),
|
773 | 7c96d46e | aliguori | "%s", filename);
|
774 | 114cdfa9 | Kirill A. Shutemov | else if (!realpath(filename, backing_filename)) |
775 | 114cdfa9 | Kirill A. Shutemov | return -errno;
|
776 | 7c96d46e | aliguori | |
777 | 91a073a9 | Kevin Wolf | bdrv_qcow2 = bdrv_find_format("qcow2");
|
778 | 91a073a9 | Kevin Wolf | options = parse_option_parameters("", bdrv_qcow2->create_options, NULL); |
779 | 91a073a9 | Kevin Wolf | |
780 | 3e82990b | Jes Sorensen | set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size); |
781 | 91a073a9 | Kevin Wolf | set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename); |
782 | 91a073a9 | Kevin Wolf | if (drv) {
|
783 | 91a073a9 | Kevin Wolf | set_option_parameter(options, BLOCK_OPT_BACKING_FMT, |
784 | 91a073a9 | Kevin Wolf | drv->format_name); |
785 | 91a073a9 | Kevin Wolf | } |
786 | 91a073a9 | Kevin Wolf | |
787 | 91a073a9 | Kevin Wolf | ret = bdrv_create(bdrv_qcow2, tmp_filename, options); |
788 | d748768c | Jan Kiszka | free_option_parameters(options); |
789 | 51d7c00c | aliguori | if (ret < 0) { |
790 | 51d7c00c | aliguori | return ret;
|
791 | ea2384d3 | bellard | } |
792 | 91a073a9 | Kevin Wolf | |
793 | ea2384d3 | bellard | filename = tmp_filename; |
794 | 91a073a9 | Kevin Wolf | drv = bdrv_qcow2; |
795 | ea2384d3 | bellard | bs->is_temporary = 1;
|
796 | ea2384d3 | bellard | } |
797 | 712e7874 | bellard | |
798 | b6ce07aa | Kevin Wolf | /* Find the right image format driver */
|
799 | 6db95603 | Christoph Hellwig | if (!drv) {
|
800 | c98ac35d | Stefan Weil | ret = find_image_format(filename, &drv); |
801 | 51d7c00c | aliguori | } |
802 | 6987307c | Christoph Hellwig | |
803 | 51d7c00c | aliguori | if (!drv) {
|
804 | 51d7c00c | aliguori | goto unlink_and_fail;
|
805 | ea2384d3 | bellard | } |
806 | b6ce07aa | Kevin Wolf | |
807 | b6ce07aa | Kevin Wolf | /* Open the image */
|
808 | b6ce07aa | Kevin Wolf | ret = bdrv_open_common(bs, filename, flags, drv); |
809 | b6ce07aa | Kevin Wolf | if (ret < 0) { |
810 | 6987307c | Christoph Hellwig | goto unlink_and_fail;
|
811 | 6987307c | Christoph Hellwig | } |
812 | 6987307c | Christoph Hellwig | |
813 | b6ce07aa | Kevin Wolf | /* If there is a backing file, use it */
|
814 | b6ce07aa | Kevin Wolf | if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') { |
815 | b6ce07aa | Kevin Wolf | char backing_filename[PATH_MAX];
|
816 | b6ce07aa | Kevin Wolf | int back_flags;
|
817 | b6ce07aa | Kevin Wolf | BlockDriver *back_drv = NULL;
|
818 | b6ce07aa | Kevin Wolf | |
819 | b6ce07aa | Kevin Wolf | bs->backing_hd = bdrv_new("");
|
820 | dc5a1371 | Paolo Bonzini | bdrv_get_full_backing_filename(bs, backing_filename, |
821 | dc5a1371 | Paolo Bonzini | sizeof(backing_filename));
|
822 | df2dbb4a | Stefan Hajnoczi | |
823 | df2dbb4a | Stefan Hajnoczi | if (bs->backing_format[0] != '\0') { |
824 | b6ce07aa | Kevin Wolf | back_drv = bdrv_find_format(bs->backing_format); |
825 | df2dbb4a | Stefan Hajnoczi | } |
826 | b6ce07aa | Kevin Wolf | |
827 | b6ce07aa | Kevin Wolf | /* backing files always opened read-only */
|
828 | b6ce07aa | Kevin Wolf | back_flags = |
829 | b6ce07aa | Kevin Wolf | flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); |
830 | b6ce07aa | Kevin Wolf | |
831 | b6ce07aa | Kevin Wolf | ret = bdrv_open(bs->backing_hd, backing_filename, back_flags, back_drv); |
832 | b6ce07aa | Kevin Wolf | if (ret < 0) { |
833 | b6ce07aa | Kevin Wolf | bdrv_close(bs); |
834 | b6ce07aa | Kevin Wolf | return ret;
|
835 | b6ce07aa | Kevin Wolf | } |
836 | b6ce07aa | Kevin Wolf | if (bs->is_temporary) {
|
837 | b6ce07aa | Kevin Wolf | bs->backing_hd->keep_read_only = !(flags & BDRV_O_RDWR); |
838 | b6ce07aa | Kevin Wolf | } else {
|
839 | b6ce07aa | Kevin Wolf | /* base image inherits from "parent" */
|
840 | b6ce07aa | Kevin Wolf | bs->backing_hd->keep_read_only = bs->keep_read_only; |
841 | b6ce07aa | Kevin Wolf | } |
842 | b6ce07aa | Kevin Wolf | } |
843 | b6ce07aa | Kevin Wolf | |
844 | b6ce07aa | Kevin Wolf | if (!bdrv_key_required(bs)) {
|
845 | 7d4b4ba5 | Markus Armbruster | bdrv_dev_change_media_cb(bs, true);
|
846 | b6ce07aa | Kevin Wolf | } |
847 | b6ce07aa | Kevin Wolf | |
848 | 98f90dba | Zhi Yong Wu | /* throttling disk I/O limits */
|
849 | 98f90dba | Zhi Yong Wu | if (bs->io_limits_enabled) {
|
850 | 98f90dba | Zhi Yong Wu | bdrv_io_limits_enable(bs); |
851 | 98f90dba | Zhi Yong Wu | } |
852 | 98f90dba | Zhi Yong Wu | |
853 | b6ce07aa | Kevin Wolf | return 0; |
854 | b6ce07aa | Kevin Wolf | |
855 | b6ce07aa | Kevin Wolf | unlink_and_fail:
|
856 | b6ce07aa | Kevin Wolf | if (bs->is_temporary) {
|
857 | b6ce07aa | Kevin Wolf | unlink(filename); |
858 | b6ce07aa | Kevin Wolf | } |
859 | b6ce07aa | Kevin Wolf | return ret;
|
860 | b6ce07aa | Kevin Wolf | } |
861 | b6ce07aa | Kevin Wolf | |
862 | fc01f7e7 | bellard | void bdrv_close(BlockDriverState *bs)
|
863 | fc01f7e7 | bellard | { |
864 | 80ccf93b | Liu Yuan | bdrv_flush(bs); |
865 | 19cb3738 | bellard | if (bs->drv) {
|
866 | 3e914655 | Paolo Bonzini | if (bs->job) {
|
867 | 3e914655 | Paolo Bonzini | block_job_cancel_sync(bs->job); |
868 | 3e914655 | Paolo Bonzini | } |
869 | 7094f12f | Kevin Wolf | bdrv_drain_all(); |
870 | 7094f12f | Kevin Wolf | |
871 | f9092b10 | Markus Armbruster | if (bs == bs_snapshots) {
|
872 | f9092b10 | Markus Armbruster | bs_snapshots = NULL;
|
873 | f9092b10 | Markus Armbruster | } |
874 | 557df6ac | Stefan Hajnoczi | if (bs->backing_hd) {
|
875 | ea2384d3 | bellard | bdrv_delete(bs->backing_hd); |
876 | 557df6ac | Stefan Hajnoczi | bs->backing_hd = NULL;
|
877 | 557df6ac | Stefan Hajnoczi | } |
878 | ea2384d3 | bellard | bs->drv->bdrv_close(bs); |
879 | 7267c094 | Anthony Liguori | g_free(bs->opaque); |
880 | ea2384d3 | bellard | #ifdef _WIN32
|
881 | ea2384d3 | bellard | if (bs->is_temporary) {
|
882 | ea2384d3 | bellard | unlink(bs->filename); |
883 | ea2384d3 | bellard | } |
884 | 67b915a5 | bellard | #endif
|
885 | ea2384d3 | bellard | bs->opaque = NULL;
|
886 | ea2384d3 | bellard | bs->drv = NULL;
|
887 | 53fec9d3 | Stefan Hajnoczi | bs->copy_on_read = 0;
|
888 | a275fa42 | Paolo Bonzini | bs->backing_file[0] = '\0'; |
889 | a275fa42 | Paolo Bonzini | bs->backing_format[0] = '\0'; |
890 | 6405875c | Paolo Bonzini | bs->total_sectors = 0;
|
891 | 6405875c | Paolo Bonzini | bs->encrypted = 0;
|
892 | 6405875c | Paolo Bonzini | bs->valid_key = 0;
|
893 | 6405875c | Paolo Bonzini | bs->sg = 0;
|
894 | 6405875c | Paolo Bonzini | bs->growable = 0;
|
895 | b338082b | bellard | |
896 | 66f82cee | Kevin Wolf | if (bs->file != NULL) { |
897 | 0ac9377d | Paolo Bonzini | bdrv_delete(bs->file); |
898 | 0ac9377d | Paolo Bonzini | bs->file = NULL;
|
899 | 66f82cee | Kevin Wolf | } |
900 | 66f82cee | Kevin Wolf | |
901 | 7d4b4ba5 | Markus Armbruster | bdrv_dev_change_media_cb(bs, false);
|
902 | b338082b | bellard | } |
903 | 98f90dba | Zhi Yong Wu | |
904 | 98f90dba | Zhi Yong Wu | /*throttling disk I/O limits*/
|
905 | 98f90dba | Zhi Yong Wu | if (bs->io_limits_enabled) {
|
906 | 98f90dba | Zhi Yong Wu | bdrv_io_limits_disable(bs); |
907 | 98f90dba | Zhi Yong Wu | } |
908 | b338082b | bellard | } |
909 | b338082b | bellard | |
910 | 2bc93fed | MORITA Kazutaka | void bdrv_close_all(void) |
911 | 2bc93fed | MORITA Kazutaka | { |
912 | 2bc93fed | MORITA Kazutaka | BlockDriverState *bs; |
913 | 2bc93fed | MORITA Kazutaka | |
914 | 2bc93fed | MORITA Kazutaka | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
915 | 2bc93fed | MORITA Kazutaka | bdrv_close(bs); |
916 | 2bc93fed | MORITA Kazutaka | } |
917 | 2bc93fed | MORITA Kazutaka | } |
918 | 2bc93fed | MORITA Kazutaka | |
919 | 922453bc | Stefan Hajnoczi | /*
|
920 | 922453bc | Stefan Hajnoczi | * Wait for pending requests to complete across all BlockDriverStates
|
921 | 922453bc | Stefan Hajnoczi | *
|
922 | 922453bc | Stefan Hajnoczi | * This function does not flush data to disk, use bdrv_flush_all() for that
|
923 | 922453bc | Stefan Hajnoczi | * after calling this function.
|
924 | 4c355d53 | Zhi Yong Wu | *
|
925 | 4c355d53 | Zhi Yong Wu | * Note that completion of an asynchronous I/O operation can trigger any
|
926 | 4c355d53 | Zhi Yong Wu | * number of other I/O operations on other devices---for example a coroutine
|
927 | 4c355d53 | Zhi Yong Wu | * can be arbitrarily complex and a constant flow of I/O can come until the
|
928 | 4c355d53 | Zhi Yong Wu | * coroutine is complete. Because of this, it is not possible to have a
|
929 | 4c355d53 | Zhi Yong Wu | * function to drain a single device's I/O queue.
|
930 | 922453bc | Stefan Hajnoczi | */
|
931 | 922453bc | Stefan Hajnoczi | void bdrv_drain_all(void) |
932 | 922453bc | Stefan Hajnoczi | { |
933 | 922453bc | Stefan Hajnoczi | BlockDriverState *bs; |
934 | 4c355d53 | Zhi Yong Wu | bool busy;
|
935 | 4c355d53 | Zhi Yong Wu | |
936 | 4c355d53 | Zhi Yong Wu | do {
|
937 | 4c355d53 | Zhi Yong Wu | busy = qemu_aio_wait(); |
938 | 922453bc | Stefan Hajnoczi | |
939 | 4c355d53 | Zhi Yong Wu | /* FIXME: We do not have timer support here, so this is effectively
|
940 | 4c355d53 | Zhi Yong Wu | * a busy wait.
|
941 | 4c355d53 | Zhi Yong Wu | */
|
942 | 4c355d53 | Zhi Yong Wu | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
943 | 4c355d53 | Zhi Yong Wu | if (!qemu_co_queue_empty(&bs->throttled_reqs)) {
|
944 | 4c355d53 | Zhi Yong Wu | qemu_co_queue_restart_all(&bs->throttled_reqs); |
945 | 4c355d53 | Zhi Yong Wu | busy = true;
|
946 | 4c355d53 | Zhi Yong Wu | } |
947 | 4c355d53 | Zhi Yong Wu | } |
948 | 4c355d53 | Zhi Yong Wu | } while (busy);
|
949 | 922453bc | Stefan Hajnoczi | |
950 | 922453bc | Stefan Hajnoczi | /* If requests are still pending there is a bug somewhere */
|
951 | 922453bc | Stefan Hajnoczi | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
952 | 922453bc | Stefan Hajnoczi | assert(QLIST_EMPTY(&bs->tracked_requests)); |
953 | 922453bc | Stefan Hajnoczi | assert(qemu_co_queue_empty(&bs->throttled_reqs)); |
954 | 922453bc | Stefan Hajnoczi | } |
955 | 922453bc | Stefan Hajnoczi | } |
956 | 922453bc | Stefan Hajnoczi | |
957 | d22b2f41 | Ryan Harper | /* make a BlockDriverState anonymous by removing from bdrv_state list.
|
958 | d22b2f41 | Ryan Harper | Also, NULL terminate the device_name to prevent double remove */
|
959 | d22b2f41 | Ryan Harper | void bdrv_make_anon(BlockDriverState *bs)
|
960 | d22b2f41 | Ryan Harper | { |
961 | d22b2f41 | Ryan Harper | if (bs->device_name[0] != '\0') { |
962 | d22b2f41 | Ryan Harper | QTAILQ_REMOVE(&bdrv_states, bs, list); |
963 | d22b2f41 | Ryan Harper | } |
964 | d22b2f41 | Ryan Harper | bs->device_name[0] = '\0'; |
965 | d22b2f41 | Ryan Harper | } |
966 | d22b2f41 | Ryan Harper | |
967 | e023b2e2 | Paolo Bonzini | static void bdrv_rebind(BlockDriverState *bs) |
968 | e023b2e2 | Paolo Bonzini | { |
969 | e023b2e2 | Paolo Bonzini | if (bs->drv && bs->drv->bdrv_rebind) {
|
970 | e023b2e2 | Paolo Bonzini | bs->drv->bdrv_rebind(bs); |
971 | e023b2e2 | Paolo Bonzini | } |
972 | e023b2e2 | Paolo Bonzini | } |
973 | e023b2e2 | Paolo Bonzini | |
974 | 4ddc07ca | Paolo Bonzini | static void bdrv_move_feature_fields(BlockDriverState *bs_dest, |
975 | 4ddc07ca | Paolo Bonzini | BlockDriverState *bs_src) |
976 | 8802d1fd | Jeff Cody | { |
977 | 4ddc07ca | Paolo Bonzini | /* move some fields that need to stay attached to the device */
|
978 | 4ddc07ca | Paolo Bonzini | bs_dest->open_flags = bs_src->open_flags; |
979 | 8802d1fd | Jeff Cody | |
980 | 8802d1fd | Jeff Cody | /* dev info */
|
981 | 4ddc07ca | Paolo Bonzini | bs_dest->dev_ops = bs_src->dev_ops; |
982 | 4ddc07ca | Paolo Bonzini | bs_dest->dev_opaque = bs_src->dev_opaque; |
983 | 4ddc07ca | Paolo Bonzini | bs_dest->dev = bs_src->dev; |
984 | 4ddc07ca | Paolo Bonzini | bs_dest->buffer_alignment = bs_src->buffer_alignment; |
985 | 4ddc07ca | Paolo Bonzini | bs_dest->copy_on_read = bs_src->copy_on_read; |
986 | 8802d1fd | Jeff Cody | |
987 | 4ddc07ca | Paolo Bonzini | bs_dest->enable_write_cache = bs_src->enable_write_cache; |
988 | c4a248a1 | Paolo Bonzini | |
989 | 8802d1fd | Jeff Cody | /* i/o timing parameters */
|
990 | 4ddc07ca | Paolo Bonzini | bs_dest->slice_time = bs_src->slice_time; |
991 | 4ddc07ca | Paolo Bonzini | bs_dest->slice_start = bs_src->slice_start; |
992 | 4ddc07ca | Paolo Bonzini | bs_dest->slice_end = bs_src->slice_end; |
993 | 4ddc07ca | Paolo Bonzini | bs_dest->io_limits = bs_src->io_limits; |
994 | 4ddc07ca | Paolo Bonzini | bs_dest->io_base = bs_src->io_base; |
995 | 4ddc07ca | Paolo Bonzini | bs_dest->throttled_reqs = bs_src->throttled_reqs; |
996 | 4ddc07ca | Paolo Bonzini | bs_dest->block_timer = bs_src->block_timer; |
997 | 4ddc07ca | Paolo Bonzini | bs_dest->io_limits_enabled = bs_src->io_limits_enabled; |
998 | 8802d1fd | Jeff Cody | |
999 | 8802d1fd | Jeff Cody | /* r/w error */
|
1000 | 4ddc07ca | Paolo Bonzini | bs_dest->on_read_error = bs_src->on_read_error; |
1001 | 4ddc07ca | Paolo Bonzini | bs_dest->on_write_error = bs_src->on_write_error; |
1002 | 8802d1fd | Jeff Cody | |
1003 | 8802d1fd | Jeff Cody | /* i/o status */
|
1004 | 4ddc07ca | Paolo Bonzini | bs_dest->iostatus_enabled = bs_src->iostatus_enabled; |
1005 | 4ddc07ca | Paolo Bonzini | bs_dest->iostatus = bs_src->iostatus; |
1006 | 8802d1fd | Jeff Cody | |
1007 | a9fc4408 | Paolo Bonzini | /* dirty bitmap */
|
1008 | 4ddc07ca | Paolo Bonzini | bs_dest->dirty_count = bs_src->dirty_count; |
1009 | 4ddc07ca | Paolo Bonzini | bs_dest->dirty_bitmap = bs_src->dirty_bitmap; |
1010 | a9fc4408 | Paolo Bonzini | |
1011 | a9fc4408 | Paolo Bonzini | /* job */
|
1012 | 4ddc07ca | Paolo Bonzini | bs_dest->in_use = bs_src->in_use; |
1013 | 4ddc07ca | Paolo Bonzini | bs_dest->job = bs_src->job; |
1014 | a9fc4408 | Paolo Bonzini | |
1015 | 8802d1fd | Jeff Cody | /* keep the same entry in bdrv_states */
|
1016 | 4ddc07ca | Paolo Bonzini | pstrcpy(bs_dest->device_name, sizeof(bs_dest->device_name),
|
1017 | 4ddc07ca | Paolo Bonzini | bs_src->device_name); |
1018 | 4ddc07ca | Paolo Bonzini | bs_dest->list = bs_src->list; |
1019 | 4ddc07ca | Paolo Bonzini | } |
1020 | 8802d1fd | Jeff Cody | |
1021 | 4ddc07ca | Paolo Bonzini | /*
|
1022 | 4ddc07ca | Paolo Bonzini | * Swap bs contents for two image chains while they are live,
|
1023 | 4ddc07ca | Paolo Bonzini | * while keeping required fields on the BlockDriverState that is
|
1024 | 4ddc07ca | Paolo Bonzini | * actually attached to a device.
|
1025 | 4ddc07ca | Paolo Bonzini | *
|
1026 | 4ddc07ca | Paolo Bonzini | * This will modify the BlockDriverState fields, and swap contents
|
1027 | 4ddc07ca | Paolo Bonzini | * between bs_new and bs_old. Both bs_new and bs_old are modified.
|
1028 | 4ddc07ca | Paolo Bonzini | *
|
1029 | 4ddc07ca | Paolo Bonzini | * bs_new is required to be anonymous.
|
1030 | 4ddc07ca | Paolo Bonzini | *
|
1031 | 4ddc07ca | Paolo Bonzini | * This function does not create any image files.
|
1032 | 4ddc07ca | Paolo Bonzini | */
|
1033 | 4ddc07ca | Paolo Bonzini | void bdrv_swap(BlockDriverState *bs_new, BlockDriverState *bs_old)
|
1034 | 4ddc07ca | Paolo Bonzini | { |
1035 | 4ddc07ca | Paolo Bonzini | BlockDriverState tmp; |
1036 | f6801b83 | Jeff Cody | |
1037 | 4ddc07ca | Paolo Bonzini | /* bs_new must be anonymous and shouldn't have anything fancy enabled */
|
1038 | 4ddc07ca | Paolo Bonzini | assert(bs_new->device_name[0] == '\0'); |
1039 | 4ddc07ca | Paolo Bonzini | assert(bs_new->dirty_bitmap == NULL);
|
1040 | 4ddc07ca | Paolo Bonzini | assert(bs_new->job == NULL);
|
1041 | 4ddc07ca | Paolo Bonzini | assert(bs_new->dev == NULL);
|
1042 | 4ddc07ca | Paolo Bonzini | assert(bs_new->in_use == 0);
|
1043 | 4ddc07ca | Paolo Bonzini | assert(bs_new->io_limits_enabled == false);
|
1044 | 4ddc07ca | Paolo Bonzini | assert(bs_new->block_timer == NULL);
|
1045 | 8802d1fd | Jeff Cody | |
1046 | 4ddc07ca | Paolo Bonzini | tmp = *bs_new; |
1047 | 4ddc07ca | Paolo Bonzini | *bs_new = *bs_old; |
1048 | 4ddc07ca | Paolo Bonzini | *bs_old = tmp; |
1049 | a9fc4408 | Paolo Bonzini | |
1050 | 4ddc07ca | Paolo Bonzini | /* there are some fields that should not be swapped, move them back */
|
1051 | 4ddc07ca | Paolo Bonzini | bdrv_move_feature_fields(&tmp, bs_old); |
1052 | 4ddc07ca | Paolo Bonzini | bdrv_move_feature_fields(bs_old, bs_new); |
1053 | 4ddc07ca | Paolo Bonzini | bdrv_move_feature_fields(bs_new, &tmp); |
1054 | 8802d1fd | Jeff Cody | |
1055 | 4ddc07ca | Paolo Bonzini | /* bs_new shouldn't be in bdrv_states even after the swap! */
|
1056 | 4ddc07ca | Paolo Bonzini | assert(bs_new->device_name[0] == '\0'); |
1057 | 4ddc07ca | Paolo Bonzini | |
1058 | 4ddc07ca | Paolo Bonzini | /* Check a few fields that should remain attached to the device */
|
1059 | 4ddc07ca | Paolo Bonzini | assert(bs_new->dev == NULL);
|
1060 | 4ddc07ca | Paolo Bonzini | assert(bs_new->job == NULL);
|
1061 | 4ddc07ca | Paolo Bonzini | assert(bs_new->in_use == 0);
|
1062 | 4ddc07ca | Paolo Bonzini | assert(bs_new->io_limits_enabled == false);
|
1063 | 4ddc07ca | Paolo Bonzini | assert(bs_new->block_timer == NULL);
|
1064 | e023b2e2 | Paolo Bonzini | |
1065 | e023b2e2 | Paolo Bonzini | bdrv_rebind(bs_new); |
1066 | 4ddc07ca | Paolo Bonzini | bdrv_rebind(bs_old); |
1067 | 4ddc07ca | Paolo Bonzini | } |
1068 | 4ddc07ca | Paolo Bonzini | |
1069 | 4ddc07ca | Paolo Bonzini | /*
|
1070 | 4ddc07ca | Paolo Bonzini | * Add new bs contents at the top of an image chain while the chain is
|
1071 | 4ddc07ca | Paolo Bonzini | * live, while keeping required fields on the top layer.
|
1072 | 4ddc07ca | Paolo Bonzini | *
|
1073 | 4ddc07ca | Paolo Bonzini | * This will modify the BlockDriverState fields, and swap contents
|
1074 | 4ddc07ca | Paolo Bonzini | * between bs_new and bs_top. Both bs_new and bs_top are modified.
|
1075 | 4ddc07ca | Paolo Bonzini | *
|
1076 | 4ddc07ca | Paolo Bonzini | * bs_new is required to be anonymous.
|
1077 | 4ddc07ca | Paolo Bonzini | *
|
1078 | 4ddc07ca | Paolo Bonzini | * This function does not create any image files.
|
1079 | 4ddc07ca | Paolo Bonzini | */
|
1080 | 4ddc07ca | Paolo Bonzini | void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top)
|
1081 | 4ddc07ca | Paolo Bonzini | { |
1082 | 4ddc07ca | Paolo Bonzini | bdrv_swap(bs_new, bs_top); |
1083 | 4ddc07ca | Paolo Bonzini | |
1084 | 4ddc07ca | Paolo Bonzini | /* The contents of 'tmp' will become bs_top, as we are
|
1085 | 4ddc07ca | Paolo Bonzini | * swapping bs_new and bs_top contents. */
|
1086 | 4ddc07ca | Paolo Bonzini | bs_top->backing_hd = bs_new; |
1087 | 4ddc07ca | Paolo Bonzini | bs_top->open_flags &= ~BDRV_O_NO_BACKING; |
1088 | 4ddc07ca | Paolo Bonzini | pstrcpy(bs_top->backing_file, sizeof(bs_top->backing_file),
|
1089 | 4ddc07ca | Paolo Bonzini | bs_new->filename); |
1090 | 4ddc07ca | Paolo Bonzini | pstrcpy(bs_top->backing_format, sizeof(bs_top->backing_format),
|
1091 | 4ddc07ca | Paolo Bonzini | bs_new->drv ? bs_new->drv->format_name : "");
|
1092 | 8802d1fd | Jeff Cody | } |
1093 | 8802d1fd | Jeff Cody | |
1094 | b338082b | bellard | void bdrv_delete(BlockDriverState *bs)
|
1095 | b338082b | bellard | { |
1096 | fa879d62 | Markus Armbruster | assert(!bs->dev); |
1097 | 3e914655 | Paolo Bonzini | assert(!bs->job); |
1098 | 3e914655 | Paolo Bonzini | assert(!bs->in_use); |
1099 | 18846dee | Markus Armbruster | |
1100 | 1b7bdbc1 | Stefan Hajnoczi | /* remove from list, if necessary */
|
1101 | d22b2f41 | Ryan Harper | bdrv_make_anon(bs); |
1102 | 34c6f050 | aurel32 | |
1103 | b338082b | bellard | bdrv_close(bs); |
1104 | 66f82cee | Kevin Wolf | |
1105 | f9092b10 | Markus Armbruster | assert(bs != bs_snapshots); |
1106 | 7267c094 | Anthony Liguori | g_free(bs); |
1107 | fc01f7e7 | bellard | } |
1108 | fc01f7e7 | bellard | |
1109 | fa879d62 | Markus Armbruster | int bdrv_attach_dev(BlockDriverState *bs, void *dev) |
1110 | fa879d62 | Markus Armbruster | /* TODO change to DeviceState *dev when all users are qdevified */
|
1111 | 18846dee | Markus Armbruster | { |
1112 | fa879d62 | Markus Armbruster | if (bs->dev) {
|
1113 | 18846dee | Markus Armbruster | return -EBUSY;
|
1114 | 18846dee | Markus Armbruster | } |
1115 | fa879d62 | Markus Armbruster | bs->dev = dev; |
1116 | 28a7282a | Luiz Capitulino | bdrv_iostatus_reset(bs); |
1117 | 18846dee | Markus Armbruster | return 0; |
1118 | 18846dee | Markus Armbruster | } |
1119 | 18846dee | Markus Armbruster | |
1120 | fa879d62 | Markus Armbruster | /* TODO qdevified devices don't use this, remove when devices are qdevified */
|
1121 | fa879d62 | Markus Armbruster | void bdrv_attach_dev_nofail(BlockDriverState *bs, void *dev) |
1122 | 18846dee | Markus Armbruster | { |
1123 | fa879d62 | Markus Armbruster | if (bdrv_attach_dev(bs, dev) < 0) { |
1124 | fa879d62 | Markus Armbruster | abort(); |
1125 | fa879d62 | Markus Armbruster | } |
1126 | fa879d62 | Markus Armbruster | } |
1127 | fa879d62 | Markus Armbruster | |
1128 | fa879d62 | Markus Armbruster | void bdrv_detach_dev(BlockDriverState *bs, void *dev) |
1129 | fa879d62 | Markus Armbruster | /* TODO change to DeviceState *dev when all users are qdevified */
|
1130 | fa879d62 | Markus Armbruster | { |
1131 | fa879d62 | Markus Armbruster | assert(bs->dev == dev); |
1132 | fa879d62 | Markus Armbruster | bs->dev = NULL;
|
1133 | 0e49de52 | Markus Armbruster | bs->dev_ops = NULL;
|
1134 | 0e49de52 | Markus Armbruster | bs->dev_opaque = NULL;
|
1135 | 29e05f20 | Markus Armbruster | bs->buffer_alignment = 512;
|
1136 | 18846dee | Markus Armbruster | } |
1137 | 18846dee | Markus Armbruster | |
1138 | fa879d62 | Markus Armbruster | /* TODO change to return DeviceState * when all users are qdevified */
|
1139 | fa879d62 | Markus Armbruster | void *bdrv_get_attached_dev(BlockDriverState *bs)
|
1140 | 18846dee | Markus Armbruster | { |
1141 | fa879d62 | Markus Armbruster | return bs->dev;
|
1142 | 18846dee | Markus Armbruster | } |
1143 | 18846dee | Markus Armbruster | |
1144 | 0e49de52 | Markus Armbruster | void bdrv_set_dev_ops(BlockDriverState *bs, const BlockDevOps *ops, |
1145 | 0e49de52 | Markus Armbruster | void *opaque)
|
1146 | 0e49de52 | Markus Armbruster | { |
1147 | 0e49de52 | Markus Armbruster | bs->dev_ops = ops; |
1148 | 0e49de52 | Markus Armbruster | bs->dev_opaque = opaque; |
1149 | 2c6942fa | Markus Armbruster | if (bdrv_dev_has_removable_media(bs) && bs == bs_snapshots) {
|
1150 | 2c6942fa | Markus Armbruster | bs_snapshots = NULL;
|
1151 | 2c6942fa | Markus Armbruster | } |
1152 | 0e49de52 | Markus Armbruster | } |
1153 | 0e49de52 | Markus Armbruster | |
1154 | 329c0a48 | Luiz Capitulino | void bdrv_emit_qmp_error_event(const BlockDriverState *bdrv, |
1155 | 329c0a48 | Luiz Capitulino | BlockQMPEventAction action, int is_read)
|
1156 | 329c0a48 | Luiz Capitulino | { |
1157 | 329c0a48 | Luiz Capitulino | QObject *data; |
1158 | 329c0a48 | Luiz Capitulino | const char *action_str; |
1159 | 329c0a48 | Luiz Capitulino | |
1160 | 329c0a48 | Luiz Capitulino | switch (action) {
|
1161 | 329c0a48 | Luiz Capitulino | case BDRV_ACTION_REPORT:
|
1162 | 329c0a48 | Luiz Capitulino | action_str = "report";
|
1163 | 329c0a48 | Luiz Capitulino | break;
|
1164 | 329c0a48 | Luiz Capitulino | case BDRV_ACTION_IGNORE:
|
1165 | 329c0a48 | Luiz Capitulino | action_str = "ignore";
|
1166 | 329c0a48 | Luiz Capitulino | break;
|
1167 | 329c0a48 | Luiz Capitulino | case BDRV_ACTION_STOP:
|
1168 | 329c0a48 | Luiz Capitulino | action_str = "stop";
|
1169 | 329c0a48 | Luiz Capitulino | break;
|
1170 | 329c0a48 | Luiz Capitulino | default:
|
1171 | 329c0a48 | Luiz Capitulino | abort(); |
1172 | 329c0a48 | Luiz Capitulino | } |
1173 | 329c0a48 | Luiz Capitulino | |
1174 | 329c0a48 | Luiz Capitulino | data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }",
|
1175 | 329c0a48 | Luiz Capitulino | bdrv->device_name, |
1176 | 329c0a48 | Luiz Capitulino | action_str, |
1177 | 329c0a48 | Luiz Capitulino | is_read ? "read" : "write"); |
1178 | 329c0a48 | Luiz Capitulino | monitor_protocol_event(QEVENT_BLOCK_IO_ERROR, data); |
1179 | 329c0a48 | Luiz Capitulino | |
1180 | 329c0a48 | Luiz Capitulino | qobject_decref(data); |
1181 | 329c0a48 | Luiz Capitulino | } |
1182 | 329c0a48 | Luiz Capitulino | |
1183 | 6f382ed2 | Luiz Capitulino | static void bdrv_emit_qmp_eject_event(BlockDriverState *bs, bool ejected) |
1184 | 6f382ed2 | Luiz Capitulino | { |
1185 | 6f382ed2 | Luiz Capitulino | QObject *data; |
1186 | 6f382ed2 | Luiz Capitulino | |
1187 | 6f382ed2 | Luiz Capitulino | data = qobject_from_jsonf("{ 'device': %s, 'tray-open': %i }",
|
1188 | 6f382ed2 | Luiz Capitulino | bdrv_get_device_name(bs), ejected); |
1189 | 6f382ed2 | Luiz Capitulino | monitor_protocol_event(QEVENT_DEVICE_TRAY_MOVED, data); |
1190 | 6f382ed2 | Luiz Capitulino | |
1191 | 6f382ed2 | Luiz Capitulino | qobject_decref(data); |
1192 | 6f382ed2 | Luiz Capitulino | } |
1193 | 6f382ed2 | Luiz Capitulino | |
1194 | 7d4b4ba5 | Markus Armbruster | static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load) |
1195 | 0e49de52 | Markus Armbruster | { |
1196 | 145feb17 | Markus Armbruster | if (bs->dev_ops && bs->dev_ops->change_media_cb) {
|
1197 | 6f382ed2 | Luiz Capitulino | bool tray_was_closed = !bdrv_dev_is_tray_open(bs);
|
1198 | 7d4b4ba5 | Markus Armbruster | bs->dev_ops->change_media_cb(bs->dev_opaque, load); |
1199 | 6f382ed2 | Luiz Capitulino | if (tray_was_closed) {
|
1200 | 6f382ed2 | Luiz Capitulino | /* tray open */
|
1201 | 6f382ed2 | Luiz Capitulino | bdrv_emit_qmp_eject_event(bs, true);
|
1202 | 6f382ed2 | Luiz Capitulino | } |
1203 | 6f382ed2 | Luiz Capitulino | if (load) {
|
1204 | 6f382ed2 | Luiz Capitulino | /* tray close */
|
1205 | 6f382ed2 | Luiz Capitulino | bdrv_emit_qmp_eject_event(bs, false);
|
1206 | 6f382ed2 | Luiz Capitulino | } |
1207 | 145feb17 | Markus Armbruster | } |
1208 | 145feb17 | Markus Armbruster | } |
1209 | 145feb17 | Markus Armbruster | |
1210 | 2c6942fa | Markus Armbruster | bool bdrv_dev_has_removable_media(BlockDriverState *bs)
|
1211 | 2c6942fa | Markus Armbruster | { |
1212 | 2c6942fa | Markus Armbruster | return !bs->dev || (bs->dev_ops && bs->dev_ops->change_media_cb);
|
1213 | 2c6942fa | Markus Armbruster | } |
1214 | 2c6942fa | Markus Armbruster | |
1215 | 025ccaa7 | Paolo Bonzini | void bdrv_dev_eject_request(BlockDriverState *bs, bool force) |
1216 | 025ccaa7 | Paolo Bonzini | { |
1217 | 025ccaa7 | Paolo Bonzini | if (bs->dev_ops && bs->dev_ops->eject_request_cb) {
|
1218 | 025ccaa7 | Paolo Bonzini | bs->dev_ops->eject_request_cb(bs->dev_opaque, force); |
1219 | 025ccaa7 | Paolo Bonzini | } |
1220 | 025ccaa7 | Paolo Bonzini | } |
1221 | 025ccaa7 | Paolo Bonzini | |
1222 | e4def80b | Markus Armbruster | bool bdrv_dev_is_tray_open(BlockDriverState *bs)
|
1223 | e4def80b | Markus Armbruster | { |
1224 | e4def80b | Markus Armbruster | if (bs->dev_ops && bs->dev_ops->is_tray_open) {
|
1225 | e4def80b | Markus Armbruster | return bs->dev_ops->is_tray_open(bs->dev_opaque);
|
1226 | e4def80b | Markus Armbruster | } |
1227 | e4def80b | Markus Armbruster | return false; |
1228 | e4def80b | Markus Armbruster | } |
1229 | e4def80b | Markus Armbruster | |
1230 | 145feb17 | Markus Armbruster | static void bdrv_dev_resize_cb(BlockDriverState *bs) |
1231 | 145feb17 | Markus Armbruster | { |
1232 | 145feb17 | Markus Armbruster | if (bs->dev_ops && bs->dev_ops->resize_cb) {
|
1233 | 145feb17 | Markus Armbruster | bs->dev_ops->resize_cb(bs->dev_opaque); |
1234 | 0e49de52 | Markus Armbruster | } |
1235 | 0e49de52 | Markus Armbruster | } |
1236 | 0e49de52 | Markus Armbruster | |
1237 | f107639a | Markus Armbruster | bool bdrv_dev_is_medium_locked(BlockDriverState *bs)
|
1238 | f107639a | Markus Armbruster | { |
1239 | f107639a | Markus Armbruster | if (bs->dev_ops && bs->dev_ops->is_medium_locked) {
|
1240 | f107639a | Markus Armbruster | return bs->dev_ops->is_medium_locked(bs->dev_opaque);
|
1241 | f107639a | Markus Armbruster | } |
1242 | f107639a | Markus Armbruster | return false; |
1243 | f107639a | Markus Armbruster | } |
1244 | f107639a | Markus Armbruster | |
1245 | e97fc193 | aliguori | /*
|
1246 | e97fc193 | aliguori | * Run consistency checks on an image
|
1247 | e97fc193 | aliguori | *
|
1248 | e076f338 | Kevin Wolf | * Returns 0 if the check could be completed (it doesn't mean that the image is
|
1249 | a1c7273b | Stefan Weil | * free of errors) or -errno when an internal error occurred. The results of the
|
1250 | e076f338 | Kevin Wolf | * check are stored in res.
|
1251 | e97fc193 | aliguori | */
|
1252 | 4534ff54 | Kevin Wolf | int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix)
|
1253 | e97fc193 | aliguori | { |
1254 | e97fc193 | aliguori | if (bs->drv->bdrv_check == NULL) { |
1255 | e97fc193 | aliguori | return -ENOTSUP;
|
1256 | e97fc193 | aliguori | } |
1257 | e97fc193 | aliguori | |
1258 | e076f338 | Kevin Wolf | memset(res, 0, sizeof(*res)); |
1259 | 4534ff54 | Kevin Wolf | return bs->drv->bdrv_check(bs, res, fix);
|
1260 | e97fc193 | aliguori | } |
1261 | e97fc193 | aliguori | |
1262 | 8a426614 | Kevin Wolf | #define COMMIT_BUF_SECTORS 2048 |
1263 | 8a426614 | Kevin Wolf | |
1264 | 33e3963e | bellard | /* commit COW file into the raw image */
|
1265 | 33e3963e | bellard | int bdrv_commit(BlockDriverState *bs)
|
1266 | 33e3963e | bellard | { |
1267 | 19cb3738 | bellard | BlockDriver *drv = bs->drv; |
1268 | ee181196 | Kevin Wolf | BlockDriver *backing_drv; |
1269 | 8a426614 | Kevin Wolf | int64_t sector, total_sectors; |
1270 | 8a426614 | Kevin Wolf | int n, ro, open_flags;
|
1271 | 4dca4b63 | Naphtali Sprei | int ret = 0, rw_ret = 0; |
1272 | 8a426614 | Kevin Wolf | uint8_t *buf; |
1273 | 4dca4b63 | Naphtali Sprei | char filename[1024]; |
1274 | 4dca4b63 | Naphtali Sprei | BlockDriverState *bs_rw, *bs_ro; |
1275 | 33e3963e | bellard | |
1276 | 19cb3738 | bellard | if (!drv)
|
1277 | 19cb3738 | bellard | return -ENOMEDIUM;
|
1278 | 4dca4b63 | Naphtali Sprei | |
1279 | 4dca4b63 | Naphtali Sprei | if (!bs->backing_hd) {
|
1280 | 4dca4b63 | Naphtali Sprei | return -ENOTSUP;
|
1281 | 33e3963e | bellard | } |
1282 | 33e3963e | bellard | |
1283 | 4dca4b63 | Naphtali Sprei | if (bs->backing_hd->keep_read_only) {
|
1284 | 4dca4b63 | Naphtali Sprei | return -EACCES;
|
1285 | 4dca4b63 | Naphtali Sprei | } |
1286 | ee181196 | Kevin Wolf | |
1287 | 2d3735d3 | Stefan Hajnoczi | if (bdrv_in_use(bs) || bdrv_in_use(bs->backing_hd)) {
|
1288 | 2d3735d3 | Stefan Hajnoczi | return -EBUSY;
|
1289 | 2d3735d3 | Stefan Hajnoczi | } |
1290 | 2d3735d3 | Stefan Hajnoczi | |
1291 | ee181196 | Kevin Wolf | backing_drv = bs->backing_hd->drv; |
1292 | 4dca4b63 | Naphtali Sprei | ro = bs->backing_hd->read_only; |
1293 | 4dca4b63 | Naphtali Sprei | strncpy(filename, bs->backing_hd->filename, sizeof(filename));
|
1294 | 4dca4b63 | Naphtali Sprei | open_flags = bs->backing_hd->open_flags; |
1295 | 4dca4b63 | Naphtali Sprei | |
1296 | 4dca4b63 | Naphtali Sprei | if (ro) {
|
1297 | 4dca4b63 | Naphtali Sprei | /* re-open as RW */
|
1298 | 4dca4b63 | Naphtali Sprei | bdrv_delete(bs->backing_hd); |
1299 | 4dca4b63 | Naphtali Sprei | bs->backing_hd = NULL;
|
1300 | 4dca4b63 | Naphtali Sprei | bs_rw = bdrv_new("");
|
1301 | ee181196 | Kevin Wolf | rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR, |
1302 | ee181196 | Kevin Wolf | backing_drv); |
1303 | 4dca4b63 | Naphtali Sprei | if (rw_ret < 0) { |
1304 | 4dca4b63 | Naphtali Sprei | bdrv_delete(bs_rw); |
1305 | 4dca4b63 | Naphtali Sprei | /* try to re-open read-only */
|
1306 | 4dca4b63 | Naphtali Sprei | bs_ro = bdrv_new("");
|
1307 | ee181196 | Kevin Wolf | ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, |
1308 | ee181196 | Kevin Wolf | backing_drv); |
1309 | 4dca4b63 | Naphtali Sprei | if (ret < 0) { |
1310 | 4dca4b63 | Naphtali Sprei | bdrv_delete(bs_ro); |
1311 | 4dca4b63 | Naphtali Sprei | /* drive not functional anymore */
|
1312 | 4dca4b63 | Naphtali Sprei | bs->drv = NULL;
|
1313 | 4dca4b63 | Naphtali Sprei | return ret;
|
1314 | 4dca4b63 | Naphtali Sprei | } |
1315 | 4dca4b63 | Naphtali Sprei | bs->backing_hd = bs_ro; |
1316 | 4dca4b63 | Naphtali Sprei | return rw_ret;
|
1317 | 4dca4b63 | Naphtali Sprei | } |
1318 | 4dca4b63 | Naphtali Sprei | bs->backing_hd = bs_rw; |
1319 | ea2384d3 | bellard | } |
1320 | 33e3963e | bellard | |
1321 | 6ea44308 | Jan Kiszka | total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; |
1322 | 7267c094 | Anthony Liguori | buf = g_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE); |
1323 | 8a426614 | Kevin Wolf | |
1324 | 8a426614 | Kevin Wolf | for (sector = 0; sector < total_sectors; sector += n) { |
1325 | 05c4af54 | Stefan Hajnoczi | if (bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n)) {
|
1326 | 8a426614 | Kevin Wolf | |
1327 | 8a426614 | Kevin Wolf | if (bdrv_read(bs, sector, buf, n) != 0) { |
1328 | 8a426614 | Kevin Wolf | ret = -EIO; |
1329 | 8a426614 | Kevin Wolf | goto ro_cleanup;
|
1330 | 8a426614 | Kevin Wolf | } |
1331 | 8a426614 | Kevin Wolf | |
1332 | 8a426614 | Kevin Wolf | if (bdrv_write(bs->backing_hd, sector, buf, n) != 0) { |
1333 | 8a426614 | Kevin Wolf | ret = -EIO; |
1334 | 8a426614 | Kevin Wolf | goto ro_cleanup;
|
1335 | 8a426614 | Kevin Wolf | } |
1336 | ea2384d3 | bellard | } |
1337 | 33e3963e | bellard | } |
1338 | 95389c86 | bellard | |
1339 | 1d44952f | Christoph Hellwig | if (drv->bdrv_make_empty) {
|
1340 | 1d44952f | Christoph Hellwig | ret = drv->bdrv_make_empty(bs); |
1341 | 1d44952f | Christoph Hellwig | bdrv_flush(bs); |
1342 | 1d44952f | Christoph Hellwig | } |
1343 | 95389c86 | bellard | |
1344 | 3f5075ae | Christoph Hellwig | /*
|
1345 | 3f5075ae | Christoph Hellwig | * Make sure all data we wrote to the backing device is actually
|
1346 | 3f5075ae | Christoph Hellwig | * stable on disk.
|
1347 | 3f5075ae | Christoph Hellwig | */
|
1348 | 3f5075ae | Christoph Hellwig | if (bs->backing_hd)
|
1349 | 3f5075ae | Christoph Hellwig | bdrv_flush(bs->backing_hd); |
1350 | 4dca4b63 | Naphtali Sprei | |
1351 | 4dca4b63 | Naphtali Sprei | ro_cleanup:
|
1352 | 7267c094 | Anthony Liguori | g_free(buf); |
1353 | 4dca4b63 | Naphtali Sprei | |
1354 | 4dca4b63 | Naphtali Sprei | if (ro) {
|
1355 | 4dca4b63 | Naphtali Sprei | /* re-open as RO */
|
1356 | 4dca4b63 | Naphtali Sprei | bdrv_delete(bs->backing_hd); |
1357 | 4dca4b63 | Naphtali Sprei | bs->backing_hd = NULL;
|
1358 | 4dca4b63 | Naphtali Sprei | bs_ro = bdrv_new("");
|
1359 | ee181196 | Kevin Wolf | ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, |
1360 | ee181196 | Kevin Wolf | backing_drv); |
1361 | 4dca4b63 | Naphtali Sprei | if (ret < 0) { |
1362 | 4dca4b63 | Naphtali Sprei | bdrv_delete(bs_ro); |
1363 | 4dca4b63 | Naphtali Sprei | /* drive not functional anymore */
|
1364 | 4dca4b63 | Naphtali Sprei | bs->drv = NULL;
|
1365 | 4dca4b63 | Naphtali Sprei | return ret;
|
1366 | 4dca4b63 | Naphtali Sprei | } |
1367 | 4dca4b63 | Naphtali Sprei | bs->backing_hd = bs_ro; |
1368 | 4dca4b63 | Naphtali Sprei | bs->backing_hd->keep_read_only = 0;
|
1369 | 4dca4b63 | Naphtali Sprei | } |
1370 | 4dca4b63 | Naphtali Sprei | |
1371 | 1d44952f | Christoph Hellwig | return ret;
|
1372 | 33e3963e | bellard | } |
1373 | 33e3963e | bellard | |
1374 | e8877497 | Stefan Hajnoczi | int bdrv_commit_all(void) |
1375 | 6ab4b5ab | Markus Armbruster | { |
1376 | 6ab4b5ab | Markus Armbruster | BlockDriverState *bs; |
1377 | 6ab4b5ab | Markus Armbruster | |
1378 | 6ab4b5ab | Markus Armbruster | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
1379 | e8877497 | Stefan Hajnoczi | int ret = bdrv_commit(bs);
|
1380 | e8877497 | Stefan Hajnoczi | if (ret < 0) { |
1381 | e8877497 | Stefan Hajnoczi | return ret;
|
1382 | e8877497 | Stefan Hajnoczi | } |
1383 | 6ab4b5ab | Markus Armbruster | } |
1384 | e8877497 | Stefan Hajnoczi | return 0; |
1385 | 6ab4b5ab | Markus Armbruster | } |
1386 | 6ab4b5ab | Markus Armbruster | |
1387 | dbffbdcf | Stefan Hajnoczi | struct BdrvTrackedRequest {
|
1388 | dbffbdcf | Stefan Hajnoczi | BlockDriverState *bs; |
1389 | dbffbdcf | Stefan Hajnoczi | int64_t sector_num; |
1390 | dbffbdcf | Stefan Hajnoczi | int nb_sectors;
|
1391 | dbffbdcf | Stefan Hajnoczi | bool is_write;
|
1392 | dbffbdcf | Stefan Hajnoczi | QLIST_ENTRY(BdrvTrackedRequest) list; |
1393 | 5f8b6491 | Stefan Hajnoczi | Coroutine *co; /* owner, used for deadlock detection */
|
1394 | f4658285 | Stefan Hajnoczi | CoQueue wait_queue; /* coroutines blocked on this request */
|
1395 | dbffbdcf | Stefan Hajnoczi | }; |
1396 | dbffbdcf | Stefan Hajnoczi | |
1397 | dbffbdcf | Stefan Hajnoczi | /**
|
1398 | dbffbdcf | Stefan Hajnoczi | * Remove an active request from the tracked requests list
|
1399 | dbffbdcf | Stefan Hajnoczi | *
|
1400 | dbffbdcf | Stefan Hajnoczi | * This function should be called when a tracked request is completing.
|
1401 | dbffbdcf | Stefan Hajnoczi | */
|
1402 | dbffbdcf | Stefan Hajnoczi | static void tracked_request_end(BdrvTrackedRequest *req) |
1403 | dbffbdcf | Stefan Hajnoczi | { |
1404 | dbffbdcf | Stefan Hajnoczi | QLIST_REMOVE(req, list); |
1405 | f4658285 | Stefan Hajnoczi | qemu_co_queue_restart_all(&req->wait_queue); |
1406 | dbffbdcf | Stefan Hajnoczi | } |
1407 | dbffbdcf | Stefan Hajnoczi | |
1408 | dbffbdcf | Stefan Hajnoczi | /**
|
1409 | dbffbdcf | Stefan Hajnoczi | * Add an active request to the tracked requests list
|
1410 | dbffbdcf | Stefan Hajnoczi | */
|
1411 | dbffbdcf | Stefan Hajnoczi | static void tracked_request_begin(BdrvTrackedRequest *req, |
1412 | dbffbdcf | Stefan Hajnoczi | BlockDriverState *bs, |
1413 | dbffbdcf | Stefan Hajnoczi | int64_t sector_num, |
1414 | dbffbdcf | Stefan Hajnoczi | int nb_sectors, bool is_write) |
1415 | dbffbdcf | Stefan Hajnoczi | { |
1416 | dbffbdcf | Stefan Hajnoczi | *req = (BdrvTrackedRequest){ |
1417 | dbffbdcf | Stefan Hajnoczi | .bs = bs, |
1418 | dbffbdcf | Stefan Hajnoczi | .sector_num = sector_num, |
1419 | dbffbdcf | Stefan Hajnoczi | .nb_sectors = nb_sectors, |
1420 | dbffbdcf | Stefan Hajnoczi | .is_write = is_write, |
1421 | 5f8b6491 | Stefan Hajnoczi | .co = qemu_coroutine_self(), |
1422 | dbffbdcf | Stefan Hajnoczi | }; |
1423 | dbffbdcf | Stefan Hajnoczi | |
1424 | f4658285 | Stefan Hajnoczi | qemu_co_queue_init(&req->wait_queue); |
1425 | f4658285 | Stefan Hajnoczi | |
1426 | dbffbdcf | Stefan Hajnoczi | QLIST_INSERT_HEAD(&bs->tracked_requests, req, list); |
1427 | dbffbdcf | Stefan Hajnoczi | } |
1428 | dbffbdcf | Stefan Hajnoczi | |
1429 | d83947ac | Stefan Hajnoczi | /**
|
1430 | d83947ac | Stefan Hajnoczi | * Round a region to cluster boundaries
|
1431 | d83947ac | Stefan Hajnoczi | */
|
1432 | d83947ac | Stefan Hajnoczi | static void round_to_clusters(BlockDriverState *bs, |
1433 | d83947ac | Stefan Hajnoczi | int64_t sector_num, int nb_sectors,
|
1434 | d83947ac | Stefan Hajnoczi | int64_t *cluster_sector_num, |
1435 | d83947ac | Stefan Hajnoczi | int *cluster_nb_sectors)
|
1436 | d83947ac | Stefan Hajnoczi | { |
1437 | d83947ac | Stefan Hajnoczi | BlockDriverInfo bdi; |
1438 | d83947ac | Stefan Hajnoczi | |
1439 | d83947ac | Stefan Hajnoczi | if (bdrv_get_info(bs, &bdi) < 0 || bdi.cluster_size == 0) { |
1440 | d83947ac | Stefan Hajnoczi | *cluster_sector_num = sector_num; |
1441 | d83947ac | Stefan Hajnoczi | *cluster_nb_sectors = nb_sectors; |
1442 | d83947ac | Stefan Hajnoczi | } else {
|
1443 | d83947ac | Stefan Hajnoczi | int64_t c = bdi.cluster_size / BDRV_SECTOR_SIZE; |
1444 | d83947ac | Stefan Hajnoczi | *cluster_sector_num = QEMU_ALIGN_DOWN(sector_num, c); |
1445 | d83947ac | Stefan Hajnoczi | *cluster_nb_sectors = QEMU_ALIGN_UP(sector_num - *cluster_sector_num + |
1446 | d83947ac | Stefan Hajnoczi | nb_sectors, c); |
1447 | d83947ac | Stefan Hajnoczi | } |
1448 | d83947ac | Stefan Hajnoczi | } |
1449 | d83947ac | Stefan Hajnoczi | |
1450 | f4658285 | Stefan Hajnoczi | static bool tracked_request_overlaps(BdrvTrackedRequest *req, |
1451 | f4658285 | Stefan Hajnoczi | int64_t sector_num, int nb_sectors) {
|
1452 | d83947ac | Stefan Hajnoczi | /* aaaa bbbb */
|
1453 | d83947ac | Stefan Hajnoczi | if (sector_num >= req->sector_num + req->nb_sectors) {
|
1454 | d83947ac | Stefan Hajnoczi | return false; |
1455 | d83947ac | Stefan Hajnoczi | } |
1456 | d83947ac | Stefan Hajnoczi | /* bbbb aaaa */
|
1457 | d83947ac | Stefan Hajnoczi | if (req->sector_num >= sector_num + nb_sectors) {
|
1458 | d83947ac | Stefan Hajnoczi | return false; |
1459 | d83947ac | Stefan Hajnoczi | } |
1460 | d83947ac | Stefan Hajnoczi | return true; |
1461 | f4658285 | Stefan Hajnoczi | } |
1462 | f4658285 | Stefan Hajnoczi | |
1463 | f4658285 | Stefan Hajnoczi | static void coroutine_fn wait_for_overlapping_requests(BlockDriverState *bs, |
1464 | f4658285 | Stefan Hajnoczi | int64_t sector_num, int nb_sectors)
|
1465 | f4658285 | Stefan Hajnoczi | { |
1466 | f4658285 | Stefan Hajnoczi | BdrvTrackedRequest *req; |
1467 | d83947ac | Stefan Hajnoczi | int64_t cluster_sector_num; |
1468 | d83947ac | Stefan Hajnoczi | int cluster_nb_sectors;
|
1469 | f4658285 | Stefan Hajnoczi | bool retry;
|
1470 | f4658285 | Stefan Hajnoczi | |
1471 | d83947ac | Stefan Hajnoczi | /* If we touch the same cluster it counts as an overlap. This guarantees
|
1472 | d83947ac | Stefan Hajnoczi | * that allocating writes will be serialized and not race with each other
|
1473 | d83947ac | Stefan Hajnoczi | * for the same cluster. For example, in copy-on-read it ensures that the
|
1474 | d83947ac | Stefan Hajnoczi | * CoR read and write operations are atomic and guest writes cannot
|
1475 | d83947ac | Stefan Hajnoczi | * interleave between them.
|
1476 | d83947ac | Stefan Hajnoczi | */
|
1477 | d83947ac | Stefan Hajnoczi | round_to_clusters(bs, sector_num, nb_sectors, |
1478 | d83947ac | Stefan Hajnoczi | &cluster_sector_num, &cluster_nb_sectors); |
1479 | d83947ac | Stefan Hajnoczi | |
1480 | f4658285 | Stefan Hajnoczi | do {
|
1481 | f4658285 | Stefan Hajnoczi | retry = false;
|
1482 | f4658285 | Stefan Hajnoczi | QLIST_FOREACH(req, &bs->tracked_requests, list) { |
1483 | d83947ac | Stefan Hajnoczi | if (tracked_request_overlaps(req, cluster_sector_num,
|
1484 | d83947ac | Stefan Hajnoczi | cluster_nb_sectors)) { |
1485 | 5f8b6491 | Stefan Hajnoczi | /* Hitting this means there was a reentrant request, for
|
1486 | 5f8b6491 | Stefan Hajnoczi | * example, a block driver issuing nested requests. This must
|
1487 | 5f8b6491 | Stefan Hajnoczi | * never happen since it means deadlock.
|
1488 | 5f8b6491 | Stefan Hajnoczi | */
|
1489 | 5f8b6491 | Stefan Hajnoczi | assert(qemu_coroutine_self() != req->co); |
1490 | 5f8b6491 | Stefan Hajnoczi | |
1491 | f4658285 | Stefan Hajnoczi | qemu_co_queue_wait(&req->wait_queue); |
1492 | f4658285 | Stefan Hajnoczi | retry = true;
|
1493 | f4658285 | Stefan Hajnoczi | break;
|
1494 | f4658285 | Stefan Hajnoczi | } |
1495 | f4658285 | Stefan Hajnoczi | } |
1496 | f4658285 | Stefan Hajnoczi | } while (retry);
|
1497 | f4658285 | Stefan Hajnoczi | } |
1498 | f4658285 | Stefan Hajnoczi | |
1499 | 756e6736 | Kevin Wolf | /*
|
1500 | 756e6736 | Kevin Wolf | * Return values:
|
1501 | 756e6736 | Kevin Wolf | * 0 - success
|
1502 | 756e6736 | Kevin Wolf | * -EINVAL - backing format specified, but no file
|
1503 | 756e6736 | Kevin Wolf | * -ENOSPC - can't update the backing file because no space is left in the
|
1504 | 756e6736 | Kevin Wolf | * image file header
|
1505 | 756e6736 | Kevin Wolf | * -ENOTSUP - format driver doesn't support changing the backing file
|
1506 | 756e6736 | Kevin Wolf | */
|
1507 | 756e6736 | Kevin Wolf | int bdrv_change_backing_file(BlockDriverState *bs,
|
1508 | 756e6736 | Kevin Wolf | const char *backing_file, const char *backing_fmt) |
1509 | 756e6736 | Kevin Wolf | { |
1510 | 756e6736 | Kevin Wolf | BlockDriver *drv = bs->drv; |
1511 | 469ef350 | Paolo Bonzini | int ret;
|
1512 | 756e6736 | Kevin Wolf | |
1513 | 5f377794 | Paolo Bonzini | /* Backing file format doesn't make sense without a backing file */
|
1514 | 5f377794 | Paolo Bonzini | if (backing_fmt && !backing_file) {
|
1515 | 5f377794 | Paolo Bonzini | return -EINVAL;
|
1516 | 5f377794 | Paolo Bonzini | } |
1517 | 5f377794 | Paolo Bonzini | |
1518 | 756e6736 | Kevin Wolf | if (drv->bdrv_change_backing_file != NULL) { |
1519 | 469ef350 | Paolo Bonzini | ret = drv->bdrv_change_backing_file(bs, backing_file, backing_fmt); |
1520 | 756e6736 | Kevin Wolf | } else {
|
1521 | 469ef350 | Paolo Bonzini | ret = -ENOTSUP; |
1522 | 756e6736 | Kevin Wolf | } |
1523 | 469ef350 | Paolo Bonzini | |
1524 | 469ef350 | Paolo Bonzini | if (ret == 0) { |
1525 | 469ef350 | Paolo Bonzini | pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: ""); |
1526 | 469ef350 | Paolo Bonzini | pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: ""); |
1527 | 469ef350 | Paolo Bonzini | } |
1528 | 469ef350 | Paolo Bonzini | return ret;
|
1529 | 756e6736 | Kevin Wolf | } |
1530 | 756e6736 | Kevin Wolf | |
1531 | 71d0770c | aliguori | static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset, |
1532 | 71d0770c | aliguori | size_t size) |
1533 | 71d0770c | aliguori | { |
1534 | 71d0770c | aliguori | int64_t len; |
1535 | 71d0770c | aliguori | |
1536 | 71d0770c | aliguori | if (!bdrv_is_inserted(bs))
|
1537 | 71d0770c | aliguori | return -ENOMEDIUM;
|
1538 | 71d0770c | aliguori | |
1539 | 71d0770c | aliguori | if (bs->growable)
|
1540 | 71d0770c | aliguori | return 0; |
1541 | 71d0770c | aliguori | |
1542 | 71d0770c | aliguori | len = bdrv_getlength(bs); |
1543 | 71d0770c | aliguori | |
1544 | fbb7b4e0 | Kevin Wolf | if (offset < 0) |
1545 | fbb7b4e0 | Kevin Wolf | return -EIO;
|
1546 | fbb7b4e0 | Kevin Wolf | |
1547 | fbb7b4e0 | Kevin Wolf | if ((offset > len) || (len - offset < size))
|
1548 | 71d0770c | aliguori | return -EIO;
|
1549 | 71d0770c | aliguori | |
1550 | 71d0770c | aliguori | return 0; |
1551 | 71d0770c | aliguori | } |
1552 | 71d0770c | aliguori | |
1553 | 71d0770c | aliguori | static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num, |
1554 | 71d0770c | aliguori | int nb_sectors)
|
1555 | 71d0770c | aliguori | { |
1556 | eb5a3165 | Jes Sorensen | return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE,
|
1557 | eb5a3165 | Jes Sorensen | nb_sectors * BDRV_SECTOR_SIZE); |
1558 | 71d0770c | aliguori | } |
1559 | 71d0770c | aliguori | |
1560 | 1c9805a3 | Stefan Hajnoczi | typedef struct RwCo { |
1561 | 1c9805a3 | Stefan Hajnoczi | BlockDriverState *bs; |
1562 | 1c9805a3 | Stefan Hajnoczi | int64_t sector_num; |
1563 | 1c9805a3 | Stefan Hajnoczi | int nb_sectors;
|
1564 | 1c9805a3 | Stefan Hajnoczi | QEMUIOVector *qiov; |
1565 | 1c9805a3 | Stefan Hajnoczi | bool is_write;
|
1566 | 1c9805a3 | Stefan Hajnoczi | int ret;
|
1567 | 1c9805a3 | Stefan Hajnoczi | } RwCo; |
1568 | 1c9805a3 | Stefan Hajnoczi | |
1569 | 1c9805a3 | Stefan Hajnoczi | static void coroutine_fn bdrv_rw_co_entry(void *opaque) |
1570 | fc01f7e7 | bellard | { |
1571 | 1c9805a3 | Stefan Hajnoczi | RwCo *rwco = opaque; |
1572 | ea2384d3 | bellard | |
1573 | 1c9805a3 | Stefan Hajnoczi | if (!rwco->is_write) {
|
1574 | 1c9805a3 | Stefan Hajnoczi | rwco->ret = bdrv_co_do_readv(rwco->bs, rwco->sector_num, |
1575 | 470c0504 | Stefan Hajnoczi | rwco->nb_sectors, rwco->qiov, 0);
|
1576 | 1c9805a3 | Stefan Hajnoczi | } else {
|
1577 | 1c9805a3 | Stefan Hajnoczi | rwco->ret = bdrv_co_do_writev(rwco->bs, rwco->sector_num, |
1578 | f08f2dda | Stefan Hajnoczi | rwco->nb_sectors, rwco->qiov, 0);
|
1579 | 1c9805a3 | Stefan Hajnoczi | } |
1580 | 1c9805a3 | Stefan Hajnoczi | } |
1581 | e7a8a783 | Kevin Wolf | |
1582 | 1c9805a3 | Stefan Hajnoczi | /*
|
1583 | 1c9805a3 | Stefan Hajnoczi | * Process a synchronous request using coroutines
|
1584 | 1c9805a3 | Stefan Hajnoczi | */
|
1585 | 1c9805a3 | Stefan Hajnoczi | static int bdrv_rw_co(BlockDriverState *bs, int64_t sector_num, uint8_t *buf, |
1586 | 1c9805a3 | Stefan Hajnoczi | int nb_sectors, bool is_write) |
1587 | 1c9805a3 | Stefan Hajnoczi | { |
1588 | 1c9805a3 | Stefan Hajnoczi | QEMUIOVector qiov; |
1589 | 1c9805a3 | Stefan Hajnoczi | struct iovec iov = {
|
1590 | 1c9805a3 | Stefan Hajnoczi | .iov_base = (void *)buf,
|
1591 | 1c9805a3 | Stefan Hajnoczi | .iov_len = nb_sectors * BDRV_SECTOR_SIZE, |
1592 | 1c9805a3 | Stefan Hajnoczi | }; |
1593 | 1c9805a3 | Stefan Hajnoczi | Coroutine *co; |
1594 | 1c9805a3 | Stefan Hajnoczi | RwCo rwco = { |
1595 | 1c9805a3 | Stefan Hajnoczi | .bs = bs, |
1596 | 1c9805a3 | Stefan Hajnoczi | .sector_num = sector_num, |
1597 | 1c9805a3 | Stefan Hajnoczi | .nb_sectors = nb_sectors, |
1598 | 1c9805a3 | Stefan Hajnoczi | .qiov = &qiov, |
1599 | 1c9805a3 | Stefan Hajnoczi | .is_write = is_write, |
1600 | 1c9805a3 | Stefan Hajnoczi | .ret = NOT_DONE, |
1601 | 1c9805a3 | Stefan Hajnoczi | }; |
1602 | e7a8a783 | Kevin Wolf | |
1603 | 1c9805a3 | Stefan Hajnoczi | qemu_iovec_init_external(&qiov, &iov, 1);
|
1604 | e7a8a783 | Kevin Wolf | |
1605 | 498e386c | Zhi Yong Wu | /**
|
1606 | 498e386c | Zhi Yong Wu | * In sync call context, when the vcpu is blocked, this throttling timer
|
1607 | 498e386c | Zhi Yong Wu | * will not fire; so the I/O throttling function has to be disabled here
|
1608 | 498e386c | Zhi Yong Wu | * if it has been enabled.
|
1609 | 498e386c | Zhi Yong Wu | */
|
1610 | 498e386c | Zhi Yong Wu | if (bs->io_limits_enabled) {
|
1611 | 498e386c | Zhi Yong Wu | fprintf(stderr, "Disabling I/O throttling on '%s' due "
|
1612 | 498e386c | Zhi Yong Wu | "to synchronous I/O.\n", bdrv_get_device_name(bs));
|
1613 | 498e386c | Zhi Yong Wu | bdrv_io_limits_disable(bs); |
1614 | 498e386c | Zhi Yong Wu | } |
1615 | 498e386c | Zhi Yong Wu | |
1616 | 1c9805a3 | Stefan Hajnoczi | if (qemu_in_coroutine()) {
|
1617 | 1c9805a3 | Stefan Hajnoczi | /* Fast-path if already in coroutine context */
|
1618 | 1c9805a3 | Stefan Hajnoczi | bdrv_rw_co_entry(&rwco); |
1619 | 1c9805a3 | Stefan Hajnoczi | } else {
|
1620 | 1c9805a3 | Stefan Hajnoczi | co = qemu_coroutine_create(bdrv_rw_co_entry); |
1621 | 1c9805a3 | Stefan Hajnoczi | qemu_coroutine_enter(co, &rwco); |
1622 | 1c9805a3 | Stefan Hajnoczi | while (rwco.ret == NOT_DONE) {
|
1623 | 1c9805a3 | Stefan Hajnoczi | qemu_aio_wait(); |
1624 | 1c9805a3 | Stefan Hajnoczi | } |
1625 | 1c9805a3 | Stefan Hajnoczi | } |
1626 | 1c9805a3 | Stefan Hajnoczi | return rwco.ret;
|
1627 | 1c9805a3 | Stefan Hajnoczi | } |
1628 | b338082b | bellard | |
1629 | 1c9805a3 | Stefan Hajnoczi | /* return < 0 if error. See bdrv_write() for the return codes */
|
1630 | 1c9805a3 | Stefan Hajnoczi | int bdrv_read(BlockDriverState *bs, int64_t sector_num,
|
1631 | 1c9805a3 | Stefan Hajnoczi | uint8_t *buf, int nb_sectors)
|
1632 | 1c9805a3 | Stefan Hajnoczi | { |
1633 | 1c9805a3 | Stefan Hajnoczi | return bdrv_rw_co(bs, sector_num, buf, nb_sectors, false); |
1634 | fc01f7e7 | bellard | } |
1635 | fc01f7e7 | bellard | |
1636 | 07d27a44 | Markus Armbruster | /* Just like bdrv_read(), but with I/O throttling temporarily disabled */
|
1637 | 07d27a44 | Markus Armbruster | int bdrv_read_unthrottled(BlockDriverState *bs, int64_t sector_num,
|
1638 | 07d27a44 | Markus Armbruster | uint8_t *buf, int nb_sectors)
|
1639 | 07d27a44 | Markus Armbruster | { |
1640 | 07d27a44 | Markus Armbruster | bool enabled;
|
1641 | 07d27a44 | Markus Armbruster | int ret;
|
1642 | 07d27a44 | Markus Armbruster | |
1643 | 07d27a44 | Markus Armbruster | enabled = bs->io_limits_enabled; |
1644 | 07d27a44 | Markus Armbruster | bs->io_limits_enabled = false;
|
1645 | 07d27a44 | Markus Armbruster | ret = bdrv_read(bs, 0, buf, 1); |
1646 | 07d27a44 | Markus Armbruster | bs->io_limits_enabled = enabled; |
1647 | 07d27a44 | Markus Armbruster | return ret;
|
1648 | 07d27a44 | Markus Armbruster | } |
1649 | 07d27a44 | Markus Armbruster | |
1650 | 71df14fc | Paolo Bonzini | #define BITS_PER_LONG (sizeof(unsigned long) * 8) |
1651 | 71df14fc | Paolo Bonzini | |
1652 | 7cd1e32a | lirans@il.ibm.com | static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num, |
1653 | a55eb92c | Jan Kiszka | int nb_sectors, int dirty) |
1654 | 7cd1e32a | lirans@il.ibm.com | { |
1655 | 7cd1e32a | lirans@il.ibm.com | int64_t start, end; |
1656 | c6d22830 | Jan Kiszka | unsigned long val, idx, bit; |
1657 | a55eb92c | Jan Kiszka | |
1658 | 6ea44308 | Jan Kiszka | start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK; |
1659 | c6d22830 | Jan Kiszka | end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
|
1660 | a55eb92c | Jan Kiszka | |
1661 | a55eb92c | Jan Kiszka | for (; start <= end; start++) {
|
1662 | 71df14fc | Paolo Bonzini | idx = start / BITS_PER_LONG; |
1663 | 71df14fc | Paolo Bonzini | bit = start % BITS_PER_LONG; |
1664 | c6d22830 | Jan Kiszka | val = bs->dirty_bitmap[idx]; |
1665 | c6d22830 | Jan Kiszka | if (dirty) {
|
1666 | 6d59fec1 | Marcelo Tosatti | if (!(val & (1UL << bit))) { |
1667 | aaa0eb75 | Liran Schour | bs->dirty_count++; |
1668 | 6d59fec1 | Marcelo Tosatti | val |= 1UL << bit;
|
1669 | aaa0eb75 | Liran Schour | } |
1670 | c6d22830 | Jan Kiszka | } else {
|
1671 | 6d59fec1 | Marcelo Tosatti | if (val & (1UL << bit)) { |
1672 | aaa0eb75 | Liran Schour | bs->dirty_count--; |
1673 | 6d59fec1 | Marcelo Tosatti | val &= ~(1UL << bit);
|
1674 | aaa0eb75 | Liran Schour | } |
1675 | c6d22830 | Jan Kiszka | } |
1676 | c6d22830 | Jan Kiszka | bs->dirty_bitmap[idx] = val; |
1677 | 7cd1e32a | lirans@il.ibm.com | } |
1678 | 7cd1e32a | lirans@il.ibm.com | } |
1679 | 7cd1e32a | lirans@il.ibm.com | |
1680 | 5fafdf24 | ths | /* Return < 0 if error. Important errors are:
|
1681 | 19cb3738 | bellard | -EIO generic I/O error (may happen for all errors)
|
1682 | 19cb3738 | bellard | -ENOMEDIUM No media inserted.
|
1683 | 19cb3738 | bellard | -EINVAL Invalid sector number or nb_sectors
|
1684 | 19cb3738 | bellard | -EACCES Trying to write a read-only device
|
1685 | 19cb3738 | bellard | */
|
1686 | 5fafdf24 | ths | int bdrv_write(BlockDriverState *bs, int64_t sector_num,
|
1687 | fc01f7e7 | bellard | const uint8_t *buf, int nb_sectors) |
1688 | fc01f7e7 | bellard | { |
1689 | 1c9805a3 | Stefan Hajnoczi | return bdrv_rw_co(bs, sector_num, (uint8_t *)buf, nb_sectors, true); |
1690 | 83f64091 | bellard | } |
1691 | 83f64091 | bellard | |
1692 | eda578e5 | aliguori | int bdrv_pread(BlockDriverState *bs, int64_t offset,
|
1693 | eda578e5 | aliguori | void *buf, int count1) |
1694 | 83f64091 | bellard | { |
1695 | 6ea44308 | Jan Kiszka | uint8_t tmp_buf[BDRV_SECTOR_SIZE]; |
1696 | 83f64091 | bellard | int len, nb_sectors, count;
|
1697 | 83f64091 | bellard | int64_t sector_num; |
1698 | 9a8c4cce | Kevin Wolf | int ret;
|
1699 | 83f64091 | bellard | |
1700 | 83f64091 | bellard | count = count1; |
1701 | 83f64091 | bellard | /* first read to align to sector start */
|
1702 | 6ea44308 | Jan Kiszka | len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
|
1703 | 83f64091 | bellard | if (len > count)
|
1704 | 83f64091 | bellard | len = count; |
1705 | 6ea44308 | Jan Kiszka | sector_num = offset >> BDRV_SECTOR_BITS; |
1706 | 83f64091 | bellard | if (len > 0) { |
1707 | 9a8c4cce | Kevin Wolf | if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) |
1708 | 9a8c4cce | Kevin Wolf | return ret;
|
1709 | 6ea44308 | Jan Kiszka | memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len);
|
1710 | 83f64091 | bellard | count -= len; |
1711 | 83f64091 | bellard | if (count == 0) |
1712 | 83f64091 | bellard | return count1;
|
1713 | 83f64091 | bellard | sector_num++; |
1714 | 83f64091 | bellard | buf += len; |
1715 | 83f64091 | bellard | } |
1716 | 83f64091 | bellard | |
1717 | 83f64091 | bellard | /* read the sectors "in place" */
|
1718 | 6ea44308 | Jan Kiszka | nb_sectors = count >> BDRV_SECTOR_BITS; |
1719 | 83f64091 | bellard | if (nb_sectors > 0) { |
1720 | 9a8c4cce | Kevin Wolf | if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0) |
1721 | 9a8c4cce | Kevin Wolf | return ret;
|
1722 | 83f64091 | bellard | sector_num += nb_sectors; |
1723 | 6ea44308 | Jan Kiszka | len = nb_sectors << BDRV_SECTOR_BITS; |
1724 | 83f64091 | bellard | buf += len; |
1725 | 83f64091 | bellard | count -= len; |
1726 | 83f64091 | bellard | } |
1727 | 83f64091 | bellard | |
1728 | 83f64091 | bellard | /* add data from the last sector */
|
1729 | 83f64091 | bellard | if (count > 0) { |
1730 | 9a8c4cce | Kevin Wolf | if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) |
1731 | 9a8c4cce | Kevin Wolf | return ret;
|
1732 | 83f64091 | bellard | memcpy(buf, tmp_buf, count); |
1733 | 83f64091 | bellard | } |
1734 | 83f64091 | bellard | return count1;
|
1735 | 83f64091 | bellard | } |
1736 | 83f64091 | bellard | |
1737 | eda578e5 | aliguori | int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
|
1738 | eda578e5 | aliguori | const void *buf, int count1) |
1739 | 83f64091 | bellard | { |
1740 | 6ea44308 | Jan Kiszka | uint8_t tmp_buf[BDRV_SECTOR_SIZE]; |
1741 | 83f64091 | bellard | int len, nb_sectors, count;
|
1742 | 83f64091 | bellard | int64_t sector_num; |
1743 | 9a8c4cce | Kevin Wolf | int ret;
|
1744 | 83f64091 | bellard | |
1745 | 83f64091 | bellard | count = count1; |
1746 | 83f64091 | bellard | /* first write to align to sector start */
|
1747 | 6ea44308 | Jan Kiszka | len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
|
1748 | 83f64091 | bellard | if (len > count)
|
1749 | 83f64091 | bellard | len = count; |
1750 | 6ea44308 | Jan Kiszka | sector_num = offset >> BDRV_SECTOR_BITS; |
1751 | 83f64091 | bellard | if (len > 0) { |
1752 | 9a8c4cce | Kevin Wolf | if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) |
1753 | 9a8c4cce | Kevin Wolf | return ret;
|
1754 | 6ea44308 | Jan Kiszka | memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len);
|
1755 | 9a8c4cce | Kevin Wolf | if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) |
1756 | 9a8c4cce | Kevin Wolf | return ret;
|
1757 | 83f64091 | bellard | count -= len; |
1758 | 83f64091 | bellard | if (count == 0) |
1759 | 83f64091 | bellard | return count1;
|
1760 | 83f64091 | bellard | sector_num++; |
1761 | 83f64091 | bellard | buf += len; |
1762 | 83f64091 | bellard | } |
1763 | 83f64091 | bellard | |
1764 | 83f64091 | bellard | /* write the sectors "in place" */
|
1765 | 6ea44308 | Jan Kiszka | nb_sectors = count >> BDRV_SECTOR_BITS; |
1766 | 83f64091 | bellard | if (nb_sectors > 0) { |
1767 | 9a8c4cce | Kevin Wolf | if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0) |
1768 | 9a8c4cce | Kevin Wolf | return ret;
|
1769 | 83f64091 | bellard | sector_num += nb_sectors; |
1770 | 6ea44308 | Jan Kiszka | len = nb_sectors << BDRV_SECTOR_BITS; |
1771 | 83f64091 | bellard | buf += len; |
1772 | 83f64091 | bellard | count -= len; |
1773 | 83f64091 | bellard | } |
1774 | 83f64091 | bellard | |
1775 | 83f64091 | bellard | /* add data from the last sector */
|
1776 | 83f64091 | bellard | if (count > 0) { |
1777 | 9a8c4cce | Kevin Wolf | if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) |
1778 | 9a8c4cce | Kevin Wolf | return ret;
|
1779 | 83f64091 | bellard | memcpy(tmp_buf, buf, count); |
1780 | 9a8c4cce | Kevin Wolf | if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) |
1781 | 9a8c4cce | Kevin Wolf | return ret;
|
1782 | 83f64091 | bellard | } |
1783 | 83f64091 | bellard | return count1;
|
1784 | 83f64091 | bellard | } |
1785 | 83f64091 | bellard | |
1786 | f08145fe | Kevin Wolf | /*
|
1787 | f08145fe | Kevin Wolf | * Writes to the file and ensures that no writes are reordered across this
|
1788 | f08145fe | Kevin Wolf | * request (acts as a barrier)
|
1789 | f08145fe | Kevin Wolf | *
|
1790 | f08145fe | Kevin Wolf | * Returns 0 on success, -errno in error cases.
|
1791 | f08145fe | Kevin Wolf | */
|
1792 | f08145fe | Kevin Wolf | int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset,
|
1793 | f08145fe | Kevin Wolf | const void *buf, int count) |
1794 | f08145fe | Kevin Wolf | { |
1795 | f08145fe | Kevin Wolf | int ret;
|
1796 | f08145fe | Kevin Wolf | |
1797 | f08145fe | Kevin Wolf | ret = bdrv_pwrite(bs, offset, buf, count); |
1798 | f08145fe | Kevin Wolf | if (ret < 0) { |
1799 | f08145fe | Kevin Wolf | return ret;
|
1800 | f08145fe | Kevin Wolf | } |
1801 | f08145fe | Kevin Wolf | |
1802 | f05fa4ad | Paolo Bonzini | /* No flush needed for cache modes that already do it */
|
1803 | f05fa4ad | Paolo Bonzini | if (bs->enable_write_cache) {
|
1804 | f08145fe | Kevin Wolf | bdrv_flush(bs); |
1805 | f08145fe | Kevin Wolf | } |
1806 | f08145fe | Kevin Wolf | |
1807 | f08145fe | Kevin Wolf | return 0; |
1808 | f08145fe | Kevin Wolf | } |
1809 | f08145fe | Kevin Wolf | |
1810 | 470c0504 | Stefan Hajnoczi | static int coroutine_fn bdrv_co_do_copy_on_readv(BlockDriverState *bs, |
1811 | ab185921 | Stefan Hajnoczi | int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
|
1812 | ab185921 | Stefan Hajnoczi | { |
1813 | ab185921 | Stefan Hajnoczi | /* Perform I/O through a temporary buffer so that users who scribble over
|
1814 | ab185921 | Stefan Hajnoczi | * their read buffer while the operation is in progress do not end up
|
1815 | ab185921 | Stefan Hajnoczi | * modifying the image file. This is critical for zero-copy guest I/O
|
1816 | ab185921 | Stefan Hajnoczi | * where anything might happen inside guest memory.
|
1817 | ab185921 | Stefan Hajnoczi | */
|
1818 | ab185921 | Stefan Hajnoczi | void *bounce_buffer;
|
1819 | ab185921 | Stefan Hajnoczi | |
1820 | 79c053bd | Stefan Hajnoczi | BlockDriver *drv = bs->drv; |
1821 | ab185921 | Stefan Hajnoczi | struct iovec iov;
|
1822 | ab185921 | Stefan Hajnoczi | QEMUIOVector bounce_qiov; |
1823 | ab185921 | Stefan Hajnoczi | int64_t cluster_sector_num; |
1824 | ab185921 | Stefan Hajnoczi | int cluster_nb_sectors;
|
1825 | ab185921 | Stefan Hajnoczi | size_t skip_bytes; |
1826 | ab185921 | Stefan Hajnoczi | int ret;
|
1827 | ab185921 | Stefan Hajnoczi | |
1828 | ab185921 | Stefan Hajnoczi | /* Cover entire cluster so no additional backing file I/O is required when
|
1829 | ab185921 | Stefan Hajnoczi | * allocating cluster in the image file.
|
1830 | ab185921 | Stefan Hajnoczi | */
|
1831 | ab185921 | Stefan Hajnoczi | round_to_clusters(bs, sector_num, nb_sectors, |
1832 | ab185921 | Stefan Hajnoczi | &cluster_sector_num, &cluster_nb_sectors); |
1833 | ab185921 | Stefan Hajnoczi | |
1834 | 470c0504 | Stefan Hajnoczi | trace_bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors, |
1835 | 470c0504 | Stefan Hajnoczi | cluster_sector_num, cluster_nb_sectors); |
1836 | ab185921 | Stefan Hajnoczi | |
1837 | ab185921 | Stefan Hajnoczi | iov.iov_len = cluster_nb_sectors * BDRV_SECTOR_SIZE; |
1838 | ab185921 | Stefan Hajnoczi | iov.iov_base = bounce_buffer = qemu_blockalign(bs, iov.iov_len); |
1839 | ab185921 | Stefan Hajnoczi | qemu_iovec_init_external(&bounce_qiov, &iov, 1);
|
1840 | ab185921 | Stefan Hajnoczi | |
1841 | 79c053bd | Stefan Hajnoczi | ret = drv->bdrv_co_readv(bs, cluster_sector_num, cluster_nb_sectors, |
1842 | 79c053bd | Stefan Hajnoczi | &bounce_qiov); |
1843 | ab185921 | Stefan Hajnoczi | if (ret < 0) { |
1844 | ab185921 | Stefan Hajnoczi | goto err;
|
1845 | ab185921 | Stefan Hajnoczi | } |
1846 | ab185921 | Stefan Hajnoczi | |
1847 | 79c053bd | Stefan Hajnoczi | if (drv->bdrv_co_write_zeroes &&
|
1848 | 79c053bd | Stefan Hajnoczi | buffer_is_zero(bounce_buffer, iov.iov_len)) { |
1849 | 621f0589 | Kevin Wolf | ret = bdrv_co_do_write_zeroes(bs, cluster_sector_num, |
1850 | 621f0589 | Kevin Wolf | cluster_nb_sectors); |
1851 | 79c053bd | Stefan Hajnoczi | } else {
|
1852 | f05fa4ad | Paolo Bonzini | /* This does not change the data on the disk, it is not necessary
|
1853 | f05fa4ad | Paolo Bonzini | * to flush even in cache=writethrough mode.
|
1854 | f05fa4ad | Paolo Bonzini | */
|
1855 | 79c053bd | Stefan Hajnoczi | ret = drv->bdrv_co_writev(bs, cluster_sector_num, cluster_nb_sectors, |
1856 | ab185921 | Stefan Hajnoczi | &bounce_qiov); |
1857 | 79c053bd | Stefan Hajnoczi | } |
1858 | 79c053bd | Stefan Hajnoczi | |
1859 | ab185921 | Stefan Hajnoczi | if (ret < 0) { |
1860 | ab185921 | Stefan Hajnoczi | /* It might be okay to ignore write errors for guest requests. If this
|
1861 | ab185921 | Stefan Hajnoczi | * is a deliberate copy-on-read then we don't want to ignore the error.
|
1862 | ab185921 | Stefan Hajnoczi | * Simply report it in all cases.
|
1863 | ab185921 | Stefan Hajnoczi | */
|
1864 | ab185921 | Stefan Hajnoczi | goto err;
|
1865 | ab185921 | Stefan Hajnoczi | } |
1866 | ab185921 | Stefan Hajnoczi | |
1867 | ab185921 | Stefan Hajnoczi | skip_bytes = (sector_num - cluster_sector_num) * BDRV_SECTOR_SIZE; |
1868 | 03396148 | Michael Tokarev | qemu_iovec_from_buf(qiov, 0, bounce_buffer + skip_bytes,
|
1869 | 03396148 | Michael Tokarev | nb_sectors * BDRV_SECTOR_SIZE); |
1870 | ab185921 | Stefan Hajnoczi | |
1871 | ab185921 | Stefan Hajnoczi | err:
|
1872 | ab185921 | Stefan Hajnoczi | qemu_vfree(bounce_buffer); |
1873 | ab185921 | Stefan Hajnoczi | return ret;
|
1874 | ab185921 | Stefan Hajnoczi | } |
1875 | ab185921 | Stefan Hajnoczi | |
1876 | c5fbe571 | Stefan Hajnoczi | /*
|
1877 | c5fbe571 | Stefan Hajnoczi | * Handle a read request in coroutine context
|
1878 | c5fbe571 | Stefan Hajnoczi | */
|
1879 | c5fbe571 | Stefan Hajnoczi | static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs, |
1880 | 470c0504 | Stefan Hajnoczi | int64_t sector_num, int nb_sectors, QEMUIOVector *qiov,
|
1881 | 470c0504 | Stefan Hajnoczi | BdrvRequestFlags flags) |
1882 | da1fa91d | Kevin Wolf | { |
1883 | da1fa91d | Kevin Wolf | BlockDriver *drv = bs->drv; |
1884 | dbffbdcf | Stefan Hajnoczi | BdrvTrackedRequest req; |
1885 | dbffbdcf | Stefan Hajnoczi | int ret;
|
1886 | da1fa91d | Kevin Wolf | |
1887 | da1fa91d | Kevin Wolf | if (!drv) {
|
1888 | da1fa91d | Kevin Wolf | return -ENOMEDIUM;
|
1889 | da1fa91d | Kevin Wolf | } |
1890 | da1fa91d | Kevin Wolf | if (bdrv_check_request(bs, sector_num, nb_sectors)) {
|
1891 | da1fa91d | Kevin Wolf | return -EIO;
|
1892 | da1fa91d | Kevin Wolf | } |
1893 | da1fa91d | Kevin Wolf | |
1894 | 98f90dba | Zhi Yong Wu | /* throttling disk read I/O */
|
1895 | 98f90dba | Zhi Yong Wu | if (bs->io_limits_enabled) {
|
1896 | 98f90dba | Zhi Yong Wu | bdrv_io_limits_intercept(bs, false, nb_sectors);
|
1897 | 98f90dba | Zhi Yong Wu | } |
1898 | 98f90dba | Zhi Yong Wu | |
1899 | f4658285 | Stefan Hajnoczi | if (bs->copy_on_read) {
|
1900 | 470c0504 | Stefan Hajnoczi | flags |= BDRV_REQ_COPY_ON_READ; |
1901 | 470c0504 | Stefan Hajnoczi | } |
1902 | 470c0504 | Stefan Hajnoczi | if (flags & BDRV_REQ_COPY_ON_READ) {
|
1903 | 470c0504 | Stefan Hajnoczi | bs->copy_on_read_in_flight++; |
1904 | 470c0504 | Stefan Hajnoczi | } |
1905 | 470c0504 | Stefan Hajnoczi | |
1906 | 470c0504 | Stefan Hajnoczi | if (bs->copy_on_read_in_flight) {
|
1907 | f4658285 | Stefan Hajnoczi | wait_for_overlapping_requests(bs, sector_num, nb_sectors); |
1908 | f4658285 | Stefan Hajnoczi | } |
1909 | f4658285 | Stefan Hajnoczi | |
1910 | dbffbdcf | Stefan Hajnoczi | tracked_request_begin(&req, bs, sector_num, nb_sectors, false);
|
1911 | ab185921 | Stefan Hajnoczi | |
1912 | 470c0504 | Stefan Hajnoczi | if (flags & BDRV_REQ_COPY_ON_READ) {
|
1913 | ab185921 | Stefan Hajnoczi | int pnum;
|
1914 | ab185921 | Stefan Hajnoczi | |
1915 | ab185921 | Stefan Hajnoczi | ret = bdrv_co_is_allocated(bs, sector_num, nb_sectors, &pnum); |
1916 | ab185921 | Stefan Hajnoczi | if (ret < 0) { |
1917 | ab185921 | Stefan Hajnoczi | goto out;
|
1918 | ab185921 | Stefan Hajnoczi | } |
1919 | ab185921 | Stefan Hajnoczi | |
1920 | ab185921 | Stefan Hajnoczi | if (!ret || pnum != nb_sectors) {
|
1921 | 470c0504 | Stefan Hajnoczi | ret = bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors, qiov); |
1922 | ab185921 | Stefan Hajnoczi | goto out;
|
1923 | ab185921 | Stefan Hajnoczi | } |
1924 | ab185921 | Stefan Hajnoczi | } |
1925 | ab185921 | Stefan Hajnoczi | |
1926 | dbffbdcf | Stefan Hajnoczi | ret = drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov); |
1927 | ab185921 | Stefan Hajnoczi | |
1928 | ab185921 | Stefan Hajnoczi | out:
|
1929 | dbffbdcf | Stefan Hajnoczi | tracked_request_end(&req); |
1930 | 470c0504 | Stefan Hajnoczi | |
1931 | 470c0504 | Stefan Hajnoczi | if (flags & BDRV_REQ_COPY_ON_READ) {
|
1932 | 470c0504 | Stefan Hajnoczi | bs->copy_on_read_in_flight--; |
1933 | 470c0504 | Stefan Hajnoczi | } |
1934 | 470c0504 | Stefan Hajnoczi | |
1935 | dbffbdcf | Stefan Hajnoczi | return ret;
|
1936 | da1fa91d | Kevin Wolf | } |
1937 | da1fa91d | Kevin Wolf | |
1938 | c5fbe571 | Stefan Hajnoczi | int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num,
|
1939 | da1fa91d | Kevin Wolf | int nb_sectors, QEMUIOVector *qiov)
|
1940 | da1fa91d | Kevin Wolf | { |
1941 | c5fbe571 | Stefan Hajnoczi | trace_bdrv_co_readv(bs, sector_num, nb_sectors); |
1942 | da1fa91d | Kevin Wolf | |
1943 | 470c0504 | Stefan Hajnoczi | return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov, 0); |
1944 | 470c0504 | Stefan Hajnoczi | } |
1945 | 470c0504 | Stefan Hajnoczi | |
1946 | 470c0504 | Stefan Hajnoczi | int coroutine_fn bdrv_co_copy_on_readv(BlockDriverState *bs,
|
1947 | 470c0504 | Stefan Hajnoczi | int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
|
1948 | 470c0504 | Stefan Hajnoczi | { |
1949 | 470c0504 | Stefan Hajnoczi | trace_bdrv_co_copy_on_readv(bs, sector_num, nb_sectors); |
1950 | 470c0504 | Stefan Hajnoczi | |
1951 | 470c0504 | Stefan Hajnoczi | return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov,
|
1952 | 470c0504 | Stefan Hajnoczi | BDRV_REQ_COPY_ON_READ); |
1953 | c5fbe571 | Stefan Hajnoczi | } |
1954 | c5fbe571 | Stefan Hajnoczi | |
1955 | f08f2dda | Stefan Hajnoczi | static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs, |
1956 | f08f2dda | Stefan Hajnoczi | int64_t sector_num, int nb_sectors)
|
1957 | f08f2dda | Stefan Hajnoczi | { |
1958 | f08f2dda | Stefan Hajnoczi | BlockDriver *drv = bs->drv; |
1959 | f08f2dda | Stefan Hajnoczi | QEMUIOVector qiov; |
1960 | f08f2dda | Stefan Hajnoczi | struct iovec iov;
|
1961 | f08f2dda | Stefan Hajnoczi | int ret;
|
1962 | f08f2dda | Stefan Hajnoczi | |
1963 | 621f0589 | Kevin Wolf | /* TODO Emulate only part of misaligned requests instead of letting block
|
1964 | 621f0589 | Kevin Wolf | * drivers return -ENOTSUP and emulate everything */
|
1965 | 621f0589 | Kevin Wolf | |
1966 | f08f2dda | Stefan Hajnoczi | /* First try the efficient write zeroes operation */
|
1967 | f08f2dda | Stefan Hajnoczi | if (drv->bdrv_co_write_zeroes) {
|
1968 | 621f0589 | Kevin Wolf | ret = drv->bdrv_co_write_zeroes(bs, sector_num, nb_sectors); |
1969 | 621f0589 | Kevin Wolf | if (ret != -ENOTSUP) {
|
1970 | 621f0589 | Kevin Wolf | return ret;
|
1971 | 621f0589 | Kevin Wolf | } |
1972 | f08f2dda | Stefan Hajnoczi | } |
1973 | f08f2dda | Stefan Hajnoczi | |
1974 | f08f2dda | Stefan Hajnoczi | /* Fall back to bounce buffer if write zeroes is unsupported */
|
1975 | f08f2dda | Stefan Hajnoczi | iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE; |
1976 | f08f2dda | Stefan Hajnoczi | iov.iov_base = qemu_blockalign(bs, iov.iov_len); |
1977 | f08f2dda | Stefan Hajnoczi | memset(iov.iov_base, 0, iov.iov_len);
|
1978 | f08f2dda | Stefan Hajnoczi | qemu_iovec_init_external(&qiov, &iov, 1);
|
1979 | f08f2dda | Stefan Hajnoczi | |
1980 | f08f2dda | Stefan Hajnoczi | ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, &qiov); |
1981 | f08f2dda | Stefan Hajnoczi | |
1982 | f08f2dda | Stefan Hajnoczi | qemu_vfree(iov.iov_base); |
1983 | f08f2dda | Stefan Hajnoczi | return ret;
|
1984 | f08f2dda | Stefan Hajnoczi | } |
1985 | f08f2dda | Stefan Hajnoczi | |
1986 | c5fbe571 | Stefan Hajnoczi | /*
|
1987 | c5fbe571 | Stefan Hajnoczi | * Handle a write request in coroutine context
|
1988 | c5fbe571 | Stefan Hajnoczi | */
|
1989 | c5fbe571 | Stefan Hajnoczi | static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs, |
1990 | f08f2dda | Stefan Hajnoczi | int64_t sector_num, int nb_sectors, QEMUIOVector *qiov,
|
1991 | f08f2dda | Stefan Hajnoczi | BdrvRequestFlags flags) |
1992 | c5fbe571 | Stefan Hajnoczi | { |
1993 | c5fbe571 | Stefan Hajnoczi | BlockDriver *drv = bs->drv; |
1994 | dbffbdcf | Stefan Hajnoczi | BdrvTrackedRequest req; |
1995 | 6b7cb247 | Stefan Hajnoczi | int ret;
|
1996 | da1fa91d | Kevin Wolf | |
1997 | da1fa91d | Kevin Wolf | if (!bs->drv) {
|
1998 | da1fa91d | Kevin Wolf | return -ENOMEDIUM;
|
1999 | da1fa91d | Kevin Wolf | } |
2000 | da1fa91d | Kevin Wolf | if (bs->read_only) {
|
2001 | da1fa91d | Kevin Wolf | return -EACCES;
|
2002 | da1fa91d | Kevin Wolf | } |
2003 | da1fa91d | Kevin Wolf | if (bdrv_check_request(bs, sector_num, nb_sectors)) {
|
2004 | da1fa91d | Kevin Wolf | return -EIO;
|
2005 | da1fa91d | Kevin Wolf | } |
2006 | da1fa91d | Kevin Wolf | |
2007 | 98f90dba | Zhi Yong Wu | /* throttling disk write I/O */
|
2008 | 98f90dba | Zhi Yong Wu | if (bs->io_limits_enabled) {
|
2009 | 98f90dba | Zhi Yong Wu | bdrv_io_limits_intercept(bs, true, nb_sectors);
|
2010 | 98f90dba | Zhi Yong Wu | } |
2011 | 98f90dba | Zhi Yong Wu | |
2012 | 470c0504 | Stefan Hajnoczi | if (bs->copy_on_read_in_flight) {
|
2013 | f4658285 | Stefan Hajnoczi | wait_for_overlapping_requests(bs, sector_num, nb_sectors); |
2014 | f4658285 | Stefan Hajnoczi | } |
2015 | f4658285 | Stefan Hajnoczi | |
2016 | dbffbdcf | Stefan Hajnoczi | tracked_request_begin(&req, bs, sector_num, nb_sectors, true);
|
2017 | dbffbdcf | Stefan Hajnoczi | |
2018 | f08f2dda | Stefan Hajnoczi | if (flags & BDRV_REQ_ZERO_WRITE) {
|
2019 | f08f2dda | Stefan Hajnoczi | ret = bdrv_co_do_write_zeroes(bs, sector_num, nb_sectors); |
2020 | f08f2dda | Stefan Hajnoczi | } else {
|
2021 | f08f2dda | Stefan Hajnoczi | ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov); |
2022 | f08f2dda | Stefan Hajnoczi | } |
2023 | 6b7cb247 | Stefan Hajnoczi | |
2024 | f05fa4ad | Paolo Bonzini | if (ret == 0 && !bs->enable_write_cache) { |
2025 | f05fa4ad | Paolo Bonzini | ret = bdrv_co_flush(bs); |
2026 | f05fa4ad | Paolo Bonzini | } |
2027 | f05fa4ad | Paolo Bonzini | |
2028 | da1fa91d | Kevin Wolf | if (bs->dirty_bitmap) {
|
2029 | da1fa91d | Kevin Wolf | set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
|
2030 | da1fa91d | Kevin Wolf | } |
2031 | da1fa91d | Kevin Wolf | |
2032 | da1fa91d | Kevin Wolf | if (bs->wr_highest_sector < sector_num + nb_sectors - 1) { |
2033 | da1fa91d | Kevin Wolf | bs->wr_highest_sector = sector_num + nb_sectors - 1;
|
2034 | da1fa91d | Kevin Wolf | } |
2035 | da1fa91d | Kevin Wolf | |
2036 | dbffbdcf | Stefan Hajnoczi | tracked_request_end(&req); |
2037 | dbffbdcf | Stefan Hajnoczi | |
2038 | 6b7cb247 | Stefan Hajnoczi | return ret;
|
2039 | da1fa91d | Kevin Wolf | } |
2040 | da1fa91d | Kevin Wolf | |
2041 | c5fbe571 | Stefan Hajnoczi | int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num,
|
2042 | c5fbe571 | Stefan Hajnoczi | int nb_sectors, QEMUIOVector *qiov)
|
2043 | c5fbe571 | Stefan Hajnoczi | { |
2044 | c5fbe571 | Stefan Hajnoczi | trace_bdrv_co_writev(bs, sector_num, nb_sectors); |
2045 | c5fbe571 | Stefan Hajnoczi | |
2046 | f08f2dda | Stefan Hajnoczi | return bdrv_co_do_writev(bs, sector_num, nb_sectors, qiov, 0); |
2047 | f08f2dda | Stefan Hajnoczi | } |
2048 | f08f2dda | Stefan Hajnoczi | |
2049 | f08f2dda | Stefan Hajnoczi | int coroutine_fn bdrv_co_write_zeroes(BlockDriverState *bs,
|
2050 | f08f2dda | Stefan Hajnoczi | int64_t sector_num, int nb_sectors)
|
2051 | f08f2dda | Stefan Hajnoczi | { |
2052 | f08f2dda | Stefan Hajnoczi | trace_bdrv_co_write_zeroes(bs, sector_num, nb_sectors); |
2053 | f08f2dda | Stefan Hajnoczi | |
2054 | f08f2dda | Stefan Hajnoczi | return bdrv_co_do_writev(bs, sector_num, nb_sectors, NULL, |
2055 | f08f2dda | Stefan Hajnoczi | BDRV_REQ_ZERO_WRITE); |
2056 | c5fbe571 | Stefan Hajnoczi | } |
2057 | c5fbe571 | Stefan Hajnoczi | |
2058 | 83f64091 | bellard | /**
|
2059 | 83f64091 | bellard | * Truncate file to 'offset' bytes (needed only for file protocols)
|
2060 | 83f64091 | bellard | */
|
2061 | 83f64091 | bellard | int bdrv_truncate(BlockDriverState *bs, int64_t offset)
|
2062 | 83f64091 | bellard | { |
2063 | 83f64091 | bellard | BlockDriver *drv = bs->drv; |
2064 | 51762288 | Stefan Hajnoczi | int ret;
|
2065 | 83f64091 | bellard | if (!drv)
|
2066 | 19cb3738 | bellard | return -ENOMEDIUM;
|
2067 | 83f64091 | bellard | if (!drv->bdrv_truncate)
|
2068 | 83f64091 | bellard | return -ENOTSUP;
|
2069 | 59f2689d | Naphtali Sprei | if (bs->read_only)
|
2070 | 59f2689d | Naphtali Sprei | return -EACCES;
|
2071 | 8591675f | Marcelo Tosatti | if (bdrv_in_use(bs))
|
2072 | 8591675f | Marcelo Tosatti | return -EBUSY;
|
2073 | 51762288 | Stefan Hajnoczi | ret = drv->bdrv_truncate(bs, offset); |
2074 | 51762288 | Stefan Hajnoczi | if (ret == 0) { |
2075 | 51762288 | Stefan Hajnoczi | ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS); |
2076 | 145feb17 | Markus Armbruster | bdrv_dev_resize_cb(bs); |
2077 | 51762288 | Stefan Hajnoczi | } |
2078 | 51762288 | Stefan Hajnoczi | return ret;
|
2079 | 83f64091 | bellard | } |
2080 | 83f64091 | bellard | |
2081 | 83f64091 | bellard | /**
|
2082 | 4a1d5e1f | Fam Zheng | * Length of a allocated file in bytes. Sparse files are counted by actual
|
2083 | 4a1d5e1f | Fam Zheng | * allocated space. Return < 0 if error or unknown.
|
2084 | 4a1d5e1f | Fam Zheng | */
|
2085 | 4a1d5e1f | Fam Zheng | int64_t bdrv_get_allocated_file_size(BlockDriverState *bs) |
2086 | 4a1d5e1f | Fam Zheng | { |
2087 | 4a1d5e1f | Fam Zheng | BlockDriver *drv = bs->drv; |
2088 | 4a1d5e1f | Fam Zheng | if (!drv) {
|
2089 | 4a1d5e1f | Fam Zheng | return -ENOMEDIUM;
|
2090 | 4a1d5e1f | Fam Zheng | } |
2091 | 4a1d5e1f | Fam Zheng | if (drv->bdrv_get_allocated_file_size) {
|
2092 | 4a1d5e1f | Fam Zheng | return drv->bdrv_get_allocated_file_size(bs);
|
2093 | 4a1d5e1f | Fam Zheng | } |
2094 | 4a1d5e1f | Fam Zheng | if (bs->file) {
|
2095 | 4a1d5e1f | Fam Zheng | return bdrv_get_allocated_file_size(bs->file);
|
2096 | 4a1d5e1f | Fam Zheng | } |
2097 | 4a1d5e1f | Fam Zheng | return -ENOTSUP;
|
2098 | 4a1d5e1f | Fam Zheng | } |
2099 | 4a1d5e1f | Fam Zheng | |
2100 | 4a1d5e1f | Fam Zheng | /**
|
2101 | 83f64091 | bellard | * Length of a file in bytes. Return < 0 if error or unknown.
|
2102 | 83f64091 | bellard | */
|
2103 | 83f64091 | bellard | int64_t bdrv_getlength(BlockDriverState *bs) |
2104 | 83f64091 | bellard | { |
2105 | 83f64091 | bellard | BlockDriver *drv = bs->drv; |
2106 | 83f64091 | bellard | if (!drv)
|
2107 | 19cb3738 | bellard | return -ENOMEDIUM;
|
2108 | 51762288 | Stefan Hajnoczi | |
2109 | 2c6942fa | Markus Armbruster | if (bs->growable || bdrv_dev_has_removable_media(bs)) {
|
2110 | 46a4e4e6 | Stefan Hajnoczi | if (drv->bdrv_getlength) {
|
2111 | 46a4e4e6 | Stefan Hajnoczi | return drv->bdrv_getlength(bs);
|
2112 | 46a4e4e6 | Stefan Hajnoczi | } |
2113 | 83f64091 | bellard | } |
2114 | 46a4e4e6 | Stefan Hajnoczi | return bs->total_sectors * BDRV_SECTOR_SIZE;
|
2115 | fc01f7e7 | bellard | } |
2116 | fc01f7e7 | bellard | |
2117 | 19cb3738 | bellard | /* return 0 as number of sectors if no device present or error */
|
2118 | 96b8f136 | ths | void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
|
2119 | fc01f7e7 | bellard | { |
2120 | 19cb3738 | bellard | int64_t length; |
2121 | 19cb3738 | bellard | length = bdrv_getlength(bs); |
2122 | 19cb3738 | bellard | if (length < 0) |
2123 | 19cb3738 | bellard | length = 0;
|
2124 | 19cb3738 | bellard | else
|
2125 | 6ea44308 | Jan Kiszka | length = length >> BDRV_SECTOR_BITS; |
2126 | 19cb3738 | bellard | *nb_sectors_ptr = length; |
2127 | fc01f7e7 | bellard | } |
2128 | cf98951b | bellard | |
2129 | 0563e191 | Zhi Yong Wu | /* throttling disk io limits */
|
2130 | 0563e191 | Zhi Yong Wu | void bdrv_set_io_limits(BlockDriverState *bs,
|
2131 | 0563e191 | Zhi Yong Wu | BlockIOLimit *io_limits) |
2132 | 0563e191 | Zhi Yong Wu | { |
2133 | 0563e191 | Zhi Yong Wu | bs->io_limits = *io_limits; |
2134 | 0563e191 | Zhi Yong Wu | bs->io_limits_enabled = bdrv_io_limits_enabled(bs); |
2135 | 0563e191 | Zhi Yong Wu | } |
2136 | 0563e191 | Zhi Yong Wu | |
2137 | abd7f68d | Markus Armbruster | void bdrv_set_on_error(BlockDriverState *bs, BlockErrorAction on_read_error,
|
2138 | abd7f68d | Markus Armbruster | BlockErrorAction on_write_error) |
2139 | abd7f68d | Markus Armbruster | { |
2140 | abd7f68d | Markus Armbruster | bs->on_read_error = on_read_error; |
2141 | abd7f68d | Markus Armbruster | bs->on_write_error = on_write_error; |
2142 | abd7f68d | Markus Armbruster | } |
2143 | abd7f68d | Markus Armbruster | |
2144 | abd7f68d | Markus Armbruster | BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read)
|
2145 | abd7f68d | Markus Armbruster | { |
2146 | abd7f68d | Markus Armbruster | return is_read ? bs->on_read_error : bs->on_write_error;
|
2147 | abd7f68d | Markus Armbruster | } |
2148 | abd7f68d | Markus Armbruster | |
2149 | b338082b | bellard | int bdrv_is_read_only(BlockDriverState *bs)
|
2150 | b338082b | bellard | { |
2151 | b338082b | bellard | return bs->read_only;
|
2152 | b338082b | bellard | } |
2153 | b338082b | bellard | |
2154 | 985a03b0 | ths | int bdrv_is_sg(BlockDriverState *bs)
|
2155 | 985a03b0 | ths | { |
2156 | 985a03b0 | ths | return bs->sg;
|
2157 | 985a03b0 | ths | } |
2158 | 985a03b0 | ths | |
2159 | e900a7b7 | Christoph Hellwig | int bdrv_enable_write_cache(BlockDriverState *bs)
|
2160 | e900a7b7 | Christoph Hellwig | { |
2161 | e900a7b7 | Christoph Hellwig | return bs->enable_write_cache;
|
2162 | e900a7b7 | Christoph Hellwig | } |
2163 | e900a7b7 | Christoph Hellwig | |
2164 | 425b0148 | Paolo Bonzini | void bdrv_set_enable_write_cache(BlockDriverState *bs, bool wce) |
2165 | 425b0148 | Paolo Bonzini | { |
2166 | 425b0148 | Paolo Bonzini | bs->enable_write_cache = wce; |
2167 | 425b0148 | Paolo Bonzini | } |
2168 | 425b0148 | Paolo Bonzini | |
2169 | ea2384d3 | bellard | int bdrv_is_encrypted(BlockDriverState *bs)
|
2170 | ea2384d3 | bellard | { |
2171 | ea2384d3 | bellard | if (bs->backing_hd && bs->backing_hd->encrypted)
|
2172 | ea2384d3 | bellard | return 1; |
2173 | ea2384d3 | bellard | return bs->encrypted;
|
2174 | ea2384d3 | bellard | } |
2175 | ea2384d3 | bellard | |
2176 | c0f4ce77 | aliguori | int bdrv_key_required(BlockDriverState *bs)
|
2177 | c0f4ce77 | aliguori | { |
2178 | c0f4ce77 | aliguori | BlockDriverState *backing_hd = bs->backing_hd; |
2179 | c0f4ce77 | aliguori | |
2180 | c0f4ce77 | aliguori | if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
|
2181 | c0f4ce77 | aliguori | return 1; |
2182 | c0f4ce77 | aliguori | return (bs->encrypted && !bs->valid_key);
|
2183 | c0f4ce77 | aliguori | } |
2184 | c0f4ce77 | aliguori | |
2185 | ea2384d3 | bellard | int bdrv_set_key(BlockDriverState *bs, const char *key) |
2186 | ea2384d3 | bellard | { |
2187 | ea2384d3 | bellard | int ret;
|
2188 | ea2384d3 | bellard | if (bs->backing_hd && bs->backing_hd->encrypted) {
|
2189 | ea2384d3 | bellard | ret = bdrv_set_key(bs->backing_hd, key); |
2190 | ea2384d3 | bellard | if (ret < 0) |
2191 | ea2384d3 | bellard | return ret;
|
2192 | ea2384d3 | bellard | if (!bs->encrypted)
|
2193 | ea2384d3 | bellard | return 0; |
2194 | ea2384d3 | bellard | } |
2195 | fd04a2ae | Shahar Havivi | if (!bs->encrypted) {
|
2196 | fd04a2ae | Shahar Havivi | return -EINVAL;
|
2197 | fd04a2ae | Shahar Havivi | } else if (!bs->drv || !bs->drv->bdrv_set_key) { |
2198 | fd04a2ae | Shahar Havivi | return -ENOMEDIUM;
|
2199 | fd04a2ae | Shahar Havivi | } |
2200 | c0f4ce77 | aliguori | ret = bs->drv->bdrv_set_key(bs, key); |
2201 | bb5fc20f | aliguori | if (ret < 0) { |
2202 | bb5fc20f | aliguori | bs->valid_key = 0;
|
2203 | bb5fc20f | aliguori | } else if (!bs->valid_key) { |
2204 | bb5fc20f | aliguori | bs->valid_key = 1;
|
2205 | bb5fc20f | aliguori | /* call the change callback now, we skipped it on open */
|
2206 | 7d4b4ba5 | Markus Armbruster | bdrv_dev_change_media_cb(bs, true);
|
2207 | bb5fc20f | aliguori | } |
2208 | c0f4ce77 | aliguori | return ret;
|
2209 | ea2384d3 | bellard | } |
2210 | ea2384d3 | bellard | |
2211 | f8d6bba1 | Markus Armbruster | const char *bdrv_get_format_name(BlockDriverState *bs) |
2212 | ea2384d3 | bellard | { |
2213 | f8d6bba1 | Markus Armbruster | return bs->drv ? bs->drv->format_name : NULL; |
2214 | ea2384d3 | bellard | } |
2215 | ea2384d3 | bellard | |
2216 | 5fafdf24 | ths | void bdrv_iterate_format(void (*it)(void *opaque, const char *name), |
2217 | ea2384d3 | bellard | void *opaque)
|
2218 | ea2384d3 | bellard | { |
2219 | ea2384d3 | bellard | BlockDriver *drv; |
2220 | ea2384d3 | bellard | |
2221 | 8a22f02a | Stefan Hajnoczi | QLIST_FOREACH(drv, &bdrv_drivers, list) { |
2222 | ea2384d3 | bellard | it(opaque, drv->format_name); |
2223 | ea2384d3 | bellard | } |
2224 | ea2384d3 | bellard | } |
2225 | ea2384d3 | bellard | |
2226 | b338082b | bellard | BlockDriverState *bdrv_find(const char *name) |
2227 | b338082b | bellard | { |
2228 | b338082b | bellard | BlockDriverState *bs; |
2229 | b338082b | bellard | |
2230 | 1b7bdbc1 | Stefan Hajnoczi | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
2231 | 1b7bdbc1 | Stefan Hajnoczi | if (!strcmp(name, bs->device_name)) {
|
2232 | b338082b | bellard | return bs;
|
2233 | 1b7bdbc1 | Stefan Hajnoczi | } |
2234 | b338082b | bellard | } |
2235 | b338082b | bellard | return NULL; |
2236 | b338082b | bellard | } |
2237 | b338082b | bellard | |
2238 | 2f399b0a | Markus Armbruster | BlockDriverState *bdrv_next(BlockDriverState *bs) |
2239 | 2f399b0a | Markus Armbruster | { |
2240 | 2f399b0a | Markus Armbruster | if (!bs) {
|
2241 | 2f399b0a | Markus Armbruster | return QTAILQ_FIRST(&bdrv_states);
|
2242 | 2f399b0a | Markus Armbruster | } |
2243 | 2f399b0a | Markus Armbruster | return QTAILQ_NEXT(bs, list);
|
2244 | 2f399b0a | Markus Armbruster | } |
2245 | 2f399b0a | Markus Armbruster | |
2246 | 51de9760 | aliguori | void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque) |
2247 | 81d0912d | bellard | { |
2248 | 81d0912d | bellard | BlockDriverState *bs; |
2249 | 81d0912d | bellard | |
2250 | 1b7bdbc1 | Stefan Hajnoczi | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
2251 | 51de9760 | aliguori | it(opaque, bs); |
2252 | 81d0912d | bellard | } |
2253 | 81d0912d | bellard | } |
2254 | 81d0912d | bellard | |
2255 | ea2384d3 | bellard | const char *bdrv_get_device_name(BlockDriverState *bs) |
2256 | ea2384d3 | bellard | { |
2257 | ea2384d3 | bellard | return bs->device_name;
|
2258 | ea2384d3 | bellard | } |
2259 | ea2384d3 | bellard | |
2260 | c8433287 | Markus Armbruster | int bdrv_get_flags(BlockDriverState *bs)
|
2261 | c8433287 | Markus Armbruster | { |
2262 | c8433287 | Markus Armbruster | return bs->open_flags;
|
2263 | c8433287 | Markus Armbruster | } |
2264 | c8433287 | Markus Armbruster | |
2265 | c6ca28d6 | aliguori | void bdrv_flush_all(void) |
2266 | c6ca28d6 | aliguori | { |
2267 | c6ca28d6 | aliguori | BlockDriverState *bs; |
2268 | c6ca28d6 | aliguori | |
2269 | 1b7bdbc1 | Stefan Hajnoczi | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
2270 | 29cdb251 | Paolo Bonzini | bdrv_flush(bs); |
2271 | 1b7bdbc1 | Stefan Hajnoczi | } |
2272 | c6ca28d6 | aliguori | } |
2273 | c6ca28d6 | aliguori | |
2274 | f2feebbd | Kevin Wolf | int bdrv_has_zero_init(BlockDriverState *bs)
|
2275 | f2feebbd | Kevin Wolf | { |
2276 | f2feebbd | Kevin Wolf | assert(bs->drv); |
2277 | f2feebbd | Kevin Wolf | |
2278 | 336c1c12 | Kevin Wolf | if (bs->drv->bdrv_has_zero_init) {
|
2279 | 336c1c12 | Kevin Wolf | return bs->drv->bdrv_has_zero_init(bs);
|
2280 | f2feebbd | Kevin Wolf | } |
2281 | f2feebbd | Kevin Wolf | |
2282 | f2feebbd | Kevin Wolf | return 1; |
2283 | f2feebbd | Kevin Wolf | } |
2284 | f2feebbd | Kevin Wolf | |
2285 | 376ae3f1 | Stefan Hajnoczi | typedef struct BdrvCoIsAllocatedData { |
2286 | 376ae3f1 | Stefan Hajnoczi | BlockDriverState *bs; |
2287 | 376ae3f1 | Stefan Hajnoczi | int64_t sector_num; |
2288 | 376ae3f1 | Stefan Hajnoczi | int nb_sectors;
|
2289 | 376ae3f1 | Stefan Hajnoczi | int *pnum;
|
2290 | 376ae3f1 | Stefan Hajnoczi | int ret;
|
2291 | 376ae3f1 | Stefan Hajnoczi | bool done;
|
2292 | 376ae3f1 | Stefan Hajnoczi | } BdrvCoIsAllocatedData; |
2293 | 376ae3f1 | Stefan Hajnoczi | |
2294 | f58c7b35 | ths | /*
|
2295 | f58c7b35 | ths | * Returns true iff the specified sector is present in the disk image. Drivers
|
2296 | f58c7b35 | ths | * not implementing the functionality are assumed to not support backing files,
|
2297 | f58c7b35 | ths | * hence all their sectors are reported as allocated.
|
2298 | f58c7b35 | ths | *
|
2299 | bd9533e3 | Stefan Hajnoczi | * If 'sector_num' is beyond the end of the disk image the return value is 0
|
2300 | bd9533e3 | Stefan Hajnoczi | * and 'pnum' is set to 0.
|
2301 | bd9533e3 | Stefan Hajnoczi | *
|
2302 | f58c7b35 | ths | * 'pnum' is set to the number of sectors (including and immediately following
|
2303 | f58c7b35 | ths | * the specified sector) that are known to be in the same
|
2304 | f58c7b35 | ths | * allocated/unallocated state.
|
2305 | f58c7b35 | ths | *
|
2306 | bd9533e3 | Stefan Hajnoczi | * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes
|
2307 | bd9533e3 | Stefan Hajnoczi | * beyond the end of the disk image it will be clamped.
|
2308 | f58c7b35 | ths | */
|
2309 | 060f51c9 | Stefan Hajnoczi | int coroutine_fn bdrv_co_is_allocated(BlockDriverState *bs, int64_t sector_num,
|
2310 | 060f51c9 | Stefan Hajnoczi | int nb_sectors, int *pnum) |
2311 | f58c7b35 | ths | { |
2312 | bd9533e3 | Stefan Hajnoczi | int64_t n; |
2313 | bd9533e3 | Stefan Hajnoczi | |
2314 | bd9533e3 | Stefan Hajnoczi | if (sector_num >= bs->total_sectors) {
|
2315 | bd9533e3 | Stefan Hajnoczi | *pnum = 0;
|
2316 | bd9533e3 | Stefan Hajnoczi | return 0; |
2317 | bd9533e3 | Stefan Hajnoczi | } |
2318 | bd9533e3 | Stefan Hajnoczi | |
2319 | bd9533e3 | Stefan Hajnoczi | n = bs->total_sectors - sector_num; |
2320 | bd9533e3 | Stefan Hajnoczi | if (n < nb_sectors) {
|
2321 | bd9533e3 | Stefan Hajnoczi | nb_sectors = n; |
2322 | bd9533e3 | Stefan Hajnoczi | } |
2323 | bd9533e3 | Stefan Hajnoczi | |
2324 | 6aebab14 | Stefan Hajnoczi | if (!bs->drv->bdrv_co_is_allocated) {
|
2325 | bd9533e3 | Stefan Hajnoczi | *pnum = nb_sectors; |
2326 | f58c7b35 | ths | return 1; |
2327 | f58c7b35 | ths | } |
2328 | 6aebab14 | Stefan Hajnoczi | |
2329 | 060f51c9 | Stefan Hajnoczi | return bs->drv->bdrv_co_is_allocated(bs, sector_num, nb_sectors, pnum);
|
2330 | 060f51c9 | Stefan Hajnoczi | } |
2331 | 060f51c9 | Stefan Hajnoczi | |
2332 | 060f51c9 | Stefan Hajnoczi | /* Coroutine wrapper for bdrv_is_allocated() */
|
2333 | 060f51c9 | Stefan Hajnoczi | static void coroutine_fn bdrv_is_allocated_co_entry(void *opaque) |
2334 | 060f51c9 | Stefan Hajnoczi | { |
2335 | 060f51c9 | Stefan Hajnoczi | BdrvCoIsAllocatedData *data = opaque; |
2336 | 060f51c9 | Stefan Hajnoczi | BlockDriverState *bs = data->bs; |
2337 | 060f51c9 | Stefan Hajnoczi | |
2338 | 060f51c9 | Stefan Hajnoczi | data->ret = bdrv_co_is_allocated(bs, data->sector_num, data->nb_sectors, |
2339 | 060f51c9 | Stefan Hajnoczi | data->pnum); |
2340 | 060f51c9 | Stefan Hajnoczi | data->done = true;
|
2341 | 060f51c9 | Stefan Hajnoczi | } |
2342 | 060f51c9 | Stefan Hajnoczi | |
2343 | 060f51c9 | Stefan Hajnoczi | /*
|
2344 | 060f51c9 | Stefan Hajnoczi | * Synchronous wrapper around bdrv_co_is_allocated().
|
2345 | 060f51c9 | Stefan Hajnoczi | *
|
2346 | 060f51c9 | Stefan Hajnoczi | * See bdrv_co_is_allocated() for details.
|
2347 | 060f51c9 | Stefan Hajnoczi | */
|
2348 | 060f51c9 | Stefan Hajnoczi | int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, |
2349 | 060f51c9 | Stefan Hajnoczi | int *pnum)
|
2350 | 060f51c9 | Stefan Hajnoczi | { |
2351 | 6aebab14 | Stefan Hajnoczi | Coroutine *co; |
2352 | 6aebab14 | Stefan Hajnoczi | BdrvCoIsAllocatedData data = { |
2353 | 6aebab14 | Stefan Hajnoczi | .bs = bs, |
2354 | 6aebab14 | Stefan Hajnoczi | .sector_num = sector_num, |
2355 | 6aebab14 | Stefan Hajnoczi | .nb_sectors = nb_sectors, |
2356 | 6aebab14 | Stefan Hajnoczi | .pnum = pnum, |
2357 | 6aebab14 | Stefan Hajnoczi | .done = false,
|
2358 | 6aebab14 | Stefan Hajnoczi | }; |
2359 | 6aebab14 | Stefan Hajnoczi | |
2360 | 6aebab14 | Stefan Hajnoczi | co = qemu_coroutine_create(bdrv_is_allocated_co_entry); |
2361 | 6aebab14 | Stefan Hajnoczi | qemu_coroutine_enter(co, &data); |
2362 | 6aebab14 | Stefan Hajnoczi | while (!data.done) {
|
2363 | 6aebab14 | Stefan Hajnoczi | qemu_aio_wait(); |
2364 | 6aebab14 | Stefan Hajnoczi | } |
2365 | 6aebab14 | Stefan Hajnoczi | return data.ret;
|
2366 | f58c7b35 | ths | } |
2367 | f58c7b35 | ths | |
2368 | 188a7bbf | Paolo Bonzini | /*
|
2369 | 188a7bbf | Paolo Bonzini | * Given an image chain: ... -> [BASE] -> [INTER1] -> [INTER2] -> [TOP]
|
2370 | 188a7bbf | Paolo Bonzini | *
|
2371 | 188a7bbf | Paolo Bonzini | * Return true if the given sector is allocated in any image between
|
2372 | 188a7bbf | Paolo Bonzini | * BASE and TOP (inclusive). BASE can be NULL to check if the given
|
2373 | 188a7bbf | Paolo Bonzini | * sector is allocated in any image of the chain. Return false otherwise.
|
2374 | 188a7bbf | Paolo Bonzini | *
|
2375 | 188a7bbf | Paolo Bonzini | * 'pnum' is set to the number of sectors (including and immediately following
|
2376 | 188a7bbf | Paolo Bonzini | * the specified sector) that are known to be in the same
|
2377 | 188a7bbf | Paolo Bonzini | * allocated/unallocated state.
|
2378 | 188a7bbf | Paolo Bonzini | *
|
2379 | 188a7bbf | Paolo Bonzini | */
|
2380 | 188a7bbf | Paolo Bonzini | int coroutine_fn bdrv_co_is_allocated_above(BlockDriverState *top,
|
2381 | 188a7bbf | Paolo Bonzini | BlockDriverState *base, |
2382 | 188a7bbf | Paolo Bonzini | int64_t sector_num, |
2383 | 188a7bbf | Paolo Bonzini | int nb_sectors, int *pnum) |
2384 | 188a7bbf | Paolo Bonzini | { |
2385 | 188a7bbf | Paolo Bonzini | BlockDriverState *intermediate; |
2386 | 188a7bbf | Paolo Bonzini | int ret, n = nb_sectors;
|
2387 | 188a7bbf | Paolo Bonzini | |
2388 | 188a7bbf | Paolo Bonzini | intermediate = top; |
2389 | 188a7bbf | Paolo Bonzini | while (intermediate && intermediate != base) {
|
2390 | 188a7bbf | Paolo Bonzini | int pnum_inter;
|
2391 | 188a7bbf | Paolo Bonzini | ret = bdrv_co_is_allocated(intermediate, sector_num, nb_sectors, |
2392 | 188a7bbf | Paolo Bonzini | &pnum_inter); |
2393 | 188a7bbf | Paolo Bonzini | if (ret < 0) { |
2394 | 188a7bbf | Paolo Bonzini | return ret;
|
2395 | 188a7bbf | Paolo Bonzini | } else if (ret) { |
2396 | 188a7bbf | Paolo Bonzini | *pnum = pnum_inter; |
2397 | 188a7bbf | Paolo Bonzini | return 1; |
2398 | 188a7bbf | Paolo Bonzini | } |
2399 | 188a7bbf | Paolo Bonzini | |
2400 | 188a7bbf | Paolo Bonzini | /*
|
2401 | 188a7bbf | Paolo Bonzini | * [sector_num, nb_sectors] is unallocated on top but intermediate
|
2402 | 188a7bbf | Paolo Bonzini | * might have
|
2403 | 188a7bbf | Paolo Bonzini | *
|
2404 | 188a7bbf | Paolo Bonzini | * [sector_num+x, nr_sectors] allocated.
|
2405 | 188a7bbf | Paolo Bonzini | */
|
2406 | 188a7bbf | Paolo Bonzini | if (n > pnum_inter) {
|
2407 | 188a7bbf | Paolo Bonzini | n = pnum_inter; |
2408 | 188a7bbf | Paolo Bonzini | } |
2409 | 188a7bbf | Paolo Bonzini | |
2410 | 188a7bbf | Paolo Bonzini | intermediate = intermediate->backing_hd; |
2411 | 188a7bbf | Paolo Bonzini | } |
2412 | 188a7bbf | Paolo Bonzini | |
2413 | 188a7bbf | Paolo Bonzini | *pnum = n; |
2414 | 188a7bbf | Paolo Bonzini | return 0; |
2415 | 188a7bbf | Paolo Bonzini | } |
2416 | 188a7bbf | Paolo Bonzini | |
2417 | b2023818 | Luiz Capitulino | BlockInfoList *qmp_query_block(Error **errp) |
2418 | b338082b | bellard | { |
2419 | b2023818 | Luiz Capitulino | BlockInfoList *head = NULL, *cur_item = NULL; |
2420 | b338082b | bellard | BlockDriverState *bs; |
2421 | b338082b | bellard | |
2422 | 1b7bdbc1 | Stefan Hajnoczi | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
2423 | b2023818 | Luiz Capitulino | BlockInfoList *info = g_malloc0(sizeof(*info));
|
2424 | d15e5465 | Luiz Capitulino | |
2425 | b2023818 | Luiz Capitulino | info->value = g_malloc0(sizeof(*info->value));
|
2426 | b2023818 | Luiz Capitulino | info->value->device = g_strdup(bs->device_name); |
2427 | b2023818 | Luiz Capitulino | info->value->type = g_strdup("unknown");
|
2428 | b2023818 | Luiz Capitulino | info->value->locked = bdrv_dev_is_medium_locked(bs); |
2429 | b2023818 | Luiz Capitulino | info->value->removable = bdrv_dev_has_removable_media(bs); |
2430 | d15e5465 | Luiz Capitulino | |
2431 | e4def80b | Markus Armbruster | if (bdrv_dev_has_removable_media(bs)) {
|
2432 | b2023818 | Luiz Capitulino | info->value->has_tray_open = true;
|
2433 | b2023818 | Luiz Capitulino | info->value->tray_open = bdrv_dev_is_tray_open(bs); |
2434 | e4def80b | Markus Armbruster | } |
2435 | f04ef601 | Luiz Capitulino | |
2436 | f04ef601 | Luiz Capitulino | if (bdrv_iostatus_is_enabled(bs)) {
|
2437 | b2023818 | Luiz Capitulino | info->value->has_io_status = true;
|
2438 | b2023818 | Luiz Capitulino | info->value->io_status = bs->iostatus; |
2439 | f04ef601 | Luiz Capitulino | } |
2440 | f04ef601 | Luiz Capitulino | |
2441 | 19cb3738 | bellard | if (bs->drv) {
|
2442 | b2023818 | Luiz Capitulino | info->value->has_inserted = true;
|
2443 | b2023818 | Luiz Capitulino | info->value->inserted = g_malloc0(sizeof(*info->value->inserted));
|
2444 | b2023818 | Luiz Capitulino | info->value->inserted->file = g_strdup(bs->filename); |
2445 | b2023818 | Luiz Capitulino | info->value->inserted->ro = bs->read_only; |
2446 | b2023818 | Luiz Capitulino | info->value->inserted->drv = g_strdup(bs->drv->format_name); |
2447 | b2023818 | Luiz Capitulino | info->value->inserted->encrypted = bs->encrypted; |
2448 | c75a1a8a | Luiz Capitulino | info->value->inserted->encryption_key_missing = bdrv_key_required(bs); |
2449 | b2023818 | Luiz Capitulino | if (bs->backing_file[0]) { |
2450 | b2023818 | Luiz Capitulino | info->value->inserted->has_backing_file = true;
|
2451 | b2023818 | Luiz Capitulino | info->value->inserted->backing_file = g_strdup(bs->backing_file); |
2452 | 376253ec | aliguori | } |
2453 | 727f005e | Zhi Yong Wu | |
2454 | 2e3e3317 | Benoรฎt Canet | info->value->inserted->backing_file_depth = |
2455 | 2e3e3317 | Benoรฎt Canet | bdrv_get_backing_file_depth(bs); |
2456 | 2e3e3317 | Benoรฎt Canet | |
2457 | 727f005e | Zhi Yong Wu | if (bs->io_limits_enabled) {
|
2458 | 727f005e | Zhi Yong Wu | info->value->inserted->bps = |
2459 | 727f005e | Zhi Yong Wu | bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]; |
2460 | 727f005e | Zhi Yong Wu | info->value->inserted->bps_rd = |
2461 | 727f005e | Zhi Yong Wu | bs->io_limits.bps[BLOCK_IO_LIMIT_READ]; |
2462 | 727f005e | Zhi Yong Wu | info->value->inserted->bps_wr = |
2463 | 727f005e | Zhi Yong Wu | bs->io_limits.bps[BLOCK_IO_LIMIT_WRITE]; |
2464 | 727f005e | Zhi Yong Wu | info->value->inserted->iops = |
2465 | 727f005e | Zhi Yong Wu | bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]; |
2466 | 727f005e | Zhi Yong Wu | info->value->inserted->iops_rd = |
2467 | 727f005e | Zhi Yong Wu | bs->io_limits.iops[BLOCK_IO_LIMIT_READ]; |
2468 | 727f005e | Zhi Yong Wu | info->value->inserted->iops_wr = |
2469 | 727f005e | Zhi Yong Wu | bs->io_limits.iops[BLOCK_IO_LIMIT_WRITE]; |
2470 | 727f005e | Zhi Yong Wu | } |
2471 | b2023818 | Luiz Capitulino | } |
2472 | d15e5465 | Luiz Capitulino | |
2473 | b2023818 | Luiz Capitulino | /* XXX: waiting for the qapi to support GSList */
|
2474 | b2023818 | Luiz Capitulino | if (!cur_item) {
|
2475 | b2023818 | Luiz Capitulino | head = cur_item = info; |
2476 | b2023818 | Luiz Capitulino | } else {
|
2477 | b2023818 | Luiz Capitulino | cur_item->next = info; |
2478 | b2023818 | Luiz Capitulino | cur_item = info; |
2479 | b338082b | bellard | } |
2480 | b338082b | bellard | } |
2481 | d15e5465 | Luiz Capitulino | |
2482 | b2023818 | Luiz Capitulino | return head;
|
2483 | b338082b | bellard | } |
2484 | a36e69dd | ths | |
2485 | f11f57e4 | Luiz Capitulino | /* Consider exposing this as a full fledged QMP command */
|
2486 | f11f57e4 | Luiz Capitulino | static BlockStats *qmp_query_blockstat(const BlockDriverState *bs, Error **errp) |
2487 | f11f57e4 | Luiz Capitulino | { |
2488 | f11f57e4 | Luiz Capitulino | BlockStats *s; |
2489 | f11f57e4 | Luiz Capitulino | |
2490 | f11f57e4 | Luiz Capitulino | s = g_malloc0(sizeof(*s));
|
2491 | f11f57e4 | Luiz Capitulino | |
2492 | f11f57e4 | Luiz Capitulino | if (bs->device_name[0]) { |
2493 | f11f57e4 | Luiz Capitulino | s->has_device = true;
|
2494 | f11f57e4 | Luiz Capitulino | s->device = g_strdup(bs->device_name); |
2495 | 294cc35f | Kevin Wolf | } |
2496 | 294cc35f | Kevin Wolf | |
2497 | f11f57e4 | Luiz Capitulino | s->stats = g_malloc0(sizeof(*s->stats));
|
2498 | f11f57e4 | Luiz Capitulino | s->stats->rd_bytes = bs->nr_bytes[BDRV_ACCT_READ]; |
2499 | f11f57e4 | Luiz Capitulino | s->stats->wr_bytes = bs->nr_bytes[BDRV_ACCT_WRITE]; |
2500 | f11f57e4 | Luiz Capitulino | s->stats->rd_operations = bs->nr_ops[BDRV_ACCT_READ]; |
2501 | f11f57e4 | Luiz Capitulino | s->stats->wr_operations = bs->nr_ops[BDRV_ACCT_WRITE]; |
2502 | f11f57e4 | Luiz Capitulino | s->stats->wr_highest_offset = bs->wr_highest_sector * BDRV_SECTOR_SIZE; |
2503 | f11f57e4 | Luiz Capitulino | s->stats->flush_operations = bs->nr_ops[BDRV_ACCT_FLUSH]; |
2504 | f11f57e4 | Luiz Capitulino | s->stats->wr_total_time_ns = bs->total_time_ns[BDRV_ACCT_WRITE]; |
2505 | f11f57e4 | Luiz Capitulino | s->stats->rd_total_time_ns = bs->total_time_ns[BDRV_ACCT_READ]; |
2506 | f11f57e4 | Luiz Capitulino | s->stats->flush_total_time_ns = bs->total_time_ns[BDRV_ACCT_FLUSH]; |
2507 | f11f57e4 | Luiz Capitulino | |
2508 | 294cc35f | Kevin Wolf | if (bs->file) {
|
2509 | f11f57e4 | Luiz Capitulino | s->has_parent = true;
|
2510 | f11f57e4 | Luiz Capitulino | s->parent = qmp_query_blockstat(bs->file, NULL);
|
2511 | 294cc35f | Kevin Wolf | } |
2512 | 294cc35f | Kevin Wolf | |
2513 | f11f57e4 | Luiz Capitulino | return s;
|
2514 | 294cc35f | Kevin Wolf | } |
2515 | 294cc35f | Kevin Wolf | |
2516 | f11f57e4 | Luiz Capitulino | BlockStatsList *qmp_query_blockstats(Error **errp) |
2517 | 218a536a | Luiz Capitulino | { |
2518 | f11f57e4 | Luiz Capitulino | BlockStatsList *head = NULL, *cur_item = NULL; |
2519 | a36e69dd | ths | BlockDriverState *bs; |
2520 | a36e69dd | ths | |
2521 | 1b7bdbc1 | Stefan Hajnoczi | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
2522 | f11f57e4 | Luiz Capitulino | BlockStatsList *info = g_malloc0(sizeof(*info));
|
2523 | f11f57e4 | Luiz Capitulino | info->value = qmp_query_blockstat(bs, NULL);
|
2524 | f11f57e4 | Luiz Capitulino | |
2525 | f11f57e4 | Luiz Capitulino | /* XXX: waiting for the qapi to support GSList */
|
2526 | f11f57e4 | Luiz Capitulino | if (!cur_item) {
|
2527 | f11f57e4 | Luiz Capitulino | head = cur_item = info; |
2528 | f11f57e4 | Luiz Capitulino | } else {
|
2529 | f11f57e4 | Luiz Capitulino | cur_item->next = info; |
2530 | f11f57e4 | Luiz Capitulino | cur_item = info; |
2531 | f11f57e4 | Luiz Capitulino | } |
2532 | a36e69dd | ths | } |
2533 | 218a536a | Luiz Capitulino | |
2534 | f11f57e4 | Luiz Capitulino | return head;
|
2535 | a36e69dd | ths | } |
2536 | ea2384d3 | bellard | |
2537 | 045df330 | aliguori | const char *bdrv_get_encrypted_filename(BlockDriverState *bs) |
2538 | 045df330 | aliguori | { |
2539 | 045df330 | aliguori | if (bs->backing_hd && bs->backing_hd->encrypted)
|
2540 | 045df330 | aliguori | return bs->backing_file;
|
2541 | 045df330 | aliguori | else if (bs->encrypted) |
2542 | 045df330 | aliguori | return bs->filename;
|
2543 | 045df330 | aliguori | else
|
2544 | 045df330 | aliguori | return NULL; |
2545 | 045df330 | aliguori | } |
2546 | 045df330 | aliguori | |
2547 | 5fafdf24 | ths | void bdrv_get_backing_filename(BlockDriverState *bs,
|
2548 | 83f64091 | bellard | char *filename, int filename_size) |
2549 | 83f64091 | bellard | { |
2550 | 3574c608 | Kevin Wolf | pstrcpy(filename, filename_size, bs->backing_file); |
2551 | 83f64091 | bellard | } |
2552 | 83f64091 | bellard | |
2553 | 5fafdf24 | ths | int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
|
2554 | faea38e7 | bellard | const uint8_t *buf, int nb_sectors) |
2555 | faea38e7 | bellard | { |
2556 | faea38e7 | bellard | BlockDriver *drv = bs->drv; |
2557 | faea38e7 | bellard | if (!drv)
|
2558 | 19cb3738 | bellard | return -ENOMEDIUM;
|
2559 | faea38e7 | bellard | if (!drv->bdrv_write_compressed)
|
2560 | faea38e7 | bellard | return -ENOTSUP;
|
2561 | fbb7b4e0 | Kevin Wolf | if (bdrv_check_request(bs, sector_num, nb_sectors))
|
2562 | fbb7b4e0 | Kevin Wolf | return -EIO;
|
2563 | a55eb92c | Jan Kiszka | |
2564 | c6d22830 | Jan Kiszka | if (bs->dirty_bitmap) {
|
2565 | 7cd1e32a | lirans@il.ibm.com | set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
|
2566 | 7cd1e32a | lirans@il.ibm.com | } |
2567 | a55eb92c | Jan Kiszka | |
2568 | faea38e7 | bellard | return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
|
2569 | faea38e7 | bellard | } |
2570 | 3b46e624 | ths | |
2571 | faea38e7 | bellard | int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
|
2572 | faea38e7 | bellard | { |
2573 | faea38e7 | bellard | BlockDriver *drv = bs->drv; |
2574 | faea38e7 | bellard | if (!drv)
|
2575 | 19cb3738 | bellard | return -ENOMEDIUM;
|
2576 | faea38e7 | bellard | if (!drv->bdrv_get_info)
|
2577 | faea38e7 | bellard | return -ENOTSUP;
|
2578 | faea38e7 | bellard | memset(bdi, 0, sizeof(*bdi)); |
2579 | faea38e7 | bellard | return drv->bdrv_get_info(bs, bdi);
|
2580 | faea38e7 | bellard | } |
2581 | faea38e7 | bellard | |
2582 | 45566e9c | Christoph Hellwig | int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf, |
2583 | 45566e9c | Christoph Hellwig | int64_t pos, int size)
|
2584 | 178e08a5 | aliguori | { |
2585 | 178e08a5 | aliguori | BlockDriver *drv = bs->drv; |
2586 | 178e08a5 | aliguori | if (!drv)
|
2587 | 178e08a5 | aliguori | return -ENOMEDIUM;
|
2588 | 7cdb1f6d | MORITA Kazutaka | if (drv->bdrv_save_vmstate)
|
2589 | 7cdb1f6d | MORITA Kazutaka | return drv->bdrv_save_vmstate(bs, buf, pos, size);
|
2590 | 7cdb1f6d | MORITA Kazutaka | if (bs->file)
|
2591 | 7cdb1f6d | MORITA Kazutaka | return bdrv_save_vmstate(bs->file, buf, pos, size);
|
2592 | 7cdb1f6d | MORITA Kazutaka | return -ENOTSUP;
|
2593 | 178e08a5 | aliguori | } |
2594 | 178e08a5 | aliguori | |
2595 | 45566e9c | Christoph Hellwig | int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
|
2596 | 45566e9c | Christoph Hellwig | int64_t pos, int size)
|
2597 | 178e08a5 | aliguori | { |
2598 | 178e08a5 | aliguori | BlockDriver *drv = bs->drv; |
2599 | 178e08a5 | aliguori | if (!drv)
|
2600 | 178e08a5 | aliguori | return -ENOMEDIUM;
|
2601 | 7cdb1f6d | MORITA Kazutaka | if (drv->bdrv_load_vmstate)
|
2602 | 7cdb1f6d | MORITA Kazutaka | return drv->bdrv_load_vmstate(bs, buf, pos, size);
|
2603 | 7cdb1f6d | MORITA Kazutaka | if (bs->file)
|
2604 | 7cdb1f6d | MORITA Kazutaka | return bdrv_load_vmstate(bs->file, buf, pos, size);
|
2605 | 7cdb1f6d | MORITA Kazutaka | return -ENOTSUP;
|
2606 | 178e08a5 | aliguori | } |
2607 | 178e08a5 | aliguori | |
2608 | 8b9b0cc2 | Kevin Wolf | void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event)
|
2609 | 8b9b0cc2 | Kevin Wolf | { |
2610 | 8b9b0cc2 | Kevin Wolf | BlockDriver *drv = bs->drv; |
2611 | 8b9b0cc2 | Kevin Wolf | |
2612 | 8b9b0cc2 | Kevin Wolf | if (!drv || !drv->bdrv_debug_event) {
|
2613 | 8b9b0cc2 | Kevin Wolf | return;
|
2614 | 8b9b0cc2 | Kevin Wolf | } |
2615 | 8b9b0cc2 | Kevin Wolf | |
2616 | 0ed8b6f6 | Blue Swirl | drv->bdrv_debug_event(bs, event); |
2617 | 8b9b0cc2 | Kevin Wolf | |
2618 | 8b9b0cc2 | Kevin Wolf | } |
2619 | 8b9b0cc2 | Kevin Wolf | |
2620 | faea38e7 | bellard | /**************************************************************/
|
2621 | faea38e7 | bellard | /* handling of snapshots */
|
2622 | faea38e7 | bellard | |
2623 | feeee5ac | Miguel Di Ciurcio Filho | int bdrv_can_snapshot(BlockDriverState *bs)
|
2624 | feeee5ac | Miguel Di Ciurcio Filho | { |
2625 | feeee5ac | Miguel Di Ciurcio Filho | BlockDriver *drv = bs->drv; |
2626 | 07b70bfb | Markus Armbruster | if (!drv || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
|
2627 | feeee5ac | Miguel Di Ciurcio Filho | return 0; |
2628 | feeee5ac | Miguel Di Ciurcio Filho | } |
2629 | feeee5ac | Miguel Di Ciurcio Filho | |
2630 | feeee5ac | Miguel Di Ciurcio Filho | if (!drv->bdrv_snapshot_create) {
|
2631 | feeee5ac | Miguel Di Ciurcio Filho | if (bs->file != NULL) { |
2632 | feeee5ac | Miguel Di Ciurcio Filho | return bdrv_can_snapshot(bs->file);
|
2633 | feeee5ac | Miguel Di Ciurcio Filho | } |
2634 | feeee5ac | Miguel Di Ciurcio Filho | return 0; |
2635 | feeee5ac | Miguel Di Ciurcio Filho | } |
2636 | feeee5ac | Miguel Di Ciurcio Filho | |
2637 | feeee5ac | Miguel Di Ciurcio Filho | return 1; |
2638 | feeee5ac | Miguel Di Ciurcio Filho | } |
2639 | feeee5ac | Miguel Di Ciurcio Filho | |
2640 | 199630b6 | Blue Swirl | int bdrv_is_snapshot(BlockDriverState *bs)
|
2641 | 199630b6 | Blue Swirl | { |
2642 | 199630b6 | Blue Swirl | return !!(bs->open_flags & BDRV_O_SNAPSHOT);
|
2643 | 199630b6 | Blue Swirl | } |
2644 | 199630b6 | Blue Swirl | |
2645 | f9092b10 | Markus Armbruster | BlockDriverState *bdrv_snapshots(void)
|
2646 | f9092b10 | Markus Armbruster | { |
2647 | f9092b10 | Markus Armbruster | BlockDriverState *bs; |
2648 | f9092b10 | Markus Armbruster | |
2649 | 3ac906f7 | Markus Armbruster | if (bs_snapshots) {
|
2650 | f9092b10 | Markus Armbruster | return bs_snapshots;
|
2651 | 3ac906f7 | Markus Armbruster | } |
2652 | f9092b10 | Markus Armbruster | |
2653 | f9092b10 | Markus Armbruster | bs = NULL;
|
2654 | f9092b10 | Markus Armbruster | while ((bs = bdrv_next(bs))) {
|
2655 | f9092b10 | Markus Armbruster | if (bdrv_can_snapshot(bs)) {
|
2656 | 3ac906f7 | Markus Armbruster | bs_snapshots = bs; |
2657 | 3ac906f7 | Markus Armbruster | return bs;
|
2658 | f9092b10 | Markus Armbruster | } |
2659 | f9092b10 | Markus Armbruster | } |
2660 | f9092b10 | Markus Armbruster | return NULL; |
2661 | f9092b10 | Markus Armbruster | } |
2662 | f9092b10 | Markus Armbruster | |
2663 | 5fafdf24 | ths | int bdrv_snapshot_create(BlockDriverState *bs,
|
2664 | faea38e7 | bellard | QEMUSnapshotInfo *sn_info) |
2665 | faea38e7 | bellard | { |
2666 | faea38e7 | bellard | BlockDriver *drv = bs->drv; |
2667 | faea38e7 | bellard | if (!drv)
|
2668 | 19cb3738 | bellard | return -ENOMEDIUM;
|
2669 | 7cdb1f6d | MORITA Kazutaka | if (drv->bdrv_snapshot_create)
|
2670 | 7cdb1f6d | MORITA Kazutaka | return drv->bdrv_snapshot_create(bs, sn_info);
|
2671 | 7cdb1f6d | MORITA Kazutaka | if (bs->file)
|
2672 | 7cdb1f6d | MORITA Kazutaka | return bdrv_snapshot_create(bs->file, sn_info);
|
2673 | 7cdb1f6d | MORITA Kazutaka | return -ENOTSUP;
|
2674 | faea38e7 | bellard | } |
2675 | faea38e7 | bellard | |
2676 | 5fafdf24 | ths | int bdrv_snapshot_goto(BlockDriverState *bs,
|
2677 | faea38e7 | bellard | const char *snapshot_id) |
2678 | faea38e7 | bellard | { |
2679 | faea38e7 | bellard | BlockDriver *drv = bs->drv; |
2680 | 7cdb1f6d | MORITA Kazutaka | int ret, open_ret;
|
2681 | 7cdb1f6d | MORITA Kazutaka | |
2682 | faea38e7 | bellard | if (!drv)
|
2683 | 19cb3738 | bellard | return -ENOMEDIUM;
|
2684 | 7cdb1f6d | MORITA Kazutaka | if (drv->bdrv_snapshot_goto)
|
2685 | 7cdb1f6d | MORITA Kazutaka | return drv->bdrv_snapshot_goto(bs, snapshot_id);
|
2686 | 7cdb1f6d | MORITA Kazutaka | |
2687 | 7cdb1f6d | MORITA Kazutaka | if (bs->file) {
|
2688 | 7cdb1f6d | MORITA Kazutaka | drv->bdrv_close(bs); |
2689 | 7cdb1f6d | MORITA Kazutaka | ret = bdrv_snapshot_goto(bs->file, snapshot_id); |
2690 | 7cdb1f6d | MORITA Kazutaka | open_ret = drv->bdrv_open(bs, bs->open_flags); |
2691 | 7cdb1f6d | MORITA Kazutaka | if (open_ret < 0) { |
2692 | 7cdb1f6d | MORITA Kazutaka | bdrv_delete(bs->file); |
2693 | 7cdb1f6d | MORITA Kazutaka | bs->drv = NULL;
|
2694 | 7cdb1f6d | MORITA Kazutaka | return open_ret;
|
2695 | 7cdb1f6d | MORITA Kazutaka | } |
2696 | 7cdb1f6d | MORITA Kazutaka | return ret;
|
2697 | 7cdb1f6d | MORITA Kazutaka | } |
2698 | 7cdb1f6d | MORITA Kazutaka | |
2699 | 7cdb1f6d | MORITA Kazutaka | return -ENOTSUP;
|
2700 | faea38e7 | bellard | } |
2701 | faea38e7 | bellard | |
2702 | faea38e7 | bellard | int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id) |
2703 | faea38e7 | bellard | { |
2704 | faea38e7 | bellard | BlockDriver *drv = bs->drv; |
2705 | faea38e7 | bellard | if (!drv)
|
2706 | 19cb3738 | bellard | return -ENOMEDIUM;
|
2707 | 7cdb1f6d | MORITA Kazutaka | if (drv->bdrv_snapshot_delete)
|
2708 | 7cdb1f6d | MORITA Kazutaka | return drv->bdrv_snapshot_delete(bs, snapshot_id);
|
2709 | 7cdb1f6d | MORITA Kazutaka | if (bs->file)
|
2710 | 7cdb1f6d | MORITA Kazutaka | return bdrv_snapshot_delete(bs->file, snapshot_id);
|
2711 | 7cdb1f6d | MORITA Kazutaka | return -ENOTSUP;
|
2712 | faea38e7 | bellard | } |
2713 | faea38e7 | bellard | |
2714 | 5fafdf24 | ths | int bdrv_snapshot_list(BlockDriverState *bs,
|
2715 | faea38e7 | bellard | QEMUSnapshotInfo **psn_info) |
2716 | faea38e7 | bellard | { |
2717 | faea38e7 | bellard | BlockDriver *drv = bs->drv; |
2718 | faea38e7 | bellard | if (!drv)
|
2719 | 19cb3738 | bellard | return -ENOMEDIUM;
|
2720 | 7cdb1f6d | MORITA Kazutaka | if (drv->bdrv_snapshot_list)
|
2721 | 7cdb1f6d | MORITA Kazutaka | return drv->bdrv_snapshot_list(bs, psn_info);
|
2722 | 7cdb1f6d | MORITA Kazutaka | if (bs->file)
|
2723 | 7cdb1f6d | MORITA Kazutaka | return bdrv_snapshot_list(bs->file, psn_info);
|
2724 | 7cdb1f6d | MORITA Kazutaka | return -ENOTSUP;
|
2725 | faea38e7 | bellard | } |
2726 | faea38e7 | bellard | |
2727 | 51ef6727 | edison | int bdrv_snapshot_load_tmp(BlockDriverState *bs,
|
2728 | 51ef6727 | edison | const char *snapshot_name) |
2729 | 51ef6727 | edison | { |
2730 | 51ef6727 | edison | BlockDriver *drv = bs->drv; |
2731 | 51ef6727 | edison | if (!drv) {
|
2732 | 51ef6727 | edison | return -ENOMEDIUM;
|
2733 | 51ef6727 | edison | } |
2734 | 51ef6727 | edison | if (!bs->read_only) {
|
2735 | 51ef6727 | edison | return -EINVAL;
|
2736 | 51ef6727 | edison | } |
2737 | 51ef6727 | edison | if (drv->bdrv_snapshot_load_tmp) {
|
2738 | 51ef6727 | edison | return drv->bdrv_snapshot_load_tmp(bs, snapshot_name);
|
2739 | 51ef6727 | edison | } |
2740 | 51ef6727 | edison | return -ENOTSUP;
|
2741 | 51ef6727 | edison | } |
2742 | 51ef6727 | edison | |
2743 | e8a6bb9c | Marcelo Tosatti | BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs, |
2744 | e8a6bb9c | Marcelo Tosatti | const char *backing_file) |
2745 | e8a6bb9c | Marcelo Tosatti | { |
2746 | e8a6bb9c | Marcelo Tosatti | if (!bs->drv) {
|
2747 | e8a6bb9c | Marcelo Tosatti | return NULL; |
2748 | e8a6bb9c | Marcelo Tosatti | } |
2749 | e8a6bb9c | Marcelo Tosatti | |
2750 | e8a6bb9c | Marcelo Tosatti | if (bs->backing_hd) {
|
2751 | e8a6bb9c | Marcelo Tosatti | if (strcmp(bs->backing_file, backing_file) == 0) { |
2752 | e8a6bb9c | Marcelo Tosatti | return bs->backing_hd;
|
2753 | e8a6bb9c | Marcelo Tosatti | } else {
|
2754 | e8a6bb9c | Marcelo Tosatti | return bdrv_find_backing_image(bs->backing_hd, backing_file);
|
2755 | e8a6bb9c | Marcelo Tosatti | } |
2756 | e8a6bb9c | Marcelo Tosatti | } |
2757 | e8a6bb9c | Marcelo Tosatti | |
2758 | e8a6bb9c | Marcelo Tosatti | return NULL; |
2759 | e8a6bb9c | Marcelo Tosatti | } |
2760 | e8a6bb9c | Marcelo Tosatti | |
2761 | f198fd1c | Benoรฎt Canet | int bdrv_get_backing_file_depth(BlockDriverState *bs)
|
2762 | f198fd1c | Benoรฎt Canet | { |
2763 | f198fd1c | Benoรฎt Canet | if (!bs->drv) {
|
2764 | f198fd1c | Benoรฎt Canet | return 0; |
2765 | f198fd1c | Benoรฎt Canet | } |
2766 | f198fd1c | Benoรฎt Canet | |
2767 | f198fd1c | Benoรฎt Canet | if (!bs->backing_hd) {
|
2768 | f198fd1c | Benoรฎt Canet | return 0; |
2769 | f198fd1c | Benoรฎt Canet | } |
2770 | f198fd1c | Benoรฎt Canet | |
2771 | f198fd1c | Benoรฎt Canet | return 1 + bdrv_get_backing_file_depth(bs->backing_hd); |
2772 | f198fd1c | Benoรฎt Canet | } |
2773 | f198fd1c | Benoรฎt Canet | |
2774 | faea38e7 | bellard | #define NB_SUFFIXES 4 |
2775 | faea38e7 | bellard | |
2776 | faea38e7 | bellard | char *get_human_readable_size(char *buf, int buf_size, int64_t size) |
2777 | faea38e7 | bellard | { |
2778 | faea38e7 | bellard | static const char suffixes[NB_SUFFIXES] = "KMGT"; |
2779 | faea38e7 | bellard | int64_t base; |
2780 | faea38e7 | bellard | int i;
|
2781 | faea38e7 | bellard | |
2782 | faea38e7 | bellard | if (size <= 999) { |
2783 | faea38e7 | bellard | snprintf(buf, buf_size, "%" PRId64, size);
|
2784 | faea38e7 | bellard | } else {
|
2785 | faea38e7 | bellard | base = 1024;
|
2786 | faea38e7 | bellard | for(i = 0; i < NB_SUFFIXES; i++) { |
2787 | faea38e7 | bellard | if (size < (10 * base)) { |
2788 | 5fafdf24 | ths | snprintf(buf, buf_size, "%0.1f%c",
|
2789 | faea38e7 | bellard | (double)size / base,
|
2790 | faea38e7 | bellard | suffixes[i]); |
2791 | faea38e7 | bellard | break;
|
2792 | faea38e7 | bellard | } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) { |
2793 | 5fafdf24 | ths | snprintf(buf, buf_size, "%" PRId64 "%c", |
2794 | faea38e7 | bellard | ((size + (base >> 1)) / base),
|
2795 | faea38e7 | bellard | suffixes[i]); |
2796 | faea38e7 | bellard | break;
|
2797 | faea38e7 | bellard | } |
2798 | faea38e7 | bellard | base = base * 1024;
|
2799 | faea38e7 | bellard | } |
2800 | faea38e7 | bellard | } |
2801 | faea38e7 | bellard | return buf;
|
2802 | faea38e7 | bellard | } |
2803 | faea38e7 | bellard | |
2804 | faea38e7 | bellard | char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) |
2805 | faea38e7 | bellard | { |
2806 | faea38e7 | bellard | char buf1[128], date_buf[128], clock_buf[128]; |
2807 | 3b9f94e1 | bellard | #ifdef _WIN32
|
2808 | 3b9f94e1 | bellard | struct tm *ptm;
|
2809 | 3b9f94e1 | bellard | #else
|
2810 | faea38e7 | bellard | struct tm tm;
|
2811 | 3b9f94e1 | bellard | #endif
|
2812 | faea38e7 | bellard | time_t ti; |
2813 | faea38e7 | bellard | int64_t secs; |
2814 | faea38e7 | bellard | |
2815 | faea38e7 | bellard | if (!sn) {
|
2816 | 5fafdf24 | ths | snprintf(buf, buf_size, |
2817 | 5fafdf24 | ths | "%-10s%-20s%7s%20s%15s",
|
2818 | faea38e7 | bellard | "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); |
2819 | faea38e7 | bellard | } else {
|
2820 | faea38e7 | bellard | ti = sn->date_sec; |
2821 | 3b9f94e1 | bellard | #ifdef _WIN32
|
2822 | 3b9f94e1 | bellard | ptm = localtime(&ti); |
2823 | 3b9f94e1 | bellard | strftime(date_buf, sizeof(date_buf),
|
2824 | 3b9f94e1 | bellard | "%Y-%m-%d %H:%M:%S", ptm);
|
2825 | 3b9f94e1 | bellard | #else
|
2826 | faea38e7 | bellard | localtime_r(&ti, &tm); |
2827 | faea38e7 | bellard | strftime(date_buf, sizeof(date_buf),
|
2828 | faea38e7 | bellard | "%Y-%m-%d %H:%M:%S", &tm);
|
2829 | 3b9f94e1 | bellard | #endif
|
2830 | faea38e7 | bellard | secs = sn->vm_clock_nsec / 1000000000;
|
2831 | faea38e7 | bellard | snprintf(clock_buf, sizeof(clock_buf),
|
2832 | faea38e7 | bellard | "%02d:%02d:%02d.%03d",
|
2833 | faea38e7 | bellard | (int)(secs / 3600), |
2834 | faea38e7 | bellard | (int)((secs / 60) % 60), |
2835 | 5fafdf24 | ths | (int)(secs % 60), |
2836 | faea38e7 | bellard | (int)((sn->vm_clock_nsec / 1000000) % 1000)); |
2837 | faea38e7 | bellard | snprintf(buf, buf_size, |
2838 | 5fafdf24 | ths | "%-10s%-20s%7s%20s%15s",
|
2839 | faea38e7 | bellard | sn->id_str, sn->name, |
2840 | faea38e7 | bellard | get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
|
2841 | faea38e7 | bellard | date_buf, |
2842 | faea38e7 | bellard | clock_buf); |
2843 | faea38e7 | bellard | } |
2844 | faea38e7 | bellard | return buf;
|
2845 | faea38e7 | bellard | } |
2846 | faea38e7 | bellard | |
2847 | ea2384d3 | bellard | /**************************************************************/
|
2848 | 83f64091 | bellard | /* async I/Os */
|
2849 | ea2384d3 | bellard | |
2850 | 3b69e4b9 | aliguori | BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num, |
2851 | f141eafe | aliguori | QEMUIOVector *qiov, int nb_sectors,
|
2852 | 3b69e4b9 | aliguori | BlockDriverCompletionFunc *cb, void *opaque)
|
2853 | 3b69e4b9 | aliguori | { |
2854 | bbf0a440 | Stefan Hajnoczi | trace_bdrv_aio_readv(bs, sector_num, nb_sectors, opaque); |
2855 | bbf0a440 | Stefan Hajnoczi | |
2856 | b2a61371 | Stefan Hajnoczi | return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors,
|
2857 | 8c5873d6 | Stefan Hajnoczi | cb, opaque, false);
|
2858 | ea2384d3 | bellard | } |
2859 | ea2384d3 | bellard | |
2860 | f141eafe | aliguori | BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num, |
2861 | f141eafe | aliguori | QEMUIOVector *qiov, int nb_sectors,
|
2862 | f141eafe | aliguori | BlockDriverCompletionFunc *cb, void *opaque)
|
2863 | ea2384d3 | bellard | { |
2864 | bbf0a440 | Stefan Hajnoczi | trace_bdrv_aio_writev(bs, sector_num, nb_sectors, opaque); |
2865 | bbf0a440 | Stefan Hajnoczi | |
2866 | 1a6e115b | Stefan Hajnoczi | return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors,
|
2867 | 8c5873d6 | Stefan Hajnoczi | cb, opaque, true);
|
2868 | 83f64091 | bellard | } |
2869 | 83f64091 | bellard | |
2870 | 40b4f539 | Kevin Wolf | |
2871 | 40b4f539 | Kevin Wolf | typedef struct MultiwriteCB { |
2872 | 40b4f539 | Kevin Wolf | int error;
|
2873 | 40b4f539 | Kevin Wolf | int num_requests;
|
2874 | 40b4f539 | Kevin Wolf | int num_callbacks;
|
2875 | 40b4f539 | Kevin Wolf | struct {
|
2876 | 40b4f539 | Kevin Wolf | BlockDriverCompletionFunc *cb; |
2877 | 40b4f539 | Kevin Wolf | void *opaque;
|
2878 | 40b4f539 | Kevin Wolf | QEMUIOVector *free_qiov; |
2879 | 40b4f539 | Kevin Wolf | } callbacks[]; |
2880 | 40b4f539 | Kevin Wolf | } MultiwriteCB; |
2881 | 40b4f539 | Kevin Wolf | |
2882 | 40b4f539 | Kevin Wolf | static void multiwrite_user_cb(MultiwriteCB *mcb) |
2883 | 40b4f539 | Kevin Wolf | { |
2884 | 40b4f539 | Kevin Wolf | int i;
|
2885 | 40b4f539 | Kevin Wolf | |
2886 | 40b4f539 | Kevin Wolf | for (i = 0; i < mcb->num_callbacks; i++) { |
2887 | 40b4f539 | Kevin Wolf | mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error); |
2888 | 1e1ea48d | Stefan Hajnoczi | if (mcb->callbacks[i].free_qiov) {
|
2889 | 1e1ea48d | Stefan Hajnoczi | qemu_iovec_destroy(mcb->callbacks[i].free_qiov); |
2890 | 1e1ea48d | Stefan Hajnoczi | } |
2891 | 7267c094 | Anthony Liguori | g_free(mcb->callbacks[i].free_qiov); |
2892 | 40b4f539 | Kevin Wolf | } |
2893 | 40b4f539 | Kevin Wolf | } |
2894 | 40b4f539 | Kevin Wolf | |
2895 | 40b4f539 | Kevin Wolf | static void multiwrite_cb(void *opaque, int ret) |
2896 | 40b4f539 | Kevin Wolf | { |
2897 | 40b4f539 | Kevin Wolf | MultiwriteCB *mcb = opaque; |
2898 | 40b4f539 | Kevin Wolf | |
2899 | 6d519a5f | Stefan Hajnoczi | trace_multiwrite_cb(mcb, ret); |
2900 | 6d519a5f | Stefan Hajnoczi | |
2901 | cb6d3ca0 | Kevin Wolf | if (ret < 0 && !mcb->error) { |
2902 | 40b4f539 | Kevin Wolf | mcb->error = ret; |
2903 | 40b4f539 | Kevin Wolf | } |
2904 | 40b4f539 | Kevin Wolf | |
2905 | 40b4f539 | Kevin Wolf | mcb->num_requests--; |
2906 | 40b4f539 | Kevin Wolf | if (mcb->num_requests == 0) { |
2907 | de189a1b | Kevin Wolf | multiwrite_user_cb(mcb); |
2908 | 7267c094 | Anthony Liguori | g_free(mcb); |
2909 | 40b4f539 | Kevin Wolf | } |
2910 | 40b4f539 | Kevin Wolf | } |
2911 | 40b4f539 | Kevin Wolf | |
2912 | 40b4f539 | Kevin Wolf | static int multiwrite_req_compare(const void *a, const void *b) |
2913 | 40b4f539 | Kevin Wolf | { |
2914 | 77be4366 | Christoph Hellwig | const BlockRequest *req1 = a, *req2 = b;
|
2915 | 77be4366 | Christoph Hellwig | |
2916 | 77be4366 | Christoph Hellwig | /*
|
2917 | 77be4366 | Christoph Hellwig | * Note that we can't simply subtract req2->sector from req1->sector
|
2918 | 77be4366 | Christoph Hellwig | * here as that could overflow the return value.
|
2919 | 77be4366 | Christoph Hellwig | */
|
2920 | 77be4366 | Christoph Hellwig | if (req1->sector > req2->sector) {
|
2921 | 77be4366 | Christoph Hellwig | return 1; |
2922 | 77be4366 | Christoph Hellwig | } else if (req1->sector < req2->sector) { |
2923 | 77be4366 | Christoph Hellwig | return -1; |
2924 | 77be4366 | Christoph Hellwig | } else {
|
2925 | 77be4366 | Christoph Hellwig | return 0; |
2926 | 77be4366 | Christoph Hellwig | } |
2927 | 40b4f539 | Kevin Wolf | } |
2928 | 40b4f539 | Kevin Wolf | |
2929 | 40b4f539 | Kevin Wolf | /*
|
2930 | 40b4f539 | Kevin Wolf | * Takes a bunch of requests and tries to merge them. Returns the number of
|
2931 | 40b4f539 | Kevin Wolf | * requests that remain after merging.
|
2932 | 40b4f539 | Kevin Wolf | */
|
2933 | 40b4f539 | Kevin Wolf | static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs, |
2934 | 40b4f539 | Kevin Wolf | int num_reqs, MultiwriteCB *mcb)
|
2935 | 40b4f539 | Kevin Wolf | { |
2936 | 40b4f539 | Kevin Wolf | int i, outidx;
|
2937 | 40b4f539 | Kevin Wolf | |
2938 | 40b4f539 | Kevin Wolf | // Sort requests by start sector
|
2939 | 40b4f539 | Kevin Wolf | qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare);
|
2940 | 40b4f539 | Kevin Wolf | |
2941 | 40b4f539 | Kevin Wolf | // Check if adjacent requests touch the same clusters. If so, combine them,
|
2942 | 40b4f539 | Kevin Wolf | // filling up gaps with zero sectors.
|
2943 | 40b4f539 | Kevin Wolf | outidx = 0;
|
2944 | 40b4f539 | Kevin Wolf | for (i = 1; i < num_reqs; i++) { |
2945 | 40b4f539 | Kevin Wolf | int merge = 0; |
2946 | 40b4f539 | Kevin Wolf | int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors; |
2947 | 40b4f539 | Kevin Wolf | |
2948 | b6a127a1 | Paolo Bonzini | // Handle exactly sequential writes and overlapping writes.
|
2949 | 40b4f539 | Kevin Wolf | if (reqs[i].sector <= oldreq_last) {
|
2950 | 40b4f539 | Kevin Wolf | merge = 1;
|
2951 | 40b4f539 | Kevin Wolf | } |
2952 | 40b4f539 | Kevin Wolf | |
2953 | e2a305fb | Christoph Hellwig | if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) { |
2954 | e2a305fb | Christoph Hellwig | merge = 0;
|
2955 | e2a305fb | Christoph Hellwig | } |
2956 | e2a305fb | Christoph Hellwig | |
2957 | 40b4f539 | Kevin Wolf | if (merge) {
|
2958 | 40b4f539 | Kevin Wolf | size_t size; |
2959 | 7267c094 | Anthony Liguori | QEMUIOVector *qiov = g_malloc0(sizeof(*qiov));
|
2960 | 40b4f539 | Kevin Wolf | qemu_iovec_init(qiov, |
2961 | 40b4f539 | Kevin Wolf | reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1);
|
2962 | 40b4f539 | Kevin Wolf | |
2963 | 40b4f539 | Kevin Wolf | // Add the first request to the merged one. If the requests are
|
2964 | 40b4f539 | Kevin Wolf | // overlapping, drop the last sectors of the first request.
|
2965 | 40b4f539 | Kevin Wolf | size = (reqs[i].sector - reqs[outidx].sector) << 9;
|
2966 | 1b093c48 | Michael Tokarev | qemu_iovec_concat(qiov, reqs[outidx].qiov, 0, size);
|
2967 | 40b4f539 | Kevin Wolf | |
2968 | b6a127a1 | Paolo Bonzini | // We should need to add any zeros between the two requests
|
2969 | b6a127a1 | Paolo Bonzini | assert (reqs[i].sector <= oldreq_last); |
2970 | 40b4f539 | Kevin Wolf | |
2971 | 40b4f539 | Kevin Wolf | // Add the second request
|
2972 | 1b093c48 | Michael Tokarev | qemu_iovec_concat(qiov, reqs[i].qiov, 0, reqs[i].qiov->size);
|
2973 | 40b4f539 | Kevin Wolf | |
2974 | cbf1dff2 | Kevin Wolf | reqs[outidx].nb_sectors = qiov->size >> 9;
|
2975 | 40b4f539 | Kevin Wolf | reqs[outidx].qiov = qiov; |
2976 | 40b4f539 | Kevin Wolf | |
2977 | 40b4f539 | Kevin Wolf | mcb->callbacks[i].free_qiov = reqs[outidx].qiov; |
2978 | 40b4f539 | Kevin Wolf | } else {
|
2979 | 40b4f539 | Kevin Wolf | outidx++; |
2980 | 40b4f539 | Kevin Wolf | reqs[outidx].sector = reqs[i].sector; |
2981 | 40b4f539 | Kevin Wolf | reqs[outidx].nb_sectors = reqs[i].nb_sectors; |
2982 | 40b4f539 | Kevin Wolf | reqs[outidx].qiov = reqs[i].qiov; |
2983 | 40b4f539 | Kevin Wolf | } |
2984 | 40b4f539 | Kevin Wolf | } |
2985 | 40b4f539 | Kevin Wolf | |
2986 | 40b4f539 | Kevin Wolf | return outidx + 1; |
2987 | 40b4f539 | Kevin Wolf | } |
2988 | 40b4f539 | Kevin Wolf | |
2989 | 40b4f539 | Kevin Wolf | /*
|
2990 | 40b4f539 | Kevin Wolf | * Submit multiple AIO write requests at once.
|
2991 | 40b4f539 | Kevin Wolf | *
|
2992 | 40b4f539 | Kevin Wolf | * On success, the function returns 0 and all requests in the reqs array have
|
2993 | 40b4f539 | Kevin Wolf | * been submitted. In error case this function returns -1, and any of the
|
2994 | 40b4f539 | Kevin Wolf | * requests may or may not be submitted yet. In particular, this means that the
|
2995 | 40b4f539 | Kevin Wolf | * callback will be called for some of the requests, for others it won't. The
|
2996 | 40b4f539 | Kevin Wolf | * caller must check the error field of the BlockRequest to wait for the right
|
2997 | 40b4f539 | Kevin Wolf | * callbacks (if error != 0, no callback will be called).
|
2998 | 40b4f539 | Kevin Wolf | *
|
2999 | 40b4f539 | Kevin Wolf | * The implementation may modify the contents of the reqs array, e.g. to merge
|
3000 | 40b4f539 | Kevin Wolf | * requests. However, the fields opaque and error are left unmodified as they
|
3001 | 40b4f539 | Kevin Wolf | * are used to signal failure for a single request to the caller.
|
3002 | 40b4f539 | Kevin Wolf | */
|
3003 | 40b4f539 | Kevin Wolf | int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs) |
3004 | 40b4f539 | Kevin Wolf | { |
3005 | 40b4f539 | Kevin Wolf | MultiwriteCB *mcb; |
3006 | 40b4f539 | Kevin Wolf | int i;
|
3007 | 40b4f539 | Kevin Wolf | |
3008 | 301db7c2 | Ryan Harper | /* don't submit writes if we don't have a medium */
|
3009 | 301db7c2 | Ryan Harper | if (bs->drv == NULL) { |
3010 | 301db7c2 | Ryan Harper | for (i = 0; i < num_reqs; i++) { |
3011 | 301db7c2 | Ryan Harper | reqs[i].error = -ENOMEDIUM; |
3012 | 301db7c2 | Ryan Harper | } |
3013 | 301db7c2 | Ryan Harper | return -1; |
3014 | 301db7c2 | Ryan Harper | } |
3015 | 301db7c2 | Ryan Harper | |
3016 | 40b4f539 | Kevin Wolf | if (num_reqs == 0) { |
3017 | 40b4f539 | Kevin Wolf | return 0; |
3018 | 40b4f539 | Kevin Wolf | } |
3019 | 40b4f539 | Kevin Wolf | |
3020 | 40b4f539 | Kevin Wolf | // Create MultiwriteCB structure
|
3021 | 7267c094 | Anthony Liguori | mcb = g_malloc0(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks)); |
3022 | 40b4f539 | Kevin Wolf | mcb->num_requests = 0;
|
3023 | 40b4f539 | Kevin Wolf | mcb->num_callbacks = num_reqs; |
3024 | 40b4f539 | Kevin Wolf | |
3025 | 40b4f539 | Kevin Wolf | for (i = 0; i < num_reqs; i++) { |
3026 | 40b4f539 | Kevin Wolf | mcb->callbacks[i].cb = reqs[i].cb; |
3027 | 40b4f539 | Kevin Wolf | mcb->callbacks[i].opaque = reqs[i].opaque; |
3028 | 40b4f539 | Kevin Wolf | } |
3029 | 40b4f539 | Kevin Wolf | |
3030 | 40b4f539 | Kevin Wolf | // Check for mergable requests
|
3031 | 40b4f539 | Kevin Wolf | num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb); |
3032 | 40b4f539 | Kevin Wolf | |
3033 | 6d519a5f | Stefan Hajnoczi | trace_bdrv_aio_multiwrite(mcb, mcb->num_callbacks, num_reqs); |
3034 | 6d519a5f | Stefan Hajnoczi | |
3035 | df9309fb | Paolo Bonzini | /* Run the aio requests. */
|
3036 | df9309fb | Paolo Bonzini | mcb->num_requests = num_reqs; |
3037 | 40b4f539 | Kevin Wolf | for (i = 0; i < num_reqs; i++) { |
3038 | ad54ae80 | Paolo Bonzini | bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov, |
3039 | 40b4f539 | Kevin Wolf | reqs[i].nb_sectors, multiwrite_cb, mcb); |
3040 | 40b4f539 | Kevin Wolf | } |
3041 | 40b4f539 | Kevin Wolf | |
3042 | 40b4f539 | Kevin Wolf | return 0; |
3043 | 40b4f539 | Kevin Wolf | } |
3044 | 40b4f539 | Kevin Wolf | |
3045 | 83f64091 | bellard | void bdrv_aio_cancel(BlockDriverAIOCB *acb)
|
3046 | 83f64091 | bellard | { |
3047 | 6bbff9a0 | aliguori | acb->pool->cancel(acb); |
3048 | 83f64091 | bellard | } |
3049 | 83f64091 | bellard | |
3050 | 98f90dba | Zhi Yong Wu | /* block I/O throttling */
|
3051 | 98f90dba | Zhi Yong Wu | static bool bdrv_exceed_bps_limits(BlockDriverState *bs, int nb_sectors, |
3052 | 98f90dba | Zhi Yong Wu | bool is_write, double elapsed_time, uint64_t *wait) |
3053 | 98f90dba | Zhi Yong Wu | { |
3054 | 98f90dba | Zhi Yong Wu | uint64_t bps_limit = 0;
|
3055 | 98f90dba | Zhi Yong Wu | double bytes_limit, bytes_base, bytes_res;
|
3056 | 98f90dba | Zhi Yong Wu | double slice_time, wait_time;
|
3057 | 98f90dba | Zhi Yong Wu | |
3058 | 98f90dba | Zhi Yong Wu | if (bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]) {
|
3059 | 98f90dba | Zhi Yong Wu | bps_limit = bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]; |
3060 | 98f90dba | Zhi Yong Wu | } else if (bs->io_limits.bps[is_write]) { |
3061 | 98f90dba | Zhi Yong Wu | bps_limit = bs->io_limits.bps[is_write]; |
3062 | 98f90dba | Zhi Yong Wu | } else {
|
3063 | 98f90dba | Zhi Yong Wu | if (wait) {
|
3064 | 98f90dba | Zhi Yong Wu | *wait = 0;
|
3065 | 98f90dba | Zhi Yong Wu | } |
3066 | 98f90dba | Zhi Yong Wu | |
3067 | 98f90dba | Zhi Yong Wu | return false; |
3068 | 98f90dba | Zhi Yong Wu | } |
3069 | 98f90dba | Zhi Yong Wu | |
3070 | 98f90dba | Zhi Yong Wu | slice_time = bs->slice_end - bs->slice_start; |
3071 | 98f90dba | Zhi Yong Wu | slice_time /= (NANOSECONDS_PER_SECOND); |
3072 | 98f90dba | Zhi Yong Wu | bytes_limit = bps_limit * slice_time; |
3073 | 98f90dba | Zhi Yong Wu | bytes_base = bs->nr_bytes[is_write] - bs->io_base.bytes[is_write]; |
3074 | 98f90dba | Zhi Yong Wu | if (bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]) {
|
3075 | 98f90dba | Zhi Yong Wu | bytes_base += bs->nr_bytes[!is_write] - bs->io_base.bytes[!is_write]; |
3076 | 98f90dba | Zhi Yong Wu | } |
3077 | 98f90dba | Zhi Yong Wu | |
3078 | 98f90dba | Zhi Yong Wu | /* bytes_base: the bytes of data which have been read/written; and
|
3079 | 98f90dba | Zhi Yong Wu | * it is obtained from the history statistic info.
|
3080 | 98f90dba | Zhi Yong Wu | * bytes_res: the remaining bytes of data which need to be read/written.
|
3081 | 98f90dba | Zhi Yong Wu | * (bytes_base + bytes_res) / bps_limit: used to calcuate
|
3082 | 98f90dba | Zhi Yong Wu | * the total time for completing reading/writting all data.
|
3083 | 98f90dba | Zhi Yong Wu | */
|
3084 | 98f90dba | Zhi Yong Wu | bytes_res = (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
|
3085 | 98f90dba | Zhi Yong Wu | |
3086 | 98f90dba | Zhi Yong Wu | if (bytes_base + bytes_res <= bytes_limit) {
|
3087 | 98f90dba | Zhi Yong Wu | if (wait) {
|
3088 | 98f90dba | Zhi Yong Wu | *wait = 0;
|
3089 | 98f90dba | Zhi Yong Wu | } |
3090 | 98f90dba | Zhi Yong Wu | |
3091 | 98f90dba | Zhi Yong Wu | return false; |
3092 | 98f90dba | Zhi Yong Wu | } |
3093 | 98f90dba | Zhi Yong Wu | |
3094 | 98f90dba | Zhi Yong Wu | /* Calc approx time to dispatch */
|
3095 | 98f90dba | Zhi Yong Wu | wait_time = (bytes_base + bytes_res) / bps_limit - elapsed_time; |
3096 | 98f90dba | Zhi Yong Wu | |
3097 | 98f90dba | Zhi Yong Wu | /* When the I/O rate at runtime exceeds the limits,
|
3098 | 98f90dba | Zhi Yong Wu | * bs->slice_end need to be extended in order that the current statistic
|
3099 | 98f90dba | Zhi Yong Wu | * info can be kept until the timer fire, so it is increased and tuned
|
3100 | 98f90dba | Zhi Yong Wu | * based on the result of experiment.
|
3101 | 98f90dba | Zhi Yong Wu | */
|
3102 | 98f90dba | Zhi Yong Wu | bs->slice_time = wait_time * BLOCK_IO_SLICE_TIME * 10;
|
3103 | 98f90dba | Zhi Yong Wu | bs->slice_end += bs->slice_time - 3 * BLOCK_IO_SLICE_TIME;
|
3104 | 98f90dba | Zhi Yong Wu | if (wait) {
|
3105 | 98f90dba | Zhi Yong Wu | *wait = wait_time * BLOCK_IO_SLICE_TIME * 10;
|
3106 | 98f90dba | Zhi Yong Wu | } |
3107 | 98f90dba | Zhi Yong Wu | |
3108 | 98f90dba | Zhi Yong Wu | return true; |
3109 | 98f90dba | Zhi Yong Wu | } |
3110 | 98f90dba | Zhi Yong Wu | |
3111 | 98f90dba | Zhi Yong Wu | static bool bdrv_exceed_iops_limits(BlockDriverState *bs, bool is_write, |
3112 | 98f90dba | Zhi Yong Wu | double elapsed_time, uint64_t *wait)
|
3113 | 98f90dba | Zhi Yong Wu | { |
3114 | 98f90dba | Zhi Yong Wu | uint64_t iops_limit = 0;
|
3115 | 98f90dba | Zhi Yong Wu | double ios_limit, ios_base;
|
3116 | 98f90dba | Zhi Yong Wu | double slice_time, wait_time;
|
3117 | 98f90dba | Zhi Yong Wu | |
3118 | 98f90dba | Zhi Yong Wu | if (bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]) {
|
3119 | 98f90dba | Zhi Yong Wu | iops_limit = bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]; |
3120 | 98f90dba | Zhi Yong Wu | } else if (bs->io_limits.iops[is_write]) { |
3121 | 98f90dba | Zhi Yong Wu | iops_limit = bs->io_limits.iops[is_write]; |
3122 | 98f90dba | Zhi Yong Wu | } else {
|
3123 | 98f90dba | Zhi Yong Wu | if (wait) {
|
3124 | 98f90dba | Zhi Yong Wu | *wait = 0;
|
3125 | 98f90dba | Zhi Yong Wu | } |
3126 | 98f90dba | Zhi Yong Wu | |
3127 | 98f90dba | Zhi Yong Wu | return false; |
3128 | 98f90dba | Zhi Yong Wu | } |
3129 | 98f90dba | Zhi Yong Wu | |
3130 | 98f90dba | Zhi Yong Wu | slice_time = bs->slice_end - bs->slice_start; |
3131 | 98f90dba | Zhi Yong Wu | slice_time /= (NANOSECONDS_PER_SECOND); |
3132 | 98f90dba | Zhi Yong Wu | ios_limit = iops_limit * slice_time; |
3133 | 98f90dba | Zhi Yong Wu | ios_base = bs->nr_ops[is_write] - bs->io_base.ios[is_write]; |
3134 | 98f90dba | Zhi Yong Wu | if (bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]) {
|
3135 | 98f90dba | Zhi Yong Wu | ios_base += bs->nr_ops[!is_write] - bs->io_base.ios[!is_write]; |
3136 | 98f90dba | Zhi Yong Wu | } |
3137 | 98f90dba | Zhi Yong Wu | |
3138 | 98f90dba | Zhi Yong Wu | if (ios_base + 1 <= ios_limit) { |
3139 | 98f90dba | Zhi Yong Wu | if (wait) {
|
3140 | 98f90dba | Zhi Yong Wu | *wait = 0;
|
3141 | 98f90dba | Zhi Yong Wu | } |
3142 | 98f90dba | Zhi Yong Wu | |
3143 | 98f90dba | Zhi Yong Wu | return false; |
3144 | 98f90dba | Zhi Yong Wu | } |
3145 | 98f90dba | Zhi Yong Wu | |
3146 | 98f90dba | Zhi Yong Wu | /* Calc approx time to dispatch */
|
3147 | 98f90dba | Zhi Yong Wu | wait_time = (ios_base + 1) / iops_limit;
|
3148 | 98f90dba | Zhi Yong Wu | if (wait_time > elapsed_time) {
|
3149 | 98f90dba | Zhi Yong Wu | wait_time = wait_time - elapsed_time; |
3150 | 98f90dba | Zhi Yong Wu | } else {
|
3151 | 98f90dba | Zhi Yong Wu | wait_time = 0;
|
3152 | 98f90dba | Zhi Yong Wu | } |
3153 | 98f90dba | Zhi Yong Wu | |
3154 | 98f90dba | Zhi Yong Wu | bs->slice_time = wait_time * BLOCK_IO_SLICE_TIME * 10;
|
3155 | 98f90dba | Zhi Yong Wu | bs->slice_end += bs->slice_time - 3 * BLOCK_IO_SLICE_TIME;
|
3156 | 98f90dba | Zhi Yong Wu | if (wait) {
|
3157 | 98f90dba | Zhi Yong Wu | *wait = wait_time * BLOCK_IO_SLICE_TIME * 10;
|
3158 | 98f90dba | Zhi Yong Wu | } |
3159 | 98f90dba | Zhi Yong Wu | |
3160 | 98f90dba | Zhi Yong Wu | return true; |
3161 | 98f90dba | Zhi Yong Wu | } |
3162 | 98f90dba | Zhi Yong Wu | |
3163 | 98f90dba | Zhi Yong Wu | static bool bdrv_exceed_io_limits(BlockDriverState *bs, int nb_sectors, |
3164 | 98f90dba | Zhi Yong Wu | bool is_write, int64_t *wait)
|
3165 | 98f90dba | Zhi Yong Wu | { |
3166 | 98f90dba | Zhi Yong Wu | int64_t now, max_wait; |
3167 | 98f90dba | Zhi Yong Wu | uint64_t bps_wait = 0, iops_wait = 0; |
3168 | 98f90dba | Zhi Yong Wu | double elapsed_time;
|
3169 | 98f90dba | Zhi Yong Wu | int bps_ret, iops_ret;
|
3170 | 98f90dba | Zhi Yong Wu | |
3171 | 98f90dba | Zhi Yong Wu | now = qemu_get_clock_ns(vm_clock); |
3172 | 98f90dba | Zhi Yong Wu | if ((bs->slice_start < now)
|
3173 | 98f90dba | Zhi Yong Wu | && (bs->slice_end > now)) { |
3174 | 98f90dba | Zhi Yong Wu | bs->slice_end = now + bs->slice_time; |
3175 | 98f90dba | Zhi Yong Wu | } else {
|
3176 | 98f90dba | Zhi Yong Wu | bs->slice_time = 5 * BLOCK_IO_SLICE_TIME;
|
3177 | 98f90dba | Zhi Yong Wu | bs->slice_start = now; |
3178 | 98f90dba | Zhi Yong Wu | bs->slice_end = now + bs->slice_time; |
3179 | 98f90dba | Zhi Yong Wu | |
3180 | 98f90dba | Zhi Yong Wu | bs->io_base.bytes[is_write] = bs->nr_bytes[is_write]; |
3181 | 98f90dba | Zhi Yong Wu | bs->io_base.bytes[!is_write] = bs->nr_bytes[!is_write]; |
3182 | 98f90dba | Zhi Yong Wu | |
3183 | 98f90dba | Zhi Yong Wu | bs->io_base.ios[is_write] = bs->nr_ops[is_write]; |
3184 | 98f90dba | Zhi Yong Wu | bs->io_base.ios[!is_write] = bs->nr_ops[!is_write]; |
3185 | 98f90dba | Zhi Yong Wu | } |
3186 | 98f90dba | Zhi Yong Wu | |
3187 | 98f90dba | Zhi Yong Wu | elapsed_time = now - bs->slice_start; |
3188 | 98f90dba | Zhi Yong Wu | elapsed_time /= (NANOSECONDS_PER_SECOND); |
3189 | 98f90dba | Zhi Yong Wu | |
3190 | 98f90dba | Zhi Yong Wu | bps_ret = bdrv_exceed_bps_limits(bs, nb_sectors, |
3191 | 98f90dba | Zhi Yong Wu | is_write, elapsed_time, &bps_wait); |
3192 | 98f90dba | Zhi Yong Wu | iops_ret = bdrv_exceed_iops_limits(bs, is_write, |
3193 | 98f90dba | Zhi Yong Wu | elapsed_time, &iops_wait); |
3194 | 98f90dba | Zhi Yong Wu | if (bps_ret || iops_ret) {
|
3195 | 98f90dba | Zhi Yong Wu | max_wait = bps_wait > iops_wait ? bps_wait : iops_wait; |
3196 | 98f90dba | Zhi Yong Wu | if (wait) {
|
3197 | 98f90dba | Zhi Yong Wu | *wait = max_wait; |
3198 | 98f90dba | Zhi Yong Wu | } |
3199 | 98f90dba | Zhi Yong Wu | |
3200 | 98f90dba | Zhi Yong Wu | now = qemu_get_clock_ns(vm_clock); |
3201 | 98f90dba | Zhi Yong Wu | if (bs->slice_end < now + max_wait) {
|
3202 | 98f90dba | Zhi Yong Wu | bs->slice_end = now + max_wait; |
3203 | 98f90dba | Zhi Yong Wu | } |
3204 | 98f90dba | Zhi Yong Wu | |
3205 | 98f90dba | Zhi Yong Wu | return true; |
3206 | 98f90dba | Zhi Yong Wu | } |
3207 | 98f90dba | Zhi Yong Wu | |
3208 | 98f90dba | Zhi Yong Wu | if (wait) {
|
3209 | 98f90dba | Zhi Yong Wu | *wait = 0;
|
3210 | 98f90dba | Zhi Yong Wu | } |
3211 | 98f90dba | Zhi Yong Wu | |
3212 | 98f90dba | Zhi Yong Wu | return false; |
3213 | 98f90dba | Zhi Yong Wu | } |
3214 | ce1a14dc | pbrook | |
3215 | 83f64091 | bellard | /**************************************************************/
|
3216 | 83f64091 | bellard | /* async block device emulation */
|
3217 | 83f64091 | bellard | |
3218 | c16b5a2c | Christoph Hellwig | typedef struct BlockDriverAIOCBSync { |
3219 | c16b5a2c | Christoph Hellwig | BlockDriverAIOCB common; |
3220 | c16b5a2c | Christoph Hellwig | QEMUBH *bh; |
3221 | c16b5a2c | Christoph Hellwig | int ret;
|
3222 | c16b5a2c | Christoph Hellwig | /* vector translation state */
|
3223 | c16b5a2c | Christoph Hellwig | QEMUIOVector *qiov; |
3224 | c16b5a2c | Christoph Hellwig | uint8_t *bounce; |
3225 | c16b5a2c | Christoph Hellwig | int is_write;
|
3226 | c16b5a2c | Christoph Hellwig | } BlockDriverAIOCBSync; |
3227 | c16b5a2c | Christoph Hellwig | |
3228 | c16b5a2c | Christoph Hellwig | static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb) |
3229 | c16b5a2c | Christoph Hellwig | { |
3230 | b666d239 | Kevin Wolf | BlockDriverAIOCBSync *acb = |
3231 | b666d239 | Kevin Wolf | container_of(blockacb, BlockDriverAIOCBSync, common); |
3232 | 6a7ad299 | Dor Laor | qemu_bh_delete(acb->bh); |
3233 | 36afc451 | Avi Kivity | acb->bh = NULL;
|
3234 | c16b5a2c | Christoph Hellwig | qemu_aio_release(acb); |
3235 | c16b5a2c | Christoph Hellwig | } |
3236 | c16b5a2c | Christoph Hellwig | |
3237 | c16b5a2c | Christoph Hellwig | static AIOPool bdrv_em_aio_pool = {
|
3238 | c16b5a2c | Christoph Hellwig | .aiocb_size = sizeof(BlockDriverAIOCBSync),
|
3239 | c16b5a2c | Christoph Hellwig | .cancel = bdrv_aio_cancel_em, |
3240 | c16b5a2c | Christoph Hellwig | }; |
3241 | c16b5a2c | Christoph Hellwig | |
3242 | ce1a14dc | pbrook | static void bdrv_aio_bh_cb(void *opaque) |
3243 | 83f64091 | bellard | { |
3244 | ce1a14dc | pbrook | BlockDriverAIOCBSync *acb = opaque; |
3245 | f141eafe | aliguori | |
3246 | f141eafe | aliguori | if (!acb->is_write)
|
3247 | 03396148 | Michael Tokarev | qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size);
|
3248 | ceb42de8 | aliguori | qemu_vfree(acb->bounce); |
3249 | ce1a14dc | pbrook | acb->common.cb(acb->common.opaque, acb->ret); |
3250 | 6a7ad299 | Dor Laor | qemu_bh_delete(acb->bh); |
3251 | 36afc451 | Avi Kivity | acb->bh = NULL;
|
3252 | ce1a14dc | pbrook | qemu_aio_release(acb); |
3253 | 83f64091 | bellard | } |
3254 | beac80cd | bellard | |
3255 | f141eafe | aliguori | static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
|
3256 | f141eafe | aliguori | int64_t sector_num, |
3257 | f141eafe | aliguori | QEMUIOVector *qiov, |
3258 | f141eafe | aliguori | int nb_sectors,
|
3259 | f141eafe | aliguori | BlockDriverCompletionFunc *cb, |
3260 | f141eafe | aliguori | void *opaque,
|
3261 | f141eafe | aliguori | int is_write)
|
3262 | f141eafe | aliguori | |
3263 | 83f64091 | bellard | { |
3264 | ce1a14dc | pbrook | BlockDriverAIOCBSync *acb; |
3265 | ce1a14dc | pbrook | |
3266 | c16b5a2c | Christoph Hellwig | acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); |
3267 | f141eafe | aliguori | acb->is_write = is_write; |
3268 | f141eafe | aliguori | acb->qiov = qiov; |
3269 | e268ca52 | aliguori | acb->bounce = qemu_blockalign(bs, qiov->size); |
3270 | 3f3aace8 | Paolo Bonzini | acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); |
3271 | f141eafe | aliguori | |
3272 | f141eafe | aliguori | if (is_write) {
|
3273 | d5e6b161 | Michael Tokarev | qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size);
|
3274 | 1ed20acf | Stefan Hajnoczi | acb->ret = bs->drv->bdrv_write(bs, sector_num, acb->bounce, nb_sectors); |
3275 | f141eafe | aliguori | } else {
|
3276 | 1ed20acf | Stefan Hajnoczi | acb->ret = bs->drv->bdrv_read(bs, sector_num, acb->bounce, nb_sectors); |
3277 | f141eafe | aliguori | } |
3278 | f141eafe | aliguori | |
3279 | ce1a14dc | pbrook | qemu_bh_schedule(acb->bh); |
3280 | f141eafe | aliguori | |
3281 | ce1a14dc | pbrook | return &acb->common;
|
3282 | beac80cd | bellard | } |
3283 | beac80cd | bellard | |
3284 | f141eafe | aliguori | static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
|
3285 | f141eafe | aliguori | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
3286 | ce1a14dc | pbrook | BlockDriverCompletionFunc *cb, void *opaque)
|
3287 | beac80cd | bellard | { |
3288 | f141eafe | aliguori | return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0); |
3289 | f141eafe | aliguori | } |
3290 | 83f64091 | bellard | |
3291 | f141eafe | aliguori | static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
|
3292 | f141eafe | aliguori | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
3293 | f141eafe | aliguori | BlockDriverCompletionFunc *cb, void *opaque)
|
3294 | f141eafe | aliguori | { |
3295 | f141eafe | aliguori | return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1); |
3296 | beac80cd | bellard | } |
3297 | beac80cd | bellard | |
3298 | 68485420 | Kevin Wolf | |
3299 | 68485420 | Kevin Wolf | typedef struct BlockDriverAIOCBCoroutine { |
3300 | 68485420 | Kevin Wolf | BlockDriverAIOCB common; |
3301 | 68485420 | Kevin Wolf | BlockRequest req; |
3302 | 68485420 | Kevin Wolf | bool is_write;
|
3303 | 68485420 | Kevin Wolf | QEMUBH* bh; |
3304 | 68485420 | Kevin Wolf | } BlockDriverAIOCBCoroutine; |
3305 | 68485420 | Kevin Wolf | |
3306 | 68485420 | Kevin Wolf | static void bdrv_aio_co_cancel_em(BlockDriverAIOCB *blockacb) |
3307 | 68485420 | Kevin Wolf | { |
3308 | 68485420 | Kevin Wolf | qemu_aio_flush(); |
3309 | 68485420 | Kevin Wolf | } |
3310 | 68485420 | Kevin Wolf | |
3311 | 68485420 | Kevin Wolf | static AIOPool bdrv_em_co_aio_pool = {
|
3312 | 68485420 | Kevin Wolf | .aiocb_size = sizeof(BlockDriverAIOCBCoroutine),
|
3313 | 68485420 | Kevin Wolf | .cancel = bdrv_aio_co_cancel_em, |
3314 | 68485420 | Kevin Wolf | }; |
3315 | 68485420 | Kevin Wolf | |
3316 | 35246a68 | Paolo Bonzini | static void bdrv_co_em_bh(void *opaque) |
3317 | 68485420 | Kevin Wolf | { |
3318 | 68485420 | Kevin Wolf | BlockDriverAIOCBCoroutine *acb = opaque; |
3319 | 68485420 | Kevin Wolf | |
3320 | 68485420 | Kevin Wolf | acb->common.cb(acb->common.opaque, acb->req.error); |
3321 | 68485420 | Kevin Wolf | qemu_bh_delete(acb->bh); |
3322 | 68485420 | Kevin Wolf | qemu_aio_release(acb); |
3323 | 68485420 | Kevin Wolf | } |
3324 | 68485420 | Kevin Wolf | |
3325 | b2a61371 | Stefan Hajnoczi | /* Invoke bdrv_co_do_readv/bdrv_co_do_writev */
|
3326 | b2a61371 | Stefan Hajnoczi | static void coroutine_fn bdrv_co_do_rw(void *opaque) |
3327 | b2a61371 | Stefan Hajnoczi | { |
3328 | b2a61371 | Stefan Hajnoczi | BlockDriverAIOCBCoroutine *acb = opaque; |
3329 | b2a61371 | Stefan Hajnoczi | BlockDriverState *bs = acb->common.bs; |
3330 | b2a61371 | Stefan Hajnoczi | |
3331 | b2a61371 | Stefan Hajnoczi | if (!acb->is_write) {
|
3332 | b2a61371 | Stefan Hajnoczi | acb->req.error = bdrv_co_do_readv(bs, acb->req.sector, |
3333 | 470c0504 | Stefan Hajnoczi | acb->req.nb_sectors, acb->req.qiov, 0);
|
3334 | b2a61371 | Stefan Hajnoczi | } else {
|
3335 | b2a61371 | Stefan Hajnoczi | acb->req.error = bdrv_co_do_writev(bs, acb->req.sector, |
3336 | f08f2dda | Stefan Hajnoczi | acb->req.nb_sectors, acb->req.qiov, 0);
|
3337 | b2a61371 | Stefan Hajnoczi | } |
3338 | b2a61371 | Stefan Hajnoczi | |
3339 | 35246a68 | Paolo Bonzini | acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); |
3340 | b2a61371 | Stefan Hajnoczi | qemu_bh_schedule(acb->bh); |
3341 | b2a61371 | Stefan Hajnoczi | } |
3342 | b2a61371 | Stefan Hajnoczi | |
3343 | 68485420 | Kevin Wolf | static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs,
|
3344 | 68485420 | Kevin Wolf | int64_t sector_num, |
3345 | 68485420 | Kevin Wolf | QEMUIOVector *qiov, |
3346 | 68485420 | Kevin Wolf | int nb_sectors,
|
3347 | 68485420 | Kevin Wolf | BlockDriverCompletionFunc *cb, |
3348 | 68485420 | Kevin Wolf | void *opaque,
|
3349 | 8c5873d6 | Stefan Hajnoczi | bool is_write)
|
3350 | 68485420 | Kevin Wolf | { |
3351 | 68485420 | Kevin Wolf | Coroutine *co; |
3352 | 68485420 | Kevin Wolf | BlockDriverAIOCBCoroutine *acb; |
3353 | 68485420 | Kevin Wolf | |
3354 | 68485420 | Kevin Wolf | acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque); |
3355 | 68485420 | Kevin Wolf | acb->req.sector = sector_num; |
3356 | 68485420 | Kevin Wolf | acb->req.nb_sectors = nb_sectors; |
3357 | 68485420 | Kevin Wolf | acb->req.qiov = qiov; |
3358 | 68485420 | Kevin Wolf | acb->is_write = is_write; |
3359 | 68485420 | Kevin Wolf | |
3360 | 8c5873d6 | Stefan Hajnoczi | co = qemu_coroutine_create(bdrv_co_do_rw); |
3361 | 68485420 | Kevin Wolf | qemu_coroutine_enter(co, acb); |
3362 | 68485420 | Kevin Wolf | |
3363 | 68485420 | Kevin Wolf | return &acb->common;
|
3364 | 68485420 | Kevin Wolf | } |
3365 | 68485420 | Kevin Wolf | |
3366 | 07f07615 | Paolo Bonzini | static void coroutine_fn bdrv_aio_flush_co_entry(void *opaque) |
3367 | b2e12bc6 | Christoph Hellwig | { |
3368 | 07f07615 | Paolo Bonzini | BlockDriverAIOCBCoroutine *acb = opaque; |
3369 | 07f07615 | Paolo Bonzini | BlockDriverState *bs = acb->common.bs; |
3370 | b2e12bc6 | Christoph Hellwig | |
3371 | 07f07615 | Paolo Bonzini | acb->req.error = bdrv_co_flush(bs); |
3372 | 07f07615 | Paolo Bonzini | acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); |
3373 | b2e12bc6 | Christoph Hellwig | qemu_bh_schedule(acb->bh); |
3374 | b2e12bc6 | Christoph Hellwig | } |
3375 | b2e12bc6 | Christoph Hellwig | |
3376 | 07f07615 | Paolo Bonzini | BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs, |
3377 | 016f5cf6 | Alexander Graf | BlockDriverCompletionFunc *cb, void *opaque)
|
3378 | 016f5cf6 | Alexander Graf | { |
3379 | 07f07615 | Paolo Bonzini | trace_bdrv_aio_flush(bs, opaque); |
3380 | 016f5cf6 | Alexander Graf | |
3381 | 07f07615 | Paolo Bonzini | Coroutine *co; |
3382 | 07f07615 | Paolo Bonzini | BlockDriverAIOCBCoroutine *acb; |
3383 | 016f5cf6 | Alexander Graf | |
3384 | 07f07615 | Paolo Bonzini | acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque); |
3385 | 07f07615 | Paolo Bonzini | co = qemu_coroutine_create(bdrv_aio_flush_co_entry); |
3386 | 07f07615 | Paolo Bonzini | qemu_coroutine_enter(co, acb); |
3387 | 016f5cf6 | Alexander Graf | |
3388 | 016f5cf6 | Alexander Graf | return &acb->common;
|
3389 | 016f5cf6 | Alexander Graf | } |
3390 | 016f5cf6 | Alexander Graf | |
3391 | 4265d620 | Paolo Bonzini | static void coroutine_fn bdrv_aio_discard_co_entry(void *opaque) |
3392 | 4265d620 | Paolo Bonzini | { |
3393 | 4265d620 | Paolo Bonzini | BlockDriverAIOCBCoroutine *acb = opaque; |
3394 | 4265d620 | Paolo Bonzini | BlockDriverState *bs = acb->common.bs; |
3395 | 4265d620 | Paolo Bonzini | |
3396 | 4265d620 | Paolo Bonzini | acb->req.error = bdrv_co_discard(bs, acb->req.sector, acb->req.nb_sectors); |
3397 | 4265d620 | Paolo Bonzini | acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); |
3398 | 4265d620 | Paolo Bonzini | qemu_bh_schedule(acb->bh); |
3399 | 4265d620 | Paolo Bonzini | } |
3400 | 4265d620 | Paolo Bonzini | |
3401 | 4265d620 | Paolo Bonzini | BlockDriverAIOCB *bdrv_aio_discard(BlockDriverState *bs, |
3402 | 4265d620 | Paolo Bonzini | int64_t sector_num, int nb_sectors,
|
3403 | 4265d620 | Paolo Bonzini | BlockDriverCompletionFunc *cb, void *opaque)
|
3404 | 4265d620 | Paolo Bonzini | { |
3405 | 4265d620 | Paolo Bonzini | Coroutine *co; |
3406 | 4265d620 | Paolo Bonzini | BlockDriverAIOCBCoroutine *acb; |
3407 | 4265d620 | Paolo Bonzini | |
3408 | 4265d620 | Paolo Bonzini | trace_bdrv_aio_discard(bs, sector_num, nb_sectors, opaque); |
3409 | 4265d620 | Paolo Bonzini | |
3410 | 4265d620 | Paolo Bonzini | acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque); |
3411 | 4265d620 | Paolo Bonzini | acb->req.sector = sector_num; |
3412 | 4265d620 | Paolo Bonzini | acb->req.nb_sectors = nb_sectors; |
3413 | 4265d620 | Paolo Bonzini | co = qemu_coroutine_create(bdrv_aio_discard_co_entry); |
3414 | 4265d620 | Paolo Bonzini | qemu_coroutine_enter(co, acb); |
3415 | 4265d620 | Paolo Bonzini | |
3416 | 4265d620 | Paolo Bonzini | return &acb->common;
|
3417 | 4265d620 | Paolo Bonzini | } |
3418 | 4265d620 | Paolo Bonzini | |
3419 | ea2384d3 | bellard | void bdrv_init(void) |
3420 | ea2384d3 | bellard | { |
3421 | 5efa9d5a | Anthony Liguori | module_call_init(MODULE_INIT_BLOCK); |
3422 | ea2384d3 | bellard | } |
3423 | ce1a14dc | pbrook | |
3424 | eb852011 | Markus Armbruster | void bdrv_init_with_whitelist(void) |
3425 | eb852011 | Markus Armbruster | { |
3426 | eb852011 | Markus Armbruster | use_bdrv_whitelist = 1;
|
3427 | eb852011 | Markus Armbruster | bdrv_init(); |
3428 | eb852011 | Markus Armbruster | } |
3429 | eb852011 | Markus Armbruster | |
3430 | c16b5a2c | Christoph Hellwig | void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
|
3431 | c16b5a2c | Christoph Hellwig | BlockDriverCompletionFunc *cb, void *opaque)
|
3432 | ce1a14dc | pbrook | { |
3433 | ce1a14dc | pbrook | BlockDriverAIOCB *acb; |
3434 | ce1a14dc | pbrook | |
3435 | 6bbff9a0 | aliguori | if (pool->free_aiocb) {
|
3436 | 6bbff9a0 | aliguori | acb = pool->free_aiocb; |
3437 | 6bbff9a0 | aliguori | pool->free_aiocb = acb->next; |
3438 | ce1a14dc | pbrook | } else {
|
3439 | 7267c094 | Anthony Liguori | acb = g_malloc0(pool->aiocb_size); |
3440 | 6bbff9a0 | aliguori | acb->pool = pool; |
3441 | ce1a14dc | pbrook | } |
3442 | ce1a14dc | pbrook | acb->bs = bs; |
3443 | ce1a14dc | pbrook | acb->cb = cb; |
3444 | ce1a14dc | pbrook | acb->opaque = opaque; |
3445 | ce1a14dc | pbrook | return acb;
|
3446 | ce1a14dc | pbrook | } |
3447 | ce1a14dc | pbrook | |
3448 | ce1a14dc | pbrook | void qemu_aio_release(void *p) |
3449 | ce1a14dc | pbrook | { |
3450 | 6bbff9a0 | aliguori | BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p; |
3451 | 6bbff9a0 | aliguori | AIOPool *pool = acb->pool; |
3452 | 6bbff9a0 | aliguori | acb->next = pool->free_aiocb; |
3453 | 6bbff9a0 | aliguori | pool->free_aiocb = acb; |
3454 | ce1a14dc | pbrook | } |
3455 | 19cb3738 | bellard | |
3456 | 19cb3738 | bellard | /**************************************************************/
|
3457 | f9f05dc5 | Kevin Wolf | /* Coroutine block device emulation */
|
3458 | f9f05dc5 | Kevin Wolf | |
3459 | f9f05dc5 | Kevin Wolf | typedef struct CoroutineIOCompletion { |
3460 | f9f05dc5 | Kevin Wolf | Coroutine *coroutine; |
3461 | f9f05dc5 | Kevin Wolf | int ret;
|
3462 | f9f05dc5 | Kevin Wolf | } CoroutineIOCompletion; |
3463 | f9f05dc5 | Kevin Wolf | |
3464 | f9f05dc5 | Kevin Wolf | static void bdrv_co_io_em_complete(void *opaque, int ret) |
3465 | f9f05dc5 | Kevin Wolf | { |
3466 | f9f05dc5 | Kevin Wolf | CoroutineIOCompletion *co = opaque; |
3467 | f9f05dc5 | Kevin Wolf | |
3468 | f9f05dc5 | Kevin Wolf | co->ret = ret; |
3469 | f9f05dc5 | Kevin Wolf | qemu_coroutine_enter(co->coroutine, NULL);
|
3470 | f9f05dc5 | Kevin Wolf | } |
3471 | f9f05dc5 | Kevin Wolf | |
3472 | f9f05dc5 | Kevin Wolf | static int coroutine_fn bdrv_co_io_em(BlockDriverState *bs, int64_t sector_num, |
3473 | f9f05dc5 | Kevin Wolf | int nb_sectors, QEMUIOVector *iov,
|
3474 | f9f05dc5 | Kevin Wolf | bool is_write)
|
3475 | f9f05dc5 | Kevin Wolf | { |
3476 | f9f05dc5 | Kevin Wolf | CoroutineIOCompletion co = { |
3477 | f9f05dc5 | Kevin Wolf | .coroutine = qemu_coroutine_self(), |
3478 | f9f05dc5 | Kevin Wolf | }; |
3479 | f9f05dc5 | Kevin Wolf | BlockDriverAIOCB *acb; |
3480 | f9f05dc5 | Kevin Wolf | |
3481 | f9f05dc5 | Kevin Wolf | if (is_write) {
|
3482 | a652d160 | Stefan Hajnoczi | acb = bs->drv->bdrv_aio_writev(bs, sector_num, iov, nb_sectors, |
3483 | a652d160 | Stefan Hajnoczi | bdrv_co_io_em_complete, &co); |
3484 | f9f05dc5 | Kevin Wolf | } else {
|
3485 | a652d160 | Stefan Hajnoczi | acb = bs->drv->bdrv_aio_readv(bs, sector_num, iov, nb_sectors, |
3486 | a652d160 | Stefan Hajnoczi | bdrv_co_io_em_complete, &co); |
3487 | f9f05dc5 | Kevin Wolf | } |
3488 | f9f05dc5 | Kevin Wolf | |
3489 | 59370aaa | Stefan Hajnoczi | trace_bdrv_co_io_em(bs, sector_num, nb_sectors, is_write, acb); |
3490 | f9f05dc5 | Kevin Wolf | if (!acb) {
|
3491 | f9f05dc5 | Kevin Wolf | return -EIO;
|
3492 | f9f05dc5 | Kevin Wolf | } |
3493 | f9f05dc5 | Kevin Wolf | qemu_coroutine_yield(); |
3494 | f9f05dc5 | Kevin Wolf | |
3495 | f9f05dc5 | Kevin Wolf | return co.ret;
|
3496 | f9f05dc5 | Kevin Wolf | } |
3497 | f9f05dc5 | Kevin Wolf | |
3498 | f9f05dc5 | Kevin Wolf | static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs, |
3499 | f9f05dc5 | Kevin Wolf | int64_t sector_num, int nb_sectors,
|
3500 | f9f05dc5 | Kevin Wolf | QEMUIOVector *iov) |
3501 | f9f05dc5 | Kevin Wolf | { |
3502 | f9f05dc5 | Kevin Wolf | return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, false); |
3503 | f9f05dc5 | Kevin Wolf | } |
3504 | f9f05dc5 | Kevin Wolf | |
3505 | f9f05dc5 | Kevin Wolf | static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs, |
3506 | f9f05dc5 | Kevin Wolf | int64_t sector_num, int nb_sectors,
|
3507 | f9f05dc5 | Kevin Wolf | QEMUIOVector *iov) |
3508 | f9f05dc5 | Kevin Wolf | { |
3509 | f9f05dc5 | Kevin Wolf | return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, true); |
3510 | f9f05dc5 | Kevin Wolf | } |
3511 | f9f05dc5 | Kevin Wolf | |
3512 | 07f07615 | Paolo Bonzini | static void coroutine_fn bdrv_flush_co_entry(void *opaque) |
3513 | e7a8a783 | Kevin Wolf | { |
3514 | 07f07615 | Paolo Bonzini | RwCo *rwco = opaque; |
3515 | 07f07615 | Paolo Bonzini | |
3516 | 07f07615 | Paolo Bonzini | rwco->ret = bdrv_co_flush(rwco->bs); |
3517 | 07f07615 | Paolo Bonzini | } |
3518 | 07f07615 | Paolo Bonzini | |
3519 | 07f07615 | Paolo Bonzini | int coroutine_fn bdrv_co_flush(BlockDriverState *bs)
|
3520 | 07f07615 | Paolo Bonzini | { |
3521 | eb489bb1 | Kevin Wolf | int ret;
|
3522 | eb489bb1 | Kevin Wolf | |
3523 | 29cdb251 | Paolo Bonzini | if (!bs || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
|
3524 | 07f07615 | Paolo Bonzini | return 0; |
3525 | eb489bb1 | Kevin Wolf | } |
3526 | eb489bb1 | Kevin Wolf | |
3527 | ca716364 | Kevin Wolf | /* Write back cached data to the OS even with cache=unsafe */
|
3528 | eb489bb1 | Kevin Wolf | if (bs->drv->bdrv_co_flush_to_os) {
|
3529 | eb489bb1 | Kevin Wolf | ret = bs->drv->bdrv_co_flush_to_os(bs); |
3530 | eb489bb1 | Kevin Wolf | if (ret < 0) { |
3531 | eb489bb1 | Kevin Wolf | return ret;
|
3532 | eb489bb1 | Kevin Wolf | } |
3533 | eb489bb1 | Kevin Wolf | } |
3534 | eb489bb1 | Kevin Wolf | |
3535 | ca716364 | Kevin Wolf | /* But don't actually force it to the disk with cache=unsafe */
|
3536 | ca716364 | Kevin Wolf | if (bs->open_flags & BDRV_O_NO_FLUSH) {
|
3537 | d4c82329 | Kevin Wolf | goto flush_parent;
|
3538 | ca716364 | Kevin Wolf | } |
3539 | ca716364 | Kevin Wolf | |
3540 | eb489bb1 | Kevin Wolf | if (bs->drv->bdrv_co_flush_to_disk) {
|
3541 | 29cdb251 | Paolo Bonzini | ret = bs->drv->bdrv_co_flush_to_disk(bs); |
3542 | 07f07615 | Paolo Bonzini | } else if (bs->drv->bdrv_aio_flush) { |
3543 | 07f07615 | Paolo Bonzini | BlockDriverAIOCB *acb; |
3544 | 07f07615 | Paolo Bonzini | CoroutineIOCompletion co = { |
3545 | 07f07615 | Paolo Bonzini | .coroutine = qemu_coroutine_self(), |
3546 | 07f07615 | Paolo Bonzini | }; |
3547 | 07f07615 | Paolo Bonzini | |
3548 | 07f07615 | Paolo Bonzini | acb = bs->drv->bdrv_aio_flush(bs, bdrv_co_io_em_complete, &co); |
3549 | 07f07615 | Paolo Bonzini | if (acb == NULL) { |
3550 | 29cdb251 | Paolo Bonzini | ret = -EIO; |
3551 | 07f07615 | Paolo Bonzini | } else {
|
3552 | 07f07615 | Paolo Bonzini | qemu_coroutine_yield(); |
3553 | 29cdb251 | Paolo Bonzini | ret = co.ret; |
3554 | 07f07615 | Paolo Bonzini | } |
3555 | 07f07615 | Paolo Bonzini | } else {
|
3556 | 07f07615 | Paolo Bonzini | /*
|
3557 | 07f07615 | Paolo Bonzini | * Some block drivers always operate in either writethrough or unsafe
|
3558 | 07f07615 | Paolo Bonzini | * mode and don't support bdrv_flush therefore. Usually qemu doesn't
|
3559 | 07f07615 | Paolo Bonzini | * know how the server works (because the behaviour is hardcoded or
|
3560 | 07f07615 | Paolo Bonzini | * depends on server-side configuration), so we can't ensure that
|
3561 | 07f07615 | Paolo Bonzini | * everything is safe on disk. Returning an error doesn't work because
|
3562 | 07f07615 | Paolo Bonzini | * that would break guests even if the server operates in writethrough
|
3563 | 07f07615 | Paolo Bonzini | * mode.
|
3564 | 07f07615 | Paolo Bonzini | *
|
3565 | 07f07615 | Paolo Bonzini | * Let's hope the user knows what he's doing.
|
3566 | 07f07615 | Paolo Bonzini | */
|
3567 | 29cdb251 | Paolo Bonzini | ret = 0;
|
3568 | 07f07615 | Paolo Bonzini | } |
3569 | 29cdb251 | Paolo Bonzini | if (ret < 0) { |
3570 | 29cdb251 | Paolo Bonzini | return ret;
|
3571 | 29cdb251 | Paolo Bonzini | } |
3572 | 29cdb251 | Paolo Bonzini | |
3573 | 29cdb251 | Paolo Bonzini | /* Now flush the underlying protocol. It will also have BDRV_O_NO_FLUSH
|
3574 | 29cdb251 | Paolo Bonzini | * in the case of cache=unsafe, so there are no useless flushes.
|
3575 | 29cdb251 | Paolo Bonzini | */
|
3576 | d4c82329 | Kevin Wolf | flush_parent:
|
3577 | 29cdb251 | Paolo Bonzini | return bdrv_co_flush(bs->file);
|
3578 | 07f07615 | Paolo Bonzini | } |
3579 | 07f07615 | Paolo Bonzini | |
3580 | 0f15423c | Anthony Liguori | void bdrv_invalidate_cache(BlockDriverState *bs)
|
3581 | 0f15423c | Anthony Liguori | { |
3582 | 0f15423c | Anthony Liguori | if (bs->drv && bs->drv->bdrv_invalidate_cache) {
|
3583 | 0f15423c | Anthony Liguori | bs->drv->bdrv_invalidate_cache(bs); |
3584 | 0f15423c | Anthony Liguori | } |
3585 | 0f15423c | Anthony Liguori | } |
3586 | 0f15423c | Anthony Liguori | |
3587 | 0f15423c | Anthony Liguori | void bdrv_invalidate_cache_all(void) |
3588 | 0f15423c | Anthony Liguori | { |
3589 | 0f15423c | Anthony Liguori | BlockDriverState *bs; |
3590 | 0f15423c | Anthony Liguori | |
3591 | 0f15423c | Anthony Liguori | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
3592 | 0f15423c | Anthony Liguori | bdrv_invalidate_cache(bs); |
3593 | 0f15423c | Anthony Liguori | } |
3594 | 0f15423c | Anthony Liguori | } |
3595 | 0f15423c | Anthony Liguori | |
3596 | 07789269 | Benoรฎt Canet | void bdrv_clear_incoming_migration_all(void) |
3597 | 07789269 | Benoรฎt Canet | { |
3598 | 07789269 | Benoรฎt Canet | BlockDriverState *bs; |
3599 | 07789269 | Benoรฎt Canet | |
3600 | 07789269 | Benoรฎt Canet | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
3601 | 07789269 | Benoรฎt Canet | bs->open_flags = bs->open_flags & ~(BDRV_O_INCOMING); |
3602 | 07789269 | Benoรฎt Canet | } |
3603 | 07789269 | Benoรฎt Canet | } |
3604 | 07789269 | Benoรฎt Canet | |
3605 | 07f07615 | Paolo Bonzini | int bdrv_flush(BlockDriverState *bs)
|
3606 | 07f07615 | Paolo Bonzini | { |
3607 | 07f07615 | Paolo Bonzini | Coroutine *co; |
3608 | 07f07615 | Paolo Bonzini | RwCo rwco = { |
3609 | 07f07615 | Paolo Bonzini | .bs = bs, |
3610 | 07f07615 | Paolo Bonzini | .ret = NOT_DONE, |
3611 | e7a8a783 | Kevin Wolf | }; |
3612 | e7a8a783 | Kevin Wolf | |
3613 | 07f07615 | Paolo Bonzini | if (qemu_in_coroutine()) {
|
3614 | 07f07615 | Paolo Bonzini | /* Fast-path if already in coroutine context */
|
3615 | 07f07615 | Paolo Bonzini | bdrv_flush_co_entry(&rwco); |
3616 | 07f07615 | Paolo Bonzini | } else {
|
3617 | 07f07615 | Paolo Bonzini | co = qemu_coroutine_create(bdrv_flush_co_entry); |
3618 | 07f07615 | Paolo Bonzini | qemu_coroutine_enter(co, &rwco); |
3619 | 07f07615 | Paolo Bonzini | while (rwco.ret == NOT_DONE) {
|
3620 | 07f07615 | Paolo Bonzini | qemu_aio_wait(); |
3621 | 07f07615 | Paolo Bonzini | } |
3622 | e7a8a783 | Kevin Wolf | } |
3623 | 07f07615 | Paolo Bonzini | |
3624 | 07f07615 | Paolo Bonzini | return rwco.ret;
|
3625 | e7a8a783 | Kevin Wolf | } |
3626 | e7a8a783 | Kevin Wolf | |
3627 | 4265d620 | Paolo Bonzini | static void coroutine_fn bdrv_discard_co_entry(void *opaque) |
3628 | 4265d620 | Paolo Bonzini | { |
3629 | 4265d620 | Paolo Bonzini | RwCo *rwco = opaque; |
3630 | 4265d620 | Paolo Bonzini | |
3631 | 4265d620 | Paolo Bonzini | rwco->ret = bdrv_co_discard(rwco->bs, rwco->sector_num, rwco->nb_sectors); |
3632 | 4265d620 | Paolo Bonzini | } |
3633 | 4265d620 | Paolo Bonzini | |
3634 | 4265d620 | Paolo Bonzini | int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num,
|
3635 | 4265d620 | Paolo Bonzini | int nb_sectors)
|
3636 | 4265d620 | Paolo Bonzini | { |
3637 | 4265d620 | Paolo Bonzini | if (!bs->drv) {
|
3638 | 4265d620 | Paolo Bonzini | return -ENOMEDIUM;
|
3639 | 4265d620 | Paolo Bonzini | } else if (bdrv_check_request(bs, sector_num, nb_sectors)) { |
3640 | 4265d620 | Paolo Bonzini | return -EIO;
|
3641 | 4265d620 | Paolo Bonzini | } else if (bs->read_only) { |
3642 | 4265d620 | Paolo Bonzini | return -EROFS;
|
3643 | 4265d620 | Paolo Bonzini | } else if (bs->drv->bdrv_co_discard) { |
3644 | 4265d620 | Paolo Bonzini | return bs->drv->bdrv_co_discard(bs, sector_num, nb_sectors);
|
3645 | 4265d620 | Paolo Bonzini | } else if (bs->drv->bdrv_aio_discard) { |
3646 | 4265d620 | Paolo Bonzini | BlockDriverAIOCB *acb; |
3647 | 4265d620 | Paolo Bonzini | CoroutineIOCompletion co = { |
3648 | 4265d620 | Paolo Bonzini | .coroutine = qemu_coroutine_self(), |
3649 | 4265d620 | Paolo Bonzini | }; |
3650 | 4265d620 | Paolo Bonzini | |
3651 | 4265d620 | Paolo Bonzini | acb = bs->drv->bdrv_aio_discard(bs, sector_num, nb_sectors, |
3652 | 4265d620 | Paolo Bonzini | bdrv_co_io_em_complete, &co); |
3653 | 4265d620 | Paolo Bonzini | if (acb == NULL) { |
3654 | 4265d620 | Paolo Bonzini | return -EIO;
|
3655 | 4265d620 | Paolo Bonzini | } else {
|
3656 | 4265d620 | Paolo Bonzini | qemu_coroutine_yield(); |
3657 | 4265d620 | Paolo Bonzini | return co.ret;
|
3658 | 4265d620 | Paolo Bonzini | } |
3659 | 4265d620 | Paolo Bonzini | } else {
|
3660 | 4265d620 | Paolo Bonzini | return 0; |
3661 | 4265d620 | Paolo Bonzini | } |
3662 | 4265d620 | Paolo Bonzini | } |
3663 | 4265d620 | Paolo Bonzini | |
3664 | 4265d620 | Paolo Bonzini | int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors) |
3665 | 4265d620 | Paolo Bonzini | { |
3666 | 4265d620 | Paolo Bonzini | Coroutine *co; |
3667 | 4265d620 | Paolo Bonzini | RwCo rwco = { |
3668 | 4265d620 | Paolo Bonzini | .bs = bs, |
3669 | 4265d620 | Paolo Bonzini | .sector_num = sector_num, |
3670 | 4265d620 | Paolo Bonzini | .nb_sectors = nb_sectors, |
3671 | 4265d620 | Paolo Bonzini | .ret = NOT_DONE, |
3672 | 4265d620 | Paolo Bonzini | }; |
3673 | 4265d620 | Paolo Bonzini | |
3674 | 4265d620 | Paolo Bonzini | if (qemu_in_coroutine()) {
|
3675 | 4265d620 | Paolo Bonzini | /* Fast-path if already in coroutine context */
|
3676 | 4265d620 | Paolo Bonzini | bdrv_discard_co_entry(&rwco); |
3677 | 4265d620 | Paolo Bonzini | } else {
|
3678 | 4265d620 | Paolo Bonzini | co = qemu_coroutine_create(bdrv_discard_co_entry); |
3679 | 4265d620 | Paolo Bonzini | qemu_coroutine_enter(co, &rwco); |
3680 | 4265d620 | Paolo Bonzini | while (rwco.ret == NOT_DONE) {
|
3681 | 4265d620 | Paolo Bonzini | qemu_aio_wait(); |
3682 | 4265d620 | Paolo Bonzini | } |
3683 | 4265d620 | Paolo Bonzini | } |
3684 | 4265d620 | Paolo Bonzini | |
3685 | 4265d620 | Paolo Bonzini | return rwco.ret;
|
3686 | 4265d620 | Paolo Bonzini | } |
3687 | 4265d620 | Paolo Bonzini | |
3688 | f9f05dc5 | Kevin Wolf | /**************************************************************/
|
3689 | 19cb3738 | bellard | /* removable device support */
|
3690 | 19cb3738 | bellard | |
3691 | 19cb3738 | bellard | /**
|
3692 | 19cb3738 | bellard | * Return TRUE if the media is present
|
3693 | 19cb3738 | bellard | */
|
3694 | 19cb3738 | bellard | int bdrv_is_inserted(BlockDriverState *bs)
|
3695 | 19cb3738 | bellard | { |
3696 | 19cb3738 | bellard | BlockDriver *drv = bs->drv; |
3697 | a1aff5bf | Markus Armbruster | |
3698 | 19cb3738 | bellard | if (!drv)
|
3699 | 19cb3738 | bellard | return 0; |
3700 | 19cb3738 | bellard | if (!drv->bdrv_is_inserted)
|
3701 | a1aff5bf | Markus Armbruster | return 1; |
3702 | a1aff5bf | Markus Armbruster | return drv->bdrv_is_inserted(bs);
|
3703 | 19cb3738 | bellard | } |
3704 | 19cb3738 | bellard | |
3705 | 19cb3738 | bellard | /**
|
3706 | 8e49ca46 | Markus Armbruster | * Return whether the media changed since the last call to this
|
3707 | 8e49ca46 | Markus Armbruster | * function, or -ENOTSUP if we don't know. Most drivers don't know.
|
3708 | 19cb3738 | bellard | */
|
3709 | 19cb3738 | bellard | int bdrv_media_changed(BlockDriverState *bs)
|
3710 | 19cb3738 | bellard | { |
3711 | 19cb3738 | bellard | BlockDriver *drv = bs->drv; |
3712 | 19cb3738 | bellard | |
3713 | 8e49ca46 | Markus Armbruster | if (drv && drv->bdrv_media_changed) {
|
3714 | 8e49ca46 | Markus Armbruster | return drv->bdrv_media_changed(bs);
|
3715 | 8e49ca46 | Markus Armbruster | } |
3716 | 8e49ca46 | Markus Armbruster | return -ENOTSUP;
|
3717 | 19cb3738 | bellard | } |
3718 | 19cb3738 | bellard | |
3719 | 19cb3738 | bellard | /**
|
3720 | 19cb3738 | bellard | * If eject_flag is TRUE, eject the media. Otherwise, close the tray
|
3721 | 19cb3738 | bellard | */
|
3722 | f36f3949 | Luiz Capitulino | void bdrv_eject(BlockDriverState *bs, bool eject_flag) |
3723 | 19cb3738 | bellard | { |
3724 | 19cb3738 | bellard | BlockDriver *drv = bs->drv; |
3725 | 19cb3738 | bellard | |
3726 | 822e1cd1 | Markus Armbruster | if (drv && drv->bdrv_eject) {
|
3727 | 822e1cd1 | Markus Armbruster | drv->bdrv_eject(bs, eject_flag); |
3728 | 19cb3738 | bellard | } |
3729 | 6f382ed2 | Luiz Capitulino | |
3730 | 6f382ed2 | Luiz Capitulino | if (bs->device_name[0] != '\0') { |
3731 | 6f382ed2 | Luiz Capitulino | bdrv_emit_qmp_eject_event(bs, eject_flag); |
3732 | 6f382ed2 | Luiz Capitulino | } |
3733 | 19cb3738 | bellard | } |
3734 | 19cb3738 | bellard | |
3735 | 19cb3738 | bellard | /**
|
3736 | 19cb3738 | bellard | * Lock or unlock the media (if it is locked, the user won't be able
|
3737 | 19cb3738 | bellard | * to eject it manually).
|
3738 | 19cb3738 | bellard | */
|
3739 | 025e849a | Markus Armbruster | void bdrv_lock_medium(BlockDriverState *bs, bool locked) |
3740 | 19cb3738 | bellard | { |
3741 | 19cb3738 | bellard | BlockDriver *drv = bs->drv; |
3742 | 19cb3738 | bellard | |
3743 | 025e849a | Markus Armbruster | trace_bdrv_lock_medium(bs, locked); |
3744 | b8c6d095 | Stefan Hajnoczi | |
3745 | 025e849a | Markus Armbruster | if (drv && drv->bdrv_lock_medium) {
|
3746 | 025e849a | Markus Armbruster | drv->bdrv_lock_medium(bs, locked); |
3747 | 19cb3738 | bellard | } |
3748 | 19cb3738 | bellard | } |
3749 | 985a03b0 | ths | |
3750 | 985a03b0 | ths | /* needed for generic scsi interface */
|
3751 | 985a03b0 | ths | |
3752 | 985a03b0 | ths | int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) |
3753 | 985a03b0 | ths | { |
3754 | 985a03b0 | ths | BlockDriver *drv = bs->drv; |
3755 | 985a03b0 | ths | |
3756 | 985a03b0 | ths | if (drv && drv->bdrv_ioctl)
|
3757 | 985a03b0 | ths | return drv->bdrv_ioctl(bs, req, buf);
|
3758 | 985a03b0 | ths | return -ENOTSUP;
|
3759 | 985a03b0 | ths | } |
3760 | 7d780669 | aliguori | |
3761 | 221f715d | aliguori | BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs, |
3762 | 221f715d | aliguori | unsigned long int req, void *buf, |
3763 | 221f715d | aliguori | BlockDriverCompletionFunc *cb, void *opaque)
|
3764 | 7d780669 | aliguori | { |
3765 | 221f715d | aliguori | BlockDriver *drv = bs->drv; |
3766 | 7d780669 | aliguori | |
3767 | 221f715d | aliguori | if (drv && drv->bdrv_aio_ioctl)
|
3768 | 221f715d | aliguori | return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
|
3769 | 221f715d | aliguori | return NULL; |
3770 | 7d780669 | aliguori | } |
3771 | e268ca52 | aliguori | |
3772 | 7b6f9300 | Markus Armbruster | void bdrv_set_buffer_alignment(BlockDriverState *bs, int align) |
3773 | 7b6f9300 | Markus Armbruster | { |
3774 | 7b6f9300 | Markus Armbruster | bs->buffer_alignment = align; |
3775 | 7b6f9300 | Markus Armbruster | } |
3776 | 7cd1e32a | lirans@il.ibm.com | |
3777 | e268ca52 | aliguori | void *qemu_blockalign(BlockDriverState *bs, size_t size)
|
3778 | e268ca52 | aliguori | { |
3779 | e268ca52 | aliguori | return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size); |
3780 | e268ca52 | aliguori | } |
3781 | 7cd1e32a | lirans@il.ibm.com | |
3782 | 7cd1e32a | lirans@il.ibm.com | void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable) |
3783 | 7cd1e32a | lirans@il.ibm.com | { |
3784 | 7cd1e32a | lirans@il.ibm.com | int64_t bitmap_size; |
3785 | a55eb92c | Jan Kiszka | |
3786 | aaa0eb75 | Liran Schour | bs->dirty_count = 0;
|
3787 | a55eb92c | Jan Kiszka | if (enable) {
|
3788 | c6d22830 | Jan Kiszka | if (!bs->dirty_bitmap) {
|
3789 | c6d22830 | Jan Kiszka | bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) + |
3790 | 71df14fc | Paolo Bonzini | BDRV_SECTORS_PER_DIRTY_CHUNK * BITS_PER_LONG - 1;
|
3791 | 71df14fc | Paolo Bonzini | bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * BITS_PER_LONG; |
3792 | a55eb92c | Jan Kiszka | |
3793 | 71df14fc | Paolo Bonzini | bs->dirty_bitmap = g_new0(unsigned long, bitmap_size); |
3794 | a55eb92c | Jan Kiszka | } |
3795 | 7cd1e32a | lirans@il.ibm.com | } else {
|
3796 | c6d22830 | Jan Kiszka | if (bs->dirty_bitmap) {
|
3797 | 7267c094 | Anthony Liguori | g_free(bs->dirty_bitmap); |
3798 | c6d22830 | Jan Kiszka | bs->dirty_bitmap = NULL;
|
3799 | a55eb92c | Jan Kiszka | } |
3800 | 7cd1e32a | lirans@il.ibm.com | } |
3801 | 7cd1e32a | lirans@il.ibm.com | } |
3802 | 7cd1e32a | lirans@il.ibm.com | |
3803 | 7cd1e32a | lirans@il.ibm.com | int bdrv_get_dirty(BlockDriverState *bs, int64_t sector)
|
3804 | 7cd1e32a | lirans@il.ibm.com | { |
3805 | 6ea44308 | Jan Kiszka | int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK; |
3806 | a55eb92c | Jan Kiszka | |
3807 | c6d22830 | Jan Kiszka | if (bs->dirty_bitmap &&
|
3808 | c6d22830 | Jan Kiszka | (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) { |
3809 | 6d59fec1 | Marcelo Tosatti | return !!(bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] & |
3810 | 6d59fec1 | Marcelo Tosatti | (1UL << (chunk % (sizeof(unsigned long) * 8)))); |
3811 | 7cd1e32a | lirans@il.ibm.com | } else {
|
3812 | 7cd1e32a | lirans@il.ibm.com | return 0; |
3813 | 7cd1e32a | lirans@il.ibm.com | } |
3814 | 7cd1e32a | lirans@il.ibm.com | } |
3815 | 7cd1e32a | lirans@il.ibm.com | |
3816 | a55eb92c | Jan Kiszka | void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector,
|
3817 | a55eb92c | Jan Kiszka | int nr_sectors)
|
3818 | 7cd1e32a | lirans@il.ibm.com | { |
3819 | 7cd1e32a | lirans@il.ibm.com | set_dirty_bitmap(bs, cur_sector, nr_sectors, 0);
|
3820 | 7cd1e32a | lirans@il.ibm.com | } |
3821 | aaa0eb75 | Liran Schour | |
3822 | aaa0eb75 | Liran Schour | int64_t bdrv_get_dirty_count(BlockDriverState *bs) |
3823 | aaa0eb75 | Liran Schour | { |
3824 | aaa0eb75 | Liran Schour | return bs->dirty_count;
|
3825 | aaa0eb75 | Liran Schour | } |
3826 | f88e1a42 | Jes Sorensen | |
3827 | db593f25 | Marcelo Tosatti | void bdrv_set_in_use(BlockDriverState *bs, int in_use) |
3828 | db593f25 | Marcelo Tosatti | { |
3829 | db593f25 | Marcelo Tosatti | assert(bs->in_use != in_use); |
3830 | db593f25 | Marcelo Tosatti | bs->in_use = in_use; |
3831 | db593f25 | Marcelo Tosatti | } |
3832 | db593f25 | Marcelo Tosatti | |
3833 | db593f25 | Marcelo Tosatti | int bdrv_in_use(BlockDriverState *bs)
|
3834 | db593f25 | Marcelo Tosatti | { |
3835 | db593f25 | Marcelo Tosatti | return bs->in_use;
|
3836 | db593f25 | Marcelo Tosatti | } |
3837 | db593f25 | Marcelo Tosatti | |
3838 | 28a7282a | Luiz Capitulino | void bdrv_iostatus_enable(BlockDriverState *bs)
|
3839 | 28a7282a | Luiz Capitulino | { |
3840 | d6bf279e | Luiz Capitulino | bs->iostatus_enabled = true;
|
3841 | 58e21ef5 | Luiz Capitulino | bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK; |
3842 | 28a7282a | Luiz Capitulino | } |
3843 | 28a7282a | Luiz Capitulino | |
3844 | 28a7282a | Luiz Capitulino | /* The I/O status is only enabled if the drive explicitly
|
3845 | 28a7282a | Luiz Capitulino | * enables it _and_ the VM is configured to stop on errors */
|
3846 | 28a7282a | Luiz Capitulino | bool bdrv_iostatus_is_enabled(const BlockDriverState *bs) |
3847 | 28a7282a | Luiz Capitulino | { |
3848 | d6bf279e | Luiz Capitulino | return (bs->iostatus_enabled &&
|
3849 | 28a7282a | Luiz Capitulino | (bs->on_write_error == BLOCK_ERR_STOP_ENOSPC || |
3850 | 28a7282a | Luiz Capitulino | bs->on_write_error == BLOCK_ERR_STOP_ANY || |
3851 | 28a7282a | Luiz Capitulino | bs->on_read_error == BLOCK_ERR_STOP_ANY)); |
3852 | 28a7282a | Luiz Capitulino | } |
3853 | 28a7282a | Luiz Capitulino | |
3854 | 28a7282a | Luiz Capitulino | void bdrv_iostatus_disable(BlockDriverState *bs)
|
3855 | 28a7282a | Luiz Capitulino | { |
3856 | d6bf279e | Luiz Capitulino | bs->iostatus_enabled = false;
|
3857 | 28a7282a | Luiz Capitulino | } |
3858 | 28a7282a | Luiz Capitulino | |
3859 | 28a7282a | Luiz Capitulino | void bdrv_iostatus_reset(BlockDriverState *bs)
|
3860 | 28a7282a | Luiz Capitulino | { |
3861 | 28a7282a | Luiz Capitulino | if (bdrv_iostatus_is_enabled(bs)) {
|
3862 | 58e21ef5 | Luiz Capitulino | bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK; |
3863 | 28a7282a | Luiz Capitulino | } |
3864 | 28a7282a | Luiz Capitulino | } |
3865 | 28a7282a | Luiz Capitulino | |
3866 | 28a7282a | Luiz Capitulino | /* XXX: Today this is set by device models because it makes the implementation
|
3867 | 28a7282a | Luiz Capitulino | quite simple. However, the block layer knows about the error, so it's
|
3868 | 28a7282a | Luiz Capitulino | possible to implement this without device models being involved */
|
3869 | 28a7282a | Luiz Capitulino | void bdrv_iostatus_set_err(BlockDriverState *bs, int error) |
3870 | 28a7282a | Luiz Capitulino | { |
3871 | 58e21ef5 | Luiz Capitulino | if (bdrv_iostatus_is_enabled(bs) &&
|
3872 | 58e21ef5 | Luiz Capitulino | bs->iostatus == BLOCK_DEVICE_IO_STATUS_OK) { |
3873 | 28a7282a | Luiz Capitulino | assert(error >= 0);
|
3874 | 58e21ef5 | Luiz Capitulino | bs->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE : |
3875 | 58e21ef5 | Luiz Capitulino | BLOCK_DEVICE_IO_STATUS_FAILED; |
3876 | 28a7282a | Luiz Capitulino | } |
3877 | 28a7282a | Luiz Capitulino | } |
3878 | 28a7282a | Luiz Capitulino | |
3879 | a597e79c | Christoph Hellwig | void
|
3880 | a597e79c | Christoph Hellwig | bdrv_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, int64_t bytes, |
3881 | a597e79c | Christoph Hellwig | enum BlockAcctType type)
|
3882 | a597e79c | Christoph Hellwig | { |
3883 | a597e79c | Christoph Hellwig | assert(type < BDRV_MAX_IOTYPE); |
3884 | a597e79c | Christoph Hellwig | |
3885 | a597e79c | Christoph Hellwig | cookie->bytes = bytes; |
3886 | c488c7f6 | Christoph Hellwig | cookie->start_time_ns = get_clock(); |
3887 | a597e79c | Christoph Hellwig | cookie->type = type; |
3888 | a597e79c | Christoph Hellwig | } |
3889 | a597e79c | Christoph Hellwig | |
3890 | a597e79c | Christoph Hellwig | void
|
3891 | a597e79c | Christoph Hellwig | bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) |
3892 | a597e79c | Christoph Hellwig | { |
3893 | a597e79c | Christoph Hellwig | assert(cookie->type < BDRV_MAX_IOTYPE); |
3894 | a597e79c | Christoph Hellwig | |
3895 | a597e79c | Christoph Hellwig | bs->nr_bytes[cookie->type] += cookie->bytes; |
3896 | a597e79c | Christoph Hellwig | bs->nr_ops[cookie->type]++; |
3897 | c488c7f6 | Christoph Hellwig | bs->total_time_ns[cookie->type] += get_clock() - cookie->start_time_ns; |
3898 | a597e79c | Christoph Hellwig | } |
3899 | a597e79c | Christoph Hellwig | |
3900 | f88e1a42 | Jes Sorensen | int bdrv_img_create(const char *filename, const char *fmt, |
3901 | f88e1a42 | Jes Sorensen | const char *base_filename, const char *base_fmt, |
3902 | f88e1a42 | Jes Sorensen | char *options, uint64_t img_size, int flags) |
3903 | f88e1a42 | Jes Sorensen | { |
3904 | f88e1a42 | Jes Sorensen | QEMUOptionParameter *param = NULL, *create_options = NULL; |
3905 | d220894e | Kevin Wolf | QEMUOptionParameter *backing_fmt, *backing_file, *size; |
3906 | f88e1a42 | Jes Sorensen | BlockDriverState *bs = NULL;
|
3907 | f88e1a42 | Jes Sorensen | BlockDriver *drv, *proto_drv; |
3908 | 96df67d1 | Stefan Hajnoczi | BlockDriver *backing_drv = NULL;
|
3909 | f88e1a42 | Jes Sorensen | int ret = 0; |
3910 | f88e1a42 | Jes Sorensen | |
3911 | f88e1a42 | Jes Sorensen | /* Find driver and parse its options */
|
3912 | f88e1a42 | Jes Sorensen | drv = bdrv_find_format(fmt); |
3913 | f88e1a42 | Jes Sorensen | if (!drv) {
|
3914 | f88e1a42 | Jes Sorensen | error_report("Unknown file format '%s'", fmt);
|
3915 | 4f70f249 | Jes Sorensen | ret = -EINVAL; |
3916 | f88e1a42 | Jes Sorensen | goto out;
|
3917 | f88e1a42 | Jes Sorensen | } |
3918 | f88e1a42 | Jes Sorensen | |
3919 | f88e1a42 | Jes Sorensen | proto_drv = bdrv_find_protocol(filename); |
3920 | f88e1a42 | Jes Sorensen | if (!proto_drv) {
|
3921 | f88e1a42 | Jes Sorensen | error_report("Unknown protocol '%s'", filename);
|
3922 | 4f70f249 | Jes Sorensen | ret = -EINVAL; |
3923 | f88e1a42 | Jes Sorensen | goto out;
|
3924 | f88e1a42 | Jes Sorensen | } |
3925 | f88e1a42 | Jes Sorensen | |
3926 | f88e1a42 | Jes Sorensen | create_options = append_option_parameters(create_options, |
3927 | f88e1a42 | Jes Sorensen | drv->create_options); |
3928 | f88e1a42 | Jes Sorensen | create_options = append_option_parameters(create_options, |
3929 | f88e1a42 | Jes Sorensen | proto_drv->create_options); |
3930 | f88e1a42 | Jes Sorensen | |
3931 | f88e1a42 | Jes Sorensen | /* Create parameter list with default values */
|
3932 | f88e1a42 | Jes Sorensen | param = parse_option_parameters("", create_options, param);
|
3933 | f88e1a42 | Jes Sorensen | |
3934 | f88e1a42 | Jes Sorensen | set_option_parameter_int(param, BLOCK_OPT_SIZE, img_size); |
3935 | f88e1a42 | Jes Sorensen | |
3936 | f88e1a42 | Jes Sorensen | /* Parse -o options */
|
3937 | f88e1a42 | Jes Sorensen | if (options) {
|
3938 | f88e1a42 | Jes Sorensen | param = parse_option_parameters(options, create_options, param); |
3939 | f88e1a42 | Jes Sorensen | if (param == NULL) { |
3940 | f88e1a42 | Jes Sorensen | error_report("Invalid options for file format '%s'.", fmt);
|
3941 | 4f70f249 | Jes Sorensen | ret = -EINVAL; |
3942 | f88e1a42 | Jes Sorensen | goto out;
|
3943 | f88e1a42 | Jes Sorensen | } |
3944 | f88e1a42 | Jes Sorensen | } |
3945 | f88e1a42 | Jes Sorensen | |
3946 | f88e1a42 | Jes Sorensen | if (base_filename) {
|
3947 | f88e1a42 | Jes Sorensen | if (set_option_parameter(param, BLOCK_OPT_BACKING_FILE,
|
3948 | f88e1a42 | Jes Sorensen | base_filename)) { |
3949 | f88e1a42 | Jes Sorensen | error_report("Backing file not supported for file format '%s'",
|
3950 | f88e1a42 | Jes Sorensen | fmt); |
3951 | 4f70f249 | Jes Sorensen | ret = -EINVAL; |
3952 | f88e1a42 | Jes Sorensen | goto out;
|
3953 | f88e1a42 | Jes Sorensen | } |
3954 | f88e1a42 | Jes Sorensen | } |
3955 | f88e1a42 | Jes Sorensen | |
3956 | f88e1a42 | Jes Sorensen | if (base_fmt) {
|
3957 | f88e1a42 | Jes Sorensen | if (set_option_parameter(param, BLOCK_OPT_BACKING_FMT, base_fmt)) {
|
3958 | f88e1a42 | Jes Sorensen | error_report("Backing file format not supported for file "
|
3959 | f88e1a42 | Jes Sorensen | "format '%s'", fmt);
|
3960 | 4f70f249 | Jes Sorensen | ret = -EINVAL; |
3961 | f88e1a42 | Jes Sorensen | goto out;
|
3962 | f88e1a42 | Jes Sorensen | } |
3963 | f88e1a42 | Jes Sorensen | } |
3964 | f88e1a42 | Jes Sorensen | |
3965 | 792da93a | Jes Sorensen | backing_file = get_option_parameter(param, BLOCK_OPT_BACKING_FILE); |
3966 | 792da93a | Jes Sorensen | if (backing_file && backing_file->value.s) {
|
3967 | 792da93a | Jes Sorensen | if (!strcmp(filename, backing_file->value.s)) {
|
3968 | 792da93a | Jes Sorensen | error_report("Error: Trying to create an image with the "
|
3969 | 792da93a | Jes Sorensen | "same filename as the backing file");
|
3970 | 4f70f249 | Jes Sorensen | ret = -EINVAL; |
3971 | 792da93a | Jes Sorensen | goto out;
|
3972 | 792da93a | Jes Sorensen | } |
3973 | 792da93a | Jes Sorensen | } |
3974 | 792da93a | Jes Sorensen | |
3975 | f88e1a42 | Jes Sorensen | backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT); |
3976 | f88e1a42 | Jes Sorensen | if (backing_fmt && backing_fmt->value.s) {
|
3977 | 96df67d1 | Stefan Hajnoczi | backing_drv = bdrv_find_format(backing_fmt->value.s); |
3978 | 96df67d1 | Stefan Hajnoczi | if (!backing_drv) {
|
3979 | f88e1a42 | Jes Sorensen | error_report("Unknown backing file format '%s'",
|
3980 | f88e1a42 | Jes Sorensen | backing_fmt->value.s); |
3981 | 4f70f249 | Jes Sorensen | ret = -EINVAL; |
3982 | f88e1a42 | Jes Sorensen | goto out;
|
3983 | f88e1a42 | Jes Sorensen | } |
3984 | f88e1a42 | Jes Sorensen | } |
3985 | f88e1a42 | Jes Sorensen | |
3986 | f88e1a42 | Jes Sorensen | // The size for the image must always be specified, with one exception:
|
3987 | f88e1a42 | Jes Sorensen | // If we are using a backing file, we can obtain the size from there
|
3988 | d220894e | Kevin Wolf | size = get_option_parameter(param, BLOCK_OPT_SIZE); |
3989 | d220894e | Kevin Wolf | if (size && size->value.n == -1) { |
3990 | f88e1a42 | Jes Sorensen | if (backing_file && backing_file->value.s) {
|
3991 | f88e1a42 | Jes Sorensen | uint64_t size; |
3992 | f88e1a42 | Jes Sorensen | char buf[32]; |
3993 | 63090dac | Paolo Bonzini | int back_flags;
|
3994 | 63090dac | Paolo Bonzini | |
3995 | 63090dac | Paolo Bonzini | /* backing files always opened read-only */
|
3996 | 63090dac | Paolo Bonzini | back_flags = |
3997 | 63090dac | Paolo Bonzini | flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); |
3998 | f88e1a42 | Jes Sorensen | |
3999 | f88e1a42 | Jes Sorensen | bs = bdrv_new("");
|
4000 | f88e1a42 | Jes Sorensen | |
4001 | 63090dac | Paolo Bonzini | ret = bdrv_open(bs, backing_file->value.s, back_flags, backing_drv); |
4002 | f88e1a42 | Jes Sorensen | if (ret < 0) { |
4003 | 96df67d1 | Stefan Hajnoczi | error_report("Could not open '%s'", backing_file->value.s);
|
4004 | f88e1a42 | Jes Sorensen | goto out;
|
4005 | f88e1a42 | Jes Sorensen | } |
4006 | f88e1a42 | Jes Sorensen | bdrv_get_geometry(bs, &size); |
4007 | f88e1a42 | Jes Sorensen | size *= 512;
|
4008 | f88e1a42 | Jes Sorensen | |
4009 | f88e1a42 | Jes Sorensen | snprintf(buf, sizeof(buf), "%" PRId64, size); |
4010 | f88e1a42 | Jes Sorensen | set_option_parameter(param, BLOCK_OPT_SIZE, buf); |
4011 | f88e1a42 | Jes Sorensen | } else {
|
4012 | f88e1a42 | Jes Sorensen | error_report("Image creation needs a size parameter");
|
4013 | 4f70f249 | Jes Sorensen | ret = -EINVAL; |
4014 | f88e1a42 | Jes Sorensen | goto out;
|
4015 | f88e1a42 | Jes Sorensen | } |
4016 | f88e1a42 | Jes Sorensen | } |
4017 | f88e1a42 | Jes Sorensen | |
4018 | f88e1a42 | Jes Sorensen | printf("Formatting '%s', fmt=%s ", filename, fmt);
|
4019 | f88e1a42 | Jes Sorensen | print_option_parameters(param); |
4020 | f88e1a42 | Jes Sorensen | puts("");
|
4021 | f88e1a42 | Jes Sorensen | |
4022 | f88e1a42 | Jes Sorensen | ret = bdrv_create(drv, filename, param); |
4023 | f88e1a42 | Jes Sorensen | |
4024 | f88e1a42 | Jes Sorensen | if (ret < 0) { |
4025 | f88e1a42 | Jes Sorensen | if (ret == -ENOTSUP) {
|
4026 | f88e1a42 | Jes Sorensen | error_report("Formatting or formatting option not supported for "
|
4027 | f88e1a42 | Jes Sorensen | "file format '%s'", fmt);
|
4028 | f88e1a42 | Jes Sorensen | } else if (ret == -EFBIG) { |
4029 | f88e1a42 | Jes Sorensen | error_report("The image size is too large for file format '%s'",
|
4030 | f88e1a42 | Jes Sorensen | fmt); |
4031 | f88e1a42 | Jes Sorensen | } else {
|
4032 | f88e1a42 | Jes Sorensen | error_report("%s: error while creating %s: %s", filename, fmt,
|
4033 | f88e1a42 | Jes Sorensen | strerror(-ret)); |
4034 | f88e1a42 | Jes Sorensen | } |
4035 | f88e1a42 | Jes Sorensen | } |
4036 | f88e1a42 | Jes Sorensen | |
4037 | f88e1a42 | Jes Sorensen | out:
|
4038 | f88e1a42 | Jes Sorensen | free_option_parameters(create_options); |
4039 | f88e1a42 | Jes Sorensen | free_option_parameters(param); |
4040 | f88e1a42 | Jes Sorensen | |
4041 | f88e1a42 | Jes Sorensen | if (bs) {
|
4042 | f88e1a42 | Jes Sorensen | bdrv_delete(bs); |
4043 | f88e1a42 | Jes Sorensen | } |
4044 | 4f70f249 | Jes Sorensen | |
4045 | 4f70f249 | Jes Sorensen | return ret;
|
4046 | f88e1a42 | Jes Sorensen | } |
4047 | eeec61f2 | Stefan Hajnoczi | |
4048 | eeec61f2 | Stefan Hajnoczi | void *block_job_create(const BlockJobType *job_type, BlockDriverState *bs, |
4049 | c83c66c3 | Stefan Hajnoczi | int64_t speed, BlockDriverCompletionFunc *cb, |
4050 | c83c66c3 | Stefan Hajnoczi | void *opaque, Error **errp)
|
4051 | eeec61f2 | Stefan Hajnoczi | { |
4052 | eeec61f2 | Stefan Hajnoczi | BlockJob *job; |
4053 | eeec61f2 | Stefan Hajnoczi | |
4054 | eeec61f2 | Stefan Hajnoczi | if (bs->job || bdrv_in_use(bs)) {
|
4055 | fd7f8c65 | Stefan Hajnoczi | error_set(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs)); |
4056 | eeec61f2 | Stefan Hajnoczi | return NULL; |
4057 | eeec61f2 | Stefan Hajnoczi | } |
4058 | eeec61f2 | Stefan Hajnoczi | bdrv_set_in_use(bs, 1);
|
4059 | eeec61f2 | Stefan Hajnoczi | |
4060 | eeec61f2 | Stefan Hajnoczi | job = g_malloc0(job_type->instance_size); |
4061 | eeec61f2 | Stefan Hajnoczi | job->job_type = job_type; |
4062 | eeec61f2 | Stefan Hajnoczi | job->bs = bs; |
4063 | eeec61f2 | Stefan Hajnoczi | job->cb = cb; |
4064 | eeec61f2 | Stefan Hajnoczi | job->opaque = opaque; |
4065 | 4513eafe | Paolo Bonzini | job->busy = true;
|
4066 | eeec61f2 | Stefan Hajnoczi | bs->job = job; |
4067 | c83c66c3 | Stefan Hajnoczi | |
4068 | c83c66c3 | Stefan Hajnoczi | /* Only set speed when necessary to avoid NotSupported error */
|
4069 | c83c66c3 | Stefan Hajnoczi | if (speed != 0) { |
4070 | c83c66c3 | Stefan Hajnoczi | Error *local_err = NULL;
|
4071 | c83c66c3 | Stefan Hajnoczi | |
4072 | c83c66c3 | Stefan Hajnoczi | block_job_set_speed(job, speed, &local_err); |
4073 | c83c66c3 | Stefan Hajnoczi | if (error_is_set(&local_err)) {
|
4074 | c83c66c3 | Stefan Hajnoczi | bs->job = NULL;
|
4075 | c83c66c3 | Stefan Hajnoczi | g_free(job); |
4076 | c83c66c3 | Stefan Hajnoczi | bdrv_set_in_use(bs, 0);
|
4077 | c83c66c3 | Stefan Hajnoczi | error_propagate(errp, local_err); |
4078 | c83c66c3 | Stefan Hajnoczi | return NULL; |
4079 | c83c66c3 | Stefan Hajnoczi | } |
4080 | c83c66c3 | Stefan Hajnoczi | } |
4081 | eeec61f2 | Stefan Hajnoczi | return job;
|
4082 | eeec61f2 | Stefan Hajnoczi | } |
4083 | eeec61f2 | Stefan Hajnoczi | |
4084 | eeec61f2 | Stefan Hajnoczi | void block_job_complete(BlockJob *job, int ret) |
4085 | eeec61f2 | Stefan Hajnoczi | { |
4086 | eeec61f2 | Stefan Hajnoczi | BlockDriverState *bs = job->bs; |
4087 | eeec61f2 | Stefan Hajnoczi | |
4088 | eeec61f2 | Stefan Hajnoczi | assert(bs->job == job); |
4089 | eeec61f2 | Stefan Hajnoczi | job->cb(job->opaque, ret); |
4090 | eeec61f2 | Stefan Hajnoczi | bs->job = NULL;
|
4091 | eeec61f2 | Stefan Hajnoczi | g_free(job); |
4092 | eeec61f2 | Stefan Hajnoczi | bdrv_set_in_use(bs, 0);
|
4093 | eeec61f2 | Stefan Hajnoczi | } |
4094 | eeec61f2 | Stefan Hajnoczi | |
4095 | 882ec7ce | Stefan Hajnoczi | void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp)
|
4096 | eeec61f2 | Stefan Hajnoczi | { |
4097 | 9e6636c7 | Stefan Hajnoczi | Error *local_err = NULL;
|
4098 | 9f25eccc | Paolo Bonzini | |
4099 | eeec61f2 | Stefan Hajnoczi | if (!job->job_type->set_speed) {
|
4100 | 9e6636c7 | Stefan Hajnoczi | error_set(errp, QERR_NOT_SUPPORTED); |
4101 | 9e6636c7 | Stefan Hajnoczi | return;
|
4102 | eeec61f2 | Stefan Hajnoczi | } |
4103 | 882ec7ce | Stefan Hajnoczi | job->job_type->set_speed(job, speed, &local_err); |
4104 | 9e6636c7 | Stefan Hajnoczi | if (error_is_set(&local_err)) {
|
4105 | 9e6636c7 | Stefan Hajnoczi | error_propagate(errp, local_err); |
4106 | 9e6636c7 | Stefan Hajnoczi | return;
|
4107 | 9f25eccc | Paolo Bonzini | } |
4108 | 9e6636c7 | Stefan Hajnoczi | |
4109 | 882ec7ce | Stefan Hajnoczi | job->speed = speed; |
4110 | eeec61f2 | Stefan Hajnoczi | } |
4111 | eeec61f2 | Stefan Hajnoczi | |
4112 | eeec61f2 | Stefan Hajnoczi | void block_job_cancel(BlockJob *job)
|
4113 | eeec61f2 | Stefan Hajnoczi | { |
4114 | eeec61f2 | Stefan Hajnoczi | job->cancelled = true;
|
4115 | fa4478d5 | Paolo Bonzini | if (job->co && !job->busy) {
|
4116 | fa4478d5 | Paolo Bonzini | qemu_coroutine_enter(job->co, NULL);
|
4117 | fa4478d5 | Paolo Bonzini | } |
4118 | eeec61f2 | Stefan Hajnoczi | } |
4119 | eeec61f2 | Stefan Hajnoczi | |
4120 | eeec61f2 | Stefan Hajnoczi | bool block_job_is_cancelled(BlockJob *job)
|
4121 | eeec61f2 | Stefan Hajnoczi | { |
4122 | eeec61f2 | Stefan Hajnoczi | return job->cancelled;
|
4123 | eeec61f2 | Stefan Hajnoczi | } |
4124 | 3e914655 | Paolo Bonzini | |
4125 | fa4478d5 | Paolo Bonzini | struct BlockCancelData {
|
4126 | fa4478d5 | Paolo Bonzini | BlockJob *job; |
4127 | fa4478d5 | Paolo Bonzini | BlockDriverCompletionFunc *cb; |
4128 | fa4478d5 | Paolo Bonzini | void *opaque;
|
4129 | fa4478d5 | Paolo Bonzini | bool cancelled;
|
4130 | fa4478d5 | Paolo Bonzini | int ret;
|
4131 | fa4478d5 | Paolo Bonzini | }; |
4132 | fa4478d5 | Paolo Bonzini | |
4133 | fa4478d5 | Paolo Bonzini | static void block_job_cancel_cb(void *opaque, int ret) |
4134 | 3e914655 | Paolo Bonzini | { |
4135 | fa4478d5 | Paolo Bonzini | struct BlockCancelData *data = opaque;
|
4136 | fa4478d5 | Paolo Bonzini | |
4137 | fa4478d5 | Paolo Bonzini | data->cancelled = block_job_is_cancelled(data->job); |
4138 | fa4478d5 | Paolo Bonzini | data->ret = ret; |
4139 | fa4478d5 | Paolo Bonzini | data->cb(data->opaque, ret); |
4140 | fa4478d5 | Paolo Bonzini | } |
4141 | fa4478d5 | Paolo Bonzini | |
4142 | fa4478d5 | Paolo Bonzini | int block_job_cancel_sync(BlockJob *job)
|
4143 | fa4478d5 | Paolo Bonzini | { |
4144 | fa4478d5 | Paolo Bonzini | struct BlockCancelData data;
|
4145 | 3e914655 | Paolo Bonzini | BlockDriverState *bs = job->bs; |
4146 | 3e914655 | Paolo Bonzini | |
4147 | 3e914655 | Paolo Bonzini | assert(bs->job == job); |
4148 | fa4478d5 | Paolo Bonzini | |
4149 | fa4478d5 | Paolo Bonzini | /* Set up our own callback to store the result and chain to
|
4150 | fa4478d5 | Paolo Bonzini | * the original callback.
|
4151 | fa4478d5 | Paolo Bonzini | */
|
4152 | fa4478d5 | Paolo Bonzini | data.job = job; |
4153 | fa4478d5 | Paolo Bonzini | data.cb = job->cb; |
4154 | fa4478d5 | Paolo Bonzini | data.opaque = job->opaque; |
4155 | fa4478d5 | Paolo Bonzini | data.ret = -EINPROGRESS; |
4156 | fa4478d5 | Paolo Bonzini | job->cb = block_job_cancel_cb; |
4157 | fa4478d5 | Paolo Bonzini | job->opaque = &data; |
4158 | 3e914655 | Paolo Bonzini | block_job_cancel(job); |
4159 | fa4478d5 | Paolo Bonzini | while (data.ret == -EINPROGRESS) {
|
4160 | 3e914655 | Paolo Bonzini | qemu_aio_wait(); |
4161 | 3e914655 | Paolo Bonzini | } |
4162 | fa4478d5 | Paolo Bonzini | return (data.cancelled && data.ret == 0) ? -ECANCELED : data.ret; |
4163 | 3e914655 | Paolo Bonzini | } |
4164 | 4513eafe | Paolo Bonzini | |
4165 | 4513eafe | Paolo Bonzini | void block_job_sleep_ns(BlockJob *job, QEMUClock *clock, int64_t ns)
|
4166 | 4513eafe | Paolo Bonzini | { |
4167 | 4513eafe | Paolo Bonzini | /* Check cancellation *before* setting busy = false, too! */
|
4168 | 4513eafe | Paolo Bonzini | if (!block_job_is_cancelled(job)) {
|
4169 | 4513eafe | Paolo Bonzini | job->busy = false;
|
4170 | 4513eafe | Paolo Bonzini | co_sleep_ns(clock, ns); |
4171 | 4513eafe | Paolo Bonzini | job->busy = true;
|
4172 | 4513eafe | Paolo Bonzini | } |
4173 | 4513eafe | Paolo Bonzini | } |