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