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 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.
158 * We need to return an indirect pointer because bdrv_snapshot_goto()
159 * has to modify the BdrvChild pointer.
161 static BdrvChild
**bdrv_snapshot_fallback_ptr(BlockDriverState
*bs
)
163 BdrvChild
**fallback
;
164 BdrvChild
*child
= bdrv_primary_child(bs
);
166 /* We allow fallback only to primary child */
170 fallback
= (child
== bs
->file
? &bs
->file
: &bs
->backing
);
171 assert(*fallback
== child
);
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
178 QLIST_FOREACH(child
, &bs
->children
, next
) {
179 if (child
->role
& (BDRV_CHILD_DATA
| BDRV_CHILD_METADATA
|
180 BDRV_CHILD_FILTERED
) &&
190 static BlockDriverState
*bdrv_snapshot_fallback(BlockDriverState
*bs
)
192 BdrvChild
**child_ptr
= bdrv_snapshot_fallback_ptr(bs
);
193 return child_ptr
? (*child_ptr
)->bs
: NULL
;
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
;
240 BdrvChild
**fallback_ptr
;
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 fallback_ptr
= bdrv_snapshot_fallback_ptr(bs
);
267 Error
*local_err
= NULL
;
268 BlockDriverState
*fallback_bs
= (*fallback_ptr
)->bs
;
269 char *subqdict_prefix
= g_strdup_printf("%s.", (*fallback_ptr
)->name
);
271 options
= qdict_clone_shallow(bs
->options
);
273 /* Prevent it from getting deleted when detached from bs */
274 bdrv_ref(fallback_bs
);
276 qdict_extract_subqdict(options
, &file_options
, subqdict_prefix
);
277 qobject_unref(file_options
);
278 g_free(subqdict_prefix
);
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
));
284 /* Now close bs, apply the snapshot on fallback_bs, and re-open bs */
285 if (drv
->bdrv_close
) {
289 /* .bdrv_open() will re-attach it */
290 bdrv_unref_child(bs
, *fallback_ptr
);
291 *fallback_ptr
= NULL
;
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
);
297 bdrv_unref(fallback_bs
);
299 /* A bdrv_snapshot_goto() error takes precedence */
300 error_propagate(errp
, local_err
);
301 return ret
< 0 ? ret
: open_ret
;
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.
310 assert(bdrv_primary_bs(bs
) == fallback_bs
);
311 bdrv_unref(fallback_bs
);
315 error_setg(errp
, "Block driver does not support snapshots");
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
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
330 * If only @name is specified, delete the first one with name @name.
331 * if none is specified, return -EINVAL.
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.
341 int bdrv_snapshot_delete(BlockDriverState
*bs
,
342 const char *snapshot_id
,
346 BlockDriver
*drv
= bs
->drv
;
347 BlockDriverState
*fallback_bs
= bdrv_snapshot_fallback(bs
);
353 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, bdrv_get_device_name(bs
));
356 if (!snapshot_id
&& !name
) {
357 error_setg(errp
, "snapshot_id and name are both NULL");
361 /* drain all pending i/o before deleting snapshot */
362 bdrv_drained_begin(bs
);
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
);
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
));
375 bdrv_drained_end(bs
);
379 int bdrv_snapshot_list(BlockDriverState
*bs
,
380 QEMUSnapshotInfo
**psn_info
)
382 BlockDriver
*drv
= bs
->drv
;
383 BlockDriverState
*fallback_bs
= bdrv_snapshot_fallback(bs
);
389 if (drv
->bdrv_snapshot_list
) {
390 return drv
->bdrv_snapshot_list(bs
, psn_info
);
393 return bdrv_snapshot_list(fallback_bs
, psn_info
);
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
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
409 * If only @name is specified, load the first one with name @name.
410 * if none is specified, return -EINVAL.
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
418 int bdrv_snapshot_load_tmp(BlockDriverState
*bs
,
419 const char *snapshot_id
,
423 BlockDriver
*drv
= bs
->drv
;
428 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, bdrv_get_device_name(bs
));
431 if (!snapshot_id
&& !name
) {
432 error_setg(errp
, "snapshot_id and name are both NULL");
435 if (!bdrv_is_read_only(bs
)) {
436 error_setg(errp
, "Device is not readonly");
439 if (drv
->bdrv_snapshot_load_tmp
) {
440 return drv
->bdrv_snapshot_load_tmp(bs
, snapshot_id
, name
, errp
);
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
));
448 int bdrv_snapshot_load_tmp_by_id_or_name(BlockDriverState
*bs
,
449 const char *id_or_name
,
453 Error
*local_err
= NULL
;
457 ret
= bdrv_snapshot_load_tmp(bs
, id_or_name
, NULL
, &local_err
);
458 if (ret
== -ENOENT
|| ret
== -EINVAL
) {
459 error_free(local_err
);
461 ret
= bdrv_snapshot_load_tmp(bs
, NULL
, id_or_name
, &local_err
);
464 error_propagate(errp
, local_err
);
470 static int bdrv_all_get_snapshot_devices(bool has_devices
, strList
*devices
,
474 g_autoptr(GList
) bdrvs
= NULL
;
478 error_setg(errp
, "At least one device is required for snapshot");
483 BlockDriverState
*bs
= bdrv_find_node(devices
->value
);
485 error_setg(errp
, "No block device node '%s'", devices
->value
);
488 bdrvs
= g_list_append(bdrvs
, bs
);
489 devices
= devices
->next
;
492 BlockDriverState
*bs
;
494 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
495 bdrvs
= g_list_append(bdrvs
, bs
);
499 *all_bdrvs
= g_steal_pointer(&bdrvs
);
504 static bool bdrv_all_snapshots_includes_bs(BlockDriverState
*bs
)
506 if (!bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
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
);
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) */
519 bool bdrv_all_can_snapshot(bool has_devices
, strList
*devices
,
522 g_autoptr(GList
) bdrvs
= NULL
;
527 if (bdrv_all_get_snapshot_devices(has_devices
, devices
, &bdrvs
, errp
) < 0) {
533 BlockDriverState
*bs
= iterbdrvs
->data
;
534 AioContext
*ctx
= bdrv_get_aio_context(bs
);
537 aio_context_acquire(ctx
);
538 if (devices
|| bdrv_all_snapshots_includes_bs(bs
)) {
539 ok
= bdrv_can_snapshot(bs
);
541 aio_context_release(ctx
);
543 error_setg(errp
, "Device '%s' is writable but does not support "
544 "snapshots", bdrv_get_device_or_node_name(bs
));
548 iterbdrvs
= iterbdrvs
->next
;
554 int bdrv_all_delete_snapshot(const char *name
,
555 bool has_devices
, strList
*devices
,
558 g_autoptr(GList
) bdrvs
= NULL
;
563 if (bdrv_all_get_snapshot_devices(has_devices
, devices
, &bdrvs
, errp
) < 0) {
569 BlockDriverState
*bs
= iterbdrvs
->data
;
570 AioContext
*ctx
= bdrv_get_aio_context(bs
);
571 QEMUSnapshotInfo sn1
, *snapshot
= &sn1
;
574 aio_context_acquire(ctx
);
575 if ((devices
|| bdrv_all_snapshots_includes_bs(bs
)) &&
576 bdrv_snapshot_find(bs
, snapshot
, name
) >= 0)
578 ret
= bdrv_snapshot_delete(bs
, snapshot
->id_str
,
579 snapshot
->name
, errp
);
581 aio_context_release(ctx
);
583 error_prepend(errp
, "Could not delete snapshot '%s' on '%s': ",
584 name
, bdrv_get_device_or_node_name(bs
));
588 iterbdrvs
= iterbdrvs
->next
;
595 int bdrv_all_goto_snapshot(const char *name
,
596 bool has_devices
, strList
*devices
,
599 g_autoptr(GList
) bdrvs
= NULL
;
604 if (bdrv_all_get_snapshot_devices(has_devices
, devices
, &bdrvs
, errp
) < 0) {
610 BlockDriverState
*bs
= iterbdrvs
->data
;
611 AioContext
*ctx
= bdrv_get_aio_context(bs
);
614 aio_context_acquire(ctx
);
615 if (devices
|| bdrv_all_snapshots_includes_bs(bs
)) {
616 ret
= bdrv_snapshot_goto(bs
, name
, errp
);
618 aio_context_release(ctx
);
620 error_prepend(errp
, "Could not load snapshot '%s' on '%s': ",
621 name
, bdrv_get_device_or_node_name(bs
));
625 iterbdrvs
= iterbdrvs
->next
;
631 int bdrv_all_has_snapshot(const char *name
,
632 bool has_devices
, strList
*devices
,
635 g_autoptr(GList
) bdrvs
= NULL
;
640 if (bdrv_all_get_snapshot_devices(has_devices
, devices
, &bdrvs
, errp
) < 0) {
646 BlockDriverState
*bs
= iterbdrvs
->data
;
647 AioContext
*ctx
= bdrv_get_aio_context(bs
);
651 aio_context_acquire(ctx
);
652 if (devices
|| bdrv_all_snapshots_includes_bs(bs
)) {
653 ret
= bdrv_snapshot_find(bs
, &sn
, name
);
655 aio_context_release(ctx
);
657 if (ret
== -ENOENT
) {
660 error_setg_errno(errp
, errno
,
661 "Could not check snapshot '%s' on '%s'",
662 name
, bdrv_get_device_or_node_name(bs
));
667 iterbdrvs
= iterbdrvs
->next
;
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
,
679 g_autoptr(GList
) bdrvs
= NULL
;
683 if (bdrv_all_get_snapshot_devices(has_devices
, devices
, &bdrvs
, errp
) < 0) {
689 BlockDriverState
*bs
= iterbdrvs
->data
;
690 AioContext
*ctx
= bdrv_get_aio_context(bs
);
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
);
701 aio_context_release(ctx
);
703 error_setg(errp
, "Could not create snapshot '%s' on '%s'",
704 sn
->name
, bdrv_get_device_or_node_name(bs
));
708 iterbdrvs
= iterbdrvs
->next
;
715 BlockDriverState
*bdrv_all_find_vmstate_bs(const char *vmstate_bs
,
716 bool has_devices
, strList
*devices
,
719 g_autoptr(GList
) bdrvs
= NULL
;
724 if (bdrv_all_get_snapshot_devices(has_devices
, devices
, &bdrvs
, errp
) < 0) {
730 BlockDriverState
*bs
= iterbdrvs
->data
;
731 AioContext
*ctx
= bdrv_get_aio_context(bs
);
734 aio_context_acquire(ctx
);
735 found
= (devices
|| bdrv_all_snapshots_includes_bs(bs
)) &&
736 bdrv_can_snapshot(bs
);
737 aio_context_release(ctx
);
740 if (g_str_equal(vmstate_bs
,
741 bdrv_get_node_name(bs
))) {
746 "vmstate block device '%s' does not support snapshots",
755 iterbdrvs
= iterbdrvs
->next
;
760 "vmstate block device '%s' does not exist", vmstate_bs
);
763 "no block device can store vmstate for snapshot");