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"
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
;
60 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
64 for (i
= 0; i
< nb_sns
; i
++) {
66 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
77 * Look up an internal snapshot by @id and @name.
78 * @bs: block device to search
79 * @id: unique snapshot ID, or NULL
80 * @name: snapshot name, or NULL
81 * @sn_info: location to store information on the snapshot found
82 * @errp: location to store error, will be set only for exception
84 * This function will traverse snapshot list in @bs to search the matching
85 * one, @id and @name are the matching condition:
86 * If both @id and @name are specified, find the first one with id @id and
88 * If only @id is specified, find the first one with id @id.
89 * If only @name is specified, find the first one with name @name.
90 * if none is specified, abort().
92 * Returns: true when a snapshot is found and @sn_info will be filled, false
93 * when error or not found. If all operation succeed but no matching one is
94 * found, @errp will NOT be set.
96 bool bdrv_snapshot_find_by_id_and_name(BlockDriverState
*bs
,
99 QEMUSnapshotInfo
*sn_info
,
102 QEMUSnapshotInfo
*sn_tab
, *sn
;
108 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
110 error_setg_errno(errp
, -nb_sns
, "Failed to get a snapshot list");
112 } else if (nb_sns
== 0) {
117 for (i
= 0; i
< nb_sns
; i
++) {
119 if (!strcmp(sn
->id_str
, id
) && !strcmp(sn
->name
, name
)) {
126 for (i
= 0; i
< nb_sns
; i
++) {
128 if (!strcmp(sn
->id_str
, id
)) {
135 for (i
= 0; i
< nb_sns
; i
++) {
137 if (!strcmp(sn
->name
, name
)) {
149 int bdrv_can_snapshot(BlockDriverState
*bs
)
151 BlockDriver
*drv
= bs
->drv
;
152 if (!drv
|| !bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
156 if (!drv
->bdrv_snapshot_create
) {
157 if (bs
->file
!= NULL
) {
158 return bdrv_can_snapshot(bs
->file
->bs
);
166 int bdrv_snapshot_create(BlockDriverState
*bs
,
167 QEMUSnapshotInfo
*sn_info
)
169 BlockDriver
*drv
= bs
->drv
;
173 if (drv
->bdrv_snapshot_create
) {
174 return drv
->bdrv_snapshot_create(bs
, sn_info
);
177 return bdrv_snapshot_create(bs
->file
->bs
, sn_info
);
182 int bdrv_snapshot_goto(BlockDriverState
*bs
,
183 const char *snapshot_id
,
186 BlockDriver
*drv
= bs
->drv
;
190 error_setg(errp
, "Block driver is closed");
194 if (!QLIST_EMPTY(&bs
->dirty_bitmaps
)) {
195 error_setg(errp
, "Device has active dirty bitmaps");
199 if (drv
->bdrv_snapshot_goto
) {
200 ret
= drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
202 error_setg_errno(errp
, -ret
, "Failed to load snapshot");
208 BlockDriverState
*file
;
209 QDict
*options
= qdict_clone_shallow(bs
->options
);
211 Error
*local_err
= NULL
;
214 /* Prevent it from getting deleted when detached from bs */
217 qdict_extract_subqdict(options
, &file_options
, "file.");
218 qobject_unref(file_options
);
219 qdict_put_str(options
, "file", bdrv_get_node_name(file
));
221 if (drv
->bdrv_close
) {
224 bdrv_unref_child(bs
, bs
->file
);
227 ret
= bdrv_snapshot_goto(file
, snapshot_id
, errp
);
228 open_ret
= drv
->bdrv_open(bs
, options
, bs
->open_flags
, &local_err
);
229 qobject_unref(options
);
233 /* A bdrv_snapshot_goto() error takes precedence */
234 error_propagate(errp
, local_err
);
235 return ret
< 0 ? ret
: open_ret
;
238 assert(bs
->file
->bs
== file
);
243 error_setg(errp
, "Block driver does not support snapshots");
248 * Delete an internal snapshot by @snapshot_id and @name.
249 * @bs: block device used in the operation
250 * @snapshot_id: unique snapshot ID, or NULL
251 * @name: snapshot name, or NULL
252 * @errp: location to store error
254 * If both @snapshot_id and @name are specified, delete the first one with
255 * id @snapshot_id and name @name.
256 * If only @snapshot_id is specified, delete the first one with id
258 * If only @name is specified, delete the first one with name @name.
259 * if none is specified, return -EINVAL.
261 * Returns: 0 on success, -errno on failure. If @bs is not inserted, return
262 * -ENOMEDIUM. If @snapshot_id and @name are both NULL, return -EINVAL. If @bs
263 * does not support internal snapshot deletion, return -ENOTSUP. If @bs does
264 * not support parameter @snapshot_id or @name, or one of them is not correctly
265 * specified, return -EINVAL. If @bs can't find one matching @id and @name,
266 * return -ENOENT. If @errp != NULL, it will always be filled with error
267 * message on failure.
269 int bdrv_snapshot_delete(BlockDriverState
*bs
,
270 const char *snapshot_id
,
274 BlockDriver
*drv
= bs
->drv
;
278 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, bdrv_get_device_name(bs
));
281 if (!snapshot_id
&& !name
) {
282 error_setg(errp
, "snapshot_id and name are both NULL");
286 /* drain all pending i/o before deleting snapshot */
287 bdrv_drained_begin(bs
);
289 if (drv
->bdrv_snapshot_delete
) {
290 ret
= drv
->bdrv_snapshot_delete(bs
, snapshot_id
, name
, errp
);
291 } else if (bs
->file
) {
292 ret
= bdrv_snapshot_delete(bs
->file
->bs
, snapshot_id
, name
, errp
);
294 error_setg(errp
, "Block format '%s' used by device '%s' "
295 "does not support internal snapshot deletion",
296 drv
->format_name
, bdrv_get_device_name(bs
));
300 bdrv_drained_end(bs
);
304 int bdrv_snapshot_delete_by_id_or_name(BlockDriverState
*bs
,
305 const char *id_or_name
,
309 Error
*local_err
= NULL
;
311 ret
= bdrv_snapshot_delete(bs
, id_or_name
, NULL
, &local_err
);
312 if (ret
== -ENOENT
|| ret
== -EINVAL
) {
313 error_free(local_err
);
315 ret
= bdrv_snapshot_delete(bs
, NULL
, id_or_name
, &local_err
);
319 error_propagate(errp
, local_err
);
324 int bdrv_snapshot_list(BlockDriverState
*bs
,
325 QEMUSnapshotInfo
**psn_info
)
327 BlockDriver
*drv
= bs
->drv
;
331 if (drv
->bdrv_snapshot_list
) {
332 return drv
->bdrv_snapshot_list(bs
, psn_info
);
335 return bdrv_snapshot_list(bs
->file
->bs
, psn_info
);
341 * Temporarily load an internal snapshot by @snapshot_id and @name.
342 * @bs: block device used in the operation
343 * @snapshot_id: unique snapshot ID, or NULL
344 * @name: snapshot name, or NULL
345 * @errp: location to store error
347 * If both @snapshot_id and @name are specified, load the first one with
348 * id @snapshot_id and name @name.
349 * If only @snapshot_id is specified, load the first one with id
351 * If only @name is specified, load the first one with name @name.
352 * if none is specified, return -EINVAL.
354 * Returns: 0 on success, -errno on fail. If @bs is not inserted, return
355 * -ENOMEDIUM. If @bs is not readonly, return -EINVAL. If @bs did not support
356 * internal snapshot, return -ENOTSUP. If qemu can't find a matching @id and
357 * @name, return -ENOENT. If @errp != NULL, it will always be filled on
360 int bdrv_snapshot_load_tmp(BlockDriverState
*bs
,
361 const char *snapshot_id
,
365 BlockDriver
*drv
= bs
->drv
;
368 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, bdrv_get_device_name(bs
));
371 if (!snapshot_id
&& !name
) {
372 error_setg(errp
, "snapshot_id and name are both NULL");
375 if (!bs
->read_only
) {
376 error_setg(errp
, "Device is not readonly");
379 if (drv
->bdrv_snapshot_load_tmp
) {
380 return drv
->bdrv_snapshot_load_tmp(bs
, snapshot_id
, name
, errp
);
382 error_setg(errp
, "Block format '%s' used by device '%s' "
383 "does not support temporarily loading internal snapshots",
384 drv
->format_name
, bdrv_get_device_name(bs
));
388 int bdrv_snapshot_load_tmp_by_id_or_name(BlockDriverState
*bs
,
389 const char *id_or_name
,
393 Error
*local_err
= NULL
;
395 ret
= bdrv_snapshot_load_tmp(bs
, id_or_name
, NULL
, &local_err
);
396 if (ret
== -ENOENT
|| ret
== -EINVAL
) {
397 error_free(local_err
);
399 ret
= bdrv_snapshot_load_tmp(bs
, NULL
, id_or_name
, &local_err
);
402 error_propagate(errp
, local_err
);
408 /* Group operations. All block drivers are involved.
409 * These functions will properly handle dataplane (take aio_context_acquire
410 * when appropriate for appropriate block drivers) */
412 bool bdrv_all_can_snapshot(BlockDriverState
**first_bad_bs
)
415 BlockDriverState
*bs
;
418 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
419 AioContext
*ctx
= bdrv_get_aio_context(bs
);
421 aio_context_acquire(ctx
);
422 if (bdrv_is_inserted(bs
) && !bdrv_is_read_only(bs
)) {
423 ok
= bdrv_can_snapshot(bs
);
425 aio_context_release(ctx
);
427 bdrv_next_cleanup(&it
);
437 int bdrv_all_delete_snapshot(const char *name
, BlockDriverState
**first_bad_bs
,
441 BlockDriverState
*bs
;
443 QEMUSnapshotInfo sn1
, *snapshot
= &sn1
;
445 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
446 AioContext
*ctx
= bdrv_get_aio_context(bs
);
448 aio_context_acquire(ctx
);
449 if (bdrv_can_snapshot(bs
) &&
450 bdrv_snapshot_find(bs
, snapshot
, name
) >= 0) {
451 ret
= bdrv_snapshot_delete_by_id_or_name(bs
, name
, err
);
453 aio_context_release(ctx
);
455 bdrv_next_cleanup(&it
);
466 int bdrv_all_goto_snapshot(const char *name
, BlockDriverState
**first_bad_bs
,
470 BlockDriverState
*bs
;
473 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
474 AioContext
*ctx
= bdrv_get_aio_context(bs
);
476 aio_context_acquire(ctx
);
477 if (bdrv_can_snapshot(bs
)) {
478 ret
= bdrv_snapshot_goto(bs
, name
, errp
);
480 aio_context_release(ctx
);
482 bdrv_next_cleanup(&it
);
492 int bdrv_all_find_snapshot(const char *name
, BlockDriverState
**first_bad_bs
)
496 BlockDriverState
*bs
;
499 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
500 AioContext
*ctx
= bdrv_get_aio_context(bs
);
502 aio_context_acquire(ctx
);
503 if (bdrv_can_snapshot(bs
)) {
504 err
= bdrv_snapshot_find(bs
, &sn
, name
);
506 aio_context_release(ctx
);
508 bdrv_next_cleanup(&it
);
518 int bdrv_all_create_snapshot(QEMUSnapshotInfo
*sn
,
519 BlockDriverState
*vm_state_bs
,
520 uint64_t vm_state_size
,
521 BlockDriverState
**first_bad_bs
)
524 BlockDriverState
*bs
;
527 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
528 AioContext
*ctx
= bdrv_get_aio_context(bs
);
530 aio_context_acquire(ctx
);
531 if (bs
== vm_state_bs
) {
532 sn
->vm_state_size
= vm_state_size
;
533 err
= bdrv_snapshot_create(bs
, sn
);
534 } else if (bdrv_can_snapshot(bs
)) {
535 sn
->vm_state_size
= 0;
536 err
= bdrv_snapshot_create(bs
, sn
);
538 aio_context_release(ctx
);
540 bdrv_next_cleanup(&it
);
550 BlockDriverState
*bdrv_all_find_vmstate_bs(void)
552 BlockDriverState
*bs
;
555 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
556 AioContext
*ctx
= bdrv_get_aio_context(bs
);
559 aio_context_acquire(ctx
);
560 found
= bdrv_can_snapshot(bs
);
561 aio_context_release(ctx
);
564 bdrv_next_cleanup(&it
);