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