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