]> git.proxmox.com Git - qemu.git/blame - block/snapshot.c
vfio-pci: Fix multifunction=on
[qemu.git] / block / snapshot.c
CommitLineData
de08c606
WX
1/*
2 * Block layer snapshot related functions
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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
22 * THE SOFTWARE.
23 */
24
25#include "block/snapshot.h"
26#include "block/block_int.h"
27
28int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
29 const char *name)
30{
31 QEMUSnapshotInfo *sn_tab, *sn;
32 int nb_sns, i, ret;
33
34 ret = -ENOENT;
35 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
36 if (nb_sns < 0) {
37 return ret;
38 }
39 for (i = 0; i < nb_sns; i++) {
40 sn = &sn_tab[i];
41 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
42 *sn_info = *sn;
43 ret = 0;
44 break;
45 }
46 }
47 g_free(sn_tab);
48 return ret;
49}
50
2ea1dd75
WX
51/**
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
58 *
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
62 * name @name.
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().
66 *
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.
70 */
71bool bdrv_snapshot_find_by_id_and_name(BlockDriverState *bs,
72 const char *id,
73 const char *name,
74 QEMUSnapshotInfo *sn_info,
75 Error **errp)
76{
77 QEMUSnapshotInfo *sn_tab, *sn;
78 int nb_sns, i;
79 bool ret = false;
80
81 assert(id || name);
82
83 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
84 if (nb_sns < 0) {
85 error_setg_errno(errp, -nb_sns, "Failed to get a snapshot list");
86 return false;
87 } else if (nb_sns == 0) {
88 return false;
89 }
90
91 if (id && name) {
92 for (i = 0; i < nb_sns; i++) {
93 sn = &sn_tab[i];
94 if (!strcmp(sn->id_str, id) && !strcmp(sn->name, name)) {
95 *sn_info = *sn;
96 ret = true;
97 break;
98 }
99 }
100 } else if (id) {
101 for (i = 0; i < nb_sns; i++) {
102 sn = &sn_tab[i];
103 if (!strcmp(sn->id_str, id)) {
104 *sn_info = *sn;
105 ret = true;
106 break;
107 }
108 }
109 } else if (name) {
110 for (i = 0; i < nb_sns; i++) {
111 sn = &sn_tab[i];
112 if (!strcmp(sn->name, name)) {
113 *sn_info = *sn;
114 ret = true;
115 break;
116 }
117 }
118 }
119
120 g_free(sn_tab);
121 return ret;
122}
123
de08c606
WX
124int bdrv_can_snapshot(BlockDriverState *bs)
125{
126 BlockDriver *drv = bs->drv;
127 if (!drv || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
128 return 0;
129 }
130
131 if (!drv->bdrv_snapshot_create) {
132 if (bs->file != NULL) {
133 return bdrv_can_snapshot(bs->file);
134 }
135 return 0;
136 }
137
138 return 1;
139}
140
141int bdrv_snapshot_create(BlockDriverState *bs,
142 QEMUSnapshotInfo *sn_info)
143{
144 BlockDriver *drv = bs->drv;
145 if (!drv) {
146 return -ENOMEDIUM;
147 }
148 if (drv->bdrv_snapshot_create) {
149 return drv->bdrv_snapshot_create(bs, sn_info);
150 }
151 if (bs->file) {
152 return bdrv_snapshot_create(bs->file, sn_info);
153 }
154 return -ENOTSUP;
155}
156
157int bdrv_snapshot_goto(BlockDriverState *bs,
158 const char *snapshot_id)
159{
160 BlockDriver *drv = bs->drv;
161 int ret, open_ret;
162
163 if (!drv) {
164 return -ENOMEDIUM;
165 }
166 if (drv->bdrv_snapshot_goto) {
167 return drv->bdrv_snapshot_goto(bs, snapshot_id);
168 }
169
170 if (bs->file) {
171 drv->bdrv_close(bs);
172 ret = bdrv_snapshot_goto(bs->file, snapshot_id);
015a1036 173 open_ret = drv->bdrv_open(bs, NULL, bs->open_flags, NULL);
de08c606 174 if (open_ret < 0) {
4f6fd349 175 bdrv_unref(bs->file);
de08c606
WX
176 bs->drv = NULL;
177 return open_ret;
178 }
179 return ret;
180 }
181
182 return -ENOTSUP;
183}
184
a89d89d3
WX
185/**
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
191 *
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
195 * @snapshot_id.
196 * If only @name is specified, delete the first one with name @name.
197 * if none is specified, return -ENINVAL.
198 *
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.
206 */
207int bdrv_snapshot_delete(BlockDriverState *bs,
208 const char *snapshot_id,
209 const char *name,
210 Error **errp)
de08c606
WX
211{
212 BlockDriver *drv = bs->drv;
213 if (!drv) {
a89d89d3 214 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, bdrv_get_device_name(bs));
de08c606
WX
215 return -ENOMEDIUM;
216 }
a89d89d3
WX
217 if (!snapshot_id && !name) {
218 error_setg(errp, "snapshot_id and name are both NULL");
219 return -EINVAL;
220 }
de08c606 221 if (drv->bdrv_snapshot_delete) {
a89d89d3 222 return drv->bdrv_snapshot_delete(bs, snapshot_id, name, errp);
de08c606
WX
223 }
224 if (bs->file) {
a89d89d3 225 return bdrv_snapshot_delete(bs->file, snapshot_id, name, errp);
de08c606 226 }
a89d89d3
WX
227 error_set(errp, QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
228 drv->format_name, bdrv_get_device_name(bs),
229 "internal snapshot deletion");
de08c606
WX
230 return -ENOTSUP;
231}
232
a89d89d3
WX
233void bdrv_snapshot_delete_by_id_or_name(BlockDriverState *bs,
234 const char *id_or_name,
235 Error **errp)
236{
237 int ret;
238 Error *local_err = NULL;
239
240 ret = bdrv_snapshot_delete(bs, id_or_name, NULL, &local_err);
241 if (ret == -ENOENT || ret == -EINVAL) {
242 error_free(local_err);
243 local_err = NULL;
244 ret = bdrv_snapshot_delete(bs, NULL, id_or_name, &local_err);
245 }
246
247 if (ret < 0) {
248 error_propagate(errp, local_err);
249 }
250}
251
de08c606
WX
252int bdrv_snapshot_list(BlockDriverState *bs,
253 QEMUSnapshotInfo **psn_info)
254{
255 BlockDriver *drv = bs->drv;
256 if (!drv) {
257 return -ENOMEDIUM;
258 }
259 if (drv->bdrv_snapshot_list) {
260 return drv->bdrv_snapshot_list(bs, psn_info);
261 }
262 if (bs->file) {
263 return bdrv_snapshot_list(bs->file, psn_info);
264 }
265 return -ENOTSUP;
266}
267
268int bdrv_snapshot_load_tmp(BlockDriverState *bs,
269 const char *snapshot_name)
270{
271 BlockDriver *drv = bs->drv;
272 if (!drv) {
273 return -ENOMEDIUM;
274 }
275 if (!bs->read_only) {
276 return -EINVAL;
277 }
278 if (drv->bdrv_snapshot_load_tmp) {
279 return drv->bdrv_snapshot_load_tmp(bs, snapshot_name);
280 }
281 return -ENOTSUP;
282}