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
;
61 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
65 for (i
= 0; i
< nb_sns
; i
++) {
67 if (!strcmp(sn
->name
, name
)) {
78 * Look up an internal snapshot by @id and @name.
79 * @bs: block device to search
80 * @id: unique snapshot ID, or NULL
81 * @name: snapshot name, or NULL
82 * @sn_info: location to store information on the snapshot found
83 * @errp: location to store error, will be set only for exception
85 * This function will traverse snapshot list in @bs to search the matching
86 * one, @id and @name are the matching condition:
87 * If both @id and @name are specified, find the first one with id @id and
89 * If only @id is specified, find the first one with id @id.
90 * If only @name is specified, find the first one with name @name.
91 * if none is specified, abort().
93 * Returns: true when a snapshot is found and @sn_info will be filled, false
94 * when error or not found. If all operation succeed but no matching one is
95 * found, @errp will NOT be set.
97 bool bdrv_snapshot_find_by_id_and_name(BlockDriverState
*bs
,
100 QEMUSnapshotInfo
*sn_info
,
103 QEMUSnapshotInfo
*sn_tab
, *sn
;
109 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
111 error_setg_errno(errp
, -nb_sns
, "Failed to get a snapshot list");
113 } else if (nb_sns
== 0) {
118 for (i
= 0; i
< nb_sns
; i
++) {
120 if (!strcmp(sn
->id_str
, id
) && !strcmp(sn
->name
, name
)) {
127 for (i
= 0; i
< nb_sns
; i
++) {
129 if (!strcmp(sn
->id_str
, id
)) {
136 for (i
= 0; i
< nb_sns
; i
++) {
138 if (!strcmp(sn
->name
, name
)) {
151 * Return a pointer to the child BDS pointer to which we can fall
152 * back if the given BDS does not support snapshots.
153 * Return NULL if there is no BDS to (safely) fall back to.
155 * We need to return an indirect pointer because bdrv_snapshot_goto()
156 * has to modify the BdrvChild pointer.
158 static BdrvChild
**bdrv_snapshot_fallback_ptr(BlockDriverState
*bs
)
160 BdrvChild
**fallback
;
164 * The only BdrvChild pointers that are safe to modify (and which
165 * we can thus return a reference to) are bs->file and
168 fallback
= &bs
->file
;
169 if (!*fallback
&& bs
->drv
&& bs
->drv
->is_filter
) {
170 fallback
= &bs
->backing
;
178 * Check that there are no other children that would need to be
179 * snapshotted. If there are, it is not safe to fall back to
182 QLIST_FOREACH(child
, &bs
->children
, next
) {
183 if (child
->role
& (BDRV_CHILD_DATA
| BDRV_CHILD_METADATA
|
184 BDRV_CHILD_FILTERED
) &&
194 static BlockDriverState
*bdrv_snapshot_fallback(BlockDriverState
*bs
)
196 BdrvChild
**child_ptr
= bdrv_snapshot_fallback_ptr(bs
);
197 return child_ptr
? (*child_ptr
)->bs
: NULL
;
200 int bdrv_can_snapshot(BlockDriverState
*bs
)
202 BlockDriver
*drv
= bs
->drv
;
203 if (!drv
|| !bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
207 if (!drv
->bdrv_snapshot_create
) {
208 BlockDriverState
*fallback_bs
= bdrv_snapshot_fallback(bs
);
210 return bdrv_can_snapshot(fallback_bs
);
218 int bdrv_snapshot_create(BlockDriverState
*bs
,
219 QEMUSnapshotInfo
*sn_info
)
221 BlockDriver
*drv
= bs
->drv
;
222 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
;
244 error_setg(errp
, "Block driver is closed");
248 if (!QLIST_EMPTY(&bs
->dirty_bitmaps
)) {
249 error_setg(errp
, "Device has active dirty bitmaps");
253 if (drv
->bdrv_snapshot_goto
) {
254 ret
= drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
256 error_setg_errno(errp
, -ret
, "Failed to load snapshot");
261 fallback_ptr
= bdrv_snapshot_fallback_ptr(bs
);
265 Error
*local_err
= NULL
;
266 BlockDriverState
*fallback_bs
= (*fallback_ptr
)->bs
;
267 char *subqdict_prefix
= g_strdup_printf("%s.", (*fallback_ptr
)->name
);
269 options
= qdict_clone_shallow(bs
->options
);
271 /* Prevent it from getting deleted when detached from bs */
272 bdrv_ref(fallback_bs
);
274 qdict_extract_subqdict(options
, &file_options
, subqdict_prefix
);
275 qobject_unref(file_options
);
276 g_free(subqdict_prefix
);
278 qdict_put_str(options
, (*fallback_ptr
)->name
,
279 bdrv_get_node_name(fallback_bs
));
281 if (drv
->bdrv_close
) {
285 bdrv_unref_child(bs
, *fallback_ptr
);
286 *fallback_ptr
= NULL
;
288 ret
= bdrv_snapshot_goto(fallback_bs
, snapshot_id
, errp
);
289 open_ret
= drv
->bdrv_open(bs
, options
, bs
->open_flags
, &local_err
);
290 qobject_unref(options
);
292 bdrv_unref(fallback_bs
);
294 /* A bdrv_snapshot_goto() error takes precedence */
295 error_propagate(errp
, local_err
);
296 return ret
< 0 ? ret
: open_ret
;
299 assert(fallback_bs
== (*fallback_ptr
)->bs
);
300 bdrv_unref(fallback_bs
);
304 error_setg(errp
, "Block driver does not support snapshots");
309 * Delete an internal snapshot by @snapshot_id and @name.
310 * @bs: block device used in the operation
311 * @snapshot_id: unique snapshot ID, or NULL
312 * @name: snapshot name, or NULL
313 * @errp: location to store error
315 * If both @snapshot_id and @name are specified, delete the first one with
316 * id @snapshot_id and name @name.
317 * If only @snapshot_id is specified, delete the first one with id
319 * If only @name is specified, delete the first one with name @name.
320 * if none is specified, return -EINVAL.
322 * Returns: 0 on success, -errno on failure. If @bs is not inserted, return
323 * -ENOMEDIUM. If @snapshot_id and @name are both NULL, return -EINVAL. If @bs
324 * does not support internal snapshot deletion, return -ENOTSUP. If @bs does
325 * not support parameter @snapshot_id or @name, or one of them is not correctly
326 * specified, return -EINVAL. If @bs can't find one matching @id and @name,
327 * return -ENOENT. If @errp != NULL, it will always be filled with error
328 * message on failure.
330 int bdrv_snapshot_delete(BlockDriverState
*bs
,
331 const char *snapshot_id
,
335 BlockDriver
*drv
= bs
->drv
;
336 BlockDriverState
*fallback_bs
= bdrv_snapshot_fallback(bs
);
340 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, bdrv_get_device_name(bs
));
343 if (!snapshot_id
&& !name
) {
344 error_setg(errp
, "snapshot_id and name are both NULL");
348 /* drain all pending i/o before deleting snapshot */
349 bdrv_drained_begin(bs
);
351 if (drv
->bdrv_snapshot_delete
) {
352 ret
= drv
->bdrv_snapshot_delete(bs
, snapshot_id
, name
, errp
);
353 } else if (fallback_bs
) {
354 ret
= bdrv_snapshot_delete(fallback_bs
, snapshot_id
, name
, errp
);
356 error_setg(errp
, "Block format '%s' used by device '%s' "
357 "does not support internal snapshot deletion",
358 drv
->format_name
, bdrv_get_device_name(bs
));
362 bdrv_drained_end(bs
);
366 int bdrv_snapshot_list(BlockDriverState
*bs
,
367 QEMUSnapshotInfo
**psn_info
)
369 BlockDriver
*drv
= bs
->drv
;
370 BlockDriverState
*fallback_bs
= bdrv_snapshot_fallback(bs
);
374 if (drv
->bdrv_snapshot_list
) {
375 return drv
->bdrv_snapshot_list(bs
, psn_info
);
378 return bdrv_snapshot_list(fallback_bs
, psn_info
);
384 * Temporarily load an internal snapshot by @snapshot_id and @name.
385 * @bs: block device used in the operation
386 * @snapshot_id: unique snapshot ID, or NULL
387 * @name: snapshot name, or NULL
388 * @errp: location to store error
390 * If both @snapshot_id and @name are specified, load the first one with
391 * id @snapshot_id and name @name.
392 * If only @snapshot_id is specified, load the first one with id
394 * If only @name is specified, load the first one with name @name.
395 * if none is specified, return -EINVAL.
397 * Returns: 0 on success, -errno on fail. If @bs is not inserted, return
398 * -ENOMEDIUM. If @bs is not readonly, return -EINVAL. If @bs did not support
399 * internal snapshot, return -ENOTSUP. If qemu can't find a matching @id and
400 * @name, return -ENOENT. If @errp != NULL, it will always be filled on
403 int bdrv_snapshot_load_tmp(BlockDriverState
*bs
,
404 const char *snapshot_id
,
408 BlockDriver
*drv
= bs
->drv
;
411 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, bdrv_get_device_name(bs
));
414 if (!snapshot_id
&& !name
) {
415 error_setg(errp
, "snapshot_id and name are both NULL");
418 if (!bdrv_is_read_only(bs
)) {
419 error_setg(errp
, "Device is not readonly");
422 if (drv
->bdrv_snapshot_load_tmp
) {
423 return drv
->bdrv_snapshot_load_tmp(bs
, snapshot_id
, name
, errp
);
425 error_setg(errp
, "Block format '%s' used by device '%s' "
426 "does not support temporarily loading internal snapshots",
427 drv
->format_name
, bdrv_get_device_name(bs
));
431 int bdrv_snapshot_load_tmp_by_id_or_name(BlockDriverState
*bs
,
432 const char *id_or_name
,
436 Error
*local_err
= NULL
;
438 ret
= bdrv_snapshot_load_tmp(bs
, id_or_name
, NULL
, &local_err
);
439 if (ret
== -ENOENT
|| ret
== -EINVAL
) {
440 error_free(local_err
);
442 ret
= bdrv_snapshot_load_tmp(bs
, NULL
, id_or_name
, &local_err
);
445 error_propagate(errp
, local_err
);
451 static int bdrv_all_get_snapshot_devices(bool has_devices
, strList
*devices
,
455 g_autoptr(GList
) bdrvs
= NULL
;
459 error_setg(errp
, "At least one device is required for snapshot");
464 BlockDriverState
*bs
= bdrv_find_node(devices
->value
);
466 error_setg(errp
, "No block device node '%s'", devices
->value
);
469 bdrvs
= g_list_append(bdrvs
, bs
);
470 devices
= devices
->next
;
473 BlockDriverState
*bs
;
475 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
476 bdrvs
= g_list_append(bdrvs
, bs
);
480 *all_bdrvs
= g_steal_pointer(&bdrvs
);
485 static bool bdrv_all_snapshots_includes_bs(BlockDriverState
*bs
)
487 if (!bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
491 /* Include all nodes that are either in use by a BlockBackend, or that
492 * aren't attached to any node, but owned by the monitor. */
493 return bdrv_has_blk(bs
) || QLIST_EMPTY(&bs
->parents
);
496 /* Group operations. All block drivers are involved.
497 * These functions will properly handle dataplane (take aio_context_acquire
498 * when appropriate for appropriate block drivers) */
500 bool bdrv_all_can_snapshot(bool has_devices
, strList
*devices
,
503 g_autoptr(GList
) bdrvs
= NULL
;
506 if (bdrv_all_get_snapshot_devices(has_devices
, devices
, &bdrvs
, errp
) < 0) {
512 BlockDriverState
*bs
= iterbdrvs
->data
;
513 AioContext
*ctx
= bdrv_get_aio_context(bs
);
516 aio_context_acquire(ctx
);
517 if (devices
|| bdrv_all_snapshots_includes_bs(bs
)) {
518 ok
= bdrv_can_snapshot(bs
);
520 aio_context_release(ctx
);
522 error_setg(errp
, "Device '%s' is writable but does not support "
523 "snapshots", bdrv_get_device_or_node_name(bs
));
527 iterbdrvs
= iterbdrvs
->next
;
533 int bdrv_all_delete_snapshot(const char *name
,
534 bool has_devices
, strList
*devices
,
537 g_autoptr(GList
) bdrvs
= NULL
;
540 if (bdrv_all_get_snapshot_devices(has_devices
, devices
, &bdrvs
, errp
) < 0) {
546 BlockDriverState
*bs
= iterbdrvs
->data
;
547 AioContext
*ctx
= bdrv_get_aio_context(bs
);
548 QEMUSnapshotInfo sn1
, *snapshot
= &sn1
;
551 aio_context_acquire(ctx
);
552 if ((devices
|| bdrv_all_snapshots_includes_bs(bs
)) &&
553 bdrv_snapshot_find(bs
, snapshot
, name
) >= 0)
555 ret
= bdrv_snapshot_delete(bs
, snapshot
->id_str
,
556 snapshot
->name
, errp
);
558 aio_context_release(ctx
);
560 error_prepend(errp
, "Could not delete snapshot '%s' on '%s': ",
561 name
, bdrv_get_device_or_node_name(bs
));
565 iterbdrvs
= iterbdrvs
->next
;
572 int bdrv_all_goto_snapshot(const char *name
,
573 bool has_devices
, strList
*devices
,
576 g_autoptr(GList
) bdrvs
= NULL
;
579 if (bdrv_all_get_snapshot_devices(has_devices
, devices
, &bdrvs
, errp
) < 0) {
585 BlockDriverState
*bs
= iterbdrvs
->data
;
586 AioContext
*ctx
= bdrv_get_aio_context(bs
);
589 aio_context_acquire(ctx
);
590 if (devices
|| bdrv_all_snapshots_includes_bs(bs
)) {
591 ret
= bdrv_snapshot_goto(bs
, name
, errp
);
593 aio_context_release(ctx
);
595 error_prepend(errp
, "Could not load snapshot '%s' on '%s': ",
596 name
, bdrv_get_device_or_node_name(bs
));
600 iterbdrvs
= iterbdrvs
->next
;
606 int bdrv_all_has_snapshot(const char *name
,
607 bool has_devices
, strList
*devices
,
610 g_autoptr(GList
) bdrvs
= NULL
;
613 if (bdrv_all_get_snapshot_devices(has_devices
, devices
, &bdrvs
, errp
) < 0) {
619 BlockDriverState
*bs
= iterbdrvs
->data
;
620 AioContext
*ctx
= bdrv_get_aio_context(bs
);
624 aio_context_acquire(ctx
);
625 if (devices
|| bdrv_all_snapshots_includes_bs(bs
)) {
626 ret
= bdrv_snapshot_find(bs
, &sn
, name
);
628 aio_context_release(ctx
);
630 if (ret
== -ENOENT
) {
633 error_setg_errno(errp
, errno
,
634 "Could not check snapshot '%s' on '%s'",
635 name
, bdrv_get_device_or_node_name(bs
));
640 iterbdrvs
= iterbdrvs
->next
;
646 int bdrv_all_create_snapshot(QEMUSnapshotInfo
*sn
,
647 BlockDriverState
*vm_state_bs
,
648 uint64_t vm_state_size
,
649 bool has_devices
, strList
*devices
,
652 g_autoptr(GList
) bdrvs
= NULL
;
655 if (bdrv_all_get_snapshot_devices(has_devices
, devices
, &bdrvs
, errp
) < 0) {
661 BlockDriverState
*bs
= iterbdrvs
->data
;
662 AioContext
*ctx
= bdrv_get_aio_context(bs
);
665 aio_context_acquire(ctx
);
666 if (bs
== vm_state_bs
) {
667 sn
->vm_state_size
= vm_state_size
;
668 ret
= bdrv_snapshot_create(bs
, sn
);
669 } else if (devices
|| bdrv_all_snapshots_includes_bs(bs
)) {
670 sn
->vm_state_size
= 0;
671 ret
= bdrv_snapshot_create(bs
, sn
);
673 aio_context_release(ctx
);
675 error_setg(errp
, "Could not create snapshot '%s' on '%s'",
676 sn
->name
, bdrv_get_device_or_node_name(bs
));
680 iterbdrvs
= iterbdrvs
->next
;
687 BlockDriverState
*bdrv_all_find_vmstate_bs(const char *vmstate_bs
,
688 bool has_devices
, strList
*devices
,
691 g_autoptr(GList
) bdrvs
= NULL
;
694 if (bdrv_all_get_snapshot_devices(has_devices
, devices
, &bdrvs
, errp
) < 0) {
700 BlockDriverState
*bs
= iterbdrvs
->data
;
701 AioContext
*ctx
= bdrv_get_aio_context(bs
);
704 aio_context_acquire(ctx
);
705 found
= (devices
|| bdrv_all_snapshots_includes_bs(bs
)) &&
706 bdrv_can_snapshot(bs
);
707 aio_context_release(ctx
);
710 if (g_str_equal(vmstate_bs
,
711 bdrv_get_node_name(bs
))) {
716 "vmstate block device '%s' does not support snapshots",
725 iterbdrvs
= iterbdrvs
->next
;
730 "vmstate block device '%s' does not exist", vmstate_bs
);
733 "no block device can store vmstate for snapshot");