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"
31 QemuOptsList internal_snapshot_opts
= {
33 .head
= QTAILQ_HEAD_INITIALIZER(internal_snapshot_opts
.head
),
36 .name
= SNAPSHOT_OPT_ID
,
37 .type
= QEMU_OPT_STRING
,
40 .name
= SNAPSHOT_OPT_NAME
,
41 .type
= QEMU_OPT_STRING
,
42 .help
= "snapshot name"
49 int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
52 QEMUSnapshotInfo
*sn_tab
, *sn
;
56 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
60 for (i
= 0; i
< nb_sns
; i
++) {
62 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
73 * Look up an internal snapshot by @id and @name.
74 * @bs: block device to search
75 * @id: unique snapshot ID, or NULL
76 * @name: snapshot name, or NULL
77 * @sn_info: location to store information on the snapshot found
78 * @errp: location to store error, will be set only for exception
80 * This function will traverse snapshot list in @bs to search the matching
81 * one, @id and @name are the matching condition:
82 * If both @id and @name are specified, find the first one with id @id and
84 * If only @id is specified, find the first one with id @id.
85 * If only @name is specified, find the first one with name @name.
86 * if none is specified, abort().
88 * Returns: true when a snapshot is found and @sn_info will be filled, false
89 * when error or not found. If all operation succeed but no matching one is
90 * found, @errp will NOT be set.
92 bool bdrv_snapshot_find_by_id_and_name(BlockDriverState
*bs
,
95 QEMUSnapshotInfo
*sn_info
,
98 QEMUSnapshotInfo
*sn_tab
, *sn
;
104 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
106 error_setg_errno(errp
, -nb_sns
, "Failed to get a snapshot list");
108 } else if (nb_sns
== 0) {
113 for (i
= 0; i
< nb_sns
; i
++) {
115 if (!strcmp(sn
->id_str
, id
) && !strcmp(sn
->name
, name
)) {
122 for (i
= 0; i
< nb_sns
; i
++) {
124 if (!strcmp(sn
->id_str
, id
)) {
131 for (i
= 0; i
< nb_sns
; i
++) {
133 if (!strcmp(sn
->name
, name
)) {
145 int bdrv_can_snapshot(BlockDriverState
*bs
)
147 BlockDriver
*drv
= bs
->drv
;
148 if (!drv
|| !bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
152 if (!drv
->bdrv_snapshot_create
) {
153 if (bs
->file
!= NULL
) {
154 return bdrv_can_snapshot(bs
->file
->bs
);
162 int bdrv_snapshot_create(BlockDriverState
*bs
,
163 QEMUSnapshotInfo
*sn_info
)
165 BlockDriver
*drv
= bs
->drv
;
169 if (drv
->bdrv_snapshot_create
) {
170 return drv
->bdrv_snapshot_create(bs
, sn_info
);
173 return bdrv_snapshot_create(bs
->file
->bs
, sn_info
);
178 int bdrv_snapshot_goto(BlockDriverState
*bs
,
179 const char *snapshot_id
)
181 BlockDriver
*drv
= bs
->drv
;
187 if (drv
->bdrv_snapshot_goto
) {
188 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
193 ret
= bdrv_snapshot_goto(bs
->file
->bs
, snapshot_id
);
194 open_ret
= drv
->bdrv_open(bs
, NULL
, bs
->open_flags
, NULL
);
196 bdrv_unref(bs
->file
->bs
);
207 * Delete an internal snapshot by @snapshot_id and @name.
208 * @bs: block device used in the operation
209 * @snapshot_id: unique snapshot ID, or NULL
210 * @name: snapshot name, or NULL
211 * @errp: location to store error
213 * If both @snapshot_id and @name are specified, delete the first one with
214 * id @snapshot_id and name @name.
215 * If only @snapshot_id is specified, delete the first one with id
217 * If only @name is specified, delete the first one with name @name.
218 * if none is specified, return -EINVAL.
220 * Returns: 0 on success, -errno on failure. If @bs is not inserted, return
221 * -ENOMEDIUM. If @snapshot_id and @name are both NULL, return -EINVAL. If @bs
222 * does not support internal snapshot deletion, return -ENOTSUP. If @bs does
223 * not support parameter @snapshot_id or @name, or one of them is not correctly
224 * specified, return -EINVAL. If @bs can't find one matching @id and @name,
225 * return -ENOENT. If @errp != NULL, it will always be filled with error
226 * message on failure.
228 int bdrv_snapshot_delete(BlockDriverState
*bs
,
229 const char *snapshot_id
,
233 BlockDriver
*drv
= bs
->drv
;
237 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, bdrv_get_device_name(bs
));
240 if (!snapshot_id
&& !name
) {
241 error_setg(errp
, "snapshot_id and name are both NULL");
245 /* drain all pending i/o before deleting snapshot */
246 bdrv_drained_begin(bs
);
248 if (drv
->bdrv_snapshot_delete
) {
249 ret
= drv
->bdrv_snapshot_delete(bs
, snapshot_id
, name
, errp
);
250 } else if (bs
->file
) {
251 ret
= bdrv_snapshot_delete(bs
->file
->bs
, snapshot_id
, name
, errp
);
253 error_setg(errp
, "Block format '%s' used by device '%s' "
254 "does not support internal snapshot deletion",
255 drv
->format_name
, bdrv_get_device_name(bs
));
259 bdrv_drained_end(bs
);
263 int bdrv_snapshot_delete_by_id_or_name(BlockDriverState
*bs
,
264 const char *id_or_name
,
268 Error
*local_err
= NULL
;
270 ret
= bdrv_snapshot_delete(bs
, id_or_name
, NULL
, &local_err
);
271 if (ret
== -ENOENT
|| ret
== -EINVAL
) {
272 error_free(local_err
);
274 ret
= bdrv_snapshot_delete(bs
, NULL
, id_or_name
, &local_err
);
278 error_propagate(errp
, local_err
);
283 int bdrv_snapshot_list(BlockDriverState
*bs
,
284 QEMUSnapshotInfo
**psn_info
)
286 BlockDriver
*drv
= bs
->drv
;
290 if (drv
->bdrv_snapshot_list
) {
291 return drv
->bdrv_snapshot_list(bs
, psn_info
);
294 return bdrv_snapshot_list(bs
->file
->bs
, psn_info
);
300 * Temporarily load an internal snapshot by @snapshot_id and @name.
301 * @bs: block device used in the operation
302 * @snapshot_id: unique snapshot ID, or NULL
303 * @name: snapshot name, or NULL
304 * @errp: location to store error
306 * If both @snapshot_id and @name are specified, load the first one with
307 * id @snapshot_id and name @name.
308 * If only @snapshot_id is specified, load the first one with id
310 * If only @name is specified, load the first one with name @name.
311 * if none is specified, return -EINVAL.
313 * Returns: 0 on success, -errno on fail. If @bs is not inserted, return
314 * -ENOMEDIUM. If @bs is not readonly, return -EINVAL. If @bs did not support
315 * internal snapshot, return -ENOTSUP. If qemu can't find a matching @id and
316 * @name, return -ENOENT. If @errp != NULL, it will always be filled on
319 int bdrv_snapshot_load_tmp(BlockDriverState
*bs
,
320 const char *snapshot_id
,
324 BlockDriver
*drv
= bs
->drv
;
327 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, bdrv_get_device_name(bs
));
330 if (!snapshot_id
&& !name
) {
331 error_setg(errp
, "snapshot_id and name are both NULL");
334 if (!bs
->read_only
) {
335 error_setg(errp
, "Device is not readonly");
338 if (drv
->bdrv_snapshot_load_tmp
) {
339 return drv
->bdrv_snapshot_load_tmp(bs
, snapshot_id
, name
, errp
);
341 error_setg(errp
, "Block format '%s' used by device '%s' "
342 "does not support temporarily loading internal snapshots",
343 drv
->format_name
, bdrv_get_device_name(bs
));
347 int bdrv_snapshot_load_tmp_by_id_or_name(BlockDriverState
*bs
,
348 const char *id_or_name
,
352 Error
*local_err
= NULL
;
354 ret
= bdrv_snapshot_load_tmp(bs
, id_or_name
, NULL
, &local_err
);
355 if (ret
== -ENOENT
|| ret
== -EINVAL
) {
356 error_free(local_err
);
358 ret
= bdrv_snapshot_load_tmp(bs
, NULL
, id_or_name
, &local_err
);
362 error_propagate(errp
, local_err
);
369 /* Group operations. All block drivers are involved.
370 * These functions will properly handle dataplane (take aio_context_acquire
371 * when appropriate for appropriate block drivers) */
373 bool bdrv_all_can_snapshot(BlockDriverState
**first_bad_bs
)
376 BlockDriverState
*bs
;
377 BdrvNextIterator
*it
= NULL
;
379 while (ok
&& (it
= bdrv_next(it
, &bs
))) {
380 AioContext
*ctx
= bdrv_get_aio_context(bs
);
382 aio_context_acquire(ctx
);
383 if (bdrv_is_inserted(bs
) && !bdrv_is_read_only(bs
)) {
384 ok
= bdrv_can_snapshot(bs
);
386 aio_context_release(ctx
);
393 int bdrv_all_delete_snapshot(const char *name
, BlockDriverState
**first_bad_bs
,
397 BlockDriverState
*bs
;
398 BdrvNextIterator
*it
= NULL
;
399 QEMUSnapshotInfo sn1
, *snapshot
= &sn1
;
401 while (ret
== 0 && (it
= bdrv_next(it
, &bs
))) {
402 AioContext
*ctx
= bdrv_get_aio_context(bs
);
404 aio_context_acquire(ctx
);
405 if (bdrv_can_snapshot(bs
) &&
406 bdrv_snapshot_find(bs
, snapshot
, name
) >= 0) {
407 ret
= bdrv_snapshot_delete_by_id_or_name(bs
, name
, err
);
409 aio_context_release(ctx
);
417 int bdrv_all_goto_snapshot(const char *name
, BlockDriverState
**first_bad_bs
)
420 BlockDriverState
*bs
;
421 BdrvNextIterator
*it
= NULL
;
423 while (err
== 0 && (it
= bdrv_next(it
, &bs
))) {
424 AioContext
*ctx
= bdrv_get_aio_context(bs
);
426 aio_context_acquire(ctx
);
427 if (bdrv_can_snapshot(bs
)) {
428 err
= bdrv_snapshot_goto(bs
, name
);
430 aio_context_release(ctx
);
437 int bdrv_all_find_snapshot(const char *name
, BlockDriverState
**first_bad_bs
)
441 BlockDriverState
*bs
;
442 BdrvNextIterator
*it
= NULL
;
444 while (err
== 0 && (it
= bdrv_next(it
, &bs
))) {
445 AioContext
*ctx
= bdrv_get_aio_context(bs
);
447 aio_context_acquire(ctx
);
448 if (bdrv_can_snapshot(bs
)) {
449 err
= bdrv_snapshot_find(bs
, &sn
, name
);
451 aio_context_release(ctx
);
458 int bdrv_all_create_snapshot(QEMUSnapshotInfo
*sn
,
459 BlockDriverState
*vm_state_bs
,
460 uint64_t vm_state_size
,
461 BlockDriverState
**first_bad_bs
)
464 BlockDriverState
*bs
;
465 BdrvNextIterator
*it
= NULL
;
467 while (err
== 0 && (it
= bdrv_next(it
, &bs
))) {
468 AioContext
*ctx
= bdrv_get_aio_context(bs
);
470 aio_context_acquire(ctx
);
471 if (bs
== vm_state_bs
) {
472 sn
->vm_state_size
= vm_state_size
;
473 err
= bdrv_snapshot_create(bs
, sn
);
474 } else if (bdrv_can_snapshot(bs
)) {
475 sn
->vm_state_size
= 0;
476 err
= bdrv_snapshot_create(bs
, sn
);
478 aio_context_release(ctx
);
485 BlockDriverState
*bdrv_all_find_vmstate_bs(void)
487 bool not_found
= true;
488 BlockDriverState
*bs
;
489 BdrvNextIterator
*it
= NULL
;
491 while (not_found
&& (it
= bdrv_next(it
, &bs
))) {
492 AioContext
*ctx
= bdrv_get_aio_context(bs
);
494 aio_context_acquire(ctx
);
495 not_found
= !bdrv_can_snapshot(bs
);
496 aio_context_release(ctx
);