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
*bdrv_snapshot_fallback_child(BlockDriverState
*bs
)
160 BdrvChild
*fallback
= bdrv_primary_child(bs
);
163 /* We allow fallback only to primary child */
169 * Check that there are no other children that would need to be
170 * snapshotted. If there are, it is not safe to fall back to
173 QLIST_FOREACH(child
, &bs
->children
, next
) {
174 if (child
->role
& (BDRV_CHILD_DATA
| BDRV_CHILD_METADATA
|
175 BDRV_CHILD_FILTERED
) &&
185 static BlockDriverState
*bdrv_snapshot_fallback(BlockDriverState
*bs
)
187 return child_bs(bdrv_snapshot_fallback_child(bs
));
190 int bdrv_can_snapshot(BlockDriverState
*bs
)
192 BlockDriver
*drv
= bs
->drv
;
194 if (!drv
|| !bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
198 if (!drv
->bdrv_snapshot_create
) {
199 BlockDriverState
*fallback_bs
= bdrv_snapshot_fallback(bs
);
201 return bdrv_can_snapshot(fallback_bs
);
209 int bdrv_snapshot_create(BlockDriverState
*bs
,
210 QEMUSnapshotInfo
*sn_info
)
212 BlockDriver
*drv
= bs
->drv
;
213 BlockDriverState
*fallback_bs
= bdrv_snapshot_fallback(bs
);
220 if (drv
->bdrv_snapshot_create
) {
221 return drv
->bdrv_snapshot_create(bs
, sn_info
);
224 return bdrv_snapshot_create(fallback_bs
, sn_info
);
229 int bdrv_snapshot_goto(BlockDriverState
*bs
,
230 const char *snapshot_id
,
233 BlockDriver
*drv
= bs
->drv
;
240 error_setg(errp
, "Block driver is closed");
244 if (!QLIST_EMPTY(&bs
->dirty_bitmaps
)) {
245 error_setg(errp
, "Device has active dirty bitmaps");
249 if (drv
->bdrv_snapshot_goto
) {
250 ret
= drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
252 error_setg_errno(errp
, -ret
, "Failed to load snapshot");
257 fallback
= bdrv_snapshot_fallback_child(bs
);
261 Error
*local_err
= NULL
;
262 BlockDriverState
*fallback_bs
= fallback
->bs
;
263 char *subqdict_prefix
= g_strdup_printf("%s.", fallback
->name
);
265 options
= qdict_clone_shallow(bs
->options
);
267 /* Prevent it from getting deleted when detached from bs */
268 bdrv_ref(fallback_bs
);
270 qdict_extract_subqdict(options
, &file_options
, subqdict_prefix
);
271 qobject_unref(file_options
);
272 g_free(subqdict_prefix
);
274 /* Force .bdrv_open() below to re-attach fallback_bs on fallback */
275 qdict_put_str(options
, fallback
->name
,
276 bdrv_get_node_name(fallback_bs
));
278 /* Now close bs, apply the snapshot on fallback_bs, and re-open bs */
279 if (drv
->bdrv_close
) {
283 /* .bdrv_open() will re-attach it */
284 bdrv_unref_child(bs
, fallback
);
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
);
290 bdrv_unref(fallback_bs
);
292 /* A bdrv_snapshot_goto() error takes precedence */
293 error_propagate(errp
, local_err
);
294 return ret
< 0 ? ret
: open_ret
;
298 * fallback was a primary child. It was closed above and set to NULL,
299 * but the .bdrv_open() call has opened it again, because we set the
300 * respective option (with the qdict_put_str() call above).
301 * Assert that .bdrv_open() has attached the right BDS as primary child.
303 assert(bdrv_primary_bs(bs
) == fallback_bs
);
304 bdrv_unref(fallback_bs
);
308 error_setg(errp
, "Block driver does not support snapshots");
313 * Delete an internal snapshot by @snapshot_id and @name.
314 * @bs: block device used in the operation
315 * @snapshot_id: unique snapshot ID, or NULL
316 * @name: snapshot name, or NULL
317 * @errp: location to store error
319 * If both @snapshot_id and @name are specified, delete the first one with
320 * id @snapshot_id and name @name.
321 * If only @snapshot_id is specified, delete the first one with id
323 * If only @name is specified, delete the first one with name @name.
324 * if none is specified, return -EINVAL.
326 * Returns: 0 on success, -errno on failure. If @bs is not inserted, return
327 * -ENOMEDIUM. If @snapshot_id and @name are both NULL, return -EINVAL. If @bs
328 * does not support internal snapshot deletion, return -ENOTSUP. If @bs does
329 * not support parameter @snapshot_id or @name, or one of them is not correctly
330 * specified, return -EINVAL. If @bs can't find one matching @id and @name,
331 * return -ENOENT. If @errp != NULL, it will always be filled with error
332 * message on failure.
334 int bdrv_snapshot_delete(BlockDriverState
*bs
,
335 const char *snapshot_id
,
339 BlockDriver
*drv
= bs
->drv
;
340 BlockDriverState
*fallback_bs
= bdrv_snapshot_fallback(bs
);
346 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, bdrv_get_device_name(bs
));
349 if (!snapshot_id
&& !name
) {
350 error_setg(errp
, "snapshot_id and name are both NULL");
354 /* drain all pending i/o before deleting snapshot */
355 bdrv_drained_begin(bs
);
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
);
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
));
368 bdrv_drained_end(bs
);
372 int bdrv_snapshot_list(BlockDriverState
*bs
,
373 QEMUSnapshotInfo
**psn_info
)
375 BlockDriver
*drv
= bs
->drv
;
376 BlockDriverState
*fallback_bs
= bdrv_snapshot_fallback(bs
);
382 if (drv
->bdrv_snapshot_list
) {
383 return drv
->bdrv_snapshot_list(bs
, psn_info
);
386 return bdrv_snapshot_list(fallback_bs
, psn_info
);
392 * Temporarily load an internal snapshot by @snapshot_id and @name.
393 * @bs: block device used in the operation
394 * @snapshot_id: unique snapshot ID, or NULL
395 * @name: snapshot name, or NULL
396 * @errp: location to store error
398 * If both @snapshot_id and @name are specified, load the first one with
399 * id @snapshot_id and name @name.
400 * If only @snapshot_id is specified, load the first one with id
402 * If only @name is specified, load the first one with name @name.
403 * if none is specified, return -EINVAL.
405 * Returns: 0 on success, -errno on fail. If @bs is not inserted, return
406 * -ENOMEDIUM. If @bs is not readonly, return -EINVAL. If @bs did not support
407 * internal snapshot, return -ENOTSUP. If qemu can't find a matching @id and
408 * @name, return -ENOENT. If @errp != NULL, it will always be filled on
411 int bdrv_snapshot_load_tmp(BlockDriverState
*bs
,
412 const char *snapshot_id
,
416 BlockDriver
*drv
= bs
->drv
;
421 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, bdrv_get_device_name(bs
));
424 if (!snapshot_id
&& !name
) {
425 error_setg(errp
, "snapshot_id and name are both NULL");
428 if (!bdrv_is_read_only(bs
)) {
429 error_setg(errp
, "Device is not readonly");
432 if (drv
->bdrv_snapshot_load_tmp
) {
433 return drv
->bdrv_snapshot_load_tmp(bs
, snapshot_id
, name
, errp
);
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
));
441 int bdrv_snapshot_load_tmp_by_id_or_name(BlockDriverState
*bs
,
442 const char *id_or_name
,
446 Error
*local_err
= NULL
;
450 ret
= bdrv_snapshot_load_tmp(bs
, id_or_name
, NULL
, &local_err
);
451 if (ret
== -ENOENT
|| ret
== -EINVAL
) {
452 error_free(local_err
);
454 ret
= bdrv_snapshot_load_tmp(bs
, NULL
, id_or_name
, &local_err
);
457 error_propagate(errp
, local_err
);
463 static int bdrv_all_get_snapshot_devices(bool has_devices
, strList
*devices
,
467 g_autoptr(GList
) bdrvs
= NULL
;
471 error_setg(errp
, "At least one device is required for snapshot");
476 BlockDriverState
*bs
= bdrv_find_node(devices
->value
);
478 error_setg(errp
, "No block device node '%s'", devices
->value
);
481 bdrvs
= g_list_append(bdrvs
, bs
);
482 devices
= devices
->next
;
485 BlockDriverState
*bs
;
487 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
488 bdrvs
= g_list_append(bdrvs
, bs
);
492 *all_bdrvs
= g_steal_pointer(&bdrvs
);
497 static bool bdrv_all_snapshots_includes_bs(BlockDriverState
*bs
)
499 if (!bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
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
);
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) */
512 bool bdrv_all_can_snapshot(bool has_devices
, strList
*devices
,
515 g_autoptr(GList
) bdrvs
= NULL
;
520 if (bdrv_all_get_snapshot_devices(has_devices
, devices
, &bdrvs
, errp
) < 0) {
526 BlockDriverState
*bs
= iterbdrvs
->data
;
527 AioContext
*ctx
= bdrv_get_aio_context(bs
);
530 aio_context_acquire(ctx
);
531 if (devices
|| bdrv_all_snapshots_includes_bs(bs
)) {
532 ok
= bdrv_can_snapshot(bs
);
534 aio_context_release(ctx
);
536 error_setg(errp
, "Device '%s' is writable but does not support "
537 "snapshots", bdrv_get_device_or_node_name(bs
));
541 iterbdrvs
= iterbdrvs
->next
;
547 int bdrv_all_delete_snapshot(const char *name
,
548 bool has_devices
, strList
*devices
,
551 g_autoptr(GList
) bdrvs
= NULL
;
556 if (bdrv_all_get_snapshot_devices(has_devices
, devices
, &bdrvs
, errp
) < 0) {
562 BlockDriverState
*bs
= iterbdrvs
->data
;
563 AioContext
*ctx
= bdrv_get_aio_context(bs
);
564 QEMUSnapshotInfo sn1
, *snapshot
= &sn1
;
567 aio_context_acquire(ctx
);
568 if ((devices
|| bdrv_all_snapshots_includes_bs(bs
)) &&
569 bdrv_snapshot_find(bs
, snapshot
, name
) >= 0)
571 ret
= bdrv_snapshot_delete(bs
, snapshot
->id_str
,
572 snapshot
->name
, errp
);
574 aio_context_release(ctx
);
576 error_prepend(errp
, "Could not delete snapshot '%s' on '%s': ",
577 name
, bdrv_get_device_or_node_name(bs
));
581 iterbdrvs
= iterbdrvs
->next
;
588 int bdrv_all_goto_snapshot(const char *name
,
589 bool has_devices
, strList
*devices
,
592 g_autoptr(GList
) bdrvs
= NULL
;
597 if (bdrv_all_get_snapshot_devices(has_devices
, devices
, &bdrvs
, errp
) < 0) {
603 BlockDriverState
*bs
= iterbdrvs
->data
;
604 AioContext
*ctx
= bdrv_get_aio_context(bs
);
607 aio_context_acquire(ctx
);
608 if (devices
|| bdrv_all_snapshots_includes_bs(bs
)) {
609 ret
= bdrv_snapshot_goto(bs
, name
, errp
);
611 aio_context_release(ctx
);
613 error_prepend(errp
, "Could not load snapshot '%s' on '%s': ",
614 name
, bdrv_get_device_or_node_name(bs
));
618 iterbdrvs
= iterbdrvs
->next
;
624 int bdrv_all_has_snapshot(const char *name
,
625 bool has_devices
, strList
*devices
,
628 g_autoptr(GList
) bdrvs
= NULL
;
633 if (bdrv_all_get_snapshot_devices(has_devices
, devices
, &bdrvs
, errp
) < 0) {
639 BlockDriverState
*bs
= iterbdrvs
->data
;
640 AioContext
*ctx
= bdrv_get_aio_context(bs
);
644 aio_context_acquire(ctx
);
645 if (devices
|| bdrv_all_snapshots_includes_bs(bs
)) {
646 ret
= bdrv_snapshot_find(bs
, &sn
, name
);
648 aio_context_release(ctx
);
650 if (ret
== -ENOENT
) {
653 error_setg_errno(errp
, errno
,
654 "Could not check snapshot '%s' on '%s'",
655 name
, bdrv_get_device_or_node_name(bs
));
660 iterbdrvs
= iterbdrvs
->next
;
666 int bdrv_all_create_snapshot(QEMUSnapshotInfo
*sn
,
667 BlockDriverState
*vm_state_bs
,
668 uint64_t vm_state_size
,
669 bool has_devices
, strList
*devices
,
672 g_autoptr(GList
) bdrvs
= NULL
;
676 if (bdrv_all_get_snapshot_devices(has_devices
, devices
, &bdrvs
, errp
) < 0) {
682 BlockDriverState
*bs
= iterbdrvs
->data
;
683 AioContext
*ctx
= bdrv_get_aio_context(bs
);
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
);
694 aio_context_release(ctx
);
696 error_setg(errp
, "Could not create snapshot '%s' on '%s'",
697 sn
->name
, bdrv_get_device_or_node_name(bs
));
701 iterbdrvs
= iterbdrvs
->next
;
708 BlockDriverState
*bdrv_all_find_vmstate_bs(const char *vmstate_bs
,
709 bool has_devices
, strList
*devices
,
712 g_autoptr(GList
) bdrvs
= NULL
;
717 if (bdrv_all_get_snapshot_devices(has_devices
, devices
, &bdrvs
, errp
) < 0) {
723 BlockDriverState
*bs
= iterbdrvs
->data
;
724 AioContext
*ctx
= bdrv_get_aio_context(bs
);
727 aio_context_acquire(ctx
);
728 found
= (devices
|| bdrv_all_snapshots_includes_bs(bs
)) &&
729 bdrv_can_snapshot(bs
);
730 aio_context_release(ctx
);
733 if (g_str_equal(vmstate_bs
,
734 bdrv_get_node_name(bs
))) {
739 "vmstate block device '%s' does not support snapshots",
748 iterbdrvs
= iterbdrvs
->next
;
753 "vmstate block device '%s' does not exist", vmstate_bs
);
756 "no block device can store vmstate for snapshot");