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 "qapi/qmp/qdict.h" 31 #include "qapi/qmp/qerror.h" 32 #include "qapi/qmp/qstring.h" 33 #include "qemu/option.h" 34 #include "sysemu/block-backend.h" 35 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 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 60 GLOBAL_STATE_CODE(); 61 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]; 69 if (!strcmp(sn->name, name)) { 70 *sn_info = *sn; 71 ret = 0; 72 break; 73 } 74 } 75 g_free(sn_tab); 76 return ret; 77 } 78 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); 110 GLOBAL_STATE_CODE(); 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 153 /** 154 * Return a pointer to child of given BDS to which we can fall 155 * back if the given BDS does not support snapshots. 156 * Return NULL if there is no BDS to (safely) fall back to. 157 */ 158 static BdrvChild * GRAPH_RDLOCK 159 bdrv_snapshot_fallback_child(BlockDriverState *bs) 160 { 161 BdrvChild *fallback = bdrv_primary_child(bs); 162 BdrvChild *child; 163 164 GLOBAL_STATE_CODE(); 165 assert_bdrv_graph_readable(); 166 167 /* We allow fallback only to primary child */ 168 if (!fallback) { 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 175 * fallback. 176 */ 177 QLIST_FOREACH(child, &bs->children, next) { 178 if (child->role & (BDRV_CHILD_DATA | BDRV_CHILD_METADATA | 179 BDRV_CHILD_FILTERED) && 180 child != fallback) 181 { 182 return NULL; 183 } 184 } 185 186 return fallback; 187 } 188 189 static BlockDriverState * GRAPH_RDLOCK 190 bdrv_snapshot_fallback(BlockDriverState *bs) 191 { 192 GLOBAL_STATE_CODE(); 193 return child_bs(bdrv_snapshot_fallback_child(bs)); 194 } 195 196 int bdrv_can_snapshot(BlockDriverState *bs) 197 { 198 BlockDriver *drv = bs->drv; 199 200 GLOBAL_STATE_CODE(); 201 202 if (!drv || !bdrv_is_inserted(bs) || !bdrv_is_writable(bs)) { 203 return 0; 204 } 205 206 if (!drv->bdrv_snapshot_create) { 207 BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs); 208 if (fallback_bs) { 209 return bdrv_can_snapshot(fallback_bs); 210 } 211 return 0; 212 } 213 214 return 1; 215 } 216 217 int bdrv_snapshot_create(BlockDriverState *bs, 218 QEMUSnapshotInfo *sn_info) 219 { 220 BlockDriver *drv = bs->drv; 221 BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs); 222 223 GLOBAL_STATE_CODE(); 224 225 if (!drv) { 226 return -ENOMEDIUM; 227 } 228 if (drv->bdrv_snapshot_create) { 229 return drv->bdrv_snapshot_create(bs, sn_info); 230 } 231 if (fallback_bs) { 232 return bdrv_snapshot_create(fallback_bs, sn_info); 233 } 234 return -ENOTSUP; 235 } 236 237 int bdrv_snapshot_goto(BlockDriverState *bs, 238 const char *snapshot_id, 239 Error **errp) 240 { 241 BlockDriver *drv = bs->drv; 242 BdrvChild *fallback; 243 int ret, open_ret; 244 245 GLOBAL_STATE_CODE(); 246 247 if (!drv) { 248 error_setg(errp, "Block driver is closed"); 249 return -ENOMEDIUM; 250 } 251 252 if (!QLIST_EMPTY(&bs->dirty_bitmaps)) { 253 error_setg(errp, "Device has active dirty bitmaps"); 254 return -EBUSY; 255 } 256 257 if (drv->bdrv_snapshot_goto) { 258 ret = drv->bdrv_snapshot_goto(bs, snapshot_id); 259 if (ret < 0) { 260 error_setg_errno(errp, -ret, "Failed to load snapshot"); 261 } 262 return ret; 263 } 264 265 bdrv_graph_rdlock_main_loop(); 266 fallback = bdrv_snapshot_fallback_child(bs); 267 bdrv_graph_rdunlock_main_loop(); 268 269 if (fallback) { 270 QDict *options; 271 QDict *file_options; 272 Error *local_err = NULL; 273 BlockDriverState *fallback_bs = fallback->bs; 274 char *subqdict_prefix = g_strdup_printf("%s.", fallback->name); 275 276 options = qdict_clone_shallow(bs->options); 277 278 /* Prevent it from getting deleted when detached from bs */ 279 bdrv_ref(fallback_bs); 280 281 qdict_extract_subqdict(options, &file_options, subqdict_prefix); 282 qobject_unref(file_options); 283 g_free(subqdict_prefix); 284 285 /* Force .bdrv_open() below to re-attach fallback_bs on fallback */ 286 qdict_put_str(options, fallback->name, 287 bdrv_get_node_name(fallback_bs)); 288 289 /* Now close bs, apply the snapshot on fallback_bs, and re-open bs */ 290 if (drv->bdrv_close) { 291 drv->bdrv_close(bs); 292 } 293 294 /* .bdrv_open() will re-attach it */ 295 bdrv_graph_wrlock(); 296 bdrv_unref_child(bs, fallback); 297 bdrv_graph_wrunlock(); 298 299 ret = bdrv_snapshot_goto(fallback_bs, snapshot_id, errp); 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, QERR_DEVICE_HAS_NO_MEDIUM, bdrv_get_device_name(bs)); 363 return -ENOMEDIUM; 364 } 365 if (!snapshot_id && !name) { 366 error_setg(errp, "snapshot_id and name are both NULL"); 367 return -EINVAL; 368 } 369 370 /* drain all pending i/o before deleting snapshot */ 371 bdrv_drained_begin(bs); 372 373 if (drv->bdrv_snapshot_delete) { 374 ret = drv->bdrv_snapshot_delete(bs, snapshot_id, name, errp); 375 } else if (fallback_bs) { 376 ret = bdrv_snapshot_delete(fallback_bs, snapshot_id, name, errp); 377 } else { 378 error_setg(errp, "Block format '%s' used by device '%s' " 379 "does not support internal snapshot deletion", 380 drv->format_name, bdrv_get_device_name(bs)); 381 ret = -ENOTSUP; 382 } 383 384 bdrv_drained_end(bs); 385 return ret; 386 } 387 388 int bdrv_snapshot_list(BlockDriverState *bs, 389 QEMUSnapshotInfo **psn_info) 390 { 391 GLOBAL_STATE_CODE(); 392 GRAPH_RDLOCK_GUARD_MAINLOOP(); 393 394 BlockDriver *drv = bs->drv; 395 BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs); 396 397 if (!drv) { 398 return -ENOMEDIUM; 399 } 400 if (drv->bdrv_snapshot_list) { 401 return drv->bdrv_snapshot_list(bs, psn_info); 402 } 403 if (fallback_bs) { 404 return bdrv_snapshot_list(fallback_bs, psn_info); 405 } 406 return -ENOTSUP; 407 } 408 409 /** 410 * Temporarily load an internal snapshot by @snapshot_id and @name. 411 * @bs: block device used in the operation 412 * @snapshot_id: unique snapshot ID, or NULL 413 * @name: snapshot name, or NULL 414 * @errp: location to store error 415 * 416 * If both @snapshot_id and @name are specified, load the first one with 417 * id @snapshot_id and name @name. 418 * If only @snapshot_id is specified, load the first one with id 419 * @snapshot_id. 420 * If only @name is specified, load the first one with name @name. 421 * if none is specified, return -EINVAL. 422 * 423 * Returns: 0 on success, -errno on fail. If @bs is not inserted, return 424 * -ENOMEDIUM. If @bs is not readonly, return -EINVAL. If @bs did not support 425 * internal snapshot, return -ENOTSUP. If qemu can't find a matching @id and 426 * @name, return -ENOENT. If @errp != NULL, it will always be filled on 427 * failure. 428 */ 429 int bdrv_snapshot_load_tmp(BlockDriverState *bs, 430 const char *snapshot_id, 431 const char *name, 432 Error **errp) 433 { 434 BlockDriver *drv = bs->drv; 435 436 GLOBAL_STATE_CODE(); 437 GRAPH_RDLOCK_GUARD_MAINLOOP(); 438 439 if (!drv) { 440 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, bdrv_get_device_name(bs)); 441 return -ENOMEDIUM; 442 } 443 if (!snapshot_id && !name) { 444 error_setg(errp, "snapshot_id and name are both NULL"); 445 return -EINVAL; 446 } 447 if (!bdrv_is_read_only(bs)) { 448 error_setg(errp, "Device is not readonly"); 449 return -EINVAL; 450 } 451 if (drv->bdrv_snapshot_load_tmp) { 452 return drv->bdrv_snapshot_load_tmp(bs, snapshot_id, name, errp); 453 } 454 error_setg(errp, "Block format '%s' used by device '%s' " 455 "does not support temporarily loading internal snapshots", 456 drv->format_name, bdrv_get_device_name(bs)); 457 return -ENOTSUP; 458 } 459 460 int bdrv_snapshot_load_tmp_by_id_or_name(BlockDriverState *bs, 461 const char *id_or_name, 462 Error **errp) 463 { 464 int ret; 465 Error *local_err = NULL; 466 467 GLOBAL_STATE_CODE(); 468 469 ret = bdrv_snapshot_load_tmp(bs, id_or_name, NULL, &local_err); 470 if (ret == -ENOENT || ret == -EINVAL) { 471 error_free(local_err); 472 local_err = NULL; 473 ret = bdrv_snapshot_load_tmp(bs, NULL, id_or_name, &local_err); 474 } 475 476 error_propagate(errp, local_err); 477 478 return ret; 479 } 480 481 482 static int GRAPH_RDLOCK 483 bdrv_all_get_snapshot_devices(bool has_devices, strList *devices, 484 GList **all_bdrvs, Error **errp) 485 { 486 g_autoptr(GList) bdrvs = NULL; 487 488 if (has_devices) { 489 if (!devices) { 490 error_setg(errp, "At least one device is required for snapshot"); 491 return -1; 492 } 493 494 while (devices) { 495 BlockDriverState *bs = bdrv_find_node(devices->value); 496 if (!bs) { 497 error_setg(errp, "No block device node '%s'", devices->value); 498 return -1; 499 } 500 bdrvs = g_list_append(bdrvs, bs); 501 devices = devices->next; 502 } 503 } else { 504 BlockDriverState *bs; 505 BdrvNextIterator it; 506 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) { 507 bdrvs = g_list_append(bdrvs, bs); 508 } 509 } 510 511 *all_bdrvs = g_steal_pointer(&bdrvs); 512 return 0; 513 } 514 515 516 static bool GRAPH_RDLOCK bdrv_all_snapshots_includes_bs(BlockDriverState *bs) 517 { 518 GLOBAL_STATE_CODE(); 519 assert_bdrv_graph_readable(); 520 521 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) { 522 return false; 523 } 524 525 /* Include all nodes that are either in use by a BlockBackend, or that 526 * aren't attached to any node, but owned by the monitor. */ 527 return bdrv_has_blk(bs) || QLIST_EMPTY(&bs->parents); 528 } 529 530 /* Group operations. All block drivers are involved. */ 531 532 bool bdrv_all_can_snapshot(bool has_devices, strList *devices, 533 Error **errp) 534 { 535 g_autoptr(GList) bdrvs = NULL; 536 GList *iterbdrvs; 537 538 GLOBAL_STATE_CODE(); 539 GRAPH_RDLOCK_GUARD_MAINLOOP(); 540 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; 548 bool ok = true; 549 550 if (devices || bdrv_all_snapshots_includes_bs(bs)) { 551 ok = bdrv_can_snapshot(bs); 552 } 553 if (!ok) { 554 error_setg(errp, "Device '%s' is writable but does not support " 555 "snapshots", bdrv_get_device_or_node_name(bs)); 556 return false; 557 } 558 559 iterbdrvs = iterbdrvs->next; 560 } 561 562 return true; 563 } 564 565 int bdrv_all_delete_snapshot(const char *name, 566 bool has_devices, strList *devices, 567 Error **errp) 568 { 569 ERRP_GUARD(); 570 g_autoptr(GList) bdrvs = NULL; 571 GList *iterbdrvs; 572 573 GLOBAL_STATE_CODE(); 574 GRAPH_RDLOCK_GUARD_MAINLOOP(); 575 576 if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) { 577 return -1; 578 } 579 580 iterbdrvs = bdrvs; 581 while (iterbdrvs) { 582 BlockDriverState *bs = iterbdrvs->data; 583 QEMUSnapshotInfo sn1, *snapshot = &sn1; 584 int ret = 0; 585 586 if ((devices || bdrv_all_snapshots_includes_bs(bs)) && 587 bdrv_snapshot_find(bs, snapshot, name) >= 0) 588 { 589 ret = bdrv_snapshot_delete(bs, snapshot->id_str, 590 snapshot->name, errp); 591 } 592 if (ret < 0) { 593 error_prepend(errp, "Could not delete snapshot '%s' on '%s': ", 594 name, bdrv_get_device_or_node_name(bs)); 595 return -1; 596 } 597 598 iterbdrvs = iterbdrvs->next; 599 } 600 601 return 0; 602 } 603 604 605 int bdrv_all_goto_snapshot(const char *name, 606 bool has_devices, strList *devices, 607 Error **errp) 608 { 609 ERRP_GUARD(); 610 g_autoptr(GList) bdrvs = NULL; 611 GList *iterbdrvs; 612 int ret; 613 614 GLOBAL_STATE_CODE(); 615 616 bdrv_graph_rdlock_main_loop(); 617 ret = bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp); 618 bdrv_graph_rdunlock_main_loop(); 619 620 if (ret < 0) { 621 return -1; 622 } 623 624 iterbdrvs = bdrvs; 625 while (iterbdrvs) { 626 BlockDriverState *bs = iterbdrvs->data; 627 bool all_snapshots_includes_bs; 628 629 bdrv_graph_rdlock_main_loop(); 630 all_snapshots_includes_bs = bdrv_all_snapshots_includes_bs(bs); 631 bdrv_graph_rdunlock_main_loop(); 632 633 ret = (devices || all_snapshots_includes_bs) ? 634 bdrv_snapshot_goto(bs, name, errp) : 0; 635 if (ret < 0) { 636 bdrv_graph_rdlock_main_loop(); 637 error_prepend(errp, "Could not load snapshot '%s' on '%s': ", 638 name, bdrv_get_device_or_node_name(bs)); 639 bdrv_graph_rdunlock_main_loop(); 640 return -1; 641 } 642 643 iterbdrvs = iterbdrvs->next; 644 } 645 646 return 0; 647 } 648 649 int bdrv_all_has_snapshot(const char *name, 650 bool has_devices, strList *devices, 651 Error **errp) 652 { 653 g_autoptr(GList) bdrvs = NULL; 654 GList *iterbdrvs; 655 656 GLOBAL_STATE_CODE(); 657 GRAPH_RDLOCK_GUARD_MAINLOOP(); 658 659 if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) { 660 return -1; 661 } 662 663 iterbdrvs = bdrvs; 664 while (iterbdrvs) { 665 BlockDriverState *bs = iterbdrvs->data; 666 QEMUSnapshotInfo sn; 667 int ret = 0; 668 669 if (devices || bdrv_all_snapshots_includes_bs(bs)) { 670 ret = bdrv_snapshot_find(bs, &sn, name); 671 } 672 if (ret < 0) { 673 if (ret == -ENOENT) { 674 return 0; 675 } else { 676 error_setg_errno(errp, errno, 677 "Could not check snapshot '%s' on '%s'", 678 name, bdrv_get_device_or_node_name(bs)); 679 return -1; 680 } 681 } 682 683 iterbdrvs = iterbdrvs->next; 684 } 685 686 return 1; 687 } 688 689 int bdrv_all_create_snapshot(QEMUSnapshotInfo *sn, 690 BlockDriverState *vm_state_bs, 691 uint64_t vm_state_size, 692 bool has_devices, strList *devices, 693 Error **errp) 694 { 695 g_autoptr(GList) bdrvs = NULL; 696 GList *iterbdrvs; 697 698 GLOBAL_STATE_CODE(); 699 GRAPH_RDLOCK_GUARD_MAINLOOP(); 700 701 if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) { 702 return -1; 703 } 704 705 iterbdrvs = bdrvs; 706 while (iterbdrvs) { 707 BlockDriverState *bs = iterbdrvs->data; 708 int ret = 0; 709 710 if (bs == vm_state_bs) { 711 sn->vm_state_size = vm_state_size; 712 ret = bdrv_snapshot_create(bs, sn); 713 } else if (devices || bdrv_all_snapshots_includes_bs(bs)) { 714 sn->vm_state_size = 0; 715 ret = bdrv_snapshot_create(bs, sn); 716 } 717 if (ret < 0) { 718 error_setg(errp, "Could not create snapshot '%s' on '%s'", 719 sn->name, bdrv_get_device_or_node_name(bs)); 720 return -1; 721 } 722 723 iterbdrvs = iterbdrvs->next; 724 } 725 726 return 0; 727 } 728 729 730 BlockDriverState *bdrv_all_find_vmstate_bs(const char *vmstate_bs, 731 bool has_devices, strList *devices, 732 Error **errp) 733 { 734 g_autoptr(GList) bdrvs = NULL; 735 GList *iterbdrvs; 736 737 GLOBAL_STATE_CODE(); 738 GRAPH_RDLOCK_GUARD_MAINLOOP(); 739 740 if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) { 741 return NULL; 742 } 743 744 iterbdrvs = bdrvs; 745 while (iterbdrvs) { 746 BlockDriverState *bs = iterbdrvs->data; 747 bool found = false; 748 749 found = (devices || bdrv_all_snapshots_includes_bs(bs)) && 750 bdrv_can_snapshot(bs); 751 752 if (vmstate_bs) { 753 if (g_str_equal(vmstate_bs, 754 bdrv_get_node_name(bs))) { 755 if (found) { 756 return bs; 757 } else { 758 error_setg(errp, 759 "vmstate block device '%s' does not support snapshots", 760 vmstate_bs); 761 return NULL; 762 } 763 } 764 } else if (found) { 765 return bs; 766 } 767 768 iterbdrvs = iterbdrvs->next; 769 } 770 771 if (vmstate_bs) { 772 error_setg(errp, 773 "vmstate block device '%s' does not exist", vmstate_bs); 774 } else { 775 error_setg(errp, 776 "no block device can store vmstate for snapshot"); 777 } 778 return NULL; 779 } 780