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 https://opensource.org/licenses/CDDL-1.0. 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, 2021 by Delphix. All rights reserved. 24 * Copyright 2016 Gary Mills 25 * Copyright (c) 2017, 2019, Datto Inc. All rights reserved. 26 * Copyright (c) 2015, Nexenta Systems, Inc. All rights reserved. 27 * Copyright 2019 Joyent, 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/arc_impl.h> 41 #include <sys/zap.h> 42 #include <sys/zio.h> 43 #include <sys/zfs_context.h> 44 #include <sys/fs/zfs.h> 45 #include <sys/zfs_znode.h> 46 #include <sys/spa_impl.h> 47 #include <sys/vdev_impl.h> 48 #include <sys/zil_impl.h> 49 #include <sys/zio_checksum.h> 50 #include <sys/brt.h> 51 #include <sys/ddt.h> 52 #include <sys/sa.h> 53 #include <sys/sa_impl.h> 54 #include <sys/zfeature.h> 55 #include <sys/abd.h> 56 #include <sys/range_tree.h> 57 #include <sys/dbuf.h> 58 #ifdef _KERNEL 59 #include <sys/zfs_vfsops.h> 60 #endif 61 62 /* 63 * Grand theory statement on scan queue sorting 64 * 65 * Scanning is implemented by recursively traversing all indirection levels 66 * in an object and reading all blocks referenced from said objects. This 67 * results in us approximately traversing the object from lowest logical 68 * offset to the highest. For best performance, we would want the logical 69 * blocks to be physically contiguous. However, this is frequently not the 70 * case with pools given the allocation patterns of copy-on-write filesystems. 71 * So instead, we put the I/Os into a reordering queue and issue them in a 72 * way that will most benefit physical disks (LBA-order). 73 * 74 * Queue management: 75 * 76 * Ideally, we would want to scan all metadata and queue up all block I/O 77 * prior to starting to issue it, because that allows us to do an optimal 78 * sorting job. This can however consume large amounts of memory. Therefore 79 * we continuously monitor the size of the queues and constrain them to 5% 80 * (zfs_scan_mem_lim_fact) of physmem. If the queues grow larger than this 81 * limit, we clear out a few of the largest extents at the head of the queues 82 * to make room for more scanning. Hopefully, these extents will be fairly 83 * large and contiguous, allowing us to approach sequential I/O throughput 84 * even without a fully sorted tree. 85 * 86 * Metadata scanning takes place in dsl_scan_visit(), which is called from 87 * dsl_scan_sync() every spa_sync(). If we have either fully scanned all 88 * metadata on the pool, or we need to make room in memory because our 89 * queues are too large, dsl_scan_visit() is postponed and 90 * scan_io_queues_run() is called from dsl_scan_sync() instead. This implies 91 * that metadata scanning and queued I/O issuing are mutually exclusive. This 92 * allows us to provide maximum sequential I/O throughput for the majority of 93 * I/O's issued since sequential I/O performance is significantly negatively 94 * impacted if it is interleaved with random I/O. 95 * 96 * Implementation Notes 97 * 98 * One side effect of the queued scanning algorithm is that the scanning code 99 * needs to be notified whenever a block is freed. This is needed to allow 100 * the scanning code to remove these I/Os from the issuing queue. Additionally, 101 * we do not attempt to queue gang blocks to be issued sequentially since this 102 * is very hard to do and would have an extremely limited performance benefit. 103 * Instead, we simply issue gang I/Os as soon as we find them using the legacy 104 * algorithm. 105 * 106 * Backwards compatibility 107 * 108 * This new algorithm is backwards compatible with the legacy on-disk data 109 * structures (and therefore does not require a new feature flag). 110 * Periodically during scanning (see zfs_scan_checkpoint_intval), the scan 111 * will stop scanning metadata (in logical order) and wait for all outstanding 112 * sorted I/O to complete. Once this is done, we write out a checkpoint 113 * bookmark, indicating that we have scanned everything logically before it. 114 * If the pool is imported on a machine without the new sorting algorithm, 115 * the scan simply resumes from the last checkpoint using the legacy algorithm. 116 */ 117 118 typedef int (scan_cb_t)(dsl_pool_t *, const blkptr_t *, 119 const zbookmark_phys_t *); 120 121 static scan_cb_t dsl_scan_scrub_cb; 122 123 static int scan_ds_queue_compare(const void *a, const void *b); 124 static int scan_prefetch_queue_compare(const void *a, const void *b); 125 static void scan_ds_queue_clear(dsl_scan_t *scn); 126 static void scan_ds_prefetch_queue_clear(dsl_scan_t *scn); 127 static boolean_t scan_ds_queue_contains(dsl_scan_t *scn, uint64_t dsobj, 128 uint64_t *txg); 129 static void scan_ds_queue_insert(dsl_scan_t *scn, uint64_t dsobj, uint64_t txg); 130 static void scan_ds_queue_remove(dsl_scan_t *scn, uint64_t dsobj); 131 static void scan_ds_queue_sync(dsl_scan_t *scn, dmu_tx_t *tx); 132 static uint64_t dsl_scan_count_data_disks(spa_t *spa); 133 static void read_by_block_level(dsl_scan_t *scn, zbookmark_phys_t zb); 134 135 extern uint_t zfs_vdev_async_write_active_min_dirty_percent; 136 static int zfs_scan_blkstats = 0; 137 138 /* 139 * 'zpool status' uses bytes processed per pass to report throughput and 140 * estimate time remaining. We define a pass to start when the scanning 141 * phase completes for a sequential resilver. Optionally, this value 142 * may be used to reset the pass statistics every N txgs to provide an 143 * estimated completion time based on currently observed performance. 144 */ 145 static uint_t zfs_scan_report_txgs = 0; 146 147 /* 148 * By default zfs will check to ensure it is not over the hard memory 149 * limit before each txg. If finer-grained control of this is needed 150 * this value can be set to 1 to enable checking before scanning each 151 * block. 152 */ 153 static int zfs_scan_strict_mem_lim = B_FALSE; 154 155 /* 156 * Maximum number of parallelly executed bytes per leaf vdev. We attempt 157 * to strike a balance here between keeping the vdev queues full of I/Os 158 * at all times and not overflowing the queues to cause long latency, 159 * which would cause long txg sync times. No matter what, we will not 160 * overload the drives with I/O, since that is protected by 161 * zfs_vdev_scrub_max_active. 162 */ 163 static uint64_t zfs_scan_vdev_limit = 16 << 20; 164 165 static uint_t zfs_scan_issue_strategy = 0; 166 167 /* don't queue & sort zios, go direct */ 168 static int zfs_scan_legacy = B_FALSE; 169 static uint64_t zfs_scan_max_ext_gap = 2 << 20; /* in bytes */ 170 171 /* 172 * fill_weight is non-tunable at runtime, so we copy it at module init from 173 * zfs_scan_fill_weight. Runtime adjustments to zfs_scan_fill_weight would 174 * break queue sorting. 175 */ 176 static uint_t zfs_scan_fill_weight = 3; 177 static uint64_t fill_weight; 178 179 /* See dsl_scan_should_clear() for details on the memory limit tunables */ 180 static const uint64_t zfs_scan_mem_lim_min = 16 << 20; /* bytes */ 181 static const uint64_t zfs_scan_mem_lim_soft_max = 128 << 20; /* bytes */ 182 183 184 /* fraction of physmem */ 185 static uint_t zfs_scan_mem_lim_fact = 20; 186 187 /* fraction of mem lim above */ 188 static uint_t zfs_scan_mem_lim_soft_fact = 20; 189 190 /* minimum milliseconds to scrub per txg */ 191 static uint_t zfs_scrub_min_time_ms = 1000; 192 193 /* minimum milliseconds to obsolete per txg */ 194 static uint_t zfs_obsolete_min_time_ms = 500; 195 196 /* minimum milliseconds to free per txg */ 197 static uint_t zfs_free_min_time_ms = 1000; 198 199 /* minimum milliseconds to resilver per txg */ 200 static uint_t zfs_resilver_min_time_ms = 3000; 201 202 static uint_t zfs_scan_checkpoint_intval = 7200; /* in seconds */ 203 int zfs_scan_suspend_progress = 0; /* set to prevent scans from progressing */ 204 static int zfs_no_scrub_io = B_FALSE; /* set to disable scrub i/o */ 205 static int zfs_no_scrub_prefetch = B_FALSE; /* set to disable scrub prefetch */ 206 static const ddt_class_t zfs_scrub_ddt_class_max = DDT_CLASS_DUPLICATE; 207 /* max number of blocks to free in a single TXG */ 208 static uint64_t zfs_async_block_max_blocks = UINT64_MAX; 209 /* max number of dedup blocks to free in a single TXG */ 210 static uint64_t zfs_max_async_dedup_frees = 100000; 211 212 /* set to disable resilver deferring */ 213 static int zfs_resilver_disable_defer = B_FALSE; 214 215 /* Don't defer a resilver if the one in progress only got this far: */ 216 static uint_t zfs_resilver_defer_percent = 10; 217 218 /* 219 * We wait a few txgs after importing a pool to begin scanning so that 220 * the import / mounting code isn't held up by scrub / resilver IO. 221 * Unfortunately, it is a bit difficult to determine exactly how long 222 * this will take since userspace will trigger fs mounts asynchronously 223 * and the kernel will create zvol minors asynchronously. As a result, 224 * the value provided here is a bit arbitrary, but represents a 225 * reasonable estimate of how many txgs it will take to finish fully 226 * importing a pool 227 */ 228 #define SCAN_IMPORT_WAIT_TXGS 5 229 230 #define DSL_SCAN_IS_SCRUB_RESILVER(scn) \ 231 ((scn)->scn_phys.scn_func == POOL_SCAN_SCRUB || \ 232 (scn)->scn_phys.scn_func == POOL_SCAN_RESILVER) 233 234 #define DSL_SCAN_IS_SCRUB(scn) \ 235 ((scn)->scn_phys.scn_func == POOL_SCAN_SCRUB) 236 237 /* 238 * Enable/disable the processing of the free_bpobj object. 239 */ 240 static int zfs_free_bpobj_enabled = 1; 241 242 /* Error blocks to be scrubbed in one txg. */ 243 static uint_t zfs_scrub_error_blocks_per_txg = 1 << 12; 244 245 /* the order has to match pool_scan_type */ 246 static scan_cb_t *scan_funcs[POOL_SCAN_FUNCS] = { 247 NULL, 248 dsl_scan_scrub_cb, /* POOL_SCAN_SCRUB */ 249 dsl_scan_scrub_cb, /* POOL_SCAN_RESILVER */ 250 }; 251 252 /* In core node for the scn->scn_queue. Represents a dataset to be scanned */ 253 typedef struct { 254 uint64_t sds_dsobj; 255 uint64_t sds_txg; 256 avl_node_t sds_node; 257 } scan_ds_t; 258 259 /* 260 * This controls what conditions are placed on dsl_scan_sync_state(): 261 * SYNC_OPTIONAL) write out scn_phys iff scn_queues_pending == 0 262 * SYNC_MANDATORY) write out scn_phys always. scn_queues_pending must be 0. 263 * SYNC_CACHED) if scn_queues_pending == 0, write out scn_phys. Otherwise 264 * write out the scn_phys_cached version. 265 * See dsl_scan_sync_state for details. 266 */ 267 typedef enum { 268 SYNC_OPTIONAL, 269 SYNC_MANDATORY, 270 SYNC_CACHED 271 } state_sync_type_t; 272 273 /* 274 * This struct represents the minimum information needed to reconstruct a 275 * zio for sequential scanning. This is useful because many of these will 276 * accumulate in the sequential IO queues before being issued, so saving 277 * memory matters here. 278 */ 279 typedef struct scan_io { 280 /* fields from blkptr_t */ 281 uint64_t sio_blk_prop; 282 uint64_t sio_phys_birth; 283 uint64_t sio_birth; 284 zio_cksum_t sio_cksum; 285 uint32_t sio_nr_dvas; 286 287 /* fields from zio_t */ 288 uint32_t sio_flags; 289 zbookmark_phys_t sio_zb; 290 291 /* members for queue sorting */ 292 union { 293 avl_node_t sio_addr_node; /* link into issuing queue */ 294 list_node_t sio_list_node; /* link for issuing to disk */ 295 } sio_nodes; 296 297 /* 298 * There may be up to SPA_DVAS_PER_BP DVAs here from the bp, 299 * depending on how many were in the original bp. Only the 300 * first DVA is really used for sorting and issuing purposes. 301 * The other DVAs (if provided) simply exist so that the zio 302 * layer can find additional copies to repair from in the 303 * event of an error. This array must go at the end of the 304 * struct to allow this for the variable number of elements. 305 */ 306 dva_t sio_dva[]; 307 } scan_io_t; 308 309 #define SIO_SET_OFFSET(sio, x) DVA_SET_OFFSET(&(sio)->sio_dva[0], x) 310 #define SIO_SET_ASIZE(sio, x) DVA_SET_ASIZE(&(sio)->sio_dva[0], x) 311 #define SIO_GET_OFFSET(sio) DVA_GET_OFFSET(&(sio)->sio_dva[0]) 312 #define SIO_GET_ASIZE(sio) DVA_GET_ASIZE(&(sio)->sio_dva[0]) 313 #define SIO_GET_END_OFFSET(sio) \ 314 (SIO_GET_OFFSET(sio) + SIO_GET_ASIZE(sio)) 315 #define SIO_GET_MUSED(sio) \ 316 (sizeof (scan_io_t) + ((sio)->sio_nr_dvas * sizeof (dva_t))) 317 318 struct dsl_scan_io_queue { 319 dsl_scan_t *q_scn; /* associated dsl_scan_t */ 320 vdev_t *q_vd; /* top-level vdev that this queue represents */ 321 zio_t *q_zio; /* scn_zio_root child for waiting on IO */ 322 323 /* trees used for sorting I/Os and extents of I/Os */ 324 zfs_range_tree_t *q_exts_by_addr; 325 zfs_btree_t q_exts_by_size; 326 avl_tree_t q_sios_by_addr; 327 uint64_t q_sio_memused; 328 uint64_t q_last_ext_addr; 329 330 /* members for zio rate limiting */ 331 uint64_t q_maxinflight_bytes; 332 uint64_t q_inflight_bytes; 333 kcondvar_t q_zio_cv; /* used under vd->vdev_scan_io_queue_lock */ 334 335 /* per txg statistics */ 336 uint64_t q_total_seg_size_this_txg; 337 uint64_t q_segs_this_txg; 338 uint64_t q_total_zio_size_this_txg; 339 uint64_t q_zios_this_txg; 340 }; 341 342 /* private data for dsl_scan_prefetch_cb() */ 343 typedef struct scan_prefetch_ctx { 344 zfs_refcount_t spc_refcnt; /* refcount for memory management */ 345 dsl_scan_t *spc_scn; /* dsl_scan_t for the pool */ 346 boolean_t spc_root; /* is this prefetch for an objset? */ 347 uint8_t spc_indblkshift; /* dn_indblkshift of current dnode */ 348 uint16_t spc_datablkszsec; /* dn_idatablkszsec of current dnode */ 349 } scan_prefetch_ctx_t; 350 351 /* private data for dsl_scan_prefetch() */ 352 typedef struct scan_prefetch_issue_ctx { 353 avl_node_t spic_avl_node; /* link into scn->scn_prefetch_queue */ 354 scan_prefetch_ctx_t *spic_spc; /* spc for the callback */ 355 blkptr_t spic_bp; /* bp to prefetch */ 356 zbookmark_phys_t spic_zb; /* bookmark to prefetch */ 357 } scan_prefetch_issue_ctx_t; 358 359 static void scan_exec_io(dsl_pool_t *dp, const blkptr_t *bp, int zio_flags, 360 const zbookmark_phys_t *zb, dsl_scan_io_queue_t *queue); 361 static void scan_io_queue_insert_impl(dsl_scan_io_queue_t *queue, 362 scan_io_t *sio); 363 364 static dsl_scan_io_queue_t *scan_io_queue_create(vdev_t *vd); 365 static void scan_io_queues_destroy(dsl_scan_t *scn); 366 367 static kmem_cache_t *sio_cache[SPA_DVAS_PER_BP]; 368 369 /* sio->sio_nr_dvas must be set so we know which cache to free from */ 370 static void 371 sio_free(scan_io_t *sio) 372 { 373 ASSERT3U(sio->sio_nr_dvas, >, 0); 374 ASSERT3U(sio->sio_nr_dvas, <=, SPA_DVAS_PER_BP); 375 376 kmem_cache_free(sio_cache[sio->sio_nr_dvas - 1], sio); 377 } 378 379 /* It is up to the caller to set sio->sio_nr_dvas for freeing */ 380 static scan_io_t * 381 sio_alloc(unsigned short nr_dvas) 382 { 383 ASSERT3U(nr_dvas, >, 0); 384 ASSERT3U(nr_dvas, <=, SPA_DVAS_PER_BP); 385 386 return (kmem_cache_alloc(sio_cache[nr_dvas - 1], KM_SLEEP)); 387 } 388 389 void 390 scan_init(void) 391 { 392 /* 393 * This is used in ext_size_compare() to weight segments 394 * based on how sparse they are. This cannot be changed 395 * mid-scan and the tree comparison functions don't currently 396 * have a mechanism for passing additional context to the 397 * compare functions. Thus we store this value globally and 398 * we only allow it to be set at module initialization time 399 */ 400 fill_weight = zfs_scan_fill_weight; 401 402 for (int i = 0; i < SPA_DVAS_PER_BP; i++) { 403 char name[36]; 404 405 (void) snprintf(name, sizeof (name), "sio_cache_%d", i); 406 sio_cache[i] = kmem_cache_create(name, 407 (sizeof (scan_io_t) + ((i + 1) * sizeof (dva_t))), 408 0, NULL, NULL, NULL, NULL, NULL, 0); 409 } 410 } 411 412 void 413 scan_fini(void) 414 { 415 for (int i = 0; i < SPA_DVAS_PER_BP; i++) { 416 kmem_cache_destroy(sio_cache[i]); 417 } 418 } 419 420 static inline boolean_t 421 dsl_scan_is_running(const dsl_scan_t *scn) 422 { 423 return (scn->scn_phys.scn_state == DSS_SCANNING); 424 } 425 426 boolean_t 427 dsl_scan_resilvering(dsl_pool_t *dp) 428 { 429 return (dsl_scan_is_running(dp->dp_scan) && 430 dp->dp_scan->scn_phys.scn_func == POOL_SCAN_RESILVER); 431 } 432 433 static inline void 434 sio2bp(const scan_io_t *sio, blkptr_t *bp) 435 { 436 memset(bp, 0, sizeof (*bp)); 437 bp->blk_prop = sio->sio_blk_prop; 438 BP_SET_PHYSICAL_BIRTH(bp, sio->sio_phys_birth); 439 BP_SET_LOGICAL_BIRTH(bp, sio->sio_birth); 440 bp->blk_fill = 1; /* we always only work with data pointers */ 441 bp->blk_cksum = sio->sio_cksum; 442 443 ASSERT3U(sio->sio_nr_dvas, >, 0); 444 ASSERT3U(sio->sio_nr_dvas, <=, SPA_DVAS_PER_BP); 445 446 memcpy(bp->blk_dva, sio->sio_dva, sio->sio_nr_dvas * sizeof (dva_t)); 447 } 448 449 static inline void 450 bp2sio(const blkptr_t *bp, scan_io_t *sio, int dva_i) 451 { 452 sio->sio_blk_prop = bp->blk_prop; 453 sio->sio_phys_birth = BP_GET_PHYSICAL_BIRTH(bp); 454 sio->sio_birth = BP_GET_LOGICAL_BIRTH(bp); 455 sio->sio_cksum = bp->blk_cksum; 456 sio->sio_nr_dvas = BP_GET_NDVAS(bp); 457 458 /* 459 * Copy the DVAs to the sio. We need all copies of the block so 460 * that the self healing code can use the alternate copies if the 461 * first is corrupted. We want the DVA at index dva_i to be first 462 * in the sio since this is the primary one that we want to issue. 463 */ 464 for (int i = 0, j = dva_i; i < sio->sio_nr_dvas; i++, j++) { 465 sio->sio_dva[i] = bp->blk_dva[j % sio->sio_nr_dvas]; 466 } 467 } 468 469 int 470 dsl_scan_init(dsl_pool_t *dp, uint64_t txg) 471 { 472 int err; 473 dsl_scan_t *scn; 474 spa_t *spa = dp->dp_spa; 475 uint64_t f; 476 477 scn = dp->dp_scan = kmem_zalloc(sizeof (dsl_scan_t), KM_SLEEP); 478 scn->scn_dp = dp; 479 480 /* 481 * It's possible that we're resuming a scan after a reboot so 482 * make sure that the scan_async_destroying flag is initialized 483 * appropriately. 484 */ 485 ASSERT(!scn->scn_async_destroying); 486 scn->scn_async_destroying = spa_feature_is_active(dp->dp_spa, 487 SPA_FEATURE_ASYNC_DESTROY); 488 489 /* 490 * Calculate the max number of in-flight bytes for pool-wide 491 * scanning operations (minimum 1MB, maximum 1/4 of arc_c_max). 492 * Limits for the issuing phase are done per top-level vdev and 493 * are handled separately. 494 */ 495 scn->scn_maxinflight_bytes = MIN(arc_c_max / 4, MAX(1ULL << 20, 496 zfs_scan_vdev_limit * dsl_scan_count_data_disks(spa))); 497 498 avl_create(&scn->scn_queue, scan_ds_queue_compare, sizeof (scan_ds_t), 499 offsetof(scan_ds_t, sds_node)); 500 mutex_init(&scn->scn_queue_lock, NULL, MUTEX_DEFAULT, NULL); 501 avl_create(&scn->scn_prefetch_queue, scan_prefetch_queue_compare, 502 sizeof (scan_prefetch_issue_ctx_t), 503 offsetof(scan_prefetch_issue_ctx_t, spic_avl_node)); 504 505 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 506 "scrub_func", sizeof (uint64_t), 1, &f); 507 if (err == 0) { 508 /* 509 * There was an old-style scrub in progress. Restart a 510 * new-style scrub from the beginning. 511 */ 512 scn->scn_restart_txg = txg; 513 zfs_dbgmsg("old-style scrub was in progress for %s; " 514 "restarting new-style scrub in txg %llu", 515 spa->spa_name, 516 (longlong_t)scn->scn_restart_txg); 517 518 /* 519 * Load the queue obj from the old location so that it 520 * can be freed by dsl_scan_done(). 521 */ 522 (void) zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 523 "scrub_queue", sizeof (uint64_t), 1, 524 &scn->scn_phys.scn_queue_obj); 525 } else { 526 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 527 DMU_POOL_ERRORSCRUB, sizeof (uint64_t), 528 ERRORSCRUB_PHYS_NUMINTS, &scn->errorscrub_phys); 529 530 if (err != 0 && err != ENOENT) 531 return (err); 532 533 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 534 DMU_POOL_SCAN, sizeof (uint64_t), SCAN_PHYS_NUMINTS, 535 &scn->scn_phys); 536 537 /* 538 * Detect if the pool contains the signature of #2094. If it 539 * does properly update the scn->scn_phys structure and notify 540 * the administrator by setting an errata for the pool. 541 */ 542 if (err == EOVERFLOW) { 543 uint64_t zaptmp[SCAN_PHYS_NUMINTS + 1]; 544 VERIFY3S(SCAN_PHYS_NUMINTS, ==, 24); 545 VERIFY3S(offsetof(dsl_scan_phys_t, scn_flags), ==, 546 (23 * sizeof (uint64_t))); 547 548 err = zap_lookup(dp->dp_meta_objset, 549 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SCAN, 550 sizeof (uint64_t), SCAN_PHYS_NUMINTS + 1, &zaptmp); 551 if (err == 0) { 552 uint64_t overflow = zaptmp[SCAN_PHYS_NUMINTS]; 553 554 if (overflow & ~DSL_SCAN_FLAGS_MASK || 555 scn->scn_async_destroying) { 556 spa->spa_errata = 557 ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY; 558 return (EOVERFLOW); 559 } 560 561 memcpy(&scn->scn_phys, zaptmp, 562 SCAN_PHYS_NUMINTS * sizeof (uint64_t)); 563 scn->scn_phys.scn_flags = overflow; 564 565 /* Required scrub already in progress. */ 566 if (scn->scn_phys.scn_state == DSS_FINISHED || 567 scn->scn_phys.scn_state == DSS_CANCELED) 568 spa->spa_errata = 569 ZPOOL_ERRATA_ZOL_2094_SCRUB; 570 } 571 } 572 573 if (err == ENOENT) 574 return (0); 575 else if (err) 576 return (err); 577 578 /* 579 * We might be restarting after a reboot, so jump the issued 580 * counter to how far we've scanned. We know we're consistent 581 * up to here. 582 */ 583 scn->scn_issued_before_pass = scn->scn_phys.scn_examined - 584 scn->scn_phys.scn_skipped; 585 586 if (dsl_scan_is_running(scn) && 587 spa_prev_software_version(dp->dp_spa) < SPA_VERSION_SCAN) { 588 /* 589 * A new-type scrub was in progress on an old 590 * pool, and the pool was accessed by old 591 * software. Restart from the beginning, since 592 * the old software may have changed the pool in 593 * the meantime. 594 */ 595 scn->scn_restart_txg = txg; 596 zfs_dbgmsg("new-style scrub for %s was modified " 597 "by old software; restarting in txg %llu", 598 spa->spa_name, 599 (longlong_t)scn->scn_restart_txg); 600 } else if (dsl_scan_resilvering(dp)) { 601 /* 602 * If a resilver is in progress and there are already 603 * errors, restart it instead of finishing this scan and 604 * then restarting it. If there haven't been any errors 605 * then remember that the incore DTL is valid. 606 */ 607 if (scn->scn_phys.scn_errors > 0) { 608 scn->scn_restart_txg = txg; 609 zfs_dbgmsg("resilver can't excise DTL_MISSING " 610 "when finished; restarting on %s in txg " 611 "%llu", 612 spa->spa_name, 613 (u_longlong_t)scn->scn_restart_txg); 614 } else { 615 /* it's safe to excise DTL when finished */ 616 spa->spa_scrub_started = B_TRUE; 617 } 618 } 619 } 620 621 memcpy(&scn->scn_phys_cached, &scn->scn_phys, sizeof (scn->scn_phys)); 622 623 /* reload the queue into the in-core state */ 624 if (scn->scn_phys.scn_queue_obj != 0) { 625 zap_cursor_t zc; 626 zap_attribute_t *za = zap_attribute_alloc(); 627 628 for (zap_cursor_init(&zc, dp->dp_meta_objset, 629 scn->scn_phys.scn_queue_obj); 630 zap_cursor_retrieve(&zc, za) == 0; 631 (void) zap_cursor_advance(&zc)) { 632 scan_ds_queue_insert(scn, 633 zfs_strtonum(za->za_name, NULL), 634 za->za_first_integer); 635 } 636 zap_cursor_fini(&zc); 637 zap_attribute_free(za); 638 } 639 640 ddt_walk_init(spa, scn->scn_phys.scn_max_txg); 641 642 spa_scan_stat_init(spa); 643 vdev_scan_stat_init(spa->spa_root_vdev); 644 645 return (0); 646 } 647 648 void 649 dsl_scan_fini(dsl_pool_t *dp) 650 { 651 if (dp->dp_scan != NULL) { 652 dsl_scan_t *scn = dp->dp_scan; 653 654 if (scn->scn_taskq != NULL) 655 taskq_destroy(scn->scn_taskq); 656 657 scan_ds_queue_clear(scn); 658 avl_destroy(&scn->scn_queue); 659 mutex_destroy(&scn->scn_queue_lock); 660 scan_ds_prefetch_queue_clear(scn); 661 avl_destroy(&scn->scn_prefetch_queue); 662 663 kmem_free(dp->dp_scan, sizeof (dsl_scan_t)); 664 dp->dp_scan = NULL; 665 } 666 } 667 668 static boolean_t 669 dsl_scan_restarting(dsl_scan_t *scn, dmu_tx_t *tx) 670 { 671 return (scn->scn_restart_txg != 0 && 672 scn->scn_restart_txg <= tx->tx_txg); 673 } 674 675 boolean_t 676 dsl_scan_resilver_scheduled(dsl_pool_t *dp) 677 { 678 return ((dp->dp_scan && dp->dp_scan->scn_restart_txg != 0) || 679 (spa_async_tasks(dp->dp_spa) & SPA_ASYNC_RESILVER)); 680 } 681 682 boolean_t 683 dsl_scan_scrubbing(const dsl_pool_t *dp) 684 { 685 dsl_scan_phys_t *scn_phys = &dp->dp_scan->scn_phys; 686 687 return (scn_phys->scn_state == DSS_SCANNING && 688 scn_phys->scn_func == POOL_SCAN_SCRUB); 689 } 690 691 boolean_t 692 dsl_errorscrubbing(const dsl_pool_t *dp) 693 { 694 dsl_errorscrub_phys_t *errorscrub_phys = &dp->dp_scan->errorscrub_phys; 695 696 return (errorscrub_phys->dep_state == DSS_ERRORSCRUBBING && 697 errorscrub_phys->dep_func == POOL_SCAN_ERRORSCRUB); 698 } 699 700 boolean_t 701 dsl_errorscrub_is_paused(const dsl_scan_t *scn) 702 { 703 return (dsl_errorscrubbing(scn->scn_dp) && 704 scn->errorscrub_phys.dep_paused_flags); 705 } 706 707 boolean_t 708 dsl_scan_is_paused_scrub(const dsl_scan_t *scn) 709 { 710 return (dsl_scan_scrubbing(scn->scn_dp) && 711 scn->scn_phys.scn_flags & DSF_SCRUB_PAUSED); 712 } 713 714 static void 715 dsl_errorscrub_sync_state(dsl_scan_t *scn, dmu_tx_t *tx) 716 { 717 scn->errorscrub_phys.dep_cursor = 718 zap_cursor_serialize(&scn->errorscrub_cursor); 719 720 VERIFY0(zap_update(scn->scn_dp->dp_meta_objset, 721 DMU_POOL_DIRECTORY_OBJECT, 722 DMU_POOL_ERRORSCRUB, sizeof (uint64_t), ERRORSCRUB_PHYS_NUMINTS, 723 &scn->errorscrub_phys, tx)); 724 } 725 726 static void 727 dsl_errorscrub_setup_sync(void *arg, dmu_tx_t *tx) 728 { 729 dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan; 730 pool_scan_func_t *funcp = arg; 731 dsl_pool_t *dp = scn->scn_dp; 732 spa_t *spa = dp->dp_spa; 733 734 ASSERT(!dsl_scan_is_running(scn)); 735 ASSERT(!dsl_errorscrubbing(scn->scn_dp)); 736 ASSERT(*funcp > POOL_SCAN_NONE && *funcp < POOL_SCAN_FUNCS); 737 738 memset(&scn->errorscrub_phys, 0, sizeof (scn->errorscrub_phys)); 739 scn->errorscrub_phys.dep_func = *funcp; 740 scn->errorscrub_phys.dep_state = DSS_ERRORSCRUBBING; 741 scn->errorscrub_phys.dep_start_time = gethrestime_sec(); 742 scn->errorscrub_phys.dep_to_examine = spa_get_last_errlog_size(spa); 743 scn->errorscrub_phys.dep_examined = 0; 744 scn->errorscrub_phys.dep_errors = 0; 745 scn->errorscrub_phys.dep_cursor = 0; 746 zap_cursor_init_serialized(&scn->errorscrub_cursor, 747 spa->spa_meta_objset, spa->spa_errlog_last, 748 scn->errorscrub_phys.dep_cursor); 749 750 vdev_config_dirty(spa->spa_root_vdev); 751 spa_event_notify(spa, NULL, NULL, ESC_ZFS_ERRORSCRUB_START); 752 753 dsl_errorscrub_sync_state(scn, tx); 754 755 spa_history_log_internal(spa, "error scrub setup", tx, 756 "func=%u mintxg=%u maxtxg=%llu", 757 *funcp, 0, (u_longlong_t)tx->tx_txg); 758 } 759 760 static int 761 dsl_errorscrub_setup_check(void *arg, dmu_tx_t *tx) 762 { 763 (void) arg; 764 dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan; 765 766 if (dsl_scan_is_running(scn) || (dsl_errorscrubbing(scn->scn_dp))) { 767 return (SET_ERROR(EBUSY)); 768 } 769 770 if (spa_get_last_errlog_size(scn->scn_dp->dp_spa) == 0) { 771 return (ECANCELED); 772 } 773 return (0); 774 } 775 776 /* 777 * Writes out a persistent dsl_scan_phys_t record to the pool directory. 778 * Because we can be running in the block sorting algorithm, we do not always 779 * want to write out the record, only when it is "safe" to do so. This safety 780 * condition is achieved by making sure that the sorting queues are empty 781 * (scn_queues_pending == 0). When this condition is not true, the sync'd state 782 * is inconsistent with how much actual scanning progress has been made. The 783 * kind of sync to be performed is specified by the sync_type argument. If the 784 * sync is optional, we only sync if the queues are empty. If the sync is 785 * mandatory, we do a hard ASSERT to make sure that the queues are empty. The 786 * third possible state is a "cached" sync. This is done in response to: 787 * 1) The dataset that was in the last sync'd dsl_scan_phys_t having been 788 * destroyed, so we wouldn't be able to restart scanning from it. 789 * 2) The snapshot that was in the last sync'd dsl_scan_phys_t having been 790 * superseded by a newer snapshot. 791 * 3) The dataset that was in the last sync'd dsl_scan_phys_t having been 792 * swapped with its clone. 793 * In all cases, a cached sync simply rewrites the last record we've written, 794 * just slightly modified. For the modifications that are performed to the 795 * last written dsl_scan_phys_t, see dsl_scan_ds_destroyed, 796 * dsl_scan_ds_snapshotted and dsl_scan_ds_clone_swapped. 797 */ 798 static void 799 dsl_scan_sync_state(dsl_scan_t *scn, dmu_tx_t *tx, state_sync_type_t sync_type) 800 { 801 int i; 802 spa_t *spa = scn->scn_dp->dp_spa; 803 804 ASSERT(sync_type != SYNC_MANDATORY || scn->scn_queues_pending == 0); 805 if (scn->scn_queues_pending == 0) { 806 for (i = 0; i < spa->spa_root_vdev->vdev_children; i++) { 807 vdev_t *vd = spa->spa_root_vdev->vdev_child[i]; 808 dsl_scan_io_queue_t *q = vd->vdev_scan_io_queue; 809 810 if (q == NULL) 811 continue; 812 813 mutex_enter(&vd->vdev_scan_io_queue_lock); 814 ASSERT3P(avl_first(&q->q_sios_by_addr), ==, NULL); 815 ASSERT3P(zfs_btree_first(&q->q_exts_by_size, NULL), ==, 816 NULL); 817 ASSERT3P(zfs_range_tree_first(q->q_exts_by_addr), ==, 818 NULL); 819 mutex_exit(&vd->vdev_scan_io_queue_lock); 820 } 821 822 if (scn->scn_phys.scn_queue_obj != 0) 823 scan_ds_queue_sync(scn, tx); 824 VERIFY0(zap_update(scn->scn_dp->dp_meta_objset, 825 DMU_POOL_DIRECTORY_OBJECT, 826 DMU_POOL_SCAN, sizeof (uint64_t), SCAN_PHYS_NUMINTS, 827 &scn->scn_phys, tx)); 828 memcpy(&scn->scn_phys_cached, &scn->scn_phys, 829 sizeof (scn->scn_phys)); 830 831 if (scn->scn_checkpointing) 832 zfs_dbgmsg("finish scan checkpoint for %s", 833 spa->spa_name); 834 835 scn->scn_checkpointing = B_FALSE; 836 scn->scn_last_checkpoint = ddi_get_lbolt(); 837 } else if (sync_type == SYNC_CACHED) { 838 VERIFY0(zap_update(scn->scn_dp->dp_meta_objset, 839 DMU_POOL_DIRECTORY_OBJECT, 840 DMU_POOL_SCAN, sizeof (uint64_t), SCAN_PHYS_NUMINTS, 841 &scn->scn_phys_cached, tx)); 842 } 843 } 844 845 int 846 dsl_scan_setup_check(void *arg, dmu_tx_t *tx) 847 { 848 (void) arg; 849 dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan; 850 vdev_t *rvd = scn->scn_dp->dp_spa->spa_root_vdev; 851 852 if (dsl_scan_is_running(scn) || vdev_rebuild_active(rvd) || 853 dsl_errorscrubbing(scn->scn_dp)) 854 return (SET_ERROR(EBUSY)); 855 856 return (0); 857 } 858 859 void 860 dsl_scan_setup_sync(void *arg, dmu_tx_t *tx) 861 { 862 setup_sync_arg_t *setup_sync_arg = (setup_sync_arg_t *)arg; 863 dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan; 864 dmu_object_type_t ot = 0; 865 dsl_pool_t *dp = scn->scn_dp; 866 spa_t *spa = dp->dp_spa; 867 868 ASSERT(!dsl_scan_is_running(scn)); 869 ASSERT3U(setup_sync_arg->func, >, POOL_SCAN_NONE); 870 ASSERT3U(setup_sync_arg->func, <, POOL_SCAN_FUNCS); 871 memset(&scn->scn_phys, 0, sizeof (scn->scn_phys)); 872 873 /* 874 * If we are starting a fresh scrub, we erase the error scrub 875 * information from disk. 876 */ 877 memset(&scn->errorscrub_phys, 0, sizeof (scn->errorscrub_phys)); 878 dsl_errorscrub_sync_state(scn, tx); 879 880 scn->scn_phys.scn_func = setup_sync_arg->func; 881 scn->scn_phys.scn_state = DSS_SCANNING; 882 scn->scn_phys.scn_min_txg = setup_sync_arg->txgstart; 883 if (setup_sync_arg->txgend == 0) { 884 scn->scn_phys.scn_max_txg = tx->tx_txg; 885 } else { 886 scn->scn_phys.scn_max_txg = setup_sync_arg->txgend; 887 } 888 scn->scn_phys.scn_ddt_class_max = DDT_CLASSES - 1; /* the entire DDT */ 889 scn->scn_phys.scn_start_time = gethrestime_sec(); 890 scn->scn_phys.scn_errors = 0; 891 scn->scn_phys.scn_to_examine = spa->spa_root_vdev->vdev_stat.vs_alloc; 892 scn->scn_issued_before_pass = 0; 893 scn->scn_restart_txg = 0; 894 scn->scn_done_txg = 0; 895 scn->scn_last_checkpoint = 0; 896 scn->scn_checkpointing = B_FALSE; 897 spa_scan_stat_init(spa); 898 vdev_scan_stat_init(spa->spa_root_vdev); 899 900 if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) { 901 scn->scn_phys.scn_ddt_class_max = zfs_scrub_ddt_class_max; 902 903 /* rewrite all disk labels */ 904 vdev_config_dirty(spa->spa_root_vdev); 905 906 if (vdev_resilver_needed(spa->spa_root_vdev, 907 &scn->scn_phys.scn_min_txg, &scn->scn_phys.scn_max_txg)) { 908 nvlist_t *aux = fnvlist_alloc(); 909 fnvlist_add_string(aux, ZFS_EV_RESILVER_TYPE, 910 "healing"); 911 spa_event_notify(spa, NULL, aux, 912 ESC_ZFS_RESILVER_START); 913 nvlist_free(aux); 914 } else { 915 spa_event_notify(spa, NULL, NULL, ESC_ZFS_SCRUB_START); 916 } 917 918 spa->spa_scrub_started = B_TRUE; 919 /* 920 * If this is an incremental scrub, limit the DDT scrub phase 921 * to just the auto-ditto class (for correctness); the rest 922 * of the scrub should go faster using top-down pruning. 923 */ 924 if (scn->scn_phys.scn_min_txg > TXG_INITIAL) 925 scn->scn_phys.scn_ddt_class_max = DDT_CLASS_DITTO; 926 927 /* 928 * When starting a resilver clear any existing rebuild state. 929 * This is required to prevent stale rebuild status from 930 * being reported when a rebuild is run, then a resilver and 931 * finally a scrub. In which case only the scrub status 932 * should be reported by 'zpool status'. 933 */ 934 if (scn->scn_phys.scn_func == POOL_SCAN_RESILVER) { 935 vdev_t *rvd = spa->spa_root_vdev; 936 for (uint64_t i = 0; i < rvd->vdev_children; i++) { 937 vdev_t *vd = rvd->vdev_child[i]; 938 vdev_rebuild_clear_sync( 939 (void *)(uintptr_t)vd->vdev_id, tx); 940 } 941 } 942 } 943 944 /* back to the generic stuff */ 945 946 if (zfs_scan_blkstats) { 947 if (dp->dp_blkstats == NULL) { 948 dp->dp_blkstats = 949 vmem_alloc(sizeof (zfs_all_blkstats_t), KM_SLEEP); 950 } 951 memset(&dp->dp_blkstats->zab_type, 0, 952 sizeof (dp->dp_blkstats->zab_type)); 953 } else { 954 if (dp->dp_blkstats) { 955 vmem_free(dp->dp_blkstats, sizeof (zfs_all_blkstats_t)); 956 dp->dp_blkstats = NULL; 957 } 958 } 959 960 if (spa_version(spa) < SPA_VERSION_DSL_SCRUB) 961 ot = DMU_OT_ZAP_OTHER; 962 963 scn->scn_phys.scn_queue_obj = zap_create(dp->dp_meta_objset, 964 ot ? ot : DMU_OT_SCAN_QUEUE, DMU_OT_NONE, 0, tx); 965 966 memcpy(&scn->scn_phys_cached, &scn->scn_phys, sizeof (scn->scn_phys)); 967 968 ddt_walk_init(spa, scn->scn_phys.scn_max_txg); 969 970 dsl_scan_sync_state(scn, tx, SYNC_MANDATORY); 971 972 spa_history_log_internal(spa, "scan setup", tx, 973 "func=%u mintxg=%llu maxtxg=%llu", 974 setup_sync_arg->func, (u_longlong_t)scn->scn_phys.scn_min_txg, 975 (u_longlong_t)scn->scn_phys.scn_max_txg); 976 } 977 978 /* 979 * Called by ZFS_IOC_POOL_SCRUB and ZFS_IOC_POOL_SCAN ioctl to start a scrub, 980 * error scrub or resilver. Can also be called to resume a paused scrub or 981 * error scrub. 982 */ 983 int 984 dsl_scan(dsl_pool_t *dp, pool_scan_func_t func, uint64_t txgstart, 985 uint64_t txgend) 986 { 987 spa_t *spa = dp->dp_spa; 988 dsl_scan_t *scn = dp->dp_scan; 989 setup_sync_arg_t setup_sync_arg; 990 991 if (func != POOL_SCAN_SCRUB && (txgstart != 0 || txgend != 0)) { 992 return (EINVAL); 993 } 994 995 /* 996 * Purge all vdev caches and probe all devices. We do this here 997 * rather than in sync context because this requires a writer lock 998 * on the spa_config lock, which we can't do from sync context. The 999 * spa_scrub_reopen flag indicates that vdev_open() should not 1000 * attempt to start another scrub. 1001 */ 1002 spa_vdev_state_enter(spa, SCL_NONE); 1003 spa->spa_scrub_reopen = B_TRUE; 1004 vdev_reopen(spa->spa_root_vdev); 1005 spa->spa_scrub_reopen = B_FALSE; 1006 (void) spa_vdev_state_exit(spa, NULL, 0); 1007 1008 if (func == POOL_SCAN_RESILVER) { 1009 dsl_scan_restart_resilver(spa->spa_dsl_pool, 0); 1010 return (0); 1011 } 1012 1013 if (func == POOL_SCAN_ERRORSCRUB) { 1014 if (dsl_errorscrub_is_paused(dp->dp_scan)) { 1015 /* 1016 * got error scrub start cmd, resume paused error scrub. 1017 */ 1018 int err = dsl_scrub_set_pause_resume(scn->scn_dp, 1019 POOL_SCRUB_NORMAL); 1020 if (err == 0) { 1021 spa_event_notify(spa, NULL, NULL, 1022 ESC_ZFS_ERRORSCRUB_RESUME); 1023 return (ECANCELED); 1024 } 1025 return (SET_ERROR(err)); 1026 } 1027 1028 return (dsl_sync_task(spa_name(dp->dp_spa), 1029 dsl_errorscrub_setup_check, dsl_errorscrub_setup_sync, 1030 &func, 0, ZFS_SPACE_CHECK_RESERVED)); 1031 } 1032 1033 if (func == POOL_SCAN_SCRUB && dsl_scan_is_paused_scrub(scn)) { 1034 /* got scrub start cmd, resume paused scrub */ 1035 int err = dsl_scrub_set_pause_resume(scn->scn_dp, 1036 POOL_SCRUB_NORMAL); 1037 if (err == 0) { 1038 spa_event_notify(spa, NULL, NULL, ESC_ZFS_SCRUB_RESUME); 1039 return (SET_ERROR(ECANCELED)); 1040 } 1041 return (SET_ERROR(err)); 1042 } 1043 1044 setup_sync_arg.func = func; 1045 setup_sync_arg.txgstart = txgstart; 1046 setup_sync_arg.txgend = txgend; 1047 1048 return (dsl_sync_task(spa_name(spa), dsl_scan_setup_check, 1049 dsl_scan_setup_sync, &setup_sync_arg, 0, 1050 ZFS_SPACE_CHECK_EXTRA_RESERVED)); 1051 } 1052 1053 static void 1054 dsl_errorscrub_done(dsl_scan_t *scn, boolean_t complete, dmu_tx_t *tx) 1055 { 1056 dsl_pool_t *dp = scn->scn_dp; 1057 spa_t *spa = dp->dp_spa; 1058 1059 if (complete) { 1060 spa_event_notify(spa, NULL, NULL, ESC_ZFS_ERRORSCRUB_FINISH); 1061 spa_history_log_internal(spa, "error scrub done", tx, 1062 "errors=%llu", (u_longlong_t)spa_approx_errlog_size(spa)); 1063 } else { 1064 spa_history_log_internal(spa, "error scrub canceled", tx, 1065 "errors=%llu", (u_longlong_t)spa_approx_errlog_size(spa)); 1066 } 1067 1068 scn->errorscrub_phys.dep_state = complete ? DSS_FINISHED : DSS_CANCELED; 1069 spa->spa_scrub_active = B_FALSE; 1070 spa_errlog_rotate(spa); 1071 scn->errorscrub_phys.dep_end_time = gethrestime_sec(); 1072 zap_cursor_fini(&scn->errorscrub_cursor); 1073 1074 if (spa->spa_errata == ZPOOL_ERRATA_ZOL_2094_SCRUB) 1075 spa->spa_errata = 0; 1076 1077 ASSERT(!dsl_errorscrubbing(scn->scn_dp)); 1078 } 1079 1080 static void 1081 dsl_scan_done(dsl_scan_t *scn, boolean_t complete, dmu_tx_t *tx) 1082 { 1083 static const char *old_names[] = { 1084 "scrub_bookmark", 1085 "scrub_ddt_bookmark", 1086 "scrub_ddt_class_max", 1087 "scrub_queue", 1088 "scrub_min_txg", 1089 "scrub_max_txg", 1090 "scrub_func", 1091 "scrub_errors", 1092 NULL 1093 }; 1094 1095 dsl_pool_t *dp = scn->scn_dp; 1096 spa_t *spa = dp->dp_spa; 1097 int i; 1098 1099 /* Remove any remnants of an old-style scrub. */ 1100 for (i = 0; old_names[i]; i++) { 1101 (void) zap_remove(dp->dp_meta_objset, 1102 DMU_POOL_DIRECTORY_OBJECT, old_names[i], tx); 1103 } 1104 1105 if (scn->scn_phys.scn_queue_obj != 0) { 1106 VERIFY0(dmu_object_free(dp->dp_meta_objset, 1107 scn->scn_phys.scn_queue_obj, tx)); 1108 scn->scn_phys.scn_queue_obj = 0; 1109 } 1110 scan_ds_queue_clear(scn); 1111 scan_ds_prefetch_queue_clear(scn); 1112 1113 scn->scn_phys.scn_flags &= ~DSF_SCRUB_PAUSED; 1114 1115 /* 1116 * If we were "restarted" from a stopped state, don't bother 1117 * with anything else. 1118 */ 1119 if (!dsl_scan_is_running(scn)) { 1120 ASSERT(!scn->scn_is_sorted); 1121 return; 1122 } 1123 1124 if (scn->scn_is_sorted) { 1125 scan_io_queues_destroy(scn); 1126 scn->scn_is_sorted = B_FALSE; 1127 1128 if (scn->scn_taskq != NULL) { 1129 taskq_destroy(scn->scn_taskq); 1130 scn->scn_taskq = NULL; 1131 } 1132 } 1133 1134 scn->scn_phys.scn_state = complete ? DSS_FINISHED : DSS_CANCELED; 1135 1136 spa_notify_waiters(spa); 1137 1138 if (dsl_scan_restarting(scn, tx)) { 1139 spa_history_log_internal(spa, "scan aborted, restarting", tx, 1140 "errors=%llu", (u_longlong_t)spa_approx_errlog_size(spa)); 1141 } else if (!complete) { 1142 spa_history_log_internal(spa, "scan cancelled", tx, 1143 "errors=%llu", (u_longlong_t)spa_approx_errlog_size(spa)); 1144 } else { 1145 spa_history_log_internal(spa, "scan done", tx, 1146 "errors=%llu", (u_longlong_t)spa_approx_errlog_size(spa)); 1147 if (DSL_SCAN_IS_SCRUB(scn)) { 1148 VERIFY0(zap_update(dp->dp_meta_objset, 1149 DMU_POOL_DIRECTORY_OBJECT, 1150 DMU_POOL_LAST_SCRUBBED_TXG, 1151 sizeof (uint64_t), 1, 1152 &scn->scn_phys.scn_max_txg, tx)); 1153 spa->spa_scrubbed_last_txg = scn->scn_phys.scn_max_txg; 1154 } 1155 } 1156 1157 if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) { 1158 spa->spa_scrub_active = B_FALSE; 1159 1160 /* 1161 * If the scrub/resilver completed, update all DTLs to 1162 * reflect this. Whether it succeeded or not, vacate 1163 * all temporary scrub DTLs. 1164 * 1165 * As the scrub does not currently support traversing 1166 * data that have been freed but are part of a checkpoint, 1167 * we don't mark the scrub as done in the DTLs as faults 1168 * may still exist in those vdevs. 1169 */ 1170 if (complete && 1171 !spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) { 1172 vdev_dtl_reassess(spa->spa_root_vdev, tx->tx_txg, 1173 scn->scn_phys.scn_max_txg, B_TRUE, B_FALSE); 1174 1175 if (scn->scn_phys.scn_min_txg) { 1176 nvlist_t *aux = fnvlist_alloc(); 1177 fnvlist_add_string(aux, ZFS_EV_RESILVER_TYPE, 1178 "healing"); 1179 spa_event_notify(spa, NULL, aux, 1180 ESC_ZFS_RESILVER_FINISH); 1181 nvlist_free(aux); 1182 } else { 1183 spa_event_notify(spa, NULL, NULL, 1184 ESC_ZFS_SCRUB_FINISH); 1185 } 1186 } else { 1187 vdev_dtl_reassess(spa->spa_root_vdev, tx->tx_txg, 1188 0, B_TRUE, B_FALSE); 1189 } 1190 spa_errlog_rotate(spa); 1191 1192 /* 1193 * Don't clear flag until after vdev_dtl_reassess to ensure that 1194 * DTL_MISSING will get updated when possible. 1195 */ 1196 spa->spa_scrub_started = B_FALSE; 1197 1198 /* 1199 * We may have finished replacing a device. 1200 * Let the async thread assess this and handle the detach. 1201 */ 1202 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 1203 1204 /* 1205 * Clear any resilver_deferred flags in the config. 1206 * If there are drives that need resilvering, kick 1207 * off an asynchronous request to start resilver. 1208 * vdev_clear_resilver_deferred() may update the config 1209 * before the resilver can restart. In the event of 1210 * a crash during this period, the spa loading code 1211 * will find the drives that need to be resilvered 1212 * and start the resilver then. 1213 */ 1214 if (spa_feature_is_enabled(spa, SPA_FEATURE_RESILVER_DEFER) && 1215 vdev_clear_resilver_deferred(spa->spa_root_vdev, tx)) { 1216 spa_history_log_internal(spa, 1217 "starting deferred resilver", tx, "errors=%llu", 1218 (u_longlong_t)spa_approx_errlog_size(spa)); 1219 spa_async_request(spa, SPA_ASYNC_RESILVER); 1220 } 1221 1222 /* Clear recent error events (i.e. duplicate events tracking) */ 1223 if (complete) 1224 zfs_ereport_clear(spa, NULL); 1225 } 1226 1227 scn->scn_phys.scn_end_time = gethrestime_sec(); 1228 1229 if (spa->spa_errata == ZPOOL_ERRATA_ZOL_2094_SCRUB) 1230 spa->spa_errata = 0; 1231 1232 ASSERT(!dsl_scan_is_running(scn)); 1233 } 1234 1235 static int 1236 dsl_errorscrub_pause_resume_check(void *arg, dmu_tx_t *tx) 1237 { 1238 pool_scrub_cmd_t *cmd = arg; 1239 dsl_pool_t *dp = dmu_tx_pool(tx); 1240 dsl_scan_t *scn = dp->dp_scan; 1241 1242 if (*cmd == POOL_SCRUB_PAUSE) { 1243 /* 1244 * can't pause a error scrub when there is no in-progress 1245 * error scrub. 1246 */ 1247 if (!dsl_errorscrubbing(dp)) 1248 return (SET_ERROR(ENOENT)); 1249 1250 /* can't pause a paused error scrub */ 1251 if (dsl_errorscrub_is_paused(scn)) 1252 return (SET_ERROR(EBUSY)); 1253 } else if (*cmd != POOL_SCRUB_NORMAL) { 1254 return (SET_ERROR(ENOTSUP)); 1255 } 1256 1257 return (0); 1258 } 1259 1260 static void 1261 dsl_errorscrub_pause_resume_sync(void *arg, dmu_tx_t *tx) 1262 { 1263 pool_scrub_cmd_t *cmd = arg; 1264 dsl_pool_t *dp = dmu_tx_pool(tx); 1265 spa_t *spa = dp->dp_spa; 1266 dsl_scan_t *scn = dp->dp_scan; 1267 1268 if (*cmd == POOL_SCRUB_PAUSE) { 1269 spa->spa_scan_pass_errorscrub_pause = gethrestime_sec(); 1270 scn->errorscrub_phys.dep_paused_flags = B_TRUE; 1271 dsl_errorscrub_sync_state(scn, tx); 1272 spa_event_notify(spa, NULL, NULL, ESC_ZFS_ERRORSCRUB_PAUSED); 1273 } else { 1274 ASSERT3U(*cmd, ==, POOL_SCRUB_NORMAL); 1275 if (dsl_errorscrub_is_paused(scn)) { 1276 /* 1277 * We need to keep track of how much time we spend 1278 * paused per pass so that we can adjust the error scrub 1279 * rate shown in the output of 'zpool status'. 1280 */ 1281 spa->spa_scan_pass_errorscrub_spent_paused += 1282 gethrestime_sec() - 1283 spa->spa_scan_pass_errorscrub_pause; 1284 1285 spa->spa_scan_pass_errorscrub_pause = 0; 1286 scn->errorscrub_phys.dep_paused_flags = B_FALSE; 1287 1288 zap_cursor_init_serialized( 1289 &scn->errorscrub_cursor, 1290 spa->spa_meta_objset, spa->spa_errlog_last, 1291 scn->errorscrub_phys.dep_cursor); 1292 1293 dsl_errorscrub_sync_state(scn, tx); 1294 } 1295 } 1296 } 1297 1298 static int 1299 dsl_errorscrub_cancel_check(void *arg, dmu_tx_t *tx) 1300 { 1301 (void) arg; 1302 dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan; 1303 /* can't cancel a error scrub when there is no one in-progress */ 1304 if (!dsl_errorscrubbing(scn->scn_dp)) 1305 return (SET_ERROR(ENOENT)); 1306 return (0); 1307 } 1308 1309 static void 1310 dsl_errorscrub_cancel_sync(void *arg, dmu_tx_t *tx) 1311 { 1312 (void) arg; 1313 dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan; 1314 1315 dsl_errorscrub_done(scn, B_FALSE, tx); 1316 dsl_errorscrub_sync_state(scn, tx); 1317 spa_event_notify(scn->scn_dp->dp_spa, NULL, NULL, 1318 ESC_ZFS_ERRORSCRUB_ABORT); 1319 } 1320 1321 static int 1322 dsl_scan_cancel_check(void *arg, dmu_tx_t *tx) 1323 { 1324 (void) arg; 1325 dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan; 1326 1327 if (!dsl_scan_is_running(scn)) 1328 return (SET_ERROR(ENOENT)); 1329 return (0); 1330 } 1331 1332 static void 1333 dsl_scan_cancel_sync(void *arg, dmu_tx_t *tx) 1334 { 1335 (void) arg; 1336 dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan; 1337 1338 dsl_scan_done(scn, B_FALSE, tx); 1339 dsl_scan_sync_state(scn, tx, SYNC_MANDATORY); 1340 spa_event_notify(scn->scn_dp->dp_spa, NULL, NULL, ESC_ZFS_SCRUB_ABORT); 1341 } 1342 1343 int 1344 dsl_scan_cancel(dsl_pool_t *dp) 1345 { 1346 if (dsl_errorscrubbing(dp)) { 1347 return (dsl_sync_task(spa_name(dp->dp_spa), 1348 dsl_errorscrub_cancel_check, dsl_errorscrub_cancel_sync, 1349 NULL, 3, ZFS_SPACE_CHECK_RESERVED)); 1350 } 1351 return (dsl_sync_task(spa_name(dp->dp_spa), dsl_scan_cancel_check, 1352 dsl_scan_cancel_sync, NULL, 3, ZFS_SPACE_CHECK_RESERVED)); 1353 } 1354 1355 static int 1356 dsl_scrub_pause_resume_check(void *arg, dmu_tx_t *tx) 1357 { 1358 pool_scrub_cmd_t *cmd = arg; 1359 dsl_pool_t *dp = dmu_tx_pool(tx); 1360 dsl_scan_t *scn = dp->dp_scan; 1361 1362 if (*cmd == POOL_SCRUB_PAUSE) { 1363 /* can't pause a scrub when there is no in-progress scrub */ 1364 if (!dsl_scan_scrubbing(dp)) 1365 return (SET_ERROR(ENOENT)); 1366 1367 /* can't pause a paused scrub */ 1368 if (dsl_scan_is_paused_scrub(scn)) 1369 return (SET_ERROR(EBUSY)); 1370 } else if (*cmd != POOL_SCRUB_NORMAL) { 1371 return (SET_ERROR(ENOTSUP)); 1372 } 1373 1374 return (0); 1375 } 1376 1377 static void 1378 dsl_scrub_pause_resume_sync(void *arg, dmu_tx_t *tx) 1379 { 1380 pool_scrub_cmd_t *cmd = arg; 1381 dsl_pool_t *dp = dmu_tx_pool(tx); 1382 spa_t *spa = dp->dp_spa; 1383 dsl_scan_t *scn = dp->dp_scan; 1384 1385 if (*cmd == POOL_SCRUB_PAUSE) { 1386 /* can't pause a scrub when there is no in-progress scrub */ 1387 spa->spa_scan_pass_scrub_pause = gethrestime_sec(); 1388 scn->scn_phys.scn_flags |= DSF_SCRUB_PAUSED; 1389 scn->scn_phys_cached.scn_flags |= DSF_SCRUB_PAUSED; 1390 dsl_scan_sync_state(scn, tx, SYNC_CACHED); 1391 spa_event_notify(spa, NULL, NULL, ESC_ZFS_SCRUB_PAUSED); 1392 spa_notify_waiters(spa); 1393 } else { 1394 ASSERT3U(*cmd, ==, POOL_SCRUB_NORMAL); 1395 if (dsl_scan_is_paused_scrub(scn)) { 1396 /* 1397 * We need to keep track of how much time we spend 1398 * paused per pass so that we can adjust the scrub rate 1399 * shown in the output of 'zpool status' 1400 */ 1401 spa->spa_scan_pass_scrub_spent_paused += 1402 gethrestime_sec() - spa->spa_scan_pass_scrub_pause; 1403 spa->spa_scan_pass_scrub_pause = 0; 1404 scn->scn_phys.scn_flags &= ~DSF_SCRUB_PAUSED; 1405 scn->scn_phys_cached.scn_flags &= ~DSF_SCRUB_PAUSED; 1406 dsl_scan_sync_state(scn, tx, SYNC_CACHED); 1407 } 1408 } 1409 } 1410 1411 /* 1412 * Set scrub pause/resume state if it makes sense to do so 1413 */ 1414 int 1415 dsl_scrub_set_pause_resume(const dsl_pool_t *dp, pool_scrub_cmd_t cmd) 1416 { 1417 if (dsl_errorscrubbing(dp)) { 1418 return (dsl_sync_task(spa_name(dp->dp_spa), 1419 dsl_errorscrub_pause_resume_check, 1420 dsl_errorscrub_pause_resume_sync, &cmd, 3, 1421 ZFS_SPACE_CHECK_RESERVED)); 1422 } 1423 return (dsl_sync_task(spa_name(dp->dp_spa), 1424 dsl_scrub_pause_resume_check, dsl_scrub_pause_resume_sync, &cmd, 3, 1425 ZFS_SPACE_CHECK_RESERVED)); 1426 } 1427 1428 1429 /* start a new scan, or restart an existing one. */ 1430 void 1431 dsl_scan_restart_resilver(dsl_pool_t *dp, uint64_t txg) 1432 { 1433 if (txg == 0) { 1434 dmu_tx_t *tx; 1435 tx = dmu_tx_create_dd(dp->dp_mos_dir); 1436 VERIFY(0 == dmu_tx_assign(tx, TXG_WAIT)); 1437 1438 txg = dmu_tx_get_txg(tx); 1439 dp->dp_scan->scn_restart_txg = txg; 1440 dmu_tx_commit(tx); 1441 } else { 1442 dp->dp_scan->scn_restart_txg = txg; 1443 } 1444 zfs_dbgmsg("restarting resilver for %s at txg=%llu", 1445 dp->dp_spa->spa_name, (longlong_t)txg); 1446 } 1447 1448 void 1449 dsl_free(dsl_pool_t *dp, uint64_t txg, const blkptr_t *bp) 1450 { 1451 zio_free(dp->dp_spa, txg, bp); 1452 } 1453 1454 void 1455 dsl_free_sync(zio_t *pio, dsl_pool_t *dp, uint64_t txg, const blkptr_t *bpp) 1456 { 1457 ASSERT(dsl_pool_sync_context(dp)); 1458 zio_nowait(zio_free_sync(pio, dp->dp_spa, txg, bpp, pio->io_flags)); 1459 } 1460 1461 static int 1462 scan_ds_queue_compare(const void *a, const void *b) 1463 { 1464 const scan_ds_t *sds_a = a, *sds_b = b; 1465 1466 if (sds_a->sds_dsobj < sds_b->sds_dsobj) 1467 return (-1); 1468 if (sds_a->sds_dsobj == sds_b->sds_dsobj) 1469 return (0); 1470 return (1); 1471 } 1472 1473 static void 1474 scan_ds_queue_clear(dsl_scan_t *scn) 1475 { 1476 void *cookie = NULL; 1477 scan_ds_t *sds; 1478 while ((sds = avl_destroy_nodes(&scn->scn_queue, &cookie)) != NULL) { 1479 kmem_free(sds, sizeof (*sds)); 1480 } 1481 } 1482 1483 static boolean_t 1484 scan_ds_queue_contains(dsl_scan_t *scn, uint64_t dsobj, uint64_t *txg) 1485 { 1486 scan_ds_t srch, *sds; 1487 1488 srch.sds_dsobj = dsobj; 1489 sds = avl_find(&scn->scn_queue, &srch, NULL); 1490 if (sds != NULL && txg != NULL) 1491 *txg = sds->sds_txg; 1492 return (sds != NULL); 1493 } 1494 1495 static void 1496 scan_ds_queue_insert(dsl_scan_t *scn, uint64_t dsobj, uint64_t txg) 1497 { 1498 scan_ds_t *sds; 1499 avl_index_t where; 1500 1501 sds = kmem_zalloc(sizeof (*sds), KM_SLEEP); 1502 sds->sds_dsobj = dsobj; 1503 sds->sds_txg = txg; 1504 1505 VERIFY3P(avl_find(&scn->scn_queue, sds, &where), ==, NULL); 1506 avl_insert(&scn->scn_queue, sds, where); 1507 } 1508 1509 static void 1510 scan_ds_queue_remove(dsl_scan_t *scn, uint64_t dsobj) 1511 { 1512 scan_ds_t srch, *sds; 1513 1514 srch.sds_dsobj = dsobj; 1515 1516 sds = avl_find(&scn->scn_queue, &srch, NULL); 1517 VERIFY(sds != NULL); 1518 avl_remove(&scn->scn_queue, sds); 1519 kmem_free(sds, sizeof (*sds)); 1520 } 1521 1522 static void 1523 scan_ds_queue_sync(dsl_scan_t *scn, dmu_tx_t *tx) 1524 { 1525 dsl_pool_t *dp = scn->scn_dp; 1526 spa_t *spa = dp->dp_spa; 1527 dmu_object_type_t ot = (spa_version(spa) >= SPA_VERSION_DSL_SCRUB) ? 1528 DMU_OT_SCAN_QUEUE : DMU_OT_ZAP_OTHER; 1529 1530 ASSERT0(scn->scn_queues_pending); 1531 ASSERT(scn->scn_phys.scn_queue_obj != 0); 1532 1533 VERIFY0(dmu_object_free(dp->dp_meta_objset, 1534 scn->scn_phys.scn_queue_obj, tx)); 1535 scn->scn_phys.scn_queue_obj = zap_create(dp->dp_meta_objset, ot, 1536 DMU_OT_NONE, 0, tx); 1537 for (scan_ds_t *sds = avl_first(&scn->scn_queue); 1538 sds != NULL; sds = AVL_NEXT(&scn->scn_queue, sds)) { 1539 VERIFY0(zap_add_int_key(dp->dp_meta_objset, 1540 scn->scn_phys.scn_queue_obj, sds->sds_dsobj, 1541 sds->sds_txg, tx)); 1542 } 1543 } 1544 1545 /* 1546 * Computes the memory limit state that we're currently in. A sorted scan 1547 * needs quite a bit of memory to hold the sorting queue, so we need to 1548 * reasonably constrain the size so it doesn't impact overall system 1549 * performance. We compute two limits: 1550 * 1) Hard memory limit: if the amount of memory used by the sorting 1551 * queues on a pool gets above this value, we stop the metadata 1552 * scanning portion and start issuing the queued up and sorted 1553 * I/Os to reduce memory usage. 1554 * This limit is calculated as a fraction of physmem (by default 5%). 1555 * We constrain the lower bound of the hard limit to an absolute 1556 * minimum of zfs_scan_mem_lim_min (default: 16 MiB). We also constrain 1557 * the upper bound to 5% of the total pool size - no chance we'll 1558 * ever need that much memory, but just to keep the value in check. 1559 * 2) Soft memory limit: once we hit the hard memory limit, we start 1560 * issuing I/O to reduce queue memory usage, but we don't want to 1561 * completely empty out the queues, since we might be able to find I/Os 1562 * that will fill in the gaps of our non-sequential IOs at some point 1563 * in the future. So we stop the issuing of I/Os once the amount of 1564 * memory used drops below the soft limit (at which point we stop issuing 1565 * I/O and start scanning metadata again). 1566 * 1567 * This limit is calculated by subtracting a fraction of the hard 1568 * limit from the hard limit. By default this fraction is 5%, so 1569 * the soft limit is 95% of the hard limit. We cap the size of the 1570 * difference between the hard and soft limits at an absolute 1571 * maximum of zfs_scan_mem_lim_soft_max (default: 128 MiB) - this is 1572 * sufficient to not cause too frequent switching between the 1573 * metadata scan and I/O issue (even at 2k recordsize, 128 MiB's 1574 * worth of queues is about 1.2 GiB of on-pool data, so scanning 1575 * that should take at least a decent fraction of a second). 1576 */ 1577 static boolean_t 1578 dsl_scan_should_clear(dsl_scan_t *scn) 1579 { 1580 spa_t *spa = scn->scn_dp->dp_spa; 1581 vdev_t *rvd = scn->scn_dp->dp_spa->spa_root_vdev; 1582 uint64_t alloc, mlim_hard, mlim_soft, mused; 1583 1584 alloc = metaslab_class_get_alloc(spa_normal_class(spa)); 1585 alloc += metaslab_class_get_alloc(spa_special_class(spa)); 1586 alloc += metaslab_class_get_alloc(spa_dedup_class(spa)); 1587 1588 mlim_hard = MAX((physmem / zfs_scan_mem_lim_fact) * PAGESIZE, 1589 zfs_scan_mem_lim_min); 1590 mlim_hard = MIN(mlim_hard, alloc / 20); 1591 mlim_soft = mlim_hard - MIN(mlim_hard / zfs_scan_mem_lim_soft_fact, 1592 zfs_scan_mem_lim_soft_max); 1593 mused = 0; 1594 for (uint64_t i = 0; i < rvd->vdev_children; i++) { 1595 vdev_t *tvd = rvd->vdev_child[i]; 1596 dsl_scan_io_queue_t *queue; 1597 1598 mutex_enter(&tvd->vdev_scan_io_queue_lock); 1599 queue = tvd->vdev_scan_io_queue; 1600 if (queue != NULL) { 1601 /* 1602 * # of extents in exts_by_addr = # in exts_by_size. 1603 * B-tree efficiency is ~75%, but can be as low as 50%. 1604 */ 1605 mused += zfs_btree_numnodes(&queue->q_exts_by_size) * (( 1606 sizeof (zfs_range_seg_gap_t) + sizeof (uint64_t)) * 1607 3 / 2) + queue->q_sio_memused; 1608 } 1609 mutex_exit(&tvd->vdev_scan_io_queue_lock); 1610 } 1611 1612 dprintf("current scan memory usage: %llu bytes\n", (longlong_t)mused); 1613 1614 if (mused == 0) 1615 ASSERT0(scn->scn_queues_pending); 1616 1617 /* 1618 * If we are above our hard limit, we need to clear out memory. 1619 * If we are below our soft limit, we need to accumulate sequential IOs. 1620 * Otherwise, we should keep doing whatever we are currently doing. 1621 */ 1622 if (mused >= mlim_hard) 1623 return (B_TRUE); 1624 else if (mused < mlim_soft) 1625 return (B_FALSE); 1626 else 1627 return (scn->scn_clearing); 1628 } 1629 1630 static boolean_t 1631 dsl_scan_check_suspend(dsl_scan_t *scn, const zbookmark_phys_t *zb) 1632 { 1633 /* we never skip user/group accounting objects */ 1634 if (zb && (int64_t)zb->zb_object < 0) 1635 return (B_FALSE); 1636 1637 if (scn->scn_suspending) 1638 return (B_TRUE); /* we're already suspending */ 1639 1640 if (!ZB_IS_ZERO(&scn->scn_phys.scn_bookmark)) 1641 return (B_FALSE); /* we're resuming */ 1642 1643 /* We only know how to resume from level-0 and objset blocks. */ 1644 if (zb && (zb->zb_level != 0 && zb->zb_level != ZB_ROOT_LEVEL)) 1645 return (B_FALSE); 1646 1647 /* 1648 * We suspend if: 1649 * - we have scanned for at least the minimum time (default 1 sec 1650 * for scrub, 3 sec for resilver), and either we have sufficient 1651 * dirty data that we are starting to write more quickly 1652 * (default 30%), someone is explicitly waiting for this txg 1653 * to complete, or we have used up all of the time in the txg 1654 * timeout (default 5 sec). 1655 * or 1656 * - the spa is shutting down because this pool is being exported 1657 * or the machine is rebooting. 1658 * or 1659 * - the scan queue has reached its memory use limit 1660 */ 1661 uint64_t curr_time_ns = gethrtime(); 1662 uint64_t scan_time_ns = curr_time_ns - scn->scn_sync_start_time; 1663 uint64_t sync_time_ns = curr_time_ns - 1664 scn->scn_dp->dp_spa->spa_sync_starttime; 1665 uint64_t dirty_min_bytes = zfs_dirty_data_max * 1666 zfs_vdev_async_write_active_min_dirty_percent / 100; 1667 uint_t mintime = (scn->scn_phys.scn_func == POOL_SCAN_RESILVER) ? 1668 zfs_resilver_min_time_ms : zfs_scrub_min_time_ms; 1669 1670 if ((NSEC2MSEC(scan_time_ns) > mintime && 1671 (scn->scn_dp->dp_dirty_total >= dirty_min_bytes || 1672 txg_sync_waiting(scn->scn_dp) || 1673 NSEC2SEC(sync_time_ns) >= zfs_txg_timeout)) || 1674 spa_shutting_down(scn->scn_dp->dp_spa) || 1675 (zfs_scan_strict_mem_lim && dsl_scan_should_clear(scn)) || 1676 !ddt_walk_ready(scn->scn_dp->dp_spa)) { 1677 if (zb && zb->zb_level == ZB_ROOT_LEVEL) { 1678 dprintf("suspending at first available bookmark " 1679 "%llx/%llx/%llx/%llx\n", 1680 (longlong_t)zb->zb_objset, 1681 (longlong_t)zb->zb_object, 1682 (longlong_t)zb->zb_level, 1683 (longlong_t)zb->zb_blkid); 1684 SET_BOOKMARK(&scn->scn_phys.scn_bookmark, 1685 zb->zb_objset, 0, 0, 0); 1686 } else if (zb != NULL) { 1687 dprintf("suspending at bookmark %llx/%llx/%llx/%llx\n", 1688 (longlong_t)zb->zb_objset, 1689 (longlong_t)zb->zb_object, 1690 (longlong_t)zb->zb_level, 1691 (longlong_t)zb->zb_blkid); 1692 scn->scn_phys.scn_bookmark = *zb; 1693 } else { 1694 #ifdef ZFS_DEBUG 1695 dsl_scan_phys_t *scnp = &scn->scn_phys; 1696 dprintf("suspending at at DDT bookmark " 1697 "%llx/%llx/%llx/%llx\n", 1698 (longlong_t)scnp->scn_ddt_bookmark.ddb_class, 1699 (longlong_t)scnp->scn_ddt_bookmark.ddb_type, 1700 (longlong_t)scnp->scn_ddt_bookmark.ddb_checksum, 1701 (longlong_t)scnp->scn_ddt_bookmark.ddb_cursor); 1702 #endif 1703 } 1704 scn->scn_suspending = B_TRUE; 1705 return (B_TRUE); 1706 } 1707 return (B_FALSE); 1708 } 1709 1710 static boolean_t 1711 dsl_error_scrub_check_suspend(dsl_scan_t *scn, const zbookmark_phys_t *zb) 1712 { 1713 /* 1714 * We suspend if: 1715 * - we have scrubbed for at least the minimum time (default 1 sec 1716 * for error scrub), someone is explicitly waiting for this txg 1717 * to complete, or we have used up all of the time in the txg 1718 * timeout (default 5 sec). 1719 * or 1720 * - the spa is shutting down because this pool is being exported 1721 * or the machine is rebooting. 1722 */ 1723 uint64_t curr_time_ns = gethrtime(); 1724 uint64_t error_scrub_time_ns = curr_time_ns - scn->scn_sync_start_time; 1725 uint64_t sync_time_ns = curr_time_ns - 1726 scn->scn_dp->dp_spa->spa_sync_starttime; 1727 int mintime = zfs_scrub_min_time_ms; 1728 1729 if ((NSEC2MSEC(error_scrub_time_ns) > mintime && 1730 (txg_sync_waiting(scn->scn_dp) || 1731 NSEC2SEC(sync_time_ns) >= zfs_txg_timeout)) || 1732 spa_shutting_down(scn->scn_dp->dp_spa)) { 1733 if (zb) { 1734 dprintf("error scrub suspending at bookmark " 1735 "%llx/%llx/%llx/%llx\n", 1736 (longlong_t)zb->zb_objset, 1737 (longlong_t)zb->zb_object, 1738 (longlong_t)zb->zb_level, 1739 (longlong_t)zb->zb_blkid); 1740 } 1741 return (B_TRUE); 1742 } 1743 return (B_FALSE); 1744 } 1745 1746 typedef struct zil_scan_arg { 1747 dsl_pool_t *zsa_dp; 1748 zil_header_t *zsa_zh; 1749 } zil_scan_arg_t; 1750 1751 static int 1752 dsl_scan_zil_block(zilog_t *zilog, const blkptr_t *bp, void *arg, 1753 uint64_t claim_txg) 1754 { 1755 (void) zilog; 1756 zil_scan_arg_t *zsa = arg; 1757 dsl_pool_t *dp = zsa->zsa_dp; 1758 dsl_scan_t *scn = dp->dp_scan; 1759 zil_header_t *zh = zsa->zsa_zh; 1760 zbookmark_phys_t zb; 1761 1762 ASSERT(!BP_IS_REDACTED(bp)); 1763 if (BP_IS_HOLE(bp) || 1764 BP_GET_LOGICAL_BIRTH(bp) <= scn->scn_phys.scn_cur_min_txg) 1765 return (0); 1766 1767 /* 1768 * One block ("stubby") can be allocated a long time ago; we 1769 * want to visit that one because it has been allocated 1770 * (on-disk) even if it hasn't been claimed (even though for 1771 * scrub there's nothing to do to it). 1772 */ 1773 if (claim_txg == 0 && 1774 BP_GET_LOGICAL_BIRTH(bp) >= spa_min_claim_txg(dp->dp_spa)) 1775 return (0); 1776 1777 SET_BOOKMARK(&zb, zh->zh_log.blk_cksum.zc_word[ZIL_ZC_OBJSET], 1778 ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, bp->blk_cksum.zc_word[ZIL_ZC_SEQ]); 1779 1780 VERIFY(0 == scan_funcs[scn->scn_phys.scn_func](dp, bp, &zb)); 1781 return (0); 1782 } 1783 1784 static int 1785 dsl_scan_zil_record(zilog_t *zilog, const lr_t *lrc, void *arg, 1786 uint64_t claim_txg) 1787 { 1788 (void) zilog; 1789 if (lrc->lrc_txtype == TX_WRITE) { 1790 zil_scan_arg_t *zsa = arg; 1791 dsl_pool_t *dp = zsa->zsa_dp; 1792 dsl_scan_t *scn = dp->dp_scan; 1793 zil_header_t *zh = zsa->zsa_zh; 1794 const lr_write_t *lr = (const lr_write_t *)lrc; 1795 const blkptr_t *bp = &lr->lr_blkptr; 1796 zbookmark_phys_t zb; 1797 1798 ASSERT(!BP_IS_REDACTED(bp)); 1799 if (BP_IS_HOLE(bp) || 1800 BP_GET_LOGICAL_BIRTH(bp) <= scn->scn_phys.scn_cur_min_txg) 1801 return (0); 1802 1803 /* 1804 * birth can be < claim_txg if this record's txg is 1805 * already txg sync'ed (but this log block contains 1806 * other records that are not synced) 1807 */ 1808 if (claim_txg == 0 || BP_GET_LOGICAL_BIRTH(bp) < claim_txg) 1809 return (0); 1810 1811 ASSERT3U(BP_GET_LSIZE(bp), !=, 0); 1812 SET_BOOKMARK(&zb, zh->zh_log.blk_cksum.zc_word[ZIL_ZC_OBJSET], 1813 lr->lr_foid, ZB_ZIL_LEVEL, 1814 lr->lr_offset / BP_GET_LSIZE(bp)); 1815 1816 VERIFY(0 == scan_funcs[scn->scn_phys.scn_func](dp, bp, &zb)); 1817 } 1818 return (0); 1819 } 1820 1821 static void 1822 dsl_scan_zil(dsl_pool_t *dp, zil_header_t *zh) 1823 { 1824 uint64_t claim_txg = zh->zh_claim_txg; 1825 zil_scan_arg_t zsa = { dp, zh }; 1826 zilog_t *zilog; 1827 1828 ASSERT(spa_writeable(dp->dp_spa)); 1829 1830 /* 1831 * We only want to visit blocks that have been claimed but not yet 1832 * replayed (or, in read-only mode, blocks that *would* be claimed). 1833 */ 1834 if (claim_txg == 0) 1835 return; 1836 1837 zilog = zil_alloc(dp->dp_meta_objset, zh); 1838 1839 (void) zil_parse(zilog, dsl_scan_zil_block, dsl_scan_zil_record, &zsa, 1840 claim_txg, B_FALSE); 1841 1842 zil_free(zilog); 1843 } 1844 1845 /* 1846 * We compare scan_prefetch_issue_ctx_t's based on their bookmarks. The idea 1847 * here is to sort the AVL tree by the order each block will be needed. 1848 */ 1849 static int 1850 scan_prefetch_queue_compare(const void *a, const void *b) 1851 { 1852 const scan_prefetch_issue_ctx_t *spic_a = a, *spic_b = b; 1853 const scan_prefetch_ctx_t *spc_a = spic_a->spic_spc; 1854 const scan_prefetch_ctx_t *spc_b = spic_b->spic_spc; 1855 1856 return (zbookmark_compare(spc_a->spc_datablkszsec, 1857 spc_a->spc_indblkshift, spc_b->spc_datablkszsec, 1858 spc_b->spc_indblkshift, &spic_a->spic_zb, &spic_b->spic_zb)); 1859 } 1860 1861 static void 1862 scan_prefetch_ctx_rele(scan_prefetch_ctx_t *spc, const void *tag) 1863 { 1864 if (zfs_refcount_remove(&spc->spc_refcnt, tag) == 0) { 1865 zfs_refcount_destroy(&spc->spc_refcnt); 1866 kmem_free(spc, sizeof (scan_prefetch_ctx_t)); 1867 } 1868 } 1869 1870 static scan_prefetch_ctx_t * 1871 scan_prefetch_ctx_create(dsl_scan_t *scn, dnode_phys_t *dnp, const void *tag) 1872 { 1873 scan_prefetch_ctx_t *spc; 1874 1875 spc = kmem_alloc(sizeof (scan_prefetch_ctx_t), KM_SLEEP); 1876 zfs_refcount_create(&spc->spc_refcnt); 1877 zfs_refcount_add(&spc->spc_refcnt, tag); 1878 spc->spc_scn = scn; 1879 if (dnp != NULL) { 1880 spc->spc_datablkszsec = dnp->dn_datablkszsec; 1881 spc->spc_indblkshift = dnp->dn_indblkshift; 1882 spc->spc_root = B_FALSE; 1883 } else { 1884 spc->spc_datablkszsec = 0; 1885 spc->spc_indblkshift = 0; 1886 spc->spc_root = B_TRUE; 1887 } 1888 1889 return (spc); 1890 } 1891 1892 static void 1893 scan_prefetch_ctx_add_ref(scan_prefetch_ctx_t *spc, const void *tag) 1894 { 1895 zfs_refcount_add(&spc->spc_refcnt, tag); 1896 } 1897 1898 static void 1899 scan_ds_prefetch_queue_clear(dsl_scan_t *scn) 1900 { 1901 spa_t *spa = scn->scn_dp->dp_spa; 1902 void *cookie = NULL; 1903 scan_prefetch_issue_ctx_t *spic = NULL; 1904 1905 mutex_enter(&spa->spa_scrub_lock); 1906 while ((spic = avl_destroy_nodes(&scn->scn_prefetch_queue, 1907 &cookie)) != NULL) { 1908 scan_prefetch_ctx_rele(spic->spic_spc, scn); 1909 kmem_free(spic, sizeof (scan_prefetch_issue_ctx_t)); 1910 } 1911 mutex_exit(&spa->spa_scrub_lock); 1912 } 1913 1914 static boolean_t 1915 dsl_scan_check_prefetch_resume(scan_prefetch_ctx_t *spc, 1916 const zbookmark_phys_t *zb) 1917 { 1918 zbookmark_phys_t *last_zb = &spc->spc_scn->scn_prefetch_bookmark; 1919 dnode_phys_t tmp_dnp; 1920 dnode_phys_t *dnp = (spc->spc_root) ? NULL : &tmp_dnp; 1921 1922 if (zb->zb_objset != last_zb->zb_objset) 1923 return (B_TRUE); 1924 if ((int64_t)zb->zb_object < 0) 1925 return (B_FALSE); 1926 1927 tmp_dnp.dn_datablkszsec = spc->spc_datablkszsec; 1928 tmp_dnp.dn_indblkshift = spc->spc_indblkshift; 1929 1930 if (zbookmark_subtree_completed(dnp, zb, last_zb)) 1931 return (B_TRUE); 1932 1933 return (B_FALSE); 1934 } 1935 1936 static void 1937 dsl_scan_prefetch(scan_prefetch_ctx_t *spc, blkptr_t *bp, zbookmark_phys_t *zb) 1938 { 1939 avl_index_t idx; 1940 dsl_scan_t *scn = spc->spc_scn; 1941 spa_t *spa = scn->scn_dp->dp_spa; 1942 scan_prefetch_issue_ctx_t *spic; 1943 1944 if (zfs_no_scrub_prefetch || BP_IS_REDACTED(bp)) 1945 return; 1946 1947 if (BP_IS_HOLE(bp) || 1948 BP_GET_LOGICAL_BIRTH(bp) <= scn->scn_phys.scn_cur_min_txg || 1949 (BP_GET_LEVEL(bp) == 0 && BP_GET_TYPE(bp) != DMU_OT_DNODE && 1950 BP_GET_TYPE(bp) != DMU_OT_OBJSET)) 1951 return; 1952 1953 if (dsl_scan_check_prefetch_resume(spc, zb)) 1954 return; 1955 1956 scan_prefetch_ctx_add_ref(spc, scn); 1957 spic = kmem_alloc(sizeof (scan_prefetch_issue_ctx_t), KM_SLEEP); 1958 spic->spic_spc = spc; 1959 spic->spic_bp = *bp; 1960 spic->spic_zb = *zb; 1961 1962 /* 1963 * Add the IO to the queue of blocks to prefetch. This allows us to 1964 * prioritize blocks that we will need first for the main traversal 1965 * thread. 1966 */ 1967 mutex_enter(&spa->spa_scrub_lock); 1968 if (avl_find(&scn->scn_prefetch_queue, spic, &idx) != NULL) { 1969 /* this block is already queued for prefetch */ 1970 kmem_free(spic, sizeof (scan_prefetch_issue_ctx_t)); 1971 scan_prefetch_ctx_rele(spc, scn); 1972 mutex_exit(&spa->spa_scrub_lock); 1973 return; 1974 } 1975 1976 avl_insert(&scn->scn_prefetch_queue, spic, idx); 1977 cv_broadcast(&spa->spa_scrub_io_cv); 1978 mutex_exit(&spa->spa_scrub_lock); 1979 } 1980 1981 static void 1982 dsl_scan_prefetch_dnode(dsl_scan_t *scn, dnode_phys_t *dnp, 1983 uint64_t objset, uint64_t object) 1984 { 1985 int i; 1986 zbookmark_phys_t zb; 1987 scan_prefetch_ctx_t *spc; 1988 1989 if (dnp->dn_nblkptr == 0 && !(dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR)) 1990 return; 1991 1992 SET_BOOKMARK(&zb, objset, object, 0, 0); 1993 1994 spc = scan_prefetch_ctx_create(scn, dnp, FTAG); 1995 1996 for (i = 0; i < dnp->dn_nblkptr; i++) { 1997 zb.zb_level = BP_GET_LEVEL(&dnp->dn_blkptr[i]); 1998 zb.zb_blkid = i; 1999 dsl_scan_prefetch(spc, &dnp->dn_blkptr[i], &zb); 2000 } 2001 2002 if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) { 2003 zb.zb_level = 0; 2004 zb.zb_blkid = DMU_SPILL_BLKID; 2005 dsl_scan_prefetch(spc, DN_SPILL_BLKPTR(dnp), &zb); 2006 } 2007 2008 scan_prefetch_ctx_rele(spc, FTAG); 2009 } 2010 2011 static void 2012 dsl_scan_prefetch_cb(zio_t *zio, const zbookmark_phys_t *zb, const blkptr_t *bp, 2013 arc_buf_t *buf, void *private) 2014 { 2015 (void) zio; 2016 scan_prefetch_ctx_t *spc = private; 2017 dsl_scan_t *scn = spc->spc_scn; 2018 spa_t *spa = scn->scn_dp->dp_spa; 2019 2020 /* broadcast that the IO has completed for rate limiting purposes */ 2021 mutex_enter(&spa->spa_scrub_lock); 2022 ASSERT3U(spa->spa_scrub_inflight, >=, BP_GET_PSIZE(bp)); 2023 spa->spa_scrub_inflight -= BP_GET_PSIZE(bp); 2024 cv_broadcast(&spa->spa_scrub_io_cv); 2025 mutex_exit(&spa->spa_scrub_lock); 2026 2027 /* if there was an error or we are done prefetching, just cleanup */ 2028 if (buf == NULL || scn->scn_prefetch_stop) 2029 goto out; 2030 2031 if (BP_GET_LEVEL(bp) > 0) { 2032 int i; 2033 blkptr_t *cbp; 2034 int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT; 2035 zbookmark_phys_t czb; 2036 2037 for (i = 0, cbp = buf->b_data; i < epb; i++, cbp++) { 2038 SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object, 2039 zb->zb_level - 1, zb->zb_blkid * epb + i); 2040 dsl_scan_prefetch(spc, cbp, &czb); 2041 } 2042 } else if (BP_GET_TYPE(bp) == DMU_OT_DNODE) { 2043 dnode_phys_t *cdnp; 2044 int i; 2045 int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT; 2046 2047 for (i = 0, cdnp = buf->b_data; i < epb; 2048 i += cdnp->dn_extra_slots + 1, 2049 cdnp += cdnp->dn_extra_slots + 1) { 2050 dsl_scan_prefetch_dnode(scn, cdnp, 2051 zb->zb_objset, zb->zb_blkid * epb + i); 2052 } 2053 } else if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) { 2054 objset_phys_t *osp = buf->b_data; 2055 2056 dsl_scan_prefetch_dnode(scn, &osp->os_meta_dnode, 2057 zb->zb_objset, DMU_META_DNODE_OBJECT); 2058 2059 if (OBJSET_BUF_HAS_USERUSED(buf)) { 2060 if (OBJSET_BUF_HAS_PROJECTUSED(buf)) { 2061 dsl_scan_prefetch_dnode(scn, 2062 &osp->os_projectused_dnode, zb->zb_objset, 2063 DMU_PROJECTUSED_OBJECT); 2064 } 2065 dsl_scan_prefetch_dnode(scn, 2066 &osp->os_groupused_dnode, zb->zb_objset, 2067 DMU_GROUPUSED_OBJECT); 2068 dsl_scan_prefetch_dnode(scn, 2069 &osp->os_userused_dnode, zb->zb_objset, 2070 DMU_USERUSED_OBJECT); 2071 } 2072 } 2073 2074 out: 2075 if (buf != NULL) 2076 arc_buf_destroy(buf, private); 2077 scan_prefetch_ctx_rele(spc, scn); 2078 } 2079 2080 static void 2081 dsl_scan_prefetch_thread(void *arg) 2082 { 2083 dsl_scan_t *scn = arg; 2084 spa_t *spa = scn->scn_dp->dp_spa; 2085 scan_prefetch_issue_ctx_t *spic; 2086 2087 /* loop until we are told to stop */ 2088 while (!scn->scn_prefetch_stop) { 2089 arc_flags_t flags = ARC_FLAG_NOWAIT | 2090 ARC_FLAG_PRESCIENT_PREFETCH | ARC_FLAG_PREFETCH; 2091 int zio_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCAN_THREAD; 2092 2093 mutex_enter(&spa->spa_scrub_lock); 2094 2095 /* 2096 * Wait until we have an IO to issue and are not above our 2097 * maximum in flight limit. 2098 */ 2099 while (!scn->scn_prefetch_stop && 2100 (avl_numnodes(&scn->scn_prefetch_queue) == 0 || 2101 spa->spa_scrub_inflight >= scn->scn_maxinflight_bytes)) { 2102 cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 2103 } 2104 2105 /* recheck if we should stop since we waited for the cv */ 2106 if (scn->scn_prefetch_stop) { 2107 mutex_exit(&spa->spa_scrub_lock); 2108 break; 2109 } 2110 2111 /* remove the prefetch IO from the tree */ 2112 spic = avl_first(&scn->scn_prefetch_queue); 2113 spa->spa_scrub_inflight += BP_GET_PSIZE(&spic->spic_bp); 2114 avl_remove(&scn->scn_prefetch_queue, spic); 2115 2116 mutex_exit(&spa->spa_scrub_lock); 2117 2118 if (BP_IS_PROTECTED(&spic->spic_bp)) { 2119 ASSERT(BP_GET_TYPE(&spic->spic_bp) == DMU_OT_DNODE || 2120 BP_GET_TYPE(&spic->spic_bp) == DMU_OT_OBJSET); 2121 ASSERT3U(BP_GET_LEVEL(&spic->spic_bp), ==, 0); 2122 zio_flags |= ZIO_FLAG_RAW; 2123 } 2124 2125 /* We don't need data L1 buffer since we do not prefetch L0. */ 2126 blkptr_t *bp = &spic->spic_bp; 2127 if (BP_GET_LEVEL(bp) == 1 && BP_GET_TYPE(bp) != DMU_OT_DNODE && 2128 BP_GET_TYPE(bp) != DMU_OT_OBJSET) 2129 flags |= ARC_FLAG_NO_BUF; 2130 2131 /* issue the prefetch asynchronously */ 2132 (void) arc_read(scn->scn_zio_root, spa, bp, 2133 dsl_scan_prefetch_cb, spic->spic_spc, ZIO_PRIORITY_SCRUB, 2134 zio_flags, &flags, &spic->spic_zb); 2135 2136 kmem_free(spic, sizeof (scan_prefetch_issue_ctx_t)); 2137 } 2138 2139 ASSERT(scn->scn_prefetch_stop); 2140 2141 /* free any prefetches we didn't get to complete */ 2142 mutex_enter(&spa->spa_scrub_lock); 2143 while ((spic = avl_first(&scn->scn_prefetch_queue)) != NULL) { 2144 avl_remove(&scn->scn_prefetch_queue, spic); 2145 scan_prefetch_ctx_rele(spic->spic_spc, scn); 2146 kmem_free(spic, sizeof (scan_prefetch_issue_ctx_t)); 2147 } 2148 ASSERT0(avl_numnodes(&scn->scn_prefetch_queue)); 2149 mutex_exit(&spa->spa_scrub_lock); 2150 } 2151 2152 static boolean_t 2153 dsl_scan_check_resume(dsl_scan_t *scn, const dnode_phys_t *dnp, 2154 const zbookmark_phys_t *zb) 2155 { 2156 /* 2157 * We never skip over user/group accounting objects (obj<0) 2158 */ 2159 if (!ZB_IS_ZERO(&scn->scn_phys.scn_bookmark) && 2160 (int64_t)zb->zb_object >= 0) { 2161 /* 2162 * If we already visited this bp & everything below (in 2163 * a prior txg sync), don't bother doing it again. 2164 */ 2165 if (zbookmark_subtree_completed(dnp, zb, 2166 &scn->scn_phys.scn_bookmark)) 2167 return (B_TRUE); 2168 2169 /* 2170 * If we found the block we're trying to resume from, or 2171 * we went past it, zero it out to indicate that it's OK 2172 * to start checking for suspending again. 2173 */ 2174 if (zbookmark_subtree_tbd(dnp, zb, 2175 &scn->scn_phys.scn_bookmark)) { 2176 dprintf("resuming at %llx/%llx/%llx/%llx\n", 2177 (longlong_t)zb->zb_objset, 2178 (longlong_t)zb->zb_object, 2179 (longlong_t)zb->zb_level, 2180 (longlong_t)zb->zb_blkid); 2181 memset(&scn->scn_phys.scn_bookmark, 0, sizeof (*zb)); 2182 } 2183 } 2184 return (B_FALSE); 2185 } 2186 2187 static void dsl_scan_visitbp(const blkptr_t *bp, const zbookmark_phys_t *zb, 2188 dnode_phys_t *dnp, dsl_dataset_t *ds, dsl_scan_t *scn, 2189 dmu_objset_type_t ostype, dmu_tx_t *tx); 2190 inline __attribute__((always_inline)) static void dsl_scan_visitdnode( 2191 dsl_scan_t *, dsl_dataset_t *ds, dmu_objset_type_t ostype, 2192 dnode_phys_t *dnp, uint64_t object, dmu_tx_t *tx); 2193 2194 /* 2195 * Return nonzero on i/o error. 2196 * Return new buf to write out in *bufp. 2197 */ 2198 inline __attribute__((always_inline)) static int 2199 dsl_scan_recurse(dsl_scan_t *scn, dsl_dataset_t *ds, dmu_objset_type_t ostype, 2200 dnode_phys_t *dnp, const blkptr_t *bp, 2201 const zbookmark_phys_t *zb, dmu_tx_t *tx) 2202 { 2203 dsl_pool_t *dp = scn->scn_dp; 2204 spa_t *spa = dp->dp_spa; 2205 int zio_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCAN_THREAD; 2206 int err; 2207 2208 ASSERT(!BP_IS_REDACTED(bp)); 2209 2210 /* 2211 * There is an unlikely case of encountering dnodes with contradicting 2212 * dn_bonuslen and DNODE_FLAG_SPILL_BLKPTR flag before in files created 2213 * or modified before commit 4254acb was merged. As it is not possible 2214 * to know which of the two is correct, report an error. 2215 */ 2216 if (dnp != NULL && 2217 dnp->dn_bonuslen > DN_MAX_BONUS_LEN(dnp)) { 2218 scn->scn_phys.scn_errors++; 2219 spa_log_error(spa, zb, BP_GET_LOGICAL_BIRTH(bp)); 2220 return (SET_ERROR(EINVAL)); 2221 } 2222 2223 if (BP_GET_LEVEL(bp) > 0) { 2224 arc_flags_t flags = ARC_FLAG_WAIT; 2225 int i; 2226 blkptr_t *cbp; 2227 int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT; 2228 arc_buf_t *buf; 2229 2230 err = arc_read(NULL, spa, bp, arc_getbuf_func, &buf, 2231 ZIO_PRIORITY_SCRUB, zio_flags, &flags, zb); 2232 if (err) { 2233 scn->scn_phys.scn_errors++; 2234 return (err); 2235 } 2236 for (i = 0, cbp = buf->b_data; i < epb; i++, cbp++) { 2237 zbookmark_phys_t czb; 2238 2239 SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object, 2240 zb->zb_level - 1, 2241 zb->zb_blkid * epb + i); 2242 dsl_scan_visitbp(cbp, &czb, dnp, 2243 ds, scn, ostype, tx); 2244 } 2245 arc_buf_destroy(buf, &buf); 2246 } else if (BP_GET_TYPE(bp) == DMU_OT_DNODE) { 2247 arc_flags_t flags = ARC_FLAG_WAIT; 2248 dnode_phys_t *cdnp; 2249 int i; 2250 int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT; 2251 arc_buf_t *buf; 2252 2253 if (BP_IS_PROTECTED(bp)) { 2254 ASSERT3U(BP_GET_COMPRESS(bp), ==, ZIO_COMPRESS_OFF); 2255 zio_flags |= ZIO_FLAG_RAW; 2256 } 2257 2258 err = arc_read(NULL, spa, bp, arc_getbuf_func, &buf, 2259 ZIO_PRIORITY_SCRUB, zio_flags, &flags, zb); 2260 if (err) { 2261 scn->scn_phys.scn_errors++; 2262 return (err); 2263 } 2264 for (i = 0, cdnp = buf->b_data; i < epb; 2265 i += cdnp->dn_extra_slots + 1, 2266 cdnp += cdnp->dn_extra_slots + 1) { 2267 dsl_scan_visitdnode(scn, ds, ostype, 2268 cdnp, zb->zb_blkid * epb + i, tx); 2269 } 2270 2271 arc_buf_destroy(buf, &buf); 2272 } else if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) { 2273 arc_flags_t flags = ARC_FLAG_WAIT; 2274 objset_phys_t *osp; 2275 arc_buf_t *buf; 2276 2277 err = arc_read(NULL, spa, bp, arc_getbuf_func, &buf, 2278 ZIO_PRIORITY_SCRUB, zio_flags, &flags, zb); 2279 if (err) { 2280 scn->scn_phys.scn_errors++; 2281 return (err); 2282 } 2283 2284 osp = buf->b_data; 2285 2286 dsl_scan_visitdnode(scn, ds, osp->os_type, 2287 &osp->os_meta_dnode, DMU_META_DNODE_OBJECT, tx); 2288 2289 if (OBJSET_BUF_HAS_USERUSED(buf)) { 2290 /* 2291 * We also always visit user/group/project accounting 2292 * objects, and never skip them, even if we are 2293 * suspending. This is necessary so that the 2294 * space deltas from this txg get integrated. 2295 */ 2296 if (OBJSET_BUF_HAS_PROJECTUSED(buf)) 2297 dsl_scan_visitdnode(scn, ds, osp->os_type, 2298 &osp->os_projectused_dnode, 2299 DMU_PROJECTUSED_OBJECT, tx); 2300 dsl_scan_visitdnode(scn, ds, osp->os_type, 2301 &osp->os_groupused_dnode, 2302 DMU_GROUPUSED_OBJECT, tx); 2303 dsl_scan_visitdnode(scn, ds, osp->os_type, 2304 &osp->os_userused_dnode, 2305 DMU_USERUSED_OBJECT, tx); 2306 } 2307 arc_buf_destroy(buf, &buf); 2308 } else if (!zfs_blkptr_verify(spa, bp, 2309 BLK_CONFIG_NEEDED, BLK_VERIFY_LOG)) { 2310 /* 2311 * Sanity check the block pointer contents, this is handled 2312 * by arc_read() for the cases above. 2313 */ 2314 scn->scn_phys.scn_errors++; 2315 spa_log_error(spa, zb, BP_GET_LOGICAL_BIRTH(bp)); 2316 return (SET_ERROR(EINVAL)); 2317 } 2318 2319 return (0); 2320 } 2321 2322 inline __attribute__((always_inline)) static void 2323 dsl_scan_visitdnode(dsl_scan_t *scn, dsl_dataset_t *ds, 2324 dmu_objset_type_t ostype, dnode_phys_t *dnp, 2325 uint64_t object, dmu_tx_t *tx) 2326 { 2327 int j; 2328 2329 for (j = 0; j < dnp->dn_nblkptr; j++) { 2330 zbookmark_phys_t czb; 2331 2332 SET_BOOKMARK(&czb, ds ? ds->ds_object : 0, object, 2333 dnp->dn_nlevels - 1, j); 2334 dsl_scan_visitbp(&dnp->dn_blkptr[j], 2335 &czb, dnp, ds, scn, ostype, tx); 2336 } 2337 2338 if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) { 2339 zbookmark_phys_t czb; 2340 SET_BOOKMARK(&czb, ds ? ds->ds_object : 0, object, 2341 0, DMU_SPILL_BLKID); 2342 dsl_scan_visitbp(DN_SPILL_BLKPTR(dnp), 2343 &czb, dnp, ds, scn, ostype, tx); 2344 } 2345 } 2346 2347 /* 2348 * The arguments are in this order because mdb can only print the 2349 * first 5; we want them to be useful. 2350 */ 2351 static void 2352 dsl_scan_visitbp(const blkptr_t *bp, const zbookmark_phys_t *zb, 2353 dnode_phys_t *dnp, dsl_dataset_t *ds, dsl_scan_t *scn, 2354 dmu_objset_type_t ostype, dmu_tx_t *tx) 2355 { 2356 dsl_pool_t *dp = scn->scn_dp; 2357 2358 if (dsl_scan_check_suspend(scn, zb)) 2359 return; 2360 2361 if (dsl_scan_check_resume(scn, dnp, zb)) 2362 return; 2363 2364 scn->scn_visited_this_txg++; 2365 2366 if (BP_IS_HOLE(bp)) { 2367 scn->scn_holes_this_txg++; 2368 return; 2369 } 2370 2371 if (BP_IS_REDACTED(bp)) { 2372 ASSERT(dsl_dataset_feature_is_active(ds, 2373 SPA_FEATURE_REDACTED_DATASETS)); 2374 return; 2375 } 2376 2377 /* 2378 * Check if this block contradicts any filesystem flags. 2379 */ 2380 spa_feature_t f = SPA_FEATURE_LARGE_BLOCKS; 2381 if (BP_GET_LSIZE(bp) > SPA_OLD_MAXBLOCKSIZE) 2382 ASSERT(dsl_dataset_feature_is_active(ds, f)); 2383 2384 f = zio_checksum_to_feature(BP_GET_CHECKSUM(bp)); 2385 if (f != SPA_FEATURE_NONE) 2386 ASSERT(dsl_dataset_feature_is_active(ds, f)); 2387 2388 f = zio_compress_to_feature(BP_GET_COMPRESS(bp)); 2389 if (f != SPA_FEATURE_NONE) 2390 ASSERT(dsl_dataset_feature_is_active(ds, f)); 2391 2392 if (BP_GET_LOGICAL_BIRTH(bp) <= scn->scn_phys.scn_cur_min_txg) { 2393 scn->scn_lt_min_this_txg++; 2394 return; 2395 } 2396 2397 if (dsl_scan_recurse(scn, ds, ostype, dnp, bp, zb, tx) != 0) 2398 return; 2399 2400 /* 2401 * If dsl_scan_ddt() has already visited this block, it will have 2402 * already done any translations or scrubbing, so don't call the 2403 * callback again. 2404 */ 2405 if (ddt_class_contains(dp->dp_spa, 2406 scn->scn_phys.scn_ddt_class_max, bp)) { 2407 scn->scn_ddt_contained_this_txg++; 2408 return; 2409 } 2410 2411 /* 2412 * If this block is from the future (after cur_max_txg), then we 2413 * are doing this on behalf of a deleted snapshot, and we will 2414 * revisit the future block on the next pass of this dataset. 2415 * Don't scan it now unless we need to because something 2416 * under it was modified. 2417 */ 2418 if (BP_GET_BIRTH(bp) > scn->scn_phys.scn_cur_max_txg) { 2419 scn->scn_gt_max_this_txg++; 2420 return; 2421 } 2422 2423 scan_funcs[scn->scn_phys.scn_func](dp, bp, zb); 2424 } 2425 2426 static void 2427 dsl_scan_visit_rootbp(dsl_scan_t *scn, dsl_dataset_t *ds, blkptr_t *bp, 2428 dmu_tx_t *tx) 2429 { 2430 zbookmark_phys_t zb; 2431 scan_prefetch_ctx_t *spc; 2432 2433 SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET, 2434 ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 2435 2436 if (ZB_IS_ZERO(&scn->scn_phys.scn_bookmark)) { 2437 SET_BOOKMARK(&scn->scn_prefetch_bookmark, 2438 zb.zb_objset, 0, 0, 0); 2439 } else { 2440 scn->scn_prefetch_bookmark = scn->scn_phys.scn_bookmark; 2441 } 2442 2443 scn->scn_objsets_visited_this_txg++; 2444 2445 spc = scan_prefetch_ctx_create(scn, NULL, FTAG); 2446 dsl_scan_prefetch(spc, bp, &zb); 2447 scan_prefetch_ctx_rele(spc, FTAG); 2448 2449 dsl_scan_visitbp(bp, &zb, NULL, ds, scn, DMU_OST_NONE, tx); 2450 2451 dprintf_ds(ds, "finished scan%s", ""); 2452 } 2453 2454 static void 2455 ds_destroyed_scn_phys(dsl_dataset_t *ds, dsl_scan_phys_t *scn_phys) 2456 { 2457 if (scn_phys->scn_bookmark.zb_objset == ds->ds_object) { 2458 if (ds->ds_is_snapshot) { 2459 /* 2460 * Note: 2461 * - scn_cur_{min,max}_txg stays the same. 2462 * - Setting the flag is not really necessary if 2463 * scn_cur_max_txg == scn_max_txg, because there 2464 * is nothing after this snapshot that we care 2465 * about. However, we set it anyway and then 2466 * ignore it when we retraverse it in 2467 * dsl_scan_visitds(). 2468 */ 2469 scn_phys->scn_bookmark.zb_objset = 2470 dsl_dataset_phys(ds)->ds_next_snap_obj; 2471 zfs_dbgmsg("destroying ds %llu on %s; currently " 2472 "traversing; reset zb_objset to %llu", 2473 (u_longlong_t)ds->ds_object, 2474 ds->ds_dir->dd_pool->dp_spa->spa_name, 2475 (u_longlong_t)dsl_dataset_phys(ds)-> 2476 ds_next_snap_obj); 2477 scn_phys->scn_flags |= DSF_VISIT_DS_AGAIN; 2478 } else { 2479 SET_BOOKMARK(&scn_phys->scn_bookmark, 2480 ZB_DESTROYED_OBJSET, 0, 0, 0); 2481 zfs_dbgmsg("destroying ds %llu on %s; currently " 2482 "traversing; reset bookmark to -1,0,0,0", 2483 (u_longlong_t)ds->ds_object, 2484 ds->ds_dir->dd_pool->dp_spa->spa_name); 2485 } 2486 } 2487 } 2488 2489 /* 2490 * Invoked when a dataset is destroyed. We need to make sure that: 2491 * 2492 * 1) If it is the dataset that was currently being scanned, we write 2493 * a new dsl_scan_phys_t and marking the objset reference in it 2494 * as destroyed. 2495 * 2) Remove it from the work queue, if it was present. 2496 * 2497 * If the dataset was actually a snapshot, instead of marking the dataset 2498 * as destroyed, we instead substitute the next snapshot in line. 2499 */ 2500 void 2501 dsl_scan_ds_destroyed(dsl_dataset_t *ds, dmu_tx_t *tx) 2502 { 2503 dsl_pool_t *dp = ds->ds_dir->dd_pool; 2504 dsl_scan_t *scn = dp->dp_scan; 2505 uint64_t mintxg; 2506 2507 if (!dsl_scan_is_running(scn)) 2508 return; 2509 2510 ds_destroyed_scn_phys(ds, &scn->scn_phys); 2511 ds_destroyed_scn_phys(ds, &scn->scn_phys_cached); 2512 2513 if (scan_ds_queue_contains(scn, ds->ds_object, &mintxg)) { 2514 scan_ds_queue_remove(scn, ds->ds_object); 2515 if (ds->ds_is_snapshot) 2516 scan_ds_queue_insert(scn, 2517 dsl_dataset_phys(ds)->ds_next_snap_obj, mintxg); 2518 } 2519 2520 if (zap_lookup_int_key(dp->dp_meta_objset, scn->scn_phys.scn_queue_obj, 2521 ds->ds_object, &mintxg) == 0) { 2522 ASSERT3U(dsl_dataset_phys(ds)->ds_num_children, <=, 1); 2523 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset, 2524 scn->scn_phys.scn_queue_obj, ds->ds_object, tx)); 2525 if (ds->ds_is_snapshot) { 2526 /* 2527 * We keep the same mintxg; it could be > 2528 * ds_creation_txg if the previous snapshot was 2529 * deleted too. 2530 */ 2531 VERIFY(zap_add_int_key(dp->dp_meta_objset, 2532 scn->scn_phys.scn_queue_obj, 2533 dsl_dataset_phys(ds)->ds_next_snap_obj, 2534 mintxg, tx) == 0); 2535 zfs_dbgmsg("destroying ds %llu on %s; in queue; " 2536 "replacing with %llu", 2537 (u_longlong_t)ds->ds_object, 2538 dp->dp_spa->spa_name, 2539 (u_longlong_t)dsl_dataset_phys(ds)-> 2540 ds_next_snap_obj); 2541 } else { 2542 zfs_dbgmsg("destroying ds %llu on %s; in queue; " 2543 "removing", 2544 (u_longlong_t)ds->ds_object, 2545 dp->dp_spa->spa_name); 2546 } 2547 } 2548 2549 /* 2550 * dsl_scan_sync() should be called after this, and should sync 2551 * out our changed state, but just to be safe, do it here. 2552 */ 2553 dsl_scan_sync_state(scn, tx, SYNC_CACHED); 2554 } 2555 2556 static void 2557 ds_snapshotted_bookmark(dsl_dataset_t *ds, zbookmark_phys_t *scn_bookmark) 2558 { 2559 if (scn_bookmark->zb_objset == ds->ds_object) { 2560 scn_bookmark->zb_objset = 2561 dsl_dataset_phys(ds)->ds_prev_snap_obj; 2562 zfs_dbgmsg("snapshotting ds %llu on %s; currently traversing; " 2563 "reset zb_objset to %llu", 2564 (u_longlong_t)ds->ds_object, 2565 ds->ds_dir->dd_pool->dp_spa->spa_name, 2566 (u_longlong_t)dsl_dataset_phys(ds)->ds_prev_snap_obj); 2567 } 2568 } 2569 2570 /* 2571 * Called when a dataset is snapshotted. If we were currently traversing 2572 * this snapshot, we reset our bookmark to point at the newly created 2573 * snapshot. We also modify our work queue to remove the old snapshot and 2574 * replace with the new one. 2575 */ 2576 void 2577 dsl_scan_ds_snapshotted(dsl_dataset_t *ds, dmu_tx_t *tx) 2578 { 2579 dsl_pool_t *dp = ds->ds_dir->dd_pool; 2580 dsl_scan_t *scn = dp->dp_scan; 2581 uint64_t mintxg; 2582 2583 if (!dsl_scan_is_running(scn)) 2584 return; 2585 2586 ASSERT(dsl_dataset_phys(ds)->ds_prev_snap_obj != 0); 2587 2588 ds_snapshotted_bookmark(ds, &scn->scn_phys.scn_bookmark); 2589 ds_snapshotted_bookmark(ds, &scn->scn_phys_cached.scn_bookmark); 2590 2591 if (scan_ds_queue_contains(scn, ds->ds_object, &mintxg)) { 2592 scan_ds_queue_remove(scn, ds->ds_object); 2593 scan_ds_queue_insert(scn, 2594 dsl_dataset_phys(ds)->ds_prev_snap_obj, mintxg); 2595 } 2596 2597 if (zap_lookup_int_key(dp->dp_meta_objset, scn->scn_phys.scn_queue_obj, 2598 ds->ds_object, &mintxg) == 0) { 2599 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset, 2600 scn->scn_phys.scn_queue_obj, ds->ds_object, tx)); 2601 VERIFY(zap_add_int_key(dp->dp_meta_objset, 2602 scn->scn_phys.scn_queue_obj, 2603 dsl_dataset_phys(ds)->ds_prev_snap_obj, mintxg, tx) == 0); 2604 zfs_dbgmsg("snapshotting ds %llu on %s; in queue; " 2605 "replacing with %llu", 2606 (u_longlong_t)ds->ds_object, 2607 dp->dp_spa->spa_name, 2608 (u_longlong_t)dsl_dataset_phys(ds)->ds_prev_snap_obj); 2609 } 2610 2611 dsl_scan_sync_state(scn, tx, SYNC_CACHED); 2612 } 2613 2614 static void 2615 ds_clone_swapped_bookmark(dsl_dataset_t *ds1, dsl_dataset_t *ds2, 2616 zbookmark_phys_t *scn_bookmark) 2617 { 2618 if (scn_bookmark->zb_objset == ds1->ds_object) { 2619 scn_bookmark->zb_objset = ds2->ds_object; 2620 zfs_dbgmsg("clone_swap ds %llu on %s; currently traversing; " 2621 "reset zb_objset to %llu", 2622 (u_longlong_t)ds1->ds_object, 2623 ds1->ds_dir->dd_pool->dp_spa->spa_name, 2624 (u_longlong_t)ds2->ds_object); 2625 } else if (scn_bookmark->zb_objset == ds2->ds_object) { 2626 scn_bookmark->zb_objset = ds1->ds_object; 2627 zfs_dbgmsg("clone_swap ds %llu on %s; currently traversing; " 2628 "reset zb_objset to %llu", 2629 (u_longlong_t)ds2->ds_object, 2630 ds2->ds_dir->dd_pool->dp_spa->spa_name, 2631 (u_longlong_t)ds1->ds_object); 2632 } 2633 } 2634 2635 /* 2636 * Called when an origin dataset and its clone are swapped. If we were 2637 * currently traversing the dataset, we need to switch to traversing the 2638 * newly promoted clone. 2639 */ 2640 void 2641 dsl_scan_ds_clone_swapped(dsl_dataset_t *ds1, dsl_dataset_t *ds2, dmu_tx_t *tx) 2642 { 2643 dsl_pool_t *dp = ds1->ds_dir->dd_pool; 2644 dsl_scan_t *scn = dp->dp_scan; 2645 uint64_t mintxg1, mintxg2; 2646 boolean_t ds1_queued, ds2_queued; 2647 2648 if (!dsl_scan_is_running(scn)) 2649 return; 2650 2651 ds_clone_swapped_bookmark(ds1, ds2, &scn->scn_phys.scn_bookmark); 2652 ds_clone_swapped_bookmark(ds1, ds2, &scn->scn_phys_cached.scn_bookmark); 2653 2654 /* 2655 * Handle the in-memory scan queue. 2656 */ 2657 ds1_queued = scan_ds_queue_contains(scn, ds1->ds_object, &mintxg1); 2658 ds2_queued = scan_ds_queue_contains(scn, ds2->ds_object, &mintxg2); 2659 2660 /* Sanity checking. */ 2661 if (ds1_queued) { 2662 ASSERT3U(mintxg1, ==, dsl_dataset_phys(ds1)->ds_prev_snap_txg); 2663 ASSERT3U(mintxg1, ==, dsl_dataset_phys(ds2)->ds_prev_snap_txg); 2664 } 2665 if (ds2_queued) { 2666 ASSERT3U(mintxg2, ==, dsl_dataset_phys(ds1)->ds_prev_snap_txg); 2667 ASSERT3U(mintxg2, ==, dsl_dataset_phys(ds2)->ds_prev_snap_txg); 2668 } 2669 2670 if (ds1_queued && ds2_queued) { 2671 /* 2672 * If both are queued, we don't need to do anything. 2673 * The swapping code below would not handle this case correctly, 2674 * since we can't insert ds2 if it is already there. That's 2675 * because scan_ds_queue_insert() prohibits a duplicate insert 2676 * and panics. 2677 */ 2678 } else if (ds1_queued) { 2679 scan_ds_queue_remove(scn, ds1->ds_object); 2680 scan_ds_queue_insert(scn, ds2->ds_object, mintxg1); 2681 } else if (ds2_queued) { 2682 scan_ds_queue_remove(scn, ds2->ds_object); 2683 scan_ds_queue_insert(scn, ds1->ds_object, mintxg2); 2684 } 2685 2686 /* 2687 * Handle the on-disk scan queue. 2688 * The on-disk state is an out-of-date version of the in-memory state, 2689 * so the in-memory and on-disk values for ds1_queued and ds2_queued may 2690 * be different. Therefore we need to apply the swap logic to the 2691 * on-disk state independently of the in-memory state. 2692 */ 2693 ds1_queued = zap_lookup_int_key(dp->dp_meta_objset, 2694 scn->scn_phys.scn_queue_obj, ds1->ds_object, &mintxg1) == 0; 2695 ds2_queued = zap_lookup_int_key(dp->dp_meta_objset, 2696 scn->scn_phys.scn_queue_obj, ds2->ds_object, &mintxg2) == 0; 2697 2698 /* Sanity checking. */ 2699 if (ds1_queued) { 2700 ASSERT3U(mintxg1, ==, dsl_dataset_phys(ds1)->ds_prev_snap_txg); 2701 ASSERT3U(mintxg1, ==, dsl_dataset_phys(ds2)->ds_prev_snap_txg); 2702 } 2703 if (ds2_queued) { 2704 ASSERT3U(mintxg2, ==, dsl_dataset_phys(ds1)->ds_prev_snap_txg); 2705 ASSERT3U(mintxg2, ==, dsl_dataset_phys(ds2)->ds_prev_snap_txg); 2706 } 2707 2708 if (ds1_queued && ds2_queued) { 2709 /* 2710 * If both are queued, we don't need to do anything. 2711 * Alternatively, we could check for EEXIST from 2712 * zap_add_int_key() and back out to the original state, but 2713 * that would be more work than checking for this case upfront. 2714 */ 2715 } else if (ds1_queued) { 2716 VERIFY3S(0, ==, zap_remove_int(dp->dp_meta_objset, 2717 scn->scn_phys.scn_queue_obj, ds1->ds_object, tx)); 2718 VERIFY3S(0, ==, zap_add_int_key(dp->dp_meta_objset, 2719 scn->scn_phys.scn_queue_obj, ds2->ds_object, mintxg1, tx)); 2720 zfs_dbgmsg("clone_swap ds %llu on %s; in queue; " 2721 "replacing with %llu", 2722 (u_longlong_t)ds1->ds_object, 2723 dp->dp_spa->spa_name, 2724 (u_longlong_t)ds2->ds_object); 2725 } else if (ds2_queued) { 2726 VERIFY3S(0, ==, zap_remove_int(dp->dp_meta_objset, 2727 scn->scn_phys.scn_queue_obj, ds2->ds_object, tx)); 2728 VERIFY3S(0, ==, zap_add_int_key(dp->dp_meta_objset, 2729 scn->scn_phys.scn_queue_obj, ds1->ds_object, mintxg2, tx)); 2730 zfs_dbgmsg("clone_swap ds %llu on %s; in queue; " 2731 "replacing with %llu", 2732 (u_longlong_t)ds2->ds_object, 2733 dp->dp_spa->spa_name, 2734 (u_longlong_t)ds1->ds_object); 2735 } 2736 2737 dsl_scan_sync_state(scn, tx, SYNC_CACHED); 2738 } 2739 2740 static int 2741 enqueue_clones_cb(dsl_pool_t *dp, dsl_dataset_t *hds, void *arg) 2742 { 2743 uint64_t originobj = *(uint64_t *)arg; 2744 dsl_dataset_t *ds; 2745 int err; 2746 dsl_scan_t *scn = dp->dp_scan; 2747 2748 if (dsl_dir_phys(hds->ds_dir)->dd_origin_obj != originobj) 2749 return (0); 2750 2751 err = dsl_dataset_hold_obj(dp, hds->ds_object, FTAG, &ds); 2752 if (err) 2753 return (err); 2754 2755 while (dsl_dataset_phys(ds)->ds_prev_snap_obj != originobj) { 2756 dsl_dataset_t *prev; 2757 err = dsl_dataset_hold_obj(dp, 2758 dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &prev); 2759 2760 dsl_dataset_rele(ds, FTAG); 2761 if (err) 2762 return (err); 2763 ds = prev; 2764 } 2765 mutex_enter(&scn->scn_queue_lock); 2766 scan_ds_queue_insert(scn, ds->ds_object, 2767 dsl_dataset_phys(ds)->ds_prev_snap_txg); 2768 mutex_exit(&scn->scn_queue_lock); 2769 dsl_dataset_rele(ds, FTAG); 2770 return (0); 2771 } 2772 2773 static void 2774 dsl_scan_visitds(dsl_scan_t *scn, uint64_t dsobj, dmu_tx_t *tx) 2775 { 2776 dsl_pool_t *dp = scn->scn_dp; 2777 dsl_dataset_t *ds; 2778 2779 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds)); 2780 2781 if (scn->scn_phys.scn_cur_min_txg >= 2782 scn->scn_phys.scn_max_txg) { 2783 /* 2784 * This can happen if this snapshot was created after the 2785 * scan started, and we already completed a previous snapshot 2786 * that was created after the scan started. This snapshot 2787 * only references blocks with: 2788 * 2789 * birth < our ds_creation_txg 2790 * cur_min_txg is no less than ds_creation_txg. 2791 * We have already visited these blocks. 2792 * or 2793 * birth > scn_max_txg 2794 * The scan requested not to visit these blocks. 2795 * 2796 * Subsequent snapshots (and clones) can reference our 2797 * blocks, or blocks with even higher birth times. 2798 * Therefore we do not need to visit them either, 2799 * so we do not add them to the work queue. 2800 * 2801 * Note that checking for cur_min_txg >= cur_max_txg 2802 * is not sufficient, because in that case we may need to 2803 * visit subsequent snapshots. This happens when min_txg > 0, 2804 * which raises cur_min_txg. In this case we will visit 2805 * this dataset but skip all of its blocks, because the 2806 * rootbp's birth time is < cur_min_txg. Then we will 2807 * add the next snapshots/clones to the work queue. 2808 */ 2809 char *dsname = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP); 2810 dsl_dataset_name(ds, dsname); 2811 zfs_dbgmsg("scanning dataset %llu (%s) is unnecessary because " 2812 "cur_min_txg (%llu) >= max_txg (%llu)", 2813 (longlong_t)dsobj, dsname, 2814 (longlong_t)scn->scn_phys.scn_cur_min_txg, 2815 (longlong_t)scn->scn_phys.scn_max_txg); 2816 kmem_free(dsname, MAXNAMELEN); 2817 2818 goto out; 2819 } 2820 2821 /* 2822 * Only the ZIL in the head (non-snapshot) is valid. Even though 2823 * snapshots can have ZIL block pointers (which may be the same 2824 * BP as in the head), they must be ignored. In addition, $ORIGIN 2825 * doesn't have a objset (i.e. its ds_bp is a hole) so we don't 2826 * need to look for a ZIL in it either. So we traverse the ZIL here, 2827 * rather than in scan_recurse(), because the regular snapshot 2828 * block-sharing rules don't apply to it. 2829 */ 2830 if (!dsl_dataset_is_snapshot(ds) && 2831 (dp->dp_origin_snap == NULL || 2832 ds->ds_dir != dp->dp_origin_snap->ds_dir)) { 2833 objset_t *os; 2834 if (dmu_objset_from_ds(ds, &os) != 0) { 2835 goto out; 2836 } 2837 dsl_scan_zil(dp, &os->os_zil_header); 2838 } 2839 2840 /* 2841 * Iterate over the bps in this ds. 2842 */ 2843 dmu_buf_will_dirty(ds->ds_dbuf, tx); 2844 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG); 2845 dsl_scan_visit_rootbp(scn, ds, &dsl_dataset_phys(ds)->ds_bp, tx); 2846 rrw_exit(&ds->ds_bp_rwlock, FTAG); 2847 2848 char *dsname = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP); 2849 dsl_dataset_name(ds, dsname); 2850 zfs_dbgmsg("scanned dataset %llu (%s) with min=%llu max=%llu; " 2851 "suspending=%u", 2852 (longlong_t)dsobj, dsname, 2853 (longlong_t)scn->scn_phys.scn_cur_min_txg, 2854 (longlong_t)scn->scn_phys.scn_cur_max_txg, 2855 (int)scn->scn_suspending); 2856 kmem_free(dsname, ZFS_MAX_DATASET_NAME_LEN); 2857 2858 if (scn->scn_suspending) 2859 goto out; 2860 2861 /* 2862 * We've finished this pass over this dataset. 2863 */ 2864 2865 /* 2866 * If we did not completely visit this dataset, do another pass. 2867 */ 2868 if (scn->scn_phys.scn_flags & DSF_VISIT_DS_AGAIN) { 2869 zfs_dbgmsg("incomplete pass on %s; visiting again", 2870 dp->dp_spa->spa_name); 2871 scn->scn_phys.scn_flags &= ~DSF_VISIT_DS_AGAIN; 2872 scan_ds_queue_insert(scn, ds->ds_object, 2873 scn->scn_phys.scn_cur_max_txg); 2874 goto out; 2875 } 2876 2877 /* 2878 * Add descendant datasets to work queue. 2879 */ 2880 if (dsl_dataset_phys(ds)->ds_next_snap_obj != 0) { 2881 scan_ds_queue_insert(scn, 2882 dsl_dataset_phys(ds)->ds_next_snap_obj, 2883 dsl_dataset_phys(ds)->ds_creation_txg); 2884 } 2885 if (dsl_dataset_phys(ds)->ds_num_children > 1) { 2886 boolean_t usenext = B_FALSE; 2887 if (dsl_dataset_phys(ds)->ds_next_clones_obj != 0) { 2888 uint64_t count; 2889 /* 2890 * A bug in a previous version of the code could 2891 * cause upgrade_clones_cb() to not set 2892 * ds_next_snap_obj when it should, leading to a 2893 * missing entry. Therefore we can only use the 2894 * next_clones_obj when its count is correct. 2895 */ 2896 int err = zap_count(dp->dp_meta_objset, 2897 dsl_dataset_phys(ds)->ds_next_clones_obj, &count); 2898 if (err == 0 && 2899 count == dsl_dataset_phys(ds)->ds_num_children - 1) 2900 usenext = B_TRUE; 2901 } 2902 2903 if (usenext) { 2904 zap_cursor_t zc; 2905 zap_attribute_t *za = zap_attribute_alloc(); 2906 for (zap_cursor_init(&zc, dp->dp_meta_objset, 2907 dsl_dataset_phys(ds)->ds_next_clones_obj); 2908 zap_cursor_retrieve(&zc, za) == 0; 2909 (void) zap_cursor_advance(&zc)) { 2910 scan_ds_queue_insert(scn, 2911 zfs_strtonum(za->za_name, NULL), 2912 dsl_dataset_phys(ds)->ds_creation_txg); 2913 } 2914 zap_cursor_fini(&zc); 2915 zap_attribute_free(za); 2916 } else { 2917 VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj, 2918 enqueue_clones_cb, &ds->ds_object, 2919 DS_FIND_CHILDREN)); 2920 } 2921 } 2922 2923 out: 2924 dsl_dataset_rele(ds, FTAG); 2925 } 2926 2927 static int 2928 enqueue_cb(dsl_pool_t *dp, dsl_dataset_t *hds, void *arg) 2929 { 2930 (void) arg; 2931 dsl_dataset_t *ds; 2932 int err; 2933 dsl_scan_t *scn = dp->dp_scan; 2934 2935 err = dsl_dataset_hold_obj(dp, hds->ds_object, FTAG, &ds); 2936 if (err) 2937 return (err); 2938 2939 while (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) { 2940 dsl_dataset_t *prev; 2941 err = dsl_dataset_hold_obj(dp, 2942 dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &prev); 2943 if (err) { 2944 dsl_dataset_rele(ds, FTAG); 2945 return (err); 2946 } 2947 2948 /* 2949 * If this is a clone, we don't need to worry about it for now. 2950 */ 2951 if (dsl_dataset_phys(prev)->ds_next_snap_obj != ds->ds_object) { 2952 dsl_dataset_rele(ds, FTAG); 2953 dsl_dataset_rele(prev, FTAG); 2954 return (0); 2955 } 2956 dsl_dataset_rele(ds, FTAG); 2957 ds = prev; 2958 } 2959 2960 mutex_enter(&scn->scn_queue_lock); 2961 scan_ds_queue_insert(scn, ds->ds_object, 2962 dsl_dataset_phys(ds)->ds_prev_snap_txg); 2963 mutex_exit(&scn->scn_queue_lock); 2964 dsl_dataset_rele(ds, FTAG); 2965 return (0); 2966 } 2967 2968 void 2969 dsl_scan_ddt_entry(dsl_scan_t *scn, enum zio_checksum checksum, 2970 ddt_t *ddt, ddt_lightweight_entry_t *ddlwe, dmu_tx_t *tx) 2971 { 2972 (void) tx; 2973 const ddt_key_t *ddk = &ddlwe->ddlwe_key; 2974 blkptr_t bp; 2975 zbookmark_phys_t zb = { 0 }; 2976 2977 if (!dsl_scan_is_running(scn)) 2978 return; 2979 2980 /* 2981 * This function is special because it is the only thing 2982 * that can add scan_io_t's to the vdev scan queues from 2983 * outside dsl_scan_sync(). For the most part this is ok 2984 * as long as it is called from within syncing context. 2985 * However, dsl_scan_sync() expects that no new sio's will 2986 * be added between when all the work for a scan is done 2987 * and the next txg when the scan is actually marked as 2988 * completed. This check ensures we do not issue new sio's 2989 * during this period. 2990 */ 2991 if (scn->scn_done_txg != 0) 2992 return; 2993 2994 for (int p = 0; p < DDT_NPHYS(ddt); p++) { 2995 ddt_phys_variant_t v = DDT_PHYS_VARIANT(ddt, p); 2996 uint64_t phys_birth = ddt_phys_birth(&ddlwe->ddlwe_phys, v); 2997 2998 if (phys_birth == 0 || phys_birth > scn->scn_phys.scn_max_txg) 2999 continue; 3000 ddt_bp_create(checksum, ddk, &ddlwe->ddlwe_phys, v, &bp); 3001 3002 scn->scn_visited_this_txg++; 3003 scan_funcs[scn->scn_phys.scn_func](scn->scn_dp, &bp, &zb); 3004 } 3005 } 3006 3007 /* 3008 * Scrub/dedup interaction. 3009 * 3010 * If there are N references to a deduped block, we don't want to scrub it 3011 * N times -- ideally, we should scrub it exactly once. 3012 * 3013 * We leverage the fact that the dde's replication class (ddt_class_t) 3014 * is ordered from highest replication class (DDT_CLASS_DITTO) to lowest 3015 * (DDT_CLASS_UNIQUE) so that we may walk the DDT in that order. 3016 * 3017 * To prevent excess scrubbing, the scrub begins by walking the DDT 3018 * to find all blocks with refcnt > 1, and scrubs each of these once. 3019 * Since there are two replication classes which contain blocks with 3020 * refcnt > 1, we scrub the highest replication class (DDT_CLASS_DITTO) first. 3021 * Finally the top-down scrub begins, only visiting blocks with refcnt == 1. 3022 * 3023 * There would be nothing more to say if a block's refcnt couldn't change 3024 * during a scrub, but of course it can so we must account for changes 3025 * in a block's replication class. 3026 * 3027 * Here's an example of what can occur: 3028 * 3029 * If a block has refcnt > 1 during the DDT scrub phase, but has refcnt == 1 3030 * when visited during the top-down scrub phase, it will be scrubbed twice. 3031 * This negates our scrub optimization, but is otherwise harmless. 3032 * 3033 * If a block has refcnt == 1 during the DDT scrub phase, but has refcnt > 1 3034 * on each visit during the top-down scrub phase, it will never be scrubbed. 3035 * To catch this, ddt_sync_entry() notifies the scrub code whenever a block's 3036 * reference class transitions to a higher level (i.e DDT_CLASS_UNIQUE to 3037 * DDT_CLASS_DUPLICATE); if it transitions from refcnt == 1 to refcnt > 1 3038 * while a scrub is in progress, it scrubs the block right then. 3039 */ 3040 static void 3041 dsl_scan_ddt(dsl_scan_t *scn, dmu_tx_t *tx) 3042 { 3043 ddt_bookmark_t *ddb = &scn->scn_phys.scn_ddt_bookmark; 3044 ddt_lightweight_entry_t ddlwe = {0}; 3045 int error; 3046 uint64_t n = 0; 3047 3048 while ((error = ddt_walk(scn->scn_dp->dp_spa, ddb, &ddlwe)) == 0) { 3049 ddt_t *ddt; 3050 3051 if (ddb->ddb_class > scn->scn_phys.scn_ddt_class_max) 3052 break; 3053 dprintf("visiting ddb=%llu/%llu/%llu/%llx\n", 3054 (longlong_t)ddb->ddb_class, 3055 (longlong_t)ddb->ddb_type, 3056 (longlong_t)ddb->ddb_checksum, 3057 (longlong_t)ddb->ddb_cursor); 3058 3059 /* There should be no pending changes to the dedup table */ 3060 ddt = scn->scn_dp->dp_spa->spa_ddt[ddb->ddb_checksum]; 3061 ASSERT(avl_first(&ddt->ddt_tree) == NULL); 3062 3063 dsl_scan_ddt_entry(scn, ddb->ddb_checksum, ddt, &ddlwe, tx); 3064 n++; 3065 3066 if (dsl_scan_check_suspend(scn, NULL)) 3067 break; 3068 } 3069 3070 if (error == EAGAIN) { 3071 dsl_scan_check_suspend(scn, NULL); 3072 error = 0; 3073 3074 zfs_dbgmsg("waiting for ddt to become ready for scan " 3075 "on %s with class_max = %u; suspending=%u", 3076 scn->scn_dp->dp_spa->spa_name, 3077 (int)scn->scn_phys.scn_ddt_class_max, 3078 (int)scn->scn_suspending); 3079 } else 3080 zfs_dbgmsg("scanned %llu ddt entries on %s with " 3081 "class_max = %u; suspending=%u", (longlong_t)n, 3082 scn->scn_dp->dp_spa->spa_name, 3083 (int)scn->scn_phys.scn_ddt_class_max, 3084 (int)scn->scn_suspending); 3085 3086 ASSERT(error == 0 || error == ENOENT); 3087 ASSERT(error != ENOENT || 3088 ddb->ddb_class > scn->scn_phys.scn_ddt_class_max); 3089 } 3090 3091 static uint64_t 3092 dsl_scan_ds_maxtxg(dsl_dataset_t *ds) 3093 { 3094 uint64_t smt = ds->ds_dir->dd_pool->dp_scan->scn_phys.scn_max_txg; 3095 if (ds->ds_is_snapshot) 3096 return (MIN(smt, dsl_dataset_phys(ds)->ds_creation_txg)); 3097 return (smt); 3098 } 3099 3100 static void 3101 dsl_scan_visit(dsl_scan_t *scn, dmu_tx_t *tx) 3102 { 3103 scan_ds_t *sds; 3104 dsl_pool_t *dp = scn->scn_dp; 3105 3106 if (scn->scn_phys.scn_ddt_bookmark.ddb_class <= 3107 scn->scn_phys.scn_ddt_class_max) { 3108 scn->scn_phys.scn_cur_min_txg = scn->scn_phys.scn_min_txg; 3109 scn->scn_phys.scn_cur_max_txg = scn->scn_phys.scn_max_txg; 3110 dsl_scan_ddt(scn, tx); 3111 if (scn->scn_suspending) 3112 return; 3113 } 3114 3115 if (scn->scn_phys.scn_bookmark.zb_objset == DMU_META_OBJSET) { 3116 /* First do the MOS & ORIGIN */ 3117 3118 scn->scn_phys.scn_cur_min_txg = scn->scn_phys.scn_min_txg; 3119 scn->scn_phys.scn_cur_max_txg = scn->scn_phys.scn_max_txg; 3120 dsl_scan_visit_rootbp(scn, NULL, 3121 &dp->dp_meta_rootbp, tx); 3122 if (scn->scn_suspending) 3123 return; 3124 3125 if (spa_version(dp->dp_spa) < SPA_VERSION_DSL_SCRUB) { 3126 VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj, 3127 enqueue_cb, NULL, DS_FIND_CHILDREN)); 3128 } else { 3129 dsl_scan_visitds(scn, 3130 dp->dp_origin_snap->ds_object, tx); 3131 } 3132 ASSERT(!scn->scn_suspending); 3133 } else if (scn->scn_phys.scn_bookmark.zb_objset != 3134 ZB_DESTROYED_OBJSET) { 3135 uint64_t dsobj = scn->scn_phys.scn_bookmark.zb_objset; 3136 /* 3137 * If we were suspended, continue from here. Note if the 3138 * ds we were suspended on was deleted, the zb_objset may 3139 * be -1, so we will skip this and find a new objset 3140 * below. 3141 */ 3142 dsl_scan_visitds(scn, dsobj, tx); 3143 if (scn->scn_suspending) 3144 return; 3145 } 3146 3147 /* 3148 * In case we suspended right at the end of the ds, zero the 3149 * bookmark so we don't think that we're still trying to resume. 3150 */ 3151 memset(&scn->scn_phys.scn_bookmark, 0, sizeof (zbookmark_phys_t)); 3152 3153 /* 3154 * Keep pulling things out of the dataset avl queue. Updates to the 3155 * persistent zap-object-as-queue happen only at checkpoints. 3156 */ 3157 while ((sds = avl_first(&scn->scn_queue)) != NULL) { 3158 dsl_dataset_t *ds; 3159 uint64_t dsobj = sds->sds_dsobj; 3160 uint64_t txg = sds->sds_txg; 3161 3162 /* dequeue and free the ds from the queue */ 3163 scan_ds_queue_remove(scn, dsobj); 3164 sds = NULL; 3165 3166 /* set up min / max txg */ 3167 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds)); 3168 if (txg != 0) { 3169 scn->scn_phys.scn_cur_min_txg = 3170 MAX(scn->scn_phys.scn_min_txg, txg); 3171 } else { 3172 scn->scn_phys.scn_cur_min_txg = 3173 MAX(scn->scn_phys.scn_min_txg, 3174 dsl_dataset_phys(ds)->ds_prev_snap_txg); 3175 } 3176 scn->scn_phys.scn_cur_max_txg = dsl_scan_ds_maxtxg(ds); 3177 dsl_dataset_rele(ds, FTAG); 3178 3179 dsl_scan_visitds(scn, dsobj, tx); 3180 if (scn->scn_suspending) 3181 return; 3182 } 3183 3184 /* No more objsets to fetch, we're done */ 3185 scn->scn_phys.scn_bookmark.zb_objset = ZB_DESTROYED_OBJSET; 3186 ASSERT0(scn->scn_suspending); 3187 } 3188 3189 static uint64_t 3190 dsl_scan_count_data_disks(spa_t *spa) 3191 { 3192 vdev_t *rvd = spa->spa_root_vdev; 3193 uint64_t i, leaves = 0; 3194 3195 for (i = 0; i < rvd->vdev_children; i++) { 3196 vdev_t *vd = rvd->vdev_child[i]; 3197 if (vd->vdev_islog || vd->vdev_isspare || vd->vdev_isl2cache) 3198 continue; 3199 leaves += vdev_get_ndisks(vd) - vdev_get_nparity(vd); 3200 } 3201 return (leaves); 3202 } 3203 3204 static void 3205 scan_io_queues_update_zio_stats(dsl_scan_io_queue_t *q, const blkptr_t *bp) 3206 { 3207 int i; 3208 uint64_t cur_size = 0; 3209 3210 for (i = 0; i < BP_GET_NDVAS(bp); i++) { 3211 cur_size += DVA_GET_ASIZE(&bp->blk_dva[i]); 3212 } 3213 3214 q->q_total_zio_size_this_txg += cur_size; 3215 q->q_zios_this_txg++; 3216 } 3217 3218 static void 3219 scan_io_queues_update_seg_stats(dsl_scan_io_queue_t *q, uint64_t start, 3220 uint64_t end) 3221 { 3222 q->q_total_seg_size_this_txg += end - start; 3223 q->q_segs_this_txg++; 3224 } 3225 3226 static boolean_t 3227 scan_io_queue_check_suspend(dsl_scan_t *scn) 3228 { 3229 /* See comment in dsl_scan_check_suspend() */ 3230 uint64_t curr_time_ns = gethrtime(); 3231 uint64_t scan_time_ns = curr_time_ns - scn->scn_sync_start_time; 3232 uint64_t sync_time_ns = curr_time_ns - 3233 scn->scn_dp->dp_spa->spa_sync_starttime; 3234 uint64_t dirty_min_bytes = zfs_dirty_data_max * 3235 zfs_vdev_async_write_active_min_dirty_percent / 100; 3236 uint_t mintime = (scn->scn_phys.scn_func == POOL_SCAN_RESILVER) ? 3237 zfs_resilver_min_time_ms : zfs_scrub_min_time_ms; 3238 3239 return ((NSEC2MSEC(scan_time_ns) > mintime && 3240 (scn->scn_dp->dp_dirty_total >= dirty_min_bytes || 3241 txg_sync_waiting(scn->scn_dp) || 3242 NSEC2SEC(sync_time_ns) >= zfs_txg_timeout)) || 3243 spa_shutting_down(scn->scn_dp->dp_spa)); 3244 } 3245 3246 /* 3247 * Given a list of scan_io_t's in io_list, this issues the I/Os out to 3248 * disk. This consumes the io_list and frees the scan_io_t's. This is 3249 * called when emptying queues, either when we're up against the memory 3250 * limit or when we have finished scanning. Returns B_TRUE if we stopped 3251 * processing the list before we finished. Any sios that were not issued 3252 * will remain in the io_list. 3253 */ 3254 static boolean_t 3255 scan_io_queue_issue(dsl_scan_io_queue_t *queue, list_t *io_list) 3256 { 3257 dsl_scan_t *scn = queue->q_scn; 3258 scan_io_t *sio; 3259 boolean_t suspended = B_FALSE; 3260 3261 while ((sio = list_head(io_list)) != NULL) { 3262 blkptr_t bp; 3263 3264 if (scan_io_queue_check_suspend(scn)) { 3265 suspended = B_TRUE; 3266 break; 3267 } 3268 3269 sio2bp(sio, &bp); 3270 scan_exec_io(scn->scn_dp, &bp, sio->sio_flags, 3271 &sio->sio_zb, queue); 3272 (void) list_remove_head(io_list); 3273 scan_io_queues_update_zio_stats(queue, &bp); 3274 sio_free(sio); 3275 } 3276 return (suspended); 3277 } 3278 3279 /* 3280 * This function removes sios from an IO queue which reside within a given 3281 * zfs_range_seg_t and inserts them (in offset order) into a list. Note that 3282 * we only ever return a maximum of 32 sios at once. If there are more sios 3283 * to process within this segment that did not make it onto the list we 3284 * return B_TRUE and otherwise B_FALSE. 3285 */ 3286 static boolean_t 3287 scan_io_queue_gather(dsl_scan_io_queue_t *queue, zfs_range_seg_t *rs, 3288 list_t *list) 3289 { 3290 scan_io_t *srch_sio, *sio, *next_sio; 3291 avl_index_t idx; 3292 uint_t num_sios = 0; 3293 int64_t bytes_issued = 0; 3294 3295 ASSERT(rs != NULL); 3296 ASSERT(MUTEX_HELD(&queue->q_vd->vdev_scan_io_queue_lock)); 3297 3298 srch_sio = sio_alloc(1); 3299 srch_sio->sio_nr_dvas = 1; 3300 SIO_SET_OFFSET(srch_sio, zfs_rs_get_start(rs, queue->q_exts_by_addr)); 3301 3302 /* 3303 * The exact start of the extent might not contain any matching zios, 3304 * so if that's the case, examine the next one in the tree. 3305 */ 3306 sio = avl_find(&queue->q_sios_by_addr, srch_sio, &idx); 3307 sio_free(srch_sio); 3308 3309 if (sio == NULL) 3310 sio = avl_nearest(&queue->q_sios_by_addr, idx, AVL_AFTER); 3311 3312 while (sio != NULL && SIO_GET_OFFSET(sio) < zfs_rs_get_end(rs, 3313 queue->q_exts_by_addr) && num_sios <= 32) { 3314 ASSERT3U(SIO_GET_OFFSET(sio), >=, zfs_rs_get_start(rs, 3315 queue->q_exts_by_addr)); 3316 ASSERT3U(SIO_GET_END_OFFSET(sio), <=, zfs_rs_get_end(rs, 3317 queue->q_exts_by_addr)); 3318 3319 next_sio = AVL_NEXT(&queue->q_sios_by_addr, sio); 3320 avl_remove(&queue->q_sios_by_addr, sio); 3321 if (avl_is_empty(&queue->q_sios_by_addr)) 3322 atomic_add_64(&queue->q_scn->scn_queues_pending, -1); 3323 queue->q_sio_memused -= SIO_GET_MUSED(sio); 3324 3325 bytes_issued += SIO_GET_ASIZE(sio); 3326 num_sios++; 3327 list_insert_tail(list, sio); 3328 sio = next_sio; 3329 } 3330 3331 /* 3332 * We limit the number of sios we process at once to 32 to avoid 3333 * biting off more than we can chew. If we didn't take everything 3334 * in the segment we update it to reflect the work we were able to 3335 * complete. Otherwise, we remove it from the range tree entirely. 3336 */ 3337 if (sio != NULL && SIO_GET_OFFSET(sio) < zfs_rs_get_end(rs, 3338 queue->q_exts_by_addr)) { 3339 zfs_range_tree_adjust_fill(queue->q_exts_by_addr, rs, 3340 -bytes_issued); 3341 zfs_range_tree_resize_segment(queue->q_exts_by_addr, rs, 3342 SIO_GET_OFFSET(sio), zfs_rs_get_end(rs, 3343 queue->q_exts_by_addr) - SIO_GET_OFFSET(sio)); 3344 queue->q_last_ext_addr = SIO_GET_OFFSET(sio); 3345 return (B_TRUE); 3346 } else { 3347 uint64_t rstart = zfs_rs_get_start(rs, queue->q_exts_by_addr); 3348 uint64_t rend = zfs_rs_get_end(rs, queue->q_exts_by_addr); 3349 zfs_range_tree_remove(queue->q_exts_by_addr, rstart, rend - 3350 rstart); 3351 queue->q_last_ext_addr = -1; 3352 return (B_FALSE); 3353 } 3354 } 3355 3356 /* 3357 * This is called from the queue emptying thread and selects the next 3358 * extent from which we are to issue I/Os. The behavior of this function 3359 * depends on the state of the scan, the current memory consumption and 3360 * whether or not we are performing a scan shutdown. 3361 * 1) We select extents in an elevator algorithm (LBA-order) if the scan 3362 * needs to perform a checkpoint 3363 * 2) We select the largest available extent if we are up against the 3364 * memory limit. 3365 * 3) Otherwise we don't select any extents. 3366 */ 3367 static zfs_range_seg_t * 3368 scan_io_queue_fetch_ext(dsl_scan_io_queue_t *queue) 3369 { 3370 dsl_scan_t *scn = queue->q_scn; 3371 zfs_range_tree_t *rt = queue->q_exts_by_addr; 3372 3373 ASSERT(MUTEX_HELD(&queue->q_vd->vdev_scan_io_queue_lock)); 3374 ASSERT(scn->scn_is_sorted); 3375 3376 if (!scn->scn_checkpointing && !scn->scn_clearing) 3377 return (NULL); 3378 3379 /* 3380 * During normal clearing, we want to issue our largest segments 3381 * first, keeping IO as sequential as possible, and leaving the 3382 * smaller extents for later with the hope that they might eventually 3383 * grow to larger sequential segments. However, when the scan is 3384 * checkpointing, no new extents will be added to the sorting queue, 3385 * so the way we are sorted now is as good as it will ever get. 3386 * In this case, we instead switch to issuing extents in LBA order. 3387 */ 3388 if ((zfs_scan_issue_strategy < 1 && scn->scn_checkpointing) || 3389 zfs_scan_issue_strategy == 1) 3390 return (zfs_range_tree_first(rt)); 3391 3392 /* 3393 * Try to continue previous extent if it is not completed yet. After 3394 * shrink in scan_io_queue_gather() it may no longer be the best, but 3395 * otherwise we leave shorter remnant every txg. 3396 */ 3397 uint64_t start; 3398 uint64_t size = 1ULL << rt->rt_shift; 3399 zfs_range_seg_t *addr_rs; 3400 if (queue->q_last_ext_addr != -1) { 3401 start = queue->q_last_ext_addr; 3402 addr_rs = zfs_range_tree_find(rt, start, size); 3403 if (addr_rs != NULL) 3404 return (addr_rs); 3405 } 3406 3407 /* 3408 * Nothing to continue, so find new best extent. 3409 */ 3410 uint64_t *v = zfs_btree_first(&queue->q_exts_by_size, NULL); 3411 if (v == NULL) 3412 return (NULL); 3413 queue->q_last_ext_addr = start = *v << rt->rt_shift; 3414 3415 /* 3416 * We need to get the original entry in the by_addr tree so we can 3417 * modify it. 3418 */ 3419 addr_rs = zfs_range_tree_find(rt, start, size); 3420 ASSERT3P(addr_rs, !=, NULL); 3421 ASSERT3U(zfs_rs_get_start(addr_rs, rt), ==, start); 3422 ASSERT3U(zfs_rs_get_end(addr_rs, rt), >, start); 3423 return (addr_rs); 3424 } 3425 3426 static void 3427 scan_io_queues_run_one(void *arg) 3428 { 3429 dsl_scan_io_queue_t *queue = arg; 3430 kmutex_t *q_lock = &queue->q_vd->vdev_scan_io_queue_lock; 3431 boolean_t suspended = B_FALSE; 3432 zfs_range_seg_t *rs; 3433 scan_io_t *sio; 3434 zio_t *zio; 3435 list_t sio_list; 3436 3437 ASSERT(queue->q_scn->scn_is_sorted); 3438 3439 list_create(&sio_list, sizeof (scan_io_t), 3440 offsetof(scan_io_t, sio_nodes.sio_list_node)); 3441 zio = zio_null(queue->q_scn->scn_zio_root, queue->q_scn->scn_dp->dp_spa, 3442 NULL, NULL, NULL, ZIO_FLAG_CANFAIL); 3443 mutex_enter(q_lock); 3444 queue->q_zio = zio; 3445 3446 /* Calculate maximum in-flight bytes for this vdev. */ 3447 queue->q_maxinflight_bytes = MAX(1, zfs_scan_vdev_limit * 3448 (vdev_get_ndisks(queue->q_vd) - vdev_get_nparity(queue->q_vd))); 3449 3450 /* reset per-queue scan statistics for this txg */ 3451 queue->q_total_seg_size_this_txg = 0; 3452 queue->q_segs_this_txg = 0; 3453 queue->q_total_zio_size_this_txg = 0; 3454 queue->q_zios_this_txg = 0; 3455 3456 /* loop until we run out of time or sios */ 3457 while ((rs = scan_io_queue_fetch_ext(queue)) != NULL) { 3458 uint64_t seg_start = 0, seg_end = 0; 3459 boolean_t more_left; 3460 3461 ASSERT(list_is_empty(&sio_list)); 3462 3463 /* loop while we still have sios left to process in this rs */ 3464 do { 3465 scan_io_t *first_sio, *last_sio; 3466 3467 /* 3468 * We have selected which extent needs to be 3469 * processed next. Gather up the corresponding sios. 3470 */ 3471 more_left = scan_io_queue_gather(queue, rs, &sio_list); 3472 ASSERT(!list_is_empty(&sio_list)); 3473 first_sio = list_head(&sio_list); 3474 last_sio = list_tail(&sio_list); 3475 3476 seg_end = SIO_GET_END_OFFSET(last_sio); 3477 if (seg_start == 0) 3478 seg_start = SIO_GET_OFFSET(first_sio); 3479 3480 /* 3481 * Issuing sios can take a long time so drop the 3482 * queue lock. The sio queue won't be updated by 3483 * other threads since we're in syncing context so 3484 * we can be sure that our trees will remain exactly 3485 * as we left them. 3486 */ 3487 mutex_exit(q_lock); 3488 suspended = scan_io_queue_issue(queue, &sio_list); 3489 mutex_enter(q_lock); 3490 3491 if (suspended) 3492 break; 3493 } while (more_left); 3494 3495 /* update statistics for debugging purposes */ 3496 scan_io_queues_update_seg_stats(queue, seg_start, seg_end); 3497 3498 if (suspended) 3499 break; 3500 } 3501 3502 /* 3503 * If we were suspended in the middle of processing, 3504 * requeue any unfinished sios and exit. 3505 */ 3506 while ((sio = list_remove_head(&sio_list)) != NULL) 3507 scan_io_queue_insert_impl(queue, sio); 3508 3509 queue->q_zio = NULL; 3510 mutex_exit(q_lock); 3511 zio_nowait(zio); 3512 list_destroy(&sio_list); 3513 } 3514 3515 /* 3516 * Performs an emptying run on all scan queues in the pool. This just 3517 * punches out one thread per top-level vdev, each of which processes 3518 * only that vdev's scan queue. We can parallelize the I/O here because 3519 * we know that each queue's I/Os only affect its own top-level vdev. 3520 * 3521 * This function waits for the queue runs to complete, and must be 3522 * called from dsl_scan_sync (or in general, syncing context). 3523 */ 3524 static void 3525 scan_io_queues_run(dsl_scan_t *scn) 3526 { 3527 spa_t *spa = scn->scn_dp->dp_spa; 3528 3529 ASSERT(scn->scn_is_sorted); 3530 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 3531 3532 if (scn->scn_queues_pending == 0) 3533 return; 3534 3535 if (scn->scn_taskq == NULL) { 3536 int nthreads = spa->spa_root_vdev->vdev_children; 3537 3538 /* 3539 * We need to make this taskq *always* execute as many 3540 * threads in parallel as we have top-level vdevs and no 3541 * less, otherwise strange serialization of the calls to 3542 * scan_io_queues_run_one can occur during spa_sync runs 3543 * and that significantly impacts performance. 3544 */ 3545 scn->scn_taskq = taskq_create("dsl_scan_iss", nthreads, 3546 minclsyspri, nthreads, nthreads, TASKQ_PREPOPULATE); 3547 } 3548 3549 for (uint64_t i = 0; i < spa->spa_root_vdev->vdev_children; i++) { 3550 vdev_t *vd = spa->spa_root_vdev->vdev_child[i]; 3551 3552 mutex_enter(&vd->vdev_scan_io_queue_lock); 3553 if (vd->vdev_scan_io_queue != NULL) { 3554 VERIFY(taskq_dispatch(scn->scn_taskq, 3555 scan_io_queues_run_one, vd->vdev_scan_io_queue, 3556 TQ_SLEEP) != TASKQID_INVALID); 3557 } 3558 mutex_exit(&vd->vdev_scan_io_queue_lock); 3559 } 3560 3561 /* 3562 * Wait for the queues to finish issuing their IOs for this run 3563 * before we return. There may still be IOs in flight at this 3564 * point. 3565 */ 3566 taskq_wait(scn->scn_taskq); 3567 } 3568 3569 static boolean_t 3570 dsl_scan_async_block_should_pause(dsl_scan_t *scn) 3571 { 3572 uint64_t elapsed_nanosecs; 3573 3574 if (zfs_recover) 3575 return (B_FALSE); 3576 3577 if (zfs_async_block_max_blocks != 0 && 3578 scn->scn_visited_this_txg >= zfs_async_block_max_blocks) { 3579 return (B_TRUE); 3580 } 3581 3582 if (zfs_max_async_dedup_frees != 0 && 3583 scn->scn_dedup_frees_this_txg >= zfs_max_async_dedup_frees) { 3584 return (B_TRUE); 3585 } 3586 3587 elapsed_nanosecs = gethrtime() - scn->scn_sync_start_time; 3588 return (elapsed_nanosecs / NANOSEC > zfs_txg_timeout || 3589 (NSEC2MSEC(elapsed_nanosecs) > scn->scn_async_block_min_time_ms && 3590 txg_sync_waiting(scn->scn_dp)) || 3591 spa_shutting_down(scn->scn_dp->dp_spa)); 3592 } 3593 3594 static int 3595 dsl_scan_free_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 3596 { 3597 dsl_scan_t *scn = arg; 3598 3599 if (!scn->scn_is_bptree || 3600 (BP_GET_LEVEL(bp) == 0 && BP_GET_TYPE(bp) != DMU_OT_OBJSET)) { 3601 if (dsl_scan_async_block_should_pause(scn)) 3602 return (SET_ERROR(ERESTART)); 3603 } 3604 3605 zio_nowait(zio_free_sync(scn->scn_zio_root, scn->scn_dp->dp_spa, 3606 dmu_tx_get_txg(tx), bp, 0)); 3607 dsl_dir_diduse_space(tx->tx_pool->dp_free_dir, DD_USED_HEAD, 3608 -bp_get_dsize_sync(scn->scn_dp->dp_spa, bp), 3609 -BP_GET_PSIZE(bp), -BP_GET_UCSIZE(bp), tx); 3610 scn->scn_visited_this_txg++; 3611 if (BP_GET_DEDUP(bp)) 3612 scn->scn_dedup_frees_this_txg++; 3613 return (0); 3614 } 3615 3616 static void 3617 dsl_scan_update_stats(dsl_scan_t *scn) 3618 { 3619 spa_t *spa = scn->scn_dp->dp_spa; 3620 uint64_t i; 3621 uint64_t seg_size_total = 0, zio_size_total = 0; 3622 uint64_t seg_count_total = 0, zio_count_total = 0; 3623 3624 for (i = 0; i < spa->spa_root_vdev->vdev_children; i++) { 3625 vdev_t *vd = spa->spa_root_vdev->vdev_child[i]; 3626 dsl_scan_io_queue_t *queue = vd->vdev_scan_io_queue; 3627 3628 if (queue == NULL) 3629 continue; 3630 3631 seg_size_total += queue->q_total_seg_size_this_txg; 3632 zio_size_total += queue->q_total_zio_size_this_txg; 3633 seg_count_total += queue->q_segs_this_txg; 3634 zio_count_total += queue->q_zios_this_txg; 3635 } 3636 3637 if (seg_count_total == 0 || zio_count_total == 0) { 3638 scn->scn_avg_seg_size_this_txg = 0; 3639 scn->scn_avg_zio_size_this_txg = 0; 3640 scn->scn_segs_this_txg = 0; 3641 scn->scn_zios_this_txg = 0; 3642 return; 3643 } 3644 3645 scn->scn_avg_seg_size_this_txg = seg_size_total / seg_count_total; 3646 scn->scn_avg_zio_size_this_txg = zio_size_total / zio_count_total; 3647 scn->scn_segs_this_txg = seg_count_total; 3648 scn->scn_zios_this_txg = zio_count_total; 3649 } 3650 3651 static int 3652 bpobj_dsl_scan_free_block_cb(void *arg, const blkptr_t *bp, boolean_t bp_freed, 3653 dmu_tx_t *tx) 3654 { 3655 ASSERT(!bp_freed); 3656 return (dsl_scan_free_block_cb(arg, bp, tx)); 3657 } 3658 3659 static int 3660 dsl_scan_obsolete_block_cb(void *arg, const blkptr_t *bp, boolean_t bp_freed, 3661 dmu_tx_t *tx) 3662 { 3663 ASSERT(!bp_freed); 3664 dsl_scan_t *scn = arg; 3665 const dva_t *dva = &bp->blk_dva[0]; 3666 3667 if (dsl_scan_async_block_should_pause(scn)) 3668 return (SET_ERROR(ERESTART)); 3669 3670 spa_vdev_indirect_mark_obsolete(scn->scn_dp->dp_spa, 3671 DVA_GET_VDEV(dva), DVA_GET_OFFSET(dva), 3672 DVA_GET_ASIZE(dva), tx); 3673 scn->scn_visited_this_txg++; 3674 return (0); 3675 } 3676 3677 boolean_t 3678 dsl_scan_active(dsl_scan_t *scn) 3679 { 3680 spa_t *spa = scn->scn_dp->dp_spa; 3681 uint64_t used = 0, comp, uncomp; 3682 boolean_t clones_left; 3683 3684 if (spa->spa_load_state != SPA_LOAD_NONE) 3685 return (B_FALSE); 3686 if (spa_shutting_down(spa)) 3687 return (B_FALSE); 3688 if ((dsl_scan_is_running(scn) && !dsl_scan_is_paused_scrub(scn)) || 3689 (scn->scn_async_destroying && !scn->scn_async_stalled)) 3690 return (B_TRUE); 3691 3692 if (spa_version(scn->scn_dp->dp_spa) >= SPA_VERSION_DEADLISTS) { 3693 (void) bpobj_space(&scn->scn_dp->dp_free_bpobj, 3694 &used, &comp, &uncomp); 3695 } 3696 clones_left = spa_livelist_delete_check(spa); 3697 return ((used != 0) || (clones_left)); 3698 } 3699 3700 boolean_t 3701 dsl_errorscrub_active(dsl_scan_t *scn) 3702 { 3703 spa_t *spa = scn->scn_dp->dp_spa; 3704 if (spa->spa_load_state != SPA_LOAD_NONE) 3705 return (B_FALSE); 3706 if (spa_shutting_down(spa)) 3707 return (B_FALSE); 3708 if (dsl_errorscrubbing(scn->scn_dp)) 3709 return (B_TRUE); 3710 return (B_FALSE); 3711 } 3712 3713 static boolean_t 3714 dsl_scan_check_deferred(vdev_t *vd) 3715 { 3716 boolean_t need_resilver = B_FALSE; 3717 3718 for (int c = 0; c < vd->vdev_children; c++) { 3719 need_resilver |= 3720 dsl_scan_check_deferred(vd->vdev_child[c]); 3721 } 3722 3723 if (!vdev_is_concrete(vd) || vd->vdev_aux || 3724 !vd->vdev_ops->vdev_op_leaf) 3725 return (need_resilver); 3726 3727 if (!vd->vdev_resilver_deferred) 3728 need_resilver = B_TRUE; 3729 3730 return (need_resilver); 3731 } 3732 3733 static boolean_t 3734 dsl_scan_need_resilver(spa_t *spa, const dva_t *dva, size_t psize, 3735 uint64_t phys_birth) 3736 { 3737 vdev_t *vd; 3738 3739 vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva)); 3740 3741 if (vd->vdev_ops == &vdev_indirect_ops) { 3742 /* 3743 * The indirect vdev can point to multiple 3744 * vdevs. For simplicity, always create 3745 * the resilver zio_t. zio_vdev_io_start() 3746 * will bypass the child resilver i/o's if 3747 * they are on vdevs that don't have DTL's. 3748 */ 3749 return (B_TRUE); 3750 } 3751 3752 if (DVA_GET_GANG(dva)) { 3753 /* 3754 * Gang members may be spread across multiple 3755 * vdevs, so the best estimate we have is the 3756 * scrub range, which has already been checked. 3757 * XXX -- it would be better to change our 3758 * allocation policy to ensure that all 3759 * gang members reside on the same vdev. 3760 */ 3761 return (B_TRUE); 3762 } 3763 3764 /* 3765 * Check if the top-level vdev must resilver this offset. 3766 * When the offset does not intersect with a dirty leaf DTL 3767 * then it may be possible to skip the resilver IO. The psize 3768 * is provided instead of asize to simplify the check for RAIDZ. 3769 */ 3770 if (!vdev_dtl_need_resilver(vd, dva, psize, phys_birth)) 3771 return (B_FALSE); 3772 3773 /* 3774 * Check that this top-level vdev has a device under it which 3775 * is resilvering and is not deferred. 3776 */ 3777 if (!dsl_scan_check_deferred(vd)) 3778 return (B_FALSE); 3779 3780 return (B_TRUE); 3781 } 3782 3783 static int 3784 dsl_process_async_destroys(dsl_pool_t *dp, dmu_tx_t *tx) 3785 { 3786 dsl_scan_t *scn = dp->dp_scan; 3787 spa_t *spa = dp->dp_spa; 3788 int err = 0; 3789 3790 if (spa_suspend_async_destroy(spa)) 3791 return (0); 3792 3793 if (zfs_free_bpobj_enabled && 3794 spa_version(spa) >= SPA_VERSION_DEADLISTS) { 3795 scn->scn_is_bptree = B_FALSE; 3796 scn->scn_async_block_min_time_ms = zfs_free_min_time_ms; 3797 scn->scn_zio_root = zio_root(spa, NULL, 3798 NULL, ZIO_FLAG_MUSTSUCCEED); 3799 err = bpobj_iterate(&dp->dp_free_bpobj, 3800 bpobj_dsl_scan_free_block_cb, scn, tx); 3801 VERIFY0(zio_wait(scn->scn_zio_root)); 3802 scn->scn_zio_root = NULL; 3803 3804 if (err != 0 && err != ERESTART) 3805 zfs_panic_recover("error %u from bpobj_iterate()", err); 3806 } 3807 3808 if (err == 0 && spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY)) { 3809 ASSERT(scn->scn_async_destroying); 3810 scn->scn_is_bptree = B_TRUE; 3811 scn->scn_zio_root = zio_root(spa, NULL, 3812 NULL, ZIO_FLAG_MUSTSUCCEED); 3813 err = bptree_iterate(dp->dp_meta_objset, 3814 dp->dp_bptree_obj, B_TRUE, dsl_scan_free_block_cb, scn, tx); 3815 VERIFY0(zio_wait(scn->scn_zio_root)); 3816 scn->scn_zio_root = NULL; 3817 3818 if (err == EIO || err == ECKSUM) { 3819 err = 0; 3820 } else if (err != 0 && err != ERESTART) { 3821 zfs_panic_recover("error %u from " 3822 "traverse_dataset_destroyed()", err); 3823 } 3824 3825 if (bptree_is_empty(dp->dp_meta_objset, dp->dp_bptree_obj)) { 3826 /* finished; deactivate async destroy feature */ 3827 spa_feature_decr(spa, SPA_FEATURE_ASYNC_DESTROY, tx); 3828 ASSERT(!spa_feature_is_active(spa, 3829 SPA_FEATURE_ASYNC_DESTROY)); 3830 VERIFY0(zap_remove(dp->dp_meta_objset, 3831 DMU_POOL_DIRECTORY_OBJECT, 3832 DMU_POOL_BPTREE_OBJ, tx)); 3833 VERIFY0(bptree_free(dp->dp_meta_objset, 3834 dp->dp_bptree_obj, tx)); 3835 dp->dp_bptree_obj = 0; 3836 scn->scn_async_destroying = B_FALSE; 3837 scn->scn_async_stalled = B_FALSE; 3838 } else { 3839 /* 3840 * If we didn't make progress, mark the async 3841 * destroy as stalled, so that we will not initiate 3842 * a spa_sync() on its behalf. Note that we only 3843 * check this if we are not finished, because if the 3844 * bptree had no blocks for us to visit, we can 3845 * finish without "making progress". 3846 */ 3847 scn->scn_async_stalled = 3848 (scn->scn_visited_this_txg == 0); 3849 } 3850 } 3851 if (scn->scn_visited_this_txg) { 3852 zfs_dbgmsg("freed %llu blocks in %llums from " 3853 "free_bpobj/bptree on %s in txg %llu; err=%u", 3854 (longlong_t)scn->scn_visited_this_txg, 3855 (longlong_t) 3856 NSEC2MSEC(gethrtime() - scn->scn_sync_start_time), 3857 spa->spa_name, (longlong_t)tx->tx_txg, err); 3858 scn->scn_visited_this_txg = 0; 3859 scn->scn_dedup_frees_this_txg = 0; 3860 3861 /* 3862 * Write out changes to the DDT and the BRT that may be required 3863 * as a result of the blocks freed. This ensures that the DDT 3864 * and the BRT are clean when a scrub/resilver runs. 3865 */ 3866 ddt_sync(spa, tx->tx_txg); 3867 brt_sync(spa, tx->tx_txg); 3868 } 3869 if (err != 0) 3870 return (err); 3871 if (dp->dp_free_dir != NULL && !scn->scn_async_destroying && 3872 zfs_free_leak_on_eio && 3873 (dsl_dir_phys(dp->dp_free_dir)->dd_used_bytes != 0 || 3874 dsl_dir_phys(dp->dp_free_dir)->dd_compressed_bytes != 0 || 3875 dsl_dir_phys(dp->dp_free_dir)->dd_uncompressed_bytes != 0)) { 3876 /* 3877 * We have finished background destroying, but there is still 3878 * some space left in the dp_free_dir. Transfer this leaked 3879 * space to the dp_leak_dir. 3880 */ 3881 if (dp->dp_leak_dir == NULL) { 3882 rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG); 3883 (void) dsl_dir_create_sync(dp, dp->dp_root_dir, 3884 LEAK_DIR_NAME, tx); 3885 VERIFY0(dsl_pool_open_special_dir(dp, 3886 LEAK_DIR_NAME, &dp->dp_leak_dir)); 3887 rrw_exit(&dp->dp_config_rwlock, FTAG); 3888 } 3889 dsl_dir_diduse_space(dp->dp_leak_dir, DD_USED_HEAD, 3890 dsl_dir_phys(dp->dp_free_dir)->dd_used_bytes, 3891 dsl_dir_phys(dp->dp_free_dir)->dd_compressed_bytes, 3892 dsl_dir_phys(dp->dp_free_dir)->dd_uncompressed_bytes, tx); 3893 dsl_dir_diduse_space(dp->dp_free_dir, DD_USED_HEAD, 3894 -dsl_dir_phys(dp->dp_free_dir)->dd_used_bytes, 3895 -dsl_dir_phys(dp->dp_free_dir)->dd_compressed_bytes, 3896 -dsl_dir_phys(dp->dp_free_dir)->dd_uncompressed_bytes, tx); 3897 } 3898 3899 if (dp->dp_free_dir != NULL && !scn->scn_async_destroying && 3900 !spa_livelist_delete_check(spa)) { 3901 /* finished; verify that space accounting went to zero */ 3902 ASSERT0(dsl_dir_phys(dp->dp_free_dir)->dd_used_bytes); 3903 ASSERT0(dsl_dir_phys(dp->dp_free_dir)->dd_compressed_bytes); 3904 ASSERT0(dsl_dir_phys(dp->dp_free_dir)->dd_uncompressed_bytes); 3905 } 3906 3907 spa_notify_waiters(spa); 3908 3909 EQUIV(bpobj_is_open(&dp->dp_obsolete_bpobj), 3910 0 == zap_contains(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 3911 DMU_POOL_OBSOLETE_BPOBJ)); 3912 if (err == 0 && bpobj_is_open(&dp->dp_obsolete_bpobj)) { 3913 ASSERT(spa_feature_is_active(dp->dp_spa, 3914 SPA_FEATURE_OBSOLETE_COUNTS)); 3915 3916 scn->scn_is_bptree = B_FALSE; 3917 scn->scn_async_block_min_time_ms = zfs_obsolete_min_time_ms; 3918 err = bpobj_iterate(&dp->dp_obsolete_bpobj, 3919 dsl_scan_obsolete_block_cb, scn, tx); 3920 if (err != 0 && err != ERESTART) 3921 zfs_panic_recover("error %u from bpobj_iterate()", err); 3922 3923 if (bpobj_is_empty(&dp->dp_obsolete_bpobj)) 3924 dsl_pool_destroy_obsolete_bpobj(dp, tx); 3925 } 3926 return (0); 3927 } 3928 3929 static void 3930 name_to_bookmark(char *buf, zbookmark_phys_t *zb) 3931 { 3932 zb->zb_objset = zfs_strtonum(buf, &buf); 3933 ASSERT(*buf == ':'); 3934 zb->zb_object = zfs_strtonum(buf + 1, &buf); 3935 ASSERT(*buf == ':'); 3936 zb->zb_level = (int)zfs_strtonum(buf + 1, &buf); 3937 ASSERT(*buf == ':'); 3938 zb->zb_blkid = zfs_strtonum(buf + 1, &buf); 3939 ASSERT(*buf == '\0'); 3940 } 3941 3942 static void 3943 name_to_object(char *buf, uint64_t *obj) 3944 { 3945 *obj = zfs_strtonum(buf, &buf); 3946 ASSERT(*buf == '\0'); 3947 } 3948 3949 static void 3950 read_by_block_level(dsl_scan_t *scn, zbookmark_phys_t zb) 3951 { 3952 dsl_pool_t *dp = scn->scn_dp; 3953 dsl_dataset_t *ds; 3954 objset_t *os; 3955 if (dsl_dataset_hold_obj(dp, zb.zb_objset, FTAG, &ds) != 0) 3956 return; 3957 3958 if (dmu_objset_from_ds(ds, &os) != 0) { 3959 dsl_dataset_rele(ds, FTAG); 3960 return; 3961 } 3962 3963 /* 3964 * If the key is not loaded dbuf_dnode_findbp() will error out with 3965 * EACCES. However in that case dnode_hold() will eventually call 3966 * dbuf_read()->zio_wait() which may call spa_log_error(). This will 3967 * lead to a deadlock due to us holding the mutex spa_errlist_lock. 3968 * Avoid this by checking here if the keys are loaded, if not return. 3969 * If the keys are not loaded the head_errlog feature is meaningless 3970 * as we cannot figure out the birth txg of the block pointer. 3971 */ 3972 if (dsl_dataset_get_keystatus(ds->ds_dir) == 3973 ZFS_KEYSTATUS_UNAVAILABLE) { 3974 dsl_dataset_rele(ds, FTAG); 3975 return; 3976 } 3977 3978 dnode_t *dn; 3979 blkptr_t bp; 3980 3981 if (dnode_hold(os, zb.zb_object, FTAG, &dn) != 0) { 3982 dsl_dataset_rele(ds, FTAG); 3983 return; 3984 } 3985 3986 rw_enter(&dn->dn_struct_rwlock, RW_READER); 3987 int error = dbuf_dnode_findbp(dn, zb.zb_level, zb.zb_blkid, &bp, NULL, 3988 NULL); 3989 3990 if (error) { 3991 rw_exit(&dn->dn_struct_rwlock); 3992 dnode_rele(dn, FTAG); 3993 dsl_dataset_rele(ds, FTAG); 3994 return; 3995 } 3996 3997 if (!error && BP_IS_HOLE(&bp)) { 3998 rw_exit(&dn->dn_struct_rwlock); 3999 dnode_rele(dn, FTAG); 4000 dsl_dataset_rele(ds, FTAG); 4001 return; 4002 } 4003 4004 int zio_flags = ZIO_FLAG_SCAN_THREAD | ZIO_FLAG_RAW | 4005 ZIO_FLAG_CANFAIL | ZIO_FLAG_SCRUB; 4006 4007 /* If it's an intent log block, failure is expected. */ 4008 if (zb.zb_level == ZB_ZIL_LEVEL) 4009 zio_flags |= ZIO_FLAG_SPECULATIVE; 4010 4011 ASSERT(!BP_IS_EMBEDDED(&bp)); 4012 scan_exec_io(dp, &bp, zio_flags, &zb, NULL); 4013 rw_exit(&dn->dn_struct_rwlock); 4014 dnode_rele(dn, FTAG); 4015 dsl_dataset_rele(ds, FTAG); 4016 } 4017 4018 /* 4019 * We keep track of the scrubbed error blocks in "count". This will be used 4020 * when deciding whether we exceeded zfs_scrub_error_blocks_per_txg. This 4021 * function is modelled after check_filesystem(). 4022 */ 4023 static int 4024 scrub_filesystem(spa_t *spa, uint64_t fs, zbookmark_err_phys_t *zep, 4025 int *count) 4026 { 4027 dsl_dataset_t *ds; 4028 dsl_pool_t *dp = spa->spa_dsl_pool; 4029 dsl_scan_t *scn = dp->dp_scan; 4030 4031 int error = dsl_dataset_hold_obj(dp, fs, FTAG, &ds); 4032 if (error != 0) 4033 return (error); 4034 4035 uint64_t latest_txg; 4036 uint64_t txg_to_consider = spa->spa_syncing_txg; 4037 boolean_t check_snapshot = B_TRUE; 4038 4039 error = find_birth_txg(ds, zep, &latest_txg); 4040 4041 /* 4042 * If find_birth_txg() errors out, then err on the side of caution and 4043 * proceed. In worst case scenario scrub all objects. If zep->zb_birth 4044 * is 0 (e.g. in case of encryption with unloaded keys) also proceed to 4045 * scrub all objects. 4046 */ 4047 if (error == 0 && zep->zb_birth == latest_txg) { 4048 /* Block neither free nor re written. */ 4049 zbookmark_phys_t zb; 4050 zep_to_zb(fs, zep, &zb); 4051 scn->scn_zio_root = zio_root(spa, NULL, NULL, 4052 ZIO_FLAG_CANFAIL); 4053 /* We have already acquired the config lock for spa */ 4054 read_by_block_level(scn, zb); 4055 4056 (void) zio_wait(scn->scn_zio_root); 4057 scn->scn_zio_root = NULL; 4058 4059 scn->errorscrub_phys.dep_examined++; 4060 scn->errorscrub_phys.dep_to_examine--; 4061 (*count)++; 4062 if ((*count) == zfs_scrub_error_blocks_per_txg || 4063 dsl_error_scrub_check_suspend(scn, &zb)) { 4064 dsl_dataset_rele(ds, FTAG); 4065 return (SET_ERROR(EFAULT)); 4066 } 4067 4068 check_snapshot = B_FALSE; 4069 } else if (error == 0) { 4070 txg_to_consider = latest_txg; 4071 } 4072 4073 /* 4074 * Retrieve the number of snapshots if the dataset is not a snapshot. 4075 */ 4076 uint64_t snap_count = 0; 4077 if (dsl_dataset_phys(ds)->ds_snapnames_zapobj != 0) { 4078 4079 error = zap_count(spa->spa_meta_objset, 4080 dsl_dataset_phys(ds)->ds_snapnames_zapobj, &snap_count); 4081 4082 if (error != 0) { 4083 dsl_dataset_rele(ds, FTAG); 4084 return (error); 4085 } 4086 } 4087 4088 if (snap_count == 0) { 4089 /* Filesystem without snapshots. */ 4090 dsl_dataset_rele(ds, FTAG); 4091 return (0); 4092 } 4093 4094 uint64_t snap_obj = dsl_dataset_phys(ds)->ds_prev_snap_obj; 4095 uint64_t snap_obj_txg = dsl_dataset_phys(ds)->ds_prev_snap_txg; 4096 4097 dsl_dataset_rele(ds, FTAG); 4098 4099 /* Check only snapshots created from this file system. */ 4100 while (snap_obj != 0 && zep->zb_birth < snap_obj_txg && 4101 snap_obj_txg <= txg_to_consider) { 4102 4103 error = dsl_dataset_hold_obj(dp, snap_obj, FTAG, &ds); 4104 if (error != 0) 4105 return (error); 4106 4107 if (dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj != fs) { 4108 snap_obj = dsl_dataset_phys(ds)->ds_prev_snap_obj; 4109 snap_obj_txg = dsl_dataset_phys(ds)->ds_prev_snap_txg; 4110 dsl_dataset_rele(ds, FTAG); 4111 continue; 4112 } 4113 4114 boolean_t affected = B_TRUE; 4115 if (check_snapshot) { 4116 uint64_t blk_txg; 4117 error = find_birth_txg(ds, zep, &blk_txg); 4118 4119 /* 4120 * Scrub the snapshot also when zb_birth == 0 or when 4121 * find_birth_txg() returns an error. 4122 */ 4123 affected = (error == 0 && zep->zb_birth == blk_txg) || 4124 (error != 0) || (zep->zb_birth == 0); 4125 } 4126 4127 /* Scrub snapshots. */ 4128 if (affected) { 4129 zbookmark_phys_t zb; 4130 zep_to_zb(snap_obj, zep, &zb); 4131 scn->scn_zio_root = zio_root(spa, NULL, NULL, 4132 ZIO_FLAG_CANFAIL); 4133 /* We have already acquired the config lock for spa */ 4134 read_by_block_level(scn, zb); 4135 4136 (void) zio_wait(scn->scn_zio_root); 4137 scn->scn_zio_root = NULL; 4138 4139 scn->errorscrub_phys.dep_examined++; 4140 scn->errorscrub_phys.dep_to_examine--; 4141 (*count)++; 4142 if ((*count) == zfs_scrub_error_blocks_per_txg || 4143 dsl_error_scrub_check_suspend(scn, &zb)) { 4144 dsl_dataset_rele(ds, FTAG); 4145 return (EFAULT); 4146 } 4147 } 4148 snap_obj_txg = dsl_dataset_phys(ds)->ds_prev_snap_txg; 4149 snap_obj = dsl_dataset_phys(ds)->ds_prev_snap_obj; 4150 dsl_dataset_rele(ds, FTAG); 4151 } 4152 return (0); 4153 } 4154 4155 void 4156 dsl_errorscrub_sync(dsl_pool_t *dp, dmu_tx_t *tx) 4157 { 4158 spa_t *spa = dp->dp_spa; 4159 dsl_scan_t *scn = dp->dp_scan; 4160 4161 /* 4162 * Only process scans in sync pass 1. 4163 */ 4164 4165 if (spa_sync_pass(spa) > 1) 4166 return; 4167 4168 /* 4169 * If the spa is shutting down, then stop scanning. This will 4170 * ensure that the scan does not dirty any new data during the 4171 * shutdown phase. 4172 */ 4173 if (spa_shutting_down(spa)) 4174 return; 4175 4176 if (!dsl_errorscrub_active(scn) || dsl_errorscrub_is_paused(scn)) { 4177 return; 4178 } 4179 4180 if (dsl_scan_resilvering(scn->scn_dp)) { 4181 /* cancel the error scrub if resilver started */ 4182 dsl_scan_cancel(scn->scn_dp); 4183 return; 4184 } 4185 4186 spa->spa_scrub_active = B_TRUE; 4187 scn->scn_sync_start_time = gethrtime(); 4188 4189 /* 4190 * zfs_scan_suspend_progress can be set to disable scrub progress. 4191 * See more detailed comment in dsl_scan_sync(). 4192 */ 4193 if (zfs_scan_suspend_progress) { 4194 uint64_t scan_time_ns = gethrtime() - scn->scn_sync_start_time; 4195 int mintime = zfs_scrub_min_time_ms; 4196 4197 while (zfs_scan_suspend_progress && 4198 !txg_sync_waiting(scn->scn_dp) && 4199 !spa_shutting_down(scn->scn_dp->dp_spa) && 4200 NSEC2MSEC(scan_time_ns) < mintime) { 4201 delay(hz); 4202 scan_time_ns = gethrtime() - scn->scn_sync_start_time; 4203 } 4204 return; 4205 } 4206 4207 int i = 0; 4208 zap_attribute_t *za; 4209 zbookmark_phys_t *zb; 4210 boolean_t limit_exceeded = B_FALSE; 4211 4212 za = zap_attribute_alloc(); 4213 zb = kmem_zalloc(sizeof (zbookmark_phys_t), KM_SLEEP); 4214 4215 if (!spa_feature_is_enabled(spa, SPA_FEATURE_HEAD_ERRLOG)) { 4216 for (; zap_cursor_retrieve(&scn->errorscrub_cursor, za) == 0; 4217 zap_cursor_advance(&scn->errorscrub_cursor)) { 4218 name_to_bookmark(za->za_name, zb); 4219 4220 scn->scn_zio_root = zio_root(dp->dp_spa, NULL, 4221 NULL, ZIO_FLAG_CANFAIL); 4222 dsl_pool_config_enter(dp, FTAG); 4223 read_by_block_level(scn, *zb); 4224 dsl_pool_config_exit(dp, FTAG); 4225 4226 (void) zio_wait(scn->scn_zio_root); 4227 scn->scn_zio_root = NULL; 4228 4229 scn->errorscrub_phys.dep_examined += 1; 4230 scn->errorscrub_phys.dep_to_examine -= 1; 4231 i++; 4232 if (i == zfs_scrub_error_blocks_per_txg || 4233 dsl_error_scrub_check_suspend(scn, zb)) { 4234 limit_exceeded = B_TRUE; 4235 break; 4236 } 4237 } 4238 4239 if (!limit_exceeded) 4240 dsl_errorscrub_done(scn, B_TRUE, tx); 4241 4242 dsl_errorscrub_sync_state(scn, tx); 4243 zap_attribute_free(za); 4244 kmem_free(zb, sizeof (*zb)); 4245 return; 4246 } 4247 4248 int error = 0; 4249 for (; zap_cursor_retrieve(&scn->errorscrub_cursor, za) == 0; 4250 zap_cursor_advance(&scn->errorscrub_cursor)) { 4251 4252 zap_cursor_t *head_ds_cursor; 4253 zap_attribute_t *head_ds_attr; 4254 zbookmark_err_phys_t head_ds_block; 4255 4256 head_ds_cursor = kmem_zalloc(sizeof (zap_cursor_t), KM_SLEEP); 4257 head_ds_attr = zap_attribute_alloc(); 4258 4259 uint64_t head_ds_err_obj = za->za_first_integer; 4260 uint64_t head_ds; 4261 name_to_object(za->za_name, &head_ds); 4262 boolean_t config_held = B_FALSE; 4263 uint64_t top_affected_fs; 4264 4265 for (zap_cursor_init(head_ds_cursor, spa->spa_meta_objset, 4266 head_ds_err_obj); zap_cursor_retrieve(head_ds_cursor, 4267 head_ds_attr) == 0; zap_cursor_advance(head_ds_cursor)) { 4268 4269 name_to_errphys(head_ds_attr->za_name, &head_ds_block); 4270 4271 /* 4272 * In case we are called from spa_sync the pool 4273 * config is already held. 4274 */ 4275 if (!dsl_pool_config_held(dp)) { 4276 dsl_pool_config_enter(dp, FTAG); 4277 config_held = B_TRUE; 4278 } 4279 4280 error = find_top_affected_fs(spa, 4281 head_ds, &head_ds_block, &top_affected_fs); 4282 if (error) 4283 break; 4284 4285 error = scrub_filesystem(spa, top_affected_fs, 4286 &head_ds_block, &i); 4287 4288 if (error == SET_ERROR(EFAULT)) { 4289 limit_exceeded = B_TRUE; 4290 break; 4291 } 4292 } 4293 4294 zap_cursor_fini(head_ds_cursor); 4295 kmem_free(head_ds_cursor, sizeof (*head_ds_cursor)); 4296 zap_attribute_free(head_ds_attr); 4297 4298 if (config_held) 4299 dsl_pool_config_exit(dp, FTAG); 4300 } 4301 4302 zap_attribute_free(za); 4303 kmem_free(zb, sizeof (*zb)); 4304 if (!limit_exceeded) 4305 dsl_errorscrub_done(scn, B_TRUE, tx); 4306 4307 dsl_errorscrub_sync_state(scn, tx); 4308 } 4309 4310 /* 4311 * This is the primary entry point for scans that is called from syncing 4312 * context. Scans must happen entirely during syncing context so that we 4313 * can guarantee that blocks we are currently scanning will not change out 4314 * from under us. While a scan is active, this function controls how quickly 4315 * transaction groups proceed, instead of the normal handling provided by 4316 * txg_sync_thread(). 4317 */ 4318 void 4319 dsl_scan_sync(dsl_pool_t *dp, dmu_tx_t *tx) 4320 { 4321 int err = 0; 4322 dsl_scan_t *scn = dp->dp_scan; 4323 spa_t *spa = dp->dp_spa; 4324 state_sync_type_t sync_type = SYNC_OPTIONAL; 4325 int restart_early = 0; 4326 4327 if (spa->spa_resilver_deferred) { 4328 uint64_t to_issue, issued; 4329 4330 if (!spa_feature_is_active(dp->dp_spa, 4331 SPA_FEATURE_RESILVER_DEFER)) 4332 spa_feature_incr(spa, SPA_FEATURE_RESILVER_DEFER, tx); 4333 4334 /* 4335 * See print_scan_scrub_resilver_status() issued/total_i 4336 * @ cmd/zpool/zpool_main.c 4337 */ 4338 to_issue = 4339 scn->scn_phys.scn_to_examine - scn->scn_phys.scn_skipped; 4340 issued = 4341 scn->scn_issued_before_pass + spa->spa_scan_pass_issued; 4342 restart_early = 4343 zfs_resilver_disable_defer || 4344 (issued < (to_issue * zfs_resilver_defer_percent / 100)); 4345 } 4346 4347 /* 4348 * Only process scans in sync pass 1. 4349 */ 4350 if (spa_sync_pass(spa) > 1) 4351 return; 4352 4353 4354 /* 4355 * Check for scn_restart_txg before checking spa_load_state, so 4356 * that we can restart an old-style scan while the pool is being 4357 * imported (see dsl_scan_init). We also restart scans if there 4358 * is a deferred resilver and the user has manually disabled 4359 * deferred resilvers via zfs_resilver_disable_defer, or if the 4360 * current scan progress is below zfs_resilver_defer_percent. 4361 */ 4362 if (dsl_scan_restarting(scn, tx) || restart_early) { 4363 setup_sync_arg_t setup_sync_arg = { 4364 .func = POOL_SCAN_SCRUB, 4365 .txgstart = 0, 4366 .txgend = 0, 4367 }; 4368 dsl_scan_done(scn, B_FALSE, tx); 4369 if (vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) 4370 setup_sync_arg.func = POOL_SCAN_RESILVER; 4371 zfs_dbgmsg("restarting scan func=%u on %s txg=%llu early=%d", 4372 setup_sync_arg.func, dp->dp_spa->spa_name, 4373 (longlong_t)tx->tx_txg, restart_early); 4374 dsl_scan_setup_sync(&setup_sync_arg, tx); 4375 } 4376 4377 /* 4378 * If the spa is shutting down, then stop scanning. This will 4379 * ensure that the scan does not dirty any new data during the 4380 * shutdown phase. 4381 */ 4382 if (spa_shutting_down(spa)) 4383 return; 4384 4385 /* 4386 * If the scan is inactive due to a stalled async destroy, try again. 4387 */ 4388 if (!scn->scn_async_stalled && !dsl_scan_active(scn)) 4389 return; 4390 4391 /* reset scan statistics */ 4392 scn->scn_visited_this_txg = 0; 4393 scn->scn_dedup_frees_this_txg = 0; 4394 scn->scn_holes_this_txg = 0; 4395 scn->scn_lt_min_this_txg = 0; 4396 scn->scn_gt_max_this_txg = 0; 4397 scn->scn_ddt_contained_this_txg = 0; 4398 scn->scn_objsets_visited_this_txg = 0; 4399 scn->scn_avg_seg_size_this_txg = 0; 4400 scn->scn_segs_this_txg = 0; 4401 scn->scn_avg_zio_size_this_txg = 0; 4402 scn->scn_zios_this_txg = 0; 4403 scn->scn_suspending = B_FALSE; 4404 scn->scn_sync_start_time = gethrtime(); 4405 spa->spa_scrub_active = B_TRUE; 4406 4407 /* 4408 * First process the async destroys. If we suspend, don't do 4409 * any scrubbing or resilvering. This ensures that there are no 4410 * async destroys while we are scanning, so the scan code doesn't 4411 * have to worry about traversing it. It is also faster to free the 4412 * blocks than to scrub them. 4413 */ 4414 err = dsl_process_async_destroys(dp, tx); 4415 if (err != 0) 4416 return; 4417 4418 if (!dsl_scan_is_running(scn) || dsl_scan_is_paused_scrub(scn)) 4419 return; 4420 4421 /* 4422 * Wait a few txgs after importing to begin scanning so that 4423 * we can get the pool imported quickly. 4424 */ 4425 if (spa->spa_syncing_txg < spa->spa_first_txg + SCAN_IMPORT_WAIT_TXGS) 4426 return; 4427 4428 /* 4429 * zfs_scan_suspend_progress can be set to disable scan progress. 4430 * We don't want to spin the txg_sync thread, so we add a delay 4431 * here to simulate the time spent doing a scan. This is mostly 4432 * useful for testing and debugging. 4433 */ 4434 if (zfs_scan_suspend_progress) { 4435 uint64_t scan_time_ns = gethrtime() - scn->scn_sync_start_time; 4436 uint_t mintime = (scn->scn_phys.scn_func == 4437 POOL_SCAN_RESILVER) ? zfs_resilver_min_time_ms : 4438 zfs_scrub_min_time_ms; 4439 4440 while (zfs_scan_suspend_progress && 4441 !txg_sync_waiting(scn->scn_dp) && 4442 !spa_shutting_down(scn->scn_dp->dp_spa) && 4443 NSEC2MSEC(scan_time_ns) < mintime) { 4444 delay(hz); 4445 scan_time_ns = gethrtime() - scn->scn_sync_start_time; 4446 } 4447 return; 4448 } 4449 4450 /* 4451 * Disabled by default, set zfs_scan_report_txgs to report 4452 * average performance over the last zfs_scan_report_txgs TXGs. 4453 */ 4454 if (zfs_scan_report_txgs != 0 && 4455 tx->tx_txg % zfs_scan_report_txgs == 0) { 4456 scn->scn_issued_before_pass += spa->spa_scan_pass_issued; 4457 spa_scan_stat_init(spa); 4458 } 4459 4460 /* 4461 * It is possible to switch from unsorted to sorted at any time, 4462 * but afterwards the scan will remain sorted unless reloaded from 4463 * a checkpoint after a reboot. 4464 */ 4465 if (!zfs_scan_legacy) { 4466 scn->scn_is_sorted = B_TRUE; 4467 if (scn->scn_last_checkpoint == 0) 4468 scn->scn_last_checkpoint = ddi_get_lbolt(); 4469 } 4470 4471 /* 4472 * For sorted scans, determine what kind of work we will be doing 4473 * this txg based on our memory limitations and whether or not we 4474 * need to perform a checkpoint. 4475 */ 4476 if (scn->scn_is_sorted) { 4477 /* 4478 * If we are over our checkpoint interval, set scn_clearing 4479 * so that we can begin checkpointing immediately. The 4480 * checkpoint allows us to save a consistent bookmark 4481 * representing how much data we have scrubbed so far. 4482 * Otherwise, use the memory limit to determine if we should 4483 * scan for metadata or start issue scrub IOs. We accumulate 4484 * metadata until we hit our hard memory limit at which point 4485 * we issue scrub IOs until we are at our soft memory limit. 4486 */ 4487 if (scn->scn_checkpointing || 4488 ddi_get_lbolt() - scn->scn_last_checkpoint > 4489 SEC_TO_TICK(zfs_scan_checkpoint_intval)) { 4490 if (!scn->scn_checkpointing) 4491 zfs_dbgmsg("begin scan checkpoint for %s", 4492 spa->spa_name); 4493 4494 scn->scn_checkpointing = B_TRUE; 4495 scn->scn_clearing = B_TRUE; 4496 } else { 4497 boolean_t should_clear = dsl_scan_should_clear(scn); 4498 if (should_clear && !scn->scn_clearing) { 4499 zfs_dbgmsg("begin scan clearing for %s", 4500 spa->spa_name); 4501 scn->scn_clearing = B_TRUE; 4502 } else if (!should_clear && scn->scn_clearing) { 4503 zfs_dbgmsg("finish scan clearing for %s", 4504 spa->spa_name); 4505 scn->scn_clearing = B_FALSE; 4506 } 4507 } 4508 } else { 4509 ASSERT0(scn->scn_checkpointing); 4510 ASSERT0(scn->scn_clearing); 4511 } 4512 4513 if (!scn->scn_clearing && scn->scn_done_txg == 0) { 4514 /* Need to scan metadata for more blocks to scrub */ 4515 dsl_scan_phys_t *scnp = &scn->scn_phys; 4516 taskqid_t prefetch_tqid; 4517 4518 /* 4519 * Calculate the max number of in-flight bytes for pool-wide 4520 * scanning operations (minimum 1MB, maximum 1/4 of arc_c_max). 4521 * Limits for the issuing phase are done per top-level vdev and 4522 * are handled separately. 4523 */ 4524 scn->scn_maxinflight_bytes = MIN(arc_c_max / 4, MAX(1ULL << 20, 4525 zfs_scan_vdev_limit * dsl_scan_count_data_disks(spa))); 4526 4527 if (scnp->scn_ddt_bookmark.ddb_class <= 4528 scnp->scn_ddt_class_max) { 4529 ASSERT(ZB_IS_ZERO(&scnp->scn_bookmark)); 4530 zfs_dbgmsg("doing scan sync for %s txg %llu; " 4531 "ddt bm=%llu/%llu/%llu/%llx", 4532 spa->spa_name, 4533 (longlong_t)tx->tx_txg, 4534 (longlong_t)scnp->scn_ddt_bookmark.ddb_class, 4535 (longlong_t)scnp->scn_ddt_bookmark.ddb_type, 4536 (longlong_t)scnp->scn_ddt_bookmark.ddb_checksum, 4537 (longlong_t)scnp->scn_ddt_bookmark.ddb_cursor); 4538 } else { 4539 zfs_dbgmsg("doing scan sync for %s txg %llu; " 4540 "bm=%llu/%llu/%llu/%llu", 4541 spa->spa_name, 4542 (longlong_t)tx->tx_txg, 4543 (longlong_t)scnp->scn_bookmark.zb_objset, 4544 (longlong_t)scnp->scn_bookmark.zb_object, 4545 (longlong_t)scnp->scn_bookmark.zb_level, 4546 (longlong_t)scnp->scn_bookmark.zb_blkid); 4547 } 4548 4549 scn->scn_zio_root = zio_root(dp->dp_spa, NULL, 4550 NULL, ZIO_FLAG_CANFAIL); 4551 4552 scn->scn_prefetch_stop = B_FALSE; 4553 prefetch_tqid = taskq_dispatch(dp->dp_sync_taskq, 4554 dsl_scan_prefetch_thread, scn, TQ_SLEEP); 4555 ASSERT(prefetch_tqid != TASKQID_INVALID); 4556 4557 dsl_pool_config_enter(dp, FTAG); 4558 dsl_scan_visit(scn, tx); 4559 dsl_pool_config_exit(dp, FTAG); 4560 4561 mutex_enter(&dp->dp_spa->spa_scrub_lock); 4562 scn->scn_prefetch_stop = B_TRUE; 4563 cv_broadcast(&spa->spa_scrub_io_cv); 4564 mutex_exit(&dp->dp_spa->spa_scrub_lock); 4565 4566 taskq_wait_id(dp->dp_sync_taskq, prefetch_tqid); 4567 (void) zio_wait(scn->scn_zio_root); 4568 scn->scn_zio_root = NULL; 4569 4570 zfs_dbgmsg("scan visited %llu blocks of %s in %llums " 4571 "(%llu os's, %llu holes, %llu < mintxg, " 4572 "%llu in ddt, %llu > maxtxg)", 4573 (longlong_t)scn->scn_visited_this_txg, 4574 spa->spa_name, 4575 (longlong_t)NSEC2MSEC(gethrtime() - 4576 scn->scn_sync_start_time), 4577 (longlong_t)scn->scn_objsets_visited_this_txg, 4578 (longlong_t)scn->scn_holes_this_txg, 4579 (longlong_t)scn->scn_lt_min_this_txg, 4580 (longlong_t)scn->scn_ddt_contained_this_txg, 4581 (longlong_t)scn->scn_gt_max_this_txg); 4582 4583 if (!scn->scn_suspending) { 4584 ASSERT0(avl_numnodes(&scn->scn_queue)); 4585 scn->scn_done_txg = tx->tx_txg + 1; 4586 if (scn->scn_is_sorted) { 4587 scn->scn_checkpointing = B_TRUE; 4588 scn->scn_clearing = B_TRUE; 4589 scn->scn_issued_before_pass += 4590 spa->spa_scan_pass_issued; 4591 spa_scan_stat_init(spa); 4592 } 4593 zfs_dbgmsg("scan complete for %s txg %llu", 4594 spa->spa_name, 4595 (longlong_t)tx->tx_txg); 4596 } 4597 } else if (scn->scn_is_sorted && scn->scn_queues_pending != 0) { 4598 ASSERT(scn->scn_clearing); 4599 4600 /* need to issue scrubbing IOs from per-vdev queues */ 4601 scn->scn_zio_root = zio_root(dp->dp_spa, NULL, 4602 NULL, ZIO_FLAG_CANFAIL); 4603 scan_io_queues_run(scn); 4604 (void) zio_wait(scn->scn_zio_root); 4605 scn->scn_zio_root = NULL; 4606 4607 /* calculate and dprintf the current memory usage */ 4608 (void) dsl_scan_should_clear(scn); 4609 dsl_scan_update_stats(scn); 4610 4611 zfs_dbgmsg("scan issued %llu blocks for %s (%llu segs) " 4612 "in %llums (avg_block_size = %llu, avg_seg_size = %llu)", 4613 (longlong_t)scn->scn_zios_this_txg, 4614 spa->spa_name, 4615 (longlong_t)scn->scn_segs_this_txg, 4616 (longlong_t)NSEC2MSEC(gethrtime() - 4617 scn->scn_sync_start_time), 4618 (longlong_t)scn->scn_avg_zio_size_this_txg, 4619 (longlong_t)scn->scn_avg_seg_size_this_txg); 4620 } else if (scn->scn_done_txg != 0 && scn->scn_done_txg <= tx->tx_txg) { 4621 /* Finished with everything. Mark the scrub as complete */ 4622 zfs_dbgmsg("scan issuing complete txg %llu for %s", 4623 (longlong_t)tx->tx_txg, 4624 spa->spa_name); 4625 ASSERT3U(scn->scn_done_txg, !=, 0); 4626 ASSERT0(spa->spa_scrub_inflight); 4627 ASSERT0(scn->scn_queues_pending); 4628 dsl_scan_done(scn, B_TRUE, tx); 4629 sync_type = SYNC_MANDATORY; 4630 } 4631 4632 dsl_scan_sync_state(scn, tx, sync_type); 4633 } 4634 4635 static void 4636 count_block_issued(spa_t *spa, const blkptr_t *bp, boolean_t all) 4637 { 4638 /* 4639 * Don't count embedded bp's, since we already did the work of 4640 * scanning these when we scanned the containing block. 4641 */ 4642 if (BP_IS_EMBEDDED(bp)) 4643 return; 4644 4645 /* 4646 * Update the spa's stats on how many bytes we have issued. 4647 * Sequential scrubs create a zio for each DVA of the bp. Each 4648 * of these will include all DVAs for repair purposes, but the 4649 * zio code will only try the first one unless there is an issue. 4650 * Therefore, we should only count the first DVA for these IOs. 4651 */ 4652 atomic_add_64(&spa->spa_scan_pass_issued, 4653 all ? BP_GET_ASIZE(bp) : DVA_GET_ASIZE(&bp->blk_dva[0])); 4654 } 4655 4656 static void 4657 count_block_skipped(dsl_scan_t *scn, const blkptr_t *bp, boolean_t all) 4658 { 4659 if (BP_IS_EMBEDDED(bp)) 4660 return; 4661 atomic_add_64(&scn->scn_phys.scn_skipped, 4662 all ? BP_GET_ASIZE(bp) : DVA_GET_ASIZE(&bp->blk_dva[0])); 4663 } 4664 4665 static void 4666 count_block(zfs_all_blkstats_t *zab, const blkptr_t *bp) 4667 { 4668 /* 4669 * If we resume after a reboot, zab will be NULL; don't record 4670 * incomplete stats in that case. 4671 */ 4672 if (zab == NULL) 4673 return; 4674 4675 for (int i = 0; i < 4; i++) { 4676 int l = (i < 2) ? BP_GET_LEVEL(bp) : DN_MAX_LEVELS; 4677 int t = (i & 1) ? BP_GET_TYPE(bp) : DMU_OT_TOTAL; 4678 4679 if (t & DMU_OT_NEWTYPE) 4680 t = DMU_OT_OTHER; 4681 zfs_blkstat_t *zb = &zab->zab_type[l][t]; 4682 int equal; 4683 4684 zb->zb_count++; 4685 zb->zb_asize += BP_GET_ASIZE(bp); 4686 zb->zb_lsize += BP_GET_LSIZE(bp); 4687 zb->zb_psize += BP_GET_PSIZE(bp); 4688 zb->zb_gangs += BP_COUNT_GANG(bp); 4689 4690 switch (BP_GET_NDVAS(bp)) { 4691 case 2: 4692 if (DVA_GET_VDEV(&bp->blk_dva[0]) == 4693 DVA_GET_VDEV(&bp->blk_dva[1])) 4694 zb->zb_ditto_2_of_2_samevdev++; 4695 break; 4696 case 3: 4697 equal = (DVA_GET_VDEV(&bp->blk_dva[0]) == 4698 DVA_GET_VDEV(&bp->blk_dva[1])) + 4699 (DVA_GET_VDEV(&bp->blk_dva[0]) == 4700 DVA_GET_VDEV(&bp->blk_dva[2])) + 4701 (DVA_GET_VDEV(&bp->blk_dva[1]) == 4702 DVA_GET_VDEV(&bp->blk_dva[2])); 4703 if (equal == 1) 4704 zb->zb_ditto_2_of_3_samevdev++; 4705 else if (equal == 3) 4706 zb->zb_ditto_3_of_3_samevdev++; 4707 break; 4708 } 4709 } 4710 } 4711 4712 static void 4713 scan_io_queue_insert_impl(dsl_scan_io_queue_t *queue, scan_io_t *sio) 4714 { 4715 avl_index_t idx; 4716 dsl_scan_t *scn = queue->q_scn; 4717 4718 ASSERT(MUTEX_HELD(&queue->q_vd->vdev_scan_io_queue_lock)); 4719 4720 if (unlikely(avl_is_empty(&queue->q_sios_by_addr))) 4721 atomic_add_64(&scn->scn_queues_pending, 1); 4722 if (avl_find(&queue->q_sios_by_addr, sio, &idx) != NULL) { 4723 /* block is already scheduled for reading */ 4724 sio_free(sio); 4725 return; 4726 } 4727 avl_insert(&queue->q_sios_by_addr, sio, idx); 4728 queue->q_sio_memused += SIO_GET_MUSED(sio); 4729 zfs_range_tree_add(queue->q_exts_by_addr, SIO_GET_OFFSET(sio), 4730 SIO_GET_ASIZE(sio)); 4731 } 4732 4733 /* 4734 * Given all the info we got from our metadata scanning process, we 4735 * construct a scan_io_t and insert it into the scan sorting queue. The 4736 * I/O must already be suitable for us to process. This is controlled 4737 * by dsl_scan_enqueue(). 4738 */ 4739 static void 4740 scan_io_queue_insert(dsl_scan_io_queue_t *queue, const blkptr_t *bp, int dva_i, 4741 int zio_flags, const zbookmark_phys_t *zb) 4742 { 4743 scan_io_t *sio = sio_alloc(BP_GET_NDVAS(bp)); 4744 4745 ASSERT0(BP_IS_GANG(bp)); 4746 ASSERT(MUTEX_HELD(&queue->q_vd->vdev_scan_io_queue_lock)); 4747 4748 bp2sio(bp, sio, dva_i); 4749 sio->sio_flags = zio_flags; 4750 sio->sio_zb = *zb; 4751 4752 queue->q_last_ext_addr = -1; 4753 scan_io_queue_insert_impl(queue, sio); 4754 } 4755 4756 /* 4757 * Given a set of I/O parameters as discovered by the metadata traversal 4758 * process, attempts to place the I/O into the sorted queues (if allowed), 4759 * or immediately executes the I/O. 4760 */ 4761 static void 4762 dsl_scan_enqueue(dsl_pool_t *dp, const blkptr_t *bp, int zio_flags, 4763 const zbookmark_phys_t *zb) 4764 { 4765 spa_t *spa = dp->dp_spa; 4766 4767 ASSERT(!BP_IS_EMBEDDED(bp)); 4768 4769 /* 4770 * Gang blocks are hard to issue sequentially, so we just issue them 4771 * here immediately instead of queuing them. 4772 */ 4773 if (!dp->dp_scan->scn_is_sorted || BP_IS_GANG(bp)) { 4774 scan_exec_io(dp, bp, zio_flags, zb, NULL); 4775 return; 4776 } 4777 4778 for (int i = 0; i < BP_GET_NDVAS(bp); i++) { 4779 dva_t dva; 4780 vdev_t *vdev; 4781 4782 dva = bp->blk_dva[i]; 4783 vdev = vdev_lookup_top(spa, DVA_GET_VDEV(&dva)); 4784 ASSERT(vdev != NULL); 4785 4786 mutex_enter(&vdev->vdev_scan_io_queue_lock); 4787 if (vdev->vdev_scan_io_queue == NULL) 4788 vdev->vdev_scan_io_queue = scan_io_queue_create(vdev); 4789 ASSERT(dp->dp_scan != NULL); 4790 scan_io_queue_insert(vdev->vdev_scan_io_queue, bp, 4791 i, zio_flags, zb); 4792 mutex_exit(&vdev->vdev_scan_io_queue_lock); 4793 } 4794 } 4795 4796 static int 4797 dsl_scan_scrub_cb(dsl_pool_t *dp, 4798 const blkptr_t *bp, const zbookmark_phys_t *zb) 4799 { 4800 dsl_scan_t *scn = dp->dp_scan; 4801 spa_t *spa = dp->dp_spa; 4802 uint64_t phys_birth = BP_GET_BIRTH(bp); 4803 size_t psize = BP_GET_PSIZE(bp); 4804 boolean_t needs_io = B_FALSE; 4805 int zio_flags = ZIO_FLAG_SCAN_THREAD | ZIO_FLAG_RAW | ZIO_FLAG_CANFAIL; 4806 4807 count_block(dp->dp_blkstats, bp); 4808 if (phys_birth <= scn->scn_phys.scn_min_txg || 4809 phys_birth >= scn->scn_phys.scn_max_txg) { 4810 count_block_skipped(scn, bp, B_TRUE); 4811 return (0); 4812 } 4813 4814 /* Embedded BP's have phys_birth==0, so we reject them above. */ 4815 ASSERT(!BP_IS_EMBEDDED(bp)); 4816 4817 ASSERT(DSL_SCAN_IS_SCRUB_RESILVER(scn)); 4818 if (scn->scn_phys.scn_func == POOL_SCAN_SCRUB) { 4819 zio_flags |= ZIO_FLAG_SCRUB; 4820 needs_io = B_TRUE; 4821 } else { 4822 ASSERT3U(scn->scn_phys.scn_func, ==, POOL_SCAN_RESILVER); 4823 zio_flags |= ZIO_FLAG_RESILVER; 4824 needs_io = B_FALSE; 4825 } 4826 4827 /* If it's an intent log block, failure is expected. */ 4828 if (zb->zb_level == ZB_ZIL_LEVEL) 4829 zio_flags |= ZIO_FLAG_SPECULATIVE; 4830 4831 for (int d = 0; d < BP_GET_NDVAS(bp); d++) { 4832 const dva_t *dva = &bp->blk_dva[d]; 4833 4834 /* 4835 * Keep track of how much data we've examined so that 4836 * zpool(8) status can make useful progress reports. 4837 */ 4838 uint64_t asize = DVA_GET_ASIZE(dva); 4839 scn->scn_phys.scn_examined += asize; 4840 spa->spa_scan_pass_exam += asize; 4841 4842 /* if it's a resilver, this may not be in the target range */ 4843 if (!needs_io) 4844 needs_io = dsl_scan_need_resilver(spa, dva, psize, 4845 phys_birth); 4846 } 4847 4848 if (needs_io && !zfs_no_scrub_io) { 4849 dsl_scan_enqueue(dp, bp, zio_flags, zb); 4850 } else { 4851 count_block_skipped(scn, bp, B_TRUE); 4852 } 4853 4854 /* do not relocate this block */ 4855 return (0); 4856 } 4857 4858 static void 4859 dsl_scan_scrub_done(zio_t *zio) 4860 { 4861 spa_t *spa = zio->io_spa; 4862 blkptr_t *bp = zio->io_bp; 4863 dsl_scan_io_queue_t *queue = zio->io_private; 4864 4865 abd_free(zio->io_abd); 4866 4867 if (queue == NULL) { 4868 mutex_enter(&spa->spa_scrub_lock); 4869 ASSERT3U(spa->spa_scrub_inflight, >=, BP_GET_PSIZE(bp)); 4870 spa->spa_scrub_inflight -= BP_GET_PSIZE(bp); 4871 cv_broadcast(&spa->spa_scrub_io_cv); 4872 mutex_exit(&spa->spa_scrub_lock); 4873 } else { 4874 mutex_enter(&queue->q_vd->vdev_scan_io_queue_lock); 4875 ASSERT3U(queue->q_inflight_bytes, >=, BP_GET_PSIZE(bp)); 4876 queue->q_inflight_bytes -= BP_GET_PSIZE(bp); 4877 cv_broadcast(&queue->q_zio_cv); 4878 mutex_exit(&queue->q_vd->vdev_scan_io_queue_lock); 4879 } 4880 4881 if (zio->io_error && (zio->io_error != ECKSUM || 4882 !(zio->io_flags & ZIO_FLAG_SPECULATIVE))) { 4883 if (dsl_errorscrubbing(spa->spa_dsl_pool) && 4884 !dsl_errorscrub_is_paused(spa->spa_dsl_pool->dp_scan)) { 4885 atomic_inc_64(&spa->spa_dsl_pool->dp_scan 4886 ->errorscrub_phys.dep_errors); 4887 } else { 4888 atomic_inc_64(&spa->spa_dsl_pool->dp_scan->scn_phys 4889 .scn_errors); 4890 } 4891 } 4892 } 4893 4894 /* 4895 * Given a scanning zio's information, executes the zio. The zio need 4896 * not necessarily be only sortable, this function simply executes the 4897 * zio, no matter what it is. The optional queue argument allows the 4898 * caller to specify that they want per top level vdev IO rate limiting 4899 * instead of the legacy global limiting. 4900 */ 4901 static void 4902 scan_exec_io(dsl_pool_t *dp, const blkptr_t *bp, int zio_flags, 4903 const zbookmark_phys_t *zb, dsl_scan_io_queue_t *queue) 4904 { 4905 spa_t *spa = dp->dp_spa; 4906 dsl_scan_t *scn = dp->dp_scan; 4907 size_t size = BP_GET_PSIZE(bp); 4908 abd_t *data = abd_alloc_for_io(size, B_FALSE); 4909 zio_t *pio; 4910 4911 if (queue == NULL) { 4912 ASSERT3U(scn->scn_maxinflight_bytes, >, 0); 4913 mutex_enter(&spa->spa_scrub_lock); 4914 while (spa->spa_scrub_inflight >= scn->scn_maxinflight_bytes) 4915 cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 4916 spa->spa_scrub_inflight += BP_GET_PSIZE(bp); 4917 mutex_exit(&spa->spa_scrub_lock); 4918 pio = scn->scn_zio_root; 4919 } else { 4920 kmutex_t *q_lock = &queue->q_vd->vdev_scan_io_queue_lock; 4921 4922 ASSERT3U(queue->q_maxinflight_bytes, >, 0); 4923 mutex_enter(q_lock); 4924 while (queue->q_inflight_bytes >= queue->q_maxinflight_bytes) 4925 cv_wait(&queue->q_zio_cv, q_lock); 4926 queue->q_inflight_bytes += BP_GET_PSIZE(bp); 4927 pio = queue->q_zio; 4928 mutex_exit(q_lock); 4929 } 4930 4931 ASSERT(pio != NULL); 4932 count_block_issued(spa, bp, queue == NULL); 4933 zio_nowait(zio_read(pio, spa, bp, data, size, dsl_scan_scrub_done, 4934 queue, ZIO_PRIORITY_SCRUB, zio_flags, zb)); 4935 } 4936 4937 /* 4938 * This is the primary extent sorting algorithm. We balance two parameters: 4939 * 1) how many bytes of I/O are in an extent 4940 * 2) how well the extent is filled with I/O (as a fraction of its total size) 4941 * Since we allow extents to have gaps between their constituent I/Os, it's 4942 * possible to have a fairly large extent that contains the same amount of 4943 * I/O bytes than a much smaller extent, which just packs the I/O more tightly. 4944 * The algorithm sorts based on a score calculated from the extent's size, 4945 * the relative fill volume (in %) and a "fill weight" parameter that controls 4946 * the split between whether we prefer larger extents or more well populated 4947 * extents: 4948 * 4949 * SCORE = FILL_IN_BYTES + (FILL_IN_PERCENT * FILL_IN_BYTES * FILL_WEIGHT) 4950 * 4951 * Example: 4952 * 1) assume extsz = 64 MiB 4953 * 2) assume fill = 32 MiB (extent is half full) 4954 * 3) assume fill_weight = 3 4955 * 4) SCORE = 32M + (((32M * 100) / 64M) * 3 * 32M) / 100 4956 * SCORE = 32M + (50 * 3 * 32M) / 100 4957 * SCORE = 32M + (4800M / 100) 4958 * SCORE = 32M + 48M 4959 * ^ ^ 4960 * | +--- final total relative fill-based score 4961 * +--------- final total fill-based score 4962 * SCORE = 80M 4963 * 4964 * As can be seen, at fill_ratio=3, the algorithm is slightly biased towards 4965 * extents that are more completely filled (in a 3:2 ratio) vs just larger. 4966 * Note that as an optimization, we replace multiplication and division by 4967 * 100 with bitshifting by 7 (which effectively multiplies and divides by 128). 4968 * 4969 * Since we do not care if one extent is only few percent better than another, 4970 * compress the score into 6 bits via binary logarithm AKA highbit64() and 4971 * put into otherwise unused due to ashift high bits of offset. This allows 4972 * to reduce q_exts_by_size B-tree elements to only 64 bits and compare them 4973 * with single operation. Plus it makes scrubs more sequential and reduces 4974 * chances that minor extent change move it within the B-tree. 4975 */ 4976 __attribute__((always_inline)) inline 4977 static int 4978 ext_size_compare(const void *x, const void *y) 4979 { 4980 const uint64_t *a = x, *b = y; 4981 4982 return (TREE_CMP(*a, *b)); 4983 } 4984 4985 ZFS_BTREE_FIND_IN_BUF_FUNC(ext_size_find_in_buf, uint64_t, 4986 ext_size_compare) 4987 4988 static void 4989 ext_size_create(zfs_range_tree_t *rt, void *arg) 4990 { 4991 (void) rt; 4992 zfs_btree_t *size_tree = arg; 4993 4994 zfs_btree_create(size_tree, ext_size_compare, ext_size_find_in_buf, 4995 sizeof (uint64_t)); 4996 } 4997 4998 static void 4999 ext_size_destroy(zfs_range_tree_t *rt, void *arg) 5000 { 5001 (void) rt; 5002 zfs_btree_t *size_tree = arg; 5003 ASSERT0(zfs_btree_numnodes(size_tree)); 5004 5005 zfs_btree_destroy(size_tree); 5006 } 5007 5008 static uint64_t 5009 ext_size_value(zfs_range_tree_t *rt, zfs_range_seg_gap_t *rsg) 5010 { 5011 (void) rt; 5012 uint64_t size = rsg->rs_end - rsg->rs_start; 5013 uint64_t score = rsg->rs_fill + ((((rsg->rs_fill << 7) / size) * 5014 fill_weight * rsg->rs_fill) >> 7); 5015 ASSERT3U(rt->rt_shift, >=, 8); 5016 return (((uint64_t)(64 - highbit64(score)) << 56) | rsg->rs_start); 5017 } 5018 5019 static void 5020 ext_size_add(zfs_range_tree_t *rt, zfs_range_seg_t *rs, void *arg) 5021 { 5022 zfs_btree_t *size_tree = arg; 5023 ASSERT3U(rt->rt_type, ==, ZFS_RANGE_SEG_GAP); 5024 uint64_t v = ext_size_value(rt, (zfs_range_seg_gap_t *)rs); 5025 zfs_btree_add(size_tree, &v); 5026 } 5027 5028 static void 5029 ext_size_remove(zfs_range_tree_t *rt, zfs_range_seg_t *rs, void *arg) 5030 { 5031 zfs_btree_t *size_tree = arg; 5032 ASSERT3U(rt->rt_type, ==, ZFS_RANGE_SEG_GAP); 5033 uint64_t v = ext_size_value(rt, (zfs_range_seg_gap_t *)rs); 5034 zfs_btree_remove(size_tree, &v); 5035 } 5036 5037 static void 5038 ext_size_vacate(zfs_range_tree_t *rt, void *arg) 5039 { 5040 zfs_btree_t *size_tree = arg; 5041 zfs_btree_clear(size_tree); 5042 zfs_btree_destroy(size_tree); 5043 5044 ext_size_create(rt, arg); 5045 } 5046 5047 static const zfs_range_tree_ops_t ext_size_ops = { 5048 .rtop_create = ext_size_create, 5049 .rtop_destroy = ext_size_destroy, 5050 .rtop_add = ext_size_add, 5051 .rtop_remove = ext_size_remove, 5052 .rtop_vacate = ext_size_vacate 5053 }; 5054 5055 /* 5056 * Comparator for the q_sios_by_addr tree. Sorting is simply performed 5057 * based on LBA-order (from lowest to highest). 5058 */ 5059 static int 5060 sio_addr_compare(const void *x, const void *y) 5061 { 5062 const scan_io_t *a = x, *b = y; 5063 5064 return (TREE_CMP(SIO_GET_OFFSET(a), SIO_GET_OFFSET(b))); 5065 } 5066 5067 /* IO queues are created on demand when they are needed. */ 5068 static dsl_scan_io_queue_t * 5069 scan_io_queue_create(vdev_t *vd) 5070 { 5071 dsl_scan_t *scn = vd->vdev_spa->spa_dsl_pool->dp_scan; 5072 dsl_scan_io_queue_t *q = kmem_zalloc(sizeof (*q), KM_SLEEP); 5073 5074 q->q_scn = scn; 5075 q->q_vd = vd; 5076 q->q_sio_memused = 0; 5077 q->q_last_ext_addr = -1; 5078 cv_init(&q->q_zio_cv, NULL, CV_DEFAULT, NULL); 5079 q->q_exts_by_addr = zfs_range_tree_create_gap(&ext_size_ops, 5080 ZFS_RANGE_SEG_GAP, &q->q_exts_by_size, 0, vd->vdev_ashift, 5081 zfs_scan_max_ext_gap); 5082 avl_create(&q->q_sios_by_addr, sio_addr_compare, 5083 sizeof (scan_io_t), offsetof(scan_io_t, sio_nodes.sio_addr_node)); 5084 5085 return (q); 5086 } 5087 5088 /* 5089 * Destroys a scan queue and all segments and scan_io_t's contained in it. 5090 * No further execution of I/O occurs, anything pending in the queue is 5091 * simply freed without being executed. 5092 */ 5093 void 5094 dsl_scan_io_queue_destroy(dsl_scan_io_queue_t *queue) 5095 { 5096 dsl_scan_t *scn = queue->q_scn; 5097 scan_io_t *sio; 5098 void *cookie = NULL; 5099 5100 ASSERT(MUTEX_HELD(&queue->q_vd->vdev_scan_io_queue_lock)); 5101 5102 if (!avl_is_empty(&queue->q_sios_by_addr)) 5103 atomic_add_64(&scn->scn_queues_pending, -1); 5104 while ((sio = avl_destroy_nodes(&queue->q_sios_by_addr, &cookie)) != 5105 NULL) { 5106 ASSERT(zfs_range_tree_contains(queue->q_exts_by_addr, 5107 SIO_GET_OFFSET(sio), SIO_GET_ASIZE(sio))); 5108 queue->q_sio_memused -= SIO_GET_MUSED(sio); 5109 sio_free(sio); 5110 } 5111 5112 ASSERT0(queue->q_sio_memused); 5113 zfs_range_tree_vacate(queue->q_exts_by_addr, NULL, queue); 5114 zfs_range_tree_destroy(queue->q_exts_by_addr); 5115 avl_destroy(&queue->q_sios_by_addr); 5116 cv_destroy(&queue->q_zio_cv); 5117 5118 kmem_free(queue, sizeof (*queue)); 5119 } 5120 5121 /* 5122 * Properly transfers a dsl_scan_queue_t from `svd' to `tvd'. This is 5123 * called on behalf of vdev_top_transfer when creating or destroying 5124 * a mirror vdev due to zpool attach/detach. 5125 */ 5126 void 5127 dsl_scan_io_queue_vdev_xfer(vdev_t *svd, vdev_t *tvd) 5128 { 5129 mutex_enter(&svd->vdev_scan_io_queue_lock); 5130 mutex_enter(&tvd->vdev_scan_io_queue_lock); 5131 5132 VERIFY3P(tvd->vdev_scan_io_queue, ==, NULL); 5133 tvd->vdev_scan_io_queue = svd->vdev_scan_io_queue; 5134 svd->vdev_scan_io_queue = NULL; 5135 if (tvd->vdev_scan_io_queue != NULL) 5136 tvd->vdev_scan_io_queue->q_vd = tvd; 5137 5138 mutex_exit(&tvd->vdev_scan_io_queue_lock); 5139 mutex_exit(&svd->vdev_scan_io_queue_lock); 5140 } 5141 5142 static void 5143 scan_io_queues_destroy(dsl_scan_t *scn) 5144 { 5145 vdev_t *rvd = scn->scn_dp->dp_spa->spa_root_vdev; 5146 5147 for (uint64_t i = 0; i < rvd->vdev_children; i++) { 5148 vdev_t *tvd = rvd->vdev_child[i]; 5149 5150 mutex_enter(&tvd->vdev_scan_io_queue_lock); 5151 if (tvd->vdev_scan_io_queue != NULL) 5152 dsl_scan_io_queue_destroy(tvd->vdev_scan_io_queue); 5153 tvd->vdev_scan_io_queue = NULL; 5154 mutex_exit(&tvd->vdev_scan_io_queue_lock); 5155 } 5156 } 5157 5158 static void 5159 dsl_scan_freed_dva(spa_t *spa, const blkptr_t *bp, int dva_i) 5160 { 5161 dsl_pool_t *dp = spa->spa_dsl_pool; 5162 dsl_scan_t *scn = dp->dp_scan; 5163 vdev_t *vdev; 5164 kmutex_t *q_lock; 5165 dsl_scan_io_queue_t *queue; 5166 scan_io_t *srch_sio, *sio; 5167 avl_index_t idx; 5168 uint64_t start, size; 5169 5170 vdev = vdev_lookup_top(spa, DVA_GET_VDEV(&bp->blk_dva[dva_i])); 5171 ASSERT(vdev != NULL); 5172 q_lock = &vdev->vdev_scan_io_queue_lock; 5173 queue = vdev->vdev_scan_io_queue; 5174 5175 mutex_enter(q_lock); 5176 if (queue == NULL) { 5177 mutex_exit(q_lock); 5178 return; 5179 } 5180 5181 srch_sio = sio_alloc(BP_GET_NDVAS(bp)); 5182 bp2sio(bp, srch_sio, dva_i); 5183 start = SIO_GET_OFFSET(srch_sio); 5184 size = SIO_GET_ASIZE(srch_sio); 5185 5186 /* 5187 * We can find the zio in two states: 5188 * 1) Cold, just sitting in the queue of zio's to be issued at 5189 * some point in the future. In this case, all we do is 5190 * remove the zio from the q_sios_by_addr tree, decrement 5191 * its data volume from the containing zfs_range_seg_t and 5192 * resort the q_exts_by_size tree to reflect that the 5193 * zfs_range_seg_t has lost some of its 'fill'. We don't shorten 5194 * the zfs_range_seg_t - this is usually rare enough not to be 5195 * worth the extra hassle of trying keep track of precise 5196 * extent boundaries. 5197 * 2) Hot, where the zio is currently in-flight in 5198 * dsl_scan_issue_ios. In this case, we can't simply 5199 * reach in and stop the in-flight zio's, so we instead 5200 * block the caller. Eventually, dsl_scan_issue_ios will 5201 * be done with issuing the zio's it gathered and will 5202 * signal us. 5203 */ 5204 sio = avl_find(&queue->q_sios_by_addr, srch_sio, &idx); 5205 sio_free(srch_sio); 5206 5207 if (sio != NULL) { 5208 blkptr_t tmpbp; 5209 5210 /* Got it while it was cold in the queue */ 5211 ASSERT3U(start, ==, SIO_GET_OFFSET(sio)); 5212 ASSERT3U(size, ==, SIO_GET_ASIZE(sio)); 5213 avl_remove(&queue->q_sios_by_addr, sio); 5214 if (avl_is_empty(&queue->q_sios_by_addr)) 5215 atomic_add_64(&scn->scn_queues_pending, -1); 5216 queue->q_sio_memused -= SIO_GET_MUSED(sio); 5217 5218 ASSERT(zfs_range_tree_contains(queue->q_exts_by_addr, start, 5219 size)); 5220 zfs_range_tree_remove_fill(queue->q_exts_by_addr, start, size); 5221 5222 /* count the block as though we skipped it */ 5223 sio2bp(sio, &tmpbp); 5224 count_block_skipped(scn, &tmpbp, B_FALSE); 5225 5226 sio_free(sio); 5227 } 5228 mutex_exit(q_lock); 5229 } 5230 5231 /* 5232 * Callback invoked when a zio_free() zio is executing. This needs to be 5233 * intercepted to prevent the zio from deallocating a particular portion 5234 * of disk space and it then getting reallocated and written to, while we 5235 * still have it queued up for processing. 5236 */ 5237 void 5238 dsl_scan_freed(spa_t *spa, const blkptr_t *bp) 5239 { 5240 dsl_pool_t *dp = spa->spa_dsl_pool; 5241 dsl_scan_t *scn = dp->dp_scan; 5242 5243 ASSERT(!BP_IS_EMBEDDED(bp)); 5244 ASSERT(scn != NULL); 5245 if (!dsl_scan_is_running(scn)) 5246 return; 5247 5248 for (int i = 0; i < BP_GET_NDVAS(bp); i++) 5249 dsl_scan_freed_dva(spa, bp, i); 5250 } 5251 5252 /* 5253 * Check if a vdev needs resilvering (non-empty DTL), if so, and resilver has 5254 * not started, start it. Otherwise, only restart if max txg in DTL range is 5255 * greater than the max txg in the current scan. If the DTL max is less than 5256 * the scan max, then the vdev has not missed any new data since the resilver 5257 * started, so a restart is not needed. 5258 */ 5259 void 5260 dsl_scan_assess_vdev(dsl_pool_t *dp, vdev_t *vd) 5261 { 5262 uint64_t min, max; 5263 5264 if (!vdev_resilver_needed(vd, &min, &max)) 5265 return; 5266 5267 if (!dsl_scan_resilvering(dp)) { 5268 spa_async_request(dp->dp_spa, SPA_ASYNC_RESILVER); 5269 return; 5270 } 5271 5272 if (max <= dp->dp_scan->scn_phys.scn_max_txg) 5273 return; 5274 5275 /* restart is needed, check if it can be deferred */ 5276 if (spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_RESILVER_DEFER)) 5277 vdev_defer_resilver(vd); 5278 else 5279 spa_async_request(dp->dp_spa, SPA_ASYNC_RESILVER); 5280 } 5281 5282 ZFS_MODULE_PARAM(zfs, zfs_, scan_vdev_limit, U64, ZMOD_RW, 5283 "Max bytes in flight per leaf vdev for scrubs and resilvers"); 5284 5285 ZFS_MODULE_PARAM(zfs, zfs_, scrub_min_time_ms, UINT, ZMOD_RW, 5286 "Min millisecs to scrub per txg"); 5287 5288 ZFS_MODULE_PARAM(zfs, zfs_, obsolete_min_time_ms, UINT, ZMOD_RW, 5289 "Min millisecs to obsolete per txg"); 5290 5291 ZFS_MODULE_PARAM(zfs, zfs_, free_min_time_ms, UINT, ZMOD_RW, 5292 "Min millisecs to free per txg"); 5293 5294 ZFS_MODULE_PARAM(zfs, zfs_, resilver_min_time_ms, UINT, ZMOD_RW, 5295 "Min millisecs to resilver per txg"); 5296 5297 ZFS_MODULE_PARAM(zfs, zfs_, scan_suspend_progress, INT, ZMOD_RW, 5298 "Set to prevent scans from progressing"); 5299 5300 ZFS_MODULE_PARAM(zfs, zfs_, no_scrub_io, INT, ZMOD_RW, 5301 "Set to disable scrub I/O"); 5302 5303 ZFS_MODULE_PARAM(zfs, zfs_, no_scrub_prefetch, INT, ZMOD_RW, 5304 "Set to disable scrub prefetching"); 5305 5306 ZFS_MODULE_PARAM(zfs, zfs_, async_block_max_blocks, U64, ZMOD_RW, 5307 "Max number of blocks freed in one txg"); 5308 5309 ZFS_MODULE_PARAM(zfs, zfs_, max_async_dedup_frees, U64, ZMOD_RW, 5310 "Max number of dedup blocks freed in one txg"); 5311 5312 ZFS_MODULE_PARAM(zfs, zfs_, free_bpobj_enabled, INT, ZMOD_RW, 5313 "Enable processing of the free_bpobj"); 5314 5315 ZFS_MODULE_PARAM(zfs, zfs_, scan_blkstats, INT, ZMOD_RW, 5316 "Enable block statistics calculation during scrub"); 5317 5318 ZFS_MODULE_PARAM(zfs, zfs_, scan_mem_lim_fact, UINT, ZMOD_RW, 5319 "Fraction of RAM for scan hard limit"); 5320 5321 ZFS_MODULE_PARAM(zfs, zfs_, scan_issue_strategy, UINT, ZMOD_RW, 5322 "IO issuing strategy during scrubbing. 0 = default, 1 = LBA, 2 = size"); 5323 5324 ZFS_MODULE_PARAM(zfs, zfs_, scan_legacy, INT, ZMOD_RW, 5325 "Scrub using legacy non-sequential method"); 5326 5327 ZFS_MODULE_PARAM(zfs, zfs_, scan_checkpoint_intval, UINT, ZMOD_RW, 5328 "Scan progress on-disk checkpointing interval"); 5329 5330 ZFS_MODULE_PARAM(zfs, zfs_, scan_max_ext_gap, U64, ZMOD_RW, 5331 "Max gap in bytes between sequential scrub / resilver I/Os"); 5332 5333 ZFS_MODULE_PARAM(zfs, zfs_, scan_mem_lim_soft_fact, UINT, ZMOD_RW, 5334 "Fraction of hard limit used as soft limit"); 5335 5336 ZFS_MODULE_PARAM(zfs, zfs_, scan_strict_mem_lim, INT, ZMOD_RW, 5337 "Tunable to attempt to reduce lock contention"); 5338 5339 ZFS_MODULE_PARAM(zfs, zfs_, scan_fill_weight, UINT, ZMOD_RW, 5340 "Tunable to adjust bias towards more filled segments during scans"); 5341 5342 ZFS_MODULE_PARAM(zfs, zfs_, scan_report_txgs, UINT, ZMOD_RW, 5343 "Tunable to report resilver performance over the last N txgs"); 5344 5345 ZFS_MODULE_PARAM(zfs, zfs_, resilver_disable_defer, INT, ZMOD_RW, 5346 "Process all resilvers immediately"); 5347 5348 ZFS_MODULE_PARAM(zfs, zfs_, resilver_defer_percent, UINT, ZMOD_RW, 5349 "Issued IO percent complete after which resilvers are deferred"); 5350 5351 ZFS_MODULE_PARAM(zfs, zfs_, scrub_error_blocks_per_txg, UINT, ZMOD_RW, 5352 "Error blocks to be scrubbed in one txg"); 5353