xref: /freebsd-src/sys/contrib/openzfs/module/zfs/zio.c (revision dd21556857e8d40f66bf5ad54754d9d52669ebf7)
1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * CDDL HEADER START
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy  *
8eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
10eda14cbcSMatt Macy  * See the License for the specific language governing permissions
11eda14cbcSMatt Macy  * and limitations under the License.
12eda14cbcSMatt Macy  *
13eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy  *
19eda14cbcSMatt Macy  * CDDL HEADER END
20eda14cbcSMatt Macy  */
21eda14cbcSMatt Macy /*
22eda14cbcSMatt Macy  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23bb2d13b6SMartin Matuska  * Copyright (c) 2011, 2022 by Delphix. All rights reserved.
24eda14cbcSMatt Macy  * Copyright (c) 2011 Nexenta Systems, Inc. All rights reserved.
25eda14cbcSMatt Macy  * Copyright (c) 2017, Intel Corporation.
261719886fSMartin Matuska  * Copyright (c) 2019, 2023, 2024, Klara Inc.
27eda14cbcSMatt Macy  * Copyright (c) 2019, Allan Jude
2816038816SMartin Matuska  * Copyright (c) 2021, Datto, Inc.
297a7741afSMartin Matuska  * Copyright (c) 2021, 2024 by George Melikov. All rights reserved.
30eda14cbcSMatt Macy  */
31eda14cbcSMatt Macy 
32eda14cbcSMatt Macy #include <sys/sysmacros.h>
33eda14cbcSMatt Macy #include <sys/zfs_context.h>
34eda14cbcSMatt Macy #include <sys/fm/fs/zfs.h>
35eda14cbcSMatt Macy #include <sys/spa.h>
36eda14cbcSMatt Macy #include <sys/txg.h>
37eda14cbcSMatt Macy #include <sys/spa_impl.h>
38eda14cbcSMatt Macy #include <sys/vdev_impl.h>
39eda14cbcSMatt Macy #include <sys/vdev_trim.h>
40eda14cbcSMatt Macy #include <sys/zio_impl.h>
41eda14cbcSMatt Macy #include <sys/zio_compress.h>
42eda14cbcSMatt Macy #include <sys/zio_checksum.h>
43eda14cbcSMatt Macy #include <sys/dmu_objset.h>
44eda14cbcSMatt Macy #include <sys/arc.h>
452a58b312SMartin Matuska #include <sys/brt.h>
46eda14cbcSMatt Macy #include <sys/ddt.h>
47eda14cbcSMatt Macy #include <sys/blkptr.h>
48eda14cbcSMatt Macy #include <sys/zfeature.h>
49eda14cbcSMatt Macy #include <sys/dsl_scan.h>
50eda14cbcSMatt Macy #include <sys/metaslab_impl.h>
51eda14cbcSMatt Macy #include <sys/time.h>
52eda14cbcSMatt Macy #include <sys/trace_zfs.h>
53eda14cbcSMatt Macy #include <sys/abd.h>
54eda14cbcSMatt Macy #include <sys/dsl_crypt.h>
55eda14cbcSMatt Macy #include <cityhash.h>
56eda14cbcSMatt Macy 
57eda14cbcSMatt Macy /*
58eda14cbcSMatt Macy  * ==========================================================================
59eda14cbcSMatt Macy  * I/O type descriptions
60eda14cbcSMatt Macy  * ==========================================================================
61eda14cbcSMatt Macy  */
62e92ffd9bSMartin Matuska const char *const zio_type_name[ZIO_TYPES] = {
63eda14cbcSMatt Macy 	/*
64eda14cbcSMatt Macy 	 * Note: Linux kernel thread name length is limited
65eda14cbcSMatt Macy 	 * so these names will differ from upstream open zfs.
66eda14cbcSMatt Macy 	 */
671719886fSMartin Matuska 	"z_null", "z_rd", "z_wr", "z_fr", "z_cl", "z_flush", "z_trim"
68eda14cbcSMatt Macy };
69eda14cbcSMatt Macy 
70eda14cbcSMatt Macy int zio_dva_throttle_enabled = B_TRUE;
71e92ffd9bSMartin Matuska static int zio_deadman_log_all = B_FALSE;
72eda14cbcSMatt Macy 
73eda14cbcSMatt Macy /*
74eda14cbcSMatt Macy  * ==========================================================================
75eda14cbcSMatt Macy  * I/O kmem caches
76eda14cbcSMatt Macy  * ==========================================================================
77eda14cbcSMatt Macy  */
78e92ffd9bSMartin Matuska static kmem_cache_t *zio_cache;
79e92ffd9bSMartin Matuska static kmem_cache_t *zio_link_cache;
80eda14cbcSMatt Macy kmem_cache_t *zio_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
81eda14cbcSMatt Macy kmem_cache_t *zio_data_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
82eda14cbcSMatt Macy #if defined(ZFS_DEBUG) && !defined(_KERNEL)
83e92ffd9bSMartin Matuska static uint64_t zio_buf_cache_allocs[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
84e92ffd9bSMartin Matuska static uint64_t zio_buf_cache_frees[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
85eda14cbcSMatt Macy #endif
86eda14cbcSMatt Macy 
87eda14cbcSMatt Macy /* Mark IOs as "slow" if they take longer than 30 seconds */
88be181ee2SMartin Matuska static uint_t zio_slow_io_ms = (30 * MILLISEC);
89eda14cbcSMatt Macy 
90eda14cbcSMatt Macy #define	BP_SPANB(indblkshift, level) \
91eda14cbcSMatt Macy 	(((uint64_t)1) << ((level) * ((indblkshift) - SPA_BLKPTRSHIFT)))
92eda14cbcSMatt Macy #define	COMPARE_META_LEVEL	0x80000000ul
93eda14cbcSMatt Macy /*
94eda14cbcSMatt Macy  * The following actions directly effect the spa's sync-to-convergence logic.
95eda14cbcSMatt Macy  * The values below define the sync pass when we start performing the action.
96eda14cbcSMatt Macy  * Care should be taken when changing these values as they directly impact
97eda14cbcSMatt Macy  * spa_sync() performance. Tuning these values may introduce subtle performance
98eda14cbcSMatt Macy  * pathologies and should only be done in the context of performance analysis.
99eda14cbcSMatt Macy  * These tunables will eventually be removed and replaced with #defines once
100eda14cbcSMatt Macy  * enough analysis has been done to determine optimal values.
101eda14cbcSMatt Macy  *
102eda14cbcSMatt Macy  * The 'zfs_sync_pass_deferred_free' pass must be greater than 1 to ensure that
103eda14cbcSMatt Macy  * regular blocks are not deferred.
104eda14cbcSMatt Macy  *
105eda14cbcSMatt Macy  * Starting in sync pass 8 (zfs_sync_pass_dont_compress), we disable
106eda14cbcSMatt Macy  * compression (including of metadata).  In practice, we don't have this
107eda14cbcSMatt Macy  * many sync passes, so this has no effect.
108eda14cbcSMatt Macy  *
109eda14cbcSMatt Macy  * The original intent was that disabling compression would help the sync
110eda14cbcSMatt Macy  * passes to converge. However, in practice disabling compression increases
111eda14cbcSMatt Macy  * the average number of sync passes, because when we turn compression off, a
112eda14cbcSMatt Macy  * lot of block's size will change and thus we have to re-allocate (not
113eda14cbcSMatt Macy  * overwrite) them. It also increases the number of 128KB allocations (e.g.
114eda14cbcSMatt Macy  * for indirect blocks and spacemaps) because these will not be compressed.
115eda14cbcSMatt Macy  * The 128K allocations are especially detrimental to performance on highly
116eda14cbcSMatt Macy  * fragmented systems, which may have very few free segments of this size,
117eda14cbcSMatt Macy  * and may need to load new metaslabs to satisfy 128K allocations.
118eda14cbcSMatt Macy  */
119be181ee2SMartin Matuska 
120be181ee2SMartin Matuska /* defer frees starting in this pass */
121be181ee2SMartin Matuska uint_t zfs_sync_pass_deferred_free = 2;
122be181ee2SMartin Matuska 
123be181ee2SMartin Matuska /* don't compress starting in this pass */
124be181ee2SMartin Matuska static uint_t zfs_sync_pass_dont_compress = 8;
125be181ee2SMartin Matuska 
126be181ee2SMartin Matuska /* rewrite new bps starting in this pass */
127be181ee2SMartin Matuska static uint_t zfs_sync_pass_rewrite = 2;
128eda14cbcSMatt Macy 
129eda14cbcSMatt Macy /*
130eda14cbcSMatt Macy  * An allocating zio is one that either currently has the DVA allocate
131eda14cbcSMatt Macy  * stage set or will have it later in its lifetime.
132eda14cbcSMatt Macy  */
133eda14cbcSMatt Macy #define	IO_IS_ALLOCATING(zio) ((zio)->io_orig_pipeline & ZIO_STAGE_DVA_ALLOCATE)
134eda14cbcSMatt Macy 
135eda14cbcSMatt Macy /*
136eda14cbcSMatt Macy  * Enable smaller cores by excluding metadata
137eda14cbcSMatt Macy  * allocations as well.
138eda14cbcSMatt Macy  */
139eda14cbcSMatt Macy int zio_exclude_metadata = 0;
140e92ffd9bSMartin Matuska static int zio_requeue_io_start_cut_in_line = 1;
141eda14cbcSMatt Macy 
142eda14cbcSMatt Macy #ifdef ZFS_DEBUG
143e92ffd9bSMartin Matuska static const int zio_buf_debug_limit = 16384;
144eda14cbcSMatt Macy #else
145e92ffd9bSMartin Matuska static const int zio_buf_debug_limit = 0;
146eda14cbcSMatt Macy #endif
147eda14cbcSMatt Macy 
148eda14cbcSMatt Macy static inline void __zio_execute(zio_t *zio);
149eda14cbcSMatt Macy 
150eda14cbcSMatt Macy static void zio_taskq_dispatch(zio_t *, zio_taskq_type_t, boolean_t);
151eda14cbcSMatt Macy 
152eda14cbcSMatt Macy void
153eda14cbcSMatt Macy zio_init(void)
154eda14cbcSMatt Macy {
155eda14cbcSMatt Macy 	size_t c;
156eda14cbcSMatt Macy 
157eda14cbcSMatt Macy 	zio_cache = kmem_cache_create("zio_cache",
158eda14cbcSMatt Macy 	    sizeof (zio_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
159eda14cbcSMatt Macy 	zio_link_cache = kmem_cache_create("zio_link_cache",
160eda14cbcSMatt Macy 	    sizeof (zio_link_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
161eda14cbcSMatt Macy 
162eda14cbcSMatt Macy 	for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) {
163eda14cbcSMatt Macy 		size_t size = (c + 1) << SPA_MINBLOCKSHIFT;
164f8b1db88SMartin Matuska 		size_t align, cflags, data_cflags;
165f8b1db88SMartin Matuska 		char name[32];
166f8b1db88SMartin Matuska 
167f8b1db88SMartin Matuska 		/*
168f8b1db88SMartin Matuska 		 * Create cache for each half-power of 2 size, starting from
169f8b1db88SMartin Matuska 		 * SPA_MINBLOCKSIZE.  It should give us memory space efficiency
170f8b1db88SMartin Matuska 		 * of ~7/8, sufficient for transient allocations mostly using
171f8b1db88SMartin Matuska 		 * these caches.
172f8b1db88SMartin Matuska 		 */
173eda14cbcSMatt Macy 		size_t p2 = size;
174eda14cbcSMatt Macy 		while (!ISP2(p2))
175eda14cbcSMatt Macy 			p2 &= p2 - 1;
176f8b1db88SMartin Matuska 		if (!IS_P2ALIGNED(size, p2 / 2))
177f8b1db88SMartin Matuska 			continue;
178eda14cbcSMatt Macy 
179eda14cbcSMatt Macy #ifndef _KERNEL
180eda14cbcSMatt Macy 		/*
181eda14cbcSMatt Macy 		 * If we are using watchpoints, put each buffer on its own page,
182eda14cbcSMatt Macy 		 * to eliminate the performance overhead of trapping to the
183eda14cbcSMatt Macy 		 * kernel when modifying a non-watched buffer that shares the
184eda14cbcSMatt Macy 		 * page with a watched buffer.
185eda14cbcSMatt Macy 		 */
186eda14cbcSMatt Macy 		if (arc_watch && !IS_P2ALIGNED(size, PAGESIZE))
187eda14cbcSMatt Macy 			continue;
188eda14cbcSMatt Macy #endif
189eda14cbcSMatt Macy 
190f8b1db88SMartin Matuska 		if (IS_P2ALIGNED(size, PAGESIZE))
191f8b1db88SMartin Matuska 			align = PAGESIZE;
192f8b1db88SMartin Matuska 		else
193f8b1db88SMartin Matuska 			align = 1 << (highbit64(size ^ (size - 1)) - 1);
194f8b1db88SMartin Matuska 
195f8b1db88SMartin Matuska 		cflags = (zio_exclude_metadata || size > zio_buf_debug_limit) ?
196f8b1db88SMartin Matuska 		    KMC_NODEBUG : 0;
197f8b1db88SMartin Matuska 		data_cflags = KMC_NODEBUG;
198ce4dcb97SMartin Matuska 		if (abd_size_alloc_linear(size)) {
199ce4dcb97SMartin Matuska 			cflags |= KMC_RECLAIMABLE;
200ce4dcb97SMartin Matuska 			data_cflags |= KMC_RECLAIMABLE;
201ce4dcb97SMartin Matuska 		}
2024426311aSMateusz Guzik 		if (cflags == data_cflags) {
2034426311aSMateusz Guzik 			/*
2044426311aSMateusz Guzik 			 * Resulting kmem caches would be identical.
2054426311aSMateusz Guzik 			 * Save memory by creating only one.
2064426311aSMateusz Guzik 			 */
2074426311aSMateusz Guzik 			(void) snprintf(name, sizeof (name),
2084426311aSMateusz Guzik 			    "zio_buf_comb_%lu", (ulong_t)size);
209f8b1db88SMartin Matuska 			zio_buf_cache[c] = kmem_cache_create(name, size, align,
210f8b1db88SMartin Matuska 			    NULL, NULL, NULL, NULL, NULL, cflags);
2114426311aSMateusz Guzik 			zio_data_buf_cache[c] = zio_buf_cache[c];
2124426311aSMateusz Guzik 			continue;
2134426311aSMateusz Guzik 		}
214eda14cbcSMatt Macy 		(void) snprintf(name, sizeof (name), "zio_buf_%lu",
215eda14cbcSMatt Macy 		    (ulong_t)size);
216f8b1db88SMartin Matuska 		zio_buf_cache[c] = kmem_cache_create(name, size, align,
217f8b1db88SMartin Matuska 		    NULL, NULL, NULL, NULL, NULL, cflags);
218eda14cbcSMatt Macy 
219eda14cbcSMatt Macy 		(void) snprintf(name, sizeof (name), "zio_data_buf_%lu",
220eda14cbcSMatt Macy 		    (ulong_t)size);
221f8b1db88SMartin Matuska 		zio_data_buf_cache[c] = kmem_cache_create(name, size, align,
222f8b1db88SMartin Matuska 		    NULL, NULL, NULL, NULL, NULL, data_cflags);
223eda14cbcSMatt Macy 	}
224eda14cbcSMatt Macy 
225eda14cbcSMatt Macy 	while (--c != 0) {
226eda14cbcSMatt Macy 		ASSERT(zio_buf_cache[c] != NULL);
227eda14cbcSMatt Macy 		if (zio_buf_cache[c - 1] == NULL)
228eda14cbcSMatt Macy 			zio_buf_cache[c - 1] = zio_buf_cache[c];
229eda14cbcSMatt Macy 
230eda14cbcSMatt Macy 		ASSERT(zio_data_buf_cache[c] != NULL);
231eda14cbcSMatt Macy 		if (zio_data_buf_cache[c - 1] == NULL)
232eda14cbcSMatt Macy 			zio_data_buf_cache[c - 1] = zio_data_buf_cache[c];
233eda14cbcSMatt Macy 	}
234eda14cbcSMatt Macy 
235eda14cbcSMatt Macy 	zio_inject_init();
236eda14cbcSMatt Macy 
237eda14cbcSMatt Macy 	lz4_init();
238eda14cbcSMatt Macy }
239eda14cbcSMatt Macy 
240eda14cbcSMatt Macy void
241eda14cbcSMatt Macy zio_fini(void)
242eda14cbcSMatt Macy {
24316038816SMartin Matuska 	size_t n = SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT;
2444426311aSMateusz Guzik 
245eda14cbcSMatt Macy #if defined(ZFS_DEBUG) && !defined(_KERNEL)
24616038816SMartin Matuska 	for (size_t i = 0; i < n; i++) {
2474426311aSMateusz Guzik 		if (zio_buf_cache_allocs[i] != zio_buf_cache_frees[i])
248eda14cbcSMatt Macy 			(void) printf("zio_fini: [%d] %llu != %llu\n",
2494426311aSMateusz Guzik 			    (int)((i + 1) << SPA_MINBLOCKSHIFT),
2504426311aSMateusz Guzik 			    (long long unsigned)zio_buf_cache_allocs[i],
2514426311aSMateusz Guzik 			    (long long unsigned)zio_buf_cache_frees[i]);
2524426311aSMateusz Guzik 	}
253eda14cbcSMatt Macy #endif
254eda14cbcSMatt Macy 
2554426311aSMateusz Guzik 	/*
2564426311aSMateusz Guzik 	 * The same kmem cache can show up multiple times in both zio_buf_cache
2574426311aSMateusz Guzik 	 * and zio_data_buf_cache. Do a wasteful but trivially correct scan to
2584426311aSMateusz Guzik 	 * sort it out.
2594426311aSMateusz Guzik 	 */
26016038816SMartin Matuska 	for (size_t i = 0; i < n; i++) {
26116038816SMartin Matuska 		kmem_cache_t *cache = zio_buf_cache[i];
2624426311aSMateusz Guzik 		if (cache == NULL)
2634426311aSMateusz Guzik 			continue;
26416038816SMartin Matuska 		for (size_t j = i; j < n; j++) {
2654426311aSMateusz Guzik 			if (cache == zio_buf_cache[j])
2664426311aSMateusz Guzik 				zio_buf_cache[j] = NULL;
2674426311aSMateusz Guzik 			if (cache == zio_data_buf_cache[j])
2684426311aSMateusz Guzik 				zio_data_buf_cache[j] = NULL;
269eda14cbcSMatt Macy 		}
2704426311aSMateusz Guzik 		kmem_cache_destroy(cache);
2714426311aSMateusz Guzik 	}
2724426311aSMateusz Guzik 
27316038816SMartin Matuska 	for (size_t i = 0; i < n; i++) {
27416038816SMartin Matuska 		kmem_cache_t *cache = zio_data_buf_cache[i];
2754426311aSMateusz Guzik 		if (cache == NULL)
2764426311aSMateusz Guzik 			continue;
27716038816SMartin Matuska 		for (size_t j = i; j < n; j++) {
2784426311aSMateusz Guzik 			if (cache == zio_data_buf_cache[j])
2794426311aSMateusz Guzik 				zio_data_buf_cache[j] = NULL;
2804426311aSMateusz Guzik 		}
2814426311aSMateusz Guzik 		kmem_cache_destroy(cache);
2824426311aSMateusz Guzik 	}
2834426311aSMateusz Guzik 
28416038816SMartin Matuska 	for (size_t i = 0; i < n; i++) {
28516038816SMartin Matuska 		VERIFY3P(zio_buf_cache[i], ==, NULL);
28616038816SMartin Matuska 		VERIFY3P(zio_data_buf_cache[i], ==, NULL);
287eda14cbcSMatt Macy 	}
288eda14cbcSMatt Macy 
289eda14cbcSMatt Macy 	kmem_cache_destroy(zio_link_cache);
290eda14cbcSMatt Macy 	kmem_cache_destroy(zio_cache);
291eda14cbcSMatt Macy 
292eda14cbcSMatt Macy 	zio_inject_fini();
293eda14cbcSMatt Macy 
294eda14cbcSMatt Macy 	lz4_fini();
295eda14cbcSMatt Macy }
296eda14cbcSMatt Macy 
297eda14cbcSMatt Macy /*
298eda14cbcSMatt Macy  * ==========================================================================
299eda14cbcSMatt Macy  * Allocate and free I/O buffers
300eda14cbcSMatt Macy  * ==========================================================================
301eda14cbcSMatt Macy  */
302eda14cbcSMatt Macy 
303e2df9bb4SMartin Matuska #if defined(ZFS_DEBUG) && defined(_KERNEL)
304e2df9bb4SMartin Matuska #define	ZFS_ZIO_BUF_CANARY	1
3053494f7c0SMartin Matuska #endif
3063494f7c0SMartin Matuska 
307e2df9bb4SMartin Matuska #ifdef ZFS_ZIO_BUF_CANARY
308e2df9bb4SMartin Matuska static const ulong_t zio_buf_canary = (ulong_t)0xdeadc0dedead210b;
309e2df9bb4SMartin Matuska 
3103494f7c0SMartin Matuska /*
3113494f7c0SMartin Matuska  * Use empty space after the buffer to detect overflows.
3123494f7c0SMartin Matuska  *
3133494f7c0SMartin Matuska  * Since zio_init() creates kmem caches only for certain set of buffer sizes,
3143494f7c0SMartin Matuska  * allocations of different sizes may have some unused space after the data.
3153494f7c0SMartin Matuska  * Filling part of that space with a known pattern on allocation and checking
3163494f7c0SMartin Matuska  * it on free should allow us to detect some buffer overflows.
3173494f7c0SMartin Matuska  */
3183494f7c0SMartin Matuska static void
3193494f7c0SMartin Matuska zio_buf_put_canary(ulong_t *p, size_t size, kmem_cache_t **cache, size_t c)
3203494f7c0SMartin Matuska {
3213494f7c0SMartin Matuska 	size_t off = P2ROUNDUP(size, sizeof (ulong_t));
3223494f7c0SMartin Matuska 	ulong_t *canary = p + off / sizeof (ulong_t);
3233494f7c0SMartin Matuska 	size_t asize = (c + 1) << SPA_MINBLOCKSHIFT;
3243494f7c0SMartin Matuska 	if (c + 1 < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT &&
3253494f7c0SMartin Matuska 	    cache[c] == cache[c + 1])
3263494f7c0SMartin Matuska 		asize = (c + 2) << SPA_MINBLOCKSHIFT;
3273494f7c0SMartin Matuska 	for (; off < asize; canary++, off += sizeof (ulong_t))
3283494f7c0SMartin Matuska 		*canary = zio_buf_canary;
3293494f7c0SMartin Matuska }
3303494f7c0SMartin Matuska 
3313494f7c0SMartin Matuska static void
3323494f7c0SMartin Matuska zio_buf_check_canary(ulong_t *p, size_t size, kmem_cache_t **cache, size_t c)
3333494f7c0SMartin Matuska {
3343494f7c0SMartin Matuska 	size_t off = P2ROUNDUP(size, sizeof (ulong_t));
3353494f7c0SMartin Matuska 	ulong_t *canary = p + off / sizeof (ulong_t);
3363494f7c0SMartin Matuska 	size_t asize = (c + 1) << SPA_MINBLOCKSHIFT;
3373494f7c0SMartin Matuska 	if (c + 1 < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT &&
3383494f7c0SMartin Matuska 	    cache[c] == cache[c + 1])
3393494f7c0SMartin Matuska 		asize = (c + 2) << SPA_MINBLOCKSHIFT;
3403494f7c0SMartin Matuska 	for (; off < asize; canary++, off += sizeof (ulong_t)) {
3413494f7c0SMartin Matuska 		if (unlikely(*canary != zio_buf_canary)) {
3423494f7c0SMartin Matuska 			PANIC("ZIO buffer overflow %p (%zu) + %zu %#lx != %#lx",
3433494f7c0SMartin Matuska 			    p, size, (canary - p) * sizeof (ulong_t),
3443494f7c0SMartin Matuska 			    *canary, zio_buf_canary);
3453494f7c0SMartin Matuska 		}
3463494f7c0SMartin Matuska 	}
3473494f7c0SMartin Matuska }
348e2df9bb4SMartin Matuska #endif
3493494f7c0SMartin Matuska 
350eda14cbcSMatt Macy /*
351eda14cbcSMatt Macy  * Use zio_buf_alloc to allocate ZFS metadata.  This data will appear in a
352eda14cbcSMatt Macy  * crashdump if the kernel panics, so use it judiciously.  Obviously, it's
353eda14cbcSMatt Macy  * useful to inspect ZFS metadata, but if possible, we should avoid keeping
354eda14cbcSMatt Macy  * excess / transient data in-core during a crashdump.
355eda14cbcSMatt Macy  */
356eda14cbcSMatt Macy void *
357eda14cbcSMatt Macy zio_buf_alloc(size_t size)
358eda14cbcSMatt Macy {
359eda14cbcSMatt Macy 	size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
360eda14cbcSMatt Macy 
361eda14cbcSMatt Macy 	VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
362eda14cbcSMatt Macy #if defined(ZFS_DEBUG) && !defined(_KERNEL)
363eda14cbcSMatt Macy 	atomic_add_64(&zio_buf_cache_allocs[c], 1);
364eda14cbcSMatt Macy #endif
365eda14cbcSMatt Macy 
3663494f7c0SMartin Matuska 	void *p = kmem_cache_alloc(zio_buf_cache[c], KM_PUSHPAGE);
367e2df9bb4SMartin Matuska #ifdef ZFS_ZIO_BUF_CANARY
3683494f7c0SMartin Matuska 	zio_buf_put_canary(p, size, zio_buf_cache, c);
369e2df9bb4SMartin Matuska #endif
3703494f7c0SMartin Matuska 	return (p);
371eda14cbcSMatt Macy }
372eda14cbcSMatt Macy 
373eda14cbcSMatt Macy /*
374eda14cbcSMatt Macy  * Use zio_data_buf_alloc to allocate data.  The data will not appear in a
375eda14cbcSMatt Macy  * crashdump if the kernel panics.  This exists so that we will limit the amount
376eda14cbcSMatt Macy  * of ZFS data that shows up in a kernel crashdump.  (Thus reducing the amount
377eda14cbcSMatt Macy  * of kernel heap dumped to disk when the kernel panics)
378eda14cbcSMatt Macy  */
379eda14cbcSMatt Macy void *
380eda14cbcSMatt Macy zio_data_buf_alloc(size_t size)
381eda14cbcSMatt Macy {
382eda14cbcSMatt Macy 	size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
383eda14cbcSMatt Macy 
384eda14cbcSMatt Macy 	VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
385eda14cbcSMatt Macy 
3863494f7c0SMartin Matuska 	void *p = kmem_cache_alloc(zio_data_buf_cache[c], KM_PUSHPAGE);
387e2df9bb4SMartin Matuska #ifdef ZFS_ZIO_BUF_CANARY
3883494f7c0SMartin Matuska 	zio_buf_put_canary(p, size, zio_data_buf_cache, c);
389e2df9bb4SMartin Matuska #endif
3903494f7c0SMartin Matuska 	return (p);
391eda14cbcSMatt Macy }
392eda14cbcSMatt Macy 
393eda14cbcSMatt Macy void
394eda14cbcSMatt Macy zio_buf_free(void *buf, size_t size)
395eda14cbcSMatt Macy {
396eda14cbcSMatt Macy 	size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
397eda14cbcSMatt Macy 
398eda14cbcSMatt Macy 	VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
399eda14cbcSMatt Macy #if defined(ZFS_DEBUG) && !defined(_KERNEL)
400eda14cbcSMatt Macy 	atomic_add_64(&zio_buf_cache_frees[c], 1);
401eda14cbcSMatt Macy #endif
402eda14cbcSMatt Macy 
403e2df9bb4SMartin Matuska #ifdef ZFS_ZIO_BUF_CANARY
4043494f7c0SMartin Matuska 	zio_buf_check_canary(buf, size, zio_buf_cache, c);
405e2df9bb4SMartin Matuska #endif
406eda14cbcSMatt Macy 	kmem_cache_free(zio_buf_cache[c], buf);
407eda14cbcSMatt Macy }
408eda14cbcSMatt Macy 
409eda14cbcSMatt Macy void
410eda14cbcSMatt Macy zio_data_buf_free(void *buf, size_t size)
411eda14cbcSMatt Macy {
412eda14cbcSMatt Macy 	size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
413eda14cbcSMatt Macy 
414eda14cbcSMatt Macy 	VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
415eda14cbcSMatt Macy 
416e2df9bb4SMartin Matuska #ifdef ZFS_ZIO_BUF_CANARY
4173494f7c0SMartin Matuska 	zio_buf_check_canary(buf, size, zio_data_buf_cache, c);
418e2df9bb4SMartin Matuska #endif
419eda14cbcSMatt Macy 	kmem_cache_free(zio_data_buf_cache[c], buf);
420eda14cbcSMatt Macy }
421eda14cbcSMatt Macy 
422eda14cbcSMatt Macy static void
423eda14cbcSMatt Macy zio_abd_free(void *abd, size_t size)
424eda14cbcSMatt Macy {
425e92ffd9bSMartin Matuska 	(void) size;
426eda14cbcSMatt Macy 	abd_free((abd_t *)abd);
427eda14cbcSMatt Macy }
428eda14cbcSMatt Macy 
429eda14cbcSMatt Macy /*
430eda14cbcSMatt Macy  * ==========================================================================
431eda14cbcSMatt Macy  * Push and pop I/O transform buffers
432eda14cbcSMatt Macy  * ==========================================================================
433eda14cbcSMatt Macy  */
434eda14cbcSMatt Macy void
435eda14cbcSMatt Macy zio_push_transform(zio_t *zio, abd_t *data, uint64_t size, uint64_t bufsize,
436eda14cbcSMatt Macy     zio_transform_func_t *transform)
437eda14cbcSMatt Macy {
438eda14cbcSMatt Macy 	zio_transform_t *zt = kmem_alloc(sizeof (zio_transform_t), KM_SLEEP);
439eda14cbcSMatt Macy 
440eda14cbcSMatt Macy 	zt->zt_orig_abd = zio->io_abd;
441eda14cbcSMatt Macy 	zt->zt_orig_size = zio->io_size;
442eda14cbcSMatt Macy 	zt->zt_bufsize = bufsize;
443eda14cbcSMatt Macy 	zt->zt_transform = transform;
444eda14cbcSMatt Macy 
445eda14cbcSMatt Macy 	zt->zt_next = zio->io_transform_stack;
446eda14cbcSMatt Macy 	zio->io_transform_stack = zt;
447eda14cbcSMatt Macy 
448eda14cbcSMatt Macy 	zio->io_abd = data;
449eda14cbcSMatt Macy 	zio->io_size = size;
450eda14cbcSMatt Macy }
451eda14cbcSMatt Macy 
452eda14cbcSMatt Macy void
453eda14cbcSMatt Macy zio_pop_transforms(zio_t *zio)
454eda14cbcSMatt Macy {
455eda14cbcSMatt Macy 	zio_transform_t *zt;
456eda14cbcSMatt Macy 
457eda14cbcSMatt Macy 	while ((zt = zio->io_transform_stack) != NULL) {
458eda14cbcSMatt Macy 		if (zt->zt_transform != NULL)
459eda14cbcSMatt Macy 			zt->zt_transform(zio,
460eda14cbcSMatt Macy 			    zt->zt_orig_abd, zt->zt_orig_size);
461eda14cbcSMatt Macy 
462eda14cbcSMatt Macy 		if (zt->zt_bufsize != 0)
463eda14cbcSMatt Macy 			abd_free(zio->io_abd);
464eda14cbcSMatt Macy 
465eda14cbcSMatt Macy 		zio->io_abd = zt->zt_orig_abd;
466eda14cbcSMatt Macy 		zio->io_size = zt->zt_orig_size;
467eda14cbcSMatt Macy 		zio->io_transform_stack = zt->zt_next;
468eda14cbcSMatt Macy 
469eda14cbcSMatt Macy 		kmem_free(zt, sizeof (zio_transform_t));
470eda14cbcSMatt Macy 	}
471eda14cbcSMatt Macy }
472eda14cbcSMatt Macy 
473eda14cbcSMatt Macy /*
474eda14cbcSMatt Macy  * ==========================================================================
475eda14cbcSMatt Macy  * I/O transform callbacks for subblocks, decompression, and decryption
476eda14cbcSMatt Macy  * ==========================================================================
477eda14cbcSMatt Macy  */
478eda14cbcSMatt Macy static void
479eda14cbcSMatt Macy zio_subblock(zio_t *zio, abd_t *data, uint64_t size)
480eda14cbcSMatt Macy {
481eda14cbcSMatt Macy 	ASSERT(zio->io_size > size);
482eda14cbcSMatt Macy 
483eda14cbcSMatt Macy 	if (zio->io_type == ZIO_TYPE_READ)
484eda14cbcSMatt Macy 		abd_copy(data, zio->io_abd, size);
485eda14cbcSMatt Macy }
486eda14cbcSMatt Macy 
487eda14cbcSMatt Macy static void
488eda14cbcSMatt Macy zio_decompress(zio_t *zio, abd_t *data, uint64_t size)
489eda14cbcSMatt Macy {
490eda14cbcSMatt Macy 	if (zio->io_error == 0) {
491eda14cbcSMatt Macy 		int ret = zio_decompress_data(BP_GET_COMPRESS(zio->io_bp),
492e2df9bb4SMartin Matuska 		    zio->io_abd, data, zio->io_size, size,
493eda14cbcSMatt Macy 		    &zio->io_prop.zp_complevel);
494eda14cbcSMatt Macy 
495eda14cbcSMatt Macy 		if (zio_injection_enabled && ret == 0)
496eda14cbcSMatt Macy 			ret = zio_handle_fault_injection(zio, EINVAL);
497eda14cbcSMatt Macy 
498eda14cbcSMatt Macy 		if (ret != 0)
499eda14cbcSMatt Macy 			zio->io_error = SET_ERROR(EIO);
500eda14cbcSMatt Macy 	}
501eda14cbcSMatt Macy }
502eda14cbcSMatt Macy 
503eda14cbcSMatt Macy static void
504eda14cbcSMatt Macy zio_decrypt(zio_t *zio, abd_t *data, uint64_t size)
505eda14cbcSMatt Macy {
506eda14cbcSMatt Macy 	int ret;
507eda14cbcSMatt Macy 	void *tmp;
508eda14cbcSMatt Macy 	blkptr_t *bp = zio->io_bp;
509eda14cbcSMatt Macy 	spa_t *spa = zio->io_spa;
510eda14cbcSMatt Macy 	uint64_t dsobj = zio->io_bookmark.zb_objset;
511eda14cbcSMatt Macy 	uint64_t lsize = BP_GET_LSIZE(bp);
512eda14cbcSMatt Macy 	dmu_object_type_t ot = BP_GET_TYPE(bp);
513eda14cbcSMatt Macy 	uint8_t salt[ZIO_DATA_SALT_LEN];
514eda14cbcSMatt Macy 	uint8_t iv[ZIO_DATA_IV_LEN];
515eda14cbcSMatt Macy 	uint8_t mac[ZIO_DATA_MAC_LEN];
516eda14cbcSMatt Macy 	boolean_t no_crypt = B_FALSE;
517eda14cbcSMatt Macy 
518eda14cbcSMatt Macy 	ASSERT(BP_USES_CRYPT(bp));
519eda14cbcSMatt Macy 	ASSERT3U(size, !=, 0);
520eda14cbcSMatt Macy 
521eda14cbcSMatt Macy 	if (zio->io_error != 0)
522eda14cbcSMatt Macy 		return;
523eda14cbcSMatt Macy 
524eda14cbcSMatt Macy 	/*
525eda14cbcSMatt Macy 	 * Verify the cksum of MACs stored in an indirect bp. It will always
526eda14cbcSMatt Macy 	 * be possible to verify this since it does not require an encryption
527eda14cbcSMatt Macy 	 * key.
528eda14cbcSMatt Macy 	 */
529eda14cbcSMatt Macy 	if (BP_HAS_INDIRECT_MAC_CKSUM(bp)) {
530eda14cbcSMatt Macy 		zio_crypt_decode_mac_bp(bp, mac);
531eda14cbcSMatt Macy 
532eda14cbcSMatt Macy 		if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF) {
533eda14cbcSMatt Macy 			/*
534eda14cbcSMatt Macy 			 * We haven't decompressed the data yet, but
535eda14cbcSMatt Macy 			 * zio_crypt_do_indirect_mac_checksum() requires
536eda14cbcSMatt Macy 			 * decompressed data to be able to parse out the MACs
537eda14cbcSMatt Macy 			 * from the indirect block. We decompress it now and
538eda14cbcSMatt Macy 			 * throw away the result after we are finished.
539eda14cbcSMatt Macy 			 */
540e2df9bb4SMartin Matuska 			abd_t *abd = abd_alloc_linear(lsize, B_TRUE);
541eda14cbcSMatt Macy 			ret = zio_decompress_data(BP_GET_COMPRESS(bp),
542e2df9bb4SMartin Matuska 			    zio->io_abd, abd, zio->io_size, lsize,
543eda14cbcSMatt Macy 			    &zio->io_prop.zp_complevel);
544eda14cbcSMatt Macy 			if (ret != 0) {
545e2df9bb4SMartin Matuska 				abd_free(abd);
546eda14cbcSMatt Macy 				ret = SET_ERROR(EIO);
547eda14cbcSMatt Macy 				goto error;
548eda14cbcSMatt Macy 			}
549e2df9bb4SMartin Matuska 			ret = zio_crypt_do_indirect_mac_checksum_abd(B_FALSE,
550e2df9bb4SMartin Matuska 			    abd, lsize, BP_SHOULD_BYTESWAP(bp), mac);
551e2df9bb4SMartin Matuska 			abd_free(abd);
552eda14cbcSMatt Macy 		} else {
553eda14cbcSMatt Macy 			ret = zio_crypt_do_indirect_mac_checksum_abd(B_FALSE,
554eda14cbcSMatt Macy 			    zio->io_abd, size, BP_SHOULD_BYTESWAP(bp), mac);
555eda14cbcSMatt Macy 		}
556eda14cbcSMatt Macy 		abd_copy(data, zio->io_abd, size);
557eda14cbcSMatt Macy 
558eda14cbcSMatt Macy 		if (zio_injection_enabled && ot != DMU_OT_DNODE && ret == 0) {
559eda14cbcSMatt Macy 			ret = zio_handle_decrypt_injection(spa,
560eda14cbcSMatt Macy 			    &zio->io_bookmark, ot, ECKSUM);
561eda14cbcSMatt Macy 		}
562eda14cbcSMatt Macy 		if (ret != 0)
563eda14cbcSMatt Macy 			goto error;
564eda14cbcSMatt Macy 
565eda14cbcSMatt Macy 		return;
566eda14cbcSMatt Macy 	}
567eda14cbcSMatt Macy 
568eda14cbcSMatt Macy 	/*
569eda14cbcSMatt Macy 	 * If this is an authenticated block, just check the MAC. It would be
570dbd5678dSMartin Matuska 	 * nice to separate this out into its own flag, but when this was done,
571dbd5678dSMartin Matuska 	 * we had run out of bits in what is now zio_flag_t. Future cleanup
572dbd5678dSMartin Matuska 	 * could make this a flag bit.
573eda14cbcSMatt Macy 	 */
574eda14cbcSMatt Macy 	if (BP_IS_AUTHENTICATED(bp)) {
575eda14cbcSMatt Macy 		if (ot == DMU_OT_OBJSET) {
576eda14cbcSMatt Macy 			ret = spa_do_crypt_objset_mac_abd(B_FALSE, spa,
577eda14cbcSMatt Macy 			    dsobj, zio->io_abd, size, BP_SHOULD_BYTESWAP(bp));
578eda14cbcSMatt Macy 		} else {
579eda14cbcSMatt Macy 			zio_crypt_decode_mac_bp(bp, mac);
580eda14cbcSMatt Macy 			ret = spa_do_crypt_mac_abd(B_FALSE, spa, dsobj,
581eda14cbcSMatt Macy 			    zio->io_abd, size, mac);
582eda14cbcSMatt Macy 			if (zio_injection_enabled && ret == 0) {
583eda14cbcSMatt Macy 				ret = zio_handle_decrypt_injection(spa,
584eda14cbcSMatt Macy 				    &zio->io_bookmark, ot, ECKSUM);
585eda14cbcSMatt Macy 			}
586eda14cbcSMatt Macy 		}
587eda14cbcSMatt Macy 		abd_copy(data, zio->io_abd, size);
588eda14cbcSMatt Macy 
589eda14cbcSMatt Macy 		if (ret != 0)
590eda14cbcSMatt Macy 			goto error;
591eda14cbcSMatt Macy 
592eda14cbcSMatt Macy 		return;
593eda14cbcSMatt Macy 	}
594eda14cbcSMatt Macy 
595eda14cbcSMatt Macy 	zio_crypt_decode_params_bp(bp, salt, iv);
596eda14cbcSMatt Macy 
597eda14cbcSMatt Macy 	if (ot == DMU_OT_INTENT_LOG) {
598eda14cbcSMatt Macy 		tmp = abd_borrow_buf_copy(zio->io_abd, sizeof (zil_chain_t));
599eda14cbcSMatt Macy 		zio_crypt_decode_mac_zil(tmp, mac);
600eda14cbcSMatt Macy 		abd_return_buf(zio->io_abd, tmp, sizeof (zil_chain_t));
601eda14cbcSMatt Macy 	} else {
602eda14cbcSMatt Macy 		zio_crypt_decode_mac_bp(bp, mac);
603eda14cbcSMatt Macy 	}
604eda14cbcSMatt Macy 
605eda14cbcSMatt Macy 	ret = spa_do_crypt_abd(B_FALSE, spa, &zio->io_bookmark, BP_GET_TYPE(bp),
606eda14cbcSMatt Macy 	    BP_GET_DEDUP(bp), BP_SHOULD_BYTESWAP(bp), salt, iv, mac, size, data,
607eda14cbcSMatt Macy 	    zio->io_abd, &no_crypt);
608eda14cbcSMatt Macy 	if (no_crypt)
609eda14cbcSMatt Macy 		abd_copy(data, zio->io_abd, size);
610eda14cbcSMatt Macy 
611eda14cbcSMatt Macy 	if (ret != 0)
612eda14cbcSMatt Macy 		goto error;
613eda14cbcSMatt Macy 
614eda14cbcSMatt Macy 	return;
615eda14cbcSMatt Macy 
616eda14cbcSMatt Macy error:
617eda14cbcSMatt Macy 	/* assert that the key was found unless this was speculative */
618eda14cbcSMatt Macy 	ASSERT(ret != EACCES || (zio->io_flags & ZIO_FLAG_SPECULATIVE));
619eda14cbcSMatt Macy 
620eda14cbcSMatt Macy 	/*
621eda14cbcSMatt Macy 	 * If there was a decryption / authentication error return EIO as
622eda14cbcSMatt Macy 	 * the io_error. If this was not a speculative zio, create an ereport.
623eda14cbcSMatt Macy 	 */
624eda14cbcSMatt Macy 	if (ret == ECKSUM) {
625eda14cbcSMatt Macy 		zio->io_error = SET_ERROR(EIO);
626eda14cbcSMatt Macy 		if ((zio->io_flags & ZIO_FLAG_SPECULATIVE) == 0) {
6272a58b312SMartin Matuska 			spa_log_error(spa, &zio->io_bookmark,
628783d3ff6SMartin Matuska 			    BP_GET_LOGICAL_BIRTH(zio->io_bp));
629eac7052fSMatt Macy 			(void) zfs_ereport_post(FM_EREPORT_ZFS_AUTHENTICATION,
6302c48331dSMatt Macy 			    spa, NULL, &zio->io_bookmark, zio, 0);
631eda14cbcSMatt Macy 		}
632eda14cbcSMatt Macy 	} else {
633eda14cbcSMatt Macy 		zio->io_error = ret;
634eda14cbcSMatt Macy 	}
635eda14cbcSMatt Macy }
636eda14cbcSMatt Macy 
637eda14cbcSMatt Macy /*
638eda14cbcSMatt Macy  * ==========================================================================
639eda14cbcSMatt Macy  * I/O parent/child relationships and pipeline interlocks
640eda14cbcSMatt Macy  * ==========================================================================
641eda14cbcSMatt Macy  */
642eda14cbcSMatt Macy zio_t *
643eda14cbcSMatt Macy zio_walk_parents(zio_t *cio, zio_link_t **zl)
644eda14cbcSMatt Macy {
645eda14cbcSMatt Macy 	list_t *pl = &cio->io_parent_list;
646eda14cbcSMatt Macy 
647eda14cbcSMatt Macy 	*zl = (*zl == NULL) ? list_head(pl) : list_next(pl, *zl);
648eda14cbcSMatt Macy 	if (*zl == NULL)
649eda14cbcSMatt Macy 		return (NULL);
650eda14cbcSMatt Macy 
651eda14cbcSMatt Macy 	ASSERT((*zl)->zl_child == cio);
652eda14cbcSMatt Macy 	return ((*zl)->zl_parent);
653eda14cbcSMatt Macy }
654eda14cbcSMatt Macy 
655eda14cbcSMatt Macy zio_t *
656eda14cbcSMatt Macy zio_walk_children(zio_t *pio, zio_link_t **zl)
657eda14cbcSMatt Macy {
658eda14cbcSMatt Macy 	list_t *cl = &pio->io_child_list;
659eda14cbcSMatt Macy 
660eda14cbcSMatt Macy 	ASSERT(MUTEX_HELD(&pio->io_lock));
661eda14cbcSMatt Macy 
662eda14cbcSMatt Macy 	*zl = (*zl == NULL) ? list_head(cl) : list_next(cl, *zl);
663eda14cbcSMatt Macy 	if (*zl == NULL)
664eda14cbcSMatt Macy 		return (NULL);
665eda14cbcSMatt Macy 
666eda14cbcSMatt Macy 	ASSERT((*zl)->zl_parent == pio);
667eda14cbcSMatt Macy 	return ((*zl)->zl_child);
668eda14cbcSMatt Macy }
669eda14cbcSMatt Macy 
670eda14cbcSMatt Macy zio_t *
671eda14cbcSMatt Macy zio_unique_parent(zio_t *cio)
672eda14cbcSMatt Macy {
673eda14cbcSMatt Macy 	zio_link_t *zl = NULL;
674eda14cbcSMatt Macy 	zio_t *pio = zio_walk_parents(cio, &zl);
675eda14cbcSMatt Macy 
676eda14cbcSMatt Macy 	VERIFY3P(zio_walk_parents(cio, &zl), ==, NULL);
677eda14cbcSMatt Macy 	return (pio);
678eda14cbcSMatt Macy }
679eda14cbcSMatt Macy 
680eda14cbcSMatt Macy void
681eda14cbcSMatt Macy zio_add_child(zio_t *pio, zio_t *cio)
682eda14cbcSMatt Macy {
683eda14cbcSMatt Macy 	/*
684eda14cbcSMatt Macy 	 * Logical I/Os can have logical, gang, or vdev children.
685eda14cbcSMatt Macy 	 * Gang I/Os can have gang or vdev children.
686eda14cbcSMatt Macy 	 * Vdev I/Os can only have vdev children.
687eda14cbcSMatt Macy 	 * The following ASSERT captures all of these constraints.
688eda14cbcSMatt Macy 	 */
689eda14cbcSMatt Macy 	ASSERT3S(cio->io_child_type, <=, pio->io_child_type);
690eda14cbcSMatt Macy 
6916c1e79dfSMartin Matuska 	/* Parent should not have READY stage if child doesn't have it. */
6926c1e79dfSMartin Matuska 	IMPLY((cio->io_pipeline & ZIO_STAGE_READY) == 0 &&
6936c1e79dfSMartin Matuska 	    (cio->io_child_type != ZIO_CHILD_VDEV),
6946c1e79dfSMartin Matuska 	    (pio->io_pipeline & ZIO_STAGE_READY) == 0);
6956c1e79dfSMartin Matuska 
6960a97523dSMartin Matuska 	zio_link_t *zl = kmem_cache_alloc(zio_link_cache, KM_SLEEP);
697eda14cbcSMatt Macy 	zl->zl_parent = pio;
698eda14cbcSMatt Macy 	zl->zl_child = cio;
699eda14cbcSMatt Macy 
700eda14cbcSMatt Macy 	mutex_enter(&pio->io_lock);
701eda14cbcSMatt Macy 	mutex_enter(&cio->io_lock);
702eda14cbcSMatt Macy 
703eda14cbcSMatt Macy 	ASSERT(pio->io_state[ZIO_WAIT_DONE] == 0);
704eda14cbcSMatt Macy 
7050a97523dSMartin Matuska 	uint64_t *countp = pio->io_children[cio->io_child_type];
706eda14cbcSMatt Macy 	for (int w = 0; w < ZIO_WAIT_TYPES; w++)
7070a97523dSMartin Matuska 		countp[w] += !cio->io_state[w];
708eda14cbcSMatt Macy 
709eda14cbcSMatt Macy 	list_insert_head(&pio->io_child_list, zl);
710eda14cbcSMatt Macy 	list_insert_head(&cio->io_parent_list, zl);
711eda14cbcSMatt Macy 
712eda14cbcSMatt Macy 	mutex_exit(&cio->io_lock);
713eda14cbcSMatt Macy 	mutex_exit(&pio->io_lock);
714eda14cbcSMatt Macy }
715eda14cbcSMatt Macy 
7160a97523dSMartin Matuska void
7170a97523dSMartin Matuska zio_add_child_first(zio_t *pio, zio_t *cio)
7180a97523dSMartin Matuska {
7190a97523dSMartin Matuska 	/*
7200a97523dSMartin Matuska 	 * Logical I/Os can have logical, gang, or vdev children.
7210a97523dSMartin Matuska 	 * Gang I/Os can have gang or vdev children.
7220a97523dSMartin Matuska 	 * Vdev I/Os can only have vdev children.
7230a97523dSMartin Matuska 	 * The following ASSERT captures all of these constraints.
7240a97523dSMartin Matuska 	 */
7250a97523dSMartin Matuska 	ASSERT3S(cio->io_child_type, <=, pio->io_child_type);
7260a97523dSMartin Matuska 
7276c1e79dfSMartin Matuska 	/* Parent should not have READY stage if child doesn't have it. */
7286c1e79dfSMartin Matuska 	IMPLY((cio->io_pipeline & ZIO_STAGE_READY) == 0 &&
7296c1e79dfSMartin Matuska 	    (cio->io_child_type != ZIO_CHILD_VDEV),
7306c1e79dfSMartin Matuska 	    (pio->io_pipeline & ZIO_STAGE_READY) == 0);
7316c1e79dfSMartin Matuska 
7320a97523dSMartin Matuska 	zio_link_t *zl = kmem_cache_alloc(zio_link_cache, KM_SLEEP);
7330a97523dSMartin Matuska 	zl->zl_parent = pio;
7340a97523dSMartin Matuska 	zl->zl_child = cio;
7350a97523dSMartin Matuska 
7360a97523dSMartin Matuska 	ASSERT(list_is_empty(&cio->io_parent_list));
7370a97523dSMartin Matuska 	list_insert_head(&cio->io_parent_list, zl);
7380a97523dSMartin Matuska 
7390a97523dSMartin Matuska 	mutex_enter(&pio->io_lock);
7400a97523dSMartin Matuska 
7410a97523dSMartin Matuska 	ASSERT(pio->io_state[ZIO_WAIT_DONE] == 0);
7420a97523dSMartin Matuska 
7430a97523dSMartin Matuska 	uint64_t *countp = pio->io_children[cio->io_child_type];
7440a97523dSMartin Matuska 	for (int w = 0; w < ZIO_WAIT_TYPES; w++)
7450a97523dSMartin Matuska 		countp[w] += !cio->io_state[w];
7460a97523dSMartin Matuska 
7470a97523dSMartin Matuska 	list_insert_head(&pio->io_child_list, zl);
7480a97523dSMartin Matuska 
7490a97523dSMartin Matuska 	mutex_exit(&pio->io_lock);
7500a97523dSMartin Matuska }
7510a97523dSMartin Matuska 
752eda14cbcSMatt Macy static void
753eda14cbcSMatt Macy zio_remove_child(zio_t *pio, zio_t *cio, zio_link_t *zl)
754eda14cbcSMatt Macy {
755eda14cbcSMatt Macy 	ASSERT(zl->zl_parent == pio);
756eda14cbcSMatt Macy 	ASSERT(zl->zl_child == cio);
757eda14cbcSMatt Macy 
758eda14cbcSMatt Macy 	mutex_enter(&pio->io_lock);
759eda14cbcSMatt Macy 	mutex_enter(&cio->io_lock);
760eda14cbcSMatt Macy 
761eda14cbcSMatt Macy 	list_remove(&pio->io_child_list, zl);
762eda14cbcSMatt Macy 	list_remove(&cio->io_parent_list, zl);
763eda14cbcSMatt Macy 
764eda14cbcSMatt Macy 	mutex_exit(&cio->io_lock);
765eda14cbcSMatt Macy 	mutex_exit(&pio->io_lock);
766eda14cbcSMatt Macy 	kmem_cache_free(zio_link_cache, zl);
767eda14cbcSMatt Macy }
768eda14cbcSMatt Macy 
769eda14cbcSMatt Macy static boolean_t
770eda14cbcSMatt Macy zio_wait_for_children(zio_t *zio, uint8_t childbits, enum zio_wait_type wait)
771eda14cbcSMatt Macy {
772eda14cbcSMatt Macy 	boolean_t waiting = B_FALSE;
773eda14cbcSMatt Macy 
774eda14cbcSMatt Macy 	mutex_enter(&zio->io_lock);
775eda14cbcSMatt Macy 	ASSERT(zio->io_stall == NULL);
776eda14cbcSMatt Macy 	for (int c = 0; c < ZIO_CHILD_TYPES; c++) {
777eda14cbcSMatt Macy 		if (!(ZIO_CHILD_BIT_IS_SET(childbits, c)))
778eda14cbcSMatt Macy 			continue;
779eda14cbcSMatt Macy 
780eda14cbcSMatt Macy 		uint64_t *countp = &zio->io_children[c][wait];
781eda14cbcSMatt Macy 		if (*countp != 0) {
782eda14cbcSMatt Macy 			zio->io_stage >>= 1;
783eda14cbcSMatt Macy 			ASSERT3U(zio->io_stage, !=, ZIO_STAGE_OPEN);
784eda14cbcSMatt Macy 			zio->io_stall = countp;
785eda14cbcSMatt Macy 			waiting = B_TRUE;
786eda14cbcSMatt Macy 			break;
787eda14cbcSMatt Macy 		}
788eda14cbcSMatt Macy 	}
789eda14cbcSMatt Macy 	mutex_exit(&zio->io_lock);
790eda14cbcSMatt Macy 	return (waiting);
791eda14cbcSMatt Macy }
792eda14cbcSMatt Macy 
793eda14cbcSMatt Macy __attribute__((always_inline))
794eda14cbcSMatt Macy static inline void
795eda14cbcSMatt Macy zio_notify_parent(zio_t *pio, zio_t *zio, enum zio_wait_type wait,
796eda14cbcSMatt Macy     zio_t **next_to_executep)
797eda14cbcSMatt Macy {
798eda14cbcSMatt Macy 	uint64_t *countp = &pio->io_children[zio->io_child_type][wait];
799eda14cbcSMatt Macy 	int *errorp = &pio->io_child_error[zio->io_child_type];
800eda14cbcSMatt Macy 
801eda14cbcSMatt Macy 	mutex_enter(&pio->io_lock);
802eda14cbcSMatt Macy 	if (zio->io_error && !(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
803eda14cbcSMatt Macy 		*errorp = zio_worst_error(*errorp, zio->io_error);
804eda14cbcSMatt Macy 	pio->io_reexecute |= zio->io_reexecute;
805eda14cbcSMatt Macy 	ASSERT3U(*countp, >, 0);
806eda14cbcSMatt Macy 
80787bf66d4SMartin Matuska 	/*
80887bf66d4SMartin Matuska 	 * Propogate the Direct I/O checksum verify failure to the parent.
80987bf66d4SMartin Matuska 	 */
81087bf66d4SMartin Matuska 	if (zio->io_flags & ZIO_FLAG_DIO_CHKSUM_ERR)
8117a7741afSMartin Matuska 		pio->io_flags |= ZIO_FLAG_DIO_CHKSUM_ERR;
8127a7741afSMartin Matuska 
813eda14cbcSMatt Macy 	(*countp)--;
814eda14cbcSMatt Macy 
815eda14cbcSMatt Macy 	if (*countp == 0 && pio->io_stall == countp) {
816eda14cbcSMatt Macy 		zio_taskq_type_t type =
817eda14cbcSMatt Macy 		    pio->io_stage < ZIO_STAGE_VDEV_IO_START ? ZIO_TASKQ_ISSUE :
818eda14cbcSMatt Macy 		    ZIO_TASKQ_INTERRUPT;
819eda14cbcSMatt Macy 		pio->io_stall = NULL;
820eda14cbcSMatt Macy 		mutex_exit(&pio->io_lock);
821eda14cbcSMatt Macy 
822eda14cbcSMatt Macy 		/*
823eda14cbcSMatt Macy 		 * If we can tell the caller to execute this parent next, do
824b985c9caSMartin Matuska 		 * so. We do this if the parent's zio type matches the child's
825b985c9caSMartin Matuska 		 * type, or if it's a zio_null() with no done callback, and so
826b985c9caSMartin Matuska 		 * has no actual work to do. Otherwise dispatch the parent zio
827b985c9caSMartin Matuska 		 * in its own taskq.
828eda14cbcSMatt Macy 		 *
829eda14cbcSMatt Macy 		 * Having the caller execute the parent when possible reduces
830eda14cbcSMatt Macy 		 * locking on the zio taskq's, reduces context switch
831eda14cbcSMatt Macy 		 * overhead, and has no recursion penalty.  Note that one
832eda14cbcSMatt Macy 		 * read from disk typically causes at least 3 zio's: a
833eda14cbcSMatt Macy 		 * zio_null(), the logical zio_read(), and then a physical
834eda14cbcSMatt Macy 		 * zio.  When the physical ZIO completes, we are able to call
835eda14cbcSMatt Macy 		 * zio_done() on all 3 of these zio's from one invocation of
836eda14cbcSMatt Macy 		 * zio_execute() by returning the parent back to
837eda14cbcSMatt Macy 		 * zio_execute().  Since the parent isn't executed until this
838eda14cbcSMatt Macy 		 * thread returns back to zio_execute(), the caller should do
839eda14cbcSMatt Macy 		 * so promptly.
840eda14cbcSMatt Macy 		 *
841eda14cbcSMatt Macy 		 * In other cases, dispatching the parent prevents
842eda14cbcSMatt Macy 		 * overflowing the stack when we have deeply nested
843eda14cbcSMatt Macy 		 * parent-child relationships, as we do with the "mega zio"
844eda14cbcSMatt Macy 		 * of writes for spa_sync(), and the chain of ZIL blocks.
845eda14cbcSMatt Macy 		 */
846bb2d13b6SMartin Matuska 		if (next_to_executep != NULL && *next_to_executep == NULL &&
847b985c9caSMartin Matuska 		    (pio->io_type == zio->io_type ||
848b985c9caSMartin Matuska 		    (pio->io_type == ZIO_TYPE_NULL && !pio->io_done))) {
849eda14cbcSMatt Macy 			*next_to_executep = pio;
850eda14cbcSMatt Macy 		} else {
851eda14cbcSMatt Macy 			zio_taskq_dispatch(pio, type, B_FALSE);
852eda14cbcSMatt Macy 		}
853eda14cbcSMatt Macy 	} else {
854eda14cbcSMatt Macy 		mutex_exit(&pio->io_lock);
855eda14cbcSMatt Macy 	}
856eda14cbcSMatt Macy }
857eda14cbcSMatt Macy 
858eda14cbcSMatt Macy static void
859eda14cbcSMatt Macy zio_inherit_child_errors(zio_t *zio, enum zio_child c)
860eda14cbcSMatt Macy {
861eda14cbcSMatt Macy 	if (zio->io_child_error[c] != 0 && zio->io_error == 0)
862eda14cbcSMatt Macy 		zio->io_error = zio->io_child_error[c];
863eda14cbcSMatt Macy }
864eda14cbcSMatt Macy 
865eda14cbcSMatt Macy int
866eda14cbcSMatt Macy zio_bookmark_compare(const void *x1, const void *x2)
867eda14cbcSMatt Macy {
868eda14cbcSMatt Macy 	const zio_t *z1 = x1;
869eda14cbcSMatt Macy 	const zio_t *z2 = x2;
870eda14cbcSMatt Macy 
871eda14cbcSMatt Macy 	if (z1->io_bookmark.zb_objset < z2->io_bookmark.zb_objset)
872eda14cbcSMatt Macy 		return (-1);
873eda14cbcSMatt Macy 	if (z1->io_bookmark.zb_objset > z2->io_bookmark.zb_objset)
874eda14cbcSMatt Macy 		return (1);
875eda14cbcSMatt Macy 
876eda14cbcSMatt Macy 	if (z1->io_bookmark.zb_object < z2->io_bookmark.zb_object)
877eda14cbcSMatt Macy 		return (-1);
878eda14cbcSMatt Macy 	if (z1->io_bookmark.zb_object > z2->io_bookmark.zb_object)
879eda14cbcSMatt Macy 		return (1);
880eda14cbcSMatt Macy 
881eda14cbcSMatt Macy 	if (z1->io_bookmark.zb_level < z2->io_bookmark.zb_level)
882eda14cbcSMatt Macy 		return (-1);
883eda14cbcSMatt Macy 	if (z1->io_bookmark.zb_level > z2->io_bookmark.zb_level)
884eda14cbcSMatt Macy 		return (1);
885eda14cbcSMatt Macy 
886eda14cbcSMatt Macy 	if (z1->io_bookmark.zb_blkid < z2->io_bookmark.zb_blkid)
887eda14cbcSMatt Macy 		return (-1);
888eda14cbcSMatt Macy 	if (z1->io_bookmark.zb_blkid > z2->io_bookmark.zb_blkid)
889eda14cbcSMatt Macy 		return (1);
890eda14cbcSMatt Macy 
891eda14cbcSMatt Macy 	if (z1 < z2)
892eda14cbcSMatt Macy 		return (-1);
893eda14cbcSMatt Macy 	if (z1 > z2)
894eda14cbcSMatt Macy 		return (1);
895eda14cbcSMatt Macy 
896eda14cbcSMatt Macy 	return (0);
897eda14cbcSMatt Macy }
898eda14cbcSMatt Macy 
899eda14cbcSMatt Macy /*
900eda14cbcSMatt Macy  * ==========================================================================
901eda14cbcSMatt Macy  * Create the various types of I/O (read, write, free, etc)
902eda14cbcSMatt Macy  * ==========================================================================
903eda14cbcSMatt Macy  */
904eda14cbcSMatt Macy static zio_t *
905eda14cbcSMatt Macy zio_create(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
906eda14cbcSMatt Macy     abd_t *data, uint64_t lsize, uint64_t psize, zio_done_func_t *done,
907eda14cbcSMatt Macy     void *private, zio_type_t type, zio_priority_t priority,
908dbd5678dSMartin Matuska     zio_flag_t flags, vdev_t *vd, uint64_t offset,
909eda14cbcSMatt Macy     const zbookmark_phys_t *zb, enum zio_stage stage,
910eda14cbcSMatt Macy     enum zio_stage pipeline)
911eda14cbcSMatt Macy {
912eda14cbcSMatt Macy 	zio_t *zio;
913eda14cbcSMatt Macy 
914eda14cbcSMatt Macy 	IMPLY(type != ZIO_TYPE_TRIM, psize <= SPA_MAXBLOCKSIZE);
915eda14cbcSMatt Macy 	ASSERT(P2PHASE(psize, SPA_MINBLOCKSIZE) == 0);
916eda14cbcSMatt Macy 	ASSERT(P2PHASE(offset, SPA_MINBLOCKSIZE) == 0);
917eda14cbcSMatt Macy 
918eda14cbcSMatt Macy 	ASSERT(!vd || spa_config_held(spa, SCL_STATE_ALL, RW_READER));
919eda14cbcSMatt Macy 	ASSERT(!bp || !(flags & ZIO_FLAG_CONFIG_WRITER));
920eda14cbcSMatt Macy 	ASSERT(vd || stage == ZIO_STAGE_OPEN);
921eda14cbcSMatt Macy 
922eda14cbcSMatt Macy 	IMPLY(lsize != psize, (flags & ZIO_FLAG_RAW_COMPRESS) != 0);
923eda14cbcSMatt Macy 
924eda14cbcSMatt Macy 	zio = kmem_cache_alloc(zio_cache, KM_SLEEP);
925da5137abSMartin Matuska 	memset(zio, 0, sizeof (zio_t));
926eda14cbcSMatt Macy 
927eda14cbcSMatt Macy 	mutex_init(&zio->io_lock, NULL, MUTEX_NOLOCKDEP, NULL);
928eda14cbcSMatt Macy 	cv_init(&zio->io_cv, NULL, CV_DEFAULT, NULL);
929eda14cbcSMatt Macy 
930eda14cbcSMatt Macy 	list_create(&zio->io_parent_list, sizeof (zio_link_t),
931eda14cbcSMatt Macy 	    offsetof(zio_link_t, zl_parent_node));
932eda14cbcSMatt Macy 	list_create(&zio->io_child_list, sizeof (zio_link_t),
933eda14cbcSMatt Macy 	    offsetof(zio_link_t, zl_child_node));
934eda14cbcSMatt Macy 	metaslab_trace_init(&zio->io_alloc_list);
935eda14cbcSMatt Macy 
936eda14cbcSMatt Macy 	if (vd != NULL)
937eda14cbcSMatt Macy 		zio->io_child_type = ZIO_CHILD_VDEV;
938eda14cbcSMatt Macy 	else if (flags & ZIO_FLAG_GANG_CHILD)
939eda14cbcSMatt Macy 		zio->io_child_type = ZIO_CHILD_GANG;
940eda14cbcSMatt Macy 	else if (flags & ZIO_FLAG_DDT_CHILD)
941eda14cbcSMatt Macy 		zio->io_child_type = ZIO_CHILD_DDT;
942eda14cbcSMatt Macy 	else
943eda14cbcSMatt Macy 		zio->io_child_type = ZIO_CHILD_LOGICAL;
944eda14cbcSMatt Macy 
945eda14cbcSMatt Macy 	if (bp != NULL) {
946eda14cbcSMatt Macy 		if (type != ZIO_TYPE_WRITE ||
9470a97523dSMartin Matuska 		    zio->io_child_type == ZIO_CHILD_DDT) {
9480a97523dSMartin Matuska 			zio->io_bp_copy = *bp;
949eda14cbcSMatt Macy 			zio->io_bp = &zio->io_bp_copy;	/* so caller can free */
9500a97523dSMartin Matuska 		} else {
9510a97523dSMartin Matuska 			zio->io_bp = (blkptr_t *)bp;
9520a97523dSMartin Matuska 		}
9530a97523dSMartin Matuska 		zio->io_bp_orig = *bp;
954eda14cbcSMatt Macy 		if (zio->io_child_type == ZIO_CHILD_LOGICAL)
955eda14cbcSMatt Macy 			zio->io_logical = zio;
956eda14cbcSMatt Macy 		if (zio->io_child_type > ZIO_CHILD_GANG && BP_IS_GANG(bp))
957eda14cbcSMatt Macy 			pipeline |= ZIO_GANG_STAGES;
958eda14cbcSMatt Macy 	}
959eda14cbcSMatt Macy 
960eda14cbcSMatt Macy 	zio->io_spa = spa;
961eda14cbcSMatt Macy 	zio->io_txg = txg;
962eda14cbcSMatt Macy 	zio->io_done = done;
963eda14cbcSMatt Macy 	zio->io_private = private;
964eda14cbcSMatt Macy 	zio->io_type = type;
965eda14cbcSMatt Macy 	zio->io_priority = priority;
966eda14cbcSMatt Macy 	zio->io_vd = vd;
967eda14cbcSMatt Macy 	zio->io_offset = offset;
968eda14cbcSMatt Macy 	zio->io_orig_abd = zio->io_abd = data;
969eda14cbcSMatt Macy 	zio->io_orig_size = zio->io_size = psize;
970eda14cbcSMatt Macy 	zio->io_lsize = lsize;
971eda14cbcSMatt Macy 	zio->io_orig_flags = zio->io_flags = flags;
972eda14cbcSMatt Macy 	zio->io_orig_stage = zio->io_stage = stage;
973eda14cbcSMatt Macy 	zio->io_orig_pipeline = zio->io_pipeline = pipeline;
974eda14cbcSMatt Macy 	zio->io_pipeline_trace = ZIO_STAGE_OPEN;
97514c2e0a0SMartin Matuska 	zio->io_allocator = ZIO_ALLOCATOR_NONE;
976eda14cbcSMatt Macy 
9776c1e79dfSMartin Matuska 	zio->io_state[ZIO_WAIT_READY] = (stage >= ZIO_STAGE_READY) ||
9786c1e79dfSMartin Matuska 	    (pipeline & ZIO_STAGE_READY) == 0;
979eda14cbcSMatt Macy 	zio->io_state[ZIO_WAIT_DONE] = (stage >= ZIO_STAGE_DONE);
980eda14cbcSMatt Macy 
981eda14cbcSMatt Macy 	if (zb != NULL)
982eda14cbcSMatt Macy 		zio->io_bookmark = *zb;
983eda14cbcSMatt Macy 
984eda14cbcSMatt Macy 	if (pio != NULL) {
985eda14cbcSMatt Macy 		zio->io_metaslab_class = pio->io_metaslab_class;
986eda14cbcSMatt Macy 		if (zio->io_logical == NULL)
987eda14cbcSMatt Macy 			zio->io_logical = pio->io_logical;
988eda14cbcSMatt Macy 		if (zio->io_child_type == ZIO_CHILD_GANG)
989eda14cbcSMatt Macy 			zio->io_gang_leader = pio->io_gang_leader;
9900a97523dSMartin Matuska 		zio_add_child_first(pio, zio);
991eda14cbcSMatt Macy 	}
992eda14cbcSMatt Macy 
993eda14cbcSMatt Macy 	taskq_init_ent(&zio->io_tqent);
994eda14cbcSMatt Macy 
995eda14cbcSMatt Macy 	return (zio);
996eda14cbcSMatt Macy }
997eda14cbcSMatt Macy 
998271171e0SMartin Matuska void
999eda14cbcSMatt Macy zio_destroy(zio_t *zio)
1000eda14cbcSMatt Macy {
1001eda14cbcSMatt Macy 	metaslab_trace_fini(&zio->io_alloc_list);
1002eda14cbcSMatt Macy 	list_destroy(&zio->io_parent_list);
1003eda14cbcSMatt Macy 	list_destroy(&zio->io_child_list);
1004eda14cbcSMatt Macy 	mutex_destroy(&zio->io_lock);
1005eda14cbcSMatt Macy 	cv_destroy(&zio->io_cv);
1006eda14cbcSMatt Macy 	kmem_cache_free(zio_cache, zio);
1007eda14cbcSMatt Macy }
1008eda14cbcSMatt Macy 
10096c1e79dfSMartin Matuska /*
10106c1e79dfSMartin Matuska  * ZIO intended to be between others.  Provides synchronization at READY
10116c1e79dfSMartin Matuska  * and DONE pipeline stages and calls the respective callbacks.
10126c1e79dfSMartin Matuska  */
1013eda14cbcSMatt Macy zio_t *
1014eda14cbcSMatt Macy zio_null(zio_t *pio, spa_t *spa, vdev_t *vd, zio_done_func_t *done,
1015dbd5678dSMartin Matuska     void *private, zio_flag_t flags)
1016eda14cbcSMatt Macy {
1017eda14cbcSMatt Macy 	zio_t *zio;
1018eda14cbcSMatt Macy 
1019eda14cbcSMatt Macy 	zio = zio_create(pio, spa, 0, NULL, NULL, 0, 0, done, private,
1020eda14cbcSMatt Macy 	    ZIO_TYPE_NULL, ZIO_PRIORITY_NOW, flags, vd, 0, NULL,
1021eda14cbcSMatt Macy 	    ZIO_STAGE_OPEN, ZIO_INTERLOCK_PIPELINE);
1022eda14cbcSMatt Macy 
1023eda14cbcSMatt Macy 	return (zio);
1024eda14cbcSMatt Macy }
1025eda14cbcSMatt Macy 
10266c1e79dfSMartin Matuska /*
10276c1e79dfSMartin Matuska  * ZIO intended to be a root of a tree.  Unlike null ZIO does not have a
10286c1e79dfSMartin Matuska  * READY pipeline stage (is ready on creation), so it should not be used
10296c1e79dfSMartin Matuska  * as child of any ZIO that may need waiting for grandchildren READY stage
10306c1e79dfSMartin Matuska  * (any other ZIO type).
10316c1e79dfSMartin Matuska  */
1032eda14cbcSMatt Macy zio_t *
1033dbd5678dSMartin Matuska zio_root(spa_t *spa, zio_done_func_t *done, void *private, zio_flag_t flags)
1034eda14cbcSMatt Macy {
10356c1e79dfSMartin Matuska 	zio_t *zio;
10366c1e79dfSMartin Matuska 
10376c1e79dfSMartin Matuska 	zio = zio_create(NULL, spa, 0, NULL, NULL, 0, 0, done, private,
10386c1e79dfSMartin Matuska 	    ZIO_TYPE_NULL, ZIO_PRIORITY_NOW, flags, NULL, 0, NULL,
10396c1e79dfSMartin Matuska 	    ZIO_STAGE_OPEN, ZIO_ROOT_PIPELINE);
10406c1e79dfSMartin Matuska 
10416c1e79dfSMartin Matuska 	return (zio);
1042eda14cbcSMatt Macy }
1043eda14cbcSMatt Macy 
1044eda14cbcSMatt Macy static int
1045eda14cbcSMatt Macy zfs_blkptr_verify_log(spa_t *spa, const blkptr_t *bp,
1046eda14cbcSMatt Macy     enum blk_verify_flag blk_verify, const char *fmt, ...)
1047eda14cbcSMatt Macy {
1048eda14cbcSMatt Macy 	va_list adx;
1049eda14cbcSMatt Macy 	char buf[256];
1050eda14cbcSMatt Macy 
1051eda14cbcSMatt Macy 	va_start(adx, fmt);
1052eda14cbcSMatt Macy 	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
1053eda14cbcSMatt Macy 	va_end(adx);
1054eda14cbcSMatt Macy 
1055e639e0d2SMartin Matuska 	zfs_dbgmsg("bad blkptr at %px: "
1056e639e0d2SMartin Matuska 	    "DVA[0]=%#llx/%#llx "
1057e639e0d2SMartin Matuska 	    "DVA[1]=%#llx/%#llx "
1058e639e0d2SMartin Matuska 	    "DVA[2]=%#llx/%#llx "
1059e639e0d2SMartin Matuska 	    "prop=%#llx "
1060e639e0d2SMartin Matuska 	    "pad=%#llx,%#llx "
1061e639e0d2SMartin Matuska 	    "phys_birth=%#llx "
1062e639e0d2SMartin Matuska 	    "birth=%#llx "
1063e639e0d2SMartin Matuska 	    "fill=%#llx "
1064e639e0d2SMartin Matuska 	    "cksum=%#llx/%#llx/%#llx/%#llx",
1065e639e0d2SMartin Matuska 	    bp,
1066e639e0d2SMartin Matuska 	    (long long)bp->blk_dva[0].dva_word[0],
1067e639e0d2SMartin Matuska 	    (long long)bp->blk_dva[0].dva_word[1],
1068e639e0d2SMartin Matuska 	    (long long)bp->blk_dva[1].dva_word[0],
1069e639e0d2SMartin Matuska 	    (long long)bp->blk_dva[1].dva_word[1],
1070e639e0d2SMartin Matuska 	    (long long)bp->blk_dva[2].dva_word[0],
1071e639e0d2SMartin Matuska 	    (long long)bp->blk_dva[2].dva_word[1],
1072e639e0d2SMartin Matuska 	    (long long)bp->blk_prop,
1073e639e0d2SMartin Matuska 	    (long long)bp->blk_pad[0],
1074e639e0d2SMartin Matuska 	    (long long)bp->blk_pad[1],
1075783d3ff6SMartin Matuska 	    (long long)BP_GET_PHYSICAL_BIRTH(bp),
1076783d3ff6SMartin Matuska 	    (long long)BP_GET_LOGICAL_BIRTH(bp),
1077e639e0d2SMartin Matuska 	    (long long)bp->blk_fill,
1078e639e0d2SMartin Matuska 	    (long long)bp->blk_cksum.zc_word[0],
1079e639e0d2SMartin Matuska 	    (long long)bp->blk_cksum.zc_word[1],
1080e639e0d2SMartin Matuska 	    (long long)bp->blk_cksum.zc_word[2],
1081e639e0d2SMartin Matuska 	    (long long)bp->blk_cksum.zc_word[3]);
1082eda14cbcSMatt Macy 	switch (blk_verify) {
1083eda14cbcSMatt Macy 	case BLK_VERIFY_HALT:
1084eda14cbcSMatt Macy 		zfs_panic_recover("%s: %s", spa_name(spa), buf);
1085eda14cbcSMatt Macy 		break;
1086eda14cbcSMatt Macy 	case BLK_VERIFY_LOG:
1087eda14cbcSMatt Macy 		zfs_dbgmsg("%s: %s", spa_name(spa), buf);
1088eda14cbcSMatt Macy 		break;
1089eda14cbcSMatt Macy 	case BLK_VERIFY_ONLY:
1090eda14cbcSMatt Macy 		break;
1091eda14cbcSMatt Macy 	}
1092eda14cbcSMatt Macy 
1093eda14cbcSMatt Macy 	return (1);
1094eda14cbcSMatt Macy }
1095eda14cbcSMatt Macy 
1096eda14cbcSMatt Macy /*
1097eda14cbcSMatt Macy  * Verify the block pointer fields contain reasonable values.  This means
1098eda14cbcSMatt Macy  * it only contains known object types, checksum/compression identifiers,
1099eda14cbcSMatt Macy  * block sizes within the maximum allowed limits, valid DVAs, etc.
1100eda14cbcSMatt Macy  *
1101eda14cbcSMatt Macy  * If everything checks out B_TRUE is returned.  The zfs_blkptr_verify
1102eda14cbcSMatt Macy  * argument controls the behavior when an invalid field is detected.
1103eda14cbcSMatt Macy  *
1104e639e0d2SMartin Matuska  * Values for blk_verify_flag:
1105e639e0d2SMartin Matuska  *   BLK_VERIFY_ONLY: evaluate the block
1106e639e0d2SMartin Matuska  *   BLK_VERIFY_LOG: evaluate the block and log problems
1107e639e0d2SMartin Matuska  *   BLK_VERIFY_HALT: call zfs_panic_recover on error
1108e639e0d2SMartin Matuska  *
1109e639e0d2SMartin Matuska  * Values for blk_config_flag:
1110e639e0d2SMartin Matuska  *   BLK_CONFIG_HELD: caller holds SCL_VDEV for writer
1111e639e0d2SMartin Matuska  *   BLK_CONFIG_NEEDED: caller holds no config lock, SCL_VDEV will be
1112e639e0d2SMartin Matuska  *   obtained for reader
1113e639e0d2SMartin Matuska  *   BLK_CONFIG_SKIP: skip checks which require SCL_VDEV, for better
1114e639e0d2SMartin Matuska  *   performance
1115eda14cbcSMatt Macy  */
1116eda14cbcSMatt Macy boolean_t
1117e639e0d2SMartin Matuska zfs_blkptr_verify(spa_t *spa, const blkptr_t *bp,
1118e639e0d2SMartin Matuska     enum blk_config_flag blk_config, enum blk_verify_flag blk_verify)
1119eda14cbcSMatt Macy {
1120eda14cbcSMatt Macy 	int errors = 0;
1121eda14cbcSMatt Macy 
1122ce4dcb97SMartin Matuska 	if (unlikely(!DMU_OT_IS_VALID(BP_GET_TYPE(bp)))) {
1123eda14cbcSMatt Macy 		errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1124e639e0d2SMartin Matuska 		    "blkptr at %px has invalid TYPE %llu",
1125eda14cbcSMatt Macy 		    bp, (longlong_t)BP_GET_TYPE(bp));
1126eda14cbcSMatt Macy 	}
1127ce4dcb97SMartin Matuska 	if (unlikely(BP_GET_COMPRESS(bp) >= ZIO_COMPRESS_FUNCTIONS)) {
1128eda14cbcSMatt Macy 		errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1129e639e0d2SMartin Matuska 		    "blkptr at %px has invalid COMPRESS %llu",
1130eda14cbcSMatt Macy 		    bp, (longlong_t)BP_GET_COMPRESS(bp));
1131eda14cbcSMatt Macy 	}
1132ce4dcb97SMartin Matuska 	if (unlikely(BP_GET_LSIZE(bp) > SPA_MAXBLOCKSIZE)) {
1133eda14cbcSMatt Macy 		errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1134e639e0d2SMartin Matuska 		    "blkptr at %px has invalid LSIZE %llu",
1135eda14cbcSMatt Macy 		    bp, (longlong_t)BP_GET_LSIZE(bp));
1136eda14cbcSMatt Macy 	}
1137eda14cbcSMatt Macy 	if (BP_IS_EMBEDDED(bp)) {
1138ce4dcb97SMartin Matuska 		if (unlikely(BPE_GET_ETYPE(bp) >= NUM_BP_EMBEDDED_TYPES)) {
1139eda14cbcSMatt Macy 			errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1140e639e0d2SMartin Matuska 			    "blkptr at %px has invalid ETYPE %llu",
1141eda14cbcSMatt Macy 			    bp, (longlong_t)BPE_GET_ETYPE(bp));
1142eda14cbcSMatt Macy 		}
1143ce4dcb97SMartin Matuska 		if (unlikely(BPE_GET_PSIZE(bp) > BPE_PAYLOAD_SIZE)) {
1144ce4dcb97SMartin Matuska 			errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1145ce4dcb97SMartin Matuska 			    "blkptr at %px has invalid PSIZE %llu",
1146ce4dcb97SMartin Matuska 			    bp, (longlong_t)BPE_GET_PSIZE(bp));
1147ce4dcb97SMartin Matuska 		}
1148ce4dcb97SMartin Matuska 		return (errors == 0);
1149ce4dcb97SMartin Matuska 	}
1150ce4dcb97SMartin Matuska 	if (unlikely(BP_GET_CHECKSUM(bp) >= ZIO_CHECKSUM_FUNCTIONS)) {
1151ce4dcb97SMartin Matuska 		errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1152ce4dcb97SMartin Matuska 		    "blkptr at %px has invalid CHECKSUM %llu",
1153ce4dcb97SMartin Matuska 		    bp, (longlong_t)BP_GET_CHECKSUM(bp));
1154ce4dcb97SMartin Matuska 	}
1155ce4dcb97SMartin Matuska 	if (unlikely(BP_GET_PSIZE(bp) > SPA_MAXBLOCKSIZE)) {
1156ce4dcb97SMartin Matuska 		errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1157ce4dcb97SMartin Matuska 		    "blkptr at %px has invalid PSIZE %llu",
1158ce4dcb97SMartin Matuska 		    bp, (longlong_t)BP_GET_PSIZE(bp));
1159eda14cbcSMatt Macy 	}
1160eda14cbcSMatt Macy 
1161eda14cbcSMatt Macy 	/*
1162eda14cbcSMatt Macy 	 * Do not verify individual DVAs if the config is not trusted. This
1163eda14cbcSMatt Macy 	 * will be done once the zio is executed in vdev_mirror_map_alloc.
1164eda14cbcSMatt Macy 	 */
1165ce4dcb97SMartin Matuska 	if (unlikely(!spa->spa_trust_config))
116653b70c86SMartin Matuska 		return (errors == 0);
1167eda14cbcSMatt Macy 
1168e639e0d2SMartin Matuska 	switch (blk_config) {
1169e639e0d2SMartin Matuska 	case BLK_CONFIG_HELD:
1170eda14cbcSMatt Macy 		ASSERT(spa_config_held(spa, SCL_VDEV, RW_WRITER));
1171e639e0d2SMartin Matuska 		break;
1172e639e0d2SMartin Matuska 	case BLK_CONFIG_NEEDED:
1173e639e0d2SMartin Matuska 		spa_config_enter(spa, SCL_VDEV, bp, RW_READER);
1174e639e0d2SMartin Matuska 		break;
1175e639e0d2SMartin Matuska 	case BLK_CONFIG_SKIP:
1176e639e0d2SMartin Matuska 		return (errors == 0);
1177e639e0d2SMartin Matuska 	default:
1178e639e0d2SMartin Matuska 		panic("invalid blk_config %u", blk_config);
1179e639e0d2SMartin Matuska 	}
1180e639e0d2SMartin Matuska 
1181eda14cbcSMatt Macy 	/*
1182eda14cbcSMatt Macy 	 * Pool-specific checks.
1183eda14cbcSMatt Macy 	 *
1184783d3ff6SMartin Matuska 	 * Note: it would be nice to verify that the logical birth
1185783d3ff6SMartin Matuska 	 * and physical birth are not too large.  However,
1186783d3ff6SMartin Matuska 	 * spa_freeze() allows the birth time of log blocks (and
1187783d3ff6SMartin Matuska 	 * dmu_sync()-ed blocks that are in the log) to be arbitrarily
1188783d3ff6SMartin Matuska 	 * large.
1189eda14cbcSMatt Macy 	 */
1190eda14cbcSMatt Macy 	for (int i = 0; i < BP_GET_NDVAS(bp); i++) {
11916db169e9SMartin Matuska 		const dva_t *dva = &bp->blk_dva[i];
11926db169e9SMartin Matuska 		uint64_t vdevid = DVA_GET_VDEV(dva);
1193eda14cbcSMatt Macy 
1194ce4dcb97SMartin Matuska 		if (unlikely(vdevid >= spa->spa_root_vdev->vdev_children)) {
1195eda14cbcSMatt Macy 			errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1196e639e0d2SMartin Matuska 			    "blkptr at %px DVA %u has invalid VDEV %llu",
1197eda14cbcSMatt Macy 			    bp, i, (longlong_t)vdevid);
1198eda14cbcSMatt Macy 			continue;
1199eda14cbcSMatt Macy 		}
1200eda14cbcSMatt Macy 		vdev_t *vd = spa->spa_root_vdev->vdev_child[vdevid];
1201ce4dcb97SMartin Matuska 		if (unlikely(vd == NULL)) {
1202eda14cbcSMatt Macy 			errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1203e639e0d2SMartin Matuska 			    "blkptr at %px DVA %u has invalid VDEV %llu",
1204eda14cbcSMatt Macy 			    bp, i, (longlong_t)vdevid);
1205eda14cbcSMatt Macy 			continue;
1206eda14cbcSMatt Macy 		}
1207ce4dcb97SMartin Matuska 		if (unlikely(vd->vdev_ops == &vdev_hole_ops)) {
1208eda14cbcSMatt Macy 			errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1209e639e0d2SMartin Matuska 			    "blkptr at %px DVA %u has hole VDEV %llu",
1210eda14cbcSMatt Macy 			    bp, i, (longlong_t)vdevid);
1211eda14cbcSMatt Macy 			continue;
1212eda14cbcSMatt Macy 		}
1213eda14cbcSMatt Macy 		if (vd->vdev_ops == &vdev_missing_ops) {
1214eda14cbcSMatt Macy 			/*
1215eda14cbcSMatt Macy 			 * "missing" vdevs are valid during import, but we
1216eda14cbcSMatt Macy 			 * don't have their detailed info (e.g. asize), so
1217eda14cbcSMatt Macy 			 * we can't perform any more checks on them.
1218eda14cbcSMatt Macy 			 */
1219eda14cbcSMatt Macy 			continue;
1220eda14cbcSMatt Macy 		}
12216db169e9SMartin Matuska 		uint64_t offset = DVA_GET_OFFSET(dva);
12226db169e9SMartin Matuska 		uint64_t asize = DVA_GET_ASIZE(dva);
12236db169e9SMartin Matuska 		if (DVA_GET_GANG(dva))
12246db169e9SMartin Matuska 			asize = vdev_gang_header_asize(vd);
1225ce4dcb97SMartin Matuska 		if (unlikely(offset + asize > vd->vdev_asize)) {
1226eda14cbcSMatt Macy 			errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1227e639e0d2SMartin Matuska 			    "blkptr at %px DVA %u has invalid OFFSET %llu",
1228eda14cbcSMatt Macy 			    bp, i, (longlong_t)offset);
1229eda14cbcSMatt Macy 		}
1230eda14cbcSMatt Macy 	}
1231e639e0d2SMartin Matuska 	if (blk_config == BLK_CONFIG_NEEDED)
1232eda14cbcSMatt Macy 		spa_config_exit(spa, SCL_VDEV, bp);
1233eda14cbcSMatt Macy 
1234eda14cbcSMatt Macy 	return (errors == 0);
1235eda14cbcSMatt Macy }
1236eda14cbcSMatt Macy 
1237eda14cbcSMatt Macy boolean_t
1238eda14cbcSMatt Macy zfs_dva_valid(spa_t *spa, const dva_t *dva, const blkptr_t *bp)
1239eda14cbcSMatt Macy {
1240e92ffd9bSMartin Matuska 	(void) bp;
1241eda14cbcSMatt Macy 	uint64_t vdevid = DVA_GET_VDEV(dva);
1242eda14cbcSMatt Macy 
1243eda14cbcSMatt Macy 	if (vdevid >= spa->spa_root_vdev->vdev_children)
1244eda14cbcSMatt Macy 		return (B_FALSE);
1245eda14cbcSMatt Macy 
1246eda14cbcSMatt Macy 	vdev_t *vd = spa->spa_root_vdev->vdev_child[vdevid];
1247eda14cbcSMatt Macy 	if (vd == NULL)
1248eda14cbcSMatt Macy 		return (B_FALSE);
1249eda14cbcSMatt Macy 
1250eda14cbcSMatt Macy 	if (vd->vdev_ops == &vdev_hole_ops)
1251eda14cbcSMatt Macy 		return (B_FALSE);
1252eda14cbcSMatt Macy 
1253eda14cbcSMatt Macy 	if (vd->vdev_ops == &vdev_missing_ops) {
1254eda14cbcSMatt Macy 		return (B_FALSE);
1255eda14cbcSMatt Macy 	}
1256eda14cbcSMatt Macy 
1257eda14cbcSMatt Macy 	uint64_t offset = DVA_GET_OFFSET(dva);
1258eda14cbcSMatt Macy 	uint64_t asize = DVA_GET_ASIZE(dva);
1259eda14cbcSMatt Macy 
12606db169e9SMartin Matuska 	if (DVA_GET_GANG(dva))
12616db169e9SMartin Matuska 		asize = vdev_gang_header_asize(vd);
1262eda14cbcSMatt Macy 	if (offset + asize > vd->vdev_asize)
1263eda14cbcSMatt Macy 		return (B_FALSE);
1264eda14cbcSMatt Macy 
1265eda14cbcSMatt Macy 	return (B_TRUE);
1266eda14cbcSMatt Macy }
1267eda14cbcSMatt Macy 
1268eda14cbcSMatt Macy zio_t *
1269eda14cbcSMatt Macy zio_read(zio_t *pio, spa_t *spa, const blkptr_t *bp,
1270eda14cbcSMatt Macy     abd_t *data, uint64_t size, zio_done_func_t *done, void *private,
1271dbd5678dSMartin Matuska     zio_priority_t priority, zio_flag_t flags, const zbookmark_phys_t *zb)
1272eda14cbcSMatt Macy {
1273eda14cbcSMatt Macy 	zio_t *zio;
1274eda14cbcSMatt Macy 
1275783d3ff6SMartin Matuska 	zio = zio_create(pio, spa, BP_GET_BIRTH(bp), bp,
1276eda14cbcSMatt Macy 	    data, size, size, done, private,
1277eda14cbcSMatt Macy 	    ZIO_TYPE_READ, priority, flags, NULL, 0, zb,
1278eda14cbcSMatt Macy 	    ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ?
1279eda14cbcSMatt Macy 	    ZIO_DDT_CHILD_READ_PIPELINE : ZIO_READ_PIPELINE);
1280eda14cbcSMatt Macy 
1281eda14cbcSMatt Macy 	return (zio);
1282eda14cbcSMatt Macy }
1283eda14cbcSMatt Macy 
1284eda14cbcSMatt Macy zio_t *
1285eda14cbcSMatt Macy zio_write(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
1286eda14cbcSMatt Macy     abd_t *data, uint64_t lsize, uint64_t psize, const zio_prop_t *zp,
1287eda14cbcSMatt Macy     zio_done_func_t *ready, zio_done_func_t *children_ready,
1288b7198dcfSMartin Matuska     zio_done_func_t *done, void *private, zio_priority_t priority,
1289b7198dcfSMartin Matuska     zio_flag_t flags, const zbookmark_phys_t *zb)
1290eda14cbcSMatt Macy {
1291eda14cbcSMatt Macy 	zio_t *zio;
12927a7741afSMartin Matuska 	enum zio_stage pipeline = zp->zp_direct_write == B_TRUE ?
12937a7741afSMartin Matuska 	    ZIO_DIRECT_WRITE_PIPELINE : (flags & ZIO_FLAG_DDT_CHILD) ?
12947a7741afSMartin Matuska 	    ZIO_DDT_CHILD_WRITE_PIPELINE : ZIO_WRITE_PIPELINE;
1295eda14cbcSMatt Macy 
1296eda14cbcSMatt Macy 
1297eda14cbcSMatt Macy 	zio = zio_create(pio, spa, txg, bp, data, lsize, psize, done, private,
1298eda14cbcSMatt Macy 	    ZIO_TYPE_WRITE, priority, flags, NULL, 0, zb,
12997a7741afSMartin Matuska 	    ZIO_STAGE_OPEN, pipeline);
1300eda14cbcSMatt Macy 
1301eda14cbcSMatt Macy 	zio->io_ready = ready;
1302eda14cbcSMatt Macy 	zio->io_children_ready = children_ready;
1303eda14cbcSMatt Macy 	zio->io_prop = *zp;
1304eda14cbcSMatt Macy 
1305eda14cbcSMatt Macy 	/*
1306eda14cbcSMatt Macy 	 * Data can be NULL if we are going to call zio_write_override() to
1307eda14cbcSMatt Macy 	 * provide the already-allocated BP.  But we may need the data to
1308eda14cbcSMatt Macy 	 * verify a dedup hit (if requested).  In this case, don't try to
1309eda14cbcSMatt Macy 	 * dedup (just take the already-allocated BP verbatim). Encrypted
1310eda14cbcSMatt Macy 	 * dedup blocks need data as well so we also disable dedup in this
1311eda14cbcSMatt Macy 	 * case.
1312eda14cbcSMatt Macy 	 */
1313eda14cbcSMatt Macy 	if (data == NULL &&
1314eda14cbcSMatt Macy 	    (zio->io_prop.zp_dedup_verify || zio->io_prop.zp_encrypt)) {
1315eda14cbcSMatt Macy 		zio->io_prop.zp_dedup = zio->io_prop.zp_dedup_verify = B_FALSE;
1316eda14cbcSMatt Macy 	}
1317eda14cbcSMatt Macy 
1318eda14cbcSMatt Macy 	return (zio);
1319eda14cbcSMatt Macy }
1320eda14cbcSMatt Macy 
1321eda14cbcSMatt Macy zio_t *
1322eda14cbcSMatt Macy zio_rewrite(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, abd_t *data,
1323eda14cbcSMatt Macy     uint64_t size, zio_done_func_t *done, void *private,
1324dbd5678dSMartin Matuska     zio_priority_t priority, zio_flag_t flags, zbookmark_phys_t *zb)
1325eda14cbcSMatt Macy {
1326eda14cbcSMatt Macy 	zio_t *zio;
1327eda14cbcSMatt Macy 
1328eda14cbcSMatt Macy 	zio = zio_create(pio, spa, txg, bp, data, size, size, done, private,
1329eda14cbcSMatt Macy 	    ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_IO_REWRITE, NULL, 0, zb,
1330eda14cbcSMatt Macy 	    ZIO_STAGE_OPEN, ZIO_REWRITE_PIPELINE);
1331eda14cbcSMatt Macy 
1332eda14cbcSMatt Macy 	return (zio);
1333eda14cbcSMatt Macy }
1334eda14cbcSMatt Macy 
1335eda14cbcSMatt Macy void
13362a58b312SMartin Matuska zio_write_override(zio_t *zio, blkptr_t *bp, int copies, boolean_t nopwrite,
13372a58b312SMartin Matuska     boolean_t brtwrite)
1338eda14cbcSMatt Macy {
1339eda14cbcSMatt Macy 	ASSERT(zio->io_type == ZIO_TYPE_WRITE);
1340eda14cbcSMatt Macy 	ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1341eda14cbcSMatt Macy 	ASSERT(zio->io_stage == ZIO_STAGE_OPEN);
1342eda14cbcSMatt Macy 	ASSERT(zio->io_txg == spa_syncing_txg(zio->io_spa));
13432a58b312SMartin Matuska 	ASSERT(!brtwrite || !nopwrite);
1344eda14cbcSMatt Macy 
1345eda14cbcSMatt Macy 	/*
1346eda14cbcSMatt Macy 	 * We must reset the io_prop to match the values that existed
1347eda14cbcSMatt Macy 	 * when the bp was first written by dmu_sync() keeping in mind
1348eda14cbcSMatt Macy 	 * that nopwrite and dedup are mutually exclusive.
1349eda14cbcSMatt Macy 	 */
1350eda14cbcSMatt Macy 	zio->io_prop.zp_dedup = nopwrite ? B_FALSE : zio->io_prop.zp_dedup;
1351eda14cbcSMatt Macy 	zio->io_prop.zp_nopwrite = nopwrite;
13522a58b312SMartin Matuska 	zio->io_prop.zp_brtwrite = brtwrite;
1353eda14cbcSMatt Macy 	zio->io_prop.zp_copies = copies;
1354eda14cbcSMatt Macy 	zio->io_bp_override = bp;
1355eda14cbcSMatt Macy }
1356eda14cbcSMatt Macy 
1357eda14cbcSMatt Macy void
1358eda14cbcSMatt Macy zio_free(spa_t *spa, uint64_t txg, const blkptr_t *bp)
1359eda14cbcSMatt Macy {
1360eda14cbcSMatt Macy 
1361e639e0d2SMartin Matuska 	(void) zfs_blkptr_verify(spa, bp, BLK_CONFIG_NEEDED, BLK_VERIFY_HALT);
1362eda14cbcSMatt Macy 
1363eda14cbcSMatt Macy 	/*
1364eda14cbcSMatt Macy 	 * The check for EMBEDDED is a performance optimization.  We
1365eda14cbcSMatt Macy 	 * process the free here (by ignoring it) rather than
1366eda14cbcSMatt Macy 	 * putting it on the list and then processing it in zio_free_sync().
1367eda14cbcSMatt Macy 	 */
1368eda14cbcSMatt Macy 	if (BP_IS_EMBEDDED(bp))
1369eda14cbcSMatt Macy 		return;
1370eda14cbcSMatt Macy 
1371eda14cbcSMatt Macy 	/*
1372eda14cbcSMatt Macy 	 * Frees that are for the currently-syncing txg, are not going to be
1373eda14cbcSMatt Macy 	 * deferred, and which will not need to do a read (i.e. not GANG or
1374eda14cbcSMatt Macy 	 * DEDUP), can be processed immediately.  Otherwise, put them on the
1375eda14cbcSMatt Macy 	 * in-memory list for later processing.
1376eda14cbcSMatt Macy 	 *
1377eda14cbcSMatt Macy 	 * Note that we only defer frees after zfs_sync_pass_deferred_free
1378eda14cbcSMatt Macy 	 * when the log space map feature is disabled. [see relevant comment
1379eda14cbcSMatt Macy 	 * in spa_sync_iterate_to_convergence()]
1380eda14cbcSMatt Macy 	 */
1381eda14cbcSMatt Macy 	if (BP_IS_GANG(bp) ||
1382eda14cbcSMatt Macy 	    BP_GET_DEDUP(bp) ||
1383eda14cbcSMatt Macy 	    txg != spa->spa_syncing_txg ||
1384eda14cbcSMatt Macy 	    (spa_sync_pass(spa) >= zfs_sync_pass_deferred_free &&
13852a58b312SMartin Matuska 	    !spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP)) ||
13862a58b312SMartin Matuska 	    brt_maybe_exists(spa, bp)) {
1387dbd5678dSMartin Matuska 		metaslab_check_free(spa, bp);
1388eda14cbcSMatt Macy 		bplist_append(&spa->spa_free_bplist[txg & TXG_MASK], bp);
1389eda14cbcSMatt Macy 	} else {
1390eda14cbcSMatt Macy 		VERIFY3P(zio_free_sync(NULL, spa, txg, bp, 0), ==, NULL);
1391eda14cbcSMatt Macy 	}
1392eda14cbcSMatt Macy }
1393eda14cbcSMatt Macy 
1394eda14cbcSMatt Macy /*
1395eda14cbcSMatt Macy  * To improve performance, this function may return NULL if we were able
1396eda14cbcSMatt Macy  * to do the free immediately.  This avoids the cost of creating a zio
1397eda14cbcSMatt Macy  * (and linking it to the parent, etc).
1398eda14cbcSMatt Macy  */
1399eda14cbcSMatt Macy zio_t *
1400eda14cbcSMatt Macy zio_free_sync(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
1401dbd5678dSMartin Matuska     zio_flag_t flags)
1402eda14cbcSMatt Macy {
1403eda14cbcSMatt Macy 	ASSERT(!BP_IS_HOLE(bp));
1404eda14cbcSMatt Macy 	ASSERT(spa_syncing_txg(spa) == txg);
1405eda14cbcSMatt Macy 
1406eda14cbcSMatt Macy 	if (BP_IS_EMBEDDED(bp))
1407eda14cbcSMatt Macy 		return (NULL);
1408eda14cbcSMatt Macy 
1409eda14cbcSMatt Macy 	metaslab_check_free(spa, bp);
1410eda14cbcSMatt Macy 	arc_freed(spa, bp);
1411eda14cbcSMatt Macy 	dsl_scan_freed(spa, bp);
1412eda14cbcSMatt Macy 
14132a58b312SMartin Matuska 	if (BP_IS_GANG(bp) ||
14142a58b312SMartin Matuska 	    BP_GET_DEDUP(bp) ||
14152a58b312SMartin Matuska 	    brt_maybe_exists(spa, bp)) {
1416eda14cbcSMatt Macy 		/*
14172a58b312SMartin Matuska 		 * GANG, DEDUP and BRT blocks can induce a read (for the gang
14182a58b312SMartin Matuska 		 * block header, the DDT or the BRT), so issue them
14192a58b312SMartin Matuska 		 * asynchronously so that this thread is not tied up.
1420eda14cbcSMatt Macy 		 */
1421eda14cbcSMatt Macy 		enum zio_stage stage =
1422eda14cbcSMatt Macy 		    ZIO_FREE_PIPELINE | ZIO_STAGE_ISSUE_ASYNC;
1423eda14cbcSMatt Macy 
1424eda14cbcSMatt Macy 		return (zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp),
1425eda14cbcSMatt Macy 		    BP_GET_PSIZE(bp), NULL, NULL,
1426eda14cbcSMatt Macy 		    ZIO_TYPE_FREE, ZIO_PRIORITY_NOW,
1427eda14cbcSMatt Macy 		    flags, NULL, 0, NULL, ZIO_STAGE_OPEN, stage));
1428eda14cbcSMatt Macy 	} else {
1429eda14cbcSMatt Macy 		metaslab_free(spa, bp, txg, B_FALSE);
1430eda14cbcSMatt Macy 		return (NULL);
1431eda14cbcSMatt Macy 	}
1432eda14cbcSMatt Macy }
1433eda14cbcSMatt Macy 
1434eda14cbcSMatt Macy zio_t *
1435eda14cbcSMatt Macy zio_claim(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
1436dbd5678dSMartin Matuska     zio_done_func_t *done, void *private, zio_flag_t flags)
1437eda14cbcSMatt Macy {
1438eda14cbcSMatt Macy 	zio_t *zio;
1439eda14cbcSMatt Macy 
1440e639e0d2SMartin Matuska 	(void) zfs_blkptr_verify(spa, bp, (flags & ZIO_FLAG_CONFIG_WRITER) ?
1441e639e0d2SMartin Matuska 	    BLK_CONFIG_HELD : BLK_CONFIG_NEEDED, BLK_VERIFY_HALT);
1442eda14cbcSMatt Macy 
1443eda14cbcSMatt Macy 	if (BP_IS_EMBEDDED(bp))
1444eda14cbcSMatt Macy 		return (zio_null(pio, spa, NULL, NULL, NULL, 0));
1445eda14cbcSMatt Macy 
1446eda14cbcSMatt Macy 	/*
1447eda14cbcSMatt Macy 	 * A claim is an allocation of a specific block.  Claims are needed
1448eda14cbcSMatt Macy 	 * to support immediate writes in the intent log.  The issue is that
1449eda14cbcSMatt Macy 	 * immediate writes contain committed data, but in a txg that was
1450eda14cbcSMatt Macy 	 * *not* committed.  Upon opening the pool after an unclean shutdown,
1451eda14cbcSMatt Macy 	 * the intent log claims all blocks that contain immediate write data
1452eda14cbcSMatt Macy 	 * so that the SPA knows they're in use.
1453eda14cbcSMatt Macy 	 *
1454eda14cbcSMatt Macy 	 * All claims *must* be resolved in the first txg -- before the SPA
1455eda14cbcSMatt Macy 	 * starts allocating blocks -- so that nothing is allocated twice.
1456eda14cbcSMatt Macy 	 * If txg == 0 we just verify that the block is claimable.
1457eda14cbcSMatt Macy 	 */
1458783d3ff6SMartin Matuska 	ASSERT3U(BP_GET_LOGICAL_BIRTH(&spa->spa_uberblock.ub_rootbp), <,
1459eda14cbcSMatt Macy 	    spa_min_claim_txg(spa));
1460eda14cbcSMatt Macy 	ASSERT(txg == spa_min_claim_txg(spa) || txg == 0);
14617877fdebSMatt Macy 	ASSERT(!BP_GET_DEDUP(bp) || !spa_writeable(spa));	/* zdb(8) */
1462eda14cbcSMatt Macy 
1463eda14cbcSMatt Macy 	zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp),
1464eda14cbcSMatt Macy 	    BP_GET_PSIZE(bp), done, private, ZIO_TYPE_CLAIM, ZIO_PRIORITY_NOW,
1465eda14cbcSMatt Macy 	    flags, NULL, 0, NULL, ZIO_STAGE_OPEN, ZIO_CLAIM_PIPELINE);
1466eda14cbcSMatt Macy 	ASSERT0(zio->io_queued_timestamp);
1467eda14cbcSMatt Macy 
1468eda14cbcSMatt Macy 	return (zio);
1469eda14cbcSMatt Macy }
1470eda14cbcSMatt Macy 
1471eda14cbcSMatt Macy zio_t *
1472eda14cbcSMatt Macy zio_trim(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
1473eda14cbcSMatt Macy     zio_done_func_t *done, void *private, zio_priority_t priority,
1474dbd5678dSMartin Matuska     zio_flag_t flags, enum trim_flag trim_flags)
1475eda14cbcSMatt Macy {
1476eda14cbcSMatt Macy 	zio_t *zio;
1477eda14cbcSMatt Macy 
1478eda14cbcSMatt Macy 	ASSERT0(vd->vdev_children);
1479eda14cbcSMatt Macy 	ASSERT0(P2PHASE(offset, 1ULL << vd->vdev_ashift));
1480eda14cbcSMatt Macy 	ASSERT0(P2PHASE(size, 1ULL << vd->vdev_ashift));
1481eda14cbcSMatt Macy 	ASSERT3U(size, !=, 0);
1482eda14cbcSMatt Macy 
1483eda14cbcSMatt Macy 	zio = zio_create(pio, vd->vdev_spa, 0, NULL, NULL, size, size, done,
1484eda14cbcSMatt Macy 	    private, ZIO_TYPE_TRIM, priority, flags | ZIO_FLAG_PHYSICAL,
1485eda14cbcSMatt Macy 	    vd, offset, NULL, ZIO_STAGE_OPEN, ZIO_TRIM_PIPELINE);
1486eda14cbcSMatt Macy 	zio->io_trim_flags = trim_flags;
1487eda14cbcSMatt Macy 
1488eda14cbcSMatt Macy 	return (zio);
1489eda14cbcSMatt Macy }
1490eda14cbcSMatt Macy 
1491eda14cbcSMatt Macy zio_t *
1492eda14cbcSMatt Macy zio_read_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
1493eda14cbcSMatt Macy     abd_t *data, int checksum, zio_done_func_t *done, void *private,
1494dbd5678dSMartin Matuska     zio_priority_t priority, zio_flag_t flags, boolean_t labels)
1495eda14cbcSMatt Macy {
1496eda14cbcSMatt Macy 	zio_t *zio;
1497eda14cbcSMatt Macy 
1498eda14cbcSMatt Macy 	ASSERT(vd->vdev_children == 0);
1499eda14cbcSMatt Macy 	ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE ||
1500eda14cbcSMatt Macy 	    offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
1501eda14cbcSMatt Macy 	ASSERT3U(offset + size, <=, vd->vdev_psize);
1502eda14cbcSMatt Macy 
1503eda14cbcSMatt Macy 	zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, size, done,
1504eda14cbcSMatt Macy 	    private, ZIO_TYPE_READ, priority, flags | ZIO_FLAG_PHYSICAL, vd,
1505eda14cbcSMatt Macy 	    offset, NULL, ZIO_STAGE_OPEN, ZIO_READ_PHYS_PIPELINE);
1506eda14cbcSMatt Macy 
1507eda14cbcSMatt Macy 	zio->io_prop.zp_checksum = checksum;
1508eda14cbcSMatt Macy 
1509eda14cbcSMatt Macy 	return (zio);
1510eda14cbcSMatt Macy }
1511eda14cbcSMatt Macy 
1512eda14cbcSMatt Macy zio_t *
1513eda14cbcSMatt Macy zio_write_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
1514eda14cbcSMatt Macy     abd_t *data, int checksum, zio_done_func_t *done, void *private,
1515dbd5678dSMartin Matuska     zio_priority_t priority, zio_flag_t flags, boolean_t labels)
1516eda14cbcSMatt Macy {
1517eda14cbcSMatt Macy 	zio_t *zio;
1518eda14cbcSMatt Macy 
1519eda14cbcSMatt Macy 	ASSERT(vd->vdev_children == 0);
1520eda14cbcSMatt Macy 	ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE ||
1521eda14cbcSMatt Macy 	    offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
1522eda14cbcSMatt Macy 	ASSERT3U(offset + size, <=, vd->vdev_psize);
1523eda14cbcSMatt Macy 
1524eda14cbcSMatt Macy 	zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, size, done,
1525eda14cbcSMatt Macy 	    private, ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_PHYSICAL, vd,
1526eda14cbcSMatt Macy 	    offset, NULL, ZIO_STAGE_OPEN, ZIO_WRITE_PHYS_PIPELINE);
1527eda14cbcSMatt Macy 
1528eda14cbcSMatt Macy 	zio->io_prop.zp_checksum = checksum;
1529eda14cbcSMatt Macy 
1530eda14cbcSMatt Macy 	if (zio_checksum_table[checksum].ci_flags & ZCHECKSUM_FLAG_EMBEDDED) {
1531eda14cbcSMatt Macy 		/*
1532eda14cbcSMatt Macy 		 * zec checksums are necessarily destructive -- they modify
1533eda14cbcSMatt Macy 		 * the end of the write buffer to hold the verifier/checksum.
1534eda14cbcSMatt Macy 		 * Therefore, we must make a local copy in case the data is
1535eda14cbcSMatt Macy 		 * being written to multiple places in parallel.
1536eda14cbcSMatt Macy 		 */
1537eda14cbcSMatt Macy 		abd_t *wbuf = abd_alloc_sametype(data, size);
1538eda14cbcSMatt Macy 		abd_copy(wbuf, data, size);
1539eda14cbcSMatt Macy 
1540eda14cbcSMatt Macy 		zio_push_transform(zio, wbuf, size, size, NULL);
1541eda14cbcSMatt Macy 	}
1542eda14cbcSMatt Macy 
1543eda14cbcSMatt Macy 	return (zio);
1544eda14cbcSMatt Macy }
1545eda14cbcSMatt Macy 
1546eda14cbcSMatt Macy /*
1547eda14cbcSMatt Macy  * Create a child I/O to do some work for us.
1548eda14cbcSMatt Macy  */
1549eda14cbcSMatt Macy zio_t *
1550eda14cbcSMatt Macy zio_vdev_child_io(zio_t *pio, blkptr_t *bp, vdev_t *vd, uint64_t offset,
1551eda14cbcSMatt Macy     abd_t *data, uint64_t size, int type, zio_priority_t priority,
1552dbd5678dSMartin Matuska     zio_flag_t flags, zio_done_func_t *done, void *private)
1553eda14cbcSMatt Macy {
1554eda14cbcSMatt Macy 	enum zio_stage pipeline = ZIO_VDEV_CHILD_PIPELINE;
1555eda14cbcSMatt Macy 	zio_t *zio;
1556eda14cbcSMatt Macy 
1557eda14cbcSMatt Macy 	/*
1558eda14cbcSMatt Macy 	 * vdev child I/Os do not propagate their error to the parent.
1559eda14cbcSMatt Macy 	 * Therefore, for correct operation the caller *must* check for
1560eda14cbcSMatt Macy 	 * and handle the error in the child i/o's done callback.
1561eda14cbcSMatt Macy 	 * The only exceptions are i/os that we don't care about
1562eda14cbcSMatt Macy 	 * (OPTIONAL or REPAIR).
1563eda14cbcSMatt Macy 	 */
1564eda14cbcSMatt Macy 	ASSERT((flags & ZIO_FLAG_OPTIONAL) || (flags & ZIO_FLAG_IO_REPAIR) ||
1565eda14cbcSMatt Macy 	    done != NULL);
1566eda14cbcSMatt Macy 
1567eda14cbcSMatt Macy 	if (type == ZIO_TYPE_READ && bp != NULL) {
1568eda14cbcSMatt Macy 		/*
1569eda14cbcSMatt Macy 		 * If we have the bp, then the child should perform the
1570eda14cbcSMatt Macy 		 * checksum and the parent need not.  This pushes error
1571eda14cbcSMatt Macy 		 * detection as close to the leaves as possible and
1572eda14cbcSMatt Macy 		 * eliminates redundant checksums in the interior nodes.
1573eda14cbcSMatt Macy 		 */
1574eda14cbcSMatt Macy 		pipeline |= ZIO_STAGE_CHECKSUM_VERIFY;
1575eda14cbcSMatt Macy 		pio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY;
157687bf66d4SMartin Matuska 		/*
157787bf66d4SMartin Matuska 		 * We never allow the mirror VDEV to attempt reading from any
157887bf66d4SMartin Matuska 		 * additional data copies after the first Direct I/O checksum
157987bf66d4SMartin Matuska 		 * verify failure. This is to avoid bad data being written out
158087bf66d4SMartin Matuska 		 * through the mirror during self healing. See comment in
158187bf66d4SMartin Matuska 		 * vdev_mirror_io_done() for more details.
158287bf66d4SMartin Matuska 		 */
158387bf66d4SMartin Matuska 		ASSERT0(pio->io_flags & ZIO_FLAG_DIO_CHKSUM_ERR);
15847a7741afSMartin Matuska 	} else if (type == ZIO_TYPE_WRITE &&
15857a7741afSMartin Matuska 	    pio->io_prop.zp_direct_write == B_TRUE) {
15867a7741afSMartin Matuska 		/*
15877a7741afSMartin Matuska 		 * By default we only will verify checksums for Direct I/O
15887a7741afSMartin Matuska 		 * writes for Linux. FreeBSD is able to place user pages under
15897a7741afSMartin Matuska 		 * write protection before issuing them to the ZIO pipeline.
15907a7741afSMartin Matuska 		 *
15917a7741afSMartin Matuska 		 * Checksum validation errors will only be reported through
15927a7741afSMartin Matuska 		 * the top-level VDEV, which is set by this child ZIO.
15937a7741afSMartin Matuska 		 */
15947a7741afSMartin Matuska 		ASSERT3P(bp, !=, NULL);
15957a7741afSMartin Matuska 		ASSERT3U(pio->io_child_type, ==, ZIO_CHILD_LOGICAL);
15967a7741afSMartin Matuska 		pipeline |= ZIO_STAGE_DIO_CHECKSUM_VERIFY;
1597eda14cbcSMatt Macy 	}
1598eda14cbcSMatt Macy 
1599eda14cbcSMatt Macy 	if (vd->vdev_ops->vdev_op_leaf) {
1600eda14cbcSMatt Macy 		ASSERT0(vd->vdev_children);
1601eda14cbcSMatt Macy 		offset += VDEV_LABEL_START_SIZE;
1602eda14cbcSMatt Macy 	}
1603eda14cbcSMatt Macy 
1604eda14cbcSMatt Macy 	flags |= ZIO_VDEV_CHILD_FLAGS(pio);
1605eda14cbcSMatt Macy 
1606eda14cbcSMatt Macy 	/*
1607eda14cbcSMatt Macy 	 * If we've decided to do a repair, the write is not speculative --
1608eda14cbcSMatt Macy 	 * even if the original read was.
1609eda14cbcSMatt Macy 	 */
1610eda14cbcSMatt Macy 	if (flags & ZIO_FLAG_IO_REPAIR)
1611eda14cbcSMatt Macy 		flags &= ~ZIO_FLAG_SPECULATIVE;
1612eda14cbcSMatt Macy 
1613eda14cbcSMatt Macy 	/*
1614eda14cbcSMatt Macy 	 * If we're creating a child I/O that is not associated with a
1615eda14cbcSMatt Macy 	 * top-level vdev, then the child zio is not an allocating I/O.
1616eda14cbcSMatt Macy 	 * If this is a retried I/O then we ignore it since we will
1617eda14cbcSMatt Macy 	 * have already processed the original allocating I/O.
1618eda14cbcSMatt Macy 	 */
1619eda14cbcSMatt Macy 	if (flags & ZIO_FLAG_IO_ALLOCATING &&
1620eda14cbcSMatt Macy 	    (vd != vd->vdev_top || (flags & ZIO_FLAG_IO_RETRY))) {
1621eda14cbcSMatt Macy 		ASSERT(pio->io_metaslab_class != NULL);
1622eda14cbcSMatt Macy 		ASSERT(pio->io_metaslab_class->mc_alloc_throttle_enabled);
1623eda14cbcSMatt Macy 		ASSERT(type == ZIO_TYPE_WRITE);
1624eda14cbcSMatt Macy 		ASSERT(priority == ZIO_PRIORITY_ASYNC_WRITE);
1625eda14cbcSMatt Macy 		ASSERT(!(flags & ZIO_FLAG_IO_REPAIR));
1626eda14cbcSMatt Macy 		ASSERT(!(pio->io_flags & ZIO_FLAG_IO_REWRITE) ||
1627eda14cbcSMatt Macy 		    pio->io_child_type == ZIO_CHILD_GANG);
1628eda14cbcSMatt Macy 
1629eda14cbcSMatt Macy 		flags &= ~ZIO_FLAG_IO_ALLOCATING;
1630eda14cbcSMatt Macy 	}
1631eda14cbcSMatt Macy 
1632eda14cbcSMatt Macy 	zio = zio_create(pio, pio->io_spa, pio->io_txg, bp, data, size, size,
1633eda14cbcSMatt Macy 	    done, private, type, priority, flags, vd, offset, &pio->io_bookmark,
1634eda14cbcSMatt Macy 	    ZIO_STAGE_VDEV_IO_START >> 1, pipeline);
1635eda14cbcSMatt Macy 	ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_VDEV);
1636eda14cbcSMatt Macy 
1637eda14cbcSMatt Macy 	return (zio);
1638eda14cbcSMatt Macy }
1639eda14cbcSMatt Macy 
1640eda14cbcSMatt Macy zio_t *
1641eda14cbcSMatt Macy zio_vdev_delegated_io(vdev_t *vd, uint64_t offset, abd_t *data, uint64_t size,
1642dbd5678dSMartin Matuska     zio_type_t type, zio_priority_t priority, zio_flag_t flags,
1643eda14cbcSMatt Macy     zio_done_func_t *done, void *private)
1644eda14cbcSMatt Macy {
1645eda14cbcSMatt Macy 	zio_t *zio;
1646eda14cbcSMatt Macy 
1647eda14cbcSMatt Macy 	ASSERT(vd->vdev_ops->vdev_op_leaf);
1648eda14cbcSMatt Macy 
1649eda14cbcSMatt Macy 	zio = zio_create(NULL, vd->vdev_spa, 0, NULL,
1650eda14cbcSMatt Macy 	    data, size, size, done, private, type, priority,
1651eda14cbcSMatt Macy 	    flags | ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY | ZIO_FLAG_DELEGATED,
1652eda14cbcSMatt Macy 	    vd, offset, NULL,
1653eda14cbcSMatt Macy 	    ZIO_STAGE_VDEV_IO_START >> 1, ZIO_VDEV_CHILD_PIPELINE);
1654eda14cbcSMatt Macy 
1655eda14cbcSMatt Macy 	return (zio);
1656eda14cbcSMatt Macy }
1657eda14cbcSMatt Macy 
16581719886fSMartin Matuska 
16591719886fSMartin Matuska /*
16601719886fSMartin Matuska  * Send a flush command to the given vdev. Unlike most zio creation functions,
16611719886fSMartin Matuska  * the flush zios are issued immediately. You can wait on pio to pause until
16621719886fSMartin Matuska  * the flushes complete.
16631719886fSMartin Matuska  */
1664eda14cbcSMatt Macy void
1665a2b560ccSMartin Matuska zio_flush(zio_t *pio, vdev_t *vd)
1666eda14cbcSMatt Macy {
16671719886fSMartin Matuska 	const zio_flag_t flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE |
16681719886fSMartin Matuska 	    ZIO_FLAG_DONT_RETRY;
16691719886fSMartin Matuska 
1670a2b560ccSMartin Matuska 	if (vd->vdev_nowritecache)
1671a2b560ccSMartin Matuska 		return;
16721719886fSMartin Matuska 
1673a2b560ccSMartin Matuska 	if (vd->vdev_children == 0) {
16741719886fSMartin Matuska 		zio_nowait(zio_create(pio, vd->vdev_spa, 0, NULL, NULL, 0, 0,
16751719886fSMartin Matuska 		    NULL, NULL, ZIO_TYPE_FLUSH, ZIO_PRIORITY_NOW, flags, vd, 0,
16761719886fSMartin Matuska 		    NULL, ZIO_STAGE_OPEN, ZIO_FLUSH_PIPELINE));
1677a2b560ccSMartin Matuska 	} else {
1678a2b560ccSMartin Matuska 		for (uint64_t c = 0; c < vd->vdev_children; c++)
1679a2b560ccSMartin Matuska 			zio_flush(pio, vd->vdev_child[c]);
1680a2b560ccSMartin Matuska 	}
1681eda14cbcSMatt Macy }
1682eda14cbcSMatt Macy 
1683eda14cbcSMatt Macy void
1684eda14cbcSMatt Macy zio_shrink(zio_t *zio, uint64_t size)
1685eda14cbcSMatt Macy {
1686eda14cbcSMatt Macy 	ASSERT3P(zio->io_executor, ==, NULL);
1687eda14cbcSMatt Macy 	ASSERT3U(zio->io_orig_size, ==, zio->io_size);
1688eda14cbcSMatt Macy 	ASSERT3U(size, <=, zio->io_size);
1689eda14cbcSMatt Macy 
1690eda14cbcSMatt Macy 	/*
1691eda14cbcSMatt Macy 	 * We don't shrink for raidz because of problems with the
1692eda14cbcSMatt Macy 	 * reconstruction when reading back less than the block size.
1693eda14cbcSMatt Macy 	 * Note, BP_IS_RAIDZ() assumes no compression.
1694eda14cbcSMatt Macy 	 */
1695eda14cbcSMatt Macy 	ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF);
1696eda14cbcSMatt Macy 	if (!BP_IS_RAIDZ(zio->io_bp)) {
1697eda14cbcSMatt Macy 		/* we are not doing a raw write */
1698eda14cbcSMatt Macy 		ASSERT3U(zio->io_size, ==, zio->io_lsize);
1699eda14cbcSMatt Macy 		zio->io_orig_size = zio->io_size = zio->io_lsize = size;
1700eda14cbcSMatt Macy 	}
1701eda14cbcSMatt Macy }
1702eda14cbcSMatt Macy 
1703eda14cbcSMatt Macy /*
1704315ee00fSMartin Matuska  * Round provided allocation size up to a value that can be allocated
1705315ee00fSMartin Matuska  * by at least some vdev(s) in the pool with minimum or no additional
1706315ee00fSMartin Matuska  * padding and without extra space usage on others
1707315ee00fSMartin Matuska  */
1708315ee00fSMartin Matuska static uint64_t
1709315ee00fSMartin Matuska zio_roundup_alloc_size(spa_t *spa, uint64_t size)
1710315ee00fSMartin Matuska {
1711315ee00fSMartin Matuska 	if (size > spa->spa_min_alloc)
1712315ee00fSMartin Matuska 		return (roundup(size, spa->spa_gcd_alloc));
1713315ee00fSMartin Matuska 	return (spa->spa_min_alloc);
1714315ee00fSMartin Matuska }
1715315ee00fSMartin Matuska 
17167a7741afSMartin Matuska size_t
17177a7741afSMartin Matuska zio_get_compression_max_size(enum zio_compress compress, uint64_t gcd_alloc,
17187a7741afSMartin Matuska     uint64_t min_alloc, size_t s_len)
17197a7741afSMartin Matuska {
17207a7741afSMartin Matuska 	size_t d_len;
17217a7741afSMartin Matuska 
17227a7741afSMartin Matuska 	/* minimum 12.5% must be saved (legacy value, may be changed later) */
17237a7741afSMartin Matuska 	d_len = s_len - (s_len >> 3);
17247a7741afSMartin Matuska 
17257a7741afSMartin Matuska 	/* ZLE can't use exactly d_len bytes, it needs more, so ignore it */
17267a7741afSMartin Matuska 	if (compress == ZIO_COMPRESS_ZLE)
17277a7741afSMartin Matuska 		return (d_len);
17287a7741afSMartin Matuska 
17297a7741afSMartin Matuska 	d_len = d_len - d_len % gcd_alloc;
17307a7741afSMartin Matuska 
17317a7741afSMartin Matuska 	if (d_len < min_alloc)
17327a7741afSMartin Matuska 		return (BPE_PAYLOAD_SIZE);
17337a7741afSMartin Matuska 	return (d_len);
17347a7741afSMartin Matuska }
17357a7741afSMartin Matuska 
1736315ee00fSMartin Matuska /*
1737eda14cbcSMatt Macy  * ==========================================================================
1738eda14cbcSMatt Macy  * Prepare to read and write logical blocks
1739eda14cbcSMatt Macy  * ==========================================================================
1740eda14cbcSMatt Macy  */
1741eda14cbcSMatt Macy 
1742eda14cbcSMatt Macy static zio_t *
1743eda14cbcSMatt Macy zio_read_bp_init(zio_t *zio)
1744eda14cbcSMatt Macy {
1745eda14cbcSMatt Macy 	blkptr_t *bp = zio->io_bp;
1746eda14cbcSMatt Macy 	uint64_t psize =
1747eda14cbcSMatt Macy 	    BP_IS_EMBEDDED(bp) ? BPE_GET_PSIZE(bp) : BP_GET_PSIZE(bp);
1748eda14cbcSMatt Macy 
1749eda14cbcSMatt Macy 	ASSERT3P(zio->io_bp, ==, &zio->io_bp_copy);
1750eda14cbcSMatt Macy 
1751eda14cbcSMatt Macy 	if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF &&
1752eda14cbcSMatt Macy 	    zio->io_child_type == ZIO_CHILD_LOGICAL &&
1753eda14cbcSMatt Macy 	    !(zio->io_flags & ZIO_FLAG_RAW_COMPRESS)) {
1754eda14cbcSMatt Macy 		zio_push_transform(zio, abd_alloc_sametype(zio->io_abd, psize),
1755eda14cbcSMatt Macy 		    psize, psize, zio_decompress);
1756eda14cbcSMatt Macy 	}
1757eda14cbcSMatt Macy 
1758eda14cbcSMatt Macy 	if (((BP_IS_PROTECTED(bp) && !(zio->io_flags & ZIO_FLAG_RAW_ENCRYPT)) ||
1759eda14cbcSMatt Macy 	    BP_HAS_INDIRECT_MAC_CKSUM(bp)) &&
1760eda14cbcSMatt Macy 	    zio->io_child_type == ZIO_CHILD_LOGICAL) {
1761eda14cbcSMatt Macy 		zio_push_transform(zio, abd_alloc_sametype(zio->io_abd, psize),
1762eda14cbcSMatt Macy 		    psize, psize, zio_decrypt);
1763eda14cbcSMatt Macy 	}
1764eda14cbcSMatt Macy 
1765eda14cbcSMatt Macy 	if (BP_IS_EMBEDDED(bp) && BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA) {
1766eda14cbcSMatt Macy 		int psize = BPE_GET_PSIZE(bp);
1767eda14cbcSMatt Macy 		void *data = abd_borrow_buf(zio->io_abd, psize);
1768eda14cbcSMatt Macy 
1769eda14cbcSMatt Macy 		zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1770eda14cbcSMatt Macy 		decode_embedded_bp_compressed(bp, data);
1771eda14cbcSMatt Macy 		abd_return_buf_copy(zio->io_abd, data, psize);
1772eda14cbcSMatt Macy 	} else {
1773eda14cbcSMatt Macy 		ASSERT(!BP_IS_EMBEDDED(bp));
1774eda14cbcSMatt Macy 	}
1775eda14cbcSMatt Macy 
1776eda14cbcSMatt Macy 	if (BP_GET_DEDUP(bp) && zio->io_child_type == ZIO_CHILD_LOGICAL)
1777eda14cbcSMatt Macy 		zio->io_pipeline = ZIO_DDT_READ_PIPELINE;
1778eda14cbcSMatt Macy 
1779eda14cbcSMatt Macy 	return (zio);
1780eda14cbcSMatt Macy }
1781eda14cbcSMatt Macy 
1782eda14cbcSMatt Macy static zio_t *
1783eda14cbcSMatt Macy zio_write_bp_init(zio_t *zio)
1784eda14cbcSMatt Macy {
1785eda14cbcSMatt Macy 	if (!IO_IS_ALLOCATING(zio))
1786eda14cbcSMatt Macy 		return (zio);
1787eda14cbcSMatt Macy 
1788eda14cbcSMatt Macy 	ASSERT(zio->io_child_type != ZIO_CHILD_DDT);
1789eda14cbcSMatt Macy 
1790eda14cbcSMatt Macy 	if (zio->io_bp_override) {
1791eda14cbcSMatt Macy 		blkptr_t *bp = zio->io_bp;
1792eda14cbcSMatt Macy 		zio_prop_t *zp = &zio->io_prop;
1793eda14cbcSMatt Macy 
1794783d3ff6SMartin Matuska 		ASSERT(BP_GET_LOGICAL_BIRTH(bp) != zio->io_txg);
1795eda14cbcSMatt Macy 
1796eda14cbcSMatt Macy 		*bp = *zio->io_bp_override;
1797eda14cbcSMatt Macy 		zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1798eda14cbcSMatt Macy 
17992a58b312SMartin Matuska 		if (zp->zp_brtwrite)
18002a58b312SMartin Matuska 			return (zio);
18012a58b312SMartin Matuska 
18022a58b312SMartin Matuska 		ASSERT(!BP_GET_DEDUP(zio->io_bp_override));
18032a58b312SMartin Matuska 
1804eda14cbcSMatt Macy 		if (BP_IS_EMBEDDED(bp))
1805eda14cbcSMatt Macy 			return (zio);
1806eda14cbcSMatt Macy 
1807eda14cbcSMatt Macy 		/*
1808eda14cbcSMatt Macy 		 * If we've been overridden and nopwrite is set then
1809eda14cbcSMatt Macy 		 * set the flag accordingly to indicate that a nopwrite
1810eda14cbcSMatt Macy 		 * has already occurred.
1811eda14cbcSMatt Macy 		 */
1812eda14cbcSMatt Macy 		if (!BP_IS_HOLE(bp) && zp->zp_nopwrite) {
1813eda14cbcSMatt Macy 			ASSERT(!zp->zp_dedup);
1814eda14cbcSMatt Macy 			ASSERT3U(BP_GET_CHECKSUM(bp), ==, zp->zp_checksum);
1815eda14cbcSMatt Macy 			zio->io_flags |= ZIO_FLAG_NOPWRITE;
1816eda14cbcSMatt Macy 			return (zio);
1817eda14cbcSMatt Macy 		}
1818eda14cbcSMatt Macy 
1819eda14cbcSMatt Macy 		ASSERT(!zp->zp_nopwrite);
1820eda14cbcSMatt Macy 
1821eda14cbcSMatt Macy 		if (BP_IS_HOLE(bp) || !zp->zp_dedup)
1822eda14cbcSMatt Macy 			return (zio);
1823eda14cbcSMatt Macy 
1824eda14cbcSMatt Macy 		ASSERT((zio_checksum_table[zp->zp_checksum].ci_flags &
1825eda14cbcSMatt Macy 		    ZCHECKSUM_FLAG_DEDUP) || zp->zp_dedup_verify);
1826eda14cbcSMatt Macy 
1827eda14cbcSMatt Macy 		if (BP_GET_CHECKSUM(bp) == zp->zp_checksum &&
1828eda14cbcSMatt Macy 		    !zp->zp_encrypt) {
1829eda14cbcSMatt Macy 			BP_SET_DEDUP(bp, 1);
1830eda14cbcSMatt Macy 			zio->io_pipeline |= ZIO_STAGE_DDT_WRITE;
1831eda14cbcSMatt Macy 			return (zio);
1832eda14cbcSMatt Macy 		}
1833eda14cbcSMatt Macy 
1834eda14cbcSMatt Macy 		/*
1835eda14cbcSMatt Macy 		 * We were unable to handle this as an override bp, treat
1836eda14cbcSMatt Macy 		 * it as a regular write I/O.
1837eda14cbcSMatt Macy 		 */
1838eda14cbcSMatt Macy 		zio->io_bp_override = NULL;
1839eda14cbcSMatt Macy 		*bp = zio->io_bp_orig;
1840eda14cbcSMatt Macy 		zio->io_pipeline = zio->io_orig_pipeline;
1841eda14cbcSMatt Macy 	}
1842eda14cbcSMatt Macy 
1843eda14cbcSMatt Macy 	return (zio);
1844eda14cbcSMatt Macy }
1845eda14cbcSMatt Macy 
1846eda14cbcSMatt Macy static zio_t *
1847eda14cbcSMatt Macy zio_write_compress(zio_t *zio)
1848eda14cbcSMatt Macy {
1849eda14cbcSMatt Macy 	spa_t *spa = zio->io_spa;
1850eda14cbcSMatt Macy 	zio_prop_t *zp = &zio->io_prop;
1851eda14cbcSMatt Macy 	enum zio_compress compress = zp->zp_compress;
1852eda14cbcSMatt Macy 	blkptr_t *bp = zio->io_bp;
1853eda14cbcSMatt Macy 	uint64_t lsize = zio->io_lsize;
1854eda14cbcSMatt Macy 	uint64_t psize = zio->io_size;
1855be181ee2SMartin Matuska 	uint32_t pass = 1;
1856eda14cbcSMatt Macy 
1857eda14cbcSMatt Macy 	/*
1858eda14cbcSMatt Macy 	 * If our children haven't all reached the ready stage,
1859eda14cbcSMatt Macy 	 * wait for them and then repeat this pipeline stage.
1860eda14cbcSMatt Macy 	 */
1861eda14cbcSMatt Macy 	if (zio_wait_for_children(zio, ZIO_CHILD_LOGICAL_BIT |
1862eda14cbcSMatt Macy 	    ZIO_CHILD_GANG_BIT, ZIO_WAIT_READY)) {
1863eda14cbcSMatt Macy 		return (NULL);
1864eda14cbcSMatt Macy 	}
1865eda14cbcSMatt Macy 
1866eda14cbcSMatt Macy 	if (!IO_IS_ALLOCATING(zio))
1867eda14cbcSMatt Macy 		return (zio);
1868eda14cbcSMatt Macy 
1869eda14cbcSMatt Macy 	if (zio->io_children_ready != NULL) {
1870eda14cbcSMatt Macy 		/*
1871eda14cbcSMatt Macy 		 * Now that all our children are ready, run the callback
1872eda14cbcSMatt Macy 		 * associated with this zio in case it wants to modify the
1873eda14cbcSMatt Macy 		 * data to be written.
1874eda14cbcSMatt Macy 		 */
1875eda14cbcSMatt Macy 		ASSERT3U(zp->zp_level, >, 0);
1876eda14cbcSMatt Macy 		zio->io_children_ready(zio);
1877eda14cbcSMatt Macy 	}
1878eda14cbcSMatt Macy 
1879eda14cbcSMatt Macy 	ASSERT(zio->io_child_type != ZIO_CHILD_DDT);
1880eda14cbcSMatt Macy 	ASSERT(zio->io_bp_override == NULL);
1881eda14cbcSMatt Macy 
1882783d3ff6SMartin Matuska 	if (!BP_IS_HOLE(bp) && BP_GET_LOGICAL_BIRTH(bp) == zio->io_txg) {
1883eda14cbcSMatt Macy 		/*
1884eda14cbcSMatt Macy 		 * We're rewriting an existing block, which means we're
1885eda14cbcSMatt Macy 		 * working on behalf of spa_sync().  For spa_sync() to
1886eda14cbcSMatt Macy 		 * converge, it must eventually be the case that we don't
1887eda14cbcSMatt Macy 		 * have to allocate new blocks.  But compression changes
1888eda14cbcSMatt Macy 		 * the blocksize, which forces a reallocate, and makes
1889eda14cbcSMatt Macy 		 * convergence take longer.  Therefore, after the first
1890eda14cbcSMatt Macy 		 * few passes, stop compressing to ensure convergence.
1891eda14cbcSMatt Macy 		 */
1892eda14cbcSMatt Macy 		pass = spa_sync_pass(spa);
1893eda14cbcSMatt Macy 
1894eda14cbcSMatt Macy 		ASSERT(zio->io_txg == spa_syncing_txg(spa));
1895eda14cbcSMatt Macy 		ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1896eda14cbcSMatt Macy 		ASSERT(!BP_GET_DEDUP(bp));
1897eda14cbcSMatt Macy 
1898eda14cbcSMatt Macy 		if (pass >= zfs_sync_pass_dont_compress)
1899eda14cbcSMatt Macy 			compress = ZIO_COMPRESS_OFF;
1900eda14cbcSMatt Macy 
1901eda14cbcSMatt Macy 		/* Make sure someone doesn't change their mind on overwrites */
1902315ee00fSMartin Matuska 		ASSERT(BP_IS_EMBEDDED(bp) || BP_IS_GANG(bp) ||
1903315ee00fSMartin Matuska 		    MIN(zp->zp_copies, spa_max_replication(spa))
1904315ee00fSMartin Matuska 		    == BP_GET_NDVAS(bp));
1905eda14cbcSMatt Macy 	}
1906eda14cbcSMatt Macy 
1907eda14cbcSMatt Macy 	/* If it's a compressed write that is not raw, compress the buffer. */
1908eda14cbcSMatt Macy 	if (compress != ZIO_COMPRESS_OFF &&
1909eda14cbcSMatt Macy 	    !(zio->io_flags & ZIO_FLAG_RAW_COMPRESS)) {
1910e2df9bb4SMartin Matuska 		abd_t *cabd = NULL;
1911ce4dcb97SMartin Matuska 		if (abd_cmp_zero(zio->io_abd, lsize) == 0)
1912ce4dcb97SMartin Matuska 			psize = 0;
1913ce4dcb97SMartin Matuska 		else if (compress == ZIO_COMPRESS_EMPTY)
1914ce4dcb97SMartin Matuska 			psize = lsize;
1915ce4dcb97SMartin Matuska 		else
1916e2df9bb4SMartin Matuska 			psize = zio_compress_data(compress, zio->io_abd, &cabd,
19177a7741afSMartin Matuska 			    lsize,
19187a7741afSMartin Matuska 			    zio_get_compression_max_size(compress,
19197a7741afSMartin Matuska 			    spa->spa_gcd_alloc, spa->spa_min_alloc, lsize),
19207a7741afSMartin Matuska 			    zp->zp_complevel);
19212a58b312SMartin Matuska 		if (psize == 0) {
1922eda14cbcSMatt Macy 			compress = ZIO_COMPRESS_OFF;
19232a58b312SMartin Matuska 		} else if (psize >= lsize) {
19242a58b312SMartin Matuska 			compress = ZIO_COMPRESS_OFF;
1925e2df9bb4SMartin Matuska 			if (cabd != NULL)
1926e2df9bb4SMartin Matuska 				abd_free(cabd);
1927eda14cbcSMatt Macy 		} else if (!zp->zp_dedup && !zp->zp_encrypt &&
1928eda14cbcSMatt Macy 		    psize <= BPE_PAYLOAD_SIZE &&
1929eda14cbcSMatt Macy 		    zp->zp_level == 0 && !DMU_OT_HAS_FILL(zp->zp_type) &&
1930eda14cbcSMatt Macy 		    spa_feature_is_enabled(spa, SPA_FEATURE_EMBEDDED_DATA)) {
1931e2df9bb4SMartin Matuska 			void *cbuf = abd_borrow_buf_copy(cabd, lsize);
1932eda14cbcSMatt Macy 			encode_embedded_bp_compressed(bp,
1933eda14cbcSMatt Macy 			    cbuf, compress, lsize, psize);
1934eda14cbcSMatt Macy 			BPE_SET_ETYPE(bp, BP_EMBEDDED_TYPE_DATA);
1935eda14cbcSMatt Macy 			BP_SET_TYPE(bp, zio->io_prop.zp_type);
1936eda14cbcSMatt Macy 			BP_SET_LEVEL(bp, zio->io_prop.zp_level);
1937e2df9bb4SMartin Matuska 			abd_return_buf(cabd, cbuf, lsize);
1938e2df9bb4SMartin Matuska 			abd_free(cabd);
1939783d3ff6SMartin Matuska 			BP_SET_LOGICAL_BIRTH(bp, zio->io_txg);
1940eda14cbcSMatt Macy 			zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1941eda14cbcSMatt Macy 			ASSERT(spa_feature_is_active(spa,
1942eda14cbcSMatt Macy 			    SPA_FEATURE_EMBEDDED_DATA));
1943eda14cbcSMatt Macy 			return (zio);
1944eda14cbcSMatt Macy 		} else {
1945eda14cbcSMatt Macy 			/*
19467877fdebSMatt Macy 			 * Round compressed size up to the minimum allocation
19477877fdebSMatt Macy 			 * size of the smallest-ashift device, and zero the
19487877fdebSMatt Macy 			 * tail. This ensures that the compressed size of the
19497877fdebSMatt Macy 			 * BP (and thus compressratio property) are correct,
1950eda14cbcSMatt Macy 			 * in that we charge for the padding used to fill out
1951eda14cbcSMatt Macy 			 * the last sector.
1952eda14cbcSMatt Macy 			 */
1953315ee00fSMartin Matuska 			size_t rounded = (size_t)zio_roundup_alloc_size(spa,
1954315ee00fSMartin Matuska 			    psize);
1955eda14cbcSMatt Macy 			if (rounded >= lsize) {
1956eda14cbcSMatt Macy 				compress = ZIO_COMPRESS_OFF;
1957e2df9bb4SMartin Matuska 				abd_free(cabd);
1958eda14cbcSMatt Macy 				psize = lsize;
1959eda14cbcSMatt Macy 			} else {
1960e2df9bb4SMartin Matuska 				abd_zero_off(cabd, psize, rounded - psize);
1961eda14cbcSMatt Macy 				psize = rounded;
1962e2df9bb4SMartin Matuska 				zio_push_transform(zio, cabd,
1963eda14cbcSMatt Macy 				    psize, lsize, NULL);
1964eda14cbcSMatt Macy 			}
1965eda14cbcSMatt Macy 		}
1966eda14cbcSMatt Macy 
1967eda14cbcSMatt Macy 		/*
1968eda14cbcSMatt Macy 		 * We were unable to handle this as an override bp, treat
1969eda14cbcSMatt Macy 		 * it as a regular write I/O.
1970eda14cbcSMatt Macy 		 */
1971eda14cbcSMatt Macy 		zio->io_bp_override = NULL;
1972eda14cbcSMatt Macy 		*bp = zio->io_bp_orig;
1973eda14cbcSMatt Macy 		zio->io_pipeline = zio->io_orig_pipeline;
1974eda14cbcSMatt Macy 
1975eda14cbcSMatt Macy 	} else if ((zio->io_flags & ZIO_FLAG_RAW_ENCRYPT) != 0 &&
1976eda14cbcSMatt Macy 	    zp->zp_type == DMU_OT_DNODE) {
1977eda14cbcSMatt Macy 		/*
1978eda14cbcSMatt Macy 		 * The DMU actually relies on the zio layer's compression
1979eda14cbcSMatt Macy 		 * to free metadnode blocks that have had all contained
1980eda14cbcSMatt Macy 		 * dnodes freed. As a result, even when doing a raw
1981eda14cbcSMatt Macy 		 * receive, we must check whether the block can be compressed
1982eda14cbcSMatt Macy 		 * to a hole.
1983eda14cbcSMatt Macy 		 */
1984ce4dcb97SMartin Matuska 		if (abd_cmp_zero(zio->io_abd, lsize) == 0) {
1985ce4dcb97SMartin Matuska 			psize = 0;
1986eda14cbcSMatt Macy 			compress = ZIO_COMPRESS_OFF;
1987ce4dcb97SMartin Matuska 		} else {
1988ce4dcb97SMartin Matuska 			psize = lsize;
1989ce4dcb97SMartin Matuska 		}
1990c03c5b1cSMartin Matuska 	} else if (zio->io_flags & ZIO_FLAG_RAW_COMPRESS &&
1991c03c5b1cSMartin Matuska 	    !(zio->io_flags & ZIO_FLAG_RAW_ENCRYPT)) {
1992c03c5b1cSMartin Matuska 		/*
1993c03c5b1cSMartin Matuska 		 * If we are raw receiving an encrypted dataset we should not
1994c03c5b1cSMartin Matuska 		 * take this codepath because it will change the on-disk block
1995c03c5b1cSMartin Matuska 		 * and decryption will fail.
1996c03c5b1cSMartin Matuska 		 */
1997315ee00fSMartin Matuska 		size_t rounded = MIN((size_t)zio_roundup_alloc_size(spa, psize),
1998315ee00fSMartin Matuska 		    lsize);
199953b70c86SMartin Matuska 
200053b70c86SMartin Matuska 		if (rounded != psize) {
200153b70c86SMartin Matuska 			abd_t *cdata = abd_alloc_linear(rounded, B_TRUE);
200253b70c86SMartin Matuska 			abd_zero_off(cdata, psize, rounded - psize);
200353b70c86SMartin Matuska 			abd_copy_off(cdata, zio->io_abd, 0, 0, psize);
200453b70c86SMartin Matuska 			psize = rounded;
200553b70c86SMartin Matuska 			zio_push_transform(zio, cdata,
200653b70c86SMartin Matuska 			    psize, rounded, NULL);
200753b70c86SMartin Matuska 		}
2008eda14cbcSMatt Macy 	} else {
2009eda14cbcSMatt Macy 		ASSERT3U(psize, !=, 0);
2010eda14cbcSMatt Macy 	}
2011eda14cbcSMatt Macy 
2012eda14cbcSMatt Macy 	/*
2013eda14cbcSMatt Macy 	 * The final pass of spa_sync() must be all rewrites, but the first
2014eda14cbcSMatt Macy 	 * few passes offer a trade-off: allocating blocks defers convergence,
2015eda14cbcSMatt Macy 	 * but newly allocated blocks are sequential, so they can be written
2016eda14cbcSMatt Macy 	 * to disk faster.  Therefore, we allow the first few passes of
2017eda14cbcSMatt Macy 	 * spa_sync() to allocate new blocks, but force rewrites after that.
2018eda14cbcSMatt Macy 	 * There should only be a handful of blocks after pass 1 in any case.
2019eda14cbcSMatt Macy 	 */
2020783d3ff6SMartin Matuska 	if (!BP_IS_HOLE(bp) && BP_GET_LOGICAL_BIRTH(bp) == zio->io_txg &&
2021eda14cbcSMatt Macy 	    BP_GET_PSIZE(bp) == psize &&
2022eda14cbcSMatt Macy 	    pass >= zfs_sync_pass_rewrite) {
2023eda14cbcSMatt Macy 		VERIFY3U(psize, !=, 0);
2024eda14cbcSMatt Macy 		enum zio_stage gang_stages = zio->io_pipeline & ZIO_GANG_STAGES;
2025eda14cbcSMatt Macy 
2026eda14cbcSMatt Macy 		zio->io_pipeline = ZIO_REWRITE_PIPELINE | gang_stages;
2027eda14cbcSMatt Macy 		zio->io_flags |= ZIO_FLAG_IO_REWRITE;
2028eda14cbcSMatt Macy 	} else {
2029eda14cbcSMatt Macy 		BP_ZERO(bp);
2030eda14cbcSMatt Macy 		zio->io_pipeline = ZIO_WRITE_PIPELINE;
2031eda14cbcSMatt Macy 	}
2032eda14cbcSMatt Macy 
2033eda14cbcSMatt Macy 	if (psize == 0) {
2034783d3ff6SMartin Matuska 		if (BP_GET_LOGICAL_BIRTH(&zio->io_bp_orig) != 0 &&
2035eda14cbcSMatt Macy 		    spa_feature_is_active(spa, SPA_FEATURE_HOLE_BIRTH)) {
2036eda14cbcSMatt Macy 			BP_SET_LSIZE(bp, lsize);
2037eda14cbcSMatt Macy 			BP_SET_TYPE(bp, zp->zp_type);
2038eda14cbcSMatt Macy 			BP_SET_LEVEL(bp, zp->zp_level);
2039eda14cbcSMatt Macy 			BP_SET_BIRTH(bp, zio->io_txg, 0);
2040eda14cbcSMatt Macy 		}
2041eda14cbcSMatt Macy 		zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2042eda14cbcSMatt Macy 	} else {
2043eda14cbcSMatt Macy 		ASSERT(zp->zp_checksum != ZIO_CHECKSUM_GANG_HEADER);
2044eda14cbcSMatt Macy 		BP_SET_LSIZE(bp, lsize);
2045eda14cbcSMatt Macy 		BP_SET_TYPE(bp, zp->zp_type);
2046eda14cbcSMatt Macy 		BP_SET_LEVEL(bp, zp->zp_level);
2047eda14cbcSMatt Macy 		BP_SET_PSIZE(bp, psize);
2048eda14cbcSMatt Macy 		BP_SET_COMPRESS(bp, compress);
2049eda14cbcSMatt Macy 		BP_SET_CHECKSUM(bp, zp->zp_checksum);
2050eda14cbcSMatt Macy 		BP_SET_DEDUP(bp, zp->zp_dedup);
2051eda14cbcSMatt Macy 		BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
2052eda14cbcSMatt Macy 		if (zp->zp_dedup) {
2053eda14cbcSMatt Macy 			ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2054eda14cbcSMatt Macy 			ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
2055eda14cbcSMatt Macy 			ASSERT(!zp->zp_encrypt ||
2056eda14cbcSMatt Macy 			    DMU_OT_IS_ENCRYPTED(zp->zp_type));
2057eda14cbcSMatt Macy 			zio->io_pipeline = ZIO_DDT_WRITE_PIPELINE;
2058eda14cbcSMatt Macy 		}
2059eda14cbcSMatt Macy 		if (zp->zp_nopwrite) {
2060eda14cbcSMatt Macy 			ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2061eda14cbcSMatt Macy 			ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
2062eda14cbcSMatt Macy 			zio->io_pipeline |= ZIO_STAGE_NOP_WRITE;
2063eda14cbcSMatt Macy 		}
2064eda14cbcSMatt Macy 	}
2065eda14cbcSMatt Macy 	return (zio);
2066eda14cbcSMatt Macy }
2067eda14cbcSMatt Macy 
2068eda14cbcSMatt Macy static zio_t *
2069eda14cbcSMatt Macy zio_free_bp_init(zio_t *zio)
2070eda14cbcSMatt Macy {
2071eda14cbcSMatt Macy 	blkptr_t *bp = zio->io_bp;
2072eda14cbcSMatt Macy 
2073eda14cbcSMatt Macy 	if (zio->io_child_type == ZIO_CHILD_LOGICAL) {
2074eda14cbcSMatt Macy 		if (BP_GET_DEDUP(bp))
2075eda14cbcSMatt Macy 			zio->io_pipeline = ZIO_DDT_FREE_PIPELINE;
2076eda14cbcSMatt Macy 	}
2077eda14cbcSMatt Macy 
2078eda14cbcSMatt Macy 	ASSERT3P(zio->io_bp, ==, &zio->io_bp_copy);
2079eda14cbcSMatt Macy 
2080eda14cbcSMatt Macy 	return (zio);
2081eda14cbcSMatt Macy }
2082eda14cbcSMatt Macy 
2083eda14cbcSMatt Macy /*
2084eda14cbcSMatt Macy  * ==========================================================================
2085eda14cbcSMatt Macy  * Execute the I/O pipeline
2086eda14cbcSMatt Macy  * ==========================================================================
2087eda14cbcSMatt Macy  */
2088eda14cbcSMatt Macy 
2089eda14cbcSMatt Macy static void
2090eda14cbcSMatt Macy zio_taskq_dispatch(zio_t *zio, zio_taskq_type_t q, boolean_t cutinline)
2091eda14cbcSMatt Macy {
2092eda14cbcSMatt Macy 	spa_t *spa = zio->io_spa;
2093eda14cbcSMatt Macy 	zio_type_t t = zio->io_type;
2094eda14cbcSMatt Macy 
2095eda14cbcSMatt Macy 	/*
2096eda14cbcSMatt Macy 	 * If we're a config writer or a probe, the normal issue and
2097eda14cbcSMatt Macy 	 * interrupt threads may all be blocked waiting for the config lock.
2098eda14cbcSMatt Macy 	 * In this case, select the otherwise-unused taskq for ZIO_TYPE_NULL.
2099eda14cbcSMatt Macy 	 */
2100eda14cbcSMatt Macy 	if (zio->io_flags & (ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_PROBE))
2101eda14cbcSMatt Macy 		t = ZIO_TYPE_NULL;
2102eda14cbcSMatt Macy 
2103eda14cbcSMatt Macy 	/*
2104eda14cbcSMatt Macy 	 * A similar issue exists for the L2ARC write thread until L2ARC 2.0.
2105eda14cbcSMatt Macy 	 */
2106eda14cbcSMatt Macy 	if (t == ZIO_TYPE_WRITE && zio->io_vd && zio->io_vd->vdev_aux)
2107eda14cbcSMatt Macy 		t = ZIO_TYPE_NULL;
2108eda14cbcSMatt Macy 
2109eda14cbcSMatt Macy 	/*
2110eda14cbcSMatt Macy 	 * If this is a high priority I/O, then use the high priority taskq if
2111aca928a5SMartin Matuska 	 * available or cut the line otherwise.
2112eda14cbcSMatt Macy 	 */
2113aca928a5SMartin Matuska 	if (zio->io_priority == ZIO_PRIORITY_SYNC_WRITE) {
2114aca928a5SMartin Matuska 		if (spa->spa_zio_taskq[t][q + 1].stqs_count != 0)
2115eda14cbcSMatt Macy 			q++;
2116aca928a5SMartin Matuska 		else
2117aca928a5SMartin Matuska 			cutinline = B_TRUE;
2118aca928a5SMartin Matuska 	}
2119eda14cbcSMatt Macy 
2120eda14cbcSMatt Macy 	ASSERT3U(q, <, ZIO_TASKQ_TYPES);
2121eda14cbcSMatt Macy 
2122aca928a5SMartin Matuska 	spa_taskq_dispatch(spa, t, q, zio_execute, zio, cutinline);
2123eda14cbcSMatt Macy }
2124eda14cbcSMatt Macy 
2125eda14cbcSMatt Macy static boolean_t
2126eda14cbcSMatt Macy zio_taskq_member(zio_t *zio, zio_taskq_type_t q)
2127eda14cbcSMatt Macy {
2128eda14cbcSMatt Macy 	spa_t *spa = zio->io_spa;
2129eda14cbcSMatt Macy 
2130eda14cbcSMatt Macy 	taskq_t *tq = taskq_of_curthread();
2131eda14cbcSMatt Macy 
2132eda14cbcSMatt Macy 	for (zio_type_t t = 0; t < ZIO_TYPES; t++) {
2133eda14cbcSMatt Macy 		spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
2134eda14cbcSMatt Macy 		uint_t i;
2135eda14cbcSMatt Macy 		for (i = 0; i < tqs->stqs_count; i++) {
2136eda14cbcSMatt Macy 			if (tqs->stqs_taskq[i] == tq)
2137eda14cbcSMatt Macy 				return (B_TRUE);
2138eda14cbcSMatt Macy 		}
2139eda14cbcSMatt Macy 	}
2140eda14cbcSMatt Macy 
2141eda14cbcSMatt Macy 	return (B_FALSE);
2142eda14cbcSMatt Macy }
2143eda14cbcSMatt Macy 
2144eda14cbcSMatt Macy static zio_t *
2145eda14cbcSMatt Macy zio_issue_async(zio_t *zio)
2146eda14cbcSMatt Macy {
214714c2e0a0SMartin Matuska 	ASSERT((zio->io_type != ZIO_TYPE_WRITE) || ZIO_HAS_ALLOCATOR(zio));
2148eda14cbcSMatt Macy 	zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE);
2149eda14cbcSMatt Macy 	return (NULL);
2150eda14cbcSMatt Macy }
2151eda14cbcSMatt Macy 
2152eda14cbcSMatt Macy void
21533f9d360cSMartin Matuska zio_interrupt(void *zio)
2154eda14cbcSMatt Macy {
2155eda14cbcSMatt Macy 	zio_taskq_dispatch(zio, ZIO_TASKQ_INTERRUPT, B_FALSE);
2156eda14cbcSMatt Macy }
2157eda14cbcSMatt Macy 
2158eda14cbcSMatt Macy void
2159eda14cbcSMatt Macy zio_delay_interrupt(zio_t *zio)
2160eda14cbcSMatt Macy {
2161eda14cbcSMatt Macy 	/*
2162eda14cbcSMatt Macy 	 * The timeout_generic() function isn't defined in userspace, so
2163eda14cbcSMatt Macy 	 * rather than trying to implement the function, the zio delay
2164eda14cbcSMatt Macy 	 * functionality has been disabled for userspace builds.
2165eda14cbcSMatt Macy 	 */
2166eda14cbcSMatt Macy 
2167eda14cbcSMatt Macy #ifdef _KERNEL
2168eda14cbcSMatt Macy 	/*
2169eda14cbcSMatt Macy 	 * If io_target_timestamp is zero, then no delay has been registered
2170eda14cbcSMatt Macy 	 * for this IO, thus jump to the end of this function and "skip" the
2171eda14cbcSMatt Macy 	 * delay; issuing it directly to the zio layer.
2172eda14cbcSMatt Macy 	 */
2173eda14cbcSMatt Macy 	if (zio->io_target_timestamp != 0) {
2174eda14cbcSMatt Macy 		hrtime_t now = gethrtime();
2175eda14cbcSMatt Macy 
2176eda14cbcSMatt Macy 		if (now >= zio->io_target_timestamp) {
2177eda14cbcSMatt Macy 			/*
2178eda14cbcSMatt Macy 			 * This IO has already taken longer than the target
2179eda14cbcSMatt Macy 			 * delay to complete, so we don't want to delay it
2180eda14cbcSMatt Macy 			 * any longer; we "miss" the delay and issue it
2181eda14cbcSMatt Macy 			 * directly to the zio layer. This is likely due to
2182eda14cbcSMatt Macy 			 * the target latency being set to a value less than
2183eda14cbcSMatt Macy 			 * the underlying hardware can satisfy (e.g. delay
2184eda14cbcSMatt Macy 			 * set to 1ms, but the disks take 10ms to complete an
2185eda14cbcSMatt Macy 			 * IO request).
2186eda14cbcSMatt Macy 			 */
2187eda14cbcSMatt Macy 
2188eda14cbcSMatt Macy 			DTRACE_PROBE2(zio__delay__miss, zio_t *, zio,
2189eda14cbcSMatt Macy 			    hrtime_t, now);
2190eda14cbcSMatt Macy 
2191eda14cbcSMatt Macy 			zio_interrupt(zio);
2192eda14cbcSMatt Macy 		} else {
2193eda14cbcSMatt Macy 			taskqid_t tid;
2194eda14cbcSMatt Macy 			hrtime_t diff = zio->io_target_timestamp - now;
2195718519f4SMartin Matuska 			int ticks = MAX(1, NSEC_TO_TICK(diff));
2196718519f4SMartin Matuska 			clock_t expire_at_tick = ddi_get_lbolt() + ticks;
2197eda14cbcSMatt Macy 
2198eda14cbcSMatt Macy 			DTRACE_PROBE3(zio__delay__hit, zio_t *, zio,
2199eda14cbcSMatt Macy 			    hrtime_t, now, hrtime_t, diff);
2200eda14cbcSMatt Macy 
2201718519f4SMartin Matuska 			tid = taskq_dispatch_delay(system_taskq, zio_interrupt,
2202718519f4SMartin Matuska 			    zio, TQ_NOSLEEP, expire_at_tick);
2203eda14cbcSMatt Macy 			if (tid == TASKQID_INVALID) {
2204eda14cbcSMatt Macy 				/*
2205718519f4SMartin Matuska 				 * Couldn't allocate a task.  Just finish the
2206718519f4SMartin Matuska 				 * zio without a delay.
2207eda14cbcSMatt Macy 				 */
2208eda14cbcSMatt Macy 				zio_interrupt(zio);
2209eda14cbcSMatt Macy 			}
2210eda14cbcSMatt Macy 		}
2211eda14cbcSMatt Macy 		return;
2212eda14cbcSMatt Macy 	}
2213eda14cbcSMatt Macy #endif
2214eda14cbcSMatt Macy 	DTRACE_PROBE1(zio__delay__skip, zio_t *, zio);
2215eda14cbcSMatt Macy 	zio_interrupt(zio);
2216eda14cbcSMatt Macy }
2217eda14cbcSMatt Macy 
2218eda14cbcSMatt Macy static void
2219eda14cbcSMatt Macy zio_deadman_impl(zio_t *pio, int ziodepth)
2220eda14cbcSMatt Macy {
2221eda14cbcSMatt Macy 	zio_t *cio, *cio_next;
2222eda14cbcSMatt Macy 	zio_link_t *zl = NULL;
2223eda14cbcSMatt Macy 	vdev_t *vd = pio->io_vd;
2224eda14cbcSMatt Macy 
2225eda14cbcSMatt Macy 	if (zio_deadman_log_all || (vd != NULL && vd->vdev_ops->vdev_op_leaf)) {
2226eda14cbcSMatt Macy 		vdev_queue_t *vq = vd ? &vd->vdev_queue : NULL;
2227eda14cbcSMatt Macy 		zbookmark_phys_t *zb = &pio->io_bookmark;
2228eda14cbcSMatt Macy 		uint64_t delta = gethrtime() - pio->io_timestamp;
2229eda14cbcSMatt Macy 		uint64_t failmode = spa_get_deadman_failmode(pio->io_spa);
2230eda14cbcSMatt Macy 
2231eda14cbcSMatt Macy 		zfs_dbgmsg("slow zio[%d]: zio=%px timestamp=%llu "
2232eda14cbcSMatt Macy 		    "delta=%llu queued=%llu io=%llu "
223333b8c039SMartin Matuska 		    "path=%s "
223433b8c039SMartin Matuska 		    "last=%llu type=%d "
2235dbd5678dSMartin Matuska 		    "priority=%d flags=0x%llx stage=0x%x "
223633b8c039SMartin Matuska 		    "pipeline=0x%x pipeline-trace=0x%x "
223733b8c039SMartin Matuska 		    "objset=%llu object=%llu "
223833b8c039SMartin Matuska 		    "level=%llu blkid=%llu "
223933b8c039SMartin Matuska 		    "offset=%llu size=%llu "
224033b8c039SMartin Matuska 		    "error=%d",
2241eda14cbcSMatt Macy 		    ziodepth, pio, pio->io_timestamp,
224233b8c039SMartin Matuska 		    (u_longlong_t)delta, pio->io_delta, pio->io_delay,
224333b8c039SMartin Matuska 		    vd ? vd->vdev_path : "NULL",
224433b8c039SMartin Matuska 		    vq ? vq->vq_io_complete_ts : 0, pio->io_type,
2245dbd5678dSMartin Matuska 		    pio->io_priority, (u_longlong_t)pio->io_flags,
2246dbd5678dSMartin Matuska 		    pio->io_stage, pio->io_pipeline, pio->io_pipeline_trace,
224733b8c039SMartin Matuska 		    (u_longlong_t)zb->zb_objset, (u_longlong_t)zb->zb_object,
224833b8c039SMartin Matuska 		    (u_longlong_t)zb->zb_level, (u_longlong_t)zb->zb_blkid,
224933b8c039SMartin Matuska 		    (u_longlong_t)pio->io_offset, (u_longlong_t)pio->io_size,
225033b8c039SMartin Matuska 		    pio->io_error);
2251eac7052fSMatt Macy 		(void) zfs_ereport_post(FM_EREPORT_ZFS_DEADMAN,
22522c48331dSMatt Macy 		    pio->io_spa, vd, zb, pio, 0);
2253eda14cbcSMatt Macy 
2254eda14cbcSMatt Macy 		if (failmode == ZIO_FAILURE_MODE_CONTINUE &&
2255eda14cbcSMatt Macy 		    taskq_empty_ent(&pio->io_tqent)) {
2256eda14cbcSMatt Macy 			zio_interrupt(pio);
2257eda14cbcSMatt Macy 		}
2258eda14cbcSMatt Macy 	}
2259eda14cbcSMatt Macy 
2260eda14cbcSMatt Macy 	mutex_enter(&pio->io_lock);
2261eda14cbcSMatt Macy 	for (cio = zio_walk_children(pio, &zl); cio != NULL; cio = cio_next) {
2262eda14cbcSMatt Macy 		cio_next = zio_walk_children(pio, &zl);
2263eda14cbcSMatt Macy 		zio_deadman_impl(cio, ziodepth + 1);
2264eda14cbcSMatt Macy 	}
2265eda14cbcSMatt Macy 	mutex_exit(&pio->io_lock);
2266eda14cbcSMatt Macy }
2267eda14cbcSMatt Macy 
2268eda14cbcSMatt Macy /*
2269eda14cbcSMatt Macy  * Log the critical information describing this zio and all of its children
2270eda14cbcSMatt Macy  * using the zfs_dbgmsg() interface then post deadman event for the ZED.
2271eda14cbcSMatt Macy  */
2272eda14cbcSMatt Macy void
2273a0b956f5SMartin Matuska zio_deadman(zio_t *pio, const char *tag)
2274eda14cbcSMatt Macy {
2275eda14cbcSMatt Macy 	spa_t *spa = pio->io_spa;
2276eda14cbcSMatt Macy 	char *name = spa_name(spa);
2277eda14cbcSMatt Macy 
2278eda14cbcSMatt Macy 	if (!zfs_deadman_enabled || spa_suspended(spa))
2279eda14cbcSMatt Macy 		return;
2280eda14cbcSMatt Macy 
2281eda14cbcSMatt Macy 	zio_deadman_impl(pio, 0);
2282eda14cbcSMatt Macy 
2283eda14cbcSMatt Macy 	switch (spa_get_deadman_failmode(spa)) {
2284eda14cbcSMatt Macy 	case ZIO_FAILURE_MODE_WAIT:
2285eda14cbcSMatt Macy 		zfs_dbgmsg("%s waiting for hung I/O to pool '%s'", tag, name);
2286eda14cbcSMatt Macy 		break;
2287eda14cbcSMatt Macy 
2288eda14cbcSMatt Macy 	case ZIO_FAILURE_MODE_CONTINUE:
2289eda14cbcSMatt Macy 		zfs_dbgmsg("%s restarting hung I/O for pool '%s'", tag, name);
2290eda14cbcSMatt Macy 		break;
2291eda14cbcSMatt Macy 
2292eda14cbcSMatt Macy 	case ZIO_FAILURE_MODE_PANIC:
2293eda14cbcSMatt Macy 		fm_panic("%s determined I/O to pool '%s' is hung.", tag, name);
2294eda14cbcSMatt Macy 		break;
2295eda14cbcSMatt Macy 	}
2296eda14cbcSMatt Macy }
2297eda14cbcSMatt Macy 
2298eda14cbcSMatt Macy /*
2299eda14cbcSMatt Macy  * Execute the I/O pipeline until one of the following occurs:
2300eda14cbcSMatt Macy  * (1) the I/O completes; (2) the pipeline stalls waiting for
2301eda14cbcSMatt Macy  * dependent child I/Os; (3) the I/O issues, so we're waiting
2302eda14cbcSMatt Macy  * for an I/O completion interrupt; (4) the I/O is delegated by
2303eda14cbcSMatt Macy  * vdev-level caching or aggregation; (5) the I/O is deferred
2304eda14cbcSMatt Macy  * due to vdev-level queueing; (6) the I/O is handed off to
2305eda14cbcSMatt Macy  * another thread.  In all cases, the pipeline stops whenever
2306eda14cbcSMatt Macy  * there's no CPU work; it never burns a thread in cv_wait_io().
2307eda14cbcSMatt Macy  *
2308eda14cbcSMatt Macy  * There's no locking on io_stage because there's no legitimate way
2309eda14cbcSMatt Macy  * for multiple threads to be attempting to process the same I/O.
2310eda14cbcSMatt Macy  */
2311eda14cbcSMatt Macy static zio_pipe_stage_t *zio_pipeline[];
2312eda14cbcSMatt Macy 
2313eda14cbcSMatt Macy /*
2314eda14cbcSMatt Macy  * zio_execute() is a wrapper around the static function
2315eda14cbcSMatt Macy  * __zio_execute() so that we can force  __zio_execute() to be
2316eda14cbcSMatt Macy  * inlined.  This reduces stack overhead which is important
2317eda14cbcSMatt Macy  * because __zio_execute() is called recursively in several zio
2318eda14cbcSMatt Macy  * code paths.  zio_execute() itself cannot be inlined because
2319eda14cbcSMatt Macy  * it is externally visible.
2320eda14cbcSMatt Macy  */
2321eda14cbcSMatt Macy void
23223f9d360cSMartin Matuska zio_execute(void *zio)
2323eda14cbcSMatt Macy {
2324eda14cbcSMatt Macy 	fstrans_cookie_t cookie;
2325eda14cbcSMatt Macy 
2326eda14cbcSMatt Macy 	cookie = spl_fstrans_mark();
2327eda14cbcSMatt Macy 	__zio_execute(zio);
2328eda14cbcSMatt Macy 	spl_fstrans_unmark(cookie);
2329eda14cbcSMatt Macy }
2330eda14cbcSMatt Macy 
2331eda14cbcSMatt Macy /*
2332eda14cbcSMatt Macy  * Used to determine if in the current context the stack is sized large
2333eda14cbcSMatt Macy  * enough to allow zio_execute() to be called recursively.  A minimum
2334eda14cbcSMatt Macy  * stack size of 16K is required to avoid needing to re-dispatch the zio.
2335eda14cbcSMatt Macy  */
2336eda14cbcSMatt Macy static boolean_t
2337eda14cbcSMatt Macy zio_execute_stack_check(zio_t *zio)
2338eda14cbcSMatt Macy {
2339eda14cbcSMatt Macy #if !defined(HAVE_LARGE_STACKS)
2340eda14cbcSMatt Macy 	dsl_pool_t *dp = spa_get_dsl(zio->io_spa);
2341eda14cbcSMatt Macy 
2342eda14cbcSMatt Macy 	/* Executing in txg_sync_thread() context. */
2343eda14cbcSMatt Macy 	if (dp && curthread == dp->dp_tx.tx_sync_thread)
2344eda14cbcSMatt Macy 		return (B_TRUE);
2345eda14cbcSMatt Macy 
2346eda14cbcSMatt Macy 	/* Pool initialization outside of zio_taskq context. */
2347eda14cbcSMatt Macy 	if (dp && spa_is_initializing(dp->dp_spa) &&
2348eda14cbcSMatt Macy 	    !zio_taskq_member(zio, ZIO_TASKQ_ISSUE) &&
2349eda14cbcSMatt Macy 	    !zio_taskq_member(zio, ZIO_TASKQ_ISSUE_HIGH))
2350eda14cbcSMatt Macy 		return (B_TRUE);
2351e92ffd9bSMartin Matuska #else
2352e92ffd9bSMartin Matuska 	(void) zio;
2353eda14cbcSMatt Macy #endif /* HAVE_LARGE_STACKS */
2354eda14cbcSMatt Macy 
2355eda14cbcSMatt Macy 	return (B_FALSE);
2356eda14cbcSMatt Macy }
2357eda14cbcSMatt Macy 
2358eda14cbcSMatt Macy __attribute__((always_inline))
2359eda14cbcSMatt Macy static inline void
2360eda14cbcSMatt Macy __zio_execute(zio_t *zio)
2361eda14cbcSMatt Macy {
2362eda14cbcSMatt Macy 	ASSERT3U(zio->io_queued_timestamp, >, 0);
2363eda14cbcSMatt Macy 
2364eda14cbcSMatt Macy 	while (zio->io_stage < ZIO_STAGE_DONE) {
2365eda14cbcSMatt Macy 		enum zio_stage pipeline = zio->io_pipeline;
2366eda14cbcSMatt Macy 		enum zio_stage stage = zio->io_stage;
2367eda14cbcSMatt Macy 
2368eda14cbcSMatt Macy 		zio->io_executor = curthread;
2369eda14cbcSMatt Macy 
2370eda14cbcSMatt Macy 		ASSERT(!MUTEX_HELD(&zio->io_lock));
2371eda14cbcSMatt Macy 		ASSERT(ISP2(stage));
2372eda14cbcSMatt Macy 		ASSERT(zio->io_stall == NULL);
2373eda14cbcSMatt Macy 
2374eda14cbcSMatt Macy 		do {
2375eda14cbcSMatt Macy 			stage <<= 1;
2376eda14cbcSMatt Macy 		} while ((stage & pipeline) == 0);
2377eda14cbcSMatt Macy 
2378eda14cbcSMatt Macy 		ASSERT(stage <= ZIO_STAGE_DONE);
2379eda14cbcSMatt Macy 
2380eda14cbcSMatt Macy 		/*
2381eda14cbcSMatt Macy 		 * If we are in interrupt context and this pipeline stage
2382eda14cbcSMatt Macy 		 * will grab a config lock that is held across I/O,
2383eda14cbcSMatt Macy 		 * or may wait for an I/O that needs an interrupt thread
2384eda14cbcSMatt Macy 		 * to complete, issue async to avoid deadlock.
2385eda14cbcSMatt Macy 		 *
2386eda14cbcSMatt Macy 		 * For VDEV_IO_START, we cut in line so that the io will
2387eda14cbcSMatt Macy 		 * be sent to disk promptly.
2388eda14cbcSMatt Macy 		 */
2389eda14cbcSMatt Macy 		if ((stage & ZIO_BLOCKING_STAGES) && zio->io_vd == NULL &&
2390eda14cbcSMatt Macy 		    zio_taskq_member(zio, ZIO_TASKQ_INTERRUPT)) {
2391eda14cbcSMatt Macy 			boolean_t cut = (stage == ZIO_STAGE_VDEV_IO_START) ?
2392eda14cbcSMatt Macy 			    zio_requeue_io_start_cut_in_line : B_FALSE;
2393eda14cbcSMatt Macy 			zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, cut);
2394eda14cbcSMatt Macy 			return;
2395eda14cbcSMatt Macy 		}
2396eda14cbcSMatt Macy 
2397eda14cbcSMatt Macy 		/*
2398eda14cbcSMatt Macy 		 * If the current context doesn't have large enough stacks
2399eda14cbcSMatt Macy 		 * the zio must be issued asynchronously to prevent overflow.
2400eda14cbcSMatt Macy 		 */
2401eda14cbcSMatt Macy 		if (zio_execute_stack_check(zio)) {
2402eda14cbcSMatt Macy 			boolean_t cut = (stage == ZIO_STAGE_VDEV_IO_START) ?
2403eda14cbcSMatt Macy 			    zio_requeue_io_start_cut_in_line : B_FALSE;
2404eda14cbcSMatt Macy 			zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, cut);
2405eda14cbcSMatt Macy 			return;
2406eda14cbcSMatt Macy 		}
2407eda14cbcSMatt Macy 
2408eda14cbcSMatt Macy 		zio->io_stage = stage;
2409eda14cbcSMatt Macy 		zio->io_pipeline_trace |= zio->io_stage;
2410eda14cbcSMatt Macy 
2411eda14cbcSMatt Macy 		/*
2412eda14cbcSMatt Macy 		 * The zio pipeline stage returns the next zio to execute
2413eda14cbcSMatt Macy 		 * (typically the same as this one), or NULL if we should
2414eda14cbcSMatt Macy 		 * stop.
2415eda14cbcSMatt Macy 		 */
2416eda14cbcSMatt Macy 		zio = zio_pipeline[highbit64(stage) - 1](zio);
2417eda14cbcSMatt Macy 
2418eda14cbcSMatt Macy 		if (zio == NULL)
2419eda14cbcSMatt Macy 			return;
2420eda14cbcSMatt Macy 	}
2421eda14cbcSMatt Macy }
2422eda14cbcSMatt Macy 
2423eda14cbcSMatt Macy 
2424eda14cbcSMatt Macy /*
2425eda14cbcSMatt Macy  * ==========================================================================
2426eda14cbcSMatt Macy  * Initiate I/O, either sync or async
2427eda14cbcSMatt Macy  * ==========================================================================
2428eda14cbcSMatt Macy  */
2429eda14cbcSMatt Macy int
2430eda14cbcSMatt Macy zio_wait(zio_t *zio)
2431eda14cbcSMatt Macy {
2432eda14cbcSMatt Macy 	/*
2433eda14cbcSMatt Macy 	 * Some routines, like zio_free_sync(), may return a NULL zio
2434eda14cbcSMatt Macy 	 * to avoid the performance overhead of creating and then destroying
2435eda14cbcSMatt Macy 	 * an unneeded zio.  For the callers' simplicity, we accept a NULL
2436eda14cbcSMatt Macy 	 * zio and ignore it.
2437eda14cbcSMatt Macy 	 */
2438eda14cbcSMatt Macy 	if (zio == NULL)
2439eda14cbcSMatt Macy 		return (0);
2440eda14cbcSMatt Macy 
2441eda14cbcSMatt Macy 	long timeout = MSEC_TO_TICK(zfs_deadman_ziotime_ms);
2442eda14cbcSMatt Macy 	int error;
2443eda14cbcSMatt Macy 
2444eda14cbcSMatt Macy 	ASSERT3S(zio->io_stage, ==, ZIO_STAGE_OPEN);
2445eda14cbcSMatt Macy 	ASSERT3P(zio->io_executor, ==, NULL);
2446eda14cbcSMatt Macy 
2447eda14cbcSMatt Macy 	zio->io_waiter = curthread;
2448eda14cbcSMatt Macy 	ASSERT0(zio->io_queued_timestamp);
2449eda14cbcSMatt Macy 	zio->io_queued_timestamp = gethrtime();
2450eda14cbcSMatt Macy 
245114c2e0a0SMartin Matuska 	if (zio->io_type == ZIO_TYPE_WRITE) {
245214c2e0a0SMartin Matuska 		spa_select_allocator(zio);
245314c2e0a0SMartin Matuska 	}
2454eda14cbcSMatt Macy 	__zio_execute(zio);
2455eda14cbcSMatt Macy 
2456eda14cbcSMatt Macy 	mutex_enter(&zio->io_lock);
2457eda14cbcSMatt Macy 	while (zio->io_executor != NULL) {
2458eda14cbcSMatt Macy 		error = cv_timedwait_io(&zio->io_cv, &zio->io_lock,
2459eda14cbcSMatt Macy 		    ddi_get_lbolt() + timeout);
2460eda14cbcSMatt Macy 
2461eda14cbcSMatt Macy 		if (zfs_deadman_enabled && error == -1 &&
2462eda14cbcSMatt Macy 		    gethrtime() - zio->io_queued_timestamp >
2463eda14cbcSMatt Macy 		    spa_deadman_ziotime(zio->io_spa)) {
2464eda14cbcSMatt Macy 			mutex_exit(&zio->io_lock);
2465eda14cbcSMatt Macy 			timeout = MSEC_TO_TICK(zfs_deadman_checktime_ms);
2466eda14cbcSMatt Macy 			zio_deadman(zio, FTAG);
2467eda14cbcSMatt Macy 			mutex_enter(&zio->io_lock);
2468eda14cbcSMatt Macy 		}
2469eda14cbcSMatt Macy 	}
2470eda14cbcSMatt Macy 	mutex_exit(&zio->io_lock);
2471eda14cbcSMatt Macy 
2472eda14cbcSMatt Macy 	error = zio->io_error;
2473eda14cbcSMatt Macy 	zio_destroy(zio);
2474eda14cbcSMatt Macy 
2475eda14cbcSMatt Macy 	return (error);
2476eda14cbcSMatt Macy }
2477eda14cbcSMatt Macy 
2478eda14cbcSMatt Macy void
2479eda14cbcSMatt Macy zio_nowait(zio_t *zio)
2480eda14cbcSMatt Macy {
2481eda14cbcSMatt Macy 	/*
2482eda14cbcSMatt Macy 	 * See comment in zio_wait().
2483eda14cbcSMatt Macy 	 */
2484eda14cbcSMatt Macy 	if (zio == NULL)
2485eda14cbcSMatt Macy 		return;
2486eda14cbcSMatt Macy 
2487eda14cbcSMatt Macy 	ASSERT3P(zio->io_executor, ==, NULL);
2488eda14cbcSMatt Macy 
2489eda14cbcSMatt Macy 	if (zio->io_child_type == ZIO_CHILD_LOGICAL &&
2490e639e0d2SMartin Matuska 	    list_is_empty(&zio->io_parent_list)) {
2491eda14cbcSMatt Macy 		zio_t *pio;
2492eda14cbcSMatt Macy 
2493eda14cbcSMatt Macy 		/*
2494eda14cbcSMatt Macy 		 * This is a logical async I/O with no parent to wait for it.
2495eda14cbcSMatt Macy 		 * We add it to the spa_async_root_zio "Godfather" I/O which
2496eda14cbcSMatt Macy 		 * will ensure they complete prior to unloading the pool.
2497eda14cbcSMatt Macy 		 */
2498eda14cbcSMatt Macy 		spa_t *spa = zio->io_spa;
24997877fdebSMatt Macy 		pio = spa->spa_async_zio_root[CPU_SEQID_UNSTABLE];
2500eda14cbcSMatt Macy 
2501eda14cbcSMatt Macy 		zio_add_child(pio, zio);
2502eda14cbcSMatt Macy 	}
2503eda14cbcSMatt Macy 
2504eda14cbcSMatt Macy 	ASSERT0(zio->io_queued_timestamp);
2505eda14cbcSMatt Macy 	zio->io_queued_timestamp = gethrtime();
250614c2e0a0SMartin Matuska 	if (zio->io_type == ZIO_TYPE_WRITE) {
250714c2e0a0SMartin Matuska 		spa_select_allocator(zio);
250814c2e0a0SMartin Matuska 	}
2509eda14cbcSMatt Macy 	__zio_execute(zio);
2510eda14cbcSMatt Macy }
2511eda14cbcSMatt Macy 
2512eda14cbcSMatt Macy /*
2513eda14cbcSMatt Macy  * ==========================================================================
2514eda14cbcSMatt Macy  * Reexecute, cancel, or suspend/resume failed I/O
2515eda14cbcSMatt Macy  * ==========================================================================
2516eda14cbcSMatt Macy  */
2517eda14cbcSMatt Macy 
2518eda14cbcSMatt Macy static void
25193f9d360cSMartin Matuska zio_reexecute(void *arg)
2520eda14cbcSMatt Macy {
25213f9d360cSMartin Matuska 	zio_t *pio = arg;
25226c1e79dfSMartin Matuska 	zio_t *cio, *cio_next, *gio;
2523eda14cbcSMatt Macy 
2524eda14cbcSMatt Macy 	ASSERT(pio->io_child_type == ZIO_CHILD_LOGICAL);
2525eda14cbcSMatt Macy 	ASSERT(pio->io_orig_stage == ZIO_STAGE_OPEN);
2526eda14cbcSMatt Macy 	ASSERT(pio->io_gang_leader == NULL);
2527eda14cbcSMatt Macy 	ASSERT(pio->io_gang_tree == NULL);
2528eda14cbcSMatt Macy 
25296c1e79dfSMartin Matuska 	mutex_enter(&pio->io_lock);
2530eda14cbcSMatt Macy 	pio->io_flags = pio->io_orig_flags;
2531eda14cbcSMatt Macy 	pio->io_stage = pio->io_orig_stage;
2532eda14cbcSMatt Macy 	pio->io_pipeline = pio->io_orig_pipeline;
2533eda14cbcSMatt Macy 	pio->io_reexecute = 0;
2534eda14cbcSMatt Macy 	pio->io_flags |= ZIO_FLAG_REEXECUTED;
2535eda14cbcSMatt Macy 	pio->io_pipeline_trace = 0;
2536eda14cbcSMatt Macy 	pio->io_error = 0;
25376c1e79dfSMartin Matuska 	pio->io_state[ZIO_WAIT_READY] = (pio->io_stage >= ZIO_STAGE_READY) ||
25386c1e79dfSMartin Matuska 	    (pio->io_pipeline & ZIO_STAGE_READY) == 0;
25396c1e79dfSMartin Matuska 	pio->io_state[ZIO_WAIT_DONE] = (pio->io_stage >= ZIO_STAGE_DONE);
25406c1e79dfSMartin Matuska 	zio_link_t *zl = NULL;
25416c1e79dfSMartin Matuska 	while ((gio = zio_walk_parents(pio, &zl)) != NULL) {
25426c1e79dfSMartin Matuska 		for (int w = 0; w < ZIO_WAIT_TYPES; w++) {
25436c1e79dfSMartin Matuska 			gio->io_children[pio->io_child_type][w] +=
25446c1e79dfSMartin Matuska 			    !pio->io_state[w];
25456c1e79dfSMartin Matuska 		}
25466c1e79dfSMartin Matuska 	}
2547eda14cbcSMatt Macy 	for (int c = 0; c < ZIO_CHILD_TYPES; c++)
2548eda14cbcSMatt Macy 		pio->io_child_error[c] = 0;
2549eda14cbcSMatt Macy 
2550eda14cbcSMatt Macy 	if (IO_IS_ALLOCATING(pio))
2551eda14cbcSMatt Macy 		BP_ZERO(pio->io_bp);
2552eda14cbcSMatt Macy 
2553eda14cbcSMatt Macy 	/*
2554eda14cbcSMatt Macy 	 * As we reexecute pio's children, new children could be created.
2555eda14cbcSMatt Macy 	 * New children go to the head of pio's io_child_list, however,
2556eda14cbcSMatt Macy 	 * so we will (correctly) not reexecute them.  The key is that
2557eda14cbcSMatt Macy 	 * the remainder of pio's io_child_list, from 'cio_next' onward,
2558eda14cbcSMatt Macy 	 * cannot be affected by any side effects of reexecuting 'cio'.
2559eda14cbcSMatt Macy 	 */
25606c1e79dfSMartin Matuska 	zl = NULL;
2561eda14cbcSMatt Macy 	for (cio = zio_walk_children(pio, &zl); cio != NULL; cio = cio_next) {
2562eda14cbcSMatt Macy 		cio_next = zio_walk_children(pio, &zl);
2563eda14cbcSMatt Macy 		mutex_exit(&pio->io_lock);
2564eda14cbcSMatt Macy 		zio_reexecute(cio);
2565eda14cbcSMatt Macy 		mutex_enter(&pio->io_lock);
2566eda14cbcSMatt Macy 	}
2567eda14cbcSMatt Macy 	mutex_exit(&pio->io_lock);
2568eda14cbcSMatt Macy 
2569eda14cbcSMatt Macy 	/*
2570eda14cbcSMatt Macy 	 * Now that all children have been reexecuted, execute the parent.
2571eda14cbcSMatt Macy 	 * We don't reexecute "The Godfather" I/O here as it's the
2572eda14cbcSMatt Macy 	 * responsibility of the caller to wait on it.
2573eda14cbcSMatt Macy 	 */
2574eda14cbcSMatt Macy 	if (!(pio->io_flags & ZIO_FLAG_GODFATHER)) {
2575eda14cbcSMatt Macy 		pio->io_queued_timestamp = gethrtime();
2576eda14cbcSMatt Macy 		__zio_execute(pio);
2577eda14cbcSMatt Macy 	}
2578eda14cbcSMatt Macy }
2579eda14cbcSMatt Macy 
2580eda14cbcSMatt Macy void
2581eda14cbcSMatt Macy zio_suspend(spa_t *spa, zio_t *zio, zio_suspend_reason_t reason)
2582eda14cbcSMatt Macy {
2583eda14cbcSMatt Macy 	if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_PANIC)
2584eda14cbcSMatt Macy 		fm_panic("Pool '%s' has encountered an uncorrectable I/O "
2585eda14cbcSMatt Macy 		    "failure and the failure mode property for this pool "
2586eda14cbcSMatt Macy 		    "is set to panic.", spa_name(spa));
2587eda14cbcSMatt Macy 
2588b985c9caSMartin Matuska 	if (reason != ZIO_SUSPEND_MMP) {
2589b985c9caSMartin Matuska 		cmn_err(CE_WARN, "Pool '%s' has encountered an uncorrectable "
25907a7741afSMartin Matuska 		    "I/O failure and has been suspended.", spa_name(spa));
2591b985c9caSMartin Matuska 	}
2592eda14cbcSMatt Macy 
2593eac7052fSMatt Macy 	(void) zfs_ereport_post(FM_EREPORT_ZFS_IO_FAILURE, spa, NULL,
25942c48331dSMatt Macy 	    NULL, NULL, 0);
2595eda14cbcSMatt Macy 
2596eda14cbcSMatt Macy 	mutex_enter(&spa->spa_suspend_lock);
2597eda14cbcSMatt Macy 
2598eda14cbcSMatt Macy 	if (spa->spa_suspend_zio_root == NULL)
2599eda14cbcSMatt Macy 		spa->spa_suspend_zio_root = zio_root(spa, NULL, NULL,
2600eda14cbcSMatt Macy 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
2601eda14cbcSMatt Macy 		    ZIO_FLAG_GODFATHER);
2602eda14cbcSMatt Macy 
2603eda14cbcSMatt Macy 	spa->spa_suspended = reason;
2604eda14cbcSMatt Macy 
2605eda14cbcSMatt Macy 	if (zio != NULL) {
2606eda14cbcSMatt Macy 		ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER));
2607eda14cbcSMatt Macy 		ASSERT(zio != spa->spa_suspend_zio_root);
2608eda14cbcSMatt Macy 		ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2609eda14cbcSMatt Macy 		ASSERT(zio_unique_parent(zio) == NULL);
2610eda14cbcSMatt Macy 		ASSERT(zio->io_stage == ZIO_STAGE_DONE);
2611eda14cbcSMatt Macy 		zio_add_child(spa->spa_suspend_zio_root, zio);
2612eda14cbcSMatt Macy 	}
2613eda14cbcSMatt Macy 
2614eda14cbcSMatt Macy 	mutex_exit(&spa->spa_suspend_lock);
2615eda14cbcSMatt Macy }
2616eda14cbcSMatt Macy 
2617eda14cbcSMatt Macy int
2618eda14cbcSMatt Macy zio_resume(spa_t *spa)
2619eda14cbcSMatt Macy {
2620eda14cbcSMatt Macy 	zio_t *pio;
2621eda14cbcSMatt Macy 
2622eda14cbcSMatt Macy 	/*
2623eda14cbcSMatt Macy 	 * Reexecute all previously suspended i/o.
2624eda14cbcSMatt Macy 	 */
2625eda14cbcSMatt Macy 	mutex_enter(&spa->spa_suspend_lock);
26267a7741afSMartin Matuska 	if (spa->spa_suspended != ZIO_SUSPEND_NONE)
26277a7741afSMartin Matuska 		cmn_err(CE_WARN, "Pool '%s' was suspended and is being "
26287a7741afSMartin Matuska 		    "resumed. Failed I/O will be retried.",
26297a7741afSMartin Matuska 		    spa_name(spa));
2630eda14cbcSMatt Macy 	spa->spa_suspended = ZIO_SUSPEND_NONE;
2631eda14cbcSMatt Macy 	cv_broadcast(&spa->spa_suspend_cv);
2632eda14cbcSMatt Macy 	pio = spa->spa_suspend_zio_root;
2633eda14cbcSMatt Macy 	spa->spa_suspend_zio_root = NULL;
2634eda14cbcSMatt Macy 	mutex_exit(&spa->spa_suspend_lock);
2635eda14cbcSMatt Macy 
2636eda14cbcSMatt Macy 	if (pio == NULL)
2637eda14cbcSMatt Macy 		return (0);
2638eda14cbcSMatt Macy 
2639eda14cbcSMatt Macy 	zio_reexecute(pio);
2640eda14cbcSMatt Macy 	return (zio_wait(pio));
2641eda14cbcSMatt Macy }
2642eda14cbcSMatt Macy 
2643eda14cbcSMatt Macy void
2644eda14cbcSMatt Macy zio_resume_wait(spa_t *spa)
2645eda14cbcSMatt Macy {
2646eda14cbcSMatt Macy 	mutex_enter(&spa->spa_suspend_lock);
2647eda14cbcSMatt Macy 	while (spa_suspended(spa))
2648eda14cbcSMatt Macy 		cv_wait(&spa->spa_suspend_cv, &spa->spa_suspend_lock);
2649eda14cbcSMatt Macy 	mutex_exit(&spa->spa_suspend_lock);
2650eda14cbcSMatt Macy }
2651eda14cbcSMatt Macy 
2652eda14cbcSMatt Macy /*
2653eda14cbcSMatt Macy  * ==========================================================================
2654eda14cbcSMatt Macy  * Gang blocks.
2655eda14cbcSMatt Macy  *
2656eda14cbcSMatt Macy  * A gang block is a collection of small blocks that looks to the DMU
2657eda14cbcSMatt Macy  * like one large block.  When zio_dva_allocate() cannot find a block
2658eda14cbcSMatt Macy  * of the requested size, due to either severe fragmentation or the pool
2659eda14cbcSMatt Macy  * being nearly full, it calls zio_write_gang_block() to construct the
2660eda14cbcSMatt Macy  * block from smaller fragments.
2661eda14cbcSMatt Macy  *
2662eda14cbcSMatt Macy  * A gang block consists of a gang header (zio_gbh_phys_t) and up to
2663eda14cbcSMatt Macy  * three (SPA_GBH_NBLKPTRS) gang members.  The gang header is just like
2664eda14cbcSMatt Macy  * an indirect block: it's an array of block pointers.  It consumes
2665eda14cbcSMatt Macy  * only one sector and hence is allocatable regardless of fragmentation.
2666eda14cbcSMatt Macy  * The gang header's bps point to its gang members, which hold the data.
2667eda14cbcSMatt Macy  *
2668eda14cbcSMatt Macy  * Gang blocks are self-checksumming, using the bp's <vdev, offset, txg>
2669eda14cbcSMatt Macy  * as the verifier to ensure uniqueness of the SHA256 checksum.
2670eda14cbcSMatt Macy  * Critically, the gang block bp's blk_cksum is the checksum of the data,
2671eda14cbcSMatt Macy  * not the gang header.  This ensures that data block signatures (needed for
2672eda14cbcSMatt Macy  * deduplication) are independent of how the block is physically stored.
2673eda14cbcSMatt Macy  *
2674eda14cbcSMatt Macy  * Gang blocks can be nested: a gang member may itself be a gang block.
2675eda14cbcSMatt Macy  * Thus every gang block is a tree in which root and all interior nodes are
2676eda14cbcSMatt Macy  * gang headers, and the leaves are normal blocks that contain user data.
2677eda14cbcSMatt Macy  * The root of the gang tree is called the gang leader.
2678eda14cbcSMatt Macy  *
2679eda14cbcSMatt Macy  * To perform any operation (read, rewrite, free, claim) on a gang block,
2680eda14cbcSMatt Macy  * zio_gang_assemble() first assembles the gang tree (minus data leaves)
2681eda14cbcSMatt Macy  * in the io_gang_tree field of the original logical i/o by recursively
2682eda14cbcSMatt Macy  * reading the gang leader and all gang headers below it.  This yields
2683eda14cbcSMatt Macy  * an in-core tree containing the contents of every gang header and the
2684eda14cbcSMatt Macy  * bps for every constituent of the gang block.
2685eda14cbcSMatt Macy  *
2686eda14cbcSMatt Macy  * With the gang tree now assembled, zio_gang_issue() just walks the gang tree
2687eda14cbcSMatt Macy  * and invokes a callback on each bp.  To free a gang block, zio_gang_issue()
2688eda14cbcSMatt Macy  * calls zio_free_gang() -- a trivial wrapper around zio_free() -- for each bp.
2689eda14cbcSMatt Macy  * zio_claim_gang() provides a similarly trivial wrapper for zio_claim().
2690eda14cbcSMatt Macy  * zio_read_gang() is a wrapper around zio_read() that omits reading gang
2691eda14cbcSMatt Macy  * headers, since we already have those in io_gang_tree.  zio_rewrite_gang()
2692eda14cbcSMatt Macy  * performs a zio_rewrite() of the data or, for gang headers, a zio_rewrite()
2693eda14cbcSMatt Macy  * of the gang header plus zio_checksum_compute() of the data to update the
2694eda14cbcSMatt Macy  * gang header's blk_cksum as described above.
2695eda14cbcSMatt Macy  *
2696eda14cbcSMatt Macy  * The two-phase assemble/issue model solves the problem of partial failure --
2697eda14cbcSMatt Macy  * what if you'd freed part of a gang block but then couldn't read the
2698eda14cbcSMatt Macy  * gang header for another part?  Assembling the entire gang tree first
2699eda14cbcSMatt Macy  * ensures that all the necessary gang header I/O has succeeded before
2700eda14cbcSMatt Macy  * starting the actual work of free, claim, or write.  Once the gang tree
2701eda14cbcSMatt Macy  * is assembled, free and claim are in-memory operations that cannot fail.
2702eda14cbcSMatt Macy  *
2703eda14cbcSMatt Macy  * In the event that a gang write fails, zio_dva_unallocate() walks the
2704eda14cbcSMatt Macy  * gang tree to immediately free (i.e. insert back into the space map)
2705eda14cbcSMatt Macy  * everything we've allocated.  This ensures that we don't get ENOSPC
2706eda14cbcSMatt Macy  * errors during repeated suspend/resume cycles due to a flaky device.
2707eda14cbcSMatt Macy  *
2708eda14cbcSMatt Macy  * Gang rewrites only happen during sync-to-convergence.  If we can't assemble
2709eda14cbcSMatt Macy  * the gang tree, we won't modify the block, so we can safely defer the free
2710eda14cbcSMatt Macy  * (knowing that the block is still intact).  If we *can* assemble the gang
2711eda14cbcSMatt Macy  * tree, then even if some of the rewrites fail, zio_dva_unallocate() will free
2712eda14cbcSMatt Macy  * each constituent bp and we can allocate a new block on the next sync pass.
2713eda14cbcSMatt Macy  *
2714eda14cbcSMatt Macy  * In all cases, the gang tree allows complete recovery from partial failure.
2715eda14cbcSMatt Macy  * ==========================================================================
2716eda14cbcSMatt Macy  */
2717eda14cbcSMatt Macy 
2718eda14cbcSMatt Macy static void
2719eda14cbcSMatt Macy zio_gang_issue_func_done(zio_t *zio)
2720eda14cbcSMatt Macy {
2721184c1b94SMartin Matuska 	abd_free(zio->io_abd);
2722eda14cbcSMatt Macy }
2723eda14cbcSMatt Macy 
2724eda14cbcSMatt Macy static zio_t *
2725eda14cbcSMatt Macy zio_read_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data,
2726eda14cbcSMatt Macy     uint64_t offset)
2727eda14cbcSMatt Macy {
2728eda14cbcSMatt Macy 	if (gn != NULL)
2729eda14cbcSMatt Macy 		return (pio);
2730eda14cbcSMatt Macy 
2731eda14cbcSMatt Macy 	return (zio_read(pio, pio->io_spa, bp, abd_get_offset(data, offset),
2732eda14cbcSMatt Macy 	    BP_GET_PSIZE(bp), zio_gang_issue_func_done,
2733eda14cbcSMatt Macy 	    NULL, pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio),
2734eda14cbcSMatt Macy 	    &pio->io_bookmark));
2735eda14cbcSMatt Macy }
2736eda14cbcSMatt Macy 
2737eda14cbcSMatt Macy static zio_t *
2738eda14cbcSMatt Macy zio_rewrite_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data,
2739eda14cbcSMatt Macy     uint64_t offset)
2740eda14cbcSMatt Macy {
2741eda14cbcSMatt Macy 	zio_t *zio;
2742eda14cbcSMatt Macy 
2743eda14cbcSMatt Macy 	if (gn != NULL) {
2744eda14cbcSMatt Macy 		abd_t *gbh_abd =
2745eda14cbcSMatt Macy 		    abd_get_from_buf(gn->gn_gbh, SPA_GANGBLOCKSIZE);
2746eda14cbcSMatt Macy 		zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp,
2747eda14cbcSMatt Macy 		    gbh_abd, SPA_GANGBLOCKSIZE, zio_gang_issue_func_done, NULL,
2748eda14cbcSMatt Macy 		    pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio),
2749eda14cbcSMatt Macy 		    &pio->io_bookmark);
2750eda14cbcSMatt Macy 		/*
2751eda14cbcSMatt Macy 		 * As we rewrite each gang header, the pipeline will compute
2752eda14cbcSMatt Macy 		 * a new gang block header checksum for it; but no one will
2753eda14cbcSMatt Macy 		 * compute a new data checksum, so we do that here.  The one
2754eda14cbcSMatt Macy 		 * exception is the gang leader: the pipeline already computed
2755eda14cbcSMatt Macy 		 * its data checksum because that stage precedes gang assembly.
2756eda14cbcSMatt Macy 		 * (Presently, nothing actually uses interior data checksums;
2757eda14cbcSMatt Macy 		 * this is just good hygiene.)
2758eda14cbcSMatt Macy 		 */
2759eda14cbcSMatt Macy 		if (gn != pio->io_gang_leader->io_gang_tree) {
2760eda14cbcSMatt Macy 			abd_t *buf = abd_get_offset(data, offset);
2761eda14cbcSMatt Macy 
2762eda14cbcSMatt Macy 			zio_checksum_compute(zio, BP_GET_CHECKSUM(bp),
2763eda14cbcSMatt Macy 			    buf, BP_GET_PSIZE(bp));
2764eda14cbcSMatt Macy 
2765184c1b94SMartin Matuska 			abd_free(buf);
2766eda14cbcSMatt Macy 		}
2767eda14cbcSMatt Macy 		/*
2768eda14cbcSMatt Macy 		 * If we are here to damage data for testing purposes,
2769eda14cbcSMatt Macy 		 * leave the GBH alone so that we can detect the damage.
2770eda14cbcSMatt Macy 		 */
2771eda14cbcSMatt Macy 		if (pio->io_gang_leader->io_flags & ZIO_FLAG_INDUCE_DAMAGE)
2772eda14cbcSMatt Macy 			zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES;
2773eda14cbcSMatt Macy 	} else {
2774eda14cbcSMatt Macy 		zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp,
2775eda14cbcSMatt Macy 		    abd_get_offset(data, offset), BP_GET_PSIZE(bp),
2776eda14cbcSMatt Macy 		    zio_gang_issue_func_done, NULL, pio->io_priority,
2777eda14cbcSMatt Macy 		    ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
2778eda14cbcSMatt Macy 	}
2779eda14cbcSMatt Macy 
2780eda14cbcSMatt Macy 	return (zio);
2781eda14cbcSMatt Macy }
2782eda14cbcSMatt Macy 
2783eda14cbcSMatt Macy static zio_t *
2784eda14cbcSMatt Macy zio_free_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data,
2785eda14cbcSMatt Macy     uint64_t offset)
2786eda14cbcSMatt Macy {
2787e92ffd9bSMartin Matuska 	(void) gn, (void) data, (void) offset;
2788e92ffd9bSMartin Matuska 
2789eda14cbcSMatt Macy 	zio_t *zio = zio_free_sync(pio, pio->io_spa, pio->io_txg, bp,
2790eda14cbcSMatt Macy 	    ZIO_GANG_CHILD_FLAGS(pio));
2791eda14cbcSMatt Macy 	if (zio == NULL) {
2792eda14cbcSMatt Macy 		zio = zio_null(pio, pio->io_spa,
2793eda14cbcSMatt Macy 		    NULL, NULL, NULL, ZIO_GANG_CHILD_FLAGS(pio));
2794eda14cbcSMatt Macy 	}
2795eda14cbcSMatt Macy 	return (zio);
2796eda14cbcSMatt Macy }
2797eda14cbcSMatt Macy 
2798eda14cbcSMatt Macy static zio_t *
2799eda14cbcSMatt Macy zio_claim_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data,
2800eda14cbcSMatt Macy     uint64_t offset)
2801eda14cbcSMatt Macy {
2802e92ffd9bSMartin Matuska 	(void) gn, (void) data, (void) offset;
2803eda14cbcSMatt Macy 	return (zio_claim(pio, pio->io_spa, pio->io_txg, bp,
2804eda14cbcSMatt Macy 	    NULL, NULL, ZIO_GANG_CHILD_FLAGS(pio)));
2805eda14cbcSMatt Macy }
2806eda14cbcSMatt Macy 
2807eda14cbcSMatt Macy static zio_gang_issue_func_t *zio_gang_issue_func[ZIO_TYPES] = {
2808eda14cbcSMatt Macy 	NULL,
2809eda14cbcSMatt Macy 	zio_read_gang,
2810eda14cbcSMatt Macy 	zio_rewrite_gang,
2811eda14cbcSMatt Macy 	zio_free_gang,
2812eda14cbcSMatt Macy 	zio_claim_gang,
2813eda14cbcSMatt Macy 	NULL
2814eda14cbcSMatt Macy };
2815eda14cbcSMatt Macy 
2816eda14cbcSMatt Macy static void zio_gang_tree_assemble_done(zio_t *zio);
2817eda14cbcSMatt Macy 
2818eda14cbcSMatt Macy static zio_gang_node_t *
2819eda14cbcSMatt Macy zio_gang_node_alloc(zio_gang_node_t **gnpp)
2820eda14cbcSMatt Macy {
2821eda14cbcSMatt Macy 	zio_gang_node_t *gn;
2822eda14cbcSMatt Macy 
2823eda14cbcSMatt Macy 	ASSERT(*gnpp == NULL);
2824eda14cbcSMatt Macy 
2825eda14cbcSMatt Macy 	gn = kmem_zalloc(sizeof (*gn), KM_SLEEP);
2826eda14cbcSMatt Macy 	gn->gn_gbh = zio_buf_alloc(SPA_GANGBLOCKSIZE);
2827eda14cbcSMatt Macy 	*gnpp = gn;
2828eda14cbcSMatt Macy 
2829eda14cbcSMatt Macy 	return (gn);
2830eda14cbcSMatt Macy }
2831eda14cbcSMatt Macy 
2832eda14cbcSMatt Macy static void
2833eda14cbcSMatt Macy zio_gang_node_free(zio_gang_node_t **gnpp)
2834eda14cbcSMatt Macy {
2835eda14cbcSMatt Macy 	zio_gang_node_t *gn = *gnpp;
2836eda14cbcSMatt Macy 
2837eda14cbcSMatt Macy 	for (int g = 0; g < SPA_GBH_NBLKPTRS; g++)
2838eda14cbcSMatt Macy 		ASSERT(gn->gn_child[g] == NULL);
2839eda14cbcSMatt Macy 
2840eda14cbcSMatt Macy 	zio_buf_free(gn->gn_gbh, SPA_GANGBLOCKSIZE);
2841eda14cbcSMatt Macy 	kmem_free(gn, sizeof (*gn));
2842eda14cbcSMatt Macy 	*gnpp = NULL;
2843eda14cbcSMatt Macy }
2844eda14cbcSMatt Macy 
2845eda14cbcSMatt Macy static void
2846eda14cbcSMatt Macy zio_gang_tree_free(zio_gang_node_t **gnpp)
2847eda14cbcSMatt Macy {
2848eda14cbcSMatt Macy 	zio_gang_node_t *gn = *gnpp;
2849eda14cbcSMatt Macy 
2850eda14cbcSMatt Macy 	if (gn == NULL)
2851eda14cbcSMatt Macy 		return;
2852eda14cbcSMatt Macy 
2853eda14cbcSMatt Macy 	for (int g = 0; g < SPA_GBH_NBLKPTRS; g++)
2854eda14cbcSMatt Macy 		zio_gang_tree_free(&gn->gn_child[g]);
2855eda14cbcSMatt Macy 
2856eda14cbcSMatt Macy 	zio_gang_node_free(gnpp);
2857eda14cbcSMatt Macy }
2858eda14cbcSMatt Macy 
2859eda14cbcSMatt Macy static void
2860eda14cbcSMatt Macy zio_gang_tree_assemble(zio_t *gio, blkptr_t *bp, zio_gang_node_t **gnpp)
2861eda14cbcSMatt Macy {
2862eda14cbcSMatt Macy 	zio_gang_node_t *gn = zio_gang_node_alloc(gnpp);
2863eda14cbcSMatt Macy 	abd_t *gbh_abd = abd_get_from_buf(gn->gn_gbh, SPA_GANGBLOCKSIZE);
2864eda14cbcSMatt Macy 
2865eda14cbcSMatt Macy 	ASSERT(gio->io_gang_leader == gio);
2866eda14cbcSMatt Macy 	ASSERT(BP_IS_GANG(bp));
2867eda14cbcSMatt Macy 
2868eda14cbcSMatt Macy 	zio_nowait(zio_read(gio, gio->io_spa, bp, gbh_abd, SPA_GANGBLOCKSIZE,
2869eda14cbcSMatt Macy 	    zio_gang_tree_assemble_done, gn, gio->io_priority,
2870eda14cbcSMatt Macy 	    ZIO_GANG_CHILD_FLAGS(gio), &gio->io_bookmark));
2871eda14cbcSMatt Macy }
2872eda14cbcSMatt Macy 
2873eda14cbcSMatt Macy static void
2874eda14cbcSMatt Macy zio_gang_tree_assemble_done(zio_t *zio)
2875eda14cbcSMatt Macy {
2876eda14cbcSMatt Macy 	zio_t *gio = zio->io_gang_leader;
2877eda14cbcSMatt Macy 	zio_gang_node_t *gn = zio->io_private;
2878eda14cbcSMatt Macy 	blkptr_t *bp = zio->io_bp;
2879eda14cbcSMatt Macy 
2880eda14cbcSMatt Macy 	ASSERT(gio == zio_unique_parent(zio));
2881b7198dcfSMartin Matuska 	ASSERT(list_is_empty(&zio->io_child_list));
2882eda14cbcSMatt Macy 
2883eda14cbcSMatt Macy 	if (zio->io_error)
2884eda14cbcSMatt Macy 		return;
2885eda14cbcSMatt Macy 
2886eda14cbcSMatt Macy 	/* this ABD was created from a linear buf in zio_gang_tree_assemble */
2887eda14cbcSMatt Macy 	if (BP_SHOULD_BYTESWAP(bp))
2888eda14cbcSMatt Macy 		byteswap_uint64_array(abd_to_buf(zio->io_abd), zio->io_size);
2889eda14cbcSMatt Macy 
2890eda14cbcSMatt Macy 	ASSERT3P(abd_to_buf(zio->io_abd), ==, gn->gn_gbh);
2891eda14cbcSMatt Macy 	ASSERT(zio->io_size == SPA_GANGBLOCKSIZE);
2892eda14cbcSMatt Macy 	ASSERT(gn->gn_gbh->zg_tail.zec_magic == ZEC_MAGIC);
2893eda14cbcSMatt Macy 
2894184c1b94SMartin Matuska 	abd_free(zio->io_abd);
2895eda14cbcSMatt Macy 
2896eda14cbcSMatt Macy 	for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
2897eda14cbcSMatt Macy 		blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g];
2898eda14cbcSMatt Macy 		if (!BP_IS_GANG(gbp))
2899eda14cbcSMatt Macy 			continue;
2900eda14cbcSMatt Macy 		zio_gang_tree_assemble(gio, gbp, &gn->gn_child[g]);
2901eda14cbcSMatt Macy 	}
2902eda14cbcSMatt Macy }
2903eda14cbcSMatt Macy 
2904eda14cbcSMatt Macy static void
2905eda14cbcSMatt Macy zio_gang_tree_issue(zio_t *pio, zio_gang_node_t *gn, blkptr_t *bp, abd_t *data,
2906eda14cbcSMatt Macy     uint64_t offset)
2907eda14cbcSMatt Macy {
2908eda14cbcSMatt Macy 	zio_t *gio = pio->io_gang_leader;
2909eda14cbcSMatt Macy 	zio_t *zio;
2910eda14cbcSMatt Macy 
2911eda14cbcSMatt Macy 	ASSERT(BP_IS_GANG(bp) == !!gn);
2912eda14cbcSMatt Macy 	ASSERT(BP_GET_CHECKSUM(bp) == BP_GET_CHECKSUM(gio->io_bp));
2913eda14cbcSMatt Macy 	ASSERT(BP_GET_LSIZE(bp) == BP_GET_PSIZE(bp) || gn == gio->io_gang_tree);
2914eda14cbcSMatt Macy 
2915eda14cbcSMatt Macy 	/*
2916eda14cbcSMatt Macy 	 * If you're a gang header, your data is in gn->gn_gbh.
2917eda14cbcSMatt Macy 	 * If you're a gang member, your data is in 'data' and gn == NULL.
2918eda14cbcSMatt Macy 	 */
2919eda14cbcSMatt Macy 	zio = zio_gang_issue_func[gio->io_type](pio, bp, gn, data, offset);
2920eda14cbcSMatt Macy 
2921eda14cbcSMatt Macy 	if (gn != NULL) {
2922eda14cbcSMatt Macy 		ASSERT(gn->gn_gbh->zg_tail.zec_magic == ZEC_MAGIC);
2923eda14cbcSMatt Macy 
2924eda14cbcSMatt Macy 		for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
2925eda14cbcSMatt Macy 			blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g];
2926eda14cbcSMatt Macy 			if (BP_IS_HOLE(gbp))
2927eda14cbcSMatt Macy 				continue;
2928eda14cbcSMatt Macy 			zio_gang_tree_issue(zio, gn->gn_child[g], gbp, data,
2929eda14cbcSMatt Macy 			    offset);
2930eda14cbcSMatt Macy 			offset += BP_GET_PSIZE(gbp);
2931eda14cbcSMatt Macy 		}
2932eda14cbcSMatt Macy 	}
2933eda14cbcSMatt Macy 
2934eda14cbcSMatt Macy 	if (gn == gio->io_gang_tree)
2935eda14cbcSMatt Macy 		ASSERT3U(gio->io_size, ==, offset);
2936eda14cbcSMatt Macy 
2937eda14cbcSMatt Macy 	if (zio != pio)
2938eda14cbcSMatt Macy 		zio_nowait(zio);
2939eda14cbcSMatt Macy }
2940eda14cbcSMatt Macy 
2941eda14cbcSMatt Macy static zio_t *
2942eda14cbcSMatt Macy zio_gang_assemble(zio_t *zio)
2943eda14cbcSMatt Macy {
2944eda14cbcSMatt Macy 	blkptr_t *bp = zio->io_bp;
2945eda14cbcSMatt Macy 
2946eda14cbcSMatt Macy 	ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == NULL);
2947eda14cbcSMatt Macy 	ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
2948eda14cbcSMatt Macy 
2949eda14cbcSMatt Macy 	zio->io_gang_leader = zio;
2950eda14cbcSMatt Macy 
2951eda14cbcSMatt Macy 	zio_gang_tree_assemble(zio, bp, &zio->io_gang_tree);
2952eda14cbcSMatt Macy 
2953eda14cbcSMatt Macy 	return (zio);
2954eda14cbcSMatt Macy }
2955eda14cbcSMatt Macy 
2956eda14cbcSMatt Macy static zio_t *
2957eda14cbcSMatt Macy zio_gang_issue(zio_t *zio)
2958eda14cbcSMatt Macy {
2959eda14cbcSMatt Macy 	blkptr_t *bp = zio->io_bp;
2960eda14cbcSMatt Macy 
2961eda14cbcSMatt Macy 	if (zio_wait_for_children(zio, ZIO_CHILD_GANG_BIT, ZIO_WAIT_DONE)) {
2962eda14cbcSMatt Macy 		return (NULL);
2963eda14cbcSMatt Macy 	}
2964eda14cbcSMatt Macy 
2965eda14cbcSMatt Macy 	ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == zio);
2966eda14cbcSMatt Macy 	ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
2967eda14cbcSMatt Macy 
2968eda14cbcSMatt Macy 	if (zio->io_child_error[ZIO_CHILD_GANG] == 0)
2969eda14cbcSMatt Macy 		zio_gang_tree_issue(zio, zio->io_gang_tree, bp, zio->io_abd,
2970eda14cbcSMatt Macy 		    0);
2971eda14cbcSMatt Macy 	else
2972eda14cbcSMatt Macy 		zio_gang_tree_free(&zio->io_gang_tree);
2973eda14cbcSMatt Macy 
2974eda14cbcSMatt Macy 	zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2975eda14cbcSMatt Macy 
2976eda14cbcSMatt Macy 	return (zio);
2977eda14cbcSMatt Macy }
2978eda14cbcSMatt Macy 
2979eda14cbcSMatt Macy static void
298014c2e0a0SMartin Matuska zio_gang_inherit_allocator(zio_t *pio, zio_t *cio)
298114c2e0a0SMartin Matuska {
298214c2e0a0SMartin Matuska 	cio->io_allocator = pio->io_allocator;
298314c2e0a0SMartin Matuska }
298414c2e0a0SMartin Matuska 
298514c2e0a0SMartin Matuska static void
2986eda14cbcSMatt Macy zio_write_gang_member_ready(zio_t *zio)
2987eda14cbcSMatt Macy {
2988eda14cbcSMatt Macy 	zio_t *pio = zio_unique_parent(zio);
2989eda14cbcSMatt Macy 	dva_t *cdva = zio->io_bp->blk_dva;
2990eda14cbcSMatt Macy 	dva_t *pdva = pio->io_bp->blk_dva;
2991eda14cbcSMatt Macy 	uint64_t asize;
2992eda14cbcSMatt Macy 	zio_t *gio __maybe_unused = zio->io_gang_leader;
2993eda14cbcSMatt Macy 
2994eda14cbcSMatt Macy 	if (BP_IS_HOLE(zio->io_bp))
2995eda14cbcSMatt Macy 		return;
2996eda14cbcSMatt Macy 
2997eda14cbcSMatt Macy 	ASSERT(BP_IS_HOLE(&zio->io_bp_orig));
2998eda14cbcSMatt Macy 
2999eda14cbcSMatt Macy 	ASSERT(zio->io_child_type == ZIO_CHILD_GANG);
3000eda14cbcSMatt Macy 	ASSERT3U(zio->io_prop.zp_copies, ==, gio->io_prop.zp_copies);
3001eda14cbcSMatt Macy 	ASSERT3U(zio->io_prop.zp_copies, <=, BP_GET_NDVAS(zio->io_bp));
3002eda14cbcSMatt Macy 	ASSERT3U(pio->io_prop.zp_copies, <=, BP_GET_NDVAS(pio->io_bp));
3003c9539b89SMartin Matuska 	VERIFY3U(BP_GET_NDVAS(zio->io_bp), <=, BP_GET_NDVAS(pio->io_bp));
3004eda14cbcSMatt Macy 
3005eda14cbcSMatt Macy 	mutex_enter(&pio->io_lock);
3006eda14cbcSMatt Macy 	for (int d = 0; d < BP_GET_NDVAS(zio->io_bp); d++) {
3007eda14cbcSMatt Macy 		ASSERT(DVA_GET_GANG(&pdva[d]));
3008eda14cbcSMatt Macy 		asize = DVA_GET_ASIZE(&pdva[d]);
3009eda14cbcSMatt Macy 		asize += DVA_GET_ASIZE(&cdva[d]);
3010eda14cbcSMatt Macy 		DVA_SET_ASIZE(&pdva[d], asize);
3011eda14cbcSMatt Macy 	}
3012eda14cbcSMatt Macy 	mutex_exit(&pio->io_lock);
3013eda14cbcSMatt Macy }
3014eda14cbcSMatt Macy 
3015eda14cbcSMatt Macy static void
3016eda14cbcSMatt Macy zio_write_gang_done(zio_t *zio)
3017eda14cbcSMatt Macy {
3018eda14cbcSMatt Macy 	/*
3019eda14cbcSMatt Macy 	 * The io_abd field will be NULL for a zio with no data.  The io_flags
3020eda14cbcSMatt Macy 	 * will initially have the ZIO_FLAG_NODATA bit flag set, but we can't
3021eda14cbcSMatt Macy 	 * check for it here as it is cleared in zio_ready.
3022eda14cbcSMatt Macy 	 */
3023eda14cbcSMatt Macy 	if (zio->io_abd != NULL)
3024184c1b94SMartin Matuska 		abd_free(zio->io_abd);
3025eda14cbcSMatt Macy }
3026eda14cbcSMatt Macy 
3027eda14cbcSMatt Macy static zio_t *
3028184c1b94SMartin Matuska zio_write_gang_block(zio_t *pio, metaslab_class_t *mc)
3029eda14cbcSMatt Macy {
3030eda14cbcSMatt Macy 	spa_t *spa = pio->io_spa;
3031eda14cbcSMatt Macy 	blkptr_t *bp = pio->io_bp;
3032eda14cbcSMatt Macy 	zio_t *gio = pio->io_gang_leader;
3033eda14cbcSMatt Macy 	zio_t *zio;
3034eda14cbcSMatt Macy 	zio_gang_node_t *gn, **gnpp;
3035eda14cbcSMatt Macy 	zio_gbh_phys_t *gbh;
3036eda14cbcSMatt Macy 	abd_t *gbh_abd;
3037eda14cbcSMatt Macy 	uint64_t txg = pio->io_txg;
3038eda14cbcSMatt Macy 	uint64_t resid = pio->io_size;
3039eda14cbcSMatt Macy 	uint64_t lsize;
3040eda14cbcSMatt Macy 	int copies = gio->io_prop.zp_copies;
3041eda14cbcSMatt Macy 	zio_prop_t zp;
3042eda14cbcSMatt Macy 	int error;
3043eda14cbcSMatt Macy 	boolean_t has_data = !(pio->io_flags & ZIO_FLAG_NODATA);
3044eda14cbcSMatt Macy 
3045eda14cbcSMatt Macy 	/*
3046c9539b89SMartin Matuska 	 * If one copy was requested, store 2 copies of the GBH, so that we
3047c9539b89SMartin Matuska 	 * can still traverse all the data (e.g. to free or scrub) even if a
3048c9539b89SMartin Matuska 	 * block is damaged.  Note that we can't store 3 copies of the GBH in
3049c9539b89SMartin Matuska 	 * all cases, e.g. with encryption, which uses DVA[2] for the IV+salt.
3050eda14cbcSMatt Macy 	 */
3051c9539b89SMartin Matuska 	int gbh_copies = copies;
3052c9539b89SMartin Matuska 	if (gbh_copies == 1) {
3053c9539b89SMartin Matuska 		gbh_copies = MIN(2, spa_max_replication(spa));
3054c9539b89SMartin Matuska 	}
3055eda14cbcSMatt Macy 
305614c2e0a0SMartin Matuska 	ASSERT(ZIO_HAS_ALLOCATOR(pio));
3057eda14cbcSMatt Macy 	int flags = METASLAB_HINTBP_FAVOR | METASLAB_GANG_HEADER;
3058eda14cbcSMatt Macy 	if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
3059eda14cbcSMatt Macy 		ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
3060eda14cbcSMatt Macy 		ASSERT(has_data);
3061eda14cbcSMatt Macy 
3062eda14cbcSMatt Macy 		flags |= METASLAB_ASYNC_ALLOC;
30637877fdebSMatt Macy 		VERIFY(zfs_refcount_held(&mc->mc_allocator[pio->io_allocator].
30647877fdebSMatt Macy 		    mca_alloc_slots, pio));
3065eda14cbcSMatt Macy 
3066eda14cbcSMatt Macy 		/*
3067eda14cbcSMatt Macy 		 * The logical zio has already placed a reservation for
3068eda14cbcSMatt Macy 		 * 'copies' allocation slots but gang blocks may require
3069eda14cbcSMatt Macy 		 * additional copies. These additional copies
3070eda14cbcSMatt Macy 		 * (i.e. gbh_copies - copies) are guaranteed to succeed
3071eda14cbcSMatt Macy 		 * since metaslab_class_throttle_reserve() always allows
3072eda14cbcSMatt Macy 		 * additional reservations for gang blocks.
3073eda14cbcSMatt Macy 		 */
3074eda14cbcSMatt Macy 		VERIFY(metaslab_class_throttle_reserve(mc, gbh_copies - copies,
3075eda14cbcSMatt Macy 		    pio->io_allocator, pio, flags));
3076eda14cbcSMatt Macy 	}
3077eda14cbcSMatt Macy 
3078eda14cbcSMatt Macy 	error = metaslab_alloc(spa, mc, SPA_GANGBLOCKSIZE,
3079eda14cbcSMatt Macy 	    bp, gbh_copies, txg, pio == gio ? NULL : gio->io_bp, flags,
3080eda14cbcSMatt Macy 	    &pio->io_alloc_list, pio, pio->io_allocator);
3081eda14cbcSMatt Macy 	if (error) {
3082eda14cbcSMatt Macy 		if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
3083eda14cbcSMatt Macy 			ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
3084eda14cbcSMatt Macy 			ASSERT(has_data);
3085eda14cbcSMatt Macy 
3086eda14cbcSMatt Macy 			/*
3087eda14cbcSMatt Macy 			 * If we failed to allocate the gang block header then
3088eda14cbcSMatt Macy 			 * we remove any additional allocation reservations that
3089eda14cbcSMatt Macy 			 * we placed here. The original reservation will
3090eda14cbcSMatt Macy 			 * be removed when the logical I/O goes to the ready
3091eda14cbcSMatt Macy 			 * stage.
3092eda14cbcSMatt Macy 			 */
3093eda14cbcSMatt Macy 			metaslab_class_throttle_unreserve(mc,
3094eda14cbcSMatt Macy 			    gbh_copies - copies, pio->io_allocator, pio);
3095eda14cbcSMatt Macy 		}
3096eda14cbcSMatt Macy 
3097eda14cbcSMatt Macy 		pio->io_error = error;
3098eda14cbcSMatt Macy 		return (pio);
3099eda14cbcSMatt Macy 	}
3100eda14cbcSMatt Macy 
3101eda14cbcSMatt Macy 	if (pio == gio) {
3102eda14cbcSMatt Macy 		gnpp = &gio->io_gang_tree;
3103eda14cbcSMatt Macy 	} else {
3104eda14cbcSMatt Macy 		gnpp = pio->io_private;
3105eda14cbcSMatt Macy 		ASSERT(pio->io_ready == zio_write_gang_member_ready);
3106eda14cbcSMatt Macy 	}
3107eda14cbcSMatt Macy 
3108eda14cbcSMatt Macy 	gn = zio_gang_node_alloc(gnpp);
3109eda14cbcSMatt Macy 	gbh = gn->gn_gbh;
3110da5137abSMartin Matuska 	memset(gbh, 0, SPA_GANGBLOCKSIZE);
3111eda14cbcSMatt Macy 	gbh_abd = abd_get_from_buf(gbh, SPA_GANGBLOCKSIZE);
3112eda14cbcSMatt Macy 
3113eda14cbcSMatt Macy 	/*
3114eda14cbcSMatt Macy 	 * Create the gang header.
3115eda14cbcSMatt Macy 	 */
3116eda14cbcSMatt Macy 	zio = zio_rewrite(pio, spa, txg, bp, gbh_abd, SPA_GANGBLOCKSIZE,
3117eda14cbcSMatt Macy 	    zio_write_gang_done, NULL, pio->io_priority,
3118eda14cbcSMatt Macy 	    ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
3119eda14cbcSMatt Macy 
312014c2e0a0SMartin Matuska 	zio_gang_inherit_allocator(pio, zio);
312114c2e0a0SMartin Matuska 
3122eda14cbcSMatt Macy 	/*
3123eda14cbcSMatt Macy 	 * Create and nowait the gang children.
3124eda14cbcSMatt Macy 	 */
3125eda14cbcSMatt Macy 	for (int g = 0; resid != 0; resid -= lsize, g++) {
3126eda14cbcSMatt Macy 		lsize = P2ROUNDUP(resid / (SPA_GBH_NBLKPTRS - g),
3127eda14cbcSMatt Macy 		    SPA_MINBLOCKSIZE);
3128eda14cbcSMatt Macy 		ASSERT(lsize >= SPA_MINBLOCKSIZE && lsize <= resid);
3129eda14cbcSMatt Macy 
3130eda14cbcSMatt Macy 		zp.zp_checksum = gio->io_prop.zp_checksum;
3131eda14cbcSMatt Macy 		zp.zp_compress = ZIO_COMPRESS_OFF;
3132eda14cbcSMatt Macy 		zp.zp_complevel = gio->io_prop.zp_complevel;
3133ce4dcb97SMartin Matuska 		zp.zp_type = zp.zp_storage_type = DMU_OT_NONE;
3134eda14cbcSMatt Macy 		zp.zp_level = 0;
3135eda14cbcSMatt Macy 		zp.zp_copies = gio->io_prop.zp_copies;
3136eda14cbcSMatt Macy 		zp.zp_dedup = B_FALSE;
3137eda14cbcSMatt Macy 		zp.zp_dedup_verify = B_FALSE;
3138eda14cbcSMatt Macy 		zp.zp_nopwrite = B_FALSE;
3139eda14cbcSMatt Macy 		zp.zp_encrypt = gio->io_prop.zp_encrypt;
3140eda14cbcSMatt Macy 		zp.zp_byteorder = gio->io_prop.zp_byteorder;
31417a7741afSMartin Matuska 		zp.zp_direct_write = B_FALSE;
3142da5137abSMartin Matuska 		memset(zp.zp_salt, 0, ZIO_DATA_SALT_LEN);
3143da5137abSMartin Matuska 		memset(zp.zp_iv, 0, ZIO_DATA_IV_LEN);
3144da5137abSMartin Matuska 		memset(zp.zp_mac, 0, ZIO_DATA_MAC_LEN);
3145eda14cbcSMatt Macy 
3146eda14cbcSMatt Macy 		zio_t *cio = zio_write(zio, spa, txg, &gbh->zg_blkptr[g],
3147eda14cbcSMatt Macy 		    has_data ? abd_get_offset(pio->io_abd, pio->io_size -
3148eda14cbcSMatt Macy 		    resid) : NULL, lsize, lsize, &zp,
3149b7198dcfSMartin Matuska 		    zio_write_gang_member_ready, NULL,
3150eda14cbcSMatt Macy 		    zio_write_gang_done, &gn->gn_child[g], pio->io_priority,
3151eda14cbcSMatt Macy 		    ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
3152eda14cbcSMatt Macy 
315314c2e0a0SMartin Matuska 		zio_gang_inherit_allocator(zio, cio);
315414c2e0a0SMartin Matuska 
3155eda14cbcSMatt Macy 		if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
3156eda14cbcSMatt Macy 			ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
3157eda14cbcSMatt Macy 			ASSERT(has_data);
3158eda14cbcSMatt Macy 
3159eda14cbcSMatt Macy 			/*
3160eda14cbcSMatt Macy 			 * Gang children won't throttle but we should
3161eda14cbcSMatt Macy 			 * account for their work, so reserve an allocation
3162eda14cbcSMatt Macy 			 * slot for them here.
3163eda14cbcSMatt Macy 			 */
3164eda14cbcSMatt Macy 			VERIFY(metaslab_class_throttle_reserve(mc,
3165eda14cbcSMatt Macy 			    zp.zp_copies, cio->io_allocator, cio, flags));
3166eda14cbcSMatt Macy 		}
3167eda14cbcSMatt Macy 		zio_nowait(cio);
3168eda14cbcSMatt Macy 	}
3169eda14cbcSMatt Macy 
3170eda14cbcSMatt Macy 	/*
3171eda14cbcSMatt Macy 	 * Set pio's pipeline to just wait for zio to finish.
3172eda14cbcSMatt Macy 	 */
3173eda14cbcSMatt Macy 	pio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
3174eda14cbcSMatt Macy 
3175eda14cbcSMatt Macy 	zio_nowait(zio);
3176eda14cbcSMatt Macy 
3177eda14cbcSMatt Macy 	return (pio);
3178eda14cbcSMatt Macy }
3179eda14cbcSMatt Macy 
3180eda14cbcSMatt Macy /*
3181eda14cbcSMatt Macy  * The zio_nop_write stage in the pipeline determines if allocating a
3182eda14cbcSMatt Macy  * new bp is necessary.  The nopwrite feature can handle writes in
3183eda14cbcSMatt Macy  * either syncing or open context (i.e. zil writes) and as a result is
3184eda14cbcSMatt Macy  * mutually exclusive with dedup.
3185eda14cbcSMatt Macy  *
3186eda14cbcSMatt Macy  * By leveraging a cryptographically secure checksum, such as SHA256, we
3187eda14cbcSMatt Macy  * can compare the checksums of the new data and the old to determine if
3188eda14cbcSMatt Macy  * allocating a new block is required.  Note that our requirements for
3189eda14cbcSMatt Macy  * cryptographic strength are fairly weak: there can't be any accidental
3190eda14cbcSMatt Macy  * hash collisions, but we don't need to be secure against intentional
3191eda14cbcSMatt Macy  * (malicious) collisions.  To trigger a nopwrite, you have to be able
3192eda14cbcSMatt Macy  * to write the file to begin with, and triggering an incorrect (hash
3193eda14cbcSMatt Macy  * collision) nopwrite is no worse than simply writing to the file.
3194eda14cbcSMatt Macy  * That said, there are no known attacks against the checksum algorithms
3195eda14cbcSMatt Macy  * used for nopwrite, assuming that the salt and the checksums
3196eda14cbcSMatt Macy  * themselves remain secret.
3197eda14cbcSMatt Macy  */
3198eda14cbcSMatt Macy static zio_t *
3199eda14cbcSMatt Macy zio_nop_write(zio_t *zio)
3200eda14cbcSMatt Macy {
3201eda14cbcSMatt Macy 	blkptr_t *bp = zio->io_bp;
3202eda14cbcSMatt Macy 	blkptr_t *bp_orig = &zio->io_bp_orig;
3203eda14cbcSMatt Macy 	zio_prop_t *zp = &zio->io_prop;
3204eda14cbcSMatt Macy 
3205bb2d13b6SMartin Matuska 	ASSERT(BP_IS_HOLE(bp));
3206eda14cbcSMatt Macy 	ASSERT(BP_GET_LEVEL(bp) == 0);
3207eda14cbcSMatt Macy 	ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
3208eda14cbcSMatt Macy 	ASSERT(zp->zp_nopwrite);
3209eda14cbcSMatt Macy 	ASSERT(!zp->zp_dedup);
3210eda14cbcSMatt Macy 	ASSERT(zio->io_bp_override == NULL);
3211eda14cbcSMatt Macy 	ASSERT(IO_IS_ALLOCATING(zio));
3212eda14cbcSMatt Macy 
3213eda14cbcSMatt Macy 	/*
3214eda14cbcSMatt Macy 	 * Check to see if the original bp and the new bp have matching
3215eda14cbcSMatt Macy 	 * characteristics (i.e. same checksum, compression algorithms, etc).
3216eda14cbcSMatt Macy 	 * If they don't then just continue with the pipeline which will
3217eda14cbcSMatt Macy 	 * allocate a new bp.
3218eda14cbcSMatt Macy 	 */
3219eda14cbcSMatt Macy 	if (BP_IS_HOLE(bp_orig) ||
3220eda14cbcSMatt Macy 	    !(zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_flags &
3221eda14cbcSMatt Macy 	    ZCHECKSUM_FLAG_NOPWRITE) ||
3222eda14cbcSMatt Macy 	    BP_IS_ENCRYPTED(bp) || BP_IS_ENCRYPTED(bp_orig) ||
3223eda14cbcSMatt Macy 	    BP_GET_CHECKSUM(bp) != BP_GET_CHECKSUM(bp_orig) ||
3224eda14cbcSMatt Macy 	    BP_GET_COMPRESS(bp) != BP_GET_COMPRESS(bp_orig) ||
3225eda14cbcSMatt Macy 	    BP_GET_DEDUP(bp) != BP_GET_DEDUP(bp_orig) ||
3226eda14cbcSMatt Macy 	    zp->zp_copies != BP_GET_NDVAS(bp_orig))
3227eda14cbcSMatt Macy 		return (zio);
3228eda14cbcSMatt Macy 
3229eda14cbcSMatt Macy 	/*
3230eda14cbcSMatt Macy 	 * If the checksums match then reset the pipeline so that we
3231eda14cbcSMatt Macy 	 * avoid allocating a new bp and issuing any I/O.
3232eda14cbcSMatt Macy 	 */
3233eda14cbcSMatt Macy 	if (ZIO_CHECKSUM_EQUAL(bp->blk_cksum, bp_orig->blk_cksum)) {
3234eda14cbcSMatt Macy 		ASSERT(zio_checksum_table[zp->zp_checksum].ci_flags &
3235eda14cbcSMatt Macy 		    ZCHECKSUM_FLAG_NOPWRITE);
3236eda14cbcSMatt Macy 		ASSERT3U(BP_GET_PSIZE(bp), ==, BP_GET_PSIZE(bp_orig));
3237eda14cbcSMatt Macy 		ASSERT3U(BP_GET_LSIZE(bp), ==, BP_GET_LSIZE(bp_orig));
3238eda14cbcSMatt Macy 		ASSERT(zp->zp_compress != ZIO_COMPRESS_OFF);
3239bb2d13b6SMartin Matuska 		ASSERT3U(bp->blk_prop, ==, bp_orig->blk_prop);
3240eda14cbcSMatt Macy 
3241eda14cbcSMatt Macy 		/*
3242eda14cbcSMatt Macy 		 * If we're overwriting a block that is currently on an
3243eda14cbcSMatt Macy 		 * indirect vdev, then ignore the nopwrite request and
3244eda14cbcSMatt Macy 		 * allow a new block to be allocated on a concrete vdev.
3245eda14cbcSMatt Macy 		 */
3246eda14cbcSMatt Macy 		spa_config_enter(zio->io_spa, SCL_VDEV, FTAG, RW_READER);
3247bb2d13b6SMartin Matuska 		for (int d = 0; d < BP_GET_NDVAS(bp_orig); d++) {
3248eda14cbcSMatt Macy 			vdev_t *tvd = vdev_lookup_top(zio->io_spa,
3249bb2d13b6SMartin Matuska 			    DVA_GET_VDEV(&bp_orig->blk_dva[d]));
3250eda14cbcSMatt Macy 			if (tvd->vdev_ops == &vdev_indirect_ops) {
3251eda14cbcSMatt Macy 				spa_config_exit(zio->io_spa, SCL_VDEV, FTAG);
3252eda14cbcSMatt Macy 				return (zio);
3253eda14cbcSMatt Macy 			}
3254bb2d13b6SMartin Matuska 		}
3255eda14cbcSMatt Macy 		spa_config_exit(zio->io_spa, SCL_VDEV, FTAG);
3256eda14cbcSMatt Macy 
3257eda14cbcSMatt Macy 		*bp = *bp_orig;
3258eda14cbcSMatt Macy 		zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
3259eda14cbcSMatt Macy 		zio->io_flags |= ZIO_FLAG_NOPWRITE;
3260eda14cbcSMatt Macy 	}
3261eda14cbcSMatt Macy 
3262eda14cbcSMatt Macy 	return (zio);
3263eda14cbcSMatt Macy }
3264eda14cbcSMatt Macy 
3265eda14cbcSMatt Macy /*
3266eda14cbcSMatt Macy  * ==========================================================================
32672a58b312SMartin Matuska  * Block Reference Table
32682a58b312SMartin Matuska  * ==========================================================================
32692a58b312SMartin Matuska  */
32702a58b312SMartin Matuska static zio_t *
32712a58b312SMartin Matuska zio_brt_free(zio_t *zio)
32722a58b312SMartin Matuska {
32732a58b312SMartin Matuska 	blkptr_t *bp;
32742a58b312SMartin Matuska 
32752a58b312SMartin Matuska 	bp = zio->io_bp;
32762a58b312SMartin Matuska 
32772a58b312SMartin Matuska 	if (BP_GET_LEVEL(bp) > 0 ||
32782a58b312SMartin Matuska 	    BP_IS_METADATA(bp) ||
32792a58b312SMartin Matuska 	    !brt_maybe_exists(zio->io_spa, bp)) {
32802a58b312SMartin Matuska 		return (zio);
32812a58b312SMartin Matuska 	}
32822a58b312SMartin Matuska 
32832a58b312SMartin Matuska 	if (!brt_entry_decref(zio->io_spa, bp)) {
32842a58b312SMartin Matuska 		/*
32852a58b312SMartin Matuska 		 * This isn't the last reference, so we cannot free
32862a58b312SMartin Matuska 		 * the data yet.
32872a58b312SMartin Matuska 		 */
32882a58b312SMartin Matuska 		zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
32892a58b312SMartin Matuska 	}
32902a58b312SMartin Matuska 
32912a58b312SMartin Matuska 	return (zio);
32922a58b312SMartin Matuska }
32932a58b312SMartin Matuska 
32942a58b312SMartin Matuska /*
32952a58b312SMartin Matuska  * ==========================================================================
3296eda14cbcSMatt Macy  * Dedup
3297eda14cbcSMatt Macy  * ==========================================================================
3298eda14cbcSMatt Macy  */
3299eda14cbcSMatt Macy static void
3300eda14cbcSMatt Macy zio_ddt_child_read_done(zio_t *zio)
3301eda14cbcSMatt Macy {
3302eda14cbcSMatt Macy 	blkptr_t *bp = zio->io_bp;
3303e2df9bb4SMartin Matuska 	ddt_t *ddt;
3304eda14cbcSMatt Macy 	ddt_entry_t *dde = zio->io_private;
3305eda14cbcSMatt Macy 	zio_t *pio = zio_unique_parent(zio);
3306eda14cbcSMatt Macy 
3307eda14cbcSMatt Macy 	mutex_enter(&pio->io_lock);
3308e2df9bb4SMartin Matuska 	ddt = ddt_select(zio->io_spa, bp);
3309eda14cbcSMatt Macy 
3310e2df9bb4SMartin Matuska 	if (zio->io_error == 0) {
3311e2df9bb4SMartin Matuska 		ddt_phys_variant_t v = ddt_phys_select(ddt, dde, bp);
3312e2df9bb4SMartin Matuska 		/* this phys variant doesn't need repair */
3313e2df9bb4SMartin Matuska 		ddt_phys_clear(dde->dde_phys, v);
3314e2df9bb4SMartin Matuska 	}
3315e2df9bb4SMartin Matuska 
3316e2df9bb4SMartin Matuska 	if (zio->io_error == 0 && dde->dde_io->dde_repair_abd == NULL)
3317e2df9bb4SMartin Matuska 		dde->dde_io->dde_repair_abd = zio->io_abd;
3318eda14cbcSMatt Macy 	else
3319eda14cbcSMatt Macy 		abd_free(zio->io_abd);
3320eda14cbcSMatt Macy 	mutex_exit(&pio->io_lock);
3321eda14cbcSMatt Macy }
3322eda14cbcSMatt Macy 
3323eda14cbcSMatt Macy static zio_t *
3324eda14cbcSMatt Macy zio_ddt_read_start(zio_t *zio)
3325eda14cbcSMatt Macy {
3326eda14cbcSMatt Macy 	blkptr_t *bp = zio->io_bp;
3327eda14cbcSMatt Macy 
3328eda14cbcSMatt Macy 	ASSERT(BP_GET_DEDUP(bp));
3329eda14cbcSMatt Macy 	ASSERT(BP_GET_PSIZE(bp) == zio->io_size);
3330eda14cbcSMatt Macy 	ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
3331eda14cbcSMatt Macy 
3332eda14cbcSMatt Macy 	if (zio->io_child_error[ZIO_CHILD_DDT]) {
3333eda14cbcSMatt Macy 		ddt_t *ddt = ddt_select(zio->io_spa, bp);
3334eda14cbcSMatt Macy 		ddt_entry_t *dde = ddt_repair_start(ddt, bp);
3335e2df9bb4SMartin Matuska 		ddt_phys_variant_t v_self = ddt_phys_select(ddt, dde, bp);
3336e2df9bb4SMartin Matuska 		ddt_univ_phys_t *ddp = dde->dde_phys;
3337eda14cbcSMatt Macy 		blkptr_t blk;
3338eda14cbcSMatt Macy 
3339eda14cbcSMatt Macy 		ASSERT(zio->io_vsd == NULL);
3340eda14cbcSMatt Macy 		zio->io_vsd = dde;
3341eda14cbcSMatt Macy 
3342e2df9bb4SMartin Matuska 		if (v_self == DDT_PHYS_NONE)
3343eda14cbcSMatt Macy 			return (zio);
3344eda14cbcSMatt Macy 
3345e2df9bb4SMartin Matuska 		/* issue I/O for the other copies */
3346e2df9bb4SMartin Matuska 		for (int p = 0; p < DDT_NPHYS(ddt); p++) {
3347e2df9bb4SMartin Matuska 			ddt_phys_variant_t v = DDT_PHYS_VARIANT(ddt, p);
3348e2df9bb4SMartin Matuska 
3349e2df9bb4SMartin Matuska 			if (ddt_phys_birth(ddp, v) == 0 || v == v_self)
3350eda14cbcSMatt Macy 				continue;
3351e2df9bb4SMartin Matuska 
3352e2df9bb4SMartin Matuska 			ddt_bp_create(ddt->ddt_checksum, &dde->dde_key,
3353e2df9bb4SMartin Matuska 			    ddp, v, &blk);
3354eda14cbcSMatt Macy 			zio_nowait(zio_read(zio, zio->io_spa, &blk,
3355eda14cbcSMatt Macy 			    abd_alloc_for_io(zio->io_size, B_TRUE),
3356eda14cbcSMatt Macy 			    zio->io_size, zio_ddt_child_read_done, dde,
3357eda14cbcSMatt Macy 			    zio->io_priority, ZIO_DDT_CHILD_FLAGS(zio) |
3358eda14cbcSMatt Macy 			    ZIO_FLAG_DONT_PROPAGATE, &zio->io_bookmark));
3359eda14cbcSMatt Macy 		}
3360eda14cbcSMatt Macy 		return (zio);
3361eda14cbcSMatt Macy 	}
3362eda14cbcSMatt Macy 
3363eda14cbcSMatt Macy 	zio_nowait(zio_read(zio, zio->io_spa, bp,
3364eda14cbcSMatt Macy 	    zio->io_abd, zio->io_size, NULL, NULL, zio->io_priority,
3365eda14cbcSMatt Macy 	    ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark));
3366eda14cbcSMatt Macy 
3367eda14cbcSMatt Macy 	return (zio);
3368eda14cbcSMatt Macy }
3369eda14cbcSMatt Macy 
3370eda14cbcSMatt Macy static zio_t *
3371eda14cbcSMatt Macy zio_ddt_read_done(zio_t *zio)
3372eda14cbcSMatt Macy {
3373eda14cbcSMatt Macy 	blkptr_t *bp = zio->io_bp;
3374eda14cbcSMatt Macy 
3375eda14cbcSMatt Macy 	if (zio_wait_for_children(zio, ZIO_CHILD_DDT_BIT, ZIO_WAIT_DONE)) {
3376eda14cbcSMatt Macy 		return (NULL);
3377eda14cbcSMatt Macy 	}
3378eda14cbcSMatt Macy 
3379eda14cbcSMatt Macy 	ASSERT(BP_GET_DEDUP(bp));
3380eda14cbcSMatt Macy 	ASSERT(BP_GET_PSIZE(bp) == zio->io_size);
3381eda14cbcSMatt Macy 	ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
3382eda14cbcSMatt Macy 
3383eda14cbcSMatt Macy 	if (zio->io_child_error[ZIO_CHILD_DDT]) {
3384eda14cbcSMatt Macy 		ddt_t *ddt = ddt_select(zio->io_spa, bp);
3385eda14cbcSMatt Macy 		ddt_entry_t *dde = zio->io_vsd;
3386eda14cbcSMatt Macy 		if (ddt == NULL) {
3387eda14cbcSMatt Macy 			ASSERT(spa_load_state(zio->io_spa) != SPA_LOAD_NONE);
3388eda14cbcSMatt Macy 			return (zio);
3389eda14cbcSMatt Macy 		}
3390eda14cbcSMatt Macy 		if (dde == NULL) {
3391eda14cbcSMatt Macy 			zio->io_stage = ZIO_STAGE_DDT_READ_START >> 1;
3392eda14cbcSMatt Macy 			zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE);
3393eda14cbcSMatt Macy 			return (NULL);
3394eda14cbcSMatt Macy 		}
3395e2df9bb4SMartin Matuska 		if (dde->dde_io->dde_repair_abd != NULL) {
3396e2df9bb4SMartin Matuska 			abd_copy(zio->io_abd, dde->dde_io->dde_repair_abd,
3397eda14cbcSMatt Macy 			    zio->io_size);
3398eda14cbcSMatt Macy 			zio->io_child_error[ZIO_CHILD_DDT] = 0;
3399eda14cbcSMatt Macy 		}
3400eda14cbcSMatt Macy 		ddt_repair_done(ddt, dde);
3401eda14cbcSMatt Macy 		zio->io_vsd = NULL;
3402eda14cbcSMatt Macy 	}
3403eda14cbcSMatt Macy 
3404eda14cbcSMatt Macy 	ASSERT(zio->io_vsd == NULL);
3405eda14cbcSMatt Macy 
3406eda14cbcSMatt Macy 	return (zio);
3407eda14cbcSMatt Macy }
3408eda14cbcSMatt Macy 
3409eda14cbcSMatt Macy static boolean_t
3410eda14cbcSMatt Macy zio_ddt_collision(zio_t *zio, ddt_t *ddt, ddt_entry_t *dde)
3411eda14cbcSMatt Macy {
3412eda14cbcSMatt Macy 	spa_t *spa = zio->io_spa;
3413eda14cbcSMatt Macy 	boolean_t do_raw = !!(zio->io_flags & ZIO_FLAG_RAW);
3414eda14cbcSMatt Macy 
3415eda14cbcSMatt Macy 	ASSERT(!(zio->io_bp_override && do_raw));
3416eda14cbcSMatt Macy 
3417eda14cbcSMatt Macy 	/*
3418eda14cbcSMatt Macy 	 * Note: we compare the original data, not the transformed data,
3419eda14cbcSMatt Macy 	 * because when zio->io_bp is an override bp, we will not have
3420eda14cbcSMatt Macy 	 * pushed the I/O transforms.  That's an important optimization
3421eda14cbcSMatt Macy 	 * because otherwise we'd compress/encrypt all dmu_sync() data twice.
3422eda14cbcSMatt Macy 	 * However, we should never get a raw, override zio so in these
3423eda14cbcSMatt Macy 	 * cases we can compare the io_abd directly. This is useful because
3424eda14cbcSMatt Macy 	 * it allows us to do dedup verification even if we don't have access
3425eda14cbcSMatt Macy 	 * to the original data (for instance, if the encryption keys aren't
3426eda14cbcSMatt Macy 	 * loaded).
3427eda14cbcSMatt Macy 	 */
3428eda14cbcSMatt Macy 
3429e2df9bb4SMartin Matuska 	for (int p = 0; p < DDT_NPHYS(ddt); p++) {
3430e2df9bb4SMartin Matuska 		if (DDT_PHYS_IS_DITTO(ddt, p))
3431e2df9bb4SMartin Matuska 			continue;
3432eda14cbcSMatt Macy 
3433e2df9bb4SMartin Matuska 		if (dde->dde_io == NULL)
3434e2df9bb4SMartin Matuska 			continue;
3435e2df9bb4SMartin Matuska 
3436e2df9bb4SMartin Matuska 		zio_t *lio = dde->dde_io->dde_lead_zio[p];
3437e2df9bb4SMartin Matuska 		if (lio == NULL)
3438e2df9bb4SMartin Matuska 			continue;
3439e2df9bb4SMartin Matuska 
3440e2df9bb4SMartin Matuska 		if (do_raw)
3441eda14cbcSMatt Macy 			return (lio->io_size != zio->io_size ||
3442eda14cbcSMatt Macy 			    abd_cmp(zio->io_abd, lio->io_abd) != 0);
3443e2df9bb4SMartin Matuska 
3444eda14cbcSMatt Macy 		return (lio->io_orig_size != zio->io_orig_size ||
3445eda14cbcSMatt Macy 		    abd_cmp(zio->io_orig_abd, lio->io_orig_abd) != 0);
3446eda14cbcSMatt Macy 	}
3447eda14cbcSMatt Macy 
3448e2df9bb4SMartin Matuska 	for (int p = 0; p < DDT_NPHYS(ddt); p++) {
3449e2df9bb4SMartin Matuska 		ddt_phys_variant_t v = DDT_PHYS_VARIANT(ddt, p);
3450e2df9bb4SMartin Matuska 		uint64_t phys_birth = ddt_phys_birth(dde->dde_phys, v);
3451eda14cbcSMatt Macy 
3452e2df9bb4SMartin Matuska 		if (phys_birth != 0 && do_raw) {
3453eda14cbcSMatt Macy 			blkptr_t blk = *zio->io_bp;
3454eda14cbcSMatt Macy 			uint64_t psize;
3455eda14cbcSMatt Macy 			abd_t *tmpabd;
3456eda14cbcSMatt Macy 			int error;
3457eda14cbcSMatt Macy 
3458e2df9bb4SMartin Matuska 			ddt_bp_fill(dde->dde_phys, v, &blk, phys_birth);
3459eda14cbcSMatt Macy 			psize = BP_GET_PSIZE(&blk);
3460eda14cbcSMatt Macy 
3461eda14cbcSMatt Macy 			if (psize != zio->io_size)
3462eda14cbcSMatt Macy 				return (B_TRUE);
3463eda14cbcSMatt Macy 
3464eda14cbcSMatt Macy 			ddt_exit(ddt);
3465eda14cbcSMatt Macy 
3466eda14cbcSMatt Macy 			tmpabd = abd_alloc_for_io(psize, B_TRUE);
3467eda14cbcSMatt Macy 
3468eda14cbcSMatt Macy 			error = zio_wait(zio_read(NULL, spa, &blk, tmpabd,
3469eda14cbcSMatt Macy 			    psize, NULL, NULL, ZIO_PRIORITY_SYNC_READ,
3470eda14cbcSMatt Macy 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
3471eda14cbcSMatt Macy 			    ZIO_FLAG_RAW, &zio->io_bookmark));
3472eda14cbcSMatt Macy 
3473eda14cbcSMatt Macy 			if (error == 0) {
3474eda14cbcSMatt Macy 				if (abd_cmp(tmpabd, zio->io_abd) != 0)
3475eda14cbcSMatt Macy 					error = SET_ERROR(ENOENT);
3476eda14cbcSMatt Macy 			}
3477eda14cbcSMatt Macy 
3478eda14cbcSMatt Macy 			abd_free(tmpabd);
3479eda14cbcSMatt Macy 			ddt_enter(ddt);
3480eda14cbcSMatt Macy 			return (error != 0);
3481e2df9bb4SMartin Matuska 		} else if (phys_birth != 0) {
3482eda14cbcSMatt Macy 			arc_buf_t *abuf = NULL;
3483eda14cbcSMatt Macy 			arc_flags_t aflags = ARC_FLAG_WAIT;
3484eda14cbcSMatt Macy 			blkptr_t blk = *zio->io_bp;
3485eda14cbcSMatt Macy 			int error;
3486eda14cbcSMatt Macy 
3487e2df9bb4SMartin Matuska 			ddt_bp_fill(dde->dde_phys, v, &blk, phys_birth);
3488eda14cbcSMatt Macy 
3489eda14cbcSMatt Macy 			if (BP_GET_LSIZE(&blk) != zio->io_orig_size)
3490eda14cbcSMatt Macy 				return (B_TRUE);
3491eda14cbcSMatt Macy 
3492eda14cbcSMatt Macy 			ddt_exit(ddt);
3493eda14cbcSMatt Macy 
3494eda14cbcSMatt Macy 			error = arc_read(NULL, spa, &blk,
3495eda14cbcSMatt Macy 			    arc_getbuf_func, &abuf, ZIO_PRIORITY_SYNC_READ,
3496eda14cbcSMatt Macy 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
3497eda14cbcSMatt Macy 			    &aflags, &zio->io_bookmark);
3498eda14cbcSMatt Macy 
3499eda14cbcSMatt Macy 			if (error == 0) {
3500eda14cbcSMatt Macy 				if (abd_cmp_buf(zio->io_orig_abd, abuf->b_data,
3501eda14cbcSMatt Macy 				    zio->io_orig_size) != 0)
3502eda14cbcSMatt Macy 					error = SET_ERROR(ENOENT);
3503eda14cbcSMatt Macy 				arc_buf_destroy(abuf, &abuf);
3504eda14cbcSMatt Macy 			}
3505eda14cbcSMatt Macy 
3506eda14cbcSMatt Macy 			ddt_enter(ddt);
3507eda14cbcSMatt Macy 			return (error != 0);
3508eda14cbcSMatt Macy 		}
3509eda14cbcSMatt Macy 	}
3510eda14cbcSMatt Macy 
3511eda14cbcSMatt Macy 	return (B_FALSE);
3512eda14cbcSMatt Macy }
3513eda14cbcSMatt Macy 
3514eda14cbcSMatt Macy static void
3515e2df9bb4SMartin Matuska zio_ddt_child_write_done(zio_t *zio)
3516eda14cbcSMatt Macy {
3517eda14cbcSMatt Macy 	ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp);
3518eda14cbcSMatt Macy 	ddt_entry_t *dde = zio->io_private;
3519eda14cbcSMatt Macy 
3520e2df9bb4SMartin Matuska 	zio_link_t *zl = NULL;
3521e2df9bb4SMartin Matuska 	ASSERT3P(zio_walk_parents(zio, &zl), !=, NULL);
3522e2df9bb4SMartin Matuska 
3523e2df9bb4SMartin Matuska 	int p = DDT_PHYS_FOR_COPIES(ddt, zio->io_prop.zp_copies);
3524e2df9bb4SMartin Matuska 	ddt_phys_variant_t v = DDT_PHYS_VARIANT(ddt, p);
3525e2df9bb4SMartin Matuska 	ddt_univ_phys_t *ddp = dde->dde_phys;
3526eda14cbcSMatt Macy 
3527eda14cbcSMatt Macy 	ddt_enter(ddt);
3528eda14cbcSMatt Macy 
3529e2df9bb4SMartin Matuska 	/* we're the lead, so once we're done there's no one else outstanding */
3530e2df9bb4SMartin Matuska 	if (dde->dde_io->dde_lead_zio[p] == zio)
3531e2df9bb4SMartin Matuska 		dde->dde_io->dde_lead_zio[p] = NULL;
3532eda14cbcSMatt Macy 
3533e2df9bb4SMartin Matuska 	ddt_univ_phys_t *orig = &dde->dde_io->dde_orig_phys;
3534eda14cbcSMatt Macy 
3535e2df9bb4SMartin Matuska 	if (zio->io_error != 0) {
3536e2df9bb4SMartin Matuska 		/*
3537e2df9bb4SMartin Matuska 		 * The write failed, so we're about to abort the entire IO
3538e2df9bb4SMartin Matuska 		 * chain. We need to revert the entry back to what it was at
3539e2df9bb4SMartin Matuska 		 * the last time it was successfully extended.
3540e2df9bb4SMartin Matuska 		 */
3541e2df9bb4SMartin Matuska 		ddt_phys_copy(ddp, orig, v);
3542e2df9bb4SMartin Matuska 		ddt_phys_clear(orig, v);
3543e2df9bb4SMartin Matuska 
3544e2df9bb4SMartin Matuska 		ddt_exit(ddt);
3545e2df9bb4SMartin Matuska 		return;
3546e2df9bb4SMartin Matuska 	}
3547e2df9bb4SMartin Matuska 
3548e2df9bb4SMartin Matuska 	/*
3549e2df9bb4SMartin Matuska 	 * We've successfully added new DVAs to the entry. Clear the saved
3550e2df9bb4SMartin Matuska 	 * state or, if there's still outstanding IO, remember it so we can
3551e2df9bb4SMartin Matuska 	 * revert to a known good state if that IO fails.
3552e2df9bb4SMartin Matuska 	 */
3553e2df9bb4SMartin Matuska 	if (dde->dde_io->dde_lead_zio[p] == NULL)
3554e2df9bb4SMartin Matuska 		ddt_phys_clear(orig, v);
3555e2df9bb4SMartin Matuska 	else
3556e2df9bb4SMartin Matuska 		ddt_phys_copy(orig, ddp, v);
3557e2df9bb4SMartin Matuska 
3558e2df9bb4SMartin Matuska 	/*
3559e2df9bb4SMartin Matuska 	 * Add references for all dedup writes that were waiting on the
3560e2df9bb4SMartin Matuska 	 * physical one, skipping any other physical writes that are waiting.
3561e2df9bb4SMartin Matuska 	 */
3562e2df9bb4SMartin Matuska 	zio_t *pio;
3563e2df9bb4SMartin Matuska 	zl = NULL;
3564e2df9bb4SMartin Matuska 	while ((pio = zio_walk_parents(zio, &zl)) != NULL) {
3565e2df9bb4SMartin Matuska 		if (!(pio->io_flags & ZIO_FLAG_DDT_CHILD))
3566e2df9bb4SMartin Matuska 			ddt_phys_addref(ddp, v);
3567e2df9bb4SMartin Matuska 	}
3568eda14cbcSMatt Macy 
3569eda14cbcSMatt Macy 	ddt_exit(ddt);
3570eda14cbcSMatt Macy }
3571eda14cbcSMatt Macy 
3572eda14cbcSMatt Macy static void
3573e2df9bb4SMartin Matuska zio_ddt_child_write_ready(zio_t *zio)
3574eda14cbcSMatt Macy {
3575eda14cbcSMatt Macy 	ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp);
3576eda14cbcSMatt Macy 	ddt_entry_t *dde = zio->io_private;
3577e2df9bb4SMartin Matuska 
3578e2df9bb4SMartin Matuska 	zio_link_t *zl = NULL;
3579e2df9bb4SMartin Matuska 	ASSERT3P(zio_walk_parents(zio, &zl), !=, NULL);
3580e2df9bb4SMartin Matuska 
3581e2df9bb4SMartin Matuska 	int p = DDT_PHYS_FOR_COPIES(ddt, zio->io_prop.zp_copies);
3582e2df9bb4SMartin Matuska 	ddt_phys_variant_t v = DDT_PHYS_VARIANT(ddt, p);
3583e2df9bb4SMartin Matuska 
3584e2df9bb4SMartin Matuska 	if (zio->io_error != 0)
3585e2df9bb4SMartin Matuska 		return;
3586eda14cbcSMatt Macy 
3587eda14cbcSMatt Macy 	ddt_enter(ddt);
3588eda14cbcSMatt Macy 
3589e2df9bb4SMartin Matuska 	ddt_phys_extend(dde->dde_phys, v, zio->io_bp);
3590eda14cbcSMatt Macy 
3591e2df9bb4SMartin Matuska 	zio_t *pio;
3592e2df9bb4SMartin Matuska 	zl = NULL;
3593e2df9bb4SMartin Matuska 	while ((pio = zio_walk_parents(zio, &zl)) != NULL) {
3594e2df9bb4SMartin Matuska 		if (!(pio->io_flags & ZIO_FLAG_DDT_CHILD))
3595e2df9bb4SMartin Matuska 			ddt_bp_fill(dde->dde_phys, v, pio->io_bp, zio->io_txg);
3596eda14cbcSMatt Macy 	}
3597eda14cbcSMatt Macy 
3598eda14cbcSMatt Macy 	ddt_exit(ddt);
3599eda14cbcSMatt Macy }
3600eda14cbcSMatt Macy 
3601eda14cbcSMatt Macy static zio_t *
3602eda14cbcSMatt Macy zio_ddt_write(zio_t *zio)
3603eda14cbcSMatt Macy {
3604eda14cbcSMatt Macy 	spa_t *spa = zio->io_spa;
3605eda14cbcSMatt Macy 	blkptr_t *bp = zio->io_bp;
3606eda14cbcSMatt Macy 	uint64_t txg = zio->io_txg;
3607eda14cbcSMatt Macy 	zio_prop_t *zp = &zio->io_prop;
3608eda14cbcSMatt Macy 	ddt_t *ddt = ddt_select(spa, bp);
3609eda14cbcSMatt Macy 	ddt_entry_t *dde;
3610eda14cbcSMatt Macy 
3611eda14cbcSMatt Macy 	ASSERT(BP_GET_DEDUP(bp));
3612eda14cbcSMatt Macy 	ASSERT(BP_GET_CHECKSUM(bp) == zp->zp_checksum);
3613eda14cbcSMatt Macy 	ASSERT(BP_IS_HOLE(bp) || zio->io_bp_override);
3614eda14cbcSMatt Macy 	ASSERT(!(zio->io_bp_override && (zio->io_flags & ZIO_FLAG_RAW)));
36157a7741afSMartin Matuska 	/*
36167a7741afSMartin Matuska 	 * Deduplication will not take place for Direct I/O writes. The
36177a7741afSMartin Matuska 	 * ddt_tree will be emptied in syncing context. Direct I/O writes take
36187a7741afSMartin Matuska 	 * place in the open-context. Direct I/O write can not attempt to
36197a7741afSMartin Matuska 	 * modify the ddt_tree while issuing out a write.
36207a7741afSMartin Matuska 	 */
36217a7741afSMartin Matuska 	ASSERT3B(zio->io_prop.zp_direct_write, ==, B_FALSE);
3622eda14cbcSMatt Macy 
3623eda14cbcSMatt Macy 	ddt_enter(ddt);
3624e2df9bb4SMartin Matuska 	dde = ddt_lookup(ddt, bp);
3625ce4dcb97SMartin Matuska 	if (dde == NULL) {
3626ce4dcb97SMartin Matuska 		/* DDT size is over its quota so no new entries */
3627ce4dcb97SMartin Matuska 		zp->zp_dedup = B_FALSE;
3628ce4dcb97SMartin Matuska 		BP_SET_DEDUP(bp, B_FALSE);
3629ce4dcb97SMartin Matuska 		if (zio->io_bp_override == NULL)
3630ce4dcb97SMartin Matuska 			zio->io_pipeline = ZIO_WRITE_PIPELINE;
3631ce4dcb97SMartin Matuska 		ddt_exit(ddt);
3632ce4dcb97SMartin Matuska 		return (zio);
3633ce4dcb97SMartin Matuska 	}
3634eda14cbcSMatt Macy 
3635eda14cbcSMatt Macy 	if (zp->zp_dedup_verify && zio_ddt_collision(zio, ddt, dde)) {
3636eda14cbcSMatt Macy 		/*
3637eda14cbcSMatt Macy 		 * If we're using a weak checksum, upgrade to a strong checksum
3638eda14cbcSMatt Macy 		 * and try again.  If we're already using a strong checksum,
3639eda14cbcSMatt Macy 		 * we can't resolve it, so just convert to an ordinary write.
3640eda14cbcSMatt Macy 		 * (And automatically e-mail a paper to Nature?)
3641eda14cbcSMatt Macy 		 */
3642eda14cbcSMatt Macy 		if (!(zio_checksum_table[zp->zp_checksum].ci_flags &
3643eda14cbcSMatt Macy 		    ZCHECKSUM_FLAG_DEDUP)) {
3644eda14cbcSMatt Macy 			zp->zp_checksum = spa_dedup_checksum(spa);
3645eda14cbcSMatt Macy 			zio_pop_transforms(zio);
3646eda14cbcSMatt Macy 			zio->io_stage = ZIO_STAGE_OPEN;
3647eda14cbcSMatt Macy 			BP_ZERO(bp);
3648eda14cbcSMatt Macy 		} else {
3649eda14cbcSMatt Macy 			zp->zp_dedup = B_FALSE;
3650eda14cbcSMatt Macy 			BP_SET_DEDUP(bp, B_FALSE);
3651eda14cbcSMatt Macy 		}
3652eda14cbcSMatt Macy 		ASSERT(!BP_GET_DEDUP(bp));
3653eda14cbcSMatt Macy 		zio->io_pipeline = ZIO_WRITE_PIPELINE;
3654eda14cbcSMatt Macy 		ddt_exit(ddt);
3655eda14cbcSMatt Macy 		return (zio);
3656eda14cbcSMatt Macy 	}
3657eda14cbcSMatt Macy 
3658e2df9bb4SMartin Matuska 	int p = DDT_PHYS_FOR_COPIES(ddt, zp->zp_copies);
3659e2df9bb4SMartin Matuska 	ddt_phys_variant_t v = DDT_PHYS_VARIANT(ddt, p);
3660e2df9bb4SMartin Matuska 	ddt_univ_phys_t *ddp = dde->dde_phys;
3661e2df9bb4SMartin Matuska 
3662e2df9bb4SMartin Matuska 	/*
3663e2df9bb4SMartin Matuska 	 * In the common cases, at this point we have a regular BP with no
3664e2df9bb4SMartin Matuska 	 * allocated DVAs, and the corresponding DDT entry for its checksum.
3665e2df9bb4SMartin Matuska 	 * Our goal is to fill the BP with enough DVAs to satisfy its copies=
3666e2df9bb4SMartin Matuska 	 * requirement.
3667e2df9bb4SMartin Matuska 	 *
3668e2df9bb4SMartin Matuska 	 * One of three things needs to happen to fulfill this:
3669e2df9bb4SMartin Matuska 	 *
3670e2df9bb4SMartin Matuska 	 * - if the DDT entry has enough DVAs to satisfy the BP, we just copy
3671e2df9bb4SMartin Matuska 	 *   them out of the entry and return;
3672e2df9bb4SMartin Matuska 	 *
3673e2df9bb4SMartin Matuska 	 * - if the DDT entry has no DVAs (ie its brand new), then we have to
3674e2df9bb4SMartin Matuska 	 *   issue the write as normal so that DVAs can be allocated and the
3675e2df9bb4SMartin Matuska 	 *   data land on disk. We then copy the DVAs into the DDT entry on
3676e2df9bb4SMartin Matuska 	 *   return.
3677e2df9bb4SMartin Matuska 	 *
3678e2df9bb4SMartin Matuska 	 * - if the DDT entry has some DVAs, but too few, we have to issue the
3679e2df9bb4SMartin Matuska 	 *   write, adjusted to have allocate fewer copies. When it returns, we
3680e2df9bb4SMartin Matuska 	 *   add the new DVAs to the DDT entry, and update the BP to have the
3681e2df9bb4SMartin Matuska 	 *   full amount it originally requested.
3682e2df9bb4SMartin Matuska 	 *
3683e2df9bb4SMartin Matuska 	 * In all cases, if there's already a writing IO in flight, we need to
3684e2df9bb4SMartin Matuska 	 * defer the action until after the write is done. If our action is to
3685e2df9bb4SMartin Matuska 	 * write, we need to adjust our request for additional DVAs to match
3686e2df9bb4SMartin Matuska 	 * what will be in the DDT entry after it completes. In this way every
3687e2df9bb4SMartin Matuska 	 * IO can be guaranteed to recieve enough DVAs simply by joining the
3688e2df9bb4SMartin Matuska 	 * end of the chain and letting the sequence play out.
3689e2df9bb4SMartin Matuska 	 */
3690e2df9bb4SMartin Matuska 
3691e2df9bb4SMartin Matuska 	/*
3692e2df9bb4SMartin Matuska 	 * Number of DVAs in the DDT entry. If the BP is encrypted we ignore
3693e2df9bb4SMartin Matuska 	 * the third one as normal.
3694e2df9bb4SMartin Matuska 	 */
3695e2df9bb4SMartin Matuska 	int have_dvas = ddt_phys_dva_count(ddp, v, BP_IS_ENCRYPTED(bp));
3696e2df9bb4SMartin Matuska 	IMPLY(have_dvas == 0, ddt_phys_birth(ddp, v) == 0);
3697e2df9bb4SMartin Matuska 
3698e2df9bb4SMartin Matuska 	/* Number of DVAs requested bya the IO. */
3699e2df9bb4SMartin Matuska 	uint8_t need_dvas = zp->zp_copies;
3700e2df9bb4SMartin Matuska 
3701e2df9bb4SMartin Matuska 	/*
3702e2df9bb4SMartin Matuska 	 * What we do next depends on whether or not there's IO outstanding that
3703e2df9bb4SMartin Matuska 	 * will update this entry.
3704e2df9bb4SMartin Matuska 	 */
3705e2df9bb4SMartin Matuska 	if (dde->dde_io == NULL || dde->dde_io->dde_lead_zio[p] == NULL) {
3706e2df9bb4SMartin Matuska 		/*
3707e2df9bb4SMartin Matuska 		 * No IO outstanding, so we only need to worry about ourselves.
3708e2df9bb4SMartin Matuska 		 */
3709e2df9bb4SMartin Matuska 
3710e2df9bb4SMartin Matuska 		/*
3711e2df9bb4SMartin Matuska 		 * Override BPs bring their own DVAs and their own problems.
3712e2df9bb4SMartin Matuska 		 */
3713e2df9bb4SMartin Matuska 		if (zio->io_bp_override) {
3714e2df9bb4SMartin Matuska 			/*
3715e2df9bb4SMartin Matuska 			 * For a brand-new entry, all the work has been done
3716e2df9bb4SMartin Matuska 			 * for us, and we can just fill it out from the provided
3717e2df9bb4SMartin Matuska 			 * block and leave.
3718e2df9bb4SMartin Matuska 			 */
3719e2df9bb4SMartin Matuska 			if (have_dvas == 0) {
3720783d3ff6SMartin Matuska 				ASSERT(BP_GET_LOGICAL_BIRTH(bp) == txg);
3721eda14cbcSMatt Macy 				ASSERT(BP_EQUAL(bp, zio->io_bp_override));
3722e2df9bb4SMartin Matuska 				ddt_phys_extend(ddp, v, bp);
3723e2df9bb4SMartin Matuska 				ddt_phys_addref(ddp, v);
3724e2df9bb4SMartin Matuska 				ddt_exit(ddt);
3725e2df9bb4SMartin Matuska 				return (zio);
3726e2df9bb4SMartin Matuska 			}
3727e2df9bb4SMartin Matuska 
3728e2df9bb4SMartin Matuska 			/*
3729e2df9bb4SMartin Matuska 			 * If we already have this entry, then we want to treat
3730e2df9bb4SMartin Matuska 			 * it like a regular write. To do this we just wipe
3731e2df9bb4SMartin Matuska 			 * them out and proceed like a regular write.
3732e2df9bb4SMartin Matuska 			 *
3733e2df9bb4SMartin Matuska 			 * Even if there are some DVAs in the entry, we still
3734e2df9bb4SMartin Matuska 			 * have to clear them out. We can't use them to fill
3735e2df9bb4SMartin Matuska 			 * out the dedup entry, as they are all referenced
3736e2df9bb4SMartin Matuska 			 * together by a bp already on disk, and will be freed
3737e2df9bb4SMartin Matuska 			 * as a group.
3738e2df9bb4SMartin Matuska 			 */
3739e2df9bb4SMartin Matuska 			BP_ZERO_DVAS(bp);
3740e2df9bb4SMartin Matuska 			BP_SET_BIRTH(bp, 0, 0);
3741e2df9bb4SMartin Matuska 		}
3742e2df9bb4SMartin Matuska 
3743e2df9bb4SMartin Matuska 		/*
3744e2df9bb4SMartin Matuska 		 * If there are enough DVAs in the entry to service our request,
3745e2df9bb4SMartin Matuska 		 * then we can just use them as-is.
3746e2df9bb4SMartin Matuska 		 */
3747e2df9bb4SMartin Matuska 		if (have_dvas >= need_dvas) {
3748e2df9bb4SMartin Matuska 			ddt_bp_fill(ddp, v, bp, txg);
3749e2df9bb4SMartin Matuska 			ddt_phys_addref(ddp, v);
3750e2df9bb4SMartin Matuska 			ddt_exit(ddt);
3751e2df9bb4SMartin Matuska 			return (zio);
3752e2df9bb4SMartin Matuska 		}
3753e2df9bb4SMartin Matuska 
3754e2df9bb4SMartin Matuska 		/*
3755e2df9bb4SMartin Matuska 		 * Otherwise, we have to issue IO to fill the entry up to the
3756e2df9bb4SMartin Matuska 		 * amount we need.
3757e2df9bb4SMartin Matuska 		 */
3758e2df9bb4SMartin Matuska 		need_dvas -= have_dvas;
3759eda14cbcSMatt Macy 	} else {
3760e2df9bb4SMartin Matuska 		/*
3761e2df9bb4SMartin Matuska 		 * There's a write in-flight. If there's already enough DVAs on
3762e2df9bb4SMartin Matuska 		 * the entry, then either there were already enough to start
3763e2df9bb4SMartin Matuska 		 * with, or the in-flight IO is between READY and DONE, and so
3764e2df9bb4SMartin Matuska 		 * has extended the entry with new DVAs. Either way, we don't
3765e2df9bb4SMartin Matuska 		 * need to do anything, we can just slot in behind it.
3766e2df9bb4SMartin Matuska 		 */
3767e2df9bb4SMartin Matuska 
3768e2df9bb4SMartin Matuska 		if (zio->io_bp_override) {
3769e2df9bb4SMartin Matuska 			/*
3770e2df9bb4SMartin Matuska 			 * If there's a write out, then we're soon going to
3771e2df9bb4SMartin Matuska 			 * have our own copies of this block, so clear out the
3772e2df9bb4SMartin Matuska 			 * override block and treat it as a regular dedup
3773e2df9bb4SMartin Matuska 			 * write. See comment above.
3774e2df9bb4SMartin Matuska 			 */
3775e2df9bb4SMartin Matuska 			BP_ZERO_DVAS(bp);
3776e2df9bb4SMartin Matuska 			BP_SET_BIRTH(bp, 0, 0);
3777e2df9bb4SMartin Matuska 		}
3778e2df9bb4SMartin Matuska 
3779e2df9bb4SMartin Matuska 		if (have_dvas >= need_dvas) {
3780e2df9bb4SMartin Matuska 			/*
3781e2df9bb4SMartin Matuska 			 * A minor point: there might already be enough
3782e2df9bb4SMartin Matuska 			 * committed DVAs in the entry to service our request,
3783e2df9bb4SMartin Matuska 			 * but we don't know which are completed and which are
3784e2df9bb4SMartin Matuska 			 * allocated but not yet written. In this case, should
3785e2df9bb4SMartin Matuska 			 * the IO for the new DVAs fail, we will be on the end
3786e2df9bb4SMartin Matuska 			 * of the IO chain and will also recieve an error, even
3787e2df9bb4SMartin Matuska 			 * though our request could have been serviced.
3788e2df9bb4SMartin Matuska 			 *
3789e2df9bb4SMartin Matuska 			 * This is an extremely rare case, as it requires the
3790e2df9bb4SMartin Matuska 			 * original block to be copied with a request for a
3791e2df9bb4SMartin Matuska 			 * larger number of DVAs, then copied again requesting
3792e2df9bb4SMartin Matuska 			 * the same (or already fulfilled) number of DVAs while
3793e2df9bb4SMartin Matuska 			 * the first request is active, and then that first
3794e2df9bb4SMartin Matuska 			 * request errors. In return, the logic required to
3795e2df9bb4SMartin Matuska 			 * catch and handle it is complex. For now, I'm just
3796e2df9bb4SMartin Matuska 			 * not going to bother with it.
3797e2df9bb4SMartin Matuska 			 */
3798e2df9bb4SMartin Matuska 
3799e2df9bb4SMartin Matuska 			/*
3800e2df9bb4SMartin Matuska 			 * We always fill the bp here as we may have arrived
3801e2df9bb4SMartin Matuska 			 * after the in-flight write has passed READY, and so
3802e2df9bb4SMartin Matuska 			 * missed out.
3803e2df9bb4SMartin Matuska 			 */
3804e2df9bb4SMartin Matuska 			ddt_bp_fill(ddp, v, bp, txg);
3805e2df9bb4SMartin Matuska 			zio_add_child(zio, dde->dde_io->dde_lead_zio[p]);
3806e2df9bb4SMartin Matuska 			ddt_exit(ddt);
3807e2df9bb4SMartin Matuska 			return (zio);
3808e2df9bb4SMartin Matuska 		}
3809e2df9bb4SMartin Matuska 
3810e2df9bb4SMartin Matuska 		/*
3811e2df9bb4SMartin Matuska 		 * There's not enough in the entry yet, so we need to look at
3812e2df9bb4SMartin Matuska 		 * the write in-flight and see how many DVAs it will have once
3813e2df9bb4SMartin Matuska 		 * it completes.
3814e2df9bb4SMartin Matuska 		 *
3815e2df9bb4SMartin Matuska 		 * The in-flight write has potentially had its copies request
3816e2df9bb4SMartin Matuska 		 * reduced (if we're filling out an existing entry), so we need
3817e2df9bb4SMartin Matuska 		 * to reach in and get the original write to find out what it is
3818e2df9bb4SMartin Matuska 		 * expecting.
3819e2df9bb4SMartin Matuska 		 *
3820e2df9bb4SMartin Matuska 		 * Note that the parent of the lead zio will always have the
3821e2df9bb4SMartin Matuska 		 * highest zp_copies of any zio in the chain, because ones that
3822e2df9bb4SMartin Matuska 		 * can be serviced without additional IO are always added to
3823e2df9bb4SMartin Matuska 		 * the back of the chain.
3824e2df9bb4SMartin Matuska 		 */
3825e2df9bb4SMartin Matuska 		zio_link_t *zl = NULL;
3826e2df9bb4SMartin Matuska 		zio_t *pio =
3827e2df9bb4SMartin Matuska 		    zio_walk_parents(dde->dde_io->dde_lead_zio[p], &zl);
3828e2df9bb4SMartin Matuska 		ASSERT(pio);
3829e2df9bb4SMartin Matuska 		uint8_t parent_dvas = pio->io_prop.zp_copies;
3830e2df9bb4SMartin Matuska 
3831e2df9bb4SMartin Matuska 		if (parent_dvas >= need_dvas) {
3832e2df9bb4SMartin Matuska 			zio_add_child(zio, dde->dde_io->dde_lead_zio[p]);
3833e2df9bb4SMartin Matuska 			ddt_exit(ddt);
3834e2df9bb4SMartin Matuska 			return (zio);
3835e2df9bb4SMartin Matuska 		}
3836e2df9bb4SMartin Matuska 
3837e2df9bb4SMartin Matuska 		/*
3838e2df9bb4SMartin Matuska 		 * Still not enough, so we will need to issue to get the
3839e2df9bb4SMartin Matuska 		 * shortfall.
3840e2df9bb4SMartin Matuska 		 */
3841e2df9bb4SMartin Matuska 		need_dvas -= parent_dvas;
3842e2df9bb4SMartin Matuska 	}
3843e2df9bb4SMartin Matuska 
3844e2df9bb4SMartin Matuska 	/*
3845e2df9bb4SMartin Matuska 	 * We need to write. We will create a new write with the copies
3846e2df9bb4SMartin Matuska 	 * property adjusted to match the number of DVAs we need to need to
3847e2df9bb4SMartin Matuska 	 * grow the DDT entry by to satisfy the request.
3848e2df9bb4SMartin Matuska 	 */
3849e2df9bb4SMartin Matuska 	zio_prop_t czp = *zp;
3850e2df9bb4SMartin Matuska 	czp.zp_copies = need_dvas;
3851e2df9bb4SMartin Matuska 	zio_t *cio = zio_write(zio, spa, txg, bp, zio->io_orig_abd,
3852e2df9bb4SMartin Matuska 	    zio->io_orig_size, zio->io_orig_size, &czp,
3853b7198dcfSMartin Matuska 	    zio_ddt_child_write_ready, NULL,
3854eda14cbcSMatt Macy 	    zio_ddt_child_write_done, dde, zio->io_priority,
3855eda14cbcSMatt Macy 	    ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark);
3856eda14cbcSMatt Macy 
3857eda14cbcSMatt Macy 	zio_push_transform(cio, zio->io_abd, zio->io_size, 0, NULL);
3858e2df9bb4SMartin Matuska 
3859e2df9bb4SMartin Matuska 	/*
3860e2df9bb4SMartin Matuska 	 * We are the new lead zio, because our parent has the highest
3861e2df9bb4SMartin Matuska 	 * zp_copies that has been requested for this entry so far.
3862e2df9bb4SMartin Matuska 	 */
3863e2df9bb4SMartin Matuska 	ddt_alloc_entry_io(dde);
3864e2df9bb4SMartin Matuska 	if (dde->dde_io->dde_lead_zio[p] == NULL) {
3865e2df9bb4SMartin Matuska 		/*
3866e2df9bb4SMartin Matuska 		 * First time out, take a copy of the stable entry to revert
3867e2df9bb4SMartin Matuska 		 * to if there's an error (see zio_ddt_child_write_done())
3868e2df9bb4SMartin Matuska 		 */
3869e2df9bb4SMartin Matuska 		ddt_phys_copy(&dde->dde_io->dde_orig_phys, dde->dde_phys, v);
3870e2df9bb4SMartin Matuska 	} else {
3871e2df9bb4SMartin Matuska 		/*
3872e2df9bb4SMartin Matuska 		 * Make the existing chain our child, because it cannot
3873e2df9bb4SMartin Matuska 		 * complete until we have.
3874e2df9bb4SMartin Matuska 		 */
3875e2df9bb4SMartin Matuska 		zio_add_child(cio, dde->dde_io->dde_lead_zio[p]);
3876eda14cbcSMatt Macy 	}
3877e2df9bb4SMartin Matuska 	dde->dde_io->dde_lead_zio[p] = cio;
3878eda14cbcSMatt Macy 
3879eda14cbcSMatt Macy 	ddt_exit(ddt);
3880eda14cbcSMatt Macy 
3881eda14cbcSMatt Macy 	zio_nowait(cio);
3882eda14cbcSMatt Macy 
3883eda14cbcSMatt Macy 	return (zio);
3884eda14cbcSMatt Macy }
3885eda14cbcSMatt Macy 
3886dbd5678dSMartin Matuska static ddt_entry_t *freedde; /* for debugging */
3887eda14cbcSMatt Macy 
3888eda14cbcSMatt Macy static zio_t *
3889eda14cbcSMatt Macy zio_ddt_free(zio_t *zio)
3890eda14cbcSMatt Macy {
3891eda14cbcSMatt Macy 	spa_t *spa = zio->io_spa;
3892eda14cbcSMatt Macy 	blkptr_t *bp = zio->io_bp;
3893eda14cbcSMatt Macy 	ddt_t *ddt = ddt_select(spa, bp);
3894e2df9bb4SMartin Matuska 	ddt_entry_t *dde = NULL;
3895eda14cbcSMatt Macy 
3896eda14cbcSMatt Macy 	ASSERT(BP_GET_DEDUP(bp));
3897eda14cbcSMatt Macy 	ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
3898eda14cbcSMatt Macy 
3899eda14cbcSMatt Macy 	ddt_enter(ddt);
3900e2df9bb4SMartin Matuska 	freedde = dde = ddt_lookup(ddt, bp);
3901eda14cbcSMatt Macy 	if (dde) {
3902e2df9bb4SMartin Matuska 		ddt_phys_variant_t v = ddt_phys_select(ddt, dde, bp);
3903e2df9bb4SMartin Matuska 		if (v != DDT_PHYS_NONE)
3904e2df9bb4SMartin Matuska 			ddt_phys_decref(dde->dde_phys, v);
3905eda14cbcSMatt Macy 	}
3906eda14cbcSMatt Macy 	ddt_exit(ddt);
3907eda14cbcSMatt Macy 
3908e2df9bb4SMartin Matuska 	/*
3909e2df9bb4SMartin Matuska 	 * When no entry was found, it must have been pruned,
3910e2df9bb4SMartin Matuska 	 * so we can free it now instead of decrementing the
3911e2df9bb4SMartin Matuska 	 * refcount in the DDT.
3912e2df9bb4SMartin Matuska 	 */
3913e2df9bb4SMartin Matuska 	if (!dde) {
3914e2df9bb4SMartin Matuska 		BP_SET_DEDUP(bp, 0);
3915e2df9bb4SMartin Matuska 		zio->io_pipeline |= ZIO_STAGE_DVA_FREE;
3916e2df9bb4SMartin Matuska 	}
3917e2df9bb4SMartin Matuska 
3918eda14cbcSMatt Macy 	return (zio);
3919eda14cbcSMatt Macy }
3920eda14cbcSMatt Macy 
3921eda14cbcSMatt Macy /*
3922eda14cbcSMatt Macy  * ==========================================================================
3923eda14cbcSMatt Macy  * Allocate and free blocks
3924eda14cbcSMatt Macy  * ==========================================================================
3925eda14cbcSMatt Macy  */
3926eda14cbcSMatt Macy 
3927eda14cbcSMatt Macy static zio_t *
3928eda14cbcSMatt Macy zio_io_to_allocate(spa_t *spa, int allocator)
3929eda14cbcSMatt Macy {
3930eda14cbcSMatt Macy 	zio_t *zio;
3931eda14cbcSMatt Macy 
39323f9d360cSMartin Matuska 	ASSERT(MUTEX_HELD(&spa->spa_allocs[allocator].spaa_lock));
3933eda14cbcSMatt Macy 
39343f9d360cSMartin Matuska 	zio = avl_first(&spa->spa_allocs[allocator].spaa_tree);
3935eda14cbcSMatt Macy 	if (zio == NULL)
3936eda14cbcSMatt Macy 		return (NULL);
3937eda14cbcSMatt Macy 
3938eda14cbcSMatt Macy 	ASSERT(IO_IS_ALLOCATING(zio));
393914c2e0a0SMartin Matuska 	ASSERT(ZIO_HAS_ALLOCATOR(zio));
3940eda14cbcSMatt Macy 
3941eda14cbcSMatt Macy 	/*
3942eda14cbcSMatt Macy 	 * Try to place a reservation for this zio. If we're unable to
3943eda14cbcSMatt Macy 	 * reserve then we throttle.
3944eda14cbcSMatt Macy 	 */
3945eda14cbcSMatt Macy 	ASSERT3U(zio->io_allocator, ==, allocator);
3946eda14cbcSMatt Macy 	if (!metaslab_class_throttle_reserve(zio->io_metaslab_class,
39473f9d360cSMartin Matuska 	    zio->io_prop.zp_copies, allocator, zio, 0)) {
3948eda14cbcSMatt Macy 		return (NULL);
3949eda14cbcSMatt Macy 	}
3950eda14cbcSMatt Macy 
39513f9d360cSMartin Matuska 	avl_remove(&spa->spa_allocs[allocator].spaa_tree, zio);
3952eda14cbcSMatt Macy 	ASSERT3U(zio->io_stage, <, ZIO_STAGE_DVA_ALLOCATE);
3953eda14cbcSMatt Macy 
3954eda14cbcSMatt Macy 	return (zio);
3955eda14cbcSMatt Macy }
3956eda14cbcSMatt Macy 
3957eda14cbcSMatt Macy static zio_t *
3958eda14cbcSMatt Macy zio_dva_throttle(zio_t *zio)
3959eda14cbcSMatt Macy {
3960eda14cbcSMatt Macy 	spa_t *spa = zio->io_spa;
3961eda14cbcSMatt Macy 	zio_t *nio;
3962eda14cbcSMatt Macy 	metaslab_class_t *mc;
3963eda14cbcSMatt Macy 
3964eda14cbcSMatt Macy 	/* locate an appropriate allocation class */
3965ce4dcb97SMartin Matuska 	mc = spa_preferred_class(spa, zio);
3966eda14cbcSMatt Macy 
3967eda14cbcSMatt Macy 	if (zio->io_priority == ZIO_PRIORITY_SYNC_WRITE ||
3968eda14cbcSMatt Macy 	    !mc->mc_alloc_throttle_enabled ||
3969eda14cbcSMatt Macy 	    zio->io_child_type == ZIO_CHILD_GANG ||
3970eda14cbcSMatt Macy 	    zio->io_flags & ZIO_FLAG_NODATA) {
3971eda14cbcSMatt Macy 		return (zio);
3972eda14cbcSMatt Macy 	}
3973eda14cbcSMatt Macy 
39743f9d360cSMartin Matuska 	ASSERT(zio->io_type == ZIO_TYPE_WRITE);
397514c2e0a0SMartin Matuska 	ASSERT(ZIO_HAS_ALLOCATOR(zio));
3976eda14cbcSMatt Macy 	ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
3977eda14cbcSMatt Macy 	ASSERT3U(zio->io_queued_timestamp, >, 0);
3978eda14cbcSMatt Macy 	ASSERT(zio->io_stage == ZIO_STAGE_DVA_THROTTLE);
3979eda14cbcSMatt Macy 
398014c2e0a0SMartin Matuska 	int allocator = zio->io_allocator;
3981eda14cbcSMatt Macy 	zio->io_metaslab_class = mc;
39823f9d360cSMartin Matuska 	mutex_enter(&spa->spa_allocs[allocator].spaa_lock);
39833f9d360cSMartin Matuska 	avl_add(&spa->spa_allocs[allocator].spaa_tree, zio);
39843f9d360cSMartin Matuska 	nio = zio_io_to_allocate(spa, allocator);
39853f9d360cSMartin Matuska 	mutex_exit(&spa->spa_allocs[allocator].spaa_lock);
3986eda14cbcSMatt Macy 	return (nio);
3987eda14cbcSMatt Macy }
3988eda14cbcSMatt Macy 
3989eda14cbcSMatt Macy static void
3990eda14cbcSMatt Macy zio_allocate_dispatch(spa_t *spa, int allocator)
3991eda14cbcSMatt Macy {
3992eda14cbcSMatt Macy 	zio_t *zio;
3993eda14cbcSMatt Macy 
39943f9d360cSMartin Matuska 	mutex_enter(&spa->spa_allocs[allocator].spaa_lock);
3995eda14cbcSMatt Macy 	zio = zio_io_to_allocate(spa, allocator);
39963f9d360cSMartin Matuska 	mutex_exit(&spa->spa_allocs[allocator].spaa_lock);
3997eda14cbcSMatt Macy 	if (zio == NULL)
3998eda14cbcSMatt Macy 		return;
3999eda14cbcSMatt Macy 
4000eda14cbcSMatt Macy 	ASSERT3U(zio->io_stage, ==, ZIO_STAGE_DVA_THROTTLE);
4001eda14cbcSMatt Macy 	ASSERT0(zio->io_error);
4002eda14cbcSMatt Macy 	zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_TRUE);
4003eda14cbcSMatt Macy }
4004eda14cbcSMatt Macy 
4005eda14cbcSMatt Macy static zio_t *
4006eda14cbcSMatt Macy zio_dva_allocate(zio_t *zio)
4007eda14cbcSMatt Macy {
4008eda14cbcSMatt Macy 	spa_t *spa = zio->io_spa;
4009eda14cbcSMatt Macy 	metaslab_class_t *mc;
4010eda14cbcSMatt Macy 	blkptr_t *bp = zio->io_bp;
4011eda14cbcSMatt Macy 	int error;
4012eda14cbcSMatt Macy 	int flags = 0;
4013eda14cbcSMatt Macy 
4014eda14cbcSMatt Macy 	if (zio->io_gang_leader == NULL) {
4015eda14cbcSMatt Macy 		ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
4016eda14cbcSMatt Macy 		zio->io_gang_leader = zio;
4017eda14cbcSMatt Macy 	}
4018eda14cbcSMatt Macy 
4019eda14cbcSMatt Macy 	ASSERT(BP_IS_HOLE(bp));
4020eda14cbcSMatt Macy 	ASSERT0(BP_GET_NDVAS(bp));
4021eda14cbcSMatt Macy 	ASSERT3U(zio->io_prop.zp_copies, >, 0);
4022eda14cbcSMatt Macy 	ASSERT3U(zio->io_prop.zp_copies, <=, spa_max_replication(spa));
4023eda14cbcSMatt Macy 	ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp));
4024eda14cbcSMatt Macy 
4025eda14cbcSMatt Macy 	if (zio->io_flags & ZIO_FLAG_NODATA)
4026eda14cbcSMatt Macy 		flags |= METASLAB_DONT_THROTTLE;
4027eda14cbcSMatt Macy 	if (zio->io_flags & ZIO_FLAG_GANG_CHILD)
4028eda14cbcSMatt Macy 		flags |= METASLAB_GANG_CHILD;
4029eda14cbcSMatt Macy 	if (zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE)
4030eda14cbcSMatt Macy 		flags |= METASLAB_ASYNC_ALLOC;
4031eda14cbcSMatt Macy 
4032eda14cbcSMatt Macy 	/*
4033eda14cbcSMatt Macy 	 * if not already chosen, locate an appropriate allocation class
4034eda14cbcSMatt Macy 	 */
4035eda14cbcSMatt Macy 	mc = zio->io_metaslab_class;
4036eda14cbcSMatt Macy 	if (mc == NULL) {
4037ce4dcb97SMartin Matuska 		mc = spa_preferred_class(spa, zio);
4038eda14cbcSMatt Macy 		zio->io_metaslab_class = mc;
4039eda14cbcSMatt Macy 	}
4040eda14cbcSMatt Macy 
4041184c1b94SMartin Matuska 	/*
4042184c1b94SMartin Matuska 	 * Try allocating the block in the usual metaslab class.
4043184c1b94SMartin Matuska 	 * If that's full, allocate it in the normal class.
4044184c1b94SMartin Matuska 	 * If that's full, allocate as a gang block,
4045184c1b94SMartin Matuska 	 * and if all are full, the allocation fails (which shouldn't happen).
4046184c1b94SMartin Matuska 	 *
4047184c1b94SMartin Matuska 	 * Note that we do not fall back on embedded slog (ZIL) space, to
4048184c1b94SMartin Matuska 	 * preserve unfragmented slog space, which is critical for decent
4049184c1b94SMartin Matuska 	 * sync write performance.  If a log allocation fails, we will fall
4050184c1b94SMartin Matuska 	 * back to spa_sync() which is abysmal for performance.
4051184c1b94SMartin Matuska 	 */
405214c2e0a0SMartin Matuska 	ASSERT(ZIO_HAS_ALLOCATOR(zio));
4053eda14cbcSMatt Macy 	error = metaslab_alloc(spa, mc, zio->io_size, bp,
4054eda14cbcSMatt Macy 	    zio->io_prop.zp_copies, zio->io_txg, NULL, flags,
4055eda14cbcSMatt Macy 	    &zio->io_alloc_list, zio, zio->io_allocator);
4056eda14cbcSMatt Macy 
4057eda14cbcSMatt Macy 	/*
4058eda14cbcSMatt Macy 	 * Fallback to normal class when an alloc class is full
4059eda14cbcSMatt Macy 	 */
4060eda14cbcSMatt Macy 	if (error == ENOSPC && mc != spa_normal_class(spa)) {
4061eda14cbcSMatt Macy 		/*
4062ce4dcb97SMartin Matuska 		 * When the dedup or special class is spilling into the  normal
4063ce4dcb97SMartin Matuska 		 * class, there can still be significant space available due
4064ce4dcb97SMartin Matuska 		 * to deferred frees that are in-flight.  We track the txg when
4065ce4dcb97SMartin Matuska 		 * this occurred and back off adding new DDT entries for a few
4066ce4dcb97SMartin Matuska 		 * txgs to allow the free blocks to be processed.
4067ce4dcb97SMartin Matuska 		 */
4068ce4dcb97SMartin Matuska 		if ((mc == spa_dedup_class(spa) || (spa_special_has_ddt(spa) &&
4069ce4dcb97SMartin Matuska 		    mc == spa_special_class(spa))) &&
4070ce4dcb97SMartin Matuska 		    spa->spa_dedup_class_full_txg != zio->io_txg) {
4071ce4dcb97SMartin Matuska 			spa->spa_dedup_class_full_txg = zio->io_txg;
4072ce4dcb97SMartin Matuska 			zfs_dbgmsg("%s[%d]: %s class spilling, req size %d, "
4073ce4dcb97SMartin Matuska 			    "%llu allocated of %llu",
4074ce4dcb97SMartin Matuska 			    spa_name(spa), (int)zio->io_txg,
4075ce4dcb97SMartin Matuska 			    mc == spa_dedup_class(spa) ? "dedup" : "special",
4076ce4dcb97SMartin Matuska 			    (int)zio->io_size,
4077ce4dcb97SMartin Matuska 			    (u_longlong_t)metaslab_class_get_alloc(mc),
4078ce4dcb97SMartin Matuska 			    (u_longlong_t)metaslab_class_get_space(mc));
4079ce4dcb97SMartin Matuska 		}
4080ce4dcb97SMartin Matuska 
4081ce4dcb97SMartin Matuska 		/*
4082eda14cbcSMatt Macy 		 * If throttling, transfer reservation over to normal class.
4083eda14cbcSMatt Macy 		 * The io_allocator slot can remain the same even though we
4084eda14cbcSMatt Macy 		 * are switching classes.
4085eda14cbcSMatt Macy 		 */
4086eda14cbcSMatt Macy 		if (mc->mc_alloc_throttle_enabled &&
4087eda14cbcSMatt Macy 		    (zio->io_flags & ZIO_FLAG_IO_ALLOCATING)) {
4088eda14cbcSMatt Macy 			metaslab_class_throttle_unreserve(mc,
4089eda14cbcSMatt Macy 			    zio->io_prop.zp_copies, zio->io_allocator, zio);
4090eda14cbcSMatt Macy 			zio->io_flags &= ~ZIO_FLAG_IO_ALLOCATING;
4091eda14cbcSMatt Macy 
4092184c1b94SMartin Matuska 			VERIFY(metaslab_class_throttle_reserve(
4093184c1b94SMartin Matuska 			    spa_normal_class(spa),
4094eda14cbcSMatt Macy 			    zio->io_prop.zp_copies, zio->io_allocator, zio,
4095eda14cbcSMatt Macy 			    flags | METASLAB_MUST_RESERVE));
4096eda14cbcSMatt Macy 		}
4097184c1b94SMartin Matuska 		zio->io_metaslab_class = mc = spa_normal_class(spa);
4098184c1b94SMartin Matuska 		if (zfs_flags & ZFS_DEBUG_METASLAB_ALLOC) {
4099184c1b94SMartin Matuska 			zfs_dbgmsg("%s: metaslab allocation failure, "
4100184c1b94SMartin Matuska 			    "trying normal class: zio %px, size %llu, error %d",
410133b8c039SMartin Matuska 			    spa_name(spa), zio, (u_longlong_t)zio->io_size,
410233b8c039SMartin Matuska 			    error);
4103184c1b94SMartin Matuska 		}
4104eda14cbcSMatt Macy 
4105eda14cbcSMatt Macy 		error = metaslab_alloc(spa, mc, zio->io_size, bp,
4106eda14cbcSMatt Macy 		    zio->io_prop.zp_copies, zio->io_txg, NULL, flags,
4107eda14cbcSMatt Macy 		    &zio->io_alloc_list, zio, zio->io_allocator);
4108eda14cbcSMatt Macy 	}
4109eda14cbcSMatt Macy 
4110184c1b94SMartin Matuska 	if (error == ENOSPC && zio->io_size > SPA_MINBLOCKSIZE) {
4111184c1b94SMartin Matuska 		if (zfs_flags & ZFS_DEBUG_METASLAB_ALLOC) {
4112184c1b94SMartin Matuska 			zfs_dbgmsg("%s: metaslab allocation failure, "
4113184c1b94SMartin Matuska 			    "trying ganging: zio %px, size %llu, error %d",
411433b8c039SMartin Matuska 			    spa_name(spa), zio, (u_longlong_t)zio->io_size,
411533b8c039SMartin Matuska 			    error);
4116184c1b94SMartin Matuska 		}
4117184c1b94SMartin Matuska 		return (zio_write_gang_block(zio, mc));
4118184c1b94SMartin Matuska 	}
4119eda14cbcSMatt Macy 	if (error != 0) {
4120184c1b94SMartin Matuska 		if (error != ENOSPC ||
4121184c1b94SMartin Matuska 		    (zfs_flags & ZFS_DEBUG_METASLAB_ALLOC)) {
4122eda14cbcSMatt Macy 			zfs_dbgmsg("%s: metaslab allocation failure: zio %px, "
4123184c1b94SMartin Matuska 			    "size %llu, error %d",
412433b8c039SMartin Matuska 			    spa_name(spa), zio, (u_longlong_t)zio->io_size,
412533b8c039SMartin Matuska 			    error);
4126184c1b94SMartin Matuska 		}
4127eda14cbcSMatt Macy 		zio->io_error = error;
4128eda14cbcSMatt Macy 	}
4129eda14cbcSMatt Macy 
4130eda14cbcSMatt Macy 	return (zio);
4131eda14cbcSMatt Macy }
4132eda14cbcSMatt Macy 
4133eda14cbcSMatt Macy static zio_t *
4134eda14cbcSMatt Macy zio_dva_free(zio_t *zio)
4135eda14cbcSMatt Macy {
4136eda14cbcSMatt Macy 	metaslab_free(zio->io_spa, zio->io_bp, zio->io_txg, B_FALSE);
4137eda14cbcSMatt Macy 
4138eda14cbcSMatt Macy 	return (zio);
4139eda14cbcSMatt Macy }
4140eda14cbcSMatt Macy 
4141eda14cbcSMatt Macy static zio_t *
4142eda14cbcSMatt Macy zio_dva_claim(zio_t *zio)
4143eda14cbcSMatt Macy {
4144eda14cbcSMatt Macy 	int error;
4145eda14cbcSMatt Macy 
4146eda14cbcSMatt Macy 	error = metaslab_claim(zio->io_spa, zio->io_bp, zio->io_txg);
4147eda14cbcSMatt Macy 	if (error)
4148eda14cbcSMatt Macy 		zio->io_error = error;
4149eda14cbcSMatt Macy 
4150eda14cbcSMatt Macy 	return (zio);
4151eda14cbcSMatt Macy }
4152eda14cbcSMatt Macy 
4153eda14cbcSMatt Macy /*
4154eda14cbcSMatt Macy  * Undo an allocation.  This is used by zio_done() when an I/O fails
4155eda14cbcSMatt Macy  * and we want to give back the block we just allocated.
4156eda14cbcSMatt Macy  * This handles both normal blocks and gang blocks.
4157eda14cbcSMatt Macy  */
4158eda14cbcSMatt Macy static void
4159eda14cbcSMatt Macy zio_dva_unallocate(zio_t *zio, zio_gang_node_t *gn, blkptr_t *bp)
4160eda14cbcSMatt Macy {
4161783d3ff6SMartin Matuska 	ASSERT(BP_GET_LOGICAL_BIRTH(bp) == zio->io_txg || BP_IS_HOLE(bp));
4162eda14cbcSMatt Macy 	ASSERT(zio->io_bp_override == NULL);
4163eda14cbcSMatt Macy 
4164783d3ff6SMartin Matuska 	if (!BP_IS_HOLE(bp)) {
4165783d3ff6SMartin Matuska 		metaslab_free(zio->io_spa, bp, BP_GET_LOGICAL_BIRTH(bp),
4166783d3ff6SMartin Matuska 		    B_TRUE);
4167783d3ff6SMartin Matuska 	}
4168eda14cbcSMatt Macy 
4169eda14cbcSMatt Macy 	if (gn != NULL) {
4170eda14cbcSMatt Macy 		for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
4171eda14cbcSMatt Macy 			zio_dva_unallocate(zio, gn->gn_child[g],
4172eda14cbcSMatt Macy 			    &gn->gn_gbh->zg_blkptr[g]);
4173eda14cbcSMatt Macy 		}
4174eda14cbcSMatt Macy 	}
4175eda14cbcSMatt Macy }
4176eda14cbcSMatt Macy 
4177eda14cbcSMatt Macy /*
4178eda14cbcSMatt Macy  * Try to allocate an intent log block.  Return 0 on success, errno on failure.
4179eda14cbcSMatt Macy  */
4180eda14cbcSMatt Macy int
4181eda14cbcSMatt Macy zio_alloc_zil(spa_t *spa, objset_t *os, uint64_t txg, blkptr_t *new_bp,
4182eda14cbcSMatt Macy     uint64_t size, boolean_t *slog)
4183eda14cbcSMatt Macy {
4184eda14cbcSMatt Macy 	int error = 1;
4185eda14cbcSMatt Macy 	zio_alloc_list_t io_alloc_list;
4186eda14cbcSMatt Macy 
4187eda14cbcSMatt Macy 	ASSERT(txg > spa_syncing_txg(spa));
4188eda14cbcSMatt Macy 
4189eda14cbcSMatt Macy 	metaslab_trace_init(&io_alloc_list);
4190eda14cbcSMatt Macy 
4191eda14cbcSMatt Macy 	/*
4192eda14cbcSMatt Macy 	 * Block pointer fields are useful to metaslabs for stats and debugging.
4193eda14cbcSMatt Macy 	 * Fill in the obvious ones before calling into metaslab_alloc().
4194eda14cbcSMatt Macy 	 */
4195eda14cbcSMatt Macy 	BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG);
4196eda14cbcSMatt Macy 	BP_SET_PSIZE(new_bp, size);
4197eda14cbcSMatt Macy 	BP_SET_LEVEL(new_bp, 0);
4198eda14cbcSMatt Macy 
4199eda14cbcSMatt Macy 	/*
4200eda14cbcSMatt Macy 	 * When allocating a zil block, we don't have information about
4201eda14cbcSMatt Macy 	 * the final destination of the block except the objset it's part
4202eda14cbcSMatt Macy 	 * of, so we just hash the objset ID to pick the allocator to get
4203eda14cbcSMatt Macy 	 * some parallelism.
4204eda14cbcSMatt Macy 	 */
4205315ee00fSMartin Matuska 	int flags = METASLAB_ZIL;
42067a7741afSMartin Matuska 	int allocator = (uint_t)cityhash1(os->os_dsl_dataset->ds_object)
42077a7741afSMartin Matuska 	    % spa->spa_alloc_count;
4208184c1b94SMartin Matuska 	error = metaslab_alloc(spa, spa_log_class(spa), size, new_bp, 1,
4209184c1b94SMartin Matuska 	    txg, NULL, flags, &io_alloc_list, NULL, allocator);
4210184c1b94SMartin Matuska 	*slog = (error == 0);
4211184c1b94SMartin Matuska 	if (error != 0) {
4212184c1b94SMartin Matuska 		error = metaslab_alloc(spa, spa_embedded_log_class(spa), size,
4213184c1b94SMartin Matuska 		    new_bp, 1, txg, NULL, flags,
4214184c1b94SMartin Matuska 		    &io_alloc_list, NULL, allocator);
4215184c1b94SMartin Matuska 	}
4216184c1b94SMartin Matuska 	if (error != 0) {
4217184c1b94SMartin Matuska 		error = metaslab_alloc(spa, spa_normal_class(spa), size,
4218184c1b94SMartin Matuska 		    new_bp, 1, txg, NULL, flags,
4219184c1b94SMartin Matuska 		    &io_alloc_list, NULL, allocator);
4220eda14cbcSMatt Macy 	}
4221eda14cbcSMatt Macy 	metaslab_trace_fini(&io_alloc_list);
4222eda14cbcSMatt Macy 
4223eda14cbcSMatt Macy 	if (error == 0) {
4224eda14cbcSMatt Macy 		BP_SET_LSIZE(new_bp, size);
4225eda14cbcSMatt Macy 		BP_SET_PSIZE(new_bp, size);
4226eda14cbcSMatt Macy 		BP_SET_COMPRESS(new_bp, ZIO_COMPRESS_OFF);
4227eda14cbcSMatt Macy 		BP_SET_CHECKSUM(new_bp,
4228eda14cbcSMatt Macy 		    spa_version(spa) >= SPA_VERSION_SLIM_ZIL
4229eda14cbcSMatt Macy 		    ? ZIO_CHECKSUM_ZILOG2 : ZIO_CHECKSUM_ZILOG);
4230eda14cbcSMatt Macy 		BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG);
4231eda14cbcSMatt Macy 		BP_SET_LEVEL(new_bp, 0);
4232eda14cbcSMatt Macy 		BP_SET_DEDUP(new_bp, 0);
4233eda14cbcSMatt Macy 		BP_SET_BYTEORDER(new_bp, ZFS_HOST_BYTEORDER);
4234eda14cbcSMatt Macy 
4235eda14cbcSMatt Macy 		/*
4236eda14cbcSMatt Macy 		 * encrypted blocks will require an IV and salt. We generate
4237eda14cbcSMatt Macy 		 * these now since we will not be rewriting the bp at
4238eda14cbcSMatt Macy 		 * rewrite time.
4239eda14cbcSMatt Macy 		 */
4240eda14cbcSMatt Macy 		if (os->os_encrypted) {
4241eda14cbcSMatt Macy 			uint8_t iv[ZIO_DATA_IV_LEN];
4242eda14cbcSMatt Macy 			uint8_t salt[ZIO_DATA_SALT_LEN];
4243eda14cbcSMatt Macy 
4244eda14cbcSMatt Macy 			BP_SET_CRYPT(new_bp, B_TRUE);
4245eda14cbcSMatt Macy 			VERIFY0(spa_crypt_get_salt(spa,
4246eda14cbcSMatt Macy 			    dmu_objset_id(os), salt));
4247eda14cbcSMatt Macy 			VERIFY0(zio_crypt_generate_iv(iv));
4248eda14cbcSMatt Macy 
4249eda14cbcSMatt Macy 			zio_crypt_encode_params_bp(new_bp, salt, iv);
4250eda14cbcSMatt Macy 		}
4251eda14cbcSMatt Macy 	} else {
4252eda14cbcSMatt Macy 		zfs_dbgmsg("%s: zil block allocation failure: "
425333b8c039SMartin Matuska 		    "size %llu, error %d", spa_name(spa), (u_longlong_t)size,
425433b8c039SMartin Matuska 		    error);
4255eda14cbcSMatt Macy 	}
4256eda14cbcSMatt Macy 
4257eda14cbcSMatt Macy 	return (error);
4258eda14cbcSMatt Macy }
4259eda14cbcSMatt Macy 
4260eda14cbcSMatt Macy /*
4261eda14cbcSMatt Macy  * ==========================================================================
4262eda14cbcSMatt Macy  * Read and write to physical devices
4263eda14cbcSMatt Macy  * ==========================================================================
4264eda14cbcSMatt Macy  */
4265eda14cbcSMatt Macy 
4266eda14cbcSMatt Macy /*
4267eda14cbcSMatt Macy  * Issue an I/O to the underlying vdev. Typically the issue pipeline
4268eda14cbcSMatt Macy  * stops after this stage and will resume upon I/O completion.
4269eda14cbcSMatt Macy  * However, there are instances where the vdev layer may need to
4270eda14cbcSMatt Macy  * continue the pipeline when an I/O was not issued. Since the I/O
4271eda14cbcSMatt Macy  * that was sent to the vdev layer might be different than the one
4272eda14cbcSMatt Macy  * currently active in the pipeline (see vdev_queue_io()), we explicitly
4273eda14cbcSMatt Macy  * force the underlying vdev layers to call either zio_execute() or
4274eda14cbcSMatt Macy  * zio_interrupt() to ensure that the pipeline continues with the correct I/O.
4275eda14cbcSMatt Macy  */
4276eda14cbcSMatt Macy static zio_t *
4277eda14cbcSMatt Macy zio_vdev_io_start(zio_t *zio)
4278eda14cbcSMatt Macy {
4279eda14cbcSMatt Macy 	vdev_t *vd = zio->io_vd;
4280eda14cbcSMatt Macy 	uint64_t align;
4281eda14cbcSMatt Macy 	spa_t *spa = zio->io_spa;
4282eda14cbcSMatt Macy 
4283eda14cbcSMatt Macy 	zio->io_delay = 0;
4284eda14cbcSMatt Macy 
4285eda14cbcSMatt Macy 	ASSERT(zio->io_error == 0);
4286eda14cbcSMatt Macy 	ASSERT(zio->io_child_error[ZIO_CHILD_VDEV] == 0);
4287eda14cbcSMatt Macy 
4288eda14cbcSMatt Macy 	if (vd == NULL) {
4289eda14cbcSMatt Macy 		if (!(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
4290eda14cbcSMatt Macy 			spa_config_enter(spa, SCL_ZIO, zio, RW_READER);
4291eda14cbcSMatt Macy 
4292eda14cbcSMatt Macy 		/*
4293eda14cbcSMatt Macy 		 * The mirror_ops handle multiple DVAs in a single BP.
4294eda14cbcSMatt Macy 		 */
4295eda14cbcSMatt Macy 		vdev_mirror_ops.vdev_op_io_start(zio);
4296eda14cbcSMatt Macy 		return (NULL);
4297eda14cbcSMatt Macy 	}
4298eda14cbcSMatt Macy 
4299eda14cbcSMatt Macy 	ASSERT3P(zio->io_logical, !=, zio);
4300eda14cbcSMatt Macy 	if (zio->io_type == ZIO_TYPE_WRITE) {
4301eda14cbcSMatt Macy 		ASSERT(spa->spa_trust_config);
4302eda14cbcSMatt Macy 
4303eda14cbcSMatt Macy 		/*
4304eda14cbcSMatt Macy 		 * Note: the code can handle other kinds of writes,
4305eda14cbcSMatt Macy 		 * but we don't expect them.
4306eda14cbcSMatt Macy 		 */
4307681ce946SMartin Matuska 		if (zio->io_vd->vdev_noalloc) {
4308eda14cbcSMatt Macy 			ASSERT(zio->io_flags &
4309eda14cbcSMatt Macy 			    (ZIO_FLAG_PHYSICAL | ZIO_FLAG_SELF_HEAL |
4310eda14cbcSMatt Macy 			    ZIO_FLAG_RESILVER | ZIO_FLAG_INDUCE_DAMAGE));
4311eda14cbcSMatt Macy 		}
4312eda14cbcSMatt Macy 	}
4313eda14cbcSMatt Macy 
4314eda14cbcSMatt Macy 	align = 1ULL << vd->vdev_top->vdev_ashift;
4315eda14cbcSMatt Macy 
4316eda14cbcSMatt Macy 	if (!(zio->io_flags & ZIO_FLAG_PHYSICAL) &&
4317eda14cbcSMatt Macy 	    P2PHASE(zio->io_size, align) != 0) {
4318eda14cbcSMatt Macy 		/* Transform logical writes to be a full physical block size. */
4319eda14cbcSMatt Macy 		uint64_t asize = P2ROUNDUP(zio->io_size, align);
4320eda14cbcSMatt Macy 		abd_t *abuf = abd_alloc_sametype(zio->io_abd, asize);
4321eda14cbcSMatt Macy 		ASSERT(vd == vd->vdev_top);
4322eda14cbcSMatt Macy 		if (zio->io_type == ZIO_TYPE_WRITE) {
4323eda14cbcSMatt Macy 			abd_copy(abuf, zio->io_abd, zio->io_size);
4324eda14cbcSMatt Macy 			abd_zero_off(abuf, zio->io_size, asize - zio->io_size);
4325eda14cbcSMatt Macy 		}
4326eda14cbcSMatt Macy 		zio_push_transform(zio, abuf, asize, asize, zio_subblock);
4327eda14cbcSMatt Macy 	}
4328eda14cbcSMatt Macy 
4329eda14cbcSMatt Macy 	/*
4330eda14cbcSMatt Macy 	 * If this is not a physical io, make sure that it is properly aligned
4331eda14cbcSMatt Macy 	 * before proceeding.
4332eda14cbcSMatt Macy 	 */
4333eda14cbcSMatt Macy 	if (!(zio->io_flags & ZIO_FLAG_PHYSICAL)) {
4334eda14cbcSMatt Macy 		ASSERT0(P2PHASE(zio->io_offset, align));
4335eda14cbcSMatt Macy 		ASSERT0(P2PHASE(zio->io_size, align));
4336eda14cbcSMatt Macy 	} else {
4337eda14cbcSMatt Macy 		/*
4338eda14cbcSMatt Macy 		 * For physical writes, we allow 512b aligned writes and assume
4339eda14cbcSMatt Macy 		 * the device will perform a read-modify-write as necessary.
4340eda14cbcSMatt Macy 		 */
4341eda14cbcSMatt Macy 		ASSERT0(P2PHASE(zio->io_offset, SPA_MINBLOCKSIZE));
4342eda14cbcSMatt Macy 		ASSERT0(P2PHASE(zio->io_size, SPA_MINBLOCKSIZE));
4343eda14cbcSMatt Macy 	}
4344eda14cbcSMatt Macy 
4345eda14cbcSMatt Macy 	VERIFY(zio->io_type != ZIO_TYPE_WRITE || spa_writeable(spa));
4346eda14cbcSMatt Macy 
4347eda14cbcSMatt Macy 	/*
4348eda14cbcSMatt Macy 	 * If this is a repair I/O, and there's no self-healing involved --
4349eda14cbcSMatt Macy 	 * that is, we're just resilvering what we expect to resilver --
4350eda14cbcSMatt Macy 	 * then don't do the I/O unless zio's txg is actually in vd's DTL.
4351eda14cbcSMatt Macy 	 * This prevents spurious resilvering.
4352eda14cbcSMatt Macy 	 *
4353eda14cbcSMatt Macy 	 * There are a few ways that we can end up creating these spurious
4354eda14cbcSMatt Macy 	 * resilver i/os:
4355eda14cbcSMatt Macy 	 *
4356eda14cbcSMatt Macy 	 * 1. A resilver i/o will be issued if any DVA in the BP has a
4357eda14cbcSMatt Macy 	 * dirty DTL.  The mirror code will issue resilver writes to
4358eda14cbcSMatt Macy 	 * each DVA, including the one(s) that are not on vdevs with dirty
4359eda14cbcSMatt Macy 	 * DTLs.
4360eda14cbcSMatt Macy 	 *
4361eda14cbcSMatt Macy 	 * 2. With nested replication, which happens when we have a
4362eda14cbcSMatt Macy 	 * "replacing" or "spare" vdev that's a child of a mirror or raidz.
4363eda14cbcSMatt Macy 	 * For example, given mirror(replacing(A+B), C), it's likely that
4364eda14cbcSMatt Macy 	 * only A is out of date (it's the new device). In this case, we'll
4365eda14cbcSMatt Macy 	 * read from C, then use the data to resilver A+B -- but we don't
4366eda14cbcSMatt Macy 	 * actually want to resilver B, just A. The top-level mirror has no
4367eda14cbcSMatt Macy 	 * way to know this, so instead we just discard unnecessary repairs
4368eda14cbcSMatt Macy 	 * as we work our way down the vdev tree.
4369eda14cbcSMatt Macy 	 *
4370eda14cbcSMatt Macy 	 * 3. ZTEST also creates mirrors of mirrors, mirrors of raidz, etc.
4371eda14cbcSMatt Macy 	 * The same logic applies to any form of nested replication: ditto
4372eda14cbcSMatt Macy 	 * + mirror, RAID-Z + replacing, etc.
4373eda14cbcSMatt Macy 	 *
4374eda14cbcSMatt Macy 	 * However, indirect vdevs point off to other vdevs which may have
4375eda14cbcSMatt Macy 	 * DTL's, so we never bypass them.  The child i/os on concrete vdevs
4376eda14cbcSMatt Macy 	 * will be properly bypassed instead.
43777877fdebSMatt Macy 	 *
43787877fdebSMatt Macy 	 * Leaf DTL_PARTIAL can be empty when a legitimate write comes from
43797877fdebSMatt Macy 	 * a dRAID spare vdev. For example, when a dRAID spare is first
43807877fdebSMatt Macy 	 * used, its spare blocks need to be written to but the leaf vdev's
43817877fdebSMatt Macy 	 * of such blocks can have empty DTL_PARTIAL.
43827877fdebSMatt Macy 	 *
43837877fdebSMatt Macy 	 * There seemed no clean way to allow such writes while bypassing
43847877fdebSMatt Macy 	 * spurious ones. At this point, just avoid all bypassing for dRAID
43857877fdebSMatt Macy 	 * for correctness.
4386eda14cbcSMatt Macy 	 */
4387eda14cbcSMatt Macy 	if ((zio->io_flags & ZIO_FLAG_IO_REPAIR) &&
4388eda14cbcSMatt Macy 	    !(zio->io_flags & ZIO_FLAG_SELF_HEAL) &&
4389eda14cbcSMatt Macy 	    zio->io_txg != 0 &&	/* not a delegated i/o */
4390eda14cbcSMatt Macy 	    vd->vdev_ops != &vdev_indirect_ops &&
43917877fdebSMatt Macy 	    vd->vdev_top->vdev_ops != &vdev_draid_ops &&
4392eda14cbcSMatt Macy 	    !vdev_dtl_contains(vd, DTL_PARTIAL, zio->io_txg, 1)) {
4393eda14cbcSMatt Macy 		ASSERT(zio->io_type == ZIO_TYPE_WRITE);
4394eda14cbcSMatt Macy 		zio_vdev_io_bypass(zio);
4395eda14cbcSMatt Macy 		return (zio);
4396eda14cbcSMatt Macy 	}
4397eda14cbcSMatt Macy 
43987877fdebSMatt Macy 	/*
43997877fdebSMatt Macy 	 * Select the next best leaf I/O to process.  Distributed spares are
44007877fdebSMatt Macy 	 * excluded since they dispatch the I/O directly to a leaf vdev after
44017877fdebSMatt Macy 	 * applying the dRAID mapping.
44027877fdebSMatt Macy 	 */
44037877fdebSMatt Macy 	if (vd->vdev_ops->vdev_op_leaf &&
44047877fdebSMatt Macy 	    vd->vdev_ops != &vdev_draid_spare_ops &&
44057877fdebSMatt Macy 	    (zio->io_type == ZIO_TYPE_READ ||
44067877fdebSMatt Macy 	    zio->io_type == ZIO_TYPE_WRITE ||
44077877fdebSMatt Macy 	    zio->io_type == ZIO_TYPE_TRIM)) {
4408eda14cbcSMatt Macy 
44091719886fSMartin Matuska 		if (zio_handle_device_injection(vd, zio, ENOSYS) != 0) {
44101719886fSMartin Matuska 			/*
44111719886fSMartin Matuska 			 * "no-op" injections return success, but do no actual
44121719886fSMartin Matuska 			 * work. Just skip the remaining vdev stages.
44131719886fSMartin Matuska 			 */
44141719886fSMartin Matuska 			zio_vdev_io_bypass(zio);
44151719886fSMartin Matuska 			zio_interrupt(zio);
44161719886fSMartin Matuska 			return (NULL);
44171719886fSMartin Matuska 		}
44181719886fSMartin Matuska 
4419eda14cbcSMatt Macy 		if ((zio = vdev_queue_io(zio)) == NULL)
4420eda14cbcSMatt Macy 			return (NULL);
4421eda14cbcSMatt Macy 
4422eda14cbcSMatt Macy 		if (!vdev_accessible(vd, zio)) {
4423eda14cbcSMatt Macy 			zio->io_error = SET_ERROR(ENXIO);
4424eda14cbcSMatt Macy 			zio_interrupt(zio);
4425eda14cbcSMatt Macy 			return (NULL);
4426eda14cbcSMatt Macy 		}
4427eda14cbcSMatt Macy 		zio->io_delay = gethrtime();
4428eda14cbcSMatt Macy 	}
4429eda14cbcSMatt Macy 
4430eda14cbcSMatt Macy 	vd->vdev_ops->vdev_op_io_start(zio);
4431eda14cbcSMatt Macy 	return (NULL);
4432eda14cbcSMatt Macy }
4433eda14cbcSMatt Macy 
4434eda14cbcSMatt Macy static zio_t *
4435eda14cbcSMatt Macy zio_vdev_io_done(zio_t *zio)
4436eda14cbcSMatt Macy {
4437eda14cbcSMatt Macy 	vdev_t *vd = zio->io_vd;
4438eda14cbcSMatt Macy 	vdev_ops_t *ops = vd ? vd->vdev_ops : &vdev_mirror_ops;
4439eda14cbcSMatt Macy 	boolean_t unexpected_error = B_FALSE;
4440eda14cbcSMatt Macy 
4441eda14cbcSMatt Macy 	if (zio_wait_for_children(zio, ZIO_CHILD_VDEV_BIT, ZIO_WAIT_DONE)) {
4442eda14cbcSMatt Macy 		return (NULL);
4443eda14cbcSMatt Macy 	}
4444eda14cbcSMatt Macy 
4445eda14cbcSMatt Macy 	ASSERT(zio->io_type == ZIO_TYPE_READ ||
44461719886fSMartin Matuska 	    zio->io_type == ZIO_TYPE_WRITE ||
44471719886fSMartin Matuska 	    zio->io_type == ZIO_TYPE_FLUSH ||
44481719886fSMartin Matuska 	    zio->io_type == ZIO_TYPE_TRIM);
4449eda14cbcSMatt Macy 
4450eda14cbcSMatt Macy 	if (zio->io_delay)
4451eda14cbcSMatt Macy 		zio->io_delay = gethrtime() - zio->io_delay;
4452eda14cbcSMatt Macy 
44537877fdebSMatt Macy 	if (vd != NULL && vd->vdev_ops->vdev_op_leaf &&
44547877fdebSMatt Macy 	    vd->vdev_ops != &vdev_draid_spare_ops) {
44551719886fSMartin Matuska 		if (zio->io_type != ZIO_TYPE_FLUSH)
4456eda14cbcSMatt Macy 			vdev_queue_io_done(zio);
4457eda14cbcSMatt Macy 
4458eda14cbcSMatt Macy 		if (zio_injection_enabled && zio->io_error == 0)
4459eda14cbcSMatt Macy 			zio->io_error = zio_handle_device_injections(vd, zio,
4460eda14cbcSMatt Macy 			    EIO, EILSEQ);
4461eda14cbcSMatt Macy 
4462eda14cbcSMatt Macy 		if (zio_injection_enabled && zio->io_error == 0)
4463eda14cbcSMatt Macy 			zio->io_error = zio_handle_label_injection(zio, EIO);
4464eda14cbcSMatt Macy 
44650d4ad640SMartin Matuska 		if (zio->io_error && zio->io_type != ZIO_TYPE_FLUSH &&
44660d4ad640SMartin Matuska 		    zio->io_type != ZIO_TYPE_TRIM) {
4467eda14cbcSMatt Macy 			if (!vdev_accessible(vd, zio)) {
4468eda14cbcSMatt Macy 				zio->io_error = SET_ERROR(ENXIO);
4469eda14cbcSMatt Macy 			} else {
4470eda14cbcSMatt Macy 				unexpected_error = B_TRUE;
4471eda14cbcSMatt Macy 			}
4472eda14cbcSMatt Macy 		}
4473eda14cbcSMatt Macy 	}
4474eda14cbcSMatt Macy 
4475eda14cbcSMatt Macy 	ops->vdev_op_io_done(zio);
4476eda14cbcSMatt Macy 
4477be181ee2SMartin Matuska 	if (unexpected_error && vd->vdev_remove_wanted == B_FALSE)
4478eda14cbcSMatt Macy 		VERIFY(vdev_probe(vd, zio) == NULL);
4479eda14cbcSMatt Macy 
4480eda14cbcSMatt Macy 	return (zio);
4481eda14cbcSMatt Macy }
4482eda14cbcSMatt Macy 
4483eda14cbcSMatt Macy /*
4484eda14cbcSMatt Macy  * This function is used to change the priority of an existing zio that is
4485eda14cbcSMatt Macy  * currently in-flight. This is used by the arc to upgrade priority in the
4486eda14cbcSMatt Macy  * event that a demand read is made for a block that is currently queued
4487eda14cbcSMatt Macy  * as a scrub or async read IO. Otherwise, the high priority read request
4488eda14cbcSMatt Macy  * would end up having to wait for the lower priority IO.
4489eda14cbcSMatt Macy  */
4490eda14cbcSMatt Macy void
4491eda14cbcSMatt Macy zio_change_priority(zio_t *pio, zio_priority_t priority)
4492eda14cbcSMatt Macy {
4493eda14cbcSMatt Macy 	zio_t *cio, *cio_next;
4494eda14cbcSMatt Macy 	zio_link_t *zl = NULL;
4495eda14cbcSMatt Macy 
4496eda14cbcSMatt Macy 	ASSERT3U(priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
4497eda14cbcSMatt Macy 
4498eda14cbcSMatt Macy 	if (pio->io_vd != NULL && pio->io_vd->vdev_ops->vdev_op_leaf) {
4499eda14cbcSMatt Macy 		vdev_queue_change_io_priority(pio, priority);
4500eda14cbcSMatt Macy 	} else {
4501eda14cbcSMatt Macy 		pio->io_priority = priority;
4502eda14cbcSMatt Macy 	}
4503eda14cbcSMatt Macy 
4504eda14cbcSMatt Macy 	mutex_enter(&pio->io_lock);
4505eda14cbcSMatt Macy 	for (cio = zio_walk_children(pio, &zl); cio != NULL; cio = cio_next) {
4506eda14cbcSMatt Macy 		cio_next = zio_walk_children(pio, &zl);
4507eda14cbcSMatt Macy 		zio_change_priority(cio, priority);
4508eda14cbcSMatt Macy 	}
4509eda14cbcSMatt Macy 	mutex_exit(&pio->io_lock);
4510eda14cbcSMatt Macy }
4511eda14cbcSMatt Macy 
4512eda14cbcSMatt Macy /*
4513eda14cbcSMatt Macy  * For non-raidz ZIOs, we can just copy aside the bad data read from the
4514eda14cbcSMatt Macy  * disk, and use that to finish the checksum ereport later.
4515eda14cbcSMatt Macy  */
4516eda14cbcSMatt Macy static void
4517eda14cbcSMatt Macy zio_vsd_default_cksum_finish(zio_cksum_report_t *zcr,
4518eda14cbcSMatt Macy     const abd_t *good_buf)
4519eda14cbcSMatt Macy {
4520eda14cbcSMatt Macy 	/* no processing needed */
4521eda14cbcSMatt Macy 	zfs_ereport_finish_checksum(zcr, good_buf, zcr->zcr_cbdata, B_FALSE);
4522eda14cbcSMatt Macy }
4523eda14cbcSMatt Macy 
4524eda14cbcSMatt Macy void
4525f9693befSMartin Matuska zio_vsd_default_cksum_report(zio_t *zio, zio_cksum_report_t *zcr)
4526eda14cbcSMatt Macy {
4527eda14cbcSMatt Macy 	void *abd = abd_alloc_sametype(zio->io_abd, zio->io_size);
4528eda14cbcSMatt Macy 
4529eda14cbcSMatt Macy 	abd_copy(abd, zio->io_abd, zio->io_size);
4530eda14cbcSMatt Macy 
4531eda14cbcSMatt Macy 	zcr->zcr_cbinfo = zio->io_size;
4532eda14cbcSMatt Macy 	zcr->zcr_cbdata = abd;
4533eda14cbcSMatt Macy 	zcr->zcr_finish = zio_vsd_default_cksum_finish;
4534eda14cbcSMatt Macy 	zcr->zcr_free = zio_abd_free;
4535eda14cbcSMatt Macy }
4536eda14cbcSMatt Macy 
4537eda14cbcSMatt Macy static zio_t *
4538eda14cbcSMatt Macy zio_vdev_io_assess(zio_t *zio)
4539eda14cbcSMatt Macy {
4540eda14cbcSMatt Macy 	vdev_t *vd = zio->io_vd;
4541eda14cbcSMatt Macy 
4542eda14cbcSMatt Macy 	if (zio_wait_for_children(zio, ZIO_CHILD_VDEV_BIT, ZIO_WAIT_DONE)) {
4543eda14cbcSMatt Macy 		return (NULL);
4544eda14cbcSMatt Macy 	}
4545eda14cbcSMatt Macy 
4546eda14cbcSMatt Macy 	if (vd == NULL && !(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
4547eda14cbcSMatt Macy 		spa_config_exit(zio->io_spa, SCL_ZIO, zio);
4548eda14cbcSMatt Macy 
4549eda14cbcSMatt Macy 	if (zio->io_vsd != NULL) {
4550eda14cbcSMatt Macy 		zio->io_vsd_ops->vsd_free(zio);
4551eda14cbcSMatt Macy 		zio->io_vsd = NULL;
4552eda14cbcSMatt Macy 	}
4553eda14cbcSMatt Macy 
45547a7741afSMartin Matuska 	/*
455587bf66d4SMartin Matuska 	 * If a Direct I/O operation has a checksum verify error then this I/O
455687bf66d4SMartin Matuska 	 * should not attempt to be issued again.
45577a7741afSMartin Matuska 	 */
45587a7741afSMartin Matuska 	if (zio->io_flags & ZIO_FLAG_DIO_CHKSUM_ERR) {
455987bf66d4SMartin Matuska 		if (zio->io_type == ZIO_TYPE_WRITE) {
45607a7741afSMartin Matuska 			ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_LOGICAL);
45617a7741afSMartin Matuska 			ASSERT3U(zio->io_error, ==, EIO);
456287bf66d4SMartin Matuska 		}
45637a7741afSMartin Matuska 		zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
45647a7741afSMartin Matuska 		return (zio);
45657a7741afSMartin Matuska 	}
45667a7741afSMartin Matuska 
4567eda14cbcSMatt Macy 	if (zio_injection_enabled && zio->io_error == 0)
4568eda14cbcSMatt Macy 		zio->io_error = zio_handle_fault_injection(zio, EIO);
4569eda14cbcSMatt Macy 
4570eda14cbcSMatt Macy 	/*
4571eda14cbcSMatt Macy 	 * If the I/O failed, determine whether we should attempt to retry it.
4572eda14cbcSMatt Macy 	 *
4573eda14cbcSMatt Macy 	 * On retry, we cut in line in the issue queue, since we don't want
4574eda14cbcSMatt Macy 	 * compression/checksumming/etc. work to prevent our (cheap) IO reissue.
4575eda14cbcSMatt Macy 	 */
4576eda14cbcSMatt Macy 	if (zio->io_error && vd == NULL &&
4577eda14cbcSMatt Macy 	    !(zio->io_flags & (ZIO_FLAG_DONT_RETRY | ZIO_FLAG_IO_RETRY))) {
4578eda14cbcSMatt Macy 		ASSERT(!(zio->io_flags & ZIO_FLAG_DONT_QUEUE));	/* not a leaf */
4579eda14cbcSMatt Macy 		ASSERT(!(zio->io_flags & ZIO_FLAG_IO_BYPASS));	/* not a leaf */
4580eda14cbcSMatt Macy 		zio->io_error = 0;
45814e8d558cSMartin Matuska 		zio->io_flags |= ZIO_FLAG_IO_RETRY | ZIO_FLAG_DONT_AGGREGATE;
4582eda14cbcSMatt Macy 		zio->io_stage = ZIO_STAGE_VDEV_IO_START >> 1;
4583eda14cbcSMatt Macy 		zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE,
4584eda14cbcSMatt Macy 		    zio_requeue_io_start_cut_in_line);
4585eda14cbcSMatt Macy 		return (NULL);
4586eda14cbcSMatt Macy 	}
4587eda14cbcSMatt Macy 
4588eda14cbcSMatt Macy 	/*
4589eda14cbcSMatt Macy 	 * If we got an error on a leaf device, convert it to ENXIO
4590eda14cbcSMatt Macy 	 * if the device is not accessible at all.
4591eda14cbcSMatt Macy 	 */
4592eda14cbcSMatt Macy 	if (zio->io_error && vd != NULL && vd->vdev_ops->vdev_op_leaf &&
4593eda14cbcSMatt Macy 	    !vdev_accessible(vd, zio))
4594eda14cbcSMatt Macy 		zio->io_error = SET_ERROR(ENXIO);
4595eda14cbcSMatt Macy 
4596eda14cbcSMatt Macy 	/*
4597eda14cbcSMatt Macy 	 * If we can't write to an interior vdev (mirror or RAID-Z),
4598eda14cbcSMatt Macy 	 * set vdev_cant_write so that we stop trying to allocate from it.
4599eda14cbcSMatt Macy 	 */
4600eda14cbcSMatt Macy 	if (zio->io_error == ENXIO && zio->io_type == ZIO_TYPE_WRITE &&
4601eda14cbcSMatt Macy 	    vd != NULL && !vd->vdev_ops->vdev_op_leaf) {
46026db169e9SMartin Matuska 		vdev_dbgmsg(vd, "zio_vdev_io_assess(zio=%px) setting "
46036db169e9SMartin Matuska 		    "cant_write=TRUE due to write failure with ENXIO",
46046db169e9SMartin Matuska 		    zio);
4605eda14cbcSMatt Macy 		vd->vdev_cant_write = B_TRUE;
4606eda14cbcSMatt Macy 	}
4607eda14cbcSMatt Macy 
4608eda14cbcSMatt Macy 	/*
4609*dd215568SMartin Matuska 	 * If a cache flush returns ENOTSUP we know that no future
4610eda14cbcSMatt Macy 	 * attempts will ever succeed. In this case we set a persistent
4611*dd215568SMartin Matuska 	 * boolean flag so that we don't bother with it in the future, and
4612*dd215568SMartin Matuska 	 * then we act like the flush succeeded.
4613eda14cbcSMatt Macy 	 */
4614*dd215568SMartin Matuska 	if (zio->io_error == ENOTSUP && zio->io_type == ZIO_TYPE_FLUSH &&
4615*dd215568SMartin Matuska 	    vd != NULL) {
4616eda14cbcSMatt Macy 		vd->vdev_nowritecache = B_TRUE;
4617*dd215568SMartin Matuska 		zio->io_error = 0;
4618*dd215568SMartin Matuska 	}
4619eda14cbcSMatt Macy 
4620eda14cbcSMatt Macy 	if (zio->io_error)
4621eda14cbcSMatt Macy 		zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
4622eda14cbcSMatt Macy 
4623eda14cbcSMatt Macy 	return (zio);
4624eda14cbcSMatt Macy }
4625eda14cbcSMatt Macy 
4626eda14cbcSMatt Macy void
4627eda14cbcSMatt Macy zio_vdev_io_reissue(zio_t *zio)
4628eda14cbcSMatt Macy {
4629eda14cbcSMatt Macy 	ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
4630eda14cbcSMatt Macy 	ASSERT(zio->io_error == 0);
4631eda14cbcSMatt Macy 
4632eda14cbcSMatt Macy 	zio->io_stage >>= 1;
4633eda14cbcSMatt Macy }
4634eda14cbcSMatt Macy 
4635eda14cbcSMatt Macy void
4636eda14cbcSMatt Macy zio_vdev_io_redone(zio_t *zio)
4637eda14cbcSMatt Macy {
4638eda14cbcSMatt Macy 	ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_DONE);
4639eda14cbcSMatt Macy 
4640eda14cbcSMatt Macy 	zio->io_stage >>= 1;
4641eda14cbcSMatt Macy }
4642eda14cbcSMatt Macy 
4643eda14cbcSMatt Macy void
4644eda14cbcSMatt Macy zio_vdev_io_bypass(zio_t *zio)
4645eda14cbcSMatt Macy {
4646eda14cbcSMatt Macy 	ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
4647eda14cbcSMatt Macy 	ASSERT(zio->io_error == 0);
4648eda14cbcSMatt Macy 
4649eda14cbcSMatt Macy 	zio->io_flags |= ZIO_FLAG_IO_BYPASS;
4650eda14cbcSMatt Macy 	zio->io_stage = ZIO_STAGE_VDEV_IO_ASSESS >> 1;
4651eda14cbcSMatt Macy }
4652eda14cbcSMatt Macy 
4653eda14cbcSMatt Macy /*
4654eda14cbcSMatt Macy  * ==========================================================================
4655eda14cbcSMatt Macy  * Encrypt and store encryption parameters
4656eda14cbcSMatt Macy  * ==========================================================================
4657eda14cbcSMatt Macy  */
4658eda14cbcSMatt Macy 
4659eda14cbcSMatt Macy 
4660eda14cbcSMatt Macy /*
4661eda14cbcSMatt Macy  * This function is used for ZIO_STAGE_ENCRYPT. It is responsible for
4662eda14cbcSMatt Macy  * managing the storage of encryption parameters and passing them to the
4663eda14cbcSMatt Macy  * lower-level encryption functions.
4664eda14cbcSMatt Macy  */
4665eda14cbcSMatt Macy static zio_t *
4666eda14cbcSMatt Macy zio_encrypt(zio_t *zio)
4667eda14cbcSMatt Macy {
4668eda14cbcSMatt Macy 	zio_prop_t *zp = &zio->io_prop;
4669eda14cbcSMatt Macy 	spa_t *spa = zio->io_spa;
4670eda14cbcSMatt Macy 	blkptr_t *bp = zio->io_bp;
4671eda14cbcSMatt Macy 	uint64_t psize = BP_GET_PSIZE(bp);
4672eda14cbcSMatt Macy 	uint64_t dsobj = zio->io_bookmark.zb_objset;
4673eda14cbcSMatt Macy 	dmu_object_type_t ot = BP_GET_TYPE(bp);
4674eda14cbcSMatt Macy 	void *enc_buf = NULL;
4675eda14cbcSMatt Macy 	abd_t *eabd = NULL;
4676eda14cbcSMatt Macy 	uint8_t salt[ZIO_DATA_SALT_LEN];
4677eda14cbcSMatt Macy 	uint8_t iv[ZIO_DATA_IV_LEN];
4678eda14cbcSMatt Macy 	uint8_t mac[ZIO_DATA_MAC_LEN];
4679eda14cbcSMatt Macy 	boolean_t no_crypt = B_FALSE;
4680eda14cbcSMatt Macy 
4681eda14cbcSMatt Macy 	/* the root zio already encrypted the data */
4682eda14cbcSMatt Macy 	if (zio->io_child_type == ZIO_CHILD_GANG)
4683eda14cbcSMatt Macy 		return (zio);
4684eda14cbcSMatt Macy 
4685eda14cbcSMatt Macy 	/* only ZIL blocks are re-encrypted on rewrite */
4686eda14cbcSMatt Macy 	if (!IO_IS_ALLOCATING(zio) && ot != DMU_OT_INTENT_LOG)
4687eda14cbcSMatt Macy 		return (zio);
4688eda14cbcSMatt Macy 
4689eda14cbcSMatt Macy 	if (!(zp->zp_encrypt || BP_IS_ENCRYPTED(bp))) {
4690eda14cbcSMatt Macy 		BP_SET_CRYPT(bp, B_FALSE);
4691eda14cbcSMatt Macy 		return (zio);
4692eda14cbcSMatt Macy 	}
4693eda14cbcSMatt Macy 
4694eda14cbcSMatt Macy 	/* if we are doing raw encryption set the provided encryption params */
4695eda14cbcSMatt Macy 	if (zio->io_flags & ZIO_FLAG_RAW_ENCRYPT) {
4696eda14cbcSMatt Macy 		ASSERT0(BP_GET_LEVEL(bp));
4697eda14cbcSMatt Macy 		BP_SET_CRYPT(bp, B_TRUE);
4698eda14cbcSMatt Macy 		BP_SET_BYTEORDER(bp, zp->zp_byteorder);
4699eda14cbcSMatt Macy 		if (ot != DMU_OT_OBJSET)
4700eda14cbcSMatt Macy 			zio_crypt_encode_mac_bp(bp, zp->zp_mac);
4701eda14cbcSMatt Macy 
4702eda14cbcSMatt Macy 		/* dnode blocks must be written out in the provided byteorder */
4703eda14cbcSMatt Macy 		if (zp->zp_byteorder != ZFS_HOST_BYTEORDER &&
4704eda14cbcSMatt Macy 		    ot == DMU_OT_DNODE) {
4705eda14cbcSMatt Macy 			void *bswap_buf = zio_buf_alloc(psize);
4706eda14cbcSMatt Macy 			abd_t *babd = abd_get_from_buf(bswap_buf, psize);
4707eda14cbcSMatt Macy 
4708eda14cbcSMatt Macy 			ASSERT3U(BP_GET_COMPRESS(bp), ==, ZIO_COMPRESS_OFF);
4709eda14cbcSMatt Macy 			abd_copy_to_buf(bswap_buf, zio->io_abd, psize);
4710eda14cbcSMatt Macy 			dmu_ot_byteswap[DMU_OT_BYTESWAP(ot)].ob_func(bswap_buf,
4711eda14cbcSMatt Macy 			    psize);
4712eda14cbcSMatt Macy 
4713eda14cbcSMatt Macy 			abd_take_ownership_of_buf(babd, B_TRUE);
4714eda14cbcSMatt Macy 			zio_push_transform(zio, babd, psize, psize, NULL);
4715eda14cbcSMatt Macy 		}
4716eda14cbcSMatt Macy 
4717eda14cbcSMatt Macy 		if (DMU_OT_IS_ENCRYPTED(ot))
4718eda14cbcSMatt Macy 			zio_crypt_encode_params_bp(bp, zp->zp_salt, zp->zp_iv);
4719eda14cbcSMatt Macy 		return (zio);
4720eda14cbcSMatt Macy 	}
4721eda14cbcSMatt Macy 
4722eda14cbcSMatt Macy 	/* indirect blocks only maintain a cksum of the lower level MACs */
4723eda14cbcSMatt Macy 	if (BP_GET_LEVEL(bp) > 0) {
4724eda14cbcSMatt Macy 		BP_SET_CRYPT(bp, B_TRUE);
4725eda14cbcSMatt Macy 		VERIFY0(zio_crypt_do_indirect_mac_checksum_abd(B_TRUE,
4726eda14cbcSMatt Macy 		    zio->io_orig_abd, BP_GET_LSIZE(bp), BP_SHOULD_BYTESWAP(bp),
4727eda14cbcSMatt Macy 		    mac));
4728eda14cbcSMatt Macy 		zio_crypt_encode_mac_bp(bp, mac);
4729eda14cbcSMatt Macy 		return (zio);
4730eda14cbcSMatt Macy 	}
4731eda14cbcSMatt Macy 
4732eda14cbcSMatt Macy 	/*
4733eda14cbcSMatt Macy 	 * Objset blocks are a special case since they have 2 256-bit MACs
4734eda14cbcSMatt Macy 	 * embedded within them.
4735eda14cbcSMatt Macy 	 */
4736eda14cbcSMatt Macy 	if (ot == DMU_OT_OBJSET) {
4737eda14cbcSMatt Macy 		ASSERT0(DMU_OT_IS_ENCRYPTED(ot));
4738eda14cbcSMatt Macy 		ASSERT3U(BP_GET_COMPRESS(bp), ==, ZIO_COMPRESS_OFF);
4739eda14cbcSMatt Macy 		BP_SET_CRYPT(bp, B_TRUE);
4740eda14cbcSMatt Macy 		VERIFY0(spa_do_crypt_objset_mac_abd(B_TRUE, spa, dsobj,
4741eda14cbcSMatt Macy 		    zio->io_abd, psize, BP_SHOULD_BYTESWAP(bp)));
4742eda14cbcSMatt Macy 		return (zio);
4743eda14cbcSMatt Macy 	}
4744eda14cbcSMatt Macy 
4745eda14cbcSMatt Macy 	/* unencrypted object types are only authenticated with a MAC */
4746eda14cbcSMatt Macy 	if (!DMU_OT_IS_ENCRYPTED(ot)) {
4747eda14cbcSMatt Macy 		BP_SET_CRYPT(bp, B_TRUE);
4748eda14cbcSMatt Macy 		VERIFY0(spa_do_crypt_mac_abd(B_TRUE, spa, dsobj,
4749eda14cbcSMatt Macy 		    zio->io_abd, psize, mac));
4750eda14cbcSMatt Macy 		zio_crypt_encode_mac_bp(bp, mac);
4751eda14cbcSMatt Macy 		return (zio);
4752eda14cbcSMatt Macy 	}
4753eda14cbcSMatt Macy 
4754eda14cbcSMatt Macy 	/*
4755eda14cbcSMatt Macy 	 * Later passes of sync-to-convergence may decide to rewrite data
4756eda14cbcSMatt Macy 	 * in place to avoid more disk reallocations. This presents a problem
4757eda14cbcSMatt Macy 	 * for encryption because this constitutes rewriting the new data with
4758eda14cbcSMatt Macy 	 * the same encryption key and IV. However, this only applies to blocks
4759eda14cbcSMatt Macy 	 * in the MOS (particularly the spacemaps) and we do not encrypt the
4760eda14cbcSMatt Macy 	 * MOS. We assert that the zio is allocating or an intent log write
4761eda14cbcSMatt Macy 	 * to enforce this.
4762eda14cbcSMatt Macy 	 */
4763eda14cbcSMatt Macy 	ASSERT(IO_IS_ALLOCATING(zio) || ot == DMU_OT_INTENT_LOG);
4764eda14cbcSMatt Macy 	ASSERT(BP_GET_LEVEL(bp) == 0 || ot == DMU_OT_INTENT_LOG);
4765eda14cbcSMatt Macy 	ASSERT(spa_feature_is_active(spa, SPA_FEATURE_ENCRYPTION));
4766eda14cbcSMatt Macy 	ASSERT3U(psize, !=, 0);
4767eda14cbcSMatt Macy 
4768eda14cbcSMatt Macy 	enc_buf = zio_buf_alloc(psize);
4769eda14cbcSMatt Macy 	eabd = abd_get_from_buf(enc_buf, psize);
4770eda14cbcSMatt Macy 	abd_take_ownership_of_buf(eabd, B_TRUE);
4771eda14cbcSMatt Macy 
4772eda14cbcSMatt Macy 	/*
4773eda14cbcSMatt Macy 	 * For an explanation of what encryption parameters are stored
4774eda14cbcSMatt Macy 	 * where, see the block comment in zio_crypt.c.
4775eda14cbcSMatt Macy 	 */
4776eda14cbcSMatt Macy 	if (ot == DMU_OT_INTENT_LOG) {
4777eda14cbcSMatt Macy 		zio_crypt_decode_params_bp(bp, salt, iv);
4778eda14cbcSMatt Macy 	} else {
4779eda14cbcSMatt Macy 		BP_SET_CRYPT(bp, B_TRUE);
4780eda14cbcSMatt Macy 	}
4781eda14cbcSMatt Macy 
4782eda14cbcSMatt Macy 	/* Perform the encryption. This should not fail */
4783eda14cbcSMatt Macy 	VERIFY0(spa_do_crypt_abd(B_TRUE, spa, &zio->io_bookmark,
4784eda14cbcSMatt Macy 	    BP_GET_TYPE(bp), BP_GET_DEDUP(bp), BP_SHOULD_BYTESWAP(bp),
4785eda14cbcSMatt Macy 	    salt, iv, mac, psize, zio->io_abd, eabd, &no_crypt));
4786eda14cbcSMatt Macy 
4787eda14cbcSMatt Macy 	/* encode encryption metadata into the bp */
4788eda14cbcSMatt Macy 	if (ot == DMU_OT_INTENT_LOG) {
4789eda14cbcSMatt Macy 		/*
4790eda14cbcSMatt Macy 		 * ZIL blocks store the MAC in the embedded checksum, so the
4791eda14cbcSMatt Macy 		 * transform must always be applied.
4792eda14cbcSMatt Macy 		 */
4793eda14cbcSMatt Macy 		zio_crypt_encode_mac_zil(enc_buf, mac);
4794eda14cbcSMatt Macy 		zio_push_transform(zio, eabd, psize, psize, NULL);
4795eda14cbcSMatt Macy 	} else {
4796eda14cbcSMatt Macy 		BP_SET_CRYPT(bp, B_TRUE);
4797eda14cbcSMatt Macy 		zio_crypt_encode_params_bp(bp, salt, iv);
4798eda14cbcSMatt Macy 		zio_crypt_encode_mac_bp(bp, mac);
4799eda14cbcSMatt Macy 
4800eda14cbcSMatt Macy 		if (no_crypt) {
4801eda14cbcSMatt Macy 			ASSERT3U(ot, ==, DMU_OT_DNODE);
4802eda14cbcSMatt Macy 			abd_free(eabd);
4803eda14cbcSMatt Macy 		} else {
4804eda14cbcSMatt Macy 			zio_push_transform(zio, eabd, psize, psize, NULL);
4805eda14cbcSMatt Macy 		}
4806eda14cbcSMatt Macy 	}
4807eda14cbcSMatt Macy 
4808eda14cbcSMatt Macy 	return (zio);
4809eda14cbcSMatt Macy }
4810eda14cbcSMatt Macy 
4811eda14cbcSMatt Macy /*
4812eda14cbcSMatt Macy  * ==========================================================================
4813eda14cbcSMatt Macy  * Generate and verify checksums
4814eda14cbcSMatt Macy  * ==========================================================================
4815eda14cbcSMatt Macy  */
4816eda14cbcSMatt Macy static zio_t *
4817eda14cbcSMatt Macy zio_checksum_generate(zio_t *zio)
4818eda14cbcSMatt Macy {
4819eda14cbcSMatt Macy 	blkptr_t *bp = zio->io_bp;
4820eda14cbcSMatt Macy 	enum zio_checksum checksum;
4821eda14cbcSMatt Macy 
4822eda14cbcSMatt Macy 	if (bp == NULL) {
4823eda14cbcSMatt Macy 		/*
4824eda14cbcSMatt Macy 		 * This is zio_write_phys().
4825eda14cbcSMatt Macy 		 * We're either generating a label checksum, or none at all.
4826eda14cbcSMatt Macy 		 */
4827eda14cbcSMatt Macy 		checksum = zio->io_prop.zp_checksum;
4828eda14cbcSMatt Macy 
4829eda14cbcSMatt Macy 		if (checksum == ZIO_CHECKSUM_OFF)
4830eda14cbcSMatt Macy 			return (zio);
4831eda14cbcSMatt Macy 
4832eda14cbcSMatt Macy 		ASSERT(checksum == ZIO_CHECKSUM_LABEL);
4833eda14cbcSMatt Macy 	} else {
4834eda14cbcSMatt Macy 		if (BP_IS_GANG(bp) && zio->io_child_type == ZIO_CHILD_GANG) {
4835eda14cbcSMatt Macy 			ASSERT(!IO_IS_ALLOCATING(zio));
4836eda14cbcSMatt Macy 			checksum = ZIO_CHECKSUM_GANG_HEADER;
4837eda14cbcSMatt Macy 		} else {
4838eda14cbcSMatt Macy 			checksum = BP_GET_CHECKSUM(bp);
4839eda14cbcSMatt Macy 		}
4840eda14cbcSMatt Macy 	}
4841eda14cbcSMatt Macy 
4842eda14cbcSMatt Macy 	zio_checksum_compute(zio, checksum, zio->io_abd, zio->io_size);
4843eda14cbcSMatt Macy 
4844eda14cbcSMatt Macy 	return (zio);
4845eda14cbcSMatt Macy }
4846eda14cbcSMatt Macy 
4847eda14cbcSMatt Macy static zio_t *
4848eda14cbcSMatt Macy zio_checksum_verify(zio_t *zio)
4849eda14cbcSMatt Macy {
4850eda14cbcSMatt Macy 	zio_bad_cksum_t info;
4851eda14cbcSMatt Macy 	blkptr_t *bp = zio->io_bp;
4852eda14cbcSMatt Macy 	int error;
4853eda14cbcSMatt Macy 
4854eda14cbcSMatt Macy 	ASSERT(zio->io_vd != NULL);
4855eda14cbcSMatt Macy 
4856eda14cbcSMatt Macy 	if (bp == NULL) {
4857eda14cbcSMatt Macy 		/*
4858eda14cbcSMatt Macy 		 * This is zio_read_phys().
4859eda14cbcSMatt Macy 		 * We're either verifying a label checksum, or nothing at all.
4860eda14cbcSMatt Macy 		 */
4861eda14cbcSMatt Macy 		if (zio->io_prop.zp_checksum == ZIO_CHECKSUM_OFF)
4862eda14cbcSMatt Macy 			return (zio);
4863eda14cbcSMatt Macy 
48647877fdebSMatt Macy 		ASSERT3U(zio->io_prop.zp_checksum, ==, ZIO_CHECKSUM_LABEL);
4865eda14cbcSMatt Macy 	}
4866eda14cbcSMatt Macy 
486787bf66d4SMartin Matuska 	ASSERT0(zio->io_flags & ZIO_FLAG_DIO_CHKSUM_ERR);
486887bf66d4SMartin Matuska 	IMPLY(zio->io_flags & ZIO_FLAG_DIO_READ,
486987bf66d4SMartin Matuska 	    !(zio->io_flags & ZIO_FLAG_SPECULATIVE));
487087bf66d4SMartin Matuska 
4871eda14cbcSMatt Macy 	if ((error = zio_checksum_error(zio, &info)) != 0) {
4872eda14cbcSMatt Macy 		zio->io_error = error;
4873eda14cbcSMatt Macy 		if (error == ECKSUM &&
4874eda14cbcSMatt Macy 		    !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
487587bf66d4SMartin Matuska 			if (zio->io_flags & ZIO_FLAG_DIO_READ) {
487687bf66d4SMartin Matuska 				zio->io_flags |= ZIO_FLAG_DIO_CHKSUM_ERR;
487787bf66d4SMartin Matuska 				zio_t *pio = zio_unique_parent(zio);
487887bf66d4SMartin Matuska 				/*
487987bf66d4SMartin Matuska 				 * Any Direct I/O read that has a checksum
488087bf66d4SMartin Matuska 				 * error must be treated as suspicous as the
488187bf66d4SMartin Matuska 				 * contents of the buffer could be getting
488287bf66d4SMartin Matuska 				 * manipulated while the I/O is taking place.
488387bf66d4SMartin Matuska 				 *
488487bf66d4SMartin Matuska 				 * The checksum verify error will only be
488587bf66d4SMartin Matuska 				 * reported here for disk and file VDEV's and
488687bf66d4SMartin Matuska 				 * will be reported on those that the failure
488787bf66d4SMartin Matuska 				 * occurred on. Other types of VDEV's report the
488887bf66d4SMartin Matuska 				 * verify failure in their own code paths.
488987bf66d4SMartin Matuska 				 */
489087bf66d4SMartin Matuska 				if (pio->io_child_type == ZIO_CHILD_LOGICAL) {
489187bf66d4SMartin Matuska 					zio_dio_chksum_verify_error_report(zio);
489287bf66d4SMartin Matuska 				}
489387bf66d4SMartin Matuska 			} else {
4894eda14cbcSMatt Macy 				mutex_enter(&zio->io_vd->vdev_stat_lock);
4895eda14cbcSMatt Macy 				zio->io_vd->vdev_stat.vs_checksum_errors++;
4896eda14cbcSMatt Macy 				mutex_exit(&zio->io_vd->vdev_stat_lock);
4897bb2d13b6SMartin Matuska 				(void) zfs_ereport_start_checksum(zio->io_spa,
4898bb2d13b6SMartin Matuska 				    zio->io_vd, &zio->io_bookmark, zio,
4899bb2d13b6SMartin Matuska 				    zio->io_offset, zio->io_size, &info);
49002c48331dSMatt Macy 			}
4901eda14cbcSMatt Macy 		}
490287bf66d4SMartin Matuska 	}
4903eda14cbcSMatt Macy 
4904eda14cbcSMatt Macy 	return (zio);
4905eda14cbcSMatt Macy }
4906eda14cbcSMatt Macy 
49077a7741afSMartin Matuska static zio_t *
49087a7741afSMartin Matuska zio_dio_checksum_verify(zio_t *zio)
49097a7741afSMartin Matuska {
49107a7741afSMartin Matuska 	zio_t *pio = zio_unique_parent(zio);
49117a7741afSMartin Matuska 	int error;
49127a7741afSMartin Matuska 
49137a7741afSMartin Matuska 	ASSERT3P(zio->io_vd, !=, NULL);
49147a7741afSMartin Matuska 	ASSERT3P(zio->io_bp, !=, NULL);
49157a7741afSMartin Matuska 	ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_VDEV);
49167a7741afSMartin Matuska 	ASSERT3U(zio->io_type, ==, ZIO_TYPE_WRITE);
49177a7741afSMartin Matuska 	ASSERT3B(pio->io_prop.zp_direct_write, ==, B_TRUE);
49187a7741afSMartin Matuska 	ASSERT3U(pio->io_child_type, ==, ZIO_CHILD_LOGICAL);
49197a7741afSMartin Matuska 
49207a7741afSMartin Matuska 	if (zfs_vdev_direct_write_verify == 0 || zio->io_error != 0)
49217a7741afSMartin Matuska 		goto out;
49227a7741afSMartin Matuska 
49237a7741afSMartin Matuska 	if ((error = zio_checksum_error(zio, NULL)) != 0) {
49247a7741afSMartin Matuska 		zio->io_error = error;
49257a7741afSMartin Matuska 		if (error == ECKSUM) {
49267a7741afSMartin Matuska 			zio->io_flags |= ZIO_FLAG_DIO_CHKSUM_ERR;
492787bf66d4SMartin Matuska 			zio_dio_chksum_verify_error_report(zio);
49287a7741afSMartin Matuska 		}
49297a7741afSMartin Matuska 	}
49307a7741afSMartin Matuska 
49317a7741afSMartin Matuska out:
49327a7741afSMartin Matuska 	return (zio);
49337a7741afSMartin Matuska }
49347a7741afSMartin Matuska 
49357a7741afSMartin Matuska 
4936eda14cbcSMatt Macy /*
4937eda14cbcSMatt Macy  * Called by RAID-Z to ensure we don't compute the checksum twice.
4938eda14cbcSMatt Macy  */
4939eda14cbcSMatt Macy void
4940eda14cbcSMatt Macy zio_checksum_verified(zio_t *zio)
4941eda14cbcSMatt Macy {
4942eda14cbcSMatt Macy 	zio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY;
4943eda14cbcSMatt Macy }
4944eda14cbcSMatt Macy 
4945eda14cbcSMatt Macy /*
494687bf66d4SMartin Matuska  * Report Direct I/O checksum verify error and create ZED event.
494787bf66d4SMartin Matuska  */
494887bf66d4SMartin Matuska void
494987bf66d4SMartin Matuska zio_dio_chksum_verify_error_report(zio_t *zio)
495087bf66d4SMartin Matuska {
495187bf66d4SMartin Matuska 	ASSERT(zio->io_flags & ZIO_FLAG_DIO_CHKSUM_ERR);
495287bf66d4SMartin Matuska 
495387bf66d4SMartin Matuska 	if (zio->io_child_type == ZIO_CHILD_LOGICAL)
495487bf66d4SMartin Matuska 		return;
495587bf66d4SMartin Matuska 
495687bf66d4SMartin Matuska 	mutex_enter(&zio->io_vd->vdev_stat_lock);
495787bf66d4SMartin Matuska 	zio->io_vd->vdev_stat.vs_dio_verify_errors++;
495887bf66d4SMartin Matuska 	mutex_exit(&zio->io_vd->vdev_stat_lock);
495987bf66d4SMartin Matuska 	if (zio->io_type == ZIO_TYPE_WRITE) {
496087bf66d4SMartin Matuska 		/*
496187bf66d4SMartin Matuska 		 * Convert checksum error for writes into EIO.
496287bf66d4SMartin Matuska 		 */
496387bf66d4SMartin Matuska 		zio->io_error = SET_ERROR(EIO);
496487bf66d4SMartin Matuska 		/*
496587bf66d4SMartin Matuska 		 * Report dio_verify_wr ZED event.
496687bf66d4SMartin Matuska 		 */
496787bf66d4SMartin Matuska 		(void) zfs_ereport_post(FM_EREPORT_ZFS_DIO_VERIFY_WR,
496887bf66d4SMartin Matuska 		    zio->io_spa,  zio->io_vd, &zio->io_bookmark, zio, 0);
496987bf66d4SMartin Matuska 	} else {
497087bf66d4SMartin Matuska 		/*
497187bf66d4SMartin Matuska 		 * Report dio_verify_rd ZED event.
497287bf66d4SMartin Matuska 		 */
497387bf66d4SMartin Matuska 		(void) zfs_ereport_post(FM_EREPORT_ZFS_DIO_VERIFY_RD,
497487bf66d4SMartin Matuska 		    zio->io_spa, zio->io_vd, &zio->io_bookmark, zio, 0);
497587bf66d4SMartin Matuska 	}
497687bf66d4SMartin Matuska }
497787bf66d4SMartin Matuska 
497887bf66d4SMartin Matuska /*
4979eda14cbcSMatt Macy  * ==========================================================================
4980eda14cbcSMatt Macy  * Error rank.  Error are ranked in the order 0, ENXIO, ECKSUM, EIO, other.
4981eda14cbcSMatt Macy  * An error of 0 indicates success.  ENXIO indicates whole-device failure,
4982eda14cbcSMatt Macy  * which may be transient (e.g. unplugged) or permanent.  ECKSUM and EIO
4983eda14cbcSMatt Macy  * indicate errors that are specific to one I/O, and most likely permanent.
4984eda14cbcSMatt Macy  * Any other error is presumed to be worse because we weren't expecting it.
4985eda14cbcSMatt Macy  * ==========================================================================
4986eda14cbcSMatt Macy  */
4987eda14cbcSMatt Macy int
4988eda14cbcSMatt Macy zio_worst_error(int e1, int e2)
4989eda14cbcSMatt Macy {
4990eda14cbcSMatt Macy 	static int zio_error_rank[] = { 0, ENXIO, ECKSUM, EIO };
4991eda14cbcSMatt Macy 	int r1, r2;
4992eda14cbcSMatt Macy 
4993eda14cbcSMatt Macy 	for (r1 = 0; r1 < sizeof (zio_error_rank) / sizeof (int); r1++)
4994eda14cbcSMatt Macy 		if (e1 == zio_error_rank[r1])
4995eda14cbcSMatt Macy 			break;
4996eda14cbcSMatt Macy 
4997eda14cbcSMatt Macy 	for (r2 = 0; r2 < sizeof (zio_error_rank) / sizeof (int); r2++)
4998eda14cbcSMatt Macy 		if (e2 == zio_error_rank[r2])
4999eda14cbcSMatt Macy 			break;
5000eda14cbcSMatt Macy 
5001eda14cbcSMatt Macy 	return (r1 > r2 ? e1 : e2);
5002eda14cbcSMatt Macy }
5003eda14cbcSMatt Macy 
5004eda14cbcSMatt Macy /*
5005eda14cbcSMatt Macy  * ==========================================================================
5006eda14cbcSMatt Macy  * I/O completion
5007eda14cbcSMatt Macy  * ==========================================================================
5008eda14cbcSMatt Macy  */
5009eda14cbcSMatt Macy static zio_t *
5010eda14cbcSMatt Macy zio_ready(zio_t *zio)
5011eda14cbcSMatt Macy {
5012eda14cbcSMatt Macy 	blkptr_t *bp = zio->io_bp;
5013eda14cbcSMatt Macy 	zio_t *pio, *pio_next;
5014eda14cbcSMatt Macy 	zio_link_t *zl = NULL;
5015eda14cbcSMatt Macy 
5016315ee00fSMartin Matuska 	if (zio_wait_for_children(zio, ZIO_CHILD_LOGICAL_BIT |
5017315ee00fSMartin Matuska 	    ZIO_CHILD_GANG_BIT | ZIO_CHILD_DDT_BIT, ZIO_WAIT_READY)) {
5018eda14cbcSMatt Macy 		return (NULL);
5019eda14cbcSMatt Macy 	}
5020eda14cbcSMatt Macy 
5021eda14cbcSMatt Macy 	if (zio->io_ready) {
5022eda14cbcSMatt Macy 		ASSERT(IO_IS_ALLOCATING(zio));
5023783d3ff6SMartin Matuska 		ASSERT(BP_GET_LOGICAL_BIRTH(bp) == zio->io_txg ||
5024783d3ff6SMartin Matuska 		    BP_IS_HOLE(bp) || (zio->io_flags & ZIO_FLAG_NOPWRITE));
5025eda14cbcSMatt Macy 		ASSERT(zio->io_children[ZIO_CHILD_GANG][ZIO_WAIT_READY] == 0);
5026eda14cbcSMatt Macy 
5027eda14cbcSMatt Macy 		zio->io_ready(zio);
5028eda14cbcSMatt Macy 	}
5029eda14cbcSMatt Macy 
50300a97523dSMartin Matuska #ifdef ZFS_DEBUG
5031eda14cbcSMatt Macy 	if (bp != NULL && bp != &zio->io_bp_copy)
5032eda14cbcSMatt Macy 		zio->io_bp_copy = *bp;
50330a97523dSMartin Matuska #endif
5034eda14cbcSMatt Macy 
5035eda14cbcSMatt Macy 	if (zio->io_error != 0) {
5036eda14cbcSMatt Macy 		zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
5037eda14cbcSMatt Macy 
5038eda14cbcSMatt Macy 		if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
5039eda14cbcSMatt Macy 			ASSERT(IO_IS_ALLOCATING(zio));
5040eda14cbcSMatt Macy 			ASSERT(zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
5041eda14cbcSMatt Macy 			ASSERT(zio->io_metaslab_class != NULL);
504214c2e0a0SMartin Matuska 			ASSERT(ZIO_HAS_ALLOCATOR(zio));
5043eda14cbcSMatt Macy 
5044eda14cbcSMatt Macy 			/*
5045eda14cbcSMatt Macy 			 * We were unable to allocate anything, unreserve and
5046eda14cbcSMatt Macy 			 * issue the next I/O to allocate.
5047eda14cbcSMatt Macy 			 */
5048eda14cbcSMatt Macy 			metaslab_class_throttle_unreserve(
5049eda14cbcSMatt Macy 			    zio->io_metaslab_class, zio->io_prop.zp_copies,
5050eda14cbcSMatt Macy 			    zio->io_allocator, zio);
5051eda14cbcSMatt Macy 			zio_allocate_dispatch(zio->io_spa, zio->io_allocator);
5052eda14cbcSMatt Macy 		}
5053eda14cbcSMatt Macy 	}
5054eda14cbcSMatt Macy 
5055eda14cbcSMatt Macy 	mutex_enter(&zio->io_lock);
5056eda14cbcSMatt Macy 	zio->io_state[ZIO_WAIT_READY] = 1;
5057eda14cbcSMatt Macy 	pio = zio_walk_parents(zio, &zl);
5058eda14cbcSMatt Macy 	mutex_exit(&zio->io_lock);
5059eda14cbcSMatt Macy 
5060eda14cbcSMatt Macy 	/*
5061eda14cbcSMatt Macy 	 * As we notify zio's parents, new parents could be added.
5062eda14cbcSMatt Macy 	 * New parents go to the head of zio's io_parent_list, however,
5063eda14cbcSMatt Macy 	 * so we will (correctly) not notify them.  The remainder of zio's
5064eda14cbcSMatt Macy 	 * io_parent_list, from 'pio_next' onward, cannot change because
5065eda14cbcSMatt Macy 	 * all parents must wait for us to be done before they can be done.
5066eda14cbcSMatt Macy 	 */
5067eda14cbcSMatt Macy 	for (; pio != NULL; pio = pio_next) {
5068eda14cbcSMatt Macy 		pio_next = zio_walk_parents(zio, &zl);
5069eda14cbcSMatt Macy 		zio_notify_parent(pio, zio, ZIO_WAIT_READY, NULL);
5070eda14cbcSMatt Macy 	}
5071eda14cbcSMatt Macy 
5072eda14cbcSMatt Macy 	if (zio->io_flags & ZIO_FLAG_NODATA) {
50732a58b312SMartin Matuska 		if (bp != NULL && BP_IS_GANG(bp)) {
5074eda14cbcSMatt Macy 			zio->io_flags &= ~ZIO_FLAG_NODATA;
5075eda14cbcSMatt Macy 		} else {
5076eda14cbcSMatt Macy 			ASSERT((uintptr_t)zio->io_abd < SPA_MAXBLOCKSIZE);
5077eda14cbcSMatt Macy 			zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES;
5078eda14cbcSMatt Macy 		}
5079eda14cbcSMatt Macy 	}
5080eda14cbcSMatt Macy 
5081eda14cbcSMatt Macy 	if (zio_injection_enabled &&
5082eda14cbcSMatt Macy 	    zio->io_spa->spa_syncing_txg == zio->io_txg)
5083eda14cbcSMatt Macy 		zio_handle_ignored_writes(zio);
5084eda14cbcSMatt Macy 
5085eda14cbcSMatt Macy 	return (zio);
5086eda14cbcSMatt Macy }
5087eda14cbcSMatt Macy 
5088eda14cbcSMatt Macy /*
5089eda14cbcSMatt Macy  * Update the allocation throttle accounting.
5090eda14cbcSMatt Macy  */
5091eda14cbcSMatt Macy static void
5092eda14cbcSMatt Macy zio_dva_throttle_done(zio_t *zio)
5093eda14cbcSMatt Macy {
5094eda14cbcSMatt Macy 	zio_t *lio __maybe_unused = zio->io_logical;
5095eda14cbcSMatt Macy 	zio_t *pio = zio_unique_parent(zio);
5096eda14cbcSMatt Macy 	vdev_t *vd = zio->io_vd;
5097eda14cbcSMatt Macy 	int flags = METASLAB_ASYNC_ALLOC;
5098eda14cbcSMatt Macy 
5099eda14cbcSMatt Macy 	ASSERT3P(zio->io_bp, !=, NULL);
5100eda14cbcSMatt Macy 	ASSERT3U(zio->io_type, ==, ZIO_TYPE_WRITE);
5101eda14cbcSMatt Macy 	ASSERT3U(zio->io_priority, ==, ZIO_PRIORITY_ASYNC_WRITE);
5102eda14cbcSMatt Macy 	ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_VDEV);
5103eda14cbcSMatt Macy 	ASSERT(vd != NULL);
5104eda14cbcSMatt Macy 	ASSERT3P(vd, ==, vd->vdev_top);
5105eda14cbcSMatt Macy 	ASSERT(zio_injection_enabled || !(zio->io_flags & ZIO_FLAG_IO_RETRY));
5106eda14cbcSMatt Macy 	ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REPAIR));
5107eda14cbcSMatt Macy 	ASSERT(zio->io_flags & ZIO_FLAG_IO_ALLOCATING);
5108eda14cbcSMatt Macy 	ASSERT(!(lio->io_flags & ZIO_FLAG_IO_REWRITE));
5109eda14cbcSMatt Macy 	ASSERT(!(lio->io_orig_flags & ZIO_FLAG_NODATA));
5110eda14cbcSMatt Macy 
5111eda14cbcSMatt Macy 	/*
5112eda14cbcSMatt Macy 	 * Parents of gang children can have two flavors -- ones that
5113eda14cbcSMatt Macy 	 * allocated the gang header (will have ZIO_FLAG_IO_REWRITE set)
5114eda14cbcSMatt Macy 	 * and ones that allocated the constituent blocks. The allocation
5115eda14cbcSMatt Macy 	 * throttle needs to know the allocating parent zio so we must find
5116eda14cbcSMatt Macy 	 * it here.
5117eda14cbcSMatt Macy 	 */
5118eda14cbcSMatt Macy 	if (pio->io_child_type == ZIO_CHILD_GANG) {
5119eda14cbcSMatt Macy 		/*
5120eda14cbcSMatt Macy 		 * If our parent is a rewrite gang child then our grandparent
5121eda14cbcSMatt Macy 		 * would have been the one that performed the allocation.
5122eda14cbcSMatt Macy 		 */
5123eda14cbcSMatt Macy 		if (pio->io_flags & ZIO_FLAG_IO_REWRITE)
5124eda14cbcSMatt Macy 			pio = zio_unique_parent(pio);
5125eda14cbcSMatt Macy 		flags |= METASLAB_GANG_CHILD;
5126eda14cbcSMatt Macy 	}
5127eda14cbcSMatt Macy 
5128eda14cbcSMatt Macy 	ASSERT(IO_IS_ALLOCATING(pio));
512914c2e0a0SMartin Matuska 	ASSERT(ZIO_HAS_ALLOCATOR(pio));
5130eda14cbcSMatt Macy 	ASSERT3P(zio, !=, zio->io_logical);
5131eda14cbcSMatt Macy 	ASSERT(zio->io_logical != NULL);
5132eda14cbcSMatt Macy 	ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REPAIR));
5133eda14cbcSMatt Macy 	ASSERT0(zio->io_flags & ZIO_FLAG_NOPWRITE);
5134eda14cbcSMatt Macy 	ASSERT(zio->io_metaslab_class != NULL);
5135eda14cbcSMatt Macy 
5136eda14cbcSMatt Macy 	mutex_enter(&pio->io_lock);
5137eda14cbcSMatt Macy 	metaslab_group_alloc_decrement(zio->io_spa, vd->vdev_id, pio, flags,
5138eda14cbcSMatt Macy 	    pio->io_allocator, B_TRUE);
5139eda14cbcSMatt Macy 	mutex_exit(&pio->io_lock);
5140eda14cbcSMatt Macy 
5141eda14cbcSMatt Macy 	metaslab_class_throttle_unreserve(zio->io_metaslab_class, 1,
5142eda14cbcSMatt Macy 	    pio->io_allocator, pio);
5143eda14cbcSMatt Macy 
5144eda14cbcSMatt Macy 	/*
5145eda14cbcSMatt Macy 	 * Call into the pipeline to see if there is more work that
5146eda14cbcSMatt Macy 	 * needs to be done. If there is work to be done it will be
5147eda14cbcSMatt Macy 	 * dispatched to another taskq thread.
5148eda14cbcSMatt Macy 	 */
5149eda14cbcSMatt Macy 	zio_allocate_dispatch(zio->io_spa, pio->io_allocator);
5150eda14cbcSMatt Macy }
5151eda14cbcSMatt Macy 
5152eda14cbcSMatt Macy static zio_t *
5153eda14cbcSMatt Macy zio_done(zio_t *zio)
5154eda14cbcSMatt Macy {
5155eda14cbcSMatt Macy 	/*
5156eda14cbcSMatt Macy 	 * Always attempt to keep stack usage minimal here since
5157eda14cbcSMatt Macy 	 * we can be called recursively up to 19 levels deep.
5158eda14cbcSMatt Macy 	 */
5159eda14cbcSMatt Macy 	const uint64_t psize = zio->io_size;
5160eda14cbcSMatt Macy 	zio_t *pio, *pio_next;
5161eda14cbcSMatt Macy 	zio_link_t *zl = NULL;
5162eda14cbcSMatt Macy 
5163eda14cbcSMatt Macy 	/*
5164eda14cbcSMatt Macy 	 * If our children haven't all completed,
5165eda14cbcSMatt Macy 	 * wait for them and then repeat this pipeline stage.
5166eda14cbcSMatt Macy 	 */
5167eda14cbcSMatt Macy 	if (zio_wait_for_children(zio, ZIO_CHILD_ALL_BITS, ZIO_WAIT_DONE)) {
5168eda14cbcSMatt Macy 		return (NULL);
5169eda14cbcSMatt Macy 	}
5170eda14cbcSMatt Macy 
5171eda14cbcSMatt Macy 	/*
5172eda14cbcSMatt Macy 	 * If the allocation throttle is enabled, then update the accounting.
5173eda14cbcSMatt Macy 	 * We only track child I/Os that are part of an allocating async
5174eda14cbcSMatt Macy 	 * write. We must do this since the allocation is performed
5175eda14cbcSMatt Macy 	 * by the logical I/O but the actual write is done by child I/Os.
5176eda14cbcSMatt Macy 	 */
5177eda14cbcSMatt Macy 	if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING &&
5178eda14cbcSMatt Macy 	    zio->io_child_type == ZIO_CHILD_VDEV) {
5179eda14cbcSMatt Macy 		ASSERT(zio->io_metaslab_class != NULL);
5180eda14cbcSMatt Macy 		ASSERT(zio->io_metaslab_class->mc_alloc_throttle_enabled);
5181eda14cbcSMatt Macy 		zio_dva_throttle_done(zio);
5182eda14cbcSMatt Macy 	}
5183eda14cbcSMatt Macy 
5184eda14cbcSMatt Macy 	/*
5185eda14cbcSMatt Macy 	 * If the allocation throttle is enabled, verify that
5186eda14cbcSMatt Macy 	 * we have decremented the refcounts for every I/O that was throttled.
5187eda14cbcSMatt Macy 	 */
5188eda14cbcSMatt Macy 	if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
5189eda14cbcSMatt Macy 		ASSERT(zio->io_type == ZIO_TYPE_WRITE);
5190eda14cbcSMatt Macy 		ASSERT(zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
5191eda14cbcSMatt Macy 		ASSERT(zio->io_bp != NULL);
519214c2e0a0SMartin Matuska 		ASSERT(ZIO_HAS_ALLOCATOR(zio));
5193eda14cbcSMatt Macy 
5194eda14cbcSMatt Macy 		metaslab_group_alloc_verify(zio->io_spa, zio->io_bp, zio,
5195eda14cbcSMatt Macy 		    zio->io_allocator);
51967877fdebSMatt Macy 		VERIFY(zfs_refcount_not_held(&zio->io_metaslab_class->
51977877fdebSMatt Macy 		    mc_allocator[zio->io_allocator].mca_alloc_slots, zio));
5198eda14cbcSMatt Macy 	}
5199eda14cbcSMatt Macy 
5200eda14cbcSMatt Macy 
5201eda14cbcSMatt Macy 	for (int c = 0; c < ZIO_CHILD_TYPES; c++)
5202eda14cbcSMatt Macy 		for (int w = 0; w < ZIO_WAIT_TYPES; w++)
5203eda14cbcSMatt Macy 			ASSERT(zio->io_children[c][w] == 0);
5204eda14cbcSMatt Macy 
5205eda14cbcSMatt Macy 	if (zio->io_bp != NULL && !BP_IS_EMBEDDED(zio->io_bp)) {
5206eda14cbcSMatt Macy 		ASSERT(zio->io_bp->blk_pad[0] == 0);
5207eda14cbcSMatt Macy 		ASSERT(zio->io_bp->blk_pad[1] == 0);
5208da5137abSMartin Matuska 		ASSERT(memcmp(zio->io_bp, &zio->io_bp_copy,
5209eda14cbcSMatt Macy 		    sizeof (blkptr_t)) == 0 ||
5210eda14cbcSMatt Macy 		    (zio->io_bp == zio_unique_parent(zio)->io_bp));
5211eda14cbcSMatt Macy 		if (zio->io_type == ZIO_TYPE_WRITE && !BP_IS_HOLE(zio->io_bp) &&
5212eda14cbcSMatt Macy 		    zio->io_bp_override == NULL &&
5213eda14cbcSMatt Macy 		    !(zio->io_flags & ZIO_FLAG_IO_REPAIR)) {
5214eda14cbcSMatt Macy 			ASSERT3U(zio->io_prop.zp_copies, <=,
5215eda14cbcSMatt Macy 			    BP_GET_NDVAS(zio->io_bp));
5216eda14cbcSMatt Macy 			ASSERT(BP_COUNT_GANG(zio->io_bp) == 0 ||
5217eda14cbcSMatt Macy 			    (BP_COUNT_GANG(zio->io_bp) ==
5218eda14cbcSMatt Macy 			    BP_GET_NDVAS(zio->io_bp)));
5219eda14cbcSMatt Macy 		}
5220eda14cbcSMatt Macy 		if (zio->io_flags & ZIO_FLAG_NOPWRITE)
5221eda14cbcSMatt Macy 			VERIFY(BP_EQUAL(zio->io_bp, &zio->io_bp_orig));
5222eda14cbcSMatt Macy 	}
5223eda14cbcSMatt Macy 
5224eda14cbcSMatt Macy 	/*
5225eda14cbcSMatt Macy 	 * If there were child vdev/gang/ddt errors, they apply to us now.
5226eda14cbcSMatt Macy 	 */
5227eda14cbcSMatt Macy 	zio_inherit_child_errors(zio, ZIO_CHILD_VDEV);
5228eda14cbcSMatt Macy 	zio_inherit_child_errors(zio, ZIO_CHILD_GANG);
5229eda14cbcSMatt Macy 	zio_inherit_child_errors(zio, ZIO_CHILD_DDT);
5230eda14cbcSMatt Macy 
5231eda14cbcSMatt Macy 	/*
5232eda14cbcSMatt Macy 	 * If the I/O on the transformed data was successful, generate any
5233eda14cbcSMatt Macy 	 * checksum reports now while we still have the transformed data.
5234eda14cbcSMatt Macy 	 */
5235eda14cbcSMatt Macy 	if (zio->io_error == 0) {
5236eda14cbcSMatt Macy 		while (zio->io_cksum_report != NULL) {
5237eda14cbcSMatt Macy 			zio_cksum_report_t *zcr = zio->io_cksum_report;
5238eda14cbcSMatt Macy 			uint64_t align = zcr->zcr_align;
5239eda14cbcSMatt Macy 			uint64_t asize = P2ROUNDUP(psize, align);
5240eda14cbcSMatt Macy 			abd_t *adata = zio->io_abd;
5241eda14cbcSMatt Macy 
524216038816SMartin Matuska 			if (adata != NULL && asize != psize) {
5243eda14cbcSMatt Macy 				adata = abd_alloc(asize, B_TRUE);
5244eda14cbcSMatt Macy 				abd_copy(adata, zio->io_abd, psize);
5245eda14cbcSMatt Macy 				abd_zero_off(adata, psize, asize - psize);
5246eda14cbcSMatt Macy 			}
5247eda14cbcSMatt Macy 
5248eda14cbcSMatt Macy 			zio->io_cksum_report = zcr->zcr_next;
5249eda14cbcSMatt Macy 			zcr->zcr_next = NULL;
5250eda14cbcSMatt Macy 			zcr->zcr_finish(zcr, adata);
5251eda14cbcSMatt Macy 			zfs_ereport_free_checksum(zcr);
5252eda14cbcSMatt Macy 
525316038816SMartin Matuska 			if (adata != NULL && asize != psize)
5254eda14cbcSMatt Macy 				abd_free(adata);
5255eda14cbcSMatt Macy 		}
5256eda14cbcSMatt Macy 	}
5257eda14cbcSMatt Macy 
5258eda14cbcSMatt Macy 	zio_pop_transforms(zio);	/* note: may set zio->io_error */
5259eda14cbcSMatt Macy 
5260eda14cbcSMatt Macy 	vdev_stat_update(zio, psize);
5261eda14cbcSMatt Macy 
5262eda14cbcSMatt Macy 	/*
5263eda14cbcSMatt Macy 	 * If this I/O is attached to a particular vdev is slow, exceeding
5264eda14cbcSMatt Macy 	 * 30 seconds to complete, post an error described the I/O delay.
5265eda14cbcSMatt Macy 	 * We ignore these errors if the device is currently unavailable.
5266eda14cbcSMatt Macy 	 */
5267eda14cbcSMatt Macy 	if (zio->io_delay >= MSEC2NSEC(zio_slow_io_ms)) {
5268eda14cbcSMatt Macy 		if (zio->io_vd != NULL && !vdev_is_dead(zio->io_vd)) {
5269eda14cbcSMatt Macy 			/*
5270eda14cbcSMatt Macy 			 * We want to only increment our slow IO counters if
5271eda14cbcSMatt Macy 			 * the IO is valid (i.e. not if the drive is removed).
5272eda14cbcSMatt Macy 			 *
5273eda14cbcSMatt Macy 			 * zfs_ereport_post() will also do these checks, but
5274eda14cbcSMatt Macy 			 * it can also ratelimit and have other failures, so we
5275eda14cbcSMatt Macy 			 * need to increment the slow_io counters independent
5276eda14cbcSMatt Macy 			 * of it.
5277eda14cbcSMatt Macy 			 */
5278eda14cbcSMatt Macy 			if (zfs_ereport_is_valid(FM_EREPORT_ZFS_DELAY,
5279eda14cbcSMatt Macy 			    zio->io_spa, zio->io_vd, zio)) {
5280eda14cbcSMatt Macy 				mutex_enter(&zio->io_vd->vdev_stat_lock);
5281eda14cbcSMatt Macy 				zio->io_vd->vdev_stat.vs_slow_ios++;
5282eda14cbcSMatt Macy 				mutex_exit(&zio->io_vd->vdev_stat_lock);
5283eda14cbcSMatt Macy 
5284eac7052fSMatt Macy 				(void) zfs_ereport_post(FM_EREPORT_ZFS_DELAY,
5285eda14cbcSMatt Macy 				    zio->io_spa, zio->io_vd, &zio->io_bookmark,
52862c48331dSMatt Macy 				    zio, 0);
5287eda14cbcSMatt Macy 			}
5288eda14cbcSMatt Macy 		}
5289eda14cbcSMatt Macy 	}
5290eda14cbcSMatt Macy 
5291eda14cbcSMatt Macy 	if (zio->io_error) {
5292eda14cbcSMatt Macy 		/*
5293eda14cbcSMatt Macy 		 * If this I/O is attached to a particular vdev,
5294eda14cbcSMatt Macy 		 * generate an error message describing the I/O failure
5295eda14cbcSMatt Macy 		 * at the block level.  We ignore these errors if the
5296eda14cbcSMatt Macy 		 * device is currently unavailable.
5297eda14cbcSMatt Macy 		 */
5298eda14cbcSMatt Macy 		if (zio->io_error != ECKSUM && zio->io_vd != NULL &&
52997a7741afSMartin Matuska 		    !vdev_is_dead(zio->io_vd) &&
53007a7741afSMartin Matuska 		    !(zio->io_flags & ZIO_FLAG_DIO_CHKSUM_ERR)) {
53012c48331dSMatt Macy 			int ret = zfs_ereport_post(FM_EREPORT_ZFS_IO,
53022c48331dSMatt Macy 			    zio->io_spa, zio->io_vd, &zio->io_bookmark, zio, 0);
53032c48331dSMatt Macy 			if (ret != EALREADY) {
5304eda14cbcSMatt Macy 				mutex_enter(&zio->io_vd->vdev_stat_lock);
53052c48331dSMatt Macy 				if (zio->io_type == ZIO_TYPE_READ)
5306eda14cbcSMatt Macy 					zio->io_vd->vdev_stat.vs_read_errors++;
53072c48331dSMatt Macy 				else if (zio->io_type == ZIO_TYPE_WRITE)
5308eda14cbcSMatt Macy 					zio->io_vd->vdev_stat.vs_write_errors++;
5309eda14cbcSMatt Macy 				mutex_exit(&zio->io_vd->vdev_stat_lock);
53102c48331dSMatt Macy 			}
5311eda14cbcSMatt Macy 		}
5312eda14cbcSMatt Macy 
5313eda14cbcSMatt Macy 		if ((zio->io_error == EIO || !(zio->io_flags &
5314eda14cbcSMatt Macy 		    (ZIO_FLAG_SPECULATIVE | ZIO_FLAG_DONT_PROPAGATE))) &&
53157a7741afSMartin Matuska 		    !(zio->io_flags & ZIO_FLAG_DIO_CHKSUM_ERR) &&
5316eda14cbcSMatt Macy 		    zio == zio->io_logical) {
5317eda14cbcSMatt Macy 			/*
5318eda14cbcSMatt Macy 			 * For logical I/O requests, tell the SPA to log the
5319eda14cbcSMatt Macy 			 * error and generate a logical data ereport.
5320eda14cbcSMatt Macy 			 */
53212a58b312SMartin Matuska 			spa_log_error(zio->io_spa, &zio->io_bookmark,
5322783d3ff6SMartin Matuska 			    BP_GET_LOGICAL_BIRTH(zio->io_bp));
5323eac7052fSMatt Macy 			(void) zfs_ereport_post(FM_EREPORT_ZFS_DATA,
53242c48331dSMatt Macy 			    zio->io_spa, NULL, &zio->io_bookmark, zio, 0);
5325eda14cbcSMatt Macy 		}
5326eda14cbcSMatt Macy 	}
5327eda14cbcSMatt Macy 
5328eda14cbcSMatt Macy 	if (zio->io_error && zio == zio->io_logical) {
5329eda14cbcSMatt Macy 		/*
5330eda14cbcSMatt Macy 		 * Determine whether zio should be reexecuted.  This will
5331eda14cbcSMatt Macy 		 * propagate all the way to the root via zio_notify_parent().
5332eda14cbcSMatt Macy 		 */
5333eda14cbcSMatt Macy 		ASSERT(zio->io_vd == NULL && zio->io_bp != NULL);
5334eda14cbcSMatt Macy 		ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
5335eda14cbcSMatt Macy 
5336eda14cbcSMatt Macy 		if (IO_IS_ALLOCATING(zio) &&
53377a7741afSMartin Matuska 		    !(zio->io_flags & ZIO_FLAG_CANFAIL) &&
53387a7741afSMartin Matuska 		    !(zio->io_flags & ZIO_FLAG_DIO_CHKSUM_ERR)) {
5339eda14cbcSMatt Macy 			if (zio->io_error != ENOSPC)
5340eda14cbcSMatt Macy 				zio->io_reexecute |= ZIO_REEXECUTE_NOW;
5341eda14cbcSMatt Macy 			else
5342eda14cbcSMatt Macy 				zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
5343eda14cbcSMatt Macy 		}
5344eda14cbcSMatt Macy 
5345eda14cbcSMatt Macy 		if ((zio->io_type == ZIO_TYPE_READ ||
5346eda14cbcSMatt Macy 		    zio->io_type == ZIO_TYPE_FREE) &&
5347eda14cbcSMatt Macy 		    !(zio->io_flags & ZIO_FLAG_SCAN_THREAD) &&
5348eda14cbcSMatt Macy 		    zio->io_error == ENXIO &&
5349eda14cbcSMatt Macy 		    spa_load_state(zio->io_spa) == SPA_LOAD_NONE &&
5350eda14cbcSMatt Macy 		    spa_get_failmode(zio->io_spa) != ZIO_FAILURE_MODE_CONTINUE)
5351eda14cbcSMatt Macy 			zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
5352eda14cbcSMatt Macy 
5353eda14cbcSMatt Macy 		if (!(zio->io_flags & ZIO_FLAG_CANFAIL) && !zio->io_reexecute)
5354eda14cbcSMatt Macy 			zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
5355eda14cbcSMatt Macy 
5356eda14cbcSMatt Macy 		/*
5357eda14cbcSMatt Macy 		 * Here is a possibly good place to attempt to do
5358eda14cbcSMatt Macy 		 * either combinatorial reconstruction or error correction
5359eda14cbcSMatt Macy 		 * based on checksums.  It also might be a good place
5360eda14cbcSMatt Macy 		 * to send out preliminary ereports before we suspend
5361eda14cbcSMatt Macy 		 * processing.
5362eda14cbcSMatt Macy 		 */
5363eda14cbcSMatt Macy 	}
5364eda14cbcSMatt Macy 
5365eda14cbcSMatt Macy 	/*
5366eda14cbcSMatt Macy 	 * If there were logical child errors, they apply to us now.
5367eda14cbcSMatt Macy 	 * We defer this until now to avoid conflating logical child
5368eda14cbcSMatt Macy 	 * errors with errors that happened to the zio itself when
5369eda14cbcSMatt Macy 	 * updating vdev stats and reporting FMA events above.
5370eda14cbcSMatt Macy 	 */
5371eda14cbcSMatt Macy 	zio_inherit_child_errors(zio, ZIO_CHILD_LOGICAL);
5372eda14cbcSMatt Macy 
5373eda14cbcSMatt Macy 	if ((zio->io_error || zio->io_reexecute) &&
5374eda14cbcSMatt Macy 	    IO_IS_ALLOCATING(zio) && zio->io_gang_leader == zio &&
5375eda14cbcSMatt Macy 	    !(zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE)))
5376eda14cbcSMatt Macy 		zio_dva_unallocate(zio, zio->io_gang_tree, zio->io_bp);
5377eda14cbcSMatt Macy 
5378eda14cbcSMatt Macy 	zio_gang_tree_free(&zio->io_gang_tree);
5379eda14cbcSMatt Macy 
5380eda14cbcSMatt Macy 	/*
5381eda14cbcSMatt Macy 	 * Godfather I/Os should never suspend.
5382eda14cbcSMatt Macy 	 */
5383eda14cbcSMatt Macy 	if ((zio->io_flags & ZIO_FLAG_GODFATHER) &&
5384eda14cbcSMatt Macy 	    (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND))
5385eda14cbcSMatt Macy 		zio->io_reexecute &= ~ZIO_REEXECUTE_SUSPEND;
5386eda14cbcSMatt Macy 
5387eda14cbcSMatt Macy 	if (zio->io_reexecute) {
5388eda14cbcSMatt Macy 		/*
538987bf66d4SMartin Matuska 		 * A Direct I/O operation that has a checksum verify error
539087bf66d4SMartin Matuska 		 * should not attempt to reexecute. Instead, the error should
539187bf66d4SMartin Matuska 		 * just be propagated back.
53927a7741afSMartin Matuska 		 */
53937a7741afSMartin Matuska 		ASSERT(!(zio->io_flags & ZIO_FLAG_DIO_CHKSUM_ERR));
53947a7741afSMartin Matuska 
53957a7741afSMartin Matuska 		/*
5396eda14cbcSMatt Macy 		 * This is a logical I/O that wants to reexecute.
5397eda14cbcSMatt Macy 		 *
5398eda14cbcSMatt Macy 		 * Reexecute is top-down.  When an i/o fails, if it's not
5399eda14cbcSMatt Macy 		 * the root, it simply notifies its parent and sticks around.
5400eda14cbcSMatt Macy 		 * The parent, seeing that it still has children in zio_done(),
5401eda14cbcSMatt Macy 		 * does the same.  This percolates all the way up to the root.
5402eda14cbcSMatt Macy 		 * The root i/o will reexecute or suspend the entire tree.
5403eda14cbcSMatt Macy 		 *
5404eda14cbcSMatt Macy 		 * This approach ensures that zio_reexecute() honors
5405eda14cbcSMatt Macy 		 * all the original i/o dependency relationships, e.g.
5406eda14cbcSMatt Macy 		 * parents not executing until children are ready.
5407eda14cbcSMatt Macy 		 */
5408eda14cbcSMatt Macy 		ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
5409eda14cbcSMatt Macy 
5410eda14cbcSMatt Macy 		zio->io_gang_leader = NULL;
5411eda14cbcSMatt Macy 
5412eda14cbcSMatt Macy 		mutex_enter(&zio->io_lock);
5413eda14cbcSMatt Macy 		zio->io_state[ZIO_WAIT_DONE] = 1;
5414eda14cbcSMatt Macy 		mutex_exit(&zio->io_lock);
5415eda14cbcSMatt Macy 
5416eda14cbcSMatt Macy 		/*
5417eda14cbcSMatt Macy 		 * "The Godfather" I/O monitors its children but is
5418eda14cbcSMatt Macy 		 * not a true parent to them. It will track them through
5419eda14cbcSMatt Macy 		 * the pipeline but severs its ties whenever they get into
5420eda14cbcSMatt Macy 		 * trouble (e.g. suspended). This allows "The Godfather"
5421eda14cbcSMatt Macy 		 * I/O to return status without blocking.
5422eda14cbcSMatt Macy 		 */
5423eda14cbcSMatt Macy 		zl = NULL;
5424eda14cbcSMatt Macy 		for (pio = zio_walk_parents(zio, &zl); pio != NULL;
5425eda14cbcSMatt Macy 		    pio = pio_next) {
5426eda14cbcSMatt Macy 			zio_link_t *remove_zl = zl;
5427eda14cbcSMatt Macy 			pio_next = zio_walk_parents(zio, &zl);
5428eda14cbcSMatt Macy 
5429eda14cbcSMatt Macy 			if ((pio->io_flags & ZIO_FLAG_GODFATHER) &&
5430eda14cbcSMatt Macy 			    (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND)) {
5431eda14cbcSMatt Macy 				zio_remove_child(pio, zio, remove_zl);
5432eda14cbcSMatt Macy 				/*
5433eda14cbcSMatt Macy 				 * This is a rare code path, so we don't
5434eda14cbcSMatt Macy 				 * bother with "next_to_execute".
5435eda14cbcSMatt Macy 				 */
5436eda14cbcSMatt Macy 				zio_notify_parent(pio, zio, ZIO_WAIT_DONE,
5437eda14cbcSMatt Macy 				    NULL);
5438eda14cbcSMatt Macy 			}
5439eda14cbcSMatt Macy 		}
5440eda14cbcSMatt Macy 
5441eda14cbcSMatt Macy 		if ((pio = zio_unique_parent(zio)) != NULL) {
5442eda14cbcSMatt Macy 			/*
5443eda14cbcSMatt Macy 			 * We're not a root i/o, so there's nothing to do
5444eda14cbcSMatt Macy 			 * but notify our parent.  Don't propagate errors
5445eda14cbcSMatt Macy 			 * upward since we haven't permanently failed yet.
5446eda14cbcSMatt Macy 			 */
5447eda14cbcSMatt Macy 			ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER));
5448eda14cbcSMatt Macy 			zio->io_flags |= ZIO_FLAG_DONT_PROPAGATE;
5449eda14cbcSMatt Macy 			/*
5450eda14cbcSMatt Macy 			 * This is a rare code path, so we don't bother with
5451eda14cbcSMatt Macy 			 * "next_to_execute".
5452eda14cbcSMatt Macy 			 */
5453eda14cbcSMatt Macy 			zio_notify_parent(pio, zio, ZIO_WAIT_DONE, NULL);
5454eda14cbcSMatt Macy 		} else if (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND) {
5455eda14cbcSMatt Macy 			/*
5456eda14cbcSMatt Macy 			 * We'd fail again if we reexecuted now, so suspend
5457eda14cbcSMatt Macy 			 * until conditions improve (e.g. device comes online).
5458eda14cbcSMatt Macy 			 */
5459eda14cbcSMatt Macy 			zio_suspend(zio->io_spa, zio, ZIO_SUSPEND_IOERR);
5460eda14cbcSMatt Macy 		} else {
5461eda14cbcSMatt Macy 			/*
5462eda14cbcSMatt Macy 			 * Reexecution is potentially a huge amount of work.
5463eda14cbcSMatt Macy 			 * Hand it off to the otherwise-unused claim taskq.
5464eda14cbcSMatt Macy 			 */
5465aca928a5SMartin Matuska 			spa_taskq_dispatch(zio->io_spa,
5466eda14cbcSMatt Macy 			    ZIO_TYPE_CLAIM, ZIO_TASKQ_ISSUE,
5467aca928a5SMartin Matuska 			    zio_reexecute, zio, B_FALSE);
5468eda14cbcSMatt Macy 		}
5469eda14cbcSMatt Macy 		return (NULL);
5470eda14cbcSMatt Macy 	}
5471eda14cbcSMatt Macy 
5472b7198dcfSMartin Matuska 	ASSERT(list_is_empty(&zio->io_child_list));
5473eda14cbcSMatt Macy 	ASSERT(zio->io_reexecute == 0);
5474eda14cbcSMatt Macy 	ASSERT(zio->io_error == 0 || (zio->io_flags & ZIO_FLAG_CANFAIL));
5475eda14cbcSMatt Macy 
5476eda14cbcSMatt Macy 	/*
5477eda14cbcSMatt Macy 	 * Report any checksum errors, since the I/O is complete.
5478eda14cbcSMatt Macy 	 */
5479eda14cbcSMatt Macy 	while (zio->io_cksum_report != NULL) {
5480eda14cbcSMatt Macy 		zio_cksum_report_t *zcr = zio->io_cksum_report;
5481eda14cbcSMatt Macy 		zio->io_cksum_report = zcr->zcr_next;
5482eda14cbcSMatt Macy 		zcr->zcr_next = NULL;
5483eda14cbcSMatt Macy 		zcr->zcr_finish(zcr, NULL);
5484eda14cbcSMatt Macy 		zfs_ereport_free_checksum(zcr);
5485eda14cbcSMatt Macy 	}
5486eda14cbcSMatt Macy 
5487eda14cbcSMatt Macy 	/*
5488eda14cbcSMatt Macy 	 * It is the responsibility of the done callback to ensure that this
5489eda14cbcSMatt Macy 	 * particular zio is no longer discoverable for adoption, and as
5490eda14cbcSMatt Macy 	 * such, cannot acquire any new parents.
5491eda14cbcSMatt Macy 	 */
5492eda14cbcSMatt Macy 	if (zio->io_done)
5493eda14cbcSMatt Macy 		zio->io_done(zio);
5494eda14cbcSMatt Macy 
5495eda14cbcSMatt Macy 	mutex_enter(&zio->io_lock);
5496eda14cbcSMatt Macy 	zio->io_state[ZIO_WAIT_DONE] = 1;
5497eda14cbcSMatt Macy 	mutex_exit(&zio->io_lock);
5498eda14cbcSMatt Macy 
5499eda14cbcSMatt Macy 	/*
5500eda14cbcSMatt Macy 	 * We are done executing this zio.  We may want to execute a parent
5501eda14cbcSMatt Macy 	 * next.  See the comment in zio_notify_parent().
5502eda14cbcSMatt Macy 	 */
5503eda14cbcSMatt Macy 	zio_t *next_to_execute = NULL;
5504eda14cbcSMatt Macy 	zl = NULL;
5505eda14cbcSMatt Macy 	for (pio = zio_walk_parents(zio, &zl); pio != NULL; pio = pio_next) {
5506eda14cbcSMatt Macy 		zio_link_t *remove_zl = zl;
5507eda14cbcSMatt Macy 		pio_next = zio_walk_parents(zio, &zl);
5508eda14cbcSMatt Macy 		zio_remove_child(pio, zio, remove_zl);
5509eda14cbcSMatt Macy 		zio_notify_parent(pio, zio, ZIO_WAIT_DONE, &next_to_execute);
5510eda14cbcSMatt Macy 	}
5511eda14cbcSMatt Macy 
5512eda14cbcSMatt Macy 	if (zio->io_waiter != NULL) {
5513eda14cbcSMatt Macy 		mutex_enter(&zio->io_lock);
5514eda14cbcSMatt Macy 		zio->io_executor = NULL;
5515eda14cbcSMatt Macy 		cv_broadcast(&zio->io_cv);
5516eda14cbcSMatt Macy 		mutex_exit(&zio->io_lock);
5517eda14cbcSMatt Macy 	} else {
5518eda14cbcSMatt Macy 		zio_destroy(zio);
5519eda14cbcSMatt Macy 	}
5520eda14cbcSMatt Macy 
5521eda14cbcSMatt Macy 	return (next_to_execute);
5522eda14cbcSMatt Macy }
5523eda14cbcSMatt Macy 
5524eda14cbcSMatt Macy /*
5525eda14cbcSMatt Macy  * ==========================================================================
5526eda14cbcSMatt Macy  * I/O pipeline definition
5527eda14cbcSMatt Macy  * ==========================================================================
5528eda14cbcSMatt Macy  */
5529eda14cbcSMatt Macy static zio_pipe_stage_t *zio_pipeline[] = {
5530eda14cbcSMatt Macy 	NULL,
5531eda14cbcSMatt Macy 	zio_read_bp_init,
5532eda14cbcSMatt Macy 	zio_write_bp_init,
5533eda14cbcSMatt Macy 	zio_free_bp_init,
5534eda14cbcSMatt Macy 	zio_issue_async,
5535eda14cbcSMatt Macy 	zio_write_compress,
5536eda14cbcSMatt Macy 	zio_encrypt,
5537eda14cbcSMatt Macy 	zio_checksum_generate,
5538eda14cbcSMatt Macy 	zio_nop_write,
55392a58b312SMartin Matuska 	zio_brt_free,
5540eda14cbcSMatt Macy 	zio_ddt_read_start,
5541eda14cbcSMatt Macy 	zio_ddt_read_done,
5542eda14cbcSMatt Macy 	zio_ddt_write,
5543eda14cbcSMatt Macy 	zio_ddt_free,
5544eda14cbcSMatt Macy 	zio_gang_assemble,
5545eda14cbcSMatt Macy 	zio_gang_issue,
5546eda14cbcSMatt Macy 	zio_dva_throttle,
5547eda14cbcSMatt Macy 	zio_dva_allocate,
5548eda14cbcSMatt Macy 	zio_dva_free,
5549eda14cbcSMatt Macy 	zio_dva_claim,
5550eda14cbcSMatt Macy 	zio_ready,
5551eda14cbcSMatt Macy 	zio_vdev_io_start,
5552eda14cbcSMatt Macy 	zio_vdev_io_done,
5553eda14cbcSMatt Macy 	zio_vdev_io_assess,
5554eda14cbcSMatt Macy 	zio_checksum_verify,
55557a7741afSMartin Matuska 	zio_dio_checksum_verify,
5556eda14cbcSMatt Macy 	zio_done
5557eda14cbcSMatt Macy };
5558eda14cbcSMatt Macy 
5559eda14cbcSMatt Macy 
5560eda14cbcSMatt Macy 
5561eda14cbcSMatt Macy 
5562eda14cbcSMatt Macy /*
5563eda14cbcSMatt Macy  * Compare two zbookmark_phys_t's to see which we would reach first in a
5564eda14cbcSMatt Macy  * pre-order traversal of the object tree.
5565eda14cbcSMatt Macy  *
5566eda14cbcSMatt Macy  * This is simple in every case aside from the meta-dnode object. For all other
5567eda14cbcSMatt Macy  * objects, we traverse them in order (object 1 before object 2, and so on).
5568eda14cbcSMatt Macy  * However, all of these objects are traversed while traversing object 0, since
5569eda14cbcSMatt Macy  * the data it points to is the list of objects.  Thus, we need to convert to a
5570eda14cbcSMatt Macy  * canonical representation so we can compare meta-dnode bookmarks to
5571eda14cbcSMatt Macy  * non-meta-dnode bookmarks.
5572eda14cbcSMatt Macy  *
5573eda14cbcSMatt Macy  * We do this by calculating "equivalents" for each field of the zbookmark.
5574eda14cbcSMatt Macy  * zbookmarks outside of the meta-dnode use their own object and level, and
5575eda14cbcSMatt Macy  * calculate the level 0 equivalent (the first L0 blkid that is contained in the
5576eda14cbcSMatt Macy  * blocks this bookmark refers to) by multiplying their blkid by their span
5577eda14cbcSMatt Macy  * (the number of L0 blocks contained within one block at their level).
5578eda14cbcSMatt Macy  * zbookmarks inside the meta-dnode calculate their object equivalent
5579eda14cbcSMatt Macy  * (which is L0equiv * dnodes per data block), use 0 for their L0equiv, and use
5580eda14cbcSMatt Macy  * level + 1<<31 (any value larger than a level could ever be) for their level.
5581eda14cbcSMatt Macy  * This causes them to always compare before a bookmark in their object
5582eda14cbcSMatt Macy  * equivalent, compare appropriately to bookmarks in other objects, and to
5583eda14cbcSMatt Macy  * compare appropriately to other bookmarks in the meta-dnode.
5584eda14cbcSMatt Macy  */
5585eda14cbcSMatt Macy int
5586eda14cbcSMatt Macy zbookmark_compare(uint16_t dbss1, uint8_t ibs1, uint16_t dbss2, uint8_t ibs2,
5587eda14cbcSMatt Macy     const zbookmark_phys_t *zb1, const zbookmark_phys_t *zb2)
5588eda14cbcSMatt Macy {
5589eda14cbcSMatt Macy 	/*
5590eda14cbcSMatt Macy 	 * These variables represent the "equivalent" values for the zbookmark,
5591eda14cbcSMatt Macy 	 * after converting zbookmarks inside the meta dnode to their
5592eda14cbcSMatt Macy 	 * normal-object equivalents.
5593eda14cbcSMatt Macy 	 */
5594eda14cbcSMatt Macy 	uint64_t zb1obj, zb2obj;
5595eda14cbcSMatt Macy 	uint64_t zb1L0, zb2L0;
5596eda14cbcSMatt Macy 	uint64_t zb1level, zb2level;
5597eda14cbcSMatt Macy 
5598eda14cbcSMatt Macy 	if (zb1->zb_object == zb2->zb_object &&
5599eda14cbcSMatt Macy 	    zb1->zb_level == zb2->zb_level &&
5600eda14cbcSMatt Macy 	    zb1->zb_blkid == zb2->zb_blkid)
5601eda14cbcSMatt Macy 		return (0);
5602eda14cbcSMatt Macy 
5603eda14cbcSMatt Macy 	IMPLY(zb1->zb_level > 0, ibs1 >= SPA_MINBLOCKSHIFT);
5604eda14cbcSMatt Macy 	IMPLY(zb2->zb_level > 0, ibs2 >= SPA_MINBLOCKSHIFT);
5605eda14cbcSMatt Macy 
5606eda14cbcSMatt Macy 	/*
5607eda14cbcSMatt Macy 	 * BP_SPANB calculates the span in blocks.
5608eda14cbcSMatt Macy 	 */
5609eda14cbcSMatt Macy 	zb1L0 = (zb1->zb_blkid) * BP_SPANB(ibs1, zb1->zb_level);
5610eda14cbcSMatt Macy 	zb2L0 = (zb2->zb_blkid) * BP_SPANB(ibs2, zb2->zb_level);
5611eda14cbcSMatt Macy 
5612eda14cbcSMatt Macy 	if (zb1->zb_object == DMU_META_DNODE_OBJECT) {
5613eda14cbcSMatt Macy 		zb1obj = zb1L0 * (dbss1 << (SPA_MINBLOCKSHIFT - DNODE_SHIFT));
5614eda14cbcSMatt Macy 		zb1L0 = 0;
5615eda14cbcSMatt Macy 		zb1level = zb1->zb_level + COMPARE_META_LEVEL;
5616eda14cbcSMatt Macy 	} else {
5617eda14cbcSMatt Macy 		zb1obj = zb1->zb_object;
5618eda14cbcSMatt Macy 		zb1level = zb1->zb_level;
5619eda14cbcSMatt Macy 	}
5620eda14cbcSMatt Macy 
5621eda14cbcSMatt Macy 	if (zb2->zb_object == DMU_META_DNODE_OBJECT) {
5622eda14cbcSMatt Macy 		zb2obj = zb2L0 * (dbss2 << (SPA_MINBLOCKSHIFT - DNODE_SHIFT));
5623eda14cbcSMatt Macy 		zb2L0 = 0;
5624eda14cbcSMatt Macy 		zb2level = zb2->zb_level + COMPARE_META_LEVEL;
5625eda14cbcSMatt Macy 	} else {
5626eda14cbcSMatt Macy 		zb2obj = zb2->zb_object;
5627eda14cbcSMatt Macy 		zb2level = zb2->zb_level;
5628eda14cbcSMatt Macy 	}
5629eda14cbcSMatt Macy 
5630eda14cbcSMatt Macy 	/* Now that we have a canonical representation, do the comparison. */
5631eda14cbcSMatt Macy 	if (zb1obj != zb2obj)
5632eda14cbcSMatt Macy 		return (zb1obj < zb2obj ? -1 : 1);
5633eda14cbcSMatt Macy 	else if (zb1L0 != zb2L0)
5634eda14cbcSMatt Macy 		return (zb1L0 < zb2L0 ? -1 : 1);
5635eda14cbcSMatt Macy 	else if (zb1level != zb2level)
5636eda14cbcSMatt Macy 		return (zb1level > zb2level ? -1 : 1);
5637eda14cbcSMatt Macy 	/*
5638eda14cbcSMatt Macy 	 * This can (theoretically) happen if the bookmarks have the same object
5639eda14cbcSMatt Macy 	 * and level, but different blkids, if the block sizes are not the same.
5640eda14cbcSMatt Macy 	 * There is presently no way to change the indirect block sizes
5641eda14cbcSMatt Macy 	 */
5642eda14cbcSMatt Macy 	return (0);
5643eda14cbcSMatt Macy }
5644eda14cbcSMatt Macy 
5645eda14cbcSMatt Macy /*
5646eda14cbcSMatt Macy  *  This function checks the following: given that last_block is the place that
5647eda14cbcSMatt Macy  *  our traversal stopped last time, does that guarantee that we've visited
5648eda14cbcSMatt Macy  *  every node under subtree_root?  Therefore, we can't just use the raw output
5649eda14cbcSMatt Macy  *  of zbookmark_compare.  We have to pass in a modified version of
5650eda14cbcSMatt Macy  *  subtree_root; by incrementing the block id, and then checking whether
5651eda14cbcSMatt Macy  *  last_block is before or equal to that, we can tell whether or not having
5652eda14cbcSMatt Macy  *  visited last_block implies that all of subtree_root's children have been
5653eda14cbcSMatt Macy  *  visited.
5654eda14cbcSMatt Macy  */
5655eda14cbcSMatt Macy boolean_t
5656eda14cbcSMatt Macy zbookmark_subtree_completed(const dnode_phys_t *dnp,
5657eda14cbcSMatt Macy     const zbookmark_phys_t *subtree_root, const zbookmark_phys_t *last_block)
5658eda14cbcSMatt Macy {
5659eda14cbcSMatt Macy 	zbookmark_phys_t mod_zb = *subtree_root;
5660eda14cbcSMatt Macy 	mod_zb.zb_blkid++;
5661271171e0SMartin Matuska 	ASSERT0(last_block->zb_level);
5662eda14cbcSMatt Macy 
5663eda14cbcSMatt Macy 	/* The objset_phys_t isn't before anything. */
5664eda14cbcSMatt Macy 	if (dnp == NULL)
5665eda14cbcSMatt Macy 		return (B_FALSE);
5666eda14cbcSMatt Macy 
5667eda14cbcSMatt Macy 	/*
5668eda14cbcSMatt Macy 	 * We pass in 1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT) for the
5669eda14cbcSMatt Macy 	 * data block size in sectors, because that variable is only used if
5670eda14cbcSMatt Macy 	 * the bookmark refers to a block in the meta-dnode.  Since we don't
5671eda14cbcSMatt Macy 	 * know without examining it what object it refers to, and there's no
5672eda14cbcSMatt Macy 	 * harm in passing in this value in other cases, we always pass it in.
5673eda14cbcSMatt Macy 	 *
5674eda14cbcSMatt Macy 	 * We pass in 0 for the indirect block size shift because zb2 must be
5675eda14cbcSMatt Macy 	 * level 0.  The indirect block size is only used to calculate the span
5676eda14cbcSMatt Macy 	 * of the bookmark, but since the bookmark must be level 0, the span is
5677eda14cbcSMatt Macy 	 * always 1, so the math works out.
5678eda14cbcSMatt Macy 	 *
5679eda14cbcSMatt Macy 	 * If you make changes to how the zbookmark_compare code works, be sure
5680eda14cbcSMatt Macy 	 * to make sure that this code still works afterwards.
5681eda14cbcSMatt Macy 	 */
5682eda14cbcSMatt Macy 	return (zbookmark_compare(dnp->dn_datablkszsec, dnp->dn_indblkshift,
5683eda14cbcSMatt Macy 	    1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT), 0, &mod_zb,
5684eda14cbcSMatt Macy 	    last_block) <= 0);
5685eda14cbcSMatt Macy }
5686eda14cbcSMatt Macy 
5687271171e0SMartin Matuska /*
5688271171e0SMartin Matuska  * This function is similar to zbookmark_subtree_completed(), but returns true
5689271171e0SMartin Matuska  * if subtree_root is equal or ahead of last_block, i.e. still to be done.
5690271171e0SMartin Matuska  */
5691271171e0SMartin Matuska boolean_t
5692271171e0SMartin Matuska zbookmark_subtree_tbd(const dnode_phys_t *dnp,
5693271171e0SMartin Matuska     const zbookmark_phys_t *subtree_root, const zbookmark_phys_t *last_block)
5694271171e0SMartin Matuska {
5695271171e0SMartin Matuska 	ASSERT0(last_block->zb_level);
5696271171e0SMartin Matuska 	if (dnp == NULL)
5697271171e0SMartin Matuska 		return (B_FALSE);
5698271171e0SMartin Matuska 	return (zbookmark_compare(dnp->dn_datablkszsec, dnp->dn_indblkshift,
5699271171e0SMartin Matuska 	    1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT), 0, subtree_root,
5700271171e0SMartin Matuska 	    last_block) >= 0);
5701271171e0SMartin Matuska }
5702271171e0SMartin Matuska 
5703eda14cbcSMatt Macy EXPORT_SYMBOL(zio_type_name);
5704eda14cbcSMatt Macy EXPORT_SYMBOL(zio_buf_alloc);
5705eda14cbcSMatt Macy EXPORT_SYMBOL(zio_data_buf_alloc);
5706eda14cbcSMatt Macy EXPORT_SYMBOL(zio_buf_free);
5707eda14cbcSMatt Macy EXPORT_SYMBOL(zio_data_buf_free);
5708eda14cbcSMatt Macy 
5709eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs_zio, zio_, slow_io_ms, INT, ZMOD_RW,
5710eda14cbcSMatt Macy 	"Max I/O completion time (milliseconds) before marking it as slow");
5711eda14cbcSMatt Macy 
5712eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs_zio, zio_, requeue_io_start_cut_in_line, INT, ZMOD_RW,
5713eda14cbcSMatt Macy 	"Prioritize requeued I/O");
5714eda14cbcSMatt Macy 
5715be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs, zfs_, sync_pass_deferred_free,  UINT, ZMOD_RW,
5716eda14cbcSMatt Macy 	"Defer frees starting in this pass");
5717eda14cbcSMatt Macy 
5718be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs, zfs_, sync_pass_dont_compress, UINT, ZMOD_RW,
5719eda14cbcSMatt Macy 	"Don't compress starting in this pass");
5720eda14cbcSMatt Macy 
5721be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs, zfs_, sync_pass_rewrite, UINT, ZMOD_RW,
5722eda14cbcSMatt Macy 	"Rewrite new bps starting in this pass");
5723eda14cbcSMatt Macy 
5724eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs_zio, zio_, dva_throttle_enabled, INT, ZMOD_RW,
5725eda14cbcSMatt Macy 	"Throttle block allocations in the ZIO pipeline");
5726eda14cbcSMatt Macy 
5727eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs_zio, zio_, deadman_log_all, INT, ZMOD_RW,
5728eda14cbcSMatt Macy 	"Log all slow ZIOs, not just those with vdevs");
5729