1 /* 2 * QEMU backup 3 * 4 * Copyright (C) 2013 Proxmox Server Solutions 5 * Copyright (c) 2019 Virtuozzo International GmbH. 6 * 7 * Authors: 8 * Dietmar Maurer (dietmar@proxmox.com) 9 * 10 * This work is licensed under the terms of the GNU GPL, version 2 or later. 11 * See the COPYING file in the top-level directory. 12 * 13 */ 14 15 #include "qemu/osdep.h" 16 17 #include "trace.h" 18 #include "block/block.h" 19 #include "block/block_int.h" 20 #include "block/blockjob_int.h" 21 #include "block/block_backup.h" 22 #include "block/block-copy.h" 23 #include "block/dirty-bitmap.h" 24 #include "qapi/error.h" 25 #include "qapi/qmp/qerror.h" 26 #include "qemu/cutils.h" 27 #include "sysemu/block-backend.h" 28 #include "qemu/bitmap.h" 29 #include "qemu/error-report.h" 30 31 #include "block/copy-before-write.h" 32 33 typedef struct BackupBlockJob { 34 BlockJob common; 35 BlockDriverState *cbw; 36 BlockDriverState *source_bs; 37 BlockDriverState *target_bs; 38 39 BdrvDirtyBitmap *sync_bitmap; 40 41 MirrorSyncMode sync_mode; 42 BitmapSyncMode bitmap_mode; 43 BlockdevOnError on_source_error; 44 BlockdevOnError on_target_error; 45 uint64_t len; 46 int64_t cluster_size; 47 BackupPerf perf; 48 49 BlockCopyState *bcs; 50 51 bool wait; 52 BlockCopyCallState *bg_bcs_call; 53 } BackupBlockJob; 54 55 static const BlockJobDriver backup_job_driver; 56 57 static void backup_cleanup_sync_bitmap(BackupBlockJob *job, int ret) 58 { 59 BdrvDirtyBitmap *bm; 60 bool sync = (((ret == 0) || (job->bitmap_mode == BITMAP_SYNC_MODE_ALWAYS)) \ 61 && (job->bitmap_mode != BITMAP_SYNC_MODE_NEVER)); 62 63 if (sync) { 64 /* 65 * We succeeded, or we always intended to sync the bitmap. 66 * Delete this bitmap and install the child. 67 */ 68 bm = bdrv_dirty_bitmap_abdicate(job->sync_bitmap, NULL); 69 } else { 70 /* 71 * We failed, or we never intended to sync the bitmap anyway. 72 * Merge the successor back into the parent, keeping all data. 73 */ 74 bm = bdrv_reclaim_dirty_bitmap(job->sync_bitmap, NULL); 75 } 76 77 assert(bm); 78 79 if (ret < 0 && job->bitmap_mode == BITMAP_SYNC_MODE_ALWAYS) { 80 /* If we failed and synced, merge in the bits we didn't copy: */ 81 bdrv_dirty_bitmap_merge_internal(bm, block_copy_dirty_bitmap(job->bcs), 82 NULL, true); 83 } 84 } 85 86 static void backup_commit(Job *job) 87 { 88 BackupBlockJob *s = container_of(job, BackupBlockJob, common.job); 89 if (s->sync_bitmap) { 90 backup_cleanup_sync_bitmap(s, 0); 91 } 92 } 93 94 static void backup_abort(Job *job) 95 { 96 BackupBlockJob *s = container_of(job, BackupBlockJob, common.job); 97 if (s->sync_bitmap) { 98 backup_cleanup_sync_bitmap(s, -1); 99 } 100 } 101 102 static void backup_clean(Job *job) 103 { 104 BackupBlockJob *s = container_of(job, BackupBlockJob, common.job); 105 block_job_remove_all_bdrv(&s->common); 106 bdrv_cbw_drop(s->cbw); 107 } 108 109 void backup_do_checkpoint(BlockJob *job, Error **errp) 110 { 111 BackupBlockJob *backup_job = container_of(job, BackupBlockJob, common); 112 113 assert(block_job_driver(job) == &backup_job_driver); 114 115 if (backup_job->sync_mode != MIRROR_SYNC_MODE_NONE) { 116 error_setg(errp, "The backup job only supports block checkpoint in" 117 " sync=none mode"); 118 return; 119 } 120 121 bdrv_set_dirty_bitmap(block_copy_dirty_bitmap(backup_job->bcs), 0, 122 backup_job->len); 123 } 124 125 static BlockErrorAction backup_error_action(BackupBlockJob *job, 126 bool read, int error) 127 { 128 if (read) { 129 return block_job_error_action(&job->common, job->on_source_error, 130 true, error); 131 } else { 132 return block_job_error_action(&job->common, job->on_target_error, 133 false, error); 134 } 135 } 136 137 static void coroutine_fn backup_block_copy_callback(void *opaque) 138 { 139 BackupBlockJob *s = opaque; 140 141 if (s->wait) { 142 s->wait = false; 143 aio_co_wake(s->common.job.co); 144 } else { 145 job_enter(&s->common.job); 146 } 147 } 148 149 static int coroutine_fn backup_loop(BackupBlockJob *job) 150 { 151 BlockCopyCallState *s = NULL; 152 int ret = 0; 153 bool error_is_read; 154 BlockErrorAction act; 155 156 while (true) { /* retry loop */ 157 job->bg_bcs_call = s = block_copy_async(job->bcs, 0, 158 QEMU_ALIGN_UP(job->len, job->cluster_size), 159 job->perf.max_workers, job->perf.max_chunk, 160 backup_block_copy_callback, job); 161 162 while (!block_copy_call_finished(s) && 163 !job_is_cancelled(&job->common.job)) 164 { 165 job_yield(&job->common.job); 166 } 167 168 if (!block_copy_call_finished(s)) { 169 assert(job_is_cancelled(&job->common.job)); 170 /* 171 * Note that we can't use job_yield() here, as it doesn't work for 172 * cancelled job. 173 */ 174 block_copy_call_cancel(s); 175 job->wait = true; 176 qemu_coroutine_yield(); 177 assert(block_copy_call_finished(s)); 178 ret = 0; 179 goto out; 180 } 181 182 if (job_is_cancelled(&job->common.job) || 183 block_copy_call_succeeded(s)) 184 { 185 ret = 0; 186 goto out; 187 } 188 189 if (block_copy_call_cancelled(s)) { 190 /* 191 * Job is not cancelled but only block-copy call. This is possible 192 * after job pause. Now the pause is finished, start new block-copy 193 * iteration. 194 */ 195 block_copy_call_free(s); 196 continue; 197 } 198 199 /* The only remaining case is failed block-copy call. */ 200 assert(block_copy_call_failed(s)); 201 202 ret = block_copy_call_status(s, &error_is_read); 203 act = backup_error_action(job, error_is_read, -ret); 204 switch (act) { 205 case BLOCK_ERROR_ACTION_REPORT: 206 goto out; 207 case BLOCK_ERROR_ACTION_STOP: 208 /* 209 * Go to pause prior to starting new block-copy call on the next 210 * iteration. 211 */ 212 job_pause_point(&job->common.job); 213 break; 214 case BLOCK_ERROR_ACTION_IGNORE: 215 /* Proceed to new block-copy call to retry. */ 216 break; 217 default: 218 abort(); 219 } 220 221 block_copy_call_free(s); 222 } 223 224 out: 225 block_copy_call_free(s); 226 job->bg_bcs_call = NULL; 227 return ret; 228 } 229 230 static void backup_init_bcs_bitmap(BackupBlockJob *job) 231 { 232 uint64_t estimate; 233 BdrvDirtyBitmap *bcs_bitmap = block_copy_dirty_bitmap(job->bcs); 234 235 if (job->sync_mode == MIRROR_SYNC_MODE_BITMAP) { 236 bdrv_clear_dirty_bitmap(bcs_bitmap, NULL); 237 bdrv_dirty_bitmap_merge_internal(bcs_bitmap, job->sync_bitmap, NULL, 238 true); 239 } else if (job->sync_mode == MIRROR_SYNC_MODE_TOP) { 240 /* 241 * We can't hog the coroutine to initialize this thoroughly. 242 * Set a flag and resume work when we are able to yield safely. 243 */ 244 block_copy_set_skip_unallocated(job->bcs, true); 245 } 246 247 estimate = bdrv_get_dirty_count(bcs_bitmap); 248 job_progress_set_remaining(&job->common.job, estimate); 249 } 250 251 static int coroutine_fn backup_run(Job *job, Error **errp) 252 { 253 BackupBlockJob *s = container_of(job, BackupBlockJob, common.job); 254 int ret; 255 256 backup_init_bcs_bitmap(s); 257 258 if (s->sync_mode == MIRROR_SYNC_MODE_TOP) { 259 int64_t offset = 0; 260 int64_t count; 261 262 for (offset = 0; offset < s->len; ) { 263 if (job_is_cancelled(job)) { 264 return -ECANCELED; 265 } 266 267 job_pause_point(job); 268 269 if (job_is_cancelled(job)) { 270 return -ECANCELED; 271 } 272 273 ret = block_copy_reset_unallocated(s->bcs, offset, &count); 274 if (ret < 0) { 275 return ret; 276 } 277 278 offset += count; 279 } 280 block_copy_set_skip_unallocated(s->bcs, false); 281 } 282 283 if (s->sync_mode == MIRROR_SYNC_MODE_NONE) { 284 /* 285 * All bits are set in bcs bitmap to allow any cluster to be copied. 286 * This does not actually require them to be copied. 287 */ 288 while (!job_is_cancelled(job)) { 289 /* 290 * Yield until the job is cancelled. We just let our before_write 291 * notify callback service CoW requests. 292 */ 293 job_yield(job); 294 } 295 } else { 296 return backup_loop(s); 297 } 298 299 return 0; 300 } 301 302 static void coroutine_fn backup_pause(Job *job) 303 { 304 BackupBlockJob *s = container_of(job, BackupBlockJob, common.job); 305 306 if (s->bg_bcs_call && !block_copy_call_finished(s->bg_bcs_call)) { 307 block_copy_call_cancel(s->bg_bcs_call); 308 s->wait = true; 309 qemu_coroutine_yield(); 310 } 311 } 312 313 static void backup_set_speed(BlockJob *job, int64_t speed) 314 { 315 BackupBlockJob *s = container_of(job, BackupBlockJob, common); 316 317 /* 318 * block_job_set_speed() is called first from block_job_create(), when we 319 * don't yet have s->bcs. 320 */ 321 if (s->bcs) { 322 block_copy_set_speed(s->bcs, speed); 323 if (s->bg_bcs_call) { 324 block_copy_kick(s->bg_bcs_call); 325 } 326 } 327 } 328 329 static bool backup_cancel(Job *job, bool force) 330 { 331 BackupBlockJob *s = container_of(job, BackupBlockJob, common.job); 332 333 bdrv_cancel_in_flight(s->target_bs); 334 return true; 335 } 336 337 static const BlockJobDriver backup_job_driver = { 338 .job_driver = { 339 .instance_size = sizeof(BackupBlockJob), 340 .job_type = JOB_TYPE_BACKUP, 341 .free = block_job_free, 342 .user_resume = block_job_user_resume, 343 .run = backup_run, 344 .commit = backup_commit, 345 .abort = backup_abort, 346 .clean = backup_clean, 347 .pause = backup_pause, 348 .cancel = backup_cancel, 349 }, 350 .set_speed = backup_set_speed, 351 }; 352 353 BlockJob *backup_job_create(const char *job_id, BlockDriverState *bs, 354 BlockDriverState *target, int64_t speed, 355 MirrorSyncMode sync_mode, BdrvDirtyBitmap *sync_bitmap, 356 BitmapSyncMode bitmap_mode, 357 bool compress, 358 const char *filter_node_name, 359 BackupPerf *perf, 360 BlockdevOnError on_source_error, 361 BlockdevOnError on_target_error, 362 int creation_flags, 363 BlockCompletionFunc *cb, void *opaque, 364 JobTxn *txn, Error **errp) 365 { 366 int64_t len, target_len; 367 BackupBlockJob *job = NULL; 368 int64_t cluster_size; 369 BlockDriverState *cbw = NULL; 370 BlockCopyState *bcs = NULL; 371 372 assert(bs); 373 assert(target); 374 GLOBAL_STATE_CODE(); 375 376 /* QMP interface protects us from these cases */ 377 assert(sync_mode != MIRROR_SYNC_MODE_INCREMENTAL); 378 assert(sync_bitmap || sync_mode != MIRROR_SYNC_MODE_BITMAP); 379 380 if (bs == target) { 381 error_setg(errp, "Source and target cannot be the same"); 382 return NULL; 383 } 384 385 if (!bdrv_is_inserted(bs)) { 386 error_setg(errp, "Device is not inserted: %s", 387 bdrv_get_device_name(bs)); 388 return NULL; 389 } 390 391 if (!bdrv_is_inserted(target)) { 392 error_setg(errp, "Device is not inserted: %s", 393 bdrv_get_device_name(target)); 394 return NULL; 395 } 396 397 if (compress && !bdrv_supports_compressed_writes(target)) { 398 error_setg(errp, "Compression is not supported for this drive %s", 399 bdrv_get_device_name(target)); 400 return NULL; 401 } 402 403 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) { 404 return NULL; 405 } 406 407 if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_BACKUP_TARGET, errp)) { 408 return NULL; 409 } 410 411 if (perf->max_workers < 1 || perf->max_workers > INT_MAX) { 412 error_setg(errp, "max-workers must be between 1 and %d", INT_MAX); 413 return NULL; 414 } 415 416 if (perf->max_chunk < 0) { 417 error_setg(errp, "max-chunk must be zero (which means no limit) or " 418 "positive"); 419 return NULL; 420 } 421 422 if (sync_bitmap) { 423 /* If we need to write to this bitmap, check that we can: */ 424 if (bitmap_mode != BITMAP_SYNC_MODE_NEVER && 425 bdrv_dirty_bitmap_check(sync_bitmap, BDRV_BITMAP_DEFAULT, errp)) { 426 return NULL; 427 } 428 429 /* Create a new bitmap, and freeze/disable this one. */ 430 if (bdrv_dirty_bitmap_create_successor(sync_bitmap, errp) < 0) { 431 return NULL; 432 } 433 } 434 435 len = bdrv_getlength(bs); 436 if (len < 0) { 437 error_setg_errno(errp, -len, "Unable to get length for '%s'", 438 bdrv_get_device_or_node_name(bs)); 439 goto error; 440 } 441 442 target_len = bdrv_getlength(target); 443 if (target_len < 0) { 444 error_setg_errno(errp, -target_len, "Unable to get length for '%s'", 445 bdrv_get_device_or_node_name(bs)); 446 goto error; 447 } 448 449 if (target_len != len) { 450 error_setg(errp, "Source and target image have different sizes"); 451 goto error; 452 } 453 454 cbw = bdrv_cbw_append(bs, target, filter_node_name, &bcs, errp); 455 if (!cbw) { 456 goto error; 457 } 458 459 cluster_size = block_copy_cluster_size(bcs); 460 461 if (perf->max_chunk && perf->max_chunk < cluster_size) { 462 error_setg(errp, "Required max-chunk (%" PRIi64 ") is less than backup " 463 "cluster size (%" PRIi64 ")", perf->max_chunk, cluster_size); 464 goto error; 465 } 466 467 /* job->len is fixed, so we can't allow resize */ 468 job = block_job_create(job_id, &backup_job_driver, txn, cbw, 469 0, BLK_PERM_ALL, 470 speed, creation_flags, cb, opaque, errp); 471 if (!job) { 472 goto error; 473 } 474 475 job->cbw = cbw; 476 job->source_bs = bs; 477 job->target_bs = target; 478 job->on_source_error = on_source_error; 479 job->on_target_error = on_target_error; 480 job->sync_mode = sync_mode; 481 job->sync_bitmap = sync_bitmap; 482 job->bitmap_mode = bitmap_mode; 483 job->bcs = bcs; 484 job->cluster_size = cluster_size; 485 job->len = len; 486 job->perf = *perf; 487 488 block_copy_set_copy_opts(bcs, perf->use_copy_range, compress); 489 block_copy_set_progress_meter(bcs, &job->common.job.progress); 490 block_copy_set_speed(bcs, speed); 491 492 /* Required permissions are taken by copy-before-write filter target */ 493 block_job_add_bdrv(&job->common, "target", target, 0, BLK_PERM_ALL, 494 &error_abort); 495 496 return &job->common; 497 498 error: 499 if (sync_bitmap) { 500 bdrv_reclaim_dirty_bitmap(sync_bitmap, NULL); 501 } 502 if (cbw) { 503 bdrv_cbw_drop(cbw); 504 } 505 506 return NULL; 507 } 508