1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/zfs_context.h>
27 #include <sys/fm/fs/zfs.h>
28 #include <sys/spa.h>
29 #include <sys/txg.h>
30 #include <sys/spa_impl.h>
31 #include <sys/vdev_impl.h>
32 #include <sys/zio_impl.h>
33 #include <sys/zio_compress.h>
34 #include <sys/zio_checksum.h>
35 #include <sys/dmu_objset.h>
36 #include <sys/arc.h>
37 #include <sys/ddt.h>
38 
39 /*
40  * ==========================================================================
41  * I/O priority table
42  * ==========================================================================
43  */
44 uint8_t zio_priority_table[ZIO_PRIORITY_TABLE_SIZE] = {
45 	0,	/* ZIO_PRIORITY_NOW		*/
46 	0,	/* ZIO_PRIORITY_SYNC_READ	*/
47 	0,	/* ZIO_PRIORITY_SYNC_WRITE	*/
48 	0,	/* ZIO_PRIORITY_LOG_WRITE	*/
49 	1,	/* ZIO_PRIORITY_CACHE_FILL	*/
50 	1,	/* ZIO_PRIORITY_AGG		*/
51 	4,	/* ZIO_PRIORITY_FREE		*/
52 	4,	/* ZIO_PRIORITY_ASYNC_WRITE	*/
53 	6,	/* ZIO_PRIORITY_ASYNC_READ	*/
54 	10,	/* ZIO_PRIORITY_RESILVER	*/
55 	20,	/* ZIO_PRIORITY_SCRUB		*/
56 };
57 
58 /*
59  * ==========================================================================
60  * I/O type descriptions
61  * ==========================================================================
62  */
63 char *zio_type_name[ZIO_TYPES] = {
64 	"zio_null", "zio_read", "zio_write", "zio_free", "zio_claim",
65 	"zio_ioctl"
66 };
67 
68 /*
69  * ==========================================================================
70  * I/O kmem caches
71  * ==========================================================================
72  */
73 kmem_cache_t *zio_cache;
74 kmem_cache_t *zio_link_cache;
75 kmem_cache_t *zio_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
76 kmem_cache_t *zio_data_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
77 
78 #if defined(_KERNEL) && !defined(__NetBSD__)
79 extern vmem_t *zio_alloc_arena;
80 #endif
81 
82 /*
83  * An allocating zio is one that either currently has the DVA allocate
84  * stage set or will have it later in its lifetime.
85  */
86 #define	IO_IS_ALLOCATING(zio) ((zio)->io_orig_pipeline & ZIO_STAGE_DVA_ALLOCATE)
87 
88 boolean_t	zio_requeue_io_start_cut_in_line = B_TRUE;
89 
90 #ifdef ZFS_DEBUG
91 int zio_buf_debug_limit = 16384;
92 #else
93 int zio_buf_debug_limit = 0;
94 #endif
95 
96 void
zio_init(void)97 zio_init(void)
98 {
99 	size_t c;
100 	vmem_t *data_alloc_arena = NULL;
101 
102 #if defined(_KERNEL) && !defined(__NetBSD__)
103 	data_alloc_arena = zio_alloc_arena;
104 #endif
105 	zio_cache = kmem_cache_create("zio_cache",
106 	    sizeof (zio_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
107 	zio_link_cache = kmem_cache_create("zio_link_cache",
108 	    sizeof (zio_link_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
109 
110 #ifndef __NetBSD__
111 	/*
112 	 * For small buffers, we want a cache for each multiple of
113 	 * SPA_MINBLOCKSIZE.  For medium-size buffers, we want a cache
114 	 * for each quarter-power of 2.  For large buffers, we want
115 	 * a cache for each multiple of PAGESIZE.
116 	 */
117 	for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) {
118 		size_t size = (c + 1) << SPA_MINBLOCKSHIFT;
119 		size_t p2 = size;
120 		size_t align = 0;
121 
122 		while (p2 & (p2 - 1))
123 			p2 &= p2 - 1;
124 
125 		if (size <= 4 * SPA_MINBLOCKSIZE) {
126 			align = SPA_MINBLOCKSIZE;
127 		} else if (P2PHASE(size, PAGESIZE) == 0) {
128 			align = PAGESIZE;
129 		} else if (P2PHASE(size, p2 >> 2) == 0) {
130 			align = p2 >> 2;
131 		}
132 
133 		if (align != 0) {
134 			char name[36];
135 			(void) snprintf(name, sizeof(name), "zio_buf_%lu",
136 			    (ulong_t)size);
137 			zio_buf_cache[c] = kmem_cache_create(name, size,
138 			    align, NULL, NULL, NULL, NULL, NULL,
139 			    size > zio_buf_debug_limit ? KMC_NODEBUG : 0);
140 
141 			(void) snprintf(name, sizeof(name), "zio_data_buf_%lu",
142 			    (ulong_t)size);
143 			zio_data_buf_cache[c] = kmem_cache_create(name, size,
144 			    align, NULL, NULL, NULL, NULL, data_alloc_arena,
145 			    size > zio_buf_debug_limit ? KMC_NODEBUG : 0);
146 		}
147 	}
148 
149 	while (--c != 0) {
150 		ASSERT(zio_buf_cache[c] != NULL);
151 		if (zio_buf_cache[c - 1] == NULL)
152 			zio_buf_cache[c - 1] = zio_buf_cache[c];
153 
154 		ASSERT(zio_data_buf_cache[c] != NULL);
155 		if (zio_data_buf_cache[c - 1] == NULL)
156 			zio_data_buf_cache[c - 1] = zio_data_buf_cache[c];
157 	}
158 #endif /* __NetBSD__ */
159 	zio_inject_init();
160 }
161 
162 void
zio_fini(void)163 zio_fini(void)
164 {
165 	size_t c;
166 	kmem_cache_t *last_cache = NULL;
167 	kmem_cache_t *last_data_cache = NULL;
168 
169 #ifndef __NetBSD__
170 	for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) {
171 		if (zio_buf_cache[c] != last_cache) {
172 			last_cache = zio_buf_cache[c];
173 			kmem_cache_destroy(zio_buf_cache[c]);
174 		}
175 		zio_buf_cache[c] = NULL;
176 
177 		if (zio_data_buf_cache[c] != last_data_cache) {
178 			last_data_cache = zio_data_buf_cache[c];
179 			kmem_cache_destroy(zio_data_buf_cache[c]);
180 		}
181 		zio_data_buf_cache[c] = NULL;
182 	}
183 #endif /* __NetBSD__ */
184 
185 	kmem_cache_destroy(zio_link_cache);
186 	kmem_cache_destroy(zio_cache);
187 
188 	zio_inject_fini();
189 }
190 
191 /*
192  * ==========================================================================
193  * Allocate and free I/O buffers
194  * ==========================================================================
195  */
196 
197 /*
198  * Use zio_buf_alloc to allocate ZFS metadata.  This data will appear in a
199  * crashdump if the kernel panics, so use it judiciously.  Obviously, it's
200  * useful to inspect ZFS metadata, but if possible, we should avoid keeping
201  * excess / transient data in-core during a crashdump.
202  */
203 void *
zio_buf_alloc(size_t size)204 zio_buf_alloc(size_t size)
205 {
206 	size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
207 	ASSERT(c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
208 #ifdef __NetBSD__
209 	return (kmem_alloc(size, KM_SLEEP));
210 #else
211 	return (kmem_cache_alloc(zio_buf_cache[c], KM_PUSHPAGE));
212 #endif
213 }
214 
215 /*
216  * Use zio_data_buf_alloc to allocate data.  The data will not appear in a
217  * crashdump if the kernel panics.  This exists so that we will limit the amount
218  * of ZFS data that shows up in a kernel crashdump.  (Thus reducing the amount
219  * of kernel heap dumped to disk when the kernel panics)
220  */
221 void *
zio_data_buf_alloc(size_t size)222 zio_data_buf_alloc(size_t size)
223 {
224 	size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
225 
226 	ASSERT(c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
227 #ifdef __NetBSD__
228 	return (kmem_alloc(size, KM_SLEEP));
229 #else
230 	return (kmem_cache_alloc(zio_data_buf_cache[c], KM_PUSHPAGE));
231 #endif
232 }
233 
234 void
zio_buf_free(void * buf,size_t size)235 zio_buf_free(void *buf, size_t size)
236 {
237 	size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
238 
239 	ASSERT(c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
240 
241 #ifdef __NetBSD__
242 	kmem_free(buf, size);
243 #else
244 	kmem_cache_free(zio_buf_cache[c], buf);
245 #endif
246 }
247 
248 void
zio_data_buf_free(void * buf,size_t size)249 zio_data_buf_free(void *buf, size_t size)
250 {
251 	size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
252 
253 	ASSERT(c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
254 
255 #ifdef __NetBSD__
256 	kmem_free(buf, size);
257 #else
258 	kmem_cache_free(zio_data_buf_cache[c], buf);
259 #endif
260 }
261 
262 /*
263  * ==========================================================================
264  * Push and pop I/O transform buffers
265  * ==========================================================================
266  */
267 static void
zio_push_transform(zio_t * zio,void * data,uint64_t size,uint64_t bufsize,zio_transform_func_t * transform)268 zio_push_transform(zio_t *zio, void *data, uint64_t size, uint64_t bufsize,
269 	zio_transform_func_t *transform)
270 {
271 	zio_transform_t *zt = kmem_alloc(sizeof (zio_transform_t), KM_SLEEP);
272 
273 	zt->zt_orig_data = zio->io_data;
274 	zt->zt_orig_size = zio->io_size;
275 	zt->zt_bufsize = bufsize;
276 	zt->zt_transform = transform;
277 
278 	zt->zt_next = zio->io_transform_stack;
279 	zio->io_transform_stack = zt;
280 
281 	zio->io_data = data;
282 	zio->io_size = size;
283 }
284 
285 static void
zio_pop_transforms(zio_t * zio)286 zio_pop_transforms(zio_t *zio)
287 {
288 	zio_transform_t *zt;
289 
290 	while ((zt = zio->io_transform_stack) != NULL) {
291 		if (zt->zt_transform != NULL)
292 			zt->zt_transform(zio,
293 			    zt->zt_orig_data, zt->zt_orig_size);
294 
295 		if (zt->zt_bufsize != 0)
296 			zio_buf_free(zio->io_data, zt->zt_bufsize);
297 
298 		zio->io_data = zt->zt_orig_data;
299 		zio->io_size = zt->zt_orig_size;
300 		zio->io_transform_stack = zt->zt_next;
301 
302 		kmem_free(zt, sizeof (zio_transform_t));
303 	}
304 }
305 
306 /*
307  * ==========================================================================
308  * I/O transform callbacks for subblocks and decompression
309  * ==========================================================================
310  */
311 static void
zio_subblock(zio_t * zio,void * data,uint64_t size)312 zio_subblock(zio_t *zio, void *data, uint64_t size)
313 {
314 	ASSERT(zio->io_size > size);
315 
316 	if (zio->io_type == ZIO_TYPE_READ)
317 		bcopy(zio->io_data, data, size);
318 }
319 
320 static void
zio_decompress(zio_t * zio,void * data,uint64_t size)321 zio_decompress(zio_t *zio, void *data, uint64_t size)
322 {
323 	if (zio->io_error == 0 &&
324 	    zio_decompress_data(BP_GET_COMPRESS(zio->io_bp),
325 	    zio->io_data, data, zio->io_size, size) != 0)
326 		zio->io_error = EIO;
327 }
328 
329 /*
330  * ==========================================================================
331  * I/O parent/child relationships and pipeline interlocks
332  * ==========================================================================
333  */
334 /*
335  * NOTE - Callers to zio_walk_parents() and zio_walk_children must
336  *        continue calling these functions until they return NULL.
337  *        Otherwise, the next caller will pick up the list walk in
338  *        some indeterminate state.  (Otherwise every caller would
339  *        have to pass in a cookie to keep the state represented by
340  *        io_walk_link, which gets annoying.)
341  */
342 zio_t *
zio_walk_parents(zio_t * cio)343 zio_walk_parents(zio_t *cio)
344 {
345 	zio_link_t *zl = cio->io_walk_link;
346 	list_t *pl = &cio->io_parent_list;
347 
348 	zl = (zl == NULL) ? list_head(pl) : list_next(pl, zl);
349 	cio->io_walk_link = zl;
350 
351 	if (zl == NULL)
352 		return (NULL);
353 
354 	ASSERT(zl->zl_child == cio);
355 	return (zl->zl_parent);
356 }
357 
358 zio_t *
zio_walk_children(zio_t * pio)359 zio_walk_children(zio_t *pio)
360 {
361 	zio_link_t *zl = pio->io_walk_link;
362 	list_t *cl = &pio->io_child_list;
363 
364 	zl = (zl == NULL) ? list_head(cl) : list_next(cl, zl);
365 	pio->io_walk_link = zl;
366 
367 	if (zl == NULL)
368 		return (NULL);
369 
370 	ASSERT(zl->zl_parent == pio);
371 	return (zl->zl_child);
372 }
373 
374 zio_t *
zio_unique_parent(zio_t * cio)375 zio_unique_parent(zio_t *cio)
376 {
377 	zio_t *pio = zio_walk_parents(cio);
378 
379 	VERIFY(zio_walk_parents(cio) == NULL);
380 	return (pio);
381 }
382 
383 void
zio_add_child(zio_t * pio,zio_t * cio)384 zio_add_child(zio_t *pio, zio_t *cio)
385 {
386 	zio_link_t *zl = kmem_cache_alloc(zio_link_cache, KM_SLEEP);
387 
388 	/*
389 	 * Logical I/Os can have logical, gang, or vdev children.
390 	 * Gang I/Os can have gang or vdev children.
391 	 * Vdev I/Os can only have vdev children.
392 	 * The following ASSERT captures all of these constraints.
393 	 */
394 	ASSERT(cio->io_child_type <= pio->io_child_type);
395 
396 	zl->zl_parent = pio;
397 	zl->zl_child = cio;
398 
399 	mutex_enter(&cio->io_lock);
400 	mutex_enter(&pio->io_lock);
401 
402 	ASSERT(pio->io_state[ZIO_WAIT_DONE] == 0);
403 
404 	for (int w = 0; w < ZIO_WAIT_TYPES; w++)
405 		pio->io_children[cio->io_child_type][w] += !cio->io_state[w];
406 
407 	list_insert_head(&pio->io_child_list, zl);
408 	list_insert_head(&cio->io_parent_list, zl);
409 
410 	pio->io_child_count++;
411 	cio->io_parent_count++;
412 
413 	mutex_exit(&pio->io_lock);
414 	mutex_exit(&cio->io_lock);
415 }
416 
417 static void
zio_remove_child(zio_t * pio,zio_t * cio,zio_link_t * zl)418 zio_remove_child(zio_t *pio, zio_t *cio, zio_link_t *zl)
419 {
420 	ASSERT(zl->zl_parent == pio);
421 	ASSERT(zl->zl_child == cio);
422 
423 	mutex_enter(&cio->io_lock);
424 	mutex_enter(&pio->io_lock);
425 
426 	list_remove(&pio->io_child_list, zl);
427 	list_remove(&cio->io_parent_list, zl);
428 
429 	pio->io_child_count--;
430 	cio->io_parent_count--;
431 
432 	mutex_exit(&pio->io_lock);
433 	mutex_exit(&cio->io_lock);
434 
435 	kmem_cache_free(zio_link_cache, zl);
436 }
437 
438 static boolean_t
zio_wait_for_children(zio_t * zio,enum zio_child child,enum zio_wait_type wait)439 zio_wait_for_children(zio_t *zio, enum zio_child child, enum zio_wait_type wait)
440 {
441 	uint64_t *countp = &zio->io_children[child][wait];
442 	boolean_t waiting = B_FALSE;
443 
444 	mutex_enter(&zio->io_lock);
445 	ASSERT(zio->io_stall == NULL);
446 	if (*countp != 0) {
447 		zio->io_stage >>= 1;
448 		zio->io_stall = countp;
449 		waiting = B_TRUE;
450 	}
451 	mutex_exit(&zio->io_lock);
452 
453 	return (waiting);
454 }
455 
456 static void
zio_notify_parent(zio_t * pio,zio_t * zio,enum zio_wait_type wait)457 zio_notify_parent(zio_t *pio, zio_t *zio, enum zio_wait_type wait)
458 {
459 	uint64_t *countp = &pio->io_children[zio->io_child_type][wait];
460 	int *errorp = &pio->io_child_error[zio->io_child_type];
461 
462 	mutex_enter(&pio->io_lock);
463 	if (zio->io_error && !(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
464 		*errorp = zio_worst_error(*errorp, zio->io_error);
465 	pio->io_reexecute |= zio->io_reexecute;
466 	ASSERT3U(*countp, >, 0);
467 	if (--*countp == 0 && pio->io_stall == countp) {
468 		pio->io_stall = NULL;
469 		mutex_exit(&pio->io_lock);
470 		zio_execute(pio);
471 	} else {
472 		mutex_exit(&pio->io_lock);
473 	}
474 }
475 
476 static void
zio_inherit_child_errors(zio_t * zio,enum zio_child c)477 zio_inherit_child_errors(zio_t *zio, enum zio_child c)
478 {
479 	if (zio->io_child_error[c] != 0 && zio->io_error == 0)
480 		zio->io_error = zio->io_child_error[c];
481 }
482 
483 /*
484  * ==========================================================================
485  * Create the various types of I/O (read, write, free, etc)
486  * ==========================================================================
487  */
488 static zio_t *
zio_create(zio_t * pio,spa_t * spa,uint64_t txg,const blkptr_t * bp,void * data,uint64_t size,zio_done_func_t * done,void * private,zio_type_t type,int priority,enum zio_flag flags,vdev_t * vd,uint64_t offset,const zbookmark_t * zb,enum zio_stage stage,enum zio_stage pipeline)489 zio_create(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
490     void *data, uint64_t size, zio_done_func_t *done, void *private,
491     zio_type_t type, int priority, enum zio_flag flags,
492     vdev_t *vd, uint64_t offset, const zbookmark_t *zb,
493     enum zio_stage stage, enum zio_stage pipeline)
494 {
495 	zio_t *zio;
496 
497 	ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
498 	ASSERT(P2PHASE(size, SPA_MINBLOCKSIZE) == 0);
499 	ASSERT(P2PHASE(offset, SPA_MINBLOCKSIZE) == 0);
500 
501 	ASSERT(!vd || spa_config_held(spa, SCL_STATE_ALL, RW_READER));
502 	ASSERT(!bp || !(flags & ZIO_FLAG_CONFIG_WRITER));
503 	ASSERT(vd || stage == ZIO_STAGE_OPEN);
504 
505 	zio = kmem_cache_alloc(zio_cache, KM_SLEEP);
506 	bzero(zio, sizeof (zio_t));
507 
508 	mutex_init(&zio->io_lock, NULL, MUTEX_DEFAULT, NULL);
509 	cv_init(&zio->io_cv, NULL, CV_DEFAULT, NULL);
510 
511 	list_create(&zio->io_parent_list, sizeof (zio_link_t),
512 	    offsetof(zio_link_t, zl_parent_node));
513 	list_create(&zio->io_child_list, sizeof (zio_link_t),
514 	    offsetof(zio_link_t, zl_child_node));
515 
516 	if (vd != NULL)
517 		zio->io_child_type = ZIO_CHILD_VDEV;
518 	else if (flags & ZIO_FLAG_GANG_CHILD)
519 		zio->io_child_type = ZIO_CHILD_GANG;
520 	else if (flags & ZIO_FLAG_DDT_CHILD)
521 		zio->io_child_type = ZIO_CHILD_DDT;
522 	else
523 		zio->io_child_type = ZIO_CHILD_LOGICAL;
524 
525 	if (bp != NULL) {
526 		zio->io_bp = (blkptr_t *)bp;
527 		zio->io_bp_copy = *bp;
528 		zio->io_bp_orig = *bp;
529 		if (type != ZIO_TYPE_WRITE ||
530 		    zio->io_child_type == ZIO_CHILD_DDT)
531 			zio->io_bp = &zio->io_bp_copy;	/* so caller can free */
532 		if (zio->io_child_type == ZIO_CHILD_LOGICAL)
533 			zio->io_logical = zio;
534 		if (zio->io_child_type > ZIO_CHILD_GANG && BP_IS_GANG(bp))
535 			pipeline |= ZIO_GANG_STAGES;
536 	}
537 
538 	zio->io_spa = spa;
539 	zio->io_txg = txg;
540 	zio->io_done = done;
541 	zio->io_private = private;
542 	zio->io_type = type;
543 	zio->io_priority = priority;
544 	zio->io_vd = vd;
545 	zio->io_offset = offset;
546 	zio->io_orig_data = zio->io_data = data;
547 	zio->io_orig_size = zio->io_size = size;
548 	zio->io_orig_flags = zio->io_flags = flags;
549 	zio->io_orig_stage = zio->io_stage = stage;
550 	zio->io_orig_pipeline = zio->io_pipeline = pipeline;
551 
552 	zio->io_state[ZIO_WAIT_READY] = (stage >= ZIO_STAGE_READY);
553 	zio->io_state[ZIO_WAIT_DONE] = (stage >= ZIO_STAGE_DONE);
554 
555 	if (zb != NULL)
556 		zio->io_bookmark = *zb;
557 
558 	if (pio != NULL) {
559 		if (zio->io_logical == NULL)
560 			zio->io_logical = pio->io_logical;
561 		if (zio->io_child_type == ZIO_CHILD_GANG)
562 			zio->io_gang_leader = pio->io_gang_leader;
563 		zio_add_child(pio, zio);
564 	}
565 
566 	return (zio);
567 }
568 
569 static void
zio_destroy(zio_t * zio)570 zio_destroy(zio_t *zio)
571 {
572 	list_destroy(&zio->io_parent_list);
573 	list_destroy(&zio->io_child_list);
574 	mutex_destroy(&zio->io_lock);
575 	cv_destroy(&zio->io_cv);
576 	kmem_cache_free(zio_cache, zio);
577 }
578 
579 zio_t *
zio_null(zio_t * pio,spa_t * spa,vdev_t * vd,zio_done_func_t * done,void * private,enum zio_flag flags)580 zio_null(zio_t *pio, spa_t *spa, vdev_t *vd, zio_done_func_t *done,
581     void *private, enum zio_flag flags)
582 {
583 	zio_t *zio;
584 
585 	zio = zio_create(pio, spa, 0, NULL, NULL, 0, done, private,
586 	    ZIO_TYPE_NULL, ZIO_PRIORITY_NOW, flags, vd, 0, NULL,
587 	    ZIO_STAGE_OPEN, ZIO_INTERLOCK_PIPELINE);
588 
589 	return (zio);
590 }
591 
592 zio_t *
zio_root(spa_t * spa,zio_done_func_t * done,void * private,enum zio_flag flags)593 zio_root(spa_t *spa, zio_done_func_t *done, void *private, enum zio_flag flags)
594 {
595 	return (zio_null(NULL, spa, NULL, done, private, flags));
596 }
597 
598 zio_t *
zio_read(zio_t * pio,spa_t * spa,const blkptr_t * bp,void * data,uint64_t size,zio_done_func_t * done,void * private,int priority,enum zio_flag flags,const zbookmark_t * zb)599 zio_read(zio_t *pio, spa_t *spa, const blkptr_t *bp,
600     void *data, uint64_t size, zio_done_func_t *done, void *private,
601     int priority, enum zio_flag flags, const zbookmark_t *zb)
602 {
603 	zio_t *zio;
604 
605 	zio = zio_create(pio, spa, BP_PHYSICAL_BIRTH(bp), bp,
606 	    data, size, done, private,
607 	    ZIO_TYPE_READ, priority, flags, NULL, 0, zb,
608 	    ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ?
609 	    ZIO_DDT_CHILD_READ_PIPELINE : ZIO_READ_PIPELINE);
610 
611 	return (zio);
612 }
613 
614 zio_t *
zio_write(zio_t * pio,spa_t * spa,uint64_t txg,blkptr_t * bp,void * data,uint64_t size,const zio_prop_t * zp,zio_done_func_t * ready,zio_done_func_t * done,void * private,int priority,enum zio_flag flags,const zbookmark_t * zb)615 zio_write(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
616     void *data, uint64_t size, const zio_prop_t *zp,
617     zio_done_func_t *ready, zio_done_func_t *done, void *private,
618     int priority, enum zio_flag flags, const zbookmark_t *zb)
619 {
620 	zio_t *zio;
621 
622 	ASSERT(zp->zp_checksum >= ZIO_CHECKSUM_OFF &&
623 	    zp->zp_checksum < ZIO_CHECKSUM_FUNCTIONS &&
624 	    zp->zp_compress >= ZIO_COMPRESS_OFF &&
625 	    zp->zp_compress < ZIO_COMPRESS_FUNCTIONS &&
626 	    zp->zp_type < DMU_OT_NUMTYPES &&
627 	    zp->zp_level < 32 &&
628 	    zp->zp_copies > 0 &&
629 	    zp->zp_copies <= spa_max_replication(spa) &&
630 	    zp->zp_dedup <= 1 &&
631 	    zp->zp_dedup_verify <= 1);
632 
633 	zio = zio_create(pio, spa, txg, bp, data, size, done, private,
634 	    ZIO_TYPE_WRITE, priority, flags, NULL, 0, zb,
635 	    ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ?
636 	    ZIO_DDT_CHILD_WRITE_PIPELINE : ZIO_WRITE_PIPELINE);
637 
638 	zio->io_ready = ready;
639 	zio->io_prop = *zp;
640 
641 	return (zio);
642 }
643 
644 zio_t *
zio_rewrite(zio_t * pio,spa_t * spa,uint64_t txg,blkptr_t * bp,void * data,uint64_t size,zio_done_func_t * done,void * private,int priority,enum zio_flag flags,zbookmark_t * zb)645 zio_rewrite(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, void *data,
646     uint64_t size, zio_done_func_t *done, void *private, int priority,
647     enum zio_flag flags, zbookmark_t *zb)
648 {
649 	zio_t *zio;
650 
651 	zio = zio_create(pio, spa, txg, bp, data, size, done, private,
652 	    ZIO_TYPE_WRITE, priority, flags, NULL, 0, zb,
653 	    ZIO_STAGE_OPEN, ZIO_REWRITE_PIPELINE);
654 
655 	return (zio);
656 }
657 
658 void
zio_write_override(zio_t * zio,blkptr_t * bp,int copies)659 zio_write_override(zio_t *zio, blkptr_t *bp, int copies)
660 {
661 	ASSERT(zio->io_type == ZIO_TYPE_WRITE);
662 	ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
663 	ASSERT(zio->io_stage == ZIO_STAGE_OPEN);
664 	ASSERT(zio->io_txg == spa_syncing_txg(zio->io_spa));
665 
666 	zio->io_prop.zp_copies = copies;
667 	zio->io_bp_override = bp;
668 }
669 
670 void
zio_free(spa_t * spa,uint64_t txg,const blkptr_t * bp)671 zio_free(spa_t *spa, uint64_t txg, const blkptr_t *bp)
672 {
673 	bplist_enqueue_deferred(&spa->spa_free_bplist[txg & TXG_MASK], bp);
674 }
675 
676 zio_t *
zio_free_sync(zio_t * pio,spa_t * spa,uint64_t txg,const blkptr_t * bp,enum zio_flag flags)677 zio_free_sync(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
678     enum zio_flag flags)
679 {
680 	zio_t *zio;
681 
682 	ASSERT(!BP_IS_HOLE(bp));
683 	ASSERT(spa_syncing_txg(spa) == txg);
684 	ASSERT(spa_sync_pass(spa) <= SYNC_PASS_DEFERRED_FREE);
685 
686 	zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp),
687 	    NULL, NULL, ZIO_TYPE_FREE, ZIO_PRIORITY_FREE, flags,
688 	    NULL, 0, NULL, ZIO_STAGE_OPEN, ZIO_FREE_PIPELINE);
689 
690 	return (zio);
691 }
692 
693 zio_t *
zio_claim(zio_t * pio,spa_t * spa,uint64_t txg,const blkptr_t * bp,zio_done_func_t * done,void * private,enum zio_flag flags)694 zio_claim(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
695     zio_done_func_t *done, void *private, enum zio_flag flags)
696 {
697 	zio_t *zio;
698 
699 	/*
700 	 * A claim is an allocation of a specific block.  Claims are needed
701 	 * to support immediate writes in the intent log.  The issue is that
702 	 * immediate writes contain committed data, but in a txg that was
703 	 * *not* committed.  Upon opening the pool after an unclean shutdown,
704 	 * the intent log claims all blocks that contain immediate write data
705 	 * so that the SPA knows they're in use.
706 	 *
707 	 * All claims *must* be resolved in the first txg -- before the SPA
708 	 * starts allocating blocks -- so that nothing is allocated twice.
709 	 * If txg == 0 we just verify that the block is claimable.
710 	 */
711 	ASSERT3U(spa->spa_uberblock.ub_rootbp.blk_birth, <, spa_first_txg(spa));
712 	ASSERT(txg == spa_first_txg(spa) || txg == 0);
713 	ASSERT(!BP_GET_DEDUP(bp) || !spa_writeable(spa));	/* zdb(1M) */
714 
715 	zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp),
716 	    done, private, ZIO_TYPE_CLAIM, ZIO_PRIORITY_NOW, flags,
717 	    NULL, 0, NULL, ZIO_STAGE_OPEN, ZIO_CLAIM_PIPELINE);
718 
719 	return (zio);
720 }
721 
722 zio_t *
zio_ioctl(zio_t * pio,spa_t * spa,vdev_t * vd,int cmd,zio_done_func_t * done,void * private,int priority,enum zio_flag flags)723 zio_ioctl(zio_t *pio, spa_t *spa, vdev_t *vd, int cmd,
724     zio_done_func_t *done, void *private, int priority, enum zio_flag flags)
725 {
726 	zio_t *zio;
727 	int c;
728 
729 	if (vd->vdev_children == 0) {
730 		zio = zio_create(pio, spa, 0, NULL, NULL, 0, done, private,
731 		    ZIO_TYPE_IOCTL, priority, flags, vd, 0, NULL,
732 		    ZIO_STAGE_OPEN, ZIO_IOCTL_PIPELINE);
733 
734 		zio->io_cmd = cmd;
735 	} else {
736 		zio = zio_null(pio, spa, NULL, NULL, NULL, flags);
737 
738 		for (c = 0; c < vd->vdev_children; c++)
739 			zio_nowait(zio_ioctl(zio, spa, vd->vdev_child[c], cmd,
740 			    done, private, priority, flags));
741 	}
742 
743 	return (zio);
744 }
745 
746 zio_t *
zio_read_phys(zio_t * pio,vdev_t * vd,uint64_t offset,uint64_t size,void * data,int checksum,zio_done_func_t * done,void * private,int priority,enum zio_flag flags,boolean_t labels)747 zio_read_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
748     void *data, int checksum, zio_done_func_t *done, void *private,
749     int priority, enum zio_flag flags, boolean_t labels)
750 {
751 	zio_t *zio;
752 
753 	ASSERT(vd->vdev_children == 0);
754 	ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE ||
755 	    offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
756 	ASSERT3U(offset + size, <=, vd->vdev_psize);
757 
758 	zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, done, private,
759 	    ZIO_TYPE_READ, priority, flags, vd, offset, NULL,
760 	    ZIO_STAGE_OPEN, ZIO_READ_PHYS_PIPELINE);
761 
762 	zio->io_prop.zp_checksum = checksum;
763 
764 	return (zio);
765 }
766 
767 zio_t *
zio_write_phys(zio_t * pio,vdev_t * vd,uint64_t offset,uint64_t size,void * data,int checksum,zio_done_func_t * done,void * private,int priority,enum zio_flag flags,boolean_t labels)768 zio_write_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
769     void *data, int checksum, zio_done_func_t *done, void *private,
770     int priority, enum zio_flag flags, boolean_t labels)
771 {
772 	zio_t *zio;
773 
774 	ASSERT(vd->vdev_children == 0);
775 	ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE ||
776 	    offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
777 	ASSERT3U(offset + size, <=, vd->vdev_psize);
778 
779 	zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, done, private,
780 	    ZIO_TYPE_WRITE, priority, flags, vd, offset, NULL,
781 	    ZIO_STAGE_OPEN, ZIO_WRITE_PHYS_PIPELINE);
782 
783 	zio->io_prop.zp_checksum = checksum;
784 
785 	if (zio_checksum_table[checksum].ci_eck) {
786 		/*
787 		 * zec checksums are necessarily destructive -- they modify
788 		 * the end of the write buffer to hold the verifier/checksum.
789 		 * Therefore, we must make a local copy in case the data is
790 		 * being written to multiple places in parallel.
791 		 */
792 		void *wbuf = zio_buf_alloc(size);
793 		bcopy(data, wbuf, size);
794 		zio_push_transform(zio, wbuf, size, size, NULL);
795 	}
796 
797 	return (zio);
798 }
799 
800 /*
801  * Create a child I/O to do some work for us.
802  */
803 zio_t *
zio_vdev_child_io(zio_t * pio,blkptr_t * bp,vdev_t * vd,uint64_t offset,void * data,uint64_t size,int type,int priority,enum zio_flag flags,zio_done_func_t * done,void * private)804 zio_vdev_child_io(zio_t *pio, blkptr_t *bp, vdev_t *vd, uint64_t offset,
805 	void *data, uint64_t size, int type, int priority, enum zio_flag flags,
806 	zio_done_func_t *done, void *private)
807 {
808 	enum zio_stage pipeline = ZIO_VDEV_CHILD_PIPELINE;
809 	zio_t *zio;
810 
811 	ASSERT(vd->vdev_parent ==
812 	    (pio->io_vd ? pio->io_vd : pio->io_spa->spa_root_vdev));
813 
814 	if (type == ZIO_TYPE_READ && bp != NULL) {
815 		/*
816 		 * If we have the bp, then the child should perform the
817 		 * checksum and the parent need not.  This pushes error
818 		 * detection as close to the leaves as possible and
819 		 * eliminates redundant checksums in the interior nodes.
820 		 */
821 		pipeline |= ZIO_STAGE_CHECKSUM_VERIFY;
822 		pio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY;
823 	}
824 
825 	if (vd->vdev_children == 0)
826 		offset += VDEV_LABEL_START_SIZE;
827 
828 	flags |= ZIO_VDEV_CHILD_FLAGS(pio) | ZIO_FLAG_DONT_PROPAGATE;
829 
830 	/*
831 	 * If we've decided to do a repair, the write is not speculative --
832 	 * even if the original read was.
833 	 */
834 	if (flags & ZIO_FLAG_IO_REPAIR)
835 		flags &= ~ZIO_FLAG_SPECULATIVE;
836 
837 	zio = zio_create(pio, pio->io_spa, pio->io_txg, bp, data, size,
838 	    done, private, type, priority, flags, vd, offset, &pio->io_bookmark,
839 	    ZIO_STAGE_VDEV_IO_START >> 1, pipeline);
840 
841 	return (zio);
842 }
843 
844 zio_t *
zio_vdev_delegated_io(vdev_t * vd,uint64_t offset,void * data,uint64_t size,int type,int priority,enum zio_flag flags,zio_done_func_t * done,void * private)845 zio_vdev_delegated_io(vdev_t *vd, uint64_t offset, void *data, uint64_t size,
846 	int type, int priority, enum zio_flag flags,
847 	zio_done_func_t *done, void *private)
848 {
849 	zio_t *zio;
850 
851 	ASSERT(vd->vdev_ops->vdev_op_leaf);
852 
853 	zio = zio_create(NULL, vd->vdev_spa, 0, NULL,
854 	    data, size, done, private, type, priority,
855 	    flags | ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY,
856 	    vd, offset, NULL,
857 	    ZIO_STAGE_VDEV_IO_START >> 1, ZIO_VDEV_CHILD_PIPELINE);
858 
859 	return (zio);
860 }
861 
862 void
zio_flush(zio_t * zio,vdev_t * vd)863 zio_flush(zio_t *zio, vdev_t *vd)
864 {
865 	zio_nowait(zio_ioctl(zio, zio->io_spa, vd, DKIOCFLUSHWRITECACHE,
866 	    NULL, NULL, ZIO_PRIORITY_NOW,
867 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY));
868 }
869 
870 void
zio_shrink(zio_t * zio,uint64_t size)871 zio_shrink(zio_t *zio, uint64_t size)
872 {
873 	ASSERT(zio->io_executor == NULL);
874 	ASSERT(zio->io_orig_size == zio->io_size);
875 	ASSERT(size <= zio->io_size);
876 
877 	/*
878 	 * We don't shrink for raidz because of problems with the
879 	 * reconstruction when reading back less than the block size.
880 	 * Note, BP_IS_RAIDZ() assumes no compression.
881 	 */
882 	ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF);
883 	if (!BP_IS_RAIDZ(zio->io_bp))
884 		zio->io_orig_size = zio->io_size = size;
885 }
886 
887 /*
888  * ==========================================================================
889  * Prepare to read and write logical blocks
890  * ==========================================================================
891  */
892 
893 static int
zio_read_bp_init(zio_t * zio)894 zio_read_bp_init(zio_t *zio)
895 {
896 	blkptr_t *bp = zio->io_bp;
897 
898 	if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF &&
899 	    zio->io_child_type == ZIO_CHILD_LOGICAL &&
900 	    !(zio->io_flags & ZIO_FLAG_RAW)) {
901 		uint64_t psize = BP_GET_PSIZE(bp);
902 		void *cbuf = zio_buf_alloc(psize);
903 
904 		zio_push_transform(zio, cbuf, psize, psize, zio_decompress);
905 	}
906 
907 	if (!dmu_ot[BP_GET_TYPE(bp)].ot_metadata && BP_GET_LEVEL(bp) == 0)
908 		zio->io_flags |= ZIO_FLAG_DONT_CACHE;
909 
910 	if (BP_GET_TYPE(bp) == DMU_OT_DDT_ZAP)
911 		zio->io_flags |= ZIO_FLAG_DONT_CACHE;
912 
913 	if (BP_GET_DEDUP(bp) && zio->io_child_type == ZIO_CHILD_LOGICAL)
914 		zio->io_pipeline = ZIO_DDT_READ_PIPELINE;
915 
916 	return (ZIO_PIPELINE_CONTINUE);
917 }
918 
919 static int
zio_write_bp_init(zio_t * zio)920 zio_write_bp_init(zio_t *zio)
921 {
922 	spa_t *spa = zio->io_spa;
923 	zio_prop_t *zp = &zio->io_prop;
924 	enum zio_compress compress = zp->zp_compress;
925 	blkptr_t *bp = zio->io_bp;
926 	uint64_t lsize = zio->io_size;
927 	uint64_t psize = lsize;
928 	int pass = 1;
929 
930 	/*
931 	 * If our children haven't all reached the ready stage,
932 	 * wait for them and then repeat this pipeline stage.
933 	 */
934 	if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_READY) ||
935 	    zio_wait_for_children(zio, ZIO_CHILD_LOGICAL, ZIO_WAIT_READY))
936 		return (ZIO_PIPELINE_STOP);
937 
938 	if (!IO_IS_ALLOCATING(zio))
939 		return (ZIO_PIPELINE_CONTINUE);
940 
941 	ASSERT(zio->io_child_type != ZIO_CHILD_DDT);
942 
943 	if (zio->io_bp_override) {
944 		ASSERT(bp->blk_birth != zio->io_txg);
945 		ASSERT(BP_GET_DEDUP(zio->io_bp_override) == 0);
946 
947 		*bp = *zio->io_bp_override;
948 		zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
949 
950 		if (BP_IS_HOLE(bp) || !zp->zp_dedup)
951 			return (ZIO_PIPELINE_CONTINUE);
952 
953 		ASSERT(zio_checksum_table[zp->zp_checksum].ci_dedup ||
954 		    zp->zp_dedup_verify);
955 
956 		if (BP_GET_CHECKSUM(bp) == zp->zp_checksum) {
957 			BP_SET_DEDUP(bp, 1);
958 			zio->io_pipeline |= ZIO_STAGE_DDT_WRITE;
959 			return (ZIO_PIPELINE_CONTINUE);
960 		}
961 		zio->io_bp_override = NULL;
962 		BP_ZERO(bp);
963 	}
964 
965 	if (bp->blk_birth == zio->io_txg) {
966 		/*
967 		 * We're rewriting an existing block, which means we're
968 		 * working on behalf of spa_sync().  For spa_sync() to
969 		 * converge, it must eventually be the case that we don't
970 		 * have to allocate new blocks.  But compression changes
971 		 * the blocksize, which forces a reallocate, and makes
972 		 * convergence take longer.  Therefore, after the first
973 		 * few passes, stop compressing to ensure convergence.
974 		 */
975 		pass = spa_sync_pass(spa);
976 
977 		ASSERT(zio->io_txg == spa_syncing_txg(spa));
978 		ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
979 		ASSERT(!BP_GET_DEDUP(bp));
980 
981 		if (pass > SYNC_PASS_DONT_COMPRESS)
982 			compress = ZIO_COMPRESS_OFF;
983 
984 		/* Make sure someone doesn't change their mind on overwrites */
985 		ASSERT(MIN(zp->zp_copies + BP_IS_GANG(bp),
986 		    spa_max_replication(spa)) == BP_GET_NDVAS(bp));
987 	}
988 
989 	if (compress != ZIO_COMPRESS_OFF) {
990 		void *cbuf = zio_buf_alloc(lsize);
991 		psize = zio_compress_data(compress, zio->io_data, cbuf, lsize);
992 		if (psize == 0 || psize == lsize) {
993 			compress = ZIO_COMPRESS_OFF;
994 			zio_buf_free(cbuf, lsize);
995 		} else {
996 			ASSERT(psize < lsize);
997 			zio_push_transform(zio, cbuf, psize, lsize, NULL);
998 		}
999 	}
1000 
1001 	/*
1002 	 * The final pass of spa_sync() must be all rewrites, but the first
1003 	 * few passes offer a trade-off: allocating blocks defers convergence,
1004 	 * but newly allocated blocks are sequential, so they can be written
1005 	 * to disk faster.  Therefore, we allow the first few passes of
1006 	 * spa_sync() to allocate new blocks, but force rewrites after that.
1007 	 * There should only be a handful of blocks after pass 1 in any case.
1008 	 */
1009 	if (bp->blk_birth == zio->io_txg && BP_GET_PSIZE(bp) == psize &&
1010 	    pass > SYNC_PASS_REWRITE) {
1011 		ASSERT(psize != 0);
1012 		enum zio_stage gang_stages = zio->io_pipeline & ZIO_GANG_STAGES;
1013 		zio->io_pipeline = ZIO_REWRITE_PIPELINE | gang_stages;
1014 		zio->io_flags |= ZIO_FLAG_IO_REWRITE;
1015 	} else {
1016 		BP_ZERO(bp);
1017 		zio->io_pipeline = ZIO_WRITE_PIPELINE;
1018 	}
1019 
1020 	if (psize == 0) {
1021 		zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1022 	} else {
1023 		ASSERT(zp->zp_checksum != ZIO_CHECKSUM_GANG_HEADER);
1024 		BP_SET_LSIZE(bp, lsize);
1025 		BP_SET_PSIZE(bp, psize);
1026 		BP_SET_COMPRESS(bp, compress);
1027 		BP_SET_CHECKSUM(bp, zp->zp_checksum);
1028 		BP_SET_TYPE(bp, zp->zp_type);
1029 		BP_SET_LEVEL(bp, zp->zp_level);
1030 		BP_SET_DEDUP(bp, zp->zp_dedup);
1031 		BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
1032 		if (zp->zp_dedup) {
1033 			ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1034 			ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
1035 			zio->io_pipeline = ZIO_DDT_WRITE_PIPELINE;
1036 		}
1037 	}
1038 
1039 	return (ZIO_PIPELINE_CONTINUE);
1040 }
1041 
1042 static int
zio_free_bp_init(zio_t * zio)1043 zio_free_bp_init(zio_t *zio)
1044 {
1045 	blkptr_t *bp = zio->io_bp;
1046 
1047 	if (zio->io_child_type == ZIO_CHILD_LOGICAL) {
1048 		if (BP_GET_DEDUP(bp))
1049 			zio->io_pipeline = ZIO_DDT_FREE_PIPELINE;
1050 		else
1051 			arc_free(zio->io_spa, bp);
1052 	}
1053 
1054 	return (ZIO_PIPELINE_CONTINUE);
1055 }
1056 
1057 /*
1058  * ==========================================================================
1059  * Execute the I/O pipeline
1060  * ==========================================================================
1061  */
1062 
1063 static void
zio_taskq_dispatch(zio_t * zio,enum zio_taskq_type q,boolean_t cutinline)1064 zio_taskq_dispatch(zio_t *zio, enum zio_taskq_type q, boolean_t cutinline)
1065 {
1066 	spa_t *spa = zio->io_spa;
1067 	zio_type_t t = zio->io_type;
1068 	int flags = TQ_SLEEP | (cutinline ? TQ_FRONT : 0);
1069 
1070 	/*
1071 	 * If we're a config writer or a probe, the normal issue and
1072 	 * interrupt threads may all be blocked waiting for the config lock.
1073 	 * In this case, select the otherwise-unused taskq for ZIO_TYPE_NULL.
1074 	 */
1075 	if (zio->io_flags & (ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_PROBE))
1076 		t = ZIO_TYPE_NULL;
1077 
1078 	/*
1079 	 * A similar issue exists for the L2ARC write thread until L2ARC 2.0.
1080 	 */
1081 	if (t == ZIO_TYPE_WRITE && zio->io_vd && zio->io_vd->vdev_aux)
1082 		t = ZIO_TYPE_NULL;
1083 
1084 	/*
1085 	 * If this is a high priority I/O, then use the high priority taskq.
1086 	 */
1087 	if (zio->io_priority == ZIO_PRIORITY_NOW &&
1088 	    spa->spa_zio_taskq[t][q + 1] != NULL)
1089 		q++;
1090 
1091 	ASSERT3U(q, <, ZIO_TASKQ_TYPES);
1092 	(void) taskq_dispatch(spa->spa_zio_taskq[t][q],
1093 	    (task_func_t *)zio_execute, zio, flags);
1094 }
1095 
1096 static boolean_t
zio_taskq_member(zio_t * zio,enum zio_taskq_type q)1097 zio_taskq_member(zio_t *zio, enum zio_taskq_type q)
1098 {
1099 	kthread_t *executor = zio->io_executor;
1100 	spa_t *spa = zio->io_spa;
1101 
1102 	for (zio_type_t t = 0; t < ZIO_TYPES; t++)
1103 		if (taskq_member(spa->spa_zio_taskq[t][q], executor))
1104 			return (B_TRUE);
1105 
1106 	return (B_FALSE);
1107 }
1108 
1109 static int
zio_issue_async(zio_t * zio)1110 zio_issue_async(zio_t *zio)
1111 {
1112 	zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE);
1113 
1114 	return (ZIO_PIPELINE_STOP);
1115 }
1116 
1117 void
zio_interrupt(zio_t * zio)1118 zio_interrupt(zio_t *zio)
1119 {
1120 	zio_taskq_dispatch(zio, ZIO_TASKQ_INTERRUPT, B_FALSE);
1121 }
1122 
1123 /*
1124  * Execute the I/O pipeline until one of the following occurs:
1125  * (1) the I/O completes; (2) the pipeline stalls waiting for
1126  * dependent child I/Os; (3) the I/O issues, so we're waiting
1127  * for an I/O completion interrupt; (4) the I/O is delegated by
1128  * vdev-level caching or aggregation; (5) the I/O is deferred
1129  * due to vdev-level queueing; (6) the I/O is handed off to
1130  * another thread.  In all cases, the pipeline stops whenever
1131  * there's no CPU work; it never burns a thread in cv_wait().
1132  *
1133  * There's no locking on io_stage because there's no legitimate way
1134  * for multiple threads to be attempting to process the same I/O.
1135  */
1136 static zio_pipe_stage_t *zio_pipeline[];
1137 
1138 void
zio_execute(zio_t * zio)1139 zio_execute(zio_t *zio)
1140 {
1141 	zio->io_executor = curthread;
1142 
1143 	while (zio->io_stage < ZIO_STAGE_DONE) {
1144 		enum zio_stage pipeline = zio->io_pipeline;
1145 		enum zio_stage stage = zio->io_stage;
1146 		int rv;
1147 
1148 		ASSERT(!MUTEX_HELD(&zio->io_lock));
1149 		ASSERT(ISP2(stage));
1150 		ASSERT(zio->io_stall == NULL);
1151 
1152 		do {
1153 			stage <<= 1;
1154 		} while ((stage & pipeline) == 0);
1155 
1156 		ASSERT(stage <= ZIO_STAGE_DONE);
1157 
1158 		/*
1159 		 * If we are in interrupt context and this pipeline stage
1160 		 * will grab a config lock that is held across I/O,
1161 		 * or may wait for an I/O that needs an interrupt thread
1162 		 * to complete, issue async to avoid deadlock.
1163 		 *
1164 		 * For VDEV_IO_START, we cut in line so that the io will
1165 		 * be sent to disk promptly.
1166 		 */
1167 		if ((stage & ZIO_BLOCKING_STAGES) && zio->io_vd == NULL &&
1168 		    zio_taskq_member(zio, ZIO_TASKQ_INTERRUPT)) {
1169 			boolean_t cut = (stage == ZIO_STAGE_VDEV_IO_START) ?
1170 			    zio_requeue_io_start_cut_in_line : B_FALSE;
1171 			zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, cut);
1172 			return;
1173 		}
1174 
1175 		zio->io_stage = stage;
1176 		rv = zio_pipeline[highbit(stage) - 1](zio);
1177 
1178 		if (rv == ZIO_PIPELINE_STOP)
1179 			return;
1180 
1181 		ASSERT(rv == ZIO_PIPELINE_CONTINUE);
1182 	}
1183 }
1184 
1185 /*
1186  * ==========================================================================
1187  * Initiate I/O, either sync or async
1188  * ==========================================================================
1189  */
1190 int
zio_wait(zio_t * zio)1191 zio_wait(zio_t *zio)
1192 {
1193 	int error;
1194 
1195 	ASSERT(zio->io_stage == ZIO_STAGE_OPEN);
1196 	ASSERT(zio->io_executor == NULL);
1197 
1198 	zio->io_waiter = curthread;
1199 
1200 	zio_execute(zio);
1201 
1202 	mutex_enter(&zio->io_lock);
1203 	while (zio->io_executor != NULL)
1204 		cv_wait(&zio->io_cv, &zio->io_lock);
1205 	mutex_exit(&zio->io_lock);
1206 
1207 	error = zio->io_error;
1208 	zio_destroy(zio);
1209 
1210 	return (error);
1211 }
1212 
1213 void
zio_nowait(zio_t * zio)1214 zio_nowait(zio_t *zio)
1215 {
1216 	ASSERT(zio->io_executor == NULL);
1217 
1218 	if (zio->io_child_type == ZIO_CHILD_LOGICAL &&
1219 	    zio_unique_parent(zio) == NULL) {
1220 		/*
1221 		 * This is a logical async I/O with no parent to wait for it.
1222 		 * We add it to the spa_async_root_zio "Godfather" I/O which
1223 		 * will ensure they complete prior to unloading the pool.
1224 		 */
1225 		spa_t *spa = zio->io_spa;
1226 
1227 		zio_add_child(spa->spa_async_zio_root, zio);
1228 	}
1229 
1230 	zio_execute(zio);
1231 }
1232 
1233 /*
1234  * ==========================================================================
1235  * Reexecute or suspend/resume failed I/O
1236  * ==========================================================================
1237  */
1238 
1239 static void
zio_reexecute(zio_t * pio)1240 zio_reexecute(zio_t *pio)
1241 {
1242 	zio_t *cio, *cio_next;
1243 
1244 	ASSERT(pio->io_child_type == ZIO_CHILD_LOGICAL);
1245 	ASSERT(pio->io_orig_stage == ZIO_STAGE_OPEN);
1246 	ASSERT(pio->io_gang_leader == NULL);
1247 	ASSERT(pio->io_gang_tree == NULL);
1248 
1249 	pio->io_flags = pio->io_orig_flags;
1250 	pio->io_stage = pio->io_orig_stage;
1251 	pio->io_pipeline = pio->io_orig_pipeline;
1252 	pio->io_reexecute = 0;
1253 	pio->io_error = 0;
1254 	for (int w = 0; w < ZIO_WAIT_TYPES; w++)
1255 		pio->io_state[w] = 0;
1256 	for (int c = 0; c < ZIO_CHILD_TYPES; c++)
1257 		pio->io_child_error[c] = 0;
1258 
1259 	if (IO_IS_ALLOCATING(pio))
1260 		BP_ZERO(pio->io_bp);
1261 
1262 	/*
1263 	 * As we reexecute pio's children, new children could be created.
1264 	 * New children go to the head of pio's io_child_list, however,
1265 	 * so we will (correctly) not reexecute them.  The key is that
1266 	 * the remainder of pio's io_child_list, from 'cio_next' onward,
1267 	 * cannot be affected by any side effects of reexecuting 'cio'.
1268 	 */
1269 	for (cio = zio_walk_children(pio); cio != NULL; cio = cio_next) {
1270 		cio_next = zio_walk_children(pio);
1271 		mutex_enter(&pio->io_lock);
1272 		for (int w = 0; w < ZIO_WAIT_TYPES; w++)
1273 			pio->io_children[cio->io_child_type][w]++;
1274 		mutex_exit(&pio->io_lock);
1275 		zio_reexecute(cio);
1276 	}
1277 
1278 	/*
1279 	 * Now that all children have been reexecuted, execute the parent.
1280 	 * We don't reexecute "The Godfather" I/O here as it's the
1281 	 * responsibility of the caller to wait on him.
1282 	 */
1283 	if (!(pio->io_flags & ZIO_FLAG_GODFATHER))
1284 		zio_execute(pio);
1285 }
1286 
1287 void
zio_suspend(spa_t * spa,zio_t * zio)1288 zio_suspend(spa_t *spa, zio_t *zio)
1289 {
1290 	if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_PANIC)
1291 		fm_panic("Pool '%s' has encountered an uncorrectable I/O "
1292 		    "failure and the failure mode property for this pool "
1293 		    "is set to panic.", spa_name(spa));
1294 
1295 	zfs_ereport_post(FM_EREPORT_ZFS_IO_FAILURE, spa, NULL, NULL, 0, 0);
1296 
1297 	mutex_enter(&spa->spa_suspend_lock);
1298 
1299 	if (spa->spa_suspend_zio_root == NULL)
1300 		spa->spa_suspend_zio_root = zio_root(spa, NULL, NULL,
1301 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
1302 		    ZIO_FLAG_GODFATHER);
1303 
1304 	spa->spa_suspended = B_TRUE;
1305 
1306 	if (zio != NULL) {
1307 		ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER));
1308 		ASSERT(zio != spa->spa_suspend_zio_root);
1309 		ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1310 		ASSERT(zio_unique_parent(zio) == NULL);
1311 		ASSERT(zio->io_stage == ZIO_STAGE_DONE);
1312 		zio_add_child(spa->spa_suspend_zio_root, zio);
1313 	}
1314 
1315 	mutex_exit(&spa->spa_suspend_lock);
1316 }
1317 
1318 int
zio_resume(spa_t * spa)1319 zio_resume(spa_t *spa)
1320 {
1321 	zio_t *pio;
1322 
1323 	/*
1324 	 * Reexecute all previously suspended i/o.
1325 	 */
1326 	mutex_enter(&spa->spa_suspend_lock);
1327 	spa->spa_suspended = B_FALSE;
1328 	cv_broadcast(&spa->spa_suspend_cv);
1329 	pio = spa->spa_suspend_zio_root;
1330 	spa->spa_suspend_zio_root = NULL;
1331 	mutex_exit(&spa->spa_suspend_lock);
1332 
1333 	if (pio == NULL)
1334 		return (0);
1335 
1336 	zio_reexecute(pio);
1337 	return (zio_wait(pio));
1338 }
1339 
1340 void
zio_resume_wait(spa_t * spa)1341 zio_resume_wait(spa_t *spa)
1342 {
1343 	mutex_enter(&spa->spa_suspend_lock);
1344 	while (spa_suspended(spa))
1345 		cv_wait(&spa->spa_suspend_cv, &spa->spa_suspend_lock);
1346 	mutex_exit(&spa->spa_suspend_lock);
1347 }
1348 
1349 /*
1350  * ==========================================================================
1351  * Gang blocks.
1352  *
1353  * A gang block is a collection of small blocks that looks to the DMU
1354  * like one large block.  When zio_dva_allocate() cannot find a block
1355  * of the requested size, due to either severe fragmentation or the pool
1356  * being nearly full, it calls zio_write_gang_block() to construct the
1357  * block from smaller fragments.
1358  *
1359  * A gang block consists of a gang header (zio_gbh_phys_t) and up to
1360  * three (SPA_GBH_NBLKPTRS) gang members.  The gang header is just like
1361  * an indirect block: it's an array of block pointers.  It consumes
1362  * only one sector and hence is allocatable regardless of fragmentation.
1363  * The gang header's bps point to its gang members, which hold the data.
1364  *
1365  * Gang blocks are self-checksumming, using the bp's <vdev, offset, txg>
1366  * as the verifier to ensure uniqueness of the SHA256 checksum.
1367  * Critically, the gang block bp's blk_cksum is the checksum of the data,
1368  * not the gang header.  This ensures that data block signatures (needed for
1369  * deduplication) are independent of how the block is physically stored.
1370  *
1371  * Gang blocks can be nested: a gang member may itself be a gang block.
1372  * Thus every gang block is a tree in which root and all interior nodes are
1373  * gang headers, and the leaves are normal blocks that contain user data.
1374  * The root of the gang tree is called the gang leader.
1375  *
1376  * To perform any operation (read, rewrite, free, claim) on a gang block,
1377  * zio_gang_assemble() first assembles the gang tree (minus data leaves)
1378  * in the io_gang_tree field of the original logical i/o by recursively
1379  * reading the gang leader and all gang headers below it.  This yields
1380  * an in-core tree containing the contents of every gang header and the
1381  * bps for every constituent of the gang block.
1382  *
1383  * With the gang tree now assembled, zio_gang_issue() just walks the gang tree
1384  * and invokes a callback on each bp.  To free a gang block, zio_gang_issue()
1385  * calls zio_free_gang() -- a trivial wrapper around zio_free() -- for each bp.
1386  * zio_claim_gang() provides a similarly trivial wrapper for zio_claim().
1387  * zio_read_gang() is a wrapper around zio_read() that omits reading gang
1388  * headers, since we already have those in io_gang_tree.  zio_rewrite_gang()
1389  * performs a zio_rewrite() of the data or, for gang headers, a zio_rewrite()
1390  * of the gang header plus zio_checksum_compute() of the data to update the
1391  * gang header's blk_cksum as described above.
1392  *
1393  * The two-phase assemble/issue model solves the problem of partial failure --
1394  * what if you'd freed part of a gang block but then couldn't read the
1395  * gang header for another part?  Assembling the entire gang tree first
1396  * ensures that all the necessary gang header I/O has succeeded before
1397  * starting the actual work of free, claim, or write.  Once the gang tree
1398  * is assembled, free and claim are in-memory operations that cannot fail.
1399  *
1400  * In the event that a gang write fails, zio_dva_unallocate() walks the
1401  * gang tree to immediately free (i.e. insert back into the space map)
1402  * everything we've allocated.  This ensures that we don't get ENOSPC
1403  * errors during repeated suspend/resume cycles due to a flaky device.
1404  *
1405  * Gang rewrites only happen during sync-to-convergence.  If we can't assemble
1406  * the gang tree, we won't modify the block, so we can safely defer the free
1407  * (knowing that the block is still intact).  If we *can* assemble the gang
1408  * tree, then even if some of the rewrites fail, zio_dva_unallocate() will free
1409  * each constituent bp and we can allocate a new block on the next sync pass.
1410  *
1411  * In all cases, the gang tree allows complete recovery from partial failure.
1412  * ==========================================================================
1413  */
1414 
1415 static zio_t *
zio_read_gang(zio_t * pio,blkptr_t * bp,zio_gang_node_t * gn,void * data)1416 zio_read_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
1417 {
1418 	if (gn != NULL)
1419 		return (pio);
1420 
1421 	return (zio_read(pio, pio->io_spa, bp, data, BP_GET_PSIZE(bp),
1422 	    NULL, NULL, pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio),
1423 	    &pio->io_bookmark));
1424 }
1425 
1426 zio_t *
zio_rewrite_gang(zio_t * pio,blkptr_t * bp,zio_gang_node_t * gn,void * data)1427 zio_rewrite_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
1428 {
1429 	zio_t *zio;
1430 
1431 	if (gn != NULL) {
1432 		zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp,
1433 		    gn->gn_gbh, SPA_GANGBLOCKSIZE, NULL, NULL, pio->io_priority,
1434 		    ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
1435 		/*
1436 		 * As we rewrite each gang header, the pipeline will compute
1437 		 * a new gang block header checksum for it; but no one will
1438 		 * compute a new data checksum, so we do that here.  The one
1439 		 * exception is the gang leader: the pipeline already computed
1440 		 * its data checksum because that stage precedes gang assembly.
1441 		 * (Presently, nothing actually uses interior data checksums;
1442 		 * this is just good hygiene.)
1443 		 */
1444 		if (gn != pio->io_gang_leader->io_gang_tree) {
1445 			zio_checksum_compute(zio, BP_GET_CHECKSUM(bp),
1446 			    data, BP_GET_PSIZE(bp));
1447 		}
1448 		/*
1449 		 * If we are here to damage data for testing purposes,
1450 		 * leave the GBH alone so that we can detect the damage.
1451 		 */
1452 		if (pio->io_gang_leader->io_flags & ZIO_FLAG_INDUCE_DAMAGE)
1453 			zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES;
1454 	} else {
1455 		zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp,
1456 		    data, BP_GET_PSIZE(bp), NULL, NULL, pio->io_priority,
1457 		    ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
1458 	}
1459 
1460 	return (zio);
1461 }
1462 
1463 /* ARGSUSED */
1464 zio_t *
zio_free_gang(zio_t * pio,blkptr_t * bp,zio_gang_node_t * gn,void * data)1465 zio_free_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
1466 {
1467 	return (zio_free_sync(pio, pio->io_spa, pio->io_txg, bp,
1468 	    ZIO_GANG_CHILD_FLAGS(pio)));
1469 }
1470 
1471 /* ARGSUSED */
1472 zio_t *
zio_claim_gang(zio_t * pio,blkptr_t * bp,zio_gang_node_t * gn,void * data)1473 zio_claim_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
1474 {
1475 	return (zio_claim(pio, pio->io_spa, pio->io_txg, bp,
1476 	    NULL, NULL, ZIO_GANG_CHILD_FLAGS(pio)));
1477 }
1478 
1479 static zio_gang_issue_func_t *zio_gang_issue_func[ZIO_TYPES] = {
1480 	NULL,
1481 	zio_read_gang,
1482 	zio_rewrite_gang,
1483 	zio_free_gang,
1484 	zio_claim_gang,
1485 	NULL
1486 };
1487 
1488 static void zio_gang_tree_assemble_done(zio_t *zio);
1489 
1490 static zio_gang_node_t *
zio_gang_node_alloc(zio_gang_node_t ** gnpp)1491 zio_gang_node_alloc(zio_gang_node_t **gnpp)
1492 {
1493 	zio_gang_node_t *gn;
1494 
1495 	ASSERT(*gnpp == NULL);
1496 
1497 	gn = kmem_zalloc(sizeof (*gn), KM_SLEEP);
1498 	gn->gn_gbh = zio_buf_alloc(SPA_GANGBLOCKSIZE);
1499 	*gnpp = gn;
1500 
1501 	return (gn);
1502 }
1503 
1504 static void
zio_gang_node_free(zio_gang_node_t ** gnpp)1505 zio_gang_node_free(zio_gang_node_t **gnpp)
1506 {
1507 	zio_gang_node_t *gn = *gnpp;
1508 
1509 	for (int g = 0; g < SPA_GBH_NBLKPTRS; g++)
1510 		ASSERT(gn->gn_child[g] == NULL);
1511 
1512 	zio_buf_free(gn->gn_gbh, SPA_GANGBLOCKSIZE);
1513 	kmem_free(gn, sizeof (*gn));
1514 	*gnpp = NULL;
1515 }
1516 
1517 static void
zio_gang_tree_free(zio_gang_node_t ** gnpp)1518 zio_gang_tree_free(zio_gang_node_t **gnpp)
1519 {
1520 	zio_gang_node_t *gn = *gnpp;
1521 
1522 	if (gn == NULL)
1523 		return;
1524 
1525 	for (int g = 0; g < SPA_GBH_NBLKPTRS; g++)
1526 		zio_gang_tree_free(&gn->gn_child[g]);
1527 
1528 	zio_gang_node_free(gnpp);
1529 }
1530 
1531 static void
zio_gang_tree_assemble(zio_t * gio,blkptr_t * bp,zio_gang_node_t ** gnpp)1532 zio_gang_tree_assemble(zio_t *gio, blkptr_t *bp, zio_gang_node_t **gnpp)
1533 {
1534 	zio_gang_node_t *gn = zio_gang_node_alloc(gnpp);
1535 
1536 	ASSERT(gio->io_gang_leader == gio);
1537 	ASSERT(BP_IS_GANG(bp));
1538 
1539 	zio_nowait(zio_read(gio, gio->io_spa, bp, gn->gn_gbh,
1540 	    SPA_GANGBLOCKSIZE, zio_gang_tree_assemble_done, gn,
1541 	    gio->io_priority, ZIO_GANG_CHILD_FLAGS(gio), &gio->io_bookmark));
1542 }
1543 
1544 static void
zio_gang_tree_assemble_done(zio_t * zio)1545 zio_gang_tree_assemble_done(zio_t *zio)
1546 {
1547 	zio_t *gio = zio->io_gang_leader;
1548 	zio_gang_node_t *gn = zio->io_private;
1549 	blkptr_t *bp = zio->io_bp;
1550 
1551 	ASSERT(gio == zio_unique_parent(zio));
1552 	ASSERT(zio->io_child_count == 0);
1553 
1554 	if (zio->io_error)
1555 		return;
1556 
1557 	if (BP_SHOULD_BYTESWAP(bp))
1558 		byteswap_uint64_array(zio->io_data, zio->io_size);
1559 
1560 	ASSERT(zio->io_data == gn->gn_gbh);
1561 	ASSERT(zio->io_size == SPA_GANGBLOCKSIZE);
1562 	ASSERT(gn->gn_gbh->zg_tail.zec_magic == ZEC_MAGIC);
1563 
1564 	for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
1565 		blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g];
1566 		if (!BP_IS_GANG(gbp))
1567 			continue;
1568 		zio_gang_tree_assemble(gio, gbp, &gn->gn_child[g]);
1569 	}
1570 }
1571 
1572 static void
zio_gang_tree_issue(zio_t * pio,zio_gang_node_t * gn,blkptr_t * bp,void * data)1573 zio_gang_tree_issue(zio_t *pio, zio_gang_node_t *gn, blkptr_t *bp, void *data)
1574 {
1575 	zio_t *gio = pio->io_gang_leader;
1576 	zio_t *zio;
1577 
1578 	ASSERT(BP_IS_GANG(bp) == !!gn);
1579 	ASSERT(BP_GET_CHECKSUM(bp) == BP_GET_CHECKSUM(gio->io_bp));
1580 	ASSERT(BP_GET_LSIZE(bp) == BP_GET_PSIZE(bp) || gn == gio->io_gang_tree);
1581 
1582 	/*
1583 	 * If you're a gang header, your data is in gn->gn_gbh.
1584 	 * If you're a gang member, your data is in 'data' and gn == NULL.
1585 	 */
1586 	zio = zio_gang_issue_func[gio->io_type](pio, bp, gn, data);
1587 
1588 	if (gn != NULL) {
1589 		ASSERT(gn->gn_gbh->zg_tail.zec_magic == ZEC_MAGIC);
1590 
1591 		for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
1592 			blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g];
1593 			if (BP_IS_HOLE(gbp))
1594 				continue;
1595 			zio_gang_tree_issue(zio, gn->gn_child[g], gbp, data);
1596 			data = (char *)data + BP_GET_PSIZE(gbp);
1597 		}
1598 	}
1599 
1600 	if (gn == gio->io_gang_tree)
1601 		ASSERT3P((char *)gio->io_data + gio->io_size, ==, data);
1602 
1603 	if (zio != pio)
1604 		zio_nowait(zio);
1605 }
1606 
1607 static int
zio_gang_assemble(zio_t * zio)1608 zio_gang_assemble(zio_t *zio)
1609 {
1610 	blkptr_t *bp = zio->io_bp;
1611 
1612 	ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == NULL);
1613 	ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
1614 
1615 	zio->io_gang_leader = zio;
1616 
1617 	zio_gang_tree_assemble(zio, bp, &zio->io_gang_tree);
1618 
1619 	return (ZIO_PIPELINE_CONTINUE);
1620 }
1621 
1622 static int
zio_gang_issue(zio_t * zio)1623 zio_gang_issue(zio_t *zio)
1624 {
1625 	blkptr_t *bp = zio->io_bp;
1626 
1627 	if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_DONE))
1628 		return (ZIO_PIPELINE_STOP);
1629 
1630 	ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == zio);
1631 	ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
1632 
1633 	if (zio->io_child_error[ZIO_CHILD_GANG] == 0)
1634 		zio_gang_tree_issue(zio, zio->io_gang_tree, bp, zio->io_data);
1635 	else
1636 		zio_gang_tree_free(&zio->io_gang_tree);
1637 
1638 	zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1639 
1640 	return (ZIO_PIPELINE_CONTINUE);
1641 }
1642 
1643 static void
zio_write_gang_member_ready(zio_t * zio)1644 zio_write_gang_member_ready(zio_t *zio)
1645 {
1646 	zio_t *pio = zio_unique_parent(zio);
1647 	zio_t *gio = zio->io_gang_leader;
1648 	dva_t *cdva = zio->io_bp->blk_dva;
1649 	dva_t *pdva = pio->io_bp->blk_dva;
1650 	uint64_t asize;
1651 
1652 	if (BP_IS_HOLE(zio->io_bp))
1653 		return;
1654 
1655 	ASSERT(BP_IS_HOLE(&zio->io_bp_orig));
1656 
1657 	ASSERT(zio->io_child_type == ZIO_CHILD_GANG);
1658 	ASSERT3U(zio->io_prop.zp_copies, ==, gio->io_prop.zp_copies);
1659 	ASSERT3U(zio->io_prop.zp_copies, <=, BP_GET_NDVAS(zio->io_bp));
1660 	ASSERT3U(pio->io_prop.zp_copies, <=, BP_GET_NDVAS(pio->io_bp));
1661 	ASSERT3U(BP_GET_NDVAS(zio->io_bp), <=, BP_GET_NDVAS(pio->io_bp));
1662 
1663 	mutex_enter(&pio->io_lock);
1664 	for (int d = 0; d < BP_GET_NDVAS(zio->io_bp); d++) {
1665 		ASSERT(DVA_GET_GANG(&pdva[d]));
1666 		asize = DVA_GET_ASIZE(&pdva[d]);
1667 		asize += DVA_GET_ASIZE(&cdva[d]);
1668 		DVA_SET_ASIZE(&pdva[d], asize);
1669 	}
1670 	mutex_exit(&pio->io_lock);
1671 }
1672 
1673 static int
zio_write_gang_block(zio_t * pio)1674 zio_write_gang_block(zio_t *pio)
1675 {
1676 	spa_t *spa = pio->io_spa;
1677 	blkptr_t *bp = pio->io_bp;
1678 	zio_t *gio = pio->io_gang_leader;
1679 	zio_t *zio;
1680 	zio_gang_node_t *gn, **gnpp;
1681 	zio_gbh_phys_t *gbh;
1682 	uint64_t txg = pio->io_txg;
1683 	uint64_t resid = pio->io_size;
1684 	uint64_t lsize;
1685 	int copies = gio->io_prop.zp_copies;
1686 	int gbh_copies = MIN(copies + 1, spa_max_replication(spa));
1687 	zio_prop_t zp;
1688 	int error;
1689 
1690 	error = metaslab_alloc(spa, spa_normal_class(spa), SPA_GANGBLOCKSIZE,
1691 	    bp, gbh_copies, txg, pio == gio ? NULL : gio->io_bp,
1692 	    METASLAB_HINTBP_FAVOR | METASLAB_GANG_HEADER);
1693 	if (error) {
1694 		pio->io_error = error;
1695 		return (ZIO_PIPELINE_CONTINUE);
1696 	}
1697 
1698 	if (pio == gio) {
1699 		gnpp = &gio->io_gang_tree;
1700 	} else {
1701 		gnpp = pio->io_private;
1702 		ASSERT(pio->io_ready == zio_write_gang_member_ready);
1703 	}
1704 
1705 	gn = zio_gang_node_alloc(gnpp);
1706 	gbh = gn->gn_gbh;
1707 	bzero(gbh, SPA_GANGBLOCKSIZE);
1708 
1709 	/*
1710 	 * Create the gang header.
1711 	 */
1712 	zio = zio_rewrite(pio, spa, txg, bp, gbh, SPA_GANGBLOCKSIZE, NULL, NULL,
1713 	    pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
1714 
1715 	/*
1716 	 * Create and nowait the gang children.
1717 	 */
1718 	for (int g = 0; resid != 0; resid -= lsize, g++) {
1719 		lsize = P2ROUNDUP(resid / (SPA_GBH_NBLKPTRS - g),
1720 		    SPA_MINBLOCKSIZE);
1721 		ASSERT(lsize >= SPA_MINBLOCKSIZE && lsize <= resid);
1722 
1723 		zp.zp_checksum = gio->io_prop.zp_checksum;
1724 		zp.zp_compress = ZIO_COMPRESS_OFF;
1725 		zp.zp_type = DMU_OT_NONE;
1726 		zp.zp_level = 0;
1727 		zp.zp_copies = gio->io_prop.zp_copies;
1728 		zp.zp_dedup = 0;
1729 		zp.zp_dedup_verify = 0;
1730 
1731 		zio_nowait(zio_write(zio, spa, txg, &gbh->zg_blkptr[g],
1732 		    (char *)pio->io_data + (pio->io_size - resid), lsize, &zp,
1733 		    zio_write_gang_member_ready, NULL, &gn->gn_child[g],
1734 		    pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio),
1735 		    &pio->io_bookmark));
1736 	}
1737 
1738 	/*
1739 	 * Set pio's pipeline to just wait for zio to finish.
1740 	 */
1741 	pio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1742 
1743 	zio_nowait(zio);
1744 
1745 	return (ZIO_PIPELINE_CONTINUE);
1746 }
1747 
1748 /*
1749  * ==========================================================================
1750  * Dedup
1751  * ==========================================================================
1752  */
1753 static void
zio_ddt_child_read_done(zio_t * zio)1754 zio_ddt_child_read_done(zio_t *zio)
1755 {
1756 	blkptr_t *bp = zio->io_bp;
1757 	ddt_entry_t *dde = zio->io_private;
1758 	ddt_phys_t *ddp;
1759 	zio_t *pio = zio_unique_parent(zio);
1760 
1761 	mutex_enter(&pio->io_lock);
1762 	ddp = ddt_phys_select(dde, bp);
1763 	if (zio->io_error == 0)
1764 		ddt_phys_clear(ddp);	/* this ddp doesn't need repair */
1765 	if (zio->io_error == 0 && dde->dde_repair_data == NULL)
1766 		dde->dde_repair_data = zio->io_data;
1767 	else
1768 		zio_buf_free(zio->io_data, zio->io_size);
1769 	mutex_exit(&pio->io_lock);
1770 }
1771 
1772 static int
zio_ddt_read_start(zio_t * zio)1773 zio_ddt_read_start(zio_t *zio)
1774 {
1775 	blkptr_t *bp = zio->io_bp;
1776 
1777 	ASSERT(BP_GET_DEDUP(bp));
1778 	ASSERT(BP_GET_PSIZE(bp) == zio->io_size);
1779 	ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1780 
1781 	if (zio->io_child_error[ZIO_CHILD_DDT]) {
1782 		ddt_t *ddt = ddt_select(zio->io_spa, bp);
1783 		ddt_entry_t *dde = ddt_repair_start(ddt, bp);
1784 		ddt_phys_t *ddp = dde->dde_phys;
1785 		ddt_phys_t *ddp_self = ddt_phys_select(dde, bp);
1786 		blkptr_t blk;
1787 
1788 		ASSERT(zio->io_vsd == NULL);
1789 		zio->io_vsd = dde;
1790 
1791 		if (ddp_self == NULL)
1792 			return (ZIO_PIPELINE_CONTINUE);
1793 
1794 		for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
1795 			if (ddp->ddp_phys_birth == 0 || ddp == ddp_self)
1796 				continue;
1797 			ddt_bp_create(ddt->ddt_checksum, &dde->dde_key, ddp,
1798 			    &blk);
1799 			zio_nowait(zio_read(zio, zio->io_spa, &blk,
1800 			    zio_buf_alloc(zio->io_size), zio->io_size,
1801 			    zio_ddt_child_read_done, dde, zio->io_priority,
1802 			    ZIO_DDT_CHILD_FLAGS(zio) | ZIO_FLAG_DONT_PROPAGATE,
1803 			    &zio->io_bookmark));
1804 		}
1805 		return (ZIO_PIPELINE_CONTINUE);
1806 	}
1807 
1808 	zio_nowait(zio_read(zio, zio->io_spa, bp,
1809 	    zio->io_data, zio->io_size, NULL, NULL, zio->io_priority,
1810 	    ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark));
1811 
1812 	return (ZIO_PIPELINE_CONTINUE);
1813 }
1814 
1815 static int
zio_ddt_read_done(zio_t * zio)1816 zio_ddt_read_done(zio_t *zio)
1817 {
1818 	blkptr_t *bp = zio->io_bp;
1819 
1820 	if (zio_wait_for_children(zio, ZIO_CHILD_DDT, ZIO_WAIT_DONE))
1821 		return (ZIO_PIPELINE_STOP);
1822 
1823 	ASSERT(BP_GET_DEDUP(bp));
1824 	ASSERT(BP_GET_PSIZE(bp) == zio->io_size);
1825 	ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1826 
1827 	if (zio->io_child_error[ZIO_CHILD_DDT]) {
1828 		ddt_t *ddt = ddt_select(zio->io_spa, bp);
1829 		ddt_entry_t *dde = zio->io_vsd;
1830 		if (ddt == NULL) {
1831 			ASSERT(spa_load_state(zio->io_spa) != SPA_LOAD_NONE);
1832 			return (ZIO_PIPELINE_CONTINUE);
1833 		}
1834 		if (dde == NULL) {
1835 			zio->io_stage = ZIO_STAGE_DDT_READ_START >> 1;
1836 			zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE);
1837 			return (ZIO_PIPELINE_STOP);
1838 		}
1839 		if (dde->dde_repair_data != NULL) {
1840 			bcopy(dde->dde_repair_data, zio->io_data, zio->io_size);
1841 			zio->io_child_error[ZIO_CHILD_DDT] = 0;
1842 		}
1843 		ddt_repair_done(ddt, dde);
1844 		zio->io_vsd = NULL;
1845 	}
1846 
1847 	ASSERT(zio->io_vsd == NULL);
1848 
1849 	return (ZIO_PIPELINE_CONTINUE);
1850 }
1851 
1852 static boolean_t
zio_ddt_collision(zio_t * zio,ddt_t * ddt,ddt_entry_t * dde)1853 zio_ddt_collision(zio_t *zio, ddt_t *ddt, ddt_entry_t *dde)
1854 {
1855 	spa_t *spa = zio->io_spa;
1856 
1857 	/*
1858 	 * Note: we compare the original data, not the transformed data,
1859 	 * because when zio->io_bp is an override bp, we will not have
1860 	 * pushed the I/O transforms.  That's an important optimization
1861 	 * because otherwise we'd compress/encrypt all dmu_sync() data twice.
1862 	 */
1863 	for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) {
1864 		zio_t *lio = dde->dde_lead_zio[p];
1865 
1866 		if (lio != NULL) {
1867 			return (lio->io_orig_size != zio->io_orig_size ||
1868 			    bcmp(zio->io_orig_data, lio->io_orig_data,
1869 			    zio->io_orig_size) != 0);
1870 		}
1871 	}
1872 
1873 	for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) {
1874 		ddt_phys_t *ddp = &dde->dde_phys[p];
1875 
1876 		if (ddp->ddp_phys_birth != 0) {
1877 			arc_buf_t *abuf = NULL;
1878 			uint32_t aflags = ARC_WAIT;
1879 			blkptr_t blk = *zio->io_bp;
1880 			int error;
1881 
1882 			ddt_bp_fill(ddp, &blk, ddp->ddp_phys_birth);
1883 
1884 			ddt_exit(ddt);
1885 
1886 			error = arc_read_nolock(NULL, spa, &blk,
1887 			    arc_getbuf_func, &abuf, ZIO_PRIORITY_SYNC_READ,
1888 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
1889 			    &aflags, &zio->io_bookmark);
1890 
1891 			if (error == 0) {
1892 				if (arc_buf_size(abuf) != zio->io_orig_size ||
1893 				    bcmp(abuf->b_data, zio->io_orig_data,
1894 				    zio->io_orig_size) != 0)
1895 					error = EEXIST;
1896 				VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
1897 			}
1898 
1899 			ddt_enter(ddt);
1900 			return (error != 0);
1901 		}
1902 	}
1903 
1904 	return (B_FALSE);
1905 }
1906 
1907 static void
zio_ddt_child_write_ready(zio_t * zio)1908 zio_ddt_child_write_ready(zio_t *zio)
1909 {
1910 	int p = zio->io_prop.zp_copies;
1911 	ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp);
1912 	ddt_entry_t *dde = zio->io_private;
1913 	ddt_phys_t *ddp = &dde->dde_phys[p];
1914 	zio_t *pio;
1915 
1916 	if (zio->io_error)
1917 		return;
1918 
1919 	ddt_enter(ddt);
1920 
1921 	ASSERT(dde->dde_lead_zio[p] == zio);
1922 
1923 	ddt_phys_fill(ddp, zio->io_bp);
1924 
1925 	while ((pio = zio_walk_parents(zio)) != NULL)
1926 		ddt_bp_fill(ddp, pio->io_bp, zio->io_txg);
1927 
1928 	ddt_exit(ddt);
1929 }
1930 
1931 static void
zio_ddt_child_write_done(zio_t * zio)1932 zio_ddt_child_write_done(zio_t *zio)
1933 {
1934 	int p = zio->io_prop.zp_copies;
1935 	ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp);
1936 	ddt_entry_t *dde = zio->io_private;
1937 	ddt_phys_t *ddp = &dde->dde_phys[p];
1938 
1939 	ddt_enter(ddt);
1940 
1941 	ASSERT(ddp->ddp_refcnt == 0);
1942 	ASSERT(dde->dde_lead_zio[p] == zio);
1943 	dde->dde_lead_zio[p] = NULL;
1944 
1945 	if (zio->io_error == 0) {
1946 		while (zio_walk_parents(zio) != NULL)
1947 			ddt_phys_addref(ddp);
1948 	} else {
1949 		ddt_phys_clear(ddp);
1950 	}
1951 
1952 	ddt_exit(ddt);
1953 }
1954 
1955 static void
zio_ddt_ditto_write_done(zio_t * zio)1956 zio_ddt_ditto_write_done(zio_t *zio)
1957 {
1958 	int p = DDT_PHYS_DITTO;
1959 	zio_prop_t *zp = &zio->io_prop;
1960 	blkptr_t *bp = zio->io_bp;
1961 	ddt_t *ddt = ddt_select(zio->io_spa, bp);
1962 	ddt_entry_t *dde = zio->io_private;
1963 	ddt_phys_t *ddp = &dde->dde_phys[p];
1964 	ddt_key_t *ddk = &dde->dde_key;
1965 
1966 	ddt_enter(ddt);
1967 
1968 	ASSERT(ddp->ddp_refcnt == 0);
1969 	ASSERT(dde->dde_lead_zio[p] == zio);
1970 	dde->dde_lead_zio[p] = NULL;
1971 
1972 	if (zio->io_error == 0) {
1973 		ASSERT(ZIO_CHECKSUM_EQUAL(bp->blk_cksum, ddk->ddk_cksum));
1974 		ASSERT(zp->zp_copies < SPA_DVAS_PER_BP);
1975 		ASSERT(zp->zp_copies == BP_GET_NDVAS(bp) - BP_IS_GANG(bp));
1976 		if (ddp->ddp_phys_birth != 0)
1977 			ddt_phys_free(ddt, ddk, ddp, zio->io_txg);
1978 		ddt_phys_fill(ddp, bp);
1979 	}
1980 
1981 	ddt_exit(ddt);
1982 }
1983 
1984 static int
zio_ddt_write(zio_t * zio)1985 zio_ddt_write(zio_t *zio)
1986 {
1987 	spa_t *spa = zio->io_spa;
1988 	blkptr_t *bp = zio->io_bp;
1989 	uint64_t txg = zio->io_txg;
1990 	zio_prop_t *zp = &zio->io_prop;
1991 	int p = zp->zp_copies;
1992 	int ditto_copies;
1993 	zio_t *cio = NULL;
1994 	zio_t *dio = NULL;
1995 	ddt_t *ddt = ddt_select(spa, bp);
1996 	ddt_entry_t *dde;
1997 	ddt_phys_t *ddp;
1998 
1999 	ASSERT(BP_GET_DEDUP(bp));
2000 	ASSERT(BP_GET_CHECKSUM(bp) == zp->zp_checksum);
2001 	ASSERT(BP_IS_HOLE(bp) || zio->io_bp_override);
2002 
2003 	ddt_enter(ddt);
2004 	dde = ddt_lookup(ddt, bp, B_TRUE);
2005 	ddp = &dde->dde_phys[p];
2006 
2007 	if (zp->zp_dedup_verify && zio_ddt_collision(zio, ddt, dde)) {
2008 		/*
2009 		 * If we're using a weak checksum, upgrade to a strong checksum
2010 		 * and try again.  If we're already using a strong checksum,
2011 		 * we can't resolve it, so just convert to an ordinary write.
2012 		 * (And automatically e-mail a paper to Nature?)
2013 		 */
2014 		if (!zio_checksum_table[zp->zp_checksum].ci_dedup) {
2015 			zp->zp_checksum = spa_dedup_checksum(spa);
2016 			zio_pop_transforms(zio);
2017 			zio->io_stage = ZIO_STAGE_OPEN;
2018 			BP_ZERO(bp);
2019 		} else {
2020 			zp->zp_dedup = 0;
2021 		}
2022 		zio->io_pipeline = ZIO_WRITE_PIPELINE;
2023 		ddt_exit(ddt);
2024 		return (ZIO_PIPELINE_CONTINUE);
2025 	}
2026 
2027 	ditto_copies = ddt_ditto_copies_needed(ddt, dde, ddp);
2028 	ASSERT(ditto_copies < SPA_DVAS_PER_BP);
2029 
2030 	if (ditto_copies > ddt_ditto_copies_present(dde) &&
2031 	    dde->dde_lead_zio[DDT_PHYS_DITTO] == NULL) {
2032 		zio_prop_t czp = *zp;
2033 
2034 		czp.zp_copies = ditto_copies;
2035 
2036 		/*
2037 		 * If we arrived here with an override bp, we won't have run
2038 		 * the transform stack, so we won't have the data we need to
2039 		 * generate a child i/o.  So, toss the override bp and restart.
2040 		 * This is safe, because using the override bp is just an
2041 		 * optimization; and it's rare, so the cost doesn't matter.
2042 		 */
2043 		if (zio->io_bp_override) {
2044 			zio_pop_transforms(zio);
2045 			zio->io_stage = ZIO_STAGE_OPEN;
2046 			zio->io_pipeline = ZIO_WRITE_PIPELINE;
2047 			zio->io_bp_override = NULL;
2048 			BP_ZERO(bp);
2049 			ddt_exit(ddt);
2050 			return (ZIO_PIPELINE_CONTINUE);
2051 		}
2052 
2053 		dio = zio_write(zio, spa, txg, bp, zio->io_orig_data,
2054 		    zio->io_orig_size, &czp, NULL,
2055 		    zio_ddt_ditto_write_done, dde, zio->io_priority,
2056 		    ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark);
2057 
2058 		zio_push_transform(dio, zio->io_data, zio->io_size, 0, NULL);
2059 		dde->dde_lead_zio[DDT_PHYS_DITTO] = dio;
2060 	}
2061 
2062 	if (ddp->ddp_phys_birth != 0 || dde->dde_lead_zio[p] != NULL) {
2063 		if (ddp->ddp_phys_birth != 0)
2064 			ddt_bp_fill(ddp, bp, txg);
2065 		if (dde->dde_lead_zio[p] != NULL)
2066 			zio_add_child(zio, dde->dde_lead_zio[p]);
2067 		else
2068 			ddt_phys_addref(ddp);
2069 	} else if (zio->io_bp_override) {
2070 		ASSERT(bp->blk_birth == txg);
2071 		ASSERT(BP_EQUAL(bp, zio->io_bp_override));
2072 		ddt_phys_fill(ddp, bp);
2073 		ddt_phys_addref(ddp);
2074 	} else {
2075 		cio = zio_write(zio, spa, txg, bp, zio->io_orig_data,
2076 		    zio->io_orig_size, zp, zio_ddt_child_write_ready,
2077 		    zio_ddt_child_write_done, dde, zio->io_priority,
2078 		    ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark);
2079 
2080 		zio_push_transform(cio, zio->io_data, zio->io_size, 0, NULL);
2081 		dde->dde_lead_zio[p] = cio;
2082 	}
2083 
2084 	ddt_exit(ddt);
2085 
2086 	if (cio)
2087 		zio_nowait(cio);
2088 	if (dio)
2089 		zio_nowait(dio);
2090 
2091 	return (ZIO_PIPELINE_CONTINUE);
2092 }
2093 
2094 static int
zio_ddt_free(zio_t * zio)2095 zio_ddt_free(zio_t *zio)
2096 {
2097 	spa_t *spa = zio->io_spa;
2098 	blkptr_t *bp = zio->io_bp;
2099 	ddt_t *ddt = ddt_select(spa, bp);
2100 	ddt_entry_t *dde;
2101 	ddt_phys_t *ddp;
2102 
2103 	ASSERT(BP_GET_DEDUP(bp));
2104 	ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2105 
2106 	ddt_enter(ddt);
2107 	dde = ddt_lookup(ddt, bp, B_TRUE);
2108 	ddp = ddt_phys_select(dde, bp);
2109 	ddt_phys_decref(ddp);
2110 	ddt_exit(ddt);
2111 
2112 	return (ZIO_PIPELINE_CONTINUE);
2113 }
2114 
2115 /*
2116  * ==========================================================================
2117  * Allocate and free blocks
2118  * ==========================================================================
2119  */
2120 static int
zio_dva_allocate(zio_t * zio)2121 zio_dva_allocate(zio_t *zio)
2122 {
2123 	spa_t *spa = zio->io_spa;
2124 	metaslab_class_t *mc = spa_normal_class(spa);
2125 	blkptr_t *bp = zio->io_bp;
2126 	int error;
2127 
2128 	if (zio->io_gang_leader == NULL) {
2129 		ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
2130 		zio->io_gang_leader = zio;
2131 	}
2132 
2133 	ASSERT(BP_IS_HOLE(bp));
2134 	ASSERT3U(BP_GET_NDVAS(bp), ==, 0);
2135 	ASSERT3U(zio->io_prop.zp_copies, >, 0);
2136 	ASSERT3U(zio->io_prop.zp_copies, <=, spa_max_replication(spa));
2137 	ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp));
2138 
2139 	error = metaslab_alloc(spa, mc, zio->io_size, bp,
2140 	    zio->io_prop.zp_copies, zio->io_txg, NULL, 0);
2141 
2142 	if (error) {
2143 		if (error == ENOSPC && zio->io_size > SPA_MINBLOCKSIZE)
2144 			return (zio_write_gang_block(zio));
2145 		zio->io_error = error;
2146 	}
2147 
2148 	return (ZIO_PIPELINE_CONTINUE);
2149 }
2150 
2151 static int
zio_dva_free(zio_t * zio)2152 zio_dva_free(zio_t *zio)
2153 {
2154 	metaslab_free(zio->io_spa, zio->io_bp, zio->io_txg, B_FALSE);
2155 
2156 	return (ZIO_PIPELINE_CONTINUE);
2157 }
2158 
2159 static int
zio_dva_claim(zio_t * zio)2160 zio_dva_claim(zio_t *zio)
2161 {
2162 	int error;
2163 
2164 	error = metaslab_claim(zio->io_spa, zio->io_bp, zio->io_txg);
2165 	if (error)
2166 		zio->io_error = error;
2167 
2168 	return (ZIO_PIPELINE_CONTINUE);
2169 }
2170 
2171 /*
2172  * Undo an allocation.  This is used by zio_done() when an I/O fails
2173  * and we want to give back the block we just allocated.
2174  * This handles both normal blocks and gang blocks.
2175  */
2176 static void
zio_dva_unallocate(zio_t * zio,zio_gang_node_t * gn,blkptr_t * bp)2177 zio_dva_unallocate(zio_t *zio, zio_gang_node_t *gn, blkptr_t *bp)
2178 {
2179 	ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp));
2180 	ASSERT(zio->io_bp_override == NULL);
2181 
2182 	if (!BP_IS_HOLE(bp))
2183 		metaslab_free(zio->io_spa, bp, bp->blk_birth, B_TRUE);
2184 
2185 	if (gn != NULL) {
2186 		for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
2187 			zio_dva_unallocate(zio, gn->gn_child[g],
2188 			    &gn->gn_gbh->zg_blkptr[g]);
2189 		}
2190 	}
2191 }
2192 
2193 /*
2194  * Try to allocate an intent log block.  Return 0 on success, errno on failure.
2195  */
2196 int
zio_alloc_zil(spa_t * spa,uint64_t txg,blkptr_t * new_bp,blkptr_t * old_bp,uint64_t size,boolean_t use_slog)2197 zio_alloc_zil(spa_t *spa, uint64_t txg, blkptr_t *new_bp, blkptr_t *old_bp,
2198     uint64_t size, boolean_t use_slog)
2199 {
2200 	int error = 1;
2201 
2202 	ASSERT(txg > spa_syncing_txg(spa));
2203 
2204 	if (use_slog)
2205 		error = metaslab_alloc(spa, spa_log_class(spa), size,
2206 		    new_bp, 1, txg, old_bp, METASLAB_HINTBP_AVOID);
2207 
2208 	if (error)
2209 		error = metaslab_alloc(spa, spa_normal_class(spa), size,
2210 		    new_bp, 1, txg, old_bp, METASLAB_HINTBP_AVOID);
2211 
2212 	if (error == 0) {
2213 		BP_SET_LSIZE(new_bp, size);
2214 		BP_SET_PSIZE(new_bp, size);
2215 		BP_SET_COMPRESS(new_bp, ZIO_COMPRESS_OFF);
2216 		BP_SET_CHECKSUM(new_bp,
2217 		    spa_version(spa) >= SPA_VERSION_SLIM_ZIL
2218 		    ? ZIO_CHECKSUM_ZILOG2 : ZIO_CHECKSUM_ZILOG);
2219 		BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG);
2220 		BP_SET_LEVEL(new_bp, 0);
2221 		BP_SET_DEDUP(new_bp, 0);
2222 		BP_SET_BYTEORDER(new_bp, ZFS_HOST_BYTEORDER);
2223 	}
2224 
2225 	return (error);
2226 }
2227 
2228 /*
2229  * Free an intent log block.
2230  */
2231 void
zio_free_zil(spa_t * spa,uint64_t txg,blkptr_t * bp)2232 zio_free_zil(spa_t *spa, uint64_t txg, blkptr_t *bp)
2233 {
2234 	ASSERT(BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG);
2235 	ASSERT(!BP_IS_GANG(bp));
2236 
2237 	zio_free(spa, txg, bp);
2238 }
2239 
2240 /*
2241  * ==========================================================================
2242  * Read and write to physical devices
2243  * ==========================================================================
2244  */
2245 static int
zio_vdev_io_start(zio_t * zio)2246 zio_vdev_io_start(zio_t *zio)
2247 {
2248 	vdev_t *vd = zio->io_vd;
2249 	uint64_t align;
2250 	spa_t *spa = zio->io_spa;
2251 
2252 	ASSERT(zio->io_error == 0);
2253 	ASSERT(zio->io_child_error[ZIO_CHILD_VDEV] == 0);
2254 
2255 	if (vd == NULL) {
2256 		if (!(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
2257 			spa_config_enter(spa, SCL_ZIO, zio, RW_READER);
2258 
2259 		/*
2260 		 * The mirror_ops handle multiple DVAs in a single BP.
2261 		 */
2262 		return (vdev_mirror_ops.vdev_op_io_start(zio));
2263 	}
2264 
2265 	align = 1ULL << vd->vdev_top->vdev_ashift;
2266 
2267 	if (P2PHASE(zio->io_size, align) != 0) {
2268 		uint64_t asize = P2ROUNDUP(zio->io_size, align);
2269 		char *abuf = zio_buf_alloc(asize);
2270 		ASSERT(vd == vd->vdev_top);
2271 		if (zio->io_type == ZIO_TYPE_WRITE) {
2272 			bcopy(zio->io_data, abuf, zio->io_size);
2273 			bzero(abuf + zio->io_size, asize - zio->io_size);
2274 		}
2275 		zio_push_transform(zio, abuf, asize, asize, zio_subblock);
2276 	}
2277 
2278 	ASSERT(P2PHASE(zio->io_offset, align) == 0);
2279 	ASSERT(P2PHASE(zio->io_size, align) == 0);
2280 	ASSERT(zio->io_type != ZIO_TYPE_WRITE || spa_writeable(spa));
2281 
2282 	/*
2283 	 * If this is a repair I/O, and there's no self-healing involved --
2284 	 * that is, we're just resilvering what we expect to resilver --
2285 	 * then don't do the I/O unless zio's txg is actually in vd's DTL.
2286 	 * This prevents spurious resilvering with nested replication.
2287 	 * For example, given a mirror of mirrors, (A+B)+(C+D), if only
2288 	 * A is out of date, we'll read from C+D, then use the data to
2289 	 * resilver A+B -- but we don't actually want to resilver B, just A.
2290 	 * The top-level mirror has no way to know this, so instead we just
2291 	 * discard unnecessary repairs as we work our way down the vdev tree.
2292 	 * The same logic applies to any form of nested replication:
2293 	 * ditto + mirror, RAID-Z + replacing, etc.  This covers them all.
2294 	 */
2295 	if ((zio->io_flags & ZIO_FLAG_IO_REPAIR) &&
2296 	    !(zio->io_flags & ZIO_FLAG_SELF_HEAL) &&
2297 	    zio->io_txg != 0 &&	/* not a delegated i/o */
2298 	    !vdev_dtl_contains(vd, DTL_PARTIAL, zio->io_txg, 1)) {
2299 		ASSERT(zio->io_type == ZIO_TYPE_WRITE);
2300 		zio_vdev_io_bypass(zio);
2301 		return (ZIO_PIPELINE_CONTINUE);
2302 	}
2303 
2304 	if (vd->vdev_ops->vdev_op_leaf &&
2305 	    (zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE)) {
2306 
2307 		if (zio->io_type == ZIO_TYPE_READ && vdev_cache_read(zio) == 0)
2308 			return (ZIO_PIPELINE_CONTINUE);
2309 
2310 		if ((zio = vdev_queue_io(zio)) == NULL)
2311 			return (ZIO_PIPELINE_STOP);
2312 
2313 		if (!vdev_accessible(vd, zio)) {
2314 			zio->io_error = ENXIO;
2315 			zio_interrupt(zio);
2316 			return (ZIO_PIPELINE_STOP);
2317 		}
2318 	}
2319 
2320 	return (vd->vdev_ops->vdev_op_io_start(zio));
2321 }
2322 
2323 static int
zio_vdev_io_done(zio_t * zio)2324 zio_vdev_io_done(zio_t *zio)
2325 {
2326 	vdev_t *vd = zio->io_vd;
2327 	vdev_ops_t *ops = vd ? vd->vdev_ops : &vdev_mirror_ops;
2328 	boolean_t unexpected_error = B_FALSE;
2329 
2330 	if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE))
2331 		return (ZIO_PIPELINE_STOP);
2332 
2333 	ASSERT(zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE);
2334 
2335 	if (vd != NULL && vd->vdev_ops->vdev_op_leaf) {
2336 
2337 		vdev_queue_io_done(zio);
2338 
2339 		if (zio->io_type == ZIO_TYPE_WRITE)
2340 			vdev_cache_write(zio);
2341 
2342 		if (zio_injection_enabled && zio->io_error == 0)
2343 			zio->io_error = zio_handle_device_injection(vd,
2344 			    zio, EIO);
2345 
2346 		if (zio_injection_enabled && zio->io_error == 0)
2347 			zio->io_error = zio_handle_label_injection(zio, EIO);
2348 
2349 		if (zio->io_error) {
2350 			if (!vdev_accessible(vd, zio)) {
2351 				zio->io_error = ENXIO;
2352 			} else {
2353 				unexpected_error = B_TRUE;
2354 			}
2355 		}
2356 	}
2357 
2358 	ops->vdev_op_io_done(zio);
2359 
2360 	if (unexpected_error)
2361 		VERIFY(vdev_probe(vd, zio) == NULL);
2362 
2363 	return (ZIO_PIPELINE_CONTINUE);
2364 }
2365 
2366 /*
2367  * For non-raidz ZIOs, we can just copy aside the bad data read from the
2368  * disk, and use that to finish the checksum ereport later.
2369  */
2370 static void
zio_vsd_default_cksum_finish(zio_cksum_report_t * zcr,const void * good_buf)2371 zio_vsd_default_cksum_finish(zio_cksum_report_t *zcr,
2372     const void *good_buf)
2373 {
2374 	/* no processing needed */
2375 	zfs_ereport_finish_checksum(zcr, good_buf, zcr->zcr_cbdata, B_FALSE);
2376 }
2377 
2378 /*ARGSUSED*/
2379 void
zio_vsd_default_cksum_report(zio_t * zio,zio_cksum_report_t * zcr,void * ignored)2380 zio_vsd_default_cksum_report(zio_t *zio, zio_cksum_report_t *zcr, void *ignored)
2381 {
2382 	void *buf = zio_buf_alloc(zio->io_size);
2383 
2384 	bcopy(zio->io_data, buf, zio->io_size);
2385 
2386 	zcr->zcr_cbinfo = zio->io_size;
2387 	zcr->zcr_cbdata = buf;
2388 	zcr->zcr_finish = zio_vsd_default_cksum_finish;
2389 	zcr->zcr_free = zio_buf_free;
2390 }
2391 
2392 static int
zio_vdev_io_assess(zio_t * zio)2393 zio_vdev_io_assess(zio_t *zio)
2394 {
2395 	vdev_t *vd = zio->io_vd;
2396 
2397 	if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE))
2398 		return (ZIO_PIPELINE_STOP);
2399 
2400 	if (vd == NULL && !(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
2401 		spa_config_exit(zio->io_spa, SCL_ZIO, zio);
2402 
2403 	if (zio->io_vsd != NULL) {
2404 		zio->io_vsd_ops->vsd_free(zio);
2405 		zio->io_vsd = NULL;
2406 	}
2407 
2408 	if (zio_injection_enabled && zio->io_error == 0)
2409 		zio->io_error = zio_handle_fault_injection(zio, EIO);
2410 
2411 	/*
2412 	 * If the I/O failed, determine whether we should attempt to retry it.
2413 	 *
2414 	 * On retry, we cut in line in the issue queue, since we don't want
2415 	 * compression/checksumming/etc. work to prevent our (cheap) IO reissue.
2416 	 */
2417 	if (zio->io_error && vd == NULL &&
2418 	    !(zio->io_flags & (ZIO_FLAG_DONT_RETRY | ZIO_FLAG_IO_RETRY))) {
2419 		ASSERT(!(zio->io_flags & ZIO_FLAG_DONT_QUEUE));	/* not a leaf */
2420 		ASSERT(!(zio->io_flags & ZIO_FLAG_IO_BYPASS));	/* not a leaf */
2421 		zio->io_error = 0;
2422 		zio->io_flags |= ZIO_FLAG_IO_RETRY |
2423 		    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE;
2424 		zio->io_stage = ZIO_STAGE_VDEV_IO_START >> 1;
2425 		zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE,
2426 		    zio_requeue_io_start_cut_in_line);
2427 		return (ZIO_PIPELINE_STOP);
2428 	}
2429 
2430 	/*
2431 	 * If we got an error on a leaf device, convert it to ENXIO
2432 	 * if the device is not accessible at all.
2433 	 */
2434 	if (zio->io_error && vd != NULL && vd->vdev_ops->vdev_op_leaf &&
2435 	    !vdev_accessible(vd, zio))
2436 		zio->io_error = ENXIO;
2437 
2438 	/*
2439 	 * If we can't write to an interior vdev (mirror or RAID-Z),
2440 	 * set vdev_cant_write so that we stop trying to allocate from it.
2441 	 */
2442 	if (zio->io_error == ENXIO && zio->io_type == ZIO_TYPE_WRITE &&
2443 	    vd != NULL && !vd->vdev_ops->vdev_op_leaf)
2444 		vd->vdev_cant_write = B_TRUE;
2445 
2446 	if (zio->io_error)
2447 		zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2448 
2449 	return (ZIO_PIPELINE_CONTINUE);
2450 }
2451 
2452 void
zio_vdev_io_reissue(zio_t * zio)2453 zio_vdev_io_reissue(zio_t *zio)
2454 {
2455 	ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
2456 	ASSERT(zio->io_error == 0);
2457 
2458 	zio->io_stage >>= 1;
2459 }
2460 
2461 void
zio_vdev_io_redone(zio_t * zio)2462 zio_vdev_io_redone(zio_t *zio)
2463 {
2464 	ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_DONE);
2465 
2466 	zio->io_stage >>= 1;
2467 }
2468 
2469 void
zio_vdev_io_bypass(zio_t * zio)2470 zio_vdev_io_bypass(zio_t *zio)
2471 {
2472 	ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
2473 	ASSERT(zio->io_error == 0);
2474 
2475 	zio->io_flags |= ZIO_FLAG_IO_BYPASS;
2476 	zio->io_stage = ZIO_STAGE_VDEV_IO_ASSESS >> 1;
2477 }
2478 
2479 /*
2480  * ==========================================================================
2481  * Generate and verify checksums
2482  * ==========================================================================
2483  */
2484 static int
zio_checksum_generate(zio_t * zio)2485 zio_checksum_generate(zio_t *zio)
2486 {
2487 	blkptr_t *bp = zio->io_bp;
2488 	enum zio_checksum checksum;
2489 
2490 	if (bp == NULL) {
2491 		/*
2492 		 * This is zio_write_phys().
2493 		 * We're either generating a label checksum, or none at all.
2494 		 */
2495 		checksum = zio->io_prop.zp_checksum;
2496 
2497 		if (checksum == ZIO_CHECKSUM_OFF)
2498 			return (ZIO_PIPELINE_CONTINUE);
2499 
2500 		ASSERT(checksum == ZIO_CHECKSUM_LABEL);
2501 	} else {
2502 		if (BP_IS_GANG(bp) && zio->io_child_type == ZIO_CHILD_GANG) {
2503 			ASSERT(!IO_IS_ALLOCATING(zio));
2504 			checksum = ZIO_CHECKSUM_GANG_HEADER;
2505 		} else {
2506 			checksum = BP_GET_CHECKSUM(bp);
2507 		}
2508 	}
2509 
2510 	zio_checksum_compute(zio, checksum, zio->io_data, zio->io_size);
2511 
2512 	return (ZIO_PIPELINE_CONTINUE);
2513 }
2514 
2515 static int
zio_checksum_verify(zio_t * zio)2516 zio_checksum_verify(zio_t *zio)
2517 {
2518 	zio_bad_cksum_t info;
2519 	blkptr_t *bp = zio->io_bp;
2520 	int error;
2521 
2522 	ASSERT(zio->io_vd != NULL);
2523 
2524 	if (bp == NULL) {
2525 		/*
2526 		 * This is zio_read_phys().
2527 		 * We're either verifying a label checksum, or nothing at all.
2528 		 */
2529 		if (zio->io_prop.zp_checksum == ZIO_CHECKSUM_OFF)
2530 			return (ZIO_PIPELINE_CONTINUE);
2531 
2532 		ASSERT(zio->io_prop.zp_checksum == ZIO_CHECKSUM_LABEL);
2533 	}
2534 
2535 	if ((error = zio_checksum_error(zio, &info)) != 0) {
2536 		zio->io_error = error;
2537 		if (!(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
2538 			zfs_ereport_start_checksum(zio->io_spa,
2539 			    zio->io_vd, zio, zio->io_offset,
2540 			    zio->io_size, NULL, &info);
2541 		}
2542 	}
2543 
2544 	return (ZIO_PIPELINE_CONTINUE);
2545 }
2546 
2547 /*
2548  * Called by RAID-Z to ensure we don't compute the checksum twice.
2549  */
2550 void
zio_checksum_verified(zio_t * zio)2551 zio_checksum_verified(zio_t *zio)
2552 {
2553 	zio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY;
2554 }
2555 
2556 /*
2557  * ==========================================================================
2558  * Error rank.  Error are ranked in the order 0, ENXIO, ECKSUM, EIO, other.
2559  * An error of 0 indictes success.  ENXIO indicates whole-device failure,
2560  * which may be transient (e.g. unplugged) or permament.  ECKSUM and EIO
2561  * indicate errors that are specific to one I/O, and most likely permanent.
2562  * Any other error is presumed to be worse because we weren't expecting it.
2563  * ==========================================================================
2564  */
2565 int
zio_worst_error(int e1,int e2)2566 zio_worst_error(int e1, int e2)
2567 {
2568 	static int zio_error_rank[] = { 0, ENXIO, ECKSUM, EIO };
2569 	int r1, r2;
2570 
2571 	for (r1 = 0; r1 < sizeof (zio_error_rank) / sizeof (int); r1++)
2572 		if (e1 == zio_error_rank[r1])
2573 			break;
2574 
2575 	for (r2 = 0; r2 < sizeof (zio_error_rank) / sizeof (int); r2++)
2576 		if (e2 == zio_error_rank[r2])
2577 			break;
2578 
2579 	return (r1 > r2 ? e1 : e2);
2580 }
2581 
2582 /*
2583  * ==========================================================================
2584  * I/O completion
2585  * ==========================================================================
2586  */
2587 static int
zio_ready(zio_t * zio)2588 zio_ready(zio_t *zio)
2589 {
2590 	blkptr_t *bp = zio->io_bp;
2591 	zio_t *pio, *pio_next;
2592 
2593 	if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_READY) ||
2594 	    zio_wait_for_children(zio, ZIO_CHILD_DDT, ZIO_WAIT_READY))
2595 		return (ZIO_PIPELINE_STOP);
2596 
2597 	if (zio->io_ready) {
2598 		ASSERT(IO_IS_ALLOCATING(zio));
2599 		ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp));
2600 		ASSERT(zio->io_children[ZIO_CHILD_GANG][ZIO_WAIT_READY] == 0);
2601 
2602 		zio->io_ready(zio);
2603 	}
2604 
2605 	if (bp != NULL && bp != &zio->io_bp_copy)
2606 		zio->io_bp_copy = *bp;
2607 
2608 	if (zio->io_error)
2609 		zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2610 
2611 	mutex_enter(&zio->io_lock);
2612 	zio->io_state[ZIO_WAIT_READY] = 1;
2613 	pio = zio_walk_parents(zio);
2614 	mutex_exit(&zio->io_lock);
2615 
2616 	/*
2617 	 * As we notify zio's parents, new parents could be added.
2618 	 * New parents go to the head of zio's io_parent_list, however,
2619 	 * so we will (correctly) not notify them.  The remainder of zio's
2620 	 * io_parent_list, from 'pio_next' onward, cannot change because
2621 	 * all parents must wait for us to be done before they can be done.
2622 	 */
2623 	for (; pio != NULL; pio = pio_next) {
2624 		pio_next = zio_walk_parents(zio);
2625 		zio_notify_parent(pio, zio, ZIO_WAIT_READY);
2626 	}
2627 
2628 	if (zio->io_flags & ZIO_FLAG_NODATA) {
2629 		if (BP_IS_GANG(bp)) {
2630 			zio->io_flags &= ~ZIO_FLAG_NODATA;
2631 		} else {
2632 			ASSERT((uintptr_t)zio->io_data < SPA_MAXBLOCKSIZE);
2633 			zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES;
2634 		}
2635 	}
2636 
2637 	if (zio_injection_enabled &&
2638 	    zio->io_spa->spa_syncing_txg == zio->io_txg)
2639 		zio_handle_ignored_writes(zio);
2640 
2641 	return (ZIO_PIPELINE_CONTINUE);
2642 }
2643 
2644 static int
zio_done(zio_t * zio)2645 zio_done(zio_t *zio)
2646 {
2647 	spa_t *spa = zio->io_spa;
2648 	zio_t *lio = zio->io_logical;
2649 	blkptr_t *bp = zio->io_bp;
2650 	vdev_t *vd = zio->io_vd;
2651 	uint64_t psize = zio->io_size;
2652 	zio_t *pio, *pio_next;
2653 
2654 	/*
2655 	 * If our children haven't all completed,
2656 	 * wait for them and then repeat this pipeline stage.
2657 	 */
2658 	if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE) ||
2659 	    zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_DONE) ||
2660 	    zio_wait_for_children(zio, ZIO_CHILD_DDT, ZIO_WAIT_DONE) ||
2661 	    zio_wait_for_children(zio, ZIO_CHILD_LOGICAL, ZIO_WAIT_DONE))
2662 		return (ZIO_PIPELINE_STOP);
2663 
2664 	for (int c = 0; c < ZIO_CHILD_TYPES; c++)
2665 		for (int w = 0; w < ZIO_WAIT_TYPES; w++)
2666 			ASSERT(zio->io_children[c][w] == 0);
2667 
2668 	if (bp != NULL) {
2669 		ASSERT(bp->blk_pad[0] == 0);
2670 		ASSERT(bp->blk_pad[1] == 0);
2671 		ASSERT(bcmp(bp, &zio->io_bp_copy, sizeof (blkptr_t)) == 0 ||
2672 		    (bp == zio_unique_parent(zio)->io_bp));
2673 		if (zio->io_type == ZIO_TYPE_WRITE && !BP_IS_HOLE(bp) &&
2674 		    zio->io_bp_override == NULL &&
2675 		    !(zio->io_flags & ZIO_FLAG_IO_REPAIR)) {
2676 			ASSERT(!BP_SHOULD_BYTESWAP(bp));
2677 			ASSERT3U(zio->io_prop.zp_copies, <=, BP_GET_NDVAS(bp));
2678 			ASSERT(BP_COUNT_GANG(bp) == 0 ||
2679 			    (BP_COUNT_GANG(bp) == BP_GET_NDVAS(bp)));
2680 		}
2681 	}
2682 
2683 	/*
2684 	 * If there were child vdev/gang/ddt errors, they apply to us now.
2685 	 */
2686 	zio_inherit_child_errors(zio, ZIO_CHILD_VDEV);
2687 	zio_inherit_child_errors(zio, ZIO_CHILD_GANG);
2688 	zio_inherit_child_errors(zio, ZIO_CHILD_DDT);
2689 
2690 	/*
2691 	 * If the I/O on the transformed data was successful, generate any
2692 	 * checksum reports now while we still have the transformed data.
2693 	 */
2694 	if (zio->io_error == 0) {
2695 		while (zio->io_cksum_report != NULL) {
2696 			zio_cksum_report_t *zcr = zio->io_cksum_report;
2697 			uint64_t align = zcr->zcr_align;
2698 			uint64_t asize = P2ROUNDUP(psize, align);
2699 			char *abuf = zio->io_data;
2700 
2701 			if (asize != psize) {
2702 				abuf = zio_buf_alloc(asize);
2703 				bcopy(zio->io_data, abuf, psize);
2704 				bzero(abuf + psize, asize - psize);
2705 			}
2706 
2707 			zio->io_cksum_report = zcr->zcr_next;
2708 			zcr->zcr_next = NULL;
2709 			zcr->zcr_finish(zcr, abuf);
2710 			zfs_ereport_free_checksum(zcr);
2711 
2712 			if (asize != psize)
2713 				zio_buf_free(abuf, asize);
2714 		}
2715 	}
2716 
2717 	zio_pop_transforms(zio);	/* note: may set zio->io_error */
2718 
2719 	vdev_stat_update(zio, psize);
2720 
2721 	if (zio->io_error) {
2722 		/*
2723 		 * If this I/O is attached to a particular vdev,
2724 		 * generate an error message describing the I/O failure
2725 		 * at the block level.  We ignore these errors if the
2726 		 * device is currently unavailable.
2727 		 */
2728 		if (zio->io_error != ECKSUM && vd != NULL && !vdev_is_dead(vd))
2729 			zfs_ereport_post(FM_EREPORT_ZFS_IO, spa, vd, zio, 0, 0);
2730 
2731 		if ((zio->io_error == EIO || !(zio->io_flags &
2732 		    (ZIO_FLAG_SPECULATIVE | ZIO_FLAG_DONT_PROPAGATE))) &&
2733 		    zio == lio) {
2734 			/*
2735 			 * For logical I/O requests, tell the SPA to log the
2736 			 * error and generate a logical data ereport.
2737 			 */
2738 			spa_log_error(spa, zio);
2739 			zfs_ereport_post(FM_EREPORT_ZFS_DATA, spa, NULL, zio,
2740 			    0, 0);
2741 		}
2742 	}
2743 
2744 	if (zio->io_error && zio == lio) {
2745 		/*
2746 		 * Determine whether zio should be reexecuted.  This will
2747 		 * propagate all the way to the root via zio_notify_parent().
2748 		 */
2749 		ASSERT(vd == NULL && bp != NULL);
2750 		ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2751 
2752 		if (IO_IS_ALLOCATING(zio) &&
2753 		    !(zio->io_flags & ZIO_FLAG_CANFAIL)) {
2754 			if (zio->io_error != ENOSPC)
2755 				zio->io_reexecute |= ZIO_REEXECUTE_NOW;
2756 			else
2757 				zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
2758 		}
2759 
2760 		if ((zio->io_type == ZIO_TYPE_READ ||
2761 		    zio->io_type == ZIO_TYPE_FREE) &&
2762 		    zio->io_error == ENXIO &&
2763 		    spa_load_state(spa) == SPA_LOAD_NONE &&
2764 		    spa_get_failmode(spa) != ZIO_FAILURE_MODE_CONTINUE)
2765 			zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
2766 
2767 		if (!(zio->io_flags & ZIO_FLAG_CANFAIL) && !zio->io_reexecute)
2768 			zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
2769 
2770 		/*
2771 		 * Here is a possibly good place to attempt to do
2772 		 * either combinatorial reconstruction or error correction
2773 		 * based on checksums.  It also might be a good place
2774 		 * to send out preliminary ereports before we suspend
2775 		 * processing.
2776 		 */
2777 	}
2778 
2779 	/*
2780 	 * If there were logical child errors, they apply to us now.
2781 	 * We defer this until now to avoid conflating logical child
2782 	 * errors with errors that happened to the zio itself when
2783 	 * updating vdev stats and reporting FMA events above.
2784 	 */
2785 	zio_inherit_child_errors(zio, ZIO_CHILD_LOGICAL);
2786 
2787 	if ((zio->io_error || zio->io_reexecute) &&
2788 	    IO_IS_ALLOCATING(zio) && zio->io_gang_leader == zio &&
2789 	    !(zio->io_flags & ZIO_FLAG_IO_REWRITE))
2790 		zio_dva_unallocate(zio, zio->io_gang_tree, bp);
2791 
2792 	zio_gang_tree_free(&zio->io_gang_tree);
2793 
2794 	/*
2795 	 * Godfather I/Os should never suspend.
2796 	 */
2797 	if ((zio->io_flags & ZIO_FLAG_GODFATHER) &&
2798 	    (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND))
2799 		zio->io_reexecute = 0;
2800 
2801 	if (zio->io_reexecute) {
2802 		/*
2803 		 * This is a logical I/O that wants to reexecute.
2804 		 *
2805 		 * Reexecute is top-down.  When an i/o fails, if it's not
2806 		 * the root, it simply notifies its parent and sticks around.
2807 		 * The parent, seeing that it still has children in zio_done(),
2808 		 * does the same.  This percolates all the way up to the root.
2809 		 * The root i/o will reexecute or suspend the entire tree.
2810 		 *
2811 		 * This approach ensures that zio_reexecute() honors
2812 		 * all the original i/o dependency relationships, e.g.
2813 		 * parents not executing until children are ready.
2814 		 */
2815 		ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2816 
2817 		zio->io_gang_leader = NULL;
2818 
2819 		mutex_enter(&zio->io_lock);
2820 		zio->io_state[ZIO_WAIT_DONE] = 1;
2821 		mutex_exit(&zio->io_lock);
2822 
2823 		/*
2824 		 * "The Godfather" I/O monitors its children but is
2825 		 * not a true parent to them. It will track them through
2826 		 * the pipeline but severs its ties whenever they get into
2827 		 * trouble (e.g. suspended). This allows "The Godfather"
2828 		 * I/O to return status without blocking.
2829 		 */
2830 		for (pio = zio_walk_parents(zio); pio != NULL; pio = pio_next) {
2831 			zio_link_t *zl = zio->io_walk_link;
2832 			pio_next = zio_walk_parents(zio);
2833 
2834 			if ((pio->io_flags & ZIO_FLAG_GODFATHER) &&
2835 			    (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND)) {
2836 				zio_remove_child(pio, zio, zl);
2837 				zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
2838 			}
2839 		}
2840 
2841 		if ((pio = zio_unique_parent(zio)) != NULL) {
2842 			/*
2843 			 * We're not a root i/o, so there's nothing to do
2844 			 * but notify our parent.  Don't propagate errors
2845 			 * upward since we haven't permanently failed yet.
2846 			 */
2847 			ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER));
2848 			zio->io_flags |= ZIO_FLAG_DONT_PROPAGATE;
2849 			zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
2850 		} else if (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND) {
2851 			/*
2852 			 * We'd fail again if we reexecuted now, so suspend
2853 			 * until conditions improve (e.g. device comes online).
2854 			 */
2855 			zio_suspend(spa, zio);
2856 		} else {
2857 			/*
2858 			 * Reexecution is potentially a huge amount of work.
2859 			 * Hand it off to the otherwise-unused claim taskq.
2860 			 */
2861 			(void) taskq_dispatch(
2862 			    spa->spa_zio_taskq[ZIO_TYPE_CLAIM][ZIO_TASKQ_ISSUE],
2863 			    (task_func_t *)zio_reexecute, zio, TQ_SLEEP);
2864 		}
2865 		return (ZIO_PIPELINE_STOP);
2866 	}
2867 
2868 	ASSERT(zio->io_child_count == 0);
2869 	ASSERT(zio->io_reexecute == 0);
2870 	ASSERT(zio->io_error == 0 || (zio->io_flags & ZIO_FLAG_CANFAIL));
2871 
2872 	/*
2873 	 * Report any checksum errors, since the I/O is complete.
2874 	 */
2875 	while (zio->io_cksum_report != NULL) {
2876 		zio_cksum_report_t *zcr = zio->io_cksum_report;
2877 		zio->io_cksum_report = zcr->zcr_next;
2878 		zcr->zcr_next = NULL;
2879 		zcr->zcr_finish(zcr, NULL);
2880 		zfs_ereport_free_checksum(zcr);
2881 	}
2882 
2883 	/*
2884 	 * It is the responsibility of the done callback to ensure that this
2885 	 * particular zio is no longer discoverable for adoption, and as
2886 	 * such, cannot acquire any new parents.
2887 	 */
2888 	if (zio->io_done)
2889 		zio->io_done(zio);
2890 
2891 	mutex_enter(&zio->io_lock);
2892 	zio->io_state[ZIO_WAIT_DONE] = 1;
2893 	mutex_exit(&zio->io_lock);
2894 
2895 	for (pio = zio_walk_parents(zio); pio != NULL; pio = pio_next) {
2896 		zio_link_t *zl = zio->io_walk_link;
2897 		pio_next = zio_walk_parents(zio);
2898 		zio_remove_child(pio, zio, zl);
2899 		zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
2900 	}
2901 
2902 	if (zio->io_waiter != NULL) {
2903 		mutex_enter(&zio->io_lock);
2904 		zio->io_executor = NULL;
2905 		cv_broadcast(&zio->io_cv);
2906 		mutex_exit(&zio->io_lock);
2907 	} else {
2908 		zio_destroy(zio);
2909 	}
2910 
2911 	return (ZIO_PIPELINE_STOP);
2912 }
2913 
2914 /*
2915  * ==========================================================================
2916  * I/O pipeline definition
2917  * ==========================================================================
2918  */
2919 static zio_pipe_stage_t *zio_pipeline[] = {
2920 	NULL,
2921 	zio_read_bp_init,
2922 	zio_free_bp_init,
2923 	zio_issue_async,
2924 	zio_write_bp_init,
2925 	zio_checksum_generate,
2926 	zio_ddt_read_start,
2927 	zio_ddt_read_done,
2928 	zio_ddt_write,
2929 	zio_ddt_free,
2930 	zio_gang_assemble,
2931 	zio_gang_issue,
2932 	zio_dva_allocate,
2933 	zio_dva_free,
2934 	zio_dva_claim,
2935 	zio_ready,
2936 	zio_vdev_io_start,
2937 	zio_vdev_io_done,
2938 	zio_vdev_io_assess,
2939 	zio_checksum_verify,
2940 	zio_done
2941 };
2942