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_graph_wrlock(); 295 bdrv_unref_child(bs, fallback); 296 bdrv_graph_wrunlock(); 297 298 ret = bdrv_snapshot_goto(fallback_bs, snapshot_id, errp); 299 memset(bs->opaque, 0, drv->instance_size); 300 open_ret = drv->bdrv_open(bs, options, bs->open_flags, &local_err); 301 qobject_unref(options); 302 if (open_ret < 0) { 303 bdrv_unref(fallback_bs); 304 bs->drv = NULL; 305 /* A bdrv_snapshot_goto() error takes precedence */ 306 error_propagate(errp, local_err); 307 return ret < 0 ? ret : open_ret; 308 } 309 310 /* 311 * fallback was a primary child. It was closed above and set to NULL, 312 * but the .bdrv_open() call has opened it again, because we set the 313 * respective option (with the qdict_put_str() call above). 314 * Assert that .bdrv_open() has attached the right BDS as primary child. 315 */ 316 bdrv_graph_rdlock_main_loop(); 317 assert(bdrv_primary_bs(bs) == fallback_bs); 318 bdrv_graph_rdunlock_main_loop(); 319 320 bdrv_unref(fallback_bs); 321 return ret; 322 } 323 324 error_setg(errp, "Block driver does not support snapshots"); 325 return -ENOTSUP; 326 } 327 328 /** 329 * Delete an internal snapshot by @snapshot_id and @name. 330 * @bs: block device used in the operation 331 * @snapshot_id: unique snapshot ID, or NULL 332 * @name: snapshot name, or NULL 333 * @errp: location to store error 334 * 335 * If both @snapshot_id and @name are specified, delete the first one with 336 * id @snapshot_id and name @name. 337 * If only @snapshot_id is specified, delete the first one with id 338 * @snapshot_id. 339 * If only @name is specified, delete the first one with name @name. 340 * if none is specified, return -EINVAL. 341 * 342 * Returns: 0 on success, -errno on failure. If @bs is not inserted, return 343 * -ENOMEDIUM. If @snapshot_id and @name are both NULL, return -EINVAL. If @bs 344 * does not support internal snapshot deletion, return -ENOTSUP. If @bs does 345 * not support parameter @snapshot_id or @name, or one of them is not correctly 346 * specified, return -EINVAL. If @bs can't find one matching @id and @name, 347 * return -ENOENT. If @errp != NULL, it will always be filled with error 348 * message on failure. 349 */ 350 int bdrv_snapshot_delete(BlockDriverState *bs, 351 const char *snapshot_id, 352 const char *name, 353 Error **errp) 354 { 355 BlockDriver *drv = bs->drv; 356 BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs); 357 int ret; 358 359 GLOBAL_STATE_CODE(); 360 361 if (!drv) { 362 error_setg(errp, "Device '%s' has no medium", 363 bdrv_get_device_name(bs)); 364 return -ENOMEDIUM; 365 } 366 if (!snapshot_id && !name) { 367 error_setg(errp, "snapshot_id and name are both NULL"); 368 return -EINVAL; 369 } 370 371 /* drain all pending i/o before deleting snapshot */ 372 bdrv_drained_begin(bs); 373 374 if (drv->bdrv_snapshot_delete) { 375 ret = drv->bdrv_snapshot_delete(bs, snapshot_id, name, errp); 376 } else if (fallback_bs) { 377 ret = bdrv_snapshot_delete(fallback_bs, snapshot_id, name, errp); 378 } else { 379 error_setg(errp, "Block format '%s' used by device '%s' " 380 "does not support internal snapshot deletion", 381 drv->format_name, bdrv_get_device_name(bs)); 382 ret = -ENOTSUP; 383 } 384 385 bdrv_drained_end(bs); 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 575 GLOBAL_STATE_CODE(); 576 GRAPH_RDLOCK_GUARD_MAINLOOP(); 577 578 if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) { 579 return -1; 580 } 581 582 iterbdrvs = bdrvs; 583 while (iterbdrvs) { 584 BlockDriverState *bs = iterbdrvs->data; 585 QEMUSnapshotInfo sn1, *snapshot = &sn1; 586 int ret = 0; 587 588 if ((devices || bdrv_all_snapshots_includes_bs(bs)) && 589 bdrv_snapshot_find(bs, snapshot, name) >= 0) 590 { 591 ret = bdrv_snapshot_delete(bs, snapshot->id_str, 592 snapshot->name, errp); 593 } 594 if (ret < 0) { 595 error_prepend(errp, "Could not delete snapshot '%s' on '%s': ", 596 name, bdrv_get_device_or_node_name(bs)); 597 return -1; 598 } 599 600 iterbdrvs = iterbdrvs->next; 601 } 602 603 return 0; 604 } 605 606 607 int bdrv_all_goto_snapshot(const char *name, 608 bool has_devices, strList *devices, 609 Error **errp) 610 { 611 ERRP_GUARD(); 612 g_autoptr(GList) bdrvs = NULL; 613 GList *iterbdrvs; 614 int ret; 615 616 GLOBAL_STATE_CODE(); 617 618 bdrv_graph_rdlock_main_loop(); 619 ret = bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp); 620 bdrv_graph_rdunlock_main_loop(); 621 622 if (ret < 0) { 623 return -1; 624 } 625 626 iterbdrvs = bdrvs; 627 while (iterbdrvs) { 628 BlockDriverState *bs = iterbdrvs->data; 629 bool all_snapshots_includes_bs; 630 631 bdrv_graph_rdlock_main_loop(); 632 all_snapshots_includes_bs = bdrv_all_snapshots_includes_bs(bs); 633 bdrv_graph_rdunlock_main_loop(); 634 635 ret = (devices || all_snapshots_includes_bs) ? 636 bdrv_snapshot_goto(bs, name, errp) : 0; 637 if (ret < 0) { 638 bdrv_graph_rdlock_main_loop(); 639 error_prepend(errp, "Could not load snapshot '%s' on '%s': ", 640 name, bdrv_get_device_or_node_name(bs)); 641 bdrv_graph_rdunlock_main_loop(); 642 return -1; 643 } 644 645 iterbdrvs = iterbdrvs->next; 646 } 647 648 return 0; 649 } 650 651 int bdrv_all_has_snapshot(const char *name, 652 bool has_devices, strList *devices, 653 Error **errp) 654 { 655 g_autoptr(GList) bdrvs = NULL; 656 GList *iterbdrvs; 657 658 GLOBAL_STATE_CODE(); 659 GRAPH_RDLOCK_GUARD_MAINLOOP(); 660 661 if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) { 662 return -1; 663 } 664 665 iterbdrvs = bdrvs; 666 while (iterbdrvs) { 667 BlockDriverState *bs = iterbdrvs->data; 668 QEMUSnapshotInfo sn; 669 int ret = 0; 670 671 if (devices || bdrv_all_snapshots_includes_bs(bs)) { 672 ret = bdrv_snapshot_find(bs, &sn, name); 673 } 674 if (ret < 0) { 675 if (ret == -ENOENT) { 676 return 0; 677 } else { 678 error_setg_errno(errp, errno, 679 "Could not check snapshot '%s' on '%s'", 680 name, bdrv_get_device_or_node_name(bs)); 681 return -1; 682 } 683 } 684 685 iterbdrvs = iterbdrvs->next; 686 } 687 688 return 1; 689 } 690 691 int bdrv_all_create_snapshot(QEMUSnapshotInfo *sn, 692 BlockDriverState *vm_state_bs, 693 uint64_t vm_state_size, 694 bool has_devices, strList *devices, 695 Error **errp) 696 { 697 g_autoptr(GList) bdrvs = NULL; 698 GList *iterbdrvs; 699 700 GLOBAL_STATE_CODE(); 701 GRAPH_RDLOCK_GUARD_MAINLOOP(); 702 703 if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) { 704 return -1; 705 } 706 707 iterbdrvs = bdrvs; 708 while (iterbdrvs) { 709 BlockDriverState *bs = iterbdrvs->data; 710 int ret = 0; 711 712 if (bs == vm_state_bs) { 713 sn->vm_state_size = vm_state_size; 714 ret = bdrv_snapshot_create(bs, sn); 715 } else if (devices || bdrv_all_snapshots_includes_bs(bs)) { 716 sn->vm_state_size = 0; 717 ret = bdrv_snapshot_create(bs, sn); 718 } 719 if (ret < 0) { 720 error_setg(errp, "Could not create snapshot '%s' on '%s'", 721 sn->name, bdrv_get_device_or_node_name(bs)); 722 return -1; 723 } 724 725 iterbdrvs = iterbdrvs->next; 726 } 727 728 return 0; 729 } 730 731 732 BlockDriverState *bdrv_all_find_vmstate_bs(const char *vmstate_bs, 733 bool has_devices, strList *devices, 734 Error **errp) 735 { 736 g_autoptr(GList) bdrvs = NULL; 737 GList *iterbdrvs; 738 739 GLOBAL_STATE_CODE(); 740 GRAPH_RDLOCK_GUARD_MAINLOOP(); 741 742 if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) { 743 return NULL; 744 } 745 746 iterbdrvs = bdrvs; 747 while (iterbdrvs) { 748 BlockDriverState *bs = iterbdrvs->data; 749 bool found = false; 750 751 found = (devices || bdrv_all_snapshots_includes_bs(bs)) && 752 bdrv_can_snapshot(bs); 753 754 if (vmstate_bs) { 755 if (g_str_equal(vmstate_bs, 756 bdrv_get_node_name(bs))) { 757 if (found) { 758 return bs; 759 } else { 760 error_setg(errp, 761 "vmstate block device '%s' does not support snapshots", 762 vmstate_bs); 763 return NULL; 764 } 765 } 766 } else if (found) { 767 return bs; 768 } 769 770 iterbdrvs = iterbdrvs->next; 771 } 772 773 if (vmstate_bs) { 774 error_setg(errp, 775 "vmstate block device '%s' does not exist", vmstate_bs); 776 } else { 777 error_setg(errp, 778 "no block device can store vmstate for snapshot"); 779 } 780 return NULL; 781 } 782