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 "qapi/error.h"
29 #include "qapi/qmp/qerror.h"
30 #include "qapi/qmp/qstring.h"
32 QemuOptsList internal_snapshot_opts
= {
34 .head
= QTAILQ_HEAD_INITIALIZER(internal_snapshot_opts
.head
),
37 .name
= SNAPSHOT_OPT_ID
,
38 .type
= QEMU_OPT_STRING
,
41 .name
= SNAPSHOT_OPT_NAME
,
42 .type
= QEMU_OPT_STRING
,
43 .help
= "snapshot name"
50 int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
53 QEMUSnapshotInfo
*sn_tab
, *sn
;
57 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
61 for (i
= 0; i
< nb_sns
; i
++) {
63 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
74 * Look up an internal snapshot by @id and @name.
75 * @bs: block device to search
76 * @id: unique snapshot ID, or NULL
77 * @name: snapshot name, or NULL
78 * @sn_info: location to store information on the snapshot found
79 * @errp: location to store error, will be set only for exception
81 * This function will traverse snapshot list in @bs to search the matching
82 * one, @id and @name are the matching condition:
83 * If both @id and @name are specified, find the first one with id @id and
85 * If only @id is specified, find the first one with id @id.
86 * If only @name is specified, find the first one with name @name.
87 * if none is specified, abort().
89 * Returns: true when a snapshot is found and @sn_info will be filled, false
90 * when error or not found. If all operation succeed but no matching one is
91 * found, @errp will NOT be set.
93 bool bdrv_snapshot_find_by_id_and_name(BlockDriverState
*bs
,
96 QEMUSnapshotInfo
*sn_info
,
99 QEMUSnapshotInfo
*sn_tab
, *sn
;
105 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
107 error_setg_errno(errp
, -nb_sns
, "Failed to get a snapshot list");
109 } else if (nb_sns
== 0) {
114 for (i
= 0; i
< nb_sns
; i
++) {
116 if (!strcmp(sn
->id_str
, id
) && !strcmp(sn
->name
, name
)) {
123 for (i
= 0; i
< nb_sns
; i
++) {
125 if (!strcmp(sn
->id_str
, id
)) {
132 for (i
= 0; i
< nb_sns
; i
++) {
134 if (!strcmp(sn
->name
, name
)) {
146 int bdrv_can_snapshot(BlockDriverState
*bs
)
148 BlockDriver
*drv
= bs
->drv
;
149 if (!drv
|| !bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
153 if (!drv
->bdrv_snapshot_create
) {
154 if (bs
->file
!= NULL
) {
155 return bdrv_can_snapshot(bs
->file
->bs
);
163 int bdrv_snapshot_create(BlockDriverState
*bs
,
164 QEMUSnapshotInfo
*sn_info
)
166 BlockDriver
*drv
= bs
->drv
;
170 if (drv
->bdrv_snapshot_create
) {
171 return drv
->bdrv_snapshot_create(bs
, sn_info
);
174 return bdrv_snapshot_create(bs
->file
->bs
, sn_info
);
179 int bdrv_snapshot_goto(BlockDriverState
*bs
,
180 const char *snapshot_id
)
182 BlockDriver
*drv
= bs
->drv
;
190 len
= bdrv_getlength(bs
);
194 /* We should set all bits in all enabled dirty bitmaps, because dirty
195 * bitmaps reflect active state of disk and snapshot switch operation
196 * actually dirties active state.
197 * TODO: It may make sense not to set all bits but analyze block status of
198 * current state and destination snapshot and do not set bits corresponding
199 * to both-zero or both-unallocated areas. */
200 bdrv_set_dirty(bs
, 0, len
);
202 if (drv
->bdrv_snapshot_goto
) {
203 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
207 BlockDriverState
*file
;
208 QDict
*options
= qdict_clone_shallow(bs
->options
);
212 /* Prevent it from getting deleted when detached from bs */
215 qdict_extract_subqdict(options
, &file_options
, "file.");
216 QDECREF(file_options
);
217 qdict_put_str(options
, "file", bdrv_get_node_name(file
));
220 bdrv_unref_child(bs
, bs
->file
);
223 ret
= bdrv_snapshot_goto(file
, snapshot_id
);
224 open_ret
= drv
->bdrv_open(bs
, options
, bs
->open_flags
, NULL
);
232 assert(bs
->file
->bs
== file
);
241 * Delete an internal snapshot by @snapshot_id and @name.
242 * @bs: block device used in the operation
243 * @snapshot_id: unique snapshot ID, or NULL
244 * @name: snapshot name, or NULL
245 * @errp: location to store error
247 * If both @snapshot_id and @name are specified, delete the first one with
248 * id @snapshot_id and name @name.
249 * If only @snapshot_id is specified, delete the first one with id
251 * If only @name is specified, delete the first one with name @name.
252 * if none is specified, return -EINVAL.
254 * Returns: 0 on success, -errno on failure. If @bs is not inserted, return
255 * -ENOMEDIUM. If @snapshot_id and @name are both NULL, return -EINVAL. If @bs
256 * does not support internal snapshot deletion, return -ENOTSUP. If @bs does
257 * not support parameter @snapshot_id or @name, or one of them is not correctly
258 * specified, return -EINVAL. If @bs can't find one matching @id and @name,
259 * return -ENOENT. If @errp != NULL, it will always be filled with error
260 * message on failure.
262 int bdrv_snapshot_delete(BlockDriverState
*bs
,
263 const char *snapshot_id
,
267 BlockDriver
*drv
= bs
->drv
;
271 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, bdrv_get_device_name(bs
));
274 if (!snapshot_id
&& !name
) {
275 error_setg(errp
, "snapshot_id and name are both NULL");
279 /* drain all pending i/o before deleting snapshot */
280 bdrv_drained_begin(bs
);
282 if (drv
->bdrv_snapshot_delete
) {
283 ret
= drv
->bdrv_snapshot_delete(bs
, snapshot_id
, name
, errp
);
284 } else if (bs
->file
) {
285 ret
= bdrv_snapshot_delete(bs
->file
->bs
, snapshot_id
, name
, errp
);
287 error_setg(errp
, "Block format '%s' used by device '%s' "
288 "does not support internal snapshot deletion",
289 drv
->format_name
, bdrv_get_device_name(bs
));
293 bdrv_drained_end(bs
);
297 int bdrv_snapshot_delete_by_id_or_name(BlockDriverState
*bs
,
298 const char *id_or_name
,
302 Error
*local_err
= NULL
;
304 ret
= bdrv_snapshot_delete(bs
, id_or_name
, NULL
, &local_err
);
305 if (ret
== -ENOENT
|| ret
== -EINVAL
) {
306 error_free(local_err
);
308 ret
= bdrv_snapshot_delete(bs
, NULL
, id_or_name
, &local_err
);
312 error_propagate(errp
, local_err
);
317 int bdrv_snapshot_list(BlockDriverState
*bs
,
318 QEMUSnapshotInfo
**psn_info
)
320 BlockDriver
*drv
= bs
->drv
;
324 if (drv
->bdrv_snapshot_list
) {
325 return drv
->bdrv_snapshot_list(bs
, psn_info
);
328 return bdrv_snapshot_list(bs
->file
->bs
, psn_info
);
334 * Temporarily load an internal snapshot by @snapshot_id and @name.
335 * @bs: block device used in the operation
336 * @snapshot_id: unique snapshot ID, or NULL
337 * @name: snapshot name, or NULL
338 * @errp: location to store error
340 * If both @snapshot_id and @name are specified, load the first one with
341 * id @snapshot_id and name @name.
342 * If only @snapshot_id is specified, load the first one with id
344 * If only @name is specified, load the first one with name @name.
345 * if none is specified, return -EINVAL.
347 * Returns: 0 on success, -errno on fail. If @bs is not inserted, return
348 * -ENOMEDIUM. If @bs is not readonly, return -EINVAL. If @bs did not support
349 * internal snapshot, return -ENOTSUP. If qemu can't find a matching @id and
350 * @name, return -ENOENT. If @errp != NULL, it will always be filled on
353 int bdrv_snapshot_load_tmp(BlockDriverState
*bs
,
354 const char *snapshot_id
,
358 BlockDriver
*drv
= bs
->drv
;
361 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, bdrv_get_device_name(bs
));
364 if (!snapshot_id
&& !name
) {
365 error_setg(errp
, "snapshot_id and name are both NULL");
368 if (!bs
->read_only
) {
369 error_setg(errp
, "Device is not readonly");
372 if (drv
->bdrv_snapshot_load_tmp
) {
373 return drv
->bdrv_snapshot_load_tmp(bs
, snapshot_id
, name
, errp
);
375 error_setg(errp
, "Block format '%s' used by device '%s' "
376 "does not support temporarily loading internal snapshots",
377 drv
->format_name
, bdrv_get_device_name(bs
));
381 int bdrv_snapshot_load_tmp_by_id_or_name(BlockDriverState
*bs
,
382 const char *id_or_name
,
386 Error
*local_err
= NULL
;
388 ret
= bdrv_snapshot_load_tmp(bs
, id_or_name
, NULL
, &local_err
);
389 if (ret
== -ENOENT
|| ret
== -EINVAL
) {
390 error_free(local_err
);
392 ret
= bdrv_snapshot_load_tmp(bs
, NULL
, id_or_name
, &local_err
);
395 error_propagate(errp
, local_err
);
401 /* Group operations. All block drivers are involved.
402 * These functions will properly handle dataplane (take aio_context_acquire
403 * when appropriate for appropriate block drivers) */
405 bool bdrv_all_can_snapshot(BlockDriverState
**first_bad_bs
)
408 BlockDriverState
*bs
;
411 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
412 AioContext
*ctx
= bdrv_get_aio_context(bs
);
414 aio_context_acquire(ctx
);
415 if (bdrv_is_inserted(bs
) && !bdrv_is_read_only(bs
)) {
416 ok
= bdrv_can_snapshot(bs
);
418 aio_context_release(ctx
);
420 bdrv_next_cleanup(&it
);
430 int bdrv_all_delete_snapshot(const char *name
, BlockDriverState
**first_bad_bs
,
434 BlockDriverState
*bs
;
436 QEMUSnapshotInfo sn1
, *snapshot
= &sn1
;
438 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
439 AioContext
*ctx
= bdrv_get_aio_context(bs
);
441 aio_context_acquire(ctx
);
442 if (bdrv_can_snapshot(bs
) &&
443 bdrv_snapshot_find(bs
, snapshot
, name
) >= 0) {
444 ret
= bdrv_snapshot_delete_by_id_or_name(bs
, name
, err
);
446 aio_context_release(ctx
);
448 bdrv_next_cleanup(&it
);
459 int bdrv_all_goto_snapshot(const char *name
, BlockDriverState
**first_bad_bs
)
462 BlockDriverState
*bs
;
465 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
466 AioContext
*ctx
= bdrv_get_aio_context(bs
);
468 aio_context_acquire(ctx
);
469 if (bdrv_can_snapshot(bs
)) {
470 err
= bdrv_snapshot_goto(bs
, name
);
472 aio_context_release(ctx
);
474 bdrv_next_cleanup(&it
);
484 int bdrv_all_find_snapshot(const char *name
, BlockDriverState
**first_bad_bs
)
488 BlockDriverState
*bs
;
491 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
492 AioContext
*ctx
= bdrv_get_aio_context(bs
);
494 aio_context_acquire(ctx
);
495 if (bdrv_can_snapshot(bs
)) {
496 err
= bdrv_snapshot_find(bs
, &sn
, name
);
498 aio_context_release(ctx
);
500 bdrv_next_cleanup(&it
);
510 int bdrv_all_create_snapshot(QEMUSnapshotInfo
*sn
,
511 BlockDriverState
*vm_state_bs
,
512 uint64_t vm_state_size
,
513 BlockDriverState
**first_bad_bs
)
516 BlockDriverState
*bs
;
519 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
520 AioContext
*ctx
= bdrv_get_aio_context(bs
);
522 aio_context_acquire(ctx
);
523 if (bs
== vm_state_bs
) {
524 sn
->vm_state_size
= vm_state_size
;
525 err
= bdrv_snapshot_create(bs
, sn
);
526 } else if (bdrv_can_snapshot(bs
)) {
527 sn
->vm_state_size
= 0;
528 err
= bdrv_snapshot_create(bs
, sn
);
530 aio_context_release(ctx
);
532 bdrv_next_cleanup(&it
);
542 BlockDriverState
*bdrv_all_find_vmstate_bs(void)
544 BlockDriverState
*bs
;
547 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
548 AioContext
*ctx
= bdrv_get_aio_context(bs
);
551 aio_context_acquire(ctx
);
552 found
= bdrv_can_snapshot(bs
);
553 aio_context_release(ctx
);
556 bdrv_next_cleanup(&it
);