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