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 QemuOptsList internal_snapshot_opts
= {
30 .head
= QTAILQ_HEAD_INITIALIZER(internal_snapshot_opts
.head
),
33 .name
= SNAPSHOT_OPT_ID
,
34 .type
= QEMU_OPT_STRING
,
37 .name
= SNAPSHOT_OPT_NAME
,
38 .type
= QEMU_OPT_STRING
,
39 .help
= "snapshot name"
46 int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
49 QEMUSnapshotInfo
*sn_tab
, *sn
;
53 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
57 for (i
= 0; i
< nb_sns
; i
++) {
59 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
70 * Look up an internal snapshot by @id and @name.
71 * @bs: block device to search
72 * @id: unique snapshot ID, or NULL
73 * @name: snapshot name, or NULL
74 * @sn_info: location to store information on the snapshot found
75 * @errp: location to store error, will be set only for exception
77 * This function will traverse snapshot list in @bs to search the matching
78 * one, @id and @name are the matching condition:
79 * If both @id and @name are specified, find the first one with id @id and
81 * If only @id is specified, find the first one with id @id.
82 * If only @name is specified, find the first one with name @name.
83 * if none is specified, abort().
85 * Returns: true when a snapshot is found and @sn_info will be filled, false
86 * when error or not found. If all operation succeed but no matching one is
87 * found, @errp will NOT be set.
89 bool bdrv_snapshot_find_by_id_and_name(BlockDriverState
*bs
,
92 QEMUSnapshotInfo
*sn_info
,
95 QEMUSnapshotInfo
*sn_tab
, *sn
;
101 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
103 error_setg_errno(errp
, -nb_sns
, "Failed to get a snapshot list");
105 } else if (nb_sns
== 0) {
110 for (i
= 0; i
< nb_sns
; i
++) {
112 if (!strcmp(sn
->id_str
, id
) && !strcmp(sn
->name
, name
)) {
119 for (i
= 0; i
< nb_sns
; i
++) {
121 if (!strcmp(sn
->id_str
, id
)) {
128 for (i
= 0; i
< nb_sns
; i
++) {
130 if (!strcmp(sn
->name
, name
)) {
142 int bdrv_can_snapshot(BlockDriverState
*bs
)
144 BlockDriver
*drv
= bs
->drv
;
145 if (!drv
|| !bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
149 if (!drv
->bdrv_snapshot_create
) {
150 if (bs
->file
!= NULL
) {
151 return bdrv_can_snapshot(bs
->file
);
159 int bdrv_snapshot_create(BlockDriverState
*bs
,
160 QEMUSnapshotInfo
*sn_info
)
162 BlockDriver
*drv
= bs
->drv
;
166 if (drv
->bdrv_snapshot_create
) {
167 return drv
->bdrv_snapshot_create(bs
, sn_info
);
170 return bdrv_snapshot_create(bs
->file
, sn_info
);
175 int bdrv_snapshot_goto(BlockDriverState
*bs
,
176 const char *snapshot_id
)
178 BlockDriver
*drv
= bs
->drv
;
184 if (drv
->bdrv_snapshot_goto
) {
185 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
190 ret
= bdrv_snapshot_goto(bs
->file
, snapshot_id
);
191 open_ret
= drv
->bdrv_open(bs
, NULL
, bs
->open_flags
, NULL
);
193 bdrv_unref(bs
->file
);
204 * Delete an internal snapshot by @snapshot_id and @name.
205 * @bs: block device used in the operation
206 * @snapshot_id: unique snapshot ID, or NULL
207 * @name: snapshot name, or NULL
208 * @errp: location to store error
210 * If both @snapshot_id and @name are specified, delete the first one with
211 * id @snapshot_id and name @name.
212 * If only @snapshot_id is specified, delete the first one with id
214 * If only @name is specified, delete the first one with name @name.
215 * if none is specified, return -EINVAL.
217 * Returns: 0 on success, -errno on failure. If @bs is not inserted, return
218 * -ENOMEDIUM. If @snapshot_id and @name are both NULL, return -EINVAL. If @bs
219 * does not support internal snapshot deletion, return -ENOTSUP. If @bs does
220 * not support parameter @snapshot_id or @name, or one of them is not correctly
221 * specified, return -EINVAL. If @bs can't find one matching @id and @name,
222 * return -ENOENT. If @errp != NULL, it will always be filled with error
223 * message on failure.
225 int bdrv_snapshot_delete(BlockDriverState
*bs
,
226 const char *snapshot_id
,
230 BlockDriver
*drv
= bs
->drv
;
232 error_set(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, bdrv_get_device_name(bs
));
235 if (!snapshot_id
&& !name
) {
236 error_setg(errp
, "snapshot_id and name are both NULL");
240 /* drain all pending i/o before deleting snapshot */
243 if (drv
->bdrv_snapshot_delete
) {
244 return drv
->bdrv_snapshot_delete(bs
, snapshot_id
, name
, errp
);
247 return bdrv_snapshot_delete(bs
->file
, snapshot_id
, name
, errp
);
249 error_set(errp
, QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED
,
250 drv
->format_name
, bdrv_get_device_name(bs
),
251 "internal snapshot deletion");
255 void bdrv_snapshot_delete_by_id_or_name(BlockDriverState
*bs
,
256 const char *id_or_name
,
260 Error
*local_err
= NULL
;
262 ret
= bdrv_snapshot_delete(bs
, id_or_name
, NULL
, &local_err
);
263 if (ret
== -ENOENT
|| ret
== -EINVAL
) {
264 error_free(local_err
);
266 ret
= bdrv_snapshot_delete(bs
, NULL
, id_or_name
, &local_err
);
270 error_propagate(errp
, local_err
);
274 int bdrv_snapshot_list(BlockDriverState
*bs
,
275 QEMUSnapshotInfo
**psn_info
)
277 BlockDriver
*drv
= bs
->drv
;
281 if (drv
->bdrv_snapshot_list
) {
282 return drv
->bdrv_snapshot_list(bs
, psn_info
);
285 return bdrv_snapshot_list(bs
->file
, psn_info
);
291 * Temporarily load an internal snapshot by @snapshot_id and @name.
292 * @bs: block device used in the operation
293 * @snapshot_id: unique snapshot ID, or NULL
294 * @name: snapshot name, or NULL
295 * @errp: location to store error
297 * If both @snapshot_id and @name are specified, load the first one with
298 * id @snapshot_id and name @name.
299 * If only @snapshot_id is specified, load the first one with id
301 * If only @name is specified, load the first one with name @name.
302 * if none is specified, return -EINVAL.
304 * Returns: 0 on success, -errno on fail. If @bs is not inserted, return
305 * -ENOMEDIUM. If @bs is not readonly, return -EINVAL. If @bs did not support
306 * internal snapshot, return -ENOTSUP. If qemu can't find a matching @id and
307 * @name, return -ENOENT. If @errp != NULL, it will always be filled on
310 int bdrv_snapshot_load_tmp(BlockDriverState
*bs
,
311 const char *snapshot_id
,
315 BlockDriver
*drv
= bs
->drv
;
318 error_set(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, bdrv_get_device_name(bs
));
321 if (!snapshot_id
&& !name
) {
322 error_setg(errp
, "snapshot_id and name are both NULL");
325 if (!bs
->read_only
) {
326 error_setg(errp
, "Device is not readonly");
329 if (drv
->bdrv_snapshot_load_tmp
) {
330 return drv
->bdrv_snapshot_load_tmp(bs
, snapshot_id
, name
, errp
);
332 error_set(errp
, QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED
,
333 drv
->format_name
, bdrv_get_device_name(bs
),
334 "temporarily load internal snapshot");
338 int bdrv_snapshot_load_tmp_by_id_or_name(BlockDriverState
*bs
,
339 const char *id_or_name
,
343 Error
*local_err
= NULL
;
345 ret
= bdrv_snapshot_load_tmp(bs
, id_or_name
, NULL
, &local_err
);
346 if (ret
== -ENOENT
|| ret
== -EINVAL
) {
347 error_free(local_err
);
349 ret
= bdrv_snapshot_load_tmp(bs
, NULL
, id_or_name
, &local_err
);
353 error_propagate(errp
, local_err
);