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
;
167 * The only BdrvChild pointers that are safe to modify (and which
168 * we can thus return a reference to) are bs->file and
171 fallback
= &bs
->file
;
172 if (!*fallback
&& bs
->drv
&& bs
->drv
->is_filter
) {
173 fallback
= &bs
->backing
;
181 * Check that there are no other children that would need to be
182 * snapshotted. If there are, it is not safe to fall back to
185 QLIST_FOREACH(child
, &bs
->children
, next
) {
186 if (child
->role
& (BDRV_CHILD_DATA
| BDRV_CHILD_METADATA
|
187 BDRV_CHILD_FILTERED
) &&
197 static BlockDriverState
*bdrv_snapshot_fallback(BlockDriverState
*bs
)
199 BdrvChild
**child_ptr
= bdrv_snapshot_fallback_ptr(bs
);
200 return child_ptr
? (*child_ptr
)->bs
: NULL
;
203 int bdrv_can_snapshot(BlockDriverState
*bs
)
205 BlockDriver
*drv
= bs
->drv
;
207 if (!drv
|| !bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
211 if (!drv
->bdrv_snapshot_create
) {
212 BlockDriverState
*fallback_bs
= bdrv_snapshot_fallback(bs
);
214 return bdrv_can_snapshot(fallback_bs
);
222 int bdrv_snapshot_create(BlockDriverState
*bs
,
223 QEMUSnapshotInfo
*sn_info
)
225 BlockDriver
*drv
= bs
->drv
;
226 BlockDriverState
*fallback_bs
= bdrv_snapshot_fallback(bs
);
233 if (drv
->bdrv_snapshot_create
) {
234 return drv
->bdrv_snapshot_create(bs
, sn_info
);
237 return bdrv_snapshot_create(fallback_bs
, sn_info
);
242 int bdrv_snapshot_goto(BlockDriverState
*bs
,
243 const char *snapshot_id
,
246 BlockDriver
*drv
= bs
->drv
;
247 BdrvChild
**fallback_ptr
;
253 error_setg(errp
, "Block driver is closed");
257 if (!QLIST_EMPTY(&bs
->dirty_bitmaps
)) {
258 error_setg(errp
, "Device has active dirty bitmaps");
262 if (drv
->bdrv_snapshot_goto
) {
263 ret
= drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
265 error_setg_errno(errp
, -ret
, "Failed to load snapshot");
270 fallback_ptr
= bdrv_snapshot_fallback_ptr(bs
);
274 Error
*local_err
= NULL
;
275 BlockDriverState
*fallback_bs
= (*fallback_ptr
)->bs
;
276 char *subqdict_prefix
= g_strdup_printf("%s.", (*fallback_ptr
)->name
);
278 options
= qdict_clone_shallow(bs
->options
);
280 /* Prevent it from getting deleted when detached from bs */
281 bdrv_ref(fallback_bs
);
283 qdict_extract_subqdict(options
, &file_options
, subqdict_prefix
);
284 qobject_unref(file_options
);
285 g_free(subqdict_prefix
);
287 /* Force .bdrv_open() below to re-attach fallback_bs on *fallback_ptr */
288 qdict_put_str(options
, (*fallback_ptr
)->name
,
289 bdrv_get_node_name(fallback_bs
));
291 /* Now close bs, apply the snapshot on fallback_bs, and re-open bs */
292 if (drv
->bdrv_close
) {
296 /* .bdrv_open() will re-attach it */
297 bdrv_unref_child(bs
, *fallback_ptr
);
298 *fallback_ptr
= NULL
;
300 ret
= bdrv_snapshot_goto(fallback_bs
, snapshot_id
, errp
);
301 open_ret
= drv
->bdrv_open(bs
, options
, bs
->open_flags
, &local_err
);
302 qobject_unref(options
);
304 bdrv_unref(fallback_bs
);
306 /* A bdrv_snapshot_goto() error takes precedence */
307 error_propagate(errp
, local_err
);
308 return ret
< 0 ? ret
: open_ret
;
312 * fallback_ptr is &bs->file or &bs->backing. *fallback_ptr
313 * was closed above and set to NULL, but the .bdrv_open() call
314 * has opened it again, because we set the respective option
315 * (with the qdict_put_str() call above).
316 * Assert that .bdrv_open() has attached some child on
317 * *fallback_ptr, and that it has attached the one we wanted
318 * it to (i.e., fallback_bs).
320 assert(*fallback_ptr
&& fallback_bs
== (*fallback_ptr
)->bs
);
321 bdrv_unref(fallback_bs
);
325 error_setg(errp
, "Block driver does not support snapshots");
330 * Delete an internal snapshot by @snapshot_id and @name.
331 * @bs: block device used in the operation
332 * @snapshot_id: unique snapshot ID, or NULL
333 * @name: snapshot name, or NULL
334 * @errp: location to store error
336 * If both @snapshot_id and @name are specified, delete the first one with
337 * id @snapshot_id and name @name.
338 * If only @snapshot_id is specified, delete the first one with id
340 * If only @name is specified, delete the first one with name @name.
341 * if none is specified, return -EINVAL.
343 * Returns: 0 on success, -errno on failure. If @bs is not inserted, return
344 * -ENOMEDIUM. If @snapshot_id and @name are both NULL, return -EINVAL. If @bs
345 * does not support internal snapshot deletion, return -ENOTSUP. If @bs does
346 * not support parameter @snapshot_id or @name, or one of them is not correctly
347 * specified, return -EINVAL. If @bs can't find one matching @id and @name,
348 * return -ENOENT. If @errp != NULL, it will always be filled with error
349 * message on failure.
351 int bdrv_snapshot_delete(BlockDriverState
*bs
,
352 const char *snapshot_id
,
356 BlockDriver
*drv
= bs
->drv
;
357 BlockDriverState
*fallback_bs
= bdrv_snapshot_fallback(bs
);
363 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, bdrv_get_device_name(bs
));
366 if (!snapshot_id
&& !name
) {
367 error_setg(errp
, "snapshot_id and name are both NULL");
371 /* drain all pending i/o before deleting snapshot */
372 bdrv_drained_begin(bs
);
374 if (drv
->bdrv_snapshot_delete
) {
375 ret
= drv
->bdrv_snapshot_delete(bs
, snapshot_id
, name
, errp
);
376 } else if (fallback_bs
) {
377 ret
= bdrv_snapshot_delete(fallback_bs
, snapshot_id
, name
, errp
);
379 error_setg(errp
, "Block format '%s' used by device '%s' "
380 "does not support internal snapshot deletion",
381 drv
->format_name
, bdrv_get_device_name(bs
));
385 bdrv_drained_end(bs
);
389 int bdrv_snapshot_list(BlockDriverState
*bs
,
390 QEMUSnapshotInfo
**psn_info
)
392 BlockDriver
*drv
= bs
->drv
;
393 BlockDriverState
*fallback_bs
= bdrv_snapshot_fallback(bs
);
399 if (drv
->bdrv_snapshot_list
) {
400 return drv
->bdrv_snapshot_list(bs
, psn_info
);
403 return bdrv_snapshot_list(fallback_bs
, psn_info
);
409 * Temporarily load an internal snapshot by @snapshot_id and @name.
410 * @bs: block device used in the operation
411 * @snapshot_id: unique snapshot ID, or NULL
412 * @name: snapshot name, or NULL
413 * @errp: location to store error
415 * If both @snapshot_id and @name are specified, load the first one with
416 * id @snapshot_id and name @name.
417 * If only @snapshot_id is specified, load the first one with id
419 * If only @name is specified, load the first one with name @name.
420 * if none is specified, return -EINVAL.
422 * Returns: 0 on success, -errno on fail. If @bs is not inserted, return
423 * -ENOMEDIUM. If @bs is not readonly, return -EINVAL. If @bs did not support
424 * internal snapshot, return -ENOTSUP. If qemu can't find a matching @id and
425 * @name, return -ENOENT. If @errp != NULL, it will always be filled on
428 int bdrv_snapshot_load_tmp(BlockDriverState
*bs
,
429 const char *snapshot_id
,
433 BlockDriver
*drv
= bs
->drv
;
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 bdrv_all_get_snapshot_devices(bool has_devices
, strList
*devices
,
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 bdrv_all_snapshots_includes_bs(BlockDriverState
*bs
)
516 if (!bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
520 /* Include all nodes that are either in use by a BlockBackend, or that
521 * aren't attached to any node, but owned by the monitor. */
522 return bdrv_has_blk(bs
) || QLIST_EMPTY(&bs
->parents
);
525 /* Group operations. All block drivers are involved.
526 * These functions will properly handle dataplane (take aio_context_acquire
527 * when appropriate for appropriate block drivers) */
529 bool bdrv_all_can_snapshot(bool has_devices
, strList
*devices
,
532 g_autoptr(GList
) bdrvs
= NULL
;
537 if (bdrv_all_get_snapshot_devices(has_devices
, devices
, &bdrvs
, errp
) < 0) {
543 BlockDriverState
*bs
= iterbdrvs
->data
;
544 AioContext
*ctx
= bdrv_get_aio_context(bs
);
547 aio_context_acquire(ctx
);
548 if (devices
|| bdrv_all_snapshots_includes_bs(bs
)) {
549 ok
= bdrv_can_snapshot(bs
);
551 aio_context_release(ctx
);
553 error_setg(errp
, "Device '%s' is writable but does not support "
554 "snapshots", bdrv_get_device_or_node_name(bs
));
558 iterbdrvs
= iterbdrvs
->next
;
564 int bdrv_all_delete_snapshot(const char *name
,
565 bool has_devices
, strList
*devices
,
568 g_autoptr(GList
) bdrvs
= NULL
;
573 if (bdrv_all_get_snapshot_devices(has_devices
, devices
, &bdrvs
, errp
) < 0) {
579 BlockDriverState
*bs
= iterbdrvs
->data
;
580 AioContext
*ctx
= bdrv_get_aio_context(bs
);
581 QEMUSnapshotInfo sn1
, *snapshot
= &sn1
;
584 aio_context_acquire(ctx
);
585 if ((devices
|| bdrv_all_snapshots_includes_bs(bs
)) &&
586 bdrv_snapshot_find(bs
, snapshot
, name
) >= 0)
588 ret
= bdrv_snapshot_delete(bs
, snapshot
->id_str
,
589 snapshot
->name
, errp
);
591 aio_context_release(ctx
);
593 error_prepend(errp
, "Could not delete snapshot '%s' on '%s': ",
594 name
, bdrv_get_device_or_node_name(bs
));
598 iterbdrvs
= iterbdrvs
->next
;
605 int bdrv_all_goto_snapshot(const char *name
,
606 bool has_devices
, strList
*devices
,
609 g_autoptr(GList
) bdrvs
= NULL
;
614 if (bdrv_all_get_snapshot_devices(has_devices
, devices
, &bdrvs
, errp
) < 0) {
620 BlockDriverState
*bs
= iterbdrvs
->data
;
621 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_goto(bs
, name
, errp
);
628 aio_context_release(ctx
);
630 error_prepend(errp
, "Could not load snapshot '%s' on '%s': ",
631 name
, bdrv_get_device_or_node_name(bs
));
635 iterbdrvs
= iterbdrvs
->next
;
641 int bdrv_all_has_snapshot(const char *name
,
642 bool has_devices
, strList
*devices
,
645 g_autoptr(GList
) bdrvs
= NULL
;
650 if (bdrv_all_get_snapshot_devices(has_devices
, devices
, &bdrvs
, errp
) < 0) {
656 BlockDriverState
*bs
= iterbdrvs
->data
;
657 AioContext
*ctx
= bdrv_get_aio_context(bs
);
661 aio_context_acquire(ctx
);
662 if (devices
|| bdrv_all_snapshots_includes_bs(bs
)) {
663 ret
= bdrv_snapshot_find(bs
, &sn
, name
);
665 aio_context_release(ctx
);
667 if (ret
== -ENOENT
) {
670 error_setg_errno(errp
, errno
,
671 "Could not check snapshot '%s' on '%s'",
672 name
, bdrv_get_device_or_node_name(bs
));
677 iterbdrvs
= iterbdrvs
->next
;
683 int bdrv_all_create_snapshot(QEMUSnapshotInfo
*sn
,
684 BlockDriverState
*vm_state_bs
,
685 uint64_t vm_state_size
,
686 bool has_devices
, strList
*devices
,
689 g_autoptr(GList
) bdrvs
= NULL
;
693 if (bdrv_all_get_snapshot_devices(has_devices
, devices
, &bdrvs
, errp
) < 0) {
699 BlockDriverState
*bs
= iterbdrvs
->data
;
700 AioContext
*ctx
= bdrv_get_aio_context(bs
);
703 aio_context_acquire(ctx
);
704 if (bs
== vm_state_bs
) {
705 sn
->vm_state_size
= vm_state_size
;
706 ret
= bdrv_snapshot_create(bs
, sn
);
707 } else if (devices
|| bdrv_all_snapshots_includes_bs(bs
)) {
708 sn
->vm_state_size
= 0;
709 ret
= bdrv_snapshot_create(bs
, sn
);
711 aio_context_release(ctx
);
713 error_setg(errp
, "Could not create snapshot '%s' on '%s'",
714 sn
->name
, bdrv_get_device_or_node_name(bs
));
718 iterbdrvs
= iterbdrvs
->next
;
725 BlockDriverState
*bdrv_all_find_vmstate_bs(const char *vmstate_bs
,
726 bool has_devices
, strList
*devices
,
729 g_autoptr(GList
) bdrvs
= NULL
;
734 if (bdrv_all_get_snapshot_devices(has_devices
, devices
, &bdrvs
, errp
) < 0) {
740 BlockDriverState
*bs
= iterbdrvs
->data
;
741 AioContext
*ctx
= bdrv_get_aio_context(bs
);
744 aio_context_acquire(ctx
);
745 found
= (devices
|| bdrv_all_snapshots_includes_bs(bs
)) &&
746 bdrv_can_snapshot(bs
);
747 aio_context_release(ctx
);
750 if (g_str_equal(vmstate_bs
,
751 bdrv_get_node_name(bs
))) {
756 "vmstate block device '%s' does not support snapshots",
765 iterbdrvs
= iterbdrvs
->next
;
770 "vmstate block device '%s' does not exist", vmstate_bs
);
773 "no block device can store vmstate for snapshot");