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