block: Fix crash when loading snapshot on inactive node
[qemu/ar7.git] / block / snapshot.c
blobc4d40e80dd2b6e18852497222a13068adc05f274
1 /*
2 * Block layer snapshot related functions
4 * Copyright (c) 2003-2008 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.
25 #include "qemu/osdep.h"
26 #include "block/snapshot.h"
27 #include "block/block_int.h"
28 #include "block/qdict.h"
29 #include "qapi/error.h"
30 #include "qapi/qmp/qdict.h"
31 #include "qapi/qmp/qerror.h"
32 #include "qapi/qmp/qstring.h"
33 #include "qemu/option.h"
34 #include "sysemu/block-backend.h"
36 QemuOptsList internal_snapshot_opts = {
37 .name = "snapshot",
38 .head = QTAILQ_HEAD_INITIALIZER(internal_snapshot_opts.head),
39 .desc = {
41 .name = SNAPSHOT_OPT_ID,
42 .type = QEMU_OPT_STRING,
43 .help = "snapshot id"
44 },{
45 .name = SNAPSHOT_OPT_NAME,
46 .type = QEMU_OPT_STRING,
47 .help = "snapshot name"
48 },{
49 /* end of list */
54 int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
55 const char *name)
57 QEMUSnapshotInfo *sn_tab, *sn;
58 int nb_sns, i, ret;
60 GLOBAL_STATE_CODE();
62 ret = -ENOENT;
63 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
64 if (nb_sns < 0) {
65 return ret;
67 for (i = 0; i < nb_sns; i++) {
68 sn = &sn_tab[i];
69 if (!strcmp(sn->name, name)) {
70 *sn_info = *sn;
71 ret = 0;
72 break;
75 g_free(sn_tab);
76 return ret;
79 /**
80 * Look up an internal snapshot by @id and @name.
81 * @bs: block device to search
82 * @id: unique snapshot ID, or NULL
83 * @name: snapshot name, or NULL
84 * @sn_info: location to store information on the snapshot found
85 * @errp: location to store error, will be set only for exception
87 * This function will traverse snapshot list in @bs to search the matching
88 * one, @id and @name are the matching condition:
89 * If both @id and @name are specified, find the first one with id @id and
90 * name @name.
91 * If only @id is specified, find the first one with id @id.
92 * If only @name is specified, find the first one with name @name.
93 * if none is specified, abort().
95 * Returns: true when a snapshot is found and @sn_info will be filled, false
96 * when error or not found. If all operation succeed but no matching one is
97 * found, @errp will NOT be set.
99 bool bdrv_snapshot_find_by_id_and_name(BlockDriverState *bs,
100 const char *id,
101 const char *name,
102 QEMUSnapshotInfo *sn_info,
103 Error **errp)
105 QEMUSnapshotInfo *sn_tab, *sn;
106 int nb_sns, i;
107 bool ret = false;
109 assert(id || name);
110 GLOBAL_STATE_CODE();
112 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
113 if (nb_sns < 0) {
114 error_setg_errno(errp, -nb_sns, "Failed to get a snapshot list");
115 return false;
116 } else if (nb_sns == 0) {
117 return false;
120 if (id && name) {
121 for (i = 0; i < nb_sns; i++) {
122 sn = &sn_tab[i];
123 if (!strcmp(sn->id_str, id) && !strcmp(sn->name, name)) {
124 *sn_info = *sn;
125 ret = true;
126 break;
129 } else if (id) {
130 for (i = 0; i < nb_sns; i++) {
131 sn = &sn_tab[i];
132 if (!strcmp(sn->id_str, id)) {
133 *sn_info = *sn;
134 ret = true;
135 break;
138 } else if (name) {
139 for (i = 0; i < nb_sns; i++) {
140 sn = &sn_tab[i];
141 if (!strcmp(sn->name, name)) {
142 *sn_info = *sn;
143 ret = true;
144 break;
149 g_free(sn_tab);
150 return ret;
154 * Return a pointer to child of given BDS to which we can fall
155 * back if the given BDS does not support snapshots.
156 * Return NULL if there is no BDS to (safely) fall back to.
158 static BdrvChild * GRAPH_RDLOCK
159 bdrv_snapshot_fallback_child(BlockDriverState *bs)
161 BdrvChild *fallback = bdrv_primary_child(bs);
162 BdrvChild *child;
164 GLOBAL_STATE_CODE();
165 assert_bdrv_graph_readable();
167 /* We allow fallback only to primary child */
168 if (!fallback) {
169 return NULL;
173 * Check that there are no other children that would need to be
174 * snapshotted. If there are, it is not safe to fall back to
175 * fallback.
177 QLIST_FOREACH(child, &bs->children, next) {
178 if (child->role & (BDRV_CHILD_DATA | BDRV_CHILD_METADATA |
179 BDRV_CHILD_FILTERED) &&
180 child != fallback)
182 return NULL;
186 return fallback;
189 static BlockDriverState * GRAPH_RDLOCK
190 bdrv_snapshot_fallback(BlockDriverState *bs)
192 GLOBAL_STATE_CODE();
193 return child_bs(bdrv_snapshot_fallback_child(bs));
196 int bdrv_can_snapshot(BlockDriverState *bs)
198 BlockDriver *drv = bs->drv;
200 GLOBAL_STATE_CODE();
202 if (!drv || !bdrv_is_inserted(bs) || !bdrv_is_writable(bs)) {
203 return 0;
206 if (!drv->bdrv_snapshot_create) {
207 BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs);
208 if (fallback_bs) {
209 return bdrv_can_snapshot(fallback_bs);
211 return 0;
214 return 1;
217 int bdrv_snapshot_create(BlockDriverState *bs,
218 QEMUSnapshotInfo *sn_info)
220 BlockDriver *drv = bs->drv;
221 BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs);
223 GLOBAL_STATE_CODE();
225 if (!drv) {
226 return -ENOMEDIUM;
228 if (drv->bdrv_snapshot_create) {
229 return drv->bdrv_snapshot_create(bs, sn_info);
231 if (fallback_bs) {
232 return bdrv_snapshot_create(fallback_bs, sn_info);
234 return -ENOTSUP;
237 int bdrv_snapshot_goto(BlockDriverState *bs,
238 const char *snapshot_id,
239 Error **errp)
241 BlockDriver *drv = bs->drv;
242 BdrvChild *fallback;
243 int ret, open_ret;
245 GLOBAL_STATE_CODE();
247 if (!drv) {
248 error_setg(errp, "Block driver is closed");
249 return -ENOMEDIUM;
252 if (!QLIST_EMPTY(&bs->dirty_bitmaps)) {
253 error_setg(errp, "Device has active dirty bitmaps");
254 return -EBUSY;
257 if (drv->bdrv_snapshot_goto) {
258 ret = drv->bdrv_snapshot_goto(bs, snapshot_id);
259 if (ret < 0) {
260 error_setg_errno(errp, -ret, "Failed to load snapshot");
262 return ret;
265 bdrv_graph_rdlock_main_loop();
266 fallback = bdrv_snapshot_fallback_child(bs);
267 bdrv_graph_rdunlock_main_loop();
269 if (fallback) {
270 QDict *options;
271 QDict *file_options;
272 Error *local_err = NULL;
273 BlockDriverState *fallback_bs = fallback->bs;
274 char *subqdict_prefix = g_strdup_printf("%s.", fallback->name);
276 options = qdict_clone_shallow(bs->options);
278 /* Prevent it from getting deleted when detached from bs */
279 bdrv_ref(fallback_bs);
281 qdict_extract_subqdict(options, &file_options, subqdict_prefix);
282 qobject_unref(file_options);
283 g_free(subqdict_prefix);
285 /* Force .bdrv_open() below to re-attach fallback_bs on fallback */
286 qdict_put_str(options, fallback->name,
287 bdrv_get_node_name(fallback_bs));
289 /* Now close bs, apply the snapshot on fallback_bs, and re-open bs */
290 if (drv->bdrv_close) {
291 drv->bdrv_close(bs);
294 /* .bdrv_open() will re-attach it */
295 bdrv_graph_wrlock(NULL);
296 bdrv_unref_child(bs, fallback);
297 bdrv_graph_wrunlock(NULL);
299 ret = bdrv_snapshot_goto(fallback_bs, snapshot_id, errp);
300 open_ret = drv->bdrv_open(bs, options, bs->open_flags, &local_err);
301 qobject_unref(options);
302 if (open_ret < 0) {
303 bdrv_unref(fallback_bs);
304 bs->drv = NULL;
305 /* A bdrv_snapshot_goto() error takes precedence */
306 error_propagate(errp, local_err);
307 return ret < 0 ? ret : open_ret;
311 * fallback was a primary child. It was closed above and set to NULL,
312 * but the .bdrv_open() call has opened it again, because we set the
313 * respective option (with the qdict_put_str() call above).
314 * Assert that .bdrv_open() has attached the right BDS as primary child.
316 bdrv_graph_rdlock_main_loop();
317 assert(bdrv_primary_bs(bs) == fallback_bs);
318 bdrv_graph_rdunlock_main_loop();
320 bdrv_unref(fallback_bs);
321 return ret;
324 error_setg(errp, "Block driver does not support snapshots");
325 return -ENOTSUP;
329 * Delete an internal snapshot by @snapshot_id and @name.
330 * @bs: block device used in the operation
331 * @snapshot_id: unique snapshot ID, or NULL
332 * @name: snapshot name, or NULL
333 * @errp: location to store error
335 * If both @snapshot_id and @name are specified, delete the first one with
336 * id @snapshot_id and name @name.
337 * If only @snapshot_id is specified, delete the first one with id
338 * @snapshot_id.
339 * If only @name is specified, delete the first one with name @name.
340 * if none is specified, return -EINVAL.
342 * Returns: 0 on success, -errno on failure. If @bs is not inserted, return
343 * -ENOMEDIUM. If @snapshot_id and @name are both NULL, return -EINVAL. If @bs
344 * does not support internal snapshot deletion, return -ENOTSUP. If @bs does
345 * not support parameter @snapshot_id or @name, or one of them is not correctly
346 * specified, return -EINVAL. If @bs can't find one matching @id and @name,
347 * return -ENOENT. If @errp != NULL, it will always be filled with error
348 * message on failure.
350 int bdrv_snapshot_delete(BlockDriverState *bs,
351 const char *snapshot_id,
352 const char *name,
353 Error **errp)
355 BlockDriver *drv = bs->drv;
356 BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs);
357 int ret;
359 GLOBAL_STATE_CODE();
361 if (!drv) {
362 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, bdrv_get_device_name(bs));
363 return -ENOMEDIUM;
365 if (!snapshot_id && !name) {
366 error_setg(errp, "snapshot_id and name are both NULL");
367 return -EINVAL;
370 /* drain all pending i/o before deleting snapshot */
371 bdrv_drained_begin(bs);
373 if (drv->bdrv_snapshot_delete) {
374 ret = drv->bdrv_snapshot_delete(bs, snapshot_id, name, errp);
375 } else if (fallback_bs) {
376 ret = bdrv_snapshot_delete(fallback_bs, snapshot_id, name, errp);
377 } else {
378 error_setg(errp, "Block format '%s' used by device '%s' "
379 "does not support internal snapshot deletion",
380 drv->format_name, bdrv_get_device_name(bs));
381 ret = -ENOTSUP;
384 bdrv_drained_end(bs);
385 return ret;
388 int bdrv_snapshot_list(BlockDriverState *bs,
389 QEMUSnapshotInfo **psn_info)
391 GLOBAL_STATE_CODE();
392 GRAPH_RDLOCK_GUARD_MAINLOOP();
394 BlockDriver *drv = bs->drv;
395 BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs);
397 if (!drv) {
398 return -ENOMEDIUM;
400 if (drv->bdrv_snapshot_list) {
401 return drv->bdrv_snapshot_list(bs, psn_info);
403 if (fallback_bs) {
404 return bdrv_snapshot_list(fallback_bs, psn_info);
406 return -ENOTSUP;
410 * Temporarily load an internal snapshot by @snapshot_id and @name.
411 * @bs: block device used in the operation
412 * @snapshot_id: unique snapshot ID, or NULL
413 * @name: snapshot name, or NULL
414 * @errp: location to store error
416 * If both @snapshot_id and @name are specified, load the first one with
417 * id @snapshot_id and name @name.
418 * If only @snapshot_id is specified, load the first one with id
419 * @snapshot_id.
420 * If only @name is specified, load the first one with name @name.
421 * if none is specified, return -EINVAL.
423 * Returns: 0 on success, -errno on fail. If @bs is not inserted, return
424 * -ENOMEDIUM. If @bs is not readonly, return -EINVAL. If @bs did not support
425 * internal snapshot, return -ENOTSUP. If qemu can't find a matching @id and
426 * @name, return -ENOENT. If @errp != NULL, it will always be filled on
427 * failure.
429 int bdrv_snapshot_load_tmp(BlockDriverState *bs,
430 const char *snapshot_id,
431 const char *name,
432 Error **errp)
434 BlockDriver *drv = bs->drv;
436 GLOBAL_STATE_CODE();
437 GRAPH_RDLOCK_GUARD_MAINLOOP();
439 if (!drv) {
440 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, bdrv_get_device_name(bs));
441 return -ENOMEDIUM;
443 if (!snapshot_id && !name) {
444 error_setg(errp, "snapshot_id and name are both NULL");
445 return -EINVAL;
447 if (!bdrv_is_read_only(bs)) {
448 error_setg(errp, "Device is not readonly");
449 return -EINVAL;
451 if (drv->bdrv_snapshot_load_tmp) {
452 return drv->bdrv_snapshot_load_tmp(bs, snapshot_id, name, errp);
454 error_setg(errp, "Block format '%s' used by device '%s' "
455 "does not support temporarily loading internal snapshots",
456 drv->format_name, bdrv_get_device_name(bs));
457 return -ENOTSUP;
460 int bdrv_snapshot_load_tmp_by_id_or_name(BlockDriverState *bs,
461 const char *id_or_name,
462 Error **errp)
464 int ret;
465 Error *local_err = NULL;
467 GLOBAL_STATE_CODE();
469 ret = bdrv_snapshot_load_tmp(bs, id_or_name, NULL, &local_err);
470 if (ret == -ENOENT || ret == -EINVAL) {
471 error_free(local_err);
472 local_err = NULL;
473 ret = bdrv_snapshot_load_tmp(bs, NULL, id_or_name, &local_err);
476 error_propagate(errp, local_err);
478 return ret;
482 static int GRAPH_RDLOCK
483 bdrv_all_get_snapshot_devices(bool has_devices, strList *devices,
484 GList **all_bdrvs, Error **errp)
486 g_autoptr(GList) bdrvs = NULL;
488 if (has_devices) {
489 if (!devices) {
490 error_setg(errp, "At least one device is required for snapshot");
491 return -1;
494 while (devices) {
495 BlockDriverState *bs = bdrv_find_node(devices->value);
496 if (!bs) {
497 error_setg(errp, "No block device node '%s'", devices->value);
498 return -1;
500 bdrvs = g_list_append(bdrvs, bs);
501 devices = devices->next;
503 } else {
504 BlockDriverState *bs;
505 BdrvNextIterator it;
506 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
507 bdrvs = g_list_append(bdrvs, bs);
511 *all_bdrvs = g_steal_pointer(&bdrvs);
512 return 0;
516 static bool GRAPH_RDLOCK bdrv_all_snapshots_includes_bs(BlockDriverState *bs)
518 GLOBAL_STATE_CODE();
519 assert_bdrv_graph_readable();
521 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
522 return false;
525 /* Include all nodes that are either in use by a BlockBackend, or that
526 * aren't attached to any node, but owned by the monitor. */
527 return bdrv_has_blk(bs) || QLIST_EMPTY(&bs->parents);
530 /* Group operations. All block drivers are involved.
531 * These functions will properly handle dataplane (take aio_context_acquire
532 * when appropriate for appropriate block drivers) */
534 bool bdrv_all_can_snapshot(bool has_devices, strList *devices,
535 Error **errp)
537 g_autoptr(GList) bdrvs = NULL;
538 GList *iterbdrvs;
540 GLOBAL_STATE_CODE();
541 GRAPH_RDLOCK_GUARD_MAINLOOP();
543 if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) {
544 return false;
547 iterbdrvs = bdrvs;
548 while (iterbdrvs) {
549 BlockDriverState *bs = iterbdrvs->data;
550 AioContext *ctx = bdrv_get_aio_context(bs);
551 bool ok = true;
553 aio_context_acquire(ctx);
554 if (devices || bdrv_all_snapshots_includes_bs(bs)) {
555 ok = bdrv_can_snapshot(bs);
557 aio_context_release(ctx);
558 if (!ok) {
559 error_setg(errp, "Device '%s' is writable but does not support "
560 "snapshots", bdrv_get_device_or_node_name(bs));
561 return false;
564 iterbdrvs = iterbdrvs->next;
567 return true;
570 int bdrv_all_delete_snapshot(const char *name,
571 bool has_devices, strList *devices,
572 Error **errp)
574 g_autoptr(GList) bdrvs = NULL;
575 GList *iterbdrvs;
577 GLOBAL_STATE_CODE();
578 GRAPH_RDLOCK_GUARD_MAINLOOP();
580 if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) {
581 return -1;
584 iterbdrvs = bdrvs;
585 while (iterbdrvs) {
586 BlockDriverState *bs = iterbdrvs->data;
587 AioContext *ctx = bdrv_get_aio_context(bs);
588 QEMUSnapshotInfo sn1, *snapshot = &sn1;
589 int ret = 0;
591 aio_context_acquire(ctx);
592 if ((devices || bdrv_all_snapshots_includes_bs(bs)) &&
593 bdrv_snapshot_find(bs, snapshot, name) >= 0)
595 ret = bdrv_snapshot_delete(bs, snapshot->id_str,
596 snapshot->name, errp);
598 aio_context_release(ctx);
599 if (ret < 0) {
600 error_prepend(errp, "Could not delete snapshot '%s' on '%s': ",
601 name, bdrv_get_device_or_node_name(bs));
602 return -1;
605 iterbdrvs = iterbdrvs->next;
608 return 0;
612 int bdrv_all_goto_snapshot(const char *name,
613 bool has_devices, strList *devices,
614 Error **errp)
616 g_autoptr(GList) bdrvs = NULL;
617 GList *iterbdrvs;
618 int ret;
620 GLOBAL_STATE_CODE();
622 bdrv_graph_rdlock_main_loop();
623 ret = bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp);
624 bdrv_graph_rdunlock_main_loop();
626 if (ret < 0) {
627 return -1;
630 iterbdrvs = bdrvs;
631 while (iterbdrvs) {
632 BlockDriverState *bs = iterbdrvs->data;
633 AioContext *ctx = bdrv_get_aio_context(bs);
634 bool all_snapshots_includes_bs;
636 aio_context_acquire(ctx);
637 bdrv_graph_rdlock_main_loop();
638 all_snapshots_includes_bs = bdrv_all_snapshots_includes_bs(bs);
639 bdrv_graph_rdunlock_main_loop();
641 ret = (devices || all_snapshots_includes_bs) ?
642 bdrv_snapshot_goto(bs, name, errp) : 0;
643 aio_context_release(ctx);
644 if (ret < 0) {
645 bdrv_graph_rdlock_main_loop();
646 error_prepend(errp, "Could not load snapshot '%s' on '%s': ",
647 name, bdrv_get_device_or_node_name(bs));
648 bdrv_graph_rdunlock_main_loop();
649 return -1;
652 iterbdrvs = iterbdrvs->next;
655 return 0;
658 int bdrv_all_has_snapshot(const char *name,
659 bool has_devices, strList *devices,
660 Error **errp)
662 g_autoptr(GList) bdrvs = NULL;
663 GList *iterbdrvs;
665 GLOBAL_STATE_CODE();
666 GRAPH_RDLOCK_GUARD_MAINLOOP();
668 if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) {
669 return -1;
672 iterbdrvs = bdrvs;
673 while (iterbdrvs) {
674 BlockDriverState *bs = iterbdrvs->data;
675 AioContext *ctx = bdrv_get_aio_context(bs);
676 QEMUSnapshotInfo sn;
677 int ret = 0;
679 aio_context_acquire(ctx);
680 if (devices || bdrv_all_snapshots_includes_bs(bs)) {
681 ret = bdrv_snapshot_find(bs, &sn, name);
683 aio_context_release(ctx);
684 if (ret < 0) {
685 if (ret == -ENOENT) {
686 return 0;
687 } else {
688 error_setg_errno(errp, errno,
689 "Could not check snapshot '%s' on '%s'",
690 name, bdrv_get_device_or_node_name(bs));
691 return -1;
695 iterbdrvs = iterbdrvs->next;
698 return 1;
701 int bdrv_all_create_snapshot(QEMUSnapshotInfo *sn,
702 BlockDriverState *vm_state_bs,
703 uint64_t vm_state_size,
704 bool has_devices, strList *devices,
705 Error **errp)
707 g_autoptr(GList) bdrvs = NULL;
708 GList *iterbdrvs;
710 GLOBAL_STATE_CODE();
711 GRAPH_RDLOCK_GUARD_MAINLOOP();
713 if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) {
714 return -1;
717 iterbdrvs = bdrvs;
718 while (iterbdrvs) {
719 BlockDriverState *bs = iterbdrvs->data;
720 AioContext *ctx = bdrv_get_aio_context(bs);
721 int ret = 0;
723 aio_context_acquire(ctx);
724 if (bs == vm_state_bs) {
725 sn->vm_state_size = vm_state_size;
726 ret = bdrv_snapshot_create(bs, sn);
727 } else if (devices || bdrv_all_snapshots_includes_bs(bs)) {
728 sn->vm_state_size = 0;
729 ret = bdrv_snapshot_create(bs, sn);
731 aio_context_release(ctx);
732 if (ret < 0) {
733 error_setg(errp, "Could not create snapshot '%s' on '%s'",
734 sn->name, bdrv_get_device_or_node_name(bs));
735 return -1;
738 iterbdrvs = iterbdrvs->next;
741 return 0;
745 BlockDriverState *bdrv_all_find_vmstate_bs(const char *vmstate_bs,
746 bool has_devices, strList *devices,
747 Error **errp)
749 g_autoptr(GList) bdrvs = NULL;
750 GList *iterbdrvs;
752 GLOBAL_STATE_CODE();
753 GRAPH_RDLOCK_GUARD_MAINLOOP();
755 if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) {
756 return NULL;
759 iterbdrvs = bdrvs;
760 while (iterbdrvs) {
761 BlockDriverState *bs = iterbdrvs->data;
762 AioContext *ctx = bdrv_get_aio_context(bs);
763 bool found = false;
765 aio_context_acquire(ctx);
766 found = (devices || bdrv_all_snapshots_includes_bs(bs)) &&
767 bdrv_can_snapshot(bs);
768 aio_context_release(ctx);
770 if (vmstate_bs) {
771 if (g_str_equal(vmstate_bs,
772 bdrv_get_node_name(bs))) {
773 if (found) {
774 return bs;
775 } else {
776 error_setg(errp,
777 "vmstate block device '%s' does not support snapshots",
778 vmstate_bs);
779 return NULL;
782 } else if (found) {
783 return bs;
786 iterbdrvs = iterbdrvs->next;
789 if (vmstate_bs) {
790 error_setg(errp,
791 "vmstate block device '%s' does not exist", vmstate_bs);
792 } else {
793 error_setg(errp,
794 "no block device can store vmstate for snapshot");
796 return NULL;