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