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