xref: /onnv-gate/usr/src/uts/common/fs/zfs/zio.c (revision 2856:6f4d5ee1906a)
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 /*
221544Seschrock  * Copyright 2006 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 
67789Sahrens typedef struct zio_sync_pass {
68789Sahrens 	int	zp_defer_free;		/* defer frees after this pass */
69789Sahrens 	int	zp_dontcompress;	/* don't compress after this pass */
70789Sahrens 	int	zp_rewrite;		/* rewrite new bps after this pass */
71789Sahrens } zio_sync_pass_t;
72789Sahrens 
73789Sahrens zio_sync_pass_t zio_sync_pass = {
74789Sahrens 	1,	/* zp_defer_free */
75789Sahrens 	4,	/* zp_dontcompress */
76789Sahrens 	1,	/* zp_rewrite */
77789Sahrens };
78789Sahrens 
79789Sahrens /*
80789Sahrens  * ==========================================================================
81789Sahrens  * I/O kmem caches
82789Sahrens  * ==========================================================================
83789Sahrens  */
84789Sahrens kmem_cache_t *zio_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
85789Sahrens 
86789Sahrens void
87789Sahrens zio_init(void)
88789Sahrens {
89789Sahrens 	size_t c;
90789Sahrens 
91789Sahrens 	/*
92789Sahrens 	 * For small buffers, we want a cache for each multiple of
93789Sahrens 	 * SPA_MINBLOCKSIZE.  For medium-size buffers, we want a cache
94789Sahrens 	 * for each quarter-power of 2.  For large buffers, we want
95789Sahrens 	 * a cache for each multiple of PAGESIZE.
96789Sahrens 	 */
97789Sahrens 	for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) {
98789Sahrens 		size_t size = (c + 1) << SPA_MINBLOCKSHIFT;
99789Sahrens 		size_t p2 = size;
100789Sahrens 		size_t align = 0;
101789Sahrens 
102789Sahrens 		while (p2 & (p2 - 1))
103789Sahrens 			p2 &= p2 - 1;
104789Sahrens 
105789Sahrens 		if (size <= 4 * SPA_MINBLOCKSIZE) {
106789Sahrens 			align = SPA_MINBLOCKSIZE;
107789Sahrens 		} else if (P2PHASE(size, PAGESIZE) == 0) {
108789Sahrens 			align = PAGESIZE;
109789Sahrens 		} else if (P2PHASE(size, p2 >> 2) == 0) {
110789Sahrens 			align = p2 >> 2;
111789Sahrens 		}
112789Sahrens 
113789Sahrens 		if (align != 0) {
114789Sahrens 			char name[30];
115*2856Snd150628 			(void) sprintf(name, "zio_buf_%lu", (ulong_t)size);
116789Sahrens 			zio_buf_cache[c] = kmem_cache_create(name, size,
117849Sbonwick 			    align, NULL, NULL, NULL, NULL, NULL, KMC_NODEBUG);
118789Sahrens 			dprintf("creating cache for size %5lx align %5lx\n",
119789Sahrens 			    size, align);
120789Sahrens 		}
121789Sahrens 	}
122789Sahrens 
123789Sahrens 	while (--c != 0) {
124789Sahrens 		ASSERT(zio_buf_cache[c] != NULL);
125789Sahrens 		if (zio_buf_cache[c - 1] == NULL)
126789Sahrens 			zio_buf_cache[c - 1] = zio_buf_cache[c];
127789Sahrens 	}
1281544Seschrock 
1291544Seschrock 	zio_inject_init();
130789Sahrens }
131789Sahrens 
132789Sahrens void
133789Sahrens zio_fini(void)
134789Sahrens {
135789Sahrens 	size_t c;
136789Sahrens 	kmem_cache_t *last_cache = NULL;
137789Sahrens 
138789Sahrens 	for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) {
139789Sahrens 		if (zio_buf_cache[c] != last_cache) {
140789Sahrens 			last_cache = zio_buf_cache[c];
141789Sahrens 			kmem_cache_destroy(zio_buf_cache[c]);
142789Sahrens 		}
143789Sahrens 		zio_buf_cache[c] = NULL;
144789Sahrens 	}
1451544Seschrock 
1461544Seschrock 	zio_inject_fini();
147789Sahrens }
148789Sahrens 
149789Sahrens /*
150789Sahrens  * ==========================================================================
151789Sahrens  * Allocate and free I/O buffers
152789Sahrens  * ==========================================================================
153789Sahrens  */
154789Sahrens void *
155789Sahrens zio_buf_alloc(size_t size)
156789Sahrens {
157789Sahrens 	size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
158789Sahrens 
159789Sahrens 	ASSERT(c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
160789Sahrens 
161789Sahrens 	return (kmem_cache_alloc(zio_buf_cache[c], KM_SLEEP));
162789Sahrens }
163789Sahrens 
164789Sahrens void
165789Sahrens zio_buf_free(void *buf, size_t size)
166789Sahrens {
167789Sahrens 	size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
168789Sahrens 
169789Sahrens 	ASSERT(c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
170789Sahrens 
171789Sahrens 	kmem_cache_free(zio_buf_cache[c], buf);
172789Sahrens }
173789Sahrens 
174789Sahrens /*
175789Sahrens  * ==========================================================================
176789Sahrens  * Push and pop I/O transform buffers
177789Sahrens  * ==========================================================================
178789Sahrens  */
179789Sahrens static void
180789Sahrens zio_push_transform(zio_t *zio, void *data, uint64_t size, uint64_t bufsize)
181789Sahrens {
182789Sahrens 	zio_transform_t *zt = kmem_alloc(sizeof (zio_transform_t), KM_SLEEP);
183789Sahrens 
184789Sahrens 	zt->zt_data = data;
185789Sahrens 	zt->zt_size = size;
186789Sahrens 	zt->zt_bufsize = bufsize;
187789Sahrens 
188789Sahrens 	zt->zt_next = zio->io_transform_stack;
189789Sahrens 	zio->io_transform_stack = zt;
190789Sahrens 
191789Sahrens 	zio->io_data = data;
192789Sahrens 	zio->io_size = size;
193789Sahrens }
194789Sahrens 
195789Sahrens static void
196789Sahrens zio_pop_transform(zio_t *zio, void **data, uint64_t *size, uint64_t *bufsize)
197789Sahrens {
198789Sahrens 	zio_transform_t *zt = zio->io_transform_stack;
199789Sahrens 
200789Sahrens 	*data = zt->zt_data;
201789Sahrens 	*size = zt->zt_size;
202789Sahrens 	*bufsize = zt->zt_bufsize;
203789Sahrens 
204789Sahrens 	zio->io_transform_stack = zt->zt_next;
205789Sahrens 	kmem_free(zt, sizeof (zio_transform_t));
206789Sahrens 
207789Sahrens 	if ((zt = zio->io_transform_stack) != NULL) {
208789Sahrens 		zio->io_data = zt->zt_data;
209789Sahrens 		zio->io_size = zt->zt_size;
210789Sahrens 	}
211789Sahrens }
212789Sahrens 
213789Sahrens static void
214789Sahrens zio_clear_transform_stack(zio_t *zio)
215789Sahrens {
216789Sahrens 	void *data;
217789Sahrens 	uint64_t size, bufsize;
218789Sahrens 
219789Sahrens 	ASSERT(zio->io_transform_stack != NULL);
220789Sahrens 
221789Sahrens 	zio_pop_transform(zio, &data, &size, &bufsize);
222789Sahrens 	while (zio->io_transform_stack != NULL) {
223789Sahrens 		zio_buf_free(data, bufsize);
224789Sahrens 		zio_pop_transform(zio, &data, &size, &bufsize);
225789Sahrens 	}
226789Sahrens }
227789Sahrens 
228789Sahrens /*
229789Sahrens  * ==========================================================================
230789Sahrens  * Create the various types of I/O (read, write, free)
231789Sahrens  * ==========================================================================
232789Sahrens  */
233789Sahrens static zio_t *
234789Sahrens zio_create(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
235789Sahrens     void *data, uint64_t size, zio_done_func_t *done, void *private,
236789Sahrens     zio_type_t type, int priority, int flags, uint8_t stage, uint32_t pipeline)
237789Sahrens {
238789Sahrens 	zio_t *zio;
239789Sahrens 
240789Sahrens 	ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
241789Sahrens 	ASSERT(P2PHASE(size, SPA_MINBLOCKSIZE) == 0);
242789Sahrens 
243789Sahrens 	zio = kmem_zalloc(sizeof (zio_t), KM_SLEEP);
244789Sahrens 	zio->io_parent = pio;
245789Sahrens 	zio->io_spa = spa;
246789Sahrens 	zio->io_txg = txg;
247789Sahrens 	if (bp != NULL) {
248789Sahrens 		zio->io_bp = bp;
249789Sahrens 		zio->io_bp_copy = *bp;
250789Sahrens 		zio->io_bp_orig = *bp;
251789Sahrens 	}
252789Sahrens 	zio->io_done = done;
253789Sahrens 	zio->io_private = private;
254789Sahrens 	zio->io_type = type;
255789Sahrens 	zio->io_priority = priority;
256789Sahrens 	zio->io_stage = stage;
257789Sahrens 	zio->io_pipeline = pipeline;
258789Sahrens 	zio->io_async_stages = ZIO_ASYNC_PIPELINE_STAGES;
259789Sahrens 	zio->io_timestamp = lbolt64;
260789Sahrens 	zio->io_flags = flags;
261*2856Snd150628 	mutex_init(&zio->io_lock, NULL, MUTEX_DEFAULT, NULL);
262789Sahrens 	zio_push_transform(zio, data, size, size);
263789Sahrens 
264789Sahrens 	if (pio == NULL) {
265789Sahrens 		if (!(flags & ZIO_FLAG_CONFIG_HELD))
2661544Seschrock 			spa_config_enter(zio->io_spa, RW_READER, zio);
267789Sahrens 		zio->io_root = zio;
268789Sahrens 	} else {
269789Sahrens 		zio->io_root = pio->io_root;
2701544Seschrock 		if (!(flags & ZIO_FLAG_NOBOOKMARK))
2711544Seschrock 			zio->io_logical = pio->io_logical;
272789Sahrens 		mutex_enter(&pio->io_lock);
273789Sahrens 		if (stage < ZIO_STAGE_READY)
274789Sahrens 			pio->io_children_notready++;
275789Sahrens 		pio->io_children_notdone++;
276789Sahrens 		zio->io_sibling_next = pio->io_child;
277789Sahrens 		zio->io_sibling_prev = NULL;
278789Sahrens 		if (pio->io_child != NULL)
279789Sahrens 			pio->io_child->io_sibling_prev = zio;
280789Sahrens 		pio->io_child = zio;
2811775Sbillm 		zio->io_ndvas = pio->io_ndvas;
282789Sahrens 		mutex_exit(&pio->io_lock);
283789Sahrens 	}
284789Sahrens 
285789Sahrens 	return (zio);
286789Sahrens }
287789Sahrens 
288789Sahrens zio_t *
289789Sahrens zio_null(zio_t *pio, spa_t *spa, zio_done_func_t *done, void *private,
290789Sahrens 	int flags)
291789Sahrens {
292789Sahrens 	zio_t *zio;
293789Sahrens 
294789Sahrens 	zio = zio_create(pio, spa, 0, NULL, NULL, 0, done, private,
295789Sahrens 	    ZIO_TYPE_NULL, ZIO_PRIORITY_NOW, flags, ZIO_STAGE_OPEN,
296789Sahrens 	    ZIO_WAIT_FOR_CHILDREN_PIPELINE);
297789Sahrens 
298789Sahrens 	return (zio);
299789Sahrens }
300789Sahrens 
301789Sahrens zio_t *
302789Sahrens zio_root(spa_t *spa, zio_done_func_t *done, void *private, int flags)
303789Sahrens {
304789Sahrens 	return (zio_null(NULL, spa, done, private, flags));
305789Sahrens }
306789Sahrens 
307789Sahrens zio_t *
308789Sahrens zio_read(zio_t *pio, spa_t *spa, blkptr_t *bp, void *data,
309789Sahrens     uint64_t size, zio_done_func_t *done, void *private,
3101544Seschrock     int priority, int flags, zbookmark_t *zb)
311789Sahrens {
312789Sahrens 	zio_t *zio;
313789Sahrens 
314789Sahrens 	ASSERT3U(size, ==, BP_GET_LSIZE(bp));
315789Sahrens 
316789Sahrens 	zio = zio_create(pio, spa, bp->blk_birth, bp, data, size, done, private,
317789Sahrens 	    ZIO_TYPE_READ, priority, flags, ZIO_STAGE_OPEN, ZIO_READ_PIPELINE);
3181544Seschrock 	zio->io_bookmark = *zb;
3191544Seschrock 
3201544Seschrock 	zio->io_logical = zio;
321789Sahrens 
322789Sahrens 	/*
323789Sahrens 	 * Work off our copy of the bp so the caller can free it.
324789Sahrens 	 */
325789Sahrens 	zio->io_bp = &zio->io_bp_copy;
326789Sahrens 
327789Sahrens 	if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF) {
328789Sahrens 		uint64_t csize = BP_GET_PSIZE(bp);
329789Sahrens 		void *cbuf = zio_buf_alloc(csize);
330789Sahrens 
331789Sahrens 		zio_push_transform(zio, cbuf, csize, csize);
332789Sahrens 		zio->io_pipeline |= 1U << ZIO_STAGE_READ_DECOMPRESS;
333789Sahrens 	}
334789Sahrens 
3351775Sbillm 	if (BP_IS_GANG(bp)) {
336789Sahrens 		uint64_t gsize = SPA_GANGBLOCKSIZE;
337789Sahrens 		void *gbuf = zio_buf_alloc(gsize);
338789Sahrens 
339789Sahrens 		zio_push_transform(zio, gbuf, gsize, gsize);
340789Sahrens 		zio->io_pipeline |= 1U << ZIO_STAGE_READ_GANG_MEMBERS;
341789Sahrens 	}
342789Sahrens 
343789Sahrens 	return (zio);
344789Sahrens }
345789Sahrens 
346789Sahrens zio_t *
3471775Sbillm zio_write(zio_t *pio, spa_t *spa, int checksum, int compress, int ncopies,
348789Sahrens     uint64_t txg, blkptr_t *bp, void *data, uint64_t size,
3491544Seschrock     zio_done_func_t *done, void *private, int priority, int flags,
3501544Seschrock     zbookmark_t *zb)
351789Sahrens {
352789Sahrens 	zio_t *zio;
353789Sahrens 
354789Sahrens 	ASSERT(checksum >= ZIO_CHECKSUM_OFF &&
355789Sahrens 	    checksum < ZIO_CHECKSUM_FUNCTIONS);
356789Sahrens 
357789Sahrens 	ASSERT(compress >= ZIO_COMPRESS_OFF &&
358789Sahrens 	    compress < ZIO_COMPRESS_FUNCTIONS);
359789Sahrens 
360789Sahrens 	zio = zio_create(pio, spa, txg, bp, data, size, done, private,
361789Sahrens 	    ZIO_TYPE_WRITE, priority, flags,
362789Sahrens 	    ZIO_STAGE_OPEN, ZIO_WRITE_PIPELINE);
363789Sahrens 
3641544Seschrock 	zio->io_bookmark = *zb;
3651544Seschrock 
3661544Seschrock 	zio->io_logical = zio;
3671544Seschrock 
368789Sahrens 	zio->io_checksum = checksum;
369789Sahrens 	zio->io_compress = compress;
3701775Sbillm 	zio->io_ndvas = ncopies;
371789Sahrens 
372789Sahrens 	if (compress != ZIO_COMPRESS_OFF)
373789Sahrens 		zio->io_async_stages |= 1U << ZIO_STAGE_WRITE_COMPRESS;
374789Sahrens 
375789Sahrens 	if (bp->blk_birth != txg) {
376789Sahrens 		/* XXX the bp usually (always?) gets re-zeroed later */
377789Sahrens 		BP_ZERO(bp);
378789Sahrens 		BP_SET_LSIZE(bp, size);
379789Sahrens 		BP_SET_PSIZE(bp, size);
3801775Sbillm 	} else {
3811775Sbillm 		/* Make sure someone doesn't change their mind on overwrites */
3821775Sbillm 		ASSERT(MIN(zio->io_ndvas + BP_IS_GANG(bp),
3831775Sbillm 		    spa_max_replication(spa)) == BP_GET_NDVAS(bp));
384789Sahrens 	}
385789Sahrens 
386789Sahrens 	return (zio);
387789Sahrens }
388789Sahrens 
389789Sahrens zio_t *
390789Sahrens zio_rewrite(zio_t *pio, spa_t *spa, int checksum,
391789Sahrens     uint64_t txg, blkptr_t *bp, void *data, uint64_t size,
3921544Seschrock     zio_done_func_t *done, void *private, int priority, int flags,
3931544Seschrock     zbookmark_t *zb)
394789Sahrens {
395789Sahrens 	zio_t *zio;
396789Sahrens 
397789Sahrens 	zio = zio_create(pio, spa, txg, bp, data, size, done, private,
398789Sahrens 	    ZIO_TYPE_WRITE, priority, flags,
399789Sahrens 	    ZIO_STAGE_OPEN, ZIO_REWRITE_PIPELINE);
400789Sahrens 
4011544Seschrock 	zio->io_bookmark = *zb;
402789Sahrens 	zio->io_checksum = checksum;
403789Sahrens 	zio->io_compress = ZIO_COMPRESS_OFF;
404789Sahrens 
4051775Sbillm 	if (pio != NULL)
4061775Sbillm 		ASSERT3U(zio->io_ndvas, <=, BP_GET_NDVAS(bp));
4071775Sbillm 
408789Sahrens 	return (zio);
409789Sahrens }
410789Sahrens 
411789Sahrens static zio_t *
412789Sahrens zio_write_allocate(zio_t *pio, spa_t *spa, int checksum,
413789Sahrens     uint64_t txg, blkptr_t *bp, void *data, uint64_t size,
414789Sahrens     zio_done_func_t *done, void *private, int priority, int flags)
415789Sahrens {
416789Sahrens 	zio_t *zio;
417789Sahrens 
418789Sahrens 	BP_ZERO(bp);
419789Sahrens 	BP_SET_LSIZE(bp, size);
420789Sahrens 	BP_SET_PSIZE(bp, size);
421789Sahrens 	BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF);
422789Sahrens 
423789Sahrens 	zio = zio_create(pio, spa, txg, bp, data, size, done, private,
424789Sahrens 	    ZIO_TYPE_WRITE, priority, flags,
425789Sahrens 	    ZIO_STAGE_OPEN, ZIO_WRITE_ALLOCATE_PIPELINE);
426789Sahrens 
427789Sahrens 	zio->io_checksum = checksum;
428789Sahrens 	zio->io_compress = ZIO_COMPRESS_OFF;
429789Sahrens 
430789Sahrens 	return (zio);
431789Sahrens }
432789Sahrens 
433789Sahrens zio_t *
434789Sahrens zio_free(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
435789Sahrens     zio_done_func_t *done, void *private)
436789Sahrens {
437789Sahrens 	zio_t *zio;
438789Sahrens 
439789Sahrens 	ASSERT(!BP_IS_HOLE(bp));
440789Sahrens 
441789Sahrens 	if (txg == spa->spa_syncing_txg &&
442789Sahrens 	    spa->spa_sync_pass > zio_sync_pass.zp_defer_free) {
443789Sahrens 		bplist_enqueue_deferred(&spa->spa_sync_bplist, bp);
444789Sahrens 		return (zio_null(pio, spa, NULL, NULL, 0));
445789Sahrens 	}
446789Sahrens 
447789Sahrens 	zio = zio_create(pio, spa, txg, bp, NULL, 0, done, private,
448789Sahrens 	    ZIO_TYPE_FREE, ZIO_PRIORITY_FREE, 0,
449789Sahrens 	    ZIO_STAGE_OPEN, ZIO_FREE_PIPELINE);
450789Sahrens 
451789Sahrens 	zio->io_bp = &zio->io_bp_copy;
452789Sahrens 
453789Sahrens 	return (zio);
454789Sahrens }
455789Sahrens 
456789Sahrens zio_t *
457789Sahrens zio_claim(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
458789Sahrens     zio_done_func_t *done, void *private)
459789Sahrens {
460789Sahrens 	zio_t *zio;
461789Sahrens 
462789Sahrens 	/*
463789Sahrens 	 * A claim is an allocation of a specific block.  Claims are needed
464789Sahrens 	 * to support immediate writes in the intent log.  The issue is that
465789Sahrens 	 * immediate writes contain committed data, but in a txg that was
466789Sahrens 	 * *not* committed.  Upon opening the pool after an unclean shutdown,
467789Sahrens 	 * the intent log claims all blocks that contain immediate write data
468789Sahrens 	 * so that the SPA knows they're in use.
469789Sahrens 	 *
470789Sahrens 	 * All claims *must* be resolved in the first txg -- before the SPA
471789Sahrens 	 * starts allocating blocks -- so that nothing is allocated twice.
472789Sahrens 	 */
473789Sahrens 	ASSERT3U(spa->spa_uberblock.ub_rootbp.blk_birth, <, spa_first_txg(spa));
474789Sahrens 	ASSERT3U(spa_first_txg(spa), <=, txg);
475789Sahrens 
476789Sahrens 	zio = zio_create(pio, spa, txg, bp, NULL, 0, done, private,
477789Sahrens 	    ZIO_TYPE_CLAIM, ZIO_PRIORITY_NOW, 0,
478789Sahrens 	    ZIO_STAGE_OPEN, ZIO_CLAIM_PIPELINE);
479789Sahrens 
480789Sahrens 	zio->io_bp = &zio->io_bp_copy;
481789Sahrens 
482789Sahrens 	return (zio);
483789Sahrens }
484789Sahrens 
485789Sahrens zio_t *
486789Sahrens zio_ioctl(zio_t *pio, spa_t *spa, vdev_t *vd, int cmd,
487789Sahrens     zio_done_func_t *done, void *private, int priority, int flags)
488789Sahrens {
489789Sahrens 	zio_t *zio;
490789Sahrens 	int c;
491789Sahrens 
492789Sahrens 	if (vd->vdev_children == 0) {
493789Sahrens 		zio = zio_create(pio, spa, 0, NULL, NULL, 0, done, private,
494789Sahrens 		    ZIO_TYPE_IOCTL, priority, flags,
495789Sahrens 		    ZIO_STAGE_OPEN, ZIO_IOCTL_PIPELINE);
496789Sahrens 
497789Sahrens 		zio->io_vd = vd;
498789Sahrens 		zio->io_cmd = cmd;
499789Sahrens 	} else {
500789Sahrens 		zio = zio_null(pio, spa, NULL, NULL, flags);
501789Sahrens 
502789Sahrens 		for (c = 0; c < vd->vdev_children; c++)
503789Sahrens 			zio_nowait(zio_ioctl(zio, spa, vd->vdev_child[c], cmd,
504789Sahrens 			    done, private, priority, flags));
505789Sahrens 	}
506789Sahrens 
507789Sahrens 	return (zio);
508789Sahrens }
509789Sahrens 
510789Sahrens static void
511789Sahrens zio_phys_bp_init(vdev_t *vd, blkptr_t *bp, uint64_t offset, uint64_t size,
512789Sahrens     int checksum)
513789Sahrens {
514789Sahrens 	ASSERT(vd->vdev_children == 0);
515789Sahrens 
516789Sahrens 	ASSERT(size <= SPA_MAXBLOCKSIZE);
517789Sahrens 	ASSERT(P2PHASE(size, SPA_MINBLOCKSIZE) == 0);
518789Sahrens 	ASSERT(P2PHASE(offset, SPA_MINBLOCKSIZE) == 0);
519789Sahrens 
520789Sahrens 	ASSERT(offset + size <= VDEV_LABEL_START_SIZE ||
521789Sahrens 	    offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
522789Sahrens 	ASSERT3U(offset + size, <=, vd->vdev_psize);
523789Sahrens 
524789Sahrens 	BP_ZERO(bp);
525789Sahrens 
526789Sahrens 	BP_SET_LSIZE(bp, size);
527789Sahrens 	BP_SET_PSIZE(bp, size);
528789Sahrens 
529789Sahrens 	BP_SET_CHECKSUM(bp, checksum);
530789Sahrens 	BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF);
531789Sahrens 	BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
532789Sahrens 
533789Sahrens 	if (checksum != ZIO_CHECKSUM_OFF)
534789Sahrens 		ZIO_SET_CHECKSUM(&bp->blk_cksum, offset, 0, 0, 0);
535789Sahrens }
536789Sahrens 
537789Sahrens zio_t *
538789Sahrens zio_read_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
539789Sahrens     void *data, int checksum, zio_done_func_t *done, void *private,
540789Sahrens     int priority, int flags)
541789Sahrens {
542789Sahrens 	zio_t *zio;
543789Sahrens 	blkptr_t blk;
544789Sahrens 
545789Sahrens 	zio_phys_bp_init(vd, &blk, offset, size, checksum);
546789Sahrens 
547789Sahrens 	zio = zio_create(pio, vd->vdev_spa, 0, &blk, data, size, done, private,
548789Sahrens 	    ZIO_TYPE_READ, priority, flags | ZIO_FLAG_PHYSICAL,
549789Sahrens 	    ZIO_STAGE_OPEN, ZIO_READ_PHYS_PIPELINE);
550789Sahrens 
551789Sahrens 	zio->io_vd = vd;
552789Sahrens 	zio->io_offset = offset;
553789Sahrens 
554789Sahrens 	/*
555789Sahrens 	 * Work off our copy of the bp so the caller can free it.
556789Sahrens 	 */
557789Sahrens 	zio->io_bp = &zio->io_bp_copy;
558789Sahrens 
559789Sahrens 	return (zio);
560789Sahrens }
561789Sahrens 
562789Sahrens zio_t *
563789Sahrens zio_write_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
564789Sahrens     void *data, int checksum, zio_done_func_t *done, void *private,
565789Sahrens     int priority, int flags)
566789Sahrens {
567789Sahrens 	zio_block_tail_t *zbt;
568789Sahrens 	void *wbuf;
569789Sahrens 	zio_t *zio;
570789Sahrens 	blkptr_t blk;
571789Sahrens 
572789Sahrens 	zio_phys_bp_init(vd, &blk, offset, size, checksum);
573789Sahrens 
574789Sahrens 	zio = zio_create(pio, vd->vdev_spa, 0, &blk, data, size, done, private,
575789Sahrens 	    ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_PHYSICAL,
576789Sahrens 	    ZIO_STAGE_OPEN, ZIO_WRITE_PHYS_PIPELINE);
577789Sahrens 
578789Sahrens 	zio->io_vd = vd;
579789Sahrens 	zio->io_offset = offset;
580789Sahrens 
581789Sahrens 	zio->io_bp = &zio->io_bp_copy;
582789Sahrens 	zio->io_checksum = checksum;
583789Sahrens 
584789Sahrens 	if (zio_checksum_table[checksum].ci_zbt) {
585789Sahrens 		/*
586789Sahrens 		 * zbt checksums are necessarily destructive -- they modify
587789Sahrens 		 * one word of the write buffer to hold the verifier/checksum.
588789Sahrens 		 * Therefore, we must make a local copy in case the data is
589789Sahrens 		 * being written to multiple places.
590789Sahrens 		 */
591789Sahrens 		wbuf = zio_buf_alloc(size);
592789Sahrens 		bcopy(data, wbuf, size);
593789Sahrens 		zio_push_transform(zio, wbuf, size, size);
594789Sahrens 
595789Sahrens 		zbt = (zio_block_tail_t *)((char *)wbuf + size) - 1;
596789Sahrens 		zbt->zbt_cksum = blk.blk_cksum;
597789Sahrens 	}
598789Sahrens 
599789Sahrens 	return (zio);
600789Sahrens }
601789Sahrens 
602789Sahrens /*
603789Sahrens  * Create a child I/O to do some work for us.  It has no associated bp.
604789Sahrens  */
605789Sahrens zio_t *
606789Sahrens zio_vdev_child_io(zio_t *zio, blkptr_t *bp, vdev_t *vd, uint64_t offset,
607789Sahrens 	void *data, uint64_t size, int type, int priority, int flags,
608789Sahrens 	zio_done_func_t *done, void *private)
609789Sahrens {
610789Sahrens 	uint32_t pipeline = ZIO_VDEV_CHILD_PIPELINE;
611789Sahrens 	zio_t *cio;
612789Sahrens 
613789Sahrens 	if (type == ZIO_TYPE_READ && bp != NULL) {
614789Sahrens 		/*
615789Sahrens 		 * If we have the bp, then the child should perform the
616789Sahrens 		 * checksum and the parent need not.  This pushes error
617789Sahrens 		 * detection as close to the leaves as possible and
618789Sahrens 		 * eliminates redundant checksums in the interior nodes.
619789Sahrens 		 */
620789Sahrens 		pipeline |= 1U << ZIO_STAGE_CHECKSUM_VERIFY;
621789Sahrens 		zio->io_pipeline &= ~(1U << ZIO_STAGE_CHECKSUM_VERIFY);
622789Sahrens 	}
623789Sahrens 
624789Sahrens 	cio = zio_create(zio, zio->io_spa, zio->io_txg, bp, data, size,
625789Sahrens 	    done, private, type, priority,
626789Sahrens 	    (zio->io_flags & ZIO_FLAG_VDEV_INHERIT) | ZIO_FLAG_CANFAIL | flags,
6271775Sbillm 	    ZIO_STAGE_VDEV_IO_START - 1, pipeline);
628789Sahrens 
629789Sahrens 	cio->io_vd = vd;
630789Sahrens 	cio->io_offset = offset;
631789Sahrens 
632789Sahrens 	return (cio);
633789Sahrens }
634789Sahrens 
635789Sahrens /*
636789Sahrens  * ==========================================================================
637789Sahrens  * Initiate I/O, either sync or async
638789Sahrens  * ==========================================================================
639789Sahrens  */
640789Sahrens int
641789Sahrens zio_wait(zio_t *zio)
642789Sahrens {
643789Sahrens 	int error;
644789Sahrens 
645789Sahrens 	ASSERT(zio->io_stage == ZIO_STAGE_OPEN);
646789Sahrens 
647789Sahrens 	zio->io_waiter = curthread;
648789Sahrens 
649789Sahrens 	zio_next_stage_async(zio);
650789Sahrens 
651789Sahrens 	mutex_enter(&zio->io_lock);
652789Sahrens 	while (zio->io_stalled != ZIO_STAGE_DONE)
653789Sahrens 		cv_wait(&zio->io_cv, &zio->io_lock);
654789Sahrens 	mutex_exit(&zio->io_lock);
655789Sahrens 
656789Sahrens 	error = zio->io_error;
657*2856Snd150628 	mutex_destroy(&zio->io_lock);
658789Sahrens 	kmem_free(zio, sizeof (zio_t));
659789Sahrens 
660789Sahrens 	return (error);
661789Sahrens }
662789Sahrens 
663789Sahrens void
664789Sahrens zio_nowait(zio_t *zio)
665789Sahrens {
666789Sahrens 	zio_next_stage_async(zio);
667789Sahrens }
668789Sahrens 
669789Sahrens /*
670789Sahrens  * ==========================================================================
671789Sahrens  * I/O pipeline interlocks: parent/child dependency scoreboarding
672789Sahrens  * ==========================================================================
673789Sahrens  */
674789Sahrens static void
675789Sahrens zio_wait_for_children(zio_t *zio, uint32_t stage, uint64_t *countp)
676789Sahrens {
677789Sahrens 	mutex_enter(&zio->io_lock);
678789Sahrens 	if (*countp == 0) {
679789Sahrens 		ASSERT(zio->io_stalled == 0);
680789Sahrens 		mutex_exit(&zio->io_lock);
681789Sahrens 		zio_next_stage(zio);
682789Sahrens 	} else {
683789Sahrens 		zio->io_stalled = stage;
684789Sahrens 		mutex_exit(&zio->io_lock);
685789Sahrens 	}
686789Sahrens }
687789Sahrens 
688789Sahrens static void
689789Sahrens zio_notify_parent(zio_t *zio, uint32_t stage, uint64_t *countp)
690789Sahrens {
691789Sahrens 	zio_t *pio = zio->io_parent;
692789Sahrens 
693789Sahrens 	mutex_enter(&pio->io_lock);
694789Sahrens 	if (pio->io_error == 0 && !(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
695789Sahrens 		pio->io_error = zio->io_error;
696789Sahrens 	if (--*countp == 0 && pio->io_stalled == stage) {
697789Sahrens 		pio->io_stalled = 0;
698789Sahrens 		mutex_exit(&pio->io_lock);
699789Sahrens 		zio_next_stage_async(pio);
700789Sahrens 	} else {
701789Sahrens 		mutex_exit(&pio->io_lock);
702789Sahrens 	}
703789Sahrens }
704789Sahrens 
705789Sahrens static void
706789Sahrens zio_wait_children_ready(zio_t *zio)
707789Sahrens {
708789Sahrens 	zio_wait_for_children(zio, ZIO_STAGE_WAIT_CHILDREN_READY,
709789Sahrens 	    &zio->io_children_notready);
710789Sahrens }
711789Sahrens 
712789Sahrens void
713789Sahrens zio_wait_children_done(zio_t *zio)
714789Sahrens {
715789Sahrens 	zio_wait_for_children(zio, ZIO_STAGE_WAIT_CHILDREN_DONE,
716789Sahrens 	    &zio->io_children_notdone);
717789Sahrens }
718789Sahrens 
719789Sahrens static void
720789Sahrens zio_ready(zio_t *zio)
721789Sahrens {
722789Sahrens 	zio_t *pio = zio->io_parent;
723789Sahrens 
724789Sahrens 	if (pio != NULL)
725789Sahrens 		zio_notify_parent(zio, ZIO_STAGE_WAIT_CHILDREN_READY,
726789Sahrens 		    &pio->io_children_notready);
727789Sahrens 
728789Sahrens 	if (zio->io_bp)
729789Sahrens 		zio->io_bp_copy = *zio->io_bp;
730789Sahrens 
731789Sahrens 	zio_next_stage(zio);
732789Sahrens }
733789Sahrens 
734789Sahrens static void
735789Sahrens zio_done(zio_t *zio)
736789Sahrens {
737789Sahrens 	zio_t *pio = zio->io_parent;
738789Sahrens 	spa_t *spa = zio->io_spa;
739789Sahrens 	blkptr_t *bp = zio->io_bp;
740789Sahrens 	vdev_t *vd = zio->io_vd;
741896Smaybee 	char blkbuf[BP_SPRINTF_LEN];
742789Sahrens 
743789Sahrens 	ASSERT(zio->io_children_notready == 0);
744789Sahrens 	ASSERT(zio->io_children_notdone == 0);
745789Sahrens 
746789Sahrens 	if (bp != NULL) {
747789Sahrens 		ASSERT(bp->blk_pad[0] == 0);
748789Sahrens 		ASSERT(bp->blk_pad[1] == 0);
749789Sahrens 		ASSERT(bp->blk_pad[2] == 0);
750789Sahrens 		ASSERT(bcmp(bp, &zio->io_bp_copy, sizeof (blkptr_t)) == 0);
751789Sahrens 		if (zio->io_type == ZIO_TYPE_WRITE && !BP_IS_HOLE(bp) &&
7521775Sbillm 		    !(zio->io_flags & ZIO_FLAG_IO_REPAIR)) {
753789Sahrens 			ASSERT(!BP_SHOULD_BYTESWAP(bp));
7541775Sbillm 			if (zio->io_ndvas != 0)
7551775Sbillm 				ASSERT3U(zio->io_ndvas, <=, BP_GET_NDVAS(bp));
7561775Sbillm 			ASSERT(BP_COUNT_GANG(bp) == 0 ||
7571775Sbillm 			    (BP_COUNT_GANG(bp) == BP_GET_NDVAS(bp)));
7581775Sbillm 		}
759789Sahrens 	}
760789Sahrens 
761789Sahrens 	if (vd != NULL)
762789Sahrens 		vdev_stat_update(zio);
763789Sahrens 
764789Sahrens 	if (zio->io_error) {
7651544Seschrock 		/*
7661544Seschrock 		 * If this I/O is attached to a particular vdev,
7671544Seschrock 		 * generate an error message describing the I/O failure
7681544Seschrock 		 * at the block level.  We ignore these errors if the
7691544Seschrock 		 * device is currently unavailable.
7701544Seschrock 		 */
7711732Sbonwick 		if (zio->io_error != ECKSUM && vd != NULL && !vdev_is_dead(vd))
7721544Seschrock 			zfs_ereport_post(FM_EREPORT_ZFS_IO,
7731732Sbonwick 			    zio->io_spa, vd, zio, 0, 0);
774789Sahrens 
7751544Seschrock 		if ((zio->io_error == EIO ||
7761544Seschrock 		    !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) &&
7771544Seschrock 		    zio->io_logical == zio) {
7781544Seschrock 			/*
7791544Seschrock 			 * For root I/O requests, tell the SPA to log the error
7801544Seschrock 			 * appropriately.  Also, generate a logical data
7811544Seschrock 			 * ereport.
7821544Seschrock 			 */
7831544Seschrock 			spa_log_error(zio->io_spa, zio);
7841544Seschrock 
7851544Seschrock 			zfs_ereport_post(FM_EREPORT_ZFS_DATA,
7861544Seschrock 			    zio->io_spa, NULL, zio, 0, 0);
7871544Seschrock 		}
788789Sahrens 
7891544Seschrock 		/*
7901544Seschrock 		 * For I/O requests that cannot fail, panic appropriately.
7911544Seschrock 		 */
7921544Seschrock 		if (!(zio->io_flags & ZIO_FLAG_CANFAIL)) {
7931544Seschrock 			sprintf_blkptr(blkbuf, BP_SPRINTF_LEN,
7941544Seschrock 			    bp ? bp : &zio->io_bp_copy);
7951544Seschrock 			panic("ZFS: %s (%s on %s off %llx: zio %p %s): error "
7961544Seschrock 			    "%d", zio->io_error == ECKSUM ?
7971544Seschrock 			    "bad checksum" : "I/O failure",
7981544Seschrock 			    zio_type_name[zio->io_type],
7991544Seschrock 			    vdev_description(vd),
8001544Seschrock 			    (u_longlong_t)zio->io_offset,
8011544Seschrock 			    zio, blkbuf, zio->io_error);
8021544Seschrock 		}
803789Sahrens 	}
804789Sahrens 
805789Sahrens 	zio_clear_transform_stack(zio);
806789Sahrens 
807789Sahrens 	if (zio->io_done)
808789Sahrens 		zio->io_done(zio);
809789Sahrens 
810789Sahrens 	ASSERT(zio->io_delegate_list == NULL);
811789Sahrens 	ASSERT(zio->io_delegate_next == NULL);
812789Sahrens 
813789Sahrens 	if (pio != NULL) {
814789Sahrens 		zio_t *next, *prev;
815789Sahrens 
816789Sahrens 		mutex_enter(&pio->io_lock);
817789Sahrens 		next = zio->io_sibling_next;
818789Sahrens 		prev = zio->io_sibling_prev;
819789Sahrens 		if (next != NULL)
820789Sahrens 			next->io_sibling_prev = prev;
821789Sahrens 		if (prev != NULL)
822789Sahrens 			prev->io_sibling_next = next;
823789Sahrens 		if (pio->io_child == zio)
824789Sahrens 			pio->io_child = next;
825789Sahrens 		mutex_exit(&pio->io_lock);
826789Sahrens 
827789Sahrens 		zio_notify_parent(zio, ZIO_STAGE_WAIT_CHILDREN_DONE,
828789Sahrens 		    &pio->io_children_notdone);
829789Sahrens 	}
830789Sahrens 
831789Sahrens 	if (pio == NULL && !(zio->io_flags & ZIO_FLAG_CONFIG_HELD))
8321544Seschrock 		spa_config_exit(spa, zio);
833789Sahrens 
834789Sahrens 	if (zio->io_waiter != NULL) {
835789Sahrens 		mutex_enter(&zio->io_lock);
836789Sahrens 		ASSERT(zio->io_stage == ZIO_STAGE_DONE);
837789Sahrens 		zio->io_stalled = zio->io_stage;
838789Sahrens 		cv_broadcast(&zio->io_cv);
839789Sahrens 		mutex_exit(&zio->io_lock);
840789Sahrens 	} else {
841789Sahrens 		kmem_free(zio, sizeof (zio_t));
842789Sahrens 	}
843789Sahrens }
844789Sahrens 
845789Sahrens /*
846789Sahrens  * ==========================================================================
847789Sahrens  * Compression support
848789Sahrens  * ==========================================================================
849789Sahrens  */
850789Sahrens static void
851789Sahrens zio_write_compress(zio_t *zio)
852789Sahrens {
853789Sahrens 	int compress = zio->io_compress;
854789Sahrens 	blkptr_t *bp = zio->io_bp;
855789Sahrens 	void *cbuf;
856789Sahrens 	uint64_t lsize = zio->io_size;
857789Sahrens 	uint64_t csize = lsize;
858789Sahrens 	uint64_t cbufsize = 0;
859789Sahrens 	int pass;
860789Sahrens 
861789Sahrens 	if (bp->blk_birth == zio->io_txg) {
862789Sahrens 		/*
863789Sahrens 		 * We're rewriting an existing block, which means we're
864789Sahrens 		 * working on behalf of spa_sync().  For spa_sync() to
865789Sahrens 		 * converge, it must eventually be the case that we don't
866789Sahrens 		 * have to allocate new blocks.  But compression changes
867789Sahrens 		 * the blocksize, which forces a reallocate, and makes
868789Sahrens 		 * convergence take longer.  Therefore, after the first
869789Sahrens 		 * few passes, stop compressing to ensure convergence.
870789Sahrens 		 */
871789Sahrens 		pass = spa_sync_pass(zio->io_spa);
872789Sahrens 		if (pass > zio_sync_pass.zp_dontcompress)
873789Sahrens 			compress = ZIO_COMPRESS_OFF;
874789Sahrens 	} else {
875789Sahrens 		ASSERT(BP_IS_HOLE(bp));
876789Sahrens 		pass = 1;
877789Sahrens 	}
878789Sahrens 
879789Sahrens 	if (compress != ZIO_COMPRESS_OFF)
880789Sahrens 		if (!zio_compress_data(compress, zio->io_data, zio->io_size,
881789Sahrens 		    &cbuf, &csize, &cbufsize))
882789Sahrens 			compress = ZIO_COMPRESS_OFF;
883789Sahrens 
884789Sahrens 	if (compress != ZIO_COMPRESS_OFF && csize != 0)
885789Sahrens 		zio_push_transform(zio, cbuf, csize, cbufsize);
886789Sahrens 
887789Sahrens 	/*
888789Sahrens 	 * The final pass of spa_sync() must be all rewrites, but the first
889789Sahrens 	 * few passes offer a trade-off: allocating blocks defers convergence,
890789Sahrens 	 * but newly allocated blocks are sequential, so they can be written
891789Sahrens 	 * to disk faster.  Therefore, we allow the first few passes of
892789Sahrens 	 * spa_sync() to reallocate new blocks, but force rewrites after that.
893789Sahrens 	 * There should only be a handful of blocks after pass 1 in any case.
894789Sahrens 	 */
895789Sahrens 	if (bp->blk_birth == zio->io_txg && BP_GET_PSIZE(bp) == csize &&
896789Sahrens 	    pass > zio_sync_pass.zp_rewrite) {
897789Sahrens 		ASSERT(csize != 0);
898789Sahrens 		ASSERT3U(BP_GET_COMPRESS(bp), ==, compress);
899789Sahrens 		ASSERT3U(BP_GET_LSIZE(bp), ==, lsize);
900789Sahrens 
901789Sahrens 		zio->io_pipeline = ZIO_REWRITE_PIPELINE;
902789Sahrens 	} else {
903789Sahrens 		if (bp->blk_birth == zio->io_txg) {
904789Sahrens 			ASSERT3U(BP_GET_LSIZE(bp), ==, lsize);
905789Sahrens 			bzero(bp, sizeof (blkptr_t));
906789Sahrens 		}
907789Sahrens 		if (csize == 0) {
908789Sahrens 			BP_ZERO(bp);
909789Sahrens 			zio->io_pipeline = ZIO_WAIT_FOR_CHILDREN_PIPELINE;
910789Sahrens 		} else {
9111775Sbillm 			ASSERT3U(BP_GET_NDVAS(bp), ==, 0);
912789Sahrens 			BP_SET_LSIZE(bp, lsize);
913789Sahrens 			BP_SET_PSIZE(bp, csize);
914789Sahrens 			BP_SET_COMPRESS(bp, compress);
915789Sahrens 			zio->io_pipeline = ZIO_WRITE_ALLOCATE_PIPELINE;
916789Sahrens 		}
917789Sahrens 	}
918789Sahrens 
919789Sahrens 	zio_next_stage(zio);
920789Sahrens }
921789Sahrens 
922789Sahrens static void
923789Sahrens zio_read_decompress(zio_t *zio)
924789Sahrens {
925789Sahrens 	blkptr_t *bp = zio->io_bp;
926789Sahrens 	void *data;
927789Sahrens 	uint64_t size;
928789Sahrens 	uint64_t bufsize;
929789Sahrens 	int compress = BP_GET_COMPRESS(bp);
930789Sahrens 
931789Sahrens 	ASSERT(compress != ZIO_COMPRESS_OFF);
932789Sahrens 
933789Sahrens 	zio_pop_transform(zio, &data, &size, &bufsize);
934789Sahrens 
935789Sahrens 	if (zio_decompress_data(compress, data, size,
936789Sahrens 	    zio->io_data, zio->io_size))
937789Sahrens 		zio->io_error = EIO;
938789Sahrens 
939789Sahrens 	zio_buf_free(data, bufsize);
940789Sahrens 
941789Sahrens 	zio_next_stage(zio);
942789Sahrens }
943789Sahrens 
944789Sahrens /*
945789Sahrens  * ==========================================================================
946789Sahrens  * Gang block support
947789Sahrens  * ==========================================================================
948789Sahrens  */
949789Sahrens static void
950789Sahrens zio_gang_pipeline(zio_t *zio)
951789Sahrens {
952789Sahrens 	/*
953789Sahrens 	 * By default, the pipeline assumes that we're dealing with a gang
954789Sahrens 	 * block.  If we're not, strip out any gang-specific stages.
955789Sahrens 	 */
9561775Sbillm 	if (!BP_IS_GANG(zio->io_bp))
957789Sahrens 		zio->io_pipeline &= ~ZIO_GANG_STAGES;
958789Sahrens 
959789Sahrens 	zio_next_stage(zio);
960789Sahrens }
961789Sahrens 
962789Sahrens static void
963789Sahrens zio_gang_byteswap(zio_t *zio)
964789Sahrens {
965789Sahrens 	ASSERT(zio->io_size == SPA_GANGBLOCKSIZE);
966789Sahrens 
967789Sahrens 	if (BP_SHOULD_BYTESWAP(zio->io_bp))
968789Sahrens 		byteswap_uint64_array(zio->io_data, zio->io_size);
969789Sahrens }
970789Sahrens 
971789Sahrens static void
972789Sahrens zio_get_gang_header(zio_t *zio)
973789Sahrens {
974789Sahrens 	blkptr_t *bp = zio->io_bp;
975789Sahrens 	uint64_t gsize = SPA_GANGBLOCKSIZE;
976789Sahrens 	void *gbuf = zio_buf_alloc(gsize);
977789Sahrens 
9781775Sbillm 	ASSERT(BP_IS_GANG(bp));
979789Sahrens 
980789Sahrens 	zio_push_transform(zio, gbuf, gsize, gsize);
981789Sahrens 
982789Sahrens 	zio_nowait(zio_create(zio, zio->io_spa, bp->blk_birth, bp, gbuf, gsize,
983789Sahrens 	    NULL, NULL, ZIO_TYPE_READ, zio->io_priority,
984789Sahrens 	    zio->io_flags & ZIO_FLAG_GANG_INHERIT,
985789Sahrens 	    ZIO_STAGE_OPEN, ZIO_READ_PIPELINE));
986789Sahrens 
987789Sahrens 	zio_wait_children_done(zio);
988789Sahrens }
989789Sahrens 
990789Sahrens static void
991789Sahrens zio_read_gang_members(zio_t *zio)
992789Sahrens {
993789Sahrens 	zio_gbh_phys_t *gbh;
994789Sahrens 	uint64_t gsize, gbufsize, loff, lsize;
995789Sahrens 	int i;
996789Sahrens 
9971775Sbillm 	ASSERT(BP_IS_GANG(zio->io_bp));
998789Sahrens 
999789Sahrens 	zio_gang_byteswap(zio);
1000789Sahrens 	zio_pop_transform(zio, (void **)&gbh, &gsize, &gbufsize);
1001789Sahrens 
1002789Sahrens 	for (loff = 0, i = 0; loff != zio->io_size; loff += lsize, i++) {
1003789Sahrens 		blkptr_t *gbp = &gbh->zg_blkptr[i];
1004789Sahrens 		lsize = BP_GET_PSIZE(gbp);
1005789Sahrens 
1006789Sahrens 		ASSERT(BP_GET_COMPRESS(gbp) == ZIO_COMPRESS_OFF);
1007789Sahrens 		ASSERT3U(lsize, ==, BP_GET_LSIZE(gbp));
1008789Sahrens 		ASSERT3U(loff + lsize, <=, zio->io_size);
1009789Sahrens 		ASSERT(i < SPA_GBH_NBLKPTRS);
1010789Sahrens 		ASSERT(!BP_IS_HOLE(gbp));
1011789Sahrens 
1012789Sahrens 		zio_nowait(zio_read(zio, zio->io_spa, gbp,
1013789Sahrens 		    (char *)zio->io_data + loff, lsize, NULL, NULL,
10141544Seschrock 		    zio->io_priority, zio->io_flags & ZIO_FLAG_GANG_INHERIT,
10151544Seschrock 		    &zio->io_bookmark));
1016789Sahrens 	}
1017789Sahrens 
1018789Sahrens 	zio_buf_free(gbh, gbufsize);
1019789Sahrens 	zio_wait_children_done(zio);
1020789Sahrens }
1021789Sahrens 
1022789Sahrens static void
1023789Sahrens zio_rewrite_gang_members(zio_t *zio)
1024789Sahrens {
1025789Sahrens 	zio_gbh_phys_t *gbh;
1026789Sahrens 	uint64_t gsize, gbufsize, loff, lsize;
1027789Sahrens 	int i;
1028789Sahrens 
10291775Sbillm 	ASSERT(BP_IS_GANG(zio->io_bp));
1030789Sahrens 	ASSERT3U(zio->io_size, ==, SPA_GANGBLOCKSIZE);
1031789Sahrens 
1032789Sahrens 	zio_gang_byteswap(zio);
1033789Sahrens 	zio_pop_transform(zio, (void **)&gbh, &gsize, &gbufsize);
1034789Sahrens 
1035789Sahrens 	ASSERT(gsize == gbufsize);
1036789Sahrens 
1037789Sahrens 	for (loff = 0, i = 0; loff != zio->io_size; loff += lsize, i++) {
1038789Sahrens 		blkptr_t *gbp = &gbh->zg_blkptr[i];
1039789Sahrens 		lsize = BP_GET_PSIZE(gbp);
1040789Sahrens 
1041789Sahrens 		ASSERT(BP_GET_COMPRESS(gbp) == ZIO_COMPRESS_OFF);
1042789Sahrens 		ASSERT3U(lsize, ==, BP_GET_LSIZE(gbp));
1043789Sahrens 		ASSERT3U(loff + lsize, <=, zio->io_size);
1044789Sahrens 		ASSERT(i < SPA_GBH_NBLKPTRS);
1045789Sahrens 		ASSERT(!BP_IS_HOLE(gbp));
1046789Sahrens 
1047789Sahrens 		zio_nowait(zio_rewrite(zio, zio->io_spa, zio->io_checksum,
1048789Sahrens 		    zio->io_txg, gbp, (char *)zio->io_data + loff, lsize,
10491544Seschrock 		    NULL, NULL, zio->io_priority, zio->io_flags,
10501544Seschrock 		    &zio->io_bookmark));
1051789Sahrens 	}
1052789Sahrens 
1053789Sahrens 	zio_push_transform(zio, gbh, gsize, gbufsize);
1054789Sahrens 	zio_wait_children_ready(zio);
1055789Sahrens }
1056789Sahrens 
1057789Sahrens static void
1058789Sahrens zio_free_gang_members(zio_t *zio)
1059789Sahrens {
1060789Sahrens 	zio_gbh_phys_t *gbh;
1061789Sahrens 	uint64_t gsize, gbufsize;
1062789Sahrens 	int i;
1063789Sahrens 
10641775Sbillm 	ASSERT(BP_IS_GANG(zio->io_bp));
1065789Sahrens 
1066789Sahrens 	zio_gang_byteswap(zio);
1067789Sahrens 	zio_pop_transform(zio, (void **)&gbh, &gsize, &gbufsize);
1068789Sahrens 
1069789Sahrens 	for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
1070789Sahrens 		blkptr_t *gbp = &gbh->zg_blkptr[i];
1071789Sahrens 
1072789Sahrens 		if (BP_IS_HOLE(gbp))
1073789Sahrens 			continue;
1074789Sahrens 		zio_nowait(zio_free(zio, zio->io_spa, zio->io_txg,
1075789Sahrens 		    gbp, NULL, NULL));
1076789Sahrens 	}
1077789Sahrens 
1078789Sahrens 	zio_buf_free(gbh, gbufsize);
1079789Sahrens 	zio_next_stage(zio);
1080789Sahrens }
1081789Sahrens 
1082789Sahrens static void
1083789Sahrens zio_claim_gang_members(zio_t *zio)
1084789Sahrens {
1085789Sahrens 	zio_gbh_phys_t *gbh;
1086789Sahrens 	uint64_t gsize, gbufsize;
1087789Sahrens 	int i;
1088789Sahrens 
10891775Sbillm 	ASSERT(BP_IS_GANG(zio->io_bp));
1090789Sahrens 
1091789Sahrens 	zio_gang_byteswap(zio);
1092789Sahrens 	zio_pop_transform(zio, (void **)&gbh, &gsize, &gbufsize);
1093789Sahrens 
1094789Sahrens 	for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
1095789Sahrens 		blkptr_t *gbp = &gbh->zg_blkptr[i];
1096789Sahrens 		if (BP_IS_HOLE(gbp))
1097789Sahrens 			continue;
1098789Sahrens 		zio_nowait(zio_claim(zio, zio->io_spa, zio->io_txg,
1099789Sahrens 		    gbp, NULL, NULL));
1100789Sahrens 	}
1101789Sahrens 
1102789Sahrens 	zio_buf_free(gbh, gbufsize);
1103789Sahrens 	zio_next_stage(zio);
1104789Sahrens }
1105789Sahrens 
1106789Sahrens static void
1107789Sahrens zio_write_allocate_gang_member_done(zio_t *zio)
1108789Sahrens {
1109789Sahrens 	zio_t *pio = zio->io_parent;
11101775Sbillm 	dva_t *cdva = zio->io_bp->blk_dva;
11111775Sbillm 	dva_t *pdva = pio->io_bp->blk_dva;
1112789Sahrens 	uint64_t asize;
11131775Sbillm 	int d;
1114789Sahrens 
11151775Sbillm 	ASSERT3U(pio->io_ndvas, ==, zio->io_ndvas);
11161775Sbillm 	ASSERT3U(BP_GET_NDVAS(zio->io_bp), <=, BP_GET_NDVAS(pio->io_bp));
11171775Sbillm 	ASSERT3U(zio->io_ndvas, <=, BP_GET_NDVAS(zio->io_bp));
11181775Sbillm 	ASSERT3U(pio->io_ndvas, <=, BP_GET_NDVAS(pio->io_bp));
11191775Sbillm 
1120789Sahrens 	mutex_enter(&pio->io_lock);
11211775Sbillm 	for (d = 0; d < BP_GET_NDVAS(pio->io_bp); d++) {
11221775Sbillm 		ASSERT(DVA_GET_GANG(&pdva[d]));
11231775Sbillm 		asize = DVA_GET_ASIZE(&pdva[d]);
11241775Sbillm 		asize += DVA_GET_ASIZE(&cdva[d]);
11251775Sbillm 		DVA_SET_ASIZE(&pdva[d], asize);
11261775Sbillm 	}
1127789Sahrens 	mutex_exit(&pio->io_lock);
1128789Sahrens }
1129789Sahrens 
1130789Sahrens static void
1131789Sahrens zio_write_allocate_gang_members(zio_t *zio)
1132789Sahrens {
1133789Sahrens 	blkptr_t *bp = zio->io_bp;
11341775Sbillm 	dva_t *dva = bp->blk_dva;
11351775Sbillm 	spa_t *spa = zio->io_spa;
1136789Sahrens 	zio_gbh_phys_t *gbh;
11371775Sbillm 	uint64_t txg = zio->io_txg;
1138789Sahrens 	uint64_t resid = zio->io_size;
1139789Sahrens 	uint64_t maxalloc = P2ROUNDUP(zio->io_size >> 1, SPA_MINBLOCKSIZE);
1140789Sahrens 	uint64_t gsize, loff, lsize;
1141789Sahrens 	uint32_t gbps_left;
11421775Sbillm 	int ndvas = zio->io_ndvas;
11431775Sbillm 	int gbh_ndvas = MIN(ndvas + 1, spa_max_replication(spa));
1144789Sahrens 	int error;
11451775Sbillm 	int i, d;
1146789Sahrens 
1147789Sahrens 	gsize = SPA_GANGBLOCKSIZE;
1148789Sahrens 	gbps_left = SPA_GBH_NBLKPTRS;
1149789Sahrens 
11501775Sbillm 	error = metaslab_alloc(spa, gsize, bp, gbh_ndvas, txg, NULL);
1151789Sahrens 	if (error == ENOSPC)
1152789Sahrens 		panic("can't allocate gang block header");
1153789Sahrens 	ASSERT(error == 0);
1154789Sahrens 
11551775Sbillm 	for (d = 0; d < gbh_ndvas; d++)
11561775Sbillm 		DVA_SET_GANG(&dva[d], 1);
1157789Sahrens 
11581775Sbillm 	bp->blk_birth = txg;
1159789Sahrens 
1160789Sahrens 	gbh = zio_buf_alloc(gsize);
1161789Sahrens 	bzero(gbh, gsize);
1162789Sahrens 
11631775Sbillm 	/* We need to test multi-level gang blocks */
11641775Sbillm 	if (maxalloc >= zio_gang_bang && (lbolt & 0x1) == 0)
11651775Sbillm 		maxalloc = MAX(maxalloc >> 2, SPA_MINBLOCKSIZE);
11661775Sbillm 
1167789Sahrens 	for (loff = 0, i = 0; loff != zio->io_size;
1168789Sahrens 	    loff += lsize, resid -= lsize, gbps_left--, i++) {
1169789Sahrens 		blkptr_t *gbp = &gbh->zg_blkptr[i];
11701775Sbillm 		dva = gbp->blk_dva;
1171789Sahrens 
1172789Sahrens 		ASSERT(gbps_left != 0);
1173789Sahrens 		maxalloc = MIN(maxalloc, resid);
1174789Sahrens 
1175789Sahrens 		while (resid <= maxalloc * gbps_left) {
11761775Sbillm 			error = metaslab_alloc(spa, maxalloc, gbp, ndvas,
11771775Sbillm 			    txg, bp);
1178789Sahrens 			if (error == 0)
1179789Sahrens 				break;
1180789Sahrens 			ASSERT3U(error, ==, ENOSPC);
1181789Sahrens 			if (maxalloc == SPA_MINBLOCKSIZE)
1182789Sahrens 				panic("really out of space");
1183789Sahrens 			maxalloc = P2ROUNDUP(maxalloc >> 1, SPA_MINBLOCKSIZE);
1184789Sahrens 		}
1185789Sahrens 
1186789Sahrens 		if (resid <= maxalloc * gbps_left) {
1187789Sahrens 			lsize = maxalloc;
1188789Sahrens 			BP_SET_LSIZE(gbp, lsize);
1189789Sahrens 			BP_SET_PSIZE(gbp, lsize);
1190789Sahrens 			BP_SET_COMPRESS(gbp, ZIO_COMPRESS_OFF);
11911775Sbillm 			gbp->blk_birth = txg;
11921775Sbillm 			zio_nowait(zio_rewrite(zio, spa,
11931775Sbillm 			    zio->io_checksum, txg, gbp,
1194789Sahrens 			    (char *)zio->io_data + loff, lsize,
1195789Sahrens 			    zio_write_allocate_gang_member_done, NULL,
11961544Seschrock 			    zio->io_priority, zio->io_flags,
11971544Seschrock 			    &zio->io_bookmark));
1198789Sahrens 		} else {
1199789Sahrens 			lsize = P2ROUNDUP(resid / gbps_left, SPA_MINBLOCKSIZE);
1200789Sahrens 			ASSERT(lsize != SPA_MINBLOCKSIZE);
12011775Sbillm 			zio_nowait(zio_write_allocate(zio, spa,
12021775Sbillm 			    zio->io_checksum, txg, gbp,
1203789Sahrens 			    (char *)zio->io_data + loff, lsize,
1204789Sahrens 			    zio_write_allocate_gang_member_done, NULL,
1205789Sahrens 			    zio->io_priority, zio->io_flags));
1206789Sahrens 		}
1207789Sahrens 	}
1208789Sahrens 
1209789Sahrens 	ASSERT(resid == 0 && loff == zio->io_size);
1210789Sahrens 
1211789Sahrens 	zio->io_pipeline |= 1U << ZIO_STAGE_GANG_CHECKSUM_GENERATE;
1212789Sahrens 
1213789Sahrens 	zio_push_transform(zio, gbh, gsize, gsize);
12141775Sbillm 	/*
12151775Sbillm 	 * As much as we'd like this to be zio_wait_children_ready(),
12161775Sbillm 	 * updating our ASIZE doesn't happen until the io_done callback,
12171775Sbillm 	 * so we have to wait for that to finish in order for our BP
12181775Sbillm 	 * to be stable.
12191775Sbillm 	 */
1220789Sahrens 	zio_wait_children_done(zio);
1221789Sahrens }
1222789Sahrens 
1223789Sahrens /*
1224789Sahrens  * ==========================================================================
1225789Sahrens  * Allocate and free blocks
1226789Sahrens  * ==========================================================================
1227789Sahrens  */
1228789Sahrens static void
1229789Sahrens zio_dva_allocate(zio_t *zio)
1230789Sahrens {
1231789Sahrens 	blkptr_t *bp = zio->io_bp;
1232789Sahrens 	int error;
1233789Sahrens 
1234789Sahrens 	ASSERT(BP_IS_HOLE(bp));
12351775Sbillm 	ASSERT3U(BP_GET_NDVAS(bp), ==, 0);
12361775Sbillm 	ASSERT3U(zio->io_ndvas, >, 0);
12371775Sbillm 	ASSERT3U(zio->io_ndvas, <=, spa_max_replication(zio->io_spa));
1238789Sahrens 
1239789Sahrens 	/* For testing, make some blocks above a certain size be gang blocks */
1240789Sahrens 	if (zio->io_size >= zio_gang_bang && (lbolt & 0x3) == 0) {
1241789Sahrens 		zio_write_allocate_gang_members(zio);
1242789Sahrens 		return;
1243789Sahrens 	}
1244789Sahrens 
1245789Sahrens 	ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp));
1246789Sahrens 
12471775Sbillm 	error = metaslab_alloc(zio->io_spa, zio->io_size, bp, zio->io_ndvas,
12481775Sbillm 	    zio->io_txg, NULL);
1249789Sahrens 
1250789Sahrens 	if (error == 0) {
1251789Sahrens 		bp->blk_birth = zio->io_txg;
1252789Sahrens 	} else if (error == ENOSPC) {
1253789Sahrens 		if (zio->io_size == SPA_MINBLOCKSIZE)
1254789Sahrens 			panic("really, truly out of space");
1255789Sahrens 		zio_write_allocate_gang_members(zio);
1256789Sahrens 		return;
1257789Sahrens 	} else {
1258789Sahrens 		zio->io_error = error;
1259789Sahrens 	}
1260789Sahrens 	zio_next_stage(zio);
1261789Sahrens }
1262789Sahrens 
1263789Sahrens static void
1264789Sahrens zio_dva_free(zio_t *zio)
1265789Sahrens {
1266789Sahrens 	blkptr_t *bp = zio->io_bp;
1267789Sahrens 
12681807Sbonwick 	metaslab_free(zio->io_spa, bp, zio->io_txg, B_FALSE);
1269789Sahrens 
1270789Sahrens 	BP_ZERO(bp);
1271789Sahrens 
1272789Sahrens 	zio_next_stage(zio);
1273789Sahrens }
1274789Sahrens 
1275789Sahrens static void
1276789Sahrens zio_dva_claim(zio_t *zio)
1277789Sahrens {
12781807Sbonwick 	zio->io_error = metaslab_claim(zio->io_spa, zio->io_bp, zio->io_txg);
1279789Sahrens 
1280789Sahrens 	zio_next_stage(zio);
1281789Sahrens }
1282789Sahrens 
1283789Sahrens /*
1284789Sahrens  * ==========================================================================
1285789Sahrens  * Read and write to physical devices
1286789Sahrens  * ==========================================================================
1287789Sahrens  */
1288789Sahrens 
1289789Sahrens static void
12901775Sbillm zio_vdev_io_start(zio_t *zio)
1291789Sahrens {
1292789Sahrens 	vdev_t *vd = zio->io_vd;
12931775Sbillm 	vdev_t *tvd = vd ? vd->vdev_top : NULL;
12941775Sbillm 	blkptr_t *bp = zio->io_bp;
12951775Sbillm 	uint64_t align;
1296789Sahrens 
12971775Sbillm 	if (vd == NULL) {
12981775Sbillm 		/* The mirror_ops handle multiple DVAs in a single BP */
12991775Sbillm 		vdev_mirror_ops.vdev_op_io_start(zio);
13001775Sbillm 		return;
13011775Sbillm 	}
13021775Sbillm 
13031775Sbillm 	align = 1ULL << tvd->vdev_ashift;
13041775Sbillm 
13051732Sbonwick 	if (zio->io_retries == 0 && vd == tvd)
1306789Sahrens 		zio->io_flags |= ZIO_FLAG_FAILFAST;
1307789Sahrens 
13081775Sbillm 	if (!(zio->io_flags & ZIO_FLAG_PHYSICAL) &&
13091775Sbillm 	    vd->vdev_children == 0) {
1310789Sahrens 		zio->io_flags |= ZIO_FLAG_PHYSICAL;
1311789Sahrens 		zio->io_offset += VDEV_LABEL_START_SIZE;
1312789Sahrens 	}
1313789Sahrens 
13141732Sbonwick 	if (P2PHASE(zio->io_size, align) != 0) {
13151732Sbonwick 		uint64_t asize = P2ROUNDUP(zio->io_size, align);
13161732Sbonwick 		char *abuf = zio_buf_alloc(asize);
13171732Sbonwick 		ASSERT(vd == tvd);
13181732Sbonwick 		if (zio->io_type == ZIO_TYPE_WRITE) {
13191732Sbonwick 			bcopy(zio->io_data, abuf, zio->io_size);
13201732Sbonwick 			bzero(abuf + zio->io_size, asize - zio->io_size);
13211732Sbonwick 		}
13221732Sbonwick 		zio_push_transform(zio, abuf, asize, asize);
13231732Sbonwick 		ASSERT(!(zio->io_flags & ZIO_FLAG_SUBBLOCK));
13241732Sbonwick 		zio->io_flags |= ZIO_FLAG_SUBBLOCK;
13251732Sbonwick 	}
13261732Sbonwick 
13271732Sbonwick 	ASSERT(P2PHASE(zio->io_offset, align) == 0);
13281732Sbonwick 	ASSERT(P2PHASE(zio->io_size, align) == 0);
13291732Sbonwick 	ASSERT(bp == NULL ||
13301732Sbonwick 	    P2ROUNDUP(ZIO_GET_IOSIZE(zio), align) == zio->io_size);
1331789Sahrens 	ASSERT(zio->io_type != ZIO_TYPE_WRITE || (spa_mode & FWRITE));
1332789Sahrens 
1333789Sahrens 	vdev_io_start(zio);
1334789Sahrens 
1335789Sahrens 	/* zio_next_stage_async() gets called from io completion interrupt */
1336789Sahrens }
1337789Sahrens 
1338789Sahrens static void
1339789Sahrens zio_vdev_io_done(zio_t *zio)
1340789Sahrens {
13411775Sbillm 	if (zio->io_vd == NULL)
13421775Sbillm 		/* The mirror_ops handle multiple DVAs in a single BP */
13431775Sbillm 		vdev_mirror_ops.vdev_op_io_done(zio);
13441775Sbillm 	else
13451775Sbillm 		vdev_io_done(zio);
1346789Sahrens }
1347789Sahrens 
1348789Sahrens /* XXPOLICY */
13491544Seschrock boolean_t
1350789Sahrens zio_should_retry(zio_t *zio)
1351789Sahrens {
1352789Sahrens 	vdev_t *vd = zio->io_vd;
1353789Sahrens 
1354789Sahrens 	if (zio->io_error == 0)
1355789Sahrens 		return (B_FALSE);
1356789Sahrens 	if (zio->io_delegate_list != NULL)
1357789Sahrens 		return (B_FALSE);
13581775Sbillm 	if (vd && vd != vd->vdev_top)
1359789Sahrens 		return (B_FALSE);
1360789Sahrens 	if (zio->io_flags & ZIO_FLAG_DONT_RETRY)
1361789Sahrens 		return (B_FALSE);
13621544Seschrock 	if (zio->io_retries > 0)
1363789Sahrens 		return (B_FALSE);
1364789Sahrens 
1365789Sahrens 	return (B_TRUE);
1366789Sahrens }
1367789Sahrens 
1368789Sahrens static void
1369789Sahrens zio_vdev_io_assess(zio_t *zio)
1370789Sahrens {
1371789Sahrens 	vdev_t *vd = zio->io_vd;
13721775Sbillm 	vdev_t *tvd = vd ? vd->vdev_top : NULL;
1373789Sahrens 
13741544Seschrock 	ASSERT(zio->io_vsd == NULL);
1375789Sahrens 
13761732Sbonwick 	if (zio->io_flags & ZIO_FLAG_SUBBLOCK) {
13771732Sbonwick 		void *abuf;
13781732Sbonwick 		uint64_t asize;
13791732Sbonwick 		ASSERT(vd == tvd);
13801732Sbonwick 		zio_pop_transform(zio, &abuf, &asize, &asize);
13811732Sbonwick 		if (zio->io_type == ZIO_TYPE_READ)
13821732Sbonwick 			bcopy(abuf, zio->io_data, zio->io_size);
13831732Sbonwick 		zio_buf_free(abuf, asize);
13841732Sbonwick 		zio->io_flags &= ~ZIO_FLAG_SUBBLOCK;
13851732Sbonwick 	}
13861732Sbonwick 
13871544Seschrock 	if (zio_injection_enabled && !zio->io_error)
13881544Seschrock 		zio->io_error = zio_handle_fault_injection(zio, EIO);
1389789Sahrens 
1390789Sahrens 	/*
1391789Sahrens 	 * If the I/O failed, determine whether we should attempt to retry it.
1392789Sahrens 	 */
1393789Sahrens 	/* XXPOLICY */
1394789Sahrens 	if (zio_should_retry(zio)) {
1395789Sahrens 		ASSERT(tvd == vd);
1396789Sahrens 
1397789Sahrens 		zio->io_retries++;
1398789Sahrens 		zio->io_error = 0;
1399789Sahrens 		zio->io_flags &= ZIO_FLAG_VDEV_INHERIT;
1400789Sahrens 		/* XXPOLICY */
1401789Sahrens 		zio->io_flags &= ~ZIO_FLAG_FAILFAST;
1402789Sahrens 		zio->io_flags |= ZIO_FLAG_DONT_CACHE;
14031775Sbillm 		zio->io_stage = ZIO_STAGE_VDEV_IO_START - 1;
1404789Sahrens 
1405789Sahrens 		dprintf("retry #%d for %s to %s offset %llx\n",
1406789Sahrens 		    zio->io_retries, zio_type_name[zio->io_type],
1407789Sahrens 		    vdev_description(vd), zio->io_offset);
1408789Sahrens 
14091544Seschrock 		zio_next_stage_async(zio);
14101544Seschrock 		return;
14111544Seschrock 	}
1412789Sahrens 
14131775Sbillm 	if (zio->io_error != 0 && zio->io_error != ECKSUM &&
14141775Sbillm 	    !(zio->io_flags & ZIO_FLAG_SPECULATIVE) && vd) {
1415789Sahrens 		/*
14161544Seschrock 		 * Poor man's hotplug support.  Even if we're done retrying this
14171544Seschrock 		 * I/O, try to reopen the vdev to see if it's still attached.
14181544Seschrock 		 * To avoid excessive thrashing, we only try it once a minute.
14191544Seschrock 		 * This also has the effect of detecting when missing devices
14201544Seschrock 		 * have come back, by polling the device once a minute.
14211544Seschrock 		 *
14221544Seschrock 		 * We need to do this asynchronously because we can't grab
14231544Seschrock 		 * all the necessary locks way down here.
1424789Sahrens 		 */
14251544Seschrock 		if (gethrtime() - vd->vdev_last_try > 60ULL * NANOSEC) {
14261544Seschrock 			vd->vdev_last_try = gethrtime();
14271544Seschrock 			tvd->vdev_reopen_wanted = 1;
14281544Seschrock 			spa_async_request(vd->vdev_spa, SPA_ASYNC_REOPEN);
14291544Seschrock 		}
1430789Sahrens 	}
1431789Sahrens 
1432789Sahrens 	zio_next_stage(zio);
1433789Sahrens }
1434789Sahrens 
1435789Sahrens void
1436789Sahrens zio_vdev_io_reissue(zio_t *zio)
1437789Sahrens {
1438789Sahrens 	ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
1439789Sahrens 	ASSERT(zio->io_error == 0);
1440789Sahrens 
1441789Sahrens 	zio->io_stage--;
1442789Sahrens }
1443789Sahrens 
1444789Sahrens void
1445789Sahrens zio_vdev_io_redone(zio_t *zio)
1446789Sahrens {
1447789Sahrens 	ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_DONE);
1448789Sahrens 
1449789Sahrens 	zio->io_stage--;
1450789Sahrens }
1451789Sahrens 
1452789Sahrens void
1453789Sahrens zio_vdev_io_bypass(zio_t *zio)
1454789Sahrens {
1455789Sahrens 	ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
1456789Sahrens 	ASSERT(zio->io_error == 0);
1457789Sahrens 
1458789Sahrens 	zio->io_flags |= ZIO_FLAG_IO_BYPASS;
1459789Sahrens 	zio->io_stage = ZIO_STAGE_VDEV_IO_ASSESS - 1;
1460789Sahrens }
1461789Sahrens 
1462789Sahrens /*
1463789Sahrens  * ==========================================================================
1464789Sahrens  * Generate and verify checksums
1465789Sahrens  * ==========================================================================
1466789Sahrens  */
1467789Sahrens static void
1468789Sahrens zio_checksum_generate(zio_t *zio)
1469789Sahrens {
1470789Sahrens 	int checksum = zio->io_checksum;
1471789Sahrens 	blkptr_t *bp = zio->io_bp;
1472789Sahrens 
1473789Sahrens 	ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp));
1474789Sahrens 
1475789Sahrens 	BP_SET_CHECKSUM(bp, checksum);
1476789Sahrens 	BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
1477789Sahrens 
1478789Sahrens 	zio_checksum(checksum, &bp->blk_cksum, zio->io_data, zio->io_size);
1479789Sahrens 
1480789Sahrens 	zio_next_stage(zio);
1481789Sahrens }
1482789Sahrens 
1483789Sahrens static void
1484789Sahrens zio_gang_checksum_generate(zio_t *zio)
1485789Sahrens {
1486789Sahrens 	zio_cksum_t zc;
1487789Sahrens 	zio_gbh_phys_t *gbh = zio->io_data;
1488789Sahrens 
14891775Sbillm 	ASSERT(BP_IS_GANG(zio->io_bp));
1490789Sahrens 	ASSERT3U(zio->io_size, ==, SPA_GANGBLOCKSIZE);
1491789Sahrens 
1492789Sahrens 	zio_set_gang_verifier(zio, &gbh->zg_tail.zbt_cksum);
1493789Sahrens 
1494789Sahrens 	zio_checksum(ZIO_CHECKSUM_GANG_HEADER, &zc, zio->io_data, zio->io_size);
1495789Sahrens 
1496789Sahrens 	zio_next_stage(zio);
1497789Sahrens }
1498789Sahrens 
1499789Sahrens static void
1500789Sahrens zio_checksum_verify(zio_t *zio)
1501789Sahrens {
1502789Sahrens 	if (zio->io_bp != NULL) {
1503789Sahrens 		zio->io_error = zio_checksum_error(zio);
15041544Seschrock 		if (zio->io_error && !(zio->io_flags & ZIO_FLAG_SPECULATIVE))
15051544Seschrock 			zfs_ereport_post(FM_EREPORT_ZFS_CHECKSUM,
15061544Seschrock 			    zio->io_spa, zio->io_vd, zio, 0, 0);
1507789Sahrens 	}
1508789Sahrens 
1509789Sahrens 	zio_next_stage(zio);
1510789Sahrens }
1511789Sahrens 
1512789Sahrens /*
1513789Sahrens  * Called by RAID-Z to ensure we don't compute the checksum twice.
1514789Sahrens  */
1515789Sahrens void
1516789Sahrens zio_checksum_verified(zio_t *zio)
1517789Sahrens {
1518789Sahrens 	zio->io_pipeline &= ~(1U << ZIO_STAGE_CHECKSUM_VERIFY);
1519789Sahrens }
1520789Sahrens 
1521789Sahrens /*
1522789Sahrens  * Set the external verifier for a gang block based on stuff in the bp
1523789Sahrens  */
1524789Sahrens void
1525789Sahrens zio_set_gang_verifier(zio_t *zio, zio_cksum_t *zcp)
1526789Sahrens {
15271775Sbillm 	blkptr_t *bp = zio->io_bp;
15281775Sbillm 
15291775Sbillm 	zcp->zc_word[0] = DVA_GET_VDEV(BP_IDENTITY(bp));
15301775Sbillm 	zcp->zc_word[1] = DVA_GET_OFFSET(BP_IDENTITY(bp));
15311775Sbillm 	zcp->zc_word[2] = bp->blk_birth;
1532789Sahrens 	zcp->zc_word[3] = 0;
1533789Sahrens }
1534789Sahrens 
1535789Sahrens /*
1536789Sahrens  * ==========================================================================
1537789Sahrens  * Define the pipeline
1538789Sahrens  * ==========================================================================
1539789Sahrens  */
1540789Sahrens typedef void zio_pipe_stage_t(zio_t *zio);
1541789Sahrens 
1542789Sahrens static void
1543789Sahrens zio_badop(zio_t *zio)
1544789Sahrens {
1545789Sahrens 	panic("Invalid I/O pipeline stage %u for zio %p", zio->io_stage, zio);
1546789Sahrens }
1547789Sahrens 
1548789Sahrens zio_pipe_stage_t *zio_pipeline[ZIO_STAGE_DONE + 2] = {
1549789Sahrens 	zio_badop,
1550789Sahrens 	zio_wait_children_ready,
1551789Sahrens 	zio_write_compress,
1552789Sahrens 	zio_checksum_generate,
1553789Sahrens 	zio_gang_pipeline,
1554789Sahrens 	zio_get_gang_header,
1555789Sahrens 	zio_rewrite_gang_members,
1556789Sahrens 	zio_free_gang_members,
1557789Sahrens 	zio_claim_gang_members,
1558789Sahrens 	zio_dva_allocate,
1559789Sahrens 	zio_dva_free,
1560789Sahrens 	zio_dva_claim,
1561789Sahrens 	zio_gang_checksum_generate,
1562789Sahrens 	zio_ready,
1563789Sahrens 	zio_vdev_io_start,
1564789Sahrens 	zio_vdev_io_done,
1565789Sahrens 	zio_vdev_io_assess,
1566789Sahrens 	zio_wait_children_done,
1567789Sahrens 	zio_checksum_verify,
1568789Sahrens 	zio_read_gang_members,
1569789Sahrens 	zio_read_decompress,
1570789Sahrens 	zio_done,
1571789Sahrens 	zio_badop
1572789Sahrens };
1573789Sahrens 
1574789Sahrens /*
1575789Sahrens  * Move an I/O to the next stage of the pipeline and execute that stage.
1576789Sahrens  * There's no locking on io_stage because there's no legitimate way for
1577789Sahrens  * multiple threads to be attempting to process the same I/O.
1578789Sahrens  */
1579789Sahrens void
1580789Sahrens zio_next_stage(zio_t *zio)
1581789Sahrens {
1582789Sahrens 	uint32_t pipeline = zio->io_pipeline;
1583789Sahrens 
1584789Sahrens 	ASSERT(!MUTEX_HELD(&zio->io_lock));
1585789Sahrens 
1586789Sahrens 	if (zio->io_error) {
1587789Sahrens 		dprintf("zio %p vdev %s offset %llx stage %d error %d\n",
1588789Sahrens 		    zio, vdev_description(zio->io_vd),
1589789Sahrens 		    zio->io_offset, zio->io_stage, zio->io_error);
1590789Sahrens 		if (((1U << zio->io_stage) & ZIO_VDEV_IO_PIPELINE) == 0)
1591789Sahrens 			pipeline &= ZIO_ERROR_PIPELINE_MASK;
1592789Sahrens 	}
1593789Sahrens 
1594789Sahrens 	while (((1U << ++zio->io_stage) & pipeline) == 0)
1595789Sahrens 		continue;
1596789Sahrens 
1597789Sahrens 	ASSERT(zio->io_stage <= ZIO_STAGE_DONE);
1598789Sahrens 	ASSERT(zio->io_stalled == 0);
1599789Sahrens 
1600789Sahrens 	zio_pipeline[zio->io_stage](zio);
1601789Sahrens }
1602789Sahrens 
1603789Sahrens void
1604789Sahrens zio_next_stage_async(zio_t *zio)
1605789Sahrens {
1606789Sahrens 	taskq_t *tq;
1607789Sahrens 	uint32_t pipeline = zio->io_pipeline;
1608789Sahrens 
1609789Sahrens 	ASSERT(!MUTEX_HELD(&zio->io_lock));
1610789Sahrens 
1611789Sahrens 	if (zio->io_error) {
1612789Sahrens 		dprintf("zio %p vdev %s offset %llx stage %d error %d\n",
1613789Sahrens 		    zio, vdev_description(zio->io_vd),
1614789Sahrens 		    zio->io_offset, zio->io_stage, zio->io_error);
1615789Sahrens 		if (((1U << zio->io_stage) & ZIO_VDEV_IO_PIPELINE) == 0)
1616789Sahrens 			pipeline &= ZIO_ERROR_PIPELINE_MASK;
1617789Sahrens 	}
1618789Sahrens 
1619789Sahrens 	while (((1U << ++zio->io_stage) & pipeline) == 0)
1620789Sahrens 		continue;
1621789Sahrens 
1622789Sahrens 	ASSERT(zio->io_stage <= ZIO_STAGE_DONE);
1623789Sahrens 	ASSERT(zio->io_stalled == 0);
1624789Sahrens 
1625789Sahrens 	/*
1626789Sahrens 	 * For performance, we'll probably want two sets of task queues:
1627789Sahrens 	 * per-CPU issue taskqs and per-CPU completion taskqs.  The per-CPU
1628789Sahrens 	 * part is for read performance: since we have to make a pass over
1629789Sahrens 	 * the data to checksum it anyway, we want to do this on the same CPU
1630789Sahrens 	 * that issued the read, because (assuming CPU scheduling affinity)
1631789Sahrens 	 * that thread is probably still there.  Getting this optimization
1632789Sahrens 	 * right avoids performance-hostile cache-to-cache transfers.
1633789Sahrens 	 *
1634789Sahrens 	 * Note that having two sets of task queues is also necessary for
1635789Sahrens 	 * correctness: if all of the issue threads get bogged down waiting
1636789Sahrens 	 * for dependent reads (e.g. metaslab freelist) to complete, then
1637789Sahrens 	 * there won't be any threads available to service I/O completion
1638789Sahrens 	 * interrupts.
1639789Sahrens 	 */
1640789Sahrens 	if ((1U << zio->io_stage) & zio->io_async_stages) {
1641789Sahrens 		if (zio->io_stage < ZIO_STAGE_VDEV_IO_DONE)
1642789Sahrens 			tq = zio->io_spa->spa_zio_issue_taskq[zio->io_type];
1643789Sahrens 		else
1644789Sahrens 			tq = zio->io_spa->spa_zio_intr_taskq[zio->io_type];
1645789Sahrens 		(void) taskq_dispatch(tq,
1646789Sahrens 		    (task_func_t *)zio_pipeline[zio->io_stage], zio, TQ_SLEEP);
1647789Sahrens 	} else {
1648789Sahrens 		zio_pipeline[zio->io_stage](zio);
1649789Sahrens 	}
1650789Sahrens }
1651789Sahrens 
1652789Sahrens /*
1653789Sahrens  * Try to allocate an intent log block.  Return 0 on success, errno on failure.
1654789Sahrens  */
1655789Sahrens int
16561807Sbonwick zio_alloc_blk(spa_t *spa, uint64_t size, blkptr_t *bp, uint64_t txg)
1657789Sahrens {
1658789Sahrens 	int error;
1659789Sahrens 
16601544Seschrock 	spa_config_enter(spa, RW_READER, FTAG);
1661789Sahrens 
1662789Sahrens 	BP_ZERO(bp);
1663789Sahrens 
16641775Sbillm 	error = metaslab_alloc(spa, size, bp, 1, txg, NULL);
1665789Sahrens 
1666789Sahrens 	if (error == 0) {
1667789Sahrens 		BP_SET_LSIZE(bp, size);
1668789Sahrens 		BP_SET_PSIZE(bp, size);
1669789Sahrens 		BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF);
16701807Sbonwick 		BP_SET_CHECKSUM(bp, ZIO_CHECKSUM_ZILOG);
1671789Sahrens 		BP_SET_TYPE(bp, DMU_OT_INTENT_LOG);
1672789Sahrens 		BP_SET_LEVEL(bp, 0);
1673789Sahrens 		BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
1674789Sahrens 		bp->blk_birth = txg;
1675789Sahrens 	}
1676789Sahrens 
16771544Seschrock 	spa_config_exit(spa, FTAG);
1678789Sahrens 
1679789Sahrens 	return (error);
1680789Sahrens }
1681789Sahrens 
1682789Sahrens /*
1683789Sahrens  * Free an intent log block.  We know it can't be a gang block, so there's
1684789Sahrens  * nothing to do except metaslab_free() it.
1685789Sahrens  */
1686789Sahrens void
1687789Sahrens zio_free_blk(spa_t *spa, blkptr_t *bp, uint64_t txg)
1688789Sahrens {
16891775Sbillm 	ASSERT(!BP_IS_GANG(bp));
1690789Sahrens 
16911544Seschrock 	spa_config_enter(spa, RW_READER, FTAG);
1692789Sahrens 
16931807Sbonwick 	metaslab_free(spa, bp, txg, B_FALSE);
1694789Sahrens 
16951544Seschrock 	spa_config_exit(spa, FTAG);
1696789Sahrens }
1697