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