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/qmp/qerror.h"
30 QemuOptsList internal_snapshot_opts
= {
32 .head
= QTAILQ_HEAD_INITIALIZER(internal_snapshot_opts
.head
),
35 .name
= SNAPSHOT_OPT_ID
,
36 .type
= QEMU_OPT_STRING
,
39 .name
= SNAPSHOT_OPT_NAME
,
40 .type
= QEMU_OPT_STRING
,
41 .help
= "snapshot name"
48 int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
51 QEMUSnapshotInfo
*sn_tab
, *sn
;
55 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
59 for (i
= 0; i
< nb_sns
; i
++) {
61 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
72 * Look up an internal snapshot by @id and @name.
73 * @bs: block device to search
74 * @id: unique snapshot ID, or NULL
75 * @name: snapshot name, or NULL
76 * @sn_info: location to store information on the snapshot found
77 * @errp: location to store error, will be set only for exception
79 * This function will traverse snapshot list in @bs to search the matching
80 * one, @id and @name are the matching condition:
81 * If both @id and @name are specified, find the first one with id @id and
83 * If only @id is specified, find the first one with id @id.
84 * If only @name is specified, find the first one with name @name.
85 * if none is specified, abort().
87 * Returns: true when a snapshot is found and @sn_info will be filled, false
88 * when error or not found. If all operation succeed but no matching one is
89 * found, @errp will NOT be set.
91 bool bdrv_snapshot_find_by_id_and_name(BlockDriverState
*bs
,
94 QEMUSnapshotInfo
*sn_info
,
97 QEMUSnapshotInfo
*sn_tab
, *sn
;
103 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
105 error_setg_errno(errp
, -nb_sns
, "Failed to get a snapshot list");
107 } else if (nb_sns
== 0) {
112 for (i
= 0; i
< nb_sns
; i
++) {
114 if (!strcmp(sn
->id_str
, id
) && !strcmp(sn
->name
, name
)) {
121 for (i
= 0; i
< nb_sns
; i
++) {
123 if (!strcmp(sn
->id_str
, id
)) {
130 for (i
= 0; i
< nb_sns
; i
++) {
132 if (!strcmp(sn
->name
, name
)) {
144 int bdrv_can_snapshot(BlockDriverState
*bs
)
146 BlockDriver
*drv
= bs
->drv
;
147 if (!drv
|| !bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
151 if (!drv
->bdrv_snapshot_create
) {
152 if (bs
->file
!= NULL
) {
153 return bdrv_can_snapshot(bs
->file
->bs
);
161 int bdrv_snapshot_create(BlockDriverState
*bs
,
162 QEMUSnapshotInfo
*sn_info
)
164 BlockDriver
*drv
= bs
->drv
;
168 if (drv
->bdrv_snapshot_create
) {
169 return drv
->bdrv_snapshot_create(bs
, sn_info
);
172 return bdrv_snapshot_create(bs
->file
->bs
, sn_info
);
177 int bdrv_snapshot_goto(BlockDriverState
*bs
,
178 const char *snapshot_id
)
180 BlockDriver
*drv
= bs
->drv
;
186 if (drv
->bdrv_snapshot_goto
) {
187 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
192 ret
= bdrv_snapshot_goto(bs
->file
->bs
, snapshot_id
);
193 open_ret
= drv
->bdrv_open(bs
, NULL
, bs
->open_flags
, NULL
);
195 bdrv_unref(bs
->file
->bs
);
206 * Delete an internal snapshot by @snapshot_id and @name.
207 * @bs: block device used in the operation
208 * @snapshot_id: unique snapshot ID, or NULL
209 * @name: snapshot name, or NULL
210 * @errp: location to store error
212 * If both @snapshot_id and @name are specified, delete the first one with
213 * id @snapshot_id and name @name.
214 * If only @snapshot_id is specified, delete the first one with id
216 * If only @name is specified, delete the first one with name @name.
217 * if none is specified, return -EINVAL.
219 * Returns: 0 on success, -errno on failure. If @bs is not inserted, return
220 * -ENOMEDIUM. If @snapshot_id and @name are both NULL, return -EINVAL. If @bs
221 * does not support internal snapshot deletion, return -ENOTSUP. If @bs does
222 * not support parameter @snapshot_id or @name, or one of them is not correctly
223 * specified, return -EINVAL. If @bs can't find one matching @id and @name,
224 * return -ENOENT. If @errp != NULL, it will always be filled with error
225 * message on failure.
227 int bdrv_snapshot_delete(BlockDriverState
*bs
,
228 const char *snapshot_id
,
232 BlockDriver
*drv
= bs
->drv
;
236 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, bdrv_get_device_name(bs
));
239 if (!snapshot_id
&& !name
) {
240 error_setg(errp
, "snapshot_id and name are both NULL");
244 /* drain all pending i/o before deleting snapshot */
245 bdrv_drained_begin(bs
);
247 if (drv
->bdrv_snapshot_delete
) {
248 ret
= drv
->bdrv_snapshot_delete(bs
, snapshot_id
, name
, errp
);
249 } else if (bs
->file
) {
250 ret
= bdrv_snapshot_delete(bs
->file
->bs
, snapshot_id
, name
, errp
);
252 error_setg(errp
, "Block format '%s' used by device '%s' "
253 "does not support internal snapshot deletion",
254 drv
->format_name
, bdrv_get_device_name(bs
));
258 bdrv_drained_end(bs
);
262 int bdrv_snapshot_delete_by_id_or_name(BlockDriverState
*bs
,
263 const char *id_or_name
,
267 Error
*local_err
= NULL
;
269 ret
= bdrv_snapshot_delete(bs
, id_or_name
, NULL
, &local_err
);
270 if (ret
== -ENOENT
|| ret
== -EINVAL
) {
271 error_free(local_err
);
273 ret
= bdrv_snapshot_delete(bs
, NULL
, id_or_name
, &local_err
);
277 error_propagate(errp
, local_err
);
282 int bdrv_snapshot_list(BlockDriverState
*bs
,
283 QEMUSnapshotInfo
**psn_info
)
285 BlockDriver
*drv
= bs
->drv
;
289 if (drv
->bdrv_snapshot_list
) {
290 return drv
->bdrv_snapshot_list(bs
, psn_info
);
293 return bdrv_snapshot_list(bs
->file
->bs
, psn_info
);
299 * Temporarily load an internal snapshot by @snapshot_id and @name.
300 * @bs: block device used in the operation
301 * @snapshot_id: unique snapshot ID, or NULL
302 * @name: snapshot name, or NULL
303 * @errp: location to store error
305 * If both @snapshot_id and @name are specified, load the first one with
306 * id @snapshot_id and name @name.
307 * If only @snapshot_id is specified, load the first one with id
309 * If only @name is specified, load the first one with name @name.
310 * if none is specified, return -EINVAL.
312 * Returns: 0 on success, -errno on fail. If @bs is not inserted, return
313 * -ENOMEDIUM. If @bs is not readonly, return -EINVAL. If @bs did not support
314 * internal snapshot, return -ENOTSUP. If qemu can't find a matching @id and
315 * @name, return -ENOENT. If @errp != NULL, it will always be filled on
318 int bdrv_snapshot_load_tmp(BlockDriverState
*bs
,
319 const char *snapshot_id
,
323 BlockDriver
*drv
= bs
->drv
;
326 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, bdrv_get_device_name(bs
));
329 if (!snapshot_id
&& !name
) {
330 error_setg(errp
, "snapshot_id and name are both NULL");
333 if (!bs
->read_only
) {
334 error_setg(errp
, "Device is not readonly");
337 if (drv
->bdrv_snapshot_load_tmp
) {
338 return drv
->bdrv_snapshot_load_tmp(bs
, snapshot_id
, name
, errp
);
340 error_setg(errp
, "Block format '%s' used by device '%s' "
341 "does not support temporarily loading internal snapshots",
342 drv
->format_name
, bdrv_get_device_name(bs
));
346 int bdrv_snapshot_load_tmp_by_id_or_name(BlockDriverState
*bs
,
347 const char *id_or_name
,
351 Error
*local_err
= NULL
;
353 ret
= bdrv_snapshot_load_tmp(bs
, id_or_name
, NULL
, &local_err
);
354 if (ret
== -ENOENT
|| ret
== -EINVAL
) {
355 error_free(local_err
);
357 ret
= bdrv_snapshot_load_tmp(bs
, NULL
, id_or_name
, &local_err
);
361 error_propagate(errp
, local_err
);
368 /* Group operations. All block drivers are involved.
369 * These functions will properly handle dataplane (take aio_context_acquire
370 * when appropriate for appropriate block drivers) */
372 bool bdrv_all_can_snapshot(BlockDriverState
**first_bad_bs
)
375 BlockDriverState
*bs
= NULL
;
377 while (ok
&& (bs
= bdrv_next(bs
))) {
378 AioContext
*ctx
= bdrv_get_aio_context(bs
);
380 aio_context_acquire(ctx
);
381 if (bdrv_is_inserted(bs
) && !bdrv_is_read_only(bs
)) {
382 ok
= bdrv_can_snapshot(bs
);
384 aio_context_release(ctx
);
391 int bdrv_all_delete_snapshot(const char *name
, BlockDriverState
**first_bad_bs
,
395 BlockDriverState
*bs
= NULL
;
396 QEMUSnapshotInfo sn1
, *snapshot
= &sn1
;
398 while (ret
== 0 && (bs
= bdrv_next(bs
))) {
399 AioContext
*ctx
= bdrv_get_aio_context(bs
);
401 aio_context_acquire(ctx
);
402 if (bdrv_can_snapshot(bs
) &&
403 bdrv_snapshot_find(bs
, snapshot
, name
) >= 0) {
404 ret
= bdrv_snapshot_delete_by_id_or_name(bs
, name
, err
);
406 aio_context_release(ctx
);
414 int bdrv_all_goto_snapshot(const char *name
, BlockDriverState
**first_bad_bs
)
417 BlockDriverState
*bs
= NULL
;
419 while (err
== 0 && (bs
= bdrv_next(bs
))) {
420 AioContext
*ctx
= bdrv_get_aio_context(bs
);
422 aio_context_acquire(ctx
);
423 if (bdrv_can_snapshot(bs
)) {
424 err
= bdrv_snapshot_goto(bs
, name
);
426 aio_context_release(ctx
);
433 int bdrv_all_find_snapshot(const char *name
, BlockDriverState
**first_bad_bs
)
437 BlockDriverState
*bs
= NULL
;
439 while (err
== 0 && (bs
= bdrv_next(bs
))) {
440 AioContext
*ctx
= bdrv_get_aio_context(bs
);
442 aio_context_acquire(ctx
);
443 if (bdrv_can_snapshot(bs
)) {
444 err
= bdrv_snapshot_find(bs
, &sn
, name
);
446 aio_context_release(ctx
);
453 int bdrv_all_create_snapshot(QEMUSnapshotInfo
*sn
,
454 BlockDriverState
*vm_state_bs
,
455 uint64_t vm_state_size
,
456 BlockDriverState
**first_bad_bs
)
459 BlockDriverState
*bs
= NULL
;
461 while (err
== 0 && (bs
= bdrv_next(bs
))) {
462 AioContext
*ctx
= bdrv_get_aio_context(bs
);
464 aio_context_acquire(ctx
);
465 if (bs
== vm_state_bs
) {
466 sn
->vm_state_size
= vm_state_size
;
467 err
= bdrv_snapshot_create(bs
, sn
);
468 } else if (bdrv_can_snapshot(bs
)) {
469 sn
->vm_state_size
= 0;
470 err
= bdrv_snapshot_create(bs
, sn
);
472 aio_context_release(ctx
);
479 BlockDriverState
*bdrv_all_find_vmstate_bs(void)
481 bool not_found
= true;
482 BlockDriverState
*bs
= NULL
;
484 while (not_found
&& (bs
= bdrv_next(bs
))) {
485 AioContext
*ctx
= bdrv_get_aio_context(bs
);
487 aio_context_acquire(ctx
);
488 not_found
= !bdrv_can_snapshot(bs
);
489 aio_context_release(ctx
);