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