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]
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.
27 #include <sys/sysmacros.h>
28 #include <sys/zfs_context.h>
29 #include <sys/fm/fs/zfs.h>
32 #include <sys/spa_impl.h>
33 #include <sys/vdev_impl.h>
34 #include <sys/zio_impl.h>
35 #include <sys/zio_compress.h>
36 #include <sys/zio_checksum.h>
37 #include <sys/dmu_objset.h>
40 #include <sys/blkptr.h>
41 #include <sys/zfeature.h>
42 #include <sys/metaslab_impl.h>
44 #include <sys/trace_zio.h>
48 * ==========================================================================
49 * I/O type descriptions
50 * ==========================================================================
52 const char *zio_type_name
[ZIO_TYPES
] = {
54 * Note: Linux kernel thread name length is limited
55 * so these names will differ from upstream open zfs.
57 "z_null", "z_rd", "z_wr", "z_fr", "z_cl", "z_ioctl"
60 int zio_dva_throttle_enabled
= B_TRUE
;
63 * ==========================================================================
65 * ==========================================================================
67 kmem_cache_t
*zio_cache
;
68 kmem_cache_t
*zio_link_cache
;
69 kmem_cache_t
*zio_buf_cache
[SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
];
70 kmem_cache_t
*zio_data_buf_cache
[SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
];
71 #if defined(ZFS_DEBUG) && !defined(_KERNEL)
72 uint64_t zio_buf_cache_allocs
[SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
];
73 uint64_t zio_buf_cache_frees
[SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
];
76 int zio_delay_max
= ZIO_DELAY_MAX
;
78 #define ZIO_PIPELINE_CONTINUE 0x100
79 #define ZIO_PIPELINE_STOP 0x101
81 #define BP_SPANB(indblkshift, level) \
82 (((uint64_t)1) << ((level) * ((indblkshift) - SPA_BLKPTRSHIFT)))
83 #define COMPARE_META_LEVEL 0x80000000ul
85 * The following actions directly effect the spa's sync-to-convergence logic.
86 * The values below define the sync pass when we start performing the action.
87 * Care should be taken when changing these values as they directly impact
88 * spa_sync() performance. Tuning these values may introduce subtle performance
89 * pathologies and should only be done in the context of performance analysis.
90 * These tunables will eventually be removed and replaced with #defines once
91 * enough analysis has been done to determine optimal values.
93 * The 'zfs_sync_pass_deferred_free' pass must be greater than 1 to ensure that
94 * regular blocks are not deferred.
96 int zfs_sync_pass_deferred_free
= 2; /* defer frees starting in this pass */
97 int zfs_sync_pass_dont_compress
= 5; /* don't compress starting in this pass */
98 int zfs_sync_pass_rewrite
= 2; /* rewrite new bps starting in this pass */
101 * An allocating zio is one that either currently has the DVA allocate
102 * stage set or will have it later in its lifetime.
104 #define IO_IS_ALLOCATING(zio) ((zio)->io_orig_pipeline & ZIO_STAGE_DVA_ALLOCATE)
106 int zio_requeue_io_start_cut_in_line
= 1;
109 int zio_buf_debug_limit
= 16384;
111 int zio_buf_debug_limit
= 0;
114 static inline void __zio_execute(zio_t
*zio
);
116 static void zio_taskq_dispatch(zio_t
*, zio_taskq_type_t
, boolean_t
);
122 vmem_t
*data_alloc_arena
= NULL
;
124 zio_cache
= kmem_cache_create("zio_cache",
125 sizeof (zio_t
), 0, NULL
, NULL
, NULL
, NULL
, NULL
, 0);
126 zio_link_cache
= kmem_cache_create("zio_link_cache",
127 sizeof (zio_link_t
), 0, NULL
, NULL
, NULL
, NULL
, NULL
, 0);
130 * For small buffers, we want a cache for each multiple of
131 * SPA_MINBLOCKSIZE. For larger buffers, we want a cache
132 * for each quarter-power of 2.
134 for (c
= 0; c
< SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
; c
++) {
135 size_t size
= (c
+ 1) << SPA_MINBLOCKSHIFT
;
138 size_t cflags
= (size
> zio_buf_debug_limit
) ? KMC_NODEBUG
: 0;
140 #if defined(_ILP32) && defined(_KERNEL)
142 * Cache size limited to 1M on 32-bit platforms until ARC
143 * buffers no longer require virtual address space.
145 if (size
> zfs_max_recordsize
)
154 * If we are using watchpoints, put each buffer on its own page,
155 * to eliminate the performance overhead of trapping to the
156 * kernel when modifying a non-watched buffer that shares the
157 * page with a watched buffer.
159 if (arc_watch
&& !IS_P2ALIGNED(size
, PAGESIZE
))
162 * Here's the problem - on 4K native devices in userland on
163 * Linux using O_DIRECT, buffers must be 4K aligned or I/O
164 * will fail with EINVAL, causing zdb (and others) to coredump.
165 * Since userland probably doesn't need optimized buffer caches,
166 * we just force 4K alignment on everything.
168 align
= 8 * SPA_MINBLOCKSIZE
;
170 if (size
< PAGESIZE
) {
171 align
= SPA_MINBLOCKSIZE
;
172 } else if (IS_P2ALIGNED(size
, p2
>> 2)) {
179 (void) sprintf(name
, "zio_buf_%lu", (ulong_t
)size
);
180 zio_buf_cache
[c
] = kmem_cache_create(name
, size
,
181 align
, NULL
, NULL
, NULL
, NULL
, NULL
, cflags
);
183 (void) sprintf(name
, "zio_data_buf_%lu", (ulong_t
)size
);
184 zio_data_buf_cache
[c
] = kmem_cache_create(name
, size
,
185 align
, NULL
, NULL
, NULL
, NULL
,
186 data_alloc_arena
, cflags
);
191 ASSERT(zio_buf_cache
[c
] != NULL
);
192 if (zio_buf_cache
[c
- 1] == NULL
)
193 zio_buf_cache
[c
- 1] = zio_buf_cache
[c
];
195 ASSERT(zio_data_buf_cache
[c
] != NULL
);
196 if (zio_data_buf_cache
[c
- 1] == NULL
)
197 zio_data_buf_cache
[c
- 1] = zio_data_buf_cache
[c
];
209 kmem_cache_t
*last_cache
= NULL
;
210 kmem_cache_t
*last_data_cache
= NULL
;
212 for (c
= 0; c
< SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
; c
++) {
215 * Cache size limited to 1M on 32-bit platforms until ARC
216 * buffers no longer require virtual address space.
218 if (((c
+ 1) << SPA_MINBLOCKSHIFT
) > zfs_max_recordsize
)
221 #if defined(ZFS_DEBUG) && !defined(_KERNEL)
222 if (zio_buf_cache_allocs
[c
] != zio_buf_cache_frees
[c
])
223 (void) printf("zio_fini: [%d] %llu != %llu\n",
224 (int)((c
+ 1) << SPA_MINBLOCKSHIFT
),
225 (long long unsigned)zio_buf_cache_allocs
[c
],
226 (long long unsigned)zio_buf_cache_frees
[c
]);
228 if (zio_buf_cache
[c
] != last_cache
) {
229 last_cache
= zio_buf_cache
[c
];
230 kmem_cache_destroy(zio_buf_cache
[c
]);
232 zio_buf_cache
[c
] = NULL
;
234 if (zio_data_buf_cache
[c
] != last_data_cache
) {
235 last_data_cache
= zio_data_buf_cache
[c
];
236 kmem_cache_destroy(zio_data_buf_cache
[c
]);
238 zio_data_buf_cache
[c
] = NULL
;
241 kmem_cache_destroy(zio_link_cache
);
242 kmem_cache_destroy(zio_cache
);
250 * ==========================================================================
251 * Allocate and free I/O buffers
252 * ==========================================================================
256 * Use zio_buf_alloc to allocate ZFS metadata. This data will appear in a
257 * crashdump if the kernel panics, so use it judiciously. Obviously, it's
258 * useful to inspect ZFS metadata, but if possible, we should avoid keeping
259 * excess / transient data in-core during a crashdump.
262 zio_buf_alloc(size_t size
)
264 size_t c
= (size
- 1) >> SPA_MINBLOCKSHIFT
;
266 VERIFY3U(c
, <, SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
);
267 #if defined(ZFS_DEBUG) && !defined(_KERNEL)
268 atomic_add_64(&zio_buf_cache_allocs
[c
], 1);
271 return (kmem_cache_alloc(zio_buf_cache
[c
], KM_PUSHPAGE
));
275 * Use zio_data_buf_alloc to allocate data. The data will not appear in a
276 * crashdump if the kernel panics. This exists so that we will limit the amount
277 * of ZFS data that shows up in a kernel crashdump. (Thus reducing the amount
278 * of kernel heap dumped to disk when the kernel panics)
281 zio_data_buf_alloc(size_t size
)
283 size_t c
= (size
- 1) >> SPA_MINBLOCKSHIFT
;
285 VERIFY3U(c
, <, SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
);
287 return (kmem_cache_alloc(zio_data_buf_cache
[c
], KM_PUSHPAGE
));
291 zio_buf_free(void *buf
, size_t size
)
293 size_t c
= (size
- 1) >> SPA_MINBLOCKSHIFT
;
295 VERIFY3U(c
, <, SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
);
296 #if defined(ZFS_DEBUG) && !defined(_KERNEL)
297 atomic_add_64(&zio_buf_cache_frees
[c
], 1);
300 kmem_cache_free(zio_buf_cache
[c
], buf
);
304 zio_data_buf_free(void *buf
, size_t size
)
306 size_t c
= (size
- 1) >> SPA_MINBLOCKSHIFT
;
308 VERIFY3U(c
, <, SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
);
310 kmem_cache_free(zio_data_buf_cache
[c
], buf
);
314 zio_abd_free(void *abd
, size_t size
)
316 abd_free((abd_t
*)abd
);
320 * ==========================================================================
321 * Push and pop I/O transform buffers
322 * ==========================================================================
325 zio_push_transform(zio_t
*zio
, abd_t
*data
, uint64_t size
, uint64_t bufsize
,
326 zio_transform_func_t
*transform
)
328 zio_transform_t
*zt
= kmem_alloc(sizeof (zio_transform_t
), KM_SLEEP
);
331 * Ensure that anyone expecting this zio to contain a linear ABD isn't
332 * going to get a nasty surprise when they try to access the data.
334 IMPLY(abd_is_linear(zio
->io_abd
), abd_is_linear(data
));
336 zt
->zt_orig_abd
= zio
->io_abd
;
337 zt
->zt_orig_size
= zio
->io_size
;
338 zt
->zt_bufsize
= bufsize
;
339 zt
->zt_transform
= transform
;
341 zt
->zt_next
= zio
->io_transform_stack
;
342 zio
->io_transform_stack
= zt
;
349 zio_pop_transforms(zio_t
*zio
)
353 while ((zt
= zio
->io_transform_stack
) != NULL
) {
354 if (zt
->zt_transform
!= NULL
)
355 zt
->zt_transform(zio
,
356 zt
->zt_orig_abd
, zt
->zt_orig_size
);
358 if (zt
->zt_bufsize
!= 0)
359 abd_free(zio
->io_abd
);
361 zio
->io_abd
= zt
->zt_orig_abd
;
362 zio
->io_size
= zt
->zt_orig_size
;
363 zio
->io_transform_stack
= zt
->zt_next
;
365 kmem_free(zt
, sizeof (zio_transform_t
));
370 * ==========================================================================
371 * I/O transform callbacks for subblocks and decompression
372 * ==========================================================================
375 zio_subblock(zio_t
*zio
, abd_t
*data
, uint64_t size
)
377 ASSERT(zio
->io_size
> size
);
379 if (zio
->io_type
== ZIO_TYPE_READ
)
380 abd_copy(data
, zio
->io_abd
, size
);
384 zio_decompress(zio_t
*zio
, abd_t
*data
, uint64_t size
)
386 if (zio
->io_error
== 0) {
387 void *tmp
= abd_borrow_buf(data
, size
);
388 int ret
= zio_decompress_data(BP_GET_COMPRESS(zio
->io_bp
),
389 zio
->io_abd
, tmp
, zio
->io_size
, size
);
390 abd_return_buf_copy(data
, tmp
, size
);
393 zio
->io_error
= SET_ERROR(EIO
);
398 * ==========================================================================
399 * I/O parent/child relationships and pipeline interlocks
400 * ==========================================================================
403 zio_walk_parents(zio_t
*cio
, zio_link_t
**zl
)
405 list_t
*pl
= &cio
->io_parent_list
;
407 *zl
= (*zl
== NULL
) ? list_head(pl
) : list_next(pl
, *zl
);
411 ASSERT((*zl
)->zl_child
== cio
);
412 return ((*zl
)->zl_parent
);
416 zio_walk_children(zio_t
*pio
, zio_link_t
**zl
)
418 list_t
*cl
= &pio
->io_child_list
;
420 *zl
= (*zl
== NULL
) ? list_head(cl
) : list_next(cl
, *zl
);
424 ASSERT((*zl
)->zl_parent
== pio
);
425 return ((*zl
)->zl_child
);
429 zio_unique_parent(zio_t
*cio
)
431 zio_link_t
*zl
= NULL
;
432 zio_t
*pio
= zio_walk_parents(cio
, &zl
);
434 VERIFY3P(zio_walk_parents(cio
, &zl
), ==, NULL
);
439 zio_add_child(zio_t
*pio
, zio_t
*cio
)
441 zio_link_t
*zl
= kmem_cache_alloc(zio_link_cache
, KM_SLEEP
);
445 * Logical I/Os can have logical, gang, or vdev children.
446 * Gang I/Os can have gang or vdev children.
447 * Vdev I/Os can only have vdev children.
448 * The following ASSERT captures all of these constraints.
450 ASSERT(cio
->io_child_type
<= pio
->io_child_type
);
455 mutex_enter(&cio
->io_lock
);
456 mutex_enter(&pio
->io_lock
);
458 ASSERT(pio
->io_state
[ZIO_WAIT_DONE
] == 0);
460 for (w
= 0; w
< ZIO_WAIT_TYPES
; w
++)
461 pio
->io_children
[cio
->io_child_type
][w
] += !cio
->io_state
[w
];
463 list_insert_head(&pio
->io_child_list
, zl
);
464 list_insert_head(&cio
->io_parent_list
, zl
);
466 pio
->io_child_count
++;
467 cio
->io_parent_count
++;
469 mutex_exit(&pio
->io_lock
);
470 mutex_exit(&cio
->io_lock
);
474 zio_remove_child(zio_t
*pio
, zio_t
*cio
, zio_link_t
*zl
)
476 ASSERT(zl
->zl_parent
== pio
);
477 ASSERT(zl
->zl_child
== cio
);
479 mutex_enter(&cio
->io_lock
);
480 mutex_enter(&pio
->io_lock
);
482 list_remove(&pio
->io_child_list
, zl
);
483 list_remove(&cio
->io_parent_list
, zl
);
485 pio
->io_child_count
--;
486 cio
->io_parent_count
--;
488 mutex_exit(&pio
->io_lock
);
489 mutex_exit(&cio
->io_lock
);
490 kmem_cache_free(zio_link_cache
, zl
);
494 zio_wait_for_children(zio_t
*zio
, enum zio_child child
, enum zio_wait_type wait
)
496 uint64_t *countp
= &zio
->io_children
[child
][wait
];
497 boolean_t waiting
= B_FALSE
;
499 mutex_enter(&zio
->io_lock
);
500 ASSERT(zio
->io_stall
== NULL
);
503 ASSERT3U(zio
->io_stage
, !=, ZIO_STAGE_OPEN
);
504 zio
->io_stall
= countp
;
507 mutex_exit(&zio
->io_lock
);
512 __attribute__((always_inline
))
514 zio_notify_parent(zio_t
*pio
, zio_t
*zio
, enum zio_wait_type wait
)
516 uint64_t *countp
= &pio
->io_children
[zio
->io_child_type
][wait
];
517 int *errorp
= &pio
->io_child_error
[zio
->io_child_type
];
519 mutex_enter(&pio
->io_lock
);
520 if (zio
->io_error
&& !(zio
->io_flags
& ZIO_FLAG_DONT_PROPAGATE
))
521 *errorp
= zio_worst_error(*errorp
, zio
->io_error
);
522 pio
->io_reexecute
|= zio
->io_reexecute
;
523 ASSERT3U(*countp
, >, 0);
527 if (*countp
== 0 && pio
->io_stall
== countp
) {
528 zio_taskq_type_t type
=
529 pio
->io_stage
< ZIO_STAGE_VDEV_IO_START
? ZIO_TASKQ_ISSUE
:
531 pio
->io_stall
= NULL
;
532 mutex_exit(&pio
->io_lock
);
534 * Dispatch the parent zio in its own taskq so that
535 * the child can continue to make progress. This also
536 * prevents overflowing the stack when we have deeply nested
537 * parent-child relationships.
539 zio_taskq_dispatch(pio
, type
, B_FALSE
);
541 mutex_exit(&pio
->io_lock
);
546 zio_inherit_child_errors(zio_t
*zio
, enum zio_child c
)
548 if (zio
->io_child_error
[c
] != 0 && zio
->io_error
== 0)
549 zio
->io_error
= zio
->io_child_error
[c
];
553 zio_bookmark_compare(const void *x1
, const void *x2
)
555 const zio_t
*z1
= x1
;
556 const zio_t
*z2
= x2
;
558 if (z1
->io_bookmark
.zb_objset
< z2
->io_bookmark
.zb_objset
)
560 if (z1
->io_bookmark
.zb_objset
> z2
->io_bookmark
.zb_objset
)
563 if (z1
->io_bookmark
.zb_object
< z2
->io_bookmark
.zb_object
)
565 if (z1
->io_bookmark
.zb_object
> z2
->io_bookmark
.zb_object
)
568 if (z1
->io_bookmark
.zb_level
< z2
->io_bookmark
.zb_level
)
570 if (z1
->io_bookmark
.zb_level
> z2
->io_bookmark
.zb_level
)
573 if (z1
->io_bookmark
.zb_blkid
< z2
->io_bookmark
.zb_blkid
)
575 if (z1
->io_bookmark
.zb_blkid
> z2
->io_bookmark
.zb_blkid
)
587 * ==========================================================================
588 * Create the various types of I/O (read, write, free, etc)
589 * ==========================================================================
592 zio_create(zio_t
*pio
, spa_t
*spa
, uint64_t txg
, const blkptr_t
*bp
,
593 abd_t
*data
, uint64_t lsize
, uint64_t psize
, zio_done_func_t
*done
,
594 void *private, zio_type_t type
, zio_priority_t priority
,
595 enum zio_flag flags
, vdev_t
*vd
, uint64_t offset
,
596 const zbookmark_phys_t
*zb
, enum zio_stage stage
,
597 enum zio_stage pipeline
)
601 ASSERT3U(psize
, <=, SPA_MAXBLOCKSIZE
);
602 ASSERT(P2PHASE(psize
, SPA_MINBLOCKSIZE
) == 0);
603 ASSERT(P2PHASE(offset
, SPA_MINBLOCKSIZE
) == 0);
605 ASSERT(!vd
|| spa_config_held(spa
, SCL_STATE_ALL
, RW_READER
));
606 ASSERT(!bp
|| !(flags
& ZIO_FLAG_CONFIG_WRITER
));
607 ASSERT(vd
|| stage
== ZIO_STAGE_OPEN
);
609 IMPLY(lsize
!= psize
, (flags
& ZIO_FLAG_RAW
) != 0);
611 zio
= kmem_cache_alloc(zio_cache
, KM_SLEEP
);
612 bzero(zio
, sizeof (zio_t
));
614 mutex_init(&zio
->io_lock
, NULL
, MUTEX_NOLOCKDEP
, NULL
);
615 cv_init(&zio
->io_cv
, NULL
, CV_DEFAULT
, NULL
);
617 list_create(&zio
->io_parent_list
, sizeof (zio_link_t
),
618 offsetof(zio_link_t
, zl_parent_node
));
619 list_create(&zio
->io_child_list
, sizeof (zio_link_t
),
620 offsetof(zio_link_t
, zl_child_node
));
621 metaslab_trace_init(&zio
->io_alloc_list
);
624 zio
->io_child_type
= ZIO_CHILD_VDEV
;
625 else if (flags
& ZIO_FLAG_GANG_CHILD
)
626 zio
->io_child_type
= ZIO_CHILD_GANG
;
627 else if (flags
& ZIO_FLAG_DDT_CHILD
)
628 zio
->io_child_type
= ZIO_CHILD_DDT
;
630 zio
->io_child_type
= ZIO_CHILD_LOGICAL
;
633 zio
->io_bp
= (blkptr_t
*)bp
;
634 zio
->io_bp_copy
= *bp
;
635 zio
->io_bp_orig
= *bp
;
636 if (type
!= ZIO_TYPE_WRITE
||
637 zio
->io_child_type
== ZIO_CHILD_DDT
)
638 zio
->io_bp
= &zio
->io_bp_copy
; /* so caller can free */
639 if (zio
->io_child_type
== ZIO_CHILD_LOGICAL
)
640 zio
->io_logical
= zio
;
641 if (zio
->io_child_type
> ZIO_CHILD_GANG
&& BP_IS_GANG(bp
))
642 pipeline
|= ZIO_GANG_STAGES
;
648 zio
->io_private
= private;
650 zio
->io_priority
= priority
;
652 zio
->io_offset
= offset
;
653 zio
->io_orig_abd
= zio
->io_abd
= data
;
654 zio
->io_orig_size
= zio
->io_size
= psize
;
655 zio
->io_lsize
= lsize
;
656 zio
->io_orig_flags
= zio
->io_flags
= flags
;
657 zio
->io_orig_stage
= zio
->io_stage
= stage
;
658 zio
->io_orig_pipeline
= zio
->io_pipeline
= pipeline
;
659 zio
->io_pipeline_trace
= ZIO_STAGE_OPEN
;
661 zio
->io_state
[ZIO_WAIT_READY
] = (stage
>= ZIO_STAGE_READY
);
662 zio
->io_state
[ZIO_WAIT_DONE
] = (stage
>= ZIO_STAGE_DONE
);
665 zio
->io_bookmark
= *zb
;
668 if (zio
->io_logical
== NULL
)
669 zio
->io_logical
= pio
->io_logical
;
670 if (zio
->io_child_type
== ZIO_CHILD_GANG
)
671 zio
->io_gang_leader
= pio
->io_gang_leader
;
672 zio_add_child(pio
, zio
);
675 taskq_init_ent(&zio
->io_tqent
);
681 zio_destroy(zio_t
*zio
)
683 metaslab_trace_fini(&zio
->io_alloc_list
);
684 list_destroy(&zio
->io_parent_list
);
685 list_destroy(&zio
->io_child_list
);
686 mutex_destroy(&zio
->io_lock
);
687 cv_destroy(&zio
->io_cv
);
688 kmem_cache_free(zio_cache
, zio
);
692 zio_null(zio_t
*pio
, spa_t
*spa
, vdev_t
*vd
, zio_done_func_t
*done
,
693 void *private, enum zio_flag flags
)
697 zio
= zio_create(pio
, spa
, 0, NULL
, NULL
, 0, 0, done
, private,
698 ZIO_TYPE_NULL
, ZIO_PRIORITY_NOW
, flags
, vd
, 0, NULL
,
699 ZIO_STAGE_OPEN
, ZIO_INTERLOCK_PIPELINE
);
705 zio_root(spa_t
*spa
, zio_done_func_t
*done
, void *private, enum zio_flag flags
)
707 return (zio_null(NULL
, spa
, NULL
, done
, private, flags
));
711 zfs_blkptr_verify(spa_t
*spa
, const blkptr_t
*bp
)
715 if (!DMU_OT_IS_VALID(BP_GET_TYPE(bp
))) {
716 zfs_panic_recover("blkptr at %p has invalid TYPE %llu",
717 bp
, (longlong_t
)BP_GET_TYPE(bp
));
719 if (BP_GET_CHECKSUM(bp
) >= ZIO_CHECKSUM_FUNCTIONS
||
720 BP_GET_CHECKSUM(bp
) <= ZIO_CHECKSUM_ON
) {
721 zfs_panic_recover("blkptr at %p has invalid CHECKSUM %llu",
722 bp
, (longlong_t
)BP_GET_CHECKSUM(bp
));
724 if (BP_GET_COMPRESS(bp
) >= ZIO_COMPRESS_FUNCTIONS
||
725 BP_GET_COMPRESS(bp
) <= ZIO_COMPRESS_ON
) {
726 zfs_panic_recover("blkptr at %p has invalid COMPRESS %llu",
727 bp
, (longlong_t
)BP_GET_COMPRESS(bp
));
729 if (BP_GET_LSIZE(bp
) > SPA_MAXBLOCKSIZE
) {
730 zfs_panic_recover("blkptr at %p has invalid LSIZE %llu",
731 bp
, (longlong_t
)BP_GET_LSIZE(bp
));
733 if (BP_GET_PSIZE(bp
) > SPA_MAXBLOCKSIZE
) {
734 zfs_panic_recover("blkptr at %p has invalid PSIZE %llu",
735 bp
, (longlong_t
)BP_GET_PSIZE(bp
));
738 if (BP_IS_EMBEDDED(bp
)) {
739 if (BPE_GET_ETYPE(bp
) > NUM_BP_EMBEDDED_TYPES
) {
740 zfs_panic_recover("blkptr at %p has invalid ETYPE %llu",
741 bp
, (longlong_t
)BPE_GET_ETYPE(bp
));
746 * Pool-specific checks.
748 * Note: it would be nice to verify that the blk_birth and
749 * BP_PHYSICAL_BIRTH() are not too large. However, spa_freeze()
750 * allows the birth time of log blocks (and dmu_sync()-ed blocks
751 * that are in the log) to be arbitrarily large.
753 for (i
= 0; i
< BP_GET_NDVAS(bp
); i
++) {
754 uint64_t vdevid
= DVA_GET_VDEV(&bp
->blk_dva
[i
]);
756 uint64_t offset
, asize
;
757 if (vdevid
>= spa
->spa_root_vdev
->vdev_children
) {
758 zfs_panic_recover("blkptr at %p DVA %u has invalid "
760 bp
, i
, (longlong_t
)vdevid
);
763 vd
= spa
->spa_root_vdev
->vdev_child
[vdevid
];
765 zfs_panic_recover("blkptr at %p DVA %u has invalid "
767 bp
, i
, (longlong_t
)vdevid
);
770 if (vd
->vdev_ops
== &vdev_hole_ops
) {
771 zfs_panic_recover("blkptr at %p DVA %u has hole "
773 bp
, i
, (longlong_t
)vdevid
);
776 if (vd
->vdev_ops
== &vdev_missing_ops
) {
778 * "missing" vdevs are valid during import, but we
779 * don't have their detailed info (e.g. asize), so
780 * we can't perform any more checks on them.
784 offset
= DVA_GET_OFFSET(&bp
->blk_dva
[i
]);
785 asize
= DVA_GET_ASIZE(&bp
->blk_dva
[i
]);
787 asize
= vdev_psize_to_asize(vd
, SPA_GANGBLOCKSIZE
);
788 if (offset
+ asize
> vd
->vdev_asize
) {
789 zfs_panic_recover("blkptr at %p DVA %u has invalid "
791 bp
, i
, (longlong_t
)offset
);
797 zio_read(zio_t
*pio
, spa_t
*spa
, const blkptr_t
*bp
,
798 abd_t
*data
, uint64_t size
, zio_done_func_t
*done
, void *private,
799 zio_priority_t priority
, enum zio_flag flags
, const zbookmark_phys_t
*zb
)
803 zfs_blkptr_verify(spa
, bp
);
805 zio
= zio_create(pio
, spa
, BP_PHYSICAL_BIRTH(bp
), bp
,
806 data
, size
, size
, done
, private,
807 ZIO_TYPE_READ
, priority
, flags
, NULL
, 0, zb
,
808 ZIO_STAGE_OPEN
, (flags
& ZIO_FLAG_DDT_CHILD
) ?
809 ZIO_DDT_CHILD_READ_PIPELINE
: ZIO_READ_PIPELINE
);
815 zio_write(zio_t
*pio
, spa_t
*spa
, uint64_t txg
, blkptr_t
*bp
,
816 abd_t
*data
, uint64_t lsize
, uint64_t psize
, const zio_prop_t
*zp
,
817 zio_done_func_t
*ready
, zio_done_func_t
*children_ready
,
818 zio_done_func_t
*physdone
, zio_done_func_t
*done
,
819 void *private, zio_priority_t priority
, enum zio_flag flags
,
820 const zbookmark_phys_t
*zb
)
824 ASSERT(zp
->zp_checksum
>= ZIO_CHECKSUM_OFF
&&
825 zp
->zp_checksum
< ZIO_CHECKSUM_FUNCTIONS
&&
826 zp
->zp_compress
>= ZIO_COMPRESS_OFF
&&
827 zp
->zp_compress
< ZIO_COMPRESS_FUNCTIONS
&&
828 DMU_OT_IS_VALID(zp
->zp_type
) &&
831 zp
->zp_copies
<= spa_max_replication(spa
));
833 zio
= zio_create(pio
, spa
, txg
, bp
, data
, lsize
, psize
, done
, private,
834 ZIO_TYPE_WRITE
, priority
, flags
, NULL
, 0, zb
,
835 ZIO_STAGE_OPEN
, (flags
& ZIO_FLAG_DDT_CHILD
) ?
836 ZIO_DDT_CHILD_WRITE_PIPELINE
: ZIO_WRITE_PIPELINE
);
838 zio
->io_ready
= ready
;
839 zio
->io_children_ready
= children_ready
;
840 zio
->io_physdone
= physdone
;
844 * Data can be NULL if we are going to call zio_write_override() to
845 * provide the already-allocated BP. But we may need the data to
846 * verify a dedup hit (if requested). In this case, don't try to
847 * dedup (just take the already-allocated BP verbatim).
849 if (data
== NULL
&& zio
->io_prop
.zp_dedup_verify
) {
850 zio
->io_prop
.zp_dedup
= zio
->io_prop
.zp_dedup_verify
= B_FALSE
;
857 zio_rewrite(zio_t
*pio
, spa_t
*spa
, uint64_t txg
, blkptr_t
*bp
, abd_t
*data
,
858 uint64_t size
, zio_done_func_t
*done
, void *private,
859 zio_priority_t priority
, enum zio_flag flags
, zbookmark_phys_t
*zb
)
863 zio
= zio_create(pio
, spa
, txg
, bp
, data
, size
, size
, done
, private,
864 ZIO_TYPE_WRITE
, priority
, flags
| ZIO_FLAG_IO_REWRITE
, NULL
, 0, zb
,
865 ZIO_STAGE_OPEN
, ZIO_REWRITE_PIPELINE
);
871 zio_write_override(zio_t
*zio
, blkptr_t
*bp
, int copies
, boolean_t nopwrite
)
873 ASSERT(zio
->io_type
== ZIO_TYPE_WRITE
);
874 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
875 ASSERT(zio
->io_stage
== ZIO_STAGE_OPEN
);
876 ASSERT(zio
->io_txg
== spa_syncing_txg(zio
->io_spa
));
879 * We must reset the io_prop to match the values that existed
880 * when the bp was first written by dmu_sync() keeping in mind
881 * that nopwrite and dedup are mutually exclusive.
883 zio
->io_prop
.zp_dedup
= nopwrite
? B_FALSE
: zio
->io_prop
.zp_dedup
;
884 zio
->io_prop
.zp_nopwrite
= nopwrite
;
885 zio
->io_prop
.zp_copies
= copies
;
886 zio
->io_bp_override
= bp
;
890 zio_free(spa_t
*spa
, uint64_t txg
, const blkptr_t
*bp
)
894 * The check for EMBEDDED is a performance optimization. We
895 * process the free here (by ignoring it) rather than
896 * putting it on the list and then processing it in zio_free_sync().
898 if (BP_IS_EMBEDDED(bp
))
900 metaslab_check_free(spa
, bp
);
903 * Frees that are for the currently-syncing txg, are not going to be
904 * deferred, and which will not need to do a read (i.e. not GANG or
905 * DEDUP), can be processed immediately. Otherwise, put them on the
906 * in-memory list for later processing.
908 if (BP_IS_GANG(bp
) || BP_GET_DEDUP(bp
) ||
909 txg
!= spa
->spa_syncing_txg
||
910 spa_sync_pass(spa
) >= zfs_sync_pass_deferred_free
) {
911 bplist_append(&spa
->spa_free_bplist
[txg
& TXG_MASK
], bp
);
913 VERIFY0(zio_wait(zio_free_sync(NULL
, spa
, txg
, bp
, 0)));
918 zio_free_sync(zio_t
*pio
, spa_t
*spa
, uint64_t txg
, const blkptr_t
*bp
,
922 enum zio_stage stage
= ZIO_FREE_PIPELINE
;
924 ASSERT(!BP_IS_HOLE(bp
));
925 ASSERT(spa_syncing_txg(spa
) == txg
);
926 ASSERT(spa_sync_pass(spa
) < zfs_sync_pass_deferred_free
);
928 if (BP_IS_EMBEDDED(bp
))
929 return (zio_null(pio
, spa
, NULL
, NULL
, NULL
, 0));
931 metaslab_check_free(spa
, bp
);
935 * GANG and DEDUP blocks can induce a read (for the gang block header,
936 * or the DDT), so issue them asynchronously so that this thread is
939 if (BP_IS_GANG(bp
) || BP_GET_DEDUP(bp
))
940 stage
|= ZIO_STAGE_ISSUE_ASYNC
;
942 zio
= zio_create(pio
, spa
, txg
, bp
, NULL
, BP_GET_PSIZE(bp
),
943 BP_GET_PSIZE(bp
), NULL
, NULL
, ZIO_TYPE_FREE
, ZIO_PRIORITY_NOW
,
944 flags
, NULL
, 0, NULL
, ZIO_STAGE_OPEN
, stage
);
950 zio_claim(zio_t
*pio
, spa_t
*spa
, uint64_t txg
, const blkptr_t
*bp
,
951 zio_done_func_t
*done
, void *private, enum zio_flag flags
)
955 dprintf_bp(bp
, "claiming in txg %llu", txg
);
957 if (BP_IS_EMBEDDED(bp
))
958 return (zio_null(pio
, spa
, NULL
, NULL
, NULL
, 0));
961 * A claim is an allocation of a specific block. Claims are needed
962 * to support immediate writes in the intent log. The issue is that
963 * immediate writes contain committed data, but in a txg that was
964 * *not* committed. Upon opening the pool after an unclean shutdown,
965 * the intent log claims all blocks that contain immediate write data
966 * so that the SPA knows they're in use.
968 * All claims *must* be resolved in the first txg -- before the SPA
969 * starts allocating blocks -- so that nothing is allocated twice.
970 * If txg == 0 we just verify that the block is claimable.
972 ASSERT3U(spa
->spa_uberblock
.ub_rootbp
.blk_birth
, <, spa_first_txg(spa
));
973 ASSERT(txg
== spa_first_txg(spa
) || txg
== 0);
974 ASSERT(!BP_GET_DEDUP(bp
) || !spa_writeable(spa
)); /* zdb(1M) */
976 zio
= zio_create(pio
, spa
, txg
, bp
, NULL
, BP_GET_PSIZE(bp
),
977 BP_GET_PSIZE(bp
), done
, private, ZIO_TYPE_CLAIM
, ZIO_PRIORITY_NOW
,
978 flags
, NULL
, 0, NULL
, ZIO_STAGE_OPEN
, ZIO_CLAIM_PIPELINE
);
979 ASSERT0(zio
->io_queued_timestamp
);
985 zio_ioctl(zio_t
*pio
, spa_t
*spa
, vdev_t
*vd
, int cmd
,
986 zio_done_func_t
*done
, void *private, enum zio_flag flags
)
991 if (vd
->vdev_children
== 0) {
992 zio
= zio_create(pio
, spa
, 0, NULL
, NULL
, 0, 0, done
, private,
993 ZIO_TYPE_IOCTL
, ZIO_PRIORITY_NOW
, flags
, vd
, 0, NULL
,
994 ZIO_STAGE_OPEN
, ZIO_IOCTL_PIPELINE
);
998 zio
= zio_null(pio
, spa
, NULL
, NULL
, NULL
, flags
);
1000 for (c
= 0; c
< vd
->vdev_children
; c
++)
1001 zio_nowait(zio_ioctl(zio
, spa
, vd
->vdev_child
[c
], cmd
,
1002 done
, private, flags
));
1009 zio_read_phys(zio_t
*pio
, vdev_t
*vd
, uint64_t offset
, uint64_t size
,
1010 abd_t
*data
, int checksum
, zio_done_func_t
*done
, void *private,
1011 zio_priority_t priority
, enum zio_flag flags
, boolean_t labels
)
1015 ASSERT(vd
->vdev_children
== 0);
1016 ASSERT(!labels
|| offset
+ size
<= VDEV_LABEL_START_SIZE
||
1017 offset
>= vd
->vdev_psize
- VDEV_LABEL_END_SIZE
);
1018 ASSERT3U(offset
+ size
, <=, vd
->vdev_psize
);
1020 zio
= zio_create(pio
, vd
->vdev_spa
, 0, NULL
, data
, size
, size
, done
,
1021 private, ZIO_TYPE_READ
, priority
, flags
| ZIO_FLAG_PHYSICAL
, vd
,
1022 offset
, NULL
, ZIO_STAGE_OPEN
, ZIO_READ_PHYS_PIPELINE
);
1024 zio
->io_prop
.zp_checksum
= checksum
;
1030 zio_write_phys(zio_t
*pio
, vdev_t
*vd
, uint64_t offset
, uint64_t size
,
1031 abd_t
*data
, int checksum
, zio_done_func_t
*done
, void *private,
1032 zio_priority_t priority
, enum zio_flag flags
, boolean_t labels
)
1036 ASSERT(vd
->vdev_children
== 0);
1037 ASSERT(!labels
|| offset
+ size
<= VDEV_LABEL_START_SIZE
||
1038 offset
>= vd
->vdev_psize
- VDEV_LABEL_END_SIZE
);
1039 ASSERT3U(offset
+ size
, <=, vd
->vdev_psize
);
1041 zio
= zio_create(pio
, vd
->vdev_spa
, 0, NULL
, data
, size
, size
, done
,
1042 private, ZIO_TYPE_WRITE
, priority
, flags
| ZIO_FLAG_PHYSICAL
, vd
,
1043 offset
, NULL
, ZIO_STAGE_OPEN
, ZIO_WRITE_PHYS_PIPELINE
);
1045 zio
->io_prop
.zp_checksum
= checksum
;
1047 if (zio_checksum_table
[checksum
].ci_flags
& ZCHECKSUM_FLAG_EMBEDDED
) {
1049 * zec checksums are necessarily destructive -- they modify
1050 * the end of the write buffer to hold the verifier/checksum.
1051 * Therefore, we must make a local copy in case the data is
1052 * being written to multiple places in parallel.
1054 abd_t
*wbuf
= abd_alloc_sametype(data
, size
);
1055 abd_copy(wbuf
, data
, size
);
1057 zio_push_transform(zio
, wbuf
, size
, size
, NULL
);
1064 * Create a child I/O to do some work for us.
1067 zio_vdev_child_io(zio_t
*pio
, blkptr_t
*bp
, vdev_t
*vd
, uint64_t offset
,
1068 abd_t
*data
, uint64_t size
, int type
, zio_priority_t priority
,
1069 enum zio_flag flags
, zio_done_func_t
*done
, void *private)
1071 enum zio_stage pipeline
= ZIO_VDEV_CHILD_PIPELINE
;
1074 ASSERT(vd
->vdev_parent
==
1075 (pio
->io_vd
? pio
->io_vd
: pio
->io_spa
->spa_root_vdev
));
1077 if (type
== ZIO_TYPE_READ
&& bp
!= NULL
) {
1079 * If we have the bp, then the child should perform the
1080 * checksum and the parent need not. This pushes error
1081 * detection as close to the leaves as possible and
1082 * eliminates redundant checksums in the interior nodes.
1084 pipeline
|= ZIO_STAGE_CHECKSUM_VERIFY
;
1085 pio
->io_pipeline
&= ~ZIO_STAGE_CHECKSUM_VERIFY
;
1088 if (vd
->vdev_children
== 0)
1089 offset
+= VDEV_LABEL_START_SIZE
;
1091 flags
|= ZIO_VDEV_CHILD_FLAGS(pio
) | ZIO_FLAG_DONT_PROPAGATE
;
1094 * If we've decided to do a repair, the write is not speculative --
1095 * even if the original read was.
1097 if (flags
& ZIO_FLAG_IO_REPAIR
)
1098 flags
&= ~ZIO_FLAG_SPECULATIVE
;
1101 * If we're creating a child I/O that is not associated with a
1102 * top-level vdev, then the child zio is not an allocating I/O.
1103 * If this is a retried I/O then we ignore it since we will
1104 * have already processed the original allocating I/O.
1106 if (flags
& ZIO_FLAG_IO_ALLOCATING
&&
1107 (vd
!= vd
->vdev_top
|| (flags
& ZIO_FLAG_IO_RETRY
))) {
1108 ASSERTV(metaslab_class_t
*mc
= spa_normal_class(pio
->io_spa
));
1110 ASSERT(mc
->mc_alloc_throttle_enabled
);
1111 ASSERT(type
== ZIO_TYPE_WRITE
);
1112 ASSERT(priority
== ZIO_PRIORITY_ASYNC_WRITE
);
1113 ASSERT(!(flags
& ZIO_FLAG_IO_REPAIR
));
1114 ASSERT(!(pio
->io_flags
& ZIO_FLAG_IO_REWRITE
) ||
1115 pio
->io_child_type
== ZIO_CHILD_GANG
);
1117 flags
&= ~ZIO_FLAG_IO_ALLOCATING
;
1121 zio
= zio_create(pio
, pio
->io_spa
, pio
->io_txg
, bp
, data
, size
, size
,
1122 done
, private, type
, priority
, flags
, vd
, offset
, &pio
->io_bookmark
,
1123 ZIO_STAGE_VDEV_IO_START
>> 1, pipeline
);
1124 ASSERT3U(zio
->io_child_type
, ==, ZIO_CHILD_VDEV
);
1126 zio
->io_physdone
= pio
->io_physdone
;
1127 if (vd
->vdev_ops
->vdev_op_leaf
&& zio
->io_logical
!= NULL
)
1128 zio
->io_logical
->io_phys_children
++;
1134 zio_vdev_delegated_io(vdev_t
*vd
, uint64_t offset
, abd_t
*data
, uint64_t size
,
1135 int type
, zio_priority_t priority
, enum zio_flag flags
,
1136 zio_done_func_t
*done
, void *private)
1140 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
1142 zio
= zio_create(NULL
, vd
->vdev_spa
, 0, NULL
,
1143 data
, size
, size
, done
, private, type
, priority
,
1144 flags
| ZIO_FLAG_CANFAIL
| ZIO_FLAG_DONT_RETRY
| ZIO_FLAG_DELEGATED
,
1146 ZIO_STAGE_VDEV_IO_START
>> 1, ZIO_VDEV_CHILD_PIPELINE
);
1152 zio_flush(zio_t
*zio
, vdev_t
*vd
)
1154 zio_nowait(zio_ioctl(zio
, zio
->io_spa
, vd
, DKIOCFLUSHWRITECACHE
,
1156 ZIO_FLAG_CANFAIL
| ZIO_FLAG_DONT_PROPAGATE
| ZIO_FLAG_DONT_RETRY
));
1160 zio_shrink(zio_t
*zio
, uint64_t size
)
1162 ASSERT(zio
->io_executor
== NULL
);
1163 ASSERT(zio
->io_orig_size
== zio
->io_size
);
1164 ASSERT(size
<= zio
->io_size
);
1167 * We don't shrink for raidz because of problems with the
1168 * reconstruction when reading back less than the block size.
1169 * Note, BP_IS_RAIDZ() assumes no compression.
1171 ASSERT(BP_GET_COMPRESS(zio
->io_bp
) == ZIO_COMPRESS_OFF
);
1172 if (!BP_IS_RAIDZ(zio
->io_bp
)) {
1173 /* we are not doing a raw write */
1174 ASSERT3U(zio
->io_size
, ==, zio
->io_lsize
);
1175 zio
->io_orig_size
= zio
->io_size
= zio
->io_lsize
= size
;
1180 * ==========================================================================
1181 * Prepare to read and write logical blocks
1182 * ==========================================================================
1186 zio_read_bp_init(zio_t
*zio
)
1188 blkptr_t
*bp
= zio
->io_bp
;
1190 if (BP_GET_COMPRESS(bp
) != ZIO_COMPRESS_OFF
&&
1191 zio
->io_child_type
== ZIO_CHILD_LOGICAL
&&
1192 !(zio
->io_flags
& ZIO_FLAG_RAW
)) {
1194 BP_IS_EMBEDDED(bp
) ? BPE_GET_PSIZE(bp
) : BP_GET_PSIZE(bp
);
1195 zio_push_transform(zio
, abd_alloc_sametype(zio
->io_abd
, psize
),
1196 psize
, psize
, zio_decompress
);
1199 if (BP_IS_EMBEDDED(bp
) && BPE_GET_ETYPE(bp
) == BP_EMBEDDED_TYPE_DATA
) {
1200 int psize
= BPE_GET_PSIZE(bp
);
1201 void *data
= abd_borrow_buf(zio
->io_abd
, psize
);
1203 zio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
1204 decode_embedded_bp_compressed(bp
, data
);
1205 abd_return_buf_copy(zio
->io_abd
, data
, psize
);
1207 ASSERT(!BP_IS_EMBEDDED(bp
));
1210 if (!DMU_OT_IS_METADATA(BP_GET_TYPE(bp
)) && BP_GET_LEVEL(bp
) == 0)
1211 zio
->io_flags
|= ZIO_FLAG_DONT_CACHE
;
1213 if (BP_GET_TYPE(bp
) == DMU_OT_DDT_ZAP
)
1214 zio
->io_flags
|= ZIO_FLAG_DONT_CACHE
;
1216 if (BP_GET_DEDUP(bp
) && zio
->io_child_type
== ZIO_CHILD_LOGICAL
)
1217 zio
->io_pipeline
= ZIO_DDT_READ_PIPELINE
;
1219 return (ZIO_PIPELINE_CONTINUE
);
1223 zio_write_bp_init(zio_t
*zio
)
1226 if (!IO_IS_ALLOCATING(zio
))
1227 return (ZIO_PIPELINE_CONTINUE
);
1229 ASSERT(zio
->io_child_type
!= ZIO_CHILD_DDT
);
1231 if (zio
->io_bp_override
) {
1232 blkptr_t
*bp
= zio
->io_bp
;
1233 zio_prop_t
*zp
= &zio
->io_prop
;
1235 ASSERT(bp
->blk_birth
!= zio
->io_txg
);
1236 ASSERT(BP_GET_DEDUP(zio
->io_bp_override
) == 0);
1238 *bp
= *zio
->io_bp_override
;
1239 zio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
1241 if (BP_IS_EMBEDDED(bp
))
1242 return (ZIO_PIPELINE_CONTINUE
);
1245 * If we've been overridden and nopwrite is set then
1246 * set the flag accordingly to indicate that a nopwrite
1247 * has already occurred.
1249 if (!BP_IS_HOLE(bp
) && zp
->zp_nopwrite
) {
1250 ASSERT(!zp
->zp_dedup
);
1251 ASSERT3U(BP_GET_CHECKSUM(bp
), ==, zp
->zp_checksum
);
1252 zio
->io_flags
|= ZIO_FLAG_NOPWRITE
;
1253 return (ZIO_PIPELINE_CONTINUE
);
1256 ASSERT(!zp
->zp_nopwrite
);
1258 if (BP_IS_HOLE(bp
) || !zp
->zp_dedup
)
1259 return (ZIO_PIPELINE_CONTINUE
);
1261 ASSERT((zio_checksum_table
[zp
->zp_checksum
].ci_flags
&
1262 ZCHECKSUM_FLAG_DEDUP
) || zp
->zp_dedup_verify
);
1264 if (BP_GET_CHECKSUM(bp
) == zp
->zp_checksum
) {
1265 BP_SET_DEDUP(bp
, 1);
1266 zio
->io_pipeline
|= ZIO_STAGE_DDT_WRITE
;
1267 return (ZIO_PIPELINE_CONTINUE
);
1271 * We were unable to handle this as an override bp, treat
1272 * it as a regular write I/O.
1274 zio
->io_bp_override
= NULL
;
1275 *bp
= zio
->io_bp_orig
;
1276 zio
->io_pipeline
= zio
->io_orig_pipeline
;
1279 return (ZIO_PIPELINE_CONTINUE
);
1283 zio_write_compress(zio_t
*zio
)
1285 spa_t
*spa
= zio
->io_spa
;
1286 zio_prop_t
*zp
= &zio
->io_prop
;
1287 enum zio_compress compress
= zp
->zp_compress
;
1288 blkptr_t
*bp
= zio
->io_bp
;
1289 uint64_t lsize
= zio
->io_lsize
;
1290 uint64_t psize
= zio
->io_size
;
1293 EQUIV(lsize
!= psize
, (zio
->io_flags
& ZIO_FLAG_RAW
) != 0);
1296 * If our children haven't all reached the ready stage,
1297 * wait for them and then repeat this pipeline stage.
1299 if (zio_wait_for_children(zio
, ZIO_CHILD_GANG
, ZIO_WAIT_READY
) ||
1300 zio_wait_for_children(zio
, ZIO_CHILD_LOGICAL
, ZIO_WAIT_READY
))
1301 return (ZIO_PIPELINE_STOP
);
1303 if (!IO_IS_ALLOCATING(zio
))
1304 return (ZIO_PIPELINE_CONTINUE
);
1306 if (zio
->io_children_ready
!= NULL
) {
1308 * Now that all our children are ready, run the callback
1309 * associated with this zio in case it wants to modify the
1310 * data to be written.
1312 ASSERT3U(zp
->zp_level
, >, 0);
1313 zio
->io_children_ready(zio
);
1316 ASSERT(zio
->io_child_type
!= ZIO_CHILD_DDT
);
1317 ASSERT(zio
->io_bp_override
== NULL
);
1319 if (!BP_IS_HOLE(bp
) && bp
->blk_birth
== zio
->io_txg
) {
1321 * We're rewriting an existing block, which means we're
1322 * working on behalf of spa_sync(). For spa_sync() to
1323 * converge, it must eventually be the case that we don't
1324 * have to allocate new blocks. But compression changes
1325 * the blocksize, which forces a reallocate, and makes
1326 * convergence take longer. Therefore, after the first
1327 * few passes, stop compressing to ensure convergence.
1329 pass
= spa_sync_pass(spa
);
1331 ASSERT(zio
->io_txg
== spa_syncing_txg(spa
));
1332 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
1333 ASSERT(!BP_GET_DEDUP(bp
));
1335 if (pass
>= zfs_sync_pass_dont_compress
)
1336 compress
= ZIO_COMPRESS_OFF
;
1338 /* Make sure someone doesn't change their mind on overwrites */
1339 ASSERT(BP_IS_EMBEDDED(bp
) || MIN(zp
->zp_copies
+ BP_IS_GANG(bp
),
1340 spa_max_replication(spa
)) == BP_GET_NDVAS(bp
));
1343 /* If it's a compressed write that is not raw, compress the buffer. */
1344 if (compress
!= ZIO_COMPRESS_OFF
&& psize
== lsize
) {
1345 void *cbuf
= zio_buf_alloc(lsize
);
1346 psize
= zio_compress_data(compress
, zio
->io_abd
, cbuf
, lsize
);
1347 if (psize
== 0 || psize
== lsize
) {
1348 compress
= ZIO_COMPRESS_OFF
;
1349 zio_buf_free(cbuf
, lsize
);
1350 } else if (!zp
->zp_dedup
&& psize
<= BPE_PAYLOAD_SIZE
&&
1351 zp
->zp_level
== 0 && !DMU_OT_HAS_FILL(zp
->zp_type
) &&
1352 spa_feature_is_enabled(spa
, SPA_FEATURE_EMBEDDED_DATA
)) {
1353 encode_embedded_bp_compressed(bp
,
1354 cbuf
, compress
, lsize
, psize
);
1355 BPE_SET_ETYPE(bp
, BP_EMBEDDED_TYPE_DATA
);
1356 BP_SET_TYPE(bp
, zio
->io_prop
.zp_type
);
1357 BP_SET_LEVEL(bp
, zio
->io_prop
.zp_level
);
1358 zio_buf_free(cbuf
, lsize
);
1359 bp
->blk_birth
= zio
->io_txg
;
1360 zio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
1361 ASSERT(spa_feature_is_active(spa
,
1362 SPA_FEATURE_EMBEDDED_DATA
));
1363 return (ZIO_PIPELINE_CONTINUE
);
1366 * Round up compressed size up to the ashift
1367 * of the smallest-ashift device, and zero the tail.
1368 * This ensures that the compressed size of the BP
1369 * (and thus compressratio property) are correct,
1370 * in that we charge for the padding used to fill out
1375 ASSERT3U(spa
->spa_min_ashift
, >=, SPA_MINBLOCKSHIFT
);
1377 rounded
= (size_t)P2ROUNDUP(psize
,
1378 1ULL << spa
->spa_min_ashift
);
1379 if (rounded
>= lsize
) {
1380 compress
= ZIO_COMPRESS_OFF
;
1381 zio_buf_free(cbuf
, lsize
);
1384 abd_t
*cdata
= abd_get_from_buf(cbuf
, lsize
);
1385 abd_take_ownership_of_buf(cdata
, B_TRUE
);
1386 abd_zero_off(cdata
, psize
, rounded
- psize
);
1388 zio_push_transform(zio
, cdata
,
1389 psize
, lsize
, NULL
);
1394 * We were unable to handle this as an override bp, treat
1395 * it as a regular write I/O.
1397 zio
->io_bp_override
= NULL
;
1398 *bp
= zio
->io_bp_orig
;
1399 zio
->io_pipeline
= zio
->io_orig_pipeline
;
1402 ASSERT3U(psize
, !=, 0);
1407 * The final pass of spa_sync() must be all rewrites, but the first
1408 * few passes offer a trade-off: allocating blocks defers convergence,
1409 * but newly allocated blocks are sequential, so they can be written
1410 * to disk faster. Therefore, we allow the first few passes of
1411 * spa_sync() to allocate new blocks, but force rewrites after that.
1412 * There should only be a handful of blocks after pass 1 in any case.
1414 if (!BP_IS_HOLE(bp
) && bp
->blk_birth
== zio
->io_txg
&&
1415 BP_GET_PSIZE(bp
) == psize
&&
1416 pass
>= zfs_sync_pass_rewrite
) {
1417 enum zio_stage gang_stages
= zio
->io_pipeline
& ZIO_GANG_STAGES
;
1419 zio
->io_pipeline
= ZIO_REWRITE_PIPELINE
| gang_stages
;
1420 zio
->io_flags
|= ZIO_FLAG_IO_REWRITE
;
1423 zio
->io_pipeline
= ZIO_WRITE_PIPELINE
;
1427 if (zio
->io_bp_orig
.blk_birth
!= 0 &&
1428 spa_feature_is_active(spa
, SPA_FEATURE_HOLE_BIRTH
)) {
1429 BP_SET_LSIZE(bp
, lsize
);
1430 BP_SET_TYPE(bp
, zp
->zp_type
);
1431 BP_SET_LEVEL(bp
, zp
->zp_level
);
1432 BP_SET_BIRTH(bp
, zio
->io_txg
, 0);
1434 zio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
1436 ASSERT(zp
->zp_checksum
!= ZIO_CHECKSUM_GANG_HEADER
);
1437 BP_SET_LSIZE(bp
, lsize
);
1438 BP_SET_TYPE(bp
, zp
->zp_type
);
1439 BP_SET_LEVEL(bp
, zp
->zp_level
);
1440 BP_SET_PSIZE(bp
, psize
);
1441 BP_SET_COMPRESS(bp
, compress
);
1442 BP_SET_CHECKSUM(bp
, zp
->zp_checksum
);
1443 BP_SET_DEDUP(bp
, zp
->zp_dedup
);
1444 BP_SET_BYTEORDER(bp
, ZFS_HOST_BYTEORDER
);
1446 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
1447 ASSERT(!(zio
->io_flags
& ZIO_FLAG_IO_REWRITE
));
1448 zio
->io_pipeline
= ZIO_DDT_WRITE_PIPELINE
;
1450 if (zp
->zp_nopwrite
) {
1451 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
1452 ASSERT(!(zio
->io_flags
& ZIO_FLAG_IO_REWRITE
));
1453 zio
->io_pipeline
|= ZIO_STAGE_NOP_WRITE
;
1456 return (ZIO_PIPELINE_CONTINUE
);
1460 zio_free_bp_init(zio_t
*zio
)
1462 blkptr_t
*bp
= zio
->io_bp
;
1464 if (zio
->io_child_type
== ZIO_CHILD_LOGICAL
) {
1465 if (BP_GET_DEDUP(bp
))
1466 zio
->io_pipeline
= ZIO_DDT_FREE_PIPELINE
;
1469 return (ZIO_PIPELINE_CONTINUE
);
1473 * ==========================================================================
1474 * Execute the I/O pipeline
1475 * ==========================================================================
1479 zio_taskq_dispatch(zio_t
*zio
, zio_taskq_type_t q
, boolean_t cutinline
)
1481 spa_t
*spa
= zio
->io_spa
;
1482 zio_type_t t
= zio
->io_type
;
1483 int flags
= (cutinline
? TQ_FRONT
: 0);
1486 * If we're a config writer or a probe, the normal issue and
1487 * interrupt threads may all be blocked waiting for the config lock.
1488 * In this case, select the otherwise-unused taskq for ZIO_TYPE_NULL.
1490 if (zio
->io_flags
& (ZIO_FLAG_CONFIG_WRITER
| ZIO_FLAG_PROBE
))
1494 * A similar issue exists for the L2ARC write thread until L2ARC 2.0.
1496 if (t
== ZIO_TYPE_WRITE
&& zio
->io_vd
&& zio
->io_vd
->vdev_aux
)
1500 * If this is a high priority I/O, then use the high priority taskq if
1503 if (zio
->io_priority
== ZIO_PRIORITY_NOW
&&
1504 spa
->spa_zio_taskq
[t
][q
+ 1].stqs_count
!= 0)
1507 ASSERT3U(q
, <, ZIO_TASKQ_TYPES
);
1510 * NB: We are assuming that the zio can only be dispatched
1511 * to a single taskq at a time. It would be a grievous error
1512 * to dispatch the zio to another taskq at the same time.
1514 ASSERT(taskq_empty_ent(&zio
->io_tqent
));
1515 spa_taskq_dispatch_ent(spa
, t
, q
, (task_func_t
*)zio_execute
, zio
,
1516 flags
, &zio
->io_tqent
);
1520 zio_taskq_member(zio_t
*zio
, zio_taskq_type_t q
)
1522 kthread_t
*executor
= zio
->io_executor
;
1523 spa_t
*spa
= zio
->io_spa
;
1526 for (t
= 0; t
< ZIO_TYPES
; t
++) {
1527 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1529 for (i
= 0; i
< tqs
->stqs_count
; i
++) {
1530 if (taskq_member(tqs
->stqs_taskq
[i
], executor
))
1539 zio_issue_async(zio_t
*zio
)
1541 zio_taskq_dispatch(zio
, ZIO_TASKQ_ISSUE
, B_FALSE
);
1543 return (ZIO_PIPELINE_STOP
);
1547 zio_interrupt(zio_t
*zio
)
1549 zio_taskq_dispatch(zio
, ZIO_TASKQ_INTERRUPT
, B_FALSE
);
1553 zio_delay_interrupt(zio_t
*zio
)
1556 * The timeout_generic() function isn't defined in userspace, so
1557 * rather than trying to implement the function, the zio delay
1558 * functionality has been disabled for userspace builds.
1563 * If io_target_timestamp is zero, then no delay has been registered
1564 * for this IO, thus jump to the end of this function and "skip" the
1565 * delay; issuing it directly to the zio layer.
1567 if (zio
->io_target_timestamp
!= 0) {
1568 hrtime_t now
= gethrtime();
1570 if (now
>= zio
->io_target_timestamp
) {
1572 * This IO has already taken longer than the target
1573 * delay to complete, so we don't want to delay it
1574 * any longer; we "miss" the delay and issue it
1575 * directly to the zio layer. This is likely due to
1576 * the target latency being set to a value less than
1577 * the underlying hardware can satisfy (e.g. delay
1578 * set to 1ms, but the disks take 10ms to complete an
1582 DTRACE_PROBE2(zio__delay__miss
, zio_t
*, zio
,
1588 hrtime_t diff
= zio
->io_target_timestamp
- now
;
1589 clock_t expire_at_tick
= ddi_get_lbolt() +
1592 DTRACE_PROBE3(zio__delay__hit
, zio_t
*, zio
,
1593 hrtime_t
, now
, hrtime_t
, diff
);
1595 if (NSEC_TO_TICK(diff
) == 0) {
1596 /* Our delay is less than a jiffy - just spin */
1597 zfs_sleep_until(zio
->io_target_timestamp
);
1600 * Use taskq_dispatch_delay() in the place of
1601 * OpenZFS's timeout_generic().
1603 tid
= taskq_dispatch_delay(system_taskq
,
1604 (task_func_t
*)zio_interrupt
,
1605 zio
, TQ_NOSLEEP
, expire_at_tick
);
1606 if (tid
== TASKQID_INVALID
) {
1608 * Couldn't allocate a task. Just
1609 * finish the zio without a delay.
1618 DTRACE_PROBE1(zio__delay__skip
, zio_t
*, zio
);
1623 * Execute the I/O pipeline until one of the following occurs:
1624 * (1) the I/O completes; (2) the pipeline stalls waiting for
1625 * dependent child I/Os; (3) the I/O issues, so we're waiting
1626 * for an I/O completion interrupt; (4) the I/O is delegated by
1627 * vdev-level caching or aggregation; (5) the I/O is deferred
1628 * due to vdev-level queueing; (6) the I/O is handed off to
1629 * another thread. In all cases, the pipeline stops whenever
1630 * there's no CPU work; it never burns a thread in cv_wait_io().
1632 * There's no locking on io_stage because there's no legitimate way
1633 * for multiple threads to be attempting to process the same I/O.
1635 static zio_pipe_stage_t
*zio_pipeline
[];
1638 * zio_execute() is a wrapper around the static function
1639 * __zio_execute() so that we can force __zio_execute() to be
1640 * inlined. This reduces stack overhead which is important
1641 * because __zio_execute() is called recursively in several zio
1642 * code paths. zio_execute() itself cannot be inlined because
1643 * it is externally visible.
1646 zio_execute(zio_t
*zio
)
1648 fstrans_cookie_t cookie
;
1650 cookie
= spl_fstrans_mark();
1652 spl_fstrans_unmark(cookie
);
1656 * Used to determine if in the current context the stack is sized large
1657 * enough to allow zio_execute() to be called recursively. A minimum
1658 * stack size of 16K is required to avoid needing to re-dispatch the zio.
1661 zio_execute_stack_check(zio_t
*zio
)
1663 #if !defined(HAVE_LARGE_STACKS)
1664 dsl_pool_t
*dp
= spa_get_dsl(zio
->io_spa
);
1666 /* Executing in txg_sync_thread() context. */
1667 if (dp
&& curthread
== dp
->dp_tx
.tx_sync_thread
)
1670 /* Pool initialization outside of zio_taskq context. */
1671 if (dp
&& spa_is_initializing(dp
->dp_spa
) &&
1672 !zio_taskq_member(zio
, ZIO_TASKQ_ISSUE
) &&
1673 !zio_taskq_member(zio
, ZIO_TASKQ_ISSUE_HIGH
))
1675 #endif /* HAVE_LARGE_STACKS */
1680 __attribute__((always_inline
))
1682 __zio_execute(zio_t
*zio
)
1684 zio
->io_executor
= curthread
;
1686 ASSERT3U(zio
->io_queued_timestamp
, >, 0);
1688 while (zio
->io_stage
< ZIO_STAGE_DONE
) {
1689 enum zio_stage pipeline
= zio
->io_pipeline
;
1690 enum zio_stage stage
= zio
->io_stage
;
1693 ASSERT(!MUTEX_HELD(&zio
->io_lock
));
1694 ASSERT(ISP2(stage
));
1695 ASSERT(zio
->io_stall
== NULL
);
1699 } while ((stage
& pipeline
) == 0);
1701 ASSERT(stage
<= ZIO_STAGE_DONE
);
1704 * If we are in interrupt context and this pipeline stage
1705 * will grab a config lock that is held across I/O,
1706 * or may wait for an I/O that needs an interrupt thread
1707 * to complete, issue async to avoid deadlock.
1709 * For VDEV_IO_START, we cut in line so that the io will
1710 * be sent to disk promptly.
1712 if ((stage
& ZIO_BLOCKING_STAGES
) && zio
->io_vd
== NULL
&&
1713 zio_taskq_member(zio
, ZIO_TASKQ_INTERRUPT
)) {
1714 boolean_t cut
= (stage
== ZIO_STAGE_VDEV_IO_START
) ?
1715 zio_requeue_io_start_cut_in_line
: B_FALSE
;
1716 zio_taskq_dispatch(zio
, ZIO_TASKQ_ISSUE
, cut
);
1721 * If the current context doesn't have large enough stacks
1722 * the zio must be issued asynchronously to prevent overflow.
1724 if (zio_execute_stack_check(zio
)) {
1725 boolean_t cut
= (stage
== ZIO_STAGE_VDEV_IO_START
) ?
1726 zio_requeue_io_start_cut_in_line
: B_FALSE
;
1727 zio_taskq_dispatch(zio
, ZIO_TASKQ_ISSUE
, cut
);
1731 zio
->io_stage
= stage
;
1732 zio
->io_pipeline_trace
|= zio
->io_stage
;
1733 rv
= zio_pipeline
[highbit64(stage
) - 1](zio
);
1735 if (rv
== ZIO_PIPELINE_STOP
)
1738 ASSERT(rv
== ZIO_PIPELINE_CONTINUE
);
1744 * ==========================================================================
1745 * Initiate I/O, either sync or async
1746 * ==========================================================================
1749 zio_wait(zio_t
*zio
)
1753 ASSERT(zio
->io_stage
== ZIO_STAGE_OPEN
);
1754 ASSERT(zio
->io_executor
== NULL
);
1756 zio
->io_waiter
= curthread
;
1757 ASSERT0(zio
->io_queued_timestamp
);
1758 zio
->io_queued_timestamp
= gethrtime();
1762 mutex_enter(&zio
->io_lock
);
1763 while (zio
->io_executor
!= NULL
)
1764 cv_wait_io(&zio
->io_cv
, &zio
->io_lock
);
1765 mutex_exit(&zio
->io_lock
);
1767 error
= zio
->io_error
;
1774 zio_nowait(zio_t
*zio
)
1776 ASSERT(zio
->io_executor
== NULL
);
1778 if (zio
->io_child_type
== ZIO_CHILD_LOGICAL
&&
1779 zio_unique_parent(zio
) == NULL
) {
1783 * This is a logical async I/O with no parent to wait for it.
1784 * We add it to the spa_async_root_zio "Godfather" I/O which
1785 * will ensure they complete prior to unloading the pool.
1787 spa_t
*spa
= zio
->io_spa
;
1789 pio
= spa
->spa_async_zio_root
[CPU_SEQID
];
1792 zio_add_child(pio
, zio
);
1795 ASSERT0(zio
->io_queued_timestamp
);
1796 zio
->io_queued_timestamp
= gethrtime();
1801 * ==========================================================================
1802 * Reexecute or suspend/resume failed I/O
1803 * ==========================================================================
1807 zio_reexecute(zio_t
*pio
)
1809 zio_t
*cio
, *cio_next
;
1811 zio_link_t
*zl
= NULL
;
1813 ASSERT(pio
->io_child_type
== ZIO_CHILD_LOGICAL
);
1814 ASSERT(pio
->io_orig_stage
== ZIO_STAGE_OPEN
);
1815 ASSERT(pio
->io_gang_leader
== NULL
);
1816 ASSERT(pio
->io_gang_tree
== NULL
);
1818 pio
->io_flags
= pio
->io_orig_flags
;
1819 pio
->io_stage
= pio
->io_orig_stage
;
1820 pio
->io_pipeline
= pio
->io_orig_pipeline
;
1821 pio
->io_reexecute
= 0;
1822 pio
->io_flags
|= ZIO_FLAG_REEXECUTED
;
1823 pio
->io_pipeline_trace
= 0;
1825 for (w
= 0; w
< ZIO_WAIT_TYPES
; w
++)
1826 pio
->io_state
[w
] = 0;
1827 for (c
= 0; c
< ZIO_CHILD_TYPES
; c
++)
1828 pio
->io_child_error
[c
] = 0;
1830 if (IO_IS_ALLOCATING(pio
))
1831 BP_ZERO(pio
->io_bp
);
1834 * As we reexecute pio's children, new children could be created.
1835 * New children go to the head of pio's io_child_list, however,
1836 * so we will (correctly) not reexecute them. The key is that
1837 * the remainder of pio's io_child_list, from 'cio_next' onward,
1838 * cannot be affected by any side effects of reexecuting 'cio'.
1840 for (cio
= zio_walk_children(pio
, &zl
); cio
!= NULL
; cio
= cio_next
) {
1841 cio_next
= zio_walk_children(pio
, &zl
);
1842 mutex_enter(&pio
->io_lock
);
1843 for (w
= 0; w
< ZIO_WAIT_TYPES
; w
++)
1844 pio
->io_children
[cio
->io_child_type
][w
]++;
1845 mutex_exit(&pio
->io_lock
);
1850 * Now that all children have been reexecuted, execute the parent.
1851 * We don't reexecute "The Godfather" I/O here as it's the
1852 * responsibility of the caller to wait on it.
1854 if (!(pio
->io_flags
& ZIO_FLAG_GODFATHER
)) {
1855 pio
->io_queued_timestamp
= gethrtime();
1861 zio_suspend(spa_t
*spa
, zio_t
*zio
)
1863 if (spa_get_failmode(spa
) == ZIO_FAILURE_MODE_PANIC
)
1864 fm_panic("Pool '%s' has encountered an uncorrectable I/O "
1865 "failure and the failure mode property for this pool "
1866 "is set to panic.", spa_name(spa
));
1868 cmn_err(CE_WARN
, "Pool '%s' has encountered an uncorrectable I/O "
1869 "failure and has been suspended.\n", spa_name(spa
));
1871 zfs_ereport_post(FM_EREPORT_ZFS_IO_FAILURE
, spa
, NULL
, NULL
, 0, 0);
1873 mutex_enter(&spa
->spa_suspend_lock
);
1875 if (spa
->spa_suspend_zio_root
== NULL
)
1876 spa
->spa_suspend_zio_root
= zio_root(spa
, NULL
, NULL
,
1877 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
1878 ZIO_FLAG_GODFATHER
);
1880 spa
->spa_suspended
= B_TRUE
;
1883 ASSERT(!(zio
->io_flags
& ZIO_FLAG_GODFATHER
));
1884 ASSERT(zio
!= spa
->spa_suspend_zio_root
);
1885 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
1886 ASSERT(zio_unique_parent(zio
) == NULL
);
1887 ASSERT(zio
->io_stage
== ZIO_STAGE_DONE
);
1888 zio_add_child(spa
->spa_suspend_zio_root
, zio
);
1891 mutex_exit(&spa
->spa_suspend_lock
);
1895 zio_resume(spa_t
*spa
)
1900 * Reexecute all previously suspended i/o.
1902 mutex_enter(&spa
->spa_suspend_lock
);
1903 spa
->spa_suspended
= B_FALSE
;
1904 cv_broadcast(&spa
->spa_suspend_cv
);
1905 pio
= spa
->spa_suspend_zio_root
;
1906 spa
->spa_suspend_zio_root
= NULL
;
1907 mutex_exit(&spa
->spa_suspend_lock
);
1913 return (zio_wait(pio
));
1917 zio_resume_wait(spa_t
*spa
)
1919 mutex_enter(&spa
->spa_suspend_lock
);
1920 while (spa_suspended(spa
))
1921 cv_wait(&spa
->spa_suspend_cv
, &spa
->spa_suspend_lock
);
1922 mutex_exit(&spa
->spa_suspend_lock
);
1926 * ==========================================================================
1929 * A gang block is a collection of small blocks that looks to the DMU
1930 * like one large block. When zio_dva_allocate() cannot find a block
1931 * of the requested size, due to either severe fragmentation or the pool
1932 * being nearly full, it calls zio_write_gang_block() to construct the
1933 * block from smaller fragments.
1935 * A gang block consists of a gang header (zio_gbh_phys_t) and up to
1936 * three (SPA_GBH_NBLKPTRS) gang members. The gang header is just like
1937 * an indirect block: it's an array of block pointers. It consumes
1938 * only one sector and hence is allocatable regardless of fragmentation.
1939 * The gang header's bps point to its gang members, which hold the data.
1941 * Gang blocks are self-checksumming, using the bp's <vdev, offset, txg>
1942 * as the verifier to ensure uniqueness of the SHA256 checksum.
1943 * Critically, the gang block bp's blk_cksum is the checksum of the data,
1944 * not the gang header. This ensures that data block signatures (needed for
1945 * deduplication) are independent of how the block is physically stored.
1947 * Gang blocks can be nested: a gang member may itself be a gang block.
1948 * Thus every gang block is a tree in which root and all interior nodes are
1949 * gang headers, and the leaves are normal blocks that contain user data.
1950 * The root of the gang tree is called the gang leader.
1952 * To perform any operation (read, rewrite, free, claim) on a gang block,
1953 * zio_gang_assemble() first assembles the gang tree (minus data leaves)
1954 * in the io_gang_tree field of the original logical i/o by recursively
1955 * reading the gang leader and all gang headers below it. This yields
1956 * an in-core tree containing the contents of every gang header and the
1957 * bps for every constituent of the gang block.
1959 * With the gang tree now assembled, zio_gang_issue() just walks the gang tree
1960 * and invokes a callback on each bp. To free a gang block, zio_gang_issue()
1961 * calls zio_free_gang() -- a trivial wrapper around zio_free() -- for each bp.
1962 * zio_claim_gang() provides a similarly trivial wrapper for zio_claim().
1963 * zio_read_gang() is a wrapper around zio_read() that omits reading gang
1964 * headers, since we already have those in io_gang_tree. zio_rewrite_gang()
1965 * performs a zio_rewrite() of the data or, for gang headers, a zio_rewrite()
1966 * of the gang header plus zio_checksum_compute() of the data to update the
1967 * gang header's blk_cksum as described above.
1969 * The two-phase assemble/issue model solves the problem of partial failure --
1970 * what if you'd freed part of a gang block but then couldn't read the
1971 * gang header for another part? Assembling the entire gang tree first
1972 * ensures that all the necessary gang header I/O has succeeded before
1973 * starting the actual work of free, claim, or write. Once the gang tree
1974 * is assembled, free and claim are in-memory operations that cannot fail.
1976 * In the event that a gang write fails, zio_dva_unallocate() walks the
1977 * gang tree to immediately free (i.e. insert back into the space map)
1978 * everything we've allocated. This ensures that we don't get ENOSPC
1979 * errors during repeated suspend/resume cycles due to a flaky device.
1981 * Gang rewrites only happen during sync-to-convergence. If we can't assemble
1982 * the gang tree, we won't modify the block, so we can safely defer the free
1983 * (knowing that the block is still intact). If we *can* assemble the gang
1984 * tree, then even if some of the rewrites fail, zio_dva_unallocate() will free
1985 * each constituent bp and we can allocate a new block on the next sync pass.
1987 * In all cases, the gang tree allows complete recovery from partial failure.
1988 * ==========================================================================
1992 zio_gang_issue_func_done(zio_t
*zio
)
1994 abd_put(zio
->io_abd
);
1998 zio_read_gang(zio_t
*pio
, blkptr_t
*bp
, zio_gang_node_t
*gn
, abd_t
*data
,
2004 return (zio_read(pio
, pio
->io_spa
, bp
, abd_get_offset(data
, offset
),
2005 BP_GET_PSIZE(bp
), zio_gang_issue_func_done
,
2006 NULL
, pio
->io_priority
, ZIO_GANG_CHILD_FLAGS(pio
),
2007 &pio
->io_bookmark
));
2011 zio_rewrite_gang(zio_t
*pio
, blkptr_t
*bp
, zio_gang_node_t
*gn
, abd_t
*data
,
2018 abd_get_from_buf(gn
->gn_gbh
, SPA_GANGBLOCKSIZE
);
2019 zio
= zio_rewrite(pio
, pio
->io_spa
, pio
->io_txg
, bp
,
2020 gbh_abd
, SPA_GANGBLOCKSIZE
, zio_gang_issue_func_done
, NULL
,
2021 pio
->io_priority
, ZIO_GANG_CHILD_FLAGS(pio
),
2024 * As we rewrite each gang header, the pipeline will compute
2025 * a new gang block header checksum for it; but no one will
2026 * compute a new data checksum, so we do that here. The one
2027 * exception is the gang leader: the pipeline already computed
2028 * its data checksum because that stage precedes gang assembly.
2029 * (Presently, nothing actually uses interior data checksums;
2030 * this is just good hygiene.)
2032 if (gn
!= pio
->io_gang_leader
->io_gang_tree
) {
2033 abd_t
*buf
= abd_get_offset(data
, offset
);
2035 zio_checksum_compute(zio
, BP_GET_CHECKSUM(bp
),
2036 buf
, BP_GET_PSIZE(bp
));
2041 * If we are here to damage data for testing purposes,
2042 * leave the GBH alone so that we can detect the damage.
2044 if (pio
->io_gang_leader
->io_flags
& ZIO_FLAG_INDUCE_DAMAGE
)
2045 zio
->io_pipeline
&= ~ZIO_VDEV_IO_STAGES
;
2047 zio
= zio_rewrite(pio
, pio
->io_spa
, pio
->io_txg
, bp
,
2048 abd_get_offset(data
, offset
), BP_GET_PSIZE(bp
),
2049 zio_gang_issue_func_done
, NULL
, pio
->io_priority
,
2050 ZIO_GANG_CHILD_FLAGS(pio
), &pio
->io_bookmark
);
2058 zio_free_gang(zio_t
*pio
, blkptr_t
*bp
, zio_gang_node_t
*gn
, abd_t
*data
,
2061 return (zio_free_sync(pio
, pio
->io_spa
, pio
->io_txg
, bp
,
2062 ZIO_GANG_CHILD_FLAGS(pio
)));
2067 zio_claim_gang(zio_t
*pio
, blkptr_t
*bp
, zio_gang_node_t
*gn
, abd_t
*data
,
2070 return (zio_claim(pio
, pio
->io_spa
, pio
->io_txg
, bp
,
2071 NULL
, NULL
, ZIO_GANG_CHILD_FLAGS(pio
)));
2074 static zio_gang_issue_func_t
*zio_gang_issue_func
[ZIO_TYPES
] = {
2083 static void zio_gang_tree_assemble_done(zio_t
*zio
);
2085 static zio_gang_node_t
*
2086 zio_gang_node_alloc(zio_gang_node_t
**gnpp
)
2088 zio_gang_node_t
*gn
;
2090 ASSERT(*gnpp
== NULL
);
2092 gn
= kmem_zalloc(sizeof (*gn
), KM_SLEEP
);
2093 gn
->gn_gbh
= zio_buf_alloc(SPA_GANGBLOCKSIZE
);
2100 zio_gang_node_free(zio_gang_node_t
**gnpp
)
2102 zio_gang_node_t
*gn
= *gnpp
;
2105 for (g
= 0; g
< SPA_GBH_NBLKPTRS
; g
++)
2106 ASSERT(gn
->gn_child
[g
] == NULL
);
2108 zio_buf_free(gn
->gn_gbh
, SPA_GANGBLOCKSIZE
);
2109 kmem_free(gn
, sizeof (*gn
));
2114 zio_gang_tree_free(zio_gang_node_t
**gnpp
)
2116 zio_gang_node_t
*gn
= *gnpp
;
2122 for (g
= 0; g
< SPA_GBH_NBLKPTRS
; g
++)
2123 zio_gang_tree_free(&gn
->gn_child
[g
]);
2125 zio_gang_node_free(gnpp
);
2129 zio_gang_tree_assemble(zio_t
*gio
, blkptr_t
*bp
, zio_gang_node_t
**gnpp
)
2131 zio_gang_node_t
*gn
= zio_gang_node_alloc(gnpp
);
2132 abd_t
*gbh_abd
= abd_get_from_buf(gn
->gn_gbh
, SPA_GANGBLOCKSIZE
);
2134 ASSERT(gio
->io_gang_leader
== gio
);
2135 ASSERT(BP_IS_GANG(bp
));
2137 zio_nowait(zio_read(gio
, gio
->io_spa
, bp
, gbh_abd
, SPA_GANGBLOCKSIZE
,
2138 zio_gang_tree_assemble_done
, gn
, gio
->io_priority
,
2139 ZIO_GANG_CHILD_FLAGS(gio
), &gio
->io_bookmark
));
2143 zio_gang_tree_assemble_done(zio_t
*zio
)
2145 zio_t
*gio
= zio
->io_gang_leader
;
2146 zio_gang_node_t
*gn
= zio
->io_private
;
2147 blkptr_t
*bp
= zio
->io_bp
;
2150 ASSERT(gio
== zio_unique_parent(zio
));
2151 ASSERT(zio
->io_child_count
== 0);
2156 /* this ABD was created from a linear buf in zio_gang_tree_assemble */
2157 if (BP_SHOULD_BYTESWAP(bp
))
2158 byteswap_uint64_array(abd_to_buf(zio
->io_abd
), zio
->io_size
);
2160 ASSERT3P(abd_to_buf(zio
->io_abd
), ==, gn
->gn_gbh
);
2161 ASSERT(zio
->io_size
== SPA_GANGBLOCKSIZE
);
2162 ASSERT(gn
->gn_gbh
->zg_tail
.zec_magic
== ZEC_MAGIC
);
2164 abd_put(zio
->io_abd
);
2166 for (g
= 0; g
< SPA_GBH_NBLKPTRS
; g
++) {
2167 blkptr_t
*gbp
= &gn
->gn_gbh
->zg_blkptr
[g
];
2168 if (!BP_IS_GANG(gbp
))
2170 zio_gang_tree_assemble(gio
, gbp
, &gn
->gn_child
[g
]);
2175 zio_gang_tree_issue(zio_t
*pio
, zio_gang_node_t
*gn
, blkptr_t
*bp
, abd_t
*data
,
2178 zio_t
*gio
= pio
->io_gang_leader
;
2182 ASSERT(BP_IS_GANG(bp
) == !!gn
);
2183 ASSERT(BP_GET_CHECKSUM(bp
) == BP_GET_CHECKSUM(gio
->io_bp
));
2184 ASSERT(BP_GET_LSIZE(bp
) == BP_GET_PSIZE(bp
) || gn
== gio
->io_gang_tree
);
2187 * If you're a gang header, your data is in gn->gn_gbh.
2188 * If you're a gang member, your data is in 'data' and gn == NULL.
2190 zio
= zio_gang_issue_func
[gio
->io_type
](pio
, bp
, gn
, data
, offset
);
2193 ASSERT(gn
->gn_gbh
->zg_tail
.zec_magic
== ZEC_MAGIC
);
2195 for (g
= 0; g
< SPA_GBH_NBLKPTRS
; g
++) {
2196 blkptr_t
*gbp
= &gn
->gn_gbh
->zg_blkptr
[g
];
2197 if (BP_IS_HOLE(gbp
))
2199 zio_gang_tree_issue(zio
, gn
->gn_child
[g
], gbp
, data
,
2201 offset
+= BP_GET_PSIZE(gbp
);
2205 if (gn
== gio
->io_gang_tree
)
2206 ASSERT3U(gio
->io_size
, ==, offset
);
2213 zio_gang_assemble(zio_t
*zio
)
2215 blkptr_t
*bp
= zio
->io_bp
;
2217 ASSERT(BP_IS_GANG(bp
) && zio
->io_gang_leader
== NULL
);
2218 ASSERT(zio
->io_child_type
> ZIO_CHILD_GANG
);
2220 zio
->io_gang_leader
= zio
;
2222 zio_gang_tree_assemble(zio
, bp
, &zio
->io_gang_tree
);
2224 return (ZIO_PIPELINE_CONTINUE
);
2228 zio_gang_issue(zio_t
*zio
)
2230 blkptr_t
*bp
= zio
->io_bp
;
2232 if (zio_wait_for_children(zio
, ZIO_CHILD_GANG
, ZIO_WAIT_DONE
))
2233 return (ZIO_PIPELINE_STOP
);
2235 ASSERT(BP_IS_GANG(bp
) && zio
->io_gang_leader
== zio
);
2236 ASSERT(zio
->io_child_type
> ZIO_CHILD_GANG
);
2238 if (zio
->io_child_error
[ZIO_CHILD_GANG
] == 0)
2239 zio_gang_tree_issue(zio
, zio
->io_gang_tree
, bp
, zio
->io_abd
,
2242 zio_gang_tree_free(&zio
->io_gang_tree
);
2244 zio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
2246 return (ZIO_PIPELINE_CONTINUE
);
2250 zio_write_gang_member_ready(zio_t
*zio
)
2252 zio_t
*pio
= zio_unique_parent(zio
);
2253 dva_t
*cdva
= zio
->io_bp
->blk_dva
;
2254 dva_t
*pdva
= pio
->io_bp
->blk_dva
;
2257 ASSERTV(zio_t
*gio
= zio
->io_gang_leader
);
2259 if (BP_IS_HOLE(zio
->io_bp
))
2262 ASSERT(BP_IS_HOLE(&zio
->io_bp_orig
));
2264 ASSERT(zio
->io_child_type
== ZIO_CHILD_GANG
);
2265 ASSERT3U(zio
->io_prop
.zp_copies
, ==, gio
->io_prop
.zp_copies
);
2266 ASSERT3U(zio
->io_prop
.zp_copies
, <=, BP_GET_NDVAS(zio
->io_bp
));
2267 ASSERT3U(pio
->io_prop
.zp_copies
, <=, BP_GET_NDVAS(pio
->io_bp
));
2268 ASSERT3U(BP_GET_NDVAS(zio
->io_bp
), <=, BP_GET_NDVAS(pio
->io_bp
));
2270 mutex_enter(&pio
->io_lock
);
2271 for (d
= 0; d
< BP_GET_NDVAS(zio
->io_bp
); d
++) {
2272 ASSERT(DVA_GET_GANG(&pdva
[d
]));
2273 asize
= DVA_GET_ASIZE(&pdva
[d
]);
2274 asize
+= DVA_GET_ASIZE(&cdva
[d
]);
2275 DVA_SET_ASIZE(&pdva
[d
], asize
);
2277 mutex_exit(&pio
->io_lock
);
2281 zio_write_gang_done(zio_t
*zio
)
2283 abd_put(zio
->io_abd
);
2287 zio_write_gang_block(zio_t
*pio
)
2289 spa_t
*spa
= pio
->io_spa
;
2290 metaslab_class_t
*mc
= spa_normal_class(spa
);
2291 blkptr_t
*bp
= pio
->io_bp
;
2292 zio_t
*gio
= pio
->io_gang_leader
;
2294 zio_gang_node_t
*gn
, **gnpp
;
2295 zio_gbh_phys_t
*gbh
;
2297 uint64_t txg
= pio
->io_txg
;
2298 uint64_t resid
= pio
->io_size
;
2300 int copies
= gio
->io_prop
.zp_copies
;
2301 int gbh_copies
= MIN(copies
+ 1, spa_max_replication(spa
));
2305 int flags
= METASLAB_HINTBP_FAVOR
| METASLAB_GANG_HEADER
;
2306 if (pio
->io_flags
& ZIO_FLAG_IO_ALLOCATING
) {
2307 ASSERT(pio
->io_priority
== ZIO_PRIORITY_ASYNC_WRITE
);
2308 ASSERT(!(pio
->io_flags
& ZIO_FLAG_NODATA
));
2310 flags
|= METASLAB_ASYNC_ALLOC
;
2311 VERIFY(refcount_held(&mc
->mc_alloc_slots
, pio
));
2314 * The logical zio has already placed a reservation for
2315 * 'copies' allocation slots but gang blocks may require
2316 * additional copies. These additional copies
2317 * (i.e. gbh_copies - copies) are guaranteed to succeed
2318 * since metaslab_class_throttle_reserve() always allows
2319 * additional reservations for gang blocks.
2321 VERIFY(metaslab_class_throttle_reserve(mc
, gbh_copies
- copies
,
2325 error
= metaslab_alloc(spa
, mc
, SPA_GANGBLOCKSIZE
,
2326 bp
, gbh_copies
, txg
, pio
== gio
? NULL
: gio
->io_bp
, flags
,
2327 &pio
->io_alloc_list
, pio
);
2329 if (pio
->io_flags
& ZIO_FLAG_IO_ALLOCATING
) {
2330 ASSERT(pio
->io_priority
== ZIO_PRIORITY_ASYNC_WRITE
);
2331 ASSERT(!(pio
->io_flags
& ZIO_FLAG_NODATA
));
2334 * If we failed to allocate the gang block header then
2335 * we remove any additional allocation reservations that
2336 * we placed here. The original reservation will
2337 * be removed when the logical I/O goes to the ready
2340 metaslab_class_throttle_unreserve(mc
,
2341 gbh_copies
- copies
, pio
);
2344 pio
->io_error
= error
;
2345 return (ZIO_PIPELINE_CONTINUE
);
2349 gnpp
= &gio
->io_gang_tree
;
2351 gnpp
= pio
->io_private
;
2352 ASSERT(pio
->io_ready
== zio_write_gang_member_ready
);
2355 gn
= zio_gang_node_alloc(gnpp
);
2357 bzero(gbh
, SPA_GANGBLOCKSIZE
);
2358 gbh_abd
= abd_get_from_buf(gbh
, SPA_GANGBLOCKSIZE
);
2361 * Create the gang header.
2363 zio
= zio_rewrite(pio
, spa
, txg
, bp
, gbh_abd
, SPA_GANGBLOCKSIZE
,
2364 zio_write_gang_done
, NULL
, pio
->io_priority
,
2365 ZIO_GANG_CHILD_FLAGS(pio
), &pio
->io_bookmark
);
2368 * Create and nowait the gang children.
2370 for (g
= 0; resid
!= 0; resid
-= lsize
, g
++) {
2373 lsize
= P2ROUNDUP(resid
/ (SPA_GBH_NBLKPTRS
- g
),
2375 ASSERT(lsize
>= SPA_MINBLOCKSIZE
&& lsize
<= resid
);
2377 zp
.zp_checksum
= gio
->io_prop
.zp_checksum
;
2378 zp
.zp_compress
= ZIO_COMPRESS_OFF
;
2379 zp
.zp_type
= DMU_OT_NONE
;
2381 zp
.zp_copies
= gio
->io_prop
.zp_copies
;
2382 zp
.zp_dedup
= B_FALSE
;
2383 zp
.zp_dedup_verify
= B_FALSE
;
2384 zp
.zp_nopwrite
= B_FALSE
;
2386 cio
= zio_write(zio
, spa
, txg
, &gbh
->zg_blkptr
[g
],
2387 abd_get_offset(pio
->io_abd
, pio
->io_size
- resid
), lsize
,
2388 lsize
, &zp
, zio_write_gang_member_ready
, NULL
, NULL
,
2389 zio_write_gang_done
, &gn
->gn_child
[g
], pio
->io_priority
,
2390 ZIO_GANG_CHILD_FLAGS(pio
), &pio
->io_bookmark
);
2392 if (pio
->io_flags
& ZIO_FLAG_IO_ALLOCATING
) {
2393 ASSERT(pio
->io_priority
== ZIO_PRIORITY_ASYNC_WRITE
);
2394 ASSERT(!(pio
->io_flags
& ZIO_FLAG_NODATA
));
2397 * Gang children won't throttle but we should
2398 * account for their work, so reserve an allocation
2399 * slot for them here.
2401 VERIFY(metaslab_class_throttle_reserve(mc
,
2402 zp
.zp_copies
, cio
, flags
));
2408 * Set pio's pipeline to just wait for zio to finish.
2410 pio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
2413 * We didn't allocate this bp, so make sure it doesn't get unmarked.
2415 pio
->io_flags
&= ~ZIO_FLAG_FASTWRITE
;
2419 return (ZIO_PIPELINE_CONTINUE
);
2423 * The zio_nop_write stage in the pipeline determines if allocating a
2424 * new bp is necessary. The nopwrite feature can handle writes in
2425 * either syncing or open context (i.e. zil writes) and as a result is
2426 * mutually exclusive with dedup.
2428 * By leveraging a cryptographically secure checksum, such as SHA256, we
2429 * can compare the checksums of the new data and the old to determine if
2430 * allocating a new block is required. Note that our requirements for
2431 * cryptographic strength are fairly weak: there can't be any accidental
2432 * hash collisions, but we don't need to be secure against intentional
2433 * (malicious) collisions. To trigger a nopwrite, you have to be able
2434 * to write the file to begin with, and triggering an incorrect (hash
2435 * collision) nopwrite is no worse than simply writing to the file.
2436 * That said, there are no known attacks against the checksum algorithms
2437 * used for nopwrite, assuming that the salt and the checksums
2438 * themselves remain secret.
2441 zio_nop_write(zio_t
*zio
)
2443 blkptr_t
*bp
= zio
->io_bp
;
2444 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
2445 zio_prop_t
*zp
= &zio
->io_prop
;
2447 ASSERT(BP_GET_LEVEL(bp
) == 0);
2448 ASSERT(!(zio
->io_flags
& ZIO_FLAG_IO_REWRITE
));
2449 ASSERT(zp
->zp_nopwrite
);
2450 ASSERT(!zp
->zp_dedup
);
2451 ASSERT(zio
->io_bp_override
== NULL
);
2452 ASSERT(IO_IS_ALLOCATING(zio
));
2455 * Check to see if the original bp and the new bp have matching
2456 * characteristics (i.e. same checksum, compression algorithms, etc).
2457 * If they don't then just continue with the pipeline which will
2458 * allocate a new bp.
2460 if (BP_IS_HOLE(bp_orig
) ||
2461 !(zio_checksum_table
[BP_GET_CHECKSUM(bp
)].ci_flags
&
2462 ZCHECKSUM_FLAG_NOPWRITE
) ||
2463 BP_GET_CHECKSUM(bp
) != BP_GET_CHECKSUM(bp_orig
) ||
2464 BP_GET_COMPRESS(bp
) != BP_GET_COMPRESS(bp_orig
) ||
2465 BP_GET_DEDUP(bp
) != BP_GET_DEDUP(bp_orig
) ||
2466 zp
->zp_copies
!= BP_GET_NDVAS(bp_orig
))
2467 return (ZIO_PIPELINE_CONTINUE
);
2470 * If the checksums match then reset the pipeline so that we
2471 * avoid allocating a new bp and issuing any I/O.
2473 if (ZIO_CHECKSUM_EQUAL(bp
->blk_cksum
, bp_orig
->blk_cksum
)) {
2474 ASSERT(zio_checksum_table
[zp
->zp_checksum
].ci_flags
&
2475 ZCHECKSUM_FLAG_NOPWRITE
);
2476 ASSERT3U(BP_GET_PSIZE(bp
), ==, BP_GET_PSIZE(bp_orig
));
2477 ASSERT3U(BP_GET_LSIZE(bp
), ==, BP_GET_LSIZE(bp_orig
));
2478 ASSERT(zp
->zp_compress
!= ZIO_COMPRESS_OFF
);
2479 ASSERT(bcmp(&bp
->blk_prop
, &bp_orig
->blk_prop
,
2480 sizeof (uint64_t)) == 0);
2483 zio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
2484 zio
->io_flags
|= ZIO_FLAG_NOPWRITE
;
2487 return (ZIO_PIPELINE_CONTINUE
);
2491 * ==========================================================================
2493 * ==========================================================================
2496 zio_ddt_child_read_done(zio_t
*zio
)
2498 blkptr_t
*bp
= zio
->io_bp
;
2499 ddt_entry_t
*dde
= zio
->io_private
;
2501 zio_t
*pio
= zio_unique_parent(zio
);
2503 mutex_enter(&pio
->io_lock
);
2504 ddp
= ddt_phys_select(dde
, bp
);
2505 if (zio
->io_error
== 0)
2506 ddt_phys_clear(ddp
); /* this ddp doesn't need repair */
2508 if (zio
->io_error
== 0 && dde
->dde_repair_abd
== NULL
)
2509 dde
->dde_repair_abd
= zio
->io_abd
;
2511 abd_free(zio
->io_abd
);
2512 mutex_exit(&pio
->io_lock
);
2516 zio_ddt_read_start(zio_t
*zio
)
2518 blkptr_t
*bp
= zio
->io_bp
;
2521 ASSERT(BP_GET_DEDUP(bp
));
2522 ASSERT(BP_GET_PSIZE(bp
) == zio
->io_size
);
2523 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
2525 if (zio
->io_child_error
[ZIO_CHILD_DDT
]) {
2526 ddt_t
*ddt
= ddt_select(zio
->io_spa
, bp
);
2527 ddt_entry_t
*dde
= ddt_repair_start(ddt
, bp
);
2528 ddt_phys_t
*ddp
= dde
->dde_phys
;
2529 ddt_phys_t
*ddp_self
= ddt_phys_select(dde
, bp
);
2532 ASSERT(zio
->io_vsd
== NULL
);
2535 if (ddp_self
== NULL
)
2536 return (ZIO_PIPELINE_CONTINUE
);
2538 for (p
= 0; p
< DDT_PHYS_TYPES
; p
++, ddp
++) {
2539 if (ddp
->ddp_phys_birth
== 0 || ddp
== ddp_self
)
2541 ddt_bp_create(ddt
->ddt_checksum
, &dde
->dde_key
, ddp
,
2543 zio_nowait(zio_read(zio
, zio
->io_spa
, &blk
,
2544 abd_alloc_for_io(zio
->io_size
, B_TRUE
),
2545 zio
->io_size
, zio_ddt_child_read_done
, dde
,
2546 zio
->io_priority
, ZIO_DDT_CHILD_FLAGS(zio
) |
2547 ZIO_FLAG_DONT_PROPAGATE
, &zio
->io_bookmark
));
2549 return (ZIO_PIPELINE_CONTINUE
);
2552 zio_nowait(zio_read(zio
, zio
->io_spa
, bp
,
2553 zio
->io_abd
, zio
->io_size
, NULL
, NULL
, zio
->io_priority
,
2554 ZIO_DDT_CHILD_FLAGS(zio
), &zio
->io_bookmark
));
2556 return (ZIO_PIPELINE_CONTINUE
);
2560 zio_ddt_read_done(zio_t
*zio
)
2562 blkptr_t
*bp
= zio
->io_bp
;
2564 if (zio_wait_for_children(zio
, ZIO_CHILD_DDT
, ZIO_WAIT_DONE
))
2565 return (ZIO_PIPELINE_STOP
);
2567 ASSERT(BP_GET_DEDUP(bp
));
2568 ASSERT(BP_GET_PSIZE(bp
) == zio
->io_size
);
2569 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
2571 if (zio
->io_child_error
[ZIO_CHILD_DDT
]) {
2572 ddt_t
*ddt
= ddt_select(zio
->io_spa
, bp
);
2573 ddt_entry_t
*dde
= zio
->io_vsd
;
2575 ASSERT(spa_load_state(zio
->io_spa
) != SPA_LOAD_NONE
);
2576 return (ZIO_PIPELINE_CONTINUE
);
2579 zio
->io_stage
= ZIO_STAGE_DDT_READ_START
>> 1;
2580 zio_taskq_dispatch(zio
, ZIO_TASKQ_ISSUE
, B_FALSE
);
2581 return (ZIO_PIPELINE_STOP
);
2583 if (dde
->dde_repair_abd
!= NULL
) {
2584 abd_copy(zio
->io_abd
, dde
->dde_repair_abd
,
2586 zio
->io_child_error
[ZIO_CHILD_DDT
] = 0;
2588 ddt_repair_done(ddt
, dde
);
2592 ASSERT(zio
->io_vsd
== NULL
);
2594 return (ZIO_PIPELINE_CONTINUE
);
2598 zio_ddt_collision(zio_t
*zio
, ddt_t
*ddt
, ddt_entry_t
*dde
)
2600 spa_t
*spa
= zio
->io_spa
;
2602 boolean_t do_raw
= !!(zio
->io_flags
& ZIO_FLAG_RAW
);
2604 ASSERT(!(zio
->io_bp_override
&& do_raw
));
2607 * Note: we compare the original data, not the transformed data,
2608 * because when zio->io_bp is an override bp, we will not have
2609 * pushed the I/O transforms. That's an important optimization
2610 * because otherwise we'd compress/encrypt all dmu_sync() data twice.
2611 * However, we should never get a raw, override zio so in these
2612 * cases we can compare the io_data directly. This is useful because
2613 * it allows us to do dedup verification even if we don't have access
2614 * to the original data (for instance, if the encryption keys aren't
2618 for (p
= DDT_PHYS_SINGLE
; p
<= DDT_PHYS_TRIPLE
; p
++) {
2619 zio_t
*lio
= dde
->dde_lead_zio
[p
];
2621 if (lio
!= NULL
&& do_raw
) {
2622 return (lio
->io_size
!= zio
->io_size
||
2623 abd_cmp(zio
->io_abd
, lio
->io_abd
) != 0);
2624 } else if (lio
!= NULL
) {
2625 return (lio
->io_orig_size
!= zio
->io_orig_size
||
2626 abd_cmp(zio
->io_orig_abd
, lio
->io_orig_abd
) != 0);
2630 for (p
= DDT_PHYS_SINGLE
; p
<= DDT_PHYS_TRIPLE
; p
++) {
2631 ddt_phys_t
*ddp
= &dde
->dde_phys
[p
];
2633 if (ddp
->ddp_phys_birth
!= 0 && do_raw
) {
2634 blkptr_t blk
= *zio
->io_bp
;
2639 ddt_bp_fill(ddp
, &blk
, ddp
->ddp_phys_birth
);
2640 psize
= BP_GET_PSIZE(&blk
);
2642 if (psize
!= zio
->io_size
)
2647 tmpabd
= abd_alloc_for_io(psize
, B_TRUE
);
2649 error
= zio_wait(zio_read(NULL
, spa
, &blk
, tmpabd
,
2650 psize
, NULL
, NULL
, ZIO_PRIORITY_SYNC_READ
,
2651 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
2652 ZIO_FLAG_RAW
, &zio
->io_bookmark
));
2655 if (abd_cmp(tmpabd
, zio
->io_abd
) != 0)
2656 error
= SET_ERROR(ENOENT
);
2661 return (error
!= 0);
2662 } else if (ddp
->ddp_phys_birth
!= 0) {
2663 arc_buf_t
*abuf
= NULL
;
2664 arc_flags_t aflags
= ARC_FLAG_WAIT
;
2665 blkptr_t blk
= *zio
->io_bp
;
2668 ddt_bp_fill(ddp
, &blk
, ddp
->ddp_phys_birth
);
2670 if (BP_GET_LSIZE(&blk
) != zio
->io_orig_size
)
2675 error
= arc_read(NULL
, spa
, &blk
,
2676 arc_getbuf_func
, &abuf
, ZIO_PRIORITY_SYNC_READ
,
2677 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
2678 &aflags
, &zio
->io_bookmark
);
2681 if (abd_cmp_buf(zio
->io_orig_abd
, abuf
->b_data
,
2682 zio
->io_orig_size
) != 0)
2683 error
= SET_ERROR(ENOENT
);
2684 arc_buf_destroy(abuf
, &abuf
);
2688 return (error
!= 0);
2696 zio_ddt_child_write_ready(zio_t
*zio
)
2698 int p
= zio
->io_prop
.zp_copies
;
2699 ddt_t
*ddt
= ddt_select(zio
->io_spa
, zio
->io_bp
);
2700 ddt_entry_t
*dde
= zio
->io_private
;
2701 ddt_phys_t
*ddp
= &dde
->dde_phys
[p
];
2710 ASSERT(dde
->dde_lead_zio
[p
] == zio
);
2712 ddt_phys_fill(ddp
, zio
->io_bp
);
2715 while ((pio
= zio_walk_parents(zio
, &zl
)) != NULL
)
2716 ddt_bp_fill(ddp
, pio
->io_bp
, zio
->io_txg
);
2722 zio_ddt_child_write_done(zio_t
*zio
)
2724 int p
= zio
->io_prop
.zp_copies
;
2725 ddt_t
*ddt
= ddt_select(zio
->io_spa
, zio
->io_bp
);
2726 ddt_entry_t
*dde
= zio
->io_private
;
2727 ddt_phys_t
*ddp
= &dde
->dde_phys
[p
];
2731 ASSERT(ddp
->ddp_refcnt
== 0);
2732 ASSERT(dde
->dde_lead_zio
[p
] == zio
);
2733 dde
->dde_lead_zio
[p
] = NULL
;
2735 if (zio
->io_error
== 0) {
2736 zio_link_t
*zl
= NULL
;
2737 while (zio_walk_parents(zio
, &zl
) != NULL
)
2738 ddt_phys_addref(ddp
);
2740 ddt_phys_clear(ddp
);
2747 zio_ddt_ditto_write_done(zio_t
*zio
)
2749 int p
= DDT_PHYS_DITTO
;
2750 blkptr_t
*bp
= zio
->io_bp
;
2751 ddt_t
*ddt
= ddt_select(zio
->io_spa
, bp
);
2752 ddt_entry_t
*dde
= zio
->io_private
;
2753 ddt_phys_t
*ddp
= &dde
->dde_phys
[p
];
2754 ddt_key_t
*ddk
= &dde
->dde_key
;
2755 ASSERTV(zio_prop_t
*zp
= &zio
->io_prop
);
2759 ASSERT(ddp
->ddp_refcnt
== 0);
2760 ASSERT(dde
->dde_lead_zio
[p
] == zio
);
2761 dde
->dde_lead_zio
[p
] = NULL
;
2763 if (zio
->io_error
== 0) {
2764 ASSERT(ZIO_CHECKSUM_EQUAL(bp
->blk_cksum
, ddk
->ddk_cksum
));
2765 ASSERT(zp
->zp_copies
< SPA_DVAS_PER_BP
);
2766 ASSERT(zp
->zp_copies
== BP_GET_NDVAS(bp
) - BP_IS_GANG(bp
));
2767 if (ddp
->ddp_phys_birth
!= 0)
2768 ddt_phys_free(ddt
, ddk
, ddp
, zio
->io_txg
);
2769 ddt_phys_fill(ddp
, bp
);
2776 zio_ddt_write(zio_t
*zio
)
2778 spa_t
*spa
= zio
->io_spa
;
2779 blkptr_t
*bp
= zio
->io_bp
;
2780 uint64_t txg
= zio
->io_txg
;
2781 zio_prop_t
*zp
= &zio
->io_prop
;
2782 int p
= zp
->zp_copies
;
2786 ddt_t
*ddt
= ddt_select(spa
, bp
);
2790 ASSERT(BP_GET_DEDUP(bp
));
2791 ASSERT(BP_GET_CHECKSUM(bp
) == zp
->zp_checksum
);
2792 ASSERT(BP_IS_HOLE(bp
) || zio
->io_bp_override
);
2793 ASSERT(!(zio
->io_bp_override
&& (zio
->io_flags
& ZIO_FLAG_RAW
)));
2796 dde
= ddt_lookup(ddt
, bp
, B_TRUE
);
2797 ddp
= &dde
->dde_phys
[p
];
2799 if (zp
->zp_dedup_verify
&& zio_ddt_collision(zio
, ddt
, dde
)) {
2801 * If we're using a weak checksum, upgrade to a strong checksum
2802 * and try again. If we're already using a strong checksum,
2803 * we can't resolve it, so just convert to an ordinary write.
2804 * (And automatically e-mail a paper to Nature?)
2806 if (!(zio_checksum_table
[zp
->zp_checksum
].ci_flags
&
2807 ZCHECKSUM_FLAG_DEDUP
)) {
2808 zp
->zp_checksum
= spa_dedup_checksum(spa
);
2809 zio_pop_transforms(zio
);
2810 zio
->io_stage
= ZIO_STAGE_OPEN
;
2813 zp
->zp_dedup
= B_FALSE
;
2815 zio
->io_pipeline
= ZIO_WRITE_PIPELINE
;
2817 return (ZIO_PIPELINE_CONTINUE
);
2820 ditto_copies
= ddt_ditto_copies_needed(ddt
, dde
, ddp
);
2821 ASSERT(ditto_copies
< SPA_DVAS_PER_BP
);
2823 if (ditto_copies
> ddt_ditto_copies_present(dde
) &&
2824 dde
->dde_lead_zio
[DDT_PHYS_DITTO
] == NULL
) {
2825 zio_prop_t czp
= *zp
;
2827 czp
.zp_copies
= ditto_copies
;
2830 * If we arrived here with an override bp, we won't have run
2831 * the transform stack, so we won't have the data we need to
2832 * generate a child i/o. So, toss the override bp and restart.
2833 * This is safe, because using the override bp is just an
2834 * optimization; and it's rare, so the cost doesn't matter.
2836 if (zio
->io_bp_override
) {
2837 zio_pop_transforms(zio
);
2838 zio
->io_stage
= ZIO_STAGE_OPEN
;
2839 zio
->io_pipeline
= ZIO_WRITE_PIPELINE
;
2840 zio
->io_bp_override
= NULL
;
2843 return (ZIO_PIPELINE_CONTINUE
);
2846 dio
= zio_write(zio
, spa
, txg
, bp
, zio
->io_orig_abd
,
2847 zio
->io_orig_size
, zio
->io_orig_size
, &czp
, NULL
, NULL
,
2848 NULL
, zio_ddt_ditto_write_done
, dde
, zio
->io_priority
,
2849 ZIO_DDT_CHILD_FLAGS(zio
), &zio
->io_bookmark
);
2851 zio_push_transform(dio
, zio
->io_abd
, zio
->io_size
, 0, NULL
);
2852 dde
->dde_lead_zio
[DDT_PHYS_DITTO
] = dio
;
2855 if (ddp
->ddp_phys_birth
!= 0 || dde
->dde_lead_zio
[p
] != NULL
) {
2856 if (ddp
->ddp_phys_birth
!= 0)
2857 ddt_bp_fill(ddp
, bp
, txg
);
2858 if (dde
->dde_lead_zio
[p
] != NULL
)
2859 zio_add_child(zio
, dde
->dde_lead_zio
[p
]);
2861 ddt_phys_addref(ddp
);
2862 } else if (zio
->io_bp_override
) {
2863 ASSERT(bp
->blk_birth
== txg
);
2864 ASSERT(BP_EQUAL(bp
, zio
->io_bp_override
));
2865 ddt_phys_fill(ddp
, bp
);
2866 ddt_phys_addref(ddp
);
2868 cio
= zio_write(zio
, spa
, txg
, bp
, zio
->io_orig_abd
,
2869 zio
->io_orig_size
, zio
->io_orig_size
, zp
,
2870 zio_ddt_child_write_ready
, NULL
, NULL
,
2871 zio_ddt_child_write_done
, dde
, zio
->io_priority
,
2872 ZIO_DDT_CHILD_FLAGS(zio
), &zio
->io_bookmark
);
2874 zio_push_transform(cio
, zio
->io_abd
, zio
->io_size
, 0, NULL
);
2875 dde
->dde_lead_zio
[p
] = cio
;
2885 return (ZIO_PIPELINE_CONTINUE
);
2888 ddt_entry_t
*freedde
; /* for debugging */
2891 zio_ddt_free(zio_t
*zio
)
2893 spa_t
*spa
= zio
->io_spa
;
2894 blkptr_t
*bp
= zio
->io_bp
;
2895 ddt_t
*ddt
= ddt_select(spa
, bp
);
2899 ASSERT(BP_GET_DEDUP(bp
));
2900 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
2903 freedde
= dde
= ddt_lookup(ddt
, bp
, B_TRUE
);
2905 ddp
= ddt_phys_select(dde
, bp
);
2907 ddt_phys_decref(ddp
);
2911 return (ZIO_PIPELINE_CONTINUE
);
2915 * ==========================================================================
2916 * Allocate and free blocks
2917 * ==========================================================================
2921 zio_io_to_allocate(spa_t
*spa
)
2925 ASSERT(MUTEX_HELD(&spa
->spa_alloc_lock
));
2927 zio
= avl_first(&spa
->spa_alloc_tree
);
2931 ASSERT(IO_IS_ALLOCATING(zio
));
2934 * Try to place a reservation for this zio. If we're unable to
2935 * reserve then we throttle.
2937 if (!metaslab_class_throttle_reserve(spa_normal_class(spa
),
2938 zio
->io_prop
.zp_copies
, zio
, 0)) {
2942 avl_remove(&spa
->spa_alloc_tree
, zio
);
2943 ASSERT3U(zio
->io_stage
, <, ZIO_STAGE_DVA_ALLOCATE
);
2949 zio_dva_throttle(zio_t
*zio
)
2951 spa_t
*spa
= zio
->io_spa
;
2954 if (zio
->io_priority
== ZIO_PRIORITY_SYNC_WRITE
||
2955 !spa_normal_class(zio
->io_spa
)->mc_alloc_throttle_enabled
||
2956 zio
->io_child_type
== ZIO_CHILD_GANG
||
2957 zio
->io_flags
& ZIO_FLAG_NODATA
) {
2958 return (ZIO_PIPELINE_CONTINUE
);
2961 ASSERT(zio
->io_child_type
> ZIO_CHILD_GANG
);
2963 ASSERT3U(zio
->io_queued_timestamp
, >, 0);
2964 ASSERT(zio
->io_stage
== ZIO_STAGE_DVA_THROTTLE
);
2966 mutex_enter(&spa
->spa_alloc_lock
);
2968 ASSERT(zio
->io_type
== ZIO_TYPE_WRITE
);
2969 avl_add(&spa
->spa_alloc_tree
, zio
);
2971 nio
= zio_io_to_allocate(zio
->io_spa
);
2972 mutex_exit(&spa
->spa_alloc_lock
);
2975 return (ZIO_PIPELINE_CONTINUE
);
2978 ASSERT(nio
->io_stage
== ZIO_STAGE_DVA_THROTTLE
);
2980 * We are passing control to a new zio so make sure that
2981 * it is processed by a different thread. We do this to
2982 * avoid stack overflows that can occur when parents are
2983 * throttled and children are making progress. We allow
2984 * it to go to the head of the taskq since it's already
2987 zio_taskq_dispatch(nio
, ZIO_TASKQ_ISSUE
, B_TRUE
);
2989 return (ZIO_PIPELINE_STOP
);
2993 zio_allocate_dispatch(spa_t
*spa
)
2997 mutex_enter(&spa
->spa_alloc_lock
);
2998 zio
= zio_io_to_allocate(spa
);
2999 mutex_exit(&spa
->spa_alloc_lock
);
3003 ASSERT3U(zio
->io_stage
, ==, ZIO_STAGE_DVA_THROTTLE
);
3004 ASSERT0(zio
->io_error
);
3005 zio_taskq_dispatch(zio
, ZIO_TASKQ_ISSUE
, B_TRUE
);
3009 zio_dva_allocate(zio_t
*zio
)
3011 spa_t
*spa
= zio
->io_spa
;
3012 metaslab_class_t
*mc
= spa_normal_class(spa
);
3013 blkptr_t
*bp
= zio
->io_bp
;
3017 if (zio
->io_gang_leader
== NULL
) {
3018 ASSERT(zio
->io_child_type
> ZIO_CHILD_GANG
);
3019 zio
->io_gang_leader
= zio
;
3022 ASSERT(BP_IS_HOLE(bp
));
3023 ASSERT0(BP_GET_NDVAS(bp
));
3024 ASSERT3U(zio
->io_prop
.zp_copies
, >, 0);
3025 ASSERT3U(zio
->io_prop
.zp_copies
, <=, spa_max_replication(spa
));
3026 ASSERT3U(zio
->io_size
, ==, BP_GET_PSIZE(bp
));
3028 flags
|= (zio
->io_flags
& ZIO_FLAG_FASTWRITE
) ? METASLAB_FASTWRITE
: 0;
3029 if (zio
->io_flags
& ZIO_FLAG_NODATA
)
3030 flags
|= METASLAB_DONT_THROTTLE
;
3031 if (zio
->io_flags
& ZIO_FLAG_GANG_CHILD
)
3032 flags
|= METASLAB_GANG_CHILD
;
3033 if (zio
->io_priority
== ZIO_PRIORITY_ASYNC_WRITE
)
3034 flags
|= METASLAB_ASYNC_ALLOC
;
3036 error
= metaslab_alloc(spa
, mc
, zio
->io_size
, bp
,
3037 zio
->io_prop
.zp_copies
, zio
->io_txg
, NULL
, flags
,
3038 &zio
->io_alloc_list
, zio
);
3041 spa_dbgmsg(spa
, "%s: metaslab allocation failure: zio %p, "
3042 "size %llu, error %d", spa_name(spa
), zio
, zio
->io_size
,
3044 if (error
== ENOSPC
&& zio
->io_size
> SPA_MINBLOCKSIZE
)
3045 return (zio_write_gang_block(zio
));
3046 zio
->io_error
= error
;
3049 return (ZIO_PIPELINE_CONTINUE
);
3053 zio_dva_free(zio_t
*zio
)
3055 metaslab_free(zio
->io_spa
, zio
->io_bp
, zio
->io_txg
, B_FALSE
);
3057 return (ZIO_PIPELINE_CONTINUE
);
3061 zio_dva_claim(zio_t
*zio
)
3065 error
= metaslab_claim(zio
->io_spa
, zio
->io_bp
, zio
->io_txg
);
3067 zio
->io_error
= error
;
3069 return (ZIO_PIPELINE_CONTINUE
);
3073 * Undo an allocation. This is used by zio_done() when an I/O fails
3074 * and we want to give back the block we just allocated.
3075 * This handles both normal blocks and gang blocks.
3078 zio_dva_unallocate(zio_t
*zio
, zio_gang_node_t
*gn
, blkptr_t
*bp
)
3082 ASSERT(bp
->blk_birth
== zio
->io_txg
|| BP_IS_HOLE(bp
));
3083 ASSERT(zio
->io_bp_override
== NULL
);
3085 if (!BP_IS_HOLE(bp
))
3086 metaslab_free(zio
->io_spa
, bp
, bp
->blk_birth
, B_TRUE
);
3089 for (g
= 0; g
< SPA_GBH_NBLKPTRS
; g
++) {
3090 zio_dva_unallocate(zio
, gn
->gn_child
[g
],
3091 &gn
->gn_gbh
->zg_blkptr
[g
]);
3097 * Try to allocate an intent log block. Return 0 on success, errno on failure.
3100 zio_alloc_zil(spa_t
*spa
, uint64_t txg
, blkptr_t
*new_bp
, uint64_t size
,
3104 zio_alloc_list_t io_alloc_list
;
3106 ASSERT(txg
> spa_syncing_txg(spa
));
3108 metaslab_trace_init(&io_alloc_list
);
3109 error
= metaslab_alloc(spa
, spa_log_class(spa
), size
, new_bp
, 1,
3110 txg
, NULL
, METASLAB_FASTWRITE
, &io_alloc_list
, NULL
);
3114 error
= metaslab_alloc(spa
, spa_normal_class(spa
), size
,
3115 new_bp
, 1, txg
, NULL
, METASLAB_FASTWRITE
,
3116 &io_alloc_list
, NULL
);
3120 metaslab_trace_fini(&io_alloc_list
);
3123 BP_SET_LSIZE(new_bp
, size
);
3124 BP_SET_PSIZE(new_bp
, size
);
3125 BP_SET_COMPRESS(new_bp
, ZIO_COMPRESS_OFF
);
3126 BP_SET_CHECKSUM(new_bp
,
3127 spa_version(spa
) >= SPA_VERSION_SLIM_ZIL
3128 ? ZIO_CHECKSUM_ZILOG2
: ZIO_CHECKSUM_ZILOG
);
3129 BP_SET_TYPE(new_bp
, DMU_OT_INTENT_LOG
);
3130 BP_SET_LEVEL(new_bp
, 0);
3131 BP_SET_DEDUP(new_bp
, 0);
3132 BP_SET_BYTEORDER(new_bp
, ZFS_HOST_BYTEORDER
);
3139 * Free an intent log block.
3142 zio_free_zil(spa_t
*spa
, uint64_t txg
, blkptr_t
*bp
)
3144 ASSERT(BP_GET_TYPE(bp
) == DMU_OT_INTENT_LOG
);
3145 ASSERT(!BP_IS_GANG(bp
));
3147 zio_free(spa
, txg
, bp
);
3151 * ==========================================================================
3152 * Read and write to physical devices
3153 * ==========================================================================
3158 * Issue an I/O to the underlying vdev. Typically the issue pipeline
3159 * stops after this stage and will resume upon I/O completion.
3160 * However, there are instances where the vdev layer may need to
3161 * continue the pipeline when an I/O was not issued. Since the I/O
3162 * that was sent to the vdev layer might be different than the one
3163 * currently active in the pipeline (see vdev_queue_io()), we explicitly
3164 * force the underlying vdev layers to call either zio_execute() or
3165 * zio_interrupt() to ensure that the pipeline continues with the correct I/O.
3168 zio_vdev_io_start(zio_t
*zio
)
3170 vdev_t
*vd
= zio
->io_vd
;
3172 spa_t
*spa
= zio
->io_spa
;
3176 ASSERT(zio
->io_error
== 0);
3177 ASSERT(zio
->io_child_error
[ZIO_CHILD_VDEV
] == 0);
3180 if (!(zio
->io_flags
& ZIO_FLAG_CONFIG_WRITER
))
3181 spa_config_enter(spa
, SCL_ZIO
, zio
, RW_READER
);
3184 * The mirror_ops handle multiple DVAs in a single BP.
3186 vdev_mirror_ops
.vdev_op_io_start(zio
);
3187 return (ZIO_PIPELINE_STOP
);
3190 ASSERT3P(zio
->io_logical
, !=, zio
);
3193 * We keep track of time-sensitive I/Os so that the scan thread
3194 * can quickly react to certain workloads. In particular, we care
3195 * about non-scrubbing, top-level reads and writes with the following
3197 * - synchronous writes of user data to non-slog devices
3198 * - any reads of user data
3199 * When these conditions are met, adjust the timestamp of spa_last_io
3200 * which allows the scan thread to adjust its workload accordingly.
3202 if (!(zio
->io_flags
& ZIO_FLAG_SCAN_THREAD
) && zio
->io_bp
!= NULL
&&
3203 vd
== vd
->vdev_top
&& !vd
->vdev_islog
&&
3204 zio
->io_bookmark
.zb_objset
!= DMU_META_OBJSET
&&
3205 zio
->io_txg
!= spa_syncing_txg(spa
)) {
3206 uint64_t old
= spa
->spa_last_io
;
3207 uint64_t new = ddi_get_lbolt64();
3209 (void) atomic_cas_64(&spa
->spa_last_io
, old
, new);
3212 align
= 1ULL << vd
->vdev_top
->vdev_ashift
;
3214 if (!(zio
->io_flags
& ZIO_FLAG_PHYSICAL
) &&
3215 P2PHASE(zio
->io_size
, align
) != 0) {
3216 /* Transform logical writes to be a full physical block size. */
3217 uint64_t asize
= P2ROUNDUP(zio
->io_size
, align
);
3218 abd_t
*abuf
= abd_alloc_sametype(zio
->io_abd
, asize
);
3219 ASSERT(vd
== vd
->vdev_top
);
3220 if (zio
->io_type
== ZIO_TYPE_WRITE
) {
3221 abd_copy(abuf
, zio
->io_abd
, zio
->io_size
);
3222 abd_zero_off(abuf
, zio
->io_size
, asize
- zio
->io_size
);
3224 zio_push_transform(zio
, abuf
, asize
, asize
, zio_subblock
);
3228 * If this is not a physical io, make sure that it is properly aligned
3229 * before proceeding.
3231 if (!(zio
->io_flags
& ZIO_FLAG_PHYSICAL
)) {
3232 ASSERT0(P2PHASE(zio
->io_offset
, align
));
3233 ASSERT0(P2PHASE(zio
->io_size
, align
));
3236 * For physical writes, we allow 512b aligned writes and assume
3237 * the device will perform a read-modify-write as necessary.
3239 ASSERT0(P2PHASE(zio
->io_offset
, SPA_MINBLOCKSIZE
));
3240 ASSERT0(P2PHASE(zio
->io_size
, SPA_MINBLOCKSIZE
));
3243 VERIFY(zio
->io_type
!= ZIO_TYPE_WRITE
|| spa_writeable(spa
));
3246 * If this is a repair I/O, and there's no self-healing involved --
3247 * that is, we're just resilvering what we expect to resilver --
3248 * then don't do the I/O unless zio's txg is actually in vd's DTL.
3249 * This prevents spurious resilvering with nested replication.
3250 * For example, given a mirror of mirrors, (A+B)+(C+D), if only
3251 * A is out of date, we'll read from C+D, then use the data to
3252 * resilver A+B -- but we don't actually want to resilver B, just A.
3253 * The top-level mirror has no way to know this, so instead we just
3254 * discard unnecessary repairs as we work our way down the vdev tree.
3255 * The same logic applies to any form of nested replication:
3256 * ditto + mirror, RAID-Z + replacing, etc. This covers them all.
3258 if ((zio
->io_flags
& ZIO_FLAG_IO_REPAIR
) &&
3259 !(zio
->io_flags
& ZIO_FLAG_SELF_HEAL
) &&
3260 zio
->io_txg
!= 0 && /* not a delegated i/o */
3261 !vdev_dtl_contains(vd
, DTL_PARTIAL
, zio
->io_txg
, 1)) {
3262 ASSERT(zio
->io_type
== ZIO_TYPE_WRITE
);
3263 zio_vdev_io_bypass(zio
);
3264 return (ZIO_PIPELINE_CONTINUE
);
3267 if (vd
->vdev_ops
->vdev_op_leaf
&&
3268 (zio
->io_type
== ZIO_TYPE_READ
|| zio
->io_type
== ZIO_TYPE_WRITE
)) {
3270 if (zio
->io_type
== ZIO_TYPE_READ
&& vdev_cache_read(zio
))
3271 return (ZIO_PIPELINE_CONTINUE
);
3273 if ((zio
= vdev_queue_io(zio
)) == NULL
)
3274 return (ZIO_PIPELINE_STOP
);
3276 if (!vdev_accessible(vd
, zio
)) {
3277 zio
->io_error
= SET_ERROR(ENXIO
);
3279 return (ZIO_PIPELINE_STOP
);
3281 zio
->io_delay
= gethrtime();
3284 vd
->vdev_ops
->vdev_op_io_start(zio
);
3285 return (ZIO_PIPELINE_STOP
);
3289 zio_vdev_io_done(zio_t
*zio
)
3291 vdev_t
*vd
= zio
->io_vd
;
3292 vdev_ops_t
*ops
= vd
? vd
->vdev_ops
: &vdev_mirror_ops
;
3293 boolean_t unexpected_error
= B_FALSE
;
3295 if (zio_wait_for_children(zio
, ZIO_CHILD_VDEV
, ZIO_WAIT_DONE
))
3296 return (ZIO_PIPELINE_STOP
);
3298 ASSERT(zio
->io_type
== ZIO_TYPE_READ
|| zio
->io_type
== ZIO_TYPE_WRITE
);
3301 zio
->io_delay
= gethrtime() - zio
->io_delay
;
3303 if (vd
!= NULL
&& vd
->vdev_ops
->vdev_op_leaf
) {
3305 vdev_queue_io_done(zio
);
3307 if (zio
->io_type
== ZIO_TYPE_WRITE
)
3308 vdev_cache_write(zio
);
3310 if (zio_injection_enabled
&& zio
->io_error
== 0)
3311 zio
->io_error
= zio_handle_device_injection(vd
,
3314 if (zio_injection_enabled
&& zio
->io_error
== 0)
3315 zio
->io_error
= zio_handle_label_injection(zio
, EIO
);
3317 if (zio
->io_error
) {
3318 if (!vdev_accessible(vd
, zio
)) {
3319 zio
->io_error
= SET_ERROR(ENXIO
);
3321 unexpected_error
= B_TRUE
;
3326 ops
->vdev_op_io_done(zio
);
3328 if (unexpected_error
)
3329 VERIFY(vdev_probe(vd
, zio
) == NULL
);
3331 return (ZIO_PIPELINE_CONTINUE
);
3335 * For non-raidz ZIOs, we can just copy aside the bad data read from the
3336 * disk, and use that to finish the checksum ereport later.
3339 zio_vsd_default_cksum_finish(zio_cksum_report_t
*zcr
,
3340 const abd_t
*good_buf
)
3342 /* no processing needed */
3343 zfs_ereport_finish_checksum(zcr
, good_buf
, zcr
->zcr_cbdata
, B_FALSE
);
3348 zio_vsd_default_cksum_report(zio_t
*zio
, zio_cksum_report_t
*zcr
, void *ignored
)
3350 void *abd
= abd_alloc_sametype(zio
->io_abd
, zio
->io_size
);
3352 abd_copy(abd
, zio
->io_abd
, zio
->io_size
);
3354 zcr
->zcr_cbinfo
= zio
->io_size
;
3355 zcr
->zcr_cbdata
= abd
;
3356 zcr
->zcr_finish
= zio_vsd_default_cksum_finish
;
3357 zcr
->zcr_free
= zio_abd_free
;
3361 zio_vdev_io_assess(zio_t
*zio
)
3363 vdev_t
*vd
= zio
->io_vd
;
3365 if (zio_wait_for_children(zio
, ZIO_CHILD_VDEV
, ZIO_WAIT_DONE
))
3366 return (ZIO_PIPELINE_STOP
);
3368 if (vd
== NULL
&& !(zio
->io_flags
& ZIO_FLAG_CONFIG_WRITER
))
3369 spa_config_exit(zio
->io_spa
, SCL_ZIO
, zio
);
3371 if (zio
->io_vsd
!= NULL
) {
3372 zio
->io_vsd_ops
->vsd_free(zio
);
3376 if (zio_injection_enabled
&& zio
->io_error
== 0)
3377 zio
->io_error
= zio_handle_fault_injection(zio
, EIO
);
3380 * If the I/O failed, determine whether we should attempt to retry it.
3382 * On retry, we cut in line in the issue queue, since we don't want
3383 * compression/checksumming/etc. work to prevent our (cheap) IO reissue.
3385 if (zio
->io_error
&& vd
== NULL
&&
3386 !(zio
->io_flags
& (ZIO_FLAG_DONT_RETRY
| ZIO_FLAG_IO_RETRY
))) {
3387 ASSERT(!(zio
->io_flags
& ZIO_FLAG_DONT_QUEUE
)); /* not a leaf */
3388 ASSERT(!(zio
->io_flags
& ZIO_FLAG_IO_BYPASS
)); /* not a leaf */
3390 zio
->io_flags
|= ZIO_FLAG_IO_RETRY
|
3391 ZIO_FLAG_DONT_CACHE
| ZIO_FLAG_DONT_AGGREGATE
;
3392 zio
->io_stage
= ZIO_STAGE_VDEV_IO_START
>> 1;
3393 zio_taskq_dispatch(zio
, ZIO_TASKQ_ISSUE
,
3394 zio_requeue_io_start_cut_in_line
);
3395 return (ZIO_PIPELINE_STOP
);
3399 * If we got an error on a leaf device, convert it to ENXIO
3400 * if the device is not accessible at all.
3402 if (zio
->io_error
&& vd
!= NULL
&& vd
->vdev_ops
->vdev_op_leaf
&&
3403 !vdev_accessible(vd
, zio
))
3404 zio
->io_error
= SET_ERROR(ENXIO
);
3407 * If we can't write to an interior vdev (mirror or RAID-Z),
3408 * set vdev_cant_write so that we stop trying to allocate from it.
3410 if (zio
->io_error
== ENXIO
&& zio
->io_type
== ZIO_TYPE_WRITE
&&
3411 vd
!= NULL
&& !vd
->vdev_ops
->vdev_op_leaf
) {
3412 vd
->vdev_cant_write
= B_TRUE
;
3416 * If a cache flush returns ENOTSUP or ENOTTY, we know that no future
3417 * attempts will ever succeed. In this case we set a persistent bit so
3418 * that we don't bother with it in the future.
3420 if ((zio
->io_error
== ENOTSUP
|| zio
->io_error
== ENOTTY
) &&
3421 zio
->io_type
== ZIO_TYPE_IOCTL
&&
3422 zio
->io_cmd
== DKIOCFLUSHWRITECACHE
&& vd
!= NULL
)
3423 vd
->vdev_nowritecache
= B_TRUE
;
3426 zio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
3428 if (vd
!= NULL
&& vd
->vdev_ops
->vdev_op_leaf
&&
3429 zio
->io_physdone
!= NULL
) {
3430 ASSERT(!(zio
->io_flags
& ZIO_FLAG_DELEGATED
));
3431 ASSERT(zio
->io_child_type
== ZIO_CHILD_VDEV
);
3432 zio
->io_physdone(zio
->io_logical
);
3435 return (ZIO_PIPELINE_CONTINUE
);
3439 zio_vdev_io_reissue(zio_t
*zio
)
3441 ASSERT(zio
->io_stage
== ZIO_STAGE_VDEV_IO_START
);
3442 ASSERT(zio
->io_error
== 0);
3444 zio
->io_stage
>>= 1;
3448 zio_vdev_io_redone(zio_t
*zio
)
3450 ASSERT(zio
->io_stage
== ZIO_STAGE_VDEV_IO_DONE
);
3452 zio
->io_stage
>>= 1;
3456 zio_vdev_io_bypass(zio_t
*zio
)
3458 ASSERT(zio
->io_stage
== ZIO_STAGE_VDEV_IO_START
);
3459 ASSERT(zio
->io_error
== 0);
3461 zio
->io_flags
|= ZIO_FLAG_IO_BYPASS
;
3462 zio
->io_stage
= ZIO_STAGE_VDEV_IO_ASSESS
>> 1;
3466 * ==========================================================================
3467 * Generate and verify checksums
3468 * ==========================================================================
3471 zio_checksum_generate(zio_t
*zio
)
3473 blkptr_t
*bp
= zio
->io_bp
;
3474 enum zio_checksum checksum
;
3478 * This is zio_write_phys().
3479 * We're either generating a label checksum, or none at all.
3481 checksum
= zio
->io_prop
.zp_checksum
;
3483 if (checksum
== ZIO_CHECKSUM_OFF
)
3484 return (ZIO_PIPELINE_CONTINUE
);
3486 ASSERT(checksum
== ZIO_CHECKSUM_LABEL
);
3488 if (BP_IS_GANG(bp
) && zio
->io_child_type
== ZIO_CHILD_GANG
) {
3489 ASSERT(!IO_IS_ALLOCATING(zio
));
3490 checksum
= ZIO_CHECKSUM_GANG_HEADER
;
3492 checksum
= BP_GET_CHECKSUM(bp
);
3496 zio_checksum_compute(zio
, checksum
, zio
->io_abd
, zio
->io_size
);
3498 return (ZIO_PIPELINE_CONTINUE
);
3502 zio_checksum_verify(zio_t
*zio
)
3504 zio_bad_cksum_t info
;
3505 blkptr_t
*bp
= zio
->io_bp
;
3508 ASSERT(zio
->io_vd
!= NULL
);
3512 * This is zio_read_phys().
3513 * We're either verifying a label checksum, or nothing at all.
3515 if (zio
->io_prop
.zp_checksum
== ZIO_CHECKSUM_OFF
)
3516 return (ZIO_PIPELINE_CONTINUE
);
3518 ASSERT(zio
->io_prop
.zp_checksum
== ZIO_CHECKSUM_LABEL
);
3521 if ((error
= zio_checksum_error(zio
, &info
)) != 0) {
3522 zio
->io_error
= error
;
3523 if (error
== ECKSUM
&&
3524 !(zio
->io_flags
& ZIO_FLAG_SPECULATIVE
)) {
3525 zfs_ereport_start_checksum(zio
->io_spa
,
3526 zio
->io_vd
, zio
, zio
->io_offset
,
3527 zio
->io_size
, NULL
, &info
);
3531 return (ZIO_PIPELINE_CONTINUE
);
3535 * Called by RAID-Z to ensure we don't compute the checksum twice.
3538 zio_checksum_verified(zio_t
*zio
)
3540 zio
->io_pipeline
&= ~ZIO_STAGE_CHECKSUM_VERIFY
;
3544 * ==========================================================================
3545 * Error rank. Error are ranked in the order 0, ENXIO, ECKSUM, EIO, other.
3546 * An error of 0 indicates success. ENXIO indicates whole-device failure,
3547 * which may be transient (e.g. unplugged) or permament. ECKSUM and EIO
3548 * indicate errors that are specific to one I/O, and most likely permanent.
3549 * Any other error is presumed to be worse because we weren't expecting it.
3550 * ==========================================================================
3553 zio_worst_error(int e1
, int e2
)
3555 static int zio_error_rank
[] = { 0, ENXIO
, ECKSUM
, EIO
};
3558 for (r1
= 0; r1
< sizeof (zio_error_rank
) / sizeof (int); r1
++)
3559 if (e1
== zio_error_rank
[r1
])
3562 for (r2
= 0; r2
< sizeof (zio_error_rank
) / sizeof (int); r2
++)
3563 if (e2
== zio_error_rank
[r2
])
3566 return (r1
> r2
? e1
: e2
);
3570 * ==========================================================================
3572 * ==========================================================================
3575 zio_ready(zio_t
*zio
)
3577 blkptr_t
*bp
= zio
->io_bp
;
3578 zio_t
*pio
, *pio_next
;
3579 zio_link_t
*zl
= NULL
;
3581 if (zio_wait_for_children(zio
, ZIO_CHILD_GANG
, ZIO_WAIT_READY
) ||
3582 zio_wait_for_children(zio
, ZIO_CHILD_DDT
, ZIO_WAIT_READY
))
3583 return (ZIO_PIPELINE_STOP
);
3585 if (zio
->io_ready
) {
3586 ASSERT(IO_IS_ALLOCATING(zio
));
3587 ASSERT(bp
->blk_birth
== zio
->io_txg
|| BP_IS_HOLE(bp
) ||
3588 (zio
->io_flags
& ZIO_FLAG_NOPWRITE
));
3589 ASSERT(zio
->io_children
[ZIO_CHILD_GANG
][ZIO_WAIT_READY
] == 0);
3594 if (bp
!= NULL
&& bp
!= &zio
->io_bp_copy
)
3595 zio
->io_bp_copy
= *bp
;
3597 if (zio
->io_error
!= 0) {
3598 zio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
3600 if (zio
->io_flags
& ZIO_FLAG_IO_ALLOCATING
) {
3601 ASSERT(IO_IS_ALLOCATING(zio
));
3602 ASSERT(zio
->io_priority
== ZIO_PRIORITY_ASYNC_WRITE
);
3604 * We were unable to allocate anything, unreserve and
3605 * issue the next I/O to allocate.
3607 metaslab_class_throttle_unreserve(
3608 spa_normal_class(zio
->io_spa
),
3609 zio
->io_prop
.zp_copies
, zio
);
3610 zio_allocate_dispatch(zio
->io_spa
);
3614 mutex_enter(&zio
->io_lock
);
3615 zio
->io_state
[ZIO_WAIT_READY
] = 1;
3616 pio
= zio_walk_parents(zio
, &zl
);
3617 mutex_exit(&zio
->io_lock
);
3620 * As we notify zio's parents, new parents could be added.
3621 * New parents go to the head of zio's io_parent_list, however,
3622 * so we will (correctly) not notify them. The remainder of zio's
3623 * io_parent_list, from 'pio_next' onward, cannot change because
3624 * all parents must wait for us to be done before they can be done.
3626 for (; pio
!= NULL
; pio
= pio_next
) {
3627 pio_next
= zio_walk_parents(zio
, &zl
);
3628 zio_notify_parent(pio
, zio
, ZIO_WAIT_READY
);
3631 if (zio
->io_flags
& ZIO_FLAG_NODATA
) {
3632 if (BP_IS_GANG(bp
)) {
3633 zio
->io_flags
&= ~ZIO_FLAG_NODATA
;
3635 ASSERT((uintptr_t)zio
->io_abd
< SPA_MAXBLOCKSIZE
);
3636 zio
->io_pipeline
&= ~ZIO_VDEV_IO_STAGES
;
3640 if (zio_injection_enabled
&&
3641 zio
->io_spa
->spa_syncing_txg
== zio
->io_txg
)
3642 zio_handle_ignored_writes(zio
);
3644 return (ZIO_PIPELINE_CONTINUE
);
3648 * Update the allocation throttle accounting.
3651 zio_dva_throttle_done(zio_t
*zio
)
3653 zio_t
*pio
= zio_unique_parent(zio
);
3654 vdev_t
*vd
= zio
->io_vd
;
3655 int flags
= METASLAB_ASYNC_ALLOC
;
3656 ASSERTV(zio_t
*lio
= zio
->io_logical
);
3658 ASSERT3P(zio
->io_bp
, !=, NULL
);
3659 ASSERT3U(zio
->io_type
, ==, ZIO_TYPE_WRITE
);
3660 ASSERT3U(zio
->io_priority
, ==, ZIO_PRIORITY_ASYNC_WRITE
);
3661 ASSERT3U(zio
->io_child_type
, ==, ZIO_CHILD_VDEV
);
3663 ASSERT3P(vd
, ==, vd
->vdev_top
);
3664 ASSERT(zio_injection_enabled
|| !(zio
->io_flags
& ZIO_FLAG_IO_RETRY
));
3665 ASSERT(!(zio
->io_flags
& ZIO_FLAG_IO_REPAIR
));
3666 ASSERT(zio
->io_flags
& ZIO_FLAG_IO_ALLOCATING
);
3667 ASSERT(!(lio
->io_flags
& ZIO_FLAG_IO_REWRITE
));
3668 ASSERT(!(lio
->io_orig_flags
& ZIO_FLAG_NODATA
));
3671 * Parents of gang children can have two flavors -- ones that
3672 * allocated the gang header (will have ZIO_FLAG_IO_REWRITE set)
3673 * and ones that allocated the constituent blocks. The allocation
3674 * throttle needs to know the allocating parent zio so we must find
3677 if (pio
->io_child_type
== ZIO_CHILD_GANG
) {
3679 * If our parent is a rewrite gang child then our grandparent
3680 * would have been the one that performed the allocation.
3682 if (pio
->io_flags
& ZIO_FLAG_IO_REWRITE
)
3683 pio
= zio_unique_parent(pio
);
3684 flags
|= METASLAB_GANG_CHILD
;
3687 ASSERT(IO_IS_ALLOCATING(pio
));
3688 ASSERT3P(zio
, !=, zio
->io_logical
);
3689 ASSERT(zio
->io_logical
!= NULL
);
3690 ASSERT(!(zio
->io_flags
& ZIO_FLAG_IO_REPAIR
));
3691 ASSERT0(zio
->io_flags
& ZIO_FLAG_NOPWRITE
);
3693 mutex_enter(&pio
->io_lock
);
3694 metaslab_group_alloc_decrement(zio
->io_spa
, vd
->vdev_id
, pio
, flags
);
3695 mutex_exit(&pio
->io_lock
);
3697 metaslab_class_throttle_unreserve(spa_normal_class(zio
->io_spa
),
3701 * Call into the pipeline to see if there is more work that
3702 * needs to be done. If there is work to be done it will be
3703 * dispatched to another taskq thread.
3705 zio_allocate_dispatch(zio
->io_spa
);
3709 zio_done(zio_t
*zio
)
3712 * Always attempt to keep stack usage minimal here since
3713 * we can be called recurisvely up to 19 levels deep.
3715 const uint64_t psize
= zio
->io_size
;
3716 zio_t
*pio
, *pio_next
;
3718 zio_link_t
*zl
= NULL
;
3721 * If our children haven't all completed,
3722 * wait for them and then repeat this pipeline stage.
3724 if (zio_wait_for_children(zio
, ZIO_CHILD_VDEV
, ZIO_WAIT_DONE
) ||
3725 zio_wait_for_children(zio
, ZIO_CHILD_GANG
, ZIO_WAIT_DONE
) ||
3726 zio_wait_for_children(zio
, ZIO_CHILD_DDT
, ZIO_WAIT_DONE
) ||
3727 zio_wait_for_children(zio
, ZIO_CHILD_LOGICAL
, ZIO_WAIT_DONE
))
3728 return (ZIO_PIPELINE_STOP
);
3731 * If the allocation throttle is enabled, then update the accounting.
3732 * We only track child I/Os that are part of an allocating async
3733 * write. We must do this since the allocation is performed
3734 * by the logical I/O but the actual write is done by child I/Os.
3736 if (zio
->io_flags
& ZIO_FLAG_IO_ALLOCATING
&&
3737 zio
->io_child_type
== ZIO_CHILD_VDEV
) {
3738 ASSERT(spa_normal_class(
3739 zio
->io_spa
)->mc_alloc_throttle_enabled
);
3740 zio_dva_throttle_done(zio
);
3744 * If the allocation throttle is enabled, verify that
3745 * we have decremented the refcounts for every I/O that was throttled.
3747 if (zio
->io_flags
& ZIO_FLAG_IO_ALLOCATING
) {
3748 ASSERT(zio
->io_type
== ZIO_TYPE_WRITE
);
3749 ASSERT(zio
->io_priority
== ZIO_PRIORITY_ASYNC_WRITE
);
3750 ASSERT(zio
->io_bp
!= NULL
);
3751 metaslab_group_alloc_verify(zio
->io_spa
, zio
->io_bp
, zio
);
3752 VERIFY(refcount_not_held(
3753 &(spa_normal_class(zio
->io_spa
)->mc_alloc_slots
), zio
));
3757 for (c
= 0; c
< ZIO_CHILD_TYPES
; c
++)
3758 for (w
= 0; w
< ZIO_WAIT_TYPES
; w
++)
3759 ASSERT(zio
->io_children
[c
][w
] == 0);
3761 if (zio
->io_bp
!= NULL
&& !BP_IS_EMBEDDED(zio
->io_bp
)) {
3762 ASSERT(zio
->io_bp
->blk_pad
[0] == 0);
3763 ASSERT(zio
->io_bp
->blk_pad
[1] == 0);
3764 ASSERT(bcmp(zio
->io_bp
, &zio
->io_bp_copy
,
3765 sizeof (blkptr_t
)) == 0 ||
3766 (zio
->io_bp
== zio_unique_parent(zio
)->io_bp
));
3767 if (zio
->io_type
== ZIO_TYPE_WRITE
&& !BP_IS_HOLE(zio
->io_bp
) &&
3768 zio
->io_bp_override
== NULL
&&
3769 !(zio
->io_flags
& ZIO_FLAG_IO_REPAIR
)) {
3770 ASSERT(!BP_SHOULD_BYTESWAP(zio
->io_bp
));
3771 ASSERT3U(zio
->io_prop
.zp_copies
, <=,
3772 BP_GET_NDVAS(zio
->io_bp
));
3773 ASSERT(BP_COUNT_GANG(zio
->io_bp
) == 0 ||
3774 (BP_COUNT_GANG(zio
->io_bp
) ==
3775 BP_GET_NDVAS(zio
->io_bp
)));
3777 if (zio
->io_flags
& ZIO_FLAG_NOPWRITE
)
3778 VERIFY(BP_EQUAL(zio
->io_bp
, &zio
->io_bp_orig
));
3782 * If there were child vdev/gang/ddt errors, they apply to us now.
3784 zio_inherit_child_errors(zio
, ZIO_CHILD_VDEV
);
3785 zio_inherit_child_errors(zio
, ZIO_CHILD_GANG
);
3786 zio_inherit_child_errors(zio
, ZIO_CHILD_DDT
);
3789 * If the I/O on the transformed data was successful, generate any
3790 * checksum reports now while we still have the transformed data.
3792 if (zio
->io_error
== 0) {
3793 while (zio
->io_cksum_report
!= NULL
) {
3794 zio_cksum_report_t
*zcr
= zio
->io_cksum_report
;
3795 uint64_t align
= zcr
->zcr_align
;
3796 uint64_t asize
= P2ROUNDUP(psize
, align
);
3797 abd_t
*adata
= zio
->io_abd
;
3799 if (asize
!= psize
) {
3800 adata
= abd_alloc(asize
, B_TRUE
);
3801 abd_copy(adata
, zio
->io_abd
, psize
);
3802 abd_zero_off(adata
, psize
, asize
- psize
);
3805 zio
->io_cksum_report
= zcr
->zcr_next
;
3806 zcr
->zcr_next
= NULL
;
3807 zcr
->zcr_finish(zcr
, adata
);
3808 zfs_ereport_free_checksum(zcr
);
3815 zio_pop_transforms(zio
); /* note: may set zio->io_error */
3817 vdev_stat_update(zio
, psize
);
3820 * If this I/O is attached to a particular vdev is slow, exceeding
3821 * 30 seconds to complete, post an error described the I/O delay.
3822 * We ignore these errors if the device is currently unavailable.
3824 if (zio
->io_delay
>= MSEC2NSEC(zio_delay_max
)) {
3825 if (zio
->io_vd
!= NULL
&& !vdev_is_dead(zio
->io_vd
))
3826 zfs_ereport_post(FM_EREPORT_ZFS_DELAY
, zio
->io_spa
,
3827 zio
->io_vd
, zio
, 0, 0);
3830 if (zio
->io_error
) {
3832 * If this I/O is attached to a particular vdev,
3833 * generate an error message describing the I/O failure
3834 * at the block level. We ignore these errors if the
3835 * device is currently unavailable.
3837 if (zio
->io_error
!= ECKSUM
&& zio
->io_vd
!= NULL
&&
3838 !vdev_is_dead(zio
->io_vd
))
3839 zfs_ereport_post(FM_EREPORT_ZFS_IO
, zio
->io_spa
,
3840 zio
->io_vd
, zio
, 0, 0);
3842 if ((zio
->io_error
== EIO
|| !(zio
->io_flags
&
3843 (ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_DONT_PROPAGATE
))) &&
3844 zio
== zio
->io_logical
) {
3846 * For logical I/O requests, tell the SPA to log the
3847 * error and generate a logical data ereport.
3849 spa_log_error(zio
->io_spa
, zio
);
3850 zfs_ereport_post(FM_EREPORT_ZFS_DATA
, zio
->io_spa
,
3855 if (zio
->io_error
&& zio
== zio
->io_logical
) {
3857 * Determine whether zio should be reexecuted. This will
3858 * propagate all the way to the root via zio_notify_parent().
3860 ASSERT(zio
->io_vd
== NULL
&& zio
->io_bp
!= NULL
);
3861 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
3863 if (IO_IS_ALLOCATING(zio
) &&
3864 !(zio
->io_flags
& ZIO_FLAG_CANFAIL
)) {
3865 if (zio
->io_error
!= ENOSPC
)
3866 zio
->io_reexecute
|= ZIO_REEXECUTE_NOW
;
3868 zio
->io_reexecute
|= ZIO_REEXECUTE_SUSPEND
;
3871 if ((zio
->io_type
== ZIO_TYPE_READ
||
3872 zio
->io_type
== ZIO_TYPE_FREE
) &&
3873 !(zio
->io_flags
& ZIO_FLAG_SCAN_THREAD
) &&
3874 zio
->io_error
== ENXIO
&&
3875 spa_load_state(zio
->io_spa
) == SPA_LOAD_NONE
&&
3876 spa_get_failmode(zio
->io_spa
) != ZIO_FAILURE_MODE_CONTINUE
)
3877 zio
->io_reexecute
|= ZIO_REEXECUTE_SUSPEND
;
3879 if (!(zio
->io_flags
& ZIO_FLAG_CANFAIL
) && !zio
->io_reexecute
)
3880 zio
->io_reexecute
|= ZIO_REEXECUTE_SUSPEND
;
3883 * Here is a possibly good place to attempt to do
3884 * either combinatorial reconstruction or error correction
3885 * based on checksums. It also might be a good place
3886 * to send out preliminary ereports before we suspend
3892 * If there were logical child errors, they apply to us now.
3893 * We defer this until now to avoid conflating logical child
3894 * errors with errors that happened to the zio itself when
3895 * updating vdev stats and reporting FMA events above.
3897 zio_inherit_child_errors(zio
, ZIO_CHILD_LOGICAL
);
3899 if ((zio
->io_error
|| zio
->io_reexecute
) &&
3900 IO_IS_ALLOCATING(zio
) && zio
->io_gang_leader
== zio
&&
3901 !(zio
->io_flags
& (ZIO_FLAG_IO_REWRITE
| ZIO_FLAG_NOPWRITE
)))
3902 zio_dva_unallocate(zio
, zio
->io_gang_tree
, zio
->io_bp
);
3904 zio_gang_tree_free(&zio
->io_gang_tree
);
3907 * Godfather I/Os should never suspend.
3909 if ((zio
->io_flags
& ZIO_FLAG_GODFATHER
) &&
3910 (zio
->io_reexecute
& ZIO_REEXECUTE_SUSPEND
))
3911 zio
->io_reexecute
&= ~ZIO_REEXECUTE_SUSPEND
;
3913 if (zio
->io_reexecute
) {
3915 * This is a logical I/O that wants to reexecute.
3917 * Reexecute is top-down. When an i/o fails, if it's not
3918 * the root, it simply notifies its parent and sticks around.
3919 * The parent, seeing that it still has children in zio_done(),
3920 * does the same. This percolates all the way up to the root.
3921 * The root i/o will reexecute or suspend the entire tree.
3923 * This approach ensures that zio_reexecute() honors
3924 * all the original i/o dependency relationships, e.g.
3925 * parents not executing until children are ready.
3927 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
3929 zio
->io_gang_leader
= NULL
;
3931 mutex_enter(&zio
->io_lock
);
3932 zio
->io_state
[ZIO_WAIT_DONE
] = 1;
3933 mutex_exit(&zio
->io_lock
);
3936 * "The Godfather" I/O monitors its children but is
3937 * not a true parent to them. It will track them through
3938 * the pipeline but severs its ties whenever they get into
3939 * trouble (e.g. suspended). This allows "The Godfather"
3940 * I/O to return status without blocking.
3943 for (pio
= zio_walk_parents(zio
, &zl
); pio
!= NULL
;
3945 zio_link_t
*remove_zl
= zl
;
3946 pio_next
= zio_walk_parents(zio
, &zl
);
3948 if ((pio
->io_flags
& ZIO_FLAG_GODFATHER
) &&
3949 (zio
->io_reexecute
& ZIO_REEXECUTE_SUSPEND
)) {
3950 zio_remove_child(pio
, zio
, remove_zl
);
3951 zio_notify_parent(pio
, zio
, ZIO_WAIT_DONE
);
3955 if ((pio
= zio_unique_parent(zio
)) != NULL
) {
3957 * We're not a root i/o, so there's nothing to do
3958 * but notify our parent. Don't propagate errors
3959 * upward since we haven't permanently failed yet.
3961 ASSERT(!(zio
->io_flags
& ZIO_FLAG_GODFATHER
));
3962 zio
->io_flags
|= ZIO_FLAG_DONT_PROPAGATE
;
3963 zio_notify_parent(pio
, zio
, ZIO_WAIT_DONE
);
3964 } else if (zio
->io_reexecute
& ZIO_REEXECUTE_SUSPEND
) {
3966 * We'd fail again if we reexecuted now, so suspend
3967 * until conditions improve (e.g. device comes online).
3969 zio_suspend(zio
->io_spa
, zio
);
3972 * Reexecution is potentially a huge amount of work.
3973 * Hand it off to the otherwise-unused claim taskq.
3975 ASSERT(taskq_empty_ent(&zio
->io_tqent
));
3976 spa_taskq_dispatch_ent(zio
->io_spa
,
3977 ZIO_TYPE_CLAIM
, ZIO_TASKQ_ISSUE
,
3978 (task_func_t
*)zio_reexecute
, zio
, 0,
3981 return (ZIO_PIPELINE_STOP
);
3984 ASSERT(zio
->io_child_count
== 0);
3985 ASSERT(zio
->io_reexecute
== 0);
3986 ASSERT(zio
->io_error
== 0 || (zio
->io_flags
& ZIO_FLAG_CANFAIL
));
3989 * Report any checksum errors, since the I/O is complete.
3991 while (zio
->io_cksum_report
!= NULL
) {
3992 zio_cksum_report_t
*zcr
= zio
->io_cksum_report
;
3993 zio
->io_cksum_report
= zcr
->zcr_next
;
3994 zcr
->zcr_next
= NULL
;
3995 zcr
->zcr_finish(zcr
, NULL
);
3996 zfs_ereport_free_checksum(zcr
);
3999 if (zio
->io_flags
& ZIO_FLAG_FASTWRITE
&& zio
->io_bp
&&
4000 !BP_IS_HOLE(zio
->io_bp
) && !BP_IS_EMBEDDED(zio
->io_bp
) &&
4001 !(zio
->io_flags
& ZIO_FLAG_NOPWRITE
)) {
4002 metaslab_fastwrite_unmark(zio
->io_spa
, zio
->io_bp
);
4006 * It is the responsibility of the done callback to ensure that this
4007 * particular zio is no longer discoverable for adoption, and as
4008 * such, cannot acquire any new parents.
4013 mutex_enter(&zio
->io_lock
);
4014 zio
->io_state
[ZIO_WAIT_DONE
] = 1;
4015 mutex_exit(&zio
->io_lock
);
4018 for (pio
= zio_walk_parents(zio
, &zl
); pio
!= NULL
; pio
= pio_next
) {
4019 zio_link_t
*remove_zl
= zl
;
4020 pio_next
= zio_walk_parents(zio
, &zl
);
4021 zio_remove_child(pio
, zio
, remove_zl
);
4022 zio_notify_parent(pio
, zio
, ZIO_WAIT_DONE
);
4025 if (zio
->io_waiter
!= NULL
) {
4026 mutex_enter(&zio
->io_lock
);
4027 zio
->io_executor
= NULL
;
4028 cv_broadcast(&zio
->io_cv
);
4029 mutex_exit(&zio
->io_lock
);
4034 return (ZIO_PIPELINE_STOP
);
4038 * ==========================================================================
4039 * I/O pipeline definition
4040 * ==========================================================================
4042 static zio_pipe_stage_t
*zio_pipeline
[] = {
4049 zio_checksum_generate
,
4065 zio_checksum_verify
,
4073 * Compare two zbookmark_phys_t's to see which we would reach first in a
4074 * pre-order traversal of the object tree.
4076 * This is simple in every case aside from the meta-dnode object. For all other
4077 * objects, we traverse them in order (object 1 before object 2, and so on).
4078 * However, all of these objects are traversed while traversing object 0, since
4079 * the data it points to is the list of objects. Thus, we need to convert to a
4080 * canonical representation so we can compare meta-dnode bookmarks to
4081 * non-meta-dnode bookmarks.
4083 * We do this by calculating "equivalents" for each field of the zbookmark.
4084 * zbookmarks outside of the meta-dnode use their own object and level, and
4085 * calculate the level 0 equivalent (the first L0 blkid that is contained in the
4086 * blocks this bookmark refers to) by multiplying their blkid by their span
4087 * (the number of L0 blocks contained within one block at their level).
4088 * zbookmarks inside the meta-dnode calculate their object equivalent
4089 * (which is L0equiv * dnodes per data block), use 0 for their L0equiv, and use
4090 * level + 1<<31 (any value larger than a level could ever be) for their level.
4091 * This causes them to always compare before a bookmark in their object
4092 * equivalent, compare appropriately to bookmarks in other objects, and to
4093 * compare appropriately to other bookmarks in the meta-dnode.
4096 zbookmark_compare(uint16_t dbss1
, uint8_t ibs1
, uint16_t dbss2
, uint8_t ibs2
,
4097 const zbookmark_phys_t
*zb1
, const zbookmark_phys_t
*zb2
)
4100 * These variables represent the "equivalent" values for the zbookmark,
4101 * after converting zbookmarks inside the meta dnode to their
4102 * normal-object equivalents.
4104 uint64_t zb1obj
, zb2obj
;
4105 uint64_t zb1L0
, zb2L0
;
4106 uint64_t zb1level
, zb2level
;
4108 if (zb1
->zb_object
== zb2
->zb_object
&&
4109 zb1
->zb_level
== zb2
->zb_level
&&
4110 zb1
->zb_blkid
== zb2
->zb_blkid
)
4114 * BP_SPANB calculates the span in blocks.
4116 zb1L0
= (zb1
->zb_blkid
) * BP_SPANB(ibs1
, zb1
->zb_level
);
4117 zb2L0
= (zb2
->zb_blkid
) * BP_SPANB(ibs2
, zb2
->zb_level
);
4119 if (zb1
->zb_object
== DMU_META_DNODE_OBJECT
) {
4120 zb1obj
= zb1L0
* (dbss1
<< (SPA_MINBLOCKSHIFT
- DNODE_SHIFT
));
4122 zb1level
= zb1
->zb_level
+ COMPARE_META_LEVEL
;
4124 zb1obj
= zb1
->zb_object
;
4125 zb1level
= zb1
->zb_level
;
4128 if (zb2
->zb_object
== DMU_META_DNODE_OBJECT
) {
4129 zb2obj
= zb2L0
* (dbss2
<< (SPA_MINBLOCKSHIFT
- DNODE_SHIFT
));
4131 zb2level
= zb2
->zb_level
+ COMPARE_META_LEVEL
;
4133 zb2obj
= zb2
->zb_object
;
4134 zb2level
= zb2
->zb_level
;
4137 /* Now that we have a canonical representation, do the comparison. */
4138 if (zb1obj
!= zb2obj
)
4139 return (zb1obj
< zb2obj
? -1 : 1);
4140 else if (zb1L0
!= zb2L0
)
4141 return (zb1L0
< zb2L0
? -1 : 1);
4142 else if (zb1level
!= zb2level
)
4143 return (zb1level
> zb2level
? -1 : 1);
4145 * This can (theoretically) happen if the bookmarks have the same object
4146 * and level, but different blkids, if the block sizes are not the same.
4147 * There is presently no way to change the indirect block sizes
4153 * This function checks the following: given that last_block is the place that
4154 * our traversal stopped last time, does that guarantee that we've visited
4155 * every node under subtree_root? Therefore, we can't just use the raw output
4156 * of zbookmark_compare. We have to pass in a modified version of
4157 * subtree_root; by incrementing the block id, and then checking whether
4158 * last_block is before or equal to that, we can tell whether or not having
4159 * visited last_block implies that all of subtree_root's children have been
4163 zbookmark_subtree_completed(const dnode_phys_t
*dnp
,
4164 const zbookmark_phys_t
*subtree_root
, const zbookmark_phys_t
*last_block
)
4166 zbookmark_phys_t mod_zb
= *subtree_root
;
4168 ASSERT(last_block
->zb_level
== 0);
4170 /* The objset_phys_t isn't before anything. */
4175 * We pass in 1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT) for the
4176 * data block size in sectors, because that variable is only used if
4177 * the bookmark refers to a block in the meta-dnode. Since we don't
4178 * know without examining it what object it refers to, and there's no
4179 * harm in passing in this value in other cases, we always pass it in.
4181 * We pass in 0 for the indirect block size shift because zb2 must be
4182 * level 0. The indirect block size is only used to calculate the span
4183 * of the bookmark, but since the bookmark must be level 0, the span is
4184 * always 1, so the math works out.
4186 * If you make changes to how the zbookmark_compare code works, be sure
4187 * to make sure that this code still works afterwards.
4189 return (zbookmark_compare(dnp
->dn_datablkszsec
, dnp
->dn_indblkshift
,
4190 1ULL << (DNODE_BLOCK_SHIFT
- SPA_MINBLOCKSHIFT
), 0, &mod_zb
,
4194 #if defined(_KERNEL) && defined(HAVE_SPL)
4195 EXPORT_SYMBOL(zio_type_name
);
4196 EXPORT_SYMBOL(zio_buf_alloc
);
4197 EXPORT_SYMBOL(zio_data_buf_alloc
);
4198 EXPORT_SYMBOL(zio_buf_free
);
4199 EXPORT_SYMBOL(zio_data_buf_free
);
4201 module_param(zio_delay_max
, int, 0644);
4202 MODULE_PARM_DESC(zio_delay_max
, "Max zio millisec delay before posting event");
4204 module_param(zio_requeue_io_start_cut_in_line
, int, 0644);
4205 MODULE_PARM_DESC(zio_requeue_io_start_cut_in_line
, "Prioritize requeued I/O");
4207 module_param(zfs_sync_pass_deferred_free
, int, 0644);
4208 MODULE_PARM_DESC(zfs_sync_pass_deferred_free
,
4209 "Defer frees starting in this pass");
4211 module_param(zfs_sync_pass_dont_compress
, int, 0644);
4212 MODULE_PARM_DESC(zfs_sync_pass_dont_compress
,
4213 "Don't compress starting in this pass");
4215 module_param(zfs_sync_pass_rewrite
, int, 0644);
4216 MODULE_PARM_DESC(zfs_sync_pass_rewrite
,
4217 "Rewrite new bps starting in this pass");
4219 module_param(zio_dva_throttle_enabled
, int, 0644);
4220 MODULE_PARM_DESC(zio_dva_throttle_enabled
,
4221 "Throttle block allocations in the ZIO pipeline");