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"
28 int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
31 QEMUSnapshotInfo *sn_tab, *sn;
35 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
39 for (i = 0; i < nb_sns; i++) {
41 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
51 int bdrv_can_snapshot(BlockDriverState *bs)
53 BlockDriver *drv = bs->drv;
54 if (!drv || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
58 if (!drv->bdrv_snapshot_create) {
59 if (bs->file != NULL) {
60 return bdrv_can_snapshot(bs->file);
68 int bdrv_snapshot_create(BlockDriverState *bs,
69 QEMUSnapshotInfo *sn_info)
71 BlockDriver *drv = bs->drv;
75 if (drv->bdrv_snapshot_create) {
76 return drv->bdrv_snapshot_create(bs, sn_info);
79 return bdrv_snapshot_create(bs->file, sn_info);
84 int bdrv_snapshot_goto(BlockDriverState *bs,
85 const char *snapshot_id)
87 BlockDriver *drv = bs->drv;
93 if (drv->bdrv_snapshot_goto) {
94 return drv->bdrv_snapshot_goto(bs, snapshot_id);
99 ret = bdrv_snapshot_goto(bs->file, snapshot_id);
100 open_ret = drv->bdrv_open(bs, NULL, bs->open_flags);
102 bdrv_delete(bs->file);
112 int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
114 BlockDriver *drv = bs->drv;
118 if (drv->bdrv_snapshot_delete) {
119 return drv->bdrv_snapshot_delete(bs, snapshot_id);
122 return bdrv_snapshot_delete(bs->file, snapshot_id);
127 int bdrv_snapshot_list(BlockDriverState *bs,
128 QEMUSnapshotInfo **psn_info)
130 BlockDriver *drv = bs->drv;
134 if (drv->bdrv_snapshot_list) {
135 return drv->bdrv_snapshot_list(bs, psn_info);
138 return bdrv_snapshot_list(bs->file, psn_info);
143 int bdrv_snapshot_load_tmp(BlockDriverState *bs,
144 const char *snapshot_name)
146 BlockDriver *drv = bs->drv;
150 if (!bs->read_only) {
153 if (drv->bdrv_snapshot_load_tmp) {
154 return drv->bdrv_snapshot_load_tmp(bs, snapshot_name);