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
)) {
52 * Look up an internal snapshot by @id and @name.
53 * @bs: block device to search
54 * @id: unique snapshot ID, or NULL
55 * @name: snapshot name, or NULL
56 * @sn_info: location to store information on the snapshot found
57 * @errp: location to store error, will be set only for exception
59 * This function will traverse snapshot list in @bs to search the matching
60 * one, @id and @name are the matching condition:
61 * If both @id and @name are specified, find the first one with id @id and
63 * If only @id is specified, find the first one with id @id.
64 * If only @name is specified, find the first one with name @name.
65 * if none is specified, abort().
67 * Returns: true when a snapshot is found and @sn_info will be filled, false
68 * when error or not found. If all operation succeed but no matching one is
69 * found, @errp will NOT be set.
71 bool bdrv_snapshot_find_by_id_and_name(BlockDriverState
*bs
,
74 QEMUSnapshotInfo
*sn_info
,
77 QEMUSnapshotInfo
*sn_tab
, *sn
;
83 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
85 error_setg_errno(errp
, -nb_sns
, "Failed to get a snapshot list");
87 } else if (nb_sns
== 0) {
92 for (i
= 0; i
< nb_sns
; i
++) {
94 if (!strcmp(sn
->id_str
, id
) && !strcmp(sn
->name
, name
)) {
101 for (i
= 0; i
< nb_sns
; i
++) {
103 if (!strcmp(sn
->id_str
, id
)) {
110 for (i
= 0; i
< nb_sns
; i
++) {
112 if (!strcmp(sn
->name
, name
)) {
124 int bdrv_can_snapshot(BlockDriverState
*bs
)
126 BlockDriver
*drv
= bs
->drv
;
127 if (!drv
|| !bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
131 if (!drv
->bdrv_snapshot_create
) {
132 if (bs
->file
!= NULL
) {
133 return bdrv_can_snapshot(bs
->file
);
141 int bdrv_snapshot_create(BlockDriverState
*bs
,
142 QEMUSnapshotInfo
*sn_info
)
144 BlockDriver
*drv
= bs
->drv
;
148 if (drv
->bdrv_snapshot_create
) {
149 return drv
->bdrv_snapshot_create(bs
, sn_info
);
152 return bdrv_snapshot_create(bs
->file
, sn_info
);
157 int bdrv_snapshot_goto(BlockDriverState
*bs
,
158 const char *snapshot_id
)
160 BlockDriver
*drv
= bs
->drv
;
166 if (drv
->bdrv_snapshot_goto
) {
167 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
172 ret
= bdrv_snapshot_goto(bs
->file
, snapshot_id
);
173 open_ret
= drv
->bdrv_open(bs
, NULL
, bs
->open_flags
, NULL
);
175 bdrv_unref(bs
->file
);
186 * Delete an internal snapshot by @snapshot_id and @name.
187 * @bs: block device used in the operation
188 * @snapshot_id: unique snapshot ID, or NULL
189 * @name: snapshot name, or NULL
190 * @errp: location to store error
192 * If both @snapshot_id and @name are specified, delete the first one with
193 * id @snapshot_id and name @name.
194 * If only @snapshot_id is specified, delete the first one with id
196 * If only @name is specified, delete the first one with name @name.
197 * if none is specified, return -ENINVAL.
199 * Returns: 0 on success, -errno on failure. If @bs is not inserted, return
200 * -ENOMEDIUM. If @snapshot_id and @name are both NULL, return -EINVAL. If @bs
201 * does not support internal snapshot deletion, return -ENOTSUP. If @bs does
202 * not support parameter @snapshot_id or @name, or one of them is not correctly
203 * specified, return -EINVAL. If @bs can't find one matching @id and @name,
204 * return -ENOENT. If @errp != NULL, it will always be filled with error
205 * message on failure.
207 int bdrv_snapshot_delete(BlockDriverState
*bs
,
208 const char *snapshot_id
,
212 BlockDriver
*drv
= bs
->drv
;
214 error_set(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, bdrv_get_device_name(bs
));
217 if (!snapshot_id
&& !name
) {
218 error_setg(errp
, "snapshot_id and name are both NULL");
221 if (drv
->bdrv_snapshot_delete
) {
222 return drv
->bdrv_snapshot_delete(bs
, snapshot_id
, name
, errp
);
225 return bdrv_snapshot_delete(bs
->file
, snapshot_id
, name
, errp
);
227 error_set(errp
, QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED
,
228 drv
->format_name
, bdrv_get_device_name(bs
),
229 "internal snapshot deletion");
233 void bdrv_snapshot_delete_by_id_or_name(BlockDriverState
*bs
,
234 const char *id_or_name
,
238 Error
*local_err
= NULL
;
240 ret
= bdrv_snapshot_delete(bs
, id_or_name
, NULL
, &local_err
);
241 if (ret
== -ENOENT
|| ret
== -EINVAL
) {
242 error_free(local_err
);
244 ret
= bdrv_snapshot_delete(bs
, NULL
, id_or_name
, &local_err
);
248 error_propagate(errp
, local_err
);
252 int bdrv_snapshot_list(BlockDriverState
*bs
,
253 QEMUSnapshotInfo
**psn_info
)
255 BlockDriver
*drv
= bs
->drv
;
259 if (drv
->bdrv_snapshot_list
) {
260 return drv
->bdrv_snapshot_list(bs
, psn_info
);
263 return bdrv_snapshot_list(bs
->file
, psn_info
);
268 int bdrv_snapshot_load_tmp(BlockDriverState
*bs
,
269 const char *snapshot_name
)
271 BlockDriver
*drv
= bs
->drv
;
275 if (!bs
->read_only
) {
278 if (drv
->bdrv_snapshot_load_tmp
) {
279 return drv
->bdrv_snapshot_load_tmp(bs
, snapshot_name
);