xref: /onnv-gate/usr/src/uts/common/fs/zfs/zio.c (revision 5403:0bfd0977c989)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51544Seschrock  * Common Development and Distribution License (the "License").
61544Seschrock  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
223459Sek110237  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
27789Sahrens 
28789Sahrens #include <sys/zfs_context.h>
291544Seschrock #include <sys/fm/fs/zfs.h>
30789Sahrens #include <sys/spa.h>
31789Sahrens #include <sys/txg.h>
32789Sahrens #include <sys/spa_impl.h>
33789Sahrens #include <sys/vdev_impl.h>
34789Sahrens #include <sys/zio_impl.h>
35789Sahrens #include <sys/zio_compress.h>
36789Sahrens #include <sys/zio_checksum.h>
37789Sahrens 
38789Sahrens /*
39789Sahrens  * ==========================================================================
40789Sahrens  * I/O priority table
41789Sahrens  * ==========================================================================
42789Sahrens  */
43789Sahrens uint8_t zio_priority_table[ZIO_PRIORITY_TABLE_SIZE] = {
44789Sahrens 	0,	/* ZIO_PRIORITY_NOW		*/
45789Sahrens 	0,	/* ZIO_PRIORITY_SYNC_READ	*/
46789Sahrens 	0,	/* ZIO_PRIORITY_SYNC_WRITE	*/
47789Sahrens 	6,	/* ZIO_PRIORITY_ASYNC_READ	*/
48789Sahrens 	4,	/* ZIO_PRIORITY_ASYNC_WRITE	*/
49789Sahrens 	4,	/* ZIO_PRIORITY_FREE		*/
50789Sahrens 	0,	/* ZIO_PRIORITY_CACHE_FILL	*/
51789Sahrens 	0,	/* ZIO_PRIORITY_LOG_WRITE	*/
52789Sahrens 	10,	/* ZIO_PRIORITY_RESILVER	*/
53789Sahrens 	20,	/* ZIO_PRIORITY_SCRUB		*/
54789Sahrens };
55789Sahrens 
56789Sahrens /*
57789Sahrens  * ==========================================================================
58789Sahrens  * I/O type descriptions
59789Sahrens  * ==========================================================================
60789Sahrens  */
61789Sahrens char *zio_type_name[ZIO_TYPES] = {
62789Sahrens 	"null", "read", "write", "free", "claim", "ioctl" };
63789Sahrens 
64789Sahrens /* At or above this size, force gang blocking - for testing */
65789Sahrens uint64_t zio_gang_bang = SPA_MAXBLOCKSIZE + 1;
66789Sahrens 
673668Sgw25295 /* Force an allocation failure when non-zero */
683668Sgw25295 uint16_t zio_zil_fail_shift = 0;
695329Sgw25295 uint16_t zio_io_fail_shift = 0;
705329Sgw25295 
715329Sgw25295 /* Enable/disable the write-retry logic */
725329Sgw25295 int zio_write_retry = 1;
735329Sgw25295 
745329Sgw25295 /* Taskq to handle reissuing of I/Os */
755329Sgw25295 taskq_t *zio_taskq;
765329Sgw25295 int zio_resume_threads = 4;
773668Sgw25295 
78789Sahrens typedef struct zio_sync_pass {
79789Sahrens 	int	zp_defer_free;		/* defer frees after this pass */
80789Sahrens 	int	zp_dontcompress;	/* don't compress after this pass */
81789Sahrens 	int	zp_rewrite;		/* rewrite new bps after this pass */
82789Sahrens } zio_sync_pass_t;
83789Sahrens 
84789Sahrens zio_sync_pass_t zio_sync_pass = {
85789Sahrens 	1,	/* zp_defer_free */
86789Sahrens 	4,	/* zp_dontcompress */
87789Sahrens 	1,	/* zp_rewrite */
88789Sahrens };
89789Sahrens 
905329Sgw25295 static boolean_t zio_io_should_fail(uint16_t);
915329Sgw25295 
92789Sahrens /*
93789Sahrens  * ==========================================================================
94789Sahrens  * I/O kmem caches
95789Sahrens  * ==========================================================================
96789Sahrens  */
974055Seschrock kmem_cache_t *zio_cache;
98789Sahrens kmem_cache_t *zio_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
993290Sjohansen kmem_cache_t *zio_data_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
1003290Sjohansen 
1013290Sjohansen #ifdef _KERNEL
1023290Sjohansen extern vmem_t *zio_alloc_arena;
1033290Sjohansen #endif
104789Sahrens 
1055329Sgw25295 /*
1065329Sgw25295  * Determine if we are allowed to issue the IO based on the
1075329Sgw25295  * pool state. If we must wait then block until we are told
1085329Sgw25295  * that we may continue.
1095329Sgw25295  */
1105329Sgw25295 #define	ZIO_ENTER(spa) {						\
1115329Sgw25295 	if (spa->spa_state == POOL_STATE_IO_FAILURE) {			\
1125329Sgw25295 		mutex_enter(&spa->spa_zio_lock);			\
1135329Sgw25295 		while (spa->spa_state == POOL_STATE_IO_FAILURE)		\
1145329Sgw25295 			cv_wait(&spa->spa_zio_cv, &spa->spa_zio_lock);	\
1155329Sgw25295 		mutex_exit(&spa->spa_zio_lock);				\
1165329Sgw25295 	}								\
1175329Sgw25295 }
1185329Sgw25295 
1195329Sgw25295 /*
1205329Sgw25295  * An allocation zio is one that either currently has the DVA allocate
1215329Sgw25295  * stage set or will have it later in it's lifetime.
1225329Sgw25295  */
1235329Sgw25295 #define	IO_IS_ALLOCATING(zio) \
1245329Sgw25295 	((zio)->io_orig_pipeline == ZIO_WRITE_PIPELINE ||		\
1255329Sgw25295 	(zio)->io_pipeline & (1U << ZIO_STAGE_DVA_ALLOCATE))
1265329Sgw25295 
127789Sahrens void
128789Sahrens zio_init(void)
129789Sahrens {
130789Sahrens 	size_t c;
1313290Sjohansen 	vmem_t *data_alloc_arena = NULL;
1323290Sjohansen 
1333290Sjohansen #ifdef _KERNEL
1343290Sjohansen 	data_alloc_arena = zio_alloc_arena;
1353290Sjohansen #endif
136789Sahrens 
1374055Seschrock 	zio_cache = kmem_cache_create("zio_cache", sizeof (zio_t), 0,
1384055Seschrock 	    NULL, NULL, NULL, NULL, NULL, 0);
1394055Seschrock 
140789Sahrens 	/*
141789Sahrens 	 * For small buffers, we want a cache for each multiple of
142789Sahrens 	 * SPA_MINBLOCKSIZE.  For medium-size buffers, we want a cache
143789Sahrens 	 * for each quarter-power of 2.  For large buffers, we want
144789Sahrens 	 * a cache for each multiple of PAGESIZE.
145789Sahrens 	 */
146789Sahrens 	for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) {
147789Sahrens 		size_t size = (c + 1) << SPA_MINBLOCKSHIFT;
148789Sahrens 		size_t p2 = size;
149789Sahrens 		size_t align = 0;
150789Sahrens 
151789Sahrens 		while (p2 & (p2 - 1))
152789Sahrens 			p2 &= p2 - 1;
153789Sahrens 
154789Sahrens 		if (size <= 4 * SPA_MINBLOCKSIZE) {
155789Sahrens 			align = SPA_MINBLOCKSIZE;
156789Sahrens 		} else if (P2PHASE(size, PAGESIZE) == 0) {
157789Sahrens 			align = PAGESIZE;
158789Sahrens 		} else if (P2PHASE(size, p2 >> 2) == 0) {
159789Sahrens 			align = p2 >> 2;
160789Sahrens 		}
161789Sahrens 
162789Sahrens 		if (align != 0) {
1633290Sjohansen 			char name[36];
1642856Snd150628 			(void) sprintf(name, "zio_buf_%lu", (ulong_t)size);
165789Sahrens 			zio_buf_cache[c] = kmem_cache_create(name, size,
166849Sbonwick 			    align, NULL, NULL, NULL, NULL, NULL, KMC_NODEBUG);
1673290Sjohansen 
1683290Sjohansen 			(void) sprintf(name, "zio_data_buf_%lu", (ulong_t)size);
1693290Sjohansen 			zio_data_buf_cache[c] = kmem_cache_create(name, size,
1703290Sjohansen 			    align, NULL, NULL, NULL, NULL, data_alloc_arena,
1713290Sjohansen 			    KMC_NODEBUG);
1723290Sjohansen 
173789Sahrens 			dprintf("creating cache for size %5lx align %5lx\n",
174789Sahrens 			    size, align);
175789Sahrens 		}
176789Sahrens 	}
177789Sahrens 
178789Sahrens 	while (--c != 0) {
179789Sahrens 		ASSERT(zio_buf_cache[c] != NULL);
180789Sahrens 		if (zio_buf_cache[c - 1] == NULL)
181789Sahrens 			zio_buf_cache[c - 1] = zio_buf_cache[c];
1823290Sjohansen 
1833290Sjohansen 		ASSERT(zio_data_buf_cache[c] != NULL);
1843290Sjohansen 		if (zio_data_buf_cache[c - 1] == NULL)
1853290Sjohansen 			zio_data_buf_cache[c - 1] = zio_data_buf_cache[c];
186789Sahrens 	}
1871544Seschrock 
1885329Sgw25295 	zio_taskq = taskq_create("zio_taskq", zio_resume_threads,
1895329Sgw25295 	    maxclsyspri, 50, INT_MAX, TASKQ_PREPOPULATE);
1905329Sgw25295 
1911544Seschrock 	zio_inject_init();
192789Sahrens }
193789Sahrens 
194789Sahrens void
195789Sahrens zio_fini(void)
196789Sahrens {
197789Sahrens 	size_t c;
198789Sahrens 	kmem_cache_t *last_cache = NULL;
1993290Sjohansen 	kmem_cache_t *last_data_cache = NULL;
200789Sahrens 
201789Sahrens 	for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) {
202789Sahrens 		if (zio_buf_cache[c] != last_cache) {
203789Sahrens 			last_cache = zio_buf_cache[c];
204789Sahrens 			kmem_cache_destroy(zio_buf_cache[c]);
205789Sahrens 		}
206789Sahrens 		zio_buf_cache[c] = NULL;
2073290Sjohansen 
2083290Sjohansen 		if (zio_data_buf_cache[c] != last_data_cache) {
2093290Sjohansen 			last_data_cache = zio_data_buf_cache[c];
2103290Sjohansen 			kmem_cache_destroy(zio_data_buf_cache[c]);
2113290Sjohansen 		}
2123290Sjohansen 		zio_data_buf_cache[c] = NULL;
213789Sahrens 	}
2141544Seschrock 
2155329Sgw25295 	taskq_destroy(zio_taskq);
2165329Sgw25295 
2174055Seschrock 	kmem_cache_destroy(zio_cache);
2184055Seschrock 
2191544Seschrock 	zio_inject_fini();
220789Sahrens }
221789Sahrens 
222789Sahrens /*
223789Sahrens  * ==========================================================================
224789Sahrens  * Allocate and free I/O buffers
225789Sahrens  * ==========================================================================
226789Sahrens  */
2273290Sjohansen 
2283290Sjohansen /*
2293290Sjohansen  * Use zio_buf_alloc to allocate ZFS metadata.  This data will appear in a
2303290Sjohansen  * crashdump if the kernel panics, so use it judiciously.  Obviously, it's
2313290Sjohansen  * useful to inspect ZFS metadata, but if possible, we should avoid keeping
2323290Sjohansen  * excess / transient data in-core during a crashdump.
2333290Sjohansen  */
234789Sahrens void *
235789Sahrens zio_buf_alloc(size_t size)
236789Sahrens {
237789Sahrens 	size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
238789Sahrens 
239789Sahrens 	ASSERT(c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
240789Sahrens 
241789Sahrens 	return (kmem_cache_alloc(zio_buf_cache[c], KM_SLEEP));
242789Sahrens }
243789Sahrens 
2443290Sjohansen /*
2453290Sjohansen  * Use zio_data_buf_alloc to allocate data.  The data will not appear in a
2463290Sjohansen  * crashdump if the kernel panics.  This exists so that we will limit the amount
2473290Sjohansen  * of ZFS data that shows up in a kernel crashdump.  (Thus reducing the amount
2483290Sjohansen  * of kernel heap dumped to disk when the kernel panics)
2493290Sjohansen  */
2503290Sjohansen void *
2513290Sjohansen zio_data_buf_alloc(size_t size)
2523290Sjohansen {
2533290Sjohansen 	size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
2543290Sjohansen 
2553290Sjohansen 	ASSERT(c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
2563290Sjohansen 
2573290Sjohansen 	return (kmem_cache_alloc(zio_data_buf_cache[c], KM_SLEEP));
2583290Sjohansen }
2593290Sjohansen 
260789Sahrens void
261789Sahrens zio_buf_free(void *buf, size_t size)
262789Sahrens {
263789Sahrens 	size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
264789Sahrens 
265789Sahrens 	ASSERT(c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
266789Sahrens 
267789Sahrens 	kmem_cache_free(zio_buf_cache[c], buf);
268789Sahrens }
269789Sahrens 
2703290Sjohansen void
2713290Sjohansen zio_data_buf_free(void *buf, size_t size)
2723290Sjohansen {
2733290Sjohansen 	size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
2743290Sjohansen 
2753290Sjohansen 	ASSERT(c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
2763290Sjohansen 
2773290Sjohansen 	kmem_cache_free(zio_data_buf_cache[c], buf);
2783290Sjohansen }
2793463Sahrens 
280789Sahrens /*
281789Sahrens  * ==========================================================================
282789Sahrens  * Push and pop I/O transform buffers
283789Sahrens  * ==========================================================================
284789Sahrens  */
285789Sahrens static void
286789Sahrens zio_push_transform(zio_t *zio, void *data, uint64_t size, uint64_t bufsize)
287789Sahrens {
288789Sahrens 	zio_transform_t *zt = kmem_alloc(sizeof (zio_transform_t), KM_SLEEP);
289789Sahrens 
290789Sahrens 	zt->zt_data = data;
291789Sahrens 	zt->zt_size = size;
292789Sahrens 	zt->zt_bufsize = bufsize;
293789Sahrens 
294789Sahrens 	zt->zt_next = zio->io_transform_stack;
295789Sahrens 	zio->io_transform_stack = zt;
296789Sahrens 
297789Sahrens 	zio->io_data = data;
298789Sahrens 	zio->io_size = size;
299789Sahrens }
300789Sahrens 
301789Sahrens static void
302789Sahrens zio_pop_transform(zio_t *zio, void **data, uint64_t *size, uint64_t *bufsize)
303789Sahrens {
304789Sahrens 	zio_transform_t *zt = zio->io_transform_stack;
305789Sahrens 
306789Sahrens 	*data = zt->zt_data;
307789Sahrens 	*size = zt->zt_size;
308789Sahrens 	*bufsize = zt->zt_bufsize;
309789Sahrens 
310789Sahrens 	zio->io_transform_stack = zt->zt_next;
311789Sahrens 	kmem_free(zt, sizeof (zio_transform_t));
312789Sahrens 
313789Sahrens 	if ((zt = zio->io_transform_stack) != NULL) {
314789Sahrens 		zio->io_data = zt->zt_data;
315789Sahrens 		zio->io_size = zt->zt_size;
316789Sahrens 	}
317789Sahrens }
318789Sahrens 
319789Sahrens static void
320789Sahrens zio_clear_transform_stack(zio_t *zio)
321789Sahrens {
322789Sahrens 	void *data;
323789Sahrens 	uint64_t size, bufsize;
324789Sahrens 
325789Sahrens 	ASSERT(zio->io_transform_stack != NULL);
326789Sahrens 
327789Sahrens 	zio_pop_transform(zio, &data, &size, &bufsize);
328789Sahrens 	while (zio->io_transform_stack != NULL) {
329789Sahrens 		zio_buf_free(data, bufsize);
330789Sahrens 		zio_pop_transform(zio, &data, &size, &bufsize);
331789Sahrens 	}
332789Sahrens }
333789Sahrens 
334789Sahrens /*
335789Sahrens  * ==========================================================================
336789Sahrens  * Create the various types of I/O (read, write, free)
337789Sahrens  * ==========================================================================
338789Sahrens  */
339789Sahrens static zio_t *
340789Sahrens zio_create(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
341789Sahrens     void *data, uint64_t size, zio_done_func_t *done, void *private,
342789Sahrens     zio_type_t type, int priority, int flags, uint8_t stage, uint32_t pipeline)
343789Sahrens {
344789Sahrens 	zio_t *zio;
345789Sahrens 
346789Sahrens 	ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
347789Sahrens 	ASSERT(P2PHASE(size, SPA_MINBLOCKSIZE) == 0);
348789Sahrens 
3494055Seschrock 	zio = kmem_cache_alloc(zio_cache, KM_SLEEP);
3504055Seschrock 	bzero(zio, sizeof (zio_t));
351789Sahrens 	zio->io_parent = pio;
352789Sahrens 	zio->io_spa = spa;
353789Sahrens 	zio->io_txg = txg;
3544634Sek110237 	zio->io_flags = flags;
355789Sahrens 	if (bp != NULL) {
356789Sahrens 		zio->io_bp = bp;
357789Sahrens 		zio->io_bp_copy = *bp;
358789Sahrens 		zio->io_bp_orig = *bp;
3594634Sek110237 		if (dmu_ot[BP_GET_TYPE(bp)].ot_metadata ||
3604634Sek110237 		    BP_GET_LEVEL(bp) != 0)
3614634Sek110237 			zio->io_flags |= ZIO_FLAG_METADATA;
362789Sahrens 	}
363789Sahrens 	zio->io_done = done;
364789Sahrens 	zio->io_private = private;
365789Sahrens 	zio->io_type = type;
366789Sahrens 	zio->io_priority = priority;
367789Sahrens 	zio->io_stage = stage;
368789Sahrens 	zio->io_pipeline = pipeline;
369789Sahrens 	zio->io_async_stages = ZIO_ASYNC_PIPELINE_STAGES;
370789Sahrens 	zio->io_timestamp = lbolt64;
3714634Sek110237 	if (pio != NULL)
3724634Sek110237 		zio->io_flags |= (pio->io_flags & ZIO_FLAG_METADATA);
3732856Snd150628 	mutex_init(&zio->io_lock, NULL, MUTEX_DEFAULT, NULL);
3744831Sgw25295 	cv_init(&zio->io_cv, NULL, CV_DEFAULT, NULL);
375789Sahrens 	zio_push_transform(zio, data, size, size);
376789Sahrens 
3773463Sahrens 	/*
3783463Sahrens 	 * Note on config lock:
3793463Sahrens 	 *
3803463Sahrens 	 * If CONFIG_HELD is set, then the caller already has the config
3813463Sahrens 	 * lock, so we don't need it for this io.
3823463Sahrens 	 *
3833463Sahrens 	 * We set CONFIG_GRABBED to indicate that we have grabbed the
3843463Sahrens 	 * config lock on behalf of this io, so it should be released
3853463Sahrens 	 * in zio_done.
3863463Sahrens 	 *
3873463Sahrens 	 * Unless CONFIG_HELD is set, we will grab the config lock for
3883463Sahrens 	 * any top-level (parent-less) io, *except* NULL top-level ios.
3893463Sahrens 	 * The NULL top-level ios rarely have any children, so we delay
3903463Sahrens 	 * grabbing the lock until the first child is added (but it is
3913463Sahrens 	 * still grabbed on behalf of the top-level i/o, so additional
3923463Sahrens 	 * children don't need to also grab it).  This greatly reduces
3933463Sahrens 	 * contention on the config lock.
3943463Sahrens 	 */
395789Sahrens 	if (pio == NULL) {
3963463Sahrens 		if (type != ZIO_TYPE_NULL &&
3973463Sahrens 		    !(flags & ZIO_FLAG_CONFIG_HELD)) {
3981544Seschrock 			spa_config_enter(zio->io_spa, RW_READER, zio);
3993463Sahrens 			zio->io_flags |= ZIO_FLAG_CONFIG_GRABBED;
4003463Sahrens 		}
401789Sahrens 		zio->io_root = zio;
402789Sahrens 	} else {
403789Sahrens 		zio->io_root = pio->io_root;
4041544Seschrock 		if (!(flags & ZIO_FLAG_NOBOOKMARK))
4051544Seschrock 			zio->io_logical = pio->io_logical;
406789Sahrens 		mutex_enter(&pio->io_lock);
4073463Sahrens 		if (pio->io_parent == NULL &&
4083463Sahrens 		    pio->io_type == ZIO_TYPE_NULL &&
4093463Sahrens 		    !(pio->io_flags & ZIO_FLAG_CONFIG_GRABBED) &&
4103463Sahrens 		    !(pio->io_flags & ZIO_FLAG_CONFIG_HELD)) {
4113463Sahrens 			pio->io_flags |= ZIO_FLAG_CONFIG_GRABBED;
4123463Sahrens 			spa_config_enter(zio->io_spa, RW_READER, pio);
4133463Sahrens 		}
414789Sahrens 		if (stage < ZIO_STAGE_READY)
415789Sahrens 			pio->io_children_notready++;
416789Sahrens 		pio->io_children_notdone++;
417789Sahrens 		zio->io_sibling_next = pio->io_child;
418789Sahrens 		zio->io_sibling_prev = NULL;
419789Sahrens 		if (pio->io_child != NULL)
420789Sahrens 			pio->io_child->io_sibling_prev = zio;
421789Sahrens 		pio->io_child = zio;
4221775Sbillm 		zio->io_ndvas = pio->io_ndvas;
423789Sahrens 		mutex_exit(&pio->io_lock);
424789Sahrens 	}
425789Sahrens 
4265329Sgw25295 	/*
4275329Sgw25295 	 * Save off the original state incase we need to retry later.
4285329Sgw25295 	 */
4295329Sgw25295 	zio->io_orig_stage = zio->io_stage;
4305329Sgw25295 	zio->io_orig_pipeline = zio->io_pipeline;
4315329Sgw25295 	zio->io_orig_flags = zio->io_flags;
4325329Sgw25295 
433789Sahrens 	return (zio);
434789Sahrens }
435789Sahrens 
4365329Sgw25295 static void
4375329Sgw25295 zio_reset(zio_t *zio)
4385329Sgw25295 {
4395329Sgw25295 	zio_clear_transform_stack(zio);
4405329Sgw25295 
4415329Sgw25295 	zio->io_flags = zio->io_orig_flags;
4425329Sgw25295 	zio->io_stage = zio->io_orig_stage;
4435329Sgw25295 	zio->io_pipeline = zio->io_orig_pipeline;
4445329Sgw25295 	zio_push_transform(zio, zio->io_data, zio->io_size, zio->io_size);
4455329Sgw25295 }
4465329Sgw25295 
447789Sahrens zio_t *
448789Sahrens zio_null(zio_t *pio, spa_t *spa, zio_done_func_t *done, void *private,
449789Sahrens 	int flags)
450789Sahrens {
451789Sahrens 	zio_t *zio;
452789Sahrens 
453789Sahrens 	zio = zio_create(pio, spa, 0, NULL, NULL, 0, done, private,
454789Sahrens 	    ZIO_TYPE_NULL, ZIO_PRIORITY_NOW, flags, ZIO_STAGE_OPEN,
455789Sahrens 	    ZIO_WAIT_FOR_CHILDREN_PIPELINE);
456789Sahrens 
457789Sahrens 	return (zio);
458789Sahrens }
459789Sahrens 
460789Sahrens zio_t *
461789Sahrens zio_root(spa_t *spa, zio_done_func_t *done, void *private, int flags)
462789Sahrens {
463789Sahrens 	return (zio_null(NULL, spa, done, private, flags));
464789Sahrens }
465789Sahrens 
466789Sahrens zio_t *
467789Sahrens zio_read(zio_t *pio, spa_t *spa, blkptr_t *bp, void *data,
468789Sahrens     uint64_t size, zio_done_func_t *done, void *private,
4691544Seschrock     int priority, int flags, zbookmark_t *zb)
470789Sahrens {
471789Sahrens 	zio_t *zio;
472789Sahrens 
473789Sahrens 	ASSERT3U(size, ==, BP_GET_LSIZE(bp));
474789Sahrens 
4755329Sgw25295 	/*
4765329Sgw25295 	 * If the user has specified that we allow I/Os to continue
4775329Sgw25295 	 * then attempt to satisfy the read.
4785329Sgw25295 	 */
4795329Sgw25295 	if (spa_get_failmode(spa) != ZIO_FAILURE_MODE_CONTINUE)
4805329Sgw25295 		ZIO_ENTER(spa);
4815329Sgw25295 
482789Sahrens 	zio = zio_create(pio, spa, bp->blk_birth, bp, data, size, done, private,
4832981Sahrens 	    ZIO_TYPE_READ, priority, flags | ZIO_FLAG_USER,
4842981Sahrens 	    ZIO_STAGE_OPEN, ZIO_READ_PIPELINE);
4851544Seschrock 	zio->io_bookmark = *zb;
4861544Seschrock 
4871544Seschrock 	zio->io_logical = zio;
488789Sahrens 
489789Sahrens 	/*
490789Sahrens 	 * Work off our copy of the bp so the caller can free it.
491789Sahrens 	 */
492789Sahrens 	zio->io_bp = &zio->io_bp_copy;
493789Sahrens 
494789Sahrens 	return (zio);
495789Sahrens }
496789Sahrens 
497789Sahrens zio_t *
4981775Sbillm zio_write(zio_t *pio, spa_t *spa, int checksum, int compress, int ncopies,
499789Sahrens     uint64_t txg, blkptr_t *bp, void *data, uint64_t size,
5003547Smaybee     zio_done_func_t *ready, zio_done_func_t *done, void *private, int priority,
5013547Smaybee     int flags, zbookmark_t *zb)
502789Sahrens {
503789Sahrens 	zio_t *zio;
504789Sahrens 
505789Sahrens 	ASSERT(checksum >= ZIO_CHECKSUM_OFF &&
506789Sahrens 	    checksum < ZIO_CHECKSUM_FUNCTIONS);
507789Sahrens 
508789Sahrens 	ASSERT(compress >= ZIO_COMPRESS_OFF &&
509789Sahrens 	    compress < ZIO_COMPRESS_FUNCTIONS);
510789Sahrens 
5115329Sgw25295 	ZIO_ENTER(spa);
5125329Sgw25295 
513789Sahrens 	zio = zio_create(pio, spa, txg, bp, data, size, done, private,
5142981Sahrens 	    ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_USER,
515789Sahrens 	    ZIO_STAGE_OPEN, ZIO_WRITE_PIPELINE);
516789Sahrens 
5173547Smaybee 	zio->io_ready = ready;
5183547Smaybee 
5191544Seschrock 	zio->io_bookmark = *zb;
5201544Seschrock 
5211544Seschrock 	zio->io_logical = zio;
5221544Seschrock 
523789Sahrens 	zio->io_checksum = checksum;
524789Sahrens 	zio->io_compress = compress;
5251775Sbillm 	zio->io_ndvas = ncopies;
526789Sahrens 
527789Sahrens 	if (compress != ZIO_COMPRESS_OFF)
528789Sahrens 		zio->io_async_stages |= 1U << ZIO_STAGE_WRITE_COMPRESS;
529789Sahrens 
530789Sahrens 	if (bp->blk_birth != txg) {
531789Sahrens 		/* XXX the bp usually (always?) gets re-zeroed later */
532789Sahrens 		BP_ZERO(bp);
533789Sahrens 		BP_SET_LSIZE(bp, size);
534789Sahrens 		BP_SET_PSIZE(bp, size);
5351775Sbillm 	} else {
5361775Sbillm 		/* Make sure someone doesn't change their mind on overwrites */
5371775Sbillm 		ASSERT(MIN(zio->io_ndvas + BP_IS_GANG(bp),
5381775Sbillm 		    spa_max_replication(spa)) == BP_GET_NDVAS(bp));
539789Sahrens 	}
540789Sahrens 
541789Sahrens 	return (zio);
542789Sahrens }
543789Sahrens 
544789Sahrens zio_t *
545789Sahrens zio_rewrite(zio_t *pio, spa_t *spa, int checksum,
546789Sahrens     uint64_t txg, blkptr_t *bp, void *data, uint64_t size,
5471544Seschrock     zio_done_func_t *done, void *private, int priority, int flags,
5481544Seschrock     zbookmark_t *zb)
549789Sahrens {
550789Sahrens 	zio_t *zio;
551789Sahrens 
552789Sahrens 	zio = zio_create(pio, spa, txg, bp, data, size, done, private,
5532981Sahrens 	    ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_USER,
554789Sahrens 	    ZIO_STAGE_OPEN, ZIO_REWRITE_PIPELINE);
555789Sahrens 
5561544Seschrock 	zio->io_bookmark = *zb;
557789Sahrens 	zio->io_checksum = checksum;
558789Sahrens 	zio->io_compress = ZIO_COMPRESS_OFF;
559789Sahrens 
5601775Sbillm 	if (pio != NULL)
5611775Sbillm 		ASSERT3U(zio->io_ndvas, <=, BP_GET_NDVAS(bp));
5621775Sbillm 
563789Sahrens 	return (zio);
564789Sahrens }
565789Sahrens 
5665329Sgw25295 static void
5675329Sgw25295 zio_write_allocate_ready(zio_t *zio)
5685329Sgw25295 {
5695329Sgw25295 	/* Free up the previous block */
5705329Sgw25295 	if (!BP_IS_HOLE(&zio->io_bp_orig)) {
5715329Sgw25295 		zio_nowait(zio_free(zio, zio->io_spa, zio->io_txg,
5725329Sgw25295 		    &zio->io_bp_orig, NULL, NULL));
5735329Sgw25295 	}
5745329Sgw25295 }
5755329Sgw25295 
576789Sahrens static zio_t *
577789Sahrens zio_write_allocate(zio_t *pio, spa_t *spa, int checksum,
578789Sahrens     uint64_t txg, blkptr_t *bp, void *data, uint64_t size,
579789Sahrens     zio_done_func_t *done, void *private, int priority, int flags)
580789Sahrens {
581789Sahrens 	zio_t *zio;
582789Sahrens 
583789Sahrens 	BP_ZERO(bp);
584789Sahrens 	BP_SET_LSIZE(bp, size);
585789Sahrens 	BP_SET_PSIZE(bp, size);
586789Sahrens 	BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF);
587789Sahrens 
588789Sahrens 	zio = zio_create(pio, spa, txg, bp, data, size, done, private,
589789Sahrens 	    ZIO_TYPE_WRITE, priority, flags,
590789Sahrens 	    ZIO_STAGE_OPEN, ZIO_WRITE_ALLOCATE_PIPELINE);
591789Sahrens 
592789Sahrens 	zio->io_checksum = checksum;
593789Sahrens 	zio->io_compress = ZIO_COMPRESS_OFF;
5945329Sgw25295 	zio->io_ready = zio_write_allocate_ready;
595789Sahrens 
596789Sahrens 	return (zio);
597789Sahrens }
598789Sahrens 
599789Sahrens zio_t *
600789Sahrens zio_free(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
601789Sahrens     zio_done_func_t *done, void *private)
602789Sahrens {
603789Sahrens 	zio_t *zio;
604789Sahrens 
605789Sahrens 	ASSERT(!BP_IS_HOLE(bp));
606789Sahrens 
607789Sahrens 	if (txg == spa->spa_syncing_txg &&
608789Sahrens 	    spa->spa_sync_pass > zio_sync_pass.zp_defer_free) {
609789Sahrens 		bplist_enqueue_deferred(&spa->spa_sync_bplist, bp);
610789Sahrens 		return (zio_null(pio, spa, NULL, NULL, 0));
611789Sahrens 	}
612789Sahrens 
613789Sahrens 	zio = zio_create(pio, spa, txg, bp, NULL, 0, done, private,
6142981Sahrens 	    ZIO_TYPE_FREE, ZIO_PRIORITY_FREE, ZIO_FLAG_USER,
615789Sahrens 	    ZIO_STAGE_OPEN, ZIO_FREE_PIPELINE);
616789Sahrens 
617789Sahrens 	zio->io_bp = &zio->io_bp_copy;
618789Sahrens 
619789Sahrens 	return (zio);
620789Sahrens }
621789Sahrens 
622789Sahrens zio_t *
623789Sahrens zio_claim(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
624789Sahrens     zio_done_func_t *done, void *private)
625789Sahrens {
626789Sahrens 	zio_t *zio;
627789Sahrens 
628789Sahrens 	/*
629789Sahrens 	 * A claim is an allocation of a specific block.  Claims are needed
630789Sahrens 	 * to support immediate writes in the intent log.  The issue is that
631789Sahrens 	 * immediate writes contain committed data, but in a txg that was
632789Sahrens 	 * *not* committed.  Upon opening the pool after an unclean shutdown,
633789Sahrens 	 * the intent log claims all blocks that contain immediate write data
634789Sahrens 	 * so that the SPA knows they're in use.
635789Sahrens 	 *
636789Sahrens 	 * All claims *must* be resolved in the first txg -- before the SPA
637789Sahrens 	 * starts allocating blocks -- so that nothing is allocated twice.
638789Sahrens 	 */
639789Sahrens 	ASSERT3U(spa->spa_uberblock.ub_rootbp.blk_birth, <, spa_first_txg(spa));
640789Sahrens 	ASSERT3U(spa_first_txg(spa), <=, txg);
641789Sahrens 
642789Sahrens 	zio = zio_create(pio, spa, txg, bp, NULL, 0, done, private,
643789Sahrens 	    ZIO_TYPE_CLAIM, ZIO_PRIORITY_NOW, 0,
644789Sahrens 	    ZIO_STAGE_OPEN, ZIO_CLAIM_PIPELINE);
645789Sahrens 
646789Sahrens 	zio->io_bp = &zio->io_bp_copy;
647789Sahrens 
648789Sahrens 	return (zio);
649789Sahrens }
650789Sahrens 
651789Sahrens zio_t *
652789Sahrens zio_ioctl(zio_t *pio, spa_t *spa, vdev_t *vd, int cmd,
653789Sahrens     zio_done_func_t *done, void *private, int priority, int flags)
654789Sahrens {
655789Sahrens 	zio_t *zio;
656789Sahrens 	int c;
657789Sahrens 
658789Sahrens 	if (vd->vdev_children == 0) {
659789Sahrens 		zio = zio_create(pio, spa, 0, NULL, NULL, 0, done, private,
660789Sahrens 		    ZIO_TYPE_IOCTL, priority, flags,
661789Sahrens 		    ZIO_STAGE_OPEN, ZIO_IOCTL_PIPELINE);
662789Sahrens 
663789Sahrens 		zio->io_vd = vd;
664789Sahrens 		zio->io_cmd = cmd;
665789Sahrens 	} else {
666789Sahrens 		zio = zio_null(pio, spa, NULL, NULL, flags);
667789Sahrens 
668789Sahrens 		for (c = 0; c < vd->vdev_children; c++)
669789Sahrens 			zio_nowait(zio_ioctl(zio, spa, vd->vdev_child[c], cmd,
670789Sahrens 			    done, private, priority, flags));
671789Sahrens 	}
672789Sahrens 
673789Sahrens 	return (zio);
674789Sahrens }
675789Sahrens 
676789Sahrens static void
677789Sahrens zio_phys_bp_init(vdev_t *vd, blkptr_t *bp, uint64_t offset, uint64_t size,
678789Sahrens     int checksum)
679789Sahrens {
680789Sahrens 	ASSERT(vd->vdev_children == 0);
681789Sahrens 
682789Sahrens 	ASSERT(size <= SPA_MAXBLOCKSIZE);
683789Sahrens 	ASSERT(P2PHASE(size, SPA_MINBLOCKSIZE) == 0);
684789Sahrens 	ASSERT(P2PHASE(offset, SPA_MINBLOCKSIZE) == 0);
685789Sahrens 
686789Sahrens 	ASSERT(offset + size <= VDEV_LABEL_START_SIZE ||
687789Sahrens 	    offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
688789Sahrens 	ASSERT3U(offset + size, <=, vd->vdev_psize);
689789Sahrens 
690789Sahrens 	BP_ZERO(bp);
691789Sahrens 
692789Sahrens 	BP_SET_LSIZE(bp, size);
693789Sahrens 	BP_SET_PSIZE(bp, size);
694789Sahrens 
695789Sahrens 	BP_SET_CHECKSUM(bp, checksum);
696789Sahrens 	BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF);
697789Sahrens 	BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
698789Sahrens 
699789Sahrens 	if (checksum != ZIO_CHECKSUM_OFF)
700789Sahrens 		ZIO_SET_CHECKSUM(&bp->blk_cksum, offset, 0, 0, 0);
701789Sahrens }
702789Sahrens 
703789Sahrens zio_t *
704789Sahrens zio_read_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
705789Sahrens     void *data, int checksum, zio_done_func_t *done, void *private,
706789Sahrens     int priority, int flags)
707789Sahrens {
708789Sahrens 	zio_t *zio;
709789Sahrens 	blkptr_t blk;
710789Sahrens 
7115329Sgw25295 	ZIO_ENTER(vd->vdev_spa);
7125329Sgw25295 
713789Sahrens 	zio_phys_bp_init(vd, &blk, offset, size, checksum);
714789Sahrens 
715789Sahrens 	zio = zio_create(pio, vd->vdev_spa, 0, &blk, data, size, done, private,
716789Sahrens 	    ZIO_TYPE_READ, priority, flags | ZIO_FLAG_PHYSICAL,
717789Sahrens 	    ZIO_STAGE_OPEN, ZIO_READ_PHYS_PIPELINE);
718789Sahrens 
719789Sahrens 	zio->io_vd = vd;
720789Sahrens 	zio->io_offset = offset;
721789Sahrens 
722789Sahrens 	/*
723789Sahrens 	 * Work off our copy of the bp so the caller can free it.
724789Sahrens 	 */
725789Sahrens 	zio->io_bp = &zio->io_bp_copy;
726789Sahrens 
727789Sahrens 	return (zio);
728789Sahrens }
729789Sahrens 
730789Sahrens zio_t *
731789Sahrens zio_write_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
732789Sahrens     void *data, int checksum, zio_done_func_t *done, void *private,
733789Sahrens     int priority, int flags)
734789Sahrens {
735789Sahrens 	zio_block_tail_t *zbt;
736789Sahrens 	void *wbuf;
737789Sahrens 	zio_t *zio;
738789Sahrens 	blkptr_t blk;
739789Sahrens 
7405329Sgw25295 	ZIO_ENTER(vd->vdev_spa);
7415329Sgw25295 
742789Sahrens 	zio_phys_bp_init(vd, &blk, offset, size, checksum);
743789Sahrens 
744789Sahrens 	zio = zio_create(pio, vd->vdev_spa, 0, &blk, data, size, done, private,
745789Sahrens 	    ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_PHYSICAL,
746789Sahrens 	    ZIO_STAGE_OPEN, ZIO_WRITE_PHYS_PIPELINE);
747789Sahrens 
748789Sahrens 	zio->io_vd = vd;
749789Sahrens 	zio->io_offset = offset;
750789Sahrens 
751789Sahrens 	zio->io_bp = &zio->io_bp_copy;
752789Sahrens 	zio->io_checksum = checksum;
753789Sahrens 
754789Sahrens 	if (zio_checksum_table[checksum].ci_zbt) {
755789Sahrens 		/*
756789Sahrens 		 * zbt checksums are necessarily destructive -- they modify
757789Sahrens 		 * one word of the write buffer to hold the verifier/checksum.
758789Sahrens 		 * Therefore, we must make a local copy in case the data is
759789Sahrens 		 * being written to multiple places.
760789Sahrens 		 */
761789Sahrens 		wbuf = zio_buf_alloc(size);
762789Sahrens 		bcopy(data, wbuf, size);
763789Sahrens 		zio_push_transform(zio, wbuf, size, size);
764789Sahrens 
765789Sahrens 		zbt = (zio_block_tail_t *)((char *)wbuf + size) - 1;
766789Sahrens 		zbt->zbt_cksum = blk.blk_cksum;
767789Sahrens 	}
768789Sahrens 
769789Sahrens 	return (zio);
770789Sahrens }
771789Sahrens 
772789Sahrens /*
773789Sahrens  * Create a child I/O to do some work for us.  It has no associated bp.
774789Sahrens  */
775789Sahrens zio_t *
776789Sahrens zio_vdev_child_io(zio_t *zio, blkptr_t *bp, vdev_t *vd, uint64_t offset,
777789Sahrens 	void *data, uint64_t size, int type, int priority, int flags,
778789Sahrens 	zio_done_func_t *done, void *private)
779789Sahrens {
780789Sahrens 	uint32_t pipeline = ZIO_VDEV_CHILD_PIPELINE;
781789Sahrens 	zio_t *cio;
782789Sahrens 
783789Sahrens 	if (type == ZIO_TYPE_READ && bp != NULL) {
784789Sahrens 		/*
785789Sahrens 		 * If we have the bp, then the child should perform the
786789Sahrens 		 * checksum and the parent need not.  This pushes error
787789Sahrens 		 * detection as close to the leaves as possible and
788789Sahrens 		 * eliminates redundant checksums in the interior nodes.
789789Sahrens 		 */
790789Sahrens 		pipeline |= 1U << ZIO_STAGE_CHECKSUM_VERIFY;
791789Sahrens 		zio->io_pipeline &= ~(1U << ZIO_STAGE_CHECKSUM_VERIFY);
792789Sahrens 	}
793789Sahrens 
794789Sahrens 	cio = zio_create(zio, zio->io_spa, zio->io_txg, bp, data, size,
795789Sahrens 	    done, private, type, priority,
796789Sahrens 	    (zio->io_flags & ZIO_FLAG_VDEV_INHERIT) | ZIO_FLAG_CANFAIL | flags,
7971775Sbillm 	    ZIO_STAGE_VDEV_IO_START - 1, pipeline);
798789Sahrens 
799789Sahrens 	cio->io_vd = vd;
800789Sahrens 	cio->io_offset = offset;
801789Sahrens 
802789Sahrens 	return (cio);
803789Sahrens }
804789Sahrens 
805789Sahrens /*
806789Sahrens  * ==========================================================================
807789Sahrens  * Initiate I/O, either sync or async
808789Sahrens  * ==========================================================================
809789Sahrens  */
810789Sahrens int
811789Sahrens zio_wait(zio_t *zio)
812789Sahrens {
813789Sahrens 	int error;
814789Sahrens 
815789Sahrens 	ASSERT(zio->io_stage == ZIO_STAGE_OPEN);
816789Sahrens 
817789Sahrens 	zio->io_waiter = curthread;
818789Sahrens 
819789Sahrens 	zio_next_stage_async(zio);
820789Sahrens 
821789Sahrens 	mutex_enter(&zio->io_lock);
822789Sahrens 	while (zio->io_stalled != ZIO_STAGE_DONE)
823789Sahrens 		cv_wait(&zio->io_cv, &zio->io_lock);
824789Sahrens 	mutex_exit(&zio->io_lock);
825789Sahrens 
826789Sahrens 	error = zio->io_error;
8272856Snd150628 	mutex_destroy(&zio->io_lock);
8284831Sgw25295 	cv_destroy(&zio->io_cv);
8294055Seschrock 	kmem_cache_free(zio_cache, zio);
830789Sahrens 
831789Sahrens 	return (error);
832789Sahrens }
833789Sahrens 
834789Sahrens void
835789Sahrens zio_nowait(zio_t *zio)
836789Sahrens {
837789Sahrens 	zio_next_stage_async(zio);
838789Sahrens }
839789Sahrens 
840789Sahrens /*
841789Sahrens  * ==========================================================================
842789Sahrens  * I/O pipeline interlocks: parent/child dependency scoreboarding
843789Sahrens  * ==========================================================================
844789Sahrens  */
845789Sahrens static void
846789Sahrens zio_wait_for_children(zio_t *zio, uint32_t stage, uint64_t *countp)
847789Sahrens {
848789Sahrens 	mutex_enter(&zio->io_lock);
849789Sahrens 	if (*countp == 0) {
850789Sahrens 		ASSERT(zio->io_stalled == 0);
851789Sahrens 		mutex_exit(&zio->io_lock);
852789Sahrens 		zio_next_stage(zio);
853789Sahrens 	} else {
854789Sahrens 		zio->io_stalled = stage;
855789Sahrens 		mutex_exit(&zio->io_lock);
856789Sahrens 	}
857789Sahrens }
858789Sahrens 
859789Sahrens static void
860789Sahrens zio_notify_parent(zio_t *zio, uint32_t stage, uint64_t *countp)
861789Sahrens {
862789Sahrens 	zio_t *pio = zio->io_parent;
863789Sahrens 
864789Sahrens 	mutex_enter(&pio->io_lock);
865789Sahrens 	if (pio->io_error == 0 && !(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
866789Sahrens 		pio->io_error = zio->io_error;
8675329Sgw25295 	ASSERT3U(*countp, >, 0);
868789Sahrens 	if (--*countp == 0 && pio->io_stalled == stage) {
869789Sahrens 		pio->io_stalled = 0;
870789Sahrens 		mutex_exit(&pio->io_lock);
871789Sahrens 		zio_next_stage_async(pio);
872789Sahrens 	} else {
873789Sahrens 		mutex_exit(&pio->io_lock);
874789Sahrens 	}
875789Sahrens }
876789Sahrens 
877789Sahrens static void
878789Sahrens zio_wait_children_ready(zio_t *zio)
879789Sahrens {
880789Sahrens 	zio_wait_for_children(zio, ZIO_STAGE_WAIT_CHILDREN_READY,
881789Sahrens 	    &zio->io_children_notready);
882789Sahrens }
883789Sahrens 
884789Sahrens void
885789Sahrens zio_wait_children_done(zio_t *zio)
886789Sahrens {
887789Sahrens 	zio_wait_for_children(zio, ZIO_STAGE_WAIT_CHILDREN_DONE,
888789Sahrens 	    &zio->io_children_notdone);
889789Sahrens }
890789Sahrens 
891789Sahrens static void
8925329Sgw25295 zio_read_init(zio_t *zio)
8935329Sgw25295 {
8945329Sgw25295 	if (BP_GET_COMPRESS(zio->io_bp) != ZIO_COMPRESS_OFF) {
8955329Sgw25295 		uint64_t csize = BP_GET_PSIZE(zio->io_bp);
8965329Sgw25295 		void *cbuf = zio_buf_alloc(csize);
8975329Sgw25295 
8985329Sgw25295 		zio_push_transform(zio, cbuf, csize, csize);
8995329Sgw25295 		zio->io_pipeline |= 1U << ZIO_STAGE_READ_DECOMPRESS;
9005329Sgw25295 	}
9015329Sgw25295 
9025329Sgw25295 	if (BP_IS_GANG(zio->io_bp)) {
9035329Sgw25295 		uint64_t gsize = SPA_GANGBLOCKSIZE;
9045329Sgw25295 		void *gbuf = zio_buf_alloc(gsize);
9055329Sgw25295 
9065329Sgw25295 		zio_push_transform(zio, gbuf, gsize, gsize);
9075329Sgw25295 		zio->io_pipeline |= 1U << ZIO_STAGE_READ_GANG_MEMBERS;
9085329Sgw25295 	}
9095329Sgw25295 	zio_next_stage(zio);
9105329Sgw25295 }
9115329Sgw25295 
9125329Sgw25295 static void
913789Sahrens zio_ready(zio_t *zio)
914789Sahrens {
915789Sahrens 	zio_t *pio = zio->io_parent;
916789Sahrens 
9173547Smaybee 	if (zio->io_ready)
9183547Smaybee 		zio->io_ready(zio);
9193547Smaybee 
920789Sahrens 	if (pio != NULL)
921789Sahrens 		zio_notify_parent(zio, ZIO_STAGE_WAIT_CHILDREN_READY,
922789Sahrens 		    &pio->io_children_notready);
923789Sahrens 
924789Sahrens 	if (zio->io_bp)
925789Sahrens 		zio->io_bp_copy = *zio->io_bp;
926789Sahrens 
927789Sahrens 	zio_next_stage(zio);
928789Sahrens }
929789Sahrens 
930789Sahrens static void
9315329Sgw25295 zio_vdev_retry_io(zio_t *zio)
932789Sahrens {
933789Sahrens 	zio_t *pio = zio->io_parent;
9345329Sgw25295 
9355329Sgw25295 	/*
9365329Sgw25295 	 * Preserve the failed bp so that the io_ready() callback can
9375329Sgw25295 	 * update the accounting accordingly. The callback will also be
9385329Sgw25295 	 * responsible for freeing the previously allocated block, if one
9395329Sgw25295 	 * exists.
9405329Sgw25295 	 */
9415329Sgw25295 	zio->io_bp_orig = *zio->io_bp;
9425329Sgw25295 
9435329Sgw25295 	/*
9445329Sgw25295 	 * We must zero out the old DVA and blk_birth before reallocating
945*5403Sgw25295 	 * the bp.
9465329Sgw25295 	 */
947*5403Sgw25295 	BP_ZERO_DVAS(zio->io_bp);
9485329Sgw25295 	zio_reset(zio);
9495329Sgw25295 
9505329Sgw25295 	if (pio) {
9515329Sgw25295 		/*
9525329Sgw25295 		 * Let the parent know that we will
9535329Sgw25295 		 * re-alloc the write (=> new bp info).
9545329Sgw25295 		 */
9555329Sgw25295 		mutex_enter(&pio->io_lock);
9565329Sgw25295 		pio->io_children_notready++;
9575329Sgw25295 
9585329Sgw25295 		/*
9595329Sgw25295 		 * If the parent I/O is still in the open stage, then
9605329Sgw25295 		 * don't bother telling it to retry since it hasn't
9615329Sgw25295 		 * progressed far enough for it to care.
9625329Sgw25295 		 */
9635329Sgw25295 		if (pio->io_stage > ZIO_STAGE_OPEN && IO_IS_ALLOCATING(pio))
9645329Sgw25295 			pio->io_flags |= ZIO_FLAG_WRITE_RETRY;
9655329Sgw25295 
9665329Sgw25295 		ASSERT(pio->io_stage <= ZIO_STAGE_WAIT_CHILDREN_DONE);
9675329Sgw25295 		mutex_exit(&pio->io_lock);
9685329Sgw25295 	}
9695329Sgw25295 
9705329Sgw25295 	/*
9715329Sgw25295 	 * We are getting ready to process the retry request so clear
9725329Sgw25295 	 * the flag and the zio's current error status.
9735329Sgw25295 	 */
9745329Sgw25295 	zio->io_flags &= ~ZIO_FLAG_WRITE_RETRY;
9755329Sgw25295 	zio->io_error = 0;
9765329Sgw25295 	zio_next_stage_async(zio);
9775329Sgw25295 }
9785329Sgw25295 
9795329Sgw25295 int
9805329Sgw25295 zio_vdev_resume_io(spa_t *spa)
9815329Sgw25295 {
9825329Sgw25295 	zio_t *zio;
9835329Sgw25295 
9845329Sgw25295 	mutex_enter(&spa->spa_zio_lock);
9855329Sgw25295 
9865329Sgw25295 	/*
9875329Sgw25295 	 * Probe all of vdevs that have experienced an I/O error.
9885329Sgw25295 	 * If we are still unable to verify the integrity of the vdev
9895329Sgw25295 	 * then we prevent the resume from proceeeding.
9905329Sgw25295 	 */
9915329Sgw25295 	for (zio = list_head(&spa->spa_zio_list); zio != NULL;
9925329Sgw25295 	    zio = list_next(&spa->spa_zio_list, zio)) {
9935329Sgw25295 		int error = 0;
9945329Sgw25295 
9955329Sgw25295 		/* We only care about I/Os that must succeed */
9965329Sgw25295 		if (zio->io_vd == NULL || zio->io_flags & ZIO_FLAG_CANFAIL)
9975329Sgw25295 			continue;
9985329Sgw25295 		error = vdev_probe(zio->io_vd);
9995329Sgw25295 		if (error) {
10005329Sgw25295 			mutex_exit(&spa->spa_zio_lock);
10015329Sgw25295 			return (error);
10025329Sgw25295 		}
10035329Sgw25295 	}
10045329Sgw25295 
10055329Sgw25295 	/*
10065329Sgw25295 	 * Clear the vdev stats so that I/O can flow.
10075329Sgw25295 	 */
10085329Sgw25295 	vdev_clear(spa, NULL, B_FALSE);
10095329Sgw25295 
10105329Sgw25295 	spa->spa_state = POOL_STATE_ACTIVE;
10115329Sgw25295 	while ((zio = list_head(&spa->spa_zio_list)) != NULL) {
10125329Sgw25295 		list_remove(&spa->spa_zio_list, zio);
10135329Sgw25295 		zio->io_error = 0;
10145329Sgw25295 
10155329Sgw25295 		/*
10165329Sgw25295 		 * If we are resuming an allocating I/O then we force it
10175329Sgw25295 		 * to retry and let it resume operation where it left off.
10185329Sgw25295 		 * Otherwise, go back to the ready stage and pick up from
10195329Sgw25295 		 * there.
10205329Sgw25295 		 */
10215329Sgw25295 		if (zio_write_retry && IO_IS_ALLOCATING(zio)) {
10225329Sgw25295 			zio->io_flags |= ZIO_FLAG_WRITE_RETRY;
10235329Sgw25295 			zio->io_stage--;
10245329Sgw25295 		} else {
10255329Sgw25295 			zio->io_stage = ZIO_STAGE_READY;
10265329Sgw25295 		}
10275329Sgw25295 
10285329Sgw25295 		(void) taskq_dispatch(zio_taskq, zio_resubmit_stage_async,
10295329Sgw25295 		    zio, TQ_SLEEP);
10305329Sgw25295 	}
10315329Sgw25295 	mutex_exit(&spa->spa_zio_lock);
10325329Sgw25295 
10335329Sgw25295 	/*
10345329Sgw25295 	 * Wait for the taskqs to finish and recheck the pool state since
10355329Sgw25295 	 * it's possible that a resumed I/O has failed again.
10365329Sgw25295 	 */
10375329Sgw25295 	taskq_wait(zio_taskq);
10385329Sgw25295 	if (spa_state(spa) == POOL_STATE_IO_FAILURE)
10395329Sgw25295 		return (EIO);
10405329Sgw25295 
10415329Sgw25295 	mutex_enter(&spa->spa_zio_lock);
10425329Sgw25295 	cv_broadcast(&spa->spa_zio_cv);
10435329Sgw25295 	mutex_exit(&spa->spa_zio_lock);
10445329Sgw25295 
10455329Sgw25295 	return (0);
10465329Sgw25295 }
10475329Sgw25295 
10485329Sgw25295 static void
10495329Sgw25295 zio_vdev_suspend_io(zio_t *zio)
10505329Sgw25295 {
10515329Sgw25295 	spa_t *spa = zio->io_spa;
10525329Sgw25295 
10535329Sgw25295 	/*
10545329Sgw25295 	 * We've experienced an unrecoverable failure so
10555329Sgw25295 	 * set the pool state accordingly and queue all
10565329Sgw25295 	 * failed IOs.
10575329Sgw25295 	 */
10585329Sgw25295 	spa->spa_state = POOL_STATE_IO_FAILURE;
10595329Sgw25295 
10605329Sgw25295 	mutex_enter(&spa->spa_zio_lock);
10615329Sgw25295 	list_insert_tail(&spa->spa_zio_list, zio);
10625329Sgw25295 
10635329Sgw25295 #ifndef _KERNEL
10645329Sgw25295 	/* Used to notify ztest that the pool has suspended */
10655329Sgw25295 	cv_broadcast(&spa->spa_zio_cv);
10665329Sgw25295 #endif
10675329Sgw25295 	mutex_exit(&spa->spa_zio_lock);
10685329Sgw25295 }
10695329Sgw25295 
10705329Sgw25295 static void
10715329Sgw25295 zio_assess(zio_t *zio)
10725329Sgw25295 {
1073789Sahrens 	spa_t *spa = zio->io_spa;
1074789Sahrens 	blkptr_t *bp = zio->io_bp;
1075789Sahrens 	vdev_t *vd = zio->io_vd;
1076789Sahrens 
1077789Sahrens 	ASSERT(zio->io_children_notready == 0);
1078789Sahrens 	ASSERT(zio->io_children_notdone == 0);
1079789Sahrens 
1080789Sahrens 	if (bp != NULL) {
1081789Sahrens 		ASSERT(bp->blk_pad[0] == 0);
1082789Sahrens 		ASSERT(bp->blk_pad[1] == 0);
1083789Sahrens 		ASSERT(bp->blk_pad[2] == 0);
1084789Sahrens 		ASSERT(bcmp(bp, &zio->io_bp_copy, sizeof (blkptr_t)) == 0);
1085789Sahrens 		if (zio->io_type == ZIO_TYPE_WRITE && !BP_IS_HOLE(bp) &&
10861775Sbillm 		    !(zio->io_flags & ZIO_FLAG_IO_REPAIR)) {
1087789Sahrens 			ASSERT(!BP_SHOULD_BYTESWAP(bp));
10881775Sbillm 			if (zio->io_ndvas != 0)
10891775Sbillm 				ASSERT3U(zio->io_ndvas, <=, BP_GET_NDVAS(bp));
10901775Sbillm 			ASSERT(BP_COUNT_GANG(bp) == 0 ||
10911775Sbillm 			    (BP_COUNT_GANG(bp) == BP_GET_NDVAS(bp)));
10921775Sbillm 		}
1093789Sahrens 	}
1094789Sahrens 
10955329Sgw25295 	/*
10965329Sgw25295 	 * Some child I/O has indicated that a retry is necessary, so
10975329Sgw25295 	 * we set an error on the I/O and let the logic below do the
10985329Sgw25295 	 * rest.
10995329Sgw25295 	 */
11005329Sgw25295 	if (zio->io_flags & ZIO_FLAG_WRITE_RETRY)
11015329Sgw25295 		zio->io_error = ERESTART;
11025329Sgw25295 
1103789Sahrens 	if (vd != NULL)
1104789Sahrens 		vdev_stat_update(zio);
1105789Sahrens 
1106789Sahrens 	if (zio->io_error) {
11071544Seschrock 		/*
11081544Seschrock 		 * If this I/O is attached to a particular vdev,
11091544Seschrock 		 * generate an error message describing the I/O failure
11101544Seschrock 		 * at the block level.  We ignore these errors if the
11111544Seschrock 		 * device is currently unavailable.
11121544Seschrock 		 */
11131732Sbonwick 		if (zio->io_error != ECKSUM && vd != NULL && !vdev_is_dead(vd))
11145329Sgw25295 			zfs_ereport_post(FM_EREPORT_ZFS_IO, spa, vd, zio, 0, 0);
1115789Sahrens 
11161544Seschrock 		if ((zio->io_error == EIO ||
11171544Seschrock 		    !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) &&
11181544Seschrock 		    zio->io_logical == zio) {
11191544Seschrock 			/*
11201544Seschrock 			 * For root I/O requests, tell the SPA to log the error
11211544Seschrock 			 * appropriately.  Also, generate a logical data
11221544Seschrock 			 * ereport.
11231544Seschrock 			 */
11245329Sgw25295 			spa_log_error(spa, zio);
11251544Seschrock 
11265329Sgw25295 			zfs_ereport_post(FM_EREPORT_ZFS_DATA, spa, NULL, zio,
11275329Sgw25295 			    0, 0);
11281544Seschrock 		}
1129789Sahrens 
11301544Seschrock 		/*
1131*5403Sgw25295 		 * If we are an allocating I/O then we attempt to reissue
1132*5403Sgw25295 		 * the I/O on another vdev unless the pool is out of space.
1133*5403Sgw25295 		 * We handle this condition based on the spa's failmode
1134*5403Sgw25295 		 * property.
11355329Sgw25295 		 */
11365329Sgw25295 		if (zio_write_retry && zio->io_error != ENOSPC &&
1137*5403Sgw25295 		    IO_IS_ALLOCATING(zio)) {
11385329Sgw25295 			zio_vdev_retry_io(zio);
11395329Sgw25295 			return;
11405329Sgw25295 		}
11415329Sgw25295 		ASSERT(!(zio->io_flags & ZIO_FLAG_WRITE_RETRY));
11425329Sgw25295 
11435329Sgw25295 		/*
11445329Sgw25295 		 * For I/O requests that cannot fail, we carry out
11455329Sgw25295 		 * the requested behavior based on the failmode pool
11465329Sgw25295 		 * property.
11475329Sgw25295 		 *
11485329Sgw25295 		 * XXX - Need to differentiate between an ENOSPC as
11495329Sgw25295 		 * a result of vdev failures vs. a full pool.
11501544Seschrock 		 */
11511544Seschrock 		if (!(zio->io_flags & ZIO_FLAG_CANFAIL)) {
11523459Sek110237 			char *blkbuf;
11533459Sek110237 
11545329Sgw25295 #ifdef ZFS_DEBUG
11553459Sek110237 			blkbuf = kmem_alloc(BP_SPRINTF_LEN, KM_NOSLEEP);
11563459Sek110237 			if (blkbuf) {
11573459Sek110237 				sprintf_blkptr(blkbuf, BP_SPRINTF_LEN,
11583459Sek110237 				    bp ? bp : &zio->io_bp_copy);
11593459Sek110237 			}
11605329Sgw25295 			cmn_err(CE_WARN, "ZFS: %s (%s on %s off %llx: zio %p "
11615329Sgw25295 			    "%s): error %d", zio->io_error == ECKSUM ?
11621544Seschrock 			    "bad checksum" : "I/O failure",
11631544Seschrock 			    zio_type_name[zio->io_type],
11641544Seschrock 			    vdev_description(vd),
11651544Seschrock 			    (u_longlong_t)zio->io_offset,
11665329Sgw25295 			    (void *)zio, blkbuf ? blkbuf : "", zio->io_error);
11675329Sgw25295 #endif
11685329Sgw25295 
11695329Sgw25295 			if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_PANIC) {
11705329Sgw25295 				fm_panic("Pool '%s' has encountered an "
11715329Sgw25295 				    "uncorrectable I/O failure and the "
11725329Sgw25295 				    "failure mode property for this pool "
11735329Sgw25295 				    "is set to panic.", spa_name(spa));
11745329Sgw25295 			} else {
11755329Sgw25295 				cmn_err(CE_WARN, "Pool '%s' has encountered "
11765329Sgw25295 				    "an uncorrectable I/O error. Manual "
11775329Sgw25295 				    "intervention is required.",
11785329Sgw25295 				    spa_name(spa));
11795329Sgw25295 				zio_vdev_suspend_io(zio);
11805329Sgw25295 			}
11815329Sgw25295 			return;
11821544Seschrock 		}
1183789Sahrens 	}
11845329Sgw25295 	ASSERT(!(zio->io_flags & ZIO_FLAG_WRITE_RETRY));
11855329Sgw25295 	ASSERT(zio->io_children_notready == 0);
11865329Sgw25295 	zio_next_stage(zio);
11875329Sgw25295 }
11885329Sgw25295 
11895329Sgw25295 static void
11905329Sgw25295 zio_done(zio_t *zio)
11915329Sgw25295 {
11925329Sgw25295 	zio_t *pio = zio->io_parent;
11935329Sgw25295 	spa_t *spa = zio->io_spa;
11945329Sgw25295 
11955329Sgw25295 	ASSERT(zio->io_children_notready == 0);
11965329Sgw25295 	ASSERT(zio->io_children_notdone == 0);
11975329Sgw25295 
1198789Sahrens 	zio_clear_transform_stack(zio);
1199789Sahrens 
1200789Sahrens 	if (zio->io_done)
1201789Sahrens 		zio->io_done(zio);
1202789Sahrens 
1203789Sahrens 	ASSERT(zio->io_delegate_list == NULL);
1204789Sahrens 	ASSERT(zio->io_delegate_next == NULL);
1205789Sahrens 
1206789Sahrens 	if (pio != NULL) {
1207789Sahrens 		zio_t *next, *prev;
1208789Sahrens 
1209789Sahrens 		mutex_enter(&pio->io_lock);
1210789Sahrens 		next = zio->io_sibling_next;
1211789Sahrens 		prev = zio->io_sibling_prev;
1212789Sahrens 		if (next != NULL)
1213789Sahrens 			next->io_sibling_prev = prev;
1214789Sahrens 		if (prev != NULL)
1215789Sahrens 			prev->io_sibling_next = next;
1216789Sahrens 		if (pio->io_child == zio)
1217789Sahrens 			pio->io_child = next;
1218789Sahrens 		mutex_exit(&pio->io_lock);
1219789Sahrens 
1220789Sahrens 		zio_notify_parent(zio, ZIO_STAGE_WAIT_CHILDREN_DONE,
1221789Sahrens 		    &pio->io_children_notdone);
1222789Sahrens 	}
1223789Sahrens 
12243463Sahrens 	/*
12254055Seschrock 	 * Note: this I/O is now done, and will shortly be freed, so there is no
12264055Seschrock 	 * need to clear this (or any other) flag.
12273463Sahrens 	 */
12283463Sahrens 	if (zio->io_flags & ZIO_FLAG_CONFIG_GRABBED)
12291544Seschrock 		spa_config_exit(spa, zio);
1230789Sahrens 
1231789Sahrens 	if (zio->io_waiter != NULL) {
1232789Sahrens 		mutex_enter(&zio->io_lock);
1233789Sahrens 		ASSERT(zio->io_stage == ZIO_STAGE_DONE);
1234789Sahrens 		zio->io_stalled = zio->io_stage;
1235789Sahrens 		cv_broadcast(&zio->io_cv);
1236789Sahrens 		mutex_exit(&zio->io_lock);
1237789Sahrens 	} else {
12384831Sgw25295 		mutex_destroy(&zio->io_lock);
12394831Sgw25295 		cv_destroy(&zio->io_cv);
12404055Seschrock 		kmem_cache_free(zio_cache, zio);
1241789Sahrens 	}
1242789Sahrens }
1243789Sahrens 
1244789Sahrens /*
1245789Sahrens  * ==========================================================================
1246789Sahrens  * Compression support
1247789Sahrens  * ==========================================================================
1248789Sahrens  */
1249789Sahrens static void
1250789Sahrens zio_write_compress(zio_t *zio)
1251789Sahrens {
1252789Sahrens 	int compress = zio->io_compress;
1253789Sahrens 	blkptr_t *bp = zio->io_bp;
1254789Sahrens 	void *cbuf;
1255789Sahrens 	uint64_t lsize = zio->io_size;
1256789Sahrens 	uint64_t csize = lsize;
1257789Sahrens 	uint64_t cbufsize = 0;
1258789Sahrens 	int pass;
1259789Sahrens 
1260789Sahrens 	if (bp->blk_birth == zio->io_txg) {
1261789Sahrens 		/*
1262789Sahrens 		 * We're rewriting an existing block, which means we're
1263789Sahrens 		 * working on behalf of spa_sync().  For spa_sync() to
1264789Sahrens 		 * converge, it must eventually be the case that we don't
1265789Sahrens 		 * have to allocate new blocks.  But compression changes
1266789Sahrens 		 * the blocksize, which forces a reallocate, and makes
1267789Sahrens 		 * convergence take longer.  Therefore, after the first
1268789Sahrens 		 * few passes, stop compressing to ensure convergence.
1269789Sahrens 		 */
1270789Sahrens 		pass = spa_sync_pass(zio->io_spa);
1271789Sahrens 		if (pass > zio_sync_pass.zp_dontcompress)
1272789Sahrens 			compress = ZIO_COMPRESS_OFF;
1273789Sahrens 	} else {
1274789Sahrens 		ASSERT(BP_IS_HOLE(bp));
1275789Sahrens 		pass = 1;
1276789Sahrens 	}
1277789Sahrens 
1278789Sahrens 	if (compress != ZIO_COMPRESS_OFF)
1279789Sahrens 		if (!zio_compress_data(compress, zio->io_data, zio->io_size,
1280789Sahrens 		    &cbuf, &csize, &cbufsize))
1281789Sahrens 			compress = ZIO_COMPRESS_OFF;
1282789Sahrens 
1283789Sahrens 	if (compress != ZIO_COMPRESS_OFF && csize != 0)
1284789Sahrens 		zio_push_transform(zio, cbuf, csize, cbufsize);
1285789Sahrens 
1286789Sahrens 	/*
1287789Sahrens 	 * The final pass of spa_sync() must be all rewrites, but the first
1288789Sahrens 	 * few passes offer a trade-off: allocating blocks defers convergence,
1289789Sahrens 	 * but newly allocated blocks are sequential, so they can be written
1290789Sahrens 	 * to disk faster.  Therefore, we allow the first few passes of
1291789Sahrens 	 * spa_sync() to reallocate new blocks, but force rewrites after that.
1292789Sahrens 	 * There should only be a handful of blocks after pass 1 in any case.
1293789Sahrens 	 */
1294789Sahrens 	if (bp->blk_birth == zio->io_txg && BP_GET_PSIZE(bp) == csize &&
1295789Sahrens 	    pass > zio_sync_pass.zp_rewrite) {
1296789Sahrens 		ASSERT(csize != 0);
12972885Sahrens 		BP_SET_LSIZE(bp, lsize);
12982885Sahrens 		BP_SET_COMPRESS(bp, compress);
1299789Sahrens 		zio->io_pipeline = ZIO_REWRITE_PIPELINE;
1300789Sahrens 	} else {
13013882Sahrens 		if (bp->blk_birth == zio->io_txg)
13023882Sahrens 			BP_ZERO(bp);
1303789Sahrens 		if (csize == 0) {
1304789Sahrens 			BP_ZERO(bp);
1305789Sahrens 			zio->io_pipeline = ZIO_WAIT_FOR_CHILDREN_PIPELINE;
1306789Sahrens 		} else {
13071775Sbillm 			ASSERT3U(BP_GET_NDVAS(bp), ==, 0);
1308789Sahrens 			BP_SET_LSIZE(bp, lsize);
1309789Sahrens 			BP_SET_PSIZE(bp, csize);
1310789Sahrens 			BP_SET_COMPRESS(bp, compress);
1311789Sahrens 			zio->io_pipeline = ZIO_WRITE_ALLOCATE_PIPELINE;
1312789Sahrens 		}
1313789Sahrens 	}
1314789Sahrens 
1315789Sahrens 	zio_next_stage(zio);
1316789Sahrens }
1317789Sahrens 
1318789Sahrens static void
1319789Sahrens zio_read_decompress(zio_t *zio)
1320789Sahrens {
1321789Sahrens 	blkptr_t *bp = zio->io_bp;
1322789Sahrens 	void *data;
1323789Sahrens 	uint64_t size;
1324789Sahrens 	uint64_t bufsize;
1325789Sahrens 	int compress = BP_GET_COMPRESS(bp);
1326789Sahrens 
1327789Sahrens 	ASSERT(compress != ZIO_COMPRESS_OFF);
1328789Sahrens 
1329789Sahrens 	zio_pop_transform(zio, &data, &size, &bufsize);
1330789Sahrens 
1331789Sahrens 	if (zio_decompress_data(compress, data, size,
1332789Sahrens 	    zio->io_data, zio->io_size))
1333789Sahrens 		zio->io_error = EIO;
1334789Sahrens 
1335789Sahrens 	zio_buf_free(data, bufsize);
1336789Sahrens 
1337789Sahrens 	zio_next_stage(zio);
1338789Sahrens }
1339789Sahrens 
1340789Sahrens /*
1341789Sahrens  * ==========================================================================
1342789Sahrens  * Gang block support
1343789Sahrens  * ==========================================================================
1344789Sahrens  */
1345789Sahrens static void
1346789Sahrens zio_gang_pipeline(zio_t *zio)
1347789Sahrens {
1348789Sahrens 	/*
1349789Sahrens 	 * By default, the pipeline assumes that we're dealing with a gang
1350789Sahrens 	 * block.  If we're not, strip out any gang-specific stages.
1351789Sahrens 	 */
13521775Sbillm 	if (!BP_IS_GANG(zio->io_bp))
1353789Sahrens 		zio->io_pipeline &= ~ZIO_GANG_STAGES;
1354789Sahrens 
1355789Sahrens 	zio_next_stage(zio);
1356789Sahrens }
1357789Sahrens 
1358789Sahrens static void
1359789Sahrens zio_gang_byteswap(zio_t *zio)
1360789Sahrens {
1361789Sahrens 	ASSERT(zio->io_size == SPA_GANGBLOCKSIZE);
1362789Sahrens 
1363789Sahrens 	if (BP_SHOULD_BYTESWAP(zio->io_bp))
1364789Sahrens 		byteswap_uint64_array(zio->io_data, zio->io_size);
1365789Sahrens }
1366789Sahrens 
1367789Sahrens static void
1368789Sahrens zio_get_gang_header(zio_t *zio)
1369789Sahrens {
1370789Sahrens 	blkptr_t *bp = zio->io_bp;
1371789Sahrens 	uint64_t gsize = SPA_GANGBLOCKSIZE;
1372789Sahrens 	void *gbuf = zio_buf_alloc(gsize);
1373789Sahrens 
13741775Sbillm 	ASSERT(BP_IS_GANG(bp));
1375789Sahrens 
1376789Sahrens 	zio_push_transform(zio, gbuf, gsize, gsize);
1377789Sahrens 
1378789Sahrens 	zio_nowait(zio_create(zio, zio->io_spa, bp->blk_birth, bp, gbuf, gsize,
1379789Sahrens 	    NULL, NULL, ZIO_TYPE_READ, zio->io_priority,
1380789Sahrens 	    zio->io_flags & ZIO_FLAG_GANG_INHERIT,
13815329Sgw25295 	    ZIO_STAGE_OPEN, ZIO_READ_GANG_PIPELINE));
1382789Sahrens 
1383789Sahrens 	zio_wait_children_done(zio);
1384789Sahrens }
1385789Sahrens 
1386789Sahrens static void
1387789Sahrens zio_read_gang_members(zio_t *zio)
1388789Sahrens {
1389789Sahrens 	zio_gbh_phys_t *gbh;
1390789Sahrens 	uint64_t gsize, gbufsize, loff, lsize;
1391789Sahrens 	int i;
1392789Sahrens 
13931775Sbillm 	ASSERT(BP_IS_GANG(zio->io_bp));
1394789Sahrens 
1395789Sahrens 	zio_gang_byteswap(zio);
1396789Sahrens 	zio_pop_transform(zio, (void **)&gbh, &gsize, &gbufsize);
1397789Sahrens 
1398789Sahrens 	for (loff = 0, i = 0; loff != zio->io_size; loff += lsize, i++) {
1399789Sahrens 		blkptr_t *gbp = &gbh->zg_blkptr[i];
1400789Sahrens 		lsize = BP_GET_PSIZE(gbp);
1401789Sahrens 
1402789Sahrens 		ASSERT(BP_GET_COMPRESS(gbp) == ZIO_COMPRESS_OFF);
1403789Sahrens 		ASSERT3U(lsize, ==, BP_GET_LSIZE(gbp));
1404789Sahrens 		ASSERT3U(loff + lsize, <=, zio->io_size);
1405789Sahrens 		ASSERT(i < SPA_GBH_NBLKPTRS);
1406789Sahrens 		ASSERT(!BP_IS_HOLE(gbp));
1407789Sahrens 
1408789Sahrens 		zio_nowait(zio_read(zio, zio->io_spa, gbp,
1409789Sahrens 		    (char *)zio->io_data + loff, lsize, NULL, NULL,
14101544Seschrock 		    zio->io_priority, zio->io_flags & ZIO_FLAG_GANG_INHERIT,
14111544Seschrock 		    &zio->io_bookmark));
1412789Sahrens 	}
1413789Sahrens 
1414789Sahrens 	zio_buf_free(gbh, gbufsize);
1415789Sahrens 	zio_wait_children_done(zio);
1416789Sahrens }
1417789Sahrens 
1418789Sahrens static void
1419789Sahrens zio_rewrite_gang_members(zio_t *zio)
1420789Sahrens {
1421789Sahrens 	zio_gbh_phys_t *gbh;
1422789Sahrens 	uint64_t gsize, gbufsize, loff, lsize;
1423789Sahrens 	int i;
1424789Sahrens 
14251775Sbillm 	ASSERT(BP_IS_GANG(zio->io_bp));
1426789Sahrens 	ASSERT3U(zio->io_size, ==, SPA_GANGBLOCKSIZE);
1427789Sahrens 
1428789Sahrens 	zio_gang_byteswap(zio);
1429789Sahrens 	zio_pop_transform(zio, (void **)&gbh, &gsize, &gbufsize);
1430789Sahrens 
1431789Sahrens 	ASSERT(gsize == gbufsize);
1432789Sahrens 
1433789Sahrens 	for (loff = 0, i = 0; loff != zio->io_size; loff += lsize, i++) {
1434789Sahrens 		blkptr_t *gbp = &gbh->zg_blkptr[i];
1435789Sahrens 		lsize = BP_GET_PSIZE(gbp);
1436789Sahrens 
1437789Sahrens 		ASSERT(BP_GET_COMPRESS(gbp) == ZIO_COMPRESS_OFF);
1438789Sahrens 		ASSERT3U(lsize, ==, BP_GET_LSIZE(gbp));
1439789Sahrens 		ASSERT3U(loff + lsize, <=, zio->io_size);
1440789Sahrens 		ASSERT(i < SPA_GBH_NBLKPTRS);
1441789Sahrens 		ASSERT(!BP_IS_HOLE(gbp));
1442789Sahrens 
1443789Sahrens 		zio_nowait(zio_rewrite(zio, zio->io_spa, zio->io_checksum,
1444789Sahrens 		    zio->io_txg, gbp, (char *)zio->io_data + loff, lsize,
14451544Seschrock 		    NULL, NULL, zio->io_priority, zio->io_flags,
14461544Seschrock 		    &zio->io_bookmark));
1447789Sahrens 	}
1448789Sahrens 
1449789Sahrens 	zio_push_transform(zio, gbh, gsize, gbufsize);
1450789Sahrens 	zio_wait_children_ready(zio);
1451789Sahrens }
1452789Sahrens 
1453789Sahrens static void
1454789Sahrens zio_free_gang_members(zio_t *zio)
1455789Sahrens {
1456789Sahrens 	zio_gbh_phys_t *gbh;
1457789Sahrens 	uint64_t gsize, gbufsize;
1458789Sahrens 	int i;
1459789Sahrens 
14601775Sbillm 	ASSERT(BP_IS_GANG(zio->io_bp));
1461789Sahrens 
1462789Sahrens 	zio_gang_byteswap(zio);
1463789Sahrens 	zio_pop_transform(zio, (void **)&gbh, &gsize, &gbufsize);
1464789Sahrens 
1465789Sahrens 	for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
1466789Sahrens 		blkptr_t *gbp = &gbh->zg_blkptr[i];
1467789Sahrens 
1468789Sahrens 		if (BP_IS_HOLE(gbp))
1469789Sahrens 			continue;
1470789Sahrens 		zio_nowait(zio_free(zio, zio->io_spa, zio->io_txg,
1471789Sahrens 		    gbp, NULL, NULL));
1472789Sahrens 	}
1473789Sahrens 
1474789Sahrens 	zio_buf_free(gbh, gbufsize);
1475789Sahrens 	zio_next_stage(zio);
1476789Sahrens }
1477789Sahrens 
1478789Sahrens static void
1479789Sahrens zio_claim_gang_members(zio_t *zio)
1480789Sahrens {
1481789Sahrens 	zio_gbh_phys_t *gbh;
1482789Sahrens 	uint64_t gsize, gbufsize;
1483789Sahrens 	int i;
1484789Sahrens 
14851775Sbillm 	ASSERT(BP_IS_GANG(zio->io_bp));
1486789Sahrens 
1487789Sahrens 	zio_gang_byteswap(zio);
1488789Sahrens 	zio_pop_transform(zio, (void **)&gbh, &gsize, &gbufsize);
1489789Sahrens 
1490789Sahrens 	for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
1491789Sahrens 		blkptr_t *gbp = &gbh->zg_blkptr[i];
1492789Sahrens 		if (BP_IS_HOLE(gbp))
1493789Sahrens 			continue;
1494789Sahrens 		zio_nowait(zio_claim(zio, zio->io_spa, zio->io_txg,
1495789Sahrens 		    gbp, NULL, NULL));
1496789Sahrens 	}
1497789Sahrens 
1498789Sahrens 	zio_buf_free(gbh, gbufsize);
1499789Sahrens 	zio_next_stage(zio);
1500789Sahrens }
1501789Sahrens 
1502789Sahrens static void
1503789Sahrens zio_write_allocate_gang_member_done(zio_t *zio)
1504789Sahrens {
1505789Sahrens 	zio_t *pio = zio->io_parent;
15061775Sbillm 	dva_t *cdva = zio->io_bp->blk_dva;
15071775Sbillm 	dva_t *pdva = pio->io_bp->blk_dva;
1508789Sahrens 	uint64_t asize;
15091775Sbillm 	int d;
1510789Sahrens 
15111775Sbillm 	ASSERT3U(pio->io_ndvas, ==, zio->io_ndvas);
15121775Sbillm 	ASSERT3U(BP_GET_NDVAS(zio->io_bp), <=, BP_GET_NDVAS(pio->io_bp));
15131775Sbillm 	ASSERT3U(zio->io_ndvas, <=, BP_GET_NDVAS(zio->io_bp));
15141775Sbillm 	ASSERT3U(pio->io_ndvas, <=, BP_GET_NDVAS(pio->io_bp));
15151775Sbillm 
1516789Sahrens 	mutex_enter(&pio->io_lock);
15171775Sbillm 	for (d = 0; d < BP_GET_NDVAS(pio->io_bp); d++) {
15181775Sbillm 		ASSERT(DVA_GET_GANG(&pdva[d]));
15191775Sbillm 		asize = DVA_GET_ASIZE(&pdva[d]);
15201775Sbillm 		asize += DVA_GET_ASIZE(&cdva[d]);
15211775Sbillm 		DVA_SET_ASIZE(&pdva[d], asize);
15221775Sbillm 	}
1523789Sahrens 	mutex_exit(&pio->io_lock);
1524789Sahrens }
1525789Sahrens 
15265329Sgw25295 static int
15274527Sperrin zio_write_allocate_gang_members(zio_t *zio, metaslab_class_t *mc)
1528789Sahrens {
1529789Sahrens 	blkptr_t *bp = zio->io_bp;
15301775Sbillm 	dva_t *dva = bp->blk_dva;
15311775Sbillm 	spa_t *spa = zio->io_spa;
1532789Sahrens 	zio_gbh_phys_t *gbh;
15331775Sbillm 	uint64_t txg = zio->io_txg;
1534789Sahrens 	uint64_t resid = zio->io_size;
1535789Sahrens 	uint64_t maxalloc = P2ROUNDUP(zio->io_size >> 1, SPA_MINBLOCKSIZE);
1536789Sahrens 	uint64_t gsize, loff, lsize;
1537789Sahrens 	uint32_t gbps_left;
15381775Sbillm 	int ndvas = zio->io_ndvas;
15391775Sbillm 	int gbh_ndvas = MIN(ndvas + 1, spa_max_replication(spa));
1540789Sahrens 	int error;
15411775Sbillm 	int i, d;
1542789Sahrens 
1543789Sahrens 	gsize = SPA_GANGBLOCKSIZE;
1544789Sahrens 	gbps_left = SPA_GBH_NBLKPTRS;
1545789Sahrens 
15464527Sperrin 	error = metaslab_alloc(spa, mc, gsize, bp, gbh_ndvas, txg, NULL,
15474527Sperrin 	    B_FALSE);
15485329Sgw25295 	if (error)
15495329Sgw25295 		return (error);
1550789Sahrens 
15511775Sbillm 	for (d = 0; d < gbh_ndvas; d++)
15521775Sbillm 		DVA_SET_GANG(&dva[d], 1);
1553789Sahrens 
15541775Sbillm 	bp->blk_birth = txg;
1555789Sahrens 
1556789Sahrens 	gbh = zio_buf_alloc(gsize);
1557789Sahrens 	bzero(gbh, gsize);
1558789Sahrens 
15591775Sbillm 	/* We need to test multi-level gang blocks */
15601775Sbillm 	if (maxalloc >= zio_gang_bang && (lbolt & 0x1) == 0)
15611775Sbillm 		maxalloc = MAX(maxalloc >> 2, SPA_MINBLOCKSIZE);
15621775Sbillm 
1563789Sahrens 	for (loff = 0, i = 0; loff != zio->io_size;
1564789Sahrens 	    loff += lsize, resid -= lsize, gbps_left--, i++) {
1565789Sahrens 		blkptr_t *gbp = &gbh->zg_blkptr[i];
15661775Sbillm 		dva = gbp->blk_dva;
1567789Sahrens 
1568789Sahrens 		ASSERT(gbps_left != 0);
1569789Sahrens 		maxalloc = MIN(maxalloc, resid);
1570789Sahrens 
1571789Sahrens 		while (resid <= maxalloc * gbps_left) {
15724527Sperrin 			error = metaslab_alloc(spa, mc, maxalloc, gbp, ndvas,
15733063Sperrin 			    txg, bp, B_FALSE);
1574789Sahrens 			if (error == 0)
1575789Sahrens 				break;
1576789Sahrens 			ASSERT3U(error, ==, ENOSPC);
15775329Sgw25295 			/* XXX - free up previous allocations? */
1578789Sahrens 			if (maxalloc == SPA_MINBLOCKSIZE)
15795329Sgw25295 				return (error);
1580789Sahrens 			maxalloc = P2ROUNDUP(maxalloc >> 1, SPA_MINBLOCKSIZE);
1581789Sahrens 		}
1582789Sahrens 
1583789Sahrens 		if (resid <= maxalloc * gbps_left) {
1584789Sahrens 			lsize = maxalloc;
1585789Sahrens 			BP_SET_LSIZE(gbp, lsize);
1586789Sahrens 			BP_SET_PSIZE(gbp, lsize);
1587789Sahrens 			BP_SET_COMPRESS(gbp, ZIO_COMPRESS_OFF);
15881775Sbillm 			gbp->blk_birth = txg;
15891775Sbillm 			zio_nowait(zio_rewrite(zio, spa,
15901775Sbillm 			    zio->io_checksum, txg, gbp,
1591789Sahrens 			    (char *)zio->io_data + loff, lsize,
1592789Sahrens 			    zio_write_allocate_gang_member_done, NULL,
1593*5403Sgw25295 			    zio->io_priority,
1594*5403Sgw25295 			    zio->io_flags & ZIO_FLAG_GANG_INHERIT,
15951544Seschrock 			    &zio->io_bookmark));
1596789Sahrens 		} else {
1597789Sahrens 			lsize = P2ROUNDUP(resid / gbps_left, SPA_MINBLOCKSIZE);
1598789Sahrens 			ASSERT(lsize != SPA_MINBLOCKSIZE);
15991775Sbillm 			zio_nowait(zio_write_allocate(zio, spa,
16001775Sbillm 			    zio->io_checksum, txg, gbp,
1601789Sahrens 			    (char *)zio->io_data + loff, lsize,
1602789Sahrens 			    zio_write_allocate_gang_member_done, NULL,
1603*5403Sgw25295 			    zio->io_priority,
1604*5403Sgw25295 			    zio->io_flags & ZIO_FLAG_GANG_INHERIT));
1605789Sahrens 		}
1606789Sahrens 	}
1607789Sahrens 
1608789Sahrens 	ASSERT(resid == 0 && loff == zio->io_size);
1609789Sahrens 
1610789Sahrens 	zio->io_pipeline |= 1U << ZIO_STAGE_GANG_CHECKSUM_GENERATE;
1611789Sahrens 
1612789Sahrens 	zio_push_transform(zio, gbh, gsize, gsize);
16131775Sbillm 	/*
16141775Sbillm 	 * As much as we'd like this to be zio_wait_children_ready(),
16151775Sbillm 	 * updating our ASIZE doesn't happen until the io_done callback,
16161775Sbillm 	 * so we have to wait for that to finish in order for our BP
16171775Sbillm 	 * to be stable.
16181775Sbillm 	 */
1619789Sahrens 	zio_wait_children_done(zio);
16205329Sgw25295 	return (0);
1621789Sahrens }
1622789Sahrens 
1623789Sahrens /*
1624789Sahrens  * ==========================================================================
1625789Sahrens  * Allocate and free blocks
1626789Sahrens  * ==========================================================================
1627789Sahrens  */
1628789Sahrens static void
1629789Sahrens zio_dva_allocate(zio_t *zio)
1630789Sahrens {
16314527Sperrin 	spa_t *spa = zio->io_spa;
16324527Sperrin 	metaslab_class_t *mc = spa->spa_normal_class;
1633789Sahrens 	blkptr_t *bp = zio->io_bp;
1634789Sahrens 	int error;
1635789Sahrens 
1636789Sahrens 	ASSERT(BP_IS_HOLE(bp));
16371775Sbillm 	ASSERT3U(BP_GET_NDVAS(bp), ==, 0);
16381775Sbillm 	ASSERT3U(zio->io_ndvas, >, 0);
16394527Sperrin 	ASSERT3U(zio->io_ndvas, <=, spa_max_replication(spa));
1640789Sahrens 
1641789Sahrens 	/* For testing, make some blocks above a certain size be gang blocks */
1642789Sahrens 	if (zio->io_size >= zio_gang_bang && (lbolt & 0x3) == 0) {
16435329Sgw25295 		error = zio_write_allocate_gang_members(zio, mc);
16445329Sgw25295 		if (error)
16455329Sgw25295 			zio->io_error = error;
1646789Sahrens 		return;
1647789Sahrens 	}
1648789Sahrens 
16495329Sgw25295 	/*
16505329Sgw25295 	 * For testing purposes, we force I/Os to retry. We don't allow
16515329Sgw25295 	 * retries beyond the first pass since those I/Os are non-allocating
1652*5403Sgw25295 	 * writes.
16535329Sgw25295 	 */
16545329Sgw25295 	if (zio_io_fail_shift &&
16555329Sgw25295 	    spa_sync_pass(zio->io_spa) <= zio_sync_pass.zp_rewrite &&
16565329Sgw25295 	    zio_io_should_fail(zio_io_fail_shift))
16575329Sgw25295 		zio->io_flags |= ZIO_FLAG_WRITE_RETRY;
16585329Sgw25295 
1659789Sahrens 	ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp));
1660789Sahrens 
16614527Sperrin 	error = metaslab_alloc(spa, mc, zio->io_size, bp, zio->io_ndvas,
16623063Sperrin 	    zio->io_txg, NULL, B_FALSE);
1663789Sahrens 
1664789Sahrens 	if (error == 0) {
1665789Sahrens 		bp->blk_birth = zio->io_txg;
16665329Sgw25295 	} else if (error == ENOSPC && zio->io_size > SPA_MINBLOCKSIZE) {
16675329Sgw25295 		error = zio_write_allocate_gang_members(zio, mc);
16685329Sgw25295 		if (error == 0)
16695329Sgw25295 			return;
16705329Sgw25295 		zio->io_error = error;
1671789Sahrens 	} else {
1672789Sahrens 		zio->io_error = error;
1673789Sahrens 	}
1674789Sahrens 	zio_next_stage(zio);
1675789Sahrens }
1676789Sahrens 
1677789Sahrens static void
1678789Sahrens zio_dva_free(zio_t *zio)
1679789Sahrens {
1680789Sahrens 	blkptr_t *bp = zio->io_bp;
1681789Sahrens 
16821807Sbonwick 	metaslab_free(zio->io_spa, bp, zio->io_txg, B_FALSE);
1683789Sahrens 
1684789Sahrens 	BP_ZERO(bp);
1685789Sahrens 
1686789Sahrens 	zio_next_stage(zio);
1687789Sahrens }
1688789Sahrens 
1689789Sahrens static void
1690789Sahrens zio_dva_claim(zio_t *zio)
1691789Sahrens {
16921807Sbonwick 	zio->io_error = metaslab_claim(zio->io_spa, zio->io_bp, zio->io_txg);
1693789Sahrens 
1694789Sahrens 	zio_next_stage(zio);
1695789Sahrens }
1696789Sahrens 
1697789Sahrens /*
1698789Sahrens  * ==========================================================================
1699789Sahrens  * Read and write to physical devices
1700789Sahrens  * ==========================================================================
1701789Sahrens  */
1702789Sahrens 
1703789Sahrens static void
17041775Sbillm zio_vdev_io_start(zio_t *zio)
1705789Sahrens {
1706789Sahrens 	vdev_t *vd = zio->io_vd;
17071775Sbillm 	vdev_t *tvd = vd ? vd->vdev_top : NULL;
17081775Sbillm 	blkptr_t *bp = zio->io_bp;
17091775Sbillm 	uint64_t align;
17105329Sgw25295 	spa_t *spa = zio->io_spa;
17115329Sgw25295 
17125329Sgw25295 	/*
17135329Sgw25295 	 * If the pool is already in a failure state then just suspend
17145329Sgw25295 	 * this IO until the problem is resolved. We will reissue them
17155329Sgw25295 	 * at that time.
17165329Sgw25295 	 */
17175329Sgw25295 	if (spa_state(spa) == POOL_STATE_IO_FAILURE &&
17185329Sgw25295 	    zio->io_type == ZIO_TYPE_WRITE) {
17195329Sgw25295 		zio_vdev_suspend_io(zio);
17205329Sgw25295 		return;
17215329Sgw25295 	}
1722789Sahrens 
17231775Sbillm 	if (vd == NULL) {
17241775Sbillm 		/* The mirror_ops handle multiple DVAs in a single BP */
17251775Sbillm 		vdev_mirror_ops.vdev_op_io_start(zio);
17261775Sbillm 		return;
17271775Sbillm 	}
17281775Sbillm 
17291775Sbillm 	align = 1ULL << tvd->vdev_ashift;
17301775Sbillm 
17311732Sbonwick 	if (zio->io_retries == 0 && vd == tvd)
1732789Sahrens 		zio->io_flags |= ZIO_FLAG_FAILFAST;
1733789Sahrens 
17341775Sbillm 	if (!(zio->io_flags & ZIO_FLAG_PHYSICAL) &&
17351775Sbillm 	    vd->vdev_children == 0) {
1736789Sahrens 		zio->io_flags |= ZIO_FLAG_PHYSICAL;
1737789Sahrens 		zio->io_offset += VDEV_LABEL_START_SIZE;
1738789Sahrens 	}
1739789Sahrens 
17401732Sbonwick 	if (P2PHASE(zio->io_size, align) != 0) {
17411732Sbonwick 		uint64_t asize = P2ROUNDUP(zio->io_size, align);
17421732Sbonwick 		char *abuf = zio_buf_alloc(asize);
17431732Sbonwick 		ASSERT(vd == tvd);
17441732Sbonwick 		if (zio->io_type == ZIO_TYPE_WRITE) {
17451732Sbonwick 			bcopy(zio->io_data, abuf, zio->io_size);
17461732Sbonwick 			bzero(abuf + zio->io_size, asize - zio->io_size);
17471732Sbonwick 		}
17481732Sbonwick 		zio_push_transform(zio, abuf, asize, asize);
17491732Sbonwick 		ASSERT(!(zio->io_flags & ZIO_FLAG_SUBBLOCK));
17501732Sbonwick 		zio->io_flags |= ZIO_FLAG_SUBBLOCK;
17511732Sbonwick 	}
17521732Sbonwick 
17531732Sbonwick 	ASSERT(P2PHASE(zio->io_offset, align) == 0);
17541732Sbonwick 	ASSERT(P2PHASE(zio->io_size, align) == 0);
17551732Sbonwick 	ASSERT(bp == NULL ||
17561732Sbonwick 	    P2ROUNDUP(ZIO_GET_IOSIZE(zio), align) == zio->io_size);
1757789Sahrens 	ASSERT(zio->io_type != ZIO_TYPE_WRITE || (spa_mode & FWRITE));
1758789Sahrens 
1759789Sahrens 	vdev_io_start(zio);
1760789Sahrens 
1761789Sahrens 	/* zio_next_stage_async() gets called from io completion interrupt */
1762789Sahrens }
1763789Sahrens 
1764789Sahrens static void
1765789Sahrens zio_vdev_io_done(zio_t *zio)
1766789Sahrens {
17671775Sbillm 	if (zio->io_vd == NULL)
17681775Sbillm 		/* The mirror_ops handle multiple DVAs in a single BP */
17691775Sbillm 		vdev_mirror_ops.vdev_op_io_done(zio);
17701775Sbillm 	else
17711775Sbillm 		vdev_io_done(zio);
1772789Sahrens }
1773789Sahrens 
1774789Sahrens /* XXPOLICY */
17751544Seschrock boolean_t
1776789Sahrens zio_should_retry(zio_t *zio)
1777789Sahrens {
1778789Sahrens 	vdev_t *vd = zio->io_vd;
1779789Sahrens 
1780789Sahrens 	if (zio->io_error == 0)
1781789Sahrens 		return (B_FALSE);
1782789Sahrens 	if (zio->io_delegate_list != NULL)
1783789Sahrens 		return (B_FALSE);
17841775Sbillm 	if (vd && vd != vd->vdev_top)
1785789Sahrens 		return (B_FALSE);
1786789Sahrens 	if (zio->io_flags & ZIO_FLAG_DONT_RETRY)
1787789Sahrens 		return (B_FALSE);
17881544Seschrock 	if (zio->io_retries > 0)
1789789Sahrens 		return (B_FALSE);
1790789Sahrens 
1791789Sahrens 	return (B_TRUE);
1792789Sahrens }
1793789Sahrens 
1794789Sahrens static void
1795789Sahrens zio_vdev_io_assess(zio_t *zio)
1796789Sahrens {
1797789Sahrens 	vdev_t *vd = zio->io_vd;
17981775Sbillm 	vdev_t *tvd = vd ? vd->vdev_top : NULL;
1799789Sahrens 
18001544Seschrock 	ASSERT(zio->io_vsd == NULL);
1801789Sahrens 
18021732Sbonwick 	if (zio->io_flags & ZIO_FLAG_SUBBLOCK) {
18031732Sbonwick 		void *abuf;
18041732Sbonwick 		uint64_t asize;
18051732Sbonwick 		ASSERT(vd == tvd);
18061732Sbonwick 		zio_pop_transform(zio, &abuf, &asize, &asize);
18071732Sbonwick 		if (zio->io_type == ZIO_TYPE_READ)
18081732Sbonwick 			bcopy(abuf, zio->io_data, zio->io_size);
18091732Sbonwick 		zio_buf_free(abuf, asize);
18101732Sbonwick 		zio->io_flags &= ~ZIO_FLAG_SUBBLOCK;
18111732Sbonwick 	}
18121732Sbonwick 
18131544Seschrock 	if (zio_injection_enabled && !zio->io_error)
18141544Seschrock 		zio->io_error = zio_handle_fault_injection(zio, EIO);
1815789Sahrens 
1816789Sahrens 	/*
1817789Sahrens 	 * If the I/O failed, determine whether we should attempt to retry it.
1818789Sahrens 	 */
1819789Sahrens 	/* XXPOLICY */
1820789Sahrens 	if (zio_should_retry(zio)) {
1821789Sahrens 		ASSERT(tvd == vd);
1822789Sahrens 
1823789Sahrens 		zio->io_retries++;
1824789Sahrens 		zio->io_error = 0;
18253463Sahrens 		zio->io_flags &= ZIO_FLAG_VDEV_INHERIT |
18263463Sahrens 		    ZIO_FLAG_CONFIG_GRABBED;
1827789Sahrens 		/* XXPOLICY */
1828789Sahrens 		zio->io_flags &= ~ZIO_FLAG_FAILFAST;
1829789Sahrens 		zio->io_flags |= ZIO_FLAG_DONT_CACHE;
18301775Sbillm 		zio->io_stage = ZIO_STAGE_VDEV_IO_START - 1;
1831789Sahrens 
1832789Sahrens 		dprintf("retry #%d for %s to %s offset %llx\n",
1833789Sahrens 		    zio->io_retries, zio_type_name[zio->io_type],
1834789Sahrens 		    vdev_description(vd), zio->io_offset);
1835789Sahrens 
18361544Seschrock 		zio_next_stage_async(zio);
18371544Seschrock 		return;
18381544Seschrock 	}
1839789Sahrens 
1840789Sahrens 	zio_next_stage(zio);
1841789Sahrens }
1842789Sahrens 
1843789Sahrens void
1844789Sahrens zio_vdev_io_reissue(zio_t *zio)
1845789Sahrens {
1846789Sahrens 	ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
1847789Sahrens 	ASSERT(zio->io_error == 0);
1848789Sahrens 
1849789Sahrens 	zio->io_stage--;
1850789Sahrens }
1851789Sahrens 
1852789Sahrens void
1853789Sahrens zio_vdev_io_redone(zio_t *zio)
1854789Sahrens {
1855789Sahrens 	ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_DONE);
1856789Sahrens 
1857789Sahrens 	zio->io_stage--;
1858789Sahrens }
1859789Sahrens 
1860789Sahrens void
1861789Sahrens zio_vdev_io_bypass(zio_t *zio)
1862789Sahrens {
1863789Sahrens 	ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
1864789Sahrens 	ASSERT(zio->io_error == 0);
1865789Sahrens 
1866789Sahrens 	zio->io_flags |= ZIO_FLAG_IO_BYPASS;
1867789Sahrens 	zio->io_stage = ZIO_STAGE_VDEV_IO_ASSESS - 1;
1868789Sahrens }
1869789Sahrens 
1870789Sahrens /*
1871789Sahrens  * ==========================================================================
1872789Sahrens  * Generate and verify checksums
1873789Sahrens  * ==========================================================================
1874789Sahrens  */
1875789Sahrens static void
1876789Sahrens zio_checksum_generate(zio_t *zio)
1877789Sahrens {
1878789Sahrens 	int checksum = zio->io_checksum;
1879789Sahrens 	blkptr_t *bp = zio->io_bp;
1880789Sahrens 
1881789Sahrens 	ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp));
1882789Sahrens 
1883789Sahrens 	BP_SET_CHECKSUM(bp, checksum);
1884789Sahrens 	BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
1885789Sahrens 
1886789Sahrens 	zio_checksum(checksum, &bp->blk_cksum, zio->io_data, zio->io_size);
1887789Sahrens 
1888789Sahrens 	zio_next_stage(zio);
1889789Sahrens }
1890789Sahrens 
1891789Sahrens static void
1892789Sahrens zio_gang_checksum_generate(zio_t *zio)
1893789Sahrens {
1894789Sahrens 	zio_cksum_t zc;
1895789Sahrens 	zio_gbh_phys_t *gbh = zio->io_data;
1896789Sahrens 
18971775Sbillm 	ASSERT(BP_IS_GANG(zio->io_bp));
1898789Sahrens 	ASSERT3U(zio->io_size, ==, SPA_GANGBLOCKSIZE);
1899789Sahrens 
1900789Sahrens 	zio_set_gang_verifier(zio, &gbh->zg_tail.zbt_cksum);
1901789Sahrens 
1902789Sahrens 	zio_checksum(ZIO_CHECKSUM_GANG_HEADER, &zc, zio->io_data, zio->io_size);
1903789Sahrens 
1904789Sahrens 	zio_next_stage(zio);
1905789Sahrens }
1906789Sahrens 
1907789Sahrens static void
1908789Sahrens zio_checksum_verify(zio_t *zio)
1909789Sahrens {
1910789Sahrens 	if (zio->io_bp != NULL) {
1911789Sahrens 		zio->io_error = zio_checksum_error(zio);
19121544Seschrock 		if (zio->io_error && !(zio->io_flags & ZIO_FLAG_SPECULATIVE))
19131544Seschrock 			zfs_ereport_post(FM_EREPORT_ZFS_CHECKSUM,
19141544Seschrock 			    zio->io_spa, zio->io_vd, zio, 0, 0);
1915789Sahrens 	}
1916789Sahrens 
1917789Sahrens 	zio_next_stage(zio);
1918789Sahrens }
1919789Sahrens 
1920789Sahrens /*
1921789Sahrens  * Called by RAID-Z to ensure we don't compute the checksum twice.
1922789Sahrens  */
1923789Sahrens void
1924789Sahrens zio_checksum_verified(zio_t *zio)
1925789Sahrens {
1926789Sahrens 	zio->io_pipeline &= ~(1U << ZIO_STAGE_CHECKSUM_VERIFY);
1927789Sahrens }
1928789Sahrens 
1929789Sahrens /*
1930789Sahrens  * Set the external verifier for a gang block based on stuff in the bp
1931789Sahrens  */
1932789Sahrens void
1933789Sahrens zio_set_gang_verifier(zio_t *zio, zio_cksum_t *zcp)
1934789Sahrens {
19351775Sbillm 	blkptr_t *bp = zio->io_bp;
19361775Sbillm 
19371775Sbillm 	zcp->zc_word[0] = DVA_GET_VDEV(BP_IDENTITY(bp));
19381775Sbillm 	zcp->zc_word[1] = DVA_GET_OFFSET(BP_IDENTITY(bp));
19391775Sbillm 	zcp->zc_word[2] = bp->blk_birth;
1940789Sahrens 	zcp->zc_word[3] = 0;
1941789Sahrens }
1942789Sahrens 
1943789Sahrens /*
1944789Sahrens  * ==========================================================================
1945789Sahrens  * Define the pipeline
1946789Sahrens  * ==========================================================================
1947789Sahrens  */
1948789Sahrens typedef void zio_pipe_stage_t(zio_t *zio);
1949789Sahrens 
1950789Sahrens static void
1951789Sahrens zio_badop(zio_t *zio)
1952789Sahrens {
1953789Sahrens 	panic("Invalid I/O pipeline stage %u for zio %p", zio->io_stage, zio);
1954789Sahrens }
1955789Sahrens 
1956789Sahrens zio_pipe_stage_t *zio_pipeline[ZIO_STAGE_DONE + 2] = {
1957789Sahrens 	zio_badop,
1958789Sahrens 	zio_wait_children_ready,
1959789Sahrens 	zio_write_compress,
1960789Sahrens 	zio_checksum_generate,
1961789Sahrens 	zio_gang_pipeline,
1962789Sahrens 	zio_get_gang_header,
1963789Sahrens 	zio_rewrite_gang_members,
1964789Sahrens 	zio_free_gang_members,
1965789Sahrens 	zio_claim_gang_members,
1966789Sahrens 	zio_dva_allocate,
1967789Sahrens 	zio_dva_free,
1968789Sahrens 	zio_dva_claim,
1969789Sahrens 	zio_gang_checksum_generate,
1970789Sahrens 	zio_ready,
19715329Sgw25295 	zio_read_init,
1972789Sahrens 	zio_vdev_io_start,
1973789Sahrens 	zio_vdev_io_done,
1974789Sahrens 	zio_vdev_io_assess,
1975789Sahrens 	zio_wait_children_done,
1976789Sahrens 	zio_checksum_verify,
1977789Sahrens 	zio_read_gang_members,
1978789Sahrens 	zio_read_decompress,
19795329Sgw25295 	zio_assess,
1980789Sahrens 	zio_done,
1981789Sahrens 	zio_badop
1982789Sahrens };
1983789Sahrens 
1984789Sahrens /*
1985789Sahrens  * Move an I/O to the next stage of the pipeline and execute that stage.
1986789Sahrens  * There's no locking on io_stage because there's no legitimate way for
1987789Sahrens  * multiple threads to be attempting to process the same I/O.
1988789Sahrens  */
1989789Sahrens void
1990789Sahrens zio_next_stage(zio_t *zio)
1991789Sahrens {
1992789Sahrens 	uint32_t pipeline = zio->io_pipeline;
1993789Sahrens 
1994789Sahrens 	ASSERT(!MUTEX_HELD(&zio->io_lock));
1995789Sahrens 
1996789Sahrens 	if (zio->io_error) {
1997789Sahrens 		dprintf("zio %p vdev %s offset %llx stage %d error %d\n",
1998789Sahrens 		    zio, vdev_description(zio->io_vd),
1999789Sahrens 		    zio->io_offset, zio->io_stage, zio->io_error);
2000789Sahrens 		if (((1U << zio->io_stage) & ZIO_VDEV_IO_PIPELINE) == 0)
2001789Sahrens 			pipeline &= ZIO_ERROR_PIPELINE_MASK;
2002789Sahrens 	}
2003789Sahrens 
2004789Sahrens 	while (((1U << ++zio->io_stage) & pipeline) == 0)
2005789Sahrens 		continue;
2006789Sahrens 
2007789Sahrens 	ASSERT(zio->io_stage <= ZIO_STAGE_DONE);
2008789Sahrens 	ASSERT(zio->io_stalled == 0);
2009789Sahrens 
20103689Sek110237 	/*
20113689Sek110237 	 * See the comment in zio_next_stage_async() about per-CPU taskqs.
20123689Sek110237 	 */
20133689Sek110237 	if (((1U << zio->io_stage) & zio->io_async_stages) &&
20143689Sek110237 	    (zio->io_stage == ZIO_STAGE_WRITE_COMPRESS) &&
20153689Sek110237 	    !(zio->io_flags & ZIO_FLAG_METADATA)) {
20163689Sek110237 		taskq_t *tq = zio->io_spa->spa_zio_issue_taskq[zio->io_type];
20173689Sek110237 		(void) taskq_dispatch(tq,
20183689Sek110237 		    (task_func_t *)zio_pipeline[zio->io_stage], zio, TQ_SLEEP);
20193689Sek110237 	} else {
20203689Sek110237 		zio_pipeline[zio->io_stage](zio);
20213689Sek110237 	}
2022789Sahrens }
2023789Sahrens 
2024789Sahrens void
2025789Sahrens zio_next_stage_async(zio_t *zio)
2026789Sahrens {
2027789Sahrens 	taskq_t *tq;
2028789Sahrens 	uint32_t pipeline = zio->io_pipeline;
2029789Sahrens 
2030789Sahrens 	ASSERT(!MUTEX_HELD(&zio->io_lock));
2031789Sahrens 
2032789Sahrens 	if (zio->io_error) {
2033789Sahrens 		dprintf("zio %p vdev %s offset %llx stage %d error %d\n",
2034789Sahrens 		    zio, vdev_description(zio->io_vd),
2035789Sahrens 		    zio->io_offset, zio->io_stage, zio->io_error);
2036789Sahrens 		if (((1U << zio->io_stage) & ZIO_VDEV_IO_PIPELINE) == 0)
2037789Sahrens 			pipeline &= ZIO_ERROR_PIPELINE_MASK;
2038789Sahrens 	}
2039789Sahrens 
2040789Sahrens 	while (((1U << ++zio->io_stage) & pipeline) == 0)
2041789Sahrens 		continue;
2042789Sahrens 
2043789Sahrens 	ASSERT(zio->io_stage <= ZIO_STAGE_DONE);
2044789Sahrens 	ASSERT(zio->io_stalled == 0);
2045789Sahrens 
2046789Sahrens 	/*
2047789Sahrens 	 * For performance, we'll probably want two sets of task queues:
2048789Sahrens 	 * per-CPU issue taskqs and per-CPU completion taskqs.  The per-CPU
2049789Sahrens 	 * part is for read performance: since we have to make a pass over
2050789Sahrens 	 * the data to checksum it anyway, we want to do this on the same CPU
2051789Sahrens 	 * that issued the read, because (assuming CPU scheduling affinity)
2052789Sahrens 	 * that thread is probably still there.  Getting this optimization
2053789Sahrens 	 * right avoids performance-hostile cache-to-cache transfers.
2054789Sahrens 	 *
2055789Sahrens 	 * Note that having two sets of task queues is also necessary for
2056789Sahrens 	 * correctness: if all of the issue threads get bogged down waiting
2057789Sahrens 	 * for dependent reads (e.g. metaslab freelist) to complete, then
2058789Sahrens 	 * there won't be any threads available to service I/O completion
2059789Sahrens 	 * interrupts.
2060789Sahrens 	 */
2061789Sahrens 	if ((1U << zio->io_stage) & zio->io_async_stages) {
2062789Sahrens 		if (zio->io_stage < ZIO_STAGE_VDEV_IO_DONE)
2063789Sahrens 			tq = zio->io_spa->spa_zio_issue_taskq[zio->io_type];
2064789Sahrens 		else
2065789Sahrens 			tq = zio->io_spa->spa_zio_intr_taskq[zio->io_type];
2066789Sahrens 		(void) taskq_dispatch(tq,
2067789Sahrens 		    (task_func_t *)zio_pipeline[zio->io_stage], zio, TQ_SLEEP);
2068789Sahrens 	} else {
2069789Sahrens 		zio_pipeline[zio->io_stage](zio);
2070789Sahrens 	}
2071789Sahrens }
2072789Sahrens 
20735329Sgw25295 void
20745329Sgw25295 zio_resubmit_stage_async(void *arg)
20755329Sgw25295 {
20765329Sgw25295 	zio_t *zio = (zio_t *)(uintptr_t)arg;
20775329Sgw25295 
20785329Sgw25295 	zio_next_stage_async(zio);
20795329Sgw25295 }
20805329Sgw25295 
20813668Sgw25295 static boolean_t
20825329Sgw25295 zio_io_should_fail(uint16_t range)
20833668Sgw25295 {
20843668Sgw25295 	static uint16_t	allocs = 0;
20853668Sgw25295 
20865329Sgw25295 	return (P2PHASE(allocs++, 1U<<range) == 0);
20873668Sgw25295 }
20883668Sgw25295 
2089789Sahrens /*
2090789Sahrens  * Try to allocate an intent log block.  Return 0 on success, errno on failure.
2091789Sahrens  */
2092789Sahrens int
20933063Sperrin zio_alloc_blk(spa_t *spa, uint64_t size, blkptr_t *new_bp, blkptr_t *old_bp,
20943063Sperrin     uint64_t txg)
2095789Sahrens {
2096789Sahrens 	int error;
2097789Sahrens 
20981544Seschrock 	spa_config_enter(spa, RW_READER, FTAG);
2099789Sahrens 
21005329Sgw25295 	if (zio_zil_fail_shift && zio_io_should_fail(zio_zil_fail_shift)) {
21013668Sgw25295 		spa_config_exit(spa, FTAG);
21023668Sgw25295 		return (ENOSPC);
21033668Sgw25295 	}
21043668Sgw25295 
21053063Sperrin 	/*
21064527Sperrin 	 * We were passed the previous log block's DVA in bp->blk_dva[0].
21074527Sperrin 	 * We use that as a hint for which vdev to allocate from next.
21083063Sperrin 	 */
21094527Sperrin 	error = metaslab_alloc(spa, spa->spa_log_class, size,
21104527Sperrin 	    new_bp, 1, txg, old_bp, B_TRUE);
21114527Sperrin 
21124527Sperrin 	if (error)
21134527Sperrin 		error = metaslab_alloc(spa, spa->spa_normal_class, size,
21144527Sperrin 		    new_bp, 1, txg, old_bp, B_TRUE);
2115789Sahrens 
2116789Sahrens 	if (error == 0) {
21173063Sperrin 		BP_SET_LSIZE(new_bp, size);
21183063Sperrin 		BP_SET_PSIZE(new_bp, size);
21193063Sperrin 		BP_SET_COMPRESS(new_bp, ZIO_COMPRESS_OFF);
21203063Sperrin 		BP_SET_CHECKSUM(new_bp, ZIO_CHECKSUM_ZILOG);
21213063Sperrin 		BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG);
21223063Sperrin 		BP_SET_LEVEL(new_bp, 0);
21233063Sperrin 		BP_SET_BYTEORDER(new_bp, ZFS_HOST_BYTEORDER);
21243063Sperrin 		new_bp->blk_birth = txg;
2125789Sahrens 	}
2126789Sahrens 
21271544Seschrock 	spa_config_exit(spa, FTAG);
2128789Sahrens 
2129789Sahrens 	return (error);
2130789Sahrens }
2131789Sahrens 
2132789Sahrens /*
2133789Sahrens  * Free an intent log block.  We know it can't be a gang block, so there's
2134789Sahrens  * nothing to do except metaslab_free() it.
2135789Sahrens  */
2136789Sahrens void
2137789Sahrens zio_free_blk(spa_t *spa, blkptr_t *bp, uint64_t txg)
2138789Sahrens {
21391775Sbillm 	ASSERT(!BP_IS_GANG(bp));
2140789Sahrens 
21411544Seschrock 	spa_config_enter(spa, RW_READER, FTAG);
2142789Sahrens 
21431807Sbonwick 	metaslab_free(spa, bp, txg, B_FALSE);
2144789Sahrens 
21451544Seschrock 	spa_config_exit(spa, FTAG);
2146789Sahrens }
21474469Sperrin 
21484469Sperrin /*
21494469Sperrin  * start an async flush of the write cache for this vdev
21504469Sperrin  */
21514469Sperrin void
21524469Sperrin zio_flush_vdev(spa_t *spa, uint64_t vdev, zio_t **zio)
21534469Sperrin {
21544469Sperrin 	vdev_t *vd;
21554469Sperrin 
21564469Sperrin 	/*
21574469Sperrin 	 * Lock out configuration changes.
21584469Sperrin 	 */
21594469Sperrin 	spa_config_enter(spa, RW_READER, FTAG);
21604469Sperrin 
21614469Sperrin 	if (*zio == NULL)
21624469Sperrin 		*zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
21634469Sperrin 
21644469Sperrin 	vd = vdev_lookup_top(spa, vdev);
21654469Sperrin 	ASSERT(vd);
21664469Sperrin 
21674469Sperrin 	(void) zio_nowait(zio_ioctl(*zio, spa, vd, DKIOCFLUSHWRITECACHE,
21684469Sperrin 	    NULL, NULL, ZIO_PRIORITY_NOW,
21694469Sperrin 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
21704469Sperrin 
21714469Sperrin 	spa_config_exit(spa, FTAG);
21724469Sperrin }
2173