1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. 23 * Copyright 2016 Gary Mills 24 * Copyright (c) 2011, 2016 by Delphix. All rights reserved. 25 */ 26 27 #include <sys/dsl_scan.h> 28 #include <sys/dsl_pool.h> 29 #include <sys/dsl_dataset.h> 30 #include <sys/dsl_prop.h> 31 #include <sys/dsl_dir.h> 32 #include <sys/dsl_synctask.h> 33 #include <sys/dnode.h> 34 #include <sys/dmu_tx.h> 35 #include <sys/dmu_objset.h> 36 #include <sys/arc.h> 37 #include <sys/zap.h> 38 #include <sys/zio.h> 39 #include <sys/zfs_context.h> 40 #include <sys/fs/zfs.h> 41 #include <sys/zfs_znode.h> 42 #include <sys/spa_impl.h> 43 #include <sys/vdev_impl.h> 44 #include <sys/zil_impl.h> 45 #include <sys/zio_checksum.h> 46 #include <sys/ddt.h> 47 #include <sys/sa.h> 48 #include <sys/sa_impl.h> 49 #include <sys/zfeature.h> 50 #include <sys/abd.h> 51 #ifdef _KERNEL 52 #include <sys/zfs_vfsops.h> 53 #endif 54 55 typedef int (scan_cb_t)(dsl_pool_t *, const blkptr_t *, 56 const zbookmark_phys_t *); 57 58 static scan_cb_t dsl_scan_scrub_cb; 59 static void dsl_scan_cancel_sync(void *, dmu_tx_t *); 60 static void dsl_scan_sync_state(dsl_scan_t *, dmu_tx_t *); 61 static boolean_t dsl_scan_restarting(dsl_scan_t *, dmu_tx_t *); 62 63 int zfs_top_maxinflight = 32; /* maximum I/Os per top-level */ 64 int zfs_resilver_delay = 2; /* number of ticks to delay resilver */ 65 int zfs_scrub_delay = 4; /* number of ticks to delay scrub */ 66 int zfs_scan_idle = 50; /* idle window in clock ticks */ 67 68 int zfs_scan_min_time_ms = 1000; /* min millisecs to scrub per txg */ 69 int zfs_free_min_time_ms = 1000; /* min millisecs to free per txg */ 70 int zfs_resilver_min_time_ms = 3000; /* min millisecs to resilver per txg */ 71 boolean_t zfs_no_scrub_io = B_FALSE; /* set to disable scrub i/o */ 72 boolean_t zfs_no_scrub_prefetch = B_FALSE; /* set to disable scrub prefetch */ 73 enum ddt_class zfs_scrub_ddt_class_max = DDT_CLASS_DUPLICATE; 74 int dsl_scan_delay_completion = B_FALSE; /* set to delay scan completion */ 75 /* max number of blocks to free in a single TXG */ 76 uint64_t zfs_free_max_blocks = UINT64_MAX; 77 78 #define DSL_SCAN_IS_SCRUB_RESILVER(scn) \ 79 ((scn)->scn_phys.scn_func == POOL_SCAN_SCRUB || \ 80 (scn)->scn_phys.scn_func == POOL_SCAN_RESILVER) 81 82 extern int zfs_txg_timeout; 83 84 /* 85 * Enable/disable the processing of the free_bpobj object. 86 */ 87 boolean_t zfs_free_bpobj_enabled = B_TRUE; 88 89 /* the order has to match pool_scan_type */ 90 static scan_cb_t *scan_funcs[POOL_SCAN_FUNCS] = { 91 NULL, 92 dsl_scan_scrub_cb, /* POOL_SCAN_SCRUB */ 93 dsl_scan_scrub_cb, /* POOL_SCAN_RESILVER */ 94 }; 95 96 int 97 dsl_scan_init(dsl_pool_t *dp, uint64_t txg) 98 { 99 int err; 100 dsl_scan_t *scn; 101 spa_t *spa = dp->dp_spa; 102 uint64_t f; 103 104 scn = dp->dp_scan = kmem_zalloc(sizeof (dsl_scan_t), KM_SLEEP); 105 scn->scn_dp = dp; 106 107 /* 108 * It's possible that we're resuming a scan after a reboot so 109 * make sure that the scan_async_destroying flag is initialized 110 * appropriately. 111 */ 112 ASSERT(!scn->scn_async_destroying); 113 scn->scn_async_destroying = spa_feature_is_active(dp->dp_spa, 114 SPA_FEATURE_ASYNC_DESTROY); 115 116 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 117 "scrub_func", sizeof (uint64_t), 1, &f); 118 if (err == 0) { 119 /* 120 * There was an old-style scrub in progress. Restart a 121 * new-style scrub from the beginning. 122 */ 123 scn->scn_restart_txg = txg; 124 zfs_dbgmsg("old-style scrub was in progress; " 125 "restarting new-style scrub in txg %llu", 126 scn->scn_restart_txg); 127 128 /* 129 * Load the queue obj from the old location so that it 130 * can be freed by dsl_scan_done(). 131 */ 132 (void) zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 133 "scrub_queue", sizeof (uint64_t), 1, 134 &scn->scn_phys.scn_queue_obj); 135 } else { 136 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 137 DMU_POOL_SCAN, sizeof (uint64_t), SCAN_PHYS_NUMINTS, 138 &scn->scn_phys); 139 if (err == ENOENT) 140 return (0); 141 else if (err) 142 return (err); 143 144 if (scn->scn_phys.scn_state == DSS_SCANNING && 145 spa_prev_software_version(dp->dp_spa) < SPA_VERSION_SCAN) { 146 /* 147 * A new-type scrub was in progress on an old 148 * pool, and the pool was accessed by old 149 * software. Restart from the beginning, since 150 * the old software may have changed the pool in 151 * the meantime. 152 */ 153 scn->scn_restart_txg = txg; 154 zfs_dbgmsg("new-style scrub was modified " 155 "by old software; restarting in txg %llu", 156 scn->scn_restart_txg); 157 } 158 } 159 160 spa_scan_stat_init(spa); 161 return (0); 162 } 163 164 void 165 dsl_scan_fini(dsl_pool_t *dp) 166 { 167 if (dp->dp_scan) { 168 kmem_free(dp->dp_scan, sizeof (dsl_scan_t)); 169 dp->dp_scan = NULL; 170 } 171 } 172 173 /* ARGSUSED */ 174 static int 175 dsl_scan_setup_check(void *arg, dmu_tx_t *tx) 176 { 177 dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan; 178 179 if (scn->scn_phys.scn_state == DSS_SCANNING) 180 return (SET_ERROR(EBUSY)); 181 182 return (0); 183 } 184 185 static void 186 dsl_scan_setup_sync(void *arg, dmu_tx_t *tx) 187 { 188 dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan; 189 pool_scan_func_t *funcp = arg; 190 dmu_object_type_t ot = 0; 191 dsl_pool_t *dp = scn->scn_dp; 192 spa_t *spa = dp->dp_spa; 193 194 ASSERT(scn->scn_phys.scn_state != DSS_SCANNING); 195 ASSERT(*funcp > POOL_SCAN_NONE && *funcp < POOL_SCAN_FUNCS); 196 bzero(&scn->scn_phys, sizeof (scn->scn_phys)); 197 scn->scn_phys.scn_func = *funcp; 198 scn->scn_phys.scn_state = DSS_SCANNING; 199 scn->scn_phys.scn_min_txg = 0; 200 scn->scn_phys.scn_max_txg = tx->tx_txg; 201 scn->scn_phys.scn_ddt_class_max = DDT_CLASSES - 1; /* the entire DDT */ 202 scn->scn_phys.scn_start_time = gethrestime_sec(); 203 scn->scn_phys.scn_errors = 0; 204 scn->scn_phys.scn_to_examine = spa->spa_root_vdev->vdev_stat.vs_alloc; 205 scn->scn_restart_txg = 0; 206 scn->scn_done_txg = 0; 207 spa_scan_stat_init(spa); 208 209 if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) { 210 scn->scn_phys.scn_ddt_class_max = zfs_scrub_ddt_class_max; 211 212 /* rewrite all disk labels */ 213 vdev_config_dirty(spa->spa_root_vdev); 214 215 if (vdev_resilver_needed(spa->spa_root_vdev, 216 &scn->scn_phys.scn_min_txg, &scn->scn_phys.scn_max_txg)) { 217 spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_START); 218 } else { 219 spa_event_notify(spa, NULL, ESC_ZFS_SCRUB_START); 220 } 221 222 spa->spa_scrub_started = B_TRUE; 223 /* 224 * If this is an incremental scrub, limit the DDT scrub phase 225 * to just the auto-ditto class (for correctness); the rest 226 * of the scrub should go faster using top-down pruning. 227 */ 228 if (scn->scn_phys.scn_min_txg > TXG_INITIAL) 229 scn->scn_phys.scn_ddt_class_max = DDT_CLASS_DITTO; 230 231 } 232 233 /* back to the generic stuff */ 234 235 if (dp->dp_blkstats == NULL) { 236 dp->dp_blkstats = 237 kmem_alloc(sizeof (zfs_all_blkstats_t), KM_SLEEP); 238 } 239 bzero(dp->dp_blkstats, sizeof (zfs_all_blkstats_t)); 240 241 if (spa_version(spa) < SPA_VERSION_DSL_SCRUB) 242 ot = DMU_OT_ZAP_OTHER; 243 244 scn->scn_phys.scn_queue_obj = zap_create(dp->dp_meta_objset, 245 ot ? ot : DMU_OT_SCAN_QUEUE, DMU_OT_NONE, 0, tx); 246 247 dsl_scan_sync_state(scn, tx); 248 249 spa_history_log_internal(spa, "scan setup", tx, 250 "func=%u mintxg=%llu maxtxg=%llu", 251 *funcp, scn->scn_phys.scn_min_txg, scn->scn_phys.scn_max_txg); 252 } 253 254 /* ARGSUSED */ 255 static void 256 dsl_scan_done(dsl_scan_t *scn, boolean_t complete, dmu_tx_t *tx) 257 { 258 static const char *old_names[] = { 259 "scrub_bookmark", 260 "scrub_ddt_bookmark", 261 "scrub_ddt_class_max", 262 "scrub_queue", 263 "scrub_min_txg", 264 "scrub_max_txg", 265 "scrub_func", 266 "scrub_errors", 267 NULL 268 }; 269 270 dsl_pool_t *dp = scn->scn_dp; 271 spa_t *spa = dp->dp_spa; 272 int i; 273 274 /* Remove any remnants of an old-style scrub. */ 275 for (i = 0; old_names[i]; i++) { 276 (void) zap_remove(dp->dp_meta_objset, 277 DMU_POOL_DIRECTORY_OBJECT, old_names[i], tx); 278 } 279 280 if (scn->scn_phys.scn_queue_obj != 0) { 281 VERIFY(0 == dmu_object_free(dp->dp_meta_objset, 282 scn->scn_phys.scn_queue_obj, tx)); 283 scn->scn_phys.scn_queue_obj = 0; 284 } 285 286 /* 287 * If we were "restarted" from a stopped state, don't bother 288 * with anything else. 289 */ 290 if (scn->scn_phys.scn_state != DSS_SCANNING) 291 return; 292 293 if (complete) 294 scn->scn_phys.scn_state = DSS_FINISHED; 295 else 296 scn->scn_phys.scn_state = DSS_CANCELED; 297 298 if (dsl_scan_restarting(scn, tx)) 299 spa_history_log_internal(spa, "scan aborted, restarting", tx, 300 "errors=%llu", spa_get_errlog_size(spa)); 301 else if (!complete) 302 spa_history_log_internal(spa, "scan cancelled", tx, 303 "errors=%llu", spa_get_errlog_size(spa)); 304 else 305 spa_history_log_internal(spa, "scan done", tx, 306 "errors=%llu", spa_get_errlog_size(spa)); 307 308 if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) { 309 mutex_enter(&spa->spa_scrub_lock); 310 while (spa->spa_scrub_inflight > 0) { 311 cv_wait(&spa->spa_scrub_io_cv, 312 &spa->spa_scrub_lock); 313 } 314 mutex_exit(&spa->spa_scrub_lock); 315 spa->spa_scrub_started = B_FALSE; 316 spa->spa_scrub_active = B_FALSE; 317 318 /* 319 * If the scrub/resilver completed, update all DTLs to 320 * reflect this. Whether it succeeded or not, vacate 321 * all temporary scrub DTLs. 322 */ 323 vdev_dtl_reassess(spa->spa_root_vdev, tx->tx_txg, 324 complete ? scn->scn_phys.scn_max_txg : 0, B_TRUE); 325 if (complete) { 326 spa_event_notify(spa, NULL, scn->scn_phys.scn_min_txg ? 327 ESC_ZFS_RESILVER_FINISH : ESC_ZFS_SCRUB_FINISH); 328 } 329 spa_errlog_rotate(spa); 330 331 /* 332 * We may have finished replacing a device. 333 * Let the async thread assess this and handle the detach. 334 */ 335 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 336 } 337 338 scn->scn_phys.scn_end_time = gethrestime_sec(); 339 } 340 341 /* ARGSUSED */ 342 static int 343 dsl_scan_cancel_check(void *arg, dmu_tx_t *tx) 344 { 345 dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan; 346 347 if (scn->scn_phys.scn_state != DSS_SCANNING) 348 return (SET_ERROR(ENOENT)); 349 return (0); 350 } 351 352 /* ARGSUSED */ 353 static void 354 dsl_scan_cancel_sync(void *arg, dmu_tx_t *tx) 355 { 356 dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan; 357 358 dsl_scan_done(scn, B_FALSE, tx); 359 dsl_scan_sync_state(scn, tx); 360 } 361 362 int 363 dsl_scan_cancel(dsl_pool_t *dp) 364 { 365 return (dsl_sync_task(spa_name(dp->dp_spa), dsl_scan_cancel_check, 366 dsl_scan_cancel_sync, NULL, 3, ZFS_SPACE_CHECK_RESERVED)); 367 } 368 369 static void dsl_scan_visitbp(blkptr_t *bp, const zbookmark_phys_t *zb, 370 dnode_phys_t *dnp, dsl_dataset_t *ds, dsl_scan_t *scn, 371 dmu_objset_type_t ostype, dmu_tx_t *tx); 372 static void dsl_scan_visitdnode(dsl_scan_t *, dsl_dataset_t *ds, 373 dmu_objset_type_t ostype, 374 dnode_phys_t *dnp, uint64_t object, dmu_tx_t *tx); 375 376 void 377 dsl_free(dsl_pool_t *dp, uint64_t txg, const blkptr_t *bp) 378 { 379 zio_free(dp->dp_spa, txg, bp); 380 } 381 382 void 383 dsl_free_sync(zio_t *pio, dsl_pool_t *dp, uint64_t txg, const blkptr_t *bpp) 384 { 385 ASSERT(dsl_pool_sync_context(dp)); 386 zio_nowait(zio_free_sync(pio, dp->dp_spa, txg, bpp, pio->io_flags)); 387 } 388 389 static uint64_t 390 dsl_scan_ds_maxtxg(dsl_dataset_t *ds) 391 { 392 uint64_t smt = ds->ds_dir->dd_pool->dp_scan->scn_phys.scn_max_txg; 393 if (ds->ds_is_snapshot) 394 return (MIN(smt, dsl_dataset_phys(ds)->ds_creation_txg)); 395 return (smt); 396 } 397 398 static void 399 dsl_scan_sync_state(dsl_scan_t *scn, dmu_tx_t *tx) 400 { 401 VERIFY0(zap_update(scn->scn_dp->dp_meta_objset, 402 DMU_POOL_DIRECTORY_OBJECT, 403 DMU_POOL_SCAN, sizeof (uint64_t), SCAN_PHYS_NUMINTS, 404 &scn->scn_phys, tx)); 405 } 406 407 extern int zfs_vdev_async_write_active_min_dirty_percent; 408 409 static boolean_t 410 dsl_scan_check_pause(dsl_scan_t *scn, const zbookmark_phys_t *zb) 411 { 412 /* we never skip user/group accounting objects */ 413 if (zb && (int64_t)zb->zb_object < 0) 414 return (B_FALSE); 415 416 if (scn->scn_pausing) 417 return (B_TRUE); /* we're already pausing */ 418 419 if (!ZB_IS_ZERO(&scn->scn_phys.scn_bookmark)) 420 return (B_FALSE); /* we're resuming */ 421 422 /* We only know how to resume from level-0 blocks. */ 423 if (zb && zb->zb_level != 0) 424 return (B_FALSE); 425 426 /* 427 * We pause if: 428 * - we have scanned for the maximum time: an entire txg 429 * timeout (default 5 sec) 430 * or 431 * - we have scanned for at least the minimum time (default 1 sec 432 * for scrub, 3 sec for resilver), and either we have sufficient 433 * dirty data that we are starting to write more quickly 434 * (default 30%), or someone is explicitly waiting for this txg 435 * to complete. 436 * or 437 * - the spa is shutting down because this pool is being exported 438 * or the machine is rebooting. 439 */ 440 int mintime = (scn->scn_phys.scn_func == POOL_SCAN_RESILVER) ? 441 zfs_resilver_min_time_ms : zfs_scan_min_time_ms; 442 uint64_t elapsed_nanosecs = gethrtime() - scn->scn_sync_start_time; 443 int dirty_pct = scn->scn_dp->dp_dirty_total * 100 / zfs_dirty_data_max; 444 if (elapsed_nanosecs / NANOSEC >= zfs_txg_timeout || 445 (NSEC2MSEC(elapsed_nanosecs) > mintime && 446 (txg_sync_waiting(scn->scn_dp) || 447 dirty_pct >= zfs_vdev_async_write_active_min_dirty_percent)) || 448 spa_shutting_down(scn->scn_dp->dp_spa)) { 449 if (zb) { 450 dprintf("pausing at bookmark %llx/%llx/%llx/%llx\n", 451 (longlong_t)zb->zb_objset, 452 (longlong_t)zb->zb_object, 453 (longlong_t)zb->zb_level, 454 (longlong_t)zb->zb_blkid); 455 scn->scn_phys.scn_bookmark = *zb; 456 } 457 dprintf("pausing at DDT bookmark %llx/%llx/%llx/%llx\n", 458 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_class, 459 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_type, 460 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_checksum, 461 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_cursor); 462 scn->scn_pausing = B_TRUE; 463 return (B_TRUE); 464 } 465 return (B_FALSE); 466 } 467 468 typedef struct zil_scan_arg { 469 dsl_pool_t *zsa_dp; 470 zil_header_t *zsa_zh; 471 } zil_scan_arg_t; 472 473 /* ARGSUSED */ 474 static int 475 dsl_scan_zil_block(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg) 476 { 477 zil_scan_arg_t *zsa = arg; 478 dsl_pool_t *dp = zsa->zsa_dp; 479 dsl_scan_t *scn = dp->dp_scan; 480 zil_header_t *zh = zsa->zsa_zh; 481 zbookmark_phys_t zb; 482 483 if (BP_IS_HOLE(bp) || bp->blk_birth <= scn->scn_phys.scn_cur_min_txg) 484 return (0); 485 486 /* 487 * One block ("stubby") can be allocated a long time ago; we 488 * want to visit that one because it has been allocated 489 * (on-disk) even if it hasn't been claimed (even though for 490 * scrub there's nothing to do to it). 491 */ 492 if (claim_txg == 0 && bp->blk_birth >= spa_first_txg(dp->dp_spa)) 493 return (0); 494 495 SET_BOOKMARK(&zb, zh->zh_log.blk_cksum.zc_word[ZIL_ZC_OBJSET], 496 ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, bp->blk_cksum.zc_word[ZIL_ZC_SEQ]); 497 498 VERIFY(0 == scan_funcs[scn->scn_phys.scn_func](dp, bp, &zb)); 499 return (0); 500 } 501 502 /* ARGSUSED */ 503 static int 504 dsl_scan_zil_record(zilog_t *zilog, lr_t *lrc, void *arg, uint64_t claim_txg) 505 { 506 if (lrc->lrc_txtype == TX_WRITE) { 507 zil_scan_arg_t *zsa = arg; 508 dsl_pool_t *dp = zsa->zsa_dp; 509 dsl_scan_t *scn = dp->dp_scan; 510 zil_header_t *zh = zsa->zsa_zh; 511 lr_write_t *lr = (lr_write_t *)lrc; 512 blkptr_t *bp = &lr->lr_blkptr; 513 zbookmark_phys_t zb; 514 515 if (BP_IS_HOLE(bp) || 516 bp->blk_birth <= scn->scn_phys.scn_cur_min_txg) 517 return (0); 518 519 /* 520 * birth can be < claim_txg if this record's txg is 521 * already txg sync'ed (but this log block contains 522 * other records that are not synced) 523 */ 524 if (claim_txg == 0 || bp->blk_birth < claim_txg) 525 return (0); 526 527 SET_BOOKMARK(&zb, zh->zh_log.blk_cksum.zc_word[ZIL_ZC_OBJSET], 528 lr->lr_foid, ZB_ZIL_LEVEL, 529 lr->lr_offset / BP_GET_LSIZE(bp)); 530 531 VERIFY(0 == scan_funcs[scn->scn_phys.scn_func](dp, bp, &zb)); 532 } 533 return (0); 534 } 535 536 static void 537 dsl_scan_zil(dsl_pool_t *dp, zil_header_t *zh) 538 { 539 uint64_t claim_txg = zh->zh_claim_txg; 540 zil_scan_arg_t zsa = { dp, zh }; 541 zilog_t *zilog; 542 543 /* 544 * We only want to visit blocks that have been claimed but not yet 545 * replayed (or, in read-only mode, blocks that *would* be claimed). 546 */ 547 if (claim_txg == 0 && spa_writeable(dp->dp_spa)) 548 return; 549 550 zilog = zil_alloc(dp->dp_meta_objset, zh); 551 552 (void) zil_parse(zilog, dsl_scan_zil_block, dsl_scan_zil_record, &zsa, 553 claim_txg); 554 555 zil_free(zilog); 556 } 557 558 /* ARGSUSED */ 559 static void 560 dsl_scan_prefetch(dsl_scan_t *scn, arc_buf_t *buf, blkptr_t *bp, 561 uint64_t objset, uint64_t object, uint64_t blkid) 562 { 563 zbookmark_phys_t czb; 564 arc_flags_t flags = ARC_FLAG_NOWAIT | ARC_FLAG_PREFETCH; 565 566 if (zfs_no_scrub_prefetch) 567 return; 568 569 if (BP_IS_HOLE(bp) || bp->blk_birth <= scn->scn_phys.scn_min_txg || 570 (BP_GET_LEVEL(bp) == 0 && BP_GET_TYPE(bp) != DMU_OT_DNODE)) 571 return; 572 573 SET_BOOKMARK(&czb, objset, object, BP_GET_LEVEL(bp), blkid); 574 575 (void) arc_read(scn->scn_zio_root, scn->scn_dp->dp_spa, bp, 576 NULL, NULL, ZIO_PRIORITY_ASYNC_READ, 577 ZIO_FLAG_CANFAIL | ZIO_FLAG_SCAN_THREAD, &flags, &czb); 578 } 579 580 static boolean_t 581 dsl_scan_check_resume(dsl_scan_t *scn, const dnode_phys_t *dnp, 582 const zbookmark_phys_t *zb) 583 { 584 /* 585 * We never skip over user/group accounting objects (obj<0) 586 */ 587 if (!ZB_IS_ZERO(&scn->scn_phys.scn_bookmark) && 588 (int64_t)zb->zb_object >= 0) { 589 /* 590 * If we already visited this bp & everything below (in 591 * a prior txg sync), don't bother doing it again. 592 */ 593 if (zbookmark_subtree_completed(dnp, zb, 594 &scn->scn_phys.scn_bookmark)) 595 return (B_TRUE); 596 597 /* 598 * If we found the block we're trying to resume from, or 599 * we went past it to a different object, zero it out to 600 * indicate that it's OK to start checking for pausing 601 * again. 602 */ 603 if (bcmp(zb, &scn->scn_phys.scn_bookmark, sizeof (*zb)) == 0 || 604 zb->zb_object > scn->scn_phys.scn_bookmark.zb_object) { 605 dprintf("resuming at %llx/%llx/%llx/%llx\n", 606 (longlong_t)zb->zb_objset, 607 (longlong_t)zb->zb_object, 608 (longlong_t)zb->zb_level, 609 (longlong_t)zb->zb_blkid); 610 bzero(&scn->scn_phys.scn_bookmark, sizeof (*zb)); 611 } 612 } 613 return (B_FALSE); 614 } 615 616 /* 617 * Return nonzero on i/o error. 618 * Return new buf to write out in *bufp. 619 */ 620 static int 621 dsl_scan_recurse(dsl_scan_t *scn, dsl_dataset_t *ds, dmu_objset_type_t ostype, 622 dnode_phys_t *dnp, const blkptr_t *bp, 623 const zbookmark_phys_t *zb, dmu_tx_t *tx) 624 { 625 dsl_pool_t *dp = scn->scn_dp; 626 int zio_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCAN_THREAD; 627 int err; 628 629 if (BP_GET_LEVEL(bp) > 0) { 630 arc_flags_t flags = ARC_FLAG_WAIT; 631 int i; 632 blkptr_t *cbp; 633 int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT; 634 arc_buf_t *buf; 635 636 err = arc_read(NULL, dp->dp_spa, bp, arc_getbuf_func, &buf, 637 ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb); 638 if (err) { 639 scn->scn_phys.scn_errors++; 640 return (err); 641 } 642 for (i = 0, cbp = buf->b_data; i < epb; i++, cbp++) { 643 dsl_scan_prefetch(scn, buf, cbp, zb->zb_objset, 644 zb->zb_object, zb->zb_blkid * epb + i); 645 } 646 for (i = 0, cbp = buf->b_data; i < epb; i++, cbp++) { 647 zbookmark_phys_t czb; 648 649 SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object, 650 zb->zb_level - 1, 651 zb->zb_blkid * epb + i); 652 dsl_scan_visitbp(cbp, &czb, dnp, 653 ds, scn, ostype, tx); 654 } 655 arc_buf_destroy(buf, &buf); 656 } else if (BP_GET_TYPE(bp) == DMU_OT_DNODE) { 657 arc_flags_t flags = ARC_FLAG_WAIT; 658 dnode_phys_t *cdnp; 659 int i, j; 660 int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT; 661 arc_buf_t *buf; 662 663 err = arc_read(NULL, dp->dp_spa, bp, arc_getbuf_func, &buf, 664 ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb); 665 if (err) { 666 scn->scn_phys.scn_errors++; 667 return (err); 668 } 669 for (i = 0, cdnp = buf->b_data; i < epb; i++, cdnp++) { 670 for (j = 0; j < cdnp->dn_nblkptr; j++) { 671 blkptr_t *cbp = &cdnp->dn_blkptr[j]; 672 dsl_scan_prefetch(scn, buf, cbp, 673 zb->zb_objset, zb->zb_blkid * epb + i, j); 674 } 675 } 676 for (i = 0, cdnp = buf->b_data; i < epb; i++, cdnp++) { 677 dsl_scan_visitdnode(scn, ds, ostype, 678 cdnp, zb->zb_blkid * epb + i, tx); 679 } 680 681 arc_buf_destroy(buf, &buf); 682 } else if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) { 683 arc_flags_t flags = ARC_FLAG_WAIT; 684 objset_phys_t *osp; 685 arc_buf_t *buf; 686 687 err = arc_read(NULL, dp->dp_spa, bp, arc_getbuf_func, &buf, 688 ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb); 689 if (err) { 690 scn->scn_phys.scn_errors++; 691 return (err); 692 } 693 694 osp = buf->b_data; 695 696 dsl_scan_visitdnode(scn, ds, osp->os_type, 697 &osp->os_meta_dnode, DMU_META_DNODE_OBJECT, tx); 698 699 if (OBJSET_BUF_HAS_USERUSED(buf)) { 700 /* 701 * We also always visit user/group accounting 702 * objects, and never skip them, even if we are 703 * pausing. This is necessary so that the space 704 * deltas from this txg get integrated. 705 */ 706 dsl_scan_visitdnode(scn, ds, osp->os_type, 707 &osp->os_groupused_dnode, 708 DMU_GROUPUSED_OBJECT, tx); 709 dsl_scan_visitdnode(scn, ds, osp->os_type, 710 &osp->os_userused_dnode, 711 DMU_USERUSED_OBJECT, tx); 712 } 713 arc_buf_destroy(buf, &buf); 714 } 715 716 return (0); 717 } 718 719 static void 720 dsl_scan_visitdnode(dsl_scan_t *scn, dsl_dataset_t *ds, 721 dmu_objset_type_t ostype, dnode_phys_t *dnp, 722 uint64_t object, dmu_tx_t *tx) 723 { 724 int j; 725 726 for (j = 0; j < dnp->dn_nblkptr; j++) { 727 zbookmark_phys_t czb; 728 729 SET_BOOKMARK(&czb, ds ? ds->ds_object : 0, object, 730 dnp->dn_nlevels - 1, j); 731 dsl_scan_visitbp(&dnp->dn_blkptr[j], 732 &czb, dnp, ds, scn, ostype, tx); 733 } 734 735 if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) { 736 zbookmark_phys_t czb; 737 SET_BOOKMARK(&czb, ds ? ds->ds_object : 0, object, 738 0, DMU_SPILL_BLKID); 739 dsl_scan_visitbp(&dnp->dn_spill, 740 &czb, dnp, ds, scn, ostype, tx); 741 } 742 } 743 744 /* 745 * The arguments are in this order because mdb can only print the 746 * first 5; we want them to be useful. 747 */ 748 static void 749 dsl_scan_visitbp(blkptr_t *bp, const zbookmark_phys_t *zb, 750 dnode_phys_t *dnp, dsl_dataset_t *ds, dsl_scan_t *scn, 751 dmu_objset_type_t ostype, dmu_tx_t *tx) 752 { 753 dsl_pool_t *dp = scn->scn_dp; 754 arc_buf_t *buf = NULL; 755 blkptr_t bp_toread = *bp; 756 757 /* ASSERT(pbuf == NULL || arc_released(pbuf)); */ 758 759 if (dsl_scan_check_pause(scn, zb)) 760 return; 761 762 if (dsl_scan_check_resume(scn, dnp, zb)) 763 return; 764 765 if (BP_IS_HOLE(bp)) 766 return; 767 768 scn->scn_visited_this_txg++; 769 770 dprintf_bp(bp, 771 "visiting ds=%p/%llu zb=%llx/%llx/%llx/%llx bp=%p", 772 ds, ds ? ds->ds_object : 0, 773 zb->zb_objset, zb->zb_object, zb->zb_level, zb->zb_blkid, 774 bp); 775 776 if (bp->blk_birth <= scn->scn_phys.scn_cur_min_txg) 777 return; 778 779 if (dsl_scan_recurse(scn, ds, ostype, dnp, &bp_toread, zb, tx) != 0) 780 return; 781 782 /* 783 * If dsl_scan_ddt() has already visited this block, it will have 784 * already done any translations or scrubbing, so don't call the 785 * callback again. 786 */ 787 if (ddt_class_contains(dp->dp_spa, 788 scn->scn_phys.scn_ddt_class_max, bp)) { 789 ASSERT(buf == NULL); 790 return; 791 } 792 793 /* 794 * If this block is from the future (after cur_max_txg), then we 795 * are doing this on behalf of a deleted snapshot, and we will 796 * revisit the future block on the next pass of this dataset. 797 * Don't scan it now unless we need to because something 798 * under it was modified. 799 */ 800 if (BP_PHYSICAL_BIRTH(bp) <= scn->scn_phys.scn_cur_max_txg) { 801 scan_funcs[scn->scn_phys.scn_func](dp, bp, zb); 802 } 803 } 804 805 static void 806 dsl_scan_visit_rootbp(dsl_scan_t *scn, dsl_dataset_t *ds, blkptr_t *bp, 807 dmu_tx_t *tx) 808 { 809 zbookmark_phys_t zb; 810 811 SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET, 812 ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 813 dsl_scan_visitbp(bp, &zb, NULL, 814 ds, scn, DMU_OST_NONE, tx); 815 816 dprintf_ds(ds, "finished scan%s", ""); 817 } 818 819 void 820 dsl_scan_ds_destroyed(dsl_dataset_t *ds, dmu_tx_t *tx) 821 { 822 dsl_pool_t *dp = ds->ds_dir->dd_pool; 823 dsl_scan_t *scn = dp->dp_scan; 824 uint64_t mintxg; 825 826 if (scn->scn_phys.scn_state != DSS_SCANNING) 827 return; 828 829 if (scn->scn_phys.scn_bookmark.zb_objset == ds->ds_object) { 830 if (ds->ds_is_snapshot) { 831 /* 832 * Note: 833 * - scn_cur_{min,max}_txg stays the same. 834 * - Setting the flag is not really necessary if 835 * scn_cur_max_txg == scn_max_txg, because there 836 * is nothing after this snapshot that we care 837 * about. However, we set it anyway and then 838 * ignore it when we retraverse it in 839 * dsl_scan_visitds(). 840 */ 841 scn->scn_phys.scn_bookmark.zb_objset = 842 dsl_dataset_phys(ds)->ds_next_snap_obj; 843 zfs_dbgmsg("destroying ds %llu; currently traversing; " 844 "reset zb_objset to %llu", 845 (u_longlong_t)ds->ds_object, 846 (u_longlong_t)dsl_dataset_phys(ds)-> 847 ds_next_snap_obj); 848 scn->scn_phys.scn_flags |= DSF_VISIT_DS_AGAIN; 849 } else { 850 SET_BOOKMARK(&scn->scn_phys.scn_bookmark, 851 ZB_DESTROYED_OBJSET, 0, 0, 0); 852 zfs_dbgmsg("destroying ds %llu; currently traversing; " 853 "reset bookmark to -1,0,0,0", 854 (u_longlong_t)ds->ds_object); 855 } 856 } else if (zap_lookup_int_key(dp->dp_meta_objset, 857 scn->scn_phys.scn_queue_obj, ds->ds_object, &mintxg) == 0) { 858 ASSERT3U(dsl_dataset_phys(ds)->ds_num_children, <=, 1); 859 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset, 860 scn->scn_phys.scn_queue_obj, ds->ds_object, tx)); 861 if (ds->ds_is_snapshot) { 862 /* 863 * We keep the same mintxg; it could be > 864 * ds_creation_txg if the previous snapshot was 865 * deleted too. 866 */ 867 VERIFY(zap_add_int_key(dp->dp_meta_objset, 868 scn->scn_phys.scn_queue_obj, 869 dsl_dataset_phys(ds)->ds_next_snap_obj, 870 mintxg, tx) == 0); 871 zfs_dbgmsg("destroying ds %llu; in queue; " 872 "replacing with %llu", 873 (u_longlong_t)ds->ds_object, 874 (u_longlong_t)dsl_dataset_phys(ds)-> 875 ds_next_snap_obj); 876 } else { 877 zfs_dbgmsg("destroying ds %llu; in queue; removing", 878 (u_longlong_t)ds->ds_object); 879 } 880 } 881 882 /* 883 * dsl_scan_sync() should be called after this, and should sync 884 * out our changed state, but just to be safe, do it here. 885 */ 886 dsl_scan_sync_state(scn, tx); 887 } 888 889 void 890 dsl_scan_ds_snapshotted(dsl_dataset_t *ds, dmu_tx_t *tx) 891 { 892 dsl_pool_t *dp = ds->ds_dir->dd_pool; 893 dsl_scan_t *scn = dp->dp_scan; 894 uint64_t mintxg; 895 896 if (scn->scn_phys.scn_state != DSS_SCANNING) 897 return; 898 899 ASSERT(dsl_dataset_phys(ds)->ds_prev_snap_obj != 0); 900 901 if (scn->scn_phys.scn_bookmark.zb_objset == ds->ds_object) { 902 scn->scn_phys.scn_bookmark.zb_objset = 903 dsl_dataset_phys(ds)->ds_prev_snap_obj; 904 zfs_dbgmsg("snapshotting ds %llu; currently traversing; " 905 "reset zb_objset to %llu", 906 (u_longlong_t)ds->ds_object, 907 (u_longlong_t)dsl_dataset_phys(ds)->ds_prev_snap_obj); 908 } else if (zap_lookup_int_key(dp->dp_meta_objset, 909 scn->scn_phys.scn_queue_obj, ds->ds_object, &mintxg) == 0) { 910 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset, 911 scn->scn_phys.scn_queue_obj, ds->ds_object, tx)); 912 VERIFY(zap_add_int_key(dp->dp_meta_objset, 913 scn->scn_phys.scn_queue_obj, 914 dsl_dataset_phys(ds)->ds_prev_snap_obj, mintxg, tx) == 0); 915 zfs_dbgmsg("snapshotting ds %llu; in queue; " 916 "replacing with %llu", 917 (u_longlong_t)ds->ds_object, 918 (u_longlong_t)dsl_dataset_phys(ds)->ds_prev_snap_obj); 919 } 920 dsl_scan_sync_state(scn, tx); 921 } 922 923 void 924 dsl_scan_ds_clone_swapped(dsl_dataset_t *ds1, dsl_dataset_t *ds2, dmu_tx_t *tx) 925 { 926 dsl_pool_t *dp = ds1->ds_dir->dd_pool; 927 dsl_scan_t *scn = dp->dp_scan; 928 uint64_t mintxg; 929 930 if (scn->scn_phys.scn_state != DSS_SCANNING) 931 return; 932 933 if (scn->scn_phys.scn_bookmark.zb_objset == ds1->ds_object) { 934 scn->scn_phys.scn_bookmark.zb_objset = ds2->ds_object; 935 zfs_dbgmsg("clone_swap ds %llu; currently traversing; " 936 "reset zb_objset to %llu", 937 (u_longlong_t)ds1->ds_object, 938 (u_longlong_t)ds2->ds_object); 939 } else if (scn->scn_phys.scn_bookmark.zb_objset == ds2->ds_object) { 940 scn->scn_phys.scn_bookmark.zb_objset = ds1->ds_object; 941 zfs_dbgmsg("clone_swap ds %llu; currently traversing; " 942 "reset zb_objset to %llu", 943 (u_longlong_t)ds2->ds_object, 944 (u_longlong_t)ds1->ds_object); 945 } 946 947 if (zap_lookup_int_key(dp->dp_meta_objset, scn->scn_phys.scn_queue_obj, 948 ds1->ds_object, &mintxg) == 0) { 949 int err; 950 951 ASSERT3U(mintxg, ==, dsl_dataset_phys(ds1)->ds_prev_snap_txg); 952 ASSERT3U(mintxg, ==, dsl_dataset_phys(ds2)->ds_prev_snap_txg); 953 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset, 954 scn->scn_phys.scn_queue_obj, ds1->ds_object, tx)); 955 err = zap_add_int_key(dp->dp_meta_objset, 956 scn->scn_phys.scn_queue_obj, ds2->ds_object, mintxg, tx); 957 VERIFY(err == 0 || err == EEXIST); 958 if (err == EEXIST) { 959 /* Both were there to begin with */ 960 VERIFY(0 == zap_add_int_key(dp->dp_meta_objset, 961 scn->scn_phys.scn_queue_obj, 962 ds1->ds_object, mintxg, tx)); 963 } 964 zfs_dbgmsg("clone_swap ds %llu; in queue; " 965 "replacing with %llu", 966 (u_longlong_t)ds1->ds_object, 967 (u_longlong_t)ds2->ds_object); 968 } else if (zap_lookup_int_key(dp->dp_meta_objset, 969 scn->scn_phys.scn_queue_obj, ds2->ds_object, &mintxg) == 0) { 970 ASSERT3U(mintxg, ==, dsl_dataset_phys(ds1)->ds_prev_snap_txg); 971 ASSERT3U(mintxg, ==, dsl_dataset_phys(ds2)->ds_prev_snap_txg); 972 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset, 973 scn->scn_phys.scn_queue_obj, ds2->ds_object, tx)); 974 VERIFY(0 == zap_add_int_key(dp->dp_meta_objset, 975 scn->scn_phys.scn_queue_obj, ds1->ds_object, mintxg, tx)); 976 zfs_dbgmsg("clone_swap ds %llu; in queue; " 977 "replacing with %llu", 978 (u_longlong_t)ds2->ds_object, 979 (u_longlong_t)ds1->ds_object); 980 } 981 982 dsl_scan_sync_state(scn, tx); 983 } 984 985 struct enqueue_clones_arg { 986 dmu_tx_t *tx; 987 uint64_t originobj; 988 }; 989 990 /* ARGSUSED */ 991 static int 992 enqueue_clones_cb(dsl_pool_t *dp, dsl_dataset_t *hds, void *arg) 993 { 994 struct enqueue_clones_arg *eca = arg; 995 dsl_dataset_t *ds; 996 int err; 997 dsl_scan_t *scn = dp->dp_scan; 998 999 if (dsl_dir_phys(hds->ds_dir)->dd_origin_obj != eca->originobj) 1000 return (0); 1001 1002 err = dsl_dataset_hold_obj(dp, hds->ds_object, FTAG, &ds); 1003 if (err) 1004 return (err); 1005 1006 while (dsl_dataset_phys(ds)->ds_prev_snap_obj != eca->originobj) { 1007 dsl_dataset_t *prev; 1008 err = dsl_dataset_hold_obj(dp, 1009 dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &prev); 1010 1011 dsl_dataset_rele(ds, FTAG); 1012 if (err) 1013 return (err); 1014 ds = prev; 1015 } 1016 VERIFY(zap_add_int_key(dp->dp_meta_objset, 1017 scn->scn_phys.scn_queue_obj, ds->ds_object, 1018 dsl_dataset_phys(ds)->ds_prev_snap_txg, eca->tx) == 0); 1019 dsl_dataset_rele(ds, FTAG); 1020 return (0); 1021 } 1022 1023 static void 1024 dsl_scan_visitds(dsl_scan_t *scn, uint64_t dsobj, dmu_tx_t *tx) 1025 { 1026 dsl_pool_t *dp = scn->scn_dp; 1027 dsl_dataset_t *ds; 1028 objset_t *os; 1029 1030 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds)); 1031 1032 if (scn->scn_phys.scn_cur_min_txg >= 1033 scn->scn_phys.scn_max_txg) { 1034 /* 1035 * This can happen if this snapshot was created after the 1036 * scan started, and we already completed a previous snapshot 1037 * that was created after the scan started. This snapshot 1038 * only references blocks with: 1039 * 1040 * birth < our ds_creation_txg 1041 * cur_min_txg is no less than ds_creation_txg. 1042 * We have already visited these blocks. 1043 * or 1044 * birth > scn_max_txg 1045 * The scan requested not to visit these blocks. 1046 * 1047 * Subsequent snapshots (and clones) can reference our 1048 * blocks, or blocks with even higher birth times. 1049 * Therefore we do not need to visit them either, 1050 * so we do not add them to the work queue. 1051 * 1052 * Note that checking for cur_min_txg >= cur_max_txg 1053 * is not sufficient, because in that case we may need to 1054 * visit subsequent snapshots. This happens when min_txg > 0, 1055 * which raises cur_min_txg. In this case we will visit 1056 * this dataset but skip all of its blocks, because the 1057 * rootbp's birth time is < cur_min_txg. Then we will 1058 * add the next snapshots/clones to the work queue. 1059 */ 1060 char *dsname = kmem_alloc(MAXNAMELEN, KM_SLEEP); 1061 dsl_dataset_name(ds, dsname); 1062 zfs_dbgmsg("scanning dataset %llu (%s) is unnecessary because " 1063 "cur_min_txg (%llu) >= max_txg (%llu)", 1064 dsobj, dsname, 1065 scn->scn_phys.scn_cur_min_txg, 1066 scn->scn_phys.scn_max_txg); 1067 kmem_free(dsname, MAXNAMELEN); 1068 1069 goto out; 1070 } 1071 1072 if (dmu_objset_from_ds(ds, &os)) 1073 goto out; 1074 1075 /* 1076 * Only the ZIL in the head (non-snapshot) is valid. Even though 1077 * snapshots can have ZIL block pointers (which may be the same 1078 * BP as in the head), they must be ignored. So we traverse the 1079 * ZIL here, rather than in scan_recurse(), because the regular 1080 * snapshot block-sharing rules don't apply to it. 1081 */ 1082 if (DSL_SCAN_IS_SCRUB_RESILVER(scn) && !ds->ds_is_snapshot) 1083 dsl_scan_zil(dp, &os->os_zil_header); 1084 1085 /* 1086 * Iterate over the bps in this ds. 1087 */ 1088 dmu_buf_will_dirty(ds->ds_dbuf, tx); 1089 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG); 1090 dsl_scan_visit_rootbp(scn, ds, &dsl_dataset_phys(ds)->ds_bp, tx); 1091 rrw_exit(&ds->ds_bp_rwlock, FTAG); 1092 1093 char *dsname = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP); 1094 dsl_dataset_name(ds, dsname); 1095 zfs_dbgmsg("scanned dataset %llu (%s) with min=%llu max=%llu; " 1096 "pausing=%u", 1097 (longlong_t)dsobj, dsname, 1098 (longlong_t)scn->scn_phys.scn_cur_min_txg, 1099 (longlong_t)scn->scn_phys.scn_cur_max_txg, 1100 (int)scn->scn_pausing); 1101 kmem_free(dsname, ZFS_MAX_DATASET_NAME_LEN); 1102 1103 if (scn->scn_pausing) 1104 goto out; 1105 1106 /* 1107 * We've finished this pass over this dataset. 1108 */ 1109 1110 /* 1111 * If we did not completely visit this dataset, do another pass. 1112 */ 1113 if (scn->scn_phys.scn_flags & DSF_VISIT_DS_AGAIN) { 1114 zfs_dbgmsg("incomplete pass; visiting again"); 1115 scn->scn_phys.scn_flags &= ~DSF_VISIT_DS_AGAIN; 1116 VERIFY(zap_add_int_key(dp->dp_meta_objset, 1117 scn->scn_phys.scn_queue_obj, ds->ds_object, 1118 scn->scn_phys.scn_cur_max_txg, tx) == 0); 1119 goto out; 1120 } 1121 1122 /* 1123 * Add descendent datasets to work queue. 1124 */ 1125 if (dsl_dataset_phys(ds)->ds_next_snap_obj != 0) { 1126 VERIFY(zap_add_int_key(dp->dp_meta_objset, 1127 scn->scn_phys.scn_queue_obj, 1128 dsl_dataset_phys(ds)->ds_next_snap_obj, 1129 dsl_dataset_phys(ds)->ds_creation_txg, tx) == 0); 1130 } 1131 if (dsl_dataset_phys(ds)->ds_num_children > 1) { 1132 boolean_t usenext = B_FALSE; 1133 if (dsl_dataset_phys(ds)->ds_next_clones_obj != 0) { 1134 uint64_t count; 1135 /* 1136 * A bug in a previous version of the code could 1137 * cause upgrade_clones_cb() to not set 1138 * ds_next_snap_obj when it should, leading to a 1139 * missing entry. Therefore we can only use the 1140 * next_clones_obj when its count is correct. 1141 */ 1142 int err = zap_count(dp->dp_meta_objset, 1143 dsl_dataset_phys(ds)->ds_next_clones_obj, &count); 1144 if (err == 0 && 1145 count == dsl_dataset_phys(ds)->ds_num_children - 1) 1146 usenext = B_TRUE; 1147 } 1148 1149 if (usenext) { 1150 VERIFY0(zap_join_key(dp->dp_meta_objset, 1151 dsl_dataset_phys(ds)->ds_next_clones_obj, 1152 scn->scn_phys.scn_queue_obj, 1153 dsl_dataset_phys(ds)->ds_creation_txg, tx)); 1154 } else { 1155 struct enqueue_clones_arg eca; 1156 eca.tx = tx; 1157 eca.originobj = ds->ds_object; 1158 1159 VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj, 1160 enqueue_clones_cb, &eca, DS_FIND_CHILDREN)); 1161 } 1162 } 1163 1164 out: 1165 dsl_dataset_rele(ds, FTAG); 1166 } 1167 1168 /* ARGSUSED */ 1169 static int 1170 enqueue_cb(dsl_pool_t *dp, dsl_dataset_t *hds, void *arg) 1171 { 1172 dmu_tx_t *tx = arg; 1173 dsl_dataset_t *ds; 1174 int err; 1175 dsl_scan_t *scn = dp->dp_scan; 1176 1177 err = dsl_dataset_hold_obj(dp, hds->ds_object, FTAG, &ds); 1178 if (err) 1179 return (err); 1180 1181 while (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) { 1182 dsl_dataset_t *prev; 1183 err = dsl_dataset_hold_obj(dp, 1184 dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &prev); 1185 if (err) { 1186 dsl_dataset_rele(ds, FTAG); 1187 return (err); 1188 } 1189 1190 /* 1191 * If this is a clone, we don't need to worry about it for now. 1192 */ 1193 if (dsl_dataset_phys(prev)->ds_next_snap_obj != ds->ds_object) { 1194 dsl_dataset_rele(ds, FTAG); 1195 dsl_dataset_rele(prev, FTAG); 1196 return (0); 1197 } 1198 dsl_dataset_rele(ds, FTAG); 1199 ds = prev; 1200 } 1201 1202 VERIFY(zap_add_int_key(dp->dp_meta_objset, scn->scn_phys.scn_queue_obj, 1203 ds->ds_object, dsl_dataset_phys(ds)->ds_prev_snap_txg, tx) == 0); 1204 dsl_dataset_rele(ds, FTAG); 1205 return (0); 1206 } 1207 1208 /* 1209 * Scrub/dedup interaction. 1210 * 1211 * If there are N references to a deduped block, we don't want to scrub it 1212 * N times -- ideally, we should scrub it exactly once. 1213 * 1214 * We leverage the fact that the dde's replication class (enum ddt_class) 1215 * is ordered from highest replication class (DDT_CLASS_DITTO) to lowest 1216 * (DDT_CLASS_UNIQUE) so that we may walk the DDT in that order. 1217 * 1218 * To prevent excess scrubbing, the scrub begins by walking the DDT 1219 * to find all blocks with refcnt > 1, and scrubs each of these once. 1220 * Since there are two replication classes which contain blocks with 1221 * refcnt > 1, we scrub the highest replication class (DDT_CLASS_DITTO) first. 1222 * Finally the top-down scrub begins, only visiting blocks with refcnt == 1. 1223 * 1224 * There would be nothing more to say if a block's refcnt couldn't change 1225 * during a scrub, but of course it can so we must account for changes 1226 * in a block's replication class. 1227 * 1228 * Here's an example of what can occur: 1229 * 1230 * If a block has refcnt > 1 during the DDT scrub phase, but has refcnt == 1 1231 * when visited during the top-down scrub phase, it will be scrubbed twice. 1232 * This negates our scrub optimization, but is otherwise harmless. 1233 * 1234 * If a block has refcnt == 1 during the DDT scrub phase, but has refcnt > 1 1235 * on each visit during the top-down scrub phase, it will never be scrubbed. 1236 * To catch this, ddt_sync_entry() notifies the scrub code whenever a block's 1237 * reference class transitions to a higher level (i.e DDT_CLASS_UNIQUE to 1238 * DDT_CLASS_DUPLICATE); if it transitions from refcnt == 1 to refcnt > 1 1239 * while a scrub is in progress, it scrubs the block right then. 1240 */ 1241 static void 1242 dsl_scan_ddt(dsl_scan_t *scn, dmu_tx_t *tx) 1243 { 1244 ddt_bookmark_t *ddb = &scn->scn_phys.scn_ddt_bookmark; 1245 ddt_entry_t dde = { 0 }; 1246 int error; 1247 uint64_t n = 0; 1248 1249 while ((error = ddt_walk(scn->scn_dp->dp_spa, ddb, &dde)) == 0) { 1250 ddt_t *ddt; 1251 1252 if (ddb->ddb_class > scn->scn_phys.scn_ddt_class_max) 1253 break; 1254 dprintf("visiting ddb=%llu/%llu/%llu/%llx\n", 1255 (longlong_t)ddb->ddb_class, 1256 (longlong_t)ddb->ddb_type, 1257 (longlong_t)ddb->ddb_checksum, 1258 (longlong_t)ddb->ddb_cursor); 1259 1260 /* There should be no pending changes to the dedup table */ 1261 ddt = scn->scn_dp->dp_spa->spa_ddt[ddb->ddb_checksum]; 1262 ASSERT(avl_first(&ddt->ddt_tree) == NULL); 1263 1264 dsl_scan_ddt_entry(scn, ddb->ddb_checksum, &dde, tx); 1265 n++; 1266 1267 if (dsl_scan_check_pause(scn, NULL)) 1268 break; 1269 } 1270 1271 zfs_dbgmsg("scanned %llu ddt entries with class_max = %u; pausing=%u", 1272 (longlong_t)n, (int)scn->scn_phys.scn_ddt_class_max, 1273 (int)scn->scn_pausing); 1274 1275 ASSERT(error == 0 || error == ENOENT); 1276 ASSERT(error != ENOENT || 1277 ddb->ddb_class > scn->scn_phys.scn_ddt_class_max); 1278 } 1279 1280 /* ARGSUSED */ 1281 void 1282 dsl_scan_ddt_entry(dsl_scan_t *scn, enum zio_checksum checksum, 1283 ddt_entry_t *dde, dmu_tx_t *tx) 1284 { 1285 const ddt_key_t *ddk = &dde->dde_key; 1286 ddt_phys_t *ddp = dde->dde_phys; 1287 blkptr_t bp; 1288 zbookmark_phys_t zb = { 0 }; 1289 1290 if (scn->scn_phys.scn_state != DSS_SCANNING) 1291 return; 1292 1293 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) { 1294 if (ddp->ddp_phys_birth == 0 || 1295 ddp->ddp_phys_birth > scn->scn_phys.scn_max_txg) 1296 continue; 1297 ddt_bp_create(checksum, ddk, ddp, &bp); 1298 1299 scn->scn_visited_this_txg++; 1300 scan_funcs[scn->scn_phys.scn_func](scn->scn_dp, &bp, &zb); 1301 } 1302 } 1303 1304 static void 1305 dsl_scan_visit(dsl_scan_t *scn, dmu_tx_t *tx) 1306 { 1307 dsl_pool_t *dp = scn->scn_dp; 1308 zap_cursor_t zc; 1309 zap_attribute_t za; 1310 1311 if (scn->scn_phys.scn_ddt_bookmark.ddb_class <= 1312 scn->scn_phys.scn_ddt_class_max) { 1313 scn->scn_phys.scn_cur_min_txg = scn->scn_phys.scn_min_txg; 1314 scn->scn_phys.scn_cur_max_txg = scn->scn_phys.scn_max_txg; 1315 dsl_scan_ddt(scn, tx); 1316 if (scn->scn_pausing) 1317 return; 1318 } 1319 1320 if (scn->scn_phys.scn_bookmark.zb_objset == DMU_META_OBJSET) { 1321 /* First do the MOS & ORIGIN */ 1322 1323 scn->scn_phys.scn_cur_min_txg = scn->scn_phys.scn_min_txg; 1324 scn->scn_phys.scn_cur_max_txg = scn->scn_phys.scn_max_txg; 1325 dsl_scan_visit_rootbp(scn, NULL, 1326 &dp->dp_meta_rootbp, tx); 1327 spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp); 1328 if (scn->scn_pausing) 1329 return; 1330 1331 if (spa_version(dp->dp_spa) < SPA_VERSION_DSL_SCRUB) { 1332 VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj, 1333 enqueue_cb, tx, DS_FIND_CHILDREN)); 1334 } else { 1335 dsl_scan_visitds(scn, 1336 dp->dp_origin_snap->ds_object, tx); 1337 } 1338 ASSERT(!scn->scn_pausing); 1339 } else if (scn->scn_phys.scn_bookmark.zb_objset != 1340 ZB_DESTROYED_OBJSET) { 1341 /* 1342 * If we were paused, continue from here. Note if the 1343 * ds we were paused on was deleted, the zb_objset may 1344 * be -1, so we will skip this and find a new objset 1345 * below. 1346 */ 1347 dsl_scan_visitds(scn, scn->scn_phys.scn_bookmark.zb_objset, tx); 1348 if (scn->scn_pausing) 1349 return; 1350 } 1351 1352 /* 1353 * In case we were paused right at the end of the ds, zero the 1354 * bookmark so we don't think that we're still trying to resume. 1355 */ 1356 bzero(&scn->scn_phys.scn_bookmark, sizeof (zbookmark_phys_t)); 1357 1358 /* keep pulling things out of the zap-object-as-queue */ 1359 while (zap_cursor_init(&zc, dp->dp_meta_objset, 1360 scn->scn_phys.scn_queue_obj), 1361 zap_cursor_retrieve(&zc, &za) == 0) { 1362 dsl_dataset_t *ds; 1363 uint64_t dsobj; 1364 1365 dsobj = strtonum(za.za_name, NULL); 1366 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset, 1367 scn->scn_phys.scn_queue_obj, dsobj, tx)); 1368 1369 /* Set up min/max txg */ 1370 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds)); 1371 if (za.za_first_integer != 0) { 1372 scn->scn_phys.scn_cur_min_txg = 1373 MAX(scn->scn_phys.scn_min_txg, 1374 za.za_first_integer); 1375 } else { 1376 scn->scn_phys.scn_cur_min_txg = 1377 MAX(scn->scn_phys.scn_min_txg, 1378 dsl_dataset_phys(ds)->ds_prev_snap_txg); 1379 } 1380 scn->scn_phys.scn_cur_max_txg = dsl_scan_ds_maxtxg(ds); 1381 dsl_dataset_rele(ds, FTAG); 1382 1383 dsl_scan_visitds(scn, dsobj, tx); 1384 zap_cursor_fini(&zc); 1385 if (scn->scn_pausing) 1386 return; 1387 } 1388 zap_cursor_fini(&zc); 1389 } 1390 1391 static boolean_t 1392 dsl_scan_free_should_pause(dsl_scan_t *scn) 1393 { 1394 uint64_t elapsed_nanosecs; 1395 1396 if (zfs_recover) 1397 return (B_FALSE); 1398 1399 if (scn->scn_visited_this_txg >= zfs_free_max_blocks) 1400 return (B_TRUE); 1401 1402 elapsed_nanosecs = gethrtime() - scn->scn_sync_start_time; 1403 return (elapsed_nanosecs / NANOSEC > zfs_txg_timeout || 1404 (NSEC2MSEC(elapsed_nanosecs) > zfs_free_min_time_ms && 1405 txg_sync_waiting(scn->scn_dp)) || 1406 spa_shutting_down(scn->scn_dp->dp_spa)); 1407 } 1408 1409 static int 1410 dsl_scan_free_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 1411 { 1412 dsl_scan_t *scn = arg; 1413 1414 if (!scn->scn_is_bptree || 1415 (BP_GET_LEVEL(bp) == 0 && BP_GET_TYPE(bp) != DMU_OT_OBJSET)) { 1416 if (dsl_scan_free_should_pause(scn)) 1417 return (SET_ERROR(ERESTART)); 1418 } 1419 1420 zio_nowait(zio_free_sync(scn->scn_zio_root, scn->scn_dp->dp_spa, 1421 dmu_tx_get_txg(tx), bp, 0)); 1422 dsl_dir_diduse_space(tx->tx_pool->dp_free_dir, DD_USED_HEAD, 1423 -bp_get_dsize_sync(scn->scn_dp->dp_spa, bp), 1424 -BP_GET_PSIZE(bp), -BP_GET_UCSIZE(bp), tx); 1425 scn->scn_visited_this_txg++; 1426 return (0); 1427 } 1428 1429 boolean_t 1430 dsl_scan_active(dsl_scan_t *scn) 1431 { 1432 spa_t *spa = scn->scn_dp->dp_spa; 1433 uint64_t used = 0, comp, uncomp; 1434 1435 if (spa->spa_load_state != SPA_LOAD_NONE) 1436 return (B_FALSE); 1437 if (spa_shutting_down(spa)) 1438 return (B_FALSE); 1439 if (scn->scn_phys.scn_state == DSS_SCANNING || 1440 (scn->scn_async_destroying && !scn->scn_async_stalled)) 1441 return (B_TRUE); 1442 1443 if (spa_version(scn->scn_dp->dp_spa) >= SPA_VERSION_DEADLISTS) { 1444 (void) bpobj_space(&scn->scn_dp->dp_free_bpobj, 1445 &used, &comp, &uncomp); 1446 } 1447 return (used != 0); 1448 } 1449 1450 /* Called whenever a txg syncs. */ 1451 void 1452 dsl_scan_sync(dsl_pool_t *dp, dmu_tx_t *tx) 1453 { 1454 dsl_scan_t *scn = dp->dp_scan; 1455 spa_t *spa = dp->dp_spa; 1456 int err = 0; 1457 1458 /* 1459 * Check for scn_restart_txg before checking spa_load_state, so 1460 * that we can restart an old-style scan while the pool is being 1461 * imported (see dsl_scan_init). 1462 */ 1463 if (dsl_scan_restarting(scn, tx)) { 1464 pool_scan_func_t func = POOL_SCAN_SCRUB; 1465 dsl_scan_done(scn, B_FALSE, tx); 1466 if (vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) 1467 func = POOL_SCAN_RESILVER; 1468 zfs_dbgmsg("restarting scan func=%u txg=%llu", 1469 func, tx->tx_txg); 1470 dsl_scan_setup_sync(&func, tx); 1471 } 1472 1473 /* 1474 * Only process scans in sync pass 1. 1475 */ 1476 if (spa_sync_pass(dp->dp_spa) > 1) 1477 return; 1478 1479 /* 1480 * If the spa is shutting down, then stop scanning. This will 1481 * ensure that the scan does not dirty any new data during the 1482 * shutdown phase. 1483 */ 1484 if (spa_shutting_down(spa)) 1485 return; 1486 1487 /* 1488 * If the scan is inactive due to a stalled async destroy, try again. 1489 */ 1490 if (!scn->scn_async_stalled && !dsl_scan_active(scn)) 1491 return; 1492 1493 scn->scn_visited_this_txg = 0; 1494 scn->scn_pausing = B_FALSE; 1495 scn->scn_sync_start_time = gethrtime(); 1496 spa->spa_scrub_active = B_TRUE; 1497 1498 /* 1499 * First process the async destroys. If we pause, don't do 1500 * any scrubbing or resilvering. This ensures that there are no 1501 * async destroys while we are scanning, so the scan code doesn't 1502 * have to worry about traversing it. It is also faster to free the 1503 * blocks than to scrub them. 1504 */ 1505 if (zfs_free_bpobj_enabled && 1506 spa_version(dp->dp_spa) >= SPA_VERSION_DEADLISTS) { 1507 scn->scn_is_bptree = B_FALSE; 1508 scn->scn_zio_root = zio_root(dp->dp_spa, NULL, 1509 NULL, ZIO_FLAG_MUSTSUCCEED); 1510 err = bpobj_iterate(&dp->dp_free_bpobj, 1511 dsl_scan_free_block_cb, scn, tx); 1512 VERIFY3U(0, ==, zio_wait(scn->scn_zio_root)); 1513 1514 if (err != 0 && err != ERESTART) 1515 zfs_panic_recover("error %u from bpobj_iterate()", err); 1516 } 1517 1518 if (err == 0 && spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY)) { 1519 ASSERT(scn->scn_async_destroying); 1520 scn->scn_is_bptree = B_TRUE; 1521 scn->scn_zio_root = zio_root(dp->dp_spa, NULL, 1522 NULL, ZIO_FLAG_MUSTSUCCEED); 1523 err = bptree_iterate(dp->dp_meta_objset, 1524 dp->dp_bptree_obj, B_TRUE, dsl_scan_free_block_cb, scn, tx); 1525 VERIFY0(zio_wait(scn->scn_zio_root)); 1526 1527 if (err == EIO || err == ECKSUM) { 1528 err = 0; 1529 } else if (err != 0 && err != ERESTART) { 1530 zfs_panic_recover("error %u from " 1531 "traverse_dataset_destroyed()", err); 1532 } 1533 1534 if (bptree_is_empty(dp->dp_meta_objset, dp->dp_bptree_obj)) { 1535 /* finished; deactivate async destroy feature */ 1536 spa_feature_decr(spa, SPA_FEATURE_ASYNC_DESTROY, tx); 1537 ASSERT(!spa_feature_is_active(spa, 1538 SPA_FEATURE_ASYNC_DESTROY)); 1539 VERIFY0(zap_remove(dp->dp_meta_objset, 1540 DMU_POOL_DIRECTORY_OBJECT, 1541 DMU_POOL_BPTREE_OBJ, tx)); 1542 VERIFY0(bptree_free(dp->dp_meta_objset, 1543 dp->dp_bptree_obj, tx)); 1544 dp->dp_bptree_obj = 0; 1545 scn->scn_async_destroying = B_FALSE; 1546 scn->scn_async_stalled = B_FALSE; 1547 } else { 1548 /* 1549 * If we didn't make progress, mark the async 1550 * destroy as stalled, so that we will not initiate 1551 * a spa_sync() on its behalf. Note that we only 1552 * check this if we are not finished, because if the 1553 * bptree had no blocks for us to visit, we can 1554 * finish without "making progress". 1555 */ 1556 scn->scn_async_stalled = 1557 (scn->scn_visited_this_txg == 0); 1558 } 1559 } 1560 if (scn->scn_visited_this_txg) { 1561 zfs_dbgmsg("freed %llu blocks in %llums from " 1562 "free_bpobj/bptree txg %llu; err=%u", 1563 (longlong_t)scn->scn_visited_this_txg, 1564 (longlong_t) 1565 NSEC2MSEC(gethrtime() - scn->scn_sync_start_time), 1566 (longlong_t)tx->tx_txg, err); 1567 scn->scn_visited_this_txg = 0; 1568 1569 /* 1570 * Write out changes to the DDT that may be required as a 1571 * result of the blocks freed. This ensures that the DDT 1572 * is clean when a scrub/resilver runs. 1573 */ 1574 ddt_sync(spa, tx->tx_txg); 1575 } 1576 if (err != 0) 1577 return; 1578 if (dp->dp_free_dir != NULL && !scn->scn_async_destroying && 1579 zfs_free_leak_on_eio && 1580 (dsl_dir_phys(dp->dp_free_dir)->dd_used_bytes != 0 || 1581 dsl_dir_phys(dp->dp_free_dir)->dd_compressed_bytes != 0 || 1582 dsl_dir_phys(dp->dp_free_dir)->dd_uncompressed_bytes != 0)) { 1583 /* 1584 * We have finished background destroying, but there is still 1585 * some space left in the dp_free_dir. Transfer this leaked 1586 * space to the dp_leak_dir. 1587 */ 1588 if (dp->dp_leak_dir == NULL) { 1589 rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG); 1590 (void) dsl_dir_create_sync(dp, dp->dp_root_dir, 1591 LEAK_DIR_NAME, tx); 1592 VERIFY0(dsl_pool_open_special_dir(dp, 1593 LEAK_DIR_NAME, &dp->dp_leak_dir)); 1594 rrw_exit(&dp->dp_config_rwlock, FTAG); 1595 } 1596 dsl_dir_diduse_space(dp->dp_leak_dir, DD_USED_HEAD, 1597 dsl_dir_phys(dp->dp_free_dir)->dd_used_bytes, 1598 dsl_dir_phys(dp->dp_free_dir)->dd_compressed_bytes, 1599 dsl_dir_phys(dp->dp_free_dir)->dd_uncompressed_bytes, tx); 1600 dsl_dir_diduse_space(dp->dp_free_dir, DD_USED_HEAD, 1601 -dsl_dir_phys(dp->dp_free_dir)->dd_used_bytes, 1602 -dsl_dir_phys(dp->dp_free_dir)->dd_compressed_bytes, 1603 -dsl_dir_phys(dp->dp_free_dir)->dd_uncompressed_bytes, tx); 1604 } 1605 if (dp->dp_free_dir != NULL && !scn->scn_async_destroying) { 1606 /* finished; verify that space accounting went to zero */ 1607 ASSERT0(dsl_dir_phys(dp->dp_free_dir)->dd_used_bytes); 1608 ASSERT0(dsl_dir_phys(dp->dp_free_dir)->dd_compressed_bytes); 1609 ASSERT0(dsl_dir_phys(dp->dp_free_dir)->dd_uncompressed_bytes); 1610 } 1611 1612 if (scn->scn_phys.scn_state != DSS_SCANNING) 1613 return; 1614 1615 if (scn->scn_done_txg == tx->tx_txg) { 1616 ASSERT(!scn->scn_pausing); 1617 /* finished with scan. */ 1618 zfs_dbgmsg("txg %llu scan complete", tx->tx_txg); 1619 dsl_scan_done(scn, B_TRUE, tx); 1620 ASSERT3U(spa->spa_scrub_inflight, ==, 0); 1621 dsl_scan_sync_state(scn, tx); 1622 return; 1623 } 1624 1625 if (scn->scn_phys.scn_ddt_bookmark.ddb_class <= 1626 scn->scn_phys.scn_ddt_class_max) { 1627 zfs_dbgmsg("doing scan sync txg %llu; " 1628 "ddt bm=%llu/%llu/%llu/%llx", 1629 (longlong_t)tx->tx_txg, 1630 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_class, 1631 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_type, 1632 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_checksum, 1633 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_cursor); 1634 ASSERT(scn->scn_phys.scn_bookmark.zb_objset == 0); 1635 ASSERT(scn->scn_phys.scn_bookmark.zb_object == 0); 1636 ASSERT(scn->scn_phys.scn_bookmark.zb_level == 0); 1637 ASSERT(scn->scn_phys.scn_bookmark.zb_blkid == 0); 1638 } else { 1639 zfs_dbgmsg("doing scan sync txg %llu; bm=%llu/%llu/%llu/%llu", 1640 (longlong_t)tx->tx_txg, 1641 (longlong_t)scn->scn_phys.scn_bookmark.zb_objset, 1642 (longlong_t)scn->scn_phys.scn_bookmark.zb_object, 1643 (longlong_t)scn->scn_phys.scn_bookmark.zb_level, 1644 (longlong_t)scn->scn_phys.scn_bookmark.zb_blkid); 1645 } 1646 1647 scn->scn_zio_root = zio_root(dp->dp_spa, NULL, 1648 NULL, ZIO_FLAG_CANFAIL); 1649 dsl_pool_config_enter(dp, FTAG); 1650 dsl_scan_visit(scn, tx); 1651 dsl_pool_config_exit(dp, FTAG); 1652 (void) zio_wait(scn->scn_zio_root); 1653 scn->scn_zio_root = NULL; 1654 1655 zfs_dbgmsg("visited %llu blocks in %llums", 1656 (longlong_t)scn->scn_visited_this_txg, 1657 (longlong_t)NSEC2MSEC(gethrtime() - scn->scn_sync_start_time)); 1658 1659 if (!scn->scn_pausing) { 1660 scn->scn_done_txg = tx->tx_txg + 1; 1661 zfs_dbgmsg("txg %llu traversal complete, waiting till txg %llu", 1662 tx->tx_txg, scn->scn_done_txg); 1663 } 1664 1665 if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) { 1666 mutex_enter(&spa->spa_scrub_lock); 1667 while (spa->spa_scrub_inflight > 0) { 1668 cv_wait(&spa->spa_scrub_io_cv, 1669 &spa->spa_scrub_lock); 1670 } 1671 mutex_exit(&spa->spa_scrub_lock); 1672 } 1673 1674 dsl_scan_sync_state(scn, tx); 1675 } 1676 1677 /* 1678 * This will start a new scan, or restart an existing one. 1679 */ 1680 void 1681 dsl_resilver_restart(dsl_pool_t *dp, uint64_t txg) 1682 { 1683 if (txg == 0) { 1684 dmu_tx_t *tx; 1685 tx = dmu_tx_create_dd(dp->dp_mos_dir); 1686 VERIFY(0 == dmu_tx_assign(tx, TXG_WAIT)); 1687 1688 txg = dmu_tx_get_txg(tx); 1689 dp->dp_scan->scn_restart_txg = txg; 1690 dmu_tx_commit(tx); 1691 } else { 1692 dp->dp_scan->scn_restart_txg = txg; 1693 } 1694 zfs_dbgmsg("restarting resilver txg=%llu", txg); 1695 } 1696 1697 boolean_t 1698 dsl_scan_resilvering(dsl_pool_t *dp) 1699 { 1700 return (dp->dp_scan->scn_phys.scn_state == DSS_SCANNING && 1701 dp->dp_scan->scn_phys.scn_func == POOL_SCAN_RESILVER); 1702 } 1703 1704 /* 1705 * scrub consumers 1706 */ 1707 1708 static void 1709 count_block(zfs_all_blkstats_t *zab, const blkptr_t *bp) 1710 { 1711 int i; 1712 1713 /* 1714 * If we resume after a reboot, zab will be NULL; don't record 1715 * incomplete stats in that case. 1716 */ 1717 if (zab == NULL) 1718 return; 1719 1720 for (i = 0; i < 4; i++) { 1721 int l = (i < 2) ? BP_GET_LEVEL(bp) : DN_MAX_LEVELS; 1722 int t = (i & 1) ? BP_GET_TYPE(bp) : DMU_OT_TOTAL; 1723 if (t & DMU_OT_NEWTYPE) 1724 t = DMU_OT_OTHER; 1725 zfs_blkstat_t *zb = &zab->zab_type[l][t]; 1726 int equal; 1727 1728 zb->zb_count++; 1729 zb->zb_asize += BP_GET_ASIZE(bp); 1730 zb->zb_lsize += BP_GET_LSIZE(bp); 1731 zb->zb_psize += BP_GET_PSIZE(bp); 1732 zb->zb_gangs += BP_COUNT_GANG(bp); 1733 1734 switch (BP_GET_NDVAS(bp)) { 1735 case 2: 1736 if (DVA_GET_VDEV(&bp->blk_dva[0]) == 1737 DVA_GET_VDEV(&bp->blk_dva[1])) 1738 zb->zb_ditto_2_of_2_samevdev++; 1739 break; 1740 case 3: 1741 equal = (DVA_GET_VDEV(&bp->blk_dva[0]) == 1742 DVA_GET_VDEV(&bp->blk_dva[1])) + 1743 (DVA_GET_VDEV(&bp->blk_dva[0]) == 1744 DVA_GET_VDEV(&bp->blk_dva[2])) + 1745 (DVA_GET_VDEV(&bp->blk_dva[1]) == 1746 DVA_GET_VDEV(&bp->blk_dva[2])); 1747 if (equal == 1) 1748 zb->zb_ditto_2_of_3_samevdev++; 1749 else if (equal == 3) 1750 zb->zb_ditto_3_of_3_samevdev++; 1751 break; 1752 } 1753 } 1754 } 1755 1756 static void 1757 dsl_scan_scrub_done(zio_t *zio) 1758 { 1759 spa_t *spa = zio->io_spa; 1760 1761 abd_free(zio->io_abd); 1762 1763 mutex_enter(&spa->spa_scrub_lock); 1764 spa->spa_scrub_inflight--; 1765 cv_broadcast(&spa->spa_scrub_io_cv); 1766 1767 if (zio->io_error && (zio->io_error != ECKSUM || 1768 !(zio->io_flags & ZIO_FLAG_SPECULATIVE))) { 1769 spa->spa_dsl_pool->dp_scan->scn_phys.scn_errors++; 1770 } 1771 mutex_exit(&spa->spa_scrub_lock); 1772 } 1773 1774 static int 1775 dsl_scan_scrub_cb(dsl_pool_t *dp, 1776 const blkptr_t *bp, const zbookmark_phys_t *zb) 1777 { 1778 dsl_scan_t *scn = dp->dp_scan; 1779 size_t size = BP_GET_PSIZE(bp); 1780 spa_t *spa = dp->dp_spa; 1781 uint64_t phys_birth = BP_PHYSICAL_BIRTH(bp); 1782 boolean_t needs_io; 1783 int zio_flags = ZIO_FLAG_SCAN_THREAD | ZIO_FLAG_RAW | ZIO_FLAG_CANFAIL; 1784 int scan_delay = 0; 1785 1786 if (phys_birth <= scn->scn_phys.scn_min_txg || 1787 phys_birth >= scn->scn_phys.scn_max_txg) 1788 return (0); 1789 1790 count_block(dp->dp_blkstats, bp); 1791 1792 if (BP_IS_EMBEDDED(bp)) 1793 return (0); 1794 1795 ASSERT(DSL_SCAN_IS_SCRUB_RESILVER(scn)); 1796 if (scn->scn_phys.scn_func == POOL_SCAN_SCRUB) { 1797 zio_flags |= ZIO_FLAG_SCRUB; 1798 needs_io = B_TRUE; 1799 scan_delay = zfs_scrub_delay; 1800 } else { 1801 ASSERT3U(scn->scn_phys.scn_func, ==, POOL_SCAN_RESILVER); 1802 zio_flags |= ZIO_FLAG_RESILVER; 1803 needs_io = B_FALSE; 1804 scan_delay = zfs_resilver_delay; 1805 } 1806 1807 /* If it's an intent log block, failure is expected. */ 1808 if (zb->zb_level == ZB_ZIL_LEVEL) 1809 zio_flags |= ZIO_FLAG_SPECULATIVE; 1810 1811 for (int d = 0; d < BP_GET_NDVAS(bp); d++) { 1812 vdev_t *vd = vdev_lookup_top(spa, 1813 DVA_GET_VDEV(&bp->blk_dva[d])); 1814 1815 /* 1816 * Keep track of how much data we've examined so that 1817 * zpool(1M) status can make useful progress reports. 1818 */ 1819 scn->scn_phys.scn_examined += DVA_GET_ASIZE(&bp->blk_dva[d]); 1820 spa->spa_scan_pass_exam += DVA_GET_ASIZE(&bp->blk_dva[d]); 1821 1822 /* if it's a resilver, this may not be in the target range */ 1823 if (!needs_io) { 1824 if (DVA_GET_GANG(&bp->blk_dva[d])) { 1825 /* 1826 * Gang members may be spread across multiple 1827 * vdevs, so the best estimate we have is the 1828 * scrub range, which has already been checked. 1829 * XXX -- it would be better to change our 1830 * allocation policy to ensure that all 1831 * gang members reside on the same vdev. 1832 */ 1833 needs_io = B_TRUE; 1834 } else { 1835 needs_io = vdev_dtl_contains(vd, DTL_PARTIAL, 1836 phys_birth, 1); 1837 } 1838 } 1839 } 1840 1841 if (needs_io && !zfs_no_scrub_io) { 1842 vdev_t *rvd = spa->spa_root_vdev; 1843 uint64_t maxinflight = rvd->vdev_children * zfs_top_maxinflight; 1844 1845 mutex_enter(&spa->spa_scrub_lock); 1846 while (spa->spa_scrub_inflight >= maxinflight) 1847 cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 1848 spa->spa_scrub_inflight++; 1849 mutex_exit(&spa->spa_scrub_lock); 1850 1851 /* 1852 * If we're seeing recent (zfs_scan_idle) "important" I/Os 1853 * then throttle our workload to limit the impact of a scan. 1854 */ 1855 if (ddi_get_lbolt64() - spa->spa_last_io <= zfs_scan_idle) 1856 delay(scan_delay); 1857 1858 zio_nowait(zio_read(NULL, spa, bp, 1859 abd_alloc_for_io(size, B_FALSE), size, dsl_scan_scrub_done, 1860 NULL, ZIO_PRIORITY_SCRUB, zio_flags, zb)); 1861 } 1862 1863 /* do not relocate this block */ 1864 return (0); 1865 } 1866 1867 /* Called by the ZFS_IOC_POOL_SCAN ioctl to start a scrub or resilver */ 1868 int 1869 dsl_scan(dsl_pool_t *dp, pool_scan_func_t func) 1870 { 1871 spa_t *spa = dp->dp_spa; 1872 1873 /* 1874 * Purge all vdev caches and probe all devices. We do this here 1875 * rather than in sync context because this requires a writer lock 1876 * on the spa_config lock, which we can't do from sync context. The 1877 * spa_scrub_reopen flag indicates that vdev_open() should not 1878 * attempt to start another scrub. 1879 */ 1880 spa_vdev_state_enter(spa, SCL_NONE); 1881 spa->spa_scrub_reopen = B_TRUE; 1882 vdev_reopen(spa->spa_root_vdev); 1883 spa->spa_scrub_reopen = B_FALSE; 1884 (void) spa_vdev_state_exit(spa, NULL, 0); 1885 1886 return (dsl_sync_task(spa_name(spa), dsl_scan_setup_check, 1887 dsl_scan_setup_sync, &func, 0, ZFS_SPACE_CHECK_NONE)); 1888 } 1889 1890 static boolean_t 1891 dsl_scan_restarting(dsl_scan_t *scn, dmu_tx_t *tx) 1892 { 1893 return (scn->scn_restart_txg != 0 && 1894 scn->scn_restart_txg <= tx->tx_txg); 1895 } 1896