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