| 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_drained(); |
| 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, must be drained |
| 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 | assert(bs->quiesce_counter > 0); |
| 362 | |
| 363 | if (!drv) { |
| 364 | error_setg(errp, "Device '%s' has no medium", |
| 365 | bdrv_get_device_name(bs)); |
| 366 | return -ENOMEDIUM; |
| 367 | } |
| 368 | if (!snapshot_id && !name) { |
| 369 | error_setg(errp, "snapshot_id and name are both NULL"); |
| 370 | return -EINVAL; |
| 371 | } |
| 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 | return ret; |
| 385 | } |
| 386 | |
| 387 | int bdrv_snapshot_list(BlockDriverState *bs, |
| 388 | QEMUSnapshotInfo **psn_info) |
| 389 | { |
| 390 | GLOBAL_STATE_CODE(); |
| 391 | GRAPH_RDLOCK_GUARD_MAINLOOP(); |
| 392 | |
| 393 | BlockDriver *drv = bs->drv; |
| 394 | BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs); |
| 395 | |
| 396 | if (!drv) { |
| 397 | return -ENOMEDIUM; |
| 398 | } |
| 399 | if (drv->bdrv_snapshot_list) { |
| 400 | return drv->bdrv_snapshot_list(bs, psn_info); |
| 401 | } |
| 402 | if (fallback_bs) { |
| 403 | return bdrv_snapshot_list(fallback_bs, psn_info); |
| 404 | } |
| 405 | return -ENOTSUP; |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * Temporarily load an internal snapshot by @snapshot_id and @name. |
| 410 | * @bs: block device used in the operation |
| 411 | * @snapshot_id: unique snapshot ID, or NULL |
| 412 | * @name: snapshot name, or NULL |
| 413 | * @errp: location to store error |
| 414 | * |
| 415 | * If both @snapshot_id and @name are specified, load the first one with |
| 416 | * id @snapshot_id and name @name. |
| 417 | * If only @snapshot_id is specified, load the first one with id |
| 418 | * @snapshot_id. |
| 419 | * If only @name is specified, load the first one with name @name. |
| 420 | * if none is specified, return -EINVAL. |
| 421 | * |
| 422 | * Returns: 0 on success, -errno on fail. If @bs is not inserted, return |
| 423 | * -ENOMEDIUM. If @bs is not readonly, return -EINVAL. If @bs did not support |
| 424 | * internal snapshot, return -ENOTSUP. If qemu can't find a matching @id and |
| 425 | * @name, return -ENOENT. If @errp != NULL, it will always be filled on |
| 426 | * failure. |
| 427 | */ |
| 428 | int bdrv_snapshot_load_tmp(BlockDriverState *bs, |
| 429 | const char *snapshot_id, |
| 430 | const char *name, |
| 431 | Error **errp) |
| 432 | { |
| 433 | BlockDriver *drv = bs->drv; |
| 434 | |
| 435 | GLOBAL_STATE_CODE(); |
| 436 | GRAPH_RDLOCK_GUARD_MAINLOOP(); |
| 437 | |
| 438 | if (!drv) { |
| 439 | error_setg(errp, "Device '%s' has no medium", |
| 440 | 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 | int ret = 0; |
| 573 | |
| 574 | GLOBAL_STATE_CODE(); |
| 575 | |
| 576 | bdrv_drain_all_begin(); |
| 577 | bdrv_graph_rdlock_main_loop(); |
| 578 | |
| 579 | ret = bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp); |
| 580 | if (ret < 0) { |
| 581 | goto out; |
| 582 | } |
| 583 | |
| 584 | iterbdrvs = bdrvs; |
| 585 | while (iterbdrvs) { |
| 586 | BlockDriverState *bs = iterbdrvs->data; |
| 587 | QEMUSnapshotInfo sn1, *snapshot = &sn1; |
| 588 | |
| 589 | if ((devices || bdrv_all_snapshots_includes_bs(bs)) && |
| 590 | bdrv_snapshot_find(bs, snapshot, name) >= 0) |
| 591 | { |
| 592 | ret = bdrv_snapshot_delete(bs, snapshot->id_str, |
| 593 | snapshot->name, errp); |
| 594 | } |
| 595 | if (ret < 0) { |
| 596 | error_prepend(errp, "Could not delete snapshot '%s' on '%s': ", |
| 597 | name, bdrv_get_device_or_node_name(bs)); |
| 598 | goto out; |
| 599 | } |
| 600 | |
| 601 | iterbdrvs = iterbdrvs->next; |
| 602 | } |
| 603 | |
| 604 | out: |
| 605 | bdrv_graph_rdunlock_main_loop(); |
| 606 | bdrv_drain_all_end(); |
| 607 | return ret; |
| 608 | } |
| 609 | |
| 610 | |
| 611 | int bdrv_all_goto_snapshot(const char *name, |
| 612 | bool has_devices, strList *devices, |
| 613 | Error **errp) |
| 614 | { |
| 615 | ERRP_GUARD(); |
| 616 | g_autoptr(GList) bdrvs = NULL; |
| 617 | GList *iterbdrvs; |
| 618 | int ret; |
| 619 | |
| 620 | GLOBAL_STATE_CODE(); |
| 621 | |
| 622 | bdrv_graph_rdlock_main_loop(); |
| 623 | ret = bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp); |
| 624 | bdrv_graph_rdunlock_main_loop(); |
| 625 | |
| 626 | if (ret < 0) { |
| 627 | return -1; |
| 628 | } |
| 629 | |
| 630 | iterbdrvs = bdrvs; |
| 631 | while (iterbdrvs) { |
| 632 | BlockDriverState *bs = iterbdrvs->data; |
| 633 | bool all_snapshots_includes_bs; |
| 634 | |
| 635 | bdrv_graph_rdlock_main_loop(); |
| 636 | all_snapshots_includes_bs = bdrv_all_snapshots_includes_bs(bs); |
| 637 | bdrv_graph_rdunlock_main_loop(); |
| 638 | |
| 639 | ret = (devices || all_snapshots_includes_bs) ? |
| 640 | bdrv_snapshot_goto(bs, name, errp) : 0; |
| 641 | if (ret < 0) { |
| 642 | bdrv_graph_rdlock_main_loop(); |
| 643 | error_prepend(errp, "Could not load snapshot '%s' on '%s': ", |
| 644 | name, bdrv_get_device_or_node_name(bs)); |
| 645 | bdrv_graph_rdunlock_main_loop(); |
| 646 | return -1; |
| 647 | } |
| 648 | |
| 649 | iterbdrvs = iterbdrvs->next; |
| 650 | } |
| 651 | |
| 652 | return 0; |
| 653 | } |
| 654 | |
| 655 | int bdrv_all_has_snapshot(const char *name, |
| 656 | bool has_devices, strList *devices, |
| 657 | Error **errp) |
| 658 | { |
| 659 | g_autoptr(GList) bdrvs = NULL; |
| 660 | GList *iterbdrvs; |
| 661 | |
| 662 | GLOBAL_STATE_CODE(); |
| 663 | GRAPH_RDLOCK_GUARD_MAINLOOP(); |
| 664 | |
| 665 | if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) { |
| 666 | return -1; |
| 667 | } |
| 668 | |
| 669 | iterbdrvs = bdrvs; |
| 670 | while (iterbdrvs) { |
| 671 | BlockDriverState *bs = iterbdrvs->data; |
| 672 | QEMUSnapshotInfo sn; |
| 673 | int ret = 0; |
| 674 | |
| 675 | if (devices || bdrv_all_snapshots_includes_bs(bs)) { |
| 676 | ret = bdrv_snapshot_find(bs, &sn, name); |
| 677 | } |
| 678 | if (ret < 0) { |
| 679 | if (ret == -ENOENT) { |
| 680 | return 0; |
| 681 | } else { |
| 682 | error_setg_errno(errp, errno, |
| 683 | "Could not check snapshot '%s' on '%s'", |
| 684 | name, bdrv_get_device_or_node_name(bs)); |
| 685 | return -1; |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | iterbdrvs = iterbdrvs->next; |
| 690 | } |
| 691 | |
| 692 | return 1; |
| 693 | } |
| 694 | |
| 695 | int bdrv_all_create_snapshot(QEMUSnapshotInfo *sn, |
| 696 | BlockDriverState *vm_state_bs, |
| 697 | uint64_t vm_state_size, |
| 698 | bool has_devices, strList *devices, |
| 699 | Error **errp) |
| 700 | { |
| 701 | g_autoptr(GList) bdrvs = NULL; |
| 702 | GList *iterbdrvs; |
| 703 | |
| 704 | GLOBAL_STATE_CODE(); |
| 705 | GRAPH_RDLOCK_GUARD_MAINLOOP(); |
| 706 | |
| 707 | if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) { |
| 708 | return -1; |
| 709 | } |
| 710 | |
| 711 | iterbdrvs = bdrvs; |
| 712 | while (iterbdrvs) { |
| 713 | BlockDriverState *bs = iterbdrvs->data; |
| 714 | int ret = 0; |
| 715 | |
| 716 | if (bs == vm_state_bs) { |
| 717 | sn->vm_state_size = vm_state_size; |
| 718 | ret = bdrv_snapshot_create(bs, sn); |
| 719 | } else if (devices || bdrv_all_snapshots_includes_bs(bs)) { |
| 720 | sn->vm_state_size = 0; |
| 721 | ret = bdrv_snapshot_create(bs, sn); |
| 722 | } |
| 723 | if (ret < 0) { |
| 724 | error_setg(errp, "Could not create snapshot '%s' on '%s'", |
| 725 | sn->name, bdrv_get_device_or_node_name(bs)); |
| 726 | return -1; |
| 727 | } |
| 728 | |
| 729 | iterbdrvs = iterbdrvs->next; |
| 730 | } |
| 731 | |
| 732 | return 0; |
| 733 | } |
| 734 | |
| 735 | |
| 736 | BlockDriverState *bdrv_all_find_vmstate_bs(const char *vmstate_bs, |
| 737 | bool has_devices, strList *devices, |
| 738 | Error **errp) |
| 739 | { |
| 740 | g_autoptr(GList) bdrvs = NULL; |
| 741 | GList *iterbdrvs; |
| 742 | |
| 743 | GLOBAL_STATE_CODE(); |
| 744 | GRAPH_RDLOCK_GUARD_MAINLOOP(); |
| 745 | |
| 746 | if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) { |
| 747 | return NULL; |
| 748 | } |
| 749 | |
| 750 | iterbdrvs = bdrvs; |
| 751 | while (iterbdrvs) { |
| 752 | BlockDriverState *bs = iterbdrvs->data; |
| 753 | bool found = false; |
| 754 | |
| 755 | found = (devices || bdrv_all_snapshots_includes_bs(bs)) && |
| 756 | bdrv_can_snapshot(bs); |
| 757 | |
| 758 | if (vmstate_bs) { |
| 759 | if (g_str_equal(vmstate_bs, |
| 760 | bdrv_get_node_name(bs))) { |
| 761 | if (found) { |
| 762 | return bs; |
| 763 | } else { |
| 764 | error_setg(errp, |
| 765 | "vmstate block device '%s' does not support snapshots", |
| 766 | vmstate_bs); |
| 767 | return NULL; |
| 768 | } |
| 769 | } |
| 770 | } else if (found) { |
| 771 | return bs; |
| 772 | } |
| 773 | |
| 774 | iterbdrvs = iterbdrvs->next; |
| 775 | } |
| 776 | |
| 777 | if (vmstate_bs) { |
| 778 | error_setg(errp, |
| 779 | "vmstate block device '%s' does not exist", vmstate_bs); |
| 780 | } else { |
| 781 | error_setg(errp, |
| 782 | "no block device can store vmstate for snapshot"); |
| 783 | } |
| 784 | return NULL; |
| 785 | } |