xref: /illumos-gate/usr/src/uts/common/fs/zfs/zio.c (revision 084fd14f7c3336eb67ee283cabad2da8998b00d6)
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>
36*084fd14fSBrian Behlendorf #include <sys/vdev_trim.h>
37fa9e4066Sahrens #include <sys/zio_impl.h>
38fa9e4066Sahrens #include <sys/zio_compress.h>
39fa9e4066Sahrens #include <sys/zio_checksum.h>
40b24ab676SJeff Bonwick #include <sys/dmu_objset.h>
41b24ab676SJeff Bonwick #include <sys/arc.h>
42b24ab676SJeff Bonwick #include <sys/ddt.h>
435d7b4d43SMatthew Ahrens #include <sys/blkptr.h>
4443466aaeSMax Grossman #include <sys/zfeature.h>
45a3874b8bSToomas Soome #include <sys/dsl_scan.h>
460f7643c7SGeorge Wilson #include <sys/metaslab_impl.h>
47770499e1SDan Kimmel #include <sys/abd.h>
48f78cdc34SPaul Dagnelie #include <sys/cityhash.h>
49eb633035STom Caputi #include <sys/dsl_crypt.h>
50fa9e4066Sahrens 
51fa9e4066Sahrens /*
52fa9e4066Sahrens  * ==========================================================================
53fa9e4066Sahrens  * I/O type descriptions
54fa9e4066Sahrens  * ==========================================================================
55fa9e4066Sahrens  */
5669962b56SMatthew Ahrens const char *zio_type_name[ZIO_TYPES] = {
5780eb36f2SGeorge Wilson 	"zio_null", "zio_read", "zio_write", "zio_free", "zio_claim",
58*084fd14fSBrian Behlendorf 	"zio_ioctl", "z_trim"
5980eb36f2SGeorge Wilson };
60fa9e4066Sahrens 
610f7643c7SGeorge Wilson boolean_t zio_dva_throttle_enabled = B_TRUE;
620f7643c7SGeorge Wilson 
63fa9e4066Sahrens /*
64fa9e4066Sahrens  * ==========================================================================
65fa9e4066Sahrens  * I/O kmem caches
66fa9e4066Sahrens  * ==========================================================================
67fa9e4066Sahrens  */
68ccae0b50Seschrock kmem_cache_t *zio_cache;
69a3f829aeSBill Moore kmem_cache_t *zio_link_cache;
70fa9e4066Sahrens kmem_cache_t *zio_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
71ad23a2dbSjohansen kmem_cache_t *zio_data_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
72ad23a2dbSjohansen 
73ad23a2dbSjohansen #ifdef _KERNEL
74ad23a2dbSjohansen extern vmem_t *zio_alloc_arena;
75ad23a2dbSjohansen #endif
76fa9e4066Sahrens 
77738f37bcSGeorge Wilson #define	ZIO_PIPELINE_CONTINUE		0x100
78738f37bcSGeorge Wilson #define	ZIO_PIPELINE_STOP		0x101
79738f37bcSGeorge Wilson 
80a2cdcdd2SPaul Dagnelie #define	BP_SPANB(indblkshift, level) \
81a2cdcdd2SPaul Dagnelie 	(((uint64_t)1) << ((level) * ((indblkshift) - SPA_BLKPTRSHIFT)))
82a2cdcdd2SPaul Dagnelie #define	COMPARE_META_LEVEL	0x80000000ul
8301f55e48SGeorge Wilson /*
8401f55e48SGeorge Wilson  * The following actions directly effect the spa's sync-to-convergence logic.
8501f55e48SGeorge Wilson  * The values below define the sync pass when we start performing the action.
8601f55e48SGeorge Wilson  * Care should be taken when changing these values as they directly impact
8701f55e48SGeorge Wilson  * spa_sync() performance. Tuning these values may introduce subtle performance
8801f55e48SGeorge Wilson  * pathologies and should only be done in the context of performance analysis.
8901f55e48SGeorge Wilson  * These tunables will eventually be removed and replaced with #defines once
9001f55e48SGeorge Wilson  * enough analysis has been done to determine optimal values.
9101f55e48SGeorge Wilson  *
9201f55e48SGeorge Wilson  * The 'zfs_sync_pass_deferred_free' pass must be greater than 1 to ensure that
9301f55e48SGeorge Wilson  * regular blocks are not deferred.
9401f55e48SGeorge Wilson  */
9501f55e48SGeorge Wilson int zfs_sync_pass_deferred_free = 2; /* defer frees starting in this pass */
9601f55e48SGeorge Wilson int zfs_sync_pass_dont_compress = 5; /* don't compress starting in this pass */
9701f55e48SGeorge Wilson int zfs_sync_pass_rewrite = 2; /* rewrite new bps starting in this pass */
9801f55e48SGeorge Wilson 
990a4e9518Sgw /*
100e14bb325SJeff Bonwick  * An allocating zio is one that either currently has the DVA allocate
101e14bb325SJeff Bonwick  * stage set or will have it later in its lifetime.
1020a4e9518Sgw  */
103b24ab676SJeff Bonwick #define	IO_IS_ALLOCATING(zio) ((zio)->io_orig_pipeline & ZIO_STAGE_DVA_ALLOCATE)
104b24ab676SJeff Bonwick 
10535a5a358SJonathan Adams boolean_t	zio_requeue_io_start_cut_in_line = B_TRUE;
10635a5a358SJonathan Adams 
107b24ab676SJeff Bonwick #ifdef ZFS_DEBUG
108b24ab676SJeff Bonwick int zio_buf_debug_limit = 16384;
109b24ab676SJeff Bonwick #else
110b24ab676SJeff Bonwick int zio_buf_debug_limit = 0;
111b24ab676SJeff Bonwick #endif
1120a4e9518Sgw 
1130f7643c7SGeorge Wilson static void zio_taskq_dispatch(zio_t *, zio_taskq_type_t, boolean_t);
1140f7643c7SGeorge Wilson 
115fa9e4066Sahrens void
116fa9e4066Sahrens zio_init(void)
117fa9e4066Sahrens {
118fa9e4066Sahrens 	size_t c;
119ad23a2dbSjohansen 	vmem_t *data_alloc_arena = NULL;
120ad23a2dbSjohansen 
121ad23a2dbSjohansen #ifdef _KERNEL
122ad23a2dbSjohansen 	data_alloc_arena = zio_alloc_arena;
123ad23a2dbSjohansen #endif
124a3f829aeSBill Moore 	zio_cache = kmem_cache_create("zio_cache",
125a3f829aeSBill Moore 	    sizeof (zio_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
126a3f829aeSBill Moore 	zio_link_cache = kmem_cache_create("zio_link_cache",
127a3f829aeSBill Moore 	    sizeof (zio_link_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
128ccae0b50Seschrock 
129fa9e4066Sahrens 	/*
130fa9e4066Sahrens 	 * For small buffers, we want a cache for each multiple of
131b5152584SMatthew Ahrens 	 * SPA_MINBLOCKSIZE.  For larger buffers, we want a cache
132b5152584SMatthew Ahrens 	 * for each quarter-power of 2.
133fa9e4066Sahrens 	 */
134fa9e4066Sahrens 	for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) {
135fa9e4066Sahrens 		size_t size = (c + 1) << SPA_MINBLOCKSHIFT;
136fa9e4066Sahrens 		size_t p2 = size;
137fa9e4066Sahrens 		size_t align = 0;
138e291592aSJonathan Adams 		size_t cflags = (size > zio_buf_debug_limit) ? KMC_NODEBUG : 0;
139fa9e4066Sahrens 
140de710d24SJosef 'Jeff' Sipek 		while (!ISP2(p2))
141fa9e4066Sahrens 			p2 &= p2 - 1;
142fa9e4066Sahrens 
143cd1c8b85SMatthew Ahrens #ifndef _KERNEL
144cd1c8b85SMatthew Ahrens 		/*
145cd1c8b85SMatthew Ahrens 		 * If we are using watchpoints, put each buffer on its own page,
146cd1c8b85SMatthew Ahrens 		 * to eliminate the performance overhead of trapping to the
147cd1c8b85SMatthew Ahrens 		 * kernel when modifying a non-watched buffer that shares the
148cd1c8b85SMatthew Ahrens 		 * page with a watched buffer.
149cd1c8b85SMatthew Ahrens 		 */
150cd1c8b85SMatthew Ahrens 		if (arc_watch && !IS_P2ALIGNED(size, PAGESIZE))
151cd1c8b85SMatthew Ahrens 			continue;
152cd1c8b85SMatthew Ahrens #endif
153fa9e4066Sahrens 		if (size <= 4 * SPA_MINBLOCKSIZE) {
154fa9e4066Sahrens 			align = SPA_MINBLOCKSIZE;
155cd1c8b85SMatthew Ahrens 		} else if (IS_P2ALIGNED(size, p2 >> 2)) {
156b5152584SMatthew Ahrens 			align = MIN(p2 >> 2, PAGESIZE);
157fa9e4066Sahrens 		}
158fa9e4066Sahrens 
159fa9e4066Sahrens 		if (align != 0) {
160ad23a2dbSjohansen 			char name[36];
1615ad82045Snd 			(void) sprintf(name, "zio_buf_%lu", (ulong_t)size);
162fa9e4066Sahrens 			zio_buf_cache[c] = kmem_cache_create(name, size,
163e291592aSJonathan Adams 			    align, NULL, NULL, NULL, NULL, NULL, cflags);
164ad23a2dbSjohansen 
165e291592aSJonathan Adams 			/*
166e291592aSJonathan Adams 			 * Since zio_data bufs do not appear in crash dumps, we
167e291592aSJonathan Adams 			 * pass KMC_NOTOUCH so that no allocator metadata is
168e291592aSJonathan Adams 			 * stored with the buffers.
169e291592aSJonathan Adams 			 */
170ad23a2dbSjohansen 			(void) sprintf(name, "zio_data_buf_%lu", (ulong_t)size);
171ad23a2dbSjohansen 			zio_data_buf_cache[c] = kmem_cache_create(name, size,
172ad23a2dbSjohansen 			    align, NULL, NULL, NULL, NULL, data_alloc_arena,
173e291592aSJonathan Adams 			    cflags | KMC_NOTOUCH);
174fa9e4066Sahrens 		}
175fa9e4066Sahrens 	}
176fa9e4066Sahrens 
177fa9e4066Sahrens 	while (--c != 0) {
178fa9e4066Sahrens 		ASSERT(zio_buf_cache[c] != NULL);
179fa9e4066Sahrens 		if (zio_buf_cache[c - 1] == NULL)
180fa9e4066Sahrens 			zio_buf_cache[c - 1] = zio_buf_cache[c];
181ad23a2dbSjohansen 
182ad23a2dbSjohansen 		ASSERT(zio_data_buf_cache[c] != NULL);
183ad23a2dbSjohansen 		if (zio_data_buf_cache[c - 1] == NULL)
184ad23a2dbSjohansen 			zio_data_buf_cache[c - 1] = zio_data_buf_cache[c];
185fa9e4066Sahrens 	}
186ea8dc4b6Seschrock 
187ea8dc4b6Seschrock 	zio_inject_init();
188fa9e4066Sahrens }
189fa9e4066Sahrens 
190fa9e4066Sahrens void
191fa9e4066Sahrens zio_fini(void)
192fa9e4066Sahrens {
193fa9e4066Sahrens 	size_t c;
194fa9e4066Sahrens 	kmem_cache_t *last_cache = NULL;
195ad23a2dbSjohansen 	kmem_cache_t *last_data_cache = NULL;
196fa9e4066Sahrens 
197fa9e4066Sahrens 	for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) {
198fa9e4066Sahrens 		if (zio_buf_cache[c] != last_cache) {
199fa9e4066Sahrens 			last_cache = zio_buf_cache[c];
200fa9e4066Sahrens 			kmem_cache_destroy(zio_buf_cache[c]);
201fa9e4066Sahrens 		}
202fa9e4066Sahrens 		zio_buf_cache[c] = NULL;
203ad23a2dbSjohansen 
204ad23a2dbSjohansen 		if (zio_data_buf_cache[c] != last_data_cache) {
205ad23a2dbSjohansen 			last_data_cache = zio_data_buf_cache[c];
206ad23a2dbSjohansen 			kmem_cache_destroy(zio_data_buf_cache[c]);
207ad23a2dbSjohansen 		}
208ad23a2dbSjohansen 		zio_data_buf_cache[c] = NULL;
209fa9e4066Sahrens 	}
210ea8dc4b6Seschrock 
211a3f829aeSBill Moore 	kmem_cache_destroy(zio_link_cache);
212ccae0b50Seschrock 	kmem_cache_destroy(zio_cache);
213ccae0b50Seschrock 
214ea8dc4b6Seschrock 	zio_inject_fini();
215fa9e4066Sahrens }
216fa9e4066Sahrens 
217fa9e4066Sahrens /*
218fa9e4066Sahrens  * ==========================================================================
219fa9e4066Sahrens  * Allocate and free I/O buffers
220fa9e4066Sahrens  * ==========================================================================
221fa9e4066Sahrens  */
222ad23a2dbSjohansen 
223ad23a2dbSjohansen /*
224ad23a2dbSjohansen  * Use zio_buf_alloc to allocate ZFS metadata.  This data will appear in a
225ad23a2dbSjohansen  * crashdump if the kernel panics, so use it judiciously.  Obviously, it's
226ad23a2dbSjohansen  * useful to inspect ZFS metadata, but if possible, we should avoid keeping
227ad23a2dbSjohansen  * excess / transient data in-core during a crashdump.
228ad23a2dbSjohansen  */
229fa9e4066Sahrens void *
230fa9e4066Sahrens zio_buf_alloc(size_t size)
231fa9e4066Sahrens {
232fa9e4066Sahrens 	size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
233fa9e4066Sahrens 
234f63ab3d5SMatthew Ahrens 	VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
235fa9e4066Sahrens 
2361ab7f2deSmaybee 	return (kmem_cache_alloc(zio_buf_cache[c], KM_PUSHPAGE));
237fa9e4066Sahrens }
238fa9e4066Sahrens 
239ad23a2dbSjohansen /*
240ad23a2dbSjohansen  * Use zio_data_buf_alloc to allocate data.  The data will not appear in a
241ad23a2dbSjohansen  * crashdump if the kernel panics.  This exists so that we will limit the amount
242ad23a2dbSjohansen  * of ZFS data that shows up in a kernel crashdump.  (Thus reducing the amount
243ad23a2dbSjohansen  * of kernel heap dumped to disk when the kernel panics)
244ad23a2dbSjohansen  */
245ad23a2dbSjohansen void *
246ad23a2dbSjohansen zio_data_buf_alloc(size_t size)
247ad23a2dbSjohansen {
248ad23a2dbSjohansen 	size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
249ad23a2dbSjohansen 
250f63ab3d5SMatthew Ahrens 	VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
251ad23a2dbSjohansen 
2521ab7f2deSmaybee 	return (kmem_cache_alloc(zio_data_buf_cache[c], KM_PUSHPAGE));
253ad23a2dbSjohansen }
254ad23a2dbSjohansen 
255fa9e4066Sahrens void
256fa9e4066Sahrens zio_buf_free(void *buf, size_t size)
257fa9e4066Sahrens {
258fa9e4066Sahrens 	size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
259fa9e4066Sahrens 
260f63ab3d5SMatthew Ahrens 	VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
261fa9e4066Sahrens 
262fa9e4066Sahrens 	kmem_cache_free(zio_buf_cache[c], buf);
263fa9e4066Sahrens }
264fa9e4066Sahrens 
265ad23a2dbSjohansen void
266ad23a2dbSjohansen zio_data_buf_free(void *buf, size_t size)
267ad23a2dbSjohansen {
268ad23a2dbSjohansen 	size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
269ad23a2dbSjohansen 
270f63ab3d5SMatthew Ahrens 	VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
271ad23a2dbSjohansen 
272ad23a2dbSjohansen 	kmem_cache_free(zio_data_buf_cache[c], buf);
273ad23a2dbSjohansen }
274b3995adbSahrens 
275eb633035STom Caputi /* ARGSUSED */
276eb633035STom Caputi static void
277eb633035STom Caputi zio_abd_free(void *abd, size_t size)
278eb633035STom Caputi {
279eb633035STom Caputi 	abd_free((abd_t *)abd);
280eb633035STom Caputi }
281eb633035STom Caputi 
282fa9e4066Sahrens /*
283fa9e4066Sahrens  * ==========================================================================
284fa9e4066Sahrens  * Push and pop I/O transform buffers
285fa9e4066Sahrens  * ==========================================================================
286fa9e4066Sahrens  */
287dcbf3bd6SGeorge Wilson void
288770499e1SDan Kimmel zio_push_transform(zio_t *zio, abd_t *data, uint64_t size, uint64_t bufsize,
2899a686fbcSPaul Dagnelie     zio_transform_func_t *transform)
290fa9e4066Sahrens {
291fa9e4066Sahrens 	zio_transform_t *zt = kmem_alloc(sizeof (zio_transform_t), KM_SLEEP);
292fa9e4066Sahrens 
293770499e1SDan Kimmel 	/*
294770499e1SDan Kimmel 	 * Ensure that anyone expecting this zio to contain a linear ABD isn't
295770499e1SDan Kimmel 	 * going to get a nasty surprise when they try to access the data.
296770499e1SDan Kimmel 	 */
297770499e1SDan Kimmel 	IMPLY(abd_is_linear(zio->io_abd), abd_is_linear(data));
298770499e1SDan Kimmel 
299770499e1SDan Kimmel 	zt->zt_orig_abd = zio->io_abd;
300e14bb325SJeff Bonwick 	zt->zt_orig_size = zio->io_size;
301fa9e4066Sahrens 	zt->zt_bufsize = bufsize;
302e14bb325SJeff Bonwick 	zt->zt_transform = transform;
303fa9e4066Sahrens 
304fa9e4066Sahrens 	zt->zt_next = zio->io_transform_stack;
305fa9e4066Sahrens 	zio->io_transform_stack = zt;
306fa9e4066Sahrens 
307770499e1SDan Kimmel 	zio->io_abd = data;
308fa9e4066Sahrens 	zio->io_size = size;
309fa9e4066Sahrens }
310fa9e4066Sahrens 
311dcbf3bd6SGeorge Wilson void
312e14bb325SJeff Bonwick zio_pop_transforms(zio_t *zio)
313fa9e4066Sahrens {
314e14bb325SJeff Bonwick 	zio_transform_t *zt;
315e14bb325SJeff Bonwick 
316e14bb325SJeff Bonwick 	while ((zt = zio->io_transform_stack) != NULL) {
317e14bb325SJeff Bonwick 		if (zt->zt_transform != NULL)
318e14bb325SJeff Bonwick 			zt->zt_transform(zio,
319770499e1SDan Kimmel 			    zt->zt_orig_abd, zt->zt_orig_size);
320fa9e4066Sahrens 
321b24ab676SJeff Bonwick 		if (zt->zt_bufsize != 0)
322770499e1SDan Kimmel 			abd_free(zio->io_abd);
323fa9e4066Sahrens 
324770499e1SDan Kimmel 		zio->io_abd = zt->zt_orig_abd;
325e14bb325SJeff Bonwick 		zio->io_size = zt->zt_orig_size;
326e14bb325SJeff Bonwick 		zio->io_transform_stack = zt->zt_next;
327fa9e4066Sahrens 
328e14bb325SJeff Bonwick 		kmem_free(zt, sizeof (zio_transform_t));
329fa9e4066Sahrens 	}
330fa9e4066Sahrens }
331fa9e4066Sahrens 
332e14bb325SJeff Bonwick /*
333e14bb325SJeff Bonwick  * ==========================================================================
334eb633035STom Caputi  * I/O transform callbacks for subblocks, decompression, and decryption
335e14bb325SJeff Bonwick  * ==========================================================================
336e14bb325SJeff Bonwick  */
337e14bb325SJeff Bonwick static void
338770499e1SDan Kimmel zio_subblock(zio_t *zio, abd_t *data, uint64_t size)
339e14bb325SJeff Bonwick {
340e14bb325SJeff Bonwick 	ASSERT(zio->io_size > size);
341e14bb325SJeff Bonwick 
342e14bb325SJeff Bonwick 	if (zio->io_type == ZIO_TYPE_READ)
343770499e1SDan Kimmel 		abd_copy(data, zio->io_abd, size);
344e14bb325SJeff Bonwick }
345e14bb325SJeff Bonwick 
346e14bb325SJeff Bonwick static void
347770499e1SDan Kimmel zio_decompress(zio_t *zio, abd_t *data, uint64_t size)
348e14bb325SJeff Bonwick {
349770499e1SDan Kimmel 	if (zio->io_error == 0) {
350770499e1SDan Kimmel 		void *tmp = abd_borrow_buf(data, size);
351770499e1SDan Kimmel 		int ret = zio_decompress_data(BP_GET_COMPRESS(zio->io_bp),
352770499e1SDan Kimmel 		    zio->io_abd, tmp, zio->io_size, size);
353770499e1SDan Kimmel 		abd_return_buf_copy(data, tmp, size);
354770499e1SDan Kimmel 
355770499e1SDan Kimmel 		if (ret != 0)
356770499e1SDan Kimmel 			zio->io_error = SET_ERROR(EIO);
357770499e1SDan Kimmel 	}
358e14bb325SJeff Bonwick }
359e14bb325SJeff Bonwick 
360eb633035STom Caputi static void
361eb633035STom Caputi zio_decrypt(zio_t *zio, abd_t *data, uint64_t size)
362eb633035STom Caputi {
363eb633035STom Caputi 	int ret;
364eb633035STom Caputi 	void *tmp;
365eb633035STom Caputi 	blkptr_t *bp = zio->io_bp;
366eb633035STom Caputi 	spa_t *spa = zio->io_spa;
367eb633035STom Caputi 	uint64_t dsobj = zio->io_bookmark.zb_objset;
368eb633035STom Caputi 	uint64_t lsize = BP_GET_LSIZE(bp);
369eb633035STom Caputi 	dmu_object_type_t ot = BP_GET_TYPE(bp);
370eb633035STom Caputi 	uint8_t salt[ZIO_DATA_SALT_LEN];
371eb633035STom Caputi 	uint8_t iv[ZIO_DATA_IV_LEN];
372eb633035STom Caputi 	uint8_t mac[ZIO_DATA_MAC_LEN];
373eb633035STom Caputi 	boolean_t no_crypt = B_FALSE;
374eb633035STom Caputi 
375eb633035STom Caputi 	ASSERT(BP_USES_CRYPT(bp));
376eb633035STom Caputi 	ASSERT3U(size, !=, 0);
377eb633035STom Caputi 
378eb633035STom Caputi 	if (zio->io_error != 0)
379eb633035STom Caputi 		return;
380eb633035STom Caputi 
381eb633035STom Caputi 	/*
382eb633035STom Caputi 	 * Verify the cksum of MACs stored in an indirect bp. It will always
383eb633035STom Caputi 	 * be possible to verify this since it does not require an encryption
384eb633035STom Caputi 	 * key.
385eb633035STom Caputi 	 */
386eb633035STom Caputi 	if (BP_HAS_INDIRECT_MAC_CKSUM(bp)) {
387eb633035STom Caputi 		zio_crypt_decode_mac_bp(bp, mac);
388eb633035STom Caputi 
389eb633035STom Caputi 		if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF) {
390eb633035STom Caputi 			/*
391eb633035STom Caputi 			 * We haven't decompressed the data yet, but
392eb633035STom Caputi 			 * zio_crypt_do_indirect_mac_checksum() requires
393eb633035STom Caputi 			 * decompressed data to be able to parse out the MACs
394eb633035STom Caputi 			 * from the indirect block. We decompress it now and
395eb633035STom Caputi 			 * throw away the result after we are finished.
396eb633035STom Caputi 			 */
397eb633035STom Caputi 			tmp = zio_buf_alloc(lsize);
398eb633035STom Caputi 			ret = zio_decompress_data(BP_GET_COMPRESS(bp),
399eb633035STom Caputi 			    zio->io_abd, tmp, zio->io_size, lsize);
400eb633035STom Caputi 			if (ret != 0) {
401eb633035STom Caputi 				ret = SET_ERROR(EIO);
402eb633035STom Caputi 				goto error;
403eb633035STom Caputi 			}
404eb633035STom Caputi 			ret = zio_crypt_do_indirect_mac_checksum(B_FALSE,
405eb633035STom Caputi 			    tmp, lsize, BP_SHOULD_BYTESWAP(bp), mac);
406eb633035STom Caputi 			zio_buf_free(tmp, lsize);
407eb633035STom Caputi 		} else {
408eb633035STom Caputi 			ret = zio_crypt_do_indirect_mac_checksum_abd(B_FALSE,
409eb633035STom Caputi 			    zio->io_abd, size, BP_SHOULD_BYTESWAP(bp), mac);
410eb633035STom Caputi 		}
411eb633035STom Caputi 		abd_copy(data, zio->io_abd, size);
412eb633035STom Caputi 
413eb633035STom Caputi 		if (ret != 0)
414eb633035STom Caputi 			goto error;
415eb633035STom Caputi 
416eb633035STom Caputi 		return;
417eb633035STom Caputi 	}
418eb633035STom Caputi 
419eb633035STom Caputi 	/*
420eb633035STom Caputi 	 * If this is an authenticated block, just check the MAC. It would be
421eb633035STom Caputi 	 * nice to separate this out into its own flag, but for the moment
422eb633035STom Caputi 	 * enum zio_flag is out of bits.
423eb633035STom Caputi 	 */
424eb633035STom Caputi 	if (BP_IS_AUTHENTICATED(bp)) {
425eb633035STom Caputi 		if (ot == DMU_OT_OBJSET) {
426eb633035STom Caputi 			ret = spa_do_crypt_objset_mac_abd(B_FALSE, spa,
427eb633035STom Caputi 			    dsobj, zio->io_abd, size, BP_SHOULD_BYTESWAP(bp));
428eb633035STom Caputi 		} else {
429eb633035STom Caputi 			zio_crypt_decode_mac_bp(bp, mac);
430eb633035STom Caputi 			ret = spa_do_crypt_mac_abd(B_FALSE, spa, dsobj,
431eb633035STom Caputi 			    zio->io_abd, size, mac);
432eb633035STom Caputi 		}
433eb633035STom Caputi 		abd_copy(data, zio->io_abd, size);
434eb633035STom Caputi 
435eb633035STom Caputi 		if (zio_injection_enabled && ot != DMU_OT_DNODE && ret == 0) {
436eb633035STom Caputi 			ret = zio_handle_decrypt_injection(spa,
437eb633035STom Caputi 			    &zio->io_bookmark, ot, ECKSUM);
438eb633035STom Caputi 		}
439eb633035STom Caputi 		if (ret != 0)
440eb633035STom Caputi 			goto error;
441eb633035STom Caputi 
442eb633035STom Caputi 		return;
443eb633035STom Caputi 	}
444eb633035STom Caputi 
445eb633035STom Caputi 	zio_crypt_decode_params_bp(bp, salt, iv);
446eb633035STom Caputi 
447eb633035STom Caputi 	if (ot == DMU_OT_INTENT_LOG) {
448eb633035STom Caputi 		tmp = abd_borrow_buf_copy(zio->io_abd, sizeof (zil_chain_t));
449eb633035STom Caputi 		zio_crypt_decode_mac_zil(tmp, mac);
450eb633035STom Caputi 		abd_return_buf(zio->io_abd, tmp, sizeof (zil_chain_t));
451eb633035STom Caputi 	} else {
452eb633035STom Caputi 		zio_crypt_decode_mac_bp(bp, mac);
453eb633035STom Caputi 	}
454eb633035STom Caputi 
455eb633035STom Caputi 	ret = spa_do_crypt_abd(B_FALSE, spa, &zio->io_bookmark, BP_GET_TYPE(bp),
456eb633035STom Caputi 	    BP_GET_DEDUP(bp), BP_SHOULD_BYTESWAP(bp), salt, iv, mac, size, data,
457eb633035STom Caputi 	    zio->io_abd, &no_crypt);
458eb633035STom Caputi 	if (no_crypt)
459eb633035STom Caputi 		abd_copy(data, zio->io_abd, size);
460eb633035STom Caputi 
461eb633035STom Caputi 	if (ret != 0)
462eb633035STom Caputi 		goto error;
463eb633035STom Caputi 
464eb633035STom Caputi 	return;
465eb633035STom Caputi 
466eb633035STom Caputi error:
467eb633035STom Caputi 	/* assert that the key was found unless this was speculative */
468eb633035STom Caputi 	ASSERT(ret != EACCES || (zio->io_flags & ZIO_FLAG_SPECULATIVE));
469eb633035STom Caputi 
470eb633035STom Caputi 	/*
471eb633035STom Caputi 	 * If there was a decryption / authentication error return EIO as
472eb633035STom Caputi 	 * the io_error. If this was not a speculative zio, create an ereport.
473eb633035STom Caputi 	 */
474eb633035STom Caputi 	if (ret == ECKSUM) {
475eb633035STom Caputi 		zio->io_error = SET_ERROR(EIO);
476eb633035STom Caputi 		if ((zio->io_flags & ZIO_FLAG_SPECULATIVE) == 0) {
477eb633035STom Caputi 			spa_log_error(spa, &zio->io_bookmark);
478eb633035STom Caputi 			zfs_ereport_post(FM_EREPORT_ZFS_AUTHENTICATION,
479eb633035STom Caputi 			    spa, NULL, &zio->io_bookmark, zio, 0, 0);
480eb633035STom Caputi 		}
481eb633035STom Caputi 	} else {
482eb633035STom Caputi 		zio->io_error = ret;
483eb633035STom Caputi 	}
484eb633035STom Caputi }
485eb633035STom Caputi 
486e14bb325SJeff Bonwick /*
487e14bb325SJeff Bonwick  * ==========================================================================
488e14bb325SJeff Bonwick  * I/O parent/child relationships and pipeline interlocks
489e14bb325SJeff Bonwick  * ==========================================================================
490e14bb325SJeff Bonwick  */
491a3f829aeSBill Moore zio_t *
4920f7643c7SGeorge Wilson zio_walk_parents(zio_t *cio, zio_link_t **zl)
493a3f829aeSBill Moore {
494a3f829aeSBill Moore 	list_t *pl = &cio->io_parent_list;
495e14bb325SJeff Bonwick 
4960f7643c7SGeorge Wilson 	*zl = (*zl == NULL) ? list_head(pl) : list_next(pl, *zl);
4970f7643c7SGeorge Wilson 	if (*zl == NULL)
498a3f829aeSBill Moore 		return (NULL);
499a3f829aeSBill Moore 
5000f7643c7SGeorge Wilson 	ASSERT((*zl)->zl_child == cio);
5010f7643c7SGeorge Wilson 	return ((*zl)->zl_parent);
502a3f829aeSBill Moore }
503a3f829aeSBill Moore 
504a3f829aeSBill Moore zio_t *
5050f7643c7SGeorge Wilson zio_walk_children(zio_t *pio, zio_link_t **zl)
506a3f829aeSBill Moore {
507a3f829aeSBill Moore 	list_t *cl = &pio->io_child_list;
508a3f829aeSBill Moore 
509a3874b8bSToomas Soome 	ASSERT(MUTEX_HELD(&pio->io_lock));
510a3874b8bSToomas Soome 
5110f7643c7SGeorge Wilson 	*zl = (*zl == NULL) ? list_head(cl) : list_next(cl, *zl);
5120f7643c7SGeorge Wilson 	if (*zl == NULL)
513a3f829aeSBill Moore 		return (NULL);
514a3f829aeSBill Moore 
5150f7643c7SGeorge Wilson 	ASSERT((*zl)->zl_parent == pio);
5160f7643c7SGeorge Wilson 	return ((*zl)->zl_child);
517a3f829aeSBill Moore }
518a3f829aeSBill Moore 
519a3f829aeSBill Moore zio_t *
520a3f829aeSBill Moore zio_unique_parent(zio_t *cio)
521a3f829aeSBill Moore {
5220f7643c7SGeorge Wilson 	zio_link_t *zl = NULL;
5230f7643c7SGeorge Wilson 	zio_t *pio = zio_walk_parents(cio, &zl);
524a3f829aeSBill Moore 
5250f7643c7SGeorge Wilson 	VERIFY3P(zio_walk_parents(cio, &zl), ==, NULL);
526a3f829aeSBill Moore 	return (pio);
527a3f829aeSBill Moore }
528a3f829aeSBill Moore 
529a3f829aeSBill Moore void
530a3f829aeSBill Moore zio_add_child(zio_t *pio, zio_t *cio)
531e14bb325SJeff Bonwick {
532a3f829aeSBill Moore 	zio_link_t *zl = kmem_cache_alloc(zio_link_cache, KM_SLEEP);
533a3f829aeSBill Moore 
534a3f829aeSBill Moore 	/*
535a3f829aeSBill Moore 	 * Logical I/Os can have logical, gang, or vdev children.
536a3f829aeSBill Moore 	 * Gang I/Os can have gang or vdev children.
537a3f829aeSBill Moore 	 * Vdev I/Os can only have vdev children.
538a3f829aeSBill Moore 	 * The following ASSERT captures all of these constraints.
539a3f829aeSBill Moore 	 */
5401271e4b1SPrakash Surya 	ASSERT3S(cio->io_child_type, <=, pio->io_child_type);
541a3f829aeSBill Moore 
542a3f829aeSBill Moore 	zl->zl_parent = pio;
543a3f829aeSBill Moore 	zl->zl_child = cio;
544a3f829aeSBill Moore 
545e14bb325SJeff Bonwick 	mutex_enter(&pio->io_lock);
546a3874b8bSToomas Soome 	mutex_enter(&cio->io_lock);
547a3f829aeSBill Moore 
548a3f829aeSBill Moore 	ASSERT(pio->io_state[ZIO_WAIT_DONE] == 0);
549a3f829aeSBill Moore 
550a3f829aeSBill Moore 	for (int w = 0; w < ZIO_WAIT_TYPES; w++)
551a3f829aeSBill Moore 		pio->io_children[cio->io_child_type][w] += !cio->io_state[w];
552a3f829aeSBill Moore 
553a3f829aeSBill Moore 	list_insert_head(&pio->io_child_list, zl);
554a3f829aeSBill Moore 	list_insert_head(&cio->io_parent_list, zl);
555a3f829aeSBill Moore 
556b24ab676SJeff Bonwick 	pio->io_child_count++;
557b24ab676SJeff Bonwick 	cio->io_parent_count++;
558b24ab676SJeff Bonwick 
559a3f829aeSBill Moore 	mutex_exit(&cio->io_lock);
560a3874b8bSToomas Soome 	mutex_exit(&pio->io_lock);
561e14bb325SJeff Bonwick }
562e14bb325SJeff Bonwick 
563fa9e4066Sahrens static void
564a3f829aeSBill Moore zio_remove_child(zio_t *pio, zio_t *cio, zio_link_t *zl)
565e14bb325SJeff Bonwick {
566a3f829aeSBill Moore 	ASSERT(zl->zl_parent == pio);
567a3f829aeSBill Moore 	ASSERT(zl->zl_child == cio);
568e14bb325SJeff Bonwick 
569e14bb325SJeff Bonwick 	mutex_enter(&pio->io_lock);
570a3874b8bSToomas Soome 	mutex_enter(&cio->io_lock);
571a3f829aeSBill Moore 
572a3f829aeSBill Moore 	list_remove(&pio->io_child_list, zl);
573a3f829aeSBill Moore 	list_remove(&cio->io_parent_list, zl);
574a3f829aeSBill Moore 
575b24ab676SJeff Bonwick 	pio->io_child_count--;
576b24ab676SJeff Bonwick 	cio->io_parent_count--;
577b24ab676SJeff Bonwick 
578a3f829aeSBill Moore 	mutex_exit(&cio->io_lock);
579a3874b8bSToomas Soome 	mutex_exit(&pio->io_lock);
580a3f829aeSBill Moore 
581a3f829aeSBill Moore 	kmem_cache_free(zio_link_cache, zl);
582e14bb325SJeff Bonwick }
583e14bb325SJeff Bonwick 
584e14bb325SJeff Bonwick static boolean_t
585d6e1c446SGeorge Wilson zio_wait_for_children(zio_t *zio, uint8_t childbits, enum zio_wait_type wait)
586fa9e4066Sahrens {
587e14bb325SJeff Bonwick 	boolean_t waiting = B_FALSE;
588e14bb325SJeff Bonwick 
589e14bb325SJeff Bonwick 	mutex_enter(&zio->io_lock);
590e14bb325SJeff Bonwick 	ASSERT(zio->io_stall == NULL);
591d6e1c446SGeorge Wilson 	for (int c = 0; c < ZIO_CHILD_TYPES; c++) {
592d6e1c446SGeorge Wilson 		if (!(ZIO_CHILD_BIT_IS_SET(childbits, c)))
593d6e1c446SGeorge Wilson 			continue;
594d6e1c446SGeorge Wilson 
595d6e1c446SGeorge Wilson 		uint64_t *countp = &zio->io_children[c][wait];
596d6e1c446SGeorge Wilson 		if (*countp != 0) {
597d6e1c446SGeorge Wilson 			zio->io_stage >>= 1;
598d6e1c446SGeorge Wilson 			ASSERT3U(zio->io_stage, !=, ZIO_STAGE_OPEN);
599d6e1c446SGeorge Wilson 			zio->io_stall = countp;
600d6e1c446SGeorge Wilson 			waiting = B_TRUE;
601d6e1c446SGeorge Wilson 			break;
602d6e1c446SGeorge Wilson 		}
603e14bb325SJeff Bonwick 	}
604e14bb325SJeff Bonwick 	mutex_exit(&zio->io_lock);
605e14bb325SJeff Bonwick 	return (waiting);
606e14bb325SJeff Bonwick }
607fa9e4066Sahrens 
608e14bb325SJeff Bonwick static void
609e14bb325SJeff Bonwick zio_notify_parent(zio_t *pio, zio_t *zio, enum zio_wait_type wait)
610e14bb325SJeff Bonwick {
611e14bb325SJeff Bonwick 	uint64_t *countp = &pio->io_children[zio->io_child_type][wait];
612e14bb325SJeff Bonwick 	int *errorp = &pio->io_child_error[zio->io_child_type];
613fa9e4066Sahrens 
614e14bb325SJeff Bonwick 	mutex_enter(&pio->io_lock);
615e14bb325SJeff Bonwick 	if (zio->io_error && !(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
616e14bb325SJeff Bonwick 		*errorp = zio_worst_error(*errorp, zio->io_error);
617e14bb325SJeff Bonwick 	pio->io_reexecute |= zio->io_reexecute;
618e14bb325SJeff Bonwick 	ASSERT3U(*countp, >, 0);
61969962b56SMatthew Ahrens 
62069962b56SMatthew Ahrens 	(*countp)--;
62169962b56SMatthew Ahrens 
62269962b56SMatthew Ahrens 	if (*countp == 0 && pio->io_stall == countp) {
6230f7643c7SGeorge Wilson 		zio_taskq_type_t type =
6240f7643c7SGeorge Wilson 		    pio->io_stage < ZIO_STAGE_VDEV_IO_START ? ZIO_TASKQ_ISSUE :
6250f7643c7SGeorge Wilson 		    ZIO_TASKQ_INTERRUPT;
626e14bb325SJeff Bonwick 		pio->io_stall = NULL;
627e14bb325SJeff Bonwick 		mutex_exit(&pio->io_lock);
6280f7643c7SGeorge Wilson 		/*
6290f7643c7SGeorge Wilson 		 * Dispatch the parent zio in its own taskq so that
6300f7643c7SGeorge Wilson 		 * the child can continue to make progress. This also
6310f7643c7SGeorge Wilson 		 * prevents overflowing the stack when we have deeply nested
6320f7643c7SGeorge Wilson 		 * parent-child relationships.
6330f7643c7SGeorge Wilson 		 */
6340f7643c7SGeorge Wilson 		zio_taskq_dispatch(pio, type, B_FALSE);
635e14bb325SJeff Bonwick 	} else {
636e14bb325SJeff Bonwick 		mutex_exit(&pio->io_lock);
637fa9e4066Sahrens 	}
638fa9e4066Sahrens }
639fa9e4066Sahrens 
640e14bb325SJeff Bonwick static void
641e14bb325SJeff Bonwick zio_inherit_child_errors(zio_t *zio, enum zio_child c)
642e14bb325SJeff Bonwick {
643e14bb325SJeff Bonwick 	if (zio->io_child_error[c] != 0 && zio->io_error == 0)
644e14bb325SJeff Bonwick 		zio->io_error = zio->io_child_error[c];
645e14bb325SJeff Bonwick }
646e14bb325SJeff Bonwick 
6470f7643c7SGeorge Wilson int
64894c2d0ebSMatthew Ahrens zio_bookmark_compare(const void *x1, const void *x2)
6490f7643c7SGeorge Wilson {
6500f7643c7SGeorge Wilson 	const zio_t *z1 = x1;
6510f7643c7SGeorge Wilson 	const zio_t *z2 = x2;
6520f7643c7SGeorge Wilson 
65394c2d0ebSMatthew Ahrens 	if (z1->io_bookmark.zb_objset < z2->io_bookmark.zb_objset)
6540f7643c7SGeorge Wilson 		return (-1);
65594c2d0ebSMatthew Ahrens 	if (z1->io_bookmark.zb_objset > z2->io_bookmark.zb_objset)
6560f7643c7SGeorge Wilson 		return (1);
6570f7643c7SGeorge Wilson 
65894c2d0ebSMatthew Ahrens 	if (z1->io_bookmark.zb_object < z2->io_bookmark.zb_object)
6590f7643c7SGeorge Wilson 		return (-1);
66094c2d0ebSMatthew Ahrens 	if (z1->io_bookmark.zb_object > z2->io_bookmark.zb_object)
66194c2d0ebSMatthew Ahrens 		return (1);
66294c2d0ebSMatthew Ahrens 
66394c2d0ebSMatthew Ahrens 	if (z1->io_bookmark.zb_level < z2->io_bookmark.zb_level)
66494c2d0ebSMatthew Ahrens 		return (-1);
66594c2d0ebSMatthew Ahrens 	if (z1->io_bookmark.zb_level > z2->io_bookmark.zb_level)
66694c2d0ebSMatthew Ahrens 		return (1);
66794c2d0ebSMatthew Ahrens 
66894c2d0ebSMatthew Ahrens 	if (z1->io_bookmark.zb_blkid < z2->io_bookmark.zb_blkid)
66994c2d0ebSMatthew Ahrens 		return (-1);
67094c2d0ebSMatthew Ahrens 	if (z1->io_bookmark.zb_blkid > z2->io_bookmark.zb_blkid)
6710f7643c7SGeorge Wilson 		return (1);
6720f7643c7SGeorge Wilson 
6730f7643c7SGeorge Wilson 	if (z1 < z2)
6740f7643c7SGeorge Wilson 		return (-1);
6750f7643c7SGeorge Wilson 	if (z1 > z2)
6760f7643c7SGeorge Wilson 		return (1);
6770f7643c7SGeorge Wilson 
6780f7643c7SGeorge Wilson 	return (0);
6790f7643c7SGeorge Wilson }
6800f7643c7SGeorge Wilson 
681fa9e4066Sahrens /*
682fa9e4066Sahrens  * ==========================================================================
683e14bb325SJeff Bonwick  * Create the various types of I/O (read, write, free, etc)
684fa9e4066Sahrens  * ==========================================================================
685fa9e4066Sahrens  */
686fa9e4066Sahrens static zio_t *
687b24ab676SJeff Bonwick zio_create(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
688770499e1SDan Kimmel     abd_t *data, uint64_t lsize, uint64_t psize, zio_done_func_t *done,
6895602294fSDan Kimmel     void *private, zio_type_t type, zio_priority_t priority,
6905602294fSDan Kimmel     enum zio_flag flags, vdev_t *vd, uint64_t offset,
6915602294fSDan Kimmel     const zbookmark_phys_t *zb, enum zio_stage stage, enum zio_stage pipeline)
692fa9e4066Sahrens {
693fa9e4066Sahrens 	zio_t *zio;
694fa9e4066Sahrens 
695*084fd14fSBrian Behlendorf 	IMPLY(type != ZIO_TYPE_TRIM, psize <= SPA_MAXBLOCKSIZE);
6965602294fSDan Kimmel 	ASSERT(P2PHASE(psize, SPA_MINBLOCKSIZE) == 0);
697e14bb325SJeff Bonwick 	ASSERT(P2PHASE(offset, SPA_MINBLOCKSIZE) == 0);
698fa9e4066Sahrens 
699e14bb325SJeff Bonwick 	ASSERT(!vd || spa_config_held(spa, SCL_STATE_ALL, RW_READER));
700e14bb325SJeff Bonwick 	ASSERT(!bp || !(flags & ZIO_FLAG_CONFIG_WRITER));
701e14bb325SJeff Bonwick 	ASSERT(vd || stage == ZIO_STAGE_OPEN);
702088f3894Sahrens 
703eb633035STom Caputi 	IMPLY(lsize != psize, (flags & ZIO_FLAG_RAW_COMPRESS) != 0);
7045602294fSDan Kimmel 
705ccae0b50Seschrock 	zio = kmem_cache_alloc(zio_cache, KM_SLEEP);
706ccae0b50Seschrock 	bzero(zio, sizeof (zio_t));
707e14bb325SJeff Bonwick 
708e14bb325SJeff Bonwick 	mutex_init(&zio->io_lock, NULL, MUTEX_DEFAULT, NULL);
709e14bb325SJeff Bonwick 	cv_init(&zio->io_cv, NULL, CV_DEFAULT, NULL);
710e14bb325SJeff Bonwick 
711a3f829aeSBill Moore 	list_create(&zio->io_parent_list, sizeof (zio_link_t),
712a3f829aeSBill Moore 	    offsetof(zio_link_t, zl_parent_node));
713a3f829aeSBill Moore 	list_create(&zio->io_child_list, sizeof (zio_link_t),
714a3f829aeSBill Moore 	    offsetof(zio_link_t, zl_child_node));
7158363e80aSGeorge Wilson 	metaslab_trace_init(&zio->io_alloc_list);
716a3f829aeSBill Moore 
717e14bb325SJeff Bonwick 	if (vd != NULL)
718e14bb325SJeff Bonwick 		zio->io_child_type = ZIO_CHILD_VDEV;
719e14bb325SJeff Bonwick 	else if (flags & ZIO_FLAG_GANG_CHILD)
720e14bb325SJeff Bonwick 		zio->io_child_type = ZIO_CHILD_GANG;
721b24ab676SJeff Bonwick 	else if (flags & ZIO_FLAG_DDT_CHILD)
722b24ab676SJeff Bonwick 		zio->io_child_type = ZIO_CHILD_DDT;
723e14bb325SJeff Bonwick 	else
724e14bb325SJeff Bonwick 		zio->io_child_type = ZIO_CHILD_LOGICAL;
725e14bb325SJeff Bonwick 
726fa9e4066Sahrens 	if (bp != NULL) {
727b24ab676SJeff Bonwick 		zio->io_bp = (blkptr_t *)bp;
728fa9e4066Sahrens 		zio->io_bp_copy = *bp;
729fa9e4066Sahrens 		zio->io_bp_orig = *bp;
730b24ab676SJeff Bonwick 		if (type != ZIO_TYPE_WRITE ||
731b24ab676SJeff Bonwick 		    zio->io_child_type == ZIO_CHILD_DDT)
732e14bb325SJeff Bonwick 			zio->io_bp = &zio->io_bp_copy;	/* so caller can free */
733f5383399SBill Moore 		if (zio->io_child_type == ZIO_CHILD_LOGICAL)
734e14bb325SJeff Bonwick 			zio->io_logical = zio;
735f5383399SBill Moore 		if (zio->io_child_type > ZIO_CHILD_GANG && BP_IS_GANG(bp))
736f5383399SBill Moore 			pipeline |= ZIO_GANG_STAGES;
737fa9e4066Sahrens 	}
738e14bb325SJeff Bonwick 
739e14bb325SJeff Bonwick 	zio->io_spa = spa;
740e14bb325SJeff Bonwick 	zio->io_txg = txg;
741fa9e4066Sahrens 	zio->io_done = done;
742fa9e4066Sahrens 	zio->io_private = private;
743fa9e4066Sahrens 	zio->io_type = type;
744fa9e4066Sahrens 	zio->io_priority = priority;
745e14bb325SJeff Bonwick 	zio->io_vd = vd;
746e14bb325SJeff Bonwick 	zio->io_offset = offset;
747770499e1SDan Kimmel 	zio->io_orig_abd = zio->io_abd = data;
7485602294fSDan Kimmel 	zio->io_orig_size = zio->io_size = psize;
7495602294fSDan Kimmel 	zio->io_lsize = lsize;
750e14bb325SJeff Bonwick 	zio->io_orig_flags = zio->io_flags = flags;
751e14bb325SJeff Bonwick 	zio->io_orig_stage = zio->io_stage = stage;
752e14bb325SJeff Bonwick 	zio->io_orig_pipeline = zio->io_pipeline = pipeline;
7530f7643c7SGeorge Wilson 	zio->io_pipeline_trace = ZIO_STAGE_OPEN;
754fa9e4066Sahrens 
755a3f829aeSBill Moore 	zio->io_state[ZIO_WAIT_READY] = (stage >= ZIO_STAGE_READY);
756a3f829aeSBill Moore 	zio->io_state[ZIO_WAIT_DONE] = (stage >= ZIO_STAGE_DONE);
757a3f829aeSBill Moore 
758e14bb325SJeff Bonwick 	if (zb != NULL)
759e14bb325SJeff Bonwick 		zio->io_bookmark = *zb;
760e14bb325SJeff Bonwick 
761e14bb325SJeff Bonwick 	if (pio != NULL) {
762663207adSDon Brady 		if (zio->io_metaslab_class == NULL)
763663207adSDon Brady 			zio->io_metaslab_class = pio->io_metaslab_class;
764e14bb325SJeff Bonwick 		if (zio->io_logical == NULL)
765ea8dc4b6Seschrock 			zio->io_logical = pio->io_logical;
766f5383399SBill Moore 		if (zio->io_child_type == ZIO_CHILD_GANG)
767f5383399SBill Moore 			zio->io_gang_leader = pio->io_gang_leader;
768e14bb325SJeff Bonwick 		zio_add_child(pio, zio);
769fa9e4066Sahrens 	}
770fa9e4066Sahrens 
771fa9e4066Sahrens 	return (zio);
772fa9e4066Sahrens }
773fa9e4066Sahrens 
7740a4e9518Sgw static void
775e14bb325SJeff Bonwick zio_destroy(zio_t *zio)
7760a4e9518Sgw {
7778363e80aSGeorge Wilson 	metaslab_trace_fini(&zio->io_alloc_list);
778a3f829aeSBill Moore 	list_destroy(&zio->io_parent_list);
779a3f829aeSBill Moore 	list_destroy(&zio->io_child_list);
780e14bb325SJeff Bonwick 	mutex_destroy(&zio->io_lock);
781e14bb325SJeff Bonwick 	cv_destroy(&zio->io_cv);
782e14bb325SJeff Bonwick 	kmem_cache_free(zio_cache, zio);
7830a4e9518Sgw }
7840a4e9518Sgw 
785fa9e4066Sahrens zio_t *
786a3f829aeSBill Moore zio_null(zio_t *pio, spa_t *spa, vdev_t *vd, zio_done_func_t *done,
787b24ab676SJeff Bonwick     void *private, enum zio_flag flags)
788fa9e4066Sahrens {
789fa9e4066Sahrens 	zio_t *zio;
790fa9e4066Sahrens 
7915602294fSDan Kimmel 	zio = zio_create(pio, spa, 0, NULL, NULL, 0, 0, done, private,
792a3f829aeSBill Moore 	    ZIO_TYPE_NULL, ZIO_PRIORITY_NOW, flags, vd, 0, NULL,
793e14bb325SJeff Bonwick 	    ZIO_STAGE_OPEN, ZIO_INTERLOCK_PIPELINE);
794fa9e4066Sahrens 
795fa9e4066Sahrens 	return (zio);
796fa9e4066Sahrens }
797fa9e4066Sahrens 
798fa9e4066Sahrens zio_t *
799b24ab676SJeff Bonwick zio_root(spa_t *spa, zio_done_func_t *done, void *private, enum zio_flag flags)
800fa9e4066Sahrens {
801a3f829aeSBill Moore 	return (zio_null(NULL, spa, NULL, done, private, flags));
802fa9e4066Sahrens }
803fa9e4066Sahrens 
804f63ab3d5SMatthew Ahrens void
805f63ab3d5SMatthew Ahrens zfs_blkptr_verify(spa_t *spa, const blkptr_t *bp)
806f63ab3d5SMatthew Ahrens {
807f63ab3d5SMatthew Ahrens 	if (!DMU_OT_IS_VALID(BP_GET_TYPE(bp))) {
808f63ab3d5SMatthew Ahrens 		zfs_panic_recover("blkptr at %p has invalid TYPE %llu",
809f63ab3d5SMatthew Ahrens 		    bp, (longlong_t)BP_GET_TYPE(bp));
810f63ab3d5SMatthew Ahrens 	}
811f63ab3d5SMatthew Ahrens 	if (BP_GET_CHECKSUM(bp) >= ZIO_CHECKSUM_FUNCTIONS ||
812f63ab3d5SMatthew Ahrens 	    BP_GET_CHECKSUM(bp) <= ZIO_CHECKSUM_ON) {
813f63ab3d5SMatthew Ahrens 		zfs_panic_recover("blkptr at %p has invalid CHECKSUM %llu",
814f63ab3d5SMatthew Ahrens 		    bp, (longlong_t)BP_GET_CHECKSUM(bp));
815f63ab3d5SMatthew Ahrens 	}
816f63ab3d5SMatthew Ahrens 	if (BP_GET_COMPRESS(bp) >= ZIO_COMPRESS_FUNCTIONS ||
817f63ab3d5SMatthew Ahrens 	    BP_GET_COMPRESS(bp) <= ZIO_COMPRESS_ON) {
818f63ab3d5SMatthew Ahrens 		zfs_panic_recover("blkptr at %p has invalid COMPRESS %llu",
819f63ab3d5SMatthew Ahrens 		    bp, (longlong_t)BP_GET_COMPRESS(bp));
820f63ab3d5SMatthew Ahrens 	}
821f63ab3d5SMatthew Ahrens 	if (BP_GET_LSIZE(bp) > SPA_MAXBLOCKSIZE) {
822f63ab3d5SMatthew Ahrens 		zfs_panic_recover("blkptr at %p has invalid LSIZE %llu",
823f63ab3d5SMatthew Ahrens 		    bp, (longlong_t)BP_GET_LSIZE(bp));
824f63ab3d5SMatthew Ahrens 	}
825f63ab3d5SMatthew Ahrens 	if (BP_GET_PSIZE(bp) > SPA_MAXBLOCKSIZE) {
826f63ab3d5SMatthew Ahrens 		zfs_panic_recover("blkptr at %p has invalid PSIZE %llu",
827f63ab3d5SMatthew Ahrens 		    bp, (longlong_t)BP_GET_PSIZE(bp));
828f63ab3d5SMatthew Ahrens 	}
829f63ab3d5SMatthew Ahrens 
830f63ab3d5SMatthew Ahrens 	if (BP_IS_EMBEDDED(bp)) {
831f63ab3d5SMatthew Ahrens 		if (BPE_GET_ETYPE(bp) > NUM_BP_EMBEDDED_TYPES) {
832f63ab3d5SMatthew Ahrens 			zfs_panic_recover("blkptr at %p has invalid ETYPE %llu",
833f63ab3d5SMatthew Ahrens 			    bp, (longlong_t)BPE_GET_ETYPE(bp));
834f63ab3d5SMatthew Ahrens 		}
835f63ab3d5SMatthew Ahrens 	}
836f63ab3d5SMatthew Ahrens 
8376f793812SPavel Zakharov 	/*
8386f793812SPavel Zakharov 	 * Do not verify individual DVAs if the config is not trusted. This
8396f793812SPavel Zakharov 	 * will be done once the zio is executed in vdev_mirror_map_alloc.
8406f793812SPavel Zakharov 	 */
8416f793812SPavel Zakharov 	if (!spa->spa_trust_config)
8426f793812SPavel Zakharov 		return;
8436f793812SPavel Zakharov 
844f63ab3d5SMatthew Ahrens 	/*
845f63ab3d5SMatthew Ahrens 	 * Pool-specific checks.
846f63ab3d5SMatthew Ahrens 	 *
847f63ab3d5SMatthew Ahrens 	 * Note: it would be nice to verify that the blk_birth and
848f63ab3d5SMatthew Ahrens 	 * BP_PHYSICAL_BIRTH() are not too large.  However, spa_freeze()
849f63ab3d5SMatthew Ahrens 	 * allows the birth time of log blocks (and dmu_sync()-ed blocks
850f63ab3d5SMatthew Ahrens 	 * that are in the log) to be arbitrarily large.
851f63ab3d5SMatthew Ahrens 	 */
852f63ab3d5SMatthew Ahrens 	for (int i = 0; i < BP_GET_NDVAS(bp); i++) {
853f63ab3d5SMatthew Ahrens 		uint64_t vdevid = DVA_GET_VDEV(&bp->blk_dva[i]);
854f63ab3d5SMatthew Ahrens 		if (vdevid >= spa->spa_root_vdev->vdev_children) {
855f63ab3d5SMatthew Ahrens 			zfs_panic_recover("blkptr at %p DVA %u has invalid "
856f63ab3d5SMatthew Ahrens 			    "VDEV %llu",
857f63ab3d5SMatthew Ahrens 			    bp, i, (longlong_t)vdevid);
8585897eb49SJustin Gibbs 			continue;
859f63ab3d5SMatthew Ahrens 		}
860f63ab3d5SMatthew Ahrens 		vdev_t *vd = spa->spa_root_vdev->vdev_child[vdevid];
861f63ab3d5SMatthew Ahrens 		if (vd == NULL) {
862f63ab3d5SMatthew Ahrens 			zfs_panic_recover("blkptr at %p DVA %u has invalid "
863f63ab3d5SMatthew Ahrens 			    "VDEV %llu",
864f63ab3d5SMatthew Ahrens 			    bp, i, (longlong_t)vdevid);
8655897eb49SJustin Gibbs 			continue;
866f63ab3d5SMatthew Ahrens 		}
867f63ab3d5SMatthew Ahrens 		if (vd->vdev_ops == &vdev_hole_ops) {
868f63ab3d5SMatthew Ahrens 			zfs_panic_recover("blkptr at %p DVA %u has hole "
869f63ab3d5SMatthew Ahrens 			    "VDEV %llu",
870f63ab3d5SMatthew Ahrens 			    bp, i, (longlong_t)vdevid);
8715897eb49SJustin Gibbs 			continue;
872f63ab3d5SMatthew Ahrens 		}
873f63ab3d5SMatthew Ahrens 		if (vd->vdev_ops == &vdev_missing_ops) {
874f63ab3d5SMatthew Ahrens 			/*
875f63ab3d5SMatthew Ahrens 			 * "missing" vdevs are valid during import, but we
876f63ab3d5SMatthew Ahrens 			 * don't have their detailed info (e.g. asize), so
877f63ab3d5SMatthew Ahrens 			 * we can't perform any more checks on them.
878f63ab3d5SMatthew Ahrens 			 */
879f63ab3d5SMatthew Ahrens 			continue;
880f63ab3d5SMatthew Ahrens 		}
881f63ab3d5SMatthew Ahrens 		uint64_t offset = DVA_GET_OFFSET(&bp->blk_dva[i]);
882f63ab3d5SMatthew Ahrens 		uint64_t asize = DVA_GET_ASIZE(&bp->blk_dva[i]);
883f63ab3d5SMatthew Ahrens 		if (BP_IS_GANG(bp))
884f63ab3d5SMatthew Ahrens 			asize = vdev_psize_to_asize(vd, SPA_GANGBLOCKSIZE);
885f63ab3d5SMatthew Ahrens 		if (offset + asize > vd->vdev_asize) {
886f63ab3d5SMatthew Ahrens 			zfs_panic_recover("blkptr at %p DVA %u has invalid "
887f63ab3d5SMatthew Ahrens 			    "OFFSET %llu",
888f63ab3d5SMatthew Ahrens 			    bp, i, (longlong_t)offset);
889f63ab3d5SMatthew Ahrens 		}
890f63ab3d5SMatthew Ahrens 	}
891f63ab3d5SMatthew Ahrens }
892f63ab3d5SMatthew Ahrens 
8936f793812SPavel Zakharov boolean_t
8946f793812SPavel Zakharov zfs_dva_valid(spa_t *spa, const dva_t *dva, const blkptr_t *bp)
8956f793812SPavel Zakharov {
8966f793812SPavel Zakharov 	uint64_t vdevid = DVA_GET_VDEV(dva);
8976f793812SPavel Zakharov 
8986f793812SPavel Zakharov 	if (vdevid >= spa->spa_root_vdev->vdev_children)
8996f793812SPavel Zakharov 		return (B_FALSE);
9006f793812SPavel Zakharov 
9016f793812SPavel Zakharov 	vdev_t *vd = spa->spa_root_vdev->vdev_child[vdevid];
9026f793812SPavel Zakharov 	if (vd == NULL)
9036f793812SPavel Zakharov 		return (B_FALSE);
9046f793812SPavel Zakharov 
9056f793812SPavel Zakharov 	if (vd->vdev_ops == &vdev_hole_ops)
9066f793812SPavel Zakharov 		return (B_FALSE);
9076f793812SPavel Zakharov 
9086f793812SPavel Zakharov 	if (vd->vdev_ops == &vdev_missing_ops) {
9096f793812SPavel Zakharov 		return (B_FALSE);
9106f793812SPavel Zakharov 	}
9116f793812SPavel Zakharov 
9126f793812SPavel Zakharov 	uint64_t offset = DVA_GET_OFFSET(dva);
9136f793812SPavel Zakharov 	uint64_t asize = DVA_GET_ASIZE(dva);
9146f793812SPavel Zakharov 
9156f793812SPavel Zakharov 	if (BP_IS_GANG(bp))
9166f793812SPavel Zakharov 		asize = vdev_psize_to_asize(vd, SPA_GANGBLOCKSIZE);
9176f793812SPavel Zakharov 	if (offset + asize > vd->vdev_asize)
9186f793812SPavel Zakharov 		return (B_FALSE);
9196f793812SPavel Zakharov 
9206f793812SPavel Zakharov 	return (B_TRUE);
9216f793812SPavel Zakharov }
9226f793812SPavel Zakharov 
923fa9e4066Sahrens zio_t *
924e14bb325SJeff Bonwick zio_read(zio_t *pio, spa_t *spa, const blkptr_t *bp,
925770499e1SDan Kimmel     abd_t *data, uint64_t size, zio_done_func_t *done, void *private,
9267802d7bfSMatthew Ahrens     zio_priority_t priority, enum zio_flag flags, const zbookmark_phys_t *zb)
927fa9e4066Sahrens {
928fa9e4066Sahrens 	zio_t *zio;
929fa9e4066Sahrens 
930f63ab3d5SMatthew Ahrens 	zfs_blkptr_verify(spa, bp);
931f63ab3d5SMatthew Ahrens 
932b24ab676SJeff Bonwick 	zio = zio_create(pio, spa, BP_PHYSICAL_BIRTH(bp), bp,
9335602294fSDan Kimmel 	    data, size, size, done, private,
934e14bb325SJeff Bonwick 	    ZIO_TYPE_READ, priority, flags, NULL, 0, zb,
935b24ab676SJeff Bonwick 	    ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ?
936b24ab676SJeff Bonwick 	    ZIO_DDT_CHILD_READ_PIPELINE : ZIO_READ_PIPELINE);
937fa9e4066Sahrens 
938fa9e4066Sahrens 	return (zio);
939fa9e4066Sahrens }
940fa9e4066Sahrens 
941fa9e4066Sahrens zio_t *
942e14bb325SJeff Bonwick zio_write(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
943770499e1SDan Kimmel     abd_t *data, uint64_t lsize, uint64_t psize, const zio_prop_t *zp,
9448df0bcf0SPaul Dagnelie     zio_done_func_t *ready, zio_done_func_t *children_ready,
9458df0bcf0SPaul Dagnelie     zio_done_func_t *physdone, zio_done_func_t *done,
9468df0bcf0SPaul Dagnelie     void *private, zio_priority_t priority, enum zio_flag flags,
9478df0bcf0SPaul Dagnelie     const zbookmark_phys_t *zb)
948fa9e4066Sahrens {
949fa9e4066Sahrens 	zio_t *zio;
950fa9e4066Sahrens 
951e14bb325SJeff Bonwick 	ASSERT(zp->zp_checksum >= ZIO_CHECKSUM_OFF &&
952e14bb325SJeff Bonwick 	    zp->zp_checksum < ZIO_CHECKSUM_FUNCTIONS &&
953e14bb325SJeff Bonwick 	    zp->zp_compress >= ZIO_COMPRESS_OFF &&
954e14bb325SJeff Bonwick 	    zp->zp_compress < ZIO_COMPRESS_FUNCTIONS &&
955ad135b5dSChristopher Siden 	    DMU_OT_IS_VALID(zp->zp_type) &&
956e14bb325SJeff Bonwick 	    zp->zp_level < 32 &&
957b24ab676SJeff Bonwick 	    zp->zp_copies > 0 &&
95880901aeaSGeorge Wilson 	    zp->zp_copies <= spa_max_replication(spa));
9590a4e9518Sgw 
9605602294fSDan Kimmel 	zio = zio_create(pio, spa, txg, bp, data, lsize, psize, done, private,
961e14bb325SJeff Bonwick 	    ZIO_TYPE_WRITE, priority, flags, NULL, 0, zb,
962b24ab676SJeff Bonwick 	    ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ?
963b24ab676SJeff Bonwick 	    ZIO_DDT_CHILD_WRITE_PIPELINE : ZIO_WRITE_PIPELINE);
964fa9e4066Sahrens 
965c717a561Smaybee 	zio->io_ready = ready;
9668df0bcf0SPaul Dagnelie 	zio->io_children_ready = children_ready;
96769962b56SMatthew Ahrens 	zio->io_physdone = physdone;
968e14bb325SJeff Bonwick 	zio->io_prop = *zp;
969fa9e4066Sahrens 
9705d7b4d43SMatthew Ahrens 	/*
9715d7b4d43SMatthew Ahrens 	 * Data can be NULL if we are going to call zio_write_override() to
9725d7b4d43SMatthew Ahrens 	 * provide the already-allocated BP.  But we may need the data to
9735d7b4d43SMatthew Ahrens 	 * verify a dedup hit (if requested).  In this case, don't try to
974eb633035STom Caputi 	 * dedup (just take the already-allocated BP verbatim). Encrypted
975eb633035STom Caputi 	 * dedup blocks need data as well so we also disable dedup in this
976eb633035STom Caputi 	 * case.
9775d7b4d43SMatthew Ahrens 	 */
978eb633035STom Caputi 	if (data == NULL &&
979eb633035STom Caputi 	    (zio->io_prop.zp_dedup_verify || zio->io_prop.zp_encrypt)) {
9805d7b4d43SMatthew Ahrens 		zio->io_prop.zp_dedup = zio->io_prop.zp_dedup_verify = B_FALSE;
9815d7b4d43SMatthew Ahrens 	}
9825d7b4d43SMatthew Ahrens 
983fa9e4066Sahrens 	return (zio);
984fa9e4066Sahrens }
985fa9e4066Sahrens 
986fa9e4066Sahrens zio_t *
987770499e1SDan Kimmel zio_rewrite(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, abd_t *data,
98869962b56SMatthew Ahrens     uint64_t size, zio_done_func_t *done, void *private,
9897802d7bfSMatthew Ahrens     zio_priority_t priority, enum zio_flag flags, zbookmark_phys_t *zb)
990fa9e4066Sahrens {
991fa9e4066Sahrens 	zio_t *zio;
992fa9e4066Sahrens 
9935602294fSDan Kimmel 	zio = zio_create(pio, spa, txg, bp, data, size, size, done, private,
9940f7643c7SGeorge Wilson 	    ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_IO_REWRITE, NULL, 0, zb,
995e14bb325SJeff Bonwick 	    ZIO_STAGE_OPEN, ZIO_REWRITE_PIPELINE);
996fa9e4066Sahrens 
997fa9e4066Sahrens 	return (zio);
998fa9e4066Sahrens }
999fa9e4066Sahrens 
1000b24ab676SJeff Bonwick void
100180901aeaSGeorge Wilson zio_write_override(zio_t *zio, blkptr_t *bp, int copies, boolean_t nopwrite)
1002b24ab676SJeff Bonwick {
1003b24ab676SJeff Bonwick 	ASSERT(zio->io_type == ZIO_TYPE_WRITE);
1004b24ab676SJeff Bonwick 	ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1005b24ab676SJeff Bonwick 	ASSERT(zio->io_stage == ZIO_STAGE_OPEN);
1006b24ab676SJeff Bonwick 	ASSERT(zio->io_txg == spa_syncing_txg(zio->io_spa));
1007b24ab676SJeff Bonwick 
100880901aeaSGeorge Wilson 	/*
100980901aeaSGeorge Wilson 	 * We must reset the io_prop to match the values that existed
101080901aeaSGeorge Wilson 	 * when the bp was first written by dmu_sync() keeping in mind
101180901aeaSGeorge Wilson 	 * that nopwrite and dedup are mutually exclusive.
101280901aeaSGeorge Wilson 	 */
101380901aeaSGeorge Wilson 	zio->io_prop.zp_dedup = nopwrite ? B_FALSE : zio->io_prop.zp_dedup;
101480901aeaSGeorge Wilson 	zio->io_prop.zp_nopwrite = nopwrite;
1015b24ab676SJeff Bonwick 	zio->io_prop.zp_copies = copies;
1016b24ab676SJeff Bonwick 	zio->io_bp_override = bp;
1017b24ab676SJeff Bonwick }
1018b24ab676SJeff Bonwick 
1019b24ab676SJeff Bonwick void
1020b24ab676SJeff Bonwick zio_free(spa_t *spa, uint64_t txg, const blkptr_t *bp)
1021b24ab676SJeff Bonwick {
10225d7b4d43SMatthew Ahrens 
10235cabbc6bSPrashanth Sreenivasa 	zfs_blkptr_verify(spa, bp);
10245cabbc6bSPrashanth Sreenivasa 
10255d7b4d43SMatthew Ahrens 	/*
10265d7b4d43SMatthew Ahrens 	 * The check for EMBEDDED is a performance optimization.  We
10275d7b4d43SMatthew Ahrens 	 * process the free here (by ignoring it) rather than
10285d7b4d43SMatthew Ahrens 	 * putting it on the list and then processing it in zio_free_sync().
10295d7b4d43SMatthew Ahrens 	 */
10305d7b4d43SMatthew Ahrens 	if (BP_IS_EMBEDDED(bp))
10315d7b4d43SMatthew Ahrens 		return;
10323b2aab18SMatthew Ahrens 	metaslab_check_free(spa, bp);
10339cb154a3SMatthew Ahrens 
10349cb154a3SMatthew Ahrens 	/*
10359cb154a3SMatthew Ahrens 	 * Frees that are for the currently-syncing txg, are not going to be
10369cb154a3SMatthew Ahrens 	 * deferred, and which will not need to do a read (i.e. not GANG or
10379cb154a3SMatthew Ahrens 	 * DEDUP), can be processed immediately.  Otherwise, put them on the
10389cb154a3SMatthew Ahrens 	 * in-memory list for later processing.
10399cb154a3SMatthew Ahrens 	 */
10409cb154a3SMatthew Ahrens 	if (BP_IS_GANG(bp) || BP_GET_DEDUP(bp) ||
10419cb154a3SMatthew Ahrens 	    txg != spa->spa_syncing_txg ||
10429cb154a3SMatthew Ahrens 	    spa_sync_pass(spa) >= zfs_sync_pass_deferred_free) {
10439cb154a3SMatthew Ahrens 		bplist_append(&spa->spa_free_bplist[txg & TXG_MASK], bp);
10449cb154a3SMatthew Ahrens 	} else {
10459cb154a3SMatthew Ahrens 		VERIFY0(zio_wait(zio_free_sync(NULL, spa, txg, bp, 0)));
10469cb154a3SMatthew Ahrens 	}
1047b24ab676SJeff Bonwick }
1048b24ab676SJeff Bonwick 
1049fa9e4066Sahrens zio_t *
1050b24ab676SJeff Bonwick zio_free_sync(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
1051b24ab676SJeff Bonwick     enum zio_flag flags)
1052fa9e4066Sahrens {
1053fa9e4066Sahrens 	zio_t *zio;
10549cb154a3SMatthew Ahrens 	enum zio_stage stage = ZIO_FREE_PIPELINE;
1055fa9e4066Sahrens 
1056fa9e4066Sahrens 	ASSERT(!BP_IS_HOLE(bp));
1057b24ab676SJeff Bonwick 	ASSERT(spa_syncing_txg(spa) == txg);
105801f55e48SGeorge Wilson 	ASSERT(spa_sync_pass(spa) < zfs_sync_pass_deferred_free);
1059fa9e4066Sahrens 
10605d7b4d43SMatthew Ahrens 	if (BP_IS_EMBEDDED(bp))
10615d7b4d43SMatthew Ahrens 		return (zio_null(pio, spa, NULL, NULL, NULL, 0));
10625d7b4d43SMatthew Ahrens 
10633b2aab18SMatthew Ahrens 	metaslab_check_free(spa, bp);
10646e6d5868SMatthew Ahrens 	arc_freed(spa, bp);
1065a3874b8bSToomas Soome 	dsl_scan_freed(spa, bp);
10663b2aab18SMatthew Ahrens 
10679cb154a3SMatthew Ahrens 	/*
10689cb154a3SMatthew Ahrens 	 * GANG and DEDUP blocks can induce a read (for the gang block header,
10699cb154a3SMatthew Ahrens 	 * or the DDT), so issue them asynchronously so that this thread is
10709cb154a3SMatthew Ahrens 	 * not tied up.
10719cb154a3SMatthew Ahrens 	 */
10729cb154a3SMatthew Ahrens 	if (BP_IS_GANG(bp) || BP_GET_DEDUP(bp))
10739cb154a3SMatthew Ahrens 		stage |= ZIO_STAGE_ISSUE_ASYNC;
10749cb154a3SMatthew Ahrens 
1075e14bb325SJeff Bonwick 	zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp),
10765602294fSDan Kimmel 	    BP_GET_PSIZE(bp), NULL, NULL, ZIO_TYPE_FREE, ZIO_PRIORITY_NOW,
10775602294fSDan Kimmel 	    flags, NULL, 0, NULL, ZIO_STAGE_OPEN, stage);
10789cb154a3SMatthew Ahrens 
1079fa9e4066Sahrens 	return (zio);
1080fa9e4066Sahrens }
1081fa9e4066Sahrens 
1082fa9e4066Sahrens zio_t *
1083b24ab676SJeff Bonwick zio_claim(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
1084b24ab676SJeff Bonwick     zio_done_func_t *done, void *private, enum zio_flag flags)
1085fa9e4066Sahrens {
1086fa9e4066Sahrens 	zio_t *zio;
1087fa9e4066Sahrens 
10885cabbc6bSPrashanth Sreenivasa 	zfs_blkptr_verify(spa, bp);
10895d7b4d43SMatthew Ahrens 
10905d7b4d43SMatthew Ahrens 	if (BP_IS_EMBEDDED(bp))
10915d7b4d43SMatthew Ahrens 		return (zio_null(pio, spa, NULL, NULL, NULL, 0));
10925d7b4d43SMatthew Ahrens 
1093fa9e4066Sahrens 	/*
1094fa9e4066Sahrens 	 * A claim is an allocation of a specific block.  Claims are needed
1095fa9e4066Sahrens 	 * to support immediate writes in the intent log.  The issue is that
1096fa9e4066Sahrens 	 * immediate writes contain committed data, but in a txg that was
1097fa9e4066Sahrens 	 * *not* committed.  Upon opening the pool after an unclean shutdown,
1098fa9e4066Sahrens 	 * the intent log claims all blocks that contain immediate write data
1099fa9e4066Sahrens 	 * so that the SPA knows they're in use.
1100fa9e4066Sahrens 	 *
1101fa9e4066Sahrens 	 * All claims *must* be resolved in the first txg -- before the SPA
1102fa9e4066Sahrens 	 * starts allocating blocks -- so that nothing is allocated twice.
1103b24ab676SJeff Bonwick 	 * If txg == 0 we just verify that the block is claimable.
1104fa9e4066Sahrens 	 */
110586714001SSerapheim Dimitropoulos 	ASSERT3U(spa->spa_uberblock.ub_rootbp.blk_birth, <,
110686714001SSerapheim Dimitropoulos 	    spa_min_claim_txg(spa));
110786714001SSerapheim Dimitropoulos 	ASSERT(txg == spa_min_claim_txg(spa) || txg == 0);
1108b24ab676SJeff Bonwick 	ASSERT(!BP_GET_DEDUP(bp) || !spa_writeable(spa));	/* zdb(1M) */
1109fa9e4066Sahrens 
1110e14bb325SJeff Bonwick 	zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp),
11115602294fSDan Kimmel 	    BP_GET_PSIZE(bp), done, private, ZIO_TYPE_CLAIM, ZIO_PRIORITY_NOW,
11125602294fSDan Kimmel 	    flags, NULL, 0, NULL, ZIO_STAGE_OPEN, ZIO_CLAIM_PIPELINE);
11130f7643c7SGeorge Wilson 	ASSERT0(zio->io_queued_timestamp);
1114fa9e4066Sahrens 
1115fa9e4066Sahrens 	return (zio);
1116fa9e4066Sahrens }
1117fa9e4066Sahrens 
1118fa9e4066Sahrens zio_t *
1119fa9e4066Sahrens zio_ioctl(zio_t *pio, spa_t *spa, vdev_t *vd, int cmd,
112069962b56SMatthew Ahrens     zio_done_func_t *done, void *private, enum zio_flag flags)
1121fa9e4066Sahrens {
1122fa9e4066Sahrens 	zio_t *zio;
1123fa9e4066Sahrens 	int c;
1124fa9e4066Sahrens 
1125fa9e4066Sahrens 	if (vd->vdev_children == 0) {
11265602294fSDan Kimmel 		zio = zio_create(pio, spa, 0, NULL, NULL, 0, 0, done, private,
112769962b56SMatthew Ahrens 		    ZIO_TYPE_IOCTL, ZIO_PRIORITY_NOW, flags, vd, 0, NULL,
1128fa9e4066Sahrens 		    ZIO_STAGE_OPEN, ZIO_IOCTL_PIPELINE);
1129fa9e4066Sahrens 
1130fa9e4066Sahrens 		zio->io_cmd = cmd;
1131fa9e4066Sahrens 	} else {
1132a3f829aeSBill Moore 		zio = zio_null(pio, spa, NULL, NULL, NULL, flags);
1133fa9e4066Sahrens 
1134fa9e4066Sahrens 		for (c = 0; c < vd->vdev_children; c++)
1135fa9e4066Sahrens 			zio_nowait(zio_ioctl(zio, spa, vd->vdev_child[c], cmd,
113669962b56SMatthew Ahrens 			    done, private, flags));
1137fa9e4066Sahrens 	}
1138fa9e4066Sahrens 
1139fa9e4066Sahrens 	return (zio);
1140fa9e4066Sahrens }
1141fa9e4066Sahrens 
1142*084fd14fSBrian Behlendorf zio_t *
1143*084fd14fSBrian Behlendorf zio_trim(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
1144*084fd14fSBrian Behlendorf     zio_done_func_t *done, void *private, zio_priority_t priority,
1145*084fd14fSBrian Behlendorf     enum zio_flag flags, enum trim_flag trim_flags)
1146*084fd14fSBrian Behlendorf {
1147*084fd14fSBrian Behlendorf 	zio_t *zio;
1148*084fd14fSBrian Behlendorf 
1149*084fd14fSBrian Behlendorf 	ASSERT0(vd->vdev_children);
1150*084fd14fSBrian Behlendorf 	ASSERT0(P2PHASE(offset, 1ULL << vd->vdev_ashift));
1151*084fd14fSBrian Behlendorf 	ASSERT0(P2PHASE(size, 1ULL << vd->vdev_ashift));
1152*084fd14fSBrian Behlendorf 	ASSERT3U(size, !=, 0);
1153*084fd14fSBrian Behlendorf 
1154*084fd14fSBrian Behlendorf 	zio = zio_create(pio, vd->vdev_spa, 0, NULL, NULL, size, size, done,
1155*084fd14fSBrian Behlendorf 	    private, ZIO_TYPE_TRIM, priority, flags | ZIO_FLAG_PHYSICAL,
1156*084fd14fSBrian Behlendorf 	    vd, offset, NULL, ZIO_STAGE_OPEN, ZIO_TRIM_PIPELINE);
1157*084fd14fSBrian Behlendorf 	zio->io_trim_flags = trim_flags;
1158*084fd14fSBrian Behlendorf 
1159*084fd14fSBrian Behlendorf 	return (zio);
1160*084fd14fSBrian Behlendorf }
1161*084fd14fSBrian Behlendorf 
1162fa9e4066Sahrens zio_t *
1163fa9e4066Sahrens zio_read_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
1164770499e1SDan Kimmel     abd_t *data, int checksum, zio_done_func_t *done, void *private,
116569962b56SMatthew Ahrens     zio_priority_t priority, enum zio_flag flags, boolean_t labels)
1166fa9e4066Sahrens {
1167fa9e4066Sahrens 	zio_t *zio;
11680a4e9518Sgw 
1169e14bb325SJeff Bonwick 	ASSERT(vd->vdev_children == 0);
1170e14bb325SJeff Bonwick 	ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE ||
1171e14bb325SJeff Bonwick 	    offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
1172e14bb325SJeff Bonwick 	ASSERT3U(offset + size, <=, vd->vdev_psize);
1173fa9e4066Sahrens 
11745602294fSDan Kimmel 	zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, size, done,
11755602294fSDan Kimmel 	    private, ZIO_TYPE_READ, priority, flags | ZIO_FLAG_PHYSICAL, vd,
11765602294fSDan Kimmel 	    offset, NULL, ZIO_STAGE_OPEN, ZIO_READ_PHYS_PIPELINE);
1177fa9e4066Sahrens 
1178e14bb325SJeff Bonwick 	zio->io_prop.zp_checksum = checksum;
1179fa9e4066Sahrens 
1180fa9e4066Sahrens 	return (zio);
1181fa9e4066Sahrens }
1182fa9e4066Sahrens 
1183fa9e4066Sahrens zio_t *
1184fa9e4066Sahrens zio_write_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
1185770499e1SDan Kimmel     abd_t *data, int checksum, zio_done_func_t *done, void *private,
118669962b56SMatthew Ahrens     zio_priority_t priority, enum zio_flag flags, boolean_t labels)
1187fa9e4066Sahrens {
1188fa9e4066Sahrens 	zio_t *zio;
11890a4e9518Sgw 
1190e14bb325SJeff Bonwick 	ASSERT(vd->vdev_children == 0);
1191e14bb325SJeff Bonwick 	ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE ||
1192e14bb325SJeff Bonwick 	    offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
1193e14bb325SJeff Bonwick 	ASSERT3U(offset + size, <=, vd->vdev_psize);
1194fa9e4066Sahrens 
11955602294fSDan Kimmel 	zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, size, done,
11965602294fSDan Kimmel 	    private, ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_PHYSICAL, vd,
11975602294fSDan Kimmel 	    offset, NULL, ZIO_STAGE_OPEN, ZIO_WRITE_PHYS_PIPELINE);
1198fa9e4066Sahrens 
1199e14bb325SJeff Bonwick 	zio->io_prop.zp_checksum = checksum;
1200fa9e4066Sahrens 
120145818ee1SMatthew Ahrens 	if (zio_checksum_table[checksum].ci_flags & ZCHECKSUM_FLAG_EMBEDDED) {
1202fa9e4066Sahrens 		/*
12036e1f5caaSNeil Perrin 		 * zec checksums are necessarily destructive -- they modify
1204e14bb325SJeff Bonwick 		 * the end of the write buffer to hold the verifier/checksum.
1205fa9e4066Sahrens 		 * Therefore, we must make a local copy in case the data is
1206e14bb325SJeff Bonwick 		 * being written to multiple places in parallel.
1207fa9e4066Sahrens 		 */
1208770499e1SDan Kimmel 		abd_t *wbuf = abd_alloc_sametype(data, size);
1209770499e1SDan Kimmel 		abd_copy(wbuf, data, size);
1210770499e1SDan Kimmel 
1211e14bb325SJeff Bonwick 		zio_push_transform(zio, wbuf, size, size, NULL);
1212fa9e4066Sahrens 	}
1213fa9e4066Sahrens 
1214fa9e4066Sahrens 	return (zio);
1215fa9e4066Sahrens }
1216fa9e4066Sahrens 
1217fa9e4066Sahrens /*
1218e14bb325SJeff Bonwick  * Create a child I/O to do some work for us.
1219fa9e4066Sahrens  */
1220fa9e4066Sahrens zio_t *
1221e14bb325SJeff Bonwick zio_vdev_child_io(zio_t *pio, blkptr_t *bp, vdev_t *vd, uint64_t offset,
1222770499e1SDan Kimmel     abd_t *data, uint64_t size, int type, zio_priority_t priority,
1223dcbf3bd6SGeorge Wilson     enum zio_flag flags, zio_done_func_t *done, void *private)
1224fa9e4066Sahrens {
1225b24ab676SJeff Bonwick 	enum zio_stage pipeline = ZIO_VDEV_CHILD_PIPELINE;
1226e14bb325SJeff Bonwick 	zio_t *zio;
1227e14bb325SJeff Bonwick 
12285cabbc6bSPrashanth Sreenivasa 	/*
12295cabbc6bSPrashanth Sreenivasa 	 * vdev child I/Os do not propagate their error to the parent.
12305cabbc6bSPrashanth Sreenivasa 	 * Therefore, for correct operation the caller *must* check for
12315cabbc6bSPrashanth Sreenivasa 	 * and handle the error in the child i/o's done callback.
12325cabbc6bSPrashanth Sreenivasa 	 * The only exceptions are i/os that we don't care about
12335cabbc6bSPrashanth Sreenivasa 	 * (OPTIONAL or REPAIR).
12345cabbc6bSPrashanth Sreenivasa 	 */
12355cabbc6bSPrashanth Sreenivasa 	ASSERT((flags & ZIO_FLAG_OPTIONAL) || (flags & ZIO_FLAG_IO_REPAIR) ||
12365cabbc6bSPrashanth Sreenivasa 	    done != NULL);
12375cabbc6bSPrashanth Sreenivasa 
1238fa9e4066Sahrens 	if (type == ZIO_TYPE_READ && bp != NULL) {
1239fa9e4066Sahrens 		/*
1240fa9e4066Sahrens 		 * If we have the bp, then the child should perform the
1241fa9e4066Sahrens 		 * checksum and the parent need not.  This pushes error
1242fa9e4066Sahrens 		 * detection as close to the leaves as possible and
1243fa9e4066Sahrens 		 * eliminates redundant checksums in the interior nodes.
1244fa9e4066Sahrens 		 */
1245b24ab676SJeff Bonwick 		pipeline |= ZIO_STAGE_CHECKSUM_VERIFY;
1246b24ab676SJeff Bonwick 		pio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY;
1247fa9e4066Sahrens 	}
1248fa9e4066Sahrens 
12495cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_ops->vdev_op_leaf) {
12505cabbc6bSPrashanth Sreenivasa 		ASSERT0(vd->vdev_children);
1251e14bb325SJeff Bonwick 		offset += VDEV_LABEL_START_SIZE;
12525cabbc6bSPrashanth Sreenivasa 	}
1253e14bb325SJeff Bonwick 
12545cabbc6bSPrashanth Sreenivasa 	flags |= ZIO_VDEV_CHILD_FLAGS(pio);
1255b24ab676SJeff Bonwick 
1256b24ab676SJeff Bonwick 	/*
1257b24ab676SJeff Bonwick 	 * If we've decided to do a repair, the write is not speculative --
1258b24ab676SJeff Bonwick 	 * even if the original read was.
1259b24ab676SJeff Bonwick 	 */
1260b24ab676SJeff Bonwick 	if (flags & ZIO_FLAG_IO_REPAIR)
1261b24ab676SJeff Bonwick 		flags &= ~ZIO_FLAG_SPECULATIVE;
1262b24ab676SJeff Bonwick 
12630f7643c7SGeorge Wilson 	/*
12640f7643c7SGeorge Wilson 	 * If we're creating a child I/O that is not associated with a
12650f7643c7SGeorge Wilson 	 * top-level vdev, then the child zio is not an allocating I/O.
12660f7643c7SGeorge Wilson 	 * If this is a retried I/O then we ignore it since we will
12670f7643c7SGeorge Wilson 	 * have already processed the original allocating I/O.
12680f7643c7SGeorge Wilson 	 */
12690f7643c7SGeorge Wilson 	if (flags & ZIO_FLAG_IO_ALLOCATING &&
12700f7643c7SGeorge Wilson 	    (vd != vd->vdev_top || (flags & ZIO_FLAG_IO_RETRY))) {
1271663207adSDon Brady 		ASSERT(pio->io_metaslab_class != NULL);
1272663207adSDon Brady 		ASSERT(pio->io_metaslab_class->mc_alloc_throttle_enabled);
12730f7643c7SGeorge Wilson 		ASSERT(type == ZIO_TYPE_WRITE);
12740f7643c7SGeorge Wilson 		ASSERT(priority == ZIO_PRIORITY_ASYNC_WRITE);
12750f7643c7SGeorge Wilson 		ASSERT(!(flags & ZIO_FLAG_IO_REPAIR));
12760f7643c7SGeorge Wilson 		ASSERT(!(pio->io_flags & ZIO_FLAG_IO_REWRITE) ||
12770f7643c7SGeorge Wilson 		    pio->io_child_type == ZIO_CHILD_GANG);
12780f7643c7SGeorge Wilson 
12790f7643c7SGeorge Wilson 		flags &= ~ZIO_FLAG_IO_ALLOCATING;
12800f7643c7SGeorge Wilson 	}
12810f7643c7SGeorge Wilson 
12825602294fSDan Kimmel 	zio = zio_create(pio, pio->io_spa, pio->io_txg, bp, data, size, size,
1283b24ab676SJeff Bonwick 	    done, private, type, priority, flags, vd, offset, &pio->io_bookmark,
1284b24ab676SJeff Bonwick 	    ZIO_STAGE_VDEV_IO_START >> 1, pipeline);
12850f7643c7SGeorge Wilson 	ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_VDEV);
1286fa9e4066Sahrens 
128769962b56SMatthew Ahrens 	zio->io_physdone = pio->io_physdone;
128869962b56SMatthew Ahrens 	if (vd->vdev_ops->vdev_op_leaf && zio->io_logical != NULL)
128969962b56SMatthew Ahrens 		zio->io_logical->io_phys_children++;
129069962b56SMatthew Ahrens 
1291e14bb325SJeff Bonwick 	return (zio);
129232b87932Sek }
129332b87932Sek 
1294e14bb325SJeff Bonwick zio_t *
1295770499e1SDan Kimmel zio_vdev_delegated_io(vdev_t *vd, uint64_t offset, abd_t *data, uint64_t size,
12963a4b1be9SMatthew Ahrens     zio_type_t type, zio_priority_t priority, enum zio_flag flags,
12979a686fbcSPaul Dagnelie     zio_done_func_t *done, void *private)
1298fa9e4066Sahrens {
1299e14bb325SJeff Bonwick 	zio_t *zio;
1300fa9e4066Sahrens 
1301e14bb325SJeff Bonwick 	ASSERT(vd->vdev_ops->vdev_op_leaf);
1302fa9e4066Sahrens 
1303e14bb325SJeff Bonwick 	zio = zio_create(NULL, vd->vdev_spa, 0, NULL,
13045602294fSDan Kimmel 	    data, size, size, done, private, type, priority,
130569962b56SMatthew Ahrens 	    flags | ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY | ZIO_FLAG_DELEGATED,
1306e14bb325SJeff Bonwick 	    vd, offset, NULL,
1307b24ab676SJeff Bonwick 	    ZIO_STAGE_VDEV_IO_START >> 1, ZIO_VDEV_CHILD_PIPELINE);
1308fa9e4066Sahrens 
1309e14bb325SJeff Bonwick 	return (zio);
1310e05725b1Sbonwick }
1311e05725b1Sbonwick 
1312e05725b1Sbonwick void
1313e14bb325SJeff Bonwick zio_flush(zio_t *zio, vdev_t *vd)
1314e05725b1Sbonwick {
1315e14bb325SJeff Bonwick 	zio_nowait(zio_ioctl(zio, zio->io_spa, vd, DKIOCFLUSHWRITECACHE,
131669962b56SMatthew Ahrens 	    NULL, NULL,
1317e14bb325SJeff Bonwick 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY));
1318fa9e4066Sahrens }
1319fa9e4066Sahrens 
13206e1f5caaSNeil Perrin void
13216e1f5caaSNeil Perrin zio_shrink(zio_t *zio, uint64_t size)
13226e1f5caaSNeil Perrin {
13231271e4b1SPrakash Surya 	ASSERT3P(zio->io_executor, ==, NULL);
13241271e4b1SPrakash Surya 	ASSERT3P(zio->io_orig_size, ==, zio->io_size);
13251271e4b1SPrakash Surya 	ASSERT3U(size, <=, zio->io_size);
13266e1f5caaSNeil Perrin 
13276e1f5caaSNeil Perrin 	/*
13286e1f5caaSNeil Perrin 	 * We don't shrink for raidz because of problems with the
13296e1f5caaSNeil Perrin 	 * reconstruction when reading back less than the block size.
13306e1f5caaSNeil Perrin 	 * Note, BP_IS_RAIDZ() assumes no compression.
13316e1f5caaSNeil Perrin 	 */
13326e1f5caaSNeil Perrin 	ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF);
13335602294fSDan Kimmel 	if (!BP_IS_RAIDZ(zio->io_bp)) {
13345602294fSDan Kimmel 		/* we are not doing a raw write */
13355602294fSDan Kimmel 		ASSERT3U(zio->io_size, ==, zio->io_lsize);
13365602294fSDan Kimmel 		zio->io_orig_size = zio->io_size = zio->io_lsize = size;
13375602294fSDan Kimmel 	}
13386e1f5caaSNeil Perrin }
13396e1f5caaSNeil Perrin 
1340fa9e4066Sahrens /*
1341fa9e4066Sahrens  * ==========================================================================
1342e14bb325SJeff Bonwick  * Prepare to read and write logical blocks
1343fa9e4066Sahrens  * ==========================================================================
1344fa9e4066Sahrens  */
1345e14bb325SJeff Bonwick 
1346e05725b1Sbonwick static int
1347e14bb325SJeff Bonwick zio_read_bp_init(zio_t *zio)
1348fa9e4066Sahrens {
1349e14bb325SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
1350eb633035STom Caputi 	uint64_t psize =
1351eb633035STom Caputi 	    BP_IS_EMBEDDED(bp) ? BPE_GET_PSIZE(bp) : BP_GET_PSIZE(bp);
1352e05725b1Sbonwick 
13535cabbc6bSPrashanth Sreenivasa 	ASSERT3P(zio->io_bp, ==, &zio->io_bp_copy);
13545cabbc6bSPrashanth Sreenivasa 
135503361682SJeff Bonwick 	if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF &&
1356f5383399SBill Moore 	    zio->io_child_type == ZIO_CHILD_LOGICAL &&
1357eb633035STom Caputi 	    !(zio->io_flags & ZIO_FLAG_RAW_COMPRESS)) {
1358770499e1SDan Kimmel 		zio_push_transform(zio, abd_alloc_sametype(zio->io_abd, psize),
1359770499e1SDan Kimmel 		    psize, psize, zio_decompress);
1360e14bb325SJeff Bonwick 	}
1361fa9e4066Sahrens 
1362eb633035STom Caputi 	if (((BP_IS_PROTECTED(bp) && !(zio->io_flags & ZIO_FLAG_RAW_ENCRYPT)) ||
1363eb633035STom Caputi 	    BP_HAS_INDIRECT_MAC_CKSUM(bp)) &&
1364eb633035STom Caputi 	    zio->io_child_type == ZIO_CHILD_LOGICAL) {
1365eb633035STom Caputi 		zio_push_transform(zio, abd_alloc_sametype(zio->io_abd, psize),
1366eb633035STom Caputi 		    psize, psize, zio_decrypt);
1367eb633035STom Caputi 	}
1368770499e1SDan Kimmel 
1369eb633035STom Caputi 	if (BP_IS_EMBEDDED(bp) && BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA) {
1370770499e1SDan Kimmel 		int psize = BPE_GET_PSIZE(bp);
1371770499e1SDan Kimmel 		void *data = abd_borrow_buf(zio->io_abd, psize);
1372eb633035STom Caputi 
1373eb633035STom Caputi 		zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1374770499e1SDan Kimmel 		decode_embedded_bp_compressed(bp, data);
1375770499e1SDan Kimmel 		abd_return_buf_copy(zio->io_abd, data, psize);
13765d7b4d43SMatthew Ahrens 	} else {
13775d7b4d43SMatthew Ahrens 		ASSERT(!BP_IS_EMBEDDED(bp));
13785cabbc6bSPrashanth Sreenivasa 		ASSERT3P(zio->io_bp, ==, &zio->io_bp_copy);
13795d7b4d43SMatthew Ahrens 	}
13805d7b4d43SMatthew Ahrens 
1381ad135b5dSChristopher Siden 	if (!DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) && BP_GET_LEVEL(bp) == 0)
1382e14bb325SJeff Bonwick 		zio->io_flags |= ZIO_FLAG_DONT_CACHE;
1383fa9e4066Sahrens 
1384bbfd46c4SJeff Bonwick 	if (BP_GET_TYPE(bp) == DMU_OT_DDT_ZAP)
1385bbfd46c4SJeff Bonwick 		zio->io_flags |= ZIO_FLAG_DONT_CACHE;
1386bbfd46c4SJeff Bonwick 
1387b24ab676SJeff Bonwick 	if (BP_GET_DEDUP(bp) && zio->io_child_type == ZIO_CHILD_LOGICAL)
1388b24ab676SJeff Bonwick 		zio->io_pipeline = ZIO_DDT_READ_PIPELINE;
1389b24ab676SJeff Bonwick 
1390e14bb325SJeff Bonwick 	return (ZIO_PIPELINE_CONTINUE);
1391fa9e4066Sahrens }
1392fa9e4066Sahrens 
1393e05725b1Sbonwick static int
1394e14bb325SJeff Bonwick zio_write_bp_init(zio_t *zio)
13950a4e9518Sgw {
1396e14bb325SJeff Bonwick 	if (!IO_IS_ALLOCATING(zio))
1397e14bb325SJeff Bonwick 		return (ZIO_PIPELINE_CONTINUE);
13980a4e9518Sgw 
1399b24ab676SJeff Bonwick 	ASSERT(zio->io_child_type != ZIO_CHILD_DDT);
1400b24ab676SJeff Bonwick 
1401b24ab676SJeff Bonwick 	if (zio->io_bp_override) {
14020f7643c7SGeorge Wilson 		blkptr_t *bp = zio->io_bp;
14030f7643c7SGeorge Wilson 		zio_prop_t *zp = &zio->io_prop;
14040f7643c7SGeorge Wilson 
1405b24ab676SJeff Bonwick 		ASSERT(bp->blk_birth != zio->io_txg);
1406b24ab676SJeff Bonwick 		ASSERT(BP_GET_DEDUP(zio->io_bp_override) == 0);
1407b24ab676SJeff Bonwick 
1408b24ab676SJeff Bonwick 		*bp = *zio->io_bp_override;
1409b24ab676SJeff Bonwick 		zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1410b24ab676SJeff Bonwick 
14115d7b4d43SMatthew Ahrens 		if (BP_IS_EMBEDDED(bp))
14125d7b4d43SMatthew Ahrens 			return (ZIO_PIPELINE_CONTINUE);
14135d7b4d43SMatthew Ahrens 
141480901aeaSGeorge Wilson 		/*
141580901aeaSGeorge Wilson 		 * If we've been overridden and nopwrite is set then
141680901aeaSGeorge Wilson 		 * set the flag accordingly to indicate that a nopwrite
141780901aeaSGeorge Wilson 		 * has already occurred.
141880901aeaSGeorge Wilson 		 */
141980901aeaSGeorge Wilson 		if (!BP_IS_HOLE(bp) && zp->zp_nopwrite) {
142080901aeaSGeorge Wilson 			ASSERT(!zp->zp_dedup);
14210f7643c7SGeorge Wilson 			ASSERT3U(BP_GET_CHECKSUM(bp), ==, zp->zp_checksum);
142280901aeaSGeorge Wilson 			zio->io_flags |= ZIO_FLAG_NOPWRITE;
142380901aeaSGeorge Wilson 			return (ZIO_PIPELINE_CONTINUE);
142480901aeaSGeorge Wilson 		}
142580901aeaSGeorge Wilson 
142680901aeaSGeorge Wilson 		ASSERT(!zp->zp_nopwrite);
142780901aeaSGeorge Wilson 
1428b24ab676SJeff Bonwick 		if (BP_IS_HOLE(bp) || !zp->zp_dedup)
1429b24ab676SJeff Bonwick 			return (ZIO_PIPELINE_CONTINUE);
1430b24ab676SJeff Bonwick 
143145818ee1SMatthew Ahrens 		ASSERT((zio_checksum_table[zp->zp_checksum].ci_flags &
143245818ee1SMatthew Ahrens 		    ZCHECKSUM_FLAG_DEDUP) || zp->zp_dedup_verify);
1433b24ab676SJeff Bonwick 
1434eb633035STom Caputi 		if (BP_GET_CHECKSUM(bp) == zp->zp_checksum &&
1435eb633035STom Caputi 		    !zp->zp_encrypt) {
1436b24ab676SJeff Bonwick 			BP_SET_DEDUP(bp, 1);
1437b24ab676SJeff Bonwick 			zio->io_pipeline |= ZIO_STAGE_DDT_WRITE;
1438b24ab676SJeff Bonwick 			return (ZIO_PIPELINE_CONTINUE);
1439b24ab676SJeff Bonwick 		}
14400f7643c7SGeorge Wilson 
14410f7643c7SGeorge Wilson 		/*
14420f7643c7SGeorge Wilson 		 * We were unable to handle this as an override bp, treat
14430f7643c7SGeorge Wilson 		 * it as a regular write I/O.
14440f7643c7SGeorge Wilson 		 */
1445b39b744bSMatthew Ahrens 		zio->io_bp_override = NULL;
14460f7643c7SGeorge Wilson 		*bp = zio->io_bp_orig;
14470f7643c7SGeorge Wilson 		zio->io_pipeline = zio->io_orig_pipeline;
1448b24ab676SJeff Bonwick 	}
14490a4e9518Sgw 
14500f7643c7SGeorge Wilson 	return (ZIO_PIPELINE_CONTINUE);
14510f7643c7SGeorge Wilson }
14520f7643c7SGeorge Wilson 
14530f7643c7SGeorge Wilson static int
14540f7643c7SGeorge Wilson zio_write_compress(zio_t *zio)
14550f7643c7SGeorge Wilson {
14560f7643c7SGeorge Wilson 	spa_t *spa = zio->io_spa;
14570f7643c7SGeorge Wilson 	zio_prop_t *zp = &zio->io_prop;
14580f7643c7SGeorge Wilson 	enum zio_compress compress = zp->zp_compress;
14590f7643c7SGeorge Wilson 	blkptr_t *bp = zio->io_bp;
14605602294fSDan Kimmel 	uint64_t lsize = zio->io_lsize;
14615602294fSDan Kimmel 	uint64_t psize = zio->io_size;
14620f7643c7SGeorge Wilson 	int pass = 1;
14630f7643c7SGeorge Wilson 
14640f7643c7SGeorge Wilson 	/*
14650f7643c7SGeorge Wilson 	 * If our children haven't all reached the ready stage,
14660f7643c7SGeorge Wilson 	 * wait for them and then repeat this pipeline stage.
14670f7643c7SGeorge Wilson 	 */
1468d6e1c446SGeorge Wilson 	if (zio_wait_for_children(zio, ZIO_CHILD_LOGICAL_BIT |
1469d6e1c446SGeorge Wilson 	    ZIO_CHILD_GANG_BIT, ZIO_WAIT_READY)) {
14700f7643c7SGeorge Wilson 		return (ZIO_PIPELINE_STOP);
1471d6e1c446SGeorge Wilson 	}
14720f7643c7SGeorge Wilson 
14730f7643c7SGeorge Wilson 	if (!IO_IS_ALLOCATING(zio))
14740f7643c7SGeorge Wilson 		return (ZIO_PIPELINE_CONTINUE);
14750f7643c7SGeorge Wilson 
14760f7643c7SGeorge Wilson 	if (zio->io_children_ready != NULL) {
14770f7643c7SGeorge Wilson 		/*
14780f7643c7SGeorge Wilson 		 * Now that all our children are ready, run the callback
14790f7643c7SGeorge Wilson 		 * associated with this zio in case it wants to modify the
14800f7643c7SGeorge Wilson 		 * data to be written.
14810f7643c7SGeorge Wilson 		 */
14820f7643c7SGeorge Wilson 		ASSERT3U(zp->zp_level, >, 0);
14830f7643c7SGeorge Wilson 		zio->io_children_ready(zio);
14840f7643c7SGeorge Wilson 	}
14850f7643c7SGeorge Wilson 
14860f7643c7SGeorge Wilson 	ASSERT(zio->io_child_type != ZIO_CHILD_DDT);
14870f7643c7SGeorge Wilson 	ASSERT(zio->io_bp_override == NULL);
14880f7643c7SGeorge Wilson 
148943466aaeSMax Grossman 	if (!BP_IS_HOLE(bp) && bp->blk_birth == zio->io_txg) {
1490e14bb325SJeff Bonwick 		/*
1491e14bb325SJeff Bonwick 		 * We're rewriting an existing block, which means we're
1492e14bb325SJeff Bonwick 		 * working on behalf of spa_sync().  For spa_sync() to
1493e14bb325SJeff Bonwick 		 * converge, it must eventually be the case that we don't
1494e14bb325SJeff Bonwick 		 * have to allocate new blocks.  But compression changes
1495e14bb325SJeff Bonwick 		 * the blocksize, which forces a reallocate, and makes
1496e14bb325SJeff Bonwick 		 * convergence take longer.  Therefore, after the first
1497e14bb325SJeff Bonwick 		 * few passes, stop compressing to ensure convergence.
1498e14bb325SJeff Bonwick 		 */
1499b24ab676SJeff Bonwick 		pass = spa_sync_pass(spa);
1500b24ab676SJeff Bonwick 
1501b24ab676SJeff Bonwick 		ASSERT(zio->io_txg == spa_syncing_txg(spa));
1502b24ab676SJeff Bonwick 		ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1503b24ab676SJeff Bonwick 		ASSERT(!BP_GET_DEDUP(bp));
1504e05725b1Sbonwick 
150501f55e48SGeorge Wilson 		if (pass >= zfs_sync_pass_dont_compress)
1506e14bb325SJeff Bonwick 			compress = ZIO_COMPRESS_OFF;
1507e05725b1Sbonwick 
1508e14bb325SJeff Bonwick 		/* Make sure someone doesn't change their mind on overwrites */
15095d7b4d43SMatthew Ahrens 		ASSERT(BP_IS_EMBEDDED(bp) || MIN(zp->zp_copies + BP_IS_GANG(bp),
1510b24ab676SJeff Bonwick 		    spa_max_replication(spa)) == BP_GET_NDVAS(bp));
1511e14bb325SJeff Bonwick 	}
1512fa9e4066Sahrens 
15135602294fSDan Kimmel 	/* If it's a compressed write that is not raw, compress the buffer. */
1514eb633035STom Caputi 	if (compress != ZIO_COMPRESS_OFF &&
1515eb633035STom Caputi 	    !(zio->io_flags & ZIO_FLAG_RAW_COMPRESS)) {
1516b24ab676SJeff Bonwick 		void *cbuf = zio_buf_alloc(lsize);
1517770499e1SDan Kimmel 		psize = zio_compress_data(compress, zio->io_abd, cbuf, lsize);
1518b24ab676SJeff Bonwick 		if (psize == 0 || psize == lsize) {
1519e14bb325SJeff Bonwick 			compress = ZIO_COMPRESS_OFF;
1520b24ab676SJeff Bonwick 			zio_buf_free(cbuf, lsize);
1521eb633035STom Caputi 		} else if (!zp->zp_dedup && !zp->zp_encrypt &&
1522eb633035STom Caputi 		    psize <= BPE_PAYLOAD_SIZE &&
15235d7b4d43SMatthew Ahrens 		    zp->zp_level == 0 && !DMU_OT_HAS_FILL(zp->zp_type) &&
15245d7b4d43SMatthew Ahrens 		    spa_feature_is_enabled(spa, SPA_FEATURE_EMBEDDED_DATA)) {
15255d7b4d43SMatthew Ahrens 			encode_embedded_bp_compressed(bp,
15265d7b4d43SMatthew Ahrens 			    cbuf, compress, lsize, psize);
15275d7b4d43SMatthew Ahrens 			BPE_SET_ETYPE(bp, BP_EMBEDDED_TYPE_DATA);
15285d7b4d43SMatthew Ahrens 			BP_SET_TYPE(bp, zio->io_prop.zp_type);
15295d7b4d43SMatthew Ahrens 			BP_SET_LEVEL(bp, zio->io_prop.zp_level);
15305d7b4d43SMatthew Ahrens 			zio_buf_free(cbuf, lsize);
15315d7b4d43SMatthew Ahrens 			bp->blk_birth = zio->io_txg;
15325d7b4d43SMatthew Ahrens 			zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
15335d7b4d43SMatthew Ahrens 			ASSERT(spa_feature_is_active(spa,
15345d7b4d43SMatthew Ahrens 			    SPA_FEATURE_EMBEDDED_DATA));
15355d7b4d43SMatthew Ahrens 			return (ZIO_PIPELINE_CONTINUE);
1536b24ab676SJeff Bonwick 		} else {
15375d7b4d43SMatthew Ahrens 			/*
153881cd5c55SMatthew Ahrens 			 * Round up compressed size up to the ashift
153981cd5c55SMatthew Ahrens 			 * of the smallest-ashift device, and zero the tail.
154081cd5c55SMatthew Ahrens 			 * This ensures that the compressed size of the BP
154181cd5c55SMatthew Ahrens 			 * (and thus compressratio property) are correct,
154281cd5c55SMatthew Ahrens 			 * in that we charge for the padding used to fill out
154381cd5c55SMatthew Ahrens 			 * the last sector.
15445d7b4d43SMatthew Ahrens 			 */
154581cd5c55SMatthew Ahrens 			ASSERT3U(spa->spa_min_ashift, >=, SPA_MINBLOCKSHIFT);
154681cd5c55SMatthew Ahrens 			size_t rounded = (size_t)P2ROUNDUP(psize,
154781cd5c55SMatthew Ahrens 			    1ULL << spa->spa_min_ashift);
154881cd5c55SMatthew Ahrens 			if (rounded >= lsize) {
15495d7b4d43SMatthew Ahrens 				compress = ZIO_COMPRESS_OFF;
15505d7b4d43SMatthew Ahrens 				zio_buf_free(cbuf, lsize);
155181cd5c55SMatthew Ahrens 				psize = lsize;
15525d7b4d43SMatthew Ahrens 			} else {
1553770499e1SDan Kimmel 				abd_t *cdata = abd_get_from_buf(cbuf, lsize);
1554770499e1SDan Kimmel 				abd_take_ownership_of_buf(cdata, B_TRUE);
1555770499e1SDan Kimmel 				abd_zero_off(cdata, psize, rounded - psize);
155681cd5c55SMatthew Ahrens 				psize = rounded;
1557770499e1SDan Kimmel 				zio_push_transform(zio, cdata,
15585d7b4d43SMatthew Ahrens 				    psize, lsize, NULL);
15595d7b4d43SMatthew Ahrens 			}
1560e14bb325SJeff Bonwick 		}
15610f7643c7SGeorge Wilson 
15620f7643c7SGeorge Wilson 		/*
15630f7643c7SGeorge Wilson 		 * We were unable to handle this as an override bp, treat
15640f7643c7SGeorge Wilson 		 * it as a regular write I/O.
15650f7643c7SGeorge Wilson 		 */
15660f7643c7SGeorge Wilson 		zio->io_bp_override = NULL;
15670f7643c7SGeorge Wilson 		*bp = zio->io_bp_orig;
15680f7643c7SGeorge Wilson 		zio->io_pipeline = zio->io_orig_pipeline;
1569eb633035STom Caputi 
1570eb633035STom Caputi 	} else if ((zio->io_flags & ZIO_FLAG_RAW_ENCRYPT) != 0 &&
1571eb633035STom Caputi 	    zp->zp_type == DMU_OT_DNODE) {
1572eb633035STom Caputi 		/*
1573eb633035STom Caputi 		 * The DMU actually relies on the zio layer's compression
1574eb633035STom Caputi 		 * to free metadnode blocks that have had all contained
1575eb633035STom Caputi 		 * dnodes freed. As a result, even when doing a raw
1576eb633035STom Caputi 		 * receive, we must check whether the block can be compressed
1577eb633035STom Caputi 		 * to a hole.
1578eb633035STom Caputi 		 */
1579eb633035STom Caputi 		psize = zio_compress_data(ZIO_COMPRESS_EMPTY,
1580eb633035STom Caputi 		    zio->io_abd, NULL, lsize);
1581eb633035STom Caputi 		if (psize == 0)
1582eb633035STom Caputi 			compress = ZIO_COMPRESS_OFF;
15835602294fSDan Kimmel 	} else {
15845602294fSDan Kimmel 		ASSERT3U(psize, !=, 0);
1585e14bb325SJeff Bonwick 	}
1586c717a561Smaybee 
1587e14bb325SJeff Bonwick 	/*
1588e14bb325SJeff Bonwick 	 * The final pass of spa_sync() must be all rewrites, but the first
1589e14bb325SJeff Bonwick 	 * few passes offer a trade-off: allocating blocks defers convergence,
1590e14bb325SJeff Bonwick 	 * but newly allocated blocks are sequential, so they can be written
1591e14bb325SJeff Bonwick 	 * to disk faster.  Therefore, we allow the first few passes of
1592e14bb325SJeff Bonwick 	 * spa_sync() to allocate new blocks, but force rewrites after that.
1593e14bb325SJeff Bonwick 	 * There should only be a handful of blocks after pass 1 in any case.
1594e14bb325SJeff Bonwick 	 */
159543466aaeSMax Grossman 	if (!BP_IS_HOLE(bp) && bp->blk_birth == zio->io_txg &&
159643466aaeSMax Grossman 	    BP_GET_PSIZE(bp) == psize &&
159701f55e48SGeorge Wilson 	    pass >= zfs_sync_pass_rewrite) {
1598663207adSDon Brady 		VERIFY3U(psize, !=, 0);
1599b24ab676SJeff Bonwick 		enum zio_stage gang_stages = zio->io_pipeline & ZIO_GANG_STAGES;
1600e14bb325SJeff Bonwick 		zio->io_pipeline = ZIO_REWRITE_PIPELINE | gang_stages;
1601e14bb325SJeff Bonwick 		zio->io_flags |= ZIO_FLAG_IO_REWRITE;
1602e14bb325SJeff Bonwick 	} else {
1603e14bb325SJeff Bonwick 		BP_ZERO(bp);
1604e14bb325SJeff Bonwick 		zio->io_pipeline = ZIO_WRITE_PIPELINE;
1605e14bb325SJeff Bonwick 	}
1606fa9e4066Sahrens 
1607b24ab676SJeff Bonwick 	if (psize == 0) {
160843466aaeSMax Grossman 		if (zio->io_bp_orig.blk_birth != 0 &&
160943466aaeSMax Grossman 		    spa_feature_is_active(spa, SPA_FEATURE_HOLE_BIRTH)) {
161043466aaeSMax Grossman 			BP_SET_LSIZE(bp, lsize);
161143466aaeSMax Grossman 			BP_SET_TYPE(bp, zp->zp_type);
161243466aaeSMax Grossman 			BP_SET_LEVEL(bp, zp->zp_level);
161343466aaeSMax Grossman 			BP_SET_BIRTH(bp, zio->io_txg, 0);
161443466aaeSMax Grossman 		}
1615e14bb325SJeff Bonwick 		zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1616e14bb325SJeff Bonwick 	} else {
1617e14bb325SJeff Bonwick 		ASSERT(zp->zp_checksum != ZIO_CHECKSUM_GANG_HEADER);
1618e14bb325SJeff Bonwick 		BP_SET_LSIZE(bp, lsize);
161943466aaeSMax Grossman 		BP_SET_TYPE(bp, zp->zp_type);
162043466aaeSMax Grossman 		BP_SET_LEVEL(bp, zp->zp_level);
1621b24ab676SJeff Bonwick 		BP_SET_PSIZE(bp, psize);
1622e14bb325SJeff Bonwick 		BP_SET_COMPRESS(bp, compress);
1623e14bb325SJeff Bonwick 		BP_SET_CHECKSUM(bp, zp->zp_checksum);
1624b24ab676SJeff Bonwick 		BP_SET_DEDUP(bp, zp->zp_dedup);
1625e14bb325SJeff Bonwick 		BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
1626b24ab676SJeff Bonwick 		if (zp->zp_dedup) {
1627b24ab676SJeff Bonwick 			ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1628b24ab676SJeff Bonwick 			ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
1629eb633035STom Caputi 			ASSERT(!zp->zp_encrypt ||
1630eb633035STom Caputi 			    DMU_OT_IS_ENCRYPTED(zp->zp_type));
1631b24ab676SJeff Bonwick 			zio->io_pipeline = ZIO_DDT_WRITE_PIPELINE;
1632b24ab676SJeff Bonwick 		}
163380901aeaSGeorge Wilson 		if (zp->zp_nopwrite) {
163480901aeaSGeorge Wilson 			ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
163580901aeaSGeorge Wilson 			ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
163680901aeaSGeorge Wilson 			zio->io_pipeline |= ZIO_STAGE_NOP_WRITE;
163780901aeaSGeorge Wilson 		}
1638b24ab676SJeff Bonwick 	}
1639b24ab676SJeff Bonwick 	return (ZIO_PIPELINE_CONTINUE);
1640b24ab676SJeff Bonwick }
1641b24ab676SJeff Bonwick 
1642b24ab676SJeff Bonwick static int
1643b24ab676SJeff Bonwick zio_free_bp_init(zio_t *zio)
1644b24ab676SJeff Bonwick {
1645b24ab676SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
1646b24ab676SJeff Bonwick 
1647b24ab676SJeff Bonwick 	if (zio->io_child_type == ZIO_CHILD_LOGICAL) {
1648b24ab676SJeff Bonwick 		if (BP_GET_DEDUP(bp))
1649b24ab676SJeff Bonwick 			zio->io_pipeline = ZIO_DDT_FREE_PIPELINE;
1650e14bb325SJeff Bonwick 	}
1651fa9e4066Sahrens 
16525cabbc6bSPrashanth Sreenivasa 	ASSERT3P(zio->io_bp, ==, &zio->io_bp_copy);
16535cabbc6bSPrashanth Sreenivasa 
1654e05725b1Sbonwick 	return (ZIO_PIPELINE_CONTINUE);
1655fa9e4066Sahrens }
1656fa9e4066Sahrens 
1657e14bb325SJeff Bonwick /*
1658e14bb325SJeff Bonwick  * ==========================================================================
1659e14bb325SJeff Bonwick  * Execute the I/O pipeline
1660e14bb325SJeff Bonwick  * ==========================================================================
1661e14bb325SJeff Bonwick  */
1662e14bb325SJeff Bonwick 
1663e14bb325SJeff Bonwick static void
1664ec94d322SAdam Leventhal zio_taskq_dispatch(zio_t *zio, zio_taskq_type_t q, boolean_t cutinline)
1665fa9e4066Sahrens {
166680eb36f2SGeorge Wilson 	spa_t *spa = zio->io_spa;
1667e14bb325SJeff Bonwick 	zio_type_t t = zio->io_type;
16685aeb9474SGarrett D'Amore 	int flags = (cutinline ? TQ_FRONT : 0);
16690a4e9518Sgw 
16700a4e9518Sgw 	/*
1671bbe36defSGeorge Wilson 	 * If we're a config writer or a probe, the normal issue and
1672bbe36defSGeorge Wilson 	 * interrupt threads may all be blocked waiting for the config lock.
1673bbe36defSGeorge Wilson 	 * In this case, select the otherwise-unused taskq for ZIO_TYPE_NULL.
16740a4e9518Sgw 	 */
1675bbe36defSGeorge Wilson 	if (zio->io_flags & (ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_PROBE))
1676e14bb325SJeff Bonwick 		t = ZIO_TYPE_NULL;
16770a4e9518Sgw 
16780a4e9518Sgw 	/*
1679e14bb325SJeff Bonwick 	 * A similar issue exists for the L2ARC write thread until L2ARC 2.0.
16800a4e9518Sgw 	 */
1681e14bb325SJeff Bonwick 	if (t == ZIO_TYPE_WRITE && zio->io_vd && zio->io_vd->vdev_aux)
1682e14bb325SJeff Bonwick 		t = ZIO_TYPE_NULL;
16830a4e9518Sgw 
168480eb36f2SGeorge Wilson 	/*
1685ec94d322SAdam Leventhal 	 * If this is a high priority I/O, then use the high priority taskq if
1686ec94d322SAdam Leventhal 	 * available.
168780eb36f2SGeorge Wilson 	 */
16882258ad0bSGeorge Wilson 	if ((zio->io_priority == ZIO_PRIORITY_NOW ||
16892258ad0bSGeorge Wilson 	    zio->io_priority == ZIO_PRIORITY_SYNC_WRITE) &&
1690ec94d322SAdam Leventhal 	    spa->spa_zio_taskq[t][q + 1].stqs_count != 0)
169180eb36f2SGeorge Wilson 		q++;
169280eb36f2SGeorge Wilson 
169380eb36f2SGeorge Wilson 	ASSERT3U(q, <, ZIO_TASKQ_TYPES);
16945aeb9474SGarrett D'Amore 
16955aeb9474SGarrett D'Amore 	/*
16965aeb9474SGarrett D'Amore 	 * NB: We are assuming that the zio can only be dispatched
16975aeb9474SGarrett D'Amore 	 * to a single taskq at a time.  It would be a grievous error
16985aeb9474SGarrett D'Amore 	 * to dispatch the zio to another taskq at the same time.
16995aeb9474SGarrett D'Amore 	 */
17005aeb9474SGarrett D'Amore 	ASSERT(zio->io_tqent.tqent_next == NULL);
1701ec94d322SAdam Leventhal 	spa_taskq_dispatch_ent(spa, t, q, (task_func_t *)zio_execute, zio,
1702ec94d322SAdam Leventhal 	    flags, &zio->io_tqent);
1703e14bb325SJeff Bonwick }
17040a4e9518Sgw 
1705e14bb325SJeff Bonwick static boolean_t
1706ec94d322SAdam Leventhal zio_taskq_member(zio_t *zio, zio_taskq_type_t q)
1707e14bb325SJeff Bonwick {
1708e14bb325SJeff Bonwick 	kthread_t *executor = zio->io_executor;
1709e14bb325SJeff Bonwick 	spa_t *spa = zio->io_spa;
17100a4e9518Sgw 
1711ec94d322SAdam Leventhal 	for (zio_type_t t = 0; t < ZIO_TYPES; t++) {
1712ec94d322SAdam Leventhal 		spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
1713ec94d322SAdam Leventhal 		uint_t i;
1714ec94d322SAdam Leventhal 		for (i = 0; i < tqs->stqs_count; i++) {
1715ec94d322SAdam Leventhal 			if (taskq_member(tqs->stqs_taskq[i], executor))
1716ec94d322SAdam Leventhal 				return (B_TRUE);
1717ec94d322SAdam Leventhal 		}
1718ec94d322SAdam Leventhal 	}
17190a4e9518Sgw 
1720e14bb325SJeff Bonwick 	return (B_FALSE);
1721e14bb325SJeff Bonwick }
1722e05725b1Sbonwick 
1723e14bb325SJeff Bonwick static int
1724e14bb325SJeff Bonwick zio_issue_async(zio_t *zio)
1725e14bb325SJeff Bonwick {
172635a5a358SJonathan Adams 	zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE);
1727e14bb325SJeff Bonwick 
1728e14bb325SJeff Bonwick 	return (ZIO_PIPELINE_STOP);
17290a4e9518Sgw }
17300a4e9518Sgw 
1731e14bb325SJeff Bonwick void
1732e14bb325SJeff Bonwick zio_interrupt(zio_t *zio)
17330a4e9518Sgw {
173435a5a358SJonathan Adams 	zio_taskq_dispatch(zio, ZIO_TASKQ_INTERRUPT, B_FALSE);
1735e14bb325SJeff Bonwick }
17360a4e9518Sgw 
173797e81309SPrakash Surya void
173897e81309SPrakash Surya zio_delay_interrupt(zio_t *zio)
173997e81309SPrakash Surya {
174097e81309SPrakash Surya 	/*
174197e81309SPrakash Surya 	 * The timeout_generic() function isn't defined in userspace, so
174297e81309SPrakash Surya 	 * rather than trying to implement the function, the zio delay
174397e81309SPrakash Surya 	 * functionality has been disabled for userspace builds.
174497e81309SPrakash Surya 	 */
174597e81309SPrakash Surya 
174697e81309SPrakash Surya #ifdef _KERNEL
174797e81309SPrakash Surya 	/*
174897e81309SPrakash Surya 	 * If io_target_timestamp is zero, then no delay has been registered
174997e81309SPrakash Surya 	 * for this IO, thus jump to the end of this function and "skip" the
175097e81309SPrakash Surya 	 * delay; issuing it directly to the zio layer.
175197e81309SPrakash Surya 	 */
175297e81309SPrakash Surya 	if (zio->io_target_timestamp != 0) {
175397e81309SPrakash Surya 		hrtime_t now = gethrtime();
175497e81309SPrakash Surya 
175597e81309SPrakash Surya 		if (now >= zio->io_target_timestamp) {
175697e81309SPrakash Surya 			/*
175797e81309SPrakash Surya 			 * This IO has already taken longer than the target
175897e81309SPrakash Surya 			 * delay to complete, so we don't want to delay it
175997e81309SPrakash Surya 			 * any longer; we "miss" the delay and issue it
176097e81309SPrakash Surya 			 * directly to the zio layer. This is likely due to
176197e81309SPrakash Surya 			 * the target latency being set to a value less than
176297e81309SPrakash Surya 			 * the underlying hardware can satisfy (e.g. delay
176397e81309SPrakash Surya 			 * set to 1ms, but the disks take 10ms to complete an
176497e81309SPrakash Surya 			 * IO request).
176597e81309SPrakash Surya 			 */
176697e81309SPrakash Surya 
176797e81309SPrakash Surya 			DTRACE_PROBE2(zio__delay__miss, zio_t *, zio,
176897e81309SPrakash Surya 			    hrtime_t, now);
176997e81309SPrakash Surya 
177097e81309SPrakash Surya 			zio_interrupt(zio);
177197e81309SPrakash Surya 		} else {
177297e81309SPrakash Surya 			hrtime_t diff = zio->io_target_timestamp - now;
177397e81309SPrakash Surya 
177497e81309SPrakash Surya 			DTRACE_PROBE3(zio__delay__hit, zio_t *, zio,
177597e81309SPrakash Surya 			    hrtime_t, now, hrtime_t, diff);
177697e81309SPrakash Surya 
177797e81309SPrakash Surya 			(void) timeout_generic(CALLOUT_NORMAL,
177897e81309SPrakash Surya 			    (void (*)(void *))zio_interrupt, zio, diff, 1, 0);
177997e81309SPrakash Surya 		}
178097e81309SPrakash Surya 
178197e81309SPrakash Surya 		return;
178297e81309SPrakash Surya 	}
178397e81309SPrakash Surya #endif
178497e81309SPrakash Surya 
178597e81309SPrakash Surya 	DTRACE_PROBE1(zio__delay__skip, zio_t *, zio);
178697e81309SPrakash Surya 	zio_interrupt(zio);
178797e81309SPrakash Surya }
178897e81309SPrakash Surya 
1789e14bb325SJeff Bonwick /*
1790e14bb325SJeff Bonwick  * Execute the I/O pipeline until one of the following occurs:
1791f7170741SWill Andrews  *
1792f7170741SWill Andrews  *	(1) the I/O completes
1793f7170741SWill Andrews  *	(2) the pipeline stalls waiting for dependent child I/Os
1794f7170741SWill Andrews  *	(3) the I/O issues, so we're waiting for an I/O completion interrupt
1795f7170741SWill Andrews  *	(4) the I/O is delegated by vdev-level caching or aggregation
1796f7170741SWill Andrews  *	(5) the I/O is deferred due to vdev-level queueing
1797f7170741SWill Andrews  *	(6) the I/O is handed off to another thread.
1798f7170741SWill Andrews  *
1799f7170741SWill Andrews  * In all cases, the pipeline stops whenever there's no CPU work; it never
1800f7170741SWill Andrews  * burns a thread in cv_wait().
1801e14bb325SJeff Bonwick  *
1802e14bb325SJeff Bonwick  * There's no locking on io_stage because there's no legitimate way
1803e14bb325SJeff Bonwick  * for multiple threads to be attempting to process the same I/O.
1804e14bb325SJeff Bonwick  */
1805b24ab676SJeff Bonwick static zio_pipe_stage_t *zio_pipeline[];
18060a4e9518Sgw 
1807e14bb325SJeff Bonwick void
1808e14bb325SJeff Bonwick zio_execute(zio_t *zio)
1809e14bb325SJeff Bonwick {
1810e14bb325SJeff Bonwick 	zio->io_executor = curthread;
18110a4e9518Sgw 
18120f7643c7SGeorge Wilson 	ASSERT3U(zio->io_queued_timestamp, >, 0);
18130f7643c7SGeorge Wilson 
1814e14bb325SJeff Bonwick 	while (zio->io_stage < ZIO_STAGE_DONE) {
1815b24ab676SJeff Bonwick 		enum zio_stage pipeline = zio->io_pipeline;
1816b24ab676SJeff Bonwick 		enum zio_stage stage = zio->io_stage;
1817e14bb325SJeff Bonwick 		int rv;
18180a4e9518Sgw 
1819e14bb325SJeff Bonwick 		ASSERT(!MUTEX_HELD(&zio->io_lock));
1820b24ab676SJeff Bonwick 		ASSERT(ISP2(stage));
1821b24ab676SJeff Bonwick 		ASSERT(zio->io_stall == NULL);
18220a4e9518Sgw 
1823b24ab676SJeff Bonwick 		do {
1824b24ab676SJeff Bonwick 			stage <<= 1;
1825b24ab676SJeff Bonwick 		} while ((stage & pipeline) == 0);
1826e14bb325SJeff Bonwick 
1827e14bb325SJeff Bonwick 		ASSERT(stage <= ZIO_STAGE_DONE);
18280a4e9518Sgw 
18290a4e9518Sgw 		/*
1830e14bb325SJeff Bonwick 		 * If we are in interrupt context and this pipeline stage
1831e14bb325SJeff Bonwick 		 * will grab a config lock that is held across I/O,
1832b24ab676SJeff Bonwick 		 * or may wait for an I/O that needs an interrupt thread
1833b24ab676SJeff Bonwick 		 * to complete, issue async to avoid deadlock.
183435a5a358SJonathan Adams 		 *
183535a5a358SJonathan Adams 		 * For VDEV_IO_START, we cut in line so that the io will
183635a5a358SJonathan Adams 		 * be sent to disk promptly.
18370a4e9518Sgw 		 */
1838b24ab676SJeff Bonwick 		if ((stage & ZIO_BLOCKING_STAGES) && zio->io_vd == NULL &&
1839e14bb325SJeff Bonwick 		    zio_taskq_member(zio, ZIO_TASKQ_INTERRUPT)) {
184035a5a358SJonathan Adams 			boolean_t cut = (stage == ZIO_STAGE_VDEV_IO_START) ?
184135a5a358SJonathan Adams 			    zio_requeue_io_start_cut_in_line : B_FALSE;
184235a5a358SJonathan Adams 			zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, cut);
1843e14bb325SJeff Bonwick 			return;
18440a4e9518Sgw 		}
18450a4e9518Sgw 
1846e14bb325SJeff Bonwick 		zio->io_stage = stage;
18470f7643c7SGeorge Wilson 		zio->io_pipeline_trace |= zio->io_stage;
1848bf16b11eSMatthew Ahrens 		rv = zio_pipeline[highbit64(stage) - 1](zio);
18490a4e9518Sgw 
1850e14bb325SJeff Bonwick 		if (rv == ZIO_PIPELINE_STOP)
1851e14bb325SJeff Bonwick 			return;
18520a4e9518Sgw 
1853e14bb325SJeff Bonwick 		ASSERT(rv == ZIO_PIPELINE_CONTINUE);
1854e14bb325SJeff Bonwick 	}
18550a4e9518Sgw }
18560a4e9518Sgw 
1857e14bb325SJeff Bonwick /*
1858e14bb325SJeff Bonwick  * ==========================================================================
1859e14bb325SJeff Bonwick  * Initiate I/O, either sync or async
1860e14bb325SJeff Bonwick  * ==========================================================================
1861e14bb325SJeff Bonwick  */
1862e14bb325SJeff Bonwick int
1863e14bb325SJeff Bonwick zio_wait(zio_t *zio)
18640a4e9518Sgw {
1865e14bb325SJeff Bonwick 	int error;
18660a4e9518Sgw 
18671271e4b1SPrakash Surya 	ASSERT3P(zio->io_stage, ==, ZIO_STAGE_OPEN);
18681271e4b1SPrakash Surya 	ASSERT3P(zio->io_executor, ==, NULL);
18690a4e9518Sgw 
1870e14bb325SJeff Bonwick 	zio->io_waiter = curthread;
18710f7643c7SGeorge Wilson 	ASSERT0(zio->io_queued_timestamp);
18720f7643c7SGeorge Wilson 	zio->io_queued_timestamp = gethrtime();
1873e05725b1Sbonwick 
1874e14bb325SJeff Bonwick 	zio_execute(zio);
18750a4e9518Sgw 
1876e14bb325SJeff Bonwick 	mutex_enter(&zio->io_lock);
1877e14bb325SJeff Bonwick 	while (zio->io_executor != NULL)
1878e14bb325SJeff Bonwick 		cv_wait(&zio->io_cv, &zio->io_lock);
1879e14bb325SJeff Bonwick 	mutex_exit(&zio->io_lock);
188032b87932Sek 
1881e14bb325SJeff Bonwick 	error = zio->io_error;
1882e14bb325SJeff Bonwick 	zio_destroy(zio);
188332b87932Sek 
1884e14bb325SJeff Bonwick 	return (error);
188532b87932Sek }
188632b87932Sek 
1887e14bb325SJeff Bonwick void
1888e14bb325SJeff Bonwick zio_nowait(zio_t *zio)
18890a4e9518Sgw {
18901271e4b1SPrakash Surya 	ASSERT3P(zio->io_executor, ==, NULL);
1891fa9e4066Sahrens 
1892a3f829aeSBill Moore 	if (zio->io_child_type == ZIO_CHILD_LOGICAL &&
1893a3f829aeSBill Moore 	    zio_unique_parent(zio) == NULL) {
1894ea8dc4b6Seschrock 		/*
1895e14bb325SJeff Bonwick 		 * This is a logical async I/O with no parent to wait for it.
189654d692b7SGeorge Wilson 		 * We add it to the spa_async_root_zio "Godfather" I/O which
189754d692b7SGeorge Wilson 		 * will ensure they complete prior to unloading the pool.
1898ea8dc4b6Seschrock 		 */
1899e14bb325SJeff Bonwick 		spa_t *spa = zio->io_spa;
190054d692b7SGeorge Wilson 
19016f834bc1SMatthew Ahrens 		zio_add_child(spa->spa_async_zio_root[CPU_SEQID], zio);
1902e14bb325SJeff Bonwick 	}
1903ea8dc4b6Seschrock 
19040f7643c7SGeorge Wilson 	ASSERT0(zio->io_queued_timestamp);
19050f7643c7SGeorge Wilson 	zio->io_queued_timestamp = gethrtime();
1906e14bb325SJeff Bonwick 	zio_execute(zio);
1907e14bb325SJeff Bonwick }
1908ea8dc4b6Seschrock 
1909e14bb325SJeff Bonwick /*
1910e14bb325SJeff Bonwick  * ==========================================================================
19111271e4b1SPrakash Surya  * Reexecute, cancel, or suspend/resume failed I/O
1912e14bb325SJeff Bonwick  * ==========================================================================
1913e14bb325SJeff Bonwick  */
1914fa9e4066Sahrens 
1915e14bb325SJeff Bonwick static void
1916e14bb325SJeff Bonwick zio_reexecute(zio_t *pio)
1917e14bb325SJeff Bonwick {
1918a3f829aeSBill Moore 	zio_t *cio, *cio_next;
1919a3f829aeSBill Moore 
1920a3f829aeSBill Moore 	ASSERT(pio->io_child_type == ZIO_CHILD_LOGICAL);
1921a3f829aeSBill Moore 	ASSERT(pio->io_orig_stage == ZIO_STAGE_OPEN);
1922f5383399SBill Moore 	ASSERT(pio->io_gang_leader == NULL);
1923f5383399SBill Moore 	ASSERT(pio->io_gang_tree == NULL);
1924e05725b1Sbonwick 
1925e14bb325SJeff Bonwick 	pio->io_flags = pio->io_orig_flags;
1926e14bb325SJeff Bonwick 	pio->io_stage = pio->io_orig_stage;
1927e14bb325SJeff Bonwick 	pio->io_pipeline = pio->io_orig_pipeline;
1928e14bb325SJeff Bonwick 	pio->io_reexecute = 0;
192980901aeaSGeorge Wilson 	pio->io_flags |= ZIO_FLAG_REEXECUTED;
19300f7643c7SGeorge Wilson 	pio->io_pipeline_trace = 0;
1931e14bb325SJeff Bonwick 	pio->io_error = 0;
1932a3f829aeSBill Moore 	for (int w = 0; w < ZIO_WAIT_TYPES; w++)
1933a3f829aeSBill Moore 		pio->io_state[w] = 0;
1934e14bb325SJeff Bonwick 	for (int c = 0; c < ZIO_CHILD_TYPES; c++)
1935e14bb325SJeff Bonwick 		pio->io_child_error[c] = 0;
19360a4e9518Sgw 
1937b24ab676SJeff Bonwick 	if (IO_IS_ALLOCATING(pio))
1938b24ab676SJeff Bonwick 		BP_ZERO(pio->io_bp);
1939d58459f4Sek 
1940e14bb325SJeff Bonwick 	/*
1941e14bb325SJeff Bonwick 	 * As we reexecute pio's children, new children could be created.
1942a3f829aeSBill Moore 	 * New children go to the head of pio's io_child_list, however,
1943e14bb325SJeff Bonwick 	 * so we will (correctly) not reexecute them.  The key is that
1944a3f829aeSBill Moore 	 * the remainder of pio's io_child_list, from 'cio_next' onward,
1945a3f829aeSBill Moore 	 * cannot be affected by any side effects of reexecuting 'cio'.
1946e14bb325SJeff Bonwick 	 */
19470f7643c7SGeorge Wilson 	zio_link_t *zl = NULL;
1948a3874b8bSToomas Soome 	mutex_enter(&pio->io_lock);
19490f7643c7SGeorge Wilson 	for (cio = zio_walk_children(pio, &zl); cio != NULL; cio = cio_next) {
19500f7643c7SGeorge Wilson 		cio_next = zio_walk_children(pio, &zl);
1951a3f829aeSBill Moore 		for (int w = 0; w < ZIO_WAIT_TYPES; w++)
1952a3f829aeSBill Moore 			pio->io_children[cio->io_child_type][w]++;
1953e14bb325SJeff Bonwick 		mutex_exit(&pio->io_lock);
1954a3f829aeSBill Moore 		zio_reexecute(cio);
1955a3874b8bSToomas Soome 		mutex_enter(&pio->io_lock);
1956fa9e4066Sahrens 	}
1957a3874b8bSToomas Soome 	mutex_exit(&pio->io_lock);
1958e05725b1Sbonwick 
1959e14bb325SJeff Bonwick 	/*
1960e14bb325SJeff Bonwick 	 * Now that all children have been reexecuted, execute the parent.
196154d692b7SGeorge Wilson 	 * We don't reexecute "The Godfather" I/O here as it's the
196248bbca81SDaniel Hoffman 	 * responsibility of the caller to wait on it.
1963e14bb325SJeff Bonwick 	 */
19640f7643c7SGeorge Wilson 	if (!(pio->io_flags & ZIO_FLAG_GODFATHER)) {
19650f7643c7SGeorge Wilson 		pio->io_queued_timestamp = gethrtime();
196654d692b7SGeorge Wilson 		zio_execute(pio);
19670f7643c7SGeorge Wilson 	}
19680a4e9518Sgw }
19690a4e9518Sgw 
1970e14bb325SJeff Bonwick void
1971e0f1c0afSOlaf Faaland zio_suspend(spa_t *spa, zio_t *zio, zio_suspend_reason_t reason)
19720a4e9518Sgw {
1973e14bb325SJeff Bonwick 	if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_PANIC)
1974e14bb325SJeff Bonwick 		fm_panic("Pool '%s' has encountered an uncorrectable I/O "
1975e14bb325SJeff Bonwick 		    "failure and the failure mode property for this pool "
1976e14bb325SJeff Bonwick 		    "is set to panic.", spa_name(spa));
19770a4e9518Sgw 
1978eb633035STom Caputi 	zfs_ereport_post(FM_EREPORT_ZFS_IO_FAILURE, spa, NULL,
1979eb633035STom Caputi 	    NULL, NULL, 0, 0);
19800a4e9518Sgw 
1981e14bb325SJeff Bonwick 	mutex_enter(&spa->spa_suspend_lock);
1982fa9e4066Sahrens 
1983e14bb325SJeff Bonwick 	if (spa->spa_suspend_zio_root == NULL)
198454d692b7SGeorge Wilson 		spa->spa_suspend_zio_root = zio_root(spa, NULL, NULL,
198554d692b7SGeorge Wilson 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
198654d692b7SGeorge Wilson 		    ZIO_FLAG_GODFATHER);
1987fa9e4066Sahrens 
1988e0f1c0afSOlaf Faaland 	spa->spa_suspended = reason;
1989fa9e4066Sahrens 
1990e14bb325SJeff Bonwick 	if (zio != NULL) {
199154d692b7SGeorge Wilson 		ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER));
1992e14bb325SJeff Bonwick 		ASSERT(zio != spa->spa_suspend_zio_root);
1993e14bb325SJeff Bonwick 		ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1994a3f829aeSBill Moore 		ASSERT(zio_unique_parent(zio) == NULL);
1995e14bb325SJeff Bonwick 		ASSERT(zio->io_stage == ZIO_STAGE_DONE);
1996e14bb325SJeff Bonwick 		zio_add_child(spa->spa_suspend_zio_root, zio);
1997e14bb325SJeff Bonwick 	}
1998fa9e4066Sahrens 
1999e14bb325SJeff Bonwick 	mutex_exit(&spa->spa_suspend_lock);
2000e14bb325SJeff Bonwick }
2001fa9e4066Sahrens 
200254d692b7SGeorge Wilson int
2003e14bb325SJeff Bonwick zio_resume(spa_t *spa)
2004e14bb325SJeff Bonwick {
200554d692b7SGeorge Wilson 	zio_t *pio;
2006fa9e4066Sahrens 
2007b3995adbSahrens 	/*
2008e14bb325SJeff Bonwick 	 * Reexecute all previously suspended i/o.
2009b3995adbSahrens 	 */
2010e14bb325SJeff Bonwick 	mutex_enter(&spa->spa_suspend_lock);
2011e0f1c0afSOlaf Faaland 	spa->spa_suspended = ZIO_SUSPEND_NONE;
2012e14bb325SJeff Bonwick 	cv_broadcast(&spa->spa_suspend_cv);
2013e14bb325SJeff Bonwick 	pio = spa->spa_suspend_zio_root;
2014e14bb325SJeff Bonwick 	spa->spa_suspend_zio_root = NULL;
2015e14bb325SJeff Bonwick 	mutex_exit(&spa->spa_suspend_lock);
2016e14bb325SJeff Bonwick 
2017e14bb325SJeff Bonwick 	if (pio == NULL)
201854d692b7SGeorge Wilson 		return (0);
2019e14bb325SJeff Bonwick 
202054d692b7SGeorge Wilson 	zio_reexecute(pio);
202154d692b7SGeorge Wilson 	return (zio_wait(pio));
2022e14bb325SJeff Bonwick }
2023e14bb325SJeff Bonwick 
2024e14bb325SJeff Bonwick void
2025e14bb325SJeff Bonwick zio_resume_wait(spa_t *spa)
2026e14bb325SJeff Bonwick {
2027e14bb325SJeff Bonwick 	mutex_enter(&spa->spa_suspend_lock);
2028e14bb325SJeff Bonwick 	while (spa_suspended(spa))
2029e14bb325SJeff Bonwick 		cv_wait(&spa->spa_suspend_cv, &spa->spa_suspend_lock);
2030e14bb325SJeff Bonwick 	mutex_exit(&spa->spa_suspend_lock);
2031fa9e4066Sahrens }
2032fa9e4066Sahrens 
2033fa9e4066Sahrens /*
2034fa9e4066Sahrens  * ==========================================================================
2035e14bb325SJeff Bonwick  * Gang blocks.
2036e14bb325SJeff Bonwick  *
2037e14bb325SJeff Bonwick  * A gang block is a collection of small blocks that looks to the DMU
2038e14bb325SJeff Bonwick  * like one large block.  When zio_dva_allocate() cannot find a block
2039e14bb325SJeff Bonwick  * of the requested size, due to either severe fragmentation or the pool
2040e14bb325SJeff Bonwick  * being nearly full, it calls zio_write_gang_block() to construct the
2041e14bb325SJeff Bonwick  * block from smaller fragments.
2042e14bb325SJeff Bonwick  *
2043e14bb325SJeff Bonwick  * A gang block consists of a gang header (zio_gbh_phys_t) and up to
2044e14bb325SJeff Bonwick  * three (SPA_GBH_NBLKPTRS) gang members.  The gang header is just like
2045e14bb325SJeff Bonwick  * an indirect block: it's an array of block pointers.  It consumes
2046e14bb325SJeff Bonwick  * only one sector and hence is allocatable regardless of fragmentation.
2047e14bb325SJeff Bonwick  * The gang header's bps point to its gang members, which hold the data.
2048e14bb325SJeff Bonwick  *
2049e14bb325SJeff Bonwick  * Gang blocks are self-checksumming, using the bp's <vdev, offset, txg>
2050e14bb325SJeff Bonwick  * as the verifier to ensure uniqueness of the SHA256 checksum.
2051e14bb325SJeff Bonwick  * Critically, the gang block bp's blk_cksum is the checksum of the data,
2052e14bb325SJeff Bonwick  * not the gang header.  This ensures that data block signatures (needed for
2053e14bb325SJeff Bonwick  * deduplication) are independent of how the block is physically stored.
2054e14bb325SJeff Bonwick  *
2055e14bb325SJeff Bonwick  * Gang blocks can be nested: a gang member may itself be a gang block.
2056e14bb325SJeff Bonwick  * Thus every gang block is a tree in which root and all interior nodes are
2057e14bb325SJeff Bonwick  * gang headers, and the leaves are normal blocks that contain user data.
2058e14bb325SJeff Bonwick  * The root of the gang tree is called the gang leader.
2059e14bb325SJeff Bonwick  *
2060e14bb325SJeff Bonwick  * To perform any operation (read, rewrite, free, claim) on a gang block,
2061e14bb325SJeff Bonwick  * zio_gang_assemble() first assembles the gang tree (minus data leaves)
2062e14bb325SJeff Bonwick  * in the io_gang_tree field of the original logical i/o by recursively
2063e14bb325SJeff Bonwick  * reading the gang leader and all gang headers below it.  This yields
2064e14bb325SJeff Bonwick  * an in-core tree containing the contents of every gang header and the
2065e14bb325SJeff Bonwick  * bps for every constituent of the gang block.
2066e14bb325SJeff Bonwick  *
2067e14bb325SJeff Bonwick  * With the gang tree now assembled, zio_gang_issue() just walks the gang tree
2068e14bb325SJeff Bonwick  * and invokes a callback on each bp.  To free a gang block, zio_gang_issue()
2069e14bb325SJeff Bonwick  * calls zio_free_gang() -- a trivial wrapper around zio_free() -- for each bp.
2070e14bb325SJeff Bonwick  * zio_claim_gang() provides a similarly trivial wrapper for zio_claim().
2071e14bb325SJeff Bonwick  * zio_read_gang() is a wrapper around zio_read() that omits reading gang
2072e14bb325SJeff Bonwick  * headers, since we already have those in io_gang_tree.  zio_rewrite_gang()
2073e14bb325SJeff Bonwick  * performs a zio_rewrite() of the data or, for gang headers, a zio_rewrite()
2074e14bb325SJeff Bonwick  * of the gang header plus zio_checksum_compute() of the data to update the
2075e14bb325SJeff Bonwick  * gang header's blk_cksum as described above.
2076e14bb325SJeff Bonwick  *
2077e14bb325SJeff Bonwick  * The two-phase assemble/issue model solves the problem of partial failure --
2078e14bb325SJeff Bonwick  * what if you'd freed part of a gang block but then couldn't read the
2079e14bb325SJeff Bonwick  * gang header for another part?  Assembling the entire gang tree first
2080e14bb325SJeff Bonwick  * ensures that all the necessary gang header I/O has succeeded before
2081e14bb325SJeff Bonwick  * starting the actual work of free, claim, or write.  Once the gang tree
2082e14bb325SJeff Bonwick  * is assembled, free and claim are in-memory operations that cannot fail.
2083e14bb325SJeff Bonwick  *
2084e14bb325SJeff Bonwick  * In the event that a gang write fails, zio_dva_unallocate() walks the
2085e14bb325SJeff Bonwick  * gang tree to immediately free (i.e. insert back into the space map)
2086e14bb325SJeff Bonwick  * everything we've allocated.  This ensures that we don't get ENOSPC
2087e14bb325SJeff Bonwick  * errors during repeated suspend/resume cycles due to a flaky device.
2088e14bb325SJeff Bonwick  *
2089e14bb325SJeff Bonwick  * Gang rewrites only happen during sync-to-convergence.  If we can't assemble
2090e14bb325SJeff Bonwick  * the gang tree, we won't modify the block, so we can safely defer the free
2091e14bb325SJeff Bonwick  * (knowing that the block is still intact).  If we *can* assemble the gang
2092e14bb325SJeff Bonwick  * tree, then even if some of the rewrites fail, zio_dva_unallocate() will free
2093e14bb325SJeff Bonwick  * each constituent bp and we can allocate a new block on the next sync pass.
2094e14bb325SJeff Bonwick  *
2095e14bb325SJeff Bonwick  * In all cases, the gang tree allows complete recovery from partial failure.
2096fa9e4066Sahrens  * ==========================================================================
2097fa9e4066Sahrens  */
2098e14bb325SJeff Bonwick 
2099770499e1SDan Kimmel static void
2100770499e1SDan Kimmel zio_gang_issue_func_done(zio_t *zio)
2101770499e1SDan Kimmel {
2102770499e1SDan Kimmel 	abd_put(zio->io_abd);
2103770499e1SDan Kimmel }
2104770499e1SDan Kimmel 
2105e14bb325SJeff Bonwick static zio_t *
2106770499e1SDan Kimmel zio_read_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data,
2107770499e1SDan Kimmel     uint64_t offset)
2108fa9e4066Sahrens {
2109e14bb325SJeff Bonwick 	if (gn != NULL)
2110e14bb325SJeff Bonwick 		return (pio);
2111fa9e4066Sahrens 
2112770499e1SDan Kimmel 	return (zio_read(pio, pio->io_spa, bp, abd_get_offset(data, offset),
2113770499e1SDan Kimmel 	    BP_GET_PSIZE(bp), zio_gang_issue_func_done,
2114770499e1SDan Kimmel 	    NULL, pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio),
2115e14bb325SJeff Bonwick 	    &pio->io_bookmark));
2116e14bb325SJeff Bonwick }
2117e14bb325SJeff Bonwick 
2118770499e1SDan Kimmel static zio_t *
2119770499e1SDan Kimmel zio_rewrite_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data,
2120770499e1SDan Kimmel     uint64_t offset)
2121e14bb325SJeff Bonwick {
2122e14bb325SJeff Bonwick 	zio_t *zio;
2123e14bb325SJeff Bonwick 
2124e14bb325SJeff Bonwick 	if (gn != NULL) {
2125770499e1SDan Kimmel 		abd_t *gbh_abd =
2126770499e1SDan Kimmel 		    abd_get_from_buf(gn->gn_gbh, SPA_GANGBLOCKSIZE);
2127e14bb325SJeff Bonwick 		zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp,
2128770499e1SDan Kimmel 		    gbh_abd, SPA_GANGBLOCKSIZE, zio_gang_issue_func_done, NULL,
2129770499e1SDan Kimmel 		    pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio),
2130770499e1SDan Kimmel 		    &pio->io_bookmark);
2131fa9e4066Sahrens 		/*
2132e14bb325SJeff Bonwick 		 * As we rewrite each gang header, the pipeline will compute
2133e14bb325SJeff Bonwick 		 * a new gang block header checksum for it; but no one will
2134e14bb325SJeff Bonwick 		 * compute a new data checksum, so we do that here.  The one
2135e14bb325SJeff Bonwick 		 * exception is the gang leader: the pipeline already computed
2136e14bb325SJeff Bonwick 		 * its data checksum because that stage precedes gang assembly.
2137e14bb325SJeff Bonwick 		 * (Presently, nothing actually uses interior data checksums;
2138e14bb325SJeff Bonwick 		 * this is just good hygiene.)
2139fa9e4066Sahrens 		 */
2140f5383399SBill Moore 		if (gn != pio->io_gang_leader->io_gang_tree) {
2141770499e1SDan Kimmel 			abd_t *buf = abd_get_offset(data, offset);
2142770499e1SDan Kimmel 
2143e14bb325SJeff Bonwick 			zio_checksum_compute(zio, BP_GET_CHECKSUM(bp),
2144770499e1SDan Kimmel 			    buf, BP_GET_PSIZE(bp));
2145770499e1SDan Kimmel 
2146770499e1SDan Kimmel 			abd_put(buf);
2147e14bb325SJeff Bonwick 		}
2148b24ab676SJeff Bonwick 		/*
2149b24ab676SJeff Bonwick 		 * If we are here to damage data for testing purposes,
2150b24ab676SJeff Bonwick 		 * leave the GBH alone so that we can detect the damage.
2151b24ab676SJeff Bonwick 		 */
2152b24ab676SJeff Bonwick 		if (pio->io_gang_leader->io_flags & ZIO_FLAG_INDUCE_DAMAGE)
2153b24ab676SJeff Bonwick 			zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES;
2154fa9e4066Sahrens 	} else {
2155e14bb325SJeff Bonwick 		zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp,
2156770499e1SDan Kimmel 		    abd_get_offset(data, offset), BP_GET_PSIZE(bp),
2157770499e1SDan Kimmel 		    zio_gang_issue_func_done, NULL, pio->io_priority,
2158e14bb325SJeff Bonwick 		    ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
2159fa9e4066Sahrens 	}
2160fa9e4066Sahrens 
2161e14bb325SJeff Bonwick 	return (zio);
2162e14bb325SJeff Bonwick }
2163fa9e4066Sahrens 
2164e14bb325SJeff Bonwick /* ARGSUSED */
2165770499e1SDan Kimmel static zio_t *
2166770499e1SDan Kimmel zio_free_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data,
2167770499e1SDan Kimmel     uint64_t offset)
2168e14bb325SJeff Bonwick {
2169b24ab676SJeff Bonwick 	return (zio_free_sync(pio, pio->io_spa, pio->io_txg, bp,
2170b24ab676SJeff Bonwick 	    ZIO_GANG_CHILD_FLAGS(pio)));
2171fa9e4066Sahrens }
2172fa9e4066Sahrens 
2173e14bb325SJeff Bonwick /* ARGSUSED */
2174770499e1SDan Kimmel static zio_t *
2175770499e1SDan Kimmel zio_claim_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data,
2176770499e1SDan Kimmel     uint64_t offset)
2177fa9e4066Sahrens {
2178e14bb325SJeff Bonwick 	return (zio_claim(pio, pio->io_spa, pio->io_txg, bp,
2179e14bb325SJeff Bonwick 	    NULL, NULL, ZIO_GANG_CHILD_FLAGS(pio)));
2180e14bb325SJeff Bonwick }
2181fa9e4066Sahrens 
2182e14bb325SJeff Bonwick static zio_gang_issue_func_t *zio_gang_issue_func[ZIO_TYPES] = {
2183e14bb325SJeff Bonwick 	NULL,
2184e14bb325SJeff Bonwick 	zio_read_gang,
2185e14bb325SJeff Bonwick 	zio_rewrite_gang,
2186e14bb325SJeff Bonwick 	zio_free_gang,
2187e14bb325SJeff Bonwick 	zio_claim_gang,
2188e14bb325SJeff Bonwick 	NULL
2189e14bb325SJeff Bonwick };
2190fa9e4066Sahrens 
2191e14bb325SJeff Bonwick static void zio_gang_tree_assemble_done(zio_t *zio);
2192fa9e4066Sahrens 
2193e14bb325SJeff Bonwick static zio_gang_node_t *
2194e14bb325SJeff Bonwick zio_gang_node_alloc(zio_gang_node_t **gnpp)
2195e14bb325SJeff Bonwick {
2196e14bb325SJeff Bonwick 	zio_gang_node_t *gn;
2197fa9e4066Sahrens 
2198e14bb325SJeff Bonwick 	ASSERT(*gnpp == NULL);
2199fa9e4066Sahrens 
2200e14bb325SJeff Bonwick 	gn = kmem_zalloc(sizeof (*gn), KM_SLEEP);
2201e14bb325SJeff Bonwick 	gn->gn_gbh = zio_buf_alloc(SPA_GANGBLOCKSIZE);
2202e14bb325SJeff Bonwick 	*gnpp = gn;
2203e14bb325SJeff Bonwick 
2204e14bb325SJeff Bonwick 	return (gn);
2205fa9e4066Sahrens }
2206fa9e4066Sahrens 
2207fa9e4066Sahrens static void
2208e14bb325SJeff Bonwick zio_gang_node_free(zio_gang_node_t **gnpp)
2209fa9e4066Sahrens {
2210e14bb325SJeff Bonwick 	zio_gang_node_t *gn = *gnpp;
2211fa9e4066Sahrens 
2212e14bb325SJeff Bonwick 	for (int g = 0; g < SPA_GBH_NBLKPTRS; g++)
2213e14bb325SJeff Bonwick 		ASSERT(gn->gn_child[g] == NULL);
2214e14bb325SJeff Bonwick 
2215e14bb325SJeff Bonwick 	zio_buf_free(gn->gn_gbh, SPA_GANGBLOCKSIZE);
2216e14bb325SJeff Bonwick 	kmem_free(gn, sizeof (*gn));
2217e14bb325SJeff Bonwick 	*gnpp = NULL;
2218fa9e4066Sahrens }
2219fa9e4066Sahrens 
2220e14bb325SJeff Bonwick static void
2221e14bb325SJeff Bonwick zio_gang_tree_free(zio_gang_node_t **gnpp)
2222fa9e4066Sahrens {
2223e14bb325SJeff Bonwick 	zio_gang_node_t *gn = *gnpp;
2224fa9e4066Sahrens 
2225e14bb325SJeff Bonwick 	if (gn == NULL)
2226e14bb325SJeff Bonwick 		return;
2227fa9e4066Sahrens 
2228e14bb325SJeff Bonwick 	for (int g = 0; g < SPA_GBH_NBLKPTRS; g++)
2229e14bb325SJeff Bonwick 		zio_gang_tree_free(&gn->gn_child[g]);
2230fa9e4066Sahrens 
2231e14bb325SJeff Bonwick 	zio_gang_node_free(gnpp);
2232fa9e4066Sahrens }
2233fa9e4066Sahrens 
2234e14bb325SJeff Bonwick static void
2235f5383399SBill Moore zio_gang_tree_assemble(zio_t *gio, blkptr_t *bp, zio_gang_node_t **gnpp)
2236fa9e4066Sahrens {
2237e14bb325SJeff Bonwick 	zio_gang_node_t *gn = zio_gang_node_alloc(gnpp);
2238770499e1SDan Kimmel 	abd_t *gbh_abd = abd_get_from_buf(gn->gn_gbh, SPA_GANGBLOCKSIZE);
2239e14bb325SJeff Bonwick 
2240f5383399SBill Moore 	ASSERT(gio->io_gang_leader == gio);
2241e14bb325SJeff Bonwick 	ASSERT(BP_IS_GANG(bp));
2242fa9e4066Sahrens 
2243770499e1SDan Kimmel 	zio_nowait(zio_read(gio, gio->io_spa, bp, gbh_abd, SPA_GANGBLOCKSIZE,
2244770499e1SDan Kimmel 	    zio_gang_tree_assemble_done, gn, gio->io_priority,
2245770499e1SDan Kimmel 	    ZIO_GANG_CHILD_FLAGS(gio), &gio->io_bookmark));
2246e14bb325SJeff Bonwick }
2247fa9e4066Sahrens 
2248e14bb325SJeff Bonwick static void
2249e14bb325SJeff Bonwick zio_gang_tree_assemble_done(zio_t *zio)
2250e14bb325SJeff Bonwick {
2251f5383399SBill Moore 	zio_t *gio = zio->io_gang_leader;
2252e14bb325SJeff Bonwick 	zio_gang_node_t *gn = zio->io_private;
2253e14bb325SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
2254fa9e4066Sahrens 
2255f5383399SBill Moore 	ASSERT(gio == zio_unique_parent(zio));
2256b24ab676SJeff Bonwick 	ASSERT(zio->io_child_count == 0);
2257fa9e4066Sahrens 
2258e14bb325SJeff Bonwick 	if (zio->io_error)
2259e14bb325SJeff Bonwick 		return;
2260fa9e4066Sahrens 
2261770499e1SDan Kimmel 	/* this ABD was created from a linear buf in zio_gang_tree_assemble */
2262e14bb325SJeff Bonwick 	if (BP_SHOULD_BYTESWAP(bp))
2263770499e1SDan Kimmel 		byteswap_uint64_array(abd_to_buf(zio->io_abd), zio->io_size);
2264fa9e4066Sahrens 
2265770499e1SDan Kimmel 	ASSERT3P(abd_to_buf(zio->io_abd), ==, gn->gn_gbh);
2266e14bb325SJeff Bonwick 	ASSERT(zio->io_size == SPA_GANGBLOCKSIZE);
22676e1f5caaSNeil Perrin 	ASSERT(gn->gn_gbh->zg_tail.zec_magic == ZEC_MAGIC);
2268e05725b1Sbonwick 
2269770499e1SDan Kimmel 	abd_put(zio->io_abd);
2270770499e1SDan Kimmel 
2271e14bb325SJeff Bonwick 	for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
2272e14bb325SJeff Bonwick 		blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g];
2273e14bb325SJeff Bonwick 		if (!BP_IS_GANG(gbp))
2274e14bb325SJeff Bonwick 			continue;
2275f5383399SBill Moore 		zio_gang_tree_assemble(gio, gbp, &gn->gn_child[g]);
2276e14bb325SJeff Bonwick 	}
2277fa9e4066Sahrens }
2278fa9e4066Sahrens 
2279e14bb325SJeff Bonwick static void
2280770499e1SDan Kimmel zio_gang_tree_issue(zio_t *pio, zio_gang_node_t *gn, blkptr_t *bp, abd_t *data,
2281770499e1SDan Kimmel     uint64_t offset)
2282fa9e4066Sahrens {
2283f5383399SBill Moore 	zio_t *gio = pio->io_gang_leader;
2284e14bb325SJeff Bonwick 	zio_t *zio;
2285fa9e4066Sahrens 
2286e14bb325SJeff Bonwick 	ASSERT(BP_IS_GANG(bp) == !!gn);
2287f5383399SBill Moore 	ASSERT(BP_GET_CHECKSUM(bp) == BP_GET_CHECKSUM(gio->io_bp));
2288f5383399SBill Moore 	ASSERT(BP_GET_LSIZE(bp) == BP_GET_PSIZE(bp) || gn == gio->io_gang_tree);
2289fa9e4066Sahrens 
2290e14bb325SJeff Bonwick 	/*
2291e14bb325SJeff Bonwick 	 * If you're a gang header, your data is in gn->gn_gbh.
2292e14bb325SJeff Bonwick 	 * If you're a gang member, your data is in 'data' and gn == NULL.
2293e14bb325SJeff Bonwick 	 */
2294770499e1SDan Kimmel 	zio = zio_gang_issue_func[gio->io_type](pio, bp, gn, data, offset);
2295fa9e4066Sahrens 
2296e14bb325SJeff Bonwick 	if (gn != NULL) {
22976e1f5caaSNeil Perrin 		ASSERT(gn->gn_gbh->zg_tail.zec_magic == ZEC_MAGIC);
2298fa9e4066Sahrens 
2299e14bb325SJeff Bonwick 		for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
2300e14bb325SJeff Bonwick 			blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g];
2301e14bb325SJeff Bonwick 			if (BP_IS_HOLE(gbp))
2302e14bb325SJeff Bonwick 				continue;
2303770499e1SDan Kimmel 			zio_gang_tree_issue(zio, gn->gn_child[g], gbp, data,
2304770499e1SDan Kimmel 			    offset);
2305770499e1SDan Kimmel 			offset += BP_GET_PSIZE(gbp);
2306e14bb325SJeff Bonwick 		}
2307fa9e4066Sahrens 	}
2308fa9e4066Sahrens 
2309f5383399SBill Moore 	if (gn == gio->io_gang_tree)
2310770499e1SDan Kimmel 		ASSERT3U(gio->io_size, ==, offset);
2311e05725b1Sbonwick 
2312e14bb325SJeff Bonwick 	if (zio != pio)
2313e14bb325SJeff Bonwick 		zio_nowait(zio);
2314fa9e4066Sahrens }
2315fa9e4066Sahrens 
2316e05725b1Sbonwick static int
2317e14bb325SJeff Bonwick zio_gang_assemble(zio_t *zio)
2318fa9e4066Sahrens {
2319e14bb325SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
2320fa9e4066Sahrens 
2321f5383399SBill Moore 	ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == NULL);
2322f5383399SBill Moore 	ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
2323f5383399SBill Moore 
2324f5383399SBill Moore 	zio->io_gang_leader = zio;
2325fa9e4066Sahrens 
2326e14bb325SJeff Bonwick 	zio_gang_tree_assemble(zio, bp, &zio->io_gang_tree);
2327e05725b1Sbonwick 
2328e05725b1Sbonwick 	return (ZIO_PIPELINE_CONTINUE);
2329fa9e4066Sahrens }
2330fa9e4066Sahrens 
2331e05725b1Sbonwick static int
2332e14bb325SJeff Bonwick zio_gang_issue(zio_t *zio)
2333fa9e4066Sahrens {
2334e14bb325SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
2335fa9e4066Sahrens 
2336d6e1c446SGeorge Wilson 	if (zio_wait_for_children(zio, ZIO_CHILD_GANG_BIT, ZIO_WAIT_DONE)) {
2337e14bb325SJeff Bonwick 		return (ZIO_PIPELINE_STOP);
2338d6e1c446SGeorge Wilson 	}
2339fa9e4066Sahrens 
2340f5383399SBill Moore 	ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == zio);
2341f5383399SBill Moore 	ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
2342fa9e4066Sahrens 
2343e14bb325SJeff Bonwick 	if (zio->io_child_error[ZIO_CHILD_GANG] == 0)
2344770499e1SDan Kimmel 		zio_gang_tree_issue(zio, zio->io_gang_tree, bp, zio->io_abd,
2345770499e1SDan Kimmel 		    0);
2346e14bb325SJeff Bonwick 	else
2347f5383399SBill Moore 		zio_gang_tree_free(&zio->io_gang_tree);
2348fa9e4066Sahrens 
2349e14bb325SJeff Bonwick 	zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2350e05725b1Sbonwick 
2351e05725b1Sbonwick 	return (ZIO_PIPELINE_CONTINUE);
2352fa9e4066Sahrens }
2353fa9e4066Sahrens 
2354fa9e4066Sahrens static void
2355e14bb325SJeff Bonwick zio_write_gang_member_ready(zio_t *zio)
2356fa9e4066Sahrens {
2357a3f829aeSBill Moore 	zio_t *pio = zio_unique_parent(zio);
2358f5383399SBill Moore 	zio_t *gio = zio->io_gang_leader;
235944cd46caSbillm 	dva_t *cdva = zio->io_bp->blk_dva;
236044cd46caSbillm 	dva_t *pdva = pio->io_bp->blk_dva;
2361fa9e4066Sahrens 	uint64_t asize;
2362fa9e4066Sahrens 
2363e14bb325SJeff Bonwick 	if (BP_IS_HOLE(zio->io_bp))
2364e14bb325SJeff Bonwick 		return;
2365e14bb325SJeff Bonwick 
2366e14bb325SJeff Bonwick 	ASSERT(BP_IS_HOLE(&zio->io_bp_orig));
2367e14bb325SJeff Bonwick 
2368e14bb325SJeff Bonwick 	ASSERT(zio->io_child_type == ZIO_CHILD_GANG);
2369b24ab676SJeff Bonwick 	ASSERT3U(zio->io_prop.zp_copies, ==, gio->io_prop.zp_copies);
2370b24ab676SJeff Bonwick 	ASSERT3U(zio->io_prop.zp_copies, <=, BP_GET_NDVAS(zio->io_bp));
2371b24ab676SJeff Bonwick 	ASSERT3U(pio->io_prop.zp_copies, <=, BP_GET_NDVAS(pio->io_bp));
237244cd46caSbillm 	ASSERT3U(BP_GET_NDVAS(zio->io_bp), <=, BP_GET_NDVAS(pio->io_bp));
2373fa9e4066Sahrens 
2374fa9e4066Sahrens 	mutex_enter(&pio->io_lock);
2375e14bb325SJeff Bonwick 	for (int d = 0; d < BP_GET_NDVAS(zio->io_bp); d++) {
237644cd46caSbillm 		ASSERT(DVA_GET_GANG(&pdva[d]));
237744cd46caSbillm 		asize = DVA_GET_ASIZE(&pdva[d]);
237844cd46caSbillm 		asize += DVA_GET_ASIZE(&cdva[d]);
237944cd46caSbillm 		DVA_SET_ASIZE(&pdva[d], asize);
238044cd46caSbillm 	}
2381fa9e4066Sahrens 	mutex_exit(&pio->io_lock);
2382fa9e4066Sahrens }
2383fa9e4066Sahrens 
2384770499e1SDan Kimmel static void
2385770499e1SDan Kimmel zio_write_gang_done(zio_t *zio)
2386770499e1SDan Kimmel {
23877341a7deSBrad Lewis 	/*
23887341a7deSBrad Lewis 	 * The io_abd field will be NULL for a zio with no data.  The io_flags
23897341a7deSBrad Lewis 	 * will initially have the ZIO_FLAG_NODATA bit flag set, but we can't
23907341a7deSBrad Lewis 	 * check for it here as it is cleared in zio_ready.
23917341a7deSBrad Lewis 	 */
23927341a7deSBrad Lewis 	if (zio->io_abd != NULL)
23937341a7deSBrad Lewis 		abd_put(zio->io_abd);
2394770499e1SDan Kimmel }
2395770499e1SDan Kimmel 
23960a4e9518Sgw static int
2397e14bb325SJeff Bonwick zio_write_gang_block(zio_t *pio)
2398fa9e4066Sahrens {
2399e14bb325SJeff Bonwick 	spa_t *spa = pio->io_spa;
24000f7643c7SGeorge Wilson 	metaslab_class_t *mc = spa_normal_class(spa);
2401e14bb325SJeff Bonwick 	blkptr_t *bp = pio->io_bp;
2402f5383399SBill Moore 	zio_t *gio = pio->io_gang_leader;
2403e14bb325SJeff Bonwick 	zio_t *zio;
2404e14bb325SJeff Bonwick 	zio_gang_node_t *gn, **gnpp;
2405fa9e4066Sahrens 	zio_gbh_phys_t *gbh;
2406770499e1SDan Kimmel 	abd_t *gbh_abd;
2407e14bb325SJeff Bonwick 	uint64_t txg = pio->io_txg;
2408e14bb325SJeff Bonwick 	uint64_t resid = pio->io_size;
2409e14bb325SJeff Bonwick 	uint64_t lsize;
2410b24ab676SJeff Bonwick 	int copies = gio->io_prop.zp_copies;
2411b24ab676SJeff Bonwick 	int gbh_copies = MIN(copies + 1, spa_max_replication(spa));
2412e14bb325SJeff Bonwick 	zio_prop_t zp;
2413fa9e4066Sahrens 	int error;
24147341a7deSBrad Lewis 	boolean_t has_data = !(pio->io_flags & ZIO_FLAG_NODATA);
2415fa9e4066Sahrens 
2416eb633035STom Caputi 	/*
2417eb633035STom Caputi 	 * encrypted blocks need DVA[2] free so encrypted gang headers can't
2418eb633035STom Caputi 	 * have a third copy.
2419eb633035STom Caputi 	 */
2420eb633035STom Caputi 	if (gio->io_prop.zp_encrypt && gbh_copies >= SPA_DVAS_PER_BP)
2421eb633035STom Caputi 		gbh_copies = SPA_DVAS_PER_BP - 1;
2422eb633035STom Caputi 
24230f7643c7SGeorge Wilson 	int flags = METASLAB_HINTBP_FAVOR | METASLAB_GANG_HEADER;
24240f7643c7SGeorge Wilson 	if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
24250f7643c7SGeorge Wilson 		ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
24267341a7deSBrad Lewis 		ASSERT(has_data);
24270f7643c7SGeorge Wilson 
24280f7643c7SGeorge Wilson 		flags |= METASLAB_ASYNC_ALLOC;
2429e914ace2STim Schumacher 		VERIFY(zfs_refcount_held(&mc->mc_alloc_slots[pio->io_allocator],
2430f78cdc34SPaul Dagnelie 		    pio));
24310f7643c7SGeorge Wilson 
24320f7643c7SGeorge Wilson 		/*
24330f7643c7SGeorge Wilson 		 * The logical zio has already placed a reservation for
24340f7643c7SGeorge Wilson 		 * 'copies' allocation slots but gang blocks may require
24350f7643c7SGeorge Wilson 		 * additional copies. These additional copies
24360f7643c7SGeorge Wilson 		 * (i.e. gbh_copies - copies) are guaranteed to succeed
24370f7643c7SGeorge Wilson 		 * since metaslab_class_throttle_reserve() always allows
24380f7643c7SGeorge Wilson 		 * additional reservations for gang blocks.
24390f7643c7SGeorge Wilson 		 */
24400f7643c7SGeorge Wilson 		VERIFY(metaslab_class_throttle_reserve(mc, gbh_copies - copies,
2441f78cdc34SPaul Dagnelie 		    pio->io_allocator, pio, flags));
24420f7643c7SGeorge Wilson 	}
24430f7643c7SGeorge Wilson 
24440f7643c7SGeorge Wilson 	error = metaslab_alloc(spa, mc, SPA_GANGBLOCKSIZE,
24458363e80aSGeorge Wilson 	    bp, gbh_copies, txg, pio == gio ? NULL : gio->io_bp, flags,
2446f78cdc34SPaul Dagnelie 	    &pio->io_alloc_list, pio, pio->io_allocator);
2447e05725b1Sbonwick 	if (error) {
24480f7643c7SGeorge Wilson 		if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
24490f7643c7SGeorge Wilson 			ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
24507341a7deSBrad Lewis 			ASSERT(has_data);
24510f7643c7SGeorge Wilson 
24520f7643c7SGeorge Wilson 			/*
24530f7643c7SGeorge Wilson 			 * If we failed to allocate the gang block header then
24540f7643c7SGeorge Wilson 			 * we remove any additional allocation reservations that
24550f7643c7SGeorge Wilson 			 * we placed here. The original reservation will
24560f7643c7SGeorge Wilson 			 * be removed when the logical I/O goes to the ready
24570f7643c7SGeorge Wilson 			 * stage.
24580f7643c7SGeorge Wilson 			 */
24590f7643c7SGeorge Wilson 			metaslab_class_throttle_unreserve(mc,
2460f78cdc34SPaul Dagnelie 			    gbh_copies - copies, pio->io_allocator, pio);
24610f7643c7SGeorge Wilson 		}
2462e14bb325SJeff Bonwick 		pio->io_error = error;
2463e05725b1Sbonwick 		return (ZIO_PIPELINE_CONTINUE);
2464e05725b1Sbonwick 	}
2465fa9e4066Sahrens 
2466f5383399SBill Moore 	if (pio == gio) {
2467f5383399SBill Moore 		gnpp = &gio->io_gang_tree;
2468e14bb325SJeff Bonwick 	} else {
2469e14bb325SJeff Bonwick 		gnpp = pio->io_private;
2470e14bb325SJeff Bonwick 		ASSERT(pio->io_ready == zio_write_gang_member_ready);
2471fa9e4066Sahrens 	}
2472fa9e4066Sahrens 
2473e14bb325SJeff Bonwick 	gn = zio_gang_node_alloc(gnpp);
2474e14bb325SJeff Bonwick 	gbh = gn->gn_gbh;
2475e14bb325SJeff Bonwick 	bzero(gbh, SPA_GANGBLOCKSIZE);
2476770499e1SDan Kimmel 	gbh_abd = abd_get_from_buf(gbh, SPA_GANGBLOCKSIZE);
2477fa9e4066Sahrens 
2478e14bb325SJeff Bonwick 	/*
2479e14bb325SJeff Bonwick 	 * Create the gang header.
2480e14bb325SJeff Bonwick 	 */
2481770499e1SDan Kimmel 	zio = zio_rewrite(pio, spa, txg, bp, gbh_abd, SPA_GANGBLOCKSIZE,
2482770499e1SDan Kimmel 	    zio_write_gang_done, NULL, pio->io_priority,
2483770499e1SDan Kimmel 	    ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
2484fa9e4066Sahrens 
2485e14bb325SJeff Bonwick 	/*
2486e14bb325SJeff Bonwick 	 * Create and nowait the gang children.
2487e14bb325SJeff Bonwick 	 */
2488e14bb325SJeff Bonwick 	for (int g = 0; resid != 0; resid -= lsize, g++) {
2489e14bb325SJeff Bonwick 		lsize = P2ROUNDUP(resid / (SPA_GBH_NBLKPTRS - g),
2490e14bb325SJeff Bonwick 		    SPA_MINBLOCKSIZE);
2491e14bb325SJeff Bonwick 		ASSERT(lsize >= SPA_MINBLOCKSIZE && lsize <= resid);
2492e14bb325SJeff Bonwick 
2493f5383399SBill Moore 		zp.zp_checksum = gio->io_prop.zp_checksum;
2494e14bb325SJeff Bonwick 		zp.zp_compress = ZIO_COMPRESS_OFF;
2495e14bb325SJeff Bonwick 		zp.zp_type = DMU_OT_NONE;
2496e14bb325SJeff Bonwick 		zp.zp_level = 0;
2497b24ab676SJeff Bonwick 		zp.zp_copies = gio->io_prop.zp_copies;
249880901aeaSGeorge Wilson 		zp.zp_dedup = B_FALSE;
249980901aeaSGeorge Wilson 		zp.zp_dedup_verify = B_FALSE;
250080901aeaSGeorge Wilson 		zp.zp_nopwrite = B_FALSE;
2501eb633035STom Caputi 		zp.zp_encrypt = gio->io_prop.zp_encrypt;
2502eb633035STom Caputi 		zp.zp_byteorder = gio->io_prop.zp_byteorder;
2503eb633035STom Caputi 		bzero(zp.zp_salt, ZIO_DATA_SALT_LEN);
2504eb633035STom Caputi 		bzero(zp.zp_iv, ZIO_DATA_IV_LEN);
2505eb633035STom Caputi 		bzero(zp.zp_mac, ZIO_DATA_MAC_LEN);
2506e14bb325SJeff Bonwick 
25070f7643c7SGeorge Wilson 		zio_t *cio = zio_write(zio, spa, txg, &gbh->zg_blkptr[g],
25087341a7deSBrad Lewis 		    has_data ? abd_get_offset(pio->io_abd, pio->io_size -
25097341a7deSBrad Lewis 		    resid) : NULL, lsize, lsize, &zp,
25107341a7deSBrad Lewis 		    zio_write_gang_member_ready, NULL, NULL,
2511770499e1SDan Kimmel 		    zio_write_gang_done, &gn->gn_child[g], pio->io_priority,
25120f7643c7SGeorge Wilson 		    ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
25130f7643c7SGeorge Wilson 
25140f7643c7SGeorge Wilson 		if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
25150f7643c7SGeorge Wilson 			ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
25167341a7deSBrad Lewis 			ASSERT(has_data);
25170f7643c7SGeorge Wilson 
25180f7643c7SGeorge Wilson 			/*
25190f7643c7SGeorge Wilson 			 * Gang children won't throttle but we should
25200f7643c7SGeorge Wilson 			 * account for their work, so reserve an allocation
25210f7643c7SGeorge Wilson 			 * slot for them here.
25220f7643c7SGeorge Wilson 			 */
25230f7643c7SGeorge Wilson 			VERIFY(metaslab_class_throttle_reserve(mc,
2524f78cdc34SPaul Dagnelie 			    zp.zp_copies, cio->io_allocator, cio, flags));
25250f7643c7SGeorge Wilson 		}
25260f7643c7SGeorge Wilson 		zio_nowait(cio);
2527e14bb325SJeff Bonwick 	}
2528e05725b1Sbonwick 
252944cd46caSbillm 	/*
2530e14bb325SJeff Bonwick 	 * Set pio's pipeline to just wait for zio to finish.
253144cd46caSbillm 	 */
2532e14bb325SJeff Bonwick 	pio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2533e14bb325SJeff Bonwick 
2534e14bb325SJeff Bonwick 	zio_nowait(zio);
2535e14bb325SJeff Bonwick 
2536e14bb325SJeff Bonwick 	return (ZIO_PIPELINE_CONTINUE);
2537fa9e4066Sahrens }
2538fa9e4066Sahrens 
253980901aeaSGeorge Wilson /*
254045818ee1SMatthew Ahrens  * The zio_nop_write stage in the pipeline determines if allocating a
254145818ee1SMatthew Ahrens  * new bp is necessary.  The nopwrite feature can handle writes in
254245818ee1SMatthew Ahrens  * either syncing or open context (i.e. zil writes) and as a result is
254345818ee1SMatthew Ahrens  * mutually exclusive with dedup.
254445818ee1SMatthew Ahrens  *
254545818ee1SMatthew Ahrens  * By leveraging a cryptographically secure checksum, such as SHA256, we
254645818ee1SMatthew Ahrens  * can compare the checksums of the new data and the old to determine if
254745818ee1SMatthew Ahrens  * allocating a new block is required.  Note that our requirements for
254845818ee1SMatthew Ahrens  * cryptographic strength are fairly weak: there can't be any accidental
254945818ee1SMatthew Ahrens  * hash collisions, but we don't need to be secure against intentional
255045818ee1SMatthew Ahrens  * (malicious) collisions.  To trigger a nopwrite, you have to be able
255145818ee1SMatthew Ahrens  * to write the file to begin with, and triggering an incorrect (hash
255245818ee1SMatthew Ahrens  * collision) nopwrite is no worse than simply writing to the file.
255345818ee1SMatthew Ahrens  * That said, there are no known attacks against the checksum algorithms
255445818ee1SMatthew Ahrens  * used for nopwrite, assuming that the salt and the checksums
255545818ee1SMatthew Ahrens  * themselves remain secret.
255680901aeaSGeorge Wilson  */
255780901aeaSGeorge Wilson static int
255880901aeaSGeorge Wilson zio_nop_write(zio_t *zio)
255980901aeaSGeorge Wilson {
256080901aeaSGeorge Wilson 	blkptr_t *bp = zio->io_bp;
256180901aeaSGeorge Wilson 	blkptr_t *bp_orig = &zio->io_bp_orig;
256280901aeaSGeorge Wilson 	zio_prop_t *zp = &zio->io_prop;
256380901aeaSGeorge Wilson 
256480901aeaSGeorge Wilson 	ASSERT(BP_GET_LEVEL(bp) == 0);
256580901aeaSGeorge Wilson 	ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
256680901aeaSGeorge Wilson 	ASSERT(zp->zp_nopwrite);
256780901aeaSGeorge Wilson 	ASSERT(!zp->zp_dedup);
256880901aeaSGeorge Wilson 	ASSERT(zio->io_bp_override == NULL);
256980901aeaSGeorge Wilson 	ASSERT(IO_IS_ALLOCATING(zio));
257080901aeaSGeorge Wilson 
257180901aeaSGeorge Wilson 	/*
257280901aeaSGeorge Wilson 	 * Check to see if the original bp and the new bp have matching
257380901aeaSGeorge Wilson 	 * characteristics (i.e. same checksum, compression algorithms, etc).
257480901aeaSGeorge Wilson 	 * If they don't then just continue with the pipeline which will
257580901aeaSGeorge Wilson 	 * allocate a new bp.
257680901aeaSGeorge Wilson 	 */
257780901aeaSGeorge Wilson 	if (BP_IS_HOLE(bp_orig) ||
257845818ee1SMatthew Ahrens 	    !(zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_flags &
257945818ee1SMatthew Ahrens 	    ZCHECKSUM_FLAG_NOPWRITE) ||
2580eb633035STom Caputi 	    BP_IS_ENCRYPTED(bp) || BP_IS_ENCRYPTED(bp_orig) ||
258180901aeaSGeorge Wilson 	    BP_GET_CHECKSUM(bp) != BP_GET_CHECKSUM(bp_orig) ||
258280901aeaSGeorge Wilson 	    BP_GET_COMPRESS(bp) != BP_GET_COMPRESS(bp_orig) ||
258380901aeaSGeorge Wilson 	    BP_GET_DEDUP(bp) != BP_GET_DEDUP(bp_orig) ||
258480901aeaSGeorge Wilson 	    zp->zp_copies != BP_GET_NDVAS(bp_orig))
258580901aeaSGeorge Wilson 		return (ZIO_PIPELINE_CONTINUE);
258680901aeaSGeorge Wilson 
258780901aeaSGeorge Wilson 	/*
258880901aeaSGeorge Wilson 	 * If the checksums match then reset the pipeline so that we
258980901aeaSGeorge Wilson 	 * avoid allocating a new bp and issuing any I/O.
259080901aeaSGeorge Wilson 	 */
259180901aeaSGeorge Wilson 	if (ZIO_CHECKSUM_EQUAL(bp->blk_cksum, bp_orig->blk_cksum)) {
259245818ee1SMatthew Ahrens 		ASSERT(zio_checksum_table[zp->zp_checksum].ci_flags &
259345818ee1SMatthew Ahrens 		    ZCHECKSUM_FLAG_NOPWRITE);
259480901aeaSGeorge Wilson 		ASSERT3U(BP_GET_PSIZE(bp), ==, BP_GET_PSIZE(bp_orig));
259580901aeaSGeorge Wilson 		ASSERT3U(BP_GET_LSIZE(bp), ==, BP_GET_LSIZE(bp_orig));
259680901aeaSGeorge Wilson 		ASSERT(zp->zp_compress != ZIO_COMPRESS_OFF);
259780901aeaSGeorge Wilson 		ASSERT(bcmp(&bp->blk_prop, &bp_orig->blk_prop,
259880901aeaSGeorge Wilson 		    sizeof (uint64_t)) == 0);
259980901aeaSGeorge Wilson 
260080901aeaSGeorge Wilson 		*bp = *bp_orig;
260180901aeaSGeorge Wilson 		zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
260280901aeaSGeorge Wilson 		zio->io_flags |= ZIO_FLAG_NOPWRITE;
260380901aeaSGeorge Wilson 	}
260480901aeaSGeorge Wilson 
260580901aeaSGeorge Wilson 	return (ZIO_PIPELINE_CONTINUE);
260680901aeaSGeorge Wilson }
260780901aeaSGeorge Wilson 
2608fa9e4066Sahrens /*
2609fa9e4066Sahrens  * ==========================================================================
2610b24ab676SJeff Bonwick  * Dedup
2611fa9e4066Sahrens  * ==========================================================================
2612fa9e4066Sahrens  */
2613b24ab676SJeff Bonwick static void
2614b24ab676SJeff Bonwick zio_ddt_child_read_done(zio_t *zio)
2615b24ab676SJeff Bonwick {
2616b24ab676SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
2617b24ab676SJeff Bonwick 	ddt_entry_t *dde = zio->io_private;
2618b24ab676SJeff Bonwick 	ddt_phys_t *ddp;
2619b24ab676SJeff Bonwick 	zio_t *pio = zio_unique_parent(zio);
2620b24ab676SJeff Bonwick 
2621b24ab676SJeff Bonwick 	mutex_enter(&pio->io_lock);
2622b24ab676SJeff Bonwick 	ddp = ddt_phys_select(dde, bp);
2623b24ab676SJeff Bonwick 	if (zio->io_error == 0)
2624b24ab676SJeff Bonwick 		ddt_phys_clear(ddp);	/* this ddp doesn't need repair */
2625770499e1SDan Kimmel 
2626770499e1SDan Kimmel 	if (zio->io_error == 0 && dde->dde_repair_abd == NULL)
2627770499e1SDan Kimmel 		dde->dde_repair_abd = zio->io_abd;
2628b24ab676SJeff Bonwick 	else
2629770499e1SDan Kimmel 		abd_free(zio->io_abd);
2630b24ab676SJeff Bonwick 	mutex_exit(&pio->io_lock);
2631b24ab676SJeff Bonwick }
2632b24ab676SJeff Bonwick 
2633b24ab676SJeff Bonwick static int
2634b24ab676SJeff Bonwick zio_ddt_read_start(zio_t *zio)
2635b24ab676SJeff Bonwick {
2636b24ab676SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
2637b24ab676SJeff Bonwick 
2638b24ab676SJeff Bonwick 	ASSERT(BP_GET_DEDUP(bp));
2639b24ab676SJeff Bonwick 	ASSERT(BP_GET_PSIZE(bp) == zio->io_size);
2640b24ab676SJeff Bonwick 	ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2641b24ab676SJeff Bonwick 
2642b24ab676SJeff Bonwick 	if (zio->io_child_error[ZIO_CHILD_DDT]) {
2643b24ab676SJeff Bonwick 		ddt_t *ddt = ddt_select(zio->io_spa, bp);
2644b24ab676SJeff Bonwick 		ddt_entry_t *dde = ddt_repair_start(ddt, bp);
2645b24ab676SJeff Bonwick 		ddt_phys_t *ddp = dde->dde_phys;
2646b24ab676SJeff Bonwick 		ddt_phys_t *ddp_self = ddt_phys_select(dde, bp);
2647b24ab676SJeff Bonwick 		blkptr_t blk;
2648b24ab676SJeff Bonwick 
2649b24ab676SJeff Bonwick 		ASSERT(zio->io_vsd == NULL);
2650b24ab676SJeff Bonwick 		zio->io_vsd = dde;
2651b24ab676SJeff Bonwick 
2652b24ab676SJeff Bonwick 		if (ddp_self == NULL)
2653b24ab676SJeff Bonwick 			return (ZIO_PIPELINE_CONTINUE);
2654b24ab676SJeff Bonwick 
2655b24ab676SJeff Bonwick 		for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
2656b24ab676SJeff Bonwick 			if (ddp->ddp_phys_birth == 0 || ddp == ddp_self)
2657b24ab676SJeff Bonwick 				continue;
2658bbfd46c4SJeff Bonwick 			ddt_bp_create(ddt->ddt_checksum, &dde->dde_key, ddp,
2659bbfd46c4SJeff Bonwick 			    &blk);
2660b24ab676SJeff Bonwick 			zio_nowait(zio_read(zio, zio->io_spa, &blk,
2661770499e1SDan Kimmel 			    abd_alloc_for_io(zio->io_size, B_TRUE),
2662770499e1SDan Kimmel 			    zio->io_size, zio_ddt_child_read_done, dde,
2663770499e1SDan Kimmel 			    zio->io_priority, ZIO_DDT_CHILD_FLAGS(zio) |
2664770499e1SDan Kimmel 			    ZIO_FLAG_DONT_PROPAGATE, &zio->io_bookmark));
2665b24ab676SJeff Bonwick 		}
2666b24ab676SJeff Bonwick 		return (ZIO_PIPELINE_CONTINUE);
2667b24ab676SJeff Bonwick 	}
2668b24ab676SJeff Bonwick 
2669b24ab676SJeff Bonwick 	zio_nowait(zio_read(zio, zio->io_spa, bp,
2670770499e1SDan Kimmel 	    zio->io_abd, zio->io_size, NULL, NULL, zio->io_priority,
2671b24ab676SJeff Bonwick 	    ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark));
2672b24ab676SJeff Bonwick 
2673b24ab676SJeff Bonwick 	return (ZIO_PIPELINE_CONTINUE);
2674b24ab676SJeff Bonwick }
2675e14bb325SJeff Bonwick 
2676b24ab676SJeff Bonwick static int
2677b24ab676SJeff Bonwick zio_ddt_read_done(zio_t *zio)
2678b24ab676SJeff Bonwick {
2679b24ab676SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
2680b24ab676SJeff Bonwick 
2681d6e1c446SGeorge Wilson 	if (zio_wait_for_children(zio, ZIO_CHILD_DDT_BIT, ZIO_WAIT_DONE)) {
2682b24ab676SJeff Bonwick 		return (ZIO_PIPELINE_STOP);
2683d6e1c446SGeorge Wilson 	}
2684b24ab676SJeff Bonwick 
2685b24ab676SJeff Bonwick 	ASSERT(BP_GET_DEDUP(bp));
2686b24ab676SJeff Bonwick 	ASSERT(BP_GET_PSIZE(bp) == zio->io_size);
2687b24ab676SJeff Bonwick 	ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2688b24ab676SJeff Bonwick 
2689b24ab676SJeff Bonwick 	if (zio->io_child_error[ZIO_CHILD_DDT]) {
2690b24ab676SJeff Bonwick 		ddt_t *ddt = ddt_select(zio->io_spa, bp);
2691b24ab676SJeff Bonwick 		ddt_entry_t *dde = zio->io_vsd;
2692b24ab676SJeff Bonwick 		if (ddt == NULL) {
2693b16da2e2SGeorge Wilson 			ASSERT(spa_load_state(zio->io_spa) != SPA_LOAD_NONE);
2694b24ab676SJeff Bonwick 			return (ZIO_PIPELINE_CONTINUE);
2695b24ab676SJeff Bonwick 		}
2696b24ab676SJeff Bonwick 		if (dde == NULL) {
2697b24ab676SJeff Bonwick 			zio->io_stage = ZIO_STAGE_DDT_READ_START >> 1;
269835a5a358SJonathan Adams 			zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE);
2699b24ab676SJeff Bonwick 			return (ZIO_PIPELINE_STOP);
2700b24ab676SJeff Bonwick 		}
2701770499e1SDan Kimmel 		if (dde->dde_repair_abd != NULL) {
2702770499e1SDan Kimmel 			abd_copy(zio->io_abd, dde->dde_repair_abd,
2703770499e1SDan Kimmel 			    zio->io_size);
2704b24ab676SJeff Bonwick 			zio->io_child_error[ZIO_CHILD_DDT] = 0;
2705b24ab676SJeff Bonwick 		}
2706b24ab676SJeff Bonwick 		ddt_repair_done(ddt, dde);
2707b24ab676SJeff Bonwick 		zio->io_vsd = NULL;
2708b24ab676SJeff Bonwick 	}
2709b24ab676SJeff Bonwick 
2710b24ab676SJeff Bonwick 	ASSERT(zio->io_vsd == NULL);
2711b24ab676SJeff Bonwick 
2712b24ab676SJeff Bonwick 	return (ZIO_PIPELINE_CONTINUE);
2713b24ab676SJeff Bonwick }
2714b24ab676SJeff Bonwick 
2715b24ab676SJeff Bonwick static boolean_t
2716b24ab676SJeff Bonwick zio_ddt_collision(zio_t *zio, ddt_t *ddt, ddt_entry_t *dde)
2717b24ab676SJeff Bonwick {
2718b24ab676SJeff Bonwick 	spa_t *spa = zio->io_spa;
2719eb633035STom Caputi 	boolean_t do_raw = !!(zio->io_flags & ZIO_FLAG_RAW);
27205602294fSDan Kimmel 
27215602294fSDan Kimmel 	/* We should never get a raw, override zio */
27225602294fSDan Kimmel 	ASSERT(!(zio->io_bp_override && do_raw));
2723b24ab676SJeff Bonwick 
2724b24ab676SJeff Bonwick 	/*
2725b24ab676SJeff Bonwick 	 * Note: we compare the original data, not the transformed data,
2726b24ab676SJeff Bonwick 	 * because when zio->io_bp is an override bp, we will not have
2727b24ab676SJeff Bonwick 	 * pushed the I/O transforms.  That's an important optimization
2728b24ab676SJeff Bonwick 	 * because otherwise we'd compress/encrypt all dmu_sync() data twice.
2729eb633035STom Caputi 	 * However, we should never get a raw, override zio so in these
2730eb633035STom Caputi 	 * cases we can compare the io_data directly. This is useful because
2731eb633035STom Caputi 	 * it allows us to do dedup verification even if we don't have access
2732eb633035STom Caputi 	 * to the original data (for instance, if the encryption keys aren't
2733eb633035STom Caputi 	 * loaded).
2734b24ab676SJeff Bonwick 	 */
2735eb633035STom Caputi 
2736b24ab676SJeff Bonwick 	for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) {
2737b24ab676SJeff Bonwick 		zio_t *lio = dde->dde_lead_zio[p];
2738b24ab676SJeff Bonwick 
2739eb633035STom Caputi 		if (lio != NULL && do_raw) {
2740eb633035STom Caputi 			return (lio->io_size != zio->io_size ||
2741eb633035STom Caputi 			    abd_cmp(zio->io_abd, lio->io_abd,
2742eb633035STom Caputi 			    zio->io_size) != 0);
2743eb633035STom Caputi 		} else if (lio != NULL) {
2744b24ab676SJeff Bonwick 			return (lio->io_orig_size != zio->io_orig_size ||
2745770499e1SDan Kimmel 			    abd_cmp(zio->io_orig_abd, lio->io_orig_abd,
2746b24ab676SJeff Bonwick 			    zio->io_orig_size) != 0);
2747b24ab676SJeff Bonwick 		}
2748b24ab676SJeff Bonwick 	}
2749b24ab676SJeff Bonwick 
2750b24ab676SJeff Bonwick 	for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) {
2751b24ab676SJeff Bonwick 		ddt_phys_t *ddp = &dde->dde_phys[p];
2752b24ab676SJeff Bonwick 
2753eb633035STom Caputi 		if (ddp->ddp_phys_birth != 0 && do_raw) {
2754eb633035STom Caputi 			blkptr_t blk = *zio->io_bp;
2755eb633035STom Caputi 			uint64_t psize;
2756eb633035STom Caputi 			abd_t *tmpabd;
2757eb633035STom Caputi 			int error;
2758eb633035STom Caputi 
2759eb633035STom Caputi 			ddt_bp_fill(ddp, &blk, ddp->ddp_phys_birth);
2760eb633035STom Caputi 			psize = BP_GET_PSIZE(&blk);
2761eb633035STom Caputi 
2762eb633035STom Caputi 			if (psize != zio->io_size)
2763eb633035STom Caputi 				return (B_TRUE);
2764eb633035STom Caputi 
2765eb633035STom Caputi 			ddt_exit(ddt);
2766eb633035STom Caputi 
2767eb633035STom Caputi 			tmpabd = abd_alloc_for_io(psize, B_TRUE);
2768eb633035STom Caputi 
2769eb633035STom Caputi 			error = zio_wait(zio_read(NULL, spa, &blk, tmpabd,
2770eb633035STom Caputi 			    psize, NULL, NULL, ZIO_PRIORITY_SYNC_READ,
2771eb633035STom Caputi 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
2772eb633035STom Caputi 			    ZIO_FLAG_RAW, &zio->io_bookmark));
2773eb633035STom Caputi 
2774eb633035STom Caputi 			if (error == 0) {
2775eb633035STom Caputi 				if (abd_cmp(tmpabd, zio->io_abd, psize) != 0)
2776eb633035STom Caputi 					error = SET_ERROR(ENOENT);
2777eb633035STom Caputi 			}
2778eb633035STom Caputi 
2779eb633035STom Caputi 			abd_free(tmpabd);
2780eb633035STom Caputi 			ddt_enter(ddt);
2781eb633035STom Caputi 			return (error != 0);
2782eb633035STom Caputi 		} else if (ddp->ddp_phys_birth != 0) {
2783b24ab676SJeff Bonwick 			arc_buf_t *abuf = NULL;
27847adb730bSGeorge Wilson 			arc_flags_t aflags = ARC_FLAG_WAIT;
27855602294fSDan Kimmel 			int zio_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE;
2786b24ab676SJeff Bonwick 			blkptr_t blk = *zio->io_bp;
2787b24ab676SJeff Bonwick 			int error;
2788b24ab676SJeff Bonwick 
2789b24ab676SJeff Bonwick 			ddt_bp_fill(ddp, &blk, ddp->ddp_phys_birth);
2790b24ab676SJeff Bonwick 
2791eb633035STom Caputi 			if (BP_GET_LSIZE(&blk) != zio->io_orig_size)
2792eb633035STom Caputi 				return (B_TRUE);
2793eb633035STom Caputi 
2794b24ab676SJeff Bonwick 			ddt_exit(ddt);
2795b24ab676SJeff Bonwick 
27965602294fSDan Kimmel 			/*
27975602294fSDan Kimmel 			 * Intuitively, it would make more sense to compare
2798770499e1SDan Kimmel 			 * io_abd than io_orig_abd in the raw case since you
27995602294fSDan Kimmel 			 * don't want to look at any transformations that have
28005602294fSDan Kimmel 			 * happened to the data. However, for raw I/Os the
2801770499e1SDan Kimmel 			 * data will actually be the same in io_abd and
2802770499e1SDan Kimmel 			 * io_orig_abd, so all we have to do is issue this as
28035602294fSDan Kimmel 			 * a raw ARC read.
28045602294fSDan Kimmel 			 */
28055602294fSDan Kimmel 			if (do_raw) {
28065602294fSDan Kimmel 				zio_flags |= ZIO_FLAG_RAW;
28075602294fSDan Kimmel 				ASSERT3U(zio->io_size, ==, zio->io_orig_size);
2808770499e1SDan Kimmel 				ASSERT0(abd_cmp(zio->io_abd, zio->io_orig_abd,
28095602294fSDan Kimmel 				    zio->io_size));
28105602294fSDan Kimmel 				ASSERT3P(zio->io_transform_stack, ==, NULL);
28115602294fSDan Kimmel 			}
28125602294fSDan Kimmel 
28131b912ec7SGeorge Wilson 			error = arc_read(NULL, spa, &blk,
2814b24ab676SJeff Bonwick 			    arc_getbuf_func, &abuf, ZIO_PRIORITY_SYNC_READ,
28155602294fSDan Kimmel 			    zio_flags, &aflags, &zio->io_bookmark);
2816b24ab676SJeff Bonwick 
2817b24ab676SJeff Bonwick 			if (error == 0) {
2818eb633035STom Caputi 				if (abd_cmp_buf(zio->io_orig_abd, abuf->b_data,
2819b24ab676SJeff Bonwick 				    zio->io_orig_size) != 0)
2820eb633035STom Caputi 					error = SET_ERROR(ENOENT);
2821dcbf3bd6SGeorge Wilson 				arc_buf_destroy(abuf, &abuf);
2822b24ab676SJeff Bonwick 			}
2823b24ab676SJeff Bonwick 
2824b24ab676SJeff Bonwick 			ddt_enter(ddt);
2825b24ab676SJeff Bonwick 			return (error != 0);
2826b24ab676SJeff Bonwick 		}
2827b24ab676SJeff Bonwick 	}
2828b24ab676SJeff Bonwick 
2829b24ab676SJeff Bonwick 	return (B_FALSE);
2830b24ab676SJeff Bonwick }
2831b24ab676SJeff Bonwick 
2832b24ab676SJeff Bonwick static void
2833b24ab676SJeff Bonwick zio_ddt_child_write_ready(zio_t *zio)
2834b24ab676SJeff Bonwick {
2835b24ab676SJeff Bonwick 	int p = zio->io_prop.zp_copies;
2836b24ab676SJeff Bonwick 	ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp);
2837b24ab676SJeff Bonwick 	ddt_entry_t *dde = zio->io_private;
2838b24ab676SJeff Bonwick 	ddt_phys_t *ddp = &dde->dde_phys[p];
2839b24ab676SJeff Bonwick 	zio_t *pio;
2840b24ab676SJeff Bonwick 
2841b24ab676SJeff Bonwick 	if (zio->io_error)
2842b24ab676SJeff Bonwick 		return;
2843b24ab676SJeff Bonwick 
2844b24ab676SJeff Bonwick 	ddt_enter(ddt);
2845b24ab676SJeff Bonwick 
2846b24ab676SJeff Bonwick 	ASSERT(dde->dde_lead_zio[p] == zio);
2847b24ab676SJeff Bonwick 
2848b24ab676SJeff Bonwick 	ddt_phys_fill(ddp, zio->io_bp);
2849b24ab676SJeff Bonwick 
28500f7643c7SGeorge Wilson 	zio_link_t *zl = NULL;
28510f7643c7SGeorge Wilson 	while ((pio = zio_walk_parents(zio, &zl)) != NULL)
2852b24ab676SJeff Bonwick 		ddt_bp_fill(ddp, pio->io_bp, zio->io_txg);
2853b24ab676SJeff Bonwick 
2854b24ab676SJeff Bonwick 	ddt_exit(ddt);
2855b24ab676SJeff Bonwick }
2856b24ab676SJeff Bonwick 
2857b24ab676SJeff Bonwick static void
2858b24ab676SJeff Bonwick zio_ddt_child_write_done(zio_t *zio)
2859b24ab676SJeff Bonwick {
2860b24ab676SJeff Bonwick 	int p = zio->io_prop.zp_copies;
2861b24ab676SJeff Bonwick 	ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp);
2862b24ab676SJeff Bonwick 	ddt_entry_t *dde = zio->io_private;
2863b24ab676SJeff Bonwick 	ddt_phys_t *ddp = &dde->dde_phys[p];
2864b24ab676SJeff Bonwick 
2865b24ab676SJeff Bonwick 	ddt_enter(ddt);
2866b24ab676SJeff Bonwick 
2867b24ab676SJeff Bonwick 	ASSERT(ddp->ddp_refcnt == 0);
2868b24ab676SJeff Bonwick 	ASSERT(dde->dde_lead_zio[p] == zio);
2869b24ab676SJeff Bonwick 	dde->dde_lead_zio[p] = NULL;
2870b24ab676SJeff Bonwick 
2871b24ab676SJeff Bonwick 	if (zio->io_error == 0) {
28720f7643c7SGeorge Wilson 		zio_link_t *zl = NULL;
28730f7643c7SGeorge Wilson 		while (zio_walk_parents(zio, &zl) != NULL)
2874b24ab676SJeff Bonwick 			ddt_phys_addref(ddp);
2875b24ab676SJeff Bonwick 	} else {
2876b24ab676SJeff Bonwick 		ddt_phys_clear(ddp);
2877b24ab676SJeff Bonwick 	}
2878b24ab676SJeff Bonwick 
2879b24ab676SJeff Bonwick 	ddt_exit(ddt);
2880b24ab676SJeff Bonwick }
2881b24ab676SJeff Bonwick 
2882b24ab676SJeff Bonwick static void
2883b24ab676SJeff Bonwick zio_ddt_ditto_write_done(zio_t *zio)
2884b24ab676SJeff Bonwick {
2885b24ab676SJeff Bonwick 	int p = DDT_PHYS_DITTO;
2886b24ab676SJeff Bonwick 	zio_prop_t *zp = &zio->io_prop;
2887b24ab676SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
2888b24ab676SJeff Bonwick 	ddt_t *ddt = ddt_select(zio->io_spa, bp);
2889b24ab676SJeff Bonwick 	ddt_entry_t *dde = zio->io_private;
2890b24ab676SJeff Bonwick 	ddt_phys_t *ddp = &dde->dde_phys[p];
2891b24ab676SJeff Bonwick 	ddt_key_t *ddk = &dde->dde_key;
2892b24ab676SJeff Bonwick 
2893b24ab676SJeff Bonwick 	ddt_enter(ddt);
2894b24ab676SJeff Bonwick 
2895b24ab676SJeff Bonwick 	ASSERT(ddp->ddp_refcnt == 0);
2896b24ab676SJeff Bonwick 	ASSERT(dde->dde_lead_zio[p] == zio);
2897b24ab676SJeff Bonwick 	dde->dde_lead_zio[p] = NULL;
2898b24ab676SJeff Bonwick 
2899b24ab676SJeff Bonwick 	if (zio->io_error == 0) {
2900b24ab676SJeff Bonwick 		ASSERT(ZIO_CHECKSUM_EQUAL(bp->blk_cksum, ddk->ddk_cksum));
2901b24ab676SJeff Bonwick 		ASSERT(zp->zp_copies < SPA_DVAS_PER_BP);
2902b24ab676SJeff Bonwick 		ASSERT(zp->zp_copies == BP_GET_NDVAS(bp) - BP_IS_GANG(bp));
2903b24ab676SJeff Bonwick 		if (ddp->ddp_phys_birth != 0)
2904b24ab676SJeff Bonwick 			ddt_phys_free(ddt, ddk, ddp, zio->io_txg);
2905b24ab676SJeff Bonwick 		ddt_phys_fill(ddp, bp);
2906b24ab676SJeff Bonwick 	}
2907b24ab676SJeff Bonwick 
2908b24ab676SJeff Bonwick 	ddt_exit(ddt);
2909b24ab676SJeff Bonwick }
2910b24ab676SJeff Bonwick 
2911b24ab676SJeff Bonwick static int
2912b24ab676SJeff Bonwick zio_ddt_write(zio_t *zio)
2913b24ab676SJeff Bonwick {
2914b24ab676SJeff Bonwick 	spa_t *spa = zio->io_spa;
2915b24ab676SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
2916b24ab676SJeff Bonwick 	uint64_t txg = zio->io_txg;
2917b24ab676SJeff Bonwick 	zio_prop_t *zp = &zio->io_prop;
2918b24ab676SJeff Bonwick 	int p = zp->zp_copies;
2919b24ab676SJeff Bonwick 	int ditto_copies;
2920b24ab676SJeff Bonwick 	zio_t *cio = NULL;
2921b24ab676SJeff Bonwick 	zio_t *dio = NULL;
2922b24ab676SJeff Bonwick 	ddt_t *ddt = ddt_select(spa, bp);
2923b24ab676SJeff Bonwick 	ddt_entry_t *dde;
2924b24ab676SJeff Bonwick 	ddt_phys_t *ddp;
2925b24ab676SJeff Bonwick 
2926b24ab676SJeff Bonwick 	ASSERT(BP_GET_DEDUP(bp));
2927b24ab676SJeff Bonwick 	ASSERT(BP_GET_CHECKSUM(bp) == zp->zp_checksum);
2928b24ab676SJeff Bonwick 	ASSERT(BP_IS_HOLE(bp) || zio->io_bp_override);
29295602294fSDan Kimmel 	ASSERT(!(zio->io_bp_override && (zio->io_flags & ZIO_FLAG_RAW)));
2930b24ab676SJeff Bonwick 
2931b24ab676SJeff Bonwick 	ddt_enter(ddt);
2932b24ab676SJeff Bonwick 	dde = ddt_lookup(ddt, bp, B_TRUE);
2933b24ab676SJeff Bonwick 	ddp = &dde->dde_phys[p];
2934b24ab676SJeff Bonwick 
2935b24ab676SJeff Bonwick 	if (zp->zp_dedup_verify && zio_ddt_collision(zio, ddt, dde)) {
2936b24ab676SJeff Bonwick 		/*
2937b24ab676SJeff Bonwick 		 * If we're using a weak checksum, upgrade to a strong checksum
2938b24ab676SJeff Bonwick 		 * and try again.  If we're already using a strong checksum,
2939b24ab676SJeff Bonwick 		 * we can't resolve it, so just convert to an ordinary write.
2940b24ab676SJeff Bonwick 		 * (And automatically e-mail a paper to Nature?)
2941b24ab676SJeff Bonwick 		 */
294245818ee1SMatthew Ahrens 		if (!(zio_checksum_table[zp->zp_checksum].ci_flags &
294345818ee1SMatthew Ahrens 		    ZCHECKSUM_FLAG_DEDUP)) {
2944b24ab676SJeff Bonwick 			zp->zp_checksum = spa_dedup_checksum(spa);
2945b24ab676SJeff Bonwick 			zio_pop_transforms(zio);
2946b24ab676SJeff Bonwick 			zio->io_stage = ZIO_STAGE_OPEN;
2947b24ab676SJeff Bonwick 			BP_ZERO(bp);
2948b24ab676SJeff Bonwick 		} else {
294980901aeaSGeorge Wilson 			zp->zp_dedup = B_FALSE;
29505602294fSDan Kimmel 			BP_SET_DEDUP(bp, B_FALSE);
2951b24ab676SJeff Bonwick 		}
29525602294fSDan Kimmel 		ASSERT(!BP_GET_DEDUP(bp));
2953b24ab676SJeff Bonwick 		zio->io_pipeline = ZIO_WRITE_PIPELINE;
2954b24ab676SJeff Bonwick 		ddt_exit(ddt);
2955b24ab676SJeff Bonwick 		return (ZIO_PIPELINE_CONTINUE);
2956b24ab676SJeff Bonwick 	}
2957b24ab676SJeff Bonwick 
2958b24ab676SJeff Bonwick 	ditto_copies = ddt_ditto_copies_needed(ddt, dde, ddp);
2959b24ab676SJeff Bonwick 	ASSERT(ditto_copies < SPA_DVAS_PER_BP);
2960b24ab676SJeff Bonwick 
2961b24ab676SJeff Bonwick 	if (ditto_copies > ddt_ditto_copies_present(dde) &&
2962b24ab676SJeff Bonwick 	    dde->dde_lead_zio[DDT_PHYS_DITTO] == NULL) {
2963b24ab676SJeff Bonwick 		zio_prop_t czp = *zp;
2964b24ab676SJeff Bonwick 
2965b24ab676SJeff Bonwick 		czp.zp_copies = ditto_copies;
2966b24ab676SJeff Bonwick 
2967b24ab676SJeff Bonwick 		/*
2968b24ab676SJeff Bonwick 		 * If we arrived here with an override bp, we won't have run
2969b24ab676SJeff Bonwick 		 * the transform stack, so we won't have the data we need to
2970b24ab676SJeff Bonwick 		 * generate a child i/o.  So, toss the override bp and restart.
2971b24ab676SJeff Bonwick 		 * This is safe, because using the override bp is just an
2972b24ab676SJeff Bonwick 		 * optimization; and it's rare, so the cost doesn't matter.
2973b24ab676SJeff Bonwick 		 */
2974b24ab676SJeff Bonwick 		if (zio->io_bp_override) {
2975b24ab676SJeff Bonwick 			zio_pop_transforms(zio);
2976b24ab676SJeff Bonwick 			zio->io_stage = ZIO_STAGE_OPEN;
2977b24ab676SJeff Bonwick 			zio->io_pipeline = ZIO_WRITE_PIPELINE;
2978b24ab676SJeff Bonwick 			zio->io_bp_override = NULL;
2979b24ab676SJeff Bonwick 			BP_ZERO(bp);
2980b24ab676SJeff Bonwick 			ddt_exit(ddt);
2981b24ab676SJeff Bonwick 			return (ZIO_PIPELINE_CONTINUE);
2982b24ab676SJeff Bonwick 		}
2983b24ab676SJeff Bonwick 
2984770499e1SDan Kimmel 		dio = zio_write(zio, spa, txg, bp, zio->io_orig_abd,
29855602294fSDan Kimmel 		    zio->io_orig_size, zio->io_orig_size, &czp, NULL, NULL,
29868df0bcf0SPaul Dagnelie 		    NULL, zio_ddt_ditto_write_done, dde, zio->io_priority,
2987b24ab676SJeff Bonwick 		    ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark);
2988b24ab676SJeff Bonwick 
2989770499e1SDan Kimmel 		zio_push_transform(dio, zio->io_abd, zio->io_size, 0, NULL);
2990b24ab676SJeff Bonwick 		dde->dde_lead_zio[DDT_PHYS_DITTO] = dio;
2991b24ab676SJeff Bonwick 	}
2992b24ab676SJeff Bonwick 
2993b24ab676SJeff Bonwick 	if (ddp->ddp_phys_birth != 0 || dde->dde_lead_zio[p] != NULL) {
2994b24ab676SJeff Bonwick 		if (ddp->ddp_phys_birth != 0)
2995b24ab676SJeff Bonwick 			ddt_bp_fill(ddp, bp, txg);
2996b24ab676SJeff Bonwick 		if (dde->dde_lead_zio[p] != NULL)
2997b24ab676SJeff Bonwick 			zio_add_child(zio, dde->dde_lead_zio[p]);
2998b24ab676SJeff Bonwick 		else
2999b24ab676SJeff Bonwick 			ddt_phys_addref(ddp);
3000b24ab676SJeff Bonwick 	} else if (zio->io_bp_override) {
3001b24ab676SJeff Bonwick 		ASSERT(bp->blk_birth == txg);
3002b24ab676SJeff Bonwick 		ASSERT(BP_EQUAL(bp, zio->io_bp_override));
3003b24ab676SJeff Bonwick 		ddt_phys_fill(ddp, bp);
3004b24ab676SJeff Bonwick 		ddt_phys_addref(ddp);
3005b24ab676SJeff Bonwick 	} else {
3006770499e1SDan Kimmel 		cio = zio_write(zio, spa, txg, bp, zio->io_orig_abd,
30075602294fSDan Kimmel 		    zio->io_orig_size, zio->io_orig_size, zp,
30088df0bcf0SPaul Dagnelie 		    zio_ddt_child_write_ready, NULL, NULL,
3009b24ab676SJeff Bonwick 		    zio_ddt_child_write_done, dde, zio->io_priority,
3010b24ab676SJeff Bonwick 		    ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark);
3011b24ab676SJeff Bonwick 
3012770499e1SDan Kimmel 		zio_push_transform(cio, zio->io_abd, zio->io_size, 0, NULL);
3013b24ab676SJeff Bonwick 		dde->dde_lead_zio[p] = cio;
3014b24ab676SJeff Bonwick 	}
3015b24ab676SJeff Bonwick 
3016b24ab676SJeff Bonwick 	ddt_exit(ddt);
3017b24ab676SJeff Bonwick 
3018b24ab676SJeff Bonwick 	if (cio)
3019b24ab676SJeff Bonwick 		zio_nowait(cio);
3020b24ab676SJeff Bonwick 	if (dio)
3021b24ab676SJeff Bonwick 		zio_nowait(dio);
3022b24ab676SJeff Bonwick 
3023b24ab676SJeff Bonwick 	return (ZIO_PIPELINE_CONTINUE);
3024b24ab676SJeff Bonwick }
3025b24ab676SJeff Bonwick 
30263f9d6ad7SLin Ling ddt_entry_t *freedde; /* for debugging */
30273f9d6ad7SLin Ling 
3028b24ab676SJeff Bonwick static int
3029b24ab676SJeff Bonwick zio_ddt_free(zio_t *zio)
3030b24ab676SJeff Bonwick {
3031b24ab676SJeff Bonwick 	spa_t *spa = zio->io_spa;
3032b24ab676SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
3033b24ab676SJeff Bonwick 	ddt_t *ddt = ddt_select(spa, bp);
3034b24ab676SJeff Bonwick 	ddt_entry_t *dde;
3035b24ab676SJeff Bonwick 	ddt_phys_t *ddp;
3036b24ab676SJeff Bonwick 
3037b24ab676SJeff Bonwick 	ASSERT(BP_GET_DEDUP(bp));
3038b24ab676SJeff Bonwick 	ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
3039b24ab676SJeff Bonwick 
3040b24ab676SJeff Bonwick 	ddt_enter(ddt);
30413f9d6ad7SLin Ling 	freedde = dde = ddt_lookup(ddt, bp, B_TRUE);
3042b24ab676SJeff Bonwick 	ddp = ddt_phys_select(dde, bp);
3043b24ab676SJeff Bonwick 	ddt_phys_decref(ddp);
3044b24ab676SJeff Bonwick 	ddt_exit(ddt);
3045b24ab676SJeff Bonwick 
3046b24ab676SJeff Bonwick 	return (ZIO_PIPELINE_CONTINUE);
3047b24ab676SJeff Bonwick }
3048b24ab676SJeff Bonwick 
3049b24ab676SJeff Bonwick /*
3050b24ab676SJeff Bonwick  * ==========================================================================
3051b24ab676SJeff Bonwick  * Allocate and free blocks
3052b24ab676SJeff Bonwick  * ==========================================================================
3053b24ab676SJeff Bonwick  */
30540f7643c7SGeorge Wilson 
30550f7643c7SGeorge Wilson static zio_t *
3056f78cdc34SPaul Dagnelie zio_io_to_allocate(spa_t *spa, int allocator)
30570f7643c7SGeorge Wilson {
30580f7643c7SGeorge Wilson 	zio_t *zio;
30590f7643c7SGeorge Wilson 
3060f78cdc34SPaul Dagnelie 	ASSERT(MUTEX_HELD(&spa->spa_alloc_locks[allocator]));
30610f7643c7SGeorge Wilson 
3062f78cdc34SPaul Dagnelie 	zio = avl_first(&spa->spa_alloc_trees[allocator]);
30630f7643c7SGeorge Wilson 	if (zio == NULL)
30640f7643c7SGeorge Wilson 		return (NULL);
30650f7643c7SGeorge Wilson 
30660f7643c7SGeorge Wilson 	ASSERT(IO_IS_ALLOCATING(zio));
30670f7643c7SGeorge Wilson 
30680f7643c7SGeorge Wilson 	/*
30690f7643c7SGeorge Wilson 	 * Try to place a reservation for this zio. If we're unable to
30700f7643c7SGeorge Wilson 	 * reserve then we throttle.
30710f7643c7SGeorge Wilson 	 */
3072f78cdc34SPaul Dagnelie 	ASSERT3U(zio->io_allocator, ==, allocator);
3073663207adSDon Brady 	if (!metaslab_class_throttle_reserve(zio->io_metaslab_class,
3074f78cdc34SPaul Dagnelie 	    zio->io_prop.zp_copies, zio->io_allocator, zio, 0)) {
30750f7643c7SGeorge Wilson 		return (NULL);
30760f7643c7SGeorge Wilson 	}
30770f7643c7SGeorge Wilson 
3078f78cdc34SPaul Dagnelie 	avl_remove(&spa->spa_alloc_trees[allocator], zio);
30790f7643c7SGeorge Wilson 	ASSERT3U(zio->io_stage, <, ZIO_STAGE_DVA_ALLOCATE);
30800f7643c7SGeorge Wilson 
30810f7643c7SGeorge Wilson 	return (zio);
30820f7643c7SGeorge Wilson }
30830f7643c7SGeorge Wilson 
30840f7643c7SGeorge Wilson static int
30850f7643c7SGeorge Wilson zio_dva_throttle(zio_t *zio)
30860f7643c7SGeorge Wilson {
30870f7643c7SGeorge Wilson 	spa_t *spa = zio->io_spa;
30880f7643c7SGeorge Wilson 	zio_t *nio;
3089663207adSDon Brady 	metaslab_class_t *mc;
3090663207adSDon Brady 
3091663207adSDon Brady 	/* locate an appropriate allocation class */
3092663207adSDon Brady 	mc = spa_preferred_class(spa, zio->io_size, zio->io_prop.zp_type,
3093663207adSDon Brady 	    zio->io_prop.zp_level, zio->io_prop.zp_zpl_smallblk);
30940f7643c7SGeorge Wilson 
30950f7643c7SGeorge Wilson 	if (zio->io_priority == ZIO_PRIORITY_SYNC_WRITE ||
3096663207adSDon Brady 	    !mc->mc_alloc_throttle_enabled ||
30970f7643c7SGeorge Wilson 	    zio->io_child_type == ZIO_CHILD_GANG ||
30980f7643c7SGeorge Wilson 	    zio->io_flags & ZIO_FLAG_NODATA) {
30990f7643c7SGeorge Wilson 		return (ZIO_PIPELINE_CONTINUE);
31000f7643c7SGeorge Wilson 	}
31010f7643c7SGeorge Wilson 
31020f7643c7SGeorge Wilson 	ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
31030f7643c7SGeorge Wilson 
31040f7643c7SGeorge Wilson 	ASSERT3U(zio->io_queued_timestamp, >, 0);
31050f7643c7SGeorge Wilson 	ASSERT(zio->io_stage == ZIO_STAGE_DVA_THROTTLE);
31060f7643c7SGeorge Wilson 
3107f78cdc34SPaul Dagnelie 	zbookmark_phys_t *bm = &zio->io_bookmark;
3108f78cdc34SPaul Dagnelie 	/*
3109f78cdc34SPaul Dagnelie 	 * We want to try to use as many allocators as possible to help improve
3110f78cdc34SPaul Dagnelie 	 * performance, but we also want logically adjacent IOs to be physically
3111f78cdc34SPaul Dagnelie 	 * adjacent to improve sequential read performance. We chunk each object
3112f78cdc34SPaul Dagnelie 	 * into 2^20 block regions, and then hash based on the objset, object,
3113f78cdc34SPaul Dagnelie 	 * level, and region to accomplish both of these goals.
3114f78cdc34SPaul Dagnelie 	 */
3115f78cdc34SPaul Dagnelie 	zio->io_allocator = cityhash4(bm->zb_objset, bm->zb_object,
3116f78cdc34SPaul Dagnelie 	    bm->zb_level, bm->zb_blkid >> 20) % spa->spa_alloc_count;
3117f78cdc34SPaul Dagnelie 	mutex_enter(&spa->spa_alloc_locks[zio->io_allocator]);
31180f7643c7SGeorge Wilson 	ASSERT(zio->io_type == ZIO_TYPE_WRITE);
3119663207adSDon Brady 	zio->io_metaslab_class = mc;
3120f78cdc34SPaul Dagnelie 	avl_add(&spa->spa_alloc_trees[zio->io_allocator], zio);
3121663207adSDon Brady 	nio = zio_io_to_allocate(spa, zio->io_allocator);
3122f78cdc34SPaul Dagnelie 	mutex_exit(&spa->spa_alloc_locks[zio->io_allocator]);
31230f7643c7SGeorge Wilson 
31240f7643c7SGeorge Wilson 	if (nio == zio)
31250f7643c7SGeorge Wilson 		return (ZIO_PIPELINE_CONTINUE);
31260f7643c7SGeorge Wilson 
31270f7643c7SGeorge Wilson 	if (nio != NULL) {
31280f7643c7SGeorge Wilson 		ASSERT(nio->io_stage == ZIO_STAGE_DVA_THROTTLE);
31290f7643c7SGeorge Wilson 		/*
31300f7643c7SGeorge Wilson 		 * We are passing control to a new zio so make sure that
31310f7643c7SGeorge Wilson 		 * it is processed by a different thread. We do this to
31320f7643c7SGeorge Wilson 		 * avoid stack overflows that can occur when parents are
31330f7643c7SGeorge Wilson 		 * throttled and children are making progress. We allow
31340f7643c7SGeorge Wilson 		 * it to go to the head of the taskq since it's already
31350f7643c7SGeorge Wilson 		 * been waiting.
31360f7643c7SGeorge Wilson 		 */
31370f7643c7SGeorge Wilson 		zio_taskq_dispatch(nio, ZIO_TASKQ_ISSUE, B_TRUE);
31380f7643c7SGeorge Wilson 	}
31390f7643c7SGeorge Wilson 	return (ZIO_PIPELINE_STOP);
31400f7643c7SGeorge Wilson }
31410f7643c7SGeorge Wilson 
3142663207adSDon Brady static void
3143f78cdc34SPaul Dagnelie zio_allocate_dispatch(spa_t *spa, int allocator)
31440f7643c7SGeorge Wilson {
31450f7643c7SGeorge Wilson 	zio_t *zio;
31460f7643c7SGeorge Wilson 
3147f78cdc34SPaul Dagnelie 	mutex_enter(&spa->spa_alloc_locks[allocator]);
3148f78cdc34SPaul Dagnelie 	zio = zio_io_to_allocate(spa, allocator);
3149f78cdc34SPaul Dagnelie 	mutex_exit(&spa->spa_alloc_locks[allocator]);
31500f7643c7SGeorge Wilson 	if (zio == NULL)
31510f7643c7SGeorge Wilson 		return;
31520f7643c7SGeorge Wilson 
31530f7643c7SGeorge Wilson 	ASSERT3U(zio->io_stage, ==, ZIO_STAGE_DVA_THROTTLE);
31540f7643c7SGeorge Wilson 	ASSERT0(zio->io_error);
31550f7643c7SGeorge Wilson 	zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_TRUE);
31560f7643c7SGeorge Wilson }
31570f7643c7SGeorge Wilson 
3158e05725b1Sbonwick static int
3159fa9e4066Sahrens zio_dva_allocate(zio_t *zio)
3160fa9e4066Sahrens {
31618654d025Sperrin 	spa_t *spa = zio->io_spa;
3162663207adSDon Brady 	metaslab_class_t *mc;
3163fa9e4066Sahrens 	blkptr_t *bp = zio->io_bp;
3164fa9e4066Sahrens 	int error;
316509c9d376SGeorge Wilson 	int flags = 0;
3166fa9e4066Sahrens 
3167f5383399SBill Moore 	if (zio->io_gang_leader == NULL) {
3168f5383399SBill Moore 		ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
3169f5383399SBill Moore 		zio->io_gang_leader = zio;
3170f5383399SBill Moore 	}
3171f5383399SBill Moore 
3172fa9e4066Sahrens 	ASSERT(BP_IS_HOLE(bp));
3173fb09f5aaSMadhav Suresh 	ASSERT0(BP_GET_NDVAS(bp));
3174b24ab676SJeff Bonwick 	ASSERT3U(zio->io_prop.zp_copies, >, 0);
3175b24ab676SJeff Bonwick 	ASSERT3U(zio->io_prop.zp_copies, <=, spa_max_replication(spa));
3176fa9e4066Sahrens 	ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp));
3177fa9e4066Sahrens 
3178663207adSDon Brady 	if (zio->io_flags & ZIO_FLAG_NODATA)
31790f7643c7SGeorge Wilson 		flags |= METASLAB_DONT_THROTTLE;
3180663207adSDon Brady 	if (zio->io_flags & ZIO_FLAG_GANG_CHILD)
31810f7643c7SGeorge Wilson 		flags |= METASLAB_GANG_CHILD;
3182663207adSDon Brady 	if (zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE)
31830f7643c7SGeorge Wilson 		flags |= METASLAB_ASYNC_ALLOC;
3184663207adSDon Brady 
3185663207adSDon Brady 	/*
3186663207adSDon Brady 	 * if not already chosen, locate an appropriate allocation class
3187663207adSDon Brady 	 */
3188663207adSDon Brady 	mc = zio->io_metaslab_class;
3189663207adSDon Brady 	if (mc == NULL) {
3190663207adSDon Brady 		mc = spa_preferred_class(spa, zio->io_size,
3191663207adSDon Brady 		    zio->io_prop.zp_type, zio->io_prop.zp_level,
3192663207adSDon Brady 		    zio->io_prop.zp_zpl_smallblk);
3193663207adSDon Brady 		zio->io_metaslab_class = mc;
31940f7643c7SGeorge Wilson 	}
31950f7643c7SGeorge Wilson 
3196e14bb325SJeff Bonwick 	error = metaslab_alloc(spa, mc, zio->io_size, bp,
31978363e80aSGeorge Wilson 	    zio->io_prop.zp_copies, zio->io_txg, NULL, flags,
3198f78cdc34SPaul Dagnelie 	    &zio->io_alloc_list, zio, zio->io_allocator);
3199fa9e4066Sahrens 
3200663207adSDon Brady 	/*
3201663207adSDon Brady 	 * Fallback to normal class when an alloc class is full
3202663207adSDon Brady 	 */
3203663207adSDon Brady 	if (error == ENOSPC && mc != spa_normal_class(spa)) {
3204663207adSDon Brady 		/*
3205663207adSDon Brady 		 * If throttling, transfer reservation over to normal class.
3206663207adSDon Brady 		 * The io_allocator slot can remain the same even though we
3207663207adSDon Brady 		 * are switching classes.
3208663207adSDon Brady 		 */
3209663207adSDon Brady 		if (mc->mc_alloc_throttle_enabled &&
3210663207adSDon Brady 		    (zio->io_flags & ZIO_FLAG_IO_ALLOCATING)) {
3211663207adSDon Brady 			metaslab_class_throttle_unreserve(mc,
3212663207adSDon Brady 			    zio->io_prop.zp_copies, zio->io_allocator, zio);
3213663207adSDon Brady 			zio->io_flags &= ~ZIO_FLAG_IO_ALLOCATING;
3214663207adSDon Brady 
3215663207adSDon Brady 			mc = spa_normal_class(spa);
3216663207adSDon Brady 			VERIFY(metaslab_class_throttle_reserve(mc,
3217663207adSDon Brady 			    zio->io_prop.zp_copies, zio->io_allocator, zio,
3218663207adSDon Brady 			    flags | METASLAB_MUST_RESERVE));
3219663207adSDon Brady 		} else {
3220663207adSDon Brady 			mc = spa_normal_class(spa);
3221663207adSDon Brady 		}
3222663207adSDon Brady 		zio->io_metaslab_class = mc;
3223663207adSDon Brady 
3224663207adSDon Brady 		error = metaslab_alloc(spa, mc, zio->io_size, bp,
3225663207adSDon Brady 		    zio->io_prop.zp_copies, zio->io_txg, NULL, flags,
3226663207adSDon Brady 		    &zio->io_alloc_list, zio, zio->io_allocator);
3227663207adSDon Brady 	}
3228663207adSDon Brady 
32290f7643c7SGeorge Wilson 	if (error != 0) {
323021f7c81cSMatthew Ahrens 		zfs_dbgmsg("%s: metaslab allocation failure: zio %p, "
323109c9d376SGeorge Wilson 		    "size %llu, error %d", spa_name(spa), zio, zio->io_size,
323209c9d376SGeorge Wilson 		    error);
3233e14bb325SJeff Bonwick 		if (error == ENOSPC && zio->io_size > SPA_MINBLOCKSIZE)
3234e14bb325SJeff Bonwick 			return (zio_write_gang_block(zio));
3235fa9e4066Sahrens 		zio->io_error = error;
3236fa9e4066Sahrens 	}
3237e05725b1Sbonwick 
3238e05725b1Sbonwick 	return (ZIO_PIPELINE_CONTINUE);
3239fa9e4066Sahrens }
3240fa9e4066Sahrens 
3241e05725b1Sbonwick static int
3242fa9e4066Sahrens zio_dva_free(zio_t *zio)
3243fa9e4066Sahrens {
3244e14bb325SJeff Bonwick 	metaslab_free(zio->io_spa, zio->io_bp, zio->io_txg, B_FALSE);
3245fa9e4066Sahrens 
3246e05725b1Sbonwick 	return (ZIO_PIPELINE_CONTINUE);
3247fa9e4066Sahrens }
3248fa9e4066Sahrens 
3249e05725b1Sbonwick static int
3250fa9e4066Sahrens zio_dva_claim(zio_t *zio)
3251fa9e4066Sahrens {
3252e14bb325SJeff Bonwick 	int error;
3253e14bb325SJeff Bonwick 
3254e14bb325SJeff Bonwick 	error = metaslab_claim(zio->io_spa, zio->io_bp, zio->io_txg);
3255e14bb325SJeff Bonwick 	if (error)
3256e14bb325SJeff Bonwick 		zio->io_error = error;
3257fa9e4066Sahrens 
3258e05725b1Sbonwick 	return (ZIO_PIPELINE_CONTINUE);
3259fa9e4066Sahrens }
3260fa9e4066Sahrens 
3261e14bb325SJeff Bonwick /*
3262e14bb325SJeff Bonwick  * Undo an allocation.  This is used by zio_done() when an I/O fails
3263e14bb325SJeff Bonwick  * and we want to give back the block we just allocated.
3264e14bb325SJeff Bonwick  * This handles both normal blocks and gang blocks.
3265e14bb325SJeff Bonwick  */
3266e14bb325SJeff Bonwick static void
3267e14bb325SJeff Bonwick zio_dva_unallocate(zio_t *zio, zio_gang_node_t *gn, blkptr_t *bp)
3268e14bb325SJeff Bonwick {
3269e14bb325SJeff Bonwick 	ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp));
3270b24ab676SJeff Bonwick 	ASSERT(zio->io_bp_override == NULL);
3271e14bb325SJeff Bonwick 
3272e14bb325SJeff Bonwick 	if (!BP_IS_HOLE(bp))
3273b24ab676SJeff Bonwick 		metaslab_free(zio->io_spa, bp, bp->blk_birth, B_TRUE);
3274e14bb325SJeff Bonwick 
3275e14bb325SJeff Bonwick 	if (gn != NULL) {
3276e14bb325SJeff Bonwick 		for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
3277e14bb325SJeff Bonwick 			zio_dva_unallocate(zio, gn->gn_child[g],
3278e14bb325SJeff Bonwick 			    &gn->gn_gbh->zg_blkptr[g]);
3279e14bb325SJeff Bonwick 		}
3280e14bb325SJeff Bonwick 	}
3281e14bb325SJeff Bonwick }
3282e14bb325SJeff Bonwick 
3283e14bb325SJeff Bonwick /*
3284e14bb325SJeff Bonwick  * Try to allocate an intent log block.  Return 0 on success, errno on failure.
3285e14bb325SJeff Bonwick  */
3286e14bb325SJeff Bonwick int
3287eb633035STom Caputi zio_alloc_zil(spa_t *spa, objset_t *os, uint64_t txg, blkptr_t *new_bp,
3288f78cdc34SPaul Dagnelie     blkptr_t *old_bp, uint64_t size, boolean_t *slog)
3289e14bb325SJeff Bonwick {
3290e09fa4daSNeil Perrin 	int error = 1;
32918363e80aSGeorge Wilson 	zio_alloc_list_t io_alloc_list;
3292e14bb325SJeff Bonwick 
3293b24ab676SJeff Bonwick 	ASSERT(txg > spa_syncing_txg(spa));
3294b24ab676SJeff Bonwick 
32958363e80aSGeorge Wilson 	metaslab_trace_init(&io_alloc_list);
3296663207adSDon Brady 
3297663207adSDon Brady 	/*
3298663207adSDon Brady 	 * Block pointer fields are useful to metaslabs for stats and debugging.
3299663207adSDon Brady 	 * Fill in the obvious ones before calling into metaslab_alloc().
3300663207adSDon Brady 	 */
3301663207adSDon Brady 	BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG);
3302663207adSDon Brady 	BP_SET_PSIZE(new_bp, size);
3303663207adSDon Brady 	BP_SET_LEVEL(new_bp, 0);
3304663207adSDon Brady 
3305f78cdc34SPaul Dagnelie 	/*
3306f78cdc34SPaul Dagnelie 	 * When allocating a zil block, we don't have information about
3307f78cdc34SPaul Dagnelie 	 * the final destination of the block except the objset it's part
3308f78cdc34SPaul Dagnelie 	 * of, so we just hash the objset ID to pick the allocator to get
3309f78cdc34SPaul Dagnelie 	 * some parallelism.
3310f78cdc34SPaul Dagnelie 	 */
3311c5ee4681SAlexander Motin 	error = metaslab_alloc(spa, spa_log_class(spa), size, new_bp, 1,
3312f78cdc34SPaul Dagnelie 	    txg, old_bp, METASLAB_HINTBP_AVOID, &io_alloc_list, NULL,
3313eb633035STom Caputi 	    cityhash4(0, 0, 0,
3314eb633035STom Caputi 	    os->os_dsl_dataset->ds_object) % spa->spa_alloc_count);
3315c5ee4681SAlexander Motin 	if (error == 0) {
3316c5ee4681SAlexander Motin 		*slog = TRUE;
3317c5ee4681SAlexander Motin 	} else {
3318b24ab676SJeff Bonwick 		error = metaslab_alloc(spa, spa_normal_class(spa), size,
33198363e80aSGeorge Wilson 		    new_bp, 1, txg, old_bp, METASLAB_HINTBP_AVOID,
3320eb633035STom Caputi 		    &io_alloc_list, NULL, cityhash4(0, 0, 0,
3321eb633035STom Caputi 		    os->os_dsl_dataset->ds_object) % spa->spa_alloc_count);
3322c5ee4681SAlexander Motin 		if (error == 0)
3323c5ee4681SAlexander Motin 			*slog = FALSE;
3324840345f6SGeorge Wilson 	}
33258363e80aSGeorge Wilson 	metaslab_trace_fini(&io_alloc_list);
3326e14bb325SJeff Bonwick 
3327e14bb325SJeff Bonwick 	if (error == 0) {
3328e14bb325SJeff Bonwick 		BP_SET_LSIZE(new_bp, size);
3329e14bb325SJeff Bonwick 		BP_SET_PSIZE(new_bp, size);
3330e14bb325SJeff Bonwick 		BP_SET_COMPRESS(new_bp, ZIO_COMPRESS_OFF);
33316e1f5caaSNeil Perrin 		BP_SET_CHECKSUM(new_bp,
33326e1f5caaSNeil Perrin 		    spa_version(spa) >= SPA_VERSION_SLIM_ZIL
33336e1f5caaSNeil Perrin 		    ? ZIO_CHECKSUM_ZILOG2 : ZIO_CHECKSUM_ZILOG);
3334e14bb325SJeff Bonwick 		BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG);
3335e14bb325SJeff Bonwick 		BP_SET_LEVEL(new_bp, 0);
3336b24ab676SJeff Bonwick 		BP_SET_DEDUP(new_bp, 0);
3337e14bb325SJeff Bonwick 		BP_SET_BYTEORDER(new_bp, ZFS_HOST_BYTEORDER);
3338eb633035STom Caputi 
3339eb633035STom Caputi 		/*
3340eb633035STom Caputi 		 * encrypted blocks will require an IV and salt. We generate
3341eb633035STom Caputi 		 * these now since we will not be rewriting the bp at
3342eb633035STom Caputi 		 * rewrite time.
3343eb633035STom Caputi 		 */
3344eb633035STom Caputi 		if (os->os_encrypted) {
3345eb633035STom Caputi 			uint8_t iv[ZIO_DATA_IV_LEN];
3346eb633035STom Caputi 			uint8_t salt[ZIO_DATA_SALT_LEN];
3347eb633035STom Caputi 
3348eb633035STom Caputi 			BP_SET_CRYPT(new_bp, B_TRUE);
3349eb633035STom Caputi 			VERIFY0(spa_crypt_get_salt(spa,
3350eb633035STom Caputi 			    dmu_objset_id(os), salt));
3351eb633035STom Caputi 			VERIFY0(zio_crypt_generate_iv(iv));
3352eb633035STom Caputi 
3353eb633035STom Caputi 			zio_crypt_encode_params_bp(new_bp, salt, iv);
3354eb633035STom Caputi 		}
33551271e4b1SPrakash Surya 	} else {
33561271e4b1SPrakash Surya 		zfs_dbgmsg("%s: zil block allocation failure: "
33571271e4b1SPrakash Surya 		    "size %llu, error %d", spa_name(spa), size, error);
3358e14bb325SJeff Bonwick 	}
3359e14bb325SJeff Bonwick 
3360e14bb325SJeff Bonwick 	return (error);
3361e14bb325SJeff Bonwick }
3362e14bb325SJeff Bonwick 
3363fa9e4066Sahrens /*
3364fa9e4066Sahrens  * ==========================================================================
3365fa9e4066Sahrens  * Read and write to physical devices
3366fa9e4066Sahrens  * ==========================================================================
3367fa9e4066Sahrens  */
3368738f37bcSGeorge Wilson 
3369738f37bcSGeorge Wilson /*
3370738f37bcSGeorge Wilson  * Issue an I/O to the underlying vdev. Typically the issue pipeline
3371738f37bcSGeorge Wilson  * stops after this stage and will resume upon I/O completion.
3372738f37bcSGeorge Wilson  * However, there are instances where the vdev layer may need to
3373738f37bcSGeorge Wilson  * continue the pipeline when an I/O was not issued. Since the I/O
3374738f37bcSGeorge Wilson  * that was sent to the vdev layer might be different than the one
3375738f37bcSGeorge Wilson  * currently active in the pipeline (see vdev_queue_io()), we explicitly
3376738f37bcSGeorge Wilson  * force the underlying vdev layers to call either zio_execute() or
3377738f37bcSGeorge Wilson  * zio_interrupt() to ensure that the pipeline continues with the correct I/O.
3378738f37bcSGeorge Wilson  */
3379e05725b1Sbonwick static int
338044cd46caSbillm zio_vdev_io_start(zio_t *zio)
3381fa9e4066Sahrens {
3382fa9e4066Sahrens 	vdev_t *vd = zio->io_vd;
338344cd46caSbillm 	uint64_t align;
33840a4e9518Sgw 	spa_t *spa = zio->io_spa;
33850a4e9518Sgw 
3386e14bb325SJeff Bonwick 	ASSERT(zio->io_error == 0);
3387e14bb325SJeff Bonwick 	ASSERT(zio->io_child_error[ZIO_CHILD_VDEV] == 0);
3388fa9e4066Sahrens 
3389e14bb325SJeff Bonwick 	if (vd == NULL) {
3390e14bb325SJeff Bonwick 		if (!(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
3391e14bb325SJeff Bonwick 			spa_config_enter(spa, SCL_ZIO, zio, RW_READER);
3392fa9e4066Sahrens 
3393e14bb325SJeff Bonwick 		/*
3394e14bb325SJeff Bonwick 		 * The mirror_ops handle multiple DVAs in a single BP.
3395e14bb325SJeff Bonwick 		 */
3396738f37bcSGeorge Wilson 		vdev_mirror_ops.vdev_op_io_start(zio);
3397738f37bcSGeorge Wilson 		return (ZIO_PIPELINE_STOP);
3398fa9e4066Sahrens 	}
3399fa9e4066Sahrens 
34000f7643c7SGeorge Wilson 	ASSERT3P(zio->io_logical, !=, zio);
34016f793812SPavel Zakharov 	if (zio->io_type == ZIO_TYPE_WRITE) {
34026f793812SPavel Zakharov 		ASSERT(spa->spa_trust_config);
34036f793812SPavel Zakharov 
3404a3874b8bSToomas Soome 		/*
3405a3874b8bSToomas Soome 		 * Note: the code can handle other kinds of writes,
3406a3874b8bSToomas Soome 		 * but we don't expect them.
3407a3874b8bSToomas Soome 		 */
34086f793812SPavel Zakharov 		if (zio->io_vd->vdev_removing) {
34096f793812SPavel Zakharov 			ASSERT(zio->io_flags &
34106f793812SPavel Zakharov 			    (ZIO_FLAG_PHYSICAL | ZIO_FLAG_SELF_HEAL |
34113a4b1be9SMatthew Ahrens 			    ZIO_FLAG_RESILVER | ZIO_FLAG_INDUCE_DAMAGE));
34126f793812SPavel Zakharov 		}
34135cabbc6bSPrashanth Sreenivasa 	}
34140f7643c7SGeorge Wilson 
3415e14bb325SJeff Bonwick 	align = 1ULL << vd->vdev_top->vdev_ashift;
3416e14bb325SJeff Bonwick 
34172a104a52SAlex Reece 	if (!(zio->io_flags & ZIO_FLAG_PHYSICAL) &&
34182a104a52SAlex Reece 	    P2PHASE(zio->io_size, align) != 0) {
34192a104a52SAlex Reece 		/* Transform logical writes to be a full physical block size. */
3420ecc2d604Sbonwick 		uint64_t asize = P2ROUNDUP(zio->io_size, align);
3421770499e1SDan Kimmel 		abd_t *abuf = abd_alloc_sametype(zio->io_abd, asize);
3422e14bb325SJeff Bonwick 		ASSERT(vd == vd->vdev_top);
3423ecc2d604Sbonwick 		if (zio->io_type == ZIO_TYPE_WRITE) {
3424770499e1SDan Kimmel 			abd_copy(abuf, zio->io_abd, zio->io_size);
3425770499e1SDan Kimmel 			abd_zero_off(abuf, zio->io_size, asize - zio->io_size);
3426ecc2d604Sbonwick 		}
3427e14bb325SJeff Bonwick 		zio_push_transform(zio, abuf, asize, asize, zio_subblock);
3428ecc2d604Sbonwick 	}
3429ecc2d604Sbonwick 
34302a104a52SAlex Reece 	/*
34312a104a52SAlex Reece 	 * If this is not a physical io, make sure that it is properly aligned
34322a104a52SAlex Reece 	 * before proceeding.
34332a104a52SAlex Reece 	 */
34342a104a52SAlex Reece 	if (!(zio->io_flags & ZIO_FLAG_PHYSICAL)) {
34352a104a52SAlex Reece 		ASSERT0(P2PHASE(zio->io_offset, align));
34362a104a52SAlex Reece 		ASSERT0(P2PHASE(zio->io_size, align));
34372a104a52SAlex Reece 	} else {
34382a104a52SAlex Reece 		/*
34392a104a52SAlex Reece 		 * For physical writes, we allow 512b aligned writes and assume
34402a104a52SAlex Reece 		 * the device will perform a read-modify-write as necessary.
34412a104a52SAlex Reece 		 */
34422a104a52SAlex Reece 		ASSERT0(P2PHASE(zio->io_offset, SPA_MINBLOCKSIZE));
34432a104a52SAlex Reece 		ASSERT0(P2PHASE(zio->io_size, SPA_MINBLOCKSIZE));
34442a104a52SAlex Reece 	}
34452a104a52SAlex Reece 
3446f9af39baSGeorge Wilson 	VERIFY(zio->io_type != ZIO_TYPE_WRITE || spa_writeable(spa));
34478ad4d6ddSJeff Bonwick 
34488ad4d6ddSJeff Bonwick 	/*
34498ad4d6ddSJeff Bonwick 	 * If this is a repair I/O, and there's no self-healing involved --
34508ad4d6ddSJeff Bonwick 	 * that is, we're just resilvering what we expect to resilver --
34518ad4d6ddSJeff Bonwick 	 * then don't do the I/O unless zio's txg is actually in vd's DTL.
34523a4b1be9SMatthew Ahrens 	 * This prevents spurious resilvering.
34533a4b1be9SMatthew Ahrens 	 *
34543a4b1be9SMatthew Ahrens 	 * There are a few ways that we can end up creating these spurious
34553a4b1be9SMatthew Ahrens 	 * resilver i/os:
34563a4b1be9SMatthew Ahrens 	 *
34573a4b1be9SMatthew Ahrens 	 * 1. A resilver i/o will be issued if any DVA in the BP has a
34583a4b1be9SMatthew Ahrens 	 * dirty DTL.  The mirror code will issue resilver writes to
34593a4b1be9SMatthew Ahrens 	 * each DVA, including the one(s) that are not on vdevs with dirty
34603a4b1be9SMatthew Ahrens 	 * DTLs.
34613a4b1be9SMatthew Ahrens 	 *
34623a4b1be9SMatthew Ahrens 	 * 2. With nested replication, which happens when we have a
34633a4b1be9SMatthew Ahrens 	 * "replacing" or "spare" vdev that's a child of a mirror or raidz.
34643a4b1be9SMatthew Ahrens 	 * For example, given mirror(replacing(A+B), C), it's likely that
34653a4b1be9SMatthew Ahrens 	 * only A is out of date (it's the new device). In this case, we'll
34663a4b1be9SMatthew Ahrens 	 * read from C, then use the data to resilver A+B -- but we don't
34673a4b1be9SMatthew Ahrens 	 * actually want to resilver B, just A. The top-level mirror has no
34683a4b1be9SMatthew Ahrens 	 * way to know this, so instead we just discard unnecessary repairs
34693a4b1be9SMatthew Ahrens 	 * as we work our way down the vdev tree.
34703a4b1be9SMatthew Ahrens 	 *
34713a4b1be9SMatthew Ahrens 	 * 3. ZTEST also creates mirrors of mirrors, mirrors of raidz, etc.
34723a4b1be9SMatthew Ahrens 	 * The same logic applies to any form of nested replication: ditto
34733a4b1be9SMatthew Ahrens 	 * + mirror, RAID-Z + replacing, etc.
34743a4b1be9SMatthew Ahrens 	 *
34753a4b1be9SMatthew Ahrens 	 * However, indirect vdevs point off to other vdevs which may have
34763a4b1be9SMatthew Ahrens 	 * DTL's, so we never bypass them.  The child i/os on concrete vdevs
34773a4b1be9SMatthew Ahrens 	 * will be properly bypassed instead.
34788ad4d6ddSJeff Bonwick 	 */
34798ad4d6ddSJeff Bonwick 	if ((zio->io_flags & ZIO_FLAG_IO_REPAIR) &&
34808ad4d6ddSJeff Bonwick 	    !(zio->io_flags & ZIO_FLAG_SELF_HEAL) &&
34818ad4d6ddSJeff Bonwick 	    zio->io_txg != 0 &&	/* not a delegated i/o */
34823a4b1be9SMatthew Ahrens 	    vd->vdev_ops != &vdev_indirect_ops &&
34838ad4d6ddSJeff Bonwick 	    !vdev_dtl_contains(vd, DTL_PARTIAL, zio->io_txg, 1)) {
34848ad4d6ddSJeff Bonwick 		ASSERT(zio->io_type == ZIO_TYPE_WRITE);
34858ad4d6ddSJeff Bonwick 		zio_vdev_io_bypass(zio);
34868ad4d6ddSJeff Bonwick 		return (ZIO_PIPELINE_CONTINUE);
34878ad4d6ddSJeff Bonwick 	}
3488fa9e4066Sahrens 
3489*084fd14fSBrian Behlendorf 	if (vd->vdev_ops->vdev_op_leaf && (zio->io_type == ZIO_TYPE_READ ||
3490*084fd14fSBrian Behlendorf 	    zio->io_type == ZIO_TYPE_WRITE || zio->io_type == ZIO_TYPE_TRIM)) {
3491e14bb325SJeff Bonwick 
349243466aaeSMax Grossman 		if (zio->io_type == ZIO_TYPE_READ && vdev_cache_read(zio))
3493a3f829aeSBill Moore 			return (ZIO_PIPELINE_CONTINUE);
3494e14bb325SJeff Bonwick 
3495e14bb325SJeff Bonwick 		if ((zio = vdev_queue_io(zio)) == NULL)
3496e14bb325SJeff Bonwick 			return (ZIO_PIPELINE_STOP);
3497e14bb325SJeff Bonwick 
3498e14bb325SJeff Bonwick 		if (!vdev_accessible(vd, zio)) {
3499be6fd75aSMatthew Ahrens 			zio->io_error = SET_ERROR(ENXIO);
3500e14bb325SJeff Bonwick 			zio_interrupt(zio);
3501e14bb325SJeff Bonwick 			return (ZIO_PIPELINE_STOP);
3502e14bb325SJeff Bonwick 		}
3503e14bb325SJeff Bonwick 	}
3504e14bb325SJeff Bonwick 
3505738f37bcSGeorge Wilson 	vd->vdev_ops->vdev_op_io_start(zio);
3506738f37bcSGeorge Wilson 	return (ZIO_PIPELINE_STOP);
3507fa9e4066Sahrens }
3508fa9e4066Sahrens 
3509e05725b1Sbonwick static int
3510fa9e4066Sahrens zio_vdev_io_done(zio_t *zio)
3511fa9e4066Sahrens {
3512e14bb325SJeff Bonwick 	vdev_t *vd = zio->io_vd;
3513e14bb325SJeff Bonwick 	vdev_ops_t *ops = vd ? vd->vdev_ops : &vdev_mirror_ops;
3514e14bb325SJeff Bonwick 	boolean_t unexpected_error = B_FALSE;
3515e05725b1Sbonwick 
3516d6e1c446SGeorge Wilson 	if (zio_wait_for_children(zio, ZIO_CHILD_VDEV_BIT, ZIO_WAIT_DONE)) {
3517e14bb325SJeff Bonwick 		return (ZIO_PIPELINE_STOP);
3518d6e1c446SGeorge Wilson 	}
3519fa9e4066Sahrens 
3520*084fd14fSBrian Behlendorf 	ASSERT(zio->io_type == ZIO_TYPE_READ ||
3521*084fd14fSBrian Behlendorf 	    zio->io_type == ZIO_TYPE_WRITE || zio->io_type == ZIO_TYPE_TRIM);
3522e14bb325SJeff Bonwick 
3523e14bb325SJeff Bonwick 	if (vd != NULL && vd->vdev_ops->vdev_op_leaf) {
3524e14bb325SJeff Bonwick 
3525e14bb325SJeff Bonwick 		vdev_queue_io_done(zio);
3526fa9e4066Sahrens 
3527e14bb325SJeff Bonwick 		if (zio->io_type == ZIO_TYPE_WRITE)
3528e14bb325SJeff Bonwick 			vdev_cache_write(zio);
3529e14bb325SJeff Bonwick 
3530e14bb325SJeff Bonwick 		if (zio_injection_enabled && zio->io_error == 0)
35318956713aSEric Schrock 			zio->io_error = zio_handle_device_injection(vd,
35328956713aSEric Schrock 			    zio, EIO);
3533e14bb325SJeff Bonwick 
3534e14bb325SJeff Bonwick 		if (zio_injection_enabled && zio->io_error == 0)
3535e14bb325SJeff Bonwick 			zio->io_error = zio_handle_label_injection(zio, EIO);
3536e14bb325SJeff Bonwick 
3537*084fd14fSBrian Behlendorf 		if (zio->io_error && zio->io_type != ZIO_TYPE_TRIM) {
3538e14bb325SJeff Bonwick 			if (!vdev_accessible(vd, zio)) {
3539be6fd75aSMatthew Ahrens 				zio->io_error = SET_ERROR(ENXIO);
3540e14bb325SJeff Bonwick 			} else {
3541e14bb325SJeff Bonwick 				unexpected_error = B_TRUE;
3542e14bb325SJeff Bonwick 			}
3543e14bb325SJeff Bonwick 		}
354451ece835Seschrock 	}
3545fa9e4066Sahrens 
3546e14bb325SJeff Bonwick 	ops->vdev_op_io_done(zio);
3547e14bb325SJeff Bonwick 
3548e14bb325SJeff Bonwick 	if (unexpected_error)
3549a3f829aeSBill Moore 		VERIFY(vdev_probe(vd, zio) == NULL);
3550e14bb325SJeff Bonwick 
3551e14bb325SJeff Bonwick 	return (ZIO_PIPELINE_CONTINUE);
3552fa9e4066Sahrens }
3553fa9e4066Sahrens 
3554a3874b8bSToomas Soome /*
3555a3874b8bSToomas Soome  * This function is used to change the priority of an existing zio that is
3556a3874b8bSToomas Soome  * currently in-flight. This is used by the arc to upgrade priority in the
3557a3874b8bSToomas Soome  * event that a demand read is made for a block that is currently queued
3558a3874b8bSToomas Soome  * as a scrub or async read IO. Otherwise, the high priority read request
3559a3874b8bSToomas Soome  * would end up having to wait for the lower priority IO.
3560a3874b8bSToomas Soome  */
3561a3874b8bSToomas Soome void
3562a3874b8bSToomas Soome zio_change_priority(zio_t *pio, zio_priority_t priority)
3563a3874b8bSToomas Soome {
3564a3874b8bSToomas Soome 	zio_t *cio, *cio_next;
3565a3874b8bSToomas Soome 	zio_link_t *zl = NULL;
3566a3874b8bSToomas Soome 
3567a3874b8bSToomas Soome 	ASSERT3U(priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
3568a3874b8bSToomas Soome 
3569a3874b8bSToomas Soome 	if (pio->io_vd != NULL && pio->io_vd->vdev_ops->vdev_op_leaf) {
3570a3874b8bSToomas Soome 		vdev_queue_change_io_priority(pio, priority);
3571a3874b8bSToomas Soome 	} else {
3572a3874b8bSToomas Soome 		pio->io_priority = priority;
3573a3874b8bSToomas Soome 	}
3574a3874b8bSToomas Soome 
3575a3874b8bSToomas Soome 	mutex_enter(&pio->io_lock);
3576a3874b8bSToomas Soome 	for (cio = zio_walk_children(pio, &zl); cio != NULL; cio = cio_next) {
3577a3874b8bSToomas Soome 		cio_next = zio_walk_children(pio, &zl);
3578a3874b8bSToomas Soome 		zio_change_priority(cio, priority);
3579a3874b8bSToomas Soome 	}
3580a3874b8bSToomas Soome 	mutex_exit(&pio->io_lock);
3581a3874b8bSToomas Soome }
3582a3874b8bSToomas Soome 
358322fe2c88SJonathan Adams /*
358422fe2c88SJonathan Adams  * For non-raidz ZIOs, we can just copy aside the bad data read from the
358522fe2c88SJonathan Adams  * disk, and use that to finish the checksum ereport later.
358622fe2c88SJonathan Adams  */
358722fe2c88SJonathan Adams static void
358822fe2c88SJonathan Adams zio_vsd_default_cksum_finish(zio_cksum_report_t *zcr,
3589eb633035STom Caputi     const abd_t *good_buf)
359022fe2c88SJonathan Adams {
359122fe2c88SJonathan Adams 	/* no processing needed */
359222fe2c88SJonathan Adams 	zfs_ereport_finish_checksum(zcr, good_buf, zcr->zcr_cbdata, B_FALSE);
359322fe2c88SJonathan Adams }
359422fe2c88SJonathan Adams 
359522fe2c88SJonathan Adams /*ARGSUSED*/
359622fe2c88SJonathan Adams void
359722fe2c88SJonathan Adams zio_vsd_default_cksum_report(zio_t *zio, zio_cksum_report_t *zcr, void *ignored)
359822fe2c88SJonathan Adams {
3599eb633035STom Caputi 	void *abd = abd_alloc_sametype(zio->io_abd, zio->io_size);
360022fe2c88SJonathan Adams 
3601eb633035STom Caputi 	abd_copy(abd, zio->io_abd, zio->io_size);
360222fe2c88SJonathan Adams 
360322fe2c88SJonathan Adams 	zcr->zcr_cbinfo = zio->io_size;
3604eb633035STom Caputi 	zcr->zcr_cbdata = abd;
360522fe2c88SJonathan Adams 	zcr->zcr_finish = zio_vsd_default_cksum_finish;
3606eb633035STom Caputi 	zcr->zcr_free = zio_abd_free;
360722fe2c88SJonathan Adams }
360822fe2c88SJonathan Adams 
3609e05725b1Sbonwick static int
3610fa9e4066Sahrens zio_vdev_io_assess(zio_t *zio)
3611fa9e4066Sahrens {
3612fa9e4066Sahrens 	vdev_t *vd = zio->io_vd;
3613e14bb325SJeff Bonwick 
3614d6e1c446SGeorge Wilson 	if (zio_wait_for_children(zio, ZIO_CHILD_VDEV_BIT, ZIO_WAIT_DONE)) {
3615e14bb325SJeff Bonwick 		return (ZIO_PIPELINE_STOP);
3616d6e1c446SGeorge Wilson 	}
3617e14bb325SJeff Bonwick 
3618e14bb325SJeff Bonwick 	if (vd == NULL && !(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
3619e14bb325SJeff Bonwick 		spa_config_exit(zio->io_spa, SCL_ZIO, zio);
3620e14bb325SJeff Bonwick 
3621e14bb325SJeff Bonwick 	if (zio->io_vsd != NULL) {
362222fe2c88SJonathan Adams 		zio->io_vsd_ops->vsd_free(zio);
3623e14bb325SJeff Bonwick 		zio->io_vsd = NULL;
3624ecc2d604Sbonwick 	}
3625ecc2d604Sbonwick 
3626e14bb325SJeff Bonwick 	if (zio_injection_enabled && zio->io_error == 0)
3627ea8dc4b6Seschrock 		zio->io_error = zio_handle_fault_injection(zio, EIO);
3628ea8dc4b6Seschrock 
3629fa9e4066Sahrens 	/*
3630fa9e4066Sahrens 	 * If the I/O failed, determine whether we should attempt to retry it.
363135a5a358SJonathan Adams 	 *
363235a5a358SJonathan Adams 	 * On retry, we cut in line in the issue queue, since we don't want
363335a5a358SJonathan Adams 	 * compression/checksumming/etc. work to prevent our (cheap) IO reissue.
3634fa9e4066Sahrens 	 */
3635e14bb325SJeff Bonwick 	if (zio->io_error && vd == NULL &&
3636e14bb325SJeff Bonwick 	    !(zio->io_flags & (ZIO_FLAG_DONT_RETRY | ZIO_FLAG_IO_RETRY))) {
3637e14bb325SJeff Bonwick 		ASSERT(!(zio->io_flags & ZIO_FLAG_DONT_QUEUE));	/* not a leaf */
3638e14bb325SJeff Bonwick 		ASSERT(!(zio->io_flags & ZIO_FLAG_IO_BYPASS));	/* not a leaf */
3639fa9e4066Sahrens 		zio->io_error = 0;
3640e14bb325SJeff Bonwick 		zio->io_flags |= ZIO_FLAG_IO_RETRY |
3641e14bb325SJeff Bonwick 		    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE;
3642b24ab676SJeff Bonwick 		zio->io_stage = ZIO_STAGE_VDEV_IO_START >> 1;
364335a5a358SJonathan Adams 		zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE,
364435a5a358SJonathan Adams 		    zio_requeue_io_start_cut_in_line);
3645e14bb325SJeff Bonwick 		return (ZIO_PIPELINE_STOP);
3646ea8dc4b6Seschrock 	}
3647fa9e4066Sahrens 
3648e14bb325SJeff Bonwick 	/*
3649e14bb325SJeff Bonwick 	 * If we got an error on a leaf device, convert it to ENXIO
3650e14bb325SJeff Bonwick 	 * if the device is not accessible at all.
3651e14bb325SJeff Bonwick 	 */
3652e14bb325SJeff Bonwick 	if (zio->io_error && vd != NULL && vd->vdev_ops->vdev_op_leaf &&
3653e14bb325SJeff Bonwick 	    !vdev_accessible(vd, zio))
3654be6fd75aSMatthew Ahrens 		zio->io_error = SET_ERROR(ENXIO);
3655e14bb325SJeff Bonwick 
3656e14bb325SJeff Bonwick 	/*
3657e14bb325SJeff Bonwick 	 * If we can't write to an interior vdev (mirror or RAID-Z),
3658e14bb325SJeff Bonwick 	 * set vdev_cant_write so that we stop trying to allocate from it.
3659e14bb325SJeff Bonwick 	 */
3660e14bb325SJeff Bonwick 	if (zio->io_error == ENXIO && zio->io_type == ZIO_TYPE_WRITE &&
36613b2aab18SMatthew Ahrens 	    vd != NULL && !vd->vdev_ops->vdev_op_leaf) {
3662e14bb325SJeff Bonwick 		vd->vdev_cant_write = B_TRUE;
36633b2aab18SMatthew Ahrens 	}
3664e14bb325SJeff Bonwick 
3665295438baSHans Rosenfeld 	/*
3666295438baSHans Rosenfeld 	 * If a cache flush returns ENOTSUP or ENOTTY, we know that no future
3667*084fd14fSBrian Behlendorf 	 * attempts will ever succeed. In this case we set a persistent
3668*084fd14fSBrian Behlendorf 	 * boolean flag so that we don't bother with it in the future.
3669295438baSHans Rosenfeld 	 */
3670295438baSHans Rosenfeld 	if ((zio->io_error == ENOTSUP || zio->io_error == ENOTTY) &&
3671295438baSHans Rosenfeld 	    zio->io_type == ZIO_TYPE_IOCTL &&
3672295438baSHans Rosenfeld 	    zio->io_cmd == DKIOCFLUSHWRITECACHE && vd != NULL)
3673295438baSHans Rosenfeld 		vd->vdev_nowritecache = B_TRUE;
3674295438baSHans Rosenfeld 
3675e14bb325SJeff Bonwick 	if (zio->io_error)
3676e14bb325SJeff Bonwick 		zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
3677e14bb325SJeff Bonwick 
367869962b56SMatthew Ahrens 	if (vd != NULL && vd->vdev_ops->vdev_op_leaf &&
367969962b56SMatthew Ahrens 	    zio->io_physdone != NULL) {
368069962b56SMatthew Ahrens 		ASSERT(!(zio->io_flags & ZIO_FLAG_DELEGATED));
368169962b56SMatthew Ahrens 		ASSERT(zio->io_child_type == ZIO_CHILD_VDEV);
368269962b56SMatthew Ahrens 		zio->io_physdone(zio->io_logical);
368369962b56SMatthew Ahrens 	}
368469962b56SMatthew Ahrens 
3685e05725b1Sbonwick 	return (ZIO_PIPELINE_CONTINUE);
3686fa9e4066Sahrens }
3687fa9e4066Sahrens 
3688fa9e4066Sahrens void
3689fa9e4066Sahrens zio_vdev_io_reissue(zio_t *zio)
3690fa9e4066Sahrens {
3691fa9e4066Sahrens 	ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
3692fa9e4066Sahrens 	ASSERT(zio->io_error == 0);
3693fa9e4066Sahrens 
3694b24ab676SJeff Bonwick 	zio->io_stage >>= 1;
3695fa9e4066Sahrens }
3696fa9e4066Sahrens 
3697fa9e4066Sahrens void
3698fa9e4066Sahrens zio_vdev_io_redone(zio_t *zio)
3699fa9e4066Sahrens {
3700fa9e4066Sahrens 	ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_DONE);
3701fa9e4066Sahrens 
3702b24ab676SJeff Bonwick 	zio->io_stage >>= 1;
3703fa9e4066Sahrens }
3704fa9e4066Sahrens 
3705fa9e4066Sahrens void
3706fa9e4066Sahrens zio_vdev_io_bypass(zio_t *zio)
3707fa9e4066Sahrens {
3708fa9e4066Sahrens 	ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
3709fa9e4066Sahrens 	ASSERT(zio->io_error == 0);
3710fa9e4066Sahrens 
3711fa9e4066Sahrens 	zio->io_flags |= ZIO_FLAG_IO_BYPASS;
3712b24ab676SJeff Bonwick 	zio->io_stage = ZIO_STAGE_VDEV_IO_ASSESS >> 1;
3713fa9e4066Sahrens }
3714fa9e4066Sahrens 
3715eb633035STom Caputi /*
3716eb633035STom Caputi  * ==========================================================================
3717eb633035STom Caputi  * Encrypt and store encryption parameters
3718eb633035STom Caputi  * ==========================================================================
3719eb633035STom Caputi  */
3720eb633035STom Caputi 
3721eb633035STom Caputi 
3722eb633035STom Caputi /*
3723eb633035STom Caputi  * This function is used for ZIO_STAGE_ENCRYPT. It is responsible for
3724eb633035STom Caputi  * managing the storage of encryption parameters and passing them to the
3725eb633035STom Caputi  * lower-level encryption functions.
3726eb633035STom Caputi  */
3727eb633035STom Caputi static int
3728eb633035STom Caputi zio_encrypt(zio_t *zio)
3729eb633035STom Caputi {
3730eb633035STom Caputi 	zio_prop_t *zp = &zio->io_prop;
3731eb633035STom Caputi 	spa_t *spa = zio->io_spa;
3732eb633035STom Caputi 	blkptr_t *bp = zio->io_bp;
3733eb633035STom Caputi 	uint64_t psize = BP_GET_PSIZE(bp);
3734eb633035STom Caputi 	uint64_t dsobj = zio->io_bookmark.zb_objset;
3735eb633035STom Caputi 	dmu_object_type_t ot = BP_GET_TYPE(bp);
3736eb633035STom Caputi 	void *enc_buf = NULL;
3737eb633035STom Caputi 	abd_t *eabd = NULL;
3738eb633035STom Caputi 	uint8_t salt[ZIO_DATA_SALT_LEN];
3739eb633035STom Caputi 	uint8_t iv[ZIO_DATA_IV_LEN];
3740eb633035STom Caputi 	uint8_t mac[ZIO_DATA_MAC_LEN];
3741eb633035STom Caputi 	boolean_t no_crypt = B_FALSE;
3742eb633035STom Caputi 
3743eb633035STom Caputi 	/* the root zio already encrypted the data */
3744eb633035STom Caputi 	if (zio->io_child_type == ZIO_CHILD_GANG)
3745eb633035STom Caputi 		return (ZIO_PIPELINE_CONTINUE);
3746eb633035STom Caputi 
3747eb633035STom Caputi 	/* only ZIL blocks are re-encrypted on rewrite */
3748eb633035STom Caputi 	if (!IO_IS_ALLOCATING(zio) && ot != DMU_OT_INTENT_LOG)
3749eb633035STom Caputi 		return (ZIO_PIPELINE_CONTINUE);
3750eb633035STom Caputi 
3751eb633035STom Caputi 	if (!(zp->zp_encrypt || BP_IS_ENCRYPTED(bp))) {
3752eb633035STom Caputi 		BP_SET_CRYPT(bp, B_FALSE);
3753eb633035STom Caputi 		return (ZIO_PIPELINE_CONTINUE);
3754eb633035STom Caputi 	}
3755eb633035STom Caputi 
3756eb633035STom Caputi 	/* if we are doing raw encryption set the provided encryption params */
3757eb633035STom Caputi 	if (zio->io_flags & ZIO_FLAG_RAW_ENCRYPT) {
3758eb633035STom Caputi 		ASSERT0(BP_GET_LEVEL(bp));
3759eb633035STom Caputi 		BP_SET_CRYPT(bp, B_TRUE);
3760eb633035STom Caputi 		BP_SET_BYTEORDER(bp, zp->zp_byteorder);
3761eb633035STom Caputi 		if (ot != DMU_OT_OBJSET)
3762eb633035STom Caputi 			zio_crypt_encode_mac_bp(bp, zp->zp_mac);
3763eb633035STom Caputi 
3764eb633035STom Caputi 		/* dnode blocks must be written out in the provided byteorder */
3765eb633035STom Caputi 		if (zp->zp_byteorder != ZFS_HOST_BYTEORDER &&
3766eb633035STom Caputi 		    ot == DMU_OT_DNODE) {
3767eb633035STom Caputi 			void *bswap_buf = zio_buf_alloc(psize);
3768eb633035STom Caputi 			abd_t *babd = abd_get_from_buf(bswap_buf, psize);
3769eb633035STom Caputi 
3770eb633035STom Caputi 			ASSERT3U(BP_GET_COMPRESS(bp), ==, ZIO_COMPRESS_OFF);
3771eb633035STom Caputi 			abd_copy_to_buf(bswap_buf, zio->io_abd, psize);
3772eb633035STom Caputi 			dmu_ot_byteswap[DMU_OT_BYTESWAP(ot)].ob_func(bswap_buf,
3773eb633035STom Caputi 			    psize);
3774eb633035STom Caputi 
3775eb633035STom Caputi 			abd_take_ownership_of_buf(babd, B_TRUE);
3776eb633035STom Caputi 			zio_push_transform(zio, babd, psize, psize, NULL);
3777eb633035STom Caputi 		}
3778eb633035STom Caputi 
3779eb633035STom Caputi 		if (DMU_OT_IS_ENCRYPTED(ot))
3780eb633035STom Caputi 			zio_crypt_encode_params_bp(bp, zp->zp_salt, zp->zp_iv);
3781eb633035STom Caputi 		return (ZIO_PIPELINE_CONTINUE);
3782eb633035STom Caputi 	}
3783eb633035STom Caputi 
3784eb633035STom Caputi 	/* indirect blocks only maintain a cksum of the lower level MACs */
3785eb633035STom Caputi 	if (BP_GET_LEVEL(bp) > 0) {
3786eb633035STom Caputi 		BP_SET_CRYPT(bp, B_TRUE);
3787eb633035STom Caputi 		VERIFY0(zio_crypt_do_indirect_mac_checksum_abd(B_TRUE,
3788eb633035STom Caputi 		    zio->io_orig_abd, BP_GET_LSIZE(bp), BP_SHOULD_BYTESWAP(bp),
3789eb633035STom Caputi 		    mac));
3790eb633035STom Caputi 		zio_crypt_encode_mac_bp(bp, mac);
3791eb633035STom Caputi 		return (ZIO_PIPELINE_CONTINUE);
3792eb633035STom Caputi 	}
3793eb633035STom Caputi 
3794eb633035STom Caputi 	/*
3795eb633035STom Caputi 	 * Objset blocks are a special case since they have 2 256-bit MACs
3796eb633035STom Caputi 	 * embedded within them.
3797eb633035STom Caputi 	 */
3798eb633035STom Caputi 	if (ot == DMU_OT_OBJSET) {
3799eb633035STom Caputi 		ASSERT0(DMU_OT_IS_ENCRYPTED(ot));
3800eb633035STom Caputi 		ASSERT3U(BP_GET_COMPRESS(bp), ==, ZIO_COMPRESS_OFF);
3801eb633035STom Caputi 		BP_SET_CRYPT(bp, B_TRUE);
3802eb633035STom Caputi 		VERIFY0(spa_do_crypt_objset_mac_abd(B_TRUE, spa, dsobj,
3803eb633035STom Caputi 		    zio->io_abd, psize, BP_SHOULD_BYTESWAP(bp)));
3804eb633035STom Caputi 		return (ZIO_PIPELINE_CONTINUE);
3805eb633035STom Caputi 	}
3806eb633035STom Caputi 
3807eb633035STom Caputi 	/* unencrypted object types are only authenticated with a MAC */
3808eb633035STom Caputi 	if (!DMU_OT_IS_ENCRYPTED(ot)) {
3809eb633035STom Caputi 		BP_SET_CRYPT(bp, B_TRUE);
3810eb633035STom Caputi 		VERIFY0(spa_do_crypt_mac_abd(B_TRUE, spa, dsobj,
3811eb633035STom Caputi 		    zio->io_abd, psize, mac));
3812eb633035STom Caputi 		zio_crypt_encode_mac_bp(bp, mac);
3813eb633035STom Caputi 		return (ZIO_PIPELINE_CONTINUE);
3814eb633035STom Caputi 	}
3815eb633035STom Caputi 
3816eb633035STom Caputi 	/*
3817eb633035STom Caputi 	 * Later passes of sync-to-convergence may decide to rewrite data
3818eb633035STom Caputi 	 * in place to avoid more disk reallocations. This presents a problem
3819eb633035STom Caputi 	 * for encryption because this consitutes rewriting the new data with
3820eb633035STom Caputi 	 * the same encryption key and IV. However, this only applies to blocks
3821eb633035STom Caputi 	 * in the MOS (particularly the spacemaps) and we do not encrypt the
3822eb633035STom Caputi 	 * MOS. We assert that the zio is allocating or an intent log write
3823eb633035STom Caputi 	 * to enforce this.
3824eb633035STom Caputi 	 */
3825eb633035STom Caputi 	ASSERT(IO_IS_ALLOCATING(zio) || ot == DMU_OT_INTENT_LOG);
3826eb633035STom Caputi 	ASSERT(BP_GET_LEVEL(bp) == 0 || ot == DMU_OT_INTENT_LOG);
3827eb633035STom Caputi 	ASSERT(spa_feature_is_active(spa, SPA_FEATURE_ENCRYPTION));
3828eb633035STom Caputi 	ASSERT3U(psize, !=, 0);
3829eb633035STom Caputi 
3830eb633035STom Caputi 	enc_buf = zio_buf_alloc(psize);
3831eb633035STom Caputi 	eabd = abd_get_from_buf(enc_buf, psize);
3832eb633035STom Caputi 	abd_take_ownership_of_buf(eabd, B_TRUE);
3833eb633035STom Caputi 
3834eb633035STom Caputi 	/*
3835eb633035STom Caputi 	 * For an explanation of what encryption parameters are stored
3836eb633035STom Caputi 	 * where, see the block comment in zio_crypt.c.
3837eb633035STom Caputi 	 */
3838eb633035STom Caputi 	if (ot == DMU_OT_INTENT_LOG) {
3839eb633035STom Caputi 		zio_crypt_decode_params_bp(bp, salt, iv);
3840eb633035STom Caputi 	} else {
3841eb633035STom Caputi 		BP_SET_CRYPT(bp, B_TRUE);
3842eb633035STom Caputi 	}
3843eb633035STom Caputi 
3844eb633035STom Caputi 	/* Perform the encryption. This should not fail */
3845eb633035STom Caputi 	VERIFY0(spa_do_crypt_abd(B_TRUE, spa, &zio->io_bookmark,
3846eb633035STom Caputi 	    BP_GET_TYPE(bp), BP_GET_DEDUP(bp), BP_SHOULD_BYTESWAP(bp),
3847eb633035STom Caputi 	    salt, iv, mac, psize, zio->io_abd, eabd, &no_crypt));
3848eb633035STom Caputi 
3849eb633035STom Caputi 	/* encode encryption metadata into the bp */
3850eb633035STom Caputi 	if (ot == DMU_OT_INTENT_LOG) {
3851eb633035STom Caputi 		/*
3852eb633035STom Caputi 		 * ZIL blocks store the MAC in the embedded checksum, so the
3853eb633035STom Caputi 		 * transform must always be applied.
3854eb633035STom Caputi 		 */
3855eb633035STom Caputi 		zio_crypt_encode_mac_zil(enc_buf, mac);
3856eb633035STom Caputi 		zio_push_transform(zio, eabd, psize, psize, NULL);
3857eb633035STom Caputi 	} else {
3858eb633035STom Caputi 		BP_SET_CRYPT(bp, B_TRUE);
3859eb633035STom Caputi 		zio_crypt_encode_params_bp(bp, salt, iv);
3860eb633035STom Caputi 		zio_crypt_encode_mac_bp(bp, mac);
3861eb633035STom Caputi 
3862eb633035STom Caputi 		if (no_crypt) {
3863eb633035STom Caputi 			ASSERT3U(ot, ==, DMU_OT_DNODE);
3864eb633035STom Caputi 			abd_free(eabd);
3865eb633035STom Caputi 		} else {
3866eb633035STom Caputi 			zio_push_transform(zio, eabd, psize, psize, NULL);
3867eb633035STom Caputi 		}
3868eb633035STom Caputi 	}
3869eb633035STom Caputi 
3870eb633035STom Caputi 	return (ZIO_PIPELINE_CONTINUE);
3871eb633035STom Caputi }
3872eb633035STom Caputi 
3873fa9e4066Sahrens /*
3874fa9e4066Sahrens  * ==========================================================================
3875fa9e4066Sahrens  * Generate and verify checksums
3876fa9e4066Sahrens  * ==========================================================================
3877fa9e4066Sahrens  */
3878e05725b1Sbonwick static int
3879fa9e4066Sahrens zio_checksum_generate(zio_t *zio)
3880fa9e4066Sahrens {
3881fa9e4066Sahrens 	blkptr_t *bp = zio->io_bp;
3882e14bb325SJeff Bonwick 	enum zio_checksum checksum;
3883fa9e4066Sahrens 
3884e14bb325SJeff Bonwick 	if (bp == NULL) {
3885e14bb325SJeff Bonwick 		/*
3886e14bb325SJeff Bonwick 		 * This is zio_write_phys().
3887e14bb325SJeff Bonwick 		 * We're either generating a label checksum, or none at all.
3888e14bb325SJeff Bonwick 		 */
3889e14bb325SJeff Bonwick 		checksum = zio->io_prop.zp_checksum;
3890e14bb325SJeff Bonwick 
3891e14bb325SJeff Bonwick 		if (checksum == ZIO_CHECKSUM_OFF)
3892e14bb325SJeff Bonwick 			return (ZIO_PIPELINE_CONTINUE);
3893fa9e4066Sahrens 
3894e14bb325SJeff Bonwick 		ASSERT(checksum == ZIO_CHECKSUM_LABEL);
3895e14bb325SJeff Bonwick 	} else {
3896e14bb325SJeff Bonwick 		if (BP_IS_GANG(bp) && zio->io_child_type == ZIO_CHILD_GANG) {
3897e14bb325SJeff Bonwick 			ASSERT(!IO_IS_ALLOCATING(zio));
3898e14bb325SJeff Bonwick 			checksum = ZIO_CHECKSUM_GANG_HEADER;
3899e14bb325SJeff Bonwick 		} else {
3900e14bb325SJeff Bonwick 			checksum = BP_GET_CHECKSUM(bp);
3901e14bb325SJeff Bonwick 		}
3902e14bb325SJeff Bonwick 	}
3903fa9e4066Sahrens 
3904770499e1SDan Kimmel 	zio_checksum_compute(zio, checksum, zio->io_abd, zio->io_size);
3905fa9e4066Sahrens 
3906e05725b1Sbonwick 	return (ZIO_PIPELINE_CONTINUE);
3907fa9e4066Sahrens }
3908fa9e4066Sahrens 
3909e05725b1Sbonwick static int
3910e14bb325SJeff Bonwick zio_checksum_verify(zio_t *zio)
3911fa9e4066Sahrens {
391222fe2c88SJonathan Adams 	zio_bad_cksum_t info;
3913e14bb325SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
3914e14bb325SJeff Bonwick 	int error;
3915fa9e4066Sahrens 
3916b24ab676SJeff Bonwick 	ASSERT(zio->io_vd != NULL);
3917b24ab676SJeff Bonwick 
3918e14bb325SJeff Bonwick 	if (bp == NULL) {
3919e14bb325SJeff Bonwick 		/*
3920e14bb325SJeff Bonwick 		 * This is zio_read_phys().
3921e14bb325SJeff Bonwick 		 * We're either verifying a label checksum, or nothing at all.
3922e14bb325SJeff Bonwick 		 */
3923e14bb325SJeff Bonwick 		if (zio->io_prop.zp_checksum == ZIO_CHECKSUM_OFF)
3924e14bb325SJeff Bonwick 			return (ZIO_PIPELINE_CONTINUE);
3925fa9e4066Sahrens 
3926e14bb325SJeff Bonwick 		ASSERT(zio->io_prop.zp_checksum == ZIO_CHECKSUM_LABEL);
3927e14bb325SJeff Bonwick 	}
3928fa9e4066Sahrens 
392922fe2c88SJonathan Adams 	if ((error = zio_checksum_error(zio, &info)) != 0) {
3930e14bb325SJeff Bonwick 		zio->io_error = error;
3931373dc1cfSMatthew Ahrens 		if (error == ECKSUM &&
3932373dc1cfSMatthew Ahrens 		    !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
393322fe2c88SJonathan Adams 			zfs_ereport_start_checksum(zio->io_spa,
3934eb633035STom Caputi 			    zio->io_vd, &zio->io_bookmark, zio,
3935eb633035STom Caputi 			    zio->io_offset, zio->io_size, NULL, &info);
3936e14bb325SJeff Bonwick 		}
3937fa9e4066Sahrens 	}
3938fa9e4066Sahrens 
3939e05725b1Sbonwick 	return (ZIO_PIPELINE_CONTINUE);
3940fa9e4066Sahrens }
3941fa9e4066Sahrens 
3942fa9e4066Sahrens /*
3943fa9e4066Sahrens  * Called by RAID-Z to ensure we don't compute the checksum twice.
3944fa9e4066Sahrens  */
3945fa9e4066Sahrens void
3946fa9e4066Sahrens zio_checksum_verified(zio_t *zio)
3947fa9e4066Sahrens {
3948b24ab676SJeff Bonwick 	zio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY;
3949fa9e4066Sahrens }
3950fa9e4066Sahrens 
3951fa9e4066Sahrens /*
3952e14bb325SJeff Bonwick  * ==========================================================================
3953e14bb325SJeff Bonwick  * Error rank.  Error are ranked in the order 0, ENXIO, ECKSUM, EIO, other.
39545d7b4d43SMatthew Ahrens  * An error of 0 indicates success.  ENXIO indicates whole-device failure,
3955e14bb325SJeff Bonwick  * which may be transient (e.g. unplugged) or permament.  ECKSUM and EIO
3956e14bb325SJeff Bonwick  * indicate errors that are specific to one I/O, and most likely permanent.
3957e14bb325SJeff Bonwick  * Any other error is presumed to be worse because we weren't expecting it.
3958e14bb325SJeff Bonwick  * ==========================================================================
3959fa9e4066Sahrens  */
3960e14bb325SJeff Bonwick int
3961e14bb325SJeff Bonwick zio_worst_error(int e1, int e2)
3962fa9e4066Sahrens {
3963e14bb325SJeff Bonwick 	static int zio_error_rank[] = { 0, ENXIO, ECKSUM, EIO };
3964e14bb325SJeff Bonwick 	int r1, r2;
3965e14bb325SJeff Bonwick 
3966e14bb325SJeff Bonwick 	for (r1 = 0; r1 < sizeof (zio_error_rank) / sizeof (int); r1++)
3967e14bb325SJeff Bonwick 		if (e1 == zio_error_rank[r1])
3968e14bb325SJeff Bonwick 			break;
3969e14bb325SJeff Bonwick 
3970e14bb325SJeff Bonwick 	for (r2 = 0; r2 < sizeof (zio_error_rank) / sizeof (int); r2++)
3971e14bb325SJeff Bonwick 		if (e2 == zio_error_rank[r2])
3972e14bb325SJeff Bonwick 			break;
397344cd46caSbillm 
3974e14bb325SJeff Bonwick 	return (r1 > r2 ? e1 : e2);
3975fa9e4066Sahrens }
3976fa9e4066Sahrens 
3977fa9e4066Sahrens /*
3978fa9e4066Sahrens  * ==========================================================================
3979e14bb325SJeff Bonwick  * I/O completion
3980fa9e4066Sahrens  * ==========================================================================
3981fa9e4066Sahrens  */
3982e14bb325SJeff Bonwick static int
3983e14bb325SJeff Bonwick zio_ready(zio_t *zio)
3984fa9e4066Sahrens {
3985e14bb325SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
3986a3f829aeSBill Moore 	zio_t *pio, *pio_next;
39870f7643c7SGeorge Wilson 	zio_link_t *zl = NULL;
3988fa9e4066Sahrens 
3989d6e1c446SGeorge Wilson 	if (zio_wait_for_children(zio, ZIO_CHILD_GANG_BIT | ZIO_CHILD_DDT_BIT,
3990d6e1c446SGeorge Wilson 	    ZIO_WAIT_READY)) {
3991f5383399SBill Moore 		return (ZIO_PIPELINE_STOP);
3992d6e1c446SGeorge Wilson 	}
3993fa9e4066Sahrens 
3994f5383399SBill Moore 	if (zio->io_ready) {
3995e14bb325SJeff Bonwick 		ASSERT(IO_IS_ALLOCATING(zio));
399680901aeaSGeorge Wilson 		ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp) ||
399780901aeaSGeorge Wilson 		    (zio->io_flags & ZIO_FLAG_NOPWRITE));
3998e14bb325SJeff Bonwick 		ASSERT(zio->io_children[ZIO_CHILD_GANG][ZIO_WAIT_READY] == 0);
3999fa9e4066Sahrens 
4000e14bb325SJeff Bonwick 		zio->io_ready(zio);
4001e14bb325SJeff Bonwick 	}
4002fa9e4066Sahrens 
4003e14bb325SJeff Bonwick 	if (bp != NULL && bp != &zio->io_bp_copy)
4004e14bb325SJeff Bonwick 		zio->io_bp_copy = *bp;
4005fa9e4066Sahrens 
40060f7643c7SGeorge Wilson 	if (zio->io_error != 0) {
4007e14bb325SJeff Bonwick 		zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
4008fa9e4066Sahrens 
40090f7643c7SGeorge Wilson 		if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
40100f7643c7SGeorge Wilson 			ASSERT(IO_IS_ALLOCATING(zio));
40110f7643c7SGeorge Wilson 			ASSERT(zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
4012663207adSDon Brady 			ASSERT(zio->io_metaslab_class != NULL);
4013663207adSDon Brady 
40140f7643c7SGeorge Wilson 			/*
40150f7643c7SGeorge Wilson 			 * We were unable to allocate anything, unreserve and
40160f7643c7SGeorge Wilson 			 * issue the next I/O to allocate.
40170f7643c7SGeorge Wilson 			 */
40180f7643c7SGeorge Wilson 			metaslab_class_throttle_unreserve(
4019663207adSDon Brady 			    zio->io_metaslab_class, zio->io_prop.zp_copies,
4020663207adSDon Brady 			    zio->io_allocator, zio);
4021f78cdc34SPaul Dagnelie 			zio_allocate_dispatch(zio->io_spa, zio->io_allocator);
40220f7643c7SGeorge Wilson 		}
40230f7643c7SGeorge Wilson 	}
40240f7643c7SGeorge Wilson 
4025a3f829aeSBill Moore 	mutex_enter(&zio->io_lock);
4026a3f829aeSBill Moore 	zio->io_state[ZIO_WAIT_READY] = 1;
40270f7643c7SGeorge Wilson 	pio = zio_walk_parents(zio, &zl);
4028a3f829aeSBill Moore 	mutex_exit(&zio->io_lock);
4029a3f829aeSBill Moore 
4030a3f829aeSBill Moore 	/*
4031a3f829aeSBill Moore 	 * As we notify zio's parents, new parents could be added.
4032a3f829aeSBill Moore 	 * New parents go to the head of zio's io_parent_list, however,
4033a3f829aeSBill Moore 	 * so we will (correctly) not notify them.  The remainder of zio's
4034a3f829aeSBill Moore 	 * io_parent_list, from 'pio_next' onward, cannot change because
4035a3f829aeSBill Moore 	 * all parents must wait for us to be done before they can be done.
4036a3f829aeSBill Moore 	 */
4037a3f829aeSBill Moore 	for (; pio != NULL; pio = pio_next) {
40380f7643c7SGeorge Wilson 		pio_next = zio_walk_parents(zio, &zl);
4039e14bb325SJeff Bonwick 		zio_notify_parent(pio, zio, ZIO_WAIT_READY);
4040a3f829aeSBill Moore 	}
4041fa9e4066Sahrens 
4042b24ab676SJeff Bonwick 	if (zio->io_flags & ZIO_FLAG_NODATA) {
4043b24ab676SJeff Bonwick 		if (BP_IS_GANG(bp)) {
4044b24ab676SJeff Bonwick 			zio->io_flags &= ~ZIO_FLAG_NODATA;
4045b24ab676SJeff Bonwick 		} else {
4046770499e1SDan Kimmel 			ASSERT((uintptr_t)zio->io_abd < SPA_MAXBLOCKSIZE);
4047b24ab676SJeff Bonwick 			zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES;
4048b24ab676SJeff Bonwick 		}
4049b24ab676SJeff Bonwick 	}
4050b24ab676SJeff Bonwick 
4051a33cae98STim Haley 	if (zio_injection_enabled &&
4052a33cae98STim Haley 	    zio->io_spa->spa_syncing_txg == zio->io_txg)
4053a33cae98STim Haley 		zio_handle_ignored_writes(zio);
4054a33cae98STim Haley 
4055e14bb325SJeff Bonwick 	return (ZIO_PIPELINE_CONTINUE);
4056fa9e4066Sahrens }
4057fa9e4066Sahrens 
40580f7643c7SGeorge Wilson /*
40590f7643c7SGeorge Wilson  * Update the allocation throttle accounting.
40600f7643c7SGeorge Wilson  */
40610f7643c7SGeorge Wilson static void
40620f7643c7SGeorge Wilson zio_dva_throttle_done(zio_t *zio)
40630f7643c7SGeorge Wilson {
40640f7643c7SGeorge Wilson 	zio_t *lio = zio->io_logical;
40650f7643c7SGeorge Wilson 	zio_t *pio = zio_unique_parent(zio);
40660f7643c7SGeorge Wilson 	vdev_t *vd = zio->io_vd;
40670f7643c7SGeorge Wilson 	int flags = METASLAB_ASYNC_ALLOC;
40680f7643c7SGeorge Wilson 
40690f7643c7SGeorge Wilson 	ASSERT3P(zio->io_bp, !=, NULL);
40700f7643c7SGeorge Wilson 	ASSERT3U(zio->io_type, ==, ZIO_TYPE_WRITE);
40710f7643c7SGeorge Wilson 	ASSERT3U(zio->io_priority, ==, ZIO_PRIORITY_ASYNC_WRITE);
40720f7643c7SGeorge Wilson 	ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_VDEV);
40730f7643c7SGeorge Wilson 	ASSERT(vd != NULL);
40740f7643c7SGeorge Wilson 	ASSERT3P(vd, ==, vd->vdev_top);
40750f7643c7SGeorge Wilson 	ASSERT(!(zio->io_flags & (ZIO_FLAG_IO_REPAIR | ZIO_FLAG_IO_RETRY)));
40760f7643c7SGeorge Wilson 	ASSERT(zio->io_flags & ZIO_FLAG_IO_ALLOCATING);
40770f7643c7SGeorge Wilson 	ASSERT(!(lio->io_flags & ZIO_FLAG_IO_REWRITE));
40780f7643c7SGeorge Wilson 	ASSERT(!(lio->io_orig_flags & ZIO_FLAG_NODATA));
40790f7643c7SGeorge Wilson 
40800f7643c7SGeorge Wilson 	/*
40810f7643c7SGeorge Wilson 	 * Parents of gang children can have two flavors -- ones that
40820f7643c7SGeorge Wilson 	 * allocated the gang header (will have ZIO_FLAG_IO_REWRITE set)
40830f7643c7SGeorge Wilson 	 * and ones that allocated the constituent blocks. The allocation
40840f7643c7SGeorge Wilson 	 * throttle needs to know the allocating parent zio so we must find
40850f7643c7SGeorge Wilson 	 * it here.
40860f7643c7SGeorge Wilson 	 */
40870f7643c7SGeorge Wilson 	if (pio->io_child_type == ZIO_CHILD_GANG) {
40880f7643c7SGeorge Wilson 		/*
40890f7643c7SGeorge Wilson 		 * If our parent is a rewrite gang child then our grandparent
40900f7643c7SGeorge Wilson 		 * would have been the one that performed the allocation.
40910f7643c7SGeorge Wilson 		 */
40920f7643c7SGeorge Wilson 		if (pio->io_flags & ZIO_FLAG_IO_REWRITE)
40930f7643c7SGeorge Wilson 			pio = zio_unique_parent(pio);
40940f7643c7SGeorge Wilson 		flags |= METASLAB_GANG_CHILD;
40950f7643c7SGeorge Wilson 	}
40960f7643c7SGeorge Wilson 
40970f7643c7SGeorge Wilson 	ASSERT(IO_IS_ALLOCATING(pio));
40980f7643c7SGeorge Wilson 	ASSERT3P(zio, !=, zio->io_logical);
40990f7643c7SGeorge Wilson 	ASSERT(zio->io_logical != NULL);
41000f7643c7SGeorge Wilson 	ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REPAIR));
41010f7643c7SGeorge Wilson 	ASSERT0(zio->io_flags & ZIO_FLAG_NOPWRITE);
4102663207adSDon Brady 	ASSERT(zio->io_metaslab_class != NULL);
41030f7643c7SGeorge Wilson 
41040f7643c7SGeorge Wilson 	mutex_enter(&pio->io_lock);
4105f78cdc34SPaul Dagnelie 	metaslab_group_alloc_decrement(zio->io_spa, vd->vdev_id, pio, flags,
4106f78cdc34SPaul Dagnelie 	    pio->io_allocator, B_TRUE);
41070f7643c7SGeorge Wilson 	mutex_exit(&pio->io_lock);
41080f7643c7SGeorge Wilson 
4109663207adSDon Brady 	metaslab_class_throttle_unreserve(zio->io_metaslab_class, 1,
4110663207adSDon Brady 	    pio->io_allocator, pio);
41110f7643c7SGeorge Wilson 
41120f7643c7SGeorge Wilson 	/*
41130f7643c7SGeorge Wilson 	 * Call into the pipeline to see if there is more work that
41140f7643c7SGeorge Wilson 	 * needs to be done. If there is work to be done it will be
41150f7643c7SGeorge Wilson 	 * dispatched to another taskq thread.
41160f7643c7SGeorge Wilson 	 */
4117f78cdc34SPaul Dagnelie 	zio_allocate_dispatch(zio->io_spa, pio->io_allocator);
41180f7643c7SGeorge Wilson }
41190f7643c7SGeorge Wilson 
4120e14bb325SJeff Bonwick static int
4121e14bb325SJeff Bonwick zio_done(zio_t *zio)
4122d63d470bSgw {
4123e14bb325SJeff Bonwick 	spa_t *spa = zio->io_spa;
4124e14bb325SJeff Bonwick 	zio_t *lio = zio->io_logical;
4125e14bb325SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
4126e14bb325SJeff Bonwick 	vdev_t *vd = zio->io_vd;
4127e14bb325SJeff Bonwick 	uint64_t psize = zio->io_size;
4128a3f829aeSBill Moore 	zio_t *pio, *pio_next;
41290f7643c7SGeorge Wilson 	zio_link_t *zl = NULL;
4130d63d470bSgw 
4131e14bb325SJeff Bonwick 	/*
4132f5383399SBill Moore 	 * If our children haven't all completed,
4133e14bb325SJeff Bonwick 	 * wait for them and then repeat this pipeline stage.
4134e14bb325SJeff Bonwick 	 */
4135d6e1c446SGeorge Wilson 	if (zio_wait_for_children(zio, ZIO_CHILD_ALL_BITS, ZIO_WAIT_DONE)) {
4136e14bb325SJeff Bonwick 		return (ZIO_PIPELINE_STOP);
4137d6e1c446SGeorge Wilson 	}
4138d63d470bSgw 
41390f7643c7SGeorge Wilson 	/*
41400f7643c7SGeorge Wilson 	 * If the allocation throttle is enabled, then update the accounting.
41410f7643c7SGeorge Wilson 	 * We only track child I/Os that are part of an allocating async
41420f7643c7SGeorge Wilson 	 * write. We must do this since the allocation is performed
41430f7643c7SGeorge Wilson 	 * by the logical I/O but the actual write is done by child I/Os.
41440f7643c7SGeorge Wilson 	 */
41450f7643c7SGeorge Wilson 	if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING &&
41460f7643c7SGeorge Wilson 	    zio->io_child_type == ZIO_CHILD_VDEV) {
4147663207adSDon Brady 		ASSERT(zio->io_metaslab_class != NULL);
4148663207adSDon Brady 		ASSERT(zio->io_metaslab_class->mc_alloc_throttle_enabled);
41490f7643c7SGeorge Wilson 		zio_dva_throttle_done(zio);
41500f7643c7SGeorge Wilson 	}
41510f7643c7SGeorge Wilson 
41520f7643c7SGeorge Wilson 	/*
41530f7643c7SGeorge Wilson 	 * If the allocation throttle is enabled, verify that
41540f7643c7SGeorge Wilson 	 * we have decremented the refcounts for every I/O that was throttled.
41550f7643c7SGeorge Wilson 	 */
41560f7643c7SGeorge Wilson 	if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
41570f7643c7SGeorge Wilson 		ASSERT(zio->io_type == ZIO_TYPE_WRITE);
41580f7643c7SGeorge Wilson 		ASSERT(zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
41590f7643c7SGeorge Wilson 		ASSERT(bp != NULL);
4160663207adSDon Brady 
4161f78cdc34SPaul Dagnelie 		metaslab_group_alloc_verify(spa, zio->io_bp, zio,
4162f78cdc34SPaul Dagnelie 		    zio->io_allocator);
4163e914ace2STim Schumacher 		VERIFY(zfs_refcount_not_held(
4164663207adSDon Brady 		    &zio->io_metaslab_class->mc_alloc_slots[zio->io_allocator],
4165663207adSDon Brady 		    zio));
41660f7643c7SGeorge Wilson 	}
41670f7643c7SGeorge Wilson 
4168e14bb325SJeff Bonwick 	for (int c = 0; c < ZIO_CHILD_TYPES; c++)
4169e14bb325SJeff Bonwick 		for (int w = 0; w < ZIO_WAIT_TYPES; w++)
4170e14bb325SJeff Bonwick 			ASSERT(zio->io_children[c][w] == 0);
4171e14bb325SJeff Bonwick 
41725d7b4d43SMatthew Ahrens 	if (bp != NULL && !BP_IS_EMBEDDED(bp)) {
4173e14bb325SJeff Bonwick 		ASSERT(bp->blk_pad[0] == 0);
4174e14bb325SJeff Bonwick 		ASSERT(bp->blk_pad[1] == 0);
4175e14bb325SJeff Bonwick 		ASSERT(bcmp(bp, &zio->io_bp_copy, sizeof (blkptr_t)) == 0 ||
4176a3f829aeSBill Moore 		    (bp == zio_unique_parent(zio)->io_bp));
4177e14bb325SJeff Bonwick 		if (zio->io_type == ZIO_TYPE_WRITE && !BP_IS_HOLE(bp) &&
4178b24ab676SJeff Bonwick 		    zio->io_bp_override == NULL &&
4179e14bb325SJeff Bonwick 		    !(zio->io_flags & ZIO_FLAG_IO_REPAIR)) {
4180b24ab676SJeff Bonwick 			ASSERT3U(zio->io_prop.zp_copies, <=, BP_GET_NDVAS(bp));
4181e14bb325SJeff Bonwick 			ASSERT(BP_COUNT_GANG(bp) == 0 ||
4182e14bb325SJeff Bonwick 			    (BP_COUNT_GANG(bp) == BP_GET_NDVAS(bp)));
4183e14bb325SJeff Bonwick 		}
418480901aeaSGeorge Wilson 		if (zio->io_flags & ZIO_FLAG_NOPWRITE)
418580901aeaSGeorge Wilson 			VERIFY(BP_EQUAL(bp, &zio->io_bp_orig));
4186e14bb325SJeff Bonwick 	}
4187fa9e4066Sahrens 
4188e14bb325SJeff Bonwick 	/*
4189b24ab676SJeff Bonwick 	 * If there were child vdev/gang/ddt errors, they apply to us now.
4190e14bb325SJeff Bonwick 	 */
4191e14bb325SJeff Bonwick 	zio_inherit_child_errors(zio, ZIO_CHILD_VDEV);
4192e14bb325SJeff Bonwick 	zio_inherit_child_errors(zio, ZIO_CHILD_GANG);
4193b24ab676SJeff Bonwick 	zio_inherit_child_errors(zio, ZIO_CHILD_DDT);
4194b24ab676SJeff Bonwick 
4195b24ab676SJeff Bonwick 	/*
4196b24ab676SJeff Bonwick 	 * If the I/O on the transformed data was successful, generate any
4197b24ab676SJeff Bonwick 	 * checksum reports now while we still have the transformed data.
4198b24ab676SJeff Bonwick 	 */
4199b24ab676SJeff Bonwick 	if (zio->io_error == 0) {
4200b24ab676SJeff Bonwick 		while (zio->io_cksum_report != NULL) {
4201b24ab676SJeff Bonwick 			zio_cksum_report_t *zcr = zio->io_cksum_report;
4202b24ab676SJeff Bonwick 			uint64_t align = zcr->zcr_align;
4203b24ab676SJeff Bonwick 			uint64_t asize = P2ROUNDUP(psize, align);
4204770499e1SDan Kimmel 			abd_t *adata = zio->io_abd;
4205b24ab676SJeff Bonwick 
4206b24ab676SJeff Bonwick 			if (asize != psize) {
4207eb633035STom Caputi 				adata = abd_alloc(asize, B_TRUE);
4208770499e1SDan Kimmel 				abd_copy(adata, zio->io_abd, psize);
4209770499e1SDan Kimmel 				abd_zero_off(adata, psize, asize - psize);
4210b24ab676SJeff Bonwick 			}
4211b24ab676SJeff Bonwick 
4212b24ab676SJeff Bonwick 			zio->io_cksum_report = zcr->zcr_next;
4213b24ab676SJeff Bonwick 			zcr->zcr_next = NULL;
4214eb633035STom Caputi 			zcr->zcr_finish(zcr, adata);
4215b24ab676SJeff Bonwick 			zfs_ereport_free_checksum(zcr);
4216b24ab676SJeff Bonwick 
4217b24ab676SJeff Bonwick 			if (asize != psize)
4218770499e1SDan Kimmel 				abd_free(adata);
4219b24ab676SJeff Bonwick 		}
4220b24ab676SJeff Bonwick 	}
4221e14bb325SJeff Bonwick 
4222e14bb325SJeff Bonwick 	zio_pop_transforms(zio);	/* note: may set zio->io_error */
4223e14bb325SJeff Bonwick 
4224e14bb325SJeff Bonwick 	vdev_stat_update(zio, psize);
4225e14bb325SJeff Bonwick 
4226e14bb325SJeff Bonwick 	if (zio->io_error) {
4227e14bb325SJeff Bonwick 		/*
4228e14bb325SJeff Bonwick 		 * If this I/O is attached to a particular vdev,
4229e14bb325SJeff Bonwick 		 * generate an error message describing the I/O failure
4230e14bb325SJeff Bonwick 		 * at the block level.  We ignore these errors if the
4231e14bb325SJeff Bonwick 		 * device is currently unavailable.
4232e14bb325SJeff Bonwick 		 */
4233e14bb325SJeff Bonwick 		if (zio->io_error != ECKSUM && vd != NULL && !vdev_is_dead(vd))
4234eb633035STom Caputi 			zfs_ereport_post(FM_EREPORT_ZFS_IO, spa, vd,
4235eb633035STom Caputi 			    &zio->io_bookmark, zio, 0, 0);
4236e14bb325SJeff Bonwick 
42378f18d1faSGeorge Wilson 		if ((zio->io_error == EIO || !(zio->io_flags &
42388f18d1faSGeorge Wilson 		    (ZIO_FLAG_SPECULATIVE | ZIO_FLAG_DONT_PROPAGATE))) &&
42398f18d1faSGeorge Wilson 		    zio == lio) {
4240e14bb325SJeff Bonwick 			/*
4241e14bb325SJeff Bonwick 			 * For logical I/O requests, tell the SPA to log the
4242e14bb325SJeff Bonwick 			 * error and generate a logical data ereport.
4243e14bb325SJeff Bonwick 			 */
4244eb633035STom Caputi 			spa_log_error(spa, &zio->io_bookmark);
4245eb633035STom Caputi 			zfs_ereport_post(FM_EREPORT_ZFS_DATA, spa, NULL,
4246eb633035STom Caputi 			    &zio->io_bookmark, zio, 0, 0);
4247e14bb325SJeff Bonwick 		}
4248e14bb325SJeff Bonwick 	}
4249fa9e4066Sahrens 
4250e14bb325SJeff Bonwick 	if (zio->io_error && zio == lio) {
4251e14bb325SJeff Bonwick 		/*
4252e14bb325SJeff Bonwick 		 * Determine whether zio should be reexecuted.  This will
4253e14bb325SJeff Bonwick 		 * propagate all the way to the root via zio_notify_parent().
4254e14bb325SJeff Bonwick 		 */
4255e14bb325SJeff Bonwick 		ASSERT(vd == NULL && bp != NULL);
4256b24ab676SJeff Bonwick 		ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
4257e14bb325SJeff Bonwick 
4258b24ab676SJeff Bonwick 		if (IO_IS_ALLOCATING(zio) &&
4259b24ab676SJeff Bonwick 		    !(zio->io_flags & ZIO_FLAG_CANFAIL)) {
4260e14bb325SJeff Bonwick 			if (zio->io_error != ENOSPC)
4261e14bb325SJeff Bonwick 				zio->io_reexecute |= ZIO_REEXECUTE_NOW;
4262e14bb325SJeff Bonwick 			else
4263e14bb325SJeff Bonwick 				zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
4264b24ab676SJeff Bonwick 		}
4265e14bb325SJeff Bonwick 
4266e14bb325SJeff Bonwick 		if ((zio->io_type == ZIO_TYPE_READ ||
4267e14bb325SJeff Bonwick 		    zio->io_type == ZIO_TYPE_FREE) &&
426844ecc532SGeorge Wilson 		    !(zio->io_flags & ZIO_FLAG_SCAN_THREAD) &&
4269e14bb325SJeff Bonwick 		    zio->io_error == ENXIO &&
4270b16da2e2SGeorge Wilson 		    spa_load_state(spa) == SPA_LOAD_NONE &&
4271e14bb325SJeff Bonwick 		    spa_get_failmode(spa) != ZIO_FAILURE_MODE_CONTINUE)
4272e14bb325SJeff Bonwick 			zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
4273e14bb325SJeff Bonwick 
4274e14bb325SJeff Bonwick 		if (!(zio->io_flags & ZIO_FLAG_CANFAIL) && !zio->io_reexecute)
4275e14bb325SJeff Bonwick 			zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
427622fe2c88SJonathan Adams 
427722fe2c88SJonathan Adams 		/*
427822fe2c88SJonathan Adams 		 * Here is a possibly good place to attempt to do
427922fe2c88SJonathan Adams 		 * either combinatorial reconstruction or error correction
428022fe2c88SJonathan Adams 		 * based on checksums.  It also might be a good place
428122fe2c88SJonathan Adams 		 * to send out preliminary ereports before we suspend
428222fe2c88SJonathan Adams 		 * processing.
428322fe2c88SJonathan Adams 		 */
4284d63d470bSgw 	}
4285d63d470bSgw 
428667bd71c6Sperrin 	/*
4287e14bb325SJeff Bonwick 	 * If there were logical child errors, they apply to us now.
4288e14bb325SJeff Bonwick 	 * We defer this until now to avoid conflating logical child
4289e14bb325SJeff Bonwick 	 * errors with errors that happened to the zio itself when
4290e14bb325SJeff Bonwick 	 * updating vdev stats and reporting FMA events above.
429167bd71c6Sperrin 	 */
4292e14bb325SJeff Bonwick 	zio_inherit_child_errors(zio, ZIO_CHILD_LOGICAL);
42938654d025Sperrin 
4294b24ab676SJeff Bonwick 	if ((zio->io_error || zio->io_reexecute) &&
4295b24ab676SJeff Bonwick 	    IO_IS_ALLOCATING(zio) && zio->io_gang_leader == zio &&
429680901aeaSGeorge Wilson 	    !(zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE)))
4297f5383399SBill Moore 		zio_dva_unallocate(zio, zio->io_gang_tree, bp);
4298f5383399SBill Moore 
4299f5383399SBill Moore 	zio_gang_tree_free(&zio->io_gang_tree);
4300f5383399SBill Moore 
430133a372edSGeorge Wilson 	/*
430233a372edSGeorge Wilson 	 * Godfather I/Os should never suspend.
430333a372edSGeorge Wilson 	 */
430433a372edSGeorge Wilson 	if ((zio->io_flags & ZIO_FLAG_GODFATHER) &&
430533a372edSGeorge Wilson 	    (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND))
430633a372edSGeorge Wilson 		zio->io_reexecute = 0;
430733a372edSGeorge Wilson 
430833a372edSGeorge Wilson 	if (zio->io_reexecute) {
4309e14bb325SJeff Bonwick 		/*
4310e14bb325SJeff Bonwick 		 * This is a logical I/O that wants to reexecute.
4311e14bb325SJeff Bonwick 		 *
4312e14bb325SJeff Bonwick 		 * Reexecute is top-down.  When an i/o fails, if it's not
4313e14bb325SJeff Bonwick 		 * the root, it simply notifies its parent and sticks around.
4314e14bb325SJeff Bonwick 		 * The parent, seeing that it still has children in zio_done(),
4315e14bb325SJeff Bonwick 		 * does the same.  This percolates all the way up to the root.
4316e14bb325SJeff Bonwick 		 * The root i/o will reexecute or suspend the entire tree.
4317e14bb325SJeff Bonwick 		 *
4318e14bb325SJeff Bonwick 		 * This approach ensures that zio_reexecute() honors
4319e14bb325SJeff Bonwick 		 * all the original i/o dependency relationships, e.g.
4320e14bb325SJeff Bonwick 		 * parents not executing until children are ready.
4321e14bb325SJeff Bonwick 		 */
4322e14bb325SJeff Bonwick 		ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
4323fa9e4066Sahrens 
4324f5383399SBill Moore 		zio->io_gang_leader = NULL;
4325e14bb325SJeff Bonwick 
4326a3f829aeSBill Moore 		mutex_enter(&zio->io_lock);
4327a3f829aeSBill Moore 		zio->io_state[ZIO_WAIT_DONE] = 1;
4328a3f829aeSBill Moore 		mutex_exit(&zio->io_lock);
4329a3f829aeSBill Moore 
433054d692b7SGeorge Wilson 		/*
433154d692b7SGeorge Wilson 		 * "The Godfather" I/O monitors its children but is
433254d692b7SGeorge Wilson 		 * not a true parent to them. It will track them through
433354d692b7SGeorge Wilson 		 * the pipeline but severs its ties whenever they get into
433454d692b7SGeorge Wilson 		 * trouble (e.g. suspended). This allows "The Godfather"
433554d692b7SGeorge Wilson 		 * I/O to return status without blocking.
433654d692b7SGeorge Wilson 		 */
43370f7643c7SGeorge Wilson 		zl = NULL;
43380f7643c7SGeorge Wilson 		for (pio = zio_walk_parents(zio, &zl); pio != NULL;
43390f7643c7SGeorge Wilson 		    pio = pio_next) {
43400f7643c7SGeorge Wilson 			zio_link_t *remove_zl = zl;
43410f7643c7SGeorge Wilson 			pio_next = zio_walk_parents(zio, &zl);
434254d692b7SGeorge Wilson 
434354d692b7SGeorge Wilson 			if ((pio->io_flags & ZIO_FLAG_GODFATHER) &&
434454d692b7SGeorge Wilson 			    (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND)) {
43450f7643c7SGeorge Wilson 				zio_remove_child(pio, zio, remove_zl);
434654d692b7SGeorge Wilson 				zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
434754d692b7SGeorge Wilson 			}
434854d692b7SGeorge Wilson 		}
434954d692b7SGeorge Wilson 
4350a3f829aeSBill Moore 		if ((pio = zio_unique_parent(zio)) != NULL) {
4351e14bb325SJeff Bonwick 			/*
4352e14bb325SJeff Bonwick 			 * We're not a root i/o, so there's nothing to do
4353e14bb325SJeff Bonwick 			 * but notify our parent.  Don't propagate errors
4354e14bb325SJeff Bonwick 			 * upward since we haven't permanently failed yet.
4355e14bb325SJeff Bonwick 			 */
435633a372edSGeorge Wilson 			ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER));
4357e14bb325SJeff Bonwick 			zio->io_flags |= ZIO_FLAG_DONT_PROPAGATE;
4358e14bb325SJeff Bonwick 			zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
4359e14bb325SJeff Bonwick 		} else if (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND) {
4360e14bb325SJeff Bonwick 			/*
4361e14bb325SJeff Bonwick 			 * We'd fail again if we reexecuted now, so suspend
4362e14bb325SJeff Bonwick 			 * until conditions improve (e.g. device comes online).
4363e14bb325SJeff Bonwick 			 */
4364e0f1c0afSOlaf Faaland 			zio_suspend(zio->io_spa, zio, ZIO_SUSPEND_IOERR);
4365e14bb325SJeff Bonwick 		} else {
4366e14bb325SJeff Bonwick 			/*
4367e14bb325SJeff Bonwick 			 * Reexecution is potentially a huge amount of work.
4368e14bb325SJeff Bonwick 			 * Hand it off to the otherwise-unused claim taskq.
4369e14bb325SJeff Bonwick 			 */
43705aeb9474SGarrett D'Amore 			ASSERT(zio->io_tqent.tqent_next == NULL);
4371ec94d322SAdam Leventhal 			spa_taskq_dispatch_ent(spa, ZIO_TYPE_CLAIM,
4372ec94d322SAdam Leventhal 			    ZIO_TASKQ_ISSUE, (task_func_t *)zio_reexecute, zio,
4373ec94d322SAdam Leventhal 			    0, &zio->io_tqent);
4374e14bb325SJeff Bonwick 		}
4375e14bb325SJeff Bonwick 		return (ZIO_PIPELINE_STOP);
4376fa9e4066Sahrens 	}
4377fa9e4066Sahrens 
4378b24ab676SJeff Bonwick 	ASSERT(zio->io_child_count == 0);
437933a372edSGeorge Wilson 	ASSERT(zio->io_reexecute == 0);
4380e14bb325SJeff Bonwick 	ASSERT(zio->io_error == 0 || (zio->io_flags & ZIO_FLAG_CANFAIL));
4381fa9e4066Sahrens 
4382b24ab676SJeff Bonwick 	/*
4383b24ab676SJeff Bonwick 	 * Report any checksum errors, since the I/O is complete.
4384b24ab676SJeff Bonwick 	 */
438522fe2c88SJonathan Adams 	while (zio->io_cksum_report != NULL) {
4386b24ab676SJeff Bonwick 		zio_cksum_report_t *zcr = zio->io_cksum_report;
4387b24ab676SJeff Bonwick 		zio->io_cksum_report = zcr->zcr_next;
4388b24ab676SJeff Bonwick 		zcr->zcr_next = NULL;
4389b24ab676SJeff Bonwick 		zcr->zcr_finish(zcr, NULL);
4390b24ab676SJeff Bonwick 		zfs_ereport_free_checksum(zcr);
439122fe2c88SJonathan Adams 	}
439222fe2c88SJonathan Adams 
4393a3f829aeSBill Moore 	/*
4394a3f829aeSBill Moore 	 * It is the responsibility of the done callback to ensure that this
4395a3f829aeSBill Moore 	 * particular zio is no longer discoverable for adoption, and as
4396a3f829aeSBill Moore 	 * such, cannot acquire any new parents.
4397a3f829aeSBill Moore 	 */
4398e14bb325SJeff Bonwick 	if (zio->io_done)
4399e14bb325SJeff Bonwick 		zio->io_done(zio);
4400fa9e4066Sahrens 
4401a3f829aeSBill Moore 	mutex_enter(&zio->io_lock);
4402a3f829aeSBill Moore 	zio->io_state[ZIO_WAIT_DONE] = 1;
4403a3f829aeSBill Moore 	mutex_exit(&zio->io_lock);
4404fa9e4066Sahrens 
44050f7643c7SGeorge Wilson 	zl = NULL;
44060f7643c7SGeorge Wilson 	for (pio = zio_walk_parents(zio, &zl); pio != NULL; pio = pio_next) {
44070f7643c7SGeorge Wilson 		zio_link_t *remove_zl = zl;
44080f7643c7SGeorge Wilson 		pio_next = zio_walk_parents(zio, &zl);
44090f7643c7SGeorge Wilson 		zio_remove_child(pio, zio, remove_zl);
4410e14bb325SJeff Bonwick 		zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
4411e14bb325SJeff Bonwick 	}
4412fa9e4066Sahrens 
4413e14bb325SJeff Bonwick 	if (zio->io_waiter != NULL) {
4414e14bb325SJeff Bonwick 		mutex_enter(&zio->io_lock);
4415e14bb325SJeff Bonwick 		zio->io_executor = NULL;
4416e14bb325SJeff Bonwick 		cv_broadcast(&zio->io_cv);
4417e14bb325SJeff Bonwick 		mutex_exit(&zio->io_lock);
4418e14bb325SJeff Bonwick 	} else {
4419e14bb325SJeff Bonwick 		zio_destroy(zio);
4420e14bb325SJeff Bonwick 	}
4421fa9e4066Sahrens 
4422e14bb325SJeff Bonwick 	return (ZIO_PIPELINE_STOP);
4423fa9e4066Sahrens }
442446341222Sperrin 
442546341222Sperrin /*
4426e14bb325SJeff Bonwick  * ==========================================================================
4427e14bb325SJeff Bonwick  * I/O pipeline definition
4428e14bb325SJeff Bonwick  * ==========================================================================
442946341222Sperrin  */
4430b24ab676SJeff Bonwick static zio_pipe_stage_t *zio_pipeline[] = {
4431e14bb325SJeff Bonwick 	NULL,
4432e14bb325SJeff Bonwick 	zio_read_bp_init,
44330f7643c7SGeorge Wilson 	zio_write_bp_init,
4434b24ab676SJeff Bonwick 	zio_free_bp_init,
4435b24ab676SJeff Bonwick 	zio_issue_async,
44360f7643c7SGeorge Wilson 	zio_write_compress,
4437eb633035STom Caputi 	zio_encrypt,
4438e14bb325SJeff Bonwick 	zio_checksum_generate,
443980901aeaSGeorge Wilson 	zio_nop_write,
4440b24ab676SJeff Bonwick 	zio_ddt_read_start,
4441b24ab676SJeff Bonwick 	zio_ddt_read_done,
4442b24ab676SJeff Bonwick 	zio_ddt_write,
4443b24ab676SJeff Bonwick 	zio_ddt_free,
4444e14bb325SJeff Bonwick 	zio_gang_assemble,
4445e14bb325SJeff Bonwick 	zio_gang_issue,
44460f7643c7SGeorge Wilson 	zio_dva_throttle,
4447e14bb325SJeff Bonwick 	zio_dva_allocate,
4448e14bb325SJeff Bonwick 	zio_dva_free,
4449e14bb325SJeff Bonwick 	zio_dva_claim,
4450e14bb325SJeff Bonwick 	zio_ready,
4451e14bb325SJeff Bonwick 	zio_vdev_io_start,
4452e14bb325SJeff Bonwick 	zio_vdev_io_done,
4453e14bb325SJeff Bonwick 	zio_vdev_io_assess,
4454e14bb325SJeff Bonwick 	zio_checksum_verify,
4455e14bb325SJeff Bonwick 	zio_done
4456e14bb325SJeff Bonwick };
4457ad135b5dSChristopher Siden 
4458ad135b5dSChristopher Siden 
4459ad135b5dSChristopher Siden 
4460ad135b5dSChristopher Siden 
4461a2cdcdd2SPaul Dagnelie /*
4462a2cdcdd2SPaul Dagnelie  * Compare two zbookmark_phys_t's to see which we would reach first in a
4463a2cdcdd2SPaul Dagnelie  * pre-order traversal of the object tree.
4464a2cdcdd2SPaul Dagnelie  *
4465a2cdcdd2SPaul Dagnelie  * This is simple in every case aside from the meta-dnode object. For all other
4466a2cdcdd2SPaul Dagnelie  * objects, we traverse them in order (object 1 before object 2, and so on).
4467a2cdcdd2SPaul Dagnelie  * However, all of these objects are traversed while traversing object 0, since
4468a2cdcdd2SPaul Dagnelie  * the data it points to is the list of objects.  Thus, we need to convert to a
4469a2cdcdd2SPaul Dagnelie  * canonical representation so we can compare meta-dnode bookmarks to
4470a2cdcdd2SPaul Dagnelie  * non-meta-dnode bookmarks.
4471a2cdcdd2SPaul Dagnelie  *
4472a2cdcdd2SPaul Dagnelie  * We do this by calculating "equivalents" for each field of the zbookmark.
4473a2cdcdd2SPaul Dagnelie  * zbookmarks outside of the meta-dnode use their own object and level, and
4474a2cdcdd2SPaul Dagnelie  * calculate the level 0 equivalent (the first L0 blkid that is contained in the
4475a2cdcdd2SPaul Dagnelie  * blocks this bookmark refers to) by multiplying their blkid by their span
4476a2cdcdd2SPaul Dagnelie  * (the number of L0 blocks contained within one block at their level).
4477a2cdcdd2SPaul Dagnelie  * zbookmarks inside the meta-dnode calculate their object equivalent
4478a2cdcdd2SPaul Dagnelie  * (which is L0equiv * dnodes per data block), use 0 for their L0equiv, and use
4479a2cdcdd2SPaul Dagnelie  * level + 1<<31 (any value larger than a level could ever be) for their level.
4480a2cdcdd2SPaul Dagnelie  * This causes them to always compare before a bookmark in their object
4481a2cdcdd2SPaul Dagnelie  * equivalent, compare appropriately to bookmarks in other objects, and to
4482a2cdcdd2SPaul Dagnelie  * compare appropriately to other bookmarks in the meta-dnode.
4483a2cdcdd2SPaul Dagnelie  */
4484a2cdcdd2SPaul Dagnelie int
4485a2cdcdd2SPaul Dagnelie zbookmark_compare(uint16_t dbss1, uint8_t ibs1, uint16_t dbss2, uint8_t ibs2,
4486a2cdcdd2SPaul Dagnelie     const zbookmark_phys_t *zb1, const zbookmark_phys_t *zb2)
4487a2cdcdd2SPaul Dagnelie {
4488a2cdcdd2SPaul Dagnelie 	/*
4489a2cdcdd2SPaul Dagnelie 	 * These variables represent the "equivalent" values for the zbookmark,
4490a2cdcdd2SPaul Dagnelie 	 * after converting zbookmarks inside the meta dnode to their
4491a2cdcdd2SPaul Dagnelie 	 * normal-object equivalents.
4492a2cdcdd2SPaul Dagnelie 	 */
4493a2cdcdd2SPaul Dagnelie 	uint64_t zb1obj, zb2obj;
4494a2cdcdd2SPaul Dagnelie 	uint64_t zb1L0, zb2L0;
4495a2cdcdd2SPaul Dagnelie 	uint64_t zb1level, zb2level;
4496ad135b5dSChristopher Siden 
4497a2cdcdd2SPaul Dagnelie 	if (zb1->zb_object == zb2->zb_object &&
4498a2cdcdd2SPaul Dagnelie 	    zb1->zb_level == zb2->zb_level &&
4499a2cdcdd2SPaul Dagnelie 	    zb1->zb_blkid == zb2->zb_blkid)
4500a2cdcdd2SPaul Dagnelie 		return (0);
4501a2cdcdd2SPaul Dagnelie 
4502a2cdcdd2SPaul Dagnelie 	/*
4503a2cdcdd2SPaul Dagnelie 	 * BP_SPANB calculates the span in blocks.
4504a2cdcdd2SPaul Dagnelie 	 */
4505a2cdcdd2SPaul Dagnelie 	zb1L0 = (zb1->zb_blkid) * BP_SPANB(ibs1, zb1->zb_level);
4506a2cdcdd2SPaul Dagnelie 	zb2L0 = (zb2->zb_blkid) * BP_SPANB(ibs2, zb2->zb_level);
4507ad135b5dSChristopher Siden 
4508ad135b5dSChristopher Siden 	if (zb1->zb_object == DMU_META_DNODE_OBJECT) {
4509a2cdcdd2SPaul Dagnelie 		zb1obj = zb1L0 * (dbss1 << (SPA_MINBLOCKSHIFT - DNODE_SHIFT));
4510a2cdcdd2SPaul Dagnelie 		zb1L0 = 0;
4511a2cdcdd2SPaul Dagnelie 		zb1level = zb1->zb_level + COMPARE_META_LEVEL;
4512a2cdcdd2SPaul Dagnelie 	} else {
4513a2cdcdd2SPaul Dagnelie 		zb1obj = zb1->zb_object;
4514a2cdcdd2SPaul Dagnelie 		zb1level = zb1->zb_level;
4515ad135b5dSChristopher Siden 	}
4516ad135b5dSChristopher Siden 
4517a2cdcdd2SPaul Dagnelie 	if (zb2->zb_object == DMU_META_DNODE_OBJECT) {
4518a2cdcdd2SPaul Dagnelie 		zb2obj = zb2L0 * (dbss2 << (SPA_MINBLOCKSHIFT - DNODE_SHIFT));
4519a2cdcdd2SPaul Dagnelie 		zb2L0 = 0;
4520a2cdcdd2SPaul Dagnelie 		zb2level = zb2->zb_level + COMPARE_META_LEVEL;
4521a2cdcdd2SPaul Dagnelie 	} else {
4522a2cdcdd2SPaul Dagnelie 		zb2obj = zb2->zb_object;
4523a2cdcdd2SPaul Dagnelie 		zb2level = zb2->zb_level;
4524a2cdcdd2SPaul Dagnelie 	}
4525a2cdcdd2SPaul Dagnelie 
4526a2cdcdd2SPaul Dagnelie 	/* Now that we have a canonical representation, do the comparison. */
4527a2cdcdd2SPaul Dagnelie 	if (zb1obj != zb2obj)
4528a2cdcdd2SPaul Dagnelie 		return (zb1obj < zb2obj ? -1 : 1);
4529a2cdcdd2SPaul Dagnelie 	else if (zb1L0 != zb2L0)
4530a2cdcdd2SPaul Dagnelie 		return (zb1L0 < zb2L0 ? -1 : 1);
4531a2cdcdd2SPaul Dagnelie 	else if (zb1level != zb2level)
4532a2cdcdd2SPaul Dagnelie 		return (zb1level > zb2level ? -1 : 1);
4533a2cdcdd2SPaul Dagnelie 	/*
4534a2cdcdd2SPaul Dagnelie 	 * This can (theoretically) happen if the bookmarks have the same object
4535a2cdcdd2SPaul Dagnelie 	 * and level, but different blkids, if the block sizes are not the same.
4536a2cdcdd2SPaul Dagnelie 	 * There is presently no way to change the indirect block sizes
4537a2cdcdd2SPaul Dagnelie 	 */
4538a2cdcdd2SPaul Dagnelie 	return (0);
4539a2cdcdd2SPaul Dagnelie }
4540a2cdcdd2SPaul Dagnelie 
4541a2cdcdd2SPaul Dagnelie /*
4542a2cdcdd2SPaul Dagnelie  *  This function checks the following: given that last_block is the place that
4543a2cdcdd2SPaul Dagnelie  *  our traversal stopped last time, does that guarantee that we've visited
4544a2cdcdd2SPaul Dagnelie  *  every node under subtree_root?  Therefore, we can't just use the raw output
4545a2cdcdd2SPaul Dagnelie  *  of zbookmark_compare.  We have to pass in a modified version of
4546a2cdcdd2SPaul Dagnelie  *  subtree_root; by incrementing the block id, and then checking whether
4547a2cdcdd2SPaul Dagnelie  *  last_block is before or equal to that, we can tell whether or not having
4548a2cdcdd2SPaul Dagnelie  *  visited last_block implies that all of subtree_root's children have been
4549a2cdcdd2SPaul Dagnelie  *  visited.
4550a2cdcdd2SPaul Dagnelie  */
4551a2cdcdd2SPaul Dagnelie boolean_t
4552a2cdcdd2SPaul Dagnelie zbookmark_subtree_completed(const dnode_phys_t *dnp,
4553a2cdcdd2SPaul Dagnelie     const zbookmark_phys_t *subtree_root, const zbookmark_phys_t *last_block)
4554a2cdcdd2SPaul Dagnelie {
4555a2cdcdd2SPaul Dagnelie 	zbookmark_phys_t mod_zb = *subtree_root;
4556a2cdcdd2SPaul Dagnelie 	mod_zb.zb_blkid++;
4557a2cdcdd2SPaul Dagnelie 	ASSERT(last_block->zb_level == 0);
4558a2cdcdd2SPaul Dagnelie 
4559a2cdcdd2SPaul Dagnelie 	/* The objset_phys_t isn't before anything. */
4560a2cdcdd2SPaul Dagnelie 	if (dnp == NULL)
4561ad135b5dSChristopher Siden 		return (B_FALSE);
4562a2cdcdd2SPaul Dagnelie 
4563a2cdcdd2SPaul Dagnelie 	/*
4564a2cdcdd2SPaul Dagnelie 	 * We pass in 1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT) for the
4565a2cdcdd2SPaul Dagnelie 	 * data block size in sectors, because that variable is only used if
4566a2cdcdd2SPaul Dagnelie 	 * the bookmark refers to a block in the meta-dnode.  Since we don't
4567a2cdcdd2SPaul Dagnelie 	 * know without examining it what object it refers to, and there's no
4568a2cdcdd2SPaul Dagnelie 	 * harm in passing in this value in other cases, we always pass it in.
4569a2cdcdd2SPaul Dagnelie 	 *
4570a2cdcdd2SPaul Dagnelie 	 * We pass in 0 for the indirect block size shift because zb2 must be
4571a2cdcdd2SPaul Dagnelie 	 * level 0.  The indirect block size is only used to calculate the span
4572a2cdcdd2SPaul Dagnelie 	 * of the bookmark, but since the bookmark must be level 0, the span is
4573a2cdcdd2SPaul Dagnelie 	 * always 1, so the math works out.
4574a2cdcdd2SPaul Dagnelie 	 *
4575a2cdcdd2SPaul Dagnelie 	 * If you make changes to how the zbookmark_compare code works, be sure
4576a2cdcdd2SPaul Dagnelie 	 * to make sure that this code still works afterwards.
4577a2cdcdd2SPaul Dagnelie 	 */
4578a2cdcdd2SPaul Dagnelie 	return (zbookmark_compare(dnp->dn_datablkszsec, dnp->dn_indblkshift,
4579a2cdcdd2SPaul Dagnelie 	    1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT), 0, &mod_zb,
4580a2cdcdd2SPaul Dagnelie 	    last_block) <= 0);
4581ad135b5dSChristopher Siden }
4582