Merge commit '5cabbc6b49070407fb9610cfe73d4c0e0dea3e77' into merges
[unleashed.git] / kernel / fs / zfs / zio.c
blob3f2199e0f795b66e302280f051121e3dffce0f08
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2011, 2017 by Delphix. All rights reserved.
24 * Copyright (c) 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2014 Integros [integros.com]
28 #include <sys/sysmacros.h>
29 #include <sys/zfs_context.h>
30 #include <sys/fm/fs/zfs.h>
31 #include <sys/spa.h>
32 #include <sys/txg.h>
33 #include <sys/spa_impl.h>
34 #include <sys/vdev_impl.h>
35 #include <sys/zio_impl.h>
36 #include <sys/zio_compress.h>
37 #include <sys/zio_checksum.h>
38 #include <sys/dmu_objset.h>
39 #include <sys/arc.h>
40 #include <sys/ddt.h>
41 #include <sys/blkptr.h>
42 #include <sys/zfeature.h>
43 #include <sys/metaslab_impl.h>
44 #include <sys/abd.h>
47 * ==========================================================================
48 * I/O type descriptions
49 * ==========================================================================
51 const char *zio_type_name[ZIO_TYPES] = {
52 "zio_null", "zio_read", "zio_write", "zio_free", "zio_claim",
53 "zio_ioctl"
56 boolean_t zio_dva_throttle_enabled = B_TRUE;
59 * ==========================================================================
60 * I/O kmem caches
61 * ==========================================================================
63 kmem_cache_t *zio_cache;
64 kmem_cache_t *zio_link_cache;
65 kmem_cache_t *zio_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
66 kmem_cache_t *zio_data_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
68 #ifdef _KERNEL
69 extern vmem_t *zio_alloc_arena;
70 #endif
72 #define ZIO_PIPELINE_CONTINUE 0x100
73 #define ZIO_PIPELINE_STOP 0x101
75 #define BP_SPANB(indblkshift, level) \
76 (((uint64_t)1) << ((level) * ((indblkshift) - SPA_BLKPTRSHIFT)))
77 #define COMPARE_META_LEVEL 0x80000000ul
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.
87 * The 'zfs_sync_pass_deferred_free' pass must be greater than 1 to ensure that
88 * regular blocks are not deferred.
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 */
95 * An allocating zio is one that either currently has the DVA allocate
96 * stage set or will have it later in its lifetime.
98 #define IO_IS_ALLOCATING(zio) ((zio)->io_orig_pipeline & ZIO_STAGE_DVA_ALLOCATE)
100 boolean_t zio_requeue_io_start_cut_in_line = B_TRUE;
102 #ifdef ZFS_DEBUG
103 int zio_buf_debug_limit = 16384;
104 #else
105 int zio_buf_debug_limit = 0;
106 #endif
108 static void zio_taskq_dispatch(zio_t *, zio_taskq_type_t, boolean_t);
110 void
111 zio_init(void)
113 size_t c;
114 vmem_t *data_alloc_arena = NULL;
116 #ifdef _KERNEL
117 data_alloc_arena = zio_alloc_arena;
118 #endif
119 zio_cache = kmem_cache_create("zio_cache",
120 sizeof (zio_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
121 zio_link_cache = kmem_cache_create("zio_link_cache",
122 sizeof (zio_link_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
125 * For small buffers, we want a cache for each multiple of
126 * SPA_MINBLOCKSIZE. For larger buffers, we want a cache
127 * for each quarter-power of 2.
129 for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) {
130 size_t size = (c + 1) << SPA_MINBLOCKSHIFT;
131 size_t p2 = size;
132 size_t align = 0;
133 size_t cflags = (size > zio_buf_debug_limit) ? KMC_NODEBUG : 0;
135 while (!ISP2(p2))
136 p2 &= p2 - 1;
138 #ifndef _KERNEL
140 * If we are using watchpoints, put each buffer on its own page,
141 * to eliminate the performance overhead of trapping to the
142 * kernel when modifying a non-watched buffer that shares the
143 * page with a watched buffer.
145 if (arc_watch && !IS_P2ALIGNED(size, PAGESIZE))
146 continue;
147 #endif
148 if (size <= 4 * SPA_MINBLOCKSIZE) {
149 align = SPA_MINBLOCKSIZE;
150 } else if (IS_P2ALIGNED(size, p2 >> 2)) {
151 align = MIN(p2 >> 2, PAGESIZE);
154 if (align != 0) {
155 char name[36];
156 (void) sprintf(name, "zio_buf_%lu", (ulong_t)size);
157 zio_buf_cache[c] = kmem_cache_create(name, size,
158 align, NULL, NULL, NULL, NULL, NULL, cflags);
161 * Since zio_data bufs do not appear in crash dumps, we
162 * pass KMC_NOTOUCH so that no allocator metadata is
163 * stored with the buffers.
165 (void) sprintf(name, "zio_data_buf_%lu", (ulong_t)size);
166 zio_data_buf_cache[c] = kmem_cache_create(name, size,
167 align, NULL, NULL, NULL, NULL, data_alloc_arena,
168 cflags | KMC_NOTOUCH);
172 while (--c != 0) {
173 ASSERT(zio_buf_cache[c] != NULL);
174 if (zio_buf_cache[c - 1] == NULL)
175 zio_buf_cache[c - 1] = zio_buf_cache[c];
177 ASSERT(zio_data_buf_cache[c] != NULL);
178 if (zio_data_buf_cache[c - 1] == NULL)
179 zio_data_buf_cache[c - 1] = zio_data_buf_cache[c];
182 zio_inject_init();
185 void
186 zio_fini(void)
188 size_t c;
189 kmem_cache_t *last_cache = NULL;
190 kmem_cache_t *last_data_cache = NULL;
192 for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) {
193 if (zio_buf_cache[c] != last_cache) {
194 last_cache = zio_buf_cache[c];
195 kmem_cache_destroy(zio_buf_cache[c]);
197 zio_buf_cache[c] = NULL;
199 if (zio_data_buf_cache[c] != last_data_cache) {
200 last_data_cache = zio_data_buf_cache[c];
201 kmem_cache_destroy(zio_data_buf_cache[c]);
203 zio_data_buf_cache[c] = NULL;
206 kmem_cache_destroy(zio_link_cache);
207 kmem_cache_destroy(zio_cache);
209 zio_inject_fini();
213 * ==========================================================================
214 * Allocate and free I/O buffers
215 * ==========================================================================
219 * Use zio_buf_alloc to allocate ZFS metadata. This data will appear in a
220 * crashdump if the kernel panics, so use it judiciously. Obviously, it's
221 * useful to inspect ZFS metadata, but if possible, we should avoid keeping
222 * excess / transient data in-core during a crashdump.
224 void *
225 zio_buf_alloc(size_t size)
227 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
229 VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
231 return (kmem_cache_alloc(zio_buf_cache[c], KM_PUSHPAGE));
235 * Use zio_data_buf_alloc to allocate data. The data will not appear in a
236 * crashdump if the kernel panics. This exists so that we will limit the amount
237 * of ZFS data that shows up in a kernel crashdump. (Thus reducing the amount
238 * of kernel heap dumped to disk when the kernel panics)
240 void *
241 zio_data_buf_alloc(size_t size)
243 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
245 VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
247 return (kmem_cache_alloc(zio_data_buf_cache[c], KM_PUSHPAGE));
250 void
251 zio_buf_free(void *buf, size_t size)
253 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
255 VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
257 kmem_cache_free(zio_buf_cache[c], buf);
260 void
261 zio_data_buf_free(void *buf, size_t size)
263 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
265 VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
267 kmem_cache_free(zio_data_buf_cache[c], buf);
271 * ==========================================================================
272 * Push and pop I/O transform buffers
273 * ==========================================================================
275 void
276 zio_push_transform(zio_t *zio, abd_t *data, uint64_t size, uint64_t bufsize,
277 zio_transform_func_t *transform)
279 zio_transform_t *zt = kmem_alloc(sizeof (zio_transform_t), KM_SLEEP);
282 * Ensure that anyone expecting this zio to contain a linear ABD isn't
283 * going to get a nasty surprise when they try to access the data.
285 IMPLY(abd_is_linear(zio->io_abd), abd_is_linear(data));
287 zt->zt_orig_abd = zio->io_abd;
288 zt->zt_orig_size = zio->io_size;
289 zt->zt_bufsize = bufsize;
290 zt->zt_transform = transform;
292 zt->zt_next = zio->io_transform_stack;
293 zio->io_transform_stack = zt;
295 zio->io_abd = data;
296 zio->io_size = size;
299 void
300 zio_pop_transforms(zio_t *zio)
302 zio_transform_t *zt;
304 while ((zt = zio->io_transform_stack) != NULL) {
305 if (zt->zt_transform != NULL)
306 zt->zt_transform(zio,
307 zt->zt_orig_abd, zt->zt_orig_size);
309 if (zt->zt_bufsize != 0)
310 abd_free(zio->io_abd);
312 zio->io_abd = zt->zt_orig_abd;
313 zio->io_size = zt->zt_orig_size;
314 zio->io_transform_stack = zt->zt_next;
316 kmem_free(zt, sizeof (zio_transform_t));
321 * ==========================================================================
322 * I/O transform callbacks for subblocks and decompression
323 * ==========================================================================
325 static void
326 zio_subblock(zio_t *zio, abd_t *data, uint64_t size)
328 ASSERT(zio->io_size > size);
330 if (zio->io_type == ZIO_TYPE_READ)
331 abd_copy(data, zio->io_abd, size);
334 static void
335 zio_decompress(zio_t *zio, abd_t *data, uint64_t size)
337 if (zio->io_error == 0) {
338 void *tmp = abd_borrow_buf(data, size);
339 int ret = zio_decompress_data(BP_GET_COMPRESS(zio->io_bp),
340 zio->io_abd, tmp, zio->io_size, size);
341 abd_return_buf_copy(data, tmp, size);
343 if (ret != 0)
344 zio->io_error = SET_ERROR(EIO);
349 * ==========================================================================
350 * I/O parent/child relationships and pipeline interlocks
351 * ==========================================================================
353 zio_t *
354 zio_walk_parents(zio_t *cio, zio_link_t **zl)
356 list_t *pl = &cio->io_parent_list;
358 *zl = (*zl == NULL) ? list_head(pl) : list_next(pl, *zl);
359 if (*zl == NULL)
360 return (NULL);
362 ASSERT((*zl)->zl_child == cio);
363 return ((*zl)->zl_parent);
366 zio_t *
367 zio_walk_children(zio_t *pio, zio_link_t **zl)
369 list_t *cl = &pio->io_child_list;
371 *zl = (*zl == NULL) ? list_head(cl) : list_next(cl, *zl);
372 if (*zl == NULL)
373 return (NULL);
375 ASSERT((*zl)->zl_parent == pio);
376 return ((*zl)->zl_child);
379 zio_t *
380 zio_unique_parent(zio_t *cio)
382 zio_link_t *zl = NULL;
383 zio_t *pio = zio_walk_parents(cio, &zl);
385 VERIFY3P(zio_walk_parents(cio, &zl), ==, NULL);
386 return (pio);
389 void
390 zio_add_child(zio_t *pio, zio_t *cio)
392 zio_link_t *zl = kmem_cache_alloc(zio_link_cache, KM_SLEEP);
395 * Logical I/Os can have logical, gang, or vdev children.
396 * Gang I/Os can have gang or vdev children.
397 * Vdev I/Os can only have vdev children.
398 * The following ASSERT captures all of these constraints.
400 ASSERT3S(cio->io_child_type, <=, pio->io_child_type);
402 zl->zl_parent = pio;
403 zl->zl_child = cio;
405 mutex_enter(&cio->io_lock);
406 mutex_enter(&pio->io_lock);
408 ASSERT(pio->io_state[ZIO_WAIT_DONE] == 0);
410 for (int w = 0; w < ZIO_WAIT_TYPES; w++)
411 pio->io_children[cio->io_child_type][w] += !cio->io_state[w];
413 list_insert_head(&pio->io_child_list, zl);
414 list_insert_head(&cio->io_parent_list, zl);
416 pio->io_child_count++;
417 cio->io_parent_count++;
419 mutex_exit(&pio->io_lock);
420 mutex_exit(&cio->io_lock);
423 static void
424 zio_remove_child(zio_t *pio, zio_t *cio, zio_link_t *zl)
426 ASSERT(zl->zl_parent == pio);
427 ASSERT(zl->zl_child == cio);
429 mutex_enter(&cio->io_lock);
430 mutex_enter(&pio->io_lock);
432 list_remove(&pio->io_child_list, zl);
433 list_remove(&cio->io_parent_list, zl);
435 pio->io_child_count--;
436 cio->io_parent_count--;
438 mutex_exit(&pio->io_lock);
439 mutex_exit(&cio->io_lock);
441 kmem_cache_free(zio_link_cache, zl);
444 static boolean_t
445 zio_wait_for_children(zio_t *zio, enum zio_child child, enum zio_wait_type wait)
447 uint64_t *countp = &zio->io_children[child][wait];
448 boolean_t waiting = B_FALSE;
450 mutex_enter(&zio->io_lock);
451 ASSERT(zio->io_stall == NULL);
452 if (*countp != 0) {
453 zio->io_stage >>= 1;
454 ASSERT3U(zio->io_stage, !=, ZIO_STAGE_OPEN);
455 zio->io_stall = countp;
456 waiting = B_TRUE;
458 mutex_exit(&zio->io_lock);
460 return (waiting);
463 static void
464 zio_notify_parent(zio_t *pio, zio_t *zio, enum zio_wait_type wait)
466 uint64_t *countp = &pio->io_children[zio->io_child_type][wait];
467 int *errorp = &pio->io_child_error[zio->io_child_type];
469 mutex_enter(&pio->io_lock);
470 if (zio->io_error && !(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
471 *errorp = zio_worst_error(*errorp, zio->io_error);
472 pio->io_reexecute |= zio->io_reexecute;
473 ASSERT3U(*countp, >, 0);
475 (*countp)--;
477 if (*countp == 0 && pio->io_stall == countp) {
478 zio_taskq_type_t type =
479 pio->io_stage < ZIO_STAGE_VDEV_IO_START ? ZIO_TASKQ_ISSUE :
480 ZIO_TASKQ_INTERRUPT;
481 pio->io_stall = NULL;
482 mutex_exit(&pio->io_lock);
484 * Dispatch the parent zio in its own taskq so that
485 * the child can continue to make progress. This also
486 * prevents overflowing the stack when we have deeply nested
487 * parent-child relationships.
489 zio_taskq_dispatch(pio, type, B_FALSE);
490 } else {
491 mutex_exit(&pio->io_lock);
495 static void
496 zio_inherit_child_errors(zio_t *zio, enum zio_child c)
498 if (zio->io_child_error[c] != 0 && zio->io_error == 0)
499 zio->io_error = zio->io_child_error[c];
503 zio_bookmark_compare(const void *x1, const void *x2)
505 const zio_t *z1 = x1;
506 const zio_t *z2 = x2;
508 if (z1->io_bookmark.zb_objset < z2->io_bookmark.zb_objset)
509 return (-1);
510 if (z1->io_bookmark.zb_objset > z2->io_bookmark.zb_objset)
511 return (1);
513 if (z1->io_bookmark.zb_object < z2->io_bookmark.zb_object)
514 return (-1);
515 if (z1->io_bookmark.zb_object > z2->io_bookmark.zb_object)
516 return (1);
518 if (z1->io_bookmark.zb_level < z2->io_bookmark.zb_level)
519 return (-1);
520 if (z1->io_bookmark.zb_level > z2->io_bookmark.zb_level)
521 return (1);
523 if (z1->io_bookmark.zb_blkid < z2->io_bookmark.zb_blkid)
524 return (-1);
525 if (z1->io_bookmark.zb_blkid > z2->io_bookmark.zb_blkid)
526 return (1);
528 if (z1 < z2)
529 return (-1);
530 if (z1 > z2)
531 return (1);
533 return (0);
537 * ==========================================================================
538 * Create the various types of I/O (read, write, free, etc)
539 * ==========================================================================
541 static zio_t *
542 zio_create(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
543 abd_t *data, uint64_t lsize, uint64_t psize, zio_done_func_t *done,
544 void *private, zio_type_t type, zio_priority_t priority,
545 enum zio_flag flags, vdev_t *vd, uint64_t offset,
546 const zbookmark_phys_t *zb, enum zio_stage stage, enum zio_stage pipeline)
548 zio_t *zio;
550 ASSERT3U(psize, <=, SPA_MAXBLOCKSIZE);
551 ASSERT(P2PHASE(psize, SPA_MINBLOCKSIZE) == 0);
552 ASSERT(P2PHASE(offset, SPA_MINBLOCKSIZE) == 0);
554 ASSERT(!vd || spa_config_held(spa, SCL_STATE_ALL, RW_READER));
555 ASSERT(!bp || !(flags & ZIO_FLAG_CONFIG_WRITER));
556 ASSERT(vd || stage == ZIO_STAGE_OPEN);
558 IMPLY(lsize != psize, (flags & ZIO_FLAG_RAW) != 0);
560 zio = kmem_cache_alloc(zio_cache, KM_SLEEP);
561 bzero(zio, sizeof (zio_t));
563 mutex_init(&zio->io_lock, NULL, MUTEX_DEFAULT, NULL);
564 cv_init(&zio->io_cv, NULL, CV_DEFAULT, NULL);
566 list_create(&zio->io_parent_list, sizeof (zio_link_t),
567 offsetof(zio_link_t, zl_parent_node));
568 list_create(&zio->io_child_list, sizeof (zio_link_t),
569 offsetof(zio_link_t, zl_child_node));
570 metaslab_trace_init(&zio->io_alloc_list);
572 if (vd != NULL)
573 zio->io_child_type = ZIO_CHILD_VDEV;
574 else if (flags & ZIO_FLAG_GANG_CHILD)
575 zio->io_child_type = ZIO_CHILD_GANG;
576 else if (flags & ZIO_FLAG_DDT_CHILD)
577 zio->io_child_type = ZIO_CHILD_DDT;
578 else
579 zio->io_child_type = ZIO_CHILD_LOGICAL;
581 if (bp != NULL) {
582 zio->io_bp = (blkptr_t *)bp;
583 zio->io_bp_copy = *bp;
584 zio->io_bp_orig = *bp;
585 if (type != ZIO_TYPE_WRITE ||
586 zio->io_child_type == ZIO_CHILD_DDT)
587 zio->io_bp = &zio->io_bp_copy; /* so caller can free */
588 if (zio->io_child_type == ZIO_CHILD_LOGICAL)
589 zio->io_logical = zio;
590 if (zio->io_child_type > ZIO_CHILD_GANG && BP_IS_GANG(bp))
591 pipeline |= ZIO_GANG_STAGES;
594 zio->io_spa = spa;
595 zio->io_txg = txg;
596 zio->io_done = done;
597 zio->io_private = private;
598 zio->io_type = type;
599 zio->io_priority = priority;
600 zio->io_vd = vd;
601 zio->io_offset = offset;
602 zio->io_orig_abd = zio->io_abd = data;
603 zio->io_orig_size = zio->io_size = psize;
604 zio->io_lsize = lsize;
605 zio->io_orig_flags = zio->io_flags = flags;
606 zio->io_orig_stage = zio->io_stage = stage;
607 zio->io_orig_pipeline = zio->io_pipeline = pipeline;
608 zio->io_pipeline_trace = ZIO_STAGE_OPEN;
610 zio->io_state[ZIO_WAIT_READY] = (stage >= ZIO_STAGE_READY);
611 zio->io_state[ZIO_WAIT_DONE] = (stage >= ZIO_STAGE_DONE);
613 if (zb != NULL)
614 zio->io_bookmark = *zb;
616 if (pio != NULL) {
617 if (zio->io_logical == NULL)
618 zio->io_logical = pio->io_logical;
619 if (zio->io_child_type == ZIO_CHILD_GANG)
620 zio->io_gang_leader = pio->io_gang_leader;
621 zio_add_child(pio, zio);
624 return (zio);
627 static void
628 zio_destroy(zio_t *zio)
630 metaslab_trace_fini(&zio->io_alloc_list);
631 list_destroy(&zio->io_parent_list);
632 list_destroy(&zio->io_child_list);
633 mutex_destroy(&zio->io_lock);
634 cv_destroy(&zio->io_cv);
635 kmem_cache_free(zio_cache, zio);
638 zio_t *
639 zio_null(zio_t *pio, spa_t *spa, vdev_t *vd, zio_done_func_t *done,
640 void *private, enum zio_flag flags)
642 zio_t *zio;
644 zio = zio_create(pio, spa, 0, NULL, NULL, 0, 0, done, private,
645 ZIO_TYPE_NULL, ZIO_PRIORITY_NOW, flags, vd, 0, NULL,
646 ZIO_STAGE_OPEN, ZIO_INTERLOCK_PIPELINE);
648 return (zio);
651 zio_t *
652 zio_root(spa_t *spa, zio_done_func_t *done, void *private, enum zio_flag flags)
654 return (zio_null(NULL, spa, NULL, done, private, flags));
657 void
658 zfs_blkptr_verify(spa_t *spa, const blkptr_t *bp)
660 if (!DMU_OT_IS_VALID(BP_GET_TYPE(bp))) {
661 zfs_panic_recover("blkptr at %p has invalid TYPE %llu",
662 bp, (longlong_t)BP_GET_TYPE(bp));
664 if (BP_GET_CHECKSUM(bp) >= ZIO_CHECKSUM_FUNCTIONS ||
665 BP_GET_CHECKSUM(bp) <= ZIO_CHECKSUM_ON) {
666 zfs_panic_recover("blkptr at %p has invalid CHECKSUM %llu",
667 bp, (longlong_t)BP_GET_CHECKSUM(bp));
669 if (BP_GET_COMPRESS(bp) >= ZIO_COMPRESS_FUNCTIONS ||
670 BP_GET_COMPRESS(bp) <= ZIO_COMPRESS_ON) {
671 zfs_panic_recover("blkptr at %p has invalid COMPRESS %llu",
672 bp, (longlong_t)BP_GET_COMPRESS(bp));
674 if (BP_GET_LSIZE(bp) > SPA_MAXBLOCKSIZE) {
675 zfs_panic_recover("blkptr at %p has invalid LSIZE %llu",
676 bp, (longlong_t)BP_GET_LSIZE(bp));
678 if (BP_GET_PSIZE(bp) > SPA_MAXBLOCKSIZE) {
679 zfs_panic_recover("blkptr at %p has invalid PSIZE %llu",
680 bp, (longlong_t)BP_GET_PSIZE(bp));
683 if (BP_IS_EMBEDDED(bp)) {
684 if (BPE_GET_ETYPE(bp) > NUM_BP_EMBEDDED_TYPES) {
685 zfs_panic_recover("blkptr at %p has invalid ETYPE %llu",
686 bp, (longlong_t)BPE_GET_ETYPE(bp));
691 * Pool-specific checks.
693 * Note: it would be nice to verify that the blk_birth and
694 * BP_PHYSICAL_BIRTH() are not too large. However, spa_freeze()
695 * allows the birth time of log blocks (and dmu_sync()-ed blocks
696 * that are in the log) to be arbitrarily large.
698 for (int i = 0; i < BP_GET_NDVAS(bp); i++) {
699 uint64_t vdevid = DVA_GET_VDEV(&bp->blk_dva[i]);
700 if (vdevid >= spa->spa_root_vdev->vdev_children) {
701 zfs_panic_recover("blkptr at %p DVA %u has invalid "
702 "VDEV %llu",
703 bp, i, (longlong_t)vdevid);
704 continue;
706 vdev_t *vd = spa->spa_root_vdev->vdev_child[vdevid];
707 if (vd == NULL) {
708 zfs_panic_recover("blkptr at %p DVA %u has invalid "
709 "VDEV %llu",
710 bp, i, (longlong_t)vdevid);
711 continue;
713 if (vd->vdev_ops == &vdev_hole_ops) {
714 zfs_panic_recover("blkptr at %p DVA %u has hole "
715 "VDEV %llu",
716 bp, i, (longlong_t)vdevid);
717 continue;
719 if (vd->vdev_ops == &vdev_missing_ops) {
721 * "missing" vdevs are valid during import, but we
722 * don't have their detailed info (e.g. asize), so
723 * we can't perform any more checks on them.
725 continue;
727 uint64_t offset = DVA_GET_OFFSET(&bp->blk_dva[i]);
728 uint64_t asize = DVA_GET_ASIZE(&bp->blk_dva[i]);
729 if (BP_IS_GANG(bp))
730 asize = vdev_psize_to_asize(vd, SPA_GANGBLOCKSIZE);
731 if (offset + asize > vd->vdev_asize) {
732 zfs_panic_recover("blkptr at %p DVA %u has invalid "
733 "OFFSET %llu",
734 bp, i, (longlong_t)offset);
739 zio_t *
740 zio_read(zio_t *pio, spa_t *spa, const blkptr_t *bp,
741 abd_t *data, uint64_t size, zio_done_func_t *done, void *private,
742 zio_priority_t priority, enum zio_flag flags, const zbookmark_phys_t *zb)
744 zio_t *zio;
746 zfs_blkptr_verify(spa, bp);
748 zio = zio_create(pio, spa, BP_PHYSICAL_BIRTH(bp), bp,
749 data, size, size, done, private,
750 ZIO_TYPE_READ, priority, flags, NULL, 0, zb,
751 ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ?
752 ZIO_DDT_CHILD_READ_PIPELINE : ZIO_READ_PIPELINE);
754 return (zio);
757 zio_t *
758 zio_write(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
759 abd_t *data, uint64_t lsize, uint64_t psize, const zio_prop_t *zp,
760 zio_done_func_t *ready, zio_done_func_t *children_ready,
761 zio_done_func_t *physdone, zio_done_func_t *done,
762 void *private, zio_priority_t priority, enum zio_flag flags,
763 const zbookmark_phys_t *zb)
765 zio_t *zio;
767 ASSERT(zp->zp_checksum >= ZIO_CHECKSUM_OFF &&
768 zp->zp_checksum < ZIO_CHECKSUM_FUNCTIONS &&
769 zp->zp_compress >= ZIO_COMPRESS_OFF &&
770 zp->zp_compress < ZIO_COMPRESS_FUNCTIONS &&
771 DMU_OT_IS_VALID(zp->zp_type) &&
772 zp->zp_level < 32 &&
773 zp->zp_copies > 0 &&
774 zp->zp_copies <= spa_max_replication(spa));
776 zio = zio_create(pio, spa, txg, bp, data, lsize, psize, done, private,
777 ZIO_TYPE_WRITE, priority, flags, NULL, 0, zb,
778 ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ?
779 ZIO_DDT_CHILD_WRITE_PIPELINE : ZIO_WRITE_PIPELINE);
781 zio->io_ready = ready;
782 zio->io_children_ready = children_ready;
783 zio->io_physdone = physdone;
784 zio->io_prop = *zp;
787 * Data can be NULL if we are going to call zio_write_override() to
788 * provide the already-allocated BP. But we may need the data to
789 * verify a dedup hit (if requested). In this case, don't try to
790 * dedup (just take the already-allocated BP verbatim).
792 if (data == NULL && zio->io_prop.zp_dedup_verify) {
793 zio->io_prop.zp_dedup = zio->io_prop.zp_dedup_verify = B_FALSE;
796 return (zio);
799 zio_t *
800 zio_rewrite(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, abd_t *data,
801 uint64_t size, zio_done_func_t *done, void *private,
802 zio_priority_t priority, enum zio_flag flags, zbookmark_phys_t *zb)
804 zio_t *zio;
806 zio = zio_create(pio, spa, txg, bp, data, size, size, done, private,
807 ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_IO_REWRITE, NULL, 0, zb,
808 ZIO_STAGE_OPEN, ZIO_REWRITE_PIPELINE);
810 return (zio);
813 void
814 zio_write_override(zio_t *zio, blkptr_t *bp, int copies, boolean_t nopwrite)
816 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
817 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
818 ASSERT(zio->io_stage == ZIO_STAGE_OPEN);
819 ASSERT(zio->io_txg == spa_syncing_txg(zio->io_spa));
822 * We must reset the io_prop to match the values that existed
823 * when the bp was first written by dmu_sync() keeping in mind
824 * that nopwrite and dedup are mutually exclusive.
826 zio->io_prop.zp_dedup = nopwrite ? B_FALSE : zio->io_prop.zp_dedup;
827 zio->io_prop.zp_nopwrite = nopwrite;
828 zio->io_prop.zp_copies = copies;
829 zio->io_bp_override = bp;
832 void
833 zio_free(spa_t *spa, uint64_t txg, const blkptr_t *bp)
836 zfs_blkptr_verify(spa, bp);
839 * The check for EMBEDDED is a performance optimization. We
840 * process the free here (by ignoring it) rather than
841 * putting it on the list and then processing it in zio_free_sync().
843 if (BP_IS_EMBEDDED(bp))
844 return;
845 metaslab_check_free(spa, bp);
848 * Frees that are for the currently-syncing txg, are not going to be
849 * deferred, and which will not need to do a read (i.e. not GANG or
850 * DEDUP), can be processed immediately. Otherwise, put them on the
851 * in-memory list for later processing.
853 if (BP_IS_GANG(bp) || BP_GET_DEDUP(bp) ||
854 txg != spa->spa_syncing_txg ||
855 spa_sync_pass(spa) >= zfs_sync_pass_deferred_free) {
856 bplist_append(&spa->spa_free_bplist[txg & TXG_MASK], bp);
857 } else {
858 VERIFY0(zio_wait(zio_free_sync(NULL, spa, txg, bp, 0)));
862 zio_t *
863 zio_free_sync(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
864 enum zio_flag flags)
866 zio_t *zio;
867 enum zio_stage stage = ZIO_FREE_PIPELINE;
869 ASSERT(!BP_IS_HOLE(bp));
870 ASSERT(spa_syncing_txg(spa) == txg);
871 ASSERT(spa_sync_pass(spa) < zfs_sync_pass_deferred_free);
873 if (BP_IS_EMBEDDED(bp))
874 return (zio_null(pio, spa, NULL, NULL, NULL, 0));
876 metaslab_check_free(spa, bp);
877 arc_freed(spa, bp);
880 * GANG and DEDUP blocks can induce a read (for the gang block header,
881 * or the DDT), so issue them asynchronously so that this thread is
882 * not tied up.
884 if (BP_IS_GANG(bp) || BP_GET_DEDUP(bp))
885 stage |= ZIO_STAGE_ISSUE_ASYNC;
887 zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp),
888 BP_GET_PSIZE(bp), NULL, NULL, ZIO_TYPE_FREE, ZIO_PRIORITY_NOW,
889 flags, NULL, 0, NULL, ZIO_STAGE_OPEN, stage);
891 return (zio);
894 zio_t *
895 zio_claim(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
896 zio_done_func_t *done, void *private, enum zio_flag flags)
898 zio_t *zio;
900 zfs_blkptr_verify(spa, bp);
902 if (BP_IS_EMBEDDED(bp))
903 return (zio_null(pio, spa, NULL, NULL, NULL, 0));
906 * A claim is an allocation of a specific block. Claims are needed
907 * to support immediate writes in the intent log. The issue is that
908 * immediate writes contain committed data, but in a txg that was
909 * *not* committed. Upon opening the pool after an unclean shutdown,
910 * the intent log claims all blocks that contain immediate write data
911 * so that the SPA knows they're in use.
913 * All claims *must* be resolved in the first txg -- before the SPA
914 * starts allocating blocks -- so that nothing is allocated twice.
915 * If txg == 0 we just verify that the block is claimable.
917 ASSERT3U(spa->spa_uberblock.ub_rootbp.blk_birth, <, spa_first_txg(spa));
918 ASSERT(txg == spa_first_txg(spa) || txg == 0);
919 ASSERT(!BP_GET_DEDUP(bp) || !spa_writeable(spa)); /* zdb(8) */
921 zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp),
922 BP_GET_PSIZE(bp), done, private, ZIO_TYPE_CLAIM, ZIO_PRIORITY_NOW,
923 flags, NULL, 0, NULL, ZIO_STAGE_OPEN, ZIO_CLAIM_PIPELINE);
924 ASSERT0(zio->io_queued_timestamp);
926 return (zio);
929 zio_t *
930 zio_ioctl(zio_t *pio, spa_t *spa, vdev_t *vd, int cmd,
931 zio_done_func_t *done, void *private, enum zio_flag flags)
933 zio_t *zio;
934 int c;
936 if (vd->vdev_children == 0) {
937 zio = zio_create(pio, spa, 0, NULL, NULL, 0, 0, done, private,
938 ZIO_TYPE_IOCTL, ZIO_PRIORITY_NOW, flags, vd, 0, NULL,
939 ZIO_STAGE_OPEN, ZIO_IOCTL_PIPELINE);
941 zio->io_cmd = cmd;
942 } else {
943 zio = zio_null(pio, spa, NULL, NULL, NULL, flags);
945 for (c = 0; c < vd->vdev_children; c++)
946 zio_nowait(zio_ioctl(zio, spa, vd->vdev_child[c], cmd,
947 done, private, flags));
950 return (zio);
953 zio_t *
954 zio_read_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
955 abd_t *data, int checksum, zio_done_func_t *done, void *private,
956 zio_priority_t priority, enum zio_flag flags, boolean_t labels)
958 zio_t *zio;
960 ASSERT(vd->vdev_children == 0);
961 ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE ||
962 offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
963 ASSERT3U(offset + size, <=, vd->vdev_psize);
965 zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, size, done,
966 private, ZIO_TYPE_READ, priority, flags | ZIO_FLAG_PHYSICAL, vd,
967 offset, NULL, ZIO_STAGE_OPEN, ZIO_READ_PHYS_PIPELINE);
969 zio->io_prop.zp_checksum = checksum;
971 return (zio);
974 zio_t *
975 zio_write_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
976 abd_t *data, int checksum, zio_done_func_t *done, void *private,
977 zio_priority_t priority, enum zio_flag flags, boolean_t labels)
979 zio_t *zio;
981 ASSERT(vd->vdev_children == 0);
982 ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE ||
983 offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
984 ASSERT3U(offset + size, <=, vd->vdev_psize);
986 zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, size, done,
987 private, ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_PHYSICAL, vd,
988 offset, NULL, ZIO_STAGE_OPEN, ZIO_WRITE_PHYS_PIPELINE);
990 zio->io_prop.zp_checksum = checksum;
992 if (zio_checksum_table[checksum].ci_flags & ZCHECKSUM_FLAG_EMBEDDED) {
994 * zec checksums are necessarily destructive -- they modify
995 * the end of the write buffer to hold the verifier/checksum.
996 * Therefore, we must make a local copy in case the data is
997 * being written to multiple places in parallel.
999 abd_t *wbuf = abd_alloc_sametype(data, size);
1000 abd_copy(wbuf, data, size);
1002 zio_push_transform(zio, wbuf, size, size, NULL);
1005 return (zio);
1009 * Create a child I/O to do some work for us.
1011 zio_t *
1012 zio_vdev_child_io(zio_t *pio, blkptr_t *bp, vdev_t *vd, uint64_t offset,
1013 abd_t *data, uint64_t size, int type, zio_priority_t priority,
1014 enum zio_flag flags, zio_done_func_t *done, void *private)
1016 enum zio_stage pipeline = ZIO_VDEV_CHILD_PIPELINE;
1017 zio_t *zio;
1020 * vdev child I/Os do not propagate their error to the parent.
1021 * Therefore, for correct operation the caller *must* check for
1022 * and handle the error in the child i/o's done callback.
1023 * The only exceptions are i/os that we don't care about
1024 * (OPTIONAL or REPAIR).
1026 ASSERT((flags & ZIO_FLAG_OPTIONAL) || (flags & ZIO_FLAG_IO_REPAIR) ||
1027 done != NULL);
1030 * In the common case, where the parent zio was to a normal vdev,
1031 * the child zio must be to a child vdev of that vdev. Otherwise,
1032 * the child zio must be to a top-level vdev.
1034 if (pio->io_vd != NULL && pio->io_vd->vdev_ops != &vdev_indirect_ops) {
1035 ASSERT3P(vd->vdev_parent, ==, pio->io_vd);
1036 } else {
1037 ASSERT3P(vd, ==, vd->vdev_top);
1040 if (type == ZIO_TYPE_READ && bp != NULL) {
1042 * If we have the bp, then the child should perform the
1043 * checksum and the parent need not. This pushes error
1044 * detection as close to the leaves as possible and
1045 * eliminates redundant checksums in the interior nodes.
1047 pipeline |= ZIO_STAGE_CHECKSUM_VERIFY;
1048 pio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY;
1051 if (vd->vdev_ops->vdev_op_leaf) {
1052 ASSERT0(vd->vdev_children);
1053 offset += VDEV_LABEL_START_SIZE;
1056 flags |= ZIO_VDEV_CHILD_FLAGS(pio);
1059 * If we've decided to do a repair, the write is not speculative --
1060 * even if the original read was.
1062 if (flags & ZIO_FLAG_IO_REPAIR)
1063 flags &= ~ZIO_FLAG_SPECULATIVE;
1066 * If we're creating a child I/O that is not associated with a
1067 * top-level vdev, then the child zio is not an allocating I/O.
1068 * If this is a retried I/O then we ignore it since we will
1069 * have already processed the original allocating I/O.
1071 if (flags & ZIO_FLAG_IO_ALLOCATING &&
1072 (vd != vd->vdev_top || (flags & ZIO_FLAG_IO_RETRY))) {
1073 metaslab_class_t *mc = spa_normal_class(pio->io_spa);
1075 ASSERT(mc->mc_alloc_throttle_enabled);
1076 ASSERT(type == ZIO_TYPE_WRITE);
1077 ASSERT(priority == ZIO_PRIORITY_ASYNC_WRITE);
1078 ASSERT(!(flags & ZIO_FLAG_IO_REPAIR));
1079 ASSERT(!(pio->io_flags & ZIO_FLAG_IO_REWRITE) ||
1080 pio->io_child_type == ZIO_CHILD_GANG);
1082 flags &= ~ZIO_FLAG_IO_ALLOCATING;
1085 zio = zio_create(pio, pio->io_spa, pio->io_txg, bp, data, size, size,
1086 done, private, type, priority, flags, vd, offset, &pio->io_bookmark,
1087 ZIO_STAGE_VDEV_IO_START >> 1, pipeline);
1088 ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_VDEV);
1090 zio->io_physdone = pio->io_physdone;
1091 if (vd->vdev_ops->vdev_op_leaf && zio->io_logical != NULL)
1092 zio->io_logical->io_phys_children++;
1094 return (zio);
1097 zio_t *
1098 zio_vdev_delegated_io(vdev_t *vd, uint64_t offset, abd_t *data, uint64_t size,
1099 int type, zio_priority_t priority, enum zio_flag flags,
1100 zio_done_func_t *done, void *private)
1102 zio_t *zio;
1104 ASSERT(vd->vdev_ops->vdev_op_leaf);
1106 zio = zio_create(NULL, vd->vdev_spa, 0, NULL,
1107 data, size, size, done, private, type, priority,
1108 flags | ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY | ZIO_FLAG_DELEGATED,
1109 vd, offset, NULL,
1110 ZIO_STAGE_VDEV_IO_START >> 1, ZIO_VDEV_CHILD_PIPELINE);
1112 return (zio);
1115 void
1116 zio_flush(zio_t *zio, vdev_t *vd)
1118 zio_nowait(zio_ioctl(zio, zio->io_spa, vd, DKIOCFLUSHWRITECACHE,
1119 NULL, NULL,
1120 ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY));
1123 void
1124 zio_shrink(zio_t *zio, uint64_t size)
1126 ASSERT3P(zio->io_executor, ==, NULL);
1127 ASSERT3P(zio->io_orig_size, ==, zio->io_size);
1128 ASSERT3U(size, <=, zio->io_size);
1131 * We don't shrink for raidz because of problems with the
1132 * reconstruction when reading back less than the block size.
1133 * Note, BP_IS_RAIDZ() assumes no compression.
1135 ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF);
1136 if (!BP_IS_RAIDZ(zio->io_bp)) {
1137 /* we are not doing a raw write */
1138 ASSERT3U(zio->io_size, ==, zio->io_lsize);
1139 zio->io_orig_size = zio->io_size = zio->io_lsize = size;
1144 * ==========================================================================
1145 * Prepare to read and write logical blocks
1146 * ==========================================================================
1149 static int
1150 zio_read_bp_init(zio_t *zio)
1152 blkptr_t *bp = zio->io_bp;
1154 ASSERT3P(zio->io_bp, ==, &zio->io_bp_copy);
1156 if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF &&
1157 zio->io_child_type == ZIO_CHILD_LOGICAL &&
1158 !(zio->io_flags & ZIO_FLAG_RAW)) {
1159 uint64_t psize =
1160 BP_IS_EMBEDDED(bp) ? BPE_GET_PSIZE(bp) : BP_GET_PSIZE(bp);
1161 zio_push_transform(zio, abd_alloc_sametype(zio->io_abd, psize),
1162 psize, psize, zio_decompress);
1165 if (BP_IS_EMBEDDED(bp) && BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA) {
1166 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1168 int psize = BPE_GET_PSIZE(bp);
1169 void *data = abd_borrow_buf(zio->io_abd, psize);
1170 decode_embedded_bp_compressed(bp, data);
1171 abd_return_buf_copy(zio->io_abd, data, psize);
1172 } else {
1173 ASSERT(!BP_IS_EMBEDDED(bp));
1174 ASSERT3P(zio->io_bp, ==, &zio->io_bp_copy);
1177 if (!DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) && BP_GET_LEVEL(bp) == 0)
1178 zio->io_flags |= ZIO_FLAG_DONT_CACHE;
1180 if (BP_GET_TYPE(bp) == DMU_OT_DDT_ZAP)
1181 zio->io_flags |= ZIO_FLAG_DONT_CACHE;
1183 if (BP_GET_DEDUP(bp) && zio->io_child_type == ZIO_CHILD_LOGICAL)
1184 zio->io_pipeline = ZIO_DDT_READ_PIPELINE;
1186 return (ZIO_PIPELINE_CONTINUE);
1189 static int
1190 zio_write_bp_init(zio_t *zio)
1192 if (!IO_IS_ALLOCATING(zio))
1193 return (ZIO_PIPELINE_CONTINUE);
1195 ASSERT(zio->io_child_type != ZIO_CHILD_DDT);
1197 if (zio->io_bp_override) {
1198 blkptr_t *bp = zio->io_bp;
1199 zio_prop_t *zp = &zio->io_prop;
1201 ASSERT(bp->blk_birth != zio->io_txg);
1202 ASSERT(BP_GET_DEDUP(zio->io_bp_override) == 0);
1204 *bp = *zio->io_bp_override;
1205 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1207 if (BP_IS_EMBEDDED(bp))
1208 return (ZIO_PIPELINE_CONTINUE);
1211 * If we've been overridden and nopwrite is set then
1212 * set the flag accordingly to indicate that a nopwrite
1213 * has already occurred.
1215 if (!BP_IS_HOLE(bp) && zp->zp_nopwrite) {
1216 ASSERT(!zp->zp_dedup);
1217 ASSERT3U(BP_GET_CHECKSUM(bp), ==, zp->zp_checksum);
1218 zio->io_flags |= ZIO_FLAG_NOPWRITE;
1219 return (ZIO_PIPELINE_CONTINUE);
1222 ASSERT(!zp->zp_nopwrite);
1224 if (BP_IS_HOLE(bp) || !zp->zp_dedup)
1225 return (ZIO_PIPELINE_CONTINUE);
1227 ASSERT((zio_checksum_table[zp->zp_checksum].ci_flags &
1228 ZCHECKSUM_FLAG_DEDUP) || zp->zp_dedup_verify);
1230 if (BP_GET_CHECKSUM(bp) == zp->zp_checksum) {
1231 BP_SET_DEDUP(bp, 1);
1232 zio->io_pipeline |= ZIO_STAGE_DDT_WRITE;
1233 return (ZIO_PIPELINE_CONTINUE);
1237 * We were unable to handle this as an override bp, treat
1238 * it as a regular write I/O.
1240 zio->io_bp_override = NULL;
1241 *bp = zio->io_bp_orig;
1242 zio->io_pipeline = zio->io_orig_pipeline;
1245 return (ZIO_PIPELINE_CONTINUE);
1248 static int
1249 zio_write_compress(zio_t *zio)
1251 spa_t *spa = zio->io_spa;
1252 zio_prop_t *zp = &zio->io_prop;
1253 enum zio_compress compress = zp->zp_compress;
1254 blkptr_t *bp = zio->io_bp;
1255 uint64_t lsize = zio->io_lsize;
1256 uint64_t psize = zio->io_size;
1257 int pass = 1;
1259 EQUIV(lsize != psize, (zio->io_flags & ZIO_FLAG_RAW) != 0);
1262 * If our children haven't all reached the ready stage,
1263 * wait for them and then repeat this pipeline stage.
1265 if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_READY) ||
1266 zio_wait_for_children(zio, ZIO_CHILD_LOGICAL, ZIO_WAIT_READY))
1267 return (ZIO_PIPELINE_STOP);
1269 if (!IO_IS_ALLOCATING(zio))
1270 return (ZIO_PIPELINE_CONTINUE);
1272 if (zio->io_children_ready != NULL) {
1274 * Now that all our children are ready, run the callback
1275 * associated with this zio in case it wants to modify the
1276 * data to be written.
1278 ASSERT3U(zp->zp_level, >, 0);
1279 zio->io_children_ready(zio);
1282 ASSERT(zio->io_child_type != ZIO_CHILD_DDT);
1283 ASSERT(zio->io_bp_override == NULL);
1285 if (!BP_IS_HOLE(bp) && bp->blk_birth == zio->io_txg) {
1287 * We're rewriting an existing block, which means we're
1288 * working on behalf of spa_sync(). For spa_sync() to
1289 * converge, it must eventually be the case that we don't
1290 * have to allocate new blocks. But compression changes
1291 * the blocksize, which forces a reallocate, and makes
1292 * convergence take longer. Therefore, after the first
1293 * few passes, stop compressing to ensure convergence.
1295 pass = spa_sync_pass(spa);
1297 ASSERT(zio->io_txg == spa_syncing_txg(spa));
1298 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1299 ASSERT(!BP_GET_DEDUP(bp));
1301 if (pass >= zfs_sync_pass_dont_compress)
1302 compress = ZIO_COMPRESS_OFF;
1304 /* Make sure someone doesn't change their mind on overwrites */
1305 ASSERT(BP_IS_EMBEDDED(bp) || MIN(zp->zp_copies + BP_IS_GANG(bp),
1306 spa_max_replication(spa)) == BP_GET_NDVAS(bp));
1309 /* If it's a compressed write that is not raw, compress the buffer. */
1310 if (compress != ZIO_COMPRESS_OFF && psize == lsize) {
1311 void *cbuf = zio_buf_alloc(lsize);
1312 psize = zio_compress_data(compress, zio->io_abd, cbuf, lsize);
1313 if (psize == 0 || psize == lsize) {
1314 compress = ZIO_COMPRESS_OFF;
1315 zio_buf_free(cbuf, lsize);
1316 } else if (!zp->zp_dedup && psize <= BPE_PAYLOAD_SIZE &&
1317 zp->zp_level == 0 && !DMU_OT_HAS_FILL(zp->zp_type) &&
1318 spa_feature_is_enabled(spa, SPA_FEATURE_EMBEDDED_DATA)) {
1319 encode_embedded_bp_compressed(bp,
1320 cbuf, compress, lsize, psize);
1321 BPE_SET_ETYPE(bp, BP_EMBEDDED_TYPE_DATA);
1322 BP_SET_TYPE(bp, zio->io_prop.zp_type);
1323 BP_SET_LEVEL(bp, zio->io_prop.zp_level);
1324 zio_buf_free(cbuf, lsize);
1325 bp->blk_birth = zio->io_txg;
1326 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1327 ASSERT(spa_feature_is_active(spa,
1328 SPA_FEATURE_EMBEDDED_DATA));
1329 return (ZIO_PIPELINE_CONTINUE);
1330 } else {
1332 * Round up compressed size up to the ashift
1333 * of the smallest-ashift device, and zero the tail.
1334 * This ensures that the compressed size of the BP
1335 * (and thus compressratio property) are correct,
1336 * in that we charge for the padding used to fill out
1337 * the last sector.
1339 ASSERT3U(spa->spa_min_ashift, >=, SPA_MINBLOCKSHIFT);
1340 size_t rounded = (size_t)P2ROUNDUP(psize,
1341 1ULL << spa->spa_min_ashift);
1342 if (rounded >= lsize) {
1343 compress = ZIO_COMPRESS_OFF;
1344 zio_buf_free(cbuf, lsize);
1345 psize = lsize;
1346 } else {
1347 abd_t *cdata = abd_get_from_buf(cbuf, lsize);
1348 abd_take_ownership_of_buf(cdata, B_TRUE);
1349 abd_zero_off(cdata, psize, rounded - psize);
1350 psize = rounded;
1351 zio_push_transform(zio, cdata,
1352 psize, lsize, NULL);
1357 * We were unable to handle this as an override bp, treat
1358 * it as a regular write I/O.
1360 zio->io_bp_override = NULL;
1361 *bp = zio->io_bp_orig;
1362 zio->io_pipeline = zio->io_orig_pipeline;
1363 } else {
1364 ASSERT3U(psize, !=, 0);
1368 * The final pass of spa_sync() must be all rewrites, but the first
1369 * few passes offer a trade-off: allocating blocks defers convergence,
1370 * but newly allocated blocks are sequential, so they can be written
1371 * to disk faster. Therefore, we allow the first few passes of
1372 * spa_sync() to allocate new blocks, but force rewrites after that.
1373 * There should only be a handful of blocks after pass 1 in any case.
1375 if (!BP_IS_HOLE(bp) && bp->blk_birth == zio->io_txg &&
1376 BP_GET_PSIZE(bp) == psize &&
1377 pass >= zfs_sync_pass_rewrite) {
1378 ASSERT(psize != 0);
1379 enum zio_stage gang_stages = zio->io_pipeline & ZIO_GANG_STAGES;
1380 zio->io_pipeline = ZIO_REWRITE_PIPELINE | gang_stages;
1381 zio->io_flags |= ZIO_FLAG_IO_REWRITE;
1382 } else {
1383 BP_ZERO(bp);
1384 zio->io_pipeline = ZIO_WRITE_PIPELINE;
1387 if (psize == 0) {
1388 if (zio->io_bp_orig.blk_birth != 0 &&
1389 spa_feature_is_active(spa, SPA_FEATURE_HOLE_BIRTH)) {
1390 BP_SET_LSIZE(bp, lsize);
1391 BP_SET_TYPE(bp, zp->zp_type);
1392 BP_SET_LEVEL(bp, zp->zp_level);
1393 BP_SET_BIRTH(bp, zio->io_txg, 0);
1395 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1396 } else {
1397 ASSERT(zp->zp_checksum != ZIO_CHECKSUM_GANG_HEADER);
1398 BP_SET_LSIZE(bp, lsize);
1399 BP_SET_TYPE(bp, zp->zp_type);
1400 BP_SET_LEVEL(bp, zp->zp_level);
1401 BP_SET_PSIZE(bp, psize);
1402 BP_SET_COMPRESS(bp, compress);
1403 BP_SET_CHECKSUM(bp, zp->zp_checksum);
1404 BP_SET_DEDUP(bp, zp->zp_dedup);
1405 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
1406 if (zp->zp_dedup) {
1407 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1408 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
1409 zio->io_pipeline = ZIO_DDT_WRITE_PIPELINE;
1411 if (zp->zp_nopwrite) {
1412 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1413 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
1414 zio->io_pipeline |= ZIO_STAGE_NOP_WRITE;
1417 return (ZIO_PIPELINE_CONTINUE);
1420 static int
1421 zio_free_bp_init(zio_t *zio)
1423 blkptr_t *bp = zio->io_bp;
1425 if (zio->io_child_type == ZIO_CHILD_LOGICAL) {
1426 if (BP_GET_DEDUP(bp))
1427 zio->io_pipeline = ZIO_DDT_FREE_PIPELINE;
1430 ASSERT3P(zio->io_bp, ==, &zio->io_bp_copy);
1432 return (ZIO_PIPELINE_CONTINUE);
1436 * ==========================================================================
1437 * Execute the I/O pipeline
1438 * ==========================================================================
1441 static void
1442 zio_taskq_dispatch(zio_t *zio, zio_taskq_type_t q, boolean_t cutinline)
1444 spa_t *spa = zio->io_spa;
1445 zio_type_t t = zio->io_type;
1446 int flags = (cutinline ? TQ_FRONT : 0);
1449 * If we're a config writer or a probe, the normal issue and
1450 * interrupt threads may all be blocked waiting for the config lock.
1451 * In this case, select the otherwise-unused taskq for ZIO_TYPE_NULL.
1453 if (zio->io_flags & (ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_PROBE))
1454 t = ZIO_TYPE_NULL;
1457 * A similar issue exists for the L2ARC write thread until L2ARC 2.0.
1459 if (t == ZIO_TYPE_WRITE && zio->io_vd && zio->io_vd->vdev_aux)
1460 t = ZIO_TYPE_NULL;
1463 * If this is a high priority I/O, then use the high priority taskq if
1464 * available.
1466 if (zio->io_priority == ZIO_PRIORITY_NOW &&
1467 spa->spa_zio_taskq[t][q + 1].stqs_count != 0)
1468 q++;
1470 ASSERT3U(q, <, ZIO_TASKQ_TYPES);
1473 * NB: We are assuming that the zio can only be dispatched
1474 * to a single taskq at a time. It would be a grievous error
1475 * to dispatch the zio to another taskq at the same time.
1477 ASSERT(zio->io_tqent.tqent_next == NULL);
1478 spa_taskq_dispatch_ent(spa, t, q, (task_func_t *)zio_execute, zio,
1479 flags, &zio->io_tqent);
1482 static boolean_t
1483 zio_taskq_member(zio_t *zio, zio_taskq_type_t q)
1485 kthread_t *executor = zio->io_executor;
1486 spa_t *spa = zio->io_spa;
1488 for (zio_type_t t = 0; t < ZIO_TYPES; t++) {
1489 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
1490 uint_t i;
1491 for (i = 0; i < tqs->stqs_count; i++) {
1492 if (taskq_member(tqs->stqs_taskq[i], executor))
1493 return (B_TRUE);
1497 return (B_FALSE);
1500 static int
1501 zio_issue_async(zio_t *zio)
1503 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE);
1505 return (ZIO_PIPELINE_STOP);
1508 void
1509 zio_interrupt(zio_t *zio)
1511 zio_taskq_dispatch(zio, ZIO_TASKQ_INTERRUPT, B_FALSE);
1514 void
1515 zio_delay_interrupt(zio_t *zio)
1518 * The timeout_generic() function isn't defined in userspace, so
1519 * rather than trying to implement the function, the zio delay
1520 * functionality has been disabled for userspace builds.
1523 #ifdef _KERNEL
1525 * If io_target_timestamp is zero, then no delay has been registered
1526 * for this IO, thus jump to the end of this function and "skip" the
1527 * delay; issuing it directly to the zio layer.
1529 if (zio->io_target_timestamp != 0) {
1530 hrtime_t now = gethrtime();
1532 if (now >= zio->io_target_timestamp) {
1534 * This IO has already taken longer than the target
1535 * delay to complete, so we don't want to delay it
1536 * any longer; we "miss" the delay and issue it
1537 * directly to the zio layer. This is likely due to
1538 * the target latency being set to a value less than
1539 * the underlying hardware can satisfy (e.g. delay
1540 * set to 1ms, but the disks take 10ms to complete an
1541 * IO request).
1544 DTRACE_PROBE2(zio__delay__miss, zio_t *, zio,
1545 hrtime_t, now);
1547 zio_interrupt(zio);
1548 } else {
1549 hrtime_t diff = zio->io_target_timestamp - now;
1551 DTRACE_PROBE3(zio__delay__hit, zio_t *, zio,
1552 hrtime_t, now, hrtime_t, diff);
1554 (void) timeout_generic(CALLOUT_NORMAL,
1555 (void (*)(void *))zio_interrupt, zio, diff, 1, 0);
1558 return;
1560 #endif
1562 DTRACE_PROBE1(zio__delay__skip, zio_t *, zio);
1563 zio_interrupt(zio);
1567 * Execute the I/O pipeline until one of the following occurs:
1569 * (1) the I/O completes
1570 * (2) the pipeline stalls waiting for dependent child I/Os
1571 * (3) the I/O issues, so we're waiting for an I/O completion interrupt
1572 * (4) the I/O is delegated by vdev-level caching or aggregation
1573 * (5) the I/O is deferred due to vdev-level queueing
1574 * (6) the I/O is handed off to another thread.
1576 * In all cases, the pipeline stops whenever there's no CPU work; it never
1577 * burns a thread in cv_wait().
1579 * There's no locking on io_stage because there's no legitimate way
1580 * for multiple threads to be attempting to process the same I/O.
1582 static zio_pipe_stage_t *zio_pipeline[];
1584 void
1585 zio_execute(zio_t *zio)
1587 zio->io_executor = curthread;
1589 ASSERT3U(zio->io_queued_timestamp, >, 0);
1591 while (zio->io_stage < ZIO_STAGE_DONE) {
1592 enum zio_stage pipeline = zio->io_pipeline;
1593 enum zio_stage stage = zio->io_stage;
1594 int rv;
1596 ASSERT(!MUTEX_HELD(&zio->io_lock));
1597 ASSERT(ISP2(stage));
1598 ASSERT(zio->io_stall == NULL);
1600 do {
1601 stage <<= 1;
1602 } while ((stage & pipeline) == 0);
1604 ASSERT(stage <= ZIO_STAGE_DONE);
1607 * If we are in interrupt context and this pipeline stage
1608 * will grab a config lock that is held across I/O,
1609 * or may wait for an I/O that needs an interrupt thread
1610 * to complete, issue async to avoid deadlock.
1612 * For VDEV_IO_START, we cut in line so that the io will
1613 * be sent to disk promptly.
1615 if ((stage & ZIO_BLOCKING_STAGES) && zio->io_vd == NULL &&
1616 zio_taskq_member(zio, ZIO_TASKQ_INTERRUPT)) {
1617 boolean_t cut = (stage == ZIO_STAGE_VDEV_IO_START) ?
1618 zio_requeue_io_start_cut_in_line : B_FALSE;
1619 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, cut);
1620 return;
1623 zio->io_stage = stage;
1624 zio->io_pipeline_trace |= zio->io_stage;
1625 rv = zio_pipeline[highbit64(stage) - 1](zio);
1627 if (rv == ZIO_PIPELINE_STOP)
1628 return;
1630 ASSERT(rv == ZIO_PIPELINE_CONTINUE);
1635 * ==========================================================================
1636 * Initiate I/O, either sync or async
1637 * ==========================================================================
1640 zio_wait(zio_t *zio)
1642 int error;
1644 ASSERT3P(zio->io_stage, ==, ZIO_STAGE_OPEN);
1645 ASSERT3P(zio->io_executor, ==, NULL);
1647 zio->io_waiter = curthread;
1648 ASSERT0(zio->io_queued_timestamp);
1649 zio->io_queued_timestamp = gethrtime();
1651 zio_execute(zio);
1653 mutex_enter(&zio->io_lock);
1654 while (zio->io_executor != NULL)
1655 cv_wait(&zio->io_cv, &zio->io_lock);
1656 mutex_exit(&zio->io_lock);
1658 error = zio->io_error;
1659 zio_destroy(zio);
1661 return (error);
1664 void
1665 zio_nowait(zio_t *zio)
1667 ASSERT3P(zio->io_executor, ==, NULL);
1669 if (zio->io_child_type == ZIO_CHILD_LOGICAL &&
1670 zio_unique_parent(zio) == NULL) {
1672 * This is a logical async I/O with no parent to wait for it.
1673 * We add it to the spa_async_root_zio "Godfather" I/O which
1674 * will ensure they complete prior to unloading the pool.
1676 spa_t *spa = zio->io_spa;
1678 zio_add_child(spa->spa_async_zio_root[CPU_SEQID], zio);
1681 ASSERT0(zio->io_queued_timestamp);
1682 zio->io_queued_timestamp = gethrtime();
1683 zio_execute(zio);
1687 * ==========================================================================
1688 * Reexecute, cancel, or suspend/resume failed I/O
1689 * ==========================================================================
1692 static void
1693 zio_reexecute(zio_t *pio)
1695 zio_t *cio, *cio_next;
1697 ASSERT(pio->io_child_type == ZIO_CHILD_LOGICAL);
1698 ASSERT(pio->io_orig_stage == ZIO_STAGE_OPEN);
1699 ASSERT(pio->io_gang_leader == NULL);
1700 ASSERT(pio->io_gang_tree == NULL);
1702 pio->io_flags = pio->io_orig_flags;
1703 pio->io_stage = pio->io_orig_stage;
1704 pio->io_pipeline = pio->io_orig_pipeline;
1705 pio->io_reexecute = 0;
1706 pio->io_flags |= ZIO_FLAG_REEXECUTED;
1707 pio->io_pipeline_trace = 0;
1708 pio->io_error = 0;
1709 for (int w = 0; w < ZIO_WAIT_TYPES; w++)
1710 pio->io_state[w] = 0;
1711 for (int c = 0; c < ZIO_CHILD_TYPES; c++)
1712 pio->io_child_error[c] = 0;
1714 if (IO_IS_ALLOCATING(pio))
1715 BP_ZERO(pio->io_bp);
1718 * As we reexecute pio's children, new children could be created.
1719 * New children go to the head of pio's io_child_list, however,
1720 * so we will (correctly) not reexecute them. The key is that
1721 * the remainder of pio's io_child_list, from 'cio_next' onward,
1722 * cannot be affected by any side effects of reexecuting 'cio'.
1724 zio_link_t *zl = NULL;
1725 for (cio = zio_walk_children(pio, &zl); cio != NULL; cio = cio_next) {
1726 cio_next = zio_walk_children(pio, &zl);
1727 mutex_enter(&pio->io_lock);
1728 for (int w = 0; w < ZIO_WAIT_TYPES; w++)
1729 pio->io_children[cio->io_child_type][w]++;
1730 mutex_exit(&pio->io_lock);
1731 zio_reexecute(cio);
1735 * Now that all children have been reexecuted, execute the parent.
1736 * We don't reexecute "The Godfather" I/O here as it's the
1737 * responsibility of the caller to wait on it.
1739 if (!(pio->io_flags & ZIO_FLAG_GODFATHER)) {
1740 pio->io_queued_timestamp = gethrtime();
1741 zio_execute(pio);
1745 void
1746 zio_suspend(spa_t *spa, zio_t *zio)
1748 if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_PANIC)
1749 fm_panic("Pool '%s' has encountered an uncorrectable I/O "
1750 "failure and the failure mode property for this pool "
1751 "is set to panic.", spa_name(spa));
1753 zfs_ereport_post(FM_EREPORT_ZFS_IO_FAILURE, spa, NULL, NULL, 0, 0);
1755 mutex_enter(&spa->spa_suspend_lock);
1757 if (spa->spa_suspend_zio_root == NULL)
1758 spa->spa_suspend_zio_root = zio_root(spa, NULL, NULL,
1759 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
1760 ZIO_FLAG_GODFATHER);
1762 spa->spa_suspended = B_TRUE;
1764 if (zio != NULL) {
1765 ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER));
1766 ASSERT(zio != spa->spa_suspend_zio_root);
1767 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1768 ASSERT(zio_unique_parent(zio) == NULL);
1769 ASSERT(zio->io_stage == ZIO_STAGE_DONE);
1770 zio_add_child(spa->spa_suspend_zio_root, zio);
1773 mutex_exit(&spa->spa_suspend_lock);
1777 zio_resume(spa_t *spa)
1779 zio_t *pio;
1782 * Reexecute all previously suspended i/o.
1784 mutex_enter(&spa->spa_suspend_lock);
1785 spa->spa_suspended = B_FALSE;
1786 cv_broadcast(&spa->spa_suspend_cv);
1787 pio = spa->spa_suspend_zio_root;
1788 spa->spa_suspend_zio_root = NULL;
1789 mutex_exit(&spa->spa_suspend_lock);
1791 if (pio == NULL)
1792 return (0);
1794 zio_reexecute(pio);
1795 return (zio_wait(pio));
1798 void
1799 zio_resume_wait(spa_t *spa)
1801 mutex_enter(&spa->spa_suspend_lock);
1802 while (spa_suspended(spa))
1803 cv_wait(&spa->spa_suspend_cv, &spa->spa_suspend_lock);
1804 mutex_exit(&spa->spa_suspend_lock);
1808 * ==========================================================================
1809 * Gang blocks.
1811 * A gang block is a collection of small blocks that looks to the DMU
1812 * like one large block. When zio_dva_allocate() cannot find a block
1813 * of the requested size, due to either severe fragmentation or the pool
1814 * being nearly full, it calls zio_write_gang_block() to construct the
1815 * block from smaller fragments.
1817 * A gang block consists of a gang header (zio_gbh_phys_t) and up to
1818 * three (SPA_GBH_NBLKPTRS) gang members. The gang header is just like
1819 * an indirect block: it's an array of block pointers. It consumes
1820 * only one sector and hence is allocatable regardless of fragmentation.
1821 * The gang header's bps point to its gang members, which hold the data.
1823 * Gang blocks are self-checksumming, using the bp's <vdev, offset, txg>
1824 * as the verifier to ensure uniqueness of the SHA256 checksum.
1825 * Critically, the gang block bp's blk_cksum is the checksum of the data,
1826 * not the gang header. This ensures that data block signatures (needed for
1827 * deduplication) are independent of how the block is physically stored.
1829 * Gang blocks can be nested: a gang member may itself be a gang block.
1830 * Thus every gang block is a tree in which root and all interior nodes are
1831 * gang headers, and the leaves are normal blocks that contain user data.
1832 * The root of the gang tree is called the gang leader.
1834 * To perform any operation (read, rewrite, free, claim) on a gang block,
1835 * zio_gang_assemble() first assembles the gang tree (minus data leaves)
1836 * in the io_gang_tree field of the original logical i/o by recursively
1837 * reading the gang leader and all gang headers below it. This yields
1838 * an in-core tree containing the contents of every gang header and the
1839 * bps for every constituent of the gang block.
1841 * With the gang tree now assembled, zio_gang_issue() just walks the gang tree
1842 * and invokes a callback on each bp. To free a gang block, zio_gang_issue()
1843 * calls zio_free_gang() -- a trivial wrapper around zio_free() -- for each bp.
1844 * zio_claim_gang() provides a similarly trivial wrapper for zio_claim().
1845 * zio_read_gang() is a wrapper around zio_read() that omits reading gang
1846 * headers, since we already have those in io_gang_tree. zio_rewrite_gang()
1847 * performs a zio_rewrite() of the data or, for gang headers, a zio_rewrite()
1848 * of the gang header plus zio_checksum_compute() of the data to update the
1849 * gang header's blk_cksum as described above.
1851 * The two-phase assemble/issue model solves the problem of partial failure --
1852 * what if you'd freed part of a gang block but then couldn't read the
1853 * gang header for another part? Assembling the entire gang tree first
1854 * ensures that all the necessary gang header I/O has succeeded before
1855 * starting the actual work of free, claim, or write. Once the gang tree
1856 * is assembled, free and claim are in-memory operations that cannot fail.
1858 * In the event that a gang write fails, zio_dva_unallocate() walks the
1859 * gang tree to immediately free (i.e. insert back into the space map)
1860 * everything we've allocated. This ensures that we don't get ENOSPC
1861 * errors during repeated suspend/resume cycles due to a flaky device.
1863 * Gang rewrites only happen during sync-to-convergence. If we can't assemble
1864 * the gang tree, we won't modify the block, so we can safely defer the free
1865 * (knowing that the block is still intact). If we *can* assemble the gang
1866 * tree, then even if some of the rewrites fail, zio_dva_unallocate() will free
1867 * each constituent bp and we can allocate a new block on the next sync pass.
1869 * In all cases, the gang tree allows complete recovery from partial failure.
1870 * ==========================================================================
1873 static void
1874 zio_gang_issue_func_done(zio_t *zio)
1876 abd_put(zio->io_abd);
1879 static zio_t *
1880 zio_read_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data,
1881 uint64_t offset)
1883 if (gn != NULL)
1884 return (pio);
1886 return (zio_read(pio, pio->io_spa, bp, abd_get_offset(data, offset),
1887 BP_GET_PSIZE(bp), zio_gang_issue_func_done,
1888 NULL, pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio),
1889 &pio->io_bookmark));
1892 static zio_t *
1893 zio_rewrite_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data,
1894 uint64_t offset)
1896 zio_t *zio;
1898 if (gn != NULL) {
1899 abd_t *gbh_abd =
1900 abd_get_from_buf(gn->gn_gbh, SPA_GANGBLOCKSIZE);
1901 zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp,
1902 gbh_abd, SPA_GANGBLOCKSIZE, zio_gang_issue_func_done, NULL,
1903 pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio),
1904 &pio->io_bookmark);
1906 * As we rewrite each gang header, the pipeline will compute
1907 * a new gang block header checksum for it; but no one will
1908 * compute a new data checksum, so we do that here. The one
1909 * exception is the gang leader: the pipeline already computed
1910 * its data checksum because that stage precedes gang assembly.
1911 * (Presently, nothing actually uses interior data checksums;
1912 * this is just good hygiene.)
1914 if (gn != pio->io_gang_leader->io_gang_tree) {
1915 abd_t *buf = abd_get_offset(data, offset);
1917 zio_checksum_compute(zio, BP_GET_CHECKSUM(bp),
1918 buf, BP_GET_PSIZE(bp));
1920 abd_put(buf);
1923 * If we are here to damage data for testing purposes,
1924 * leave the GBH alone so that we can detect the damage.
1926 if (pio->io_gang_leader->io_flags & ZIO_FLAG_INDUCE_DAMAGE)
1927 zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES;
1928 } else {
1929 zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp,
1930 abd_get_offset(data, offset), BP_GET_PSIZE(bp),
1931 zio_gang_issue_func_done, NULL, pio->io_priority,
1932 ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
1935 return (zio);
1938 /* ARGSUSED */
1939 static zio_t *
1940 zio_free_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data,
1941 uint64_t offset)
1943 return (zio_free_sync(pio, pio->io_spa, pio->io_txg, bp,
1944 ZIO_GANG_CHILD_FLAGS(pio)));
1947 /* ARGSUSED */
1948 static zio_t *
1949 zio_claim_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data,
1950 uint64_t offset)
1952 return (zio_claim(pio, pio->io_spa, pio->io_txg, bp,
1953 NULL, NULL, ZIO_GANG_CHILD_FLAGS(pio)));
1956 static zio_gang_issue_func_t *zio_gang_issue_func[ZIO_TYPES] = {
1957 NULL,
1958 zio_read_gang,
1959 zio_rewrite_gang,
1960 zio_free_gang,
1961 zio_claim_gang,
1962 NULL
1965 static void zio_gang_tree_assemble_done(zio_t *zio);
1967 static zio_gang_node_t *
1968 zio_gang_node_alloc(zio_gang_node_t **gnpp)
1970 zio_gang_node_t *gn;
1972 ASSERT(*gnpp == NULL);
1974 gn = kmem_zalloc(sizeof (*gn), KM_SLEEP);
1975 gn->gn_gbh = zio_buf_alloc(SPA_GANGBLOCKSIZE);
1976 *gnpp = gn;
1978 return (gn);
1981 static void
1982 zio_gang_node_free(zio_gang_node_t **gnpp)
1984 zio_gang_node_t *gn = *gnpp;
1986 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++)
1987 ASSERT(gn->gn_child[g] == NULL);
1989 zio_buf_free(gn->gn_gbh, SPA_GANGBLOCKSIZE);
1990 kmem_free(gn, sizeof (*gn));
1991 *gnpp = NULL;
1994 static void
1995 zio_gang_tree_free(zio_gang_node_t **gnpp)
1997 zio_gang_node_t *gn = *gnpp;
1999 if (gn == NULL)
2000 return;
2002 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++)
2003 zio_gang_tree_free(&gn->gn_child[g]);
2005 zio_gang_node_free(gnpp);
2008 static void
2009 zio_gang_tree_assemble(zio_t *gio, blkptr_t *bp, zio_gang_node_t **gnpp)
2011 zio_gang_node_t *gn = zio_gang_node_alloc(gnpp);
2012 abd_t *gbh_abd = abd_get_from_buf(gn->gn_gbh, SPA_GANGBLOCKSIZE);
2014 ASSERT(gio->io_gang_leader == gio);
2015 ASSERT(BP_IS_GANG(bp));
2017 zio_nowait(zio_read(gio, gio->io_spa, bp, gbh_abd, SPA_GANGBLOCKSIZE,
2018 zio_gang_tree_assemble_done, gn, gio->io_priority,
2019 ZIO_GANG_CHILD_FLAGS(gio), &gio->io_bookmark));
2022 static void
2023 zio_gang_tree_assemble_done(zio_t *zio)
2025 zio_t *gio = zio->io_gang_leader;
2026 zio_gang_node_t *gn = zio->io_private;
2027 blkptr_t *bp = zio->io_bp;
2029 ASSERT(gio == zio_unique_parent(zio));
2030 ASSERT(zio->io_child_count == 0);
2032 if (zio->io_error)
2033 return;
2035 /* this ABD was created from a linear buf in zio_gang_tree_assemble */
2036 if (BP_SHOULD_BYTESWAP(bp))
2037 byteswap_uint64_array(abd_to_buf(zio->io_abd), zio->io_size);
2039 ASSERT3P(abd_to_buf(zio->io_abd), ==, gn->gn_gbh);
2040 ASSERT(zio->io_size == SPA_GANGBLOCKSIZE);
2041 ASSERT(gn->gn_gbh->zg_tail.zec_magic == ZEC_MAGIC);
2043 abd_put(zio->io_abd);
2045 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
2046 blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g];
2047 if (!BP_IS_GANG(gbp))
2048 continue;
2049 zio_gang_tree_assemble(gio, gbp, &gn->gn_child[g]);
2053 static void
2054 zio_gang_tree_issue(zio_t *pio, zio_gang_node_t *gn, blkptr_t *bp, abd_t *data,
2055 uint64_t offset)
2057 zio_t *gio = pio->io_gang_leader;
2058 zio_t *zio;
2060 ASSERT(BP_IS_GANG(bp) == !!gn);
2061 ASSERT(BP_GET_CHECKSUM(bp) == BP_GET_CHECKSUM(gio->io_bp));
2062 ASSERT(BP_GET_LSIZE(bp) == BP_GET_PSIZE(bp) || gn == gio->io_gang_tree);
2065 * If you're a gang header, your data is in gn->gn_gbh.
2066 * If you're a gang member, your data is in 'data' and gn == NULL.
2068 zio = zio_gang_issue_func[gio->io_type](pio, bp, gn, data, offset);
2070 if (gn != NULL) {
2071 ASSERT(gn->gn_gbh->zg_tail.zec_magic == ZEC_MAGIC);
2073 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
2074 blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g];
2075 if (BP_IS_HOLE(gbp))
2076 continue;
2077 zio_gang_tree_issue(zio, gn->gn_child[g], gbp, data,
2078 offset);
2079 offset += BP_GET_PSIZE(gbp);
2083 if (gn == gio->io_gang_tree)
2084 ASSERT3U(gio->io_size, ==, offset);
2086 if (zio != pio)
2087 zio_nowait(zio);
2090 static int
2091 zio_gang_assemble(zio_t *zio)
2093 blkptr_t *bp = zio->io_bp;
2095 ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == NULL);
2096 ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
2098 zio->io_gang_leader = zio;
2100 zio_gang_tree_assemble(zio, bp, &zio->io_gang_tree);
2102 return (ZIO_PIPELINE_CONTINUE);
2105 static int
2106 zio_gang_issue(zio_t *zio)
2108 blkptr_t *bp = zio->io_bp;
2110 if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_DONE))
2111 return (ZIO_PIPELINE_STOP);
2113 ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == zio);
2114 ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
2116 if (zio->io_child_error[ZIO_CHILD_GANG] == 0)
2117 zio_gang_tree_issue(zio, zio->io_gang_tree, bp, zio->io_abd,
2119 else
2120 zio_gang_tree_free(&zio->io_gang_tree);
2122 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2124 return (ZIO_PIPELINE_CONTINUE);
2127 static void
2128 zio_write_gang_member_ready(zio_t *zio)
2130 zio_t *pio = zio_unique_parent(zio);
2131 zio_t *gio = zio->io_gang_leader;
2132 dva_t *cdva = zio->io_bp->blk_dva;
2133 dva_t *pdva = pio->io_bp->blk_dva;
2134 uint64_t asize;
2136 if (BP_IS_HOLE(zio->io_bp))
2137 return;
2139 ASSERT(BP_IS_HOLE(&zio->io_bp_orig));
2141 ASSERT(zio->io_child_type == ZIO_CHILD_GANG);
2142 ASSERT3U(zio->io_prop.zp_copies, ==, gio->io_prop.zp_copies);
2143 ASSERT3U(zio->io_prop.zp_copies, <=, BP_GET_NDVAS(zio->io_bp));
2144 ASSERT3U(pio->io_prop.zp_copies, <=, BP_GET_NDVAS(pio->io_bp));
2145 ASSERT3U(BP_GET_NDVAS(zio->io_bp), <=, BP_GET_NDVAS(pio->io_bp));
2147 mutex_enter(&pio->io_lock);
2148 for (int d = 0; d < BP_GET_NDVAS(zio->io_bp); d++) {
2149 ASSERT(DVA_GET_GANG(&pdva[d]));
2150 asize = DVA_GET_ASIZE(&pdva[d]);
2151 asize += DVA_GET_ASIZE(&cdva[d]);
2152 DVA_SET_ASIZE(&pdva[d], asize);
2154 mutex_exit(&pio->io_lock);
2157 static void
2158 zio_write_gang_done(zio_t *zio)
2160 abd_put(zio->io_abd);
2163 static int
2164 zio_write_gang_block(zio_t *pio)
2166 spa_t *spa = pio->io_spa;
2167 metaslab_class_t *mc = spa_normal_class(spa);
2168 blkptr_t *bp = pio->io_bp;
2169 zio_t *gio = pio->io_gang_leader;
2170 zio_t *zio;
2171 zio_gang_node_t *gn, **gnpp;
2172 zio_gbh_phys_t *gbh;
2173 abd_t *gbh_abd;
2174 uint64_t txg = pio->io_txg;
2175 uint64_t resid = pio->io_size;
2176 uint64_t lsize;
2177 int copies = gio->io_prop.zp_copies;
2178 int gbh_copies = MIN(copies + 1, spa_max_replication(spa));
2179 zio_prop_t zp;
2180 int error;
2182 int flags = METASLAB_HINTBP_FAVOR | METASLAB_GANG_HEADER;
2183 if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
2184 ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
2185 ASSERT(!(pio->io_flags & ZIO_FLAG_NODATA));
2187 flags |= METASLAB_ASYNC_ALLOC;
2188 VERIFY(refcount_held(&mc->mc_alloc_slots, pio));
2191 * The logical zio has already placed a reservation for
2192 * 'copies' allocation slots but gang blocks may require
2193 * additional copies. These additional copies
2194 * (i.e. gbh_copies - copies) are guaranteed to succeed
2195 * since metaslab_class_throttle_reserve() always allows
2196 * additional reservations for gang blocks.
2198 VERIFY(metaslab_class_throttle_reserve(mc, gbh_copies - copies,
2199 pio, flags));
2202 error = metaslab_alloc(spa, mc, SPA_GANGBLOCKSIZE,
2203 bp, gbh_copies, txg, pio == gio ? NULL : gio->io_bp, flags,
2204 &pio->io_alloc_list, pio);
2205 if (error) {
2206 if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
2207 ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
2208 ASSERT(!(pio->io_flags & ZIO_FLAG_NODATA));
2211 * If we failed to allocate the gang block header then
2212 * we remove any additional allocation reservations that
2213 * we placed here. The original reservation will
2214 * be removed when the logical I/O goes to the ready
2215 * stage.
2217 metaslab_class_throttle_unreserve(mc,
2218 gbh_copies - copies, pio);
2220 pio->io_error = error;
2221 return (ZIO_PIPELINE_CONTINUE);
2224 if (pio == gio) {
2225 gnpp = &gio->io_gang_tree;
2226 } else {
2227 gnpp = pio->io_private;
2228 ASSERT(pio->io_ready == zio_write_gang_member_ready);
2231 gn = zio_gang_node_alloc(gnpp);
2232 gbh = gn->gn_gbh;
2233 bzero(gbh, SPA_GANGBLOCKSIZE);
2234 gbh_abd = abd_get_from_buf(gbh, SPA_GANGBLOCKSIZE);
2237 * Create the gang header.
2239 zio = zio_rewrite(pio, spa, txg, bp, gbh_abd, SPA_GANGBLOCKSIZE,
2240 zio_write_gang_done, NULL, pio->io_priority,
2241 ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
2244 * Create and nowait the gang children.
2246 for (int g = 0; resid != 0; resid -= lsize, g++) {
2247 lsize = P2ROUNDUP(resid / (SPA_GBH_NBLKPTRS - g),
2248 SPA_MINBLOCKSIZE);
2249 ASSERT(lsize >= SPA_MINBLOCKSIZE && lsize <= resid);
2251 zp.zp_checksum = gio->io_prop.zp_checksum;
2252 zp.zp_compress = ZIO_COMPRESS_OFF;
2253 zp.zp_type = DMU_OT_NONE;
2254 zp.zp_level = 0;
2255 zp.zp_copies = gio->io_prop.zp_copies;
2256 zp.zp_dedup = B_FALSE;
2257 zp.zp_dedup_verify = B_FALSE;
2258 zp.zp_nopwrite = B_FALSE;
2260 zio_t *cio = zio_write(zio, spa, txg, &gbh->zg_blkptr[g],
2261 abd_get_offset(pio->io_abd, pio->io_size - resid), lsize,
2262 lsize, &zp, zio_write_gang_member_ready, NULL, NULL,
2263 zio_write_gang_done, &gn->gn_child[g], pio->io_priority,
2264 ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
2266 if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
2267 ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
2268 ASSERT(!(pio->io_flags & ZIO_FLAG_NODATA));
2271 * Gang children won't throttle but we should
2272 * account for their work, so reserve an allocation
2273 * slot for them here.
2275 VERIFY(metaslab_class_throttle_reserve(mc,
2276 zp.zp_copies, cio, flags));
2278 zio_nowait(cio);
2282 * Set pio's pipeline to just wait for zio to finish.
2284 pio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2286 zio_nowait(zio);
2288 return (ZIO_PIPELINE_CONTINUE);
2292 * The zio_nop_write stage in the pipeline determines if allocating a
2293 * new bp is necessary. The nopwrite feature can handle writes in
2294 * either syncing or open context (i.e. zil writes) and as a result is
2295 * mutually exclusive with dedup.
2297 * By leveraging a cryptographically secure checksum, such as SHA256, we
2298 * can compare the checksums of the new data and the old to determine if
2299 * allocating a new block is required. Note that our requirements for
2300 * cryptographic strength are fairly weak: there can't be any accidental
2301 * hash collisions, but we don't need to be secure against intentional
2302 * (malicious) collisions. To trigger a nopwrite, you have to be able
2303 * to write the file to begin with, and triggering an incorrect (hash
2304 * collision) nopwrite is no worse than simply writing to the file.
2305 * That said, there are no known attacks against the checksum algorithms
2306 * used for nopwrite, assuming that the salt and the checksums
2307 * themselves remain secret.
2309 static int
2310 zio_nop_write(zio_t *zio)
2312 blkptr_t *bp = zio->io_bp;
2313 blkptr_t *bp_orig = &zio->io_bp_orig;
2314 zio_prop_t *zp = &zio->io_prop;
2316 ASSERT(BP_GET_LEVEL(bp) == 0);
2317 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
2318 ASSERT(zp->zp_nopwrite);
2319 ASSERT(!zp->zp_dedup);
2320 ASSERT(zio->io_bp_override == NULL);
2321 ASSERT(IO_IS_ALLOCATING(zio));
2324 * Check to see if the original bp and the new bp have matching
2325 * characteristics (i.e. same checksum, compression algorithms, etc).
2326 * If they don't then just continue with the pipeline which will
2327 * allocate a new bp.
2329 if (BP_IS_HOLE(bp_orig) ||
2330 !(zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_flags &
2331 ZCHECKSUM_FLAG_NOPWRITE) ||
2332 BP_GET_CHECKSUM(bp) != BP_GET_CHECKSUM(bp_orig) ||
2333 BP_GET_COMPRESS(bp) != BP_GET_COMPRESS(bp_orig) ||
2334 BP_GET_DEDUP(bp) != BP_GET_DEDUP(bp_orig) ||
2335 zp->zp_copies != BP_GET_NDVAS(bp_orig))
2336 return (ZIO_PIPELINE_CONTINUE);
2339 * If the checksums match then reset the pipeline so that we
2340 * avoid allocating a new bp and issuing any I/O.
2342 if (ZIO_CHECKSUM_EQUAL(bp->blk_cksum, bp_orig->blk_cksum)) {
2343 ASSERT(zio_checksum_table[zp->zp_checksum].ci_flags &
2344 ZCHECKSUM_FLAG_NOPWRITE);
2345 ASSERT3U(BP_GET_PSIZE(bp), ==, BP_GET_PSIZE(bp_orig));
2346 ASSERT3U(BP_GET_LSIZE(bp), ==, BP_GET_LSIZE(bp_orig));
2347 ASSERT(zp->zp_compress != ZIO_COMPRESS_OFF);
2348 ASSERT(bcmp(&bp->blk_prop, &bp_orig->blk_prop,
2349 sizeof (uint64_t)) == 0);
2351 *bp = *bp_orig;
2352 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2353 zio->io_flags |= ZIO_FLAG_NOPWRITE;
2356 return (ZIO_PIPELINE_CONTINUE);
2360 * ==========================================================================
2361 * Dedup
2362 * ==========================================================================
2364 static void
2365 zio_ddt_child_read_done(zio_t *zio)
2367 blkptr_t *bp = zio->io_bp;
2368 ddt_entry_t *dde = zio->io_private;
2369 ddt_phys_t *ddp;
2370 zio_t *pio = zio_unique_parent(zio);
2372 mutex_enter(&pio->io_lock);
2373 ddp = ddt_phys_select(dde, bp);
2374 if (zio->io_error == 0)
2375 ddt_phys_clear(ddp); /* this ddp doesn't need repair */
2377 if (zio->io_error == 0 && dde->dde_repair_abd == NULL)
2378 dde->dde_repair_abd = zio->io_abd;
2379 else
2380 abd_free(zio->io_abd);
2381 mutex_exit(&pio->io_lock);
2384 static int
2385 zio_ddt_read_start(zio_t *zio)
2387 blkptr_t *bp = zio->io_bp;
2389 ASSERT(BP_GET_DEDUP(bp));
2390 ASSERT(BP_GET_PSIZE(bp) == zio->io_size);
2391 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2393 if (zio->io_child_error[ZIO_CHILD_DDT]) {
2394 ddt_t *ddt = ddt_select(zio->io_spa, bp);
2395 ddt_entry_t *dde = ddt_repair_start(ddt, bp);
2396 ddt_phys_t *ddp = dde->dde_phys;
2397 ddt_phys_t *ddp_self = ddt_phys_select(dde, bp);
2398 blkptr_t blk;
2400 ASSERT(zio->io_vsd == NULL);
2401 zio->io_vsd = dde;
2403 if (ddp_self == NULL)
2404 return (ZIO_PIPELINE_CONTINUE);
2406 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
2407 if (ddp->ddp_phys_birth == 0 || ddp == ddp_self)
2408 continue;
2409 ddt_bp_create(ddt->ddt_checksum, &dde->dde_key, ddp,
2410 &blk);
2411 zio_nowait(zio_read(zio, zio->io_spa, &blk,
2412 abd_alloc_for_io(zio->io_size, B_TRUE),
2413 zio->io_size, zio_ddt_child_read_done, dde,
2414 zio->io_priority, ZIO_DDT_CHILD_FLAGS(zio) |
2415 ZIO_FLAG_DONT_PROPAGATE, &zio->io_bookmark));
2417 return (ZIO_PIPELINE_CONTINUE);
2420 zio_nowait(zio_read(zio, zio->io_spa, bp,
2421 zio->io_abd, zio->io_size, NULL, NULL, zio->io_priority,
2422 ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark));
2424 return (ZIO_PIPELINE_CONTINUE);
2427 static int
2428 zio_ddt_read_done(zio_t *zio)
2430 blkptr_t *bp = zio->io_bp;
2432 if (zio_wait_for_children(zio, ZIO_CHILD_DDT, ZIO_WAIT_DONE))
2433 return (ZIO_PIPELINE_STOP);
2435 ASSERT(BP_GET_DEDUP(bp));
2436 ASSERT(BP_GET_PSIZE(bp) == zio->io_size);
2437 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2439 if (zio->io_child_error[ZIO_CHILD_DDT]) {
2440 ddt_t *ddt = ddt_select(zio->io_spa, bp);
2441 ddt_entry_t *dde = zio->io_vsd;
2442 if (ddt == NULL) {
2443 ASSERT(spa_load_state(zio->io_spa) != SPA_LOAD_NONE);
2444 return (ZIO_PIPELINE_CONTINUE);
2446 if (dde == NULL) {
2447 zio->io_stage = ZIO_STAGE_DDT_READ_START >> 1;
2448 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE);
2449 return (ZIO_PIPELINE_STOP);
2451 if (dde->dde_repair_abd != NULL) {
2452 abd_copy(zio->io_abd, dde->dde_repair_abd,
2453 zio->io_size);
2454 zio->io_child_error[ZIO_CHILD_DDT] = 0;
2456 ddt_repair_done(ddt, dde);
2457 zio->io_vsd = NULL;
2460 ASSERT(zio->io_vsd == NULL);
2462 return (ZIO_PIPELINE_CONTINUE);
2465 static boolean_t
2466 zio_ddt_collision(zio_t *zio, ddt_t *ddt, ddt_entry_t *dde)
2468 spa_t *spa = zio->io_spa;
2469 boolean_t do_raw = (zio->io_flags & ZIO_FLAG_RAW);
2471 /* We should never get a raw, override zio */
2472 ASSERT(!(zio->io_bp_override && do_raw));
2475 * Note: we compare the original data, not the transformed data,
2476 * because when zio->io_bp is an override bp, we will not have
2477 * pushed the I/O transforms. That's an important optimization
2478 * because otherwise we'd compress/encrypt all dmu_sync() data twice.
2480 for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) {
2481 zio_t *lio = dde->dde_lead_zio[p];
2483 if (lio != NULL) {
2484 return (lio->io_orig_size != zio->io_orig_size ||
2485 abd_cmp(zio->io_orig_abd, lio->io_orig_abd,
2486 zio->io_orig_size) != 0);
2490 for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) {
2491 ddt_phys_t *ddp = &dde->dde_phys[p];
2493 if (ddp->ddp_phys_birth != 0) {
2494 arc_buf_t *abuf = NULL;
2495 arc_flags_t aflags = ARC_FLAG_WAIT;
2496 int zio_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE;
2497 blkptr_t blk = *zio->io_bp;
2498 int error;
2500 ddt_bp_fill(ddp, &blk, ddp->ddp_phys_birth);
2502 ddt_exit(ddt);
2505 * Intuitively, it would make more sense to compare
2506 * io_abd than io_orig_abd in the raw case since you
2507 * don't want to look at any transformations that have
2508 * happened to the data. However, for raw I/Os the
2509 * data will actually be the same in io_abd and
2510 * io_orig_abd, so all we have to do is issue this as
2511 * a raw ARC read.
2513 if (do_raw) {
2514 zio_flags |= ZIO_FLAG_RAW;
2515 ASSERT3U(zio->io_size, ==, zio->io_orig_size);
2516 ASSERT0(abd_cmp(zio->io_abd, zio->io_orig_abd,
2517 zio->io_size));
2518 ASSERT3P(zio->io_transform_stack, ==, NULL);
2521 error = arc_read(NULL, spa, &blk,
2522 arc_getbuf_func, &abuf, ZIO_PRIORITY_SYNC_READ,
2523 zio_flags, &aflags, &zio->io_bookmark);
2525 if (error == 0) {
2526 if (arc_buf_size(abuf) != zio->io_orig_size ||
2527 abd_cmp_buf(zio->io_orig_abd, abuf->b_data,
2528 zio->io_orig_size) != 0)
2529 error = SET_ERROR(EEXIST);
2530 arc_buf_destroy(abuf, &abuf);
2533 ddt_enter(ddt);
2534 return (error != 0);
2538 return (B_FALSE);
2541 static void
2542 zio_ddt_child_write_ready(zio_t *zio)
2544 int p = zio->io_prop.zp_copies;
2545 ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp);
2546 ddt_entry_t *dde = zio->io_private;
2547 ddt_phys_t *ddp = &dde->dde_phys[p];
2548 zio_t *pio;
2550 if (zio->io_error)
2551 return;
2553 ddt_enter(ddt);
2555 ASSERT(dde->dde_lead_zio[p] == zio);
2557 ddt_phys_fill(ddp, zio->io_bp);
2559 zio_link_t *zl = NULL;
2560 while ((pio = zio_walk_parents(zio, &zl)) != NULL)
2561 ddt_bp_fill(ddp, pio->io_bp, zio->io_txg);
2563 ddt_exit(ddt);
2566 static void
2567 zio_ddt_child_write_done(zio_t *zio)
2569 int p = zio->io_prop.zp_copies;
2570 ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp);
2571 ddt_entry_t *dde = zio->io_private;
2572 ddt_phys_t *ddp = &dde->dde_phys[p];
2574 ddt_enter(ddt);
2576 ASSERT(ddp->ddp_refcnt == 0);
2577 ASSERT(dde->dde_lead_zio[p] == zio);
2578 dde->dde_lead_zio[p] = NULL;
2580 if (zio->io_error == 0) {
2581 zio_link_t *zl = NULL;
2582 while (zio_walk_parents(zio, &zl) != NULL)
2583 ddt_phys_addref(ddp);
2584 } else {
2585 ddt_phys_clear(ddp);
2588 ddt_exit(ddt);
2591 static void
2592 zio_ddt_ditto_write_done(zio_t *zio)
2594 int p = DDT_PHYS_DITTO;
2595 zio_prop_t *zp = &zio->io_prop;
2596 blkptr_t *bp = zio->io_bp;
2597 ddt_t *ddt = ddt_select(zio->io_spa, bp);
2598 ddt_entry_t *dde = zio->io_private;
2599 ddt_phys_t *ddp = &dde->dde_phys[p];
2600 ddt_key_t *ddk = &dde->dde_key;
2602 ddt_enter(ddt);
2604 ASSERT(ddp->ddp_refcnt == 0);
2605 ASSERT(dde->dde_lead_zio[p] == zio);
2606 dde->dde_lead_zio[p] = NULL;
2608 if (zio->io_error == 0) {
2609 ASSERT(ZIO_CHECKSUM_EQUAL(bp->blk_cksum, ddk->ddk_cksum));
2610 ASSERT(zp->zp_copies < SPA_DVAS_PER_BP);
2611 ASSERT(zp->zp_copies == BP_GET_NDVAS(bp) - BP_IS_GANG(bp));
2612 if (ddp->ddp_phys_birth != 0)
2613 ddt_phys_free(ddt, ddk, ddp, zio->io_txg);
2614 ddt_phys_fill(ddp, bp);
2617 ddt_exit(ddt);
2620 static int
2621 zio_ddt_write(zio_t *zio)
2623 spa_t *spa = zio->io_spa;
2624 blkptr_t *bp = zio->io_bp;
2625 uint64_t txg = zio->io_txg;
2626 zio_prop_t *zp = &zio->io_prop;
2627 int p = zp->zp_copies;
2628 int ditto_copies;
2629 zio_t *cio = NULL;
2630 zio_t *dio = NULL;
2631 ddt_t *ddt = ddt_select(spa, bp);
2632 ddt_entry_t *dde;
2633 ddt_phys_t *ddp;
2635 ASSERT(BP_GET_DEDUP(bp));
2636 ASSERT(BP_GET_CHECKSUM(bp) == zp->zp_checksum);
2637 ASSERT(BP_IS_HOLE(bp) || zio->io_bp_override);
2638 ASSERT(!(zio->io_bp_override && (zio->io_flags & ZIO_FLAG_RAW)));
2640 ddt_enter(ddt);
2641 dde = ddt_lookup(ddt, bp, B_TRUE);
2642 ddp = &dde->dde_phys[p];
2644 if (zp->zp_dedup_verify && zio_ddt_collision(zio, ddt, dde)) {
2646 * If we're using a weak checksum, upgrade to a strong checksum
2647 * and try again. If we're already using a strong checksum,
2648 * we can't resolve it, so just convert to an ordinary write.
2649 * (And automatically e-mail a paper to Nature?)
2651 if (!(zio_checksum_table[zp->zp_checksum].ci_flags &
2652 ZCHECKSUM_FLAG_DEDUP)) {
2653 zp->zp_checksum = spa_dedup_checksum(spa);
2654 zio_pop_transforms(zio);
2655 zio->io_stage = ZIO_STAGE_OPEN;
2656 BP_ZERO(bp);
2657 } else {
2658 zp->zp_dedup = B_FALSE;
2659 BP_SET_DEDUP(bp, B_FALSE);
2661 ASSERT(!BP_GET_DEDUP(bp));
2662 zio->io_pipeline = ZIO_WRITE_PIPELINE;
2663 ddt_exit(ddt);
2664 return (ZIO_PIPELINE_CONTINUE);
2667 ditto_copies = ddt_ditto_copies_needed(ddt, dde, ddp);
2668 ASSERT(ditto_copies < SPA_DVAS_PER_BP);
2670 if (ditto_copies > ddt_ditto_copies_present(dde) &&
2671 dde->dde_lead_zio[DDT_PHYS_DITTO] == NULL) {
2672 zio_prop_t czp = *zp;
2674 czp.zp_copies = ditto_copies;
2677 * If we arrived here with an override bp, we won't have run
2678 * the transform stack, so we won't have the data we need to
2679 * generate a child i/o. So, toss the override bp and restart.
2680 * This is safe, because using the override bp is just an
2681 * optimization; and it's rare, so the cost doesn't matter.
2683 if (zio->io_bp_override) {
2684 zio_pop_transforms(zio);
2685 zio->io_stage = ZIO_STAGE_OPEN;
2686 zio->io_pipeline = ZIO_WRITE_PIPELINE;
2687 zio->io_bp_override = NULL;
2688 BP_ZERO(bp);
2689 ddt_exit(ddt);
2690 return (ZIO_PIPELINE_CONTINUE);
2693 dio = zio_write(zio, spa, txg, bp, zio->io_orig_abd,
2694 zio->io_orig_size, zio->io_orig_size, &czp, NULL, NULL,
2695 NULL, zio_ddt_ditto_write_done, dde, zio->io_priority,
2696 ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark);
2698 zio_push_transform(dio, zio->io_abd, zio->io_size, 0, NULL);
2699 dde->dde_lead_zio[DDT_PHYS_DITTO] = dio;
2702 if (ddp->ddp_phys_birth != 0 || dde->dde_lead_zio[p] != NULL) {
2703 if (ddp->ddp_phys_birth != 0)
2704 ddt_bp_fill(ddp, bp, txg);
2705 if (dde->dde_lead_zio[p] != NULL)
2706 zio_add_child(zio, dde->dde_lead_zio[p]);
2707 else
2708 ddt_phys_addref(ddp);
2709 } else if (zio->io_bp_override) {
2710 ASSERT(bp->blk_birth == txg);
2711 ASSERT(BP_EQUAL(bp, zio->io_bp_override));
2712 ddt_phys_fill(ddp, bp);
2713 ddt_phys_addref(ddp);
2714 } else {
2715 cio = zio_write(zio, spa, txg, bp, zio->io_orig_abd,
2716 zio->io_orig_size, zio->io_orig_size, zp,
2717 zio_ddt_child_write_ready, NULL, NULL,
2718 zio_ddt_child_write_done, dde, zio->io_priority,
2719 ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark);
2721 zio_push_transform(cio, zio->io_abd, zio->io_size, 0, NULL);
2722 dde->dde_lead_zio[p] = cio;
2725 ddt_exit(ddt);
2727 if (cio)
2728 zio_nowait(cio);
2729 if (dio)
2730 zio_nowait(dio);
2732 return (ZIO_PIPELINE_CONTINUE);
2735 ddt_entry_t *freedde; /* for debugging */
2737 static int
2738 zio_ddt_free(zio_t *zio)
2740 spa_t *spa = zio->io_spa;
2741 blkptr_t *bp = zio->io_bp;
2742 ddt_t *ddt = ddt_select(spa, bp);
2743 ddt_entry_t *dde;
2744 ddt_phys_t *ddp;
2746 ASSERT(BP_GET_DEDUP(bp));
2747 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2749 ddt_enter(ddt);
2750 freedde = dde = ddt_lookup(ddt, bp, B_TRUE);
2751 ddp = ddt_phys_select(dde, bp);
2752 ddt_phys_decref(ddp);
2753 ddt_exit(ddt);
2755 return (ZIO_PIPELINE_CONTINUE);
2759 * ==========================================================================
2760 * Allocate and free blocks
2761 * ==========================================================================
2764 static zio_t *
2765 zio_io_to_allocate(spa_t *spa)
2767 zio_t *zio;
2769 ASSERT(MUTEX_HELD(&spa->spa_alloc_lock));
2771 zio = avl_first(&spa->spa_alloc_tree);
2772 if (zio == NULL)
2773 return (NULL);
2775 ASSERT(IO_IS_ALLOCATING(zio));
2778 * Try to place a reservation for this zio. If we're unable to
2779 * reserve then we throttle.
2781 if (!metaslab_class_throttle_reserve(spa_normal_class(spa),
2782 zio->io_prop.zp_copies, zio, 0)) {
2783 return (NULL);
2786 avl_remove(&spa->spa_alloc_tree, zio);
2787 ASSERT3U(zio->io_stage, <, ZIO_STAGE_DVA_ALLOCATE);
2789 return (zio);
2792 static int
2793 zio_dva_throttle(zio_t *zio)
2795 spa_t *spa = zio->io_spa;
2796 zio_t *nio;
2798 if (zio->io_priority == ZIO_PRIORITY_SYNC_WRITE ||
2799 !spa_normal_class(zio->io_spa)->mc_alloc_throttle_enabled ||
2800 zio->io_child_type == ZIO_CHILD_GANG ||
2801 zio->io_flags & ZIO_FLAG_NODATA) {
2802 return (ZIO_PIPELINE_CONTINUE);
2805 ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
2807 ASSERT3U(zio->io_queued_timestamp, >, 0);
2808 ASSERT(zio->io_stage == ZIO_STAGE_DVA_THROTTLE);
2810 mutex_enter(&spa->spa_alloc_lock);
2812 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
2813 avl_add(&spa->spa_alloc_tree, zio);
2815 nio = zio_io_to_allocate(zio->io_spa);
2816 mutex_exit(&spa->spa_alloc_lock);
2818 if (nio == zio)
2819 return (ZIO_PIPELINE_CONTINUE);
2821 if (nio != NULL) {
2822 ASSERT(nio->io_stage == ZIO_STAGE_DVA_THROTTLE);
2824 * We are passing control to a new zio so make sure that
2825 * it is processed by a different thread. We do this to
2826 * avoid stack overflows that can occur when parents are
2827 * throttled and children are making progress. We allow
2828 * it to go to the head of the taskq since it's already
2829 * been waiting.
2831 zio_taskq_dispatch(nio, ZIO_TASKQ_ISSUE, B_TRUE);
2833 return (ZIO_PIPELINE_STOP);
2836 void
2837 zio_allocate_dispatch(spa_t *spa)
2839 zio_t *zio;
2841 mutex_enter(&spa->spa_alloc_lock);
2842 zio = zio_io_to_allocate(spa);
2843 mutex_exit(&spa->spa_alloc_lock);
2844 if (zio == NULL)
2845 return;
2847 ASSERT3U(zio->io_stage, ==, ZIO_STAGE_DVA_THROTTLE);
2848 ASSERT0(zio->io_error);
2849 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_TRUE);
2852 static int
2853 zio_dva_allocate(zio_t *zio)
2855 spa_t *spa = zio->io_spa;
2856 metaslab_class_t *mc = spa_normal_class(spa);
2857 blkptr_t *bp = zio->io_bp;
2858 int error;
2859 int flags = 0;
2861 if (zio->io_gang_leader == NULL) {
2862 ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
2863 zio->io_gang_leader = zio;
2866 ASSERT(BP_IS_HOLE(bp));
2867 ASSERT0(BP_GET_NDVAS(bp));
2868 ASSERT3U(zio->io_prop.zp_copies, >, 0);
2869 ASSERT3U(zio->io_prop.zp_copies, <=, spa_max_replication(spa));
2870 ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp));
2872 if (zio->io_flags & ZIO_FLAG_NODATA) {
2873 flags |= METASLAB_DONT_THROTTLE;
2875 if (zio->io_flags & ZIO_FLAG_GANG_CHILD) {
2876 flags |= METASLAB_GANG_CHILD;
2878 if (zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE) {
2879 flags |= METASLAB_ASYNC_ALLOC;
2882 error = metaslab_alloc(spa, mc, zio->io_size, bp,
2883 zio->io_prop.zp_copies, zio->io_txg, NULL, flags,
2884 &zio->io_alloc_list, zio);
2886 if (error != 0) {
2887 spa_dbgmsg(spa, "%s: metaslab allocation failure: zio %p, "
2888 "size %llu, error %d", spa_name(spa), zio, zio->io_size,
2889 error);
2890 if (error == ENOSPC && zio->io_size > SPA_MINBLOCKSIZE)
2891 return (zio_write_gang_block(zio));
2892 zio->io_error = error;
2895 return (ZIO_PIPELINE_CONTINUE);
2898 static int
2899 zio_dva_free(zio_t *zio)
2901 metaslab_free(zio->io_spa, zio->io_bp, zio->io_txg, B_FALSE);
2903 return (ZIO_PIPELINE_CONTINUE);
2906 static int
2907 zio_dva_claim(zio_t *zio)
2909 int error;
2911 error = metaslab_claim(zio->io_spa, zio->io_bp, zio->io_txg);
2912 if (error)
2913 zio->io_error = error;
2915 return (ZIO_PIPELINE_CONTINUE);
2919 * Undo an allocation. This is used by zio_done() when an I/O fails
2920 * and we want to give back the block we just allocated.
2921 * This handles both normal blocks and gang blocks.
2923 static void
2924 zio_dva_unallocate(zio_t *zio, zio_gang_node_t *gn, blkptr_t *bp)
2926 ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp));
2927 ASSERT(zio->io_bp_override == NULL);
2929 if (!BP_IS_HOLE(bp))
2930 metaslab_free(zio->io_spa, bp, bp->blk_birth, B_TRUE);
2932 if (gn != NULL) {
2933 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
2934 zio_dva_unallocate(zio, gn->gn_child[g],
2935 &gn->gn_gbh->zg_blkptr[g]);
2941 * Try to allocate an intent log block. Return 0 on success, errno on failure.
2944 zio_alloc_zil(spa_t *spa, uint64_t txg, blkptr_t *new_bp, blkptr_t *old_bp,
2945 uint64_t size, boolean_t *slog)
2947 int error = 1;
2948 zio_alloc_list_t io_alloc_list;
2950 ASSERT(txg > spa_syncing_txg(spa));
2952 metaslab_trace_init(&io_alloc_list);
2953 error = metaslab_alloc(spa, spa_log_class(spa), size, new_bp, 1,
2954 txg, old_bp, METASLAB_HINTBP_AVOID, &io_alloc_list, NULL);
2955 if (error == 0) {
2956 *slog = TRUE;
2957 } else {
2958 error = metaslab_alloc(spa, spa_normal_class(spa), size,
2959 new_bp, 1, txg, old_bp, METASLAB_HINTBP_AVOID,
2960 &io_alloc_list, NULL);
2961 if (error == 0)
2962 *slog = FALSE;
2964 metaslab_trace_fini(&io_alloc_list);
2966 if (error == 0) {
2967 BP_SET_LSIZE(new_bp, size);
2968 BP_SET_PSIZE(new_bp, size);
2969 BP_SET_COMPRESS(new_bp, ZIO_COMPRESS_OFF);
2970 BP_SET_CHECKSUM(new_bp,
2971 spa_version(spa) >= SPA_VERSION_SLIM_ZIL
2972 ? ZIO_CHECKSUM_ZILOG2 : ZIO_CHECKSUM_ZILOG);
2973 BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG);
2974 BP_SET_LEVEL(new_bp, 0);
2975 BP_SET_DEDUP(new_bp, 0);
2976 BP_SET_BYTEORDER(new_bp, ZFS_HOST_BYTEORDER);
2977 } else {
2978 zfs_dbgmsg("%s: zil block allocation failure: "
2979 "size %llu, error %d", spa_name(spa), size, error);
2982 return (error);
2986 * Free an intent log block.
2988 void
2989 zio_free_zil(spa_t *spa, uint64_t txg, blkptr_t *bp)
2991 ASSERT(BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG);
2992 ASSERT(!BP_IS_GANG(bp));
2994 zio_free(spa, txg, bp);
2998 * ==========================================================================
2999 * Read and write to physical devices
3000 * ==========================================================================
3005 * Issue an I/O to the underlying vdev. Typically the issue pipeline
3006 * stops after this stage and will resume upon I/O completion.
3007 * However, there are instances where the vdev layer may need to
3008 * continue the pipeline when an I/O was not issued. Since the I/O
3009 * that was sent to the vdev layer might be different than the one
3010 * currently active in the pipeline (see vdev_queue_io()), we explicitly
3011 * force the underlying vdev layers to call either zio_execute() or
3012 * zio_interrupt() to ensure that the pipeline continues with the correct I/O.
3014 static int
3015 zio_vdev_io_start(zio_t *zio)
3017 vdev_t *vd = zio->io_vd;
3018 uint64_t align;
3019 spa_t *spa = zio->io_spa;
3021 ASSERT(zio->io_error == 0);
3022 ASSERT(zio->io_child_error[ZIO_CHILD_VDEV] == 0);
3024 if (vd == NULL) {
3025 if (!(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
3026 spa_config_enter(spa, SCL_ZIO, zio, RW_READER);
3029 * The mirror_ops handle multiple DVAs in a single BP.
3031 vdev_mirror_ops.vdev_op_io_start(zio);
3032 return (ZIO_PIPELINE_STOP);
3035 ASSERT3P(zio->io_logical, !=, zio);
3036 if (zio->io_type == ZIO_TYPE_WRITE && zio->io_vd->vdev_removing) {
3037 ASSERT(zio->io_flags &
3038 (ZIO_FLAG_PHYSICAL | ZIO_FLAG_SELF_HEAL |
3039 ZIO_FLAG_INDUCE_DAMAGE));
3043 * We keep track of time-sensitive I/Os so that the scan thread
3044 * can quickly react to certain workloads. In particular, we care
3045 * about non-scrubbing, top-level reads and writes with the following
3046 * characteristics:
3047 * - synchronous writes of user data to non-slog devices
3048 * - any reads of user data
3049 * When these conditions are met, adjust the timestamp of spa_last_io
3050 * which allows the scan thread to adjust its workload accordingly.
3052 if (!(zio->io_flags & ZIO_FLAG_SCAN_THREAD) && zio->io_bp != NULL &&
3053 vd == vd->vdev_top && !vd->vdev_islog &&
3054 zio->io_bookmark.zb_objset != DMU_META_OBJSET &&
3055 zio->io_txg != spa_syncing_txg(spa)) {
3056 uint64_t old = spa->spa_last_io;
3057 uint64_t new = ddi_get_lbolt64();
3058 if (old != new)
3059 (void) atomic_cas_64(&spa->spa_last_io, old, new);
3062 align = 1ULL << vd->vdev_top->vdev_ashift;
3064 if (!(zio->io_flags & ZIO_FLAG_PHYSICAL) &&
3065 P2PHASE(zio->io_size, align) != 0) {
3066 /* Transform logical writes to be a full physical block size. */
3067 uint64_t asize = P2ROUNDUP(zio->io_size, align);
3068 abd_t *abuf = abd_alloc_sametype(zio->io_abd, asize);
3069 ASSERT(vd == vd->vdev_top);
3070 if (zio->io_type == ZIO_TYPE_WRITE) {
3071 abd_copy(abuf, zio->io_abd, zio->io_size);
3072 abd_zero_off(abuf, zio->io_size, asize - zio->io_size);
3074 zio_push_transform(zio, abuf, asize, asize, zio_subblock);
3078 * If this is not a physical io, make sure that it is properly aligned
3079 * before proceeding.
3081 if (!(zio->io_flags & ZIO_FLAG_PHYSICAL)) {
3082 ASSERT0(P2PHASE(zio->io_offset, align));
3083 ASSERT0(P2PHASE(zio->io_size, align));
3084 } else {
3086 * For physical writes, we allow 512b aligned writes and assume
3087 * the device will perform a read-modify-write as necessary.
3089 ASSERT0(P2PHASE(zio->io_offset, SPA_MINBLOCKSIZE));
3090 ASSERT0(P2PHASE(zio->io_size, SPA_MINBLOCKSIZE));
3093 VERIFY(zio->io_type != ZIO_TYPE_WRITE || spa_writeable(spa));
3096 * If this is a repair I/O, and there's no self-healing involved --
3097 * that is, we're just resilvering what we expect to resilver --
3098 * then don't do the I/O unless zio's txg is actually in vd's DTL.
3099 * This prevents spurious resilvering with nested replication.
3100 * For example, given a mirror of mirrors, (A+B)+(C+D), if only
3101 * A is out of date, we'll read from C+D, then use the data to
3102 * resilver A+B -- but we don't actually want to resilver B, just A.
3103 * The top-level mirror has no way to know this, so instead we just
3104 * discard unnecessary repairs as we work our way down the vdev tree.
3105 * The same logic applies to any form of nested replication:
3106 * ditto + mirror, RAID-Z + replacing, etc. This covers them all.
3108 if ((zio->io_flags & ZIO_FLAG_IO_REPAIR) &&
3109 !(zio->io_flags & ZIO_FLAG_SELF_HEAL) &&
3110 zio->io_txg != 0 && /* not a delegated i/o */
3111 !vdev_dtl_contains(vd, DTL_PARTIAL, zio->io_txg, 1)) {
3112 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
3113 zio_vdev_io_bypass(zio);
3114 return (ZIO_PIPELINE_CONTINUE);
3117 if (vd->vdev_ops->vdev_op_leaf &&
3118 (zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE)) {
3120 if (zio->io_type == ZIO_TYPE_READ && vdev_cache_read(zio))
3121 return (ZIO_PIPELINE_CONTINUE);
3123 if ((zio = vdev_queue_io(zio)) == NULL)
3124 return (ZIO_PIPELINE_STOP);
3126 if (!vdev_accessible(vd, zio)) {
3127 zio->io_error = SET_ERROR(ENXIO);
3128 zio_interrupt(zio);
3129 return (ZIO_PIPELINE_STOP);
3133 vd->vdev_ops->vdev_op_io_start(zio);
3134 return (ZIO_PIPELINE_STOP);
3137 static int
3138 zio_vdev_io_done(zio_t *zio)
3140 vdev_t *vd = zio->io_vd;
3141 vdev_ops_t *ops = vd ? vd->vdev_ops : &vdev_mirror_ops;
3142 boolean_t unexpected_error = B_FALSE;
3144 if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE))
3145 return (ZIO_PIPELINE_STOP);
3147 ASSERT(zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE);
3149 if (vd != NULL && vd->vdev_ops->vdev_op_leaf) {
3151 vdev_queue_io_done(zio);
3153 if (zio->io_type == ZIO_TYPE_WRITE)
3154 vdev_cache_write(zio);
3156 if (zio_injection_enabled && zio->io_error == 0)
3157 zio->io_error = zio_handle_device_injection(vd,
3158 zio, EIO);
3160 if (zio_injection_enabled && zio->io_error == 0)
3161 zio->io_error = zio_handle_label_injection(zio, EIO);
3163 if (zio->io_error) {
3164 if (!vdev_accessible(vd, zio)) {
3165 zio->io_error = SET_ERROR(ENXIO);
3166 } else {
3167 unexpected_error = B_TRUE;
3172 ops->vdev_op_io_done(zio);
3174 if (unexpected_error)
3175 VERIFY(vdev_probe(vd, zio) == NULL);
3177 return (ZIO_PIPELINE_CONTINUE);
3181 * For non-raidz ZIOs, we can just copy aside the bad data read from the
3182 * disk, and use that to finish the checksum ereport later.
3184 static void
3185 zio_vsd_default_cksum_finish(zio_cksum_report_t *zcr,
3186 const void *good_buf)
3188 /* no processing needed */
3189 zfs_ereport_finish_checksum(zcr, good_buf, zcr->zcr_cbdata, B_FALSE);
3192 /*ARGSUSED*/
3193 void
3194 zio_vsd_default_cksum_report(zio_t *zio, zio_cksum_report_t *zcr, void *ignored)
3196 void *buf = zio_buf_alloc(zio->io_size);
3198 abd_copy_to_buf(buf, zio->io_abd, zio->io_size);
3200 zcr->zcr_cbinfo = zio->io_size;
3201 zcr->zcr_cbdata = buf;
3202 zcr->zcr_finish = zio_vsd_default_cksum_finish;
3203 zcr->zcr_free = zio_buf_free;
3206 static int
3207 zio_vdev_io_assess(zio_t *zio)
3209 vdev_t *vd = zio->io_vd;
3211 if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE))
3212 return (ZIO_PIPELINE_STOP);
3214 if (vd == NULL && !(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
3215 spa_config_exit(zio->io_spa, SCL_ZIO, zio);
3217 if (zio->io_vsd != NULL) {
3218 zio->io_vsd_ops->vsd_free(zio);
3219 zio->io_vsd = NULL;
3222 if (zio_injection_enabled && zio->io_error == 0)
3223 zio->io_error = zio_handle_fault_injection(zio, EIO);
3226 * If the I/O failed, determine whether we should attempt to retry it.
3228 * On retry, we cut in line in the issue queue, since we don't want
3229 * compression/checksumming/etc. work to prevent our (cheap) IO reissue.
3231 if (zio->io_error && vd == NULL &&
3232 !(zio->io_flags & (ZIO_FLAG_DONT_RETRY | ZIO_FLAG_IO_RETRY))) {
3233 ASSERT(!(zio->io_flags & ZIO_FLAG_DONT_QUEUE)); /* not a leaf */
3234 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_BYPASS)); /* not a leaf */
3235 zio->io_error = 0;
3236 zio->io_flags |= ZIO_FLAG_IO_RETRY |
3237 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE;
3238 zio->io_stage = ZIO_STAGE_VDEV_IO_START >> 1;
3239 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE,
3240 zio_requeue_io_start_cut_in_line);
3241 return (ZIO_PIPELINE_STOP);
3245 * If we got an error on a leaf device, convert it to ENXIO
3246 * if the device is not accessible at all.
3248 if (zio->io_error && vd != NULL && vd->vdev_ops->vdev_op_leaf &&
3249 !vdev_accessible(vd, zio))
3250 zio->io_error = SET_ERROR(ENXIO);
3253 * If we can't write to an interior vdev (mirror or RAID-Z),
3254 * set vdev_cant_write so that we stop trying to allocate from it.
3256 if (zio->io_error == ENXIO && zio->io_type == ZIO_TYPE_WRITE &&
3257 vd != NULL && !vd->vdev_ops->vdev_op_leaf) {
3258 vd->vdev_cant_write = B_TRUE;
3262 * If a cache flush returns ENOTSUP or ENOTTY, we know that no future
3263 * attempts will ever succeed. In this case we set a persistent bit so
3264 * that we don't bother with it in the future.
3266 if ((zio->io_error == ENOTSUP || zio->io_error == ENOTTY) &&
3267 zio->io_type == ZIO_TYPE_IOCTL &&
3268 zio->io_cmd == DKIOCFLUSHWRITECACHE && vd != NULL)
3269 vd->vdev_nowritecache = B_TRUE;
3271 if (zio->io_error)
3272 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
3274 if (vd != NULL && vd->vdev_ops->vdev_op_leaf &&
3275 zio->io_physdone != NULL) {
3276 ASSERT(!(zio->io_flags & ZIO_FLAG_DELEGATED));
3277 ASSERT(zio->io_child_type == ZIO_CHILD_VDEV);
3278 zio->io_physdone(zio->io_logical);
3281 return (ZIO_PIPELINE_CONTINUE);
3284 void
3285 zio_vdev_io_reissue(zio_t *zio)
3287 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
3288 ASSERT(zio->io_error == 0);
3290 zio->io_stage >>= 1;
3293 void
3294 zio_vdev_io_redone(zio_t *zio)
3296 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_DONE);
3298 zio->io_stage >>= 1;
3301 void
3302 zio_vdev_io_bypass(zio_t *zio)
3304 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
3305 ASSERT(zio->io_error == 0);
3307 zio->io_flags |= ZIO_FLAG_IO_BYPASS;
3308 zio->io_stage = ZIO_STAGE_VDEV_IO_ASSESS >> 1;
3312 * ==========================================================================
3313 * Generate and verify checksums
3314 * ==========================================================================
3316 static int
3317 zio_checksum_generate(zio_t *zio)
3319 blkptr_t *bp = zio->io_bp;
3320 enum zio_checksum checksum;
3322 if (bp == NULL) {
3324 * This is zio_write_phys().
3325 * We're either generating a label checksum, or none at all.
3327 checksum = zio->io_prop.zp_checksum;
3329 if (checksum == ZIO_CHECKSUM_OFF)
3330 return (ZIO_PIPELINE_CONTINUE);
3332 ASSERT(checksum == ZIO_CHECKSUM_LABEL);
3333 } else {
3334 if (BP_IS_GANG(bp) && zio->io_child_type == ZIO_CHILD_GANG) {
3335 ASSERT(!IO_IS_ALLOCATING(zio));
3336 checksum = ZIO_CHECKSUM_GANG_HEADER;
3337 } else {
3338 checksum = BP_GET_CHECKSUM(bp);
3342 zio_checksum_compute(zio, checksum, zio->io_abd, zio->io_size);
3344 return (ZIO_PIPELINE_CONTINUE);
3347 static int
3348 zio_checksum_verify(zio_t *zio)
3350 zio_bad_cksum_t info;
3351 blkptr_t *bp = zio->io_bp;
3352 int error;
3354 ASSERT(zio->io_vd != NULL);
3356 if (bp == NULL) {
3358 * This is zio_read_phys().
3359 * We're either verifying a label checksum, or nothing at all.
3361 if (zio->io_prop.zp_checksum == ZIO_CHECKSUM_OFF)
3362 return (ZIO_PIPELINE_CONTINUE);
3364 ASSERT(zio->io_prop.zp_checksum == ZIO_CHECKSUM_LABEL);
3367 if ((error = zio_checksum_error(zio, &info)) != 0) {
3368 zio->io_error = error;
3369 if (error == ECKSUM &&
3370 !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
3371 zfs_ereport_start_checksum(zio->io_spa,
3372 zio->io_vd, zio, zio->io_offset,
3373 zio->io_size, NULL, &info);
3377 return (ZIO_PIPELINE_CONTINUE);
3381 * Called by RAID-Z to ensure we don't compute the checksum twice.
3383 void
3384 zio_checksum_verified(zio_t *zio)
3386 zio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY;
3390 * ==========================================================================
3391 * Error rank. Error are ranked in the order 0, ENXIO, ECKSUM, EIO, other.
3392 * An error of 0 indicates success. ENXIO indicates whole-device failure,
3393 * which may be transient (e.g. unplugged) or permament. ECKSUM and EIO
3394 * indicate errors that are specific to one I/O, and most likely permanent.
3395 * Any other error is presumed to be worse because we weren't expecting it.
3396 * ==========================================================================
3399 zio_worst_error(int e1, int e2)
3401 static int zio_error_rank[] = { 0, ENXIO, ECKSUM, EIO };
3402 int r1, r2;
3404 for (r1 = 0; r1 < sizeof (zio_error_rank) / sizeof (int); r1++)
3405 if (e1 == zio_error_rank[r1])
3406 break;
3408 for (r2 = 0; r2 < sizeof (zio_error_rank) / sizeof (int); r2++)
3409 if (e2 == zio_error_rank[r2])
3410 break;
3412 return (r1 > r2 ? e1 : e2);
3416 * ==========================================================================
3417 * I/O completion
3418 * ==========================================================================
3420 static int
3421 zio_ready(zio_t *zio)
3423 blkptr_t *bp = zio->io_bp;
3424 zio_t *pio, *pio_next;
3425 zio_link_t *zl = NULL;
3427 if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_READY) ||
3428 zio_wait_for_children(zio, ZIO_CHILD_DDT, ZIO_WAIT_READY))
3429 return (ZIO_PIPELINE_STOP);
3431 if (zio->io_ready) {
3432 ASSERT(IO_IS_ALLOCATING(zio));
3433 ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp) ||
3434 (zio->io_flags & ZIO_FLAG_NOPWRITE));
3435 ASSERT(zio->io_children[ZIO_CHILD_GANG][ZIO_WAIT_READY] == 0);
3437 zio->io_ready(zio);
3440 if (bp != NULL && bp != &zio->io_bp_copy)
3441 zio->io_bp_copy = *bp;
3443 if (zio->io_error != 0) {
3444 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
3446 if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
3447 ASSERT(IO_IS_ALLOCATING(zio));
3448 ASSERT(zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
3450 * We were unable to allocate anything, unreserve and
3451 * issue the next I/O to allocate.
3453 metaslab_class_throttle_unreserve(
3454 spa_normal_class(zio->io_spa),
3455 zio->io_prop.zp_copies, zio);
3456 zio_allocate_dispatch(zio->io_spa);
3460 mutex_enter(&zio->io_lock);
3461 zio->io_state[ZIO_WAIT_READY] = 1;
3462 pio = zio_walk_parents(zio, &zl);
3463 mutex_exit(&zio->io_lock);
3466 * As we notify zio's parents, new parents could be added.
3467 * New parents go to the head of zio's io_parent_list, however,
3468 * so we will (correctly) not notify them. The remainder of zio's
3469 * io_parent_list, from 'pio_next' onward, cannot change because
3470 * all parents must wait for us to be done before they can be done.
3472 for (; pio != NULL; pio = pio_next) {
3473 pio_next = zio_walk_parents(zio, &zl);
3474 zio_notify_parent(pio, zio, ZIO_WAIT_READY);
3477 if (zio->io_flags & ZIO_FLAG_NODATA) {
3478 if (BP_IS_GANG(bp)) {
3479 zio->io_flags &= ~ZIO_FLAG_NODATA;
3480 } else {
3481 ASSERT((uintptr_t)zio->io_abd < SPA_MAXBLOCKSIZE);
3482 zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES;
3486 if (zio_injection_enabled &&
3487 zio->io_spa->spa_syncing_txg == zio->io_txg)
3488 zio_handle_ignored_writes(zio);
3490 return (ZIO_PIPELINE_CONTINUE);
3494 * Update the allocation throttle accounting.
3496 static void
3497 zio_dva_throttle_done(zio_t *zio)
3499 zio_t *lio = zio->io_logical;
3500 zio_t *pio = zio_unique_parent(zio);
3501 vdev_t *vd = zio->io_vd;
3502 int flags = METASLAB_ASYNC_ALLOC;
3504 ASSERT3P(zio->io_bp, !=, NULL);
3505 ASSERT3U(zio->io_type, ==, ZIO_TYPE_WRITE);
3506 ASSERT3U(zio->io_priority, ==, ZIO_PRIORITY_ASYNC_WRITE);
3507 ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_VDEV);
3508 ASSERT(vd != NULL);
3509 ASSERT3P(vd, ==, vd->vdev_top);
3510 ASSERT(!(zio->io_flags & (ZIO_FLAG_IO_REPAIR | ZIO_FLAG_IO_RETRY)));
3511 ASSERT(zio->io_flags & ZIO_FLAG_IO_ALLOCATING);
3512 ASSERT(!(lio->io_flags & ZIO_FLAG_IO_REWRITE));
3513 ASSERT(!(lio->io_orig_flags & ZIO_FLAG_NODATA));
3516 * Parents of gang children can have two flavors -- ones that
3517 * allocated the gang header (will have ZIO_FLAG_IO_REWRITE set)
3518 * and ones that allocated the constituent blocks. The allocation
3519 * throttle needs to know the allocating parent zio so we must find
3520 * it here.
3522 if (pio->io_child_type == ZIO_CHILD_GANG) {
3524 * If our parent is a rewrite gang child then our grandparent
3525 * would have been the one that performed the allocation.
3527 if (pio->io_flags & ZIO_FLAG_IO_REWRITE)
3528 pio = zio_unique_parent(pio);
3529 flags |= METASLAB_GANG_CHILD;
3532 ASSERT(IO_IS_ALLOCATING(pio));
3533 ASSERT3P(zio, !=, zio->io_logical);
3534 ASSERT(zio->io_logical != NULL);
3535 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REPAIR));
3536 ASSERT0(zio->io_flags & ZIO_FLAG_NOPWRITE);
3538 mutex_enter(&pio->io_lock);
3539 metaslab_group_alloc_decrement(zio->io_spa, vd->vdev_id, pio, flags);
3540 mutex_exit(&pio->io_lock);
3542 metaslab_class_throttle_unreserve(spa_normal_class(zio->io_spa),
3543 1, pio);
3546 * Call into the pipeline to see if there is more work that
3547 * needs to be done. If there is work to be done it will be
3548 * dispatched to another taskq thread.
3550 zio_allocate_dispatch(zio->io_spa);
3553 static int
3554 zio_done(zio_t *zio)
3556 spa_t *spa = zio->io_spa;
3557 zio_t *lio = zio->io_logical;
3558 blkptr_t *bp = zio->io_bp;
3559 vdev_t *vd = zio->io_vd;
3560 uint64_t psize = zio->io_size;
3561 zio_t *pio, *pio_next;
3562 metaslab_class_t *mc = spa_normal_class(spa);
3563 zio_link_t *zl = NULL;
3566 * If our children haven't all completed,
3567 * wait for them and then repeat this pipeline stage.
3569 if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE) ||
3570 zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_DONE) ||
3571 zio_wait_for_children(zio, ZIO_CHILD_DDT, ZIO_WAIT_DONE) ||
3572 zio_wait_for_children(zio, ZIO_CHILD_LOGICAL, ZIO_WAIT_DONE))
3573 return (ZIO_PIPELINE_STOP);
3576 * If the allocation throttle is enabled, then update the accounting.
3577 * We only track child I/Os that are part of an allocating async
3578 * write. We must do this since the allocation is performed
3579 * by the logical I/O but the actual write is done by child I/Os.
3581 if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING &&
3582 zio->io_child_type == ZIO_CHILD_VDEV) {
3583 ASSERT(mc->mc_alloc_throttle_enabled);
3584 zio_dva_throttle_done(zio);
3588 * If the allocation throttle is enabled, verify that
3589 * we have decremented the refcounts for every I/O that was throttled.
3591 if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
3592 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
3593 ASSERT(zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
3594 ASSERT(bp != NULL);
3595 metaslab_group_alloc_verify(spa, zio->io_bp, zio);
3596 VERIFY(refcount_not_held(&mc->mc_alloc_slots, zio));
3599 for (int c = 0; c < ZIO_CHILD_TYPES; c++)
3600 for (int w = 0; w < ZIO_WAIT_TYPES; w++)
3601 ASSERT(zio->io_children[c][w] == 0);
3603 if (bp != NULL && !BP_IS_EMBEDDED(bp)) {
3604 ASSERT(bp->blk_pad[0] == 0);
3605 ASSERT(bp->blk_pad[1] == 0);
3606 ASSERT(bcmp(bp, &zio->io_bp_copy, sizeof (blkptr_t)) == 0 ||
3607 (bp == zio_unique_parent(zio)->io_bp));
3608 if (zio->io_type == ZIO_TYPE_WRITE && !BP_IS_HOLE(bp) &&
3609 zio->io_bp_override == NULL &&
3610 !(zio->io_flags & ZIO_FLAG_IO_REPAIR)) {
3611 ASSERT(!BP_SHOULD_BYTESWAP(bp));
3612 ASSERT3U(zio->io_prop.zp_copies, <=, BP_GET_NDVAS(bp));
3613 ASSERT(BP_COUNT_GANG(bp) == 0 ||
3614 (BP_COUNT_GANG(bp) == BP_GET_NDVAS(bp)));
3616 if (zio->io_flags & ZIO_FLAG_NOPWRITE)
3617 VERIFY(BP_EQUAL(bp, &zio->io_bp_orig));
3621 * If there were child vdev/gang/ddt errors, they apply to us now.
3623 zio_inherit_child_errors(zio, ZIO_CHILD_VDEV);
3624 zio_inherit_child_errors(zio, ZIO_CHILD_GANG);
3625 zio_inherit_child_errors(zio, ZIO_CHILD_DDT);
3628 * If the I/O on the transformed data was successful, generate any
3629 * checksum reports now while we still have the transformed data.
3631 if (zio->io_error == 0) {
3632 while (zio->io_cksum_report != NULL) {
3633 zio_cksum_report_t *zcr = zio->io_cksum_report;
3634 uint64_t align = zcr->zcr_align;
3635 uint64_t asize = P2ROUNDUP(psize, align);
3636 char *abuf = NULL;
3637 abd_t *adata = zio->io_abd;
3639 if (asize != psize) {
3640 adata = abd_alloc_linear(asize, B_TRUE);
3641 abd_copy(adata, zio->io_abd, psize);
3642 abd_zero_off(adata, psize, asize - psize);
3645 if (adata != NULL)
3646 abuf = abd_borrow_buf_copy(adata, asize);
3648 zio->io_cksum_report = zcr->zcr_next;
3649 zcr->zcr_next = NULL;
3650 zcr->zcr_finish(zcr, abuf);
3651 zfs_ereport_free_checksum(zcr);
3653 if (adata != NULL)
3654 abd_return_buf(adata, abuf, asize);
3656 if (asize != psize)
3657 abd_free(adata);
3661 zio_pop_transforms(zio); /* note: may set zio->io_error */
3663 vdev_stat_update(zio, psize);
3665 if (zio->io_error) {
3667 * If this I/O is attached to a particular vdev,
3668 * generate an error message describing the I/O failure
3669 * at the block level. We ignore these errors if the
3670 * device is currently unavailable.
3672 if (zio->io_error != ECKSUM && vd != NULL && !vdev_is_dead(vd))
3673 zfs_ereport_post(FM_EREPORT_ZFS_IO, spa, vd, zio, 0, 0);
3675 if ((zio->io_error == EIO || !(zio->io_flags &
3676 (ZIO_FLAG_SPECULATIVE | ZIO_FLAG_DONT_PROPAGATE))) &&
3677 zio == lio) {
3679 * For logical I/O requests, tell the SPA to log the
3680 * error and generate a logical data ereport.
3682 spa_log_error(spa, zio);
3683 zfs_ereport_post(FM_EREPORT_ZFS_DATA, spa, NULL, zio,
3684 0, 0);
3688 if (zio->io_error && zio == lio) {
3690 * Determine whether zio should be reexecuted. This will
3691 * propagate all the way to the root via zio_notify_parent().
3693 ASSERT(vd == NULL && bp != NULL);
3694 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
3696 if (IO_IS_ALLOCATING(zio) &&
3697 !(zio->io_flags & ZIO_FLAG_CANFAIL)) {
3698 if (zio->io_error != ENOSPC)
3699 zio->io_reexecute |= ZIO_REEXECUTE_NOW;
3700 else
3701 zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
3704 if ((zio->io_type == ZIO_TYPE_READ ||
3705 zio->io_type == ZIO_TYPE_FREE) &&
3706 !(zio->io_flags & ZIO_FLAG_SCAN_THREAD) &&
3707 zio->io_error == ENXIO &&
3708 spa_load_state(spa) == SPA_LOAD_NONE &&
3709 spa_get_failmode(spa) != ZIO_FAILURE_MODE_CONTINUE)
3710 zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
3712 if (!(zio->io_flags & ZIO_FLAG_CANFAIL) && !zio->io_reexecute)
3713 zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
3716 * Here is a possibly good place to attempt to do
3717 * either combinatorial reconstruction or error correction
3718 * based on checksums. It also might be a good place
3719 * to send out preliminary ereports before we suspend
3720 * processing.
3725 * If there were logical child errors, they apply to us now.
3726 * We defer this until now to avoid conflating logical child
3727 * errors with errors that happened to the zio itself when
3728 * updating vdev stats and reporting FMA events above.
3730 zio_inherit_child_errors(zio, ZIO_CHILD_LOGICAL);
3732 if ((zio->io_error || zio->io_reexecute) &&
3733 IO_IS_ALLOCATING(zio) && zio->io_gang_leader == zio &&
3734 !(zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE)))
3735 zio_dva_unallocate(zio, zio->io_gang_tree, bp);
3737 zio_gang_tree_free(&zio->io_gang_tree);
3740 * Godfather I/Os should never suspend.
3742 if ((zio->io_flags & ZIO_FLAG_GODFATHER) &&
3743 (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND))
3744 zio->io_reexecute = 0;
3746 if (zio->io_reexecute) {
3748 * This is a logical I/O that wants to reexecute.
3750 * Reexecute is top-down. When an i/o fails, if it's not
3751 * the root, it simply notifies its parent and sticks around.
3752 * The parent, seeing that it still has children in zio_done(),
3753 * does the same. This percolates all the way up to the root.
3754 * The root i/o will reexecute or suspend the entire tree.
3756 * This approach ensures that zio_reexecute() honors
3757 * all the original i/o dependency relationships, e.g.
3758 * parents not executing until children are ready.
3760 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
3762 zio->io_gang_leader = NULL;
3764 mutex_enter(&zio->io_lock);
3765 zio->io_state[ZIO_WAIT_DONE] = 1;
3766 mutex_exit(&zio->io_lock);
3769 * "The Godfather" I/O monitors its children but is
3770 * not a true parent to them. It will track them through
3771 * the pipeline but severs its ties whenever they get into
3772 * trouble (e.g. suspended). This allows "The Godfather"
3773 * I/O to return status without blocking.
3775 zl = NULL;
3776 for (pio = zio_walk_parents(zio, &zl); pio != NULL;
3777 pio = pio_next) {
3778 zio_link_t *remove_zl = zl;
3779 pio_next = zio_walk_parents(zio, &zl);
3781 if ((pio->io_flags & ZIO_FLAG_GODFATHER) &&
3782 (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND)) {
3783 zio_remove_child(pio, zio, remove_zl);
3784 zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
3788 if ((pio = zio_unique_parent(zio)) != NULL) {
3790 * We're not a root i/o, so there's nothing to do
3791 * but notify our parent. Don't propagate errors
3792 * upward since we haven't permanently failed yet.
3794 ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER));
3795 zio->io_flags |= ZIO_FLAG_DONT_PROPAGATE;
3796 zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
3797 } else if (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND) {
3799 * We'd fail again if we reexecuted now, so suspend
3800 * until conditions improve (e.g. device comes online).
3802 zio_suspend(spa, zio);
3803 } else {
3805 * Reexecution is potentially a huge amount of work.
3806 * Hand it off to the otherwise-unused claim taskq.
3808 ASSERT(zio->io_tqent.tqent_next == NULL);
3809 spa_taskq_dispatch_ent(spa, ZIO_TYPE_CLAIM,
3810 ZIO_TASKQ_ISSUE, (task_func_t *)zio_reexecute, zio,
3811 0, &zio->io_tqent);
3813 return (ZIO_PIPELINE_STOP);
3816 ASSERT(zio->io_child_count == 0);
3817 ASSERT(zio->io_reexecute == 0);
3818 ASSERT(zio->io_error == 0 || (zio->io_flags & ZIO_FLAG_CANFAIL));
3821 * Report any checksum errors, since the I/O is complete.
3823 while (zio->io_cksum_report != NULL) {
3824 zio_cksum_report_t *zcr = zio->io_cksum_report;
3825 zio->io_cksum_report = zcr->zcr_next;
3826 zcr->zcr_next = NULL;
3827 zcr->zcr_finish(zcr, NULL);
3828 zfs_ereport_free_checksum(zcr);
3832 * It is the responsibility of the done callback to ensure that this
3833 * particular zio is no longer discoverable for adoption, and as
3834 * such, cannot acquire any new parents.
3836 if (zio->io_done)
3837 zio->io_done(zio);
3839 mutex_enter(&zio->io_lock);
3840 zio->io_state[ZIO_WAIT_DONE] = 1;
3841 mutex_exit(&zio->io_lock);
3843 zl = NULL;
3844 for (pio = zio_walk_parents(zio, &zl); pio != NULL; pio = pio_next) {
3845 zio_link_t *remove_zl = zl;
3846 pio_next = zio_walk_parents(zio, &zl);
3847 zio_remove_child(pio, zio, remove_zl);
3848 zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
3851 if (zio->io_waiter != NULL) {
3852 mutex_enter(&zio->io_lock);
3853 zio->io_executor = NULL;
3854 cv_broadcast(&zio->io_cv);
3855 mutex_exit(&zio->io_lock);
3856 } else {
3857 zio_destroy(zio);
3860 return (ZIO_PIPELINE_STOP);
3864 * ==========================================================================
3865 * I/O pipeline definition
3866 * ==========================================================================
3868 static zio_pipe_stage_t *zio_pipeline[] = {
3869 NULL,
3870 zio_read_bp_init,
3871 zio_write_bp_init,
3872 zio_free_bp_init,
3873 zio_issue_async,
3874 zio_write_compress,
3875 zio_checksum_generate,
3876 zio_nop_write,
3877 zio_ddt_read_start,
3878 zio_ddt_read_done,
3879 zio_ddt_write,
3880 zio_ddt_free,
3881 zio_gang_assemble,
3882 zio_gang_issue,
3883 zio_dva_throttle,
3884 zio_dva_allocate,
3885 zio_dva_free,
3886 zio_dva_claim,
3887 zio_ready,
3888 zio_vdev_io_start,
3889 zio_vdev_io_done,
3890 zio_vdev_io_assess,
3891 zio_checksum_verify,
3892 zio_done
3899 * Compare two zbookmark_phys_t's to see which we would reach first in a
3900 * pre-order traversal of the object tree.
3902 * This is simple in every case aside from the meta-dnode object. For all other
3903 * objects, we traverse them in order (object 1 before object 2, and so on).
3904 * However, all of these objects are traversed while traversing object 0, since
3905 * the data it points to is the list of objects. Thus, we need to convert to a
3906 * canonical representation so we can compare meta-dnode bookmarks to
3907 * non-meta-dnode bookmarks.
3909 * We do this by calculating "equivalents" for each field of the zbookmark.
3910 * zbookmarks outside of the meta-dnode use their own object and level, and
3911 * calculate the level 0 equivalent (the first L0 blkid that is contained in the
3912 * blocks this bookmark refers to) by multiplying their blkid by their span
3913 * (the number of L0 blocks contained within one block at their level).
3914 * zbookmarks inside the meta-dnode calculate their object equivalent
3915 * (which is L0equiv * dnodes per data block), use 0 for their L0equiv, and use
3916 * level + 1<<31 (any value larger than a level could ever be) for their level.
3917 * This causes them to always compare before a bookmark in their object
3918 * equivalent, compare appropriately to bookmarks in other objects, and to
3919 * compare appropriately to other bookmarks in the meta-dnode.
3922 zbookmark_compare(uint16_t dbss1, uint8_t ibs1, uint16_t dbss2, uint8_t ibs2,
3923 const zbookmark_phys_t *zb1, const zbookmark_phys_t *zb2)
3926 * These variables represent the "equivalent" values for the zbookmark,
3927 * after converting zbookmarks inside the meta dnode to their
3928 * normal-object equivalents.
3930 uint64_t zb1obj, zb2obj;
3931 uint64_t zb1L0, zb2L0;
3932 uint64_t zb1level, zb2level;
3934 if (zb1->zb_object == zb2->zb_object &&
3935 zb1->zb_level == zb2->zb_level &&
3936 zb1->zb_blkid == zb2->zb_blkid)
3937 return (0);
3940 * BP_SPANB calculates the span in blocks.
3942 zb1L0 = (zb1->zb_blkid) * BP_SPANB(ibs1, zb1->zb_level);
3943 zb2L0 = (zb2->zb_blkid) * BP_SPANB(ibs2, zb2->zb_level);
3945 if (zb1->zb_object == DMU_META_DNODE_OBJECT) {
3946 zb1obj = zb1L0 * (dbss1 << (SPA_MINBLOCKSHIFT - DNODE_SHIFT));
3947 zb1L0 = 0;
3948 zb1level = zb1->zb_level + COMPARE_META_LEVEL;
3949 } else {
3950 zb1obj = zb1->zb_object;
3951 zb1level = zb1->zb_level;
3954 if (zb2->zb_object == DMU_META_DNODE_OBJECT) {
3955 zb2obj = zb2L0 * (dbss2 << (SPA_MINBLOCKSHIFT - DNODE_SHIFT));
3956 zb2L0 = 0;
3957 zb2level = zb2->zb_level + COMPARE_META_LEVEL;
3958 } else {
3959 zb2obj = zb2->zb_object;
3960 zb2level = zb2->zb_level;
3963 /* Now that we have a canonical representation, do the comparison. */
3964 if (zb1obj != zb2obj)
3965 return (zb1obj < zb2obj ? -1 : 1);
3966 else if (zb1L0 != zb2L0)
3967 return (zb1L0 < zb2L0 ? -1 : 1);
3968 else if (zb1level != zb2level)
3969 return (zb1level > zb2level ? -1 : 1);
3971 * This can (theoretically) happen if the bookmarks have the same object
3972 * and level, but different blkids, if the block sizes are not the same.
3973 * There is presently no way to change the indirect block sizes
3975 return (0);
3979 * This function checks the following: given that last_block is the place that
3980 * our traversal stopped last time, does that guarantee that we've visited
3981 * every node under subtree_root? Therefore, we can't just use the raw output
3982 * of zbookmark_compare. We have to pass in a modified version of
3983 * subtree_root; by incrementing the block id, and then checking whether
3984 * last_block is before or equal to that, we can tell whether or not having
3985 * visited last_block implies that all of subtree_root's children have been
3986 * visited.
3988 boolean_t
3989 zbookmark_subtree_completed(const dnode_phys_t *dnp,
3990 const zbookmark_phys_t *subtree_root, const zbookmark_phys_t *last_block)
3992 zbookmark_phys_t mod_zb = *subtree_root;
3993 mod_zb.zb_blkid++;
3994 ASSERT(last_block->zb_level == 0);
3996 /* The objset_phys_t isn't before anything. */
3997 if (dnp == NULL)
3998 return (B_FALSE);
4001 * We pass in 1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT) for the
4002 * data block size in sectors, because that variable is only used if
4003 * the bookmark refers to a block in the meta-dnode. Since we don't
4004 * know without examining it what object it refers to, and there's no
4005 * harm in passing in this value in other cases, we always pass it in.
4007 * We pass in 0 for the indirect block size shift because zb2 must be
4008 * level 0. The indirect block size is only used to calculate the span
4009 * of the bookmark, but since the bookmark must be level 0, the span is
4010 * always 1, so the math works out.
4012 * If you make changes to how the zbookmark_compare code works, be sure
4013 * to make sure that this code still works afterwards.
4015 return (zbookmark_compare(dnp->dn_datablkszsec, dnp->dn_indblkshift,
4016 1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT), 0, &mod_zb,
4017 last_block) <= 0);