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