]> CyberLeo.Net >> Repos - FreeBSD/releng/8.2.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/zio.c
Copy stable/8 to releng/8.2 in preparation for FreeBSD-8.2 release.
[FreeBSD/releng/8.2.git] / sys / cddl / contrib / opensolaris / uts / common / fs / zfs / zio.c
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 2009 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
36 SYSCTL_DECL(_vfs_zfs);
37 SYSCTL_NODE(_vfs_zfs, OID_AUTO, zio, CTLFLAG_RW, 0, "ZFS ZIO");
38 static int zio_use_uma = 0;
39 TUNABLE_INT("vfs.zfs.zio.use_uma", &zio_use_uma);
40 SYSCTL_INT(_vfs_zfs_zio, OID_AUTO, use_uma, CTLFLAG_RDTUN, &zio_use_uma, 0,
41     "Use uma(9) for ZIO allocations");
42
43 /*
44  * ==========================================================================
45  * I/O priority table
46  * ==========================================================================
47  */
48 uint8_t zio_priority_table[ZIO_PRIORITY_TABLE_SIZE] = {
49         0,      /* ZIO_PRIORITY_NOW             */
50         0,      /* ZIO_PRIORITY_SYNC_READ       */
51         0,      /* ZIO_PRIORITY_SYNC_WRITE      */
52         0,      /* ZIO_PRIORITY_LOG_WRITE       */
53         1,      /* ZIO_PRIORITY_CACHE_FILL      */
54         1,      /* ZIO_PRIORITY_AGG             */
55         4,      /* ZIO_PRIORITY_FREE            */
56         4,      /* ZIO_PRIORITY_ASYNC_WRITE     */
57         6,      /* ZIO_PRIORITY_ASYNC_READ      */
58         10,     /* ZIO_PRIORITY_RESILVER        */
59         20,     /* ZIO_PRIORITY_SCRUB           */
60 };
61
62 /*
63  * ==========================================================================
64  * I/O type descriptions
65  * ==========================================================================
66  */
67 char *zio_type_name[ZIO_TYPES] = {
68         "zio_null", "zio_read", "zio_write", "zio_free", "zio_claim",
69         "zio_ioctl"
70 };
71
72 #define SYNC_PASS_DEFERRED_FREE 1       /* defer frees after this pass */
73 #define SYNC_PASS_DONT_COMPRESS 4       /* don't compress after this pass */
74 #define SYNC_PASS_REWRITE       1       /* rewrite new bps after this pass */
75
76 /*
77  * ==========================================================================
78  * I/O kmem caches
79  * ==========================================================================
80  */
81 kmem_cache_t *zio_cache;
82 kmem_cache_t *zio_link_cache;
83 kmem_cache_t *zio_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
84 kmem_cache_t *zio_data_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
85
86 #ifdef _KERNEL
87 extern vmem_t *zio_alloc_arena;
88 #endif
89
90 /*
91  * An allocating zio is one that either currently has the DVA allocate
92  * stage set or will have it later in its lifetime.
93  */
94 #define IO_IS_ALLOCATING(zio) \
95         ((zio)->io_orig_pipeline & (1U << ZIO_STAGE_DVA_ALLOCATE))
96
97 void
98 zio_init(void)
99 {
100         size_t c;
101         zio_cache = kmem_cache_create("zio_cache",
102             sizeof (zio_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
103         zio_link_cache = kmem_cache_create("zio_link_cache",
104             sizeof (zio_link_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
105
106         /*
107          * For small buffers, we want a cache for each multiple of
108          * SPA_MINBLOCKSIZE.  For medium-size buffers, we want a cache
109          * for each quarter-power of 2.  For large buffers, we want
110          * a cache for each multiple of PAGESIZE.
111          */
112         for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) {
113                 size_t size = (c + 1) << SPA_MINBLOCKSHIFT;
114                 size_t p2 = size;
115                 size_t align = 0;
116
117                 while (p2 & (p2 - 1))
118                         p2 &= p2 - 1;
119
120                 if (size <= 4 * SPA_MINBLOCKSIZE) {
121                         align = SPA_MINBLOCKSIZE;
122                 } else if (P2PHASE(size, PAGESIZE) == 0) {
123                         align = PAGESIZE;
124                 } else if (P2PHASE(size, p2 >> 2) == 0) {
125                         align = p2 >> 2;
126                 }
127
128                 if (align != 0) {
129                         char name[36];
130                         (void) sprintf(name, "zio_buf_%lu", (ulong_t)size);
131                         zio_buf_cache[c] = kmem_cache_create(name, size,
132                             align, NULL, NULL, NULL, NULL, NULL, KMC_NODEBUG);
133
134                         (void) sprintf(name, "zio_data_buf_%lu", (ulong_t)size);
135                         zio_data_buf_cache[c] = kmem_cache_create(name, size,
136                             align, NULL, NULL, NULL, NULL, NULL, KMC_NODEBUG);
137                 }
138         }
139
140         while (--c != 0) {
141                 ASSERT(zio_buf_cache[c] != NULL);
142                 if (zio_buf_cache[c - 1] == NULL)
143                         zio_buf_cache[c - 1] = zio_buf_cache[c];
144
145                 ASSERT(zio_data_buf_cache[c] != NULL);
146                 if (zio_data_buf_cache[c - 1] == NULL)
147                         zio_data_buf_cache[c - 1] = zio_data_buf_cache[c];
148         }
149
150         zio_inject_init();
151 }
152
153 void
154 zio_fini(void)
155 {
156         size_t c;
157         kmem_cache_t *last_cache = NULL;
158         kmem_cache_t *last_data_cache = NULL;
159
160         for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) {
161                 if (zio_buf_cache[c] != last_cache) {
162                         last_cache = zio_buf_cache[c];
163                         kmem_cache_destroy(zio_buf_cache[c]);
164                 }
165                 zio_buf_cache[c] = NULL;
166
167                 if (zio_data_buf_cache[c] != last_data_cache) {
168                         last_data_cache = zio_data_buf_cache[c];
169                         kmem_cache_destroy(zio_data_buf_cache[c]);
170                 }
171                 zio_data_buf_cache[c] = NULL;
172         }
173
174         kmem_cache_destroy(zio_link_cache);
175         kmem_cache_destroy(zio_cache);
176
177         zio_inject_fini();
178 }
179
180 /*
181  * ==========================================================================
182  * Allocate and free I/O buffers
183  * ==========================================================================
184  */
185
186 /*
187  * Use zio_buf_alloc to allocate ZFS metadata.  This data will appear in a
188  * crashdump if the kernel panics, so use it judiciously.  Obviously, it's
189  * useful to inspect ZFS metadata, but if possible, we should avoid keeping
190  * excess / transient data in-core during a crashdump.
191  */
192 void *
193 zio_buf_alloc(size_t size)
194 {
195         size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
196
197         ASSERT(c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
198
199         if (zio_use_uma)
200                 return (kmem_cache_alloc(zio_buf_cache[c], KM_PUSHPAGE));
201         else
202                 return (kmem_alloc(size, KM_SLEEP));
203 }
204
205 /*
206  * Use zio_data_buf_alloc to allocate data.  The data will not appear in a
207  * crashdump if the kernel panics.  This exists so that we will limit the amount
208  * of ZFS data that shows up in a kernel crashdump.  (Thus reducing the amount
209  * of kernel heap dumped to disk when the kernel panics)
210  */
211 void *
212 zio_data_buf_alloc(size_t size)
213 {
214         size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
215
216         ASSERT(c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
217
218         if (zio_use_uma)
219                 return (kmem_cache_alloc(zio_data_buf_cache[c], KM_PUSHPAGE));
220         else
221                 return (kmem_alloc(size, KM_SLEEP));
222 }
223
224 void
225 zio_buf_free(void *buf, size_t size)
226 {
227         size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
228
229         ASSERT(c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
230
231         if (zio_use_uma)
232                 kmem_cache_free(zio_buf_cache[c], buf);
233         else
234                 kmem_free(buf, size);
235 }
236
237 void
238 zio_data_buf_free(void *buf, size_t size)
239 {
240         size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
241
242         ASSERT(c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
243
244         if (zio_use_uma)
245                 kmem_cache_free(zio_data_buf_cache[c], buf);
246         else
247                 kmem_free(buf, size);
248 }
249
250 /*
251  * ==========================================================================
252  * Push and pop I/O transform buffers
253  * ==========================================================================
254  */
255 static void
256 zio_push_transform(zio_t *zio, void *data, uint64_t size, uint64_t bufsize,
257         zio_transform_func_t *transform)
258 {
259         zio_transform_t *zt = kmem_alloc(sizeof (zio_transform_t), KM_SLEEP);
260
261         zt->zt_orig_data = zio->io_data;
262         zt->zt_orig_size = zio->io_size;
263         zt->zt_bufsize = bufsize;
264         zt->zt_transform = transform;
265
266         zt->zt_next = zio->io_transform_stack;
267         zio->io_transform_stack = zt;
268
269         zio->io_data = data;
270         zio->io_size = size;
271 }
272
273 static void
274 zio_pop_transforms(zio_t *zio)
275 {
276         zio_transform_t *zt;
277
278         while ((zt = zio->io_transform_stack) != NULL) {
279                 if (zt->zt_transform != NULL)
280                         zt->zt_transform(zio,
281                             zt->zt_orig_data, zt->zt_orig_size);
282
283                 zio_buf_free(zio->io_data, zt->zt_bufsize);
284
285                 zio->io_data = zt->zt_orig_data;
286                 zio->io_size = zt->zt_orig_size;
287                 zio->io_transform_stack = zt->zt_next;
288
289                 kmem_free(zt, sizeof (zio_transform_t));
290         }
291 }
292
293 /*
294  * ==========================================================================
295  * I/O transform callbacks for subblocks and decompression
296  * ==========================================================================
297  */
298 static void
299 zio_subblock(zio_t *zio, void *data, uint64_t size)
300 {
301         ASSERT(zio->io_size > size);
302
303         if (zio->io_type == ZIO_TYPE_READ)
304                 bcopy(zio->io_data, data, size);
305 }
306
307 static void
308 zio_decompress(zio_t *zio, void *data, uint64_t size)
309 {
310         if (zio->io_error == 0 &&
311             zio_decompress_data(BP_GET_COMPRESS(zio->io_bp),
312             zio->io_data, zio->io_size, data, size) != 0)
313                 zio->io_error = EIO;
314 }
315
316 /*
317  * ==========================================================================
318  * I/O parent/child relationships and pipeline interlocks
319  * ==========================================================================
320  */
321 /*
322  * NOTE - Callers to zio_walk_parents() and zio_walk_children must
323  *        continue calling these functions until they return NULL.
324  *        Otherwise, the next caller will pick up the list walk in
325  *        some indeterminate state.  (Otherwise every caller would
326  *        have to pass in a cookie to keep the state represented by
327  *        io_walk_link, which gets annoying.)
328  */
329 zio_t *
330 zio_walk_parents(zio_t *cio)
331 {
332         zio_link_t *zl = cio->io_walk_link;
333         list_t *pl = &cio->io_parent_list;
334
335         zl = (zl == NULL) ? list_head(pl) : list_next(pl, zl);
336         cio->io_walk_link = zl;
337
338         if (zl == NULL)
339                 return (NULL);
340
341         ASSERT(zl->zl_child == cio);
342         return (zl->zl_parent);
343 }
344
345 zio_t *
346 zio_walk_children(zio_t *pio)
347 {
348         zio_link_t *zl = pio->io_walk_link;
349         list_t *cl = &pio->io_child_list;
350
351         zl = (zl == NULL) ? list_head(cl) : list_next(cl, zl);
352         pio->io_walk_link = zl;
353
354         if (zl == NULL)
355                 return (NULL);
356
357         ASSERT(zl->zl_parent == pio);
358         return (zl->zl_child);
359 }
360
361 zio_t *
362 zio_unique_parent(zio_t *cio)
363 {
364         zio_t *pio = zio_walk_parents(cio);
365
366         VERIFY(zio_walk_parents(cio) == NULL);
367         return (pio);
368 }
369
370 void
371 zio_add_child(zio_t *pio, zio_t *cio)
372 {
373         zio_link_t *zl = kmem_cache_alloc(zio_link_cache, KM_SLEEP);
374
375         /*
376          * Logical I/Os can have logical, gang, or vdev children.
377          * Gang I/Os can have gang or vdev children.
378          * Vdev I/Os can only have vdev children.
379          * The following ASSERT captures all of these constraints.
380          */
381         ASSERT(cio->io_child_type <= pio->io_child_type);
382
383         zl->zl_parent = pio;
384         zl->zl_child = cio;
385
386         mutex_enter(&cio->io_lock);
387         mutex_enter(&pio->io_lock);
388
389         ASSERT(pio->io_state[ZIO_WAIT_DONE] == 0);
390
391         for (int w = 0; w < ZIO_WAIT_TYPES; w++)
392                 pio->io_children[cio->io_child_type][w] += !cio->io_state[w];
393
394         list_insert_head(&pio->io_child_list, zl);
395         list_insert_head(&cio->io_parent_list, zl);
396
397         mutex_exit(&pio->io_lock);
398         mutex_exit(&cio->io_lock);
399 }
400
401 static void
402 zio_remove_child(zio_t *pio, zio_t *cio, zio_link_t *zl)
403 {
404         ASSERT(zl->zl_parent == pio);
405         ASSERT(zl->zl_child == cio);
406
407         mutex_enter(&cio->io_lock);
408         mutex_enter(&pio->io_lock);
409
410         list_remove(&pio->io_child_list, zl);
411         list_remove(&cio->io_parent_list, zl);
412
413         mutex_exit(&pio->io_lock);
414         mutex_exit(&cio->io_lock);
415
416         kmem_cache_free(zio_link_cache, zl);
417 }
418
419 static boolean_t
420 zio_wait_for_children(zio_t *zio, enum zio_child child, enum zio_wait_type wait)
421 {
422         uint64_t *countp = &zio->io_children[child][wait];
423         boolean_t waiting = B_FALSE;
424
425         mutex_enter(&zio->io_lock);
426         ASSERT(zio->io_stall == NULL);
427         if (*countp != 0) {
428                 zio->io_stage--;
429                 zio->io_stall = countp;
430                 waiting = B_TRUE;
431         }
432         mutex_exit(&zio->io_lock);
433
434         return (waiting);
435 }
436
437 static void
438 zio_notify_parent(zio_t *pio, zio_t *zio, enum zio_wait_type wait)
439 {
440         uint64_t *countp = &pio->io_children[zio->io_child_type][wait];
441         int *errorp = &pio->io_child_error[zio->io_child_type];
442
443         mutex_enter(&pio->io_lock);
444         if (zio->io_error && !(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
445                 *errorp = zio_worst_error(*errorp, zio->io_error);
446         pio->io_reexecute |= zio->io_reexecute;
447         ASSERT3U(*countp, >, 0);
448         if (--*countp == 0 && pio->io_stall == countp) {
449                 pio->io_stall = NULL;
450                 mutex_exit(&pio->io_lock);
451                 zio_execute(pio);
452         } else {
453                 mutex_exit(&pio->io_lock);
454         }
455 }
456
457 static void
458 zio_inherit_child_errors(zio_t *zio, enum zio_child c)
459 {
460         if (zio->io_child_error[c] != 0 && zio->io_error == 0)
461                 zio->io_error = zio->io_child_error[c];
462 }
463
464 /*
465  * ==========================================================================
466  * Create the various types of I/O (read, write, free, etc)
467  * ==========================================================================
468  */
469 static zio_t *
470 zio_create(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
471     void *data, uint64_t size, zio_done_func_t *done, void *private,
472     zio_type_t type, int priority, int flags, vdev_t *vd, uint64_t offset,
473     const zbookmark_t *zb, uint8_t stage, uint32_t pipeline)
474 {
475         zio_t *zio;
476
477         ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
478         ASSERT(P2PHASE(size, SPA_MINBLOCKSIZE) == 0);
479         ASSERT(P2PHASE(offset, SPA_MINBLOCKSIZE) == 0);
480
481         ASSERT(!vd || spa_config_held(spa, SCL_STATE_ALL, RW_READER));
482         ASSERT(!bp || !(flags & ZIO_FLAG_CONFIG_WRITER));
483         ASSERT(vd || stage == ZIO_STAGE_OPEN);
484
485         zio = kmem_cache_alloc(zio_cache, KM_SLEEP);
486         bzero(zio, sizeof (zio_t));
487
488         mutex_init(&zio->io_lock, NULL, MUTEX_DEFAULT, NULL);
489         cv_init(&zio->io_cv, NULL, CV_DEFAULT, NULL);
490
491         list_create(&zio->io_parent_list, sizeof (zio_link_t),
492             offsetof(zio_link_t, zl_parent_node));
493         list_create(&zio->io_child_list, sizeof (zio_link_t),
494             offsetof(zio_link_t, zl_child_node));
495
496         if (vd != NULL)
497                 zio->io_child_type = ZIO_CHILD_VDEV;
498         else if (flags & ZIO_FLAG_GANG_CHILD)
499                 zio->io_child_type = ZIO_CHILD_GANG;
500         else
501                 zio->io_child_type = ZIO_CHILD_LOGICAL;
502
503         if (bp != NULL) {
504                 zio->io_bp = bp;
505                 zio->io_bp_copy = *bp;
506                 zio->io_bp_orig = *bp;
507                 if (type != ZIO_TYPE_WRITE)
508                         zio->io_bp = &zio->io_bp_copy;  /* so caller can free */
509                 if (zio->io_child_type == ZIO_CHILD_LOGICAL)
510                         zio->io_logical = zio;
511                 if (zio->io_child_type > ZIO_CHILD_GANG && BP_IS_GANG(bp))
512                         pipeline |= ZIO_GANG_STAGES;
513         }
514
515         zio->io_spa = spa;
516         zio->io_txg = txg;
517         zio->io_data = data;
518         zio->io_size = size;
519         zio->io_done = done;
520         zio->io_private = private;
521         zio->io_type = type;
522         zio->io_priority = priority;
523         zio->io_vd = vd;
524         zio->io_offset = offset;
525         zio->io_orig_flags = zio->io_flags = flags;
526         zio->io_orig_stage = zio->io_stage = stage;
527         zio->io_orig_pipeline = zio->io_pipeline = pipeline;
528
529         zio->io_state[ZIO_WAIT_READY] = (stage >= ZIO_STAGE_READY);
530         zio->io_state[ZIO_WAIT_DONE] = (stage >= ZIO_STAGE_DONE);
531
532         if (zb != NULL)
533                 zio->io_bookmark = *zb;
534
535         if (pio != NULL) {
536                 if (zio->io_logical == NULL)
537                         zio->io_logical = pio->io_logical;
538                 if (zio->io_child_type == ZIO_CHILD_GANG)
539                         zio->io_gang_leader = pio->io_gang_leader;
540                 zio_add_child(pio, zio);
541         }
542
543         return (zio);
544 }
545
546 static void
547 zio_destroy(zio_t *zio)
548 {
549         list_destroy(&zio->io_parent_list);
550         list_destroy(&zio->io_child_list);
551         mutex_destroy(&zio->io_lock);
552         cv_destroy(&zio->io_cv);
553         kmem_cache_free(zio_cache, zio);
554 }
555
556 zio_t *
557 zio_null(zio_t *pio, spa_t *spa, vdev_t *vd, zio_done_func_t *done,
558     void *private, int flags)
559 {
560         zio_t *zio;
561
562         zio = zio_create(pio, spa, 0, NULL, NULL, 0, done, private,
563             ZIO_TYPE_NULL, ZIO_PRIORITY_NOW, flags, vd, 0, NULL,
564             ZIO_STAGE_OPEN, ZIO_INTERLOCK_PIPELINE);
565
566         return (zio);
567 }
568
569 zio_t *
570 zio_root(spa_t *spa, zio_done_func_t *done, void *private, int flags)
571 {
572         return (zio_null(NULL, spa, NULL, done, private, flags));
573 }
574
575 zio_t *
576 zio_read(zio_t *pio, spa_t *spa, const blkptr_t *bp,
577     void *data, uint64_t size, zio_done_func_t *done, void *private,
578     int priority, int flags, const zbookmark_t *zb)
579 {
580         zio_t *zio;
581
582         zio = zio_create(pio, spa, bp->blk_birth, (blkptr_t *)bp,
583             data, size, done, private,
584             ZIO_TYPE_READ, priority, flags, NULL, 0, zb,
585             ZIO_STAGE_OPEN, ZIO_READ_PIPELINE);
586
587         return (zio);
588 }
589
590 zio_t *
591 zio_write(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
592     void *data, uint64_t size, zio_prop_t *zp,
593     zio_done_func_t *ready, zio_done_func_t *done, void *private,
594     int priority, int flags, const zbookmark_t *zb)
595 {
596         zio_t *zio;
597
598         ASSERT(zp->zp_checksum >= ZIO_CHECKSUM_OFF &&
599             zp->zp_checksum < ZIO_CHECKSUM_FUNCTIONS &&
600             zp->zp_compress >= ZIO_COMPRESS_OFF &&
601             zp->zp_compress < ZIO_COMPRESS_FUNCTIONS &&
602             zp->zp_type < DMU_OT_NUMTYPES &&
603             zp->zp_level < 32 &&
604             zp->zp_ndvas > 0 &&
605             zp->zp_ndvas <= spa_max_replication(spa));
606         ASSERT(ready != NULL);
607
608         zio = zio_create(pio, spa, txg, bp, data, size, done, private,
609             ZIO_TYPE_WRITE, priority, flags, NULL, 0, zb,
610             ZIO_STAGE_OPEN, ZIO_WRITE_PIPELINE);
611
612         zio->io_ready = ready;
613         zio->io_prop = *zp;
614
615         return (zio);
616 }
617
618 zio_t *
619 zio_rewrite(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, void *data,
620     uint64_t size, zio_done_func_t *done, void *private, int priority,
621     int flags, zbookmark_t *zb)
622 {
623         zio_t *zio;
624
625         zio = zio_create(pio, spa, txg, bp, data, size, done, private,
626             ZIO_TYPE_WRITE, priority, flags, NULL, 0, zb,
627             ZIO_STAGE_OPEN, ZIO_REWRITE_PIPELINE);
628
629         return (zio);
630 }
631
632 zio_t *
633 zio_free(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
634     zio_done_func_t *done, void *private, int flags)
635 {
636         zio_t *zio;
637
638         ASSERT(!BP_IS_HOLE(bp));
639
640         if (bp->blk_fill == BLK_FILL_ALREADY_FREED)
641                 return (zio_null(pio, spa, NULL, NULL, NULL, flags));
642
643         if (txg == spa->spa_syncing_txg &&
644             spa_sync_pass(spa) > SYNC_PASS_DEFERRED_FREE) {
645                 bplist_enqueue_deferred(&spa->spa_sync_bplist, bp);
646                 return (zio_null(pio, spa, NULL, NULL, NULL, flags));
647         }
648
649         zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp),
650             done, private, ZIO_TYPE_FREE, ZIO_PRIORITY_FREE, flags,
651             NULL, 0, NULL, ZIO_STAGE_OPEN, ZIO_FREE_PIPELINE);
652
653         return (zio);
654 }
655
656 zio_t *
657 zio_claim(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
658     zio_done_func_t *done, void *private, int flags)
659 {
660         zio_t *zio;
661
662         /*
663          * A claim is an allocation of a specific block.  Claims are needed
664          * to support immediate writes in the intent log.  The issue is that
665          * immediate writes contain committed data, but in a txg that was
666          * *not* committed.  Upon opening the pool after an unclean shutdown,
667          * the intent log claims all blocks that contain immediate write data
668          * so that the SPA knows they're in use.
669          *
670          * All claims *must* be resolved in the first txg -- before the SPA
671          * starts allocating blocks -- so that nothing is allocated twice.
672          */
673         ASSERT3U(spa->spa_uberblock.ub_rootbp.blk_birth, <, spa_first_txg(spa));
674         ASSERT3U(spa_first_txg(spa), <=, txg);
675
676         zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp),
677             done, private, ZIO_TYPE_CLAIM, ZIO_PRIORITY_NOW, flags,
678             NULL, 0, NULL, ZIO_STAGE_OPEN, ZIO_CLAIM_PIPELINE);
679
680         return (zio);
681 }
682
683 zio_t *
684 zio_ioctl(zio_t *pio, spa_t *spa, vdev_t *vd, int cmd,
685     zio_done_func_t *done, void *private, int priority, int flags)
686 {
687         zio_t *zio;
688         int c;
689
690         if (vd->vdev_children == 0) {
691                 zio = zio_create(pio, spa, 0, NULL, NULL, 0, done, private,
692                     ZIO_TYPE_IOCTL, priority, flags, vd, 0, NULL,
693                     ZIO_STAGE_OPEN, ZIO_IOCTL_PIPELINE);
694
695                 zio->io_cmd = cmd;
696         } else {
697                 zio = zio_null(pio, spa, NULL, NULL, NULL, flags);
698
699                 for (c = 0; c < vd->vdev_children; c++)
700                         zio_nowait(zio_ioctl(zio, spa, vd->vdev_child[c], cmd,
701                             done, private, priority, flags));
702         }
703
704         return (zio);
705 }
706
707 zio_t *
708 zio_read_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
709     void *data, int checksum, zio_done_func_t *done, void *private,
710     int priority, int flags, boolean_t labels)
711 {
712         zio_t *zio;
713
714         ASSERT(vd->vdev_children == 0);
715         ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE ||
716             offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
717         ASSERT3U(offset + size, <=, vd->vdev_psize);
718
719         zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, done, private,
720             ZIO_TYPE_READ, priority, flags, vd, offset, NULL,
721             ZIO_STAGE_OPEN, ZIO_READ_PHYS_PIPELINE);
722
723         zio->io_prop.zp_checksum = checksum;
724
725         return (zio);
726 }
727
728 zio_t *
729 zio_write_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
730     void *data, int checksum, zio_done_func_t *done, void *private,
731     int priority, int flags, boolean_t labels)
732 {
733         zio_t *zio;
734
735         ASSERT(vd->vdev_children == 0);
736         ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE ||
737             offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
738         ASSERT3U(offset + size, <=, vd->vdev_psize);
739
740         zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, done, private,
741             ZIO_TYPE_WRITE, priority, flags, vd, offset, NULL,
742             ZIO_STAGE_OPEN, ZIO_WRITE_PHYS_PIPELINE);
743
744         zio->io_prop.zp_checksum = checksum;
745
746         if (zio_checksum_table[checksum].ci_zbt) {
747                 /*
748                  * zbt checksums are necessarily destructive -- they modify
749                  * the end of the write buffer to hold the verifier/checksum.
750                  * Therefore, we must make a local copy in case the data is
751                  * being written to multiple places in parallel.
752                  */
753                 void *wbuf = zio_buf_alloc(size);
754                 bcopy(data, wbuf, size);
755                 zio_push_transform(zio, wbuf, size, size, NULL);
756         }
757
758         return (zio);
759 }
760
761 /*
762  * Create a child I/O to do some work for us.
763  */
764 zio_t *
765 zio_vdev_child_io(zio_t *pio, blkptr_t *bp, vdev_t *vd, uint64_t offset,
766         void *data, uint64_t size, int type, int priority, int flags,
767         zio_done_func_t *done, void *private)
768 {
769         uint32_t pipeline = ZIO_VDEV_CHILD_PIPELINE;
770         zio_t *zio;
771
772         ASSERT(vd->vdev_parent ==
773             (pio->io_vd ? pio->io_vd : pio->io_spa->spa_root_vdev));
774
775         if (type == ZIO_TYPE_READ && bp != NULL) {
776                 /*
777                  * If we have the bp, then the child should perform the
778                  * checksum and the parent need not.  This pushes error
779                  * detection as close to the leaves as possible and
780                  * eliminates redundant checksums in the interior nodes.
781                  */
782                 pipeline |= 1U << ZIO_STAGE_CHECKSUM_VERIFY;
783                 pio->io_pipeline &= ~(1U << ZIO_STAGE_CHECKSUM_VERIFY);
784         }
785
786         if (vd->vdev_children == 0)
787                 offset += VDEV_LABEL_START_SIZE;
788
789         zio = zio_create(pio, pio->io_spa, pio->io_txg, bp, data, size,
790             done, private, type, priority,
791             (pio->io_flags & ZIO_FLAG_VDEV_INHERIT) |
792             ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE | flags,
793             vd, offset, &pio->io_bookmark,
794             ZIO_STAGE_VDEV_IO_START - 1, pipeline);
795
796         return (zio);
797 }
798
799 zio_t *
800 zio_vdev_delegated_io(vdev_t *vd, uint64_t offset, void *data, uint64_t size,
801         int type, int priority, int flags, zio_done_func_t *done, void *private)
802 {
803         zio_t *zio;
804
805         ASSERT(vd->vdev_ops->vdev_op_leaf);
806
807         zio = zio_create(NULL, vd->vdev_spa, 0, NULL,
808             data, size, done, private, type, priority,
809             flags | ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY,
810             vd, offset, NULL,
811             ZIO_STAGE_VDEV_IO_START - 1, ZIO_VDEV_CHILD_PIPELINE);
812
813         return (zio);
814 }
815
816 void
817 zio_flush(zio_t *zio, vdev_t *vd)
818 {
819         zio_nowait(zio_ioctl(zio, zio->io_spa, vd, DKIOCFLUSHWRITECACHE,
820             NULL, NULL, ZIO_PRIORITY_NOW,
821             ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY));
822 }
823
824 /*
825  * ==========================================================================
826  * Prepare to read and write logical blocks
827  * ==========================================================================
828  */
829
830 static int
831 zio_read_bp_init(zio_t *zio)
832 {
833         blkptr_t *bp = zio->io_bp;
834
835         if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF &&
836             zio->io_child_type == ZIO_CHILD_LOGICAL &&
837             !(zio->io_flags & ZIO_FLAG_RAW)) {
838                 uint64_t csize = BP_GET_PSIZE(bp);
839                 void *cbuf = zio_buf_alloc(csize);
840
841                 zio_push_transform(zio, cbuf, csize, csize, zio_decompress);
842         }
843
844         if (!dmu_ot[BP_GET_TYPE(bp)].ot_metadata && BP_GET_LEVEL(bp) == 0)
845                 zio->io_flags |= ZIO_FLAG_DONT_CACHE;
846
847         return (ZIO_PIPELINE_CONTINUE);
848 }
849
850 static int
851 zio_write_bp_init(zio_t *zio)
852 {
853         zio_prop_t *zp = &zio->io_prop;
854         int compress = zp->zp_compress;
855         blkptr_t *bp = zio->io_bp;
856         void *cbuf;
857         uint64_t lsize = zio->io_size;
858         uint64_t csize = lsize;
859         uint64_t cbufsize = 0;
860         int pass = 1;
861
862         /*
863          * If our children haven't all reached the ready stage,
864          * wait for them and then repeat this pipeline stage.
865          */
866         if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_READY) ||
867             zio_wait_for_children(zio, ZIO_CHILD_LOGICAL, ZIO_WAIT_READY))
868                 return (ZIO_PIPELINE_STOP);
869
870         if (!IO_IS_ALLOCATING(zio))
871                 return (ZIO_PIPELINE_CONTINUE);
872
873         ASSERT(compress != ZIO_COMPRESS_INHERIT);
874
875         if (bp->blk_birth == zio->io_txg) {
876                 /*
877                  * We're rewriting an existing block, which means we're
878                  * working on behalf of spa_sync().  For spa_sync() to
879                  * converge, it must eventually be the case that we don't
880                  * have to allocate new blocks.  But compression changes
881                  * the blocksize, which forces a reallocate, and makes
882                  * convergence take longer.  Therefore, after the first
883                  * few passes, stop compressing to ensure convergence.
884                  */
885                 pass = spa_sync_pass(zio->io_spa);
886
887                 if (pass > SYNC_PASS_DONT_COMPRESS)
888                         compress = ZIO_COMPRESS_OFF;
889
890                 /* Make sure someone doesn't change their mind on overwrites */
891                 ASSERT(MIN(zp->zp_ndvas + BP_IS_GANG(bp),
892                     spa_max_replication(zio->io_spa)) == BP_GET_NDVAS(bp));
893         }
894
895         if (compress != ZIO_COMPRESS_OFF) {
896                 if (!zio_compress_data(compress, zio->io_data, zio->io_size,
897                     &cbuf, &csize, &cbufsize)) {
898                         compress = ZIO_COMPRESS_OFF;
899                 } else if (csize != 0) {
900                         zio_push_transform(zio, cbuf, csize, cbufsize, NULL);
901                 }
902         }
903
904         /*
905          * The final pass of spa_sync() must be all rewrites, but the first
906          * few passes offer a trade-off: allocating blocks defers convergence,
907          * but newly allocated blocks are sequential, so they can be written
908          * to disk faster.  Therefore, we allow the first few passes of
909          * spa_sync() to allocate new blocks, but force rewrites after that.
910          * There should only be a handful of blocks after pass 1 in any case.
911          */
912         if (bp->blk_birth == zio->io_txg && BP_GET_PSIZE(bp) == csize &&
913             pass > SYNC_PASS_REWRITE) {
914                 ASSERT(csize != 0);
915                 uint32_t gang_stages = zio->io_pipeline & ZIO_GANG_STAGES;
916                 zio->io_pipeline = ZIO_REWRITE_PIPELINE | gang_stages;
917                 zio->io_flags |= ZIO_FLAG_IO_REWRITE;
918         } else {
919                 BP_ZERO(bp);
920                 zio->io_pipeline = ZIO_WRITE_PIPELINE;
921         }
922
923         if (csize == 0) {
924                 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
925         } else {
926                 ASSERT(zp->zp_checksum != ZIO_CHECKSUM_GANG_HEADER);
927                 BP_SET_LSIZE(bp, lsize);
928                 BP_SET_PSIZE(bp, csize);
929                 BP_SET_COMPRESS(bp, compress);
930                 BP_SET_CHECKSUM(bp, zp->zp_checksum);
931                 BP_SET_TYPE(bp, zp->zp_type);
932                 BP_SET_LEVEL(bp, zp->zp_level);
933                 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
934         }
935
936         return (ZIO_PIPELINE_CONTINUE);
937 }
938
939 /*
940  * ==========================================================================
941  * Execute the I/O pipeline
942  * ==========================================================================
943  */
944
945 static void
946 zio_taskq_dispatch(zio_t *zio, enum zio_taskq_type q)
947 {
948         spa_t *spa = zio->io_spa;
949         zio_type_t t = zio->io_type;
950
951         /*
952          * If we're a config writer or a probe, the normal issue and
953          * interrupt threads may all be blocked waiting for the config lock.
954          * In this case, select the otherwise-unused taskq for ZIO_TYPE_NULL.
955          */
956         if (zio->io_flags & (ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_PROBE))
957                 t = ZIO_TYPE_NULL;
958
959         /*
960          * A similar issue exists for the L2ARC write thread until L2ARC 2.0.
961          */
962         if (t == ZIO_TYPE_WRITE && zio->io_vd && zio->io_vd->vdev_aux)
963                 t = ZIO_TYPE_NULL;
964
965         /*
966          * If this is a high priority I/O, then use the high priority taskq.
967          */
968         if (zio->io_priority == ZIO_PRIORITY_NOW &&
969             spa->spa_zio_taskq[t][q + 1] != NULL)
970                 q++;
971
972         ASSERT3U(q, <, ZIO_TASKQ_TYPES);
973         (void) taskq_dispatch_safe(spa->spa_zio_taskq[t][q],
974             (task_func_t *)zio_execute, zio, &zio->io_task);
975 }
976
977 static boolean_t
978 zio_taskq_member(zio_t *zio, enum zio_taskq_type q)
979 {
980         kthread_t *executor = zio->io_executor;
981         spa_t *spa = zio->io_spa;
982
983         for (zio_type_t t = 0; t < ZIO_TYPES; t++)
984                 if (taskq_member(spa->spa_zio_taskq[t][q], executor))
985                         return (B_TRUE);
986
987         return (B_FALSE);
988 }
989
990 static int
991 zio_issue_async(zio_t *zio)
992 {
993         zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE);
994
995         return (ZIO_PIPELINE_STOP);
996 }
997
998 void
999 zio_interrupt(zio_t *zio)
1000 {
1001         zio_taskq_dispatch(zio, ZIO_TASKQ_INTERRUPT);
1002 }
1003
1004 /*
1005  * Execute the I/O pipeline until one of the following occurs:
1006  * (1) the I/O completes; (2) the pipeline stalls waiting for
1007  * dependent child I/Os; (3) the I/O issues, so we're waiting
1008  * for an I/O completion interrupt; (4) the I/O is delegated by
1009  * vdev-level caching or aggregation; (5) the I/O is deferred
1010  * due to vdev-level queueing; (6) the I/O is handed off to
1011  * another thread.  In all cases, the pipeline stops whenever
1012  * there's no CPU work; it never burns a thread in cv_wait().
1013  *
1014  * There's no locking on io_stage because there's no legitimate way
1015  * for multiple threads to be attempting to process the same I/O.
1016  */
1017 static zio_pipe_stage_t *zio_pipeline[ZIO_STAGES];
1018
1019 void
1020 zio_execute(zio_t *zio)
1021 {
1022         zio->io_executor = curthread;
1023
1024         while (zio->io_stage < ZIO_STAGE_DONE) {
1025                 uint32_t pipeline = zio->io_pipeline;
1026                 zio_stage_t stage = zio->io_stage;
1027                 int rv;
1028
1029                 ASSERT(!MUTEX_HELD(&zio->io_lock));
1030
1031                 while (((1U << ++stage) & pipeline) == 0)
1032                         continue;
1033
1034                 ASSERT(stage <= ZIO_STAGE_DONE);
1035                 ASSERT(zio->io_stall == NULL);
1036
1037                 /*
1038                  * If we are in interrupt context and this pipeline stage
1039                  * will grab a config lock that is held across I/O,
1040                  * issue async to avoid deadlock.
1041                  */
1042                 if (((1U << stage) & ZIO_CONFIG_LOCK_BLOCKING_STAGES) &&
1043                     zio->io_vd == NULL &&
1044                     zio_taskq_member(zio, ZIO_TASKQ_INTERRUPT)) {
1045                         zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE);
1046                         return;
1047                 }
1048
1049                 zio->io_stage = stage;
1050                 rv = zio_pipeline[stage](zio);
1051
1052                 if (rv == ZIO_PIPELINE_STOP)
1053                         return;
1054
1055                 ASSERT(rv == ZIO_PIPELINE_CONTINUE);
1056         }
1057 }
1058
1059 /*
1060  * ==========================================================================
1061  * Initiate I/O, either sync or async
1062  * ==========================================================================
1063  */
1064 int
1065 zio_wait(zio_t *zio)
1066 {
1067         int error;
1068
1069         ASSERT(zio->io_stage == ZIO_STAGE_OPEN);
1070         ASSERT(zio->io_executor == NULL);
1071
1072         zio->io_waiter = curthread;
1073
1074         zio_execute(zio);
1075
1076         mutex_enter(&zio->io_lock);
1077         while (zio->io_executor != NULL)
1078                 cv_wait(&zio->io_cv, &zio->io_lock);
1079         mutex_exit(&zio->io_lock);
1080
1081         error = zio->io_error;
1082         zio_destroy(zio);
1083
1084         return (error);
1085 }
1086
1087 void
1088 zio_nowait(zio_t *zio)
1089 {
1090         ASSERT(zio->io_executor == NULL);
1091
1092         if (zio->io_child_type == ZIO_CHILD_LOGICAL &&
1093             zio_unique_parent(zio) == NULL) {
1094                 /*
1095                  * This is a logical async I/O with no parent to wait for it.
1096                  * We add it to the spa_async_root_zio "Godfather" I/O which
1097                  * will ensure they complete prior to unloading the pool.
1098                  */
1099                 spa_t *spa = zio->io_spa;
1100
1101                 zio_add_child(spa->spa_async_zio_root, zio);
1102         }
1103
1104         zio_execute(zio);
1105 }
1106
1107 /*
1108  * ==========================================================================
1109  * Reexecute or suspend/resume failed I/O
1110  * ==========================================================================
1111  */
1112
1113 static void
1114 zio_reexecute(zio_t *pio)
1115 {
1116         zio_t *cio, *cio_next;
1117
1118         ASSERT(pio->io_child_type == ZIO_CHILD_LOGICAL);
1119         ASSERT(pio->io_orig_stage == ZIO_STAGE_OPEN);
1120         ASSERT(pio->io_gang_leader == NULL);
1121         ASSERT(pio->io_gang_tree == NULL);
1122
1123         pio->io_flags = pio->io_orig_flags;
1124         pio->io_stage = pio->io_orig_stage;
1125         pio->io_pipeline = pio->io_orig_pipeline;
1126         pio->io_reexecute = 0;
1127         pio->io_error = 0;
1128         for (int w = 0; w < ZIO_WAIT_TYPES; w++)
1129                 pio->io_state[w] = 0;
1130         for (int c = 0; c < ZIO_CHILD_TYPES; c++)
1131                 pio->io_child_error[c] = 0;
1132
1133         if (IO_IS_ALLOCATING(pio)) {
1134                 /*
1135                  * Remember the failed bp so that the io_ready() callback
1136                  * can update its accounting upon reexecution.  The block
1137                  * was already freed in zio_done(); we indicate this with
1138                  * a fill count of -1 so that zio_free() knows to skip it.
1139                  */
1140                 blkptr_t *bp = pio->io_bp;
1141                 ASSERT(bp->blk_birth == 0 || bp->blk_birth == pio->io_txg);
1142                 bp->blk_fill = BLK_FILL_ALREADY_FREED;
1143                 pio->io_bp_orig = *bp;
1144                 BP_ZERO(bp);
1145         }
1146
1147         /*
1148          * As we reexecute pio's children, new children could be created.
1149          * New children go to the head of pio's io_child_list, however,
1150          * so we will (correctly) not reexecute them.  The key is that
1151          * the remainder of pio's io_child_list, from 'cio_next' onward,
1152          * cannot be affected by any side effects of reexecuting 'cio'.
1153          */
1154         for (cio = zio_walk_children(pio); cio != NULL; cio = cio_next) {
1155                 cio_next = zio_walk_children(pio);
1156                 mutex_enter(&pio->io_lock);
1157                 for (int w = 0; w < ZIO_WAIT_TYPES; w++)
1158                         pio->io_children[cio->io_child_type][w]++;
1159                 mutex_exit(&pio->io_lock);
1160                 zio_reexecute(cio);
1161         }
1162
1163         /*
1164          * Now that all children have been reexecuted, execute the parent.
1165          * We don't reexecute "The Godfather" I/O here as it's the
1166          * responsibility of the caller to wait on him.
1167          */
1168         if (!(pio->io_flags & ZIO_FLAG_GODFATHER))
1169                 zio_execute(pio);
1170 }
1171
1172 void
1173 zio_suspend(spa_t *spa, zio_t *zio)
1174 {
1175         if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_PANIC)
1176                 fm_panic("Pool '%s' has encountered an uncorrectable I/O "
1177                     "failure and the failure mode property for this pool "
1178                     "is set to panic.", spa_name(spa));
1179
1180         zfs_ereport_post(FM_EREPORT_ZFS_IO_FAILURE, spa, NULL, NULL, 0, 0);
1181
1182         mutex_enter(&spa->spa_suspend_lock);
1183
1184         if (spa->spa_suspend_zio_root == NULL)
1185                 spa->spa_suspend_zio_root = zio_root(spa, NULL, NULL,
1186                     ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
1187                     ZIO_FLAG_GODFATHER);
1188
1189         spa->spa_suspended = B_TRUE;
1190
1191         if (zio != NULL) {
1192                 ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER));
1193                 ASSERT(zio != spa->spa_suspend_zio_root);
1194                 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1195                 ASSERT(zio_unique_parent(zio) == NULL);
1196                 ASSERT(zio->io_stage == ZIO_STAGE_DONE);
1197                 zio_add_child(spa->spa_suspend_zio_root, zio);
1198         }
1199
1200         mutex_exit(&spa->spa_suspend_lock);
1201 }
1202
1203 int
1204 zio_resume(spa_t *spa)
1205 {
1206         zio_t *pio;
1207
1208         /*
1209          * Reexecute all previously suspended i/o.
1210          */
1211         mutex_enter(&spa->spa_suspend_lock);
1212         spa->spa_suspended = B_FALSE;
1213         cv_broadcast(&spa->spa_suspend_cv);
1214         pio = spa->spa_suspend_zio_root;
1215         spa->spa_suspend_zio_root = NULL;
1216         mutex_exit(&spa->spa_suspend_lock);
1217
1218         if (pio == NULL)
1219                 return (0);
1220
1221         zio_reexecute(pio);
1222         return (zio_wait(pio));
1223 }
1224
1225 void
1226 zio_resume_wait(spa_t *spa)
1227 {
1228         mutex_enter(&spa->spa_suspend_lock);
1229         while (spa_suspended(spa))
1230                 cv_wait(&spa->spa_suspend_cv, &spa->spa_suspend_lock);
1231         mutex_exit(&spa->spa_suspend_lock);
1232 }
1233
1234 /*
1235  * ==========================================================================
1236  * Gang blocks.
1237  *
1238  * A gang block is a collection of small blocks that looks to the DMU
1239  * like one large block.  When zio_dva_allocate() cannot find a block
1240  * of the requested size, due to either severe fragmentation or the pool
1241  * being nearly full, it calls zio_write_gang_block() to construct the
1242  * block from smaller fragments.
1243  *
1244  * A gang block consists of a gang header (zio_gbh_phys_t) and up to
1245  * three (SPA_GBH_NBLKPTRS) gang members.  The gang header is just like
1246  * an indirect block: it's an array of block pointers.  It consumes
1247  * only one sector and hence is allocatable regardless of fragmentation.
1248  * The gang header's bps point to its gang members, which hold the data.
1249  *
1250  * Gang blocks are self-checksumming, using the bp's <vdev, offset, txg>
1251  * as the verifier to ensure uniqueness of the SHA256 checksum.
1252  * Critically, the gang block bp's blk_cksum is the checksum of the data,
1253  * not the gang header.  This ensures that data block signatures (needed for
1254  * deduplication) are independent of how the block is physically stored.
1255  *
1256  * Gang blocks can be nested: a gang member may itself be a gang block.
1257  * Thus every gang block is a tree in which root and all interior nodes are
1258  * gang headers, and the leaves are normal blocks that contain user data.
1259  * The root of the gang tree is called the gang leader.
1260  *
1261  * To perform any operation (read, rewrite, free, claim) on a gang block,
1262  * zio_gang_assemble() first assembles the gang tree (minus data leaves)
1263  * in the io_gang_tree field of the original logical i/o by recursively
1264  * reading the gang leader and all gang headers below it.  This yields
1265  * an in-core tree containing the contents of every gang header and the
1266  * bps for every constituent of the gang block.
1267  *
1268  * With the gang tree now assembled, zio_gang_issue() just walks the gang tree
1269  * and invokes a callback on each bp.  To free a gang block, zio_gang_issue()
1270  * calls zio_free_gang() -- a trivial wrapper around zio_free() -- for each bp.
1271  * zio_claim_gang() provides a similarly trivial wrapper for zio_claim().
1272  * zio_read_gang() is a wrapper around zio_read() that omits reading gang
1273  * headers, since we already have those in io_gang_tree.  zio_rewrite_gang()
1274  * performs a zio_rewrite() of the data or, for gang headers, a zio_rewrite()
1275  * of the gang header plus zio_checksum_compute() of the data to update the
1276  * gang header's blk_cksum as described above.
1277  *
1278  * The two-phase assemble/issue model solves the problem of partial failure --
1279  * what if you'd freed part of a gang block but then couldn't read the
1280  * gang header for another part?  Assembling the entire gang tree first
1281  * ensures that all the necessary gang header I/O has succeeded before
1282  * starting the actual work of free, claim, or write.  Once the gang tree
1283  * is assembled, free and claim are in-memory operations that cannot fail.
1284  *
1285  * In the event that a gang write fails, zio_dva_unallocate() walks the
1286  * gang tree to immediately free (i.e. insert back into the space map)
1287  * everything we've allocated.  This ensures that we don't get ENOSPC
1288  * errors during repeated suspend/resume cycles due to a flaky device.
1289  *
1290  * Gang rewrites only happen during sync-to-convergence.  If we can't assemble
1291  * the gang tree, we won't modify the block, so we can safely defer the free
1292  * (knowing that the block is still intact).  If we *can* assemble the gang
1293  * tree, then even if some of the rewrites fail, zio_dva_unallocate() will free
1294  * each constituent bp and we can allocate a new block on the next sync pass.
1295  *
1296  * In all cases, the gang tree allows complete recovery from partial failure.
1297  * ==========================================================================
1298  */
1299
1300 static zio_t *
1301 zio_read_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
1302 {
1303         if (gn != NULL)
1304                 return (pio);
1305
1306         return (zio_read(pio, pio->io_spa, bp, data, BP_GET_PSIZE(bp),
1307             NULL, NULL, pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio),
1308             &pio->io_bookmark));
1309 }
1310
1311 zio_t *
1312 zio_rewrite_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
1313 {
1314         zio_t *zio;
1315
1316         if (gn != NULL) {
1317                 zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp,
1318                     gn->gn_gbh, SPA_GANGBLOCKSIZE, NULL, NULL, pio->io_priority,
1319                     ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
1320                 /*
1321                  * As we rewrite each gang header, the pipeline will compute
1322                  * a new gang block header checksum for it; but no one will
1323                  * compute a new data checksum, so we do that here.  The one
1324                  * exception is the gang leader: the pipeline already computed
1325                  * its data checksum because that stage precedes gang assembly.
1326                  * (Presently, nothing actually uses interior data checksums;
1327                  * this is just good hygiene.)
1328                  */
1329                 if (gn != pio->io_gang_leader->io_gang_tree) {
1330                         zio_checksum_compute(zio, BP_GET_CHECKSUM(bp),
1331                             data, BP_GET_PSIZE(bp));
1332                 }
1333         } else {
1334                 zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp,
1335                     data, BP_GET_PSIZE(bp), NULL, NULL, pio->io_priority,
1336                     ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
1337         }
1338
1339         return (zio);
1340 }
1341
1342 /* ARGSUSED */
1343 zio_t *
1344 zio_free_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
1345 {
1346         return (zio_free(pio, pio->io_spa, pio->io_txg, bp,
1347             NULL, NULL, ZIO_GANG_CHILD_FLAGS(pio)));
1348 }
1349
1350 /* ARGSUSED */
1351 zio_t *
1352 zio_claim_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
1353 {
1354         return (zio_claim(pio, pio->io_spa, pio->io_txg, bp,
1355             NULL, NULL, ZIO_GANG_CHILD_FLAGS(pio)));
1356 }
1357
1358 static zio_gang_issue_func_t *zio_gang_issue_func[ZIO_TYPES] = {
1359         NULL,
1360         zio_read_gang,
1361         zio_rewrite_gang,
1362         zio_free_gang,
1363         zio_claim_gang,
1364         NULL
1365 };
1366
1367 static void zio_gang_tree_assemble_done(zio_t *zio);
1368
1369 static zio_gang_node_t *
1370 zio_gang_node_alloc(zio_gang_node_t **gnpp)
1371 {
1372         zio_gang_node_t *gn;
1373
1374         ASSERT(*gnpp == NULL);
1375
1376         gn = kmem_zalloc(sizeof (*gn), KM_SLEEP);
1377         gn->gn_gbh = zio_buf_alloc(SPA_GANGBLOCKSIZE);
1378         *gnpp = gn;
1379
1380         return (gn);
1381 }
1382
1383 static void
1384 zio_gang_node_free(zio_gang_node_t **gnpp)
1385 {
1386         zio_gang_node_t *gn = *gnpp;
1387
1388         for (int g = 0; g < SPA_GBH_NBLKPTRS; g++)
1389                 ASSERT(gn->gn_child[g] == NULL);
1390
1391         zio_buf_free(gn->gn_gbh, SPA_GANGBLOCKSIZE);
1392         kmem_free(gn, sizeof (*gn));
1393         *gnpp = NULL;
1394 }
1395
1396 static void
1397 zio_gang_tree_free(zio_gang_node_t **gnpp)
1398 {
1399         zio_gang_node_t *gn = *gnpp;
1400
1401         if (gn == NULL)
1402                 return;
1403
1404         for (int g = 0; g < SPA_GBH_NBLKPTRS; g++)
1405                 zio_gang_tree_free(&gn->gn_child[g]);
1406
1407         zio_gang_node_free(gnpp);
1408 }
1409
1410 static void
1411 zio_gang_tree_assemble(zio_t *gio, blkptr_t *bp, zio_gang_node_t **gnpp)
1412 {
1413         zio_gang_node_t *gn = zio_gang_node_alloc(gnpp);
1414
1415         ASSERT(gio->io_gang_leader == gio);
1416         ASSERT(BP_IS_GANG(bp));
1417
1418         zio_nowait(zio_read(gio, gio->io_spa, bp, gn->gn_gbh,
1419             SPA_GANGBLOCKSIZE, zio_gang_tree_assemble_done, gn,
1420             gio->io_priority, ZIO_GANG_CHILD_FLAGS(gio), &gio->io_bookmark));
1421 }
1422
1423 static void
1424 zio_gang_tree_assemble_done(zio_t *zio)
1425 {
1426         zio_t *gio = zio->io_gang_leader;
1427         zio_gang_node_t *gn = zio->io_private;
1428         blkptr_t *bp = zio->io_bp;
1429
1430         ASSERT(gio == zio_unique_parent(zio));
1431         ASSERT(zio_walk_children(zio) == NULL);
1432
1433         if (zio->io_error)
1434                 return;
1435
1436         if (BP_SHOULD_BYTESWAP(bp))
1437                 byteswap_uint64_array(zio->io_data, zio->io_size);
1438
1439         ASSERT(zio->io_data == gn->gn_gbh);
1440         ASSERT(zio->io_size == SPA_GANGBLOCKSIZE);
1441         ASSERT(gn->gn_gbh->zg_tail.zbt_magic == ZBT_MAGIC);
1442
1443         for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
1444                 blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g];
1445                 if (!BP_IS_GANG(gbp))
1446                         continue;
1447                 zio_gang_tree_assemble(gio, gbp, &gn->gn_child[g]);
1448         }
1449 }
1450
1451 static void
1452 zio_gang_tree_issue(zio_t *pio, zio_gang_node_t *gn, blkptr_t *bp, void *data)
1453 {
1454         zio_t *gio = pio->io_gang_leader;
1455         zio_t *zio;
1456
1457         ASSERT(BP_IS_GANG(bp) == !!gn);
1458         ASSERT(BP_GET_CHECKSUM(bp) == BP_GET_CHECKSUM(gio->io_bp));
1459         ASSERT(BP_GET_LSIZE(bp) == BP_GET_PSIZE(bp) || gn == gio->io_gang_tree);
1460
1461         /*
1462          * If you're a gang header, your data is in gn->gn_gbh.
1463          * If you're a gang member, your data is in 'data' and gn == NULL.
1464          */
1465         zio = zio_gang_issue_func[gio->io_type](pio, bp, gn, data);
1466
1467         if (gn != NULL) {
1468                 ASSERT(gn->gn_gbh->zg_tail.zbt_magic == ZBT_MAGIC);
1469
1470                 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
1471                         blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g];
1472                         if (BP_IS_HOLE(gbp))
1473                                 continue;
1474                         zio_gang_tree_issue(zio, gn->gn_child[g], gbp, data);
1475                         data = (char *)data + BP_GET_PSIZE(gbp);
1476                 }
1477         }
1478
1479         if (gn == gio->io_gang_tree)
1480                 ASSERT3P((char *)gio->io_data + gio->io_size, ==, data);
1481
1482         if (zio != pio)
1483                 zio_nowait(zio);
1484 }
1485
1486 static int
1487 zio_gang_assemble(zio_t *zio)
1488 {
1489         blkptr_t *bp = zio->io_bp;
1490
1491         ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == NULL);
1492         ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
1493
1494         zio->io_gang_leader = zio;
1495
1496         zio_gang_tree_assemble(zio, bp, &zio->io_gang_tree);
1497
1498         return (ZIO_PIPELINE_CONTINUE);
1499 }
1500
1501 static int
1502 zio_gang_issue(zio_t *zio)
1503 {
1504         blkptr_t *bp = zio->io_bp;
1505
1506         if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_DONE))
1507                 return (ZIO_PIPELINE_STOP);
1508
1509         ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == zio);
1510         ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
1511
1512         if (zio->io_child_error[ZIO_CHILD_GANG] == 0)
1513                 zio_gang_tree_issue(zio, zio->io_gang_tree, bp, zio->io_data);
1514         else
1515                 zio_gang_tree_free(&zio->io_gang_tree);
1516
1517         zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1518
1519         return (ZIO_PIPELINE_CONTINUE);
1520 }
1521
1522 static void
1523 zio_write_gang_member_ready(zio_t *zio)
1524 {
1525         zio_t *pio = zio_unique_parent(zio);
1526         zio_t *gio = zio->io_gang_leader;
1527         dva_t *cdva = zio->io_bp->blk_dva;
1528         dva_t *pdva = pio->io_bp->blk_dva;
1529         uint64_t asize;
1530
1531         if (BP_IS_HOLE(zio->io_bp))
1532                 return;
1533
1534         ASSERT(BP_IS_HOLE(&zio->io_bp_orig));
1535
1536         ASSERT(zio->io_child_type == ZIO_CHILD_GANG);
1537         ASSERT3U(zio->io_prop.zp_ndvas, ==, gio->io_prop.zp_ndvas);
1538         ASSERT3U(zio->io_prop.zp_ndvas, <=, BP_GET_NDVAS(zio->io_bp));
1539         ASSERT3U(pio->io_prop.zp_ndvas, <=, BP_GET_NDVAS(pio->io_bp));
1540         ASSERT3U(BP_GET_NDVAS(zio->io_bp), <=, BP_GET_NDVAS(pio->io_bp));
1541
1542         mutex_enter(&pio->io_lock);
1543         for (int d = 0; d < BP_GET_NDVAS(zio->io_bp); d++) {
1544                 ASSERT(DVA_GET_GANG(&pdva[d]));
1545                 asize = DVA_GET_ASIZE(&pdva[d]);
1546                 asize += DVA_GET_ASIZE(&cdva[d]);
1547                 DVA_SET_ASIZE(&pdva[d], asize);
1548         }
1549         mutex_exit(&pio->io_lock);
1550 }
1551
1552 static int
1553 zio_write_gang_block(zio_t *pio)
1554 {
1555         spa_t *spa = pio->io_spa;
1556         blkptr_t *bp = pio->io_bp;
1557         zio_t *gio = pio->io_gang_leader;
1558         zio_t *zio;
1559         zio_gang_node_t *gn, **gnpp;
1560         zio_gbh_phys_t *gbh;
1561         uint64_t txg = pio->io_txg;
1562         uint64_t resid = pio->io_size;
1563         uint64_t lsize;
1564         int ndvas = gio->io_prop.zp_ndvas;
1565         int gbh_ndvas = MIN(ndvas + 1, spa_max_replication(spa));
1566         zio_prop_t zp;
1567         int error;
1568
1569         error = metaslab_alloc(spa, spa->spa_normal_class, SPA_GANGBLOCKSIZE,
1570             bp, gbh_ndvas, txg, pio == gio ? NULL : gio->io_bp,
1571             METASLAB_HINTBP_FAVOR | METASLAB_GANG_HEADER);
1572         if (error) {
1573                 pio->io_error = error;
1574                 return (ZIO_PIPELINE_CONTINUE);
1575         }
1576
1577         if (pio == gio) {
1578                 gnpp = &gio->io_gang_tree;
1579         } else {
1580                 gnpp = pio->io_private;
1581                 ASSERT(pio->io_ready == zio_write_gang_member_ready);
1582         }
1583
1584         gn = zio_gang_node_alloc(gnpp);
1585         gbh = gn->gn_gbh;
1586         bzero(gbh, SPA_GANGBLOCKSIZE);
1587
1588         /*
1589          * Create the gang header.
1590          */
1591         zio = zio_rewrite(pio, spa, txg, bp, gbh, SPA_GANGBLOCKSIZE, NULL, NULL,
1592             pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
1593
1594         /*
1595          * Create and nowait the gang children.
1596          */
1597         for (int g = 0; resid != 0; resid -= lsize, g++) {
1598                 lsize = P2ROUNDUP(resid / (SPA_GBH_NBLKPTRS - g),
1599                     SPA_MINBLOCKSIZE);
1600                 ASSERT(lsize >= SPA_MINBLOCKSIZE && lsize <= resid);
1601
1602                 zp.zp_checksum = gio->io_prop.zp_checksum;
1603                 zp.zp_compress = ZIO_COMPRESS_OFF;
1604                 zp.zp_type = DMU_OT_NONE;
1605                 zp.zp_level = 0;
1606                 zp.zp_ndvas = gio->io_prop.zp_ndvas;
1607
1608                 zio_nowait(zio_write(zio, spa, txg, &gbh->zg_blkptr[g],
1609                     (char *)pio->io_data + (pio->io_size - resid), lsize, &zp,
1610                     zio_write_gang_member_ready, NULL, &gn->gn_child[g],
1611                     pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio),
1612                     &pio->io_bookmark));
1613         }
1614
1615         /*
1616          * Set pio's pipeline to just wait for zio to finish.
1617          */
1618         pio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1619
1620         zio_nowait(zio);
1621
1622         return (ZIO_PIPELINE_CONTINUE);
1623 }
1624
1625 /*
1626  * ==========================================================================
1627  * Allocate and free blocks
1628  * ==========================================================================
1629  */
1630
1631 static int
1632 zio_dva_allocate(zio_t *zio)
1633 {
1634         spa_t *spa = zio->io_spa;
1635         metaslab_class_t *mc = spa->spa_normal_class;
1636         blkptr_t *bp = zio->io_bp;
1637         int error;
1638
1639         if (zio->io_gang_leader == NULL) {
1640                 ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
1641                 zio->io_gang_leader = zio;
1642         }
1643
1644         ASSERT(BP_IS_HOLE(bp));
1645         ASSERT3U(BP_GET_NDVAS(bp), ==, 0);
1646         ASSERT3U(zio->io_prop.zp_ndvas, >, 0);
1647         ASSERT3U(zio->io_prop.zp_ndvas, <=, spa_max_replication(spa));
1648         ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp));
1649
1650         error = metaslab_alloc(spa, mc, zio->io_size, bp,
1651             zio->io_prop.zp_ndvas, zio->io_txg, NULL, 0);
1652
1653         if (error) {
1654                 if (error == ENOSPC && zio->io_size > SPA_MINBLOCKSIZE)
1655                         return (zio_write_gang_block(zio));
1656                 zio->io_error = error;
1657         }
1658
1659         return (ZIO_PIPELINE_CONTINUE);
1660 }
1661
1662 static int
1663 zio_dva_free(zio_t *zio)
1664 {
1665         metaslab_free(zio->io_spa, zio->io_bp, zio->io_txg, B_FALSE);
1666
1667         return (ZIO_PIPELINE_CONTINUE);
1668 }
1669
1670 static int
1671 zio_dva_claim(zio_t *zio)
1672 {
1673         int error;
1674
1675         error = metaslab_claim(zio->io_spa, zio->io_bp, zio->io_txg);
1676         if (error)
1677                 zio->io_error = error;
1678
1679         return (ZIO_PIPELINE_CONTINUE);
1680 }
1681
1682 /*
1683  * Undo an allocation.  This is used by zio_done() when an I/O fails
1684  * and we want to give back the block we just allocated.
1685  * This handles both normal blocks and gang blocks.
1686  */
1687 static void
1688 zio_dva_unallocate(zio_t *zio, zio_gang_node_t *gn, blkptr_t *bp)
1689 {
1690         spa_t *spa = zio->io_spa;
1691         boolean_t now = !(zio->io_flags & ZIO_FLAG_IO_REWRITE);
1692
1693         ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp));
1694
1695         if (zio->io_bp == bp && !now) {
1696                 /*
1697                  * This is a rewrite for sync-to-convergence.
1698                  * We can't do a metaslab_free(NOW) because bp wasn't allocated
1699                  * during this sync pass, which means that metaslab_sync()
1700                  * already committed the allocation.
1701                  */
1702                 ASSERT(DVA_EQUAL(BP_IDENTITY(bp),
1703                     BP_IDENTITY(&zio->io_bp_orig)));
1704                 ASSERT(spa_sync_pass(spa) > 1);
1705
1706                 if (BP_IS_GANG(bp) && gn == NULL) {
1707                         /*
1708                          * This is a gang leader whose gang header(s) we
1709                          * couldn't read now, so defer the free until later.
1710                          * The block should still be intact because without
1711                          * the headers, we'd never even start the rewrite.
1712                          */
1713                         bplist_enqueue_deferred(&spa->spa_sync_bplist, bp);
1714                         return;
1715                 }
1716         }
1717
1718         if (!BP_IS_HOLE(bp))
1719                 metaslab_free(spa, bp, bp->blk_birth, now);
1720
1721         if (gn != NULL) {
1722                 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
1723                         zio_dva_unallocate(zio, gn->gn_child[g],
1724                             &gn->gn_gbh->zg_blkptr[g]);
1725                 }
1726         }
1727 }
1728
1729 /*
1730  * Try to allocate an intent log block.  Return 0 on success, errno on failure.
1731  */
1732 int
1733 zio_alloc_blk(spa_t *spa, uint64_t size, blkptr_t *new_bp, blkptr_t *old_bp,
1734     uint64_t txg)
1735 {
1736         int error;
1737
1738         error = metaslab_alloc(spa, spa->spa_log_class, size,
1739             new_bp, 1, txg, old_bp, METASLAB_HINTBP_AVOID);
1740
1741         if (error)
1742                 error = metaslab_alloc(spa, spa->spa_normal_class, size,
1743                     new_bp, 1, txg, old_bp, METASLAB_HINTBP_AVOID);
1744
1745         if (error == 0) {
1746                 BP_SET_LSIZE(new_bp, size);
1747                 BP_SET_PSIZE(new_bp, size);
1748                 BP_SET_COMPRESS(new_bp, ZIO_COMPRESS_OFF);
1749                 BP_SET_CHECKSUM(new_bp, ZIO_CHECKSUM_ZILOG);
1750                 BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG);
1751                 BP_SET_LEVEL(new_bp, 0);
1752                 BP_SET_BYTEORDER(new_bp, ZFS_HOST_BYTEORDER);
1753         }
1754
1755         return (error);
1756 }
1757
1758 /*
1759  * Free an intent log block.  We know it can't be a gang block, so there's
1760  * nothing to do except metaslab_free() it.
1761  */
1762 void
1763 zio_free_blk(spa_t *spa, blkptr_t *bp, uint64_t txg)
1764 {
1765         ASSERT(!BP_IS_GANG(bp));
1766
1767         metaslab_free(spa, bp, txg, B_FALSE);
1768 }
1769
1770 /*
1771  * ==========================================================================
1772  * Read and write to physical devices
1773  * ==========================================================================
1774  */
1775 static int
1776 zio_vdev_io_start(zio_t *zio)
1777 {
1778         vdev_t *vd = zio->io_vd;
1779         uint64_t align;
1780         spa_t *spa = zio->io_spa;
1781
1782         ASSERT(zio->io_error == 0);
1783         ASSERT(zio->io_child_error[ZIO_CHILD_VDEV] == 0);
1784
1785         if (vd == NULL) {
1786                 if (!(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
1787                         spa_config_enter(spa, SCL_ZIO, zio, RW_READER);
1788
1789                 /*
1790                  * The mirror_ops handle multiple DVAs in a single BP.
1791                  */
1792                 return (vdev_mirror_ops.vdev_op_io_start(zio));
1793         }
1794
1795         align = 1ULL << vd->vdev_top->vdev_ashift;
1796
1797         if (P2PHASE(zio->io_size, align) != 0) {
1798                 uint64_t asize = P2ROUNDUP(zio->io_size, align);
1799                 char *abuf = zio_buf_alloc(asize);
1800                 ASSERT(vd == vd->vdev_top);
1801                 if (zio->io_type == ZIO_TYPE_WRITE) {
1802                         bcopy(zio->io_data, abuf, zio->io_size);
1803                         bzero(abuf + zio->io_size, asize - zio->io_size);
1804                 }
1805                 zio_push_transform(zio, abuf, asize, asize, zio_subblock);
1806         }
1807
1808         ASSERT(P2PHASE(zio->io_offset, align) == 0);
1809         ASSERT(P2PHASE(zio->io_size, align) == 0);
1810         ASSERT(zio->io_type != ZIO_TYPE_WRITE || spa_writeable(spa));
1811
1812         /*
1813          * If this is a repair I/O, and there's no self-healing involved --
1814          * that is, we're just resilvering what we expect to resilver --
1815          * then don't do the I/O unless zio's txg is actually in vd's DTL.
1816          * This prevents spurious resilvering with nested replication.
1817          * For example, given a mirror of mirrors, (A+B)+(C+D), if only
1818          * A is out of date, we'll read from C+D, then use the data to
1819          * resilver A+B -- but we don't actually want to resilver B, just A.
1820          * The top-level mirror has no way to know this, so instead we just
1821          * discard unnecessary repairs as we work our way down the vdev tree.
1822          * The same logic applies to any form of nested replication:
1823          * ditto + mirror, RAID-Z + replacing, etc.  This covers them all.
1824          */
1825         if ((zio->io_flags & ZIO_FLAG_IO_REPAIR) &&
1826             !(zio->io_flags & ZIO_FLAG_SELF_HEAL) &&
1827             zio->io_txg != 0 && /* not a delegated i/o */
1828             !vdev_dtl_contains(vd, DTL_PARTIAL, zio->io_txg, 1)) {
1829                 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
1830                 zio_vdev_io_bypass(zio);
1831                 return (ZIO_PIPELINE_CONTINUE);
1832         }
1833
1834         if (vd->vdev_ops->vdev_op_leaf &&
1835             (zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE)) {
1836
1837                 if (zio->io_type == ZIO_TYPE_READ && vdev_cache_read(zio) == 0)
1838                         return (ZIO_PIPELINE_CONTINUE);
1839
1840                 if ((zio = vdev_queue_io(zio)) == NULL)
1841                         return (ZIO_PIPELINE_STOP);
1842
1843                 if (!vdev_accessible(vd, zio)) {
1844                         zio->io_error = ENXIO;
1845                         zio_interrupt(zio);
1846                         return (ZIO_PIPELINE_STOP);
1847                 }
1848         }
1849
1850         return (vd->vdev_ops->vdev_op_io_start(zio));
1851 }
1852
1853 static int
1854 zio_vdev_io_done(zio_t *zio)
1855 {
1856         vdev_t *vd = zio->io_vd;
1857         vdev_ops_t *ops = vd ? vd->vdev_ops : &vdev_mirror_ops;
1858         boolean_t unexpected_error = B_FALSE;
1859
1860         if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE))
1861                 return (ZIO_PIPELINE_STOP);
1862
1863         ASSERT(zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE);
1864
1865         if (vd != NULL && vd->vdev_ops->vdev_op_leaf) {
1866
1867                 vdev_queue_io_done(zio);
1868
1869                 if (zio->io_type == ZIO_TYPE_WRITE)
1870                         vdev_cache_write(zio);
1871
1872                 if (zio_injection_enabled && zio->io_error == 0)
1873                         zio->io_error = zio_handle_device_injection(vd,
1874                             zio, EIO);
1875
1876                 if (zio_injection_enabled && zio->io_error == 0)
1877                         zio->io_error = zio_handle_label_injection(zio, EIO);
1878
1879                 if (zio->io_error) {
1880                         if (!vdev_accessible(vd, zio)) {
1881                                 zio->io_error = ENXIO;
1882                         } else {
1883                                 unexpected_error = B_TRUE;
1884                         }
1885                 }
1886         }
1887
1888         ops->vdev_op_io_done(zio);
1889
1890         if (unexpected_error)
1891                 VERIFY(vdev_probe(vd, zio) == NULL);
1892
1893         return (ZIO_PIPELINE_CONTINUE);
1894 }
1895
1896 static int
1897 zio_vdev_io_assess(zio_t *zio)
1898 {
1899         vdev_t *vd = zio->io_vd;
1900
1901         if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE))
1902                 return (ZIO_PIPELINE_STOP);
1903
1904         if (vd == NULL && !(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
1905                 spa_config_exit(zio->io_spa, SCL_ZIO, zio);
1906
1907         if (zio->io_vsd != NULL) {
1908                 zio->io_vsd_free(zio);
1909                 zio->io_vsd = NULL;
1910         }
1911
1912         if (zio_injection_enabled && zio->io_error == 0)
1913                 zio->io_error = zio_handle_fault_injection(zio, EIO);
1914
1915         /*
1916          * If the I/O failed, determine whether we should attempt to retry it.
1917          */
1918         if (zio->io_error && vd == NULL &&
1919             !(zio->io_flags & (ZIO_FLAG_DONT_RETRY | ZIO_FLAG_IO_RETRY))) {
1920                 ASSERT(!(zio->io_flags & ZIO_FLAG_DONT_QUEUE)); /* not a leaf */
1921                 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_BYPASS));  /* not a leaf */
1922                 zio->io_error = 0;
1923                 zio->io_flags |= ZIO_FLAG_IO_RETRY |
1924                     ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE;
1925                 zio->io_stage = ZIO_STAGE_VDEV_IO_START - 1;
1926                 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE);
1927                 return (ZIO_PIPELINE_STOP);
1928         }
1929
1930         /*
1931          * If we got an error on a leaf device, convert it to ENXIO
1932          * if the device is not accessible at all.
1933          */
1934         if (zio->io_error && vd != NULL && vd->vdev_ops->vdev_op_leaf &&
1935             !vdev_accessible(vd, zio))
1936                 zio->io_error = ENXIO;
1937
1938         /*
1939          * If we can't write to an interior vdev (mirror or RAID-Z),
1940          * set vdev_cant_write so that we stop trying to allocate from it.
1941          */
1942         if (zio->io_error == ENXIO && zio->io_type == ZIO_TYPE_WRITE &&
1943             vd != NULL && !vd->vdev_ops->vdev_op_leaf)
1944                 vd->vdev_cant_write = B_TRUE;
1945
1946         if (zio->io_error)
1947                 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1948
1949         return (ZIO_PIPELINE_CONTINUE);
1950 }
1951
1952 void
1953 zio_vdev_io_reissue(zio_t *zio)
1954 {
1955         ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
1956         ASSERT(zio->io_error == 0);
1957
1958         zio->io_stage--;
1959 }
1960
1961 void
1962 zio_vdev_io_redone(zio_t *zio)
1963 {
1964         ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_DONE);
1965
1966         zio->io_stage--;
1967 }
1968
1969 void
1970 zio_vdev_io_bypass(zio_t *zio)
1971 {
1972         ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
1973         ASSERT(zio->io_error == 0);
1974
1975         zio->io_flags |= ZIO_FLAG_IO_BYPASS;
1976         zio->io_stage = ZIO_STAGE_VDEV_IO_ASSESS - 1;
1977 }
1978
1979 /*
1980  * ==========================================================================
1981  * Generate and verify checksums
1982  * ==========================================================================
1983  */
1984 static int
1985 zio_checksum_generate(zio_t *zio)
1986 {
1987         blkptr_t *bp = zio->io_bp;
1988         enum zio_checksum checksum;
1989
1990         if (bp == NULL) {
1991                 /*
1992                  * This is zio_write_phys().
1993                  * We're either generating a label checksum, or none at all.
1994                  */
1995                 checksum = zio->io_prop.zp_checksum;
1996
1997                 if (checksum == ZIO_CHECKSUM_OFF)
1998                         return (ZIO_PIPELINE_CONTINUE);
1999
2000                 ASSERT(checksum == ZIO_CHECKSUM_LABEL);
2001         } else {
2002                 if (BP_IS_GANG(bp) && zio->io_child_type == ZIO_CHILD_GANG) {
2003                         ASSERT(!IO_IS_ALLOCATING(zio));
2004                         checksum = ZIO_CHECKSUM_GANG_HEADER;
2005                 } else {
2006                         checksum = BP_GET_CHECKSUM(bp);
2007                 }
2008         }
2009
2010         zio_checksum_compute(zio, checksum, zio->io_data, zio->io_size);
2011
2012         return (ZIO_PIPELINE_CONTINUE);
2013 }
2014
2015 static int
2016 zio_checksum_verify(zio_t *zio)
2017 {
2018         blkptr_t *bp = zio->io_bp;
2019         int error;
2020
2021         if (bp == NULL) {
2022                 /*
2023                  * This is zio_read_phys().
2024                  * We're either verifying a label checksum, or nothing at all.
2025                  */
2026                 if (zio->io_prop.zp_checksum == ZIO_CHECKSUM_OFF)
2027                         return (ZIO_PIPELINE_CONTINUE);
2028
2029                 ASSERT(zio->io_prop.zp_checksum == ZIO_CHECKSUM_LABEL);
2030         }
2031
2032         if ((error = zio_checksum_error(zio)) != 0) {
2033                 zio->io_error = error;
2034                 if (!(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
2035                         zfs_ereport_post(FM_EREPORT_ZFS_CHECKSUM,
2036                             zio->io_spa, zio->io_vd, zio, 0, 0);
2037                 }
2038         }
2039
2040         return (ZIO_PIPELINE_CONTINUE);
2041 }
2042
2043 /*
2044  * Called by RAID-Z to ensure we don't compute the checksum twice.
2045  */
2046 void
2047 zio_checksum_verified(zio_t *zio)
2048 {
2049         zio->io_pipeline &= ~(1U << ZIO_STAGE_CHECKSUM_VERIFY);
2050 }
2051
2052 /*
2053  * ==========================================================================
2054  * Error rank.  Error are ranked in the order 0, ENXIO, ECKSUM, EIO, other.
2055  * An error of 0 indictes success.  ENXIO indicates whole-device failure,
2056  * which may be transient (e.g. unplugged) or permament.  ECKSUM and EIO
2057  * indicate errors that are specific to one I/O, and most likely permanent.
2058  * Any other error is presumed to be worse because we weren't expecting it.
2059  * ==========================================================================
2060  */
2061 int
2062 zio_worst_error(int e1, int e2)
2063 {
2064         static int zio_error_rank[] = { 0, ENXIO, ECKSUM, EIO };
2065         int r1, r2;
2066
2067         for (r1 = 0; r1 < sizeof (zio_error_rank) / sizeof (int); r1++)
2068                 if (e1 == zio_error_rank[r1])
2069                         break;
2070
2071         for (r2 = 0; r2 < sizeof (zio_error_rank) / sizeof (int); r2++)
2072                 if (e2 == zio_error_rank[r2])
2073                         break;
2074
2075         return (r1 > r2 ? e1 : e2);
2076 }
2077
2078 /*
2079  * ==========================================================================
2080  * I/O completion
2081  * ==========================================================================
2082  */
2083 static int
2084 zio_ready(zio_t *zio)
2085 {
2086         blkptr_t *bp = zio->io_bp;
2087         zio_t *pio, *pio_next;
2088
2089         if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_READY))
2090                 return (ZIO_PIPELINE_STOP);
2091
2092         if (zio->io_ready) {
2093                 ASSERT(IO_IS_ALLOCATING(zio));
2094                 ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp));
2095                 ASSERT(zio->io_children[ZIO_CHILD_GANG][ZIO_WAIT_READY] == 0);
2096
2097                 zio->io_ready(zio);
2098         }
2099
2100         if (bp != NULL && bp != &zio->io_bp_copy)
2101                 zio->io_bp_copy = *bp;
2102
2103         if (zio->io_error)
2104                 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2105
2106         mutex_enter(&zio->io_lock);
2107         zio->io_state[ZIO_WAIT_READY] = 1;
2108         pio = zio_walk_parents(zio);
2109         mutex_exit(&zio->io_lock);
2110
2111         /*
2112          * As we notify zio's parents, new parents could be added.
2113          * New parents go to the head of zio's io_parent_list, however,
2114          * so we will (correctly) not notify them.  The remainder of zio's
2115          * io_parent_list, from 'pio_next' onward, cannot change because
2116          * all parents must wait for us to be done before they can be done.
2117          */
2118         for (; pio != NULL; pio = pio_next) {
2119                 pio_next = zio_walk_parents(zio);
2120                 zio_notify_parent(pio, zio, ZIO_WAIT_READY);
2121         }
2122
2123         return (ZIO_PIPELINE_CONTINUE);
2124 }
2125
2126 static int
2127 zio_done(zio_t *zio)
2128 {
2129         spa_t *spa = zio->io_spa;
2130         zio_t *lio = zio->io_logical;
2131         blkptr_t *bp = zio->io_bp;
2132         vdev_t *vd = zio->io_vd;
2133         uint64_t psize = zio->io_size;
2134         zio_t *pio, *pio_next;
2135
2136         /*
2137          * If our children haven't all completed,
2138          * wait for them and then repeat this pipeline stage.
2139          */
2140         if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE) ||
2141             zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_DONE) ||
2142             zio_wait_for_children(zio, ZIO_CHILD_LOGICAL, ZIO_WAIT_DONE))
2143                 return (ZIO_PIPELINE_STOP);
2144
2145         for (int c = 0; c < ZIO_CHILD_TYPES; c++)
2146                 for (int w = 0; w < ZIO_WAIT_TYPES; w++)
2147                         ASSERT(zio->io_children[c][w] == 0);
2148
2149         if (bp != NULL) {
2150                 ASSERT(bp->blk_pad[0] == 0);
2151                 ASSERT(bp->blk_pad[1] == 0);
2152                 ASSERT(bp->blk_pad[2] == 0);
2153                 ASSERT(bcmp(bp, &zio->io_bp_copy, sizeof (blkptr_t)) == 0 ||
2154                     (bp == zio_unique_parent(zio)->io_bp));
2155                 if (zio->io_type == ZIO_TYPE_WRITE && !BP_IS_HOLE(bp) &&
2156                     !(zio->io_flags & ZIO_FLAG_IO_REPAIR)) {
2157                         ASSERT(!BP_SHOULD_BYTESWAP(bp));
2158                         ASSERT3U(zio->io_prop.zp_ndvas, <=, BP_GET_NDVAS(bp));
2159                         ASSERT(BP_COUNT_GANG(bp) == 0 ||
2160                             (BP_COUNT_GANG(bp) == BP_GET_NDVAS(bp)));
2161                 }
2162         }
2163
2164         /*
2165          * If there were child vdev or gang errors, they apply to us now.
2166          */
2167         zio_inherit_child_errors(zio, ZIO_CHILD_VDEV);
2168         zio_inherit_child_errors(zio, ZIO_CHILD_GANG);
2169
2170         zio_pop_transforms(zio);        /* note: may set zio->io_error */
2171
2172         vdev_stat_update(zio, psize);
2173
2174         if (zio->io_error) {
2175                 /*
2176                  * If this I/O is attached to a particular vdev,
2177                  * generate an error message describing the I/O failure
2178                  * at the block level.  We ignore these errors if the
2179                  * device is currently unavailable.
2180                  */
2181                 if (zio->io_error != ECKSUM && vd != NULL && !vdev_is_dead(vd))
2182                         zfs_ereport_post(FM_EREPORT_ZFS_IO, spa, vd, zio, 0, 0);
2183
2184                 if ((zio->io_error == EIO ||
2185                     !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) && zio == lio) {
2186                         /*
2187                          * For logical I/O requests, tell the SPA to log the
2188                          * error and generate a logical data ereport.
2189                          */
2190                         spa_log_error(spa, zio);
2191                         zfs_ereport_post(FM_EREPORT_ZFS_DATA, spa, NULL, zio,
2192                             0, 0);
2193                 }
2194         }
2195
2196         if (zio->io_error && zio == lio) {
2197                 /*
2198                  * Determine whether zio should be reexecuted.  This will
2199                  * propagate all the way to the root via zio_notify_parent().
2200                  */
2201                 ASSERT(vd == NULL && bp != NULL);
2202
2203                 if (IO_IS_ALLOCATING(zio))
2204                         if (zio->io_error != ENOSPC)
2205                                 zio->io_reexecute |= ZIO_REEXECUTE_NOW;
2206                         else
2207                                 zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
2208
2209                 if ((zio->io_type == ZIO_TYPE_READ ||
2210                     zio->io_type == ZIO_TYPE_FREE) &&
2211                     zio->io_error == ENXIO &&
2212                     spa->spa_load_state == SPA_LOAD_NONE &&
2213                     spa_get_failmode(spa) != ZIO_FAILURE_MODE_CONTINUE)
2214                         zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
2215
2216                 if (!(zio->io_flags & ZIO_FLAG_CANFAIL) && !zio->io_reexecute)
2217                         zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
2218         }
2219
2220         /*
2221          * If there were logical child errors, they apply to us now.
2222          * We defer this until now to avoid conflating logical child
2223          * errors with errors that happened to the zio itself when
2224          * updating vdev stats and reporting FMA events above.
2225          */
2226         zio_inherit_child_errors(zio, ZIO_CHILD_LOGICAL);
2227
2228         if ((zio->io_error || zio->io_reexecute) && IO_IS_ALLOCATING(zio) &&
2229             zio->io_child_type == ZIO_CHILD_LOGICAL) {
2230                 ASSERT(zio->io_child_type != ZIO_CHILD_GANG);
2231                 zio_dva_unallocate(zio, zio->io_gang_tree, bp);
2232         }
2233
2234         zio_gang_tree_free(&zio->io_gang_tree);
2235
2236         /*
2237          * Godfather I/Os should never suspend.
2238          */
2239         if ((zio->io_flags & ZIO_FLAG_GODFATHER) &&
2240             (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND))
2241                 zio->io_reexecute = 0;
2242
2243         if (zio->io_reexecute) {
2244                 /*
2245                  * This is a logical I/O that wants to reexecute.
2246                  *
2247                  * Reexecute is top-down.  When an i/o fails, if it's not
2248                  * the root, it simply notifies its parent and sticks around.
2249                  * The parent, seeing that it still has children in zio_done(),
2250                  * does the same.  This percolates all the way up to the root.
2251                  * The root i/o will reexecute or suspend the entire tree.
2252                  *
2253                  * This approach ensures that zio_reexecute() honors
2254                  * all the original i/o dependency relationships, e.g.
2255                  * parents not executing until children are ready.
2256                  */
2257                 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2258
2259                 zio->io_gang_leader = NULL;
2260
2261                 mutex_enter(&zio->io_lock);
2262                 zio->io_state[ZIO_WAIT_DONE] = 1;
2263                 mutex_exit(&zio->io_lock);
2264
2265                 /*
2266                  * "The Godfather" I/O monitors its children but is
2267                  * not a true parent to them. It will track them through
2268                  * the pipeline but severs its ties whenever they get into
2269                  * trouble (e.g. suspended). This allows "The Godfather"
2270                  * I/O to return status without blocking.
2271                  */
2272                 for (pio = zio_walk_parents(zio); pio != NULL; pio = pio_next) {
2273                         zio_link_t *zl = zio->io_walk_link;
2274                         pio_next = zio_walk_parents(zio);
2275
2276                         if ((pio->io_flags & ZIO_FLAG_GODFATHER) &&
2277                             (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND)) {
2278                                 zio_remove_child(pio, zio, zl);
2279                                 zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
2280                         }
2281                 }
2282
2283                 if ((pio = zio_unique_parent(zio)) != NULL) {
2284                         /*
2285                          * We're not a root i/o, so there's nothing to do
2286                          * but notify our parent.  Don't propagate errors
2287                          * upward since we haven't permanently failed yet.
2288                          */
2289                         ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER));
2290                         zio->io_flags |= ZIO_FLAG_DONT_PROPAGATE;
2291                         zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
2292                 } else if (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND) {
2293                         /*
2294                          * We'd fail again if we reexecuted now, so suspend
2295                          * until conditions improve (e.g. device comes online).
2296                          */
2297                         zio_suspend(spa, zio);
2298                 } else {
2299                         /*
2300                          * Reexecution is potentially a huge amount of work.
2301                          * Hand it off to the otherwise-unused claim taskq.
2302                          */
2303                         (void) taskq_dispatch_safe(
2304                             spa->spa_zio_taskq[ZIO_TYPE_CLAIM][ZIO_TASKQ_ISSUE],
2305                             (task_func_t *)zio_reexecute, zio, &zio->io_task);
2306                 }
2307                 return (ZIO_PIPELINE_STOP);
2308         }
2309
2310         ASSERT(zio_walk_children(zio) == NULL);
2311         ASSERT(zio->io_reexecute == 0);
2312         ASSERT(zio->io_error == 0 || (zio->io_flags & ZIO_FLAG_CANFAIL));
2313
2314         /*
2315          * It is the responsibility of the done callback to ensure that this
2316          * particular zio is no longer discoverable for adoption, and as
2317          * such, cannot acquire any new parents.
2318          */
2319         if (zio->io_done)
2320                 zio->io_done(zio);
2321
2322         mutex_enter(&zio->io_lock);
2323         zio->io_state[ZIO_WAIT_DONE] = 1;
2324         mutex_exit(&zio->io_lock);
2325
2326         for (pio = zio_walk_parents(zio); pio != NULL; pio = pio_next) {
2327                 zio_link_t *zl = zio->io_walk_link;
2328                 pio_next = zio_walk_parents(zio);
2329                 zio_remove_child(pio, zio, zl);
2330                 zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
2331         }
2332
2333         if (zio->io_waiter != NULL) {
2334                 mutex_enter(&zio->io_lock);
2335                 zio->io_executor = NULL;
2336                 cv_broadcast(&zio->io_cv);
2337                 mutex_exit(&zio->io_lock);
2338         } else {
2339                 zio_destroy(zio);
2340         }
2341
2342         return (ZIO_PIPELINE_STOP);
2343 }
2344
2345 /*
2346  * ==========================================================================
2347  * I/O pipeline definition
2348  * ==========================================================================
2349  */
2350 static zio_pipe_stage_t *zio_pipeline[ZIO_STAGES] = {
2351         NULL,
2352         zio_issue_async,
2353         zio_read_bp_init,
2354         zio_write_bp_init,
2355         zio_checksum_generate,
2356         zio_gang_assemble,
2357         zio_gang_issue,
2358         zio_dva_allocate,
2359         zio_dva_free,
2360         zio_dva_claim,
2361         zio_ready,
2362         zio_vdev_io_start,
2363         zio_vdev_io_done,
2364         zio_vdev_io_assess,
2365         zio_checksum_verify,
2366         zio_done
2367 };