]>
Commit | Line | Data |
---|---|---|
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 | ||
80c71a24 | 25 | #include "qemu/osdep.h" |
de08c606 WX |
26 | #include "block/snapshot.h" |
27 | #include "block/block_int.h" | |
609f45ea | 28 | #include "block/qdict.h" |
da34e65c | 29 | #include "qapi/error.h" |
452fcdbc | 30 | #include "qapi/qmp/qdict.h" |
cc7a8ea7 | 31 | #include "qapi/qmp/qerror.h" |
7a9e5119 | 32 | #include "qapi/qmp/qstring.h" |
922a01a0 | 33 | #include "qemu/option.h" |
05f4aced | 34 | #include "sysemu/block-backend.h" |
de08c606 | 35 | |
8c116b0e WX |
36 | QemuOptsList internal_snapshot_opts = { |
37 | .name = "snapshot", | |
38 | .head = QTAILQ_HEAD_INITIALIZER(internal_snapshot_opts.head), | |
39 | .desc = { | |
40 | { | |
41 | .name = SNAPSHOT_OPT_ID, | |
42 | .type = QEMU_OPT_STRING, | |
43 | .help = "snapshot id" | |
44 | },{ | |
45 | .name = SNAPSHOT_OPT_NAME, | |
46 | .type = QEMU_OPT_STRING, | |
47 | .help = "snapshot name" | |
48 | },{ | |
49 | /* end of list */ | |
50 | } | |
51 | }, | |
52 | }; | |
53 | ||
de08c606 WX |
54 | int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info, |
55 | const char *name) | |
56 | { | |
57 | QEMUSnapshotInfo *sn_tab, *sn; | |
58 | int nb_sns, i, ret; | |
59 | ||
60 | ret = -ENOENT; | |
61 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); | |
62 | if (nb_sns < 0) { | |
63 | return ret; | |
64 | } | |
65 | for (i = 0; i < nb_sns; i++) { | |
66 | sn = &sn_tab[i]; | |
6ca08045 | 67 | if (!strcmp(sn->name, name)) { |
de08c606 WX |
68 | *sn_info = *sn; |
69 | ret = 0; | |
70 | break; | |
71 | } | |
72 | } | |
73 | g_free(sn_tab); | |
74 | return ret; | |
75 | } | |
76 | ||
2ea1dd75 WX |
77 | /** |
78 | * Look up an internal snapshot by @id and @name. | |
79 | * @bs: block device to search | |
80 | * @id: unique snapshot ID, or NULL | |
81 | * @name: snapshot name, or NULL | |
82 | * @sn_info: location to store information on the snapshot found | |
83 | * @errp: location to store error, will be set only for exception | |
84 | * | |
85 | * This function will traverse snapshot list in @bs to search the matching | |
86 | * one, @id and @name are the matching condition: | |
87 | * If both @id and @name are specified, find the first one with id @id and | |
88 | * name @name. | |
89 | * If only @id is specified, find the first one with id @id. | |
90 | * If only @name is specified, find the first one with name @name. | |
91 | * if none is specified, abort(). | |
92 | * | |
93 | * Returns: true when a snapshot is found and @sn_info will be filled, false | |
94 | * when error or not found. If all operation succeed but no matching one is | |
95 | * found, @errp will NOT be set. | |
96 | */ | |
97 | bool bdrv_snapshot_find_by_id_and_name(BlockDriverState *bs, | |
98 | const char *id, | |
99 | const char *name, | |
100 | QEMUSnapshotInfo *sn_info, | |
101 | Error **errp) | |
102 | { | |
103 | QEMUSnapshotInfo *sn_tab, *sn; | |
104 | int nb_sns, i; | |
105 | bool ret = false; | |
106 | ||
107 | assert(id || name); | |
108 | ||
109 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); | |
110 | if (nb_sns < 0) { | |
111 | error_setg_errno(errp, -nb_sns, "Failed to get a snapshot list"); | |
112 | return false; | |
113 | } else if (nb_sns == 0) { | |
114 | return false; | |
115 | } | |
116 | ||
117 | if (id && name) { | |
118 | for (i = 0; i < nb_sns; i++) { | |
119 | sn = &sn_tab[i]; | |
120 | if (!strcmp(sn->id_str, id) && !strcmp(sn->name, name)) { | |
121 | *sn_info = *sn; | |
122 | ret = true; | |
123 | break; | |
124 | } | |
125 | } | |
126 | } else if (id) { | |
127 | for (i = 0; i < nb_sns; i++) { | |
128 | sn = &sn_tab[i]; | |
129 | if (!strcmp(sn->id_str, id)) { | |
130 | *sn_info = *sn; | |
131 | ret = true; | |
132 | break; | |
133 | } | |
134 | } | |
135 | } else if (name) { | |
136 | for (i = 0; i < nb_sns; i++) { | |
137 | sn = &sn_tab[i]; | |
138 | if (!strcmp(sn->name, name)) { | |
139 | *sn_info = *sn; | |
140 | ret = true; | |
141 | break; | |
142 | } | |
143 | } | |
144 | } | |
145 | ||
146 | g_free(sn_tab); | |
147 | return ret; | |
148 | } | |
149 | ||
c8af8757 HR |
150 | /** |
151 | * Return a pointer to the child BDS pointer to which we can fall | |
152 | * back if the given BDS does not support snapshots. | |
153 | * Return NULL if there is no BDS to (safely) fall back to. | |
154 | * | |
155 | * We need to return an indirect pointer because bdrv_snapshot_goto() | |
156 | * has to modify the BdrvChild pointer. | |
157 | */ | |
158 | static BdrvChild **bdrv_snapshot_fallback_ptr(BlockDriverState *bs) | |
159 | { | |
160 | BdrvChild **fallback; | |
161 | BdrvChild *child; | |
162 | ||
163 | /* | |
164 | * The only BdrvChild pointers that are safe to modify (and which | |
165 | * we can thus return a reference to) are bs->file and | |
166 | * bs->backing. | |
167 | */ | |
168 | fallback = &bs->file; | |
169 | if (!*fallback && bs->drv && bs->drv->is_filter) { | |
170 | fallback = &bs->backing; | |
171 | } | |
172 | ||
173 | if (!*fallback) { | |
174 | return NULL; | |
175 | } | |
176 | ||
177 | /* | |
178 | * Check that there are no other children that would need to be | |
179 | * snapshotted. If there are, it is not safe to fall back to | |
180 | * *fallback. | |
181 | */ | |
182 | QLIST_FOREACH(child, &bs->children, next) { | |
183 | if (child->role & (BDRV_CHILD_DATA | BDRV_CHILD_METADATA | | |
184 | BDRV_CHILD_FILTERED) && | |
185 | child != *fallback) | |
186 | { | |
187 | return NULL; | |
188 | } | |
189 | } | |
190 | ||
191 | return fallback; | |
192 | } | |
193 | ||
194 | static BlockDriverState *bdrv_snapshot_fallback(BlockDriverState *bs) | |
195 | { | |
196 | BdrvChild **child_ptr = bdrv_snapshot_fallback_ptr(bs); | |
197 | return child_ptr ? (*child_ptr)->bs : NULL; | |
198 | } | |
199 | ||
de08c606 WX |
200 | int bdrv_can_snapshot(BlockDriverState *bs) |
201 | { | |
202 | BlockDriver *drv = bs->drv; | |
203 | if (!drv || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) { | |
204 | return 0; | |
205 | } | |
206 | ||
207 | if (!drv->bdrv_snapshot_create) { | |
c8af8757 HR |
208 | BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs); |
209 | if (fallback_bs) { | |
210 | return bdrv_can_snapshot(fallback_bs); | |
de08c606 WX |
211 | } |
212 | return 0; | |
213 | } | |
214 | ||
215 | return 1; | |
216 | } | |
217 | ||
218 | int bdrv_snapshot_create(BlockDriverState *bs, | |
219 | QEMUSnapshotInfo *sn_info) | |
220 | { | |
221 | BlockDriver *drv = bs->drv; | |
c8af8757 | 222 | BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs); |
de08c606 WX |
223 | if (!drv) { |
224 | return -ENOMEDIUM; | |
225 | } | |
226 | if (drv->bdrv_snapshot_create) { | |
227 | return drv->bdrv_snapshot_create(bs, sn_info); | |
228 | } | |
c8af8757 HR |
229 | if (fallback_bs) { |
230 | return bdrv_snapshot_create(fallback_bs, sn_info); | |
de08c606 WX |
231 | } |
232 | return -ENOTSUP; | |
233 | } | |
234 | ||
235 | int bdrv_snapshot_goto(BlockDriverState *bs, | |
0b62bcbc KW |
236 | const char *snapshot_id, |
237 | Error **errp) | |
de08c606 WX |
238 | { |
239 | BlockDriver *drv = bs->drv; | |
c8af8757 | 240 | BdrvChild **fallback_ptr; |
de08c606 WX |
241 | int ret, open_ret; |
242 | ||
243 | if (!drv) { | |
0b62bcbc | 244 | error_setg(errp, "Block driver is closed"); |
de08c606 WX |
245 | return -ENOMEDIUM; |
246 | } | |
04dec3c3 | 247 | |
70a5afed KW |
248 | if (!QLIST_EMPTY(&bs->dirty_bitmaps)) { |
249 | error_setg(errp, "Device has active dirty bitmaps"); | |
250 | return -EBUSY; | |
04dec3c3 | 251 | } |
04dec3c3 | 252 | |
de08c606 | 253 | if (drv->bdrv_snapshot_goto) { |
0b62bcbc KW |
254 | ret = drv->bdrv_snapshot_goto(bs, snapshot_id); |
255 | if (ret < 0) { | |
256 | error_setg_errno(errp, -ret, "Failed to load snapshot"); | |
257 | } | |
258 | return ret; | |
de08c606 WX |
259 | } |
260 | ||
c8af8757 HR |
261 | fallback_ptr = bdrv_snapshot_fallback_ptr(bs); |
262 | if (fallback_ptr) { | |
263 | QDict *options; | |
7a9e5119 | 264 | QDict *file_options; |
0b62bcbc | 265 | Error *local_err = NULL; |
c8af8757 HR |
266 | BlockDriverState *fallback_bs = (*fallback_ptr)->bs; |
267 | char *subqdict_prefix = g_strdup_printf("%s.", (*fallback_ptr)->name); | |
268 | ||
269 | options = qdict_clone_shallow(bs->options); | |
7a9e5119 | 270 | |
7a9e5119 | 271 | /* Prevent it from getting deleted when detached from bs */ |
c8af8757 | 272 | bdrv_ref(fallback_bs); |
7a9e5119 | 273 | |
c8af8757 | 274 | qdict_extract_subqdict(options, &file_options, subqdict_prefix); |
cb3e7f08 | 275 | qobject_unref(file_options); |
c8af8757 HR |
276 | g_free(subqdict_prefix); |
277 | ||
278 | qdict_put_str(options, (*fallback_ptr)->name, | |
279 | bdrv_get_node_name(fallback_bs)); | |
7a9e5119 | 280 | |
3c005293 VSO |
281 | if (drv->bdrv_close) { |
282 | drv->bdrv_close(bs); | |
283 | } | |
7a9e5119 | 284 | |
c8af8757 HR |
285 | bdrv_unref_child(bs, *fallback_ptr); |
286 | *fallback_ptr = NULL; | |
287 | ||
288 | ret = bdrv_snapshot_goto(fallback_bs, snapshot_id, errp); | |
0b62bcbc | 289 | open_ret = drv->bdrv_open(bs, options, bs->open_flags, &local_err); |
cb3e7f08 | 290 | qobject_unref(options); |
de08c606 | 291 | if (open_ret < 0) { |
c8af8757 | 292 | bdrv_unref(fallback_bs); |
de08c606 | 293 | bs->drv = NULL; |
0b62bcbc KW |
294 | /* A bdrv_snapshot_goto() error takes precedence */ |
295 | error_propagate(errp, local_err); | |
296 | return ret < 0 ? ret : open_ret; | |
de08c606 | 297 | } |
7a9e5119 | 298 | |
c8af8757 HR |
299 | assert(fallback_bs == (*fallback_ptr)->bs); |
300 | bdrv_unref(fallback_bs); | |
de08c606 WX |
301 | return ret; |
302 | } | |
303 | ||
0b62bcbc | 304 | error_setg(errp, "Block driver does not support snapshots"); |
de08c606 WX |
305 | return -ENOTSUP; |
306 | } | |
307 | ||
a89d89d3 WX |
308 | /** |
309 | * Delete an internal snapshot by @snapshot_id and @name. | |
310 | * @bs: block device used in the operation | |
311 | * @snapshot_id: unique snapshot ID, or NULL | |
312 | * @name: snapshot name, or NULL | |
313 | * @errp: location to store error | |
314 | * | |
315 | * If both @snapshot_id and @name are specified, delete the first one with | |
316 | * id @snapshot_id and name @name. | |
317 | * If only @snapshot_id is specified, delete the first one with id | |
318 | * @snapshot_id. | |
319 | * If only @name is specified, delete the first one with name @name. | |
7b4c4781 | 320 | * if none is specified, return -EINVAL. |
a89d89d3 WX |
321 | * |
322 | * Returns: 0 on success, -errno on failure. If @bs is not inserted, return | |
323 | * -ENOMEDIUM. If @snapshot_id and @name are both NULL, return -EINVAL. If @bs | |
324 | * does not support internal snapshot deletion, return -ENOTSUP. If @bs does | |
325 | * not support parameter @snapshot_id or @name, or one of them is not correctly | |
326 | * specified, return -EINVAL. If @bs can't find one matching @id and @name, | |
327 | * return -ENOENT. If @errp != NULL, it will always be filled with error | |
328 | * message on failure. | |
329 | */ | |
330 | int bdrv_snapshot_delete(BlockDriverState *bs, | |
331 | const char *snapshot_id, | |
332 | const char *name, | |
333 | Error **errp) | |
de08c606 WX |
334 | { |
335 | BlockDriver *drv = bs->drv; | |
c8af8757 | 336 | BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs); |
27a7649a PB |
337 | int ret; |
338 | ||
de08c606 | 339 | if (!drv) { |
c6bd8c70 | 340 | error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, bdrv_get_device_name(bs)); |
de08c606 WX |
341 | return -ENOMEDIUM; |
342 | } | |
a89d89d3 WX |
343 | if (!snapshot_id && !name) { |
344 | error_setg(errp, "snapshot_id and name are both NULL"); | |
345 | return -EINVAL; | |
346 | } | |
3432a192 ZH |
347 | |
348 | /* drain all pending i/o before deleting snapshot */ | |
27a7649a | 349 | bdrv_drained_begin(bs); |
3432a192 | 350 | |
de08c606 | 351 | if (drv->bdrv_snapshot_delete) { |
27a7649a | 352 | ret = drv->bdrv_snapshot_delete(bs, snapshot_id, name, errp); |
c8af8757 HR |
353 | } else if (fallback_bs) { |
354 | ret = bdrv_snapshot_delete(fallback_bs, snapshot_id, name, errp); | |
27a7649a PB |
355 | } else { |
356 | error_setg(errp, "Block format '%s' used by device '%s' " | |
357 | "does not support internal snapshot deletion", | |
358 | drv->format_name, bdrv_get_device_name(bs)); | |
359 | ret = -ENOTSUP; | |
de08c606 | 360 | } |
27a7649a PB |
361 | |
362 | bdrv_drained_end(bs); | |
363 | return ret; | |
de08c606 WX |
364 | } |
365 | ||
366 | int bdrv_snapshot_list(BlockDriverState *bs, | |
367 | QEMUSnapshotInfo **psn_info) | |
368 | { | |
369 | BlockDriver *drv = bs->drv; | |
c8af8757 | 370 | BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs); |
de08c606 WX |
371 | if (!drv) { |
372 | return -ENOMEDIUM; | |
373 | } | |
374 | if (drv->bdrv_snapshot_list) { | |
375 | return drv->bdrv_snapshot_list(bs, psn_info); | |
376 | } | |
c8af8757 HR |
377 | if (fallback_bs) { |
378 | return bdrv_snapshot_list(fallback_bs, psn_info); | |
de08c606 WX |
379 | } |
380 | return -ENOTSUP; | |
381 | } | |
382 | ||
7b4c4781 WX |
383 | /** |
384 | * Temporarily load an internal snapshot by @snapshot_id and @name. | |
385 | * @bs: block device used in the operation | |
386 | * @snapshot_id: unique snapshot ID, or NULL | |
387 | * @name: snapshot name, or NULL | |
388 | * @errp: location to store error | |
389 | * | |
390 | * If both @snapshot_id and @name are specified, load the first one with | |
391 | * id @snapshot_id and name @name. | |
392 | * If only @snapshot_id is specified, load the first one with id | |
393 | * @snapshot_id. | |
394 | * If only @name is specified, load the first one with name @name. | |
395 | * if none is specified, return -EINVAL. | |
396 | * | |
397 | * Returns: 0 on success, -errno on fail. If @bs is not inserted, return | |
398 | * -ENOMEDIUM. If @bs is not readonly, return -EINVAL. If @bs did not support | |
399 | * internal snapshot, return -ENOTSUP. If qemu can't find a matching @id and | |
400 | * @name, return -ENOENT. If @errp != NULL, it will always be filled on | |
401 | * failure. | |
402 | */ | |
de08c606 | 403 | int bdrv_snapshot_load_tmp(BlockDriverState *bs, |
7b4c4781 WX |
404 | const char *snapshot_id, |
405 | const char *name, | |
406 | Error **errp) | |
de08c606 WX |
407 | { |
408 | BlockDriver *drv = bs->drv; | |
7b4c4781 | 409 | |
de08c606 | 410 | if (!drv) { |
c6bd8c70 | 411 | error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, bdrv_get_device_name(bs)); |
de08c606 WX |
412 | return -ENOMEDIUM; |
413 | } | |
7b4c4781 WX |
414 | if (!snapshot_id && !name) { |
415 | error_setg(errp, "snapshot_id and name are both NULL"); | |
416 | return -EINVAL; | |
417 | } | |
de08c606 | 418 | if (!bs->read_only) { |
7b4c4781 | 419 | error_setg(errp, "Device is not readonly"); |
de08c606 WX |
420 | return -EINVAL; |
421 | } | |
422 | if (drv->bdrv_snapshot_load_tmp) { | |
7b4c4781 | 423 | return drv->bdrv_snapshot_load_tmp(bs, snapshot_id, name, errp); |
de08c606 | 424 | } |
81e5f78a AG |
425 | error_setg(errp, "Block format '%s' used by device '%s' " |
426 | "does not support temporarily loading internal snapshots", | |
427 | drv->format_name, bdrv_get_device_name(bs)); | |
de08c606 WX |
428 | return -ENOTSUP; |
429 | } | |
7b4c4781 WX |
430 | |
431 | int bdrv_snapshot_load_tmp_by_id_or_name(BlockDriverState *bs, | |
432 | const char *id_or_name, | |
433 | Error **errp) | |
434 | { | |
435 | int ret; | |
436 | Error *local_err = NULL; | |
437 | ||
438 | ret = bdrv_snapshot_load_tmp(bs, id_or_name, NULL, &local_err); | |
439 | if (ret == -ENOENT || ret == -EINVAL) { | |
440 | error_free(local_err); | |
441 | local_err = NULL; | |
442 | ret = bdrv_snapshot_load_tmp(bs, NULL, id_or_name, &local_err); | |
443 | } | |
444 | ||
621ff94d | 445 | error_propagate(errp, local_err); |
7b4c4781 WX |
446 | |
447 | return ret; | |
448 | } | |
e9ff957a | 449 | |
cf3a74c9 DB |
450 | |
451 | static int bdrv_all_get_snapshot_devices(bool has_devices, strList *devices, | |
452 | GList **all_bdrvs, | |
453 | Error **errp) | |
454 | { | |
455 | g_autoptr(GList) bdrvs = NULL; | |
456 | ||
457 | if (has_devices) { | |
458 | if (!devices) { | |
459 | error_setg(errp, "At least one device is required for snapshot"); | |
460 | return -1; | |
461 | } | |
462 | ||
463 | while (devices) { | |
464 | BlockDriverState *bs = bdrv_find_node(devices->value); | |
465 | if (!bs) { | |
466 | error_setg(errp, "No block device node '%s'", devices->value); | |
467 | return -1; | |
468 | } | |
469 | bdrvs = g_list_append(bdrvs, bs); | |
470 | devices = devices->next; | |
471 | } | |
472 | } else { | |
473 | BlockDriverState *bs; | |
474 | BdrvNextIterator it; | |
475 | for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) { | |
476 | bdrvs = g_list_append(bdrvs, bs); | |
477 | } | |
478 | } | |
479 | ||
480 | *all_bdrvs = g_steal_pointer(&bdrvs); | |
481 | return 0; | |
482 | } | |
483 | ||
484 | ||
05f4aced KW |
485 | static bool bdrv_all_snapshots_includes_bs(BlockDriverState *bs) |
486 | { | |
487 | if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) { | |
488 | return false; | |
489 | } | |
490 | ||
491 | /* Include all nodes that are either in use by a BlockBackend, or that | |
492 | * aren't attached to any node, but owned by the monitor. */ | |
493 | return bdrv_has_blk(bs) || QLIST_EMPTY(&bs->parents); | |
494 | } | |
e9ff957a DL |
495 | |
496 | /* Group operations. All block drivers are involved. | |
497 | * These functions will properly handle dataplane (take aio_context_acquire | |
498 | * when appropriate for appropriate block drivers) */ | |
499 | ||
cf3a74c9 DB |
500 | bool bdrv_all_can_snapshot(bool has_devices, strList *devices, |
501 | Error **errp) | |
e9ff957a | 502 | { |
cf3a74c9 DB |
503 | g_autoptr(GList) bdrvs = NULL; |
504 | GList *iterbdrvs; | |
e9ff957a | 505 | |
cf3a74c9 DB |
506 | if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) { |
507 | return false; | |
508 | } | |
509 | ||
510 | iterbdrvs = bdrvs; | |
511 | while (iterbdrvs) { | |
512 | BlockDriverState *bs = iterbdrvs->data; | |
e9ff957a | 513 | AioContext *ctx = bdrv_get_aio_context(bs); |
e26f98e2 | 514 | bool ok = true; |
e9ff957a DL |
515 | |
516 | aio_context_acquire(ctx); | |
cf3a74c9 | 517 | if (devices || bdrv_all_snapshots_includes_bs(bs)) { |
e9ff957a DL |
518 | ok = bdrv_can_snapshot(bs); |
519 | } | |
520 | aio_context_release(ctx); | |
88be7b4b | 521 | if (!ok) { |
e26f98e2 DB |
522 | error_setg(errp, "Device '%s' is writable but does not support " |
523 | "snapshots", bdrv_get_device_or_node_name(bs)); | |
e26f98e2 | 524 | return false; |
88be7b4b | 525 | } |
cf3a74c9 DB |
526 | |
527 | iterbdrvs = iterbdrvs->next; | |
e9ff957a DL |
528 | } |
529 | ||
e26f98e2 | 530 | return true; |
e9ff957a | 531 | } |
9b00ea37 | 532 | |
cf3a74c9 DB |
533 | int bdrv_all_delete_snapshot(const char *name, |
534 | bool has_devices, strList *devices, | |
535 | Error **errp) | |
9b00ea37 | 536 | { |
cf3a74c9 DB |
537 | g_autoptr(GList) bdrvs = NULL; |
538 | GList *iterbdrvs; | |
539 | ||
540 | if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) { | |
541 | return -1; | |
542 | } | |
9b00ea37 | 543 | |
cf3a74c9 DB |
544 | iterbdrvs = bdrvs; |
545 | while (iterbdrvs) { | |
546 | BlockDriverState *bs = iterbdrvs->data; | |
9b00ea37 | 547 | AioContext *ctx = bdrv_get_aio_context(bs); |
cf3a74c9 | 548 | QEMUSnapshotInfo sn1, *snapshot = &sn1; |
e26f98e2 | 549 | int ret = 0; |
9b00ea37 DL |
550 | |
551 | aio_context_acquire(ctx); | |
cf3a74c9 | 552 | if ((devices || bdrv_all_snapshots_includes_bs(bs)) && |
05f4aced KW |
553 | bdrv_snapshot_find(bs, snapshot, name) >= 0) |
554 | { | |
6ca08045 | 555 | ret = bdrv_snapshot_delete(bs, snapshot->id_str, |
e53a578a | 556 | snapshot->name, errp); |
9b00ea37 DL |
557 | } |
558 | aio_context_release(ctx); | |
88be7b4b | 559 | if (ret < 0) { |
e26f98e2 DB |
560 | error_prepend(errp, "Could not delete snapshot '%s' on '%s': ", |
561 | name, bdrv_get_device_or_node_name(bs)); | |
e26f98e2 | 562 | return -1; |
88be7b4b | 563 | } |
cf3a74c9 DB |
564 | |
565 | iterbdrvs = iterbdrvs->next; | |
9b00ea37 DL |
566 | } |
567 | ||
e26f98e2 | 568 | return 0; |
9b00ea37 | 569 | } |
4c1cdbaa DL |
570 | |
571 | ||
cf3a74c9 DB |
572 | int bdrv_all_goto_snapshot(const char *name, |
573 | bool has_devices, strList *devices, | |
574 | Error **errp) | |
4c1cdbaa | 575 | { |
cf3a74c9 DB |
576 | g_autoptr(GList) bdrvs = NULL; |
577 | GList *iterbdrvs; | |
4c1cdbaa | 578 | |
cf3a74c9 DB |
579 | if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) { |
580 | return -1; | |
581 | } | |
582 | ||
583 | iterbdrvs = bdrvs; | |
584 | while (iterbdrvs) { | |
585 | BlockDriverState *bs = iterbdrvs->data; | |
4c1cdbaa | 586 | AioContext *ctx = bdrv_get_aio_context(bs); |
e26f98e2 | 587 | int ret = 0; |
4c1cdbaa DL |
588 | |
589 | aio_context_acquire(ctx); | |
cf3a74c9 | 590 | if (devices || bdrv_all_snapshots_includes_bs(bs)) { |
2b624fe0 | 591 | ret = bdrv_snapshot_goto(bs, name, errp); |
4c1cdbaa DL |
592 | } |
593 | aio_context_release(ctx); | |
2b624fe0 | 594 | if (ret < 0) { |
e26f98e2 DB |
595 | error_prepend(errp, "Could not load snapshot '%s' on '%s': ", |
596 | name, bdrv_get_device_or_node_name(bs)); | |
e26f98e2 | 597 | return -1; |
88be7b4b | 598 | } |
cf3a74c9 DB |
599 | |
600 | iterbdrvs = iterbdrvs->next; | |
4c1cdbaa DL |
601 | } |
602 | ||
e26f98e2 | 603 | return 0; |
4c1cdbaa | 604 | } |
723ccda1 | 605 | |
3d3e9b1f DB |
606 | int bdrv_all_has_snapshot(const char *name, |
607 | bool has_devices, strList *devices, | |
608 | Error **errp) | |
723ccda1 | 609 | { |
cf3a74c9 DB |
610 | g_autoptr(GList) bdrvs = NULL; |
611 | GList *iterbdrvs; | |
612 | ||
613 | if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) { | |
614 | return -1; | |
615 | } | |
723ccda1 | 616 | |
cf3a74c9 DB |
617 | iterbdrvs = bdrvs; |
618 | while (iterbdrvs) { | |
619 | BlockDriverState *bs = iterbdrvs->data; | |
723ccda1 | 620 | AioContext *ctx = bdrv_get_aio_context(bs); |
cf3a74c9 | 621 | QEMUSnapshotInfo sn; |
e26f98e2 | 622 | int ret = 0; |
723ccda1 DL |
623 | |
624 | aio_context_acquire(ctx); | |
cf3a74c9 | 625 | if (devices || bdrv_all_snapshots_includes_bs(bs)) { |
e26f98e2 | 626 | ret = bdrv_snapshot_find(bs, &sn, name); |
723ccda1 DL |
627 | } |
628 | aio_context_release(ctx); | |
e26f98e2 | 629 | if (ret < 0) { |
3d3e9b1f DB |
630 | if (ret == -ENOENT) { |
631 | return 0; | |
632 | } else { | |
633 | error_setg_errno(errp, errno, | |
634 | "Could not check snapshot '%s' on '%s'", | |
635 | name, bdrv_get_device_or_node_name(bs)); | |
636 | return -1; | |
637 | } | |
88be7b4b | 638 | } |
cf3a74c9 DB |
639 | |
640 | iterbdrvs = iterbdrvs->next; | |
723ccda1 DL |
641 | } |
642 | ||
3d3e9b1f | 643 | return 1; |
723ccda1 | 644 | } |
a9085f9b DL |
645 | |
646 | int bdrv_all_create_snapshot(QEMUSnapshotInfo *sn, | |
647 | BlockDriverState *vm_state_bs, | |
648 | uint64_t vm_state_size, | |
cf3a74c9 | 649 | bool has_devices, strList *devices, |
e26f98e2 | 650 | Error **errp) |
a9085f9b | 651 | { |
cf3a74c9 DB |
652 | g_autoptr(GList) bdrvs = NULL; |
653 | GList *iterbdrvs; | |
654 | ||
655 | if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) { | |
656 | return -1; | |
657 | } | |
a9085f9b | 658 | |
cf3a74c9 DB |
659 | iterbdrvs = bdrvs; |
660 | while (iterbdrvs) { | |
661 | BlockDriverState *bs = iterbdrvs->data; | |
a9085f9b | 662 | AioContext *ctx = bdrv_get_aio_context(bs); |
e26f98e2 | 663 | int ret = 0; |
a9085f9b DL |
664 | |
665 | aio_context_acquire(ctx); | |
666 | if (bs == vm_state_bs) { | |
667 | sn->vm_state_size = vm_state_size; | |
e26f98e2 | 668 | ret = bdrv_snapshot_create(bs, sn); |
cf3a74c9 | 669 | } else if (devices || bdrv_all_snapshots_includes_bs(bs)) { |
a9085f9b | 670 | sn->vm_state_size = 0; |
e26f98e2 | 671 | ret = bdrv_snapshot_create(bs, sn); |
a9085f9b DL |
672 | } |
673 | aio_context_release(ctx); | |
e26f98e2 DB |
674 | if (ret < 0) { |
675 | error_setg(errp, "Could not create snapshot '%s' on '%s'", | |
676 | sn->name, bdrv_get_device_or_node_name(bs)); | |
e26f98e2 | 677 | return -1; |
88be7b4b | 678 | } |
cf3a74c9 DB |
679 | |
680 | iterbdrvs = iterbdrvs->next; | |
a9085f9b DL |
681 | } |
682 | ||
e26f98e2 | 683 | return 0; |
a9085f9b | 684 | } |
7cb14481 | 685 | |
c22d644c DB |
686 | |
687 | BlockDriverState *bdrv_all_find_vmstate_bs(const char *vmstate_bs, | |
688 | bool has_devices, strList *devices, | |
cf3a74c9 | 689 | Error **errp) |
7cb14481 | 690 | { |
cf3a74c9 DB |
691 | g_autoptr(GList) bdrvs = NULL; |
692 | GList *iterbdrvs; | |
7cb14481 | 693 | |
cf3a74c9 DB |
694 | if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) { |
695 | return NULL; | |
696 | } | |
697 | ||
698 | iterbdrvs = bdrvs; | |
699 | while (iterbdrvs) { | |
700 | BlockDriverState *bs = iterbdrvs->data; | |
7cb14481 | 701 | AioContext *ctx = bdrv_get_aio_context(bs); |
cf3a74c9 | 702 | bool found = false; |
7cb14481 DL |
703 | |
704 | aio_context_acquire(ctx); | |
cf3a74c9 DB |
705 | found = (devices || bdrv_all_snapshots_includes_bs(bs)) && |
706 | bdrv_can_snapshot(bs); | |
7cb14481 | 707 | aio_context_release(ctx); |
88be7b4b | 708 | |
c22d644c DB |
709 | if (vmstate_bs) { |
710 | if (g_str_equal(vmstate_bs, | |
711 | bdrv_get_node_name(bs))) { | |
712 | if (found) { | |
713 | return bs; | |
714 | } else { | |
715 | error_setg(errp, | |
716 | "vmstate block device '%s' does not support snapshots", | |
717 | vmstate_bs); | |
718 | return NULL; | |
719 | } | |
720 | } | |
721 | } else if (found) { | |
cf3a74c9 | 722 | return bs; |
88be7b4b | 723 | } |
cf3a74c9 DB |
724 | |
725 | iterbdrvs = iterbdrvs->next; | |
7cb14481 | 726 | } |
cf3a74c9 | 727 | |
c22d644c DB |
728 | if (vmstate_bs) { |
729 | error_setg(errp, | |
730 | "vmstate block device '%s' does not exist", vmstate_bs); | |
731 | } else { | |
732 | error_setg(errp, | |
733 | "no block device can store vmstate for snapshot"); | |
734 | } | |
cf3a74c9 | 735 | return NULL; |
7cb14481 | 736 | } |