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/qdict.h"
30 #include "qapi/qmp/qerror.h"
31 #include "qapi/qmp/qstring.h"
32 #include "qemu/option.h"
34 QemuOptsList internal_snapshot_opts = {
36 .head = QTAILQ_HEAD_INITIALIZER(internal_snapshot_opts.head),
39 .name = SNAPSHOT_OPT_ID,
40 .type = QEMU_OPT_STRING,
43 .name = SNAPSHOT_OPT_NAME,
44 .type = QEMU_OPT_STRING,
45 .help = "snapshot name"
52 int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
55 QEMUSnapshotInfo *sn_tab, *sn;
59 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
63 for (i = 0; i < nb_sns; i++) {
65 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
76 * Look up an internal snapshot by @id and @name.
77 * @bs: block device to search
78 * @id: unique snapshot ID, or NULL
79 * @name: snapshot name, or NULL
80 * @sn_info: location to store information on the snapshot found
81 * @errp: location to store error, will be set only for exception
83 * This function will traverse snapshot list in @bs to search the matching
84 * one, @id and @name are the matching condition:
85 * If both @id and @name are specified, find the first one with id @id and
87 * If only @id is specified, find the first one with id @id.
88 * If only @name is specified, find the first one with name @name.
89 * if none is specified, abort().
91 * Returns: true when a snapshot is found and @sn_info will be filled, false
92 * when error or not found. If all operation succeed but no matching one is
93 * found, @errp will NOT be set.
95 bool bdrv_snapshot_find_by_id_and_name(BlockDriverState *bs,
98 QEMUSnapshotInfo *sn_info,
101 QEMUSnapshotInfo *sn_tab, *sn;
107 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
109 error_setg_errno(errp, -nb_sns, "Failed to get a snapshot list");
111 } else if (nb_sns == 0) {
116 for (i = 0; i < nb_sns; i++) {
118 if (!strcmp(sn->id_str, id) && !strcmp(sn->name, name)) {
125 for (i = 0; i < nb_sns; i++) {
127 if (!strcmp(sn->id_str, id)) {
134 for (i = 0; i < nb_sns; i++) {
136 if (!strcmp(sn->name, name)) {
148 int bdrv_can_snapshot(BlockDriverState *bs)
150 BlockDriver *drv = bs->drv;
151 if (!drv || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
155 if (!drv->bdrv_snapshot_create) {
156 if (bs->file != NULL) {
157 return bdrv_can_snapshot(bs->file->bs);
165 int bdrv_snapshot_create(BlockDriverState *bs,
166 QEMUSnapshotInfo *sn_info)
168 BlockDriver *drv = bs->drv;
172 if (drv->bdrv_snapshot_create) {
173 return drv->bdrv_snapshot_create(bs, sn_info);
176 return bdrv_snapshot_create(bs->file->bs, sn_info);
181 int bdrv_snapshot_goto(BlockDriverState *bs,
182 const char *snapshot_id,
185 BlockDriver *drv = bs->drv;
189 error_setg(errp, "Block driver is closed");
193 if (!QLIST_EMPTY(&bs->dirty_bitmaps)) {
194 error_setg(errp, "Device has active dirty bitmaps");
198 if (drv->bdrv_snapshot_goto) {
199 ret = drv->bdrv_snapshot_goto(bs, snapshot_id);
201 error_setg_errno(errp, -ret, "Failed to load snapshot");
207 BlockDriverState *file;
208 QDict *options = qdict_clone_shallow(bs->options);
210 Error *local_err = NULL;
213 /* Prevent it from getting deleted when detached from bs */
216 qdict_extract_subqdict(options, &file_options, "file.");
217 QDECREF(file_options);
218 qdict_put_str(options, "file", bdrv_get_node_name(file));
221 bdrv_unref_child(bs, bs->file);
224 ret = bdrv_snapshot_goto(file, snapshot_id, errp);
225 open_ret = drv->bdrv_open(bs, options, bs->open_flags, &local_err);
230 /* A bdrv_snapshot_goto() error takes precedence */
231 error_propagate(errp, local_err);
232 return ret < 0 ? ret : open_ret;
235 assert(bs->file->bs == file);
240 error_setg(errp, "Block driver does not support snapshots");
245 * Delete an internal snapshot by @snapshot_id and @name.
246 * @bs: block device used in the operation
247 * @snapshot_id: unique snapshot ID, or NULL
248 * @name: snapshot name, or NULL
249 * @errp: location to store error
251 * If both @snapshot_id and @name are specified, delete the first one with
252 * id @snapshot_id and name @name.
253 * If only @snapshot_id is specified, delete the first one with id
255 * If only @name is specified, delete the first one with name @name.
256 * if none is specified, return -EINVAL.
258 * Returns: 0 on success, -errno on failure. If @bs is not inserted, return
259 * -ENOMEDIUM. If @snapshot_id and @name are both NULL, return -EINVAL. If @bs
260 * does not support internal snapshot deletion, return -ENOTSUP. If @bs does
261 * not support parameter @snapshot_id or @name, or one of them is not correctly
262 * specified, return -EINVAL. If @bs can't find one matching @id and @name,
263 * return -ENOENT. If @errp != NULL, it will always be filled with error
264 * message on failure.
266 int bdrv_snapshot_delete(BlockDriverState *bs,
267 const char *snapshot_id,
271 BlockDriver *drv = bs->drv;
275 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, bdrv_get_device_name(bs));
278 if (!snapshot_id && !name) {
279 error_setg(errp, "snapshot_id and name are both NULL");
283 /* drain all pending i/o before deleting snapshot */
284 bdrv_drained_begin(bs);
286 if (drv->bdrv_snapshot_delete) {
287 ret = drv->bdrv_snapshot_delete(bs, snapshot_id, name, errp);
288 } else if (bs->file) {
289 ret = bdrv_snapshot_delete(bs->file->bs, snapshot_id, name, errp);
291 error_setg(errp, "Block format '%s' used by device '%s' "
292 "does not support internal snapshot deletion",
293 drv->format_name, bdrv_get_device_name(bs));
297 bdrv_drained_end(bs);
301 int bdrv_snapshot_delete_by_id_or_name(BlockDriverState *bs,
302 const char *id_or_name,
306 Error *local_err = NULL;
308 ret = bdrv_snapshot_delete(bs, id_or_name, NULL, &local_err);
309 if (ret == -ENOENT || ret == -EINVAL) {
310 error_free(local_err);
312 ret = bdrv_snapshot_delete(bs, NULL, id_or_name, &local_err);
316 error_propagate(errp, local_err);
321 int bdrv_snapshot_list(BlockDriverState *bs,
322 QEMUSnapshotInfo **psn_info)
324 BlockDriver *drv = bs->drv;
328 if (drv->bdrv_snapshot_list) {
329 return drv->bdrv_snapshot_list(bs, psn_info);
332 return bdrv_snapshot_list(bs->file->bs, psn_info);
338 * Temporarily load an internal snapshot by @snapshot_id and @name.
339 * @bs: block device used in the operation
340 * @snapshot_id: unique snapshot ID, or NULL
341 * @name: snapshot name, or NULL
342 * @errp: location to store error
344 * If both @snapshot_id and @name are specified, load the first one with
345 * id @snapshot_id and name @name.
346 * If only @snapshot_id is specified, load the first one with id
348 * If only @name is specified, load the first one with name @name.
349 * if none is specified, return -EINVAL.
351 * Returns: 0 on success, -errno on fail. If @bs is not inserted, return
352 * -ENOMEDIUM. If @bs is not readonly, return -EINVAL. If @bs did not support
353 * internal snapshot, return -ENOTSUP. If qemu can't find a matching @id and
354 * @name, return -ENOENT. If @errp != NULL, it will always be filled on
357 int bdrv_snapshot_load_tmp(BlockDriverState *bs,
358 const char *snapshot_id,
362 BlockDriver *drv = bs->drv;
365 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, bdrv_get_device_name(bs));
368 if (!snapshot_id && !name) {
369 error_setg(errp, "snapshot_id and name are both NULL");
372 if (!bs->read_only) {
373 error_setg(errp, "Device is not readonly");
376 if (drv->bdrv_snapshot_load_tmp) {
377 return drv->bdrv_snapshot_load_tmp(bs, snapshot_id, name, errp);
379 error_setg(errp, "Block format '%s' used by device '%s' "
380 "does not support temporarily loading internal snapshots",
381 drv->format_name, bdrv_get_device_name(bs));
385 int bdrv_snapshot_load_tmp_by_id_or_name(BlockDriverState *bs,
386 const char *id_or_name,
390 Error *local_err = NULL;
392 ret = bdrv_snapshot_load_tmp(bs, id_or_name, NULL, &local_err);
393 if (ret == -ENOENT || ret == -EINVAL) {
394 error_free(local_err);
396 ret = bdrv_snapshot_load_tmp(bs, NULL, id_or_name, &local_err);
399 error_propagate(errp, local_err);
405 /* Group operations. All block drivers are involved.
406 * These functions will properly handle dataplane (take aio_context_acquire
407 * when appropriate for appropriate block drivers) */
409 bool bdrv_all_can_snapshot(BlockDriverState **first_bad_bs)
412 BlockDriverState *bs;
415 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
416 AioContext *ctx = bdrv_get_aio_context(bs);
418 aio_context_acquire(ctx);
419 if (bdrv_is_inserted(bs) && !bdrv_is_read_only(bs)) {
420 ok = bdrv_can_snapshot(bs);
422 aio_context_release(ctx);
424 bdrv_next_cleanup(&it);
434 int bdrv_all_delete_snapshot(const char *name, BlockDriverState **first_bad_bs,
438 BlockDriverState *bs;
440 QEMUSnapshotInfo sn1, *snapshot = &sn1;
442 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
443 AioContext *ctx = bdrv_get_aio_context(bs);
445 aio_context_acquire(ctx);
446 if (bdrv_can_snapshot(bs) &&
447 bdrv_snapshot_find(bs, snapshot, name) >= 0) {
448 ret = bdrv_snapshot_delete_by_id_or_name(bs, name, err);
450 aio_context_release(ctx);
452 bdrv_next_cleanup(&it);
463 int bdrv_all_goto_snapshot(const char *name, BlockDriverState **first_bad_bs,
467 BlockDriverState *bs;
470 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
471 AioContext *ctx = bdrv_get_aio_context(bs);
473 aio_context_acquire(ctx);
474 if (bdrv_can_snapshot(bs)) {
475 ret = bdrv_snapshot_goto(bs, name, errp);
477 aio_context_release(ctx);
479 bdrv_next_cleanup(&it);
489 int bdrv_all_find_snapshot(const char *name, BlockDriverState **first_bad_bs)
493 BlockDriverState *bs;
496 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
497 AioContext *ctx = bdrv_get_aio_context(bs);
499 aio_context_acquire(ctx);
500 if (bdrv_can_snapshot(bs)) {
501 err = bdrv_snapshot_find(bs, &sn, name);
503 aio_context_release(ctx);
505 bdrv_next_cleanup(&it);
515 int bdrv_all_create_snapshot(QEMUSnapshotInfo *sn,
516 BlockDriverState *vm_state_bs,
517 uint64_t vm_state_size,
518 BlockDriverState **first_bad_bs)
521 BlockDriverState *bs;
524 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
525 AioContext *ctx = bdrv_get_aio_context(bs);
527 aio_context_acquire(ctx);
528 if (bs == vm_state_bs) {
529 sn->vm_state_size = vm_state_size;
530 err = bdrv_snapshot_create(bs, sn);
531 } else if (bdrv_can_snapshot(bs)) {
532 sn->vm_state_size = 0;
533 err = bdrv_snapshot_create(bs, sn);
535 aio_context_release(ctx);
537 bdrv_next_cleanup(&it);
547 BlockDriverState *bdrv_all_find_vmstate_bs(void)
549 BlockDriverState *bs;
552 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
553 AioContext *ctx = bdrv_get_aio_context(bs);
556 aio_context_acquire(ctx);
557 found = bdrv_can_snapshot(bs);
558 aio_context_release(ctx);
561 bdrv_next_cleanup(&it);