xref: /openbmc/qemu/block/snapshot.c (revision f0e48cbd)
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     ret = -ENOENT;
61     nb_sns = bdrv_snapshot_list(bs, &sn_tab);
62     if (nb_sns < 0) {
63         return ret;
64     }
65     for (i = 0; i < nb_sns; i++) {
66         sn = &sn_tab[i];
67         if (!strcmp(sn->name, name)) {
68             *sn_info = *sn;
69             ret = 0;
70             break;
71         }
72     }
73     g_free(sn_tab);
74     return ret;
75 }
76 
77 /**
78  * Look up an internal snapshot by @id and @name.
79  * @bs: block device to search
80  * @id: unique snapshot ID, or NULL
81  * @name: snapshot name, or NULL
82  * @sn_info: location to store information on the snapshot found
83  * @errp: location to store error, will be set only for exception
84  *
85  * This function will traverse snapshot list in @bs to search the matching
86  * one, @id and @name are the matching condition:
87  * If both @id and @name are specified, find the first one with id @id and
88  * name @name.
89  * If only @id is specified, find the first one with id @id.
90  * If only @name is specified, find the first one with name @name.
91  * if none is specified, abort().
92  *
93  * Returns: true when a snapshot is found and @sn_info will be filled, false
94  * when error or not found. If all operation succeed but no matching one is
95  * found, @errp will NOT be set.
96  */
97 bool bdrv_snapshot_find_by_id_and_name(BlockDriverState *bs,
98                                        const char *id,
99                                        const char *name,
100                                        QEMUSnapshotInfo *sn_info,
101                                        Error **errp)
102 {
103     QEMUSnapshotInfo *sn_tab, *sn;
104     int nb_sns, i;
105     bool ret = false;
106 
107     assert(id || name);
108 
109     nb_sns = bdrv_snapshot_list(bs, &sn_tab);
110     if (nb_sns < 0) {
111         error_setg_errno(errp, -nb_sns, "Failed to get a snapshot list");
112         return false;
113     } else if (nb_sns == 0) {
114         return false;
115     }
116 
117     if (id && name) {
118         for (i = 0; i < nb_sns; i++) {
119             sn = &sn_tab[i];
120             if (!strcmp(sn->id_str, id) && !strcmp(sn->name, name)) {
121                 *sn_info = *sn;
122                 ret = true;
123                 break;
124             }
125         }
126     } else if (id) {
127         for (i = 0; i < nb_sns; i++) {
128             sn = &sn_tab[i];
129             if (!strcmp(sn->id_str, id)) {
130                 *sn_info = *sn;
131                 ret = true;
132                 break;
133             }
134         }
135     } else if (name) {
136         for (i = 0; i < nb_sns; i++) {
137             sn = &sn_tab[i];
138             if (!strcmp(sn->name, name)) {
139                 *sn_info = *sn;
140                 ret = true;
141                 break;
142             }
143         }
144     }
145 
146     g_free(sn_tab);
147     return ret;
148 }
149 
150 /**
151  * Return a pointer to the child BDS pointer to which we can fall
152  * back if the given BDS does not support snapshots.
153  * Return NULL if there is no BDS to (safely) fall back to.
154  *
155  * We need to return an indirect pointer because bdrv_snapshot_goto()
156  * has to modify the BdrvChild pointer.
157  */
158 static BdrvChild **bdrv_snapshot_fallback_ptr(BlockDriverState *bs)
159 {
160     BdrvChild **fallback;
161     BdrvChild *child;
162 
163     /*
164      * The only BdrvChild pointers that are safe to modify (and which
165      * we can thus return a reference to) are bs->file and
166      * bs->backing.
167      */
168     fallback = &bs->file;
169     if (!*fallback && bs->drv && bs->drv->is_filter) {
170         fallback = &bs->backing;
171     }
172 
173     if (!*fallback) {
174         return NULL;
175     }
176 
177     /*
178      * Check that there are no other children that would need to be
179      * snapshotted.  If there are, it is not safe to fall back to
180      * *fallback.
181      */
182     QLIST_FOREACH(child, &bs->children, next) {
183         if (child->role & (BDRV_CHILD_DATA | BDRV_CHILD_METADATA |
184                            BDRV_CHILD_FILTERED) &&
185             child != *fallback)
186         {
187             return NULL;
188         }
189     }
190 
191     return fallback;
192 }
193 
194 static BlockDriverState *bdrv_snapshot_fallback(BlockDriverState *bs)
195 {
196     BdrvChild **child_ptr = bdrv_snapshot_fallback_ptr(bs);
197     return child_ptr ? (*child_ptr)->bs : NULL;
198 }
199 
200 int bdrv_can_snapshot(BlockDriverState *bs)
201 {
202     BlockDriver *drv = bs->drv;
203     if (!drv || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
204         return 0;
205     }
206 
207     if (!drv->bdrv_snapshot_create) {
208         BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs);
209         if (fallback_bs) {
210             return bdrv_can_snapshot(fallback_bs);
211         }
212         return 0;
213     }
214 
215     return 1;
216 }
217 
218 int bdrv_snapshot_create(BlockDriverState *bs,
219                          QEMUSnapshotInfo *sn_info)
220 {
221     BlockDriver *drv = bs->drv;
222     BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs);
223     if (!drv) {
224         return -ENOMEDIUM;
225     }
226     if (drv->bdrv_snapshot_create) {
227         return drv->bdrv_snapshot_create(bs, sn_info);
228     }
229     if (fallback_bs) {
230         return bdrv_snapshot_create(fallback_bs, sn_info);
231     }
232     return -ENOTSUP;
233 }
234 
235 int bdrv_snapshot_goto(BlockDriverState *bs,
236                        const char *snapshot_id,
237                        Error **errp)
238 {
239     BlockDriver *drv = bs->drv;
240     BdrvChild **fallback_ptr;
241     int ret, open_ret;
242 
243     if (!drv) {
244         error_setg(errp, "Block driver is closed");
245         return -ENOMEDIUM;
246     }
247 
248     if (!QLIST_EMPTY(&bs->dirty_bitmaps)) {
249         error_setg(errp, "Device has active dirty bitmaps");
250         return -EBUSY;
251     }
252 
253     if (drv->bdrv_snapshot_goto) {
254         ret = drv->bdrv_snapshot_goto(bs, snapshot_id);
255         if (ret < 0) {
256             error_setg_errno(errp, -ret, "Failed to load snapshot");
257         }
258         return ret;
259     }
260 
261     fallback_ptr = bdrv_snapshot_fallback_ptr(bs);
262     if (fallback_ptr) {
263         QDict *options;
264         QDict *file_options;
265         Error *local_err = NULL;
266         BlockDriverState *fallback_bs = (*fallback_ptr)->bs;
267         char *subqdict_prefix = g_strdup_printf("%s.", (*fallback_ptr)->name);
268 
269         options = qdict_clone_shallow(bs->options);
270 
271         /* Prevent it from getting deleted when detached from bs */
272         bdrv_ref(fallback_bs);
273 
274         qdict_extract_subqdict(options, &file_options, subqdict_prefix);
275         qobject_unref(file_options);
276         g_free(subqdict_prefix);
277 
278         /* Force .bdrv_open() below to re-attach fallback_bs on *fallback_ptr */
279         qdict_put_str(options, (*fallback_ptr)->name,
280                       bdrv_get_node_name(fallback_bs));
281 
282         /* Now close bs, apply the snapshot on fallback_bs, and re-open bs */
283         if (drv->bdrv_close) {
284             drv->bdrv_close(bs);
285         }
286 
287         /* .bdrv_open() will re-attach it */
288         bdrv_unref_child(bs, *fallback_ptr);
289         *fallback_ptr = NULL;
290 
291         ret = bdrv_snapshot_goto(fallback_bs, snapshot_id, errp);
292         open_ret = drv->bdrv_open(bs, options, bs->open_flags, &local_err);
293         qobject_unref(options);
294         if (open_ret < 0) {
295             bdrv_unref(fallback_bs);
296             bs->drv = NULL;
297             /* A bdrv_snapshot_goto() error takes precedence */
298             error_propagate(errp, local_err);
299             return ret < 0 ? ret : open_ret;
300         }
301 
302         /*
303          * fallback_ptr is &bs->file or &bs->backing.  *fallback_ptr
304          * was closed above and set to NULL, but the .bdrv_open() call
305          * has opened it again, because we set the respective option
306          * (with the qdict_put_str() call above).
307          * Assert that .bdrv_open() has attached some child on
308          * *fallback_ptr, and that it has attached the one we wanted
309          * it to (i.e., fallback_bs).
310          */
311         assert(*fallback_ptr && fallback_bs == (*fallback_ptr)->bs);
312         bdrv_unref(fallback_bs);
313         return ret;
314     }
315 
316     error_setg(errp, "Block driver does not support snapshots");
317     return -ENOTSUP;
318 }
319 
320 /**
321  * Delete an internal snapshot by @snapshot_id and @name.
322  * @bs: block device used in the operation
323  * @snapshot_id: unique snapshot ID, or NULL
324  * @name: snapshot name, or NULL
325  * @errp: location to store error
326  *
327  * If both @snapshot_id and @name are specified, delete the first one with
328  * id @snapshot_id and name @name.
329  * If only @snapshot_id is specified, delete the first one with id
330  * @snapshot_id.
331  * If only @name is specified, delete the first one with name @name.
332  * if none is specified, return -EINVAL.
333  *
334  * Returns: 0 on success, -errno on failure. If @bs is not inserted, return
335  * -ENOMEDIUM. If @snapshot_id and @name are both NULL, return -EINVAL. If @bs
336  * does not support internal snapshot deletion, return -ENOTSUP. If @bs does
337  * not support parameter @snapshot_id or @name, or one of them is not correctly
338  * specified, return -EINVAL. If @bs can't find one matching @id and @name,
339  * return -ENOENT. If @errp != NULL, it will always be filled with error
340  * message on failure.
341  */
342 int bdrv_snapshot_delete(BlockDriverState *bs,
343                          const char *snapshot_id,
344                          const char *name,
345                          Error **errp)
346 {
347     BlockDriver *drv = bs->drv;
348     BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs);
349     int ret;
350 
351     if (!drv) {
352         error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, bdrv_get_device_name(bs));
353         return -ENOMEDIUM;
354     }
355     if (!snapshot_id && !name) {
356         error_setg(errp, "snapshot_id and name are both NULL");
357         return -EINVAL;
358     }
359 
360     /* drain all pending i/o before deleting snapshot */
361     bdrv_drained_begin(bs);
362 
363     if (drv->bdrv_snapshot_delete) {
364         ret = drv->bdrv_snapshot_delete(bs, snapshot_id, name, errp);
365     } else if (fallback_bs) {
366         ret = bdrv_snapshot_delete(fallback_bs, snapshot_id, name, errp);
367     } else {
368         error_setg(errp, "Block format '%s' used by device '%s' "
369                    "does not support internal snapshot deletion",
370                    drv->format_name, bdrv_get_device_name(bs));
371         ret = -ENOTSUP;
372     }
373 
374     bdrv_drained_end(bs);
375     return ret;
376 }
377 
378 int bdrv_snapshot_list(BlockDriverState *bs,
379                        QEMUSnapshotInfo **psn_info)
380 {
381     BlockDriver *drv = bs->drv;
382     BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs);
383     if (!drv) {
384         return -ENOMEDIUM;
385     }
386     if (drv->bdrv_snapshot_list) {
387         return drv->bdrv_snapshot_list(bs, psn_info);
388     }
389     if (fallback_bs) {
390         return bdrv_snapshot_list(fallback_bs, psn_info);
391     }
392     return -ENOTSUP;
393 }
394 
395 /**
396  * Temporarily load an internal snapshot by @snapshot_id and @name.
397  * @bs: block device used in the operation
398  * @snapshot_id: unique snapshot ID, or NULL
399  * @name: snapshot name, or NULL
400  * @errp: location to store error
401  *
402  * If both @snapshot_id and @name are specified, load the first one with
403  * id @snapshot_id and name @name.
404  * If only @snapshot_id is specified, load the first one with id
405  * @snapshot_id.
406  * If only @name is specified, load the first one with name @name.
407  * if none is specified, return -EINVAL.
408  *
409  * Returns: 0 on success, -errno on fail. If @bs is not inserted, return
410  * -ENOMEDIUM. If @bs is not readonly, return -EINVAL. If @bs did not support
411  * internal snapshot, return -ENOTSUP. If qemu can't find a matching @id and
412  * @name, return -ENOENT. If @errp != NULL, it will always be filled on
413  * failure.
414  */
415 int bdrv_snapshot_load_tmp(BlockDriverState *bs,
416                            const char *snapshot_id,
417                            const char *name,
418                            Error **errp)
419 {
420     BlockDriver *drv = bs->drv;
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     ret = bdrv_snapshot_load_tmp(bs, id_or_name, NULL, &local_err);
451     if (ret == -ENOENT || ret == -EINVAL) {
452         error_free(local_err);
453         local_err = NULL;
454         ret = bdrv_snapshot_load_tmp(bs, NULL, id_or_name, &local_err);
455     }
456 
457     error_propagate(errp, local_err);
458 
459     return ret;
460 }
461 
462 
463 static int bdrv_all_get_snapshot_devices(bool has_devices, strList *devices,
464                                          GList **all_bdrvs,
465                                          Error **errp)
466 {
467     g_autoptr(GList) bdrvs = NULL;
468 
469     if (has_devices) {
470         if (!devices) {
471             error_setg(errp, "At least one device is required for snapshot");
472             return -1;
473         }
474 
475         while (devices) {
476             BlockDriverState *bs = bdrv_find_node(devices->value);
477             if (!bs) {
478                 error_setg(errp, "No block device node '%s'", devices->value);
479                 return -1;
480             }
481             bdrvs = g_list_append(bdrvs, bs);
482             devices = devices->next;
483         }
484     } else {
485         BlockDriverState *bs;
486         BdrvNextIterator it;
487         for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
488             bdrvs = g_list_append(bdrvs, bs);
489         }
490     }
491 
492     *all_bdrvs = g_steal_pointer(&bdrvs);
493     return 0;
494 }
495 
496 
497 static bool bdrv_all_snapshots_includes_bs(BlockDriverState *bs)
498 {
499     if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
500         return false;
501     }
502 
503     /* Include all nodes that are either in use by a BlockBackend, or that
504      * aren't attached to any node, but owned by the monitor. */
505     return bdrv_has_blk(bs) || QLIST_EMPTY(&bs->parents);
506 }
507 
508 /* Group operations. All block drivers are involved.
509  * These functions will properly handle dataplane (take aio_context_acquire
510  * when appropriate for appropriate block drivers) */
511 
512 bool bdrv_all_can_snapshot(bool has_devices, strList *devices,
513                            Error **errp)
514 {
515     g_autoptr(GList) bdrvs = NULL;
516     GList *iterbdrvs;
517 
518     if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) {
519         return false;
520     }
521 
522     iterbdrvs = bdrvs;
523     while (iterbdrvs) {
524         BlockDriverState *bs = iterbdrvs->data;
525         AioContext *ctx = bdrv_get_aio_context(bs);
526         bool ok = true;
527 
528         aio_context_acquire(ctx);
529         if (devices || bdrv_all_snapshots_includes_bs(bs)) {
530             ok = bdrv_can_snapshot(bs);
531         }
532         aio_context_release(ctx);
533         if (!ok) {
534             error_setg(errp, "Device '%s' is writable but does not support "
535                        "snapshots", bdrv_get_device_or_node_name(bs));
536             return false;
537         }
538 
539         iterbdrvs = iterbdrvs->next;
540     }
541 
542     return true;
543 }
544 
545 int bdrv_all_delete_snapshot(const char *name,
546                              bool has_devices, strList *devices,
547                              Error **errp)
548 {
549     g_autoptr(GList) bdrvs = NULL;
550     GList *iterbdrvs;
551 
552     if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) {
553         return -1;
554     }
555 
556     iterbdrvs = bdrvs;
557     while (iterbdrvs) {
558         BlockDriverState *bs = iterbdrvs->data;
559         AioContext *ctx = bdrv_get_aio_context(bs);
560         QEMUSnapshotInfo sn1, *snapshot = &sn1;
561         int ret = 0;
562 
563         aio_context_acquire(ctx);
564         if ((devices || bdrv_all_snapshots_includes_bs(bs)) &&
565             bdrv_snapshot_find(bs, snapshot, name) >= 0)
566         {
567             ret = bdrv_snapshot_delete(bs, snapshot->id_str,
568                                        snapshot->name, errp);
569         }
570         aio_context_release(ctx);
571         if (ret < 0) {
572             error_prepend(errp, "Could not delete snapshot '%s' on '%s': ",
573                           name, bdrv_get_device_or_node_name(bs));
574             return -1;
575         }
576 
577         iterbdrvs = iterbdrvs->next;
578     }
579 
580     return 0;
581 }
582 
583 
584 int bdrv_all_goto_snapshot(const char *name,
585                            bool has_devices, strList *devices,
586                            Error **errp)
587 {
588     g_autoptr(GList) bdrvs = NULL;
589     GList *iterbdrvs;
590 
591     if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) {
592         return -1;
593     }
594 
595     iterbdrvs = bdrvs;
596     while (iterbdrvs) {
597         BlockDriverState *bs = iterbdrvs->data;
598         AioContext *ctx = bdrv_get_aio_context(bs);
599         int ret = 0;
600 
601         aio_context_acquire(ctx);
602         if (devices || bdrv_all_snapshots_includes_bs(bs)) {
603             ret = bdrv_snapshot_goto(bs, name, errp);
604         }
605         aio_context_release(ctx);
606         if (ret < 0) {
607             error_prepend(errp, "Could not load snapshot '%s' on '%s': ",
608                           name, bdrv_get_device_or_node_name(bs));
609             return -1;
610         }
611 
612         iterbdrvs = iterbdrvs->next;
613     }
614 
615     return 0;
616 }
617 
618 int bdrv_all_has_snapshot(const char *name,
619                           bool has_devices, strList *devices,
620                           Error **errp)
621 {
622     g_autoptr(GList) bdrvs = NULL;
623     GList *iterbdrvs;
624 
625     if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) {
626         return -1;
627     }
628 
629     iterbdrvs = bdrvs;
630     while (iterbdrvs) {
631         BlockDriverState *bs = iterbdrvs->data;
632         AioContext *ctx = bdrv_get_aio_context(bs);
633         QEMUSnapshotInfo sn;
634         int ret = 0;
635 
636         aio_context_acquire(ctx);
637         if (devices || bdrv_all_snapshots_includes_bs(bs)) {
638             ret = bdrv_snapshot_find(bs, &sn, name);
639         }
640         aio_context_release(ctx);
641         if (ret < 0) {
642             if (ret == -ENOENT) {
643                 return 0;
644             } else {
645                 error_setg_errno(errp, errno,
646                                  "Could not check snapshot '%s' on '%s'",
647                                  name, bdrv_get_device_or_node_name(bs));
648                 return -1;
649             }
650         }
651 
652         iterbdrvs = iterbdrvs->next;
653     }
654 
655     return 1;
656 }
657 
658 int bdrv_all_create_snapshot(QEMUSnapshotInfo *sn,
659                              BlockDriverState *vm_state_bs,
660                              uint64_t vm_state_size,
661                              bool has_devices, strList *devices,
662                              Error **errp)
663 {
664     g_autoptr(GList) bdrvs = NULL;
665     GList *iterbdrvs;
666 
667     if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) {
668         return -1;
669     }
670 
671     iterbdrvs = bdrvs;
672     while (iterbdrvs) {
673         BlockDriverState *bs = iterbdrvs->data;
674         AioContext *ctx = bdrv_get_aio_context(bs);
675         int ret = 0;
676 
677         aio_context_acquire(ctx);
678         if (bs == vm_state_bs) {
679             sn->vm_state_size = vm_state_size;
680             ret = bdrv_snapshot_create(bs, sn);
681         } else if (devices || bdrv_all_snapshots_includes_bs(bs)) {
682             sn->vm_state_size = 0;
683             ret = bdrv_snapshot_create(bs, sn);
684         }
685         aio_context_release(ctx);
686         if (ret < 0) {
687             error_setg(errp, "Could not create snapshot '%s' on '%s'",
688                        sn->name, bdrv_get_device_or_node_name(bs));
689             return -1;
690         }
691 
692         iterbdrvs = iterbdrvs->next;
693     }
694 
695     return 0;
696 }
697 
698 
699 BlockDriverState *bdrv_all_find_vmstate_bs(const char *vmstate_bs,
700                                            bool has_devices, strList *devices,
701                                            Error **errp)
702 {
703     g_autoptr(GList) bdrvs = NULL;
704     GList *iterbdrvs;
705 
706     if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) {
707         return NULL;
708     }
709 
710     iterbdrvs = bdrvs;
711     while (iterbdrvs) {
712         BlockDriverState *bs = iterbdrvs->data;
713         AioContext *ctx = bdrv_get_aio_context(bs);
714         bool found = false;
715 
716         aio_context_acquire(ctx);
717         found = (devices || bdrv_all_snapshots_includes_bs(bs)) &&
718             bdrv_can_snapshot(bs);
719         aio_context_release(ctx);
720 
721         if (vmstate_bs) {
722             if (g_str_equal(vmstate_bs,
723                             bdrv_get_node_name(bs))) {
724                 if (found) {
725                     return bs;
726                 } else {
727                     error_setg(errp,
728                                "vmstate block device '%s' does not support snapshots",
729                                vmstate_bs);
730                     return NULL;
731                 }
732             }
733         } else if (found) {
734             return bs;
735         }
736 
737         iterbdrvs = iterbdrvs->next;
738     }
739 
740     if (vmstate_bs) {
741         error_setg(errp,
742                    "vmstate block device '%s' does not exist", vmstate_bs);
743     } else {
744         error_setg(errp,
745                    "no block device can store vmstate for snapshot");
746     }
747     return NULL;
748 }
749