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