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
)) {
150 int bdrv_can_snapshot(BlockDriverState
*bs
)
152 BlockDriver
*drv
= bs
->drv
;
153 if (!drv
|| !bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
157 if (!drv
->bdrv_snapshot_create
) {
158 if (bs
->file
!= NULL
) {
159 return bdrv_can_snapshot(bs
->file
->bs
);
167 int bdrv_snapshot_create(BlockDriverState
*bs
,
168 QEMUSnapshotInfo
*sn_info
)
170 BlockDriver
*drv
= bs
->drv
;
174 if (drv
->bdrv_snapshot_create
) {
175 return drv
->bdrv_snapshot_create(bs
, sn_info
);
178 return bdrv_snapshot_create(bs
->file
->bs
, sn_info
);
183 int bdrv_snapshot_goto(BlockDriverState
*bs
,
184 const char *snapshot_id
,
187 BlockDriver
*drv
= bs
->drv
;
191 error_setg(errp
, "Block driver is closed");
195 if (!QLIST_EMPTY(&bs
->dirty_bitmaps
)) {
196 error_setg(errp
, "Device has active dirty bitmaps");
200 if (drv
->bdrv_snapshot_goto
) {
201 ret
= drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
203 error_setg_errno(errp
, -ret
, "Failed to load snapshot");
209 BlockDriverState
*file
;
210 QDict
*options
= qdict_clone_shallow(bs
->options
);
212 Error
*local_err
= NULL
;
215 /* Prevent it from getting deleted when detached from bs */
218 qdict_extract_subqdict(options
, &file_options
, "file.");
219 qobject_unref(file_options
);
220 qdict_put_str(options
, "file", bdrv_get_node_name(file
));
222 if (drv
->bdrv_close
) {
225 bdrv_unref_child(bs
, bs
->file
);
228 ret
= bdrv_snapshot_goto(file
, snapshot_id
, errp
);
229 open_ret
= drv
->bdrv_open(bs
, options
, bs
->open_flags
, &local_err
);
230 qobject_unref(options
);
234 /* A bdrv_snapshot_goto() error takes precedence */
235 error_propagate(errp
, local_err
);
236 return ret
< 0 ? ret
: open_ret
;
239 assert(bs
->file
->bs
== file
);
244 error_setg(errp
, "Block driver does not support snapshots");
249 * Delete an internal snapshot by @snapshot_id and @name.
250 * @bs: block device used in the operation
251 * @snapshot_id: unique snapshot ID, or NULL
252 * @name: snapshot name, or NULL
253 * @errp: location to store error
255 * If both @snapshot_id and @name are specified, delete the first one with
256 * id @snapshot_id and name @name.
257 * If only @snapshot_id is specified, delete the first one with id
259 * If only @name is specified, delete the first one with name @name.
260 * if none is specified, return -EINVAL.
262 * Returns: 0 on success, -errno on failure. If @bs is not inserted, return
263 * -ENOMEDIUM. If @snapshot_id and @name are both NULL, return -EINVAL. If @bs
264 * does not support internal snapshot deletion, return -ENOTSUP. If @bs does
265 * not support parameter @snapshot_id or @name, or one of them is not correctly
266 * specified, return -EINVAL. If @bs can't find one matching @id and @name,
267 * return -ENOENT. If @errp != NULL, it will always be filled with error
268 * message on failure.
270 int bdrv_snapshot_delete(BlockDriverState
*bs
,
271 const char *snapshot_id
,
275 BlockDriver
*drv
= bs
->drv
;
279 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, bdrv_get_device_name(bs
));
282 if (!snapshot_id
&& !name
) {
283 error_setg(errp
, "snapshot_id and name are both NULL");
287 /* drain all pending i/o before deleting snapshot */
288 bdrv_drained_begin(bs
);
290 if (drv
->bdrv_snapshot_delete
) {
291 ret
= drv
->bdrv_snapshot_delete(bs
, snapshot_id
, name
, errp
);
292 } else if (bs
->file
) {
293 ret
= bdrv_snapshot_delete(bs
->file
->bs
, snapshot_id
, name
, errp
);
295 error_setg(errp
, "Block format '%s' used by device '%s' "
296 "does not support internal snapshot deletion",
297 drv
->format_name
, bdrv_get_device_name(bs
));
301 bdrv_drained_end(bs
);
305 int bdrv_snapshot_list(BlockDriverState
*bs
,
306 QEMUSnapshotInfo
**psn_info
)
308 BlockDriver
*drv
= bs
->drv
;
312 if (drv
->bdrv_snapshot_list
) {
313 return drv
->bdrv_snapshot_list(bs
, psn_info
);
316 return bdrv_snapshot_list(bs
->file
->bs
, psn_info
);
322 * Temporarily load an internal snapshot by @snapshot_id and @name.
323 * @bs: block device used in the operation
324 * @snapshot_id: unique snapshot ID, or NULL
325 * @name: snapshot name, or NULL
326 * @errp: location to store error
328 * If both @snapshot_id and @name are specified, load the first one with
329 * id @snapshot_id and name @name.
330 * If only @snapshot_id is specified, load the first one with id
332 * If only @name is specified, load the first one with name @name.
333 * if none is specified, return -EINVAL.
335 * Returns: 0 on success, -errno on fail. If @bs is not inserted, return
336 * -ENOMEDIUM. If @bs is not readonly, return -EINVAL. If @bs did not support
337 * internal snapshot, return -ENOTSUP. If qemu can't find a matching @id and
338 * @name, return -ENOENT. If @errp != NULL, it will always be filled on
341 int bdrv_snapshot_load_tmp(BlockDriverState
*bs
,
342 const char *snapshot_id
,
346 BlockDriver
*drv
= bs
->drv
;
349 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, bdrv_get_device_name(bs
));
352 if (!snapshot_id
&& !name
) {
353 error_setg(errp
, "snapshot_id and name are both NULL");
356 if (!bs
->read_only
) {
357 error_setg(errp
, "Device is not readonly");
360 if (drv
->bdrv_snapshot_load_tmp
) {
361 return drv
->bdrv_snapshot_load_tmp(bs
, snapshot_id
, name
, errp
);
363 error_setg(errp
, "Block format '%s' used by device '%s' "
364 "does not support temporarily loading internal snapshots",
365 drv
->format_name
, bdrv_get_device_name(bs
));
369 int bdrv_snapshot_load_tmp_by_id_or_name(BlockDriverState
*bs
,
370 const char *id_or_name
,
374 Error
*local_err
= NULL
;
376 ret
= bdrv_snapshot_load_tmp(bs
, id_or_name
, NULL
, &local_err
);
377 if (ret
== -ENOENT
|| ret
== -EINVAL
) {
378 error_free(local_err
);
380 ret
= bdrv_snapshot_load_tmp(bs
, NULL
, id_or_name
, &local_err
);
383 error_propagate(errp
, local_err
);
388 static bool bdrv_all_snapshots_includes_bs(BlockDriverState
*bs
)
390 if (!bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
394 /* Include all nodes that are either in use by a BlockBackend, or that
395 * aren't attached to any node, but owned by the monitor. */
396 return bdrv_has_blk(bs
) || QLIST_EMPTY(&bs
->parents
);
399 /* Group operations. All block drivers are involved.
400 * These functions will properly handle dataplane (take aio_context_acquire
401 * when appropriate for appropriate block drivers) */
403 bool bdrv_all_can_snapshot(BlockDriverState
**first_bad_bs
)
406 BlockDriverState
*bs
;
409 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
410 AioContext
*ctx
= bdrv_get_aio_context(bs
);
412 aio_context_acquire(ctx
);
413 if (bdrv_all_snapshots_includes_bs(bs
)) {
414 ok
= bdrv_can_snapshot(bs
);
416 aio_context_release(ctx
);
418 bdrv_next_cleanup(&it
);
428 int bdrv_all_delete_snapshot(const char *name
, BlockDriverState
**first_bad_bs
,
432 BlockDriverState
*bs
;
434 QEMUSnapshotInfo sn1
, *snapshot
= &sn1
;
436 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
437 AioContext
*ctx
= bdrv_get_aio_context(bs
);
439 aio_context_acquire(ctx
);
440 if (bdrv_all_snapshots_includes_bs(bs
) &&
441 bdrv_snapshot_find(bs
, snapshot
, name
) >= 0)
443 ret
= bdrv_snapshot_delete(bs
, snapshot
->id_str
,
444 snapshot
->name
, errp
);
446 aio_context_release(ctx
);
448 bdrv_next_cleanup(&it
);
459 int bdrv_all_goto_snapshot(const char *name
, BlockDriverState
**first_bad_bs
,
463 BlockDriverState
*bs
;
466 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
467 AioContext
*ctx
= bdrv_get_aio_context(bs
);
469 aio_context_acquire(ctx
);
470 if (bdrv_all_snapshots_includes_bs(bs
)) {
471 ret
= bdrv_snapshot_goto(bs
, name
, errp
);
473 aio_context_release(ctx
);
475 bdrv_next_cleanup(&it
);
485 int bdrv_all_find_snapshot(const char *name
, BlockDriverState
**first_bad_bs
)
489 BlockDriverState
*bs
;
492 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
493 AioContext
*ctx
= bdrv_get_aio_context(bs
);
495 aio_context_acquire(ctx
);
496 if (bdrv_all_snapshots_includes_bs(bs
)) {
497 err
= bdrv_snapshot_find(bs
, &sn
, name
);
499 aio_context_release(ctx
);
501 bdrv_next_cleanup(&it
);
511 int bdrv_all_create_snapshot(QEMUSnapshotInfo
*sn
,
512 BlockDriverState
*vm_state_bs
,
513 uint64_t vm_state_size
,
514 BlockDriverState
**first_bad_bs
)
517 BlockDriverState
*bs
;
520 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
521 AioContext
*ctx
= bdrv_get_aio_context(bs
);
523 aio_context_acquire(ctx
);
524 if (bs
== vm_state_bs
) {
525 sn
->vm_state_size
= vm_state_size
;
526 err
= bdrv_snapshot_create(bs
, sn
);
527 } else if (bdrv_all_snapshots_includes_bs(bs
)) {
528 sn
->vm_state_size
= 0;
529 err
= bdrv_snapshot_create(bs
, sn
);
531 aio_context_release(ctx
);
533 bdrv_next_cleanup(&it
);
543 BlockDriverState
*bdrv_all_find_vmstate_bs(void)
545 BlockDriverState
*bs
;
548 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
549 AioContext
*ctx
= bdrv_get_aio_context(bs
);
552 aio_context_acquire(ctx
);
553 found
= bdrv_all_snapshots_includes_bs(bs
) && bdrv_can_snapshot(bs
);
554 aio_context_release(ctx
);
557 bdrv_next_cleanup(&it
);