]>
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" | |
da34e65c | 28 | #include "qapi/error.h" |
cc7a8ea7 | 29 | #include "qapi/qmp/qerror.h" |
7a9e5119 | 30 | #include "qapi/qmp/qstring.h" |
de08c606 | 31 | |
8c116b0e WX |
32 | QemuOptsList internal_snapshot_opts = { |
33 | .name = "snapshot", | |
34 | .head = QTAILQ_HEAD_INITIALIZER(internal_snapshot_opts.head), | |
35 | .desc = { | |
36 | { | |
37 | .name = SNAPSHOT_OPT_ID, | |
38 | .type = QEMU_OPT_STRING, | |
39 | .help = "snapshot id" | |
40 | },{ | |
41 | .name = SNAPSHOT_OPT_NAME, | |
42 | .type = QEMU_OPT_STRING, | |
43 | .help = "snapshot name" | |
44 | },{ | |
45 | /* end of list */ | |
46 | } | |
47 | }, | |
48 | }; | |
49 | ||
de08c606 WX |
50 | int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info, |
51 | const char *name) | |
52 | { | |
53 | QEMUSnapshotInfo *sn_tab, *sn; | |
54 | int nb_sns, i, ret; | |
55 | ||
56 | ret = -ENOENT; | |
57 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); | |
58 | if (nb_sns < 0) { | |
59 | return ret; | |
60 | } | |
61 | for (i = 0; i < nb_sns; i++) { | |
62 | sn = &sn_tab[i]; | |
63 | if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) { | |
64 | *sn_info = *sn; | |
65 | ret = 0; | |
66 | break; | |
67 | } | |
68 | } | |
69 | g_free(sn_tab); | |
70 | return ret; | |
71 | } | |
72 | ||
2ea1dd75 WX |
73 | /** |
74 | * Look up an internal snapshot by @id and @name. | |
75 | * @bs: block device to search | |
76 | * @id: unique snapshot ID, or NULL | |
77 | * @name: snapshot name, or NULL | |
78 | * @sn_info: location to store information on the snapshot found | |
79 | * @errp: location to store error, will be set only for exception | |
80 | * | |
81 | * This function will traverse snapshot list in @bs to search the matching | |
82 | * one, @id and @name are the matching condition: | |
83 | * If both @id and @name are specified, find the first one with id @id and | |
84 | * name @name. | |
85 | * If only @id is specified, find the first one with id @id. | |
86 | * If only @name is specified, find the first one with name @name. | |
87 | * if none is specified, abort(). | |
88 | * | |
89 | * Returns: true when a snapshot is found and @sn_info will be filled, false | |
90 | * when error or not found. If all operation succeed but no matching one is | |
91 | * found, @errp will NOT be set. | |
92 | */ | |
93 | bool bdrv_snapshot_find_by_id_and_name(BlockDriverState *bs, | |
94 | const char *id, | |
95 | const char *name, | |
96 | QEMUSnapshotInfo *sn_info, | |
97 | Error **errp) | |
98 | { | |
99 | QEMUSnapshotInfo *sn_tab, *sn; | |
100 | int nb_sns, i; | |
101 | bool ret = false; | |
102 | ||
103 | assert(id || name); | |
104 | ||
105 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); | |
106 | if (nb_sns < 0) { | |
107 | error_setg_errno(errp, -nb_sns, "Failed to get a snapshot list"); | |
108 | return false; | |
109 | } else if (nb_sns == 0) { | |
110 | return false; | |
111 | } | |
112 | ||
113 | if (id && name) { | |
114 | for (i = 0; i < nb_sns; i++) { | |
115 | sn = &sn_tab[i]; | |
116 | if (!strcmp(sn->id_str, id) && !strcmp(sn->name, name)) { | |
117 | *sn_info = *sn; | |
118 | ret = true; | |
119 | break; | |
120 | } | |
121 | } | |
122 | } else if (id) { | |
123 | for (i = 0; i < nb_sns; i++) { | |
124 | sn = &sn_tab[i]; | |
125 | if (!strcmp(sn->id_str, id)) { | |
126 | *sn_info = *sn; | |
127 | ret = true; | |
128 | break; | |
129 | } | |
130 | } | |
131 | } else if (name) { | |
132 | for (i = 0; i < nb_sns; i++) { | |
133 | sn = &sn_tab[i]; | |
134 | if (!strcmp(sn->name, name)) { | |
135 | *sn_info = *sn; | |
136 | ret = true; | |
137 | break; | |
138 | } | |
139 | } | |
140 | } | |
141 | ||
142 | g_free(sn_tab); | |
143 | return ret; | |
144 | } | |
145 | ||
de08c606 WX |
146 | int bdrv_can_snapshot(BlockDriverState *bs) |
147 | { | |
148 | BlockDriver *drv = bs->drv; | |
149 | if (!drv || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) { | |
150 | return 0; | |
151 | } | |
152 | ||
153 | if (!drv->bdrv_snapshot_create) { | |
154 | if (bs->file != NULL) { | |
9a4f4c31 | 155 | return bdrv_can_snapshot(bs->file->bs); |
de08c606 WX |
156 | } |
157 | return 0; | |
158 | } | |
159 | ||
160 | return 1; | |
161 | } | |
162 | ||
163 | int bdrv_snapshot_create(BlockDriverState *bs, | |
164 | QEMUSnapshotInfo *sn_info) | |
165 | { | |
166 | BlockDriver *drv = bs->drv; | |
167 | if (!drv) { | |
168 | return -ENOMEDIUM; | |
169 | } | |
170 | if (drv->bdrv_snapshot_create) { | |
171 | return drv->bdrv_snapshot_create(bs, sn_info); | |
172 | } | |
173 | if (bs->file) { | |
9a4f4c31 | 174 | return bdrv_snapshot_create(bs->file->bs, sn_info); |
de08c606 WX |
175 | } |
176 | return -ENOTSUP; | |
177 | } | |
178 | ||
179 | int bdrv_snapshot_goto(BlockDriverState *bs, | |
0b62bcbc KW |
180 | const char *snapshot_id, |
181 | Error **errp) | |
de08c606 WX |
182 | { |
183 | BlockDriver *drv = bs->drv; | |
184 | int ret, open_ret; | |
04dec3c3 | 185 | int64_t len; |
de08c606 WX |
186 | |
187 | if (!drv) { | |
0b62bcbc | 188 | error_setg(errp, "Block driver is closed"); |
de08c606 WX |
189 | return -ENOMEDIUM; |
190 | } | |
04dec3c3 VSO |
191 | |
192 | len = bdrv_getlength(bs); | |
193 | if (len < 0) { | |
0b62bcbc | 194 | error_setg_errno(errp, -len, "Cannot get block device size"); |
04dec3c3 VSO |
195 | return len; |
196 | } | |
197 | /* We should set all bits in all enabled dirty bitmaps, because dirty | |
198 | * bitmaps reflect active state of disk and snapshot switch operation | |
199 | * actually dirties active state. | |
200 | * TODO: It may make sense not to set all bits but analyze block status of | |
201 | * current state and destination snapshot and do not set bits corresponding | |
202 | * to both-zero or both-unallocated areas. */ | |
203 | bdrv_set_dirty(bs, 0, len); | |
204 | ||
de08c606 | 205 | if (drv->bdrv_snapshot_goto) { |
0b62bcbc KW |
206 | ret = drv->bdrv_snapshot_goto(bs, snapshot_id); |
207 | if (ret < 0) { | |
208 | error_setg_errno(errp, -ret, "Failed to load snapshot"); | |
209 | } | |
210 | return ret; | |
de08c606 WX |
211 | } |
212 | ||
213 | if (bs->file) { | |
7a9e5119 DJS |
214 | BlockDriverState *file; |
215 | QDict *options = qdict_clone_shallow(bs->options); | |
216 | QDict *file_options; | |
0b62bcbc | 217 | Error *local_err = NULL; |
7a9e5119 DJS |
218 | |
219 | file = bs->file->bs; | |
220 | /* Prevent it from getting deleted when detached from bs */ | |
221 | bdrv_ref(file); | |
222 | ||
223 | qdict_extract_subqdict(options, &file_options, "file."); | |
224 | QDECREF(file_options); | |
46f5ac20 | 225 | qdict_put_str(options, "file", bdrv_get_node_name(file)); |
7a9e5119 | 226 | |
de08c606 | 227 | drv->bdrv_close(bs); |
7a9e5119 DJS |
228 | bdrv_unref_child(bs, bs->file); |
229 | bs->file = NULL; | |
230 | ||
0b62bcbc KW |
231 | ret = bdrv_snapshot_goto(file, snapshot_id, errp); |
232 | open_ret = drv->bdrv_open(bs, options, bs->open_flags, &local_err); | |
7a9e5119 | 233 | QDECREF(options); |
de08c606 | 234 | if (open_ret < 0) { |
7a9e5119 | 235 | bdrv_unref(file); |
de08c606 | 236 | bs->drv = NULL; |
0b62bcbc KW |
237 | /* A bdrv_snapshot_goto() error takes precedence */ |
238 | error_propagate(errp, local_err); | |
239 | return ret < 0 ? ret : open_ret; | |
de08c606 | 240 | } |
7a9e5119 DJS |
241 | |
242 | assert(bs->file->bs == file); | |
243 | bdrv_unref(file); | |
de08c606 WX |
244 | return ret; |
245 | } | |
246 | ||
0b62bcbc | 247 | error_setg(errp, "Block driver does not support snapshots"); |
de08c606 WX |
248 | return -ENOTSUP; |
249 | } | |
250 | ||
a89d89d3 WX |
251 | /** |
252 | * Delete an internal snapshot by @snapshot_id and @name. | |
253 | * @bs: block device used in the operation | |
254 | * @snapshot_id: unique snapshot ID, or NULL | |
255 | * @name: snapshot name, or NULL | |
256 | * @errp: location to store error | |
257 | * | |
258 | * If both @snapshot_id and @name are specified, delete the first one with | |
259 | * id @snapshot_id and name @name. | |
260 | * If only @snapshot_id is specified, delete the first one with id | |
261 | * @snapshot_id. | |
262 | * If only @name is specified, delete the first one with name @name. | |
7b4c4781 | 263 | * if none is specified, return -EINVAL. |
a89d89d3 WX |
264 | * |
265 | * Returns: 0 on success, -errno on failure. If @bs is not inserted, return | |
266 | * -ENOMEDIUM. If @snapshot_id and @name are both NULL, return -EINVAL. If @bs | |
267 | * does not support internal snapshot deletion, return -ENOTSUP. If @bs does | |
268 | * not support parameter @snapshot_id or @name, or one of them is not correctly | |
269 | * specified, return -EINVAL. If @bs can't find one matching @id and @name, | |
270 | * return -ENOENT. If @errp != NULL, it will always be filled with error | |
271 | * message on failure. | |
272 | */ | |
273 | int bdrv_snapshot_delete(BlockDriverState *bs, | |
274 | const char *snapshot_id, | |
275 | const char *name, | |
276 | Error **errp) | |
de08c606 WX |
277 | { |
278 | BlockDriver *drv = bs->drv; | |
27a7649a PB |
279 | int ret; |
280 | ||
de08c606 | 281 | if (!drv) { |
c6bd8c70 | 282 | error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, bdrv_get_device_name(bs)); |
de08c606 WX |
283 | return -ENOMEDIUM; |
284 | } | |
a89d89d3 WX |
285 | if (!snapshot_id && !name) { |
286 | error_setg(errp, "snapshot_id and name are both NULL"); | |
287 | return -EINVAL; | |
288 | } | |
3432a192 ZH |
289 | |
290 | /* drain all pending i/o before deleting snapshot */ | |
27a7649a | 291 | bdrv_drained_begin(bs); |
3432a192 | 292 | |
de08c606 | 293 | if (drv->bdrv_snapshot_delete) { |
27a7649a PB |
294 | ret = drv->bdrv_snapshot_delete(bs, snapshot_id, name, errp); |
295 | } else if (bs->file) { | |
296 | ret = bdrv_snapshot_delete(bs->file->bs, snapshot_id, name, errp); | |
297 | } else { | |
298 | error_setg(errp, "Block format '%s' used by device '%s' " | |
299 | "does not support internal snapshot deletion", | |
300 | drv->format_name, bdrv_get_device_name(bs)); | |
301 | ret = -ENOTSUP; | |
de08c606 | 302 | } |
27a7649a PB |
303 | |
304 | bdrv_drained_end(bs); | |
305 | return ret; | |
de08c606 WX |
306 | } |
307 | ||
25af925f DL |
308 | int bdrv_snapshot_delete_by_id_or_name(BlockDriverState *bs, |
309 | const char *id_or_name, | |
310 | Error **errp) | |
a89d89d3 WX |
311 | { |
312 | int ret; | |
313 | Error *local_err = NULL; | |
314 | ||
315 | ret = bdrv_snapshot_delete(bs, id_or_name, NULL, &local_err); | |
316 | if (ret == -ENOENT || ret == -EINVAL) { | |
317 | error_free(local_err); | |
318 | local_err = NULL; | |
319 | ret = bdrv_snapshot_delete(bs, NULL, id_or_name, &local_err); | |
320 | } | |
321 | ||
322 | if (ret < 0) { | |
323 | error_propagate(errp, local_err); | |
324 | } | |
25af925f | 325 | return ret; |
a89d89d3 WX |
326 | } |
327 | ||
de08c606 WX |
328 | int bdrv_snapshot_list(BlockDriverState *bs, |
329 | QEMUSnapshotInfo **psn_info) | |
330 | { | |
331 | BlockDriver *drv = bs->drv; | |
332 | if (!drv) { | |
333 | return -ENOMEDIUM; | |
334 | } | |
335 | if (drv->bdrv_snapshot_list) { | |
336 | return drv->bdrv_snapshot_list(bs, psn_info); | |
337 | } | |
338 | if (bs->file) { | |
9a4f4c31 | 339 | return bdrv_snapshot_list(bs->file->bs, psn_info); |
de08c606 WX |
340 | } |
341 | return -ENOTSUP; | |
342 | } | |
343 | ||
7b4c4781 WX |
344 | /** |
345 | * Temporarily load an internal snapshot by @snapshot_id and @name. | |
346 | * @bs: block device used in the operation | |
347 | * @snapshot_id: unique snapshot ID, or NULL | |
348 | * @name: snapshot name, or NULL | |
349 | * @errp: location to store error | |
350 | * | |
351 | * If both @snapshot_id and @name are specified, load the first one with | |
352 | * id @snapshot_id and name @name. | |
353 | * If only @snapshot_id is specified, load the first one with id | |
354 | * @snapshot_id. | |
355 | * If only @name is specified, load the first one with name @name. | |
356 | * if none is specified, return -EINVAL. | |
357 | * | |
358 | * Returns: 0 on success, -errno on fail. If @bs is not inserted, return | |
359 | * -ENOMEDIUM. If @bs is not readonly, return -EINVAL. If @bs did not support | |
360 | * internal snapshot, return -ENOTSUP. If qemu can't find a matching @id and | |
361 | * @name, return -ENOENT. If @errp != NULL, it will always be filled on | |
362 | * failure. | |
363 | */ | |
de08c606 | 364 | int bdrv_snapshot_load_tmp(BlockDriverState *bs, |
7b4c4781 WX |
365 | const char *snapshot_id, |
366 | const char *name, | |
367 | Error **errp) | |
de08c606 WX |
368 | { |
369 | BlockDriver *drv = bs->drv; | |
7b4c4781 | 370 | |
de08c606 | 371 | if (!drv) { |
c6bd8c70 | 372 | error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, bdrv_get_device_name(bs)); |
de08c606 WX |
373 | return -ENOMEDIUM; |
374 | } | |
7b4c4781 WX |
375 | if (!snapshot_id && !name) { |
376 | error_setg(errp, "snapshot_id and name are both NULL"); | |
377 | return -EINVAL; | |
378 | } | |
de08c606 | 379 | if (!bs->read_only) { |
7b4c4781 | 380 | error_setg(errp, "Device is not readonly"); |
de08c606 WX |
381 | return -EINVAL; |
382 | } | |
383 | if (drv->bdrv_snapshot_load_tmp) { | |
7b4c4781 | 384 | return drv->bdrv_snapshot_load_tmp(bs, snapshot_id, name, errp); |
de08c606 | 385 | } |
81e5f78a AG |
386 | error_setg(errp, "Block format '%s' used by device '%s' " |
387 | "does not support temporarily loading internal snapshots", | |
388 | drv->format_name, bdrv_get_device_name(bs)); | |
de08c606 WX |
389 | return -ENOTSUP; |
390 | } | |
7b4c4781 WX |
391 | |
392 | int bdrv_snapshot_load_tmp_by_id_or_name(BlockDriverState *bs, | |
393 | const char *id_or_name, | |
394 | Error **errp) | |
395 | { | |
396 | int ret; | |
397 | Error *local_err = NULL; | |
398 | ||
399 | ret = bdrv_snapshot_load_tmp(bs, id_or_name, NULL, &local_err); | |
400 | if (ret == -ENOENT || ret == -EINVAL) { | |
401 | error_free(local_err); | |
402 | local_err = NULL; | |
403 | ret = bdrv_snapshot_load_tmp(bs, NULL, id_or_name, &local_err); | |
404 | } | |
405 | ||
621ff94d | 406 | error_propagate(errp, local_err); |
7b4c4781 WX |
407 | |
408 | return ret; | |
409 | } | |
e9ff957a DL |
410 | |
411 | ||
412 | /* Group operations. All block drivers are involved. | |
413 | * These functions will properly handle dataplane (take aio_context_acquire | |
414 | * when appropriate for appropriate block drivers) */ | |
415 | ||
416 | bool bdrv_all_can_snapshot(BlockDriverState **first_bad_bs) | |
417 | { | |
418 | bool ok = true; | |
7c8eece4 | 419 | BlockDriverState *bs; |
88be7b4b | 420 | BdrvNextIterator it; |
e9ff957a | 421 | |
88be7b4b | 422 | for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) { |
e9ff957a DL |
423 | AioContext *ctx = bdrv_get_aio_context(bs); |
424 | ||
425 | aio_context_acquire(ctx); | |
426 | if (bdrv_is_inserted(bs) && !bdrv_is_read_only(bs)) { | |
427 | ok = bdrv_can_snapshot(bs); | |
428 | } | |
429 | aio_context_release(ctx); | |
88be7b4b | 430 | if (!ok) { |
5e003f17 | 431 | bdrv_next_cleanup(&it); |
88be7b4b KW |
432 | goto fail; |
433 | } | |
e9ff957a DL |
434 | } |
435 | ||
88be7b4b | 436 | fail: |
e9ff957a DL |
437 | *first_bad_bs = bs; |
438 | return ok; | |
439 | } | |
9b00ea37 DL |
440 | |
441 | int bdrv_all_delete_snapshot(const char *name, BlockDriverState **first_bad_bs, | |
442 | Error **err) | |
443 | { | |
444 | int ret = 0; | |
7c8eece4 | 445 | BlockDriverState *bs; |
88be7b4b | 446 | BdrvNextIterator it; |
9b00ea37 DL |
447 | QEMUSnapshotInfo sn1, *snapshot = &sn1; |
448 | ||
88be7b4b | 449 | for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) { |
9b00ea37 DL |
450 | AioContext *ctx = bdrv_get_aio_context(bs); |
451 | ||
452 | aio_context_acquire(ctx); | |
453 | if (bdrv_can_snapshot(bs) && | |
454 | bdrv_snapshot_find(bs, snapshot, name) >= 0) { | |
455 | ret = bdrv_snapshot_delete_by_id_or_name(bs, name, err); | |
456 | } | |
457 | aio_context_release(ctx); | |
88be7b4b | 458 | if (ret < 0) { |
5e003f17 | 459 | bdrv_next_cleanup(&it); |
88be7b4b KW |
460 | goto fail; |
461 | } | |
9b00ea37 DL |
462 | } |
463 | ||
88be7b4b | 464 | fail: |
9b00ea37 DL |
465 | *first_bad_bs = bs; |
466 | return ret; | |
467 | } | |
4c1cdbaa DL |
468 | |
469 | ||
2b624fe0 KW |
470 | int bdrv_all_goto_snapshot(const char *name, BlockDriverState **first_bad_bs, |
471 | Error **errp) | |
4c1cdbaa | 472 | { |
2b624fe0 | 473 | int ret = 0; |
7c8eece4 | 474 | BlockDriverState *bs; |
88be7b4b | 475 | BdrvNextIterator it; |
4c1cdbaa | 476 | |
88be7b4b | 477 | for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) { |
4c1cdbaa DL |
478 | AioContext *ctx = bdrv_get_aio_context(bs); |
479 | ||
480 | aio_context_acquire(ctx); | |
481 | if (bdrv_can_snapshot(bs)) { | |
2b624fe0 | 482 | ret = bdrv_snapshot_goto(bs, name, errp); |
4c1cdbaa DL |
483 | } |
484 | aio_context_release(ctx); | |
2b624fe0 | 485 | if (ret < 0) { |
5e003f17 | 486 | bdrv_next_cleanup(&it); |
88be7b4b KW |
487 | goto fail; |
488 | } | |
4c1cdbaa DL |
489 | } |
490 | ||
88be7b4b | 491 | fail: |
4c1cdbaa | 492 | *first_bad_bs = bs; |
2b624fe0 | 493 | return ret; |
4c1cdbaa | 494 | } |
723ccda1 DL |
495 | |
496 | int bdrv_all_find_snapshot(const char *name, BlockDriverState **first_bad_bs) | |
497 | { | |
498 | QEMUSnapshotInfo sn; | |
499 | int err = 0; | |
7c8eece4 | 500 | BlockDriverState *bs; |
88be7b4b | 501 | BdrvNextIterator it; |
723ccda1 | 502 | |
88be7b4b | 503 | for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) { |
723ccda1 DL |
504 | AioContext *ctx = bdrv_get_aio_context(bs); |
505 | ||
506 | aio_context_acquire(ctx); | |
507 | if (bdrv_can_snapshot(bs)) { | |
508 | err = bdrv_snapshot_find(bs, &sn, name); | |
509 | } | |
510 | aio_context_release(ctx); | |
88be7b4b | 511 | if (err < 0) { |
5e003f17 | 512 | bdrv_next_cleanup(&it); |
88be7b4b KW |
513 | goto fail; |
514 | } | |
723ccda1 DL |
515 | } |
516 | ||
88be7b4b | 517 | fail: |
723ccda1 DL |
518 | *first_bad_bs = bs; |
519 | return err; | |
520 | } | |
a9085f9b DL |
521 | |
522 | int bdrv_all_create_snapshot(QEMUSnapshotInfo *sn, | |
523 | BlockDriverState *vm_state_bs, | |
524 | uint64_t vm_state_size, | |
525 | BlockDriverState **first_bad_bs) | |
526 | { | |
527 | int err = 0; | |
7c8eece4 | 528 | BlockDriverState *bs; |
88be7b4b | 529 | BdrvNextIterator it; |
a9085f9b | 530 | |
88be7b4b | 531 | for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) { |
a9085f9b DL |
532 | AioContext *ctx = bdrv_get_aio_context(bs); |
533 | ||
534 | aio_context_acquire(ctx); | |
535 | if (bs == vm_state_bs) { | |
536 | sn->vm_state_size = vm_state_size; | |
537 | err = bdrv_snapshot_create(bs, sn); | |
538 | } else if (bdrv_can_snapshot(bs)) { | |
539 | sn->vm_state_size = 0; | |
540 | err = bdrv_snapshot_create(bs, sn); | |
541 | } | |
542 | aio_context_release(ctx); | |
88be7b4b | 543 | if (err < 0) { |
5e003f17 | 544 | bdrv_next_cleanup(&it); |
88be7b4b KW |
545 | goto fail; |
546 | } | |
a9085f9b DL |
547 | } |
548 | ||
88be7b4b | 549 | fail: |
a9085f9b DL |
550 | *first_bad_bs = bs; |
551 | return err; | |
552 | } | |
7cb14481 DL |
553 | |
554 | BlockDriverState *bdrv_all_find_vmstate_bs(void) | |
555 | { | |
7c8eece4 | 556 | BlockDriverState *bs; |
88be7b4b | 557 | BdrvNextIterator it; |
7cb14481 | 558 | |
88be7b4b | 559 | for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) { |
7cb14481 | 560 | AioContext *ctx = bdrv_get_aio_context(bs); |
88be7b4b | 561 | bool found; |
7cb14481 DL |
562 | |
563 | aio_context_acquire(ctx); | |
88be7b4b | 564 | found = bdrv_can_snapshot(bs); |
7cb14481 | 565 | aio_context_release(ctx); |
88be7b4b KW |
566 | |
567 | if (found) { | |
5e003f17 | 568 | bdrv_next_cleanup(&it); |
88be7b4b KW |
569 | break; |
570 | } | |
7cb14481 DL |
571 | } |
572 | return bs; | |
573 | } |