blockdev: Add list of all BlockBackends
[qemu/ar7.git] / block.c
blob582a9e8ce3846e2423b5723f05e38a0355d1ce57
1 /*
2 * QEMU System Emulator block driver
4 * Copyright (c) 2003 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 #include "qemu/osdep.h"
25 #include "qemu-common.h"
26 #include "trace.h"
27 #include "block/block_int.h"
28 #include "block/blockjob.h"
29 #include "qemu/error-report.h"
30 #include "qemu/module.h"
31 #include "qapi/qmp/qerror.h"
32 #include "qapi/qmp/qbool.h"
33 #include "qapi/qmp/qjson.h"
34 #include "sysemu/block-backend.h"
35 #include "sysemu/sysemu.h"
36 #include "qemu/notify.h"
37 #include "qemu/coroutine.h"
38 #include "block/qapi.h"
39 #include "qmp-commands.h"
40 #include "qemu/timer.h"
41 #include "qapi-event.h"
42 #include "block/throttle-groups.h"
44 #ifdef CONFIG_BSD
45 #include <sys/ioctl.h>
46 #include <sys/queue.h>
47 #ifndef __DragonFly__
48 #include <sys/disk.h>
49 #endif
50 #endif
52 #ifdef _WIN32
53 #include <windows.h>
54 #endif
56 #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
58 struct BdrvStates bdrv_states = QTAILQ_HEAD_INITIALIZER(bdrv_states);
60 static QTAILQ_HEAD(, BlockDriverState) graph_bdrv_states =
61 QTAILQ_HEAD_INITIALIZER(graph_bdrv_states);
63 static QTAILQ_HEAD(, BlockDriverState) all_bdrv_states =
64 QTAILQ_HEAD_INITIALIZER(all_bdrv_states);
66 static QLIST_HEAD(, BlockDriver) bdrv_drivers =
67 QLIST_HEAD_INITIALIZER(bdrv_drivers);
69 static int bdrv_open_inherit(BlockDriverState **pbs, const char *filename,
70 const char *reference, QDict *options, int flags,
71 BlockDriverState *parent,
72 const BdrvChildRole *child_role, Error **errp);
74 /* If non-zero, use only whitelisted block drivers */
75 static int use_bdrv_whitelist;
77 static void bdrv_close(BlockDriverState *bs);
79 #ifdef _WIN32
80 static int is_windows_drive_prefix(const char *filename)
82 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
83 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
84 filename[1] == ':');
87 int is_windows_drive(const char *filename)
89 if (is_windows_drive_prefix(filename) &&
90 filename[2] == '\0')
91 return 1;
92 if (strstart(filename, "\\\\.\\", NULL) ||
93 strstart(filename, "//./", NULL))
94 return 1;
95 return 0;
97 #endif
99 size_t bdrv_opt_mem_align(BlockDriverState *bs)
101 if (!bs || !bs->drv) {
102 /* page size or 4k (hdd sector size) should be on the safe side */
103 return MAX(4096, getpagesize());
106 return bs->bl.opt_mem_alignment;
109 size_t bdrv_min_mem_align(BlockDriverState *bs)
111 if (!bs || !bs->drv) {
112 /* page size or 4k (hdd sector size) should be on the safe side */
113 return MAX(4096, getpagesize());
116 return bs->bl.min_mem_alignment;
119 /* check if the path starts with "<protocol>:" */
120 int path_has_protocol(const char *path)
122 const char *p;
124 #ifdef _WIN32
125 if (is_windows_drive(path) ||
126 is_windows_drive_prefix(path)) {
127 return 0;
129 p = path + strcspn(path, ":/\\");
130 #else
131 p = path + strcspn(path, ":/");
132 #endif
134 return *p == ':';
137 int path_is_absolute(const char *path)
139 #ifdef _WIN32
140 /* specific case for names like: "\\.\d:" */
141 if (is_windows_drive(path) || is_windows_drive_prefix(path)) {
142 return 1;
144 return (*path == '/' || *path == '\\');
145 #else
146 return (*path == '/');
147 #endif
150 /* if filename is absolute, just copy it to dest. Otherwise, build a
151 path to it by considering it is relative to base_path. URL are
152 supported. */
153 void path_combine(char *dest, int dest_size,
154 const char *base_path,
155 const char *filename)
157 const char *p, *p1;
158 int len;
160 if (dest_size <= 0)
161 return;
162 if (path_is_absolute(filename)) {
163 pstrcpy(dest, dest_size, filename);
164 } else {
165 p = strchr(base_path, ':');
166 if (p)
167 p++;
168 else
169 p = base_path;
170 p1 = strrchr(base_path, '/');
171 #ifdef _WIN32
173 const char *p2;
174 p2 = strrchr(base_path, '\\');
175 if (!p1 || p2 > p1)
176 p1 = p2;
178 #endif
179 if (p1)
180 p1++;
181 else
182 p1 = base_path;
183 if (p1 > p)
184 p = p1;
185 len = p - base_path;
186 if (len > dest_size - 1)
187 len = dest_size - 1;
188 memcpy(dest, base_path, len);
189 dest[len] = '\0';
190 pstrcat(dest, dest_size, filename);
194 void bdrv_get_full_backing_filename_from_filename(const char *backed,
195 const char *backing,
196 char *dest, size_t sz,
197 Error **errp)
199 if (backing[0] == '\0' || path_has_protocol(backing) ||
200 path_is_absolute(backing))
202 pstrcpy(dest, sz, backing);
203 } else if (backed[0] == '\0' || strstart(backed, "json:", NULL)) {
204 error_setg(errp, "Cannot use relative backing file names for '%s'",
205 backed);
206 } else {
207 path_combine(dest, sz, backed, backing);
211 void bdrv_get_full_backing_filename(BlockDriverState *bs, char *dest, size_t sz,
212 Error **errp)
214 char *backed = bs->exact_filename[0] ? bs->exact_filename : bs->filename;
216 bdrv_get_full_backing_filename_from_filename(backed, bs->backing_file,
217 dest, sz, errp);
220 void bdrv_register(BlockDriver *bdrv)
222 bdrv_setup_io_funcs(bdrv);
224 QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list);
227 BlockDriverState *bdrv_new_root(void)
229 BlockDriverState *bs = bdrv_new();
231 QTAILQ_INSERT_TAIL(&bdrv_states, bs, device_list);
232 return bs;
235 BlockDriverState *bdrv_new(void)
237 BlockDriverState *bs;
238 int i;
240 bs = g_new0(BlockDriverState, 1);
241 QLIST_INIT(&bs->dirty_bitmaps);
242 for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
243 QLIST_INIT(&bs->op_blockers[i]);
245 notifier_with_return_list_init(&bs->before_write_notifiers);
246 qemu_co_queue_init(&bs->throttled_reqs[0]);
247 qemu_co_queue_init(&bs->throttled_reqs[1]);
248 bs->refcnt = 1;
249 bs->aio_context = qemu_get_aio_context();
251 QTAILQ_INSERT_TAIL(&all_bdrv_states, bs, bs_list);
253 return bs;
256 BlockDriver *bdrv_find_format(const char *format_name)
258 BlockDriver *drv1;
259 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
260 if (!strcmp(drv1->format_name, format_name)) {
261 return drv1;
264 return NULL;
267 static int bdrv_is_whitelisted(BlockDriver *drv, bool read_only)
269 static const char *whitelist_rw[] = {
270 CONFIG_BDRV_RW_WHITELIST
272 static const char *whitelist_ro[] = {
273 CONFIG_BDRV_RO_WHITELIST
275 const char **p;
277 if (!whitelist_rw[0] && !whitelist_ro[0]) {
278 return 1; /* no whitelist, anything goes */
281 for (p = whitelist_rw; *p; p++) {
282 if (!strcmp(drv->format_name, *p)) {
283 return 1;
286 if (read_only) {
287 for (p = whitelist_ro; *p; p++) {
288 if (!strcmp(drv->format_name, *p)) {
289 return 1;
293 return 0;
296 typedef struct CreateCo {
297 BlockDriver *drv;
298 char *filename;
299 QemuOpts *opts;
300 int ret;
301 Error *err;
302 } CreateCo;
304 static void coroutine_fn bdrv_create_co_entry(void *opaque)
306 Error *local_err = NULL;
307 int ret;
309 CreateCo *cco = opaque;
310 assert(cco->drv);
312 ret = cco->drv->bdrv_create(cco->filename, cco->opts, &local_err);
313 if (local_err) {
314 error_propagate(&cco->err, local_err);
316 cco->ret = ret;
319 int bdrv_create(BlockDriver *drv, const char* filename,
320 QemuOpts *opts, Error **errp)
322 int ret;
324 Coroutine *co;
325 CreateCo cco = {
326 .drv = drv,
327 .filename = g_strdup(filename),
328 .opts = opts,
329 .ret = NOT_DONE,
330 .err = NULL,
333 if (!drv->bdrv_create) {
334 error_setg(errp, "Driver '%s' does not support image creation", drv->format_name);
335 ret = -ENOTSUP;
336 goto out;
339 if (qemu_in_coroutine()) {
340 /* Fast-path if already in coroutine context */
341 bdrv_create_co_entry(&cco);
342 } else {
343 co = qemu_coroutine_create(bdrv_create_co_entry);
344 qemu_coroutine_enter(co, &cco);
345 while (cco.ret == NOT_DONE) {
346 aio_poll(qemu_get_aio_context(), true);
350 ret = cco.ret;
351 if (ret < 0) {
352 if (cco.err) {
353 error_propagate(errp, cco.err);
354 } else {
355 error_setg_errno(errp, -ret, "Could not create image");
359 out:
360 g_free(cco.filename);
361 return ret;
364 int bdrv_create_file(const char *filename, QemuOpts *opts, Error **errp)
366 BlockDriver *drv;
367 Error *local_err = NULL;
368 int ret;
370 drv = bdrv_find_protocol(filename, true, errp);
371 if (drv == NULL) {
372 return -ENOENT;
375 ret = bdrv_create(drv, filename, opts, &local_err);
376 if (local_err) {
377 error_propagate(errp, local_err);
379 return ret;
383 * Try to get @bs's logical and physical block size.
384 * On success, store them in @bsz struct and return 0.
385 * On failure return -errno.
386 * @bs must not be empty.
388 int bdrv_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz)
390 BlockDriver *drv = bs->drv;
392 if (drv && drv->bdrv_probe_blocksizes) {
393 return drv->bdrv_probe_blocksizes(bs, bsz);
396 return -ENOTSUP;
400 * Try to get @bs's geometry (cyls, heads, sectors).
401 * On success, store them in @geo struct and return 0.
402 * On failure return -errno.
403 * @bs must not be empty.
405 int bdrv_probe_geometry(BlockDriverState *bs, HDGeometry *geo)
407 BlockDriver *drv = bs->drv;
409 if (drv && drv->bdrv_probe_geometry) {
410 return drv->bdrv_probe_geometry(bs, geo);
413 return -ENOTSUP;
417 * Create a uniquely-named empty temporary file.
418 * Return 0 upon success, otherwise a negative errno value.
420 int get_tmp_filename(char *filename, int size)
422 #ifdef _WIN32
423 char temp_dir[MAX_PATH];
424 /* GetTempFileName requires that its output buffer (4th param)
425 have length MAX_PATH or greater. */
426 assert(size >= MAX_PATH);
427 return (GetTempPath(MAX_PATH, temp_dir)
428 && GetTempFileName(temp_dir, "qem", 0, filename)
429 ? 0 : -GetLastError());
430 #else
431 int fd;
432 const char *tmpdir;
433 tmpdir = getenv("TMPDIR");
434 if (!tmpdir) {
435 tmpdir = "/var/tmp";
437 if (snprintf(filename, size, "%s/vl.XXXXXX", tmpdir) >= size) {
438 return -EOVERFLOW;
440 fd = mkstemp(filename);
441 if (fd < 0) {
442 return -errno;
444 if (close(fd) != 0) {
445 unlink(filename);
446 return -errno;
448 return 0;
449 #endif
453 * Detect host devices. By convention, /dev/cdrom[N] is always
454 * recognized as a host CDROM.
456 static BlockDriver *find_hdev_driver(const char *filename)
458 int score_max = 0, score;
459 BlockDriver *drv = NULL, *d;
461 QLIST_FOREACH(d, &bdrv_drivers, list) {
462 if (d->bdrv_probe_device) {
463 score = d->bdrv_probe_device(filename);
464 if (score > score_max) {
465 score_max = score;
466 drv = d;
471 return drv;
474 BlockDriver *bdrv_find_protocol(const char *filename,
475 bool allow_protocol_prefix,
476 Error **errp)
478 BlockDriver *drv1;
479 char protocol[128];
480 int len;
481 const char *p;
483 /* TODO Drivers without bdrv_file_open must be specified explicitly */
486 * XXX(hch): we really should not let host device detection
487 * override an explicit protocol specification, but moving this
488 * later breaks access to device names with colons in them.
489 * Thanks to the brain-dead persistent naming schemes on udev-
490 * based Linux systems those actually are quite common.
492 drv1 = find_hdev_driver(filename);
493 if (drv1) {
494 return drv1;
497 if (!path_has_protocol(filename) || !allow_protocol_prefix) {
498 return &bdrv_file;
501 p = strchr(filename, ':');
502 assert(p != NULL);
503 len = p - filename;
504 if (len > sizeof(protocol) - 1)
505 len = sizeof(protocol) - 1;
506 memcpy(protocol, filename, len);
507 protocol[len] = '\0';
508 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
509 if (drv1->protocol_name &&
510 !strcmp(drv1->protocol_name, protocol)) {
511 return drv1;
515 error_setg(errp, "Unknown protocol '%s'", protocol);
516 return NULL;
520 * Guess image format by probing its contents.
521 * This is not a good idea when your image is raw (CVE-2008-2004), but
522 * we do it anyway for backward compatibility.
524 * @buf contains the image's first @buf_size bytes.
525 * @buf_size is the buffer size in bytes (generally BLOCK_PROBE_BUF_SIZE,
526 * but can be smaller if the image file is smaller)
527 * @filename is its filename.
529 * For all block drivers, call the bdrv_probe() method to get its
530 * probing score.
531 * Return the first block driver with the highest probing score.
533 BlockDriver *bdrv_probe_all(const uint8_t *buf, int buf_size,
534 const char *filename)
536 int score_max = 0, score;
537 BlockDriver *drv = NULL, *d;
539 QLIST_FOREACH(d, &bdrv_drivers, list) {
540 if (d->bdrv_probe) {
541 score = d->bdrv_probe(buf, buf_size, filename);
542 if (score > score_max) {
543 score_max = score;
544 drv = d;
549 return drv;
552 static int find_image_format(BlockDriverState *bs, const char *filename,
553 BlockDriver **pdrv, Error **errp)
555 BlockDriver *drv;
556 uint8_t buf[BLOCK_PROBE_BUF_SIZE];
557 int ret = 0;
559 /* Return the raw BlockDriver * to scsi-generic devices or empty drives */
560 if (bdrv_is_sg(bs) || !bdrv_is_inserted(bs) || bdrv_getlength(bs) == 0) {
561 *pdrv = &bdrv_raw;
562 return ret;
565 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
566 if (ret < 0) {
567 error_setg_errno(errp, -ret, "Could not read image for determining its "
568 "format");
569 *pdrv = NULL;
570 return ret;
573 drv = bdrv_probe_all(buf, ret, filename);
574 if (!drv) {
575 error_setg(errp, "Could not determine image format: No compatible "
576 "driver found");
577 ret = -ENOENT;
579 *pdrv = drv;
580 return ret;
584 * Set the current 'total_sectors' value
585 * Return 0 on success, -errno on error.
587 static int refresh_total_sectors(BlockDriverState *bs, int64_t hint)
589 BlockDriver *drv = bs->drv;
591 /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */
592 if (bdrv_is_sg(bs))
593 return 0;
595 /* query actual device if possible, otherwise just trust the hint */
596 if (drv->bdrv_getlength) {
597 int64_t length = drv->bdrv_getlength(bs);
598 if (length < 0) {
599 return length;
601 hint = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE);
604 bs->total_sectors = hint;
605 return 0;
609 * Combines a QDict of new block driver @options with any missing options taken
610 * from @old_options, so that leaving out an option defaults to its old value.
612 static void bdrv_join_options(BlockDriverState *bs, QDict *options,
613 QDict *old_options)
615 if (bs->drv && bs->drv->bdrv_join_options) {
616 bs->drv->bdrv_join_options(options, old_options);
617 } else {
618 qdict_join(options, old_options, false);
623 * Set open flags for a given discard mode
625 * Return 0 on success, -1 if the discard mode was invalid.
627 int bdrv_parse_discard_flags(const char *mode, int *flags)
629 *flags &= ~BDRV_O_UNMAP;
631 if (!strcmp(mode, "off") || !strcmp(mode, "ignore")) {
632 /* do nothing */
633 } else if (!strcmp(mode, "on") || !strcmp(mode, "unmap")) {
634 *flags |= BDRV_O_UNMAP;
635 } else {
636 return -1;
639 return 0;
643 * Set open flags for a given cache mode
645 * Return 0 on success, -1 if the cache mode was invalid.
647 int bdrv_parse_cache_flags(const char *mode, int *flags)
649 *flags &= ~BDRV_O_CACHE_MASK;
651 if (!strcmp(mode, "off") || !strcmp(mode, "none")) {
652 *flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
653 } else if (!strcmp(mode, "directsync")) {
654 *flags |= BDRV_O_NOCACHE;
655 } else if (!strcmp(mode, "writeback")) {
656 *flags |= BDRV_O_CACHE_WB;
657 } else if (!strcmp(mode, "unsafe")) {
658 *flags |= BDRV_O_CACHE_WB;
659 *flags |= BDRV_O_NO_FLUSH;
660 } else if (!strcmp(mode, "writethrough")) {
661 /* this is the default */
662 } else {
663 return -1;
666 return 0;
670 * Returns the options and flags that a temporary snapshot should get, based on
671 * the originally requested flags (the originally requested image will have
672 * flags like a backing file)
674 static void bdrv_temp_snapshot_options(int *child_flags, QDict *child_options,
675 int parent_flags, QDict *parent_options)
677 *child_flags = (parent_flags & ~BDRV_O_SNAPSHOT) | BDRV_O_TEMPORARY;
679 /* For temporary files, unconditional cache=unsafe is fine */
680 qdict_set_default_str(child_options, BDRV_OPT_CACHE_WB, "on");
681 qdict_set_default_str(child_options, BDRV_OPT_CACHE_DIRECT, "off");
682 qdict_set_default_str(child_options, BDRV_OPT_CACHE_NO_FLUSH, "on");
686 * Returns the options and flags that bs->file should get if a protocol driver
687 * is expected, based on the given options and flags for the parent BDS
689 static void bdrv_inherited_options(int *child_flags, QDict *child_options,
690 int parent_flags, QDict *parent_options)
692 int flags = parent_flags;
694 /* Enable protocol handling, disable format probing for bs->file */
695 flags |= BDRV_O_PROTOCOL;
697 /* If the cache mode isn't explicitly set, inherit direct and no-flush from
698 * the parent. */
699 qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_DIRECT);
700 qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_NO_FLUSH);
702 /* Our block drivers take care to send flushes and respect unmap policy,
703 * so we can default to enable both on lower layers regardless of the
704 * corresponding parent options. */
705 qdict_set_default_str(child_options, BDRV_OPT_CACHE_WB, "on");
706 flags |= BDRV_O_UNMAP;
708 /* Clear flags that only apply to the top layer */
709 flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_COPY_ON_READ);
711 *child_flags = flags;
714 const BdrvChildRole child_file = {
715 .inherit_options = bdrv_inherited_options,
719 * Returns the options and flags that bs->file should get if the use of formats
720 * (and not only protocols) is permitted for it, based on the given options and
721 * flags for the parent BDS
723 static void bdrv_inherited_fmt_options(int *child_flags, QDict *child_options,
724 int parent_flags, QDict *parent_options)
726 child_file.inherit_options(child_flags, child_options,
727 parent_flags, parent_options);
729 *child_flags &= ~BDRV_O_PROTOCOL;
732 const BdrvChildRole child_format = {
733 .inherit_options = bdrv_inherited_fmt_options,
737 * Returns the options and flags that bs->backing should get, based on the
738 * given options and flags for the parent BDS
740 static void bdrv_backing_options(int *child_flags, QDict *child_options,
741 int parent_flags, QDict *parent_options)
743 int flags = parent_flags;
745 /* The cache mode is inherited unmodified for backing files */
746 qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_WB);
747 qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_DIRECT);
748 qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_NO_FLUSH);
750 /* backing files always opened read-only */
751 flags &= ~(BDRV_O_RDWR | BDRV_O_COPY_ON_READ);
753 /* snapshot=on is handled on the top layer */
754 flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_TEMPORARY);
756 *child_flags = flags;
759 static const BdrvChildRole child_backing = {
760 .inherit_options = bdrv_backing_options,
763 static int bdrv_open_flags(BlockDriverState *bs, int flags)
765 int open_flags = flags | BDRV_O_CACHE_WB;
768 * Clear flags that are internal to the block layer before opening the
769 * image.
771 open_flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_PROTOCOL);
774 * Snapshots should be writable.
776 if (flags & BDRV_O_TEMPORARY) {
777 open_flags |= BDRV_O_RDWR;
780 return open_flags;
783 static void update_flags_from_options(int *flags, QemuOpts *opts)
785 *flags &= ~BDRV_O_CACHE_MASK;
787 assert(qemu_opt_find(opts, BDRV_OPT_CACHE_WB));
788 if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, false)) {
789 *flags |= BDRV_O_CACHE_WB;
792 assert(qemu_opt_find(opts, BDRV_OPT_CACHE_NO_FLUSH));
793 if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_NO_FLUSH, false)) {
794 *flags |= BDRV_O_NO_FLUSH;
797 assert(qemu_opt_find(opts, BDRV_OPT_CACHE_DIRECT));
798 if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_DIRECT, false)) {
799 *flags |= BDRV_O_NOCACHE;
803 static void update_options_from_flags(QDict *options, int flags)
805 if (!qdict_haskey(options, BDRV_OPT_CACHE_WB)) {
806 qdict_put(options, BDRV_OPT_CACHE_WB,
807 qbool_from_bool(flags & BDRV_O_CACHE_WB));
809 if (!qdict_haskey(options, BDRV_OPT_CACHE_DIRECT)) {
810 qdict_put(options, BDRV_OPT_CACHE_DIRECT,
811 qbool_from_bool(flags & BDRV_O_NOCACHE));
813 if (!qdict_haskey(options, BDRV_OPT_CACHE_NO_FLUSH)) {
814 qdict_put(options, BDRV_OPT_CACHE_NO_FLUSH,
815 qbool_from_bool(flags & BDRV_O_NO_FLUSH));
819 static void bdrv_assign_node_name(BlockDriverState *bs,
820 const char *node_name,
821 Error **errp)
823 char *gen_node_name = NULL;
825 if (!node_name) {
826 node_name = gen_node_name = id_generate(ID_BLOCK);
827 } else if (!id_wellformed(node_name)) {
829 * Check for empty string or invalid characters, but not if it is
830 * generated (generated names use characters not available to the user)
832 error_setg(errp, "Invalid node name");
833 return;
836 /* takes care of avoiding namespaces collisions */
837 if (blk_by_name(node_name)) {
838 error_setg(errp, "node-name=%s is conflicting with a device id",
839 node_name);
840 goto out;
843 /* takes care of avoiding duplicates node names */
844 if (bdrv_find_node(node_name)) {
845 error_setg(errp, "Duplicate node name");
846 goto out;
849 /* copy node name into the bs and insert it into the graph list */
850 pstrcpy(bs->node_name, sizeof(bs->node_name), node_name);
851 QTAILQ_INSERT_TAIL(&graph_bdrv_states, bs, node_list);
852 out:
853 g_free(gen_node_name);
856 static QemuOptsList bdrv_runtime_opts = {
857 .name = "bdrv_common",
858 .head = QTAILQ_HEAD_INITIALIZER(bdrv_runtime_opts.head),
859 .desc = {
861 .name = "node-name",
862 .type = QEMU_OPT_STRING,
863 .help = "Node name of the block device node",
866 .name = "driver",
867 .type = QEMU_OPT_STRING,
868 .help = "Block driver to use for the node",
871 .name = BDRV_OPT_CACHE_WB,
872 .type = QEMU_OPT_BOOL,
873 .help = "Enable writeback mode",
876 .name = BDRV_OPT_CACHE_DIRECT,
877 .type = QEMU_OPT_BOOL,
878 .help = "Bypass software writeback cache on the host",
881 .name = BDRV_OPT_CACHE_NO_FLUSH,
882 .type = QEMU_OPT_BOOL,
883 .help = "Ignore flush requests",
885 { /* end of list */ }
890 * Common part for opening disk images and files
892 * Removes all processed options from *options.
894 static int bdrv_open_common(BlockDriverState *bs, BdrvChild *file,
895 QDict *options, Error **errp)
897 int ret, open_flags;
898 const char *filename;
899 const char *driver_name = NULL;
900 const char *node_name = NULL;
901 QemuOpts *opts;
902 BlockDriver *drv;
903 Error *local_err = NULL;
905 assert(bs->file == NULL);
906 assert(options != NULL && bs->options != options);
908 opts = qemu_opts_create(&bdrv_runtime_opts, NULL, 0, &error_abort);
909 qemu_opts_absorb_qdict(opts, options, &local_err);
910 if (local_err) {
911 error_propagate(errp, local_err);
912 ret = -EINVAL;
913 goto fail_opts;
916 driver_name = qemu_opt_get(opts, "driver");
917 drv = bdrv_find_format(driver_name);
918 assert(drv != NULL);
920 if (file != NULL) {
921 filename = file->bs->filename;
922 } else {
923 filename = qdict_get_try_str(options, "filename");
926 if (drv->bdrv_needs_filename && !filename) {
927 error_setg(errp, "The '%s' block driver requires a file name",
928 drv->format_name);
929 ret = -EINVAL;
930 goto fail_opts;
933 trace_bdrv_open_common(bs, filename ?: "", bs->open_flags,
934 drv->format_name);
936 node_name = qemu_opt_get(opts, "node-name");
937 bdrv_assign_node_name(bs, node_name, &local_err);
938 if (local_err) {
939 error_propagate(errp, local_err);
940 ret = -EINVAL;
941 goto fail_opts;
944 bs->request_alignment = 512;
945 bs->zero_beyond_eof = true;
946 bs->read_only = !(bs->open_flags & BDRV_O_RDWR);
948 if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv, bs->read_only)) {
949 error_setg(errp,
950 !bs->read_only && bdrv_is_whitelisted(drv, true)
951 ? "Driver '%s' can only be used for read-only devices"
952 : "Driver '%s' is not whitelisted",
953 drv->format_name);
954 ret = -ENOTSUP;
955 goto fail_opts;
958 assert(bs->copy_on_read == 0); /* bdrv_new() and bdrv_close() make it so */
959 if (bs->open_flags & BDRV_O_COPY_ON_READ) {
960 if (!bs->read_only) {
961 bdrv_enable_copy_on_read(bs);
962 } else {
963 error_setg(errp, "Can't use copy-on-read on read-only device");
964 ret = -EINVAL;
965 goto fail_opts;
969 if (filename != NULL) {
970 pstrcpy(bs->filename, sizeof(bs->filename), filename);
971 } else {
972 bs->filename[0] = '\0';
974 pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), bs->filename);
976 bs->drv = drv;
977 bs->opaque = g_malloc0(drv->instance_size);
979 /* Apply cache mode options */
980 update_flags_from_options(&bs->open_flags, opts);
981 bdrv_set_enable_write_cache(bs, bs->open_flags & BDRV_O_CACHE_WB);
983 /* Open the image, either directly or using a protocol */
984 open_flags = bdrv_open_flags(bs, bs->open_flags);
985 if (drv->bdrv_file_open) {
986 assert(file == NULL);
987 assert(!drv->bdrv_needs_filename || filename != NULL);
988 ret = drv->bdrv_file_open(bs, options, open_flags, &local_err);
989 } else {
990 if (file == NULL) {
991 error_setg(errp, "Can't use '%s' as a block driver for the "
992 "protocol level", drv->format_name);
993 ret = -EINVAL;
994 goto free_and_fail;
996 bs->file = file;
997 ret = drv->bdrv_open(bs, options, open_flags, &local_err);
1000 if (ret < 0) {
1001 if (local_err) {
1002 error_propagate(errp, local_err);
1003 } else if (bs->filename[0]) {
1004 error_setg_errno(errp, -ret, "Could not open '%s'", bs->filename);
1005 } else {
1006 error_setg_errno(errp, -ret, "Could not open image");
1008 goto free_and_fail;
1011 if (bs->encrypted) {
1012 error_report("Encrypted images are deprecated");
1013 error_printf("Support for them will be removed in a future release.\n"
1014 "You can use 'qemu-img convert' to convert your image"
1015 " to an unencrypted one.\n");
1018 ret = refresh_total_sectors(bs, bs->total_sectors);
1019 if (ret < 0) {
1020 error_setg_errno(errp, -ret, "Could not refresh total sector count");
1021 goto free_and_fail;
1024 bdrv_refresh_limits(bs, &local_err);
1025 if (local_err) {
1026 error_propagate(errp, local_err);
1027 ret = -EINVAL;
1028 goto free_and_fail;
1031 assert(bdrv_opt_mem_align(bs) != 0);
1032 assert(bdrv_min_mem_align(bs) != 0);
1033 assert((bs->request_alignment != 0) || bdrv_is_sg(bs));
1035 qemu_opts_del(opts);
1036 return 0;
1038 free_and_fail:
1039 bs->file = NULL;
1040 g_free(bs->opaque);
1041 bs->opaque = NULL;
1042 bs->drv = NULL;
1043 fail_opts:
1044 qemu_opts_del(opts);
1045 return ret;
1048 static QDict *parse_json_filename(const char *filename, Error **errp)
1050 QObject *options_obj;
1051 QDict *options;
1052 int ret;
1054 ret = strstart(filename, "json:", &filename);
1055 assert(ret);
1057 options_obj = qobject_from_json(filename);
1058 if (!options_obj) {
1059 error_setg(errp, "Could not parse the JSON options");
1060 return NULL;
1063 if (qobject_type(options_obj) != QTYPE_QDICT) {
1064 qobject_decref(options_obj);
1065 error_setg(errp, "Invalid JSON object given");
1066 return NULL;
1069 options = qobject_to_qdict(options_obj);
1070 qdict_flatten(options);
1072 return options;
1075 static void parse_json_protocol(QDict *options, const char **pfilename,
1076 Error **errp)
1078 QDict *json_options;
1079 Error *local_err = NULL;
1081 /* Parse json: pseudo-protocol */
1082 if (!*pfilename || !g_str_has_prefix(*pfilename, "json:")) {
1083 return;
1086 json_options = parse_json_filename(*pfilename, &local_err);
1087 if (local_err) {
1088 error_propagate(errp, local_err);
1089 return;
1092 /* Options given in the filename have lower priority than options
1093 * specified directly */
1094 qdict_join(options, json_options, false);
1095 QDECREF(json_options);
1096 *pfilename = NULL;
1100 * Fills in default options for opening images and converts the legacy
1101 * filename/flags pair to option QDict entries.
1102 * The BDRV_O_PROTOCOL flag in *flags will be set or cleared accordingly if a
1103 * block driver has been specified explicitly.
1105 static int bdrv_fill_options(QDict **options, const char *filename,
1106 int *flags, Error **errp)
1108 const char *drvname;
1109 bool protocol = *flags & BDRV_O_PROTOCOL;
1110 bool parse_filename = false;
1111 BlockDriver *drv = NULL;
1112 Error *local_err = NULL;
1114 drvname = qdict_get_try_str(*options, "driver");
1115 if (drvname) {
1116 drv = bdrv_find_format(drvname);
1117 if (!drv) {
1118 error_setg(errp, "Unknown driver '%s'", drvname);
1119 return -ENOENT;
1121 /* If the user has explicitly specified the driver, this choice should
1122 * override the BDRV_O_PROTOCOL flag */
1123 protocol = drv->bdrv_file_open;
1126 if (protocol) {
1127 *flags |= BDRV_O_PROTOCOL;
1128 } else {
1129 *flags &= ~BDRV_O_PROTOCOL;
1132 /* Translate cache options from flags into options */
1133 update_options_from_flags(*options, *flags);
1135 /* Fetch the file name from the options QDict if necessary */
1136 if (protocol && filename) {
1137 if (!qdict_haskey(*options, "filename")) {
1138 qdict_put(*options, "filename", qstring_from_str(filename));
1139 parse_filename = true;
1140 } else {
1141 error_setg(errp, "Can't specify 'file' and 'filename' options at "
1142 "the same time");
1143 return -EINVAL;
1147 /* Find the right block driver */
1148 filename = qdict_get_try_str(*options, "filename");
1150 if (!drvname && protocol) {
1151 if (filename) {
1152 drv = bdrv_find_protocol(filename, parse_filename, errp);
1153 if (!drv) {
1154 return -EINVAL;
1157 drvname = drv->format_name;
1158 qdict_put(*options, "driver", qstring_from_str(drvname));
1159 } else {
1160 error_setg(errp, "Must specify either driver or file");
1161 return -EINVAL;
1165 assert(drv || !protocol);
1167 /* Driver-specific filename parsing */
1168 if (drv && drv->bdrv_parse_filename && parse_filename) {
1169 drv->bdrv_parse_filename(filename, *options, &local_err);
1170 if (local_err) {
1171 error_propagate(errp, local_err);
1172 return -EINVAL;
1175 if (!drv->bdrv_needs_filename) {
1176 qdict_del(*options, "filename");
1180 return 0;
1183 static BdrvChild *bdrv_attach_child(BlockDriverState *parent_bs,
1184 BlockDriverState *child_bs,
1185 const char *child_name,
1186 const BdrvChildRole *child_role)
1188 BdrvChild *child = g_new(BdrvChild, 1);
1189 *child = (BdrvChild) {
1190 .bs = child_bs,
1191 .name = g_strdup(child_name),
1192 .role = child_role,
1195 QLIST_INSERT_HEAD(&parent_bs->children, child, next);
1196 QLIST_INSERT_HEAD(&child_bs->parents, child, next_parent);
1198 return child;
1201 static void bdrv_detach_child(BdrvChild *child)
1203 QLIST_REMOVE(child, next);
1204 QLIST_REMOVE(child, next_parent);
1205 g_free(child->name);
1206 g_free(child);
1209 void bdrv_unref_child(BlockDriverState *parent, BdrvChild *child)
1211 BlockDriverState *child_bs;
1213 if (child == NULL) {
1214 return;
1217 if (child->bs->inherits_from == parent) {
1218 child->bs->inherits_from = NULL;
1221 child_bs = child->bs;
1222 bdrv_detach_child(child);
1223 bdrv_unref(child_bs);
1227 * Sets the backing file link of a BDS. A new reference is created; callers
1228 * which don't need their own reference any more must call bdrv_unref().
1230 void bdrv_set_backing_hd(BlockDriverState *bs, BlockDriverState *backing_hd)
1232 if (backing_hd) {
1233 bdrv_ref(backing_hd);
1236 if (bs->backing) {
1237 assert(bs->backing_blocker);
1238 bdrv_op_unblock_all(bs->backing->bs, bs->backing_blocker);
1239 bdrv_unref_child(bs, bs->backing);
1240 } else if (backing_hd) {
1241 error_setg(&bs->backing_blocker,
1242 "node is used as backing hd of '%s'",
1243 bdrv_get_device_or_node_name(bs));
1246 if (!backing_hd) {
1247 error_free(bs->backing_blocker);
1248 bs->backing_blocker = NULL;
1249 bs->backing = NULL;
1250 goto out;
1252 bs->backing = bdrv_attach_child(bs, backing_hd, "backing", &child_backing);
1253 bs->open_flags &= ~BDRV_O_NO_BACKING;
1254 pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_hd->filename);
1255 pstrcpy(bs->backing_format, sizeof(bs->backing_format),
1256 backing_hd->drv ? backing_hd->drv->format_name : "");
1258 bdrv_op_block_all(backing_hd, bs->backing_blocker);
1259 /* Otherwise we won't be able to commit due to check in bdrv_commit */
1260 bdrv_op_unblock(backing_hd, BLOCK_OP_TYPE_COMMIT_TARGET,
1261 bs->backing_blocker);
1262 out:
1263 bdrv_refresh_limits(bs, NULL);
1267 * Opens the backing file for a BlockDriverState if not yet open
1269 * bdref_key specifies the key for the image's BlockdevRef in the options QDict.
1270 * That QDict has to be flattened; therefore, if the BlockdevRef is a QDict
1271 * itself, all options starting with "${bdref_key}." are considered part of the
1272 * BlockdevRef.
1274 * TODO Can this be unified with bdrv_open_image()?
1276 int bdrv_open_backing_file(BlockDriverState *bs, QDict *parent_options,
1277 const char *bdref_key, Error **errp)
1279 char *backing_filename = g_malloc0(PATH_MAX);
1280 char *bdref_key_dot;
1281 const char *reference = NULL;
1282 int ret = 0;
1283 BlockDriverState *backing_hd;
1284 QDict *options;
1285 QDict *tmp_parent_options = NULL;
1286 Error *local_err = NULL;
1288 if (bs->backing != NULL) {
1289 goto free_exit;
1292 /* NULL means an empty set of options */
1293 if (parent_options == NULL) {
1294 tmp_parent_options = qdict_new();
1295 parent_options = tmp_parent_options;
1298 bs->open_flags &= ~BDRV_O_NO_BACKING;
1300 bdref_key_dot = g_strdup_printf("%s.", bdref_key);
1301 qdict_extract_subqdict(parent_options, &options, bdref_key_dot);
1302 g_free(bdref_key_dot);
1304 reference = qdict_get_try_str(parent_options, bdref_key);
1305 if (reference || qdict_haskey(options, "file.filename")) {
1306 backing_filename[0] = '\0';
1307 } else if (bs->backing_file[0] == '\0' && qdict_size(options) == 0) {
1308 QDECREF(options);
1309 goto free_exit;
1310 } else {
1311 bdrv_get_full_backing_filename(bs, backing_filename, PATH_MAX,
1312 &local_err);
1313 if (local_err) {
1314 ret = -EINVAL;
1315 error_propagate(errp, local_err);
1316 QDECREF(options);
1317 goto free_exit;
1321 if (!bs->drv || !bs->drv->supports_backing) {
1322 ret = -EINVAL;
1323 error_setg(errp, "Driver doesn't support backing files");
1324 QDECREF(options);
1325 goto free_exit;
1328 if (bs->backing_format[0] != '\0' && !qdict_haskey(options, "driver")) {
1329 qdict_put(options, "driver", qstring_from_str(bs->backing_format));
1332 backing_hd = NULL;
1333 ret = bdrv_open_inherit(&backing_hd,
1334 *backing_filename ? backing_filename : NULL,
1335 reference, options, 0, bs, &child_backing,
1336 errp);
1337 if (ret < 0) {
1338 bs->open_flags |= BDRV_O_NO_BACKING;
1339 error_prepend(errp, "Could not open backing file: ");
1340 goto free_exit;
1343 /* Hook up the backing file link; drop our reference, bs owns the
1344 * backing_hd reference now */
1345 bdrv_set_backing_hd(bs, backing_hd);
1346 bdrv_unref(backing_hd);
1348 qdict_del(parent_options, bdref_key);
1350 free_exit:
1351 g_free(backing_filename);
1352 QDECREF(tmp_parent_options);
1353 return ret;
1357 * Opens a disk image whose options are given as BlockdevRef in another block
1358 * device's options.
1360 * If allow_none is true, no image will be opened if filename is false and no
1361 * BlockdevRef is given. NULL will be returned, but errp remains unset.
1363 * bdrev_key specifies the key for the image's BlockdevRef in the options QDict.
1364 * That QDict has to be flattened; therefore, if the BlockdevRef is a QDict
1365 * itself, all options starting with "${bdref_key}." are considered part of the
1366 * BlockdevRef.
1368 * The BlockdevRef will be removed from the options QDict.
1370 BdrvChild *bdrv_open_child(const char *filename,
1371 QDict *options, const char *bdref_key,
1372 BlockDriverState* parent,
1373 const BdrvChildRole *child_role,
1374 bool allow_none, Error **errp)
1376 BdrvChild *c = NULL;
1377 BlockDriverState *bs;
1378 QDict *image_options;
1379 int ret;
1380 char *bdref_key_dot;
1381 const char *reference;
1383 assert(child_role != NULL);
1385 bdref_key_dot = g_strdup_printf("%s.", bdref_key);
1386 qdict_extract_subqdict(options, &image_options, bdref_key_dot);
1387 g_free(bdref_key_dot);
1389 reference = qdict_get_try_str(options, bdref_key);
1390 if (!filename && !reference && !qdict_size(image_options)) {
1391 if (!allow_none) {
1392 error_setg(errp, "A block device must be specified for \"%s\"",
1393 bdref_key);
1395 QDECREF(image_options);
1396 goto done;
1399 bs = NULL;
1400 ret = bdrv_open_inherit(&bs, filename, reference, image_options, 0,
1401 parent, child_role, errp);
1402 if (ret < 0) {
1403 goto done;
1406 c = bdrv_attach_child(parent, bs, bdref_key, child_role);
1408 done:
1409 qdict_del(options, bdref_key);
1410 return c;
1413 static int bdrv_append_temp_snapshot(BlockDriverState *bs, int flags,
1414 QDict *snapshot_options, Error **errp)
1416 /* TODO: extra byte is a hack to ensure MAX_PATH space on Windows. */
1417 char *tmp_filename = g_malloc0(PATH_MAX + 1);
1418 int64_t total_size;
1419 QemuOpts *opts = NULL;
1420 BlockDriverState *bs_snapshot;
1421 Error *local_err = NULL;
1422 int ret;
1424 /* if snapshot, we create a temporary backing file and open it
1425 instead of opening 'filename' directly */
1427 /* Get the required size from the image */
1428 total_size = bdrv_getlength(bs);
1429 if (total_size < 0) {
1430 ret = total_size;
1431 error_setg_errno(errp, -total_size, "Could not get image size");
1432 goto out;
1435 /* Create the temporary image */
1436 ret = get_tmp_filename(tmp_filename, PATH_MAX + 1);
1437 if (ret < 0) {
1438 error_setg_errno(errp, -ret, "Could not get temporary filename");
1439 goto out;
1442 opts = qemu_opts_create(bdrv_qcow2.create_opts, NULL, 0,
1443 &error_abort);
1444 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, total_size, &error_abort);
1445 ret = bdrv_create(&bdrv_qcow2, tmp_filename, opts, errp);
1446 qemu_opts_del(opts);
1447 if (ret < 0) {
1448 error_prepend(errp, "Could not create temporary overlay '%s': ",
1449 tmp_filename);
1450 goto out;
1453 /* Prepare options QDict for the temporary file */
1454 qdict_put(snapshot_options, "file.driver",
1455 qstring_from_str("file"));
1456 qdict_put(snapshot_options, "file.filename",
1457 qstring_from_str(tmp_filename));
1458 qdict_put(snapshot_options, "driver",
1459 qstring_from_str("qcow2"));
1461 bs_snapshot = bdrv_new();
1463 ret = bdrv_open(&bs_snapshot, NULL, NULL, snapshot_options,
1464 flags, &local_err);
1465 snapshot_options = NULL;
1466 if (ret < 0) {
1467 error_propagate(errp, local_err);
1468 goto out;
1471 bdrv_append(bs_snapshot, bs);
1473 out:
1474 QDECREF(snapshot_options);
1475 g_free(tmp_filename);
1476 return ret;
1480 * Opens a disk image (raw, qcow2, vmdk, ...)
1482 * options is a QDict of options to pass to the block drivers, or NULL for an
1483 * empty set of options. The reference to the QDict belongs to the block layer
1484 * after the call (even on failure), so if the caller intends to reuse the
1485 * dictionary, it needs to use QINCREF() before calling bdrv_open.
1487 * If *pbs is NULL, a new BDS will be created with a pointer to it stored there.
1488 * If it is not NULL, the referenced BDS will be reused.
1490 * The reference parameter may be used to specify an existing block device which
1491 * should be opened. If specified, neither options nor a filename may be given,
1492 * nor can an existing BDS be reused (that is, *pbs has to be NULL).
1494 static int bdrv_open_inherit(BlockDriverState **pbs, const char *filename,
1495 const char *reference, QDict *options, int flags,
1496 BlockDriverState *parent,
1497 const BdrvChildRole *child_role, Error **errp)
1499 int ret;
1500 BdrvChild *file = NULL;
1501 BlockDriverState *bs;
1502 BlockDriver *drv = NULL;
1503 const char *drvname;
1504 const char *backing;
1505 Error *local_err = NULL;
1506 QDict *snapshot_options = NULL;
1507 int snapshot_flags = 0;
1509 assert(pbs);
1510 assert(!child_role || !flags);
1511 assert(!child_role == !parent);
1513 if (reference) {
1514 bool options_non_empty = options ? qdict_size(options) : false;
1515 QDECREF(options);
1517 if (*pbs) {
1518 error_setg(errp, "Cannot reuse an existing BDS when referencing "
1519 "another block device");
1520 return -EINVAL;
1523 if (filename || options_non_empty) {
1524 error_setg(errp, "Cannot reference an existing block device with "
1525 "additional options or a new filename");
1526 return -EINVAL;
1529 bs = bdrv_lookup_bs(reference, reference, errp);
1530 if (!bs) {
1531 return -ENODEV;
1533 bdrv_ref(bs);
1534 *pbs = bs;
1535 return 0;
1538 if (*pbs) {
1539 bs = *pbs;
1540 } else {
1541 bs = bdrv_new();
1544 /* NULL means an empty set of options */
1545 if (options == NULL) {
1546 options = qdict_new();
1549 /* json: syntax counts as explicit options, as if in the QDict */
1550 parse_json_protocol(options, &filename, &local_err);
1551 if (local_err) {
1552 ret = -EINVAL;
1553 goto fail;
1556 bs->explicit_options = qdict_clone_shallow(options);
1558 if (child_role) {
1559 bs->inherits_from = parent;
1560 child_role->inherit_options(&flags, options,
1561 parent->open_flags, parent->options);
1564 ret = bdrv_fill_options(&options, filename, &flags, &local_err);
1565 if (local_err) {
1566 goto fail;
1569 bs->open_flags = flags;
1570 bs->options = options;
1571 options = qdict_clone_shallow(options);
1573 /* Find the right image format driver */
1574 drvname = qdict_get_try_str(options, "driver");
1575 if (drvname) {
1576 drv = bdrv_find_format(drvname);
1577 if (!drv) {
1578 error_setg(errp, "Unknown driver: '%s'", drvname);
1579 ret = -EINVAL;
1580 goto fail;
1584 assert(drvname || !(flags & BDRV_O_PROTOCOL));
1586 backing = qdict_get_try_str(options, "backing");
1587 if (backing && *backing == '\0') {
1588 flags |= BDRV_O_NO_BACKING;
1589 qdict_del(options, "backing");
1592 /* Open image file without format layer */
1593 if ((flags & BDRV_O_PROTOCOL) == 0) {
1594 if (flags & BDRV_O_RDWR) {
1595 flags |= BDRV_O_ALLOW_RDWR;
1597 if (flags & BDRV_O_SNAPSHOT) {
1598 snapshot_options = qdict_new();
1599 bdrv_temp_snapshot_options(&snapshot_flags, snapshot_options,
1600 flags, options);
1601 bdrv_backing_options(&flags, options, flags, options);
1604 bs->open_flags = flags;
1606 file = bdrv_open_child(filename, options, "file", bs,
1607 &child_file, true, &local_err);
1608 if (local_err) {
1609 ret = -EINVAL;
1610 goto fail;
1614 /* Image format probing */
1615 bs->probed = !drv;
1616 if (!drv && file) {
1617 ret = find_image_format(file->bs, filename, &drv, &local_err);
1618 if (ret < 0) {
1619 goto fail;
1622 * This option update would logically belong in bdrv_fill_options(),
1623 * but we first need to open bs->file for the probing to work, while
1624 * opening bs->file already requires the (mostly) final set of options
1625 * so that cache mode etc. can be inherited.
1627 * Adding the driver later is somewhat ugly, but it's not an option
1628 * that would ever be inherited, so it's correct. We just need to make
1629 * sure to update both bs->options (which has the full effective
1630 * options for bs) and options (which has file.* already removed).
1632 qdict_put(bs->options, "driver", qstring_from_str(drv->format_name));
1633 qdict_put(options, "driver", qstring_from_str(drv->format_name));
1634 } else if (!drv) {
1635 error_setg(errp, "Must specify either driver or file");
1636 ret = -EINVAL;
1637 goto fail;
1640 /* BDRV_O_PROTOCOL must be set iff a protocol BDS is about to be created */
1641 assert(!!(flags & BDRV_O_PROTOCOL) == !!drv->bdrv_file_open);
1642 /* file must be NULL if a protocol BDS is about to be created
1643 * (the inverse results in an error message from bdrv_open_common()) */
1644 assert(!(flags & BDRV_O_PROTOCOL) || !file);
1646 /* Open the image */
1647 ret = bdrv_open_common(bs, file, options, &local_err);
1648 if (ret < 0) {
1649 goto fail;
1652 if (file && (bs->file != file)) {
1653 bdrv_unref_child(bs, file);
1654 file = NULL;
1657 /* If there is a backing file, use it */
1658 if ((flags & BDRV_O_NO_BACKING) == 0) {
1659 ret = bdrv_open_backing_file(bs, options, "backing", &local_err);
1660 if (ret < 0) {
1661 goto close_and_fail;
1665 bdrv_refresh_filename(bs);
1667 /* Check if any unknown options were used */
1668 if (options && (qdict_size(options) != 0)) {
1669 const QDictEntry *entry = qdict_first(options);
1670 if (flags & BDRV_O_PROTOCOL) {
1671 error_setg(errp, "Block protocol '%s' doesn't support the option "
1672 "'%s'", drv->format_name, entry->key);
1673 } else {
1674 error_setg(errp,
1675 "Block format '%s' does not support the option '%s'",
1676 drv->format_name, entry->key);
1679 ret = -EINVAL;
1680 goto close_and_fail;
1683 if (!bdrv_key_required(bs)) {
1684 if (bs->blk) {
1685 blk_dev_change_media_cb(bs->blk, true);
1687 } else if (!runstate_check(RUN_STATE_PRELAUNCH)
1688 && !runstate_check(RUN_STATE_INMIGRATE)
1689 && !runstate_check(RUN_STATE_PAUSED)) { /* HACK */
1690 error_setg(errp,
1691 "Guest must be stopped for opening of encrypted image");
1692 ret = -EBUSY;
1693 goto close_and_fail;
1696 QDECREF(options);
1697 *pbs = bs;
1699 /* For snapshot=on, create a temporary qcow2 overlay. bs points to the
1700 * temporary snapshot afterwards. */
1701 if (snapshot_flags) {
1702 ret = bdrv_append_temp_snapshot(bs, snapshot_flags, snapshot_options,
1703 &local_err);
1704 snapshot_options = NULL;
1705 if (local_err) {
1706 goto close_and_fail;
1710 return 0;
1712 fail:
1713 if (file != NULL) {
1714 bdrv_unref_child(bs, file);
1716 QDECREF(snapshot_options);
1717 QDECREF(bs->explicit_options);
1718 QDECREF(bs->options);
1719 QDECREF(options);
1720 bs->options = NULL;
1721 if (!*pbs) {
1722 /* If *pbs is NULL, a new BDS has been created in this function and
1723 needs to be freed now. Otherwise, it does not need to be closed,
1724 since it has not really been opened yet. */
1725 bdrv_unref(bs);
1727 if (local_err) {
1728 error_propagate(errp, local_err);
1730 return ret;
1732 close_and_fail:
1733 /* See fail path, but now the BDS has to be always closed */
1734 if (*pbs) {
1735 bdrv_close(bs);
1736 } else {
1737 bdrv_unref(bs);
1739 QDECREF(snapshot_options);
1740 QDECREF(options);
1741 if (local_err) {
1742 error_propagate(errp, local_err);
1744 return ret;
1747 int bdrv_open(BlockDriverState **pbs, const char *filename,
1748 const char *reference, QDict *options, int flags, Error **errp)
1750 return bdrv_open_inherit(pbs, filename, reference, options, flags, NULL,
1751 NULL, errp);
1754 typedef struct BlockReopenQueueEntry {
1755 bool prepared;
1756 BDRVReopenState state;
1757 QSIMPLEQ_ENTRY(BlockReopenQueueEntry) entry;
1758 } BlockReopenQueueEntry;
1761 * Adds a BlockDriverState to a simple queue for an atomic, transactional
1762 * reopen of multiple devices.
1764 * bs_queue can either be an existing BlockReopenQueue that has had QSIMPLE_INIT
1765 * already performed, or alternatively may be NULL a new BlockReopenQueue will
1766 * be created and initialized. This newly created BlockReopenQueue should be
1767 * passed back in for subsequent calls that are intended to be of the same
1768 * atomic 'set'.
1770 * bs is the BlockDriverState to add to the reopen queue.
1772 * options contains the changed options for the associated bs
1773 * (the BlockReopenQueue takes ownership)
1775 * flags contains the open flags for the associated bs
1777 * returns a pointer to bs_queue, which is either the newly allocated
1778 * bs_queue, or the existing bs_queue being used.
1781 static BlockReopenQueue *bdrv_reopen_queue_child(BlockReopenQueue *bs_queue,
1782 BlockDriverState *bs,
1783 QDict *options,
1784 int flags,
1785 const BdrvChildRole *role,
1786 QDict *parent_options,
1787 int parent_flags)
1789 assert(bs != NULL);
1791 BlockReopenQueueEntry *bs_entry;
1792 BdrvChild *child;
1793 QDict *old_options, *explicit_options;
1795 if (bs_queue == NULL) {
1796 bs_queue = g_new0(BlockReopenQueue, 1);
1797 QSIMPLEQ_INIT(bs_queue);
1800 if (!options) {
1801 options = qdict_new();
1805 * Precedence of options:
1806 * 1. Explicitly passed in options (highest)
1807 * 2. Set in flags (only for top level)
1808 * 3. Retained from explicitly set options of bs
1809 * 4. Inherited from parent node
1810 * 5. Retained from effective options of bs
1813 if (!parent_options) {
1815 * Any setting represented by flags is always updated. If the
1816 * corresponding QDict option is set, it takes precedence. Otherwise
1817 * the flag is translated into a QDict option. The old setting of bs is
1818 * not considered.
1820 update_options_from_flags(options, flags);
1823 /* Old explicitly set values (don't overwrite by inherited value) */
1824 old_options = qdict_clone_shallow(bs->explicit_options);
1825 bdrv_join_options(bs, options, old_options);
1826 QDECREF(old_options);
1828 explicit_options = qdict_clone_shallow(options);
1830 /* Inherit from parent node */
1831 if (parent_options) {
1832 assert(!flags);
1833 role->inherit_options(&flags, options, parent_flags, parent_options);
1836 /* Old values are used for options that aren't set yet */
1837 old_options = qdict_clone_shallow(bs->options);
1838 bdrv_join_options(bs, options, old_options);
1839 QDECREF(old_options);
1841 /* bdrv_open() masks this flag out */
1842 flags &= ~BDRV_O_PROTOCOL;
1844 QLIST_FOREACH(child, &bs->children, next) {
1845 QDict *new_child_options;
1846 char *child_key_dot;
1848 /* reopen can only change the options of block devices that were
1849 * implicitly created and inherited options. For other (referenced)
1850 * block devices, a syntax like "backing.foo" results in an error. */
1851 if (child->bs->inherits_from != bs) {
1852 continue;
1855 child_key_dot = g_strdup_printf("%s.", child->name);
1856 qdict_extract_subqdict(options, &new_child_options, child_key_dot);
1857 g_free(child_key_dot);
1859 bdrv_reopen_queue_child(bs_queue, child->bs, new_child_options, 0,
1860 child->role, options, flags);
1863 bs_entry = g_new0(BlockReopenQueueEntry, 1);
1864 QSIMPLEQ_INSERT_TAIL(bs_queue, bs_entry, entry);
1866 bs_entry->state.bs = bs;
1867 bs_entry->state.options = options;
1868 bs_entry->state.explicit_options = explicit_options;
1869 bs_entry->state.flags = flags;
1871 return bs_queue;
1874 BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue,
1875 BlockDriverState *bs,
1876 QDict *options, int flags)
1878 return bdrv_reopen_queue_child(bs_queue, bs, options, flags,
1879 NULL, NULL, 0);
1883 * Reopen multiple BlockDriverStates atomically & transactionally.
1885 * The queue passed in (bs_queue) must have been built up previous
1886 * via bdrv_reopen_queue().
1888 * Reopens all BDS specified in the queue, with the appropriate
1889 * flags. All devices are prepared for reopen, and failure of any
1890 * device will cause all device changes to be abandonded, and intermediate
1891 * data cleaned up.
1893 * If all devices prepare successfully, then the changes are committed
1894 * to all devices.
1897 int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp)
1899 int ret = -1;
1900 BlockReopenQueueEntry *bs_entry, *next;
1901 Error *local_err = NULL;
1903 assert(bs_queue != NULL);
1905 bdrv_drain_all();
1907 QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) {
1908 if (bdrv_reopen_prepare(&bs_entry->state, bs_queue, &local_err)) {
1909 error_propagate(errp, local_err);
1910 goto cleanup;
1912 bs_entry->prepared = true;
1915 /* If we reach this point, we have success and just need to apply the
1916 * changes
1918 QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) {
1919 bdrv_reopen_commit(&bs_entry->state);
1922 ret = 0;
1924 cleanup:
1925 QSIMPLEQ_FOREACH_SAFE(bs_entry, bs_queue, entry, next) {
1926 if (ret && bs_entry->prepared) {
1927 bdrv_reopen_abort(&bs_entry->state);
1928 } else if (ret) {
1929 QDECREF(bs_entry->state.explicit_options);
1931 QDECREF(bs_entry->state.options);
1932 g_free(bs_entry);
1934 g_free(bs_queue);
1935 return ret;
1939 /* Reopen a single BlockDriverState with the specified flags. */
1940 int bdrv_reopen(BlockDriverState *bs, int bdrv_flags, Error **errp)
1942 int ret = -1;
1943 Error *local_err = NULL;
1944 BlockReopenQueue *queue = bdrv_reopen_queue(NULL, bs, NULL, bdrv_flags);
1946 ret = bdrv_reopen_multiple(queue, &local_err);
1947 if (local_err != NULL) {
1948 error_propagate(errp, local_err);
1950 return ret;
1955 * Prepares a BlockDriverState for reopen. All changes are staged in the
1956 * 'opaque' field of the BDRVReopenState, which is used and allocated by
1957 * the block driver layer .bdrv_reopen_prepare()
1959 * bs is the BlockDriverState to reopen
1960 * flags are the new open flags
1961 * queue is the reopen queue
1963 * Returns 0 on success, non-zero on error. On error errp will be set
1964 * as well.
1966 * On failure, bdrv_reopen_abort() will be called to clean up any data.
1967 * It is the responsibility of the caller to then call the abort() or
1968 * commit() for any other BDS that have been left in a prepare() state
1971 int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue,
1972 Error **errp)
1974 int ret = -1;
1975 Error *local_err = NULL;
1976 BlockDriver *drv;
1977 QemuOpts *opts;
1978 const char *value;
1980 assert(reopen_state != NULL);
1981 assert(reopen_state->bs->drv != NULL);
1982 drv = reopen_state->bs->drv;
1984 /* Process generic block layer options */
1985 opts = qemu_opts_create(&bdrv_runtime_opts, NULL, 0, &error_abort);
1986 qemu_opts_absorb_qdict(opts, reopen_state->options, &local_err);
1987 if (local_err) {
1988 error_propagate(errp, local_err);
1989 ret = -EINVAL;
1990 goto error;
1993 update_flags_from_options(&reopen_state->flags, opts);
1995 /* If a guest device is attached, it owns WCE */
1996 if (reopen_state->bs->blk && blk_get_attached_dev(reopen_state->bs->blk)) {
1997 bool old_wce = bdrv_enable_write_cache(reopen_state->bs);
1998 bool new_wce = (reopen_state->flags & BDRV_O_CACHE_WB);
1999 if (old_wce != new_wce) {
2000 error_setg(errp, "Cannot change cache.writeback: Device attached");
2001 ret = -EINVAL;
2002 goto error;
2006 /* node-name and driver must be unchanged. Put them back into the QDict, so
2007 * that they are checked at the end of this function. */
2008 value = qemu_opt_get(opts, "node-name");
2009 if (value) {
2010 qdict_put(reopen_state->options, "node-name", qstring_from_str(value));
2013 value = qemu_opt_get(opts, "driver");
2014 if (value) {
2015 qdict_put(reopen_state->options, "driver", qstring_from_str(value));
2018 /* if we are to stay read-only, do not allow permission change
2019 * to r/w */
2020 if (!(reopen_state->bs->open_flags & BDRV_O_ALLOW_RDWR) &&
2021 reopen_state->flags & BDRV_O_RDWR) {
2022 error_setg(errp, "Node '%s' is read only",
2023 bdrv_get_device_or_node_name(reopen_state->bs));
2024 goto error;
2028 ret = bdrv_flush(reopen_state->bs);
2029 if (ret) {
2030 error_setg_errno(errp, -ret, "Error flushing drive");
2031 goto error;
2034 if (drv->bdrv_reopen_prepare) {
2035 ret = drv->bdrv_reopen_prepare(reopen_state, queue, &local_err);
2036 if (ret) {
2037 if (local_err != NULL) {
2038 error_propagate(errp, local_err);
2039 } else {
2040 error_setg(errp, "failed while preparing to reopen image '%s'",
2041 reopen_state->bs->filename);
2043 goto error;
2045 } else {
2046 /* It is currently mandatory to have a bdrv_reopen_prepare()
2047 * handler for each supported drv. */
2048 error_setg(errp, "Block format '%s' used by node '%s' "
2049 "does not support reopening files", drv->format_name,
2050 bdrv_get_device_or_node_name(reopen_state->bs));
2051 ret = -1;
2052 goto error;
2055 /* Options that are not handled are only okay if they are unchanged
2056 * compared to the old state. It is expected that some options are only
2057 * used for the initial open, but not reopen (e.g. filename) */
2058 if (qdict_size(reopen_state->options)) {
2059 const QDictEntry *entry = qdict_first(reopen_state->options);
2061 do {
2062 QString *new_obj = qobject_to_qstring(entry->value);
2063 const char *new = qstring_get_str(new_obj);
2064 const char *old = qdict_get_try_str(reopen_state->bs->options,
2065 entry->key);
2067 if (!old || strcmp(new, old)) {
2068 error_setg(errp, "Cannot change the option '%s'", entry->key);
2069 ret = -EINVAL;
2070 goto error;
2072 } while ((entry = qdict_next(reopen_state->options, entry)));
2075 ret = 0;
2077 error:
2078 qemu_opts_del(opts);
2079 return ret;
2083 * Takes the staged changes for the reopen from bdrv_reopen_prepare(), and
2084 * makes them final by swapping the staging BlockDriverState contents into
2085 * the active BlockDriverState contents.
2087 void bdrv_reopen_commit(BDRVReopenState *reopen_state)
2089 BlockDriver *drv;
2091 assert(reopen_state != NULL);
2092 drv = reopen_state->bs->drv;
2093 assert(drv != NULL);
2095 /* If there are any driver level actions to take */
2096 if (drv->bdrv_reopen_commit) {
2097 drv->bdrv_reopen_commit(reopen_state);
2100 /* set BDS specific flags now */
2101 QDECREF(reopen_state->bs->explicit_options);
2103 reopen_state->bs->explicit_options = reopen_state->explicit_options;
2104 reopen_state->bs->open_flags = reopen_state->flags;
2105 reopen_state->bs->enable_write_cache = !!(reopen_state->flags &
2106 BDRV_O_CACHE_WB);
2107 reopen_state->bs->read_only = !(reopen_state->flags & BDRV_O_RDWR);
2109 bdrv_refresh_limits(reopen_state->bs, NULL);
2113 * Abort the reopen, and delete and free the staged changes in
2114 * reopen_state
2116 void bdrv_reopen_abort(BDRVReopenState *reopen_state)
2118 BlockDriver *drv;
2120 assert(reopen_state != NULL);
2121 drv = reopen_state->bs->drv;
2122 assert(drv != NULL);
2124 if (drv->bdrv_reopen_abort) {
2125 drv->bdrv_reopen_abort(reopen_state);
2128 QDECREF(reopen_state->explicit_options);
2132 static void bdrv_close(BlockDriverState *bs)
2134 BdrvAioNotifier *ban, *ban_next;
2136 assert(!bs->job);
2138 /* Disable I/O limits and drain all pending throttled requests */
2139 if (bs->throttle_state) {
2140 bdrv_io_limits_disable(bs);
2143 bdrv_drained_begin(bs); /* complete I/O */
2144 bdrv_flush(bs);
2145 bdrv_drain(bs); /* in case flush left pending I/O */
2147 bdrv_release_named_dirty_bitmaps(bs);
2148 assert(QLIST_EMPTY(&bs->dirty_bitmaps));
2150 if (bs->blk) {
2151 blk_dev_change_media_cb(bs->blk, false);
2154 if (bs->drv) {
2155 BdrvChild *child, *next;
2157 bs->drv->bdrv_close(bs);
2158 bs->drv = NULL;
2160 bdrv_set_backing_hd(bs, NULL);
2162 if (bs->file != NULL) {
2163 bdrv_unref_child(bs, bs->file);
2164 bs->file = NULL;
2167 QLIST_FOREACH_SAFE(child, &bs->children, next, next) {
2168 /* TODO Remove bdrv_unref() from drivers' close function and use
2169 * bdrv_unref_child() here */
2170 if (child->bs->inherits_from == bs) {
2171 child->bs->inherits_from = NULL;
2173 bdrv_detach_child(child);
2176 g_free(bs->opaque);
2177 bs->opaque = NULL;
2178 bs->copy_on_read = 0;
2179 bs->backing_file[0] = '\0';
2180 bs->backing_format[0] = '\0';
2181 bs->total_sectors = 0;
2182 bs->encrypted = 0;
2183 bs->valid_key = 0;
2184 bs->sg = 0;
2185 bs->zero_beyond_eof = false;
2186 QDECREF(bs->options);
2187 QDECREF(bs->explicit_options);
2188 bs->options = NULL;
2189 QDECREF(bs->full_open_options);
2190 bs->full_open_options = NULL;
2193 QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) {
2194 g_free(ban);
2196 QLIST_INIT(&bs->aio_notifiers);
2197 bdrv_drained_end(bs);
2200 void bdrv_close_all(void)
2202 BlockDriverState *bs;
2203 AioContext *aio_context;
2205 /* Drop references from requests still in flight, such as canceled block
2206 * jobs whose AIO context has not been polled yet */
2207 bdrv_drain_all();
2209 blk_remove_all_bs();
2210 blockdev_close_all_bdrv_states();
2212 /* Cancel all block jobs */
2213 while (!QTAILQ_EMPTY(&all_bdrv_states)) {
2214 QTAILQ_FOREACH(bs, &all_bdrv_states, bs_list) {
2215 aio_context = bdrv_get_aio_context(bs);
2217 aio_context_acquire(aio_context);
2218 if (bs->job) {
2219 block_job_cancel_sync(bs->job);
2220 aio_context_release(aio_context);
2221 break;
2223 aio_context_release(aio_context);
2226 /* All the remaining BlockDriverStates are referenced directly or
2227 * indirectly from block jobs, so there needs to be at least one BDS
2228 * directly used by a block job */
2229 assert(bs);
2233 /* Note that bs->device_list.tqe_prev is initially null,
2234 * and gets set to non-null by QTAILQ_INSERT_TAIL(). Establish
2235 * the useful invariant "bs in bdrv_states iff bs->tqe_prev" by
2236 * resetting it to null on remove. */
2237 void bdrv_device_remove(BlockDriverState *bs)
2239 QTAILQ_REMOVE(&bdrv_states, bs, device_list);
2240 bs->device_list.tqe_prev = NULL;
2243 /* make a BlockDriverState anonymous by removing from bdrv_state and
2244 * graph_bdrv_state list.
2245 Also, NULL terminate the device_name to prevent double remove */
2246 void bdrv_make_anon(BlockDriverState *bs)
2248 /* Take care to remove bs from bdrv_states only when it's actually
2249 * in it. */
2250 if (bs->device_list.tqe_prev) {
2251 bdrv_device_remove(bs);
2253 if (bs->node_name[0] != '\0') {
2254 QTAILQ_REMOVE(&graph_bdrv_states, bs, node_list);
2256 bs->node_name[0] = '\0';
2259 /* Fields that need to stay with the top-level BDS */
2260 static void bdrv_move_feature_fields(BlockDriverState *bs_dest,
2261 BlockDriverState *bs_src)
2263 /* move some fields that need to stay attached to the device */
2265 /* dev info */
2266 bs_dest->copy_on_read = bs_src->copy_on_read;
2268 bs_dest->enable_write_cache = bs_src->enable_write_cache;
2270 /* dirty bitmap */
2271 bs_dest->dirty_bitmaps = bs_src->dirty_bitmaps;
2274 static void change_parent_backing_link(BlockDriverState *from,
2275 BlockDriverState *to)
2277 BdrvChild *c, *next;
2279 QLIST_FOREACH_SAFE(c, &from->parents, next_parent, next) {
2280 assert(c->role != &child_backing);
2281 c->bs = to;
2282 QLIST_REMOVE(c, next_parent);
2283 QLIST_INSERT_HEAD(&to->parents, c, next_parent);
2284 bdrv_ref(to);
2285 bdrv_unref(from);
2287 if (from->blk) {
2288 blk_set_bs(from->blk, to);
2289 if (!to->device_list.tqe_prev) {
2290 QTAILQ_INSERT_BEFORE(from, to, device_list);
2292 bdrv_device_remove(from);
2296 static void swap_feature_fields(BlockDriverState *bs_top,
2297 BlockDriverState *bs_new)
2299 BlockDriverState tmp;
2301 bdrv_move_feature_fields(&tmp, bs_top);
2302 bdrv_move_feature_fields(bs_top, bs_new);
2303 bdrv_move_feature_fields(bs_new, &tmp);
2305 assert(!bs_new->throttle_state);
2306 if (bs_top->throttle_state) {
2307 assert(bs_top->io_limits_enabled);
2308 bdrv_io_limits_enable(bs_new, throttle_group_get_name(bs_top));
2309 bdrv_io_limits_disable(bs_top);
2314 * Add new bs contents at the top of an image chain while the chain is
2315 * live, while keeping required fields on the top layer.
2317 * This will modify the BlockDriverState fields, and swap contents
2318 * between bs_new and bs_top. Both bs_new and bs_top are modified.
2320 * bs_new must not be attached to a BlockBackend.
2322 * This function does not create any image files.
2324 * bdrv_append() takes ownership of a bs_new reference and unrefs it because
2325 * that's what the callers commonly need. bs_new will be referenced by the old
2326 * parents of bs_top after bdrv_append() returns. If the caller needs to keep a
2327 * reference of its own, it must call bdrv_ref().
2329 void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top)
2331 assert(!bdrv_requests_pending(bs_top));
2332 assert(!bdrv_requests_pending(bs_new));
2334 bdrv_ref(bs_top);
2335 change_parent_backing_link(bs_top, bs_new);
2337 /* Some fields always stay on top of the backing file chain */
2338 swap_feature_fields(bs_top, bs_new);
2340 bdrv_set_backing_hd(bs_new, bs_top);
2341 bdrv_unref(bs_top);
2343 /* bs_new is now referenced by its new parents, we don't need the
2344 * additional reference any more. */
2345 bdrv_unref(bs_new);
2348 void bdrv_replace_in_backing_chain(BlockDriverState *old, BlockDriverState *new)
2350 assert(!bdrv_requests_pending(old));
2351 assert(!bdrv_requests_pending(new));
2353 bdrv_ref(old);
2355 if (old->blk) {
2356 /* As long as these fields aren't in BlockBackend, but in the top-level
2357 * BlockDriverState, it's not possible for a BDS to have two BBs.
2359 * We really want to copy the fields from old to new, but we go for a
2360 * swap instead so that pointers aren't duplicated and cause trouble.
2361 * (Also, bdrv_swap() used to do the same.) */
2362 assert(!new->blk);
2363 swap_feature_fields(old, new);
2365 change_parent_backing_link(old, new);
2367 /* Change backing files if a previously independent node is added to the
2368 * chain. For active commit, we replace top by its own (indirect) backing
2369 * file and don't do anything here so we don't build a loop. */
2370 if (new->backing == NULL && !bdrv_chain_contains(backing_bs(old), new)) {
2371 bdrv_set_backing_hd(new, backing_bs(old));
2372 bdrv_set_backing_hd(old, NULL);
2375 bdrv_unref(old);
2378 static void bdrv_delete(BlockDriverState *bs)
2380 assert(!bs->job);
2381 assert(bdrv_op_blocker_is_empty(bs));
2382 assert(!bs->refcnt);
2384 bdrv_close(bs);
2386 /* remove from list, if necessary */
2387 bdrv_make_anon(bs);
2389 QTAILQ_REMOVE(&all_bdrv_states, bs, bs_list);
2391 g_free(bs);
2395 * Run consistency checks on an image
2397 * Returns 0 if the check could be completed (it doesn't mean that the image is
2398 * free of errors) or -errno when an internal error occurred. The results of the
2399 * check are stored in res.
2401 int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix)
2403 if (bs->drv == NULL) {
2404 return -ENOMEDIUM;
2406 if (bs->drv->bdrv_check == NULL) {
2407 return -ENOTSUP;
2410 memset(res, 0, sizeof(*res));
2411 return bs->drv->bdrv_check(bs, res, fix);
2414 #define COMMIT_BUF_SECTORS 2048
2416 /* commit COW file into the raw image */
2417 int bdrv_commit(BlockDriverState *bs)
2419 BlockDriver *drv = bs->drv;
2420 int64_t sector, total_sectors, length, backing_length;
2421 int n, ro, open_flags;
2422 int ret = 0;
2423 uint8_t *buf = NULL;
2425 if (!drv)
2426 return -ENOMEDIUM;
2428 if (!bs->backing) {
2429 return -ENOTSUP;
2432 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, NULL) ||
2433 bdrv_op_is_blocked(bs->backing->bs, BLOCK_OP_TYPE_COMMIT_TARGET, NULL)) {
2434 return -EBUSY;
2437 ro = bs->backing->bs->read_only;
2438 open_flags = bs->backing->bs->open_flags;
2440 if (ro) {
2441 if (bdrv_reopen(bs->backing->bs, open_flags | BDRV_O_RDWR, NULL)) {
2442 return -EACCES;
2446 length = bdrv_getlength(bs);
2447 if (length < 0) {
2448 ret = length;
2449 goto ro_cleanup;
2452 backing_length = bdrv_getlength(bs->backing->bs);
2453 if (backing_length < 0) {
2454 ret = backing_length;
2455 goto ro_cleanup;
2458 /* If our top snapshot is larger than the backing file image,
2459 * grow the backing file image if possible. If not possible,
2460 * we must return an error */
2461 if (length > backing_length) {
2462 ret = bdrv_truncate(bs->backing->bs, length);
2463 if (ret < 0) {
2464 goto ro_cleanup;
2468 total_sectors = length >> BDRV_SECTOR_BITS;
2470 /* qemu_try_blockalign() for bs will choose an alignment that works for
2471 * bs->backing->bs as well, so no need to compare the alignment manually. */
2472 buf = qemu_try_blockalign(bs, COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE);
2473 if (buf == NULL) {
2474 ret = -ENOMEM;
2475 goto ro_cleanup;
2478 for (sector = 0; sector < total_sectors; sector += n) {
2479 ret = bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n);
2480 if (ret < 0) {
2481 goto ro_cleanup;
2483 if (ret) {
2484 ret = bdrv_read(bs, sector, buf, n);
2485 if (ret < 0) {
2486 goto ro_cleanup;
2489 ret = bdrv_write(bs->backing->bs, sector, buf, n);
2490 if (ret < 0) {
2491 goto ro_cleanup;
2496 if (drv->bdrv_make_empty) {
2497 ret = drv->bdrv_make_empty(bs);
2498 if (ret < 0) {
2499 goto ro_cleanup;
2501 bdrv_flush(bs);
2505 * Make sure all data we wrote to the backing device is actually
2506 * stable on disk.
2508 if (bs->backing) {
2509 bdrv_flush(bs->backing->bs);
2512 ret = 0;
2513 ro_cleanup:
2514 qemu_vfree(buf);
2516 if (ro) {
2517 /* ignoring error return here */
2518 bdrv_reopen(bs->backing->bs, open_flags & ~BDRV_O_RDWR, NULL);
2521 return ret;
2524 int bdrv_commit_all(void)
2526 BlockDriverState *bs;
2528 QTAILQ_FOREACH(bs, &bdrv_states, device_list) {
2529 AioContext *aio_context = bdrv_get_aio_context(bs);
2531 aio_context_acquire(aio_context);
2532 if (bs->drv && bs->backing) {
2533 int ret = bdrv_commit(bs);
2534 if (ret < 0) {
2535 aio_context_release(aio_context);
2536 return ret;
2539 aio_context_release(aio_context);
2541 return 0;
2545 * Return values:
2546 * 0 - success
2547 * -EINVAL - backing format specified, but no file
2548 * -ENOSPC - can't update the backing file because no space is left in the
2549 * image file header
2550 * -ENOTSUP - format driver doesn't support changing the backing file
2552 int bdrv_change_backing_file(BlockDriverState *bs,
2553 const char *backing_file, const char *backing_fmt)
2555 BlockDriver *drv = bs->drv;
2556 int ret;
2558 /* Backing file format doesn't make sense without a backing file */
2559 if (backing_fmt && !backing_file) {
2560 return -EINVAL;
2563 if (drv->bdrv_change_backing_file != NULL) {
2564 ret = drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
2565 } else {
2566 ret = -ENOTSUP;
2569 if (ret == 0) {
2570 pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: "");
2571 pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: "");
2573 return ret;
2577 * Finds the image layer in the chain that has 'bs' as its backing file.
2579 * active is the current topmost image.
2581 * Returns NULL if bs is not found in active's image chain,
2582 * or if active == bs.
2584 * Returns the bottommost base image if bs == NULL.
2586 BlockDriverState *bdrv_find_overlay(BlockDriverState *active,
2587 BlockDriverState *bs)
2589 while (active && bs != backing_bs(active)) {
2590 active = backing_bs(active);
2593 return active;
2596 /* Given a BDS, searches for the base layer. */
2597 BlockDriverState *bdrv_find_base(BlockDriverState *bs)
2599 return bdrv_find_overlay(bs, NULL);
2603 * Drops images above 'base' up to and including 'top', and sets the image
2604 * above 'top' to have base as its backing file.
2606 * Requires that the overlay to 'top' is opened r/w, so that the backing file
2607 * information in 'bs' can be properly updated.
2609 * E.g., this will convert the following chain:
2610 * bottom <- base <- intermediate <- top <- active
2612 * to
2614 * bottom <- base <- active
2616 * It is allowed for bottom==base, in which case it converts:
2618 * base <- intermediate <- top <- active
2620 * to
2622 * base <- active
2624 * If backing_file_str is non-NULL, it will be used when modifying top's
2625 * overlay image metadata.
2627 * Error conditions:
2628 * if active == top, that is considered an error
2631 int bdrv_drop_intermediate(BlockDriverState *active, BlockDriverState *top,
2632 BlockDriverState *base, const char *backing_file_str)
2634 BlockDriverState *new_top_bs = NULL;
2635 int ret = -EIO;
2637 if (!top->drv || !base->drv) {
2638 goto exit;
2641 new_top_bs = bdrv_find_overlay(active, top);
2643 if (new_top_bs == NULL) {
2644 /* we could not find the image above 'top', this is an error */
2645 goto exit;
2648 /* special case of new_top_bs->backing->bs already pointing to base - nothing
2649 * to do, no intermediate images */
2650 if (backing_bs(new_top_bs) == base) {
2651 ret = 0;
2652 goto exit;
2655 /* Make sure that base is in the backing chain of top */
2656 if (!bdrv_chain_contains(top, base)) {
2657 goto exit;
2660 /* success - we can delete the intermediate states, and link top->base */
2661 backing_file_str = backing_file_str ? backing_file_str : base->filename;
2662 ret = bdrv_change_backing_file(new_top_bs, backing_file_str,
2663 base->drv ? base->drv->format_name : "");
2664 if (ret) {
2665 goto exit;
2667 bdrv_set_backing_hd(new_top_bs, base);
2669 ret = 0;
2670 exit:
2671 return ret;
2675 * Truncate file to 'offset' bytes (needed only for file protocols)
2677 int bdrv_truncate(BlockDriverState *bs, int64_t offset)
2679 BlockDriver *drv = bs->drv;
2680 int ret;
2681 if (!drv)
2682 return -ENOMEDIUM;
2683 if (!drv->bdrv_truncate)
2684 return -ENOTSUP;
2685 if (bs->read_only)
2686 return -EACCES;
2688 ret = drv->bdrv_truncate(bs, offset);
2689 if (ret == 0) {
2690 ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
2691 bdrv_dirty_bitmap_truncate(bs);
2692 if (bs->blk) {
2693 blk_dev_resize_cb(bs->blk);
2696 return ret;
2700 * Length of a allocated file in bytes. Sparse files are counted by actual
2701 * allocated space. Return < 0 if error or unknown.
2703 int64_t bdrv_get_allocated_file_size(BlockDriverState *bs)
2705 BlockDriver *drv = bs->drv;
2706 if (!drv) {
2707 return -ENOMEDIUM;
2709 if (drv->bdrv_get_allocated_file_size) {
2710 return drv->bdrv_get_allocated_file_size(bs);
2712 if (bs->file) {
2713 return bdrv_get_allocated_file_size(bs->file->bs);
2715 return -ENOTSUP;
2719 * Return number of sectors on success, -errno on error.
2721 int64_t bdrv_nb_sectors(BlockDriverState *bs)
2723 BlockDriver *drv = bs->drv;
2725 if (!drv)
2726 return -ENOMEDIUM;
2728 if (drv->has_variable_length) {
2729 int ret = refresh_total_sectors(bs, bs->total_sectors);
2730 if (ret < 0) {
2731 return ret;
2734 return bs->total_sectors;
2738 * Return length in bytes on success, -errno on error.
2739 * The length is always a multiple of BDRV_SECTOR_SIZE.
2741 int64_t bdrv_getlength(BlockDriverState *bs)
2743 int64_t ret = bdrv_nb_sectors(bs);
2745 ret = ret > INT64_MAX / BDRV_SECTOR_SIZE ? -EFBIG : ret;
2746 return ret < 0 ? ret : ret * BDRV_SECTOR_SIZE;
2749 /* return 0 as number of sectors if no device present or error */
2750 void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
2752 int64_t nb_sectors = bdrv_nb_sectors(bs);
2754 *nb_sectors_ptr = nb_sectors < 0 ? 0 : nb_sectors;
2757 int bdrv_is_read_only(BlockDriverState *bs)
2759 return bs->read_only;
2762 int bdrv_is_sg(BlockDriverState *bs)
2764 return bs->sg;
2767 int bdrv_enable_write_cache(BlockDriverState *bs)
2769 return bs->enable_write_cache;
2772 void bdrv_set_enable_write_cache(BlockDriverState *bs, bool wce)
2774 bs->enable_write_cache = wce;
2776 /* so a reopen() will preserve wce */
2777 if (wce) {
2778 bs->open_flags |= BDRV_O_CACHE_WB;
2779 } else {
2780 bs->open_flags &= ~BDRV_O_CACHE_WB;
2784 int bdrv_is_encrypted(BlockDriverState *bs)
2786 if (bs->backing && bs->backing->bs->encrypted) {
2787 return 1;
2789 return bs->encrypted;
2792 int bdrv_key_required(BlockDriverState *bs)
2794 BdrvChild *backing = bs->backing;
2796 if (backing && backing->bs->encrypted && !backing->bs->valid_key) {
2797 return 1;
2799 return (bs->encrypted && !bs->valid_key);
2802 int bdrv_set_key(BlockDriverState *bs, const char *key)
2804 int ret;
2805 if (bs->backing && bs->backing->bs->encrypted) {
2806 ret = bdrv_set_key(bs->backing->bs, key);
2807 if (ret < 0)
2808 return ret;
2809 if (!bs->encrypted)
2810 return 0;
2812 if (!bs->encrypted) {
2813 return -EINVAL;
2814 } else if (!bs->drv || !bs->drv->bdrv_set_key) {
2815 return -ENOMEDIUM;
2817 ret = bs->drv->bdrv_set_key(bs, key);
2818 if (ret < 0) {
2819 bs->valid_key = 0;
2820 } else if (!bs->valid_key) {
2821 bs->valid_key = 1;
2822 if (bs->blk) {
2823 /* call the change callback now, we skipped it on open */
2824 blk_dev_change_media_cb(bs->blk, true);
2827 return ret;
2831 * Provide an encryption key for @bs.
2832 * If @key is non-null:
2833 * If @bs is not encrypted, fail.
2834 * Else if the key is invalid, fail.
2835 * Else set @bs's key to @key, replacing the existing key, if any.
2836 * If @key is null:
2837 * If @bs is encrypted and still lacks a key, fail.
2838 * Else do nothing.
2839 * On failure, store an error object through @errp if non-null.
2841 void bdrv_add_key(BlockDriverState *bs, const char *key, Error **errp)
2843 if (key) {
2844 if (!bdrv_is_encrypted(bs)) {
2845 error_setg(errp, "Node '%s' is not encrypted",
2846 bdrv_get_device_or_node_name(bs));
2847 } else if (bdrv_set_key(bs, key) < 0) {
2848 error_setg(errp, QERR_INVALID_PASSWORD);
2850 } else {
2851 if (bdrv_key_required(bs)) {
2852 error_set(errp, ERROR_CLASS_DEVICE_ENCRYPTED,
2853 "'%s' (%s) is encrypted",
2854 bdrv_get_device_or_node_name(bs),
2855 bdrv_get_encrypted_filename(bs));
2860 const char *bdrv_get_format_name(BlockDriverState *bs)
2862 return bs->drv ? bs->drv->format_name : NULL;
2865 static int qsort_strcmp(const void *a, const void *b)
2867 return strcmp(a, b);
2870 void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
2871 void *opaque)
2873 BlockDriver *drv;
2874 int count = 0;
2875 int i;
2876 const char **formats = NULL;
2878 QLIST_FOREACH(drv, &bdrv_drivers, list) {
2879 if (drv->format_name) {
2880 bool found = false;
2881 int i = count;
2882 while (formats && i && !found) {
2883 found = !strcmp(formats[--i], drv->format_name);
2886 if (!found) {
2887 formats = g_renew(const char *, formats, count + 1);
2888 formats[count++] = drv->format_name;
2893 qsort(formats, count, sizeof(formats[0]), qsort_strcmp);
2895 for (i = 0; i < count; i++) {
2896 it(opaque, formats[i]);
2899 g_free(formats);
2902 /* This function is to find a node in the bs graph */
2903 BlockDriverState *bdrv_find_node(const char *node_name)
2905 BlockDriverState *bs;
2907 assert(node_name);
2909 QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) {
2910 if (!strcmp(node_name, bs->node_name)) {
2911 return bs;
2914 return NULL;
2917 /* Put this QMP function here so it can access the static graph_bdrv_states. */
2918 BlockDeviceInfoList *bdrv_named_nodes_list(Error **errp)
2920 BlockDeviceInfoList *list, *entry;
2921 BlockDriverState *bs;
2923 list = NULL;
2924 QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) {
2925 BlockDeviceInfo *info = bdrv_block_device_info(bs, errp);
2926 if (!info) {
2927 qapi_free_BlockDeviceInfoList(list);
2928 return NULL;
2930 entry = g_malloc0(sizeof(*entry));
2931 entry->value = info;
2932 entry->next = list;
2933 list = entry;
2936 return list;
2939 BlockDriverState *bdrv_lookup_bs(const char *device,
2940 const char *node_name,
2941 Error **errp)
2943 BlockBackend *blk;
2944 BlockDriverState *bs;
2946 if (device) {
2947 blk = blk_by_name(device);
2949 if (blk) {
2950 bs = blk_bs(blk);
2951 if (!bs) {
2952 error_setg(errp, "Device '%s' has no medium", device);
2955 return bs;
2959 if (node_name) {
2960 bs = bdrv_find_node(node_name);
2962 if (bs) {
2963 return bs;
2967 error_setg(errp, "Cannot find device=%s nor node_name=%s",
2968 device ? device : "",
2969 node_name ? node_name : "");
2970 return NULL;
2973 /* If 'base' is in the same chain as 'top', return true. Otherwise,
2974 * return false. If either argument is NULL, return false. */
2975 bool bdrv_chain_contains(BlockDriverState *top, BlockDriverState *base)
2977 while (top && top != base) {
2978 top = backing_bs(top);
2981 return top != NULL;
2984 BlockDriverState *bdrv_next_node(BlockDriverState *bs)
2986 if (!bs) {
2987 return QTAILQ_FIRST(&graph_bdrv_states);
2989 return QTAILQ_NEXT(bs, node_list);
2992 BlockDriverState *bdrv_next(BlockDriverState *bs)
2994 if (!bs) {
2995 return QTAILQ_FIRST(&bdrv_states);
2997 return QTAILQ_NEXT(bs, device_list);
3000 const char *bdrv_get_node_name(const BlockDriverState *bs)
3002 return bs->node_name;
3005 /* TODO check what callers really want: bs->node_name or blk_name() */
3006 const char *bdrv_get_device_name(const BlockDriverState *bs)
3008 return bs->blk ? blk_name(bs->blk) : "";
3011 /* This can be used to identify nodes that might not have a device
3012 * name associated. Since node and device names live in the same
3013 * namespace, the result is unambiguous. The exception is if both are
3014 * absent, then this returns an empty (non-null) string. */
3015 const char *bdrv_get_device_or_node_name(const BlockDriverState *bs)
3017 return bs->blk ? blk_name(bs->blk) : bs->node_name;
3020 int bdrv_get_flags(BlockDriverState *bs)
3022 return bs->open_flags;
3025 int bdrv_has_zero_init_1(BlockDriverState *bs)
3027 return 1;
3030 int bdrv_has_zero_init(BlockDriverState *bs)
3032 assert(bs->drv);
3034 /* If BS is a copy on write image, it is initialized to
3035 the contents of the base image, which may not be zeroes. */
3036 if (bs->backing) {
3037 return 0;
3039 if (bs->drv->bdrv_has_zero_init) {
3040 return bs->drv->bdrv_has_zero_init(bs);
3043 /* safe default */
3044 return 0;
3047 bool bdrv_unallocated_blocks_are_zero(BlockDriverState *bs)
3049 BlockDriverInfo bdi;
3051 if (bs->backing) {
3052 return false;
3055 if (bdrv_get_info(bs, &bdi) == 0) {
3056 return bdi.unallocated_blocks_are_zero;
3059 return false;
3062 bool bdrv_can_write_zeroes_with_unmap(BlockDriverState *bs)
3064 BlockDriverInfo bdi;
3066 if (bs->backing || !(bs->open_flags & BDRV_O_UNMAP)) {
3067 return false;
3070 if (bdrv_get_info(bs, &bdi) == 0) {
3071 return bdi.can_write_zeroes_with_unmap;
3074 return false;
3077 const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
3079 if (bs->backing && bs->backing->bs->encrypted)
3080 return bs->backing_file;
3081 else if (bs->encrypted)
3082 return bs->filename;
3083 else
3084 return NULL;
3087 void bdrv_get_backing_filename(BlockDriverState *bs,
3088 char *filename, int filename_size)
3090 pstrcpy(filename, filename_size, bs->backing_file);
3093 int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
3095 BlockDriver *drv = bs->drv;
3096 if (!drv)
3097 return -ENOMEDIUM;
3098 if (!drv->bdrv_get_info)
3099 return -ENOTSUP;
3100 memset(bdi, 0, sizeof(*bdi));
3101 return drv->bdrv_get_info(bs, bdi);
3104 ImageInfoSpecific *bdrv_get_specific_info(BlockDriverState *bs)
3106 BlockDriver *drv = bs->drv;
3107 if (drv && drv->bdrv_get_specific_info) {
3108 return drv->bdrv_get_specific_info(bs);
3110 return NULL;
3113 void bdrv_debug_event(BlockDriverState *bs, BlkdebugEvent event)
3115 if (!bs || !bs->drv || !bs->drv->bdrv_debug_event) {
3116 return;
3119 bs->drv->bdrv_debug_event(bs, event);
3122 int bdrv_debug_breakpoint(BlockDriverState *bs, const char *event,
3123 const char *tag)
3125 while (bs && bs->drv && !bs->drv->bdrv_debug_breakpoint) {
3126 bs = bs->file ? bs->file->bs : NULL;
3129 if (bs && bs->drv && bs->drv->bdrv_debug_breakpoint) {
3130 return bs->drv->bdrv_debug_breakpoint(bs, event, tag);
3133 return -ENOTSUP;
3136 int bdrv_debug_remove_breakpoint(BlockDriverState *bs, const char *tag)
3138 while (bs && bs->drv && !bs->drv->bdrv_debug_remove_breakpoint) {
3139 bs = bs->file ? bs->file->bs : NULL;
3142 if (bs && bs->drv && bs->drv->bdrv_debug_remove_breakpoint) {
3143 return bs->drv->bdrv_debug_remove_breakpoint(bs, tag);
3146 return -ENOTSUP;
3149 int bdrv_debug_resume(BlockDriverState *bs, const char *tag)
3151 while (bs && (!bs->drv || !bs->drv->bdrv_debug_resume)) {
3152 bs = bs->file ? bs->file->bs : NULL;
3155 if (bs && bs->drv && bs->drv->bdrv_debug_resume) {
3156 return bs->drv->bdrv_debug_resume(bs, tag);
3159 return -ENOTSUP;
3162 bool bdrv_debug_is_suspended(BlockDriverState *bs, const char *tag)
3164 while (bs && bs->drv && !bs->drv->bdrv_debug_is_suspended) {
3165 bs = bs->file ? bs->file->bs : NULL;
3168 if (bs && bs->drv && bs->drv->bdrv_debug_is_suspended) {
3169 return bs->drv->bdrv_debug_is_suspended(bs, tag);
3172 return false;
3175 int bdrv_is_snapshot(BlockDriverState *bs)
3177 return !!(bs->open_flags & BDRV_O_SNAPSHOT);
3180 /* backing_file can either be relative, or absolute, or a protocol. If it is
3181 * relative, it must be relative to the chain. So, passing in bs->filename
3182 * from a BDS as backing_file should not be done, as that may be relative to
3183 * the CWD rather than the chain. */
3184 BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs,
3185 const char *backing_file)
3187 char *filename_full = NULL;
3188 char *backing_file_full = NULL;
3189 char *filename_tmp = NULL;
3190 int is_protocol = 0;
3191 BlockDriverState *curr_bs = NULL;
3192 BlockDriverState *retval = NULL;
3194 if (!bs || !bs->drv || !backing_file) {
3195 return NULL;
3198 filename_full = g_malloc(PATH_MAX);
3199 backing_file_full = g_malloc(PATH_MAX);
3200 filename_tmp = g_malloc(PATH_MAX);
3202 is_protocol = path_has_protocol(backing_file);
3204 for (curr_bs = bs; curr_bs->backing; curr_bs = curr_bs->backing->bs) {
3206 /* If either of the filename paths is actually a protocol, then
3207 * compare unmodified paths; otherwise make paths relative */
3208 if (is_protocol || path_has_protocol(curr_bs->backing_file)) {
3209 if (strcmp(backing_file, curr_bs->backing_file) == 0) {
3210 retval = curr_bs->backing->bs;
3211 break;
3213 } else {
3214 /* If not an absolute filename path, make it relative to the current
3215 * image's filename path */
3216 path_combine(filename_tmp, PATH_MAX, curr_bs->filename,
3217 backing_file);
3219 /* We are going to compare absolute pathnames */
3220 if (!realpath(filename_tmp, filename_full)) {
3221 continue;
3224 /* We need to make sure the backing filename we are comparing against
3225 * is relative to the current image filename (or absolute) */
3226 path_combine(filename_tmp, PATH_MAX, curr_bs->filename,
3227 curr_bs->backing_file);
3229 if (!realpath(filename_tmp, backing_file_full)) {
3230 continue;
3233 if (strcmp(backing_file_full, filename_full) == 0) {
3234 retval = curr_bs->backing->bs;
3235 break;
3240 g_free(filename_full);
3241 g_free(backing_file_full);
3242 g_free(filename_tmp);
3243 return retval;
3246 int bdrv_get_backing_file_depth(BlockDriverState *bs)
3248 if (!bs->drv) {
3249 return 0;
3252 if (!bs->backing) {
3253 return 0;
3256 return 1 + bdrv_get_backing_file_depth(bs->backing->bs);
3259 void bdrv_init(void)
3261 module_call_init(MODULE_INIT_BLOCK);
3264 void bdrv_init_with_whitelist(void)
3266 use_bdrv_whitelist = 1;
3267 bdrv_init();
3270 void bdrv_invalidate_cache(BlockDriverState *bs, Error **errp)
3272 Error *local_err = NULL;
3273 int ret;
3275 if (!bs->drv) {
3276 return;
3279 if (!(bs->open_flags & BDRV_O_INACTIVE)) {
3280 return;
3282 bs->open_flags &= ~BDRV_O_INACTIVE;
3284 if (bs->drv->bdrv_invalidate_cache) {
3285 bs->drv->bdrv_invalidate_cache(bs, &local_err);
3286 } else if (bs->file) {
3287 bdrv_invalidate_cache(bs->file->bs, &local_err);
3289 if (local_err) {
3290 bs->open_flags |= BDRV_O_INACTIVE;
3291 error_propagate(errp, local_err);
3292 return;
3295 ret = refresh_total_sectors(bs, bs->total_sectors);
3296 if (ret < 0) {
3297 bs->open_flags |= BDRV_O_INACTIVE;
3298 error_setg_errno(errp, -ret, "Could not refresh total sector count");
3299 return;
3303 void bdrv_invalidate_cache_all(Error **errp)
3305 BlockDriverState *bs;
3306 Error *local_err = NULL;
3308 QTAILQ_FOREACH(bs, &bdrv_states, device_list) {
3309 AioContext *aio_context = bdrv_get_aio_context(bs);
3311 aio_context_acquire(aio_context);
3312 bdrv_invalidate_cache(bs, &local_err);
3313 aio_context_release(aio_context);
3314 if (local_err) {
3315 error_propagate(errp, local_err);
3316 return;
3321 static int bdrv_inactivate(BlockDriverState *bs)
3323 int ret;
3325 if (bs->drv->bdrv_inactivate) {
3326 ret = bs->drv->bdrv_inactivate(bs);
3327 if (ret < 0) {
3328 return ret;
3332 bs->open_flags |= BDRV_O_INACTIVE;
3333 return 0;
3336 int bdrv_inactivate_all(void)
3338 BlockDriverState *bs;
3339 int ret;
3341 QTAILQ_FOREACH(bs, &bdrv_states, device_list) {
3342 AioContext *aio_context = bdrv_get_aio_context(bs);
3344 aio_context_acquire(aio_context);
3345 ret = bdrv_inactivate(bs);
3346 aio_context_release(aio_context);
3347 if (ret < 0) {
3348 return ret;
3352 return 0;
3355 /**************************************************************/
3356 /* removable device support */
3359 * Return TRUE if the media is present
3361 bool bdrv_is_inserted(BlockDriverState *bs)
3363 BlockDriver *drv = bs->drv;
3364 BdrvChild *child;
3366 if (!drv) {
3367 return false;
3369 if (drv->bdrv_is_inserted) {
3370 return drv->bdrv_is_inserted(bs);
3372 QLIST_FOREACH(child, &bs->children, next) {
3373 if (!bdrv_is_inserted(child->bs)) {
3374 return false;
3377 return true;
3381 * Return whether the media changed since the last call to this
3382 * function, or -ENOTSUP if we don't know. Most drivers don't know.
3384 int bdrv_media_changed(BlockDriverState *bs)
3386 BlockDriver *drv = bs->drv;
3388 if (drv && drv->bdrv_media_changed) {
3389 return drv->bdrv_media_changed(bs);
3391 return -ENOTSUP;
3395 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
3397 void bdrv_eject(BlockDriverState *bs, bool eject_flag)
3399 BlockDriver *drv = bs->drv;
3400 const char *device_name;
3402 if (drv && drv->bdrv_eject) {
3403 drv->bdrv_eject(bs, eject_flag);
3406 device_name = bdrv_get_device_name(bs);
3407 if (device_name[0] != '\0') {
3408 qapi_event_send_device_tray_moved(device_name,
3409 eject_flag, &error_abort);
3414 * Lock or unlock the media (if it is locked, the user won't be able
3415 * to eject it manually).
3417 void bdrv_lock_medium(BlockDriverState *bs, bool locked)
3419 BlockDriver *drv = bs->drv;
3421 trace_bdrv_lock_medium(bs, locked);
3423 if (drv && drv->bdrv_lock_medium) {
3424 drv->bdrv_lock_medium(bs, locked);
3428 /* Get a reference to bs */
3429 void bdrv_ref(BlockDriverState *bs)
3431 bs->refcnt++;
3434 /* Release a previously grabbed reference to bs.
3435 * If after releasing, reference count is zero, the BlockDriverState is
3436 * deleted. */
3437 void bdrv_unref(BlockDriverState *bs)
3439 if (!bs) {
3440 return;
3442 assert(bs->refcnt > 0);
3443 if (--bs->refcnt == 0) {
3444 bdrv_delete(bs);
3448 struct BdrvOpBlocker {
3449 Error *reason;
3450 QLIST_ENTRY(BdrvOpBlocker) list;
3453 bool bdrv_op_is_blocked(BlockDriverState *bs, BlockOpType op, Error **errp)
3455 BdrvOpBlocker *blocker;
3456 assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX);
3457 if (!QLIST_EMPTY(&bs->op_blockers[op])) {
3458 blocker = QLIST_FIRST(&bs->op_blockers[op]);
3459 if (errp) {
3460 *errp = error_copy(blocker->reason);
3461 error_prepend(errp, "Node '%s' is busy: ",
3462 bdrv_get_device_or_node_name(bs));
3464 return true;
3466 return false;
3469 void bdrv_op_block(BlockDriverState *bs, BlockOpType op, Error *reason)
3471 BdrvOpBlocker *blocker;
3472 assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX);
3474 blocker = g_new0(BdrvOpBlocker, 1);
3475 blocker->reason = reason;
3476 QLIST_INSERT_HEAD(&bs->op_blockers[op], blocker, list);
3479 void bdrv_op_unblock(BlockDriverState *bs, BlockOpType op, Error *reason)
3481 BdrvOpBlocker *blocker, *next;
3482 assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX);
3483 QLIST_FOREACH_SAFE(blocker, &bs->op_blockers[op], list, next) {
3484 if (blocker->reason == reason) {
3485 QLIST_REMOVE(blocker, list);
3486 g_free(blocker);
3491 void bdrv_op_block_all(BlockDriverState *bs, Error *reason)
3493 int i;
3494 for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
3495 bdrv_op_block(bs, i, reason);
3499 void bdrv_op_unblock_all(BlockDriverState *bs, Error *reason)
3501 int i;
3502 for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
3503 bdrv_op_unblock(bs, i, reason);
3507 bool bdrv_op_blocker_is_empty(BlockDriverState *bs)
3509 int i;
3511 for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
3512 if (!QLIST_EMPTY(&bs->op_blockers[i])) {
3513 return false;
3516 return true;
3519 void bdrv_img_create(const char *filename, const char *fmt,
3520 const char *base_filename, const char *base_fmt,
3521 char *options, uint64_t img_size, int flags,
3522 Error **errp, bool quiet)
3524 QemuOptsList *create_opts = NULL;
3525 QemuOpts *opts = NULL;
3526 const char *backing_fmt, *backing_file;
3527 int64_t size;
3528 BlockDriver *drv, *proto_drv;
3529 Error *local_err = NULL;
3530 int ret = 0;
3532 /* Find driver and parse its options */
3533 drv = bdrv_find_format(fmt);
3534 if (!drv) {
3535 error_setg(errp, "Unknown file format '%s'", fmt);
3536 return;
3539 proto_drv = bdrv_find_protocol(filename, true, errp);
3540 if (!proto_drv) {
3541 return;
3544 if (!drv->create_opts) {
3545 error_setg(errp, "Format driver '%s' does not support image creation",
3546 drv->format_name);
3547 return;
3550 if (!proto_drv->create_opts) {
3551 error_setg(errp, "Protocol driver '%s' does not support image creation",
3552 proto_drv->format_name);
3553 return;
3556 create_opts = qemu_opts_append(create_opts, drv->create_opts);
3557 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
3559 /* Create parameter list with default values */
3560 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
3561 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort);
3563 /* Parse -o options */
3564 if (options) {
3565 qemu_opts_do_parse(opts, options, NULL, &local_err);
3566 if (local_err) {
3567 error_report_err(local_err);
3568 local_err = NULL;
3569 error_setg(errp, "Invalid options for file format '%s'", fmt);
3570 goto out;
3574 if (base_filename) {
3575 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &local_err);
3576 if (local_err) {
3577 error_setg(errp, "Backing file not supported for file format '%s'",
3578 fmt);
3579 goto out;
3583 if (base_fmt) {
3584 qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &local_err);
3585 if (local_err) {
3586 error_setg(errp, "Backing file format not supported for file "
3587 "format '%s'", fmt);
3588 goto out;
3592 backing_file = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
3593 if (backing_file) {
3594 if (!strcmp(filename, backing_file)) {
3595 error_setg(errp, "Error: Trying to create an image with the "
3596 "same filename as the backing file");
3597 goto out;
3601 backing_fmt = qemu_opt_get(opts, BLOCK_OPT_BACKING_FMT);
3603 // The size for the image must always be specified, with one exception:
3604 // If we are using a backing file, we can obtain the size from there
3605 size = qemu_opt_get_size(opts, BLOCK_OPT_SIZE, 0);
3606 if (size == -1) {
3607 if (backing_file) {
3608 BlockDriverState *bs;
3609 char *full_backing = g_new0(char, PATH_MAX);
3610 int64_t size;
3611 int back_flags;
3612 QDict *backing_options = NULL;
3614 bdrv_get_full_backing_filename_from_filename(filename, backing_file,
3615 full_backing, PATH_MAX,
3616 &local_err);
3617 if (local_err) {
3618 g_free(full_backing);
3619 goto out;
3622 /* backing files always opened read-only */
3623 back_flags =
3624 flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
3626 if (backing_fmt) {
3627 backing_options = qdict_new();
3628 qdict_put(backing_options, "driver",
3629 qstring_from_str(backing_fmt));
3632 bs = NULL;
3633 ret = bdrv_open(&bs, full_backing, NULL, backing_options,
3634 back_flags, &local_err);
3635 g_free(full_backing);
3636 if (ret < 0) {
3637 goto out;
3639 size = bdrv_getlength(bs);
3640 if (size < 0) {
3641 error_setg_errno(errp, -size, "Could not get size of '%s'",
3642 backing_file);
3643 bdrv_unref(bs);
3644 goto out;
3647 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, size, &error_abort);
3649 bdrv_unref(bs);
3650 } else {
3651 error_setg(errp, "Image creation needs a size parameter");
3652 goto out;
3656 if (!quiet) {
3657 printf("Formatting '%s', fmt=%s ", filename, fmt);
3658 qemu_opts_print(opts, " ");
3659 puts("");
3662 ret = bdrv_create(drv, filename, opts, &local_err);
3664 if (ret == -EFBIG) {
3665 /* This is generally a better message than whatever the driver would
3666 * deliver (especially because of the cluster_size_hint), since that
3667 * is most probably not much different from "image too large". */
3668 const char *cluster_size_hint = "";
3669 if (qemu_opt_get_size(opts, BLOCK_OPT_CLUSTER_SIZE, 0)) {
3670 cluster_size_hint = " (try using a larger cluster size)";
3672 error_setg(errp, "The image size is too large for file format '%s'"
3673 "%s", fmt, cluster_size_hint);
3674 error_free(local_err);
3675 local_err = NULL;
3678 out:
3679 qemu_opts_del(opts);
3680 qemu_opts_free(create_opts);
3681 if (local_err) {
3682 error_propagate(errp, local_err);
3686 AioContext *bdrv_get_aio_context(BlockDriverState *bs)
3688 return bs->aio_context;
3691 void bdrv_detach_aio_context(BlockDriverState *bs)
3693 BdrvAioNotifier *baf;
3695 if (!bs->drv) {
3696 return;
3699 QLIST_FOREACH(baf, &bs->aio_notifiers, list) {
3700 baf->detach_aio_context(baf->opaque);
3703 if (bs->throttle_state) {
3704 throttle_timers_detach_aio_context(&bs->throttle_timers);
3706 if (bs->drv->bdrv_detach_aio_context) {
3707 bs->drv->bdrv_detach_aio_context(bs);
3709 if (bs->file) {
3710 bdrv_detach_aio_context(bs->file->bs);
3712 if (bs->backing) {
3713 bdrv_detach_aio_context(bs->backing->bs);
3716 bs->aio_context = NULL;
3719 void bdrv_attach_aio_context(BlockDriverState *bs,
3720 AioContext *new_context)
3722 BdrvAioNotifier *ban;
3724 if (!bs->drv) {
3725 return;
3728 bs->aio_context = new_context;
3730 if (bs->backing) {
3731 bdrv_attach_aio_context(bs->backing->bs, new_context);
3733 if (bs->file) {
3734 bdrv_attach_aio_context(bs->file->bs, new_context);
3736 if (bs->drv->bdrv_attach_aio_context) {
3737 bs->drv->bdrv_attach_aio_context(bs, new_context);
3739 if (bs->throttle_state) {
3740 throttle_timers_attach_aio_context(&bs->throttle_timers, new_context);
3743 QLIST_FOREACH(ban, &bs->aio_notifiers, list) {
3744 ban->attached_aio_context(new_context, ban->opaque);
3748 void bdrv_set_aio_context(BlockDriverState *bs, AioContext *new_context)
3750 bdrv_drain(bs); /* ensure there are no in-flight requests */
3752 bdrv_detach_aio_context(bs);
3754 /* This function executes in the old AioContext so acquire the new one in
3755 * case it runs in a different thread.
3757 aio_context_acquire(new_context);
3758 bdrv_attach_aio_context(bs, new_context);
3759 aio_context_release(new_context);
3762 void bdrv_add_aio_context_notifier(BlockDriverState *bs,
3763 void (*attached_aio_context)(AioContext *new_context, void *opaque),
3764 void (*detach_aio_context)(void *opaque), void *opaque)
3766 BdrvAioNotifier *ban = g_new(BdrvAioNotifier, 1);
3767 *ban = (BdrvAioNotifier){
3768 .attached_aio_context = attached_aio_context,
3769 .detach_aio_context = detach_aio_context,
3770 .opaque = opaque
3773 QLIST_INSERT_HEAD(&bs->aio_notifiers, ban, list);
3776 void bdrv_remove_aio_context_notifier(BlockDriverState *bs,
3777 void (*attached_aio_context)(AioContext *,
3778 void *),
3779 void (*detach_aio_context)(void *),
3780 void *opaque)
3782 BdrvAioNotifier *ban, *ban_next;
3784 QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) {
3785 if (ban->attached_aio_context == attached_aio_context &&
3786 ban->detach_aio_context == detach_aio_context &&
3787 ban->opaque == opaque)
3789 QLIST_REMOVE(ban, list);
3790 g_free(ban);
3792 return;
3796 abort();
3799 int bdrv_amend_options(BlockDriverState *bs, QemuOpts *opts,
3800 BlockDriverAmendStatusCB *status_cb, void *cb_opaque)
3802 if (!bs->drv->bdrv_amend_options) {
3803 return -ENOTSUP;
3805 return bs->drv->bdrv_amend_options(bs, opts, status_cb, cb_opaque);
3808 /* This function will be called by the bdrv_recurse_is_first_non_filter method
3809 * of block filter and by bdrv_is_first_non_filter.
3810 * It is used to test if the given bs is the candidate or recurse more in the
3811 * node graph.
3813 bool bdrv_recurse_is_first_non_filter(BlockDriverState *bs,
3814 BlockDriverState *candidate)
3816 /* return false if basic checks fails */
3817 if (!bs || !bs->drv) {
3818 return false;
3821 /* the code reached a non block filter driver -> check if the bs is
3822 * the same as the candidate. It's the recursion termination condition.
3824 if (!bs->drv->is_filter) {
3825 return bs == candidate;
3827 /* Down this path the driver is a block filter driver */
3829 /* If the block filter recursion method is defined use it to recurse down
3830 * the node graph.
3832 if (bs->drv->bdrv_recurse_is_first_non_filter) {
3833 return bs->drv->bdrv_recurse_is_first_non_filter(bs, candidate);
3836 /* the driver is a block filter but don't allow to recurse -> return false
3838 return false;
3841 /* This function checks if the candidate is the first non filter bs down it's
3842 * bs chain. Since we don't have pointers to parents it explore all bs chains
3843 * from the top. Some filters can choose not to pass down the recursion.
3845 bool bdrv_is_first_non_filter(BlockDriverState *candidate)
3847 BlockDriverState *bs;
3849 /* walk down the bs forest recursively */
3850 QTAILQ_FOREACH(bs, &bdrv_states, device_list) {
3851 bool perm;
3853 /* try to recurse in this top level bs */
3854 perm = bdrv_recurse_is_first_non_filter(bs, candidate);
3856 /* candidate is the first non filter */
3857 if (perm) {
3858 return true;
3862 return false;
3865 BlockDriverState *check_to_replace_node(BlockDriverState *parent_bs,
3866 const char *node_name, Error **errp)
3868 BlockDriverState *to_replace_bs = bdrv_find_node(node_name);
3869 AioContext *aio_context;
3871 if (!to_replace_bs) {
3872 error_setg(errp, "Node name '%s' not found", node_name);
3873 return NULL;
3876 aio_context = bdrv_get_aio_context(to_replace_bs);
3877 aio_context_acquire(aio_context);
3879 if (bdrv_op_is_blocked(to_replace_bs, BLOCK_OP_TYPE_REPLACE, errp)) {
3880 to_replace_bs = NULL;
3881 goto out;
3884 /* We don't want arbitrary node of the BDS chain to be replaced only the top
3885 * most non filter in order to prevent data corruption.
3886 * Another benefit is that this tests exclude backing files which are
3887 * blocked by the backing blockers.
3889 if (!bdrv_recurse_is_first_non_filter(parent_bs, to_replace_bs)) {
3890 error_setg(errp, "Only top most non filter can be replaced");
3891 to_replace_bs = NULL;
3892 goto out;
3895 out:
3896 aio_context_release(aio_context);
3897 return to_replace_bs;
3900 static bool append_open_options(QDict *d, BlockDriverState *bs)
3902 const QDictEntry *entry;
3903 QemuOptDesc *desc;
3904 BdrvChild *child;
3905 bool found_any = false;
3906 const char *p;
3908 for (entry = qdict_first(bs->options); entry;
3909 entry = qdict_next(bs->options, entry))
3911 /* Exclude options for children */
3912 QLIST_FOREACH(child, &bs->children, next) {
3913 if (strstart(qdict_entry_key(entry), child->name, &p)
3914 && (!*p || *p == '.'))
3916 break;
3919 if (child) {
3920 continue;
3923 /* And exclude all non-driver-specific options */
3924 for (desc = bdrv_runtime_opts.desc; desc->name; desc++) {
3925 if (!strcmp(qdict_entry_key(entry), desc->name)) {
3926 break;
3929 if (desc->name) {
3930 continue;
3933 qobject_incref(qdict_entry_value(entry));
3934 qdict_put_obj(d, qdict_entry_key(entry), qdict_entry_value(entry));
3935 found_any = true;
3938 return found_any;
3941 /* Updates the following BDS fields:
3942 * - exact_filename: A filename which may be used for opening a block device
3943 * which (mostly) equals the given BDS (even without any
3944 * other options; so reading and writing must return the same
3945 * results, but caching etc. may be different)
3946 * - full_open_options: Options which, when given when opening a block device
3947 * (without a filename), result in a BDS (mostly)
3948 * equalling the given one
3949 * - filename: If exact_filename is set, it is copied here. Otherwise,
3950 * full_open_options is converted to a JSON object, prefixed with
3951 * "json:" (for use through the JSON pseudo protocol) and put here.
3953 void bdrv_refresh_filename(BlockDriverState *bs)
3955 BlockDriver *drv = bs->drv;
3956 QDict *opts;
3958 if (!drv) {
3959 return;
3962 /* This BDS's file name will most probably depend on its file's name, so
3963 * refresh that first */
3964 if (bs->file) {
3965 bdrv_refresh_filename(bs->file->bs);
3968 if (drv->bdrv_refresh_filename) {
3969 /* Obsolete information is of no use here, so drop the old file name
3970 * information before refreshing it */
3971 bs->exact_filename[0] = '\0';
3972 if (bs->full_open_options) {
3973 QDECREF(bs->full_open_options);
3974 bs->full_open_options = NULL;
3977 opts = qdict_new();
3978 append_open_options(opts, bs);
3979 drv->bdrv_refresh_filename(bs, opts);
3980 QDECREF(opts);
3981 } else if (bs->file) {
3982 /* Try to reconstruct valid information from the underlying file */
3983 bool has_open_options;
3985 bs->exact_filename[0] = '\0';
3986 if (bs->full_open_options) {
3987 QDECREF(bs->full_open_options);
3988 bs->full_open_options = NULL;
3991 opts = qdict_new();
3992 has_open_options = append_open_options(opts, bs);
3994 /* If no specific options have been given for this BDS, the filename of
3995 * the underlying file should suffice for this one as well */
3996 if (bs->file->bs->exact_filename[0] && !has_open_options) {
3997 strcpy(bs->exact_filename, bs->file->bs->exact_filename);
3999 /* Reconstructing the full options QDict is simple for most format block
4000 * drivers, as long as the full options are known for the underlying
4001 * file BDS. The full options QDict of that file BDS should somehow
4002 * contain a representation of the filename, therefore the following
4003 * suffices without querying the (exact_)filename of this BDS. */
4004 if (bs->file->bs->full_open_options) {
4005 qdict_put_obj(opts, "driver",
4006 QOBJECT(qstring_from_str(drv->format_name)));
4007 QINCREF(bs->file->bs->full_open_options);
4008 qdict_put_obj(opts, "file",
4009 QOBJECT(bs->file->bs->full_open_options));
4011 bs->full_open_options = opts;
4012 } else {
4013 QDECREF(opts);
4015 } else if (!bs->full_open_options && qdict_size(bs->options)) {
4016 /* There is no underlying file BDS (at least referenced by BDS.file),
4017 * so the full options QDict should be equal to the options given
4018 * specifically for this block device when it was opened (plus the
4019 * driver specification).
4020 * Because those options don't change, there is no need to update
4021 * full_open_options when it's already set. */
4023 opts = qdict_new();
4024 append_open_options(opts, bs);
4025 qdict_put_obj(opts, "driver",
4026 QOBJECT(qstring_from_str(drv->format_name)));
4028 if (bs->exact_filename[0]) {
4029 /* This may not work for all block protocol drivers (some may
4030 * require this filename to be parsed), but we have to find some
4031 * default solution here, so just include it. If some block driver
4032 * does not support pure options without any filename at all or
4033 * needs some special format of the options QDict, it needs to
4034 * implement the driver-specific bdrv_refresh_filename() function.
4036 qdict_put_obj(opts, "filename",
4037 QOBJECT(qstring_from_str(bs->exact_filename)));
4040 bs->full_open_options = opts;
4043 if (bs->exact_filename[0]) {
4044 pstrcpy(bs->filename, sizeof(bs->filename), bs->exact_filename);
4045 } else if (bs->full_open_options) {
4046 QString *json = qobject_to_json(QOBJECT(bs->full_open_options));
4047 snprintf(bs->filename, sizeof(bs->filename), "json:%s",
4048 qstring_get_str(json));
4049 QDECREF(json);