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
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
= {
38 .head
= QTAILQ_HEAD_INITIALIZER(internal_snapshot_opts
.head
),
41 .name
= SNAPSHOT_OPT_ID
,
42 .type
= QEMU_OPT_STRING
,
45 .name
= SNAPSHOT_OPT_NAME
,
46 .type
= QEMU_OPT_STRING
,
47 .help
= "snapshot name"
54 int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
57 QEMUSnapshotInfo
*sn_tab
, *sn
;
63 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
67 for (i
= 0; i
< nb_sns
; i
++) {
69 if (!strcmp(sn
->name
, name
)) {
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
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
,
102 QEMUSnapshotInfo
*sn_info
,
105 QEMUSnapshotInfo
*sn_tab
, *sn
;
112 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
114 error_setg_errno(errp
, -nb_sns
, "Failed to get a snapshot list");
116 } else if (nb_sns
== 0) {
121 for (i
= 0; i
< nb_sns
; i
++) {
123 if (!strcmp(sn
->id_str
, id
) && !strcmp(sn
->name
, name
)) {
130 for (i
= 0; i
< nb_sns
; i
++) {
132 if (!strcmp(sn
->id_str
, id
)) {
139 for (i
= 0; i
< nb_sns
; i
++) {
141 if (!strcmp(sn
->name
, name
)) {
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
);
165 assert_bdrv_graph_readable();
167 /* We allow fallback only to primary child */
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
177 QLIST_FOREACH(child
, &bs
->children
, next
) {
178 if (child
->role
& (BDRV_CHILD_DATA
| BDRV_CHILD_METADATA
|
179 BDRV_CHILD_FILTERED
) &&
189 static BlockDriverState
* GRAPH_RDLOCK
190 bdrv_snapshot_fallback(BlockDriverState
*bs
)
193 return child_bs(bdrv_snapshot_fallback_child(bs
));
196 int bdrv_can_snapshot(BlockDriverState
*bs
)
198 BlockDriver
*drv
= bs
->drv
;
200 if (!drv
|| !bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
204 if (!drv
->bdrv_snapshot_create
) {
205 BlockDriverState
*fallback_bs
= bdrv_snapshot_fallback(bs
);
207 return bdrv_can_snapshot(fallback_bs
);
215 int bdrv_snapshot_create(BlockDriverState
*bs
,
216 QEMUSnapshotInfo
*sn_info
)
218 BlockDriver
*drv
= bs
->drv
;
219 BlockDriverState
*fallback_bs
= bdrv_snapshot_fallback(bs
);
226 if (drv
->bdrv_snapshot_create
) {
227 return drv
->bdrv_snapshot_create(bs
, sn_info
);
230 return bdrv_snapshot_create(fallback_bs
, sn_info
);
235 int bdrv_snapshot_goto(BlockDriverState
*bs
,
236 const char *snapshot_id
,
239 BlockDriver
*drv
= bs
->drv
;
246 error_setg(errp
, "Block driver is closed");
250 if (!QLIST_EMPTY(&bs
->dirty_bitmaps
)) {
251 error_setg(errp
, "Device has active dirty bitmaps");
255 if (drv
->bdrv_snapshot_goto
) {
256 ret
= drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
258 error_setg_errno(errp
, -ret
, "Failed to load snapshot");
263 bdrv_graph_rdlock_main_loop();
264 fallback
= bdrv_snapshot_fallback_child(bs
);
265 bdrv_graph_rdunlock_main_loop();
270 Error
*local_err
= NULL
;
271 BlockDriverState
*fallback_bs
= fallback
->bs
;
272 char *subqdict_prefix
= g_strdup_printf("%s.", fallback
->name
);
274 options
= qdict_clone_shallow(bs
->options
);
276 /* Prevent it from getting deleted when detached from bs */
277 bdrv_ref(fallback_bs
);
279 qdict_extract_subqdict(options
, &file_options
, subqdict_prefix
);
280 qobject_unref(file_options
);
281 g_free(subqdict_prefix
);
283 /* Force .bdrv_open() below to re-attach fallback_bs on fallback */
284 qdict_put_str(options
, fallback
->name
,
285 bdrv_get_node_name(fallback_bs
));
287 /* Now close bs, apply the snapshot on fallback_bs, and re-open bs */
288 if (drv
->bdrv_close
) {
292 /* .bdrv_open() will re-attach it */
293 bdrv_graph_wrlock(NULL
);
294 bdrv_unref_child(bs
, fallback
);
295 bdrv_graph_wrunlock();
297 ret
= bdrv_snapshot_goto(fallback_bs
, snapshot_id
, errp
);
298 open_ret
= drv
->bdrv_open(bs
, options
, bs
->open_flags
, &local_err
);
299 qobject_unref(options
);
301 bdrv_unref(fallback_bs
);
303 /* A bdrv_snapshot_goto() error takes precedence */
304 error_propagate(errp
, local_err
);
305 return ret
< 0 ? ret
: open_ret
;
309 * fallback was a primary child. It was closed above and set to NULL,
310 * but the .bdrv_open() call has opened it again, because we set the
311 * respective option (with the qdict_put_str() call above).
312 * Assert that .bdrv_open() has attached the right BDS as primary child.
314 bdrv_graph_rdlock_main_loop();
315 assert(bdrv_primary_bs(bs
) == fallback_bs
);
316 bdrv_graph_rdunlock_main_loop();
318 bdrv_unref(fallback_bs
);
322 error_setg(errp
, "Block driver does not support snapshots");
327 * Delete an internal snapshot by @snapshot_id and @name.
328 * @bs: block device used in the operation
329 * @snapshot_id: unique snapshot ID, or NULL
330 * @name: snapshot name, or NULL
331 * @errp: location to store error
333 * If both @snapshot_id and @name are specified, delete the first one with
334 * id @snapshot_id and name @name.
335 * If only @snapshot_id is specified, delete the first one with id
337 * If only @name is specified, delete the first one with name @name.
338 * if none is specified, return -EINVAL.
340 * Returns: 0 on success, -errno on failure. If @bs is not inserted, return
341 * -ENOMEDIUM. If @snapshot_id and @name are both NULL, return -EINVAL. If @bs
342 * does not support internal snapshot deletion, return -ENOTSUP. If @bs does
343 * not support parameter @snapshot_id or @name, or one of them is not correctly
344 * specified, return -EINVAL. If @bs can't find one matching @id and @name,
345 * return -ENOENT. If @errp != NULL, it will always be filled with error
346 * message on failure.
348 int bdrv_snapshot_delete(BlockDriverState
*bs
,
349 const char *snapshot_id
,
353 BlockDriver
*drv
= bs
->drv
;
354 BlockDriverState
*fallback_bs
= bdrv_snapshot_fallback(bs
);
360 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, bdrv_get_device_name(bs
));
363 if (!snapshot_id
&& !name
) {
364 error_setg(errp
, "snapshot_id and name are both NULL");
368 /* drain all pending i/o before deleting snapshot */
369 bdrv_drained_begin(bs
);
371 if (drv
->bdrv_snapshot_delete
) {
372 ret
= drv
->bdrv_snapshot_delete(bs
, snapshot_id
, name
, errp
);
373 } else if (fallback_bs
) {
374 ret
= bdrv_snapshot_delete(fallback_bs
, snapshot_id
, name
, errp
);
376 error_setg(errp
, "Block format '%s' used by device '%s' "
377 "does not support internal snapshot deletion",
378 drv
->format_name
, bdrv_get_device_name(bs
));
382 bdrv_drained_end(bs
);
386 int bdrv_snapshot_list(BlockDriverState
*bs
,
387 QEMUSnapshotInfo
**psn_info
)
390 GRAPH_RDLOCK_GUARD_MAINLOOP();
392 BlockDriver
*drv
= bs
->drv
;
393 BlockDriverState
*fallback_bs
= bdrv_snapshot_fallback(bs
);
398 if (drv
->bdrv_snapshot_list
) {
399 return drv
->bdrv_snapshot_list(bs
, psn_info
);
402 return bdrv_snapshot_list(fallback_bs
, psn_info
);
408 * Temporarily load an internal snapshot by @snapshot_id and @name.
409 * @bs: block device used in the operation
410 * @snapshot_id: unique snapshot ID, or NULL
411 * @name: snapshot name, or NULL
412 * @errp: location to store error
414 * If both @snapshot_id and @name are specified, load the first one with
415 * id @snapshot_id and name @name.
416 * If only @snapshot_id is specified, load the first one with id
418 * If only @name is specified, load the first one with name @name.
419 * if none is specified, return -EINVAL.
421 * Returns: 0 on success, -errno on fail. If @bs is not inserted, return
422 * -ENOMEDIUM. If @bs is not readonly, return -EINVAL. If @bs did not support
423 * internal snapshot, return -ENOTSUP. If qemu can't find a matching @id and
424 * @name, return -ENOENT. If @errp != NULL, it will always be filled on
427 int bdrv_snapshot_load_tmp(BlockDriverState
*bs
,
428 const char *snapshot_id
,
432 BlockDriver
*drv
= bs
->drv
;
435 GRAPH_RDLOCK_GUARD_MAINLOOP();
438 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, bdrv_get_device_name(bs
));
441 if (!snapshot_id
&& !name
) {
442 error_setg(errp
, "snapshot_id and name are both NULL");
445 if (!bdrv_is_read_only(bs
)) {
446 error_setg(errp
, "Device is not readonly");
449 if (drv
->bdrv_snapshot_load_tmp
) {
450 return drv
->bdrv_snapshot_load_tmp(bs
, snapshot_id
, name
, errp
);
452 error_setg(errp
, "Block format '%s' used by device '%s' "
453 "does not support temporarily loading internal snapshots",
454 drv
->format_name
, bdrv_get_device_name(bs
));
458 int bdrv_snapshot_load_tmp_by_id_or_name(BlockDriverState
*bs
,
459 const char *id_or_name
,
463 Error
*local_err
= NULL
;
467 ret
= bdrv_snapshot_load_tmp(bs
, id_or_name
, NULL
, &local_err
);
468 if (ret
== -ENOENT
|| ret
== -EINVAL
) {
469 error_free(local_err
);
471 ret
= bdrv_snapshot_load_tmp(bs
, NULL
, id_or_name
, &local_err
);
474 error_propagate(errp
, local_err
);
480 static int GRAPH_RDLOCK
481 bdrv_all_get_snapshot_devices(bool has_devices
, strList
*devices
,
482 GList
**all_bdrvs
, Error
**errp
)
484 g_autoptr(GList
) bdrvs
= NULL
;
488 error_setg(errp
, "At least one device is required for snapshot");
493 BlockDriverState
*bs
= bdrv_find_node(devices
->value
);
495 error_setg(errp
, "No block device node '%s'", devices
->value
);
498 bdrvs
= g_list_append(bdrvs
, bs
);
499 devices
= devices
->next
;
502 BlockDriverState
*bs
;
504 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
505 bdrvs
= g_list_append(bdrvs
, bs
);
509 *all_bdrvs
= g_steal_pointer(&bdrvs
);
514 static bool GRAPH_RDLOCK
bdrv_all_snapshots_includes_bs(BlockDriverState
*bs
)
517 assert_bdrv_graph_readable();
519 if (!bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
523 /* Include all nodes that are either in use by a BlockBackend, or that
524 * aren't attached to any node, but owned by the monitor. */
525 return bdrv_has_blk(bs
) || QLIST_EMPTY(&bs
->parents
);
528 /* Group operations. All block drivers are involved.
529 * These functions will properly handle dataplane (take aio_context_acquire
530 * when appropriate for appropriate block drivers) */
532 bool bdrv_all_can_snapshot(bool has_devices
, strList
*devices
,
535 g_autoptr(GList
) bdrvs
= NULL
;
539 GRAPH_RDLOCK_GUARD_MAINLOOP();
541 if (bdrv_all_get_snapshot_devices(has_devices
, devices
, &bdrvs
, errp
) < 0) {
547 BlockDriverState
*bs
= iterbdrvs
->data
;
548 AioContext
*ctx
= bdrv_get_aio_context(bs
);
551 aio_context_acquire(ctx
);
552 if (devices
|| bdrv_all_snapshots_includes_bs(bs
)) {
553 ok
= bdrv_can_snapshot(bs
);
555 aio_context_release(ctx
);
557 error_setg(errp
, "Device '%s' is writable but does not support "
558 "snapshots", bdrv_get_device_or_node_name(bs
));
562 iterbdrvs
= iterbdrvs
->next
;
568 int bdrv_all_delete_snapshot(const char *name
,
569 bool has_devices
, strList
*devices
,
572 g_autoptr(GList
) bdrvs
= NULL
;
576 GRAPH_RDLOCK_GUARD_MAINLOOP();
578 if (bdrv_all_get_snapshot_devices(has_devices
, devices
, &bdrvs
, errp
) < 0) {
584 BlockDriverState
*bs
= iterbdrvs
->data
;
585 AioContext
*ctx
= bdrv_get_aio_context(bs
);
586 QEMUSnapshotInfo sn1
, *snapshot
= &sn1
;
589 aio_context_acquire(ctx
);
590 if ((devices
|| bdrv_all_snapshots_includes_bs(bs
)) &&
591 bdrv_snapshot_find(bs
, snapshot
, name
) >= 0)
593 ret
= bdrv_snapshot_delete(bs
, snapshot
->id_str
,
594 snapshot
->name
, errp
);
596 aio_context_release(ctx
);
598 error_prepend(errp
, "Could not delete snapshot '%s' on '%s': ",
599 name
, bdrv_get_device_or_node_name(bs
));
603 iterbdrvs
= iterbdrvs
->next
;
610 int bdrv_all_goto_snapshot(const char *name
,
611 bool has_devices
, strList
*devices
,
614 g_autoptr(GList
) bdrvs
= NULL
;
620 bdrv_graph_rdlock_main_loop();
621 ret
= bdrv_all_get_snapshot_devices(has_devices
, devices
, &bdrvs
, errp
);
622 bdrv_graph_rdunlock_main_loop();
630 BlockDriverState
*bs
= iterbdrvs
->data
;
631 AioContext
*ctx
= bdrv_get_aio_context(bs
);
633 bool all_snapshots_includes_bs
;
635 aio_context_acquire(ctx
);
636 bdrv_graph_rdlock_main_loop();
637 all_snapshots_includes_bs
= bdrv_all_snapshots_includes_bs(bs
);
638 bdrv_graph_rdunlock_main_loop();
640 if (devices
|| all_snapshots_includes_bs
) {
641 ret
= bdrv_snapshot_goto(bs
, name
, errp
);
643 aio_context_release(ctx
);
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();
652 iterbdrvs
= iterbdrvs
->next
;
658 int bdrv_all_has_snapshot(const char *name
,
659 bool has_devices
, strList
*devices
,
662 g_autoptr(GList
) bdrvs
= NULL
;
666 GRAPH_RDLOCK_GUARD_MAINLOOP();
668 if (bdrv_all_get_snapshot_devices(has_devices
, devices
, &bdrvs
, errp
) < 0) {
674 BlockDriverState
*bs
= iterbdrvs
->data
;
675 AioContext
*ctx
= bdrv_get_aio_context(bs
);
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
);
685 if (ret
== -ENOENT
) {
688 error_setg_errno(errp
, errno
,
689 "Could not check snapshot '%s' on '%s'",
690 name
, bdrv_get_device_or_node_name(bs
));
695 iterbdrvs
= iterbdrvs
->next
;
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
,
707 g_autoptr(GList
) bdrvs
= NULL
;
711 GRAPH_RDLOCK_GUARD_MAINLOOP();
713 if (bdrv_all_get_snapshot_devices(has_devices
, devices
, &bdrvs
, errp
) < 0) {
719 BlockDriverState
*bs
= iterbdrvs
->data
;
720 AioContext
*ctx
= bdrv_get_aio_context(bs
);
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
);
733 error_setg(errp
, "Could not create snapshot '%s' on '%s'",
734 sn
->name
, bdrv_get_device_or_node_name(bs
));
738 iterbdrvs
= iterbdrvs
->next
;
745 BlockDriverState
*bdrv_all_find_vmstate_bs(const char *vmstate_bs
,
746 bool has_devices
, strList
*devices
,
749 g_autoptr(GList
) bdrvs
= NULL
;
753 GRAPH_RDLOCK_GUARD_MAINLOOP();
755 if (bdrv_all_get_snapshot_devices(has_devices
, devices
, &bdrvs
, errp
) < 0) {
761 BlockDriverState
*bs
= iterbdrvs
->data
;
762 AioContext
*ctx
= bdrv_get_aio_context(bs
);
765 aio_context_acquire(ctx
);
766 found
= (devices
|| bdrv_all_snapshots_includes_bs(bs
)) &&
767 bdrv_can_snapshot(bs
);
768 aio_context_release(ctx
);
771 if (g_str_equal(vmstate_bs
,
772 bdrv_get_node_name(bs
))) {
777 "vmstate block device '%s' does not support snapshots",
786 iterbdrvs
= iterbdrvs
->next
;
791 "vmstate block device '%s' does not exist", vmstate_bs
);
794 "no block device can store vmstate for snapshot");