xref: /openbmc/qemu/block/snapshot.c (revision a6f4d2ec)
1 /*
2  * Block layer snapshot related functions
3  *
4  * Copyright (c) 2003-2008 Fabrice Bellard
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 #include "qemu/osdep.h"
26 #include "block/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"
35 
36 QemuOptsList internal_snapshot_opts = {
37     .name = "snapshot",
38     .head = QTAILQ_HEAD_INITIALIZER(internal_snapshot_opts.head),
39     .desc = {
40         {
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 */
50         }
51     },
52 };
53 
54 int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
55                        const char *name)
56 {
57     QEMUSnapshotInfo *sn_tab, *sn;
58     int nb_sns, i, ret;
59 
60     GLOBAL_STATE_CODE();
61 
62     ret = -ENOENT;
63     nb_sns = bdrv_snapshot_list(bs, &sn_tab);
64     if (nb_sns < 0) {
65         return ret;
66     }
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;
73         }
74     }
75     g_free(sn_tab);
76     return ret;
77 }
78 
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
86  *
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().
94  *
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.
98  */
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)
104 {
105     QEMUSnapshotInfo *sn_tab, *sn;
106     int nb_sns, i;
107     bool ret = false;
108 
109     assert(id || name);
110     GLOBAL_STATE_CODE();
111 
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;
118     }
119 
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;
127             }
128         }
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;
136             }
137         }
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;
145             }
146         }
147     }
148 
149     g_free(sn_tab);
150     return ret;
151 }
152 
153 /**
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.
157  */
158 static BdrvChild *bdrv_snapshot_fallback_child(BlockDriverState *bs)
159 {
160     BdrvChild *fallback = bdrv_primary_child(bs);
161     BdrvChild *child;
162 
163     /* We allow fallback only to primary child */
164     if (!fallback) {
165         return NULL;
166     }
167 
168     /*
169      * Check that there are no other children that would need to be
170      * snapshotted.  If there are, it is not safe to fall back to
171      * fallback.
172      */
173     QLIST_FOREACH(child, &bs->children, next) {
174         if (child->role & (BDRV_CHILD_DATA | BDRV_CHILD_METADATA |
175                            BDRV_CHILD_FILTERED) &&
176             child != fallback)
177         {
178             return NULL;
179         }
180     }
181 
182     return fallback;
183 }
184 
185 static BlockDriverState *bdrv_snapshot_fallback(BlockDriverState *bs)
186 {
187     return child_bs(bdrv_snapshot_fallback_child(bs));
188 }
189 
190 int bdrv_can_snapshot(BlockDriverState *bs)
191 {
192     BlockDriver *drv = bs->drv;
193     GLOBAL_STATE_CODE();
194     if (!drv || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
195         return 0;
196     }
197 
198     if (!drv->bdrv_snapshot_create) {
199         BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs);
200         if (fallback_bs) {
201             return bdrv_can_snapshot(fallback_bs);
202         }
203         return 0;
204     }
205 
206     return 1;
207 }
208 
209 int bdrv_snapshot_create(BlockDriverState *bs,
210                          QEMUSnapshotInfo *sn_info)
211 {
212     BlockDriver *drv = bs->drv;
213     BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs);
214 
215     GLOBAL_STATE_CODE();
216 
217     if (!drv) {
218         return -ENOMEDIUM;
219     }
220     if (drv->bdrv_snapshot_create) {
221         return drv->bdrv_snapshot_create(bs, sn_info);
222     }
223     if (fallback_bs) {
224         return bdrv_snapshot_create(fallback_bs, sn_info);
225     }
226     return -ENOTSUP;
227 }
228 
229 int bdrv_snapshot_goto(BlockDriverState *bs,
230                        const char *snapshot_id,
231                        Error **errp)
232 {
233     BlockDriver *drv = bs->drv;
234     BdrvChild *fallback;
235     int ret, open_ret;
236 
237     GLOBAL_STATE_CODE();
238 
239     if (!drv) {
240         error_setg(errp, "Block driver is closed");
241         return -ENOMEDIUM;
242     }
243 
244     if (!QLIST_EMPTY(&bs->dirty_bitmaps)) {
245         error_setg(errp, "Device has active dirty bitmaps");
246         return -EBUSY;
247     }
248 
249     if (drv->bdrv_snapshot_goto) {
250         ret = drv->bdrv_snapshot_goto(bs, snapshot_id);
251         if (ret < 0) {
252             error_setg_errno(errp, -ret, "Failed to load snapshot");
253         }
254         return ret;
255     }
256 
257     fallback = bdrv_snapshot_fallback_child(bs);
258     if (fallback) {
259         QDict *options;
260         QDict *file_options;
261         Error *local_err = NULL;
262         BlockDriverState *fallback_bs = fallback->bs;
263         char *subqdict_prefix = g_strdup_printf("%s.", fallback->name);
264 
265         options = qdict_clone_shallow(bs->options);
266 
267         /* Prevent it from getting deleted when detached from bs */
268         bdrv_ref(fallback_bs);
269 
270         qdict_extract_subqdict(options, &file_options, subqdict_prefix);
271         qobject_unref(file_options);
272         g_free(subqdict_prefix);
273 
274         /* Force .bdrv_open() below to re-attach fallback_bs on fallback */
275         qdict_put_str(options, fallback->name,
276                       bdrv_get_node_name(fallback_bs));
277 
278         /* Now close bs, apply the snapshot on fallback_bs, and re-open bs */
279         if (drv->bdrv_close) {
280             drv->bdrv_close(bs);
281         }
282 
283         /* .bdrv_open() will re-attach it */
284         bdrv_graph_wrlock(NULL);
285         bdrv_unref_child(bs, fallback);
286         bdrv_graph_wrunlock();
287 
288         ret = bdrv_snapshot_goto(fallback_bs, snapshot_id, errp);
289         open_ret = drv->bdrv_open(bs, options, bs->open_flags, &local_err);
290         qobject_unref(options);
291         if (open_ret < 0) {
292             bdrv_unref(fallback_bs);
293             bs->drv = NULL;
294             /* A bdrv_snapshot_goto() error takes precedence */
295             error_propagate(errp, local_err);
296             return ret < 0 ? ret : open_ret;
297         }
298 
299         /*
300          * fallback was a primary child. It was closed above and set to NULL,
301          * but the .bdrv_open() call has opened it again, because we set the
302          * respective option (with the qdict_put_str() call above).
303          * Assert that .bdrv_open() has attached the right BDS as primary child.
304          */
305         assert(bdrv_primary_bs(bs) == fallback_bs);
306         bdrv_unref(fallback_bs);
307         return ret;
308     }
309 
310     error_setg(errp, "Block driver does not support snapshots");
311     return -ENOTSUP;
312 }
313 
314 /**
315  * Delete an internal snapshot by @snapshot_id and @name.
316  * @bs: block device used in the operation
317  * @snapshot_id: unique snapshot ID, or NULL
318  * @name: snapshot name, or NULL
319  * @errp: location to store error
320  *
321  * If both @snapshot_id and @name are specified, delete the first one with
322  * id @snapshot_id and name @name.
323  * If only @snapshot_id is specified, delete the first one with id
324  * @snapshot_id.
325  * If only @name is specified, delete the first one with name @name.
326  * if none is specified, return -EINVAL.
327  *
328  * Returns: 0 on success, -errno on failure. If @bs is not inserted, return
329  * -ENOMEDIUM. If @snapshot_id and @name are both NULL, return -EINVAL. If @bs
330  * does not support internal snapshot deletion, return -ENOTSUP. If @bs does
331  * not support parameter @snapshot_id or @name, or one of them is not correctly
332  * specified, return -EINVAL. If @bs can't find one matching @id and @name,
333  * return -ENOENT. If @errp != NULL, it will always be filled with error
334  * message on failure.
335  */
336 int bdrv_snapshot_delete(BlockDriverState *bs,
337                          const char *snapshot_id,
338                          const char *name,
339                          Error **errp)
340 {
341     BlockDriver *drv = bs->drv;
342     BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs);
343     int ret;
344 
345     GLOBAL_STATE_CODE();
346 
347     if (!drv) {
348         error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, bdrv_get_device_name(bs));
349         return -ENOMEDIUM;
350     }
351     if (!snapshot_id && !name) {
352         error_setg(errp, "snapshot_id and name are both NULL");
353         return -EINVAL;
354     }
355 
356     /* drain all pending i/o before deleting snapshot */
357     bdrv_drained_begin(bs);
358 
359     if (drv->bdrv_snapshot_delete) {
360         ret = drv->bdrv_snapshot_delete(bs, snapshot_id, name, errp);
361     } else if (fallback_bs) {
362         ret = bdrv_snapshot_delete(fallback_bs, snapshot_id, name, errp);
363     } else {
364         error_setg(errp, "Block format '%s' used by device '%s' "
365                    "does not support internal snapshot deletion",
366                    drv->format_name, bdrv_get_device_name(bs));
367         ret = -ENOTSUP;
368     }
369 
370     bdrv_drained_end(bs);
371     return ret;
372 }
373 
374 int bdrv_snapshot_list(BlockDriverState *bs,
375                        QEMUSnapshotInfo **psn_info)
376 {
377     BlockDriver *drv = bs->drv;
378     BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs);
379 
380     GLOBAL_STATE_CODE();
381     if (!drv) {
382         return -ENOMEDIUM;
383     }
384     if (drv->bdrv_snapshot_list) {
385         return drv->bdrv_snapshot_list(bs, psn_info);
386     }
387     if (fallback_bs) {
388         return bdrv_snapshot_list(fallback_bs, psn_info);
389     }
390     return -ENOTSUP;
391 }
392 
393 /**
394  * Temporarily load an internal snapshot by @snapshot_id and @name.
395  * @bs: block device used in the operation
396  * @snapshot_id: unique snapshot ID, or NULL
397  * @name: snapshot name, or NULL
398  * @errp: location to store error
399  *
400  * If both @snapshot_id and @name are specified, load the first one with
401  * id @snapshot_id and name @name.
402  * If only @snapshot_id is specified, load the first one with id
403  * @snapshot_id.
404  * If only @name is specified, load the first one with name @name.
405  * if none is specified, return -EINVAL.
406  *
407  * Returns: 0 on success, -errno on fail. If @bs is not inserted, return
408  * -ENOMEDIUM. If @bs is not readonly, return -EINVAL. If @bs did not support
409  * internal snapshot, return -ENOTSUP. If qemu can't find a matching @id and
410  * @name, return -ENOENT. If @errp != NULL, it will always be filled on
411  * failure.
412  */
413 int bdrv_snapshot_load_tmp(BlockDriverState *bs,
414                            const char *snapshot_id,
415                            const char *name,
416                            Error **errp)
417 {
418     BlockDriver *drv = bs->drv;
419 
420     GLOBAL_STATE_CODE();
421 
422     if (!drv) {
423         error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, bdrv_get_device_name(bs));
424         return -ENOMEDIUM;
425     }
426     if (!snapshot_id && !name) {
427         error_setg(errp, "snapshot_id and name are both NULL");
428         return -EINVAL;
429     }
430     if (!bdrv_is_read_only(bs)) {
431         error_setg(errp, "Device is not readonly");
432         return -EINVAL;
433     }
434     if (drv->bdrv_snapshot_load_tmp) {
435         return drv->bdrv_snapshot_load_tmp(bs, snapshot_id, name, errp);
436     }
437     error_setg(errp, "Block format '%s' used by device '%s' "
438                "does not support temporarily loading internal snapshots",
439                drv->format_name, bdrv_get_device_name(bs));
440     return -ENOTSUP;
441 }
442 
443 int bdrv_snapshot_load_tmp_by_id_or_name(BlockDriverState *bs,
444                                          const char *id_or_name,
445                                          Error **errp)
446 {
447     int ret;
448     Error *local_err = NULL;
449 
450     GLOBAL_STATE_CODE();
451 
452     ret = bdrv_snapshot_load_tmp(bs, id_or_name, NULL, &local_err);
453     if (ret == -ENOENT || ret == -EINVAL) {
454         error_free(local_err);
455         local_err = NULL;
456         ret = bdrv_snapshot_load_tmp(bs, NULL, id_or_name, &local_err);
457     }
458 
459     error_propagate(errp, local_err);
460 
461     return ret;
462 }
463 
464 
465 static int bdrv_all_get_snapshot_devices(bool has_devices, strList *devices,
466                                          GList **all_bdrvs,
467                                          Error **errp)
468 {
469     g_autoptr(GList) bdrvs = NULL;
470 
471     if (has_devices) {
472         if (!devices) {
473             error_setg(errp, "At least one device is required for snapshot");
474             return -1;
475         }
476 
477         while (devices) {
478             BlockDriverState *bs = bdrv_find_node(devices->value);
479             if (!bs) {
480                 error_setg(errp, "No block device node '%s'", devices->value);
481                 return -1;
482             }
483             bdrvs = g_list_append(bdrvs, bs);
484             devices = devices->next;
485         }
486     } else {
487         BlockDriverState *bs;
488         BdrvNextIterator it;
489         for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
490             bdrvs = g_list_append(bdrvs, bs);
491         }
492     }
493 
494     *all_bdrvs = g_steal_pointer(&bdrvs);
495     return 0;
496 }
497 
498 
499 static bool bdrv_all_snapshots_includes_bs(BlockDriverState *bs)
500 {
501     if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
502         return false;
503     }
504 
505     /* Include all nodes that are either in use by a BlockBackend, or that
506      * aren't attached to any node, but owned by the monitor. */
507     return bdrv_has_blk(bs) || QLIST_EMPTY(&bs->parents);
508 }
509 
510 /* Group operations. All block drivers are involved.
511  * These functions will properly handle dataplane (take aio_context_acquire
512  * when appropriate for appropriate block drivers) */
513 
514 bool bdrv_all_can_snapshot(bool has_devices, strList *devices,
515                            Error **errp)
516 {
517     g_autoptr(GList) bdrvs = NULL;
518     GList *iterbdrvs;
519 
520     GLOBAL_STATE_CODE();
521 
522     if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) {
523         return false;
524     }
525 
526     iterbdrvs = bdrvs;
527     while (iterbdrvs) {
528         BlockDriverState *bs = iterbdrvs->data;
529         AioContext *ctx = bdrv_get_aio_context(bs);
530         bool ok = true;
531 
532         aio_context_acquire(ctx);
533         if (devices || bdrv_all_snapshots_includes_bs(bs)) {
534             ok = bdrv_can_snapshot(bs);
535         }
536         aio_context_release(ctx);
537         if (!ok) {
538             error_setg(errp, "Device '%s' is writable but does not support "
539                        "snapshots", bdrv_get_device_or_node_name(bs));
540             return false;
541         }
542 
543         iterbdrvs = iterbdrvs->next;
544     }
545 
546     return true;
547 }
548 
549 int bdrv_all_delete_snapshot(const char *name,
550                              bool has_devices, strList *devices,
551                              Error **errp)
552 {
553     g_autoptr(GList) bdrvs = NULL;
554     GList *iterbdrvs;
555 
556     GLOBAL_STATE_CODE();
557 
558     if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) {
559         return -1;
560     }
561 
562     iterbdrvs = bdrvs;
563     while (iterbdrvs) {
564         BlockDriverState *bs = iterbdrvs->data;
565         AioContext *ctx = bdrv_get_aio_context(bs);
566         QEMUSnapshotInfo sn1, *snapshot = &sn1;
567         int ret = 0;
568 
569         aio_context_acquire(ctx);
570         if ((devices || bdrv_all_snapshots_includes_bs(bs)) &&
571             bdrv_snapshot_find(bs, snapshot, name) >= 0)
572         {
573             ret = bdrv_snapshot_delete(bs, snapshot->id_str,
574                                        snapshot->name, errp);
575         }
576         aio_context_release(ctx);
577         if (ret < 0) {
578             error_prepend(errp, "Could not delete snapshot '%s' on '%s': ",
579                           name, bdrv_get_device_or_node_name(bs));
580             return -1;
581         }
582 
583         iterbdrvs = iterbdrvs->next;
584     }
585 
586     return 0;
587 }
588 
589 
590 int bdrv_all_goto_snapshot(const char *name,
591                            bool has_devices, strList *devices,
592                            Error **errp)
593 {
594     g_autoptr(GList) bdrvs = NULL;
595     GList *iterbdrvs;
596 
597     GLOBAL_STATE_CODE();
598 
599     if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) {
600         return -1;
601     }
602 
603     iterbdrvs = bdrvs;
604     while (iterbdrvs) {
605         BlockDriverState *bs = iterbdrvs->data;
606         AioContext *ctx = bdrv_get_aio_context(bs);
607         int ret = 0;
608 
609         aio_context_acquire(ctx);
610         if (devices || bdrv_all_snapshots_includes_bs(bs)) {
611             ret = bdrv_snapshot_goto(bs, name, errp);
612         }
613         aio_context_release(ctx);
614         if (ret < 0) {
615             error_prepend(errp, "Could not load snapshot '%s' on '%s': ",
616                           name, bdrv_get_device_or_node_name(bs));
617             return -1;
618         }
619 
620         iterbdrvs = iterbdrvs->next;
621     }
622 
623     return 0;
624 }
625 
626 int bdrv_all_has_snapshot(const char *name,
627                           bool has_devices, strList *devices,
628                           Error **errp)
629 {
630     g_autoptr(GList) bdrvs = NULL;
631     GList *iterbdrvs;
632 
633     GLOBAL_STATE_CODE();
634 
635     if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) {
636         return -1;
637     }
638 
639     iterbdrvs = bdrvs;
640     while (iterbdrvs) {
641         BlockDriverState *bs = iterbdrvs->data;
642         AioContext *ctx = bdrv_get_aio_context(bs);
643         QEMUSnapshotInfo sn;
644         int ret = 0;
645 
646         aio_context_acquire(ctx);
647         if (devices || bdrv_all_snapshots_includes_bs(bs)) {
648             ret = bdrv_snapshot_find(bs, &sn, name);
649         }
650         aio_context_release(ctx);
651         if (ret < 0) {
652             if (ret == -ENOENT) {
653                 return 0;
654             } else {
655                 error_setg_errno(errp, errno,
656                                  "Could not check snapshot '%s' on '%s'",
657                                  name, bdrv_get_device_or_node_name(bs));
658                 return -1;
659             }
660         }
661 
662         iterbdrvs = iterbdrvs->next;
663     }
664 
665     return 1;
666 }
667 
668 int bdrv_all_create_snapshot(QEMUSnapshotInfo *sn,
669                              BlockDriverState *vm_state_bs,
670                              uint64_t vm_state_size,
671                              bool has_devices, strList *devices,
672                              Error **errp)
673 {
674     g_autoptr(GList) bdrvs = NULL;
675     GList *iterbdrvs;
676     GLOBAL_STATE_CODE();
677 
678     if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) {
679         return -1;
680     }
681 
682     iterbdrvs = bdrvs;
683     while (iterbdrvs) {
684         BlockDriverState *bs = iterbdrvs->data;
685         AioContext *ctx = bdrv_get_aio_context(bs);
686         int ret = 0;
687 
688         aio_context_acquire(ctx);
689         if (bs == vm_state_bs) {
690             sn->vm_state_size = vm_state_size;
691             ret = bdrv_snapshot_create(bs, sn);
692         } else if (devices || bdrv_all_snapshots_includes_bs(bs)) {
693             sn->vm_state_size = 0;
694             ret = bdrv_snapshot_create(bs, sn);
695         }
696         aio_context_release(ctx);
697         if (ret < 0) {
698             error_setg(errp, "Could not create snapshot '%s' on '%s'",
699                        sn->name, bdrv_get_device_or_node_name(bs));
700             return -1;
701         }
702 
703         iterbdrvs = iterbdrvs->next;
704     }
705 
706     return 0;
707 }
708 
709 
710 BlockDriverState *bdrv_all_find_vmstate_bs(const char *vmstate_bs,
711                                            bool has_devices, strList *devices,
712                                            Error **errp)
713 {
714     g_autoptr(GList) bdrvs = NULL;
715     GList *iterbdrvs;
716 
717     GLOBAL_STATE_CODE();
718 
719     if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) {
720         return NULL;
721     }
722 
723     iterbdrvs = bdrvs;
724     while (iterbdrvs) {
725         BlockDriverState *bs = iterbdrvs->data;
726         AioContext *ctx = bdrv_get_aio_context(bs);
727         bool found = false;
728 
729         aio_context_acquire(ctx);
730         found = (devices || bdrv_all_snapshots_includes_bs(bs)) &&
731             bdrv_can_snapshot(bs);
732         aio_context_release(ctx);
733 
734         if (vmstate_bs) {
735             if (g_str_equal(vmstate_bs,
736                             bdrv_get_node_name(bs))) {
737                 if (found) {
738                     return bs;
739                 } else {
740                     error_setg(errp,
741                                "vmstate block device '%s' does not support snapshots",
742                                vmstate_bs);
743                     return NULL;
744                 }
745             }
746         } else if (found) {
747             return bs;
748         }
749 
750         iterbdrvs = iterbdrvs->next;
751     }
752 
753     if (vmstate_bs) {
754         error_setg(errp,
755                    "vmstate block device '%s' does not exist", vmstate_bs);
756     } else {
757         error_setg(errp,
758                    "no block device can store vmstate for snapshot");
759     }
760     return NULL;
761 }
762