]> git.proxmox.com Git - mirror_qemu.git/blob - block/snapshot.c
block/snapshot: stress that we fallback to primary child
[mirror_qemu.git] / block / snapshot.c
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 the child BDS pointer 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 * We need to return an indirect pointer because bdrv_snapshot_goto()
159 * has to modify the BdrvChild pointer.
160 */
161 static BdrvChild **bdrv_snapshot_fallback_ptr(BlockDriverState *bs)
162 {
163 BdrvChild **fallback;
164 BdrvChild *child = bdrv_primary_child(bs);
165
166 /* We allow fallback only to primary child */
167 if (!child) {
168 return NULL;
169 }
170 fallback = (child == bs->file ? &bs->file : &bs->backing);
171 assert(*fallback == child);
172
173 /*
174 * Check that there are no other children that would need to be
175 * snapshotted. If there are, it is not safe to fall back to
176 * *fallback.
177 */
178 QLIST_FOREACH(child, &bs->children, next) {
179 if (child->role & (BDRV_CHILD_DATA | BDRV_CHILD_METADATA |
180 BDRV_CHILD_FILTERED) &&
181 child != *fallback)
182 {
183 return NULL;
184 }
185 }
186
187 return fallback;
188 }
189
190 static BlockDriverState *bdrv_snapshot_fallback(BlockDriverState *bs)
191 {
192 BdrvChild **child_ptr = bdrv_snapshot_fallback_ptr(bs);
193 return child_ptr ? (*child_ptr)->bs : NULL;
194 }
195
196 int bdrv_can_snapshot(BlockDriverState *bs)
197 {
198 BlockDriver *drv = bs->drv;
199 GLOBAL_STATE_CODE();
200 if (!drv || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
201 return 0;
202 }
203
204 if (!drv->bdrv_snapshot_create) {
205 BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs);
206 if (fallback_bs) {
207 return bdrv_can_snapshot(fallback_bs);
208 }
209 return 0;
210 }
211
212 return 1;
213 }
214
215 int bdrv_snapshot_create(BlockDriverState *bs,
216 QEMUSnapshotInfo *sn_info)
217 {
218 BlockDriver *drv = bs->drv;
219 BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs);
220
221 GLOBAL_STATE_CODE();
222
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 GLOBAL_STATE_CODE();
244
245 if (!drv) {
246 error_setg(errp, "Block driver is closed");
247 return -ENOMEDIUM;
248 }
249
250 if (!QLIST_EMPTY(&bs->dirty_bitmaps)) {
251 error_setg(errp, "Device has active dirty bitmaps");
252 return -EBUSY;
253 }
254
255 if (drv->bdrv_snapshot_goto) {
256 ret = drv->bdrv_snapshot_goto(bs, snapshot_id);
257 if (ret < 0) {
258 error_setg_errno(errp, -ret, "Failed to load snapshot");
259 }
260 return ret;
261 }
262
263 fallback_ptr = bdrv_snapshot_fallback_ptr(bs);
264 if (fallback_ptr) {
265 QDict *options;
266 QDict *file_options;
267 Error *local_err = NULL;
268 BlockDriverState *fallback_bs = (*fallback_ptr)->bs;
269 char *subqdict_prefix = g_strdup_printf("%s.", (*fallback_ptr)->name);
270
271 options = qdict_clone_shallow(bs->options);
272
273 /* Prevent it from getting deleted when detached from bs */
274 bdrv_ref(fallback_bs);
275
276 qdict_extract_subqdict(options, &file_options, subqdict_prefix);
277 qobject_unref(file_options);
278 g_free(subqdict_prefix);
279
280 /* Force .bdrv_open() below to re-attach fallback_bs on *fallback_ptr */
281 qdict_put_str(options, (*fallback_ptr)->name,
282 bdrv_get_node_name(fallback_bs));
283
284 /* Now close bs, apply the snapshot on fallback_bs, and re-open bs */
285 if (drv->bdrv_close) {
286 drv->bdrv_close(bs);
287 }
288
289 /* .bdrv_open() will re-attach it */
290 bdrv_unref_child(bs, *fallback_ptr);
291 *fallback_ptr = NULL;
292
293 ret = bdrv_snapshot_goto(fallback_bs, snapshot_id, errp);
294 open_ret = drv->bdrv_open(bs, options, bs->open_flags, &local_err);
295 qobject_unref(options);
296 if (open_ret < 0) {
297 bdrv_unref(fallback_bs);
298 bs->drv = NULL;
299 /* A bdrv_snapshot_goto() error takes precedence */
300 error_propagate(errp, local_err);
301 return ret < 0 ? ret : open_ret;
302 }
303
304 /*
305 * fallback was a primary child. It was closed above and set to NULL,
306 * but the .bdrv_open() call has opened it again, because we set the
307 * respective option (with the qdict_put_str() call above).
308 * Assert that .bdrv_open() has attached the right BDS as primary child.
309 */
310 assert(bdrv_primary_bs(bs) == fallback_bs);
311 bdrv_unref(fallback_bs);
312 return ret;
313 }
314
315 error_setg(errp, "Block driver does not support snapshots");
316 return -ENOTSUP;
317 }
318
319 /**
320 * Delete an internal snapshot by @snapshot_id and @name.
321 * @bs: block device used in the operation
322 * @snapshot_id: unique snapshot ID, or NULL
323 * @name: snapshot name, or NULL
324 * @errp: location to store error
325 *
326 * If both @snapshot_id and @name are specified, delete the first one with
327 * id @snapshot_id and name @name.
328 * If only @snapshot_id is specified, delete the first one with id
329 * @snapshot_id.
330 * If only @name is specified, delete the first one with name @name.
331 * if none is specified, return -EINVAL.
332 *
333 * Returns: 0 on success, -errno on failure. If @bs is not inserted, return
334 * -ENOMEDIUM. If @snapshot_id and @name are both NULL, return -EINVAL. If @bs
335 * does not support internal snapshot deletion, return -ENOTSUP. If @bs does
336 * not support parameter @snapshot_id or @name, or one of them is not correctly
337 * specified, return -EINVAL. If @bs can't find one matching @id and @name,
338 * return -ENOENT. If @errp != NULL, it will always be filled with error
339 * message on failure.
340 */
341 int bdrv_snapshot_delete(BlockDriverState *bs,
342 const char *snapshot_id,
343 const char *name,
344 Error **errp)
345 {
346 BlockDriver *drv = bs->drv;
347 BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs);
348 int ret;
349
350 GLOBAL_STATE_CODE();
351
352 if (!drv) {
353 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, bdrv_get_device_name(bs));
354 return -ENOMEDIUM;
355 }
356 if (!snapshot_id && !name) {
357 error_setg(errp, "snapshot_id and name are both NULL");
358 return -EINVAL;
359 }
360
361 /* drain all pending i/o before deleting snapshot */
362 bdrv_drained_begin(bs);
363
364 if (drv->bdrv_snapshot_delete) {
365 ret = drv->bdrv_snapshot_delete(bs, snapshot_id, name, errp);
366 } else if (fallback_bs) {
367 ret = bdrv_snapshot_delete(fallback_bs, snapshot_id, name, errp);
368 } else {
369 error_setg(errp, "Block format '%s' used by device '%s' "
370 "does not support internal snapshot deletion",
371 drv->format_name, bdrv_get_device_name(bs));
372 ret = -ENOTSUP;
373 }
374
375 bdrv_drained_end(bs);
376 return ret;
377 }
378
379 int bdrv_snapshot_list(BlockDriverState *bs,
380 QEMUSnapshotInfo **psn_info)
381 {
382 BlockDriver *drv = bs->drv;
383 BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs);
384
385 GLOBAL_STATE_CODE();
386 if (!drv) {
387 return -ENOMEDIUM;
388 }
389 if (drv->bdrv_snapshot_list) {
390 return drv->bdrv_snapshot_list(bs, psn_info);
391 }
392 if (fallback_bs) {
393 return bdrv_snapshot_list(fallback_bs, psn_info);
394 }
395 return -ENOTSUP;
396 }
397
398 /**
399 * Temporarily load an internal snapshot by @snapshot_id and @name.
400 * @bs: block device used in the operation
401 * @snapshot_id: unique snapshot ID, or NULL
402 * @name: snapshot name, or NULL
403 * @errp: location to store error
404 *
405 * If both @snapshot_id and @name are specified, load the first one with
406 * id @snapshot_id and name @name.
407 * If only @snapshot_id is specified, load the first one with id
408 * @snapshot_id.
409 * If only @name is specified, load the first one with name @name.
410 * if none is specified, return -EINVAL.
411 *
412 * Returns: 0 on success, -errno on fail. If @bs is not inserted, return
413 * -ENOMEDIUM. If @bs is not readonly, return -EINVAL. If @bs did not support
414 * internal snapshot, return -ENOTSUP. If qemu can't find a matching @id and
415 * @name, return -ENOENT. If @errp != NULL, it will always be filled on
416 * failure.
417 */
418 int bdrv_snapshot_load_tmp(BlockDriverState *bs,
419 const char *snapshot_id,
420 const char *name,
421 Error **errp)
422 {
423 BlockDriver *drv = bs->drv;
424
425 GLOBAL_STATE_CODE();
426
427 if (!drv) {
428 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, bdrv_get_device_name(bs));
429 return -ENOMEDIUM;
430 }
431 if (!snapshot_id && !name) {
432 error_setg(errp, "snapshot_id and name are both NULL");
433 return -EINVAL;
434 }
435 if (!bdrv_is_read_only(bs)) {
436 error_setg(errp, "Device is not readonly");
437 return -EINVAL;
438 }
439 if (drv->bdrv_snapshot_load_tmp) {
440 return drv->bdrv_snapshot_load_tmp(bs, snapshot_id, name, errp);
441 }
442 error_setg(errp, "Block format '%s' used by device '%s' "
443 "does not support temporarily loading internal snapshots",
444 drv->format_name, bdrv_get_device_name(bs));
445 return -ENOTSUP;
446 }
447
448 int bdrv_snapshot_load_tmp_by_id_or_name(BlockDriverState *bs,
449 const char *id_or_name,
450 Error **errp)
451 {
452 int ret;
453 Error *local_err = NULL;
454
455 GLOBAL_STATE_CODE();
456
457 ret = bdrv_snapshot_load_tmp(bs, id_or_name, NULL, &local_err);
458 if (ret == -ENOENT || ret == -EINVAL) {
459 error_free(local_err);
460 local_err = NULL;
461 ret = bdrv_snapshot_load_tmp(bs, NULL, id_or_name, &local_err);
462 }
463
464 error_propagate(errp, local_err);
465
466 return ret;
467 }
468
469
470 static int bdrv_all_get_snapshot_devices(bool has_devices, strList *devices,
471 GList **all_bdrvs,
472 Error **errp)
473 {
474 g_autoptr(GList) bdrvs = NULL;
475
476 if (has_devices) {
477 if (!devices) {
478 error_setg(errp, "At least one device is required for snapshot");
479 return -1;
480 }
481
482 while (devices) {
483 BlockDriverState *bs = bdrv_find_node(devices->value);
484 if (!bs) {
485 error_setg(errp, "No block device node '%s'", devices->value);
486 return -1;
487 }
488 bdrvs = g_list_append(bdrvs, bs);
489 devices = devices->next;
490 }
491 } else {
492 BlockDriverState *bs;
493 BdrvNextIterator it;
494 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
495 bdrvs = g_list_append(bdrvs, bs);
496 }
497 }
498
499 *all_bdrvs = g_steal_pointer(&bdrvs);
500 return 0;
501 }
502
503
504 static bool bdrv_all_snapshots_includes_bs(BlockDriverState *bs)
505 {
506 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
507 return false;
508 }
509
510 /* Include all nodes that are either in use by a BlockBackend, or that
511 * aren't attached to any node, but owned by the monitor. */
512 return bdrv_has_blk(bs) || QLIST_EMPTY(&bs->parents);
513 }
514
515 /* Group operations. All block drivers are involved.
516 * These functions will properly handle dataplane (take aio_context_acquire
517 * when appropriate for appropriate block drivers) */
518
519 bool bdrv_all_can_snapshot(bool has_devices, strList *devices,
520 Error **errp)
521 {
522 g_autoptr(GList) bdrvs = NULL;
523 GList *iterbdrvs;
524
525 GLOBAL_STATE_CODE();
526
527 if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) {
528 return false;
529 }
530
531 iterbdrvs = bdrvs;
532 while (iterbdrvs) {
533 BlockDriverState *bs = iterbdrvs->data;
534 AioContext *ctx = bdrv_get_aio_context(bs);
535 bool ok = true;
536
537 aio_context_acquire(ctx);
538 if (devices || bdrv_all_snapshots_includes_bs(bs)) {
539 ok = bdrv_can_snapshot(bs);
540 }
541 aio_context_release(ctx);
542 if (!ok) {
543 error_setg(errp, "Device '%s' is writable but does not support "
544 "snapshots", bdrv_get_device_or_node_name(bs));
545 return false;
546 }
547
548 iterbdrvs = iterbdrvs->next;
549 }
550
551 return true;
552 }
553
554 int bdrv_all_delete_snapshot(const char *name,
555 bool has_devices, strList *devices,
556 Error **errp)
557 {
558 g_autoptr(GList) bdrvs = NULL;
559 GList *iterbdrvs;
560
561 GLOBAL_STATE_CODE();
562
563 if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) {
564 return -1;
565 }
566
567 iterbdrvs = bdrvs;
568 while (iterbdrvs) {
569 BlockDriverState *bs = iterbdrvs->data;
570 AioContext *ctx = bdrv_get_aio_context(bs);
571 QEMUSnapshotInfo sn1, *snapshot = &sn1;
572 int ret = 0;
573
574 aio_context_acquire(ctx);
575 if ((devices || bdrv_all_snapshots_includes_bs(bs)) &&
576 bdrv_snapshot_find(bs, snapshot, name) >= 0)
577 {
578 ret = bdrv_snapshot_delete(bs, snapshot->id_str,
579 snapshot->name, errp);
580 }
581 aio_context_release(ctx);
582 if (ret < 0) {
583 error_prepend(errp, "Could not delete snapshot '%s' on '%s': ",
584 name, bdrv_get_device_or_node_name(bs));
585 return -1;
586 }
587
588 iterbdrvs = iterbdrvs->next;
589 }
590
591 return 0;
592 }
593
594
595 int bdrv_all_goto_snapshot(const char *name,
596 bool has_devices, strList *devices,
597 Error **errp)
598 {
599 g_autoptr(GList) bdrvs = NULL;
600 GList *iterbdrvs;
601
602 GLOBAL_STATE_CODE();
603
604 if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) {
605 return -1;
606 }
607
608 iterbdrvs = bdrvs;
609 while (iterbdrvs) {
610 BlockDriverState *bs = iterbdrvs->data;
611 AioContext *ctx = bdrv_get_aio_context(bs);
612 int ret = 0;
613
614 aio_context_acquire(ctx);
615 if (devices || bdrv_all_snapshots_includes_bs(bs)) {
616 ret = bdrv_snapshot_goto(bs, name, errp);
617 }
618 aio_context_release(ctx);
619 if (ret < 0) {
620 error_prepend(errp, "Could not load snapshot '%s' on '%s': ",
621 name, bdrv_get_device_or_node_name(bs));
622 return -1;
623 }
624
625 iterbdrvs = iterbdrvs->next;
626 }
627
628 return 0;
629 }
630
631 int bdrv_all_has_snapshot(const char *name,
632 bool has_devices, strList *devices,
633 Error **errp)
634 {
635 g_autoptr(GList) bdrvs = NULL;
636 GList *iterbdrvs;
637
638 GLOBAL_STATE_CODE();
639
640 if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) {
641 return -1;
642 }
643
644 iterbdrvs = bdrvs;
645 while (iterbdrvs) {
646 BlockDriverState *bs = iterbdrvs->data;
647 AioContext *ctx = bdrv_get_aio_context(bs);
648 QEMUSnapshotInfo sn;
649 int ret = 0;
650
651 aio_context_acquire(ctx);
652 if (devices || bdrv_all_snapshots_includes_bs(bs)) {
653 ret = bdrv_snapshot_find(bs, &sn, name);
654 }
655 aio_context_release(ctx);
656 if (ret < 0) {
657 if (ret == -ENOENT) {
658 return 0;
659 } else {
660 error_setg_errno(errp, errno,
661 "Could not check snapshot '%s' on '%s'",
662 name, bdrv_get_device_or_node_name(bs));
663 return -1;
664 }
665 }
666
667 iterbdrvs = iterbdrvs->next;
668 }
669
670 return 1;
671 }
672
673 int bdrv_all_create_snapshot(QEMUSnapshotInfo *sn,
674 BlockDriverState *vm_state_bs,
675 uint64_t vm_state_size,
676 bool has_devices, strList *devices,
677 Error **errp)
678 {
679 g_autoptr(GList) bdrvs = NULL;
680 GList *iterbdrvs;
681 GLOBAL_STATE_CODE();
682
683 if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) {
684 return -1;
685 }
686
687 iterbdrvs = bdrvs;
688 while (iterbdrvs) {
689 BlockDriverState *bs = iterbdrvs->data;
690 AioContext *ctx = bdrv_get_aio_context(bs);
691 int ret = 0;
692
693 aio_context_acquire(ctx);
694 if (bs == vm_state_bs) {
695 sn->vm_state_size = vm_state_size;
696 ret = bdrv_snapshot_create(bs, sn);
697 } else if (devices || bdrv_all_snapshots_includes_bs(bs)) {
698 sn->vm_state_size = 0;
699 ret = bdrv_snapshot_create(bs, sn);
700 }
701 aio_context_release(ctx);
702 if (ret < 0) {
703 error_setg(errp, "Could not create snapshot '%s' on '%s'",
704 sn->name, bdrv_get_device_or_node_name(bs));
705 return -1;
706 }
707
708 iterbdrvs = iterbdrvs->next;
709 }
710
711 return 0;
712 }
713
714
715 BlockDriverState *bdrv_all_find_vmstate_bs(const char *vmstate_bs,
716 bool has_devices, strList *devices,
717 Error **errp)
718 {
719 g_autoptr(GList) bdrvs = NULL;
720 GList *iterbdrvs;
721
722 GLOBAL_STATE_CODE();
723
724 if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) {
725 return NULL;
726 }
727
728 iterbdrvs = bdrvs;
729 while (iterbdrvs) {
730 BlockDriverState *bs = iterbdrvs->data;
731 AioContext *ctx = bdrv_get_aio_context(bs);
732 bool found = false;
733
734 aio_context_acquire(ctx);
735 found = (devices || bdrv_all_snapshots_includes_bs(bs)) &&
736 bdrv_can_snapshot(bs);
737 aio_context_release(ctx);
738
739 if (vmstate_bs) {
740 if (g_str_equal(vmstate_bs,
741 bdrv_get_node_name(bs))) {
742 if (found) {
743 return bs;
744 } else {
745 error_setg(errp,
746 "vmstate block device '%s' does not support snapshots",
747 vmstate_bs);
748 return NULL;
749 }
750 }
751 } else if (found) {
752 return bs;
753 }
754
755 iterbdrvs = iterbdrvs->next;
756 }
757
758 if (vmstate_bs) {
759 error_setg(errp,
760 "vmstate block device '%s' does not exist", vmstate_bs);
761 } else {
762 error_setg(errp,
763 "no block device can store vmstate for snapshot");
764 }
765 return NULL;
766 }