]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/zio.c
OpenZFS 7090 - zfs should throttle allocations
[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) 2011, 2016 by Delphix. All rights reserved.
24 * Copyright (c) 2011 Nexenta Systems, Inc. All rights reserved.
25 */
26
27 #include <sys/sysmacros.h>
28 #include <sys/zfs_context.h>
29 #include <sys/fm/fs/zfs.h>
30 #include <sys/spa.h>
31 #include <sys/txg.h>
32 #include <sys/spa_impl.h>
33 #include <sys/vdev_impl.h>
34 #include <sys/zio_impl.h>
35 #include <sys/zio_compress.h>
36 #include <sys/zio_checksum.h>
37 #include <sys/dmu_objset.h>
38 #include <sys/arc.h>
39 #include <sys/ddt.h>
40 #include <sys/blkptr.h>
41 #include <sys/zfeature.h>
42 #include <sys/metaslab_impl.h>
43 #include <sys/time.h>
44 #include <sys/trace_zio.h>
45
46 /*
47 * ==========================================================================
48 * I/O type descriptions
49 * ==========================================================================
50 */
51 const char *zio_type_name[ZIO_TYPES] = {
52 /*
53 * Note: Linux kernel thread name length is limited
54 * so these names will differ from upstream open zfs.
55 */
56 "z_null", "z_rd", "z_wr", "z_fr", "z_cl", "z_ioctl"
57 };
58
59 int zio_dva_throttle_enabled = B_TRUE;
60
61 /*
62 * ==========================================================================
63 * I/O kmem caches
64 * ==========================================================================
65 */
66 kmem_cache_t *zio_cache;
67 kmem_cache_t *zio_link_cache;
68 kmem_cache_t *zio_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
69 kmem_cache_t *zio_data_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
70 int zio_delay_max = ZIO_DELAY_MAX;
71
72 #define ZIO_PIPELINE_CONTINUE 0x100
73 #define ZIO_PIPELINE_STOP 0x101
74
75 #define BP_SPANB(indblkshift, level) \
76 (((uint64_t)1) << ((level) * ((indblkshift) - SPA_BLKPTRSHIFT)))
77 #define COMPARE_META_LEVEL 0x80000000ul
78 /*
79 * The following actions directly effect the spa's sync-to-convergence logic.
80 * The values below define the sync pass when we start performing the action.
81 * Care should be taken when changing these values as they directly impact
82 * spa_sync() performance. Tuning these values may introduce subtle performance
83 * pathologies and should only be done in the context of performance analysis.
84 * These tunables will eventually be removed and replaced with #defines once
85 * enough analysis has been done to determine optimal values.
86 *
87 * The 'zfs_sync_pass_deferred_free' pass must be greater than 1 to ensure that
88 * regular blocks are not deferred.
89 */
90 int zfs_sync_pass_deferred_free = 2; /* defer frees starting in this pass */
91 int zfs_sync_pass_dont_compress = 5; /* don't compress starting in this pass */
92 int zfs_sync_pass_rewrite = 2; /* rewrite new bps starting in this pass */
93
94 /*
95 * An allocating zio is one that either currently has the DVA allocate
96 * stage set or will have it later in its lifetime.
97 */
98 #define IO_IS_ALLOCATING(zio) ((zio)->io_orig_pipeline & ZIO_STAGE_DVA_ALLOCATE)
99
100 int zio_requeue_io_start_cut_in_line = 1;
101
102 #ifdef ZFS_DEBUG
103 int zio_buf_debug_limit = 16384;
104 #else
105 int zio_buf_debug_limit = 0;
106 #endif
107
108 static inline void __zio_execute(zio_t *zio);
109
110 static void zio_taskq_dispatch(zio_t *, zio_taskq_type_t, boolean_t);
111
112 void
113 zio_init(void)
114 {
115 size_t c;
116 vmem_t *data_alloc_arena = NULL;
117
118 zio_cache = kmem_cache_create("zio_cache",
119 sizeof (zio_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
120 zio_link_cache = kmem_cache_create("zio_link_cache",
121 sizeof (zio_link_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
122
123 /*
124 * For small buffers, we want a cache for each multiple of
125 * SPA_MINBLOCKSIZE. For larger buffers, we want a cache
126 * for each quarter-power of 2.
127 */
128 for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) {
129 size_t size = (c + 1) << SPA_MINBLOCKSHIFT;
130 size_t p2 = size;
131 size_t align = 0;
132 size_t cflags = (size > zio_buf_debug_limit) ? KMC_NODEBUG : 0;
133
134 #ifdef _ILP32
135 /*
136 * Cache size limited to 1M on 32-bit platforms until ARC
137 * buffers no longer require virtual address space.
138 */
139 if (size > zfs_max_recordsize)
140 break;
141 #endif
142
143 while (!ISP2(p2))
144 p2 &= p2 - 1;
145
146 #ifndef _KERNEL
147 /*
148 * If we are using watchpoints, put each buffer on its own page,
149 * to eliminate the performance overhead of trapping to the
150 * kernel when modifying a non-watched buffer that shares the
151 * page with a watched buffer.
152 */
153 if (arc_watch && !IS_P2ALIGNED(size, PAGESIZE))
154 continue;
155 /*
156 * Here's the problem - on 4K native devices in userland on
157 * Linux using O_DIRECT, buffers must be 4K aligned or I/O
158 * will fail with EINVAL, causing zdb (and others) to coredump.
159 * Since userland probably doesn't need optimized buffer caches,
160 * we just force 4K alignment on everything.
161 */
162 align = 8 * SPA_MINBLOCKSIZE;
163 #else
164 if (size <= 4 * SPA_MINBLOCKSIZE) {
165 align = SPA_MINBLOCKSIZE;
166 } else if (IS_P2ALIGNED(size, p2 >> 2)) {
167 align = MIN(p2 >> 2, PAGESIZE);
168 }
169 #endif
170
171 if (align != 0) {
172 char name[36];
173 (void) sprintf(name, "zio_buf_%lu", (ulong_t)size);
174 zio_buf_cache[c] = kmem_cache_create(name, size,
175 align, NULL, NULL, NULL, NULL, NULL, cflags);
176
177 (void) sprintf(name, "zio_data_buf_%lu", (ulong_t)size);
178 zio_data_buf_cache[c] = kmem_cache_create(name, size,
179 align, NULL, NULL, NULL, NULL,
180 data_alloc_arena, cflags);
181 }
182 }
183
184 while (--c != 0) {
185 ASSERT(zio_buf_cache[c] != NULL);
186 if (zio_buf_cache[c - 1] == NULL)
187 zio_buf_cache[c - 1] = zio_buf_cache[c];
188
189 ASSERT(zio_data_buf_cache[c] != NULL);
190 if (zio_data_buf_cache[c - 1] == NULL)
191 zio_data_buf_cache[c - 1] = zio_data_buf_cache[c];
192 }
193
194 zio_inject_init();
195
196 lz4_init();
197 }
198
199 void
200 zio_fini(void)
201 {
202 size_t c;
203 kmem_cache_t *last_cache = NULL;
204 kmem_cache_t *last_data_cache = NULL;
205
206 for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) {
207 #ifdef _ILP32
208 /*
209 * Cache size limited to 1M on 32-bit platforms until ARC
210 * buffers no longer require virtual address space.
211 */
212 if (((c + 1) << SPA_MINBLOCKSHIFT) > zfs_max_recordsize)
213 break;
214 #endif
215 if (zio_buf_cache[c] != last_cache) {
216 last_cache = zio_buf_cache[c];
217 kmem_cache_destroy(zio_buf_cache[c]);
218 }
219 zio_buf_cache[c] = NULL;
220
221 if (zio_data_buf_cache[c] != last_data_cache) {
222 last_data_cache = zio_data_buf_cache[c];
223 kmem_cache_destroy(zio_data_buf_cache[c]);
224 }
225 zio_data_buf_cache[c] = NULL;
226 }
227
228 kmem_cache_destroy(zio_link_cache);
229 kmem_cache_destroy(zio_cache);
230
231 zio_inject_fini();
232
233 lz4_fini();
234 }
235
236 /*
237 * ==========================================================================
238 * Allocate and free I/O buffers
239 * ==========================================================================
240 */
241
242 /*
243 * Use zio_buf_alloc to allocate ZFS metadata. This data will appear in a
244 * crashdump if the kernel panics, so use it judiciously. Obviously, it's
245 * useful to inspect ZFS metadata, but if possible, we should avoid keeping
246 * excess / transient data in-core during a crashdump.
247 */
248 void *
249 zio_buf_alloc(size_t size)
250 {
251 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
252
253 VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
254
255 return (kmem_cache_alloc(zio_buf_cache[c], KM_PUSHPAGE));
256 }
257
258 /*
259 * Use zio_data_buf_alloc to allocate data. The data will not appear in a
260 * crashdump if the kernel panics. This exists so that we will limit the amount
261 * of ZFS data that shows up in a kernel crashdump. (Thus reducing the amount
262 * of kernel heap dumped to disk when the kernel panics)
263 */
264 void *
265 zio_data_buf_alloc(size_t size)
266 {
267 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
268
269 VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
270
271 return (kmem_cache_alloc(zio_data_buf_cache[c], KM_PUSHPAGE));
272 }
273
274 /*
275 * Use zio_buf_alloc_flags when specific allocation flags are needed. e.g.
276 * passing KM_NOSLEEP when it is acceptable for an allocation to fail.
277 */
278 void *
279 zio_buf_alloc_flags(size_t size, int flags)
280 {
281 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
282
283 VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
284
285 return (kmem_cache_alloc(zio_buf_cache[c], flags));
286 }
287
288 void
289 zio_buf_free(void *buf, size_t size)
290 {
291 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
292
293 VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
294
295 kmem_cache_free(zio_buf_cache[c], buf);
296 }
297
298 void
299 zio_data_buf_free(void *buf, size_t size)
300 {
301 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
302
303 VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
304
305 kmem_cache_free(zio_data_buf_cache[c], buf);
306 }
307
308 /*
309 * ==========================================================================
310 * Push and pop I/O transform buffers
311 * ==========================================================================
312 */
313 void
314 zio_push_transform(zio_t *zio, void *data, uint64_t size, uint64_t bufsize,
315 zio_transform_func_t *transform)
316 {
317 zio_transform_t *zt = kmem_alloc(sizeof (zio_transform_t), KM_SLEEP);
318
319 zt->zt_orig_data = zio->io_data;
320 zt->zt_orig_size = zio->io_size;
321 zt->zt_bufsize = bufsize;
322 zt->zt_transform = transform;
323
324 zt->zt_next = zio->io_transform_stack;
325 zio->io_transform_stack = zt;
326
327 zio->io_data = data;
328 zio->io_size = size;
329 }
330
331 void
332 zio_pop_transforms(zio_t *zio)
333 {
334 zio_transform_t *zt;
335
336 while ((zt = zio->io_transform_stack) != NULL) {
337 if (zt->zt_transform != NULL)
338 zt->zt_transform(zio,
339 zt->zt_orig_data, zt->zt_orig_size);
340
341 if (zt->zt_bufsize != 0)
342 zio_buf_free(zio->io_data, zt->zt_bufsize);
343
344 zio->io_data = zt->zt_orig_data;
345 zio->io_size = zt->zt_orig_size;
346 zio->io_transform_stack = zt->zt_next;
347
348 kmem_free(zt, sizeof (zio_transform_t));
349 }
350 }
351
352 /*
353 * ==========================================================================
354 * I/O transform callbacks for subblocks and decompression
355 * ==========================================================================
356 */
357 static void
358 zio_subblock(zio_t *zio, void *data, uint64_t size)
359 {
360 ASSERT(zio->io_size > size);
361
362 if (zio->io_type == ZIO_TYPE_READ)
363 bcopy(zio->io_data, data, size);
364 }
365
366 static void
367 zio_decompress(zio_t *zio, void *data, uint64_t size)
368 {
369 if (zio->io_error == 0 &&
370 zio_decompress_data(BP_GET_COMPRESS(zio->io_bp),
371 zio->io_data, data, zio->io_size, size) != 0)
372 zio->io_error = SET_ERROR(EIO);
373 }
374
375 /*
376 * ==========================================================================
377 * I/O parent/child relationships and pipeline interlocks
378 * ==========================================================================
379 */
380 zio_t *
381 zio_walk_parents(zio_t *cio, zio_link_t **zl)
382 {
383 list_t *pl = &cio->io_parent_list;
384
385 *zl = (*zl == NULL) ? list_head(pl) : list_next(pl, *zl);
386 if (*zl == NULL)
387 return (NULL);
388
389 ASSERT((*zl)->zl_child == cio);
390 return ((*zl)->zl_parent);
391 }
392
393 zio_t *
394 zio_walk_children(zio_t *pio, zio_link_t **zl)
395 {
396 list_t *cl = &pio->io_child_list;
397
398 *zl = (*zl == NULL) ? list_head(cl) : list_next(cl, *zl);
399 if (*zl == NULL)
400 return (NULL);
401
402 ASSERT((*zl)->zl_parent == pio);
403 return ((*zl)->zl_child);
404 }
405
406 zio_t *
407 zio_unique_parent(zio_t *cio)
408 {
409 zio_link_t *zl = NULL;
410 zio_t *pio = zio_walk_parents(cio, &zl);
411
412 VERIFY3P(zio_walk_parents(cio, &zl), ==, NULL);
413 return (pio);
414 }
415
416 void
417 zio_add_child(zio_t *pio, zio_t *cio)
418 {
419 zio_link_t *zl = kmem_cache_alloc(zio_link_cache, KM_SLEEP);
420 int w;
421
422 /*
423 * Logical I/Os can have logical, gang, or vdev children.
424 * Gang I/Os can have gang or vdev children.
425 * Vdev I/Os can only have vdev children.
426 * The following ASSERT captures all of these constraints.
427 */
428 ASSERT(cio->io_child_type <= pio->io_child_type);
429
430 zl->zl_parent = pio;
431 zl->zl_child = cio;
432
433 mutex_enter(&cio->io_lock);
434 mutex_enter(&pio->io_lock);
435
436 ASSERT(pio->io_state[ZIO_WAIT_DONE] == 0);
437
438 for (w = 0; w < ZIO_WAIT_TYPES; w++)
439 pio->io_children[cio->io_child_type][w] += !cio->io_state[w];
440
441 list_insert_head(&pio->io_child_list, zl);
442 list_insert_head(&cio->io_parent_list, zl);
443
444 pio->io_child_count++;
445 cio->io_parent_count++;
446
447 mutex_exit(&pio->io_lock);
448 mutex_exit(&cio->io_lock);
449 }
450
451 static void
452 zio_remove_child(zio_t *pio, zio_t *cio, zio_link_t *zl)
453 {
454 ASSERT(zl->zl_parent == pio);
455 ASSERT(zl->zl_child == cio);
456
457 mutex_enter(&cio->io_lock);
458 mutex_enter(&pio->io_lock);
459
460 list_remove(&pio->io_child_list, zl);
461 list_remove(&cio->io_parent_list, zl);
462
463 pio->io_child_count--;
464 cio->io_parent_count--;
465
466 mutex_exit(&pio->io_lock);
467 mutex_exit(&cio->io_lock);
468 kmem_cache_free(zio_link_cache, zl);
469 }
470
471 static boolean_t
472 zio_wait_for_children(zio_t *zio, enum zio_child child, enum zio_wait_type wait)
473 {
474 uint64_t *countp = &zio->io_children[child][wait];
475 boolean_t waiting = B_FALSE;
476
477 mutex_enter(&zio->io_lock);
478 ASSERT(zio->io_stall == NULL);
479 if (*countp != 0) {
480 zio->io_stage >>= 1;
481 ASSERT3U(zio->io_stage, !=, ZIO_STAGE_OPEN);
482 zio->io_stall = countp;
483 waiting = B_TRUE;
484 }
485 mutex_exit(&zio->io_lock);
486
487 return (waiting);
488 }
489
490 __attribute__((always_inline))
491 static inline void
492 zio_notify_parent(zio_t *pio, zio_t *zio, enum zio_wait_type wait)
493 {
494 uint64_t *countp = &pio->io_children[zio->io_child_type][wait];
495 int *errorp = &pio->io_child_error[zio->io_child_type];
496
497 mutex_enter(&pio->io_lock);
498 if (zio->io_error && !(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
499 *errorp = zio_worst_error(*errorp, zio->io_error);
500 pio->io_reexecute |= zio->io_reexecute;
501 ASSERT3U(*countp, >, 0);
502
503 (*countp)--;
504
505 if (*countp == 0 && pio->io_stall == countp) {
506 zio_taskq_type_t type =
507 pio->io_stage < ZIO_STAGE_VDEV_IO_START ? ZIO_TASKQ_ISSUE :
508 ZIO_TASKQ_INTERRUPT;
509 pio->io_stall = NULL;
510 mutex_exit(&pio->io_lock);
511 /*
512 * Dispatch the parent zio in its own taskq so that
513 * the child can continue to make progress. This also
514 * prevents overflowing the stack when we have deeply nested
515 * parent-child relationships.
516 */
517 zio_taskq_dispatch(pio, type, B_FALSE);
518 } else {
519 mutex_exit(&pio->io_lock);
520 }
521 }
522
523 static void
524 zio_inherit_child_errors(zio_t *zio, enum zio_child c)
525 {
526 if (zio->io_child_error[c] != 0 && zio->io_error == 0)
527 zio->io_error = zio->io_child_error[c];
528 }
529
530 int
531 zio_timestamp_compare(const void *x1, const void *x2)
532 {
533 const zio_t *z1 = x1;
534 const zio_t *z2 = x2;
535 int cmp;
536
537 cmp = AVL_CMP(z1->io_queued_timestamp, z2->io_queued_timestamp);
538 if (likely(cmp))
539 return (cmp);
540
541 cmp = AVL_CMP(z1->io_offset, z2->io_offset);
542 if (likely(cmp))
543 return (cmp);
544
545 return (AVL_PCMP(z1, z2));
546 }
547
548 /*
549 * ==========================================================================
550 * Create the various types of I/O (read, write, free, etc)
551 * ==========================================================================
552 */
553 static zio_t *
554 zio_create(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
555 void *data, uint64_t lsize, uint64_t psize, zio_done_func_t *done,
556 void *private, zio_type_t type, zio_priority_t priority,
557 enum zio_flag flags, vdev_t *vd, uint64_t offset,
558 const zbookmark_phys_t *zb, enum zio_stage stage,
559 enum zio_stage pipeline)
560 {
561 zio_t *zio;
562
563 ASSERT3U(psize, <=, SPA_MAXBLOCKSIZE);
564 ASSERT(P2PHASE(psize, SPA_MINBLOCKSIZE) == 0);
565 ASSERT(P2PHASE(offset, SPA_MINBLOCKSIZE) == 0);
566
567 ASSERT(!vd || spa_config_held(spa, SCL_STATE_ALL, RW_READER));
568 ASSERT(!bp || !(flags & ZIO_FLAG_CONFIG_WRITER));
569 ASSERT(vd || stage == ZIO_STAGE_OPEN);
570
571 IMPLY(lsize != psize, (flags & ZIO_FLAG_RAW) != 0);
572
573 zio = kmem_cache_alloc(zio_cache, KM_SLEEP);
574 bzero(zio, sizeof (zio_t));
575
576 mutex_init(&zio->io_lock, NULL, MUTEX_NOLOCKDEP, NULL);
577 cv_init(&zio->io_cv, NULL, CV_DEFAULT, NULL);
578
579 list_create(&zio->io_parent_list, sizeof (zio_link_t),
580 offsetof(zio_link_t, zl_parent_node));
581 list_create(&zio->io_child_list, sizeof (zio_link_t),
582 offsetof(zio_link_t, zl_child_node));
583
584 if (vd != NULL)
585 zio->io_child_type = ZIO_CHILD_VDEV;
586 else if (flags & ZIO_FLAG_GANG_CHILD)
587 zio->io_child_type = ZIO_CHILD_GANG;
588 else if (flags & ZIO_FLAG_DDT_CHILD)
589 zio->io_child_type = ZIO_CHILD_DDT;
590 else
591 zio->io_child_type = ZIO_CHILD_LOGICAL;
592
593 if (bp != NULL) {
594 zio->io_bp = (blkptr_t *)bp;
595 zio->io_bp_copy = *bp;
596 zio->io_bp_orig = *bp;
597 if (type != ZIO_TYPE_WRITE ||
598 zio->io_child_type == ZIO_CHILD_DDT)
599 zio->io_bp = &zio->io_bp_copy; /* so caller can free */
600 if (zio->io_child_type == ZIO_CHILD_LOGICAL)
601 zio->io_logical = zio;
602 if (zio->io_child_type > ZIO_CHILD_GANG && BP_IS_GANG(bp))
603 pipeline |= ZIO_GANG_STAGES;
604 }
605
606 zio->io_spa = spa;
607 zio->io_txg = txg;
608 zio->io_done = done;
609 zio->io_private = private;
610 zio->io_type = type;
611 zio->io_priority = priority;
612 zio->io_vd = vd;
613 zio->io_offset = offset;
614 zio->io_orig_data = zio->io_data = data;
615 zio->io_orig_size = zio->io_size = psize;
616 zio->io_lsize = lsize;
617 zio->io_orig_flags = zio->io_flags = flags;
618 zio->io_orig_stage = zio->io_stage = stage;
619 zio->io_orig_pipeline = zio->io_pipeline = pipeline;
620 zio->io_pipeline_trace = ZIO_STAGE_OPEN;
621
622 zio->io_state[ZIO_WAIT_READY] = (stage >= ZIO_STAGE_READY);
623 zio->io_state[ZIO_WAIT_DONE] = (stage >= ZIO_STAGE_DONE);
624
625 if (zb != NULL)
626 zio->io_bookmark = *zb;
627
628 if (pio != NULL) {
629 if (zio->io_logical == NULL)
630 zio->io_logical = pio->io_logical;
631 if (zio->io_child_type == ZIO_CHILD_GANG)
632 zio->io_gang_leader = pio->io_gang_leader;
633 zio_add_child(pio, zio);
634 }
635
636 taskq_init_ent(&zio->io_tqent);
637
638 return (zio);
639 }
640
641 static void
642 zio_destroy(zio_t *zio)
643 {
644 list_destroy(&zio->io_parent_list);
645 list_destroy(&zio->io_child_list);
646 mutex_destroy(&zio->io_lock);
647 cv_destroy(&zio->io_cv);
648 kmem_cache_free(zio_cache, zio);
649 }
650
651 zio_t *
652 zio_null(zio_t *pio, spa_t *spa, vdev_t *vd, zio_done_func_t *done,
653 void *private, enum zio_flag flags)
654 {
655 zio_t *zio;
656
657 zio = zio_create(pio, spa, 0, NULL, NULL, 0, 0, done, private,
658 ZIO_TYPE_NULL, ZIO_PRIORITY_NOW, flags, vd, 0, NULL,
659 ZIO_STAGE_OPEN, ZIO_INTERLOCK_PIPELINE);
660
661 return (zio);
662 }
663
664 zio_t *
665 zio_root(spa_t *spa, zio_done_func_t *done, void *private, enum zio_flag flags)
666 {
667 return (zio_null(NULL, spa, NULL, done, private, flags));
668 }
669
670 void
671 zfs_blkptr_verify(spa_t *spa, const blkptr_t *bp)
672 {
673 int i;
674
675 if (!DMU_OT_IS_VALID(BP_GET_TYPE(bp))) {
676 zfs_panic_recover("blkptr at %p has invalid TYPE %llu",
677 bp, (longlong_t)BP_GET_TYPE(bp));
678 }
679 if (BP_GET_CHECKSUM(bp) >= ZIO_CHECKSUM_FUNCTIONS ||
680 BP_GET_CHECKSUM(bp) <= ZIO_CHECKSUM_ON) {
681 zfs_panic_recover("blkptr at %p has invalid CHECKSUM %llu",
682 bp, (longlong_t)BP_GET_CHECKSUM(bp));
683 }
684 if (BP_GET_COMPRESS(bp) >= ZIO_COMPRESS_FUNCTIONS ||
685 BP_GET_COMPRESS(bp) <= ZIO_COMPRESS_ON) {
686 zfs_panic_recover("blkptr at %p has invalid COMPRESS %llu",
687 bp, (longlong_t)BP_GET_COMPRESS(bp));
688 }
689 if (BP_GET_LSIZE(bp) > SPA_MAXBLOCKSIZE) {
690 zfs_panic_recover("blkptr at %p has invalid LSIZE %llu",
691 bp, (longlong_t)BP_GET_LSIZE(bp));
692 }
693 if (BP_GET_PSIZE(bp) > SPA_MAXBLOCKSIZE) {
694 zfs_panic_recover("blkptr at %p has invalid PSIZE %llu",
695 bp, (longlong_t)BP_GET_PSIZE(bp));
696 }
697
698 if (BP_IS_EMBEDDED(bp)) {
699 if (BPE_GET_ETYPE(bp) > NUM_BP_EMBEDDED_TYPES) {
700 zfs_panic_recover("blkptr at %p has invalid ETYPE %llu",
701 bp, (longlong_t)BPE_GET_ETYPE(bp));
702 }
703 }
704
705 /*
706 * Pool-specific checks.
707 *
708 * Note: it would be nice to verify that the blk_birth and
709 * BP_PHYSICAL_BIRTH() are not too large. However, spa_freeze()
710 * allows the birth time of log blocks (and dmu_sync()-ed blocks
711 * that are in the log) to be arbitrarily large.
712 */
713 for (i = 0; i < BP_GET_NDVAS(bp); i++) {
714 uint64_t vdevid = DVA_GET_VDEV(&bp->blk_dva[i]);
715 vdev_t *vd;
716 uint64_t offset, asize;
717 if (vdevid >= spa->spa_root_vdev->vdev_children) {
718 zfs_panic_recover("blkptr at %p DVA %u has invalid "
719 "VDEV %llu",
720 bp, i, (longlong_t)vdevid);
721 continue;
722 }
723 vd = spa->spa_root_vdev->vdev_child[vdevid];
724 if (vd == NULL) {
725 zfs_panic_recover("blkptr at %p DVA %u has invalid "
726 "VDEV %llu",
727 bp, i, (longlong_t)vdevid);
728 continue;
729 }
730 if (vd->vdev_ops == &vdev_hole_ops) {
731 zfs_panic_recover("blkptr at %p DVA %u has hole "
732 "VDEV %llu",
733 bp, i, (longlong_t)vdevid);
734 continue;
735 }
736 if (vd->vdev_ops == &vdev_missing_ops) {
737 /*
738 * "missing" vdevs are valid during import, but we
739 * don't have their detailed info (e.g. asize), so
740 * we can't perform any more checks on them.
741 */
742 continue;
743 }
744 offset = DVA_GET_OFFSET(&bp->blk_dva[i]);
745 asize = DVA_GET_ASIZE(&bp->blk_dva[i]);
746 if (BP_IS_GANG(bp))
747 asize = vdev_psize_to_asize(vd, SPA_GANGBLOCKSIZE);
748 if (offset + asize > vd->vdev_asize) {
749 zfs_panic_recover("blkptr at %p DVA %u has invalid "
750 "OFFSET %llu",
751 bp, i, (longlong_t)offset);
752 }
753 }
754 }
755
756 zio_t *
757 zio_read(zio_t *pio, spa_t *spa, const blkptr_t *bp,
758 void *data, uint64_t size, zio_done_func_t *done, void *private,
759 zio_priority_t priority, enum zio_flag flags, const zbookmark_phys_t *zb)
760 {
761 zio_t *zio;
762
763 zfs_blkptr_verify(spa, bp);
764
765 zio = zio_create(pio, spa, BP_PHYSICAL_BIRTH(bp), bp,
766 data, size, size, done, private,
767 ZIO_TYPE_READ, priority, flags, NULL, 0, zb,
768 ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ?
769 ZIO_DDT_CHILD_READ_PIPELINE : ZIO_READ_PIPELINE);
770
771 return (zio);
772 }
773
774 zio_t *
775 zio_write(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
776 void *data, uint64_t lsize, uint64_t psize, const zio_prop_t *zp,
777 zio_done_func_t *ready, zio_done_func_t *children_ready,
778 zio_done_func_t *physdone, zio_done_func_t *done,
779 void *private, zio_priority_t priority, enum zio_flag flags,
780 const zbookmark_phys_t *zb)
781 {
782 zio_t *zio;
783
784 ASSERT(zp->zp_checksum >= ZIO_CHECKSUM_OFF &&
785 zp->zp_checksum < ZIO_CHECKSUM_FUNCTIONS &&
786 zp->zp_compress >= ZIO_COMPRESS_OFF &&
787 zp->zp_compress < ZIO_COMPRESS_FUNCTIONS &&
788 DMU_OT_IS_VALID(zp->zp_type) &&
789 zp->zp_level < 32 &&
790 zp->zp_copies > 0 &&
791 zp->zp_copies <= spa_max_replication(spa));
792
793 zio = zio_create(pio, spa, txg, bp, data, lsize, psize, done, private,
794 ZIO_TYPE_WRITE, priority, flags, NULL, 0, zb,
795 ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ?
796 ZIO_DDT_CHILD_WRITE_PIPELINE : ZIO_WRITE_PIPELINE);
797
798 zio->io_ready = ready;
799 zio->io_children_ready = children_ready;
800 zio->io_physdone = physdone;
801 zio->io_prop = *zp;
802
803 /*
804 * Data can be NULL if we are going to call zio_write_override() to
805 * provide the already-allocated BP. But we may need the data to
806 * verify a dedup hit (if requested). In this case, don't try to
807 * dedup (just take the already-allocated BP verbatim).
808 */
809 if (data == NULL && zio->io_prop.zp_dedup_verify) {
810 zio->io_prop.zp_dedup = zio->io_prop.zp_dedup_verify = B_FALSE;
811 }
812
813 return (zio);
814 }
815
816 zio_t *
817 zio_rewrite(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, void *data,
818 uint64_t size, zio_done_func_t *done, void *private,
819 zio_priority_t priority, enum zio_flag flags, zbookmark_phys_t *zb)
820 {
821 zio_t *zio;
822
823 zio = zio_create(pio, spa, txg, bp, data, size, size, done, private,
824 ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_IO_REWRITE, NULL, 0, zb,
825 ZIO_STAGE_OPEN, ZIO_REWRITE_PIPELINE);
826
827 return (zio);
828 }
829
830 void
831 zio_write_override(zio_t *zio, blkptr_t *bp, int copies, boolean_t nopwrite)
832 {
833 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
834 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
835 ASSERT(zio->io_stage == ZIO_STAGE_OPEN);
836 ASSERT(zio->io_txg == spa_syncing_txg(zio->io_spa));
837
838 /*
839 * We must reset the io_prop to match the values that existed
840 * when the bp was first written by dmu_sync() keeping in mind
841 * that nopwrite and dedup are mutually exclusive.
842 */
843 zio->io_prop.zp_dedup = nopwrite ? B_FALSE : zio->io_prop.zp_dedup;
844 zio->io_prop.zp_nopwrite = nopwrite;
845 zio->io_prop.zp_copies = copies;
846 zio->io_bp_override = bp;
847 }
848
849 void
850 zio_free(spa_t *spa, uint64_t txg, const blkptr_t *bp)
851 {
852
853 /*
854 * The check for EMBEDDED is a performance optimization. We
855 * process the free here (by ignoring it) rather than
856 * putting it on the list and then processing it in zio_free_sync().
857 */
858 if (BP_IS_EMBEDDED(bp))
859 return;
860 metaslab_check_free(spa, bp);
861
862 /*
863 * Frees that are for the currently-syncing txg, are not going to be
864 * deferred, and which will not need to do a read (i.e. not GANG or
865 * DEDUP), can be processed immediately. Otherwise, put them on the
866 * in-memory list for later processing.
867 */
868 if (BP_IS_GANG(bp) || BP_GET_DEDUP(bp) ||
869 txg != spa->spa_syncing_txg ||
870 spa_sync_pass(spa) >= zfs_sync_pass_deferred_free) {
871 bplist_append(&spa->spa_free_bplist[txg & TXG_MASK], bp);
872 } else {
873 VERIFY0(zio_wait(zio_free_sync(NULL, spa, txg, bp, 0)));
874 }
875 }
876
877 zio_t *
878 zio_free_sync(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
879 enum zio_flag flags)
880 {
881 zio_t *zio;
882 enum zio_stage stage = ZIO_FREE_PIPELINE;
883
884 ASSERT(!BP_IS_HOLE(bp));
885 ASSERT(spa_syncing_txg(spa) == txg);
886 ASSERT(spa_sync_pass(spa) < zfs_sync_pass_deferred_free);
887
888 if (BP_IS_EMBEDDED(bp))
889 return (zio_null(pio, spa, NULL, NULL, NULL, 0));
890
891 metaslab_check_free(spa, bp);
892 arc_freed(spa, bp);
893
894 /*
895 * GANG and DEDUP blocks can induce a read (for the gang block header,
896 * or the DDT), so issue them asynchronously so that this thread is
897 * not tied up.
898 */
899 if (BP_IS_GANG(bp) || BP_GET_DEDUP(bp))
900 stage |= ZIO_STAGE_ISSUE_ASYNC;
901
902 zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp),
903 BP_GET_PSIZE(bp), NULL, NULL, ZIO_TYPE_FREE, ZIO_PRIORITY_NOW,
904 flags, NULL, 0, NULL, ZIO_STAGE_OPEN, stage);
905
906 return (zio);
907 }
908
909 zio_t *
910 zio_claim(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
911 zio_done_func_t *done, void *private, enum zio_flag flags)
912 {
913 zio_t *zio;
914
915 dprintf_bp(bp, "claiming in txg %llu", txg);
916
917 if (BP_IS_EMBEDDED(bp))
918 return (zio_null(pio, spa, NULL, NULL, NULL, 0));
919
920 /*
921 * A claim is an allocation of a specific block. Claims are needed
922 * to support immediate writes in the intent log. The issue is that
923 * immediate writes contain committed data, but in a txg that was
924 * *not* committed. Upon opening the pool after an unclean shutdown,
925 * the intent log claims all blocks that contain immediate write data
926 * so that the SPA knows they're in use.
927 *
928 * All claims *must* be resolved in the first txg -- before the SPA
929 * starts allocating blocks -- so that nothing is allocated twice.
930 * If txg == 0 we just verify that the block is claimable.
931 */
932 ASSERT3U(spa->spa_uberblock.ub_rootbp.blk_birth, <, spa_first_txg(spa));
933 ASSERT(txg == spa_first_txg(spa) || txg == 0);
934 ASSERT(!BP_GET_DEDUP(bp) || !spa_writeable(spa)); /* zdb(1M) */
935
936 zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp),
937 BP_GET_PSIZE(bp), done, private, ZIO_TYPE_CLAIM, ZIO_PRIORITY_NOW,
938 flags, NULL, 0, NULL, ZIO_STAGE_OPEN, ZIO_CLAIM_PIPELINE);
939 ASSERT0(zio->io_queued_timestamp);
940
941 return (zio);
942 }
943
944 zio_t *
945 zio_ioctl(zio_t *pio, spa_t *spa, vdev_t *vd, int cmd,
946 zio_done_func_t *done, void *private, enum zio_flag flags)
947 {
948 zio_t *zio;
949 int c;
950
951 if (vd->vdev_children == 0) {
952 zio = zio_create(pio, spa, 0, NULL, NULL, 0, 0, done, private,
953 ZIO_TYPE_IOCTL, ZIO_PRIORITY_NOW, flags, vd, 0, NULL,
954 ZIO_STAGE_OPEN, ZIO_IOCTL_PIPELINE);
955
956 zio->io_cmd = cmd;
957 } else {
958 zio = zio_null(pio, spa, NULL, NULL, NULL, flags);
959
960 for (c = 0; c < vd->vdev_children; c++)
961 zio_nowait(zio_ioctl(zio, spa, vd->vdev_child[c], cmd,
962 done, private, flags));
963 }
964
965 return (zio);
966 }
967
968 zio_t *
969 zio_read_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
970 void *data, int checksum, zio_done_func_t *done, void *private,
971 zio_priority_t priority, enum zio_flag flags, boolean_t labels)
972 {
973 zio_t *zio;
974
975 ASSERT(vd->vdev_children == 0);
976 ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE ||
977 offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
978 ASSERT3U(offset + size, <=, vd->vdev_psize);
979
980 zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, size, done,
981 private, ZIO_TYPE_READ, priority, flags | ZIO_FLAG_PHYSICAL, vd,
982 offset, NULL, ZIO_STAGE_OPEN, ZIO_READ_PHYS_PIPELINE);
983
984 zio->io_prop.zp_checksum = checksum;
985
986 return (zio);
987 }
988
989 zio_t *
990 zio_write_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
991 void *data, int checksum, zio_done_func_t *done, void *private,
992 zio_priority_t priority, enum zio_flag flags, boolean_t labels)
993 {
994 zio_t *zio;
995
996 ASSERT(vd->vdev_children == 0);
997 ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE ||
998 offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
999 ASSERT3U(offset + size, <=, vd->vdev_psize);
1000
1001 zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, size, done,
1002 private, ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_PHYSICAL, vd,
1003 offset, NULL, ZIO_STAGE_OPEN, ZIO_WRITE_PHYS_PIPELINE);
1004
1005 zio->io_prop.zp_checksum = checksum;
1006
1007 if (zio_checksum_table[checksum].ci_flags & ZCHECKSUM_FLAG_EMBEDDED) {
1008 /*
1009 * zec checksums are necessarily destructive -- they modify
1010 * the end of the write buffer to hold the verifier/checksum.
1011 * Therefore, we must make a local copy in case the data is
1012 * being written to multiple places in parallel.
1013 */
1014 void *wbuf = zio_buf_alloc(size);
1015 bcopy(data, wbuf, size);
1016 zio_push_transform(zio, wbuf, size, size, NULL);
1017 }
1018
1019 return (zio);
1020 }
1021
1022 /*
1023 * Create a child I/O to do some work for us.
1024 */
1025 zio_t *
1026 zio_vdev_child_io(zio_t *pio, blkptr_t *bp, vdev_t *vd, uint64_t offset,
1027 void *data, uint64_t size, int type, zio_priority_t priority,
1028 enum zio_flag flags, zio_done_func_t *done, void *private)
1029 {
1030 enum zio_stage pipeline = ZIO_VDEV_CHILD_PIPELINE;
1031 zio_t *zio;
1032
1033 ASSERT(vd->vdev_parent ==
1034 (pio->io_vd ? pio->io_vd : pio->io_spa->spa_root_vdev));
1035
1036 if (type == ZIO_TYPE_READ && bp != NULL) {
1037 /*
1038 * If we have the bp, then the child should perform the
1039 * checksum and the parent need not. This pushes error
1040 * detection as close to the leaves as possible and
1041 * eliminates redundant checksums in the interior nodes.
1042 */
1043 pipeline |= ZIO_STAGE_CHECKSUM_VERIFY;
1044 pio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY;
1045 }
1046
1047 if (vd->vdev_children == 0)
1048 offset += VDEV_LABEL_START_SIZE;
1049
1050 flags |= ZIO_VDEV_CHILD_FLAGS(pio) | ZIO_FLAG_DONT_PROPAGATE;
1051
1052 /*
1053 * If we've decided to do a repair, the write is not speculative --
1054 * even if the original read was.
1055 */
1056 if (flags & ZIO_FLAG_IO_REPAIR)
1057 flags &= ~ZIO_FLAG_SPECULATIVE;
1058
1059 /*
1060 * If we're creating a child I/O that is not associated with a
1061 * top-level vdev, then the child zio is not an allocating I/O.
1062 * If this is a retried I/O then we ignore it since we will
1063 * have already processed the original allocating I/O.
1064 */
1065 if (flags & ZIO_FLAG_IO_ALLOCATING &&
1066 (vd != vd->vdev_top || (flags & ZIO_FLAG_IO_RETRY))) {
1067 metaslab_class_t *mc = spa_normal_class(pio->io_spa);
1068
1069 ASSERT(mc->mc_alloc_throttle_enabled);
1070 ASSERT(type == ZIO_TYPE_WRITE);
1071 ASSERT(priority == ZIO_PRIORITY_ASYNC_WRITE);
1072 ASSERT(!(flags & ZIO_FLAG_IO_REPAIR));
1073 ASSERT(!(pio->io_flags & ZIO_FLAG_IO_REWRITE) ||
1074 pio->io_child_type == ZIO_CHILD_GANG);
1075
1076 flags &= ~ZIO_FLAG_IO_ALLOCATING;
1077 }
1078
1079
1080 zio = zio_create(pio, pio->io_spa, pio->io_txg, bp, data, size, size,
1081 done, private, type, priority, flags, vd, offset, &pio->io_bookmark,
1082 ZIO_STAGE_VDEV_IO_START >> 1, pipeline);
1083 ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_VDEV);
1084
1085 zio->io_physdone = pio->io_physdone;
1086 if (vd->vdev_ops->vdev_op_leaf && zio->io_logical != NULL)
1087 zio->io_logical->io_phys_children++;
1088
1089 return (zio);
1090 }
1091
1092 zio_t *
1093 zio_vdev_delegated_io(vdev_t *vd, uint64_t offset, void *data, uint64_t size,
1094 int type, zio_priority_t priority, enum zio_flag flags,
1095 zio_done_func_t *done, void *private)
1096 {
1097 zio_t *zio;
1098
1099 ASSERT(vd->vdev_ops->vdev_op_leaf);
1100
1101 zio = zio_create(NULL, vd->vdev_spa, 0, NULL,
1102 data, size, size, done, private, type, priority,
1103 flags | ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY | ZIO_FLAG_DELEGATED,
1104 vd, offset, NULL,
1105 ZIO_STAGE_VDEV_IO_START >> 1, ZIO_VDEV_CHILD_PIPELINE);
1106
1107 return (zio);
1108 }
1109
1110 void
1111 zio_flush(zio_t *zio, vdev_t *vd)
1112 {
1113 zio_nowait(zio_ioctl(zio, zio->io_spa, vd, DKIOCFLUSHWRITECACHE,
1114 NULL, NULL,
1115 ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY));
1116 }
1117
1118 void
1119 zio_shrink(zio_t *zio, uint64_t size)
1120 {
1121 ASSERT(zio->io_executor == NULL);
1122 ASSERT(zio->io_orig_size == zio->io_size);
1123 ASSERT(size <= zio->io_size);
1124
1125 /*
1126 * We don't shrink for raidz because of problems with the
1127 * reconstruction when reading back less than the block size.
1128 * Note, BP_IS_RAIDZ() assumes no compression.
1129 */
1130 ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF);
1131 if (!BP_IS_RAIDZ(zio->io_bp)) {
1132 /* we are not doing a raw write */
1133 ASSERT3U(zio->io_size, ==, zio->io_lsize);
1134 zio->io_orig_size = zio->io_size = zio->io_lsize = size;
1135 }
1136 }
1137
1138 /*
1139 * ==========================================================================
1140 * Prepare to read and write logical blocks
1141 * ==========================================================================
1142 */
1143
1144 static int
1145 zio_read_bp_init(zio_t *zio)
1146 {
1147 blkptr_t *bp = zio->io_bp;
1148
1149 if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF &&
1150 zio->io_child_type == ZIO_CHILD_LOGICAL &&
1151 !(zio->io_flags & ZIO_FLAG_RAW)) {
1152 uint64_t psize =
1153 BP_IS_EMBEDDED(bp) ? BPE_GET_PSIZE(bp) : BP_GET_PSIZE(bp);
1154 void *cbuf = zio_buf_alloc(psize);
1155
1156 zio_push_transform(zio, cbuf, psize, psize, zio_decompress);
1157 }
1158
1159 if (BP_IS_EMBEDDED(bp) && BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA) {
1160 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1161 decode_embedded_bp_compressed(bp, zio->io_data);
1162 } else {
1163 ASSERT(!BP_IS_EMBEDDED(bp));
1164 }
1165
1166 if (!DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) && BP_GET_LEVEL(bp) == 0)
1167 zio->io_flags |= ZIO_FLAG_DONT_CACHE;
1168
1169 if (BP_GET_TYPE(bp) == DMU_OT_DDT_ZAP)
1170 zio->io_flags |= ZIO_FLAG_DONT_CACHE;
1171
1172 if (BP_GET_DEDUP(bp) && zio->io_child_type == ZIO_CHILD_LOGICAL)
1173 zio->io_pipeline = ZIO_DDT_READ_PIPELINE;
1174
1175 return (ZIO_PIPELINE_CONTINUE);
1176 }
1177
1178 static int
1179 zio_write_bp_init(zio_t *zio)
1180 {
1181
1182 if (!IO_IS_ALLOCATING(zio))
1183 return (ZIO_PIPELINE_CONTINUE);
1184
1185 ASSERT(zio->io_child_type != ZIO_CHILD_DDT);
1186
1187 if (zio->io_bp_override) {
1188 blkptr_t *bp = zio->io_bp;
1189 zio_prop_t *zp = &zio->io_prop;
1190
1191 ASSERT(bp->blk_birth != zio->io_txg);
1192 ASSERT(BP_GET_DEDUP(zio->io_bp_override) == 0);
1193
1194 *bp = *zio->io_bp_override;
1195 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1196
1197 if (BP_IS_EMBEDDED(bp))
1198 return (ZIO_PIPELINE_CONTINUE);
1199
1200 /*
1201 * If we've been overridden and nopwrite is set then
1202 * set the flag accordingly to indicate that a nopwrite
1203 * has already occurred.
1204 */
1205 if (!BP_IS_HOLE(bp) && zp->zp_nopwrite) {
1206 ASSERT(!zp->zp_dedup);
1207 ASSERT3U(BP_GET_CHECKSUM(bp), ==, zp->zp_checksum);
1208 zio->io_flags |= ZIO_FLAG_NOPWRITE;
1209 return (ZIO_PIPELINE_CONTINUE);
1210 }
1211
1212 ASSERT(!zp->zp_nopwrite);
1213
1214 if (BP_IS_HOLE(bp) || !zp->zp_dedup)
1215 return (ZIO_PIPELINE_CONTINUE);
1216
1217 ASSERT((zio_checksum_table[zp->zp_checksum].ci_flags &
1218 ZCHECKSUM_FLAG_DEDUP) || zp->zp_dedup_verify);
1219
1220 if (BP_GET_CHECKSUM(bp) == zp->zp_checksum) {
1221 BP_SET_DEDUP(bp, 1);
1222 zio->io_pipeline |= ZIO_STAGE_DDT_WRITE;
1223 return (ZIO_PIPELINE_CONTINUE);
1224 }
1225
1226 /*
1227 * We were unable to handle this as an override bp, treat
1228 * it as a regular write I/O.
1229 */
1230 zio->io_bp_override = NULL;
1231 *bp = zio->io_bp_orig;
1232 zio->io_pipeline = zio->io_orig_pipeline;
1233 }
1234
1235 return (ZIO_PIPELINE_CONTINUE);
1236 }
1237
1238 static int
1239 zio_write_compress(zio_t *zio)
1240 {
1241 spa_t *spa = zio->io_spa;
1242 zio_prop_t *zp = &zio->io_prop;
1243 enum zio_compress compress = zp->zp_compress;
1244 blkptr_t *bp = zio->io_bp;
1245 uint64_t lsize = zio->io_lsize;
1246 uint64_t psize = zio->io_size;
1247 int pass = 1;
1248
1249 EQUIV(lsize != psize, (zio->io_flags & ZIO_FLAG_RAW) != 0);
1250
1251 /*
1252 * If our children haven't all reached the ready stage,
1253 * wait for them and then repeat this pipeline stage.
1254 */
1255 if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_READY) ||
1256 zio_wait_for_children(zio, ZIO_CHILD_LOGICAL, ZIO_WAIT_READY))
1257 return (ZIO_PIPELINE_STOP);
1258
1259 if (!IO_IS_ALLOCATING(zio))
1260 return (ZIO_PIPELINE_CONTINUE);
1261
1262 if (zio->io_children_ready != NULL) {
1263 /*
1264 * Now that all our children are ready, run the callback
1265 * associated with this zio in case it wants to modify the
1266 * data to be written.
1267 */
1268 ASSERT3U(zp->zp_level, >, 0);
1269 zio->io_children_ready(zio);
1270 }
1271
1272 ASSERT(zio->io_child_type != ZIO_CHILD_DDT);
1273 ASSERT(zio->io_bp_override == NULL);
1274
1275 if (!BP_IS_HOLE(bp) && bp->blk_birth == zio->io_txg) {
1276 /*
1277 * We're rewriting an existing block, which means we're
1278 * working on behalf of spa_sync(). For spa_sync() to
1279 * converge, it must eventually be the case that we don't
1280 * have to allocate new blocks. But compression changes
1281 * the blocksize, which forces a reallocate, and makes
1282 * convergence take longer. Therefore, after the first
1283 * few passes, stop compressing to ensure convergence.
1284 */
1285 pass = spa_sync_pass(spa);
1286
1287 ASSERT(zio->io_txg == spa_syncing_txg(spa));
1288 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1289 ASSERT(!BP_GET_DEDUP(bp));
1290
1291 if (pass >= zfs_sync_pass_dont_compress)
1292 compress = ZIO_COMPRESS_OFF;
1293
1294 /* Make sure someone doesn't change their mind on overwrites */
1295 ASSERT(BP_IS_EMBEDDED(bp) || MIN(zp->zp_copies + BP_IS_GANG(bp),
1296 spa_max_replication(spa)) == BP_GET_NDVAS(bp));
1297 }
1298
1299 /* If it's a compressed write that is not raw, compress the buffer. */
1300 if (compress != ZIO_COMPRESS_OFF && psize == lsize) {
1301 void *cbuf = zio_buf_alloc(lsize);
1302 psize = zio_compress_data(compress, zio->io_data, cbuf, lsize);
1303 if (psize == 0 || psize == lsize) {
1304 compress = ZIO_COMPRESS_OFF;
1305 zio_buf_free(cbuf, lsize);
1306 } else if (!zp->zp_dedup && psize <= BPE_PAYLOAD_SIZE &&
1307 zp->zp_level == 0 && !DMU_OT_HAS_FILL(zp->zp_type) &&
1308 spa_feature_is_enabled(spa, SPA_FEATURE_EMBEDDED_DATA)) {
1309 encode_embedded_bp_compressed(bp,
1310 cbuf, compress, lsize, psize);
1311 BPE_SET_ETYPE(bp, BP_EMBEDDED_TYPE_DATA);
1312 BP_SET_TYPE(bp, zio->io_prop.zp_type);
1313 BP_SET_LEVEL(bp, zio->io_prop.zp_level);
1314 zio_buf_free(cbuf, lsize);
1315 bp->blk_birth = zio->io_txg;
1316 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1317 ASSERT(spa_feature_is_active(spa,
1318 SPA_FEATURE_EMBEDDED_DATA));
1319 return (ZIO_PIPELINE_CONTINUE);
1320 } else {
1321 /*
1322 * Round up compressed size up to the ashift
1323 * of the smallest-ashift device, and zero the tail.
1324 * This ensures that the compressed size of the BP
1325 * (and thus compressratio property) are correct,
1326 * in that we charge for the padding used to fill out
1327 * the last sector.
1328 */
1329 size_t rounded;
1330
1331 ASSERT3U(spa->spa_min_ashift, >=, SPA_MINBLOCKSHIFT);
1332
1333 rounded = (size_t)P2ROUNDUP(psize,
1334 1ULL << spa->spa_min_ashift);
1335 if (rounded >= lsize) {
1336 compress = ZIO_COMPRESS_OFF;
1337 zio_buf_free(cbuf, lsize);
1338 psize = lsize;
1339 } else {
1340 bzero((char *)cbuf + psize, rounded - psize);
1341 psize = rounded;
1342 zio_push_transform(zio, cbuf,
1343 psize, lsize, NULL);
1344 }
1345 }
1346
1347 /*
1348 * We were unable to handle this as an override bp, treat
1349 * it as a regular write I/O.
1350 */
1351 zio->io_bp_override = NULL;
1352 *bp = zio->io_bp_orig;
1353 zio->io_pipeline = zio->io_orig_pipeline;
1354
1355 } else {
1356 ASSERT3U(psize, !=, 0);
1357
1358 }
1359
1360 /*
1361 * The final pass of spa_sync() must be all rewrites, but the first
1362 * few passes offer a trade-off: allocating blocks defers convergence,
1363 * but newly allocated blocks are sequential, so they can be written
1364 * to disk faster. Therefore, we allow the first few passes of
1365 * spa_sync() to allocate new blocks, but force rewrites after that.
1366 * There should only be a handful of blocks after pass 1 in any case.
1367 */
1368 if (!BP_IS_HOLE(bp) && bp->blk_birth == zio->io_txg &&
1369 BP_GET_PSIZE(bp) == psize &&
1370 pass >= zfs_sync_pass_rewrite) {
1371 enum zio_stage gang_stages = zio->io_pipeline & ZIO_GANG_STAGES;
1372 ASSERT(psize != 0);
1373 zio->io_pipeline = ZIO_REWRITE_PIPELINE | gang_stages;
1374 zio->io_flags |= ZIO_FLAG_IO_REWRITE;
1375 } else {
1376 BP_ZERO(bp);
1377 zio->io_pipeline = ZIO_WRITE_PIPELINE;
1378 }
1379
1380 if (psize == 0) {
1381 if (zio->io_bp_orig.blk_birth != 0 &&
1382 spa_feature_is_active(spa, SPA_FEATURE_HOLE_BIRTH)) {
1383 BP_SET_LSIZE(bp, lsize);
1384 BP_SET_TYPE(bp, zp->zp_type);
1385 BP_SET_LEVEL(bp, zp->zp_level);
1386 BP_SET_BIRTH(bp, zio->io_txg, 0);
1387 }
1388 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1389 } else {
1390 ASSERT(zp->zp_checksum != ZIO_CHECKSUM_GANG_HEADER);
1391 BP_SET_LSIZE(bp, lsize);
1392 BP_SET_TYPE(bp, zp->zp_type);
1393 BP_SET_LEVEL(bp, zp->zp_level);
1394 BP_SET_PSIZE(bp, psize);
1395 BP_SET_COMPRESS(bp, compress);
1396 BP_SET_CHECKSUM(bp, zp->zp_checksum);
1397 BP_SET_DEDUP(bp, zp->zp_dedup);
1398 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
1399 if (zp->zp_dedup) {
1400 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1401 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
1402 zio->io_pipeline = ZIO_DDT_WRITE_PIPELINE;
1403 }
1404 if (zp->zp_nopwrite) {
1405 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1406 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
1407 zio->io_pipeline |= ZIO_STAGE_NOP_WRITE;
1408 }
1409 }
1410 return (ZIO_PIPELINE_CONTINUE);
1411 }
1412
1413 static int
1414 zio_free_bp_init(zio_t *zio)
1415 {
1416 blkptr_t *bp = zio->io_bp;
1417
1418 if (zio->io_child_type == ZIO_CHILD_LOGICAL) {
1419 if (BP_GET_DEDUP(bp))
1420 zio->io_pipeline = ZIO_DDT_FREE_PIPELINE;
1421 }
1422
1423 return (ZIO_PIPELINE_CONTINUE);
1424 }
1425
1426 /*
1427 * ==========================================================================
1428 * Execute the I/O pipeline
1429 * ==========================================================================
1430 */
1431
1432 static void
1433 zio_taskq_dispatch(zio_t *zio, zio_taskq_type_t q, boolean_t cutinline)
1434 {
1435 spa_t *spa = zio->io_spa;
1436 zio_type_t t = zio->io_type;
1437 int flags = (cutinline ? TQ_FRONT : 0);
1438
1439 /*
1440 * If we're a config writer or a probe, the normal issue and
1441 * interrupt threads may all be blocked waiting for the config lock.
1442 * In this case, select the otherwise-unused taskq for ZIO_TYPE_NULL.
1443 */
1444 if (zio->io_flags & (ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_PROBE))
1445 t = ZIO_TYPE_NULL;
1446
1447 /*
1448 * A similar issue exists for the L2ARC write thread until L2ARC 2.0.
1449 */
1450 if (t == ZIO_TYPE_WRITE && zio->io_vd && zio->io_vd->vdev_aux)
1451 t = ZIO_TYPE_NULL;
1452
1453 /*
1454 * If this is a high priority I/O, then use the high priority taskq if
1455 * available.
1456 */
1457 if (zio->io_priority == ZIO_PRIORITY_NOW &&
1458 spa->spa_zio_taskq[t][q + 1].stqs_count != 0)
1459 q++;
1460
1461 ASSERT3U(q, <, ZIO_TASKQ_TYPES);
1462
1463 /*
1464 * NB: We are assuming that the zio can only be dispatched
1465 * to a single taskq at a time. It would be a grievous error
1466 * to dispatch the zio to another taskq at the same time.
1467 */
1468 ASSERT(taskq_empty_ent(&zio->io_tqent));
1469 spa_taskq_dispatch_ent(spa, t, q, (task_func_t *)zio_execute, zio,
1470 flags, &zio->io_tqent);
1471 }
1472
1473 static boolean_t
1474 zio_taskq_member(zio_t *zio, zio_taskq_type_t q)
1475 {
1476 kthread_t *executor = zio->io_executor;
1477 spa_t *spa = zio->io_spa;
1478 zio_type_t t;
1479
1480 for (t = 0; t < ZIO_TYPES; t++) {
1481 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
1482 uint_t i;
1483 for (i = 0; i < tqs->stqs_count; i++) {
1484 if (taskq_member(tqs->stqs_taskq[i], executor))
1485 return (B_TRUE);
1486 }
1487 }
1488
1489 return (B_FALSE);
1490 }
1491
1492 static int
1493 zio_issue_async(zio_t *zio)
1494 {
1495 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE);
1496
1497 return (ZIO_PIPELINE_STOP);
1498 }
1499
1500 void
1501 zio_interrupt(zio_t *zio)
1502 {
1503 zio_taskq_dispatch(zio, ZIO_TASKQ_INTERRUPT, B_FALSE);
1504 }
1505
1506 void
1507 zio_delay_interrupt(zio_t *zio)
1508 {
1509 /*
1510 * The timeout_generic() function isn't defined in userspace, so
1511 * rather than trying to implement the function, the zio delay
1512 * functionality has been disabled for userspace builds.
1513 */
1514
1515 #ifdef _KERNEL
1516 /*
1517 * If io_target_timestamp is zero, then no delay has been registered
1518 * for this IO, thus jump to the end of this function and "skip" the
1519 * delay; issuing it directly to the zio layer.
1520 */
1521 if (zio->io_target_timestamp != 0) {
1522 hrtime_t now = gethrtime();
1523
1524 if (now >= zio->io_target_timestamp) {
1525 /*
1526 * This IO has already taken longer than the target
1527 * delay to complete, so we don't want to delay it
1528 * any longer; we "miss" the delay and issue it
1529 * directly to the zio layer. This is likely due to
1530 * the target latency being set to a value less than
1531 * the underlying hardware can satisfy (e.g. delay
1532 * set to 1ms, but the disks take 10ms to complete an
1533 * IO request).
1534 */
1535
1536 DTRACE_PROBE2(zio__delay__miss, zio_t *, zio,
1537 hrtime_t, now);
1538
1539 zio_interrupt(zio);
1540 } else {
1541 taskqid_t tid;
1542 hrtime_t diff = zio->io_target_timestamp - now;
1543 clock_t expire_at_tick = ddi_get_lbolt() +
1544 NSEC_TO_TICK(diff);
1545
1546 DTRACE_PROBE3(zio__delay__hit, zio_t *, zio,
1547 hrtime_t, now, hrtime_t, diff);
1548
1549 if (NSEC_TO_TICK(diff) == 0) {
1550 /* Our delay is less than a jiffy - just spin */
1551 zfs_sleep_until(zio->io_target_timestamp);
1552 } else {
1553 /*
1554 * Use taskq_dispatch_delay() in the place of
1555 * OpenZFS's timeout_generic().
1556 */
1557 tid = taskq_dispatch_delay(system_taskq,
1558 (task_func_t *) zio_interrupt,
1559 zio, TQ_NOSLEEP, expire_at_tick);
1560 if (!tid) {
1561 /*
1562 * Couldn't allocate a task. Just
1563 * finish the zio without a delay.
1564 */
1565 zio_interrupt(zio);
1566 }
1567 }
1568 }
1569 return;
1570 }
1571 #endif
1572 DTRACE_PROBE1(zio__delay__skip, zio_t *, zio);
1573 zio_interrupt(zio);
1574 }
1575
1576 /*
1577 * Execute the I/O pipeline until one of the following occurs:
1578 * (1) the I/O completes; (2) the pipeline stalls waiting for
1579 * dependent child I/Os; (3) the I/O issues, so we're waiting
1580 * for an I/O completion interrupt; (4) the I/O is delegated by
1581 * vdev-level caching or aggregation; (5) the I/O is deferred
1582 * due to vdev-level queueing; (6) the I/O is handed off to
1583 * another thread. In all cases, the pipeline stops whenever
1584 * there's no CPU work; it never burns a thread in cv_wait_io().
1585 *
1586 * There's no locking on io_stage because there's no legitimate way
1587 * for multiple threads to be attempting to process the same I/O.
1588 */
1589 static zio_pipe_stage_t *zio_pipeline[];
1590
1591 /*
1592 * zio_execute() is a wrapper around the static function
1593 * __zio_execute() so that we can force __zio_execute() to be
1594 * inlined. This reduces stack overhead which is important
1595 * because __zio_execute() is called recursively in several zio
1596 * code paths. zio_execute() itself cannot be inlined because
1597 * it is externally visible.
1598 */
1599 void
1600 zio_execute(zio_t *zio)
1601 {
1602 fstrans_cookie_t cookie;
1603
1604 cookie = spl_fstrans_mark();
1605 __zio_execute(zio);
1606 spl_fstrans_unmark(cookie);
1607 }
1608
1609 /*
1610 * Used to determine if in the current context the stack is sized large
1611 * enough to allow zio_execute() to be called recursively. A minimum
1612 * stack size of 16K is required to avoid needing to re-dispatch the zio.
1613 */
1614 boolean_t
1615 zio_execute_stack_check(zio_t *zio)
1616 {
1617 #if !defined(HAVE_LARGE_STACKS)
1618 dsl_pool_t *dp = spa_get_dsl(zio->io_spa);
1619
1620 /* Executing in txg_sync_thread() context. */
1621 if (dp && curthread == dp->dp_tx.tx_sync_thread)
1622 return (B_TRUE);
1623
1624 /* Pool initialization outside of zio_taskq context. */
1625 if (dp && spa_is_initializing(dp->dp_spa) &&
1626 !zio_taskq_member(zio, ZIO_TASKQ_ISSUE) &&
1627 !zio_taskq_member(zio, ZIO_TASKQ_ISSUE_HIGH))
1628 return (B_TRUE);
1629 #endif /* HAVE_LARGE_STACKS */
1630
1631 return (B_FALSE);
1632 }
1633
1634 __attribute__((always_inline))
1635 static inline void
1636 __zio_execute(zio_t *zio)
1637 {
1638 zio->io_executor = curthread;
1639
1640 ASSERT3U(zio->io_queued_timestamp, >, 0);
1641
1642 while (zio->io_stage < ZIO_STAGE_DONE) {
1643 enum zio_stage pipeline = zio->io_pipeline;
1644 enum zio_stage stage = zio->io_stage;
1645 int rv;
1646
1647 ASSERT(!MUTEX_HELD(&zio->io_lock));
1648 ASSERT(ISP2(stage));
1649 ASSERT(zio->io_stall == NULL);
1650
1651 do {
1652 stage <<= 1;
1653 } while ((stage & pipeline) == 0);
1654
1655 ASSERT(stage <= ZIO_STAGE_DONE);
1656
1657 /*
1658 * If we are in interrupt context and this pipeline stage
1659 * will grab a config lock that is held across I/O,
1660 * or may wait for an I/O that needs an interrupt thread
1661 * to complete, issue async to avoid deadlock.
1662 *
1663 * For VDEV_IO_START, we cut in line so that the io will
1664 * be sent to disk promptly.
1665 */
1666 if ((stage & ZIO_BLOCKING_STAGES) && zio->io_vd == NULL &&
1667 zio_taskq_member(zio, ZIO_TASKQ_INTERRUPT)) {
1668 boolean_t cut = (stage == ZIO_STAGE_VDEV_IO_START) ?
1669 zio_requeue_io_start_cut_in_line : B_FALSE;
1670 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, cut);
1671 return;
1672 }
1673
1674 /*
1675 * If the current context doesn't have large enough stacks
1676 * the zio must be issued asynchronously to prevent overflow.
1677 */
1678 if (zio_execute_stack_check(zio)) {
1679 boolean_t cut = (stage == ZIO_STAGE_VDEV_IO_START) ?
1680 zio_requeue_io_start_cut_in_line : B_FALSE;
1681 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, cut);
1682 return;
1683 }
1684
1685 zio->io_stage = stage;
1686 zio->io_pipeline_trace |= zio->io_stage;
1687 rv = zio_pipeline[highbit64(stage) - 1](zio);
1688
1689 if (rv == ZIO_PIPELINE_STOP)
1690 return;
1691
1692 ASSERT(rv == ZIO_PIPELINE_CONTINUE);
1693 }
1694 }
1695
1696
1697 /*
1698 * ==========================================================================
1699 * Initiate I/O, either sync or async
1700 * ==========================================================================
1701 */
1702 int
1703 zio_wait(zio_t *zio)
1704 {
1705 int error;
1706
1707 ASSERT(zio->io_stage == ZIO_STAGE_OPEN);
1708 ASSERT(zio->io_executor == NULL);
1709
1710 zio->io_waiter = curthread;
1711 ASSERT0(zio->io_queued_timestamp);
1712 zio->io_queued_timestamp = gethrtime();
1713
1714 __zio_execute(zio);
1715
1716 mutex_enter(&zio->io_lock);
1717 while (zio->io_executor != NULL)
1718 cv_wait_io(&zio->io_cv, &zio->io_lock);
1719 mutex_exit(&zio->io_lock);
1720
1721 error = zio->io_error;
1722 zio_destroy(zio);
1723
1724 return (error);
1725 }
1726
1727 void
1728 zio_nowait(zio_t *zio)
1729 {
1730 ASSERT(zio->io_executor == NULL);
1731
1732 if (zio->io_child_type == ZIO_CHILD_LOGICAL &&
1733 zio_unique_parent(zio) == NULL) {
1734 zio_t *pio;
1735
1736 /*
1737 * This is a logical async I/O with no parent to wait for it.
1738 * We add it to the spa_async_root_zio "Godfather" I/O which
1739 * will ensure they complete prior to unloading the pool.
1740 */
1741 spa_t *spa = zio->io_spa;
1742 kpreempt_disable();
1743 pio = spa->spa_async_zio_root[CPU_SEQID];
1744 kpreempt_enable();
1745
1746 zio_add_child(pio, zio);
1747 }
1748
1749 ASSERT0(zio->io_queued_timestamp);
1750 zio->io_queued_timestamp = gethrtime();
1751 __zio_execute(zio);
1752 }
1753
1754 /*
1755 * ==========================================================================
1756 * Reexecute or suspend/resume failed I/O
1757 * ==========================================================================
1758 */
1759
1760 static void
1761 zio_reexecute(zio_t *pio)
1762 {
1763 zio_t *cio, *cio_next;
1764 int c, w;
1765 zio_link_t *zl = NULL;
1766
1767 ASSERT(pio->io_child_type == ZIO_CHILD_LOGICAL);
1768 ASSERT(pio->io_orig_stage == ZIO_STAGE_OPEN);
1769 ASSERT(pio->io_gang_leader == NULL);
1770 ASSERT(pio->io_gang_tree == NULL);
1771
1772 pio->io_flags = pio->io_orig_flags;
1773 pio->io_stage = pio->io_orig_stage;
1774 pio->io_pipeline = pio->io_orig_pipeline;
1775 pio->io_reexecute = 0;
1776 pio->io_flags |= ZIO_FLAG_REEXECUTED;
1777 pio->io_pipeline_trace = 0;
1778 pio->io_error = 0;
1779 for (w = 0; w < ZIO_WAIT_TYPES; w++)
1780 pio->io_state[w] = 0;
1781 for (c = 0; c < ZIO_CHILD_TYPES; c++)
1782 pio->io_child_error[c] = 0;
1783
1784 if (IO_IS_ALLOCATING(pio))
1785 BP_ZERO(pio->io_bp);
1786
1787 /*
1788 * As we reexecute pio's children, new children could be created.
1789 * New children go to the head of pio's io_child_list, however,
1790 * so we will (correctly) not reexecute them. The key is that
1791 * the remainder of pio's io_child_list, from 'cio_next' onward,
1792 * cannot be affected by any side effects of reexecuting 'cio'.
1793 */
1794 for (cio = zio_walk_children(pio, &zl); cio != NULL; cio = cio_next) {
1795 cio_next = zio_walk_children(pio, &zl);
1796 mutex_enter(&pio->io_lock);
1797 for (w = 0; w < ZIO_WAIT_TYPES; w++)
1798 pio->io_children[cio->io_child_type][w]++;
1799 mutex_exit(&pio->io_lock);
1800 zio_reexecute(cio);
1801 }
1802
1803 /*
1804 * Now that all children have been reexecuted, execute the parent.
1805 * We don't reexecute "The Godfather" I/O here as it's the
1806 * responsibility of the caller to wait on him.
1807 */
1808 if (!(pio->io_flags & ZIO_FLAG_GODFATHER)) {
1809 pio->io_queued_timestamp = gethrtime();
1810 __zio_execute(pio);
1811 }
1812 }
1813
1814 void
1815 zio_suspend(spa_t *spa, zio_t *zio)
1816 {
1817 if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_PANIC)
1818 fm_panic("Pool '%s' has encountered an uncorrectable I/O "
1819 "failure and the failure mode property for this pool "
1820 "is set to panic.", spa_name(spa));
1821
1822 cmn_err(CE_WARN, "Pool '%s' has encountered an uncorrectable I/O "
1823 "failure and has been suspended.\n", spa_name(spa));
1824
1825 zfs_ereport_post(FM_EREPORT_ZFS_IO_FAILURE, spa, NULL, NULL, 0, 0);
1826
1827 mutex_enter(&spa->spa_suspend_lock);
1828
1829 if (spa->spa_suspend_zio_root == NULL)
1830 spa->spa_suspend_zio_root = zio_root(spa, NULL, NULL,
1831 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
1832 ZIO_FLAG_GODFATHER);
1833
1834 spa->spa_suspended = B_TRUE;
1835
1836 if (zio != NULL) {
1837 ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER));
1838 ASSERT(zio != spa->spa_suspend_zio_root);
1839 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1840 ASSERT(zio_unique_parent(zio) == NULL);
1841 ASSERT(zio->io_stage == ZIO_STAGE_DONE);
1842 zio_add_child(spa->spa_suspend_zio_root, zio);
1843 }
1844
1845 mutex_exit(&spa->spa_suspend_lock);
1846 }
1847
1848 int
1849 zio_resume(spa_t *spa)
1850 {
1851 zio_t *pio;
1852
1853 /*
1854 * Reexecute all previously suspended i/o.
1855 */
1856 mutex_enter(&spa->spa_suspend_lock);
1857 spa->spa_suspended = B_FALSE;
1858 cv_broadcast(&spa->spa_suspend_cv);
1859 pio = spa->spa_suspend_zio_root;
1860 spa->spa_suspend_zio_root = NULL;
1861 mutex_exit(&spa->spa_suspend_lock);
1862
1863 if (pio == NULL)
1864 return (0);
1865
1866 zio_reexecute(pio);
1867 return (zio_wait(pio));
1868 }
1869
1870 void
1871 zio_resume_wait(spa_t *spa)
1872 {
1873 mutex_enter(&spa->spa_suspend_lock);
1874 while (spa_suspended(spa))
1875 cv_wait(&spa->spa_suspend_cv, &spa->spa_suspend_lock);
1876 mutex_exit(&spa->spa_suspend_lock);
1877 }
1878
1879 /*
1880 * ==========================================================================
1881 * Gang blocks.
1882 *
1883 * A gang block is a collection of small blocks that looks to the DMU
1884 * like one large block. When zio_dva_allocate() cannot find a block
1885 * of the requested size, due to either severe fragmentation or the pool
1886 * being nearly full, it calls zio_write_gang_block() to construct the
1887 * block from smaller fragments.
1888 *
1889 * A gang block consists of a gang header (zio_gbh_phys_t) and up to
1890 * three (SPA_GBH_NBLKPTRS) gang members. The gang header is just like
1891 * an indirect block: it's an array of block pointers. It consumes
1892 * only one sector and hence is allocatable regardless of fragmentation.
1893 * The gang header's bps point to its gang members, which hold the data.
1894 *
1895 * Gang blocks are self-checksumming, using the bp's <vdev, offset, txg>
1896 * as the verifier to ensure uniqueness of the SHA256 checksum.
1897 * Critically, the gang block bp's blk_cksum is the checksum of the data,
1898 * not the gang header. This ensures that data block signatures (needed for
1899 * deduplication) are independent of how the block is physically stored.
1900 *
1901 * Gang blocks can be nested: a gang member may itself be a gang block.
1902 * Thus every gang block is a tree in which root and all interior nodes are
1903 * gang headers, and the leaves are normal blocks that contain user data.
1904 * The root of the gang tree is called the gang leader.
1905 *
1906 * To perform any operation (read, rewrite, free, claim) on a gang block,
1907 * zio_gang_assemble() first assembles the gang tree (minus data leaves)
1908 * in the io_gang_tree field of the original logical i/o by recursively
1909 * reading the gang leader and all gang headers below it. This yields
1910 * an in-core tree containing the contents of every gang header and the
1911 * bps for every constituent of the gang block.
1912 *
1913 * With the gang tree now assembled, zio_gang_issue() just walks the gang tree
1914 * and invokes a callback on each bp. To free a gang block, zio_gang_issue()
1915 * calls zio_free_gang() -- a trivial wrapper around zio_free() -- for each bp.
1916 * zio_claim_gang() provides a similarly trivial wrapper for zio_claim().
1917 * zio_read_gang() is a wrapper around zio_read() that omits reading gang
1918 * headers, since we already have those in io_gang_tree. zio_rewrite_gang()
1919 * performs a zio_rewrite() of the data or, for gang headers, a zio_rewrite()
1920 * of the gang header plus zio_checksum_compute() of the data to update the
1921 * gang header's blk_cksum as described above.
1922 *
1923 * The two-phase assemble/issue model solves the problem of partial failure --
1924 * what if you'd freed part of a gang block but then couldn't read the
1925 * gang header for another part? Assembling the entire gang tree first
1926 * ensures that all the necessary gang header I/O has succeeded before
1927 * starting the actual work of free, claim, or write. Once the gang tree
1928 * is assembled, free and claim are in-memory operations that cannot fail.
1929 *
1930 * In the event that a gang write fails, zio_dva_unallocate() walks the
1931 * gang tree to immediately free (i.e. insert back into the space map)
1932 * everything we've allocated. This ensures that we don't get ENOSPC
1933 * errors during repeated suspend/resume cycles due to a flaky device.
1934 *
1935 * Gang rewrites only happen during sync-to-convergence. If we can't assemble
1936 * the gang tree, we won't modify the block, so we can safely defer the free
1937 * (knowing that the block is still intact). If we *can* assemble the gang
1938 * tree, then even if some of the rewrites fail, zio_dva_unallocate() will free
1939 * each constituent bp and we can allocate a new block on the next sync pass.
1940 *
1941 * In all cases, the gang tree allows complete recovery from partial failure.
1942 * ==========================================================================
1943 */
1944
1945 static zio_t *
1946 zio_read_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
1947 {
1948 if (gn != NULL)
1949 return (pio);
1950
1951 return (zio_read(pio, pio->io_spa, bp, data, BP_GET_PSIZE(bp),
1952 NULL, NULL, pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio),
1953 &pio->io_bookmark));
1954 }
1955
1956 zio_t *
1957 zio_rewrite_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
1958 {
1959 zio_t *zio;
1960
1961 if (gn != NULL) {
1962 zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp,
1963 gn->gn_gbh, SPA_GANGBLOCKSIZE, NULL, NULL, pio->io_priority,
1964 ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
1965 /*
1966 * As we rewrite each gang header, the pipeline will compute
1967 * a new gang block header checksum for it; but no one will
1968 * compute a new data checksum, so we do that here. The one
1969 * exception is the gang leader: the pipeline already computed
1970 * its data checksum because that stage precedes gang assembly.
1971 * (Presently, nothing actually uses interior data checksums;
1972 * this is just good hygiene.)
1973 */
1974 if (gn != pio->io_gang_leader->io_gang_tree) {
1975 zio_checksum_compute(zio, BP_GET_CHECKSUM(bp),
1976 data, BP_GET_PSIZE(bp));
1977 }
1978 /*
1979 * If we are here to damage data for testing purposes,
1980 * leave the GBH alone so that we can detect the damage.
1981 */
1982 if (pio->io_gang_leader->io_flags & ZIO_FLAG_INDUCE_DAMAGE)
1983 zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES;
1984 } else {
1985 zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp,
1986 data, BP_GET_PSIZE(bp), NULL, NULL, pio->io_priority,
1987 ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
1988 }
1989
1990 return (zio);
1991 }
1992
1993 /* ARGSUSED */
1994 zio_t *
1995 zio_free_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
1996 {
1997 return (zio_free_sync(pio, pio->io_spa, pio->io_txg, bp,
1998 ZIO_GANG_CHILD_FLAGS(pio)));
1999 }
2000
2001 /* ARGSUSED */
2002 zio_t *
2003 zio_claim_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
2004 {
2005 return (zio_claim(pio, pio->io_spa, pio->io_txg, bp,
2006 NULL, NULL, ZIO_GANG_CHILD_FLAGS(pio)));
2007 }
2008
2009 static zio_gang_issue_func_t *zio_gang_issue_func[ZIO_TYPES] = {
2010 NULL,
2011 zio_read_gang,
2012 zio_rewrite_gang,
2013 zio_free_gang,
2014 zio_claim_gang,
2015 NULL
2016 };
2017
2018 static void zio_gang_tree_assemble_done(zio_t *zio);
2019
2020 static zio_gang_node_t *
2021 zio_gang_node_alloc(zio_gang_node_t **gnpp)
2022 {
2023 zio_gang_node_t *gn;
2024
2025 ASSERT(*gnpp == NULL);
2026
2027 gn = kmem_zalloc(sizeof (*gn), KM_SLEEP);
2028 gn->gn_gbh = zio_buf_alloc(SPA_GANGBLOCKSIZE);
2029 *gnpp = gn;
2030
2031 return (gn);
2032 }
2033
2034 static void
2035 zio_gang_node_free(zio_gang_node_t **gnpp)
2036 {
2037 zio_gang_node_t *gn = *gnpp;
2038 int g;
2039
2040 for (g = 0; g < SPA_GBH_NBLKPTRS; g++)
2041 ASSERT(gn->gn_child[g] == NULL);
2042
2043 zio_buf_free(gn->gn_gbh, SPA_GANGBLOCKSIZE);
2044 kmem_free(gn, sizeof (*gn));
2045 *gnpp = NULL;
2046 }
2047
2048 static void
2049 zio_gang_tree_free(zio_gang_node_t **gnpp)
2050 {
2051 zio_gang_node_t *gn = *gnpp;
2052 int g;
2053
2054 if (gn == NULL)
2055 return;
2056
2057 for (g = 0; g < SPA_GBH_NBLKPTRS; g++)
2058 zio_gang_tree_free(&gn->gn_child[g]);
2059
2060 zio_gang_node_free(gnpp);
2061 }
2062
2063 static void
2064 zio_gang_tree_assemble(zio_t *gio, blkptr_t *bp, zio_gang_node_t **gnpp)
2065 {
2066 zio_gang_node_t *gn = zio_gang_node_alloc(gnpp);
2067
2068 ASSERT(gio->io_gang_leader == gio);
2069 ASSERT(BP_IS_GANG(bp));
2070
2071 zio_nowait(zio_read(gio, gio->io_spa, bp, gn->gn_gbh,
2072 SPA_GANGBLOCKSIZE, zio_gang_tree_assemble_done, gn,
2073 gio->io_priority, ZIO_GANG_CHILD_FLAGS(gio), &gio->io_bookmark));
2074 }
2075
2076 static void
2077 zio_gang_tree_assemble_done(zio_t *zio)
2078 {
2079 zio_t *gio = zio->io_gang_leader;
2080 zio_gang_node_t *gn = zio->io_private;
2081 blkptr_t *bp = zio->io_bp;
2082 int g;
2083
2084 ASSERT(gio == zio_unique_parent(zio));
2085 ASSERT(zio->io_child_count == 0);
2086
2087 if (zio->io_error)
2088 return;
2089
2090 if (BP_SHOULD_BYTESWAP(bp))
2091 byteswap_uint64_array(zio->io_data, zio->io_size);
2092
2093 ASSERT(zio->io_data == gn->gn_gbh);
2094 ASSERT(zio->io_size == SPA_GANGBLOCKSIZE);
2095 ASSERT(gn->gn_gbh->zg_tail.zec_magic == ZEC_MAGIC);
2096
2097 for (g = 0; g < SPA_GBH_NBLKPTRS; g++) {
2098 blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g];
2099 if (!BP_IS_GANG(gbp))
2100 continue;
2101 zio_gang_tree_assemble(gio, gbp, &gn->gn_child[g]);
2102 }
2103 }
2104
2105 static void
2106 zio_gang_tree_issue(zio_t *pio, zio_gang_node_t *gn, blkptr_t *bp, void *data)
2107 {
2108 zio_t *gio = pio->io_gang_leader;
2109 zio_t *zio;
2110 int g;
2111
2112 ASSERT(BP_IS_GANG(bp) == !!gn);
2113 ASSERT(BP_GET_CHECKSUM(bp) == BP_GET_CHECKSUM(gio->io_bp));
2114 ASSERT(BP_GET_LSIZE(bp) == BP_GET_PSIZE(bp) || gn == gio->io_gang_tree);
2115
2116 /*
2117 * If you're a gang header, your data is in gn->gn_gbh.
2118 * If you're a gang member, your data is in 'data' and gn == NULL.
2119 */
2120 zio = zio_gang_issue_func[gio->io_type](pio, bp, gn, data);
2121
2122 if (gn != NULL) {
2123 ASSERT(gn->gn_gbh->zg_tail.zec_magic == ZEC_MAGIC);
2124
2125 for (g = 0; g < SPA_GBH_NBLKPTRS; g++) {
2126 blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g];
2127 if (BP_IS_HOLE(gbp))
2128 continue;
2129 zio_gang_tree_issue(zio, gn->gn_child[g], gbp, data);
2130 data = (char *)data + BP_GET_PSIZE(gbp);
2131 }
2132 }
2133
2134 if (gn == gio->io_gang_tree)
2135 ASSERT3P((char *)gio->io_data + gio->io_size, ==, data);
2136
2137 if (zio != pio)
2138 zio_nowait(zio);
2139 }
2140
2141 static int
2142 zio_gang_assemble(zio_t *zio)
2143 {
2144 blkptr_t *bp = zio->io_bp;
2145
2146 ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == NULL);
2147 ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
2148
2149 zio->io_gang_leader = zio;
2150
2151 zio_gang_tree_assemble(zio, bp, &zio->io_gang_tree);
2152
2153 return (ZIO_PIPELINE_CONTINUE);
2154 }
2155
2156 static int
2157 zio_gang_issue(zio_t *zio)
2158 {
2159 blkptr_t *bp = zio->io_bp;
2160
2161 if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_DONE))
2162 return (ZIO_PIPELINE_STOP);
2163
2164 ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == zio);
2165 ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
2166
2167 if (zio->io_child_error[ZIO_CHILD_GANG] == 0)
2168 zio_gang_tree_issue(zio, zio->io_gang_tree, bp, zio->io_data);
2169 else
2170 zio_gang_tree_free(&zio->io_gang_tree);
2171
2172 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2173
2174 return (ZIO_PIPELINE_CONTINUE);
2175 }
2176
2177 static void
2178 zio_write_gang_member_ready(zio_t *zio)
2179 {
2180 zio_t *pio = zio_unique_parent(zio);
2181 dva_t *cdva = zio->io_bp->blk_dva;
2182 dva_t *pdva = pio->io_bp->blk_dva;
2183 uint64_t asize;
2184 int d;
2185 ASSERTV(zio_t *gio = zio->io_gang_leader);
2186
2187 if (BP_IS_HOLE(zio->io_bp))
2188 return;
2189
2190 ASSERT(BP_IS_HOLE(&zio->io_bp_orig));
2191
2192 ASSERT(zio->io_child_type == ZIO_CHILD_GANG);
2193 ASSERT3U(zio->io_prop.zp_copies, ==, gio->io_prop.zp_copies);
2194 ASSERT3U(zio->io_prop.zp_copies, <=, BP_GET_NDVAS(zio->io_bp));
2195 ASSERT3U(pio->io_prop.zp_copies, <=, BP_GET_NDVAS(pio->io_bp));
2196 ASSERT3U(BP_GET_NDVAS(zio->io_bp), <=, BP_GET_NDVAS(pio->io_bp));
2197
2198 mutex_enter(&pio->io_lock);
2199 for (d = 0; d < BP_GET_NDVAS(zio->io_bp); d++) {
2200 ASSERT(DVA_GET_GANG(&pdva[d]));
2201 asize = DVA_GET_ASIZE(&pdva[d]);
2202 asize += DVA_GET_ASIZE(&cdva[d]);
2203 DVA_SET_ASIZE(&pdva[d], asize);
2204 }
2205 mutex_exit(&pio->io_lock);
2206 }
2207
2208 static int
2209 zio_write_gang_block(zio_t *pio)
2210 {
2211 spa_t *spa = pio->io_spa;
2212 metaslab_class_t *mc = spa_normal_class(spa);
2213 blkptr_t *bp = pio->io_bp;
2214 zio_t *gio = pio->io_gang_leader;
2215 zio_t *zio;
2216 zio_gang_node_t *gn, **gnpp;
2217 zio_gbh_phys_t *gbh;
2218 uint64_t txg = pio->io_txg;
2219 uint64_t resid = pio->io_size;
2220 uint64_t lsize;
2221 int copies = gio->io_prop.zp_copies;
2222 int gbh_copies = MIN(copies + 1, spa_max_replication(spa));
2223 zio_prop_t zp;
2224 int g, error;
2225
2226 int flags = METASLAB_HINTBP_FAVOR | METASLAB_GANG_HEADER;
2227 if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
2228 ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
2229 ASSERT(!(pio->io_flags & ZIO_FLAG_NODATA));
2230
2231 flags |= METASLAB_ASYNC_ALLOC;
2232 VERIFY(refcount_held(&mc->mc_alloc_slots, pio));
2233
2234 /*
2235 * The logical zio has already placed a reservation for
2236 * 'copies' allocation slots but gang blocks may require
2237 * additional copies. These additional copies
2238 * (i.e. gbh_copies - copies) are guaranteed to succeed
2239 * since metaslab_class_throttle_reserve() always allows
2240 * additional reservations for gang blocks.
2241 */
2242 VERIFY(metaslab_class_throttle_reserve(mc, gbh_copies - copies,
2243 pio, flags));
2244 }
2245
2246 error = metaslab_alloc(spa, mc, SPA_GANGBLOCKSIZE,
2247 bp, gbh_copies, txg, pio == gio ? NULL : gio->io_bp, flags, pio);
2248 if (error) {
2249 if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
2250 ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
2251 ASSERT(!(pio->io_flags & ZIO_FLAG_NODATA));
2252
2253 /*
2254 * If we failed to allocate the gang block header then
2255 * we remove any additional allocation reservations that
2256 * we placed here. The original reservation will
2257 * be removed when the logical I/O goes to the ready
2258 * stage.
2259 */
2260 metaslab_class_throttle_unreserve(mc,
2261 gbh_copies - copies, pio);
2262 }
2263
2264 pio->io_error = error;
2265 return (ZIO_PIPELINE_CONTINUE);
2266 }
2267
2268 if (pio == gio) {
2269 gnpp = &gio->io_gang_tree;
2270 } else {
2271 gnpp = pio->io_private;
2272 ASSERT(pio->io_ready == zio_write_gang_member_ready);
2273 }
2274
2275 gn = zio_gang_node_alloc(gnpp);
2276 gbh = gn->gn_gbh;
2277 bzero(gbh, SPA_GANGBLOCKSIZE);
2278
2279 /*
2280 * Create the gang header.
2281 */
2282 zio = zio_rewrite(pio, spa, txg, bp, gbh, SPA_GANGBLOCKSIZE, NULL, NULL,
2283 pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
2284
2285 /*
2286 * Create and nowait the gang children.
2287 */
2288 for (g = 0; resid != 0; resid -= lsize, g++) {
2289 zio_t *cio;
2290
2291 lsize = P2ROUNDUP(resid / (SPA_GBH_NBLKPTRS - g),
2292 SPA_MINBLOCKSIZE);
2293 ASSERT(lsize >= SPA_MINBLOCKSIZE && lsize <= resid);
2294
2295 zp.zp_checksum = gio->io_prop.zp_checksum;
2296 zp.zp_compress = ZIO_COMPRESS_OFF;
2297 zp.zp_type = DMU_OT_NONE;
2298 zp.zp_level = 0;
2299 zp.zp_copies = gio->io_prop.zp_copies;
2300 zp.zp_dedup = B_FALSE;
2301 zp.zp_dedup_verify = B_FALSE;
2302 zp.zp_nopwrite = B_FALSE;
2303
2304 cio = zio_write(zio, spa, txg, &gbh->zg_blkptr[g],
2305 (char *)pio->io_data + (pio->io_size - resid), lsize,
2306 lsize, &zp, zio_write_gang_member_ready, NULL, NULL, NULL,
2307 &gn->gn_child[g], pio->io_priority,
2308 ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
2309
2310 if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
2311 ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
2312 ASSERT(!(pio->io_flags & ZIO_FLAG_NODATA));
2313
2314 /*
2315 * Gang children won't throttle but we should
2316 * account for their work, so reserve an allocation
2317 * slot for them here.
2318 */
2319 VERIFY(metaslab_class_throttle_reserve(mc,
2320 zp.zp_copies, cio, flags));
2321 }
2322 zio_nowait(cio);
2323
2324 }
2325
2326 /*
2327 * Set pio's pipeline to just wait for zio to finish.
2328 */
2329 pio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2330
2331 /*
2332 * We didn't allocate this bp, so make sure it doesn't get unmarked.
2333 */
2334 pio->io_flags &= ~ZIO_FLAG_FASTWRITE;
2335
2336 zio_nowait(zio);
2337
2338 return (ZIO_PIPELINE_CONTINUE);
2339 }
2340
2341 /*
2342 * The zio_nop_write stage in the pipeline determines if allocating a
2343 * new bp is necessary. The nopwrite feature can handle writes in
2344 * either syncing or open context (i.e. zil writes) and as a result is
2345 * mutually exclusive with dedup.
2346 *
2347 * By leveraging a cryptographically secure checksum, such as SHA256, we
2348 * can compare the checksums of the new data and the old to determine if
2349 * allocating a new block is required. Note that our requirements for
2350 * cryptographic strength are fairly weak: there can't be any accidental
2351 * hash collisions, but we don't need to be secure against intentional
2352 * (malicious) collisions. To trigger a nopwrite, you have to be able
2353 * to write the file to begin with, and triggering an incorrect (hash
2354 * collision) nopwrite is no worse than simply writing to the file.
2355 * That said, there are no known attacks against the checksum algorithms
2356 * used for nopwrite, assuming that the salt and the checksums
2357 * themselves remain secret.
2358 */
2359 static int
2360 zio_nop_write(zio_t *zio)
2361 {
2362 blkptr_t *bp = zio->io_bp;
2363 blkptr_t *bp_orig = &zio->io_bp_orig;
2364 zio_prop_t *zp = &zio->io_prop;
2365
2366 ASSERT(BP_GET_LEVEL(bp) == 0);
2367 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
2368 ASSERT(zp->zp_nopwrite);
2369 ASSERT(!zp->zp_dedup);
2370 ASSERT(zio->io_bp_override == NULL);
2371 ASSERT(IO_IS_ALLOCATING(zio));
2372
2373 /*
2374 * Check to see if the original bp and the new bp have matching
2375 * characteristics (i.e. same checksum, compression algorithms, etc).
2376 * If they don't then just continue with the pipeline which will
2377 * allocate a new bp.
2378 */
2379 if (BP_IS_HOLE(bp_orig) ||
2380 !(zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_flags &
2381 ZCHECKSUM_FLAG_NOPWRITE) ||
2382 BP_GET_CHECKSUM(bp) != BP_GET_CHECKSUM(bp_orig) ||
2383 BP_GET_COMPRESS(bp) != BP_GET_COMPRESS(bp_orig) ||
2384 BP_GET_DEDUP(bp) != BP_GET_DEDUP(bp_orig) ||
2385 zp->zp_copies != BP_GET_NDVAS(bp_orig))
2386 return (ZIO_PIPELINE_CONTINUE);
2387
2388 /*
2389 * If the checksums match then reset the pipeline so that we
2390 * avoid allocating a new bp and issuing any I/O.
2391 */
2392 if (ZIO_CHECKSUM_EQUAL(bp->blk_cksum, bp_orig->blk_cksum)) {
2393 ASSERT(zio_checksum_table[zp->zp_checksum].ci_flags &
2394 ZCHECKSUM_FLAG_NOPWRITE);
2395 ASSERT3U(BP_GET_PSIZE(bp), ==, BP_GET_PSIZE(bp_orig));
2396 ASSERT3U(BP_GET_LSIZE(bp), ==, BP_GET_LSIZE(bp_orig));
2397 ASSERT(zp->zp_compress != ZIO_COMPRESS_OFF);
2398 ASSERT(bcmp(&bp->blk_prop, &bp_orig->blk_prop,
2399 sizeof (uint64_t)) == 0);
2400
2401 *bp = *bp_orig;
2402 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2403 zio->io_flags |= ZIO_FLAG_NOPWRITE;
2404 }
2405
2406 return (ZIO_PIPELINE_CONTINUE);
2407 }
2408
2409 /*
2410 * ==========================================================================
2411 * Dedup
2412 * ==========================================================================
2413 */
2414 static void
2415 zio_ddt_child_read_done(zio_t *zio)
2416 {
2417 blkptr_t *bp = zio->io_bp;
2418 ddt_entry_t *dde = zio->io_private;
2419 ddt_phys_t *ddp;
2420 zio_t *pio = zio_unique_parent(zio);
2421
2422 mutex_enter(&pio->io_lock);
2423 ddp = ddt_phys_select(dde, bp);
2424 if (zio->io_error == 0)
2425 ddt_phys_clear(ddp); /* this ddp doesn't need repair */
2426 if (zio->io_error == 0 && dde->dde_repair_data == NULL)
2427 dde->dde_repair_data = zio->io_data;
2428 else
2429 zio_buf_free(zio->io_data, zio->io_size);
2430 mutex_exit(&pio->io_lock);
2431 }
2432
2433 static int
2434 zio_ddt_read_start(zio_t *zio)
2435 {
2436 blkptr_t *bp = zio->io_bp;
2437 int p;
2438
2439 ASSERT(BP_GET_DEDUP(bp));
2440 ASSERT(BP_GET_PSIZE(bp) == zio->io_size);
2441 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2442
2443 if (zio->io_child_error[ZIO_CHILD_DDT]) {
2444 ddt_t *ddt = ddt_select(zio->io_spa, bp);
2445 ddt_entry_t *dde = ddt_repair_start(ddt, bp);
2446 ddt_phys_t *ddp = dde->dde_phys;
2447 ddt_phys_t *ddp_self = ddt_phys_select(dde, bp);
2448 blkptr_t blk;
2449
2450 ASSERT(zio->io_vsd == NULL);
2451 zio->io_vsd = dde;
2452
2453 if (ddp_self == NULL)
2454 return (ZIO_PIPELINE_CONTINUE);
2455
2456 for (p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
2457 if (ddp->ddp_phys_birth == 0 || ddp == ddp_self)
2458 continue;
2459 ddt_bp_create(ddt->ddt_checksum, &dde->dde_key, ddp,
2460 &blk);
2461 zio_nowait(zio_read(zio, zio->io_spa, &blk,
2462 zio_buf_alloc(zio->io_size), zio->io_size,
2463 zio_ddt_child_read_done, dde, zio->io_priority,
2464 ZIO_DDT_CHILD_FLAGS(zio) | ZIO_FLAG_DONT_PROPAGATE,
2465 &zio->io_bookmark));
2466 }
2467 return (ZIO_PIPELINE_CONTINUE);
2468 }
2469
2470 zio_nowait(zio_read(zio, zio->io_spa, bp,
2471 zio->io_data, zio->io_size, NULL, NULL, zio->io_priority,
2472 ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark));
2473
2474 return (ZIO_PIPELINE_CONTINUE);
2475 }
2476
2477 static int
2478 zio_ddt_read_done(zio_t *zio)
2479 {
2480 blkptr_t *bp = zio->io_bp;
2481
2482 if (zio_wait_for_children(zio, ZIO_CHILD_DDT, ZIO_WAIT_DONE))
2483 return (ZIO_PIPELINE_STOP);
2484
2485 ASSERT(BP_GET_DEDUP(bp));
2486 ASSERT(BP_GET_PSIZE(bp) == zio->io_size);
2487 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2488
2489 if (zio->io_child_error[ZIO_CHILD_DDT]) {
2490 ddt_t *ddt = ddt_select(zio->io_spa, bp);
2491 ddt_entry_t *dde = zio->io_vsd;
2492 if (ddt == NULL) {
2493 ASSERT(spa_load_state(zio->io_spa) != SPA_LOAD_NONE);
2494 return (ZIO_PIPELINE_CONTINUE);
2495 }
2496 if (dde == NULL) {
2497 zio->io_stage = ZIO_STAGE_DDT_READ_START >> 1;
2498 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE);
2499 return (ZIO_PIPELINE_STOP);
2500 }
2501 if (dde->dde_repair_data != NULL) {
2502 bcopy(dde->dde_repair_data, zio->io_data, zio->io_size);
2503 zio->io_child_error[ZIO_CHILD_DDT] = 0;
2504 }
2505 ddt_repair_done(ddt, dde);
2506 zio->io_vsd = NULL;
2507 }
2508
2509 ASSERT(zio->io_vsd == NULL);
2510
2511 return (ZIO_PIPELINE_CONTINUE);
2512 }
2513
2514 static boolean_t
2515 zio_ddt_collision(zio_t *zio, ddt_t *ddt, ddt_entry_t *dde)
2516 {
2517 spa_t *spa = zio->io_spa;
2518 int p;
2519 boolean_t do_raw = !!(zio->io_flags & ZIO_FLAG_RAW);
2520
2521 ASSERT(!(zio->io_bp_override && do_raw));
2522
2523 /*
2524 * Note: we compare the original data, not the transformed data,
2525 * because when zio->io_bp is an override bp, we will not have
2526 * pushed the I/O transforms. That's an important optimization
2527 * because otherwise we'd compress/encrypt all dmu_sync() data twice.
2528 * However, we should never get a raw, override zio so in these
2529 * cases we can compare the io_data directly. This is useful because
2530 * it allows us to do dedup verification even if we don't have access
2531 * to the original data (for instance, if the encryption keys aren't
2532 * loaded).
2533 */
2534
2535 for (p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) {
2536 zio_t *lio = dde->dde_lead_zio[p];
2537
2538 if (lio != NULL && do_raw) {
2539 return (lio->io_size != zio->io_size ||
2540 bcmp(zio->io_data, lio->io_data,
2541 zio->io_size) != 0);
2542 } else if (lio != NULL) {
2543 return (lio->io_orig_size != zio->io_orig_size ||
2544 bcmp(zio->io_orig_data, lio->io_orig_data,
2545 zio->io_orig_size) != 0);
2546 }
2547 }
2548
2549 for (p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) {
2550 ddt_phys_t *ddp = &dde->dde_phys[p];
2551
2552 if (ddp->ddp_phys_birth != 0 && do_raw) {
2553 blkptr_t blk = *zio->io_bp;
2554 uint64_t psize;
2555 void *tmpbuf;
2556 int error;
2557
2558 ddt_bp_fill(ddp, &blk, ddp->ddp_phys_birth);
2559 psize = BP_GET_PSIZE(&blk);
2560
2561 if (psize != zio->io_size)
2562 return (B_TRUE);
2563
2564 ddt_exit(ddt);
2565
2566 tmpbuf = zio_buf_alloc(psize);
2567
2568 error = zio_wait(zio_read(NULL, spa, &blk, tmpbuf,
2569 psize, NULL, NULL, ZIO_PRIORITY_SYNC_READ,
2570 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
2571 ZIO_FLAG_RAW, &zio->io_bookmark));
2572
2573 if (error == 0) {
2574 if (bcmp(tmpbuf, zio->io_data, psize) != 0)
2575 error = SET_ERROR(ENOENT);
2576 }
2577
2578 zio_buf_free(tmpbuf, psize);
2579 ddt_enter(ddt);
2580 return (error != 0);
2581 } else if (ddp->ddp_phys_birth != 0) {
2582 arc_buf_t *abuf = NULL;
2583 arc_flags_t aflags = ARC_FLAG_WAIT;
2584 blkptr_t blk = *zio->io_bp;
2585 int error;
2586
2587 ddt_bp_fill(ddp, &blk, ddp->ddp_phys_birth);
2588
2589 if (BP_GET_LSIZE(&blk) != zio->io_orig_size)
2590 return (B_TRUE);
2591
2592 ddt_exit(ddt);
2593
2594 error = arc_read(NULL, spa, &blk,
2595 arc_getbuf_func, &abuf, ZIO_PRIORITY_SYNC_READ,
2596 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
2597 &aflags, &zio->io_bookmark);
2598
2599 if (error == 0) {
2600 if (bcmp(abuf->b_data, zio->io_orig_data,
2601 zio->io_orig_size) != 0)
2602 error = SET_ERROR(ENOENT);
2603 arc_buf_destroy(abuf, &abuf);
2604 }
2605
2606 ddt_enter(ddt);
2607 return (error != 0);
2608 }
2609 }
2610
2611 return (B_FALSE);
2612 }
2613
2614 static void
2615 zio_ddt_child_write_ready(zio_t *zio)
2616 {
2617 int p = zio->io_prop.zp_copies;
2618 ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp);
2619 ddt_entry_t *dde = zio->io_private;
2620 ddt_phys_t *ddp = &dde->dde_phys[p];
2621 zio_t *pio;
2622 zio_link_t *zl;
2623
2624 if (zio->io_error)
2625 return;
2626
2627 ddt_enter(ddt);
2628
2629 ASSERT(dde->dde_lead_zio[p] == zio);
2630
2631 ddt_phys_fill(ddp, zio->io_bp);
2632
2633 zl = NULL;
2634 while ((pio = zio_walk_parents(zio, &zl)) != NULL)
2635 ddt_bp_fill(ddp, pio->io_bp, zio->io_txg);
2636
2637 ddt_exit(ddt);
2638 }
2639
2640 static void
2641 zio_ddt_child_write_done(zio_t *zio)
2642 {
2643 int p = zio->io_prop.zp_copies;
2644 ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp);
2645 ddt_entry_t *dde = zio->io_private;
2646 ddt_phys_t *ddp = &dde->dde_phys[p];
2647
2648 ddt_enter(ddt);
2649
2650 ASSERT(ddp->ddp_refcnt == 0);
2651 ASSERT(dde->dde_lead_zio[p] == zio);
2652 dde->dde_lead_zio[p] = NULL;
2653
2654 if (zio->io_error == 0) {
2655 zio_link_t *zl = NULL;
2656 while (zio_walk_parents(zio, &zl) != NULL)
2657 ddt_phys_addref(ddp);
2658 } else {
2659 ddt_phys_clear(ddp);
2660 }
2661
2662 ddt_exit(ddt);
2663 }
2664
2665 static void
2666 zio_ddt_ditto_write_done(zio_t *zio)
2667 {
2668 int p = DDT_PHYS_DITTO;
2669 blkptr_t *bp = zio->io_bp;
2670 ddt_t *ddt = ddt_select(zio->io_spa, bp);
2671 ddt_entry_t *dde = zio->io_private;
2672 ddt_phys_t *ddp = &dde->dde_phys[p];
2673 ddt_key_t *ddk = &dde->dde_key;
2674 ASSERTV(zio_prop_t *zp = &zio->io_prop);
2675
2676 ddt_enter(ddt);
2677
2678 ASSERT(ddp->ddp_refcnt == 0);
2679 ASSERT(dde->dde_lead_zio[p] == zio);
2680 dde->dde_lead_zio[p] = NULL;
2681
2682 if (zio->io_error == 0) {
2683 ASSERT(ZIO_CHECKSUM_EQUAL(bp->blk_cksum, ddk->ddk_cksum));
2684 ASSERT(zp->zp_copies < SPA_DVAS_PER_BP);
2685 ASSERT(zp->zp_copies == BP_GET_NDVAS(bp) - BP_IS_GANG(bp));
2686 if (ddp->ddp_phys_birth != 0)
2687 ddt_phys_free(ddt, ddk, ddp, zio->io_txg);
2688 ddt_phys_fill(ddp, bp);
2689 }
2690
2691 ddt_exit(ddt);
2692 }
2693
2694 static int
2695 zio_ddt_write(zio_t *zio)
2696 {
2697 spa_t *spa = zio->io_spa;
2698 blkptr_t *bp = zio->io_bp;
2699 uint64_t txg = zio->io_txg;
2700 zio_prop_t *zp = &zio->io_prop;
2701 int p = zp->zp_copies;
2702 int ditto_copies;
2703 zio_t *cio = NULL;
2704 zio_t *dio = NULL;
2705 ddt_t *ddt = ddt_select(spa, bp);
2706 ddt_entry_t *dde;
2707 ddt_phys_t *ddp;
2708
2709 ASSERT(BP_GET_DEDUP(bp));
2710 ASSERT(BP_GET_CHECKSUM(bp) == zp->zp_checksum);
2711 ASSERT(BP_IS_HOLE(bp) || zio->io_bp_override);
2712 ASSERT(!(zio->io_bp_override && (zio->io_flags & ZIO_FLAG_RAW)));
2713
2714 ddt_enter(ddt);
2715 dde = ddt_lookup(ddt, bp, B_TRUE);
2716 ddp = &dde->dde_phys[p];
2717
2718 if (zp->zp_dedup_verify && zio_ddt_collision(zio, ddt, dde)) {
2719 /*
2720 * If we're using a weak checksum, upgrade to a strong checksum
2721 * and try again. If we're already using a strong checksum,
2722 * we can't resolve it, so just convert to an ordinary write.
2723 * (And automatically e-mail a paper to Nature?)
2724 */
2725 if (!(zio_checksum_table[zp->zp_checksum].ci_flags &
2726 ZCHECKSUM_FLAG_DEDUP)) {
2727 zp->zp_checksum = spa_dedup_checksum(spa);
2728 zio_pop_transforms(zio);
2729 zio->io_stage = ZIO_STAGE_OPEN;
2730 BP_ZERO(bp);
2731 } else {
2732 zp->zp_dedup = B_FALSE;
2733 }
2734 zio->io_pipeline = ZIO_WRITE_PIPELINE;
2735 ddt_exit(ddt);
2736 return (ZIO_PIPELINE_CONTINUE);
2737 }
2738
2739 ditto_copies = ddt_ditto_copies_needed(ddt, dde, ddp);
2740 ASSERT(ditto_copies < SPA_DVAS_PER_BP);
2741
2742 if (ditto_copies > ddt_ditto_copies_present(dde) &&
2743 dde->dde_lead_zio[DDT_PHYS_DITTO] == NULL) {
2744 zio_prop_t czp = *zp;
2745
2746 czp.zp_copies = ditto_copies;
2747
2748 /*
2749 * If we arrived here with an override bp, we won't have run
2750 * the transform stack, so we won't have the data we need to
2751 * generate a child i/o. So, toss the override bp and restart.
2752 * This is safe, because using the override bp is just an
2753 * optimization; and it's rare, so the cost doesn't matter.
2754 */
2755 if (zio->io_bp_override) {
2756 zio_pop_transforms(zio);
2757 zio->io_stage = ZIO_STAGE_OPEN;
2758 zio->io_pipeline = ZIO_WRITE_PIPELINE;
2759 zio->io_bp_override = NULL;
2760 BP_ZERO(bp);
2761 ddt_exit(ddt);
2762 return (ZIO_PIPELINE_CONTINUE);
2763 }
2764
2765 dio = zio_write(zio, spa, txg, bp, zio->io_orig_data,
2766 zio->io_orig_size, zio->io_orig_size, &czp, NULL, NULL,
2767 NULL, zio_ddt_ditto_write_done, dde, zio->io_priority,
2768 ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark);
2769
2770 zio_push_transform(dio, zio->io_data, zio->io_size, 0, NULL);
2771 dde->dde_lead_zio[DDT_PHYS_DITTO] = dio;
2772 }
2773
2774 if (ddp->ddp_phys_birth != 0 || dde->dde_lead_zio[p] != NULL) {
2775 if (ddp->ddp_phys_birth != 0)
2776 ddt_bp_fill(ddp, bp, txg);
2777 if (dde->dde_lead_zio[p] != NULL)
2778 zio_add_child(zio, dde->dde_lead_zio[p]);
2779 else
2780 ddt_phys_addref(ddp);
2781 } else if (zio->io_bp_override) {
2782 ASSERT(bp->blk_birth == txg);
2783 ASSERT(BP_EQUAL(bp, zio->io_bp_override));
2784 ddt_phys_fill(ddp, bp);
2785 ddt_phys_addref(ddp);
2786 } else {
2787 cio = zio_write(zio, spa, txg, bp, zio->io_orig_data,
2788 zio->io_orig_size, zio->io_orig_size, zp,
2789 zio_ddt_child_write_ready, NULL, NULL,
2790 zio_ddt_child_write_done, dde, zio->io_priority,
2791 ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark);
2792
2793 zio_push_transform(cio, zio->io_data, zio->io_size, 0, NULL);
2794 dde->dde_lead_zio[p] = cio;
2795 }
2796
2797 ddt_exit(ddt);
2798
2799 if (cio)
2800 zio_nowait(cio);
2801 if (dio)
2802 zio_nowait(dio);
2803
2804 return (ZIO_PIPELINE_CONTINUE);
2805 }
2806
2807 ddt_entry_t *freedde; /* for debugging */
2808
2809 static int
2810 zio_ddt_free(zio_t *zio)
2811 {
2812 spa_t *spa = zio->io_spa;
2813 blkptr_t *bp = zio->io_bp;
2814 ddt_t *ddt = ddt_select(spa, bp);
2815 ddt_entry_t *dde;
2816 ddt_phys_t *ddp;
2817
2818 ASSERT(BP_GET_DEDUP(bp));
2819 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2820
2821 ddt_enter(ddt);
2822 freedde = dde = ddt_lookup(ddt, bp, B_TRUE);
2823 if (dde) {
2824 ddp = ddt_phys_select(dde, bp);
2825 if (ddp)
2826 ddt_phys_decref(ddp);
2827 }
2828 ddt_exit(ddt);
2829
2830 return (ZIO_PIPELINE_CONTINUE);
2831 }
2832
2833 /*
2834 * ==========================================================================
2835 * Allocate and free blocks
2836 * ==========================================================================
2837 */
2838
2839 static zio_t *
2840 zio_io_to_allocate(spa_t *spa)
2841 {
2842 zio_t *zio;
2843
2844 ASSERT(MUTEX_HELD(&spa->spa_alloc_lock));
2845
2846 zio = avl_first(&spa->spa_alloc_tree);
2847 if (zio == NULL)
2848 return (NULL);
2849
2850 ASSERT(IO_IS_ALLOCATING(zio));
2851
2852 /*
2853 * Try to place a reservation for this zio. If we're unable to
2854 * reserve then we throttle.
2855 */
2856 if (!metaslab_class_throttle_reserve(spa_normal_class(spa),
2857 zio->io_prop.zp_copies, zio, 0)) {
2858 return (NULL);
2859 }
2860
2861 avl_remove(&spa->spa_alloc_tree, zio);
2862 ASSERT3U(zio->io_stage, <, ZIO_STAGE_DVA_ALLOCATE);
2863
2864 return (zio);
2865 }
2866
2867 static int
2868 zio_dva_throttle(zio_t *zio)
2869 {
2870 spa_t *spa = zio->io_spa;
2871 zio_t *nio;
2872
2873 if (zio->io_priority == ZIO_PRIORITY_SYNC_WRITE ||
2874 !spa_normal_class(zio->io_spa)->mc_alloc_throttle_enabled ||
2875 zio->io_child_type == ZIO_CHILD_GANG ||
2876 zio->io_flags & ZIO_FLAG_NODATA) {
2877 return (ZIO_PIPELINE_CONTINUE);
2878 }
2879
2880 ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
2881
2882 ASSERT3U(zio->io_queued_timestamp, >, 0);
2883 ASSERT(zio->io_stage == ZIO_STAGE_DVA_THROTTLE);
2884
2885 mutex_enter(&spa->spa_alloc_lock);
2886
2887 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
2888 avl_add(&spa->spa_alloc_tree, zio);
2889
2890 nio = zio_io_to_allocate(zio->io_spa);
2891 mutex_exit(&spa->spa_alloc_lock);
2892
2893 if (nio == zio)
2894 return (ZIO_PIPELINE_CONTINUE);
2895
2896 if (nio != NULL) {
2897 ASSERT3U(nio->io_queued_timestamp, <=,
2898 zio->io_queued_timestamp);
2899 ASSERT(nio->io_stage == ZIO_STAGE_DVA_THROTTLE);
2900 /*
2901 * We are passing control to a new zio so make sure that
2902 * it is processed by a different thread. We do this to
2903 * avoid stack overflows that can occur when parents are
2904 * throttled and children are making progress. We allow
2905 * it to go to the head of the taskq since it's already
2906 * been waiting.
2907 */
2908 zio_taskq_dispatch(nio, ZIO_TASKQ_ISSUE, B_TRUE);
2909 }
2910 return (ZIO_PIPELINE_STOP);
2911 }
2912
2913 void
2914 zio_allocate_dispatch(spa_t *spa)
2915 {
2916 zio_t *zio;
2917
2918 mutex_enter(&spa->spa_alloc_lock);
2919 zio = zio_io_to_allocate(spa);
2920 mutex_exit(&spa->spa_alloc_lock);
2921 if (zio == NULL)
2922 return;
2923
2924 ASSERT3U(zio->io_stage, ==, ZIO_STAGE_DVA_THROTTLE);
2925 ASSERT0(zio->io_error);
2926 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_TRUE);
2927 }
2928
2929 static int
2930 zio_dva_allocate(zio_t *zio)
2931 {
2932 spa_t *spa = zio->io_spa;
2933 metaslab_class_t *mc = spa_normal_class(spa);
2934 blkptr_t *bp = zio->io_bp;
2935 int error;
2936 int flags = 0;
2937
2938 if (zio->io_gang_leader == NULL) {
2939 ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
2940 zio->io_gang_leader = zio;
2941 }
2942
2943 ASSERT(BP_IS_HOLE(bp));
2944 ASSERT0(BP_GET_NDVAS(bp));
2945 ASSERT3U(zio->io_prop.zp_copies, >, 0);
2946 ASSERT3U(zio->io_prop.zp_copies, <=, spa_max_replication(spa));
2947 ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp));
2948
2949 flags |= (zio->io_flags & ZIO_FLAG_FASTWRITE) ? METASLAB_FASTWRITE : 0;
2950 if (zio->io_flags & ZIO_FLAG_NODATA)
2951 flags |= METASLAB_DONT_THROTTLE;
2952 if (zio->io_flags & ZIO_FLAG_GANG_CHILD)
2953 flags |= METASLAB_GANG_CHILD;
2954 if (zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE)
2955 flags |= METASLAB_ASYNC_ALLOC;
2956
2957 error = metaslab_alloc(spa, mc, zio->io_size, bp,
2958 zio->io_prop.zp_copies, zio->io_txg, NULL, flags, zio);
2959
2960 if (error != 0) {
2961 spa_dbgmsg(spa, "%s: metaslab allocation failure: zio %p, "
2962 "size %llu, error %d", spa_name(spa), zio, zio->io_size,
2963 error);
2964 if (error == ENOSPC && zio->io_size > SPA_MINBLOCKSIZE)
2965 return (zio_write_gang_block(zio));
2966 zio->io_error = error;
2967 }
2968
2969 return (ZIO_PIPELINE_CONTINUE);
2970 }
2971
2972 static int
2973 zio_dva_free(zio_t *zio)
2974 {
2975 metaslab_free(zio->io_spa, zio->io_bp, zio->io_txg, B_FALSE);
2976
2977 return (ZIO_PIPELINE_CONTINUE);
2978 }
2979
2980 static int
2981 zio_dva_claim(zio_t *zio)
2982 {
2983 int error;
2984
2985 error = metaslab_claim(zio->io_spa, zio->io_bp, zio->io_txg);
2986 if (error)
2987 zio->io_error = error;
2988
2989 return (ZIO_PIPELINE_CONTINUE);
2990 }
2991
2992 /*
2993 * Undo an allocation. This is used by zio_done() when an I/O fails
2994 * and we want to give back the block we just allocated.
2995 * This handles both normal blocks and gang blocks.
2996 */
2997 static void
2998 zio_dva_unallocate(zio_t *zio, zio_gang_node_t *gn, blkptr_t *bp)
2999 {
3000 int g;
3001
3002 ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp));
3003 ASSERT(zio->io_bp_override == NULL);
3004
3005 if (!BP_IS_HOLE(bp))
3006 metaslab_free(zio->io_spa, bp, bp->blk_birth, B_TRUE);
3007
3008 if (gn != NULL) {
3009 for (g = 0; g < SPA_GBH_NBLKPTRS; g++) {
3010 zio_dva_unallocate(zio, gn->gn_child[g],
3011 &gn->gn_gbh->zg_blkptr[g]);
3012 }
3013 }
3014 }
3015
3016 /*
3017 * Try to allocate an intent log block. Return 0 on success, errno on failure.
3018 */
3019 int
3020 zio_alloc_zil(spa_t *spa, uint64_t txg, blkptr_t *new_bp, uint64_t size,
3021 boolean_t use_slog)
3022 {
3023 int error = 1;
3024
3025 ASSERT(txg > spa_syncing_txg(spa));
3026
3027 if (use_slog) {
3028 error = metaslab_alloc(spa, spa_log_class(spa), size,
3029 new_bp, 1, txg, NULL, METASLAB_FASTWRITE, NULL);
3030 }
3031
3032 if (error) {
3033 error = metaslab_alloc(spa, spa_normal_class(spa), size,
3034 new_bp, 1, txg, NULL, METASLAB_FASTWRITE, NULL);
3035 }
3036
3037 if (error == 0) {
3038 BP_SET_LSIZE(new_bp, size);
3039 BP_SET_PSIZE(new_bp, size);
3040 BP_SET_COMPRESS(new_bp, ZIO_COMPRESS_OFF);
3041 BP_SET_CHECKSUM(new_bp,
3042 spa_version(spa) >= SPA_VERSION_SLIM_ZIL
3043 ? ZIO_CHECKSUM_ZILOG2 : ZIO_CHECKSUM_ZILOG);
3044 BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG);
3045 BP_SET_LEVEL(new_bp, 0);
3046 BP_SET_DEDUP(new_bp, 0);
3047 BP_SET_BYTEORDER(new_bp, ZFS_HOST_BYTEORDER);
3048 }
3049
3050 return (error);
3051 }
3052
3053 /*
3054 * Free an intent log block.
3055 */
3056 void
3057 zio_free_zil(spa_t *spa, uint64_t txg, blkptr_t *bp)
3058 {
3059 ASSERT(BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG);
3060 ASSERT(!BP_IS_GANG(bp));
3061
3062 zio_free(spa, txg, bp);
3063 }
3064
3065 /*
3066 * ==========================================================================
3067 * Read and write to physical devices
3068 * ==========================================================================
3069 */
3070
3071
3072 /*
3073 * Issue an I/O to the underlying vdev. Typically the issue pipeline
3074 * stops after this stage and will resume upon I/O completion.
3075 * However, there are instances where the vdev layer may need to
3076 * continue the pipeline when an I/O was not issued. Since the I/O
3077 * that was sent to the vdev layer might be different than the one
3078 * currently active in the pipeline (see vdev_queue_io()), we explicitly
3079 * force the underlying vdev layers to call either zio_execute() or
3080 * zio_interrupt() to ensure that the pipeline continues with the correct I/O.
3081 */
3082 static int
3083 zio_vdev_io_start(zio_t *zio)
3084 {
3085 vdev_t *vd = zio->io_vd;
3086 uint64_t align;
3087 spa_t *spa = zio->io_spa;
3088
3089 zio->io_delay = 0;
3090
3091 ASSERT(zio->io_error == 0);
3092 ASSERT(zio->io_child_error[ZIO_CHILD_VDEV] == 0);
3093
3094 if (vd == NULL) {
3095 if (!(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
3096 spa_config_enter(spa, SCL_ZIO, zio, RW_READER);
3097
3098 /*
3099 * The mirror_ops handle multiple DVAs in a single BP.
3100 */
3101 vdev_mirror_ops.vdev_op_io_start(zio);
3102 return (ZIO_PIPELINE_STOP);
3103 }
3104
3105 ASSERT3P(zio->io_logical, !=, zio);
3106
3107 /*
3108 * We keep track of time-sensitive I/Os so that the scan thread
3109 * can quickly react to certain workloads. In particular, we care
3110 * about non-scrubbing, top-level reads and writes with the following
3111 * characteristics:
3112 * - synchronous writes of user data to non-slog devices
3113 * - any reads of user data
3114 * When these conditions are met, adjust the timestamp of spa_last_io
3115 * which allows the scan thread to adjust its workload accordingly.
3116 */
3117 if (!(zio->io_flags & ZIO_FLAG_SCAN_THREAD) && zio->io_bp != NULL &&
3118 vd == vd->vdev_top && !vd->vdev_islog &&
3119 zio->io_bookmark.zb_objset != DMU_META_OBJSET &&
3120 zio->io_txg != spa_syncing_txg(spa)) {
3121 uint64_t old = spa->spa_last_io;
3122 uint64_t new = ddi_get_lbolt64();
3123 if (old != new)
3124 (void) atomic_cas_64(&spa->spa_last_io, old, new);
3125 }
3126
3127 align = 1ULL << vd->vdev_top->vdev_ashift;
3128
3129 if (!(zio->io_flags & ZIO_FLAG_PHYSICAL) &&
3130 P2PHASE(zio->io_size, align) != 0) {
3131 /* Transform logical writes to be a full physical block size. */
3132 uint64_t asize = P2ROUNDUP(zio->io_size, align);
3133 char *abuf = zio_buf_alloc(asize);
3134 ASSERT(vd == vd->vdev_top);
3135 if (zio->io_type == ZIO_TYPE_WRITE) {
3136 bcopy(zio->io_data, abuf, zio->io_size);
3137 bzero(abuf + zio->io_size, asize - zio->io_size);
3138 }
3139 zio_push_transform(zio, abuf, asize, asize, zio_subblock);
3140 }
3141
3142 /*
3143 * If this is not a physical io, make sure that it is properly aligned
3144 * before proceeding.
3145 */
3146 if (!(zio->io_flags & ZIO_FLAG_PHYSICAL)) {
3147 ASSERT0(P2PHASE(zio->io_offset, align));
3148 ASSERT0(P2PHASE(zio->io_size, align));
3149 } else {
3150 /*
3151 * For physical writes, we allow 512b aligned writes and assume
3152 * the device will perform a read-modify-write as necessary.
3153 */
3154 ASSERT0(P2PHASE(zio->io_offset, SPA_MINBLOCKSIZE));
3155 ASSERT0(P2PHASE(zio->io_size, SPA_MINBLOCKSIZE));
3156 }
3157
3158 VERIFY(zio->io_type != ZIO_TYPE_WRITE || spa_writeable(spa));
3159
3160 /*
3161 * If this is a repair I/O, and there's no self-healing involved --
3162 * that is, we're just resilvering what we expect to resilver --
3163 * then don't do the I/O unless zio's txg is actually in vd's DTL.
3164 * This prevents spurious resilvering with nested replication.
3165 * For example, given a mirror of mirrors, (A+B)+(C+D), if only
3166 * A is out of date, we'll read from C+D, then use the data to
3167 * resilver A+B -- but we don't actually want to resilver B, just A.
3168 * The top-level mirror has no way to know this, so instead we just
3169 * discard unnecessary repairs as we work our way down the vdev tree.
3170 * The same logic applies to any form of nested replication:
3171 * ditto + mirror, RAID-Z + replacing, etc. This covers them all.
3172 */
3173 if ((zio->io_flags & ZIO_FLAG_IO_REPAIR) &&
3174 !(zio->io_flags & ZIO_FLAG_SELF_HEAL) &&
3175 zio->io_txg != 0 && /* not a delegated i/o */
3176 !vdev_dtl_contains(vd, DTL_PARTIAL, zio->io_txg, 1)) {
3177 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
3178 zio_vdev_io_bypass(zio);
3179 return (ZIO_PIPELINE_CONTINUE);
3180 }
3181
3182 if (vd->vdev_ops->vdev_op_leaf &&
3183 (zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE)) {
3184
3185 if (zio->io_type == ZIO_TYPE_READ && vdev_cache_read(zio))
3186 return (ZIO_PIPELINE_CONTINUE);
3187
3188 if ((zio = vdev_queue_io(zio)) == NULL)
3189 return (ZIO_PIPELINE_STOP);
3190
3191 if (!vdev_accessible(vd, zio)) {
3192 zio->io_error = SET_ERROR(ENXIO);
3193 zio_interrupt(zio);
3194 return (ZIO_PIPELINE_STOP);
3195 }
3196 }
3197
3198 zio->io_delay = gethrtime();
3199 vd->vdev_ops->vdev_op_io_start(zio);
3200 return (ZIO_PIPELINE_STOP);
3201 }
3202
3203 static int
3204 zio_vdev_io_done(zio_t *zio)
3205 {
3206 vdev_t *vd = zio->io_vd;
3207 vdev_ops_t *ops = vd ? vd->vdev_ops : &vdev_mirror_ops;
3208 boolean_t unexpected_error = B_FALSE;
3209
3210 if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE))
3211 return (ZIO_PIPELINE_STOP);
3212
3213 ASSERT(zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE);
3214
3215 if (zio->io_delay)
3216 zio->io_delay = gethrtime() - zio->io_delay;
3217
3218 if (vd != NULL && vd->vdev_ops->vdev_op_leaf) {
3219
3220 vdev_queue_io_done(zio);
3221
3222 if (zio->io_type == ZIO_TYPE_WRITE)
3223 vdev_cache_write(zio);
3224
3225 if (zio_injection_enabled && zio->io_error == 0)
3226 zio->io_error = zio_handle_device_injection(vd,
3227 zio, EIO);
3228
3229 if (zio_injection_enabled && zio->io_error == 0)
3230 zio->io_error = zio_handle_label_injection(zio, EIO);
3231
3232 if (zio->io_error) {
3233 if (!vdev_accessible(vd, zio)) {
3234 zio->io_error = SET_ERROR(ENXIO);
3235 } else {
3236 unexpected_error = B_TRUE;
3237 }
3238 }
3239 }
3240
3241 ops->vdev_op_io_done(zio);
3242
3243 if (unexpected_error)
3244 VERIFY(vdev_probe(vd, zio) == NULL);
3245
3246 return (ZIO_PIPELINE_CONTINUE);
3247 }
3248
3249 /*
3250 * For non-raidz ZIOs, we can just copy aside the bad data read from the
3251 * disk, and use that to finish the checksum ereport later.
3252 */
3253 static void
3254 zio_vsd_default_cksum_finish(zio_cksum_report_t *zcr,
3255 const void *good_buf)
3256 {
3257 /* no processing needed */
3258 zfs_ereport_finish_checksum(zcr, good_buf, zcr->zcr_cbdata, B_FALSE);
3259 }
3260
3261 /*ARGSUSED*/
3262 void
3263 zio_vsd_default_cksum_report(zio_t *zio, zio_cksum_report_t *zcr, void *ignored)
3264 {
3265 void *buf = zio_buf_alloc(zio->io_size);
3266
3267 bcopy(zio->io_data, buf, zio->io_size);
3268
3269 zcr->zcr_cbinfo = zio->io_size;
3270 zcr->zcr_cbdata = buf;
3271 zcr->zcr_finish = zio_vsd_default_cksum_finish;
3272 zcr->zcr_free = zio_buf_free;
3273 }
3274
3275 static int
3276 zio_vdev_io_assess(zio_t *zio)
3277 {
3278 vdev_t *vd = zio->io_vd;
3279
3280 if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE))
3281 return (ZIO_PIPELINE_STOP);
3282
3283 if (vd == NULL && !(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
3284 spa_config_exit(zio->io_spa, SCL_ZIO, zio);
3285
3286 if (zio->io_vsd != NULL) {
3287 zio->io_vsd_ops->vsd_free(zio);
3288 zio->io_vsd = NULL;
3289 }
3290
3291 if (zio_injection_enabled && zio->io_error == 0)
3292 zio->io_error = zio_handle_fault_injection(zio, EIO);
3293
3294 /*
3295 * If the I/O failed, determine whether we should attempt to retry it.
3296 *
3297 * On retry, we cut in line in the issue queue, since we don't want
3298 * compression/checksumming/etc. work to prevent our (cheap) IO reissue.
3299 */
3300 if (zio->io_error && vd == NULL &&
3301 !(zio->io_flags & (ZIO_FLAG_DONT_RETRY | ZIO_FLAG_IO_RETRY))) {
3302 ASSERT(!(zio->io_flags & ZIO_FLAG_DONT_QUEUE)); /* not a leaf */
3303 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_BYPASS)); /* not a leaf */
3304 zio->io_error = 0;
3305 zio->io_flags |= ZIO_FLAG_IO_RETRY |
3306 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE;
3307 zio->io_stage = ZIO_STAGE_VDEV_IO_START >> 1;
3308 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE,
3309 zio_requeue_io_start_cut_in_line);
3310 return (ZIO_PIPELINE_STOP);
3311 }
3312
3313 /*
3314 * If we got an error on a leaf device, convert it to ENXIO
3315 * if the device is not accessible at all.
3316 */
3317 if (zio->io_error && vd != NULL && vd->vdev_ops->vdev_op_leaf &&
3318 !vdev_accessible(vd, zio))
3319 zio->io_error = SET_ERROR(ENXIO);
3320
3321 /*
3322 * If we can't write to an interior vdev (mirror or RAID-Z),
3323 * set vdev_cant_write so that we stop trying to allocate from it.
3324 */
3325 if (zio->io_error == ENXIO && zio->io_type == ZIO_TYPE_WRITE &&
3326 vd != NULL && !vd->vdev_ops->vdev_op_leaf) {
3327 vd->vdev_cant_write = B_TRUE;
3328 }
3329
3330 if (zio->io_error)
3331 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
3332
3333 if (vd != NULL && vd->vdev_ops->vdev_op_leaf &&
3334 zio->io_physdone != NULL) {
3335 ASSERT(!(zio->io_flags & ZIO_FLAG_DELEGATED));
3336 ASSERT(zio->io_child_type == ZIO_CHILD_VDEV);
3337 zio->io_physdone(zio->io_logical);
3338 }
3339
3340 return (ZIO_PIPELINE_CONTINUE);
3341 }
3342
3343 void
3344 zio_vdev_io_reissue(zio_t *zio)
3345 {
3346 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
3347 ASSERT(zio->io_error == 0);
3348
3349 zio->io_stage >>= 1;
3350 }
3351
3352 void
3353 zio_vdev_io_redone(zio_t *zio)
3354 {
3355 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_DONE);
3356
3357 zio->io_stage >>= 1;
3358 }
3359
3360 void
3361 zio_vdev_io_bypass(zio_t *zio)
3362 {
3363 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
3364 ASSERT(zio->io_error == 0);
3365
3366 zio->io_flags |= ZIO_FLAG_IO_BYPASS;
3367 zio->io_stage = ZIO_STAGE_VDEV_IO_ASSESS >> 1;
3368 }
3369
3370 /*
3371 * ==========================================================================
3372 * Generate and verify checksums
3373 * ==========================================================================
3374 */
3375 static int
3376 zio_checksum_generate(zio_t *zio)
3377 {
3378 blkptr_t *bp = zio->io_bp;
3379 enum zio_checksum checksum;
3380
3381 if (bp == NULL) {
3382 /*
3383 * This is zio_write_phys().
3384 * We're either generating a label checksum, or none at all.
3385 */
3386 checksum = zio->io_prop.zp_checksum;
3387
3388 if (checksum == ZIO_CHECKSUM_OFF)
3389 return (ZIO_PIPELINE_CONTINUE);
3390
3391 ASSERT(checksum == ZIO_CHECKSUM_LABEL);
3392 } else {
3393 if (BP_IS_GANG(bp) && zio->io_child_type == ZIO_CHILD_GANG) {
3394 ASSERT(!IO_IS_ALLOCATING(zio));
3395 checksum = ZIO_CHECKSUM_GANG_HEADER;
3396 } else {
3397 checksum = BP_GET_CHECKSUM(bp);
3398 }
3399 }
3400
3401 zio_checksum_compute(zio, checksum, zio->io_data, zio->io_size);
3402
3403 return (ZIO_PIPELINE_CONTINUE);
3404 }
3405
3406 static int
3407 zio_checksum_verify(zio_t *zio)
3408 {
3409 zio_bad_cksum_t info;
3410 blkptr_t *bp = zio->io_bp;
3411 int error;
3412
3413 ASSERT(zio->io_vd != NULL);
3414
3415 if (bp == NULL) {
3416 /*
3417 * This is zio_read_phys().
3418 * We're either verifying a label checksum, or nothing at all.
3419 */
3420 if (zio->io_prop.zp_checksum == ZIO_CHECKSUM_OFF)
3421 return (ZIO_PIPELINE_CONTINUE);
3422
3423 ASSERT(zio->io_prop.zp_checksum == ZIO_CHECKSUM_LABEL);
3424 }
3425
3426 if ((error = zio_checksum_error(zio, &info)) != 0) {
3427 zio->io_error = error;
3428 if (error == ECKSUM &&
3429 !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
3430 zfs_ereport_start_checksum(zio->io_spa,
3431 zio->io_vd, zio, zio->io_offset,
3432 zio->io_size, NULL, &info);
3433 }
3434 }
3435
3436 return (ZIO_PIPELINE_CONTINUE);
3437 }
3438
3439 /*
3440 * Called by RAID-Z to ensure we don't compute the checksum twice.
3441 */
3442 void
3443 zio_checksum_verified(zio_t *zio)
3444 {
3445 zio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY;
3446 }
3447
3448 /*
3449 * ==========================================================================
3450 * Error rank. Error are ranked in the order 0, ENXIO, ECKSUM, EIO, other.
3451 * An error of 0 indicates success. ENXIO indicates whole-device failure,
3452 * which may be transient (e.g. unplugged) or permament. ECKSUM and EIO
3453 * indicate errors that are specific to one I/O, and most likely permanent.
3454 * Any other error is presumed to be worse because we weren't expecting it.
3455 * ==========================================================================
3456 */
3457 int
3458 zio_worst_error(int e1, int e2)
3459 {
3460 static int zio_error_rank[] = { 0, ENXIO, ECKSUM, EIO };
3461 int r1, r2;
3462
3463 for (r1 = 0; r1 < sizeof (zio_error_rank) / sizeof (int); r1++)
3464 if (e1 == zio_error_rank[r1])
3465 break;
3466
3467 for (r2 = 0; r2 < sizeof (zio_error_rank) / sizeof (int); r2++)
3468 if (e2 == zio_error_rank[r2])
3469 break;
3470
3471 return (r1 > r2 ? e1 : e2);
3472 }
3473
3474 /*
3475 * ==========================================================================
3476 * I/O completion
3477 * ==========================================================================
3478 */
3479 static int
3480 zio_ready(zio_t *zio)
3481 {
3482 blkptr_t *bp = zio->io_bp;
3483 zio_t *pio, *pio_next;
3484 zio_link_t *zl = NULL;
3485
3486 if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_READY) ||
3487 zio_wait_for_children(zio, ZIO_CHILD_DDT, ZIO_WAIT_READY))
3488 return (ZIO_PIPELINE_STOP);
3489
3490 if (zio->io_ready) {
3491 ASSERT(IO_IS_ALLOCATING(zio));
3492 ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp) ||
3493 (zio->io_flags & ZIO_FLAG_NOPWRITE));
3494 ASSERT(zio->io_children[ZIO_CHILD_GANG][ZIO_WAIT_READY] == 0);
3495
3496 zio->io_ready(zio);
3497 }
3498
3499 if (bp != NULL && bp != &zio->io_bp_copy)
3500 zio->io_bp_copy = *bp;
3501
3502 if (zio->io_error != 0) {
3503 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
3504
3505 if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
3506 ASSERT(IO_IS_ALLOCATING(zio));
3507 ASSERT(zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
3508 /*
3509 * We were unable to allocate anything, unreserve and
3510 * issue the next I/O to allocate.
3511 */
3512 metaslab_class_throttle_unreserve(
3513 spa_normal_class(zio->io_spa),
3514 zio->io_prop.zp_copies, zio);
3515 zio_allocate_dispatch(zio->io_spa);
3516 }
3517 }
3518
3519 mutex_enter(&zio->io_lock);
3520 zio->io_state[ZIO_WAIT_READY] = 1;
3521 pio = zio_walk_parents(zio, &zl);
3522 mutex_exit(&zio->io_lock);
3523
3524 /*
3525 * As we notify zio's parents, new parents could be added.
3526 * New parents go to the head of zio's io_parent_list, however,
3527 * so we will (correctly) not notify them. The remainder of zio's
3528 * io_parent_list, from 'pio_next' onward, cannot change because
3529 * all parents must wait for us to be done before they can be done.
3530 */
3531 for (; pio != NULL; pio = pio_next) {
3532 pio_next = zio_walk_parents(zio, &zl);
3533 zio_notify_parent(pio, zio, ZIO_WAIT_READY);
3534 }
3535
3536 if (zio->io_flags & ZIO_FLAG_NODATA) {
3537 if (BP_IS_GANG(bp)) {
3538 zio->io_flags &= ~ZIO_FLAG_NODATA;
3539 } else {
3540 ASSERT((uintptr_t)zio->io_data < SPA_MAXBLOCKSIZE);
3541 zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES;
3542 }
3543 }
3544
3545 if (zio_injection_enabled &&
3546 zio->io_spa->spa_syncing_txg == zio->io_txg)
3547 zio_handle_ignored_writes(zio);
3548
3549 return (ZIO_PIPELINE_CONTINUE);
3550 }
3551
3552 /*
3553 * Update the allocation throttle accounting.
3554 */
3555 static void
3556 zio_dva_throttle_done(zio_t *zio)
3557 {
3558 zio_t *lio = zio->io_logical;
3559 zio_t *pio = zio_unique_parent(zio);
3560 vdev_t *vd = zio->io_vd;
3561 int flags = METASLAB_ASYNC_ALLOC;
3562
3563 ASSERT3P(zio->io_bp, !=, NULL);
3564 ASSERT3U(zio->io_type, ==, ZIO_TYPE_WRITE);
3565 ASSERT3U(zio->io_priority, ==, ZIO_PRIORITY_ASYNC_WRITE);
3566 ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_VDEV);
3567 ASSERT(vd != NULL);
3568 ASSERT3P(vd, ==, vd->vdev_top);
3569 ASSERT(!(zio->io_flags & (ZIO_FLAG_IO_REPAIR | ZIO_FLAG_IO_RETRY)));
3570 ASSERT(zio->io_flags & ZIO_FLAG_IO_ALLOCATING);
3571 ASSERT(!(lio->io_flags & ZIO_FLAG_IO_REWRITE));
3572 ASSERT(!(lio->io_orig_flags & ZIO_FLAG_NODATA));
3573
3574 /*
3575 * Parents of gang children can have two flavors -- ones that
3576 * allocated the gang header (will have ZIO_FLAG_IO_REWRITE set)
3577 * and ones that allocated the constituent blocks. The allocation
3578 * throttle needs to know the allocating parent zio so we must find
3579 * it here.
3580 */
3581 if (pio->io_child_type == ZIO_CHILD_GANG) {
3582 /*
3583 * If our parent is a rewrite gang child then our grandparent
3584 * would have been the one that performed the allocation.
3585 */
3586 if (pio->io_flags & ZIO_FLAG_IO_REWRITE)
3587 pio = zio_unique_parent(pio);
3588 flags |= METASLAB_GANG_CHILD;
3589 }
3590
3591 ASSERT(IO_IS_ALLOCATING(pio));
3592 ASSERT3P(zio, !=, zio->io_logical);
3593 ASSERT(zio->io_logical != NULL);
3594 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REPAIR));
3595 ASSERT0(zio->io_flags & ZIO_FLAG_NOPWRITE);
3596
3597 mutex_enter(&pio->io_lock);
3598 metaslab_group_alloc_decrement(zio->io_spa, vd->vdev_id, pio, flags);
3599 mutex_exit(&pio->io_lock);
3600
3601 metaslab_class_throttle_unreserve(spa_normal_class(zio->io_spa),
3602 1, pio);
3603
3604 /*
3605 * Call into the pipeline to see if there is more work that
3606 * needs to be done. If there is work to be done it will be
3607 * dispatched to another taskq thread.
3608 */
3609 zio_allocate_dispatch(zio->io_spa);
3610 }
3611
3612 static int
3613 zio_done(zio_t *zio)
3614 {
3615 /*
3616 * Always attempt to keep stack usage minimal here since
3617 * we can be called recurisvely up to 19 levels deep.
3618 */
3619 zio_t *pio, *pio_next;
3620 int c, w;
3621 zio_link_t *zl = NULL;
3622
3623 /*
3624 * If our children haven't all completed,
3625 * wait for them and then repeat this pipeline stage.
3626 */
3627 if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE) ||
3628 zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_DONE) ||
3629 zio_wait_for_children(zio, ZIO_CHILD_DDT, ZIO_WAIT_DONE) ||
3630 zio_wait_for_children(zio, ZIO_CHILD_LOGICAL, ZIO_WAIT_DONE))
3631 return (ZIO_PIPELINE_STOP);
3632
3633 /*
3634 * If the allocation throttle is enabled, then update the accounting.
3635 * We only track child I/Os that are part of an allocating async
3636 * write. We must do this since the allocation is performed
3637 * by the logical I/O but the actual write is done by child I/Os.
3638 */
3639 if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING &&
3640 zio->io_child_type == ZIO_CHILD_VDEV) {
3641 ASSERT(spa_normal_class(
3642 zio->io_spa)->mc_alloc_throttle_enabled);
3643 zio_dva_throttle_done(zio);
3644 }
3645
3646 /*
3647 * If the allocation throttle is enabled, verify that
3648 * we have decremented the refcounts for every I/O that was throttled.
3649 */
3650 if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
3651 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
3652 ASSERT(zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
3653 ASSERT(zio->io_bp != NULL);
3654 metaslab_group_alloc_verify(zio->io_spa, zio->io_bp, zio);
3655 VERIFY(refcount_not_held(
3656 &(spa_normal_class(zio->io_spa)->mc_alloc_slots), zio));
3657 }
3658
3659
3660 for (c = 0; c < ZIO_CHILD_TYPES; c++)
3661 for (w = 0; w < ZIO_WAIT_TYPES; w++)
3662 ASSERT(zio->io_children[c][w] == 0);
3663
3664 if (zio->io_bp != NULL && !BP_IS_EMBEDDED(zio->io_bp)) {
3665 ASSERT(zio->io_bp->blk_pad[0] == 0);
3666 ASSERT(zio->io_bp->blk_pad[1] == 0);
3667 ASSERT(bcmp(zio->io_bp, &zio->io_bp_copy,
3668 sizeof (blkptr_t)) == 0 ||
3669 (zio->io_bp == zio_unique_parent(zio)->io_bp));
3670 if (zio->io_type == ZIO_TYPE_WRITE && !BP_IS_HOLE(zio->io_bp) &&
3671 zio->io_bp_override == NULL &&
3672 !(zio->io_flags & ZIO_FLAG_IO_REPAIR)) {
3673 ASSERT(!BP_SHOULD_BYTESWAP(zio->io_bp));
3674 ASSERT3U(zio->io_prop.zp_copies, <=,
3675 BP_GET_NDVAS(zio->io_bp));
3676 ASSERT(BP_COUNT_GANG(zio->io_bp) == 0 ||
3677 (BP_COUNT_GANG(zio->io_bp) ==
3678 BP_GET_NDVAS(zio->io_bp)));
3679 }
3680 if (zio->io_flags & ZIO_FLAG_NOPWRITE)
3681 VERIFY(BP_EQUAL(zio->io_bp, &zio->io_bp_orig));
3682 }
3683
3684 /*
3685 * If there were child vdev/gang/ddt errors, they apply to us now.
3686 */
3687 zio_inherit_child_errors(zio, ZIO_CHILD_VDEV);
3688 zio_inherit_child_errors(zio, ZIO_CHILD_GANG);
3689 zio_inherit_child_errors(zio, ZIO_CHILD_DDT);
3690
3691 /*
3692 * If the I/O on the transformed data was successful, generate any
3693 * checksum reports now while we still have the transformed data.
3694 */
3695 if (zio->io_error == 0) {
3696 while (zio->io_cksum_report != NULL) {
3697 zio_cksum_report_t *zcr = zio->io_cksum_report;
3698 uint64_t align = zcr->zcr_align;
3699 uint64_t asize = P2ROUNDUP(zio->io_size, align);
3700 char *abuf = zio->io_data;
3701
3702 if (asize != zio->io_size) {
3703 abuf = zio_buf_alloc(asize);
3704 bcopy(zio->io_data, abuf, zio->io_size);
3705 bzero(abuf+zio->io_size, asize-zio->io_size);
3706 }
3707
3708 zio->io_cksum_report = zcr->zcr_next;
3709 zcr->zcr_next = NULL;
3710 zcr->zcr_finish(zcr, abuf);
3711 zfs_ereport_free_checksum(zcr);
3712
3713 if (asize != zio->io_size)
3714 zio_buf_free(abuf, asize);
3715 }
3716 }
3717
3718 zio_pop_transforms(zio); /* note: may set zio->io_error */
3719
3720 vdev_stat_update(zio, zio->io_size);
3721
3722 /*
3723 * If this I/O is attached to a particular vdev is slow, exceeding
3724 * 30 seconds to complete, post an error described the I/O delay.
3725 * We ignore these errors if the device is currently unavailable.
3726 */
3727 if (zio->io_delay >= MSEC2NSEC(zio_delay_max)) {
3728 if (zio->io_vd != NULL && !vdev_is_dead(zio->io_vd))
3729 zfs_ereport_post(FM_EREPORT_ZFS_DELAY, zio->io_spa,
3730 zio->io_vd, zio, 0, 0);
3731 }
3732
3733 if (zio->io_error) {
3734 /*
3735 * If this I/O is attached to a particular vdev,
3736 * generate an error message describing the I/O failure
3737 * at the block level. We ignore these errors if the
3738 * device is currently unavailable.
3739 */
3740 if (zio->io_error != ECKSUM && zio->io_vd != NULL &&
3741 !vdev_is_dead(zio->io_vd))
3742 zfs_ereport_post(FM_EREPORT_ZFS_IO, zio->io_spa,
3743 zio->io_vd, zio, 0, 0);
3744
3745 if ((zio->io_error == EIO || !(zio->io_flags &
3746 (ZIO_FLAG_SPECULATIVE | ZIO_FLAG_DONT_PROPAGATE))) &&
3747 zio == zio->io_logical) {
3748 /*
3749 * For logical I/O requests, tell the SPA to log the
3750 * error and generate a logical data ereport.
3751 */
3752 spa_log_error(zio->io_spa, zio);
3753 zfs_ereport_post(FM_EREPORT_ZFS_DATA, zio->io_spa,
3754 NULL, zio, 0, 0);
3755 }
3756 }
3757
3758 if (zio->io_error && zio == zio->io_logical) {
3759 /*
3760 * Determine whether zio should be reexecuted. This will
3761 * propagate all the way to the root via zio_notify_parent().
3762 */
3763 ASSERT(zio->io_vd == NULL && zio->io_bp != NULL);
3764 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
3765
3766 if (IO_IS_ALLOCATING(zio) &&
3767 !(zio->io_flags & ZIO_FLAG_CANFAIL)) {
3768 if (zio->io_error != ENOSPC)
3769 zio->io_reexecute |= ZIO_REEXECUTE_NOW;
3770 else
3771 zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
3772 }
3773
3774 if ((zio->io_type == ZIO_TYPE_READ ||
3775 zio->io_type == ZIO_TYPE_FREE) &&
3776 !(zio->io_flags & ZIO_FLAG_SCAN_THREAD) &&
3777 zio->io_error == ENXIO &&
3778 spa_load_state(zio->io_spa) == SPA_LOAD_NONE &&
3779 spa_get_failmode(zio->io_spa) != ZIO_FAILURE_MODE_CONTINUE)
3780 zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
3781
3782 if (!(zio->io_flags & ZIO_FLAG_CANFAIL) && !zio->io_reexecute)
3783 zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
3784
3785 /*
3786 * Here is a possibly good place to attempt to do
3787 * either combinatorial reconstruction or error correction
3788 * based on checksums. It also might be a good place
3789 * to send out preliminary ereports before we suspend
3790 * processing.
3791 */
3792 }
3793
3794 /*
3795 * If there were logical child errors, they apply to us now.
3796 * We defer this until now to avoid conflating logical child
3797 * errors with errors that happened to the zio itself when
3798 * updating vdev stats and reporting FMA events above.
3799 */
3800 zio_inherit_child_errors(zio, ZIO_CHILD_LOGICAL);
3801
3802 if ((zio->io_error || zio->io_reexecute) &&
3803 IO_IS_ALLOCATING(zio) && zio->io_gang_leader == zio &&
3804 !(zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE)))
3805 zio_dva_unallocate(zio, zio->io_gang_tree, zio->io_bp);
3806
3807 zio_gang_tree_free(&zio->io_gang_tree);
3808
3809 /*
3810 * Godfather I/Os should never suspend.
3811 */
3812 if ((zio->io_flags & ZIO_FLAG_GODFATHER) &&
3813 (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND))
3814 zio->io_reexecute = 0;
3815
3816 if (zio->io_reexecute) {
3817 /*
3818 * This is a logical I/O that wants to reexecute.
3819 *
3820 * Reexecute is top-down. When an i/o fails, if it's not
3821 * the root, it simply notifies its parent and sticks around.
3822 * The parent, seeing that it still has children in zio_done(),
3823 * does the same. This percolates all the way up to the root.
3824 * The root i/o will reexecute or suspend the entire tree.
3825 *
3826 * This approach ensures that zio_reexecute() honors
3827 * all the original i/o dependency relationships, e.g.
3828 * parents not executing until children are ready.
3829 */
3830 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
3831
3832 zio->io_gang_leader = NULL;
3833
3834 mutex_enter(&zio->io_lock);
3835 zio->io_state[ZIO_WAIT_DONE] = 1;
3836 mutex_exit(&zio->io_lock);
3837
3838 /*
3839 * "The Godfather" I/O monitors its children but is
3840 * not a true parent to them. It will track them through
3841 * the pipeline but severs its ties whenever they get into
3842 * trouble (e.g. suspended). This allows "The Godfather"
3843 * I/O to return status without blocking.
3844 */
3845 zl = NULL;
3846 for (pio = zio_walk_parents(zio, &zl); pio != NULL;
3847 pio = pio_next) {
3848 zio_link_t *remove_zl = zl;
3849 pio_next = zio_walk_parents(zio, &zl);
3850
3851 if ((pio->io_flags & ZIO_FLAG_GODFATHER) &&
3852 (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND)) {
3853 zio_remove_child(pio, zio, remove_zl);
3854 zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
3855 }
3856 }
3857
3858 if ((pio = zio_unique_parent(zio)) != NULL) {
3859 /*
3860 * We're not a root i/o, so there's nothing to do
3861 * but notify our parent. Don't propagate errors
3862 * upward since we haven't permanently failed yet.
3863 */
3864 ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER));
3865 zio->io_flags |= ZIO_FLAG_DONT_PROPAGATE;
3866 zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
3867 } else if (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND) {
3868 /*
3869 * We'd fail again if we reexecuted now, so suspend
3870 * until conditions improve (e.g. device comes online).
3871 */
3872 zio_suspend(zio->io_spa, zio);
3873 } else {
3874 /*
3875 * Reexecution is potentially a huge amount of work.
3876 * Hand it off to the otherwise-unused claim taskq.
3877 */
3878 ASSERT(taskq_empty_ent(&zio->io_tqent));
3879 spa_taskq_dispatch_ent(zio->io_spa,
3880 ZIO_TYPE_CLAIM, ZIO_TASKQ_ISSUE,
3881 (task_func_t *)zio_reexecute, zio, 0,
3882 &zio->io_tqent);
3883 }
3884 return (ZIO_PIPELINE_STOP);
3885 }
3886
3887 ASSERT(zio->io_child_count == 0);
3888 ASSERT(zio->io_reexecute == 0);
3889 ASSERT(zio->io_error == 0 || (zio->io_flags & ZIO_FLAG_CANFAIL));
3890
3891 /*
3892 * Report any checksum errors, since the I/O is complete.
3893 */
3894 while (zio->io_cksum_report != NULL) {
3895 zio_cksum_report_t *zcr = zio->io_cksum_report;
3896 zio->io_cksum_report = zcr->zcr_next;
3897 zcr->zcr_next = NULL;
3898 zcr->zcr_finish(zcr, NULL);
3899 zfs_ereport_free_checksum(zcr);
3900 }
3901
3902 if (zio->io_flags & ZIO_FLAG_FASTWRITE && zio->io_bp &&
3903 !BP_IS_HOLE(zio->io_bp) && !BP_IS_EMBEDDED(zio->io_bp) &&
3904 !(zio->io_flags & ZIO_FLAG_NOPWRITE)) {
3905 metaslab_fastwrite_unmark(zio->io_spa, zio->io_bp);
3906 }
3907
3908 /*
3909 * It is the responsibility of the done callback to ensure that this
3910 * particular zio is no longer discoverable for adoption, and as
3911 * such, cannot acquire any new parents.
3912 */
3913 if (zio->io_done)
3914 zio->io_done(zio);
3915
3916 mutex_enter(&zio->io_lock);
3917 zio->io_state[ZIO_WAIT_DONE] = 1;
3918 mutex_exit(&zio->io_lock);
3919
3920 zl = NULL;
3921 for (pio = zio_walk_parents(zio, &zl); pio != NULL; pio = pio_next) {
3922 zio_link_t *remove_zl = zl;
3923 pio_next = zio_walk_parents(zio, &zl);
3924 zio_remove_child(pio, zio, remove_zl);
3925 zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
3926 }
3927
3928 if (zio->io_waiter != NULL) {
3929 mutex_enter(&zio->io_lock);
3930 zio->io_executor = NULL;
3931 cv_broadcast(&zio->io_cv);
3932 mutex_exit(&zio->io_lock);
3933 } else {
3934 zio_destroy(zio);
3935 }
3936
3937 return (ZIO_PIPELINE_STOP);
3938 }
3939
3940 /*
3941 * ==========================================================================
3942 * I/O pipeline definition
3943 * ==========================================================================
3944 */
3945 static zio_pipe_stage_t *zio_pipeline[] = {
3946 NULL,
3947 zio_read_bp_init,
3948 zio_write_bp_init,
3949 zio_free_bp_init,
3950 zio_issue_async,
3951 zio_write_compress,
3952 zio_checksum_generate,
3953 zio_nop_write,
3954 zio_ddt_read_start,
3955 zio_ddt_read_done,
3956 zio_ddt_write,
3957 zio_ddt_free,
3958 zio_gang_assemble,
3959 zio_gang_issue,
3960 zio_dva_throttle,
3961 zio_dva_allocate,
3962 zio_dva_free,
3963 zio_dva_claim,
3964 zio_ready,
3965 zio_vdev_io_start,
3966 zio_vdev_io_done,
3967 zio_vdev_io_assess,
3968 zio_checksum_verify,
3969 zio_done
3970 };
3971
3972
3973
3974
3975 /*
3976 * Compare two zbookmark_phys_t's to see which we would reach first in a
3977 * pre-order traversal of the object tree.
3978 *
3979 * This is simple in every case aside from the meta-dnode object. For all other
3980 * objects, we traverse them in order (object 1 before object 2, and so on).
3981 * However, all of these objects are traversed while traversing object 0, since
3982 * the data it points to is the list of objects. Thus, we need to convert to a
3983 * canonical representation so we can compare meta-dnode bookmarks to
3984 * non-meta-dnode bookmarks.
3985 *
3986 * We do this by calculating "equivalents" for each field of the zbookmark.
3987 * zbookmarks outside of the meta-dnode use their own object and level, and
3988 * calculate the level 0 equivalent (the first L0 blkid that is contained in the
3989 * blocks this bookmark refers to) by multiplying their blkid by their span
3990 * (the number of L0 blocks contained within one block at their level).
3991 * zbookmarks inside the meta-dnode calculate their object equivalent
3992 * (which is L0equiv * dnodes per data block), use 0 for their L0equiv, and use
3993 * level + 1<<31 (any value larger than a level could ever be) for their level.
3994 * This causes them to always compare before a bookmark in their object
3995 * equivalent, compare appropriately to bookmarks in other objects, and to
3996 * compare appropriately to other bookmarks in the meta-dnode.
3997 */
3998 int
3999 zbookmark_compare(uint16_t dbss1, uint8_t ibs1, uint16_t dbss2, uint8_t ibs2,
4000 const zbookmark_phys_t *zb1, const zbookmark_phys_t *zb2)
4001 {
4002 /*
4003 * These variables represent the "equivalent" values for the zbookmark,
4004 * after converting zbookmarks inside the meta dnode to their
4005 * normal-object equivalents.
4006 */
4007 uint64_t zb1obj, zb2obj;
4008 uint64_t zb1L0, zb2L0;
4009 uint64_t zb1level, zb2level;
4010
4011 if (zb1->zb_object == zb2->zb_object &&
4012 zb1->zb_level == zb2->zb_level &&
4013 zb1->zb_blkid == zb2->zb_blkid)
4014 return (0);
4015
4016 /*
4017 * BP_SPANB calculates the span in blocks.
4018 */
4019 zb1L0 = (zb1->zb_blkid) * BP_SPANB(ibs1, zb1->zb_level);
4020 zb2L0 = (zb2->zb_blkid) * BP_SPANB(ibs2, zb2->zb_level);
4021
4022 if (zb1->zb_object == DMU_META_DNODE_OBJECT) {
4023 zb1obj = zb1L0 * (dbss1 << (SPA_MINBLOCKSHIFT - DNODE_SHIFT));
4024 zb1L0 = 0;
4025 zb1level = zb1->zb_level + COMPARE_META_LEVEL;
4026 } else {
4027 zb1obj = zb1->zb_object;
4028 zb1level = zb1->zb_level;
4029 }
4030
4031 if (zb2->zb_object == DMU_META_DNODE_OBJECT) {
4032 zb2obj = zb2L0 * (dbss2 << (SPA_MINBLOCKSHIFT - DNODE_SHIFT));
4033 zb2L0 = 0;
4034 zb2level = zb2->zb_level + COMPARE_META_LEVEL;
4035 } else {
4036 zb2obj = zb2->zb_object;
4037 zb2level = zb2->zb_level;
4038 }
4039
4040 /* Now that we have a canonical representation, do the comparison. */
4041 if (zb1obj != zb2obj)
4042 return (zb1obj < zb2obj ? -1 : 1);
4043 else if (zb1L0 != zb2L0)
4044 return (zb1L0 < zb2L0 ? -1 : 1);
4045 else if (zb1level != zb2level)
4046 return (zb1level > zb2level ? -1 : 1);
4047 /*
4048 * This can (theoretically) happen if the bookmarks have the same object
4049 * and level, but different blkids, if the block sizes are not the same.
4050 * There is presently no way to change the indirect block sizes
4051 */
4052 return (0);
4053 }
4054
4055 /*
4056 * This function checks the following: given that last_block is the place that
4057 * our traversal stopped last time, does that guarantee that we've visited
4058 * every node under subtree_root? Therefore, we can't just use the raw output
4059 * of zbookmark_compare. We have to pass in a modified version of
4060 * subtree_root; by incrementing the block id, and then checking whether
4061 * last_block is before or equal to that, we can tell whether or not having
4062 * visited last_block implies that all of subtree_root's children have been
4063 * visited.
4064 */
4065 boolean_t
4066 zbookmark_subtree_completed(const dnode_phys_t *dnp,
4067 const zbookmark_phys_t *subtree_root, const zbookmark_phys_t *last_block)
4068 {
4069 zbookmark_phys_t mod_zb = *subtree_root;
4070 mod_zb.zb_blkid++;
4071 ASSERT(last_block->zb_level == 0);
4072
4073 /* The objset_phys_t isn't before anything. */
4074 if (dnp == NULL)
4075 return (B_FALSE);
4076
4077 /*
4078 * We pass in 1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT) for the
4079 * data block size in sectors, because that variable is only used if
4080 * the bookmark refers to a block in the meta-dnode. Since we don't
4081 * know without examining it what object it refers to, and there's no
4082 * harm in passing in this value in other cases, we always pass it in.
4083 *
4084 * We pass in 0 for the indirect block size shift because zb2 must be
4085 * level 0. The indirect block size is only used to calculate the span
4086 * of the bookmark, but since the bookmark must be level 0, the span is
4087 * always 1, so the math works out.
4088 *
4089 * If you make changes to how the zbookmark_compare code works, be sure
4090 * to make sure that this code still works afterwards.
4091 */
4092 return (zbookmark_compare(dnp->dn_datablkszsec, dnp->dn_indblkshift,
4093 1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT), 0, &mod_zb,
4094 last_block) <= 0);
4095 }
4096
4097 #if defined(_KERNEL) && defined(HAVE_SPL)
4098 EXPORT_SYMBOL(zio_type_name);
4099 EXPORT_SYMBOL(zio_buf_alloc);
4100 EXPORT_SYMBOL(zio_data_buf_alloc);
4101 EXPORT_SYMBOL(zio_buf_alloc_flags);
4102 EXPORT_SYMBOL(zio_buf_free);
4103 EXPORT_SYMBOL(zio_data_buf_free);
4104
4105 module_param(zio_delay_max, int, 0644);
4106 MODULE_PARM_DESC(zio_delay_max, "Max zio millisec delay before posting event");
4107
4108 module_param(zio_requeue_io_start_cut_in_line, int, 0644);
4109 MODULE_PARM_DESC(zio_requeue_io_start_cut_in_line, "Prioritize requeued I/O");
4110
4111 module_param(zfs_sync_pass_deferred_free, int, 0644);
4112 MODULE_PARM_DESC(zfs_sync_pass_deferred_free,
4113 "Defer frees starting in this pass");
4114
4115 module_param(zfs_sync_pass_dont_compress, int, 0644);
4116 MODULE_PARM_DESC(zfs_sync_pass_dont_compress,
4117 "Don't compress starting in this pass");
4118
4119 module_param(zfs_sync_pass_rewrite, int, 0644);
4120 MODULE_PARM_DESC(zfs_sync_pass_rewrite,
4121 "Rewrite new bps starting in this pass");
4122
4123 module_param(zio_dva_throttle_enabled, int, 0644);
4124 MODULE_PARM_DESC(zio_dva_throttle_enabled,
4125 "Throttle block allocations in the ZIO pipeline");
4126 #endif