]>
Commit | Line | Data |
---|---|---|
fc01f7e7 FB |
1 | /* |
2 | * QEMU System Emulator block driver | |
5fafdf24 | 3 | * |
fc01f7e7 | 4 | * Copyright (c) 2003 Fabrice Bellard |
5fafdf24 | 5 | * |
fc01f7e7 FB |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
3990d09a | 24 | #include "config-host.h" |
faf07963 | 25 | #include "qemu-common.h" |
6d519a5f | 26 | #include "trace.h" |
83c9089e | 27 | #include "monitor/monitor.h" |
737e150e PB |
28 | #include "block/block_int.h" |
29 | #include "block/blockjob.h" | |
1de7afc9 | 30 | #include "qemu/module.h" |
7b1b5d19 | 31 | #include "qapi/qmp/qjson.h" |
9c17d615 | 32 | #include "sysemu/sysemu.h" |
1de7afc9 | 33 | #include "qemu/notify.h" |
737e150e | 34 | #include "block/coroutine.h" |
c13163fb | 35 | #include "block/qapi.h" |
b2023818 | 36 | #include "qmp-commands.h" |
1de7afc9 | 37 | #include "qemu/timer.h" |
fc01f7e7 | 38 | |
71e72a19 | 39 | #ifdef CONFIG_BSD |
7674e7bf FB |
40 | #include <sys/types.h> |
41 | #include <sys/stat.h> | |
42 | #include <sys/ioctl.h> | |
72cf2d4f | 43 | #include <sys/queue.h> |
c5e97233 | 44 | #ifndef __DragonFly__ |
7674e7bf FB |
45 | #include <sys/disk.h> |
46 | #endif | |
c5e97233 | 47 | #endif |
7674e7bf | 48 | |
49dc768d AL |
49 | #ifdef _WIN32 |
50 | #include <windows.h> | |
51 | #endif | |
52 | ||
e4654d2d FZ |
53 | struct BdrvDirtyBitmap { |
54 | HBitmap *bitmap; | |
55 | QLIST_ENTRY(BdrvDirtyBitmap) list; | |
56 | }; | |
57 | ||
1c9805a3 SH |
58 | #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */ |
59 | ||
7d4b4ba5 | 60 | static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load); |
f141eafe AL |
61 | static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, |
62 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, | |
c87c0672 | 63 | BlockDriverCompletionFunc *cb, void *opaque); |
f141eafe AL |
64 | static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, |
65 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, | |
ce1a14dc | 66 | BlockDriverCompletionFunc *cb, void *opaque); |
f9f05dc5 KW |
67 | static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs, |
68 | int64_t sector_num, int nb_sectors, | |
69 | QEMUIOVector *iov); | |
70 | static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs, | |
71 | int64_t sector_num, int nb_sectors, | |
72 | QEMUIOVector *iov); | |
c5fbe571 | 73 | static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs, |
470c0504 SH |
74 | int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, |
75 | BdrvRequestFlags flags); | |
1c9805a3 | 76 | static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs, |
f08f2dda SH |
77 | int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, |
78 | BdrvRequestFlags flags); | |
b2a61371 SH |
79 | static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs, |
80 | int64_t sector_num, | |
81 | QEMUIOVector *qiov, | |
82 | int nb_sectors, | |
d20d9b7c | 83 | BdrvRequestFlags flags, |
b2a61371 SH |
84 | BlockDriverCompletionFunc *cb, |
85 | void *opaque, | |
8c5873d6 | 86 | bool is_write); |
b2a61371 | 87 | static void coroutine_fn bdrv_co_do_rw(void *opaque); |
621f0589 | 88 | static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs, |
aa7bfbff | 89 | int64_t sector_num, int nb_sectors, BdrvRequestFlags flags); |
ec530c81 | 90 | |
1b7bdbc1 SH |
91 | static QTAILQ_HEAD(, BlockDriverState) bdrv_states = |
92 | QTAILQ_HEAD_INITIALIZER(bdrv_states); | |
7ee930d0 | 93 | |
dc364f4c BC |
94 | static QTAILQ_HEAD(, BlockDriverState) graph_bdrv_states = |
95 | QTAILQ_HEAD_INITIALIZER(graph_bdrv_states); | |
96 | ||
8a22f02a SH |
97 | static QLIST_HEAD(, BlockDriver) bdrv_drivers = |
98 | QLIST_HEAD_INITIALIZER(bdrv_drivers); | |
ea2384d3 | 99 | |
eb852011 MA |
100 | /* If non-zero, use only whitelisted block drivers */ |
101 | static int use_bdrv_whitelist; | |
102 | ||
9e0b22f4 SH |
103 | #ifdef _WIN32 |
104 | static int is_windows_drive_prefix(const char *filename) | |
105 | { | |
106 | return (((filename[0] >= 'a' && filename[0] <= 'z') || | |
107 | (filename[0] >= 'A' && filename[0] <= 'Z')) && | |
108 | filename[1] == ':'); | |
109 | } | |
110 | ||
111 | int is_windows_drive(const char *filename) | |
112 | { | |
113 | if (is_windows_drive_prefix(filename) && | |
114 | filename[2] == '\0') | |
115 | return 1; | |
116 | if (strstart(filename, "\\\\.\\", NULL) || | |
117 | strstart(filename, "//./", NULL)) | |
118 | return 1; | |
119 | return 0; | |
120 | } | |
121 | #endif | |
122 | ||
0563e191 | 123 | /* throttling disk I/O limits */ |
cc0681c4 BC |
124 | void bdrv_set_io_limits(BlockDriverState *bs, |
125 | ThrottleConfig *cfg) | |
98f90dba | 126 | { |
cc0681c4 | 127 | int i; |
98f90dba | 128 | |
cc0681c4 | 129 | throttle_config(&bs->throttle_state, cfg); |
98f90dba | 130 | |
cc0681c4 BC |
131 | for (i = 0; i < 2; i++) { |
132 | qemu_co_enter_next(&bs->throttled_reqs[i]); | |
98f90dba | 133 | } |
cc0681c4 BC |
134 | } |
135 | ||
136 | /* this function drain all the throttled IOs */ | |
137 | static bool bdrv_start_throttled_reqs(BlockDriverState *bs) | |
138 | { | |
139 | bool drained = false; | |
140 | bool enabled = bs->io_limits_enabled; | |
141 | int i; | |
142 | ||
143 | bs->io_limits_enabled = false; | |
144 | ||
145 | for (i = 0; i < 2; i++) { | |
146 | while (qemu_co_enter_next(&bs->throttled_reqs[i])) { | |
147 | drained = true; | |
148 | } | |
149 | } | |
150 | ||
151 | bs->io_limits_enabled = enabled; | |
98f90dba | 152 | |
cc0681c4 | 153 | return drained; |
98f90dba ZYW |
154 | } |
155 | ||
cc0681c4 | 156 | void bdrv_io_limits_disable(BlockDriverState *bs) |
0563e191 | 157 | { |
cc0681c4 | 158 | bs->io_limits_enabled = false; |
0563e191 | 159 | |
cc0681c4 BC |
160 | bdrv_start_throttled_reqs(bs); |
161 | ||
162 | throttle_destroy(&bs->throttle_state); | |
0563e191 ZYW |
163 | } |
164 | ||
cc0681c4 | 165 | static void bdrv_throttle_read_timer_cb(void *opaque) |
0563e191 | 166 | { |
cc0681c4 BC |
167 | BlockDriverState *bs = opaque; |
168 | qemu_co_enter_next(&bs->throttled_reqs[0]); | |
0563e191 ZYW |
169 | } |
170 | ||
cc0681c4 | 171 | static void bdrv_throttle_write_timer_cb(void *opaque) |
0563e191 | 172 | { |
cc0681c4 BC |
173 | BlockDriverState *bs = opaque; |
174 | qemu_co_enter_next(&bs->throttled_reqs[1]); | |
0563e191 ZYW |
175 | } |
176 | ||
cc0681c4 BC |
177 | /* should be called before bdrv_set_io_limits if a limit is set */ |
178 | void bdrv_io_limits_enable(BlockDriverState *bs) | |
179 | { | |
180 | assert(!bs->io_limits_enabled); | |
181 | throttle_init(&bs->throttle_state, | |
182 | QEMU_CLOCK_VIRTUAL, | |
183 | bdrv_throttle_read_timer_cb, | |
184 | bdrv_throttle_write_timer_cb, | |
185 | bs); | |
186 | bs->io_limits_enabled = true; | |
187 | } | |
188 | ||
189 | /* This function makes an IO wait if needed | |
190 | * | |
191 | * @nb_sectors: the number of sectors of the IO | |
192 | * @is_write: is the IO a write | |
193 | */ | |
98f90dba | 194 | static void bdrv_io_limits_intercept(BlockDriverState *bs, |
cc0681c4 BC |
195 | int nb_sectors, |
196 | bool is_write) | |
98f90dba | 197 | { |
cc0681c4 BC |
198 | /* does this io must wait */ |
199 | bool must_wait = throttle_schedule_timer(&bs->throttle_state, is_write); | |
98f90dba | 200 | |
cc0681c4 BC |
201 | /* if must wait or any request of this type throttled queue the IO */ |
202 | if (must_wait || | |
203 | !qemu_co_queue_empty(&bs->throttled_reqs[is_write])) { | |
204 | qemu_co_queue_wait(&bs->throttled_reqs[is_write]); | |
98f90dba ZYW |
205 | } |
206 | ||
cc0681c4 BC |
207 | /* the IO will be executed, do the accounting */ |
208 | throttle_account(&bs->throttle_state, | |
209 | is_write, | |
210 | nb_sectors * BDRV_SECTOR_SIZE); | |
98f90dba | 211 | |
cc0681c4 BC |
212 | /* if the next request must wait -> do nothing */ |
213 | if (throttle_schedule_timer(&bs->throttle_state, is_write)) { | |
214 | return; | |
98f90dba ZYW |
215 | } |
216 | ||
cc0681c4 BC |
217 | /* else queue next request for execution */ |
218 | qemu_co_queue_next(&bs->throttled_reqs[is_write]); | |
98f90dba ZYW |
219 | } |
220 | ||
9e0b22f4 SH |
221 | /* check if the path starts with "<protocol>:" */ |
222 | static int path_has_protocol(const char *path) | |
223 | { | |
947995c0 PB |
224 | const char *p; |
225 | ||
9e0b22f4 SH |
226 | #ifdef _WIN32 |
227 | if (is_windows_drive(path) || | |
228 | is_windows_drive_prefix(path)) { | |
229 | return 0; | |
230 | } | |
947995c0 PB |
231 | p = path + strcspn(path, ":/\\"); |
232 | #else | |
233 | p = path + strcspn(path, ":/"); | |
9e0b22f4 SH |
234 | #endif |
235 | ||
947995c0 | 236 | return *p == ':'; |
9e0b22f4 SH |
237 | } |
238 | ||
83f64091 | 239 | int path_is_absolute(const char *path) |
3b0d4f61 | 240 | { |
21664424 FB |
241 | #ifdef _WIN32 |
242 | /* specific case for names like: "\\.\d:" */ | |
f53f4da9 | 243 | if (is_windows_drive(path) || is_windows_drive_prefix(path)) { |
21664424 | 244 | return 1; |
f53f4da9 PB |
245 | } |
246 | return (*path == '/' || *path == '\\'); | |
3b9f94e1 | 247 | #else |
f53f4da9 | 248 | return (*path == '/'); |
3b9f94e1 | 249 | #endif |
3b0d4f61 FB |
250 | } |
251 | ||
83f64091 FB |
252 | /* if filename is absolute, just copy it to dest. Otherwise, build a |
253 | path to it by considering it is relative to base_path. URL are | |
254 | supported. */ | |
255 | void path_combine(char *dest, int dest_size, | |
256 | const char *base_path, | |
257 | const char *filename) | |
3b0d4f61 | 258 | { |
83f64091 FB |
259 | const char *p, *p1; |
260 | int len; | |
261 | ||
262 | if (dest_size <= 0) | |
263 | return; | |
264 | if (path_is_absolute(filename)) { | |
265 | pstrcpy(dest, dest_size, filename); | |
266 | } else { | |
267 | p = strchr(base_path, ':'); | |
268 | if (p) | |
269 | p++; | |
270 | else | |
271 | p = base_path; | |
3b9f94e1 FB |
272 | p1 = strrchr(base_path, '/'); |
273 | #ifdef _WIN32 | |
274 | { | |
275 | const char *p2; | |
276 | p2 = strrchr(base_path, '\\'); | |
277 | if (!p1 || p2 > p1) | |
278 | p1 = p2; | |
279 | } | |
280 | #endif | |
83f64091 FB |
281 | if (p1) |
282 | p1++; | |
283 | else | |
284 | p1 = base_path; | |
285 | if (p1 > p) | |
286 | p = p1; | |
287 | len = p - base_path; | |
288 | if (len > dest_size - 1) | |
289 | len = dest_size - 1; | |
290 | memcpy(dest, base_path, len); | |
291 | dest[len] = '\0'; | |
292 | pstrcat(dest, dest_size, filename); | |
3b0d4f61 | 293 | } |
3b0d4f61 FB |
294 | } |
295 | ||
dc5a1371 PB |
296 | void bdrv_get_full_backing_filename(BlockDriverState *bs, char *dest, size_t sz) |
297 | { | |
298 | if (bs->backing_file[0] == '\0' || path_has_protocol(bs->backing_file)) { | |
299 | pstrcpy(dest, sz, bs->backing_file); | |
300 | } else { | |
301 | path_combine(dest, sz, bs->filename, bs->backing_file); | |
302 | } | |
303 | } | |
304 | ||
5efa9d5a | 305 | void bdrv_register(BlockDriver *bdrv) |
ea2384d3 | 306 | { |
8c5873d6 SH |
307 | /* Block drivers without coroutine functions need emulation */ |
308 | if (!bdrv->bdrv_co_readv) { | |
f9f05dc5 KW |
309 | bdrv->bdrv_co_readv = bdrv_co_readv_em; |
310 | bdrv->bdrv_co_writev = bdrv_co_writev_em; | |
311 | ||
f8c35c1d SH |
312 | /* bdrv_co_readv_em()/brdv_co_writev_em() work in terms of aio, so if |
313 | * the block driver lacks aio we need to emulate that too. | |
314 | */ | |
f9f05dc5 KW |
315 | if (!bdrv->bdrv_aio_readv) { |
316 | /* add AIO emulation layer */ | |
317 | bdrv->bdrv_aio_readv = bdrv_aio_readv_em; | |
318 | bdrv->bdrv_aio_writev = bdrv_aio_writev_em; | |
f9f05dc5 | 319 | } |
83f64091 | 320 | } |
b2e12bc6 | 321 | |
8a22f02a | 322 | QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list); |
ea2384d3 | 323 | } |
b338082b FB |
324 | |
325 | /* create a new block device (by default it is empty) */ | |
326 | BlockDriverState *bdrv_new(const char *device_name) | |
327 | { | |
1b7bdbc1 | 328 | BlockDriverState *bs; |
b338082b | 329 | |
7267c094 | 330 | bs = g_malloc0(sizeof(BlockDriverState)); |
e4654d2d | 331 | QLIST_INIT(&bs->dirty_bitmaps); |
b338082b | 332 | pstrcpy(bs->device_name, sizeof(bs->device_name), device_name); |
ea2384d3 | 333 | if (device_name[0] != '\0') { |
dc364f4c | 334 | QTAILQ_INSERT_TAIL(&bdrv_states, bs, device_list); |
ea2384d3 | 335 | } |
28a7282a | 336 | bdrv_iostatus_disable(bs); |
d7d512f6 | 337 | notifier_list_init(&bs->close_notifiers); |
d616b224 | 338 | notifier_with_return_list_init(&bs->before_write_notifiers); |
cc0681c4 BC |
339 | qemu_co_queue_init(&bs->throttled_reqs[0]); |
340 | qemu_co_queue_init(&bs->throttled_reqs[1]); | |
9fcb0251 | 341 | bs->refcnt = 1; |
d7d512f6 | 342 | |
b338082b FB |
343 | return bs; |
344 | } | |
345 | ||
d7d512f6 PB |
346 | void bdrv_add_close_notifier(BlockDriverState *bs, Notifier *notify) |
347 | { | |
348 | notifier_list_add(&bs->close_notifiers, notify); | |
349 | } | |
350 | ||
ea2384d3 FB |
351 | BlockDriver *bdrv_find_format(const char *format_name) |
352 | { | |
353 | BlockDriver *drv1; | |
8a22f02a SH |
354 | QLIST_FOREACH(drv1, &bdrv_drivers, list) { |
355 | if (!strcmp(drv1->format_name, format_name)) { | |
ea2384d3 | 356 | return drv1; |
8a22f02a | 357 | } |
ea2384d3 FB |
358 | } |
359 | return NULL; | |
360 | } | |
361 | ||
b64ec4e4 | 362 | static int bdrv_is_whitelisted(BlockDriver *drv, bool read_only) |
eb852011 | 363 | { |
b64ec4e4 FZ |
364 | static const char *whitelist_rw[] = { |
365 | CONFIG_BDRV_RW_WHITELIST | |
366 | }; | |
367 | static const char *whitelist_ro[] = { | |
368 | CONFIG_BDRV_RO_WHITELIST | |
eb852011 MA |
369 | }; |
370 | const char **p; | |
371 | ||
b64ec4e4 | 372 | if (!whitelist_rw[0] && !whitelist_ro[0]) { |
eb852011 | 373 | return 1; /* no whitelist, anything goes */ |
b64ec4e4 | 374 | } |
eb852011 | 375 | |
b64ec4e4 | 376 | for (p = whitelist_rw; *p; p++) { |
eb852011 MA |
377 | if (!strcmp(drv->format_name, *p)) { |
378 | return 1; | |
379 | } | |
380 | } | |
b64ec4e4 FZ |
381 | if (read_only) { |
382 | for (p = whitelist_ro; *p; p++) { | |
383 | if (!strcmp(drv->format_name, *p)) { | |
384 | return 1; | |
385 | } | |
386 | } | |
387 | } | |
eb852011 MA |
388 | return 0; |
389 | } | |
390 | ||
b64ec4e4 FZ |
391 | BlockDriver *bdrv_find_whitelisted_format(const char *format_name, |
392 | bool read_only) | |
eb852011 MA |
393 | { |
394 | BlockDriver *drv = bdrv_find_format(format_name); | |
b64ec4e4 | 395 | return drv && bdrv_is_whitelisted(drv, read_only) ? drv : NULL; |
eb852011 MA |
396 | } |
397 | ||
5b7e1542 ZYW |
398 | typedef struct CreateCo { |
399 | BlockDriver *drv; | |
400 | char *filename; | |
401 | QEMUOptionParameter *options; | |
402 | int ret; | |
cc84d90f | 403 | Error *err; |
5b7e1542 ZYW |
404 | } CreateCo; |
405 | ||
406 | static void coroutine_fn bdrv_create_co_entry(void *opaque) | |
407 | { | |
cc84d90f HR |
408 | Error *local_err = NULL; |
409 | int ret; | |
410 | ||
5b7e1542 ZYW |
411 | CreateCo *cco = opaque; |
412 | assert(cco->drv); | |
413 | ||
cc84d90f HR |
414 | ret = cco->drv->bdrv_create(cco->filename, cco->options, &local_err); |
415 | if (error_is_set(&local_err)) { | |
416 | error_propagate(&cco->err, local_err); | |
417 | } | |
418 | cco->ret = ret; | |
5b7e1542 ZYW |
419 | } |
420 | ||
0e7e1989 | 421 | int bdrv_create(BlockDriver *drv, const char* filename, |
cc84d90f | 422 | QEMUOptionParameter *options, Error **errp) |
ea2384d3 | 423 | { |
5b7e1542 ZYW |
424 | int ret; |
425 | ||
426 | Coroutine *co; | |
427 | CreateCo cco = { | |
428 | .drv = drv, | |
429 | .filename = g_strdup(filename), | |
430 | .options = options, | |
431 | .ret = NOT_DONE, | |
cc84d90f | 432 | .err = NULL, |
5b7e1542 ZYW |
433 | }; |
434 | ||
435 | if (!drv->bdrv_create) { | |
cc84d90f | 436 | error_setg(errp, "Driver '%s' does not support image creation", drv->format_name); |
80168bff LC |
437 | ret = -ENOTSUP; |
438 | goto out; | |
5b7e1542 ZYW |
439 | } |
440 | ||
441 | if (qemu_in_coroutine()) { | |
442 | /* Fast-path if already in coroutine context */ | |
443 | bdrv_create_co_entry(&cco); | |
444 | } else { | |
445 | co = qemu_coroutine_create(bdrv_create_co_entry); | |
446 | qemu_coroutine_enter(co, &cco); | |
447 | while (cco.ret == NOT_DONE) { | |
448 | qemu_aio_wait(); | |
449 | } | |
450 | } | |
451 | ||
452 | ret = cco.ret; | |
cc84d90f HR |
453 | if (ret < 0) { |
454 | if (error_is_set(&cco.err)) { | |
455 | error_propagate(errp, cco.err); | |
456 | } else { | |
457 | error_setg_errno(errp, -ret, "Could not create image"); | |
458 | } | |
459 | } | |
0e7e1989 | 460 | |
80168bff LC |
461 | out: |
462 | g_free(cco.filename); | |
5b7e1542 | 463 | return ret; |
ea2384d3 FB |
464 | } |
465 | ||
cc84d90f HR |
466 | int bdrv_create_file(const char* filename, QEMUOptionParameter *options, |
467 | Error **errp) | |
84a12e66 CH |
468 | { |
469 | BlockDriver *drv; | |
cc84d90f HR |
470 | Error *local_err = NULL; |
471 | int ret; | |
84a12e66 | 472 | |
98289620 | 473 | drv = bdrv_find_protocol(filename, true); |
84a12e66 | 474 | if (drv == NULL) { |
cc84d90f | 475 | error_setg(errp, "Could not find protocol for file '%s'", filename); |
16905d71 | 476 | return -ENOENT; |
84a12e66 CH |
477 | } |
478 | ||
cc84d90f HR |
479 | ret = bdrv_create(drv, filename, options, &local_err); |
480 | if (error_is_set(&local_err)) { | |
481 | error_propagate(errp, local_err); | |
482 | } | |
483 | return ret; | |
84a12e66 CH |
484 | } |
485 | ||
d34682cd KW |
486 | static int bdrv_refresh_limits(BlockDriverState *bs) |
487 | { | |
488 | BlockDriver *drv = bs->drv; | |
489 | ||
490 | memset(&bs->bl, 0, sizeof(bs->bl)); | |
491 | ||
466ad822 KW |
492 | if (!drv) { |
493 | return 0; | |
494 | } | |
495 | ||
496 | /* Take some limits from the children as a default */ | |
497 | if (bs->file) { | |
498 | bdrv_refresh_limits(bs->file); | |
499 | bs->bl.opt_transfer_length = bs->file->bl.opt_transfer_length; | |
500 | } | |
501 | ||
502 | if (bs->backing_hd) { | |
503 | bdrv_refresh_limits(bs->backing_hd); | |
504 | bs->bl.opt_transfer_length = | |
505 | MAX(bs->bl.opt_transfer_length, | |
506 | bs->backing_hd->bl.opt_transfer_length); | |
507 | } | |
508 | ||
509 | /* Then let the driver override it */ | |
510 | if (drv->bdrv_refresh_limits) { | |
d34682cd KW |
511 | return drv->bdrv_refresh_limits(bs); |
512 | } | |
513 | ||
514 | return 0; | |
515 | } | |
516 | ||
eba25057 JM |
517 | /* |
518 | * Create a uniquely-named empty temporary file. | |
519 | * Return 0 upon success, otherwise a negative errno value. | |
520 | */ | |
521 | int get_tmp_filename(char *filename, int size) | |
d5249393 | 522 | { |
eba25057 | 523 | #ifdef _WIN32 |
3b9f94e1 | 524 | char temp_dir[MAX_PATH]; |
eba25057 JM |
525 | /* GetTempFileName requires that its output buffer (4th param) |
526 | have length MAX_PATH or greater. */ | |
527 | assert(size >= MAX_PATH); | |
528 | return (GetTempPath(MAX_PATH, temp_dir) | |
529 | && GetTempFileName(temp_dir, "qem", 0, filename) | |
530 | ? 0 : -GetLastError()); | |
d5249393 | 531 | #else |
67b915a5 | 532 | int fd; |
7ccfb2eb | 533 | const char *tmpdir; |
0badc1ee AJ |
534 | tmpdir = getenv("TMPDIR"); |
535 | if (!tmpdir) | |
536 | tmpdir = "/tmp"; | |
eba25057 JM |
537 | if (snprintf(filename, size, "%s/vl.XXXXXX", tmpdir) >= size) { |
538 | return -EOVERFLOW; | |
539 | } | |
ea2384d3 | 540 | fd = mkstemp(filename); |
fe235a06 DH |
541 | if (fd < 0) { |
542 | return -errno; | |
543 | } | |
544 | if (close(fd) != 0) { | |
545 | unlink(filename); | |
eba25057 JM |
546 | return -errno; |
547 | } | |
548 | return 0; | |
d5249393 | 549 | #endif |
eba25057 | 550 | } |
fc01f7e7 | 551 | |
84a12e66 CH |
552 | /* |
553 | * Detect host devices. By convention, /dev/cdrom[N] is always | |
554 | * recognized as a host CDROM. | |
555 | */ | |
556 | static BlockDriver *find_hdev_driver(const char *filename) | |
557 | { | |
558 | int score_max = 0, score; | |
559 | BlockDriver *drv = NULL, *d; | |
560 | ||
561 | QLIST_FOREACH(d, &bdrv_drivers, list) { | |
562 | if (d->bdrv_probe_device) { | |
563 | score = d->bdrv_probe_device(filename); | |
564 | if (score > score_max) { | |
565 | score_max = score; | |
566 | drv = d; | |
567 | } | |
568 | } | |
569 | } | |
570 | ||
571 | return drv; | |
572 | } | |
573 | ||
98289620 KW |
574 | BlockDriver *bdrv_find_protocol(const char *filename, |
575 | bool allow_protocol_prefix) | |
83f64091 FB |
576 | { |
577 | BlockDriver *drv1; | |
578 | char protocol[128]; | |
1cec71e3 | 579 | int len; |
83f64091 | 580 | const char *p; |
19cb3738 | 581 | |
66f82cee KW |
582 | /* TODO Drivers without bdrv_file_open must be specified explicitly */ |
583 | ||
39508e7a CH |
584 | /* |
585 | * XXX(hch): we really should not let host device detection | |
586 | * override an explicit protocol specification, but moving this | |
587 | * later breaks access to device names with colons in them. | |
588 | * Thanks to the brain-dead persistent naming schemes on udev- | |
589 | * based Linux systems those actually are quite common. | |
590 | */ | |
591 | drv1 = find_hdev_driver(filename); | |
592 | if (drv1) { | |
593 | return drv1; | |
594 | } | |
595 | ||
98289620 | 596 | if (!path_has_protocol(filename) || !allow_protocol_prefix) { |
39508e7a | 597 | return bdrv_find_format("file"); |
84a12e66 | 598 | } |
98289620 | 599 | |
9e0b22f4 SH |
600 | p = strchr(filename, ':'); |
601 | assert(p != NULL); | |
1cec71e3 AL |
602 | len = p - filename; |
603 | if (len > sizeof(protocol) - 1) | |
604 | len = sizeof(protocol) - 1; | |
605 | memcpy(protocol, filename, len); | |
606 | protocol[len] = '\0'; | |
8a22f02a | 607 | QLIST_FOREACH(drv1, &bdrv_drivers, list) { |
5fafdf24 | 608 | if (drv1->protocol_name && |
8a22f02a | 609 | !strcmp(drv1->protocol_name, protocol)) { |
83f64091 | 610 | return drv1; |
8a22f02a | 611 | } |
83f64091 FB |
612 | } |
613 | return NULL; | |
614 | } | |
615 | ||
f500a6d3 | 616 | static int find_image_format(BlockDriverState *bs, const char *filename, |
34b5d2c6 | 617 | BlockDriver **pdrv, Error **errp) |
f3a5d3f8 | 618 | { |
f500a6d3 | 619 | int score, score_max; |
f3a5d3f8 CH |
620 | BlockDriver *drv1, *drv; |
621 | uint8_t buf[2048]; | |
f500a6d3 | 622 | int ret = 0; |
f8ea0b00 | 623 | |
08a00559 | 624 | /* Return the raw BlockDriver * to scsi-generic devices or empty drives */ |
8e895599 | 625 | if (bs->sg || !bdrv_is_inserted(bs) || bdrv_getlength(bs) == 0) { |
c98ac35d SW |
626 | drv = bdrv_find_format("raw"); |
627 | if (!drv) { | |
34b5d2c6 | 628 | error_setg(errp, "Could not find raw image format"); |
c98ac35d SW |
629 | ret = -ENOENT; |
630 | } | |
631 | *pdrv = drv; | |
632 | return ret; | |
1a396859 | 633 | } |
f8ea0b00 | 634 | |
83f64091 | 635 | ret = bdrv_pread(bs, 0, buf, sizeof(buf)); |
83f64091 | 636 | if (ret < 0) { |
34b5d2c6 HR |
637 | error_setg_errno(errp, -ret, "Could not read image for determining its " |
638 | "format"); | |
c98ac35d SW |
639 | *pdrv = NULL; |
640 | return ret; | |
83f64091 FB |
641 | } |
642 | ||
ea2384d3 | 643 | score_max = 0; |
84a12e66 | 644 | drv = NULL; |
8a22f02a | 645 | QLIST_FOREACH(drv1, &bdrv_drivers, list) { |
83f64091 FB |
646 | if (drv1->bdrv_probe) { |
647 | score = drv1->bdrv_probe(buf, ret, filename); | |
648 | if (score > score_max) { | |
649 | score_max = score; | |
650 | drv = drv1; | |
651 | } | |
0849bf08 | 652 | } |
fc01f7e7 | 653 | } |
c98ac35d | 654 | if (!drv) { |
34b5d2c6 HR |
655 | error_setg(errp, "Could not determine image format: No compatible " |
656 | "driver found"); | |
c98ac35d SW |
657 | ret = -ENOENT; |
658 | } | |
659 | *pdrv = drv; | |
660 | return ret; | |
ea2384d3 FB |
661 | } |
662 | ||
51762288 SH |
663 | /** |
664 | * Set the current 'total_sectors' value | |
665 | */ | |
666 | static int refresh_total_sectors(BlockDriverState *bs, int64_t hint) | |
667 | { | |
668 | BlockDriver *drv = bs->drv; | |
669 | ||
396759ad NB |
670 | /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */ |
671 | if (bs->sg) | |
672 | return 0; | |
673 | ||
51762288 SH |
674 | /* query actual device if possible, otherwise just trust the hint */ |
675 | if (drv->bdrv_getlength) { | |
676 | int64_t length = drv->bdrv_getlength(bs); | |
677 | if (length < 0) { | |
678 | return length; | |
679 | } | |
7e382003 | 680 | hint = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE); |
51762288 SH |
681 | } |
682 | ||
683 | bs->total_sectors = hint; | |
684 | return 0; | |
685 | } | |
686 | ||
9e8f1835 PB |
687 | /** |
688 | * Set open flags for a given discard mode | |
689 | * | |
690 | * Return 0 on success, -1 if the discard mode was invalid. | |
691 | */ | |
692 | int bdrv_parse_discard_flags(const char *mode, int *flags) | |
693 | { | |
694 | *flags &= ~BDRV_O_UNMAP; | |
695 | ||
696 | if (!strcmp(mode, "off") || !strcmp(mode, "ignore")) { | |
697 | /* do nothing */ | |
698 | } else if (!strcmp(mode, "on") || !strcmp(mode, "unmap")) { | |
699 | *flags |= BDRV_O_UNMAP; | |
700 | } else { | |
701 | return -1; | |
702 | } | |
703 | ||
704 | return 0; | |
705 | } | |
706 | ||
c3993cdc SH |
707 | /** |
708 | * Set open flags for a given cache mode | |
709 | * | |
710 | * Return 0 on success, -1 if the cache mode was invalid. | |
711 | */ | |
712 | int bdrv_parse_cache_flags(const char *mode, int *flags) | |
713 | { | |
714 | *flags &= ~BDRV_O_CACHE_MASK; | |
715 | ||
716 | if (!strcmp(mode, "off") || !strcmp(mode, "none")) { | |
717 | *flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB; | |
92196b2f SH |
718 | } else if (!strcmp(mode, "directsync")) { |
719 | *flags |= BDRV_O_NOCACHE; | |
c3993cdc SH |
720 | } else if (!strcmp(mode, "writeback")) { |
721 | *flags |= BDRV_O_CACHE_WB; | |
722 | } else if (!strcmp(mode, "unsafe")) { | |
723 | *flags |= BDRV_O_CACHE_WB; | |
724 | *flags |= BDRV_O_NO_FLUSH; | |
725 | } else if (!strcmp(mode, "writethrough")) { | |
726 | /* this is the default */ | |
727 | } else { | |
728 | return -1; | |
729 | } | |
730 | ||
731 | return 0; | |
732 | } | |
733 | ||
53fec9d3 SH |
734 | /** |
735 | * The copy-on-read flag is actually a reference count so multiple users may | |
736 | * use the feature without worrying about clobbering its previous state. | |
737 | * Copy-on-read stays enabled until all users have called to disable it. | |
738 | */ | |
739 | void bdrv_enable_copy_on_read(BlockDriverState *bs) | |
740 | { | |
741 | bs->copy_on_read++; | |
742 | } | |
743 | ||
744 | void bdrv_disable_copy_on_read(BlockDriverState *bs) | |
745 | { | |
746 | assert(bs->copy_on_read > 0); | |
747 | bs->copy_on_read--; | |
748 | } | |
749 | ||
7b272452 KW |
750 | static int bdrv_open_flags(BlockDriverState *bs, int flags) |
751 | { | |
752 | int open_flags = flags | BDRV_O_CACHE_WB; | |
753 | ||
754 | /* | |
755 | * Clear flags that are internal to the block layer before opening the | |
756 | * image. | |
757 | */ | |
758 | open_flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); | |
759 | ||
760 | /* | |
761 | * Snapshots should be writable. | |
762 | */ | |
763 | if (bs->is_temporary) { | |
764 | open_flags |= BDRV_O_RDWR; | |
765 | } | |
766 | ||
767 | return open_flags; | |
768 | } | |
769 | ||
6913c0c2 BC |
770 | static int bdrv_assign_node_name(BlockDriverState *bs, |
771 | const char *node_name, | |
772 | Error **errp) | |
773 | { | |
774 | if (!node_name) { | |
775 | return 0; | |
776 | } | |
777 | ||
778 | /* empty string node name is invalid */ | |
779 | if (node_name[0] == '\0') { | |
780 | error_setg(errp, "Empty node name"); | |
781 | return -EINVAL; | |
782 | } | |
783 | ||
784 | /* takes care of avoiding duplicates node names */ | |
785 | if (bdrv_find_node(node_name)) { | |
786 | error_setg(errp, "Duplicate node name"); | |
787 | return -EINVAL; | |
788 | } | |
789 | ||
790 | /* copy node name into the bs and insert it into the graph list */ | |
791 | pstrcpy(bs->node_name, sizeof(bs->node_name), node_name); | |
792 | QTAILQ_INSERT_TAIL(&graph_bdrv_states, bs, node_list); | |
793 | ||
794 | return 0; | |
795 | } | |
796 | ||
57915332 KW |
797 | /* |
798 | * Common part for opening disk images and files | |
b6ad491a KW |
799 | * |
800 | * Removes all processed options from *options. | |
57915332 | 801 | */ |
f500a6d3 | 802 | static int bdrv_open_common(BlockDriverState *bs, BlockDriverState *file, |
34b5d2c6 | 803 | QDict *options, int flags, BlockDriver *drv, Error **errp) |
57915332 KW |
804 | { |
805 | int ret, open_flags; | |
035fccdf | 806 | const char *filename; |
6913c0c2 | 807 | const char *node_name = NULL; |
34b5d2c6 | 808 | Error *local_err = NULL; |
57915332 KW |
809 | |
810 | assert(drv != NULL); | |
6405875c | 811 | assert(bs->file == NULL); |
707ff828 | 812 | assert(options != NULL && bs->options != options); |
57915332 | 813 | |
45673671 KW |
814 | if (file != NULL) { |
815 | filename = file->filename; | |
816 | } else { | |
817 | filename = qdict_get_try_str(options, "filename"); | |
818 | } | |
819 | ||
820 | trace_bdrv_open_common(bs, filename ?: "", flags, drv->format_name); | |
28dcee10 | 821 | |
6913c0c2 BC |
822 | node_name = qdict_get_try_str(options, "node-name"); |
823 | ret = bdrv_assign_node_name(bs, node_name, errp); | |
824 | if (ret < 0) { | |
825 | return ret; | |
826 | } | |
827 | qdict_del(options, "node-name"); | |
828 | ||
5d186eb0 KW |
829 | /* bdrv_open() with directly using a protocol as drv. This layer is already |
830 | * opened, so assign it to bs (while file becomes a closed BlockDriverState) | |
831 | * and return immediately. */ | |
832 | if (file != NULL && drv->bdrv_file_open) { | |
833 | bdrv_swap(file, bs); | |
834 | return 0; | |
835 | } | |
836 | ||
57915332 | 837 | bs->open_flags = flags; |
57915332 | 838 | bs->buffer_alignment = 512; |
0d51b4de | 839 | bs->zero_beyond_eof = true; |
b64ec4e4 FZ |
840 | open_flags = bdrv_open_flags(bs, flags); |
841 | bs->read_only = !(open_flags & BDRV_O_RDWR); | |
842 | ||
843 | if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv, bs->read_only)) { | |
8f94a6e4 KW |
844 | error_setg(errp, |
845 | !bs->read_only && bdrv_is_whitelisted(drv, true) | |
846 | ? "Driver '%s' can only be used for read-only devices" | |
847 | : "Driver '%s' is not whitelisted", | |
848 | drv->format_name); | |
b64ec4e4 FZ |
849 | return -ENOTSUP; |
850 | } | |
57915332 | 851 | |
53fec9d3 | 852 | assert(bs->copy_on_read == 0); /* bdrv_new() and bdrv_close() make it so */ |
0ebd24e0 KW |
853 | if (flags & BDRV_O_COPY_ON_READ) { |
854 | if (!bs->read_only) { | |
855 | bdrv_enable_copy_on_read(bs); | |
856 | } else { | |
857 | error_setg(errp, "Can't use copy-on-read on read-only device"); | |
858 | return -EINVAL; | |
859 | } | |
53fec9d3 SH |
860 | } |
861 | ||
c2ad1b0c KW |
862 | if (filename != NULL) { |
863 | pstrcpy(bs->filename, sizeof(bs->filename), filename); | |
864 | } else { | |
865 | bs->filename[0] = '\0'; | |
866 | } | |
57915332 | 867 | |
57915332 | 868 | bs->drv = drv; |
7267c094 | 869 | bs->opaque = g_malloc0(drv->instance_size); |
57915332 | 870 | |
03f541bd | 871 | bs->enable_write_cache = !!(flags & BDRV_O_CACHE_WB); |
e7c63796 | 872 | |
66f82cee KW |
873 | /* Open the image, either directly or using a protocol */ |
874 | if (drv->bdrv_file_open) { | |
5d186eb0 | 875 | assert(file == NULL); |
030be321 | 876 | assert(!drv->bdrv_needs_filename || filename != NULL); |
34b5d2c6 | 877 | ret = drv->bdrv_file_open(bs, options, open_flags, &local_err); |
f500a6d3 | 878 | } else { |
2af5ef70 | 879 | if (file == NULL) { |
34b5d2c6 HR |
880 | error_setg(errp, "Can't use '%s' as a block driver for the " |
881 | "protocol level", drv->format_name); | |
2af5ef70 KW |
882 | ret = -EINVAL; |
883 | goto free_and_fail; | |
884 | } | |
f500a6d3 | 885 | bs->file = file; |
34b5d2c6 | 886 | ret = drv->bdrv_open(bs, options, open_flags, &local_err); |
66f82cee KW |
887 | } |
888 | ||
57915332 | 889 | if (ret < 0) { |
34b5d2c6 HR |
890 | if (error_is_set(&local_err)) { |
891 | error_propagate(errp, local_err); | |
2fa9aa59 DH |
892 | } else if (bs->filename[0]) { |
893 | error_setg_errno(errp, -ret, "Could not open '%s'", bs->filename); | |
34b5d2c6 HR |
894 | } else { |
895 | error_setg_errno(errp, -ret, "Could not open image"); | |
896 | } | |
57915332 KW |
897 | goto free_and_fail; |
898 | } | |
899 | ||
51762288 SH |
900 | ret = refresh_total_sectors(bs, bs->total_sectors); |
901 | if (ret < 0) { | |
34b5d2c6 | 902 | error_setg_errno(errp, -ret, "Could not refresh total sector count"); |
51762288 | 903 | goto free_and_fail; |
57915332 | 904 | } |
51762288 | 905 | |
d34682cd KW |
906 | bdrv_refresh_limits(bs); |
907 | ||
57915332 KW |
908 | #ifndef _WIN32 |
909 | if (bs->is_temporary) { | |
d4cea8df DH |
910 | assert(bs->filename[0] != '\0'); |
911 | unlink(bs->filename); | |
57915332 KW |
912 | } |
913 | #endif | |
914 | return 0; | |
915 | ||
916 | free_and_fail: | |
f500a6d3 | 917 | bs->file = NULL; |
7267c094 | 918 | g_free(bs->opaque); |
57915332 KW |
919 | bs->opaque = NULL; |
920 | bs->drv = NULL; | |
921 | return ret; | |
922 | } | |
923 | ||
b6ce07aa KW |
924 | /* |
925 | * Opens a file using a protocol (file, host_device, nbd, ...) | |
787e4a85 KW |
926 | * |
927 | * options is a QDict of options to pass to the block drivers, or NULL for an | |
928 | * empty set of options. The reference to the QDict belongs to the block layer | |
929 | * after the call (even on failure), so if the caller intends to reuse the | |
930 | * dictionary, it needs to use QINCREF() before calling bdrv_file_open. | |
b6ce07aa | 931 | */ |
787e4a85 | 932 | int bdrv_file_open(BlockDriverState **pbs, const char *filename, |
72daa72e HR |
933 | const char *reference, QDict *options, int flags, |
934 | Error **errp) | |
ea2384d3 | 935 | { |
72daa72e | 936 | BlockDriverState *bs = NULL; |
6db95603 | 937 | BlockDriver *drv; |
c2ad1b0c | 938 | const char *drvname; |
98289620 | 939 | bool allow_protocol_prefix = false; |
34b5d2c6 | 940 | Error *local_err = NULL; |
83f64091 FB |
941 | int ret; |
942 | ||
707ff828 KW |
943 | /* NULL means an empty set of options */ |
944 | if (options == NULL) { | |
945 | options = qdict_new(); | |
946 | } | |
947 | ||
72daa72e HR |
948 | if (reference) { |
949 | if (filename || qdict_size(options)) { | |
950 | error_setg(errp, "Cannot reference an existing block device with " | |
951 | "additional options or a new filename"); | |
952 | return -EINVAL; | |
953 | } | |
954 | QDECREF(options); | |
955 | ||
956 | bs = bdrv_find(reference); | |
957 | if (!bs) { | |
958 | error_setg(errp, "Cannot find block device '%s'", reference); | |
959 | return -ENODEV; | |
960 | } | |
961 | bdrv_ref(bs); | |
962 | *pbs = bs; | |
963 | return 0; | |
964 | } | |
965 | ||
83f64091 | 966 | bs = bdrv_new(""); |
707ff828 KW |
967 | bs->options = options; |
968 | options = qdict_clone_shallow(options); | |
969 | ||
035fccdf KW |
970 | /* Fetch the file name from the options QDict if necessary */ |
971 | if (!filename) { | |
972 | filename = qdict_get_try_str(options, "filename"); | |
973 | } else if (filename && !qdict_haskey(options, "filename")) { | |
974 | qdict_put(options, "filename", qstring_from_str(filename)); | |
98289620 | 975 | allow_protocol_prefix = true; |
035fccdf | 976 | } else { |
34b5d2c6 HR |
977 | error_setg(errp, "Can't specify 'file' and 'filename' options at the " |
978 | "same time"); | |
035fccdf KW |
979 | ret = -EINVAL; |
980 | goto fail; | |
981 | } | |
982 | ||
c2ad1b0c KW |
983 | /* Find the right block driver */ |
984 | drvname = qdict_get_try_str(options, "driver"); | |
985 | if (drvname) { | |
8f94a6e4 | 986 | drv = bdrv_find_format(drvname); |
34b5d2c6 HR |
987 | if (!drv) { |
988 | error_setg(errp, "Unknown driver '%s'", drvname); | |
989 | } | |
c2ad1b0c KW |
990 | qdict_del(options, "driver"); |
991 | } else if (filename) { | |
98289620 KW |
992 | drv = bdrv_find_protocol(filename, allow_protocol_prefix); |
993 | if (!drv) { | |
34b5d2c6 | 994 | error_setg(errp, "Unknown protocol"); |
98289620 | 995 | } |
c2ad1b0c | 996 | } else { |
34b5d2c6 | 997 | error_setg(errp, "Must specify either driver or file"); |
c2ad1b0c KW |
998 | drv = NULL; |
999 | } | |
1000 | ||
1001 | if (!drv) { | |
34b5d2c6 | 1002 | /* errp has been set already */ |
c2ad1b0c KW |
1003 | ret = -ENOENT; |
1004 | goto fail; | |
1005 | } | |
1006 | ||
1007 | /* Parse the filename and open it */ | |
1008 | if (drv->bdrv_parse_filename && filename) { | |
6963a30d KW |
1009 | drv->bdrv_parse_filename(filename, options, &local_err); |
1010 | if (error_is_set(&local_err)) { | |
34b5d2c6 | 1011 | error_propagate(errp, local_err); |
6963a30d KW |
1012 | ret = -EINVAL; |
1013 | goto fail; | |
1014 | } | |
56d1b4d2 | 1015 | qdict_del(options, "filename"); |
030be321 | 1016 | } else if (drv->bdrv_needs_filename && !filename) { |
34b5d2c6 HR |
1017 | error_setg(errp, "The '%s' block driver requires a file name", |
1018 | drv->format_name); | |
c2ad1b0c KW |
1019 | ret = -EINVAL; |
1020 | goto fail; | |
6963a30d KW |
1021 | } |
1022 | ||
505d7583 HR |
1023 | if (!drv->bdrv_file_open) { |
1024 | ret = bdrv_open(bs, filename, options, flags, drv, &local_err); | |
1025 | options = NULL; | |
1026 | } else { | |
1027 | ret = bdrv_open_common(bs, NULL, options, flags, drv, &local_err); | |
1028 | } | |
83f64091 | 1029 | if (ret < 0) { |
34b5d2c6 | 1030 | error_propagate(errp, local_err); |
707ff828 KW |
1031 | goto fail; |
1032 | } | |
1033 | ||
1034 | /* Check if any unknown options were used */ | |
505d7583 | 1035 | if (options && (qdict_size(options) != 0)) { |
707ff828 | 1036 | const QDictEntry *entry = qdict_first(options); |
34b5d2c6 HR |
1037 | error_setg(errp, "Block protocol '%s' doesn't support the option '%s'", |
1038 | drv->format_name, entry->key); | |
707ff828 KW |
1039 | ret = -EINVAL; |
1040 | goto fail; | |
3b0d4f61 | 1041 | } |
707ff828 KW |
1042 | QDECREF(options); |
1043 | ||
71d0770c | 1044 | bs->growable = 1; |
83f64091 FB |
1045 | *pbs = bs; |
1046 | return 0; | |
707ff828 KW |
1047 | |
1048 | fail: | |
1049 | QDECREF(options); | |
1050 | if (!bs->drv) { | |
1051 | QDECREF(bs->options); | |
1052 | } | |
4f6fd349 | 1053 | bdrv_unref(bs); |
707ff828 | 1054 | return ret; |
83f64091 FB |
1055 | } |
1056 | ||
31ca6d07 KW |
1057 | /* |
1058 | * Opens the backing file for a BlockDriverState if not yet open | |
1059 | * | |
1060 | * options is a QDict of options to pass to the block drivers, or NULL for an | |
1061 | * empty set of options. The reference to the QDict is transferred to this | |
1062 | * function (even on failure), so if the caller intends to reuse the dictionary, | |
1063 | * it needs to use QINCREF() before calling bdrv_file_open. | |
1064 | */ | |
34b5d2c6 | 1065 | int bdrv_open_backing_file(BlockDriverState *bs, QDict *options, Error **errp) |
9156df12 PB |
1066 | { |
1067 | char backing_filename[PATH_MAX]; | |
1068 | int back_flags, ret; | |
1069 | BlockDriver *back_drv = NULL; | |
34b5d2c6 | 1070 | Error *local_err = NULL; |
9156df12 PB |
1071 | |
1072 | if (bs->backing_hd != NULL) { | |
31ca6d07 | 1073 | QDECREF(options); |
9156df12 PB |
1074 | return 0; |
1075 | } | |
1076 | ||
31ca6d07 KW |
1077 | /* NULL means an empty set of options */ |
1078 | if (options == NULL) { | |
1079 | options = qdict_new(); | |
1080 | } | |
1081 | ||
9156df12 | 1082 | bs->open_flags &= ~BDRV_O_NO_BACKING; |
1cb6f506 KW |
1083 | if (qdict_haskey(options, "file.filename")) { |
1084 | backing_filename[0] = '\0'; | |
1085 | } else if (bs->backing_file[0] == '\0' && qdict_size(options) == 0) { | |
31ca6d07 | 1086 | QDECREF(options); |
9156df12 | 1087 | return 0; |
dbecebdd FZ |
1088 | } else { |
1089 | bdrv_get_full_backing_filename(bs, backing_filename, | |
1090 | sizeof(backing_filename)); | |
9156df12 PB |
1091 | } |
1092 | ||
1093 | bs->backing_hd = bdrv_new(""); | |
9156df12 PB |
1094 | |
1095 | if (bs->backing_format[0] != '\0') { | |
1096 | back_drv = bdrv_find_format(bs->backing_format); | |
1097 | } | |
1098 | ||
1099 | /* backing files always opened read-only */ | |
87a5debd TL |
1100 | back_flags = bs->open_flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | |
1101 | BDRV_O_COPY_ON_READ); | |
9156df12 | 1102 | |
31ca6d07 KW |
1103 | ret = bdrv_open(bs->backing_hd, |
1104 | *backing_filename ? backing_filename : NULL, options, | |
34b5d2c6 | 1105 | back_flags, back_drv, &local_err); |
9156df12 | 1106 | if (ret < 0) { |
4f6fd349 | 1107 | bdrv_unref(bs->backing_hd); |
9156df12 PB |
1108 | bs->backing_hd = NULL; |
1109 | bs->open_flags |= BDRV_O_NO_BACKING; | |
b04b6b6e FZ |
1110 | error_setg(errp, "Could not open backing file: %s", |
1111 | error_get_pretty(local_err)); | |
1112 | error_free(local_err); | |
9156df12 PB |
1113 | return ret; |
1114 | } | |
d80ac658 PF |
1115 | |
1116 | if (bs->backing_hd->file) { | |
1117 | pstrcpy(bs->backing_file, sizeof(bs->backing_file), | |
1118 | bs->backing_hd->file->filename); | |
1119 | } | |
1120 | ||
d34682cd KW |
1121 | /* Recalculate the BlockLimits with the backing file */ |
1122 | bdrv_refresh_limits(bs); | |
1123 | ||
9156df12 PB |
1124 | return 0; |
1125 | } | |
1126 | ||
da557aac HR |
1127 | /* |
1128 | * Opens a disk image whose options are given as BlockdevRef in another block | |
1129 | * device's options. | |
1130 | * | |
1131 | * If force_raw is true, bdrv_file_open() will be used, thereby preventing any | |
1132 | * image format auto-detection. If it is false and a filename is given, | |
1133 | * bdrv_open() will be used for auto-detection. | |
1134 | * | |
1135 | * If allow_none is true, no image will be opened if filename is false and no | |
1136 | * BlockdevRef is given. *pbs will remain unchanged and 0 will be returned. | |
1137 | * | |
1138 | * bdrev_key specifies the key for the image's BlockdevRef in the options QDict. | |
1139 | * That QDict has to be flattened; therefore, if the BlockdevRef is a QDict | |
1140 | * itself, all options starting with "${bdref_key}." are considered part of the | |
1141 | * BlockdevRef. | |
1142 | * | |
1143 | * The BlockdevRef will be removed from the options QDict. | |
1144 | */ | |
1145 | int bdrv_open_image(BlockDriverState **pbs, const char *filename, | |
1146 | QDict *options, const char *bdref_key, int flags, | |
1147 | bool force_raw, bool allow_none, Error **errp) | |
1148 | { | |
1149 | QDict *image_options; | |
1150 | int ret; | |
1151 | char *bdref_key_dot; | |
1152 | const char *reference; | |
1153 | ||
1154 | bdref_key_dot = g_strdup_printf("%s.", bdref_key); | |
1155 | qdict_extract_subqdict(options, &image_options, bdref_key_dot); | |
1156 | g_free(bdref_key_dot); | |
1157 | ||
1158 | reference = qdict_get_try_str(options, bdref_key); | |
1159 | if (!filename && !reference && !qdict_size(image_options)) { | |
1160 | if (allow_none) { | |
1161 | ret = 0; | |
1162 | } else { | |
1163 | error_setg(errp, "A block device must be specified for \"%s\"", | |
1164 | bdref_key); | |
1165 | ret = -EINVAL; | |
1166 | } | |
1167 | goto done; | |
1168 | } | |
1169 | ||
1170 | if (filename && !force_raw) { | |
1171 | /* If a filename is given and the block driver should be detected | |
1172 | automatically (instead of using none), use bdrv_open() in order to do | |
1173 | that auto-detection. */ | |
1174 | BlockDriverState *bs; | |
1175 | ||
1176 | if (reference) { | |
1177 | error_setg(errp, "Cannot reference an existing block device while " | |
1178 | "giving a filename"); | |
1179 | ret = -EINVAL; | |
1180 | goto done; | |
1181 | } | |
1182 | ||
1183 | bs = bdrv_new(""); | |
1184 | ret = bdrv_open(bs, filename, image_options, flags, NULL, errp); | |
1185 | if (ret < 0) { | |
1186 | bdrv_unref(bs); | |
1187 | } else { | |
1188 | *pbs = bs; | |
1189 | } | |
1190 | } else { | |
1191 | ret = bdrv_file_open(pbs, filename, reference, image_options, flags, | |
1192 | errp); | |
1193 | } | |
1194 | ||
1195 | done: | |
1196 | qdict_del(options, bdref_key); | |
1197 | return ret; | |
1198 | } | |
1199 | ||
b6ce07aa KW |
1200 | /* |
1201 | * Opens a disk image (raw, qcow2, vmdk, ...) | |
de9c0cec KW |
1202 | * |
1203 | * options is a QDict of options to pass to the block drivers, or NULL for an | |
1204 | * empty set of options. The reference to the QDict belongs to the block layer | |
1205 | * after the call (even on failure), so if the caller intends to reuse the | |
1206 | * dictionary, it needs to use QINCREF() before calling bdrv_open. | |
b6ce07aa | 1207 | */ |
de9c0cec | 1208 | int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options, |
34b5d2c6 | 1209 | int flags, BlockDriver *drv, Error **errp) |
ea2384d3 | 1210 | { |
b6ce07aa | 1211 | int ret; |
89c9bc3d SW |
1212 | /* TODO: extra byte is a hack to ensure MAX_PATH space on Windows. */ |
1213 | char tmp_filename[PATH_MAX + 1]; | |
f500a6d3 | 1214 | BlockDriverState *file = NULL; |
74fe54f2 | 1215 | const char *drvname; |
34b5d2c6 | 1216 | Error *local_err = NULL; |
712e7874 | 1217 | |
de9c0cec KW |
1218 | /* NULL means an empty set of options */ |
1219 | if (options == NULL) { | |
1220 | options = qdict_new(); | |
1221 | } | |
1222 | ||
1223 | bs->options = options; | |
b6ad491a | 1224 | options = qdict_clone_shallow(options); |
de9c0cec KW |
1225 | |
1226 | /* For snapshot=on, create a temporary qcow2 overlay */ | |
83f64091 | 1227 | if (flags & BDRV_O_SNAPSHOT) { |
ea2384d3 FB |
1228 | BlockDriverState *bs1; |
1229 | int64_t total_size; | |
91a073a9 | 1230 | BlockDriver *bdrv_qcow2; |
08b392e1 | 1231 | QEMUOptionParameter *create_options; |
9fd3171a | 1232 | QDict *snapshot_options; |
c2ad1b0c | 1233 | |
ea2384d3 FB |
1234 | /* if snapshot, we create a temporary backing file and open it |
1235 | instead of opening 'filename' directly */ | |
33e3963e | 1236 | |
9fd3171a | 1237 | /* Get the required size from the image */ |
ea2384d3 | 1238 | bs1 = bdrv_new(""); |
9fd3171a | 1239 | QINCREF(options); |
c9fbb99d KW |
1240 | ret = bdrv_open(bs1, filename, options, BDRV_O_NO_BACKING, |
1241 | drv, &local_err); | |
51d7c00c | 1242 | if (ret < 0) { |
4f6fd349 | 1243 | bdrv_unref(bs1); |
de9c0cec | 1244 | goto fail; |
ea2384d3 | 1245 | } |
3e82990b | 1246 | total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK; |
7c96d46e | 1247 | |
4f6fd349 | 1248 | bdrv_unref(bs1); |
3b46e624 | 1249 | |
9fd3171a | 1250 | /* Create the temporary image */ |
eba25057 JM |
1251 | ret = get_tmp_filename(tmp_filename, sizeof(tmp_filename)); |
1252 | if (ret < 0) { | |
34b5d2c6 | 1253 | error_setg_errno(errp, -ret, "Could not get temporary filename"); |
de9c0cec | 1254 | goto fail; |
eba25057 | 1255 | } |
7c96d46e | 1256 | |
91a073a9 | 1257 | bdrv_qcow2 = bdrv_find_format("qcow2"); |
08b392e1 KW |
1258 | create_options = parse_option_parameters("", bdrv_qcow2->create_options, |
1259 | NULL); | |
91a073a9 | 1260 | |
08b392e1 | 1261 | set_option_parameter_int(create_options, BLOCK_OPT_SIZE, total_size); |
91a073a9 | 1262 | |
cc84d90f | 1263 | ret = bdrv_create(bdrv_qcow2, tmp_filename, create_options, &local_err); |
08b392e1 | 1264 | free_option_parameters(create_options); |
51d7c00c | 1265 | if (ret < 0) { |
34b5d2c6 | 1266 | error_setg_errno(errp, -ret, "Could not create temporary overlay " |
cc84d90f HR |
1267 | "'%s': %s", tmp_filename, |
1268 | error_get_pretty(local_err)); | |
1269 | error_free(local_err); | |
1270 | local_err = NULL; | |
de9c0cec | 1271 | goto fail; |
ea2384d3 | 1272 | } |
91a073a9 | 1273 | |
9fd3171a KW |
1274 | /* Prepare a new options QDict for the temporary file, where user |
1275 | * options refer to the backing file */ | |
1276 | if (filename) { | |
1277 | qdict_put(options, "file.filename", qstring_from_str(filename)); | |
1278 | } | |
1279 | if (drv) { | |
1280 | qdict_put(options, "driver", qstring_from_str(drv->format_name)); | |
1281 | } | |
1282 | ||
1283 | snapshot_options = qdict_new(); | |
1284 | qdict_put(snapshot_options, "backing", options); | |
1285 | qdict_flatten(snapshot_options); | |
1286 | ||
1287 | bs->options = snapshot_options; | |
1288 | options = qdict_clone_shallow(bs->options); | |
1289 | ||
ea2384d3 | 1290 | filename = tmp_filename; |
91a073a9 | 1291 | drv = bdrv_qcow2; |
ea2384d3 FB |
1292 | bs->is_temporary = 1; |
1293 | } | |
712e7874 | 1294 | |
f500a6d3 KW |
1295 | /* Open image file without format layer */ |
1296 | if (flags & BDRV_O_RDWR) { | |
1297 | flags |= BDRV_O_ALLOW_RDWR; | |
1298 | } | |
1299 | ||
054963f8 HR |
1300 | ret = bdrv_open_image(&file, filename, options, "file", |
1301 | bdrv_open_flags(bs, flags | BDRV_O_UNMAP), true, true, | |
1302 | &local_err); | |
1303 | if (ret < 0) { | |
1304 | goto fail; | |
f500a6d3 KW |
1305 | } |
1306 | ||
b6ce07aa | 1307 | /* Find the right image format driver */ |
74fe54f2 KW |
1308 | drvname = qdict_get_try_str(options, "driver"); |
1309 | if (drvname) { | |
8f94a6e4 | 1310 | drv = bdrv_find_format(drvname); |
74fe54f2 | 1311 | qdict_del(options, "driver"); |
06d22aa3 KW |
1312 | if (!drv) { |
1313 | error_setg(errp, "Invalid driver: '%s'", drvname); | |
1314 | ret = -EINVAL; | |
1315 | goto unlink_and_fail; | |
1316 | } | |
74fe54f2 KW |
1317 | } |
1318 | ||
6db95603 | 1319 | if (!drv) { |
2a05cbe4 HR |
1320 | if (file) { |
1321 | ret = find_image_format(file, filename, &drv, &local_err); | |
1322 | } else { | |
1323 | error_setg(errp, "Must specify either driver or file"); | |
1324 | ret = -EINVAL; | |
1325 | goto unlink_and_fail; | |
1326 | } | |
51d7c00c | 1327 | } |
6987307c | 1328 | |
51d7c00c | 1329 | if (!drv) { |
51d7c00c | 1330 | goto unlink_and_fail; |
ea2384d3 | 1331 | } |
b6ce07aa KW |
1332 | |
1333 | /* Open the image */ | |
34b5d2c6 | 1334 | ret = bdrv_open_common(bs, file, options, flags, drv, &local_err); |
b6ce07aa | 1335 | if (ret < 0) { |
6987307c CH |
1336 | goto unlink_and_fail; |
1337 | } | |
1338 | ||
2a05cbe4 | 1339 | if (file && (bs->file != file)) { |
4f6fd349 | 1340 | bdrv_unref(file); |
f500a6d3 KW |
1341 | file = NULL; |
1342 | } | |
1343 | ||
b6ce07aa | 1344 | /* If there is a backing file, use it */ |
9156df12 | 1345 | if ((flags & BDRV_O_NO_BACKING) == 0) { |
31ca6d07 KW |
1346 | QDict *backing_options; |
1347 | ||
5726d872 | 1348 | qdict_extract_subqdict(options, &backing_options, "backing."); |
34b5d2c6 | 1349 | ret = bdrv_open_backing_file(bs, backing_options, &local_err); |
b6ce07aa | 1350 | if (ret < 0) { |
b6ad491a | 1351 | goto close_and_fail; |
b6ce07aa | 1352 | } |
b6ce07aa KW |
1353 | } |
1354 | ||
b6ad491a KW |
1355 | /* Check if any unknown options were used */ |
1356 | if (qdict_size(options) != 0) { | |
1357 | const QDictEntry *entry = qdict_first(options); | |
34b5d2c6 HR |
1358 | error_setg(errp, "Block format '%s' used by device '%s' doesn't " |
1359 | "support the option '%s'", drv->format_name, bs->device_name, | |
1360 | entry->key); | |
b6ad491a KW |
1361 | |
1362 | ret = -EINVAL; | |
1363 | goto close_and_fail; | |
1364 | } | |
1365 | QDECREF(options); | |
1366 | ||
b6ce07aa | 1367 | if (!bdrv_key_required(bs)) { |
7d4b4ba5 | 1368 | bdrv_dev_change_media_cb(bs, true); |
b6ce07aa KW |
1369 | } |
1370 | ||
1371 | return 0; | |
1372 | ||
1373 | unlink_and_fail: | |
f500a6d3 | 1374 | if (file != NULL) { |
4f6fd349 | 1375 | bdrv_unref(file); |
f500a6d3 | 1376 | } |
b6ce07aa KW |
1377 | if (bs->is_temporary) { |
1378 | unlink(filename); | |
1379 | } | |
de9c0cec KW |
1380 | fail: |
1381 | QDECREF(bs->options); | |
b6ad491a | 1382 | QDECREF(options); |
de9c0cec | 1383 | bs->options = NULL; |
34b5d2c6 HR |
1384 | if (error_is_set(&local_err)) { |
1385 | error_propagate(errp, local_err); | |
1386 | } | |
b6ad491a | 1387 | return ret; |
de9c0cec | 1388 | |
b6ad491a KW |
1389 | close_and_fail: |
1390 | bdrv_close(bs); | |
1391 | QDECREF(options); | |
34b5d2c6 HR |
1392 | if (error_is_set(&local_err)) { |
1393 | error_propagate(errp, local_err); | |
1394 | } | |
b6ce07aa KW |
1395 | return ret; |
1396 | } | |
1397 | ||
e971aa12 JC |
1398 | typedef struct BlockReopenQueueEntry { |
1399 | bool prepared; | |
1400 | BDRVReopenState state; | |
1401 | QSIMPLEQ_ENTRY(BlockReopenQueueEntry) entry; | |
1402 | } BlockReopenQueueEntry; | |
1403 | ||
1404 | /* | |
1405 | * Adds a BlockDriverState to a simple queue for an atomic, transactional | |
1406 | * reopen of multiple devices. | |
1407 | * | |
1408 | * bs_queue can either be an existing BlockReopenQueue that has had QSIMPLE_INIT | |
1409 | * already performed, or alternatively may be NULL a new BlockReopenQueue will | |
1410 | * be created and initialized. This newly created BlockReopenQueue should be | |
1411 | * passed back in for subsequent calls that are intended to be of the same | |
1412 | * atomic 'set'. | |
1413 | * | |
1414 | * bs is the BlockDriverState to add to the reopen queue. | |
1415 | * | |
1416 | * flags contains the open flags for the associated bs | |
1417 | * | |
1418 | * returns a pointer to bs_queue, which is either the newly allocated | |
1419 | * bs_queue, or the existing bs_queue being used. | |
1420 | * | |
1421 | */ | |
1422 | BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue, | |
1423 | BlockDriverState *bs, int flags) | |
1424 | { | |
1425 | assert(bs != NULL); | |
1426 | ||
1427 | BlockReopenQueueEntry *bs_entry; | |
1428 | if (bs_queue == NULL) { | |
1429 | bs_queue = g_new0(BlockReopenQueue, 1); | |
1430 | QSIMPLEQ_INIT(bs_queue); | |
1431 | } | |
1432 | ||
1433 | if (bs->file) { | |
1434 | bdrv_reopen_queue(bs_queue, bs->file, flags); | |
1435 | } | |
1436 | ||
1437 | bs_entry = g_new0(BlockReopenQueueEntry, 1); | |
1438 | QSIMPLEQ_INSERT_TAIL(bs_queue, bs_entry, entry); | |
1439 | ||
1440 | bs_entry->state.bs = bs; | |
1441 | bs_entry->state.flags = flags; | |
1442 | ||
1443 | return bs_queue; | |
1444 | } | |
1445 | ||
1446 | /* | |
1447 | * Reopen multiple BlockDriverStates atomically & transactionally. | |
1448 | * | |
1449 | * The queue passed in (bs_queue) must have been built up previous | |
1450 | * via bdrv_reopen_queue(). | |
1451 | * | |
1452 | * Reopens all BDS specified in the queue, with the appropriate | |
1453 | * flags. All devices are prepared for reopen, and failure of any | |
1454 | * device will cause all device changes to be abandonded, and intermediate | |
1455 | * data cleaned up. | |
1456 | * | |
1457 | * If all devices prepare successfully, then the changes are committed | |
1458 | * to all devices. | |
1459 | * | |
1460 | */ | |
1461 | int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp) | |
1462 | { | |
1463 | int ret = -1; | |
1464 | BlockReopenQueueEntry *bs_entry, *next; | |
1465 | Error *local_err = NULL; | |
1466 | ||
1467 | assert(bs_queue != NULL); | |
1468 | ||
1469 | bdrv_drain_all(); | |
1470 | ||
1471 | QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) { | |
1472 | if (bdrv_reopen_prepare(&bs_entry->state, bs_queue, &local_err)) { | |
1473 | error_propagate(errp, local_err); | |
1474 | goto cleanup; | |
1475 | } | |
1476 | bs_entry->prepared = true; | |
1477 | } | |
1478 | ||
1479 | /* If we reach this point, we have success and just need to apply the | |
1480 | * changes | |
1481 | */ | |
1482 | QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) { | |
1483 | bdrv_reopen_commit(&bs_entry->state); | |
1484 | } | |
1485 | ||
1486 | ret = 0; | |
1487 | ||
1488 | cleanup: | |
1489 | QSIMPLEQ_FOREACH_SAFE(bs_entry, bs_queue, entry, next) { | |
1490 | if (ret && bs_entry->prepared) { | |
1491 | bdrv_reopen_abort(&bs_entry->state); | |
1492 | } | |
1493 | g_free(bs_entry); | |
1494 | } | |
1495 | g_free(bs_queue); | |
1496 | return ret; | |
1497 | } | |
1498 | ||
1499 | ||
1500 | /* Reopen a single BlockDriverState with the specified flags. */ | |
1501 | int bdrv_reopen(BlockDriverState *bs, int bdrv_flags, Error **errp) | |
1502 | { | |
1503 | int ret = -1; | |
1504 | Error *local_err = NULL; | |
1505 | BlockReopenQueue *queue = bdrv_reopen_queue(NULL, bs, bdrv_flags); | |
1506 | ||
1507 | ret = bdrv_reopen_multiple(queue, &local_err); | |
1508 | if (local_err != NULL) { | |
1509 | error_propagate(errp, local_err); | |
1510 | } | |
1511 | return ret; | |
1512 | } | |
1513 | ||
1514 | ||
1515 | /* | |
1516 | * Prepares a BlockDriverState for reopen. All changes are staged in the | |
1517 | * 'opaque' field of the BDRVReopenState, which is used and allocated by | |
1518 | * the block driver layer .bdrv_reopen_prepare() | |
1519 | * | |
1520 | * bs is the BlockDriverState to reopen | |
1521 | * flags are the new open flags | |
1522 | * queue is the reopen queue | |
1523 | * | |
1524 | * Returns 0 on success, non-zero on error. On error errp will be set | |
1525 | * as well. | |
1526 | * | |
1527 | * On failure, bdrv_reopen_abort() will be called to clean up any data. | |
1528 | * It is the responsibility of the caller to then call the abort() or | |
1529 | * commit() for any other BDS that have been left in a prepare() state | |
1530 | * | |
1531 | */ | |
1532 | int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue, | |
1533 | Error **errp) | |
1534 | { | |
1535 | int ret = -1; | |
1536 | Error *local_err = NULL; | |
1537 | BlockDriver *drv; | |
1538 | ||
1539 | assert(reopen_state != NULL); | |
1540 | assert(reopen_state->bs->drv != NULL); | |
1541 | drv = reopen_state->bs->drv; | |
1542 | ||
1543 | /* if we are to stay read-only, do not allow permission change | |
1544 | * to r/w */ | |
1545 | if (!(reopen_state->bs->open_flags & BDRV_O_ALLOW_RDWR) && | |
1546 | reopen_state->flags & BDRV_O_RDWR) { | |
1547 | error_set(errp, QERR_DEVICE_IS_READ_ONLY, | |
1548 | reopen_state->bs->device_name); | |
1549 | goto error; | |
1550 | } | |
1551 | ||
1552 | ||
1553 | ret = bdrv_flush(reopen_state->bs); | |
1554 | if (ret) { | |
1555 | error_set(errp, ERROR_CLASS_GENERIC_ERROR, "Error (%s) flushing drive", | |
1556 | strerror(-ret)); | |
1557 | goto error; | |
1558 | } | |
1559 | ||
1560 | if (drv->bdrv_reopen_prepare) { | |
1561 | ret = drv->bdrv_reopen_prepare(reopen_state, queue, &local_err); | |
1562 | if (ret) { | |
1563 | if (local_err != NULL) { | |
1564 | error_propagate(errp, local_err); | |
1565 | } else { | |
d8b6895f LC |
1566 | error_setg(errp, "failed while preparing to reopen image '%s'", |
1567 | reopen_state->bs->filename); | |
e971aa12 JC |
1568 | } |
1569 | goto error; | |
1570 | } | |
1571 | } else { | |
1572 | /* It is currently mandatory to have a bdrv_reopen_prepare() | |
1573 | * handler for each supported drv. */ | |
1574 | error_set(errp, QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED, | |
1575 | drv->format_name, reopen_state->bs->device_name, | |
1576 | "reopening of file"); | |
1577 | ret = -1; | |
1578 | goto error; | |
1579 | } | |
1580 | ||
1581 | ret = 0; | |
1582 | ||
1583 | error: | |
1584 | return ret; | |
1585 | } | |
1586 | ||
1587 | /* | |
1588 | * Takes the staged changes for the reopen from bdrv_reopen_prepare(), and | |
1589 | * makes them final by swapping the staging BlockDriverState contents into | |
1590 | * the active BlockDriverState contents. | |
1591 | */ | |
1592 | void bdrv_reopen_commit(BDRVReopenState *reopen_state) | |
1593 | { | |
1594 | BlockDriver *drv; | |
1595 | ||
1596 | assert(reopen_state != NULL); | |
1597 | drv = reopen_state->bs->drv; | |
1598 | assert(drv != NULL); | |
1599 | ||
1600 | /* If there are any driver level actions to take */ | |
1601 | if (drv->bdrv_reopen_commit) { | |
1602 | drv->bdrv_reopen_commit(reopen_state); | |
1603 | } | |
1604 | ||
1605 | /* set BDS specific flags now */ | |
1606 | reopen_state->bs->open_flags = reopen_state->flags; | |
1607 | reopen_state->bs->enable_write_cache = !!(reopen_state->flags & | |
1608 | BDRV_O_CACHE_WB); | |
1609 | reopen_state->bs->read_only = !(reopen_state->flags & BDRV_O_RDWR); | |
1610 | } | |
1611 | ||
1612 | /* | |
1613 | * Abort the reopen, and delete and free the staged changes in | |
1614 | * reopen_state | |
1615 | */ | |
1616 | void bdrv_reopen_abort(BDRVReopenState *reopen_state) | |
1617 | { | |
1618 | BlockDriver *drv; | |
1619 | ||
1620 | assert(reopen_state != NULL); | |
1621 | drv = reopen_state->bs->drv; | |
1622 | assert(drv != NULL); | |
1623 | ||
1624 | if (drv->bdrv_reopen_abort) { | |
1625 | drv->bdrv_reopen_abort(reopen_state); | |
1626 | } | |
1627 | } | |
1628 | ||
1629 | ||
fc01f7e7 FB |
1630 | void bdrv_close(BlockDriverState *bs) |
1631 | { | |
3cbc002c PB |
1632 | if (bs->job) { |
1633 | block_job_cancel_sync(bs->job); | |
1634 | } | |
58fda173 SH |
1635 | bdrv_drain_all(); /* complete I/O */ |
1636 | bdrv_flush(bs); | |
1637 | bdrv_drain_all(); /* in case flush left pending I/O */ | |
d7d512f6 | 1638 | notifier_list_notify(&bs->close_notifiers, bs); |
7094f12f | 1639 | |
3cbc002c | 1640 | if (bs->drv) { |
557df6ac | 1641 | if (bs->backing_hd) { |
4f6fd349 | 1642 | bdrv_unref(bs->backing_hd); |
557df6ac SH |
1643 | bs->backing_hd = NULL; |
1644 | } | |
ea2384d3 | 1645 | bs->drv->bdrv_close(bs); |
7267c094 | 1646 | g_free(bs->opaque); |
ea2384d3 FB |
1647 | #ifdef _WIN32 |
1648 | if (bs->is_temporary) { | |
1649 | unlink(bs->filename); | |
1650 | } | |
67b915a5 | 1651 | #endif |
ea2384d3 FB |
1652 | bs->opaque = NULL; |
1653 | bs->drv = NULL; | |
53fec9d3 | 1654 | bs->copy_on_read = 0; |
a275fa42 PB |
1655 | bs->backing_file[0] = '\0'; |
1656 | bs->backing_format[0] = '\0'; | |
6405875c PB |
1657 | bs->total_sectors = 0; |
1658 | bs->encrypted = 0; | |
1659 | bs->valid_key = 0; | |
1660 | bs->sg = 0; | |
1661 | bs->growable = 0; | |
0d51b4de | 1662 | bs->zero_beyond_eof = false; |
de9c0cec KW |
1663 | QDECREF(bs->options); |
1664 | bs->options = NULL; | |
b338082b | 1665 | |
66f82cee | 1666 | if (bs->file != NULL) { |
4f6fd349 | 1667 | bdrv_unref(bs->file); |
0ac9377d | 1668 | bs->file = NULL; |
66f82cee | 1669 | } |
b338082b | 1670 | } |
98f90dba | 1671 | |
9ca11154 PH |
1672 | bdrv_dev_change_media_cb(bs, false); |
1673 | ||
98f90dba ZYW |
1674 | /*throttling disk I/O limits*/ |
1675 | if (bs->io_limits_enabled) { | |
1676 | bdrv_io_limits_disable(bs); | |
1677 | } | |
b338082b FB |
1678 | } |
1679 | ||
2bc93fed MK |
1680 | void bdrv_close_all(void) |
1681 | { | |
1682 | BlockDriverState *bs; | |
1683 | ||
dc364f4c | 1684 | QTAILQ_FOREACH(bs, &bdrv_states, device_list) { |
2bc93fed MK |
1685 | bdrv_close(bs); |
1686 | } | |
1687 | } | |
1688 | ||
88266f5a SH |
1689 | /* Check if any requests are in-flight (including throttled requests) */ |
1690 | static bool bdrv_requests_pending(BlockDriverState *bs) | |
1691 | { | |
1692 | if (!QLIST_EMPTY(&bs->tracked_requests)) { | |
1693 | return true; | |
1694 | } | |
cc0681c4 BC |
1695 | if (!qemu_co_queue_empty(&bs->throttled_reqs[0])) { |
1696 | return true; | |
1697 | } | |
1698 | if (!qemu_co_queue_empty(&bs->throttled_reqs[1])) { | |
88266f5a SH |
1699 | return true; |
1700 | } | |
1701 | if (bs->file && bdrv_requests_pending(bs->file)) { | |
1702 | return true; | |
1703 | } | |
1704 | if (bs->backing_hd && bdrv_requests_pending(bs->backing_hd)) { | |
1705 | return true; | |
1706 | } | |
1707 | return false; | |
1708 | } | |
1709 | ||
1710 | static bool bdrv_requests_pending_all(void) | |
1711 | { | |
1712 | BlockDriverState *bs; | |
dc364f4c | 1713 | QTAILQ_FOREACH(bs, &bdrv_states, device_list) { |
88266f5a SH |
1714 | if (bdrv_requests_pending(bs)) { |
1715 | return true; | |
1716 | } | |
1717 | } | |
1718 | return false; | |
1719 | } | |
1720 | ||
922453bc SH |
1721 | /* |
1722 | * Wait for pending requests to complete across all BlockDriverStates | |
1723 | * | |
1724 | * This function does not flush data to disk, use bdrv_flush_all() for that | |
1725 | * after calling this function. | |
4c355d53 ZYW |
1726 | * |
1727 | * Note that completion of an asynchronous I/O operation can trigger any | |
1728 | * number of other I/O operations on other devices---for example a coroutine | |
1729 | * can be arbitrarily complex and a constant flow of I/O can come until the | |
1730 | * coroutine is complete. Because of this, it is not possible to have a | |
1731 | * function to drain a single device's I/O queue. | |
922453bc SH |
1732 | */ |
1733 | void bdrv_drain_all(void) | |
1734 | { | |
88266f5a SH |
1735 | /* Always run first iteration so any pending completion BHs run */ |
1736 | bool busy = true; | |
922453bc SH |
1737 | BlockDriverState *bs; |
1738 | ||
88266f5a | 1739 | while (busy) { |
dc364f4c | 1740 | QTAILQ_FOREACH(bs, &bdrv_states, device_list) { |
0b06ef3b | 1741 | bdrv_start_throttled_reqs(bs); |
4c355d53 | 1742 | } |
922453bc | 1743 | |
88266f5a SH |
1744 | busy = bdrv_requests_pending_all(); |
1745 | busy |= aio_poll(qemu_get_aio_context(), busy); | |
922453bc SH |
1746 | } |
1747 | } | |
1748 | ||
dc364f4c BC |
1749 | /* make a BlockDriverState anonymous by removing from bdrv_state and |
1750 | * graph_bdrv_state list. | |
d22b2f41 RH |
1751 | Also, NULL terminate the device_name to prevent double remove */ |
1752 | void bdrv_make_anon(BlockDriverState *bs) | |
1753 | { | |
1754 | if (bs->device_name[0] != '\0') { | |
dc364f4c | 1755 | QTAILQ_REMOVE(&bdrv_states, bs, device_list); |
d22b2f41 RH |
1756 | } |
1757 | bs->device_name[0] = '\0'; | |
dc364f4c BC |
1758 | if (bs->node_name[0] != '\0') { |
1759 | QTAILQ_REMOVE(&graph_bdrv_states, bs, node_list); | |
1760 | } | |
1761 | bs->node_name[0] = '\0'; | |
d22b2f41 RH |
1762 | } |
1763 | ||
e023b2e2 PB |
1764 | static void bdrv_rebind(BlockDriverState *bs) |
1765 | { | |
1766 | if (bs->drv && bs->drv->bdrv_rebind) { | |
1767 | bs->drv->bdrv_rebind(bs); | |
1768 | } | |
1769 | } | |
1770 | ||
4ddc07ca PB |
1771 | static void bdrv_move_feature_fields(BlockDriverState *bs_dest, |
1772 | BlockDriverState *bs_src) | |
8802d1fd | 1773 | { |
4ddc07ca PB |
1774 | /* move some fields that need to stay attached to the device */ |
1775 | bs_dest->open_flags = bs_src->open_flags; | |
8802d1fd JC |
1776 | |
1777 | /* dev info */ | |
4ddc07ca PB |
1778 | bs_dest->dev_ops = bs_src->dev_ops; |
1779 | bs_dest->dev_opaque = bs_src->dev_opaque; | |
1780 | bs_dest->dev = bs_src->dev; | |
1781 | bs_dest->buffer_alignment = bs_src->buffer_alignment; | |
1782 | bs_dest->copy_on_read = bs_src->copy_on_read; | |
8802d1fd | 1783 | |
4ddc07ca | 1784 | bs_dest->enable_write_cache = bs_src->enable_write_cache; |
c4a248a1 | 1785 | |
cc0681c4 BC |
1786 | /* i/o throttled req */ |
1787 | memcpy(&bs_dest->throttle_state, | |
1788 | &bs_src->throttle_state, | |
1789 | sizeof(ThrottleState)); | |
1790 | bs_dest->throttled_reqs[0] = bs_src->throttled_reqs[0]; | |
1791 | bs_dest->throttled_reqs[1] = bs_src->throttled_reqs[1]; | |
4ddc07ca | 1792 | bs_dest->io_limits_enabled = bs_src->io_limits_enabled; |
8802d1fd | 1793 | |
8802d1fd | 1794 | /* r/w error */ |
4ddc07ca PB |
1795 | bs_dest->on_read_error = bs_src->on_read_error; |
1796 | bs_dest->on_write_error = bs_src->on_write_error; | |
8802d1fd JC |
1797 | |
1798 | /* i/o status */ | |
4ddc07ca PB |
1799 | bs_dest->iostatus_enabled = bs_src->iostatus_enabled; |
1800 | bs_dest->iostatus = bs_src->iostatus; | |
8802d1fd | 1801 | |
a9fc4408 | 1802 | /* dirty bitmap */ |
e4654d2d | 1803 | bs_dest->dirty_bitmaps = bs_src->dirty_bitmaps; |
a9fc4408 | 1804 | |
9fcb0251 FZ |
1805 | /* reference count */ |
1806 | bs_dest->refcnt = bs_src->refcnt; | |
1807 | ||
a9fc4408 | 1808 | /* job */ |
4ddc07ca PB |
1809 | bs_dest->in_use = bs_src->in_use; |
1810 | bs_dest->job = bs_src->job; | |
a9fc4408 | 1811 | |
8802d1fd | 1812 | /* keep the same entry in bdrv_states */ |
4ddc07ca PB |
1813 | pstrcpy(bs_dest->device_name, sizeof(bs_dest->device_name), |
1814 | bs_src->device_name); | |
dc364f4c BC |
1815 | bs_dest->device_list = bs_src->device_list; |
1816 | ||
1817 | /* keep the same entry in graph_bdrv_states | |
1818 | * We do want to swap name but don't want to swap linked list entries | |
1819 | */ | |
1820 | bs_dest->node_list = bs_src->node_list; | |
4ddc07ca | 1821 | } |
8802d1fd | 1822 | |
4ddc07ca PB |
1823 | /* |
1824 | * Swap bs contents for two image chains while they are live, | |
1825 | * while keeping required fields on the BlockDriverState that is | |
1826 | * actually attached to a device. | |
1827 | * | |
1828 | * This will modify the BlockDriverState fields, and swap contents | |
1829 | * between bs_new and bs_old. Both bs_new and bs_old are modified. | |
1830 | * | |
1831 | * bs_new is required to be anonymous. | |
1832 | * | |
1833 | * This function does not create any image files. | |
1834 | */ | |
1835 | void bdrv_swap(BlockDriverState *bs_new, BlockDriverState *bs_old) | |
1836 | { | |
1837 | BlockDriverState tmp; | |
f6801b83 | 1838 | |
4ddc07ca PB |
1839 | /* bs_new must be anonymous and shouldn't have anything fancy enabled */ |
1840 | assert(bs_new->device_name[0] == '\0'); | |
e4654d2d | 1841 | assert(QLIST_EMPTY(&bs_new->dirty_bitmaps)); |
4ddc07ca PB |
1842 | assert(bs_new->job == NULL); |
1843 | assert(bs_new->dev == NULL); | |
1844 | assert(bs_new->in_use == 0); | |
1845 | assert(bs_new->io_limits_enabled == false); | |
cc0681c4 | 1846 | assert(!throttle_have_timer(&bs_new->throttle_state)); |
8802d1fd | 1847 | |
4ddc07ca PB |
1848 | tmp = *bs_new; |
1849 | *bs_new = *bs_old; | |
1850 | *bs_old = tmp; | |
a9fc4408 | 1851 | |
4ddc07ca PB |
1852 | /* there are some fields that should not be swapped, move them back */ |
1853 | bdrv_move_feature_fields(&tmp, bs_old); | |
1854 | bdrv_move_feature_fields(bs_old, bs_new); | |
1855 | bdrv_move_feature_fields(bs_new, &tmp); | |
8802d1fd | 1856 | |
4ddc07ca PB |
1857 | /* bs_new shouldn't be in bdrv_states even after the swap! */ |
1858 | assert(bs_new->device_name[0] == '\0'); | |
1859 | ||
1860 | /* Check a few fields that should remain attached to the device */ | |
1861 | assert(bs_new->dev == NULL); | |
1862 | assert(bs_new->job == NULL); | |
1863 | assert(bs_new->in_use == 0); | |
1864 | assert(bs_new->io_limits_enabled == false); | |
cc0681c4 | 1865 | assert(!throttle_have_timer(&bs_new->throttle_state)); |
e023b2e2 PB |
1866 | |
1867 | bdrv_rebind(bs_new); | |
4ddc07ca PB |
1868 | bdrv_rebind(bs_old); |
1869 | } | |
1870 | ||
1871 | /* | |
1872 | * Add new bs contents at the top of an image chain while the chain is | |
1873 | * live, while keeping required fields on the top layer. | |
1874 | * | |
1875 | * This will modify the BlockDriverState fields, and swap contents | |
1876 | * between bs_new and bs_top. Both bs_new and bs_top are modified. | |
1877 | * | |
1878 | * bs_new is required to be anonymous. | |
1879 | * | |
1880 | * This function does not create any image files. | |
1881 | */ | |
1882 | void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top) | |
1883 | { | |
1884 | bdrv_swap(bs_new, bs_top); | |
1885 | ||
1886 | /* The contents of 'tmp' will become bs_top, as we are | |
1887 | * swapping bs_new and bs_top contents. */ | |
1888 | bs_top->backing_hd = bs_new; | |
1889 | bs_top->open_flags &= ~BDRV_O_NO_BACKING; | |
1890 | pstrcpy(bs_top->backing_file, sizeof(bs_top->backing_file), | |
1891 | bs_new->filename); | |
1892 | pstrcpy(bs_top->backing_format, sizeof(bs_top->backing_format), | |
1893 | bs_new->drv ? bs_new->drv->format_name : ""); | |
8802d1fd JC |
1894 | } |
1895 | ||
4f6fd349 | 1896 | static void bdrv_delete(BlockDriverState *bs) |
b338082b | 1897 | { |
fa879d62 | 1898 | assert(!bs->dev); |
3e914655 PB |
1899 | assert(!bs->job); |
1900 | assert(!bs->in_use); | |
4f6fd349 | 1901 | assert(!bs->refcnt); |
e4654d2d | 1902 | assert(QLIST_EMPTY(&bs->dirty_bitmaps)); |
18846dee | 1903 | |
e1b5c52e SH |
1904 | bdrv_close(bs); |
1905 | ||
1b7bdbc1 | 1906 | /* remove from list, if necessary */ |
d22b2f41 | 1907 | bdrv_make_anon(bs); |
34c6f050 | 1908 | |
7267c094 | 1909 | g_free(bs); |
fc01f7e7 FB |
1910 | } |
1911 | ||
fa879d62 MA |
1912 | int bdrv_attach_dev(BlockDriverState *bs, void *dev) |
1913 | /* TODO change to DeviceState *dev when all users are qdevified */ | |
18846dee | 1914 | { |
fa879d62 | 1915 | if (bs->dev) { |
18846dee MA |
1916 | return -EBUSY; |
1917 | } | |
fa879d62 | 1918 | bs->dev = dev; |
28a7282a | 1919 | bdrv_iostatus_reset(bs); |
18846dee MA |
1920 | return 0; |
1921 | } | |
1922 | ||
fa879d62 MA |
1923 | /* TODO qdevified devices don't use this, remove when devices are qdevified */ |
1924 | void bdrv_attach_dev_nofail(BlockDriverState *bs, void *dev) | |
18846dee | 1925 | { |
fa879d62 MA |
1926 | if (bdrv_attach_dev(bs, dev) < 0) { |
1927 | abort(); | |
1928 | } | |
1929 | } | |
1930 | ||
1931 | void bdrv_detach_dev(BlockDriverState *bs, void *dev) | |
1932 | /* TODO change to DeviceState *dev when all users are qdevified */ | |
1933 | { | |
1934 | assert(bs->dev == dev); | |
1935 | bs->dev = NULL; | |
0e49de52 MA |
1936 | bs->dev_ops = NULL; |
1937 | bs->dev_opaque = NULL; | |
29e05f20 | 1938 | bs->buffer_alignment = 512; |
18846dee MA |
1939 | } |
1940 | ||
fa879d62 MA |
1941 | /* TODO change to return DeviceState * when all users are qdevified */ |
1942 | void *bdrv_get_attached_dev(BlockDriverState *bs) | |
18846dee | 1943 | { |
fa879d62 | 1944 | return bs->dev; |
18846dee MA |
1945 | } |
1946 | ||
0e49de52 MA |
1947 | void bdrv_set_dev_ops(BlockDriverState *bs, const BlockDevOps *ops, |
1948 | void *opaque) | |
1949 | { | |
1950 | bs->dev_ops = ops; | |
1951 | bs->dev_opaque = opaque; | |
1952 | } | |
1953 | ||
32c81a4a PB |
1954 | void bdrv_emit_qmp_error_event(const BlockDriverState *bdrv, |
1955 | enum MonitorEvent ev, | |
1956 | BlockErrorAction action, bool is_read) | |
329c0a48 LC |
1957 | { |
1958 | QObject *data; | |
1959 | const char *action_str; | |
1960 | ||
1961 | switch (action) { | |
1962 | case BDRV_ACTION_REPORT: | |
1963 | action_str = "report"; | |
1964 | break; | |
1965 | case BDRV_ACTION_IGNORE: | |
1966 | action_str = "ignore"; | |
1967 | break; | |
1968 | case BDRV_ACTION_STOP: | |
1969 | action_str = "stop"; | |
1970 | break; | |
1971 | default: | |
1972 | abort(); | |
1973 | } | |
1974 | ||
1975 | data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }", | |
1976 | bdrv->device_name, | |
1977 | action_str, | |
1978 | is_read ? "read" : "write"); | |
32c81a4a | 1979 | monitor_protocol_event(ev, data); |
329c0a48 LC |
1980 | |
1981 | qobject_decref(data); | |
1982 | } | |
1983 | ||
6f382ed2 LC |
1984 | static void bdrv_emit_qmp_eject_event(BlockDriverState *bs, bool ejected) |
1985 | { | |
1986 | QObject *data; | |
1987 | ||
1988 | data = qobject_from_jsonf("{ 'device': %s, 'tray-open': %i }", | |
1989 | bdrv_get_device_name(bs), ejected); | |
1990 | monitor_protocol_event(QEVENT_DEVICE_TRAY_MOVED, data); | |
1991 | ||
1992 | qobject_decref(data); | |
1993 | } | |
1994 | ||
7d4b4ba5 | 1995 | static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load) |
0e49de52 | 1996 | { |
145feb17 | 1997 | if (bs->dev_ops && bs->dev_ops->change_media_cb) { |
6f382ed2 | 1998 | bool tray_was_closed = !bdrv_dev_is_tray_open(bs); |
7d4b4ba5 | 1999 | bs->dev_ops->change_media_cb(bs->dev_opaque, load); |
6f382ed2 LC |
2000 | if (tray_was_closed) { |
2001 | /* tray open */ | |
2002 | bdrv_emit_qmp_eject_event(bs, true); | |
2003 | } | |
2004 | if (load) { | |
2005 | /* tray close */ | |
2006 | bdrv_emit_qmp_eject_event(bs, false); | |
2007 | } | |
145feb17 MA |
2008 | } |
2009 | } | |
2010 | ||
2c6942fa MA |
2011 | bool bdrv_dev_has_removable_media(BlockDriverState *bs) |
2012 | { | |
2013 | return !bs->dev || (bs->dev_ops && bs->dev_ops->change_media_cb); | |
2014 | } | |
2015 | ||
025ccaa7 PB |
2016 | void bdrv_dev_eject_request(BlockDriverState *bs, bool force) |
2017 | { | |
2018 | if (bs->dev_ops && bs->dev_ops->eject_request_cb) { | |
2019 | bs->dev_ops->eject_request_cb(bs->dev_opaque, force); | |
2020 | } | |
2021 | } | |
2022 | ||
e4def80b MA |
2023 | bool bdrv_dev_is_tray_open(BlockDriverState *bs) |
2024 | { | |
2025 | if (bs->dev_ops && bs->dev_ops->is_tray_open) { | |
2026 | return bs->dev_ops->is_tray_open(bs->dev_opaque); | |
2027 | } | |
2028 | return false; | |
2029 | } | |
2030 | ||
145feb17 MA |
2031 | static void bdrv_dev_resize_cb(BlockDriverState *bs) |
2032 | { | |
2033 | if (bs->dev_ops && bs->dev_ops->resize_cb) { | |
2034 | bs->dev_ops->resize_cb(bs->dev_opaque); | |
0e49de52 MA |
2035 | } |
2036 | } | |
2037 | ||
f107639a MA |
2038 | bool bdrv_dev_is_medium_locked(BlockDriverState *bs) |
2039 | { | |
2040 | if (bs->dev_ops && bs->dev_ops->is_medium_locked) { | |
2041 | return bs->dev_ops->is_medium_locked(bs->dev_opaque); | |
2042 | } | |
2043 | return false; | |
2044 | } | |
2045 | ||
e97fc193 AL |
2046 | /* |
2047 | * Run consistency checks on an image | |
2048 | * | |
e076f338 | 2049 | * Returns 0 if the check could be completed (it doesn't mean that the image is |
a1c7273b | 2050 | * free of errors) or -errno when an internal error occurred. The results of the |
e076f338 | 2051 | * check are stored in res. |
e97fc193 | 2052 | */ |
4534ff54 | 2053 | int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix) |
e97fc193 AL |
2054 | { |
2055 | if (bs->drv->bdrv_check == NULL) { | |
2056 | return -ENOTSUP; | |
2057 | } | |
2058 | ||
e076f338 | 2059 | memset(res, 0, sizeof(*res)); |
4534ff54 | 2060 | return bs->drv->bdrv_check(bs, res, fix); |
e97fc193 AL |
2061 | } |
2062 | ||
8a426614 KW |
2063 | #define COMMIT_BUF_SECTORS 2048 |
2064 | ||
33e3963e FB |
2065 | /* commit COW file into the raw image */ |
2066 | int bdrv_commit(BlockDriverState *bs) | |
2067 | { | |
19cb3738 | 2068 | BlockDriver *drv = bs->drv; |
72706ea4 | 2069 | int64_t sector, total_sectors, length, backing_length; |
8a426614 | 2070 | int n, ro, open_flags; |
0bce597d | 2071 | int ret = 0; |
72706ea4 | 2072 | uint8_t *buf = NULL; |
c2cba3d9 | 2073 | char filename[PATH_MAX]; |
33e3963e | 2074 | |
19cb3738 FB |
2075 | if (!drv) |
2076 | return -ENOMEDIUM; | |
4dca4b63 NS |
2077 | |
2078 | if (!bs->backing_hd) { | |
2079 | return -ENOTSUP; | |
33e3963e FB |
2080 | } |
2081 | ||
2d3735d3 SH |
2082 | if (bdrv_in_use(bs) || bdrv_in_use(bs->backing_hd)) { |
2083 | return -EBUSY; | |
2084 | } | |
2085 | ||
4dca4b63 | 2086 | ro = bs->backing_hd->read_only; |
c2cba3d9 JM |
2087 | /* Use pstrcpy (not strncpy): filename must be NUL-terminated. */ |
2088 | pstrcpy(filename, sizeof(filename), bs->backing_hd->filename); | |
4dca4b63 NS |
2089 | open_flags = bs->backing_hd->open_flags; |
2090 | ||
2091 | if (ro) { | |
0bce597d JC |
2092 | if (bdrv_reopen(bs->backing_hd, open_flags | BDRV_O_RDWR, NULL)) { |
2093 | return -EACCES; | |
4dca4b63 | 2094 | } |
ea2384d3 | 2095 | } |
33e3963e | 2096 | |
72706ea4 JC |
2097 | length = bdrv_getlength(bs); |
2098 | if (length < 0) { | |
2099 | ret = length; | |
2100 | goto ro_cleanup; | |
2101 | } | |
2102 | ||
2103 | backing_length = bdrv_getlength(bs->backing_hd); | |
2104 | if (backing_length < 0) { | |
2105 | ret = backing_length; | |
2106 | goto ro_cleanup; | |
2107 | } | |
2108 | ||
2109 | /* If our top snapshot is larger than the backing file image, | |
2110 | * grow the backing file image if possible. If not possible, | |
2111 | * we must return an error */ | |
2112 | if (length > backing_length) { | |
2113 | ret = bdrv_truncate(bs->backing_hd, length); | |
2114 | if (ret < 0) { | |
2115 | goto ro_cleanup; | |
2116 | } | |
2117 | } | |
2118 | ||
2119 | total_sectors = length >> BDRV_SECTOR_BITS; | |
7267c094 | 2120 | buf = g_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE); |
8a426614 KW |
2121 | |
2122 | for (sector = 0; sector < total_sectors; sector += n) { | |
d663640c PB |
2123 | ret = bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n); |
2124 | if (ret < 0) { | |
2125 | goto ro_cleanup; | |
2126 | } | |
2127 | if (ret) { | |
dabfa6cc KW |
2128 | ret = bdrv_read(bs, sector, buf, n); |
2129 | if (ret < 0) { | |
8a426614 KW |
2130 | goto ro_cleanup; |
2131 | } | |
2132 | ||
dabfa6cc KW |
2133 | ret = bdrv_write(bs->backing_hd, sector, buf, n); |
2134 | if (ret < 0) { | |
8a426614 KW |
2135 | goto ro_cleanup; |
2136 | } | |
ea2384d3 | 2137 | } |
33e3963e | 2138 | } |
95389c86 | 2139 | |
1d44952f CH |
2140 | if (drv->bdrv_make_empty) { |
2141 | ret = drv->bdrv_make_empty(bs); | |
dabfa6cc KW |
2142 | if (ret < 0) { |
2143 | goto ro_cleanup; | |
2144 | } | |
1d44952f CH |
2145 | bdrv_flush(bs); |
2146 | } | |
95389c86 | 2147 | |
3f5075ae CH |
2148 | /* |
2149 | * Make sure all data we wrote to the backing device is actually | |
2150 | * stable on disk. | |
2151 | */ | |
dabfa6cc | 2152 | if (bs->backing_hd) { |
3f5075ae | 2153 | bdrv_flush(bs->backing_hd); |
dabfa6cc | 2154 | } |
4dca4b63 | 2155 | |
dabfa6cc | 2156 | ret = 0; |
4dca4b63 | 2157 | ro_cleanup: |
7267c094 | 2158 | g_free(buf); |
4dca4b63 NS |
2159 | |
2160 | if (ro) { | |
0bce597d JC |
2161 | /* ignoring error return here */ |
2162 | bdrv_reopen(bs->backing_hd, open_flags & ~BDRV_O_RDWR, NULL); | |
4dca4b63 NS |
2163 | } |
2164 | ||
1d44952f | 2165 | return ret; |
33e3963e FB |
2166 | } |
2167 | ||
e8877497 | 2168 | int bdrv_commit_all(void) |
6ab4b5ab MA |
2169 | { |
2170 | BlockDriverState *bs; | |
2171 | ||
dc364f4c | 2172 | QTAILQ_FOREACH(bs, &bdrv_states, device_list) { |
272d2d8e JC |
2173 | if (bs->drv && bs->backing_hd) { |
2174 | int ret = bdrv_commit(bs); | |
2175 | if (ret < 0) { | |
2176 | return ret; | |
2177 | } | |
e8877497 | 2178 | } |
6ab4b5ab | 2179 | } |
e8877497 | 2180 | return 0; |
6ab4b5ab MA |
2181 | } |
2182 | ||
dbffbdcf SH |
2183 | /** |
2184 | * Remove an active request from the tracked requests list | |
2185 | * | |
2186 | * This function should be called when a tracked request is completing. | |
2187 | */ | |
2188 | static void tracked_request_end(BdrvTrackedRequest *req) | |
2189 | { | |
2190 | QLIST_REMOVE(req, list); | |
f4658285 | 2191 | qemu_co_queue_restart_all(&req->wait_queue); |
dbffbdcf SH |
2192 | } |
2193 | ||
2194 | /** | |
2195 | * Add an active request to the tracked requests list | |
2196 | */ | |
2197 | static void tracked_request_begin(BdrvTrackedRequest *req, | |
2198 | BlockDriverState *bs, | |
2199 | int64_t sector_num, | |
2200 | int nb_sectors, bool is_write) | |
2201 | { | |
2202 | *req = (BdrvTrackedRequest){ | |
2203 | .bs = bs, | |
2204 | .sector_num = sector_num, | |
2205 | .nb_sectors = nb_sectors, | |
2206 | .is_write = is_write, | |
5f8b6491 | 2207 | .co = qemu_coroutine_self(), |
dbffbdcf SH |
2208 | }; |
2209 | ||
f4658285 SH |
2210 | qemu_co_queue_init(&req->wait_queue); |
2211 | ||
dbffbdcf SH |
2212 | QLIST_INSERT_HEAD(&bs->tracked_requests, req, list); |
2213 | } | |
2214 | ||
d83947ac SH |
2215 | /** |
2216 | * Round a region to cluster boundaries | |
2217 | */ | |
343bded4 PB |
2218 | void bdrv_round_to_clusters(BlockDriverState *bs, |
2219 | int64_t sector_num, int nb_sectors, | |
2220 | int64_t *cluster_sector_num, | |
2221 | int *cluster_nb_sectors) | |
d83947ac SH |
2222 | { |
2223 | BlockDriverInfo bdi; | |
2224 | ||
2225 | if (bdrv_get_info(bs, &bdi) < 0 || bdi.cluster_size == 0) { | |
2226 | *cluster_sector_num = sector_num; | |
2227 | *cluster_nb_sectors = nb_sectors; | |
2228 | } else { | |
2229 | int64_t c = bdi.cluster_size / BDRV_SECTOR_SIZE; | |
2230 | *cluster_sector_num = QEMU_ALIGN_DOWN(sector_num, c); | |
2231 | *cluster_nb_sectors = QEMU_ALIGN_UP(sector_num - *cluster_sector_num + | |
2232 | nb_sectors, c); | |
2233 | } | |
2234 | } | |
2235 | ||
f4658285 SH |
2236 | static bool tracked_request_overlaps(BdrvTrackedRequest *req, |
2237 | int64_t sector_num, int nb_sectors) { | |
d83947ac SH |
2238 | /* aaaa bbbb */ |
2239 | if (sector_num >= req->sector_num + req->nb_sectors) { | |
2240 | return false; | |
2241 | } | |
2242 | /* bbbb aaaa */ | |
2243 | if (req->sector_num >= sector_num + nb_sectors) { | |
2244 | return false; | |
2245 | } | |
2246 | return true; | |
f4658285 SH |
2247 | } |
2248 | ||
2249 | static void coroutine_fn wait_for_overlapping_requests(BlockDriverState *bs, | |
2250 | int64_t sector_num, int nb_sectors) | |
2251 | { | |
2252 | BdrvTrackedRequest *req; | |
d83947ac SH |
2253 | int64_t cluster_sector_num; |
2254 | int cluster_nb_sectors; | |
f4658285 SH |
2255 | bool retry; |
2256 | ||
d83947ac SH |
2257 | /* If we touch the same cluster it counts as an overlap. This guarantees |
2258 | * that allocating writes will be serialized and not race with each other | |
2259 | * for the same cluster. For example, in copy-on-read it ensures that the | |
2260 | * CoR read and write operations are atomic and guest writes cannot | |
2261 | * interleave between them. | |
2262 | */ | |
343bded4 PB |
2263 | bdrv_round_to_clusters(bs, sector_num, nb_sectors, |
2264 | &cluster_sector_num, &cluster_nb_sectors); | |
d83947ac | 2265 | |
f4658285 SH |
2266 | do { |
2267 | retry = false; | |
2268 | QLIST_FOREACH(req, &bs->tracked_requests, list) { | |
d83947ac SH |
2269 | if (tracked_request_overlaps(req, cluster_sector_num, |
2270 | cluster_nb_sectors)) { | |
5f8b6491 SH |
2271 | /* Hitting this means there was a reentrant request, for |
2272 | * example, a block driver issuing nested requests. This must | |
2273 | * never happen since it means deadlock. | |
2274 | */ | |
2275 | assert(qemu_coroutine_self() != req->co); | |
2276 | ||
f4658285 SH |
2277 | qemu_co_queue_wait(&req->wait_queue); |
2278 | retry = true; | |
2279 | break; | |
2280 | } | |
2281 | } | |
2282 | } while (retry); | |
2283 | } | |
2284 | ||
756e6736 KW |
2285 | /* |
2286 | * Return values: | |
2287 | * 0 - success | |
2288 | * -EINVAL - backing format specified, but no file | |
2289 | * -ENOSPC - can't update the backing file because no space is left in the | |
2290 | * image file header | |
2291 | * -ENOTSUP - format driver doesn't support changing the backing file | |
2292 | */ | |
2293 | int bdrv_change_backing_file(BlockDriverState *bs, | |
2294 | const char *backing_file, const char *backing_fmt) | |
2295 | { | |
2296 | BlockDriver *drv = bs->drv; | |
469ef350 | 2297 | int ret; |
756e6736 | 2298 | |
5f377794 PB |
2299 | /* Backing file format doesn't make sense without a backing file */ |
2300 | if (backing_fmt && !backing_file) { | |
2301 | return -EINVAL; | |
2302 | } | |
2303 | ||
756e6736 | 2304 | if (drv->bdrv_change_backing_file != NULL) { |
469ef350 | 2305 | ret = drv->bdrv_change_backing_file(bs, backing_file, backing_fmt); |
756e6736 | 2306 | } else { |
469ef350 | 2307 | ret = -ENOTSUP; |
756e6736 | 2308 | } |
469ef350 PB |
2309 | |
2310 | if (ret == 0) { | |
2311 | pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: ""); | |
2312 | pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: ""); | |
2313 | } | |
2314 | return ret; | |
756e6736 KW |
2315 | } |
2316 | ||
6ebdcee2 JC |
2317 | /* |
2318 | * Finds the image layer in the chain that has 'bs' as its backing file. | |
2319 | * | |
2320 | * active is the current topmost image. | |
2321 | * | |
2322 | * Returns NULL if bs is not found in active's image chain, | |
2323 | * or if active == bs. | |
2324 | */ | |
2325 | BlockDriverState *bdrv_find_overlay(BlockDriverState *active, | |
2326 | BlockDriverState *bs) | |
2327 | { | |
2328 | BlockDriverState *overlay = NULL; | |
2329 | BlockDriverState *intermediate; | |
2330 | ||
2331 | assert(active != NULL); | |
2332 | assert(bs != NULL); | |
2333 | ||
2334 | /* if bs is the same as active, then by definition it has no overlay | |
2335 | */ | |
2336 | if (active == bs) { | |
2337 | return NULL; | |
2338 | } | |
2339 | ||
2340 | intermediate = active; | |
2341 | while (intermediate->backing_hd) { | |
2342 | if (intermediate->backing_hd == bs) { | |
2343 | overlay = intermediate; | |
2344 | break; | |
2345 | } | |
2346 | intermediate = intermediate->backing_hd; | |
2347 | } | |
2348 | ||
2349 | return overlay; | |
2350 | } | |
2351 | ||
2352 | typedef struct BlkIntermediateStates { | |
2353 | BlockDriverState *bs; | |
2354 | QSIMPLEQ_ENTRY(BlkIntermediateStates) entry; | |
2355 | } BlkIntermediateStates; | |
2356 | ||
2357 | ||
2358 | /* | |
2359 | * Drops images above 'base' up to and including 'top', and sets the image | |
2360 | * above 'top' to have base as its backing file. | |
2361 | * | |
2362 | * Requires that the overlay to 'top' is opened r/w, so that the backing file | |
2363 | * information in 'bs' can be properly updated. | |
2364 | * | |
2365 | * E.g., this will convert the following chain: | |
2366 | * bottom <- base <- intermediate <- top <- active | |
2367 | * | |
2368 | * to | |
2369 | * | |
2370 | * bottom <- base <- active | |
2371 | * | |
2372 | * It is allowed for bottom==base, in which case it converts: | |
2373 | * | |
2374 | * base <- intermediate <- top <- active | |
2375 | * | |
2376 | * to | |
2377 | * | |
2378 | * base <- active | |
2379 | * | |
2380 | * Error conditions: | |
2381 | * if active == top, that is considered an error | |
2382 | * | |
2383 | */ | |
2384 | int bdrv_drop_intermediate(BlockDriverState *active, BlockDriverState *top, | |
2385 | BlockDriverState *base) | |
2386 | { | |
2387 | BlockDriverState *intermediate; | |
2388 | BlockDriverState *base_bs = NULL; | |
2389 | BlockDriverState *new_top_bs = NULL; | |
2390 | BlkIntermediateStates *intermediate_state, *next; | |
2391 | int ret = -EIO; | |
2392 | ||
2393 | QSIMPLEQ_HEAD(states_to_delete, BlkIntermediateStates) states_to_delete; | |
2394 | QSIMPLEQ_INIT(&states_to_delete); | |
2395 | ||
2396 | if (!top->drv || !base->drv) { | |
2397 | goto exit; | |
2398 | } | |
2399 | ||
2400 | new_top_bs = bdrv_find_overlay(active, top); | |
2401 | ||
2402 | if (new_top_bs == NULL) { | |
2403 | /* we could not find the image above 'top', this is an error */ | |
2404 | goto exit; | |
2405 | } | |
2406 | ||
2407 | /* special case of new_top_bs->backing_hd already pointing to base - nothing | |
2408 | * to do, no intermediate images */ | |
2409 | if (new_top_bs->backing_hd == base) { | |
2410 | ret = 0; | |
2411 | goto exit; | |
2412 | } | |
2413 | ||
2414 | intermediate = top; | |
2415 | ||
2416 | /* now we will go down through the list, and add each BDS we find | |
2417 | * into our deletion queue, until we hit the 'base' | |
2418 | */ | |
2419 | while (intermediate) { | |
2420 | intermediate_state = g_malloc0(sizeof(BlkIntermediateStates)); | |
2421 | intermediate_state->bs = intermediate; | |
2422 | QSIMPLEQ_INSERT_TAIL(&states_to_delete, intermediate_state, entry); | |
2423 | ||
2424 | if (intermediate->backing_hd == base) { | |
2425 | base_bs = intermediate->backing_hd; | |
2426 | break; | |
2427 | } | |
2428 | intermediate = intermediate->backing_hd; | |
2429 | } | |
2430 | if (base_bs == NULL) { | |
2431 | /* something went wrong, we did not end at the base. safely | |
2432 | * unravel everything, and exit with error */ | |
2433 | goto exit; | |
2434 | } | |
2435 | ||
2436 | /* success - we can delete the intermediate states, and link top->base */ | |
2437 | ret = bdrv_change_backing_file(new_top_bs, base_bs->filename, | |
2438 | base_bs->drv ? base_bs->drv->format_name : ""); | |
2439 | if (ret) { | |
2440 | goto exit; | |
2441 | } | |
2442 | new_top_bs->backing_hd = base_bs; | |
2443 | ||
2444 | ||
2445 | QSIMPLEQ_FOREACH_SAFE(intermediate_state, &states_to_delete, entry, next) { | |
2446 | /* so that bdrv_close() does not recursively close the chain */ | |
2447 | intermediate_state->bs->backing_hd = NULL; | |
4f6fd349 | 2448 | bdrv_unref(intermediate_state->bs); |
6ebdcee2 JC |
2449 | } |
2450 | ret = 0; | |
2451 | ||
2452 | exit: | |
2453 | QSIMPLEQ_FOREACH_SAFE(intermediate_state, &states_to_delete, entry, next) { | |
2454 | g_free(intermediate_state); | |
2455 | } | |
2456 | return ret; | |
2457 | } | |
2458 | ||
2459 | ||
71d0770c AL |
2460 | static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset, |
2461 | size_t size) | |
2462 | { | |
2463 | int64_t len; | |
2464 | ||
2465 | if (!bdrv_is_inserted(bs)) | |
2466 | return -ENOMEDIUM; | |
2467 | ||
2468 | if (bs->growable) | |
2469 | return 0; | |
2470 | ||
2471 | len = bdrv_getlength(bs); | |
2472 | ||
fbb7b4e0 KW |
2473 | if (offset < 0) |
2474 | return -EIO; | |
2475 | ||
2476 | if ((offset > len) || (len - offset < size)) | |
71d0770c AL |
2477 | return -EIO; |
2478 | ||
2479 | return 0; | |
2480 | } | |
2481 | ||
2482 | static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num, | |
2483 | int nb_sectors) | |
2484 | { | |
eb5a3165 JS |
2485 | return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE, |
2486 | nb_sectors * BDRV_SECTOR_SIZE); | |
71d0770c AL |
2487 | } |
2488 | ||
1c9805a3 SH |
2489 | typedef struct RwCo { |
2490 | BlockDriverState *bs; | |
2491 | int64_t sector_num; | |
2492 | int nb_sectors; | |
2493 | QEMUIOVector *qiov; | |
2494 | bool is_write; | |
2495 | int ret; | |
4105eaaa | 2496 | BdrvRequestFlags flags; |
1c9805a3 SH |
2497 | } RwCo; |
2498 | ||
2499 | static void coroutine_fn bdrv_rw_co_entry(void *opaque) | |
fc01f7e7 | 2500 | { |
1c9805a3 | 2501 | RwCo *rwco = opaque; |
ea2384d3 | 2502 | |
1c9805a3 SH |
2503 | if (!rwco->is_write) { |
2504 | rwco->ret = bdrv_co_do_readv(rwco->bs, rwco->sector_num, | |
4105eaaa PL |
2505 | rwco->nb_sectors, rwco->qiov, |
2506 | rwco->flags); | |
1c9805a3 SH |
2507 | } else { |
2508 | rwco->ret = bdrv_co_do_writev(rwco->bs, rwco->sector_num, | |
4105eaaa PL |
2509 | rwco->nb_sectors, rwco->qiov, |
2510 | rwco->flags); | |
1c9805a3 SH |
2511 | } |
2512 | } | |
e7a8a783 | 2513 | |
1c9805a3 | 2514 | /* |
8d3b1a2d | 2515 | * Process a vectored synchronous request using coroutines |
1c9805a3 | 2516 | */ |
8d3b1a2d | 2517 | static int bdrv_rwv_co(BlockDriverState *bs, int64_t sector_num, |
4105eaaa PL |
2518 | QEMUIOVector *qiov, bool is_write, |
2519 | BdrvRequestFlags flags) | |
1c9805a3 | 2520 | { |
1c9805a3 SH |
2521 | Coroutine *co; |
2522 | RwCo rwco = { | |
2523 | .bs = bs, | |
2524 | .sector_num = sector_num, | |
8d3b1a2d KW |
2525 | .nb_sectors = qiov->size >> BDRV_SECTOR_BITS, |
2526 | .qiov = qiov, | |
1c9805a3 SH |
2527 | .is_write = is_write, |
2528 | .ret = NOT_DONE, | |
4105eaaa | 2529 | .flags = flags, |
1c9805a3 | 2530 | }; |
8d3b1a2d | 2531 | assert((qiov->size & (BDRV_SECTOR_SIZE - 1)) == 0); |
e7a8a783 | 2532 | |
498e386c ZYW |
2533 | /** |
2534 | * In sync call context, when the vcpu is blocked, this throttling timer | |
2535 | * will not fire; so the I/O throttling function has to be disabled here | |
2536 | * if it has been enabled. | |
2537 | */ | |
2538 | if (bs->io_limits_enabled) { | |
2539 | fprintf(stderr, "Disabling I/O throttling on '%s' due " | |
2540 | "to synchronous I/O.\n", bdrv_get_device_name(bs)); | |
2541 | bdrv_io_limits_disable(bs); | |
2542 | } | |
2543 | ||
1c9805a3 SH |
2544 | if (qemu_in_coroutine()) { |
2545 | /* Fast-path if already in coroutine context */ | |
2546 | bdrv_rw_co_entry(&rwco); | |
2547 | } else { | |
2548 | co = qemu_coroutine_create(bdrv_rw_co_entry); | |
2549 | qemu_coroutine_enter(co, &rwco); | |
2550 | while (rwco.ret == NOT_DONE) { | |
2551 | qemu_aio_wait(); | |
2552 | } | |
2553 | } | |
2554 | return rwco.ret; | |
2555 | } | |
b338082b | 2556 | |
8d3b1a2d KW |
2557 | /* |
2558 | * Process a synchronous request using coroutines | |
2559 | */ | |
2560 | static int bdrv_rw_co(BlockDriverState *bs, int64_t sector_num, uint8_t *buf, | |
4105eaaa | 2561 | int nb_sectors, bool is_write, BdrvRequestFlags flags) |
8d3b1a2d KW |
2562 | { |
2563 | QEMUIOVector qiov; | |
2564 | struct iovec iov = { | |
2565 | .iov_base = (void *)buf, | |
2566 | .iov_len = nb_sectors * BDRV_SECTOR_SIZE, | |
2567 | }; | |
2568 | ||
2569 | qemu_iovec_init_external(&qiov, &iov, 1); | |
4105eaaa | 2570 | return bdrv_rwv_co(bs, sector_num, &qiov, is_write, flags); |
8d3b1a2d KW |
2571 | } |
2572 | ||
1c9805a3 SH |
2573 | /* return < 0 if error. See bdrv_write() for the return codes */ |
2574 | int bdrv_read(BlockDriverState *bs, int64_t sector_num, | |
2575 | uint8_t *buf, int nb_sectors) | |
2576 | { | |
4105eaaa | 2577 | return bdrv_rw_co(bs, sector_num, buf, nb_sectors, false, 0); |
fc01f7e7 FB |
2578 | } |
2579 | ||
07d27a44 MA |
2580 | /* Just like bdrv_read(), but with I/O throttling temporarily disabled */ |
2581 | int bdrv_read_unthrottled(BlockDriverState *bs, int64_t sector_num, | |
2582 | uint8_t *buf, int nb_sectors) | |
2583 | { | |
2584 | bool enabled; | |
2585 | int ret; | |
2586 | ||
2587 | enabled = bs->io_limits_enabled; | |
2588 | bs->io_limits_enabled = false; | |
4e7395e8 | 2589 | ret = bdrv_read(bs, sector_num, buf, nb_sectors); |
07d27a44 MA |
2590 | bs->io_limits_enabled = enabled; |
2591 | return ret; | |
2592 | } | |
2593 | ||
5fafdf24 | 2594 | /* Return < 0 if error. Important errors are: |
19cb3738 FB |
2595 | -EIO generic I/O error (may happen for all errors) |
2596 | -ENOMEDIUM No media inserted. | |
2597 | -EINVAL Invalid sector number or nb_sectors | |
2598 | -EACCES Trying to write a read-only device | |
2599 | */ | |
5fafdf24 | 2600 | int bdrv_write(BlockDriverState *bs, int64_t sector_num, |
fc01f7e7 FB |
2601 | const uint8_t *buf, int nb_sectors) |
2602 | { | |
4105eaaa | 2603 | return bdrv_rw_co(bs, sector_num, (uint8_t *)buf, nb_sectors, true, 0); |
83f64091 FB |
2604 | } |
2605 | ||
8d3b1a2d KW |
2606 | int bdrv_writev(BlockDriverState *bs, int64_t sector_num, QEMUIOVector *qiov) |
2607 | { | |
4105eaaa PL |
2608 | return bdrv_rwv_co(bs, sector_num, qiov, true, 0); |
2609 | } | |
2610 | ||
aa7bfbff PL |
2611 | int bdrv_write_zeroes(BlockDriverState *bs, int64_t sector_num, |
2612 | int nb_sectors, BdrvRequestFlags flags) | |
4105eaaa PL |
2613 | { |
2614 | return bdrv_rw_co(bs, sector_num, NULL, nb_sectors, true, | |
aa7bfbff | 2615 | BDRV_REQ_ZERO_WRITE | flags); |
8d3b1a2d KW |
2616 | } |
2617 | ||
d75cbb5e PL |
2618 | /* |
2619 | * Completely zero out a block device with the help of bdrv_write_zeroes. | |
2620 | * The operation is sped up by checking the block status and only writing | |
2621 | * zeroes to the device if they currently do not return zeroes. Optional | |
2622 | * flags are passed through to bdrv_write_zeroes (e.g. BDRV_REQ_MAY_UNMAP). | |
2623 | * | |
2624 | * Returns < 0 on error, 0 on success. For error codes see bdrv_write(). | |
2625 | */ | |
2626 | int bdrv_make_zero(BlockDriverState *bs, BdrvRequestFlags flags) | |
2627 | { | |
2628 | int64_t target_size = bdrv_getlength(bs) / BDRV_SECTOR_SIZE; | |
2629 | int64_t ret, nb_sectors, sector_num = 0; | |
2630 | int n; | |
2631 | ||
2632 | for (;;) { | |
2633 | nb_sectors = target_size - sector_num; | |
2634 | if (nb_sectors <= 0) { | |
2635 | return 0; | |
2636 | } | |
2637 | if (nb_sectors > INT_MAX) { | |
2638 | nb_sectors = INT_MAX; | |
2639 | } | |
2640 | ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &n); | |
3d94ce60 PL |
2641 | if (ret < 0) { |
2642 | error_report("error getting block status at sector %" PRId64 ": %s", | |
2643 | sector_num, strerror(-ret)); | |
2644 | return ret; | |
2645 | } | |
d75cbb5e PL |
2646 | if (ret & BDRV_BLOCK_ZERO) { |
2647 | sector_num += n; | |
2648 | continue; | |
2649 | } | |
2650 | ret = bdrv_write_zeroes(bs, sector_num, n, flags); | |
2651 | if (ret < 0) { | |
2652 | error_report("error writing zeroes at sector %" PRId64 ": %s", | |
2653 | sector_num, strerror(-ret)); | |
2654 | return ret; | |
2655 | } | |
2656 | sector_num += n; | |
2657 | } | |
2658 | } | |
2659 | ||
eda578e5 AL |
2660 | int bdrv_pread(BlockDriverState *bs, int64_t offset, |
2661 | void *buf, int count1) | |
83f64091 | 2662 | { |
6ea44308 | 2663 | uint8_t tmp_buf[BDRV_SECTOR_SIZE]; |
83f64091 FB |
2664 | int len, nb_sectors, count; |
2665 | int64_t sector_num; | |
9a8c4cce | 2666 | int ret; |
83f64091 FB |
2667 | |
2668 | count = count1; | |
2669 | /* first read to align to sector start */ | |
6ea44308 | 2670 | len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); |
83f64091 FB |
2671 | if (len > count) |
2672 | len = count; | |
6ea44308 | 2673 | sector_num = offset >> BDRV_SECTOR_BITS; |
83f64091 | 2674 | if (len > 0) { |
9a8c4cce KW |
2675 | if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) |
2676 | return ret; | |
6ea44308 | 2677 | memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len); |
83f64091 FB |
2678 | count -= len; |
2679 | if (count == 0) | |
2680 | return count1; | |
2681 | sector_num++; | |
2682 | buf += len; | |
2683 | } | |
2684 | ||
2685 | /* read the sectors "in place" */ | |
6ea44308 | 2686 | nb_sectors = count >> BDRV_SECTOR_BITS; |
83f64091 | 2687 | if (nb_sectors > 0) { |
9a8c4cce KW |
2688 | if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0) |
2689 | return ret; | |
83f64091 | 2690 | sector_num += nb_sectors; |
6ea44308 | 2691 | len = nb_sectors << BDRV_SECTOR_BITS; |
83f64091 FB |
2692 | buf += len; |
2693 | count -= len; | |
2694 | } | |
2695 | ||
2696 | /* add data from the last sector */ | |
2697 | if (count > 0) { | |
9a8c4cce KW |
2698 | if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) |
2699 | return ret; | |
83f64091 FB |
2700 | memcpy(buf, tmp_buf, count); |
2701 | } | |
2702 | return count1; | |
2703 | } | |
2704 | ||
8d3b1a2d | 2705 | int bdrv_pwritev(BlockDriverState *bs, int64_t offset, QEMUIOVector *qiov) |
83f64091 | 2706 | { |
6ea44308 | 2707 | uint8_t tmp_buf[BDRV_SECTOR_SIZE]; |
83f64091 FB |
2708 | int len, nb_sectors, count; |
2709 | int64_t sector_num; | |
9a8c4cce | 2710 | int ret; |
83f64091 | 2711 | |
8d3b1a2d KW |
2712 | count = qiov->size; |
2713 | ||
83f64091 | 2714 | /* first write to align to sector start */ |
6ea44308 | 2715 | len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); |
83f64091 FB |
2716 | if (len > count) |
2717 | len = count; | |
6ea44308 | 2718 | sector_num = offset >> BDRV_SECTOR_BITS; |
83f64091 | 2719 | if (len > 0) { |
9a8c4cce KW |
2720 | if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) |
2721 | return ret; | |
8d3b1a2d KW |
2722 | qemu_iovec_to_buf(qiov, 0, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), |
2723 | len); | |
9a8c4cce KW |
2724 | if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) |
2725 | return ret; | |
83f64091 FB |
2726 | count -= len; |
2727 | if (count == 0) | |
8d3b1a2d | 2728 | return qiov->size; |
83f64091 | 2729 | sector_num++; |
83f64091 FB |
2730 | } |
2731 | ||
2732 | /* write the sectors "in place" */ | |
6ea44308 | 2733 | nb_sectors = count >> BDRV_SECTOR_BITS; |
83f64091 | 2734 | if (nb_sectors > 0) { |
8d3b1a2d KW |
2735 | QEMUIOVector qiov_inplace; |
2736 | ||
2737 | qemu_iovec_init(&qiov_inplace, qiov->niov); | |
2738 | qemu_iovec_concat(&qiov_inplace, qiov, len, | |
2739 | nb_sectors << BDRV_SECTOR_BITS); | |
2740 | ret = bdrv_writev(bs, sector_num, &qiov_inplace); | |
2741 | qemu_iovec_destroy(&qiov_inplace); | |
2742 | if (ret < 0) { | |
9a8c4cce | 2743 | return ret; |
8d3b1a2d KW |
2744 | } |
2745 | ||
83f64091 | 2746 | sector_num += nb_sectors; |
6ea44308 | 2747 | len = nb_sectors << BDRV_SECTOR_BITS; |
83f64091 FB |
2748 | count -= len; |
2749 | } | |
2750 | ||
2751 | /* add data from the last sector */ | |
2752 | if (count > 0) { | |
9a8c4cce KW |
2753 | if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) |
2754 | return ret; | |
8d3b1a2d | 2755 | qemu_iovec_to_buf(qiov, qiov->size - count, tmp_buf, count); |
9a8c4cce KW |
2756 | if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) |
2757 | return ret; | |
83f64091 | 2758 | } |
8d3b1a2d KW |
2759 | return qiov->size; |
2760 | } | |
2761 | ||
2762 | int bdrv_pwrite(BlockDriverState *bs, int64_t offset, | |
2763 | const void *buf, int count1) | |
2764 | { | |
2765 | QEMUIOVector qiov; | |
2766 | struct iovec iov = { | |
2767 | .iov_base = (void *) buf, | |
2768 | .iov_len = count1, | |
2769 | }; | |
2770 | ||
2771 | qemu_iovec_init_external(&qiov, &iov, 1); | |
2772 | return bdrv_pwritev(bs, offset, &qiov); | |
83f64091 | 2773 | } |
83f64091 | 2774 | |
f08145fe KW |
2775 | /* |
2776 | * Writes to the file and ensures that no writes are reordered across this | |
2777 | * request (acts as a barrier) | |
2778 | * | |
2779 | * Returns 0 on success, -errno in error cases. | |
2780 | */ | |
2781 | int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset, | |
2782 | const void *buf, int count) | |
2783 | { | |
2784 | int ret; | |
2785 | ||
2786 | ret = bdrv_pwrite(bs, offset, buf, count); | |
2787 | if (ret < 0) { | |
2788 | return ret; | |
2789 | } | |
2790 | ||
f05fa4ad PB |
2791 | /* No flush needed for cache modes that already do it */ |
2792 | if (bs->enable_write_cache) { | |
f08145fe KW |
2793 | bdrv_flush(bs); |
2794 | } | |
2795 | ||
2796 | return 0; | |
2797 | } | |
2798 | ||
470c0504 | 2799 | static int coroutine_fn bdrv_co_do_copy_on_readv(BlockDriverState *bs, |
ab185921 SH |
2800 | int64_t sector_num, int nb_sectors, QEMUIOVector *qiov) |
2801 | { | |
2802 | /* Perform I/O through a temporary buffer so that users who scribble over | |
2803 | * their read buffer while the operation is in progress do not end up | |
2804 | * modifying the image file. This is critical for zero-copy guest I/O | |
2805 | * where anything might happen inside guest memory. | |
2806 | */ | |
2807 | void *bounce_buffer; | |
2808 | ||
79c053bd | 2809 | BlockDriver *drv = bs->drv; |
ab185921 SH |
2810 | struct iovec iov; |
2811 | QEMUIOVector bounce_qiov; | |
2812 | int64_t cluster_sector_num; | |
2813 | int cluster_nb_sectors; | |
2814 | size_t skip_bytes; | |
2815 | int ret; | |
2816 | ||
2817 | /* Cover entire cluster so no additional backing file I/O is required when | |
2818 | * allocating cluster in the image file. | |
2819 | */ | |
343bded4 PB |
2820 | bdrv_round_to_clusters(bs, sector_num, nb_sectors, |
2821 | &cluster_sector_num, &cluster_nb_sectors); | |
ab185921 | 2822 | |
470c0504 SH |
2823 | trace_bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors, |
2824 | cluster_sector_num, cluster_nb_sectors); | |
ab185921 SH |
2825 | |
2826 | iov.iov_len = cluster_nb_sectors * BDRV_SECTOR_SIZE; | |
2827 | iov.iov_base = bounce_buffer = qemu_blockalign(bs, iov.iov_len); | |
2828 | qemu_iovec_init_external(&bounce_qiov, &iov, 1); | |
2829 | ||
79c053bd SH |
2830 | ret = drv->bdrv_co_readv(bs, cluster_sector_num, cluster_nb_sectors, |
2831 | &bounce_qiov); | |
ab185921 SH |
2832 | if (ret < 0) { |
2833 | goto err; | |
2834 | } | |
2835 | ||
79c053bd SH |
2836 | if (drv->bdrv_co_write_zeroes && |
2837 | buffer_is_zero(bounce_buffer, iov.iov_len)) { | |
621f0589 | 2838 | ret = bdrv_co_do_write_zeroes(bs, cluster_sector_num, |
aa7bfbff | 2839 | cluster_nb_sectors, 0); |
79c053bd | 2840 | } else { |
f05fa4ad PB |
2841 | /* This does not change the data on the disk, it is not necessary |
2842 | * to flush even in cache=writethrough mode. | |
2843 | */ | |
79c053bd | 2844 | ret = drv->bdrv_co_writev(bs, cluster_sector_num, cluster_nb_sectors, |
ab185921 | 2845 | &bounce_qiov); |
79c053bd SH |
2846 | } |
2847 | ||
ab185921 SH |
2848 | if (ret < 0) { |
2849 | /* It might be okay to ignore write errors for guest requests. If this | |
2850 | * is a deliberate copy-on-read then we don't want to ignore the error. | |
2851 | * Simply report it in all cases. | |
2852 | */ | |
2853 | goto err; | |
2854 | } | |
2855 | ||
2856 | skip_bytes = (sector_num - cluster_sector_num) * BDRV_SECTOR_SIZE; | |
03396148 MT |
2857 | qemu_iovec_from_buf(qiov, 0, bounce_buffer + skip_bytes, |
2858 | nb_sectors * BDRV_SECTOR_SIZE); | |
ab185921 SH |
2859 | |
2860 | err: | |
2861 | qemu_vfree(bounce_buffer); | |
2862 | return ret; | |
2863 | } | |
2864 | ||
c5fbe571 SH |
2865 | /* |
2866 | * Handle a read request in coroutine context | |
2867 | */ | |
2868 | static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs, | |
470c0504 SH |
2869 | int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, |
2870 | BdrvRequestFlags flags) | |
da1fa91d KW |
2871 | { |
2872 | BlockDriver *drv = bs->drv; | |
dbffbdcf SH |
2873 | BdrvTrackedRequest req; |
2874 | int ret; | |
da1fa91d | 2875 | |
da1fa91d KW |
2876 | if (!drv) { |
2877 | return -ENOMEDIUM; | |
2878 | } | |
2879 | if (bdrv_check_request(bs, sector_num, nb_sectors)) { | |
2880 | return -EIO; | |
2881 | } | |
2882 | ||
f4658285 | 2883 | if (bs->copy_on_read) { |
470c0504 SH |
2884 | flags |= BDRV_REQ_COPY_ON_READ; |
2885 | } | |
2886 | if (flags & BDRV_REQ_COPY_ON_READ) { | |
2887 | bs->copy_on_read_in_flight++; | |
2888 | } | |
2889 | ||
2890 | if (bs->copy_on_read_in_flight) { | |
f4658285 SH |
2891 | wait_for_overlapping_requests(bs, sector_num, nb_sectors); |
2892 | } | |
2893 | ||
cc0681c4 BC |
2894 | /* throttling disk I/O */ |
2895 | if (bs->io_limits_enabled) { | |
2896 | bdrv_io_limits_intercept(bs, nb_sectors, false); | |
2897 | } | |
2898 | ||
dbffbdcf | 2899 | tracked_request_begin(&req, bs, sector_num, nb_sectors, false); |
ab185921 | 2900 | |
470c0504 | 2901 | if (flags & BDRV_REQ_COPY_ON_READ) { |
ab185921 SH |
2902 | int pnum; |
2903 | ||
bdad13b9 | 2904 | ret = bdrv_is_allocated(bs, sector_num, nb_sectors, &pnum); |
ab185921 SH |
2905 | if (ret < 0) { |
2906 | goto out; | |
2907 | } | |
2908 | ||
2909 | if (!ret || pnum != nb_sectors) { | |
470c0504 | 2910 | ret = bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors, qiov); |
ab185921 SH |
2911 | goto out; |
2912 | } | |
2913 | } | |
2914 | ||
893a8f62 MK |
2915 | if (!(bs->zero_beyond_eof && bs->growable)) { |
2916 | ret = drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov); | |
2917 | } else { | |
2918 | /* Read zeros after EOF of growable BDSes */ | |
2919 | int64_t len, total_sectors, max_nb_sectors; | |
2920 | ||
2921 | len = bdrv_getlength(bs); | |
2922 | if (len < 0) { | |
2923 | ret = len; | |
2924 | goto out; | |
2925 | } | |
2926 | ||
d055a1fe | 2927 | total_sectors = DIV_ROUND_UP(len, BDRV_SECTOR_SIZE); |
893a8f62 MK |
2928 | max_nb_sectors = MAX(0, total_sectors - sector_num); |
2929 | if (max_nb_sectors > 0) { | |
2930 | ret = drv->bdrv_co_readv(bs, sector_num, | |
2931 | MIN(nb_sectors, max_nb_sectors), qiov); | |
2932 | } else { | |
2933 | ret = 0; | |
2934 | } | |
2935 | ||
2936 | /* Reading beyond end of file is supposed to produce zeroes */ | |
2937 | if (ret == 0 && total_sectors < sector_num + nb_sectors) { | |
2938 | uint64_t offset = MAX(0, total_sectors - sector_num); | |
2939 | uint64_t bytes = (sector_num + nb_sectors - offset) * | |
2940 | BDRV_SECTOR_SIZE; | |
2941 | qemu_iovec_memset(qiov, offset * BDRV_SECTOR_SIZE, 0, bytes); | |
2942 | } | |
2943 | } | |
ab185921 SH |
2944 | |
2945 | out: | |
dbffbdcf | 2946 | tracked_request_end(&req); |
470c0504 SH |
2947 | |
2948 | if (flags & BDRV_REQ_COPY_ON_READ) { | |
2949 | bs->copy_on_read_in_flight--; | |
2950 | } | |
2951 | ||
dbffbdcf | 2952 | return ret; |
da1fa91d KW |
2953 | } |
2954 | ||
c5fbe571 | 2955 | int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num, |
da1fa91d KW |
2956 | int nb_sectors, QEMUIOVector *qiov) |
2957 | { | |
c5fbe571 | 2958 | trace_bdrv_co_readv(bs, sector_num, nb_sectors); |
da1fa91d | 2959 | |
470c0504 SH |
2960 | return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov, 0); |
2961 | } | |
2962 | ||
2963 | int coroutine_fn bdrv_co_copy_on_readv(BlockDriverState *bs, | |
2964 | int64_t sector_num, int nb_sectors, QEMUIOVector *qiov) | |
2965 | { | |
2966 | trace_bdrv_co_copy_on_readv(bs, sector_num, nb_sectors); | |
2967 | ||
2968 | return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov, | |
2969 | BDRV_REQ_COPY_ON_READ); | |
c5fbe571 SH |
2970 | } |
2971 | ||
c31cb707 PL |
2972 | /* if no limit is specified in the BlockLimits use a default |
2973 | * of 32768 512-byte sectors (16 MiB) per request. | |
2974 | */ | |
2975 | #define MAX_WRITE_ZEROES_DEFAULT 32768 | |
2976 | ||
f08f2dda | 2977 | static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs, |
aa7bfbff | 2978 | int64_t sector_num, int nb_sectors, BdrvRequestFlags flags) |
f08f2dda SH |
2979 | { |
2980 | BlockDriver *drv = bs->drv; | |
2981 | QEMUIOVector qiov; | |
c31cb707 PL |
2982 | struct iovec iov = {0}; |
2983 | int ret = 0; | |
f08f2dda | 2984 | |
c31cb707 PL |
2985 | int max_write_zeroes = bs->bl.max_write_zeroes ? |
2986 | bs->bl.max_write_zeroes : MAX_WRITE_ZEROES_DEFAULT; | |
621f0589 | 2987 | |
c31cb707 PL |
2988 | while (nb_sectors > 0 && !ret) { |
2989 | int num = nb_sectors; | |
2990 | ||
b8d71c09 PB |
2991 | /* Align request. Block drivers can expect the "bulk" of the request |
2992 | * to be aligned. | |
2993 | */ | |
2994 | if (bs->bl.write_zeroes_alignment | |
2995 | && num > bs->bl.write_zeroes_alignment) { | |
2996 | if (sector_num % bs->bl.write_zeroes_alignment != 0) { | |
2997 | /* Make a small request up to the first aligned sector. */ | |
c31cb707 | 2998 | num = bs->bl.write_zeroes_alignment; |
b8d71c09 PB |
2999 | num -= sector_num % bs->bl.write_zeroes_alignment; |
3000 | } else if ((sector_num + num) % bs->bl.write_zeroes_alignment != 0) { | |
3001 | /* Shorten the request to the last aligned sector. num cannot | |
3002 | * underflow because num > bs->bl.write_zeroes_alignment. | |
3003 | */ | |
3004 | num -= (sector_num + num) % bs->bl.write_zeroes_alignment; | |
c31cb707 | 3005 | } |
621f0589 | 3006 | } |
f08f2dda | 3007 | |
c31cb707 PL |
3008 | /* limit request size */ |
3009 | if (num > max_write_zeroes) { | |
3010 | num = max_write_zeroes; | |
3011 | } | |
3012 | ||
3013 | ret = -ENOTSUP; | |
3014 | /* First try the efficient write zeroes operation */ | |
3015 | if (drv->bdrv_co_write_zeroes) { | |
3016 | ret = drv->bdrv_co_write_zeroes(bs, sector_num, num, flags); | |
3017 | } | |
3018 | ||
3019 | if (ret == -ENOTSUP) { | |
3020 | /* Fall back to bounce buffer if write zeroes is unsupported */ | |
3021 | iov.iov_len = num * BDRV_SECTOR_SIZE; | |
3022 | if (iov.iov_base == NULL) { | |
b8d71c09 PB |
3023 | iov.iov_base = qemu_blockalign(bs, num * BDRV_SECTOR_SIZE); |
3024 | memset(iov.iov_base, 0, num * BDRV_SECTOR_SIZE); | |
c31cb707 PL |
3025 | } |
3026 | qemu_iovec_init_external(&qiov, &iov, 1); | |
f08f2dda | 3027 | |
c31cb707 | 3028 | ret = drv->bdrv_co_writev(bs, sector_num, num, &qiov); |
b8d71c09 PB |
3029 | |
3030 | /* Keep bounce buffer around if it is big enough for all | |
3031 | * all future requests. | |
3032 | */ | |
3033 | if (num < max_write_zeroes) { | |
3034 | qemu_vfree(iov.iov_base); | |
3035 | iov.iov_base = NULL; | |
3036 | } | |
c31cb707 PL |
3037 | } |
3038 | ||
3039 | sector_num += num; | |
3040 | nb_sectors -= num; | |
3041 | } | |
f08f2dda SH |
3042 | |
3043 | qemu_vfree(iov.iov_base); | |
3044 | return ret; | |
3045 | } | |
3046 | ||
c5fbe571 SH |
3047 | /* |
3048 | * Handle a write request in coroutine context | |
3049 | */ | |
3050 | static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs, | |
f08f2dda SH |
3051 | int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, |
3052 | BdrvRequestFlags flags) | |
c5fbe571 SH |
3053 | { |
3054 | BlockDriver *drv = bs->drv; | |
dbffbdcf | 3055 | BdrvTrackedRequest req; |
6b7cb247 | 3056 | int ret; |
da1fa91d KW |
3057 | |
3058 | if (!bs->drv) { | |
3059 | return -ENOMEDIUM; | |
3060 | } | |
3061 | if (bs->read_only) { | |
3062 | return -EACCES; | |
3063 | } | |
3064 | if (bdrv_check_request(bs, sector_num, nb_sectors)) { | |
3065 | return -EIO; | |
3066 | } | |
3067 | ||
470c0504 | 3068 | if (bs->copy_on_read_in_flight) { |
f4658285 SH |
3069 | wait_for_overlapping_requests(bs, sector_num, nb_sectors); |
3070 | } | |
3071 | ||
cc0681c4 BC |
3072 | /* throttling disk I/O */ |
3073 | if (bs->io_limits_enabled) { | |
3074 | bdrv_io_limits_intercept(bs, nb_sectors, true); | |
3075 | } | |
3076 | ||
dbffbdcf SH |
3077 | tracked_request_begin(&req, bs, sector_num, nb_sectors, true); |
3078 | ||
d616b224 SH |
3079 | ret = notifier_with_return_list_notify(&bs->before_write_notifiers, &req); |
3080 | ||
3081 | if (ret < 0) { | |
3082 | /* Do nothing, write notifier decided to fail this request */ | |
3083 | } else if (flags & BDRV_REQ_ZERO_WRITE) { | |
aa7bfbff | 3084 | ret = bdrv_co_do_write_zeroes(bs, sector_num, nb_sectors, flags); |
f08f2dda SH |
3085 | } else { |
3086 | ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov); | |
3087 | } | |
6b7cb247 | 3088 | |
f05fa4ad PB |
3089 | if (ret == 0 && !bs->enable_write_cache) { |
3090 | ret = bdrv_co_flush(bs); | |
3091 | } | |
3092 | ||
e4654d2d | 3093 | bdrv_set_dirty(bs, sector_num, nb_sectors); |
da1fa91d KW |
3094 | |
3095 | if (bs->wr_highest_sector < sector_num + nb_sectors - 1) { | |
3096 | bs->wr_highest_sector = sector_num + nb_sectors - 1; | |
3097 | } | |
df2a6f29 PB |
3098 | if (bs->growable && ret >= 0) { |
3099 | bs->total_sectors = MAX(bs->total_sectors, sector_num + nb_sectors); | |
3100 | } | |
da1fa91d | 3101 | |
dbffbdcf SH |
3102 | tracked_request_end(&req); |
3103 | ||
6b7cb247 | 3104 | return ret; |
da1fa91d KW |
3105 | } |
3106 | ||
c5fbe571 SH |
3107 | int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num, |
3108 | int nb_sectors, QEMUIOVector *qiov) | |
3109 | { | |
3110 | trace_bdrv_co_writev(bs, sector_num, nb_sectors); | |
3111 | ||
f08f2dda SH |
3112 | return bdrv_co_do_writev(bs, sector_num, nb_sectors, qiov, 0); |
3113 | } | |
3114 | ||
3115 | int coroutine_fn bdrv_co_write_zeroes(BlockDriverState *bs, | |
aa7bfbff PL |
3116 | int64_t sector_num, int nb_sectors, |
3117 | BdrvRequestFlags flags) | |
f08f2dda | 3118 | { |
94d6ff21 | 3119 | trace_bdrv_co_write_zeroes(bs, sector_num, nb_sectors, flags); |
f08f2dda | 3120 | |
d32f35cb PL |
3121 | if (!(bs->open_flags & BDRV_O_UNMAP)) { |
3122 | flags &= ~BDRV_REQ_MAY_UNMAP; | |
3123 | } | |
3124 | ||
f08f2dda | 3125 | return bdrv_co_do_writev(bs, sector_num, nb_sectors, NULL, |
aa7bfbff | 3126 | BDRV_REQ_ZERO_WRITE | flags); |
c5fbe571 SH |
3127 | } |
3128 | ||
83f64091 FB |
3129 | /** |
3130 | * Truncate file to 'offset' bytes (needed only for file protocols) | |
3131 | */ | |
3132 | int bdrv_truncate(BlockDriverState *bs, int64_t offset) | |
3133 | { | |
3134 | BlockDriver *drv = bs->drv; | |
51762288 | 3135 | int ret; |
83f64091 | 3136 | if (!drv) |
19cb3738 | 3137 | return -ENOMEDIUM; |
83f64091 FB |
3138 | if (!drv->bdrv_truncate) |
3139 | return -ENOTSUP; | |
59f2689d NS |
3140 | if (bs->read_only) |
3141 | return -EACCES; | |
8591675f MT |
3142 | if (bdrv_in_use(bs)) |
3143 | return -EBUSY; | |
51762288 SH |
3144 | ret = drv->bdrv_truncate(bs, offset); |
3145 | if (ret == 0) { | |
3146 | ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS); | |
145feb17 | 3147 | bdrv_dev_resize_cb(bs); |
51762288 SH |
3148 | } |
3149 | return ret; | |
83f64091 FB |
3150 | } |
3151 | ||
4a1d5e1f FZ |
3152 | /** |
3153 | * Length of a allocated file in bytes. Sparse files are counted by actual | |
3154 | * allocated space. Return < 0 if error or unknown. | |
3155 | */ | |
3156 | int64_t bdrv_get_allocated_file_size(BlockDriverState *bs) | |
3157 | { | |
3158 | BlockDriver *drv = bs->drv; | |
3159 | if (!drv) { | |
3160 | return -ENOMEDIUM; | |
3161 | } | |
3162 | if (drv->bdrv_get_allocated_file_size) { | |
3163 | return drv->bdrv_get_allocated_file_size(bs); | |
3164 | } | |
3165 | if (bs->file) { | |
3166 | return bdrv_get_allocated_file_size(bs->file); | |
3167 | } | |
3168 | return -ENOTSUP; | |
3169 | } | |
3170 | ||
83f64091 FB |
3171 | /** |
3172 | * Length of a file in bytes. Return < 0 if error or unknown. | |
3173 | */ | |
3174 | int64_t bdrv_getlength(BlockDriverState *bs) | |
3175 | { | |
3176 | BlockDriver *drv = bs->drv; | |
3177 | if (!drv) | |
19cb3738 | 3178 | return -ENOMEDIUM; |
51762288 | 3179 | |
b94a2610 KW |
3180 | if (drv->has_variable_length) { |
3181 | int ret = refresh_total_sectors(bs, bs->total_sectors); | |
3182 | if (ret < 0) { | |
3183 | return ret; | |
46a4e4e6 | 3184 | } |
83f64091 | 3185 | } |
46a4e4e6 | 3186 | return bs->total_sectors * BDRV_SECTOR_SIZE; |
fc01f7e7 FB |
3187 | } |
3188 | ||
19cb3738 | 3189 | /* return 0 as number of sectors if no device present or error */ |
96b8f136 | 3190 | void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr) |
fc01f7e7 | 3191 | { |
19cb3738 FB |
3192 | int64_t length; |
3193 | length = bdrv_getlength(bs); | |
3194 | if (length < 0) | |
3195 | length = 0; | |
3196 | else | |
6ea44308 | 3197 | length = length >> BDRV_SECTOR_BITS; |
19cb3738 | 3198 | *nb_sectors_ptr = length; |
fc01f7e7 | 3199 | } |
cf98951b | 3200 | |
ff06f5f3 PB |
3201 | void bdrv_set_on_error(BlockDriverState *bs, BlockdevOnError on_read_error, |
3202 | BlockdevOnError on_write_error) | |
abd7f68d MA |
3203 | { |
3204 | bs->on_read_error = on_read_error; | |
3205 | bs->on_write_error = on_write_error; | |
3206 | } | |
3207 | ||
1ceee0d5 | 3208 | BlockdevOnError bdrv_get_on_error(BlockDriverState *bs, bool is_read) |
abd7f68d MA |
3209 | { |
3210 | return is_read ? bs->on_read_error : bs->on_write_error; | |
3211 | } | |
3212 | ||
3e1caa5f PB |
3213 | BlockErrorAction bdrv_get_error_action(BlockDriverState *bs, bool is_read, int error) |
3214 | { | |
3215 | BlockdevOnError on_err = is_read ? bs->on_read_error : bs->on_write_error; | |
3216 | ||
3217 | switch (on_err) { | |
3218 | case BLOCKDEV_ON_ERROR_ENOSPC: | |
3219 | return (error == ENOSPC) ? BDRV_ACTION_STOP : BDRV_ACTION_REPORT; | |
3220 | case BLOCKDEV_ON_ERROR_STOP: | |
3221 | return BDRV_ACTION_STOP; | |
3222 | case BLOCKDEV_ON_ERROR_REPORT: | |
3223 | return BDRV_ACTION_REPORT; | |
3224 | case BLOCKDEV_ON_ERROR_IGNORE: | |
3225 | return BDRV_ACTION_IGNORE; | |
3226 | default: | |
3227 | abort(); | |
3228 | } | |
3229 | } | |
3230 | ||
3231 | /* This is done by device models because, while the block layer knows | |
3232 | * about the error, it does not know whether an operation comes from | |
3233 | * the device or the block layer (from a job, for example). | |
3234 | */ | |
3235 | void bdrv_error_action(BlockDriverState *bs, BlockErrorAction action, | |
3236 | bool is_read, int error) | |
3237 | { | |
3238 | assert(error >= 0); | |
32c81a4a | 3239 | bdrv_emit_qmp_error_event(bs, QEVENT_BLOCK_IO_ERROR, action, is_read); |
3e1caa5f PB |
3240 | if (action == BDRV_ACTION_STOP) { |
3241 | vm_stop(RUN_STATE_IO_ERROR); | |
3242 | bdrv_iostatus_set_err(bs, error); | |
3243 | } | |
3244 | } | |
3245 | ||
b338082b FB |
3246 | int bdrv_is_read_only(BlockDriverState *bs) |
3247 | { | |
3248 | return bs->read_only; | |
3249 | } | |
3250 | ||
985a03b0 TS |
3251 | int bdrv_is_sg(BlockDriverState *bs) |
3252 | { | |
3253 | return bs->sg; | |
3254 | } | |
3255 | ||
e900a7b7 CH |
3256 | int bdrv_enable_write_cache(BlockDriverState *bs) |
3257 | { | |
3258 | return bs->enable_write_cache; | |
3259 | } | |
3260 | ||
425b0148 PB |
3261 | void bdrv_set_enable_write_cache(BlockDriverState *bs, bool wce) |
3262 | { | |
3263 | bs->enable_write_cache = wce; | |
55b110f2 JC |
3264 | |
3265 | /* so a reopen() will preserve wce */ | |
3266 | if (wce) { | |
3267 | bs->open_flags |= BDRV_O_CACHE_WB; | |
3268 | } else { | |
3269 | bs->open_flags &= ~BDRV_O_CACHE_WB; | |
3270 | } | |
425b0148 PB |
3271 | } |
3272 | ||
ea2384d3 FB |
3273 | int bdrv_is_encrypted(BlockDriverState *bs) |
3274 | { | |
3275 | if (bs->backing_hd && bs->backing_hd->encrypted) | |
3276 | return 1; | |
3277 | return bs->encrypted; | |
3278 | } | |
3279 | ||
c0f4ce77 AL |
3280 | int bdrv_key_required(BlockDriverState *bs) |
3281 | { | |
3282 | BlockDriverState *backing_hd = bs->backing_hd; | |
3283 | ||
3284 | if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key) | |
3285 | return 1; | |
3286 | return (bs->encrypted && !bs->valid_key); | |
3287 | } | |
3288 | ||
ea2384d3 FB |
3289 | int bdrv_set_key(BlockDriverState *bs, const char *key) |
3290 | { | |
3291 | int ret; | |
3292 | if (bs->backing_hd && bs->backing_hd->encrypted) { | |
3293 | ret = bdrv_set_key(bs->backing_hd, key); | |
3294 | if (ret < 0) | |
3295 | return ret; | |
3296 | if (!bs->encrypted) | |
3297 | return 0; | |
3298 | } | |
fd04a2ae SH |
3299 | if (!bs->encrypted) { |
3300 | return -EINVAL; | |
3301 | } else if (!bs->drv || !bs->drv->bdrv_set_key) { | |
3302 | return -ENOMEDIUM; | |
3303 | } | |
c0f4ce77 | 3304 | ret = bs->drv->bdrv_set_key(bs, key); |
bb5fc20f AL |
3305 | if (ret < 0) { |
3306 | bs->valid_key = 0; | |
3307 | } else if (!bs->valid_key) { | |
3308 | bs->valid_key = 1; | |
3309 | /* call the change callback now, we skipped it on open */ | |
7d4b4ba5 | 3310 | bdrv_dev_change_media_cb(bs, true); |
bb5fc20f | 3311 | } |
c0f4ce77 | 3312 | return ret; |
ea2384d3 FB |
3313 | } |
3314 | ||
f8d6bba1 | 3315 | const char *bdrv_get_format_name(BlockDriverState *bs) |
ea2384d3 | 3316 | { |
f8d6bba1 | 3317 | return bs->drv ? bs->drv->format_name : NULL; |
ea2384d3 FB |
3318 | } |
3319 | ||
5fafdf24 | 3320 | void bdrv_iterate_format(void (*it)(void *opaque, const char *name), |
ea2384d3 FB |
3321 | void *opaque) |
3322 | { | |
3323 | BlockDriver *drv; | |
3324 | ||
8a22f02a | 3325 | QLIST_FOREACH(drv, &bdrv_drivers, list) { |
ea2384d3 FB |
3326 | it(opaque, drv->format_name); |
3327 | } | |
3328 | } | |
3329 | ||
dc364f4c | 3330 | /* This function is to find block backend bs */ |
b338082b FB |
3331 | BlockDriverState *bdrv_find(const char *name) |
3332 | { | |
3333 | BlockDriverState *bs; | |
3334 | ||
dc364f4c | 3335 | QTAILQ_FOREACH(bs, &bdrv_states, device_list) { |
1b7bdbc1 | 3336 | if (!strcmp(name, bs->device_name)) { |
b338082b | 3337 | return bs; |
1b7bdbc1 | 3338 | } |
b338082b FB |
3339 | } |
3340 | return NULL; | |
3341 | } | |
3342 | ||
dc364f4c BC |
3343 | /* This function is to find a node in the bs graph */ |
3344 | BlockDriverState *bdrv_find_node(const char *node_name) | |
3345 | { | |
3346 | BlockDriverState *bs; | |
3347 | ||
3348 | assert(node_name); | |
3349 | ||
3350 | QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) { | |
3351 | if (!strcmp(node_name, bs->node_name)) { | |
3352 | return bs; | |
3353 | } | |
3354 | } | |
3355 | return NULL; | |
3356 | } | |
3357 | ||
c13163fb BC |
3358 | /* Put this QMP function here so it can access the static graph_bdrv_states. */ |
3359 | BlockDeviceInfoList *bdrv_named_nodes_list(void) | |
3360 | { | |
3361 | BlockDeviceInfoList *list, *entry; | |
3362 | BlockDriverState *bs; | |
3363 | ||
3364 | list = NULL; | |
3365 | QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) { | |
3366 | entry = g_malloc0(sizeof(*entry)); | |
3367 | entry->value = bdrv_block_device_info(bs); | |
3368 | entry->next = list; | |
3369 | list = entry; | |
3370 | } | |
3371 | ||
3372 | return list; | |
3373 | } | |
3374 | ||
12d3ba82 BC |
3375 | BlockDriverState *bdrv_lookup_bs(const char *device, |
3376 | const char *node_name, | |
3377 | Error **errp) | |
3378 | { | |
3379 | BlockDriverState *bs = NULL; | |
3380 | ||
3381 | if ((!device && !node_name) || (device && node_name)) { | |
3382 | error_setg(errp, "Use either device or node-name but not both"); | |
3383 | return NULL; | |
3384 | } | |
3385 | ||
3386 | if (device) { | |
3387 | bs = bdrv_find(device); | |
3388 | ||
3389 | if (!bs) { | |
3390 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); | |
3391 | return NULL; | |
3392 | } | |
3393 | ||
3394 | return bs; | |
3395 | } | |
3396 | ||
3397 | bs = bdrv_find_node(node_name); | |
3398 | ||
3399 | if (!bs) { | |
3400 | error_set(errp, QERR_DEVICE_NOT_FOUND, node_name); | |
3401 | return NULL; | |
3402 | } | |
3403 | ||
3404 | return bs; | |
3405 | } | |
3406 | ||
2f399b0a MA |
3407 | BlockDriverState *bdrv_next(BlockDriverState *bs) |
3408 | { | |
3409 | if (!bs) { | |
3410 | return QTAILQ_FIRST(&bdrv_states); | |
3411 | } | |
dc364f4c | 3412 | return QTAILQ_NEXT(bs, device_list); |
2f399b0a MA |
3413 | } |
3414 | ||
51de9760 | 3415 | void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque) |
81d0912d FB |
3416 | { |
3417 | BlockDriverState *bs; | |
3418 | ||
dc364f4c | 3419 | QTAILQ_FOREACH(bs, &bdrv_states, device_list) { |
51de9760 | 3420 | it(opaque, bs); |
81d0912d FB |
3421 | } |
3422 | } | |
3423 | ||
ea2384d3 FB |
3424 | const char *bdrv_get_device_name(BlockDriverState *bs) |
3425 | { | |
3426 | return bs->device_name; | |
3427 | } | |
3428 | ||
c8433287 MA |
3429 | int bdrv_get_flags(BlockDriverState *bs) |
3430 | { | |
3431 | return bs->open_flags; | |
3432 | } | |
3433 | ||
f0f0fdfe | 3434 | int bdrv_flush_all(void) |
c6ca28d6 AL |
3435 | { |
3436 | BlockDriverState *bs; | |
f0f0fdfe | 3437 | int result = 0; |
c6ca28d6 | 3438 | |
dc364f4c | 3439 | QTAILQ_FOREACH(bs, &bdrv_states, device_list) { |
f0f0fdfe KW |
3440 | int ret = bdrv_flush(bs); |
3441 | if (ret < 0 && !result) { | |
3442 | result = ret; | |
3443 | } | |
1b7bdbc1 | 3444 | } |
f0f0fdfe KW |
3445 | |
3446 | return result; | |
c6ca28d6 AL |
3447 | } |
3448 | ||
3ac21627 PL |
3449 | int bdrv_has_zero_init_1(BlockDriverState *bs) |
3450 | { | |
3451 | return 1; | |
3452 | } | |
3453 | ||
f2feebbd KW |
3454 | int bdrv_has_zero_init(BlockDriverState *bs) |
3455 | { | |
3456 | assert(bs->drv); | |
3457 | ||
11212d8f PB |
3458 | /* If BS is a copy on write image, it is initialized to |
3459 | the contents of the base image, which may not be zeroes. */ | |
3460 | if (bs->backing_hd) { | |
3461 | return 0; | |
3462 | } | |
336c1c12 KW |
3463 | if (bs->drv->bdrv_has_zero_init) { |
3464 | return bs->drv->bdrv_has_zero_init(bs); | |
f2feebbd KW |
3465 | } |
3466 | ||
3ac21627 PL |
3467 | /* safe default */ |
3468 | return 0; | |
f2feebbd KW |
3469 | } |
3470 | ||
4ce78691 PL |
3471 | bool bdrv_unallocated_blocks_are_zero(BlockDriverState *bs) |
3472 | { | |
3473 | BlockDriverInfo bdi; | |
3474 | ||
3475 | if (bs->backing_hd) { | |
3476 | return false; | |
3477 | } | |
3478 | ||
3479 | if (bdrv_get_info(bs, &bdi) == 0) { | |
3480 | return bdi.unallocated_blocks_are_zero; | |
3481 | } | |
3482 | ||
3483 | return false; | |
3484 | } | |
3485 | ||
3486 | bool bdrv_can_write_zeroes_with_unmap(BlockDriverState *bs) | |
3487 | { | |
3488 | BlockDriverInfo bdi; | |
3489 | ||
3490 | if (bs->backing_hd || !(bs->open_flags & BDRV_O_UNMAP)) { | |
3491 | return false; | |
3492 | } | |
3493 | ||
3494 | if (bdrv_get_info(bs, &bdi) == 0) { | |
3495 | return bdi.can_write_zeroes_with_unmap; | |
3496 | } | |
3497 | ||
3498 | return false; | |
3499 | } | |
3500 | ||
b6b8a333 | 3501 | typedef struct BdrvCoGetBlockStatusData { |
376ae3f1 | 3502 | BlockDriverState *bs; |
b35b2bba | 3503 | BlockDriverState *base; |
376ae3f1 SH |
3504 | int64_t sector_num; |
3505 | int nb_sectors; | |
3506 | int *pnum; | |
b6b8a333 | 3507 | int64_t ret; |
376ae3f1 | 3508 | bool done; |
b6b8a333 | 3509 | } BdrvCoGetBlockStatusData; |
376ae3f1 | 3510 | |
f58c7b35 TS |
3511 | /* |
3512 | * Returns true iff the specified sector is present in the disk image. Drivers | |
3513 | * not implementing the functionality are assumed to not support backing files, | |
3514 | * hence all their sectors are reported as allocated. | |
3515 | * | |
bd9533e3 SH |
3516 | * If 'sector_num' is beyond the end of the disk image the return value is 0 |
3517 | * and 'pnum' is set to 0. | |
3518 | * | |
f58c7b35 TS |
3519 | * 'pnum' is set to the number of sectors (including and immediately following |
3520 | * the specified sector) that are known to be in the same | |
3521 | * allocated/unallocated state. | |
3522 | * | |
bd9533e3 SH |
3523 | * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes |
3524 | * beyond the end of the disk image it will be clamped. | |
f58c7b35 | 3525 | */ |
b6b8a333 PB |
3526 | static int64_t coroutine_fn bdrv_co_get_block_status(BlockDriverState *bs, |
3527 | int64_t sector_num, | |
3528 | int nb_sectors, int *pnum) | |
f58c7b35 | 3529 | { |
617ccb46 | 3530 | int64_t length; |
bd9533e3 | 3531 | int64_t n; |
5daa74a6 | 3532 | int64_t ret, ret2; |
bd9533e3 | 3533 | |
617ccb46 PB |
3534 | length = bdrv_getlength(bs); |
3535 | if (length < 0) { | |
3536 | return length; | |
3537 | } | |
3538 | ||
3539 | if (sector_num >= (length >> BDRV_SECTOR_BITS)) { | |
bd9533e3 SH |
3540 | *pnum = 0; |
3541 | return 0; | |
3542 | } | |
3543 | ||
3544 | n = bs->total_sectors - sector_num; | |
3545 | if (n < nb_sectors) { | |
3546 | nb_sectors = n; | |
3547 | } | |
3548 | ||
b6b8a333 | 3549 | if (!bs->drv->bdrv_co_get_block_status) { |
bd9533e3 | 3550 | *pnum = nb_sectors; |
918e92d7 PB |
3551 | ret = BDRV_BLOCK_DATA; |
3552 | if (bs->drv->protocol_name) { | |
3553 | ret |= BDRV_BLOCK_OFFSET_VALID | (sector_num * BDRV_SECTOR_SIZE); | |
3554 | } | |
3555 | return ret; | |
f58c7b35 | 3556 | } |
6aebab14 | 3557 | |
415b5b01 PB |
3558 | ret = bs->drv->bdrv_co_get_block_status(bs, sector_num, nb_sectors, pnum); |
3559 | if (ret < 0) { | |
3e0a233d | 3560 | *pnum = 0; |
415b5b01 PB |
3561 | return ret; |
3562 | } | |
3563 | ||
92bc50a5 PL |
3564 | if (ret & BDRV_BLOCK_RAW) { |
3565 | assert(ret & BDRV_BLOCK_OFFSET_VALID); | |
3566 | return bdrv_get_block_status(bs->file, ret >> BDRV_SECTOR_BITS, | |
3567 | *pnum, pnum); | |
3568 | } | |
3569 | ||
c3d86884 PL |
3570 | if (!(ret & BDRV_BLOCK_DATA) && !(ret & BDRV_BLOCK_ZERO)) { |
3571 | if (bdrv_unallocated_blocks_are_zero(bs)) { | |
f0ad5712 | 3572 | ret |= BDRV_BLOCK_ZERO; |
1f9db224 | 3573 | } else if (bs->backing_hd) { |
f0ad5712 PB |
3574 | BlockDriverState *bs2 = bs->backing_hd; |
3575 | int64_t length2 = bdrv_getlength(bs2); | |
3576 | if (length2 >= 0 && sector_num >= (length2 >> BDRV_SECTOR_BITS)) { | |
3577 | ret |= BDRV_BLOCK_ZERO; | |
3578 | } | |
3579 | } | |
415b5b01 | 3580 | } |
5daa74a6 PB |
3581 | |
3582 | if (bs->file && | |
3583 | (ret & BDRV_BLOCK_DATA) && !(ret & BDRV_BLOCK_ZERO) && | |
3584 | (ret & BDRV_BLOCK_OFFSET_VALID)) { | |
3585 | ret2 = bdrv_co_get_block_status(bs->file, ret >> BDRV_SECTOR_BITS, | |
3586 | *pnum, pnum); | |
3587 | if (ret2 >= 0) { | |
3588 | /* Ignore errors. This is just providing extra information, it | |
3589 | * is useful but not necessary. | |
3590 | */ | |
3591 | ret |= (ret2 & BDRV_BLOCK_ZERO); | |
3592 | } | |
3593 | } | |
3594 | ||
415b5b01 | 3595 | return ret; |
060f51c9 SH |
3596 | } |
3597 | ||
b6b8a333 PB |
3598 | /* Coroutine wrapper for bdrv_get_block_status() */ |
3599 | static void coroutine_fn bdrv_get_block_status_co_entry(void *opaque) | |
060f51c9 | 3600 | { |
b6b8a333 | 3601 | BdrvCoGetBlockStatusData *data = opaque; |
060f51c9 SH |
3602 | BlockDriverState *bs = data->bs; |
3603 | ||
b6b8a333 PB |
3604 | data->ret = bdrv_co_get_block_status(bs, data->sector_num, data->nb_sectors, |
3605 | data->pnum); | |
060f51c9 SH |
3606 | data->done = true; |
3607 | } | |
3608 | ||
3609 | /* | |
b6b8a333 | 3610 | * Synchronous wrapper around bdrv_co_get_block_status(). |
060f51c9 | 3611 | * |
b6b8a333 | 3612 | * See bdrv_co_get_block_status() for details. |
060f51c9 | 3613 | */ |
b6b8a333 PB |
3614 | int64_t bdrv_get_block_status(BlockDriverState *bs, int64_t sector_num, |
3615 | int nb_sectors, int *pnum) | |
060f51c9 | 3616 | { |
6aebab14 | 3617 | Coroutine *co; |
b6b8a333 | 3618 | BdrvCoGetBlockStatusData data = { |
6aebab14 SH |
3619 | .bs = bs, |
3620 | .sector_num = sector_num, | |
3621 | .nb_sectors = nb_sectors, | |
3622 | .pnum = pnum, | |
3623 | .done = false, | |
3624 | }; | |
3625 | ||
bdad13b9 PB |
3626 | if (qemu_in_coroutine()) { |
3627 | /* Fast-path if already in coroutine context */ | |
b6b8a333 | 3628 | bdrv_get_block_status_co_entry(&data); |
bdad13b9 | 3629 | } else { |
b6b8a333 | 3630 | co = qemu_coroutine_create(bdrv_get_block_status_co_entry); |
bdad13b9 PB |
3631 | qemu_coroutine_enter(co, &data); |
3632 | while (!data.done) { | |
3633 | qemu_aio_wait(); | |
3634 | } | |
6aebab14 SH |
3635 | } |
3636 | return data.ret; | |
f58c7b35 TS |
3637 | } |
3638 | ||
b6b8a333 PB |
3639 | int coroutine_fn bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, |
3640 | int nb_sectors, int *pnum) | |
3641 | { | |
4333bb71 PB |
3642 | int64_t ret = bdrv_get_block_status(bs, sector_num, nb_sectors, pnum); |
3643 | if (ret < 0) { | |
3644 | return ret; | |
3645 | } | |
3646 | return | |
3647 | (ret & BDRV_BLOCK_DATA) || | |
3648 | ((ret & BDRV_BLOCK_ZERO) && !bdrv_has_zero_init(bs)); | |
b6b8a333 PB |
3649 | } |
3650 | ||
188a7bbf PB |
3651 | /* |
3652 | * Given an image chain: ... -> [BASE] -> [INTER1] -> [INTER2] -> [TOP] | |
3653 | * | |
3654 | * Return true if the given sector is allocated in any image between | |
3655 | * BASE and TOP (inclusive). BASE can be NULL to check if the given | |
3656 | * sector is allocated in any image of the chain. Return false otherwise. | |
3657 | * | |
3658 | * 'pnum' is set to the number of sectors (including and immediately following | |
3659 | * the specified sector) that are known to be in the same | |
3660 | * allocated/unallocated state. | |
3661 | * | |
3662 | */ | |
4f578637 PB |
3663 | int bdrv_is_allocated_above(BlockDriverState *top, |
3664 | BlockDriverState *base, | |
3665 | int64_t sector_num, | |
3666 | int nb_sectors, int *pnum) | |
188a7bbf PB |
3667 | { |
3668 | BlockDriverState *intermediate; | |
3669 | int ret, n = nb_sectors; | |
3670 | ||
3671 | intermediate = top; | |
3672 | while (intermediate && intermediate != base) { | |
3673 | int pnum_inter; | |
bdad13b9 PB |
3674 | ret = bdrv_is_allocated(intermediate, sector_num, nb_sectors, |
3675 | &pnum_inter); | |
188a7bbf PB |
3676 | if (ret < 0) { |
3677 | return ret; | |
3678 | } else if (ret) { | |
3679 | *pnum = pnum_inter; | |
3680 | return 1; | |
3681 | } | |
3682 | ||
3683 | /* | |
3684 | * [sector_num, nb_sectors] is unallocated on top but intermediate | |
3685 | * might have | |
3686 | * | |
3687 | * [sector_num+x, nr_sectors] allocated. | |
3688 | */ | |
63ba17d3 VI |
3689 | if (n > pnum_inter && |
3690 | (intermediate == top || | |
3691 | sector_num + pnum_inter < intermediate->total_sectors)) { | |
188a7bbf PB |
3692 | n = pnum_inter; |
3693 | } | |
3694 | ||
3695 | intermediate = intermediate->backing_hd; | |
3696 | } | |
3697 | ||
3698 | *pnum = n; | |
3699 | return 0; | |
3700 | } | |
3701 | ||
045df330 AL |
3702 | const char *bdrv_get_encrypted_filename(BlockDriverState *bs) |
3703 | { | |
3704 | if (bs->backing_hd && bs->backing_hd->encrypted) | |
3705 | return bs->backing_file; | |
3706 | else if (bs->encrypted) | |
3707 | return bs->filename; | |
3708 | else | |
3709 | return NULL; | |
3710 | } | |
3711 | ||
5fafdf24 | 3712 | void bdrv_get_backing_filename(BlockDriverState *bs, |
83f64091 FB |
3713 | char *filename, int filename_size) |
3714 | { | |
3574c608 | 3715 | pstrcpy(filename, filename_size, bs->backing_file); |
83f64091 FB |
3716 | } |
3717 | ||
5fafdf24 | 3718 | int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num, |
faea38e7 FB |
3719 | const uint8_t *buf, int nb_sectors) |
3720 | { | |
3721 | BlockDriver *drv = bs->drv; | |
3722 | if (!drv) | |
19cb3738 | 3723 | return -ENOMEDIUM; |
faea38e7 FB |
3724 | if (!drv->bdrv_write_compressed) |
3725 | return -ENOTSUP; | |
fbb7b4e0 KW |
3726 | if (bdrv_check_request(bs, sector_num, nb_sectors)) |
3727 | return -EIO; | |
a55eb92c | 3728 | |
e4654d2d | 3729 | assert(QLIST_EMPTY(&bs->dirty_bitmaps)); |
a55eb92c | 3730 | |
faea38e7 FB |
3731 | return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors); |
3732 | } | |
3b46e624 | 3733 | |
faea38e7 FB |
3734 | int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) |
3735 | { | |
3736 | BlockDriver *drv = bs->drv; | |
3737 | if (!drv) | |
19cb3738 | 3738 | return -ENOMEDIUM; |
faea38e7 FB |
3739 | if (!drv->bdrv_get_info) |
3740 | return -ENOTSUP; | |
3741 | memset(bdi, 0, sizeof(*bdi)); | |
3742 | return drv->bdrv_get_info(bs, bdi); | |
3743 | } | |
3744 | ||
eae041fe HR |
3745 | ImageInfoSpecific *bdrv_get_specific_info(BlockDriverState *bs) |
3746 | { | |
3747 | BlockDriver *drv = bs->drv; | |
3748 | if (drv && drv->bdrv_get_specific_info) { | |
3749 | return drv->bdrv_get_specific_info(bs); | |
3750 | } | |
3751 | return NULL; | |
3752 | } | |
3753 | ||
45566e9c CH |
3754 | int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf, |
3755 | int64_t pos, int size) | |
cf8074b3 KW |
3756 | { |
3757 | QEMUIOVector qiov; | |
3758 | struct iovec iov = { | |
3759 | .iov_base = (void *) buf, | |
3760 | .iov_len = size, | |
3761 | }; | |
3762 | ||
3763 | qemu_iovec_init_external(&qiov, &iov, 1); | |
3764 | return bdrv_writev_vmstate(bs, &qiov, pos); | |
3765 | } | |
3766 | ||
3767 | int bdrv_writev_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos) | |
178e08a5 AL |
3768 | { |
3769 | BlockDriver *drv = bs->drv; | |
cf8074b3 KW |
3770 | |
3771 | if (!drv) { | |
178e08a5 | 3772 | return -ENOMEDIUM; |
cf8074b3 KW |
3773 | } else if (drv->bdrv_save_vmstate) { |
3774 | return drv->bdrv_save_vmstate(bs, qiov, pos); | |
3775 | } else if (bs->file) { | |
3776 | return bdrv_writev_vmstate(bs->file, qiov, pos); | |
3777 | } | |
3778 | ||
7cdb1f6d | 3779 | return -ENOTSUP; |
178e08a5 AL |
3780 | } |
3781 | ||
45566e9c CH |
3782 | int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf, |
3783 | int64_t pos, int size) | |
178e08a5 AL |
3784 | { |
3785 | BlockDriver *drv = bs->drv; | |
3786 | if (!drv) | |
3787 | return -ENOMEDIUM; | |
7cdb1f6d MK |
3788 | if (drv->bdrv_load_vmstate) |
3789 | return drv->bdrv_load_vmstate(bs, buf, pos, size); | |
3790 | if (bs->file) | |
3791 | return bdrv_load_vmstate(bs->file, buf, pos, size); | |
3792 | return -ENOTSUP; | |
178e08a5 AL |
3793 | } |
3794 | ||
8b9b0cc2 KW |
3795 | void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event) |
3796 | { | |
bf736fe3 | 3797 | if (!bs || !bs->drv || !bs->drv->bdrv_debug_event) { |
8b9b0cc2 KW |
3798 | return; |
3799 | } | |
3800 | ||
bf736fe3 | 3801 | bs->drv->bdrv_debug_event(bs, event); |
41c695c7 KW |
3802 | } |
3803 | ||
3804 | int bdrv_debug_breakpoint(BlockDriverState *bs, const char *event, | |
3805 | const char *tag) | |
3806 | { | |
3807 | while (bs && bs->drv && !bs->drv->bdrv_debug_breakpoint) { | |
3808 | bs = bs->file; | |
3809 | } | |
3810 | ||
3811 | if (bs && bs->drv && bs->drv->bdrv_debug_breakpoint) { | |
3812 | return bs->drv->bdrv_debug_breakpoint(bs, event, tag); | |
3813 | } | |
3814 | ||
3815 | return -ENOTSUP; | |
3816 | } | |
3817 | ||
4cc70e93 FZ |
3818 | int bdrv_debug_remove_breakpoint(BlockDriverState *bs, const char *tag) |
3819 | { | |
3820 | while (bs && bs->drv && !bs->drv->bdrv_debug_remove_breakpoint) { | |
3821 | bs = bs->file; | |
3822 | } | |
3823 | ||
3824 | if (bs && bs->drv && bs->drv->bdrv_debug_remove_breakpoint) { | |
3825 | return bs->drv->bdrv_debug_remove_breakpoint(bs, tag); | |
3826 | } | |
3827 | ||
3828 | return -ENOTSUP; | |
3829 | } | |
3830 | ||
41c695c7 KW |
3831 | int bdrv_debug_resume(BlockDriverState *bs, const char *tag) |
3832 | { | |
3833 | while (bs && bs->drv && !bs->drv->bdrv_debug_resume) { | |
3834 | bs = bs->file; | |
3835 | } | |
8b9b0cc2 | 3836 | |
41c695c7 KW |
3837 | if (bs && bs->drv && bs->drv->bdrv_debug_resume) { |
3838 | return bs->drv->bdrv_debug_resume(bs, tag); | |
3839 | } | |
3840 | ||
3841 | return -ENOTSUP; | |
3842 | } | |
3843 | ||
3844 | bool bdrv_debug_is_suspended(BlockDriverState *bs, const char *tag) | |
3845 | { | |
3846 | while (bs && bs->drv && !bs->drv->bdrv_debug_is_suspended) { | |
3847 | bs = bs->file; | |
3848 | } | |
3849 | ||
3850 | if (bs && bs->drv && bs->drv->bdrv_debug_is_suspended) { | |
3851 | return bs->drv->bdrv_debug_is_suspended(bs, tag); | |
3852 | } | |
3853 | ||
3854 | return false; | |
8b9b0cc2 KW |
3855 | } |
3856 | ||
199630b6 BS |
3857 | int bdrv_is_snapshot(BlockDriverState *bs) |
3858 | { | |
3859 | return !!(bs->open_flags & BDRV_O_SNAPSHOT); | |
3860 | } | |
3861 | ||
b1b1d783 JC |
3862 | /* backing_file can either be relative, or absolute, or a protocol. If it is |
3863 | * relative, it must be relative to the chain. So, passing in bs->filename | |
3864 | * from a BDS as backing_file should not be done, as that may be relative to | |
3865 | * the CWD rather than the chain. */ | |
e8a6bb9c MT |
3866 | BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs, |
3867 | const char *backing_file) | |
3868 | { | |
b1b1d783 JC |
3869 | char *filename_full = NULL; |
3870 | char *backing_file_full = NULL; | |
3871 | char *filename_tmp = NULL; | |
3872 | int is_protocol = 0; | |
3873 | BlockDriverState *curr_bs = NULL; | |
3874 | BlockDriverState *retval = NULL; | |
3875 | ||
3876 | if (!bs || !bs->drv || !backing_file) { | |
e8a6bb9c MT |
3877 | return NULL; |
3878 | } | |
3879 | ||
b1b1d783 JC |
3880 | filename_full = g_malloc(PATH_MAX); |
3881 | backing_file_full = g_malloc(PATH_MAX); | |
3882 | filename_tmp = g_malloc(PATH_MAX); | |
3883 | ||
3884 | is_protocol = path_has_protocol(backing_file); | |
3885 | ||
3886 | for (curr_bs = bs; curr_bs->backing_hd; curr_bs = curr_bs->backing_hd) { | |
3887 | ||
3888 | /* If either of the filename paths is actually a protocol, then | |
3889 | * compare unmodified paths; otherwise make paths relative */ | |
3890 | if (is_protocol || path_has_protocol(curr_bs->backing_file)) { | |
3891 | if (strcmp(backing_file, curr_bs->backing_file) == 0) { | |
3892 | retval = curr_bs->backing_hd; | |
3893 | break; | |
3894 | } | |
e8a6bb9c | 3895 | } else { |
b1b1d783 JC |
3896 | /* If not an absolute filename path, make it relative to the current |
3897 | * image's filename path */ | |
3898 | path_combine(filename_tmp, PATH_MAX, curr_bs->filename, | |
3899 | backing_file); | |
3900 | ||
3901 | /* We are going to compare absolute pathnames */ | |
3902 | if (!realpath(filename_tmp, filename_full)) { | |
3903 | continue; | |
3904 | } | |
3905 | ||
3906 | /* We need to make sure the backing filename we are comparing against | |
3907 | * is relative to the current image filename (or absolute) */ | |
3908 | path_combine(filename_tmp, PATH_MAX, curr_bs->filename, | |
3909 | curr_bs->backing_file); | |
3910 | ||
3911 | if (!realpath(filename_tmp, backing_file_full)) { | |
3912 | continue; | |
3913 | } | |
3914 | ||
3915 | if (strcmp(backing_file_full, filename_full) == 0) { | |
3916 | retval = curr_bs->backing_hd; | |
3917 | break; | |
3918 | } | |
e8a6bb9c MT |
3919 | } |
3920 | } | |
3921 | ||
b1b1d783 JC |
3922 | g_free(filename_full); |
3923 | g_free(backing_file_full); | |
3924 | g_free(filename_tmp); | |
3925 | return retval; | |
e8a6bb9c MT |
3926 | } |
3927 | ||
f198fd1c BC |
3928 | int bdrv_get_backing_file_depth(BlockDriverState *bs) |
3929 | { | |
3930 | if (!bs->drv) { | |
3931 | return 0; | |
3932 | } | |
3933 | ||
3934 | if (!bs->backing_hd) { | |
3935 | return 0; | |
3936 | } | |
3937 | ||
3938 | return 1 + bdrv_get_backing_file_depth(bs->backing_hd); | |
3939 | } | |
3940 | ||
79fac568 JC |
3941 | BlockDriverState *bdrv_find_base(BlockDriverState *bs) |
3942 | { | |
3943 | BlockDriverState *curr_bs = NULL; | |
3944 | ||
3945 | if (!bs) { | |
3946 | return NULL; | |
3947 | } | |
3948 | ||
3949 | curr_bs = bs; | |
3950 | ||
3951 | while (curr_bs->backing_hd) { | |
3952 | curr_bs = curr_bs->backing_hd; | |
3953 | } | |
3954 | return curr_bs; | |
3955 | } | |
3956 | ||
ea2384d3 | 3957 | /**************************************************************/ |
83f64091 | 3958 | /* async I/Os */ |
ea2384d3 | 3959 | |
3b69e4b9 | 3960 | BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num, |
f141eafe | 3961 | QEMUIOVector *qiov, int nb_sectors, |
3b69e4b9 | 3962 | BlockDriverCompletionFunc *cb, void *opaque) |
83f64091 | 3963 | { |
bbf0a440 SH |
3964 | trace_bdrv_aio_readv(bs, sector_num, nb_sectors, opaque); |
3965 | ||
d20d9b7c | 3966 | return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, 0, |
8c5873d6 | 3967 | cb, opaque, false); |
ea2384d3 FB |
3968 | } |
3969 | ||
f141eafe AL |
3970 | BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num, |
3971 | QEMUIOVector *qiov, int nb_sectors, | |
3972 | BlockDriverCompletionFunc *cb, void *opaque) | |
ea2384d3 | 3973 | { |
bbf0a440 SH |
3974 | trace_bdrv_aio_writev(bs, sector_num, nb_sectors, opaque); |
3975 | ||
d20d9b7c | 3976 | return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, 0, |
8c5873d6 | 3977 | cb, opaque, true); |
83f64091 FB |
3978 | } |
3979 | ||
d5ef94d4 PB |
3980 | BlockDriverAIOCB *bdrv_aio_write_zeroes(BlockDriverState *bs, |
3981 | int64_t sector_num, int nb_sectors, BdrvRequestFlags flags, | |
3982 | BlockDriverCompletionFunc *cb, void *opaque) | |
3983 | { | |
3984 | trace_bdrv_aio_write_zeroes(bs, sector_num, nb_sectors, flags, opaque); | |
3985 | ||
3986 | return bdrv_co_aio_rw_vector(bs, sector_num, NULL, nb_sectors, | |
3987 | BDRV_REQ_ZERO_WRITE | flags, | |
3988 | cb, opaque, true); | |
3989 | } | |
3990 | ||
40b4f539 KW |
3991 | |
3992 | typedef struct MultiwriteCB { | |
3993 | int error; | |
3994 | int num_requests; | |
3995 | int num_callbacks; | |
3996 | struct { | |
3997 | BlockDriverCompletionFunc *cb; | |
3998 | void *opaque; | |
3999 | QEMUIOVector *free_qiov; | |
40b4f539 KW |
4000 | } callbacks[]; |
4001 | } MultiwriteCB; | |
4002 | ||
4003 | static void multiwrite_user_cb(MultiwriteCB *mcb) | |
4004 | { | |
4005 | int i; | |
4006 | ||
4007 | for (i = 0; i < mcb->num_callbacks; i++) { | |
4008 | mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error); | |
1e1ea48d SH |
4009 | if (mcb->callbacks[i].free_qiov) { |
4010 | qemu_iovec_destroy(mcb->callbacks[i].free_qiov); | |
4011 | } | |
7267c094 | 4012 | g_free(mcb->callbacks[i].free_qiov); |
40b4f539 KW |
4013 | } |
4014 | } | |
4015 | ||
4016 | static void multiwrite_cb(void *opaque, int ret) | |
4017 | { | |
4018 | MultiwriteCB *mcb = opaque; | |
4019 | ||
6d519a5f SH |
4020 | trace_multiwrite_cb(mcb, ret); |
4021 | ||
cb6d3ca0 | 4022 | if (ret < 0 && !mcb->error) { |
40b4f539 | 4023 | mcb->error = ret; |
40b4f539 KW |
4024 | } |
4025 | ||
4026 | mcb->num_requests--; | |
4027 | if (mcb->num_requests == 0) { | |
de189a1b | 4028 | multiwrite_user_cb(mcb); |
7267c094 | 4029 | g_free(mcb); |
40b4f539 KW |
4030 | } |
4031 | } | |
4032 | ||
4033 | static int multiwrite_req_compare(const void *a, const void *b) | |
4034 | { | |
77be4366 CH |
4035 | const BlockRequest *req1 = a, *req2 = b; |
4036 | ||
4037 | /* | |
4038 | * Note that we can't simply subtract req2->sector from req1->sector | |
4039 | * here as that could overflow the return value. | |
4040 | */ | |
4041 | if (req1->sector > req2->sector) { | |
4042 | return 1; | |
4043 | } else if (req1->sector < req2->sector) { | |
4044 | return -1; | |
4045 | } else { | |
4046 | return 0; | |
4047 | } | |
40b4f539 KW |
4048 | } |
4049 | ||
4050 | /* | |
4051 | * Takes a bunch of requests and tries to merge them. Returns the number of | |
4052 | * requests that remain after merging. | |
4053 | */ | |
4054 | static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs, | |
4055 | int num_reqs, MultiwriteCB *mcb) | |
4056 | { | |
4057 | int i, outidx; | |
4058 | ||
4059 | // Sort requests by start sector | |
4060 | qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare); | |
4061 | ||
4062 | // Check if adjacent requests touch the same clusters. If so, combine them, | |
4063 | // filling up gaps with zero sectors. | |
4064 | outidx = 0; | |
4065 | for (i = 1; i < num_reqs; i++) { | |
4066 | int merge = 0; | |
4067 | int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors; | |
4068 | ||
b6a127a1 | 4069 | // Handle exactly sequential writes and overlapping writes. |
40b4f539 KW |
4070 | if (reqs[i].sector <= oldreq_last) { |
4071 | merge = 1; | |
4072 | } | |
4073 | ||
e2a305fb CH |
4074 | if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) { |
4075 | merge = 0; | |
4076 | } | |
4077 | ||
40b4f539 KW |
4078 | if (merge) { |
4079 | size_t size; | |
7267c094 | 4080 | QEMUIOVector *qiov = g_malloc0(sizeof(*qiov)); |
40b4f539 KW |
4081 | qemu_iovec_init(qiov, |
4082 | reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1); | |
4083 | ||
4084 | // Add the first request to the merged one. If the requests are | |
4085 | // overlapping, drop the last sectors of the first request. | |
4086 | size = (reqs[i].sector - reqs[outidx].sector) << 9; | |
1b093c48 | 4087 | qemu_iovec_concat(qiov, reqs[outidx].qiov, 0, size); |
40b4f539 | 4088 | |
b6a127a1 PB |
4089 | // We should need to add any zeros between the two requests |
4090 | assert (reqs[i].sector <= oldreq_last); | |
40b4f539 KW |
4091 | |
4092 | // Add the second request | |
1b093c48 | 4093 | qemu_iovec_concat(qiov, reqs[i].qiov, 0, reqs[i].qiov->size); |
40b4f539 | 4094 | |
cbf1dff2 | 4095 | reqs[outidx].nb_sectors = qiov->size >> 9; |
40b4f539 KW |
4096 | reqs[outidx].qiov = qiov; |
4097 | ||
4098 | mcb->callbacks[i].free_qiov = reqs[outidx].qiov; | |
4099 | } else { | |
4100 | outidx++; | |
4101 | reqs[outidx].sector = reqs[i].sector; | |
4102 | reqs[outidx].nb_sectors = reqs[i].nb_sectors; | |
4103 | reqs[outidx].qiov = reqs[i].qiov; | |
4104 | } | |
4105 | } | |
4106 | ||
4107 | return outidx + 1; | |
4108 | } | |
4109 | ||
4110 | /* | |
4111 | * Submit multiple AIO write requests at once. | |
4112 | * | |
4113 | * On success, the function returns 0 and all requests in the reqs array have | |
4114 | * been submitted. In error case this function returns -1, and any of the | |
4115 | * requests may or may not be submitted yet. In particular, this means that the | |
4116 | * callback will be called for some of the requests, for others it won't. The | |
4117 | * caller must check the error field of the BlockRequest to wait for the right | |
4118 | * callbacks (if error != 0, no callback will be called). | |
4119 | * | |
4120 | * The implementation may modify the contents of the reqs array, e.g. to merge | |
4121 | * requests. However, the fields opaque and error are left unmodified as they | |
4122 | * are used to signal failure for a single request to the caller. | |
4123 | */ | |
4124 | int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs) | |
4125 | { | |
40b4f539 KW |
4126 | MultiwriteCB *mcb; |
4127 | int i; | |
4128 | ||
301db7c2 RH |
4129 | /* don't submit writes if we don't have a medium */ |
4130 | if (bs->drv == NULL) { | |
4131 | for (i = 0; i < num_reqs; i++) { | |
4132 | reqs[i].error = -ENOMEDIUM; | |
4133 | } | |
4134 | return -1; | |
4135 | } | |
4136 | ||
40b4f539 KW |
4137 | if (num_reqs == 0) { |
4138 | return 0; | |
4139 | } | |
4140 | ||
4141 | // Create MultiwriteCB structure | |
7267c094 | 4142 | mcb = g_malloc0(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks)); |
40b4f539 KW |
4143 | mcb->num_requests = 0; |
4144 | mcb->num_callbacks = num_reqs; | |
4145 | ||
4146 | for (i = 0; i < num_reqs; i++) { | |
4147 | mcb->callbacks[i].cb = reqs[i].cb; | |
4148 | mcb->callbacks[i].opaque = reqs[i].opaque; | |
4149 | } | |
4150 | ||
4151 | // Check for mergable requests | |
4152 | num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb); | |
4153 | ||
6d519a5f SH |
4154 | trace_bdrv_aio_multiwrite(mcb, mcb->num_callbacks, num_reqs); |
4155 | ||
df9309fb PB |
4156 | /* Run the aio requests. */ |
4157 | mcb->num_requests = num_reqs; | |
40b4f539 | 4158 | for (i = 0; i < num_reqs; i++) { |
d20d9b7c PB |
4159 | bdrv_co_aio_rw_vector(bs, reqs[i].sector, reqs[i].qiov, |
4160 | reqs[i].nb_sectors, reqs[i].flags, | |
4161 | multiwrite_cb, mcb, | |
4162 | true); | |
40b4f539 KW |
4163 | } |
4164 | ||
4165 | return 0; | |
40b4f539 KW |
4166 | } |
4167 | ||
83f64091 | 4168 | void bdrv_aio_cancel(BlockDriverAIOCB *acb) |
83f64091 | 4169 | { |
d7331bed | 4170 | acb->aiocb_info->cancel(acb); |
83f64091 FB |
4171 | } |
4172 | ||
4173 | /**************************************************************/ | |
4174 | /* async block device emulation */ | |
4175 | ||
c16b5a2c CH |
4176 | typedef struct BlockDriverAIOCBSync { |
4177 | BlockDriverAIOCB common; | |
4178 | QEMUBH *bh; | |
4179 | int ret; | |
4180 | /* vector translation state */ | |
4181 | QEMUIOVector *qiov; | |
4182 | uint8_t *bounce; | |
4183 | int is_write; | |
4184 | } BlockDriverAIOCBSync; | |
4185 | ||
4186 | static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb) | |
4187 | { | |
b666d239 KW |
4188 | BlockDriverAIOCBSync *acb = |
4189 | container_of(blockacb, BlockDriverAIOCBSync, common); | |
6a7ad299 | 4190 | qemu_bh_delete(acb->bh); |
36afc451 | 4191 | acb->bh = NULL; |
c16b5a2c CH |
4192 | qemu_aio_release(acb); |
4193 | } | |
4194 | ||
d7331bed | 4195 | static const AIOCBInfo bdrv_em_aiocb_info = { |
c16b5a2c CH |
4196 | .aiocb_size = sizeof(BlockDriverAIOCBSync), |
4197 | .cancel = bdrv_aio_cancel_em, | |
4198 | }; | |
4199 | ||
ce1a14dc | 4200 | static void bdrv_aio_bh_cb(void *opaque) |
83f64091 | 4201 | { |
ce1a14dc | 4202 | BlockDriverAIOCBSync *acb = opaque; |
f141eafe | 4203 | |
f141eafe | 4204 | if (!acb->is_write) |
03396148 | 4205 | qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size); |
ceb42de8 | 4206 | qemu_vfree(acb->bounce); |
ce1a14dc | 4207 | acb->common.cb(acb->common.opaque, acb->ret); |
6a7ad299 | 4208 | qemu_bh_delete(acb->bh); |
36afc451 | 4209 | acb->bh = NULL; |
ce1a14dc | 4210 | qemu_aio_release(acb); |
83f64091 | 4211 | } |
beac80cd | 4212 | |
f141eafe AL |
4213 | static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs, |
4214 | int64_t sector_num, | |
4215 | QEMUIOVector *qiov, | |
4216 | int nb_sectors, | |
4217 | BlockDriverCompletionFunc *cb, | |
4218 | void *opaque, | |
4219 | int is_write) | |
4220 | ||
83f64091 | 4221 | { |
ce1a14dc | 4222 | BlockDriverAIOCBSync *acb; |
ce1a14dc | 4223 | |
d7331bed | 4224 | acb = qemu_aio_get(&bdrv_em_aiocb_info, bs, cb, opaque); |
f141eafe AL |
4225 | acb->is_write = is_write; |
4226 | acb->qiov = qiov; | |
e268ca52 | 4227 | acb->bounce = qemu_blockalign(bs, qiov->size); |
3f3aace8 | 4228 | acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); |
f141eafe AL |
4229 | |
4230 | if (is_write) { | |
d5e6b161 | 4231 | qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size); |
1ed20acf | 4232 | acb->ret = bs->drv->bdrv_write(bs, sector_num, acb->bounce, nb_sectors); |
f141eafe | 4233 | } else { |
1ed20acf | 4234 | acb->ret = bs->drv->bdrv_read(bs, sector_num, acb->bounce, nb_sectors); |
f141eafe AL |
4235 | } |
4236 | ||
ce1a14dc | 4237 | qemu_bh_schedule(acb->bh); |
f141eafe | 4238 | |
ce1a14dc | 4239 | return &acb->common; |
beac80cd FB |
4240 | } |
4241 | ||
f141eafe AL |
4242 | static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, |
4243 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, | |
ce1a14dc | 4244 | BlockDriverCompletionFunc *cb, void *opaque) |
beac80cd | 4245 | { |
f141eafe AL |
4246 | return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0); |
4247 | } | |
83f64091 | 4248 | |
f141eafe AL |
4249 | static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, |
4250 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, | |
4251 | BlockDriverCompletionFunc *cb, void *opaque) | |
4252 | { | |
4253 | return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1); | |
beac80cd | 4254 | } |
beac80cd | 4255 | |
68485420 KW |
4256 | |
4257 | typedef struct BlockDriverAIOCBCoroutine { | |
4258 | BlockDriverAIOCB common; | |
4259 | BlockRequest req; | |
4260 | bool is_write; | |
d318aea9 | 4261 | bool *done; |
68485420 KW |
4262 | QEMUBH* bh; |
4263 | } BlockDriverAIOCBCoroutine; | |
4264 | ||
4265 | static void bdrv_aio_co_cancel_em(BlockDriverAIOCB *blockacb) | |
4266 | { | |
d318aea9 KW |
4267 | BlockDriverAIOCBCoroutine *acb = |
4268 | container_of(blockacb, BlockDriverAIOCBCoroutine, common); | |
4269 | bool done = false; | |
4270 | ||
4271 | acb->done = &done; | |
4272 | while (!done) { | |
4273 | qemu_aio_wait(); | |
4274 | } | |
68485420 KW |
4275 | } |
4276 | ||
d7331bed | 4277 | static const AIOCBInfo bdrv_em_co_aiocb_info = { |
68485420 KW |
4278 | .aiocb_size = sizeof(BlockDriverAIOCBCoroutine), |
4279 | .cancel = bdrv_aio_co_cancel_em, | |
4280 | }; | |
4281 | ||
35246a68 | 4282 | static void bdrv_co_em_bh(void *opaque) |
68485420 KW |
4283 | { |
4284 | BlockDriverAIOCBCoroutine *acb = opaque; | |
4285 | ||
4286 | acb->common.cb(acb->common.opaque, acb->req.error); | |
d318aea9 KW |
4287 | |
4288 | if (acb->done) { | |
4289 | *acb->done = true; | |
4290 | } | |
4291 | ||
68485420 KW |
4292 | qemu_bh_delete(acb->bh); |
4293 | qemu_aio_release(acb); | |
4294 | } | |
4295 | ||
b2a61371 SH |
4296 | /* Invoke bdrv_co_do_readv/bdrv_co_do_writev */ |
4297 | static void coroutine_fn bdrv_co_do_rw(void *opaque) | |
4298 | { | |
4299 | BlockDriverAIOCBCoroutine *acb = opaque; | |
4300 | BlockDriverState *bs = acb->common.bs; | |
4301 | ||
4302 | if (!acb->is_write) { | |
4303 | acb->req.error = bdrv_co_do_readv(bs, acb->req.sector, | |
d20d9b7c | 4304 | acb->req.nb_sectors, acb->req.qiov, acb->req.flags); |
b2a61371 SH |
4305 | } else { |
4306 | acb->req.error = bdrv_co_do_writev(bs, acb->req.sector, | |
d20d9b7c | 4307 | acb->req.nb_sectors, acb->req.qiov, acb->req.flags); |
b2a61371 SH |
4308 | } |
4309 | ||
35246a68 | 4310 | acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); |
b2a61371 SH |
4311 | qemu_bh_schedule(acb->bh); |
4312 | } | |
4313 | ||
68485420 KW |
4314 | static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs, |
4315 | int64_t sector_num, | |
4316 | QEMUIOVector *qiov, | |
4317 | int nb_sectors, | |
d20d9b7c | 4318 | BdrvRequestFlags flags, |
68485420 KW |
4319 | BlockDriverCompletionFunc *cb, |
4320 | void *opaque, | |
8c5873d6 | 4321 | bool is_write) |
68485420 KW |
4322 | { |
4323 | Coroutine *co; | |
4324 | BlockDriverAIOCBCoroutine *acb; | |
4325 | ||
d7331bed | 4326 | acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque); |
68485420 KW |
4327 | acb->req.sector = sector_num; |
4328 | acb->req.nb_sectors = nb_sectors; | |
4329 | acb->req.qiov = qiov; | |
d20d9b7c | 4330 | acb->req.flags = flags; |
68485420 | 4331 | acb->is_write = is_write; |
d318aea9 | 4332 | acb->done = NULL; |
68485420 | 4333 | |
8c5873d6 | 4334 | co = qemu_coroutine_create(bdrv_co_do_rw); |
68485420 KW |
4335 | qemu_coroutine_enter(co, acb); |
4336 | ||
4337 | return &acb->common; | |
4338 | } | |
4339 | ||
07f07615 | 4340 | static void coroutine_fn bdrv_aio_flush_co_entry(void *opaque) |
b2e12bc6 | 4341 | { |
07f07615 PB |
4342 | BlockDriverAIOCBCoroutine *acb = opaque; |
4343 | BlockDriverState *bs = acb->common.bs; | |
b2e12bc6 | 4344 | |
07f07615 PB |
4345 | acb->req.error = bdrv_co_flush(bs); |
4346 | acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); | |
b2e12bc6 | 4347 | qemu_bh_schedule(acb->bh); |
b2e12bc6 CH |
4348 | } |
4349 | ||
07f07615 | 4350 | BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs, |
016f5cf6 AG |
4351 | BlockDriverCompletionFunc *cb, void *opaque) |
4352 | { | |
07f07615 | 4353 | trace_bdrv_aio_flush(bs, opaque); |
016f5cf6 | 4354 | |
07f07615 PB |
4355 | Coroutine *co; |
4356 | BlockDriverAIOCBCoroutine *acb; | |
016f5cf6 | 4357 | |
d7331bed | 4358 | acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque); |
d318aea9 KW |
4359 | acb->done = NULL; |
4360 | ||
07f07615 PB |
4361 | co = qemu_coroutine_create(bdrv_aio_flush_co_entry); |
4362 | qemu_coroutine_enter(co, acb); | |
016f5cf6 | 4363 | |
016f5cf6 AG |
4364 | return &acb->common; |
4365 | } | |
4366 | ||
4265d620 PB |
4367 | static void coroutine_fn bdrv_aio_discard_co_entry(void *opaque) |
4368 | { | |
4369 | BlockDriverAIOCBCoroutine *acb = opaque; | |
4370 | BlockDriverState *bs = acb->common.bs; | |
4371 | ||
4372 | acb->req.error = bdrv_co_discard(bs, acb->req.sector, acb->req.nb_sectors); | |
4373 | acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); | |
4374 | qemu_bh_schedule(acb->bh); | |
4375 | } | |
4376 | ||
4377 | BlockDriverAIOCB *bdrv_aio_discard(BlockDriverState *bs, | |
4378 | int64_t sector_num, int nb_sectors, | |
4379 | BlockDriverCompletionFunc *cb, void *opaque) | |
4380 | { | |
4381 | Coroutine *co; | |
4382 | BlockDriverAIOCBCoroutine *acb; | |
4383 | ||
4384 | trace_bdrv_aio_discard(bs, sector_num, nb_sectors, opaque); | |
4385 | ||
d7331bed | 4386 | acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque); |
4265d620 PB |
4387 | acb->req.sector = sector_num; |
4388 | acb->req.nb_sectors = nb_sectors; | |
d318aea9 | 4389 | acb->done = NULL; |
4265d620 PB |
4390 | co = qemu_coroutine_create(bdrv_aio_discard_co_entry); |
4391 | qemu_coroutine_enter(co, acb); | |
4392 | ||
4393 | return &acb->common; | |
4394 | } | |
4395 | ||
ea2384d3 FB |
4396 | void bdrv_init(void) |
4397 | { | |
5efa9d5a | 4398 | module_call_init(MODULE_INIT_BLOCK); |
ea2384d3 | 4399 | } |
ce1a14dc | 4400 | |
eb852011 MA |
4401 | void bdrv_init_with_whitelist(void) |
4402 | { | |
4403 | use_bdrv_whitelist = 1; | |
4404 | bdrv_init(); | |
4405 | } | |
4406 | ||
d7331bed | 4407 | void *qemu_aio_get(const AIOCBInfo *aiocb_info, BlockDriverState *bs, |
c16b5a2c | 4408 | BlockDriverCompletionFunc *cb, void *opaque) |
ce1a14dc | 4409 | { |
ce1a14dc PB |
4410 | BlockDriverAIOCB *acb; |
4411 | ||
d7331bed SH |
4412 | acb = g_slice_alloc(aiocb_info->aiocb_size); |
4413 | acb->aiocb_info = aiocb_info; | |
ce1a14dc PB |
4414 | acb->bs = bs; |
4415 | acb->cb = cb; | |
4416 | acb->opaque = opaque; | |
4417 | return acb; | |
4418 | } | |
4419 | ||
4420 | void qemu_aio_release(void *p) | |
4421 | { | |
d37c975f | 4422 | BlockDriverAIOCB *acb = p; |
d7331bed | 4423 | g_slice_free1(acb->aiocb_info->aiocb_size, acb); |
ce1a14dc | 4424 | } |
19cb3738 | 4425 | |
f9f05dc5 KW |
4426 | /**************************************************************/ |
4427 | /* Coroutine block device emulation */ | |
4428 | ||
4429 | typedef struct CoroutineIOCompletion { | |
4430 | Coroutine *coroutine; | |
4431 | int ret; | |
4432 | } CoroutineIOCompletion; | |
4433 | ||
4434 | static void bdrv_co_io_em_complete(void *opaque, int ret) | |
4435 | { | |
4436 | CoroutineIOCompletion *co = opaque; | |
4437 | ||
4438 | co->ret = ret; | |
4439 | qemu_coroutine_enter(co->coroutine, NULL); | |
4440 | } | |
4441 | ||
4442 | static int coroutine_fn bdrv_co_io_em(BlockDriverState *bs, int64_t sector_num, | |
4443 | int nb_sectors, QEMUIOVector *iov, | |
4444 | bool is_write) | |
4445 | { | |
4446 | CoroutineIOCompletion co = { | |
4447 | .coroutine = qemu_coroutine_self(), | |
4448 | }; | |
4449 | BlockDriverAIOCB *acb; | |
4450 | ||
4451 | if (is_write) { | |
a652d160 SH |
4452 | acb = bs->drv->bdrv_aio_writev(bs, sector_num, iov, nb_sectors, |
4453 | bdrv_co_io_em_complete, &co); | |
f9f05dc5 | 4454 | } else { |
a652d160 SH |
4455 | acb = bs->drv->bdrv_aio_readv(bs, sector_num, iov, nb_sectors, |
4456 | bdrv_co_io_em_complete, &co); | |
f9f05dc5 KW |
4457 | } |
4458 | ||
59370aaa | 4459 | trace_bdrv_co_io_em(bs, sector_num, nb_sectors, is_write, acb); |
f9f05dc5 KW |
4460 | if (!acb) { |
4461 | return -EIO; | |
4462 | } | |
4463 | qemu_coroutine_yield(); | |
4464 | ||
4465 | return co.ret; | |
4466 | } | |
4467 | ||
4468 | static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs, | |
4469 | int64_t sector_num, int nb_sectors, | |
4470 | QEMUIOVector *iov) | |
4471 | { | |
4472 | return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, false); | |
4473 | } | |
4474 | ||
4475 | static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs, | |
4476 | int64_t sector_num, int nb_sectors, | |
4477 | QEMUIOVector *iov) | |
4478 | { | |
4479 | return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, true); | |
4480 | } | |
4481 | ||
07f07615 | 4482 | static void coroutine_fn bdrv_flush_co_entry(void *opaque) |
e7a8a783 | 4483 | { |
07f07615 PB |
4484 | RwCo *rwco = opaque; |
4485 | ||
4486 | rwco->ret = bdrv_co_flush(rwco->bs); | |
4487 | } | |
4488 | ||
4489 | int coroutine_fn bdrv_co_flush(BlockDriverState *bs) | |
4490 | { | |
eb489bb1 KW |
4491 | int ret; |
4492 | ||
29cdb251 | 4493 | if (!bs || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) { |
07f07615 | 4494 | return 0; |
eb489bb1 KW |
4495 | } |
4496 | ||
ca716364 | 4497 | /* Write back cached data to the OS even with cache=unsafe */ |
bf736fe3 | 4498 | BLKDBG_EVENT(bs->file, BLKDBG_FLUSH_TO_OS); |
eb489bb1 KW |
4499 | if (bs->drv->bdrv_co_flush_to_os) { |
4500 | ret = bs->drv->bdrv_co_flush_to_os(bs); | |
4501 | if (ret < 0) { | |
4502 | return ret; | |
4503 | } | |
4504 | } | |
4505 | ||
ca716364 KW |
4506 | /* But don't actually force it to the disk with cache=unsafe */ |
4507 | if (bs->open_flags & BDRV_O_NO_FLUSH) { | |
d4c82329 | 4508 | goto flush_parent; |
ca716364 KW |
4509 | } |
4510 | ||
bf736fe3 | 4511 | BLKDBG_EVENT(bs->file, BLKDBG_FLUSH_TO_DISK); |
eb489bb1 | 4512 | if (bs->drv->bdrv_co_flush_to_disk) { |
29cdb251 | 4513 | ret = bs->drv->bdrv_co_flush_to_disk(bs); |
07f07615 PB |
4514 | } else if (bs->drv->bdrv_aio_flush) { |
4515 | BlockDriverAIOCB *acb; | |
4516 | CoroutineIOCompletion co = { | |
4517 | .coroutine = qemu_coroutine_self(), | |
4518 | }; | |
4519 | ||
4520 | acb = bs->drv->bdrv_aio_flush(bs, bdrv_co_io_em_complete, &co); | |
4521 | if (acb == NULL) { | |
29cdb251 | 4522 | ret = -EIO; |
07f07615 PB |
4523 | } else { |
4524 | qemu_coroutine_yield(); | |
29cdb251 | 4525 | ret = co.ret; |
07f07615 | 4526 | } |
07f07615 PB |
4527 | } else { |
4528 | /* | |
4529 | * Some block drivers always operate in either writethrough or unsafe | |
4530 | * mode and don't support bdrv_flush therefore. Usually qemu doesn't | |
4531 | * know how the server works (because the behaviour is hardcoded or | |
4532 | * depends on server-side configuration), so we can't ensure that | |
4533 | * everything is safe on disk. Returning an error doesn't work because | |
4534 | * that would break guests even if the server operates in writethrough | |
4535 | * mode. | |
4536 | * | |
4537 | * Let's hope the user knows what he's doing. | |
4538 | */ | |
29cdb251 | 4539 | ret = 0; |
07f07615 | 4540 | } |
29cdb251 PB |
4541 | if (ret < 0) { |
4542 | return ret; | |
4543 | } | |
4544 | ||
4545 | /* Now flush the underlying protocol. It will also have BDRV_O_NO_FLUSH | |
4546 | * in the case of cache=unsafe, so there are no useless flushes. | |
4547 | */ | |
d4c82329 | 4548 | flush_parent: |
29cdb251 | 4549 | return bdrv_co_flush(bs->file); |
07f07615 PB |
4550 | } |
4551 | ||
0f15423c AL |
4552 | void bdrv_invalidate_cache(BlockDriverState *bs) |
4553 | { | |
4554 | if (bs->drv && bs->drv->bdrv_invalidate_cache) { | |
4555 | bs->drv->bdrv_invalidate_cache(bs); | |
4556 | } | |
4557 | } | |
4558 | ||
4559 | void bdrv_invalidate_cache_all(void) | |
4560 | { | |
4561 | BlockDriverState *bs; | |
4562 | ||
dc364f4c | 4563 | QTAILQ_FOREACH(bs, &bdrv_states, device_list) { |
0f15423c AL |
4564 | bdrv_invalidate_cache(bs); |
4565 | } | |
4566 | } | |
4567 | ||
07789269 BC |
4568 | void bdrv_clear_incoming_migration_all(void) |
4569 | { | |
4570 | BlockDriverState *bs; | |
4571 | ||
dc364f4c | 4572 | QTAILQ_FOREACH(bs, &bdrv_states, device_list) { |
07789269 BC |
4573 | bs->open_flags = bs->open_flags & ~(BDRV_O_INCOMING); |
4574 | } | |
4575 | } | |
4576 | ||
07f07615 PB |
4577 | int bdrv_flush(BlockDriverState *bs) |
4578 | { | |
4579 | Coroutine *co; | |
4580 | RwCo rwco = { | |
4581 | .bs = bs, | |
4582 | .ret = NOT_DONE, | |
e7a8a783 | 4583 | }; |
e7a8a783 | 4584 | |
07f07615 PB |
4585 | if (qemu_in_coroutine()) { |
4586 | /* Fast-path if already in coroutine context */ | |
4587 | bdrv_flush_co_entry(&rwco); | |
4588 | } else { | |
4589 | co = qemu_coroutine_create(bdrv_flush_co_entry); | |
4590 | qemu_coroutine_enter(co, &rwco); | |
4591 | while (rwco.ret == NOT_DONE) { | |
4592 | qemu_aio_wait(); | |
4593 | } | |
e7a8a783 | 4594 | } |
07f07615 PB |
4595 | |
4596 | return rwco.ret; | |
e7a8a783 KW |
4597 | } |
4598 | ||
4265d620 PB |
4599 | static void coroutine_fn bdrv_discard_co_entry(void *opaque) |
4600 | { | |
4601 | RwCo *rwco = opaque; | |
4602 | ||
4603 | rwco->ret = bdrv_co_discard(rwco->bs, rwco->sector_num, rwco->nb_sectors); | |
4604 | } | |
4605 | ||
6f14da52 PL |
4606 | /* if no limit is specified in the BlockLimits use a default |
4607 | * of 32768 512-byte sectors (16 MiB) per request. | |
4608 | */ | |
4609 | #define MAX_DISCARD_DEFAULT 32768 | |
4610 | ||
4265d620 PB |
4611 | int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num, |
4612 | int nb_sectors) | |
4613 | { | |
d51e9fe5 PB |
4614 | int max_discard; |
4615 | ||
4265d620 PB |
4616 | if (!bs->drv) { |
4617 | return -ENOMEDIUM; | |
4618 | } else if (bdrv_check_request(bs, sector_num, nb_sectors)) { | |
4619 | return -EIO; | |
4620 | } else if (bs->read_only) { | |
4621 | return -EROFS; | |
df702c9b PB |
4622 | } |
4623 | ||
e4654d2d | 4624 | bdrv_reset_dirty(bs, sector_num, nb_sectors); |
df702c9b | 4625 | |
9e8f1835 PB |
4626 | /* Do nothing if disabled. */ |
4627 | if (!(bs->open_flags & BDRV_O_UNMAP)) { | |
4628 | return 0; | |
4629 | } | |
4630 | ||
d51e9fe5 PB |
4631 | if (!bs->drv->bdrv_co_discard && !bs->drv->bdrv_aio_discard) { |
4632 | return 0; | |
4633 | } | |
6f14da52 | 4634 | |
d51e9fe5 PB |
4635 | max_discard = bs->bl.max_discard ? bs->bl.max_discard : MAX_DISCARD_DEFAULT; |
4636 | while (nb_sectors > 0) { | |
4637 | int ret; | |
4638 | int num = nb_sectors; | |
6f14da52 | 4639 | |
d51e9fe5 PB |
4640 | /* align request */ |
4641 | if (bs->bl.discard_alignment && | |
4642 | num >= bs->bl.discard_alignment && | |
4643 | sector_num % bs->bl.discard_alignment) { | |
4644 | if (num > bs->bl.discard_alignment) { | |
4645 | num = bs->bl.discard_alignment; | |
6f14da52 | 4646 | } |
d51e9fe5 PB |
4647 | num -= sector_num % bs->bl.discard_alignment; |
4648 | } | |
6f14da52 | 4649 | |
d51e9fe5 PB |
4650 | /* limit request size */ |
4651 | if (num > max_discard) { | |
4652 | num = max_discard; | |
4653 | } | |
6f14da52 | 4654 | |
d51e9fe5 | 4655 | if (bs->drv->bdrv_co_discard) { |
6f14da52 | 4656 | ret = bs->drv->bdrv_co_discard(bs, sector_num, num); |
d51e9fe5 PB |
4657 | } else { |
4658 | BlockDriverAIOCB *acb; | |
4659 | CoroutineIOCompletion co = { | |
4660 | .coroutine = qemu_coroutine_self(), | |
4661 | }; | |
4662 | ||
4663 | acb = bs->drv->bdrv_aio_discard(bs, sector_num, nb_sectors, | |
4664 | bdrv_co_io_em_complete, &co); | |
4665 | if (acb == NULL) { | |
4666 | return -EIO; | |
4667 | } else { | |
4668 | qemu_coroutine_yield(); | |
4669 | ret = co.ret; | |
6f14da52 | 4670 | } |
6f14da52 | 4671 | } |
7ce21016 | 4672 | if (ret && ret != -ENOTSUP) { |
d51e9fe5 | 4673 | return ret; |
4265d620 | 4674 | } |
d51e9fe5 PB |
4675 | |
4676 | sector_num += num; | |
4677 | nb_sectors -= num; | |
4265d620 | 4678 | } |
d51e9fe5 | 4679 | return 0; |
4265d620 PB |
4680 | } |
4681 | ||
4682 | int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors) | |
4683 | { | |
4684 | Coroutine *co; | |
4685 | RwCo rwco = { | |
4686 | .bs = bs, | |
4687 | .sector_num = sector_num, | |
4688 | .nb_sectors = nb_sectors, | |
4689 | .ret = NOT_DONE, | |
4690 | }; | |
4691 | ||
4692 | if (qemu_in_coroutine()) { | |
4693 | /* Fast-path if already in coroutine context */ | |
4694 | bdrv_discard_co_entry(&rwco); | |
4695 | } else { | |
4696 | co = qemu_coroutine_create(bdrv_discard_co_entry); | |
4697 | qemu_coroutine_enter(co, &rwco); | |
4698 | while (rwco.ret == NOT_DONE) { | |
4699 | qemu_aio_wait(); | |
4700 | } | |
4701 | } | |
4702 | ||
4703 | return rwco.ret; | |
4704 | } | |
4705 | ||
19cb3738 FB |
4706 | /**************************************************************/ |
4707 | /* removable device support */ | |
4708 | ||
4709 | /** | |
4710 | * Return TRUE if the media is present | |
4711 | */ | |
4712 | int bdrv_is_inserted(BlockDriverState *bs) | |
4713 | { | |
4714 | BlockDriver *drv = bs->drv; | |
a1aff5bf | 4715 | |
19cb3738 FB |
4716 | if (!drv) |
4717 | return 0; | |
4718 | if (!drv->bdrv_is_inserted) | |
a1aff5bf MA |
4719 | return 1; |
4720 | return drv->bdrv_is_inserted(bs); | |
19cb3738 FB |
4721 | } |
4722 | ||
4723 | /** | |
8e49ca46 MA |
4724 | * Return whether the media changed since the last call to this |
4725 | * function, or -ENOTSUP if we don't know. Most drivers don't know. | |
19cb3738 FB |
4726 | */ |
4727 | int bdrv_media_changed(BlockDriverState *bs) | |
4728 | { | |
4729 | BlockDriver *drv = bs->drv; | |
19cb3738 | 4730 | |
8e49ca46 MA |
4731 | if (drv && drv->bdrv_media_changed) { |
4732 | return drv->bdrv_media_changed(bs); | |
4733 | } | |
4734 | return -ENOTSUP; | |
19cb3738 FB |
4735 | } |
4736 | ||
4737 | /** | |
4738 | * If eject_flag is TRUE, eject the media. Otherwise, close the tray | |
4739 | */ | |
f36f3949 | 4740 | void bdrv_eject(BlockDriverState *bs, bool eject_flag) |
19cb3738 FB |
4741 | { |
4742 | BlockDriver *drv = bs->drv; | |
19cb3738 | 4743 | |
822e1cd1 MA |
4744 | if (drv && drv->bdrv_eject) { |
4745 | drv->bdrv_eject(bs, eject_flag); | |
19cb3738 | 4746 | } |
6f382ed2 LC |
4747 | |
4748 | if (bs->device_name[0] != '\0') { | |
4749 | bdrv_emit_qmp_eject_event(bs, eject_flag); | |
4750 | } | |
19cb3738 FB |
4751 | } |
4752 | ||
19cb3738 FB |
4753 | /** |
4754 | * Lock or unlock the media (if it is locked, the user won't be able | |
4755 | * to eject it manually). | |
4756 | */ | |
025e849a | 4757 | void bdrv_lock_medium(BlockDriverState *bs, bool locked) |
19cb3738 FB |
4758 | { |
4759 | BlockDriver *drv = bs->drv; | |
4760 | ||
025e849a | 4761 | trace_bdrv_lock_medium(bs, locked); |
b8c6d095 | 4762 | |
025e849a MA |
4763 | if (drv && drv->bdrv_lock_medium) { |
4764 | drv->bdrv_lock_medium(bs, locked); | |
19cb3738 FB |
4765 | } |
4766 | } | |
985a03b0 TS |
4767 | |
4768 | /* needed for generic scsi interface */ | |
4769 | ||
4770 | int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) | |
4771 | { | |
4772 | BlockDriver *drv = bs->drv; | |
4773 | ||
4774 | if (drv && drv->bdrv_ioctl) | |
4775 | return drv->bdrv_ioctl(bs, req, buf); | |
4776 | return -ENOTSUP; | |
4777 | } | |
7d780669 | 4778 | |
221f715d AL |
4779 | BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs, |
4780 | unsigned long int req, void *buf, | |
4781 | BlockDriverCompletionFunc *cb, void *opaque) | |
7d780669 | 4782 | { |
221f715d | 4783 | BlockDriver *drv = bs->drv; |
7d780669 | 4784 | |
221f715d AL |
4785 | if (drv && drv->bdrv_aio_ioctl) |
4786 | return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque); | |
4787 | return NULL; | |
7d780669 | 4788 | } |
e268ca52 | 4789 | |
7b6f9300 MA |
4790 | void bdrv_set_buffer_alignment(BlockDriverState *bs, int align) |
4791 | { | |
4792 | bs->buffer_alignment = align; | |
4793 | } | |
7cd1e32a | 4794 | |
e268ca52 AL |
4795 | void *qemu_blockalign(BlockDriverState *bs, size_t size) |
4796 | { | |
4797 | return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size); | |
4798 | } | |
7cd1e32a | 4799 | |
c53b1c51 SH |
4800 | /* |
4801 | * Check if all memory in this vector is sector aligned. | |
4802 | */ | |
4803 | bool bdrv_qiov_is_aligned(BlockDriverState *bs, QEMUIOVector *qiov) | |
4804 | { | |
4805 | int i; | |
4806 | ||
4807 | for (i = 0; i < qiov->niov; i++) { | |
4808 | if ((uintptr_t) qiov->iov[i].iov_base % bs->buffer_alignment) { | |
4809 | return false; | |
4810 | } | |
4811 | } | |
4812 | ||
4813 | return true; | |
4814 | } | |
4815 | ||
e4654d2d | 4816 | BdrvDirtyBitmap *bdrv_create_dirty_bitmap(BlockDriverState *bs, int granularity) |
7cd1e32a LS |
4817 | { |
4818 | int64_t bitmap_size; | |
e4654d2d | 4819 | BdrvDirtyBitmap *bitmap; |
a55eb92c | 4820 | |
50717e94 PB |
4821 | assert((granularity & (granularity - 1)) == 0); |
4822 | ||
e4654d2d FZ |
4823 | granularity >>= BDRV_SECTOR_BITS; |
4824 | assert(granularity); | |
4825 | bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS); | |
4826 | bitmap = g_malloc0(sizeof(BdrvDirtyBitmap)); | |
4827 | bitmap->bitmap = hbitmap_alloc(bitmap_size, ffs(granularity) - 1); | |
4828 | QLIST_INSERT_HEAD(&bs->dirty_bitmaps, bitmap, list); | |
4829 | return bitmap; | |
4830 | } | |
4831 | ||
4832 | void bdrv_release_dirty_bitmap(BlockDriverState *bs, BdrvDirtyBitmap *bitmap) | |
4833 | { | |
4834 | BdrvDirtyBitmap *bm, *next; | |
4835 | QLIST_FOREACH_SAFE(bm, &bs->dirty_bitmaps, list, next) { | |
4836 | if (bm == bitmap) { | |
4837 | QLIST_REMOVE(bitmap, list); | |
4838 | hbitmap_free(bitmap->bitmap); | |
4839 | g_free(bitmap); | |
4840 | return; | |
a55eb92c | 4841 | } |
7cd1e32a LS |
4842 | } |
4843 | } | |
4844 | ||
21b56835 FZ |
4845 | BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs) |
4846 | { | |
4847 | BdrvDirtyBitmap *bm; | |
4848 | BlockDirtyInfoList *list = NULL; | |
4849 | BlockDirtyInfoList **plist = &list; | |
4850 | ||
4851 | QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) { | |
4852 | BlockDirtyInfo *info = g_malloc0(sizeof(BlockDirtyInfo)); | |
4853 | BlockDirtyInfoList *entry = g_malloc0(sizeof(BlockDirtyInfoList)); | |
4854 | info->count = bdrv_get_dirty_count(bs, bm); | |
4855 | info->granularity = | |
4856 | ((int64_t) BDRV_SECTOR_SIZE << hbitmap_granularity(bm->bitmap)); | |
4857 | entry->value = info; | |
4858 | *plist = entry; | |
4859 | plist = &entry->next; | |
4860 | } | |
4861 | ||
4862 | return list; | |
4863 | } | |
4864 | ||
e4654d2d | 4865 | int bdrv_get_dirty(BlockDriverState *bs, BdrvDirtyBitmap *bitmap, int64_t sector) |
7cd1e32a | 4866 | { |
e4654d2d FZ |
4867 | if (bitmap) { |
4868 | return hbitmap_get(bitmap->bitmap, sector); | |
7cd1e32a LS |
4869 | } else { |
4870 | return 0; | |
4871 | } | |
4872 | } | |
4873 | ||
e4654d2d FZ |
4874 | void bdrv_dirty_iter_init(BlockDriverState *bs, |
4875 | BdrvDirtyBitmap *bitmap, HBitmapIter *hbi) | |
1755da16 | 4876 | { |
e4654d2d | 4877 | hbitmap_iter_init(hbi, bitmap->bitmap, 0); |
1755da16 PB |
4878 | } |
4879 | ||
4880 | void bdrv_set_dirty(BlockDriverState *bs, int64_t cur_sector, | |
4881 | int nr_sectors) | |
4882 | { | |
e4654d2d FZ |
4883 | BdrvDirtyBitmap *bitmap; |
4884 | QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) { | |
4885 | hbitmap_set(bitmap->bitmap, cur_sector, nr_sectors); | |
4886 | } | |
1755da16 PB |
4887 | } |
4888 | ||
e4654d2d | 4889 | void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector, int nr_sectors) |
7cd1e32a | 4890 | { |
e4654d2d FZ |
4891 | BdrvDirtyBitmap *bitmap; |
4892 | QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) { | |
4893 | hbitmap_reset(bitmap->bitmap, cur_sector, nr_sectors); | |
4894 | } | |
7cd1e32a | 4895 | } |
aaa0eb75 | 4896 | |
e4654d2d | 4897 | int64_t bdrv_get_dirty_count(BlockDriverState *bs, BdrvDirtyBitmap *bitmap) |
aaa0eb75 | 4898 | { |
e4654d2d | 4899 | return hbitmap_count(bitmap->bitmap); |
aaa0eb75 | 4900 | } |
f88e1a42 | 4901 | |
9fcb0251 FZ |
4902 | /* Get a reference to bs */ |
4903 | void bdrv_ref(BlockDriverState *bs) | |
4904 | { | |
4905 | bs->refcnt++; | |
4906 | } | |
4907 | ||
4908 | /* Release a previously grabbed reference to bs. | |
4909 | * If after releasing, reference count is zero, the BlockDriverState is | |
4910 | * deleted. */ | |
4911 | void bdrv_unref(BlockDriverState *bs) | |
4912 | { | |
4913 | assert(bs->refcnt > 0); | |
4914 | if (--bs->refcnt == 0) { | |
4915 | bdrv_delete(bs); | |
4916 | } | |
4917 | } | |
4918 | ||
db593f25 MT |
4919 | void bdrv_set_in_use(BlockDriverState *bs, int in_use) |
4920 | { | |
4921 | assert(bs->in_use != in_use); | |
4922 | bs->in_use = in_use; | |
4923 | } | |
4924 | ||
4925 | int bdrv_in_use(BlockDriverState *bs) | |
4926 | { | |
4927 | return bs->in_use; | |
4928 | } | |
4929 | ||
28a7282a LC |
4930 | void bdrv_iostatus_enable(BlockDriverState *bs) |
4931 | { | |
d6bf279e | 4932 | bs->iostatus_enabled = true; |
58e21ef5 | 4933 | bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK; |
28a7282a LC |
4934 | } |
4935 | ||
4936 | /* The I/O status is only enabled if the drive explicitly | |
4937 | * enables it _and_ the VM is configured to stop on errors */ | |
4938 | bool bdrv_iostatus_is_enabled(const BlockDriverState *bs) | |
4939 | { | |
d6bf279e | 4940 | return (bs->iostatus_enabled && |
92aa5c6d PB |
4941 | (bs->on_write_error == BLOCKDEV_ON_ERROR_ENOSPC || |
4942 | bs->on_write_error == BLOCKDEV_ON_ERROR_STOP || | |
4943 | bs->on_read_error == BLOCKDEV_ON_ERROR_STOP)); | |
28a7282a LC |
4944 | } |
4945 | ||
4946 | void bdrv_iostatus_disable(BlockDriverState *bs) | |
4947 | { | |
d6bf279e | 4948 | bs->iostatus_enabled = false; |
28a7282a LC |
4949 | } |
4950 | ||
4951 | void bdrv_iostatus_reset(BlockDriverState *bs) | |
4952 | { | |
4953 | if (bdrv_iostatus_is_enabled(bs)) { | |
58e21ef5 | 4954 | bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK; |
3bd293c3 PB |
4955 | if (bs->job) { |
4956 | block_job_iostatus_reset(bs->job); | |
4957 | } | |
28a7282a LC |
4958 | } |
4959 | } | |
4960 | ||
28a7282a LC |
4961 | void bdrv_iostatus_set_err(BlockDriverState *bs, int error) |
4962 | { | |
3e1caa5f PB |
4963 | assert(bdrv_iostatus_is_enabled(bs)); |
4964 | if (bs->iostatus == BLOCK_DEVICE_IO_STATUS_OK) { | |
58e21ef5 LC |
4965 | bs->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE : |
4966 | BLOCK_DEVICE_IO_STATUS_FAILED; | |
28a7282a LC |
4967 | } |
4968 | } | |
4969 | ||
a597e79c CH |
4970 | void |
4971 | bdrv_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, int64_t bytes, | |
4972 | enum BlockAcctType type) | |
4973 | { | |
4974 | assert(type < BDRV_MAX_IOTYPE); | |
4975 | ||
4976 | cookie->bytes = bytes; | |
c488c7f6 | 4977 | cookie->start_time_ns = get_clock(); |
a597e79c CH |
4978 | cookie->type = type; |
4979 | } | |
4980 | ||
4981 | void | |
4982 | bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) | |
4983 | { | |
4984 | assert(cookie->type < BDRV_MAX_IOTYPE); | |
4985 | ||
4986 | bs->nr_bytes[cookie->type] += cookie->bytes; | |
4987 | bs->nr_ops[cookie->type]++; | |
c488c7f6 | 4988 | bs->total_time_ns[cookie->type] += get_clock() - cookie->start_time_ns; |
a597e79c CH |
4989 | } |
4990 | ||
d92ada22 LC |
4991 | void bdrv_img_create(const char *filename, const char *fmt, |
4992 | const char *base_filename, const char *base_fmt, | |
f382d43a MR |
4993 | char *options, uint64_t img_size, int flags, |
4994 | Error **errp, bool quiet) | |
f88e1a42 JS |
4995 | { |
4996 | QEMUOptionParameter *param = NULL, *create_options = NULL; | |
d220894e | 4997 | QEMUOptionParameter *backing_fmt, *backing_file, *size; |
f88e1a42 | 4998 | BlockDriver *drv, *proto_drv; |
96df67d1 | 4999 | BlockDriver *backing_drv = NULL; |
cc84d90f | 5000 | Error *local_err = NULL; |
f88e1a42 JS |
5001 | int ret = 0; |
5002 | ||
5003 | /* Find driver and parse its options */ | |
5004 | drv = bdrv_find_format(fmt); | |
5005 | if (!drv) { | |
71c79813 | 5006 | error_setg(errp, "Unknown file format '%s'", fmt); |
d92ada22 | 5007 | return; |
f88e1a42 JS |
5008 | } |
5009 | ||
98289620 | 5010 | proto_drv = bdrv_find_protocol(filename, true); |
f88e1a42 | 5011 | if (!proto_drv) { |
71c79813 | 5012 | error_setg(errp, "Unknown protocol '%s'", filename); |
d92ada22 | 5013 | return; |
f88e1a42 JS |
5014 | } |
5015 | ||
5016 | create_options = append_option_parameters(create_options, | |
5017 | drv->create_options); | |
5018 | create_options = append_option_parameters(create_options, | |
5019 | proto_drv->create_options); | |
5020 | ||
5021 | /* Create parameter list with default values */ | |
5022 | param = parse_option_parameters("", create_options, param); | |
5023 | ||
5024 | set_option_parameter_int(param, BLOCK_OPT_SIZE, img_size); | |
5025 | ||
5026 | /* Parse -o options */ | |
5027 | if (options) { | |
5028 | param = parse_option_parameters(options, create_options, param); | |
5029 | if (param == NULL) { | |
71c79813 | 5030 | error_setg(errp, "Invalid options for file format '%s'.", fmt); |
f88e1a42 JS |
5031 | goto out; |
5032 | } | |
5033 | } | |
5034 | ||
5035 | if (base_filename) { | |
5036 | if (set_option_parameter(param, BLOCK_OPT_BACKING_FILE, | |
5037 | base_filename)) { | |
71c79813 LC |
5038 | error_setg(errp, "Backing file not supported for file format '%s'", |
5039 | fmt); | |
f88e1a42 JS |
5040 | goto out; |
5041 | } | |
5042 | } | |
5043 | ||
5044 | if (base_fmt) { | |
5045 | if (set_option_parameter(param, BLOCK_OPT_BACKING_FMT, base_fmt)) { | |
71c79813 LC |
5046 | error_setg(errp, "Backing file format not supported for file " |
5047 | "format '%s'", fmt); | |
f88e1a42 JS |
5048 | goto out; |
5049 | } | |
5050 | } | |
5051 | ||
792da93a JS |
5052 | backing_file = get_option_parameter(param, BLOCK_OPT_BACKING_FILE); |
5053 | if (backing_file && backing_file->value.s) { | |
5054 | if (!strcmp(filename, backing_file->value.s)) { | |
71c79813 LC |
5055 | error_setg(errp, "Error: Trying to create an image with the " |
5056 | "same filename as the backing file"); | |
792da93a JS |
5057 | goto out; |
5058 | } | |
5059 | } | |
5060 | ||
f88e1a42 JS |
5061 | backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT); |
5062 | if (backing_fmt && backing_fmt->value.s) { | |
96df67d1 SH |
5063 | backing_drv = bdrv_find_format(backing_fmt->value.s); |
5064 | if (!backing_drv) { | |
71c79813 LC |
5065 | error_setg(errp, "Unknown backing file format '%s'", |
5066 | backing_fmt->value.s); | |
f88e1a42 JS |
5067 | goto out; |
5068 | } | |
5069 | } | |
5070 | ||
5071 | // The size for the image must always be specified, with one exception: | |
5072 | // If we are using a backing file, we can obtain the size from there | |
d220894e KW |
5073 | size = get_option_parameter(param, BLOCK_OPT_SIZE); |
5074 | if (size && size->value.n == -1) { | |
f88e1a42 | 5075 | if (backing_file && backing_file->value.s) { |
66f6b814 | 5076 | BlockDriverState *bs; |
f88e1a42 | 5077 | uint64_t size; |
f88e1a42 | 5078 | char buf[32]; |
63090dac PB |
5079 | int back_flags; |
5080 | ||
5081 | /* backing files always opened read-only */ | |
5082 | back_flags = | |
5083 | flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); | |
f88e1a42 | 5084 | |
f88e1a42 JS |
5085 | bs = bdrv_new(""); |
5086 | ||
de9c0cec | 5087 | ret = bdrv_open(bs, backing_file->value.s, NULL, back_flags, |
cc84d90f | 5088 | backing_drv, &local_err); |
f88e1a42 | 5089 | if (ret < 0) { |
cc84d90f HR |
5090 | error_setg_errno(errp, -ret, "Could not open '%s': %s", |
5091 | backing_file->value.s, | |
5092 | error_get_pretty(local_err)); | |
5093 | error_free(local_err); | |
5094 | local_err = NULL; | |
66f6b814 | 5095 | bdrv_unref(bs); |
f88e1a42 JS |
5096 | goto out; |
5097 | } | |
5098 | bdrv_get_geometry(bs, &size); | |
5099 | size *= 512; | |
5100 | ||
5101 | snprintf(buf, sizeof(buf), "%" PRId64, size); | |
5102 | set_option_parameter(param, BLOCK_OPT_SIZE, buf); | |
66f6b814 HR |
5103 | |
5104 | bdrv_unref(bs); | |
f88e1a42 | 5105 | } else { |
71c79813 | 5106 | error_setg(errp, "Image creation needs a size parameter"); |
f88e1a42 JS |
5107 | goto out; |
5108 | } | |
5109 | } | |
5110 | ||
f382d43a MR |
5111 | if (!quiet) { |
5112 | printf("Formatting '%s', fmt=%s ", filename, fmt); | |
5113 | print_option_parameters(param); | |
5114 | puts(""); | |
5115 | } | |
cc84d90f HR |
5116 | ret = bdrv_create(drv, filename, param, &local_err); |
5117 | if (ret == -EFBIG) { | |
5118 | /* This is generally a better message than whatever the driver would | |
5119 | * deliver (especially because of the cluster_size_hint), since that | |
5120 | * is most probably not much different from "image too large". */ | |
5121 | const char *cluster_size_hint = ""; | |
5122 | if (get_option_parameter(create_options, BLOCK_OPT_CLUSTER_SIZE)) { | |
5123 | cluster_size_hint = " (try using a larger cluster size)"; | |
f88e1a42 | 5124 | } |
cc84d90f HR |
5125 | error_setg(errp, "The image size is too large for file format '%s'" |
5126 | "%s", fmt, cluster_size_hint); | |
5127 | error_free(local_err); | |
5128 | local_err = NULL; | |
f88e1a42 JS |
5129 | } |
5130 | ||
5131 | out: | |
5132 | free_option_parameters(create_options); | |
5133 | free_option_parameters(param); | |
5134 | ||
cc84d90f HR |
5135 | if (error_is_set(&local_err)) { |
5136 | error_propagate(errp, local_err); | |
5137 | } | |
f88e1a42 | 5138 | } |
85d126f3 SH |
5139 | |
5140 | AioContext *bdrv_get_aio_context(BlockDriverState *bs) | |
5141 | { | |
5142 | /* Currently BlockDriverState always uses the main loop AioContext */ | |
5143 | return qemu_get_aio_context(); | |
5144 | } | |
d616b224 SH |
5145 | |
5146 | void bdrv_add_before_write_notifier(BlockDriverState *bs, | |
5147 | NotifierWithReturn *notifier) | |
5148 | { | |
5149 | notifier_with_return_list_add(&bs->before_write_notifiers, notifier); | |
5150 | } | |
6f176b48 HR |
5151 | |
5152 | int bdrv_amend_options(BlockDriverState *bs, QEMUOptionParameter *options) | |
5153 | { | |
5154 | if (bs->drv->bdrv_amend_options == NULL) { | |
5155 | return -ENOTSUP; | |
5156 | } | |
5157 | return bs->drv->bdrv_amend_options(bs, options); | |
5158 | } | |
f6186f49 | 5159 | |
212a5a8f BC |
5160 | /* Used to recurse on single child block filters. |
5161 | * Single child block filter will store their child in bs->file. | |
5162 | */ | |
5163 | bool bdrv_generic_is_first_non_filter(BlockDriverState *bs, | |
5164 | BlockDriverState *candidate) | |
f6186f49 | 5165 | { |
212a5a8f BC |
5166 | if (!bs->drv) { |
5167 | return false; | |
5168 | } | |
5169 | ||
5170 | if (!bs->drv->authorizations[BS_IS_A_FILTER]) { | |
5171 | if (bs == candidate) { | |
5172 | return true; | |
5173 | } else { | |
5174 | return false; | |
5175 | } | |
5176 | } | |
5177 | ||
5178 | if (!bs->drv->authorizations[BS_FILTER_PASS_DOWN]) { | |
5179 | return false; | |
f6186f49 BC |
5180 | } |
5181 | ||
212a5a8f BC |
5182 | if (!bs->file) { |
5183 | return false; | |
5184 | } | |
5185 | ||
5186 | return bdrv_recurse_is_first_non_filter(bs->file, candidate); | |
5187 | } | |
5188 | ||
5189 | bool bdrv_recurse_is_first_non_filter(BlockDriverState *bs, | |
5190 | BlockDriverState *candidate) | |
5191 | { | |
5192 | if (bs->drv && bs->drv->bdrv_recurse_is_first_non_filter) { | |
5193 | return bs->drv->bdrv_recurse_is_first_non_filter(bs, candidate); | |
f6186f49 BC |
5194 | } |
5195 | ||
212a5a8f | 5196 | return bdrv_generic_is_first_non_filter(bs, candidate); |
f6186f49 BC |
5197 | } |
5198 | ||
212a5a8f BC |
5199 | /* This function checks if the candidate is the first non filter bs down it's |
5200 | * bs chain. Since we don't have pointers to parents it explore all bs chains | |
5201 | * from the top. Some filters can choose not to pass down the recursion. | |
5202 | */ | |
5203 | bool bdrv_is_first_non_filter(BlockDriverState *candidate) | |
f6186f49 | 5204 | { |
212a5a8f BC |
5205 | BlockDriverState *bs; |
5206 | ||
5207 | /* walk down the bs forest recursively */ | |
5208 | QTAILQ_FOREACH(bs, &bdrv_states, device_list) { | |
5209 | bool perm; | |
5210 | ||
5211 | if (!bs->file) { | |
5212 | continue; | |
5213 | } | |
5214 | ||
5215 | perm = bdrv_recurse_is_first_non_filter(bs->file, candidate); | |
5216 | ||
5217 | /* candidate is the first non filter */ | |
5218 | if (perm) { | |
5219 | return true; | |
5220 | } | |
5221 | } | |
5222 | ||
5223 | return false; | |
f6186f49 | 5224 | } |