xref: /freebsd-src/sys/contrib/openzfs/module/zfs/zio.c (revision 53b70c86d93c1e4d3c76f1282e94154e88780d7e)
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
9eda14cbcSMatt Macy  * or http://www.opensolaris.org/os/licensing.
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.
232c48331dSMatt Macy  * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
24eda14cbcSMatt Macy  * Copyright (c) 2011 Nexenta Systems, Inc. All rights reserved.
25eda14cbcSMatt Macy  * Copyright (c) 2017, Intel Corporation.
26eda14cbcSMatt Macy  * Copyright (c) 2019, Klara Inc.
27eda14cbcSMatt Macy  * Copyright (c) 2019, Allan Jude
2816038816SMartin Matuska  * Copyright (c) 2021, Datto, Inc.
29eda14cbcSMatt Macy  */
30eda14cbcSMatt Macy 
31eda14cbcSMatt Macy #include <sys/sysmacros.h>
32eda14cbcSMatt Macy #include <sys/zfs_context.h>
33eda14cbcSMatt Macy #include <sys/fm/fs/zfs.h>
34eda14cbcSMatt Macy #include <sys/spa.h>
35eda14cbcSMatt Macy #include <sys/txg.h>
36eda14cbcSMatt Macy #include <sys/spa_impl.h>
37eda14cbcSMatt Macy #include <sys/vdev_impl.h>
38eda14cbcSMatt Macy #include <sys/vdev_trim.h>
39eda14cbcSMatt Macy #include <sys/zio_impl.h>
40eda14cbcSMatt Macy #include <sys/zio_compress.h>
41eda14cbcSMatt Macy #include <sys/zio_checksum.h>
42eda14cbcSMatt Macy #include <sys/dmu_objset.h>
43eda14cbcSMatt Macy #include <sys/arc.h>
44eda14cbcSMatt Macy #include <sys/ddt.h>
45eda14cbcSMatt Macy #include <sys/blkptr.h>
46eda14cbcSMatt Macy #include <sys/zfeature.h>
47eda14cbcSMatt Macy #include <sys/dsl_scan.h>
48eda14cbcSMatt Macy #include <sys/metaslab_impl.h>
49eda14cbcSMatt Macy #include <sys/time.h>
50eda14cbcSMatt Macy #include <sys/trace_zfs.h>
51eda14cbcSMatt Macy #include <sys/abd.h>
52eda14cbcSMatt Macy #include <sys/dsl_crypt.h>
53eda14cbcSMatt Macy #include <cityhash.h>
54eda14cbcSMatt Macy 
55eda14cbcSMatt Macy /*
56eda14cbcSMatt Macy  * ==========================================================================
57eda14cbcSMatt Macy  * I/O type descriptions
58eda14cbcSMatt Macy  * ==========================================================================
59eda14cbcSMatt Macy  */
60eda14cbcSMatt Macy const char *zio_type_name[ZIO_TYPES] = {
61eda14cbcSMatt Macy 	/*
62eda14cbcSMatt Macy 	 * Note: Linux kernel thread name length is limited
63eda14cbcSMatt Macy 	 * so these names will differ from upstream open zfs.
64eda14cbcSMatt Macy 	 */
65eda14cbcSMatt Macy 	"z_null", "z_rd", "z_wr", "z_fr", "z_cl", "z_ioctl", "z_trim"
66eda14cbcSMatt Macy };
67eda14cbcSMatt Macy 
68eda14cbcSMatt Macy int zio_dva_throttle_enabled = B_TRUE;
69eda14cbcSMatt Macy int zio_deadman_log_all = B_FALSE;
70eda14cbcSMatt Macy 
71eda14cbcSMatt Macy /*
72eda14cbcSMatt Macy  * ==========================================================================
73eda14cbcSMatt Macy  * I/O kmem caches
74eda14cbcSMatt Macy  * ==========================================================================
75eda14cbcSMatt Macy  */
76eda14cbcSMatt Macy kmem_cache_t *zio_cache;
77eda14cbcSMatt Macy kmem_cache_t *zio_link_cache;
78eda14cbcSMatt Macy kmem_cache_t *zio_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
79eda14cbcSMatt Macy kmem_cache_t *zio_data_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
80eda14cbcSMatt Macy #if defined(ZFS_DEBUG) && !defined(_KERNEL)
81eda14cbcSMatt Macy uint64_t zio_buf_cache_allocs[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
82eda14cbcSMatt Macy uint64_t zio_buf_cache_frees[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
83eda14cbcSMatt Macy #endif
84eda14cbcSMatt Macy 
85eda14cbcSMatt Macy /* Mark IOs as "slow" if they take longer than 30 seconds */
86eda14cbcSMatt Macy int zio_slow_io_ms = (30 * MILLISEC);
87eda14cbcSMatt Macy 
88eda14cbcSMatt Macy #define	BP_SPANB(indblkshift, level) \
89eda14cbcSMatt Macy 	(((uint64_t)1) << ((level) * ((indblkshift) - SPA_BLKPTRSHIFT)))
90eda14cbcSMatt Macy #define	COMPARE_META_LEVEL	0x80000000ul
91eda14cbcSMatt Macy /*
92eda14cbcSMatt Macy  * The following actions directly effect the spa's sync-to-convergence logic.
93eda14cbcSMatt Macy  * The values below define the sync pass when we start performing the action.
94eda14cbcSMatt Macy  * Care should be taken when changing these values as they directly impact
95eda14cbcSMatt Macy  * spa_sync() performance. Tuning these values may introduce subtle performance
96eda14cbcSMatt Macy  * pathologies and should only be done in the context of performance analysis.
97eda14cbcSMatt Macy  * These tunables will eventually be removed and replaced with #defines once
98eda14cbcSMatt Macy  * enough analysis has been done to determine optimal values.
99eda14cbcSMatt Macy  *
100eda14cbcSMatt Macy  * The 'zfs_sync_pass_deferred_free' pass must be greater than 1 to ensure that
101eda14cbcSMatt Macy  * regular blocks are not deferred.
102eda14cbcSMatt Macy  *
103eda14cbcSMatt Macy  * Starting in sync pass 8 (zfs_sync_pass_dont_compress), we disable
104eda14cbcSMatt Macy  * compression (including of metadata).  In practice, we don't have this
105eda14cbcSMatt Macy  * many sync passes, so this has no effect.
106eda14cbcSMatt Macy  *
107eda14cbcSMatt Macy  * The original intent was that disabling compression would help the sync
108eda14cbcSMatt Macy  * passes to converge. However, in practice disabling compression increases
109eda14cbcSMatt Macy  * the average number of sync passes, because when we turn compression off, a
110eda14cbcSMatt Macy  * lot of block's size will change and thus we have to re-allocate (not
111eda14cbcSMatt Macy  * overwrite) them. It also increases the number of 128KB allocations (e.g.
112eda14cbcSMatt Macy  * for indirect blocks and spacemaps) because these will not be compressed.
113eda14cbcSMatt Macy  * The 128K allocations are especially detrimental to performance on highly
114eda14cbcSMatt Macy  * fragmented systems, which may have very few free segments of this size,
115eda14cbcSMatt Macy  * and may need to load new metaslabs to satisfy 128K allocations.
116eda14cbcSMatt Macy  */
117eda14cbcSMatt Macy int zfs_sync_pass_deferred_free = 2; /* defer frees starting in this pass */
118eda14cbcSMatt Macy int zfs_sync_pass_dont_compress = 8; /* don't compress starting in this pass */
119eda14cbcSMatt Macy int zfs_sync_pass_rewrite = 2; /* rewrite new bps starting in this pass */
120eda14cbcSMatt Macy 
121eda14cbcSMatt Macy /*
122eda14cbcSMatt Macy  * An allocating zio is one that either currently has the DVA allocate
123eda14cbcSMatt Macy  * stage set or will have it later in its lifetime.
124eda14cbcSMatt Macy  */
125eda14cbcSMatt Macy #define	IO_IS_ALLOCATING(zio) ((zio)->io_orig_pipeline & ZIO_STAGE_DVA_ALLOCATE)
126eda14cbcSMatt Macy 
127eda14cbcSMatt Macy /*
128eda14cbcSMatt Macy  * Enable smaller cores by excluding metadata
129eda14cbcSMatt Macy  * allocations as well.
130eda14cbcSMatt Macy  */
131eda14cbcSMatt Macy int zio_exclude_metadata = 0;
132eda14cbcSMatt Macy int zio_requeue_io_start_cut_in_line = 1;
133eda14cbcSMatt Macy 
134eda14cbcSMatt Macy #ifdef ZFS_DEBUG
135eda14cbcSMatt Macy int zio_buf_debug_limit = 16384;
136eda14cbcSMatt Macy #else
137eda14cbcSMatt Macy int zio_buf_debug_limit = 0;
138eda14cbcSMatt Macy #endif
139eda14cbcSMatt Macy 
140eda14cbcSMatt Macy static inline void __zio_execute(zio_t *zio);
141eda14cbcSMatt Macy 
142eda14cbcSMatt Macy static void zio_taskq_dispatch(zio_t *, zio_taskq_type_t, boolean_t);
143eda14cbcSMatt Macy 
144eda14cbcSMatt Macy void
145eda14cbcSMatt Macy zio_init(void)
146eda14cbcSMatt Macy {
147eda14cbcSMatt Macy 	size_t c;
148eda14cbcSMatt Macy 
149eda14cbcSMatt Macy 	zio_cache = kmem_cache_create("zio_cache",
150eda14cbcSMatt Macy 	    sizeof (zio_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
151eda14cbcSMatt Macy 	zio_link_cache = kmem_cache_create("zio_link_cache",
152eda14cbcSMatt Macy 	    sizeof (zio_link_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
153eda14cbcSMatt Macy 
154eda14cbcSMatt Macy 	/*
155eda14cbcSMatt Macy 	 * For small buffers, we want a cache for each multiple of
156eda14cbcSMatt Macy 	 * SPA_MINBLOCKSIZE.  For larger buffers, we want a cache
157eda14cbcSMatt Macy 	 * for each quarter-power of 2.
158eda14cbcSMatt Macy 	 */
159eda14cbcSMatt Macy 	for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) {
160eda14cbcSMatt Macy 		size_t size = (c + 1) << SPA_MINBLOCKSHIFT;
161eda14cbcSMatt Macy 		size_t p2 = size;
162eda14cbcSMatt Macy 		size_t align = 0;
163eda14cbcSMatt Macy 		size_t data_cflags, cflags;
164eda14cbcSMatt Macy 
165eda14cbcSMatt Macy 		data_cflags = KMC_NODEBUG;
166eda14cbcSMatt Macy 		cflags = (zio_exclude_metadata || size > zio_buf_debug_limit) ?
167eda14cbcSMatt Macy 		    KMC_NODEBUG : 0;
168eda14cbcSMatt Macy 
169eda14cbcSMatt Macy #if defined(_ILP32) && defined(_KERNEL)
170eda14cbcSMatt Macy 		/*
171eda14cbcSMatt Macy 		 * Cache size limited to 1M on 32-bit platforms until ARC
172eda14cbcSMatt Macy 		 * buffers no longer require virtual address space.
173eda14cbcSMatt Macy 		 */
174eda14cbcSMatt Macy 		if (size > zfs_max_recordsize)
175eda14cbcSMatt Macy 			break;
176eda14cbcSMatt Macy #endif
177eda14cbcSMatt Macy 
178eda14cbcSMatt Macy 		while (!ISP2(p2))
179eda14cbcSMatt Macy 			p2 &= p2 - 1;
180eda14cbcSMatt Macy 
181eda14cbcSMatt Macy #ifndef _KERNEL
182eda14cbcSMatt Macy 		/*
183eda14cbcSMatt Macy 		 * If we are using watchpoints, put each buffer on its own page,
184eda14cbcSMatt Macy 		 * to eliminate the performance overhead of trapping to the
185eda14cbcSMatt Macy 		 * kernel when modifying a non-watched buffer that shares the
186eda14cbcSMatt Macy 		 * page with a watched buffer.
187eda14cbcSMatt Macy 		 */
188eda14cbcSMatt Macy 		if (arc_watch && !IS_P2ALIGNED(size, PAGESIZE))
189eda14cbcSMatt Macy 			continue;
190eda14cbcSMatt Macy 		/*
191eda14cbcSMatt Macy 		 * Here's the problem - on 4K native devices in userland on
192eda14cbcSMatt Macy 		 * Linux using O_DIRECT, buffers must be 4K aligned or I/O
193eda14cbcSMatt Macy 		 * will fail with EINVAL, causing zdb (and others) to coredump.
194eda14cbcSMatt Macy 		 * Since userland probably doesn't need optimized buffer caches,
195eda14cbcSMatt Macy 		 * we just force 4K alignment on everything.
196eda14cbcSMatt Macy 		 */
197eda14cbcSMatt Macy 		align = 8 * SPA_MINBLOCKSIZE;
198eda14cbcSMatt Macy #else
199eda14cbcSMatt Macy 		if (size < PAGESIZE) {
200eda14cbcSMatt Macy 			align = SPA_MINBLOCKSIZE;
201eda14cbcSMatt Macy 		} else if (IS_P2ALIGNED(size, p2 >> 2)) {
202eda14cbcSMatt Macy 			align = PAGESIZE;
203eda14cbcSMatt Macy 		}
204eda14cbcSMatt Macy #endif
205eda14cbcSMatt Macy 
206eda14cbcSMatt Macy 		if (align != 0) {
207eda14cbcSMatt Macy 			char name[36];
2084426311aSMateusz Guzik 			if (cflags == data_cflags) {
2094426311aSMateusz Guzik 				/*
2104426311aSMateusz Guzik 				 * Resulting kmem caches would be identical.
2114426311aSMateusz Guzik 				 * Save memory by creating only one.
2124426311aSMateusz Guzik 				 */
2134426311aSMateusz Guzik 				(void) snprintf(name, sizeof (name),
2144426311aSMateusz Guzik 				    "zio_buf_comb_%lu", (ulong_t)size);
2154426311aSMateusz Guzik 				zio_buf_cache[c] = kmem_cache_create(name,
2164426311aSMateusz Guzik 				    size, align, NULL, NULL, NULL, NULL, NULL,
2174426311aSMateusz Guzik 				    cflags);
2184426311aSMateusz Guzik 				zio_data_buf_cache[c] = zio_buf_cache[c];
2194426311aSMateusz Guzik 				continue;
2204426311aSMateusz Guzik 			}
221eda14cbcSMatt Macy 			(void) snprintf(name, sizeof (name), "zio_buf_%lu",
222eda14cbcSMatt Macy 			    (ulong_t)size);
223eda14cbcSMatt Macy 			zio_buf_cache[c] = kmem_cache_create(name, size,
224eda14cbcSMatt Macy 			    align, NULL, NULL, NULL, NULL, NULL, cflags);
225eda14cbcSMatt Macy 
226eda14cbcSMatt Macy 			(void) snprintf(name, sizeof (name), "zio_data_buf_%lu",
227eda14cbcSMatt Macy 			    (ulong_t)size);
228eda14cbcSMatt Macy 			zio_data_buf_cache[c] = kmem_cache_create(name, size,
22941ce6225SMateusz Guzik 			    align, NULL, NULL, NULL, NULL, NULL, data_cflags);
230eda14cbcSMatt Macy 		}
231eda14cbcSMatt Macy 	}
232eda14cbcSMatt Macy 
233eda14cbcSMatt Macy 	while (--c != 0) {
234eda14cbcSMatt Macy 		ASSERT(zio_buf_cache[c] != NULL);
235eda14cbcSMatt Macy 		if (zio_buf_cache[c - 1] == NULL)
236eda14cbcSMatt Macy 			zio_buf_cache[c - 1] = zio_buf_cache[c];
237eda14cbcSMatt Macy 
238eda14cbcSMatt Macy 		ASSERT(zio_data_buf_cache[c] != NULL);
239eda14cbcSMatt Macy 		if (zio_data_buf_cache[c - 1] == NULL)
240eda14cbcSMatt Macy 			zio_data_buf_cache[c - 1] = zio_data_buf_cache[c];
241eda14cbcSMatt Macy 	}
242eda14cbcSMatt Macy 
243eda14cbcSMatt Macy 	zio_inject_init();
244eda14cbcSMatt Macy 
245eda14cbcSMatt Macy 	lz4_init();
246eda14cbcSMatt Macy }
247eda14cbcSMatt Macy 
248eda14cbcSMatt Macy void
249eda14cbcSMatt Macy zio_fini(void)
250eda14cbcSMatt Macy {
25116038816SMartin Matuska 	size_t n = SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT;
2524426311aSMateusz Guzik 
253eda14cbcSMatt Macy #if defined(ZFS_DEBUG) && !defined(_KERNEL)
25416038816SMartin Matuska 	for (size_t i = 0; i < n; i++) {
2554426311aSMateusz Guzik 		if (zio_buf_cache_allocs[i] != zio_buf_cache_frees[i])
256eda14cbcSMatt Macy 			(void) printf("zio_fini: [%d] %llu != %llu\n",
2574426311aSMateusz Guzik 			    (int)((i + 1) << SPA_MINBLOCKSHIFT),
2584426311aSMateusz Guzik 			    (long long unsigned)zio_buf_cache_allocs[i],
2594426311aSMateusz Guzik 			    (long long unsigned)zio_buf_cache_frees[i]);
2604426311aSMateusz Guzik 	}
261eda14cbcSMatt Macy #endif
262eda14cbcSMatt Macy 
2634426311aSMateusz Guzik 	/*
2644426311aSMateusz Guzik 	 * The same kmem cache can show up multiple times in both zio_buf_cache
2654426311aSMateusz Guzik 	 * and zio_data_buf_cache. Do a wasteful but trivially correct scan to
2664426311aSMateusz Guzik 	 * sort it out.
2674426311aSMateusz Guzik 	 */
26816038816SMartin Matuska 	for (size_t i = 0; i < n; i++) {
26916038816SMartin Matuska 		kmem_cache_t *cache = zio_buf_cache[i];
2704426311aSMateusz Guzik 		if (cache == NULL)
2714426311aSMateusz Guzik 			continue;
27216038816SMartin Matuska 		for (size_t j = i; j < n; j++) {
2734426311aSMateusz Guzik 			if (cache == zio_buf_cache[j])
2744426311aSMateusz Guzik 				zio_buf_cache[j] = NULL;
2754426311aSMateusz Guzik 			if (cache == zio_data_buf_cache[j])
2764426311aSMateusz Guzik 				zio_data_buf_cache[j] = NULL;
277eda14cbcSMatt Macy 		}
2784426311aSMateusz Guzik 		kmem_cache_destroy(cache);
2794426311aSMateusz Guzik 	}
2804426311aSMateusz Guzik 
28116038816SMartin Matuska 	for (size_t i = 0; i < n; i++) {
28216038816SMartin Matuska 		kmem_cache_t *cache = zio_data_buf_cache[i];
2834426311aSMateusz Guzik 		if (cache == NULL)
2844426311aSMateusz Guzik 			continue;
28516038816SMartin Matuska 		for (size_t j = i; j < n; j++) {
2864426311aSMateusz Guzik 			if (cache == zio_data_buf_cache[j])
2874426311aSMateusz Guzik 				zio_data_buf_cache[j] = NULL;
2884426311aSMateusz Guzik 		}
2894426311aSMateusz Guzik 		kmem_cache_destroy(cache);
2904426311aSMateusz Guzik 	}
2914426311aSMateusz Guzik 
29216038816SMartin Matuska 	for (size_t i = 0; i < n; i++) {
29316038816SMartin Matuska 		VERIFY3P(zio_buf_cache[i], ==, NULL);
29416038816SMartin Matuska 		VERIFY3P(zio_data_buf_cache[i], ==, NULL);
295eda14cbcSMatt Macy 	}
296eda14cbcSMatt Macy 
297eda14cbcSMatt Macy 	kmem_cache_destroy(zio_link_cache);
298eda14cbcSMatt Macy 	kmem_cache_destroy(zio_cache);
299eda14cbcSMatt Macy 
300eda14cbcSMatt Macy 	zio_inject_fini();
301eda14cbcSMatt Macy 
302eda14cbcSMatt Macy 	lz4_fini();
303eda14cbcSMatt Macy }
304eda14cbcSMatt Macy 
305eda14cbcSMatt Macy /*
306eda14cbcSMatt Macy  * ==========================================================================
307eda14cbcSMatt Macy  * Allocate and free I/O buffers
308eda14cbcSMatt Macy  * ==========================================================================
309eda14cbcSMatt Macy  */
310eda14cbcSMatt Macy 
311eda14cbcSMatt Macy /*
312eda14cbcSMatt Macy  * Use zio_buf_alloc to allocate ZFS metadata.  This data will appear in a
313eda14cbcSMatt Macy  * crashdump if the kernel panics, so use it judiciously.  Obviously, it's
314eda14cbcSMatt Macy  * useful to inspect ZFS metadata, but if possible, we should avoid keeping
315eda14cbcSMatt Macy  * excess / transient data in-core during a crashdump.
316eda14cbcSMatt Macy  */
317eda14cbcSMatt Macy void *
318eda14cbcSMatt Macy zio_buf_alloc(size_t size)
319eda14cbcSMatt Macy {
320eda14cbcSMatt Macy 	size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
321eda14cbcSMatt Macy 
322eda14cbcSMatt Macy 	VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
323eda14cbcSMatt Macy #if defined(ZFS_DEBUG) && !defined(_KERNEL)
324eda14cbcSMatt Macy 	atomic_add_64(&zio_buf_cache_allocs[c], 1);
325eda14cbcSMatt Macy #endif
326eda14cbcSMatt Macy 
327eda14cbcSMatt Macy 	return (kmem_cache_alloc(zio_buf_cache[c], KM_PUSHPAGE));
328eda14cbcSMatt Macy }
329eda14cbcSMatt Macy 
330eda14cbcSMatt Macy /*
331eda14cbcSMatt Macy  * Use zio_data_buf_alloc to allocate data.  The data will not appear in a
332eda14cbcSMatt Macy  * crashdump if the kernel panics.  This exists so that we will limit the amount
333eda14cbcSMatt Macy  * of ZFS data that shows up in a kernel crashdump.  (Thus reducing the amount
334eda14cbcSMatt Macy  * of kernel heap dumped to disk when the kernel panics)
335eda14cbcSMatt Macy  */
336eda14cbcSMatt Macy void *
337eda14cbcSMatt Macy zio_data_buf_alloc(size_t size)
338eda14cbcSMatt Macy {
339eda14cbcSMatt Macy 	size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
340eda14cbcSMatt Macy 
341eda14cbcSMatt Macy 	VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
342eda14cbcSMatt Macy 
343eda14cbcSMatt Macy 	return (kmem_cache_alloc(zio_data_buf_cache[c], KM_PUSHPAGE));
344eda14cbcSMatt Macy }
345eda14cbcSMatt Macy 
346eda14cbcSMatt Macy void
347eda14cbcSMatt Macy zio_buf_free(void *buf, size_t size)
348eda14cbcSMatt Macy {
349eda14cbcSMatt Macy 	size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
350eda14cbcSMatt Macy 
351eda14cbcSMatt Macy 	VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
352eda14cbcSMatt Macy #if defined(ZFS_DEBUG) && !defined(_KERNEL)
353eda14cbcSMatt Macy 	atomic_add_64(&zio_buf_cache_frees[c], 1);
354eda14cbcSMatt Macy #endif
355eda14cbcSMatt Macy 
356eda14cbcSMatt Macy 	kmem_cache_free(zio_buf_cache[c], buf);
357eda14cbcSMatt Macy }
358eda14cbcSMatt Macy 
359eda14cbcSMatt Macy void
360eda14cbcSMatt Macy zio_data_buf_free(void *buf, size_t size)
361eda14cbcSMatt Macy {
362eda14cbcSMatt Macy 	size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
363eda14cbcSMatt Macy 
364eda14cbcSMatt Macy 	VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
365eda14cbcSMatt Macy 
366eda14cbcSMatt Macy 	kmem_cache_free(zio_data_buf_cache[c], buf);
367eda14cbcSMatt Macy }
368eda14cbcSMatt Macy 
369eda14cbcSMatt Macy static void
370eda14cbcSMatt Macy zio_abd_free(void *abd, size_t size)
371eda14cbcSMatt Macy {
372eda14cbcSMatt Macy 	abd_free((abd_t *)abd);
373eda14cbcSMatt Macy }
374eda14cbcSMatt Macy 
375eda14cbcSMatt Macy /*
376eda14cbcSMatt Macy  * ==========================================================================
377eda14cbcSMatt Macy  * Push and pop I/O transform buffers
378eda14cbcSMatt Macy  * ==========================================================================
379eda14cbcSMatt Macy  */
380eda14cbcSMatt Macy void
381eda14cbcSMatt Macy zio_push_transform(zio_t *zio, abd_t *data, uint64_t size, uint64_t bufsize,
382eda14cbcSMatt Macy     zio_transform_func_t *transform)
383eda14cbcSMatt Macy {
384eda14cbcSMatt Macy 	zio_transform_t *zt = kmem_alloc(sizeof (zio_transform_t), KM_SLEEP);
385eda14cbcSMatt Macy 
386eda14cbcSMatt Macy 	zt->zt_orig_abd = zio->io_abd;
387eda14cbcSMatt Macy 	zt->zt_orig_size = zio->io_size;
388eda14cbcSMatt Macy 	zt->zt_bufsize = bufsize;
389eda14cbcSMatt Macy 	zt->zt_transform = transform;
390eda14cbcSMatt Macy 
391eda14cbcSMatt Macy 	zt->zt_next = zio->io_transform_stack;
392eda14cbcSMatt Macy 	zio->io_transform_stack = zt;
393eda14cbcSMatt Macy 
394eda14cbcSMatt Macy 	zio->io_abd = data;
395eda14cbcSMatt Macy 	zio->io_size = size;
396eda14cbcSMatt Macy }
397eda14cbcSMatt Macy 
398eda14cbcSMatt Macy void
399eda14cbcSMatt Macy zio_pop_transforms(zio_t *zio)
400eda14cbcSMatt Macy {
401eda14cbcSMatt Macy 	zio_transform_t *zt;
402eda14cbcSMatt Macy 
403eda14cbcSMatt Macy 	while ((zt = zio->io_transform_stack) != NULL) {
404eda14cbcSMatt Macy 		if (zt->zt_transform != NULL)
405eda14cbcSMatt Macy 			zt->zt_transform(zio,
406eda14cbcSMatt Macy 			    zt->zt_orig_abd, zt->zt_orig_size);
407eda14cbcSMatt Macy 
408eda14cbcSMatt Macy 		if (zt->zt_bufsize != 0)
409eda14cbcSMatt Macy 			abd_free(zio->io_abd);
410eda14cbcSMatt Macy 
411eda14cbcSMatt Macy 		zio->io_abd = zt->zt_orig_abd;
412eda14cbcSMatt Macy 		zio->io_size = zt->zt_orig_size;
413eda14cbcSMatt Macy 		zio->io_transform_stack = zt->zt_next;
414eda14cbcSMatt Macy 
415eda14cbcSMatt Macy 		kmem_free(zt, sizeof (zio_transform_t));
416eda14cbcSMatt Macy 	}
417eda14cbcSMatt Macy }
418eda14cbcSMatt Macy 
419eda14cbcSMatt Macy /*
420eda14cbcSMatt Macy  * ==========================================================================
421eda14cbcSMatt Macy  * I/O transform callbacks for subblocks, decompression, and decryption
422eda14cbcSMatt Macy  * ==========================================================================
423eda14cbcSMatt Macy  */
424eda14cbcSMatt Macy static void
425eda14cbcSMatt Macy zio_subblock(zio_t *zio, abd_t *data, uint64_t size)
426eda14cbcSMatt Macy {
427eda14cbcSMatt Macy 	ASSERT(zio->io_size > size);
428eda14cbcSMatt Macy 
429eda14cbcSMatt Macy 	if (zio->io_type == ZIO_TYPE_READ)
430eda14cbcSMatt Macy 		abd_copy(data, zio->io_abd, size);
431eda14cbcSMatt Macy }
432eda14cbcSMatt Macy 
433eda14cbcSMatt Macy static void
434eda14cbcSMatt Macy zio_decompress(zio_t *zio, abd_t *data, uint64_t size)
435eda14cbcSMatt Macy {
436eda14cbcSMatt Macy 	if (zio->io_error == 0) {
437eda14cbcSMatt Macy 		void *tmp = abd_borrow_buf(data, size);
438eda14cbcSMatt Macy 		int ret = zio_decompress_data(BP_GET_COMPRESS(zio->io_bp),
439eda14cbcSMatt Macy 		    zio->io_abd, tmp, zio->io_size, size,
440eda14cbcSMatt Macy 		    &zio->io_prop.zp_complevel);
441eda14cbcSMatt Macy 		abd_return_buf_copy(data, tmp, size);
442eda14cbcSMatt Macy 
443eda14cbcSMatt Macy 		if (zio_injection_enabled && ret == 0)
444eda14cbcSMatt Macy 			ret = zio_handle_fault_injection(zio, EINVAL);
445eda14cbcSMatt Macy 
446eda14cbcSMatt Macy 		if (ret != 0)
447eda14cbcSMatt Macy 			zio->io_error = SET_ERROR(EIO);
448eda14cbcSMatt Macy 	}
449eda14cbcSMatt Macy }
450eda14cbcSMatt Macy 
451eda14cbcSMatt Macy static void
452eda14cbcSMatt Macy zio_decrypt(zio_t *zio, abd_t *data, uint64_t size)
453eda14cbcSMatt Macy {
454eda14cbcSMatt Macy 	int ret;
455eda14cbcSMatt Macy 	void *tmp;
456eda14cbcSMatt Macy 	blkptr_t *bp = zio->io_bp;
457eda14cbcSMatt Macy 	spa_t *spa = zio->io_spa;
458eda14cbcSMatt Macy 	uint64_t dsobj = zio->io_bookmark.zb_objset;
459eda14cbcSMatt Macy 	uint64_t lsize = BP_GET_LSIZE(bp);
460eda14cbcSMatt Macy 	dmu_object_type_t ot = BP_GET_TYPE(bp);
461eda14cbcSMatt Macy 	uint8_t salt[ZIO_DATA_SALT_LEN];
462eda14cbcSMatt Macy 	uint8_t iv[ZIO_DATA_IV_LEN];
463eda14cbcSMatt Macy 	uint8_t mac[ZIO_DATA_MAC_LEN];
464eda14cbcSMatt Macy 	boolean_t no_crypt = B_FALSE;
465eda14cbcSMatt Macy 
466eda14cbcSMatt Macy 	ASSERT(BP_USES_CRYPT(bp));
467eda14cbcSMatt Macy 	ASSERT3U(size, !=, 0);
468eda14cbcSMatt Macy 
469eda14cbcSMatt Macy 	if (zio->io_error != 0)
470eda14cbcSMatt Macy 		return;
471eda14cbcSMatt Macy 
472eda14cbcSMatt Macy 	/*
473eda14cbcSMatt Macy 	 * Verify the cksum of MACs stored in an indirect bp. It will always
474eda14cbcSMatt Macy 	 * be possible to verify this since it does not require an encryption
475eda14cbcSMatt Macy 	 * key.
476eda14cbcSMatt Macy 	 */
477eda14cbcSMatt Macy 	if (BP_HAS_INDIRECT_MAC_CKSUM(bp)) {
478eda14cbcSMatt Macy 		zio_crypt_decode_mac_bp(bp, mac);
479eda14cbcSMatt Macy 
480eda14cbcSMatt Macy 		if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF) {
481eda14cbcSMatt Macy 			/*
482eda14cbcSMatt Macy 			 * We haven't decompressed the data yet, but
483eda14cbcSMatt Macy 			 * zio_crypt_do_indirect_mac_checksum() requires
484eda14cbcSMatt Macy 			 * decompressed data to be able to parse out the MACs
485eda14cbcSMatt Macy 			 * from the indirect block. We decompress it now and
486eda14cbcSMatt Macy 			 * throw away the result after we are finished.
487eda14cbcSMatt Macy 			 */
488eda14cbcSMatt Macy 			tmp = zio_buf_alloc(lsize);
489eda14cbcSMatt Macy 			ret = zio_decompress_data(BP_GET_COMPRESS(bp),
490eda14cbcSMatt Macy 			    zio->io_abd, tmp, zio->io_size, lsize,
491eda14cbcSMatt Macy 			    &zio->io_prop.zp_complevel);
492eda14cbcSMatt Macy 			if (ret != 0) {
493eda14cbcSMatt Macy 				ret = SET_ERROR(EIO);
494eda14cbcSMatt Macy 				goto error;
495eda14cbcSMatt Macy 			}
496eda14cbcSMatt Macy 			ret = zio_crypt_do_indirect_mac_checksum(B_FALSE,
497eda14cbcSMatt Macy 			    tmp, lsize, BP_SHOULD_BYTESWAP(bp), mac);
498eda14cbcSMatt Macy 			zio_buf_free(tmp, lsize);
499eda14cbcSMatt Macy 		} else {
500eda14cbcSMatt Macy 			ret = zio_crypt_do_indirect_mac_checksum_abd(B_FALSE,
501eda14cbcSMatt Macy 			    zio->io_abd, size, BP_SHOULD_BYTESWAP(bp), mac);
502eda14cbcSMatt Macy 		}
503eda14cbcSMatt Macy 		abd_copy(data, zio->io_abd, size);
504eda14cbcSMatt Macy 
505eda14cbcSMatt Macy 		if (zio_injection_enabled && ot != DMU_OT_DNODE && ret == 0) {
506eda14cbcSMatt Macy 			ret = zio_handle_decrypt_injection(spa,
507eda14cbcSMatt Macy 			    &zio->io_bookmark, ot, ECKSUM);
508eda14cbcSMatt Macy 		}
509eda14cbcSMatt Macy 		if (ret != 0)
510eda14cbcSMatt Macy 			goto error;
511eda14cbcSMatt Macy 
512eda14cbcSMatt Macy 		return;
513eda14cbcSMatt Macy 	}
514eda14cbcSMatt Macy 
515eda14cbcSMatt Macy 	/*
516eda14cbcSMatt Macy 	 * If this is an authenticated block, just check the MAC. It would be
517eda14cbcSMatt Macy 	 * nice to separate this out into its own flag, but for the moment
518eda14cbcSMatt Macy 	 * enum zio_flag is out of bits.
519eda14cbcSMatt Macy 	 */
520eda14cbcSMatt Macy 	if (BP_IS_AUTHENTICATED(bp)) {
521eda14cbcSMatt Macy 		if (ot == DMU_OT_OBJSET) {
522eda14cbcSMatt Macy 			ret = spa_do_crypt_objset_mac_abd(B_FALSE, spa,
523eda14cbcSMatt Macy 			    dsobj, zio->io_abd, size, BP_SHOULD_BYTESWAP(bp));
524eda14cbcSMatt Macy 		} else {
525eda14cbcSMatt Macy 			zio_crypt_decode_mac_bp(bp, mac);
526eda14cbcSMatt Macy 			ret = spa_do_crypt_mac_abd(B_FALSE, spa, dsobj,
527eda14cbcSMatt Macy 			    zio->io_abd, size, mac);
528eda14cbcSMatt Macy 			if (zio_injection_enabled && ret == 0) {
529eda14cbcSMatt Macy 				ret = zio_handle_decrypt_injection(spa,
530eda14cbcSMatt Macy 				    &zio->io_bookmark, ot, ECKSUM);
531eda14cbcSMatt Macy 			}
532eda14cbcSMatt Macy 		}
533eda14cbcSMatt Macy 		abd_copy(data, zio->io_abd, size);
534eda14cbcSMatt Macy 
535eda14cbcSMatt Macy 		if (ret != 0)
536eda14cbcSMatt Macy 			goto error;
537eda14cbcSMatt Macy 
538eda14cbcSMatt Macy 		return;
539eda14cbcSMatt Macy 	}
540eda14cbcSMatt Macy 
541eda14cbcSMatt Macy 	zio_crypt_decode_params_bp(bp, salt, iv);
542eda14cbcSMatt Macy 
543eda14cbcSMatt Macy 	if (ot == DMU_OT_INTENT_LOG) {
544eda14cbcSMatt Macy 		tmp = abd_borrow_buf_copy(zio->io_abd, sizeof (zil_chain_t));
545eda14cbcSMatt Macy 		zio_crypt_decode_mac_zil(tmp, mac);
546eda14cbcSMatt Macy 		abd_return_buf(zio->io_abd, tmp, sizeof (zil_chain_t));
547eda14cbcSMatt Macy 	} else {
548eda14cbcSMatt Macy 		zio_crypt_decode_mac_bp(bp, mac);
549eda14cbcSMatt Macy 	}
550eda14cbcSMatt Macy 
551eda14cbcSMatt Macy 	ret = spa_do_crypt_abd(B_FALSE, spa, &zio->io_bookmark, BP_GET_TYPE(bp),
552eda14cbcSMatt Macy 	    BP_GET_DEDUP(bp), BP_SHOULD_BYTESWAP(bp), salt, iv, mac, size, data,
553eda14cbcSMatt Macy 	    zio->io_abd, &no_crypt);
554eda14cbcSMatt Macy 	if (no_crypt)
555eda14cbcSMatt Macy 		abd_copy(data, zio->io_abd, size);
556eda14cbcSMatt Macy 
557eda14cbcSMatt Macy 	if (ret != 0)
558eda14cbcSMatt Macy 		goto error;
559eda14cbcSMatt Macy 
560eda14cbcSMatt Macy 	return;
561eda14cbcSMatt Macy 
562eda14cbcSMatt Macy error:
563eda14cbcSMatt Macy 	/* assert that the key was found unless this was speculative */
564eda14cbcSMatt Macy 	ASSERT(ret != EACCES || (zio->io_flags & ZIO_FLAG_SPECULATIVE));
565eda14cbcSMatt Macy 
566eda14cbcSMatt Macy 	/*
567eda14cbcSMatt Macy 	 * If there was a decryption / authentication error return EIO as
568eda14cbcSMatt Macy 	 * the io_error. If this was not a speculative zio, create an ereport.
569eda14cbcSMatt Macy 	 */
570eda14cbcSMatt Macy 	if (ret == ECKSUM) {
571eda14cbcSMatt Macy 		zio->io_error = SET_ERROR(EIO);
572eda14cbcSMatt Macy 		if ((zio->io_flags & ZIO_FLAG_SPECULATIVE) == 0) {
573eda14cbcSMatt Macy 			spa_log_error(spa, &zio->io_bookmark);
574eac7052fSMatt Macy 			(void) zfs_ereport_post(FM_EREPORT_ZFS_AUTHENTICATION,
5752c48331dSMatt Macy 			    spa, NULL, &zio->io_bookmark, zio, 0);
576eda14cbcSMatt Macy 		}
577eda14cbcSMatt Macy 	} else {
578eda14cbcSMatt Macy 		zio->io_error = ret;
579eda14cbcSMatt Macy 	}
580eda14cbcSMatt Macy }
581eda14cbcSMatt Macy 
582eda14cbcSMatt Macy /*
583eda14cbcSMatt Macy  * ==========================================================================
584eda14cbcSMatt Macy  * I/O parent/child relationships and pipeline interlocks
585eda14cbcSMatt Macy  * ==========================================================================
586eda14cbcSMatt Macy  */
587eda14cbcSMatt Macy zio_t *
588eda14cbcSMatt Macy zio_walk_parents(zio_t *cio, zio_link_t **zl)
589eda14cbcSMatt Macy {
590eda14cbcSMatt Macy 	list_t *pl = &cio->io_parent_list;
591eda14cbcSMatt Macy 
592eda14cbcSMatt Macy 	*zl = (*zl == NULL) ? list_head(pl) : list_next(pl, *zl);
593eda14cbcSMatt Macy 	if (*zl == NULL)
594eda14cbcSMatt Macy 		return (NULL);
595eda14cbcSMatt Macy 
596eda14cbcSMatt Macy 	ASSERT((*zl)->zl_child == cio);
597eda14cbcSMatt Macy 	return ((*zl)->zl_parent);
598eda14cbcSMatt Macy }
599eda14cbcSMatt Macy 
600eda14cbcSMatt Macy zio_t *
601eda14cbcSMatt Macy zio_walk_children(zio_t *pio, zio_link_t **zl)
602eda14cbcSMatt Macy {
603eda14cbcSMatt Macy 	list_t *cl = &pio->io_child_list;
604eda14cbcSMatt Macy 
605eda14cbcSMatt Macy 	ASSERT(MUTEX_HELD(&pio->io_lock));
606eda14cbcSMatt Macy 
607eda14cbcSMatt Macy 	*zl = (*zl == NULL) ? list_head(cl) : list_next(cl, *zl);
608eda14cbcSMatt Macy 	if (*zl == NULL)
609eda14cbcSMatt Macy 		return (NULL);
610eda14cbcSMatt Macy 
611eda14cbcSMatt Macy 	ASSERT((*zl)->zl_parent == pio);
612eda14cbcSMatt Macy 	return ((*zl)->zl_child);
613eda14cbcSMatt Macy }
614eda14cbcSMatt Macy 
615eda14cbcSMatt Macy zio_t *
616eda14cbcSMatt Macy zio_unique_parent(zio_t *cio)
617eda14cbcSMatt Macy {
618eda14cbcSMatt Macy 	zio_link_t *zl = NULL;
619eda14cbcSMatt Macy 	zio_t *pio = zio_walk_parents(cio, &zl);
620eda14cbcSMatt Macy 
621eda14cbcSMatt Macy 	VERIFY3P(zio_walk_parents(cio, &zl), ==, NULL);
622eda14cbcSMatt Macy 	return (pio);
623eda14cbcSMatt Macy }
624eda14cbcSMatt Macy 
625eda14cbcSMatt Macy void
626eda14cbcSMatt Macy zio_add_child(zio_t *pio, zio_t *cio)
627eda14cbcSMatt Macy {
628eda14cbcSMatt Macy 	zio_link_t *zl = kmem_cache_alloc(zio_link_cache, KM_SLEEP);
629eda14cbcSMatt Macy 
630eda14cbcSMatt Macy 	/*
631eda14cbcSMatt Macy 	 * Logical I/Os can have logical, gang, or vdev children.
632eda14cbcSMatt Macy 	 * Gang I/Os can have gang or vdev children.
633eda14cbcSMatt Macy 	 * Vdev I/Os can only have vdev children.
634eda14cbcSMatt Macy 	 * The following ASSERT captures all of these constraints.
635eda14cbcSMatt Macy 	 */
636eda14cbcSMatt Macy 	ASSERT3S(cio->io_child_type, <=, pio->io_child_type);
637eda14cbcSMatt Macy 
638eda14cbcSMatt Macy 	zl->zl_parent = pio;
639eda14cbcSMatt Macy 	zl->zl_child = cio;
640eda14cbcSMatt Macy 
641eda14cbcSMatt Macy 	mutex_enter(&pio->io_lock);
642eda14cbcSMatt Macy 	mutex_enter(&cio->io_lock);
643eda14cbcSMatt Macy 
644eda14cbcSMatt Macy 	ASSERT(pio->io_state[ZIO_WAIT_DONE] == 0);
645eda14cbcSMatt Macy 
646eda14cbcSMatt Macy 	for (int w = 0; w < ZIO_WAIT_TYPES; w++)
647eda14cbcSMatt Macy 		pio->io_children[cio->io_child_type][w] += !cio->io_state[w];
648eda14cbcSMatt Macy 
649eda14cbcSMatt Macy 	list_insert_head(&pio->io_child_list, zl);
650eda14cbcSMatt Macy 	list_insert_head(&cio->io_parent_list, zl);
651eda14cbcSMatt Macy 
652eda14cbcSMatt Macy 	pio->io_child_count++;
653eda14cbcSMatt Macy 	cio->io_parent_count++;
654eda14cbcSMatt Macy 
655eda14cbcSMatt Macy 	mutex_exit(&cio->io_lock);
656eda14cbcSMatt Macy 	mutex_exit(&pio->io_lock);
657eda14cbcSMatt Macy }
658eda14cbcSMatt Macy 
659eda14cbcSMatt Macy static void
660eda14cbcSMatt Macy zio_remove_child(zio_t *pio, zio_t *cio, zio_link_t *zl)
661eda14cbcSMatt Macy {
662eda14cbcSMatt Macy 	ASSERT(zl->zl_parent == pio);
663eda14cbcSMatt Macy 	ASSERT(zl->zl_child == cio);
664eda14cbcSMatt Macy 
665eda14cbcSMatt Macy 	mutex_enter(&pio->io_lock);
666eda14cbcSMatt Macy 	mutex_enter(&cio->io_lock);
667eda14cbcSMatt Macy 
668eda14cbcSMatt Macy 	list_remove(&pio->io_child_list, zl);
669eda14cbcSMatt Macy 	list_remove(&cio->io_parent_list, zl);
670eda14cbcSMatt Macy 
671eda14cbcSMatt Macy 	pio->io_child_count--;
672eda14cbcSMatt Macy 	cio->io_parent_count--;
673eda14cbcSMatt Macy 
674eda14cbcSMatt Macy 	mutex_exit(&cio->io_lock);
675eda14cbcSMatt Macy 	mutex_exit(&pio->io_lock);
676eda14cbcSMatt Macy 	kmem_cache_free(zio_link_cache, zl);
677eda14cbcSMatt Macy }
678eda14cbcSMatt Macy 
679eda14cbcSMatt Macy static boolean_t
680eda14cbcSMatt Macy zio_wait_for_children(zio_t *zio, uint8_t childbits, enum zio_wait_type wait)
681eda14cbcSMatt Macy {
682eda14cbcSMatt Macy 	boolean_t waiting = B_FALSE;
683eda14cbcSMatt Macy 
684eda14cbcSMatt Macy 	mutex_enter(&zio->io_lock);
685eda14cbcSMatt Macy 	ASSERT(zio->io_stall == NULL);
686eda14cbcSMatt Macy 	for (int c = 0; c < ZIO_CHILD_TYPES; c++) {
687eda14cbcSMatt Macy 		if (!(ZIO_CHILD_BIT_IS_SET(childbits, c)))
688eda14cbcSMatt Macy 			continue;
689eda14cbcSMatt Macy 
690eda14cbcSMatt Macy 		uint64_t *countp = &zio->io_children[c][wait];
691eda14cbcSMatt Macy 		if (*countp != 0) {
692eda14cbcSMatt Macy 			zio->io_stage >>= 1;
693eda14cbcSMatt Macy 			ASSERT3U(zio->io_stage, !=, ZIO_STAGE_OPEN);
694eda14cbcSMatt Macy 			zio->io_stall = countp;
695eda14cbcSMatt Macy 			waiting = B_TRUE;
696eda14cbcSMatt Macy 			break;
697eda14cbcSMatt Macy 		}
698eda14cbcSMatt Macy 	}
699eda14cbcSMatt Macy 	mutex_exit(&zio->io_lock);
700eda14cbcSMatt Macy 	return (waiting);
701eda14cbcSMatt Macy }
702eda14cbcSMatt Macy 
703eda14cbcSMatt Macy __attribute__((always_inline))
704eda14cbcSMatt Macy static inline void
705eda14cbcSMatt Macy zio_notify_parent(zio_t *pio, zio_t *zio, enum zio_wait_type wait,
706eda14cbcSMatt Macy     zio_t **next_to_executep)
707eda14cbcSMatt Macy {
708eda14cbcSMatt Macy 	uint64_t *countp = &pio->io_children[zio->io_child_type][wait];
709eda14cbcSMatt Macy 	int *errorp = &pio->io_child_error[zio->io_child_type];
710eda14cbcSMatt Macy 
711eda14cbcSMatt Macy 	mutex_enter(&pio->io_lock);
712eda14cbcSMatt Macy 	if (zio->io_error && !(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
713eda14cbcSMatt Macy 		*errorp = zio_worst_error(*errorp, zio->io_error);
714eda14cbcSMatt Macy 	pio->io_reexecute |= zio->io_reexecute;
715eda14cbcSMatt Macy 	ASSERT3U(*countp, >, 0);
716eda14cbcSMatt Macy 
717eda14cbcSMatt Macy 	(*countp)--;
718eda14cbcSMatt Macy 
719eda14cbcSMatt Macy 	if (*countp == 0 && pio->io_stall == countp) {
720eda14cbcSMatt Macy 		zio_taskq_type_t type =
721eda14cbcSMatt Macy 		    pio->io_stage < ZIO_STAGE_VDEV_IO_START ? ZIO_TASKQ_ISSUE :
722eda14cbcSMatt Macy 		    ZIO_TASKQ_INTERRUPT;
723eda14cbcSMatt Macy 		pio->io_stall = NULL;
724eda14cbcSMatt Macy 		mutex_exit(&pio->io_lock);
725eda14cbcSMatt Macy 
726eda14cbcSMatt Macy 		/*
727eda14cbcSMatt Macy 		 * If we can tell the caller to execute this parent next, do
728eda14cbcSMatt Macy 		 * so.  Otherwise dispatch the parent zio as its own task.
729eda14cbcSMatt Macy 		 *
730eda14cbcSMatt Macy 		 * Having the caller execute the parent when possible reduces
731eda14cbcSMatt Macy 		 * locking on the zio taskq's, reduces context switch
732eda14cbcSMatt Macy 		 * overhead, and has no recursion penalty.  Note that one
733eda14cbcSMatt Macy 		 * read from disk typically causes at least 3 zio's: a
734eda14cbcSMatt Macy 		 * zio_null(), the logical zio_read(), and then a physical
735eda14cbcSMatt Macy 		 * zio.  When the physical ZIO completes, we are able to call
736eda14cbcSMatt Macy 		 * zio_done() on all 3 of these zio's from one invocation of
737eda14cbcSMatt Macy 		 * zio_execute() by returning the parent back to
738eda14cbcSMatt Macy 		 * zio_execute().  Since the parent isn't executed until this
739eda14cbcSMatt Macy 		 * thread returns back to zio_execute(), the caller should do
740eda14cbcSMatt Macy 		 * so promptly.
741eda14cbcSMatt Macy 		 *
742eda14cbcSMatt Macy 		 * In other cases, dispatching the parent prevents
743eda14cbcSMatt Macy 		 * overflowing the stack when we have deeply nested
744eda14cbcSMatt Macy 		 * parent-child relationships, as we do with the "mega zio"
745eda14cbcSMatt Macy 		 * of writes for spa_sync(), and the chain of ZIL blocks.
746eda14cbcSMatt Macy 		 */
747eda14cbcSMatt Macy 		if (next_to_executep != NULL && *next_to_executep == NULL) {
748eda14cbcSMatt Macy 			*next_to_executep = pio;
749eda14cbcSMatt Macy 		} else {
750eda14cbcSMatt Macy 			zio_taskq_dispatch(pio, type, B_FALSE);
751eda14cbcSMatt Macy 		}
752eda14cbcSMatt Macy 	} else {
753eda14cbcSMatt Macy 		mutex_exit(&pio->io_lock);
754eda14cbcSMatt Macy 	}
755eda14cbcSMatt Macy }
756eda14cbcSMatt Macy 
757eda14cbcSMatt Macy static void
758eda14cbcSMatt Macy zio_inherit_child_errors(zio_t *zio, enum zio_child c)
759eda14cbcSMatt Macy {
760eda14cbcSMatt Macy 	if (zio->io_child_error[c] != 0 && zio->io_error == 0)
761eda14cbcSMatt Macy 		zio->io_error = zio->io_child_error[c];
762eda14cbcSMatt Macy }
763eda14cbcSMatt Macy 
764eda14cbcSMatt Macy int
765eda14cbcSMatt Macy zio_bookmark_compare(const void *x1, const void *x2)
766eda14cbcSMatt Macy {
767eda14cbcSMatt Macy 	const zio_t *z1 = x1;
768eda14cbcSMatt Macy 	const zio_t *z2 = x2;
769eda14cbcSMatt Macy 
770eda14cbcSMatt Macy 	if (z1->io_bookmark.zb_objset < z2->io_bookmark.zb_objset)
771eda14cbcSMatt Macy 		return (-1);
772eda14cbcSMatt Macy 	if (z1->io_bookmark.zb_objset > z2->io_bookmark.zb_objset)
773eda14cbcSMatt Macy 		return (1);
774eda14cbcSMatt Macy 
775eda14cbcSMatt Macy 	if (z1->io_bookmark.zb_object < z2->io_bookmark.zb_object)
776eda14cbcSMatt Macy 		return (-1);
777eda14cbcSMatt Macy 	if (z1->io_bookmark.zb_object > z2->io_bookmark.zb_object)
778eda14cbcSMatt Macy 		return (1);
779eda14cbcSMatt Macy 
780eda14cbcSMatt Macy 	if (z1->io_bookmark.zb_level < z2->io_bookmark.zb_level)
781eda14cbcSMatt Macy 		return (-1);
782eda14cbcSMatt Macy 	if (z1->io_bookmark.zb_level > z2->io_bookmark.zb_level)
783eda14cbcSMatt Macy 		return (1);
784eda14cbcSMatt Macy 
785eda14cbcSMatt Macy 	if (z1->io_bookmark.zb_blkid < z2->io_bookmark.zb_blkid)
786eda14cbcSMatt Macy 		return (-1);
787eda14cbcSMatt Macy 	if (z1->io_bookmark.zb_blkid > z2->io_bookmark.zb_blkid)
788eda14cbcSMatt Macy 		return (1);
789eda14cbcSMatt Macy 
790eda14cbcSMatt Macy 	if (z1 < z2)
791eda14cbcSMatt Macy 		return (-1);
792eda14cbcSMatt Macy 	if (z1 > z2)
793eda14cbcSMatt Macy 		return (1);
794eda14cbcSMatt Macy 
795eda14cbcSMatt Macy 	return (0);
796eda14cbcSMatt Macy }
797eda14cbcSMatt Macy 
798eda14cbcSMatt Macy /*
799eda14cbcSMatt Macy  * ==========================================================================
800eda14cbcSMatt Macy  * Create the various types of I/O (read, write, free, etc)
801eda14cbcSMatt Macy  * ==========================================================================
802eda14cbcSMatt Macy  */
803eda14cbcSMatt Macy static zio_t *
804eda14cbcSMatt Macy zio_create(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
805eda14cbcSMatt Macy     abd_t *data, uint64_t lsize, uint64_t psize, zio_done_func_t *done,
806eda14cbcSMatt Macy     void *private, zio_type_t type, zio_priority_t priority,
807eda14cbcSMatt Macy     enum zio_flag flags, vdev_t *vd, uint64_t offset,
808eda14cbcSMatt Macy     const zbookmark_phys_t *zb, enum zio_stage stage,
809eda14cbcSMatt Macy     enum zio_stage pipeline)
810eda14cbcSMatt Macy {
811eda14cbcSMatt Macy 	zio_t *zio;
812eda14cbcSMatt Macy 
813eda14cbcSMatt Macy 	IMPLY(type != ZIO_TYPE_TRIM, psize <= SPA_MAXBLOCKSIZE);
814eda14cbcSMatt Macy 	ASSERT(P2PHASE(psize, SPA_MINBLOCKSIZE) == 0);
815eda14cbcSMatt Macy 	ASSERT(P2PHASE(offset, SPA_MINBLOCKSIZE) == 0);
816eda14cbcSMatt Macy 
817eda14cbcSMatt Macy 	ASSERT(!vd || spa_config_held(spa, SCL_STATE_ALL, RW_READER));
818eda14cbcSMatt Macy 	ASSERT(!bp || !(flags & ZIO_FLAG_CONFIG_WRITER));
819eda14cbcSMatt Macy 	ASSERT(vd || stage == ZIO_STAGE_OPEN);
820eda14cbcSMatt Macy 
821eda14cbcSMatt Macy 	IMPLY(lsize != psize, (flags & ZIO_FLAG_RAW_COMPRESS) != 0);
822eda14cbcSMatt Macy 
823eda14cbcSMatt Macy 	zio = kmem_cache_alloc(zio_cache, KM_SLEEP);
824eda14cbcSMatt Macy 	bzero(zio, sizeof (zio_t));
825eda14cbcSMatt Macy 
826eda14cbcSMatt Macy 	mutex_init(&zio->io_lock, NULL, MUTEX_NOLOCKDEP, NULL);
827eda14cbcSMatt Macy 	cv_init(&zio->io_cv, NULL, CV_DEFAULT, NULL);
828eda14cbcSMatt Macy 
829eda14cbcSMatt Macy 	list_create(&zio->io_parent_list, sizeof (zio_link_t),
830eda14cbcSMatt Macy 	    offsetof(zio_link_t, zl_parent_node));
831eda14cbcSMatt Macy 	list_create(&zio->io_child_list, sizeof (zio_link_t),
832eda14cbcSMatt Macy 	    offsetof(zio_link_t, zl_child_node));
833eda14cbcSMatt Macy 	metaslab_trace_init(&zio->io_alloc_list);
834eda14cbcSMatt Macy 
835eda14cbcSMatt Macy 	if (vd != NULL)
836eda14cbcSMatt Macy 		zio->io_child_type = ZIO_CHILD_VDEV;
837eda14cbcSMatt Macy 	else if (flags & ZIO_FLAG_GANG_CHILD)
838eda14cbcSMatt Macy 		zio->io_child_type = ZIO_CHILD_GANG;
839eda14cbcSMatt Macy 	else if (flags & ZIO_FLAG_DDT_CHILD)
840eda14cbcSMatt Macy 		zio->io_child_type = ZIO_CHILD_DDT;
841eda14cbcSMatt Macy 	else
842eda14cbcSMatt Macy 		zio->io_child_type = ZIO_CHILD_LOGICAL;
843eda14cbcSMatt Macy 
844eda14cbcSMatt Macy 	if (bp != NULL) {
845eda14cbcSMatt Macy 		zio->io_bp = (blkptr_t *)bp;
846eda14cbcSMatt Macy 		zio->io_bp_copy = *bp;
847eda14cbcSMatt Macy 		zio->io_bp_orig = *bp;
848eda14cbcSMatt Macy 		if (type != ZIO_TYPE_WRITE ||
849eda14cbcSMatt Macy 		    zio->io_child_type == ZIO_CHILD_DDT)
850eda14cbcSMatt Macy 			zio->io_bp = &zio->io_bp_copy;	/* so caller can free */
851eda14cbcSMatt Macy 		if (zio->io_child_type == ZIO_CHILD_LOGICAL)
852eda14cbcSMatt Macy 			zio->io_logical = zio;
853eda14cbcSMatt Macy 		if (zio->io_child_type > ZIO_CHILD_GANG && BP_IS_GANG(bp))
854eda14cbcSMatt Macy 			pipeline |= ZIO_GANG_STAGES;
855eda14cbcSMatt Macy 	}
856eda14cbcSMatt Macy 
857eda14cbcSMatt Macy 	zio->io_spa = spa;
858eda14cbcSMatt Macy 	zio->io_txg = txg;
859eda14cbcSMatt Macy 	zio->io_done = done;
860eda14cbcSMatt Macy 	zio->io_private = private;
861eda14cbcSMatt Macy 	zio->io_type = type;
862eda14cbcSMatt Macy 	zio->io_priority = priority;
863eda14cbcSMatt Macy 	zio->io_vd = vd;
864eda14cbcSMatt Macy 	zio->io_offset = offset;
865eda14cbcSMatt Macy 	zio->io_orig_abd = zio->io_abd = data;
866eda14cbcSMatt Macy 	zio->io_orig_size = zio->io_size = psize;
867eda14cbcSMatt Macy 	zio->io_lsize = lsize;
868eda14cbcSMatt Macy 	zio->io_orig_flags = zio->io_flags = flags;
869eda14cbcSMatt Macy 	zio->io_orig_stage = zio->io_stage = stage;
870eda14cbcSMatt Macy 	zio->io_orig_pipeline = zio->io_pipeline = pipeline;
871eda14cbcSMatt Macy 	zio->io_pipeline_trace = ZIO_STAGE_OPEN;
872eda14cbcSMatt Macy 
873eda14cbcSMatt Macy 	zio->io_state[ZIO_WAIT_READY] = (stage >= ZIO_STAGE_READY);
874eda14cbcSMatt Macy 	zio->io_state[ZIO_WAIT_DONE] = (stage >= ZIO_STAGE_DONE);
875eda14cbcSMatt Macy 
876eda14cbcSMatt Macy 	if (zb != NULL)
877eda14cbcSMatt Macy 		zio->io_bookmark = *zb;
878eda14cbcSMatt Macy 
879eda14cbcSMatt Macy 	if (pio != NULL) {
880eda14cbcSMatt Macy 		zio->io_metaslab_class = pio->io_metaslab_class;
881eda14cbcSMatt Macy 		if (zio->io_logical == NULL)
882eda14cbcSMatt Macy 			zio->io_logical = pio->io_logical;
883eda14cbcSMatt Macy 		if (zio->io_child_type == ZIO_CHILD_GANG)
884eda14cbcSMatt Macy 			zio->io_gang_leader = pio->io_gang_leader;
885eda14cbcSMatt Macy 		zio_add_child(pio, zio);
886eda14cbcSMatt Macy 	}
887eda14cbcSMatt Macy 
888eda14cbcSMatt Macy 	taskq_init_ent(&zio->io_tqent);
889eda14cbcSMatt Macy 
890eda14cbcSMatt Macy 	return (zio);
891eda14cbcSMatt Macy }
892eda14cbcSMatt Macy 
893eda14cbcSMatt Macy static void
894eda14cbcSMatt Macy zio_destroy(zio_t *zio)
895eda14cbcSMatt Macy {
896eda14cbcSMatt Macy 	metaslab_trace_fini(&zio->io_alloc_list);
897eda14cbcSMatt Macy 	list_destroy(&zio->io_parent_list);
898eda14cbcSMatt Macy 	list_destroy(&zio->io_child_list);
899eda14cbcSMatt Macy 	mutex_destroy(&zio->io_lock);
900eda14cbcSMatt Macy 	cv_destroy(&zio->io_cv);
901eda14cbcSMatt Macy 	kmem_cache_free(zio_cache, zio);
902eda14cbcSMatt Macy }
903eda14cbcSMatt Macy 
904eda14cbcSMatt Macy zio_t *
905eda14cbcSMatt Macy zio_null(zio_t *pio, spa_t *spa, vdev_t *vd, zio_done_func_t *done,
906eda14cbcSMatt Macy     void *private, enum zio_flag flags)
907eda14cbcSMatt Macy {
908eda14cbcSMatt Macy 	zio_t *zio;
909eda14cbcSMatt Macy 
910eda14cbcSMatt Macy 	zio = zio_create(pio, spa, 0, NULL, NULL, 0, 0, done, private,
911eda14cbcSMatt Macy 	    ZIO_TYPE_NULL, ZIO_PRIORITY_NOW, flags, vd, 0, NULL,
912eda14cbcSMatt Macy 	    ZIO_STAGE_OPEN, ZIO_INTERLOCK_PIPELINE);
913eda14cbcSMatt Macy 
914eda14cbcSMatt Macy 	return (zio);
915eda14cbcSMatt Macy }
916eda14cbcSMatt Macy 
917eda14cbcSMatt Macy zio_t *
918eda14cbcSMatt Macy zio_root(spa_t *spa, zio_done_func_t *done, void *private, enum zio_flag flags)
919eda14cbcSMatt Macy {
920eda14cbcSMatt Macy 	return (zio_null(NULL, spa, NULL, done, private, flags));
921eda14cbcSMatt Macy }
922eda14cbcSMatt Macy 
923eda14cbcSMatt Macy static int
924eda14cbcSMatt Macy zfs_blkptr_verify_log(spa_t *spa, const blkptr_t *bp,
925eda14cbcSMatt Macy     enum blk_verify_flag blk_verify, const char *fmt, ...)
926eda14cbcSMatt Macy {
927eda14cbcSMatt Macy 	va_list adx;
928eda14cbcSMatt Macy 	char buf[256];
929eda14cbcSMatt Macy 
930eda14cbcSMatt Macy 	va_start(adx, fmt);
931eda14cbcSMatt Macy 	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
932eda14cbcSMatt Macy 	va_end(adx);
933eda14cbcSMatt Macy 
934eda14cbcSMatt Macy 	switch (blk_verify) {
935eda14cbcSMatt Macy 	case BLK_VERIFY_HALT:
936eda14cbcSMatt Macy 		dprintf_bp(bp, "blkptr at %p dprintf_bp():", bp);
937eda14cbcSMatt Macy 		zfs_panic_recover("%s: %s", spa_name(spa), buf);
938eda14cbcSMatt Macy 		break;
939eda14cbcSMatt Macy 	case BLK_VERIFY_LOG:
940eda14cbcSMatt Macy 		zfs_dbgmsg("%s: %s", spa_name(spa), buf);
941eda14cbcSMatt Macy 		break;
942eda14cbcSMatt Macy 	case BLK_VERIFY_ONLY:
943eda14cbcSMatt Macy 		break;
944eda14cbcSMatt Macy 	}
945eda14cbcSMatt Macy 
946eda14cbcSMatt Macy 	return (1);
947eda14cbcSMatt Macy }
948eda14cbcSMatt Macy 
949eda14cbcSMatt Macy /*
950eda14cbcSMatt Macy  * Verify the block pointer fields contain reasonable values.  This means
951eda14cbcSMatt Macy  * it only contains known object types, checksum/compression identifiers,
952eda14cbcSMatt Macy  * block sizes within the maximum allowed limits, valid DVAs, etc.
953eda14cbcSMatt Macy  *
954eda14cbcSMatt Macy  * If everything checks out B_TRUE is returned.  The zfs_blkptr_verify
955eda14cbcSMatt Macy  * argument controls the behavior when an invalid field is detected.
956eda14cbcSMatt Macy  *
957eda14cbcSMatt Macy  * Modes for zfs_blkptr_verify:
958eda14cbcSMatt Macy  *   1) BLK_VERIFY_ONLY (evaluate the block)
959eda14cbcSMatt Macy  *   2) BLK_VERIFY_LOG (evaluate the block and log problems)
960eda14cbcSMatt Macy  *   3) BLK_VERIFY_HALT (call zfs_panic_recover on error)
961eda14cbcSMatt Macy  */
962eda14cbcSMatt Macy boolean_t
963eda14cbcSMatt Macy zfs_blkptr_verify(spa_t *spa, const blkptr_t *bp, boolean_t config_held,
964eda14cbcSMatt Macy     enum blk_verify_flag blk_verify)
965eda14cbcSMatt Macy {
966eda14cbcSMatt Macy 	int errors = 0;
967eda14cbcSMatt Macy 
968eda14cbcSMatt Macy 	if (!DMU_OT_IS_VALID(BP_GET_TYPE(bp))) {
969eda14cbcSMatt Macy 		errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
970eda14cbcSMatt Macy 		    "blkptr at %p has invalid TYPE %llu",
971eda14cbcSMatt Macy 		    bp, (longlong_t)BP_GET_TYPE(bp));
972eda14cbcSMatt Macy 	}
973eda14cbcSMatt Macy 	if (BP_GET_CHECKSUM(bp) >= ZIO_CHECKSUM_FUNCTIONS ||
974eda14cbcSMatt Macy 	    BP_GET_CHECKSUM(bp) <= ZIO_CHECKSUM_ON) {
975eda14cbcSMatt Macy 		errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
976eda14cbcSMatt Macy 		    "blkptr at %p has invalid CHECKSUM %llu",
977eda14cbcSMatt Macy 		    bp, (longlong_t)BP_GET_CHECKSUM(bp));
978eda14cbcSMatt Macy 	}
979eda14cbcSMatt Macy 	if (BP_GET_COMPRESS(bp) >= ZIO_COMPRESS_FUNCTIONS ||
980eda14cbcSMatt Macy 	    BP_GET_COMPRESS(bp) <= ZIO_COMPRESS_ON) {
981eda14cbcSMatt Macy 		errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
982eda14cbcSMatt Macy 		    "blkptr at %p has invalid COMPRESS %llu",
983eda14cbcSMatt Macy 		    bp, (longlong_t)BP_GET_COMPRESS(bp));
984eda14cbcSMatt Macy 	}
985eda14cbcSMatt Macy 	if (BP_GET_LSIZE(bp) > SPA_MAXBLOCKSIZE) {
986eda14cbcSMatt Macy 		errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
987eda14cbcSMatt Macy 		    "blkptr at %p has invalid LSIZE %llu",
988eda14cbcSMatt Macy 		    bp, (longlong_t)BP_GET_LSIZE(bp));
989eda14cbcSMatt Macy 	}
990eda14cbcSMatt Macy 	if (BP_GET_PSIZE(bp) > SPA_MAXBLOCKSIZE) {
991eda14cbcSMatt Macy 		errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
992eda14cbcSMatt Macy 		    "blkptr at %p has invalid PSIZE %llu",
993eda14cbcSMatt Macy 		    bp, (longlong_t)BP_GET_PSIZE(bp));
994eda14cbcSMatt Macy 	}
995eda14cbcSMatt Macy 
996eda14cbcSMatt Macy 	if (BP_IS_EMBEDDED(bp)) {
997eda14cbcSMatt Macy 		if (BPE_GET_ETYPE(bp) >= NUM_BP_EMBEDDED_TYPES) {
998eda14cbcSMatt Macy 			errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
999eda14cbcSMatt Macy 			    "blkptr at %p has invalid ETYPE %llu",
1000eda14cbcSMatt Macy 			    bp, (longlong_t)BPE_GET_ETYPE(bp));
1001eda14cbcSMatt Macy 		}
1002eda14cbcSMatt Macy 	}
1003eda14cbcSMatt Macy 
1004eda14cbcSMatt Macy 	/*
1005eda14cbcSMatt Macy 	 * Do not verify individual DVAs if the config is not trusted. This
1006eda14cbcSMatt Macy 	 * will be done once the zio is executed in vdev_mirror_map_alloc.
1007eda14cbcSMatt Macy 	 */
1008eda14cbcSMatt Macy 	if (!spa->spa_trust_config)
1009*53b70c86SMartin Matuska 		return (errors == 0);
1010eda14cbcSMatt Macy 
1011eda14cbcSMatt Macy 	if (!config_held)
1012eda14cbcSMatt Macy 		spa_config_enter(spa, SCL_VDEV, bp, RW_READER);
1013eda14cbcSMatt Macy 	else
1014eda14cbcSMatt Macy 		ASSERT(spa_config_held(spa, SCL_VDEV, RW_WRITER));
1015eda14cbcSMatt Macy 	/*
1016eda14cbcSMatt Macy 	 * Pool-specific checks.
1017eda14cbcSMatt Macy 	 *
1018eda14cbcSMatt Macy 	 * Note: it would be nice to verify that the blk_birth and
1019eda14cbcSMatt Macy 	 * BP_PHYSICAL_BIRTH() are not too large.  However, spa_freeze()
1020eda14cbcSMatt Macy 	 * allows the birth time of log blocks (and dmu_sync()-ed blocks
1021eda14cbcSMatt Macy 	 * that are in the log) to be arbitrarily large.
1022eda14cbcSMatt Macy 	 */
1023eda14cbcSMatt Macy 	for (int i = 0; i < BP_GET_NDVAS(bp); i++) {
10246db169e9SMartin Matuska 		const dva_t *dva = &bp->blk_dva[i];
10256db169e9SMartin Matuska 		uint64_t vdevid = DVA_GET_VDEV(dva);
1026eda14cbcSMatt Macy 
1027eda14cbcSMatt Macy 		if (vdevid >= spa->spa_root_vdev->vdev_children) {
1028eda14cbcSMatt Macy 			errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1029eda14cbcSMatt Macy 			    "blkptr at %p DVA %u has invalid VDEV %llu",
1030eda14cbcSMatt Macy 			    bp, i, (longlong_t)vdevid);
1031eda14cbcSMatt Macy 			continue;
1032eda14cbcSMatt Macy 		}
1033eda14cbcSMatt Macy 		vdev_t *vd = spa->spa_root_vdev->vdev_child[vdevid];
1034eda14cbcSMatt Macy 		if (vd == NULL) {
1035eda14cbcSMatt Macy 			errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1036eda14cbcSMatt Macy 			    "blkptr at %p DVA %u has invalid VDEV %llu",
1037eda14cbcSMatt Macy 			    bp, i, (longlong_t)vdevid);
1038eda14cbcSMatt Macy 			continue;
1039eda14cbcSMatt Macy 		}
1040eda14cbcSMatt Macy 		if (vd->vdev_ops == &vdev_hole_ops) {
1041eda14cbcSMatt Macy 			errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1042eda14cbcSMatt Macy 			    "blkptr at %p DVA %u has hole VDEV %llu",
1043eda14cbcSMatt Macy 			    bp, i, (longlong_t)vdevid);
1044eda14cbcSMatt Macy 			continue;
1045eda14cbcSMatt Macy 		}
1046eda14cbcSMatt Macy 		if (vd->vdev_ops == &vdev_missing_ops) {
1047eda14cbcSMatt Macy 			/*
1048eda14cbcSMatt Macy 			 * "missing" vdevs are valid during import, but we
1049eda14cbcSMatt Macy 			 * don't have their detailed info (e.g. asize), so
1050eda14cbcSMatt Macy 			 * we can't perform any more checks on them.
1051eda14cbcSMatt Macy 			 */
1052eda14cbcSMatt Macy 			continue;
1053eda14cbcSMatt Macy 		}
10546db169e9SMartin Matuska 		uint64_t offset = DVA_GET_OFFSET(dva);
10556db169e9SMartin Matuska 		uint64_t asize = DVA_GET_ASIZE(dva);
10566db169e9SMartin Matuska 		if (DVA_GET_GANG(dva))
10576db169e9SMartin Matuska 			asize = vdev_gang_header_asize(vd);
1058eda14cbcSMatt Macy 		if (offset + asize > vd->vdev_asize) {
1059eda14cbcSMatt Macy 			errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1060eda14cbcSMatt Macy 			    "blkptr at %p DVA %u has invalid OFFSET %llu",
1061eda14cbcSMatt Macy 			    bp, i, (longlong_t)offset);
1062eda14cbcSMatt Macy 		}
1063eda14cbcSMatt Macy 	}
1064eda14cbcSMatt Macy 	if (errors > 0)
1065eda14cbcSMatt Macy 		dprintf_bp(bp, "blkptr at %p dprintf_bp():", bp);
1066eda14cbcSMatt Macy 	if (!config_held)
1067eda14cbcSMatt Macy 		spa_config_exit(spa, SCL_VDEV, bp);
1068eda14cbcSMatt Macy 
1069eda14cbcSMatt Macy 	return (errors == 0);
1070eda14cbcSMatt Macy }
1071eda14cbcSMatt Macy 
1072eda14cbcSMatt Macy boolean_t
1073eda14cbcSMatt Macy zfs_dva_valid(spa_t *spa, const dva_t *dva, const blkptr_t *bp)
1074eda14cbcSMatt Macy {
1075eda14cbcSMatt Macy 	uint64_t vdevid = DVA_GET_VDEV(dva);
1076eda14cbcSMatt Macy 
1077eda14cbcSMatt Macy 	if (vdevid >= spa->spa_root_vdev->vdev_children)
1078eda14cbcSMatt Macy 		return (B_FALSE);
1079eda14cbcSMatt Macy 
1080eda14cbcSMatt Macy 	vdev_t *vd = spa->spa_root_vdev->vdev_child[vdevid];
1081eda14cbcSMatt Macy 	if (vd == NULL)
1082eda14cbcSMatt Macy 		return (B_FALSE);
1083eda14cbcSMatt Macy 
1084eda14cbcSMatt Macy 	if (vd->vdev_ops == &vdev_hole_ops)
1085eda14cbcSMatt Macy 		return (B_FALSE);
1086eda14cbcSMatt Macy 
1087eda14cbcSMatt Macy 	if (vd->vdev_ops == &vdev_missing_ops) {
1088eda14cbcSMatt Macy 		return (B_FALSE);
1089eda14cbcSMatt Macy 	}
1090eda14cbcSMatt Macy 
1091eda14cbcSMatt Macy 	uint64_t offset = DVA_GET_OFFSET(dva);
1092eda14cbcSMatt Macy 	uint64_t asize = DVA_GET_ASIZE(dva);
1093eda14cbcSMatt Macy 
10946db169e9SMartin Matuska 	if (DVA_GET_GANG(dva))
10956db169e9SMartin Matuska 		asize = vdev_gang_header_asize(vd);
1096eda14cbcSMatt Macy 	if (offset + asize > vd->vdev_asize)
1097eda14cbcSMatt Macy 		return (B_FALSE);
1098eda14cbcSMatt Macy 
1099eda14cbcSMatt Macy 	return (B_TRUE);
1100eda14cbcSMatt Macy }
1101eda14cbcSMatt Macy 
1102eda14cbcSMatt Macy zio_t *
1103eda14cbcSMatt Macy zio_read(zio_t *pio, spa_t *spa, const blkptr_t *bp,
1104eda14cbcSMatt Macy     abd_t *data, uint64_t size, zio_done_func_t *done, void *private,
1105eda14cbcSMatt Macy     zio_priority_t priority, enum zio_flag flags, const zbookmark_phys_t *zb)
1106eda14cbcSMatt Macy {
1107eda14cbcSMatt Macy 	zio_t *zio;
1108eda14cbcSMatt Macy 
1109eda14cbcSMatt Macy 	zio = zio_create(pio, spa, BP_PHYSICAL_BIRTH(bp), bp,
1110eda14cbcSMatt Macy 	    data, size, size, done, private,
1111eda14cbcSMatt Macy 	    ZIO_TYPE_READ, priority, flags, NULL, 0, zb,
1112eda14cbcSMatt Macy 	    ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ?
1113eda14cbcSMatt Macy 	    ZIO_DDT_CHILD_READ_PIPELINE : ZIO_READ_PIPELINE);
1114eda14cbcSMatt Macy 
1115eda14cbcSMatt Macy 	return (zio);
1116eda14cbcSMatt Macy }
1117eda14cbcSMatt Macy 
1118eda14cbcSMatt Macy zio_t *
1119eda14cbcSMatt Macy zio_write(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
1120eda14cbcSMatt Macy     abd_t *data, uint64_t lsize, uint64_t psize, const zio_prop_t *zp,
1121eda14cbcSMatt Macy     zio_done_func_t *ready, zio_done_func_t *children_ready,
1122eda14cbcSMatt Macy     zio_done_func_t *physdone, zio_done_func_t *done,
1123eda14cbcSMatt Macy     void *private, zio_priority_t priority, enum zio_flag flags,
1124eda14cbcSMatt Macy     const zbookmark_phys_t *zb)
1125eda14cbcSMatt Macy {
1126eda14cbcSMatt Macy 	zio_t *zio;
1127eda14cbcSMatt Macy 
1128eda14cbcSMatt Macy 	ASSERT(zp->zp_checksum >= ZIO_CHECKSUM_OFF &&
1129eda14cbcSMatt Macy 	    zp->zp_checksum < ZIO_CHECKSUM_FUNCTIONS &&
1130eda14cbcSMatt Macy 	    zp->zp_compress >= ZIO_COMPRESS_OFF &&
1131eda14cbcSMatt Macy 	    zp->zp_compress < ZIO_COMPRESS_FUNCTIONS &&
1132eda14cbcSMatt Macy 	    DMU_OT_IS_VALID(zp->zp_type) &&
1133eda14cbcSMatt Macy 	    zp->zp_level < 32 &&
1134eda14cbcSMatt Macy 	    zp->zp_copies > 0 &&
1135eda14cbcSMatt Macy 	    zp->zp_copies <= spa_max_replication(spa));
1136eda14cbcSMatt Macy 
1137eda14cbcSMatt Macy 	zio = zio_create(pio, spa, txg, bp, data, lsize, psize, done, private,
1138eda14cbcSMatt Macy 	    ZIO_TYPE_WRITE, priority, flags, NULL, 0, zb,
1139eda14cbcSMatt Macy 	    ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ?
1140eda14cbcSMatt Macy 	    ZIO_DDT_CHILD_WRITE_PIPELINE : ZIO_WRITE_PIPELINE);
1141eda14cbcSMatt Macy 
1142eda14cbcSMatt Macy 	zio->io_ready = ready;
1143eda14cbcSMatt Macy 	zio->io_children_ready = children_ready;
1144eda14cbcSMatt Macy 	zio->io_physdone = physdone;
1145eda14cbcSMatt Macy 	zio->io_prop = *zp;
1146eda14cbcSMatt Macy 
1147eda14cbcSMatt Macy 	/*
1148eda14cbcSMatt Macy 	 * Data can be NULL if we are going to call zio_write_override() to
1149eda14cbcSMatt Macy 	 * provide the already-allocated BP.  But we may need the data to
1150eda14cbcSMatt Macy 	 * verify a dedup hit (if requested).  In this case, don't try to
1151eda14cbcSMatt Macy 	 * dedup (just take the already-allocated BP verbatim). Encrypted
1152eda14cbcSMatt Macy 	 * dedup blocks need data as well so we also disable dedup in this
1153eda14cbcSMatt Macy 	 * case.
1154eda14cbcSMatt Macy 	 */
1155eda14cbcSMatt Macy 	if (data == NULL &&
1156eda14cbcSMatt Macy 	    (zio->io_prop.zp_dedup_verify || zio->io_prop.zp_encrypt)) {
1157eda14cbcSMatt Macy 		zio->io_prop.zp_dedup = zio->io_prop.zp_dedup_verify = B_FALSE;
1158eda14cbcSMatt Macy 	}
1159eda14cbcSMatt Macy 
1160eda14cbcSMatt Macy 	return (zio);
1161eda14cbcSMatt Macy }
1162eda14cbcSMatt Macy 
1163eda14cbcSMatt Macy zio_t *
1164eda14cbcSMatt Macy zio_rewrite(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, abd_t *data,
1165eda14cbcSMatt Macy     uint64_t size, zio_done_func_t *done, void *private,
1166eda14cbcSMatt Macy     zio_priority_t priority, enum zio_flag flags, zbookmark_phys_t *zb)
1167eda14cbcSMatt Macy {
1168eda14cbcSMatt Macy 	zio_t *zio;
1169eda14cbcSMatt Macy 
1170eda14cbcSMatt Macy 	zio = zio_create(pio, spa, txg, bp, data, size, size, done, private,
1171eda14cbcSMatt Macy 	    ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_IO_REWRITE, NULL, 0, zb,
1172eda14cbcSMatt Macy 	    ZIO_STAGE_OPEN, ZIO_REWRITE_PIPELINE);
1173eda14cbcSMatt Macy 
1174eda14cbcSMatt Macy 	return (zio);
1175eda14cbcSMatt Macy }
1176eda14cbcSMatt Macy 
1177eda14cbcSMatt Macy void
1178eda14cbcSMatt Macy zio_write_override(zio_t *zio, blkptr_t *bp, int copies, boolean_t nopwrite)
1179eda14cbcSMatt Macy {
1180eda14cbcSMatt Macy 	ASSERT(zio->io_type == ZIO_TYPE_WRITE);
1181eda14cbcSMatt Macy 	ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1182eda14cbcSMatt Macy 	ASSERT(zio->io_stage == ZIO_STAGE_OPEN);
1183eda14cbcSMatt Macy 	ASSERT(zio->io_txg == spa_syncing_txg(zio->io_spa));
1184eda14cbcSMatt Macy 
1185eda14cbcSMatt Macy 	/*
1186eda14cbcSMatt Macy 	 * We must reset the io_prop to match the values that existed
1187eda14cbcSMatt Macy 	 * when the bp was first written by dmu_sync() keeping in mind
1188eda14cbcSMatt Macy 	 * that nopwrite and dedup are mutually exclusive.
1189eda14cbcSMatt Macy 	 */
1190eda14cbcSMatt Macy 	zio->io_prop.zp_dedup = nopwrite ? B_FALSE : zio->io_prop.zp_dedup;
1191eda14cbcSMatt Macy 	zio->io_prop.zp_nopwrite = nopwrite;
1192eda14cbcSMatt Macy 	zio->io_prop.zp_copies = copies;
1193eda14cbcSMatt Macy 	zio->io_bp_override = bp;
1194eda14cbcSMatt Macy }
1195eda14cbcSMatt Macy 
1196eda14cbcSMatt Macy void
1197eda14cbcSMatt Macy zio_free(spa_t *spa, uint64_t txg, const blkptr_t *bp)
1198eda14cbcSMatt Macy {
1199eda14cbcSMatt Macy 
1200eda14cbcSMatt Macy 	(void) zfs_blkptr_verify(spa, bp, B_FALSE, BLK_VERIFY_HALT);
1201eda14cbcSMatt Macy 
1202eda14cbcSMatt Macy 	/*
1203eda14cbcSMatt Macy 	 * The check for EMBEDDED is a performance optimization.  We
1204eda14cbcSMatt Macy 	 * process the free here (by ignoring it) rather than
1205eda14cbcSMatt Macy 	 * putting it on the list and then processing it in zio_free_sync().
1206eda14cbcSMatt Macy 	 */
1207eda14cbcSMatt Macy 	if (BP_IS_EMBEDDED(bp))
1208eda14cbcSMatt Macy 		return;
1209eda14cbcSMatt Macy 	metaslab_check_free(spa, bp);
1210eda14cbcSMatt Macy 
1211eda14cbcSMatt Macy 	/*
1212eda14cbcSMatt Macy 	 * Frees that are for the currently-syncing txg, are not going to be
1213eda14cbcSMatt Macy 	 * deferred, and which will not need to do a read (i.e. not GANG or
1214eda14cbcSMatt Macy 	 * DEDUP), can be processed immediately.  Otherwise, put them on the
1215eda14cbcSMatt Macy 	 * in-memory list for later processing.
1216eda14cbcSMatt Macy 	 *
1217eda14cbcSMatt Macy 	 * Note that we only defer frees after zfs_sync_pass_deferred_free
1218eda14cbcSMatt Macy 	 * when the log space map feature is disabled. [see relevant comment
1219eda14cbcSMatt Macy 	 * in spa_sync_iterate_to_convergence()]
1220eda14cbcSMatt Macy 	 */
1221eda14cbcSMatt Macy 	if (BP_IS_GANG(bp) ||
1222eda14cbcSMatt Macy 	    BP_GET_DEDUP(bp) ||
1223eda14cbcSMatt Macy 	    txg != spa->spa_syncing_txg ||
1224eda14cbcSMatt Macy 	    (spa_sync_pass(spa) >= zfs_sync_pass_deferred_free &&
1225eda14cbcSMatt Macy 	    !spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP))) {
1226eda14cbcSMatt Macy 		bplist_append(&spa->spa_free_bplist[txg & TXG_MASK], bp);
1227eda14cbcSMatt Macy 	} else {
1228eda14cbcSMatt Macy 		VERIFY3P(zio_free_sync(NULL, spa, txg, bp, 0), ==, NULL);
1229eda14cbcSMatt Macy 	}
1230eda14cbcSMatt Macy }
1231eda14cbcSMatt Macy 
1232eda14cbcSMatt Macy /*
1233eda14cbcSMatt Macy  * To improve performance, this function may return NULL if we were able
1234eda14cbcSMatt Macy  * to do the free immediately.  This avoids the cost of creating a zio
1235eda14cbcSMatt Macy  * (and linking it to the parent, etc).
1236eda14cbcSMatt Macy  */
1237eda14cbcSMatt Macy zio_t *
1238eda14cbcSMatt Macy zio_free_sync(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
1239eda14cbcSMatt Macy     enum zio_flag flags)
1240eda14cbcSMatt Macy {
1241eda14cbcSMatt Macy 	ASSERT(!BP_IS_HOLE(bp));
1242eda14cbcSMatt Macy 	ASSERT(spa_syncing_txg(spa) == txg);
1243eda14cbcSMatt Macy 
1244eda14cbcSMatt Macy 	if (BP_IS_EMBEDDED(bp))
1245eda14cbcSMatt Macy 		return (NULL);
1246eda14cbcSMatt Macy 
1247eda14cbcSMatt Macy 	metaslab_check_free(spa, bp);
1248eda14cbcSMatt Macy 	arc_freed(spa, bp);
1249eda14cbcSMatt Macy 	dsl_scan_freed(spa, bp);
1250eda14cbcSMatt Macy 
1251eda14cbcSMatt Macy 	if (BP_IS_GANG(bp) || BP_GET_DEDUP(bp)) {
1252eda14cbcSMatt Macy 		/*
1253eda14cbcSMatt Macy 		 * GANG and DEDUP blocks can induce a read (for the gang block
1254eda14cbcSMatt Macy 		 * header, or the DDT), so issue them asynchronously so that
1255eda14cbcSMatt Macy 		 * this thread is not tied up.
1256eda14cbcSMatt Macy 		 */
1257eda14cbcSMatt Macy 		enum zio_stage stage =
1258eda14cbcSMatt Macy 		    ZIO_FREE_PIPELINE | ZIO_STAGE_ISSUE_ASYNC;
1259eda14cbcSMatt Macy 
1260eda14cbcSMatt Macy 		return (zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp),
1261eda14cbcSMatt Macy 		    BP_GET_PSIZE(bp), NULL, NULL,
1262eda14cbcSMatt Macy 		    ZIO_TYPE_FREE, ZIO_PRIORITY_NOW,
1263eda14cbcSMatt Macy 		    flags, NULL, 0, NULL, ZIO_STAGE_OPEN, stage));
1264eda14cbcSMatt Macy 	} else {
1265eda14cbcSMatt Macy 		metaslab_free(spa, bp, txg, B_FALSE);
1266eda14cbcSMatt Macy 		return (NULL);
1267eda14cbcSMatt Macy 	}
1268eda14cbcSMatt Macy }
1269eda14cbcSMatt Macy 
1270eda14cbcSMatt Macy zio_t *
1271eda14cbcSMatt Macy zio_claim(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
1272eda14cbcSMatt Macy     zio_done_func_t *done, void *private, enum zio_flag flags)
1273eda14cbcSMatt Macy {
1274eda14cbcSMatt Macy 	zio_t *zio;
1275eda14cbcSMatt Macy 
1276eda14cbcSMatt Macy 	(void) zfs_blkptr_verify(spa, bp, flags & ZIO_FLAG_CONFIG_WRITER,
1277eda14cbcSMatt Macy 	    BLK_VERIFY_HALT);
1278eda14cbcSMatt Macy 
1279eda14cbcSMatt Macy 	if (BP_IS_EMBEDDED(bp))
1280eda14cbcSMatt Macy 		return (zio_null(pio, spa, NULL, NULL, NULL, 0));
1281eda14cbcSMatt Macy 
1282eda14cbcSMatt Macy 	/*
1283eda14cbcSMatt Macy 	 * A claim is an allocation of a specific block.  Claims are needed
1284eda14cbcSMatt Macy 	 * to support immediate writes in the intent log.  The issue is that
1285eda14cbcSMatt Macy 	 * immediate writes contain committed data, but in a txg that was
1286eda14cbcSMatt Macy 	 * *not* committed.  Upon opening the pool after an unclean shutdown,
1287eda14cbcSMatt Macy 	 * the intent log claims all blocks that contain immediate write data
1288eda14cbcSMatt Macy 	 * so that the SPA knows they're in use.
1289eda14cbcSMatt Macy 	 *
1290eda14cbcSMatt Macy 	 * All claims *must* be resolved in the first txg -- before the SPA
1291eda14cbcSMatt Macy 	 * starts allocating blocks -- so that nothing is allocated twice.
1292eda14cbcSMatt Macy 	 * If txg == 0 we just verify that the block is claimable.
1293eda14cbcSMatt Macy 	 */
1294eda14cbcSMatt Macy 	ASSERT3U(spa->spa_uberblock.ub_rootbp.blk_birth, <,
1295eda14cbcSMatt Macy 	    spa_min_claim_txg(spa));
1296eda14cbcSMatt Macy 	ASSERT(txg == spa_min_claim_txg(spa) || txg == 0);
12977877fdebSMatt Macy 	ASSERT(!BP_GET_DEDUP(bp) || !spa_writeable(spa));	/* zdb(8) */
1298eda14cbcSMatt Macy 
1299eda14cbcSMatt Macy 	zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp),
1300eda14cbcSMatt Macy 	    BP_GET_PSIZE(bp), done, private, ZIO_TYPE_CLAIM, ZIO_PRIORITY_NOW,
1301eda14cbcSMatt Macy 	    flags, NULL, 0, NULL, ZIO_STAGE_OPEN, ZIO_CLAIM_PIPELINE);
1302eda14cbcSMatt Macy 	ASSERT0(zio->io_queued_timestamp);
1303eda14cbcSMatt Macy 
1304eda14cbcSMatt Macy 	return (zio);
1305eda14cbcSMatt Macy }
1306eda14cbcSMatt Macy 
1307eda14cbcSMatt Macy zio_t *
1308eda14cbcSMatt Macy zio_ioctl(zio_t *pio, spa_t *spa, vdev_t *vd, int cmd,
1309eda14cbcSMatt Macy     zio_done_func_t *done, void *private, enum zio_flag flags)
1310eda14cbcSMatt Macy {
1311eda14cbcSMatt Macy 	zio_t *zio;
1312eda14cbcSMatt Macy 	int c;
1313eda14cbcSMatt Macy 
1314eda14cbcSMatt Macy 	if (vd->vdev_children == 0) {
1315eda14cbcSMatt Macy 		zio = zio_create(pio, spa, 0, NULL, NULL, 0, 0, done, private,
1316eda14cbcSMatt Macy 		    ZIO_TYPE_IOCTL, ZIO_PRIORITY_NOW, flags, vd, 0, NULL,
1317eda14cbcSMatt Macy 		    ZIO_STAGE_OPEN, ZIO_IOCTL_PIPELINE);
1318eda14cbcSMatt Macy 
1319eda14cbcSMatt Macy 		zio->io_cmd = cmd;
1320eda14cbcSMatt Macy 	} else {
1321eda14cbcSMatt Macy 		zio = zio_null(pio, spa, NULL, NULL, NULL, flags);
1322eda14cbcSMatt Macy 
1323eda14cbcSMatt Macy 		for (c = 0; c < vd->vdev_children; c++)
1324eda14cbcSMatt Macy 			zio_nowait(zio_ioctl(zio, spa, vd->vdev_child[c], cmd,
1325eda14cbcSMatt Macy 			    done, private, flags));
1326eda14cbcSMatt Macy 	}
1327eda14cbcSMatt Macy 
1328eda14cbcSMatt Macy 	return (zio);
1329eda14cbcSMatt Macy }
1330eda14cbcSMatt Macy 
1331eda14cbcSMatt Macy zio_t *
1332eda14cbcSMatt Macy zio_trim(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
1333eda14cbcSMatt Macy     zio_done_func_t *done, void *private, zio_priority_t priority,
1334eda14cbcSMatt Macy     enum zio_flag flags, enum trim_flag trim_flags)
1335eda14cbcSMatt Macy {
1336eda14cbcSMatt Macy 	zio_t *zio;
1337eda14cbcSMatt Macy 
1338eda14cbcSMatt Macy 	ASSERT0(vd->vdev_children);
1339eda14cbcSMatt Macy 	ASSERT0(P2PHASE(offset, 1ULL << vd->vdev_ashift));
1340eda14cbcSMatt Macy 	ASSERT0(P2PHASE(size, 1ULL << vd->vdev_ashift));
1341eda14cbcSMatt Macy 	ASSERT3U(size, !=, 0);
1342eda14cbcSMatt Macy 
1343eda14cbcSMatt Macy 	zio = zio_create(pio, vd->vdev_spa, 0, NULL, NULL, size, size, done,
1344eda14cbcSMatt Macy 	    private, ZIO_TYPE_TRIM, priority, flags | ZIO_FLAG_PHYSICAL,
1345eda14cbcSMatt Macy 	    vd, offset, NULL, ZIO_STAGE_OPEN, ZIO_TRIM_PIPELINE);
1346eda14cbcSMatt Macy 	zio->io_trim_flags = trim_flags;
1347eda14cbcSMatt Macy 
1348eda14cbcSMatt Macy 	return (zio);
1349eda14cbcSMatt Macy }
1350eda14cbcSMatt Macy 
1351eda14cbcSMatt Macy zio_t *
1352eda14cbcSMatt Macy zio_read_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
1353eda14cbcSMatt Macy     abd_t *data, int checksum, zio_done_func_t *done, void *private,
1354eda14cbcSMatt Macy     zio_priority_t priority, enum zio_flag flags, boolean_t labels)
1355eda14cbcSMatt Macy {
1356eda14cbcSMatt Macy 	zio_t *zio;
1357eda14cbcSMatt Macy 
1358eda14cbcSMatt Macy 	ASSERT(vd->vdev_children == 0);
1359eda14cbcSMatt Macy 	ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE ||
1360eda14cbcSMatt Macy 	    offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
1361eda14cbcSMatt Macy 	ASSERT3U(offset + size, <=, vd->vdev_psize);
1362eda14cbcSMatt Macy 
1363eda14cbcSMatt Macy 	zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, size, done,
1364eda14cbcSMatt Macy 	    private, ZIO_TYPE_READ, priority, flags | ZIO_FLAG_PHYSICAL, vd,
1365eda14cbcSMatt Macy 	    offset, NULL, ZIO_STAGE_OPEN, ZIO_READ_PHYS_PIPELINE);
1366eda14cbcSMatt Macy 
1367eda14cbcSMatt Macy 	zio->io_prop.zp_checksum = checksum;
1368eda14cbcSMatt Macy 
1369eda14cbcSMatt Macy 	return (zio);
1370eda14cbcSMatt Macy }
1371eda14cbcSMatt Macy 
1372eda14cbcSMatt Macy zio_t *
1373eda14cbcSMatt Macy zio_write_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
1374eda14cbcSMatt Macy     abd_t *data, int checksum, zio_done_func_t *done, void *private,
1375eda14cbcSMatt Macy     zio_priority_t priority, enum zio_flag flags, boolean_t labels)
1376eda14cbcSMatt Macy {
1377eda14cbcSMatt Macy 	zio_t *zio;
1378eda14cbcSMatt Macy 
1379eda14cbcSMatt Macy 	ASSERT(vd->vdev_children == 0);
1380eda14cbcSMatt Macy 	ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE ||
1381eda14cbcSMatt Macy 	    offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
1382eda14cbcSMatt Macy 	ASSERT3U(offset + size, <=, vd->vdev_psize);
1383eda14cbcSMatt Macy 
1384eda14cbcSMatt Macy 	zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, size, done,
1385eda14cbcSMatt Macy 	    private, ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_PHYSICAL, vd,
1386eda14cbcSMatt Macy 	    offset, NULL, ZIO_STAGE_OPEN, ZIO_WRITE_PHYS_PIPELINE);
1387eda14cbcSMatt Macy 
1388eda14cbcSMatt Macy 	zio->io_prop.zp_checksum = checksum;
1389eda14cbcSMatt Macy 
1390eda14cbcSMatt Macy 	if (zio_checksum_table[checksum].ci_flags & ZCHECKSUM_FLAG_EMBEDDED) {
1391eda14cbcSMatt Macy 		/*
1392eda14cbcSMatt Macy 		 * zec checksums are necessarily destructive -- they modify
1393eda14cbcSMatt Macy 		 * the end of the write buffer to hold the verifier/checksum.
1394eda14cbcSMatt Macy 		 * Therefore, we must make a local copy in case the data is
1395eda14cbcSMatt Macy 		 * being written to multiple places in parallel.
1396eda14cbcSMatt Macy 		 */
1397eda14cbcSMatt Macy 		abd_t *wbuf = abd_alloc_sametype(data, size);
1398eda14cbcSMatt Macy 		abd_copy(wbuf, data, size);
1399eda14cbcSMatt Macy 
1400eda14cbcSMatt Macy 		zio_push_transform(zio, wbuf, size, size, NULL);
1401eda14cbcSMatt Macy 	}
1402eda14cbcSMatt Macy 
1403eda14cbcSMatt Macy 	return (zio);
1404eda14cbcSMatt Macy }
1405eda14cbcSMatt Macy 
1406eda14cbcSMatt Macy /*
1407eda14cbcSMatt Macy  * Create a child I/O to do some work for us.
1408eda14cbcSMatt Macy  */
1409eda14cbcSMatt Macy zio_t *
1410eda14cbcSMatt Macy zio_vdev_child_io(zio_t *pio, blkptr_t *bp, vdev_t *vd, uint64_t offset,
1411eda14cbcSMatt Macy     abd_t *data, uint64_t size, int type, zio_priority_t priority,
1412eda14cbcSMatt Macy     enum zio_flag flags, zio_done_func_t *done, void *private)
1413eda14cbcSMatt Macy {
1414eda14cbcSMatt Macy 	enum zio_stage pipeline = ZIO_VDEV_CHILD_PIPELINE;
1415eda14cbcSMatt Macy 	zio_t *zio;
1416eda14cbcSMatt Macy 
1417eda14cbcSMatt Macy 	/*
1418eda14cbcSMatt Macy 	 * vdev child I/Os do not propagate their error to the parent.
1419eda14cbcSMatt Macy 	 * Therefore, for correct operation the caller *must* check for
1420eda14cbcSMatt Macy 	 * and handle the error in the child i/o's done callback.
1421eda14cbcSMatt Macy 	 * The only exceptions are i/os that we don't care about
1422eda14cbcSMatt Macy 	 * (OPTIONAL or REPAIR).
1423eda14cbcSMatt Macy 	 */
1424eda14cbcSMatt Macy 	ASSERT((flags & ZIO_FLAG_OPTIONAL) || (flags & ZIO_FLAG_IO_REPAIR) ||
1425eda14cbcSMatt Macy 	    done != NULL);
1426eda14cbcSMatt Macy 
1427eda14cbcSMatt Macy 	if (type == ZIO_TYPE_READ && bp != NULL) {
1428eda14cbcSMatt Macy 		/*
1429eda14cbcSMatt Macy 		 * If we have the bp, then the child should perform the
1430eda14cbcSMatt Macy 		 * checksum and the parent need not.  This pushes error
1431eda14cbcSMatt Macy 		 * detection as close to the leaves as possible and
1432eda14cbcSMatt Macy 		 * eliminates redundant checksums in the interior nodes.
1433eda14cbcSMatt Macy 		 */
1434eda14cbcSMatt Macy 		pipeline |= ZIO_STAGE_CHECKSUM_VERIFY;
1435eda14cbcSMatt Macy 		pio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY;
1436eda14cbcSMatt Macy 	}
1437eda14cbcSMatt Macy 
1438eda14cbcSMatt Macy 	if (vd->vdev_ops->vdev_op_leaf) {
1439eda14cbcSMatt Macy 		ASSERT0(vd->vdev_children);
1440eda14cbcSMatt Macy 		offset += VDEV_LABEL_START_SIZE;
1441eda14cbcSMatt Macy 	}
1442eda14cbcSMatt Macy 
1443eda14cbcSMatt Macy 	flags |= ZIO_VDEV_CHILD_FLAGS(pio);
1444eda14cbcSMatt Macy 
1445eda14cbcSMatt Macy 	/*
1446eda14cbcSMatt Macy 	 * If we've decided to do a repair, the write is not speculative --
1447eda14cbcSMatt Macy 	 * even if the original read was.
1448eda14cbcSMatt Macy 	 */
1449eda14cbcSMatt Macy 	if (flags & ZIO_FLAG_IO_REPAIR)
1450eda14cbcSMatt Macy 		flags &= ~ZIO_FLAG_SPECULATIVE;
1451eda14cbcSMatt Macy 
1452eda14cbcSMatt Macy 	/*
1453eda14cbcSMatt Macy 	 * If we're creating a child I/O that is not associated with a
1454eda14cbcSMatt Macy 	 * top-level vdev, then the child zio is not an allocating I/O.
1455eda14cbcSMatt Macy 	 * If this is a retried I/O then we ignore it since we will
1456eda14cbcSMatt Macy 	 * have already processed the original allocating I/O.
1457eda14cbcSMatt Macy 	 */
1458eda14cbcSMatt Macy 	if (flags & ZIO_FLAG_IO_ALLOCATING &&
1459eda14cbcSMatt Macy 	    (vd != vd->vdev_top || (flags & ZIO_FLAG_IO_RETRY))) {
1460eda14cbcSMatt Macy 		ASSERT(pio->io_metaslab_class != NULL);
1461eda14cbcSMatt Macy 		ASSERT(pio->io_metaslab_class->mc_alloc_throttle_enabled);
1462eda14cbcSMatt Macy 		ASSERT(type == ZIO_TYPE_WRITE);
1463eda14cbcSMatt Macy 		ASSERT(priority == ZIO_PRIORITY_ASYNC_WRITE);
1464eda14cbcSMatt Macy 		ASSERT(!(flags & ZIO_FLAG_IO_REPAIR));
1465eda14cbcSMatt Macy 		ASSERT(!(pio->io_flags & ZIO_FLAG_IO_REWRITE) ||
1466eda14cbcSMatt Macy 		    pio->io_child_type == ZIO_CHILD_GANG);
1467eda14cbcSMatt Macy 
1468eda14cbcSMatt Macy 		flags &= ~ZIO_FLAG_IO_ALLOCATING;
1469eda14cbcSMatt Macy 	}
1470eda14cbcSMatt Macy 
1471eda14cbcSMatt Macy 
1472eda14cbcSMatt Macy 	zio = zio_create(pio, pio->io_spa, pio->io_txg, bp, data, size, size,
1473eda14cbcSMatt Macy 	    done, private, type, priority, flags, vd, offset, &pio->io_bookmark,
1474eda14cbcSMatt Macy 	    ZIO_STAGE_VDEV_IO_START >> 1, pipeline);
1475eda14cbcSMatt Macy 	ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_VDEV);
1476eda14cbcSMatt Macy 
1477eda14cbcSMatt Macy 	zio->io_physdone = pio->io_physdone;
1478eda14cbcSMatt Macy 	if (vd->vdev_ops->vdev_op_leaf && zio->io_logical != NULL)
1479eda14cbcSMatt Macy 		zio->io_logical->io_phys_children++;
1480eda14cbcSMatt Macy 
1481eda14cbcSMatt Macy 	return (zio);
1482eda14cbcSMatt Macy }
1483eda14cbcSMatt Macy 
1484eda14cbcSMatt Macy zio_t *
1485eda14cbcSMatt Macy zio_vdev_delegated_io(vdev_t *vd, uint64_t offset, abd_t *data, uint64_t size,
1486eda14cbcSMatt Macy     zio_type_t type, zio_priority_t priority, enum zio_flag flags,
1487eda14cbcSMatt Macy     zio_done_func_t *done, void *private)
1488eda14cbcSMatt Macy {
1489eda14cbcSMatt Macy 	zio_t *zio;
1490eda14cbcSMatt Macy 
1491eda14cbcSMatt Macy 	ASSERT(vd->vdev_ops->vdev_op_leaf);
1492eda14cbcSMatt Macy 
1493eda14cbcSMatt Macy 	zio = zio_create(NULL, vd->vdev_spa, 0, NULL,
1494eda14cbcSMatt Macy 	    data, size, size, done, private, type, priority,
1495eda14cbcSMatt Macy 	    flags | ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY | ZIO_FLAG_DELEGATED,
1496eda14cbcSMatt Macy 	    vd, offset, NULL,
1497eda14cbcSMatt Macy 	    ZIO_STAGE_VDEV_IO_START >> 1, ZIO_VDEV_CHILD_PIPELINE);
1498eda14cbcSMatt Macy 
1499eda14cbcSMatt Macy 	return (zio);
1500eda14cbcSMatt Macy }
1501eda14cbcSMatt Macy 
1502eda14cbcSMatt Macy void
1503eda14cbcSMatt Macy zio_flush(zio_t *zio, vdev_t *vd)
1504eda14cbcSMatt Macy {
1505eda14cbcSMatt Macy 	zio_nowait(zio_ioctl(zio, zio->io_spa, vd, DKIOCFLUSHWRITECACHE,
1506eda14cbcSMatt Macy 	    NULL, NULL,
1507eda14cbcSMatt Macy 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY));
1508eda14cbcSMatt Macy }
1509eda14cbcSMatt Macy 
1510eda14cbcSMatt Macy void
1511eda14cbcSMatt Macy zio_shrink(zio_t *zio, uint64_t size)
1512eda14cbcSMatt Macy {
1513eda14cbcSMatt Macy 	ASSERT3P(zio->io_executor, ==, NULL);
1514eda14cbcSMatt Macy 	ASSERT3U(zio->io_orig_size, ==, zio->io_size);
1515eda14cbcSMatt Macy 	ASSERT3U(size, <=, zio->io_size);
1516eda14cbcSMatt Macy 
1517eda14cbcSMatt Macy 	/*
1518eda14cbcSMatt Macy 	 * We don't shrink for raidz because of problems with the
1519eda14cbcSMatt Macy 	 * reconstruction when reading back less than the block size.
1520eda14cbcSMatt Macy 	 * Note, BP_IS_RAIDZ() assumes no compression.
1521eda14cbcSMatt Macy 	 */
1522eda14cbcSMatt Macy 	ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF);
1523eda14cbcSMatt Macy 	if (!BP_IS_RAIDZ(zio->io_bp)) {
1524eda14cbcSMatt Macy 		/* we are not doing a raw write */
1525eda14cbcSMatt Macy 		ASSERT3U(zio->io_size, ==, zio->io_lsize);
1526eda14cbcSMatt Macy 		zio->io_orig_size = zio->io_size = zio->io_lsize = size;
1527eda14cbcSMatt Macy 	}
1528eda14cbcSMatt Macy }
1529eda14cbcSMatt Macy 
1530eda14cbcSMatt Macy /*
1531eda14cbcSMatt Macy  * ==========================================================================
1532eda14cbcSMatt Macy  * Prepare to read and write logical blocks
1533eda14cbcSMatt Macy  * ==========================================================================
1534eda14cbcSMatt Macy  */
1535eda14cbcSMatt Macy 
1536eda14cbcSMatt Macy static zio_t *
1537eda14cbcSMatt Macy zio_read_bp_init(zio_t *zio)
1538eda14cbcSMatt Macy {
1539eda14cbcSMatt Macy 	blkptr_t *bp = zio->io_bp;
1540eda14cbcSMatt Macy 	uint64_t psize =
1541eda14cbcSMatt Macy 	    BP_IS_EMBEDDED(bp) ? BPE_GET_PSIZE(bp) : BP_GET_PSIZE(bp);
1542eda14cbcSMatt Macy 
1543eda14cbcSMatt Macy 	ASSERT3P(zio->io_bp, ==, &zio->io_bp_copy);
1544eda14cbcSMatt Macy 
1545eda14cbcSMatt Macy 	if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF &&
1546eda14cbcSMatt Macy 	    zio->io_child_type == ZIO_CHILD_LOGICAL &&
1547eda14cbcSMatt Macy 	    !(zio->io_flags & ZIO_FLAG_RAW_COMPRESS)) {
1548eda14cbcSMatt Macy 		zio_push_transform(zio, abd_alloc_sametype(zio->io_abd, psize),
1549eda14cbcSMatt Macy 		    psize, psize, zio_decompress);
1550eda14cbcSMatt Macy 	}
1551eda14cbcSMatt Macy 
1552eda14cbcSMatt Macy 	if (((BP_IS_PROTECTED(bp) && !(zio->io_flags & ZIO_FLAG_RAW_ENCRYPT)) ||
1553eda14cbcSMatt Macy 	    BP_HAS_INDIRECT_MAC_CKSUM(bp)) &&
1554eda14cbcSMatt Macy 	    zio->io_child_type == ZIO_CHILD_LOGICAL) {
1555eda14cbcSMatt Macy 		zio_push_transform(zio, abd_alloc_sametype(zio->io_abd, psize),
1556eda14cbcSMatt Macy 		    psize, psize, zio_decrypt);
1557eda14cbcSMatt Macy 	}
1558eda14cbcSMatt Macy 
1559eda14cbcSMatt Macy 	if (BP_IS_EMBEDDED(bp) && BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA) {
1560eda14cbcSMatt Macy 		int psize = BPE_GET_PSIZE(bp);
1561eda14cbcSMatt Macy 		void *data = abd_borrow_buf(zio->io_abd, psize);
1562eda14cbcSMatt Macy 
1563eda14cbcSMatt Macy 		zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1564eda14cbcSMatt Macy 		decode_embedded_bp_compressed(bp, data);
1565eda14cbcSMatt Macy 		abd_return_buf_copy(zio->io_abd, data, psize);
1566eda14cbcSMatt Macy 	} else {
1567eda14cbcSMatt Macy 		ASSERT(!BP_IS_EMBEDDED(bp));
1568eda14cbcSMatt Macy 		ASSERT3P(zio->io_bp, ==, &zio->io_bp_copy);
1569eda14cbcSMatt Macy 	}
1570eda14cbcSMatt Macy 
1571eda14cbcSMatt Macy 	if (!DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) && BP_GET_LEVEL(bp) == 0)
1572eda14cbcSMatt Macy 		zio->io_flags |= ZIO_FLAG_DONT_CACHE;
1573eda14cbcSMatt Macy 
1574eda14cbcSMatt Macy 	if (BP_GET_TYPE(bp) == DMU_OT_DDT_ZAP)
1575eda14cbcSMatt Macy 		zio->io_flags |= ZIO_FLAG_DONT_CACHE;
1576eda14cbcSMatt Macy 
1577eda14cbcSMatt Macy 	if (BP_GET_DEDUP(bp) && zio->io_child_type == ZIO_CHILD_LOGICAL)
1578eda14cbcSMatt Macy 		zio->io_pipeline = ZIO_DDT_READ_PIPELINE;
1579eda14cbcSMatt Macy 
1580eda14cbcSMatt Macy 	return (zio);
1581eda14cbcSMatt Macy }
1582eda14cbcSMatt Macy 
1583eda14cbcSMatt Macy static zio_t *
1584eda14cbcSMatt Macy zio_write_bp_init(zio_t *zio)
1585eda14cbcSMatt Macy {
1586eda14cbcSMatt Macy 	if (!IO_IS_ALLOCATING(zio))
1587eda14cbcSMatt Macy 		return (zio);
1588eda14cbcSMatt Macy 
1589eda14cbcSMatt Macy 	ASSERT(zio->io_child_type != ZIO_CHILD_DDT);
1590eda14cbcSMatt Macy 
1591eda14cbcSMatt Macy 	if (zio->io_bp_override) {
1592eda14cbcSMatt Macy 		blkptr_t *bp = zio->io_bp;
1593eda14cbcSMatt Macy 		zio_prop_t *zp = &zio->io_prop;
1594eda14cbcSMatt Macy 
1595eda14cbcSMatt Macy 		ASSERT(bp->blk_birth != zio->io_txg);
1596eda14cbcSMatt Macy 		ASSERT(BP_GET_DEDUP(zio->io_bp_override) == 0);
1597eda14cbcSMatt Macy 
1598eda14cbcSMatt Macy 		*bp = *zio->io_bp_override;
1599eda14cbcSMatt Macy 		zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1600eda14cbcSMatt Macy 
1601eda14cbcSMatt Macy 		if (BP_IS_EMBEDDED(bp))
1602eda14cbcSMatt Macy 			return (zio);
1603eda14cbcSMatt Macy 
1604eda14cbcSMatt Macy 		/*
1605eda14cbcSMatt Macy 		 * If we've been overridden and nopwrite is set then
1606eda14cbcSMatt Macy 		 * set the flag accordingly to indicate that a nopwrite
1607eda14cbcSMatt Macy 		 * has already occurred.
1608eda14cbcSMatt Macy 		 */
1609eda14cbcSMatt Macy 		if (!BP_IS_HOLE(bp) && zp->zp_nopwrite) {
1610eda14cbcSMatt Macy 			ASSERT(!zp->zp_dedup);
1611eda14cbcSMatt Macy 			ASSERT3U(BP_GET_CHECKSUM(bp), ==, zp->zp_checksum);
1612eda14cbcSMatt Macy 			zio->io_flags |= ZIO_FLAG_NOPWRITE;
1613eda14cbcSMatt Macy 			return (zio);
1614eda14cbcSMatt Macy 		}
1615eda14cbcSMatt Macy 
1616eda14cbcSMatt Macy 		ASSERT(!zp->zp_nopwrite);
1617eda14cbcSMatt Macy 
1618eda14cbcSMatt Macy 		if (BP_IS_HOLE(bp) || !zp->zp_dedup)
1619eda14cbcSMatt Macy 			return (zio);
1620eda14cbcSMatt Macy 
1621eda14cbcSMatt Macy 		ASSERT((zio_checksum_table[zp->zp_checksum].ci_flags &
1622eda14cbcSMatt Macy 		    ZCHECKSUM_FLAG_DEDUP) || zp->zp_dedup_verify);
1623eda14cbcSMatt Macy 
1624eda14cbcSMatt Macy 		if (BP_GET_CHECKSUM(bp) == zp->zp_checksum &&
1625eda14cbcSMatt Macy 		    !zp->zp_encrypt) {
1626eda14cbcSMatt Macy 			BP_SET_DEDUP(bp, 1);
1627eda14cbcSMatt Macy 			zio->io_pipeline |= ZIO_STAGE_DDT_WRITE;
1628eda14cbcSMatt Macy 			return (zio);
1629eda14cbcSMatt Macy 		}
1630eda14cbcSMatt Macy 
1631eda14cbcSMatt Macy 		/*
1632eda14cbcSMatt Macy 		 * We were unable to handle this as an override bp, treat
1633eda14cbcSMatt Macy 		 * it as a regular write I/O.
1634eda14cbcSMatt Macy 		 */
1635eda14cbcSMatt Macy 		zio->io_bp_override = NULL;
1636eda14cbcSMatt Macy 		*bp = zio->io_bp_orig;
1637eda14cbcSMatt Macy 		zio->io_pipeline = zio->io_orig_pipeline;
1638eda14cbcSMatt Macy 	}
1639eda14cbcSMatt Macy 
1640eda14cbcSMatt Macy 	return (zio);
1641eda14cbcSMatt Macy }
1642eda14cbcSMatt Macy 
1643eda14cbcSMatt Macy static zio_t *
1644eda14cbcSMatt Macy zio_write_compress(zio_t *zio)
1645eda14cbcSMatt Macy {
1646eda14cbcSMatt Macy 	spa_t *spa = zio->io_spa;
1647eda14cbcSMatt Macy 	zio_prop_t *zp = &zio->io_prop;
1648eda14cbcSMatt Macy 	enum zio_compress compress = zp->zp_compress;
1649eda14cbcSMatt Macy 	blkptr_t *bp = zio->io_bp;
1650eda14cbcSMatt Macy 	uint64_t lsize = zio->io_lsize;
1651eda14cbcSMatt Macy 	uint64_t psize = zio->io_size;
1652eda14cbcSMatt Macy 	int pass = 1;
1653eda14cbcSMatt Macy 
1654eda14cbcSMatt Macy 	/*
1655eda14cbcSMatt Macy 	 * If our children haven't all reached the ready stage,
1656eda14cbcSMatt Macy 	 * wait for them and then repeat this pipeline stage.
1657eda14cbcSMatt Macy 	 */
1658eda14cbcSMatt Macy 	if (zio_wait_for_children(zio, ZIO_CHILD_LOGICAL_BIT |
1659eda14cbcSMatt Macy 	    ZIO_CHILD_GANG_BIT, ZIO_WAIT_READY)) {
1660eda14cbcSMatt Macy 		return (NULL);
1661eda14cbcSMatt Macy 	}
1662eda14cbcSMatt Macy 
1663eda14cbcSMatt Macy 	if (!IO_IS_ALLOCATING(zio))
1664eda14cbcSMatt Macy 		return (zio);
1665eda14cbcSMatt Macy 
1666eda14cbcSMatt Macy 	if (zio->io_children_ready != NULL) {
1667eda14cbcSMatt Macy 		/*
1668eda14cbcSMatt Macy 		 * Now that all our children are ready, run the callback
1669eda14cbcSMatt Macy 		 * associated with this zio in case it wants to modify the
1670eda14cbcSMatt Macy 		 * data to be written.
1671eda14cbcSMatt Macy 		 */
1672eda14cbcSMatt Macy 		ASSERT3U(zp->zp_level, >, 0);
1673eda14cbcSMatt Macy 		zio->io_children_ready(zio);
1674eda14cbcSMatt Macy 	}
1675eda14cbcSMatt Macy 
1676eda14cbcSMatt Macy 	ASSERT(zio->io_child_type != ZIO_CHILD_DDT);
1677eda14cbcSMatt Macy 	ASSERT(zio->io_bp_override == NULL);
1678eda14cbcSMatt Macy 
1679eda14cbcSMatt Macy 	if (!BP_IS_HOLE(bp) && bp->blk_birth == zio->io_txg) {
1680eda14cbcSMatt Macy 		/*
1681eda14cbcSMatt Macy 		 * We're rewriting an existing block, which means we're
1682eda14cbcSMatt Macy 		 * working on behalf of spa_sync().  For spa_sync() to
1683eda14cbcSMatt Macy 		 * converge, it must eventually be the case that we don't
1684eda14cbcSMatt Macy 		 * have to allocate new blocks.  But compression changes
1685eda14cbcSMatt Macy 		 * the blocksize, which forces a reallocate, and makes
1686eda14cbcSMatt Macy 		 * convergence take longer.  Therefore, after the first
1687eda14cbcSMatt Macy 		 * few passes, stop compressing to ensure convergence.
1688eda14cbcSMatt Macy 		 */
1689eda14cbcSMatt Macy 		pass = spa_sync_pass(spa);
1690eda14cbcSMatt Macy 
1691eda14cbcSMatt Macy 		ASSERT(zio->io_txg == spa_syncing_txg(spa));
1692eda14cbcSMatt Macy 		ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1693eda14cbcSMatt Macy 		ASSERT(!BP_GET_DEDUP(bp));
1694eda14cbcSMatt Macy 
1695eda14cbcSMatt Macy 		if (pass >= zfs_sync_pass_dont_compress)
1696eda14cbcSMatt Macy 			compress = ZIO_COMPRESS_OFF;
1697eda14cbcSMatt Macy 
1698eda14cbcSMatt Macy 		/* Make sure someone doesn't change their mind on overwrites */
1699eda14cbcSMatt Macy 		ASSERT(BP_IS_EMBEDDED(bp) || MIN(zp->zp_copies + BP_IS_GANG(bp),
1700eda14cbcSMatt Macy 		    spa_max_replication(spa)) == BP_GET_NDVAS(bp));
1701eda14cbcSMatt Macy 	}
1702eda14cbcSMatt Macy 
1703eda14cbcSMatt Macy 	/* If it's a compressed write that is not raw, compress the buffer. */
1704eda14cbcSMatt Macy 	if (compress != ZIO_COMPRESS_OFF &&
1705eda14cbcSMatt Macy 	    !(zio->io_flags & ZIO_FLAG_RAW_COMPRESS)) {
1706eda14cbcSMatt Macy 		void *cbuf = zio_buf_alloc(lsize);
1707eda14cbcSMatt Macy 		psize = zio_compress_data(compress, zio->io_abd, cbuf, lsize,
1708eda14cbcSMatt Macy 		    zp->zp_complevel);
1709eda14cbcSMatt Macy 		if (psize == 0 || psize >= lsize) {
1710eda14cbcSMatt Macy 			compress = ZIO_COMPRESS_OFF;
1711eda14cbcSMatt Macy 			zio_buf_free(cbuf, lsize);
1712eda14cbcSMatt Macy 		} else if (!zp->zp_dedup && !zp->zp_encrypt &&
1713eda14cbcSMatt Macy 		    psize <= BPE_PAYLOAD_SIZE &&
1714eda14cbcSMatt Macy 		    zp->zp_level == 0 && !DMU_OT_HAS_FILL(zp->zp_type) &&
1715eda14cbcSMatt Macy 		    spa_feature_is_enabled(spa, SPA_FEATURE_EMBEDDED_DATA)) {
1716eda14cbcSMatt Macy 			encode_embedded_bp_compressed(bp,
1717eda14cbcSMatt Macy 			    cbuf, compress, lsize, psize);
1718eda14cbcSMatt Macy 			BPE_SET_ETYPE(bp, BP_EMBEDDED_TYPE_DATA);
1719eda14cbcSMatt Macy 			BP_SET_TYPE(bp, zio->io_prop.zp_type);
1720eda14cbcSMatt Macy 			BP_SET_LEVEL(bp, zio->io_prop.zp_level);
1721eda14cbcSMatt Macy 			zio_buf_free(cbuf, lsize);
1722eda14cbcSMatt Macy 			bp->blk_birth = zio->io_txg;
1723eda14cbcSMatt Macy 			zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1724eda14cbcSMatt Macy 			ASSERT(spa_feature_is_active(spa,
1725eda14cbcSMatt Macy 			    SPA_FEATURE_EMBEDDED_DATA));
1726eda14cbcSMatt Macy 			return (zio);
1727eda14cbcSMatt Macy 		} else {
1728eda14cbcSMatt Macy 			/*
17297877fdebSMatt Macy 			 * Round compressed size up to the minimum allocation
17307877fdebSMatt Macy 			 * size of the smallest-ashift device, and zero the
17317877fdebSMatt Macy 			 * tail. This ensures that the compressed size of the
17327877fdebSMatt Macy 			 * BP (and thus compressratio property) are correct,
1733eda14cbcSMatt Macy 			 * in that we charge for the padding used to fill out
1734eda14cbcSMatt Macy 			 * the last sector.
1735eda14cbcSMatt Macy 			 */
17367877fdebSMatt Macy 			ASSERT3U(spa->spa_min_alloc, >=, SPA_MINBLOCKSHIFT);
17377877fdebSMatt Macy 			size_t rounded = (size_t)roundup(psize,
17387877fdebSMatt Macy 			    spa->spa_min_alloc);
1739eda14cbcSMatt Macy 			if (rounded >= lsize) {
1740eda14cbcSMatt Macy 				compress = ZIO_COMPRESS_OFF;
1741eda14cbcSMatt Macy 				zio_buf_free(cbuf, lsize);
1742eda14cbcSMatt Macy 				psize = lsize;
1743eda14cbcSMatt Macy 			} else {
1744eda14cbcSMatt Macy 				abd_t *cdata = abd_get_from_buf(cbuf, lsize);
1745eda14cbcSMatt Macy 				abd_take_ownership_of_buf(cdata, B_TRUE);
1746eda14cbcSMatt Macy 				abd_zero_off(cdata, psize, rounded - psize);
1747eda14cbcSMatt Macy 				psize = rounded;
1748eda14cbcSMatt Macy 				zio_push_transform(zio, cdata,
1749eda14cbcSMatt Macy 				    psize, lsize, NULL);
1750eda14cbcSMatt Macy 			}
1751eda14cbcSMatt Macy 		}
1752eda14cbcSMatt Macy 
1753eda14cbcSMatt Macy 		/*
1754eda14cbcSMatt Macy 		 * We were unable to handle this as an override bp, treat
1755eda14cbcSMatt Macy 		 * it as a regular write I/O.
1756eda14cbcSMatt Macy 		 */
1757eda14cbcSMatt Macy 		zio->io_bp_override = NULL;
1758eda14cbcSMatt Macy 		*bp = zio->io_bp_orig;
1759eda14cbcSMatt Macy 		zio->io_pipeline = zio->io_orig_pipeline;
1760eda14cbcSMatt Macy 
1761eda14cbcSMatt Macy 	} else if ((zio->io_flags & ZIO_FLAG_RAW_ENCRYPT) != 0 &&
1762eda14cbcSMatt Macy 	    zp->zp_type == DMU_OT_DNODE) {
1763eda14cbcSMatt Macy 		/*
1764eda14cbcSMatt Macy 		 * The DMU actually relies on the zio layer's compression
1765eda14cbcSMatt Macy 		 * to free metadnode blocks that have had all contained
1766eda14cbcSMatt Macy 		 * dnodes freed. As a result, even when doing a raw
1767eda14cbcSMatt Macy 		 * receive, we must check whether the block can be compressed
1768eda14cbcSMatt Macy 		 * to a hole.
1769eda14cbcSMatt Macy 		 */
1770eda14cbcSMatt Macy 		psize = zio_compress_data(ZIO_COMPRESS_EMPTY,
1771eda14cbcSMatt Macy 		    zio->io_abd, NULL, lsize, zp->zp_complevel);
1772eda14cbcSMatt Macy 		if (psize == 0 || psize >= lsize)
1773eda14cbcSMatt Macy 			compress = ZIO_COMPRESS_OFF;
1774*53b70c86SMartin Matuska 	} else if (zio->io_flags & ZIO_FLAG_RAW_COMPRESS) {
1775*53b70c86SMartin Matuska 		size_t rounded = MIN((size_t)roundup(psize,
1776*53b70c86SMartin Matuska 		    spa->spa_min_alloc), lsize);
1777*53b70c86SMartin Matuska 
1778*53b70c86SMartin Matuska 		if (rounded != psize) {
1779*53b70c86SMartin Matuska 			abd_t *cdata = abd_alloc_linear(rounded, B_TRUE);
1780*53b70c86SMartin Matuska 			abd_zero_off(cdata, psize, rounded - psize);
1781*53b70c86SMartin Matuska 			abd_copy_off(cdata, zio->io_abd, 0, 0, psize);
1782*53b70c86SMartin Matuska 			psize = rounded;
1783*53b70c86SMartin Matuska 			zio_push_transform(zio, cdata,
1784*53b70c86SMartin Matuska 			    psize, rounded, NULL);
1785*53b70c86SMartin Matuska 		}
1786eda14cbcSMatt Macy 	} else {
1787eda14cbcSMatt Macy 		ASSERT3U(psize, !=, 0);
1788eda14cbcSMatt Macy 	}
1789eda14cbcSMatt Macy 
1790eda14cbcSMatt Macy 	/*
1791eda14cbcSMatt Macy 	 * The final pass of spa_sync() must be all rewrites, but the first
1792eda14cbcSMatt Macy 	 * few passes offer a trade-off: allocating blocks defers convergence,
1793eda14cbcSMatt Macy 	 * but newly allocated blocks are sequential, so they can be written
1794eda14cbcSMatt Macy 	 * to disk faster.  Therefore, we allow the first few passes of
1795eda14cbcSMatt Macy 	 * spa_sync() to allocate new blocks, but force rewrites after that.
1796eda14cbcSMatt Macy 	 * There should only be a handful of blocks after pass 1 in any case.
1797eda14cbcSMatt Macy 	 */
1798eda14cbcSMatt Macy 	if (!BP_IS_HOLE(bp) && bp->blk_birth == zio->io_txg &&
1799eda14cbcSMatt Macy 	    BP_GET_PSIZE(bp) == psize &&
1800eda14cbcSMatt Macy 	    pass >= zfs_sync_pass_rewrite) {
1801eda14cbcSMatt Macy 		VERIFY3U(psize, !=, 0);
1802eda14cbcSMatt Macy 		enum zio_stage gang_stages = zio->io_pipeline & ZIO_GANG_STAGES;
1803eda14cbcSMatt Macy 
1804eda14cbcSMatt Macy 		zio->io_pipeline = ZIO_REWRITE_PIPELINE | gang_stages;
1805eda14cbcSMatt Macy 		zio->io_flags |= ZIO_FLAG_IO_REWRITE;
1806eda14cbcSMatt Macy 	} else {
1807eda14cbcSMatt Macy 		BP_ZERO(bp);
1808eda14cbcSMatt Macy 		zio->io_pipeline = ZIO_WRITE_PIPELINE;
1809eda14cbcSMatt Macy 	}
1810eda14cbcSMatt Macy 
1811eda14cbcSMatt Macy 	if (psize == 0) {
1812eda14cbcSMatt Macy 		if (zio->io_bp_orig.blk_birth != 0 &&
1813eda14cbcSMatt Macy 		    spa_feature_is_active(spa, SPA_FEATURE_HOLE_BIRTH)) {
1814eda14cbcSMatt Macy 			BP_SET_LSIZE(bp, lsize);
1815eda14cbcSMatt Macy 			BP_SET_TYPE(bp, zp->zp_type);
1816eda14cbcSMatt Macy 			BP_SET_LEVEL(bp, zp->zp_level);
1817eda14cbcSMatt Macy 			BP_SET_BIRTH(bp, zio->io_txg, 0);
1818eda14cbcSMatt Macy 		}
1819eda14cbcSMatt Macy 		zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1820eda14cbcSMatt Macy 	} else {
1821eda14cbcSMatt Macy 		ASSERT(zp->zp_checksum != ZIO_CHECKSUM_GANG_HEADER);
1822eda14cbcSMatt Macy 		BP_SET_LSIZE(bp, lsize);
1823eda14cbcSMatt Macy 		BP_SET_TYPE(bp, zp->zp_type);
1824eda14cbcSMatt Macy 		BP_SET_LEVEL(bp, zp->zp_level);
1825eda14cbcSMatt Macy 		BP_SET_PSIZE(bp, psize);
1826eda14cbcSMatt Macy 		BP_SET_COMPRESS(bp, compress);
1827eda14cbcSMatt Macy 		BP_SET_CHECKSUM(bp, zp->zp_checksum);
1828eda14cbcSMatt Macy 		BP_SET_DEDUP(bp, zp->zp_dedup);
1829eda14cbcSMatt Macy 		BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
1830eda14cbcSMatt Macy 		if (zp->zp_dedup) {
1831eda14cbcSMatt Macy 			ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1832eda14cbcSMatt Macy 			ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
1833eda14cbcSMatt Macy 			ASSERT(!zp->zp_encrypt ||
1834eda14cbcSMatt Macy 			    DMU_OT_IS_ENCRYPTED(zp->zp_type));
1835eda14cbcSMatt Macy 			zio->io_pipeline = ZIO_DDT_WRITE_PIPELINE;
1836eda14cbcSMatt Macy 		}
1837eda14cbcSMatt Macy 		if (zp->zp_nopwrite) {
1838eda14cbcSMatt Macy 			ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1839eda14cbcSMatt Macy 			ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
1840eda14cbcSMatt Macy 			zio->io_pipeline |= ZIO_STAGE_NOP_WRITE;
1841eda14cbcSMatt Macy 		}
1842eda14cbcSMatt Macy 	}
1843eda14cbcSMatt Macy 	return (zio);
1844eda14cbcSMatt Macy }
1845eda14cbcSMatt Macy 
1846eda14cbcSMatt Macy static zio_t *
1847eda14cbcSMatt Macy zio_free_bp_init(zio_t *zio)
1848eda14cbcSMatt Macy {
1849eda14cbcSMatt Macy 	blkptr_t *bp = zio->io_bp;
1850eda14cbcSMatt Macy 
1851eda14cbcSMatt Macy 	if (zio->io_child_type == ZIO_CHILD_LOGICAL) {
1852eda14cbcSMatt Macy 		if (BP_GET_DEDUP(bp))
1853eda14cbcSMatt Macy 			zio->io_pipeline = ZIO_DDT_FREE_PIPELINE;
1854eda14cbcSMatt Macy 	}
1855eda14cbcSMatt Macy 
1856eda14cbcSMatt Macy 	ASSERT3P(zio->io_bp, ==, &zio->io_bp_copy);
1857eda14cbcSMatt Macy 
1858eda14cbcSMatt Macy 	return (zio);
1859eda14cbcSMatt Macy }
1860eda14cbcSMatt Macy 
1861eda14cbcSMatt Macy /*
1862eda14cbcSMatt Macy  * ==========================================================================
1863eda14cbcSMatt Macy  * Execute the I/O pipeline
1864eda14cbcSMatt Macy  * ==========================================================================
1865eda14cbcSMatt Macy  */
1866eda14cbcSMatt Macy 
1867eda14cbcSMatt Macy static void
1868eda14cbcSMatt Macy zio_taskq_dispatch(zio_t *zio, zio_taskq_type_t q, boolean_t cutinline)
1869eda14cbcSMatt Macy {
1870eda14cbcSMatt Macy 	spa_t *spa = zio->io_spa;
1871eda14cbcSMatt Macy 	zio_type_t t = zio->io_type;
1872eda14cbcSMatt Macy 	int flags = (cutinline ? TQ_FRONT : 0);
1873eda14cbcSMatt Macy 
1874eda14cbcSMatt Macy 	/*
1875eda14cbcSMatt Macy 	 * If we're a config writer or a probe, the normal issue and
1876eda14cbcSMatt Macy 	 * interrupt threads may all be blocked waiting for the config lock.
1877eda14cbcSMatt Macy 	 * In this case, select the otherwise-unused taskq for ZIO_TYPE_NULL.
1878eda14cbcSMatt Macy 	 */
1879eda14cbcSMatt Macy 	if (zio->io_flags & (ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_PROBE))
1880eda14cbcSMatt Macy 		t = ZIO_TYPE_NULL;
1881eda14cbcSMatt Macy 
1882eda14cbcSMatt Macy 	/*
1883eda14cbcSMatt Macy 	 * A similar issue exists for the L2ARC write thread until L2ARC 2.0.
1884eda14cbcSMatt Macy 	 */
1885eda14cbcSMatt Macy 	if (t == ZIO_TYPE_WRITE && zio->io_vd && zio->io_vd->vdev_aux)
1886eda14cbcSMatt Macy 		t = ZIO_TYPE_NULL;
1887eda14cbcSMatt Macy 
1888eda14cbcSMatt Macy 	/*
1889eda14cbcSMatt Macy 	 * If this is a high priority I/O, then use the high priority taskq if
1890eda14cbcSMatt Macy 	 * available.
1891eda14cbcSMatt Macy 	 */
1892eda14cbcSMatt Macy 	if ((zio->io_priority == ZIO_PRIORITY_NOW ||
1893eda14cbcSMatt Macy 	    zio->io_priority == ZIO_PRIORITY_SYNC_WRITE) &&
1894eda14cbcSMatt Macy 	    spa->spa_zio_taskq[t][q + 1].stqs_count != 0)
1895eda14cbcSMatt Macy 		q++;
1896eda14cbcSMatt Macy 
1897eda14cbcSMatt Macy 	ASSERT3U(q, <, ZIO_TASKQ_TYPES);
1898eda14cbcSMatt Macy 
1899eda14cbcSMatt Macy 	/*
1900eda14cbcSMatt Macy 	 * NB: We are assuming that the zio can only be dispatched
1901eda14cbcSMatt Macy 	 * to a single taskq at a time.  It would be a grievous error
1902eda14cbcSMatt Macy 	 * to dispatch the zio to another taskq at the same time.
1903eda14cbcSMatt Macy 	 */
1904eda14cbcSMatt Macy 	ASSERT(taskq_empty_ent(&zio->io_tqent));
19053f9d360cSMartin Matuska 	spa_taskq_dispatch_ent(spa, t, q, zio_execute, zio, flags,
19063f9d360cSMartin Matuska 	    &zio->io_tqent);
1907eda14cbcSMatt Macy }
1908eda14cbcSMatt Macy 
1909eda14cbcSMatt Macy static boolean_t
1910eda14cbcSMatt Macy zio_taskq_member(zio_t *zio, zio_taskq_type_t q)
1911eda14cbcSMatt Macy {
1912eda14cbcSMatt Macy 	spa_t *spa = zio->io_spa;
1913eda14cbcSMatt Macy 
1914eda14cbcSMatt Macy 	taskq_t *tq = taskq_of_curthread();
1915eda14cbcSMatt Macy 
1916eda14cbcSMatt Macy 	for (zio_type_t t = 0; t < ZIO_TYPES; t++) {
1917eda14cbcSMatt Macy 		spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
1918eda14cbcSMatt Macy 		uint_t i;
1919eda14cbcSMatt Macy 		for (i = 0; i < tqs->stqs_count; i++) {
1920eda14cbcSMatt Macy 			if (tqs->stqs_taskq[i] == tq)
1921eda14cbcSMatt Macy 				return (B_TRUE);
1922eda14cbcSMatt Macy 		}
1923eda14cbcSMatt Macy 	}
1924eda14cbcSMatt Macy 
1925eda14cbcSMatt Macy 	return (B_FALSE);
1926eda14cbcSMatt Macy }
1927eda14cbcSMatt Macy 
1928eda14cbcSMatt Macy static zio_t *
1929eda14cbcSMatt Macy zio_issue_async(zio_t *zio)
1930eda14cbcSMatt Macy {
1931eda14cbcSMatt Macy 	zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE);
1932eda14cbcSMatt Macy 
1933eda14cbcSMatt Macy 	return (NULL);
1934eda14cbcSMatt Macy }
1935eda14cbcSMatt Macy 
1936eda14cbcSMatt Macy void
19373f9d360cSMartin Matuska zio_interrupt(void *zio)
1938eda14cbcSMatt Macy {
1939eda14cbcSMatt Macy 	zio_taskq_dispatch(zio, ZIO_TASKQ_INTERRUPT, B_FALSE);
1940eda14cbcSMatt Macy }
1941eda14cbcSMatt Macy 
1942eda14cbcSMatt Macy void
1943eda14cbcSMatt Macy zio_delay_interrupt(zio_t *zio)
1944eda14cbcSMatt Macy {
1945eda14cbcSMatt Macy 	/*
1946eda14cbcSMatt Macy 	 * The timeout_generic() function isn't defined in userspace, so
1947eda14cbcSMatt Macy 	 * rather than trying to implement the function, the zio delay
1948eda14cbcSMatt Macy 	 * functionality has been disabled for userspace builds.
1949eda14cbcSMatt Macy 	 */
1950eda14cbcSMatt Macy 
1951eda14cbcSMatt Macy #ifdef _KERNEL
1952eda14cbcSMatt Macy 	/*
1953eda14cbcSMatt Macy 	 * If io_target_timestamp is zero, then no delay has been registered
1954eda14cbcSMatt Macy 	 * for this IO, thus jump to the end of this function and "skip" the
1955eda14cbcSMatt Macy 	 * delay; issuing it directly to the zio layer.
1956eda14cbcSMatt Macy 	 */
1957eda14cbcSMatt Macy 	if (zio->io_target_timestamp != 0) {
1958eda14cbcSMatt Macy 		hrtime_t now = gethrtime();
1959eda14cbcSMatt Macy 
1960eda14cbcSMatt Macy 		if (now >= zio->io_target_timestamp) {
1961eda14cbcSMatt Macy 			/*
1962eda14cbcSMatt Macy 			 * This IO has already taken longer than the target
1963eda14cbcSMatt Macy 			 * delay to complete, so we don't want to delay it
1964eda14cbcSMatt Macy 			 * any longer; we "miss" the delay and issue it
1965eda14cbcSMatt Macy 			 * directly to the zio layer. This is likely due to
1966eda14cbcSMatt Macy 			 * the target latency being set to a value less than
1967eda14cbcSMatt Macy 			 * the underlying hardware can satisfy (e.g. delay
1968eda14cbcSMatt Macy 			 * set to 1ms, but the disks take 10ms to complete an
1969eda14cbcSMatt Macy 			 * IO request).
1970eda14cbcSMatt Macy 			 */
1971eda14cbcSMatt Macy 
1972eda14cbcSMatt Macy 			DTRACE_PROBE2(zio__delay__miss, zio_t *, zio,
1973eda14cbcSMatt Macy 			    hrtime_t, now);
1974eda14cbcSMatt Macy 
1975eda14cbcSMatt Macy 			zio_interrupt(zio);
1976eda14cbcSMatt Macy 		} else {
1977eda14cbcSMatt Macy 			taskqid_t tid;
1978eda14cbcSMatt Macy 			hrtime_t diff = zio->io_target_timestamp - now;
1979eda14cbcSMatt Macy 			clock_t expire_at_tick = ddi_get_lbolt() +
1980eda14cbcSMatt Macy 			    NSEC_TO_TICK(diff);
1981eda14cbcSMatt Macy 
1982eda14cbcSMatt Macy 			DTRACE_PROBE3(zio__delay__hit, zio_t *, zio,
1983eda14cbcSMatt Macy 			    hrtime_t, now, hrtime_t, diff);
1984eda14cbcSMatt Macy 
1985eda14cbcSMatt Macy 			if (NSEC_TO_TICK(diff) == 0) {
1986eda14cbcSMatt Macy 				/* Our delay is less than a jiffy - just spin */
1987eda14cbcSMatt Macy 				zfs_sleep_until(zio->io_target_timestamp);
1988eda14cbcSMatt Macy 				zio_interrupt(zio);
1989eda14cbcSMatt Macy 			} else {
1990eda14cbcSMatt Macy 				/*
1991eda14cbcSMatt Macy 				 * Use taskq_dispatch_delay() in the place of
1992eda14cbcSMatt Macy 				 * OpenZFS's timeout_generic().
1993eda14cbcSMatt Macy 				 */
1994eda14cbcSMatt Macy 				tid = taskq_dispatch_delay(system_taskq,
19953f9d360cSMartin Matuska 				    zio_interrupt, zio, TQ_NOSLEEP,
19963f9d360cSMartin Matuska 				    expire_at_tick);
1997eda14cbcSMatt Macy 				if (tid == TASKQID_INVALID) {
1998eda14cbcSMatt Macy 					/*
1999eda14cbcSMatt Macy 					 * Couldn't allocate a task.  Just
2000eda14cbcSMatt Macy 					 * finish the zio without a delay.
2001eda14cbcSMatt Macy 					 */
2002eda14cbcSMatt Macy 					zio_interrupt(zio);
2003eda14cbcSMatt Macy 				}
2004eda14cbcSMatt Macy 			}
2005eda14cbcSMatt Macy 		}
2006eda14cbcSMatt Macy 		return;
2007eda14cbcSMatt Macy 	}
2008eda14cbcSMatt Macy #endif
2009eda14cbcSMatt Macy 	DTRACE_PROBE1(zio__delay__skip, zio_t *, zio);
2010eda14cbcSMatt Macy 	zio_interrupt(zio);
2011eda14cbcSMatt Macy }
2012eda14cbcSMatt Macy 
2013eda14cbcSMatt Macy static void
2014eda14cbcSMatt Macy zio_deadman_impl(zio_t *pio, int ziodepth)
2015eda14cbcSMatt Macy {
2016eda14cbcSMatt Macy 	zio_t *cio, *cio_next;
2017eda14cbcSMatt Macy 	zio_link_t *zl = NULL;
2018eda14cbcSMatt Macy 	vdev_t *vd = pio->io_vd;
2019eda14cbcSMatt Macy 
2020eda14cbcSMatt Macy 	if (zio_deadman_log_all || (vd != NULL && vd->vdev_ops->vdev_op_leaf)) {
2021eda14cbcSMatt Macy 		vdev_queue_t *vq = vd ? &vd->vdev_queue : NULL;
2022eda14cbcSMatt Macy 		zbookmark_phys_t *zb = &pio->io_bookmark;
2023eda14cbcSMatt Macy 		uint64_t delta = gethrtime() - pio->io_timestamp;
2024eda14cbcSMatt Macy 		uint64_t failmode = spa_get_deadman_failmode(pio->io_spa);
2025eda14cbcSMatt Macy 
2026eda14cbcSMatt Macy 		zfs_dbgmsg("slow zio[%d]: zio=%px timestamp=%llu "
2027eda14cbcSMatt Macy 		    "delta=%llu queued=%llu io=%llu "
202833b8c039SMartin Matuska 		    "path=%s "
202933b8c039SMartin Matuska 		    "last=%llu type=%d "
203033b8c039SMartin Matuska 		    "priority=%d flags=0x%x stage=0x%x "
203133b8c039SMartin Matuska 		    "pipeline=0x%x pipeline-trace=0x%x "
203233b8c039SMartin Matuska 		    "objset=%llu object=%llu "
203333b8c039SMartin Matuska 		    "level=%llu blkid=%llu "
203433b8c039SMartin Matuska 		    "offset=%llu size=%llu "
203533b8c039SMartin Matuska 		    "error=%d",
2036eda14cbcSMatt Macy 		    ziodepth, pio, pio->io_timestamp,
203733b8c039SMartin Matuska 		    (u_longlong_t)delta, pio->io_delta, pio->io_delay,
203833b8c039SMartin Matuska 		    vd ? vd->vdev_path : "NULL",
203933b8c039SMartin Matuska 		    vq ? vq->vq_io_complete_ts : 0, pio->io_type,
204033b8c039SMartin Matuska 		    pio->io_priority, pio->io_flags, pio->io_stage,
204133b8c039SMartin Matuska 		    pio->io_pipeline, pio->io_pipeline_trace,
204233b8c039SMartin Matuska 		    (u_longlong_t)zb->zb_objset, (u_longlong_t)zb->zb_object,
204333b8c039SMartin Matuska 		    (u_longlong_t)zb->zb_level, (u_longlong_t)zb->zb_blkid,
204433b8c039SMartin Matuska 		    (u_longlong_t)pio->io_offset, (u_longlong_t)pio->io_size,
204533b8c039SMartin Matuska 		    pio->io_error);
2046eac7052fSMatt Macy 		(void) zfs_ereport_post(FM_EREPORT_ZFS_DEADMAN,
20472c48331dSMatt Macy 		    pio->io_spa, vd, zb, pio, 0);
2048eda14cbcSMatt Macy 
2049eda14cbcSMatt Macy 		if (failmode == ZIO_FAILURE_MODE_CONTINUE &&
2050eda14cbcSMatt Macy 		    taskq_empty_ent(&pio->io_tqent)) {
2051eda14cbcSMatt Macy 			zio_interrupt(pio);
2052eda14cbcSMatt Macy 		}
2053eda14cbcSMatt Macy 	}
2054eda14cbcSMatt Macy 
2055eda14cbcSMatt Macy 	mutex_enter(&pio->io_lock);
2056eda14cbcSMatt Macy 	for (cio = zio_walk_children(pio, &zl); cio != NULL; cio = cio_next) {
2057eda14cbcSMatt Macy 		cio_next = zio_walk_children(pio, &zl);
2058eda14cbcSMatt Macy 		zio_deadman_impl(cio, ziodepth + 1);
2059eda14cbcSMatt Macy 	}
2060eda14cbcSMatt Macy 	mutex_exit(&pio->io_lock);
2061eda14cbcSMatt Macy }
2062eda14cbcSMatt Macy 
2063eda14cbcSMatt Macy /*
2064eda14cbcSMatt Macy  * Log the critical information describing this zio and all of its children
2065eda14cbcSMatt Macy  * using the zfs_dbgmsg() interface then post deadman event for the ZED.
2066eda14cbcSMatt Macy  */
2067eda14cbcSMatt Macy void
2068eda14cbcSMatt Macy zio_deadman(zio_t *pio, char *tag)
2069eda14cbcSMatt Macy {
2070eda14cbcSMatt Macy 	spa_t *spa = pio->io_spa;
2071eda14cbcSMatt Macy 	char *name = spa_name(spa);
2072eda14cbcSMatt Macy 
2073eda14cbcSMatt Macy 	if (!zfs_deadman_enabled || spa_suspended(spa))
2074eda14cbcSMatt Macy 		return;
2075eda14cbcSMatt Macy 
2076eda14cbcSMatt Macy 	zio_deadman_impl(pio, 0);
2077eda14cbcSMatt Macy 
2078eda14cbcSMatt Macy 	switch (spa_get_deadman_failmode(spa)) {
2079eda14cbcSMatt Macy 	case ZIO_FAILURE_MODE_WAIT:
2080eda14cbcSMatt Macy 		zfs_dbgmsg("%s waiting for hung I/O to pool '%s'", tag, name);
2081eda14cbcSMatt Macy 		break;
2082eda14cbcSMatt Macy 
2083eda14cbcSMatt Macy 	case ZIO_FAILURE_MODE_CONTINUE:
2084eda14cbcSMatt Macy 		zfs_dbgmsg("%s restarting hung I/O for pool '%s'", tag, name);
2085eda14cbcSMatt Macy 		break;
2086eda14cbcSMatt Macy 
2087eda14cbcSMatt Macy 	case ZIO_FAILURE_MODE_PANIC:
2088eda14cbcSMatt Macy 		fm_panic("%s determined I/O to pool '%s' is hung.", tag, name);
2089eda14cbcSMatt Macy 		break;
2090eda14cbcSMatt Macy 	}
2091eda14cbcSMatt Macy }
2092eda14cbcSMatt Macy 
2093eda14cbcSMatt Macy /*
2094eda14cbcSMatt Macy  * Execute the I/O pipeline until one of the following occurs:
2095eda14cbcSMatt Macy  * (1) the I/O completes; (2) the pipeline stalls waiting for
2096eda14cbcSMatt Macy  * dependent child I/Os; (3) the I/O issues, so we're waiting
2097eda14cbcSMatt Macy  * for an I/O completion interrupt; (4) the I/O is delegated by
2098eda14cbcSMatt Macy  * vdev-level caching or aggregation; (5) the I/O is deferred
2099eda14cbcSMatt Macy  * due to vdev-level queueing; (6) the I/O is handed off to
2100eda14cbcSMatt Macy  * another thread.  In all cases, the pipeline stops whenever
2101eda14cbcSMatt Macy  * there's no CPU work; it never burns a thread in cv_wait_io().
2102eda14cbcSMatt Macy  *
2103eda14cbcSMatt Macy  * There's no locking on io_stage because there's no legitimate way
2104eda14cbcSMatt Macy  * for multiple threads to be attempting to process the same I/O.
2105eda14cbcSMatt Macy  */
2106eda14cbcSMatt Macy static zio_pipe_stage_t *zio_pipeline[];
2107eda14cbcSMatt Macy 
2108eda14cbcSMatt Macy /*
2109eda14cbcSMatt Macy  * zio_execute() is a wrapper around the static function
2110eda14cbcSMatt Macy  * __zio_execute() so that we can force  __zio_execute() to be
2111eda14cbcSMatt Macy  * inlined.  This reduces stack overhead which is important
2112eda14cbcSMatt Macy  * because __zio_execute() is called recursively in several zio
2113eda14cbcSMatt Macy  * code paths.  zio_execute() itself cannot be inlined because
2114eda14cbcSMatt Macy  * it is externally visible.
2115eda14cbcSMatt Macy  */
2116eda14cbcSMatt Macy void
21173f9d360cSMartin Matuska zio_execute(void *zio)
2118eda14cbcSMatt Macy {
2119eda14cbcSMatt Macy 	fstrans_cookie_t cookie;
2120eda14cbcSMatt Macy 
2121eda14cbcSMatt Macy 	cookie = spl_fstrans_mark();
2122eda14cbcSMatt Macy 	__zio_execute(zio);
2123eda14cbcSMatt Macy 	spl_fstrans_unmark(cookie);
2124eda14cbcSMatt Macy }
2125eda14cbcSMatt Macy 
2126eda14cbcSMatt Macy /*
2127eda14cbcSMatt Macy  * Used to determine if in the current context the stack is sized large
2128eda14cbcSMatt Macy  * enough to allow zio_execute() to be called recursively.  A minimum
2129eda14cbcSMatt Macy  * stack size of 16K is required to avoid needing to re-dispatch the zio.
2130eda14cbcSMatt Macy  */
2131eda14cbcSMatt Macy static boolean_t
2132eda14cbcSMatt Macy zio_execute_stack_check(zio_t *zio)
2133eda14cbcSMatt Macy {
2134eda14cbcSMatt Macy #if !defined(HAVE_LARGE_STACKS)
2135eda14cbcSMatt Macy 	dsl_pool_t *dp = spa_get_dsl(zio->io_spa);
2136eda14cbcSMatt Macy 
2137eda14cbcSMatt Macy 	/* Executing in txg_sync_thread() context. */
2138eda14cbcSMatt Macy 	if (dp && curthread == dp->dp_tx.tx_sync_thread)
2139eda14cbcSMatt Macy 		return (B_TRUE);
2140eda14cbcSMatt Macy 
2141eda14cbcSMatt Macy 	/* Pool initialization outside of zio_taskq context. */
2142eda14cbcSMatt Macy 	if (dp && spa_is_initializing(dp->dp_spa) &&
2143eda14cbcSMatt Macy 	    !zio_taskq_member(zio, ZIO_TASKQ_ISSUE) &&
2144eda14cbcSMatt Macy 	    !zio_taskq_member(zio, ZIO_TASKQ_ISSUE_HIGH))
2145eda14cbcSMatt Macy 		return (B_TRUE);
2146eda14cbcSMatt Macy #endif /* HAVE_LARGE_STACKS */
2147eda14cbcSMatt Macy 
2148eda14cbcSMatt Macy 	return (B_FALSE);
2149eda14cbcSMatt Macy }
2150eda14cbcSMatt Macy 
2151eda14cbcSMatt Macy __attribute__((always_inline))
2152eda14cbcSMatt Macy static inline void
2153eda14cbcSMatt Macy __zio_execute(zio_t *zio)
2154eda14cbcSMatt Macy {
2155eda14cbcSMatt Macy 	ASSERT3U(zio->io_queued_timestamp, >, 0);
2156eda14cbcSMatt Macy 
2157eda14cbcSMatt Macy 	while (zio->io_stage < ZIO_STAGE_DONE) {
2158eda14cbcSMatt Macy 		enum zio_stage pipeline = zio->io_pipeline;
2159eda14cbcSMatt Macy 		enum zio_stage stage = zio->io_stage;
2160eda14cbcSMatt Macy 
2161eda14cbcSMatt Macy 		zio->io_executor = curthread;
2162eda14cbcSMatt Macy 
2163eda14cbcSMatt Macy 		ASSERT(!MUTEX_HELD(&zio->io_lock));
2164eda14cbcSMatt Macy 		ASSERT(ISP2(stage));
2165eda14cbcSMatt Macy 		ASSERT(zio->io_stall == NULL);
2166eda14cbcSMatt Macy 
2167eda14cbcSMatt Macy 		do {
2168eda14cbcSMatt Macy 			stage <<= 1;
2169eda14cbcSMatt Macy 		} while ((stage & pipeline) == 0);
2170eda14cbcSMatt Macy 
2171eda14cbcSMatt Macy 		ASSERT(stage <= ZIO_STAGE_DONE);
2172eda14cbcSMatt Macy 
2173eda14cbcSMatt Macy 		/*
2174eda14cbcSMatt Macy 		 * If we are in interrupt context and this pipeline stage
2175eda14cbcSMatt Macy 		 * will grab a config lock that is held across I/O,
2176eda14cbcSMatt Macy 		 * or may wait for an I/O that needs an interrupt thread
2177eda14cbcSMatt Macy 		 * to complete, issue async to avoid deadlock.
2178eda14cbcSMatt Macy 		 *
2179eda14cbcSMatt Macy 		 * For VDEV_IO_START, we cut in line so that the io will
2180eda14cbcSMatt Macy 		 * be sent to disk promptly.
2181eda14cbcSMatt Macy 		 */
2182eda14cbcSMatt Macy 		if ((stage & ZIO_BLOCKING_STAGES) && zio->io_vd == NULL &&
2183eda14cbcSMatt Macy 		    zio_taskq_member(zio, ZIO_TASKQ_INTERRUPT)) {
2184eda14cbcSMatt Macy 			boolean_t cut = (stage == ZIO_STAGE_VDEV_IO_START) ?
2185eda14cbcSMatt Macy 			    zio_requeue_io_start_cut_in_line : B_FALSE;
2186eda14cbcSMatt Macy 			zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, cut);
2187eda14cbcSMatt Macy 			return;
2188eda14cbcSMatt Macy 		}
2189eda14cbcSMatt Macy 
2190eda14cbcSMatt Macy 		/*
2191eda14cbcSMatt Macy 		 * If the current context doesn't have large enough stacks
2192eda14cbcSMatt Macy 		 * the zio must be issued asynchronously to prevent overflow.
2193eda14cbcSMatt Macy 		 */
2194eda14cbcSMatt Macy 		if (zio_execute_stack_check(zio)) {
2195eda14cbcSMatt Macy 			boolean_t cut = (stage == ZIO_STAGE_VDEV_IO_START) ?
2196eda14cbcSMatt Macy 			    zio_requeue_io_start_cut_in_line : B_FALSE;
2197eda14cbcSMatt Macy 			zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, cut);
2198eda14cbcSMatt Macy 			return;
2199eda14cbcSMatt Macy 		}
2200eda14cbcSMatt Macy 
2201eda14cbcSMatt Macy 		zio->io_stage = stage;
2202eda14cbcSMatt Macy 		zio->io_pipeline_trace |= zio->io_stage;
2203eda14cbcSMatt Macy 
2204eda14cbcSMatt Macy 		/*
2205eda14cbcSMatt Macy 		 * The zio pipeline stage returns the next zio to execute
2206eda14cbcSMatt Macy 		 * (typically the same as this one), or NULL if we should
2207eda14cbcSMatt Macy 		 * stop.
2208eda14cbcSMatt Macy 		 */
2209eda14cbcSMatt Macy 		zio = zio_pipeline[highbit64(stage) - 1](zio);
2210eda14cbcSMatt Macy 
2211eda14cbcSMatt Macy 		if (zio == NULL)
2212eda14cbcSMatt Macy 			return;
2213eda14cbcSMatt Macy 	}
2214eda14cbcSMatt Macy }
2215eda14cbcSMatt Macy 
2216eda14cbcSMatt Macy 
2217eda14cbcSMatt Macy /*
2218eda14cbcSMatt Macy  * ==========================================================================
2219eda14cbcSMatt Macy  * Initiate I/O, either sync or async
2220eda14cbcSMatt Macy  * ==========================================================================
2221eda14cbcSMatt Macy  */
2222eda14cbcSMatt Macy int
2223eda14cbcSMatt Macy zio_wait(zio_t *zio)
2224eda14cbcSMatt Macy {
2225eda14cbcSMatt Macy 	/*
2226eda14cbcSMatt Macy 	 * Some routines, like zio_free_sync(), may return a NULL zio
2227eda14cbcSMatt Macy 	 * to avoid the performance overhead of creating and then destroying
2228eda14cbcSMatt Macy 	 * an unneeded zio.  For the callers' simplicity, we accept a NULL
2229eda14cbcSMatt Macy 	 * zio and ignore it.
2230eda14cbcSMatt Macy 	 */
2231eda14cbcSMatt Macy 	if (zio == NULL)
2232eda14cbcSMatt Macy 		return (0);
2233eda14cbcSMatt Macy 
2234eda14cbcSMatt Macy 	long timeout = MSEC_TO_TICK(zfs_deadman_ziotime_ms);
2235eda14cbcSMatt Macy 	int error;
2236eda14cbcSMatt Macy 
2237eda14cbcSMatt Macy 	ASSERT3S(zio->io_stage, ==, ZIO_STAGE_OPEN);
2238eda14cbcSMatt Macy 	ASSERT3P(zio->io_executor, ==, NULL);
2239eda14cbcSMatt Macy 
2240eda14cbcSMatt Macy 	zio->io_waiter = curthread;
2241eda14cbcSMatt Macy 	ASSERT0(zio->io_queued_timestamp);
2242eda14cbcSMatt Macy 	zio->io_queued_timestamp = gethrtime();
2243eda14cbcSMatt Macy 
2244eda14cbcSMatt Macy 	__zio_execute(zio);
2245eda14cbcSMatt Macy 
2246eda14cbcSMatt Macy 	mutex_enter(&zio->io_lock);
2247eda14cbcSMatt Macy 	while (zio->io_executor != NULL) {
2248eda14cbcSMatt Macy 		error = cv_timedwait_io(&zio->io_cv, &zio->io_lock,
2249eda14cbcSMatt Macy 		    ddi_get_lbolt() + timeout);
2250eda14cbcSMatt Macy 
2251eda14cbcSMatt Macy 		if (zfs_deadman_enabled && error == -1 &&
2252eda14cbcSMatt Macy 		    gethrtime() - zio->io_queued_timestamp >
2253eda14cbcSMatt Macy 		    spa_deadman_ziotime(zio->io_spa)) {
2254eda14cbcSMatt Macy 			mutex_exit(&zio->io_lock);
2255eda14cbcSMatt Macy 			timeout = MSEC_TO_TICK(zfs_deadman_checktime_ms);
2256eda14cbcSMatt Macy 			zio_deadman(zio, FTAG);
2257eda14cbcSMatt Macy 			mutex_enter(&zio->io_lock);
2258eda14cbcSMatt Macy 		}
2259eda14cbcSMatt Macy 	}
2260eda14cbcSMatt Macy 	mutex_exit(&zio->io_lock);
2261eda14cbcSMatt Macy 
2262eda14cbcSMatt Macy 	error = zio->io_error;
2263eda14cbcSMatt Macy 	zio_destroy(zio);
2264eda14cbcSMatt Macy 
2265eda14cbcSMatt Macy 	return (error);
2266eda14cbcSMatt Macy }
2267eda14cbcSMatt Macy 
2268eda14cbcSMatt Macy void
2269eda14cbcSMatt Macy zio_nowait(zio_t *zio)
2270eda14cbcSMatt Macy {
2271eda14cbcSMatt Macy 	/*
2272eda14cbcSMatt Macy 	 * See comment in zio_wait().
2273eda14cbcSMatt Macy 	 */
2274eda14cbcSMatt Macy 	if (zio == NULL)
2275eda14cbcSMatt Macy 		return;
2276eda14cbcSMatt Macy 
2277eda14cbcSMatt Macy 	ASSERT3P(zio->io_executor, ==, NULL);
2278eda14cbcSMatt Macy 
2279eda14cbcSMatt Macy 	if (zio->io_child_type == ZIO_CHILD_LOGICAL &&
2280eda14cbcSMatt Macy 	    zio_unique_parent(zio) == NULL) {
2281eda14cbcSMatt Macy 		zio_t *pio;
2282eda14cbcSMatt Macy 
2283eda14cbcSMatt Macy 		/*
2284eda14cbcSMatt Macy 		 * This is a logical async I/O with no parent to wait for it.
2285eda14cbcSMatt Macy 		 * We add it to the spa_async_root_zio "Godfather" I/O which
2286eda14cbcSMatt Macy 		 * will ensure they complete prior to unloading the pool.
2287eda14cbcSMatt Macy 		 */
2288eda14cbcSMatt Macy 		spa_t *spa = zio->io_spa;
22897877fdebSMatt Macy 		pio = spa->spa_async_zio_root[CPU_SEQID_UNSTABLE];
2290eda14cbcSMatt Macy 
2291eda14cbcSMatt Macy 		zio_add_child(pio, zio);
2292eda14cbcSMatt Macy 	}
2293eda14cbcSMatt Macy 
2294eda14cbcSMatt Macy 	ASSERT0(zio->io_queued_timestamp);
2295eda14cbcSMatt Macy 	zio->io_queued_timestamp = gethrtime();
2296eda14cbcSMatt Macy 	__zio_execute(zio);
2297eda14cbcSMatt Macy }
2298eda14cbcSMatt Macy 
2299eda14cbcSMatt Macy /*
2300eda14cbcSMatt Macy  * ==========================================================================
2301eda14cbcSMatt Macy  * Reexecute, cancel, or suspend/resume failed I/O
2302eda14cbcSMatt Macy  * ==========================================================================
2303eda14cbcSMatt Macy  */
2304eda14cbcSMatt Macy 
2305eda14cbcSMatt Macy static void
23063f9d360cSMartin Matuska zio_reexecute(void *arg)
2307eda14cbcSMatt Macy {
23083f9d360cSMartin Matuska 	zio_t *pio = arg;
2309eda14cbcSMatt Macy 	zio_t *cio, *cio_next;
2310eda14cbcSMatt Macy 
2311eda14cbcSMatt Macy 	ASSERT(pio->io_child_type == ZIO_CHILD_LOGICAL);
2312eda14cbcSMatt Macy 	ASSERT(pio->io_orig_stage == ZIO_STAGE_OPEN);
2313eda14cbcSMatt Macy 	ASSERT(pio->io_gang_leader == NULL);
2314eda14cbcSMatt Macy 	ASSERT(pio->io_gang_tree == NULL);
2315eda14cbcSMatt Macy 
2316eda14cbcSMatt Macy 	pio->io_flags = pio->io_orig_flags;
2317eda14cbcSMatt Macy 	pio->io_stage = pio->io_orig_stage;
2318eda14cbcSMatt Macy 	pio->io_pipeline = pio->io_orig_pipeline;
2319eda14cbcSMatt Macy 	pio->io_reexecute = 0;
2320eda14cbcSMatt Macy 	pio->io_flags |= ZIO_FLAG_REEXECUTED;
2321eda14cbcSMatt Macy 	pio->io_pipeline_trace = 0;
2322eda14cbcSMatt Macy 	pio->io_error = 0;
2323eda14cbcSMatt Macy 	for (int w = 0; w < ZIO_WAIT_TYPES; w++)
2324eda14cbcSMatt Macy 		pio->io_state[w] = 0;
2325eda14cbcSMatt Macy 	for (int c = 0; c < ZIO_CHILD_TYPES; c++)
2326eda14cbcSMatt Macy 		pio->io_child_error[c] = 0;
2327eda14cbcSMatt Macy 
2328eda14cbcSMatt Macy 	if (IO_IS_ALLOCATING(pio))
2329eda14cbcSMatt Macy 		BP_ZERO(pio->io_bp);
2330eda14cbcSMatt Macy 
2331eda14cbcSMatt Macy 	/*
2332eda14cbcSMatt Macy 	 * As we reexecute pio's children, new children could be created.
2333eda14cbcSMatt Macy 	 * New children go to the head of pio's io_child_list, however,
2334eda14cbcSMatt Macy 	 * so we will (correctly) not reexecute them.  The key is that
2335eda14cbcSMatt Macy 	 * the remainder of pio's io_child_list, from 'cio_next' onward,
2336eda14cbcSMatt Macy 	 * cannot be affected by any side effects of reexecuting 'cio'.
2337eda14cbcSMatt Macy 	 */
2338eda14cbcSMatt Macy 	zio_link_t *zl = NULL;
2339eda14cbcSMatt Macy 	mutex_enter(&pio->io_lock);
2340eda14cbcSMatt Macy 	for (cio = zio_walk_children(pio, &zl); cio != NULL; cio = cio_next) {
2341eda14cbcSMatt Macy 		cio_next = zio_walk_children(pio, &zl);
2342eda14cbcSMatt Macy 		for (int w = 0; w < ZIO_WAIT_TYPES; w++)
2343eda14cbcSMatt Macy 			pio->io_children[cio->io_child_type][w]++;
2344eda14cbcSMatt Macy 		mutex_exit(&pio->io_lock);
2345eda14cbcSMatt Macy 		zio_reexecute(cio);
2346eda14cbcSMatt Macy 		mutex_enter(&pio->io_lock);
2347eda14cbcSMatt Macy 	}
2348eda14cbcSMatt Macy 	mutex_exit(&pio->io_lock);
2349eda14cbcSMatt Macy 
2350eda14cbcSMatt Macy 	/*
2351eda14cbcSMatt Macy 	 * Now that all children have been reexecuted, execute the parent.
2352eda14cbcSMatt Macy 	 * We don't reexecute "The Godfather" I/O here as it's the
2353eda14cbcSMatt Macy 	 * responsibility of the caller to wait on it.
2354eda14cbcSMatt Macy 	 */
2355eda14cbcSMatt Macy 	if (!(pio->io_flags & ZIO_FLAG_GODFATHER)) {
2356eda14cbcSMatt Macy 		pio->io_queued_timestamp = gethrtime();
2357eda14cbcSMatt Macy 		__zio_execute(pio);
2358eda14cbcSMatt Macy 	}
2359eda14cbcSMatt Macy }
2360eda14cbcSMatt Macy 
2361eda14cbcSMatt Macy void
2362eda14cbcSMatt Macy zio_suspend(spa_t *spa, zio_t *zio, zio_suspend_reason_t reason)
2363eda14cbcSMatt Macy {
2364eda14cbcSMatt Macy 	if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_PANIC)
2365eda14cbcSMatt Macy 		fm_panic("Pool '%s' has encountered an uncorrectable I/O "
2366eda14cbcSMatt Macy 		    "failure and the failure mode property for this pool "
2367eda14cbcSMatt Macy 		    "is set to panic.", spa_name(spa));
2368eda14cbcSMatt Macy 
2369eda14cbcSMatt Macy 	cmn_err(CE_WARN, "Pool '%s' has encountered an uncorrectable I/O "
2370eda14cbcSMatt Macy 	    "failure and has been suspended.\n", spa_name(spa));
2371eda14cbcSMatt Macy 
2372eac7052fSMatt Macy 	(void) zfs_ereport_post(FM_EREPORT_ZFS_IO_FAILURE, spa, NULL,
23732c48331dSMatt Macy 	    NULL, NULL, 0);
2374eda14cbcSMatt Macy 
2375eda14cbcSMatt Macy 	mutex_enter(&spa->spa_suspend_lock);
2376eda14cbcSMatt Macy 
2377eda14cbcSMatt Macy 	if (spa->spa_suspend_zio_root == NULL)
2378eda14cbcSMatt Macy 		spa->spa_suspend_zio_root = zio_root(spa, NULL, NULL,
2379eda14cbcSMatt Macy 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
2380eda14cbcSMatt Macy 		    ZIO_FLAG_GODFATHER);
2381eda14cbcSMatt Macy 
2382eda14cbcSMatt Macy 	spa->spa_suspended = reason;
2383eda14cbcSMatt Macy 
2384eda14cbcSMatt Macy 	if (zio != NULL) {
2385eda14cbcSMatt Macy 		ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER));
2386eda14cbcSMatt Macy 		ASSERT(zio != spa->spa_suspend_zio_root);
2387eda14cbcSMatt Macy 		ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2388eda14cbcSMatt Macy 		ASSERT(zio_unique_parent(zio) == NULL);
2389eda14cbcSMatt Macy 		ASSERT(zio->io_stage == ZIO_STAGE_DONE);
2390eda14cbcSMatt Macy 		zio_add_child(spa->spa_suspend_zio_root, zio);
2391eda14cbcSMatt Macy 	}
2392eda14cbcSMatt Macy 
2393eda14cbcSMatt Macy 	mutex_exit(&spa->spa_suspend_lock);
2394eda14cbcSMatt Macy }
2395eda14cbcSMatt Macy 
2396eda14cbcSMatt Macy int
2397eda14cbcSMatt Macy zio_resume(spa_t *spa)
2398eda14cbcSMatt Macy {
2399eda14cbcSMatt Macy 	zio_t *pio;
2400eda14cbcSMatt Macy 
2401eda14cbcSMatt Macy 	/*
2402eda14cbcSMatt Macy 	 * Reexecute all previously suspended i/o.
2403eda14cbcSMatt Macy 	 */
2404eda14cbcSMatt Macy 	mutex_enter(&spa->spa_suspend_lock);
2405eda14cbcSMatt Macy 	spa->spa_suspended = ZIO_SUSPEND_NONE;
2406eda14cbcSMatt Macy 	cv_broadcast(&spa->spa_suspend_cv);
2407eda14cbcSMatt Macy 	pio = spa->spa_suspend_zio_root;
2408eda14cbcSMatt Macy 	spa->spa_suspend_zio_root = NULL;
2409eda14cbcSMatt Macy 	mutex_exit(&spa->spa_suspend_lock);
2410eda14cbcSMatt Macy 
2411eda14cbcSMatt Macy 	if (pio == NULL)
2412eda14cbcSMatt Macy 		return (0);
2413eda14cbcSMatt Macy 
2414eda14cbcSMatt Macy 	zio_reexecute(pio);
2415eda14cbcSMatt Macy 	return (zio_wait(pio));
2416eda14cbcSMatt Macy }
2417eda14cbcSMatt Macy 
2418eda14cbcSMatt Macy void
2419eda14cbcSMatt Macy zio_resume_wait(spa_t *spa)
2420eda14cbcSMatt Macy {
2421eda14cbcSMatt Macy 	mutex_enter(&spa->spa_suspend_lock);
2422eda14cbcSMatt Macy 	while (spa_suspended(spa))
2423eda14cbcSMatt Macy 		cv_wait(&spa->spa_suspend_cv, &spa->spa_suspend_lock);
2424eda14cbcSMatt Macy 	mutex_exit(&spa->spa_suspend_lock);
2425eda14cbcSMatt Macy }
2426eda14cbcSMatt Macy 
2427eda14cbcSMatt Macy /*
2428eda14cbcSMatt Macy  * ==========================================================================
2429eda14cbcSMatt Macy  * Gang blocks.
2430eda14cbcSMatt Macy  *
2431eda14cbcSMatt Macy  * A gang block is a collection of small blocks that looks to the DMU
2432eda14cbcSMatt Macy  * like one large block.  When zio_dva_allocate() cannot find a block
2433eda14cbcSMatt Macy  * of the requested size, due to either severe fragmentation or the pool
2434eda14cbcSMatt Macy  * being nearly full, it calls zio_write_gang_block() to construct the
2435eda14cbcSMatt Macy  * block from smaller fragments.
2436eda14cbcSMatt Macy  *
2437eda14cbcSMatt Macy  * A gang block consists of a gang header (zio_gbh_phys_t) and up to
2438eda14cbcSMatt Macy  * three (SPA_GBH_NBLKPTRS) gang members.  The gang header is just like
2439eda14cbcSMatt Macy  * an indirect block: it's an array of block pointers.  It consumes
2440eda14cbcSMatt Macy  * only one sector and hence is allocatable regardless of fragmentation.
2441eda14cbcSMatt Macy  * The gang header's bps point to its gang members, which hold the data.
2442eda14cbcSMatt Macy  *
2443eda14cbcSMatt Macy  * Gang blocks are self-checksumming, using the bp's <vdev, offset, txg>
2444eda14cbcSMatt Macy  * as the verifier to ensure uniqueness of the SHA256 checksum.
2445eda14cbcSMatt Macy  * Critically, the gang block bp's blk_cksum is the checksum of the data,
2446eda14cbcSMatt Macy  * not the gang header.  This ensures that data block signatures (needed for
2447eda14cbcSMatt Macy  * deduplication) are independent of how the block is physically stored.
2448eda14cbcSMatt Macy  *
2449eda14cbcSMatt Macy  * Gang blocks can be nested: a gang member may itself be a gang block.
2450eda14cbcSMatt Macy  * Thus every gang block is a tree in which root and all interior nodes are
2451eda14cbcSMatt Macy  * gang headers, and the leaves are normal blocks that contain user data.
2452eda14cbcSMatt Macy  * The root of the gang tree is called the gang leader.
2453eda14cbcSMatt Macy  *
2454eda14cbcSMatt Macy  * To perform any operation (read, rewrite, free, claim) on a gang block,
2455eda14cbcSMatt Macy  * zio_gang_assemble() first assembles the gang tree (minus data leaves)
2456eda14cbcSMatt Macy  * in the io_gang_tree field of the original logical i/o by recursively
2457eda14cbcSMatt Macy  * reading the gang leader and all gang headers below it.  This yields
2458eda14cbcSMatt Macy  * an in-core tree containing the contents of every gang header and the
2459eda14cbcSMatt Macy  * bps for every constituent of the gang block.
2460eda14cbcSMatt Macy  *
2461eda14cbcSMatt Macy  * With the gang tree now assembled, zio_gang_issue() just walks the gang tree
2462eda14cbcSMatt Macy  * and invokes a callback on each bp.  To free a gang block, zio_gang_issue()
2463eda14cbcSMatt Macy  * calls zio_free_gang() -- a trivial wrapper around zio_free() -- for each bp.
2464eda14cbcSMatt Macy  * zio_claim_gang() provides a similarly trivial wrapper for zio_claim().
2465eda14cbcSMatt Macy  * zio_read_gang() is a wrapper around zio_read() that omits reading gang
2466eda14cbcSMatt Macy  * headers, since we already have those in io_gang_tree.  zio_rewrite_gang()
2467eda14cbcSMatt Macy  * performs a zio_rewrite() of the data or, for gang headers, a zio_rewrite()
2468eda14cbcSMatt Macy  * of the gang header plus zio_checksum_compute() of the data to update the
2469eda14cbcSMatt Macy  * gang header's blk_cksum as described above.
2470eda14cbcSMatt Macy  *
2471eda14cbcSMatt Macy  * The two-phase assemble/issue model solves the problem of partial failure --
2472eda14cbcSMatt Macy  * what if you'd freed part of a gang block but then couldn't read the
2473eda14cbcSMatt Macy  * gang header for another part?  Assembling the entire gang tree first
2474eda14cbcSMatt Macy  * ensures that all the necessary gang header I/O has succeeded before
2475eda14cbcSMatt Macy  * starting the actual work of free, claim, or write.  Once the gang tree
2476eda14cbcSMatt Macy  * is assembled, free and claim are in-memory operations that cannot fail.
2477eda14cbcSMatt Macy  *
2478eda14cbcSMatt Macy  * In the event that a gang write fails, zio_dva_unallocate() walks the
2479eda14cbcSMatt Macy  * gang tree to immediately free (i.e. insert back into the space map)
2480eda14cbcSMatt Macy  * everything we've allocated.  This ensures that we don't get ENOSPC
2481eda14cbcSMatt Macy  * errors during repeated suspend/resume cycles due to a flaky device.
2482eda14cbcSMatt Macy  *
2483eda14cbcSMatt Macy  * Gang rewrites only happen during sync-to-convergence.  If we can't assemble
2484eda14cbcSMatt Macy  * the gang tree, we won't modify the block, so we can safely defer the free
2485eda14cbcSMatt Macy  * (knowing that the block is still intact).  If we *can* assemble the gang
2486eda14cbcSMatt Macy  * tree, then even if some of the rewrites fail, zio_dva_unallocate() will free
2487eda14cbcSMatt Macy  * each constituent bp and we can allocate a new block on the next sync pass.
2488eda14cbcSMatt Macy  *
2489eda14cbcSMatt Macy  * In all cases, the gang tree allows complete recovery from partial failure.
2490eda14cbcSMatt Macy  * ==========================================================================
2491eda14cbcSMatt Macy  */
2492eda14cbcSMatt Macy 
2493eda14cbcSMatt Macy static void
2494eda14cbcSMatt Macy zio_gang_issue_func_done(zio_t *zio)
2495eda14cbcSMatt Macy {
2496184c1b94SMartin Matuska 	abd_free(zio->io_abd);
2497eda14cbcSMatt Macy }
2498eda14cbcSMatt Macy 
2499eda14cbcSMatt Macy static zio_t *
2500eda14cbcSMatt Macy zio_read_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data,
2501eda14cbcSMatt Macy     uint64_t offset)
2502eda14cbcSMatt Macy {
2503eda14cbcSMatt Macy 	if (gn != NULL)
2504eda14cbcSMatt Macy 		return (pio);
2505eda14cbcSMatt Macy 
2506eda14cbcSMatt Macy 	return (zio_read(pio, pio->io_spa, bp, abd_get_offset(data, offset),
2507eda14cbcSMatt Macy 	    BP_GET_PSIZE(bp), zio_gang_issue_func_done,
2508eda14cbcSMatt Macy 	    NULL, pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio),
2509eda14cbcSMatt Macy 	    &pio->io_bookmark));
2510eda14cbcSMatt Macy }
2511eda14cbcSMatt Macy 
2512eda14cbcSMatt Macy static zio_t *
2513eda14cbcSMatt Macy zio_rewrite_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data,
2514eda14cbcSMatt Macy     uint64_t offset)
2515eda14cbcSMatt Macy {
2516eda14cbcSMatt Macy 	zio_t *zio;
2517eda14cbcSMatt Macy 
2518eda14cbcSMatt Macy 	if (gn != NULL) {
2519eda14cbcSMatt Macy 		abd_t *gbh_abd =
2520eda14cbcSMatt Macy 		    abd_get_from_buf(gn->gn_gbh, SPA_GANGBLOCKSIZE);
2521eda14cbcSMatt Macy 		zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp,
2522eda14cbcSMatt Macy 		    gbh_abd, SPA_GANGBLOCKSIZE, zio_gang_issue_func_done, NULL,
2523eda14cbcSMatt Macy 		    pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio),
2524eda14cbcSMatt Macy 		    &pio->io_bookmark);
2525eda14cbcSMatt Macy 		/*
2526eda14cbcSMatt Macy 		 * As we rewrite each gang header, the pipeline will compute
2527eda14cbcSMatt Macy 		 * a new gang block header checksum for it; but no one will
2528eda14cbcSMatt Macy 		 * compute a new data checksum, so we do that here.  The one
2529eda14cbcSMatt Macy 		 * exception is the gang leader: the pipeline already computed
2530eda14cbcSMatt Macy 		 * its data checksum because that stage precedes gang assembly.
2531eda14cbcSMatt Macy 		 * (Presently, nothing actually uses interior data checksums;
2532eda14cbcSMatt Macy 		 * this is just good hygiene.)
2533eda14cbcSMatt Macy 		 */
2534eda14cbcSMatt Macy 		if (gn != pio->io_gang_leader->io_gang_tree) {
2535eda14cbcSMatt Macy 			abd_t *buf = abd_get_offset(data, offset);
2536eda14cbcSMatt Macy 
2537eda14cbcSMatt Macy 			zio_checksum_compute(zio, BP_GET_CHECKSUM(bp),
2538eda14cbcSMatt Macy 			    buf, BP_GET_PSIZE(bp));
2539eda14cbcSMatt Macy 
2540184c1b94SMartin Matuska 			abd_free(buf);
2541eda14cbcSMatt Macy 		}
2542eda14cbcSMatt Macy 		/*
2543eda14cbcSMatt Macy 		 * If we are here to damage data for testing purposes,
2544eda14cbcSMatt Macy 		 * leave the GBH alone so that we can detect the damage.
2545eda14cbcSMatt Macy 		 */
2546eda14cbcSMatt Macy 		if (pio->io_gang_leader->io_flags & ZIO_FLAG_INDUCE_DAMAGE)
2547eda14cbcSMatt Macy 			zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES;
2548eda14cbcSMatt Macy 	} else {
2549eda14cbcSMatt Macy 		zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp,
2550eda14cbcSMatt Macy 		    abd_get_offset(data, offset), BP_GET_PSIZE(bp),
2551eda14cbcSMatt Macy 		    zio_gang_issue_func_done, NULL, pio->io_priority,
2552eda14cbcSMatt Macy 		    ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
2553eda14cbcSMatt Macy 	}
2554eda14cbcSMatt Macy 
2555eda14cbcSMatt Macy 	return (zio);
2556eda14cbcSMatt Macy }
2557eda14cbcSMatt Macy 
2558eda14cbcSMatt Macy /* ARGSUSED */
2559eda14cbcSMatt Macy static zio_t *
2560eda14cbcSMatt Macy zio_free_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data,
2561eda14cbcSMatt Macy     uint64_t offset)
2562eda14cbcSMatt Macy {
2563eda14cbcSMatt Macy 	zio_t *zio = zio_free_sync(pio, pio->io_spa, pio->io_txg, bp,
2564eda14cbcSMatt Macy 	    ZIO_GANG_CHILD_FLAGS(pio));
2565eda14cbcSMatt Macy 	if (zio == NULL) {
2566eda14cbcSMatt Macy 		zio = zio_null(pio, pio->io_spa,
2567eda14cbcSMatt Macy 		    NULL, NULL, NULL, ZIO_GANG_CHILD_FLAGS(pio));
2568eda14cbcSMatt Macy 	}
2569eda14cbcSMatt Macy 	return (zio);
2570eda14cbcSMatt Macy }
2571eda14cbcSMatt Macy 
2572eda14cbcSMatt Macy /* ARGSUSED */
2573eda14cbcSMatt Macy static zio_t *
2574eda14cbcSMatt Macy zio_claim_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data,
2575eda14cbcSMatt Macy     uint64_t offset)
2576eda14cbcSMatt Macy {
2577eda14cbcSMatt Macy 	return (zio_claim(pio, pio->io_spa, pio->io_txg, bp,
2578eda14cbcSMatt Macy 	    NULL, NULL, ZIO_GANG_CHILD_FLAGS(pio)));
2579eda14cbcSMatt Macy }
2580eda14cbcSMatt Macy 
2581eda14cbcSMatt Macy static zio_gang_issue_func_t *zio_gang_issue_func[ZIO_TYPES] = {
2582eda14cbcSMatt Macy 	NULL,
2583eda14cbcSMatt Macy 	zio_read_gang,
2584eda14cbcSMatt Macy 	zio_rewrite_gang,
2585eda14cbcSMatt Macy 	zio_free_gang,
2586eda14cbcSMatt Macy 	zio_claim_gang,
2587eda14cbcSMatt Macy 	NULL
2588eda14cbcSMatt Macy };
2589eda14cbcSMatt Macy 
2590eda14cbcSMatt Macy static void zio_gang_tree_assemble_done(zio_t *zio);
2591eda14cbcSMatt Macy 
2592eda14cbcSMatt Macy static zio_gang_node_t *
2593eda14cbcSMatt Macy zio_gang_node_alloc(zio_gang_node_t **gnpp)
2594eda14cbcSMatt Macy {
2595eda14cbcSMatt Macy 	zio_gang_node_t *gn;
2596eda14cbcSMatt Macy 
2597eda14cbcSMatt Macy 	ASSERT(*gnpp == NULL);
2598eda14cbcSMatt Macy 
2599eda14cbcSMatt Macy 	gn = kmem_zalloc(sizeof (*gn), KM_SLEEP);
2600eda14cbcSMatt Macy 	gn->gn_gbh = zio_buf_alloc(SPA_GANGBLOCKSIZE);
2601eda14cbcSMatt Macy 	*gnpp = gn;
2602eda14cbcSMatt Macy 
2603eda14cbcSMatt Macy 	return (gn);
2604eda14cbcSMatt Macy }
2605eda14cbcSMatt Macy 
2606eda14cbcSMatt Macy static void
2607eda14cbcSMatt Macy zio_gang_node_free(zio_gang_node_t **gnpp)
2608eda14cbcSMatt Macy {
2609eda14cbcSMatt Macy 	zio_gang_node_t *gn = *gnpp;
2610eda14cbcSMatt Macy 
2611eda14cbcSMatt Macy 	for (int g = 0; g < SPA_GBH_NBLKPTRS; g++)
2612eda14cbcSMatt Macy 		ASSERT(gn->gn_child[g] == NULL);
2613eda14cbcSMatt Macy 
2614eda14cbcSMatt Macy 	zio_buf_free(gn->gn_gbh, SPA_GANGBLOCKSIZE);
2615eda14cbcSMatt Macy 	kmem_free(gn, sizeof (*gn));
2616eda14cbcSMatt Macy 	*gnpp = NULL;
2617eda14cbcSMatt Macy }
2618eda14cbcSMatt Macy 
2619eda14cbcSMatt Macy static void
2620eda14cbcSMatt Macy zio_gang_tree_free(zio_gang_node_t **gnpp)
2621eda14cbcSMatt Macy {
2622eda14cbcSMatt Macy 	zio_gang_node_t *gn = *gnpp;
2623eda14cbcSMatt Macy 
2624eda14cbcSMatt Macy 	if (gn == NULL)
2625eda14cbcSMatt Macy 		return;
2626eda14cbcSMatt Macy 
2627eda14cbcSMatt Macy 	for (int g = 0; g < SPA_GBH_NBLKPTRS; g++)
2628eda14cbcSMatt Macy 		zio_gang_tree_free(&gn->gn_child[g]);
2629eda14cbcSMatt Macy 
2630eda14cbcSMatt Macy 	zio_gang_node_free(gnpp);
2631eda14cbcSMatt Macy }
2632eda14cbcSMatt Macy 
2633eda14cbcSMatt Macy static void
2634eda14cbcSMatt Macy zio_gang_tree_assemble(zio_t *gio, blkptr_t *bp, zio_gang_node_t **gnpp)
2635eda14cbcSMatt Macy {
2636eda14cbcSMatt Macy 	zio_gang_node_t *gn = zio_gang_node_alloc(gnpp);
2637eda14cbcSMatt Macy 	abd_t *gbh_abd = abd_get_from_buf(gn->gn_gbh, SPA_GANGBLOCKSIZE);
2638eda14cbcSMatt Macy 
2639eda14cbcSMatt Macy 	ASSERT(gio->io_gang_leader == gio);
2640eda14cbcSMatt Macy 	ASSERT(BP_IS_GANG(bp));
2641eda14cbcSMatt Macy 
2642eda14cbcSMatt Macy 	zio_nowait(zio_read(gio, gio->io_spa, bp, gbh_abd, SPA_GANGBLOCKSIZE,
2643eda14cbcSMatt Macy 	    zio_gang_tree_assemble_done, gn, gio->io_priority,
2644eda14cbcSMatt Macy 	    ZIO_GANG_CHILD_FLAGS(gio), &gio->io_bookmark));
2645eda14cbcSMatt Macy }
2646eda14cbcSMatt Macy 
2647eda14cbcSMatt Macy static void
2648eda14cbcSMatt Macy zio_gang_tree_assemble_done(zio_t *zio)
2649eda14cbcSMatt Macy {
2650eda14cbcSMatt Macy 	zio_t *gio = zio->io_gang_leader;
2651eda14cbcSMatt Macy 	zio_gang_node_t *gn = zio->io_private;
2652eda14cbcSMatt Macy 	blkptr_t *bp = zio->io_bp;
2653eda14cbcSMatt Macy 
2654eda14cbcSMatt Macy 	ASSERT(gio == zio_unique_parent(zio));
2655eda14cbcSMatt Macy 	ASSERT(zio->io_child_count == 0);
2656eda14cbcSMatt Macy 
2657eda14cbcSMatt Macy 	if (zio->io_error)
2658eda14cbcSMatt Macy 		return;
2659eda14cbcSMatt Macy 
2660eda14cbcSMatt Macy 	/* this ABD was created from a linear buf in zio_gang_tree_assemble */
2661eda14cbcSMatt Macy 	if (BP_SHOULD_BYTESWAP(bp))
2662eda14cbcSMatt Macy 		byteswap_uint64_array(abd_to_buf(zio->io_abd), zio->io_size);
2663eda14cbcSMatt Macy 
2664eda14cbcSMatt Macy 	ASSERT3P(abd_to_buf(zio->io_abd), ==, gn->gn_gbh);
2665eda14cbcSMatt Macy 	ASSERT(zio->io_size == SPA_GANGBLOCKSIZE);
2666eda14cbcSMatt Macy 	ASSERT(gn->gn_gbh->zg_tail.zec_magic == ZEC_MAGIC);
2667eda14cbcSMatt Macy 
2668184c1b94SMartin Matuska 	abd_free(zio->io_abd);
2669eda14cbcSMatt Macy 
2670eda14cbcSMatt Macy 	for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
2671eda14cbcSMatt Macy 		blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g];
2672eda14cbcSMatt Macy 		if (!BP_IS_GANG(gbp))
2673eda14cbcSMatt Macy 			continue;
2674eda14cbcSMatt Macy 		zio_gang_tree_assemble(gio, gbp, &gn->gn_child[g]);
2675eda14cbcSMatt Macy 	}
2676eda14cbcSMatt Macy }
2677eda14cbcSMatt Macy 
2678eda14cbcSMatt Macy static void
2679eda14cbcSMatt Macy zio_gang_tree_issue(zio_t *pio, zio_gang_node_t *gn, blkptr_t *bp, abd_t *data,
2680eda14cbcSMatt Macy     uint64_t offset)
2681eda14cbcSMatt Macy {
2682eda14cbcSMatt Macy 	zio_t *gio = pio->io_gang_leader;
2683eda14cbcSMatt Macy 	zio_t *zio;
2684eda14cbcSMatt Macy 
2685eda14cbcSMatt Macy 	ASSERT(BP_IS_GANG(bp) == !!gn);
2686eda14cbcSMatt Macy 	ASSERT(BP_GET_CHECKSUM(bp) == BP_GET_CHECKSUM(gio->io_bp));
2687eda14cbcSMatt Macy 	ASSERT(BP_GET_LSIZE(bp) == BP_GET_PSIZE(bp) || gn == gio->io_gang_tree);
2688eda14cbcSMatt Macy 
2689eda14cbcSMatt Macy 	/*
2690eda14cbcSMatt Macy 	 * If you're a gang header, your data is in gn->gn_gbh.
2691eda14cbcSMatt Macy 	 * If you're a gang member, your data is in 'data' and gn == NULL.
2692eda14cbcSMatt Macy 	 */
2693eda14cbcSMatt Macy 	zio = zio_gang_issue_func[gio->io_type](pio, bp, gn, data, offset);
2694eda14cbcSMatt Macy 
2695eda14cbcSMatt Macy 	if (gn != NULL) {
2696eda14cbcSMatt Macy 		ASSERT(gn->gn_gbh->zg_tail.zec_magic == ZEC_MAGIC);
2697eda14cbcSMatt Macy 
2698eda14cbcSMatt Macy 		for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
2699eda14cbcSMatt Macy 			blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g];
2700eda14cbcSMatt Macy 			if (BP_IS_HOLE(gbp))
2701eda14cbcSMatt Macy 				continue;
2702eda14cbcSMatt Macy 			zio_gang_tree_issue(zio, gn->gn_child[g], gbp, data,
2703eda14cbcSMatt Macy 			    offset);
2704eda14cbcSMatt Macy 			offset += BP_GET_PSIZE(gbp);
2705eda14cbcSMatt Macy 		}
2706eda14cbcSMatt Macy 	}
2707eda14cbcSMatt Macy 
2708eda14cbcSMatt Macy 	if (gn == gio->io_gang_tree)
2709eda14cbcSMatt Macy 		ASSERT3U(gio->io_size, ==, offset);
2710eda14cbcSMatt Macy 
2711eda14cbcSMatt Macy 	if (zio != pio)
2712eda14cbcSMatt Macy 		zio_nowait(zio);
2713eda14cbcSMatt Macy }
2714eda14cbcSMatt Macy 
2715eda14cbcSMatt Macy static zio_t *
2716eda14cbcSMatt Macy zio_gang_assemble(zio_t *zio)
2717eda14cbcSMatt Macy {
2718eda14cbcSMatt Macy 	blkptr_t *bp = zio->io_bp;
2719eda14cbcSMatt Macy 
2720eda14cbcSMatt Macy 	ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == NULL);
2721eda14cbcSMatt Macy 	ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
2722eda14cbcSMatt Macy 
2723eda14cbcSMatt Macy 	zio->io_gang_leader = zio;
2724eda14cbcSMatt Macy 
2725eda14cbcSMatt Macy 	zio_gang_tree_assemble(zio, bp, &zio->io_gang_tree);
2726eda14cbcSMatt Macy 
2727eda14cbcSMatt Macy 	return (zio);
2728eda14cbcSMatt Macy }
2729eda14cbcSMatt Macy 
2730eda14cbcSMatt Macy static zio_t *
2731eda14cbcSMatt Macy zio_gang_issue(zio_t *zio)
2732eda14cbcSMatt Macy {
2733eda14cbcSMatt Macy 	blkptr_t *bp = zio->io_bp;
2734eda14cbcSMatt Macy 
2735eda14cbcSMatt Macy 	if (zio_wait_for_children(zio, ZIO_CHILD_GANG_BIT, ZIO_WAIT_DONE)) {
2736eda14cbcSMatt Macy 		return (NULL);
2737eda14cbcSMatt Macy 	}
2738eda14cbcSMatt Macy 
2739eda14cbcSMatt Macy 	ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == zio);
2740eda14cbcSMatt Macy 	ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
2741eda14cbcSMatt Macy 
2742eda14cbcSMatt Macy 	if (zio->io_child_error[ZIO_CHILD_GANG] == 0)
2743eda14cbcSMatt Macy 		zio_gang_tree_issue(zio, zio->io_gang_tree, bp, zio->io_abd,
2744eda14cbcSMatt Macy 		    0);
2745eda14cbcSMatt Macy 	else
2746eda14cbcSMatt Macy 		zio_gang_tree_free(&zio->io_gang_tree);
2747eda14cbcSMatt Macy 
2748eda14cbcSMatt Macy 	zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2749eda14cbcSMatt Macy 
2750eda14cbcSMatt Macy 	return (zio);
2751eda14cbcSMatt Macy }
2752eda14cbcSMatt Macy 
2753eda14cbcSMatt Macy static void
2754eda14cbcSMatt Macy zio_write_gang_member_ready(zio_t *zio)
2755eda14cbcSMatt Macy {
2756eda14cbcSMatt Macy 	zio_t *pio = zio_unique_parent(zio);
2757eda14cbcSMatt Macy 	dva_t *cdva = zio->io_bp->blk_dva;
2758eda14cbcSMatt Macy 	dva_t *pdva = pio->io_bp->blk_dva;
2759eda14cbcSMatt Macy 	uint64_t asize;
2760eda14cbcSMatt Macy 	zio_t *gio __maybe_unused = zio->io_gang_leader;
2761eda14cbcSMatt Macy 
2762eda14cbcSMatt Macy 	if (BP_IS_HOLE(zio->io_bp))
2763eda14cbcSMatt Macy 		return;
2764eda14cbcSMatt Macy 
2765eda14cbcSMatt Macy 	ASSERT(BP_IS_HOLE(&zio->io_bp_orig));
2766eda14cbcSMatt Macy 
2767eda14cbcSMatt Macy 	ASSERT(zio->io_child_type == ZIO_CHILD_GANG);
2768eda14cbcSMatt Macy 	ASSERT3U(zio->io_prop.zp_copies, ==, gio->io_prop.zp_copies);
2769eda14cbcSMatt Macy 	ASSERT3U(zio->io_prop.zp_copies, <=, BP_GET_NDVAS(zio->io_bp));
2770eda14cbcSMatt Macy 	ASSERT3U(pio->io_prop.zp_copies, <=, BP_GET_NDVAS(pio->io_bp));
2771eda14cbcSMatt Macy 	ASSERT3U(BP_GET_NDVAS(zio->io_bp), <=, BP_GET_NDVAS(pio->io_bp));
2772eda14cbcSMatt Macy 
2773eda14cbcSMatt Macy 	mutex_enter(&pio->io_lock);
2774eda14cbcSMatt Macy 	for (int d = 0; d < BP_GET_NDVAS(zio->io_bp); d++) {
2775eda14cbcSMatt Macy 		ASSERT(DVA_GET_GANG(&pdva[d]));
2776eda14cbcSMatt Macy 		asize = DVA_GET_ASIZE(&pdva[d]);
2777eda14cbcSMatt Macy 		asize += DVA_GET_ASIZE(&cdva[d]);
2778eda14cbcSMatt Macy 		DVA_SET_ASIZE(&pdva[d], asize);
2779eda14cbcSMatt Macy 	}
2780eda14cbcSMatt Macy 	mutex_exit(&pio->io_lock);
2781eda14cbcSMatt Macy }
2782eda14cbcSMatt Macy 
2783eda14cbcSMatt Macy static void
2784eda14cbcSMatt Macy zio_write_gang_done(zio_t *zio)
2785eda14cbcSMatt Macy {
2786eda14cbcSMatt Macy 	/*
2787eda14cbcSMatt Macy 	 * The io_abd field will be NULL for a zio with no data.  The io_flags
2788eda14cbcSMatt Macy 	 * will initially have the ZIO_FLAG_NODATA bit flag set, but we can't
2789eda14cbcSMatt Macy 	 * check for it here as it is cleared in zio_ready.
2790eda14cbcSMatt Macy 	 */
2791eda14cbcSMatt Macy 	if (zio->io_abd != NULL)
2792184c1b94SMartin Matuska 		abd_free(zio->io_abd);
2793eda14cbcSMatt Macy }
2794eda14cbcSMatt Macy 
2795eda14cbcSMatt Macy static zio_t *
2796184c1b94SMartin Matuska zio_write_gang_block(zio_t *pio, metaslab_class_t *mc)
2797eda14cbcSMatt Macy {
2798eda14cbcSMatt Macy 	spa_t *spa = pio->io_spa;
2799eda14cbcSMatt Macy 	blkptr_t *bp = pio->io_bp;
2800eda14cbcSMatt Macy 	zio_t *gio = pio->io_gang_leader;
2801eda14cbcSMatt Macy 	zio_t *zio;
2802eda14cbcSMatt Macy 	zio_gang_node_t *gn, **gnpp;
2803eda14cbcSMatt Macy 	zio_gbh_phys_t *gbh;
2804eda14cbcSMatt Macy 	abd_t *gbh_abd;
2805eda14cbcSMatt Macy 	uint64_t txg = pio->io_txg;
2806eda14cbcSMatt Macy 	uint64_t resid = pio->io_size;
2807eda14cbcSMatt Macy 	uint64_t lsize;
2808eda14cbcSMatt Macy 	int copies = gio->io_prop.zp_copies;
2809eda14cbcSMatt Macy 	int gbh_copies;
2810eda14cbcSMatt Macy 	zio_prop_t zp;
2811eda14cbcSMatt Macy 	int error;
2812eda14cbcSMatt Macy 	boolean_t has_data = !(pio->io_flags & ZIO_FLAG_NODATA);
2813eda14cbcSMatt Macy 
2814eda14cbcSMatt Macy 	/*
2815eda14cbcSMatt Macy 	 * encrypted blocks need DVA[2] free so encrypted gang headers can't
2816eda14cbcSMatt Macy 	 * have a third copy.
2817eda14cbcSMatt Macy 	 */
2818eda14cbcSMatt Macy 	gbh_copies = MIN(copies + 1, spa_max_replication(spa));
2819eda14cbcSMatt Macy 	if (gio->io_prop.zp_encrypt && gbh_copies >= SPA_DVAS_PER_BP)
2820eda14cbcSMatt Macy 		gbh_copies = SPA_DVAS_PER_BP - 1;
2821eda14cbcSMatt Macy 
2822eda14cbcSMatt Macy 	int flags = METASLAB_HINTBP_FAVOR | METASLAB_GANG_HEADER;
2823eda14cbcSMatt Macy 	if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
2824eda14cbcSMatt Macy 		ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
2825eda14cbcSMatt Macy 		ASSERT(has_data);
2826eda14cbcSMatt Macy 
2827eda14cbcSMatt Macy 		flags |= METASLAB_ASYNC_ALLOC;
28287877fdebSMatt Macy 		VERIFY(zfs_refcount_held(&mc->mc_allocator[pio->io_allocator].
28297877fdebSMatt Macy 		    mca_alloc_slots, pio));
2830eda14cbcSMatt Macy 
2831eda14cbcSMatt Macy 		/*
2832eda14cbcSMatt Macy 		 * The logical zio has already placed a reservation for
2833eda14cbcSMatt Macy 		 * 'copies' allocation slots but gang blocks may require
2834eda14cbcSMatt Macy 		 * additional copies. These additional copies
2835eda14cbcSMatt Macy 		 * (i.e. gbh_copies - copies) are guaranteed to succeed
2836eda14cbcSMatt Macy 		 * since metaslab_class_throttle_reserve() always allows
2837eda14cbcSMatt Macy 		 * additional reservations for gang blocks.
2838eda14cbcSMatt Macy 		 */
2839eda14cbcSMatt Macy 		VERIFY(metaslab_class_throttle_reserve(mc, gbh_copies - copies,
2840eda14cbcSMatt Macy 		    pio->io_allocator, pio, flags));
2841eda14cbcSMatt Macy 	}
2842eda14cbcSMatt Macy 
2843eda14cbcSMatt Macy 	error = metaslab_alloc(spa, mc, SPA_GANGBLOCKSIZE,
2844eda14cbcSMatt Macy 	    bp, gbh_copies, txg, pio == gio ? NULL : gio->io_bp, flags,
2845eda14cbcSMatt Macy 	    &pio->io_alloc_list, pio, pio->io_allocator);
2846eda14cbcSMatt Macy 	if (error) {
2847eda14cbcSMatt Macy 		if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
2848eda14cbcSMatt Macy 			ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
2849eda14cbcSMatt Macy 			ASSERT(has_data);
2850eda14cbcSMatt Macy 
2851eda14cbcSMatt Macy 			/*
2852eda14cbcSMatt Macy 			 * If we failed to allocate the gang block header then
2853eda14cbcSMatt Macy 			 * we remove any additional allocation reservations that
2854eda14cbcSMatt Macy 			 * we placed here. The original reservation will
2855eda14cbcSMatt Macy 			 * be removed when the logical I/O goes to the ready
2856eda14cbcSMatt Macy 			 * stage.
2857eda14cbcSMatt Macy 			 */
2858eda14cbcSMatt Macy 			metaslab_class_throttle_unreserve(mc,
2859eda14cbcSMatt Macy 			    gbh_copies - copies, pio->io_allocator, pio);
2860eda14cbcSMatt Macy 		}
2861eda14cbcSMatt Macy 
2862eda14cbcSMatt Macy 		pio->io_error = error;
2863eda14cbcSMatt Macy 		return (pio);
2864eda14cbcSMatt Macy 	}
2865eda14cbcSMatt Macy 
2866eda14cbcSMatt Macy 	if (pio == gio) {
2867eda14cbcSMatt Macy 		gnpp = &gio->io_gang_tree;
2868eda14cbcSMatt Macy 	} else {
2869eda14cbcSMatt Macy 		gnpp = pio->io_private;
2870eda14cbcSMatt Macy 		ASSERT(pio->io_ready == zio_write_gang_member_ready);
2871eda14cbcSMatt Macy 	}
2872eda14cbcSMatt Macy 
2873eda14cbcSMatt Macy 	gn = zio_gang_node_alloc(gnpp);
2874eda14cbcSMatt Macy 	gbh = gn->gn_gbh;
2875eda14cbcSMatt Macy 	bzero(gbh, SPA_GANGBLOCKSIZE);
2876eda14cbcSMatt Macy 	gbh_abd = abd_get_from_buf(gbh, SPA_GANGBLOCKSIZE);
2877eda14cbcSMatt Macy 
2878eda14cbcSMatt Macy 	/*
2879eda14cbcSMatt Macy 	 * Create the gang header.
2880eda14cbcSMatt Macy 	 */
2881eda14cbcSMatt Macy 	zio = zio_rewrite(pio, spa, txg, bp, gbh_abd, SPA_GANGBLOCKSIZE,
2882eda14cbcSMatt Macy 	    zio_write_gang_done, NULL, pio->io_priority,
2883eda14cbcSMatt Macy 	    ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
2884eda14cbcSMatt Macy 
2885eda14cbcSMatt Macy 	/*
2886eda14cbcSMatt Macy 	 * Create and nowait the gang children.
2887eda14cbcSMatt Macy 	 */
2888eda14cbcSMatt Macy 	for (int g = 0; resid != 0; resid -= lsize, g++) {
2889eda14cbcSMatt Macy 		lsize = P2ROUNDUP(resid / (SPA_GBH_NBLKPTRS - g),
2890eda14cbcSMatt Macy 		    SPA_MINBLOCKSIZE);
2891eda14cbcSMatt Macy 		ASSERT(lsize >= SPA_MINBLOCKSIZE && lsize <= resid);
2892eda14cbcSMatt Macy 
2893eda14cbcSMatt Macy 		zp.zp_checksum = gio->io_prop.zp_checksum;
2894eda14cbcSMatt Macy 		zp.zp_compress = ZIO_COMPRESS_OFF;
2895eda14cbcSMatt Macy 		zp.zp_complevel = gio->io_prop.zp_complevel;
2896eda14cbcSMatt Macy 		zp.zp_type = DMU_OT_NONE;
2897eda14cbcSMatt Macy 		zp.zp_level = 0;
2898eda14cbcSMatt Macy 		zp.zp_copies = gio->io_prop.zp_copies;
2899eda14cbcSMatt Macy 		zp.zp_dedup = B_FALSE;
2900eda14cbcSMatt Macy 		zp.zp_dedup_verify = B_FALSE;
2901eda14cbcSMatt Macy 		zp.zp_nopwrite = B_FALSE;
2902eda14cbcSMatt Macy 		zp.zp_encrypt = gio->io_prop.zp_encrypt;
2903eda14cbcSMatt Macy 		zp.zp_byteorder = gio->io_prop.zp_byteorder;
2904eda14cbcSMatt Macy 		bzero(zp.zp_salt, ZIO_DATA_SALT_LEN);
2905eda14cbcSMatt Macy 		bzero(zp.zp_iv, ZIO_DATA_IV_LEN);
2906eda14cbcSMatt Macy 		bzero(zp.zp_mac, ZIO_DATA_MAC_LEN);
2907eda14cbcSMatt Macy 
2908eda14cbcSMatt Macy 		zio_t *cio = zio_write(zio, spa, txg, &gbh->zg_blkptr[g],
2909eda14cbcSMatt Macy 		    has_data ? abd_get_offset(pio->io_abd, pio->io_size -
2910eda14cbcSMatt Macy 		    resid) : NULL, lsize, lsize, &zp,
2911eda14cbcSMatt Macy 		    zio_write_gang_member_ready, NULL, NULL,
2912eda14cbcSMatt Macy 		    zio_write_gang_done, &gn->gn_child[g], pio->io_priority,
2913eda14cbcSMatt Macy 		    ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
2914eda14cbcSMatt Macy 
2915eda14cbcSMatt Macy 		if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
2916eda14cbcSMatt Macy 			ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
2917eda14cbcSMatt Macy 			ASSERT(has_data);
2918eda14cbcSMatt Macy 
2919eda14cbcSMatt Macy 			/*
2920eda14cbcSMatt Macy 			 * Gang children won't throttle but we should
2921eda14cbcSMatt Macy 			 * account for their work, so reserve an allocation
2922eda14cbcSMatt Macy 			 * slot for them here.
2923eda14cbcSMatt Macy 			 */
2924eda14cbcSMatt Macy 			VERIFY(metaslab_class_throttle_reserve(mc,
2925eda14cbcSMatt Macy 			    zp.zp_copies, cio->io_allocator, cio, flags));
2926eda14cbcSMatt Macy 		}
2927eda14cbcSMatt Macy 		zio_nowait(cio);
2928eda14cbcSMatt Macy 	}
2929eda14cbcSMatt Macy 
2930eda14cbcSMatt Macy 	/*
2931eda14cbcSMatt Macy 	 * Set pio's pipeline to just wait for zio to finish.
2932eda14cbcSMatt Macy 	 */
2933eda14cbcSMatt Macy 	pio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2934eda14cbcSMatt Macy 
2935eda14cbcSMatt Macy 	/*
2936eda14cbcSMatt Macy 	 * We didn't allocate this bp, so make sure it doesn't get unmarked.
2937eda14cbcSMatt Macy 	 */
2938eda14cbcSMatt Macy 	pio->io_flags &= ~ZIO_FLAG_FASTWRITE;
2939eda14cbcSMatt Macy 
2940eda14cbcSMatt Macy 	zio_nowait(zio);
2941eda14cbcSMatt Macy 
2942eda14cbcSMatt Macy 	return (pio);
2943eda14cbcSMatt Macy }
2944eda14cbcSMatt Macy 
2945eda14cbcSMatt Macy /*
2946eda14cbcSMatt Macy  * The zio_nop_write stage in the pipeline determines if allocating a
2947eda14cbcSMatt Macy  * new bp is necessary.  The nopwrite feature can handle writes in
2948eda14cbcSMatt Macy  * either syncing or open context (i.e. zil writes) and as a result is
2949eda14cbcSMatt Macy  * mutually exclusive with dedup.
2950eda14cbcSMatt Macy  *
2951eda14cbcSMatt Macy  * By leveraging a cryptographically secure checksum, such as SHA256, we
2952eda14cbcSMatt Macy  * can compare the checksums of the new data and the old to determine if
2953eda14cbcSMatt Macy  * allocating a new block is required.  Note that our requirements for
2954eda14cbcSMatt Macy  * cryptographic strength are fairly weak: there can't be any accidental
2955eda14cbcSMatt Macy  * hash collisions, but we don't need to be secure against intentional
2956eda14cbcSMatt Macy  * (malicious) collisions.  To trigger a nopwrite, you have to be able
2957eda14cbcSMatt Macy  * to write the file to begin with, and triggering an incorrect (hash
2958eda14cbcSMatt Macy  * collision) nopwrite is no worse than simply writing to the file.
2959eda14cbcSMatt Macy  * That said, there are no known attacks against the checksum algorithms
2960eda14cbcSMatt Macy  * used for nopwrite, assuming that the salt and the checksums
2961eda14cbcSMatt Macy  * themselves remain secret.
2962eda14cbcSMatt Macy  */
2963eda14cbcSMatt Macy static zio_t *
2964eda14cbcSMatt Macy zio_nop_write(zio_t *zio)
2965eda14cbcSMatt Macy {
2966eda14cbcSMatt Macy 	blkptr_t *bp = zio->io_bp;
2967eda14cbcSMatt Macy 	blkptr_t *bp_orig = &zio->io_bp_orig;
2968eda14cbcSMatt Macy 	zio_prop_t *zp = &zio->io_prop;
2969eda14cbcSMatt Macy 
2970eda14cbcSMatt Macy 	ASSERT(BP_GET_LEVEL(bp) == 0);
2971eda14cbcSMatt Macy 	ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
2972eda14cbcSMatt Macy 	ASSERT(zp->zp_nopwrite);
2973eda14cbcSMatt Macy 	ASSERT(!zp->zp_dedup);
2974eda14cbcSMatt Macy 	ASSERT(zio->io_bp_override == NULL);
2975eda14cbcSMatt Macy 	ASSERT(IO_IS_ALLOCATING(zio));
2976eda14cbcSMatt Macy 
2977eda14cbcSMatt Macy 	/*
2978eda14cbcSMatt Macy 	 * Check to see if the original bp and the new bp have matching
2979eda14cbcSMatt Macy 	 * characteristics (i.e. same checksum, compression algorithms, etc).
2980eda14cbcSMatt Macy 	 * If they don't then just continue with the pipeline which will
2981eda14cbcSMatt Macy 	 * allocate a new bp.
2982eda14cbcSMatt Macy 	 */
2983eda14cbcSMatt Macy 	if (BP_IS_HOLE(bp_orig) ||
2984eda14cbcSMatt Macy 	    !(zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_flags &
2985eda14cbcSMatt Macy 	    ZCHECKSUM_FLAG_NOPWRITE) ||
2986eda14cbcSMatt Macy 	    BP_IS_ENCRYPTED(bp) || BP_IS_ENCRYPTED(bp_orig) ||
2987eda14cbcSMatt Macy 	    BP_GET_CHECKSUM(bp) != BP_GET_CHECKSUM(bp_orig) ||
2988eda14cbcSMatt Macy 	    BP_GET_COMPRESS(bp) != BP_GET_COMPRESS(bp_orig) ||
2989eda14cbcSMatt Macy 	    BP_GET_DEDUP(bp) != BP_GET_DEDUP(bp_orig) ||
2990eda14cbcSMatt Macy 	    zp->zp_copies != BP_GET_NDVAS(bp_orig))
2991eda14cbcSMatt Macy 		return (zio);
2992eda14cbcSMatt Macy 
2993eda14cbcSMatt Macy 	/*
2994eda14cbcSMatt Macy 	 * If the checksums match then reset the pipeline so that we
2995eda14cbcSMatt Macy 	 * avoid allocating a new bp and issuing any I/O.
2996eda14cbcSMatt Macy 	 */
2997eda14cbcSMatt Macy 	if (ZIO_CHECKSUM_EQUAL(bp->blk_cksum, bp_orig->blk_cksum)) {
2998eda14cbcSMatt Macy 		ASSERT(zio_checksum_table[zp->zp_checksum].ci_flags &
2999eda14cbcSMatt Macy 		    ZCHECKSUM_FLAG_NOPWRITE);
3000eda14cbcSMatt Macy 		ASSERT3U(BP_GET_PSIZE(bp), ==, BP_GET_PSIZE(bp_orig));
3001eda14cbcSMatt Macy 		ASSERT3U(BP_GET_LSIZE(bp), ==, BP_GET_LSIZE(bp_orig));
3002eda14cbcSMatt Macy 		ASSERT(zp->zp_compress != ZIO_COMPRESS_OFF);
3003eda14cbcSMatt Macy 		ASSERT(bcmp(&bp->blk_prop, &bp_orig->blk_prop,
3004eda14cbcSMatt Macy 		    sizeof (uint64_t)) == 0);
3005eda14cbcSMatt Macy 
3006eda14cbcSMatt Macy 		/*
3007eda14cbcSMatt Macy 		 * If we're overwriting a block that is currently on an
3008eda14cbcSMatt Macy 		 * indirect vdev, then ignore the nopwrite request and
3009eda14cbcSMatt Macy 		 * allow a new block to be allocated on a concrete vdev.
3010eda14cbcSMatt Macy 		 */
3011eda14cbcSMatt Macy 		spa_config_enter(zio->io_spa, SCL_VDEV, FTAG, RW_READER);
3012eda14cbcSMatt Macy 		vdev_t *tvd = vdev_lookup_top(zio->io_spa,
3013eda14cbcSMatt Macy 		    DVA_GET_VDEV(&bp->blk_dva[0]));
3014eda14cbcSMatt Macy 		if (tvd->vdev_ops == &vdev_indirect_ops) {
3015eda14cbcSMatt Macy 			spa_config_exit(zio->io_spa, SCL_VDEV, FTAG);
3016eda14cbcSMatt Macy 			return (zio);
3017eda14cbcSMatt Macy 		}
3018eda14cbcSMatt Macy 		spa_config_exit(zio->io_spa, SCL_VDEV, FTAG);
3019eda14cbcSMatt Macy 
3020eda14cbcSMatt Macy 		*bp = *bp_orig;
3021eda14cbcSMatt Macy 		zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
3022eda14cbcSMatt Macy 		zio->io_flags |= ZIO_FLAG_NOPWRITE;
3023eda14cbcSMatt Macy 	}
3024eda14cbcSMatt Macy 
3025eda14cbcSMatt Macy 	return (zio);
3026eda14cbcSMatt Macy }
3027eda14cbcSMatt Macy 
3028eda14cbcSMatt Macy /*
3029eda14cbcSMatt Macy  * ==========================================================================
3030eda14cbcSMatt Macy  * Dedup
3031eda14cbcSMatt Macy  * ==========================================================================
3032eda14cbcSMatt Macy  */
3033eda14cbcSMatt Macy static void
3034eda14cbcSMatt Macy zio_ddt_child_read_done(zio_t *zio)
3035eda14cbcSMatt Macy {
3036eda14cbcSMatt Macy 	blkptr_t *bp = zio->io_bp;
3037eda14cbcSMatt Macy 	ddt_entry_t *dde = zio->io_private;
3038eda14cbcSMatt Macy 	ddt_phys_t *ddp;
3039eda14cbcSMatt Macy 	zio_t *pio = zio_unique_parent(zio);
3040eda14cbcSMatt Macy 
3041eda14cbcSMatt Macy 	mutex_enter(&pio->io_lock);
3042eda14cbcSMatt Macy 	ddp = ddt_phys_select(dde, bp);
3043eda14cbcSMatt Macy 	if (zio->io_error == 0)
3044eda14cbcSMatt Macy 		ddt_phys_clear(ddp);	/* this ddp doesn't need repair */
3045eda14cbcSMatt Macy 
3046eda14cbcSMatt Macy 	if (zio->io_error == 0 && dde->dde_repair_abd == NULL)
3047eda14cbcSMatt Macy 		dde->dde_repair_abd = zio->io_abd;
3048eda14cbcSMatt Macy 	else
3049eda14cbcSMatt Macy 		abd_free(zio->io_abd);
3050eda14cbcSMatt Macy 	mutex_exit(&pio->io_lock);
3051eda14cbcSMatt Macy }
3052eda14cbcSMatt Macy 
3053eda14cbcSMatt Macy static zio_t *
3054eda14cbcSMatt Macy zio_ddt_read_start(zio_t *zio)
3055eda14cbcSMatt Macy {
3056eda14cbcSMatt Macy 	blkptr_t *bp = zio->io_bp;
3057eda14cbcSMatt Macy 
3058eda14cbcSMatt Macy 	ASSERT(BP_GET_DEDUP(bp));
3059eda14cbcSMatt Macy 	ASSERT(BP_GET_PSIZE(bp) == zio->io_size);
3060eda14cbcSMatt Macy 	ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
3061eda14cbcSMatt Macy 
3062eda14cbcSMatt Macy 	if (zio->io_child_error[ZIO_CHILD_DDT]) {
3063eda14cbcSMatt Macy 		ddt_t *ddt = ddt_select(zio->io_spa, bp);
3064eda14cbcSMatt Macy 		ddt_entry_t *dde = ddt_repair_start(ddt, bp);
3065eda14cbcSMatt Macy 		ddt_phys_t *ddp = dde->dde_phys;
3066eda14cbcSMatt Macy 		ddt_phys_t *ddp_self = ddt_phys_select(dde, bp);
3067eda14cbcSMatt Macy 		blkptr_t blk;
3068eda14cbcSMatt Macy 
3069eda14cbcSMatt Macy 		ASSERT(zio->io_vsd == NULL);
3070eda14cbcSMatt Macy 		zio->io_vsd = dde;
3071eda14cbcSMatt Macy 
3072eda14cbcSMatt Macy 		if (ddp_self == NULL)
3073eda14cbcSMatt Macy 			return (zio);
3074eda14cbcSMatt Macy 
3075eda14cbcSMatt Macy 		for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
3076eda14cbcSMatt Macy 			if (ddp->ddp_phys_birth == 0 || ddp == ddp_self)
3077eda14cbcSMatt Macy 				continue;
3078eda14cbcSMatt Macy 			ddt_bp_create(ddt->ddt_checksum, &dde->dde_key, ddp,
3079eda14cbcSMatt Macy 			    &blk);
3080eda14cbcSMatt Macy 			zio_nowait(zio_read(zio, zio->io_spa, &blk,
3081eda14cbcSMatt Macy 			    abd_alloc_for_io(zio->io_size, B_TRUE),
3082eda14cbcSMatt Macy 			    zio->io_size, zio_ddt_child_read_done, dde,
3083eda14cbcSMatt Macy 			    zio->io_priority, ZIO_DDT_CHILD_FLAGS(zio) |
3084eda14cbcSMatt Macy 			    ZIO_FLAG_DONT_PROPAGATE, &zio->io_bookmark));
3085eda14cbcSMatt Macy 		}
3086eda14cbcSMatt Macy 		return (zio);
3087eda14cbcSMatt Macy 	}
3088eda14cbcSMatt Macy 
3089eda14cbcSMatt Macy 	zio_nowait(zio_read(zio, zio->io_spa, bp,
3090eda14cbcSMatt Macy 	    zio->io_abd, zio->io_size, NULL, NULL, zio->io_priority,
3091eda14cbcSMatt Macy 	    ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark));
3092eda14cbcSMatt Macy 
3093eda14cbcSMatt Macy 	return (zio);
3094eda14cbcSMatt Macy }
3095eda14cbcSMatt Macy 
3096eda14cbcSMatt Macy static zio_t *
3097eda14cbcSMatt Macy zio_ddt_read_done(zio_t *zio)
3098eda14cbcSMatt Macy {
3099eda14cbcSMatt Macy 	blkptr_t *bp = zio->io_bp;
3100eda14cbcSMatt Macy 
3101eda14cbcSMatt Macy 	if (zio_wait_for_children(zio, ZIO_CHILD_DDT_BIT, ZIO_WAIT_DONE)) {
3102eda14cbcSMatt Macy 		return (NULL);
3103eda14cbcSMatt Macy 	}
3104eda14cbcSMatt Macy 
3105eda14cbcSMatt Macy 	ASSERT(BP_GET_DEDUP(bp));
3106eda14cbcSMatt Macy 	ASSERT(BP_GET_PSIZE(bp) == zio->io_size);
3107eda14cbcSMatt Macy 	ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
3108eda14cbcSMatt Macy 
3109eda14cbcSMatt Macy 	if (zio->io_child_error[ZIO_CHILD_DDT]) {
3110eda14cbcSMatt Macy 		ddt_t *ddt = ddt_select(zio->io_spa, bp);
3111eda14cbcSMatt Macy 		ddt_entry_t *dde = zio->io_vsd;
3112eda14cbcSMatt Macy 		if (ddt == NULL) {
3113eda14cbcSMatt Macy 			ASSERT(spa_load_state(zio->io_spa) != SPA_LOAD_NONE);
3114eda14cbcSMatt Macy 			return (zio);
3115eda14cbcSMatt Macy 		}
3116eda14cbcSMatt Macy 		if (dde == NULL) {
3117eda14cbcSMatt Macy 			zio->io_stage = ZIO_STAGE_DDT_READ_START >> 1;
3118eda14cbcSMatt Macy 			zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE);
3119eda14cbcSMatt Macy 			return (NULL);
3120eda14cbcSMatt Macy 		}
3121eda14cbcSMatt Macy 		if (dde->dde_repair_abd != NULL) {
3122eda14cbcSMatt Macy 			abd_copy(zio->io_abd, dde->dde_repair_abd,
3123eda14cbcSMatt Macy 			    zio->io_size);
3124eda14cbcSMatt Macy 			zio->io_child_error[ZIO_CHILD_DDT] = 0;
3125eda14cbcSMatt Macy 		}
3126eda14cbcSMatt Macy 		ddt_repair_done(ddt, dde);
3127eda14cbcSMatt Macy 		zio->io_vsd = NULL;
3128eda14cbcSMatt Macy 	}
3129eda14cbcSMatt Macy 
3130eda14cbcSMatt Macy 	ASSERT(zio->io_vsd == NULL);
3131eda14cbcSMatt Macy 
3132eda14cbcSMatt Macy 	return (zio);
3133eda14cbcSMatt Macy }
3134eda14cbcSMatt Macy 
3135eda14cbcSMatt Macy static boolean_t
3136eda14cbcSMatt Macy zio_ddt_collision(zio_t *zio, ddt_t *ddt, ddt_entry_t *dde)
3137eda14cbcSMatt Macy {
3138eda14cbcSMatt Macy 	spa_t *spa = zio->io_spa;
3139eda14cbcSMatt Macy 	boolean_t do_raw = !!(zio->io_flags & ZIO_FLAG_RAW);
3140eda14cbcSMatt Macy 
3141eda14cbcSMatt Macy 	ASSERT(!(zio->io_bp_override && do_raw));
3142eda14cbcSMatt Macy 
3143eda14cbcSMatt Macy 	/*
3144eda14cbcSMatt Macy 	 * Note: we compare the original data, not the transformed data,
3145eda14cbcSMatt Macy 	 * because when zio->io_bp is an override bp, we will not have
3146eda14cbcSMatt Macy 	 * pushed the I/O transforms.  That's an important optimization
3147eda14cbcSMatt Macy 	 * because otherwise we'd compress/encrypt all dmu_sync() data twice.
3148eda14cbcSMatt Macy 	 * However, we should never get a raw, override zio so in these
3149eda14cbcSMatt Macy 	 * cases we can compare the io_abd directly. This is useful because
3150eda14cbcSMatt Macy 	 * it allows us to do dedup verification even if we don't have access
3151eda14cbcSMatt Macy 	 * to the original data (for instance, if the encryption keys aren't
3152eda14cbcSMatt Macy 	 * loaded).
3153eda14cbcSMatt Macy 	 */
3154eda14cbcSMatt Macy 
3155eda14cbcSMatt Macy 	for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) {
3156eda14cbcSMatt Macy 		zio_t *lio = dde->dde_lead_zio[p];
3157eda14cbcSMatt Macy 
3158eda14cbcSMatt Macy 		if (lio != NULL && do_raw) {
3159eda14cbcSMatt Macy 			return (lio->io_size != zio->io_size ||
3160eda14cbcSMatt Macy 			    abd_cmp(zio->io_abd, lio->io_abd) != 0);
3161eda14cbcSMatt Macy 		} else if (lio != NULL) {
3162eda14cbcSMatt Macy 			return (lio->io_orig_size != zio->io_orig_size ||
3163eda14cbcSMatt Macy 			    abd_cmp(zio->io_orig_abd, lio->io_orig_abd) != 0);
3164eda14cbcSMatt Macy 		}
3165eda14cbcSMatt Macy 	}
3166eda14cbcSMatt Macy 
3167eda14cbcSMatt Macy 	for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) {
3168eda14cbcSMatt Macy 		ddt_phys_t *ddp = &dde->dde_phys[p];
3169eda14cbcSMatt Macy 
3170eda14cbcSMatt Macy 		if (ddp->ddp_phys_birth != 0 && do_raw) {
3171eda14cbcSMatt Macy 			blkptr_t blk = *zio->io_bp;
3172eda14cbcSMatt Macy 			uint64_t psize;
3173eda14cbcSMatt Macy 			abd_t *tmpabd;
3174eda14cbcSMatt Macy 			int error;
3175eda14cbcSMatt Macy 
3176eda14cbcSMatt Macy 			ddt_bp_fill(ddp, &blk, ddp->ddp_phys_birth);
3177eda14cbcSMatt Macy 			psize = BP_GET_PSIZE(&blk);
3178eda14cbcSMatt Macy 
3179eda14cbcSMatt Macy 			if (psize != zio->io_size)
3180eda14cbcSMatt Macy 				return (B_TRUE);
3181eda14cbcSMatt Macy 
3182eda14cbcSMatt Macy 			ddt_exit(ddt);
3183eda14cbcSMatt Macy 
3184eda14cbcSMatt Macy 			tmpabd = abd_alloc_for_io(psize, B_TRUE);
3185eda14cbcSMatt Macy 
3186eda14cbcSMatt Macy 			error = zio_wait(zio_read(NULL, spa, &blk, tmpabd,
3187eda14cbcSMatt Macy 			    psize, NULL, NULL, ZIO_PRIORITY_SYNC_READ,
3188eda14cbcSMatt Macy 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
3189eda14cbcSMatt Macy 			    ZIO_FLAG_RAW, &zio->io_bookmark));
3190eda14cbcSMatt Macy 
3191eda14cbcSMatt Macy 			if (error == 0) {
3192eda14cbcSMatt Macy 				if (abd_cmp(tmpabd, zio->io_abd) != 0)
3193eda14cbcSMatt Macy 					error = SET_ERROR(ENOENT);
3194eda14cbcSMatt Macy 			}
3195eda14cbcSMatt Macy 
3196eda14cbcSMatt Macy 			abd_free(tmpabd);
3197eda14cbcSMatt Macy 			ddt_enter(ddt);
3198eda14cbcSMatt Macy 			return (error != 0);
3199eda14cbcSMatt Macy 		} else if (ddp->ddp_phys_birth != 0) {
3200eda14cbcSMatt Macy 			arc_buf_t *abuf = NULL;
3201eda14cbcSMatt Macy 			arc_flags_t aflags = ARC_FLAG_WAIT;
3202eda14cbcSMatt Macy 			blkptr_t blk = *zio->io_bp;
3203eda14cbcSMatt Macy 			int error;
3204eda14cbcSMatt Macy 
3205eda14cbcSMatt Macy 			ddt_bp_fill(ddp, &blk, ddp->ddp_phys_birth);
3206eda14cbcSMatt Macy 
3207eda14cbcSMatt Macy 			if (BP_GET_LSIZE(&blk) != zio->io_orig_size)
3208eda14cbcSMatt Macy 				return (B_TRUE);
3209eda14cbcSMatt Macy 
3210eda14cbcSMatt Macy 			ddt_exit(ddt);
3211eda14cbcSMatt Macy 
3212eda14cbcSMatt Macy 			error = arc_read(NULL, spa, &blk,
3213eda14cbcSMatt Macy 			    arc_getbuf_func, &abuf, ZIO_PRIORITY_SYNC_READ,
3214eda14cbcSMatt Macy 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
3215eda14cbcSMatt Macy 			    &aflags, &zio->io_bookmark);
3216eda14cbcSMatt Macy 
3217eda14cbcSMatt Macy 			if (error == 0) {
3218eda14cbcSMatt Macy 				if (abd_cmp_buf(zio->io_orig_abd, abuf->b_data,
3219eda14cbcSMatt Macy 				    zio->io_orig_size) != 0)
3220eda14cbcSMatt Macy 					error = SET_ERROR(ENOENT);
3221eda14cbcSMatt Macy 				arc_buf_destroy(abuf, &abuf);
3222eda14cbcSMatt Macy 			}
3223eda14cbcSMatt Macy 
3224eda14cbcSMatt Macy 			ddt_enter(ddt);
3225eda14cbcSMatt Macy 			return (error != 0);
3226eda14cbcSMatt Macy 		}
3227eda14cbcSMatt Macy 	}
3228eda14cbcSMatt Macy 
3229eda14cbcSMatt Macy 	return (B_FALSE);
3230eda14cbcSMatt Macy }
3231eda14cbcSMatt Macy 
3232eda14cbcSMatt Macy static void
3233eda14cbcSMatt Macy zio_ddt_child_write_ready(zio_t *zio)
3234eda14cbcSMatt Macy {
3235eda14cbcSMatt Macy 	int p = zio->io_prop.zp_copies;
3236eda14cbcSMatt Macy 	ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp);
3237eda14cbcSMatt Macy 	ddt_entry_t *dde = zio->io_private;
3238eda14cbcSMatt Macy 	ddt_phys_t *ddp = &dde->dde_phys[p];
3239eda14cbcSMatt Macy 	zio_t *pio;
3240eda14cbcSMatt Macy 
3241eda14cbcSMatt Macy 	if (zio->io_error)
3242eda14cbcSMatt Macy 		return;
3243eda14cbcSMatt Macy 
3244eda14cbcSMatt Macy 	ddt_enter(ddt);
3245eda14cbcSMatt Macy 
3246eda14cbcSMatt Macy 	ASSERT(dde->dde_lead_zio[p] == zio);
3247eda14cbcSMatt Macy 
3248eda14cbcSMatt Macy 	ddt_phys_fill(ddp, zio->io_bp);
3249eda14cbcSMatt Macy 
3250eda14cbcSMatt Macy 	zio_link_t *zl = NULL;
3251eda14cbcSMatt Macy 	while ((pio = zio_walk_parents(zio, &zl)) != NULL)
3252eda14cbcSMatt Macy 		ddt_bp_fill(ddp, pio->io_bp, zio->io_txg);
3253eda14cbcSMatt Macy 
3254eda14cbcSMatt Macy 	ddt_exit(ddt);
3255eda14cbcSMatt Macy }
3256eda14cbcSMatt Macy 
3257eda14cbcSMatt Macy static void
3258eda14cbcSMatt Macy zio_ddt_child_write_done(zio_t *zio)
3259eda14cbcSMatt Macy {
3260eda14cbcSMatt Macy 	int p = zio->io_prop.zp_copies;
3261eda14cbcSMatt Macy 	ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp);
3262eda14cbcSMatt Macy 	ddt_entry_t *dde = zio->io_private;
3263eda14cbcSMatt Macy 	ddt_phys_t *ddp = &dde->dde_phys[p];
3264eda14cbcSMatt Macy 
3265eda14cbcSMatt Macy 	ddt_enter(ddt);
3266eda14cbcSMatt Macy 
3267eda14cbcSMatt Macy 	ASSERT(ddp->ddp_refcnt == 0);
3268eda14cbcSMatt Macy 	ASSERT(dde->dde_lead_zio[p] == zio);
3269eda14cbcSMatt Macy 	dde->dde_lead_zio[p] = NULL;
3270eda14cbcSMatt Macy 
3271eda14cbcSMatt Macy 	if (zio->io_error == 0) {
3272eda14cbcSMatt Macy 		zio_link_t *zl = NULL;
3273eda14cbcSMatt Macy 		while (zio_walk_parents(zio, &zl) != NULL)
3274eda14cbcSMatt Macy 			ddt_phys_addref(ddp);
3275eda14cbcSMatt Macy 	} else {
3276eda14cbcSMatt Macy 		ddt_phys_clear(ddp);
3277eda14cbcSMatt Macy 	}
3278eda14cbcSMatt Macy 
3279eda14cbcSMatt Macy 	ddt_exit(ddt);
3280eda14cbcSMatt Macy }
3281eda14cbcSMatt Macy 
3282eda14cbcSMatt Macy static zio_t *
3283eda14cbcSMatt Macy zio_ddt_write(zio_t *zio)
3284eda14cbcSMatt Macy {
3285eda14cbcSMatt Macy 	spa_t *spa = zio->io_spa;
3286eda14cbcSMatt Macy 	blkptr_t *bp = zio->io_bp;
3287eda14cbcSMatt Macy 	uint64_t txg = zio->io_txg;
3288eda14cbcSMatt Macy 	zio_prop_t *zp = &zio->io_prop;
3289eda14cbcSMatt Macy 	int p = zp->zp_copies;
3290eda14cbcSMatt Macy 	zio_t *cio = NULL;
3291eda14cbcSMatt Macy 	ddt_t *ddt = ddt_select(spa, bp);
3292eda14cbcSMatt Macy 	ddt_entry_t *dde;
3293eda14cbcSMatt Macy 	ddt_phys_t *ddp;
3294eda14cbcSMatt Macy 
3295eda14cbcSMatt Macy 	ASSERT(BP_GET_DEDUP(bp));
3296eda14cbcSMatt Macy 	ASSERT(BP_GET_CHECKSUM(bp) == zp->zp_checksum);
3297eda14cbcSMatt Macy 	ASSERT(BP_IS_HOLE(bp) || zio->io_bp_override);
3298eda14cbcSMatt Macy 	ASSERT(!(zio->io_bp_override && (zio->io_flags & ZIO_FLAG_RAW)));
3299eda14cbcSMatt Macy 
3300eda14cbcSMatt Macy 	ddt_enter(ddt);
3301eda14cbcSMatt Macy 	dde = ddt_lookup(ddt, bp, B_TRUE);
3302eda14cbcSMatt Macy 	ddp = &dde->dde_phys[p];
3303eda14cbcSMatt Macy 
3304eda14cbcSMatt Macy 	if (zp->zp_dedup_verify && zio_ddt_collision(zio, ddt, dde)) {
3305eda14cbcSMatt Macy 		/*
3306eda14cbcSMatt Macy 		 * If we're using a weak checksum, upgrade to a strong checksum
3307eda14cbcSMatt Macy 		 * and try again.  If we're already using a strong checksum,
3308eda14cbcSMatt Macy 		 * we can't resolve it, so just convert to an ordinary write.
3309eda14cbcSMatt Macy 		 * (And automatically e-mail a paper to Nature?)
3310eda14cbcSMatt Macy 		 */
3311eda14cbcSMatt Macy 		if (!(zio_checksum_table[zp->zp_checksum].ci_flags &
3312eda14cbcSMatt Macy 		    ZCHECKSUM_FLAG_DEDUP)) {
3313eda14cbcSMatt Macy 			zp->zp_checksum = spa_dedup_checksum(spa);
3314eda14cbcSMatt Macy 			zio_pop_transforms(zio);
3315eda14cbcSMatt Macy 			zio->io_stage = ZIO_STAGE_OPEN;
3316eda14cbcSMatt Macy 			BP_ZERO(bp);
3317eda14cbcSMatt Macy 		} else {
3318eda14cbcSMatt Macy 			zp->zp_dedup = B_FALSE;
3319eda14cbcSMatt Macy 			BP_SET_DEDUP(bp, B_FALSE);
3320eda14cbcSMatt Macy 		}
3321eda14cbcSMatt Macy 		ASSERT(!BP_GET_DEDUP(bp));
3322eda14cbcSMatt Macy 		zio->io_pipeline = ZIO_WRITE_PIPELINE;
3323eda14cbcSMatt Macy 		ddt_exit(ddt);
3324eda14cbcSMatt Macy 		return (zio);
3325eda14cbcSMatt Macy 	}
3326eda14cbcSMatt Macy 
3327eda14cbcSMatt Macy 	if (ddp->ddp_phys_birth != 0 || dde->dde_lead_zio[p] != NULL) {
3328eda14cbcSMatt Macy 		if (ddp->ddp_phys_birth != 0)
3329eda14cbcSMatt Macy 			ddt_bp_fill(ddp, bp, txg);
3330eda14cbcSMatt Macy 		if (dde->dde_lead_zio[p] != NULL)
3331eda14cbcSMatt Macy 			zio_add_child(zio, dde->dde_lead_zio[p]);
3332eda14cbcSMatt Macy 		else
3333eda14cbcSMatt Macy 			ddt_phys_addref(ddp);
3334eda14cbcSMatt Macy 	} else if (zio->io_bp_override) {
3335eda14cbcSMatt Macy 		ASSERT(bp->blk_birth == txg);
3336eda14cbcSMatt Macy 		ASSERT(BP_EQUAL(bp, zio->io_bp_override));
3337eda14cbcSMatt Macy 		ddt_phys_fill(ddp, bp);
3338eda14cbcSMatt Macy 		ddt_phys_addref(ddp);
3339eda14cbcSMatt Macy 	} else {
3340eda14cbcSMatt Macy 		cio = zio_write(zio, spa, txg, bp, zio->io_orig_abd,
3341eda14cbcSMatt Macy 		    zio->io_orig_size, zio->io_orig_size, zp,
3342eda14cbcSMatt Macy 		    zio_ddt_child_write_ready, NULL, NULL,
3343eda14cbcSMatt Macy 		    zio_ddt_child_write_done, dde, zio->io_priority,
3344eda14cbcSMatt Macy 		    ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark);
3345eda14cbcSMatt Macy 
3346eda14cbcSMatt Macy 		zio_push_transform(cio, zio->io_abd, zio->io_size, 0, NULL);
3347eda14cbcSMatt Macy 		dde->dde_lead_zio[p] = cio;
3348eda14cbcSMatt Macy 	}
3349eda14cbcSMatt Macy 
3350eda14cbcSMatt Macy 	ddt_exit(ddt);
3351eda14cbcSMatt Macy 
3352eda14cbcSMatt Macy 	zio_nowait(cio);
3353eda14cbcSMatt Macy 
3354eda14cbcSMatt Macy 	return (zio);
3355eda14cbcSMatt Macy }
3356eda14cbcSMatt Macy 
3357eda14cbcSMatt Macy ddt_entry_t *freedde; /* for debugging */
3358eda14cbcSMatt Macy 
3359eda14cbcSMatt Macy static zio_t *
3360eda14cbcSMatt Macy zio_ddt_free(zio_t *zio)
3361eda14cbcSMatt Macy {
3362eda14cbcSMatt Macy 	spa_t *spa = zio->io_spa;
3363eda14cbcSMatt Macy 	blkptr_t *bp = zio->io_bp;
3364eda14cbcSMatt Macy 	ddt_t *ddt = ddt_select(spa, bp);
3365eda14cbcSMatt Macy 	ddt_entry_t *dde;
3366eda14cbcSMatt Macy 	ddt_phys_t *ddp;
3367eda14cbcSMatt Macy 
3368eda14cbcSMatt Macy 	ASSERT(BP_GET_DEDUP(bp));
3369eda14cbcSMatt Macy 	ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
3370eda14cbcSMatt Macy 
3371eda14cbcSMatt Macy 	ddt_enter(ddt);
3372eda14cbcSMatt Macy 	freedde = dde = ddt_lookup(ddt, bp, B_TRUE);
3373eda14cbcSMatt Macy 	if (dde) {
3374eda14cbcSMatt Macy 		ddp = ddt_phys_select(dde, bp);
3375eda14cbcSMatt Macy 		if (ddp)
3376eda14cbcSMatt Macy 			ddt_phys_decref(ddp);
3377eda14cbcSMatt Macy 	}
3378eda14cbcSMatt Macy 	ddt_exit(ddt);
3379eda14cbcSMatt Macy 
3380eda14cbcSMatt Macy 	return (zio);
3381eda14cbcSMatt Macy }
3382eda14cbcSMatt Macy 
3383eda14cbcSMatt Macy /*
3384eda14cbcSMatt Macy  * ==========================================================================
3385eda14cbcSMatt Macy  * Allocate and free blocks
3386eda14cbcSMatt Macy  * ==========================================================================
3387eda14cbcSMatt Macy  */
3388eda14cbcSMatt Macy 
3389eda14cbcSMatt Macy static zio_t *
3390eda14cbcSMatt Macy zio_io_to_allocate(spa_t *spa, int allocator)
3391eda14cbcSMatt Macy {
3392eda14cbcSMatt Macy 	zio_t *zio;
3393eda14cbcSMatt Macy 
33943f9d360cSMartin Matuska 	ASSERT(MUTEX_HELD(&spa->spa_allocs[allocator].spaa_lock));
3395eda14cbcSMatt Macy 
33963f9d360cSMartin Matuska 	zio = avl_first(&spa->spa_allocs[allocator].spaa_tree);
3397eda14cbcSMatt Macy 	if (zio == NULL)
3398eda14cbcSMatt Macy 		return (NULL);
3399eda14cbcSMatt Macy 
3400eda14cbcSMatt Macy 	ASSERT(IO_IS_ALLOCATING(zio));
3401eda14cbcSMatt Macy 
3402eda14cbcSMatt Macy 	/*
3403eda14cbcSMatt Macy 	 * Try to place a reservation for this zio. If we're unable to
3404eda14cbcSMatt Macy 	 * reserve then we throttle.
3405eda14cbcSMatt Macy 	 */
3406eda14cbcSMatt Macy 	ASSERT3U(zio->io_allocator, ==, allocator);
3407eda14cbcSMatt Macy 	if (!metaslab_class_throttle_reserve(zio->io_metaslab_class,
34083f9d360cSMartin Matuska 	    zio->io_prop.zp_copies, allocator, zio, 0)) {
3409eda14cbcSMatt Macy 		return (NULL);
3410eda14cbcSMatt Macy 	}
3411eda14cbcSMatt Macy 
34123f9d360cSMartin Matuska 	avl_remove(&spa->spa_allocs[allocator].spaa_tree, zio);
3413eda14cbcSMatt Macy 	ASSERT3U(zio->io_stage, <, ZIO_STAGE_DVA_ALLOCATE);
3414eda14cbcSMatt Macy 
3415eda14cbcSMatt Macy 	return (zio);
3416eda14cbcSMatt Macy }
3417eda14cbcSMatt Macy 
3418eda14cbcSMatt Macy static zio_t *
3419eda14cbcSMatt Macy zio_dva_throttle(zio_t *zio)
3420eda14cbcSMatt Macy {
3421eda14cbcSMatt Macy 	spa_t *spa = zio->io_spa;
3422eda14cbcSMatt Macy 	zio_t *nio;
3423eda14cbcSMatt Macy 	metaslab_class_t *mc;
3424eda14cbcSMatt Macy 
3425eda14cbcSMatt Macy 	/* locate an appropriate allocation class */
3426eda14cbcSMatt Macy 	mc = spa_preferred_class(spa, zio->io_size, zio->io_prop.zp_type,
3427eda14cbcSMatt Macy 	    zio->io_prop.zp_level, zio->io_prop.zp_zpl_smallblk);
3428eda14cbcSMatt Macy 
3429eda14cbcSMatt Macy 	if (zio->io_priority == ZIO_PRIORITY_SYNC_WRITE ||
3430eda14cbcSMatt Macy 	    !mc->mc_alloc_throttle_enabled ||
3431eda14cbcSMatt Macy 	    zio->io_child_type == ZIO_CHILD_GANG ||
3432eda14cbcSMatt Macy 	    zio->io_flags & ZIO_FLAG_NODATA) {
3433eda14cbcSMatt Macy 		return (zio);
3434eda14cbcSMatt Macy 	}
3435eda14cbcSMatt Macy 
34363f9d360cSMartin Matuska 	ASSERT(zio->io_type == ZIO_TYPE_WRITE);
3437eda14cbcSMatt Macy 	ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
3438eda14cbcSMatt Macy 	ASSERT3U(zio->io_queued_timestamp, >, 0);
3439eda14cbcSMatt Macy 	ASSERT(zio->io_stage == ZIO_STAGE_DVA_THROTTLE);
3440eda14cbcSMatt Macy 
3441eda14cbcSMatt Macy 	zbookmark_phys_t *bm = &zio->io_bookmark;
3442eda14cbcSMatt Macy 	/*
3443eda14cbcSMatt Macy 	 * We want to try to use as many allocators as possible to help improve
3444eda14cbcSMatt Macy 	 * performance, but we also want logically adjacent IOs to be physically
3445eda14cbcSMatt Macy 	 * adjacent to improve sequential read performance. We chunk each object
3446eda14cbcSMatt Macy 	 * into 2^20 block regions, and then hash based on the objset, object,
3447eda14cbcSMatt Macy 	 * level, and region to accomplish both of these goals.
3448eda14cbcSMatt Macy 	 */
34493f9d360cSMartin Matuska 	int allocator = (uint_t)cityhash4(bm->zb_objset, bm->zb_object,
3450eda14cbcSMatt Macy 	    bm->zb_level, bm->zb_blkid >> 20) % spa->spa_alloc_count;
34513f9d360cSMartin Matuska 	zio->io_allocator = allocator;
3452eda14cbcSMatt Macy 	zio->io_metaslab_class = mc;
34533f9d360cSMartin Matuska 	mutex_enter(&spa->spa_allocs[allocator].spaa_lock);
34543f9d360cSMartin Matuska 	avl_add(&spa->spa_allocs[allocator].spaa_tree, zio);
34553f9d360cSMartin Matuska 	nio = zio_io_to_allocate(spa, allocator);
34563f9d360cSMartin Matuska 	mutex_exit(&spa->spa_allocs[allocator].spaa_lock);
3457eda14cbcSMatt Macy 	return (nio);
3458eda14cbcSMatt Macy }
3459eda14cbcSMatt Macy 
3460eda14cbcSMatt Macy static void
3461eda14cbcSMatt Macy zio_allocate_dispatch(spa_t *spa, int allocator)
3462eda14cbcSMatt Macy {
3463eda14cbcSMatt Macy 	zio_t *zio;
3464eda14cbcSMatt Macy 
34653f9d360cSMartin Matuska 	mutex_enter(&spa->spa_allocs[allocator].spaa_lock);
3466eda14cbcSMatt Macy 	zio = zio_io_to_allocate(spa, allocator);
34673f9d360cSMartin Matuska 	mutex_exit(&spa->spa_allocs[allocator].spaa_lock);
3468eda14cbcSMatt Macy 	if (zio == NULL)
3469eda14cbcSMatt Macy 		return;
3470eda14cbcSMatt Macy 
3471eda14cbcSMatt Macy 	ASSERT3U(zio->io_stage, ==, ZIO_STAGE_DVA_THROTTLE);
3472eda14cbcSMatt Macy 	ASSERT0(zio->io_error);
3473eda14cbcSMatt Macy 	zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_TRUE);
3474eda14cbcSMatt Macy }
3475eda14cbcSMatt Macy 
3476eda14cbcSMatt Macy static zio_t *
3477eda14cbcSMatt Macy zio_dva_allocate(zio_t *zio)
3478eda14cbcSMatt Macy {
3479eda14cbcSMatt Macy 	spa_t *spa = zio->io_spa;
3480eda14cbcSMatt Macy 	metaslab_class_t *mc;
3481eda14cbcSMatt Macy 	blkptr_t *bp = zio->io_bp;
3482eda14cbcSMatt Macy 	int error;
3483eda14cbcSMatt Macy 	int flags = 0;
3484eda14cbcSMatt Macy 
3485eda14cbcSMatt Macy 	if (zio->io_gang_leader == NULL) {
3486eda14cbcSMatt Macy 		ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
3487eda14cbcSMatt Macy 		zio->io_gang_leader = zio;
3488eda14cbcSMatt Macy 	}
3489eda14cbcSMatt Macy 
3490eda14cbcSMatt Macy 	ASSERT(BP_IS_HOLE(bp));
3491eda14cbcSMatt Macy 	ASSERT0(BP_GET_NDVAS(bp));
3492eda14cbcSMatt Macy 	ASSERT3U(zio->io_prop.zp_copies, >, 0);
3493eda14cbcSMatt Macy 	ASSERT3U(zio->io_prop.zp_copies, <=, spa_max_replication(spa));
3494eda14cbcSMatt Macy 	ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp));
3495eda14cbcSMatt Macy 
3496eda14cbcSMatt Macy 	flags |= (zio->io_flags & ZIO_FLAG_FASTWRITE) ? METASLAB_FASTWRITE : 0;
3497eda14cbcSMatt Macy 	if (zio->io_flags & ZIO_FLAG_NODATA)
3498eda14cbcSMatt Macy 		flags |= METASLAB_DONT_THROTTLE;
3499eda14cbcSMatt Macy 	if (zio->io_flags & ZIO_FLAG_GANG_CHILD)
3500eda14cbcSMatt Macy 		flags |= METASLAB_GANG_CHILD;
3501eda14cbcSMatt Macy 	if (zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE)
3502eda14cbcSMatt Macy 		flags |= METASLAB_ASYNC_ALLOC;
3503eda14cbcSMatt Macy 
3504eda14cbcSMatt Macy 	/*
3505eda14cbcSMatt Macy 	 * if not already chosen, locate an appropriate allocation class
3506eda14cbcSMatt Macy 	 */
3507eda14cbcSMatt Macy 	mc = zio->io_metaslab_class;
3508eda14cbcSMatt Macy 	if (mc == NULL) {
3509eda14cbcSMatt Macy 		mc = spa_preferred_class(spa, zio->io_size,
3510eda14cbcSMatt Macy 		    zio->io_prop.zp_type, zio->io_prop.zp_level,
3511eda14cbcSMatt Macy 		    zio->io_prop.zp_zpl_smallblk);
3512eda14cbcSMatt Macy 		zio->io_metaslab_class = mc;
3513eda14cbcSMatt Macy 	}
3514eda14cbcSMatt Macy 
3515184c1b94SMartin Matuska 	/*
3516184c1b94SMartin Matuska 	 * Try allocating the block in the usual metaslab class.
3517184c1b94SMartin Matuska 	 * If that's full, allocate it in the normal class.
3518184c1b94SMartin Matuska 	 * If that's full, allocate as a gang block,
3519184c1b94SMartin Matuska 	 * and if all are full, the allocation fails (which shouldn't happen).
3520184c1b94SMartin Matuska 	 *
3521184c1b94SMartin Matuska 	 * Note that we do not fall back on embedded slog (ZIL) space, to
3522184c1b94SMartin Matuska 	 * preserve unfragmented slog space, which is critical for decent
3523184c1b94SMartin Matuska 	 * sync write performance.  If a log allocation fails, we will fall
3524184c1b94SMartin Matuska 	 * back to spa_sync() which is abysmal for performance.
3525184c1b94SMartin Matuska 	 */
3526eda14cbcSMatt Macy 	error = metaslab_alloc(spa, mc, zio->io_size, bp,
3527eda14cbcSMatt Macy 	    zio->io_prop.zp_copies, zio->io_txg, NULL, flags,
3528eda14cbcSMatt Macy 	    &zio->io_alloc_list, zio, zio->io_allocator);
3529eda14cbcSMatt Macy 
3530eda14cbcSMatt Macy 	/*
3531eda14cbcSMatt Macy 	 * Fallback to normal class when an alloc class is full
3532eda14cbcSMatt Macy 	 */
3533eda14cbcSMatt Macy 	if (error == ENOSPC && mc != spa_normal_class(spa)) {
3534eda14cbcSMatt Macy 		/*
3535eda14cbcSMatt Macy 		 * If throttling, transfer reservation over to normal class.
3536eda14cbcSMatt Macy 		 * The io_allocator slot can remain the same even though we
3537eda14cbcSMatt Macy 		 * are switching classes.
3538eda14cbcSMatt Macy 		 */
3539eda14cbcSMatt Macy 		if (mc->mc_alloc_throttle_enabled &&
3540eda14cbcSMatt Macy 		    (zio->io_flags & ZIO_FLAG_IO_ALLOCATING)) {
3541eda14cbcSMatt Macy 			metaslab_class_throttle_unreserve(mc,
3542eda14cbcSMatt Macy 			    zio->io_prop.zp_copies, zio->io_allocator, zio);
3543eda14cbcSMatt Macy 			zio->io_flags &= ~ZIO_FLAG_IO_ALLOCATING;
3544eda14cbcSMatt Macy 
3545184c1b94SMartin Matuska 			VERIFY(metaslab_class_throttle_reserve(
3546184c1b94SMartin Matuska 			    spa_normal_class(spa),
3547eda14cbcSMatt Macy 			    zio->io_prop.zp_copies, zio->io_allocator, zio,
3548eda14cbcSMatt Macy 			    flags | METASLAB_MUST_RESERVE));
3549eda14cbcSMatt Macy 		}
3550184c1b94SMartin Matuska 		zio->io_metaslab_class = mc = spa_normal_class(spa);
3551184c1b94SMartin Matuska 		if (zfs_flags & ZFS_DEBUG_METASLAB_ALLOC) {
3552184c1b94SMartin Matuska 			zfs_dbgmsg("%s: metaslab allocation failure, "
3553184c1b94SMartin Matuska 			    "trying normal class: zio %px, size %llu, error %d",
355433b8c039SMartin Matuska 			    spa_name(spa), zio, (u_longlong_t)zio->io_size,
355533b8c039SMartin Matuska 			    error);
3556184c1b94SMartin Matuska 		}
3557eda14cbcSMatt Macy 
3558eda14cbcSMatt Macy 		error = metaslab_alloc(spa, mc, zio->io_size, bp,
3559eda14cbcSMatt Macy 		    zio->io_prop.zp_copies, zio->io_txg, NULL, flags,
3560eda14cbcSMatt Macy 		    &zio->io_alloc_list, zio, zio->io_allocator);
3561eda14cbcSMatt Macy 	}
3562eda14cbcSMatt Macy 
3563184c1b94SMartin Matuska 	if (error == ENOSPC && zio->io_size > SPA_MINBLOCKSIZE) {
3564184c1b94SMartin Matuska 		if (zfs_flags & ZFS_DEBUG_METASLAB_ALLOC) {
3565184c1b94SMartin Matuska 			zfs_dbgmsg("%s: metaslab allocation failure, "
3566184c1b94SMartin Matuska 			    "trying ganging: zio %px, size %llu, error %d",
356733b8c039SMartin Matuska 			    spa_name(spa), zio, (u_longlong_t)zio->io_size,
356833b8c039SMartin Matuska 			    error);
3569184c1b94SMartin Matuska 		}
3570184c1b94SMartin Matuska 		return (zio_write_gang_block(zio, mc));
3571184c1b94SMartin Matuska 	}
3572eda14cbcSMatt Macy 	if (error != 0) {
3573184c1b94SMartin Matuska 		if (error != ENOSPC ||
3574184c1b94SMartin Matuska 		    (zfs_flags & ZFS_DEBUG_METASLAB_ALLOC)) {
3575eda14cbcSMatt Macy 			zfs_dbgmsg("%s: metaslab allocation failure: zio %px, "
3576184c1b94SMartin Matuska 			    "size %llu, error %d",
357733b8c039SMartin Matuska 			    spa_name(spa), zio, (u_longlong_t)zio->io_size,
357833b8c039SMartin Matuska 			    error);
3579184c1b94SMartin Matuska 		}
3580eda14cbcSMatt Macy 		zio->io_error = error;
3581eda14cbcSMatt Macy 	}
3582eda14cbcSMatt Macy 
3583eda14cbcSMatt Macy 	return (zio);
3584eda14cbcSMatt Macy }
3585eda14cbcSMatt Macy 
3586eda14cbcSMatt Macy static zio_t *
3587eda14cbcSMatt Macy zio_dva_free(zio_t *zio)
3588eda14cbcSMatt Macy {
3589eda14cbcSMatt Macy 	metaslab_free(zio->io_spa, zio->io_bp, zio->io_txg, B_FALSE);
3590eda14cbcSMatt Macy 
3591eda14cbcSMatt Macy 	return (zio);
3592eda14cbcSMatt Macy }
3593eda14cbcSMatt Macy 
3594eda14cbcSMatt Macy static zio_t *
3595eda14cbcSMatt Macy zio_dva_claim(zio_t *zio)
3596eda14cbcSMatt Macy {
3597eda14cbcSMatt Macy 	int error;
3598eda14cbcSMatt Macy 
3599eda14cbcSMatt Macy 	error = metaslab_claim(zio->io_spa, zio->io_bp, zio->io_txg);
3600eda14cbcSMatt Macy 	if (error)
3601eda14cbcSMatt Macy 		zio->io_error = error;
3602eda14cbcSMatt Macy 
3603eda14cbcSMatt Macy 	return (zio);
3604eda14cbcSMatt Macy }
3605eda14cbcSMatt Macy 
3606eda14cbcSMatt Macy /*
3607eda14cbcSMatt Macy  * Undo an allocation.  This is used by zio_done() when an I/O fails
3608eda14cbcSMatt Macy  * and we want to give back the block we just allocated.
3609eda14cbcSMatt Macy  * This handles both normal blocks and gang blocks.
3610eda14cbcSMatt Macy  */
3611eda14cbcSMatt Macy static void
3612eda14cbcSMatt Macy zio_dva_unallocate(zio_t *zio, zio_gang_node_t *gn, blkptr_t *bp)
3613eda14cbcSMatt Macy {
3614eda14cbcSMatt Macy 	ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp));
3615eda14cbcSMatt Macy 	ASSERT(zio->io_bp_override == NULL);
3616eda14cbcSMatt Macy 
3617eda14cbcSMatt Macy 	if (!BP_IS_HOLE(bp))
3618eda14cbcSMatt Macy 		metaslab_free(zio->io_spa, bp, bp->blk_birth, B_TRUE);
3619eda14cbcSMatt Macy 
3620eda14cbcSMatt Macy 	if (gn != NULL) {
3621eda14cbcSMatt Macy 		for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
3622eda14cbcSMatt Macy 			zio_dva_unallocate(zio, gn->gn_child[g],
3623eda14cbcSMatt Macy 			    &gn->gn_gbh->zg_blkptr[g]);
3624eda14cbcSMatt Macy 		}
3625eda14cbcSMatt Macy 	}
3626eda14cbcSMatt Macy }
3627eda14cbcSMatt Macy 
3628eda14cbcSMatt Macy /*
3629eda14cbcSMatt Macy  * Try to allocate an intent log block.  Return 0 on success, errno on failure.
3630eda14cbcSMatt Macy  */
3631eda14cbcSMatt Macy int
3632eda14cbcSMatt Macy zio_alloc_zil(spa_t *spa, objset_t *os, uint64_t txg, blkptr_t *new_bp,
3633eda14cbcSMatt Macy     uint64_t size, boolean_t *slog)
3634eda14cbcSMatt Macy {
3635eda14cbcSMatt Macy 	int error = 1;
3636eda14cbcSMatt Macy 	zio_alloc_list_t io_alloc_list;
3637eda14cbcSMatt Macy 
3638eda14cbcSMatt Macy 	ASSERT(txg > spa_syncing_txg(spa));
3639eda14cbcSMatt Macy 
3640eda14cbcSMatt Macy 	metaslab_trace_init(&io_alloc_list);
3641eda14cbcSMatt Macy 
3642eda14cbcSMatt Macy 	/*
3643eda14cbcSMatt Macy 	 * Block pointer fields are useful to metaslabs for stats and debugging.
3644eda14cbcSMatt Macy 	 * Fill in the obvious ones before calling into metaslab_alloc().
3645eda14cbcSMatt Macy 	 */
3646eda14cbcSMatt Macy 	BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG);
3647eda14cbcSMatt Macy 	BP_SET_PSIZE(new_bp, size);
3648eda14cbcSMatt Macy 	BP_SET_LEVEL(new_bp, 0);
3649eda14cbcSMatt Macy 
3650eda14cbcSMatt Macy 	/*
3651eda14cbcSMatt Macy 	 * When allocating a zil block, we don't have information about
3652eda14cbcSMatt Macy 	 * the final destination of the block except the objset it's part
3653eda14cbcSMatt Macy 	 * of, so we just hash the objset ID to pick the allocator to get
3654eda14cbcSMatt Macy 	 * some parallelism.
3655eda14cbcSMatt Macy 	 */
36567877fdebSMatt Macy 	int flags = METASLAB_FASTWRITE | METASLAB_ZIL;
36573f9d360cSMartin Matuska 	int allocator = (uint_t)cityhash4(0, 0, 0,
36583f9d360cSMartin Matuska 	    os->os_dsl_dataset->ds_object) % spa->spa_alloc_count;
3659184c1b94SMartin Matuska 	error = metaslab_alloc(spa, spa_log_class(spa), size, new_bp, 1,
3660184c1b94SMartin Matuska 	    txg, NULL, flags, &io_alloc_list, NULL, allocator);
3661184c1b94SMartin Matuska 	*slog = (error == 0);
3662184c1b94SMartin Matuska 	if (error != 0) {
3663184c1b94SMartin Matuska 		error = metaslab_alloc(spa, spa_embedded_log_class(spa), size,
3664184c1b94SMartin Matuska 		    new_bp, 1, txg, NULL, flags,
3665184c1b94SMartin Matuska 		    &io_alloc_list, NULL, allocator);
3666184c1b94SMartin Matuska 	}
3667184c1b94SMartin Matuska 	if (error != 0) {
3668184c1b94SMartin Matuska 		error = metaslab_alloc(spa, spa_normal_class(spa), size,
3669184c1b94SMartin Matuska 		    new_bp, 1, txg, NULL, flags,
3670184c1b94SMartin Matuska 		    &io_alloc_list, NULL, allocator);
3671eda14cbcSMatt Macy 	}
3672eda14cbcSMatt Macy 	metaslab_trace_fini(&io_alloc_list);
3673eda14cbcSMatt Macy 
3674eda14cbcSMatt Macy 	if (error == 0) {
3675eda14cbcSMatt Macy 		BP_SET_LSIZE(new_bp, size);
3676eda14cbcSMatt Macy 		BP_SET_PSIZE(new_bp, size);
3677eda14cbcSMatt Macy 		BP_SET_COMPRESS(new_bp, ZIO_COMPRESS_OFF);
3678eda14cbcSMatt Macy 		BP_SET_CHECKSUM(new_bp,
3679eda14cbcSMatt Macy 		    spa_version(spa) >= SPA_VERSION_SLIM_ZIL
3680eda14cbcSMatt Macy 		    ? ZIO_CHECKSUM_ZILOG2 : ZIO_CHECKSUM_ZILOG);
3681eda14cbcSMatt Macy 		BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG);
3682eda14cbcSMatt Macy 		BP_SET_LEVEL(new_bp, 0);
3683eda14cbcSMatt Macy 		BP_SET_DEDUP(new_bp, 0);
3684eda14cbcSMatt Macy 		BP_SET_BYTEORDER(new_bp, ZFS_HOST_BYTEORDER);
3685eda14cbcSMatt Macy 
3686eda14cbcSMatt Macy 		/*
3687eda14cbcSMatt Macy 		 * encrypted blocks will require an IV and salt. We generate
3688eda14cbcSMatt Macy 		 * these now since we will not be rewriting the bp at
3689eda14cbcSMatt Macy 		 * rewrite time.
3690eda14cbcSMatt Macy 		 */
3691eda14cbcSMatt Macy 		if (os->os_encrypted) {
3692eda14cbcSMatt Macy 			uint8_t iv[ZIO_DATA_IV_LEN];
3693eda14cbcSMatt Macy 			uint8_t salt[ZIO_DATA_SALT_LEN];
3694eda14cbcSMatt Macy 
3695eda14cbcSMatt Macy 			BP_SET_CRYPT(new_bp, B_TRUE);
3696eda14cbcSMatt Macy 			VERIFY0(spa_crypt_get_salt(spa,
3697eda14cbcSMatt Macy 			    dmu_objset_id(os), salt));
3698eda14cbcSMatt Macy 			VERIFY0(zio_crypt_generate_iv(iv));
3699eda14cbcSMatt Macy 
3700eda14cbcSMatt Macy 			zio_crypt_encode_params_bp(new_bp, salt, iv);
3701eda14cbcSMatt Macy 		}
3702eda14cbcSMatt Macy 	} else {
3703eda14cbcSMatt Macy 		zfs_dbgmsg("%s: zil block allocation failure: "
370433b8c039SMartin Matuska 		    "size %llu, error %d", spa_name(spa), (u_longlong_t)size,
370533b8c039SMartin Matuska 		    error);
3706eda14cbcSMatt Macy 	}
3707eda14cbcSMatt Macy 
3708eda14cbcSMatt Macy 	return (error);
3709eda14cbcSMatt Macy }
3710eda14cbcSMatt Macy 
3711eda14cbcSMatt Macy /*
3712eda14cbcSMatt Macy  * ==========================================================================
3713eda14cbcSMatt Macy  * Read and write to physical devices
3714eda14cbcSMatt Macy  * ==========================================================================
3715eda14cbcSMatt Macy  */
3716eda14cbcSMatt Macy 
3717eda14cbcSMatt Macy /*
3718eda14cbcSMatt Macy  * Issue an I/O to the underlying vdev. Typically the issue pipeline
3719eda14cbcSMatt Macy  * stops after this stage and will resume upon I/O completion.
3720eda14cbcSMatt Macy  * However, there are instances where the vdev layer may need to
3721eda14cbcSMatt Macy  * continue the pipeline when an I/O was not issued. Since the I/O
3722eda14cbcSMatt Macy  * that was sent to the vdev layer might be different than the one
3723eda14cbcSMatt Macy  * currently active in the pipeline (see vdev_queue_io()), we explicitly
3724eda14cbcSMatt Macy  * force the underlying vdev layers to call either zio_execute() or
3725eda14cbcSMatt Macy  * zio_interrupt() to ensure that the pipeline continues with the correct I/O.
3726eda14cbcSMatt Macy  */
3727eda14cbcSMatt Macy static zio_t *
3728eda14cbcSMatt Macy zio_vdev_io_start(zio_t *zio)
3729eda14cbcSMatt Macy {
3730eda14cbcSMatt Macy 	vdev_t *vd = zio->io_vd;
3731eda14cbcSMatt Macy 	uint64_t align;
3732eda14cbcSMatt Macy 	spa_t *spa = zio->io_spa;
3733eda14cbcSMatt Macy 
3734eda14cbcSMatt Macy 	zio->io_delay = 0;
3735eda14cbcSMatt Macy 
3736eda14cbcSMatt Macy 	ASSERT(zio->io_error == 0);
3737eda14cbcSMatt Macy 	ASSERT(zio->io_child_error[ZIO_CHILD_VDEV] == 0);
3738eda14cbcSMatt Macy 
3739eda14cbcSMatt Macy 	if (vd == NULL) {
3740eda14cbcSMatt Macy 		if (!(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
3741eda14cbcSMatt Macy 			spa_config_enter(spa, SCL_ZIO, zio, RW_READER);
3742eda14cbcSMatt Macy 
3743eda14cbcSMatt Macy 		/*
3744eda14cbcSMatt Macy 		 * The mirror_ops handle multiple DVAs in a single BP.
3745eda14cbcSMatt Macy 		 */
3746eda14cbcSMatt Macy 		vdev_mirror_ops.vdev_op_io_start(zio);
3747eda14cbcSMatt Macy 		return (NULL);
3748eda14cbcSMatt Macy 	}
3749eda14cbcSMatt Macy 
3750eda14cbcSMatt Macy 	ASSERT3P(zio->io_logical, !=, zio);
3751eda14cbcSMatt Macy 	if (zio->io_type == ZIO_TYPE_WRITE) {
3752eda14cbcSMatt Macy 		ASSERT(spa->spa_trust_config);
3753eda14cbcSMatt Macy 
3754eda14cbcSMatt Macy 		/*
3755eda14cbcSMatt Macy 		 * Note: the code can handle other kinds of writes,
3756eda14cbcSMatt Macy 		 * but we don't expect them.
3757eda14cbcSMatt Macy 		 */
3758eda14cbcSMatt Macy 		if (zio->io_vd->vdev_removing) {
3759eda14cbcSMatt Macy 			ASSERT(zio->io_flags &
3760eda14cbcSMatt Macy 			    (ZIO_FLAG_PHYSICAL | ZIO_FLAG_SELF_HEAL |
3761eda14cbcSMatt Macy 			    ZIO_FLAG_RESILVER | ZIO_FLAG_INDUCE_DAMAGE));
3762eda14cbcSMatt Macy 		}
3763eda14cbcSMatt Macy 	}
3764eda14cbcSMatt Macy 
3765eda14cbcSMatt Macy 	align = 1ULL << vd->vdev_top->vdev_ashift;
3766eda14cbcSMatt Macy 
3767eda14cbcSMatt Macy 	if (!(zio->io_flags & ZIO_FLAG_PHYSICAL) &&
3768eda14cbcSMatt Macy 	    P2PHASE(zio->io_size, align) != 0) {
3769eda14cbcSMatt Macy 		/* Transform logical writes to be a full physical block size. */
3770eda14cbcSMatt Macy 		uint64_t asize = P2ROUNDUP(zio->io_size, align);
3771eda14cbcSMatt Macy 		abd_t *abuf = abd_alloc_sametype(zio->io_abd, asize);
3772eda14cbcSMatt Macy 		ASSERT(vd == vd->vdev_top);
3773eda14cbcSMatt Macy 		if (zio->io_type == ZIO_TYPE_WRITE) {
3774eda14cbcSMatt Macy 			abd_copy(abuf, zio->io_abd, zio->io_size);
3775eda14cbcSMatt Macy 			abd_zero_off(abuf, zio->io_size, asize - zio->io_size);
3776eda14cbcSMatt Macy 		}
3777eda14cbcSMatt Macy 		zio_push_transform(zio, abuf, asize, asize, zio_subblock);
3778eda14cbcSMatt Macy 	}
3779eda14cbcSMatt Macy 
3780eda14cbcSMatt Macy 	/*
3781eda14cbcSMatt Macy 	 * If this is not a physical io, make sure that it is properly aligned
3782eda14cbcSMatt Macy 	 * before proceeding.
3783eda14cbcSMatt Macy 	 */
3784eda14cbcSMatt Macy 	if (!(zio->io_flags & ZIO_FLAG_PHYSICAL)) {
3785eda14cbcSMatt Macy 		ASSERT0(P2PHASE(zio->io_offset, align));
3786eda14cbcSMatt Macy 		ASSERT0(P2PHASE(zio->io_size, align));
3787eda14cbcSMatt Macy 	} else {
3788eda14cbcSMatt Macy 		/*
3789eda14cbcSMatt Macy 		 * For physical writes, we allow 512b aligned writes and assume
3790eda14cbcSMatt Macy 		 * the device will perform a read-modify-write as necessary.
3791eda14cbcSMatt Macy 		 */
3792eda14cbcSMatt Macy 		ASSERT0(P2PHASE(zio->io_offset, SPA_MINBLOCKSIZE));
3793eda14cbcSMatt Macy 		ASSERT0(P2PHASE(zio->io_size, SPA_MINBLOCKSIZE));
3794eda14cbcSMatt Macy 	}
3795eda14cbcSMatt Macy 
3796eda14cbcSMatt Macy 	VERIFY(zio->io_type != ZIO_TYPE_WRITE || spa_writeable(spa));
3797eda14cbcSMatt Macy 
3798eda14cbcSMatt Macy 	/*
3799eda14cbcSMatt Macy 	 * If this is a repair I/O, and there's no self-healing involved --
3800eda14cbcSMatt Macy 	 * that is, we're just resilvering what we expect to resilver --
3801eda14cbcSMatt Macy 	 * then don't do the I/O unless zio's txg is actually in vd's DTL.
3802eda14cbcSMatt Macy 	 * This prevents spurious resilvering.
3803eda14cbcSMatt Macy 	 *
3804eda14cbcSMatt Macy 	 * There are a few ways that we can end up creating these spurious
3805eda14cbcSMatt Macy 	 * resilver i/os:
3806eda14cbcSMatt Macy 	 *
3807eda14cbcSMatt Macy 	 * 1. A resilver i/o will be issued if any DVA in the BP has a
3808eda14cbcSMatt Macy 	 * dirty DTL.  The mirror code will issue resilver writes to
3809eda14cbcSMatt Macy 	 * each DVA, including the one(s) that are not on vdevs with dirty
3810eda14cbcSMatt Macy 	 * DTLs.
3811eda14cbcSMatt Macy 	 *
3812eda14cbcSMatt Macy 	 * 2. With nested replication, which happens when we have a
3813eda14cbcSMatt Macy 	 * "replacing" or "spare" vdev that's a child of a mirror or raidz.
3814eda14cbcSMatt Macy 	 * For example, given mirror(replacing(A+B), C), it's likely that
3815eda14cbcSMatt Macy 	 * only A is out of date (it's the new device). In this case, we'll
3816eda14cbcSMatt Macy 	 * read from C, then use the data to resilver A+B -- but we don't
3817eda14cbcSMatt Macy 	 * actually want to resilver B, just A. The top-level mirror has no
3818eda14cbcSMatt Macy 	 * way to know this, so instead we just discard unnecessary repairs
3819eda14cbcSMatt Macy 	 * as we work our way down the vdev tree.
3820eda14cbcSMatt Macy 	 *
3821eda14cbcSMatt Macy 	 * 3. ZTEST also creates mirrors of mirrors, mirrors of raidz, etc.
3822eda14cbcSMatt Macy 	 * The same logic applies to any form of nested replication: ditto
3823eda14cbcSMatt Macy 	 * + mirror, RAID-Z + replacing, etc.
3824eda14cbcSMatt Macy 	 *
3825eda14cbcSMatt Macy 	 * However, indirect vdevs point off to other vdevs which may have
3826eda14cbcSMatt Macy 	 * DTL's, so we never bypass them.  The child i/os on concrete vdevs
3827eda14cbcSMatt Macy 	 * will be properly bypassed instead.
38287877fdebSMatt Macy 	 *
38297877fdebSMatt Macy 	 * Leaf DTL_PARTIAL can be empty when a legitimate write comes from
38307877fdebSMatt Macy 	 * a dRAID spare vdev. For example, when a dRAID spare is first
38317877fdebSMatt Macy 	 * used, its spare blocks need to be written to but the leaf vdev's
38327877fdebSMatt Macy 	 * of such blocks can have empty DTL_PARTIAL.
38337877fdebSMatt Macy 	 *
38347877fdebSMatt Macy 	 * There seemed no clean way to allow such writes while bypassing
38357877fdebSMatt Macy 	 * spurious ones. At this point, just avoid all bypassing for dRAID
38367877fdebSMatt Macy 	 * for correctness.
3837eda14cbcSMatt Macy 	 */
3838eda14cbcSMatt Macy 	if ((zio->io_flags & ZIO_FLAG_IO_REPAIR) &&
3839eda14cbcSMatt Macy 	    !(zio->io_flags & ZIO_FLAG_SELF_HEAL) &&
3840eda14cbcSMatt Macy 	    zio->io_txg != 0 &&	/* not a delegated i/o */
3841eda14cbcSMatt Macy 	    vd->vdev_ops != &vdev_indirect_ops &&
38427877fdebSMatt Macy 	    vd->vdev_top->vdev_ops != &vdev_draid_ops &&
3843eda14cbcSMatt Macy 	    !vdev_dtl_contains(vd, DTL_PARTIAL, zio->io_txg, 1)) {
3844eda14cbcSMatt Macy 		ASSERT(zio->io_type == ZIO_TYPE_WRITE);
3845eda14cbcSMatt Macy 		zio_vdev_io_bypass(zio);
3846eda14cbcSMatt Macy 		return (zio);
3847eda14cbcSMatt Macy 	}
3848eda14cbcSMatt Macy 
38497877fdebSMatt Macy 	/*
38507877fdebSMatt Macy 	 * Select the next best leaf I/O to process.  Distributed spares are
38517877fdebSMatt Macy 	 * excluded since they dispatch the I/O directly to a leaf vdev after
38527877fdebSMatt Macy 	 * applying the dRAID mapping.
38537877fdebSMatt Macy 	 */
38547877fdebSMatt Macy 	if (vd->vdev_ops->vdev_op_leaf &&
38557877fdebSMatt Macy 	    vd->vdev_ops != &vdev_draid_spare_ops &&
38567877fdebSMatt Macy 	    (zio->io_type == ZIO_TYPE_READ ||
38577877fdebSMatt Macy 	    zio->io_type == ZIO_TYPE_WRITE ||
38587877fdebSMatt Macy 	    zio->io_type == ZIO_TYPE_TRIM)) {
3859eda14cbcSMatt Macy 
3860eda14cbcSMatt Macy 		if (zio->io_type == ZIO_TYPE_READ && vdev_cache_read(zio))
3861eda14cbcSMatt Macy 			return (zio);
3862eda14cbcSMatt Macy 
3863eda14cbcSMatt Macy 		if ((zio = vdev_queue_io(zio)) == NULL)
3864eda14cbcSMatt Macy 			return (NULL);
3865eda14cbcSMatt Macy 
3866eda14cbcSMatt Macy 		if (!vdev_accessible(vd, zio)) {
3867eda14cbcSMatt Macy 			zio->io_error = SET_ERROR(ENXIO);
3868eda14cbcSMatt Macy 			zio_interrupt(zio);
3869eda14cbcSMatt Macy 			return (NULL);
3870eda14cbcSMatt Macy 		}
3871eda14cbcSMatt Macy 		zio->io_delay = gethrtime();
3872eda14cbcSMatt Macy 	}
3873eda14cbcSMatt Macy 
3874eda14cbcSMatt Macy 	vd->vdev_ops->vdev_op_io_start(zio);
3875eda14cbcSMatt Macy 	return (NULL);
3876eda14cbcSMatt Macy }
3877eda14cbcSMatt Macy 
3878eda14cbcSMatt Macy static zio_t *
3879eda14cbcSMatt Macy zio_vdev_io_done(zio_t *zio)
3880eda14cbcSMatt Macy {
3881eda14cbcSMatt Macy 	vdev_t *vd = zio->io_vd;
3882eda14cbcSMatt Macy 	vdev_ops_t *ops = vd ? vd->vdev_ops : &vdev_mirror_ops;
3883eda14cbcSMatt Macy 	boolean_t unexpected_error = B_FALSE;
3884eda14cbcSMatt Macy 
3885eda14cbcSMatt Macy 	if (zio_wait_for_children(zio, ZIO_CHILD_VDEV_BIT, ZIO_WAIT_DONE)) {
3886eda14cbcSMatt Macy 		return (NULL);
3887eda14cbcSMatt Macy 	}
3888eda14cbcSMatt Macy 
3889eda14cbcSMatt Macy 	ASSERT(zio->io_type == ZIO_TYPE_READ ||
3890eda14cbcSMatt Macy 	    zio->io_type == ZIO_TYPE_WRITE || zio->io_type == ZIO_TYPE_TRIM);
3891eda14cbcSMatt Macy 
3892eda14cbcSMatt Macy 	if (zio->io_delay)
3893eda14cbcSMatt Macy 		zio->io_delay = gethrtime() - zio->io_delay;
3894eda14cbcSMatt Macy 
38957877fdebSMatt Macy 	if (vd != NULL && vd->vdev_ops->vdev_op_leaf &&
38967877fdebSMatt Macy 	    vd->vdev_ops != &vdev_draid_spare_ops) {
3897eda14cbcSMatt Macy 		vdev_queue_io_done(zio);
3898eda14cbcSMatt Macy 
3899eda14cbcSMatt Macy 		if (zio->io_type == ZIO_TYPE_WRITE)
3900eda14cbcSMatt Macy 			vdev_cache_write(zio);
3901eda14cbcSMatt Macy 
3902eda14cbcSMatt Macy 		if (zio_injection_enabled && zio->io_error == 0)
3903eda14cbcSMatt Macy 			zio->io_error = zio_handle_device_injections(vd, zio,
3904eda14cbcSMatt Macy 			    EIO, EILSEQ);
3905eda14cbcSMatt Macy 
3906eda14cbcSMatt Macy 		if (zio_injection_enabled && zio->io_error == 0)
3907eda14cbcSMatt Macy 			zio->io_error = zio_handle_label_injection(zio, EIO);
3908eda14cbcSMatt Macy 
3909eda14cbcSMatt Macy 		if (zio->io_error && zio->io_type != ZIO_TYPE_TRIM) {
3910eda14cbcSMatt Macy 			if (!vdev_accessible(vd, zio)) {
3911eda14cbcSMatt Macy 				zio->io_error = SET_ERROR(ENXIO);
3912eda14cbcSMatt Macy 			} else {
3913eda14cbcSMatt Macy 				unexpected_error = B_TRUE;
3914eda14cbcSMatt Macy 			}
3915eda14cbcSMatt Macy 		}
3916eda14cbcSMatt Macy 	}
3917eda14cbcSMatt Macy 
3918eda14cbcSMatt Macy 	ops->vdev_op_io_done(zio);
3919eda14cbcSMatt Macy 
3920eda14cbcSMatt Macy 	if (unexpected_error)
3921eda14cbcSMatt Macy 		VERIFY(vdev_probe(vd, zio) == NULL);
3922eda14cbcSMatt Macy 
3923eda14cbcSMatt Macy 	return (zio);
3924eda14cbcSMatt Macy }
3925eda14cbcSMatt Macy 
3926eda14cbcSMatt Macy /*
3927eda14cbcSMatt Macy  * This function is used to change the priority of an existing zio that is
3928eda14cbcSMatt Macy  * currently in-flight. This is used by the arc to upgrade priority in the
3929eda14cbcSMatt Macy  * event that a demand read is made for a block that is currently queued
3930eda14cbcSMatt Macy  * as a scrub or async read IO. Otherwise, the high priority read request
3931eda14cbcSMatt Macy  * would end up having to wait for the lower priority IO.
3932eda14cbcSMatt Macy  */
3933eda14cbcSMatt Macy void
3934eda14cbcSMatt Macy zio_change_priority(zio_t *pio, zio_priority_t priority)
3935eda14cbcSMatt Macy {
3936eda14cbcSMatt Macy 	zio_t *cio, *cio_next;
3937eda14cbcSMatt Macy 	zio_link_t *zl = NULL;
3938eda14cbcSMatt Macy 
3939eda14cbcSMatt Macy 	ASSERT3U(priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
3940eda14cbcSMatt Macy 
3941eda14cbcSMatt Macy 	if (pio->io_vd != NULL && pio->io_vd->vdev_ops->vdev_op_leaf) {
3942eda14cbcSMatt Macy 		vdev_queue_change_io_priority(pio, priority);
3943eda14cbcSMatt Macy 	} else {
3944eda14cbcSMatt Macy 		pio->io_priority = priority;
3945eda14cbcSMatt Macy 	}
3946eda14cbcSMatt Macy 
3947eda14cbcSMatt Macy 	mutex_enter(&pio->io_lock);
3948eda14cbcSMatt Macy 	for (cio = zio_walk_children(pio, &zl); cio != NULL; cio = cio_next) {
3949eda14cbcSMatt Macy 		cio_next = zio_walk_children(pio, &zl);
3950eda14cbcSMatt Macy 		zio_change_priority(cio, priority);
3951eda14cbcSMatt Macy 	}
3952eda14cbcSMatt Macy 	mutex_exit(&pio->io_lock);
3953eda14cbcSMatt Macy }
3954eda14cbcSMatt Macy 
3955eda14cbcSMatt Macy /*
3956eda14cbcSMatt Macy  * For non-raidz ZIOs, we can just copy aside the bad data read from the
3957eda14cbcSMatt Macy  * disk, and use that to finish the checksum ereport later.
3958eda14cbcSMatt Macy  */
3959eda14cbcSMatt Macy static void
3960eda14cbcSMatt Macy zio_vsd_default_cksum_finish(zio_cksum_report_t *zcr,
3961eda14cbcSMatt Macy     const abd_t *good_buf)
3962eda14cbcSMatt Macy {
3963eda14cbcSMatt Macy 	/* no processing needed */
3964eda14cbcSMatt Macy 	zfs_ereport_finish_checksum(zcr, good_buf, zcr->zcr_cbdata, B_FALSE);
3965eda14cbcSMatt Macy }
3966eda14cbcSMatt Macy 
3967eda14cbcSMatt Macy /*ARGSUSED*/
3968eda14cbcSMatt Macy void
3969f9693befSMartin Matuska zio_vsd_default_cksum_report(zio_t *zio, zio_cksum_report_t *zcr)
3970eda14cbcSMatt Macy {
3971eda14cbcSMatt Macy 	void *abd = abd_alloc_sametype(zio->io_abd, zio->io_size);
3972eda14cbcSMatt Macy 
3973eda14cbcSMatt Macy 	abd_copy(abd, zio->io_abd, zio->io_size);
3974eda14cbcSMatt Macy 
3975eda14cbcSMatt Macy 	zcr->zcr_cbinfo = zio->io_size;
3976eda14cbcSMatt Macy 	zcr->zcr_cbdata = abd;
3977eda14cbcSMatt Macy 	zcr->zcr_finish = zio_vsd_default_cksum_finish;
3978eda14cbcSMatt Macy 	zcr->zcr_free = zio_abd_free;
3979eda14cbcSMatt Macy }
3980eda14cbcSMatt Macy 
3981eda14cbcSMatt Macy static zio_t *
3982eda14cbcSMatt Macy zio_vdev_io_assess(zio_t *zio)
3983eda14cbcSMatt Macy {
3984eda14cbcSMatt Macy 	vdev_t *vd = zio->io_vd;
3985eda14cbcSMatt Macy 
3986eda14cbcSMatt Macy 	if (zio_wait_for_children(zio, ZIO_CHILD_VDEV_BIT, ZIO_WAIT_DONE)) {
3987eda14cbcSMatt Macy 		return (NULL);
3988eda14cbcSMatt Macy 	}
3989eda14cbcSMatt Macy 
3990eda14cbcSMatt Macy 	if (vd == NULL && !(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
3991eda14cbcSMatt Macy 		spa_config_exit(zio->io_spa, SCL_ZIO, zio);
3992eda14cbcSMatt Macy 
3993eda14cbcSMatt Macy 	if (zio->io_vsd != NULL) {
3994eda14cbcSMatt Macy 		zio->io_vsd_ops->vsd_free(zio);
3995eda14cbcSMatt Macy 		zio->io_vsd = NULL;
3996eda14cbcSMatt Macy 	}
3997eda14cbcSMatt Macy 
3998eda14cbcSMatt Macy 	if (zio_injection_enabled && zio->io_error == 0)
3999eda14cbcSMatt Macy 		zio->io_error = zio_handle_fault_injection(zio, EIO);
4000eda14cbcSMatt Macy 
4001eda14cbcSMatt Macy 	/*
4002eda14cbcSMatt Macy 	 * If the I/O failed, determine whether we should attempt to retry it.
4003eda14cbcSMatt Macy 	 *
4004eda14cbcSMatt Macy 	 * On retry, we cut in line in the issue queue, since we don't want
4005eda14cbcSMatt Macy 	 * compression/checksumming/etc. work to prevent our (cheap) IO reissue.
4006eda14cbcSMatt Macy 	 */
4007eda14cbcSMatt Macy 	if (zio->io_error && vd == NULL &&
4008eda14cbcSMatt Macy 	    !(zio->io_flags & (ZIO_FLAG_DONT_RETRY | ZIO_FLAG_IO_RETRY))) {
4009eda14cbcSMatt Macy 		ASSERT(!(zio->io_flags & ZIO_FLAG_DONT_QUEUE));	/* not a leaf */
4010eda14cbcSMatt Macy 		ASSERT(!(zio->io_flags & ZIO_FLAG_IO_BYPASS));	/* not a leaf */
4011eda14cbcSMatt Macy 		zio->io_error = 0;
4012eda14cbcSMatt Macy 		zio->io_flags |= ZIO_FLAG_IO_RETRY |
4013eda14cbcSMatt Macy 		    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE;
4014eda14cbcSMatt Macy 		zio->io_stage = ZIO_STAGE_VDEV_IO_START >> 1;
4015eda14cbcSMatt Macy 		zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE,
4016eda14cbcSMatt Macy 		    zio_requeue_io_start_cut_in_line);
4017eda14cbcSMatt Macy 		return (NULL);
4018eda14cbcSMatt Macy 	}
4019eda14cbcSMatt Macy 
4020eda14cbcSMatt Macy 	/*
4021eda14cbcSMatt Macy 	 * If we got an error on a leaf device, convert it to ENXIO
4022eda14cbcSMatt Macy 	 * if the device is not accessible at all.
4023eda14cbcSMatt Macy 	 */
4024eda14cbcSMatt Macy 	if (zio->io_error && vd != NULL && vd->vdev_ops->vdev_op_leaf &&
4025eda14cbcSMatt Macy 	    !vdev_accessible(vd, zio))
4026eda14cbcSMatt Macy 		zio->io_error = SET_ERROR(ENXIO);
4027eda14cbcSMatt Macy 
4028eda14cbcSMatt Macy 	/*
4029eda14cbcSMatt Macy 	 * If we can't write to an interior vdev (mirror or RAID-Z),
4030eda14cbcSMatt Macy 	 * set vdev_cant_write so that we stop trying to allocate from it.
4031eda14cbcSMatt Macy 	 */
4032eda14cbcSMatt Macy 	if (zio->io_error == ENXIO && zio->io_type == ZIO_TYPE_WRITE &&
4033eda14cbcSMatt Macy 	    vd != NULL && !vd->vdev_ops->vdev_op_leaf) {
40346db169e9SMartin Matuska 		vdev_dbgmsg(vd, "zio_vdev_io_assess(zio=%px) setting "
40356db169e9SMartin Matuska 		    "cant_write=TRUE due to write failure with ENXIO",
40366db169e9SMartin Matuska 		    zio);
4037eda14cbcSMatt Macy 		vd->vdev_cant_write = B_TRUE;
4038eda14cbcSMatt Macy 	}
4039eda14cbcSMatt Macy 
4040eda14cbcSMatt Macy 	/*
4041eda14cbcSMatt Macy 	 * If a cache flush returns ENOTSUP or ENOTTY, we know that no future
4042eda14cbcSMatt Macy 	 * attempts will ever succeed. In this case we set a persistent
4043eda14cbcSMatt Macy 	 * boolean flag so that we don't bother with it in the future.
4044eda14cbcSMatt Macy 	 */
4045eda14cbcSMatt Macy 	if ((zio->io_error == ENOTSUP || zio->io_error == ENOTTY) &&
4046eda14cbcSMatt Macy 	    zio->io_type == ZIO_TYPE_IOCTL &&
4047eda14cbcSMatt Macy 	    zio->io_cmd == DKIOCFLUSHWRITECACHE && vd != NULL)
4048eda14cbcSMatt Macy 		vd->vdev_nowritecache = B_TRUE;
4049eda14cbcSMatt Macy 
4050eda14cbcSMatt Macy 	if (zio->io_error)
4051eda14cbcSMatt Macy 		zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
4052eda14cbcSMatt Macy 
4053eda14cbcSMatt Macy 	if (vd != NULL && vd->vdev_ops->vdev_op_leaf &&
4054eda14cbcSMatt Macy 	    zio->io_physdone != NULL) {
4055eda14cbcSMatt Macy 		ASSERT(!(zio->io_flags & ZIO_FLAG_DELEGATED));
4056eda14cbcSMatt Macy 		ASSERT(zio->io_child_type == ZIO_CHILD_VDEV);
4057eda14cbcSMatt Macy 		zio->io_physdone(zio->io_logical);
4058eda14cbcSMatt Macy 	}
4059eda14cbcSMatt Macy 
4060eda14cbcSMatt Macy 	return (zio);
4061eda14cbcSMatt Macy }
4062eda14cbcSMatt Macy 
4063eda14cbcSMatt Macy void
4064eda14cbcSMatt Macy zio_vdev_io_reissue(zio_t *zio)
4065eda14cbcSMatt Macy {
4066eda14cbcSMatt Macy 	ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
4067eda14cbcSMatt Macy 	ASSERT(zio->io_error == 0);
4068eda14cbcSMatt Macy 
4069eda14cbcSMatt Macy 	zio->io_stage >>= 1;
4070eda14cbcSMatt Macy }
4071eda14cbcSMatt Macy 
4072eda14cbcSMatt Macy void
4073eda14cbcSMatt Macy zio_vdev_io_redone(zio_t *zio)
4074eda14cbcSMatt Macy {
4075eda14cbcSMatt Macy 	ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_DONE);
4076eda14cbcSMatt Macy 
4077eda14cbcSMatt Macy 	zio->io_stage >>= 1;
4078eda14cbcSMatt Macy }
4079eda14cbcSMatt Macy 
4080eda14cbcSMatt Macy void
4081eda14cbcSMatt Macy zio_vdev_io_bypass(zio_t *zio)
4082eda14cbcSMatt Macy {
4083eda14cbcSMatt Macy 	ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
4084eda14cbcSMatt Macy 	ASSERT(zio->io_error == 0);
4085eda14cbcSMatt Macy 
4086eda14cbcSMatt Macy 	zio->io_flags |= ZIO_FLAG_IO_BYPASS;
4087eda14cbcSMatt Macy 	zio->io_stage = ZIO_STAGE_VDEV_IO_ASSESS >> 1;
4088eda14cbcSMatt Macy }
4089eda14cbcSMatt Macy 
4090eda14cbcSMatt Macy /*
4091eda14cbcSMatt Macy  * ==========================================================================
4092eda14cbcSMatt Macy  * Encrypt and store encryption parameters
4093eda14cbcSMatt Macy  * ==========================================================================
4094eda14cbcSMatt Macy  */
4095eda14cbcSMatt Macy 
4096eda14cbcSMatt Macy 
4097eda14cbcSMatt Macy /*
4098eda14cbcSMatt Macy  * This function is used for ZIO_STAGE_ENCRYPT. It is responsible for
4099eda14cbcSMatt Macy  * managing the storage of encryption parameters and passing them to the
4100eda14cbcSMatt Macy  * lower-level encryption functions.
4101eda14cbcSMatt Macy  */
4102eda14cbcSMatt Macy static zio_t *
4103eda14cbcSMatt Macy zio_encrypt(zio_t *zio)
4104eda14cbcSMatt Macy {
4105eda14cbcSMatt Macy 	zio_prop_t *zp = &zio->io_prop;
4106eda14cbcSMatt Macy 	spa_t *spa = zio->io_spa;
4107eda14cbcSMatt Macy 	blkptr_t *bp = zio->io_bp;
4108eda14cbcSMatt Macy 	uint64_t psize = BP_GET_PSIZE(bp);
4109eda14cbcSMatt Macy 	uint64_t dsobj = zio->io_bookmark.zb_objset;
4110eda14cbcSMatt Macy 	dmu_object_type_t ot = BP_GET_TYPE(bp);
4111eda14cbcSMatt Macy 	void *enc_buf = NULL;
4112eda14cbcSMatt Macy 	abd_t *eabd = NULL;
4113eda14cbcSMatt Macy 	uint8_t salt[ZIO_DATA_SALT_LEN];
4114eda14cbcSMatt Macy 	uint8_t iv[ZIO_DATA_IV_LEN];
4115eda14cbcSMatt Macy 	uint8_t mac[ZIO_DATA_MAC_LEN];
4116eda14cbcSMatt Macy 	boolean_t no_crypt = B_FALSE;
4117eda14cbcSMatt Macy 
4118eda14cbcSMatt Macy 	/* the root zio already encrypted the data */
4119eda14cbcSMatt Macy 	if (zio->io_child_type == ZIO_CHILD_GANG)
4120eda14cbcSMatt Macy 		return (zio);
4121eda14cbcSMatt Macy 
4122eda14cbcSMatt Macy 	/* only ZIL blocks are re-encrypted on rewrite */
4123eda14cbcSMatt Macy 	if (!IO_IS_ALLOCATING(zio) && ot != DMU_OT_INTENT_LOG)
4124eda14cbcSMatt Macy 		return (zio);
4125eda14cbcSMatt Macy 
4126eda14cbcSMatt Macy 	if (!(zp->zp_encrypt || BP_IS_ENCRYPTED(bp))) {
4127eda14cbcSMatt Macy 		BP_SET_CRYPT(bp, B_FALSE);
4128eda14cbcSMatt Macy 		return (zio);
4129eda14cbcSMatt Macy 	}
4130eda14cbcSMatt Macy 
4131eda14cbcSMatt Macy 	/* if we are doing raw encryption set the provided encryption params */
4132eda14cbcSMatt Macy 	if (zio->io_flags & ZIO_FLAG_RAW_ENCRYPT) {
4133eda14cbcSMatt Macy 		ASSERT0(BP_GET_LEVEL(bp));
4134eda14cbcSMatt Macy 		BP_SET_CRYPT(bp, B_TRUE);
4135eda14cbcSMatt Macy 		BP_SET_BYTEORDER(bp, zp->zp_byteorder);
4136eda14cbcSMatt Macy 		if (ot != DMU_OT_OBJSET)
4137eda14cbcSMatt Macy 			zio_crypt_encode_mac_bp(bp, zp->zp_mac);
4138eda14cbcSMatt Macy 
4139eda14cbcSMatt Macy 		/* dnode blocks must be written out in the provided byteorder */
4140eda14cbcSMatt Macy 		if (zp->zp_byteorder != ZFS_HOST_BYTEORDER &&
4141eda14cbcSMatt Macy 		    ot == DMU_OT_DNODE) {
4142eda14cbcSMatt Macy 			void *bswap_buf = zio_buf_alloc(psize);
4143eda14cbcSMatt Macy 			abd_t *babd = abd_get_from_buf(bswap_buf, psize);
4144eda14cbcSMatt Macy 
4145eda14cbcSMatt Macy 			ASSERT3U(BP_GET_COMPRESS(bp), ==, ZIO_COMPRESS_OFF);
4146eda14cbcSMatt Macy 			abd_copy_to_buf(bswap_buf, zio->io_abd, psize);
4147eda14cbcSMatt Macy 			dmu_ot_byteswap[DMU_OT_BYTESWAP(ot)].ob_func(bswap_buf,
4148eda14cbcSMatt Macy 			    psize);
4149eda14cbcSMatt Macy 
4150eda14cbcSMatt Macy 			abd_take_ownership_of_buf(babd, B_TRUE);
4151eda14cbcSMatt Macy 			zio_push_transform(zio, babd, psize, psize, NULL);
4152eda14cbcSMatt Macy 		}
4153eda14cbcSMatt Macy 
4154eda14cbcSMatt Macy 		if (DMU_OT_IS_ENCRYPTED(ot))
4155eda14cbcSMatt Macy 			zio_crypt_encode_params_bp(bp, zp->zp_salt, zp->zp_iv);
4156eda14cbcSMatt Macy 		return (zio);
4157eda14cbcSMatt Macy 	}
4158eda14cbcSMatt Macy 
4159eda14cbcSMatt Macy 	/* indirect blocks only maintain a cksum of the lower level MACs */
4160eda14cbcSMatt Macy 	if (BP_GET_LEVEL(bp) > 0) {
4161eda14cbcSMatt Macy 		BP_SET_CRYPT(bp, B_TRUE);
4162eda14cbcSMatt Macy 		VERIFY0(zio_crypt_do_indirect_mac_checksum_abd(B_TRUE,
4163eda14cbcSMatt Macy 		    zio->io_orig_abd, BP_GET_LSIZE(bp), BP_SHOULD_BYTESWAP(bp),
4164eda14cbcSMatt Macy 		    mac));
4165eda14cbcSMatt Macy 		zio_crypt_encode_mac_bp(bp, mac);
4166eda14cbcSMatt Macy 		return (zio);
4167eda14cbcSMatt Macy 	}
4168eda14cbcSMatt Macy 
4169eda14cbcSMatt Macy 	/*
4170eda14cbcSMatt Macy 	 * Objset blocks are a special case since they have 2 256-bit MACs
4171eda14cbcSMatt Macy 	 * embedded within them.
4172eda14cbcSMatt Macy 	 */
4173eda14cbcSMatt Macy 	if (ot == DMU_OT_OBJSET) {
4174eda14cbcSMatt Macy 		ASSERT0(DMU_OT_IS_ENCRYPTED(ot));
4175eda14cbcSMatt Macy 		ASSERT3U(BP_GET_COMPRESS(bp), ==, ZIO_COMPRESS_OFF);
4176eda14cbcSMatt Macy 		BP_SET_CRYPT(bp, B_TRUE);
4177eda14cbcSMatt Macy 		VERIFY0(spa_do_crypt_objset_mac_abd(B_TRUE, spa, dsobj,
4178eda14cbcSMatt Macy 		    zio->io_abd, psize, BP_SHOULD_BYTESWAP(bp)));
4179eda14cbcSMatt Macy 		return (zio);
4180eda14cbcSMatt Macy 	}
4181eda14cbcSMatt Macy 
4182eda14cbcSMatt Macy 	/* unencrypted object types are only authenticated with a MAC */
4183eda14cbcSMatt Macy 	if (!DMU_OT_IS_ENCRYPTED(ot)) {
4184eda14cbcSMatt Macy 		BP_SET_CRYPT(bp, B_TRUE);
4185eda14cbcSMatt Macy 		VERIFY0(spa_do_crypt_mac_abd(B_TRUE, spa, dsobj,
4186eda14cbcSMatt Macy 		    zio->io_abd, psize, mac));
4187eda14cbcSMatt Macy 		zio_crypt_encode_mac_bp(bp, mac);
4188eda14cbcSMatt Macy 		return (zio);
4189eda14cbcSMatt Macy 	}
4190eda14cbcSMatt Macy 
4191eda14cbcSMatt Macy 	/*
4192eda14cbcSMatt Macy 	 * Later passes of sync-to-convergence may decide to rewrite data
4193eda14cbcSMatt Macy 	 * in place to avoid more disk reallocations. This presents a problem
4194eda14cbcSMatt Macy 	 * for encryption because this constitutes rewriting the new data with
4195eda14cbcSMatt Macy 	 * the same encryption key and IV. However, this only applies to blocks
4196eda14cbcSMatt Macy 	 * in the MOS (particularly the spacemaps) and we do not encrypt the
4197eda14cbcSMatt Macy 	 * MOS. We assert that the zio is allocating or an intent log write
4198eda14cbcSMatt Macy 	 * to enforce this.
4199eda14cbcSMatt Macy 	 */
4200eda14cbcSMatt Macy 	ASSERT(IO_IS_ALLOCATING(zio) || ot == DMU_OT_INTENT_LOG);
4201eda14cbcSMatt Macy 	ASSERT(BP_GET_LEVEL(bp) == 0 || ot == DMU_OT_INTENT_LOG);
4202eda14cbcSMatt Macy 	ASSERT(spa_feature_is_active(spa, SPA_FEATURE_ENCRYPTION));
4203eda14cbcSMatt Macy 	ASSERT3U(psize, !=, 0);
4204eda14cbcSMatt Macy 
4205eda14cbcSMatt Macy 	enc_buf = zio_buf_alloc(psize);
4206eda14cbcSMatt Macy 	eabd = abd_get_from_buf(enc_buf, psize);
4207eda14cbcSMatt Macy 	abd_take_ownership_of_buf(eabd, B_TRUE);
4208eda14cbcSMatt Macy 
4209eda14cbcSMatt Macy 	/*
4210eda14cbcSMatt Macy 	 * For an explanation of what encryption parameters are stored
4211eda14cbcSMatt Macy 	 * where, see the block comment in zio_crypt.c.
4212eda14cbcSMatt Macy 	 */
4213eda14cbcSMatt Macy 	if (ot == DMU_OT_INTENT_LOG) {
4214eda14cbcSMatt Macy 		zio_crypt_decode_params_bp(bp, salt, iv);
4215eda14cbcSMatt Macy 	} else {
4216eda14cbcSMatt Macy 		BP_SET_CRYPT(bp, B_TRUE);
4217eda14cbcSMatt Macy 	}
4218eda14cbcSMatt Macy 
4219eda14cbcSMatt Macy 	/* Perform the encryption. This should not fail */
4220eda14cbcSMatt Macy 	VERIFY0(spa_do_crypt_abd(B_TRUE, spa, &zio->io_bookmark,
4221eda14cbcSMatt Macy 	    BP_GET_TYPE(bp), BP_GET_DEDUP(bp), BP_SHOULD_BYTESWAP(bp),
4222eda14cbcSMatt Macy 	    salt, iv, mac, psize, zio->io_abd, eabd, &no_crypt));
4223eda14cbcSMatt Macy 
4224eda14cbcSMatt Macy 	/* encode encryption metadata into the bp */
4225eda14cbcSMatt Macy 	if (ot == DMU_OT_INTENT_LOG) {
4226eda14cbcSMatt Macy 		/*
4227eda14cbcSMatt Macy 		 * ZIL blocks store the MAC in the embedded checksum, so the
4228eda14cbcSMatt Macy 		 * transform must always be applied.
4229eda14cbcSMatt Macy 		 */
4230eda14cbcSMatt Macy 		zio_crypt_encode_mac_zil(enc_buf, mac);
4231eda14cbcSMatt Macy 		zio_push_transform(zio, eabd, psize, psize, NULL);
4232eda14cbcSMatt Macy 	} else {
4233eda14cbcSMatt Macy 		BP_SET_CRYPT(bp, B_TRUE);
4234eda14cbcSMatt Macy 		zio_crypt_encode_params_bp(bp, salt, iv);
4235eda14cbcSMatt Macy 		zio_crypt_encode_mac_bp(bp, mac);
4236eda14cbcSMatt Macy 
4237eda14cbcSMatt Macy 		if (no_crypt) {
4238eda14cbcSMatt Macy 			ASSERT3U(ot, ==, DMU_OT_DNODE);
4239eda14cbcSMatt Macy 			abd_free(eabd);
4240eda14cbcSMatt Macy 		} else {
4241eda14cbcSMatt Macy 			zio_push_transform(zio, eabd, psize, psize, NULL);
4242eda14cbcSMatt Macy 		}
4243eda14cbcSMatt Macy 	}
4244eda14cbcSMatt Macy 
4245eda14cbcSMatt Macy 	return (zio);
4246eda14cbcSMatt Macy }
4247eda14cbcSMatt Macy 
4248eda14cbcSMatt Macy /*
4249eda14cbcSMatt Macy  * ==========================================================================
4250eda14cbcSMatt Macy  * Generate and verify checksums
4251eda14cbcSMatt Macy  * ==========================================================================
4252eda14cbcSMatt Macy  */
4253eda14cbcSMatt Macy static zio_t *
4254eda14cbcSMatt Macy zio_checksum_generate(zio_t *zio)
4255eda14cbcSMatt Macy {
4256eda14cbcSMatt Macy 	blkptr_t *bp = zio->io_bp;
4257eda14cbcSMatt Macy 	enum zio_checksum checksum;
4258eda14cbcSMatt Macy 
4259eda14cbcSMatt Macy 	if (bp == NULL) {
4260eda14cbcSMatt Macy 		/*
4261eda14cbcSMatt Macy 		 * This is zio_write_phys().
4262eda14cbcSMatt Macy 		 * We're either generating a label checksum, or none at all.
4263eda14cbcSMatt Macy 		 */
4264eda14cbcSMatt Macy 		checksum = zio->io_prop.zp_checksum;
4265eda14cbcSMatt Macy 
4266eda14cbcSMatt Macy 		if (checksum == ZIO_CHECKSUM_OFF)
4267eda14cbcSMatt Macy 			return (zio);
4268eda14cbcSMatt Macy 
4269eda14cbcSMatt Macy 		ASSERT(checksum == ZIO_CHECKSUM_LABEL);
4270eda14cbcSMatt Macy 	} else {
4271eda14cbcSMatt Macy 		if (BP_IS_GANG(bp) && zio->io_child_type == ZIO_CHILD_GANG) {
4272eda14cbcSMatt Macy 			ASSERT(!IO_IS_ALLOCATING(zio));
4273eda14cbcSMatt Macy 			checksum = ZIO_CHECKSUM_GANG_HEADER;
4274eda14cbcSMatt Macy 		} else {
4275eda14cbcSMatt Macy 			checksum = BP_GET_CHECKSUM(bp);
4276eda14cbcSMatt Macy 		}
4277eda14cbcSMatt Macy 	}
4278eda14cbcSMatt Macy 
4279eda14cbcSMatt Macy 	zio_checksum_compute(zio, checksum, zio->io_abd, zio->io_size);
4280eda14cbcSMatt Macy 
4281eda14cbcSMatt Macy 	return (zio);
4282eda14cbcSMatt Macy }
4283eda14cbcSMatt Macy 
4284eda14cbcSMatt Macy static zio_t *
4285eda14cbcSMatt Macy zio_checksum_verify(zio_t *zio)
4286eda14cbcSMatt Macy {
4287eda14cbcSMatt Macy 	zio_bad_cksum_t info;
4288eda14cbcSMatt Macy 	blkptr_t *bp = zio->io_bp;
4289eda14cbcSMatt Macy 	int error;
4290eda14cbcSMatt Macy 
4291eda14cbcSMatt Macy 	ASSERT(zio->io_vd != NULL);
4292eda14cbcSMatt Macy 
4293eda14cbcSMatt Macy 	if (bp == NULL) {
4294eda14cbcSMatt Macy 		/*
4295eda14cbcSMatt Macy 		 * This is zio_read_phys().
4296eda14cbcSMatt Macy 		 * We're either verifying a label checksum, or nothing at all.
4297eda14cbcSMatt Macy 		 */
4298eda14cbcSMatt Macy 		if (zio->io_prop.zp_checksum == ZIO_CHECKSUM_OFF)
4299eda14cbcSMatt Macy 			return (zio);
4300eda14cbcSMatt Macy 
43017877fdebSMatt Macy 		ASSERT3U(zio->io_prop.zp_checksum, ==, ZIO_CHECKSUM_LABEL);
4302eda14cbcSMatt Macy 	}
4303eda14cbcSMatt Macy 
4304eda14cbcSMatt Macy 	if ((error = zio_checksum_error(zio, &info)) != 0) {
4305eda14cbcSMatt Macy 		zio->io_error = error;
4306eda14cbcSMatt Macy 		if (error == ECKSUM &&
4307eda14cbcSMatt Macy 		    !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
4308ba27dd8bSMartin Matuska 			(void) zfs_ereport_start_checksum(zio->io_spa,
43092c48331dSMatt Macy 			    zio->io_vd, &zio->io_bookmark, zio,
4310f9693befSMartin Matuska 			    zio->io_offset, zio->io_size, &info);
4311eda14cbcSMatt Macy 			mutex_enter(&zio->io_vd->vdev_stat_lock);
4312eda14cbcSMatt Macy 			zio->io_vd->vdev_stat.vs_checksum_errors++;
4313eda14cbcSMatt Macy 			mutex_exit(&zio->io_vd->vdev_stat_lock);
43142c48331dSMatt Macy 		}
4315eda14cbcSMatt Macy 	}
4316eda14cbcSMatt Macy 
4317eda14cbcSMatt Macy 	return (zio);
4318eda14cbcSMatt Macy }
4319eda14cbcSMatt Macy 
4320eda14cbcSMatt Macy /*
4321eda14cbcSMatt Macy  * Called by RAID-Z to ensure we don't compute the checksum twice.
4322eda14cbcSMatt Macy  */
4323eda14cbcSMatt Macy void
4324eda14cbcSMatt Macy zio_checksum_verified(zio_t *zio)
4325eda14cbcSMatt Macy {
4326eda14cbcSMatt Macy 	zio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY;
4327eda14cbcSMatt Macy }
4328eda14cbcSMatt Macy 
4329eda14cbcSMatt Macy /*
4330eda14cbcSMatt Macy  * ==========================================================================
4331eda14cbcSMatt Macy  * Error rank.  Error are ranked in the order 0, ENXIO, ECKSUM, EIO, other.
4332eda14cbcSMatt Macy  * An error of 0 indicates success.  ENXIO indicates whole-device failure,
4333eda14cbcSMatt Macy  * which may be transient (e.g. unplugged) or permanent.  ECKSUM and EIO
4334eda14cbcSMatt Macy  * indicate errors that are specific to one I/O, and most likely permanent.
4335eda14cbcSMatt Macy  * Any other error is presumed to be worse because we weren't expecting it.
4336eda14cbcSMatt Macy  * ==========================================================================
4337eda14cbcSMatt Macy  */
4338eda14cbcSMatt Macy int
4339eda14cbcSMatt Macy zio_worst_error(int e1, int e2)
4340eda14cbcSMatt Macy {
4341eda14cbcSMatt Macy 	static int zio_error_rank[] = { 0, ENXIO, ECKSUM, EIO };
4342eda14cbcSMatt Macy 	int r1, r2;
4343eda14cbcSMatt Macy 
4344eda14cbcSMatt Macy 	for (r1 = 0; r1 < sizeof (zio_error_rank) / sizeof (int); r1++)
4345eda14cbcSMatt Macy 		if (e1 == zio_error_rank[r1])
4346eda14cbcSMatt Macy 			break;
4347eda14cbcSMatt Macy 
4348eda14cbcSMatt Macy 	for (r2 = 0; r2 < sizeof (zio_error_rank) / sizeof (int); r2++)
4349eda14cbcSMatt Macy 		if (e2 == zio_error_rank[r2])
4350eda14cbcSMatt Macy 			break;
4351eda14cbcSMatt Macy 
4352eda14cbcSMatt Macy 	return (r1 > r2 ? e1 : e2);
4353eda14cbcSMatt Macy }
4354eda14cbcSMatt Macy 
4355eda14cbcSMatt Macy /*
4356eda14cbcSMatt Macy  * ==========================================================================
4357eda14cbcSMatt Macy  * I/O completion
4358eda14cbcSMatt Macy  * ==========================================================================
4359eda14cbcSMatt Macy  */
4360eda14cbcSMatt Macy static zio_t *
4361eda14cbcSMatt Macy zio_ready(zio_t *zio)
4362eda14cbcSMatt Macy {
4363eda14cbcSMatt Macy 	blkptr_t *bp = zio->io_bp;
4364eda14cbcSMatt Macy 	zio_t *pio, *pio_next;
4365eda14cbcSMatt Macy 	zio_link_t *zl = NULL;
4366eda14cbcSMatt Macy 
4367eda14cbcSMatt Macy 	if (zio_wait_for_children(zio, ZIO_CHILD_GANG_BIT | ZIO_CHILD_DDT_BIT,
4368eda14cbcSMatt Macy 	    ZIO_WAIT_READY)) {
4369eda14cbcSMatt Macy 		return (NULL);
4370eda14cbcSMatt Macy 	}
4371eda14cbcSMatt Macy 
4372eda14cbcSMatt Macy 	if (zio->io_ready) {
4373eda14cbcSMatt Macy 		ASSERT(IO_IS_ALLOCATING(zio));
4374eda14cbcSMatt Macy 		ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp) ||
4375eda14cbcSMatt Macy 		    (zio->io_flags & ZIO_FLAG_NOPWRITE));
4376eda14cbcSMatt Macy 		ASSERT(zio->io_children[ZIO_CHILD_GANG][ZIO_WAIT_READY] == 0);
4377eda14cbcSMatt Macy 
4378eda14cbcSMatt Macy 		zio->io_ready(zio);
4379eda14cbcSMatt Macy 	}
4380eda14cbcSMatt Macy 
4381eda14cbcSMatt Macy 	if (bp != NULL && bp != &zio->io_bp_copy)
4382eda14cbcSMatt Macy 		zio->io_bp_copy = *bp;
4383eda14cbcSMatt Macy 
4384eda14cbcSMatt Macy 	if (zio->io_error != 0) {
4385eda14cbcSMatt Macy 		zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
4386eda14cbcSMatt Macy 
4387eda14cbcSMatt Macy 		if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
4388eda14cbcSMatt Macy 			ASSERT(IO_IS_ALLOCATING(zio));
4389eda14cbcSMatt Macy 			ASSERT(zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
4390eda14cbcSMatt Macy 			ASSERT(zio->io_metaslab_class != NULL);
4391eda14cbcSMatt Macy 
4392eda14cbcSMatt Macy 			/*
4393eda14cbcSMatt Macy 			 * We were unable to allocate anything, unreserve and
4394eda14cbcSMatt Macy 			 * issue the next I/O to allocate.
4395eda14cbcSMatt Macy 			 */
4396eda14cbcSMatt Macy 			metaslab_class_throttle_unreserve(
4397eda14cbcSMatt Macy 			    zio->io_metaslab_class, zio->io_prop.zp_copies,
4398eda14cbcSMatt Macy 			    zio->io_allocator, zio);
4399eda14cbcSMatt Macy 			zio_allocate_dispatch(zio->io_spa, zio->io_allocator);
4400eda14cbcSMatt Macy 		}
4401eda14cbcSMatt Macy 	}
4402eda14cbcSMatt Macy 
4403eda14cbcSMatt Macy 	mutex_enter(&zio->io_lock);
4404eda14cbcSMatt Macy 	zio->io_state[ZIO_WAIT_READY] = 1;
4405eda14cbcSMatt Macy 	pio = zio_walk_parents(zio, &zl);
4406eda14cbcSMatt Macy 	mutex_exit(&zio->io_lock);
4407eda14cbcSMatt Macy 
4408eda14cbcSMatt Macy 	/*
4409eda14cbcSMatt Macy 	 * As we notify zio's parents, new parents could be added.
4410eda14cbcSMatt Macy 	 * New parents go to the head of zio's io_parent_list, however,
4411eda14cbcSMatt Macy 	 * so we will (correctly) not notify them.  The remainder of zio's
4412eda14cbcSMatt Macy 	 * io_parent_list, from 'pio_next' onward, cannot change because
4413eda14cbcSMatt Macy 	 * all parents must wait for us to be done before they can be done.
4414eda14cbcSMatt Macy 	 */
4415eda14cbcSMatt Macy 	for (; pio != NULL; pio = pio_next) {
4416eda14cbcSMatt Macy 		pio_next = zio_walk_parents(zio, &zl);
4417eda14cbcSMatt Macy 		zio_notify_parent(pio, zio, ZIO_WAIT_READY, NULL);
4418eda14cbcSMatt Macy 	}
4419eda14cbcSMatt Macy 
4420eda14cbcSMatt Macy 	if (zio->io_flags & ZIO_FLAG_NODATA) {
4421eda14cbcSMatt Macy 		if (BP_IS_GANG(bp)) {
4422eda14cbcSMatt Macy 			zio->io_flags &= ~ZIO_FLAG_NODATA;
4423eda14cbcSMatt Macy 		} else {
4424eda14cbcSMatt Macy 			ASSERT((uintptr_t)zio->io_abd < SPA_MAXBLOCKSIZE);
4425eda14cbcSMatt Macy 			zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES;
4426eda14cbcSMatt Macy 		}
4427eda14cbcSMatt Macy 	}
4428eda14cbcSMatt Macy 
4429eda14cbcSMatt Macy 	if (zio_injection_enabled &&
4430eda14cbcSMatt Macy 	    zio->io_spa->spa_syncing_txg == zio->io_txg)
4431eda14cbcSMatt Macy 		zio_handle_ignored_writes(zio);
4432eda14cbcSMatt Macy 
4433eda14cbcSMatt Macy 	return (zio);
4434eda14cbcSMatt Macy }
4435eda14cbcSMatt Macy 
4436eda14cbcSMatt Macy /*
4437eda14cbcSMatt Macy  * Update the allocation throttle accounting.
4438eda14cbcSMatt Macy  */
4439eda14cbcSMatt Macy static void
4440eda14cbcSMatt Macy zio_dva_throttle_done(zio_t *zio)
4441eda14cbcSMatt Macy {
4442eda14cbcSMatt Macy 	zio_t *lio __maybe_unused = zio->io_logical;
4443eda14cbcSMatt Macy 	zio_t *pio = zio_unique_parent(zio);
4444eda14cbcSMatt Macy 	vdev_t *vd = zio->io_vd;
4445eda14cbcSMatt Macy 	int flags = METASLAB_ASYNC_ALLOC;
4446eda14cbcSMatt Macy 
4447eda14cbcSMatt Macy 	ASSERT3P(zio->io_bp, !=, NULL);
4448eda14cbcSMatt Macy 	ASSERT3U(zio->io_type, ==, ZIO_TYPE_WRITE);
4449eda14cbcSMatt Macy 	ASSERT3U(zio->io_priority, ==, ZIO_PRIORITY_ASYNC_WRITE);
4450eda14cbcSMatt Macy 	ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_VDEV);
4451eda14cbcSMatt Macy 	ASSERT(vd != NULL);
4452eda14cbcSMatt Macy 	ASSERT3P(vd, ==, vd->vdev_top);
4453eda14cbcSMatt Macy 	ASSERT(zio_injection_enabled || !(zio->io_flags & ZIO_FLAG_IO_RETRY));
4454eda14cbcSMatt Macy 	ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REPAIR));
4455eda14cbcSMatt Macy 	ASSERT(zio->io_flags & ZIO_FLAG_IO_ALLOCATING);
4456eda14cbcSMatt Macy 	ASSERT(!(lio->io_flags & ZIO_FLAG_IO_REWRITE));
4457eda14cbcSMatt Macy 	ASSERT(!(lio->io_orig_flags & ZIO_FLAG_NODATA));
4458eda14cbcSMatt Macy 
4459eda14cbcSMatt Macy 	/*
4460eda14cbcSMatt Macy 	 * Parents of gang children can have two flavors -- ones that
4461eda14cbcSMatt Macy 	 * allocated the gang header (will have ZIO_FLAG_IO_REWRITE set)
4462eda14cbcSMatt Macy 	 * and ones that allocated the constituent blocks. The allocation
4463eda14cbcSMatt Macy 	 * throttle needs to know the allocating parent zio so we must find
4464eda14cbcSMatt Macy 	 * it here.
4465eda14cbcSMatt Macy 	 */
4466eda14cbcSMatt Macy 	if (pio->io_child_type == ZIO_CHILD_GANG) {
4467eda14cbcSMatt Macy 		/*
4468eda14cbcSMatt Macy 		 * If our parent is a rewrite gang child then our grandparent
4469eda14cbcSMatt Macy 		 * would have been the one that performed the allocation.
4470eda14cbcSMatt Macy 		 */
4471eda14cbcSMatt Macy 		if (pio->io_flags & ZIO_FLAG_IO_REWRITE)
4472eda14cbcSMatt Macy 			pio = zio_unique_parent(pio);
4473eda14cbcSMatt Macy 		flags |= METASLAB_GANG_CHILD;
4474eda14cbcSMatt Macy 	}
4475eda14cbcSMatt Macy 
4476eda14cbcSMatt Macy 	ASSERT(IO_IS_ALLOCATING(pio));
4477eda14cbcSMatt Macy 	ASSERT3P(zio, !=, zio->io_logical);
4478eda14cbcSMatt Macy 	ASSERT(zio->io_logical != NULL);
4479eda14cbcSMatt Macy 	ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REPAIR));
4480eda14cbcSMatt Macy 	ASSERT0(zio->io_flags & ZIO_FLAG_NOPWRITE);
4481eda14cbcSMatt Macy 	ASSERT(zio->io_metaslab_class != NULL);
4482eda14cbcSMatt Macy 
4483eda14cbcSMatt Macy 	mutex_enter(&pio->io_lock);
4484eda14cbcSMatt Macy 	metaslab_group_alloc_decrement(zio->io_spa, vd->vdev_id, pio, flags,
4485eda14cbcSMatt Macy 	    pio->io_allocator, B_TRUE);
4486eda14cbcSMatt Macy 	mutex_exit(&pio->io_lock);
4487eda14cbcSMatt Macy 
4488eda14cbcSMatt Macy 	metaslab_class_throttle_unreserve(zio->io_metaslab_class, 1,
4489eda14cbcSMatt Macy 	    pio->io_allocator, pio);
4490eda14cbcSMatt Macy 
4491eda14cbcSMatt Macy 	/*
4492eda14cbcSMatt Macy 	 * Call into the pipeline to see if there is more work that
4493eda14cbcSMatt Macy 	 * needs to be done. If there is work to be done it will be
4494eda14cbcSMatt Macy 	 * dispatched to another taskq thread.
4495eda14cbcSMatt Macy 	 */
4496eda14cbcSMatt Macy 	zio_allocate_dispatch(zio->io_spa, pio->io_allocator);
4497eda14cbcSMatt Macy }
4498eda14cbcSMatt Macy 
4499eda14cbcSMatt Macy static zio_t *
4500eda14cbcSMatt Macy zio_done(zio_t *zio)
4501eda14cbcSMatt Macy {
4502eda14cbcSMatt Macy 	/*
4503eda14cbcSMatt Macy 	 * Always attempt to keep stack usage minimal here since
4504eda14cbcSMatt Macy 	 * we can be called recursively up to 19 levels deep.
4505eda14cbcSMatt Macy 	 */
4506eda14cbcSMatt Macy 	const uint64_t psize = zio->io_size;
4507eda14cbcSMatt Macy 	zio_t *pio, *pio_next;
4508eda14cbcSMatt Macy 	zio_link_t *zl = NULL;
4509eda14cbcSMatt Macy 
4510eda14cbcSMatt Macy 	/*
4511eda14cbcSMatt Macy 	 * If our children haven't all completed,
4512eda14cbcSMatt Macy 	 * wait for them and then repeat this pipeline stage.
4513eda14cbcSMatt Macy 	 */
4514eda14cbcSMatt Macy 	if (zio_wait_for_children(zio, ZIO_CHILD_ALL_BITS, ZIO_WAIT_DONE)) {
4515eda14cbcSMatt Macy 		return (NULL);
4516eda14cbcSMatt Macy 	}
4517eda14cbcSMatt Macy 
4518eda14cbcSMatt Macy 	/*
4519eda14cbcSMatt Macy 	 * If the allocation throttle is enabled, then update the accounting.
4520eda14cbcSMatt Macy 	 * We only track child I/Os that are part of an allocating async
4521eda14cbcSMatt Macy 	 * write. We must do this since the allocation is performed
4522eda14cbcSMatt Macy 	 * by the logical I/O but the actual write is done by child I/Os.
4523eda14cbcSMatt Macy 	 */
4524eda14cbcSMatt Macy 	if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING &&
4525eda14cbcSMatt Macy 	    zio->io_child_type == ZIO_CHILD_VDEV) {
4526eda14cbcSMatt Macy 		ASSERT(zio->io_metaslab_class != NULL);
4527eda14cbcSMatt Macy 		ASSERT(zio->io_metaslab_class->mc_alloc_throttle_enabled);
4528eda14cbcSMatt Macy 		zio_dva_throttle_done(zio);
4529eda14cbcSMatt Macy 	}
4530eda14cbcSMatt Macy 
4531eda14cbcSMatt Macy 	/*
4532eda14cbcSMatt Macy 	 * If the allocation throttle is enabled, verify that
4533eda14cbcSMatt Macy 	 * we have decremented the refcounts for every I/O that was throttled.
4534eda14cbcSMatt Macy 	 */
4535eda14cbcSMatt Macy 	if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
4536eda14cbcSMatt Macy 		ASSERT(zio->io_type == ZIO_TYPE_WRITE);
4537eda14cbcSMatt Macy 		ASSERT(zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
4538eda14cbcSMatt Macy 		ASSERT(zio->io_bp != NULL);
4539eda14cbcSMatt Macy 
4540eda14cbcSMatt Macy 		metaslab_group_alloc_verify(zio->io_spa, zio->io_bp, zio,
4541eda14cbcSMatt Macy 		    zio->io_allocator);
45427877fdebSMatt Macy 		VERIFY(zfs_refcount_not_held(&zio->io_metaslab_class->
45437877fdebSMatt Macy 		    mc_allocator[zio->io_allocator].mca_alloc_slots, zio));
4544eda14cbcSMatt Macy 	}
4545eda14cbcSMatt Macy 
4546eda14cbcSMatt Macy 
4547eda14cbcSMatt Macy 	for (int c = 0; c < ZIO_CHILD_TYPES; c++)
4548eda14cbcSMatt Macy 		for (int w = 0; w < ZIO_WAIT_TYPES; w++)
4549eda14cbcSMatt Macy 			ASSERT(zio->io_children[c][w] == 0);
4550eda14cbcSMatt Macy 
4551eda14cbcSMatt Macy 	if (zio->io_bp != NULL && !BP_IS_EMBEDDED(zio->io_bp)) {
4552eda14cbcSMatt Macy 		ASSERT(zio->io_bp->blk_pad[0] == 0);
4553eda14cbcSMatt Macy 		ASSERT(zio->io_bp->blk_pad[1] == 0);
4554eda14cbcSMatt Macy 		ASSERT(bcmp(zio->io_bp, &zio->io_bp_copy,
4555eda14cbcSMatt Macy 		    sizeof (blkptr_t)) == 0 ||
4556eda14cbcSMatt Macy 		    (zio->io_bp == zio_unique_parent(zio)->io_bp));
4557eda14cbcSMatt Macy 		if (zio->io_type == ZIO_TYPE_WRITE && !BP_IS_HOLE(zio->io_bp) &&
4558eda14cbcSMatt Macy 		    zio->io_bp_override == NULL &&
4559eda14cbcSMatt Macy 		    !(zio->io_flags & ZIO_FLAG_IO_REPAIR)) {
4560eda14cbcSMatt Macy 			ASSERT3U(zio->io_prop.zp_copies, <=,
4561eda14cbcSMatt Macy 			    BP_GET_NDVAS(zio->io_bp));
4562eda14cbcSMatt Macy 			ASSERT(BP_COUNT_GANG(zio->io_bp) == 0 ||
4563eda14cbcSMatt Macy 			    (BP_COUNT_GANG(zio->io_bp) ==
4564eda14cbcSMatt Macy 			    BP_GET_NDVAS(zio->io_bp)));
4565eda14cbcSMatt Macy 		}
4566eda14cbcSMatt Macy 		if (zio->io_flags & ZIO_FLAG_NOPWRITE)
4567eda14cbcSMatt Macy 			VERIFY(BP_EQUAL(zio->io_bp, &zio->io_bp_orig));
4568eda14cbcSMatt Macy 	}
4569eda14cbcSMatt Macy 
4570eda14cbcSMatt Macy 	/*
4571eda14cbcSMatt Macy 	 * If there were child vdev/gang/ddt errors, they apply to us now.
4572eda14cbcSMatt Macy 	 */
4573eda14cbcSMatt Macy 	zio_inherit_child_errors(zio, ZIO_CHILD_VDEV);
4574eda14cbcSMatt Macy 	zio_inherit_child_errors(zio, ZIO_CHILD_GANG);
4575eda14cbcSMatt Macy 	zio_inherit_child_errors(zio, ZIO_CHILD_DDT);
4576eda14cbcSMatt Macy 
4577eda14cbcSMatt Macy 	/*
4578eda14cbcSMatt Macy 	 * If the I/O on the transformed data was successful, generate any
4579eda14cbcSMatt Macy 	 * checksum reports now while we still have the transformed data.
4580eda14cbcSMatt Macy 	 */
4581eda14cbcSMatt Macy 	if (zio->io_error == 0) {
4582eda14cbcSMatt Macy 		while (zio->io_cksum_report != NULL) {
4583eda14cbcSMatt Macy 			zio_cksum_report_t *zcr = zio->io_cksum_report;
4584eda14cbcSMatt Macy 			uint64_t align = zcr->zcr_align;
4585eda14cbcSMatt Macy 			uint64_t asize = P2ROUNDUP(psize, align);
4586eda14cbcSMatt Macy 			abd_t *adata = zio->io_abd;
4587eda14cbcSMatt Macy 
458816038816SMartin Matuska 			if (adata != NULL && asize != psize) {
4589eda14cbcSMatt Macy 				adata = abd_alloc(asize, B_TRUE);
4590eda14cbcSMatt Macy 				abd_copy(adata, zio->io_abd, psize);
4591eda14cbcSMatt Macy 				abd_zero_off(adata, psize, asize - psize);
4592eda14cbcSMatt Macy 			}
4593eda14cbcSMatt Macy 
4594eda14cbcSMatt Macy 			zio->io_cksum_report = zcr->zcr_next;
4595eda14cbcSMatt Macy 			zcr->zcr_next = NULL;
4596eda14cbcSMatt Macy 			zcr->zcr_finish(zcr, adata);
4597eda14cbcSMatt Macy 			zfs_ereport_free_checksum(zcr);
4598eda14cbcSMatt Macy 
459916038816SMartin Matuska 			if (adata != NULL && asize != psize)
4600eda14cbcSMatt Macy 				abd_free(adata);
4601eda14cbcSMatt Macy 		}
4602eda14cbcSMatt Macy 	}
4603eda14cbcSMatt Macy 
4604eda14cbcSMatt Macy 	zio_pop_transforms(zio);	/* note: may set zio->io_error */
4605eda14cbcSMatt Macy 
4606eda14cbcSMatt Macy 	vdev_stat_update(zio, psize);
4607eda14cbcSMatt Macy 
4608eda14cbcSMatt Macy 	/*
4609eda14cbcSMatt Macy 	 * If this I/O is attached to a particular vdev is slow, exceeding
4610eda14cbcSMatt Macy 	 * 30 seconds to complete, post an error described the I/O delay.
4611eda14cbcSMatt Macy 	 * We ignore these errors if the device is currently unavailable.
4612eda14cbcSMatt Macy 	 */
4613eda14cbcSMatt Macy 	if (zio->io_delay >= MSEC2NSEC(zio_slow_io_ms)) {
4614eda14cbcSMatt Macy 		if (zio->io_vd != NULL && !vdev_is_dead(zio->io_vd)) {
4615eda14cbcSMatt Macy 			/*
4616eda14cbcSMatt Macy 			 * We want to only increment our slow IO counters if
4617eda14cbcSMatt Macy 			 * the IO is valid (i.e. not if the drive is removed).
4618eda14cbcSMatt Macy 			 *
4619eda14cbcSMatt Macy 			 * zfs_ereport_post() will also do these checks, but
4620eda14cbcSMatt Macy 			 * it can also ratelimit and have other failures, so we
4621eda14cbcSMatt Macy 			 * need to increment the slow_io counters independent
4622eda14cbcSMatt Macy 			 * of it.
4623eda14cbcSMatt Macy 			 */
4624eda14cbcSMatt Macy 			if (zfs_ereport_is_valid(FM_EREPORT_ZFS_DELAY,
4625eda14cbcSMatt Macy 			    zio->io_spa, zio->io_vd, zio)) {
4626eda14cbcSMatt Macy 				mutex_enter(&zio->io_vd->vdev_stat_lock);
4627eda14cbcSMatt Macy 				zio->io_vd->vdev_stat.vs_slow_ios++;
4628eda14cbcSMatt Macy 				mutex_exit(&zio->io_vd->vdev_stat_lock);
4629eda14cbcSMatt Macy 
4630eac7052fSMatt Macy 				(void) zfs_ereport_post(FM_EREPORT_ZFS_DELAY,
4631eda14cbcSMatt Macy 				    zio->io_spa, zio->io_vd, &zio->io_bookmark,
46322c48331dSMatt Macy 				    zio, 0);
4633eda14cbcSMatt Macy 			}
4634eda14cbcSMatt Macy 		}
4635eda14cbcSMatt Macy 	}
4636eda14cbcSMatt Macy 
4637eda14cbcSMatt Macy 	if (zio->io_error) {
4638eda14cbcSMatt Macy 		/*
4639eda14cbcSMatt Macy 		 * If this I/O is attached to a particular vdev,
4640eda14cbcSMatt Macy 		 * generate an error message describing the I/O failure
4641eda14cbcSMatt Macy 		 * at the block level.  We ignore these errors if the
4642eda14cbcSMatt Macy 		 * device is currently unavailable.
4643eda14cbcSMatt Macy 		 */
4644eda14cbcSMatt Macy 		if (zio->io_error != ECKSUM && zio->io_vd != NULL &&
4645eda14cbcSMatt Macy 		    !vdev_is_dead(zio->io_vd)) {
46462c48331dSMatt Macy 			int ret = zfs_ereport_post(FM_EREPORT_ZFS_IO,
46472c48331dSMatt Macy 			    zio->io_spa, zio->io_vd, &zio->io_bookmark, zio, 0);
46482c48331dSMatt Macy 			if (ret != EALREADY) {
4649eda14cbcSMatt Macy 				mutex_enter(&zio->io_vd->vdev_stat_lock);
46502c48331dSMatt Macy 				if (zio->io_type == ZIO_TYPE_READ)
4651eda14cbcSMatt Macy 					zio->io_vd->vdev_stat.vs_read_errors++;
46522c48331dSMatt Macy 				else if (zio->io_type == ZIO_TYPE_WRITE)
4653eda14cbcSMatt Macy 					zio->io_vd->vdev_stat.vs_write_errors++;
4654eda14cbcSMatt Macy 				mutex_exit(&zio->io_vd->vdev_stat_lock);
46552c48331dSMatt Macy 			}
4656eda14cbcSMatt Macy 		}
4657eda14cbcSMatt Macy 
4658eda14cbcSMatt Macy 		if ((zio->io_error == EIO || !(zio->io_flags &
4659eda14cbcSMatt Macy 		    (ZIO_FLAG_SPECULATIVE | ZIO_FLAG_DONT_PROPAGATE))) &&
4660eda14cbcSMatt Macy 		    zio == zio->io_logical) {
4661eda14cbcSMatt Macy 			/*
4662eda14cbcSMatt Macy 			 * For logical I/O requests, tell the SPA to log the
4663eda14cbcSMatt Macy 			 * error and generate a logical data ereport.
4664eda14cbcSMatt Macy 			 */
4665eda14cbcSMatt Macy 			spa_log_error(zio->io_spa, &zio->io_bookmark);
4666eac7052fSMatt Macy 			(void) zfs_ereport_post(FM_EREPORT_ZFS_DATA,
46672c48331dSMatt Macy 			    zio->io_spa, NULL, &zio->io_bookmark, zio, 0);
4668eda14cbcSMatt Macy 		}
4669eda14cbcSMatt Macy 	}
4670eda14cbcSMatt Macy 
4671eda14cbcSMatt Macy 	if (zio->io_error && zio == zio->io_logical) {
4672eda14cbcSMatt Macy 		/*
4673eda14cbcSMatt Macy 		 * Determine whether zio should be reexecuted.  This will
4674eda14cbcSMatt Macy 		 * propagate all the way to the root via zio_notify_parent().
4675eda14cbcSMatt Macy 		 */
4676eda14cbcSMatt Macy 		ASSERT(zio->io_vd == NULL && zio->io_bp != NULL);
4677eda14cbcSMatt Macy 		ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
4678eda14cbcSMatt Macy 
4679eda14cbcSMatt Macy 		if (IO_IS_ALLOCATING(zio) &&
4680eda14cbcSMatt Macy 		    !(zio->io_flags & ZIO_FLAG_CANFAIL)) {
4681eda14cbcSMatt Macy 			if (zio->io_error != ENOSPC)
4682eda14cbcSMatt Macy 				zio->io_reexecute |= ZIO_REEXECUTE_NOW;
4683eda14cbcSMatt Macy 			else
4684eda14cbcSMatt Macy 				zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
4685eda14cbcSMatt Macy 		}
4686eda14cbcSMatt Macy 
4687eda14cbcSMatt Macy 		if ((zio->io_type == ZIO_TYPE_READ ||
4688eda14cbcSMatt Macy 		    zio->io_type == ZIO_TYPE_FREE) &&
4689eda14cbcSMatt Macy 		    !(zio->io_flags & ZIO_FLAG_SCAN_THREAD) &&
4690eda14cbcSMatt Macy 		    zio->io_error == ENXIO &&
4691eda14cbcSMatt Macy 		    spa_load_state(zio->io_spa) == SPA_LOAD_NONE &&
4692eda14cbcSMatt Macy 		    spa_get_failmode(zio->io_spa) != ZIO_FAILURE_MODE_CONTINUE)
4693eda14cbcSMatt Macy 			zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
4694eda14cbcSMatt Macy 
4695eda14cbcSMatt Macy 		if (!(zio->io_flags & ZIO_FLAG_CANFAIL) && !zio->io_reexecute)
4696eda14cbcSMatt Macy 			zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
4697eda14cbcSMatt Macy 
4698eda14cbcSMatt Macy 		/*
4699eda14cbcSMatt Macy 		 * Here is a possibly good place to attempt to do
4700eda14cbcSMatt Macy 		 * either combinatorial reconstruction or error correction
4701eda14cbcSMatt Macy 		 * based on checksums.  It also might be a good place
4702eda14cbcSMatt Macy 		 * to send out preliminary ereports before we suspend
4703eda14cbcSMatt Macy 		 * processing.
4704eda14cbcSMatt Macy 		 */
4705eda14cbcSMatt Macy 	}
4706eda14cbcSMatt Macy 
4707eda14cbcSMatt Macy 	/*
4708eda14cbcSMatt Macy 	 * If there were logical child errors, they apply to us now.
4709eda14cbcSMatt Macy 	 * We defer this until now to avoid conflating logical child
4710eda14cbcSMatt Macy 	 * errors with errors that happened to the zio itself when
4711eda14cbcSMatt Macy 	 * updating vdev stats and reporting FMA events above.
4712eda14cbcSMatt Macy 	 */
4713eda14cbcSMatt Macy 	zio_inherit_child_errors(zio, ZIO_CHILD_LOGICAL);
4714eda14cbcSMatt Macy 
4715eda14cbcSMatt Macy 	if ((zio->io_error || zio->io_reexecute) &&
4716eda14cbcSMatt Macy 	    IO_IS_ALLOCATING(zio) && zio->io_gang_leader == zio &&
4717eda14cbcSMatt Macy 	    !(zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE)))
4718eda14cbcSMatt Macy 		zio_dva_unallocate(zio, zio->io_gang_tree, zio->io_bp);
4719eda14cbcSMatt Macy 
4720eda14cbcSMatt Macy 	zio_gang_tree_free(&zio->io_gang_tree);
4721eda14cbcSMatt Macy 
4722eda14cbcSMatt Macy 	/*
4723eda14cbcSMatt Macy 	 * Godfather I/Os should never suspend.
4724eda14cbcSMatt Macy 	 */
4725eda14cbcSMatt Macy 	if ((zio->io_flags & ZIO_FLAG_GODFATHER) &&
4726eda14cbcSMatt Macy 	    (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND))
4727eda14cbcSMatt Macy 		zio->io_reexecute &= ~ZIO_REEXECUTE_SUSPEND;
4728eda14cbcSMatt Macy 
4729eda14cbcSMatt Macy 	if (zio->io_reexecute) {
4730eda14cbcSMatt Macy 		/*
4731eda14cbcSMatt Macy 		 * This is a logical I/O that wants to reexecute.
4732eda14cbcSMatt Macy 		 *
4733eda14cbcSMatt Macy 		 * Reexecute is top-down.  When an i/o fails, if it's not
4734eda14cbcSMatt Macy 		 * the root, it simply notifies its parent and sticks around.
4735eda14cbcSMatt Macy 		 * The parent, seeing that it still has children in zio_done(),
4736eda14cbcSMatt Macy 		 * does the same.  This percolates all the way up to the root.
4737eda14cbcSMatt Macy 		 * The root i/o will reexecute or suspend the entire tree.
4738eda14cbcSMatt Macy 		 *
4739eda14cbcSMatt Macy 		 * This approach ensures that zio_reexecute() honors
4740eda14cbcSMatt Macy 		 * all the original i/o dependency relationships, e.g.
4741eda14cbcSMatt Macy 		 * parents not executing until children are ready.
4742eda14cbcSMatt Macy 		 */
4743eda14cbcSMatt Macy 		ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
4744eda14cbcSMatt Macy 
4745eda14cbcSMatt Macy 		zio->io_gang_leader = NULL;
4746eda14cbcSMatt Macy 
4747eda14cbcSMatt Macy 		mutex_enter(&zio->io_lock);
4748eda14cbcSMatt Macy 		zio->io_state[ZIO_WAIT_DONE] = 1;
4749eda14cbcSMatt Macy 		mutex_exit(&zio->io_lock);
4750eda14cbcSMatt Macy 
4751eda14cbcSMatt Macy 		/*
4752eda14cbcSMatt Macy 		 * "The Godfather" I/O monitors its children but is
4753eda14cbcSMatt Macy 		 * not a true parent to them. It will track them through
4754eda14cbcSMatt Macy 		 * the pipeline but severs its ties whenever they get into
4755eda14cbcSMatt Macy 		 * trouble (e.g. suspended). This allows "The Godfather"
4756eda14cbcSMatt Macy 		 * I/O to return status without blocking.
4757eda14cbcSMatt Macy 		 */
4758eda14cbcSMatt Macy 		zl = NULL;
4759eda14cbcSMatt Macy 		for (pio = zio_walk_parents(zio, &zl); pio != NULL;
4760eda14cbcSMatt Macy 		    pio = pio_next) {
4761eda14cbcSMatt Macy 			zio_link_t *remove_zl = zl;
4762eda14cbcSMatt Macy 			pio_next = zio_walk_parents(zio, &zl);
4763eda14cbcSMatt Macy 
4764eda14cbcSMatt Macy 			if ((pio->io_flags & ZIO_FLAG_GODFATHER) &&
4765eda14cbcSMatt Macy 			    (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND)) {
4766eda14cbcSMatt Macy 				zio_remove_child(pio, zio, remove_zl);
4767eda14cbcSMatt Macy 				/*
4768eda14cbcSMatt Macy 				 * This is a rare code path, so we don't
4769eda14cbcSMatt Macy 				 * bother with "next_to_execute".
4770eda14cbcSMatt Macy 				 */
4771eda14cbcSMatt Macy 				zio_notify_parent(pio, zio, ZIO_WAIT_DONE,
4772eda14cbcSMatt Macy 				    NULL);
4773eda14cbcSMatt Macy 			}
4774eda14cbcSMatt Macy 		}
4775eda14cbcSMatt Macy 
4776eda14cbcSMatt Macy 		if ((pio = zio_unique_parent(zio)) != NULL) {
4777eda14cbcSMatt Macy 			/*
4778eda14cbcSMatt Macy 			 * We're not a root i/o, so there's nothing to do
4779eda14cbcSMatt Macy 			 * but notify our parent.  Don't propagate errors
4780eda14cbcSMatt Macy 			 * upward since we haven't permanently failed yet.
4781eda14cbcSMatt Macy 			 */
4782eda14cbcSMatt Macy 			ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER));
4783eda14cbcSMatt Macy 			zio->io_flags |= ZIO_FLAG_DONT_PROPAGATE;
4784eda14cbcSMatt Macy 			/*
4785eda14cbcSMatt Macy 			 * This is a rare code path, so we don't bother with
4786eda14cbcSMatt Macy 			 * "next_to_execute".
4787eda14cbcSMatt Macy 			 */
4788eda14cbcSMatt Macy 			zio_notify_parent(pio, zio, ZIO_WAIT_DONE, NULL);
4789eda14cbcSMatt Macy 		} else if (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND) {
4790eda14cbcSMatt Macy 			/*
4791eda14cbcSMatt Macy 			 * We'd fail again if we reexecuted now, so suspend
4792eda14cbcSMatt Macy 			 * until conditions improve (e.g. device comes online).
4793eda14cbcSMatt Macy 			 */
4794eda14cbcSMatt Macy 			zio_suspend(zio->io_spa, zio, ZIO_SUSPEND_IOERR);
4795eda14cbcSMatt Macy 		} else {
4796eda14cbcSMatt Macy 			/*
4797eda14cbcSMatt Macy 			 * Reexecution is potentially a huge amount of work.
4798eda14cbcSMatt Macy 			 * Hand it off to the otherwise-unused claim taskq.
4799eda14cbcSMatt Macy 			 */
4800eda14cbcSMatt Macy 			ASSERT(taskq_empty_ent(&zio->io_tqent));
4801eda14cbcSMatt Macy 			spa_taskq_dispatch_ent(zio->io_spa,
4802eda14cbcSMatt Macy 			    ZIO_TYPE_CLAIM, ZIO_TASKQ_ISSUE,
48033f9d360cSMartin Matuska 			    zio_reexecute, zio, 0, &zio->io_tqent);
4804eda14cbcSMatt Macy 		}
4805eda14cbcSMatt Macy 		return (NULL);
4806eda14cbcSMatt Macy 	}
4807eda14cbcSMatt Macy 
4808eda14cbcSMatt Macy 	ASSERT(zio->io_child_count == 0);
4809eda14cbcSMatt Macy 	ASSERT(zio->io_reexecute == 0);
4810eda14cbcSMatt Macy 	ASSERT(zio->io_error == 0 || (zio->io_flags & ZIO_FLAG_CANFAIL));
4811eda14cbcSMatt Macy 
4812eda14cbcSMatt Macy 	/*
4813eda14cbcSMatt Macy 	 * Report any checksum errors, since the I/O is complete.
4814eda14cbcSMatt Macy 	 */
4815eda14cbcSMatt Macy 	while (zio->io_cksum_report != NULL) {
4816eda14cbcSMatt Macy 		zio_cksum_report_t *zcr = zio->io_cksum_report;
4817eda14cbcSMatt Macy 		zio->io_cksum_report = zcr->zcr_next;
4818eda14cbcSMatt Macy 		zcr->zcr_next = NULL;
4819eda14cbcSMatt Macy 		zcr->zcr_finish(zcr, NULL);
4820eda14cbcSMatt Macy 		zfs_ereport_free_checksum(zcr);
4821eda14cbcSMatt Macy 	}
4822eda14cbcSMatt Macy 
4823eda14cbcSMatt Macy 	if (zio->io_flags & ZIO_FLAG_FASTWRITE && zio->io_bp &&
4824eda14cbcSMatt Macy 	    !BP_IS_HOLE(zio->io_bp) && !BP_IS_EMBEDDED(zio->io_bp) &&
4825eda14cbcSMatt Macy 	    !(zio->io_flags & ZIO_FLAG_NOPWRITE)) {
4826eda14cbcSMatt Macy 		metaslab_fastwrite_unmark(zio->io_spa, zio->io_bp);
4827eda14cbcSMatt Macy 	}
4828eda14cbcSMatt Macy 
4829eda14cbcSMatt Macy 	/*
4830eda14cbcSMatt Macy 	 * It is the responsibility of the done callback to ensure that this
4831eda14cbcSMatt Macy 	 * particular zio is no longer discoverable for adoption, and as
4832eda14cbcSMatt Macy 	 * such, cannot acquire any new parents.
4833eda14cbcSMatt Macy 	 */
4834eda14cbcSMatt Macy 	if (zio->io_done)
4835eda14cbcSMatt Macy 		zio->io_done(zio);
4836eda14cbcSMatt Macy 
4837eda14cbcSMatt Macy 	mutex_enter(&zio->io_lock);
4838eda14cbcSMatt Macy 	zio->io_state[ZIO_WAIT_DONE] = 1;
4839eda14cbcSMatt Macy 	mutex_exit(&zio->io_lock);
4840eda14cbcSMatt Macy 
4841eda14cbcSMatt Macy 	/*
4842eda14cbcSMatt Macy 	 * We are done executing this zio.  We may want to execute a parent
4843eda14cbcSMatt Macy 	 * next.  See the comment in zio_notify_parent().
4844eda14cbcSMatt Macy 	 */
4845eda14cbcSMatt Macy 	zio_t *next_to_execute = NULL;
4846eda14cbcSMatt Macy 	zl = NULL;
4847eda14cbcSMatt Macy 	for (pio = zio_walk_parents(zio, &zl); pio != NULL; pio = pio_next) {
4848eda14cbcSMatt Macy 		zio_link_t *remove_zl = zl;
4849eda14cbcSMatt Macy 		pio_next = zio_walk_parents(zio, &zl);
4850eda14cbcSMatt Macy 		zio_remove_child(pio, zio, remove_zl);
4851eda14cbcSMatt Macy 		zio_notify_parent(pio, zio, ZIO_WAIT_DONE, &next_to_execute);
4852eda14cbcSMatt Macy 	}
4853eda14cbcSMatt Macy 
4854eda14cbcSMatt Macy 	if (zio->io_waiter != NULL) {
4855eda14cbcSMatt Macy 		mutex_enter(&zio->io_lock);
4856eda14cbcSMatt Macy 		zio->io_executor = NULL;
4857eda14cbcSMatt Macy 		cv_broadcast(&zio->io_cv);
4858eda14cbcSMatt Macy 		mutex_exit(&zio->io_lock);
4859eda14cbcSMatt Macy 	} else {
4860eda14cbcSMatt Macy 		zio_destroy(zio);
4861eda14cbcSMatt Macy 	}
4862eda14cbcSMatt Macy 
4863eda14cbcSMatt Macy 	return (next_to_execute);
4864eda14cbcSMatt Macy }
4865eda14cbcSMatt Macy 
4866eda14cbcSMatt Macy /*
4867eda14cbcSMatt Macy  * ==========================================================================
4868eda14cbcSMatt Macy  * I/O pipeline definition
4869eda14cbcSMatt Macy  * ==========================================================================
4870eda14cbcSMatt Macy  */
4871eda14cbcSMatt Macy static zio_pipe_stage_t *zio_pipeline[] = {
4872eda14cbcSMatt Macy 	NULL,
4873eda14cbcSMatt Macy 	zio_read_bp_init,
4874eda14cbcSMatt Macy 	zio_write_bp_init,
4875eda14cbcSMatt Macy 	zio_free_bp_init,
4876eda14cbcSMatt Macy 	zio_issue_async,
4877eda14cbcSMatt Macy 	zio_write_compress,
4878eda14cbcSMatt Macy 	zio_encrypt,
4879eda14cbcSMatt Macy 	zio_checksum_generate,
4880eda14cbcSMatt Macy 	zio_nop_write,
4881eda14cbcSMatt Macy 	zio_ddt_read_start,
4882eda14cbcSMatt Macy 	zio_ddt_read_done,
4883eda14cbcSMatt Macy 	zio_ddt_write,
4884eda14cbcSMatt Macy 	zio_ddt_free,
4885eda14cbcSMatt Macy 	zio_gang_assemble,
4886eda14cbcSMatt Macy 	zio_gang_issue,
4887eda14cbcSMatt Macy 	zio_dva_throttle,
4888eda14cbcSMatt Macy 	zio_dva_allocate,
4889eda14cbcSMatt Macy 	zio_dva_free,
4890eda14cbcSMatt Macy 	zio_dva_claim,
4891eda14cbcSMatt Macy 	zio_ready,
4892eda14cbcSMatt Macy 	zio_vdev_io_start,
4893eda14cbcSMatt Macy 	zio_vdev_io_done,
4894eda14cbcSMatt Macy 	zio_vdev_io_assess,
4895eda14cbcSMatt Macy 	zio_checksum_verify,
4896eda14cbcSMatt Macy 	zio_done
4897eda14cbcSMatt Macy };
4898eda14cbcSMatt Macy 
4899eda14cbcSMatt Macy 
4900eda14cbcSMatt Macy 
4901eda14cbcSMatt Macy 
4902eda14cbcSMatt Macy /*
4903eda14cbcSMatt Macy  * Compare two zbookmark_phys_t's to see which we would reach first in a
4904eda14cbcSMatt Macy  * pre-order traversal of the object tree.
4905eda14cbcSMatt Macy  *
4906eda14cbcSMatt Macy  * This is simple in every case aside from the meta-dnode object. For all other
4907eda14cbcSMatt Macy  * objects, we traverse them in order (object 1 before object 2, and so on).
4908eda14cbcSMatt Macy  * However, all of these objects are traversed while traversing object 0, since
4909eda14cbcSMatt Macy  * the data it points to is the list of objects.  Thus, we need to convert to a
4910eda14cbcSMatt Macy  * canonical representation so we can compare meta-dnode bookmarks to
4911eda14cbcSMatt Macy  * non-meta-dnode bookmarks.
4912eda14cbcSMatt Macy  *
4913eda14cbcSMatt Macy  * We do this by calculating "equivalents" for each field of the zbookmark.
4914eda14cbcSMatt Macy  * zbookmarks outside of the meta-dnode use their own object and level, and
4915eda14cbcSMatt Macy  * calculate the level 0 equivalent (the first L0 blkid that is contained in the
4916eda14cbcSMatt Macy  * blocks this bookmark refers to) by multiplying their blkid by their span
4917eda14cbcSMatt Macy  * (the number of L0 blocks contained within one block at their level).
4918eda14cbcSMatt Macy  * zbookmarks inside the meta-dnode calculate their object equivalent
4919eda14cbcSMatt Macy  * (which is L0equiv * dnodes per data block), use 0 for their L0equiv, and use
4920eda14cbcSMatt Macy  * level + 1<<31 (any value larger than a level could ever be) for their level.
4921eda14cbcSMatt Macy  * This causes them to always compare before a bookmark in their object
4922eda14cbcSMatt Macy  * equivalent, compare appropriately to bookmarks in other objects, and to
4923eda14cbcSMatt Macy  * compare appropriately to other bookmarks in the meta-dnode.
4924eda14cbcSMatt Macy  */
4925eda14cbcSMatt Macy int
4926eda14cbcSMatt Macy zbookmark_compare(uint16_t dbss1, uint8_t ibs1, uint16_t dbss2, uint8_t ibs2,
4927eda14cbcSMatt Macy     const zbookmark_phys_t *zb1, const zbookmark_phys_t *zb2)
4928eda14cbcSMatt Macy {
4929eda14cbcSMatt Macy 	/*
4930eda14cbcSMatt Macy 	 * These variables represent the "equivalent" values for the zbookmark,
4931eda14cbcSMatt Macy 	 * after converting zbookmarks inside the meta dnode to their
4932eda14cbcSMatt Macy 	 * normal-object equivalents.
4933eda14cbcSMatt Macy 	 */
4934eda14cbcSMatt Macy 	uint64_t zb1obj, zb2obj;
4935eda14cbcSMatt Macy 	uint64_t zb1L0, zb2L0;
4936eda14cbcSMatt Macy 	uint64_t zb1level, zb2level;
4937eda14cbcSMatt Macy 
4938eda14cbcSMatt Macy 	if (zb1->zb_object == zb2->zb_object &&
4939eda14cbcSMatt Macy 	    zb1->zb_level == zb2->zb_level &&
4940eda14cbcSMatt Macy 	    zb1->zb_blkid == zb2->zb_blkid)
4941eda14cbcSMatt Macy 		return (0);
4942eda14cbcSMatt Macy 
4943eda14cbcSMatt Macy 	IMPLY(zb1->zb_level > 0, ibs1 >= SPA_MINBLOCKSHIFT);
4944eda14cbcSMatt Macy 	IMPLY(zb2->zb_level > 0, ibs2 >= SPA_MINBLOCKSHIFT);
4945eda14cbcSMatt Macy 
4946eda14cbcSMatt Macy 	/*
4947eda14cbcSMatt Macy 	 * BP_SPANB calculates the span in blocks.
4948eda14cbcSMatt Macy 	 */
4949eda14cbcSMatt Macy 	zb1L0 = (zb1->zb_blkid) * BP_SPANB(ibs1, zb1->zb_level);
4950eda14cbcSMatt Macy 	zb2L0 = (zb2->zb_blkid) * BP_SPANB(ibs2, zb2->zb_level);
4951eda14cbcSMatt Macy 
4952eda14cbcSMatt Macy 	if (zb1->zb_object == DMU_META_DNODE_OBJECT) {
4953eda14cbcSMatt Macy 		zb1obj = zb1L0 * (dbss1 << (SPA_MINBLOCKSHIFT - DNODE_SHIFT));
4954eda14cbcSMatt Macy 		zb1L0 = 0;
4955eda14cbcSMatt Macy 		zb1level = zb1->zb_level + COMPARE_META_LEVEL;
4956eda14cbcSMatt Macy 	} else {
4957eda14cbcSMatt Macy 		zb1obj = zb1->zb_object;
4958eda14cbcSMatt Macy 		zb1level = zb1->zb_level;
4959eda14cbcSMatt Macy 	}
4960eda14cbcSMatt Macy 
4961eda14cbcSMatt Macy 	if (zb2->zb_object == DMU_META_DNODE_OBJECT) {
4962eda14cbcSMatt Macy 		zb2obj = zb2L0 * (dbss2 << (SPA_MINBLOCKSHIFT - DNODE_SHIFT));
4963eda14cbcSMatt Macy 		zb2L0 = 0;
4964eda14cbcSMatt Macy 		zb2level = zb2->zb_level + COMPARE_META_LEVEL;
4965eda14cbcSMatt Macy 	} else {
4966eda14cbcSMatt Macy 		zb2obj = zb2->zb_object;
4967eda14cbcSMatt Macy 		zb2level = zb2->zb_level;
4968eda14cbcSMatt Macy 	}
4969eda14cbcSMatt Macy 
4970eda14cbcSMatt Macy 	/* Now that we have a canonical representation, do the comparison. */
4971eda14cbcSMatt Macy 	if (zb1obj != zb2obj)
4972eda14cbcSMatt Macy 		return (zb1obj < zb2obj ? -1 : 1);
4973eda14cbcSMatt Macy 	else if (zb1L0 != zb2L0)
4974eda14cbcSMatt Macy 		return (zb1L0 < zb2L0 ? -1 : 1);
4975eda14cbcSMatt Macy 	else if (zb1level != zb2level)
4976eda14cbcSMatt Macy 		return (zb1level > zb2level ? -1 : 1);
4977eda14cbcSMatt Macy 	/*
4978eda14cbcSMatt Macy 	 * This can (theoretically) happen if the bookmarks have the same object
4979eda14cbcSMatt Macy 	 * and level, but different blkids, if the block sizes are not the same.
4980eda14cbcSMatt Macy 	 * There is presently no way to change the indirect block sizes
4981eda14cbcSMatt Macy 	 */
4982eda14cbcSMatt Macy 	return (0);
4983eda14cbcSMatt Macy }
4984eda14cbcSMatt Macy 
4985eda14cbcSMatt Macy /*
4986eda14cbcSMatt Macy  *  This function checks the following: given that last_block is the place that
4987eda14cbcSMatt Macy  *  our traversal stopped last time, does that guarantee that we've visited
4988eda14cbcSMatt Macy  *  every node under subtree_root?  Therefore, we can't just use the raw output
4989eda14cbcSMatt Macy  *  of zbookmark_compare.  We have to pass in a modified version of
4990eda14cbcSMatt Macy  *  subtree_root; by incrementing the block id, and then checking whether
4991eda14cbcSMatt Macy  *  last_block is before or equal to that, we can tell whether or not having
4992eda14cbcSMatt Macy  *  visited last_block implies that all of subtree_root's children have been
4993eda14cbcSMatt Macy  *  visited.
4994eda14cbcSMatt Macy  */
4995eda14cbcSMatt Macy boolean_t
4996eda14cbcSMatt Macy zbookmark_subtree_completed(const dnode_phys_t *dnp,
4997eda14cbcSMatt Macy     const zbookmark_phys_t *subtree_root, const zbookmark_phys_t *last_block)
4998eda14cbcSMatt Macy {
4999eda14cbcSMatt Macy 	zbookmark_phys_t mod_zb = *subtree_root;
5000eda14cbcSMatt Macy 	mod_zb.zb_blkid++;
5001eda14cbcSMatt Macy 	ASSERT(last_block->zb_level == 0);
5002eda14cbcSMatt Macy 
5003eda14cbcSMatt Macy 	/* The objset_phys_t isn't before anything. */
5004eda14cbcSMatt Macy 	if (dnp == NULL)
5005eda14cbcSMatt Macy 		return (B_FALSE);
5006eda14cbcSMatt Macy 
5007eda14cbcSMatt Macy 	/*
5008eda14cbcSMatt Macy 	 * We pass in 1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT) for the
5009eda14cbcSMatt Macy 	 * data block size in sectors, because that variable is only used if
5010eda14cbcSMatt Macy 	 * the bookmark refers to a block in the meta-dnode.  Since we don't
5011eda14cbcSMatt Macy 	 * know without examining it what object it refers to, and there's no
5012eda14cbcSMatt Macy 	 * harm in passing in this value in other cases, we always pass it in.
5013eda14cbcSMatt Macy 	 *
5014eda14cbcSMatt Macy 	 * We pass in 0 for the indirect block size shift because zb2 must be
5015eda14cbcSMatt Macy 	 * level 0.  The indirect block size is only used to calculate the span
5016eda14cbcSMatt Macy 	 * of the bookmark, but since the bookmark must be level 0, the span is
5017eda14cbcSMatt Macy 	 * always 1, so the math works out.
5018eda14cbcSMatt Macy 	 *
5019eda14cbcSMatt Macy 	 * If you make changes to how the zbookmark_compare code works, be sure
5020eda14cbcSMatt Macy 	 * to make sure that this code still works afterwards.
5021eda14cbcSMatt Macy 	 */
5022eda14cbcSMatt Macy 	return (zbookmark_compare(dnp->dn_datablkszsec, dnp->dn_indblkshift,
5023eda14cbcSMatt Macy 	    1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT), 0, &mod_zb,
5024eda14cbcSMatt Macy 	    last_block) <= 0);
5025eda14cbcSMatt Macy }
5026eda14cbcSMatt Macy 
5027eda14cbcSMatt Macy EXPORT_SYMBOL(zio_type_name);
5028eda14cbcSMatt Macy EXPORT_SYMBOL(zio_buf_alloc);
5029eda14cbcSMatt Macy EXPORT_SYMBOL(zio_data_buf_alloc);
5030eda14cbcSMatt Macy EXPORT_SYMBOL(zio_buf_free);
5031eda14cbcSMatt Macy EXPORT_SYMBOL(zio_data_buf_free);
5032eda14cbcSMatt Macy 
5033eda14cbcSMatt Macy /* BEGIN CSTYLED */
5034eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs_zio, zio_, slow_io_ms, INT, ZMOD_RW,
5035eda14cbcSMatt Macy 	"Max I/O completion time (milliseconds) before marking it as slow");
5036eda14cbcSMatt Macy 
5037eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs_zio, zio_, requeue_io_start_cut_in_line, INT, ZMOD_RW,
5038eda14cbcSMatt Macy 	"Prioritize requeued I/O");
5039eda14cbcSMatt Macy 
5040eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs, zfs_, sync_pass_deferred_free,  INT, ZMOD_RW,
5041eda14cbcSMatt Macy 	"Defer frees starting in this pass");
5042eda14cbcSMatt Macy 
5043eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs, zfs_, sync_pass_dont_compress, INT, ZMOD_RW,
5044eda14cbcSMatt Macy 	"Don't compress starting in this pass");
5045eda14cbcSMatt Macy 
5046eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs, zfs_, sync_pass_rewrite, INT, ZMOD_RW,
5047eda14cbcSMatt Macy 	"Rewrite new bps starting in this pass");
5048eda14cbcSMatt Macy 
5049eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs_zio, zio_, dva_throttle_enabled, INT, ZMOD_RW,
5050eda14cbcSMatt Macy 	"Throttle block allocations in the ZIO pipeline");
5051eda14cbcSMatt Macy 
5052eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs_zio, zio_, deadman_log_all, INT, ZMOD_RW,
5053eda14cbcSMatt Macy 	"Log all slow ZIOs, not just those with vdevs");
5054eda14cbcSMatt Macy /* END CSTYLED */
5055