1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
36QemuOptsList 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
50 }
51 },
52};
53
54int 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99bool 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
155
156
157
158static BdrvChild *bdrv_snapshot_fallback_child(BlockDriverState *bs)
159{
160 BdrvChild *fallback = bdrv_primary_child(bs);
161 BdrvChild *child;
162
163
164 if (!fallback) {
165 return NULL;
166 }
167
168
169
170
171
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
185static BlockDriverState *bdrv_snapshot_fallback(BlockDriverState *bs)
186{
187 return child_bs(bdrv_snapshot_fallback_child(bs));
188}
189
190int 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
209int 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
229int 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
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
275 qdict_put_str(options, fallback->name,
276 bdrv_get_node_name(fallback_bs));
277
278
279 if (drv->bdrv_close) {
280 drv->bdrv_close(bs);
281 }
282
283
284 bdrv_unref_child(bs, fallback);
285
286 ret = bdrv_snapshot_goto(fallback_bs, snapshot_id, errp);
287 open_ret = drv->bdrv_open(bs, options, bs->open_flags, &local_err);
288 qobject_unref(options);
289 if (open_ret < 0) {
290 bdrv_unref(fallback_bs);
291 bs->drv = NULL;
292
293 error_propagate(errp, local_err);
294 return ret < 0 ? ret : open_ret;
295 }
296
297
298
299
300
301
302
303 assert(bdrv_primary_bs(bs) == fallback_bs);
304 bdrv_unref(fallback_bs);
305 return ret;
306 }
307
308 error_setg(errp, "Block driver does not support snapshots");
309 return -ENOTSUP;
310}
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334int bdrv_snapshot_delete(BlockDriverState *bs,
335 const char *snapshot_id,
336 const char *name,
337 Error **errp)
338{
339 BlockDriver *drv = bs->drv;
340 BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs);
341 int ret;
342
343 GLOBAL_STATE_CODE();
344
345 if (!drv) {
346 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, bdrv_get_device_name(bs));
347 return -ENOMEDIUM;
348 }
349 if (!snapshot_id && !name) {
350 error_setg(errp, "snapshot_id and name are both NULL");
351 return -EINVAL;
352 }
353
354
355 bdrv_drained_begin(bs);
356
357 if (drv->bdrv_snapshot_delete) {
358 ret = drv->bdrv_snapshot_delete(bs, snapshot_id, name, errp);
359 } else if (fallback_bs) {
360 ret = bdrv_snapshot_delete(fallback_bs, snapshot_id, name, errp);
361 } else {
362 error_setg(errp, "Block format '%s' used by device '%s' "
363 "does not support internal snapshot deletion",
364 drv->format_name, bdrv_get_device_name(bs));
365 ret = -ENOTSUP;
366 }
367
368 bdrv_drained_end(bs);
369 return ret;
370}
371
372int bdrv_snapshot_list(BlockDriverState *bs,
373 QEMUSnapshotInfo **psn_info)
374{
375 BlockDriver *drv = bs->drv;
376 BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs);
377
378 GLOBAL_STATE_CODE();
379 if (!drv) {
380 return -ENOMEDIUM;
381 }
382 if (drv->bdrv_snapshot_list) {
383 return drv->bdrv_snapshot_list(bs, psn_info);
384 }
385 if (fallback_bs) {
386 return bdrv_snapshot_list(fallback_bs, psn_info);
387 }
388 return -ENOTSUP;
389}
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411int bdrv_snapshot_load_tmp(BlockDriverState *bs,
412 const char *snapshot_id,
413 const char *name,
414 Error **errp)
415{
416 BlockDriver *drv = bs->drv;
417
418 GLOBAL_STATE_CODE();
419
420 if (!drv) {
421 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, bdrv_get_device_name(bs));
422 return -ENOMEDIUM;
423 }
424 if (!snapshot_id && !name) {
425 error_setg(errp, "snapshot_id and name are both NULL");
426 return -EINVAL;
427 }
428 if (!bdrv_is_read_only(bs)) {
429 error_setg(errp, "Device is not readonly");
430 return -EINVAL;
431 }
432 if (drv->bdrv_snapshot_load_tmp) {
433 return drv->bdrv_snapshot_load_tmp(bs, snapshot_id, name, errp);
434 }
435 error_setg(errp, "Block format '%s' used by device '%s' "
436 "does not support temporarily loading internal snapshots",
437 drv->format_name, bdrv_get_device_name(bs));
438 return -ENOTSUP;
439}
440
441int bdrv_snapshot_load_tmp_by_id_or_name(BlockDriverState *bs,
442 const char *id_or_name,
443 Error **errp)
444{
445 int ret;
446 Error *local_err = NULL;
447
448 GLOBAL_STATE_CODE();
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
463static 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
497static 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
504
505 return bdrv_has_blk(bs) || QLIST_EMPTY(&bs->parents);
506}
507
508
509
510
511
512bool bdrv_all_can_snapshot(bool has_devices, strList *devices,
513 Error **errp)
514{
515 g_autoptr(GList) bdrvs = NULL;
516 GList *iterbdrvs;
517
518 GLOBAL_STATE_CODE();
519
520 if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) {
521 return false;
522 }
523
524 iterbdrvs = bdrvs;
525 while (iterbdrvs) {
526 BlockDriverState *bs = iterbdrvs->data;
527 AioContext *ctx = bdrv_get_aio_context(bs);
528 bool ok = true;
529
530 aio_context_acquire(ctx);
531 if (devices || bdrv_all_snapshots_includes_bs(bs)) {
532 ok = bdrv_can_snapshot(bs);
533 }
534 aio_context_release(ctx);
535 if (!ok) {
536 error_setg(errp, "Device '%s' is writable but does not support "
537 "snapshots", bdrv_get_device_or_node_name(bs));
538 return false;
539 }
540
541 iterbdrvs = iterbdrvs->next;
542 }
543
544 return true;
545}
546
547int bdrv_all_delete_snapshot(const char *name,
548 bool has_devices, strList *devices,
549 Error **errp)
550{
551 g_autoptr(GList) bdrvs = NULL;
552 GList *iterbdrvs;
553
554 GLOBAL_STATE_CODE();
555
556 if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) {
557 return -1;
558 }
559
560 iterbdrvs = bdrvs;
561 while (iterbdrvs) {
562 BlockDriverState *bs = iterbdrvs->data;
563 AioContext *ctx = bdrv_get_aio_context(bs);
564 QEMUSnapshotInfo sn1, *snapshot = &sn1;
565 int ret = 0;
566
567 aio_context_acquire(ctx);
568 if ((devices || bdrv_all_snapshots_includes_bs(bs)) &&
569 bdrv_snapshot_find(bs, snapshot, name) >= 0)
570 {
571 ret = bdrv_snapshot_delete(bs, snapshot->id_str,
572 snapshot->name, errp);
573 }
574 aio_context_release(ctx);
575 if (ret < 0) {
576 error_prepend(errp, "Could not delete snapshot '%s' on '%s': ",
577 name, bdrv_get_device_or_node_name(bs));
578 return -1;
579 }
580
581 iterbdrvs = iterbdrvs->next;
582 }
583
584 return 0;
585}
586
587
588int bdrv_all_goto_snapshot(const char *name,
589 bool has_devices, strList *devices,
590 Error **errp)
591{
592 g_autoptr(GList) bdrvs = NULL;
593 GList *iterbdrvs;
594
595 GLOBAL_STATE_CODE();
596
597 if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) {
598 return -1;
599 }
600
601 iterbdrvs = bdrvs;
602 while (iterbdrvs) {
603 BlockDriverState *bs = iterbdrvs->data;
604 AioContext *ctx = bdrv_get_aio_context(bs);
605 int ret = 0;
606
607 aio_context_acquire(ctx);
608 if (devices || bdrv_all_snapshots_includes_bs(bs)) {
609 ret = bdrv_snapshot_goto(bs, name, errp);
610 }
611 aio_context_release(ctx);
612 if (ret < 0) {
613 error_prepend(errp, "Could not load snapshot '%s' on '%s': ",
614 name, bdrv_get_device_or_node_name(bs));
615 return -1;
616 }
617
618 iterbdrvs = iterbdrvs->next;
619 }
620
621 return 0;
622}
623
624int bdrv_all_has_snapshot(const char *name,
625 bool has_devices, strList *devices,
626 Error **errp)
627{
628 g_autoptr(GList) bdrvs = NULL;
629 GList *iterbdrvs;
630
631 GLOBAL_STATE_CODE();
632
633 if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) {
634 return -1;
635 }
636
637 iterbdrvs = bdrvs;
638 while (iterbdrvs) {
639 BlockDriverState *bs = iterbdrvs->data;
640 AioContext *ctx = bdrv_get_aio_context(bs);
641 QEMUSnapshotInfo sn;
642 int ret = 0;
643
644 aio_context_acquire(ctx);
645 if (devices || bdrv_all_snapshots_includes_bs(bs)) {
646 ret = bdrv_snapshot_find(bs, &sn, name);
647 }
648 aio_context_release(ctx);
649 if (ret < 0) {
650 if (ret == -ENOENT) {
651 return 0;
652 } else {
653 error_setg_errno(errp, errno,
654 "Could not check snapshot '%s' on '%s'",
655 name, bdrv_get_device_or_node_name(bs));
656 return -1;
657 }
658 }
659
660 iterbdrvs = iterbdrvs->next;
661 }
662
663 return 1;
664}
665
666int bdrv_all_create_snapshot(QEMUSnapshotInfo *sn,
667 BlockDriverState *vm_state_bs,
668 uint64_t vm_state_size,
669 bool has_devices, strList *devices,
670 Error **errp)
671{
672 g_autoptr(GList) bdrvs = NULL;
673 GList *iterbdrvs;
674 GLOBAL_STATE_CODE();
675
676 if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) {
677 return -1;
678 }
679
680 iterbdrvs = bdrvs;
681 while (iterbdrvs) {
682 BlockDriverState *bs = iterbdrvs->data;
683 AioContext *ctx = bdrv_get_aio_context(bs);
684 int ret = 0;
685
686 aio_context_acquire(ctx);
687 if (bs == vm_state_bs) {
688 sn->vm_state_size = vm_state_size;
689 ret = bdrv_snapshot_create(bs, sn);
690 } else if (devices || bdrv_all_snapshots_includes_bs(bs)) {
691 sn->vm_state_size = 0;
692 ret = bdrv_snapshot_create(bs, sn);
693 }
694 aio_context_release(ctx);
695 if (ret < 0) {
696 error_setg(errp, "Could not create snapshot '%s' on '%s'",
697 sn->name, bdrv_get_device_or_node_name(bs));
698 return -1;
699 }
700
701 iterbdrvs = iterbdrvs->next;
702 }
703
704 return 0;
705}
706
707
708BlockDriverState *bdrv_all_find_vmstate_bs(const char *vmstate_bs,
709 bool has_devices, strList *devices,
710 Error **errp)
711{
712 g_autoptr(GList) bdrvs = NULL;
713 GList *iterbdrvs;
714
715 GLOBAL_STATE_CODE();
716
717 if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) {
718 return NULL;
719 }
720
721 iterbdrvs = bdrvs;
722 while (iterbdrvs) {
723 BlockDriverState *bs = iterbdrvs->data;
724 AioContext *ctx = bdrv_get_aio_context(bs);
725 bool found = false;
726
727 aio_context_acquire(ctx);
728 found = (devices || bdrv_all_snapshots_includes_bs(bs)) &&
729 bdrv_can_snapshot(bs);
730 aio_context_release(ctx);
731
732 if (vmstate_bs) {
733 if (g_str_equal(vmstate_bs,
734 bdrv_get_node_name(bs))) {
735 if (found) {
736 return bs;
737 } else {
738 error_setg(errp,
739 "vmstate block device '%s' does not support snapshots",
740 vmstate_bs);
741 return NULL;
742 }
743 }
744 } else if (found) {
745 return bs;
746 }
747
748 iterbdrvs = iterbdrvs->next;
749 }
750
751 if (vmstate_bs) {
752 error_setg(errp,
753 "vmstate block device '%s' does not exist", vmstate_bs);
754 } else {
755 error_setg(errp,
756 "no block device can store vmstate for snapshot");
757 }
758 return NULL;
759}
760