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