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