]>
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 | 153 | /** |
c5c21741 | 154 | * Return a pointer to child of given BDS to which we can fall |
c8af8757 HR |
155 | * back if the given BDS does not support snapshots. |
156 | * Return NULL if there is no BDS to (safely) fall back to. | |
c8af8757 | 157 | */ |
a32e7818 KW |
158 | static BdrvChild * GRAPH_RDLOCK |
159 | bdrv_snapshot_fallback_child(BlockDriverState *bs) | |
c8af8757 | 160 | { |
c5c21741 VSO |
161 | BdrvChild *fallback = bdrv_primary_child(bs); |
162 | BdrvChild *child; | |
c8af8757 | 163 | |
a32e7818 KW |
164 | GLOBAL_STATE_CODE(); |
165 | assert_bdrv_graph_readable(); | |
166 | ||
0c6100a7 | 167 | /* We allow fallback only to primary child */ |
c5c21741 | 168 | if (!fallback) { |
c8af8757 HR |
169 | return NULL; |
170 | } | |
171 | ||
172 | /* | |
173 | * Check that there are no other children that would need to be | |
174 | * snapshotted. If there are, it is not safe to fall back to | |
c5c21741 | 175 | * fallback. |
c8af8757 HR |
176 | */ |
177 | QLIST_FOREACH(child, &bs->children, next) { | |
178 | if (child->role & (BDRV_CHILD_DATA | BDRV_CHILD_METADATA | | |
179 | BDRV_CHILD_FILTERED) && | |
c5c21741 | 180 | child != fallback) |
c8af8757 HR |
181 | { |
182 | return NULL; | |
183 | } | |
184 | } | |
185 | ||
186 | return fallback; | |
187 | } | |
188 | ||
a32e7818 KW |
189 | static BlockDriverState * GRAPH_RDLOCK |
190 | bdrv_snapshot_fallback(BlockDriverState *bs) | |
c8af8757 | 191 | { |
a32e7818 | 192 | GLOBAL_STATE_CODE(); |
c5c21741 | 193 | return child_bs(bdrv_snapshot_fallback_child(bs)); |
c8af8757 HR |
194 | } |
195 | ||
de08c606 WX |
196 | int bdrv_can_snapshot(BlockDriverState *bs) |
197 | { | |
198 | BlockDriver *drv = bs->drv; | |
6b573efe | 199 | GLOBAL_STATE_CODE(); |
de08c606 WX |
200 | if (!drv || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) { |
201 | return 0; | |
202 | } | |
203 | ||
204 | if (!drv->bdrv_snapshot_create) { | |
c8af8757 HR |
205 | BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs); |
206 | if (fallback_bs) { | |
207 | return bdrv_can_snapshot(fallback_bs); | |
de08c606 WX |
208 | } |
209 | return 0; | |
210 | } | |
211 | ||
212 | return 1; | |
213 | } | |
214 | ||
215 | int bdrv_snapshot_create(BlockDriverState *bs, | |
216 | QEMUSnapshotInfo *sn_info) | |
217 | { | |
218 | BlockDriver *drv = bs->drv; | |
c8af8757 | 219 | BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs); |
6b573efe EGE |
220 | |
221 | GLOBAL_STATE_CODE(); | |
222 | ||
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; | |
c5c21741 | 240 | BdrvChild *fallback; |
de08c606 WX |
241 | int ret, open_ret; |
242 | ||
6b573efe EGE |
243 | GLOBAL_STATE_CODE(); |
244 | ||
de08c606 | 245 | if (!drv) { |
0b62bcbc | 246 | error_setg(errp, "Block driver is closed"); |
de08c606 WX |
247 | return -ENOMEDIUM; |
248 | } | |
04dec3c3 | 249 | |
70a5afed KW |
250 | if (!QLIST_EMPTY(&bs->dirty_bitmaps)) { |
251 | error_setg(errp, "Device has active dirty bitmaps"); | |
252 | return -EBUSY; | |
04dec3c3 | 253 | } |
04dec3c3 | 254 | |
de08c606 | 255 | if (drv->bdrv_snapshot_goto) { |
0b62bcbc KW |
256 | ret = drv->bdrv_snapshot_goto(bs, snapshot_id); |
257 | if (ret < 0) { | |
258 | error_setg_errno(errp, -ret, "Failed to load snapshot"); | |
259 | } | |
260 | return ret; | |
de08c606 WX |
261 | } |
262 | ||
a32e7818 | 263 | bdrv_graph_rdlock_main_loop(); |
c5c21741 | 264 | fallback = bdrv_snapshot_fallback_child(bs); |
a32e7818 KW |
265 | bdrv_graph_rdunlock_main_loop(); |
266 | ||
c5c21741 | 267 | if (fallback) { |
c8af8757 | 268 | QDict *options; |
7a9e5119 | 269 | QDict *file_options; |
0b62bcbc | 270 | Error *local_err = NULL; |
c5c21741 VSO |
271 | BlockDriverState *fallback_bs = fallback->bs; |
272 | char *subqdict_prefix = g_strdup_printf("%s.", fallback->name); | |
c8af8757 HR |
273 | |
274 | options = qdict_clone_shallow(bs->options); | |
7a9e5119 | 275 | |
7a9e5119 | 276 | /* Prevent it from getting deleted when detached from bs */ |
c8af8757 | 277 | bdrv_ref(fallback_bs); |
7a9e5119 | 278 | |
c8af8757 | 279 | qdict_extract_subqdict(options, &file_options, subqdict_prefix); |
cb3e7f08 | 280 | qobject_unref(file_options); |
c8af8757 HR |
281 | g_free(subqdict_prefix); |
282 | ||
c5c21741 VSO |
283 | /* Force .bdrv_open() below to re-attach fallback_bs on fallback */ |
284 | qdict_put_str(options, fallback->name, | |
c8af8757 | 285 | bdrv_get_node_name(fallback_bs)); |
7a9e5119 | 286 | |
32a9a245 | 287 | /* Now close bs, apply the snapshot on fallback_bs, and re-open bs */ |
3c005293 VSO |
288 | if (drv->bdrv_close) { |
289 | drv->bdrv_close(bs); | |
290 | } | |
7a9e5119 | 291 | |
32a9a245 | 292 | /* .bdrv_open() will re-attach it */ |
32a8aba3 | 293 | bdrv_graph_wrlock(NULL); |
c5c21741 | 294 | bdrv_unref_child(bs, fallback); |
32a8aba3 | 295 | bdrv_graph_wrunlock(); |
c8af8757 HR |
296 | |
297 | ret = bdrv_snapshot_goto(fallback_bs, snapshot_id, errp); | |
0b62bcbc | 298 | open_ret = drv->bdrv_open(bs, options, bs->open_flags, &local_err); |
cb3e7f08 | 299 | qobject_unref(options); |
de08c606 | 300 | if (open_ret < 0) { |
c8af8757 | 301 | bdrv_unref(fallback_bs); |
de08c606 | 302 | bs->drv = NULL; |
0b62bcbc KW |
303 | /* A bdrv_snapshot_goto() error takes precedence */ |
304 | error_propagate(errp, local_err); | |
305 | return ret < 0 ? ret : open_ret; | |
de08c606 | 306 | } |
7a9e5119 | 307 | |
32a9a245 | 308 | /* |
0c6100a7 VSO |
309 | * fallback was a primary child. It was closed above and set to NULL, |
310 | * but the .bdrv_open() call has opened it again, because we set the | |
311 | * respective option (with the qdict_put_str() call above). | |
312 | * Assert that .bdrv_open() has attached the right BDS as primary child. | |
32a9a245 | 313 | */ |
c0fc5123 | 314 | bdrv_graph_rdlock_main_loop(); |
0c6100a7 | 315 | assert(bdrv_primary_bs(bs) == fallback_bs); |
c0fc5123 KW |
316 | bdrv_graph_rdunlock_main_loop(); |
317 | ||
c8af8757 | 318 | bdrv_unref(fallback_bs); |
de08c606 WX |
319 | return ret; |
320 | } | |
321 | ||
0b62bcbc | 322 | error_setg(errp, "Block driver does not support snapshots"); |
de08c606 WX |
323 | return -ENOTSUP; |
324 | } | |
325 | ||
a89d89d3 WX |
326 | /** |
327 | * Delete an internal snapshot by @snapshot_id and @name. | |
328 | * @bs: block device used in the operation | |
329 | * @snapshot_id: unique snapshot ID, or NULL | |
330 | * @name: snapshot name, or NULL | |
331 | * @errp: location to store error | |
332 | * | |
333 | * If both @snapshot_id and @name are specified, delete the first one with | |
334 | * id @snapshot_id and name @name. | |
335 | * If only @snapshot_id is specified, delete the first one with id | |
336 | * @snapshot_id. | |
337 | * If only @name is specified, delete the first one with name @name. | |
7b4c4781 | 338 | * if none is specified, return -EINVAL. |
a89d89d3 WX |
339 | * |
340 | * Returns: 0 on success, -errno on failure. If @bs is not inserted, return | |
341 | * -ENOMEDIUM. If @snapshot_id and @name are both NULL, return -EINVAL. If @bs | |
342 | * does not support internal snapshot deletion, return -ENOTSUP. If @bs does | |
343 | * not support parameter @snapshot_id or @name, or one of them is not correctly | |
344 | * specified, return -EINVAL. If @bs can't find one matching @id and @name, | |
345 | * return -ENOENT. If @errp != NULL, it will always be filled with error | |
346 | * message on failure. | |
347 | */ | |
348 | int bdrv_snapshot_delete(BlockDriverState *bs, | |
349 | const char *snapshot_id, | |
350 | const char *name, | |
351 | Error **errp) | |
de08c606 WX |
352 | { |
353 | BlockDriver *drv = bs->drv; | |
c8af8757 | 354 | BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs); |
27a7649a PB |
355 | int ret; |
356 | ||
6b573efe EGE |
357 | GLOBAL_STATE_CODE(); |
358 | ||
de08c606 | 359 | if (!drv) { |
c6bd8c70 | 360 | error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, bdrv_get_device_name(bs)); |
de08c606 WX |
361 | return -ENOMEDIUM; |
362 | } | |
a89d89d3 WX |
363 | if (!snapshot_id && !name) { |
364 | error_setg(errp, "snapshot_id and name are both NULL"); | |
365 | return -EINVAL; | |
366 | } | |
3432a192 ZH |
367 | |
368 | /* drain all pending i/o before deleting snapshot */ | |
27a7649a | 369 | bdrv_drained_begin(bs); |
3432a192 | 370 | |
de08c606 | 371 | if (drv->bdrv_snapshot_delete) { |
27a7649a | 372 | ret = drv->bdrv_snapshot_delete(bs, snapshot_id, name, errp); |
c8af8757 HR |
373 | } else if (fallback_bs) { |
374 | ret = bdrv_snapshot_delete(fallback_bs, snapshot_id, name, errp); | |
27a7649a PB |
375 | } else { |
376 | error_setg(errp, "Block format '%s' used by device '%s' " | |
377 | "does not support internal snapshot deletion", | |
378 | drv->format_name, bdrv_get_device_name(bs)); | |
379 | ret = -ENOTSUP; | |
de08c606 | 380 | } |
27a7649a PB |
381 | |
382 | bdrv_drained_end(bs); | |
383 | return ret; | |
de08c606 WX |
384 | } |
385 | ||
386 | int bdrv_snapshot_list(BlockDriverState *bs, | |
387 | QEMUSnapshotInfo **psn_info) | |
388 | { | |
a32e7818 KW |
389 | GLOBAL_STATE_CODE(); |
390 | GRAPH_RDLOCK_GUARD_MAINLOOP(); | |
391 | ||
de08c606 | 392 | BlockDriver *drv = bs->drv; |
c8af8757 | 393 | BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs); |
6b573efe | 394 | |
de08c606 WX |
395 | if (!drv) { |
396 | return -ENOMEDIUM; | |
397 | } | |
398 | if (drv->bdrv_snapshot_list) { | |
399 | return drv->bdrv_snapshot_list(bs, psn_info); | |
400 | } | |
c8af8757 HR |
401 | if (fallback_bs) { |
402 | return bdrv_snapshot_list(fallback_bs, psn_info); | |
de08c606 WX |
403 | } |
404 | return -ENOTSUP; | |
405 | } | |
406 | ||
7b4c4781 WX |
407 | /** |
408 | * Temporarily load an internal snapshot by @snapshot_id and @name. | |
409 | * @bs: block device used in the operation | |
410 | * @snapshot_id: unique snapshot ID, or NULL | |
411 | * @name: snapshot name, or NULL | |
412 | * @errp: location to store error | |
413 | * | |
414 | * If both @snapshot_id and @name are specified, load the first one with | |
415 | * id @snapshot_id and name @name. | |
416 | * If only @snapshot_id is specified, load the first one with id | |
417 | * @snapshot_id. | |
418 | * If only @name is specified, load the first one with name @name. | |
419 | * if none is specified, return -EINVAL. | |
420 | * | |
421 | * Returns: 0 on success, -errno on fail. If @bs is not inserted, return | |
422 | * -ENOMEDIUM. If @bs is not readonly, return -EINVAL. If @bs did not support | |
423 | * internal snapshot, return -ENOTSUP. If qemu can't find a matching @id and | |
424 | * @name, return -ENOENT. If @errp != NULL, it will always be filled on | |
425 | * failure. | |
426 | */ | |
de08c606 | 427 | int bdrv_snapshot_load_tmp(BlockDriverState *bs, |
7b4c4781 WX |
428 | const char *snapshot_id, |
429 | const char *name, | |
430 | Error **errp) | |
de08c606 WX |
431 | { |
432 | BlockDriver *drv = bs->drv; | |
7b4c4781 | 433 | |
6b573efe | 434 | GLOBAL_STATE_CODE(); |
4026f1c4 | 435 | GRAPH_RDLOCK_GUARD_MAINLOOP(); |
6b573efe | 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 | 479 | |
2b3912f1 KW |
480 | static int GRAPH_RDLOCK |
481 | bdrv_all_get_snapshot_devices(bool has_devices, strList *devices, | |
482 | GList **all_bdrvs, Error **errp) | |
cf3a74c9 DB |
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 | ||
2b3912f1 | 514 | static bool GRAPH_RDLOCK bdrv_all_snapshots_includes_bs(BlockDriverState *bs) |
05f4aced | 515 | { |
a32e7818 KW |
516 | GLOBAL_STATE_CODE(); |
517 | assert_bdrv_graph_readable(); | |
518 | ||
05f4aced KW |
519 | if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) { |
520 | return false; | |
521 | } | |
522 | ||
523 | /* Include all nodes that are either in use by a BlockBackend, or that | |
524 | * aren't attached to any node, but owned by the monitor. */ | |
525 | return bdrv_has_blk(bs) || QLIST_EMPTY(&bs->parents); | |
526 | } | |
e9ff957a DL |
527 | |
528 | /* Group operations. All block drivers are involved. | |
529 | * These functions will properly handle dataplane (take aio_context_acquire | |
530 | * when appropriate for appropriate block drivers) */ | |
531 | ||
cf3a74c9 DB |
532 | bool bdrv_all_can_snapshot(bool has_devices, strList *devices, |
533 | Error **errp) | |
e9ff957a | 534 | { |
cf3a74c9 DB |
535 | g_autoptr(GList) bdrvs = NULL; |
536 | GList *iterbdrvs; | |
e9ff957a | 537 | |
6b573efe | 538 | GLOBAL_STATE_CODE(); |
2b3912f1 | 539 | GRAPH_RDLOCK_GUARD_MAINLOOP(); |
6b573efe | 540 | |
cf3a74c9 DB |
541 | if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) { |
542 | return false; | |
543 | } | |
544 | ||
545 | iterbdrvs = bdrvs; | |
546 | while (iterbdrvs) { | |
547 | BlockDriverState *bs = iterbdrvs->data; | |
e9ff957a | 548 | AioContext *ctx = bdrv_get_aio_context(bs); |
e26f98e2 | 549 | bool ok = true; |
e9ff957a DL |
550 | |
551 | aio_context_acquire(ctx); | |
cf3a74c9 | 552 | if (devices || bdrv_all_snapshots_includes_bs(bs)) { |
e9ff957a DL |
553 | ok = bdrv_can_snapshot(bs); |
554 | } | |
555 | aio_context_release(ctx); | |
88be7b4b | 556 | if (!ok) { |
e26f98e2 DB |
557 | error_setg(errp, "Device '%s' is writable but does not support " |
558 | "snapshots", bdrv_get_device_or_node_name(bs)); | |
e26f98e2 | 559 | return false; |
88be7b4b | 560 | } |
cf3a74c9 DB |
561 | |
562 | iterbdrvs = iterbdrvs->next; | |
e9ff957a DL |
563 | } |
564 | ||
e26f98e2 | 565 | return true; |
e9ff957a | 566 | } |
9b00ea37 | 567 | |
cf3a74c9 DB |
568 | int bdrv_all_delete_snapshot(const char *name, |
569 | bool has_devices, strList *devices, | |
570 | Error **errp) | |
9b00ea37 | 571 | { |
cf3a74c9 DB |
572 | g_autoptr(GList) bdrvs = NULL; |
573 | GList *iterbdrvs; | |
574 | ||
6b573efe | 575 | GLOBAL_STATE_CODE(); |
2b3912f1 | 576 | GRAPH_RDLOCK_GUARD_MAINLOOP(); |
6b573efe | 577 | |
cf3a74c9 DB |
578 | if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) { |
579 | return -1; | |
580 | } | |
9b00ea37 | 581 | |
cf3a74c9 DB |
582 | iterbdrvs = bdrvs; |
583 | while (iterbdrvs) { | |
584 | BlockDriverState *bs = iterbdrvs->data; | |
9b00ea37 | 585 | AioContext *ctx = bdrv_get_aio_context(bs); |
cf3a74c9 | 586 | QEMUSnapshotInfo sn1, *snapshot = &sn1; |
e26f98e2 | 587 | int ret = 0; |
9b00ea37 DL |
588 | |
589 | aio_context_acquire(ctx); | |
cf3a74c9 | 590 | if ((devices || bdrv_all_snapshots_includes_bs(bs)) && |
05f4aced KW |
591 | bdrv_snapshot_find(bs, snapshot, name) >= 0) |
592 | { | |
6ca08045 | 593 | ret = bdrv_snapshot_delete(bs, snapshot->id_str, |
e53a578a | 594 | snapshot->name, errp); |
9b00ea37 DL |
595 | } |
596 | aio_context_release(ctx); | |
88be7b4b | 597 | if (ret < 0) { |
e26f98e2 DB |
598 | error_prepend(errp, "Could not delete snapshot '%s' on '%s': ", |
599 | name, bdrv_get_device_or_node_name(bs)); | |
e26f98e2 | 600 | return -1; |
88be7b4b | 601 | } |
cf3a74c9 DB |
602 | |
603 | iterbdrvs = iterbdrvs->next; | |
9b00ea37 DL |
604 | } |
605 | ||
e26f98e2 | 606 | return 0; |
9b00ea37 | 607 | } |
4c1cdbaa DL |
608 | |
609 | ||
cf3a74c9 DB |
610 | int bdrv_all_goto_snapshot(const char *name, |
611 | bool has_devices, strList *devices, | |
612 | Error **errp) | |
4c1cdbaa | 613 | { |
cf3a74c9 DB |
614 | g_autoptr(GList) bdrvs = NULL; |
615 | GList *iterbdrvs; | |
a32e7818 | 616 | int ret; |
4c1cdbaa | 617 | |
6b573efe EGE |
618 | GLOBAL_STATE_CODE(); |
619 | ||
a32e7818 KW |
620 | bdrv_graph_rdlock_main_loop(); |
621 | ret = bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp); | |
622 | bdrv_graph_rdunlock_main_loop(); | |
623 | ||
624 | if (ret < 0) { | |
cf3a74c9 DB |
625 | return -1; |
626 | } | |
627 | ||
628 | iterbdrvs = bdrvs; | |
629 | while (iterbdrvs) { | |
630 | BlockDriverState *bs = iterbdrvs->data; | |
4c1cdbaa | 631 | AioContext *ctx = bdrv_get_aio_context(bs); |
e26f98e2 | 632 | int ret = 0; |
a32e7818 | 633 | bool all_snapshots_includes_bs; |
4c1cdbaa DL |
634 | |
635 | aio_context_acquire(ctx); | |
a32e7818 KW |
636 | bdrv_graph_rdlock_main_loop(); |
637 | all_snapshots_includes_bs = bdrv_all_snapshots_includes_bs(bs); | |
638 | bdrv_graph_rdunlock_main_loop(); | |
639 | ||
640 | if (devices || all_snapshots_includes_bs) { | |
2b624fe0 | 641 | ret = bdrv_snapshot_goto(bs, name, errp); |
4c1cdbaa DL |
642 | } |
643 | aio_context_release(ctx); | |
2b624fe0 | 644 | if (ret < 0) { |
4026f1c4 | 645 | bdrv_graph_rdlock_main_loop(); |
e26f98e2 DB |
646 | error_prepend(errp, "Could not load snapshot '%s' on '%s': ", |
647 | name, bdrv_get_device_or_node_name(bs)); | |
4026f1c4 | 648 | bdrv_graph_rdunlock_main_loop(); |
e26f98e2 | 649 | return -1; |
88be7b4b | 650 | } |
cf3a74c9 DB |
651 | |
652 | iterbdrvs = iterbdrvs->next; | |
4c1cdbaa DL |
653 | } |
654 | ||
e26f98e2 | 655 | return 0; |
4c1cdbaa | 656 | } |
723ccda1 | 657 | |
3d3e9b1f DB |
658 | int bdrv_all_has_snapshot(const char *name, |
659 | bool has_devices, strList *devices, | |
660 | Error **errp) | |
723ccda1 | 661 | { |
cf3a74c9 DB |
662 | g_autoptr(GList) bdrvs = NULL; |
663 | GList *iterbdrvs; | |
664 | ||
6b573efe | 665 | GLOBAL_STATE_CODE(); |
2b3912f1 | 666 | GRAPH_RDLOCK_GUARD_MAINLOOP(); |
6b573efe | 667 | |
cf3a74c9 DB |
668 | if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) { |
669 | return -1; | |
670 | } | |
723ccda1 | 671 | |
cf3a74c9 DB |
672 | iterbdrvs = bdrvs; |
673 | while (iterbdrvs) { | |
674 | BlockDriverState *bs = iterbdrvs->data; | |
723ccda1 | 675 | AioContext *ctx = bdrv_get_aio_context(bs); |
cf3a74c9 | 676 | QEMUSnapshotInfo sn; |
e26f98e2 | 677 | int ret = 0; |
723ccda1 DL |
678 | |
679 | aio_context_acquire(ctx); | |
cf3a74c9 | 680 | if (devices || bdrv_all_snapshots_includes_bs(bs)) { |
e26f98e2 | 681 | ret = bdrv_snapshot_find(bs, &sn, name); |
723ccda1 DL |
682 | } |
683 | aio_context_release(ctx); | |
e26f98e2 | 684 | if (ret < 0) { |
3d3e9b1f DB |
685 | if (ret == -ENOENT) { |
686 | return 0; | |
687 | } else { | |
688 | error_setg_errno(errp, errno, | |
689 | "Could not check snapshot '%s' on '%s'", | |
690 | name, bdrv_get_device_or_node_name(bs)); | |
691 | return -1; | |
692 | } | |
88be7b4b | 693 | } |
cf3a74c9 DB |
694 | |
695 | iterbdrvs = iterbdrvs->next; | |
723ccda1 DL |
696 | } |
697 | ||
3d3e9b1f | 698 | return 1; |
723ccda1 | 699 | } |
a9085f9b DL |
700 | |
701 | int bdrv_all_create_snapshot(QEMUSnapshotInfo *sn, | |
702 | BlockDriverState *vm_state_bs, | |
703 | uint64_t vm_state_size, | |
cf3a74c9 | 704 | bool has_devices, strList *devices, |
e26f98e2 | 705 | Error **errp) |
a9085f9b | 706 | { |
cf3a74c9 DB |
707 | g_autoptr(GList) bdrvs = NULL; |
708 | GList *iterbdrvs; | |
2b3912f1 | 709 | |
6b573efe | 710 | GLOBAL_STATE_CODE(); |
2b3912f1 | 711 | GRAPH_RDLOCK_GUARD_MAINLOOP(); |
cf3a74c9 DB |
712 | |
713 | if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) { | |
714 | return -1; | |
715 | } | |
a9085f9b | 716 | |
cf3a74c9 DB |
717 | iterbdrvs = bdrvs; |
718 | while (iterbdrvs) { | |
719 | BlockDriverState *bs = iterbdrvs->data; | |
a9085f9b | 720 | AioContext *ctx = bdrv_get_aio_context(bs); |
e26f98e2 | 721 | int ret = 0; |
a9085f9b DL |
722 | |
723 | aio_context_acquire(ctx); | |
724 | if (bs == vm_state_bs) { | |
725 | sn->vm_state_size = vm_state_size; | |
e26f98e2 | 726 | ret = bdrv_snapshot_create(bs, sn); |
cf3a74c9 | 727 | } else if (devices || bdrv_all_snapshots_includes_bs(bs)) { |
a9085f9b | 728 | sn->vm_state_size = 0; |
e26f98e2 | 729 | ret = bdrv_snapshot_create(bs, sn); |
a9085f9b DL |
730 | } |
731 | aio_context_release(ctx); | |
e26f98e2 DB |
732 | if (ret < 0) { |
733 | error_setg(errp, "Could not create snapshot '%s' on '%s'", | |
734 | sn->name, bdrv_get_device_or_node_name(bs)); | |
e26f98e2 | 735 | return -1; |
88be7b4b | 736 | } |
cf3a74c9 DB |
737 | |
738 | iterbdrvs = iterbdrvs->next; | |
a9085f9b DL |
739 | } |
740 | ||
e26f98e2 | 741 | return 0; |
a9085f9b | 742 | } |
7cb14481 | 743 | |
c22d644c DB |
744 | |
745 | BlockDriverState *bdrv_all_find_vmstate_bs(const char *vmstate_bs, | |
746 | bool has_devices, strList *devices, | |
cf3a74c9 | 747 | Error **errp) |
7cb14481 | 748 | { |
cf3a74c9 DB |
749 | g_autoptr(GList) bdrvs = NULL; |
750 | GList *iterbdrvs; | |
7cb14481 | 751 | |
6b573efe | 752 | GLOBAL_STATE_CODE(); |
2b3912f1 | 753 | GRAPH_RDLOCK_GUARD_MAINLOOP(); |
6b573efe | 754 | |
cf3a74c9 DB |
755 | if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) { |
756 | return NULL; | |
757 | } | |
758 | ||
759 | iterbdrvs = bdrvs; | |
760 | while (iterbdrvs) { | |
761 | BlockDriverState *bs = iterbdrvs->data; | |
7cb14481 | 762 | AioContext *ctx = bdrv_get_aio_context(bs); |
cf3a74c9 | 763 | bool found = false; |
7cb14481 DL |
764 | |
765 | aio_context_acquire(ctx); | |
cf3a74c9 DB |
766 | found = (devices || bdrv_all_snapshots_includes_bs(bs)) && |
767 | bdrv_can_snapshot(bs); | |
7cb14481 | 768 | aio_context_release(ctx); |
88be7b4b | 769 | |
c22d644c DB |
770 | if (vmstate_bs) { |
771 | if (g_str_equal(vmstate_bs, | |
772 | bdrv_get_node_name(bs))) { | |
773 | if (found) { | |
774 | return bs; | |
775 | } else { | |
776 | error_setg(errp, | |
777 | "vmstate block device '%s' does not support snapshots", | |
778 | vmstate_bs); | |
779 | return NULL; | |
780 | } | |
781 | } | |
782 | } else if (found) { | |
cf3a74c9 | 783 | return bs; |
88be7b4b | 784 | } |
cf3a74c9 DB |
785 | |
786 | iterbdrvs = iterbdrvs->next; | |
7cb14481 | 787 | } |
cf3a74c9 | 788 | |
c22d644c DB |
789 | if (vmstate_bs) { |
790 | error_setg(errp, | |
791 | "vmstate block device '%s' does not exist", vmstate_bs); | |
792 | } else { | |
793 | error_setg(errp, | |
794 | "no block device can store vmstate for snapshot"); | |
795 | } | |
cf3a74c9 | 796 | return NULL; |
7cb14481 | 797 | } |