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/qstring.h"
32 #include "qemu/option.h"
33 #include "sysemu/block-backend.h"
35 QemuOptsList internal_snapshot_opts
= {
37 .head
= QTAILQ_HEAD_INITIALIZER(internal_snapshot_opts
.head
),
40 .name
= SNAPSHOT_OPT_ID
,
41 .type
= QEMU_OPT_STRING
,
44 .name
= SNAPSHOT_OPT_NAME
,
45 .type
= QEMU_OPT_STRING
,
46 .help
= "snapshot name"
53 int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
56 QEMUSnapshotInfo
*sn_tab
, *sn
;
62 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
66 for (i
= 0; i
< nb_sns
; i
++) {
68 if (!strcmp(sn
->name
, name
)) {
79 * Look up an internal snapshot by @id and @name.
80 * @bs: block device to search
81 * @id: unique snapshot ID, or NULL
82 * @name: snapshot name, or NULL
83 * @sn_info: location to store information on the snapshot found
84 * @errp: location to store error, will be set only for exception
86 * This function will traverse snapshot list in @bs to search the matching
87 * one, @id and @name are the matching condition:
88 * If both @id and @name are specified, find the first one with id @id and
90 * If only @id is specified, find the first one with id @id.
91 * If only @name is specified, find the first one with name @name.
92 * if none is specified, abort().
94 * Returns: true when a snapshot is found and @sn_info will be filled, false
95 * when error or not found. If all operation succeed but no matching one is
96 * found, @errp will NOT be set.
98 bool bdrv_snapshot_find_by_id_and_name(BlockDriverState
*bs
,
101 QEMUSnapshotInfo
*sn_info
,
104 QEMUSnapshotInfo
*sn_tab
, *sn
;
111 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
113 error_setg_errno(errp
, -nb_sns
, "Failed to get a snapshot list");
115 } else if (nb_sns
== 0) {
120 for (i
= 0; i
< nb_sns
; i
++) {
122 if (!strcmp(sn
->id_str
, id
) && !strcmp(sn
->name
, name
)) {
129 for (i
= 0; i
< nb_sns
; i
++) {
131 if (!strcmp(sn
->id_str
, id
)) {
138 for (i
= 0; i
< nb_sns
; i
++) {
140 if (!strcmp(sn
->name
, name
)) {
153 * Return a pointer to child of given BDS to which we can fall
154 * back if the given BDS does not support snapshots.
155 * Return NULL if there is no BDS to (safely) fall back to.
157 static BdrvChild
* GRAPH_RDLOCK
158 bdrv_snapshot_fallback_child(BlockDriverState
*bs
)
160 BdrvChild
*fallback
= bdrv_primary_child(bs
);
164 assert_bdrv_graph_readable();
166 /* We allow fallback only to primary child */
172 * Check that there are no other children that would need to be
173 * snapshotted. If there are, it is not safe to fall back to
176 QLIST_FOREACH(child
, &bs
->children
, next
) {
177 if (child
->role
& (BDRV_CHILD_DATA
| BDRV_CHILD_METADATA
|
178 BDRV_CHILD_FILTERED
) &&
188 static BlockDriverState
* GRAPH_RDLOCK
189 bdrv_snapshot_fallback(BlockDriverState
*bs
)
192 return child_bs(bdrv_snapshot_fallback_child(bs
));
195 int bdrv_can_snapshot(BlockDriverState
*bs
)
197 BlockDriver
*drv
= bs
->drv
;
201 if (!drv
|| !bdrv_is_inserted(bs
) || !bdrv_is_writable(bs
)) {
205 if (!drv
->bdrv_snapshot_create
) {
206 BlockDriverState
*fallback_bs
= bdrv_snapshot_fallback(bs
);
208 return bdrv_can_snapshot(fallback_bs
);
216 int bdrv_snapshot_create(BlockDriverState
*bs
,
217 QEMUSnapshotInfo
*sn_info
)
219 BlockDriver
*drv
= bs
->drv
;
220 BlockDriverState
*fallback_bs
= bdrv_snapshot_fallback(bs
);
227 if (drv
->bdrv_snapshot_create
) {
228 return drv
->bdrv_snapshot_create(bs
, sn_info
);
231 return bdrv_snapshot_create(fallback_bs
, sn_info
);
236 int bdrv_snapshot_goto(BlockDriverState
*bs
,
237 const char *snapshot_id
,
240 BlockDriver
*drv
= bs
->drv
;
247 error_setg(errp
, "Block driver is closed");
251 if (!QLIST_EMPTY(&bs
->dirty_bitmaps
)) {
252 error_setg(errp
, "Device has active dirty bitmaps");
256 if (drv
->bdrv_snapshot_goto
) {
257 ret
= drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
259 error_setg_errno(errp
, -ret
, "Failed to load snapshot");
264 bdrv_graph_rdlock_main_loop();
265 fallback
= bdrv_snapshot_fallback_child(bs
);
266 bdrv_graph_rdunlock_main_loop();
271 Error
*local_err
= NULL
;
272 BlockDriverState
*fallback_bs
= fallback
->bs
;
273 char *subqdict_prefix
= g_strdup_printf("%s.", fallback
->name
);
275 options
= qdict_clone_shallow(bs
->options
);
277 /* Prevent it from getting deleted when detached from bs */
278 bdrv_ref(fallback_bs
);
280 qdict_extract_subqdict(options
, &file_options
, subqdict_prefix
);
281 qobject_unref(file_options
);
282 g_free(subqdict_prefix
);
284 /* Force .bdrv_open() below to re-attach fallback_bs on fallback */
285 qdict_put_str(options
, fallback
->name
,
286 bdrv_get_node_name(fallback_bs
));
288 /* Now close bs, apply the snapshot on fallback_bs, and re-open bs */
289 if (drv
->bdrv_close
) {
293 /* .bdrv_open() will re-attach it */
295 bdrv_unref_child(bs
, fallback
);
296 bdrv_graph_wrunlock();
298 ret
= bdrv_snapshot_goto(fallback_bs
, snapshot_id
, errp
);
299 open_ret
= drv
->bdrv_open(bs
, options
, bs
->open_flags
, &local_err
);
300 qobject_unref(options
);
302 bdrv_unref(fallback_bs
);
304 /* A bdrv_snapshot_goto() error takes precedence */
305 error_propagate(errp
, local_err
);
306 return ret
< 0 ? ret
: open_ret
;
310 * fallback was a primary child. It was closed above and set to NULL,
311 * but the .bdrv_open() call has opened it again, because we set the
312 * respective option (with the qdict_put_str() call above).
313 * Assert that .bdrv_open() has attached the right BDS as primary child.
315 bdrv_graph_rdlock_main_loop();
316 assert(bdrv_primary_bs(bs
) == fallback_bs
);
317 bdrv_graph_rdunlock_main_loop();
319 bdrv_unref(fallback_bs
);
323 error_setg(errp
, "Block driver does not support snapshots");
328 * Delete an internal snapshot by @snapshot_id and @name.
329 * @bs: block device used in the operation
330 * @snapshot_id: unique snapshot ID, or NULL
331 * @name: snapshot name, or NULL
332 * @errp: location to store error
334 * If both @snapshot_id and @name are specified, delete the first one with
335 * id @snapshot_id and name @name.
336 * If only @snapshot_id is specified, delete the first one with id
338 * If only @name is specified, delete the first one with name @name.
339 * if none is specified, return -EINVAL.
341 * Returns: 0 on success, -errno on failure. If @bs is not inserted, return
342 * -ENOMEDIUM. If @snapshot_id and @name are both NULL, return -EINVAL. If @bs
343 * does not support internal snapshot deletion, return -ENOTSUP. If @bs does
344 * not support parameter @snapshot_id or @name, or one of them is not correctly
345 * specified, return -EINVAL. If @bs can't find one matching @id and @name,
346 * return -ENOENT. If @errp != NULL, it will always be filled with error
347 * message on failure.
349 int bdrv_snapshot_delete(BlockDriverState
*bs
,
350 const char *snapshot_id
,
354 BlockDriver
*drv
= bs
->drv
;
355 BlockDriverState
*fallback_bs
= bdrv_snapshot_fallback(bs
);
361 error_setg(errp
, "Device '%s' has no medium",
362 bdrv_get_device_name(bs
));
365 if (!snapshot_id
&& !name
) {
366 error_setg(errp
, "snapshot_id and name are both NULL");
370 /* drain all pending i/o before deleting snapshot */
371 bdrv_drained_begin(bs
);
373 if (drv
->bdrv_snapshot_delete
) {
374 ret
= drv
->bdrv_snapshot_delete(bs
, snapshot_id
, name
, errp
);
375 } else if (fallback_bs
) {
376 ret
= bdrv_snapshot_delete(fallback_bs
, snapshot_id
, name
, errp
);
378 error_setg(errp
, "Block format '%s' used by device '%s' "
379 "does not support internal snapshot deletion",
380 drv
->format_name
, bdrv_get_device_name(bs
));
384 bdrv_drained_end(bs
);
388 int bdrv_snapshot_list(BlockDriverState
*bs
,
389 QEMUSnapshotInfo
**psn_info
)
392 GRAPH_RDLOCK_GUARD_MAINLOOP();
394 BlockDriver
*drv
= bs
->drv
;
395 BlockDriverState
*fallback_bs
= bdrv_snapshot_fallback(bs
);
400 if (drv
->bdrv_snapshot_list
) {
401 return drv
->bdrv_snapshot_list(bs
, psn_info
);
404 return bdrv_snapshot_list(fallback_bs
, psn_info
);
410 * Temporarily load an internal snapshot by @snapshot_id and @name.
411 * @bs: block device used in the operation
412 * @snapshot_id: unique snapshot ID, or NULL
413 * @name: snapshot name, or NULL
414 * @errp: location to store error
416 * If both @snapshot_id and @name are specified, load the first one with
417 * id @snapshot_id and name @name.
418 * If only @snapshot_id is specified, load the first one with id
420 * If only @name is specified, load the first one with name @name.
421 * if none is specified, return -EINVAL.
423 * Returns: 0 on success, -errno on fail. If @bs is not inserted, return
424 * -ENOMEDIUM. If @bs is not readonly, return -EINVAL. If @bs did not support
425 * internal snapshot, return -ENOTSUP. If qemu can't find a matching @id and
426 * @name, return -ENOENT. If @errp != NULL, it will always be filled on
429 int bdrv_snapshot_load_tmp(BlockDriverState
*bs
,
430 const char *snapshot_id
,
434 BlockDriver
*drv
= bs
->drv
;
437 GRAPH_RDLOCK_GUARD_MAINLOOP();
440 error_setg(errp
, "Device '%s' has no medium",
441 bdrv_get_device_name(bs
));
444 if (!snapshot_id
&& !name
) {
445 error_setg(errp
, "snapshot_id and name are both NULL");
448 if (!bdrv_is_read_only(bs
)) {
449 error_setg(errp
, "Device is not readonly");
452 if (drv
->bdrv_snapshot_load_tmp
) {
453 return drv
->bdrv_snapshot_load_tmp(bs
, snapshot_id
, name
, errp
);
455 error_setg(errp
, "Block format '%s' used by device '%s' "
456 "does not support temporarily loading internal snapshots",
457 drv
->format_name
, bdrv_get_device_name(bs
));
461 int bdrv_snapshot_load_tmp_by_id_or_name(BlockDriverState
*bs
,
462 const char *id_or_name
,
466 Error
*local_err
= NULL
;
470 ret
= bdrv_snapshot_load_tmp(bs
, id_or_name
, NULL
, &local_err
);
471 if (ret
== -ENOENT
|| ret
== -EINVAL
) {
472 error_free(local_err
);
474 ret
= bdrv_snapshot_load_tmp(bs
, NULL
, id_or_name
, &local_err
);
477 error_propagate(errp
, local_err
);
483 static int GRAPH_RDLOCK
484 bdrv_all_get_snapshot_devices(bool has_devices
, strList
*devices
,
485 GList
**all_bdrvs
, Error
**errp
)
487 g_autoptr(GList
) bdrvs
= NULL
;
491 error_setg(errp
, "At least one device is required for snapshot");
496 BlockDriverState
*bs
= bdrv_find_node(devices
->value
);
498 error_setg(errp
, "No block device node '%s'", devices
->value
);
501 bdrvs
= g_list_append(bdrvs
, bs
);
502 devices
= devices
->next
;
505 BlockDriverState
*bs
;
507 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
508 bdrvs
= g_list_append(bdrvs
, bs
);
512 *all_bdrvs
= g_steal_pointer(&bdrvs
);
517 static bool GRAPH_RDLOCK
bdrv_all_snapshots_includes_bs(BlockDriverState
*bs
)
520 assert_bdrv_graph_readable();
522 if (!bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
526 /* Include all nodes that are either in use by a BlockBackend, or that
527 * aren't attached to any node, but owned by the monitor. */
528 return bdrv_has_blk(bs
) || QLIST_EMPTY(&bs
->parents
);
531 /* Group operations. All block drivers are involved. */
533 bool bdrv_all_can_snapshot(bool has_devices
, strList
*devices
,
536 g_autoptr(GList
) bdrvs
= NULL
;
540 GRAPH_RDLOCK_GUARD_MAINLOOP();
542 if (bdrv_all_get_snapshot_devices(has_devices
, devices
, &bdrvs
, errp
) < 0) {
548 BlockDriverState
*bs
= iterbdrvs
->data
;
551 if (devices
|| bdrv_all_snapshots_includes_bs(bs
)) {
552 ok
= bdrv_can_snapshot(bs
);
555 error_setg(errp
, "Device '%s' is writable but does not support "
556 "snapshots", bdrv_get_device_or_node_name(bs
));
560 iterbdrvs
= iterbdrvs
->next
;
566 int bdrv_all_delete_snapshot(const char *name
,
567 bool has_devices
, strList
*devices
,
571 g_autoptr(GList
) bdrvs
= NULL
;
575 GRAPH_RDLOCK_GUARD_MAINLOOP();
577 if (bdrv_all_get_snapshot_devices(has_devices
, devices
, &bdrvs
, errp
) < 0) {
583 BlockDriverState
*bs
= iterbdrvs
->data
;
584 QEMUSnapshotInfo sn1
, *snapshot
= &sn1
;
587 if ((devices
|| bdrv_all_snapshots_includes_bs(bs
)) &&
588 bdrv_snapshot_find(bs
, snapshot
, name
) >= 0)
590 ret
= bdrv_snapshot_delete(bs
, snapshot
->id_str
,
591 snapshot
->name
, errp
);
594 error_prepend(errp
, "Could not delete snapshot '%s' on '%s': ",
595 name
, bdrv_get_device_or_node_name(bs
));
599 iterbdrvs
= iterbdrvs
->next
;
606 int bdrv_all_goto_snapshot(const char *name
,
607 bool has_devices
, strList
*devices
,
611 g_autoptr(GList
) bdrvs
= NULL
;
617 bdrv_graph_rdlock_main_loop();
618 ret
= bdrv_all_get_snapshot_devices(has_devices
, devices
, &bdrvs
, errp
);
619 bdrv_graph_rdunlock_main_loop();
627 BlockDriverState
*bs
= iterbdrvs
->data
;
628 bool all_snapshots_includes_bs
;
630 bdrv_graph_rdlock_main_loop();
631 all_snapshots_includes_bs
= bdrv_all_snapshots_includes_bs(bs
);
632 bdrv_graph_rdunlock_main_loop();
634 ret
= (devices
|| all_snapshots_includes_bs
) ?
635 bdrv_snapshot_goto(bs
, name
, errp
) : 0;
637 bdrv_graph_rdlock_main_loop();
638 error_prepend(errp
, "Could not load snapshot '%s' on '%s': ",
639 name
, bdrv_get_device_or_node_name(bs
));
640 bdrv_graph_rdunlock_main_loop();
644 iterbdrvs
= iterbdrvs
->next
;
650 int bdrv_all_has_snapshot(const char *name
,
651 bool has_devices
, strList
*devices
,
654 g_autoptr(GList
) bdrvs
= NULL
;
658 GRAPH_RDLOCK_GUARD_MAINLOOP();
660 if (bdrv_all_get_snapshot_devices(has_devices
, devices
, &bdrvs
, errp
) < 0) {
666 BlockDriverState
*bs
= iterbdrvs
->data
;
670 if (devices
|| bdrv_all_snapshots_includes_bs(bs
)) {
671 ret
= bdrv_snapshot_find(bs
, &sn
, name
);
674 if (ret
== -ENOENT
) {
677 error_setg_errno(errp
, errno
,
678 "Could not check snapshot '%s' on '%s'",
679 name
, bdrv_get_device_or_node_name(bs
));
684 iterbdrvs
= iterbdrvs
->next
;
690 int bdrv_all_create_snapshot(QEMUSnapshotInfo
*sn
,
691 BlockDriverState
*vm_state_bs
,
692 uint64_t vm_state_size
,
693 bool has_devices
, strList
*devices
,
696 g_autoptr(GList
) bdrvs
= NULL
;
700 GRAPH_RDLOCK_GUARD_MAINLOOP();
702 if (bdrv_all_get_snapshot_devices(has_devices
, devices
, &bdrvs
, errp
) < 0) {
708 BlockDriverState
*bs
= iterbdrvs
->data
;
711 if (bs
== vm_state_bs
) {
712 sn
->vm_state_size
= vm_state_size
;
713 ret
= bdrv_snapshot_create(bs
, sn
);
714 } else if (devices
|| bdrv_all_snapshots_includes_bs(bs
)) {
715 sn
->vm_state_size
= 0;
716 ret
= bdrv_snapshot_create(bs
, sn
);
719 error_setg(errp
, "Could not create snapshot '%s' on '%s'",
720 sn
->name
, bdrv_get_device_or_node_name(bs
));
724 iterbdrvs
= iterbdrvs
->next
;
731 BlockDriverState
*bdrv_all_find_vmstate_bs(const char *vmstate_bs
,
732 bool has_devices
, strList
*devices
,
735 g_autoptr(GList
) bdrvs
= NULL
;
739 GRAPH_RDLOCK_GUARD_MAINLOOP();
741 if (bdrv_all_get_snapshot_devices(has_devices
, devices
, &bdrvs
, errp
) < 0) {
747 BlockDriverState
*bs
= iterbdrvs
->data
;
750 found
= (devices
|| bdrv_all_snapshots_includes_bs(bs
)) &&
751 bdrv_can_snapshot(bs
);
754 if (g_str_equal(vmstate_bs
,
755 bdrv_get_node_name(bs
))) {
760 "vmstate block device '%s' does not support snapshots",
769 iterbdrvs
= iterbdrvs
->next
;
774 "vmstate block device '%s' does not exist", vmstate_bs
);
777 "no block device can store vmstate for snapshot");