xref: /illumos-gate/usr/src/uts/common/fs/zfs/zio.c (revision a3874b8b1fe5103fc1f961609557c0587435fec0)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
223f9d6ad7SLin Ling  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23f78cdc34SPaul Dagnelie  * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
245aeb9474SGarrett D'Amore  * Copyright (c) 2011 Nexenta Systems, Inc. All rights reserved.
25c3d26abcSMatthew Ahrens  * Copyright (c) 2014 Integros [integros.com]
26663207adSDon Brady  * Copyright (c) 2017, Intel Corporation.
27fa9e4066Sahrens  */
28fa9e4066Sahrens 
29de710d24SJosef 'Jeff' Sipek #include <sys/sysmacros.h>
30fa9e4066Sahrens #include <sys/zfs_context.h>
31ea8dc4b6Seschrock #include <sys/fm/fs/zfs.h>
32fa9e4066Sahrens #include <sys/spa.h>
33fa9e4066Sahrens #include <sys/txg.h>
34fa9e4066Sahrens #include <sys/spa_impl.h>
35fa9e4066Sahrens #include <sys/vdev_impl.h>
36fa9e4066Sahrens #include <sys/zio_impl.h>
37fa9e4066Sahrens #include <sys/zio_compress.h>
38fa9e4066Sahrens #include <sys/zio_checksum.h>
39b24ab676SJeff Bonwick #include <sys/dmu_objset.h>
40b24ab676SJeff Bonwick #include <sys/arc.h>
41b24ab676SJeff Bonwick #include <sys/ddt.h>
425d7b4d43SMatthew Ahrens #include <sys/blkptr.h>
4343466aaeSMax Grossman #include <sys/zfeature.h>
44*a3874b8bSToomas Soome #include <sys/dsl_scan.h>
450f7643c7SGeorge Wilson #include <sys/metaslab_impl.h>
46770499e1SDan Kimmel #include <sys/abd.h>
47f78cdc34SPaul Dagnelie #include <sys/cityhash.h>
48fa9e4066Sahrens 
49fa9e4066Sahrens /*
50fa9e4066Sahrens  * ==========================================================================
51fa9e4066Sahrens  * I/O type descriptions
52fa9e4066Sahrens  * ==========================================================================
53fa9e4066Sahrens  */
5469962b56SMatthew Ahrens const char *zio_type_name[ZIO_TYPES] = {
5580eb36f2SGeorge Wilson 	"zio_null", "zio_read", "zio_write", "zio_free", "zio_claim",
5680eb36f2SGeorge Wilson 	"zio_ioctl"
5780eb36f2SGeorge Wilson };
58fa9e4066Sahrens 
590f7643c7SGeorge Wilson boolean_t zio_dva_throttle_enabled = B_TRUE;
600f7643c7SGeorge Wilson 
61fa9e4066Sahrens /*
62fa9e4066Sahrens  * ==========================================================================
63fa9e4066Sahrens  * I/O kmem caches
64fa9e4066Sahrens  * ==========================================================================
65fa9e4066Sahrens  */
66ccae0b50Seschrock kmem_cache_t *zio_cache;
67a3f829aeSBill Moore kmem_cache_t *zio_link_cache;
68fa9e4066Sahrens kmem_cache_t *zio_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
69ad23a2dbSjohansen kmem_cache_t *zio_data_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
70ad23a2dbSjohansen 
71ad23a2dbSjohansen #ifdef _KERNEL
72ad23a2dbSjohansen extern vmem_t *zio_alloc_arena;
73ad23a2dbSjohansen #endif
74fa9e4066Sahrens 
75738f37bcSGeorge Wilson #define	ZIO_PIPELINE_CONTINUE		0x100
76738f37bcSGeorge Wilson #define	ZIO_PIPELINE_STOP		0x101
77738f37bcSGeorge Wilson 
78a2cdcdd2SPaul Dagnelie #define	BP_SPANB(indblkshift, level) \
79a2cdcdd2SPaul Dagnelie 	(((uint64_t)1) << ((level) * ((indblkshift) - SPA_BLKPTRSHIFT)))
80a2cdcdd2SPaul Dagnelie #define	COMPARE_META_LEVEL	0x80000000ul
8101f55e48SGeorge Wilson /*
8201f55e48SGeorge Wilson  * The following actions directly effect the spa's sync-to-convergence logic.
8301f55e48SGeorge Wilson  * The values below define the sync pass when we start performing the action.
8401f55e48SGeorge Wilson  * Care should be taken when changing these values as they directly impact
8501f55e48SGeorge Wilson  * spa_sync() performance. Tuning these values may introduce subtle performance
8601f55e48SGeorge Wilson  * pathologies and should only be done in the context of performance analysis.
8701f55e48SGeorge Wilson  * These tunables will eventually be removed and replaced with #defines once
8801f55e48SGeorge Wilson  * enough analysis has been done to determine optimal values.
8901f55e48SGeorge Wilson  *
9001f55e48SGeorge Wilson  * The 'zfs_sync_pass_deferred_free' pass must be greater than 1 to ensure that
9101f55e48SGeorge Wilson  * regular blocks are not deferred.
9201f55e48SGeorge Wilson  */
9301f55e48SGeorge Wilson int zfs_sync_pass_deferred_free = 2; /* defer frees starting in this pass */
9401f55e48SGeorge Wilson int zfs_sync_pass_dont_compress = 5; /* don't compress starting in this pass */
9501f55e48SGeorge Wilson int zfs_sync_pass_rewrite = 2; /* rewrite new bps starting in this pass */
9601f55e48SGeorge Wilson 
970a4e9518Sgw /*
98e14bb325SJeff Bonwick  * An allocating zio is one that either currently has the DVA allocate
99e14bb325SJeff Bonwick  * stage set or will have it later in its lifetime.
1000a4e9518Sgw  */
101b24ab676SJeff Bonwick #define	IO_IS_ALLOCATING(zio) ((zio)->io_orig_pipeline & ZIO_STAGE_DVA_ALLOCATE)
102b24ab676SJeff Bonwick 
10335a5a358SJonathan Adams boolean_t	zio_requeue_io_start_cut_in_line = B_TRUE;
10435a5a358SJonathan Adams 
105b24ab676SJeff Bonwick #ifdef ZFS_DEBUG
106b24ab676SJeff Bonwick int zio_buf_debug_limit = 16384;
107b24ab676SJeff Bonwick #else
108b24ab676SJeff Bonwick int zio_buf_debug_limit = 0;
109b24ab676SJeff Bonwick #endif
1100a4e9518Sgw 
1110f7643c7SGeorge Wilson static void zio_taskq_dispatch(zio_t *, zio_taskq_type_t, boolean_t);
1120f7643c7SGeorge Wilson 
113fa9e4066Sahrens void
114fa9e4066Sahrens zio_init(void)
115fa9e4066Sahrens {
116fa9e4066Sahrens 	size_t c;
117ad23a2dbSjohansen 	vmem_t *data_alloc_arena = NULL;
118ad23a2dbSjohansen 
119ad23a2dbSjohansen #ifdef _KERNEL
120ad23a2dbSjohansen 	data_alloc_arena = zio_alloc_arena;
121ad23a2dbSjohansen #endif
122a3f829aeSBill Moore 	zio_cache = kmem_cache_create("zio_cache",
123a3f829aeSBill Moore 	    sizeof (zio_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
124a3f829aeSBill Moore 	zio_link_cache = kmem_cache_create("zio_link_cache",
125a3f829aeSBill Moore 	    sizeof (zio_link_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
126ccae0b50Seschrock 
127fa9e4066Sahrens 	/*
128fa9e4066Sahrens 	 * For small buffers, we want a cache for each multiple of
129b5152584SMatthew Ahrens 	 * SPA_MINBLOCKSIZE.  For larger buffers, we want a cache
130b5152584SMatthew Ahrens 	 * for each quarter-power of 2.
131fa9e4066Sahrens 	 */
132fa9e4066Sahrens 	for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) {
133fa9e4066Sahrens 		size_t size = (c + 1) << SPA_MINBLOCKSHIFT;
134fa9e4066Sahrens 		size_t p2 = size;
135fa9e4066Sahrens 		size_t align = 0;
136e291592aSJonathan Adams 		size_t cflags = (size > zio_buf_debug_limit) ? KMC_NODEBUG : 0;
137fa9e4066Sahrens 
138de710d24SJosef 'Jeff' Sipek 		while (!ISP2(p2))
139fa9e4066Sahrens 			p2 &= p2 - 1;
140fa9e4066Sahrens 
141cd1c8b85SMatthew Ahrens #ifndef _KERNEL
142cd1c8b85SMatthew Ahrens 		/*
143cd1c8b85SMatthew Ahrens 		 * If we are using watchpoints, put each buffer on its own page,
144cd1c8b85SMatthew Ahrens 		 * to eliminate the performance overhead of trapping to the
145cd1c8b85SMatthew Ahrens 		 * kernel when modifying a non-watched buffer that shares the
146cd1c8b85SMatthew Ahrens 		 * page with a watched buffer.
147cd1c8b85SMatthew Ahrens 		 */
148cd1c8b85SMatthew Ahrens 		if (arc_watch && !IS_P2ALIGNED(size, PAGESIZE))
149cd1c8b85SMatthew Ahrens 			continue;
150cd1c8b85SMatthew Ahrens #endif
151fa9e4066Sahrens 		if (size <= 4 * SPA_MINBLOCKSIZE) {
152fa9e4066Sahrens 			align = SPA_MINBLOCKSIZE;
153cd1c8b85SMatthew Ahrens 		} else if (IS_P2ALIGNED(size, p2 >> 2)) {
154b5152584SMatthew Ahrens 			align = MIN(p2 >> 2, PAGESIZE);
155fa9e4066Sahrens 		}
156fa9e4066Sahrens 
157fa9e4066Sahrens 		if (align != 0) {
158ad23a2dbSjohansen 			char name[36];
1595ad82045Snd 			(void) sprintf(name, "zio_buf_%lu", (ulong_t)size);
160fa9e4066Sahrens 			zio_buf_cache[c] = kmem_cache_create(name, size,
161e291592aSJonathan Adams 			    align, NULL, NULL, NULL, NULL, NULL, cflags);
162ad23a2dbSjohansen 
163e291592aSJonathan Adams 			/*
164e291592aSJonathan Adams 			 * Since zio_data bufs do not appear in crash dumps, we
165e291592aSJonathan Adams 			 * pass KMC_NOTOUCH so that no allocator metadata is
166e291592aSJonathan Adams 			 * stored with the buffers.
167e291592aSJonathan Adams 			 */
168ad23a2dbSjohansen 			(void) sprintf(name, "zio_data_buf_%lu", (ulong_t)size);
169ad23a2dbSjohansen 			zio_data_buf_cache[c] = kmem_cache_create(name, size,
170ad23a2dbSjohansen 			    align, NULL, NULL, NULL, NULL, data_alloc_arena,
171e291592aSJonathan Adams 			    cflags | KMC_NOTOUCH);
172fa9e4066Sahrens 		}
173fa9e4066Sahrens 	}
174fa9e4066Sahrens 
175fa9e4066Sahrens 	while (--c != 0) {
176fa9e4066Sahrens 		ASSERT(zio_buf_cache[c] != NULL);
177fa9e4066Sahrens 		if (zio_buf_cache[c - 1] == NULL)
178fa9e4066Sahrens 			zio_buf_cache[c - 1] = zio_buf_cache[c];
179ad23a2dbSjohansen 
180ad23a2dbSjohansen 		ASSERT(zio_data_buf_cache[c] != NULL);
181ad23a2dbSjohansen 		if (zio_data_buf_cache[c - 1] == NULL)
182ad23a2dbSjohansen 			zio_data_buf_cache[c - 1] = zio_data_buf_cache[c];
183fa9e4066Sahrens 	}
184ea8dc4b6Seschrock 
185ea8dc4b6Seschrock 	zio_inject_init();
186fa9e4066Sahrens }
187fa9e4066Sahrens 
188fa9e4066Sahrens void
189fa9e4066Sahrens zio_fini(void)
190fa9e4066Sahrens {
191fa9e4066Sahrens 	size_t c;
192fa9e4066Sahrens 	kmem_cache_t *last_cache = NULL;
193ad23a2dbSjohansen 	kmem_cache_t *last_data_cache = NULL;
194fa9e4066Sahrens 
195fa9e4066Sahrens 	for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) {
196fa9e4066Sahrens 		if (zio_buf_cache[c] != last_cache) {
197fa9e4066Sahrens 			last_cache = zio_buf_cache[c];
198fa9e4066Sahrens 			kmem_cache_destroy(zio_buf_cache[c]);
199fa9e4066Sahrens 		}
200fa9e4066Sahrens 		zio_buf_cache[c] = NULL;
201ad23a2dbSjohansen 
202ad23a2dbSjohansen 		if (zio_data_buf_cache[c] != last_data_cache) {
203ad23a2dbSjohansen 			last_data_cache = zio_data_buf_cache[c];
204ad23a2dbSjohansen 			kmem_cache_destroy(zio_data_buf_cache[c]);
205ad23a2dbSjohansen 		}
206ad23a2dbSjohansen 		zio_data_buf_cache[c] = NULL;
207fa9e4066Sahrens 	}
208ea8dc4b6Seschrock 
209a3f829aeSBill Moore 	kmem_cache_destroy(zio_link_cache);
210ccae0b50Seschrock 	kmem_cache_destroy(zio_cache);
211ccae0b50Seschrock 
212ea8dc4b6Seschrock 	zio_inject_fini();
213fa9e4066Sahrens }
214fa9e4066Sahrens 
215fa9e4066Sahrens /*
216fa9e4066Sahrens  * ==========================================================================
217fa9e4066Sahrens  * Allocate and free I/O buffers
218fa9e4066Sahrens  * ==========================================================================
219fa9e4066Sahrens  */
220ad23a2dbSjohansen 
221ad23a2dbSjohansen /*
222ad23a2dbSjohansen  * Use zio_buf_alloc to allocate ZFS metadata.  This data will appear in a
223ad23a2dbSjohansen  * crashdump if the kernel panics, so use it judiciously.  Obviously, it's
224ad23a2dbSjohansen  * useful to inspect ZFS metadata, but if possible, we should avoid keeping
225ad23a2dbSjohansen  * excess / transient data in-core during a crashdump.
226ad23a2dbSjohansen  */
227fa9e4066Sahrens void *
228fa9e4066Sahrens zio_buf_alloc(size_t size)
229fa9e4066Sahrens {
230fa9e4066Sahrens 	size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
231fa9e4066Sahrens 
232f63ab3d5SMatthew Ahrens 	VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
233fa9e4066Sahrens 
2341ab7f2deSmaybee 	return (kmem_cache_alloc(zio_buf_cache[c], KM_PUSHPAGE));
235fa9e4066Sahrens }
236fa9e4066Sahrens 
237ad23a2dbSjohansen /*
238ad23a2dbSjohansen  * Use zio_data_buf_alloc to allocate data.  The data will not appear in a
239ad23a2dbSjohansen  * crashdump if the kernel panics.  This exists so that we will limit the amount
240ad23a2dbSjohansen  * of ZFS data that shows up in a kernel crashdump.  (Thus reducing the amount
241ad23a2dbSjohansen  * of kernel heap dumped to disk when the kernel panics)
242ad23a2dbSjohansen  */
243ad23a2dbSjohansen void *
244ad23a2dbSjohansen zio_data_buf_alloc(size_t size)
245ad23a2dbSjohansen {
246ad23a2dbSjohansen 	size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
247ad23a2dbSjohansen 
248f63ab3d5SMatthew Ahrens 	VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
249ad23a2dbSjohansen 
2501ab7f2deSmaybee 	return (kmem_cache_alloc(zio_data_buf_cache[c], KM_PUSHPAGE));
251ad23a2dbSjohansen }
252ad23a2dbSjohansen 
253fa9e4066Sahrens void
254fa9e4066Sahrens zio_buf_free(void *buf, size_t size)
255fa9e4066Sahrens {
256fa9e4066Sahrens 	size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
257fa9e4066Sahrens 
258f63ab3d5SMatthew Ahrens 	VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
259fa9e4066Sahrens 
260fa9e4066Sahrens 	kmem_cache_free(zio_buf_cache[c], buf);
261fa9e4066Sahrens }
262fa9e4066Sahrens 
263ad23a2dbSjohansen void
264ad23a2dbSjohansen zio_data_buf_free(void *buf, size_t size)
265ad23a2dbSjohansen {
266ad23a2dbSjohansen 	size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
267ad23a2dbSjohansen 
268f63ab3d5SMatthew Ahrens 	VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
269ad23a2dbSjohansen 
270ad23a2dbSjohansen 	kmem_cache_free(zio_data_buf_cache[c], buf);
271ad23a2dbSjohansen }
272b3995adbSahrens 
273fa9e4066Sahrens /*
274fa9e4066Sahrens  * ==========================================================================
275fa9e4066Sahrens  * Push and pop I/O transform buffers
276fa9e4066Sahrens  * ==========================================================================
277fa9e4066Sahrens  */
278dcbf3bd6SGeorge Wilson void
279770499e1SDan Kimmel zio_push_transform(zio_t *zio, abd_t *data, uint64_t size, uint64_t bufsize,
2809a686fbcSPaul Dagnelie     zio_transform_func_t *transform)
281fa9e4066Sahrens {
282fa9e4066Sahrens 	zio_transform_t *zt = kmem_alloc(sizeof (zio_transform_t), KM_SLEEP);
283fa9e4066Sahrens 
284770499e1SDan Kimmel 	/*
285770499e1SDan Kimmel 	 * Ensure that anyone expecting this zio to contain a linear ABD isn't
286770499e1SDan Kimmel 	 * going to get a nasty surprise when they try to access the data.
287770499e1SDan Kimmel 	 */
288770499e1SDan Kimmel 	IMPLY(abd_is_linear(zio->io_abd), abd_is_linear(data));
289770499e1SDan Kimmel 
290770499e1SDan Kimmel 	zt->zt_orig_abd = zio->io_abd;
291e14bb325SJeff Bonwick 	zt->zt_orig_size = zio->io_size;
292fa9e4066Sahrens 	zt->zt_bufsize = bufsize;
293e14bb325SJeff Bonwick 	zt->zt_transform = transform;
294fa9e4066Sahrens 
295fa9e4066Sahrens 	zt->zt_next = zio->io_transform_stack;
296fa9e4066Sahrens 	zio->io_transform_stack = zt;
297fa9e4066Sahrens 
298770499e1SDan Kimmel 	zio->io_abd = data;
299fa9e4066Sahrens 	zio->io_size = size;
300fa9e4066Sahrens }
301fa9e4066Sahrens 
302dcbf3bd6SGeorge Wilson void
303e14bb325SJeff Bonwick zio_pop_transforms(zio_t *zio)
304fa9e4066Sahrens {
305e14bb325SJeff Bonwick 	zio_transform_t *zt;
306e14bb325SJeff Bonwick 
307e14bb325SJeff Bonwick 	while ((zt = zio->io_transform_stack) != NULL) {
308e14bb325SJeff Bonwick 		if (zt->zt_transform != NULL)
309e14bb325SJeff Bonwick 			zt->zt_transform(zio,
310770499e1SDan Kimmel 			    zt->zt_orig_abd, zt->zt_orig_size);
311fa9e4066Sahrens 
312b24ab676SJeff Bonwick 		if (zt->zt_bufsize != 0)
313770499e1SDan Kimmel 			abd_free(zio->io_abd);
314fa9e4066Sahrens 
315770499e1SDan Kimmel 		zio->io_abd = zt->zt_orig_abd;
316e14bb325SJeff Bonwick 		zio->io_size = zt->zt_orig_size;
317e14bb325SJeff Bonwick 		zio->io_transform_stack = zt->zt_next;
318fa9e4066Sahrens 
319e14bb325SJeff Bonwick 		kmem_free(zt, sizeof (zio_transform_t));
320fa9e4066Sahrens 	}
321fa9e4066Sahrens }
322fa9e4066Sahrens 
323e14bb325SJeff Bonwick /*
324e14bb325SJeff Bonwick  * ==========================================================================
325e14bb325SJeff Bonwick  * I/O transform callbacks for subblocks and decompression
326e14bb325SJeff Bonwick  * ==========================================================================
327e14bb325SJeff Bonwick  */
328e14bb325SJeff Bonwick static void
329770499e1SDan Kimmel zio_subblock(zio_t *zio, abd_t *data, uint64_t size)
330e14bb325SJeff Bonwick {
331e14bb325SJeff Bonwick 	ASSERT(zio->io_size > size);
332e14bb325SJeff Bonwick 
333e14bb325SJeff Bonwick 	if (zio->io_type == ZIO_TYPE_READ)
334770499e1SDan Kimmel 		abd_copy(data, zio->io_abd, size);
335e14bb325SJeff Bonwick }
336e14bb325SJeff Bonwick 
337e14bb325SJeff Bonwick static void
338770499e1SDan Kimmel zio_decompress(zio_t *zio, abd_t *data, uint64_t size)
339e14bb325SJeff Bonwick {
340770499e1SDan Kimmel 	if (zio->io_error == 0) {
341770499e1SDan Kimmel 		void *tmp = abd_borrow_buf(data, size);
342770499e1SDan Kimmel 		int ret = zio_decompress_data(BP_GET_COMPRESS(zio->io_bp),
343770499e1SDan Kimmel 		    zio->io_abd, tmp, zio->io_size, size);
344770499e1SDan Kimmel 		abd_return_buf_copy(data, tmp, size);
345770499e1SDan Kimmel 
346770499e1SDan Kimmel 		if (ret != 0)
347770499e1SDan Kimmel 			zio->io_error = SET_ERROR(EIO);
348770499e1SDan Kimmel 	}
349e14bb325SJeff Bonwick }
350e14bb325SJeff Bonwick 
351e14bb325SJeff Bonwick /*
352e14bb325SJeff Bonwick  * ==========================================================================
353e14bb325SJeff Bonwick  * I/O parent/child relationships and pipeline interlocks
354e14bb325SJeff Bonwick  * ==========================================================================
355e14bb325SJeff Bonwick  */
356a3f829aeSBill Moore zio_t *
3570f7643c7SGeorge Wilson zio_walk_parents(zio_t *cio, zio_link_t **zl)
358a3f829aeSBill Moore {
359a3f829aeSBill Moore 	list_t *pl = &cio->io_parent_list;
360e14bb325SJeff Bonwick 
3610f7643c7SGeorge Wilson 	*zl = (*zl == NULL) ? list_head(pl) : list_next(pl, *zl);
3620f7643c7SGeorge Wilson 	if (*zl == NULL)
363a3f829aeSBill Moore 		return (NULL);
364a3f829aeSBill Moore 
3650f7643c7SGeorge Wilson 	ASSERT((*zl)->zl_child == cio);
3660f7643c7SGeorge Wilson 	return ((*zl)->zl_parent);
367a3f829aeSBill Moore }
368a3f829aeSBill Moore 
369a3f829aeSBill Moore zio_t *
3700f7643c7SGeorge Wilson zio_walk_children(zio_t *pio, zio_link_t **zl)
371a3f829aeSBill Moore {
372a3f829aeSBill Moore 	list_t *cl = &pio->io_child_list;
373a3f829aeSBill Moore 
374*a3874b8bSToomas Soome 	ASSERT(MUTEX_HELD(&pio->io_lock));
375*a3874b8bSToomas Soome 
3760f7643c7SGeorge Wilson 	*zl = (*zl == NULL) ? list_head(cl) : list_next(cl, *zl);
3770f7643c7SGeorge Wilson 	if (*zl == NULL)
378a3f829aeSBill Moore 		return (NULL);
379a3f829aeSBill Moore 
3800f7643c7SGeorge Wilson 	ASSERT((*zl)->zl_parent == pio);
3810f7643c7SGeorge Wilson 	return ((*zl)->zl_child);
382a3f829aeSBill Moore }
383a3f829aeSBill Moore 
384a3f829aeSBill Moore zio_t *
385a3f829aeSBill Moore zio_unique_parent(zio_t *cio)
386a3f829aeSBill Moore {
3870f7643c7SGeorge Wilson 	zio_link_t *zl = NULL;
3880f7643c7SGeorge Wilson 	zio_t *pio = zio_walk_parents(cio, &zl);
389a3f829aeSBill Moore 
3900f7643c7SGeorge Wilson 	VERIFY3P(zio_walk_parents(cio, &zl), ==, NULL);
391a3f829aeSBill Moore 	return (pio);
392a3f829aeSBill Moore }
393a3f829aeSBill Moore 
394a3f829aeSBill Moore void
395a3f829aeSBill Moore zio_add_child(zio_t *pio, zio_t *cio)
396e14bb325SJeff Bonwick {
397a3f829aeSBill Moore 	zio_link_t *zl = kmem_cache_alloc(zio_link_cache, KM_SLEEP);
398a3f829aeSBill Moore 
399a3f829aeSBill Moore 	/*
400a3f829aeSBill Moore 	 * Logical I/Os can have logical, gang, or vdev children.
401a3f829aeSBill Moore 	 * Gang I/Os can have gang or vdev children.
402a3f829aeSBill Moore 	 * Vdev I/Os can only have vdev children.
403a3f829aeSBill Moore 	 * The following ASSERT captures all of these constraints.
404a3f829aeSBill Moore 	 */
4051271e4b1SPrakash Surya 	ASSERT3S(cio->io_child_type, <=, pio->io_child_type);
406a3f829aeSBill Moore 
407a3f829aeSBill Moore 	zl->zl_parent = pio;
408a3f829aeSBill Moore 	zl->zl_child = cio;
409a3f829aeSBill Moore 
410e14bb325SJeff Bonwick 	mutex_enter(&pio->io_lock);
411*a3874b8bSToomas Soome 	mutex_enter(&cio->io_lock);
412a3f829aeSBill Moore 
413a3f829aeSBill Moore 	ASSERT(pio->io_state[ZIO_WAIT_DONE] == 0);
414a3f829aeSBill Moore 
415a3f829aeSBill Moore 	for (int w = 0; w < ZIO_WAIT_TYPES; w++)
416a3f829aeSBill Moore 		pio->io_children[cio->io_child_type][w] += !cio->io_state[w];
417a3f829aeSBill Moore 
418a3f829aeSBill Moore 	list_insert_head(&pio->io_child_list, zl);
419a3f829aeSBill Moore 	list_insert_head(&cio->io_parent_list, zl);
420a3f829aeSBill Moore 
421b24ab676SJeff Bonwick 	pio->io_child_count++;
422b24ab676SJeff Bonwick 	cio->io_parent_count++;
423b24ab676SJeff Bonwick 
424a3f829aeSBill Moore 	mutex_exit(&cio->io_lock);
425*a3874b8bSToomas Soome 	mutex_exit(&pio->io_lock);
426e14bb325SJeff Bonwick }
427e14bb325SJeff Bonwick 
428fa9e4066Sahrens static void
429a3f829aeSBill Moore zio_remove_child(zio_t *pio, zio_t *cio, zio_link_t *zl)
430e14bb325SJeff Bonwick {
431a3f829aeSBill Moore 	ASSERT(zl->zl_parent == pio);
432a3f829aeSBill Moore 	ASSERT(zl->zl_child == cio);
433e14bb325SJeff Bonwick 
434e14bb325SJeff Bonwick 	mutex_enter(&pio->io_lock);
435*a3874b8bSToomas Soome 	mutex_enter(&cio->io_lock);
436a3f829aeSBill Moore 
437a3f829aeSBill Moore 	list_remove(&pio->io_child_list, zl);
438a3f829aeSBill Moore 	list_remove(&cio->io_parent_list, zl);
439a3f829aeSBill Moore 
440b24ab676SJeff Bonwick 	pio->io_child_count--;
441b24ab676SJeff Bonwick 	cio->io_parent_count--;
442b24ab676SJeff Bonwick 
443a3f829aeSBill Moore 	mutex_exit(&cio->io_lock);
444*a3874b8bSToomas Soome 	mutex_exit(&pio->io_lock);
445a3f829aeSBill Moore 
446a3f829aeSBill Moore 	kmem_cache_free(zio_link_cache, zl);
447e14bb325SJeff Bonwick }
448e14bb325SJeff Bonwick 
449e14bb325SJeff Bonwick static boolean_t
450d6e1c446SGeorge Wilson zio_wait_for_children(zio_t *zio, uint8_t childbits, enum zio_wait_type wait)
451fa9e4066Sahrens {
452e14bb325SJeff Bonwick 	boolean_t waiting = B_FALSE;
453e14bb325SJeff Bonwick 
454e14bb325SJeff Bonwick 	mutex_enter(&zio->io_lock);
455e14bb325SJeff Bonwick 	ASSERT(zio->io_stall == NULL);
456d6e1c446SGeorge Wilson 	for (int c = 0; c < ZIO_CHILD_TYPES; c++) {
457d6e1c446SGeorge Wilson 		if (!(ZIO_CHILD_BIT_IS_SET(childbits, c)))
458d6e1c446SGeorge Wilson 			continue;
459d6e1c446SGeorge Wilson 
460d6e1c446SGeorge Wilson 		uint64_t *countp = &zio->io_children[c][wait];
461d6e1c446SGeorge Wilson 		if (*countp != 0) {
462d6e1c446SGeorge Wilson 			zio->io_stage >>= 1;
463d6e1c446SGeorge Wilson 			ASSERT3U(zio->io_stage, !=, ZIO_STAGE_OPEN);
464d6e1c446SGeorge Wilson 			zio->io_stall = countp;
465d6e1c446SGeorge Wilson 			waiting = B_TRUE;
466d6e1c446SGeorge Wilson 			break;
467d6e1c446SGeorge Wilson 		}
468e14bb325SJeff Bonwick 	}
469e14bb325SJeff Bonwick 	mutex_exit(&zio->io_lock);
470e14bb325SJeff Bonwick 	return (waiting);
471e14bb325SJeff Bonwick }
472fa9e4066Sahrens 
473e14bb325SJeff Bonwick static void
474e14bb325SJeff Bonwick zio_notify_parent(zio_t *pio, zio_t *zio, enum zio_wait_type wait)
475e14bb325SJeff Bonwick {
476e14bb325SJeff Bonwick 	uint64_t *countp = &pio->io_children[zio->io_child_type][wait];
477e14bb325SJeff Bonwick 	int *errorp = &pio->io_child_error[zio->io_child_type];
478fa9e4066Sahrens 
479e14bb325SJeff Bonwick 	mutex_enter(&pio->io_lock);
480e14bb325SJeff Bonwick 	if (zio->io_error && !(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
481e14bb325SJeff Bonwick 		*errorp = zio_worst_error(*errorp, zio->io_error);
482e14bb325SJeff Bonwick 	pio->io_reexecute |= zio->io_reexecute;
483e14bb325SJeff Bonwick 	ASSERT3U(*countp, >, 0);
48469962b56SMatthew Ahrens 
48569962b56SMatthew Ahrens 	(*countp)--;
48669962b56SMatthew Ahrens 
48769962b56SMatthew Ahrens 	if (*countp == 0 && pio->io_stall == countp) {
4880f7643c7SGeorge Wilson 		zio_taskq_type_t type =
4890f7643c7SGeorge Wilson 		    pio->io_stage < ZIO_STAGE_VDEV_IO_START ? ZIO_TASKQ_ISSUE :
4900f7643c7SGeorge Wilson 		    ZIO_TASKQ_INTERRUPT;
491e14bb325SJeff Bonwick 		pio->io_stall = NULL;
492e14bb325SJeff Bonwick 		mutex_exit(&pio->io_lock);
4930f7643c7SGeorge Wilson 		/*
4940f7643c7SGeorge Wilson 		 * Dispatch the parent zio in its own taskq so that
4950f7643c7SGeorge Wilson 		 * the child can continue to make progress. This also
4960f7643c7SGeorge Wilson 		 * prevents overflowing the stack when we have deeply nested
4970f7643c7SGeorge Wilson 		 * parent-child relationships.
4980f7643c7SGeorge Wilson 		 */
4990f7643c7SGeorge Wilson 		zio_taskq_dispatch(pio, type, B_FALSE);
500e14bb325SJeff Bonwick 	} else {
501e14bb325SJeff Bonwick 		mutex_exit(&pio->io_lock);
502fa9e4066Sahrens 	}
503fa9e4066Sahrens }
504fa9e4066Sahrens 
505e14bb325SJeff Bonwick static void
506e14bb325SJeff Bonwick zio_inherit_child_errors(zio_t *zio, enum zio_child c)
507e14bb325SJeff Bonwick {
508e14bb325SJeff Bonwick 	if (zio->io_child_error[c] != 0 && zio->io_error == 0)
509e14bb325SJeff Bonwick 		zio->io_error = zio->io_child_error[c];
510e14bb325SJeff Bonwick }
511e14bb325SJeff Bonwick 
5120f7643c7SGeorge Wilson int
51394c2d0ebSMatthew Ahrens zio_bookmark_compare(const void *x1, const void *x2)
5140f7643c7SGeorge Wilson {
5150f7643c7SGeorge Wilson 	const zio_t *z1 = x1;
5160f7643c7SGeorge Wilson 	const zio_t *z2 = x2;
5170f7643c7SGeorge Wilson 
51894c2d0ebSMatthew Ahrens 	if (z1->io_bookmark.zb_objset < z2->io_bookmark.zb_objset)
5190f7643c7SGeorge Wilson 		return (-1);
52094c2d0ebSMatthew Ahrens 	if (z1->io_bookmark.zb_objset > z2->io_bookmark.zb_objset)
5210f7643c7SGeorge Wilson 		return (1);
5220f7643c7SGeorge Wilson 
52394c2d0ebSMatthew Ahrens 	if (z1->io_bookmark.zb_object < z2->io_bookmark.zb_object)
5240f7643c7SGeorge Wilson 		return (-1);
52594c2d0ebSMatthew Ahrens 	if (z1->io_bookmark.zb_object > z2->io_bookmark.zb_object)
52694c2d0ebSMatthew Ahrens 		return (1);
52794c2d0ebSMatthew Ahrens 
52894c2d0ebSMatthew Ahrens 	if (z1->io_bookmark.zb_level < z2->io_bookmark.zb_level)
52994c2d0ebSMatthew Ahrens 		return (-1);
53094c2d0ebSMatthew Ahrens 	if (z1->io_bookmark.zb_level > z2->io_bookmark.zb_level)
53194c2d0ebSMatthew Ahrens 		return (1);
53294c2d0ebSMatthew Ahrens 
53394c2d0ebSMatthew Ahrens 	if (z1->io_bookmark.zb_blkid < z2->io_bookmark.zb_blkid)
53494c2d0ebSMatthew Ahrens 		return (-1);
53594c2d0ebSMatthew Ahrens 	if (z1->io_bookmark.zb_blkid > z2->io_bookmark.zb_blkid)
5360f7643c7SGeorge Wilson 		return (1);
5370f7643c7SGeorge Wilson 
5380f7643c7SGeorge Wilson 	if (z1 < z2)
5390f7643c7SGeorge Wilson 		return (-1);
5400f7643c7SGeorge Wilson 	if (z1 > z2)
5410f7643c7SGeorge Wilson 		return (1);
5420f7643c7SGeorge Wilson 
5430f7643c7SGeorge Wilson 	return (0);
5440f7643c7SGeorge Wilson }
5450f7643c7SGeorge Wilson 
546fa9e4066Sahrens /*
547fa9e4066Sahrens  * ==========================================================================
548e14bb325SJeff Bonwick  * Create the various types of I/O (read, write, free, etc)
549fa9e4066Sahrens  * ==========================================================================
550fa9e4066Sahrens  */
551fa9e4066Sahrens static zio_t *
552b24ab676SJeff Bonwick zio_create(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
553770499e1SDan Kimmel     abd_t *data, uint64_t lsize, uint64_t psize, zio_done_func_t *done,
5545602294fSDan Kimmel     void *private, zio_type_t type, zio_priority_t priority,
5555602294fSDan Kimmel     enum zio_flag flags, vdev_t *vd, uint64_t offset,
5565602294fSDan Kimmel     const zbookmark_phys_t *zb, enum zio_stage stage, enum zio_stage pipeline)
557fa9e4066Sahrens {
558fa9e4066Sahrens 	zio_t *zio;
559fa9e4066Sahrens 
5605602294fSDan Kimmel 	ASSERT3U(psize, <=, SPA_MAXBLOCKSIZE);
5615602294fSDan Kimmel 	ASSERT(P2PHASE(psize, SPA_MINBLOCKSIZE) == 0);
562e14bb325SJeff Bonwick 	ASSERT(P2PHASE(offset, SPA_MINBLOCKSIZE) == 0);
563fa9e4066Sahrens 
564e14bb325SJeff Bonwick 	ASSERT(!vd || spa_config_held(spa, SCL_STATE_ALL, RW_READER));
565e14bb325SJeff Bonwick 	ASSERT(!bp || !(flags & ZIO_FLAG_CONFIG_WRITER));
566e14bb325SJeff Bonwick 	ASSERT(vd || stage == ZIO_STAGE_OPEN);
567088f3894Sahrens 
5685602294fSDan Kimmel 	IMPLY(lsize != psize, (flags & ZIO_FLAG_RAW) != 0);
5695602294fSDan Kimmel 
570ccae0b50Seschrock 	zio = kmem_cache_alloc(zio_cache, KM_SLEEP);
571ccae0b50Seschrock 	bzero(zio, sizeof (zio_t));
572e14bb325SJeff Bonwick 
573e14bb325SJeff Bonwick 	mutex_init(&zio->io_lock, NULL, MUTEX_DEFAULT, NULL);
574e14bb325SJeff Bonwick 	cv_init(&zio->io_cv, NULL, CV_DEFAULT, NULL);
575e14bb325SJeff Bonwick 
576a3f829aeSBill Moore 	list_create(&zio->io_parent_list, sizeof (zio_link_t),
577a3f829aeSBill Moore 	    offsetof(zio_link_t, zl_parent_node));
578a3f829aeSBill Moore 	list_create(&zio->io_child_list, sizeof (zio_link_t),
579a3f829aeSBill Moore 	    offsetof(zio_link_t, zl_child_node));
5808363e80aSGeorge Wilson 	metaslab_trace_init(&zio->io_alloc_list);
581a3f829aeSBill Moore 
582e14bb325SJeff Bonwick 	if (vd != NULL)
583e14bb325SJeff Bonwick 		zio->io_child_type = ZIO_CHILD_VDEV;
584e14bb325SJeff Bonwick 	else if (flags & ZIO_FLAG_GANG_CHILD)
585e14bb325SJeff Bonwick 		zio->io_child_type = ZIO_CHILD_GANG;
586b24ab676SJeff Bonwick 	else if (flags & ZIO_FLAG_DDT_CHILD)
587b24ab676SJeff Bonwick 		zio->io_child_type = ZIO_CHILD_DDT;
588e14bb325SJeff Bonwick 	else
589e14bb325SJeff Bonwick 		zio->io_child_type = ZIO_CHILD_LOGICAL;
590e14bb325SJeff Bonwick 
591fa9e4066Sahrens 	if (bp != NULL) {
592b24ab676SJeff Bonwick 		zio->io_bp = (blkptr_t *)bp;
593fa9e4066Sahrens 		zio->io_bp_copy = *bp;
594fa9e4066Sahrens 		zio->io_bp_orig = *bp;
595b24ab676SJeff Bonwick 		if (type != ZIO_TYPE_WRITE ||
596b24ab676SJeff Bonwick 		    zio->io_child_type == ZIO_CHILD_DDT)
597e14bb325SJeff Bonwick 			zio->io_bp = &zio->io_bp_copy;	/* so caller can free */
598f5383399SBill Moore 		if (zio->io_child_type == ZIO_CHILD_LOGICAL)
599e14bb325SJeff Bonwick 			zio->io_logical = zio;
600f5383399SBill Moore 		if (zio->io_child_type > ZIO_CHILD_GANG && BP_IS_GANG(bp))
601f5383399SBill Moore 			pipeline |= ZIO_GANG_STAGES;
602fa9e4066Sahrens 	}
603e14bb325SJeff Bonwick 
604e14bb325SJeff Bonwick 	zio->io_spa = spa;
605e14bb325SJeff Bonwick 	zio->io_txg = txg;
606fa9e4066Sahrens 	zio->io_done = done;
607fa9e4066Sahrens 	zio->io_private = private;
608fa9e4066Sahrens 	zio->io_type = type;
609fa9e4066Sahrens 	zio->io_priority = priority;
610e14bb325SJeff Bonwick 	zio->io_vd = vd;
611e14bb325SJeff Bonwick 	zio->io_offset = offset;
612770499e1SDan Kimmel 	zio->io_orig_abd = zio->io_abd = data;
6135602294fSDan Kimmel 	zio->io_orig_size = zio->io_size = psize;
6145602294fSDan Kimmel 	zio->io_lsize = lsize;
615e14bb325SJeff Bonwick 	zio->io_orig_flags = zio->io_flags = flags;
616e14bb325SJeff Bonwick 	zio->io_orig_stage = zio->io_stage = stage;
617e14bb325SJeff Bonwick 	zio->io_orig_pipeline = zio->io_pipeline = pipeline;
6180f7643c7SGeorge Wilson 	zio->io_pipeline_trace = ZIO_STAGE_OPEN;
619fa9e4066Sahrens 
620a3f829aeSBill Moore 	zio->io_state[ZIO_WAIT_READY] = (stage >= ZIO_STAGE_READY);
621a3f829aeSBill Moore 	zio->io_state[ZIO_WAIT_DONE] = (stage >= ZIO_STAGE_DONE);
622a3f829aeSBill Moore 
623e14bb325SJeff Bonwick 	if (zb != NULL)
624e14bb325SJeff Bonwick 		zio->io_bookmark = *zb;
625e14bb325SJeff Bonwick 
626e14bb325SJeff Bonwick 	if (pio != NULL) {
627663207adSDon Brady 		if (zio->io_metaslab_class == NULL)
628663207adSDon Brady 			zio->io_metaslab_class = pio->io_metaslab_class;
629e14bb325SJeff Bonwick 		if (zio->io_logical == NULL)
630ea8dc4b6Seschrock 			zio->io_logical = pio->io_logical;
631f5383399SBill Moore 		if (zio->io_child_type == ZIO_CHILD_GANG)
632f5383399SBill Moore 			zio->io_gang_leader = pio->io_gang_leader;
633e14bb325SJeff Bonwick 		zio_add_child(pio, zio);
634fa9e4066Sahrens 	}
635fa9e4066Sahrens 
636fa9e4066Sahrens 	return (zio);
637fa9e4066Sahrens }
638fa9e4066Sahrens 
6390a4e9518Sgw static void
640e14bb325SJeff Bonwick zio_destroy(zio_t *zio)
6410a4e9518Sgw {
6428363e80aSGeorge Wilson 	metaslab_trace_fini(&zio->io_alloc_list);
643a3f829aeSBill Moore 	list_destroy(&zio->io_parent_list);
644a3f829aeSBill Moore 	list_destroy(&zio->io_child_list);
645e14bb325SJeff Bonwick 	mutex_destroy(&zio->io_lock);
646e14bb325SJeff Bonwick 	cv_destroy(&zio->io_cv);
647e14bb325SJeff Bonwick 	kmem_cache_free(zio_cache, zio);
6480a4e9518Sgw }
6490a4e9518Sgw 
650fa9e4066Sahrens zio_t *
651a3f829aeSBill Moore zio_null(zio_t *pio, spa_t *spa, vdev_t *vd, zio_done_func_t *done,
652b24ab676SJeff Bonwick     void *private, enum zio_flag flags)
653fa9e4066Sahrens {
654fa9e4066Sahrens 	zio_t *zio;
655fa9e4066Sahrens 
6565602294fSDan Kimmel 	zio = zio_create(pio, spa, 0, NULL, NULL, 0, 0, done, private,
657a3f829aeSBill Moore 	    ZIO_TYPE_NULL, ZIO_PRIORITY_NOW, flags, vd, 0, NULL,
658e14bb325SJeff Bonwick 	    ZIO_STAGE_OPEN, ZIO_INTERLOCK_PIPELINE);
659fa9e4066Sahrens 
660fa9e4066Sahrens 	return (zio);
661fa9e4066Sahrens }
662fa9e4066Sahrens 
663fa9e4066Sahrens zio_t *
664b24ab676SJeff Bonwick zio_root(spa_t *spa, zio_done_func_t *done, void *private, enum zio_flag flags)
665fa9e4066Sahrens {
666a3f829aeSBill Moore 	return (zio_null(NULL, spa, NULL, done, private, flags));
667fa9e4066Sahrens }
668fa9e4066Sahrens 
669f63ab3d5SMatthew Ahrens void
670f63ab3d5SMatthew Ahrens zfs_blkptr_verify(spa_t *spa, const blkptr_t *bp)
671f63ab3d5SMatthew Ahrens {
672f63ab3d5SMatthew Ahrens 	if (!DMU_OT_IS_VALID(BP_GET_TYPE(bp))) {
673f63ab3d5SMatthew Ahrens 		zfs_panic_recover("blkptr at %p has invalid TYPE %llu",
674f63ab3d5SMatthew Ahrens 		    bp, (longlong_t)BP_GET_TYPE(bp));
675f63ab3d5SMatthew Ahrens 	}
676f63ab3d5SMatthew Ahrens 	if (BP_GET_CHECKSUM(bp) >= ZIO_CHECKSUM_FUNCTIONS ||
677f63ab3d5SMatthew Ahrens 	    BP_GET_CHECKSUM(bp) <= ZIO_CHECKSUM_ON) {
678f63ab3d5SMatthew Ahrens 		zfs_panic_recover("blkptr at %p has invalid CHECKSUM %llu",
679f63ab3d5SMatthew Ahrens 		    bp, (longlong_t)BP_GET_CHECKSUM(bp));
680f63ab3d5SMatthew Ahrens 	}
681f63ab3d5SMatthew Ahrens 	if (BP_GET_COMPRESS(bp) >= ZIO_COMPRESS_FUNCTIONS ||
682f63ab3d5SMatthew Ahrens 	    BP_GET_COMPRESS(bp) <= ZIO_COMPRESS_ON) {
683f63ab3d5SMatthew Ahrens 		zfs_panic_recover("blkptr at %p has invalid COMPRESS %llu",
684f63ab3d5SMatthew Ahrens 		    bp, (longlong_t)BP_GET_COMPRESS(bp));
685f63ab3d5SMatthew Ahrens 	}
686f63ab3d5SMatthew Ahrens 	if (BP_GET_LSIZE(bp) > SPA_MAXBLOCKSIZE) {
687f63ab3d5SMatthew Ahrens 		zfs_panic_recover("blkptr at %p has invalid LSIZE %llu",
688f63ab3d5SMatthew Ahrens 		    bp, (longlong_t)BP_GET_LSIZE(bp));
689f63ab3d5SMatthew Ahrens 	}
690f63ab3d5SMatthew Ahrens 	if (BP_GET_PSIZE(bp) > SPA_MAXBLOCKSIZE) {
691f63ab3d5SMatthew Ahrens 		zfs_panic_recover("blkptr at %p has invalid PSIZE %llu",
692f63ab3d5SMatthew Ahrens 		    bp, (longlong_t)BP_GET_PSIZE(bp));
693f63ab3d5SMatthew Ahrens 	}
694f63ab3d5SMatthew Ahrens 
695f63ab3d5SMatthew Ahrens 	if (BP_IS_EMBEDDED(bp)) {
696f63ab3d5SMatthew Ahrens 		if (BPE_GET_ETYPE(bp) > NUM_BP_EMBEDDED_TYPES) {
697f63ab3d5SMatthew Ahrens 			zfs_panic_recover("blkptr at %p has invalid ETYPE %llu",
698f63ab3d5SMatthew Ahrens 			    bp, (longlong_t)BPE_GET_ETYPE(bp));
699f63ab3d5SMatthew Ahrens 		}
700f63ab3d5SMatthew Ahrens 	}
701f63ab3d5SMatthew Ahrens 
7026f793812SPavel Zakharov 	/*
7036f793812SPavel Zakharov 	 * Do not verify individual DVAs if the config is not trusted. This
7046f793812SPavel Zakharov 	 * will be done once the zio is executed in vdev_mirror_map_alloc.
7056f793812SPavel Zakharov 	 */
7066f793812SPavel Zakharov 	if (!spa->spa_trust_config)
7076f793812SPavel Zakharov 		return;
7086f793812SPavel Zakharov 
709f63ab3d5SMatthew Ahrens 	/*
710f63ab3d5SMatthew Ahrens 	 * Pool-specific checks.
711f63ab3d5SMatthew Ahrens 	 *
712f63ab3d5SMatthew Ahrens 	 * Note: it would be nice to verify that the blk_birth and
713f63ab3d5SMatthew Ahrens 	 * BP_PHYSICAL_BIRTH() are not too large.  However, spa_freeze()
714f63ab3d5SMatthew Ahrens 	 * allows the birth time of log blocks (and dmu_sync()-ed blocks
715f63ab3d5SMatthew Ahrens 	 * that are in the log) to be arbitrarily large.
716f63ab3d5SMatthew Ahrens 	 */
717f63ab3d5SMatthew Ahrens 	for (int i = 0; i < BP_GET_NDVAS(bp); i++) {
718f63ab3d5SMatthew Ahrens 		uint64_t vdevid = DVA_GET_VDEV(&bp->blk_dva[i]);
719f63ab3d5SMatthew Ahrens 		if (vdevid >= spa->spa_root_vdev->vdev_children) {
720f63ab3d5SMatthew Ahrens 			zfs_panic_recover("blkptr at %p DVA %u has invalid "
721f63ab3d5SMatthew Ahrens 			    "VDEV %llu",
722f63ab3d5SMatthew Ahrens 			    bp, i, (longlong_t)vdevid);
7235897eb49SJustin Gibbs 			continue;
724f63ab3d5SMatthew Ahrens 		}
725f63ab3d5SMatthew Ahrens 		vdev_t *vd = spa->spa_root_vdev->vdev_child[vdevid];
726f63ab3d5SMatthew Ahrens 		if (vd == NULL) {
727f63ab3d5SMatthew Ahrens 			zfs_panic_recover("blkptr at %p DVA %u has invalid "
728f63ab3d5SMatthew Ahrens 			    "VDEV %llu",
729f63ab3d5SMatthew Ahrens 			    bp, i, (longlong_t)vdevid);
7305897eb49SJustin Gibbs 			continue;
731f63ab3d5SMatthew Ahrens 		}
732f63ab3d5SMatthew Ahrens 		if (vd->vdev_ops == &vdev_hole_ops) {
733f63ab3d5SMatthew Ahrens 			zfs_panic_recover("blkptr at %p DVA %u has hole "
734f63ab3d5SMatthew Ahrens 			    "VDEV %llu",
735f63ab3d5SMatthew Ahrens 			    bp, i, (longlong_t)vdevid);
7365897eb49SJustin Gibbs 			continue;
737f63ab3d5SMatthew Ahrens 		}
738f63ab3d5SMatthew Ahrens 		if (vd->vdev_ops == &vdev_missing_ops) {
739f63ab3d5SMatthew Ahrens 			/*
740f63ab3d5SMatthew Ahrens 			 * "missing" vdevs are valid during import, but we
741f63ab3d5SMatthew Ahrens 			 * don't have their detailed info (e.g. asize), so
742f63ab3d5SMatthew Ahrens 			 * we can't perform any more checks on them.
743f63ab3d5SMatthew Ahrens 			 */
744f63ab3d5SMatthew Ahrens 			continue;
745f63ab3d5SMatthew Ahrens 		}
746f63ab3d5SMatthew Ahrens 		uint64_t offset = DVA_GET_OFFSET(&bp->blk_dva[i]);
747f63ab3d5SMatthew Ahrens 		uint64_t asize = DVA_GET_ASIZE(&bp->blk_dva[i]);
748f63ab3d5SMatthew Ahrens 		if (BP_IS_GANG(bp))
749f63ab3d5SMatthew Ahrens 			asize = vdev_psize_to_asize(vd, SPA_GANGBLOCKSIZE);
750f63ab3d5SMatthew Ahrens 		if (offset + asize > vd->vdev_asize) {
751f63ab3d5SMatthew Ahrens 			zfs_panic_recover("blkptr at %p DVA %u has invalid "
752f63ab3d5SMatthew Ahrens 			    "OFFSET %llu",
753f63ab3d5SMatthew Ahrens 			    bp, i, (longlong_t)offset);
754f63ab3d5SMatthew Ahrens 		}
755f63ab3d5SMatthew Ahrens 	}
756f63ab3d5SMatthew Ahrens }
757f63ab3d5SMatthew Ahrens 
7586f793812SPavel Zakharov boolean_t
7596f793812SPavel Zakharov zfs_dva_valid(spa_t *spa, const dva_t *dva, const blkptr_t *bp)
7606f793812SPavel Zakharov {
7616f793812SPavel Zakharov 	uint64_t vdevid = DVA_GET_VDEV(dva);
7626f793812SPavel Zakharov 
7636f793812SPavel Zakharov 	if (vdevid >= spa->spa_root_vdev->vdev_children)
7646f793812SPavel Zakharov 		return (B_FALSE);
7656f793812SPavel Zakharov 
7666f793812SPavel Zakharov 	vdev_t *vd = spa->spa_root_vdev->vdev_child[vdevid];
7676f793812SPavel Zakharov 	if (vd == NULL)
7686f793812SPavel Zakharov 		return (B_FALSE);
7696f793812SPavel Zakharov 
7706f793812SPavel Zakharov 	if (vd->vdev_ops == &vdev_hole_ops)
7716f793812SPavel Zakharov 		return (B_FALSE);
7726f793812SPavel Zakharov 
7736f793812SPavel Zakharov 	if (vd->vdev_ops == &vdev_missing_ops) {
7746f793812SPavel Zakharov 		return (B_FALSE);
7756f793812SPavel Zakharov 	}
7766f793812SPavel Zakharov 
7776f793812SPavel Zakharov 	uint64_t offset = DVA_GET_OFFSET(dva);
7786f793812SPavel Zakharov 	uint64_t asize = DVA_GET_ASIZE(dva);
7796f793812SPavel Zakharov 
7806f793812SPavel Zakharov 	if (BP_IS_GANG(bp))
7816f793812SPavel Zakharov 		asize = vdev_psize_to_asize(vd, SPA_GANGBLOCKSIZE);
7826f793812SPavel Zakharov 	if (offset + asize > vd->vdev_asize)
7836f793812SPavel Zakharov 		return (B_FALSE);
7846f793812SPavel Zakharov 
7856f793812SPavel Zakharov 	return (B_TRUE);
7866f793812SPavel Zakharov }
7876f793812SPavel Zakharov 
788fa9e4066Sahrens zio_t *
789e14bb325SJeff Bonwick zio_read(zio_t *pio, spa_t *spa, const blkptr_t *bp,
790770499e1SDan Kimmel     abd_t *data, uint64_t size, zio_done_func_t *done, void *private,
7917802d7bfSMatthew Ahrens     zio_priority_t priority, enum zio_flag flags, const zbookmark_phys_t *zb)
792fa9e4066Sahrens {
793fa9e4066Sahrens 	zio_t *zio;
794fa9e4066Sahrens 
795f63ab3d5SMatthew Ahrens 	zfs_blkptr_verify(spa, bp);
796f63ab3d5SMatthew Ahrens 
797b24ab676SJeff Bonwick 	zio = zio_create(pio, spa, BP_PHYSICAL_BIRTH(bp), bp,
7985602294fSDan Kimmel 	    data, size, size, done, private,
799e14bb325SJeff Bonwick 	    ZIO_TYPE_READ, priority, flags, NULL, 0, zb,
800b24ab676SJeff Bonwick 	    ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ?
801b24ab676SJeff Bonwick 	    ZIO_DDT_CHILD_READ_PIPELINE : ZIO_READ_PIPELINE);
802fa9e4066Sahrens 
803fa9e4066Sahrens 	return (zio);
804fa9e4066Sahrens }
805fa9e4066Sahrens 
806fa9e4066Sahrens zio_t *
807e14bb325SJeff Bonwick zio_write(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
808770499e1SDan Kimmel     abd_t *data, uint64_t lsize, uint64_t psize, const zio_prop_t *zp,
8098df0bcf0SPaul Dagnelie     zio_done_func_t *ready, zio_done_func_t *children_ready,
8108df0bcf0SPaul Dagnelie     zio_done_func_t *physdone, zio_done_func_t *done,
8118df0bcf0SPaul Dagnelie     void *private, zio_priority_t priority, enum zio_flag flags,
8128df0bcf0SPaul Dagnelie     const zbookmark_phys_t *zb)
813fa9e4066Sahrens {
814fa9e4066Sahrens 	zio_t *zio;
815fa9e4066Sahrens 
816e14bb325SJeff Bonwick 	ASSERT(zp->zp_checksum >= ZIO_CHECKSUM_OFF &&
817e14bb325SJeff Bonwick 	    zp->zp_checksum < ZIO_CHECKSUM_FUNCTIONS &&
818e14bb325SJeff Bonwick 	    zp->zp_compress >= ZIO_COMPRESS_OFF &&
819e14bb325SJeff Bonwick 	    zp->zp_compress < ZIO_COMPRESS_FUNCTIONS &&
820ad135b5dSChristopher Siden 	    DMU_OT_IS_VALID(zp->zp_type) &&
821e14bb325SJeff Bonwick 	    zp->zp_level < 32 &&
822b24ab676SJeff Bonwick 	    zp->zp_copies > 0 &&
82380901aeaSGeorge Wilson 	    zp->zp_copies <= spa_max_replication(spa));
8240a4e9518Sgw 
8255602294fSDan Kimmel 	zio = zio_create(pio, spa, txg, bp, data, lsize, psize, done, private,
826e14bb325SJeff Bonwick 	    ZIO_TYPE_WRITE, priority, flags, NULL, 0, zb,
827b24ab676SJeff Bonwick 	    ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ?
828b24ab676SJeff Bonwick 	    ZIO_DDT_CHILD_WRITE_PIPELINE : ZIO_WRITE_PIPELINE);
829fa9e4066Sahrens 
830c717a561Smaybee 	zio->io_ready = ready;
8318df0bcf0SPaul Dagnelie 	zio->io_children_ready = children_ready;
83269962b56SMatthew Ahrens 	zio->io_physdone = physdone;
833e14bb325SJeff Bonwick 	zio->io_prop = *zp;
834fa9e4066Sahrens 
8355d7b4d43SMatthew Ahrens 	/*
8365d7b4d43SMatthew Ahrens 	 * Data can be NULL if we are going to call zio_write_override() to
8375d7b4d43SMatthew Ahrens 	 * provide the already-allocated BP.  But we may need the data to
8385d7b4d43SMatthew Ahrens 	 * verify a dedup hit (if requested).  In this case, don't try to
8395d7b4d43SMatthew Ahrens 	 * dedup (just take the already-allocated BP verbatim).
8405d7b4d43SMatthew Ahrens 	 */
8415d7b4d43SMatthew Ahrens 	if (data == NULL && zio->io_prop.zp_dedup_verify) {
8425d7b4d43SMatthew Ahrens 		zio->io_prop.zp_dedup = zio->io_prop.zp_dedup_verify = B_FALSE;
8435d7b4d43SMatthew Ahrens 	}
8445d7b4d43SMatthew Ahrens 
845fa9e4066Sahrens 	return (zio);
846fa9e4066Sahrens }
847fa9e4066Sahrens 
848fa9e4066Sahrens zio_t *
849770499e1SDan Kimmel zio_rewrite(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, abd_t *data,
85069962b56SMatthew Ahrens     uint64_t size, zio_done_func_t *done, void *private,
8517802d7bfSMatthew Ahrens     zio_priority_t priority, enum zio_flag flags, zbookmark_phys_t *zb)
852fa9e4066Sahrens {
853fa9e4066Sahrens 	zio_t *zio;
854fa9e4066Sahrens 
8555602294fSDan Kimmel 	zio = zio_create(pio, spa, txg, bp, data, size, size, done, private,
8560f7643c7SGeorge Wilson 	    ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_IO_REWRITE, NULL, 0, zb,
857e14bb325SJeff Bonwick 	    ZIO_STAGE_OPEN, ZIO_REWRITE_PIPELINE);
858fa9e4066Sahrens 
859fa9e4066Sahrens 	return (zio);
860fa9e4066Sahrens }
861fa9e4066Sahrens 
862b24ab676SJeff Bonwick void
86380901aeaSGeorge Wilson zio_write_override(zio_t *zio, blkptr_t *bp, int copies, boolean_t nopwrite)
864b24ab676SJeff Bonwick {
865b24ab676SJeff Bonwick 	ASSERT(zio->io_type == ZIO_TYPE_WRITE);
866b24ab676SJeff Bonwick 	ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
867b24ab676SJeff Bonwick 	ASSERT(zio->io_stage == ZIO_STAGE_OPEN);
868b24ab676SJeff Bonwick 	ASSERT(zio->io_txg == spa_syncing_txg(zio->io_spa));
869b24ab676SJeff Bonwick 
87080901aeaSGeorge Wilson 	/*
87180901aeaSGeorge Wilson 	 * We must reset the io_prop to match the values that existed
87280901aeaSGeorge Wilson 	 * when the bp was first written by dmu_sync() keeping in mind
87380901aeaSGeorge Wilson 	 * that nopwrite and dedup are mutually exclusive.
87480901aeaSGeorge Wilson 	 */
87580901aeaSGeorge Wilson 	zio->io_prop.zp_dedup = nopwrite ? B_FALSE : zio->io_prop.zp_dedup;
87680901aeaSGeorge Wilson 	zio->io_prop.zp_nopwrite = nopwrite;
877b24ab676SJeff Bonwick 	zio->io_prop.zp_copies = copies;
878b24ab676SJeff Bonwick 	zio->io_bp_override = bp;
879b24ab676SJeff Bonwick }
880b24ab676SJeff Bonwick 
881b24ab676SJeff Bonwick void
882b24ab676SJeff Bonwick zio_free(spa_t *spa, uint64_t txg, const blkptr_t *bp)
883b24ab676SJeff Bonwick {
8845d7b4d43SMatthew Ahrens 
8855cabbc6bSPrashanth Sreenivasa 	zfs_blkptr_verify(spa, bp);
8865cabbc6bSPrashanth Sreenivasa 
8875d7b4d43SMatthew Ahrens 	/*
8885d7b4d43SMatthew Ahrens 	 * The check for EMBEDDED is a performance optimization.  We
8895d7b4d43SMatthew Ahrens 	 * process the free here (by ignoring it) rather than
8905d7b4d43SMatthew Ahrens 	 * putting it on the list and then processing it in zio_free_sync().
8915d7b4d43SMatthew Ahrens 	 */
8925d7b4d43SMatthew Ahrens 	if (BP_IS_EMBEDDED(bp))
8935d7b4d43SMatthew Ahrens 		return;
8943b2aab18SMatthew Ahrens 	metaslab_check_free(spa, bp);
8959cb154a3SMatthew Ahrens 
8969cb154a3SMatthew Ahrens 	/*
8979cb154a3SMatthew Ahrens 	 * Frees that are for the currently-syncing txg, are not going to be
8989cb154a3SMatthew Ahrens 	 * deferred, and which will not need to do a read (i.e. not GANG or
8999cb154a3SMatthew Ahrens 	 * DEDUP), can be processed immediately.  Otherwise, put them on the
9009cb154a3SMatthew Ahrens 	 * in-memory list for later processing.
9019cb154a3SMatthew Ahrens 	 */
9029cb154a3SMatthew Ahrens 	if (BP_IS_GANG(bp) || BP_GET_DEDUP(bp) ||
9039cb154a3SMatthew Ahrens 	    txg != spa->spa_syncing_txg ||
9049cb154a3SMatthew Ahrens 	    spa_sync_pass(spa) >= zfs_sync_pass_deferred_free) {
9059cb154a3SMatthew Ahrens 		bplist_append(&spa->spa_free_bplist[txg & TXG_MASK], bp);
9069cb154a3SMatthew Ahrens 	} else {
9079cb154a3SMatthew Ahrens 		VERIFY0(zio_wait(zio_free_sync(NULL, spa, txg, bp, 0)));
9089cb154a3SMatthew Ahrens 	}
909b24ab676SJeff Bonwick }
910b24ab676SJeff Bonwick 
911fa9e4066Sahrens zio_t *
912b24ab676SJeff Bonwick zio_free_sync(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
913b24ab676SJeff Bonwick     enum zio_flag flags)
914fa9e4066Sahrens {
915fa9e4066Sahrens 	zio_t *zio;
9169cb154a3SMatthew Ahrens 	enum zio_stage stage = ZIO_FREE_PIPELINE;
917fa9e4066Sahrens 
918fa9e4066Sahrens 	ASSERT(!BP_IS_HOLE(bp));
919b24ab676SJeff Bonwick 	ASSERT(spa_syncing_txg(spa) == txg);
92001f55e48SGeorge Wilson 	ASSERT(spa_sync_pass(spa) < zfs_sync_pass_deferred_free);
921fa9e4066Sahrens 
9225d7b4d43SMatthew Ahrens 	if (BP_IS_EMBEDDED(bp))
9235d7b4d43SMatthew Ahrens 		return (zio_null(pio, spa, NULL, NULL, NULL, 0));
9245d7b4d43SMatthew Ahrens 
9253b2aab18SMatthew Ahrens 	metaslab_check_free(spa, bp);
9266e6d5868SMatthew Ahrens 	arc_freed(spa, bp);
927*a3874b8bSToomas Soome 	dsl_scan_freed(spa, bp);
9283b2aab18SMatthew Ahrens 
9299cb154a3SMatthew Ahrens 	/*
9309cb154a3SMatthew Ahrens 	 * GANG and DEDUP blocks can induce a read (for the gang block header,
9319cb154a3SMatthew Ahrens 	 * or the DDT), so issue them asynchronously so that this thread is
9329cb154a3SMatthew Ahrens 	 * not tied up.
9339cb154a3SMatthew Ahrens 	 */
9349cb154a3SMatthew Ahrens 	if (BP_IS_GANG(bp) || BP_GET_DEDUP(bp))
9359cb154a3SMatthew Ahrens 		stage |= ZIO_STAGE_ISSUE_ASYNC;
9369cb154a3SMatthew Ahrens 
937e14bb325SJeff Bonwick 	zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp),
9385602294fSDan Kimmel 	    BP_GET_PSIZE(bp), NULL, NULL, ZIO_TYPE_FREE, ZIO_PRIORITY_NOW,
9395602294fSDan Kimmel 	    flags, NULL, 0, NULL, ZIO_STAGE_OPEN, stage);
9409cb154a3SMatthew Ahrens 
941fa9e4066Sahrens 	return (zio);
942fa9e4066Sahrens }
943fa9e4066Sahrens 
944fa9e4066Sahrens zio_t *
945b24ab676SJeff Bonwick zio_claim(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
946b24ab676SJeff Bonwick     zio_done_func_t *done, void *private, enum zio_flag flags)
947fa9e4066Sahrens {
948fa9e4066Sahrens 	zio_t *zio;
949fa9e4066Sahrens 
9505cabbc6bSPrashanth Sreenivasa 	zfs_blkptr_verify(spa, bp);
9515d7b4d43SMatthew Ahrens 
9525d7b4d43SMatthew Ahrens 	if (BP_IS_EMBEDDED(bp))
9535d7b4d43SMatthew Ahrens 		return (zio_null(pio, spa, NULL, NULL, NULL, 0));
9545d7b4d43SMatthew Ahrens 
955fa9e4066Sahrens 	/*
956fa9e4066Sahrens 	 * A claim is an allocation of a specific block.  Claims are needed
957fa9e4066Sahrens 	 * to support immediate writes in the intent log.  The issue is that
958fa9e4066Sahrens 	 * immediate writes contain committed data, but in a txg that was
959fa9e4066Sahrens 	 * *not* committed.  Upon opening the pool after an unclean shutdown,
960fa9e4066Sahrens 	 * the intent log claims all blocks that contain immediate write data
961fa9e4066Sahrens 	 * so that the SPA knows they're in use.
962fa9e4066Sahrens 	 *
963fa9e4066Sahrens 	 * All claims *must* be resolved in the first txg -- before the SPA
964fa9e4066Sahrens 	 * starts allocating blocks -- so that nothing is allocated twice.
965b24ab676SJeff Bonwick 	 * If txg == 0 we just verify that the block is claimable.
966fa9e4066Sahrens 	 */
96786714001SSerapheim Dimitropoulos 	ASSERT3U(spa->spa_uberblock.ub_rootbp.blk_birth, <,
96886714001SSerapheim Dimitropoulos 	    spa_min_claim_txg(spa));
96986714001SSerapheim Dimitropoulos 	ASSERT(txg == spa_min_claim_txg(spa) || txg == 0);
970b24ab676SJeff Bonwick 	ASSERT(!BP_GET_DEDUP(bp) || !spa_writeable(spa));	/* zdb(1M) */
971fa9e4066Sahrens 
972e14bb325SJeff Bonwick 	zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp),
9735602294fSDan Kimmel 	    BP_GET_PSIZE(bp), done, private, ZIO_TYPE_CLAIM, ZIO_PRIORITY_NOW,
9745602294fSDan Kimmel 	    flags, NULL, 0, NULL, ZIO_STAGE_OPEN, ZIO_CLAIM_PIPELINE);
9750f7643c7SGeorge Wilson 	ASSERT0(zio->io_queued_timestamp);
976fa9e4066Sahrens 
977fa9e4066Sahrens 	return (zio);
978fa9e4066Sahrens }
979fa9e4066Sahrens 
980fa9e4066Sahrens zio_t *
981fa9e4066Sahrens zio_ioctl(zio_t *pio, spa_t *spa, vdev_t *vd, int cmd,
98269962b56SMatthew Ahrens     zio_done_func_t *done, void *private, enum zio_flag flags)
983fa9e4066Sahrens {
984fa9e4066Sahrens 	zio_t *zio;
985fa9e4066Sahrens 	int c;
986fa9e4066Sahrens 
987fa9e4066Sahrens 	if (vd->vdev_children == 0) {
9885602294fSDan Kimmel 		zio = zio_create(pio, spa, 0, NULL, NULL, 0, 0, done, private,
98969962b56SMatthew Ahrens 		    ZIO_TYPE_IOCTL, ZIO_PRIORITY_NOW, flags, vd, 0, NULL,
990fa9e4066Sahrens 		    ZIO_STAGE_OPEN, ZIO_IOCTL_PIPELINE);
991fa9e4066Sahrens 
992fa9e4066Sahrens 		zio->io_cmd = cmd;
993fa9e4066Sahrens 	} else {
994a3f829aeSBill Moore 		zio = zio_null(pio, spa, NULL, NULL, NULL, flags);
995fa9e4066Sahrens 
996fa9e4066Sahrens 		for (c = 0; c < vd->vdev_children; c++)
997fa9e4066Sahrens 			zio_nowait(zio_ioctl(zio, spa, vd->vdev_child[c], cmd,
99869962b56SMatthew Ahrens 			    done, private, flags));
999fa9e4066Sahrens 	}
1000fa9e4066Sahrens 
1001fa9e4066Sahrens 	return (zio);
1002fa9e4066Sahrens }
1003fa9e4066Sahrens 
1004fa9e4066Sahrens zio_t *
1005fa9e4066Sahrens zio_read_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
1006770499e1SDan Kimmel     abd_t *data, int checksum, zio_done_func_t *done, void *private,
100769962b56SMatthew Ahrens     zio_priority_t priority, enum zio_flag flags, boolean_t labels)
1008fa9e4066Sahrens {
1009fa9e4066Sahrens 	zio_t *zio;
10100a4e9518Sgw 
1011e14bb325SJeff Bonwick 	ASSERT(vd->vdev_children == 0);
1012e14bb325SJeff Bonwick 	ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE ||
1013e14bb325SJeff Bonwick 	    offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
1014e14bb325SJeff Bonwick 	ASSERT3U(offset + size, <=, vd->vdev_psize);
1015fa9e4066Sahrens 
10165602294fSDan Kimmel 	zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, size, done,
10175602294fSDan Kimmel 	    private, ZIO_TYPE_READ, priority, flags | ZIO_FLAG_PHYSICAL, vd,
10185602294fSDan Kimmel 	    offset, NULL, ZIO_STAGE_OPEN, ZIO_READ_PHYS_PIPELINE);
1019fa9e4066Sahrens 
1020e14bb325SJeff Bonwick 	zio->io_prop.zp_checksum = checksum;
1021fa9e4066Sahrens 
1022fa9e4066Sahrens 	return (zio);
1023fa9e4066Sahrens }
1024fa9e4066Sahrens 
1025fa9e4066Sahrens zio_t *
1026fa9e4066Sahrens zio_write_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
1027770499e1SDan Kimmel     abd_t *data, int checksum, zio_done_func_t *done, void *private,
102869962b56SMatthew Ahrens     zio_priority_t priority, enum zio_flag flags, boolean_t labels)
1029fa9e4066Sahrens {
1030fa9e4066Sahrens 	zio_t *zio;
10310a4e9518Sgw 
1032e14bb325SJeff Bonwick 	ASSERT(vd->vdev_children == 0);
1033e14bb325SJeff Bonwick 	ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE ||
1034e14bb325SJeff Bonwick 	    offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
1035e14bb325SJeff Bonwick 	ASSERT3U(offset + size, <=, vd->vdev_psize);
1036fa9e4066Sahrens 
10375602294fSDan Kimmel 	zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, size, done,
10385602294fSDan Kimmel 	    private, ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_PHYSICAL, vd,
10395602294fSDan Kimmel 	    offset, NULL, ZIO_STAGE_OPEN, ZIO_WRITE_PHYS_PIPELINE);
1040fa9e4066Sahrens 
1041e14bb325SJeff Bonwick 	zio->io_prop.zp_checksum = checksum;
1042fa9e4066Sahrens 
104345818ee1SMatthew Ahrens 	if (zio_checksum_table[checksum].ci_flags & ZCHECKSUM_FLAG_EMBEDDED) {
1044fa9e4066Sahrens 		/*
10456e1f5caaSNeil Perrin 		 * zec checksums are necessarily destructive -- they modify
1046e14bb325SJeff Bonwick 		 * the end of the write buffer to hold the verifier/checksum.
1047fa9e4066Sahrens 		 * Therefore, we must make a local copy in case the data is
1048e14bb325SJeff Bonwick 		 * being written to multiple places in parallel.
1049fa9e4066Sahrens 		 */
1050770499e1SDan Kimmel 		abd_t *wbuf = abd_alloc_sametype(data, size);
1051770499e1SDan Kimmel 		abd_copy(wbuf, data, size);
1052770499e1SDan Kimmel 
1053e14bb325SJeff Bonwick 		zio_push_transform(zio, wbuf, size, size, NULL);
1054fa9e4066Sahrens 	}
1055fa9e4066Sahrens 
1056fa9e4066Sahrens 	return (zio);
1057fa9e4066Sahrens }
1058fa9e4066Sahrens 
1059fa9e4066Sahrens /*
1060e14bb325SJeff Bonwick  * Create a child I/O to do some work for us.
1061fa9e4066Sahrens  */
1062fa9e4066Sahrens zio_t *
1063e14bb325SJeff Bonwick zio_vdev_child_io(zio_t *pio, blkptr_t *bp, vdev_t *vd, uint64_t offset,
1064770499e1SDan Kimmel     abd_t *data, uint64_t size, int type, zio_priority_t priority,
1065dcbf3bd6SGeorge Wilson     enum zio_flag flags, zio_done_func_t *done, void *private)
1066fa9e4066Sahrens {
1067b24ab676SJeff Bonwick 	enum zio_stage pipeline = ZIO_VDEV_CHILD_PIPELINE;
1068e14bb325SJeff Bonwick 	zio_t *zio;
1069e14bb325SJeff Bonwick 
10705cabbc6bSPrashanth Sreenivasa 	/*
10715cabbc6bSPrashanth Sreenivasa 	 * vdev child I/Os do not propagate their error to the parent.
10725cabbc6bSPrashanth Sreenivasa 	 * Therefore, for correct operation the caller *must* check for
10735cabbc6bSPrashanth Sreenivasa 	 * and handle the error in the child i/o's done callback.
10745cabbc6bSPrashanth Sreenivasa 	 * The only exceptions are i/os that we don't care about
10755cabbc6bSPrashanth Sreenivasa 	 * (OPTIONAL or REPAIR).
10765cabbc6bSPrashanth Sreenivasa 	 */
10775cabbc6bSPrashanth Sreenivasa 	ASSERT((flags & ZIO_FLAG_OPTIONAL) || (flags & ZIO_FLAG_IO_REPAIR) ||
10785cabbc6bSPrashanth Sreenivasa 	    done != NULL);
10795cabbc6bSPrashanth Sreenivasa 
1080fa9e4066Sahrens 	if (type == ZIO_TYPE_READ && bp != NULL) {
1081fa9e4066Sahrens 		/*
1082fa9e4066Sahrens 		 * If we have the bp, then the child should perform the
1083fa9e4066Sahrens 		 * checksum and the parent need not.  This pushes error
1084fa9e4066Sahrens 		 * detection as close to the leaves as possible and
1085fa9e4066Sahrens 		 * eliminates redundant checksums in the interior nodes.
1086fa9e4066Sahrens 		 */
1087b24ab676SJeff Bonwick 		pipeline |= ZIO_STAGE_CHECKSUM_VERIFY;
1088b24ab676SJeff Bonwick 		pio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY;
1089fa9e4066Sahrens 	}
1090fa9e4066Sahrens 
10915cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_ops->vdev_op_leaf) {
10925cabbc6bSPrashanth Sreenivasa 		ASSERT0(vd->vdev_children);
1093e14bb325SJeff Bonwick 		offset += VDEV_LABEL_START_SIZE;
10945cabbc6bSPrashanth Sreenivasa 	}
1095e14bb325SJeff Bonwick 
10965cabbc6bSPrashanth Sreenivasa 	flags |= ZIO_VDEV_CHILD_FLAGS(pio);
1097b24ab676SJeff Bonwick 
1098b24ab676SJeff Bonwick 	/*
1099b24ab676SJeff Bonwick 	 * If we've decided to do a repair, the write is not speculative --
1100b24ab676SJeff Bonwick 	 * even if the original read was.
1101b24ab676SJeff Bonwick 	 */
1102b24ab676SJeff Bonwick 	if (flags & ZIO_FLAG_IO_REPAIR)
1103b24ab676SJeff Bonwick 		flags &= ~ZIO_FLAG_SPECULATIVE;
1104b24ab676SJeff Bonwick 
11050f7643c7SGeorge Wilson 	/*
11060f7643c7SGeorge Wilson 	 * If we're creating a child I/O that is not associated with a
11070f7643c7SGeorge Wilson 	 * top-level vdev, then the child zio is not an allocating I/O.
11080f7643c7SGeorge Wilson 	 * If this is a retried I/O then we ignore it since we will
11090f7643c7SGeorge Wilson 	 * have already processed the original allocating I/O.
11100f7643c7SGeorge Wilson 	 */
11110f7643c7SGeorge Wilson 	if (flags & ZIO_FLAG_IO_ALLOCATING &&
11120f7643c7SGeorge Wilson 	    (vd != vd->vdev_top || (flags & ZIO_FLAG_IO_RETRY))) {
1113663207adSDon Brady 		ASSERT(pio->io_metaslab_class != NULL);
1114663207adSDon Brady 		ASSERT(pio->io_metaslab_class->mc_alloc_throttle_enabled);
11150f7643c7SGeorge Wilson 		ASSERT(type == ZIO_TYPE_WRITE);
11160f7643c7SGeorge Wilson 		ASSERT(priority == ZIO_PRIORITY_ASYNC_WRITE);
11170f7643c7SGeorge Wilson 		ASSERT(!(flags & ZIO_FLAG_IO_REPAIR));
11180f7643c7SGeorge Wilson 		ASSERT(!(pio->io_flags & ZIO_FLAG_IO_REWRITE) ||
11190f7643c7SGeorge Wilson 		    pio->io_child_type == ZIO_CHILD_GANG);
11200f7643c7SGeorge Wilson 
11210f7643c7SGeorge Wilson 		flags &= ~ZIO_FLAG_IO_ALLOCATING;
11220f7643c7SGeorge Wilson 	}
11230f7643c7SGeorge Wilson 
11245602294fSDan Kimmel 	zio = zio_create(pio, pio->io_spa, pio->io_txg, bp, data, size, size,
1125b24ab676SJeff Bonwick 	    done, private, type, priority, flags, vd, offset, &pio->io_bookmark,
1126b24ab676SJeff Bonwick 	    ZIO_STAGE_VDEV_IO_START >> 1, pipeline);
11270f7643c7SGeorge Wilson 	ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_VDEV);
1128fa9e4066Sahrens 
112969962b56SMatthew Ahrens 	zio->io_physdone = pio->io_physdone;
113069962b56SMatthew Ahrens 	if (vd->vdev_ops->vdev_op_leaf && zio->io_logical != NULL)
113169962b56SMatthew Ahrens 		zio->io_logical->io_phys_children++;
113269962b56SMatthew Ahrens 
1133e14bb325SJeff Bonwick 	return (zio);
113432b87932Sek }
113532b87932Sek 
1136e14bb325SJeff Bonwick zio_t *
1137770499e1SDan Kimmel zio_vdev_delegated_io(vdev_t *vd, uint64_t offset, abd_t *data, uint64_t size,
11383a4b1be9SMatthew Ahrens     zio_type_t type, zio_priority_t priority, enum zio_flag flags,
11399a686fbcSPaul Dagnelie     zio_done_func_t *done, void *private)
1140fa9e4066Sahrens {
1141e14bb325SJeff Bonwick 	zio_t *zio;
1142fa9e4066Sahrens 
1143e14bb325SJeff Bonwick 	ASSERT(vd->vdev_ops->vdev_op_leaf);
1144fa9e4066Sahrens 
1145e14bb325SJeff Bonwick 	zio = zio_create(NULL, vd->vdev_spa, 0, NULL,
11465602294fSDan Kimmel 	    data, size, size, done, private, type, priority,
114769962b56SMatthew Ahrens 	    flags | ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY | ZIO_FLAG_DELEGATED,
1148e14bb325SJeff Bonwick 	    vd, offset, NULL,
1149b24ab676SJeff Bonwick 	    ZIO_STAGE_VDEV_IO_START >> 1, ZIO_VDEV_CHILD_PIPELINE);
1150fa9e4066Sahrens 
1151e14bb325SJeff Bonwick 	return (zio);
1152e05725b1Sbonwick }
1153e05725b1Sbonwick 
1154e05725b1Sbonwick void
1155e14bb325SJeff Bonwick zio_flush(zio_t *zio, vdev_t *vd)
1156e05725b1Sbonwick {
1157e14bb325SJeff Bonwick 	zio_nowait(zio_ioctl(zio, zio->io_spa, vd, DKIOCFLUSHWRITECACHE,
115869962b56SMatthew Ahrens 	    NULL, NULL,
1159e14bb325SJeff Bonwick 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY));
1160fa9e4066Sahrens }
1161fa9e4066Sahrens 
11626e1f5caaSNeil Perrin void
11636e1f5caaSNeil Perrin zio_shrink(zio_t *zio, uint64_t size)
11646e1f5caaSNeil Perrin {
11651271e4b1SPrakash Surya 	ASSERT3P(zio->io_executor, ==, NULL);
11661271e4b1SPrakash Surya 	ASSERT3P(zio->io_orig_size, ==, zio->io_size);
11671271e4b1SPrakash Surya 	ASSERT3U(size, <=, zio->io_size);
11686e1f5caaSNeil Perrin 
11696e1f5caaSNeil Perrin 	/*
11706e1f5caaSNeil Perrin 	 * We don't shrink for raidz because of problems with the
11716e1f5caaSNeil Perrin 	 * reconstruction when reading back less than the block size.
11726e1f5caaSNeil Perrin 	 * Note, BP_IS_RAIDZ() assumes no compression.
11736e1f5caaSNeil Perrin 	 */
11746e1f5caaSNeil Perrin 	ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF);
11755602294fSDan Kimmel 	if (!BP_IS_RAIDZ(zio->io_bp)) {
11765602294fSDan Kimmel 		/* we are not doing a raw write */
11775602294fSDan Kimmel 		ASSERT3U(zio->io_size, ==, zio->io_lsize);
11785602294fSDan Kimmel 		zio->io_orig_size = zio->io_size = zio->io_lsize = size;
11795602294fSDan Kimmel 	}
11806e1f5caaSNeil Perrin }
11816e1f5caaSNeil Perrin 
1182fa9e4066Sahrens /*
1183fa9e4066Sahrens  * ==========================================================================
1184e14bb325SJeff Bonwick  * Prepare to read and write logical blocks
1185fa9e4066Sahrens  * ==========================================================================
1186fa9e4066Sahrens  */
1187e14bb325SJeff Bonwick 
1188e05725b1Sbonwick static int
1189e14bb325SJeff Bonwick zio_read_bp_init(zio_t *zio)
1190fa9e4066Sahrens {
1191e14bb325SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
1192e05725b1Sbonwick 
11935cabbc6bSPrashanth Sreenivasa 	ASSERT3P(zio->io_bp, ==, &zio->io_bp_copy);
11945cabbc6bSPrashanth Sreenivasa 
119503361682SJeff Bonwick 	if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF &&
1196f5383399SBill Moore 	    zio->io_child_type == ZIO_CHILD_LOGICAL &&
1197f5383399SBill Moore 	    !(zio->io_flags & ZIO_FLAG_RAW)) {
11985d7b4d43SMatthew Ahrens 		uint64_t psize =
11995d7b4d43SMatthew Ahrens 		    BP_IS_EMBEDDED(bp) ? BPE_GET_PSIZE(bp) : BP_GET_PSIZE(bp);
1200770499e1SDan Kimmel 		zio_push_transform(zio, abd_alloc_sametype(zio->io_abd, psize),
1201770499e1SDan Kimmel 		    psize, psize, zio_decompress);
1202e14bb325SJeff Bonwick 	}
1203fa9e4066Sahrens 
12045d7b4d43SMatthew Ahrens 	if (BP_IS_EMBEDDED(bp) && BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA) {
12055d7b4d43SMatthew Ahrens 		zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1206770499e1SDan Kimmel 
1207770499e1SDan Kimmel 		int psize = BPE_GET_PSIZE(bp);
1208770499e1SDan Kimmel 		void *data = abd_borrow_buf(zio->io_abd, psize);
1209770499e1SDan Kimmel 		decode_embedded_bp_compressed(bp, data);
1210770499e1SDan Kimmel 		abd_return_buf_copy(zio->io_abd, data, psize);
12115d7b4d43SMatthew Ahrens 	} else {
12125d7b4d43SMatthew Ahrens 		ASSERT(!BP_IS_EMBEDDED(bp));
12135cabbc6bSPrashanth Sreenivasa 		ASSERT3P(zio->io_bp, ==, &zio->io_bp_copy);
12145d7b4d43SMatthew Ahrens 	}
12155d7b4d43SMatthew Ahrens 
1216ad135b5dSChristopher Siden 	if (!DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) && BP_GET_LEVEL(bp) == 0)
1217e14bb325SJeff Bonwick 		zio->io_flags |= ZIO_FLAG_DONT_CACHE;
1218fa9e4066Sahrens 
1219bbfd46c4SJeff Bonwick 	if (BP_GET_TYPE(bp) == DMU_OT_DDT_ZAP)
1220bbfd46c4SJeff Bonwick 		zio->io_flags |= ZIO_FLAG_DONT_CACHE;
1221bbfd46c4SJeff Bonwick 
1222b24ab676SJeff Bonwick 	if (BP_GET_DEDUP(bp) && zio->io_child_type == ZIO_CHILD_LOGICAL)
1223b24ab676SJeff Bonwick 		zio->io_pipeline = ZIO_DDT_READ_PIPELINE;
1224b24ab676SJeff Bonwick 
1225e14bb325SJeff Bonwick 	return (ZIO_PIPELINE_CONTINUE);
1226fa9e4066Sahrens }
1227fa9e4066Sahrens 
1228e05725b1Sbonwick static int
1229e14bb325SJeff Bonwick zio_write_bp_init(zio_t *zio)
12300a4e9518Sgw {
1231e14bb325SJeff Bonwick 	if (!IO_IS_ALLOCATING(zio))
1232e14bb325SJeff Bonwick 		return (ZIO_PIPELINE_CONTINUE);
12330a4e9518Sgw 
1234b24ab676SJeff Bonwick 	ASSERT(zio->io_child_type != ZIO_CHILD_DDT);
1235b24ab676SJeff Bonwick 
1236b24ab676SJeff Bonwick 	if (zio->io_bp_override) {
12370f7643c7SGeorge Wilson 		blkptr_t *bp = zio->io_bp;
12380f7643c7SGeorge Wilson 		zio_prop_t *zp = &zio->io_prop;
12390f7643c7SGeorge Wilson 
1240b24ab676SJeff Bonwick 		ASSERT(bp->blk_birth != zio->io_txg);
1241b24ab676SJeff Bonwick 		ASSERT(BP_GET_DEDUP(zio->io_bp_override) == 0);
1242b24ab676SJeff Bonwick 
1243b24ab676SJeff Bonwick 		*bp = *zio->io_bp_override;
1244b24ab676SJeff Bonwick 		zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1245b24ab676SJeff Bonwick 
12465d7b4d43SMatthew Ahrens 		if (BP_IS_EMBEDDED(bp))
12475d7b4d43SMatthew Ahrens 			return (ZIO_PIPELINE_CONTINUE);
12485d7b4d43SMatthew Ahrens 
124980901aeaSGeorge Wilson 		/*
125080901aeaSGeorge Wilson 		 * If we've been overridden and nopwrite is set then
125180901aeaSGeorge Wilson 		 * set the flag accordingly to indicate that a nopwrite
125280901aeaSGeorge Wilson 		 * has already occurred.
125380901aeaSGeorge Wilson 		 */
125480901aeaSGeorge Wilson 		if (!BP_IS_HOLE(bp) && zp->zp_nopwrite) {
125580901aeaSGeorge Wilson 			ASSERT(!zp->zp_dedup);
12560f7643c7SGeorge Wilson 			ASSERT3U(BP_GET_CHECKSUM(bp), ==, zp->zp_checksum);
125780901aeaSGeorge Wilson 			zio->io_flags |= ZIO_FLAG_NOPWRITE;
125880901aeaSGeorge Wilson 			return (ZIO_PIPELINE_CONTINUE);
125980901aeaSGeorge Wilson 		}
126080901aeaSGeorge Wilson 
126180901aeaSGeorge Wilson 		ASSERT(!zp->zp_nopwrite);
126280901aeaSGeorge Wilson 
1263b24ab676SJeff Bonwick 		if (BP_IS_HOLE(bp) || !zp->zp_dedup)
1264b24ab676SJeff Bonwick 			return (ZIO_PIPELINE_CONTINUE);
1265b24ab676SJeff Bonwick 
126645818ee1SMatthew Ahrens 		ASSERT((zio_checksum_table[zp->zp_checksum].ci_flags &
126745818ee1SMatthew Ahrens 		    ZCHECKSUM_FLAG_DEDUP) || zp->zp_dedup_verify);
1268b24ab676SJeff Bonwick 
1269b24ab676SJeff Bonwick 		if (BP_GET_CHECKSUM(bp) == zp->zp_checksum) {
1270b24ab676SJeff Bonwick 			BP_SET_DEDUP(bp, 1);
1271b24ab676SJeff Bonwick 			zio->io_pipeline |= ZIO_STAGE_DDT_WRITE;
1272b24ab676SJeff Bonwick 			return (ZIO_PIPELINE_CONTINUE);
1273b24ab676SJeff Bonwick 		}
12740f7643c7SGeorge Wilson 
12750f7643c7SGeorge Wilson 		/*
12760f7643c7SGeorge Wilson 		 * We were unable to handle this as an override bp, treat
12770f7643c7SGeorge Wilson 		 * it as a regular write I/O.
12780f7643c7SGeorge Wilson 		 */
1279b39b744bSMatthew Ahrens 		zio->io_bp_override = NULL;
12800f7643c7SGeorge Wilson 		*bp = zio->io_bp_orig;
12810f7643c7SGeorge Wilson 		zio->io_pipeline = zio->io_orig_pipeline;
1282b24ab676SJeff Bonwick 	}
12830a4e9518Sgw 
12840f7643c7SGeorge Wilson 	return (ZIO_PIPELINE_CONTINUE);
12850f7643c7SGeorge Wilson }
12860f7643c7SGeorge Wilson 
12870f7643c7SGeorge Wilson static int
12880f7643c7SGeorge Wilson zio_write_compress(zio_t *zio)
12890f7643c7SGeorge Wilson {
12900f7643c7SGeorge Wilson 	spa_t *spa = zio->io_spa;
12910f7643c7SGeorge Wilson 	zio_prop_t *zp = &zio->io_prop;
12920f7643c7SGeorge Wilson 	enum zio_compress compress = zp->zp_compress;
12930f7643c7SGeorge Wilson 	blkptr_t *bp = zio->io_bp;
12945602294fSDan Kimmel 	uint64_t lsize = zio->io_lsize;
12955602294fSDan Kimmel 	uint64_t psize = zio->io_size;
12960f7643c7SGeorge Wilson 	int pass = 1;
12970f7643c7SGeorge Wilson 
12985602294fSDan Kimmel 	EQUIV(lsize != psize, (zio->io_flags & ZIO_FLAG_RAW) != 0);
12995602294fSDan Kimmel 
13000f7643c7SGeorge Wilson 	/*
13010f7643c7SGeorge Wilson 	 * If our children haven't all reached the ready stage,
13020f7643c7SGeorge Wilson 	 * wait for them and then repeat this pipeline stage.
13030f7643c7SGeorge Wilson 	 */
1304d6e1c446SGeorge Wilson 	if (zio_wait_for_children(zio, ZIO_CHILD_LOGICAL_BIT |
1305d6e1c446SGeorge Wilson 	    ZIO_CHILD_GANG_BIT, ZIO_WAIT_READY)) {
13060f7643c7SGeorge Wilson 		return (ZIO_PIPELINE_STOP);
1307d6e1c446SGeorge Wilson 	}
13080f7643c7SGeorge Wilson 
13090f7643c7SGeorge Wilson 	if (!IO_IS_ALLOCATING(zio))
13100f7643c7SGeorge Wilson 		return (ZIO_PIPELINE_CONTINUE);
13110f7643c7SGeorge Wilson 
13120f7643c7SGeorge Wilson 	if (zio->io_children_ready != NULL) {
13130f7643c7SGeorge Wilson 		/*
13140f7643c7SGeorge Wilson 		 * Now that all our children are ready, run the callback
13150f7643c7SGeorge Wilson 		 * associated with this zio in case it wants to modify the
13160f7643c7SGeorge Wilson 		 * data to be written.
13170f7643c7SGeorge Wilson 		 */
13180f7643c7SGeorge Wilson 		ASSERT3U(zp->zp_level, >, 0);
13190f7643c7SGeorge Wilson 		zio->io_children_ready(zio);
13200f7643c7SGeorge Wilson 	}
13210f7643c7SGeorge Wilson 
13220f7643c7SGeorge Wilson 	ASSERT(zio->io_child_type != ZIO_CHILD_DDT);
13230f7643c7SGeorge Wilson 	ASSERT(zio->io_bp_override == NULL);
13240f7643c7SGeorge Wilson 
132543466aaeSMax Grossman 	if (!BP_IS_HOLE(bp) && bp->blk_birth == zio->io_txg) {
1326e14bb325SJeff Bonwick 		/*
1327e14bb325SJeff Bonwick 		 * We're rewriting an existing block, which means we're
1328e14bb325SJeff Bonwick 		 * working on behalf of spa_sync().  For spa_sync() to
1329e14bb325SJeff Bonwick 		 * converge, it must eventually be the case that we don't
1330e14bb325SJeff Bonwick 		 * have to allocate new blocks.  But compression changes
1331e14bb325SJeff Bonwick 		 * the blocksize, which forces a reallocate, and makes
1332e14bb325SJeff Bonwick 		 * convergence take longer.  Therefore, after the first
1333e14bb325SJeff Bonwick 		 * few passes, stop compressing to ensure convergence.
1334e14bb325SJeff Bonwick 		 */
1335b24ab676SJeff Bonwick 		pass = spa_sync_pass(spa);
1336b24ab676SJeff Bonwick 
1337b24ab676SJeff Bonwick 		ASSERT(zio->io_txg == spa_syncing_txg(spa));
1338b24ab676SJeff Bonwick 		ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1339b24ab676SJeff Bonwick 		ASSERT(!BP_GET_DEDUP(bp));
1340e05725b1Sbonwick 
134101f55e48SGeorge Wilson 		if (pass >= zfs_sync_pass_dont_compress)
1342e14bb325SJeff Bonwick 			compress = ZIO_COMPRESS_OFF;
1343e05725b1Sbonwick 
1344e14bb325SJeff Bonwick 		/* Make sure someone doesn't change their mind on overwrites */
13455d7b4d43SMatthew Ahrens 		ASSERT(BP_IS_EMBEDDED(bp) || MIN(zp->zp_copies + BP_IS_GANG(bp),
1346b24ab676SJeff Bonwick 		    spa_max_replication(spa)) == BP_GET_NDVAS(bp));
1347e14bb325SJeff Bonwick 	}
1348fa9e4066Sahrens 
13495602294fSDan Kimmel 	/* If it's a compressed write that is not raw, compress the buffer. */
13505602294fSDan Kimmel 	if (compress != ZIO_COMPRESS_OFF && psize == lsize) {
1351b24ab676SJeff Bonwick 		void *cbuf = zio_buf_alloc(lsize);
1352770499e1SDan Kimmel 		psize = zio_compress_data(compress, zio->io_abd, cbuf, lsize);
1353b24ab676SJeff Bonwick 		if (psize == 0 || psize == lsize) {
1354e14bb325SJeff Bonwick 			compress = ZIO_COMPRESS_OFF;
1355b24ab676SJeff Bonwick 			zio_buf_free(cbuf, lsize);
13565d7b4d43SMatthew Ahrens 		} else if (!zp->zp_dedup && psize <= BPE_PAYLOAD_SIZE &&
13575d7b4d43SMatthew Ahrens 		    zp->zp_level == 0 && !DMU_OT_HAS_FILL(zp->zp_type) &&
13585d7b4d43SMatthew Ahrens 		    spa_feature_is_enabled(spa, SPA_FEATURE_EMBEDDED_DATA)) {
13595d7b4d43SMatthew Ahrens 			encode_embedded_bp_compressed(bp,
13605d7b4d43SMatthew Ahrens 			    cbuf, compress, lsize, psize);
13615d7b4d43SMatthew Ahrens 			BPE_SET_ETYPE(bp, BP_EMBEDDED_TYPE_DATA);
13625d7b4d43SMatthew Ahrens 			BP_SET_TYPE(bp, zio->io_prop.zp_type);
13635d7b4d43SMatthew Ahrens 			BP_SET_LEVEL(bp, zio->io_prop.zp_level);
13645d7b4d43SMatthew Ahrens 			zio_buf_free(cbuf, lsize);
13655d7b4d43SMatthew Ahrens 			bp->blk_birth = zio->io_txg;
13665d7b4d43SMatthew Ahrens 			zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
13675d7b4d43SMatthew Ahrens 			ASSERT(spa_feature_is_active(spa,
13685d7b4d43SMatthew Ahrens 			    SPA_FEATURE_EMBEDDED_DATA));
13695d7b4d43SMatthew Ahrens 			return (ZIO_PIPELINE_CONTINUE);
1370b24ab676SJeff Bonwick 		} else {
13715d7b4d43SMatthew Ahrens 			/*
137281cd5c55SMatthew Ahrens 			 * Round up compressed size up to the ashift
137381cd5c55SMatthew Ahrens 			 * of the smallest-ashift device, and zero the tail.
137481cd5c55SMatthew Ahrens 			 * This ensures that the compressed size of the BP
137581cd5c55SMatthew Ahrens 			 * (and thus compressratio property) are correct,
137681cd5c55SMatthew Ahrens 			 * in that we charge for the padding used to fill out
137781cd5c55SMatthew Ahrens 			 * the last sector.
13785d7b4d43SMatthew Ahrens 			 */
137981cd5c55SMatthew Ahrens 			ASSERT3U(spa->spa_min_ashift, >=, SPA_MINBLOCKSHIFT);
138081cd5c55SMatthew Ahrens 			size_t rounded = (size_t)P2ROUNDUP(psize,
138181cd5c55SMatthew Ahrens 			    1ULL << spa->spa_min_ashift);
138281cd5c55SMatthew Ahrens 			if (rounded >= lsize) {
13835d7b4d43SMatthew Ahrens 				compress = ZIO_COMPRESS_OFF;
13845d7b4d43SMatthew Ahrens 				zio_buf_free(cbuf, lsize);
138581cd5c55SMatthew Ahrens 				psize = lsize;
13865d7b4d43SMatthew Ahrens 			} else {
1387770499e1SDan Kimmel 				abd_t *cdata = abd_get_from_buf(cbuf, lsize);
1388770499e1SDan Kimmel 				abd_take_ownership_of_buf(cdata, B_TRUE);
1389770499e1SDan Kimmel 				abd_zero_off(cdata, psize, rounded - psize);
139081cd5c55SMatthew Ahrens 				psize = rounded;
1391770499e1SDan Kimmel 				zio_push_transform(zio, cdata,
13925d7b4d43SMatthew Ahrens 				    psize, lsize, NULL);
13935d7b4d43SMatthew Ahrens 			}
1394e14bb325SJeff Bonwick 		}
13950f7643c7SGeorge Wilson 
13960f7643c7SGeorge Wilson 		/*
13970f7643c7SGeorge Wilson 		 * We were unable to handle this as an override bp, treat
13980f7643c7SGeorge Wilson 		 * it as a regular write I/O.
13990f7643c7SGeorge Wilson 		 */
14000f7643c7SGeorge Wilson 		zio->io_bp_override = NULL;
14010f7643c7SGeorge Wilson 		*bp = zio->io_bp_orig;
14020f7643c7SGeorge Wilson 		zio->io_pipeline = zio->io_orig_pipeline;
14035602294fSDan Kimmel 	} else {
14045602294fSDan Kimmel 		ASSERT3U(psize, !=, 0);
1405e14bb325SJeff Bonwick 	}
1406c717a561Smaybee 
1407e14bb325SJeff Bonwick 	/*
1408e14bb325SJeff Bonwick 	 * The final pass of spa_sync() must be all rewrites, but the first
1409e14bb325SJeff Bonwick 	 * few passes offer a trade-off: allocating blocks defers convergence,
1410e14bb325SJeff Bonwick 	 * but newly allocated blocks are sequential, so they can be written
1411e14bb325SJeff Bonwick 	 * to disk faster.  Therefore, we allow the first few passes of
1412e14bb325SJeff Bonwick 	 * spa_sync() to allocate new blocks, but force rewrites after that.
1413e14bb325SJeff Bonwick 	 * There should only be a handful of blocks after pass 1 in any case.
1414e14bb325SJeff Bonwick 	 */
141543466aaeSMax Grossman 	if (!BP_IS_HOLE(bp) && bp->blk_birth == zio->io_txg &&
141643466aaeSMax Grossman 	    BP_GET_PSIZE(bp) == psize &&
141701f55e48SGeorge Wilson 	    pass >= zfs_sync_pass_rewrite) {
1418663207adSDon Brady 		VERIFY3U(psize, !=, 0);
1419b24ab676SJeff Bonwick 		enum zio_stage gang_stages = zio->io_pipeline & ZIO_GANG_STAGES;
1420663207adSDon Brady 
1421e14bb325SJeff Bonwick 		zio->io_pipeline = ZIO_REWRITE_PIPELINE | gang_stages;
1422e14bb325SJeff Bonwick 		zio->io_flags |= ZIO_FLAG_IO_REWRITE;
1423e14bb325SJeff Bonwick 	} else {
1424e14bb325SJeff Bonwick 		BP_ZERO(bp);
1425e14bb325SJeff Bonwick 		zio->io_pipeline = ZIO_WRITE_PIPELINE;
1426e14bb325SJeff Bonwick 	}
1427fa9e4066Sahrens 
1428b24ab676SJeff Bonwick 	if (psize == 0) {
142943466aaeSMax Grossman 		if (zio->io_bp_orig.blk_birth != 0 &&
143043466aaeSMax Grossman 		    spa_feature_is_active(spa, SPA_FEATURE_HOLE_BIRTH)) {
143143466aaeSMax Grossman 			BP_SET_LSIZE(bp, lsize);
143243466aaeSMax Grossman 			BP_SET_TYPE(bp, zp->zp_type);
143343466aaeSMax Grossman 			BP_SET_LEVEL(bp, zp->zp_level);
143443466aaeSMax Grossman 			BP_SET_BIRTH(bp, zio->io_txg, 0);
143543466aaeSMax Grossman 		}
1436e14bb325SJeff Bonwick 		zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1437e14bb325SJeff Bonwick 	} else {
1438e14bb325SJeff Bonwick 		ASSERT(zp->zp_checksum != ZIO_CHECKSUM_GANG_HEADER);
1439e14bb325SJeff Bonwick 		BP_SET_LSIZE(bp, lsize);
144043466aaeSMax Grossman 		BP_SET_TYPE(bp, zp->zp_type);
144143466aaeSMax Grossman 		BP_SET_LEVEL(bp, zp->zp_level);
1442b24ab676SJeff Bonwick 		BP_SET_PSIZE(bp, psize);
1443e14bb325SJeff Bonwick 		BP_SET_COMPRESS(bp, compress);
1444e14bb325SJeff Bonwick 		BP_SET_CHECKSUM(bp, zp->zp_checksum);
1445b24ab676SJeff Bonwick 		BP_SET_DEDUP(bp, zp->zp_dedup);
1446e14bb325SJeff Bonwick 		BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
1447b24ab676SJeff Bonwick 		if (zp->zp_dedup) {
1448b24ab676SJeff Bonwick 			ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1449b24ab676SJeff Bonwick 			ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
1450b24ab676SJeff Bonwick 			zio->io_pipeline = ZIO_DDT_WRITE_PIPELINE;
1451b24ab676SJeff Bonwick 		}
145280901aeaSGeorge Wilson 		if (zp->zp_nopwrite) {
145380901aeaSGeorge Wilson 			ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
145480901aeaSGeorge Wilson 			ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
145580901aeaSGeorge Wilson 			zio->io_pipeline |= ZIO_STAGE_NOP_WRITE;
145680901aeaSGeorge Wilson 		}
1457b24ab676SJeff Bonwick 	}
1458b24ab676SJeff Bonwick 	return (ZIO_PIPELINE_CONTINUE);
1459b24ab676SJeff Bonwick }
1460b24ab676SJeff Bonwick 
1461b24ab676SJeff Bonwick static int
1462b24ab676SJeff Bonwick zio_free_bp_init(zio_t *zio)
1463b24ab676SJeff Bonwick {
1464b24ab676SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
1465b24ab676SJeff Bonwick 
1466b24ab676SJeff Bonwick 	if (zio->io_child_type == ZIO_CHILD_LOGICAL) {
1467b24ab676SJeff Bonwick 		if (BP_GET_DEDUP(bp))
1468b24ab676SJeff Bonwick 			zio->io_pipeline = ZIO_DDT_FREE_PIPELINE;
1469e14bb325SJeff Bonwick 	}
1470fa9e4066Sahrens 
14715cabbc6bSPrashanth Sreenivasa 	ASSERT3P(zio->io_bp, ==, &zio->io_bp_copy);
14725cabbc6bSPrashanth Sreenivasa 
1473e05725b1Sbonwick 	return (ZIO_PIPELINE_CONTINUE);
1474fa9e4066Sahrens }
1475fa9e4066Sahrens 
1476e14bb325SJeff Bonwick /*
1477e14bb325SJeff Bonwick  * ==========================================================================
1478e14bb325SJeff Bonwick  * Execute the I/O pipeline
1479e14bb325SJeff Bonwick  * ==========================================================================
1480e14bb325SJeff Bonwick  */
1481e14bb325SJeff Bonwick 
1482e14bb325SJeff Bonwick static void
1483ec94d322SAdam Leventhal zio_taskq_dispatch(zio_t *zio, zio_taskq_type_t q, boolean_t cutinline)
1484fa9e4066Sahrens {
148580eb36f2SGeorge Wilson 	spa_t *spa = zio->io_spa;
1486e14bb325SJeff Bonwick 	zio_type_t t = zio->io_type;
14875aeb9474SGarrett D'Amore 	int flags = (cutinline ? TQ_FRONT : 0);
14880a4e9518Sgw 
14890a4e9518Sgw 	/*
1490bbe36defSGeorge Wilson 	 * If we're a config writer or a probe, the normal issue and
1491bbe36defSGeorge Wilson 	 * interrupt threads may all be blocked waiting for the config lock.
1492bbe36defSGeorge Wilson 	 * In this case, select the otherwise-unused taskq for ZIO_TYPE_NULL.
14930a4e9518Sgw 	 */
1494bbe36defSGeorge Wilson 	if (zio->io_flags & (ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_PROBE))
1495e14bb325SJeff Bonwick 		t = ZIO_TYPE_NULL;
14960a4e9518Sgw 
14970a4e9518Sgw 	/*
1498e14bb325SJeff Bonwick 	 * A similar issue exists for the L2ARC write thread until L2ARC 2.0.
14990a4e9518Sgw 	 */
1500e14bb325SJeff Bonwick 	if (t == ZIO_TYPE_WRITE && zio->io_vd && zio->io_vd->vdev_aux)
1501e14bb325SJeff Bonwick 		t = ZIO_TYPE_NULL;
15020a4e9518Sgw 
150380eb36f2SGeorge Wilson 	/*
1504ec94d322SAdam Leventhal 	 * If this is a high priority I/O, then use the high priority taskq if
1505ec94d322SAdam Leventhal 	 * available.
150680eb36f2SGeorge Wilson 	 */
15072258ad0bSGeorge Wilson 	if ((zio->io_priority == ZIO_PRIORITY_NOW ||
15082258ad0bSGeorge Wilson 	    zio->io_priority == ZIO_PRIORITY_SYNC_WRITE) &&
1509ec94d322SAdam Leventhal 	    spa->spa_zio_taskq[t][q + 1].stqs_count != 0)
151080eb36f2SGeorge Wilson 		q++;
151180eb36f2SGeorge Wilson 
151280eb36f2SGeorge Wilson 	ASSERT3U(q, <, ZIO_TASKQ_TYPES);
15135aeb9474SGarrett D'Amore 
15145aeb9474SGarrett D'Amore 	/*
15155aeb9474SGarrett D'Amore 	 * NB: We are assuming that the zio can only be dispatched
15165aeb9474SGarrett D'Amore 	 * to a single taskq at a time.  It would be a grievous error
15175aeb9474SGarrett D'Amore 	 * to dispatch the zio to another taskq at the same time.
15185aeb9474SGarrett D'Amore 	 */
15195aeb9474SGarrett D'Amore 	ASSERT(zio->io_tqent.tqent_next == NULL);
1520ec94d322SAdam Leventhal 	spa_taskq_dispatch_ent(spa, t, q, (task_func_t *)zio_execute, zio,
1521ec94d322SAdam Leventhal 	    flags, &zio->io_tqent);
1522e14bb325SJeff Bonwick }
15230a4e9518Sgw 
1524e14bb325SJeff Bonwick static boolean_t
1525ec94d322SAdam Leventhal zio_taskq_member(zio_t *zio, zio_taskq_type_t q)
1526e14bb325SJeff Bonwick {
1527e14bb325SJeff Bonwick 	kthread_t *executor = zio->io_executor;
1528e14bb325SJeff Bonwick 	spa_t *spa = zio->io_spa;
15290a4e9518Sgw 
1530ec94d322SAdam Leventhal 	for (zio_type_t t = 0; t < ZIO_TYPES; t++) {
1531ec94d322SAdam Leventhal 		spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
1532ec94d322SAdam Leventhal 		uint_t i;
1533ec94d322SAdam Leventhal 		for (i = 0; i < tqs->stqs_count; i++) {
1534ec94d322SAdam Leventhal 			if (taskq_member(tqs->stqs_taskq[i], executor))
1535ec94d322SAdam Leventhal 				return (B_TRUE);
1536ec94d322SAdam Leventhal 		}
1537ec94d322SAdam Leventhal 	}
15380a4e9518Sgw 
1539e14bb325SJeff Bonwick 	return (B_FALSE);
1540e14bb325SJeff Bonwick }
1541e05725b1Sbonwick 
1542e14bb325SJeff Bonwick static int
1543e14bb325SJeff Bonwick zio_issue_async(zio_t *zio)
1544e14bb325SJeff Bonwick {
154535a5a358SJonathan Adams 	zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE);
1546e14bb325SJeff Bonwick 
1547e14bb325SJeff Bonwick 	return (ZIO_PIPELINE_STOP);
15480a4e9518Sgw }
15490a4e9518Sgw 
1550e14bb325SJeff Bonwick void
1551e14bb325SJeff Bonwick zio_interrupt(zio_t *zio)
15520a4e9518Sgw {
155335a5a358SJonathan Adams 	zio_taskq_dispatch(zio, ZIO_TASKQ_INTERRUPT, B_FALSE);
1554e14bb325SJeff Bonwick }
15550a4e9518Sgw 
155697e81309SPrakash Surya void
155797e81309SPrakash Surya zio_delay_interrupt(zio_t *zio)
155897e81309SPrakash Surya {
155997e81309SPrakash Surya 	/*
156097e81309SPrakash Surya 	 * The timeout_generic() function isn't defined in userspace, so
156197e81309SPrakash Surya 	 * rather than trying to implement the function, the zio delay
156297e81309SPrakash Surya 	 * functionality has been disabled for userspace builds.
156397e81309SPrakash Surya 	 */
156497e81309SPrakash Surya 
156597e81309SPrakash Surya #ifdef _KERNEL
156697e81309SPrakash Surya 	/*
156797e81309SPrakash Surya 	 * If io_target_timestamp is zero, then no delay has been registered
156897e81309SPrakash Surya 	 * for this IO, thus jump to the end of this function and "skip" the
156997e81309SPrakash Surya 	 * delay; issuing it directly to the zio layer.
157097e81309SPrakash Surya 	 */
157197e81309SPrakash Surya 	if (zio->io_target_timestamp != 0) {
157297e81309SPrakash Surya 		hrtime_t now = gethrtime();
157397e81309SPrakash Surya 
157497e81309SPrakash Surya 		if (now >= zio->io_target_timestamp) {
157597e81309SPrakash Surya 			/*
157697e81309SPrakash Surya 			 * This IO has already taken longer than the target
157797e81309SPrakash Surya 			 * delay to complete, so we don't want to delay it
157897e81309SPrakash Surya 			 * any longer; we "miss" the delay and issue it
157997e81309SPrakash Surya 			 * directly to the zio layer. This is likely due to
158097e81309SPrakash Surya 			 * the target latency being set to a value less than
158197e81309SPrakash Surya 			 * the underlying hardware can satisfy (e.g. delay
158297e81309SPrakash Surya 			 * set to 1ms, but the disks take 10ms to complete an
158397e81309SPrakash Surya 			 * IO request).
158497e81309SPrakash Surya 			 */
158597e81309SPrakash Surya 
158697e81309SPrakash Surya 			DTRACE_PROBE2(zio__delay__miss, zio_t *, zio,
158797e81309SPrakash Surya 			    hrtime_t, now);
158897e81309SPrakash Surya 
158997e81309SPrakash Surya 			zio_interrupt(zio);
159097e81309SPrakash Surya 		} else {
159197e81309SPrakash Surya 			hrtime_t diff = zio->io_target_timestamp - now;
159297e81309SPrakash Surya 
159397e81309SPrakash Surya 			DTRACE_PROBE3(zio__delay__hit, zio_t *, zio,
159497e81309SPrakash Surya 			    hrtime_t, now, hrtime_t, diff);
159597e81309SPrakash Surya 
159697e81309SPrakash Surya 			(void) timeout_generic(CALLOUT_NORMAL,
159797e81309SPrakash Surya 			    (void (*)(void *))zio_interrupt, zio, diff, 1, 0);
159897e81309SPrakash Surya 		}
159997e81309SPrakash Surya 
160097e81309SPrakash Surya 		return;
160197e81309SPrakash Surya 	}
160297e81309SPrakash Surya #endif
160397e81309SPrakash Surya 
160497e81309SPrakash Surya 	DTRACE_PROBE1(zio__delay__skip, zio_t *, zio);
160597e81309SPrakash Surya 	zio_interrupt(zio);
160697e81309SPrakash Surya }
160797e81309SPrakash Surya 
1608e14bb325SJeff Bonwick /*
1609e14bb325SJeff Bonwick  * Execute the I/O pipeline until one of the following occurs:
1610f7170741SWill Andrews  *
1611f7170741SWill Andrews  *	(1) the I/O completes
1612f7170741SWill Andrews  *	(2) the pipeline stalls waiting for dependent child I/Os
1613f7170741SWill Andrews  *	(3) the I/O issues, so we're waiting for an I/O completion interrupt
1614f7170741SWill Andrews  *	(4) the I/O is delegated by vdev-level caching or aggregation
1615f7170741SWill Andrews  *	(5) the I/O is deferred due to vdev-level queueing
1616f7170741SWill Andrews  *	(6) the I/O is handed off to another thread.
1617f7170741SWill Andrews  *
1618f7170741SWill Andrews  * In all cases, the pipeline stops whenever there's no CPU work; it never
1619f7170741SWill Andrews  * burns a thread in cv_wait().
1620e14bb325SJeff Bonwick  *
1621e14bb325SJeff Bonwick  * There's no locking on io_stage because there's no legitimate way
1622e14bb325SJeff Bonwick  * for multiple threads to be attempting to process the same I/O.
1623e14bb325SJeff Bonwick  */
1624b24ab676SJeff Bonwick static zio_pipe_stage_t *zio_pipeline[];
16250a4e9518Sgw 
1626e14bb325SJeff Bonwick void
1627e14bb325SJeff Bonwick zio_execute(zio_t *zio)
1628e14bb325SJeff Bonwick {
1629e14bb325SJeff Bonwick 	zio->io_executor = curthread;
16300a4e9518Sgw 
16310f7643c7SGeorge Wilson 	ASSERT3U(zio->io_queued_timestamp, >, 0);
16320f7643c7SGeorge Wilson 
1633e14bb325SJeff Bonwick 	while (zio->io_stage < ZIO_STAGE_DONE) {
1634b24ab676SJeff Bonwick 		enum zio_stage pipeline = zio->io_pipeline;
1635b24ab676SJeff Bonwick 		enum zio_stage stage = zio->io_stage;
1636e14bb325SJeff Bonwick 		int rv;
16370a4e9518Sgw 
1638e14bb325SJeff Bonwick 		ASSERT(!MUTEX_HELD(&zio->io_lock));
1639b24ab676SJeff Bonwick 		ASSERT(ISP2(stage));
1640b24ab676SJeff Bonwick 		ASSERT(zio->io_stall == NULL);
16410a4e9518Sgw 
1642b24ab676SJeff Bonwick 		do {
1643b24ab676SJeff Bonwick 			stage <<= 1;
1644b24ab676SJeff Bonwick 		} while ((stage & pipeline) == 0);
1645e14bb325SJeff Bonwick 
1646e14bb325SJeff Bonwick 		ASSERT(stage <= ZIO_STAGE_DONE);
16470a4e9518Sgw 
16480a4e9518Sgw 		/*
1649e14bb325SJeff Bonwick 		 * If we are in interrupt context and this pipeline stage
1650e14bb325SJeff Bonwick 		 * will grab a config lock that is held across I/O,
1651b24ab676SJeff Bonwick 		 * or may wait for an I/O that needs an interrupt thread
1652b24ab676SJeff Bonwick 		 * to complete, issue async to avoid deadlock.
165335a5a358SJonathan Adams 		 *
165435a5a358SJonathan Adams 		 * For VDEV_IO_START, we cut in line so that the io will
165535a5a358SJonathan Adams 		 * be sent to disk promptly.
16560a4e9518Sgw 		 */
1657b24ab676SJeff Bonwick 		if ((stage & ZIO_BLOCKING_STAGES) && zio->io_vd == NULL &&
1658e14bb325SJeff Bonwick 		    zio_taskq_member(zio, ZIO_TASKQ_INTERRUPT)) {
165935a5a358SJonathan Adams 			boolean_t cut = (stage == ZIO_STAGE_VDEV_IO_START) ?
166035a5a358SJonathan Adams 			    zio_requeue_io_start_cut_in_line : B_FALSE;
166135a5a358SJonathan Adams 			zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, cut);
1662e14bb325SJeff Bonwick 			return;
16630a4e9518Sgw 		}
16640a4e9518Sgw 
1665e14bb325SJeff Bonwick 		zio->io_stage = stage;
16660f7643c7SGeorge Wilson 		zio->io_pipeline_trace |= zio->io_stage;
1667bf16b11eSMatthew Ahrens 		rv = zio_pipeline[highbit64(stage) - 1](zio);
16680a4e9518Sgw 
1669e14bb325SJeff Bonwick 		if (rv == ZIO_PIPELINE_STOP)
1670e14bb325SJeff Bonwick 			return;
16710a4e9518Sgw 
1672e14bb325SJeff Bonwick 		ASSERT(rv == ZIO_PIPELINE_CONTINUE);
1673e14bb325SJeff Bonwick 	}
16740a4e9518Sgw }
16750a4e9518Sgw 
1676e14bb325SJeff Bonwick /*
1677e14bb325SJeff Bonwick  * ==========================================================================
1678e14bb325SJeff Bonwick  * Initiate I/O, either sync or async
1679e14bb325SJeff Bonwick  * ==========================================================================
1680e14bb325SJeff Bonwick  */
1681e14bb325SJeff Bonwick int
1682e14bb325SJeff Bonwick zio_wait(zio_t *zio)
16830a4e9518Sgw {
1684e14bb325SJeff Bonwick 	int error;
16850a4e9518Sgw 
16861271e4b1SPrakash Surya 	ASSERT3P(zio->io_stage, ==, ZIO_STAGE_OPEN);
16871271e4b1SPrakash Surya 	ASSERT3P(zio->io_executor, ==, NULL);
16880a4e9518Sgw 
1689e14bb325SJeff Bonwick 	zio->io_waiter = curthread;
16900f7643c7SGeorge Wilson 	ASSERT0(zio->io_queued_timestamp);
16910f7643c7SGeorge Wilson 	zio->io_queued_timestamp = gethrtime();
1692e05725b1Sbonwick 
1693e14bb325SJeff Bonwick 	zio_execute(zio);
16940a4e9518Sgw 
1695e14bb325SJeff Bonwick 	mutex_enter(&zio->io_lock);
1696e14bb325SJeff Bonwick 	while (zio->io_executor != NULL)
1697e14bb325SJeff Bonwick 		cv_wait(&zio->io_cv, &zio->io_lock);
1698e14bb325SJeff Bonwick 	mutex_exit(&zio->io_lock);
169932b87932Sek 
1700e14bb325SJeff Bonwick 	error = zio->io_error;
1701e14bb325SJeff Bonwick 	zio_destroy(zio);
170232b87932Sek 
1703e14bb325SJeff Bonwick 	return (error);
170432b87932Sek }
170532b87932Sek 
1706e14bb325SJeff Bonwick void
1707e14bb325SJeff Bonwick zio_nowait(zio_t *zio)
17080a4e9518Sgw {
17091271e4b1SPrakash Surya 	ASSERT3P(zio->io_executor, ==, NULL);
1710fa9e4066Sahrens 
1711a3f829aeSBill Moore 	if (zio->io_child_type == ZIO_CHILD_LOGICAL &&
1712a3f829aeSBill Moore 	    zio_unique_parent(zio) == NULL) {
1713ea8dc4b6Seschrock 		/*
1714e14bb325SJeff Bonwick 		 * This is a logical async I/O with no parent to wait for it.
171554d692b7SGeorge Wilson 		 * We add it to the spa_async_root_zio "Godfather" I/O which
171654d692b7SGeorge Wilson 		 * will ensure they complete prior to unloading the pool.
1717ea8dc4b6Seschrock 		 */
1718e14bb325SJeff Bonwick 		spa_t *spa = zio->io_spa;
171954d692b7SGeorge Wilson 
17206f834bc1SMatthew Ahrens 		zio_add_child(spa->spa_async_zio_root[CPU_SEQID], zio);
1721e14bb325SJeff Bonwick 	}
1722ea8dc4b6Seschrock 
17230f7643c7SGeorge Wilson 	ASSERT0(zio->io_queued_timestamp);
17240f7643c7SGeorge Wilson 	zio->io_queued_timestamp = gethrtime();
1725e14bb325SJeff Bonwick 	zio_execute(zio);
1726e14bb325SJeff Bonwick }
1727ea8dc4b6Seschrock 
1728e14bb325SJeff Bonwick /*
1729e14bb325SJeff Bonwick  * ==========================================================================
17301271e4b1SPrakash Surya  * Reexecute, cancel, or suspend/resume failed I/O
1731e14bb325SJeff Bonwick  * ==========================================================================
1732e14bb325SJeff Bonwick  */
1733fa9e4066Sahrens 
1734e14bb325SJeff Bonwick static void
1735e14bb325SJeff Bonwick zio_reexecute(zio_t *pio)
1736e14bb325SJeff Bonwick {
1737a3f829aeSBill Moore 	zio_t *cio, *cio_next;
1738a3f829aeSBill Moore 
1739a3f829aeSBill Moore 	ASSERT(pio->io_child_type == ZIO_CHILD_LOGICAL);
1740a3f829aeSBill Moore 	ASSERT(pio->io_orig_stage == ZIO_STAGE_OPEN);
1741f5383399SBill Moore 	ASSERT(pio->io_gang_leader == NULL);
1742f5383399SBill Moore 	ASSERT(pio->io_gang_tree == NULL);
1743e05725b1Sbonwick 
1744e14bb325SJeff Bonwick 	pio->io_flags = pio->io_orig_flags;
1745e14bb325SJeff Bonwick 	pio->io_stage = pio->io_orig_stage;
1746e14bb325SJeff Bonwick 	pio->io_pipeline = pio->io_orig_pipeline;
1747e14bb325SJeff Bonwick 	pio->io_reexecute = 0;
174880901aeaSGeorge Wilson 	pio->io_flags |= ZIO_FLAG_REEXECUTED;
17490f7643c7SGeorge Wilson 	pio->io_pipeline_trace = 0;
1750e14bb325SJeff Bonwick 	pio->io_error = 0;
1751a3f829aeSBill Moore 	for (int w = 0; w < ZIO_WAIT_TYPES; w++)
1752a3f829aeSBill Moore 		pio->io_state[w] = 0;
1753e14bb325SJeff Bonwick 	for (int c = 0; c < ZIO_CHILD_TYPES; c++)
1754e14bb325SJeff Bonwick 		pio->io_child_error[c] = 0;
17550a4e9518Sgw 
1756b24ab676SJeff Bonwick 	if (IO_IS_ALLOCATING(pio))
1757b24ab676SJeff Bonwick 		BP_ZERO(pio->io_bp);
1758d58459f4Sek 
1759e14bb325SJeff Bonwick 	/*
1760e14bb325SJeff Bonwick 	 * As we reexecute pio's children, new children could be created.
1761a3f829aeSBill Moore 	 * New children go to the head of pio's io_child_list, however,
1762e14bb325SJeff Bonwick 	 * so we will (correctly) not reexecute them.  The key is that
1763a3f829aeSBill Moore 	 * the remainder of pio's io_child_list, from 'cio_next' onward,
1764a3f829aeSBill Moore 	 * cannot be affected by any side effects of reexecuting 'cio'.
1765e14bb325SJeff Bonwick 	 */
17660f7643c7SGeorge Wilson 	zio_link_t *zl = NULL;
1767*a3874b8bSToomas Soome 	mutex_enter(&pio->io_lock);
17680f7643c7SGeorge Wilson 	for (cio = zio_walk_children(pio, &zl); cio != NULL; cio = cio_next) {
17690f7643c7SGeorge Wilson 		cio_next = zio_walk_children(pio, &zl);
1770a3f829aeSBill Moore 		for (int w = 0; w < ZIO_WAIT_TYPES; w++)
1771a3f829aeSBill Moore 			pio->io_children[cio->io_child_type][w]++;
1772e14bb325SJeff Bonwick 		mutex_exit(&pio->io_lock);
1773a3f829aeSBill Moore 		zio_reexecute(cio);
1774*a3874b8bSToomas Soome 		mutex_enter(&pio->io_lock);
1775fa9e4066Sahrens 	}
1776*a3874b8bSToomas Soome 	mutex_exit(&pio->io_lock);
1777e05725b1Sbonwick 
1778e14bb325SJeff Bonwick 	/*
1779e14bb325SJeff Bonwick 	 * Now that all children have been reexecuted, execute the parent.
178054d692b7SGeorge Wilson 	 * We don't reexecute "The Godfather" I/O here as it's the
178148bbca81SDaniel Hoffman 	 * responsibility of the caller to wait on it.
1782e14bb325SJeff Bonwick 	 */
17830f7643c7SGeorge Wilson 	if (!(pio->io_flags & ZIO_FLAG_GODFATHER)) {
17840f7643c7SGeorge Wilson 		pio->io_queued_timestamp = gethrtime();
178554d692b7SGeorge Wilson 		zio_execute(pio);
17860f7643c7SGeorge Wilson 	}
17870a4e9518Sgw }
17880a4e9518Sgw 
1789e14bb325SJeff Bonwick void
1790e0f1c0afSOlaf Faaland zio_suspend(spa_t *spa, zio_t *zio, zio_suspend_reason_t reason)
17910a4e9518Sgw {
1792e14bb325SJeff Bonwick 	if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_PANIC)
1793e14bb325SJeff Bonwick 		fm_panic("Pool '%s' has encountered an uncorrectable I/O "
1794e14bb325SJeff Bonwick 		    "failure and the failure mode property for this pool "
1795e14bb325SJeff Bonwick 		    "is set to panic.", spa_name(spa));
17960a4e9518Sgw 
1797e14bb325SJeff Bonwick 	zfs_ereport_post(FM_EREPORT_ZFS_IO_FAILURE, spa, NULL, NULL, 0, 0);
17980a4e9518Sgw 
1799e14bb325SJeff Bonwick 	mutex_enter(&spa->spa_suspend_lock);
1800fa9e4066Sahrens 
1801e14bb325SJeff Bonwick 	if (spa->spa_suspend_zio_root == NULL)
180254d692b7SGeorge Wilson 		spa->spa_suspend_zio_root = zio_root(spa, NULL, NULL,
180354d692b7SGeorge Wilson 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
180454d692b7SGeorge Wilson 		    ZIO_FLAG_GODFATHER);
1805fa9e4066Sahrens 
1806e0f1c0afSOlaf Faaland 	spa->spa_suspended = reason;
1807fa9e4066Sahrens 
1808e14bb325SJeff Bonwick 	if (zio != NULL) {
180954d692b7SGeorge Wilson 		ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER));
1810e14bb325SJeff Bonwick 		ASSERT(zio != spa->spa_suspend_zio_root);
1811e14bb325SJeff Bonwick 		ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1812a3f829aeSBill Moore 		ASSERT(zio_unique_parent(zio) == NULL);
1813e14bb325SJeff Bonwick 		ASSERT(zio->io_stage == ZIO_STAGE_DONE);
1814e14bb325SJeff Bonwick 		zio_add_child(spa->spa_suspend_zio_root, zio);
1815e14bb325SJeff Bonwick 	}
1816fa9e4066Sahrens 
1817e14bb325SJeff Bonwick 	mutex_exit(&spa->spa_suspend_lock);
1818e14bb325SJeff Bonwick }
1819fa9e4066Sahrens 
182054d692b7SGeorge Wilson int
1821e14bb325SJeff Bonwick zio_resume(spa_t *spa)
1822e14bb325SJeff Bonwick {
182354d692b7SGeorge Wilson 	zio_t *pio;
1824fa9e4066Sahrens 
1825b3995adbSahrens 	/*
1826e14bb325SJeff Bonwick 	 * Reexecute all previously suspended i/o.
1827b3995adbSahrens 	 */
1828e14bb325SJeff Bonwick 	mutex_enter(&spa->spa_suspend_lock);
1829e0f1c0afSOlaf Faaland 	spa->spa_suspended = ZIO_SUSPEND_NONE;
1830e14bb325SJeff Bonwick 	cv_broadcast(&spa->spa_suspend_cv);
1831e14bb325SJeff Bonwick 	pio = spa->spa_suspend_zio_root;
1832e14bb325SJeff Bonwick 	spa->spa_suspend_zio_root = NULL;
1833e14bb325SJeff Bonwick 	mutex_exit(&spa->spa_suspend_lock);
1834e14bb325SJeff Bonwick 
1835e14bb325SJeff Bonwick 	if (pio == NULL)
183654d692b7SGeorge Wilson 		return (0);
1837e14bb325SJeff Bonwick 
183854d692b7SGeorge Wilson 	zio_reexecute(pio);
183954d692b7SGeorge Wilson 	return (zio_wait(pio));
1840e14bb325SJeff Bonwick }
1841e14bb325SJeff Bonwick 
1842e14bb325SJeff Bonwick void
1843e14bb325SJeff Bonwick zio_resume_wait(spa_t *spa)
1844e14bb325SJeff Bonwick {
1845e14bb325SJeff Bonwick 	mutex_enter(&spa->spa_suspend_lock);
1846e14bb325SJeff Bonwick 	while (spa_suspended(spa))
1847e14bb325SJeff Bonwick 		cv_wait(&spa->spa_suspend_cv, &spa->spa_suspend_lock);
1848e14bb325SJeff Bonwick 	mutex_exit(&spa->spa_suspend_lock);
1849fa9e4066Sahrens }
1850fa9e4066Sahrens 
1851fa9e4066Sahrens /*
1852fa9e4066Sahrens  * ==========================================================================
1853e14bb325SJeff Bonwick  * Gang blocks.
1854e14bb325SJeff Bonwick  *
1855e14bb325SJeff Bonwick  * A gang block is a collection of small blocks that looks to the DMU
1856e14bb325SJeff Bonwick  * like one large block.  When zio_dva_allocate() cannot find a block
1857e14bb325SJeff Bonwick  * of the requested size, due to either severe fragmentation or the pool
1858e14bb325SJeff Bonwick  * being nearly full, it calls zio_write_gang_block() to construct the
1859e14bb325SJeff Bonwick  * block from smaller fragments.
1860e14bb325SJeff Bonwick  *
1861e14bb325SJeff Bonwick  * A gang block consists of a gang header (zio_gbh_phys_t) and up to
1862e14bb325SJeff Bonwick  * three (SPA_GBH_NBLKPTRS) gang members.  The gang header is just like
1863e14bb325SJeff Bonwick  * an indirect block: it's an array of block pointers.  It consumes
1864e14bb325SJeff Bonwick  * only one sector and hence is allocatable regardless of fragmentation.
1865e14bb325SJeff Bonwick  * The gang header's bps point to its gang members, which hold the data.
1866e14bb325SJeff Bonwick  *
1867e14bb325SJeff Bonwick  * Gang blocks are self-checksumming, using the bp's <vdev, offset, txg>
1868e14bb325SJeff Bonwick  * as the verifier to ensure uniqueness of the SHA256 checksum.
1869e14bb325SJeff Bonwick  * Critically, the gang block bp's blk_cksum is the checksum of the data,
1870e14bb325SJeff Bonwick  * not the gang header.  This ensures that data block signatures (needed for
1871e14bb325SJeff Bonwick  * deduplication) are independent of how the block is physically stored.
1872e14bb325SJeff Bonwick  *
1873e14bb325SJeff Bonwick  * Gang blocks can be nested: a gang member may itself be a gang block.
1874e14bb325SJeff Bonwick  * Thus every gang block is a tree in which root and all interior nodes are
1875e14bb325SJeff Bonwick  * gang headers, and the leaves are normal blocks that contain user data.
1876e14bb325SJeff Bonwick  * The root of the gang tree is called the gang leader.
1877e14bb325SJeff Bonwick  *
1878e14bb325SJeff Bonwick  * To perform any operation (read, rewrite, free, claim) on a gang block,
1879e14bb325SJeff Bonwick  * zio_gang_assemble() first assembles the gang tree (minus data leaves)
1880e14bb325SJeff Bonwick  * in the io_gang_tree field of the original logical i/o by recursively
1881e14bb325SJeff Bonwick  * reading the gang leader and all gang headers below it.  This yields
1882e14bb325SJeff Bonwick  * an in-core tree containing the contents of every gang header and the
1883e14bb325SJeff Bonwick  * bps for every constituent of the gang block.
1884e14bb325SJeff Bonwick  *
1885e14bb325SJeff Bonwick  * With the gang tree now assembled, zio_gang_issue() just walks the gang tree
1886e14bb325SJeff Bonwick  * and invokes a callback on each bp.  To free a gang block, zio_gang_issue()
1887e14bb325SJeff Bonwick  * calls zio_free_gang() -- a trivial wrapper around zio_free() -- for each bp.
1888e14bb325SJeff Bonwick  * zio_claim_gang() provides a similarly trivial wrapper for zio_claim().
1889e14bb325SJeff Bonwick  * zio_read_gang() is a wrapper around zio_read() that omits reading gang
1890e14bb325SJeff Bonwick  * headers, since we already have those in io_gang_tree.  zio_rewrite_gang()
1891e14bb325SJeff Bonwick  * performs a zio_rewrite() of the data or, for gang headers, a zio_rewrite()
1892e14bb325SJeff Bonwick  * of the gang header plus zio_checksum_compute() of the data to update the
1893e14bb325SJeff Bonwick  * gang header's blk_cksum as described above.
1894e14bb325SJeff Bonwick  *
1895e14bb325SJeff Bonwick  * The two-phase assemble/issue model solves the problem of partial failure --
1896e14bb325SJeff Bonwick  * what if you'd freed part of a gang block but then couldn't read the
1897e14bb325SJeff Bonwick  * gang header for another part?  Assembling the entire gang tree first
1898e14bb325SJeff Bonwick  * ensures that all the necessary gang header I/O has succeeded before
1899e14bb325SJeff Bonwick  * starting the actual work of free, claim, or write.  Once the gang tree
1900e14bb325SJeff Bonwick  * is assembled, free and claim are in-memory operations that cannot fail.
1901e14bb325SJeff Bonwick  *
1902e14bb325SJeff Bonwick  * In the event that a gang write fails, zio_dva_unallocate() walks the
1903e14bb325SJeff Bonwick  * gang tree to immediately free (i.e. insert back into the space map)
1904e14bb325SJeff Bonwick  * everything we've allocated.  This ensures that we don't get ENOSPC
1905e14bb325SJeff Bonwick  * errors during repeated suspend/resume cycles due to a flaky device.
1906e14bb325SJeff Bonwick  *
1907e14bb325SJeff Bonwick  * Gang rewrites only happen during sync-to-convergence.  If we can't assemble
1908e14bb325SJeff Bonwick  * the gang tree, we won't modify the block, so we can safely defer the free
1909e14bb325SJeff Bonwick  * (knowing that the block is still intact).  If we *can* assemble the gang
1910e14bb325SJeff Bonwick  * tree, then even if some of the rewrites fail, zio_dva_unallocate() will free
1911e14bb325SJeff Bonwick  * each constituent bp and we can allocate a new block on the next sync pass.
1912e14bb325SJeff Bonwick  *
1913e14bb325SJeff Bonwick  * In all cases, the gang tree allows complete recovery from partial failure.
1914fa9e4066Sahrens  * ==========================================================================
1915fa9e4066Sahrens  */
1916e14bb325SJeff Bonwick 
1917770499e1SDan Kimmel static void
1918770499e1SDan Kimmel zio_gang_issue_func_done(zio_t *zio)
1919770499e1SDan Kimmel {
1920770499e1SDan Kimmel 	abd_put(zio->io_abd);
1921770499e1SDan Kimmel }
1922770499e1SDan Kimmel 
1923e14bb325SJeff Bonwick static zio_t *
1924770499e1SDan Kimmel zio_read_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data,
1925770499e1SDan Kimmel     uint64_t offset)
1926fa9e4066Sahrens {
1927e14bb325SJeff Bonwick 	if (gn != NULL)
1928e14bb325SJeff Bonwick 		return (pio);
1929fa9e4066Sahrens 
1930770499e1SDan Kimmel 	return (zio_read(pio, pio->io_spa, bp, abd_get_offset(data, offset),
1931770499e1SDan Kimmel 	    BP_GET_PSIZE(bp), zio_gang_issue_func_done,
1932770499e1SDan Kimmel 	    NULL, pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio),
1933e14bb325SJeff Bonwick 	    &pio->io_bookmark));
1934e14bb325SJeff Bonwick }
1935e14bb325SJeff Bonwick 
1936770499e1SDan Kimmel static zio_t *
1937770499e1SDan Kimmel zio_rewrite_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data,
1938770499e1SDan Kimmel     uint64_t offset)
1939e14bb325SJeff Bonwick {
1940e14bb325SJeff Bonwick 	zio_t *zio;
1941e14bb325SJeff Bonwick 
1942e14bb325SJeff Bonwick 	if (gn != NULL) {
1943770499e1SDan Kimmel 		abd_t *gbh_abd =
1944770499e1SDan Kimmel 		    abd_get_from_buf(gn->gn_gbh, SPA_GANGBLOCKSIZE);
1945e14bb325SJeff Bonwick 		zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp,
1946770499e1SDan Kimmel 		    gbh_abd, SPA_GANGBLOCKSIZE, zio_gang_issue_func_done, NULL,
1947770499e1SDan Kimmel 		    pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio),
1948770499e1SDan Kimmel 		    &pio->io_bookmark);
1949fa9e4066Sahrens 		/*
1950e14bb325SJeff Bonwick 		 * As we rewrite each gang header, the pipeline will compute
1951e14bb325SJeff Bonwick 		 * a new gang block header checksum for it; but no one will
1952e14bb325SJeff Bonwick 		 * compute a new data checksum, so we do that here.  The one
1953e14bb325SJeff Bonwick 		 * exception is the gang leader: the pipeline already computed
1954e14bb325SJeff Bonwick 		 * its data checksum because that stage precedes gang assembly.
1955e14bb325SJeff Bonwick 		 * (Presently, nothing actually uses interior data checksums;
1956e14bb325SJeff Bonwick 		 * this is just good hygiene.)
1957fa9e4066Sahrens 		 */
1958f5383399SBill Moore 		if (gn != pio->io_gang_leader->io_gang_tree) {
1959770499e1SDan Kimmel 			abd_t *buf = abd_get_offset(data, offset);
1960770499e1SDan Kimmel 
1961e14bb325SJeff Bonwick 			zio_checksum_compute(zio, BP_GET_CHECKSUM(bp),
1962770499e1SDan Kimmel 			    buf, BP_GET_PSIZE(bp));
1963770499e1SDan Kimmel 
1964770499e1SDan Kimmel 			abd_put(buf);
1965e14bb325SJeff Bonwick 		}
1966b24ab676SJeff Bonwick 		/*
1967b24ab676SJeff Bonwick 		 * If we are here to damage data for testing purposes,
1968b24ab676SJeff Bonwick 		 * leave the GBH alone so that we can detect the damage.
1969b24ab676SJeff Bonwick 		 */
1970b24ab676SJeff Bonwick 		if (pio->io_gang_leader->io_flags & ZIO_FLAG_INDUCE_DAMAGE)
1971b24ab676SJeff Bonwick 			zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES;
1972fa9e4066Sahrens 	} else {
1973e14bb325SJeff Bonwick 		zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp,
1974770499e1SDan Kimmel 		    abd_get_offset(data, offset), BP_GET_PSIZE(bp),
1975770499e1SDan Kimmel 		    zio_gang_issue_func_done, NULL, pio->io_priority,
1976e14bb325SJeff Bonwick 		    ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
1977fa9e4066Sahrens 	}
1978fa9e4066Sahrens 
1979e14bb325SJeff Bonwick 	return (zio);
1980e14bb325SJeff Bonwick }
1981fa9e4066Sahrens 
1982e14bb325SJeff Bonwick /* ARGSUSED */
1983770499e1SDan Kimmel static zio_t *
1984770499e1SDan Kimmel zio_free_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data,
1985770499e1SDan Kimmel     uint64_t offset)
1986e14bb325SJeff Bonwick {
1987b24ab676SJeff Bonwick 	return (zio_free_sync(pio, pio->io_spa, pio->io_txg, bp,
1988b24ab676SJeff Bonwick 	    ZIO_GANG_CHILD_FLAGS(pio)));
1989fa9e4066Sahrens }
1990fa9e4066Sahrens 
1991e14bb325SJeff Bonwick /* ARGSUSED */
1992770499e1SDan Kimmel static zio_t *
1993770499e1SDan Kimmel zio_claim_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data,
1994770499e1SDan Kimmel     uint64_t offset)
1995fa9e4066Sahrens {
1996e14bb325SJeff Bonwick 	return (zio_claim(pio, pio->io_spa, pio->io_txg, bp,
1997e14bb325SJeff Bonwick 	    NULL, NULL, ZIO_GANG_CHILD_FLAGS(pio)));
1998e14bb325SJeff Bonwick }
1999fa9e4066Sahrens 
2000e14bb325SJeff Bonwick static zio_gang_issue_func_t *zio_gang_issue_func[ZIO_TYPES] = {
2001e14bb325SJeff Bonwick 	NULL,
2002e14bb325SJeff Bonwick 	zio_read_gang,
2003e14bb325SJeff Bonwick 	zio_rewrite_gang,
2004e14bb325SJeff Bonwick 	zio_free_gang,
2005e14bb325SJeff Bonwick 	zio_claim_gang,
2006e14bb325SJeff Bonwick 	NULL
2007e14bb325SJeff Bonwick };
2008fa9e4066Sahrens 
2009e14bb325SJeff Bonwick static void zio_gang_tree_assemble_done(zio_t *zio);
2010fa9e4066Sahrens 
2011e14bb325SJeff Bonwick static zio_gang_node_t *
2012e14bb325SJeff Bonwick zio_gang_node_alloc(zio_gang_node_t **gnpp)
2013e14bb325SJeff Bonwick {
2014e14bb325SJeff Bonwick 	zio_gang_node_t *gn;
2015fa9e4066Sahrens 
2016e14bb325SJeff Bonwick 	ASSERT(*gnpp == NULL);
2017fa9e4066Sahrens 
2018e14bb325SJeff Bonwick 	gn = kmem_zalloc(sizeof (*gn), KM_SLEEP);
2019e14bb325SJeff Bonwick 	gn->gn_gbh = zio_buf_alloc(SPA_GANGBLOCKSIZE);
2020e14bb325SJeff Bonwick 	*gnpp = gn;
2021e14bb325SJeff Bonwick 
2022e14bb325SJeff Bonwick 	return (gn);
2023fa9e4066Sahrens }
2024fa9e4066Sahrens 
2025fa9e4066Sahrens static void
2026e14bb325SJeff Bonwick zio_gang_node_free(zio_gang_node_t **gnpp)
2027fa9e4066Sahrens {
2028e14bb325SJeff Bonwick 	zio_gang_node_t *gn = *gnpp;
2029fa9e4066Sahrens 
2030e14bb325SJeff Bonwick 	for (int g = 0; g < SPA_GBH_NBLKPTRS; g++)
2031e14bb325SJeff Bonwick 		ASSERT(gn->gn_child[g] == NULL);
2032e14bb325SJeff Bonwick 
2033e14bb325SJeff Bonwick 	zio_buf_free(gn->gn_gbh, SPA_GANGBLOCKSIZE);
2034e14bb325SJeff Bonwick 	kmem_free(gn, sizeof (*gn));
2035e14bb325SJeff Bonwick 	*gnpp = NULL;
2036fa9e4066Sahrens }
2037fa9e4066Sahrens 
2038e14bb325SJeff Bonwick static void
2039e14bb325SJeff Bonwick zio_gang_tree_free(zio_gang_node_t **gnpp)
2040fa9e4066Sahrens {
2041e14bb325SJeff Bonwick 	zio_gang_node_t *gn = *gnpp;
2042fa9e4066Sahrens 
2043e14bb325SJeff Bonwick 	if (gn == NULL)
2044e14bb325SJeff Bonwick 		return;
2045fa9e4066Sahrens 
2046e14bb325SJeff Bonwick 	for (int g = 0; g < SPA_GBH_NBLKPTRS; g++)
2047e14bb325SJeff Bonwick 		zio_gang_tree_free(&gn->gn_child[g]);
2048fa9e4066Sahrens 
2049e14bb325SJeff Bonwick 	zio_gang_node_free(gnpp);
2050fa9e4066Sahrens }
2051fa9e4066Sahrens 
2052e14bb325SJeff Bonwick static void
2053f5383399SBill Moore zio_gang_tree_assemble(zio_t *gio, blkptr_t *bp, zio_gang_node_t **gnpp)
2054fa9e4066Sahrens {
2055e14bb325SJeff Bonwick 	zio_gang_node_t *gn = zio_gang_node_alloc(gnpp);
2056770499e1SDan Kimmel 	abd_t *gbh_abd = abd_get_from_buf(gn->gn_gbh, SPA_GANGBLOCKSIZE);
2057e14bb325SJeff Bonwick 
2058f5383399SBill Moore 	ASSERT(gio->io_gang_leader == gio);
2059e14bb325SJeff Bonwick 	ASSERT(BP_IS_GANG(bp));
2060fa9e4066Sahrens 
2061770499e1SDan Kimmel 	zio_nowait(zio_read(gio, gio->io_spa, bp, gbh_abd, SPA_GANGBLOCKSIZE,
2062770499e1SDan Kimmel 	    zio_gang_tree_assemble_done, gn, gio->io_priority,
2063770499e1SDan Kimmel 	    ZIO_GANG_CHILD_FLAGS(gio), &gio->io_bookmark));
2064e14bb325SJeff Bonwick }
2065fa9e4066Sahrens 
2066e14bb325SJeff Bonwick static void
2067e14bb325SJeff Bonwick zio_gang_tree_assemble_done(zio_t *zio)
2068e14bb325SJeff Bonwick {
2069f5383399SBill Moore 	zio_t *gio = zio->io_gang_leader;
2070e14bb325SJeff Bonwick 	zio_gang_node_t *gn = zio->io_private;
2071e14bb325SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
2072fa9e4066Sahrens 
2073f5383399SBill Moore 	ASSERT(gio == zio_unique_parent(zio));
2074b24ab676SJeff Bonwick 	ASSERT(zio->io_child_count == 0);
2075fa9e4066Sahrens 
2076e14bb325SJeff Bonwick 	if (zio->io_error)
2077e14bb325SJeff Bonwick 		return;
2078fa9e4066Sahrens 
2079770499e1SDan Kimmel 	/* this ABD was created from a linear buf in zio_gang_tree_assemble */
2080e14bb325SJeff Bonwick 	if (BP_SHOULD_BYTESWAP(bp))
2081770499e1SDan Kimmel 		byteswap_uint64_array(abd_to_buf(zio->io_abd), zio->io_size);
2082fa9e4066Sahrens 
2083770499e1SDan Kimmel 	ASSERT3P(abd_to_buf(zio->io_abd), ==, gn->gn_gbh);
2084e14bb325SJeff Bonwick 	ASSERT(zio->io_size == SPA_GANGBLOCKSIZE);
20856e1f5caaSNeil Perrin 	ASSERT(gn->gn_gbh->zg_tail.zec_magic == ZEC_MAGIC);
2086e05725b1Sbonwick 
2087770499e1SDan Kimmel 	abd_put(zio->io_abd);
2088770499e1SDan Kimmel 
2089e14bb325SJeff Bonwick 	for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
2090e14bb325SJeff Bonwick 		blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g];
2091e14bb325SJeff Bonwick 		if (!BP_IS_GANG(gbp))
2092e14bb325SJeff Bonwick 			continue;
2093f5383399SBill Moore 		zio_gang_tree_assemble(gio, gbp, &gn->gn_child[g]);
2094e14bb325SJeff Bonwick 	}
2095fa9e4066Sahrens }
2096fa9e4066Sahrens 
2097e14bb325SJeff Bonwick static void
2098770499e1SDan Kimmel zio_gang_tree_issue(zio_t *pio, zio_gang_node_t *gn, blkptr_t *bp, abd_t *data,
2099770499e1SDan Kimmel     uint64_t offset)
2100fa9e4066Sahrens {
2101f5383399SBill Moore 	zio_t *gio = pio->io_gang_leader;
2102e14bb325SJeff Bonwick 	zio_t *zio;
2103fa9e4066Sahrens 
2104e14bb325SJeff Bonwick 	ASSERT(BP_IS_GANG(bp) == !!gn);
2105f5383399SBill Moore 	ASSERT(BP_GET_CHECKSUM(bp) == BP_GET_CHECKSUM(gio->io_bp));
2106f5383399SBill Moore 	ASSERT(BP_GET_LSIZE(bp) == BP_GET_PSIZE(bp) || gn == gio->io_gang_tree);
2107fa9e4066Sahrens 
2108e14bb325SJeff Bonwick 	/*
2109e14bb325SJeff Bonwick 	 * If you're a gang header, your data is in gn->gn_gbh.
2110e14bb325SJeff Bonwick 	 * If you're a gang member, your data is in 'data' and gn == NULL.
2111e14bb325SJeff Bonwick 	 */
2112770499e1SDan Kimmel 	zio = zio_gang_issue_func[gio->io_type](pio, bp, gn, data, offset);
2113fa9e4066Sahrens 
2114e14bb325SJeff Bonwick 	if (gn != NULL) {
21156e1f5caaSNeil Perrin 		ASSERT(gn->gn_gbh->zg_tail.zec_magic == ZEC_MAGIC);
2116fa9e4066Sahrens 
2117e14bb325SJeff Bonwick 		for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
2118e14bb325SJeff Bonwick 			blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g];
2119e14bb325SJeff Bonwick 			if (BP_IS_HOLE(gbp))
2120e14bb325SJeff Bonwick 				continue;
2121770499e1SDan Kimmel 			zio_gang_tree_issue(zio, gn->gn_child[g], gbp, data,
2122770499e1SDan Kimmel 			    offset);
2123770499e1SDan Kimmel 			offset += BP_GET_PSIZE(gbp);
2124e14bb325SJeff Bonwick 		}
2125fa9e4066Sahrens 	}
2126fa9e4066Sahrens 
2127f5383399SBill Moore 	if (gn == gio->io_gang_tree)
2128770499e1SDan Kimmel 		ASSERT3U(gio->io_size, ==, offset);
2129e05725b1Sbonwick 
2130e14bb325SJeff Bonwick 	if (zio != pio)
2131e14bb325SJeff Bonwick 		zio_nowait(zio);
2132fa9e4066Sahrens }
2133fa9e4066Sahrens 
2134e05725b1Sbonwick static int
2135e14bb325SJeff Bonwick zio_gang_assemble(zio_t *zio)
2136fa9e4066Sahrens {
2137e14bb325SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
2138fa9e4066Sahrens 
2139f5383399SBill Moore 	ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == NULL);
2140f5383399SBill Moore 	ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
2141f5383399SBill Moore 
2142f5383399SBill Moore 	zio->io_gang_leader = zio;
2143fa9e4066Sahrens 
2144e14bb325SJeff Bonwick 	zio_gang_tree_assemble(zio, bp, &zio->io_gang_tree);
2145e05725b1Sbonwick 
2146e05725b1Sbonwick 	return (ZIO_PIPELINE_CONTINUE);
2147fa9e4066Sahrens }
2148fa9e4066Sahrens 
2149e05725b1Sbonwick static int
2150e14bb325SJeff Bonwick zio_gang_issue(zio_t *zio)
2151fa9e4066Sahrens {
2152e14bb325SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
2153fa9e4066Sahrens 
2154d6e1c446SGeorge Wilson 	if (zio_wait_for_children(zio, ZIO_CHILD_GANG_BIT, ZIO_WAIT_DONE)) {
2155e14bb325SJeff Bonwick 		return (ZIO_PIPELINE_STOP);
2156d6e1c446SGeorge Wilson 	}
2157fa9e4066Sahrens 
2158f5383399SBill Moore 	ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == zio);
2159f5383399SBill Moore 	ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
2160fa9e4066Sahrens 
2161e14bb325SJeff Bonwick 	if (zio->io_child_error[ZIO_CHILD_GANG] == 0)
2162770499e1SDan Kimmel 		zio_gang_tree_issue(zio, zio->io_gang_tree, bp, zio->io_abd,
2163770499e1SDan Kimmel 		    0);
2164e14bb325SJeff Bonwick 	else
2165f5383399SBill Moore 		zio_gang_tree_free(&zio->io_gang_tree);
2166fa9e4066Sahrens 
2167e14bb325SJeff Bonwick 	zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2168e05725b1Sbonwick 
2169e05725b1Sbonwick 	return (ZIO_PIPELINE_CONTINUE);
2170fa9e4066Sahrens }
2171fa9e4066Sahrens 
2172fa9e4066Sahrens static void
2173e14bb325SJeff Bonwick zio_write_gang_member_ready(zio_t *zio)
2174fa9e4066Sahrens {
2175a3f829aeSBill Moore 	zio_t *pio = zio_unique_parent(zio);
2176f5383399SBill Moore 	zio_t *gio = zio->io_gang_leader;
217744cd46caSbillm 	dva_t *cdva = zio->io_bp->blk_dva;
217844cd46caSbillm 	dva_t *pdva = pio->io_bp->blk_dva;
2179fa9e4066Sahrens 	uint64_t asize;
2180fa9e4066Sahrens 
2181e14bb325SJeff Bonwick 	if (BP_IS_HOLE(zio->io_bp))
2182e14bb325SJeff Bonwick 		return;
2183e14bb325SJeff Bonwick 
2184e14bb325SJeff Bonwick 	ASSERT(BP_IS_HOLE(&zio->io_bp_orig));
2185e14bb325SJeff Bonwick 
2186e14bb325SJeff Bonwick 	ASSERT(zio->io_child_type == ZIO_CHILD_GANG);
2187b24ab676SJeff Bonwick 	ASSERT3U(zio->io_prop.zp_copies, ==, gio->io_prop.zp_copies);
2188b24ab676SJeff Bonwick 	ASSERT3U(zio->io_prop.zp_copies, <=, BP_GET_NDVAS(zio->io_bp));
2189b24ab676SJeff Bonwick 	ASSERT3U(pio->io_prop.zp_copies, <=, BP_GET_NDVAS(pio->io_bp));
219044cd46caSbillm 	ASSERT3U(BP_GET_NDVAS(zio->io_bp), <=, BP_GET_NDVAS(pio->io_bp));
2191fa9e4066Sahrens 
2192fa9e4066Sahrens 	mutex_enter(&pio->io_lock);
2193e14bb325SJeff Bonwick 	for (int d = 0; d < BP_GET_NDVAS(zio->io_bp); d++) {
219444cd46caSbillm 		ASSERT(DVA_GET_GANG(&pdva[d]));
219544cd46caSbillm 		asize = DVA_GET_ASIZE(&pdva[d]);
219644cd46caSbillm 		asize += DVA_GET_ASIZE(&cdva[d]);
219744cd46caSbillm 		DVA_SET_ASIZE(&pdva[d], asize);
219844cd46caSbillm 	}
2199fa9e4066Sahrens 	mutex_exit(&pio->io_lock);
2200fa9e4066Sahrens }
2201fa9e4066Sahrens 
2202770499e1SDan Kimmel static void
2203770499e1SDan Kimmel zio_write_gang_done(zio_t *zio)
2204770499e1SDan Kimmel {
22057341a7deSBrad Lewis 	/*
22067341a7deSBrad Lewis 	 * The io_abd field will be NULL for a zio with no data.  The io_flags
22077341a7deSBrad Lewis 	 * will initially have the ZIO_FLAG_NODATA bit flag set, but we can't
22087341a7deSBrad Lewis 	 * check for it here as it is cleared in zio_ready.
22097341a7deSBrad Lewis 	 */
22107341a7deSBrad Lewis 	if (zio->io_abd != NULL)
22117341a7deSBrad Lewis 		abd_put(zio->io_abd);
2212770499e1SDan Kimmel }
2213770499e1SDan Kimmel 
22140a4e9518Sgw static int
2215e14bb325SJeff Bonwick zio_write_gang_block(zio_t *pio)
2216fa9e4066Sahrens {
2217e14bb325SJeff Bonwick 	spa_t *spa = pio->io_spa;
22180f7643c7SGeorge Wilson 	metaslab_class_t *mc = spa_normal_class(spa);
2219e14bb325SJeff Bonwick 	blkptr_t *bp = pio->io_bp;
2220f5383399SBill Moore 	zio_t *gio = pio->io_gang_leader;
2221e14bb325SJeff Bonwick 	zio_t *zio;
2222e14bb325SJeff Bonwick 	zio_gang_node_t *gn, **gnpp;
2223fa9e4066Sahrens 	zio_gbh_phys_t *gbh;
2224770499e1SDan Kimmel 	abd_t *gbh_abd;
2225e14bb325SJeff Bonwick 	uint64_t txg = pio->io_txg;
2226e14bb325SJeff Bonwick 	uint64_t resid = pio->io_size;
2227e14bb325SJeff Bonwick 	uint64_t lsize;
2228b24ab676SJeff Bonwick 	int copies = gio->io_prop.zp_copies;
2229b24ab676SJeff Bonwick 	int gbh_copies = MIN(copies + 1, spa_max_replication(spa));
2230e14bb325SJeff Bonwick 	zio_prop_t zp;
2231fa9e4066Sahrens 	int error;
22327341a7deSBrad Lewis 	boolean_t has_data = !(pio->io_flags & ZIO_FLAG_NODATA);
2233fa9e4066Sahrens 
22340f7643c7SGeorge Wilson 	int flags = METASLAB_HINTBP_FAVOR | METASLAB_GANG_HEADER;
22350f7643c7SGeorge Wilson 	if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
22360f7643c7SGeorge Wilson 		ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
22377341a7deSBrad Lewis 		ASSERT(has_data);
22380f7643c7SGeorge Wilson 
22390f7643c7SGeorge Wilson 		flags |= METASLAB_ASYNC_ALLOC;
2240e914ace2STim Schumacher 		VERIFY(zfs_refcount_held(&mc->mc_alloc_slots[pio->io_allocator],
2241f78cdc34SPaul Dagnelie 		    pio));
22420f7643c7SGeorge Wilson 
22430f7643c7SGeorge Wilson 		/*
22440f7643c7SGeorge Wilson 		 * The logical zio has already placed a reservation for
22450f7643c7SGeorge Wilson 		 * 'copies' allocation slots but gang blocks may require
22460f7643c7SGeorge Wilson 		 * additional copies. These additional copies
22470f7643c7SGeorge Wilson 		 * (i.e. gbh_copies - copies) are guaranteed to succeed
22480f7643c7SGeorge Wilson 		 * since metaslab_class_throttle_reserve() always allows
22490f7643c7SGeorge Wilson 		 * additional reservations for gang blocks.
22500f7643c7SGeorge Wilson 		 */
22510f7643c7SGeorge Wilson 		VERIFY(metaslab_class_throttle_reserve(mc, gbh_copies - copies,
2252f78cdc34SPaul Dagnelie 		    pio->io_allocator, pio, flags));
22530f7643c7SGeorge Wilson 	}
22540f7643c7SGeorge Wilson 
22550f7643c7SGeorge Wilson 	error = metaslab_alloc(spa, mc, SPA_GANGBLOCKSIZE,
22568363e80aSGeorge Wilson 	    bp, gbh_copies, txg, pio == gio ? NULL : gio->io_bp, flags,
2257f78cdc34SPaul Dagnelie 	    &pio->io_alloc_list, pio, pio->io_allocator);
2258e05725b1Sbonwick 	if (error) {
22590f7643c7SGeorge Wilson 		if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
22600f7643c7SGeorge Wilson 			ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
22617341a7deSBrad Lewis 			ASSERT(has_data);
22620f7643c7SGeorge Wilson 
22630f7643c7SGeorge Wilson 			/*
22640f7643c7SGeorge Wilson 			 * If we failed to allocate the gang block header then
22650f7643c7SGeorge Wilson 			 * we remove any additional allocation reservations that
22660f7643c7SGeorge Wilson 			 * we placed here. The original reservation will
22670f7643c7SGeorge Wilson 			 * be removed when the logical I/O goes to the ready
22680f7643c7SGeorge Wilson 			 * stage.
22690f7643c7SGeorge Wilson 			 */
22700f7643c7SGeorge Wilson 			metaslab_class_throttle_unreserve(mc,
2271f78cdc34SPaul Dagnelie 			    gbh_copies - copies, pio->io_allocator, pio);
22720f7643c7SGeorge Wilson 		}
2273e14bb325SJeff Bonwick 		pio->io_error = error;
2274e05725b1Sbonwick 		return (ZIO_PIPELINE_CONTINUE);
2275e05725b1Sbonwick 	}
2276fa9e4066Sahrens 
2277f5383399SBill Moore 	if (pio == gio) {
2278f5383399SBill Moore 		gnpp = &gio->io_gang_tree;
2279e14bb325SJeff Bonwick 	} else {
2280e14bb325SJeff Bonwick 		gnpp = pio->io_private;
2281e14bb325SJeff Bonwick 		ASSERT(pio->io_ready == zio_write_gang_member_ready);
2282fa9e4066Sahrens 	}
2283fa9e4066Sahrens 
2284e14bb325SJeff Bonwick 	gn = zio_gang_node_alloc(gnpp);
2285e14bb325SJeff Bonwick 	gbh = gn->gn_gbh;
2286e14bb325SJeff Bonwick 	bzero(gbh, SPA_GANGBLOCKSIZE);
2287770499e1SDan Kimmel 	gbh_abd = abd_get_from_buf(gbh, SPA_GANGBLOCKSIZE);
2288fa9e4066Sahrens 
2289e14bb325SJeff Bonwick 	/*
2290e14bb325SJeff Bonwick 	 * Create the gang header.
2291e14bb325SJeff Bonwick 	 */
2292770499e1SDan Kimmel 	zio = zio_rewrite(pio, spa, txg, bp, gbh_abd, SPA_GANGBLOCKSIZE,
2293770499e1SDan Kimmel 	    zio_write_gang_done, NULL, pio->io_priority,
2294770499e1SDan Kimmel 	    ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
2295fa9e4066Sahrens 
2296e14bb325SJeff Bonwick 	/*
2297e14bb325SJeff Bonwick 	 * Create and nowait the gang children.
2298e14bb325SJeff Bonwick 	 */
2299e14bb325SJeff Bonwick 	for (int g = 0; resid != 0; resid -= lsize, g++) {
2300e14bb325SJeff Bonwick 		lsize = P2ROUNDUP(resid / (SPA_GBH_NBLKPTRS - g),
2301e14bb325SJeff Bonwick 		    SPA_MINBLOCKSIZE);
2302e14bb325SJeff Bonwick 		ASSERT(lsize >= SPA_MINBLOCKSIZE && lsize <= resid);
2303e14bb325SJeff Bonwick 
2304f5383399SBill Moore 		zp.zp_checksum = gio->io_prop.zp_checksum;
2305e14bb325SJeff Bonwick 		zp.zp_compress = ZIO_COMPRESS_OFF;
2306e14bb325SJeff Bonwick 		zp.zp_type = DMU_OT_NONE;
2307e14bb325SJeff Bonwick 		zp.zp_level = 0;
2308b24ab676SJeff Bonwick 		zp.zp_copies = gio->io_prop.zp_copies;
230980901aeaSGeorge Wilson 		zp.zp_dedup = B_FALSE;
231080901aeaSGeorge Wilson 		zp.zp_dedup_verify = B_FALSE;
231180901aeaSGeorge Wilson 		zp.zp_nopwrite = B_FALSE;
2312e14bb325SJeff Bonwick 
23130f7643c7SGeorge Wilson 		zio_t *cio = zio_write(zio, spa, txg, &gbh->zg_blkptr[g],
23147341a7deSBrad Lewis 		    has_data ? abd_get_offset(pio->io_abd, pio->io_size -
23157341a7deSBrad Lewis 		    resid) : NULL, lsize, lsize, &zp,
23167341a7deSBrad Lewis 		    zio_write_gang_member_ready, NULL, NULL,
2317770499e1SDan Kimmel 		    zio_write_gang_done, &gn->gn_child[g], pio->io_priority,
23180f7643c7SGeorge Wilson 		    ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
23190f7643c7SGeorge Wilson 
23200f7643c7SGeorge Wilson 		if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
23210f7643c7SGeorge Wilson 			ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
23227341a7deSBrad Lewis 			ASSERT(has_data);
23230f7643c7SGeorge Wilson 
23240f7643c7SGeorge Wilson 			/*
23250f7643c7SGeorge Wilson 			 * Gang children won't throttle but we should
23260f7643c7SGeorge Wilson 			 * account for their work, so reserve an allocation
23270f7643c7SGeorge Wilson 			 * slot for them here.
23280f7643c7SGeorge Wilson 			 */
23290f7643c7SGeorge Wilson 			VERIFY(metaslab_class_throttle_reserve(mc,
2330f78cdc34SPaul Dagnelie 			    zp.zp_copies, cio->io_allocator, cio, flags));
23310f7643c7SGeorge Wilson 		}
23320f7643c7SGeorge Wilson 		zio_nowait(cio);
2333e14bb325SJeff Bonwick 	}
2334e05725b1Sbonwick 
233544cd46caSbillm 	/*
2336e14bb325SJeff Bonwick 	 * Set pio's pipeline to just wait for zio to finish.
233744cd46caSbillm 	 */
2338e14bb325SJeff Bonwick 	pio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2339e14bb325SJeff Bonwick 
2340e14bb325SJeff Bonwick 	zio_nowait(zio);
2341e14bb325SJeff Bonwick 
2342e14bb325SJeff Bonwick 	return (ZIO_PIPELINE_CONTINUE);
2343fa9e4066Sahrens }
2344fa9e4066Sahrens 
234580901aeaSGeorge Wilson /*
234645818ee1SMatthew Ahrens  * The zio_nop_write stage in the pipeline determines if allocating a
234745818ee1SMatthew Ahrens  * new bp is necessary.  The nopwrite feature can handle writes in
234845818ee1SMatthew Ahrens  * either syncing or open context (i.e. zil writes) and as a result is
234945818ee1SMatthew Ahrens  * mutually exclusive with dedup.
235045818ee1SMatthew Ahrens  *
235145818ee1SMatthew Ahrens  * By leveraging a cryptographically secure checksum, such as SHA256, we
235245818ee1SMatthew Ahrens  * can compare the checksums of the new data and the old to determine if
235345818ee1SMatthew Ahrens  * allocating a new block is required.  Note that our requirements for
235445818ee1SMatthew Ahrens  * cryptographic strength are fairly weak: there can't be any accidental
235545818ee1SMatthew Ahrens  * hash collisions, but we don't need to be secure against intentional
235645818ee1SMatthew Ahrens  * (malicious) collisions.  To trigger a nopwrite, you have to be able
235745818ee1SMatthew Ahrens  * to write the file to begin with, and triggering an incorrect (hash
235845818ee1SMatthew Ahrens  * collision) nopwrite is no worse than simply writing to the file.
235945818ee1SMatthew Ahrens  * That said, there are no known attacks against the checksum algorithms
236045818ee1SMatthew Ahrens  * used for nopwrite, assuming that the salt and the checksums
236145818ee1SMatthew Ahrens  * themselves remain secret.
236280901aeaSGeorge Wilson  */
236380901aeaSGeorge Wilson static int
236480901aeaSGeorge Wilson zio_nop_write(zio_t *zio)
236580901aeaSGeorge Wilson {
236680901aeaSGeorge Wilson 	blkptr_t *bp = zio->io_bp;
236780901aeaSGeorge Wilson 	blkptr_t *bp_orig = &zio->io_bp_orig;
236880901aeaSGeorge Wilson 	zio_prop_t *zp = &zio->io_prop;
236980901aeaSGeorge Wilson 
237080901aeaSGeorge Wilson 	ASSERT(BP_GET_LEVEL(bp) == 0);
237180901aeaSGeorge Wilson 	ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
237280901aeaSGeorge Wilson 	ASSERT(zp->zp_nopwrite);
237380901aeaSGeorge Wilson 	ASSERT(!zp->zp_dedup);
237480901aeaSGeorge Wilson 	ASSERT(zio->io_bp_override == NULL);
237580901aeaSGeorge Wilson 	ASSERT(IO_IS_ALLOCATING(zio));
237680901aeaSGeorge Wilson 
237780901aeaSGeorge Wilson 	/*
237880901aeaSGeorge Wilson 	 * Check to see if the original bp and the new bp have matching
237980901aeaSGeorge Wilson 	 * characteristics (i.e. same checksum, compression algorithms, etc).
238080901aeaSGeorge Wilson 	 * If they don't then just continue with the pipeline which will
238180901aeaSGeorge Wilson 	 * allocate a new bp.
238280901aeaSGeorge Wilson 	 */
238380901aeaSGeorge Wilson 	if (BP_IS_HOLE(bp_orig) ||
238445818ee1SMatthew Ahrens 	    !(zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_flags &
238545818ee1SMatthew Ahrens 	    ZCHECKSUM_FLAG_NOPWRITE) ||
238680901aeaSGeorge Wilson 	    BP_GET_CHECKSUM(bp) != BP_GET_CHECKSUM(bp_orig) ||
238780901aeaSGeorge Wilson 	    BP_GET_COMPRESS(bp) != BP_GET_COMPRESS(bp_orig) ||
238880901aeaSGeorge Wilson 	    BP_GET_DEDUP(bp) != BP_GET_DEDUP(bp_orig) ||
238980901aeaSGeorge Wilson 	    zp->zp_copies != BP_GET_NDVAS(bp_orig))
239080901aeaSGeorge Wilson 		return (ZIO_PIPELINE_CONTINUE);
239180901aeaSGeorge Wilson 
239280901aeaSGeorge Wilson 	/*
239380901aeaSGeorge Wilson 	 * If the checksums match then reset the pipeline so that we
239480901aeaSGeorge Wilson 	 * avoid allocating a new bp and issuing any I/O.
239580901aeaSGeorge Wilson 	 */
239680901aeaSGeorge Wilson 	if (ZIO_CHECKSUM_EQUAL(bp->blk_cksum, bp_orig->blk_cksum)) {
239745818ee1SMatthew Ahrens 		ASSERT(zio_checksum_table[zp->zp_checksum].ci_flags &
239845818ee1SMatthew Ahrens 		    ZCHECKSUM_FLAG_NOPWRITE);
239980901aeaSGeorge Wilson 		ASSERT3U(BP_GET_PSIZE(bp), ==, BP_GET_PSIZE(bp_orig));
240080901aeaSGeorge Wilson 		ASSERT3U(BP_GET_LSIZE(bp), ==, BP_GET_LSIZE(bp_orig));
240180901aeaSGeorge Wilson 		ASSERT(zp->zp_compress != ZIO_COMPRESS_OFF);
240280901aeaSGeorge Wilson 		ASSERT(bcmp(&bp->blk_prop, &bp_orig->blk_prop,
240380901aeaSGeorge Wilson 		    sizeof (uint64_t)) == 0);
240480901aeaSGeorge Wilson 
240580901aeaSGeorge Wilson 		*bp = *bp_orig;
240680901aeaSGeorge Wilson 		zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
240780901aeaSGeorge Wilson 		zio->io_flags |= ZIO_FLAG_NOPWRITE;
240880901aeaSGeorge Wilson 	}
240980901aeaSGeorge Wilson 
241080901aeaSGeorge Wilson 	return (ZIO_PIPELINE_CONTINUE);
241180901aeaSGeorge Wilson }
241280901aeaSGeorge Wilson 
2413fa9e4066Sahrens /*
2414fa9e4066Sahrens  * ==========================================================================
2415b24ab676SJeff Bonwick  * Dedup
2416fa9e4066Sahrens  * ==========================================================================
2417fa9e4066Sahrens  */
2418b24ab676SJeff Bonwick static void
2419b24ab676SJeff Bonwick zio_ddt_child_read_done(zio_t *zio)
2420b24ab676SJeff Bonwick {
2421b24ab676SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
2422b24ab676SJeff Bonwick 	ddt_entry_t *dde = zio->io_private;
2423b24ab676SJeff Bonwick 	ddt_phys_t *ddp;
2424b24ab676SJeff Bonwick 	zio_t *pio = zio_unique_parent(zio);
2425b24ab676SJeff Bonwick 
2426b24ab676SJeff Bonwick 	mutex_enter(&pio->io_lock);
2427b24ab676SJeff Bonwick 	ddp = ddt_phys_select(dde, bp);
2428b24ab676SJeff Bonwick 	if (zio->io_error == 0)
2429b24ab676SJeff Bonwick 		ddt_phys_clear(ddp);	/* this ddp doesn't need repair */
2430770499e1SDan Kimmel 
2431770499e1SDan Kimmel 	if (zio->io_error == 0 && dde->dde_repair_abd == NULL)
2432770499e1SDan Kimmel 		dde->dde_repair_abd = zio->io_abd;
2433b24ab676SJeff Bonwick 	else
2434770499e1SDan Kimmel 		abd_free(zio->io_abd);
2435b24ab676SJeff Bonwick 	mutex_exit(&pio->io_lock);
2436b24ab676SJeff Bonwick }
2437b24ab676SJeff Bonwick 
2438b24ab676SJeff Bonwick static int
2439b24ab676SJeff Bonwick zio_ddt_read_start(zio_t *zio)
2440b24ab676SJeff Bonwick {
2441b24ab676SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
2442b24ab676SJeff Bonwick 
2443b24ab676SJeff Bonwick 	ASSERT(BP_GET_DEDUP(bp));
2444b24ab676SJeff Bonwick 	ASSERT(BP_GET_PSIZE(bp) == zio->io_size);
2445b24ab676SJeff Bonwick 	ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2446b24ab676SJeff Bonwick 
2447b24ab676SJeff Bonwick 	if (zio->io_child_error[ZIO_CHILD_DDT]) {
2448b24ab676SJeff Bonwick 		ddt_t *ddt = ddt_select(zio->io_spa, bp);
2449b24ab676SJeff Bonwick 		ddt_entry_t *dde = ddt_repair_start(ddt, bp);
2450b24ab676SJeff Bonwick 		ddt_phys_t *ddp = dde->dde_phys;
2451b24ab676SJeff Bonwick 		ddt_phys_t *ddp_self = ddt_phys_select(dde, bp);
2452b24ab676SJeff Bonwick 		blkptr_t blk;
2453b24ab676SJeff Bonwick 
2454b24ab676SJeff Bonwick 		ASSERT(zio->io_vsd == NULL);
2455b24ab676SJeff Bonwick 		zio->io_vsd = dde;
2456b24ab676SJeff Bonwick 
2457b24ab676SJeff Bonwick 		if (ddp_self == NULL)
2458b24ab676SJeff Bonwick 			return (ZIO_PIPELINE_CONTINUE);
2459b24ab676SJeff Bonwick 
2460b24ab676SJeff Bonwick 		for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
2461b24ab676SJeff Bonwick 			if (ddp->ddp_phys_birth == 0 || ddp == ddp_self)
2462b24ab676SJeff Bonwick 				continue;
2463bbfd46c4SJeff Bonwick 			ddt_bp_create(ddt->ddt_checksum, &dde->dde_key, ddp,
2464bbfd46c4SJeff Bonwick 			    &blk);
2465b24ab676SJeff Bonwick 			zio_nowait(zio_read(zio, zio->io_spa, &blk,
2466770499e1SDan Kimmel 			    abd_alloc_for_io(zio->io_size, B_TRUE),
2467770499e1SDan Kimmel 			    zio->io_size, zio_ddt_child_read_done, dde,
2468770499e1SDan Kimmel 			    zio->io_priority, ZIO_DDT_CHILD_FLAGS(zio) |
2469770499e1SDan Kimmel 			    ZIO_FLAG_DONT_PROPAGATE, &zio->io_bookmark));
2470b24ab676SJeff Bonwick 		}
2471b24ab676SJeff Bonwick 		return (ZIO_PIPELINE_CONTINUE);
2472b24ab676SJeff Bonwick 	}
2473b24ab676SJeff Bonwick 
2474b24ab676SJeff Bonwick 	zio_nowait(zio_read(zio, zio->io_spa, bp,
2475770499e1SDan Kimmel 	    zio->io_abd, zio->io_size, NULL, NULL, zio->io_priority,
2476b24ab676SJeff Bonwick 	    ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark));
2477b24ab676SJeff Bonwick 
2478b24ab676SJeff Bonwick 	return (ZIO_PIPELINE_CONTINUE);
2479b24ab676SJeff Bonwick }
2480e14bb325SJeff Bonwick 
2481b24ab676SJeff Bonwick static int
2482b24ab676SJeff Bonwick zio_ddt_read_done(zio_t *zio)
2483b24ab676SJeff Bonwick {
2484b24ab676SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
2485b24ab676SJeff Bonwick 
2486d6e1c446SGeorge Wilson 	if (zio_wait_for_children(zio, ZIO_CHILD_DDT_BIT, ZIO_WAIT_DONE)) {
2487b24ab676SJeff Bonwick 		return (ZIO_PIPELINE_STOP);
2488d6e1c446SGeorge Wilson 	}
2489b24ab676SJeff Bonwick 
2490b24ab676SJeff Bonwick 	ASSERT(BP_GET_DEDUP(bp));
2491b24ab676SJeff Bonwick 	ASSERT(BP_GET_PSIZE(bp) == zio->io_size);
2492b24ab676SJeff Bonwick 	ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2493b24ab676SJeff Bonwick 
2494b24ab676SJeff Bonwick 	if (zio->io_child_error[ZIO_CHILD_DDT]) {
2495b24ab676SJeff Bonwick 		ddt_t *ddt = ddt_select(zio->io_spa, bp);
2496b24ab676SJeff Bonwick 		ddt_entry_t *dde = zio->io_vsd;
2497b24ab676SJeff Bonwick 		if (ddt == NULL) {
2498b16da2e2SGeorge Wilson 			ASSERT(spa_load_state(zio->io_spa) != SPA_LOAD_NONE);
2499b24ab676SJeff Bonwick 			return (ZIO_PIPELINE_CONTINUE);
2500b24ab676SJeff Bonwick 		}
2501b24ab676SJeff Bonwick 		if (dde == NULL) {
2502b24ab676SJeff Bonwick 			zio->io_stage = ZIO_STAGE_DDT_READ_START >> 1;
250335a5a358SJonathan Adams 			zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE);
2504b24ab676SJeff Bonwick 			return (ZIO_PIPELINE_STOP);
2505b24ab676SJeff Bonwick 		}
2506770499e1SDan Kimmel 		if (dde->dde_repair_abd != NULL) {
2507770499e1SDan Kimmel 			abd_copy(zio->io_abd, dde->dde_repair_abd,
2508770499e1SDan Kimmel 			    zio->io_size);
2509b24ab676SJeff Bonwick 			zio->io_child_error[ZIO_CHILD_DDT] = 0;
2510b24ab676SJeff Bonwick 		}
2511b24ab676SJeff Bonwick 		ddt_repair_done(ddt, dde);
2512b24ab676SJeff Bonwick 		zio->io_vsd = NULL;
2513b24ab676SJeff Bonwick 	}
2514b24ab676SJeff Bonwick 
2515b24ab676SJeff Bonwick 	ASSERT(zio->io_vsd == NULL);
2516b24ab676SJeff Bonwick 
2517b24ab676SJeff Bonwick 	return (ZIO_PIPELINE_CONTINUE);
2518b24ab676SJeff Bonwick }
2519b24ab676SJeff Bonwick 
2520b24ab676SJeff Bonwick static boolean_t
2521b24ab676SJeff Bonwick zio_ddt_collision(zio_t *zio, ddt_t *ddt, ddt_entry_t *dde)
2522b24ab676SJeff Bonwick {
2523b24ab676SJeff Bonwick 	spa_t *spa = zio->io_spa;
25245602294fSDan Kimmel 	boolean_t do_raw = (zio->io_flags & ZIO_FLAG_RAW);
25255602294fSDan Kimmel 
25265602294fSDan Kimmel 	/* We should never get a raw, override zio */
25275602294fSDan Kimmel 	ASSERT(!(zio->io_bp_override && do_raw));
2528b24ab676SJeff Bonwick 
2529b24ab676SJeff Bonwick 	/*
2530b24ab676SJeff Bonwick 	 * Note: we compare the original data, not the transformed data,
2531b24ab676SJeff Bonwick 	 * because when zio->io_bp is an override bp, we will not have
2532b24ab676SJeff Bonwick 	 * pushed the I/O transforms.  That's an important optimization
2533b24ab676SJeff Bonwick 	 * because otherwise we'd compress/encrypt all dmu_sync() data twice.
2534b24ab676SJeff Bonwick 	 */
2535b24ab676SJeff Bonwick 	for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) {
2536b24ab676SJeff Bonwick 		zio_t *lio = dde->dde_lead_zio[p];
2537b24ab676SJeff Bonwick 
2538b24ab676SJeff Bonwick 		if (lio != NULL) {
2539b24ab676SJeff Bonwick 			return (lio->io_orig_size != zio->io_orig_size ||
2540770499e1SDan Kimmel 			    abd_cmp(zio->io_orig_abd, lio->io_orig_abd,
2541b24ab676SJeff Bonwick 			    zio->io_orig_size) != 0);
2542b24ab676SJeff Bonwick 		}
2543b24ab676SJeff Bonwick 	}
2544b24ab676SJeff Bonwick 
2545b24ab676SJeff Bonwick 	for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) {
2546b24ab676SJeff Bonwick 		ddt_phys_t *ddp = &dde->dde_phys[p];
2547b24ab676SJeff Bonwick 
2548b24ab676SJeff Bonwick 		if (ddp->ddp_phys_birth != 0) {
2549b24ab676SJeff Bonwick 			arc_buf_t *abuf = NULL;
25507adb730bSGeorge Wilson 			arc_flags_t aflags = ARC_FLAG_WAIT;
25515602294fSDan Kimmel 			int zio_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE;
2552b24ab676SJeff Bonwick 			blkptr_t blk = *zio->io_bp;
2553b24ab676SJeff Bonwick 			int error;
2554b24ab676SJeff Bonwick 
2555b24ab676SJeff Bonwick 			ddt_bp_fill(ddp, &blk, ddp->ddp_phys_birth);
2556b24ab676SJeff Bonwick 
2557b24ab676SJeff Bonwick 			ddt_exit(ddt);
2558b24ab676SJeff Bonwick 
25595602294fSDan Kimmel 			/*
25605602294fSDan Kimmel 			 * Intuitively, it would make more sense to compare
2561770499e1SDan Kimmel 			 * io_abd than io_orig_abd in the raw case since you
25625602294fSDan Kimmel 			 * don't want to look at any transformations that have
25635602294fSDan Kimmel 			 * happened to the data. However, for raw I/Os the
2564770499e1SDan Kimmel 			 * data will actually be the same in io_abd and
2565770499e1SDan Kimmel 			 * io_orig_abd, so all we have to do is issue this as
25665602294fSDan Kimmel 			 * a raw ARC read.
25675602294fSDan Kimmel 			 */
25685602294fSDan Kimmel 			if (do_raw) {
25695602294fSDan Kimmel 				zio_flags |= ZIO_FLAG_RAW;
25705602294fSDan Kimmel 				ASSERT3U(zio->io_size, ==, zio->io_orig_size);
2571770499e1SDan Kimmel 				ASSERT0(abd_cmp(zio->io_abd, zio->io_orig_abd,
25725602294fSDan Kimmel 				    zio->io_size));
25735602294fSDan Kimmel 				ASSERT3P(zio->io_transform_stack, ==, NULL);
25745602294fSDan Kimmel 			}
25755602294fSDan Kimmel 
25761b912ec7SGeorge Wilson 			error = arc_read(NULL, spa, &blk,
2577b24ab676SJeff Bonwick 			    arc_getbuf_func, &abuf, ZIO_PRIORITY_SYNC_READ,
25785602294fSDan Kimmel 			    zio_flags, &aflags, &zio->io_bookmark);
2579b24ab676SJeff Bonwick 
2580b24ab676SJeff Bonwick 			if (error == 0) {
2581b24ab676SJeff Bonwick 				if (arc_buf_size(abuf) != zio->io_orig_size ||
2582770499e1SDan Kimmel 				    abd_cmp_buf(zio->io_orig_abd, abuf->b_data,
2583b24ab676SJeff Bonwick 				    zio->io_orig_size) != 0)
2584be6fd75aSMatthew Ahrens 					error = SET_ERROR(EEXIST);
2585dcbf3bd6SGeorge Wilson 				arc_buf_destroy(abuf, &abuf);
2586b24ab676SJeff Bonwick 			}
2587b24ab676SJeff Bonwick 
2588b24ab676SJeff Bonwick 			ddt_enter(ddt);
2589b24ab676SJeff Bonwick 			return (error != 0);
2590b24ab676SJeff Bonwick 		}
2591b24ab676SJeff Bonwick 	}
2592b24ab676SJeff Bonwick 
2593b24ab676SJeff Bonwick 	return (B_FALSE);
2594b24ab676SJeff Bonwick }
2595b24ab676SJeff Bonwick 
2596b24ab676SJeff Bonwick static void
2597b24ab676SJeff Bonwick zio_ddt_child_write_ready(zio_t *zio)
2598b24ab676SJeff Bonwick {
2599b24ab676SJeff Bonwick 	int p = zio->io_prop.zp_copies;
2600b24ab676SJeff Bonwick 	ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp);
2601b24ab676SJeff Bonwick 	ddt_entry_t *dde = zio->io_private;
2602b24ab676SJeff Bonwick 	ddt_phys_t *ddp = &dde->dde_phys[p];
2603b24ab676SJeff Bonwick 	zio_t *pio;
2604b24ab676SJeff Bonwick 
2605b24ab676SJeff Bonwick 	if (zio->io_error)
2606b24ab676SJeff Bonwick 		return;
2607b24ab676SJeff Bonwick 
2608b24ab676SJeff Bonwick 	ddt_enter(ddt);
2609b24ab676SJeff Bonwick 
2610b24ab676SJeff Bonwick 	ASSERT(dde->dde_lead_zio[p] == zio);
2611b24ab676SJeff Bonwick 
2612b24ab676SJeff Bonwick 	ddt_phys_fill(ddp, zio->io_bp);
2613b24ab676SJeff Bonwick 
26140f7643c7SGeorge Wilson 	zio_link_t *zl = NULL;
26150f7643c7SGeorge Wilson 	while ((pio = zio_walk_parents(zio, &zl)) != NULL)
2616b24ab676SJeff Bonwick 		ddt_bp_fill(ddp, pio->io_bp, zio->io_txg);
2617b24ab676SJeff Bonwick 
2618b24ab676SJeff Bonwick 	ddt_exit(ddt);
2619b24ab676SJeff Bonwick }
2620b24ab676SJeff Bonwick 
2621b24ab676SJeff Bonwick static void
2622b24ab676SJeff Bonwick zio_ddt_child_write_done(zio_t *zio)
2623b24ab676SJeff Bonwick {
2624b24ab676SJeff Bonwick 	int p = zio->io_prop.zp_copies;
2625b24ab676SJeff Bonwick 	ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp);
2626b24ab676SJeff Bonwick 	ddt_entry_t *dde = zio->io_private;
2627b24ab676SJeff Bonwick 	ddt_phys_t *ddp = &dde->dde_phys[p];
2628b24ab676SJeff Bonwick 
2629b24ab676SJeff Bonwick 	ddt_enter(ddt);
2630b24ab676SJeff Bonwick 
2631b24ab676SJeff Bonwick 	ASSERT(ddp->ddp_refcnt == 0);
2632b24ab676SJeff Bonwick 	ASSERT(dde->dde_lead_zio[p] == zio);
2633b24ab676SJeff Bonwick 	dde->dde_lead_zio[p] = NULL;
2634b24ab676SJeff Bonwick 
2635b24ab676SJeff Bonwick 	if (zio->io_error == 0) {
26360f7643c7SGeorge Wilson 		zio_link_t *zl = NULL;
26370f7643c7SGeorge Wilson 		while (zio_walk_parents(zio, &zl) != NULL)
2638b24ab676SJeff Bonwick 			ddt_phys_addref(ddp);
2639b24ab676SJeff Bonwick 	} else {
2640b24ab676SJeff Bonwick 		ddt_phys_clear(ddp);
2641b24ab676SJeff Bonwick 	}
2642b24ab676SJeff Bonwick 
2643b24ab676SJeff Bonwick 	ddt_exit(ddt);
2644b24ab676SJeff Bonwick }
2645b24ab676SJeff Bonwick 
2646b24ab676SJeff Bonwick static void
2647b24ab676SJeff Bonwick zio_ddt_ditto_write_done(zio_t *zio)
2648b24ab676SJeff Bonwick {
2649b24ab676SJeff Bonwick 	int p = DDT_PHYS_DITTO;
2650b24ab676SJeff Bonwick 	zio_prop_t *zp = &zio->io_prop;
2651b24ab676SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
2652b24ab676SJeff Bonwick 	ddt_t *ddt = ddt_select(zio->io_spa, bp);
2653b24ab676SJeff Bonwick 	ddt_entry_t *dde = zio->io_private;
2654b24ab676SJeff Bonwick 	ddt_phys_t *ddp = &dde->dde_phys[p];
2655b24ab676SJeff Bonwick 	ddt_key_t *ddk = &dde->dde_key;
2656b24ab676SJeff Bonwick 
2657b24ab676SJeff Bonwick 	ddt_enter(ddt);
2658b24ab676SJeff Bonwick 
2659b24ab676SJeff Bonwick 	ASSERT(ddp->ddp_refcnt == 0);
2660b24ab676SJeff Bonwick 	ASSERT(dde->dde_lead_zio[p] == zio);
2661b24ab676SJeff Bonwick 	dde->dde_lead_zio[p] = NULL;
2662b24ab676SJeff Bonwick 
2663b24ab676SJeff Bonwick 	if (zio->io_error == 0) {
2664b24ab676SJeff Bonwick 		ASSERT(ZIO_CHECKSUM_EQUAL(bp->blk_cksum, ddk->ddk_cksum));
2665b24ab676SJeff Bonwick 		ASSERT(zp->zp_copies < SPA_DVAS_PER_BP);
2666b24ab676SJeff Bonwick 		ASSERT(zp->zp_copies == BP_GET_NDVAS(bp) - BP_IS_GANG(bp));
2667b24ab676SJeff Bonwick 		if (ddp->ddp_phys_birth != 0)
2668b24ab676SJeff Bonwick 			ddt_phys_free(ddt, ddk, ddp, zio->io_txg);
2669b24ab676SJeff Bonwick 		ddt_phys_fill(ddp, bp);
2670b24ab676SJeff Bonwick 	}
2671b24ab676SJeff Bonwick 
2672b24ab676SJeff Bonwick 	ddt_exit(ddt);
2673b24ab676SJeff Bonwick }
2674b24ab676SJeff Bonwick 
2675b24ab676SJeff Bonwick static int
2676b24ab676SJeff Bonwick zio_ddt_write(zio_t *zio)
2677b24ab676SJeff Bonwick {
2678b24ab676SJeff Bonwick 	spa_t *spa = zio->io_spa;
2679b24ab676SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
2680b24ab676SJeff Bonwick 	uint64_t txg = zio->io_txg;
2681b24ab676SJeff Bonwick 	zio_prop_t *zp = &zio->io_prop;
2682b24ab676SJeff Bonwick 	int p = zp->zp_copies;
2683b24ab676SJeff Bonwick 	int ditto_copies;
2684b24ab676SJeff Bonwick 	zio_t *cio = NULL;
2685b24ab676SJeff Bonwick 	zio_t *dio = NULL;
2686b24ab676SJeff Bonwick 	ddt_t *ddt = ddt_select(spa, bp);
2687b24ab676SJeff Bonwick 	ddt_entry_t *dde;
2688b24ab676SJeff Bonwick 	ddt_phys_t *ddp;
2689b24ab676SJeff Bonwick 
2690b24ab676SJeff Bonwick 	ASSERT(BP_GET_DEDUP(bp));
2691b24ab676SJeff Bonwick 	ASSERT(BP_GET_CHECKSUM(bp) == zp->zp_checksum);
2692b24ab676SJeff Bonwick 	ASSERT(BP_IS_HOLE(bp) || zio->io_bp_override);
26935602294fSDan Kimmel 	ASSERT(!(zio->io_bp_override && (zio->io_flags & ZIO_FLAG_RAW)));
2694b24ab676SJeff Bonwick 
2695b24ab676SJeff Bonwick 	ddt_enter(ddt);
2696b24ab676SJeff Bonwick 	dde = ddt_lookup(ddt, bp, B_TRUE);
2697b24ab676SJeff Bonwick 	ddp = &dde->dde_phys[p];
2698b24ab676SJeff Bonwick 
2699b24ab676SJeff Bonwick 	if (zp->zp_dedup_verify && zio_ddt_collision(zio, ddt, dde)) {
2700b24ab676SJeff Bonwick 		/*
2701b24ab676SJeff Bonwick 		 * If we're using a weak checksum, upgrade to a strong checksum
2702b24ab676SJeff Bonwick 		 * and try again.  If we're already using a strong checksum,
2703b24ab676SJeff Bonwick 		 * we can't resolve it, so just convert to an ordinary write.
2704b24ab676SJeff Bonwick 		 * (And automatically e-mail a paper to Nature?)
2705b24ab676SJeff Bonwick 		 */
270645818ee1SMatthew Ahrens 		if (!(zio_checksum_table[zp->zp_checksum].ci_flags &
270745818ee1SMatthew Ahrens 		    ZCHECKSUM_FLAG_DEDUP)) {
2708b24ab676SJeff Bonwick 			zp->zp_checksum = spa_dedup_checksum(spa);
2709b24ab676SJeff Bonwick 			zio_pop_transforms(zio);
2710b24ab676SJeff Bonwick 			zio->io_stage = ZIO_STAGE_OPEN;
2711b24ab676SJeff Bonwick 			BP_ZERO(bp);
2712b24ab676SJeff Bonwick 		} else {
271380901aeaSGeorge Wilson 			zp->zp_dedup = B_FALSE;
27145602294fSDan Kimmel 			BP_SET_DEDUP(bp, B_FALSE);
2715b24ab676SJeff Bonwick 		}
27165602294fSDan Kimmel 		ASSERT(!BP_GET_DEDUP(bp));
2717b24ab676SJeff Bonwick 		zio->io_pipeline = ZIO_WRITE_PIPELINE;
2718b24ab676SJeff Bonwick 		ddt_exit(ddt);
2719b24ab676SJeff Bonwick 		return (ZIO_PIPELINE_CONTINUE);
2720b24ab676SJeff Bonwick 	}
2721b24ab676SJeff Bonwick 
2722b24ab676SJeff Bonwick 	ditto_copies = ddt_ditto_copies_needed(ddt, dde, ddp);
2723b24ab676SJeff Bonwick 	ASSERT(ditto_copies < SPA_DVAS_PER_BP);
2724b24ab676SJeff Bonwick 
2725b24ab676SJeff Bonwick 	if (ditto_copies > ddt_ditto_copies_present(dde) &&
2726b24ab676SJeff Bonwick 	    dde->dde_lead_zio[DDT_PHYS_DITTO] == NULL) {
2727b24ab676SJeff Bonwick 		zio_prop_t czp = *zp;
2728b24ab676SJeff Bonwick 
2729b24ab676SJeff Bonwick 		czp.zp_copies = ditto_copies;
2730b24ab676SJeff Bonwick 
2731b24ab676SJeff Bonwick 		/*
2732b24ab676SJeff Bonwick 		 * If we arrived here with an override bp, we won't have run
2733b24ab676SJeff Bonwick 		 * the transform stack, so we won't have the data we need to
2734b24ab676SJeff Bonwick 		 * generate a child i/o.  So, toss the override bp and restart.
2735b24ab676SJeff Bonwick 		 * This is safe, because using the override bp is just an
2736b24ab676SJeff Bonwick 		 * optimization; and it's rare, so the cost doesn't matter.
2737b24ab676SJeff Bonwick 		 */
2738b24ab676SJeff Bonwick 		if (zio->io_bp_override) {
2739b24ab676SJeff Bonwick 			zio_pop_transforms(zio);
2740b24ab676SJeff Bonwick 			zio->io_stage = ZIO_STAGE_OPEN;
2741b24ab676SJeff Bonwick 			zio->io_pipeline = ZIO_WRITE_PIPELINE;
2742b24ab676SJeff Bonwick 			zio->io_bp_override = NULL;
2743b24ab676SJeff Bonwick 			BP_ZERO(bp);
2744b24ab676SJeff Bonwick 			ddt_exit(ddt);
2745b24ab676SJeff Bonwick 			return (ZIO_PIPELINE_CONTINUE);
2746b24ab676SJeff Bonwick 		}
2747b24ab676SJeff Bonwick 
2748770499e1SDan Kimmel 		dio = zio_write(zio, spa, txg, bp, zio->io_orig_abd,
27495602294fSDan Kimmel 		    zio->io_orig_size, zio->io_orig_size, &czp, NULL, NULL,
27508df0bcf0SPaul Dagnelie 		    NULL, zio_ddt_ditto_write_done, dde, zio->io_priority,
2751b24ab676SJeff Bonwick 		    ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark);
2752b24ab676SJeff Bonwick 
2753770499e1SDan Kimmel 		zio_push_transform(dio, zio->io_abd, zio->io_size, 0, NULL);
2754b24ab676SJeff Bonwick 		dde->dde_lead_zio[DDT_PHYS_DITTO] = dio;
2755b24ab676SJeff Bonwick 	}
2756b24ab676SJeff Bonwick 
2757b24ab676SJeff Bonwick 	if (ddp->ddp_phys_birth != 0 || dde->dde_lead_zio[p] != NULL) {
2758b24ab676SJeff Bonwick 		if (ddp->ddp_phys_birth != 0)
2759b24ab676SJeff Bonwick 			ddt_bp_fill(ddp, bp, txg);
2760b24ab676SJeff Bonwick 		if (dde->dde_lead_zio[p] != NULL)
2761b24ab676SJeff Bonwick 			zio_add_child(zio, dde->dde_lead_zio[p]);
2762b24ab676SJeff Bonwick 		else
2763b24ab676SJeff Bonwick 			ddt_phys_addref(ddp);
2764b24ab676SJeff Bonwick 	} else if (zio->io_bp_override) {
2765b24ab676SJeff Bonwick 		ASSERT(bp->blk_birth == txg);
2766b24ab676SJeff Bonwick 		ASSERT(BP_EQUAL(bp, zio->io_bp_override));
2767b24ab676SJeff Bonwick 		ddt_phys_fill(ddp, bp);
2768b24ab676SJeff Bonwick 		ddt_phys_addref(ddp);
2769b24ab676SJeff Bonwick 	} else {
2770770499e1SDan Kimmel 		cio = zio_write(zio, spa, txg, bp, zio->io_orig_abd,
27715602294fSDan Kimmel 		    zio->io_orig_size, zio->io_orig_size, zp,
27728df0bcf0SPaul Dagnelie 		    zio_ddt_child_write_ready, NULL, NULL,
2773b24ab676SJeff Bonwick 		    zio_ddt_child_write_done, dde, zio->io_priority,
2774b24ab676SJeff Bonwick 		    ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark);
2775b24ab676SJeff Bonwick 
2776770499e1SDan Kimmel 		zio_push_transform(cio, zio->io_abd, zio->io_size, 0, NULL);
2777b24ab676SJeff Bonwick 		dde->dde_lead_zio[p] = cio;
2778b24ab676SJeff Bonwick 	}
2779b24ab676SJeff Bonwick 
2780b24ab676SJeff Bonwick 	ddt_exit(ddt);
2781b24ab676SJeff Bonwick 
2782b24ab676SJeff Bonwick 	if (cio)
2783b24ab676SJeff Bonwick 		zio_nowait(cio);
2784b24ab676SJeff Bonwick 	if (dio)
2785b24ab676SJeff Bonwick 		zio_nowait(dio);
2786b24ab676SJeff Bonwick 
2787b24ab676SJeff Bonwick 	return (ZIO_PIPELINE_CONTINUE);
2788b24ab676SJeff Bonwick }
2789b24ab676SJeff Bonwick 
27903f9d6ad7SLin Ling ddt_entry_t *freedde; /* for debugging */
27913f9d6ad7SLin Ling 
2792b24ab676SJeff Bonwick static int
2793b24ab676SJeff Bonwick zio_ddt_free(zio_t *zio)
2794b24ab676SJeff Bonwick {
2795b24ab676SJeff Bonwick 	spa_t *spa = zio->io_spa;
2796b24ab676SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
2797b24ab676SJeff Bonwick 	ddt_t *ddt = ddt_select(spa, bp);
2798b24ab676SJeff Bonwick 	ddt_entry_t *dde;
2799b24ab676SJeff Bonwick 	ddt_phys_t *ddp;
2800b24ab676SJeff Bonwick 
2801b24ab676SJeff Bonwick 	ASSERT(BP_GET_DEDUP(bp));
2802b24ab676SJeff Bonwick 	ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2803b24ab676SJeff Bonwick 
2804b24ab676SJeff Bonwick 	ddt_enter(ddt);
28053f9d6ad7SLin Ling 	freedde = dde = ddt_lookup(ddt, bp, B_TRUE);
2806b24ab676SJeff Bonwick 	ddp = ddt_phys_select(dde, bp);
2807b24ab676SJeff Bonwick 	ddt_phys_decref(ddp);
2808b24ab676SJeff Bonwick 	ddt_exit(ddt);
2809b24ab676SJeff Bonwick 
2810b24ab676SJeff Bonwick 	return (ZIO_PIPELINE_CONTINUE);
2811b24ab676SJeff Bonwick }
2812b24ab676SJeff Bonwick 
2813b24ab676SJeff Bonwick /*
2814b24ab676SJeff Bonwick  * ==========================================================================
2815b24ab676SJeff Bonwick  * Allocate and free blocks
2816b24ab676SJeff Bonwick  * ==========================================================================
2817b24ab676SJeff Bonwick  */
28180f7643c7SGeorge Wilson 
28190f7643c7SGeorge Wilson static zio_t *
2820f78cdc34SPaul Dagnelie zio_io_to_allocate(spa_t *spa, int allocator)
28210f7643c7SGeorge Wilson {
28220f7643c7SGeorge Wilson 	zio_t *zio;
28230f7643c7SGeorge Wilson 
2824f78cdc34SPaul Dagnelie 	ASSERT(MUTEX_HELD(&spa->spa_alloc_locks[allocator]));
28250f7643c7SGeorge Wilson 
2826f78cdc34SPaul Dagnelie 	zio = avl_first(&spa->spa_alloc_trees[allocator]);
28270f7643c7SGeorge Wilson 	if (zio == NULL)
28280f7643c7SGeorge Wilson 		return (NULL);
28290f7643c7SGeorge Wilson 
28300f7643c7SGeorge Wilson 	ASSERT(IO_IS_ALLOCATING(zio));
28310f7643c7SGeorge Wilson 
28320f7643c7SGeorge Wilson 	/*
28330f7643c7SGeorge Wilson 	 * Try to place a reservation for this zio. If we're unable to
28340f7643c7SGeorge Wilson 	 * reserve then we throttle.
28350f7643c7SGeorge Wilson 	 */
2836f78cdc34SPaul Dagnelie 	ASSERT3U(zio->io_allocator, ==, allocator);
2837663207adSDon Brady 	if (!metaslab_class_throttle_reserve(zio->io_metaslab_class,
2838f78cdc34SPaul Dagnelie 	    zio->io_prop.zp_copies, zio->io_allocator, zio, 0)) {
28390f7643c7SGeorge Wilson 		return (NULL);
28400f7643c7SGeorge Wilson 	}
28410f7643c7SGeorge Wilson 
2842f78cdc34SPaul Dagnelie 	avl_remove(&spa->spa_alloc_trees[allocator], zio);
28430f7643c7SGeorge Wilson 	ASSERT3U(zio->io_stage, <, ZIO_STAGE_DVA_ALLOCATE);
28440f7643c7SGeorge Wilson 
28450f7643c7SGeorge Wilson 	return (zio);
28460f7643c7SGeorge Wilson }
28470f7643c7SGeorge Wilson 
28480f7643c7SGeorge Wilson static int
28490f7643c7SGeorge Wilson zio_dva_throttle(zio_t *zio)
28500f7643c7SGeorge Wilson {
28510f7643c7SGeorge Wilson 	spa_t *spa = zio->io_spa;
28520f7643c7SGeorge Wilson 	zio_t *nio;
2853663207adSDon Brady 	metaslab_class_t *mc;
2854663207adSDon Brady 
2855663207adSDon Brady 	/* locate an appropriate allocation class */
2856663207adSDon Brady 	mc = spa_preferred_class(spa, zio->io_size, zio->io_prop.zp_type,
2857663207adSDon Brady 	    zio->io_prop.zp_level, zio->io_prop.zp_zpl_smallblk);
28580f7643c7SGeorge Wilson 
28590f7643c7SGeorge Wilson 	if (zio->io_priority == ZIO_PRIORITY_SYNC_WRITE ||
2860663207adSDon Brady 	    !mc->mc_alloc_throttle_enabled ||
28610f7643c7SGeorge Wilson 	    zio->io_child_type == ZIO_CHILD_GANG ||
28620f7643c7SGeorge Wilson 	    zio->io_flags & ZIO_FLAG_NODATA) {
28630f7643c7SGeorge Wilson 		return (ZIO_PIPELINE_CONTINUE);
28640f7643c7SGeorge Wilson 	}
28650f7643c7SGeorge Wilson 
28660f7643c7SGeorge Wilson 	ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
28670f7643c7SGeorge Wilson 
28680f7643c7SGeorge Wilson 	ASSERT3U(zio->io_queued_timestamp, >, 0);
28690f7643c7SGeorge Wilson 	ASSERT(zio->io_stage == ZIO_STAGE_DVA_THROTTLE);
28700f7643c7SGeorge Wilson 
2871f78cdc34SPaul Dagnelie 	zbookmark_phys_t *bm = &zio->io_bookmark;
2872f78cdc34SPaul Dagnelie 	/*
2873f78cdc34SPaul Dagnelie 	 * We want to try to use as many allocators as possible to help improve
2874f78cdc34SPaul Dagnelie 	 * performance, but we also want logically adjacent IOs to be physically
2875f78cdc34SPaul Dagnelie 	 * adjacent to improve sequential read performance. We chunk each object
2876f78cdc34SPaul Dagnelie 	 * into 2^20 block regions, and then hash based on the objset, object,
2877f78cdc34SPaul Dagnelie 	 * level, and region to accomplish both of these goals.
2878f78cdc34SPaul Dagnelie 	 */
2879f78cdc34SPaul Dagnelie 	zio->io_allocator = cityhash4(bm->zb_objset, bm->zb_object,
2880f78cdc34SPaul Dagnelie 	    bm->zb_level, bm->zb_blkid >> 20) % spa->spa_alloc_count;
2881f78cdc34SPaul Dagnelie 	mutex_enter(&spa->spa_alloc_locks[zio->io_allocator]);
28820f7643c7SGeorge Wilson 	ASSERT(zio->io_type == ZIO_TYPE_WRITE);
2883663207adSDon Brady 	zio->io_metaslab_class = mc;
2884f78cdc34SPaul Dagnelie 	avl_add(&spa->spa_alloc_trees[zio->io_allocator], zio);
2885663207adSDon Brady 	nio = zio_io_to_allocate(spa, zio->io_allocator);
2886f78cdc34SPaul Dagnelie 	mutex_exit(&spa->spa_alloc_locks[zio->io_allocator]);
28870f7643c7SGeorge Wilson 
28880f7643c7SGeorge Wilson 	if (nio == zio)
28890f7643c7SGeorge Wilson 		return (ZIO_PIPELINE_CONTINUE);
28900f7643c7SGeorge Wilson 
28910f7643c7SGeorge Wilson 	if (nio != NULL) {
28920f7643c7SGeorge Wilson 		ASSERT(nio->io_stage == ZIO_STAGE_DVA_THROTTLE);
28930f7643c7SGeorge Wilson 		/*
28940f7643c7SGeorge Wilson 		 * We are passing control to a new zio so make sure that
28950f7643c7SGeorge Wilson 		 * it is processed by a different thread. We do this to
28960f7643c7SGeorge Wilson 		 * avoid stack overflows that can occur when parents are
28970f7643c7SGeorge Wilson 		 * throttled and children are making progress. We allow
28980f7643c7SGeorge Wilson 		 * it to go to the head of the taskq since it's already
28990f7643c7SGeorge Wilson 		 * been waiting.
29000f7643c7SGeorge Wilson 		 */
29010f7643c7SGeorge Wilson 		zio_taskq_dispatch(nio, ZIO_TASKQ_ISSUE, B_TRUE);
29020f7643c7SGeorge Wilson 	}
29030f7643c7SGeorge Wilson 	return (ZIO_PIPELINE_STOP);
29040f7643c7SGeorge Wilson }
29050f7643c7SGeorge Wilson 
2906663207adSDon Brady static void
2907f78cdc34SPaul Dagnelie zio_allocate_dispatch(spa_t *spa, int allocator)
29080f7643c7SGeorge Wilson {
29090f7643c7SGeorge Wilson 	zio_t *zio;
29100f7643c7SGeorge Wilson 
2911f78cdc34SPaul Dagnelie 	mutex_enter(&spa->spa_alloc_locks[allocator]);
2912f78cdc34SPaul Dagnelie 	zio = zio_io_to_allocate(spa, allocator);
2913f78cdc34SPaul Dagnelie 	mutex_exit(&spa->spa_alloc_locks[allocator]);
29140f7643c7SGeorge Wilson 	if (zio == NULL)
29150f7643c7SGeorge Wilson 		return;
29160f7643c7SGeorge Wilson 
29170f7643c7SGeorge Wilson 	ASSERT3U(zio->io_stage, ==, ZIO_STAGE_DVA_THROTTLE);
29180f7643c7SGeorge Wilson 	ASSERT0(zio->io_error);
29190f7643c7SGeorge Wilson 	zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_TRUE);
29200f7643c7SGeorge Wilson }
29210f7643c7SGeorge Wilson 
2922e05725b1Sbonwick static int
2923fa9e4066Sahrens zio_dva_allocate(zio_t *zio)
2924fa9e4066Sahrens {
29258654d025Sperrin 	spa_t *spa = zio->io_spa;
2926663207adSDon Brady 	metaslab_class_t *mc;
2927fa9e4066Sahrens 	blkptr_t *bp = zio->io_bp;
2928fa9e4066Sahrens 	int error;
292909c9d376SGeorge Wilson 	int flags = 0;
2930fa9e4066Sahrens 
2931f5383399SBill Moore 	if (zio->io_gang_leader == NULL) {
2932f5383399SBill Moore 		ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
2933f5383399SBill Moore 		zio->io_gang_leader = zio;
2934f5383399SBill Moore 	}
2935f5383399SBill Moore 
2936fa9e4066Sahrens 	ASSERT(BP_IS_HOLE(bp));
2937fb09f5aaSMadhav Suresh 	ASSERT0(BP_GET_NDVAS(bp));
2938b24ab676SJeff Bonwick 	ASSERT3U(zio->io_prop.zp_copies, >, 0);
2939b24ab676SJeff Bonwick 	ASSERT3U(zio->io_prop.zp_copies, <=, spa_max_replication(spa));
2940fa9e4066Sahrens 	ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp));
2941fa9e4066Sahrens 
2942663207adSDon Brady 	if (zio->io_flags & ZIO_FLAG_NODATA)
29430f7643c7SGeorge Wilson 		flags |= METASLAB_DONT_THROTTLE;
2944663207adSDon Brady 	if (zio->io_flags & ZIO_FLAG_GANG_CHILD)
29450f7643c7SGeorge Wilson 		flags |= METASLAB_GANG_CHILD;
2946663207adSDon Brady 	if (zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE)
29470f7643c7SGeorge Wilson 		flags |= METASLAB_ASYNC_ALLOC;
2948663207adSDon Brady 
2949663207adSDon Brady 	/*
2950663207adSDon Brady 	 * if not already chosen, locate an appropriate allocation class
2951663207adSDon Brady 	 */
2952663207adSDon Brady 	mc = zio->io_metaslab_class;
2953663207adSDon Brady 	if (mc == NULL) {
2954663207adSDon Brady 		mc = spa_preferred_class(spa, zio->io_size,
2955663207adSDon Brady 		    zio->io_prop.zp_type, zio->io_prop.zp_level,
2956663207adSDon Brady 		    zio->io_prop.zp_zpl_smallblk);
2957663207adSDon Brady 		zio->io_metaslab_class = mc;
29580f7643c7SGeorge Wilson 	}
29590f7643c7SGeorge Wilson 
2960e14bb325SJeff Bonwick 	error = metaslab_alloc(spa, mc, zio->io_size, bp,
29618363e80aSGeorge Wilson 	    zio->io_prop.zp_copies, zio->io_txg, NULL, flags,
2962f78cdc34SPaul Dagnelie 	    &zio->io_alloc_list, zio, zio->io_allocator);
2963fa9e4066Sahrens 
2964663207adSDon Brady 	/*
2965663207adSDon Brady 	 * Fallback to normal class when an alloc class is full
2966663207adSDon Brady 	 */
2967663207adSDon Brady 	if (error == ENOSPC && mc != spa_normal_class(spa)) {
2968663207adSDon Brady 		/*
2969663207adSDon Brady 		 * If throttling, transfer reservation over to normal class.
2970663207adSDon Brady 		 * The io_allocator slot can remain the same even though we
2971663207adSDon Brady 		 * are switching classes.
2972663207adSDon Brady 		 */
2973663207adSDon Brady 		if (mc->mc_alloc_throttle_enabled &&
2974663207adSDon Brady 		    (zio->io_flags & ZIO_FLAG_IO_ALLOCATING)) {
2975663207adSDon Brady 			metaslab_class_throttle_unreserve(mc,
2976663207adSDon Brady 			    zio->io_prop.zp_copies, zio->io_allocator, zio);
2977663207adSDon Brady 			zio->io_flags &= ~ZIO_FLAG_IO_ALLOCATING;
2978663207adSDon Brady 
2979663207adSDon Brady 			mc = spa_normal_class(spa);
2980663207adSDon Brady 			VERIFY(metaslab_class_throttle_reserve(mc,
2981663207adSDon Brady 			    zio->io_prop.zp_copies, zio->io_allocator, zio,
2982663207adSDon Brady 			    flags | METASLAB_MUST_RESERVE));
2983663207adSDon Brady 		} else {
2984663207adSDon Brady 			mc = spa_normal_class(spa);
2985663207adSDon Brady 		}
2986663207adSDon Brady 		zio->io_metaslab_class = mc;
2987663207adSDon Brady 
2988663207adSDon Brady 		error = metaslab_alloc(spa, mc, zio->io_size, bp,
2989663207adSDon Brady 		    zio->io_prop.zp_copies, zio->io_txg, NULL, flags,
2990663207adSDon Brady 		    &zio->io_alloc_list, zio, zio->io_allocator);
2991663207adSDon Brady 	}
2992663207adSDon Brady 
29930f7643c7SGeorge Wilson 	if (error != 0) {
299421f7c81cSMatthew Ahrens 		zfs_dbgmsg("%s: metaslab allocation failure: zio %p, "
299509c9d376SGeorge Wilson 		    "size %llu, error %d", spa_name(spa), zio, zio->io_size,
299609c9d376SGeorge Wilson 		    error);
2997e14bb325SJeff Bonwick 		if (error == ENOSPC && zio->io_size > SPA_MINBLOCKSIZE)
2998e14bb325SJeff Bonwick 			return (zio_write_gang_block(zio));
2999fa9e4066Sahrens 		zio->io_error = error;
3000fa9e4066Sahrens 	}
3001e05725b1Sbonwick 
3002e05725b1Sbonwick 	return (ZIO_PIPELINE_CONTINUE);
3003fa9e4066Sahrens }
3004fa9e4066Sahrens 
3005e05725b1Sbonwick static int
3006fa9e4066Sahrens zio_dva_free(zio_t *zio)
3007fa9e4066Sahrens {
3008e14bb325SJeff Bonwick 	metaslab_free(zio->io_spa, zio->io_bp, zio->io_txg, B_FALSE);
3009fa9e4066Sahrens 
3010e05725b1Sbonwick 	return (ZIO_PIPELINE_CONTINUE);
3011fa9e4066Sahrens }
3012fa9e4066Sahrens 
3013e05725b1Sbonwick static int
3014fa9e4066Sahrens zio_dva_claim(zio_t *zio)
3015fa9e4066Sahrens {
3016e14bb325SJeff Bonwick 	int error;
3017e14bb325SJeff Bonwick 
3018e14bb325SJeff Bonwick 	error = metaslab_claim(zio->io_spa, zio->io_bp, zio->io_txg);
3019e14bb325SJeff Bonwick 	if (error)
3020e14bb325SJeff Bonwick 		zio->io_error = error;
3021fa9e4066Sahrens 
3022e05725b1Sbonwick 	return (ZIO_PIPELINE_CONTINUE);
3023fa9e4066Sahrens }
3024fa9e4066Sahrens 
3025e14bb325SJeff Bonwick /*
3026e14bb325SJeff Bonwick  * Undo an allocation.  This is used by zio_done() when an I/O fails
3027e14bb325SJeff Bonwick  * and we want to give back the block we just allocated.
3028e14bb325SJeff Bonwick  * This handles both normal blocks and gang blocks.
3029e14bb325SJeff Bonwick  */
3030e14bb325SJeff Bonwick static void
3031e14bb325SJeff Bonwick zio_dva_unallocate(zio_t *zio, zio_gang_node_t *gn, blkptr_t *bp)
3032e14bb325SJeff Bonwick {
3033e14bb325SJeff Bonwick 	ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp));
3034b24ab676SJeff Bonwick 	ASSERT(zio->io_bp_override == NULL);
3035e14bb325SJeff Bonwick 
3036e14bb325SJeff Bonwick 	if (!BP_IS_HOLE(bp))
3037b24ab676SJeff Bonwick 		metaslab_free(zio->io_spa, bp, bp->blk_birth, B_TRUE);
3038e14bb325SJeff Bonwick 
3039e14bb325SJeff Bonwick 	if (gn != NULL) {
3040e14bb325SJeff Bonwick 		for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
3041e14bb325SJeff Bonwick 			zio_dva_unallocate(zio, gn->gn_child[g],
3042e14bb325SJeff Bonwick 			    &gn->gn_gbh->zg_blkptr[g]);
3043e14bb325SJeff Bonwick 		}
3044e14bb325SJeff Bonwick 	}
3045e14bb325SJeff Bonwick }
3046e14bb325SJeff Bonwick 
3047e14bb325SJeff Bonwick /*
3048e14bb325SJeff Bonwick  * Try to allocate an intent log block.  Return 0 on success, errno on failure.
3049e14bb325SJeff Bonwick  */
3050e14bb325SJeff Bonwick int
3051f78cdc34SPaul Dagnelie zio_alloc_zil(spa_t *spa, uint64_t objset, uint64_t txg, blkptr_t *new_bp,
3052f78cdc34SPaul Dagnelie     blkptr_t *old_bp, uint64_t size, boolean_t *slog)
3053e14bb325SJeff Bonwick {
3054e09fa4daSNeil Perrin 	int error = 1;
30558363e80aSGeorge Wilson 	zio_alloc_list_t io_alloc_list;
3056e14bb325SJeff Bonwick 
3057b24ab676SJeff Bonwick 	ASSERT(txg > spa_syncing_txg(spa));
3058b24ab676SJeff Bonwick 
30598363e80aSGeorge Wilson 	metaslab_trace_init(&io_alloc_list);
3060663207adSDon Brady 
3061663207adSDon Brady 	/*
3062663207adSDon Brady 	 * Block pointer fields are useful to metaslabs for stats and debugging.
3063663207adSDon Brady 	 * Fill in the obvious ones before calling into metaslab_alloc().
3064663207adSDon Brady 	 */
3065663207adSDon Brady 	BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG);
3066663207adSDon Brady 	BP_SET_PSIZE(new_bp, size);
3067663207adSDon Brady 	BP_SET_LEVEL(new_bp, 0);
3068663207adSDon Brady 
3069f78cdc34SPaul Dagnelie 	/*
3070f78cdc34SPaul Dagnelie 	 * When allocating a zil block, we don't have information about
3071f78cdc34SPaul Dagnelie 	 * the final destination of the block except the objset it's part
3072f78cdc34SPaul Dagnelie 	 * of, so we just hash the objset ID to pick the allocator to get
3073f78cdc34SPaul Dagnelie 	 * some parallelism.
3074f78cdc34SPaul Dagnelie 	 */
3075c5ee4681SAlexander Motin 	error = metaslab_alloc(spa, spa_log_class(spa), size, new_bp, 1,
3076f78cdc34SPaul Dagnelie 	    txg, old_bp, METASLAB_HINTBP_AVOID, &io_alloc_list, NULL,
3077f78cdc34SPaul Dagnelie 	    cityhash4(0, 0, 0, objset) % spa->spa_alloc_count);
3078c5ee4681SAlexander Motin 	if (error == 0) {
3079c5ee4681SAlexander Motin 		*slog = TRUE;
3080c5ee4681SAlexander Motin 	} else {
3081b24ab676SJeff Bonwick 		error = metaslab_alloc(spa, spa_normal_class(spa), size,
30828363e80aSGeorge Wilson 		    new_bp, 1, txg, old_bp, METASLAB_HINTBP_AVOID,
3083f78cdc34SPaul Dagnelie 		    &io_alloc_list, NULL, cityhash4(0, 0, 0, objset) %
3084f78cdc34SPaul Dagnelie 		    spa->spa_alloc_count);
3085c5ee4681SAlexander Motin 		if (error == 0)
3086c5ee4681SAlexander Motin 			*slog = FALSE;
3087840345f6SGeorge Wilson 	}
30888363e80aSGeorge Wilson 	metaslab_trace_fini(&io_alloc_list);
3089e14bb325SJeff Bonwick 
3090e14bb325SJeff Bonwick 	if (error == 0) {
3091e14bb325SJeff Bonwick 		BP_SET_LSIZE(new_bp, size);
3092e14bb325SJeff Bonwick 		BP_SET_PSIZE(new_bp, size);
3093e14bb325SJeff Bonwick 		BP_SET_COMPRESS(new_bp, ZIO_COMPRESS_OFF);
30946e1f5caaSNeil Perrin 		BP_SET_CHECKSUM(new_bp,
30956e1f5caaSNeil Perrin 		    spa_version(spa) >= SPA_VERSION_SLIM_ZIL
30966e1f5caaSNeil Perrin 		    ? ZIO_CHECKSUM_ZILOG2 : ZIO_CHECKSUM_ZILOG);
3097e14bb325SJeff Bonwick 		BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG);
3098e14bb325SJeff Bonwick 		BP_SET_LEVEL(new_bp, 0);
3099b24ab676SJeff Bonwick 		BP_SET_DEDUP(new_bp, 0);
3100e14bb325SJeff Bonwick 		BP_SET_BYTEORDER(new_bp, ZFS_HOST_BYTEORDER);
31011271e4b1SPrakash Surya 	} else {
31021271e4b1SPrakash Surya 		zfs_dbgmsg("%s: zil block allocation failure: "
31031271e4b1SPrakash Surya 		    "size %llu, error %d", spa_name(spa), size, error);
3104e14bb325SJeff Bonwick 	}
3105e14bb325SJeff Bonwick 
3106e14bb325SJeff Bonwick 	return (error);
3107e14bb325SJeff Bonwick }
3108e14bb325SJeff Bonwick 
3109fa9e4066Sahrens /*
3110fa9e4066Sahrens  * ==========================================================================
3111fa9e4066Sahrens  * Read and write to physical devices
3112fa9e4066Sahrens  * ==========================================================================
3113fa9e4066Sahrens  */
3114738f37bcSGeorge Wilson 
3115738f37bcSGeorge Wilson 
3116738f37bcSGeorge Wilson /*
3117738f37bcSGeorge Wilson  * Issue an I/O to the underlying vdev. Typically the issue pipeline
3118738f37bcSGeorge Wilson  * stops after this stage and will resume upon I/O completion.
3119738f37bcSGeorge Wilson  * However, there are instances where the vdev layer may need to
3120738f37bcSGeorge Wilson  * continue the pipeline when an I/O was not issued. Since the I/O
3121738f37bcSGeorge Wilson  * that was sent to the vdev layer might be different than the one
3122738f37bcSGeorge Wilson  * currently active in the pipeline (see vdev_queue_io()), we explicitly
3123738f37bcSGeorge Wilson  * force the underlying vdev layers to call either zio_execute() or
3124738f37bcSGeorge Wilson  * zio_interrupt() to ensure that the pipeline continues with the correct I/O.
3125738f37bcSGeorge Wilson  */
3126e05725b1Sbonwick static int
312744cd46caSbillm zio_vdev_io_start(zio_t *zio)
3128fa9e4066Sahrens {
3129fa9e4066Sahrens 	vdev_t *vd = zio->io_vd;
313044cd46caSbillm 	uint64_t align;
31310a4e9518Sgw 	spa_t *spa = zio->io_spa;
31320a4e9518Sgw 
3133e14bb325SJeff Bonwick 	ASSERT(zio->io_error == 0);
3134e14bb325SJeff Bonwick 	ASSERT(zio->io_child_error[ZIO_CHILD_VDEV] == 0);
3135fa9e4066Sahrens 
3136e14bb325SJeff Bonwick 	if (vd == NULL) {
3137e14bb325SJeff Bonwick 		if (!(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
3138e14bb325SJeff Bonwick 			spa_config_enter(spa, SCL_ZIO, zio, RW_READER);
3139fa9e4066Sahrens 
3140e14bb325SJeff Bonwick 		/*
3141e14bb325SJeff Bonwick 		 * The mirror_ops handle multiple DVAs in a single BP.
3142e14bb325SJeff Bonwick 		 */
3143738f37bcSGeorge Wilson 		vdev_mirror_ops.vdev_op_io_start(zio);
3144738f37bcSGeorge Wilson 		return (ZIO_PIPELINE_STOP);
3145fa9e4066Sahrens 	}
3146fa9e4066Sahrens 
31470f7643c7SGeorge Wilson 	ASSERT3P(zio->io_logical, !=, zio);
31486f793812SPavel Zakharov 	if (zio->io_type == ZIO_TYPE_WRITE) {
31496f793812SPavel Zakharov 		ASSERT(spa->spa_trust_config);
31506f793812SPavel Zakharov 
3151*a3874b8bSToomas Soome 		/*
3152*a3874b8bSToomas Soome 		 * Note: the code can handle other kinds of writes,
3153*a3874b8bSToomas Soome 		 * but we don't expect them.
3154*a3874b8bSToomas Soome 		 */
31556f793812SPavel Zakharov 		if (zio->io_vd->vdev_removing) {
31566f793812SPavel Zakharov 			ASSERT(zio->io_flags &
31576f793812SPavel Zakharov 			    (ZIO_FLAG_PHYSICAL | ZIO_FLAG_SELF_HEAL |
31583a4b1be9SMatthew Ahrens 			    ZIO_FLAG_RESILVER | ZIO_FLAG_INDUCE_DAMAGE));
31596f793812SPavel Zakharov 		}
31605cabbc6bSPrashanth Sreenivasa 	}
31610f7643c7SGeorge Wilson 
3162e14bb325SJeff Bonwick 	align = 1ULL << vd->vdev_top->vdev_ashift;
3163e14bb325SJeff Bonwick 
31642a104a52SAlex Reece 	if (!(zio->io_flags & ZIO_FLAG_PHYSICAL) &&
31652a104a52SAlex Reece 	    P2PHASE(zio->io_size, align) != 0) {
31662a104a52SAlex Reece 		/* Transform logical writes to be a full physical block size. */
3167ecc2d604Sbonwick 		uint64_t asize = P2ROUNDUP(zio->io_size, align);
3168770499e1SDan Kimmel 		abd_t *abuf = abd_alloc_sametype(zio->io_abd, asize);
3169e14bb325SJeff Bonwick 		ASSERT(vd == vd->vdev_top);
3170ecc2d604Sbonwick 		if (zio->io_type == ZIO_TYPE_WRITE) {
3171770499e1SDan Kimmel 			abd_copy(abuf, zio->io_abd, zio->io_size);
3172770499e1SDan Kimmel 			abd_zero_off(abuf, zio->io_size, asize - zio->io_size);
3173ecc2d604Sbonwick 		}
3174e14bb325SJeff Bonwick 		zio_push_transform(zio, abuf, asize, asize, zio_subblock);
3175ecc2d604Sbonwick 	}
3176ecc2d604Sbonwick 
31772a104a52SAlex Reece 	/*
31782a104a52SAlex Reece 	 * If this is not a physical io, make sure that it is properly aligned
31792a104a52SAlex Reece 	 * before proceeding.
31802a104a52SAlex Reece 	 */
31812a104a52SAlex Reece 	if (!(zio->io_flags & ZIO_FLAG_PHYSICAL)) {
31822a104a52SAlex Reece 		ASSERT0(P2PHASE(zio->io_offset, align));
31832a104a52SAlex Reece 		ASSERT0(P2PHASE(zio->io_size, align));
31842a104a52SAlex Reece 	} else {
31852a104a52SAlex Reece 		/*
31862a104a52SAlex Reece 		 * For physical writes, we allow 512b aligned writes and assume
31872a104a52SAlex Reece 		 * the device will perform a read-modify-write as necessary.
31882a104a52SAlex Reece 		 */
31892a104a52SAlex Reece 		ASSERT0(P2PHASE(zio->io_offset, SPA_MINBLOCKSIZE));
31902a104a52SAlex Reece 		ASSERT0(P2PHASE(zio->io_size, SPA_MINBLOCKSIZE));
31912a104a52SAlex Reece 	}
31922a104a52SAlex Reece 
3193f9af39baSGeorge Wilson 	VERIFY(zio->io_type != ZIO_TYPE_WRITE || spa_writeable(spa));
31948ad4d6ddSJeff Bonwick 
31958ad4d6ddSJeff Bonwick 	/*
31968ad4d6ddSJeff Bonwick 	 * If this is a repair I/O, and there's no self-healing involved --
31978ad4d6ddSJeff Bonwick 	 * that is, we're just resilvering what we expect to resilver --
31988ad4d6ddSJeff Bonwick 	 * then don't do the I/O unless zio's txg is actually in vd's DTL.
31993a4b1be9SMatthew Ahrens 	 * This prevents spurious resilvering.
32003a4b1be9SMatthew Ahrens 	 *
32013a4b1be9SMatthew Ahrens 	 * There are a few ways that we can end up creating these spurious
32023a4b1be9SMatthew Ahrens 	 * resilver i/os:
32033a4b1be9SMatthew Ahrens 	 *
32043a4b1be9SMatthew Ahrens 	 * 1. A resilver i/o will be issued if any DVA in the BP has a
32053a4b1be9SMatthew Ahrens 	 * dirty DTL.  The mirror code will issue resilver writes to
32063a4b1be9SMatthew Ahrens 	 * each DVA, including the one(s) that are not on vdevs with dirty
32073a4b1be9SMatthew Ahrens 	 * DTLs.
32083a4b1be9SMatthew Ahrens 	 *
32093a4b1be9SMatthew Ahrens 	 * 2. With nested replication, which happens when we have a
32103a4b1be9SMatthew Ahrens 	 * "replacing" or "spare" vdev that's a child of a mirror or raidz.
32113a4b1be9SMatthew Ahrens 	 * For example, given mirror(replacing(A+B), C), it's likely that
32123a4b1be9SMatthew Ahrens 	 * only A is out of date (it's the new device). In this case, we'll
32133a4b1be9SMatthew Ahrens 	 * read from C, then use the data to resilver A+B -- but we don't
32143a4b1be9SMatthew Ahrens 	 * actually want to resilver B, just A. The top-level mirror has no
32153a4b1be9SMatthew Ahrens 	 * way to know this, so instead we just discard unnecessary repairs
32163a4b1be9SMatthew Ahrens 	 * as we work our way down the vdev tree.
32173a4b1be9SMatthew Ahrens 	 *
32183a4b1be9SMatthew Ahrens 	 * 3. ZTEST also creates mirrors of mirrors, mirrors of raidz, etc.
32193a4b1be9SMatthew Ahrens 	 * The same logic applies to any form of nested replication: ditto
32203a4b1be9SMatthew Ahrens 	 * + mirror, RAID-Z + replacing, etc.
32213a4b1be9SMatthew Ahrens 	 *
32223a4b1be9SMatthew Ahrens 	 * However, indirect vdevs point off to other vdevs which may have
32233a4b1be9SMatthew Ahrens 	 * DTL's, so we never bypass them.  The child i/os on concrete vdevs
32243a4b1be9SMatthew Ahrens 	 * will be properly bypassed instead.
32258ad4d6ddSJeff Bonwick 	 */
32268ad4d6ddSJeff Bonwick 	if ((zio->io_flags & ZIO_FLAG_IO_REPAIR) &&
32278ad4d6ddSJeff Bonwick 	    !(zio->io_flags & ZIO_FLAG_SELF_HEAL) &&
32288ad4d6ddSJeff Bonwick 	    zio->io_txg != 0 &&	/* not a delegated i/o */
32293a4b1be9SMatthew Ahrens 	    vd->vdev_ops != &vdev_indirect_ops &&
32308ad4d6ddSJeff Bonwick 	    !vdev_dtl_contains(vd, DTL_PARTIAL, zio->io_txg, 1)) {
32318ad4d6ddSJeff Bonwick 		ASSERT(zio->io_type == ZIO_TYPE_WRITE);
32328ad4d6ddSJeff Bonwick 		zio_vdev_io_bypass(zio);
32338ad4d6ddSJeff Bonwick 		return (ZIO_PIPELINE_CONTINUE);
32348ad4d6ddSJeff Bonwick 	}
3235fa9e4066Sahrens 
3236e14bb325SJeff Bonwick 	if (vd->vdev_ops->vdev_op_leaf &&
3237e14bb325SJeff Bonwick 	    (zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE)) {
3238e14bb325SJeff Bonwick 
323943466aaeSMax Grossman 		if (zio->io_type == ZIO_TYPE_READ && vdev_cache_read(zio))
3240a3f829aeSBill Moore 			return (ZIO_PIPELINE_CONTINUE);
3241e14bb325SJeff Bonwick 
3242e14bb325SJeff Bonwick 		if ((zio = vdev_queue_io(zio)) == NULL)
3243e14bb325SJeff Bonwick 			return (ZIO_PIPELINE_STOP);
3244e14bb325SJeff Bonwick 
3245e14bb325SJeff Bonwick 		if (!vdev_accessible(vd, zio)) {
3246be6fd75aSMatthew Ahrens 			zio->io_error = SET_ERROR(ENXIO);
3247e14bb325SJeff Bonwick 			zio_interrupt(zio);
3248e14bb325SJeff Bonwick 			return (ZIO_PIPELINE_STOP);
3249e14bb325SJeff Bonwick 		}
3250e14bb325SJeff Bonwick 	}
3251e14bb325SJeff Bonwick 
3252738f37bcSGeorge Wilson 	vd->vdev_ops->vdev_op_io_start(zio);
3253738f37bcSGeorge Wilson 	return (ZIO_PIPELINE_STOP);
3254fa9e4066Sahrens }
3255fa9e4066Sahrens 
3256e05725b1Sbonwick static int
3257fa9e4066Sahrens zio_vdev_io_done(zio_t *zio)
3258fa9e4066Sahrens {
3259e14bb325SJeff Bonwick 	vdev_t *vd = zio->io_vd;
3260e14bb325SJeff Bonwick 	vdev_ops_t *ops = vd ? vd->vdev_ops : &vdev_mirror_ops;
3261e14bb325SJeff Bonwick 	boolean_t unexpected_error = B_FALSE;
3262e05725b1Sbonwick 
3263d6e1c446SGeorge Wilson 	if (zio_wait_for_children(zio, ZIO_CHILD_VDEV_BIT, ZIO_WAIT_DONE)) {
3264e14bb325SJeff Bonwick 		return (ZIO_PIPELINE_STOP);
3265d6e1c446SGeorge Wilson 	}
3266fa9e4066Sahrens 
3267e14bb325SJeff Bonwick 	ASSERT(zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE);
3268e14bb325SJeff Bonwick 
3269e14bb325SJeff Bonwick 	if (vd != NULL && vd->vdev_ops->vdev_op_leaf) {
3270e14bb325SJeff Bonwick 
3271e14bb325SJeff Bonwick 		vdev_queue_io_done(zio);
3272fa9e4066Sahrens 
3273e14bb325SJeff Bonwick 		if (zio->io_type == ZIO_TYPE_WRITE)
3274e14bb325SJeff Bonwick 			vdev_cache_write(zio);
3275e14bb325SJeff Bonwick 
3276e14bb325SJeff Bonwick 		if (zio_injection_enabled && zio->io_error == 0)
32778956713aSEric Schrock 			zio->io_error = zio_handle_device_injection(vd,
32788956713aSEric Schrock 			    zio, EIO);
3279e14bb325SJeff Bonwick 
3280e14bb325SJeff Bonwick 		if (zio_injection_enabled && zio->io_error == 0)
3281e14bb325SJeff Bonwick 			zio->io_error = zio_handle_label_injection(zio, EIO);
3282e14bb325SJeff Bonwick 
3283e14bb325SJeff Bonwick 		if (zio->io_error) {
3284e14bb325SJeff Bonwick 			if (!vdev_accessible(vd, zio)) {
3285be6fd75aSMatthew Ahrens 				zio->io_error = SET_ERROR(ENXIO);
3286e14bb325SJeff Bonwick 			} else {
3287e14bb325SJeff Bonwick 				unexpected_error = B_TRUE;
3288e14bb325SJeff Bonwick 			}
3289e14bb325SJeff Bonwick 		}
329051ece835Seschrock 	}
3291fa9e4066Sahrens 
3292e14bb325SJeff Bonwick 	ops->vdev_op_io_done(zio);
3293e14bb325SJeff Bonwick 
3294e14bb325SJeff Bonwick 	if (unexpected_error)
3295a3f829aeSBill Moore 		VERIFY(vdev_probe(vd, zio) == NULL);
3296e14bb325SJeff Bonwick 
3297e14bb325SJeff Bonwick 	return (ZIO_PIPELINE_CONTINUE);
3298fa9e4066Sahrens }
3299fa9e4066Sahrens 
3300*a3874b8bSToomas Soome /*
3301*a3874b8bSToomas Soome  * This function is used to change the priority of an existing zio that is
3302*a3874b8bSToomas Soome  * currently in-flight. This is used by the arc to upgrade priority in the
3303*a3874b8bSToomas Soome  * event that a demand read is made for a block that is currently queued
3304*a3874b8bSToomas Soome  * as a scrub or async read IO. Otherwise, the high priority read request
3305*a3874b8bSToomas Soome  * would end up having to wait for the lower priority IO.
3306*a3874b8bSToomas Soome  */
3307*a3874b8bSToomas Soome void
3308*a3874b8bSToomas Soome zio_change_priority(zio_t *pio, zio_priority_t priority)
3309*a3874b8bSToomas Soome {
3310*a3874b8bSToomas Soome 	zio_t *cio, *cio_next;
3311*a3874b8bSToomas Soome 	zio_link_t *zl = NULL;
3312*a3874b8bSToomas Soome 
3313*a3874b8bSToomas Soome 	ASSERT3U(priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
3314*a3874b8bSToomas Soome 
3315*a3874b8bSToomas Soome 	if (pio->io_vd != NULL && pio->io_vd->vdev_ops->vdev_op_leaf) {
3316*a3874b8bSToomas Soome 		vdev_queue_change_io_priority(pio, priority);
3317*a3874b8bSToomas Soome 	} else {
3318*a3874b8bSToomas Soome 		pio->io_priority = priority;
3319*a3874b8bSToomas Soome 	}
3320*a3874b8bSToomas Soome 
3321*a3874b8bSToomas Soome 	mutex_enter(&pio->io_lock);
3322*a3874b8bSToomas Soome 	for (cio = zio_walk_children(pio, &zl); cio != NULL; cio = cio_next) {
3323*a3874b8bSToomas Soome 		cio_next = zio_walk_children(pio, &zl);
3324*a3874b8bSToomas Soome 		zio_change_priority(cio, priority);
3325*a3874b8bSToomas Soome 	}
3326*a3874b8bSToomas Soome 	mutex_exit(&pio->io_lock);
3327*a3874b8bSToomas Soome }
3328*a3874b8bSToomas Soome 
332922fe2c88SJonathan Adams /*
333022fe2c88SJonathan Adams  * For non-raidz ZIOs, we can just copy aside the bad data read from the
333122fe2c88SJonathan Adams  * disk, and use that to finish the checksum ereport later.
333222fe2c88SJonathan Adams  */
333322fe2c88SJonathan Adams static void
333422fe2c88SJonathan Adams zio_vsd_default_cksum_finish(zio_cksum_report_t *zcr,
333522fe2c88SJonathan Adams     const void *good_buf)
333622fe2c88SJonathan Adams {
333722fe2c88SJonathan Adams 	/* no processing needed */
333822fe2c88SJonathan Adams 	zfs_ereport_finish_checksum(zcr, good_buf, zcr->zcr_cbdata, B_FALSE);
333922fe2c88SJonathan Adams }
334022fe2c88SJonathan Adams 
334122fe2c88SJonathan Adams /*ARGSUSED*/
334222fe2c88SJonathan Adams void
334322fe2c88SJonathan Adams zio_vsd_default_cksum_report(zio_t *zio, zio_cksum_report_t *zcr, void *ignored)
334422fe2c88SJonathan Adams {
334522fe2c88SJonathan Adams 	void *buf = zio_buf_alloc(zio->io_size);
334622fe2c88SJonathan Adams 
3347770499e1SDan Kimmel 	abd_copy_to_buf(buf, zio->io_abd, zio->io_size);
334822fe2c88SJonathan Adams 
334922fe2c88SJonathan Adams 	zcr->zcr_cbinfo = zio->io_size;
335022fe2c88SJonathan Adams 	zcr->zcr_cbdata = buf;
335122fe2c88SJonathan Adams 	zcr->zcr_finish = zio_vsd_default_cksum_finish;
335222fe2c88SJonathan Adams 	zcr->zcr_free = zio_buf_free;
335322fe2c88SJonathan Adams }
335422fe2c88SJonathan Adams 
3355e05725b1Sbonwick static int
3356fa9e4066Sahrens zio_vdev_io_assess(zio_t *zio)
3357fa9e4066Sahrens {
3358fa9e4066Sahrens 	vdev_t *vd = zio->io_vd;
3359e14bb325SJeff Bonwick 
3360d6e1c446SGeorge Wilson 	if (zio_wait_for_children(zio, ZIO_CHILD_VDEV_BIT, ZIO_WAIT_DONE)) {
3361e14bb325SJeff Bonwick 		return (ZIO_PIPELINE_STOP);
3362d6e1c446SGeorge Wilson 	}
3363e14bb325SJeff Bonwick 
3364e14bb325SJeff Bonwick 	if (vd == NULL && !(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
3365e14bb325SJeff Bonwick 		spa_config_exit(zio->io_spa, SCL_ZIO, zio);
3366e14bb325SJeff Bonwick 
3367e14bb325SJeff Bonwick 	if (zio->io_vsd != NULL) {
336822fe2c88SJonathan Adams 		zio->io_vsd_ops->vsd_free(zio);
3369e14bb325SJeff Bonwick 		zio->io_vsd = NULL;
3370ecc2d604Sbonwick 	}
3371ecc2d604Sbonwick 
3372e14bb325SJeff Bonwick 	if (zio_injection_enabled && zio->io_error == 0)
3373ea8dc4b6Seschrock 		zio->io_error = zio_handle_fault_injection(zio, EIO);
3374ea8dc4b6Seschrock 
3375fa9e4066Sahrens 	/*
3376fa9e4066Sahrens 	 * If the I/O failed, determine whether we should attempt to retry it.
337735a5a358SJonathan Adams 	 *
337835a5a358SJonathan Adams 	 * On retry, we cut in line in the issue queue, since we don't want
337935a5a358SJonathan Adams 	 * compression/checksumming/etc. work to prevent our (cheap) IO reissue.
3380fa9e4066Sahrens 	 */
3381e14bb325SJeff Bonwick 	if (zio->io_error && vd == NULL &&
3382e14bb325SJeff Bonwick 	    !(zio->io_flags & (ZIO_FLAG_DONT_RETRY | ZIO_FLAG_IO_RETRY))) {
3383e14bb325SJeff Bonwick 		ASSERT(!(zio->io_flags & ZIO_FLAG_DONT_QUEUE));	/* not a leaf */
3384e14bb325SJeff Bonwick 		ASSERT(!(zio->io_flags & ZIO_FLAG_IO_BYPASS));	/* not a leaf */
3385fa9e4066Sahrens 		zio->io_error = 0;
3386e14bb325SJeff Bonwick 		zio->io_flags |= ZIO_FLAG_IO_RETRY |
3387e14bb325SJeff Bonwick 		    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE;
3388b24ab676SJeff Bonwick 		zio->io_stage = ZIO_STAGE_VDEV_IO_START >> 1;
338935a5a358SJonathan Adams 		zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE,
339035a5a358SJonathan Adams 		    zio_requeue_io_start_cut_in_line);
3391e14bb325SJeff Bonwick 		return (ZIO_PIPELINE_STOP);
3392ea8dc4b6Seschrock 	}
3393fa9e4066Sahrens 
3394e14bb325SJeff Bonwick 	/*
3395e14bb325SJeff Bonwick 	 * If we got an error on a leaf device, convert it to ENXIO
3396e14bb325SJeff Bonwick 	 * if the device is not accessible at all.
3397e14bb325SJeff Bonwick 	 */
3398e14bb325SJeff Bonwick 	if (zio->io_error && vd != NULL && vd->vdev_ops->vdev_op_leaf &&
3399e14bb325SJeff Bonwick 	    !vdev_accessible(vd, zio))
3400be6fd75aSMatthew Ahrens 		zio->io_error = SET_ERROR(ENXIO);
3401e14bb325SJeff Bonwick 
3402e14bb325SJeff Bonwick 	/*
3403e14bb325SJeff Bonwick 	 * If we can't write to an interior vdev (mirror or RAID-Z),
3404e14bb325SJeff Bonwick 	 * set vdev_cant_write so that we stop trying to allocate from it.
3405e14bb325SJeff Bonwick 	 */
3406e14bb325SJeff Bonwick 	if (zio->io_error == ENXIO && zio->io_type == ZIO_TYPE_WRITE &&
34073b2aab18SMatthew Ahrens 	    vd != NULL && !vd->vdev_ops->vdev_op_leaf) {
3408e14bb325SJeff Bonwick 		vd->vdev_cant_write = B_TRUE;
34093b2aab18SMatthew Ahrens 	}
3410e14bb325SJeff Bonwick 
3411295438baSHans Rosenfeld 	/*
3412295438baSHans Rosenfeld 	 * If a cache flush returns ENOTSUP or ENOTTY, we know that no future
3413295438baSHans Rosenfeld 	 * attempts will ever succeed. In this case we set a persistent bit so
3414295438baSHans Rosenfeld 	 * that we don't bother with it in the future.
3415295438baSHans Rosenfeld 	 */
3416295438baSHans Rosenfeld 	if ((zio->io_error == ENOTSUP || zio->io_error == ENOTTY) &&
3417295438baSHans Rosenfeld 	    zio->io_type == ZIO_TYPE_IOCTL &&
3418295438baSHans Rosenfeld 	    zio->io_cmd == DKIOCFLUSHWRITECACHE && vd != NULL)
3419295438baSHans Rosenfeld 		vd->vdev_nowritecache = B_TRUE;
3420295438baSHans Rosenfeld 
3421e14bb325SJeff Bonwick 	if (zio->io_error)
3422e14bb325SJeff Bonwick 		zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
3423e14bb325SJeff Bonwick 
342469962b56SMatthew Ahrens 	if (vd != NULL && vd->vdev_ops->vdev_op_leaf &&
342569962b56SMatthew Ahrens 	    zio->io_physdone != NULL) {
342669962b56SMatthew Ahrens 		ASSERT(!(zio->io_flags & ZIO_FLAG_DELEGATED));
342769962b56SMatthew Ahrens 		ASSERT(zio->io_child_type == ZIO_CHILD_VDEV);
342869962b56SMatthew Ahrens 		zio->io_physdone(zio->io_logical);
342969962b56SMatthew Ahrens 	}
343069962b56SMatthew Ahrens 
3431e05725b1Sbonwick 	return (ZIO_PIPELINE_CONTINUE);
3432fa9e4066Sahrens }
3433fa9e4066Sahrens 
3434fa9e4066Sahrens void
3435fa9e4066Sahrens zio_vdev_io_reissue(zio_t *zio)
3436fa9e4066Sahrens {
3437fa9e4066Sahrens 	ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
3438fa9e4066Sahrens 	ASSERT(zio->io_error == 0);
3439fa9e4066Sahrens 
3440b24ab676SJeff Bonwick 	zio->io_stage >>= 1;
3441fa9e4066Sahrens }
3442fa9e4066Sahrens 
3443fa9e4066Sahrens void
3444fa9e4066Sahrens zio_vdev_io_redone(zio_t *zio)
3445fa9e4066Sahrens {
3446fa9e4066Sahrens 	ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_DONE);
3447fa9e4066Sahrens 
3448b24ab676SJeff Bonwick 	zio->io_stage >>= 1;
3449fa9e4066Sahrens }
3450fa9e4066Sahrens 
3451fa9e4066Sahrens void
3452fa9e4066Sahrens zio_vdev_io_bypass(zio_t *zio)
3453fa9e4066Sahrens {
3454fa9e4066Sahrens 	ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
3455fa9e4066Sahrens 	ASSERT(zio->io_error == 0);
3456fa9e4066Sahrens 
3457fa9e4066Sahrens 	zio->io_flags |= ZIO_FLAG_IO_BYPASS;
3458b24ab676SJeff Bonwick 	zio->io_stage = ZIO_STAGE_VDEV_IO_ASSESS >> 1;
3459fa9e4066Sahrens }
3460fa9e4066Sahrens 
3461fa9e4066Sahrens /*
3462fa9e4066Sahrens  * ==========================================================================
3463fa9e4066Sahrens  * Generate and verify checksums
3464fa9e4066Sahrens  * ==========================================================================
3465fa9e4066Sahrens  */
3466e05725b1Sbonwick static int
3467fa9e4066Sahrens zio_checksum_generate(zio_t *zio)
3468fa9e4066Sahrens {
3469fa9e4066Sahrens 	blkptr_t *bp = zio->io_bp;
3470e14bb325SJeff Bonwick 	enum zio_checksum checksum;
3471fa9e4066Sahrens 
3472e14bb325SJeff Bonwick 	if (bp == NULL) {
3473e14bb325SJeff Bonwick 		/*
3474e14bb325SJeff Bonwick 		 * This is zio_write_phys().
3475e14bb325SJeff Bonwick 		 * We're either generating a label checksum, or none at all.
3476e14bb325SJeff Bonwick 		 */
3477e14bb325SJeff Bonwick 		checksum = zio->io_prop.zp_checksum;
3478e14bb325SJeff Bonwick 
3479e14bb325SJeff Bonwick 		if (checksum == ZIO_CHECKSUM_OFF)
3480e14bb325SJeff Bonwick 			return (ZIO_PIPELINE_CONTINUE);
3481fa9e4066Sahrens 
3482e14bb325SJeff Bonwick 		ASSERT(checksum == ZIO_CHECKSUM_LABEL);
3483e14bb325SJeff Bonwick 	} else {
3484e14bb325SJeff Bonwick 		if (BP_IS_GANG(bp) && zio->io_child_type == ZIO_CHILD_GANG) {
3485e14bb325SJeff Bonwick 			ASSERT(!IO_IS_ALLOCATING(zio));
3486e14bb325SJeff Bonwick 			checksum = ZIO_CHECKSUM_GANG_HEADER;
3487e14bb325SJeff Bonwick 		} else {
3488e14bb325SJeff Bonwick 			checksum = BP_GET_CHECKSUM(bp);
3489e14bb325SJeff Bonwick 		}
3490e14bb325SJeff Bonwick 	}
3491fa9e4066Sahrens 
3492770499e1SDan Kimmel 	zio_checksum_compute(zio, checksum, zio->io_abd, zio->io_size);
3493fa9e4066Sahrens 
3494e05725b1Sbonwick 	return (ZIO_PIPELINE_CONTINUE);
3495fa9e4066Sahrens }
3496fa9e4066Sahrens 
3497e05725b1Sbonwick static int
3498e14bb325SJeff Bonwick zio_checksum_verify(zio_t *zio)
3499fa9e4066Sahrens {
350022fe2c88SJonathan Adams 	zio_bad_cksum_t info;
3501e14bb325SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
3502e14bb325SJeff Bonwick 	int error;
3503fa9e4066Sahrens 
3504b24ab676SJeff Bonwick 	ASSERT(zio->io_vd != NULL);
3505b24ab676SJeff Bonwick 
3506e14bb325SJeff Bonwick 	if (bp == NULL) {
3507e14bb325SJeff Bonwick 		/*
3508e14bb325SJeff Bonwick 		 * This is zio_read_phys().
3509e14bb325SJeff Bonwick 		 * We're either verifying a label checksum, or nothing at all.
3510e14bb325SJeff Bonwick 		 */
3511e14bb325SJeff Bonwick 		if (zio->io_prop.zp_checksum == ZIO_CHECKSUM_OFF)
3512e14bb325SJeff Bonwick 			return (ZIO_PIPELINE_CONTINUE);
3513fa9e4066Sahrens 
3514e14bb325SJeff Bonwick 		ASSERT(zio->io_prop.zp_checksum == ZIO_CHECKSUM_LABEL);
3515e14bb325SJeff Bonwick 	}
3516fa9e4066Sahrens 
351722fe2c88SJonathan Adams 	if ((error = zio_checksum_error(zio, &info)) != 0) {
3518e14bb325SJeff Bonwick 		zio->io_error = error;
3519373dc1cfSMatthew Ahrens 		if (error == ECKSUM &&
3520373dc1cfSMatthew Ahrens 		    !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
352122fe2c88SJonathan Adams 			zfs_ereport_start_checksum(zio->io_spa,
352222fe2c88SJonathan Adams 			    zio->io_vd, zio, zio->io_offset,
352322fe2c88SJonathan Adams 			    zio->io_size, NULL, &info);
3524e14bb325SJeff Bonwick 		}
3525fa9e4066Sahrens 	}
3526fa9e4066Sahrens 
3527e05725b1Sbonwick 	return (ZIO_PIPELINE_CONTINUE);
3528fa9e4066Sahrens }
3529fa9e4066Sahrens 
3530fa9e4066Sahrens /*
3531fa9e4066Sahrens  * Called by RAID-Z to ensure we don't compute the checksum twice.
3532fa9e4066Sahrens  */
3533fa9e4066Sahrens void
3534fa9e4066Sahrens zio_checksum_verified(zio_t *zio)
3535fa9e4066Sahrens {
3536b24ab676SJeff Bonwick 	zio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY;
3537fa9e4066Sahrens }
3538fa9e4066Sahrens 
3539fa9e4066Sahrens /*
3540e14bb325SJeff Bonwick  * ==========================================================================
3541e14bb325SJeff Bonwick  * Error rank.  Error are ranked in the order 0, ENXIO, ECKSUM, EIO, other.
35425d7b4d43SMatthew Ahrens  * An error of 0 indicates success.  ENXIO indicates whole-device failure,
3543e14bb325SJeff Bonwick  * which may be transient (e.g. unplugged) or permament.  ECKSUM and EIO
3544e14bb325SJeff Bonwick  * indicate errors that are specific to one I/O, and most likely permanent.
3545e14bb325SJeff Bonwick  * Any other error is presumed to be worse because we weren't expecting it.
3546e14bb325SJeff Bonwick  * ==========================================================================
3547fa9e4066Sahrens  */
3548e14bb325SJeff Bonwick int
3549e14bb325SJeff Bonwick zio_worst_error(int e1, int e2)
3550fa9e4066Sahrens {
3551e14bb325SJeff Bonwick 	static int zio_error_rank[] = { 0, ENXIO, ECKSUM, EIO };
3552e14bb325SJeff Bonwick 	int r1, r2;
3553e14bb325SJeff Bonwick 
3554e14bb325SJeff Bonwick 	for (r1 = 0; r1 < sizeof (zio_error_rank) / sizeof (int); r1++)
3555e14bb325SJeff Bonwick 		if (e1 == zio_error_rank[r1])
3556e14bb325SJeff Bonwick 			break;
3557e14bb325SJeff Bonwick 
3558e14bb325SJeff Bonwick 	for (r2 = 0; r2 < sizeof (zio_error_rank) / sizeof (int); r2++)
3559e14bb325SJeff Bonwick 		if (e2 == zio_error_rank[r2])
3560e14bb325SJeff Bonwick 			break;
356144cd46caSbillm 
3562e14bb325SJeff Bonwick 	return (r1 > r2 ? e1 : e2);
3563fa9e4066Sahrens }
3564fa9e4066Sahrens 
3565fa9e4066Sahrens /*
3566fa9e4066Sahrens  * ==========================================================================
3567e14bb325SJeff Bonwick  * I/O completion
3568fa9e4066Sahrens  * ==========================================================================
3569fa9e4066Sahrens  */
3570e14bb325SJeff Bonwick static int
3571e14bb325SJeff Bonwick zio_ready(zio_t *zio)
3572fa9e4066Sahrens {
3573e14bb325SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
3574a3f829aeSBill Moore 	zio_t *pio, *pio_next;
35750f7643c7SGeorge Wilson 	zio_link_t *zl = NULL;
3576fa9e4066Sahrens 
3577d6e1c446SGeorge Wilson 	if (zio_wait_for_children(zio, ZIO_CHILD_GANG_BIT | ZIO_CHILD_DDT_BIT,
3578d6e1c446SGeorge Wilson 	    ZIO_WAIT_READY)) {
3579f5383399SBill Moore 		return (ZIO_PIPELINE_STOP);
3580d6e1c446SGeorge Wilson 	}
3581fa9e4066Sahrens 
3582f5383399SBill Moore 	if (zio->io_ready) {
3583e14bb325SJeff Bonwick 		ASSERT(IO_IS_ALLOCATING(zio));
358480901aeaSGeorge Wilson 		ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp) ||
358580901aeaSGeorge Wilson 		    (zio->io_flags & ZIO_FLAG_NOPWRITE));
3586e14bb325SJeff Bonwick 		ASSERT(zio->io_children[ZIO_CHILD_GANG][ZIO_WAIT_READY] == 0);
3587fa9e4066Sahrens 
3588e14bb325SJeff Bonwick 		zio->io_ready(zio);
3589e14bb325SJeff Bonwick 	}
3590fa9e4066Sahrens 
3591e14bb325SJeff Bonwick 	if (bp != NULL && bp != &zio->io_bp_copy)
3592e14bb325SJeff Bonwick 		zio->io_bp_copy = *bp;
3593fa9e4066Sahrens 
35940f7643c7SGeorge Wilson 	if (zio->io_error != 0) {
3595e14bb325SJeff Bonwick 		zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
3596fa9e4066Sahrens 
35970f7643c7SGeorge Wilson 		if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
35980f7643c7SGeorge Wilson 			ASSERT(IO_IS_ALLOCATING(zio));
35990f7643c7SGeorge Wilson 			ASSERT(zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
3600663207adSDon Brady 			ASSERT(zio->io_metaslab_class != NULL);
3601663207adSDon Brady 
36020f7643c7SGeorge Wilson 			/*
36030f7643c7SGeorge Wilson 			 * We were unable to allocate anything, unreserve and
36040f7643c7SGeorge Wilson 			 * issue the next I/O to allocate.
36050f7643c7SGeorge Wilson 			 */
36060f7643c7SGeorge Wilson 			metaslab_class_throttle_unreserve(
3607663207adSDon Brady 			    zio->io_metaslab_class, zio->io_prop.zp_copies,
3608663207adSDon Brady 			    zio->io_allocator, zio);
3609f78cdc34SPaul Dagnelie 			zio_allocate_dispatch(zio->io_spa, zio->io_allocator);
36100f7643c7SGeorge Wilson 		}
36110f7643c7SGeorge Wilson 	}
36120f7643c7SGeorge Wilson 
3613a3f829aeSBill Moore 	mutex_enter(&zio->io_lock);
3614a3f829aeSBill Moore 	zio->io_state[ZIO_WAIT_READY] = 1;
36150f7643c7SGeorge Wilson 	pio = zio_walk_parents(zio, &zl);
3616a3f829aeSBill Moore 	mutex_exit(&zio->io_lock);
3617a3f829aeSBill Moore 
3618a3f829aeSBill Moore 	/*
3619a3f829aeSBill Moore 	 * As we notify zio's parents, new parents could be added.
3620a3f829aeSBill Moore 	 * New parents go to the head of zio's io_parent_list, however,
3621a3f829aeSBill Moore 	 * so we will (correctly) not notify them.  The remainder of zio's
3622a3f829aeSBill Moore 	 * io_parent_list, from 'pio_next' onward, cannot change because
3623a3f829aeSBill Moore 	 * all parents must wait for us to be done before they can be done.
3624a3f829aeSBill Moore 	 */
3625a3f829aeSBill Moore 	for (; pio != NULL; pio = pio_next) {
36260f7643c7SGeorge Wilson 		pio_next = zio_walk_parents(zio, &zl);
3627e14bb325SJeff Bonwick 		zio_notify_parent(pio, zio, ZIO_WAIT_READY);
3628a3f829aeSBill Moore 	}
3629fa9e4066Sahrens 
3630b24ab676SJeff Bonwick 	if (zio->io_flags & ZIO_FLAG_NODATA) {
3631b24ab676SJeff Bonwick 		if (BP_IS_GANG(bp)) {
3632b24ab676SJeff Bonwick 			zio->io_flags &= ~ZIO_FLAG_NODATA;
3633b24ab676SJeff Bonwick 		} else {
3634770499e1SDan Kimmel 			ASSERT((uintptr_t)zio->io_abd < SPA_MAXBLOCKSIZE);
3635b24ab676SJeff Bonwick 			zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES;
3636b24ab676SJeff Bonwick 		}
3637b24ab676SJeff Bonwick 	}
3638b24ab676SJeff Bonwick 
3639a33cae98STim Haley 	if (zio_injection_enabled &&
3640a33cae98STim Haley 	    zio->io_spa->spa_syncing_txg == zio->io_txg)
3641a33cae98STim Haley 		zio_handle_ignored_writes(zio);
3642a33cae98STim Haley 
3643e14bb325SJeff Bonwick 	return (ZIO_PIPELINE_CONTINUE);
3644fa9e4066Sahrens }
3645fa9e4066Sahrens 
36460f7643c7SGeorge Wilson /*
36470f7643c7SGeorge Wilson  * Update the allocation throttle accounting.
36480f7643c7SGeorge Wilson  */
36490f7643c7SGeorge Wilson static void
36500f7643c7SGeorge Wilson zio_dva_throttle_done(zio_t *zio)
36510f7643c7SGeorge Wilson {
36520f7643c7SGeorge Wilson 	zio_t *lio = zio->io_logical;
36530f7643c7SGeorge Wilson 	zio_t *pio = zio_unique_parent(zio);
36540f7643c7SGeorge Wilson 	vdev_t *vd = zio->io_vd;
36550f7643c7SGeorge Wilson 	int flags = METASLAB_ASYNC_ALLOC;
36560f7643c7SGeorge Wilson 
36570f7643c7SGeorge Wilson 	ASSERT3P(zio->io_bp, !=, NULL);
36580f7643c7SGeorge Wilson 	ASSERT3U(zio->io_type, ==, ZIO_TYPE_WRITE);
36590f7643c7SGeorge Wilson 	ASSERT3U(zio->io_priority, ==, ZIO_PRIORITY_ASYNC_WRITE);
36600f7643c7SGeorge Wilson 	ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_VDEV);
36610f7643c7SGeorge Wilson 	ASSERT(vd != NULL);
36620f7643c7SGeorge Wilson 	ASSERT3P(vd, ==, vd->vdev_top);
36630f7643c7SGeorge Wilson 	ASSERT(!(zio->io_flags & (ZIO_FLAG_IO_REPAIR | ZIO_FLAG_IO_RETRY)));
36640f7643c7SGeorge Wilson 	ASSERT(zio->io_flags & ZIO_FLAG_IO_ALLOCATING);
36650f7643c7SGeorge Wilson 	ASSERT(!(lio->io_flags & ZIO_FLAG_IO_REWRITE));
36660f7643c7SGeorge Wilson 	ASSERT(!(lio->io_orig_flags & ZIO_FLAG_NODATA));
36670f7643c7SGeorge Wilson 
36680f7643c7SGeorge Wilson 	/*
36690f7643c7SGeorge Wilson 	 * Parents of gang children can have two flavors -- ones that
36700f7643c7SGeorge Wilson 	 * allocated the gang header (will have ZIO_FLAG_IO_REWRITE set)
36710f7643c7SGeorge Wilson 	 * and ones that allocated the constituent blocks. The allocation
36720f7643c7SGeorge Wilson 	 * throttle needs to know the allocating parent zio so we must find
36730f7643c7SGeorge Wilson 	 * it here.
36740f7643c7SGeorge Wilson 	 */
36750f7643c7SGeorge Wilson 	if (pio->io_child_type == ZIO_CHILD_GANG) {
36760f7643c7SGeorge Wilson 		/*
36770f7643c7SGeorge Wilson 		 * If our parent is a rewrite gang child then our grandparent
36780f7643c7SGeorge Wilson 		 * would have been the one that performed the allocation.
36790f7643c7SGeorge Wilson 		 */
36800f7643c7SGeorge Wilson 		if (pio->io_flags & ZIO_FLAG_IO_REWRITE)
36810f7643c7SGeorge Wilson 			pio = zio_unique_parent(pio);
36820f7643c7SGeorge Wilson 		flags |= METASLAB_GANG_CHILD;
36830f7643c7SGeorge Wilson 	}
36840f7643c7SGeorge Wilson 
36850f7643c7SGeorge Wilson 	ASSERT(IO_IS_ALLOCATING(pio));
36860f7643c7SGeorge Wilson 	ASSERT3P(zio, !=, zio->io_logical);
36870f7643c7SGeorge Wilson 	ASSERT(zio->io_logical != NULL);
36880f7643c7SGeorge Wilson 	ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REPAIR));
36890f7643c7SGeorge Wilson 	ASSERT0(zio->io_flags & ZIO_FLAG_NOPWRITE);
3690663207adSDon Brady 	ASSERT(zio->io_metaslab_class != NULL);
36910f7643c7SGeorge Wilson 
36920f7643c7SGeorge Wilson 	mutex_enter(&pio->io_lock);
3693f78cdc34SPaul Dagnelie 	metaslab_group_alloc_decrement(zio->io_spa, vd->vdev_id, pio, flags,
3694f78cdc34SPaul Dagnelie 	    pio->io_allocator, B_TRUE);
36950f7643c7SGeorge Wilson 	mutex_exit(&pio->io_lock);
36960f7643c7SGeorge Wilson 
3697663207adSDon Brady 	metaslab_class_throttle_unreserve(zio->io_metaslab_class, 1,
3698663207adSDon Brady 	    pio->io_allocator, pio);
36990f7643c7SGeorge Wilson 
37000f7643c7SGeorge Wilson 	/*
37010f7643c7SGeorge Wilson 	 * Call into the pipeline to see if there is more work that
37020f7643c7SGeorge Wilson 	 * needs to be done. If there is work to be done it will be
37030f7643c7SGeorge Wilson 	 * dispatched to another taskq thread.
37040f7643c7SGeorge Wilson 	 */
3705f78cdc34SPaul Dagnelie 	zio_allocate_dispatch(zio->io_spa, pio->io_allocator);
37060f7643c7SGeorge Wilson }
37070f7643c7SGeorge Wilson 
3708e14bb325SJeff Bonwick static int
3709e14bb325SJeff Bonwick zio_done(zio_t *zio)
3710d63d470bSgw {
3711e14bb325SJeff Bonwick 	spa_t *spa = zio->io_spa;
3712e14bb325SJeff Bonwick 	zio_t *lio = zio->io_logical;
3713e14bb325SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
3714e14bb325SJeff Bonwick 	vdev_t *vd = zio->io_vd;
3715e14bb325SJeff Bonwick 	uint64_t psize = zio->io_size;
3716a3f829aeSBill Moore 	zio_t *pio, *pio_next;
37170f7643c7SGeorge Wilson 	zio_link_t *zl = NULL;
3718d63d470bSgw 
3719e14bb325SJeff Bonwick 	/*
3720f5383399SBill Moore 	 * If our children haven't all completed,
3721e14bb325SJeff Bonwick 	 * wait for them and then repeat this pipeline stage.
3722e14bb325SJeff Bonwick 	 */
3723d6e1c446SGeorge Wilson 	if (zio_wait_for_children(zio, ZIO_CHILD_ALL_BITS, ZIO_WAIT_DONE)) {
3724e14bb325SJeff Bonwick 		return (ZIO_PIPELINE_STOP);
3725d6e1c446SGeorge Wilson 	}
3726d63d470bSgw 
37270f7643c7SGeorge Wilson 	/*
37280f7643c7SGeorge Wilson 	 * If the allocation throttle is enabled, then update the accounting.
37290f7643c7SGeorge Wilson 	 * We only track child I/Os that are part of an allocating async
37300f7643c7SGeorge Wilson 	 * write. We must do this since the allocation is performed
37310f7643c7SGeorge Wilson 	 * by the logical I/O but the actual write is done by child I/Os.
37320f7643c7SGeorge Wilson 	 */
37330f7643c7SGeorge Wilson 	if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING &&
37340f7643c7SGeorge Wilson 	    zio->io_child_type == ZIO_CHILD_VDEV) {
3735663207adSDon Brady 		ASSERT(zio->io_metaslab_class != NULL);
3736663207adSDon Brady 		ASSERT(zio->io_metaslab_class->mc_alloc_throttle_enabled);
37370f7643c7SGeorge Wilson 		zio_dva_throttle_done(zio);
37380f7643c7SGeorge Wilson 	}
37390f7643c7SGeorge Wilson 
37400f7643c7SGeorge Wilson 	/*
37410f7643c7SGeorge Wilson 	 * If the allocation throttle is enabled, verify that
37420f7643c7SGeorge Wilson 	 * we have decremented the refcounts for every I/O that was throttled.
37430f7643c7SGeorge Wilson 	 */
37440f7643c7SGeorge Wilson 	if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
37450f7643c7SGeorge Wilson 		ASSERT(zio->io_type == ZIO_TYPE_WRITE);
37460f7643c7SGeorge Wilson 		ASSERT(zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
37470f7643c7SGeorge Wilson 		ASSERT(bp != NULL);
3748663207adSDon Brady 
3749f78cdc34SPaul Dagnelie 		metaslab_group_alloc_verify(spa, zio->io_bp, zio,
3750f78cdc34SPaul Dagnelie 		    zio->io_allocator);
3751e914ace2STim Schumacher 		VERIFY(zfs_refcount_not_held(
3752663207adSDon Brady 		    &zio->io_metaslab_class->mc_alloc_slots[zio->io_allocator],
3753663207adSDon Brady 		    zio));
37540f7643c7SGeorge Wilson 	}
37550f7643c7SGeorge Wilson 
3756e14bb325SJeff Bonwick 	for (int c = 0; c < ZIO_CHILD_TYPES; c++)
3757e14bb325SJeff Bonwick 		for (int w = 0; w < ZIO_WAIT_TYPES; w++)
3758e14bb325SJeff Bonwick 			ASSERT(zio->io_children[c][w] == 0);
3759e14bb325SJeff Bonwick 
37605d7b4d43SMatthew Ahrens 	if (bp != NULL && !BP_IS_EMBEDDED(bp)) {
3761e14bb325SJeff Bonwick 		ASSERT(bp->blk_pad[0] == 0);
3762e14bb325SJeff Bonwick 		ASSERT(bp->blk_pad[1] == 0);
3763e14bb325SJeff Bonwick 		ASSERT(bcmp(bp, &zio->io_bp_copy, sizeof (blkptr_t)) == 0 ||
3764a3f829aeSBill Moore 		    (bp == zio_unique_parent(zio)->io_bp));
3765e14bb325SJeff Bonwick 		if (zio->io_type == ZIO_TYPE_WRITE && !BP_IS_HOLE(bp) &&
3766b24ab676SJeff Bonwick 		    zio->io_bp_override == NULL &&
3767e14bb325SJeff Bonwick 		    !(zio->io_flags & ZIO_FLAG_IO_REPAIR)) {
3768e14bb325SJeff Bonwick 			ASSERT(!BP_SHOULD_BYTESWAP(bp));
3769b24ab676SJeff Bonwick 			ASSERT3U(zio->io_prop.zp_copies, <=, BP_GET_NDVAS(bp));
3770e14bb325SJeff Bonwick 			ASSERT(BP_COUNT_GANG(bp) == 0 ||
3771e14bb325SJeff Bonwick 			    (BP_COUNT_GANG(bp) == BP_GET_NDVAS(bp)));
3772e14bb325SJeff Bonwick 		}
377380901aeaSGeorge Wilson 		if (zio->io_flags & ZIO_FLAG_NOPWRITE)
377480901aeaSGeorge Wilson 			VERIFY(BP_EQUAL(bp, &zio->io_bp_orig));
3775e14bb325SJeff Bonwick 	}
3776fa9e4066Sahrens 
3777e14bb325SJeff Bonwick 	/*
3778b24ab676SJeff Bonwick 	 * If there were child vdev/gang/ddt errors, they apply to us now.
3779e14bb325SJeff Bonwick 	 */
3780e14bb325SJeff Bonwick 	zio_inherit_child_errors(zio, ZIO_CHILD_VDEV);
3781e14bb325SJeff Bonwick 	zio_inherit_child_errors(zio, ZIO_CHILD_GANG);
3782b24ab676SJeff Bonwick 	zio_inherit_child_errors(zio, ZIO_CHILD_DDT);
3783b24ab676SJeff Bonwick 
3784b24ab676SJeff Bonwick 	/*
3785b24ab676SJeff Bonwick 	 * If the I/O on the transformed data was successful, generate any
3786b24ab676SJeff Bonwick 	 * checksum reports now while we still have the transformed data.
3787b24ab676SJeff Bonwick 	 */
3788b24ab676SJeff Bonwick 	if (zio->io_error == 0) {
3789b24ab676SJeff Bonwick 		while (zio->io_cksum_report != NULL) {
3790b24ab676SJeff Bonwick 			zio_cksum_report_t *zcr = zio->io_cksum_report;
3791b24ab676SJeff Bonwick 			uint64_t align = zcr->zcr_align;
3792b24ab676SJeff Bonwick 			uint64_t asize = P2ROUNDUP(psize, align);
3793770499e1SDan Kimmel 			char *abuf = NULL;
3794770499e1SDan Kimmel 			abd_t *adata = zio->io_abd;
3795b24ab676SJeff Bonwick 
3796b24ab676SJeff Bonwick 			if (asize != psize) {
3797770499e1SDan Kimmel 				adata = abd_alloc_linear(asize, B_TRUE);
3798770499e1SDan Kimmel 				abd_copy(adata, zio->io_abd, psize);
3799770499e1SDan Kimmel 				abd_zero_off(adata, psize, asize - psize);
3800b24ab676SJeff Bonwick 			}
3801b24ab676SJeff Bonwick 
3802770499e1SDan Kimmel 			if (adata != NULL)
3803770499e1SDan Kimmel 				abuf = abd_borrow_buf_copy(adata, asize);
3804770499e1SDan Kimmel 
3805b24ab676SJeff Bonwick 			zio->io_cksum_report = zcr->zcr_next;
3806b24ab676SJeff Bonwick 			zcr->zcr_next = NULL;
3807b24ab676SJeff Bonwick 			zcr->zcr_finish(zcr, abuf);
3808b24ab676SJeff Bonwick 			zfs_ereport_free_checksum(zcr);
3809b24ab676SJeff Bonwick 
3810770499e1SDan Kimmel 			if (adata != NULL)
3811770499e1SDan Kimmel 				abd_return_buf(adata, abuf, asize);
3812770499e1SDan Kimmel 
3813b24ab676SJeff Bonwick 			if (asize != psize)
3814770499e1SDan Kimmel 				abd_free(adata);
3815b24ab676SJeff Bonwick 		}
3816b24ab676SJeff Bonwick 	}
3817e14bb325SJeff Bonwick 
3818e14bb325SJeff Bonwick 	zio_pop_transforms(zio);	/* note: may set zio->io_error */
3819e14bb325SJeff Bonwick 
3820e14bb325SJeff Bonwick 	vdev_stat_update(zio, psize);
3821e14bb325SJeff Bonwick 
3822e14bb325SJeff Bonwick 	if (zio->io_error) {
3823e14bb325SJeff Bonwick 		/*
3824e14bb325SJeff Bonwick 		 * If this I/O is attached to a particular vdev,
3825e14bb325SJeff Bonwick 		 * generate an error message describing the I/O failure
3826e14bb325SJeff Bonwick 		 * at the block level.  We ignore these errors if the
3827e14bb325SJeff Bonwick 		 * device is currently unavailable.
3828e14bb325SJeff Bonwick 		 */
3829e14bb325SJeff Bonwick 		if (zio->io_error != ECKSUM && vd != NULL && !vdev_is_dead(vd))
3830e14bb325SJeff Bonwick 			zfs_ereport_post(FM_EREPORT_ZFS_IO, spa, vd, zio, 0, 0);
3831e14bb325SJeff Bonwick 
38328f18d1faSGeorge Wilson 		if ((zio->io_error == EIO || !(zio->io_flags &
38338f18d1faSGeorge Wilson 		    (ZIO_FLAG_SPECULATIVE | ZIO_FLAG_DONT_PROPAGATE))) &&
38348f18d1faSGeorge Wilson 		    zio == lio) {
3835e14bb325SJeff Bonwick 			/*
3836e14bb325SJeff Bonwick 			 * For logical I/O requests, tell the SPA to log the
3837e14bb325SJeff Bonwick 			 * error and generate a logical data ereport.
3838e14bb325SJeff Bonwick 			 */
3839e14bb325SJeff Bonwick 			spa_log_error(spa, zio);
3840e14bb325SJeff Bonwick 			zfs_ereport_post(FM_EREPORT_ZFS_DATA, spa, NULL, zio,
3841e14bb325SJeff Bonwick 			    0, 0);
3842e14bb325SJeff Bonwick 		}
3843e14bb325SJeff Bonwick 	}
3844fa9e4066Sahrens 
3845e14bb325SJeff Bonwick 	if (zio->io_error && zio == lio) {
3846e14bb325SJeff Bonwick 		/*
3847e14bb325SJeff Bonwick 		 * Determine whether zio should be reexecuted.  This will
3848e14bb325SJeff Bonwick 		 * propagate all the way to the root via zio_notify_parent().
3849e14bb325SJeff Bonwick 		 */
3850e14bb325SJeff Bonwick 		ASSERT(vd == NULL && bp != NULL);
3851b24ab676SJeff Bonwick 		ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
3852e14bb325SJeff Bonwick 
3853b24ab676SJeff Bonwick 		if (IO_IS_ALLOCATING(zio) &&
3854b24ab676SJeff Bonwick 		    !(zio->io_flags & ZIO_FLAG_CANFAIL)) {
3855e14bb325SJeff Bonwick 			if (zio->io_error != ENOSPC)
3856e14bb325SJeff Bonwick 				zio->io_reexecute |= ZIO_REEXECUTE_NOW;
3857e14bb325SJeff Bonwick 			else
3858e14bb325SJeff Bonwick 				zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
3859b24ab676SJeff Bonwick 		}
3860e14bb325SJeff Bonwick 
3861e14bb325SJeff Bonwick 		if ((zio->io_type == ZIO_TYPE_READ ||
3862e14bb325SJeff Bonwick 		    zio->io_type == ZIO_TYPE_FREE) &&
386344ecc532SGeorge Wilson 		    !(zio->io_flags & ZIO_FLAG_SCAN_THREAD) &&
3864e14bb325SJeff Bonwick 		    zio->io_error == ENXIO &&
3865b16da2e2SGeorge Wilson 		    spa_load_state(spa) == SPA_LOAD_NONE &&
3866e14bb325SJeff Bonwick 		    spa_get_failmode(spa) != ZIO_FAILURE_MODE_CONTINUE)
3867e14bb325SJeff Bonwick 			zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
3868e14bb325SJeff Bonwick 
3869e14bb325SJeff Bonwick 		if (!(zio->io_flags & ZIO_FLAG_CANFAIL) && !zio->io_reexecute)
3870e14bb325SJeff Bonwick 			zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
387122fe2c88SJonathan Adams 
387222fe2c88SJonathan Adams 		/*
387322fe2c88SJonathan Adams 		 * Here is a possibly good place to attempt to do
387422fe2c88SJonathan Adams 		 * either combinatorial reconstruction or error correction
387522fe2c88SJonathan Adams 		 * based on checksums.  It also might be a good place
387622fe2c88SJonathan Adams 		 * to send out preliminary ereports before we suspend
387722fe2c88SJonathan Adams 		 * processing.
387822fe2c88SJonathan Adams 		 */
3879d63d470bSgw 	}
3880d63d470bSgw 
388167bd71c6Sperrin 	/*
3882e14bb325SJeff Bonwick 	 * If there were logical child errors, they apply to us now.
3883e14bb325SJeff Bonwick 	 * We defer this until now to avoid conflating logical child
3884e14bb325SJeff Bonwick 	 * errors with errors that happened to the zio itself when
3885e14bb325SJeff Bonwick 	 * updating vdev stats and reporting FMA events above.
388667bd71c6Sperrin 	 */
3887e14bb325SJeff Bonwick 	zio_inherit_child_errors(zio, ZIO_CHILD_LOGICAL);
38888654d025Sperrin 
3889b24ab676SJeff Bonwick 	if ((zio->io_error || zio->io_reexecute) &&
3890b24ab676SJeff Bonwick 	    IO_IS_ALLOCATING(zio) && zio->io_gang_leader == zio &&
389180901aeaSGeorge Wilson 	    !(zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE)))
3892f5383399SBill Moore 		zio_dva_unallocate(zio, zio->io_gang_tree, bp);
3893f5383399SBill Moore 
3894f5383399SBill Moore 	zio_gang_tree_free(&zio->io_gang_tree);
3895f5383399SBill Moore 
389633a372edSGeorge Wilson 	/*
389733a372edSGeorge Wilson 	 * Godfather I/Os should never suspend.
389833a372edSGeorge Wilson 	 */
389933a372edSGeorge Wilson 	if ((zio->io_flags & ZIO_FLAG_GODFATHER) &&
390033a372edSGeorge Wilson 	    (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND))
390133a372edSGeorge Wilson 		zio->io_reexecute = 0;
390233a372edSGeorge Wilson 
390333a372edSGeorge Wilson 	if (zio->io_reexecute) {
3904e14bb325SJeff Bonwick 		/*
3905e14bb325SJeff Bonwick 		 * This is a logical I/O that wants to reexecute.
3906e14bb325SJeff Bonwick 		 *
3907e14bb325SJeff Bonwick 		 * Reexecute is top-down.  When an i/o fails, if it's not
3908e14bb325SJeff Bonwick 		 * the root, it simply notifies its parent and sticks around.
3909e14bb325SJeff Bonwick 		 * The parent, seeing that it still has children in zio_done(),
3910e14bb325SJeff Bonwick 		 * does the same.  This percolates all the way up to the root.
3911e14bb325SJeff Bonwick 		 * The root i/o will reexecute or suspend the entire tree.
3912e14bb325SJeff Bonwick 		 *
3913e14bb325SJeff Bonwick 		 * This approach ensures that zio_reexecute() honors
3914e14bb325SJeff Bonwick 		 * all the original i/o dependency relationships, e.g.
3915e14bb325SJeff Bonwick 		 * parents not executing until children are ready.
3916e14bb325SJeff Bonwick 		 */
3917e14bb325SJeff Bonwick 		ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
3918fa9e4066Sahrens 
3919f5383399SBill Moore 		zio->io_gang_leader = NULL;
3920e14bb325SJeff Bonwick 
3921a3f829aeSBill Moore 		mutex_enter(&zio->io_lock);
3922a3f829aeSBill Moore 		zio->io_state[ZIO_WAIT_DONE] = 1;
3923a3f829aeSBill Moore 		mutex_exit(&zio->io_lock);
3924a3f829aeSBill Moore 
392554d692b7SGeorge Wilson 		/*
392654d692b7SGeorge Wilson 		 * "The Godfather" I/O monitors its children but is
392754d692b7SGeorge Wilson 		 * not a true parent to them. It will track them through
392854d692b7SGeorge Wilson 		 * the pipeline but severs its ties whenever they get into
392954d692b7SGeorge Wilson 		 * trouble (e.g. suspended). This allows "The Godfather"
393054d692b7SGeorge Wilson 		 * I/O to return status without blocking.
393154d692b7SGeorge Wilson 		 */
39320f7643c7SGeorge Wilson 		zl = NULL;
39330f7643c7SGeorge Wilson 		for (pio = zio_walk_parents(zio, &zl); pio != NULL;
39340f7643c7SGeorge Wilson 		    pio = pio_next) {
39350f7643c7SGeorge Wilson 			zio_link_t *remove_zl = zl;
39360f7643c7SGeorge Wilson 			pio_next = zio_walk_parents(zio, &zl);
393754d692b7SGeorge Wilson 
393854d692b7SGeorge Wilson 			if ((pio->io_flags & ZIO_FLAG_GODFATHER) &&
393954d692b7SGeorge Wilson 			    (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND)) {
39400f7643c7SGeorge Wilson 				zio_remove_child(pio, zio, remove_zl);
394154d692b7SGeorge Wilson 				zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
394254d692b7SGeorge Wilson 			}
394354d692b7SGeorge Wilson 		}
394454d692b7SGeorge Wilson 
3945a3f829aeSBill Moore 		if ((pio = zio_unique_parent(zio)) != NULL) {
3946e14bb325SJeff Bonwick 			/*
3947e14bb325SJeff Bonwick 			 * We're not a root i/o, so there's nothing to do
3948e14bb325SJeff Bonwick 			 * but notify our parent.  Don't propagate errors
3949e14bb325SJeff Bonwick 			 * upward since we haven't permanently failed yet.
3950e14bb325SJeff Bonwick 			 */
395133a372edSGeorge Wilson 			ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER));
3952e14bb325SJeff Bonwick 			zio->io_flags |= ZIO_FLAG_DONT_PROPAGATE;
3953e14bb325SJeff Bonwick 			zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
3954e14bb325SJeff Bonwick 		} else if (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND) {
3955e14bb325SJeff Bonwick 			/*
3956e14bb325SJeff Bonwick 			 * We'd fail again if we reexecuted now, so suspend
3957e14bb325SJeff Bonwick 			 * until conditions improve (e.g. device comes online).
3958e14bb325SJeff Bonwick 			 */
3959e0f1c0afSOlaf Faaland 			zio_suspend(zio->io_spa, zio, ZIO_SUSPEND_IOERR);
3960e14bb325SJeff Bonwick 		} else {
3961e14bb325SJeff Bonwick 			/*
3962e14bb325SJeff Bonwick 			 * Reexecution is potentially a huge amount of work.
3963e14bb325SJeff Bonwick 			 * Hand it off to the otherwise-unused claim taskq.
3964e14bb325SJeff Bonwick 			 */
39655aeb9474SGarrett D'Amore 			ASSERT(zio->io_tqent.tqent_next == NULL);
3966ec94d322SAdam Leventhal 			spa_taskq_dispatch_ent(spa, ZIO_TYPE_CLAIM,
3967ec94d322SAdam Leventhal 			    ZIO_TASKQ_ISSUE, (task_func_t *)zio_reexecute, zio,
3968ec94d322SAdam Leventhal 			    0, &zio->io_tqent);
3969e14bb325SJeff Bonwick 		}
3970e14bb325SJeff Bonwick 		return (ZIO_PIPELINE_STOP);
3971fa9e4066Sahrens 	}
3972fa9e4066Sahrens 
3973b24ab676SJeff Bonwick 	ASSERT(zio->io_child_count == 0);
397433a372edSGeorge Wilson 	ASSERT(zio->io_reexecute == 0);
3975e14bb325SJeff Bonwick 	ASSERT(zio->io_error == 0 || (zio->io_flags & ZIO_FLAG_CANFAIL));
3976fa9e4066Sahrens 
3977b24ab676SJeff Bonwick 	/*
3978b24ab676SJeff Bonwick 	 * Report any checksum errors, since the I/O is complete.
3979b24ab676SJeff Bonwick 	 */
398022fe2c88SJonathan Adams 	while (zio->io_cksum_report != NULL) {
3981b24ab676SJeff Bonwick 		zio_cksum_report_t *zcr = zio->io_cksum_report;
3982b24ab676SJeff Bonwick 		zio->io_cksum_report = zcr->zcr_next;
3983b24ab676SJeff Bonwick 		zcr->zcr_next = NULL;
3984b24ab676SJeff Bonwick 		zcr->zcr_finish(zcr, NULL);
3985b24ab676SJeff Bonwick 		zfs_ereport_free_checksum(zcr);
398622fe2c88SJonathan Adams 	}
398722fe2c88SJonathan Adams 
3988a3f829aeSBill Moore 	/*
3989a3f829aeSBill Moore 	 * It is the responsibility of the done callback to ensure that this
3990a3f829aeSBill Moore 	 * particular zio is no longer discoverable for adoption, and as
3991a3f829aeSBill Moore 	 * such, cannot acquire any new parents.
3992a3f829aeSBill Moore 	 */
3993e14bb325SJeff Bonwick 	if (zio->io_done)
3994e14bb325SJeff Bonwick 		zio->io_done(zio);
3995fa9e4066Sahrens 
3996a3f829aeSBill Moore 	mutex_enter(&zio->io_lock);
3997a3f829aeSBill Moore 	zio->io_state[ZIO_WAIT_DONE] = 1;
3998a3f829aeSBill Moore 	mutex_exit(&zio->io_lock);
3999fa9e4066Sahrens 
40000f7643c7SGeorge Wilson 	zl = NULL;
40010f7643c7SGeorge Wilson 	for (pio = zio_walk_parents(zio, &zl); pio != NULL; pio = pio_next) {
40020f7643c7SGeorge Wilson 		zio_link_t *remove_zl = zl;
40030f7643c7SGeorge Wilson 		pio_next = zio_walk_parents(zio, &zl);
40040f7643c7SGeorge Wilson 		zio_remove_child(pio, zio, remove_zl);
4005e14bb325SJeff Bonwick 		zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
4006e14bb325SJeff Bonwick 	}
4007fa9e4066Sahrens 
4008e14bb325SJeff Bonwick 	if (zio->io_waiter != NULL) {
4009e14bb325SJeff Bonwick 		mutex_enter(&zio->io_lock);
4010e14bb325SJeff Bonwick 		zio->io_executor = NULL;
4011e14bb325SJeff Bonwick 		cv_broadcast(&zio->io_cv);
4012e14bb325SJeff Bonwick 		mutex_exit(&zio->io_lock);
4013e14bb325SJeff Bonwick 	} else {
4014e14bb325SJeff Bonwick 		zio_destroy(zio);
4015e14bb325SJeff Bonwick 	}
4016fa9e4066Sahrens 
4017e14bb325SJeff Bonwick 	return (ZIO_PIPELINE_STOP);
4018fa9e4066Sahrens }
401946341222Sperrin 
402046341222Sperrin /*
4021e14bb325SJeff Bonwick  * ==========================================================================
4022e14bb325SJeff Bonwick  * I/O pipeline definition
4023e14bb325SJeff Bonwick  * ==========================================================================
402446341222Sperrin  */
4025b24ab676SJeff Bonwick static zio_pipe_stage_t *zio_pipeline[] = {
4026e14bb325SJeff Bonwick 	NULL,
4027e14bb325SJeff Bonwick 	zio_read_bp_init,
40280f7643c7SGeorge Wilson 	zio_write_bp_init,
4029b24ab676SJeff Bonwick 	zio_free_bp_init,
4030b24ab676SJeff Bonwick 	zio_issue_async,
40310f7643c7SGeorge Wilson 	zio_write_compress,
4032e14bb325SJeff Bonwick 	zio_checksum_generate,
403380901aeaSGeorge Wilson 	zio_nop_write,
4034b24ab676SJeff Bonwick 	zio_ddt_read_start,
4035b24ab676SJeff Bonwick 	zio_ddt_read_done,
4036b24ab676SJeff Bonwick 	zio_ddt_write,
4037b24ab676SJeff Bonwick 	zio_ddt_free,
4038e14bb325SJeff Bonwick 	zio_gang_assemble,
4039e14bb325SJeff Bonwick 	zio_gang_issue,
40400f7643c7SGeorge Wilson 	zio_dva_throttle,
4041e14bb325SJeff Bonwick 	zio_dva_allocate,
4042e14bb325SJeff Bonwick 	zio_dva_free,
4043e14bb325SJeff Bonwick 	zio_dva_claim,
4044e14bb325SJeff Bonwick 	zio_ready,
4045e14bb325SJeff Bonwick 	zio_vdev_io_start,
4046e14bb325SJeff Bonwick 	zio_vdev_io_done,
4047e14bb325SJeff Bonwick 	zio_vdev_io_assess,
4048e14bb325SJeff Bonwick 	zio_checksum_verify,
4049e14bb325SJeff Bonwick 	zio_done
4050e14bb325SJeff Bonwick };
4051ad135b5dSChristopher Siden 
4052ad135b5dSChristopher Siden 
4053ad135b5dSChristopher Siden 
4054ad135b5dSChristopher Siden 
4055a2cdcdd2SPaul Dagnelie /*
4056a2cdcdd2SPaul Dagnelie  * Compare two zbookmark_phys_t's to see which we would reach first in a
4057a2cdcdd2SPaul Dagnelie  * pre-order traversal of the object tree.
4058a2cdcdd2SPaul Dagnelie  *
4059a2cdcdd2SPaul Dagnelie  * This is simple in every case aside from the meta-dnode object. For all other
4060a2cdcdd2SPaul Dagnelie  * objects, we traverse them in order (object 1 before object 2, and so on).
4061a2cdcdd2SPaul Dagnelie  * However, all of these objects are traversed while traversing object 0, since
4062a2cdcdd2SPaul Dagnelie  * the data it points to is the list of objects.  Thus, we need to convert to a
4063a2cdcdd2SPaul Dagnelie  * canonical representation so we can compare meta-dnode bookmarks to
4064a2cdcdd2SPaul Dagnelie  * non-meta-dnode bookmarks.
4065a2cdcdd2SPaul Dagnelie  *
4066a2cdcdd2SPaul Dagnelie  * We do this by calculating "equivalents" for each field of the zbookmark.
4067a2cdcdd2SPaul Dagnelie  * zbookmarks outside of the meta-dnode use their own object and level, and
4068a2cdcdd2SPaul Dagnelie  * calculate the level 0 equivalent (the first L0 blkid that is contained in the
4069a2cdcdd2SPaul Dagnelie  * blocks this bookmark refers to) by multiplying their blkid by their span
4070a2cdcdd2SPaul Dagnelie  * (the number of L0 blocks contained within one block at their level).
4071a2cdcdd2SPaul Dagnelie  * zbookmarks inside the meta-dnode calculate their object equivalent
4072a2cdcdd2SPaul Dagnelie  * (which is L0equiv * dnodes per data block), use 0 for their L0equiv, and use
4073a2cdcdd2SPaul Dagnelie  * level + 1<<31 (any value larger than a level could ever be) for their level.
4074a2cdcdd2SPaul Dagnelie  * This causes them to always compare before a bookmark in their object
4075a2cdcdd2SPaul Dagnelie  * equivalent, compare appropriately to bookmarks in other objects, and to
4076a2cdcdd2SPaul Dagnelie  * compare appropriately to other bookmarks in the meta-dnode.
4077a2cdcdd2SPaul Dagnelie  */
4078a2cdcdd2SPaul Dagnelie int
4079a2cdcdd2SPaul Dagnelie zbookmark_compare(uint16_t dbss1, uint8_t ibs1, uint16_t dbss2, uint8_t ibs2,
4080a2cdcdd2SPaul Dagnelie     const zbookmark_phys_t *zb1, const zbookmark_phys_t *zb2)
4081a2cdcdd2SPaul Dagnelie {
4082a2cdcdd2SPaul Dagnelie 	/*
4083a2cdcdd2SPaul Dagnelie 	 * These variables represent the "equivalent" values for the zbookmark,
4084a2cdcdd2SPaul Dagnelie 	 * after converting zbookmarks inside the meta dnode to their
4085a2cdcdd2SPaul Dagnelie 	 * normal-object equivalents.
4086a2cdcdd2SPaul Dagnelie 	 */
4087a2cdcdd2SPaul Dagnelie 	uint64_t zb1obj, zb2obj;
4088a2cdcdd2SPaul Dagnelie 	uint64_t zb1L0, zb2L0;
4089a2cdcdd2SPaul Dagnelie 	uint64_t zb1level, zb2level;
4090ad135b5dSChristopher Siden 
4091a2cdcdd2SPaul Dagnelie 	if (zb1->zb_object == zb2->zb_object &&
4092a2cdcdd2SPaul Dagnelie 	    zb1->zb_level == zb2->zb_level &&
4093a2cdcdd2SPaul Dagnelie 	    zb1->zb_blkid == zb2->zb_blkid)
4094a2cdcdd2SPaul Dagnelie 		return (0);
4095a2cdcdd2SPaul Dagnelie 
4096a2cdcdd2SPaul Dagnelie 	/*
4097a2cdcdd2SPaul Dagnelie 	 * BP_SPANB calculates the span in blocks.
4098a2cdcdd2SPaul Dagnelie 	 */
4099a2cdcdd2SPaul Dagnelie 	zb1L0 = (zb1->zb_blkid) * BP_SPANB(ibs1, zb1->zb_level);
4100a2cdcdd2SPaul Dagnelie 	zb2L0 = (zb2->zb_blkid) * BP_SPANB(ibs2, zb2->zb_level);
4101ad135b5dSChristopher Siden 
4102ad135b5dSChristopher Siden 	if (zb1->zb_object == DMU_META_DNODE_OBJECT) {
4103a2cdcdd2SPaul Dagnelie 		zb1obj = zb1L0 * (dbss1 << (SPA_MINBLOCKSHIFT - DNODE_SHIFT));
4104a2cdcdd2SPaul Dagnelie 		zb1L0 = 0;
4105a2cdcdd2SPaul Dagnelie 		zb1level = zb1->zb_level + COMPARE_META_LEVEL;
4106a2cdcdd2SPaul Dagnelie 	} else {
4107a2cdcdd2SPaul Dagnelie 		zb1obj = zb1->zb_object;
4108a2cdcdd2SPaul Dagnelie 		zb1level = zb1->zb_level;
4109ad135b5dSChristopher Siden 	}
4110ad135b5dSChristopher Siden 
4111a2cdcdd2SPaul Dagnelie 	if (zb2->zb_object == DMU_META_DNODE_OBJECT) {
4112a2cdcdd2SPaul Dagnelie 		zb2obj = zb2L0 * (dbss2 << (SPA_MINBLOCKSHIFT - DNODE_SHIFT));
4113a2cdcdd2SPaul Dagnelie 		zb2L0 = 0;
4114a2cdcdd2SPaul Dagnelie 		zb2level = zb2->zb_level + COMPARE_META_LEVEL;
4115a2cdcdd2SPaul Dagnelie 	} else {
4116a2cdcdd2SPaul Dagnelie 		zb2obj = zb2->zb_object;
4117a2cdcdd2SPaul Dagnelie 		zb2level = zb2->zb_level;
4118a2cdcdd2SPaul Dagnelie 	}
4119a2cdcdd2SPaul Dagnelie 
4120a2cdcdd2SPaul Dagnelie 	/* Now that we have a canonical representation, do the comparison. */
4121a2cdcdd2SPaul Dagnelie 	if (zb1obj != zb2obj)
4122a2cdcdd2SPaul Dagnelie 		return (zb1obj < zb2obj ? -1 : 1);
4123a2cdcdd2SPaul Dagnelie 	else if (zb1L0 != zb2L0)
4124a2cdcdd2SPaul Dagnelie 		return (zb1L0 < zb2L0 ? -1 : 1);
4125a2cdcdd2SPaul Dagnelie 	else if (zb1level != zb2level)
4126a2cdcdd2SPaul Dagnelie 		return (zb1level > zb2level ? -1 : 1);
4127a2cdcdd2SPaul Dagnelie 	/*
4128a2cdcdd2SPaul Dagnelie 	 * This can (theoretically) happen if the bookmarks have the same object
4129a2cdcdd2SPaul Dagnelie 	 * and level, but different blkids, if the block sizes are not the same.
4130a2cdcdd2SPaul Dagnelie 	 * There is presently no way to change the indirect block sizes
4131a2cdcdd2SPaul Dagnelie 	 */
4132a2cdcdd2SPaul Dagnelie 	return (0);
4133a2cdcdd2SPaul Dagnelie }
4134a2cdcdd2SPaul Dagnelie 
4135a2cdcdd2SPaul Dagnelie /*
4136a2cdcdd2SPaul Dagnelie  *  This function checks the following: given that last_block is the place that
4137a2cdcdd2SPaul Dagnelie  *  our traversal stopped last time, does that guarantee that we've visited
4138a2cdcdd2SPaul Dagnelie  *  every node under subtree_root?  Therefore, we can't just use the raw output
4139a2cdcdd2SPaul Dagnelie  *  of zbookmark_compare.  We have to pass in a modified version of
4140a2cdcdd2SPaul Dagnelie  *  subtree_root; by incrementing the block id, and then checking whether
4141a2cdcdd2SPaul Dagnelie  *  last_block is before or equal to that, we can tell whether or not having
4142a2cdcdd2SPaul Dagnelie  *  visited last_block implies that all of subtree_root's children have been
4143a2cdcdd2SPaul Dagnelie  *  visited.
4144a2cdcdd2SPaul Dagnelie  */
4145a2cdcdd2SPaul Dagnelie boolean_t
4146a2cdcdd2SPaul Dagnelie zbookmark_subtree_completed(const dnode_phys_t *dnp,
4147a2cdcdd2SPaul Dagnelie     const zbookmark_phys_t *subtree_root, const zbookmark_phys_t *last_block)
4148a2cdcdd2SPaul Dagnelie {
4149a2cdcdd2SPaul Dagnelie 	zbookmark_phys_t mod_zb = *subtree_root;
4150a2cdcdd2SPaul Dagnelie 	mod_zb.zb_blkid++;
4151a2cdcdd2SPaul Dagnelie 	ASSERT(last_block->zb_level == 0);
4152a2cdcdd2SPaul Dagnelie 
4153a2cdcdd2SPaul Dagnelie 	/* The objset_phys_t isn't before anything. */
4154a2cdcdd2SPaul Dagnelie 	if (dnp == NULL)
4155ad135b5dSChristopher Siden 		return (B_FALSE);
4156a2cdcdd2SPaul Dagnelie 
4157a2cdcdd2SPaul Dagnelie 	/*
4158a2cdcdd2SPaul Dagnelie 	 * We pass in 1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT) for the
4159a2cdcdd2SPaul Dagnelie 	 * data block size in sectors, because that variable is only used if
4160a2cdcdd2SPaul Dagnelie 	 * the bookmark refers to a block in the meta-dnode.  Since we don't
4161a2cdcdd2SPaul Dagnelie 	 * know without examining it what object it refers to, and there's no
4162a2cdcdd2SPaul Dagnelie 	 * harm in passing in this value in other cases, we always pass it in.
4163a2cdcdd2SPaul Dagnelie 	 *
4164a2cdcdd2SPaul Dagnelie 	 * We pass in 0 for the indirect block size shift because zb2 must be
4165a2cdcdd2SPaul Dagnelie 	 * level 0.  The indirect block size is only used to calculate the span
4166a2cdcdd2SPaul Dagnelie 	 * of the bookmark, but since the bookmark must be level 0, the span is
4167a2cdcdd2SPaul Dagnelie 	 * always 1, so the math works out.
4168a2cdcdd2SPaul Dagnelie 	 *
4169a2cdcdd2SPaul Dagnelie 	 * If you make changes to how the zbookmark_compare code works, be sure
4170a2cdcdd2SPaul Dagnelie 	 * to make sure that this code still works afterwards.
4171a2cdcdd2SPaul Dagnelie 	 */
4172a2cdcdd2SPaul Dagnelie 	return (zbookmark_compare(dnp->dn_datablkszsec, dnp->dn_indblkshift,
4173a2cdcdd2SPaul Dagnelie 	    1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT), 0, &mod_zb,
4174a2cdcdd2SPaul Dagnelie 	    last_block) <= 0);
4175ad135b5dSChristopher Siden }
4176