target-ppc: Little Endian Correction to Load/Store Vector Element
[qemu.git] / block / snapshot.c
bloba05c0c0be03437eddbc990be68eabf909a0c1075
1 /*
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
22 * THE SOFTWARE.
25 #include "block/snapshot.h"
26 #include "block/block_int.h"
28 int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
29 const char *name)
31 QEMUSnapshotInfo *sn_tab, *sn;
32 int nb_sns, i, ret;
34 ret = -ENOENT;
35 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
36 if (nb_sns < 0) {
37 return ret;
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;
47 g_free(sn_tab);
48 return ret;
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
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().
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,
72 const char *id,
73 const char *name,
74 QEMUSnapshotInfo *sn_info,
75 Error **errp)
77 QEMUSnapshotInfo *sn_tab, *sn;
78 int nb_sns, i;
79 bool ret = false;
81 assert(id || name);
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;
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;
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;
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;
120 g_free(sn_tab);
121 return ret;
124 int bdrv_can_snapshot(BlockDriverState *bs)
126 BlockDriver *drv = bs->drv;
127 if (!drv || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
128 return 0;
131 if (!drv->bdrv_snapshot_create) {
132 if (bs->file != NULL) {
133 return bdrv_can_snapshot(bs->file);
135 return 0;
138 return 1;
141 int bdrv_snapshot_create(BlockDriverState *bs,
142 QEMUSnapshotInfo *sn_info)
144 BlockDriver *drv = bs->drv;
145 if (!drv) {
146 return -ENOMEDIUM;
148 if (drv->bdrv_snapshot_create) {
149 return drv->bdrv_snapshot_create(bs, sn_info);
151 if (bs->file) {
152 return bdrv_snapshot_create(bs->file, sn_info);
154 return -ENOTSUP;
157 int bdrv_snapshot_goto(BlockDriverState *bs,
158 const char *snapshot_id)
160 BlockDriver *drv = bs->drv;
161 int ret, open_ret;
163 if (!drv) {
164 return -ENOMEDIUM;
166 if (drv->bdrv_snapshot_goto) {
167 return drv->bdrv_snapshot_goto(bs, snapshot_id);
170 if (bs->file) {
171 drv->bdrv_close(bs);
172 ret = bdrv_snapshot_goto(bs->file, snapshot_id);
173 open_ret = drv->bdrv_open(bs, NULL, bs->open_flags, NULL);
174 if (open_ret < 0) {
175 bdrv_unref(bs->file);
176 bs->drv = NULL;
177 return open_ret;
179 return ret;
182 return -ENOTSUP;
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
195 * @snapshot_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,
209 const char *name,
210 Error **errp)
212 BlockDriver *drv = bs->drv;
213 if (!drv) {
214 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, bdrv_get_device_name(bs));
215 return -ENOMEDIUM;
217 if (!snapshot_id && !name) {
218 error_setg(errp, "snapshot_id and name are both NULL");
219 return -EINVAL;
221 if (drv->bdrv_snapshot_delete) {
222 return drv->bdrv_snapshot_delete(bs, snapshot_id, name, errp);
224 if (bs->file) {
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");
230 return -ENOTSUP;
233 void bdrv_snapshot_delete_by_id_or_name(BlockDriverState *bs,
234 const char *id_or_name,
235 Error **errp)
237 int ret;
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);
243 local_err = NULL;
244 ret = bdrv_snapshot_delete(bs, NULL, id_or_name, &local_err);
247 if (ret < 0) {
248 error_propagate(errp, local_err);
252 int bdrv_snapshot_list(BlockDriverState *bs,
253 QEMUSnapshotInfo **psn_info)
255 BlockDriver *drv = bs->drv;
256 if (!drv) {
257 return -ENOMEDIUM;
259 if (drv->bdrv_snapshot_list) {
260 return drv->bdrv_snapshot_list(bs, psn_info);
262 if (bs->file) {
263 return bdrv_snapshot_list(bs->file, psn_info);
265 return -ENOTSUP;
268 int bdrv_snapshot_load_tmp(BlockDriverState *bs,
269 const char *snapshot_name)
271 BlockDriver *drv = bs->drv;
272 if (!drv) {
273 return -ENOMEDIUM;
275 if (!bs->read_only) {
276 return -EINVAL;
278 if (drv->bdrv_snapshot_load_tmp) {
279 return drv->bdrv_snapshot_load_tmp(bs, snapshot_name);
281 return -ENOTSUP;