]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * QEMU System Emulator block driver | |
3 | * | |
4 | * Copyright (c) 2003 Fabrice Bellard | |
5 | * | |
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 | */ | |
24 | ||
25 | #include "qemu/osdep.h" | |
26 | #include "block/trace.h" | |
27 | #include "block/block_int.h" | |
28 | #include "block/blockjob.h" | |
29 | #include "block/nbd.h" | |
30 | #include "block/qdict.h" | |
31 | #include "qemu/error-report.h" | |
32 | #include "module_block.h" | |
33 | #include "qemu/module.h" | |
34 | #include "qapi/error.h" | |
35 | #include "qapi/qmp/qdict.h" | |
36 | #include "qapi/qmp/qjson.h" | |
37 | #include "qapi/qmp/qnull.h" | |
38 | #include "qapi/qmp/qstring.h" | |
39 | #include "qapi/qobject-output-visitor.h" | |
40 | #include "qapi/qapi-visit-block-core.h" | |
41 | #include "sysemu/block-backend.h" | |
42 | #include "sysemu/sysemu.h" | |
43 | #include "qemu/notify.h" | |
44 | #include "qemu/option.h" | |
45 | #include "qemu/coroutine.h" | |
46 | #include "block/qapi.h" | |
47 | #include "qemu/timer.h" | |
48 | #include "qemu/cutils.h" | |
49 | #include "qemu/id.h" | |
50 | ||
51 | #ifdef CONFIG_BSD | |
52 | #include <sys/ioctl.h> | |
53 | #include <sys/queue.h> | |
54 | #ifndef __DragonFly__ | |
55 | #include <sys/disk.h> | |
56 | #endif | |
57 | #endif | |
58 | ||
59 | #ifdef _WIN32 | |
60 | #include <windows.h> | |
61 | #endif | |
62 | ||
63 | #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */ | |
64 | ||
65 | static QTAILQ_HEAD(, BlockDriverState) graph_bdrv_states = | |
66 | QTAILQ_HEAD_INITIALIZER(graph_bdrv_states); | |
67 | ||
68 | static QTAILQ_HEAD(, BlockDriverState) all_bdrv_states = | |
69 | QTAILQ_HEAD_INITIALIZER(all_bdrv_states); | |
70 | ||
71 | static QLIST_HEAD(, BlockDriver) bdrv_drivers = | |
72 | QLIST_HEAD_INITIALIZER(bdrv_drivers); | |
73 | ||
74 | static BlockDriverState *bdrv_open_inherit(const char *filename, | |
75 | const char *reference, | |
76 | QDict *options, int flags, | |
77 | BlockDriverState *parent, | |
78 | const BdrvChildRole *child_role, | |
79 | Error **errp); | |
80 | ||
81 | /* If non-zero, use only whitelisted block drivers */ | |
82 | static int use_bdrv_whitelist; | |
83 | ||
84 | #ifdef _WIN32 | |
85 | static int is_windows_drive_prefix(const char *filename) | |
86 | { | |
87 | return (((filename[0] >= 'a' && filename[0] <= 'z') || | |
88 | (filename[0] >= 'A' && filename[0] <= 'Z')) && | |
89 | filename[1] == ':'); | |
90 | } | |
91 | ||
92 | int is_windows_drive(const char *filename) | |
93 | { | |
94 | if (is_windows_drive_prefix(filename) && | |
95 | filename[2] == '\0') | |
96 | return 1; | |
97 | if (strstart(filename, "\\\\.\\", NULL) || | |
98 | strstart(filename, "//./", NULL)) | |
99 | return 1; | |
100 | return 0; | |
101 | } | |
102 | #endif | |
103 | ||
104 | size_t bdrv_opt_mem_align(BlockDriverState *bs) | |
105 | { | |
106 | if (!bs || !bs->drv) { | |
107 | /* page size or 4k (hdd sector size) should be on the safe side */ | |
108 | return MAX(4096, getpagesize()); | |
109 | } | |
110 | ||
111 | return bs->bl.opt_mem_alignment; | |
112 | } | |
113 | ||
114 | size_t bdrv_min_mem_align(BlockDriverState *bs) | |
115 | { | |
116 | if (!bs || !bs->drv) { | |
117 | /* page size or 4k (hdd sector size) should be on the safe side */ | |
118 | return MAX(4096, getpagesize()); | |
119 | } | |
120 | ||
121 | return bs->bl.min_mem_alignment; | |
122 | } | |
123 | ||
124 | /* check if the path starts with "<protocol>:" */ | |
125 | int path_has_protocol(const char *path) | |
126 | { | |
127 | const char *p; | |
128 | ||
129 | #ifdef _WIN32 | |
130 | if (is_windows_drive(path) || | |
131 | is_windows_drive_prefix(path)) { | |
132 | return 0; | |
133 | } | |
134 | p = path + strcspn(path, ":/\\"); | |
135 | #else | |
136 | p = path + strcspn(path, ":/"); | |
137 | #endif | |
138 | ||
139 | return *p == ':'; | |
140 | } | |
141 | ||
142 | int path_is_absolute(const char *path) | |
143 | { | |
144 | #ifdef _WIN32 | |
145 | /* specific case for names like: "\\.\d:" */ | |
146 | if (is_windows_drive(path) || is_windows_drive_prefix(path)) { | |
147 | return 1; | |
148 | } | |
149 | return (*path == '/' || *path == '\\'); | |
150 | #else | |
151 | return (*path == '/'); | |
152 | #endif | |
153 | } | |
154 | ||
155 | /* if filename is absolute, just copy it to dest. Otherwise, build a | |
156 | path to it by considering it is relative to base_path. URL are | |
157 | supported. */ | |
158 | void path_combine(char *dest, int dest_size, | |
159 | const char *base_path, | |
160 | const char *filename) | |
161 | { | |
162 | const char *p, *p1; | |
163 | int len; | |
164 | ||
165 | if (dest_size <= 0) | |
166 | return; | |
167 | if (path_is_absolute(filename)) { | |
168 | pstrcpy(dest, dest_size, filename); | |
169 | } else { | |
170 | const char *protocol_stripped = NULL; | |
171 | ||
172 | if (path_has_protocol(base_path)) { | |
173 | protocol_stripped = strchr(base_path, ':'); | |
174 | if (protocol_stripped) { | |
175 | protocol_stripped++; | |
176 | } | |
177 | } | |
178 | p = protocol_stripped ?: base_path; | |
179 | ||
180 | p1 = strrchr(base_path, '/'); | |
181 | #ifdef _WIN32 | |
182 | { | |
183 | const char *p2; | |
184 | p2 = strrchr(base_path, '\\'); | |
185 | if (!p1 || p2 > p1) | |
186 | p1 = p2; | |
187 | } | |
188 | #endif | |
189 | if (p1) | |
190 | p1++; | |
191 | else | |
192 | p1 = base_path; | |
193 | if (p1 > p) | |
194 | p = p1; | |
195 | len = p - base_path; | |
196 | if (len > dest_size - 1) | |
197 | len = dest_size - 1; | |
198 | memcpy(dest, base_path, len); | |
199 | dest[len] = '\0'; | |
200 | pstrcat(dest, dest_size, filename); | |
201 | } | |
202 | } | |
203 | ||
204 | /* | |
205 | * Helper function for bdrv_parse_filename() implementations to remove optional | |
206 | * protocol prefixes (especially "file:") from a filename and for putting the | |
207 | * stripped filename into the options QDict if there is such a prefix. | |
208 | */ | |
209 | void bdrv_parse_filename_strip_prefix(const char *filename, const char *prefix, | |
210 | QDict *options) | |
211 | { | |
212 | if (strstart(filename, prefix, &filename)) { | |
213 | /* Stripping the explicit protocol prefix may result in a protocol | |
214 | * prefix being (wrongly) detected (if the filename contains a colon) */ | |
215 | if (path_has_protocol(filename)) { | |
216 | QString *fat_filename; | |
217 | ||
218 | /* This means there is some colon before the first slash; therefore, | |
219 | * this cannot be an absolute path */ | |
220 | assert(!path_is_absolute(filename)); | |
221 | ||
222 | /* And we can thus fix the protocol detection issue by prefixing it | |
223 | * by "./" */ | |
224 | fat_filename = qstring_from_str("./"); | |
225 | qstring_append(fat_filename, filename); | |
226 | ||
227 | assert(!path_has_protocol(qstring_get_str(fat_filename))); | |
228 | ||
229 | qdict_put(options, "filename", fat_filename); | |
230 | } else { | |
231 | /* If no protocol prefix was detected, we can use the shortened | |
232 | * filename as-is */ | |
233 | qdict_put_str(options, "filename", filename); | |
234 | } | |
235 | } | |
236 | } | |
237 | ||
238 | ||
239 | /* Returns whether the image file is opened as read-only. Note that this can | |
240 | * return false and writing to the image file is still not possible because the | |
241 | * image is inactivated. */ | |
242 | bool bdrv_is_read_only(BlockDriverState *bs) | |
243 | { | |
244 | return bs->read_only; | |
245 | } | |
246 | ||
247 | int bdrv_can_set_read_only(BlockDriverState *bs, bool read_only, | |
248 | bool ignore_allow_rdw, Error **errp) | |
249 | { | |
250 | /* Do not set read_only if copy_on_read is enabled */ | |
251 | if (bs->copy_on_read && read_only) { | |
252 | error_setg(errp, "Can't set node '%s' to r/o with copy-on-read enabled", | |
253 | bdrv_get_device_or_node_name(bs)); | |
254 | return -EINVAL; | |
255 | } | |
256 | ||
257 | /* Do not clear read_only if it is prohibited */ | |
258 | if (!read_only && !(bs->open_flags & BDRV_O_ALLOW_RDWR) && | |
259 | !ignore_allow_rdw) | |
260 | { | |
261 | error_setg(errp, "Node '%s' is read only", | |
262 | bdrv_get_device_or_node_name(bs)); | |
263 | return -EPERM; | |
264 | } | |
265 | ||
266 | return 0; | |
267 | } | |
268 | ||
269 | /* TODO Remove (deprecated since 2.11) | |
270 | * Block drivers are not supposed to automatically change bs->read_only. | |
271 | * Instead, they should just check whether they can provide what the user | |
272 | * explicitly requested and error out if read-write is requested, but they can | |
273 | * only provide read-only access. */ | |
274 | int bdrv_set_read_only(BlockDriverState *bs, bool read_only, Error **errp) | |
275 | { | |
276 | int ret = 0; | |
277 | ||
278 | ret = bdrv_can_set_read_only(bs, read_only, false, errp); | |
279 | if (ret < 0) { | |
280 | return ret; | |
281 | } | |
282 | ||
283 | bs->read_only = read_only; | |
284 | ||
285 | if (read_only) { | |
286 | bs->open_flags &= ~BDRV_O_RDWR; | |
287 | } else { | |
288 | bs->open_flags |= BDRV_O_RDWR; | |
289 | } | |
290 | ||
291 | return 0; | |
292 | } | |
293 | ||
294 | void bdrv_get_full_backing_filename_from_filename(const char *backed, | |
295 | const char *backing, | |
296 | char *dest, size_t sz, | |
297 | Error **errp) | |
298 | { | |
299 | if (backing[0] == '\0' || path_has_protocol(backing) || | |
300 | path_is_absolute(backing)) | |
301 | { | |
302 | pstrcpy(dest, sz, backing); | |
303 | } else if (backed[0] == '\0' || strstart(backed, "json:", NULL)) { | |
304 | error_setg(errp, "Cannot use relative backing file names for '%s'", | |
305 | backed); | |
306 | } else { | |
307 | path_combine(dest, sz, backed, backing); | |
308 | } | |
309 | } | |
310 | ||
311 | void bdrv_get_full_backing_filename(BlockDriverState *bs, char *dest, size_t sz, | |
312 | Error **errp) | |
313 | { | |
314 | char *backed = bs->exact_filename[0] ? bs->exact_filename : bs->filename; | |
315 | ||
316 | bdrv_get_full_backing_filename_from_filename(backed, bs->backing_file, | |
317 | dest, sz, errp); | |
318 | } | |
319 | ||
320 | void bdrv_register(BlockDriver *bdrv) | |
321 | { | |
322 | QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list); | |
323 | } | |
324 | ||
325 | BlockDriverState *bdrv_new(void) | |
326 | { | |
327 | BlockDriverState *bs; | |
328 | int i; | |
329 | ||
330 | bs = g_new0(BlockDriverState, 1); | |
331 | QLIST_INIT(&bs->dirty_bitmaps); | |
332 | for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) { | |
333 | QLIST_INIT(&bs->op_blockers[i]); | |
334 | } | |
335 | notifier_with_return_list_init(&bs->before_write_notifiers); | |
336 | qemu_co_mutex_init(&bs->reqs_lock); | |
337 | qemu_mutex_init(&bs->dirty_bitmap_mutex); | |
338 | bs->refcnt = 1; | |
339 | bs->aio_context = qemu_get_aio_context(); | |
340 | ||
341 | qemu_co_queue_init(&bs->flush_queue); | |
342 | ||
343 | for (i = 0; i < bdrv_drain_all_count; i++) { | |
344 | bdrv_drained_begin(bs); | |
345 | } | |
346 | ||
347 | QTAILQ_INSERT_TAIL(&all_bdrv_states, bs, bs_list); | |
348 | ||
349 | return bs; | |
350 | } | |
351 | ||
352 | static BlockDriver *bdrv_do_find_format(const char *format_name) | |
353 | { | |
354 | BlockDriver *drv1; | |
355 | ||
356 | QLIST_FOREACH(drv1, &bdrv_drivers, list) { | |
357 | if (!strcmp(drv1->format_name, format_name)) { | |
358 | return drv1; | |
359 | } | |
360 | } | |
361 | ||
362 | return NULL; | |
363 | } | |
364 | ||
365 | BlockDriver *bdrv_find_format(const char *format_name) | |
366 | { | |
367 | BlockDriver *drv1; | |
368 | int i; | |
369 | ||
370 | drv1 = bdrv_do_find_format(format_name); | |
371 | if (drv1) { | |
372 | return drv1; | |
373 | } | |
374 | ||
375 | /* The driver isn't registered, maybe we need to load a module */ | |
376 | for (i = 0; i < (int)ARRAY_SIZE(block_driver_modules); ++i) { | |
377 | if (!strcmp(block_driver_modules[i].format_name, format_name)) { | |
378 | block_module_load_one(block_driver_modules[i].library_name); | |
379 | break; | |
380 | } | |
381 | } | |
382 | ||
383 | return bdrv_do_find_format(format_name); | |
384 | } | |
385 | ||
386 | int bdrv_is_whitelisted(BlockDriver *drv, bool read_only) | |
387 | { | |
388 | static const char *whitelist_rw[] = { | |
389 | CONFIG_BDRV_RW_WHITELIST | |
390 | }; | |
391 | static const char *whitelist_ro[] = { | |
392 | CONFIG_BDRV_RO_WHITELIST | |
393 | }; | |
394 | const char **p; | |
395 | ||
396 | if (!whitelist_rw[0] && !whitelist_ro[0]) { | |
397 | return 1; /* no whitelist, anything goes */ | |
398 | } | |
399 | ||
400 | for (p = whitelist_rw; *p; p++) { | |
401 | if (!strcmp(drv->format_name, *p)) { | |
402 | return 1; | |
403 | } | |
404 | } | |
405 | if (read_only) { | |
406 | for (p = whitelist_ro; *p; p++) { | |
407 | if (!strcmp(drv->format_name, *p)) { | |
408 | return 1; | |
409 | } | |
410 | } | |
411 | } | |
412 | return 0; | |
413 | } | |
414 | ||
415 | bool bdrv_uses_whitelist(void) | |
416 | { | |
417 | return use_bdrv_whitelist; | |
418 | } | |
419 | ||
420 | typedef struct CreateCo { | |
421 | BlockDriver *drv; | |
422 | char *filename; | |
423 | QemuOpts *opts; | |
424 | int ret; | |
425 | Error *err; | |
426 | } CreateCo; | |
427 | ||
428 | static void coroutine_fn bdrv_create_co_entry(void *opaque) | |
429 | { | |
430 | Error *local_err = NULL; | |
431 | int ret; | |
432 | ||
433 | CreateCo *cco = opaque; | |
434 | assert(cco->drv); | |
435 | ||
436 | ret = cco->drv->bdrv_co_create_opts(cco->filename, cco->opts, &local_err); | |
437 | error_propagate(&cco->err, local_err); | |
438 | cco->ret = ret; | |
439 | } | |
440 | ||
441 | int bdrv_create(BlockDriver *drv, const char* filename, | |
442 | QemuOpts *opts, Error **errp) | |
443 | { | |
444 | int ret; | |
445 | ||
446 | Coroutine *co; | |
447 | CreateCo cco = { | |
448 | .drv = drv, | |
449 | .filename = g_strdup(filename), | |
450 | .opts = opts, | |
451 | .ret = NOT_DONE, | |
452 | .err = NULL, | |
453 | }; | |
454 | ||
455 | if (!drv->bdrv_co_create_opts) { | |
456 | error_setg(errp, "Driver '%s' does not support image creation", drv->format_name); | |
457 | ret = -ENOTSUP; | |
458 | goto out; | |
459 | } | |
460 | ||
461 | if (qemu_in_coroutine()) { | |
462 | /* Fast-path if already in coroutine context */ | |
463 | bdrv_create_co_entry(&cco); | |
464 | } else { | |
465 | co = qemu_coroutine_create(bdrv_create_co_entry, &cco); | |
466 | qemu_coroutine_enter(co); | |
467 | while (cco.ret == NOT_DONE) { | |
468 | aio_poll(qemu_get_aio_context(), true); | |
469 | } | |
470 | } | |
471 | ||
472 | ret = cco.ret; | |
473 | if (ret < 0) { | |
474 | if (cco.err) { | |
475 | error_propagate(errp, cco.err); | |
476 | } else { | |
477 | error_setg_errno(errp, -ret, "Could not create image"); | |
478 | } | |
479 | } | |
480 | ||
481 | out: | |
482 | g_free(cco.filename); | |
483 | return ret; | |
484 | } | |
485 | ||
486 | int bdrv_create_file(const char *filename, QemuOpts *opts, Error **errp) | |
487 | { | |
488 | BlockDriver *drv; | |
489 | Error *local_err = NULL; | |
490 | int ret; | |
491 | ||
492 | drv = bdrv_find_protocol(filename, true, errp); | |
493 | if (drv == NULL) { | |
494 | return -ENOENT; | |
495 | } | |
496 | ||
497 | ret = bdrv_create(drv, filename, opts, &local_err); | |
498 | error_propagate(errp, local_err); | |
499 | return ret; | |
500 | } | |
501 | ||
502 | /** | |
503 | * Try to get @bs's logical and physical block size. | |
504 | * On success, store them in @bsz struct and return 0. | |
505 | * On failure return -errno. | |
506 | * @bs must not be empty. | |
507 | */ | |
508 | int bdrv_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz) | |
509 | { | |
510 | BlockDriver *drv = bs->drv; | |
511 | ||
512 | if (drv && drv->bdrv_probe_blocksizes) { | |
513 | return drv->bdrv_probe_blocksizes(bs, bsz); | |
514 | } else if (drv && drv->is_filter && bs->file) { | |
515 | return bdrv_probe_blocksizes(bs->file->bs, bsz); | |
516 | } | |
517 | ||
518 | return -ENOTSUP; | |
519 | } | |
520 | ||
521 | /** | |
522 | * Try to get @bs's geometry (cyls, heads, sectors). | |
523 | * On success, store them in @geo struct and return 0. | |
524 | * On failure return -errno. | |
525 | * @bs must not be empty. | |
526 | */ | |
527 | int bdrv_probe_geometry(BlockDriverState *bs, HDGeometry *geo) | |
528 | { | |
529 | BlockDriver *drv = bs->drv; | |
530 | ||
531 | if (drv && drv->bdrv_probe_geometry) { | |
532 | return drv->bdrv_probe_geometry(bs, geo); | |
533 | } else if (drv && drv->is_filter && bs->file) { | |
534 | return bdrv_probe_geometry(bs->file->bs, geo); | |
535 | } | |
536 | ||
537 | return -ENOTSUP; | |
538 | } | |
539 | ||
540 | /* | |
541 | * Create a uniquely-named empty temporary file. | |
542 | * Return 0 upon success, otherwise a negative errno value. | |
543 | */ | |
544 | int get_tmp_filename(char *filename, int size) | |
545 | { | |
546 | #ifdef _WIN32 | |
547 | char temp_dir[MAX_PATH]; | |
548 | /* GetTempFileName requires that its output buffer (4th param) | |
549 | have length MAX_PATH or greater. */ | |
550 | assert(size >= MAX_PATH); | |
551 | return (GetTempPath(MAX_PATH, temp_dir) | |
552 | && GetTempFileName(temp_dir, "qem", 0, filename) | |
553 | ? 0 : -GetLastError()); | |
554 | #else | |
555 | int fd; | |
556 | const char *tmpdir; | |
557 | tmpdir = getenv("TMPDIR"); | |
558 | if (!tmpdir) { | |
559 | tmpdir = "/var/tmp"; | |
560 | } | |
561 | if (snprintf(filename, size, "%s/vl.XXXXXX", tmpdir) >= size) { | |
562 | return -EOVERFLOW; | |
563 | } | |
564 | fd = mkstemp(filename); | |
565 | if (fd < 0) { | |
566 | return -errno; | |
567 | } | |
568 | if (close(fd) != 0) { | |
569 | unlink(filename); | |
570 | return -errno; | |
571 | } | |
572 | return 0; | |
573 | #endif | |
574 | } | |
575 | ||
576 | /* | |
577 | * Detect host devices. By convention, /dev/cdrom[N] is always | |
578 | * recognized as a host CDROM. | |
579 | */ | |
580 | static BlockDriver *find_hdev_driver(const char *filename) | |
581 | { | |
582 | int score_max = 0, score; | |
583 | BlockDriver *drv = NULL, *d; | |
584 | ||
585 | QLIST_FOREACH(d, &bdrv_drivers, list) { | |
586 | if (d->bdrv_probe_device) { | |
587 | score = d->bdrv_probe_device(filename); | |
588 | if (score > score_max) { | |
589 | score_max = score; | |
590 | drv = d; | |
591 | } | |
592 | } | |
593 | } | |
594 | ||
595 | return drv; | |
596 | } | |
597 | ||
598 | static BlockDriver *bdrv_do_find_protocol(const char *protocol) | |
599 | { | |
600 | BlockDriver *drv1; | |
601 | ||
602 | QLIST_FOREACH(drv1, &bdrv_drivers, list) { | |
603 | if (drv1->protocol_name && !strcmp(drv1->protocol_name, protocol)) { | |
604 | return drv1; | |
605 | } | |
606 | } | |
607 | ||
608 | return NULL; | |
609 | } | |
610 | ||
611 | BlockDriver *bdrv_find_protocol(const char *filename, | |
612 | bool allow_protocol_prefix, | |
613 | Error **errp) | |
614 | { | |
615 | BlockDriver *drv1; | |
616 | char protocol[128]; | |
617 | int len; | |
618 | const char *p; | |
619 | int i; | |
620 | ||
621 | /* TODO Drivers without bdrv_file_open must be specified explicitly */ | |
622 | ||
623 | /* | |
624 | * XXX(hch): we really should not let host device detection | |
625 | * override an explicit protocol specification, but moving this | |
626 | * later breaks access to device names with colons in them. | |
627 | * Thanks to the brain-dead persistent naming schemes on udev- | |
628 | * based Linux systems those actually are quite common. | |
629 | */ | |
630 | drv1 = find_hdev_driver(filename); | |
631 | if (drv1) { | |
632 | return drv1; | |
633 | } | |
634 | ||
635 | if (!path_has_protocol(filename) || !allow_protocol_prefix) { | |
636 | return &bdrv_file; | |
637 | } | |
638 | ||
639 | p = strchr(filename, ':'); | |
640 | assert(p != NULL); | |
641 | len = p - filename; | |
642 | if (len > sizeof(protocol) - 1) | |
643 | len = sizeof(protocol) - 1; | |
644 | memcpy(protocol, filename, len); | |
645 | protocol[len] = '\0'; | |
646 | ||
647 | drv1 = bdrv_do_find_protocol(protocol); | |
648 | if (drv1) { | |
649 | return drv1; | |
650 | } | |
651 | ||
652 | for (i = 0; i < (int)ARRAY_SIZE(block_driver_modules); ++i) { | |
653 | if (block_driver_modules[i].protocol_name && | |
654 | !strcmp(block_driver_modules[i].protocol_name, protocol)) { | |
655 | block_module_load_one(block_driver_modules[i].library_name); | |
656 | break; | |
657 | } | |
658 | } | |
659 | ||
660 | drv1 = bdrv_do_find_protocol(protocol); | |
661 | if (!drv1) { | |
662 | error_setg(errp, "Unknown protocol '%s'", protocol); | |
663 | } | |
664 | return drv1; | |
665 | } | |
666 | ||
667 | /* | |
668 | * Guess image format by probing its contents. | |
669 | * This is not a good idea when your image is raw (CVE-2008-2004), but | |
670 | * we do it anyway for backward compatibility. | |
671 | * | |
672 | * @buf contains the image's first @buf_size bytes. | |
673 | * @buf_size is the buffer size in bytes (generally BLOCK_PROBE_BUF_SIZE, | |
674 | * but can be smaller if the image file is smaller) | |
675 | * @filename is its filename. | |
676 | * | |
677 | * For all block drivers, call the bdrv_probe() method to get its | |
678 | * probing score. | |
679 | * Return the first block driver with the highest probing score. | |
680 | */ | |
681 | BlockDriver *bdrv_probe_all(const uint8_t *buf, int buf_size, | |
682 | const char *filename) | |
683 | { | |
684 | int score_max = 0, score; | |
685 | BlockDriver *drv = NULL, *d; | |
686 | ||
687 | QLIST_FOREACH(d, &bdrv_drivers, list) { | |
688 | if (d->bdrv_probe) { | |
689 | score = d->bdrv_probe(buf, buf_size, filename); | |
690 | if (score > score_max) { | |
691 | score_max = score; | |
692 | drv = d; | |
693 | } | |
694 | } | |
695 | } | |
696 | ||
697 | return drv; | |
698 | } | |
699 | ||
700 | static int find_image_format(BlockBackend *file, const char *filename, | |
701 | BlockDriver **pdrv, Error **errp) | |
702 | { | |
703 | BlockDriver *drv; | |
704 | uint8_t buf[BLOCK_PROBE_BUF_SIZE]; | |
705 | int ret = 0; | |
706 | ||
707 | /* Return the raw BlockDriver * to scsi-generic devices or empty drives */ | |
708 | if (blk_is_sg(file) || !blk_is_inserted(file) || blk_getlength(file) == 0) { | |
709 | *pdrv = &bdrv_raw; | |
710 | return ret; | |
711 | } | |
712 | ||
713 | ret = blk_pread(file, 0, buf, sizeof(buf)); | |
714 | if (ret < 0) { | |
715 | error_setg_errno(errp, -ret, "Could not read image for determining its " | |
716 | "format"); | |
717 | *pdrv = NULL; | |
718 | return ret; | |
719 | } | |
720 | ||
721 | drv = bdrv_probe_all(buf, ret, filename); | |
722 | if (!drv) { | |
723 | error_setg(errp, "Could not determine image format: No compatible " | |
724 | "driver found"); | |
725 | ret = -ENOENT; | |
726 | } | |
727 | *pdrv = drv; | |
728 | return ret; | |
729 | } | |
730 | ||
731 | /** | |
732 | * Set the current 'total_sectors' value | |
733 | * Return 0 on success, -errno on error. | |
734 | */ | |
735 | int refresh_total_sectors(BlockDriverState *bs, int64_t hint) | |
736 | { | |
737 | BlockDriver *drv = bs->drv; | |
738 | ||
739 | if (!drv) { | |
740 | return -ENOMEDIUM; | |
741 | } | |
742 | ||
743 | /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */ | |
744 | if (bdrv_is_sg(bs)) | |
745 | return 0; | |
746 | ||
747 | /* query actual device if possible, otherwise just trust the hint */ | |
748 | if (drv->bdrv_getlength) { | |
749 | int64_t length = drv->bdrv_getlength(bs); | |
750 | if (length < 0) { | |
751 | return length; | |
752 | } | |
753 | hint = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE); | |
754 | } | |
755 | ||
756 | bs->total_sectors = hint; | |
757 | return 0; | |
758 | } | |
759 | ||
760 | /** | |
761 | * Combines a QDict of new block driver @options with any missing options taken | |
762 | * from @old_options, so that leaving out an option defaults to its old value. | |
763 | */ | |
764 | static void bdrv_join_options(BlockDriverState *bs, QDict *options, | |
765 | QDict *old_options) | |
766 | { | |
767 | if (bs->drv && bs->drv->bdrv_join_options) { | |
768 | bs->drv->bdrv_join_options(options, old_options); | |
769 | } else { | |
770 | qdict_join(options, old_options, false); | |
771 | } | |
772 | } | |
773 | ||
774 | static BlockdevDetectZeroesOptions bdrv_parse_detect_zeroes(QemuOpts *opts, | |
775 | int open_flags, | |
776 | Error **errp) | |
777 | { | |
778 | Error *local_err = NULL; | |
779 | char *value = qemu_opt_get_del(opts, "detect-zeroes"); | |
780 | BlockdevDetectZeroesOptions detect_zeroes = | |
781 | qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup, value, | |
782 | BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF, &local_err); | |
783 | g_free(value); | |
784 | if (local_err) { | |
785 | error_propagate(errp, local_err); | |
786 | return detect_zeroes; | |
787 | } | |
788 | ||
789 | if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP && | |
790 | !(open_flags & BDRV_O_UNMAP)) | |
791 | { | |
792 | error_setg(errp, "setting detect-zeroes to unmap is not allowed " | |
793 | "without setting discard operation to unmap"); | |
794 | } | |
795 | ||
796 | return detect_zeroes; | |
797 | } | |
798 | ||
799 | /** | |
800 | * Set open flags for a given discard mode | |
801 | * | |
802 | * Return 0 on success, -1 if the discard mode was invalid. | |
803 | */ | |
804 | int bdrv_parse_discard_flags(const char *mode, int *flags) | |
805 | { | |
806 | *flags &= ~BDRV_O_UNMAP; | |
807 | ||
808 | if (!strcmp(mode, "off") || !strcmp(mode, "ignore")) { | |
809 | /* do nothing */ | |
810 | } else if (!strcmp(mode, "on") || !strcmp(mode, "unmap")) { | |
811 | *flags |= BDRV_O_UNMAP; | |
812 | } else { | |
813 | return -1; | |
814 | } | |
815 | ||
816 | return 0; | |
817 | } | |
818 | ||
819 | /** | |
820 | * Set open flags for a given cache mode | |
821 | * | |
822 | * Return 0 on success, -1 if the cache mode was invalid. | |
823 | */ | |
824 | int bdrv_parse_cache_mode(const char *mode, int *flags, bool *writethrough) | |
825 | { | |
826 | *flags &= ~BDRV_O_CACHE_MASK; | |
827 | ||
828 | if (!strcmp(mode, "off") || !strcmp(mode, "none")) { | |
829 | *writethrough = false; | |
830 | *flags |= BDRV_O_NOCACHE; | |
831 | } else if (!strcmp(mode, "directsync")) { | |
832 | *writethrough = true; | |
833 | *flags |= BDRV_O_NOCACHE; | |
834 | } else if (!strcmp(mode, "writeback")) { | |
835 | *writethrough = false; | |
836 | } else if (!strcmp(mode, "unsafe")) { | |
837 | *writethrough = false; | |
838 | *flags |= BDRV_O_NO_FLUSH; | |
839 | } else if (!strcmp(mode, "writethrough")) { | |
840 | *writethrough = true; | |
841 | } else { | |
842 | return -1; | |
843 | } | |
844 | ||
845 | return 0; | |
846 | } | |
847 | ||
848 | static char *bdrv_child_get_parent_desc(BdrvChild *c) | |
849 | { | |
850 | BlockDriverState *parent = c->opaque; | |
851 | return g_strdup(bdrv_get_device_or_node_name(parent)); | |
852 | } | |
853 | ||
854 | static void bdrv_child_cb_drained_begin(BdrvChild *child) | |
855 | { | |
856 | BlockDriverState *bs = child->opaque; | |
857 | bdrv_do_drained_begin_quiesce(bs, NULL, false); | |
858 | } | |
859 | ||
860 | static bool bdrv_child_cb_drained_poll(BdrvChild *child) | |
861 | { | |
862 | BlockDriverState *bs = child->opaque; | |
863 | return bdrv_drain_poll(bs, false, NULL, false); | |
864 | } | |
865 | ||
866 | static void bdrv_child_cb_drained_end(BdrvChild *child) | |
867 | { | |
868 | BlockDriverState *bs = child->opaque; | |
869 | bdrv_drained_end(bs); | |
870 | } | |
871 | ||
872 | static void bdrv_child_cb_attach(BdrvChild *child) | |
873 | { | |
874 | BlockDriverState *bs = child->opaque; | |
875 | bdrv_apply_subtree_drain(child, bs); | |
876 | } | |
877 | ||
878 | static void bdrv_child_cb_detach(BdrvChild *child) | |
879 | { | |
880 | BlockDriverState *bs = child->opaque; | |
881 | bdrv_unapply_subtree_drain(child, bs); | |
882 | } | |
883 | ||
884 | static int bdrv_child_cb_inactivate(BdrvChild *child) | |
885 | { | |
886 | BlockDriverState *bs = child->opaque; | |
887 | assert(bs->open_flags & BDRV_O_INACTIVE); | |
888 | return 0; | |
889 | } | |
890 | ||
891 | /* | |
892 | * Returns the options and flags that a temporary snapshot should get, based on | |
893 | * the originally requested flags (the originally requested image will have | |
894 | * flags like a backing file) | |
895 | */ | |
896 | static void bdrv_temp_snapshot_options(int *child_flags, QDict *child_options, | |
897 | int parent_flags, QDict *parent_options) | |
898 | { | |
899 | *child_flags = (parent_flags & ~BDRV_O_SNAPSHOT) | BDRV_O_TEMPORARY; | |
900 | ||
901 | /* For temporary files, unconditional cache=unsafe is fine */ | |
902 | qdict_set_default_str(child_options, BDRV_OPT_CACHE_DIRECT, "off"); | |
903 | qdict_set_default_str(child_options, BDRV_OPT_CACHE_NO_FLUSH, "on"); | |
904 | ||
905 | /* Copy the read-only option from the parent */ | |
906 | qdict_copy_default(child_options, parent_options, BDRV_OPT_READ_ONLY); | |
907 | ||
908 | /* aio=native doesn't work for cache.direct=off, so disable it for the | |
909 | * temporary snapshot */ | |
910 | *child_flags &= ~BDRV_O_NATIVE_AIO; | |
911 | } | |
912 | ||
913 | /* | |
914 | * Returns the options and flags that bs->file should get if a protocol driver | |
915 | * is expected, based on the given options and flags for the parent BDS | |
916 | */ | |
917 | static void bdrv_inherited_options(int *child_flags, QDict *child_options, | |
918 | int parent_flags, QDict *parent_options) | |
919 | { | |
920 | int flags = parent_flags; | |
921 | ||
922 | /* Enable protocol handling, disable format probing for bs->file */ | |
923 | flags |= BDRV_O_PROTOCOL; | |
924 | ||
925 | /* If the cache mode isn't explicitly set, inherit direct and no-flush from | |
926 | * the parent. */ | |
927 | qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_DIRECT); | |
928 | qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_NO_FLUSH); | |
929 | qdict_copy_default(child_options, parent_options, BDRV_OPT_FORCE_SHARE); | |
930 | ||
931 | /* Inherit the read-only option from the parent if it's not set */ | |
932 | qdict_copy_default(child_options, parent_options, BDRV_OPT_READ_ONLY); | |
933 | qdict_copy_default(child_options, parent_options, BDRV_OPT_AUTO_READ_ONLY); | |
934 | ||
935 | /* Our block drivers take care to send flushes and respect unmap policy, | |
936 | * so we can default to enable both on lower layers regardless of the | |
937 | * corresponding parent options. */ | |
938 | qdict_set_default_str(child_options, BDRV_OPT_DISCARD, "unmap"); | |
939 | ||
940 | /* Clear flags that only apply to the top layer */ | |
941 | flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_COPY_ON_READ | | |
942 | BDRV_O_NO_IO); | |
943 | ||
944 | *child_flags = flags; | |
945 | } | |
946 | ||
947 | const BdrvChildRole child_file = { | |
948 | .parent_is_bds = true, | |
949 | .get_parent_desc = bdrv_child_get_parent_desc, | |
950 | .inherit_options = bdrv_inherited_options, | |
951 | .drained_begin = bdrv_child_cb_drained_begin, | |
952 | .drained_poll = bdrv_child_cb_drained_poll, | |
953 | .drained_end = bdrv_child_cb_drained_end, | |
954 | .attach = bdrv_child_cb_attach, | |
955 | .detach = bdrv_child_cb_detach, | |
956 | .inactivate = bdrv_child_cb_inactivate, | |
957 | }; | |
958 | ||
959 | /* | |
960 | * Returns the options and flags that bs->file should get if the use of formats | |
961 | * (and not only protocols) is permitted for it, based on the given options and | |
962 | * flags for the parent BDS | |
963 | */ | |
964 | static void bdrv_inherited_fmt_options(int *child_flags, QDict *child_options, | |
965 | int parent_flags, QDict *parent_options) | |
966 | { | |
967 | child_file.inherit_options(child_flags, child_options, | |
968 | parent_flags, parent_options); | |
969 | ||
970 | *child_flags &= ~(BDRV_O_PROTOCOL | BDRV_O_NO_IO); | |
971 | } | |
972 | ||
973 | const BdrvChildRole child_format = { | |
974 | .parent_is_bds = true, | |
975 | .get_parent_desc = bdrv_child_get_parent_desc, | |
976 | .inherit_options = bdrv_inherited_fmt_options, | |
977 | .drained_begin = bdrv_child_cb_drained_begin, | |
978 | .drained_poll = bdrv_child_cb_drained_poll, | |
979 | .drained_end = bdrv_child_cb_drained_end, | |
980 | .attach = bdrv_child_cb_attach, | |
981 | .detach = bdrv_child_cb_detach, | |
982 | .inactivate = bdrv_child_cb_inactivate, | |
983 | }; | |
984 | ||
985 | static void bdrv_backing_attach(BdrvChild *c) | |
986 | { | |
987 | BlockDriverState *parent = c->opaque; | |
988 | BlockDriverState *backing_hd = c->bs; | |
989 | ||
990 | assert(!parent->backing_blocker); | |
991 | error_setg(&parent->backing_blocker, | |
992 | "node is used as backing hd of '%s'", | |
993 | bdrv_get_device_or_node_name(parent)); | |
994 | ||
995 | parent->open_flags &= ~BDRV_O_NO_BACKING; | |
996 | pstrcpy(parent->backing_file, sizeof(parent->backing_file), | |
997 | backing_hd->filename); | |
998 | pstrcpy(parent->backing_format, sizeof(parent->backing_format), | |
999 | backing_hd->drv ? backing_hd->drv->format_name : ""); | |
1000 | ||
1001 | bdrv_op_block_all(backing_hd, parent->backing_blocker); | |
1002 | /* Otherwise we won't be able to commit or stream */ | |
1003 | bdrv_op_unblock(backing_hd, BLOCK_OP_TYPE_COMMIT_TARGET, | |
1004 | parent->backing_blocker); | |
1005 | bdrv_op_unblock(backing_hd, BLOCK_OP_TYPE_STREAM, | |
1006 | parent->backing_blocker); | |
1007 | /* | |
1008 | * We do backup in 3 ways: | |
1009 | * 1. drive backup | |
1010 | * The target bs is new opened, and the source is top BDS | |
1011 | * 2. blockdev backup | |
1012 | * Both the source and the target are top BDSes. | |
1013 | * 3. internal backup(used for block replication) | |
1014 | * Both the source and the target are backing file | |
1015 | * | |
1016 | * In case 1 and 2, neither the source nor the target is the backing file. | |
1017 | * In case 3, we will block the top BDS, so there is only one block job | |
1018 | * for the top BDS and its backing chain. | |
1019 | */ | |
1020 | bdrv_op_unblock(backing_hd, BLOCK_OP_TYPE_BACKUP_SOURCE, | |
1021 | parent->backing_blocker); | |
1022 | bdrv_op_unblock(backing_hd, BLOCK_OP_TYPE_BACKUP_TARGET, | |
1023 | parent->backing_blocker); | |
1024 | ||
1025 | bdrv_child_cb_attach(c); | |
1026 | } | |
1027 | ||
1028 | static void bdrv_backing_detach(BdrvChild *c) | |
1029 | { | |
1030 | BlockDriverState *parent = c->opaque; | |
1031 | ||
1032 | assert(parent->backing_blocker); | |
1033 | bdrv_op_unblock_all(c->bs, parent->backing_blocker); | |
1034 | error_free(parent->backing_blocker); | |
1035 | parent->backing_blocker = NULL; | |
1036 | ||
1037 | bdrv_child_cb_detach(c); | |
1038 | } | |
1039 | ||
1040 | /* | |
1041 | * Returns the options and flags that bs->backing should get, based on the | |
1042 | * given options and flags for the parent BDS | |
1043 | */ | |
1044 | static void bdrv_backing_options(int *child_flags, QDict *child_options, | |
1045 | int parent_flags, QDict *parent_options) | |
1046 | { | |
1047 | int flags = parent_flags; | |
1048 | ||
1049 | /* The cache mode is inherited unmodified for backing files; except WCE, | |
1050 | * which is only applied on the top level (BlockBackend) */ | |
1051 | qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_DIRECT); | |
1052 | qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_NO_FLUSH); | |
1053 | qdict_copy_default(child_options, parent_options, BDRV_OPT_FORCE_SHARE); | |
1054 | ||
1055 | /* backing files always opened read-only */ | |
1056 | qdict_set_default_str(child_options, BDRV_OPT_READ_ONLY, "on"); | |
1057 | qdict_set_default_str(child_options, BDRV_OPT_AUTO_READ_ONLY, "off"); | |
1058 | flags &= ~BDRV_O_COPY_ON_READ; | |
1059 | ||
1060 | /* snapshot=on is handled on the top layer */ | |
1061 | flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_TEMPORARY); | |
1062 | ||
1063 | *child_flags = flags; | |
1064 | } | |
1065 | ||
1066 | static int bdrv_backing_update_filename(BdrvChild *c, BlockDriverState *base, | |
1067 | const char *filename, Error **errp) | |
1068 | { | |
1069 | BlockDriverState *parent = c->opaque; | |
1070 | int orig_flags = bdrv_get_flags(parent); | |
1071 | int ret; | |
1072 | ||
1073 | if (!(orig_flags & BDRV_O_RDWR)) { | |
1074 | ret = bdrv_reopen(parent, orig_flags | BDRV_O_RDWR, errp); | |
1075 | if (ret < 0) { | |
1076 | return ret; | |
1077 | } | |
1078 | } | |
1079 | ||
1080 | ret = bdrv_change_backing_file(parent, filename, | |
1081 | base->drv ? base->drv->format_name : ""); | |
1082 | if (ret < 0) { | |
1083 | error_setg_errno(errp, -ret, "Could not update backing file link"); | |
1084 | } | |
1085 | ||
1086 | if (!(orig_flags & BDRV_O_RDWR)) { | |
1087 | bdrv_reopen(parent, orig_flags, NULL); | |
1088 | } | |
1089 | ||
1090 | return ret; | |
1091 | } | |
1092 | ||
1093 | const BdrvChildRole child_backing = { | |
1094 | .parent_is_bds = true, | |
1095 | .get_parent_desc = bdrv_child_get_parent_desc, | |
1096 | .attach = bdrv_backing_attach, | |
1097 | .detach = bdrv_backing_detach, | |
1098 | .inherit_options = bdrv_backing_options, | |
1099 | .drained_begin = bdrv_child_cb_drained_begin, | |
1100 | .drained_poll = bdrv_child_cb_drained_poll, | |
1101 | .drained_end = bdrv_child_cb_drained_end, | |
1102 | .inactivate = bdrv_child_cb_inactivate, | |
1103 | .update_filename = bdrv_backing_update_filename, | |
1104 | }; | |
1105 | ||
1106 | static int bdrv_open_flags(BlockDriverState *bs, int flags) | |
1107 | { | |
1108 | int open_flags = flags; | |
1109 | ||
1110 | /* | |
1111 | * Clear flags that are internal to the block layer before opening the | |
1112 | * image. | |
1113 | */ | |
1114 | open_flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_PROTOCOL); | |
1115 | ||
1116 | /* | |
1117 | * Snapshots should be writable. | |
1118 | */ | |
1119 | if (flags & BDRV_O_TEMPORARY) { | |
1120 | open_flags |= BDRV_O_RDWR; | |
1121 | } | |
1122 | ||
1123 | return open_flags; | |
1124 | } | |
1125 | ||
1126 | static void update_flags_from_options(int *flags, QemuOpts *opts) | |
1127 | { | |
1128 | *flags &= ~BDRV_O_CACHE_MASK; | |
1129 | ||
1130 | assert(qemu_opt_find(opts, BDRV_OPT_CACHE_NO_FLUSH)); | |
1131 | if (qemu_opt_get_bool_del(opts, BDRV_OPT_CACHE_NO_FLUSH, false)) { | |
1132 | *flags |= BDRV_O_NO_FLUSH; | |
1133 | } | |
1134 | ||
1135 | assert(qemu_opt_find(opts, BDRV_OPT_CACHE_DIRECT)); | |
1136 | if (qemu_opt_get_bool_del(opts, BDRV_OPT_CACHE_DIRECT, false)) { | |
1137 | *flags |= BDRV_O_NOCACHE; | |
1138 | } | |
1139 | ||
1140 | *flags &= ~BDRV_O_RDWR; | |
1141 | ||
1142 | assert(qemu_opt_find(opts, BDRV_OPT_READ_ONLY)); | |
1143 | if (!qemu_opt_get_bool_del(opts, BDRV_OPT_READ_ONLY, false)) { | |
1144 | *flags |= BDRV_O_RDWR; | |
1145 | } | |
1146 | ||
1147 | assert(qemu_opt_find(opts, BDRV_OPT_AUTO_READ_ONLY)); | |
1148 | if (qemu_opt_get_bool_del(opts, BDRV_OPT_AUTO_READ_ONLY, false)) { | |
1149 | *flags |= BDRV_O_AUTO_RDONLY; | |
1150 | } | |
1151 | } | |
1152 | ||
1153 | static void update_options_from_flags(QDict *options, int flags) | |
1154 | { | |
1155 | if (!qdict_haskey(options, BDRV_OPT_CACHE_DIRECT)) { | |
1156 | qdict_put_bool(options, BDRV_OPT_CACHE_DIRECT, flags & BDRV_O_NOCACHE); | |
1157 | } | |
1158 | if (!qdict_haskey(options, BDRV_OPT_CACHE_NO_FLUSH)) { | |
1159 | qdict_put_bool(options, BDRV_OPT_CACHE_NO_FLUSH, | |
1160 | flags & BDRV_O_NO_FLUSH); | |
1161 | } | |
1162 | if (!qdict_haskey(options, BDRV_OPT_READ_ONLY)) { | |
1163 | qdict_put_bool(options, BDRV_OPT_READ_ONLY, !(flags & BDRV_O_RDWR)); | |
1164 | } | |
1165 | if (!qdict_haskey(options, BDRV_OPT_AUTO_READ_ONLY)) { | |
1166 | qdict_put_bool(options, BDRV_OPT_AUTO_READ_ONLY, | |
1167 | flags & BDRV_O_AUTO_RDONLY); | |
1168 | } | |
1169 | } | |
1170 | ||
1171 | static void bdrv_assign_node_name(BlockDriverState *bs, | |
1172 | const char *node_name, | |
1173 | Error **errp) | |
1174 | { | |
1175 | char *gen_node_name = NULL; | |
1176 | ||
1177 | if (!node_name) { | |
1178 | node_name = gen_node_name = id_generate(ID_BLOCK); | |
1179 | } else if (!id_wellformed(node_name)) { | |
1180 | /* | |
1181 | * Check for empty string or invalid characters, but not if it is | |
1182 | * generated (generated names use characters not available to the user) | |
1183 | */ | |
1184 | error_setg(errp, "Invalid node name"); | |
1185 | return; | |
1186 | } | |
1187 | ||
1188 | /* takes care of avoiding namespaces collisions */ | |
1189 | if (blk_by_name(node_name)) { | |
1190 | error_setg(errp, "node-name=%s is conflicting with a device id", | |
1191 | node_name); | |
1192 | goto out; | |
1193 | } | |
1194 | ||
1195 | /* takes care of avoiding duplicates node names */ | |
1196 | if (bdrv_find_node(node_name)) { | |
1197 | error_setg(errp, "Duplicate node name"); | |
1198 | goto out; | |
1199 | } | |
1200 | ||
1201 | /* Make sure that the node name isn't truncated */ | |
1202 | if (strlen(node_name) >= sizeof(bs->node_name)) { | |
1203 | error_setg(errp, "Node name too long"); | |
1204 | goto out; | |
1205 | } | |
1206 | ||
1207 | /* copy node name into the bs and insert it into the graph list */ | |
1208 | pstrcpy(bs->node_name, sizeof(bs->node_name), node_name); | |
1209 | QTAILQ_INSERT_TAIL(&graph_bdrv_states, bs, node_list); | |
1210 | out: | |
1211 | g_free(gen_node_name); | |
1212 | } | |
1213 | ||
1214 | static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv, | |
1215 | const char *node_name, QDict *options, | |
1216 | int open_flags, Error **errp) | |
1217 | { | |
1218 | Error *local_err = NULL; | |
1219 | int i, ret; | |
1220 | ||
1221 | bdrv_assign_node_name(bs, node_name, &local_err); | |
1222 | if (local_err) { | |
1223 | error_propagate(errp, local_err); | |
1224 | return -EINVAL; | |
1225 | } | |
1226 | ||
1227 | bs->drv = drv; | |
1228 | bs->read_only = !(bs->open_flags & BDRV_O_RDWR); | |
1229 | bs->opaque = g_malloc0(drv->instance_size); | |
1230 | ||
1231 | if (drv->bdrv_file_open) { | |
1232 | assert(!drv->bdrv_needs_filename || bs->filename[0]); | |
1233 | ret = drv->bdrv_file_open(bs, options, open_flags, &local_err); | |
1234 | } else if (drv->bdrv_open) { | |
1235 | ret = drv->bdrv_open(bs, options, open_flags, &local_err); | |
1236 | } else { | |
1237 | ret = 0; | |
1238 | } | |
1239 | ||
1240 | if (ret < 0) { | |
1241 | if (local_err) { | |
1242 | error_propagate(errp, local_err); | |
1243 | } else if (bs->filename[0]) { | |
1244 | error_setg_errno(errp, -ret, "Could not open '%s'", bs->filename); | |
1245 | } else { | |
1246 | error_setg_errno(errp, -ret, "Could not open image"); | |
1247 | } | |
1248 | goto open_failed; | |
1249 | } | |
1250 | ||
1251 | ret = refresh_total_sectors(bs, bs->total_sectors); | |
1252 | if (ret < 0) { | |
1253 | error_setg_errno(errp, -ret, "Could not refresh total sector count"); | |
1254 | return ret; | |
1255 | } | |
1256 | ||
1257 | bdrv_refresh_limits(bs, &local_err); | |
1258 | if (local_err) { | |
1259 | error_propagate(errp, local_err); | |
1260 | return -EINVAL; | |
1261 | } | |
1262 | ||
1263 | assert(bdrv_opt_mem_align(bs) != 0); | |
1264 | assert(bdrv_min_mem_align(bs) != 0); | |
1265 | assert(is_power_of_2(bs->bl.request_alignment)); | |
1266 | ||
1267 | for (i = 0; i < bs->quiesce_counter; i++) { | |
1268 | if (drv->bdrv_co_drain_begin) { | |
1269 | drv->bdrv_co_drain_begin(bs); | |
1270 | } | |
1271 | } | |
1272 | ||
1273 | return 0; | |
1274 | open_failed: | |
1275 | bs->drv = NULL; | |
1276 | if (bs->file != NULL) { | |
1277 | bdrv_unref_child(bs, bs->file); | |
1278 | bs->file = NULL; | |
1279 | } | |
1280 | g_free(bs->opaque); | |
1281 | bs->opaque = NULL; | |
1282 | return ret; | |
1283 | } | |
1284 | ||
1285 | BlockDriverState *bdrv_new_open_driver(BlockDriver *drv, const char *node_name, | |
1286 | int flags, Error **errp) | |
1287 | { | |
1288 | BlockDriverState *bs; | |
1289 | int ret; | |
1290 | ||
1291 | bs = bdrv_new(); | |
1292 | bs->open_flags = flags; | |
1293 | bs->explicit_options = qdict_new(); | |
1294 | bs->options = qdict_new(); | |
1295 | bs->opaque = NULL; | |
1296 | ||
1297 | update_options_from_flags(bs->options, flags); | |
1298 | ||
1299 | ret = bdrv_open_driver(bs, drv, node_name, bs->options, flags, errp); | |
1300 | if (ret < 0) { | |
1301 | qobject_unref(bs->explicit_options); | |
1302 | bs->explicit_options = NULL; | |
1303 | qobject_unref(bs->options); | |
1304 | bs->options = NULL; | |
1305 | bdrv_unref(bs); | |
1306 | return NULL; | |
1307 | } | |
1308 | ||
1309 | return bs; | |
1310 | } | |
1311 | ||
1312 | QemuOptsList bdrv_runtime_opts = { | |
1313 | .name = "bdrv_common", | |
1314 | .head = QTAILQ_HEAD_INITIALIZER(bdrv_runtime_opts.head), | |
1315 | .desc = { | |
1316 | { | |
1317 | .name = "node-name", | |
1318 | .type = QEMU_OPT_STRING, | |
1319 | .help = "Node name of the block device node", | |
1320 | }, | |
1321 | { | |
1322 | .name = "driver", | |
1323 | .type = QEMU_OPT_STRING, | |
1324 | .help = "Block driver to use for the node", | |
1325 | }, | |
1326 | { | |
1327 | .name = BDRV_OPT_CACHE_DIRECT, | |
1328 | .type = QEMU_OPT_BOOL, | |
1329 | .help = "Bypass software writeback cache on the host", | |
1330 | }, | |
1331 | { | |
1332 | .name = BDRV_OPT_CACHE_NO_FLUSH, | |
1333 | .type = QEMU_OPT_BOOL, | |
1334 | .help = "Ignore flush requests", | |
1335 | }, | |
1336 | { | |
1337 | .name = BDRV_OPT_READ_ONLY, | |
1338 | .type = QEMU_OPT_BOOL, | |
1339 | .help = "Node is opened in read-only mode", | |
1340 | }, | |
1341 | { | |
1342 | .name = BDRV_OPT_AUTO_READ_ONLY, | |
1343 | .type = QEMU_OPT_BOOL, | |
1344 | .help = "Node can become read-only if opening read-write fails", | |
1345 | }, | |
1346 | { | |
1347 | .name = "detect-zeroes", | |
1348 | .type = QEMU_OPT_STRING, | |
1349 | .help = "try to optimize zero writes (off, on, unmap)", | |
1350 | }, | |
1351 | { | |
1352 | .name = BDRV_OPT_DISCARD, | |
1353 | .type = QEMU_OPT_STRING, | |
1354 | .help = "discard operation (ignore/off, unmap/on)", | |
1355 | }, | |
1356 | { | |
1357 | .name = BDRV_OPT_FORCE_SHARE, | |
1358 | .type = QEMU_OPT_BOOL, | |
1359 | .help = "always accept other writers (default: off)", | |
1360 | }, | |
1361 | { /* end of list */ } | |
1362 | }, | |
1363 | }; | |
1364 | ||
1365 | /* | |
1366 | * Common part for opening disk images and files | |
1367 | * | |
1368 | * Removes all processed options from *options. | |
1369 | */ | |
1370 | static int bdrv_open_common(BlockDriverState *bs, BlockBackend *file, | |
1371 | QDict *options, Error **errp) | |
1372 | { | |
1373 | int ret, open_flags; | |
1374 | const char *filename; | |
1375 | const char *driver_name = NULL; | |
1376 | const char *node_name = NULL; | |
1377 | const char *discard; | |
1378 | QemuOpts *opts; | |
1379 | BlockDriver *drv; | |
1380 | Error *local_err = NULL; | |
1381 | ||
1382 | assert(bs->file == NULL); | |
1383 | assert(options != NULL && bs->options != options); | |
1384 | ||
1385 | opts = qemu_opts_create(&bdrv_runtime_opts, NULL, 0, &error_abort); | |
1386 | qemu_opts_absorb_qdict(opts, options, &local_err); | |
1387 | if (local_err) { | |
1388 | error_propagate(errp, local_err); | |
1389 | ret = -EINVAL; | |
1390 | goto fail_opts; | |
1391 | } | |
1392 | ||
1393 | update_flags_from_options(&bs->open_flags, opts); | |
1394 | ||
1395 | driver_name = qemu_opt_get(opts, "driver"); | |
1396 | drv = bdrv_find_format(driver_name); | |
1397 | assert(drv != NULL); | |
1398 | ||
1399 | bs->force_share = qemu_opt_get_bool(opts, BDRV_OPT_FORCE_SHARE, false); | |
1400 | ||
1401 | if (bs->force_share && (bs->open_flags & BDRV_O_RDWR)) { | |
1402 | error_setg(errp, | |
1403 | BDRV_OPT_FORCE_SHARE | |
1404 | "=on can only be used with read-only images"); | |
1405 | ret = -EINVAL; | |
1406 | goto fail_opts; | |
1407 | } | |
1408 | ||
1409 | if (file != NULL) { | |
1410 | filename = blk_bs(file)->filename; | |
1411 | } else { | |
1412 | /* | |
1413 | * Caution: while qdict_get_try_str() is fine, getting | |
1414 | * non-string types would require more care. When @options | |
1415 | * come from -blockdev or blockdev_add, its members are typed | |
1416 | * according to the QAPI schema, but when they come from | |
1417 | * -drive, they're all QString. | |
1418 | */ | |
1419 | filename = qdict_get_try_str(options, "filename"); | |
1420 | } | |
1421 | ||
1422 | if (drv->bdrv_needs_filename && (!filename || !filename[0])) { | |
1423 | error_setg(errp, "The '%s' block driver requires a file name", | |
1424 | drv->format_name); | |
1425 | ret = -EINVAL; | |
1426 | goto fail_opts; | |
1427 | } | |
1428 | ||
1429 | trace_bdrv_open_common(bs, filename ?: "", bs->open_flags, | |
1430 | drv->format_name); | |
1431 | ||
1432 | bs->read_only = !(bs->open_flags & BDRV_O_RDWR); | |
1433 | ||
1434 | if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv, bs->read_only)) { | |
1435 | error_setg(errp, | |
1436 | !bs->read_only && bdrv_is_whitelisted(drv, true) | |
1437 | ? "Driver '%s' can only be used for read-only devices" | |
1438 | : "Driver '%s' is not whitelisted", | |
1439 | drv->format_name); | |
1440 | ret = -ENOTSUP; | |
1441 | goto fail_opts; | |
1442 | } | |
1443 | ||
1444 | /* bdrv_new() and bdrv_close() make it so */ | |
1445 | assert(atomic_read(&bs->copy_on_read) == 0); | |
1446 | ||
1447 | if (bs->open_flags & BDRV_O_COPY_ON_READ) { | |
1448 | if (!bs->read_only) { | |
1449 | bdrv_enable_copy_on_read(bs); | |
1450 | } else { | |
1451 | error_setg(errp, "Can't use copy-on-read on read-only device"); | |
1452 | ret = -EINVAL; | |
1453 | goto fail_opts; | |
1454 | } | |
1455 | } | |
1456 | ||
1457 | discard = qemu_opt_get(opts, BDRV_OPT_DISCARD); | |
1458 | if (discard != NULL) { | |
1459 | if (bdrv_parse_discard_flags(discard, &bs->open_flags) != 0) { | |
1460 | error_setg(errp, "Invalid discard option"); | |
1461 | ret = -EINVAL; | |
1462 | goto fail_opts; | |
1463 | } | |
1464 | } | |
1465 | ||
1466 | bs->detect_zeroes = | |
1467 | bdrv_parse_detect_zeroes(opts, bs->open_flags, &local_err); | |
1468 | if (local_err) { | |
1469 | error_propagate(errp, local_err); | |
1470 | ret = -EINVAL; | |
1471 | goto fail_opts; | |
1472 | } | |
1473 | ||
1474 | if (filename != NULL) { | |
1475 | pstrcpy(bs->filename, sizeof(bs->filename), filename); | |
1476 | } else { | |
1477 | bs->filename[0] = '\0'; | |
1478 | } | |
1479 | pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), bs->filename); | |
1480 | ||
1481 | /* Open the image, either directly or using a protocol */ | |
1482 | open_flags = bdrv_open_flags(bs, bs->open_flags); | |
1483 | node_name = qemu_opt_get(opts, "node-name"); | |
1484 | ||
1485 | assert(!drv->bdrv_file_open || file == NULL); | |
1486 | ret = bdrv_open_driver(bs, drv, node_name, options, open_flags, errp); | |
1487 | if (ret < 0) { | |
1488 | goto fail_opts; | |
1489 | } | |
1490 | ||
1491 | qemu_opts_del(opts); | |
1492 | return 0; | |
1493 | ||
1494 | fail_opts: | |
1495 | qemu_opts_del(opts); | |
1496 | return ret; | |
1497 | } | |
1498 | ||
1499 | static QDict *parse_json_filename(const char *filename, Error **errp) | |
1500 | { | |
1501 | QObject *options_obj; | |
1502 | QDict *options; | |
1503 | int ret; | |
1504 | ||
1505 | ret = strstart(filename, "json:", &filename); | |
1506 | assert(ret); | |
1507 | ||
1508 | options_obj = qobject_from_json(filename, errp); | |
1509 | if (!options_obj) { | |
1510 | error_prepend(errp, "Could not parse the JSON options: "); | |
1511 | return NULL; | |
1512 | } | |
1513 | ||
1514 | options = qobject_to(QDict, options_obj); | |
1515 | if (!options) { | |
1516 | qobject_unref(options_obj); | |
1517 | error_setg(errp, "Invalid JSON object given"); | |
1518 | return NULL; | |
1519 | } | |
1520 | ||
1521 | qdict_flatten(options); | |
1522 | ||
1523 | return options; | |
1524 | } | |
1525 | ||
1526 | static void parse_json_protocol(QDict *options, const char **pfilename, | |
1527 | Error **errp) | |
1528 | { | |
1529 | QDict *json_options; | |
1530 | Error *local_err = NULL; | |
1531 | ||
1532 | /* Parse json: pseudo-protocol */ | |
1533 | if (!*pfilename || !g_str_has_prefix(*pfilename, "json:")) { | |
1534 | return; | |
1535 | } | |
1536 | ||
1537 | json_options = parse_json_filename(*pfilename, &local_err); | |
1538 | if (local_err) { | |
1539 | error_propagate(errp, local_err); | |
1540 | return; | |
1541 | } | |
1542 | ||
1543 | /* Options given in the filename have lower priority than options | |
1544 | * specified directly */ | |
1545 | qdict_join(options, json_options, false); | |
1546 | qobject_unref(json_options); | |
1547 | *pfilename = NULL; | |
1548 | } | |
1549 | ||
1550 | /* | |
1551 | * Fills in default options for opening images and converts the legacy | |
1552 | * filename/flags pair to option QDict entries. | |
1553 | * The BDRV_O_PROTOCOL flag in *flags will be set or cleared accordingly if a | |
1554 | * block driver has been specified explicitly. | |
1555 | */ | |
1556 | static int bdrv_fill_options(QDict **options, const char *filename, | |
1557 | int *flags, Error **errp) | |
1558 | { | |
1559 | const char *drvname; | |
1560 | bool protocol = *flags & BDRV_O_PROTOCOL; | |
1561 | bool parse_filename = false; | |
1562 | BlockDriver *drv = NULL; | |
1563 | Error *local_err = NULL; | |
1564 | ||
1565 | /* | |
1566 | * Caution: while qdict_get_try_str() is fine, getting non-string | |
1567 | * types would require more care. When @options come from | |
1568 | * -blockdev or blockdev_add, its members are typed according to | |
1569 | * the QAPI schema, but when they come from -drive, they're all | |
1570 | * QString. | |
1571 | */ | |
1572 | drvname = qdict_get_try_str(*options, "driver"); | |
1573 | if (drvname) { | |
1574 | drv = bdrv_find_format(drvname); | |
1575 | if (!drv) { | |
1576 | error_setg(errp, "Unknown driver '%s'", drvname); | |
1577 | return -ENOENT; | |
1578 | } | |
1579 | /* If the user has explicitly specified the driver, this choice should | |
1580 | * override the BDRV_O_PROTOCOL flag */ | |
1581 | protocol = drv->bdrv_file_open; | |
1582 | } | |
1583 | ||
1584 | if (protocol) { | |
1585 | *flags |= BDRV_O_PROTOCOL; | |
1586 | } else { | |
1587 | *flags &= ~BDRV_O_PROTOCOL; | |
1588 | } | |
1589 | ||
1590 | /* Translate cache options from flags into options */ | |
1591 | update_options_from_flags(*options, *flags); | |
1592 | ||
1593 | /* Fetch the file name from the options QDict if necessary */ | |
1594 | if (protocol && filename) { | |
1595 | if (!qdict_haskey(*options, "filename")) { | |
1596 | qdict_put_str(*options, "filename", filename); | |
1597 | parse_filename = true; | |
1598 | } else { | |
1599 | error_setg(errp, "Can't specify 'file' and 'filename' options at " | |
1600 | "the same time"); | |
1601 | return -EINVAL; | |
1602 | } | |
1603 | } | |
1604 | ||
1605 | /* Find the right block driver */ | |
1606 | /* See cautionary note on accessing @options above */ | |
1607 | filename = qdict_get_try_str(*options, "filename"); | |
1608 | ||
1609 | if (!drvname && protocol) { | |
1610 | if (filename) { | |
1611 | drv = bdrv_find_protocol(filename, parse_filename, errp); | |
1612 | if (!drv) { | |
1613 | return -EINVAL; | |
1614 | } | |
1615 | ||
1616 | drvname = drv->format_name; | |
1617 | qdict_put_str(*options, "driver", drvname); | |
1618 | } else { | |
1619 | error_setg(errp, "Must specify either driver or file"); | |
1620 | return -EINVAL; | |
1621 | } | |
1622 | } | |
1623 | ||
1624 | assert(drv || !protocol); | |
1625 | ||
1626 | /* Driver-specific filename parsing */ | |
1627 | if (drv && drv->bdrv_parse_filename && parse_filename) { | |
1628 | drv->bdrv_parse_filename(filename, *options, &local_err); | |
1629 | if (local_err) { | |
1630 | error_propagate(errp, local_err); | |
1631 | return -EINVAL; | |
1632 | } | |
1633 | ||
1634 | if (!drv->bdrv_needs_filename) { | |
1635 | qdict_del(*options, "filename"); | |
1636 | } | |
1637 | } | |
1638 | ||
1639 | return 0; | |
1640 | } | |
1641 | ||
1642 | static int bdrv_child_check_perm(BdrvChild *c, BlockReopenQueue *q, | |
1643 | uint64_t perm, uint64_t shared, | |
1644 | GSList *ignore_children, Error **errp); | |
1645 | static void bdrv_child_abort_perm_update(BdrvChild *c); | |
1646 | static void bdrv_child_set_perm(BdrvChild *c, uint64_t perm, uint64_t shared); | |
1647 | ||
1648 | typedef struct BlockReopenQueueEntry { | |
1649 | bool prepared; | |
1650 | BDRVReopenState state; | |
1651 | QSIMPLEQ_ENTRY(BlockReopenQueueEntry) entry; | |
1652 | } BlockReopenQueueEntry; | |
1653 | ||
1654 | /* | |
1655 | * Return the flags that @bs will have after the reopens in @q have | |
1656 | * successfully completed. If @q is NULL (or @bs is not contained in @q), | |
1657 | * return the current flags. | |
1658 | */ | |
1659 | static int bdrv_reopen_get_flags(BlockReopenQueue *q, BlockDriverState *bs) | |
1660 | { | |
1661 | BlockReopenQueueEntry *entry; | |
1662 | ||
1663 | if (q != NULL) { | |
1664 | QSIMPLEQ_FOREACH(entry, q, entry) { | |
1665 | if (entry->state.bs == bs) { | |
1666 | return entry->state.flags; | |
1667 | } | |
1668 | } | |
1669 | } | |
1670 | ||
1671 | return bs->open_flags; | |
1672 | } | |
1673 | ||
1674 | /* Returns whether the image file can be written to after the reopen queue @q | |
1675 | * has been successfully applied, or right now if @q is NULL. */ | |
1676 | static bool bdrv_is_writable_after_reopen(BlockDriverState *bs, | |
1677 | BlockReopenQueue *q) | |
1678 | { | |
1679 | int flags = bdrv_reopen_get_flags(q, bs); | |
1680 | ||
1681 | return (flags & (BDRV_O_RDWR | BDRV_O_INACTIVE)) == BDRV_O_RDWR; | |
1682 | } | |
1683 | ||
1684 | /* | |
1685 | * Return whether the BDS can be written to. This is not necessarily | |
1686 | * the same as !bdrv_is_read_only(bs), as inactivated images may not | |
1687 | * be written to but do not count as read-only images. | |
1688 | */ | |
1689 | bool bdrv_is_writable(BlockDriverState *bs) | |
1690 | { | |
1691 | return bdrv_is_writable_after_reopen(bs, NULL); | |
1692 | } | |
1693 | ||
1694 | static void bdrv_child_perm(BlockDriverState *bs, BlockDriverState *child_bs, | |
1695 | BdrvChild *c, const BdrvChildRole *role, | |
1696 | BlockReopenQueue *reopen_queue, | |
1697 | uint64_t parent_perm, uint64_t parent_shared, | |
1698 | uint64_t *nperm, uint64_t *nshared) | |
1699 | { | |
1700 | if (bs->drv && bs->drv->bdrv_child_perm) { | |
1701 | bs->drv->bdrv_child_perm(bs, c, role, reopen_queue, | |
1702 | parent_perm, parent_shared, | |
1703 | nperm, nshared); | |
1704 | } | |
1705 | /* TODO Take force_share from reopen_queue */ | |
1706 | if (child_bs && child_bs->force_share) { | |
1707 | *nshared = BLK_PERM_ALL; | |
1708 | } | |
1709 | } | |
1710 | ||
1711 | /* | |
1712 | * Check whether permissions on this node can be changed in a way that | |
1713 | * @cumulative_perms and @cumulative_shared_perms are the new cumulative | |
1714 | * permissions of all its parents. This involves checking whether all necessary | |
1715 | * permission changes to child nodes can be performed. | |
1716 | * | |
1717 | * A call to this function must always be followed by a call to bdrv_set_perm() | |
1718 | * or bdrv_abort_perm_update(). | |
1719 | */ | |
1720 | static int bdrv_check_perm(BlockDriverState *bs, BlockReopenQueue *q, | |
1721 | uint64_t cumulative_perms, | |
1722 | uint64_t cumulative_shared_perms, | |
1723 | GSList *ignore_children, Error **errp) | |
1724 | { | |
1725 | BlockDriver *drv = bs->drv; | |
1726 | BdrvChild *c; | |
1727 | int ret; | |
1728 | ||
1729 | /* Write permissions never work with read-only images */ | |
1730 | if ((cumulative_perms & (BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED)) && | |
1731 | !bdrv_is_writable_after_reopen(bs, q)) | |
1732 | { | |
1733 | error_setg(errp, "Block node is read-only"); | |
1734 | return -EPERM; | |
1735 | } | |
1736 | ||
1737 | /* Check this node */ | |
1738 | if (!drv) { | |
1739 | return 0; | |
1740 | } | |
1741 | ||
1742 | if (drv->bdrv_check_perm) { | |
1743 | return drv->bdrv_check_perm(bs, cumulative_perms, | |
1744 | cumulative_shared_perms, errp); | |
1745 | } | |
1746 | ||
1747 | /* Drivers that never have children can omit .bdrv_child_perm() */ | |
1748 | if (!drv->bdrv_child_perm) { | |
1749 | assert(QLIST_EMPTY(&bs->children)); | |
1750 | return 0; | |
1751 | } | |
1752 | ||
1753 | /* Check all children */ | |
1754 | QLIST_FOREACH(c, &bs->children, next) { | |
1755 | uint64_t cur_perm, cur_shared; | |
1756 | bdrv_child_perm(bs, c->bs, c, c->role, q, | |
1757 | cumulative_perms, cumulative_shared_perms, | |
1758 | &cur_perm, &cur_shared); | |
1759 | ret = bdrv_child_check_perm(c, q, cur_perm, cur_shared, | |
1760 | ignore_children, errp); | |
1761 | if (ret < 0) { | |
1762 | return ret; | |
1763 | } | |
1764 | } | |
1765 | ||
1766 | return 0; | |
1767 | } | |
1768 | ||
1769 | /* | |
1770 | * Notifies drivers that after a previous bdrv_check_perm() call, the | |
1771 | * permission update is not performed and any preparations made for it (e.g. | |
1772 | * taken file locks) need to be undone. | |
1773 | * | |
1774 | * This function recursively notifies all child nodes. | |
1775 | */ | |
1776 | static void bdrv_abort_perm_update(BlockDriverState *bs) | |
1777 | { | |
1778 | BlockDriver *drv = bs->drv; | |
1779 | BdrvChild *c; | |
1780 | ||
1781 | if (!drv) { | |
1782 | return; | |
1783 | } | |
1784 | ||
1785 | if (drv->bdrv_abort_perm_update) { | |
1786 | drv->bdrv_abort_perm_update(bs); | |
1787 | } | |
1788 | ||
1789 | QLIST_FOREACH(c, &bs->children, next) { | |
1790 | bdrv_child_abort_perm_update(c); | |
1791 | } | |
1792 | } | |
1793 | ||
1794 | static void bdrv_set_perm(BlockDriverState *bs, uint64_t cumulative_perms, | |
1795 | uint64_t cumulative_shared_perms) | |
1796 | { | |
1797 | BlockDriver *drv = bs->drv; | |
1798 | BdrvChild *c; | |
1799 | ||
1800 | if (!drv) { | |
1801 | return; | |
1802 | } | |
1803 | ||
1804 | /* Update this node */ | |
1805 | if (drv->bdrv_set_perm) { | |
1806 | drv->bdrv_set_perm(bs, cumulative_perms, cumulative_shared_perms); | |
1807 | } | |
1808 | ||
1809 | /* Drivers that never have children can omit .bdrv_child_perm() */ | |
1810 | if (!drv->bdrv_child_perm) { | |
1811 | assert(QLIST_EMPTY(&bs->children)); | |
1812 | return; | |
1813 | } | |
1814 | ||
1815 | /* Update all children */ | |
1816 | QLIST_FOREACH(c, &bs->children, next) { | |
1817 | uint64_t cur_perm, cur_shared; | |
1818 | bdrv_child_perm(bs, c->bs, c, c->role, NULL, | |
1819 | cumulative_perms, cumulative_shared_perms, | |
1820 | &cur_perm, &cur_shared); | |
1821 | bdrv_child_set_perm(c, cur_perm, cur_shared); | |
1822 | } | |
1823 | } | |
1824 | ||
1825 | static void bdrv_get_cumulative_perm(BlockDriverState *bs, uint64_t *perm, | |
1826 | uint64_t *shared_perm) | |
1827 | { | |
1828 | BdrvChild *c; | |
1829 | uint64_t cumulative_perms = 0; | |
1830 | uint64_t cumulative_shared_perms = BLK_PERM_ALL; | |
1831 | ||
1832 | QLIST_FOREACH(c, &bs->parents, next_parent) { | |
1833 | cumulative_perms |= c->perm; | |
1834 | cumulative_shared_perms &= c->shared_perm; | |
1835 | } | |
1836 | ||
1837 | *perm = cumulative_perms; | |
1838 | *shared_perm = cumulative_shared_perms; | |
1839 | } | |
1840 | ||
1841 | static char *bdrv_child_user_desc(BdrvChild *c) | |
1842 | { | |
1843 | if (c->role->get_parent_desc) { | |
1844 | return c->role->get_parent_desc(c); | |
1845 | } | |
1846 | ||
1847 | return g_strdup("another user"); | |
1848 | } | |
1849 | ||
1850 | char *bdrv_perm_names(uint64_t perm) | |
1851 | { | |
1852 | struct perm_name { | |
1853 | uint64_t perm; | |
1854 | const char *name; | |
1855 | } permissions[] = { | |
1856 | { BLK_PERM_CONSISTENT_READ, "consistent read" }, | |
1857 | { BLK_PERM_WRITE, "write" }, | |
1858 | { BLK_PERM_WRITE_UNCHANGED, "write unchanged" }, | |
1859 | { BLK_PERM_RESIZE, "resize" }, | |
1860 | { BLK_PERM_GRAPH_MOD, "change children" }, | |
1861 | { 0, NULL } | |
1862 | }; | |
1863 | ||
1864 | char *result = g_strdup(""); | |
1865 | struct perm_name *p; | |
1866 | ||
1867 | for (p = permissions; p->name; p++) { | |
1868 | if (perm & p->perm) { | |
1869 | char *old = result; | |
1870 | result = g_strdup_printf("%s%s%s", old, *old ? ", " : "", p->name); | |
1871 | g_free(old); | |
1872 | } | |
1873 | } | |
1874 | ||
1875 | return result; | |
1876 | } | |
1877 | ||
1878 | /* | |
1879 | * Checks whether a new reference to @bs can be added if the new user requires | |
1880 | * @new_used_perm/@new_shared_perm as its permissions. If @ignore_children is | |
1881 | * set, the BdrvChild objects in this list are ignored in the calculations; | |
1882 | * this allows checking permission updates for an existing reference. | |
1883 | * | |
1884 | * Needs to be followed by a call to either bdrv_set_perm() or | |
1885 | * bdrv_abort_perm_update(). */ | |
1886 | static int bdrv_check_update_perm(BlockDriverState *bs, BlockReopenQueue *q, | |
1887 | uint64_t new_used_perm, | |
1888 | uint64_t new_shared_perm, | |
1889 | GSList *ignore_children, Error **errp) | |
1890 | { | |
1891 | BdrvChild *c; | |
1892 | uint64_t cumulative_perms = new_used_perm; | |
1893 | uint64_t cumulative_shared_perms = new_shared_perm; | |
1894 | ||
1895 | /* There is no reason why anyone couldn't tolerate write_unchanged */ | |
1896 | assert(new_shared_perm & BLK_PERM_WRITE_UNCHANGED); | |
1897 | ||
1898 | QLIST_FOREACH(c, &bs->parents, next_parent) { | |
1899 | if (g_slist_find(ignore_children, c)) { | |
1900 | continue; | |
1901 | } | |
1902 | ||
1903 | if ((new_used_perm & c->shared_perm) != new_used_perm) { | |
1904 | char *user = bdrv_child_user_desc(c); | |
1905 | char *perm_names = bdrv_perm_names(new_used_perm & ~c->shared_perm); | |
1906 | error_setg(errp, "Conflicts with use by %s as '%s', which does not " | |
1907 | "allow '%s' on %s", | |
1908 | user, c->name, perm_names, bdrv_get_node_name(c->bs)); | |
1909 | g_free(user); | |
1910 | g_free(perm_names); | |
1911 | return -EPERM; | |
1912 | } | |
1913 | ||
1914 | if ((c->perm & new_shared_perm) != c->perm) { | |
1915 | char *user = bdrv_child_user_desc(c); | |
1916 | char *perm_names = bdrv_perm_names(c->perm & ~new_shared_perm); | |
1917 | error_setg(errp, "Conflicts with use by %s as '%s', which uses " | |
1918 | "'%s' on %s", | |
1919 | user, c->name, perm_names, bdrv_get_node_name(c->bs)); | |
1920 | g_free(user); | |
1921 | g_free(perm_names); | |
1922 | return -EPERM; | |
1923 | } | |
1924 | ||
1925 | cumulative_perms |= c->perm; | |
1926 | cumulative_shared_perms &= c->shared_perm; | |
1927 | } | |
1928 | ||
1929 | return bdrv_check_perm(bs, q, cumulative_perms, cumulative_shared_perms, | |
1930 | ignore_children, errp); | |
1931 | } | |
1932 | ||
1933 | /* Needs to be followed by a call to either bdrv_child_set_perm() or | |
1934 | * bdrv_child_abort_perm_update(). */ | |
1935 | static int bdrv_child_check_perm(BdrvChild *c, BlockReopenQueue *q, | |
1936 | uint64_t perm, uint64_t shared, | |
1937 | GSList *ignore_children, Error **errp) | |
1938 | { | |
1939 | int ret; | |
1940 | ||
1941 | ignore_children = g_slist_prepend(g_slist_copy(ignore_children), c); | |
1942 | ret = bdrv_check_update_perm(c->bs, q, perm, shared, ignore_children, errp); | |
1943 | g_slist_free(ignore_children); | |
1944 | ||
1945 | return ret; | |
1946 | } | |
1947 | ||
1948 | static void bdrv_child_set_perm(BdrvChild *c, uint64_t perm, uint64_t shared) | |
1949 | { | |
1950 | uint64_t cumulative_perms, cumulative_shared_perms; | |
1951 | ||
1952 | c->perm = perm; | |
1953 | c->shared_perm = shared; | |
1954 | ||
1955 | bdrv_get_cumulative_perm(c->bs, &cumulative_perms, | |
1956 | &cumulative_shared_perms); | |
1957 | bdrv_set_perm(c->bs, cumulative_perms, cumulative_shared_perms); | |
1958 | } | |
1959 | ||
1960 | static void bdrv_child_abort_perm_update(BdrvChild *c) | |
1961 | { | |
1962 | bdrv_abort_perm_update(c->bs); | |
1963 | } | |
1964 | ||
1965 | int bdrv_child_try_set_perm(BdrvChild *c, uint64_t perm, uint64_t shared, | |
1966 | Error **errp) | |
1967 | { | |
1968 | int ret; | |
1969 | ||
1970 | ret = bdrv_child_check_perm(c, NULL, perm, shared, NULL, errp); | |
1971 | if (ret < 0) { | |
1972 | bdrv_child_abort_perm_update(c); | |
1973 | return ret; | |
1974 | } | |
1975 | ||
1976 | bdrv_child_set_perm(c, perm, shared); | |
1977 | ||
1978 | return 0; | |
1979 | } | |
1980 | ||
1981 | void bdrv_filter_default_perms(BlockDriverState *bs, BdrvChild *c, | |
1982 | const BdrvChildRole *role, | |
1983 | BlockReopenQueue *reopen_queue, | |
1984 | uint64_t perm, uint64_t shared, | |
1985 | uint64_t *nperm, uint64_t *nshared) | |
1986 | { | |
1987 | if (c == NULL) { | |
1988 | *nperm = perm & DEFAULT_PERM_PASSTHROUGH; | |
1989 | *nshared = (shared & DEFAULT_PERM_PASSTHROUGH) | DEFAULT_PERM_UNCHANGED; | |
1990 | return; | |
1991 | } | |
1992 | ||
1993 | *nperm = (perm & DEFAULT_PERM_PASSTHROUGH) | | |
1994 | (c->perm & DEFAULT_PERM_UNCHANGED); | |
1995 | *nshared = (shared & DEFAULT_PERM_PASSTHROUGH) | | |
1996 | (c->shared_perm & DEFAULT_PERM_UNCHANGED); | |
1997 | } | |
1998 | ||
1999 | void bdrv_format_default_perms(BlockDriverState *bs, BdrvChild *c, | |
2000 | const BdrvChildRole *role, | |
2001 | BlockReopenQueue *reopen_queue, | |
2002 | uint64_t perm, uint64_t shared, | |
2003 | uint64_t *nperm, uint64_t *nshared) | |
2004 | { | |
2005 | bool backing = (role == &child_backing); | |
2006 | assert(role == &child_backing || role == &child_file); | |
2007 | ||
2008 | if (!backing) { | |
2009 | int flags = bdrv_reopen_get_flags(reopen_queue, bs); | |
2010 | ||
2011 | /* Apart from the modifications below, the same permissions are | |
2012 | * forwarded and left alone as for filters */ | |
2013 | bdrv_filter_default_perms(bs, c, role, reopen_queue, perm, shared, | |
2014 | &perm, &shared); | |
2015 | ||
2016 | /* Format drivers may touch metadata even if the guest doesn't write */ | |
2017 | if (bdrv_is_writable_after_reopen(bs, reopen_queue)) { | |
2018 | perm |= BLK_PERM_WRITE | BLK_PERM_RESIZE; | |
2019 | } | |
2020 | ||
2021 | /* bs->file always needs to be consistent because of the metadata. We | |
2022 | * can never allow other users to resize or write to it. */ | |
2023 | if (!(flags & BDRV_O_NO_IO)) { | |
2024 | perm |= BLK_PERM_CONSISTENT_READ; | |
2025 | } | |
2026 | shared &= ~(BLK_PERM_WRITE | BLK_PERM_RESIZE); | |
2027 | } else { | |
2028 | /* We want consistent read from backing files if the parent needs it. | |
2029 | * No other operations are performed on backing files. */ | |
2030 | perm &= BLK_PERM_CONSISTENT_READ; | |
2031 | ||
2032 | /* If the parent can deal with changing data, we're okay with a | |
2033 | * writable and resizable backing file. */ | |
2034 | /* TODO Require !(perm & BLK_PERM_CONSISTENT_READ), too? */ | |
2035 | if (shared & BLK_PERM_WRITE) { | |
2036 | shared = BLK_PERM_WRITE | BLK_PERM_RESIZE; | |
2037 | } else { | |
2038 | shared = 0; | |
2039 | } | |
2040 | ||
2041 | shared |= BLK_PERM_CONSISTENT_READ | BLK_PERM_GRAPH_MOD | | |
2042 | BLK_PERM_WRITE_UNCHANGED; | |
2043 | } | |
2044 | ||
2045 | if (bs->open_flags & BDRV_O_INACTIVE) { | |
2046 | shared |= BLK_PERM_WRITE | BLK_PERM_RESIZE; | |
2047 | } | |
2048 | ||
2049 | *nperm = perm; | |
2050 | *nshared = shared; | |
2051 | } | |
2052 | ||
2053 | static void bdrv_replace_child_noperm(BdrvChild *child, | |
2054 | BlockDriverState *new_bs) | |
2055 | { | |
2056 | BlockDriverState *old_bs = child->bs; | |
2057 | int i; | |
2058 | ||
2059 | if (old_bs && new_bs) { | |
2060 | assert(bdrv_get_aio_context(old_bs) == bdrv_get_aio_context(new_bs)); | |
2061 | } | |
2062 | if (old_bs) { | |
2063 | /* Detach first so that the recursive drain sections coming from @child | |
2064 | * are already gone and we only end the drain sections that came from | |
2065 | * elsewhere. */ | |
2066 | if (child->role->detach) { | |
2067 | child->role->detach(child); | |
2068 | } | |
2069 | if (old_bs->quiesce_counter && child->role->drained_end) { | |
2070 | int num = old_bs->quiesce_counter; | |
2071 | if (child->role->parent_is_bds) { | |
2072 | num -= bdrv_drain_all_count; | |
2073 | } | |
2074 | assert(num >= 0); | |
2075 | for (i = 0; i < num; i++) { | |
2076 | child->role->drained_end(child); | |
2077 | } | |
2078 | } | |
2079 | QLIST_REMOVE(child, next_parent); | |
2080 | } | |
2081 | ||
2082 | child->bs = new_bs; | |
2083 | ||
2084 | if (new_bs) { | |
2085 | QLIST_INSERT_HEAD(&new_bs->parents, child, next_parent); | |
2086 | if (new_bs->quiesce_counter && child->role->drained_begin) { | |
2087 | int num = new_bs->quiesce_counter; | |
2088 | if (child->role->parent_is_bds) { | |
2089 | num -= bdrv_drain_all_count; | |
2090 | } | |
2091 | assert(num >= 0); | |
2092 | for (i = 0; i < num; i++) { | |
2093 | bdrv_parent_drained_begin_single(child, true); | |
2094 | } | |
2095 | } | |
2096 | ||
2097 | /* Attach only after starting new drained sections, so that recursive | |
2098 | * drain sections coming from @child don't get an extra .drained_begin | |
2099 | * callback. */ | |
2100 | if (child->role->attach) { | |
2101 | child->role->attach(child); | |
2102 | } | |
2103 | } | |
2104 | } | |
2105 | ||
2106 | /* | |
2107 | * Updates @child to change its reference to point to @new_bs, including | |
2108 | * checking and applying the necessary permisson updates both to the old node | |
2109 | * and to @new_bs. | |
2110 | * | |
2111 | * NULL is passed as @new_bs for removing the reference before freeing @child. | |
2112 | * | |
2113 | * If @new_bs is not NULL, bdrv_check_perm() must be called beforehand, as this | |
2114 | * function uses bdrv_set_perm() to update the permissions according to the new | |
2115 | * reference that @new_bs gets. | |
2116 | */ | |
2117 | static void bdrv_replace_child(BdrvChild *child, BlockDriverState *new_bs) | |
2118 | { | |
2119 | BlockDriverState *old_bs = child->bs; | |
2120 | uint64_t perm, shared_perm; | |
2121 | ||
2122 | bdrv_replace_child_noperm(child, new_bs); | |
2123 | ||
2124 | if (old_bs) { | |
2125 | /* Update permissions for old node. This is guaranteed to succeed | |
2126 | * because we're just taking a parent away, so we're loosening | |
2127 | * restrictions. */ | |
2128 | bdrv_get_cumulative_perm(old_bs, &perm, &shared_perm); | |
2129 | bdrv_check_perm(old_bs, NULL, perm, shared_perm, NULL, &error_abort); | |
2130 | bdrv_set_perm(old_bs, perm, shared_perm); | |
2131 | } | |
2132 | ||
2133 | if (new_bs) { | |
2134 | bdrv_get_cumulative_perm(new_bs, &perm, &shared_perm); | |
2135 | bdrv_set_perm(new_bs, perm, shared_perm); | |
2136 | } | |
2137 | } | |
2138 | ||
2139 | BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs, | |
2140 | const char *child_name, | |
2141 | const BdrvChildRole *child_role, | |
2142 | uint64_t perm, uint64_t shared_perm, | |
2143 | void *opaque, Error **errp) | |
2144 | { | |
2145 | BdrvChild *child; | |
2146 | int ret; | |
2147 | ||
2148 | ret = bdrv_check_update_perm(child_bs, NULL, perm, shared_perm, NULL, errp); | |
2149 | if (ret < 0) { | |
2150 | bdrv_abort_perm_update(child_bs); | |
2151 | return NULL; | |
2152 | } | |
2153 | ||
2154 | child = g_new(BdrvChild, 1); | |
2155 | *child = (BdrvChild) { | |
2156 | .bs = NULL, | |
2157 | .name = g_strdup(child_name), | |
2158 | .role = child_role, | |
2159 | .perm = perm, | |
2160 | .shared_perm = shared_perm, | |
2161 | .opaque = opaque, | |
2162 | }; | |
2163 | ||
2164 | /* This performs the matching bdrv_set_perm() for the above check. */ | |
2165 | bdrv_replace_child(child, child_bs); | |
2166 | ||
2167 | return child; | |
2168 | } | |
2169 | ||
2170 | BdrvChild *bdrv_attach_child(BlockDriverState *parent_bs, | |
2171 | BlockDriverState *child_bs, | |
2172 | const char *child_name, | |
2173 | const BdrvChildRole *child_role, | |
2174 | Error **errp) | |
2175 | { | |
2176 | BdrvChild *child; | |
2177 | uint64_t perm, shared_perm; | |
2178 | ||
2179 | bdrv_get_cumulative_perm(parent_bs, &perm, &shared_perm); | |
2180 | ||
2181 | assert(parent_bs->drv); | |
2182 | assert(bdrv_get_aio_context(parent_bs) == bdrv_get_aio_context(child_bs)); | |
2183 | bdrv_child_perm(parent_bs, child_bs, NULL, child_role, NULL, | |
2184 | perm, shared_perm, &perm, &shared_perm); | |
2185 | ||
2186 | child = bdrv_root_attach_child(child_bs, child_name, child_role, | |
2187 | perm, shared_perm, parent_bs, errp); | |
2188 | if (child == NULL) { | |
2189 | return NULL; | |
2190 | } | |
2191 | ||
2192 | QLIST_INSERT_HEAD(&parent_bs->children, child, next); | |
2193 | return child; | |
2194 | } | |
2195 | ||
2196 | static void bdrv_detach_child(BdrvChild *child) | |
2197 | { | |
2198 | if (child->next.le_prev) { | |
2199 | QLIST_REMOVE(child, next); | |
2200 | child->next.le_prev = NULL; | |
2201 | } | |
2202 | ||
2203 | bdrv_replace_child(child, NULL); | |
2204 | ||
2205 | g_free(child->name); | |
2206 | g_free(child); | |
2207 | } | |
2208 | ||
2209 | void bdrv_root_unref_child(BdrvChild *child) | |
2210 | { | |
2211 | BlockDriverState *child_bs; | |
2212 | ||
2213 | child_bs = child->bs; | |
2214 | bdrv_detach_child(child); | |
2215 | bdrv_unref(child_bs); | |
2216 | } | |
2217 | ||
2218 | void bdrv_unref_child(BlockDriverState *parent, BdrvChild *child) | |
2219 | { | |
2220 | if (child == NULL) { | |
2221 | return; | |
2222 | } | |
2223 | ||
2224 | if (child->bs->inherits_from == parent) { | |
2225 | BdrvChild *c; | |
2226 | ||
2227 | /* Remove inherits_from only when the last reference between parent and | |
2228 | * child->bs goes away. */ | |
2229 | QLIST_FOREACH(c, &parent->children, next) { | |
2230 | if (c != child && c->bs == child->bs) { | |
2231 | break; | |
2232 | } | |
2233 | } | |
2234 | if (c == NULL) { | |
2235 | child->bs->inherits_from = NULL; | |
2236 | } | |
2237 | } | |
2238 | ||
2239 | bdrv_root_unref_child(child); | |
2240 | } | |
2241 | ||
2242 | ||
2243 | static void bdrv_parent_cb_change_media(BlockDriverState *bs, bool load) | |
2244 | { | |
2245 | BdrvChild *c; | |
2246 | QLIST_FOREACH(c, &bs->parents, next_parent) { | |
2247 | if (c->role->change_media) { | |
2248 | c->role->change_media(c, load); | |
2249 | } | |
2250 | } | |
2251 | } | |
2252 | ||
2253 | /* | |
2254 | * Sets the backing file link of a BDS. A new reference is created; callers | |
2255 | * which don't need their own reference any more must call bdrv_unref(). | |
2256 | */ | |
2257 | void bdrv_set_backing_hd(BlockDriverState *bs, BlockDriverState *backing_hd, | |
2258 | Error **errp) | |
2259 | { | |
2260 | if (backing_hd) { | |
2261 | bdrv_ref(backing_hd); | |
2262 | } | |
2263 | ||
2264 | if (bs->backing) { | |
2265 | bdrv_unref_child(bs, bs->backing); | |
2266 | } | |
2267 | ||
2268 | if (!backing_hd) { | |
2269 | bs->backing = NULL; | |
2270 | goto out; | |
2271 | } | |
2272 | ||
2273 | bs->backing = bdrv_attach_child(bs, backing_hd, "backing", &child_backing, | |
2274 | errp); | |
2275 | if (!bs->backing) { | |
2276 | bdrv_unref(backing_hd); | |
2277 | } | |
2278 | ||
2279 | bdrv_refresh_filename(bs); | |
2280 | ||
2281 | out: | |
2282 | bdrv_refresh_limits(bs, NULL); | |
2283 | } | |
2284 | ||
2285 | /* | |
2286 | * Opens the backing file for a BlockDriverState if not yet open | |
2287 | * | |
2288 | * bdref_key specifies the key for the image's BlockdevRef in the options QDict. | |
2289 | * That QDict has to be flattened; therefore, if the BlockdevRef is a QDict | |
2290 | * itself, all options starting with "${bdref_key}." are considered part of the | |
2291 | * BlockdevRef. | |
2292 | * | |
2293 | * TODO Can this be unified with bdrv_open_image()? | |
2294 | */ | |
2295 | int bdrv_open_backing_file(BlockDriverState *bs, QDict *parent_options, | |
2296 | const char *bdref_key, Error **errp) | |
2297 | { | |
2298 | char *backing_filename = g_malloc0(PATH_MAX); | |
2299 | char *bdref_key_dot; | |
2300 | const char *reference = NULL; | |
2301 | int ret = 0; | |
2302 | BlockDriverState *backing_hd; | |
2303 | QDict *options; | |
2304 | QDict *tmp_parent_options = NULL; | |
2305 | Error *local_err = NULL; | |
2306 | ||
2307 | if (bs->backing != NULL) { | |
2308 | goto free_exit; | |
2309 | } | |
2310 | ||
2311 | /* NULL means an empty set of options */ | |
2312 | if (parent_options == NULL) { | |
2313 | tmp_parent_options = qdict_new(); | |
2314 | parent_options = tmp_parent_options; | |
2315 | } | |
2316 | ||
2317 | bs->open_flags &= ~BDRV_O_NO_BACKING; | |
2318 | ||
2319 | bdref_key_dot = g_strdup_printf("%s.", bdref_key); | |
2320 | qdict_extract_subqdict(parent_options, &options, bdref_key_dot); | |
2321 | g_free(bdref_key_dot); | |
2322 | ||
2323 | /* | |
2324 | * Caution: while qdict_get_try_str() is fine, getting non-string | |
2325 | * types would require more care. When @parent_options come from | |
2326 | * -blockdev or blockdev_add, its members are typed according to | |
2327 | * the QAPI schema, but when they come from -drive, they're all | |
2328 | * QString. | |
2329 | */ | |
2330 | reference = qdict_get_try_str(parent_options, bdref_key); | |
2331 | if (reference || qdict_haskey(options, "file.filename")) { | |
2332 | backing_filename[0] = '\0'; | |
2333 | } else if (bs->backing_file[0] == '\0' && qdict_size(options) == 0) { | |
2334 | qobject_unref(options); | |
2335 | goto free_exit; | |
2336 | } else { | |
2337 | bdrv_get_full_backing_filename(bs, backing_filename, PATH_MAX, | |
2338 | &local_err); | |
2339 | if (local_err) { | |
2340 | ret = -EINVAL; | |
2341 | error_propagate(errp, local_err); | |
2342 | qobject_unref(options); | |
2343 | goto free_exit; | |
2344 | } | |
2345 | } | |
2346 | ||
2347 | if (!bs->drv || !bs->drv->supports_backing) { | |
2348 | ret = -EINVAL; | |
2349 | error_setg(errp, "Driver doesn't support backing files"); | |
2350 | qobject_unref(options); | |
2351 | goto free_exit; | |
2352 | } | |
2353 | ||
2354 | if (!reference && | |
2355 | bs->backing_format[0] != '\0' && !qdict_haskey(options, "driver")) { | |
2356 | qdict_put_str(options, "driver", bs->backing_format); | |
2357 | } | |
2358 | ||
2359 | backing_hd = bdrv_open_inherit(*backing_filename ? backing_filename : NULL, | |
2360 | reference, options, 0, bs, &child_backing, | |
2361 | errp); | |
2362 | if (!backing_hd) { | |
2363 | bs->open_flags |= BDRV_O_NO_BACKING; | |
2364 | error_prepend(errp, "Could not open backing file: "); | |
2365 | ret = -EINVAL; | |
2366 | goto free_exit; | |
2367 | } | |
2368 | bdrv_set_aio_context(backing_hd, bdrv_get_aio_context(bs)); | |
2369 | ||
2370 | /* Hook up the backing file link; drop our reference, bs owns the | |
2371 | * backing_hd reference now */ | |
2372 | bdrv_set_backing_hd(bs, backing_hd, &local_err); | |
2373 | bdrv_unref(backing_hd); | |
2374 | if (local_err) { | |
2375 | error_propagate(errp, local_err); | |
2376 | ret = -EINVAL; | |
2377 | goto free_exit; | |
2378 | } | |
2379 | ||
2380 | qdict_del(parent_options, bdref_key); | |
2381 | ||
2382 | free_exit: | |
2383 | g_free(backing_filename); | |
2384 | qobject_unref(tmp_parent_options); | |
2385 | return ret; | |
2386 | } | |
2387 | ||
2388 | static BlockDriverState * | |
2389 | bdrv_open_child_bs(const char *filename, QDict *options, const char *bdref_key, | |
2390 | BlockDriverState *parent, const BdrvChildRole *child_role, | |
2391 | bool allow_none, Error **errp) | |
2392 | { | |
2393 | BlockDriverState *bs = NULL; | |
2394 | QDict *image_options; | |
2395 | char *bdref_key_dot; | |
2396 | const char *reference; | |
2397 | ||
2398 | assert(child_role != NULL); | |
2399 | ||
2400 | bdref_key_dot = g_strdup_printf("%s.", bdref_key); | |
2401 | qdict_extract_subqdict(options, &image_options, bdref_key_dot); | |
2402 | g_free(bdref_key_dot); | |
2403 | ||
2404 | /* | |
2405 | * Caution: while qdict_get_try_str() is fine, getting non-string | |
2406 | * types would require more care. When @options come from | |
2407 | * -blockdev or blockdev_add, its members are typed according to | |
2408 | * the QAPI schema, but when they come from -drive, they're all | |
2409 | * QString. | |
2410 | */ | |
2411 | reference = qdict_get_try_str(options, bdref_key); | |
2412 | if (!filename && !reference && !qdict_size(image_options)) { | |
2413 | if (!allow_none) { | |
2414 | error_setg(errp, "A block device must be specified for \"%s\"", | |
2415 | bdref_key); | |
2416 | } | |
2417 | qobject_unref(image_options); | |
2418 | goto done; | |
2419 | } | |
2420 | ||
2421 | bs = bdrv_open_inherit(filename, reference, image_options, 0, | |
2422 | parent, child_role, errp); | |
2423 | if (!bs) { | |
2424 | goto done; | |
2425 | } | |
2426 | ||
2427 | done: | |
2428 | qdict_del(options, bdref_key); | |
2429 | return bs; | |
2430 | } | |
2431 | ||
2432 | /* | |
2433 | * Opens a disk image whose options are given as BlockdevRef in another block | |
2434 | * device's options. | |
2435 | * | |
2436 | * If allow_none is true, no image will be opened if filename is false and no | |
2437 | * BlockdevRef is given. NULL will be returned, but errp remains unset. | |
2438 | * | |
2439 | * bdrev_key specifies the key for the image's BlockdevRef in the options QDict. | |
2440 | * That QDict has to be flattened; therefore, if the BlockdevRef is a QDict | |
2441 | * itself, all options starting with "${bdref_key}." are considered part of the | |
2442 | * BlockdevRef. | |
2443 | * | |
2444 | * The BlockdevRef will be removed from the options QDict. | |
2445 | */ | |
2446 | BdrvChild *bdrv_open_child(const char *filename, | |
2447 | QDict *options, const char *bdref_key, | |
2448 | BlockDriverState *parent, | |
2449 | const BdrvChildRole *child_role, | |
2450 | bool allow_none, Error **errp) | |
2451 | { | |
2452 | BdrvChild *c; | |
2453 | BlockDriverState *bs; | |
2454 | ||
2455 | bs = bdrv_open_child_bs(filename, options, bdref_key, parent, child_role, | |
2456 | allow_none, errp); | |
2457 | if (bs == NULL) { | |
2458 | return NULL; | |
2459 | } | |
2460 | ||
2461 | c = bdrv_attach_child(parent, bs, bdref_key, child_role, errp); | |
2462 | if (!c) { | |
2463 | bdrv_unref(bs); | |
2464 | return NULL; | |
2465 | } | |
2466 | ||
2467 | return c; | |
2468 | } | |
2469 | ||
2470 | /* TODO Future callers may need to specify parent/child_role in order for | |
2471 | * option inheritance to work. Existing callers use it for the root node. */ | |
2472 | BlockDriverState *bdrv_open_blockdev_ref(BlockdevRef *ref, Error **errp) | |
2473 | { | |
2474 | BlockDriverState *bs = NULL; | |
2475 | Error *local_err = NULL; | |
2476 | QObject *obj = NULL; | |
2477 | QDict *qdict = NULL; | |
2478 | const char *reference = NULL; | |
2479 | Visitor *v = NULL; | |
2480 | ||
2481 | if (ref->type == QTYPE_QSTRING) { | |
2482 | reference = ref->u.reference; | |
2483 | } else { | |
2484 | BlockdevOptions *options = &ref->u.definition; | |
2485 | assert(ref->type == QTYPE_QDICT); | |
2486 | ||
2487 | v = qobject_output_visitor_new(&obj); | |
2488 | visit_type_BlockdevOptions(v, NULL, &options, &local_err); | |
2489 | if (local_err) { | |
2490 | error_propagate(errp, local_err); | |
2491 | goto fail; | |
2492 | } | |
2493 | visit_complete(v, &obj); | |
2494 | ||
2495 | qdict = qobject_to(QDict, obj); | |
2496 | qdict_flatten(qdict); | |
2497 | ||
2498 | /* bdrv_open_inherit() defaults to the values in bdrv_flags (for | |
2499 | * compatibility with other callers) rather than what we want as the | |
2500 | * real defaults. Apply the defaults here instead. */ | |
2501 | qdict_set_default_str(qdict, BDRV_OPT_CACHE_DIRECT, "off"); | |
2502 | qdict_set_default_str(qdict, BDRV_OPT_CACHE_NO_FLUSH, "off"); | |
2503 | qdict_set_default_str(qdict, BDRV_OPT_READ_ONLY, "off"); | |
2504 | qdict_set_default_str(qdict, BDRV_OPT_AUTO_READ_ONLY, "off"); | |
2505 | ||
2506 | } | |
2507 | ||
2508 | bs = bdrv_open_inherit(NULL, reference, qdict, 0, NULL, NULL, errp); | |
2509 | obj = NULL; | |
2510 | ||
2511 | fail: | |
2512 | qobject_unref(obj); | |
2513 | visit_free(v); | |
2514 | return bs; | |
2515 | } | |
2516 | ||
2517 | static BlockDriverState *bdrv_append_temp_snapshot(BlockDriverState *bs, | |
2518 | int flags, | |
2519 | QDict *snapshot_options, | |
2520 | Error **errp) | |
2521 | { | |
2522 | /* TODO: extra byte is a hack to ensure MAX_PATH space on Windows. */ | |
2523 | char *tmp_filename = g_malloc0(PATH_MAX + 1); | |
2524 | int64_t total_size; | |
2525 | QemuOpts *opts = NULL; | |
2526 | BlockDriverState *bs_snapshot = NULL; | |
2527 | Error *local_err = NULL; | |
2528 | int ret; | |
2529 | ||
2530 | /* if snapshot, we create a temporary backing file and open it | |
2531 | instead of opening 'filename' directly */ | |
2532 | ||
2533 | /* Get the required size from the image */ | |
2534 | total_size = bdrv_getlength(bs); | |
2535 | if (total_size < 0) { | |
2536 | error_setg_errno(errp, -total_size, "Could not get image size"); | |
2537 | goto out; | |
2538 | } | |
2539 | ||
2540 | /* Create the temporary image */ | |
2541 | ret = get_tmp_filename(tmp_filename, PATH_MAX + 1); | |
2542 | if (ret < 0) { | |
2543 | error_setg_errno(errp, -ret, "Could not get temporary filename"); | |
2544 | goto out; | |
2545 | } | |
2546 | ||
2547 | opts = qemu_opts_create(bdrv_qcow2.create_opts, NULL, 0, | |
2548 | &error_abort); | |
2549 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, total_size, &error_abort); | |
2550 | ret = bdrv_create(&bdrv_qcow2, tmp_filename, opts, errp); | |
2551 | qemu_opts_del(opts); | |
2552 | if (ret < 0) { | |
2553 | error_prepend(errp, "Could not create temporary overlay '%s': ", | |
2554 | tmp_filename); | |
2555 | goto out; | |
2556 | } | |
2557 | ||
2558 | /* Prepare options QDict for the temporary file */ | |
2559 | qdict_put_str(snapshot_options, "file.driver", "file"); | |
2560 | qdict_put_str(snapshot_options, "file.filename", tmp_filename); | |
2561 | qdict_put_str(snapshot_options, "driver", "qcow2"); | |
2562 | ||
2563 | bs_snapshot = bdrv_open(NULL, NULL, snapshot_options, flags, errp); | |
2564 | snapshot_options = NULL; | |
2565 | if (!bs_snapshot) { | |
2566 | goto out; | |
2567 | } | |
2568 | ||
2569 | /* bdrv_append() consumes a strong reference to bs_snapshot | |
2570 | * (i.e. it will call bdrv_unref() on it) even on error, so in | |
2571 | * order to be able to return one, we have to increase | |
2572 | * bs_snapshot's refcount here */ | |
2573 | bdrv_ref(bs_snapshot); | |
2574 | bdrv_append(bs_snapshot, bs, &local_err); | |
2575 | if (local_err) { | |
2576 | error_propagate(errp, local_err); | |
2577 | bs_snapshot = NULL; | |
2578 | goto out; | |
2579 | } | |
2580 | ||
2581 | out: | |
2582 | qobject_unref(snapshot_options); | |
2583 | g_free(tmp_filename); | |
2584 | return bs_snapshot; | |
2585 | } | |
2586 | ||
2587 | /* | |
2588 | * Opens a disk image (raw, qcow2, vmdk, ...) | |
2589 | * | |
2590 | * options is a QDict of options to pass to the block drivers, or NULL for an | |
2591 | * empty set of options. The reference to the QDict belongs to the block layer | |
2592 | * after the call (even on failure), so if the caller intends to reuse the | |
2593 | * dictionary, it needs to use qobject_ref() before calling bdrv_open. | |
2594 | * | |
2595 | * If *pbs is NULL, a new BDS will be created with a pointer to it stored there. | |
2596 | * If it is not NULL, the referenced BDS will be reused. | |
2597 | * | |
2598 | * The reference parameter may be used to specify an existing block device which | |
2599 | * should be opened. If specified, neither options nor a filename may be given, | |
2600 | * nor can an existing BDS be reused (that is, *pbs has to be NULL). | |
2601 | */ | |
2602 | static BlockDriverState *bdrv_open_inherit(const char *filename, | |
2603 | const char *reference, | |
2604 | QDict *options, int flags, | |
2605 | BlockDriverState *parent, | |
2606 | const BdrvChildRole *child_role, | |
2607 | Error **errp) | |
2608 | { | |
2609 | int ret; | |
2610 | BlockBackend *file = NULL; | |
2611 | BlockDriverState *bs; | |
2612 | BlockDriver *drv = NULL; | |
2613 | BdrvChild *child; | |
2614 | const char *drvname; | |
2615 | const char *backing; | |
2616 | Error *local_err = NULL; | |
2617 | QDict *snapshot_options = NULL; | |
2618 | int snapshot_flags = 0; | |
2619 | ||
2620 | assert(!child_role || !flags); | |
2621 | assert(!child_role == !parent); | |
2622 | ||
2623 | if (reference) { | |
2624 | bool options_non_empty = options ? qdict_size(options) : false; | |
2625 | qobject_unref(options); | |
2626 | ||
2627 | if (filename || options_non_empty) { | |
2628 | error_setg(errp, "Cannot reference an existing block device with " | |
2629 | "additional options or a new filename"); | |
2630 | return NULL; | |
2631 | } | |
2632 | ||
2633 | bs = bdrv_lookup_bs(reference, reference, errp); | |
2634 | if (!bs) { | |
2635 | return NULL; | |
2636 | } | |
2637 | ||
2638 | bdrv_ref(bs); | |
2639 | return bs; | |
2640 | } | |
2641 | ||
2642 | bs = bdrv_new(); | |
2643 | ||
2644 | /* NULL means an empty set of options */ | |
2645 | if (options == NULL) { | |
2646 | options = qdict_new(); | |
2647 | } | |
2648 | ||
2649 | /* json: syntax counts as explicit options, as if in the QDict */ | |
2650 | parse_json_protocol(options, &filename, &local_err); | |
2651 | if (local_err) { | |
2652 | goto fail; | |
2653 | } | |
2654 | ||
2655 | bs->explicit_options = qdict_clone_shallow(options); | |
2656 | ||
2657 | if (child_role) { | |
2658 | bs->inherits_from = parent; | |
2659 | child_role->inherit_options(&flags, options, | |
2660 | parent->open_flags, parent->options); | |
2661 | } | |
2662 | ||
2663 | ret = bdrv_fill_options(&options, filename, &flags, &local_err); | |
2664 | if (local_err) { | |
2665 | goto fail; | |
2666 | } | |
2667 | ||
2668 | /* | |
2669 | * Set the BDRV_O_RDWR and BDRV_O_ALLOW_RDWR flags. | |
2670 | * Caution: getting a boolean member of @options requires care. | |
2671 | * When @options come from -blockdev or blockdev_add, members are | |
2672 | * typed according to the QAPI schema, but when they come from | |
2673 | * -drive, they're all QString. | |
2674 | */ | |
2675 | if (g_strcmp0(qdict_get_try_str(options, BDRV_OPT_READ_ONLY), "on") && | |
2676 | !qdict_get_try_bool(options, BDRV_OPT_READ_ONLY, false)) { | |
2677 | flags |= (BDRV_O_RDWR | BDRV_O_ALLOW_RDWR); | |
2678 | } else { | |
2679 | flags &= ~BDRV_O_RDWR; | |
2680 | } | |
2681 | ||
2682 | if (flags & BDRV_O_SNAPSHOT) { | |
2683 | snapshot_options = qdict_new(); | |
2684 | bdrv_temp_snapshot_options(&snapshot_flags, snapshot_options, | |
2685 | flags, options); | |
2686 | /* Let bdrv_backing_options() override "read-only" */ | |
2687 | qdict_del(options, BDRV_OPT_READ_ONLY); | |
2688 | bdrv_backing_options(&flags, options, flags, options); | |
2689 | } | |
2690 | ||
2691 | bs->open_flags = flags; | |
2692 | bs->options = options; | |
2693 | options = qdict_clone_shallow(options); | |
2694 | ||
2695 | /* Find the right image format driver */ | |
2696 | /* See cautionary note on accessing @options above */ | |
2697 | drvname = qdict_get_try_str(options, "driver"); | |
2698 | if (drvname) { | |
2699 | drv = bdrv_find_format(drvname); | |
2700 | if (!drv) { | |
2701 | error_setg(errp, "Unknown driver: '%s'", drvname); | |
2702 | goto fail; | |
2703 | } | |
2704 | } | |
2705 | ||
2706 | assert(drvname || !(flags & BDRV_O_PROTOCOL)); | |
2707 | ||
2708 | /* See cautionary note on accessing @options above */ | |
2709 | backing = qdict_get_try_str(options, "backing"); | |
2710 | if (qobject_to(QNull, qdict_get(options, "backing")) != NULL || | |
2711 | (backing && *backing == '\0')) | |
2712 | { | |
2713 | if (backing) { | |
2714 | warn_report("Use of \"backing\": \"\" is deprecated; " | |
2715 | "use \"backing\": null instead"); | |
2716 | } | |
2717 | flags |= BDRV_O_NO_BACKING; | |
2718 | qdict_del(options, "backing"); | |
2719 | } | |
2720 | ||
2721 | /* Open image file without format layer. This BlockBackend is only used for | |
2722 | * probing, the block drivers will do their own bdrv_open_child() for the | |
2723 | * same BDS, which is why we put the node name back into options. */ | |
2724 | if ((flags & BDRV_O_PROTOCOL) == 0) { | |
2725 | BlockDriverState *file_bs; | |
2726 | ||
2727 | file_bs = bdrv_open_child_bs(filename, options, "file", bs, | |
2728 | &child_file, true, &local_err); | |
2729 | if (local_err) { | |
2730 | goto fail; | |
2731 | } | |
2732 | if (file_bs != NULL) { | |
2733 | /* Not requesting BLK_PERM_CONSISTENT_READ because we're only | |
2734 | * looking at the header to guess the image format. This works even | |
2735 | * in cases where a guest would not see a consistent state. */ | |
2736 | file = blk_new(0, BLK_PERM_ALL); | |
2737 | blk_insert_bs(file, file_bs, &local_err); | |
2738 | bdrv_unref(file_bs); | |
2739 | if (local_err) { | |
2740 | goto fail; | |
2741 | } | |
2742 | ||
2743 | qdict_put_str(options, "file", bdrv_get_node_name(file_bs)); | |
2744 | } | |
2745 | } | |
2746 | ||
2747 | /* Image format probing */ | |
2748 | bs->probed = !drv; | |
2749 | if (!drv && file) { | |
2750 | ret = find_image_format(file, filename, &drv, &local_err); | |
2751 | if (ret < 0) { | |
2752 | goto fail; | |
2753 | } | |
2754 | /* | |
2755 | * This option update would logically belong in bdrv_fill_options(), | |
2756 | * but we first need to open bs->file for the probing to work, while | |
2757 | * opening bs->file already requires the (mostly) final set of options | |
2758 | * so that cache mode etc. can be inherited. | |
2759 | * | |
2760 | * Adding the driver later is somewhat ugly, but it's not an option | |
2761 | * that would ever be inherited, so it's correct. We just need to make | |
2762 | * sure to update both bs->options (which has the full effective | |
2763 | * options for bs) and options (which has file.* already removed). | |
2764 | */ | |
2765 | qdict_put_str(bs->options, "driver", drv->format_name); | |
2766 | qdict_put_str(options, "driver", drv->format_name); | |
2767 | } else if (!drv) { | |
2768 | error_setg(errp, "Must specify either driver or file"); | |
2769 | goto fail; | |
2770 | } | |
2771 | ||
2772 | /* BDRV_O_PROTOCOL must be set iff a protocol BDS is about to be created */ | |
2773 | assert(!!(flags & BDRV_O_PROTOCOL) == !!drv->bdrv_file_open); | |
2774 | /* file must be NULL if a protocol BDS is about to be created | |
2775 | * (the inverse results in an error message from bdrv_open_common()) */ | |
2776 | assert(!(flags & BDRV_O_PROTOCOL) || !file); | |
2777 | ||
2778 | /* Open the image */ | |
2779 | ret = bdrv_open_common(bs, file, options, &local_err); | |
2780 | if (ret < 0) { | |
2781 | goto fail; | |
2782 | } | |
2783 | ||
2784 | if (file) { | |
2785 | blk_unref(file); | |
2786 | file = NULL; | |
2787 | } | |
2788 | ||
2789 | /* If there is a backing file, use it */ | |
2790 | if ((flags & BDRV_O_NO_BACKING) == 0) { | |
2791 | ret = bdrv_open_backing_file(bs, options, "backing", &local_err); | |
2792 | if (ret < 0) { | |
2793 | goto close_and_fail; | |
2794 | } | |
2795 | } | |
2796 | ||
2797 | /* Remove all children options and references | |
2798 | * from bs->options and bs->explicit_options */ | |
2799 | QLIST_FOREACH(child, &bs->children, next) { | |
2800 | char *child_key_dot; | |
2801 | child_key_dot = g_strdup_printf("%s.", child->name); | |
2802 | qdict_extract_subqdict(bs->explicit_options, NULL, child_key_dot); | |
2803 | qdict_extract_subqdict(bs->options, NULL, child_key_dot); | |
2804 | qdict_del(bs->explicit_options, child->name); | |
2805 | qdict_del(bs->options, child->name); | |
2806 | g_free(child_key_dot); | |
2807 | } | |
2808 | ||
2809 | bdrv_refresh_filename(bs); | |
2810 | ||
2811 | /* Check if any unknown options were used */ | |
2812 | if (qdict_size(options) != 0) { | |
2813 | const QDictEntry *entry = qdict_first(options); | |
2814 | if (flags & BDRV_O_PROTOCOL) { | |
2815 | error_setg(errp, "Block protocol '%s' doesn't support the option " | |
2816 | "'%s'", drv->format_name, entry->key); | |
2817 | } else { | |
2818 | error_setg(errp, | |
2819 | "Block format '%s' does not support the option '%s'", | |
2820 | drv->format_name, entry->key); | |
2821 | } | |
2822 | ||
2823 | goto close_and_fail; | |
2824 | } | |
2825 | ||
2826 | bdrv_parent_cb_change_media(bs, true); | |
2827 | ||
2828 | qobject_unref(options); | |
2829 | options = NULL; | |
2830 | ||
2831 | /* For snapshot=on, create a temporary qcow2 overlay. bs points to the | |
2832 | * temporary snapshot afterwards. */ | |
2833 | if (snapshot_flags) { | |
2834 | BlockDriverState *snapshot_bs; | |
2835 | snapshot_bs = bdrv_append_temp_snapshot(bs, snapshot_flags, | |
2836 | snapshot_options, &local_err); | |
2837 | snapshot_options = NULL; | |
2838 | if (local_err) { | |
2839 | goto close_and_fail; | |
2840 | } | |
2841 | /* We are not going to return bs but the overlay on top of it | |
2842 | * (snapshot_bs); thus, we have to drop the strong reference to bs | |
2843 | * (which we obtained by calling bdrv_new()). bs will not be deleted, | |
2844 | * though, because the overlay still has a reference to it. */ | |
2845 | bdrv_unref(bs); | |
2846 | bs = snapshot_bs; | |
2847 | } | |
2848 | ||
2849 | return bs; | |
2850 | ||
2851 | fail: | |
2852 | blk_unref(file); | |
2853 | qobject_unref(snapshot_options); | |
2854 | qobject_unref(bs->explicit_options); | |
2855 | qobject_unref(bs->options); | |
2856 | qobject_unref(options); | |
2857 | bs->options = NULL; | |
2858 | bs->explicit_options = NULL; | |
2859 | bdrv_unref(bs); | |
2860 | error_propagate(errp, local_err); | |
2861 | return NULL; | |
2862 | ||
2863 | close_and_fail: | |
2864 | bdrv_unref(bs); | |
2865 | qobject_unref(snapshot_options); | |
2866 | qobject_unref(options); | |
2867 | error_propagate(errp, local_err); | |
2868 | return NULL; | |
2869 | } | |
2870 | ||
2871 | BlockDriverState *bdrv_open(const char *filename, const char *reference, | |
2872 | QDict *options, int flags, Error **errp) | |
2873 | { | |
2874 | return bdrv_open_inherit(filename, reference, options, flags, NULL, | |
2875 | NULL, errp); | |
2876 | } | |
2877 | ||
2878 | /* | |
2879 | * Adds a BlockDriverState to a simple queue for an atomic, transactional | |
2880 | * reopen of multiple devices. | |
2881 | * | |
2882 | * bs_queue can either be an existing BlockReopenQueue that has had QSIMPLE_INIT | |
2883 | * already performed, or alternatively may be NULL a new BlockReopenQueue will | |
2884 | * be created and initialized. This newly created BlockReopenQueue should be | |
2885 | * passed back in for subsequent calls that are intended to be of the same | |
2886 | * atomic 'set'. | |
2887 | * | |
2888 | * bs is the BlockDriverState to add to the reopen queue. | |
2889 | * | |
2890 | * options contains the changed options for the associated bs | |
2891 | * (the BlockReopenQueue takes ownership) | |
2892 | * | |
2893 | * flags contains the open flags for the associated bs | |
2894 | * | |
2895 | * returns a pointer to bs_queue, which is either the newly allocated | |
2896 | * bs_queue, or the existing bs_queue being used. | |
2897 | * | |
2898 | * bs must be drained between bdrv_reopen_queue() and bdrv_reopen_multiple(). | |
2899 | */ | |
2900 | static BlockReopenQueue *bdrv_reopen_queue_child(BlockReopenQueue *bs_queue, | |
2901 | BlockDriverState *bs, | |
2902 | QDict *options, | |
2903 | int flags, | |
2904 | const BdrvChildRole *role, | |
2905 | QDict *parent_options, | |
2906 | int parent_flags) | |
2907 | { | |
2908 | assert(bs != NULL); | |
2909 | ||
2910 | BlockReopenQueueEntry *bs_entry; | |
2911 | BdrvChild *child; | |
2912 | QDict *old_options, *explicit_options; | |
2913 | ||
2914 | /* Make sure that the caller remembered to use a drained section. This is | |
2915 | * important to avoid graph changes between the recursive queuing here and | |
2916 | * bdrv_reopen_multiple(). */ | |
2917 | assert(bs->quiesce_counter > 0); | |
2918 | ||
2919 | if (bs_queue == NULL) { | |
2920 | bs_queue = g_new0(BlockReopenQueue, 1); | |
2921 | QSIMPLEQ_INIT(bs_queue); | |
2922 | } | |
2923 | ||
2924 | if (!options) { | |
2925 | options = qdict_new(); | |
2926 | } | |
2927 | ||
2928 | /* Check if this BlockDriverState is already in the queue */ | |
2929 | QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) { | |
2930 | if (bs == bs_entry->state.bs) { | |
2931 | break; | |
2932 | } | |
2933 | } | |
2934 | ||
2935 | /* | |
2936 | * Precedence of options: | |
2937 | * 1. Explicitly passed in options (highest) | |
2938 | * 2. Set in flags (only for top level) | |
2939 | * 3. Retained from explicitly set options of bs | |
2940 | * 4. Inherited from parent node | |
2941 | * 5. Retained from effective options of bs | |
2942 | */ | |
2943 | ||
2944 | if (!parent_options) { | |
2945 | /* | |
2946 | * Any setting represented by flags is always updated. If the | |
2947 | * corresponding QDict option is set, it takes precedence. Otherwise | |
2948 | * the flag is translated into a QDict option. The old setting of bs is | |
2949 | * not considered. | |
2950 | */ | |
2951 | update_options_from_flags(options, flags); | |
2952 | } | |
2953 | ||
2954 | /* Old explicitly set values (don't overwrite by inherited value) */ | |
2955 | if (bs_entry) { | |
2956 | old_options = qdict_clone_shallow(bs_entry->state.explicit_options); | |
2957 | } else { | |
2958 | old_options = qdict_clone_shallow(bs->explicit_options); | |
2959 | } | |
2960 | bdrv_join_options(bs, options, old_options); | |
2961 | qobject_unref(old_options); | |
2962 | ||
2963 | explicit_options = qdict_clone_shallow(options); | |
2964 | ||
2965 | /* Inherit from parent node */ | |
2966 | if (parent_options) { | |
2967 | QemuOpts *opts; | |
2968 | QDict *options_copy; | |
2969 | assert(!flags); | |
2970 | role->inherit_options(&flags, options, parent_flags, parent_options); | |
2971 | options_copy = qdict_clone_shallow(options); | |
2972 | opts = qemu_opts_create(&bdrv_runtime_opts, NULL, 0, &error_abort); | |
2973 | qemu_opts_absorb_qdict(opts, options_copy, NULL); | |
2974 | update_flags_from_options(&flags, opts); | |
2975 | qemu_opts_del(opts); | |
2976 | qobject_unref(options_copy); | |
2977 | } | |
2978 | ||
2979 | /* Old values are used for options that aren't set yet */ | |
2980 | old_options = qdict_clone_shallow(bs->options); | |
2981 | bdrv_join_options(bs, options, old_options); | |
2982 | qobject_unref(old_options); | |
2983 | ||
2984 | /* bdrv_open_inherit() sets and clears some additional flags internally */ | |
2985 | flags &= ~BDRV_O_PROTOCOL; | |
2986 | if (flags & BDRV_O_RDWR) { | |
2987 | flags |= BDRV_O_ALLOW_RDWR; | |
2988 | } | |
2989 | ||
2990 | if (!bs_entry) { | |
2991 | bs_entry = g_new0(BlockReopenQueueEntry, 1); | |
2992 | QSIMPLEQ_INSERT_TAIL(bs_queue, bs_entry, entry); | |
2993 | } else { | |
2994 | qobject_unref(bs_entry->state.options); | |
2995 | qobject_unref(bs_entry->state.explicit_options); | |
2996 | } | |
2997 | ||
2998 | bs_entry->state.bs = bs; | |
2999 | bs_entry->state.options = options; | |
3000 | bs_entry->state.explicit_options = explicit_options; | |
3001 | bs_entry->state.flags = flags; | |
3002 | ||
3003 | /* This needs to be overwritten in bdrv_reopen_prepare() */ | |
3004 | bs_entry->state.perm = UINT64_MAX; | |
3005 | bs_entry->state.shared_perm = 0; | |
3006 | ||
3007 | QLIST_FOREACH(child, &bs->children, next) { | |
3008 | QDict *new_child_options; | |
3009 | char *child_key_dot; | |
3010 | ||
3011 | /* reopen can only change the options of block devices that were | |
3012 | * implicitly created and inherited options. For other (referenced) | |
3013 | * block devices, a syntax like "backing.foo" results in an error. */ | |
3014 | if (child->bs->inherits_from != bs) { | |
3015 | continue; | |
3016 | } | |
3017 | ||
3018 | child_key_dot = g_strdup_printf("%s.", child->name); | |
3019 | qdict_extract_subqdict(explicit_options, NULL, child_key_dot); | |
3020 | qdict_extract_subqdict(options, &new_child_options, child_key_dot); | |
3021 | g_free(child_key_dot); | |
3022 | ||
3023 | bdrv_reopen_queue_child(bs_queue, child->bs, new_child_options, 0, | |
3024 | child->role, options, flags); | |
3025 | } | |
3026 | ||
3027 | return bs_queue; | |
3028 | } | |
3029 | ||
3030 | BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue, | |
3031 | BlockDriverState *bs, | |
3032 | QDict *options, int flags) | |
3033 | { | |
3034 | return bdrv_reopen_queue_child(bs_queue, bs, options, flags, | |
3035 | NULL, NULL, 0); | |
3036 | } | |
3037 | ||
3038 | /* | |
3039 | * Reopen multiple BlockDriverStates atomically & transactionally. | |
3040 | * | |
3041 | * The queue passed in (bs_queue) must have been built up previous | |
3042 | * via bdrv_reopen_queue(). | |
3043 | * | |
3044 | * Reopens all BDS specified in the queue, with the appropriate | |
3045 | * flags. All devices are prepared for reopen, and failure of any | |
3046 | * device will cause all device changes to be abandoned, and intermediate | |
3047 | * data cleaned up. | |
3048 | * | |
3049 | * If all devices prepare successfully, then the changes are committed | |
3050 | * to all devices. | |
3051 | * | |
3052 | * All affected nodes must be drained between bdrv_reopen_queue() and | |
3053 | * bdrv_reopen_multiple(). | |
3054 | */ | |
3055 | int bdrv_reopen_multiple(AioContext *ctx, BlockReopenQueue *bs_queue, Error **errp) | |
3056 | { | |
3057 | int ret = -1; | |
3058 | BlockReopenQueueEntry *bs_entry, *next; | |
3059 | Error *local_err = NULL; | |
3060 | ||
3061 | assert(bs_queue != NULL); | |
3062 | ||
3063 | QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) { | |
3064 | assert(bs_entry->state.bs->quiesce_counter > 0); | |
3065 | if (bdrv_reopen_prepare(&bs_entry->state, bs_queue, &local_err)) { | |
3066 | error_propagate(errp, local_err); | |
3067 | goto cleanup; | |
3068 | } | |
3069 | bs_entry->prepared = true; | |
3070 | } | |
3071 | ||
3072 | /* If we reach this point, we have success and just need to apply the | |
3073 | * changes | |
3074 | */ | |
3075 | QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) { | |
3076 | bdrv_reopen_commit(&bs_entry->state); | |
3077 | } | |
3078 | ||
3079 | ret = 0; | |
3080 | ||
3081 | cleanup: | |
3082 | QSIMPLEQ_FOREACH_SAFE(bs_entry, bs_queue, entry, next) { | |
3083 | if (ret) { | |
3084 | if (bs_entry->prepared) { | |
3085 | bdrv_reopen_abort(&bs_entry->state); | |
3086 | } | |
3087 | qobject_unref(bs_entry->state.explicit_options); | |
3088 | qobject_unref(bs_entry->state.options); | |
3089 | } | |
3090 | g_free(bs_entry); | |
3091 | } | |
3092 | g_free(bs_queue); | |
3093 | ||
3094 | return ret; | |
3095 | } | |
3096 | ||
3097 | ||
3098 | /* Reopen a single BlockDriverState with the specified flags. */ | |
3099 | int bdrv_reopen(BlockDriverState *bs, int bdrv_flags, Error **errp) | |
3100 | { | |
3101 | int ret = -1; | |
3102 | Error *local_err = NULL; | |
3103 | BlockReopenQueue *queue; | |
3104 | ||
3105 | bdrv_subtree_drained_begin(bs); | |
3106 | ||
3107 | queue = bdrv_reopen_queue(NULL, bs, NULL, bdrv_flags); | |
3108 | ret = bdrv_reopen_multiple(bdrv_get_aio_context(bs), queue, &local_err); | |
3109 | if (local_err != NULL) { | |
3110 | error_propagate(errp, local_err); | |
3111 | } | |
3112 | ||
3113 | bdrv_subtree_drained_end(bs); | |
3114 | ||
3115 | return ret; | |
3116 | } | |
3117 | ||
3118 | static BlockReopenQueueEntry *find_parent_in_reopen_queue(BlockReopenQueue *q, | |
3119 | BdrvChild *c) | |
3120 | { | |
3121 | BlockReopenQueueEntry *entry; | |
3122 | ||
3123 | QSIMPLEQ_FOREACH(entry, q, entry) { | |
3124 | BlockDriverState *bs = entry->state.bs; | |
3125 | BdrvChild *child; | |
3126 | ||
3127 | QLIST_FOREACH(child, &bs->children, next) { | |
3128 | if (child == c) { | |
3129 | return entry; | |
3130 | } | |
3131 | } | |
3132 | } | |
3133 | ||
3134 | return NULL; | |
3135 | } | |
3136 | ||
3137 | static void bdrv_reopen_perm(BlockReopenQueue *q, BlockDriverState *bs, | |
3138 | uint64_t *perm, uint64_t *shared) | |
3139 | { | |
3140 | BdrvChild *c; | |
3141 | BlockReopenQueueEntry *parent; | |
3142 | uint64_t cumulative_perms = 0; | |
3143 | uint64_t cumulative_shared_perms = BLK_PERM_ALL; | |
3144 | ||
3145 | QLIST_FOREACH(c, &bs->parents, next_parent) { | |
3146 | parent = find_parent_in_reopen_queue(q, c); | |
3147 | if (!parent) { | |
3148 | cumulative_perms |= c->perm; | |
3149 | cumulative_shared_perms &= c->shared_perm; | |
3150 | } else { | |
3151 | uint64_t nperm, nshared; | |
3152 | ||
3153 | bdrv_child_perm(parent->state.bs, bs, c, c->role, q, | |
3154 | parent->state.perm, parent->state.shared_perm, | |
3155 | &nperm, &nshared); | |
3156 | ||
3157 | cumulative_perms |= nperm; | |
3158 | cumulative_shared_perms &= nshared; | |
3159 | } | |
3160 | } | |
3161 | *perm = cumulative_perms; | |
3162 | *shared = cumulative_shared_perms; | |
3163 | } | |
3164 | ||
3165 | /* | |
3166 | * Prepares a BlockDriverState for reopen. All changes are staged in the | |
3167 | * 'opaque' field of the BDRVReopenState, which is used and allocated by | |
3168 | * the block driver layer .bdrv_reopen_prepare() | |
3169 | * | |
3170 | * bs is the BlockDriverState to reopen | |
3171 | * flags are the new open flags | |
3172 | * queue is the reopen queue | |
3173 | * | |
3174 | * Returns 0 on success, non-zero on error. On error errp will be set | |
3175 | * as well. | |
3176 | * | |
3177 | * On failure, bdrv_reopen_abort() will be called to clean up any data. | |
3178 | * It is the responsibility of the caller to then call the abort() or | |
3179 | * commit() for any other BDS that have been left in a prepare() state | |
3180 | * | |
3181 | */ | |
3182 | int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue, | |
3183 | Error **errp) | |
3184 | { | |
3185 | int ret = -1; | |
3186 | Error *local_err = NULL; | |
3187 | BlockDriver *drv; | |
3188 | QemuOpts *opts; | |
3189 | QDict *orig_reopen_opts; | |
3190 | char *discard = NULL; | |
3191 | bool read_only; | |
3192 | ||
3193 | assert(reopen_state != NULL); | |
3194 | assert(reopen_state->bs->drv != NULL); | |
3195 | drv = reopen_state->bs->drv; | |
3196 | ||
3197 | /* This function and each driver's bdrv_reopen_prepare() remove | |
3198 | * entries from reopen_state->options as they are processed, so | |
3199 | * we need to make a copy of the original QDict. */ | |
3200 | orig_reopen_opts = qdict_clone_shallow(reopen_state->options); | |
3201 | ||
3202 | /* Process generic block layer options */ | |
3203 | opts = qemu_opts_create(&bdrv_runtime_opts, NULL, 0, &error_abort); | |
3204 | qemu_opts_absorb_qdict(opts, reopen_state->options, &local_err); | |
3205 | if (local_err) { | |
3206 | error_propagate(errp, local_err); | |
3207 | ret = -EINVAL; | |
3208 | goto error; | |
3209 | } | |
3210 | ||
3211 | update_flags_from_options(&reopen_state->flags, opts); | |
3212 | ||
3213 | discard = qemu_opt_get_del(opts, BDRV_OPT_DISCARD); | |
3214 | if (discard != NULL) { | |
3215 | if (bdrv_parse_discard_flags(discard, &reopen_state->flags) != 0) { | |
3216 | error_setg(errp, "Invalid discard option"); | |
3217 | ret = -EINVAL; | |
3218 | goto error; | |
3219 | } | |
3220 | } | |
3221 | ||
3222 | reopen_state->detect_zeroes = | |
3223 | bdrv_parse_detect_zeroes(opts, reopen_state->flags, &local_err); | |
3224 | if (local_err) { | |
3225 | error_propagate(errp, local_err); | |
3226 | ret = -EINVAL; | |
3227 | goto error; | |
3228 | } | |
3229 | ||
3230 | /* All other options (including node-name and driver) must be unchanged. | |
3231 | * Put them back into the QDict, so that they are checked at the end | |
3232 | * of this function. */ | |
3233 | qemu_opts_to_qdict(opts, reopen_state->options); | |
3234 | ||
3235 | /* If we are to stay read-only, do not allow permission change | |
3236 | * to r/w. Attempting to set to r/w may fail if either BDRV_O_ALLOW_RDWR is | |
3237 | * not set, or if the BDS still has copy_on_read enabled */ | |
3238 | read_only = !(reopen_state->flags & BDRV_O_RDWR); | |
3239 | ret = bdrv_can_set_read_only(reopen_state->bs, read_only, true, &local_err); | |
3240 | if (local_err) { | |
3241 | error_propagate(errp, local_err); | |
3242 | goto error; | |
3243 | } | |
3244 | ||
3245 | /* Calculate required permissions after reopening */ | |
3246 | bdrv_reopen_perm(queue, reopen_state->bs, | |
3247 | &reopen_state->perm, &reopen_state->shared_perm); | |
3248 | ||
3249 | ret = bdrv_flush(reopen_state->bs); | |
3250 | if (ret) { | |
3251 | error_setg_errno(errp, -ret, "Error flushing drive"); | |
3252 | goto error; | |
3253 | } | |
3254 | ||
3255 | if (drv->bdrv_reopen_prepare) { | |
3256 | ret = drv->bdrv_reopen_prepare(reopen_state, queue, &local_err); | |
3257 | if (ret) { | |
3258 | if (local_err != NULL) { | |
3259 | error_propagate(errp, local_err); | |
3260 | } else { | |
3261 | error_setg(errp, "failed while preparing to reopen image '%s'", | |
3262 | reopen_state->bs->filename); | |
3263 | } | |
3264 | goto error; | |
3265 | } | |
3266 | } else { | |
3267 | /* It is currently mandatory to have a bdrv_reopen_prepare() | |
3268 | * handler for each supported drv. */ | |
3269 | error_setg(errp, "Block format '%s' used by node '%s' " | |
3270 | "does not support reopening files", drv->format_name, | |
3271 | bdrv_get_device_or_node_name(reopen_state->bs)); | |
3272 | ret = -1; | |
3273 | goto error; | |
3274 | } | |
3275 | ||
3276 | /* Options that are not handled are only okay if they are unchanged | |
3277 | * compared to the old state. It is expected that some options are only | |
3278 | * used for the initial open, but not reopen (e.g. filename) */ | |
3279 | if (qdict_size(reopen_state->options)) { | |
3280 | const QDictEntry *entry = qdict_first(reopen_state->options); | |
3281 | ||
3282 | do { | |
3283 | QObject *new = entry->value; | |
3284 | QObject *old = qdict_get(reopen_state->bs->options, entry->key); | |
3285 | ||
3286 | /* Allow child references (child_name=node_name) as long as they | |
3287 | * point to the current child (i.e. everything stays the same). */ | |
3288 | if (qobject_type(new) == QTYPE_QSTRING) { | |
3289 | BdrvChild *child; | |
3290 | QLIST_FOREACH(child, &reopen_state->bs->children, next) { | |
3291 | if (!strcmp(child->name, entry->key)) { | |
3292 | break; | |
3293 | } | |
3294 | } | |
3295 | ||
3296 | if (child) { | |
3297 | const char *str = qobject_get_try_str(new); | |
3298 | if (!strcmp(child->bs->node_name, str)) { | |
3299 | continue; /* Found child with this name, skip option */ | |
3300 | } | |
3301 | } | |
3302 | } | |
3303 | ||
3304 | /* | |
3305 | * TODO: When using -drive to specify blockdev options, all values | |
3306 | * will be strings; however, when using -blockdev, blockdev-add or | |
3307 | * filenames using the json:{} pseudo-protocol, they will be | |
3308 | * correctly typed. | |
3309 | * In contrast, reopening options are (currently) always strings | |
3310 | * (because you can only specify them through qemu-io; all other | |
3311 | * callers do not specify any options). | |
3312 | * Therefore, when using anything other than -drive to create a BDS, | |
3313 | * this cannot detect non-string options as unchanged, because | |
3314 | * qobject_is_equal() always returns false for objects of different | |
3315 | * type. In the future, this should be remedied by correctly typing | |
3316 | * all options. For now, this is not too big of an issue because | |
3317 | * the user can simply omit options which cannot be changed anyway, | |
3318 | * so they will stay unchanged. | |
3319 | */ | |
3320 | if (!qobject_is_equal(new, old)) { | |
3321 | error_setg(errp, "Cannot change the option '%s'", entry->key); | |
3322 | ret = -EINVAL; | |
3323 | goto error; | |
3324 | } | |
3325 | } while ((entry = qdict_next(reopen_state->options, entry))); | |
3326 | } | |
3327 | ||
3328 | ret = bdrv_check_perm(reopen_state->bs, queue, reopen_state->perm, | |
3329 | reopen_state->shared_perm, NULL, errp); | |
3330 | if (ret < 0) { | |
3331 | goto error; | |
3332 | } | |
3333 | ||
3334 | ret = 0; | |
3335 | ||
3336 | /* Restore the original reopen_state->options QDict */ | |
3337 | qobject_unref(reopen_state->options); | |
3338 | reopen_state->options = qobject_ref(orig_reopen_opts); | |
3339 | ||
3340 | error: | |
3341 | qemu_opts_del(opts); | |
3342 | qobject_unref(orig_reopen_opts); | |
3343 | g_free(discard); | |
3344 | return ret; | |
3345 | } | |
3346 | ||
3347 | /* | |
3348 | * Takes the staged changes for the reopen from bdrv_reopen_prepare(), and | |
3349 | * makes them final by swapping the staging BlockDriverState contents into | |
3350 | * the active BlockDriverState contents. | |
3351 | */ | |
3352 | void bdrv_reopen_commit(BDRVReopenState *reopen_state) | |
3353 | { | |
3354 | BlockDriver *drv; | |
3355 | BlockDriverState *bs; | |
3356 | BdrvChild *child; | |
3357 | bool old_can_write, new_can_write; | |
3358 | ||
3359 | assert(reopen_state != NULL); | |
3360 | bs = reopen_state->bs; | |
3361 | drv = bs->drv; | |
3362 | assert(drv != NULL); | |
3363 | ||
3364 | old_can_write = | |
3365 | !bdrv_is_read_only(bs) && !(bdrv_get_flags(bs) & BDRV_O_INACTIVE); | |
3366 | ||
3367 | /* If there are any driver level actions to take */ | |
3368 | if (drv->bdrv_reopen_commit) { | |
3369 | drv->bdrv_reopen_commit(reopen_state); | |
3370 | } | |
3371 | ||
3372 | /* set BDS specific flags now */ | |
3373 | qobject_unref(bs->explicit_options); | |
3374 | qobject_unref(bs->options); | |
3375 | ||
3376 | bs->explicit_options = reopen_state->explicit_options; | |
3377 | bs->options = reopen_state->options; | |
3378 | bs->open_flags = reopen_state->flags; | |
3379 | bs->read_only = !(reopen_state->flags & BDRV_O_RDWR); | |
3380 | bs->detect_zeroes = reopen_state->detect_zeroes; | |
3381 | ||
3382 | /* Remove child references from bs->options and bs->explicit_options. | |
3383 | * Child options were already removed in bdrv_reopen_queue_child() */ | |
3384 | QLIST_FOREACH(child, &bs->children, next) { | |
3385 | qdict_del(bs->explicit_options, child->name); | |
3386 | qdict_del(bs->options, child->name); | |
3387 | } | |
3388 | ||
3389 | bdrv_refresh_limits(bs, NULL); | |
3390 | ||
3391 | bdrv_set_perm(reopen_state->bs, reopen_state->perm, | |
3392 | reopen_state->shared_perm); | |
3393 | ||
3394 | new_can_write = | |
3395 | !bdrv_is_read_only(bs) && !(bdrv_get_flags(bs) & BDRV_O_INACTIVE); | |
3396 | if (!old_can_write && new_can_write && drv->bdrv_reopen_bitmaps_rw) { | |
3397 | Error *local_err = NULL; | |
3398 | if (drv->bdrv_reopen_bitmaps_rw(bs, &local_err) < 0) { | |
3399 | /* This is not fatal, bitmaps just left read-only, so all following | |
3400 | * writes will fail. User can remove read-only bitmaps to unblock | |
3401 | * writes. | |
3402 | */ | |
3403 | error_reportf_err(local_err, | |
3404 | "%s: Failed to make dirty bitmaps writable: ", | |
3405 | bdrv_get_node_name(bs)); | |
3406 | } | |
3407 | } | |
3408 | } | |
3409 | ||
3410 | /* | |
3411 | * Abort the reopen, and delete and free the staged changes in | |
3412 | * reopen_state | |
3413 | */ | |
3414 | void bdrv_reopen_abort(BDRVReopenState *reopen_state) | |
3415 | { | |
3416 | BlockDriver *drv; | |
3417 | ||
3418 | assert(reopen_state != NULL); | |
3419 | drv = reopen_state->bs->drv; | |
3420 | assert(drv != NULL); | |
3421 | ||
3422 | if (drv->bdrv_reopen_abort) { | |
3423 | drv->bdrv_reopen_abort(reopen_state); | |
3424 | } | |
3425 | ||
3426 | bdrv_abort_perm_update(reopen_state->bs); | |
3427 | } | |
3428 | ||
3429 | ||
3430 | static void bdrv_close(BlockDriverState *bs) | |
3431 | { | |
3432 | BdrvAioNotifier *ban, *ban_next; | |
3433 | BdrvChild *child, *next; | |
3434 | ||
3435 | assert(!bs->job); | |
3436 | assert(!bs->refcnt); | |
3437 | ||
3438 | bdrv_drained_begin(bs); /* complete I/O */ | |
3439 | bdrv_flush(bs); | |
3440 | bdrv_drain(bs); /* in case flush left pending I/O */ | |
3441 | ||
3442 | if (bs->drv) { | |
3443 | if (bs->drv->bdrv_close) { | |
3444 | bs->drv->bdrv_close(bs); | |
3445 | } | |
3446 | bs->drv = NULL; | |
3447 | } | |
3448 | ||
3449 | bdrv_set_backing_hd(bs, NULL, &error_abort); | |
3450 | ||
3451 | if (bs->file != NULL) { | |
3452 | bdrv_unref_child(bs, bs->file); | |
3453 | bs->file = NULL; | |
3454 | } | |
3455 | ||
3456 | QLIST_FOREACH_SAFE(child, &bs->children, next, next) { | |
3457 | /* TODO Remove bdrv_unref() from drivers' close function and use | |
3458 | * bdrv_unref_child() here */ | |
3459 | if (child->bs->inherits_from == bs) { | |
3460 | child->bs->inherits_from = NULL; | |
3461 | } | |
3462 | bdrv_detach_child(child); | |
3463 | } | |
3464 | ||
3465 | g_free(bs->opaque); | |
3466 | bs->opaque = NULL; | |
3467 | atomic_set(&bs->copy_on_read, 0); | |
3468 | bs->backing_file[0] = '\0'; | |
3469 | bs->backing_format[0] = '\0'; | |
3470 | bs->total_sectors = 0; | |
3471 | bs->encrypted = false; | |
3472 | bs->sg = false; | |
3473 | qobject_unref(bs->options); | |
3474 | qobject_unref(bs->explicit_options); | |
3475 | bs->options = NULL; | |
3476 | bs->explicit_options = NULL; | |
3477 | qobject_unref(bs->full_open_options); | |
3478 | bs->full_open_options = NULL; | |
3479 | ||
3480 | bdrv_release_named_dirty_bitmaps(bs); | |
3481 | assert(QLIST_EMPTY(&bs->dirty_bitmaps)); | |
3482 | ||
3483 | QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) { | |
3484 | g_free(ban); | |
3485 | } | |
3486 | QLIST_INIT(&bs->aio_notifiers); | |
3487 | bdrv_drained_end(bs); | |
3488 | } | |
3489 | ||
3490 | void bdrv_close_all(void) | |
3491 | { | |
3492 | assert(job_next(NULL) == NULL); | |
3493 | nbd_export_close_all(); | |
3494 | ||
3495 | /* Drop references from requests still in flight, such as canceled block | |
3496 | * jobs whose AIO context has not been polled yet */ | |
3497 | bdrv_drain_all(); | |
3498 | ||
3499 | blk_remove_all_bs(); | |
3500 | blockdev_close_all_bdrv_states(); | |
3501 | ||
3502 | assert(QTAILQ_EMPTY(&all_bdrv_states)); | |
3503 | } | |
3504 | ||
3505 | static bool should_update_child(BdrvChild *c, BlockDriverState *to) | |
3506 | { | |
3507 | BdrvChild *to_c; | |
3508 | ||
3509 | if (c->role->stay_at_node) { | |
3510 | return false; | |
3511 | } | |
3512 | ||
3513 | /* If the child @c belongs to the BDS @to, replacing the current | |
3514 | * c->bs by @to would mean to create a loop. | |
3515 | * | |
3516 | * Such a case occurs when appending a BDS to a backing chain. | |
3517 | * For instance, imagine the following chain: | |
3518 | * | |
3519 | * guest device -> node A -> further backing chain... | |
3520 | * | |
3521 | * Now we create a new BDS B which we want to put on top of this | |
3522 | * chain, so we first attach A as its backing node: | |
3523 | * | |
3524 | * node B | |
3525 | * | | |
3526 | * v | |
3527 | * guest device -> node A -> further backing chain... | |
3528 | * | |
3529 | * Finally we want to replace A by B. When doing that, we want to | |
3530 | * replace all pointers to A by pointers to B -- except for the | |
3531 | * pointer from B because (1) that would create a loop, and (2) | |
3532 | * that pointer should simply stay intact: | |
3533 | * | |
3534 | * guest device -> node B | |
3535 | * | | |
3536 | * v | |
3537 | * node A -> further backing chain... | |
3538 | * | |
3539 | * In general, when replacing a node A (c->bs) by a node B (@to), | |
3540 | * if A is a child of B, that means we cannot replace A by B there | |
3541 | * because that would create a loop. Silently detaching A from B | |
3542 | * is also not really an option. So overall just leaving A in | |
3543 | * place there is the most sensible choice. */ | |
3544 | QLIST_FOREACH(to_c, &to->children, next) { | |
3545 | if (to_c == c) { | |
3546 | return false; | |
3547 | } | |
3548 | } | |
3549 | ||
3550 | return true; | |
3551 | } | |
3552 | ||
3553 | void bdrv_replace_node(BlockDriverState *from, BlockDriverState *to, | |
3554 | Error **errp) | |
3555 | { | |
3556 | BdrvChild *c, *next; | |
3557 | GSList *list = NULL, *p; | |
3558 | uint64_t old_perm, old_shared; | |
3559 | uint64_t perm = 0, shared = BLK_PERM_ALL; | |
3560 | int ret; | |
3561 | ||
3562 | assert(!atomic_read(&from->in_flight)); | |
3563 | assert(!atomic_read(&to->in_flight)); | |
3564 | ||
3565 | /* Make sure that @from doesn't go away until we have successfully attached | |
3566 | * all of its parents to @to. */ | |
3567 | bdrv_ref(from); | |
3568 | ||
3569 | /* Put all parents into @list and calculate their cumulative permissions */ | |
3570 | QLIST_FOREACH_SAFE(c, &from->parents, next_parent, next) { | |
3571 | assert(c->bs == from); | |
3572 | if (!should_update_child(c, to)) { | |
3573 | continue; | |
3574 | } | |
3575 | list = g_slist_prepend(list, c); | |
3576 | perm |= c->perm; | |
3577 | shared &= c->shared_perm; | |
3578 | } | |
3579 | ||
3580 | /* Check whether the required permissions can be granted on @to, ignoring | |
3581 | * all BdrvChild in @list so that they can't block themselves. */ | |
3582 | ret = bdrv_check_update_perm(to, NULL, perm, shared, list, errp); | |
3583 | if (ret < 0) { | |
3584 | bdrv_abort_perm_update(to); | |
3585 | goto out; | |
3586 | } | |
3587 | ||
3588 | /* Now actually perform the change. We performed the permission check for | |
3589 | * all elements of @list at once, so set the permissions all at once at the | |
3590 | * very end. */ | |
3591 | for (p = list; p != NULL; p = p->next) { | |
3592 | c = p->data; | |
3593 | ||
3594 | bdrv_ref(to); | |
3595 | bdrv_replace_child_noperm(c, to); | |
3596 | bdrv_unref(from); | |
3597 | } | |
3598 | ||
3599 | bdrv_get_cumulative_perm(to, &old_perm, &old_shared); | |
3600 | bdrv_set_perm(to, old_perm | perm, old_shared | shared); | |
3601 | ||
3602 | out: | |
3603 | g_slist_free(list); | |
3604 | bdrv_unref(from); | |
3605 | } | |
3606 | ||
3607 | /* | |
3608 | * Add new bs contents at the top of an image chain while the chain is | |
3609 | * live, while keeping required fields on the top layer. | |
3610 | * | |
3611 | * This will modify the BlockDriverState fields, and swap contents | |
3612 | * between bs_new and bs_top. Both bs_new and bs_top are modified. | |
3613 | * | |
3614 | * bs_new must not be attached to a BlockBackend. | |
3615 | * | |
3616 | * This function does not create any image files. | |
3617 | * | |
3618 | * bdrv_append() takes ownership of a bs_new reference and unrefs it because | |
3619 | * that's what the callers commonly need. bs_new will be referenced by the old | |
3620 | * parents of bs_top after bdrv_append() returns. If the caller needs to keep a | |
3621 | * reference of its own, it must call bdrv_ref(). | |
3622 | */ | |
3623 | void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top, | |
3624 | Error **errp) | |
3625 | { | |
3626 | Error *local_err = NULL; | |
3627 | ||
3628 | bdrv_set_backing_hd(bs_new, bs_top, &local_err); | |
3629 | if (local_err) { | |
3630 | error_propagate(errp, local_err); | |
3631 | goto out; | |
3632 | } | |
3633 | ||
3634 | bdrv_replace_node(bs_top, bs_new, &local_err); | |
3635 | if (local_err) { | |
3636 | error_propagate(errp, local_err); | |
3637 | bdrv_set_backing_hd(bs_new, NULL, &error_abort); | |
3638 | goto out; | |
3639 | } | |
3640 | ||
3641 | /* bs_new is now referenced by its new parents, we don't need the | |
3642 | * additional reference any more. */ | |
3643 | out: | |
3644 | bdrv_unref(bs_new); | |
3645 | } | |
3646 | ||
3647 | static void bdrv_delete(BlockDriverState *bs) | |
3648 | { | |
3649 | assert(!bs->job); | |
3650 | assert(bdrv_op_blocker_is_empty(bs)); | |
3651 | assert(!bs->refcnt); | |
3652 | ||
3653 | bdrv_close(bs); | |
3654 | ||
3655 | /* remove from list, if necessary */ | |
3656 | if (bs->node_name[0] != '\0') { | |
3657 | QTAILQ_REMOVE(&graph_bdrv_states, bs, node_list); | |
3658 | } | |
3659 | QTAILQ_REMOVE(&all_bdrv_states, bs, bs_list); | |
3660 | ||
3661 | g_free(bs); | |
3662 | } | |
3663 | ||
3664 | /* | |
3665 | * Run consistency checks on an image | |
3666 | * | |
3667 | * Returns 0 if the check could be completed (it doesn't mean that the image is | |
3668 | * free of errors) or -errno when an internal error occurred. The results of the | |
3669 | * check are stored in res. | |
3670 | */ | |
3671 | static int coroutine_fn bdrv_co_check(BlockDriverState *bs, | |
3672 | BdrvCheckResult *res, BdrvCheckMode fix) | |
3673 | { | |
3674 | if (bs->drv == NULL) { | |
3675 | return -ENOMEDIUM; | |
3676 | } | |
3677 | if (bs->drv->bdrv_co_check == NULL) { | |
3678 | return -ENOTSUP; | |
3679 | } | |
3680 | ||
3681 | memset(res, 0, sizeof(*res)); | |
3682 | return bs->drv->bdrv_co_check(bs, res, fix); | |
3683 | } | |
3684 | ||
3685 | typedef struct CheckCo { | |
3686 | BlockDriverState *bs; | |
3687 | BdrvCheckResult *res; | |
3688 | BdrvCheckMode fix; | |
3689 | int ret; | |
3690 | } CheckCo; | |
3691 | ||
3692 | static void bdrv_check_co_entry(void *opaque) | |
3693 | { | |
3694 | CheckCo *cco = opaque; | |
3695 | cco->ret = bdrv_co_check(cco->bs, cco->res, cco->fix); | |
3696 | } | |
3697 | ||
3698 | int bdrv_check(BlockDriverState *bs, | |
3699 | BdrvCheckResult *res, BdrvCheckMode fix) | |
3700 | { | |
3701 | Coroutine *co; | |
3702 | CheckCo cco = { | |
3703 | .bs = bs, | |
3704 | .res = res, | |
3705 | .ret = -EINPROGRESS, | |
3706 | .fix = fix, | |
3707 | }; | |
3708 | ||
3709 | if (qemu_in_coroutine()) { | |
3710 | /* Fast-path if already in coroutine context */ | |
3711 | bdrv_check_co_entry(&cco); | |
3712 | } else { | |
3713 | co = qemu_coroutine_create(bdrv_check_co_entry, &cco); | |
3714 | qemu_coroutine_enter(co); | |
3715 | BDRV_POLL_WHILE(bs, cco.ret == -EINPROGRESS); | |
3716 | } | |
3717 | ||
3718 | return cco.ret; | |
3719 | } | |
3720 | ||
3721 | /* | |
3722 | * Return values: | |
3723 | * 0 - success | |
3724 | * -EINVAL - backing format specified, but no file | |
3725 | * -ENOSPC - can't update the backing file because no space is left in the | |
3726 | * image file header | |
3727 | * -ENOTSUP - format driver doesn't support changing the backing file | |
3728 | */ | |
3729 | int bdrv_change_backing_file(BlockDriverState *bs, | |
3730 | const char *backing_file, const char *backing_fmt) | |
3731 | { | |
3732 | BlockDriver *drv = bs->drv; | |
3733 | int ret; | |
3734 | ||
3735 | if (!drv) { | |
3736 | return -ENOMEDIUM; | |
3737 | } | |
3738 | ||
3739 | /* Backing file format doesn't make sense without a backing file */ | |
3740 | if (backing_fmt && !backing_file) { | |
3741 | return -EINVAL; | |
3742 | } | |
3743 | ||
3744 | if (drv->bdrv_change_backing_file != NULL) { | |
3745 | ret = drv->bdrv_change_backing_file(bs, backing_file, backing_fmt); | |
3746 | } else { | |
3747 | ret = -ENOTSUP; | |
3748 | } | |
3749 | ||
3750 | if (ret == 0) { | |
3751 | pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: ""); | |
3752 | pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: ""); | |
3753 | } | |
3754 | return ret; | |
3755 | } | |
3756 | ||
3757 | /* | |
3758 | * Finds the image layer in the chain that has 'bs' as its backing file. | |
3759 | * | |
3760 | * active is the current topmost image. | |
3761 | * | |
3762 | * Returns NULL if bs is not found in active's image chain, | |
3763 | * or if active == bs. | |
3764 | * | |
3765 | * Returns the bottommost base image if bs == NULL. | |
3766 | */ | |
3767 | BlockDriverState *bdrv_find_overlay(BlockDriverState *active, | |
3768 | BlockDriverState *bs) | |
3769 | { | |
3770 | while (active && bs != backing_bs(active)) { | |
3771 | active = backing_bs(active); | |
3772 | } | |
3773 | ||
3774 | return active; | |
3775 | } | |
3776 | ||
3777 | /* Given a BDS, searches for the base layer. */ | |
3778 | BlockDriverState *bdrv_find_base(BlockDriverState *bs) | |
3779 | { | |
3780 | return bdrv_find_overlay(bs, NULL); | |
3781 | } | |
3782 | ||
3783 | /* | |
3784 | * Drops images above 'base' up to and including 'top', and sets the image | |
3785 | * above 'top' to have base as its backing file. | |
3786 | * | |
3787 | * Requires that the overlay to 'top' is opened r/w, so that the backing file | |
3788 | * information in 'bs' can be properly updated. | |
3789 | * | |
3790 | * E.g., this will convert the following chain: | |
3791 | * bottom <- base <- intermediate <- top <- active | |
3792 | * | |
3793 | * to | |
3794 | * | |
3795 | * bottom <- base <- active | |
3796 | * | |
3797 | * It is allowed for bottom==base, in which case it converts: | |
3798 | * | |
3799 | * base <- intermediate <- top <- active | |
3800 | * | |
3801 | * to | |
3802 | * | |
3803 | * base <- active | |
3804 | * | |
3805 | * If backing_file_str is non-NULL, it will be used when modifying top's | |
3806 | * overlay image metadata. | |
3807 | * | |
3808 | * Error conditions: | |
3809 | * if active == top, that is considered an error | |
3810 | * | |
3811 | */ | |
3812 | int bdrv_drop_intermediate(BlockDriverState *top, BlockDriverState *base, | |
3813 | const char *backing_file_str) | |
3814 | { | |
3815 | BdrvChild *c, *next; | |
3816 | Error *local_err = NULL; | |
3817 | int ret = -EIO; | |
3818 | ||
3819 | bdrv_ref(top); | |
3820 | ||
3821 | if (!top->drv || !base->drv) { | |
3822 | goto exit; | |
3823 | } | |
3824 | ||
3825 | /* Make sure that base is in the backing chain of top */ | |
3826 | if (!bdrv_chain_contains(top, base)) { | |
3827 | goto exit; | |
3828 | } | |
3829 | ||
3830 | /* success - we can delete the intermediate states, and link top->base */ | |
3831 | /* TODO Check graph modification op blockers (BLK_PERM_GRAPH_MOD) once | |
3832 | * we've figured out how they should work. */ | |
3833 | backing_file_str = backing_file_str ? backing_file_str : base->filename; | |
3834 | ||
3835 | QLIST_FOREACH_SAFE(c, &top->parents, next_parent, next) { | |
3836 | /* Check whether we are allowed to switch c from top to base */ | |
3837 | GSList *ignore_children = g_slist_prepend(NULL, c); | |
3838 | bdrv_check_update_perm(base, NULL, c->perm, c->shared_perm, | |
3839 | ignore_children, &local_err); | |
3840 | g_slist_free(ignore_children); | |
3841 | if (local_err) { | |
3842 | ret = -EPERM; | |
3843 | error_report_err(local_err); | |
3844 | goto exit; | |
3845 | } | |
3846 | ||
3847 | /* If so, update the backing file path in the image file */ | |
3848 | if (c->role->update_filename) { | |
3849 | ret = c->role->update_filename(c, base, backing_file_str, | |
3850 | &local_err); | |
3851 | if (ret < 0) { | |
3852 | bdrv_abort_perm_update(base); | |
3853 | error_report_err(local_err); | |
3854 | goto exit; | |
3855 | } | |
3856 | } | |
3857 | ||
3858 | /* Do the actual switch in the in-memory graph. | |
3859 | * Completes bdrv_check_update_perm() transaction internally. */ | |
3860 | bdrv_ref(base); | |
3861 | bdrv_replace_child(c, base); | |
3862 | bdrv_unref(top); | |
3863 | } | |
3864 | ||
3865 | ret = 0; | |
3866 | exit: | |
3867 | bdrv_unref(top); | |
3868 | return ret; | |
3869 | } | |
3870 | ||
3871 | /** | |
3872 | * Length of a allocated file in bytes. Sparse files are counted by actual | |
3873 | * allocated space. Return < 0 if error or unknown. | |
3874 | */ | |
3875 | int64_t bdrv_get_allocated_file_size(BlockDriverState *bs) | |
3876 | { | |
3877 | BlockDriver *drv = bs->drv; | |
3878 | if (!drv) { | |
3879 | return -ENOMEDIUM; | |
3880 | } | |
3881 | if (drv->bdrv_get_allocated_file_size) { | |
3882 | return drv->bdrv_get_allocated_file_size(bs); | |
3883 | } | |
3884 | if (bs->file) { | |
3885 | return bdrv_get_allocated_file_size(bs->file->bs); | |
3886 | } | |
3887 | return -ENOTSUP; | |
3888 | } | |
3889 | ||
3890 | /* | |
3891 | * bdrv_measure: | |
3892 | * @drv: Format driver | |
3893 | * @opts: Creation options for new image | |
3894 | * @in_bs: Existing image containing data for new image (may be NULL) | |
3895 | * @errp: Error object | |
3896 | * Returns: A #BlockMeasureInfo (free using qapi_free_BlockMeasureInfo()) | |
3897 | * or NULL on error | |
3898 | * | |
3899 | * Calculate file size required to create a new image. | |
3900 | * | |
3901 | * If @in_bs is given then space for allocated clusters and zero clusters | |
3902 | * from that image are included in the calculation. If @opts contains a | |
3903 | * backing file that is shared by @in_bs then backing clusters may be omitted | |
3904 | * from the calculation. | |
3905 | * | |
3906 | * If @in_bs is NULL then the calculation includes no allocated clusters | |
3907 | * unless a preallocation option is given in @opts. | |
3908 | * | |
3909 | * Note that @in_bs may use a different BlockDriver from @drv. | |
3910 | * | |
3911 | * If an error occurs the @errp pointer is set. | |
3912 | */ | |
3913 | BlockMeasureInfo *bdrv_measure(BlockDriver *drv, QemuOpts *opts, | |
3914 | BlockDriverState *in_bs, Error **errp) | |
3915 | { | |
3916 | if (!drv->bdrv_measure) { | |
3917 | error_setg(errp, "Block driver '%s' does not support size measurement", | |
3918 | drv->format_name); | |
3919 | return NULL; | |
3920 | } | |
3921 | ||
3922 | return drv->bdrv_measure(opts, in_bs, errp); | |
3923 | } | |
3924 | ||
3925 | /** | |
3926 | * Return number of sectors on success, -errno on error. | |
3927 | */ | |
3928 | int64_t bdrv_nb_sectors(BlockDriverState *bs) | |
3929 | { | |
3930 | BlockDriver *drv = bs->drv; | |
3931 | ||
3932 | if (!drv) | |
3933 | return -ENOMEDIUM; | |
3934 | ||
3935 | if (drv->has_variable_length) { | |
3936 | int ret = refresh_total_sectors(bs, bs->total_sectors); | |
3937 | if (ret < 0) { | |
3938 | return ret; | |
3939 | } | |
3940 | } | |
3941 | return bs->total_sectors; | |
3942 | } | |
3943 | ||
3944 | /** | |
3945 | * Return length in bytes on success, -errno on error. | |
3946 | * The length is always a multiple of BDRV_SECTOR_SIZE. | |
3947 | */ | |
3948 | int64_t bdrv_getlength(BlockDriverState *bs) | |
3949 | { | |
3950 | int64_t ret = bdrv_nb_sectors(bs); | |
3951 | ||
3952 | ret = ret > INT64_MAX / BDRV_SECTOR_SIZE ? -EFBIG : ret; | |
3953 | return ret < 0 ? ret : ret * BDRV_SECTOR_SIZE; | |
3954 | } | |
3955 | ||
3956 | /* return 0 as number of sectors if no device present or error */ | |
3957 | void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr) | |
3958 | { | |
3959 | int64_t nb_sectors = bdrv_nb_sectors(bs); | |
3960 | ||
3961 | *nb_sectors_ptr = nb_sectors < 0 ? 0 : nb_sectors; | |
3962 | } | |
3963 | ||
3964 | bool bdrv_is_sg(BlockDriverState *bs) | |
3965 | { | |
3966 | return bs->sg; | |
3967 | } | |
3968 | ||
3969 | bool bdrv_is_encrypted(BlockDriverState *bs) | |
3970 | { | |
3971 | if (bs->backing && bs->backing->bs->encrypted) { | |
3972 | return true; | |
3973 | } | |
3974 | return bs->encrypted; | |
3975 | } | |
3976 | ||
3977 | const char *bdrv_get_format_name(BlockDriverState *bs) | |
3978 | { | |
3979 | return bs->drv ? bs->drv->format_name : NULL; | |
3980 | } | |
3981 | ||
3982 | static int qsort_strcmp(const void *a, const void *b) | |
3983 | { | |
3984 | return strcmp(*(char *const *)a, *(char *const *)b); | |
3985 | } | |
3986 | ||
3987 | void bdrv_iterate_format(void (*it)(void *opaque, const char *name), | |
3988 | void *opaque) | |
3989 | { | |
3990 | BlockDriver *drv; | |
3991 | int count = 0; | |
3992 | int i; | |
3993 | const char **formats = NULL; | |
3994 | ||
3995 | QLIST_FOREACH(drv, &bdrv_drivers, list) { | |
3996 | if (drv->format_name) { | |
3997 | bool found = false; | |
3998 | int i = count; | |
3999 | while (formats && i && !found) { | |
4000 | found = !strcmp(formats[--i], drv->format_name); | |
4001 | } | |
4002 | ||
4003 | if (!found) { | |
4004 | formats = g_renew(const char *, formats, count + 1); | |
4005 | formats[count++] = drv->format_name; | |
4006 | } | |
4007 | } | |
4008 | } | |
4009 | ||
4010 | for (i = 0; i < (int)ARRAY_SIZE(block_driver_modules); i++) { | |
4011 | const char *format_name = block_driver_modules[i].format_name; | |
4012 | ||
4013 | if (format_name) { | |
4014 | bool found = false; | |
4015 | int j = count; | |
4016 | ||
4017 | while (formats && j && !found) { | |
4018 | found = !strcmp(formats[--j], format_name); | |
4019 | } | |
4020 | ||
4021 | if (!found) { | |
4022 | formats = g_renew(const char *, formats, count + 1); | |
4023 | formats[count++] = format_name; | |
4024 | } | |
4025 | } | |
4026 | } | |
4027 | ||
4028 | qsort(formats, count, sizeof(formats[0]), qsort_strcmp); | |
4029 | ||
4030 | for (i = 0; i < count; i++) { | |
4031 | it(opaque, formats[i]); | |
4032 | } | |
4033 | ||
4034 | g_free(formats); | |
4035 | } | |
4036 | ||
4037 | /* This function is to find a node in the bs graph */ | |
4038 | BlockDriverState *bdrv_find_node(const char *node_name) | |
4039 | { | |
4040 | BlockDriverState *bs; | |
4041 | ||
4042 | assert(node_name); | |
4043 | ||
4044 | QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) { | |
4045 | if (!strcmp(node_name, bs->node_name)) { | |
4046 | return bs; | |
4047 | } | |
4048 | } | |
4049 | return NULL; | |
4050 | } | |
4051 | ||
4052 | /* Put this QMP function here so it can access the static graph_bdrv_states. */ | |
4053 | BlockDeviceInfoList *bdrv_named_nodes_list(Error **errp) | |
4054 | { | |
4055 | BlockDeviceInfoList *list, *entry; | |
4056 | BlockDriverState *bs; | |
4057 | ||
4058 | list = NULL; | |
4059 | QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) { | |
4060 | BlockDeviceInfo *info = bdrv_block_device_info(NULL, bs, errp); | |
4061 | if (!info) { | |
4062 | qapi_free_BlockDeviceInfoList(list); | |
4063 | return NULL; | |
4064 | } | |
4065 | entry = g_malloc0(sizeof(*entry)); | |
4066 | entry->value = info; | |
4067 | entry->next = list; | |
4068 | list = entry; | |
4069 | } | |
4070 | ||
4071 | return list; | |
4072 | } | |
4073 | ||
4074 | BlockDriverState *bdrv_lookup_bs(const char *device, | |
4075 | const char *node_name, | |
4076 | Error **errp) | |
4077 | { | |
4078 | BlockBackend *blk; | |
4079 | BlockDriverState *bs; | |
4080 | ||
4081 | if (device) { | |
4082 | blk = blk_by_name(device); | |
4083 | ||
4084 | if (blk) { | |
4085 | bs = blk_bs(blk); | |
4086 | if (!bs) { | |
4087 | error_setg(errp, "Device '%s' has no medium", device); | |
4088 | } | |
4089 | ||
4090 | return bs; | |
4091 | } | |
4092 | } | |
4093 | ||
4094 | if (node_name) { | |
4095 | bs = bdrv_find_node(node_name); | |
4096 | ||
4097 | if (bs) { | |
4098 | return bs; | |
4099 | } | |
4100 | } | |
4101 | ||
4102 | error_setg(errp, "Cannot find device=%s nor node_name=%s", | |
4103 | device ? device : "", | |
4104 | node_name ? node_name : ""); | |
4105 | return NULL; | |
4106 | } | |
4107 | ||
4108 | /* If 'base' is in the same chain as 'top', return true. Otherwise, | |
4109 | * return false. If either argument is NULL, return false. */ | |
4110 | bool bdrv_chain_contains(BlockDriverState *top, BlockDriverState *base) | |
4111 | { | |
4112 | while (top && top != base) { | |
4113 | top = backing_bs(top); | |
4114 | } | |
4115 | ||
4116 | return top != NULL; | |
4117 | } | |
4118 | ||
4119 | BlockDriverState *bdrv_next_node(BlockDriverState *bs) | |
4120 | { | |
4121 | if (!bs) { | |
4122 | return QTAILQ_FIRST(&graph_bdrv_states); | |
4123 | } | |
4124 | return QTAILQ_NEXT(bs, node_list); | |
4125 | } | |
4126 | ||
4127 | BlockDriverState *bdrv_next_all_states(BlockDriverState *bs) | |
4128 | { | |
4129 | if (!bs) { | |
4130 | return QTAILQ_FIRST(&all_bdrv_states); | |
4131 | } | |
4132 | return QTAILQ_NEXT(bs, bs_list); | |
4133 | } | |
4134 | ||
4135 | const char *bdrv_get_node_name(const BlockDriverState *bs) | |
4136 | { | |
4137 | return bs->node_name; | |
4138 | } | |
4139 | ||
4140 | const char *bdrv_get_parent_name(const BlockDriverState *bs) | |
4141 | { | |
4142 | BdrvChild *c; | |
4143 | const char *name; | |
4144 | ||
4145 | /* If multiple parents have a name, just pick the first one. */ | |
4146 | QLIST_FOREACH(c, &bs->parents, next_parent) { | |
4147 | if (c->role->get_name) { | |
4148 | name = c->role->get_name(c); | |
4149 | if (name && *name) { | |
4150 | return name; | |
4151 | } | |
4152 | } | |
4153 | } | |
4154 | ||
4155 | return NULL; | |
4156 | } | |
4157 | ||
4158 | /* TODO check what callers really want: bs->node_name or blk_name() */ | |
4159 | const char *bdrv_get_device_name(const BlockDriverState *bs) | |
4160 | { | |
4161 | return bdrv_get_parent_name(bs) ?: ""; | |
4162 | } | |
4163 | ||
4164 | /* This can be used to identify nodes that might not have a device | |
4165 | * name associated. Since node and device names live in the same | |
4166 | * namespace, the result is unambiguous. The exception is if both are | |
4167 | * absent, then this returns an empty (non-null) string. */ | |
4168 | const char *bdrv_get_device_or_node_name(const BlockDriverState *bs) | |
4169 | { | |
4170 | return bdrv_get_parent_name(bs) ?: bs->node_name; | |
4171 | } | |
4172 | ||
4173 | int bdrv_get_flags(BlockDriverState *bs) | |
4174 | { | |
4175 | return bs->open_flags; | |
4176 | } | |
4177 | ||
4178 | int bdrv_has_zero_init_1(BlockDriverState *bs) | |
4179 | { | |
4180 | return 1; | |
4181 | } | |
4182 | ||
4183 | int bdrv_has_zero_init(BlockDriverState *bs) | |
4184 | { | |
4185 | if (!bs->drv) { | |
4186 | return 0; | |
4187 | } | |
4188 | ||
4189 | /* If BS is a copy on write image, it is initialized to | |
4190 | the contents of the base image, which may not be zeroes. */ | |
4191 | if (bs->backing) { | |
4192 | return 0; | |
4193 | } | |
4194 | if (bs->drv->bdrv_has_zero_init) { | |
4195 | return bs->drv->bdrv_has_zero_init(bs); | |
4196 | } | |
4197 | if (bs->file && bs->drv->is_filter) { | |
4198 | return bdrv_has_zero_init(bs->file->bs); | |
4199 | } | |
4200 | ||
4201 | /* safe default */ | |
4202 | return 0; | |
4203 | } | |
4204 | ||
4205 | bool bdrv_unallocated_blocks_are_zero(BlockDriverState *bs) | |
4206 | { | |
4207 | BlockDriverInfo bdi; | |
4208 | ||
4209 | if (bs->backing) { | |
4210 | return false; | |
4211 | } | |
4212 | ||
4213 | if (bdrv_get_info(bs, &bdi) == 0) { | |
4214 | return bdi.unallocated_blocks_are_zero; | |
4215 | } | |
4216 | ||
4217 | return false; | |
4218 | } | |
4219 | ||
4220 | bool bdrv_can_write_zeroes_with_unmap(BlockDriverState *bs) | |
4221 | { | |
4222 | if (!(bs->open_flags & BDRV_O_UNMAP)) { | |
4223 | return false; | |
4224 | } | |
4225 | ||
4226 | return bs->supported_zero_flags & BDRV_REQ_MAY_UNMAP; | |
4227 | } | |
4228 | ||
4229 | const char *bdrv_get_encrypted_filename(BlockDriverState *bs) | |
4230 | { | |
4231 | if (bs->backing && bs->backing->bs->encrypted) | |
4232 | return bs->backing_file; | |
4233 | else if (bs->encrypted) | |
4234 | return bs->filename; | |
4235 | else | |
4236 | return NULL; | |
4237 | } | |
4238 | ||
4239 | void bdrv_get_backing_filename(BlockDriverState *bs, | |
4240 | char *filename, int filename_size) | |
4241 | { | |
4242 | pstrcpy(filename, filename_size, bs->backing_file); | |
4243 | } | |
4244 | ||
4245 | int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) | |
4246 | { | |
4247 | BlockDriver *drv = bs->drv; | |
4248 | /* if bs->drv == NULL, bs is closed, so there's nothing to do here */ | |
4249 | if (!drv) { | |
4250 | return -ENOMEDIUM; | |
4251 | } | |
4252 | if (!drv->bdrv_get_info) { | |
4253 | if (bs->file && drv->is_filter) { | |
4254 | return bdrv_get_info(bs->file->bs, bdi); | |
4255 | } | |
4256 | return -ENOTSUP; | |
4257 | } | |
4258 | memset(bdi, 0, sizeof(*bdi)); | |
4259 | return drv->bdrv_get_info(bs, bdi); | |
4260 | } | |
4261 | ||
4262 | ImageInfoSpecific *bdrv_get_specific_info(BlockDriverState *bs) | |
4263 | { | |
4264 | BlockDriver *drv = bs->drv; | |
4265 | if (drv && drv->bdrv_get_specific_info) { | |
4266 | return drv->bdrv_get_specific_info(bs); | |
4267 | } | |
4268 | return NULL; | |
4269 | } | |
4270 | ||
4271 | void bdrv_debug_event(BlockDriverState *bs, BlkdebugEvent event) | |
4272 | { | |
4273 | if (!bs || !bs->drv || !bs->drv->bdrv_debug_event) { | |
4274 | return; | |
4275 | } | |
4276 | ||
4277 | bs->drv->bdrv_debug_event(bs, event); | |
4278 | } | |
4279 | ||
4280 | int bdrv_debug_breakpoint(BlockDriverState *bs, const char *event, | |
4281 | const char *tag) | |
4282 | { | |
4283 | while (bs && bs->drv && !bs->drv->bdrv_debug_breakpoint) { | |
4284 | bs = bs->file ? bs->file->bs : NULL; | |
4285 | } | |
4286 | ||
4287 | if (bs && bs->drv && bs->drv->bdrv_debug_breakpoint) { | |
4288 | return bs->drv->bdrv_debug_breakpoint(bs, event, tag); | |
4289 | } | |
4290 | ||
4291 | return -ENOTSUP; | |
4292 | } | |
4293 | ||
4294 | int bdrv_debug_remove_breakpoint(BlockDriverState *bs, const char *tag) | |
4295 | { | |
4296 | while (bs && bs->drv && !bs->drv->bdrv_debug_remove_breakpoint) { | |
4297 | bs = bs->file ? bs->file->bs : NULL; | |
4298 | } | |
4299 | ||
4300 | if (bs && bs->drv && bs->drv->bdrv_debug_remove_breakpoint) { | |
4301 | return bs->drv->bdrv_debug_remove_breakpoint(bs, tag); | |
4302 | } | |
4303 | ||
4304 | return -ENOTSUP; | |
4305 | } | |
4306 | ||
4307 | int bdrv_debug_resume(BlockDriverState *bs, const char *tag) | |
4308 | { | |
4309 | while (bs && (!bs->drv || !bs->drv->bdrv_debug_resume)) { | |
4310 | bs = bs->file ? bs->file->bs : NULL; | |
4311 | } | |
4312 | ||
4313 | if (bs && bs->drv && bs->drv->bdrv_debug_resume) { | |
4314 | return bs->drv->bdrv_debug_resume(bs, tag); | |
4315 | } | |
4316 | ||
4317 | return -ENOTSUP; | |
4318 | } | |
4319 | ||
4320 | bool bdrv_debug_is_suspended(BlockDriverState *bs, const char *tag) | |
4321 | { | |
4322 | while (bs && bs->drv && !bs->drv->bdrv_debug_is_suspended) { | |
4323 | bs = bs->file ? bs->file->bs : NULL; | |
4324 | } | |
4325 | ||
4326 | if (bs && bs->drv && bs->drv->bdrv_debug_is_suspended) { | |
4327 | return bs->drv->bdrv_debug_is_suspended(bs, tag); | |
4328 | } | |
4329 | ||
4330 | return false; | |
4331 | } | |
4332 | ||
4333 | /* backing_file can either be relative, or absolute, or a protocol. If it is | |
4334 | * relative, it must be relative to the chain. So, passing in bs->filename | |
4335 | * from a BDS as backing_file should not be done, as that may be relative to | |
4336 | * the CWD rather than the chain. */ | |
4337 | BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs, | |
4338 | const char *backing_file) | |
4339 | { | |
4340 | char *filename_full = NULL; | |
4341 | char *backing_file_full = NULL; | |
4342 | char *filename_tmp = NULL; | |
4343 | int is_protocol = 0; | |
4344 | BlockDriverState *curr_bs = NULL; | |
4345 | BlockDriverState *retval = NULL; | |
4346 | Error *local_error = NULL; | |
4347 | ||
4348 | if (!bs || !bs->drv || !backing_file) { | |
4349 | return NULL; | |
4350 | } | |
4351 | ||
4352 | filename_full = g_malloc(PATH_MAX); | |
4353 | backing_file_full = g_malloc(PATH_MAX); | |
4354 | filename_tmp = g_malloc(PATH_MAX); | |
4355 | ||
4356 | is_protocol = path_has_protocol(backing_file); | |
4357 | ||
4358 | for (curr_bs = bs; curr_bs->backing; curr_bs = curr_bs->backing->bs) { | |
4359 | ||
4360 | /* If either of the filename paths is actually a protocol, then | |
4361 | * compare unmodified paths; otherwise make paths relative */ | |
4362 | if (is_protocol || path_has_protocol(curr_bs->backing_file)) { | |
4363 | if (strcmp(backing_file, curr_bs->backing_file) == 0) { | |
4364 | retval = curr_bs->backing->bs; | |
4365 | break; | |
4366 | } | |
4367 | /* Also check against the full backing filename for the image */ | |
4368 | bdrv_get_full_backing_filename(curr_bs, backing_file_full, PATH_MAX, | |
4369 | &local_error); | |
4370 | if (local_error == NULL) { | |
4371 | if (strcmp(backing_file, backing_file_full) == 0) { | |
4372 | retval = curr_bs->backing->bs; | |
4373 | break; | |
4374 | } | |
4375 | } else { | |
4376 | error_free(local_error); | |
4377 | local_error = NULL; | |
4378 | } | |
4379 | } else { | |
4380 | /* If not an absolute filename path, make it relative to the current | |
4381 | * image's filename path */ | |
4382 | path_combine(filename_tmp, PATH_MAX, curr_bs->filename, | |
4383 | backing_file); | |
4384 | ||
4385 | /* We are going to compare absolute pathnames */ | |
4386 | if (!realpath(filename_tmp, filename_full)) { | |
4387 | continue; | |
4388 | } | |
4389 | ||
4390 | /* We need to make sure the backing filename we are comparing against | |
4391 | * is relative to the current image filename (or absolute) */ | |
4392 | path_combine(filename_tmp, PATH_MAX, curr_bs->filename, | |
4393 | curr_bs->backing_file); | |
4394 | ||
4395 | if (!realpath(filename_tmp, backing_file_full)) { | |
4396 | continue; | |
4397 | } | |
4398 | ||
4399 | if (strcmp(backing_file_full, filename_full) == 0) { | |
4400 | retval = curr_bs->backing->bs; | |
4401 | break; | |
4402 | } | |
4403 | } | |
4404 | } | |
4405 | ||
4406 | g_free(filename_full); | |
4407 | g_free(backing_file_full); | |
4408 | g_free(filename_tmp); | |
4409 | return retval; | |
4410 | } | |
4411 | ||
4412 | void bdrv_init(void) | |
4413 | { | |
4414 | module_call_init(MODULE_INIT_BLOCK); | |
4415 | } | |
4416 | ||
4417 | void bdrv_init_with_whitelist(void) | |
4418 | { | |
4419 | use_bdrv_whitelist = 1; | |
4420 | bdrv_init(); | |
4421 | } | |
4422 | ||
4423 | static void coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs, | |
4424 | Error **errp) | |
4425 | { | |
4426 | BdrvChild *child, *parent; | |
4427 | uint64_t perm, shared_perm; | |
4428 | Error *local_err = NULL; | |
4429 | int ret; | |
4430 | BdrvDirtyBitmap *bm; | |
4431 | ||
4432 | if (!bs->drv) { | |
4433 | return; | |
4434 | } | |
4435 | ||
4436 | if (!(bs->open_flags & BDRV_O_INACTIVE)) { | |
4437 | return; | |
4438 | } | |
4439 | ||
4440 | QLIST_FOREACH(child, &bs->children, next) { | |
4441 | bdrv_co_invalidate_cache(child->bs, &local_err); | |
4442 | if (local_err) { | |
4443 | error_propagate(errp, local_err); | |
4444 | return; | |
4445 | } | |
4446 | } | |
4447 | ||
4448 | /* | |
4449 | * Update permissions, they may differ for inactive nodes. | |
4450 | * | |
4451 | * Note that the required permissions of inactive images are always a | |
4452 | * subset of the permissions required after activating the image. This | |
4453 | * allows us to just get the permissions upfront without restricting | |
4454 | * drv->bdrv_invalidate_cache(). | |
4455 | * | |
4456 | * It also means that in error cases, we don't have to try and revert to | |
4457 | * the old permissions (which is an operation that could fail, too). We can | |
4458 | * just keep the extended permissions for the next time that an activation | |
4459 | * of the image is tried. | |
4460 | */ | |
4461 | bs->open_flags &= ~BDRV_O_INACTIVE; | |
4462 | bdrv_get_cumulative_perm(bs, &perm, &shared_perm); | |
4463 | ret = bdrv_check_perm(bs, NULL, perm, shared_perm, NULL, &local_err); | |
4464 | if (ret < 0) { | |
4465 | bs->open_flags |= BDRV_O_INACTIVE; | |
4466 | error_propagate(errp, local_err); | |
4467 | return; | |
4468 | } | |
4469 | bdrv_set_perm(bs, perm, shared_perm); | |
4470 | ||
4471 | if (bs->drv->bdrv_co_invalidate_cache) { | |
4472 | bs->drv->bdrv_co_invalidate_cache(bs, &local_err); | |
4473 | if (local_err) { | |
4474 | bs->open_flags |= BDRV_O_INACTIVE; | |
4475 | error_propagate(errp, local_err); | |
4476 | return; | |
4477 | } | |
4478 | } | |
4479 | ||
4480 | for (bm = bdrv_dirty_bitmap_next(bs, NULL); bm; | |
4481 | bm = bdrv_dirty_bitmap_next(bs, bm)) | |
4482 | { | |
4483 | bdrv_dirty_bitmap_set_migration(bm, false); | |
4484 | } | |
4485 | ||
4486 | ret = refresh_total_sectors(bs, bs->total_sectors); | |
4487 | if (ret < 0) { | |
4488 | bs->open_flags |= BDRV_O_INACTIVE; | |
4489 | error_setg_errno(errp, -ret, "Could not refresh total sector count"); | |
4490 | return; | |
4491 | } | |
4492 | ||
4493 | QLIST_FOREACH(parent, &bs->parents, next_parent) { | |
4494 | if (parent->role->activate) { | |
4495 | parent->role->activate(parent, &local_err); | |
4496 | if (local_err) { | |
4497 | error_propagate(errp, local_err); | |
4498 | return; | |
4499 | } | |
4500 | } | |
4501 | } | |
4502 | } | |
4503 | ||
4504 | typedef struct InvalidateCacheCo { | |
4505 | BlockDriverState *bs; | |
4506 | Error **errp; | |
4507 | bool done; | |
4508 | } InvalidateCacheCo; | |
4509 | ||
4510 | static void coroutine_fn bdrv_invalidate_cache_co_entry(void *opaque) | |
4511 | { | |
4512 | InvalidateCacheCo *ico = opaque; | |
4513 | bdrv_co_invalidate_cache(ico->bs, ico->errp); | |
4514 | ico->done = true; | |
4515 | } | |
4516 | ||
4517 | void bdrv_invalidate_cache(BlockDriverState *bs, Error **errp) | |
4518 | { | |
4519 | Coroutine *co; | |
4520 | InvalidateCacheCo ico = { | |
4521 | .bs = bs, | |
4522 | .done = false, | |
4523 | .errp = errp | |
4524 | }; | |
4525 | ||
4526 | if (qemu_in_coroutine()) { | |
4527 | /* Fast-path if already in coroutine context */ | |
4528 | bdrv_invalidate_cache_co_entry(&ico); | |
4529 | } else { | |
4530 | co = qemu_coroutine_create(bdrv_invalidate_cache_co_entry, &ico); | |
4531 | qemu_coroutine_enter(co); | |
4532 | BDRV_POLL_WHILE(bs, !ico.done); | |
4533 | } | |
4534 | } | |
4535 | ||
4536 | void bdrv_invalidate_cache_all(Error **errp) | |
4537 | { | |
4538 | BlockDriverState *bs; | |
4539 | Error *local_err = NULL; | |
4540 | BdrvNextIterator it; | |
4541 | ||
4542 | for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) { | |
4543 | AioContext *aio_context = bdrv_get_aio_context(bs); | |
4544 | ||
4545 | aio_context_acquire(aio_context); | |
4546 | bdrv_invalidate_cache(bs, &local_err); | |
4547 | aio_context_release(aio_context); | |
4548 | if (local_err) { | |
4549 | error_propagate(errp, local_err); | |
4550 | bdrv_next_cleanup(&it); | |
4551 | return; | |
4552 | } | |
4553 | } | |
4554 | } | |
4555 | ||
4556 | static int bdrv_inactivate_recurse(BlockDriverState *bs, | |
4557 | bool setting_flag) | |
4558 | { | |
4559 | BdrvChild *child, *parent; | |
4560 | int ret; | |
4561 | ||
4562 | if (!bs->drv) { | |
4563 | return -ENOMEDIUM; | |
4564 | } | |
4565 | ||
4566 | if (!setting_flag && bs->drv->bdrv_inactivate) { | |
4567 | ret = bs->drv->bdrv_inactivate(bs); | |
4568 | if (ret < 0) { | |
4569 | return ret; | |
4570 | } | |
4571 | } | |
4572 | ||
4573 | if (setting_flag && !(bs->open_flags & BDRV_O_INACTIVE)) { | |
4574 | uint64_t perm, shared_perm; | |
4575 | ||
4576 | QLIST_FOREACH(parent, &bs->parents, next_parent) { | |
4577 | if (parent->role->inactivate) { | |
4578 | ret = parent->role->inactivate(parent); | |
4579 | if (ret < 0) { | |
4580 | return ret; | |
4581 | } | |
4582 | } | |
4583 | } | |
4584 | ||
4585 | bs->open_flags |= BDRV_O_INACTIVE; | |
4586 | ||
4587 | /* Update permissions, they may differ for inactive nodes */ | |
4588 | bdrv_get_cumulative_perm(bs, &perm, &shared_perm); | |
4589 | bdrv_check_perm(bs, NULL, perm, shared_perm, NULL, &error_abort); | |
4590 | bdrv_set_perm(bs, perm, shared_perm); | |
4591 | } | |
4592 | ||
4593 | QLIST_FOREACH(child, &bs->children, next) { | |
4594 | ret = bdrv_inactivate_recurse(child->bs, setting_flag); | |
4595 | if (ret < 0) { | |
4596 | return ret; | |
4597 | } | |
4598 | } | |
4599 | ||
4600 | return 0; | |
4601 | } | |
4602 | ||
4603 | int bdrv_inactivate_all(void) | |
4604 | { | |
4605 | BlockDriverState *bs = NULL; | |
4606 | BdrvNextIterator it; | |
4607 | int ret = 0; | |
4608 | int pass; | |
4609 | GSList *aio_ctxs = NULL, *ctx; | |
4610 | ||
4611 | for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) { | |
4612 | AioContext *aio_context = bdrv_get_aio_context(bs); | |
4613 | ||
4614 | if (!g_slist_find(aio_ctxs, aio_context)) { | |
4615 | aio_ctxs = g_slist_prepend(aio_ctxs, aio_context); | |
4616 | aio_context_acquire(aio_context); | |
4617 | } | |
4618 | } | |
4619 | ||
4620 | /* We do two passes of inactivation. The first pass calls to drivers' | |
4621 | * .bdrv_inactivate callbacks recursively so all cache is flushed to disk; | |
4622 | * the second pass sets the BDRV_O_INACTIVE flag so that no further write | |
4623 | * is allowed. */ | |
4624 | for (pass = 0; pass < 2; pass++) { | |
4625 | for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) { | |
4626 | ret = bdrv_inactivate_recurse(bs, pass); | |
4627 | if (ret < 0) { | |
4628 | bdrv_next_cleanup(&it); | |
4629 | goto out; | |
4630 | } | |
4631 | } | |
4632 | } | |
4633 | ||
4634 | out: | |
4635 | for (ctx = aio_ctxs; ctx != NULL; ctx = ctx->next) { | |
4636 | AioContext *aio_context = ctx->data; | |
4637 | aio_context_release(aio_context); | |
4638 | } | |
4639 | g_slist_free(aio_ctxs); | |
4640 | ||
4641 | return ret; | |
4642 | } | |
4643 | ||
4644 | /**************************************************************/ | |
4645 | /* removable device support */ | |
4646 | ||
4647 | /** | |
4648 | * Return TRUE if the media is present | |
4649 | */ | |
4650 | bool bdrv_is_inserted(BlockDriverState *bs) | |
4651 | { | |
4652 | BlockDriver *drv = bs->drv; | |
4653 | BdrvChild *child; | |
4654 | ||
4655 | if (!drv) { | |
4656 | return false; | |
4657 | } | |
4658 | if (drv->bdrv_is_inserted) { | |
4659 | return drv->bdrv_is_inserted(bs); | |
4660 | } | |
4661 | QLIST_FOREACH(child, &bs->children, next) { | |
4662 | if (!bdrv_is_inserted(child->bs)) { | |
4663 | return false; | |
4664 | } | |
4665 | } | |
4666 | return true; | |
4667 | } | |
4668 | ||
4669 | /** | |
4670 | * If eject_flag is TRUE, eject the media. Otherwise, close the tray | |
4671 | */ | |
4672 | void bdrv_eject(BlockDriverState *bs, bool eject_flag) | |
4673 | { | |
4674 | BlockDriver *drv = bs->drv; | |
4675 | ||
4676 | if (drv && drv->bdrv_eject) { | |
4677 | drv->bdrv_eject(bs, eject_flag); | |
4678 | } | |
4679 | } | |
4680 | ||
4681 | /** | |
4682 | * Lock or unlock the media (if it is locked, the user won't be able | |
4683 | * to eject it manually). | |
4684 | */ | |
4685 | void bdrv_lock_medium(BlockDriverState *bs, bool locked) | |
4686 | { | |
4687 | BlockDriver *drv = bs->drv; | |
4688 | ||
4689 | trace_bdrv_lock_medium(bs, locked); | |
4690 | ||
4691 | if (drv && drv->bdrv_lock_medium) { | |
4692 | drv->bdrv_lock_medium(bs, locked); | |
4693 | } | |
4694 | } | |
4695 | ||
4696 | /* Get a reference to bs */ | |
4697 | void bdrv_ref(BlockDriverState *bs) | |
4698 | { | |
4699 | bs->refcnt++; | |
4700 | } | |
4701 | ||
4702 | /* Release a previously grabbed reference to bs. | |
4703 | * If after releasing, reference count is zero, the BlockDriverState is | |
4704 | * deleted. */ | |
4705 | void bdrv_unref(BlockDriverState *bs) | |
4706 | { | |
4707 | if (!bs) { | |
4708 | return; | |
4709 | } | |
4710 | assert(bs->refcnt > 0); | |
4711 | if (--bs->refcnt == 0) { | |
4712 | bdrv_delete(bs); | |
4713 | } | |
4714 | } | |
4715 | ||
4716 | struct BdrvOpBlocker { | |
4717 | Error *reason; | |
4718 | QLIST_ENTRY(BdrvOpBlocker) list; | |
4719 | }; | |
4720 | ||
4721 | bool bdrv_op_is_blocked(BlockDriverState *bs, BlockOpType op, Error **errp) | |
4722 | { | |
4723 | BdrvOpBlocker *blocker; | |
4724 | assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX); | |
4725 | if (!QLIST_EMPTY(&bs->op_blockers[op])) { | |
4726 | blocker = QLIST_FIRST(&bs->op_blockers[op]); | |
4727 | error_propagate_prepend(errp, error_copy(blocker->reason), | |
4728 | "Node '%s' is busy: ", | |
4729 | bdrv_get_device_or_node_name(bs)); | |
4730 | return true; | |
4731 | } | |
4732 | return false; | |
4733 | } | |
4734 | ||
4735 | void bdrv_op_block(BlockDriverState *bs, BlockOpType op, Error *reason) | |
4736 | { | |
4737 | BdrvOpBlocker *blocker; | |
4738 | assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX); | |
4739 | ||
4740 | blocker = g_new0(BdrvOpBlocker, 1); | |
4741 | blocker->reason = reason; | |
4742 | QLIST_INSERT_HEAD(&bs->op_blockers[op], blocker, list); | |
4743 | } | |
4744 | ||
4745 | void bdrv_op_unblock(BlockDriverState *bs, BlockOpType op, Error *reason) | |
4746 | { | |
4747 | BdrvOpBlocker *blocker, *next; | |
4748 | assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX); | |
4749 | QLIST_FOREACH_SAFE(blocker, &bs->op_blockers[op], list, next) { | |
4750 | if (blocker->reason == reason) { | |
4751 | QLIST_REMOVE(blocker, list); | |
4752 | g_free(blocker); | |
4753 | } | |
4754 | } | |
4755 | } | |
4756 | ||
4757 | void bdrv_op_block_all(BlockDriverState *bs, Error *reason) | |
4758 | { | |
4759 | int i; | |
4760 | for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) { | |
4761 | bdrv_op_block(bs, i, reason); | |
4762 | } | |
4763 | } | |
4764 | ||
4765 | void bdrv_op_unblock_all(BlockDriverState *bs, Error *reason) | |
4766 | { | |
4767 | int i; | |
4768 | for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) { | |
4769 | bdrv_op_unblock(bs, i, reason); | |
4770 | } | |
4771 | } | |
4772 | ||
4773 | bool bdrv_op_blocker_is_empty(BlockDriverState *bs) | |
4774 | { | |
4775 | int i; | |
4776 | ||
4777 | for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) { | |
4778 | if (!QLIST_EMPTY(&bs->op_blockers[i])) { | |
4779 | return false; | |
4780 | } | |
4781 | } | |
4782 | return true; | |
4783 | } | |
4784 | ||
4785 | void bdrv_img_create(const char *filename, const char *fmt, | |
4786 | const char *base_filename, const char *base_fmt, | |
4787 | char *options, uint64_t img_size, int flags, bool quiet, | |
4788 | Error **errp) | |
4789 | { | |
4790 | QemuOptsList *create_opts = NULL; | |
4791 | QemuOpts *opts = NULL; | |
4792 | const char *backing_fmt, *backing_file; | |
4793 | int64_t size; | |
4794 | BlockDriver *drv, *proto_drv; | |
4795 | Error *local_err = NULL; | |
4796 | int ret = 0; | |
4797 | ||
4798 | /* Find driver and parse its options */ | |
4799 | drv = bdrv_find_format(fmt); | |
4800 | if (!drv) { | |
4801 | error_setg(errp, "Unknown file format '%s'", fmt); | |
4802 | return; | |
4803 | } | |
4804 | ||
4805 | proto_drv = bdrv_find_protocol(filename, true, errp); | |
4806 | if (!proto_drv) { | |
4807 | return; | |
4808 | } | |
4809 | ||
4810 | if (!drv->create_opts) { | |
4811 | error_setg(errp, "Format driver '%s' does not support image creation", | |
4812 | drv->format_name); | |
4813 | return; | |
4814 | } | |
4815 | ||
4816 | if (!proto_drv->create_opts) { | |
4817 | error_setg(errp, "Protocol driver '%s' does not support image creation", | |
4818 | proto_drv->format_name); | |
4819 | return; | |
4820 | } | |
4821 | ||
4822 | create_opts = qemu_opts_append(create_opts, drv->create_opts); | |
4823 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); | |
4824 | ||
4825 | /* Create parameter list with default values */ | |
4826 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); | |
4827 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort); | |
4828 | ||
4829 | /* Parse -o options */ | |
4830 | if (options) { | |
4831 | qemu_opts_do_parse(opts, options, NULL, &local_err); | |
4832 | if (local_err) { | |
4833 | goto out; | |
4834 | } | |
4835 | } | |
4836 | ||
4837 | if (base_filename) { | |
4838 | qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &local_err); | |
4839 | if (local_err) { | |
4840 | error_setg(errp, "Backing file not supported for file format '%s'", | |
4841 | fmt); | |
4842 | goto out; | |
4843 | } | |
4844 | } | |
4845 | ||
4846 | if (base_fmt) { | |
4847 | qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &local_err); | |
4848 | if (local_err) { | |
4849 | error_setg(errp, "Backing file format not supported for file " | |
4850 | "format '%s'", fmt); | |
4851 | goto out; | |
4852 | } | |
4853 | } | |
4854 | ||
4855 | backing_file = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE); | |
4856 | if (backing_file) { | |
4857 | if (!strcmp(filename, backing_file)) { | |
4858 | error_setg(errp, "Error: Trying to create an image with the " | |
4859 | "same filename as the backing file"); | |
4860 | goto out; | |
4861 | } | |
4862 | } | |
4863 | ||
4864 | backing_fmt = qemu_opt_get(opts, BLOCK_OPT_BACKING_FMT); | |
4865 | ||
4866 | /* The size for the image must always be specified, unless we have a backing | |
4867 | * file and we have not been forbidden from opening it. */ | |
4868 | size = qemu_opt_get_size(opts, BLOCK_OPT_SIZE, img_size); | |
4869 | if (backing_file && !(flags & BDRV_O_NO_BACKING)) { | |
4870 | BlockDriverState *bs; | |
4871 | char *full_backing = g_new0(char, PATH_MAX); | |
4872 | int back_flags; | |
4873 | QDict *backing_options = NULL; | |
4874 | ||
4875 | bdrv_get_full_backing_filename_from_filename(filename, backing_file, | |
4876 | full_backing, PATH_MAX, | |
4877 | &local_err); | |
4878 | if (local_err) { | |
4879 | g_free(full_backing); | |
4880 | goto out; | |
4881 | } | |
4882 | ||
4883 | /* backing files always opened read-only */ | |
4884 | back_flags = flags; | |
4885 | back_flags &= ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); | |
4886 | ||
4887 | backing_options = qdict_new(); | |
4888 | if (backing_fmt) { | |
4889 | qdict_put_str(backing_options, "driver", backing_fmt); | |
4890 | } | |
4891 | qdict_put_bool(backing_options, BDRV_OPT_FORCE_SHARE, true); | |
4892 | ||
4893 | bs = bdrv_open(full_backing, NULL, backing_options, back_flags, | |
4894 | &local_err); | |
4895 | g_free(full_backing); | |
4896 | if (!bs && size != -1) { | |
4897 | /* Couldn't open BS, but we have a size, so it's nonfatal */ | |
4898 | warn_reportf_err(local_err, | |
4899 | "Could not verify backing image. " | |
4900 | "This may become an error in future versions.\n"); | |
4901 | local_err = NULL; | |
4902 | } else if (!bs) { | |
4903 | /* Couldn't open bs, do not have size */ | |
4904 | error_append_hint(&local_err, | |
4905 | "Could not open backing image to determine size.\n"); | |
4906 | goto out; | |
4907 | } else { | |
4908 | if (size == -1) { | |
4909 | /* Opened BS, have no size */ | |
4910 | size = bdrv_getlength(bs); | |
4911 | if (size < 0) { | |
4912 | error_setg_errno(errp, -size, "Could not get size of '%s'", | |
4913 | backing_file); | |
4914 | bdrv_unref(bs); | |
4915 | goto out; | |
4916 | } | |
4917 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, size, &error_abort); | |
4918 | } | |
4919 | bdrv_unref(bs); | |
4920 | } | |
4921 | } /* (backing_file && !(flags & BDRV_O_NO_BACKING)) */ | |
4922 | ||
4923 | if (size == -1) { | |
4924 | error_setg(errp, "Image creation needs a size parameter"); | |
4925 | goto out; | |
4926 | } | |
4927 | ||
4928 | if (!quiet) { | |
4929 | printf("Formatting '%s', fmt=%s ", filename, fmt); | |
4930 | qemu_opts_print(opts, " "); | |
4931 | puts(""); | |
4932 | } | |
4933 | ||
4934 | ret = bdrv_create(drv, filename, opts, &local_err); | |
4935 | ||
4936 | if (ret == -EFBIG) { | |
4937 | /* This is generally a better message than whatever the driver would | |
4938 | * deliver (especially because of the cluster_size_hint), since that | |
4939 | * is most probably not much different from "image too large". */ | |
4940 | const char *cluster_size_hint = ""; | |
4941 | if (qemu_opt_get_size(opts, BLOCK_OPT_CLUSTER_SIZE, 0)) { | |
4942 | cluster_size_hint = " (try using a larger cluster size)"; | |
4943 | } | |
4944 | error_setg(errp, "The image size is too large for file format '%s'" | |
4945 | "%s", fmt, cluster_size_hint); | |
4946 | error_free(local_err); | |
4947 | local_err = NULL; | |
4948 | } | |
4949 | ||
4950 | out: | |
4951 | qemu_opts_del(opts); | |
4952 | qemu_opts_free(create_opts); | |
4953 | error_propagate(errp, local_err); | |
4954 | } | |
4955 | ||
4956 | AioContext *bdrv_get_aio_context(BlockDriverState *bs) | |
4957 | { | |
4958 | return bs ? bs->aio_context : qemu_get_aio_context(); | |
4959 | } | |
4960 | ||
4961 | void bdrv_coroutine_enter(BlockDriverState *bs, Coroutine *co) | |
4962 | { | |
4963 | aio_co_enter(bdrv_get_aio_context(bs), co); | |
4964 | } | |
4965 | ||
4966 | static void bdrv_do_remove_aio_context_notifier(BdrvAioNotifier *ban) | |
4967 | { | |
4968 | QLIST_REMOVE(ban, list); | |
4969 | g_free(ban); | |
4970 | } | |
4971 | ||
4972 | void bdrv_detach_aio_context(BlockDriverState *bs) | |
4973 | { | |
4974 | BdrvAioNotifier *baf, *baf_tmp; | |
4975 | BdrvChild *child; | |
4976 | ||
4977 | if (!bs->drv) { | |
4978 | return; | |
4979 | } | |
4980 | ||
4981 | assert(!bs->walking_aio_notifiers); | |
4982 | bs->walking_aio_notifiers = true; | |
4983 | QLIST_FOREACH_SAFE(baf, &bs->aio_notifiers, list, baf_tmp) { | |
4984 | if (baf->deleted) { | |
4985 | bdrv_do_remove_aio_context_notifier(baf); | |
4986 | } else { | |
4987 | baf->detach_aio_context(baf->opaque); | |
4988 | } | |
4989 | } | |
4990 | /* Never mind iterating again to check for ->deleted. bdrv_close() will | |
4991 | * remove remaining aio notifiers if we aren't called again. | |
4992 | */ | |
4993 | bs->walking_aio_notifiers = false; | |
4994 | ||
4995 | if (bs->drv->bdrv_detach_aio_context) { | |
4996 | bs->drv->bdrv_detach_aio_context(bs); | |
4997 | } | |
4998 | QLIST_FOREACH(child, &bs->children, next) { | |
4999 | bdrv_detach_aio_context(child->bs); | |
5000 | } | |
5001 | ||
5002 | bs->aio_context = NULL; | |
5003 | } | |
5004 | ||
5005 | void bdrv_attach_aio_context(BlockDriverState *bs, | |
5006 | AioContext *new_context) | |
5007 | { | |
5008 | BdrvAioNotifier *ban, *ban_tmp; | |
5009 | BdrvChild *child; | |
5010 | ||
5011 | if (!bs->drv) { | |
5012 | return; | |
5013 | } | |
5014 | ||
5015 | bs->aio_context = new_context; | |
5016 | ||
5017 | QLIST_FOREACH(child, &bs->children, next) { | |
5018 | bdrv_attach_aio_context(child->bs, new_context); | |
5019 | } | |
5020 | if (bs->drv->bdrv_attach_aio_context) { | |
5021 | bs->drv->bdrv_attach_aio_context(bs, new_context); | |
5022 | } | |
5023 | ||
5024 | assert(!bs->walking_aio_notifiers); | |
5025 | bs->walking_aio_notifiers = true; | |
5026 | QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_tmp) { | |
5027 | if (ban->deleted) { | |
5028 | bdrv_do_remove_aio_context_notifier(ban); | |
5029 | } else { | |
5030 | ban->attached_aio_context(new_context, ban->opaque); | |
5031 | } | |
5032 | } | |
5033 | bs->walking_aio_notifiers = false; | |
5034 | } | |
5035 | ||
5036 | void bdrv_set_aio_context(BlockDriverState *bs, AioContext *new_context) | |
5037 | { | |
5038 | AioContext *ctx = bdrv_get_aio_context(bs); | |
5039 | ||
5040 | aio_disable_external(ctx); | |
5041 | bdrv_parent_drained_begin(bs, NULL, false); | |
5042 | bdrv_drain(bs); /* ensure there are no in-flight requests */ | |
5043 | ||
5044 | while (aio_poll(ctx, false)) { | |
5045 | /* wait for all bottom halves to execute */ | |
5046 | } | |
5047 | ||
5048 | bdrv_detach_aio_context(bs); | |
5049 | ||
5050 | /* This function executes in the old AioContext so acquire the new one in | |
5051 | * case it runs in a different thread. | |
5052 | */ | |
5053 | aio_context_acquire(new_context); | |
5054 | bdrv_attach_aio_context(bs, new_context); | |
5055 | bdrv_parent_drained_end(bs, NULL, false); | |
5056 | aio_enable_external(ctx); | |
5057 | aio_context_release(new_context); | |
5058 | } | |
5059 | ||
5060 | void bdrv_add_aio_context_notifier(BlockDriverState *bs, | |
5061 | void (*attached_aio_context)(AioContext *new_context, void *opaque), | |
5062 | void (*detach_aio_context)(void *opaque), void *opaque) | |
5063 | { | |
5064 | BdrvAioNotifier *ban = g_new(BdrvAioNotifier, 1); | |
5065 | *ban = (BdrvAioNotifier){ | |
5066 | .attached_aio_context = attached_aio_context, | |
5067 | .detach_aio_context = detach_aio_context, | |
5068 | .opaque = opaque | |
5069 | }; | |
5070 | ||
5071 | QLIST_INSERT_HEAD(&bs->aio_notifiers, ban, list); | |
5072 | } | |
5073 | ||
5074 | void bdrv_remove_aio_context_notifier(BlockDriverState *bs, | |
5075 | void (*attached_aio_context)(AioContext *, | |
5076 | void *), | |
5077 | void (*detach_aio_context)(void *), | |
5078 | void *opaque) | |
5079 | { | |
5080 | BdrvAioNotifier *ban, *ban_next; | |
5081 | ||
5082 | QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) { | |
5083 | if (ban->attached_aio_context == attached_aio_context && | |
5084 | ban->detach_aio_context == detach_aio_context && | |
5085 | ban->opaque == opaque && | |
5086 | ban->deleted == false) | |
5087 | { | |
5088 | if (bs->walking_aio_notifiers) { | |
5089 | ban->deleted = true; | |
5090 | } else { | |
5091 | bdrv_do_remove_aio_context_notifier(ban); | |
5092 | } | |
5093 | return; | |
5094 | } | |
5095 | } | |
5096 | ||
5097 | abort(); | |
5098 | } | |
5099 | ||
5100 | int bdrv_amend_options(BlockDriverState *bs, QemuOpts *opts, | |
5101 | BlockDriverAmendStatusCB *status_cb, void *cb_opaque, | |
5102 | Error **errp) | |
5103 | { | |
5104 | if (!bs->drv) { | |
5105 | error_setg(errp, "Node is ejected"); | |
5106 | return -ENOMEDIUM; | |
5107 | } | |
5108 | if (!bs->drv->bdrv_amend_options) { | |
5109 | error_setg(errp, "Block driver '%s' does not support option amendment", | |
5110 | bs->drv->format_name); | |
5111 | return -ENOTSUP; | |
5112 | } | |
5113 | return bs->drv->bdrv_amend_options(bs, opts, status_cb, cb_opaque, errp); | |
5114 | } | |
5115 | ||
5116 | /* This function will be called by the bdrv_recurse_is_first_non_filter method | |
5117 | * of block filter and by bdrv_is_first_non_filter. | |
5118 | * It is used to test if the given bs is the candidate or recurse more in the | |
5119 | * node graph. | |
5120 | */ | |
5121 | bool bdrv_recurse_is_first_non_filter(BlockDriverState *bs, | |
5122 | BlockDriverState *candidate) | |
5123 | { | |
5124 | /* return false if basic checks fails */ | |
5125 | if (!bs || !bs->drv) { | |
5126 | return false; | |
5127 | } | |
5128 | ||
5129 | /* the code reached a non block filter driver -> check if the bs is | |
5130 | * the same as the candidate. It's the recursion termination condition. | |
5131 | */ | |
5132 | if (!bs->drv->is_filter) { | |
5133 | return bs == candidate; | |
5134 | } | |
5135 | /* Down this path the driver is a block filter driver */ | |
5136 | ||
5137 | /* If the block filter recursion method is defined use it to recurse down | |
5138 | * the node graph. | |
5139 | */ | |
5140 | if (bs->drv->bdrv_recurse_is_first_non_filter) { | |
5141 | return bs->drv->bdrv_recurse_is_first_non_filter(bs, candidate); | |
5142 | } | |
5143 | ||
5144 | /* the driver is a block filter but don't allow to recurse -> return false | |
5145 | */ | |
5146 | return false; | |
5147 | } | |
5148 | ||
5149 | /* This function checks if the candidate is the first non filter bs down it's | |
5150 | * bs chain. Since we don't have pointers to parents it explore all bs chains | |
5151 | * from the top. Some filters can choose not to pass down the recursion. | |
5152 | */ | |
5153 | bool bdrv_is_first_non_filter(BlockDriverState *candidate) | |
5154 | { | |
5155 | BlockDriverState *bs; | |
5156 | BdrvNextIterator it; | |
5157 | ||
5158 | /* walk down the bs forest recursively */ | |
5159 | for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) { | |
5160 | bool perm; | |
5161 | ||
5162 | /* try to recurse in this top level bs */ | |
5163 | perm = bdrv_recurse_is_first_non_filter(bs, candidate); | |
5164 | ||
5165 | /* candidate is the first non filter */ | |
5166 | if (perm) { | |
5167 | bdrv_next_cleanup(&it); | |
5168 | return true; | |
5169 | } | |
5170 | } | |
5171 | ||
5172 | return false; | |
5173 | } | |
5174 | ||
5175 | BlockDriverState *check_to_replace_node(BlockDriverState *parent_bs, | |
5176 | const char *node_name, Error **errp) | |
5177 | { | |
5178 | BlockDriverState *to_replace_bs = bdrv_find_node(node_name); | |
5179 | AioContext *aio_context; | |
5180 | ||
5181 | if (!to_replace_bs) { | |
5182 | error_setg(errp, "Node name '%s' not found", node_name); | |
5183 | return NULL; | |
5184 | } | |
5185 | ||
5186 | aio_context = bdrv_get_aio_context(to_replace_bs); | |
5187 | aio_context_acquire(aio_context); | |
5188 | ||
5189 | if (bdrv_op_is_blocked(to_replace_bs, BLOCK_OP_TYPE_REPLACE, errp)) { | |
5190 | to_replace_bs = NULL; | |
5191 | goto out; | |
5192 | } | |
5193 | ||
5194 | /* We don't want arbitrary node of the BDS chain to be replaced only the top | |
5195 | * most non filter in order to prevent data corruption. | |
5196 | * Another benefit is that this tests exclude backing files which are | |
5197 | * blocked by the backing blockers. | |
5198 | */ | |
5199 | if (!bdrv_recurse_is_first_non_filter(parent_bs, to_replace_bs)) { | |
5200 | error_setg(errp, "Only top most non filter can be replaced"); | |
5201 | to_replace_bs = NULL; | |
5202 | goto out; | |
5203 | } | |
5204 | ||
5205 | out: | |
5206 | aio_context_release(aio_context); | |
5207 | return to_replace_bs; | |
5208 | } | |
5209 | ||
5210 | static bool append_open_options(QDict *d, BlockDriverState *bs) | |
5211 | { | |
5212 | const QDictEntry *entry; | |
5213 | QemuOptDesc *desc; | |
5214 | bool found_any = false; | |
5215 | ||
5216 | for (entry = qdict_first(bs->options); entry; | |
5217 | entry = qdict_next(bs->options, entry)) | |
5218 | { | |
5219 | /* Exclude all non-driver-specific options */ | |
5220 | for (desc = bdrv_runtime_opts.desc; desc->name; desc++) { | |
5221 | if (!strcmp(qdict_entry_key(entry), desc->name)) { | |
5222 | break; | |
5223 | } | |
5224 | } | |
5225 | if (desc->name) { | |
5226 | continue; | |
5227 | } | |
5228 | ||
5229 | qdict_put_obj(d, qdict_entry_key(entry), | |
5230 | qobject_ref(qdict_entry_value(entry))); | |
5231 | found_any = true; | |
5232 | } | |
5233 | ||
5234 | return found_any; | |
5235 | } | |
5236 | ||
5237 | /* Updates the following BDS fields: | |
5238 | * - exact_filename: A filename which may be used for opening a block device | |
5239 | * which (mostly) equals the given BDS (even without any | |
5240 | * other options; so reading and writing must return the same | |
5241 | * results, but caching etc. may be different) | |
5242 | * - full_open_options: Options which, when given when opening a block device | |
5243 | * (without a filename), result in a BDS (mostly) | |
5244 | * equalling the given one | |
5245 | * - filename: If exact_filename is set, it is copied here. Otherwise, | |
5246 | * full_open_options is converted to a JSON object, prefixed with | |
5247 | * "json:" (for use through the JSON pseudo protocol) and put here. | |
5248 | */ | |
5249 | void bdrv_refresh_filename(BlockDriverState *bs) | |
5250 | { | |
5251 | BlockDriver *drv = bs->drv; | |
5252 | QDict *opts; | |
5253 | ||
5254 | if (!drv) { | |
5255 | return; | |
5256 | } | |
5257 | ||
5258 | /* This BDS's file name will most probably depend on its file's name, so | |
5259 | * refresh that first */ | |
5260 | if (bs->file) { | |
5261 | bdrv_refresh_filename(bs->file->bs); | |
5262 | } | |
5263 | ||
5264 | if (drv->bdrv_refresh_filename) { | |
5265 | /* Obsolete information is of no use here, so drop the old file name | |
5266 | * information before refreshing it */ | |
5267 | bs->exact_filename[0] = '\0'; | |
5268 | if (bs->full_open_options) { | |
5269 | qobject_unref(bs->full_open_options); | |
5270 | bs->full_open_options = NULL; | |
5271 | } | |
5272 | ||
5273 | opts = qdict_new(); | |
5274 | append_open_options(opts, bs); | |
5275 | drv->bdrv_refresh_filename(bs, opts); | |
5276 | qobject_unref(opts); | |
5277 | } else if (bs->file) { | |
5278 | /* Try to reconstruct valid information from the underlying file */ | |
5279 | bool has_open_options; | |
5280 | ||
5281 | bs->exact_filename[0] = '\0'; | |
5282 | if (bs->full_open_options) { | |
5283 | qobject_unref(bs->full_open_options); | |
5284 | bs->full_open_options = NULL; | |
5285 | } | |
5286 | ||
5287 | opts = qdict_new(); | |
5288 | has_open_options = append_open_options(opts, bs); | |
5289 | ||
5290 | /* If no specific options have been given for this BDS, the filename of | |
5291 | * the underlying file should suffice for this one as well */ | |
5292 | if (bs->file->bs->exact_filename[0] && !has_open_options) { | |
5293 | strcpy(bs->exact_filename, bs->file->bs->exact_filename); | |
5294 | } | |
5295 | /* Reconstructing the full options QDict is simple for most format block | |
5296 | * drivers, as long as the full options are known for the underlying | |
5297 | * file BDS. The full options QDict of that file BDS should somehow | |
5298 | * contain a representation of the filename, therefore the following | |
5299 | * suffices without querying the (exact_)filename of this BDS. */ | |
5300 | if (bs->file->bs->full_open_options) { | |
5301 | qdict_put_str(opts, "driver", drv->format_name); | |
5302 | qdict_put(opts, "file", | |
5303 | qobject_ref(bs->file->bs->full_open_options)); | |
5304 | ||
5305 | bs->full_open_options = opts; | |
5306 | } else { | |
5307 | qobject_unref(opts); | |
5308 | } | |
5309 | } else if (!bs->full_open_options && qdict_size(bs->options)) { | |
5310 | /* There is no underlying file BDS (at least referenced by BDS.file), | |
5311 | * so the full options QDict should be equal to the options given | |
5312 | * specifically for this block device when it was opened (plus the | |
5313 | * driver specification). | |
5314 | * Because those options don't change, there is no need to update | |
5315 | * full_open_options when it's already set. */ | |
5316 | ||
5317 | opts = qdict_new(); | |
5318 | append_open_options(opts, bs); | |
5319 | qdict_put_str(opts, "driver", drv->format_name); | |
5320 | ||
5321 | if (bs->exact_filename[0]) { | |
5322 | /* This may not work for all block protocol drivers (some may | |
5323 | * require this filename to be parsed), but we have to find some | |
5324 | * default solution here, so just include it. If some block driver | |
5325 | * does not support pure options without any filename at all or | |
5326 | * needs some special format of the options QDict, it needs to | |
5327 | * implement the driver-specific bdrv_refresh_filename() function. | |
5328 | */ | |
5329 | qdict_put_str(opts, "filename", bs->exact_filename); | |
5330 | } | |
5331 | ||
5332 | bs->full_open_options = opts; | |
5333 | } | |
5334 | ||
5335 | if (bs->exact_filename[0]) { | |
5336 | pstrcpy(bs->filename, sizeof(bs->filename), bs->exact_filename); | |
5337 | } else if (bs->full_open_options) { | |
5338 | QString *json = qobject_to_json(QOBJECT(bs->full_open_options)); | |
5339 | snprintf(bs->filename, sizeof(bs->filename), "json:%s", | |
5340 | qstring_get_str(json)); | |
5341 | qobject_unref(json); | |
5342 | } | |
5343 | } | |
5344 | ||
5345 | /* | |
5346 | * Hot add/remove a BDS's child. So the user can take a child offline when | |
5347 | * it is broken and take a new child online | |
5348 | */ | |
5349 | void bdrv_add_child(BlockDriverState *parent_bs, BlockDriverState *child_bs, | |
5350 | Error **errp) | |
5351 | { | |
5352 | ||
5353 | if (!parent_bs->drv || !parent_bs->drv->bdrv_add_child) { | |
5354 | error_setg(errp, "The node %s does not support adding a child", | |
5355 | bdrv_get_device_or_node_name(parent_bs)); | |
5356 | return; | |
5357 | } | |
5358 | ||
5359 | if (!QLIST_EMPTY(&child_bs->parents)) { | |
5360 | error_setg(errp, "The node %s already has a parent", | |
5361 | child_bs->node_name); | |
5362 | return; | |
5363 | } | |
5364 | ||
5365 | parent_bs->drv->bdrv_add_child(parent_bs, child_bs, errp); | |
5366 | } | |
5367 | ||
5368 | void bdrv_del_child(BlockDriverState *parent_bs, BdrvChild *child, Error **errp) | |
5369 | { | |
5370 | BdrvChild *tmp; | |
5371 | ||
5372 | if (!parent_bs->drv || !parent_bs->drv->bdrv_del_child) { | |
5373 | error_setg(errp, "The node %s does not support removing a child", | |
5374 | bdrv_get_device_or_node_name(parent_bs)); | |
5375 | return; | |
5376 | } | |
5377 | ||
5378 | QLIST_FOREACH(tmp, &parent_bs->children, next) { | |
5379 | if (tmp == child) { | |
5380 | break; | |
5381 | } | |
5382 | } | |
5383 | ||
5384 | if (!tmp) { | |
5385 | error_setg(errp, "The node %s does not have a child named %s", | |
5386 | bdrv_get_device_or_node_name(parent_bs), | |
5387 | bdrv_get_device_or_node_name(child->bs)); | |
5388 | return; | |
5389 | } | |
5390 | ||
5391 | parent_bs->drv->bdrv_del_child(parent_bs, child, errp); | |
5392 | } | |
5393 | ||
5394 | bool bdrv_can_store_new_dirty_bitmap(BlockDriverState *bs, const char *name, | |
5395 | uint32_t granularity, Error **errp) | |
5396 | { | |
5397 | BlockDriver *drv = bs->drv; | |
5398 | ||
5399 | if (!drv) { | |
5400 | error_setg_errno(errp, ENOMEDIUM, | |
5401 | "Can't store persistent bitmaps to %s", | |
5402 | bdrv_get_device_or_node_name(bs)); | |
5403 | return false; | |
5404 | } | |
5405 | ||
5406 | if (!drv->bdrv_can_store_new_dirty_bitmap) { | |
5407 | error_setg_errno(errp, ENOTSUP, | |
5408 | "Can't store persistent bitmaps to %s", | |
5409 | bdrv_get_device_or_node_name(bs)); | |
5410 | return false; | |
5411 | } | |
5412 | ||
5413 | return drv->bdrv_can_store_new_dirty_bitmap(bs, name, granularity, errp); | |
5414 | } |