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 "block/snapshot.h"
26 #include "block/block_int.h"
27 #include "qapi/qmp/qerror.h"
29 QemuOptsList internal_snapshot_opts
= {
31 .head
= QTAILQ_HEAD_INITIALIZER(internal_snapshot_opts
.head
),
34 .name
= SNAPSHOT_OPT_ID
,
35 .type
= QEMU_OPT_STRING
,
38 .name
= SNAPSHOT_OPT_NAME
,
39 .type
= QEMU_OPT_STRING
,
40 .help
= "snapshot name"
47 int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
50 QEMUSnapshotInfo
*sn_tab
, *sn
;
54 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
58 for (i
= 0; i
< nb_sns
; i
++) {
60 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
71 * Look up an internal snapshot by @id and @name.
72 * @bs: block device to search
73 * @id: unique snapshot ID, or NULL
74 * @name: snapshot name, or NULL
75 * @sn_info: location to store information on the snapshot found
76 * @errp: location to store error, will be set only for exception
78 * This function will traverse snapshot list in @bs to search the matching
79 * one, @id and @name are the matching condition:
80 * If both @id and @name are specified, find the first one with id @id and
82 * If only @id is specified, find the first one with id @id.
83 * If only @name is specified, find the first one with name @name.
84 * if none is specified, abort().
86 * Returns: true when a snapshot is found and @sn_info will be filled, false
87 * when error or not found. If all operation succeed but no matching one is
88 * found, @errp will NOT be set.
90 bool bdrv_snapshot_find_by_id_and_name(BlockDriverState
*bs
,
93 QEMUSnapshotInfo
*sn_info
,
96 QEMUSnapshotInfo
*sn_tab
, *sn
;
102 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
104 error_setg_errno(errp
, -nb_sns
, "Failed to get a snapshot list");
106 } else if (nb_sns
== 0) {
111 for (i
= 0; i
< nb_sns
; i
++) {
113 if (!strcmp(sn
->id_str
, id
) && !strcmp(sn
->name
, name
)) {
120 for (i
= 0; i
< nb_sns
; i
++) {
122 if (!strcmp(sn
->id_str
, id
)) {
129 for (i
= 0; i
< nb_sns
; i
++) {
131 if (!strcmp(sn
->name
, name
)) {
143 int bdrv_can_snapshot(BlockDriverState
*bs
)
145 BlockDriver
*drv
= bs
->drv
;
146 if (!drv
|| !bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
150 if (!drv
->bdrv_snapshot_create
) {
151 if (bs
->file
!= NULL
) {
152 return bdrv_can_snapshot(bs
->file
->bs
);
160 int bdrv_snapshot_create(BlockDriverState
*bs
,
161 QEMUSnapshotInfo
*sn_info
)
163 BlockDriver
*drv
= bs
->drv
;
167 if (drv
->bdrv_snapshot_create
) {
168 return drv
->bdrv_snapshot_create(bs
, sn_info
);
171 return bdrv_snapshot_create(bs
->file
->bs
, sn_info
);
176 int bdrv_snapshot_goto(BlockDriverState
*bs
,
177 const char *snapshot_id
)
179 BlockDriver
*drv
= bs
->drv
;
185 if (drv
->bdrv_snapshot_goto
) {
186 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
191 ret
= bdrv_snapshot_goto(bs
->file
->bs
, snapshot_id
);
192 open_ret
= drv
->bdrv_open(bs
, NULL
, bs
->open_flags
, NULL
);
194 bdrv_unref(bs
->file
->bs
);
205 * Delete an internal snapshot by @snapshot_id and @name.
206 * @bs: block device used in the operation
207 * @snapshot_id: unique snapshot ID, or NULL
208 * @name: snapshot name, or NULL
209 * @errp: location to store error
211 * If both @snapshot_id and @name are specified, delete the first one with
212 * id @snapshot_id and name @name.
213 * If only @snapshot_id is specified, delete the first one with id
215 * If only @name is specified, delete the first one with name @name.
216 * if none is specified, return -EINVAL.
218 * Returns: 0 on success, -errno on failure. If @bs is not inserted, return
219 * -ENOMEDIUM. If @snapshot_id and @name are both NULL, return -EINVAL. If @bs
220 * does not support internal snapshot deletion, return -ENOTSUP. If @bs does
221 * not support parameter @snapshot_id or @name, or one of them is not correctly
222 * specified, return -EINVAL. If @bs can't find one matching @id and @name,
223 * return -ENOENT. If @errp != NULL, it will always be filled with error
224 * message on failure.
226 int bdrv_snapshot_delete(BlockDriverState
*bs
,
227 const char *snapshot_id
,
231 BlockDriver
*drv
= bs
->drv
;
235 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, bdrv_get_device_name(bs
));
238 if (!snapshot_id
&& !name
) {
239 error_setg(errp
, "snapshot_id and name are both NULL");
243 /* drain all pending i/o before deleting snapshot */
244 bdrv_drained_begin(bs
);
246 if (drv
->bdrv_snapshot_delete
) {
247 ret
= drv
->bdrv_snapshot_delete(bs
, snapshot_id
, name
, errp
);
248 } else if (bs
->file
) {
249 ret
= bdrv_snapshot_delete(bs
->file
->bs
, snapshot_id
, name
, errp
);
251 error_setg(errp
, "Block format '%s' used by device '%s' "
252 "does not support internal snapshot deletion",
253 drv
->format_name
, bdrv_get_device_name(bs
));
257 bdrv_drained_end(bs
);
261 int bdrv_snapshot_delete_by_id_or_name(BlockDriverState
*bs
,
262 const char *id_or_name
,
266 Error
*local_err
= NULL
;
268 ret
= bdrv_snapshot_delete(bs
, id_or_name
, NULL
, &local_err
);
269 if (ret
== -ENOENT
|| ret
== -EINVAL
) {
270 error_free(local_err
);
272 ret
= bdrv_snapshot_delete(bs
, NULL
, id_or_name
, &local_err
);
276 error_propagate(errp
, local_err
);
281 int bdrv_snapshot_list(BlockDriverState
*bs
,
282 QEMUSnapshotInfo
**psn_info
)
284 BlockDriver
*drv
= bs
->drv
;
288 if (drv
->bdrv_snapshot_list
) {
289 return drv
->bdrv_snapshot_list(bs
, psn_info
);
292 return bdrv_snapshot_list(bs
->file
->bs
, psn_info
);
298 * Temporarily load an internal snapshot by @snapshot_id and @name.
299 * @bs: block device used in the operation
300 * @snapshot_id: unique snapshot ID, or NULL
301 * @name: snapshot name, or NULL
302 * @errp: location to store error
304 * If both @snapshot_id and @name are specified, load the first one with
305 * id @snapshot_id and name @name.
306 * If only @snapshot_id is specified, load the first one with id
308 * If only @name is specified, load the first one with name @name.
309 * if none is specified, return -EINVAL.
311 * Returns: 0 on success, -errno on fail. If @bs is not inserted, return
312 * -ENOMEDIUM. If @bs is not readonly, return -EINVAL. If @bs did not support
313 * internal snapshot, return -ENOTSUP. If qemu can't find a matching @id and
314 * @name, return -ENOENT. If @errp != NULL, it will always be filled on
317 int bdrv_snapshot_load_tmp(BlockDriverState
*bs
,
318 const char *snapshot_id
,
322 BlockDriver
*drv
= bs
->drv
;
325 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, bdrv_get_device_name(bs
));
328 if (!snapshot_id
&& !name
) {
329 error_setg(errp
, "snapshot_id and name are both NULL");
332 if (!bs
->read_only
) {
333 error_setg(errp
, "Device is not readonly");
336 if (drv
->bdrv_snapshot_load_tmp
) {
337 return drv
->bdrv_snapshot_load_tmp(bs
, snapshot_id
, name
, errp
);
339 error_setg(errp
, "Block format '%s' used by device '%s' "
340 "does not support temporarily loading internal snapshots",
341 drv
->format_name
, bdrv_get_device_name(bs
));
345 int bdrv_snapshot_load_tmp_by_id_or_name(BlockDriverState
*bs
,
346 const char *id_or_name
,
350 Error
*local_err
= NULL
;
352 ret
= bdrv_snapshot_load_tmp(bs
, id_or_name
, NULL
, &local_err
);
353 if (ret
== -ENOENT
|| ret
== -EINVAL
) {
354 error_free(local_err
);
356 ret
= bdrv_snapshot_load_tmp(bs
, NULL
, id_or_name
, &local_err
);
360 error_propagate(errp
, local_err
);
367 /* Group operations. All block drivers are involved.
368 * These functions will properly handle dataplane (take aio_context_acquire
369 * when appropriate for appropriate block drivers) */
371 bool bdrv_all_can_snapshot(BlockDriverState
**first_bad_bs
)
374 BlockDriverState
*bs
= NULL
;
376 while (ok
&& (bs
= bdrv_next(bs
))) {
377 AioContext
*ctx
= bdrv_get_aio_context(bs
);
379 aio_context_acquire(ctx
);
380 if (bdrv_is_inserted(bs
) && !bdrv_is_read_only(bs
)) {
381 ok
= bdrv_can_snapshot(bs
);
383 aio_context_release(ctx
);
390 int bdrv_all_delete_snapshot(const char *name
, BlockDriverState
**first_bad_bs
,
394 BlockDriverState
*bs
= NULL
;
395 QEMUSnapshotInfo sn1
, *snapshot
= &sn1
;
397 while (ret
== 0 && (bs
= bdrv_next(bs
))) {
398 AioContext
*ctx
= bdrv_get_aio_context(bs
);
400 aio_context_acquire(ctx
);
401 if (bdrv_can_snapshot(bs
) &&
402 bdrv_snapshot_find(bs
, snapshot
, name
) >= 0) {
403 ret
= bdrv_snapshot_delete_by_id_or_name(bs
, name
, err
);
405 aio_context_release(ctx
);
413 int bdrv_all_goto_snapshot(const char *name
, BlockDriverState
**first_bad_bs
)
416 BlockDriverState
*bs
= NULL
;
418 while (err
== 0 && (bs
= bdrv_next(bs
))) {
419 AioContext
*ctx
= bdrv_get_aio_context(bs
);
421 aio_context_acquire(ctx
);
422 if (bdrv_can_snapshot(bs
)) {
423 err
= bdrv_snapshot_goto(bs
, name
);
425 aio_context_release(ctx
);
432 int bdrv_all_find_snapshot(const char *name
, BlockDriverState
**first_bad_bs
)
436 BlockDriverState
*bs
= NULL
;
438 while (err
== 0 && (bs
= bdrv_next(bs
))) {
439 AioContext
*ctx
= bdrv_get_aio_context(bs
);
441 aio_context_acquire(ctx
);
442 if (bdrv_can_snapshot(bs
)) {
443 err
= bdrv_snapshot_find(bs
, &sn
, name
);
445 aio_context_release(ctx
);
452 int bdrv_all_create_snapshot(QEMUSnapshotInfo
*sn
,
453 BlockDriverState
*vm_state_bs
,
454 uint64_t vm_state_size
,
455 BlockDriverState
**first_bad_bs
)
458 BlockDriverState
*bs
= NULL
;
460 while (err
== 0 && (bs
= bdrv_next(bs
))) {
461 AioContext
*ctx
= bdrv_get_aio_context(bs
);
463 aio_context_acquire(ctx
);
464 if (bs
== vm_state_bs
) {
465 sn
->vm_state_size
= vm_state_size
;
466 err
= bdrv_snapshot_create(bs
, sn
);
467 } else if (bdrv_can_snapshot(bs
)) {
468 sn
->vm_state_size
= 0;
469 err
= bdrv_snapshot_create(bs
, sn
);
471 aio_context_release(ctx
);
478 BlockDriverState
*bdrv_all_find_vmstate_bs(void)
480 bool not_found
= true;
481 BlockDriverState
*bs
= NULL
;
483 while (not_found
&& (bs
= bdrv_next(bs
))) {
484 AioContext
*ctx
= bdrv_get_aio_context(bs
);
486 aio_context_acquire(ctx
);
487 not_found
= !bdrv_can_snapshot(bs
);
488 aio_context_release(ctx
);