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, 2016 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>
47 * ==========================================================================
48 * I/O type descriptions
49 * ==========================================================================
51 const char *zio_type_name
[ZIO_TYPES
] = {
53 * Note: Linux kernel thread name length is limited
54 * so these names will differ from upstream open zfs.
56 "z_null", "z_rd", "z_wr", "z_fr", "z_cl", "z_ioctl"
59 int zio_dva_throttle_enabled
= B_TRUE
;
62 * ==========================================================================
64 * ==========================================================================
66 kmem_cache_t
*zio_cache
;
67 kmem_cache_t
*zio_link_cache
;
68 kmem_cache_t
*zio_buf_cache
[SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
];
69 kmem_cache_t
*zio_data_buf_cache
[SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
];
70 int zio_delay_max
= ZIO_DELAY_MAX
;
72 #define ZIO_PIPELINE_CONTINUE 0x100
73 #define ZIO_PIPELINE_STOP 0x101
75 #define BP_SPANB(indblkshift, level) \
76 (((uint64_t)1) << ((level) * ((indblkshift) - SPA_BLKPTRSHIFT)))
77 #define COMPARE_META_LEVEL 0x80000000ul
79 * The following actions directly effect the spa's sync-to-convergence logic.
80 * The values below define the sync pass when we start performing the action.
81 * Care should be taken when changing these values as they directly impact
82 * spa_sync() performance. Tuning these values may introduce subtle performance
83 * pathologies and should only be done in the context of performance analysis.
84 * These tunables will eventually be removed and replaced with #defines once
85 * enough analysis has been done to determine optimal values.
87 * The 'zfs_sync_pass_deferred_free' pass must be greater than 1 to ensure that
88 * regular blocks are not deferred.
90 int zfs_sync_pass_deferred_free
= 2; /* defer frees starting in this pass */
91 int zfs_sync_pass_dont_compress
= 5; /* don't compress starting in this pass */
92 int zfs_sync_pass_rewrite
= 2; /* rewrite new bps starting in this pass */
95 * An allocating zio is one that either currently has the DVA allocate
96 * stage set or will have it later in its lifetime.
98 #define IO_IS_ALLOCATING(zio) ((zio)->io_orig_pipeline & ZIO_STAGE_DVA_ALLOCATE)
100 int zio_requeue_io_start_cut_in_line
= 1;
103 int zio_buf_debug_limit
= 16384;
105 int zio_buf_debug_limit
= 0;
108 static inline void __zio_execute(zio_t
*zio
);
110 static void zio_taskq_dispatch(zio_t
*, zio_taskq_type_t
, boolean_t
);
116 vmem_t
*data_alloc_arena
= NULL
;
118 zio_cache
= kmem_cache_create("zio_cache",
119 sizeof (zio_t
), 0, NULL
, NULL
, NULL
, NULL
, NULL
, 0);
120 zio_link_cache
= kmem_cache_create("zio_link_cache",
121 sizeof (zio_link_t
), 0, NULL
, NULL
, NULL
, NULL
, NULL
, 0);
124 * For small buffers, we want a cache for each multiple of
125 * SPA_MINBLOCKSIZE. For larger buffers, we want a cache
126 * for each quarter-power of 2.
128 for (c
= 0; c
< SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
; c
++) {
129 size_t size
= (c
+ 1) << SPA_MINBLOCKSHIFT
;
132 size_t cflags
= (size
> zio_buf_debug_limit
) ? KMC_NODEBUG
: 0;
136 * Cache size limited to 1M on 32-bit platforms until ARC
137 * buffers no longer require virtual address space.
139 if (size
> zfs_max_recordsize
)
148 * If we are using watchpoints, put each buffer on its own page,
149 * to eliminate the performance overhead of trapping to the
150 * kernel when modifying a non-watched buffer that shares the
151 * page with a watched buffer.
153 if (arc_watch
&& !IS_P2ALIGNED(size
, PAGESIZE
))
156 * Here's the problem - on 4K native devices in userland on
157 * Linux using O_DIRECT, buffers must be 4K aligned or I/O
158 * will fail with EINVAL, causing zdb (and others) to coredump.
159 * Since userland probably doesn't need optimized buffer caches,
160 * we just force 4K alignment on everything.
162 align
= 8 * SPA_MINBLOCKSIZE
;
164 if (size
<= 4 * SPA_MINBLOCKSIZE
) {
165 align
= SPA_MINBLOCKSIZE
;
166 } else if (IS_P2ALIGNED(size
, p2
>> 2)) {
167 align
= MIN(p2
>> 2, PAGESIZE
);
173 (void) sprintf(name
, "zio_buf_%lu", (ulong_t
)size
);
174 zio_buf_cache
[c
] = kmem_cache_create(name
, size
,
175 align
, NULL
, NULL
, NULL
, NULL
, NULL
, cflags
);
177 (void) sprintf(name
, "zio_data_buf_%lu", (ulong_t
)size
);
178 zio_data_buf_cache
[c
] = kmem_cache_create(name
, size
,
179 align
, NULL
, NULL
, NULL
, NULL
,
180 data_alloc_arena
, cflags
);
185 ASSERT(zio_buf_cache
[c
] != NULL
);
186 if (zio_buf_cache
[c
- 1] == NULL
)
187 zio_buf_cache
[c
- 1] = zio_buf_cache
[c
];
189 ASSERT(zio_data_buf_cache
[c
] != NULL
);
190 if (zio_data_buf_cache
[c
- 1] == NULL
)
191 zio_data_buf_cache
[c
- 1] = zio_data_buf_cache
[c
];
203 kmem_cache_t
*last_cache
= NULL
;
204 kmem_cache_t
*last_data_cache
= NULL
;
206 for (c
= 0; c
< SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
; c
++) {
209 * Cache size limited to 1M on 32-bit platforms until ARC
210 * buffers no longer require virtual address space.
212 if (((c
+ 1) << SPA_MINBLOCKSHIFT
) > zfs_max_recordsize
)
215 if (zio_buf_cache
[c
] != last_cache
) {
216 last_cache
= zio_buf_cache
[c
];
217 kmem_cache_destroy(zio_buf_cache
[c
]);
219 zio_buf_cache
[c
] = NULL
;
221 if (zio_data_buf_cache
[c
] != last_data_cache
) {
222 last_data_cache
= zio_data_buf_cache
[c
];
223 kmem_cache_destroy(zio_data_buf_cache
[c
]);
225 zio_data_buf_cache
[c
] = NULL
;
228 kmem_cache_destroy(zio_link_cache
);
229 kmem_cache_destroy(zio_cache
);
237 * ==========================================================================
238 * Allocate and free I/O buffers
239 * ==========================================================================
243 * Use zio_buf_alloc to allocate ZFS metadata. This data will appear in a
244 * crashdump if the kernel panics, so use it judiciously. Obviously, it's
245 * useful to inspect ZFS metadata, but if possible, we should avoid keeping
246 * excess / transient data in-core during a crashdump.
249 zio_buf_alloc(size_t size
)
251 size_t c
= (size
- 1) >> SPA_MINBLOCKSHIFT
;
253 VERIFY3U(c
, <, SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
);
255 return (kmem_cache_alloc(zio_buf_cache
[c
], KM_PUSHPAGE
));
259 * Use zio_data_buf_alloc to allocate data. The data will not appear in a
260 * crashdump if the kernel panics. This exists so that we will limit the amount
261 * of ZFS data that shows up in a kernel crashdump. (Thus reducing the amount
262 * of kernel heap dumped to disk when the kernel panics)
265 zio_data_buf_alloc(size_t size
)
267 size_t c
= (size
- 1) >> SPA_MINBLOCKSHIFT
;
269 VERIFY3U(c
, <, SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
);
271 return (kmem_cache_alloc(zio_data_buf_cache
[c
], KM_PUSHPAGE
));
275 * Use zio_buf_alloc_flags when specific allocation flags are needed. e.g.
276 * passing KM_NOSLEEP when it is acceptable for an allocation to fail.
279 zio_buf_alloc_flags(size_t size
, int flags
)
281 size_t c
= (size
- 1) >> SPA_MINBLOCKSHIFT
;
283 VERIFY3U(c
, <, SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
);
285 return (kmem_cache_alloc(zio_buf_cache
[c
], flags
));
289 zio_buf_free(void *buf
, size_t size
)
291 size_t c
= (size
- 1) >> SPA_MINBLOCKSHIFT
;
293 VERIFY3U(c
, <, SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
);
295 kmem_cache_free(zio_buf_cache
[c
], buf
);
299 zio_data_buf_free(void *buf
, size_t size
)
301 size_t c
= (size
- 1) >> SPA_MINBLOCKSHIFT
;
303 VERIFY3U(c
, <, SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
);
305 kmem_cache_free(zio_data_buf_cache
[c
], buf
);
309 * ==========================================================================
310 * Push and pop I/O transform buffers
311 * ==========================================================================
314 zio_push_transform(zio_t
*zio
, void *data
, uint64_t size
, uint64_t bufsize
,
315 zio_transform_func_t
*transform
)
317 zio_transform_t
*zt
= kmem_alloc(sizeof (zio_transform_t
), KM_SLEEP
);
319 zt
->zt_orig_data
= zio
->io_data
;
320 zt
->zt_orig_size
= zio
->io_size
;
321 zt
->zt_bufsize
= bufsize
;
322 zt
->zt_transform
= transform
;
324 zt
->zt_next
= zio
->io_transform_stack
;
325 zio
->io_transform_stack
= zt
;
332 zio_pop_transforms(zio_t
*zio
)
336 while ((zt
= zio
->io_transform_stack
) != NULL
) {
337 if (zt
->zt_transform
!= NULL
)
338 zt
->zt_transform(zio
,
339 zt
->zt_orig_data
, zt
->zt_orig_size
);
341 if (zt
->zt_bufsize
!= 0)
342 zio_buf_free(zio
->io_data
, zt
->zt_bufsize
);
344 zio
->io_data
= zt
->zt_orig_data
;
345 zio
->io_size
= zt
->zt_orig_size
;
346 zio
->io_transform_stack
= zt
->zt_next
;
348 kmem_free(zt
, sizeof (zio_transform_t
));
353 * ==========================================================================
354 * I/O transform callbacks for subblocks and decompression
355 * ==========================================================================
358 zio_subblock(zio_t
*zio
, void *data
, uint64_t size
)
360 ASSERT(zio
->io_size
> size
);
362 if (zio
->io_type
== ZIO_TYPE_READ
)
363 bcopy(zio
->io_data
, data
, size
);
367 zio_decompress(zio_t
*zio
, void *data
, uint64_t size
)
369 if (zio
->io_error
== 0 &&
370 zio_decompress_data(BP_GET_COMPRESS(zio
->io_bp
),
371 zio
->io_data
, data
, zio
->io_size
, size
) != 0)
372 zio
->io_error
= SET_ERROR(EIO
);
376 * ==========================================================================
377 * I/O parent/child relationships and pipeline interlocks
378 * ==========================================================================
381 zio_walk_parents(zio_t
*cio
, zio_link_t
**zl
)
383 list_t
*pl
= &cio
->io_parent_list
;
385 *zl
= (*zl
== NULL
) ? list_head(pl
) : list_next(pl
, *zl
);
389 ASSERT((*zl
)->zl_child
== cio
);
390 return ((*zl
)->zl_parent
);
394 zio_walk_children(zio_t
*pio
, zio_link_t
**zl
)
396 list_t
*cl
= &pio
->io_child_list
;
398 *zl
= (*zl
== NULL
) ? list_head(cl
) : list_next(cl
, *zl
);
402 ASSERT((*zl
)->zl_parent
== pio
);
403 return ((*zl
)->zl_child
);
407 zio_unique_parent(zio_t
*cio
)
409 zio_link_t
*zl
= NULL
;
410 zio_t
*pio
= zio_walk_parents(cio
, &zl
);
412 VERIFY3P(zio_walk_parents(cio
, &zl
), ==, NULL
);
417 zio_add_child(zio_t
*pio
, zio_t
*cio
)
419 zio_link_t
*zl
= kmem_cache_alloc(zio_link_cache
, KM_SLEEP
);
423 * Logical I/Os can have logical, gang, or vdev children.
424 * Gang I/Os can have gang or vdev children.
425 * Vdev I/Os can only have vdev children.
426 * The following ASSERT captures all of these constraints.
428 ASSERT(cio
->io_child_type
<= pio
->io_child_type
);
433 mutex_enter(&cio
->io_lock
);
434 mutex_enter(&pio
->io_lock
);
436 ASSERT(pio
->io_state
[ZIO_WAIT_DONE
] == 0);
438 for (w
= 0; w
< ZIO_WAIT_TYPES
; w
++)
439 pio
->io_children
[cio
->io_child_type
][w
] += !cio
->io_state
[w
];
441 list_insert_head(&pio
->io_child_list
, zl
);
442 list_insert_head(&cio
->io_parent_list
, zl
);
444 pio
->io_child_count
++;
445 cio
->io_parent_count
++;
447 mutex_exit(&pio
->io_lock
);
448 mutex_exit(&cio
->io_lock
);
452 zio_remove_child(zio_t
*pio
, zio_t
*cio
, zio_link_t
*zl
)
454 ASSERT(zl
->zl_parent
== pio
);
455 ASSERT(zl
->zl_child
== cio
);
457 mutex_enter(&cio
->io_lock
);
458 mutex_enter(&pio
->io_lock
);
460 list_remove(&pio
->io_child_list
, zl
);
461 list_remove(&cio
->io_parent_list
, zl
);
463 pio
->io_child_count
--;
464 cio
->io_parent_count
--;
466 mutex_exit(&pio
->io_lock
);
467 mutex_exit(&cio
->io_lock
);
468 kmem_cache_free(zio_link_cache
, zl
);
472 zio_wait_for_children(zio_t
*zio
, enum zio_child child
, enum zio_wait_type wait
)
474 uint64_t *countp
= &zio
->io_children
[child
][wait
];
475 boolean_t waiting
= B_FALSE
;
477 mutex_enter(&zio
->io_lock
);
478 ASSERT(zio
->io_stall
== NULL
);
481 ASSERT3U(zio
->io_stage
, !=, ZIO_STAGE_OPEN
);
482 zio
->io_stall
= countp
;
485 mutex_exit(&zio
->io_lock
);
490 __attribute__((always_inline
))
492 zio_notify_parent(zio_t
*pio
, zio_t
*zio
, enum zio_wait_type wait
)
494 uint64_t *countp
= &pio
->io_children
[zio
->io_child_type
][wait
];
495 int *errorp
= &pio
->io_child_error
[zio
->io_child_type
];
497 mutex_enter(&pio
->io_lock
);
498 if (zio
->io_error
&& !(zio
->io_flags
& ZIO_FLAG_DONT_PROPAGATE
))
499 *errorp
= zio_worst_error(*errorp
, zio
->io_error
);
500 pio
->io_reexecute
|= zio
->io_reexecute
;
501 ASSERT3U(*countp
, >, 0);
505 if (*countp
== 0 && pio
->io_stall
== countp
) {
506 zio_taskq_type_t type
=
507 pio
->io_stage
< ZIO_STAGE_VDEV_IO_START
? ZIO_TASKQ_ISSUE
:
509 pio
->io_stall
= NULL
;
510 mutex_exit(&pio
->io_lock
);
512 * Dispatch the parent zio in its own taskq so that
513 * the child can continue to make progress. This also
514 * prevents overflowing the stack when we have deeply nested
515 * parent-child relationships.
517 zio_taskq_dispatch(pio
, type
, B_FALSE
);
519 mutex_exit(&pio
->io_lock
);
524 zio_inherit_child_errors(zio_t
*zio
, enum zio_child c
)
526 if (zio
->io_child_error
[c
] != 0 && zio
->io_error
== 0)
527 zio
->io_error
= zio
->io_child_error
[c
];
531 zio_timestamp_compare(const void *x1
, const void *x2
)
533 const zio_t
*z1
= x1
;
534 const zio_t
*z2
= x2
;
537 cmp
= AVL_CMP(z1
->io_queued_timestamp
, z2
->io_queued_timestamp
);
541 cmp
= AVL_CMP(z1
->io_offset
, z2
->io_offset
);
545 return (AVL_PCMP(z1
, z2
));
549 * ==========================================================================
550 * Create the various types of I/O (read, write, free, etc)
551 * ==========================================================================
554 zio_create(zio_t
*pio
, spa_t
*spa
, uint64_t txg
, const blkptr_t
*bp
,
555 void *data
, uint64_t lsize
, uint64_t psize
, zio_done_func_t
*done
,
556 void *private, zio_type_t type
, zio_priority_t priority
,
557 enum zio_flag flags
, vdev_t
*vd
, uint64_t offset
,
558 const zbookmark_phys_t
*zb
, enum zio_stage stage
,
559 enum zio_stage pipeline
)
563 ASSERT3U(psize
, <=, SPA_MAXBLOCKSIZE
);
564 ASSERT(P2PHASE(psize
, SPA_MINBLOCKSIZE
) == 0);
565 ASSERT(P2PHASE(offset
, SPA_MINBLOCKSIZE
) == 0);
567 ASSERT(!vd
|| spa_config_held(spa
, SCL_STATE_ALL
, RW_READER
));
568 ASSERT(!bp
|| !(flags
& ZIO_FLAG_CONFIG_WRITER
));
569 ASSERT(vd
|| stage
== ZIO_STAGE_OPEN
);
571 IMPLY(lsize
!= psize
, (flags
& ZIO_FLAG_RAW
) != 0);
573 zio
= kmem_cache_alloc(zio_cache
, KM_SLEEP
);
574 bzero(zio
, sizeof (zio_t
));
576 mutex_init(&zio
->io_lock
, NULL
, MUTEX_NOLOCKDEP
, NULL
);
577 cv_init(&zio
->io_cv
, NULL
, CV_DEFAULT
, NULL
);
579 list_create(&zio
->io_parent_list
, sizeof (zio_link_t
),
580 offsetof(zio_link_t
, zl_parent_node
));
581 list_create(&zio
->io_child_list
, sizeof (zio_link_t
),
582 offsetof(zio_link_t
, zl_child_node
));
585 zio
->io_child_type
= ZIO_CHILD_VDEV
;
586 else if (flags
& ZIO_FLAG_GANG_CHILD
)
587 zio
->io_child_type
= ZIO_CHILD_GANG
;
588 else if (flags
& ZIO_FLAG_DDT_CHILD
)
589 zio
->io_child_type
= ZIO_CHILD_DDT
;
591 zio
->io_child_type
= ZIO_CHILD_LOGICAL
;
594 zio
->io_bp
= (blkptr_t
*)bp
;
595 zio
->io_bp_copy
= *bp
;
596 zio
->io_bp_orig
= *bp
;
597 if (type
!= ZIO_TYPE_WRITE
||
598 zio
->io_child_type
== ZIO_CHILD_DDT
)
599 zio
->io_bp
= &zio
->io_bp_copy
; /* so caller can free */
600 if (zio
->io_child_type
== ZIO_CHILD_LOGICAL
)
601 zio
->io_logical
= zio
;
602 if (zio
->io_child_type
> ZIO_CHILD_GANG
&& BP_IS_GANG(bp
))
603 pipeline
|= ZIO_GANG_STAGES
;
609 zio
->io_private
= private;
611 zio
->io_priority
= priority
;
613 zio
->io_offset
= offset
;
614 zio
->io_orig_data
= zio
->io_data
= data
;
615 zio
->io_orig_size
= zio
->io_size
= psize
;
616 zio
->io_lsize
= lsize
;
617 zio
->io_orig_flags
= zio
->io_flags
= flags
;
618 zio
->io_orig_stage
= zio
->io_stage
= stage
;
619 zio
->io_orig_pipeline
= zio
->io_pipeline
= pipeline
;
620 zio
->io_pipeline_trace
= ZIO_STAGE_OPEN
;
622 zio
->io_state
[ZIO_WAIT_READY
] = (stage
>= ZIO_STAGE_READY
);
623 zio
->io_state
[ZIO_WAIT_DONE
] = (stage
>= ZIO_STAGE_DONE
);
626 zio
->io_bookmark
= *zb
;
629 if (zio
->io_logical
== NULL
)
630 zio
->io_logical
= pio
->io_logical
;
631 if (zio
->io_child_type
== ZIO_CHILD_GANG
)
632 zio
->io_gang_leader
= pio
->io_gang_leader
;
633 zio_add_child(pio
, zio
);
636 taskq_init_ent(&zio
->io_tqent
);
642 zio_destroy(zio_t
*zio
)
644 list_destroy(&zio
->io_parent_list
);
645 list_destroy(&zio
->io_child_list
);
646 mutex_destroy(&zio
->io_lock
);
647 cv_destroy(&zio
->io_cv
);
648 kmem_cache_free(zio_cache
, zio
);
652 zio_null(zio_t
*pio
, spa_t
*spa
, vdev_t
*vd
, zio_done_func_t
*done
,
653 void *private, enum zio_flag flags
)
657 zio
= zio_create(pio
, spa
, 0, NULL
, NULL
, 0, 0, done
, private,
658 ZIO_TYPE_NULL
, ZIO_PRIORITY_NOW
, flags
, vd
, 0, NULL
,
659 ZIO_STAGE_OPEN
, ZIO_INTERLOCK_PIPELINE
);
665 zio_root(spa_t
*spa
, zio_done_func_t
*done
, void *private, enum zio_flag flags
)
667 return (zio_null(NULL
, spa
, NULL
, done
, private, flags
));
671 zfs_blkptr_verify(spa_t
*spa
, const blkptr_t
*bp
)
675 if (!DMU_OT_IS_VALID(BP_GET_TYPE(bp
))) {
676 zfs_panic_recover("blkptr at %p has invalid TYPE %llu",
677 bp
, (longlong_t
)BP_GET_TYPE(bp
));
679 if (BP_GET_CHECKSUM(bp
) >= ZIO_CHECKSUM_FUNCTIONS
||
680 BP_GET_CHECKSUM(bp
) <= ZIO_CHECKSUM_ON
) {
681 zfs_panic_recover("blkptr at %p has invalid CHECKSUM %llu",
682 bp
, (longlong_t
)BP_GET_CHECKSUM(bp
));
684 if (BP_GET_COMPRESS(bp
) >= ZIO_COMPRESS_FUNCTIONS
||
685 BP_GET_COMPRESS(bp
) <= ZIO_COMPRESS_ON
) {
686 zfs_panic_recover("blkptr at %p has invalid COMPRESS %llu",
687 bp
, (longlong_t
)BP_GET_COMPRESS(bp
));
689 if (BP_GET_LSIZE(bp
) > SPA_MAXBLOCKSIZE
) {
690 zfs_panic_recover("blkptr at %p has invalid LSIZE %llu",
691 bp
, (longlong_t
)BP_GET_LSIZE(bp
));
693 if (BP_GET_PSIZE(bp
) > SPA_MAXBLOCKSIZE
) {
694 zfs_panic_recover("blkptr at %p has invalid PSIZE %llu",
695 bp
, (longlong_t
)BP_GET_PSIZE(bp
));
698 if (BP_IS_EMBEDDED(bp
)) {
699 if (BPE_GET_ETYPE(bp
) > NUM_BP_EMBEDDED_TYPES
) {
700 zfs_panic_recover("blkptr at %p has invalid ETYPE %llu",
701 bp
, (longlong_t
)BPE_GET_ETYPE(bp
));
706 * Pool-specific checks.
708 * Note: it would be nice to verify that the blk_birth and
709 * BP_PHYSICAL_BIRTH() are not too large. However, spa_freeze()
710 * allows the birth time of log blocks (and dmu_sync()-ed blocks
711 * that are in the log) to be arbitrarily large.
713 for (i
= 0; i
< BP_GET_NDVAS(bp
); i
++) {
714 uint64_t vdevid
= DVA_GET_VDEV(&bp
->blk_dva
[i
]);
716 uint64_t offset
, asize
;
717 if (vdevid
>= spa
->spa_root_vdev
->vdev_children
) {
718 zfs_panic_recover("blkptr at %p DVA %u has invalid "
720 bp
, i
, (longlong_t
)vdevid
);
723 vd
= spa
->spa_root_vdev
->vdev_child
[vdevid
];
725 zfs_panic_recover("blkptr at %p DVA %u has invalid "
727 bp
, i
, (longlong_t
)vdevid
);
730 if (vd
->vdev_ops
== &vdev_hole_ops
) {
731 zfs_panic_recover("blkptr at %p DVA %u has hole "
733 bp
, i
, (longlong_t
)vdevid
);
736 if (vd
->vdev_ops
== &vdev_missing_ops
) {
738 * "missing" vdevs are valid during import, but we
739 * don't have their detailed info (e.g. asize), so
740 * we can't perform any more checks on them.
744 offset
= DVA_GET_OFFSET(&bp
->blk_dva
[i
]);
745 asize
= DVA_GET_ASIZE(&bp
->blk_dva
[i
]);
747 asize
= vdev_psize_to_asize(vd
, SPA_GANGBLOCKSIZE
);
748 if (offset
+ asize
> vd
->vdev_asize
) {
749 zfs_panic_recover("blkptr at %p DVA %u has invalid "
751 bp
, i
, (longlong_t
)offset
);
757 zio_read(zio_t
*pio
, spa_t
*spa
, const blkptr_t
*bp
,
758 void *data
, uint64_t size
, zio_done_func_t
*done
, void *private,
759 zio_priority_t priority
, enum zio_flag flags
, const zbookmark_phys_t
*zb
)
763 zfs_blkptr_verify(spa
, bp
);
765 zio
= zio_create(pio
, spa
, BP_PHYSICAL_BIRTH(bp
), bp
,
766 data
, size
, size
, done
, private,
767 ZIO_TYPE_READ
, priority
, flags
, NULL
, 0, zb
,
768 ZIO_STAGE_OPEN
, (flags
& ZIO_FLAG_DDT_CHILD
) ?
769 ZIO_DDT_CHILD_READ_PIPELINE
: ZIO_READ_PIPELINE
);
775 zio_write(zio_t
*pio
, spa_t
*spa
, uint64_t txg
, blkptr_t
*bp
,
776 void *data
, uint64_t lsize
, uint64_t psize
, const zio_prop_t
*zp
,
777 zio_done_func_t
*ready
, zio_done_func_t
*children_ready
,
778 zio_done_func_t
*physdone
, zio_done_func_t
*done
,
779 void *private, zio_priority_t priority
, enum zio_flag flags
,
780 const zbookmark_phys_t
*zb
)
784 ASSERT(zp
->zp_checksum
>= ZIO_CHECKSUM_OFF
&&
785 zp
->zp_checksum
< ZIO_CHECKSUM_FUNCTIONS
&&
786 zp
->zp_compress
>= ZIO_COMPRESS_OFF
&&
787 zp
->zp_compress
< ZIO_COMPRESS_FUNCTIONS
&&
788 DMU_OT_IS_VALID(zp
->zp_type
) &&
791 zp
->zp_copies
<= spa_max_replication(spa
));
793 zio
= zio_create(pio
, spa
, txg
, bp
, data
, lsize
, psize
, done
, private,
794 ZIO_TYPE_WRITE
, priority
, flags
, NULL
, 0, zb
,
795 ZIO_STAGE_OPEN
, (flags
& ZIO_FLAG_DDT_CHILD
) ?
796 ZIO_DDT_CHILD_WRITE_PIPELINE
: ZIO_WRITE_PIPELINE
);
798 zio
->io_ready
= ready
;
799 zio
->io_children_ready
= children_ready
;
800 zio
->io_physdone
= physdone
;
804 * Data can be NULL if we are going to call zio_write_override() to
805 * provide the already-allocated BP. But we may need the data to
806 * verify a dedup hit (if requested). In this case, don't try to
807 * dedup (just take the already-allocated BP verbatim).
809 if (data
== NULL
&& zio
->io_prop
.zp_dedup_verify
) {
810 zio
->io_prop
.zp_dedup
= zio
->io_prop
.zp_dedup_verify
= B_FALSE
;
817 zio_rewrite(zio_t
*pio
, spa_t
*spa
, uint64_t txg
, blkptr_t
*bp
, void *data
,
818 uint64_t size
, zio_done_func_t
*done
, void *private,
819 zio_priority_t priority
, enum zio_flag flags
, zbookmark_phys_t
*zb
)
823 zio
= zio_create(pio
, spa
, txg
, bp
, data
, size
, size
, done
, private,
824 ZIO_TYPE_WRITE
, priority
, flags
| ZIO_FLAG_IO_REWRITE
, NULL
, 0, zb
,
825 ZIO_STAGE_OPEN
, ZIO_REWRITE_PIPELINE
);
831 zio_write_override(zio_t
*zio
, blkptr_t
*bp
, int copies
, boolean_t nopwrite
)
833 ASSERT(zio
->io_type
== ZIO_TYPE_WRITE
);
834 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
835 ASSERT(zio
->io_stage
== ZIO_STAGE_OPEN
);
836 ASSERT(zio
->io_txg
== spa_syncing_txg(zio
->io_spa
));
839 * We must reset the io_prop to match the values that existed
840 * when the bp was first written by dmu_sync() keeping in mind
841 * that nopwrite and dedup are mutually exclusive.
843 zio
->io_prop
.zp_dedup
= nopwrite
? B_FALSE
: zio
->io_prop
.zp_dedup
;
844 zio
->io_prop
.zp_nopwrite
= nopwrite
;
845 zio
->io_prop
.zp_copies
= copies
;
846 zio
->io_bp_override
= bp
;
850 zio_free(spa_t
*spa
, uint64_t txg
, const blkptr_t
*bp
)
854 * The check for EMBEDDED is a performance optimization. We
855 * process the free here (by ignoring it) rather than
856 * putting it on the list and then processing it in zio_free_sync().
858 if (BP_IS_EMBEDDED(bp
))
860 metaslab_check_free(spa
, bp
);
863 * Frees that are for the currently-syncing txg, are not going to be
864 * deferred, and which will not need to do a read (i.e. not GANG or
865 * DEDUP), can be processed immediately. Otherwise, put them on the
866 * in-memory list for later processing.
868 if (BP_IS_GANG(bp
) || BP_GET_DEDUP(bp
) ||
869 txg
!= spa
->spa_syncing_txg
||
870 spa_sync_pass(spa
) >= zfs_sync_pass_deferred_free
) {
871 bplist_append(&spa
->spa_free_bplist
[txg
& TXG_MASK
], bp
);
873 VERIFY0(zio_wait(zio_free_sync(NULL
, spa
, txg
, bp
, 0)));
878 zio_free_sync(zio_t
*pio
, spa_t
*spa
, uint64_t txg
, const blkptr_t
*bp
,
882 enum zio_stage stage
= ZIO_FREE_PIPELINE
;
884 ASSERT(!BP_IS_HOLE(bp
));
885 ASSERT(spa_syncing_txg(spa
) == txg
);
886 ASSERT(spa_sync_pass(spa
) < zfs_sync_pass_deferred_free
);
888 if (BP_IS_EMBEDDED(bp
))
889 return (zio_null(pio
, spa
, NULL
, NULL
, NULL
, 0));
891 metaslab_check_free(spa
, bp
);
895 * GANG and DEDUP blocks can induce a read (for the gang block header,
896 * or the DDT), so issue them asynchronously so that this thread is
899 if (BP_IS_GANG(bp
) || BP_GET_DEDUP(bp
))
900 stage
|= ZIO_STAGE_ISSUE_ASYNC
;
902 zio
= zio_create(pio
, spa
, txg
, bp
, NULL
, BP_GET_PSIZE(bp
),
903 BP_GET_PSIZE(bp
), NULL
, NULL
, ZIO_TYPE_FREE
, ZIO_PRIORITY_NOW
,
904 flags
, NULL
, 0, NULL
, ZIO_STAGE_OPEN
, stage
);
910 zio_claim(zio_t
*pio
, spa_t
*spa
, uint64_t txg
, const blkptr_t
*bp
,
911 zio_done_func_t
*done
, void *private, enum zio_flag flags
)
915 dprintf_bp(bp
, "claiming in txg %llu", txg
);
917 if (BP_IS_EMBEDDED(bp
))
918 return (zio_null(pio
, spa
, NULL
, NULL
, NULL
, 0));
921 * A claim is an allocation of a specific block. Claims are needed
922 * to support immediate writes in the intent log. The issue is that
923 * immediate writes contain committed data, but in a txg that was
924 * *not* committed. Upon opening the pool after an unclean shutdown,
925 * the intent log claims all blocks that contain immediate write data
926 * so that the SPA knows they're in use.
928 * All claims *must* be resolved in the first txg -- before the SPA
929 * starts allocating blocks -- so that nothing is allocated twice.
930 * If txg == 0 we just verify that the block is claimable.
932 ASSERT3U(spa
->spa_uberblock
.ub_rootbp
.blk_birth
, <, spa_first_txg(spa
));
933 ASSERT(txg
== spa_first_txg(spa
) || txg
== 0);
934 ASSERT(!BP_GET_DEDUP(bp
) || !spa_writeable(spa
)); /* zdb(1M) */
936 zio
= zio_create(pio
, spa
, txg
, bp
, NULL
, BP_GET_PSIZE(bp
),
937 BP_GET_PSIZE(bp
), done
, private, ZIO_TYPE_CLAIM
, ZIO_PRIORITY_NOW
,
938 flags
, NULL
, 0, NULL
, ZIO_STAGE_OPEN
, ZIO_CLAIM_PIPELINE
);
939 ASSERT0(zio
->io_queued_timestamp
);
945 zio_ioctl(zio_t
*pio
, spa_t
*spa
, vdev_t
*vd
, int cmd
,
946 zio_done_func_t
*done
, void *private, enum zio_flag flags
)
951 if (vd
->vdev_children
== 0) {
952 zio
= zio_create(pio
, spa
, 0, NULL
, NULL
, 0, 0, done
, private,
953 ZIO_TYPE_IOCTL
, ZIO_PRIORITY_NOW
, flags
, vd
, 0, NULL
,
954 ZIO_STAGE_OPEN
, ZIO_IOCTL_PIPELINE
);
958 zio
= zio_null(pio
, spa
, NULL
, NULL
, NULL
, flags
);
960 for (c
= 0; c
< vd
->vdev_children
; c
++)
961 zio_nowait(zio_ioctl(zio
, spa
, vd
->vdev_child
[c
], cmd
,
962 done
, private, flags
));
969 zio_read_phys(zio_t
*pio
, vdev_t
*vd
, uint64_t offset
, uint64_t size
,
970 void *data
, int checksum
, zio_done_func_t
*done
, void *private,
971 zio_priority_t priority
, enum zio_flag flags
, boolean_t labels
)
975 ASSERT(vd
->vdev_children
== 0);
976 ASSERT(!labels
|| offset
+ size
<= VDEV_LABEL_START_SIZE
||
977 offset
>= vd
->vdev_psize
- VDEV_LABEL_END_SIZE
);
978 ASSERT3U(offset
+ size
, <=, vd
->vdev_psize
);
980 zio
= zio_create(pio
, vd
->vdev_spa
, 0, NULL
, data
, size
, size
, done
,
981 private, ZIO_TYPE_READ
, priority
, flags
| ZIO_FLAG_PHYSICAL
, vd
,
982 offset
, NULL
, ZIO_STAGE_OPEN
, ZIO_READ_PHYS_PIPELINE
);
984 zio
->io_prop
.zp_checksum
= checksum
;
990 zio_write_phys(zio_t
*pio
, vdev_t
*vd
, uint64_t offset
, uint64_t size
,
991 void *data
, int checksum
, zio_done_func_t
*done
, void *private,
992 zio_priority_t priority
, enum zio_flag flags
, boolean_t labels
)
996 ASSERT(vd
->vdev_children
== 0);
997 ASSERT(!labels
|| offset
+ size
<= VDEV_LABEL_START_SIZE
||
998 offset
>= vd
->vdev_psize
- VDEV_LABEL_END_SIZE
);
999 ASSERT3U(offset
+ size
, <=, vd
->vdev_psize
);
1001 zio
= zio_create(pio
, vd
->vdev_spa
, 0, NULL
, data
, size
, size
, done
,
1002 private, ZIO_TYPE_WRITE
, priority
, flags
| ZIO_FLAG_PHYSICAL
, vd
,
1003 offset
, NULL
, ZIO_STAGE_OPEN
, ZIO_WRITE_PHYS_PIPELINE
);
1005 zio
->io_prop
.zp_checksum
= checksum
;
1007 if (zio_checksum_table
[checksum
].ci_flags
& ZCHECKSUM_FLAG_EMBEDDED
) {
1009 * zec checksums are necessarily destructive -- they modify
1010 * the end of the write buffer to hold the verifier/checksum.
1011 * Therefore, we must make a local copy in case the data is
1012 * being written to multiple places in parallel.
1014 void *wbuf
= zio_buf_alloc(size
);
1015 bcopy(data
, wbuf
, size
);
1016 zio_push_transform(zio
, wbuf
, size
, size
, NULL
);
1023 * Create a child I/O to do some work for us.
1026 zio_vdev_child_io(zio_t
*pio
, blkptr_t
*bp
, vdev_t
*vd
, uint64_t offset
,
1027 void *data
, uint64_t size
, int type
, zio_priority_t priority
,
1028 enum zio_flag flags
, zio_done_func_t
*done
, void *private)
1030 enum zio_stage pipeline
= ZIO_VDEV_CHILD_PIPELINE
;
1033 ASSERT(vd
->vdev_parent
==
1034 (pio
->io_vd
? pio
->io_vd
: pio
->io_spa
->spa_root_vdev
));
1036 if (type
== ZIO_TYPE_READ
&& bp
!= NULL
) {
1038 * If we have the bp, then the child should perform the
1039 * checksum and the parent need not. This pushes error
1040 * detection as close to the leaves as possible and
1041 * eliminates redundant checksums in the interior nodes.
1043 pipeline
|= ZIO_STAGE_CHECKSUM_VERIFY
;
1044 pio
->io_pipeline
&= ~ZIO_STAGE_CHECKSUM_VERIFY
;
1047 if (vd
->vdev_children
== 0)
1048 offset
+= VDEV_LABEL_START_SIZE
;
1050 flags
|= ZIO_VDEV_CHILD_FLAGS(pio
) | ZIO_FLAG_DONT_PROPAGATE
;
1053 * If we've decided to do a repair, the write is not speculative --
1054 * even if the original read was.
1056 if (flags
& ZIO_FLAG_IO_REPAIR
)
1057 flags
&= ~ZIO_FLAG_SPECULATIVE
;
1060 * If we're creating a child I/O that is not associated with a
1061 * top-level vdev, then the child zio is not an allocating I/O.
1062 * If this is a retried I/O then we ignore it since we will
1063 * have already processed the original allocating I/O.
1065 if (flags
& ZIO_FLAG_IO_ALLOCATING
&&
1066 (vd
!= vd
->vdev_top
|| (flags
& ZIO_FLAG_IO_RETRY
))) {
1067 metaslab_class_t
*mc
= spa_normal_class(pio
->io_spa
);
1069 ASSERT(mc
->mc_alloc_throttle_enabled
);
1070 ASSERT(type
== ZIO_TYPE_WRITE
);
1071 ASSERT(priority
== ZIO_PRIORITY_ASYNC_WRITE
);
1072 ASSERT(!(flags
& ZIO_FLAG_IO_REPAIR
));
1073 ASSERT(!(pio
->io_flags
& ZIO_FLAG_IO_REWRITE
) ||
1074 pio
->io_child_type
== ZIO_CHILD_GANG
);
1076 flags
&= ~ZIO_FLAG_IO_ALLOCATING
;
1080 zio
= zio_create(pio
, pio
->io_spa
, pio
->io_txg
, bp
, data
, size
, size
,
1081 done
, private, type
, priority
, flags
, vd
, offset
, &pio
->io_bookmark
,
1082 ZIO_STAGE_VDEV_IO_START
>> 1, pipeline
);
1083 ASSERT3U(zio
->io_child_type
, ==, ZIO_CHILD_VDEV
);
1085 zio
->io_physdone
= pio
->io_physdone
;
1086 if (vd
->vdev_ops
->vdev_op_leaf
&& zio
->io_logical
!= NULL
)
1087 zio
->io_logical
->io_phys_children
++;
1093 zio_vdev_delegated_io(vdev_t
*vd
, uint64_t offset
, void *data
, uint64_t size
,
1094 int type
, zio_priority_t priority
, enum zio_flag flags
,
1095 zio_done_func_t
*done
, void *private)
1099 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
1101 zio
= zio_create(NULL
, vd
->vdev_spa
, 0, NULL
,
1102 data
, size
, size
, done
, private, type
, priority
,
1103 flags
| ZIO_FLAG_CANFAIL
| ZIO_FLAG_DONT_RETRY
| ZIO_FLAG_DELEGATED
,
1105 ZIO_STAGE_VDEV_IO_START
>> 1, ZIO_VDEV_CHILD_PIPELINE
);
1111 zio_flush(zio_t
*zio
, vdev_t
*vd
)
1113 zio_nowait(zio_ioctl(zio
, zio
->io_spa
, vd
, DKIOCFLUSHWRITECACHE
,
1115 ZIO_FLAG_CANFAIL
| ZIO_FLAG_DONT_PROPAGATE
| ZIO_FLAG_DONT_RETRY
));
1119 zio_shrink(zio_t
*zio
, uint64_t size
)
1121 ASSERT(zio
->io_executor
== NULL
);
1122 ASSERT(zio
->io_orig_size
== zio
->io_size
);
1123 ASSERT(size
<= zio
->io_size
);
1126 * We don't shrink for raidz because of problems with the
1127 * reconstruction when reading back less than the block size.
1128 * Note, BP_IS_RAIDZ() assumes no compression.
1130 ASSERT(BP_GET_COMPRESS(zio
->io_bp
) == ZIO_COMPRESS_OFF
);
1131 if (!BP_IS_RAIDZ(zio
->io_bp
)) {
1132 /* we are not doing a raw write */
1133 ASSERT3U(zio
->io_size
, ==, zio
->io_lsize
);
1134 zio
->io_orig_size
= zio
->io_size
= zio
->io_lsize
= size
;
1139 * ==========================================================================
1140 * Prepare to read and write logical blocks
1141 * ==========================================================================
1145 zio_read_bp_init(zio_t
*zio
)
1147 blkptr_t
*bp
= zio
->io_bp
;
1149 if (BP_GET_COMPRESS(bp
) != ZIO_COMPRESS_OFF
&&
1150 zio
->io_child_type
== ZIO_CHILD_LOGICAL
&&
1151 !(zio
->io_flags
& ZIO_FLAG_RAW
)) {
1153 BP_IS_EMBEDDED(bp
) ? BPE_GET_PSIZE(bp
) : BP_GET_PSIZE(bp
);
1154 void *cbuf
= zio_buf_alloc(psize
);
1156 zio_push_transform(zio
, cbuf
, psize
, psize
, zio_decompress
);
1159 if (BP_IS_EMBEDDED(bp
) && BPE_GET_ETYPE(bp
) == BP_EMBEDDED_TYPE_DATA
) {
1160 zio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
1161 decode_embedded_bp_compressed(bp
, zio
->io_data
);
1163 ASSERT(!BP_IS_EMBEDDED(bp
));
1166 if (!DMU_OT_IS_METADATA(BP_GET_TYPE(bp
)) && BP_GET_LEVEL(bp
) == 0)
1167 zio
->io_flags
|= ZIO_FLAG_DONT_CACHE
;
1169 if (BP_GET_TYPE(bp
) == DMU_OT_DDT_ZAP
)
1170 zio
->io_flags
|= ZIO_FLAG_DONT_CACHE
;
1172 if (BP_GET_DEDUP(bp
) && zio
->io_child_type
== ZIO_CHILD_LOGICAL
)
1173 zio
->io_pipeline
= ZIO_DDT_READ_PIPELINE
;
1175 return (ZIO_PIPELINE_CONTINUE
);
1179 zio_write_bp_init(zio_t
*zio
)
1182 if (!IO_IS_ALLOCATING(zio
))
1183 return (ZIO_PIPELINE_CONTINUE
);
1185 ASSERT(zio
->io_child_type
!= ZIO_CHILD_DDT
);
1187 if (zio
->io_bp_override
) {
1188 blkptr_t
*bp
= zio
->io_bp
;
1189 zio_prop_t
*zp
= &zio
->io_prop
;
1191 ASSERT(bp
->blk_birth
!= zio
->io_txg
);
1192 ASSERT(BP_GET_DEDUP(zio
->io_bp_override
) == 0);
1194 *bp
= *zio
->io_bp_override
;
1195 zio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
1197 if (BP_IS_EMBEDDED(bp
))
1198 return (ZIO_PIPELINE_CONTINUE
);
1201 * If we've been overridden and nopwrite is set then
1202 * set the flag accordingly to indicate that a nopwrite
1203 * has already occurred.
1205 if (!BP_IS_HOLE(bp
) && zp
->zp_nopwrite
) {
1206 ASSERT(!zp
->zp_dedup
);
1207 ASSERT3U(BP_GET_CHECKSUM(bp
), ==, zp
->zp_checksum
);
1208 zio
->io_flags
|= ZIO_FLAG_NOPWRITE
;
1209 return (ZIO_PIPELINE_CONTINUE
);
1212 ASSERT(!zp
->zp_nopwrite
);
1214 if (BP_IS_HOLE(bp
) || !zp
->zp_dedup
)
1215 return (ZIO_PIPELINE_CONTINUE
);
1217 ASSERT((zio_checksum_table
[zp
->zp_checksum
].ci_flags
&
1218 ZCHECKSUM_FLAG_DEDUP
) || zp
->zp_dedup_verify
);
1220 if (BP_GET_CHECKSUM(bp
) == zp
->zp_checksum
) {
1221 BP_SET_DEDUP(bp
, 1);
1222 zio
->io_pipeline
|= ZIO_STAGE_DDT_WRITE
;
1223 return (ZIO_PIPELINE_CONTINUE
);
1227 * We were unable to handle this as an override bp, treat
1228 * it as a regular write I/O.
1230 zio
->io_bp_override
= NULL
;
1231 *bp
= zio
->io_bp_orig
;
1232 zio
->io_pipeline
= zio
->io_orig_pipeline
;
1235 return (ZIO_PIPELINE_CONTINUE
);
1239 zio_write_compress(zio_t
*zio
)
1241 spa_t
*spa
= zio
->io_spa
;
1242 zio_prop_t
*zp
= &zio
->io_prop
;
1243 enum zio_compress compress
= zp
->zp_compress
;
1244 blkptr_t
*bp
= zio
->io_bp
;
1245 uint64_t lsize
= zio
->io_lsize
;
1246 uint64_t psize
= zio
->io_size
;
1249 EQUIV(lsize
!= psize
, (zio
->io_flags
& ZIO_FLAG_RAW
) != 0);
1252 * If our children haven't all reached the ready stage,
1253 * wait for them and then repeat this pipeline stage.
1255 if (zio_wait_for_children(zio
, ZIO_CHILD_GANG
, ZIO_WAIT_READY
) ||
1256 zio_wait_for_children(zio
, ZIO_CHILD_LOGICAL
, ZIO_WAIT_READY
))
1257 return (ZIO_PIPELINE_STOP
);
1259 if (!IO_IS_ALLOCATING(zio
))
1260 return (ZIO_PIPELINE_CONTINUE
);
1262 if (zio
->io_children_ready
!= NULL
) {
1264 * Now that all our children are ready, run the callback
1265 * associated with this zio in case it wants to modify the
1266 * data to be written.
1268 ASSERT3U(zp
->zp_level
, >, 0);
1269 zio
->io_children_ready(zio
);
1272 ASSERT(zio
->io_child_type
!= ZIO_CHILD_DDT
);
1273 ASSERT(zio
->io_bp_override
== NULL
);
1275 if (!BP_IS_HOLE(bp
) && bp
->blk_birth
== zio
->io_txg
) {
1277 * We're rewriting an existing block, which means we're
1278 * working on behalf of spa_sync(). For spa_sync() to
1279 * converge, it must eventually be the case that we don't
1280 * have to allocate new blocks. But compression changes
1281 * the blocksize, which forces a reallocate, and makes
1282 * convergence take longer. Therefore, after the first
1283 * few passes, stop compressing to ensure convergence.
1285 pass
= spa_sync_pass(spa
);
1287 ASSERT(zio
->io_txg
== spa_syncing_txg(spa
));
1288 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
1289 ASSERT(!BP_GET_DEDUP(bp
));
1291 if (pass
>= zfs_sync_pass_dont_compress
)
1292 compress
= ZIO_COMPRESS_OFF
;
1294 /* Make sure someone doesn't change their mind on overwrites */
1295 ASSERT(BP_IS_EMBEDDED(bp
) || MIN(zp
->zp_copies
+ BP_IS_GANG(bp
),
1296 spa_max_replication(spa
)) == BP_GET_NDVAS(bp
));
1299 /* If it's a compressed write that is not raw, compress the buffer. */
1300 if (compress
!= ZIO_COMPRESS_OFF
&& psize
== lsize
) {
1301 void *cbuf
= zio_buf_alloc(lsize
);
1302 psize
= zio_compress_data(compress
, zio
->io_data
, cbuf
, lsize
);
1303 if (psize
== 0 || psize
== lsize
) {
1304 compress
= ZIO_COMPRESS_OFF
;
1305 zio_buf_free(cbuf
, lsize
);
1306 } else if (!zp
->zp_dedup
&& psize
<= BPE_PAYLOAD_SIZE
&&
1307 zp
->zp_level
== 0 && !DMU_OT_HAS_FILL(zp
->zp_type
) &&
1308 spa_feature_is_enabled(spa
, SPA_FEATURE_EMBEDDED_DATA
)) {
1309 encode_embedded_bp_compressed(bp
,
1310 cbuf
, compress
, lsize
, psize
);
1311 BPE_SET_ETYPE(bp
, BP_EMBEDDED_TYPE_DATA
);
1312 BP_SET_TYPE(bp
, zio
->io_prop
.zp_type
);
1313 BP_SET_LEVEL(bp
, zio
->io_prop
.zp_level
);
1314 zio_buf_free(cbuf
, lsize
);
1315 bp
->blk_birth
= zio
->io_txg
;
1316 zio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
1317 ASSERT(spa_feature_is_active(spa
,
1318 SPA_FEATURE_EMBEDDED_DATA
));
1319 return (ZIO_PIPELINE_CONTINUE
);
1322 * Round up compressed size up to the ashift
1323 * of the smallest-ashift device, and zero the tail.
1324 * This ensures that the compressed size of the BP
1325 * (and thus compressratio property) are correct,
1326 * in that we charge for the padding used to fill out
1331 ASSERT3U(spa
->spa_min_ashift
, >=, SPA_MINBLOCKSHIFT
);
1333 rounded
= (size_t)P2ROUNDUP(psize
,
1334 1ULL << spa
->spa_min_ashift
);
1335 if (rounded
>= lsize
) {
1336 compress
= ZIO_COMPRESS_OFF
;
1337 zio_buf_free(cbuf
, lsize
);
1340 bzero((char *)cbuf
+ psize
, rounded
- psize
);
1342 zio_push_transform(zio
, cbuf
,
1343 psize
, lsize
, NULL
);
1348 * We were unable to handle this as an override bp, treat
1349 * it as a regular write I/O.
1351 zio
->io_bp_override
= NULL
;
1352 *bp
= zio
->io_bp_orig
;
1353 zio
->io_pipeline
= zio
->io_orig_pipeline
;
1356 ASSERT3U(psize
, !=, 0);
1361 * The final pass of spa_sync() must be all rewrites, but the first
1362 * few passes offer a trade-off: allocating blocks defers convergence,
1363 * but newly allocated blocks are sequential, so they can be written
1364 * to disk faster. Therefore, we allow the first few passes of
1365 * spa_sync() to allocate new blocks, but force rewrites after that.
1366 * There should only be a handful of blocks after pass 1 in any case.
1368 if (!BP_IS_HOLE(bp
) && bp
->blk_birth
== zio
->io_txg
&&
1369 BP_GET_PSIZE(bp
) == psize
&&
1370 pass
>= zfs_sync_pass_rewrite
) {
1371 enum zio_stage gang_stages
= zio
->io_pipeline
& ZIO_GANG_STAGES
;
1373 zio
->io_pipeline
= ZIO_REWRITE_PIPELINE
| gang_stages
;
1374 zio
->io_flags
|= ZIO_FLAG_IO_REWRITE
;
1377 zio
->io_pipeline
= ZIO_WRITE_PIPELINE
;
1381 if (zio
->io_bp_orig
.blk_birth
!= 0 &&
1382 spa_feature_is_active(spa
, SPA_FEATURE_HOLE_BIRTH
)) {
1383 BP_SET_LSIZE(bp
, lsize
);
1384 BP_SET_TYPE(bp
, zp
->zp_type
);
1385 BP_SET_LEVEL(bp
, zp
->zp_level
);
1386 BP_SET_BIRTH(bp
, zio
->io_txg
, 0);
1388 zio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
1390 ASSERT(zp
->zp_checksum
!= ZIO_CHECKSUM_GANG_HEADER
);
1391 BP_SET_LSIZE(bp
, lsize
);
1392 BP_SET_TYPE(bp
, zp
->zp_type
);
1393 BP_SET_LEVEL(bp
, zp
->zp_level
);
1394 BP_SET_PSIZE(bp
, psize
);
1395 BP_SET_COMPRESS(bp
, compress
);
1396 BP_SET_CHECKSUM(bp
, zp
->zp_checksum
);
1397 BP_SET_DEDUP(bp
, zp
->zp_dedup
);
1398 BP_SET_BYTEORDER(bp
, ZFS_HOST_BYTEORDER
);
1400 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
1401 ASSERT(!(zio
->io_flags
& ZIO_FLAG_IO_REWRITE
));
1402 zio
->io_pipeline
= ZIO_DDT_WRITE_PIPELINE
;
1404 if (zp
->zp_nopwrite
) {
1405 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
1406 ASSERT(!(zio
->io_flags
& ZIO_FLAG_IO_REWRITE
));
1407 zio
->io_pipeline
|= ZIO_STAGE_NOP_WRITE
;
1410 return (ZIO_PIPELINE_CONTINUE
);
1414 zio_free_bp_init(zio_t
*zio
)
1416 blkptr_t
*bp
= zio
->io_bp
;
1418 if (zio
->io_child_type
== ZIO_CHILD_LOGICAL
) {
1419 if (BP_GET_DEDUP(bp
))
1420 zio
->io_pipeline
= ZIO_DDT_FREE_PIPELINE
;
1423 return (ZIO_PIPELINE_CONTINUE
);
1427 * ==========================================================================
1428 * Execute the I/O pipeline
1429 * ==========================================================================
1433 zio_taskq_dispatch(zio_t
*zio
, zio_taskq_type_t q
, boolean_t cutinline
)
1435 spa_t
*spa
= zio
->io_spa
;
1436 zio_type_t t
= zio
->io_type
;
1437 int flags
= (cutinline
? TQ_FRONT
: 0);
1440 * If we're a config writer or a probe, the normal issue and
1441 * interrupt threads may all be blocked waiting for the config lock.
1442 * In this case, select the otherwise-unused taskq for ZIO_TYPE_NULL.
1444 if (zio
->io_flags
& (ZIO_FLAG_CONFIG_WRITER
| ZIO_FLAG_PROBE
))
1448 * A similar issue exists for the L2ARC write thread until L2ARC 2.0.
1450 if (t
== ZIO_TYPE_WRITE
&& zio
->io_vd
&& zio
->io_vd
->vdev_aux
)
1454 * If this is a high priority I/O, then use the high priority taskq if
1457 if (zio
->io_priority
== ZIO_PRIORITY_NOW
&&
1458 spa
->spa_zio_taskq
[t
][q
+ 1].stqs_count
!= 0)
1461 ASSERT3U(q
, <, ZIO_TASKQ_TYPES
);
1464 * NB: We are assuming that the zio can only be dispatched
1465 * to a single taskq at a time. It would be a grievous error
1466 * to dispatch the zio to another taskq at the same time.
1468 ASSERT(taskq_empty_ent(&zio
->io_tqent
));
1469 spa_taskq_dispatch_ent(spa
, t
, q
, (task_func_t
*)zio_execute
, zio
,
1470 flags
, &zio
->io_tqent
);
1474 zio_taskq_member(zio_t
*zio
, zio_taskq_type_t q
)
1476 kthread_t
*executor
= zio
->io_executor
;
1477 spa_t
*spa
= zio
->io_spa
;
1480 for (t
= 0; t
< ZIO_TYPES
; t
++) {
1481 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1483 for (i
= 0; i
< tqs
->stqs_count
; i
++) {
1484 if (taskq_member(tqs
->stqs_taskq
[i
], executor
))
1493 zio_issue_async(zio_t
*zio
)
1495 zio_taskq_dispatch(zio
, ZIO_TASKQ_ISSUE
, B_FALSE
);
1497 return (ZIO_PIPELINE_STOP
);
1501 zio_interrupt(zio_t
*zio
)
1503 zio_taskq_dispatch(zio
, ZIO_TASKQ_INTERRUPT
, B_FALSE
);
1507 zio_delay_interrupt(zio_t
*zio
)
1510 * The timeout_generic() function isn't defined in userspace, so
1511 * rather than trying to implement the function, the zio delay
1512 * functionality has been disabled for userspace builds.
1517 * If io_target_timestamp is zero, then no delay has been registered
1518 * for this IO, thus jump to the end of this function and "skip" the
1519 * delay; issuing it directly to the zio layer.
1521 if (zio
->io_target_timestamp
!= 0) {
1522 hrtime_t now
= gethrtime();
1524 if (now
>= zio
->io_target_timestamp
) {
1526 * This IO has already taken longer than the target
1527 * delay to complete, so we don't want to delay it
1528 * any longer; we "miss" the delay and issue it
1529 * directly to the zio layer. This is likely due to
1530 * the target latency being set to a value less than
1531 * the underlying hardware can satisfy (e.g. delay
1532 * set to 1ms, but the disks take 10ms to complete an
1536 DTRACE_PROBE2(zio__delay__miss
, zio_t
*, zio
,
1542 hrtime_t diff
= zio
->io_target_timestamp
- now
;
1543 clock_t expire_at_tick
= ddi_get_lbolt() +
1546 DTRACE_PROBE3(zio__delay__hit
, zio_t
*, zio
,
1547 hrtime_t
, now
, hrtime_t
, diff
);
1549 if (NSEC_TO_TICK(diff
) == 0) {
1550 /* Our delay is less than a jiffy - just spin */
1551 zfs_sleep_until(zio
->io_target_timestamp
);
1554 * Use taskq_dispatch_delay() in the place of
1555 * OpenZFS's timeout_generic().
1557 tid
= taskq_dispatch_delay(system_taskq
,
1558 (task_func_t
*) zio_interrupt
,
1559 zio
, TQ_NOSLEEP
, expire_at_tick
);
1562 * Couldn't allocate a task. Just
1563 * finish the zio without a delay.
1572 DTRACE_PROBE1(zio__delay__skip
, zio_t
*, zio
);
1577 * Execute the I/O pipeline until one of the following occurs:
1578 * (1) the I/O completes; (2) the pipeline stalls waiting for
1579 * dependent child I/Os; (3) the I/O issues, so we're waiting
1580 * for an I/O completion interrupt; (4) the I/O is delegated by
1581 * vdev-level caching or aggregation; (5) the I/O is deferred
1582 * due to vdev-level queueing; (6) the I/O is handed off to
1583 * another thread. In all cases, the pipeline stops whenever
1584 * there's no CPU work; it never burns a thread in cv_wait_io().
1586 * There's no locking on io_stage because there's no legitimate way
1587 * for multiple threads to be attempting to process the same I/O.
1589 static zio_pipe_stage_t
*zio_pipeline
[];
1592 * zio_execute() is a wrapper around the static function
1593 * __zio_execute() so that we can force __zio_execute() to be
1594 * inlined. This reduces stack overhead which is important
1595 * because __zio_execute() is called recursively in several zio
1596 * code paths. zio_execute() itself cannot be inlined because
1597 * it is externally visible.
1600 zio_execute(zio_t
*zio
)
1602 fstrans_cookie_t cookie
;
1604 cookie
= spl_fstrans_mark();
1606 spl_fstrans_unmark(cookie
);
1610 * Used to determine if in the current context the stack is sized large
1611 * enough to allow zio_execute() to be called recursively. A minimum
1612 * stack size of 16K is required to avoid needing to re-dispatch the zio.
1615 zio_execute_stack_check(zio_t
*zio
)
1617 #if !defined(HAVE_LARGE_STACKS)
1618 dsl_pool_t
*dp
= spa_get_dsl(zio
->io_spa
);
1620 /* Executing in txg_sync_thread() context. */
1621 if (dp
&& curthread
== dp
->dp_tx
.tx_sync_thread
)
1624 /* Pool initialization outside of zio_taskq context. */
1625 if (dp
&& spa_is_initializing(dp
->dp_spa
) &&
1626 !zio_taskq_member(zio
, ZIO_TASKQ_ISSUE
) &&
1627 !zio_taskq_member(zio
, ZIO_TASKQ_ISSUE_HIGH
))
1629 #endif /* HAVE_LARGE_STACKS */
1634 __attribute__((always_inline
))
1636 __zio_execute(zio_t
*zio
)
1638 zio
->io_executor
= curthread
;
1640 ASSERT3U(zio
->io_queued_timestamp
, >, 0);
1642 while (zio
->io_stage
< ZIO_STAGE_DONE
) {
1643 enum zio_stage pipeline
= zio
->io_pipeline
;
1644 enum zio_stage stage
= zio
->io_stage
;
1647 ASSERT(!MUTEX_HELD(&zio
->io_lock
));
1648 ASSERT(ISP2(stage
));
1649 ASSERT(zio
->io_stall
== NULL
);
1653 } while ((stage
& pipeline
) == 0);
1655 ASSERT(stage
<= ZIO_STAGE_DONE
);
1658 * If we are in interrupt context and this pipeline stage
1659 * will grab a config lock that is held across I/O,
1660 * or may wait for an I/O that needs an interrupt thread
1661 * to complete, issue async to avoid deadlock.
1663 * For VDEV_IO_START, we cut in line so that the io will
1664 * be sent to disk promptly.
1666 if ((stage
& ZIO_BLOCKING_STAGES
) && zio
->io_vd
== NULL
&&
1667 zio_taskq_member(zio
, ZIO_TASKQ_INTERRUPT
)) {
1668 boolean_t cut
= (stage
== ZIO_STAGE_VDEV_IO_START
) ?
1669 zio_requeue_io_start_cut_in_line
: B_FALSE
;
1670 zio_taskq_dispatch(zio
, ZIO_TASKQ_ISSUE
, cut
);
1675 * If the current context doesn't have large enough stacks
1676 * the zio must be issued asynchronously to prevent overflow.
1678 if (zio_execute_stack_check(zio
)) {
1679 boolean_t cut
= (stage
== ZIO_STAGE_VDEV_IO_START
) ?
1680 zio_requeue_io_start_cut_in_line
: B_FALSE
;
1681 zio_taskq_dispatch(zio
, ZIO_TASKQ_ISSUE
, cut
);
1685 zio
->io_stage
= stage
;
1686 zio
->io_pipeline_trace
|= zio
->io_stage
;
1687 rv
= zio_pipeline
[highbit64(stage
) - 1](zio
);
1689 if (rv
== ZIO_PIPELINE_STOP
)
1692 ASSERT(rv
== ZIO_PIPELINE_CONTINUE
);
1698 * ==========================================================================
1699 * Initiate I/O, either sync or async
1700 * ==========================================================================
1703 zio_wait(zio_t
*zio
)
1707 ASSERT(zio
->io_stage
== ZIO_STAGE_OPEN
);
1708 ASSERT(zio
->io_executor
== NULL
);
1710 zio
->io_waiter
= curthread
;
1711 ASSERT0(zio
->io_queued_timestamp
);
1712 zio
->io_queued_timestamp
= gethrtime();
1716 mutex_enter(&zio
->io_lock
);
1717 while (zio
->io_executor
!= NULL
)
1718 cv_wait_io(&zio
->io_cv
, &zio
->io_lock
);
1719 mutex_exit(&zio
->io_lock
);
1721 error
= zio
->io_error
;
1728 zio_nowait(zio_t
*zio
)
1730 ASSERT(zio
->io_executor
== NULL
);
1732 if (zio
->io_child_type
== ZIO_CHILD_LOGICAL
&&
1733 zio_unique_parent(zio
) == NULL
) {
1737 * This is a logical async I/O with no parent to wait for it.
1738 * We add it to the spa_async_root_zio "Godfather" I/O which
1739 * will ensure they complete prior to unloading the pool.
1741 spa_t
*spa
= zio
->io_spa
;
1743 pio
= spa
->spa_async_zio_root
[CPU_SEQID
];
1746 zio_add_child(pio
, zio
);
1749 ASSERT0(zio
->io_queued_timestamp
);
1750 zio
->io_queued_timestamp
= gethrtime();
1755 * ==========================================================================
1756 * Reexecute or suspend/resume failed I/O
1757 * ==========================================================================
1761 zio_reexecute(zio_t
*pio
)
1763 zio_t
*cio
, *cio_next
;
1765 zio_link_t
*zl
= NULL
;
1767 ASSERT(pio
->io_child_type
== ZIO_CHILD_LOGICAL
);
1768 ASSERT(pio
->io_orig_stage
== ZIO_STAGE_OPEN
);
1769 ASSERT(pio
->io_gang_leader
== NULL
);
1770 ASSERT(pio
->io_gang_tree
== NULL
);
1772 pio
->io_flags
= pio
->io_orig_flags
;
1773 pio
->io_stage
= pio
->io_orig_stage
;
1774 pio
->io_pipeline
= pio
->io_orig_pipeline
;
1775 pio
->io_reexecute
= 0;
1776 pio
->io_flags
|= ZIO_FLAG_REEXECUTED
;
1777 pio
->io_pipeline_trace
= 0;
1779 for (w
= 0; w
< ZIO_WAIT_TYPES
; w
++)
1780 pio
->io_state
[w
] = 0;
1781 for (c
= 0; c
< ZIO_CHILD_TYPES
; c
++)
1782 pio
->io_child_error
[c
] = 0;
1784 if (IO_IS_ALLOCATING(pio
))
1785 BP_ZERO(pio
->io_bp
);
1788 * As we reexecute pio's children, new children could be created.
1789 * New children go to the head of pio's io_child_list, however,
1790 * so we will (correctly) not reexecute them. The key is that
1791 * the remainder of pio's io_child_list, from 'cio_next' onward,
1792 * cannot be affected by any side effects of reexecuting 'cio'.
1794 for (cio
= zio_walk_children(pio
, &zl
); cio
!= NULL
; cio
= cio_next
) {
1795 cio_next
= zio_walk_children(pio
, &zl
);
1796 mutex_enter(&pio
->io_lock
);
1797 for (w
= 0; w
< ZIO_WAIT_TYPES
; w
++)
1798 pio
->io_children
[cio
->io_child_type
][w
]++;
1799 mutex_exit(&pio
->io_lock
);
1804 * Now that all children have been reexecuted, execute the parent.
1805 * We don't reexecute "The Godfather" I/O here as it's the
1806 * responsibility of the caller to wait on him.
1808 if (!(pio
->io_flags
& ZIO_FLAG_GODFATHER
)) {
1809 pio
->io_queued_timestamp
= gethrtime();
1815 zio_suspend(spa_t
*spa
, zio_t
*zio
)
1817 if (spa_get_failmode(spa
) == ZIO_FAILURE_MODE_PANIC
)
1818 fm_panic("Pool '%s' has encountered an uncorrectable I/O "
1819 "failure and the failure mode property for this pool "
1820 "is set to panic.", spa_name(spa
));
1822 cmn_err(CE_WARN
, "Pool '%s' has encountered an uncorrectable I/O "
1823 "failure and has been suspended.\n", spa_name(spa
));
1825 zfs_ereport_post(FM_EREPORT_ZFS_IO_FAILURE
, spa
, NULL
, NULL
, 0, 0);
1827 mutex_enter(&spa
->spa_suspend_lock
);
1829 if (spa
->spa_suspend_zio_root
== NULL
)
1830 spa
->spa_suspend_zio_root
= zio_root(spa
, NULL
, NULL
,
1831 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
1832 ZIO_FLAG_GODFATHER
);
1834 spa
->spa_suspended
= B_TRUE
;
1837 ASSERT(!(zio
->io_flags
& ZIO_FLAG_GODFATHER
));
1838 ASSERT(zio
!= spa
->spa_suspend_zio_root
);
1839 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
1840 ASSERT(zio_unique_parent(zio
) == NULL
);
1841 ASSERT(zio
->io_stage
== ZIO_STAGE_DONE
);
1842 zio_add_child(spa
->spa_suspend_zio_root
, zio
);
1845 mutex_exit(&spa
->spa_suspend_lock
);
1849 zio_resume(spa_t
*spa
)
1854 * Reexecute all previously suspended i/o.
1856 mutex_enter(&spa
->spa_suspend_lock
);
1857 spa
->spa_suspended
= B_FALSE
;
1858 cv_broadcast(&spa
->spa_suspend_cv
);
1859 pio
= spa
->spa_suspend_zio_root
;
1860 spa
->spa_suspend_zio_root
= NULL
;
1861 mutex_exit(&spa
->spa_suspend_lock
);
1867 return (zio_wait(pio
));
1871 zio_resume_wait(spa_t
*spa
)
1873 mutex_enter(&spa
->spa_suspend_lock
);
1874 while (spa_suspended(spa
))
1875 cv_wait(&spa
->spa_suspend_cv
, &spa
->spa_suspend_lock
);
1876 mutex_exit(&spa
->spa_suspend_lock
);
1880 * ==========================================================================
1883 * A gang block is a collection of small blocks that looks to the DMU
1884 * like one large block. When zio_dva_allocate() cannot find a block
1885 * of the requested size, due to either severe fragmentation or the pool
1886 * being nearly full, it calls zio_write_gang_block() to construct the
1887 * block from smaller fragments.
1889 * A gang block consists of a gang header (zio_gbh_phys_t) and up to
1890 * three (SPA_GBH_NBLKPTRS) gang members. The gang header is just like
1891 * an indirect block: it's an array of block pointers. It consumes
1892 * only one sector and hence is allocatable regardless of fragmentation.
1893 * The gang header's bps point to its gang members, which hold the data.
1895 * Gang blocks are self-checksumming, using the bp's <vdev, offset, txg>
1896 * as the verifier to ensure uniqueness of the SHA256 checksum.
1897 * Critically, the gang block bp's blk_cksum is the checksum of the data,
1898 * not the gang header. This ensures that data block signatures (needed for
1899 * deduplication) are independent of how the block is physically stored.
1901 * Gang blocks can be nested: a gang member may itself be a gang block.
1902 * Thus every gang block is a tree in which root and all interior nodes are
1903 * gang headers, and the leaves are normal blocks that contain user data.
1904 * The root of the gang tree is called the gang leader.
1906 * To perform any operation (read, rewrite, free, claim) on a gang block,
1907 * zio_gang_assemble() first assembles the gang tree (minus data leaves)
1908 * in the io_gang_tree field of the original logical i/o by recursively
1909 * reading the gang leader and all gang headers below it. This yields
1910 * an in-core tree containing the contents of every gang header and the
1911 * bps for every constituent of the gang block.
1913 * With the gang tree now assembled, zio_gang_issue() just walks the gang tree
1914 * and invokes a callback on each bp. To free a gang block, zio_gang_issue()
1915 * calls zio_free_gang() -- a trivial wrapper around zio_free() -- for each bp.
1916 * zio_claim_gang() provides a similarly trivial wrapper for zio_claim().
1917 * zio_read_gang() is a wrapper around zio_read() that omits reading gang
1918 * headers, since we already have those in io_gang_tree. zio_rewrite_gang()
1919 * performs a zio_rewrite() of the data or, for gang headers, a zio_rewrite()
1920 * of the gang header plus zio_checksum_compute() of the data to update the
1921 * gang header's blk_cksum as described above.
1923 * The two-phase assemble/issue model solves the problem of partial failure --
1924 * what if you'd freed part of a gang block but then couldn't read the
1925 * gang header for another part? Assembling the entire gang tree first
1926 * ensures that all the necessary gang header I/O has succeeded before
1927 * starting the actual work of free, claim, or write. Once the gang tree
1928 * is assembled, free and claim are in-memory operations that cannot fail.
1930 * In the event that a gang write fails, zio_dva_unallocate() walks the
1931 * gang tree to immediately free (i.e. insert back into the space map)
1932 * everything we've allocated. This ensures that we don't get ENOSPC
1933 * errors during repeated suspend/resume cycles due to a flaky device.
1935 * Gang rewrites only happen during sync-to-convergence. If we can't assemble
1936 * the gang tree, we won't modify the block, so we can safely defer the free
1937 * (knowing that the block is still intact). If we *can* assemble the gang
1938 * tree, then even if some of the rewrites fail, zio_dva_unallocate() will free
1939 * each constituent bp and we can allocate a new block on the next sync pass.
1941 * In all cases, the gang tree allows complete recovery from partial failure.
1942 * ==========================================================================
1946 zio_read_gang(zio_t
*pio
, blkptr_t
*bp
, zio_gang_node_t
*gn
, void *data
)
1951 return (zio_read(pio
, pio
->io_spa
, bp
, data
, BP_GET_PSIZE(bp
),
1952 NULL
, NULL
, pio
->io_priority
, ZIO_GANG_CHILD_FLAGS(pio
),
1953 &pio
->io_bookmark
));
1957 zio_rewrite_gang(zio_t
*pio
, blkptr_t
*bp
, zio_gang_node_t
*gn
, void *data
)
1962 zio
= zio_rewrite(pio
, pio
->io_spa
, pio
->io_txg
, bp
,
1963 gn
->gn_gbh
, SPA_GANGBLOCKSIZE
, NULL
, NULL
, pio
->io_priority
,
1964 ZIO_GANG_CHILD_FLAGS(pio
), &pio
->io_bookmark
);
1966 * As we rewrite each gang header, the pipeline will compute
1967 * a new gang block header checksum for it; but no one will
1968 * compute a new data checksum, so we do that here. The one
1969 * exception is the gang leader: the pipeline already computed
1970 * its data checksum because that stage precedes gang assembly.
1971 * (Presently, nothing actually uses interior data checksums;
1972 * this is just good hygiene.)
1974 if (gn
!= pio
->io_gang_leader
->io_gang_tree
) {
1975 zio_checksum_compute(zio
, BP_GET_CHECKSUM(bp
),
1976 data
, BP_GET_PSIZE(bp
));
1979 * If we are here to damage data for testing purposes,
1980 * leave the GBH alone so that we can detect the damage.
1982 if (pio
->io_gang_leader
->io_flags
& ZIO_FLAG_INDUCE_DAMAGE
)
1983 zio
->io_pipeline
&= ~ZIO_VDEV_IO_STAGES
;
1985 zio
= zio_rewrite(pio
, pio
->io_spa
, pio
->io_txg
, bp
,
1986 data
, BP_GET_PSIZE(bp
), NULL
, NULL
, pio
->io_priority
,
1987 ZIO_GANG_CHILD_FLAGS(pio
), &pio
->io_bookmark
);
1995 zio_free_gang(zio_t
*pio
, blkptr_t
*bp
, zio_gang_node_t
*gn
, void *data
)
1997 return (zio_free_sync(pio
, pio
->io_spa
, pio
->io_txg
, bp
,
1998 ZIO_GANG_CHILD_FLAGS(pio
)));
2003 zio_claim_gang(zio_t
*pio
, blkptr_t
*bp
, zio_gang_node_t
*gn
, void *data
)
2005 return (zio_claim(pio
, pio
->io_spa
, pio
->io_txg
, bp
,
2006 NULL
, NULL
, ZIO_GANG_CHILD_FLAGS(pio
)));
2009 static zio_gang_issue_func_t
*zio_gang_issue_func
[ZIO_TYPES
] = {
2018 static void zio_gang_tree_assemble_done(zio_t
*zio
);
2020 static zio_gang_node_t
*
2021 zio_gang_node_alloc(zio_gang_node_t
**gnpp
)
2023 zio_gang_node_t
*gn
;
2025 ASSERT(*gnpp
== NULL
);
2027 gn
= kmem_zalloc(sizeof (*gn
), KM_SLEEP
);
2028 gn
->gn_gbh
= zio_buf_alloc(SPA_GANGBLOCKSIZE
);
2035 zio_gang_node_free(zio_gang_node_t
**gnpp
)
2037 zio_gang_node_t
*gn
= *gnpp
;
2040 for (g
= 0; g
< SPA_GBH_NBLKPTRS
; g
++)
2041 ASSERT(gn
->gn_child
[g
] == NULL
);
2043 zio_buf_free(gn
->gn_gbh
, SPA_GANGBLOCKSIZE
);
2044 kmem_free(gn
, sizeof (*gn
));
2049 zio_gang_tree_free(zio_gang_node_t
**gnpp
)
2051 zio_gang_node_t
*gn
= *gnpp
;
2057 for (g
= 0; g
< SPA_GBH_NBLKPTRS
; g
++)
2058 zio_gang_tree_free(&gn
->gn_child
[g
]);
2060 zio_gang_node_free(gnpp
);
2064 zio_gang_tree_assemble(zio_t
*gio
, blkptr_t
*bp
, zio_gang_node_t
**gnpp
)
2066 zio_gang_node_t
*gn
= zio_gang_node_alloc(gnpp
);
2068 ASSERT(gio
->io_gang_leader
== gio
);
2069 ASSERT(BP_IS_GANG(bp
));
2071 zio_nowait(zio_read(gio
, gio
->io_spa
, bp
, gn
->gn_gbh
,
2072 SPA_GANGBLOCKSIZE
, zio_gang_tree_assemble_done
, gn
,
2073 gio
->io_priority
, ZIO_GANG_CHILD_FLAGS(gio
), &gio
->io_bookmark
));
2077 zio_gang_tree_assemble_done(zio_t
*zio
)
2079 zio_t
*gio
= zio
->io_gang_leader
;
2080 zio_gang_node_t
*gn
= zio
->io_private
;
2081 blkptr_t
*bp
= zio
->io_bp
;
2084 ASSERT(gio
== zio_unique_parent(zio
));
2085 ASSERT(zio
->io_child_count
== 0);
2090 if (BP_SHOULD_BYTESWAP(bp
))
2091 byteswap_uint64_array(zio
->io_data
, zio
->io_size
);
2093 ASSERT(zio
->io_data
== gn
->gn_gbh
);
2094 ASSERT(zio
->io_size
== SPA_GANGBLOCKSIZE
);
2095 ASSERT(gn
->gn_gbh
->zg_tail
.zec_magic
== ZEC_MAGIC
);
2097 for (g
= 0; g
< SPA_GBH_NBLKPTRS
; g
++) {
2098 blkptr_t
*gbp
= &gn
->gn_gbh
->zg_blkptr
[g
];
2099 if (!BP_IS_GANG(gbp
))
2101 zio_gang_tree_assemble(gio
, gbp
, &gn
->gn_child
[g
]);
2106 zio_gang_tree_issue(zio_t
*pio
, zio_gang_node_t
*gn
, blkptr_t
*bp
, void *data
)
2108 zio_t
*gio
= pio
->io_gang_leader
;
2112 ASSERT(BP_IS_GANG(bp
) == !!gn
);
2113 ASSERT(BP_GET_CHECKSUM(bp
) == BP_GET_CHECKSUM(gio
->io_bp
));
2114 ASSERT(BP_GET_LSIZE(bp
) == BP_GET_PSIZE(bp
) || gn
== gio
->io_gang_tree
);
2117 * If you're a gang header, your data is in gn->gn_gbh.
2118 * If you're a gang member, your data is in 'data' and gn == NULL.
2120 zio
= zio_gang_issue_func
[gio
->io_type
](pio
, bp
, gn
, data
);
2123 ASSERT(gn
->gn_gbh
->zg_tail
.zec_magic
== ZEC_MAGIC
);
2125 for (g
= 0; g
< SPA_GBH_NBLKPTRS
; g
++) {
2126 blkptr_t
*gbp
= &gn
->gn_gbh
->zg_blkptr
[g
];
2127 if (BP_IS_HOLE(gbp
))
2129 zio_gang_tree_issue(zio
, gn
->gn_child
[g
], gbp
, data
);
2130 data
= (char *)data
+ BP_GET_PSIZE(gbp
);
2134 if (gn
== gio
->io_gang_tree
)
2135 ASSERT3P((char *)gio
->io_data
+ gio
->io_size
, ==, data
);
2142 zio_gang_assemble(zio_t
*zio
)
2144 blkptr_t
*bp
= zio
->io_bp
;
2146 ASSERT(BP_IS_GANG(bp
) && zio
->io_gang_leader
== NULL
);
2147 ASSERT(zio
->io_child_type
> ZIO_CHILD_GANG
);
2149 zio
->io_gang_leader
= zio
;
2151 zio_gang_tree_assemble(zio
, bp
, &zio
->io_gang_tree
);
2153 return (ZIO_PIPELINE_CONTINUE
);
2157 zio_gang_issue(zio_t
*zio
)
2159 blkptr_t
*bp
= zio
->io_bp
;
2161 if (zio_wait_for_children(zio
, ZIO_CHILD_GANG
, ZIO_WAIT_DONE
))
2162 return (ZIO_PIPELINE_STOP
);
2164 ASSERT(BP_IS_GANG(bp
) && zio
->io_gang_leader
== zio
);
2165 ASSERT(zio
->io_child_type
> ZIO_CHILD_GANG
);
2167 if (zio
->io_child_error
[ZIO_CHILD_GANG
] == 0)
2168 zio_gang_tree_issue(zio
, zio
->io_gang_tree
, bp
, zio
->io_data
);
2170 zio_gang_tree_free(&zio
->io_gang_tree
);
2172 zio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
2174 return (ZIO_PIPELINE_CONTINUE
);
2178 zio_write_gang_member_ready(zio_t
*zio
)
2180 zio_t
*pio
= zio_unique_parent(zio
);
2181 dva_t
*cdva
= zio
->io_bp
->blk_dva
;
2182 dva_t
*pdva
= pio
->io_bp
->blk_dva
;
2185 ASSERTV(zio_t
*gio
= zio
->io_gang_leader
);
2187 if (BP_IS_HOLE(zio
->io_bp
))
2190 ASSERT(BP_IS_HOLE(&zio
->io_bp_orig
));
2192 ASSERT(zio
->io_child_type
== ZIO_CHILD_GANG
);
2193 ASSERT3U(zio
->io_prop
.zp_copies
, ==, gio
->io_prop
.zp_copies
);
2194 ASSERT3U(zio
->io_prop
.zp_copies
, <=, BP_GET_NDVAS(zio
->io_bp
));
2195 ASSERT3U(pio
->io_prop
.zp_copies
, <=, BP_GET_NDVAS(pio
->io_bp
));
2196 ASSERT3U(BP_GET_NDVAS(zio
->io_bp
), <=, BP_GET_NDVAS(pio
->io_bp
));
2198 mutex_enter(&pio
->io_lock
);
2199 for (d
= 0; d
< BP_GET_NDVAS(zio
->io_bp
); d
++) {
2200 ASSERT(DVA_GET_GANG(&pdva
[d
]));
2201 asize
= DVA_GET_ASIZE(&pdva
[d
]);
2202 asize
+= DVA_GET_ASIZE(&cdva
[d
]);
2203 DVA_SET_ASIZE(&pdva
[d
], asize
);
2205 mutex_exit(&pio
->io_lock
);
2209 zio_write_gang_block(zio_t
*pio
)
2211 spa_t
*spa
= pio
->io_spa
;
2212 metaslab_class_t
*mc
= spa_normal_class(spa
);
2213 blkptr_t
*bp
= pio
->io_bp
;
2214 zio_t
*gio
= pio
->io_gang_leader
;
2216 zio_gang_node_t
*gn
, **gnpp
;
2217 zio_gbh_phys_t
*gbh
;
2218 uint64_t txg
= pio
->io_txg
;
2219 uint64_t resid
= pio
->io_size
;
2221 int copies
= gio
->io_prop
.zp_copies
;
2222 int gbh_copies
= MIN(copies
+ 1, spa_max_replication(spa
));
2226 int flags
= METASLAB_HINTBP_FAVOR
| METASLAB_GANG_HEADER
;
2227 if (pio
->io_flags
& ZIO_FLAG_IO_ALLOCATING
) {
2228 ASSERT(pio
->io_priority
== ZIO_PRIORITY_ASYNC_WRITE
);
2229 ASSERT(!(pio
->io_flags
& ZIO_FLAG_NODATA
));
2231 flags
|= METASLAB_ASYNC_ALLOC
;
2232 VERIFY(refcount_held(&mc
->mc_alloc_slots
, pio
));
2235 * The logical zio has already placed a reservation for
2236 * 'copies' allocation slots but gang blocks may require
2237 * additional copies. These additional copies
2238 * (i.e. gbh_copies - copies) are guaranteed to succeed
2239 * since metaslab_class_throttle_reserve() always allows
2240 * additional reservations for gang blocks.
2242 VERIFY(metaslab_class_throttle_reserve(mc
, gbh_copies
- copies
,
2246 error
= metaslab_alloc(spa
, mc
, SPA_GANGBLOCKSIZE
,
2247 bp
, gbh_copies
, txg
, pio
== gio
? NULL
: gio
->io_bp
, flags
, pio
);
2249 if (pio
->io_flags
& ZIO_FLAG_IO_ALLOCATING
) {
2250 ASSERT(pio
->io_priority
== ZIO_PRIORITY_ASYNC_WRITE
);
2251 ASSERT(!(pio
->io_flags
& ZIO_FLAG_NODATA
));
2254 * If we failed to allocate the gang block header then
2255 * we remove any additional allocation reservations that
2256 * we placed here. The original reservation will
2257 * be removed when the logical I/O goes to the ready
2260 metaslab_class_throttle_unreserve(mc
,
2261 gbh_copies
- copies
, pio
);
2264 pio
->io_error
= error
;
2265 return (ZIO_PIPELINE_CONTINUE
);
2269 gnpp
= &gio
->io_gang_tree
;
2271 gnpp
= pio
->io_private
;
2272 ASSERT(pio
->io_ready
== zio_write_gang_member_ready
);
2275 gn
= zio_gang_node_alloc(gnpp
);
2277 bzero(gbh
, SPA_GANGBLOCKSIZE
);
2280 * Create the gang header.
2282 zio
= zio_rewrite(pio
, spa
, txg
, bp
, gbh
, SPA_GANGBLOCKSIZE
, NULL
, NULL
,
2283 pio
->io_priority
, ZIO_GANG_CHILD_FLAGS(pio
), &pio
->io_bookmark
);
2286 * Create and nowait the gang children.
2288 for (g
= 0; resid
!= 0; resid
-= lsize
, g
++) {
2291 lsize
= P2ROUNDUP(resid
/ (SPA_GBH_NBLKPTRS
- g
),
2293 ASSERT(lsize
>= SPA_MINBLOCKSIZE
&& lsize
<= resid
);
2295 zp
.zp_checksum
= gio
->io_prop
.zp_checksum
;
2296 zp
.zp_compress
= ZIO_COMPRESS_OFF
;
2297 zp
.zp_type
= DMU_OT_NONE
;
2299 zp
.zp_copies
= gio
->io_prop
.zp_copies
;
2300 zp
.zp_dedup
= B_FALSE
;
2301 zp
.zp_dedup_verify
= B_FALSE
;
2302 zp
.zp_nopwrite
= B_FALSE
;
2304 cio
= zio_write(zio
, spa
, txg
, &gbh
->zg_blkptr
[g
],
2305 (char *)pio
->io_data
+ (pio
->io_size
- resid
), lsize
,
2306 lsize
, &zp
, zio_write_gang_member_ready
, NULL
, NULL
, NULL
,
2307 &gn
->gn_child
[g
], pio
->io_priority
,
2308 ZIO_GANG_CHILD_FLAGS(pio
), &pio
->io_bookmark
);
2310 if (pio
->io_flags
& ZIO_FLAG_IO_ALLOCATING
) {
2311 ASSERT(pio
->io_priority
== ZIO_PRIORITY_ASYNC_WRITE
);
2312 ASSERT(!(pio
->io_flags
& ZIO_FLAG_NODATA
));
2315 * Gang children won't throttle but we should
2316 * account for their work, so reserve an allocation
2317 * slot for them here.
2319 VERIFY(metaslab_class_throttle_reserve(mc
,
2320 zp
.zp_copies
, cio
, flags
));
2327 * Set pio's pipeline to just wait for zio to finish.
2329 pio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
2332 * We didn't allocate this bp, so make sure it doesn't get unmarked.
2334 pio
->io_flags
&= ~ZIO_FLAG_FASTWRITE
;
2338 return (ZIO_PIPELINE_CONTINUE
);
2342 * The zio_nop_write stage in the pipeline determines if allocating a
2343 * new bp is necessary. The nopwrite feature can handle writes in
2344 * either syncing or open context (i.e. zil writes) and as a result is
2345 * mutually exclusive with dedup.
2347 * By leveraging a cryptographically secure checksum, such as SHA256, we
2348 * can compare the checksums of the new data and the old to determine if
2349 * allocating a new block is required. Note that our requirements for
2350 * cryptographic strength are fairly weak: there can't be any accidental
2351 * hash collisions, but we don't need to be secure against intentional
2352 * (malicious) collisions. To trigger a nopwrite, you have to be able
2353 * to write the file to begin with, and triggering an incorrect (hash
2354 * collision) nopwrite is no worse than simply writing to the file.
2355 * That said, there are no known attacks against the checksum algorithms
2356 * used for nopwrite, assuming that the salt and the checksums
2357 * themselves remain secret.
2360 zio_nop_write(zio_t
*zio
)
2362 blkptr_t
*bp
= zio
->io_bp
;
2363 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
2364 zio_prop_t
*zp
= &zio
->io_prop
;
2366 ASSERT(BP_GET_LEVEL(bp
) == 0);
2367 ASSERT(!(zio
->io_flags
& ZIO_FLAG_IO_REWRITE
));
2368 ASSERT(zp
->zp_nopwrite
);
2369 ASSERT(!zp
->zp_dedup
);
2370 ASSERT(zio
->io_bp_override
== NULL
);
2371 ASSERT(IO_IS_ALLOCATING(zio
));
2374 * Check to see if the original bp and the new bp have matching
2375 * characteristics (i.e. same checksum, compression algorithms, etc).
2376 * If they don't then just continue with the pipeline which will
2377 * allocate a new bp.
2379 if (BP_IS_HOLE(bp_orig
) ||
2380 !(zio_checksum_table
[BP_GET_CHECKSUM(bp
)].ci_flags
&
2381 ZCHECKSUM_FLAG_NOPWRITE
) ||
2382 BP_GET_CHECKSUM(bp
) != BP_GET_CHECKSUM(bp_orig
) ||
2383 BP_GET_COMPRESS(bp
) != BP_GET_COMPRESS(bp_orig
) ||
2384 BP_GET_DEDUP(bp
) != BP_GET_DEDUP(bp_orig
) ||
2385 zp
->zp_copies
!= BP_GET_NDVAS(bp_orig
))
2386 return (ZIO_PIPELINE_CONTINUE
);
2389 * If the checksums match then reset the pipeline so that we
2390 * avoid allocating a new bp and issuing any I/O.
2392 if (ZIO_CHECKSUM_EQUAL(bp
->blk_cksum
, bp_orig
->blk_cksum
)) {
2393 ASSERT(zio_checksum_table
[zp
->zp_checksum
].ci_flags
&
2394 ZCHECKSUM_FLAG_NOPWRITE
);
2395 ASSERT3U(BP_GET_PSIZE(bp
), ==, BP_GET_PSIZE(bp_orig
));
2396 ASSERT3U(BP_GET_LSIZE(bp
), ==, BP_GET_LSIZE(bp_orig
));
2397 ASSERT(zp
->zp_compress
!= ZIO_COMPRESS_OFF
);
2398 ASSERT(bcmp(&bp
->blk_prop
, &bp_orig
->blk_prop
,
2399 sizeof (uint64_t)) == 0);
2402 zio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
2403 zio
->io_flags
|= ZIO_FLAG_NOPWRITE
;
2406 return (ZIO_PIPELINE_CONTINUE
);
2410 * ==========================================================================
2412 * ==========================================================================
2415 zio_ddt_child_read_done(zio_t
*zio
)
2417 blkptr_t
*bp
= zio
->io_bp
;
2418 ddt_entry_t
*dde
= zio
->io_private
;
2420 zio_t
*pio
= zio_unique_parent(zio
);
2422 mutex_enter(&pio
->io_lock
);
2423 ddp
= ddt_phys_select(dde
, bp
);
2424 if (zio
->io_error
== 0)
2425 ddt_phys_clear(ddp
); /* this ddp doesn't need repair */
2426 if (zio
->io_error
== 0 && dde
->dde_repair_data
== NULL
)
2427 dde
->dde_repair_data
= zio
->io_data
;
2429 zio_buf_free(zio
->io_data
, zio
->io_size
);
2430 mutex_exit(&pio
->io_lock
);
2434 zio_ddt_read_start(zio_t
*zio
)
2436 blkptr_t
*bp
= zio
->io_bp
;
2439 ASSERT(BP_GET_DEDUP(bp
));
2440 ASSERT(BP_GET_PSIZE(bp
) == zio
->io_size
);
2441 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
2443 if (zio
->io_child_error
[ZIO_CHILD_DDT
]) {
2444 ddt_t
*ddt
= ddt_select(zio
->io_spa
, bp
);
2445 ddt_entry_t
*dde
= ddt_repair_start(ddt
, bp
);
2446 ddt_phys_t
*ddp
= dde
->dde_phys
;
2447 ddt_phys_t
*ddp_self
= ddt_phys_select(dde
, bp
);
2450 ASSERT(zio
->io_vsd
== NULL
);
2453 if (ddp_self
== NULL
)
2454 return (ZIO_PIPELINE_CONTINUE
);
2456 for (p
= 0; p
< DDT_PHYS_TYPES
; p
++, ddp
++) {
2457 if (ddp
->ddp_phys_birth
== 0 || ddp
== ddp_self
)
2459 ddt_bp_create(ddt
->ddt_checksum
, &dde
->dde_key
, ddp
,
2461 zio_nowait(zio_read(zio
, zio
->io_spa
, &blk
,
2462 zio_buf_alloc(zio
->io_size
), zio
->io_size
,
2463 zio_ddt_child_read_done
, dde
, zio
->io_priority
,
2464 ZIO_DDT_CHILD_FLAGS(zio
) | ZIO_FLAG_DONT_PROPAGATE
,
2465 &zio
->io_bookmark
));
2467 return (ZIO_PIPELINE_CONTINUE
);
2470 zio_nowait(zio_read(zio
, zio
->io_spa
, bp
,
2471 zio
->io_data
, zio
->io_size
, NULL
, NULL
, zio
->io_priority
,
2472 ZIO_DDT_CHILD_FLAGS(zio
), &zio
->io_bookmark
));
2474 return (ZIO_PIPELINE_CONTINUE
);
2478 zio_ddt_read_done(zio_t
*zio
)
2480 blkptr_t
*bp
= zio
->io_bp
;
2482 if (zio_wait_for_children(zio
, ZIO_CHILD_DDT
, ZIO_WAIT_DONE
))
2483 return (ZIO_PIPELINE_STOP
);
2485 ASSERT(BP_GET_DEDUP(bp
));
2486 ASSERT(BP_GET_PSIZE(bp
) == zio
->io_size
);
2487 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
2489 if (zio
->io_child_error
[ZIO_CHILD_DDT
]) {
2490 ddt_t
*ddt
= ddt_select(zio
->io_spa
, bp
);
2491 ddt_entry_t
*dde
= zio
->io_vsd
;
2493 ASSERT(spa_load_state(zio
->io_spa
) != SPA_LOAD_NONE
);
2494 return (ZIO_PIPELINE_CONTINUE
);
2497 zio
->io_stage
= ZIO_STAGE_DDT_READ_START
>> 1;
2498 zio_taskq_dispatch(zio
, ZIO_TASKQ_ISSUE
, B_FALSE
);
2499 return (ZIO_PIPELINE_STOP
);
2501 if (dde
->dde_repair_data
!= NULL
) {
2502 bcopy(dde
->dde_repair_data
, zio
->io_data
, zio
->io_size
);
2503 zio
->io_child_error
[ZIO_CHILD_DDT
] = 0;
2505 ddt_repair_done(ddt
, dde
);
2509 ASSERT(zio
->io_vsd
== NULL
);
2511 return (ZIO_PIPELINE_CONTINUE
);
2515 zio_ddt_collision(zio_t
*zio
, ddt_t
*ddt
, ddt_entry_t
*dde
)
2517 spa_t
*spa
= zio
->io_spa
;
2519 boolean_t do_raw
= !!(zio
->io_flags
& ZIO_FLAG_RAW
);
2521 ASSERT(!(zio
->io_bp_override
&& do_raw
));
2524 * Note: we compare the original data, not the transformed data,
2525 * because when zio->io_bp is an override bp, we will not have
2526 * pushed the I/O transforms. That's an important optimization
2527 * because otherwise we'd compress/encrypt all dmu_sync() data twice.
2528 * However, we should never get a raw, override zio so in these
2529 * cases we can compare the io_data directly. This is useful because
2530 * it allows us to do dedup verification even if we don't have access
2531 * to the original data (for instance, if the encryption keys aren't
2535 for (p
= DDT_PHYS_SINGLE
; p
<= DDT_PHYS_TRIPLE
; p
++) {
2536 zio_t
*lio
= dde
->dde_lead_zio
[p
];
2538 if (lio
!= NULL
&& do_raw
) {
2539 return (lio
->io_size
!= zio
->io_size
||
2540 bcmp(zio
->io_data
, lio
->io_data
,
2541 zio
->io_size
) != 0);
2542 } else if (lio
!= NULL
) {
2543 return (lio
->io_orig_size
!= zio
->io_orig_size
||
2544 bcmp(zio
->io_orig_data
, lio
->io_orig_data
,
2545 zio
->io_orig_size
) != 0);
2549 for (p
= DDT_PHYS_SINGLE
; p
<= DDT_PHYS_TRIPLE
; p
++) {
2550 ddt_phys_t
*ddp
= &dde
->dde_phys
[p
];
2552 if (ddp
->ddp_phys_birth
!= 0 && do_raw
) {
2553 blkptr_t blk
= *zio
->io_bp
;
2558 ddt_bp_fill(ddp
, &blk
, ddp
->ddp_phys_birth
);
2559 psize
= BP_GET_PSIZE(&blk
);
2561 if (psize
!= zio
->io_size
)
2566 tmpbuf
= zio_buf_alloc(psize
);
2568 error
= zio_wait(zio_read(NULL
, spa
, &blk
, tmpbuf
,
2569 psize
, NULL
, NULL
, ZIO_PRIORITY_SYNC_READ
,
2570 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
2571 ZIO_FLAG_RAW
, &zio
->io_bookmark
));
2574 if (bcmp(tmpbuf
, zio
->io_data
, psize
) != 0)
2575 error
= SET_ERROR(ENOENT
);
2578 zio_buf_free(tmpbuf
, psize
);
2580 return (error
!= 0);
2581 } else if (ddp
->ddp_phys_birth
!= 0) {
2582 arc_buf_t
*abuf
= NULL
;
2583 arc_flags_t aflags
= ARC_FLAG_WAIT
;
2584 blkptr_t blk
= *zio
->io_bp
;
2587 ddt_bp_fill(ddp
, &blk
, ddp
->ddp_phys_birth
);
2589 if (BP_GET_LSIZE(&blk
) != zio
->io_orig_size
)
2594 error
= arc_read(NULL
, spa
, &blk
,
2595 arc_getbuf_func
, &abuf
, ZIO_PRIORITY_SYNC_READ
,
2596 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
2597 &aflags
, &zio
->io_bookmark
);
2600 if (bcmp(abuf
->b_data
, zio
->io_orig_data
,
2601 zio
->io_orig_size
) != 0)
2602 error
= SET_ERROR(ENOENT
);
2603 arc_buf_destroy(abuf
, &abuf
);
2607 return (error
!= 0);
2615 zio_ddt_child_write_ready(zio_t
*zio
)
2617 int p
= zio
->io_prop
.zp_copies
;
2618 ddt_t
*ddt
= ddt_select(zio
->io_spa
, zio
->io_bp
);
2619 ddt_entry_t
*dde
= zio
->io_private
;
2620 ddt_phys_t
*ddp
= &dde
->dde_phys
[p
];
2629 ASSERT(dde
->dde_lead_zio
[p
] == zio
);
2631 ddt_phys_fill(ddp
, zio
->io_bp
);
2634 while ((pio
= zio_walk_parents(zio
, &zl
)) != NULL
)
2635 ddt_bp_fill(ddp
, pio
->io_bp
, zio
->io_txg
);
2641 zio_ddt_child_write_done(zio_t
*zio
)
2643 int p
= zio
->io_prop
.zp_copies
;
2644 ddt_t
*ddt
= ddt_select(zio
->io_spa
, zio
->io_bp
);
2645 ddt_entry_t
*dde
= zio
->io_private
;
2646 ddt_phys_t
*ddp
= &dde
->dde_phys
[p
];
2650 ASSERT(ddp
->ddp_refcnt
== 0);
2651 ASSERT(dde
->dde_lead_zio
[p
] == zio
);
2652 dde
->dde_lead_zio
[p
] = NULL
;
2654 if (zio
->io_error
== 0) {
2655 zio_link_t
*zl
= NULL
;
2656 while (zio_walk_parents(zio
, &zl
) != NULL
)
2657 ddt_phys_addref(ddp
);
2659 ddt_phys_clear(ddp
);
2666 zio_ddt_ditto_write_done(zio_t
*zio
)
2668 int p
= DDT_PHYS_DITTO
;
2669 blkptr_t
*bp
= zio
->io_bp
;
2670 ddt_t
*ddt
= ddt_select(zio
->io_spa
, bp
);
2671 ddt_entry_t
*dde
= zio
->io_private
;
2672 ddt_phys_t
*ddp
= &dde
->dde_phys
[p
];
2673 ddt_key_t
*ddk
= &dde
->dde_key
;
2674 ASSERTV(zio_prop_t
*zp
= &zio
->io_prop
);
2678 ASSERT(ddp
->ddp_refcnt
== 0);
2679 ASSERT(dde
->dde_lead_zio
[p
] == zio
);
2680 dde
->dde_lead_zio
[p
] = NULL
;
2682 if (zio
->io_error
== 0) {
2683 ASSERT(ZIO_CHECKSUM_EQUAL(bp
->blk_cksum
, ddk
->ddk_cksum
));
2684 ASSERT(zp
->zp_copies
< SPA_DVAS_PER_BP
);
2685 ASSERT(zp
->zp_copies
== BP_GET_NDVAS(bp
) - BP_IS_GANG(bp
));
2686 if (ddp
->ddp_phys_birth
!= 0)
2687 ddt_phys_free(ddt
, ddk
, ddp
, zio
->io_txg
);
2688 ddt_phys_fill(ddp
, bp
);
2695 zio_ddt_write(zio_t
*zio
)
2697 spa_t
*spa
= zio
->io_spa
;
2698 blkptr_t
*bp
= zio
->io_bp
;
2699 uint64_t txg
= zio
->io_txg
;
2700 zio_prop_t
*zp
= &zio
->io_prop
;
2701 int p
= zp
->zp_copies
;
2705 ddt_t
*ddt
= ddt_select(spa
, bp
);
2709 ASSERT(BP_GET_DEDUP(bp
));
2710 ASSERT(BP_GET_CHECKSUM(bp
) == zp
->zp_checksum
);
2711 ASSERT(BP_IS_HOLE(bp
) || zio
->io_bp_override
);
2712 ASSERT(!(zio
->io_bp_override
&& (zio
->io_flags
& ZIO_FLAG_RAW
)));
2715 dde
= ddt_lookup(ddt
, bp
, B_TRUE
);
2716 ddp
= &dde
->dde_phys
[p
];
2718 if (zp
->zp_dedup_verify
&& zio_ddt_collision(zio
, ddt
, dde
)) {
2720 * If we're using a weak checksum, upgrade to a strong checksum
2721 * and try again. If we're already using a strong checksum,
2722 * we can't resolve it, so just convert to an ordinary write.
2723 * (And automatically e-mail a paper to Nature?)
2725 if (!(zio_checksum_table
[zp
->zp_checksum
].ci_flags
&
2726 ZCHECKSUM_FLAG_DEDUP
)) {
2727 zp
->zp_checksum
= spa_dedup_checksum(spa
);
2728 zio_pop_transforms(zio
);
2729 zio
->io_stage
= ZIO_STAGE_OPEN
;
2732 zp
->zp_dedup
= B_FALSE
;
2734 zio
->io_pipeline
= ZIO_WRITE_PIPELINE
;
2736 return (ZIO_PIPELINE_CONTINUE
);
2739 ditto_copies
= ddt_ditto_copies_needed(ddt
, dde
, ddp
);
2740 ASSERT(ditto_copies
< SPA_DVAS_PER_BP
);
2742 if (ditto_copies
> ddt_ditto_copies_present(dde
) &&
2743 dde
->dde_lead_zio
[DDT_PHYS_DITTO
] == NULL
) {
2744 zio_prop_t czp
= *zp
;
2746 czp
.zp_copies
= ditto_copies
;
2749 * If we arrived here with an override bp, we won't have run
2750 * the transform stack, so we won't have the data we need to
2751 * generate a child i/o. So, toss the override bp and restart.
2752 * This is safe, because using the override bp is just an
2753 * optimization; and it's rare, so the cost doesn't matter.
2755 if (zio
->io_bp_override
) {
2756 zio_pop_transforms(zio
);
2757 zio
->io_stage
= ZIO_STAGE_OPEN
;
2758 zio
->io_pipeline
= ZIO_WRITE_PIPELINE
;
2759 zio
->io_bp_override
= NULL
;
2762 return (ZIO_PIPELINE_CONTINUE
);
2765 dio
= zio_write(zio
, spa
, txg
, bp
, zio
->io_orig_data
,
2766 zio
->io_orig_size
, zio
->io_orig_size
, &czp
, NULL
, NULL
,
2767 NULL
, zio_ddt_ditto_write_done
, dde
, zio
->io_priority
,
2768 ZIO_DDT_CHILD_FLAGS(zio
), &zio
->io_bookmark
);
2770 zio_push_transform(dio
, zio
->io_data
, zio
->io_size
, 0, NULL
);
2771 dde
->dde_lead_zio
[DDT_PHYS_DITTO
] = dio
;
2774 if (ddp
->ddp_phys_birth
!= 0 || dde
->dde_lead_zio
[p
] != NULL
) {
2775 if (ddp
->ddp_phys_birth
!= 0)
2776 ddt_bp_fill(ddp
, bp
, txg
);
2777 if (dde
->dde_lead_zio
[p
] != NULL
)
2778 zio_add_child(zio
, dde
->dde_lead_zio
[p
]);
2780 ddt_phys_addref(ddp
);
2781 } else if (zio
->io_bp_override
) {
2782 ASSERT(bp
->blk_birth
== txg
);
2783 ASSERT(BP_EQUAL(bp
, zio
->io_bp_override
));
2784 ddt_phys_fill(ddp
, bp
);
2785 ddt_phys_addref(ddp
);
2787 cio
= zio_write(zio
, spa
, txg
, bp
, zio
->io_orig_data
,
2788 zio
->io_orig_size
, zio
->io_orig_size
, zp
,
2789 zio_ddt_child_write_ready
, NULL
, NULL
,
2790 zio_ddt_child_write_done
, dde
, zio
->io_priority
,
2791 ZIO_DDT_CHILD_FLAGS(zio
), &zio
->io_bookmark
);
2793 zio_push_transform(cio
, zio
->io_data
, zio
->io_size
, 0, NULL
);
2794 dde
->dde_lead_zio
[p
] = cio
;
2804 return (ZIO_PIPELINE_CONTINUE
);
2807 ddt_entry_t
*freedde
; /* for debugging */
2810 zio_ddt_free(zio_t
*zio
)
2812 spa_t
*spa
= zio
->io_spa
;
2813 blkptr_t
*bp
= zio
->io_bp
;
2814 ddt_t
*ddt
= ddt_select(spa
, bp
);
2818 ASSERT(BP_GET_DEDUP(bp
));
2819 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
2822 freedde
= dde
= ddt_lookup(ddt
, bp
, B_TRUE
);
2824 ddp
= ddt_phys_select(dde
, bp
);
2826 ddt_phys_decref(ddp
);
2830 return (ZIO_PIPELINE_CONTINUE
);
2834 * ==========================================================================
2835 * Allocate and free blocks
2836 * ==========================================================================
2840 zio_io_to_allocate(spa_t
*spa
)
2844 ASSERT(MUTEX_HELD(&spa
->spa_alloc_lock
));
2846 zio
= avl_first(&spa
->spa_alloc_tree
);
2850 ASSERT(IO_IS_ALLOCATING(zio
));
2853 * Try to place a reservation for this zio. If we're unable to
2854 * reserve then we throttle.
2856 if (!metaslab_class_throttle_reserve(spa_normal_class(spa
),
2857 zio
->io_prop
.zp_copies
, zio
, 0)) {
2861 avl_remove(&spa
->spa_alloc_tree
, zio
);
2862 ASSERT3U(zio
->io_stage
, <, ZIO_STAGE_DVA_ALLOCATE
);
2868 zio_dva_throttle(zio_t
*zio
)
2870 spa_t
*spa
= zio
->io_spa
;
2873 if (zio
->io_priority
== ZIO_PRIORITY_SYNC_WRITE
||
2874 !spa_normal_class(zio
->io_spa
)->mc_alloc_throttle_enabled
||
2875 zio
->io_child_type
== ZIO_CHILD_GANG
||
2876 zio
->io_flags
& ZIO_FLAG_NODATA
) {
2877 return (ZIO_PIPELINE_CONTINUE
);
2880 ASSERT(zio
->io_child_type
> ZIO_CHILD_GANG
);
2882 ASSERT3U(zio
->io_queued_timestamp
, >, 0);
2883 ASSERT(zio
->io_stage
== ZIO_STAGE_DVA_THROTTLE
);
2885 mutex_enter(&spa
->spa_alloc_lock
);
2887 ASSERT(zio
->io_type
== ZIO_TYPE_WRITE
);
2888 avl_add(&spa
->spa_alloc_tree
, zio
);
2890 nio
= zio_io_to_allocate(zio
->io_spa
);
2891 mutex_exit(&spa
->spa_alloc_lock
);
2894 return (ZIO_PIPELINE_CONTINUE
);
2897 ASSERT3U(nio
->io_queued_timestamp
, <=,
2898 zio
->io_queued_timestamp
);
2899 ASSERT(nio
->io_stage
== ZIO_STAGE_DVA_THROTTLE
);
2901 * We are passing control to a new zio so make sure that
2902 * it is processed by a different thread. We do this to
2903 * avoid stack overflows that can occur when parents are
2904 * throttled and children are making progress. We allow
2905 * it to go to the head of the taskq since it's already
2908 zio_taskq_dispatch(nio
, ZIO_TASKQ_ISSUE
, B_TRUE
);
2910 return (ZIO_PIPELINE_STOP
);
2914 zio_allocate_dispatch(spa_t
*spa
)
2918 mutex_enter(&spa
->spa_alloc_lock
);
2919 zio
= zio_io_to_allocate(spa
);
2920 mutex_exit(&spa
->spa_alloc_lock
);
2924 ASSERT3U(zio
->io_stage
, ==, ZIO_STAGE_DVA_THROTTLE
);
2925 ASSERT0(zio
->io_error
);
2926 zio_taskq_dispatch(zio
, ZIO_TASKQ_ISSUE
, B_TRUE
);
2930 zio_dva_allocate(zio_t
*zio
)
2932 spa_t
*spa
= zio
->io_spa
;
2933 metaslab_class_t
*mc
= spa_normal_class(spa
);
2934 blkptr_t
*bp
= zio
->io_bp
;
2938 if (zio
->io_gang_leader
== NULL
) {
2939 ASSERT(zio
->io_child_type
> ZIO_CHILD_GANG
);
2940 zio
->io_gang_leader
= zio
;
2943 ASSERT(BP_IS_HOLE(bp
));
2944 ASSERT0(BP_GET_NDVAS(bp
));
2945 ASSERT3U(zio
->io_prop
.zp_copies
, >, 0);
2946 ASSERT3U(zio
->io_prop
.zp_copies
, <=, spa_max_replication(spa
));
2947 ASSERT3U(zio
->io_size
, ==, BP_GET_PSIZE(bp
));
2949 flags
|= (zio
->io_flags
& ZIO_FLAG_FASTWRITE
) ? METASLAB_FASTWRITE
: 0;
2950 if (zio
->io_flags
& ZIO_FLAG_NODATA
)
2951 flags
|= METASLAB_DONT_THROTTLE
;
2952 if (zio
->io_flags
& ZIO_FLAG_GANG_CHILD
)
2953 flags
|= METASLAB_GANG_CHILD
;
2954 if (zio
->io_priority
== ZIO_PRIORITY_ASYNC_WRITE
)
2955 flags
|= METASLAB_ASYNC_ALLOC
;
2957 error
= metaslab_alloc(spa
, mc
, zio
->io_size
, bp
,
2958 zio
->io_prop
.zp_copies
, zio
->io_txg
, NULL
, flags
, zio
);
2961 spa_dbgmsg(spa
, "%s: metaslab allocation failure: zio %p, "
2962 "size %llu, error %d", spa_name(spa
), zio
, zio
->io_size
,
2964 if (error
== ENOSPC
&& zio
->io_size
> SPA_MINBLOCKSIZE
)
2965 return (zio_write_gang_block(zio
));
2966 zio
->io_error
= error
;
2969 return (ZIO_PIPELINE_CONTINUE
);
2973 zio_dva_free(zio_t
*zio
)
2975 metaslab_free(zio
->io_spa
, zio
->io_bp
, zio
->io_txg
, B_FALSE
);
2977 return (ZIO_PIPELINE_CONTINUE
);
2981 zio_dva_claim(zio_t
*zio
)
2985 error
= metaslab_claim(zio
->io_spa
, zio
->io_bp
, zio
->io_txg
);
2987 zio
->io_error
= error
;
2989 return (ZIO_PIPELINE_CONTINUE
);
2993 * Undo an allocation. This is used by zio_done() when an I/O fails
2994 * and we want to give back the block we just allocated.
2995 * This handles both normal blocks and gang blocks.
2998 zio_dva_unallocate(zio_t
*zio
, zio_gang_node_t
*gn
, blkptr_t
*bp
)
3002 ASSERT(bp
->blk_birth
== zio
->io_txg
|| BP_IS_HOLE(bp
));
3003 ASSERT(zio
->io_bp_override
== NULL
);
3005 if (!BP_IS_HOLE(bp
))
3006 metaslab_free(zio
->io_spa
, bp
, bp
->blk_birth
, B_TRUE
);
3009 for (g
= 0; g
< SPA_GBH_NBLKPTRS
; g
++) {
3010 zio_dva_unallocate(zio
, gn
->gn_child
[g
],
3011 &gn
->gn_gbh
->zg_blkptr
[g
]);
3017 * Try to allocate an intent log block. Return 0 on success, errno on failure.
3020 zio_alloc_zil(spa_t
*spa
, uint64_t txg
, blkptr_t
*new_bp
, uint64_t size
,
3025 ASSERT(txg
> spa_syncing_txg(spa
));
3028 error
= metaslab_alloc(spa
, spa_log_class(spa
), size
,
3029 new_bp
, 1, txg
, NULL
, METASLAB_FASTWRITE
, NULL
);
3033 error
= metaslab_alloc(spa
, spa_normal_class(spa
), size
,
3034 new_bp
, 1, txg
, NULL
, METASLAB_FASTWRITE
, NULL
);
3038 BP_SET_LSIZE(new_bp
, size
);
3039 BP_SET_PSIZE(new_bp
, size
);
3040 BP_SET_COMPRESS(new_bp
, ZIO_COMPRESS_OFF
);
3041 BP_SET_CHECKSUM(new_bp
,
3042 spa_version(spa
) >= SPA_VERSION_SLIM_ZIL
3043 ? ZIO_CHECKSUM_ZILOG2
: ZIO_CHECKSUM_ZILOG
);
3044 BP_SET_TYPE(new_bp
, DMU_OT_INTENT_LOG
);
3045 BP_SET_LEVEL(new_bp
, 0);
3046 BP_SET_DEDUP(new_bp
, 0);
3047 BP_SET_BYTEORDER(new_bp
, ZFS_HOST_BYTEORDER
);
3054 * Free an intent log block.
3057 zio_free_zil(spa_t
*spa
, uint64_t txg
, blkptr_t
*bp
)
3059 ASSERT(BP_GET_TYPE(bp
) == DMU_OT_INTENT_LOG
);
3060 ASSERT(!BP_IS_GANG(bp
));
3062 zio_free(spa
, txg
, bp
);
3066 * ==========================================================================
3067 * Read and write to physical devices
3068 * ==========================================================================
3073 * Issue an I/O to the underlying vdev. Typically the issue pipeline
3074 * stops after this stage and will resume upon I/O completion.
3075 * However, there are instances where the vdev layer may need to
3076 * continue the pipeline when an I/O was not issued. Since the I/O
3077 * that was sent to the vdev layer might be different than the one
3078 * currently active in the pipeline (see vdev_queue_io()), we explicitly
3079 * force the underlying vdev layers to call either zio_execute() or
3080 * zio_interrupt() to ensure that the pipeline continues with the correct I/O.
3083 zio_vdev_io_start(zio_t
*zio
)
3085 vdev_t
*vd
= zio
->io_vd
;
3087 spa_t
*spa
= zio
->io_spa
;
3091 ASSERT(zio
->io_error
== 0);
3092 ASSERT(zio
->io_child_error
[ZIO_CHILD_VDEV
] == 0);
3095 if (!(zio
->io_flags
& ZIO_FLAG_CONFIG_WRITER
))
3096 spa_config_enter(spa
, SCL_ZIO
, zio
, RW_READER
);
3099 * The mirror_ops handle multiple DVAs in a single BP.
3101 vdev_mirror_ops
.vdev_op_io_start(zio
);
3102 return (ZIO_PIPELINE_STOP
);
3105 ASSERT3P(zio
->io_logical
, !=, zio
);
3108 * We keep track of time-sensitive I/Os so that the scan thread
3109 * can quickly react to certain workloads. In particular, we care
3110 * about non-scrubbing, top-level reads and writes with the following
3112 * - synchronous writes of user data to non-slog devices
3113 * - any reads of user data
3114 * When these conditions are met, adjust the timestamp of spa_last_io
3115 * which allows the scan thread to adjust its workload accordingly.
3117 if (!(zio
->io_flags
& ZIO_FLAG_SCAN_THREAD
) && zio
->io_bp
!= NULL
&&
3118 vd
== vd
->vdev_top
&& !vd
->vdev_islog
&&
3119 zio
->io_bookmark
.zb_objset
!= DMU_META_OBJSET
&&
3120 zio
->io_txg
!= spa_syncing_txg(spa
)) {
3121 uint64_t old
= spa
->spa_last_io
;
3122 uint64_t new = ddi_get_lbolt64();
3124 (void) atomic_cas_64(&spa
->spa_last_io
, old
, new);
3127 align
= 1ULL << vd
->vdev_top
->vdev_ashift
;
3129 if (!(zio
->io_flags
& ZIO_FLAG_PHYSICAL
) &&
3130 P2PHASE(zio
->io_size
, align
) != 0) {
3131 /* Transform logical writes to be a full physical block size. */
3132 uint64_t asize
= P2ROUNDUP(zio
->io_size
, align
);
3133 char *abuf
= zio_buf_alloc(asize
);
3134 ASSERT(vd
== vd
->vdev_top
);
3135 if (zio
->io_type
== ZIO_TYPE_WRITE
) {
3136 bcopy(zio
->io_data
, abuf
, zio
->io_size
);
3137 bzero(abuf
+ zio
->io_size
, asize
- zio
->io_size
);
3139 zio_push_transform(zio
, abuf
, asize
, asize
, zio_subblock
);
3143 * If this is not a physical io, make sure that it is properly aligned
3144 * before proceeding.
3146 if (!(zio
->io_flags
& ZIO_FLAG_PHYSICAL
)) {
3147 ASSERT0(P2PHASE(zio
->io_offset
, align
));
3148 ASSERT0(P2PHASE(zio
->io_size
, align
));
3151 * For physical writes, we allow 512b aligned writes and assume
3152 * the device will perform a read-modify-write as necessary.
3154 ASSERT0(P2PHASE(zio
->io_offset
, SPA_MINBLOCKSIZE
));
3155 ASSERT0(P2PHASE(zio
->io_size
, SPA_MINBLOCKSIZE
));
3158 VERIFY(zio
->io_type
!= ZIO_TYPE_WRITE
|| spa_writeable(spa
));
3161 * If this is a repair I/O, and there's no self-healing involved --
3162 * that is, we're just resilvering what we expect to resilver --
3163 * then don't do the I/O unless zio's txg is actually in vd's DTL.
3164 * This prevents spurious resilvering with nested replication.
3165 * For example, given a mirror of mirrors, (A+B)+(C+D), if only
3166 * A is out of date, we'll read from C+D, then use the data to
3167 * resilver A+B -- but we don't actually want to resilver B, just A.
3168 * The top-level mirror has no way to know this, so instead we just
3169 * discard unnecessary repairs as we work our way down the vdev tree.
3170 * The same logic applies to any form of nested replication:
3171 * ditto + mirror, RAID-Z + replacing, etc. This covers them all.
3173 if ((zio
->io_flags
& ZIO_FLAG_IO_REPAIR
) &&
3174 !(zio
->io_flags
& ZIO_FLAG_SELF_HEAL
) &&
3175 zio
->io_txg
!= 0 && /* not a delegated i/o */
3176 !vdev_dtl_contains(vd
, DTL_PARTIAL
, zio
->io_txg
, 1)) {
3177 ASSERT(zio
->io_type
== ZIO_TYPE_WRITE
);
3178 zio_vdev_io_bypass(zio
);
3179 return (ZIO_PIPELINE_CONTINUE
);
3182 if (vd
->vdev_ops
->vdev_op_leaf
&&
3183 (zio
->io_type
== ZIO_TYPE_READ
|| zio
->io_type
== ZIO_TYPE_WRITE
)) {
3185 if (zio
->io_type
== ZIO_TYPE_READ
&& vdev_cache_read(zio
))
3186 return (ZIO_PIPELINE_CONTINUE
);
3188 if ((zio
= vdev_queue_io(zio
)) == NULL
)
3189 return (ZIO_PIPELINE_STOP
);
3191 if (!vdev_accessible(vd
, zio
)) {
3192 zio
->io_error
= SET_ERROR(ENXIO
);
3194 return (ZIO_PIPELINE_STOP
);
3198 zio
->io_delay
= gethrtime();
3199 vd
->vdev_ops
->vdev_op_io_start(zio
);
3200 return (ZIO_PIPELINE_STOP
);
3204 zio_vdev_io_done(zio_t
*zio
)
3206 vdev_t
*vd
= zio
->io_vd
;
3207 vdev_ops_t
*ops
= vd
? vd
->vdev_ops
: &vdev_mirror_ops
;
3208 boolean_t unexpected_error
= B_FALSE
;
3210 if (zio_wait_for_children(zio
, ZIO_CHILD_VDEV
, ZIO_WAIT_DONE
))
3211 return (ZIO_PIPELINE_STOP
);
3213 ASSERT(zio
->io_type
== ZIO_TYPE_READ
|| zio
->io_type
== ZIO_TYPE_WRITE
);
3216 zio
->io_delay
= gethrtime() - zio
->io_delay
;
3218 if (vd
!= NULL
&& vd
->vdev_ops
->vdev_op_leaf
) {
3220 vdev_queue_io_done(zio
);
3222 if (zio
->io_type
== ZIO_TYPE_WRITE
)
3223 vdev_cache_write(zio
);
3225 if (zio_injection_enabled
&& zio
->io_error
== 0)
3226 zio
->io_error
= zio_handle_device_injection(vd
,
3229 if (zio_injection_enabled
&& zio
->io_error
== 0)
3230 zio
->io_error
= zio_handle_label_injection(zio
, EIO
);
3232 if (zio
->io_error
) {
3233 if (!vdev_accessible(vd
, zio
)) {
3234 zio
->io_error
= SET_ERROR(ENXIO
);
3236 unexpected_error
= B_TRUE
;
3241 ops
->vdev_op_io_done(zio
);
3243 if (unexpected_error
)
3244 VERIFY(vdev_probe(vd
, zio
) == NULL
);
3246 return (ZIO_PIPELINE_CONTINUE
);
3250 * For non-raidz ZIOs, we can just copy aside the bad data read from the
3251 * disk, and use that to finish the checksum ereport later.
3254 zio_vsd_default_cksum_finish(zio_cksum_report_t
*zcr
,
3255 const void *good_buf
)
3257 /* no processing needed */
3258 zfs_ereport_finish_checksum(zcr
, good_buf
, zcr
->zcr_cbdata
, B_FALSE
);
3263 zio_vsd_default_cksum_report(zio_t
*zio
, zio_cksum_report_t
*zcr
, void *ignored
)
3265 void *buf
= zio_buf_alloc(zio
->io_size
);
3267 bcopy(zio
->io_data
, buf
, zio
->io_size
);
3269 zcr
->zcr_cbinfo
= zio
->io_size
;
3270 zcr
->zcr_cbdata
= buf
;
3271 zcr
->zcr_finish
= zio_vsd_default_cksum_finish
;
3272 zcr
->zcr_free
= zio_buf_free
;
3276 zio_vdev_io_assess(zio_t
*zio
)
3278 vdev_t
*vd
= zio
->io_vd
;
3280 if (zio_wait_for_children(zio
, ZIO_CHILD_VDEV
, ZIO_WAIT_DONE
))
3281 return (ZIO_PIPELINE_STOP
);
3283 if (vd
== NULL
&& !(zio
->io_flags
& ZIO_FLAG_CONFIG_WRITER
))
3284 spa_config_exit(zio
->io_spa
, SCL_ZIO
, zio
);
3286 if (zio
->io_vsd
!= NULL
) {
3287 zio
->io_vsd_ops
->vsd_free(zio
);
3291 if (zio_injection_enabled
&& zio
->io_error
== 0)
3292 zio
->io_error
= zio_handle_fault_injection(zio
, EIO
);
3295 * If the I/O failed, determine whether we should attempt to retry it.
3297 * On retry, we cut in line in the issue queue, since we don't want
3298 * compression/checksumming/etc. work to prevent our (cheap) IO reissue.
3300 if (zio
->io_error
&& vd
== NULL
&&
3301 !(zio
->io_flags
& (ZIO_FLAG_DONT_RETRY
| ZIO_FLAG_IO_RETRY
))) {
3302 ASSERT(!(zio
->io_flags
& ZIO_FLAG_DONT_QUEUE
)); /* not a leaf */
3303 ASSERT(!(zio
->io_flags
& ZIO_FLAG_IO_BYPASS
)); /* not a leaf */
3305 zio
->io_flags
|= ZIO_FLAG_IO_RETRY
|
3306 ZIO_FLAG_DONT_CACHE
| ZIO_FLAG_DONT_AGGREGATE
;
3307 zio
->io_stage
= ZIO_STAGE_VDEV_IO_START
>> 1;
3308 zio_taskq_dispatch(zio
, ZIO_TASKQ_ISSUE
,
3309 zio_requeue_io_start_cut_in_line
);
3310 return (ZIO_PIPELINE_STOP
);
3314 * If we got an error on a leaf device, convert it to ENXIO
3315 * if the device is not accessible at all.
3317 if (zio
->io_error
&& vd
!= NULL
&& vd
->vdev_ops
->vdev_op_leaf
&&
3318 !vdev_accessible(vd
, zio
))
3319 zio
->io_error
= SET_ERROR(ENXIO
);
3322 * If we can't write to an interior vdev (mirror or RAID-Z),
3323 * set vdev_cant_write so that we stop trying to allocate from it.
3325 if (zio
->io_error
== ENXIO
&& zio
->io_type
== ZIO_TYPE_WRITE
&&
3326 vd
!= NULL
&& !vd
->vdev_ops
->vdev_op_leaf
) {
3327 vd
->vdev_cant_write
= B_TRUE
;
3331 zio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
3333 if (vd
!= NULL
&& vd
->vdev_ops
->vdev_op_leaf
&&
3334 zio
->io_physdone
!= NULL
) {
3335 ASSERT(!(zio
->io_flags
& ZIO_FLAG_DELEGATED
));
3336 ASSERT(zio
->io_child_type
== ZIO_CHILD_VDEV
);
3337 zio
->io_physdone(zio
->io_logical
);
3340 return (ZIO_PIPELINE_CONTINUE
);
3344 zio_vdev_io_reissue(zio_t
*zio
)
3346 ASSERT(zio
->io_stage
== ZIO_STAGE_VDEV_IO_START
);
3347 ASSERT(zio
->io_error
== 0);
3349 zio
->io_stage
>>= 1;
3353 zio_vdev_io_redone(zio_t
*zio
)
3355 ASSERT(zio
->io_stage
== ZIO_STAGE_VDEV_IO_DONE
);
3357 zio
->io_stage
>>= 1;
3361 zio_vdev_io_bypass(zio_t
*zio
)
3363 ASSERT(zio
->io_stage
== ZIO_STAGE_VDEV_IO_START
);
3364 ASSERT(zio
->io_error
== 0);
3366 zio
->io_flags
|= ZIO_FLAG_IO_BYPASS
;
3367 zio
->io_stage
= ZIO_STAGE_VDEV_IO_ASSESS
>> 1;
3371 * ==========================================================================
3372 * Generate and verify checksums
3373 * ==========================================================================
3376 zio_checksum_generate(zio_t
*zio
)
3378 blkptr_t
*bp
= zio
->io_bp
;
3379 enum zio_checksum checksum
;
3383 * This is zio_write_phys().
3384 * We're either generating a label checksum, or none at all.
3386 checksum
= zio
->io_prop
.zp_checksum
;
3388 if (checksum
== ZIO_CHECKSUM_OFF
)
3389 return (ZIO_PIPELINE_CONTINUE
);
3391 ASSERT(checksum
== ZIO_CHECKSUM_LABEL
);
3393 if (BP_IS_GANG(bp
) && zio
->io_child_type
== ZIO_CHILD_GANG
) {
3394 ASSERT(!IO_IS_ALLOCATING(zio
));
3395 checksum
= ZIO_CHECKSUM_GANG_HEADER
;
3397 checksum
= BP_GET_CHECKSUM(bp
);
3401 zio_checksum_compute(zio
, checksum
, zio
->io_data
, zio
->io_size
);
3403 return (ZIO_PIPELINE_CONTINUE
);
3407 zio_checksum_verify(zio_t
*zio
)
3409 zio_bad_cksum_t info
;
3410 blkptr_t
*bp
= zio
->io_bp
;
3413 ASSERT(zio
->io_vd
!= NULL
);
3417 * This is zio_read_phys().
3418 * We're either verifying a label checksum, or nothing at all.
3420 if (zio
->io_prop
.zp_checksum
== ZIO_CHECKSUM_OFF
)
3421 return (ZIO_PIPELINE_CONTINUE
);
3423 ASSERT(zio
->io_prop
.zp_checksum
== ZIO_CHECKSUM_LABEL
);
3426 if ((error
= zio_checksum_error(zio
, &info
)) != 0) {
3427 zio
->io_error
= error
;
3428 if (error
== ECKSUM
&&
3429 !(zio
->io_flags
& ZIO_FLAG_SPECULATIVE
)) {
3430 zfs_ereport_start_checksum(zio
->io_spa
,
3431 zio
->io_vd
, zio
, zio
->io_offset
,
3432 zio
->io_size
, NULL
, &info
);
3436 return (ZIO_PIPELINE_CONTINUE
);
3440 * Called by RAID-Z to ensure we don't compute the checksum twice.
3443 zio_checksum_verified(zio_t
*zio
)
3445 zio
->io_pipeline
&= ~ZIO_STAGE_CHECKSUM_VERIFY
;
3449 * ==========================================================================
3450 * Error rank. Error are ranked in the order 0, ENXIO, ECKSUM, EIO, other.
3451 * An error of 0 indicates success. ENXIO indicates whole-device failure,
3452 * which may be transient (e.g. unplugged) or permament. ECKSUM and EIO
3453 * indicate errors that are specific to one I/O, and most likely permanent.
3454 * Any other error is presumed to be worse because we weren't expecting it.
3455 * ==========================================================================
3458 zio_worst_error(int e1
, int e2
)
3460 static int zio_error_rank
[] = { 0, ENXIO
, ECKSUM
, EIO
};
3463 for (r1
= 0; r1
< sizeof (zio_error_rank
) / sizeof (int); r1
++)
3464 if (e1
== zio_error_rank
[r1
])
3467 for (r2
= 0; r2
< sizeof (zio_error_rank
) / sizeof (int); r2
++)
3468 if (e2
== zio_error_rank
[r2
])
3471 return (r1
> r2
? e1
: e2
);
3475 * ==========================================================================
3477 * ==========================================================================
3480 zio_ready(zio_t
*zio
)
3482 blkptr_t
*bp
= zio
->io_bp
;
3483 zio_t
*pio
, *pio_next
;
3484 zio_link_t
*zl
= NULL
;
3486 if (zio_wait_for_children(zio
, ZIO_CHILD_GANG
, ZIO_WAIT_READY
) ||
3487 zio_wait_for_children(zio
, ZIO_CHILD_DDT
, ZIO_WAIT_READY
))
3488 return (ZIO_PIPELINE_STOP
);
3490 if (zio
->io_ready
) {
3491 ASSERT(IO_IS_ALLOCATING(zio
));
3492 ASSERT(bp
->blk_birth
== zio
->io_txg
|| BP_IS_HOLE(bp
) ||
3493 (zio
->io_flags
& ZIO_FLAG_NOPWRITE
));
3494 ASSERT(zio
->io_children
[ZIO_CHILD_GANG
][ZIO_WAIT_READY
] == 0);
3499 if (bp
!= NULL
&& bp
!= &zio
->io_bp_copy
)
3500 zio
->io_bp_copy
= *bp
;
3502 if (zio
->io_error
!= 0) {
3503 zio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
3505 if (zio
->io_flags
& ZIO_FLAG_IO_ALLOCATING
) {
3506 ASSERT(IO_IS_ALLOCATING(zio
));
3507 ASSERT(zio
->io_priority
== ZIO_PRIORITY_ASYNC_WRITE
);
3509 * We were unable to allocate anything, unreserve and
3510 * issue the next I/O to allocate.
3512 metaslab_class_throttle_unreserve(
3513 spa_normal_class(zio
->io_spa
),
3514 zio
->io_prop
.zp_copies
, zio
);
3515 zio_allocate_dispatch(zio
->io_spa
);
3519 mutex_enter(&zio
->io_lock
);
3520 zio
->io_state
[ZIO_WAIT_READY
] = 1;
3521 pio
= zio_walk_parents(zio
, &zl
);
3522 mutex_exit(&zio
->io_lock
);
3525 * As we notify zio's parents, new parents could be added.
3526 * New parents go to the head of zio's io_parent_list, however,
3527 * so we will (correctly) not notify them. The remainder of zio's
3528 * io_parent_list, from 'pio_next' onward, cannot change because
3529 * all parents must wait for us to be done before they can be done.
3531 for (; pio
!= NULL
; pio
= pio_next
) {
3532 pio_next
= zio_walk_parents(zio
, &zl
);
3533 zio_notify_parent(pio
, zio
, ZIO_WAIT_READY
);
3536 if (zio
->io_flags
& ZIO_FLAG_NODATA
) {
3537 if (BP_IS_GANG(bp
)) {
3538 zio
->io_flags
&= ~ZIO_FLAG_NODATA
;
3540 ASSERT((uintptr_t)zio
->io_data
< SPA_MAXBLOCKSIZE
);
3541 zio
->io_pipeline
&= ~ZIO_VDEV_IO_STAGES
;
3545 if (zio_injection_enabled
&&
3546 zio
->io_spa
->spa_syncing_txg
== zio
->io_txg
)
3547 zio_handle_ignored_writes(zio
);
3549 return (ZIO_PIPELINE_CONTINUE
);
3553 * Update the allocation throttle accounting.
3556 zio_dva_throttle_done(zio_t
*zio
)
3558 zio_t
*lio
= zio
->io_logical
;
3559 zio_t
*pio
= zio_unique_parent(zio
);
3560 vdev_t
*vd
= zio
->io_vd
;
3561 int flags
= METASLAB_ASYNC_ALLOC
;
3563 ASSERT3P(zio
->io_bp
, !=, NULL
);
3564 ASSERT3U(zio
->io_type
, ==, ZIO_TYPE_WRITE
);
3565 ASSERT3U(zio
->io_priority
, ==, ZIO_PRIORITY_ASYNC_WRITE
);
3566 ASSERT3U(zio
->io_child_type
, ==, ZIO_CHILD_VDEV
);
3568 ASSERT3P(vd
, ==, vd
->vdev_top
);
3569 ASSERT(!(zio
->io_flags
& (ZIO_FLAG_IO_REPAIR
| ZIO_FLAG_IO_RETRY
)));
3570 ASSERT(zio
->io_flags
& ZIO_FLAG_IO_ALLOCATING
);
3571 ASSERT(!(lio
->io_flags
& ZIO_FLAG_IO_REWRITE
));
3572 ASSERT(!(lio
->io_orig_flags
& ZIO_FLAG_NODATA
));
3575 * Parents of gang children can have two flavors -- ones that
3576 * allocated the gang header (will have ZIO_FLAG_IO_REWRITE set)
3577 * and ones that allocated the constituent blocks. The allocation
3578 * throttle needs to know the allocating parent zio so we must find
3581 if (pio
->io_child_type
== ZIO_CHILD_GANG
) {
3583 * If our parent is a rewrite gang child then our grandparent
3584 * would have been the one that performed the allocation.
3586 if (pio
->io_flags
& ZIO_FLAG_IO_REWRITE
)
3587 pio
= zio_unique_parent(pio
);
3588 flags
|= METASLAB_GANG_CHILD
;
3591 ASSERT(IO_IS_ALLOCATING(pio
));
3592 ASSERT3P(zio
, !=, zio
->io_logical
);
3593 ASSERT(zio
->io_logical
!= NULL
);
3594 ASSERT(!(zio
->io_flags
& ZIO_FLAG_IO_REPAIR
));
3595 ASSERT0(zio
->io_flags
& ZIO_FLAG_NOPWRITE
);
3597 mutex_enter(&pio
->io_lock
);
3598 metaslab_group_alloc_decrement(zio
->io_spa
, vd
->vdev_id
, pio
, flags
);
3599 mutex_exit(&pio
->io_lock
);
3601 metaslab_class_throttle_unreserve(spa_normal_class(zio
->io_spa
),
3605 * Call into the pipeline to see if there is more work that
3606 * needs to be done. If there is work to be done it will be
3607 * dispatched to another taskq thread.
3609 zio_allocate_dispatch(zio
->io_spa
);
3613 zio_done(zio_t
*zio
)
3616 * Always attempt to keep stack usage minimal here since
3617 * we can be called recurisvely up to 19 levels deep.
3619 zio_t
*pio
, *pio_next
;
3621 zio_link_t
*zl
= NULL
;
3624 * If our children haven't all completed,
3625 * wait for them and then repeat this pipeline stage.
3627 if (zio_wait_for_children(zio
, ZIO_CHILD_VDEV
, ZIO_WAIT_DONE
) ||
3628 zio_wait_for_children(zio
, ZIO_CHILD_GANG
, ZIO_WAIT_DONE
) ||
3629 zio_wait_for_children(zio
, ZIO_CHILD_DDT
, ZIO_WAIT_DONE
) ||
3630 zio_wait_for_children(zio
, ZIO_CHILD_LOGICAL
, ZIO_WAIT_DONE
))
3631 return (ZIO_PIPELINE_STOP
);
3634 * If the allocation throttle is enabled, then update the accounting.
3635 * We only track child I/Os that are part of an allocating async
3636 * write. We must do this since the allocation is performed
3637 * by the logical I/O but the actual write is done by child I/Os.
3639 if (zio
->io_flags
& ZIO_FLAG_IO_ALLOCATING
&&
3640 zio
->io_child_type
== ZIO_CHILD_VDEV
) {
3641 ASSERT(spa_normal_class(
3642 zio
->io_spa
)->mc_alloc_throttle_enabled
);
3643 zio_dva_throttle_done(zio
);
3647 * If the allocation throttle is enabled, verify that
3648 * we have decremented the refcounts for every I/O that was throttled.
3650 if (zio
->io_flags
& ZIO_FLAG_IO_ALLOCATING
) {
3651 ASSERT(zio
->io_type
== ZIO_TYPE_WRITE
);
3652 ASSERT(zio
->io_priority
== ZIO_PRIORITY_ASYNC_WRITE
);
3653 ASSERT(zio
->io_bp
!= NULL
);
3654 metaslab_group_alloc_verify(zio
->io_spa
, zio
->io_bp
, zio
);
3655 VERIFY(refcount_not_held(
3656 &(spa_normal_class(zio
->io_spa
)->mc_alloc_slots
), zio
));
3660 for (c
= 0; c
< ZIO_CHILD_TYPES
; c
++)
3661 for (w
= 0; w
< ZIO_WAIT_TYPES
; w
++)
3662 ASSERT(zio
->io_children
[c
][w
] == 0);
3664 if (zio
->io_bp
!= NULL
&& !BP_IS_EMBEDDED(zio
->io_bp
)) {
3665 ASSERT(zio
->io_bp
->blk_pad
[0] == 0);
3666 ASSERT(zio
->io_bp
->blk_pad
[1] == 0);
3667 ASSERT(bcmp(zio
->io_bp
, &zio
->io_bp_copy
,
3668 sizeof (blkptr_t
)) == 0 ||
3669 (zio
->io_bp
== zio_unique_parent(zio
)->io_bp
));
3670 if (zio
->io_type
== ZIO_TYPE_WRITE
&& !BP_IS_HOLE(zio
->io_bp
) &&
3671 zio
->io_bp_override
== NULL
&&
3672 !(zio
->io_flags
& ZIO_FLAG_IO_REPAIR
)) {
3673 ASSERT(!BP_SHOULD_BYTESWAP(zio
->io_bp
));
3674 ASSERT3U(zio
->io_prop
.zp_copies
, <=,
3675 BP_GET_NDVAS(zio
->io_bp
));
3676 ASSERT(BP_COUNT_GANG(zio
->io_bp
) == 0 ||
3677 (BP_COUNT_GANG(zio
->io_bp
) ==
3678 BP_GET_NDVAS(zio
->io_bp
)));
3680 if (zio
->io_flags
& ZIO_FLAG_NOPWRITE
)
3681 VERIFY(BP_EQUAL(zio
->io_bp
, &zio
->io_bp_orig
));
3685 * If there were child vdev/gang/ddt errors, they apply to us now.
3687 zio_inherit_child_errors(zio
, ZIO_CHILD_VDEV
);
3688 zio_inherit_child_errors(zio
, ZIO_CHILD_GANG
);
3689 zio_inherit_child_errors(zio
, ZIO_CHILD_DDT
);
3692 * If the I/O on the transformed data was successful, generate any
3693 * checksum reports now while we still have the transformed data.
3695 if (zio
->io_error
== 0) {
3696 while (zio
->io_cksum_report
!= NULL
) {
3697 zio_cksum_report_t
*zcr
= zio
->io_cksum_report
;
3698 uint64_t align
= zcr
->zcr_align
;
3699 uint64_t asize
= P2ROUNDUP(zio
->io_size
, align
);
3700 char *abuf
= zio
->io_data
;
3702 if (asize
!= zio
->io_size
) {
3703 abuf
= zio_buf_alloc(asize
);
3704 bcopy(zio
->io_data
, abuf
, zio
->io_size
);
3705 bzero(abuf
+zio
->io_size
, asize
-zio
->io_size
);
3708 zio
->io_cksum_report
= zcr
->zcr_next
;
3709 zcr
->zcr_next
= NULL
;
3710 zcr
->zcr_finish(zcr
, abuf
);
3711 zfs_ereport_free_checksum(zcr
);
3713 if (asize
!= zio
->io_size
)
3714 zio_buf_free(abuf
, asize
);
3718 zio_pop_transforms(zio
); /* note: may set zio->io_error */
3720 vdev_stat_update(zio
, zio
->io_size
);
3723 * If this I/O is attached to a particular vdev is slow, exceeding
3724 * 30 seconds to complete, post an error described the I/O delay.
3725 * We ignore these errors if the device is currently unavailable.
3727 if (zio
->io_delay
>= MSEC2NSEC(zio_delay_max
)) {
3728 if (zio
->io_vd
!= NULL
&& !vdev_is_dead(zio
->io_vd
))
3729 zfs_ereport_post(FM_EREPORT_ZFS_DELAY
, zio
->io_spa
,
3730 zio
->io_vd
, zio
, 0, 0);
3733 if (zio
->io_error
) {
3735 * If this I/O is attached to a particular vdev,
3736 * generate an error message describing the I/O failure
3737 * at the block level. We ignore these errors if the
3738 * device is currently unavailable.
3740 if (zio
->io_error
!= ECKSUM
&& zio
->io_vd
!= NULL
&&
3741 !vdev_is_dead(zio
->io_vd
))
3742 zfs_ereport_post(FM_EREPORT_ZFS_IO
, zio
->io_spa
,
3743 zio
->io_vd
, zio
, 0, 0);
3745 if ((zio
->io_error
== EIO
|| !(zio
->io_flags
&
3746 (ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_DONT_PROPAGATE
))) &&
3747 zio
== zio
->io_logical
) {
3749 * For logical I/O requests, tell the SPA to log the
3750 * error and generate a logical data ereport.
3752 spa_log_error(zio
->io_spa
, zio
);
3753 zfs_ereport_post(FM_EREPORT_ZFS_DATA
, zio
->io_spa
,
3758 if (zio
->io_error
&& zio
== zio
->io_logical
) {
3760 * Determine whether zio should be reexecuted. This will
3761 * propagate all the way to the root via zio_notify_parent().
3763 ASSERT(zio
->io_vd
== NULL
&& zio
->io_bp
!= NULL
);
3764 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
3766 if (IO_IS_ALLOCATING(zio
) &&
3767 !(zio
->io_flags
& ZIO_FLAG_CANFAIL
)) {
3768 if (zio
->io_error
!= ENOSPC
)
3769 zio
->io_reexecute
|= ZIO_REEXECUTE_NOW
;
3771 zio
->io_reexecute
|= ZIO_REEXECUTE_SUSPEND
;
3774 if ((zio
->io_type
== ZIO_TYPE_READ
||
3775 zio
->io_type
== ZIO_TYPE_FREE
) &&
3776 !(zio
->io_flags
& ZIO_FLAG_SCAN_THREAD
) &&
3777 zio
->io_error
== ENXIO
&&
3778 spa_load_state(zio
->io_spa
) == SPA_LOAD_NONE
&&
3779 spa_get_failmode(zio
->io_spa
) != ZIO_FAILURE_MODE_CONTINUE
)
3780 zio
->io_reexecute
|= ZIO_REEXECUTE_SUSPEND
;
3782 if (!(zio
->io_flags
& ZIO_FLAG_CANFAIL
) && !zio
->io_reexecute
)
3783 zio
->io_reexecute
|= ZIO_REEXECUTE_SUSPEND
;
3786 * Here is a possibly good place to attempt to do
3787 * either combinatorial reconstruction or error correction
3788 * based on checksums. It also might be a good place
3789 * to send out preliminary ereports before we suspend
3795 * If there were logical child errors, they apply to us now.
3796 * We defer this until now to avoid conflating logical child
3797 * errors with errors that happened to the zio itself when
3798 * updating vdev stats and reporting FMA events above.
3800 zio_inherit_child_errors(zio
, ZIO_CHILD_LOGICAL
);
3802 if ((zio
->io_error
|| zio
->io_reexecute
) &&
3803 IO_IS_ALLOCATING(zio
) && zio
->io_gang_leader
== zio
&&
3804 !(zio
->io_flags
& (ZIO_FLAG_IO_REWRITE
| ZIO_FLAG_NOPWRITE
)))
3805 zio_dva_unallocate(zio
, zio
->io_gang_tree
, zio
->io_bp
);
3807 zio_gang_tree_free(&zio
->io_gang_tree
);
3810 * Godfather I/Os should never suspend.
3812 if ((zio
->io_flags
& ZIO_FLAG_GODFATHER
) &&
3813 (zio
->io_reexecute
& ZIO_REEXECUTE_SUSPEND
))
3814 zio
->io_reexecute
= 0;
3816 if (zio
->io_reexecute
) {
3818 * This is a logical I/O that wants to reexecute.
3820 * Reexecute is top-down. When an i/o fails, if it's not
3821 * the root, it simply notifies its parent and sticks around.
3822 * The parent, seeing that it still has children in zio_done(),
3823 * does the same. This percolates all the way up to the root.
3824 * The root i/o will reexecute or suspend the entire tree.
3826 * This approach ensures that zio_reexecute() honors
3827 * all the original i/o dependency relationships, e.g.
3828 * parents not executing until children are ready.
3830 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
3832 zio
->io_gang_leader
= NULL
;
3834 mutex_enter(&zio
->io_lock
);
3835 zio
->io_state
[ZIO_WAIT_DONE
] = 1;
3836 mutex_exit(&zio
->io_lock
);
3839 * "The Godfather" I/O monitors its children but is
3840 * not a true parent to them. It will track them through
3841 * the pipeline but severs its ties whenever they get into
3842 * trouble (e.g. suspended). This allows "The Godfather"
3843 * I/O to return status without blocking.
3846 for (pio
= zio_walk_parents(zio
, &zl
); pio
!= NULL
;
3848 zio_link_t
*remove_zl
= zl
;
3849 pio_next
= zio_walk_parents(zio
, &zl
);
3851 if ((pio
->io_flags
& ZIO_FLAG_GODFATHER
) &&
3852 (zio
->io_reexecute
& ZIO_REEXECUTE_SUSPEND
)) {
3853 zio_remove_child(pio
, zio
, remove_zl
);
3854 zio_notify_parent(pio
, zio
, ZIO_WAIT_DONE
);
3858 if ((pio
= zio_unique_parent(zio
)) != NULL
) {
3860 * We're not a root i/o, so there's nothing to do
3861 * but notify our parent. Don't propagate errors
3862 * upward since we haven't permanently failed yet.
3864 ASSERT(!(zio
->io_flags
& ZIO_FLAG_GODFATHER
));
3865 zio
->io_flags
|= ZIO_FLAG_DONT_PROPAGATE
;
3866 zio_notify_parent(pio
, zio
, ZIO_WAIT_DONE
);
3867 } else if (zio
->io_reexecute
& ZIO_REEXECUTE_SUSPEND
) {
3869 * We'd fail again if we reexecuted now, so suspend
3870 * until conditions improve (e.g. device comes online).
3872 zio_suspend(zio
->io_spa
, zio
);
3875 * Reexecution is potentially a huge amount of work.
3876 * Hand it off to the otherwise-unused claim taskq.
3878 ASSERT(taskq_empty_ent(&zio
->io_tqent
));
3879 spa_taskq_dispatch_ent(zio
->io_spa
,
3880 ZIO_TYPE_CLAIM
, ZIO_TASKQ_ISSUE
,
3881 (task_func_t
*)zio_reexecute
, zio
, 0,
3884 return (ZIO_PIPELINE_STOP
);
3887 ASSERT(zio
->io_child_count
== 0);
3888 ASSERT(zio
->io_reexecute
== 0);
3889 ASSERT(zio
->io_error
== 0 || (zio
->io_flags
& ZIO_FLAG_CANFAIL
));
3892 * Report any checksum errors, since the I/O is complete.
3894 while (zio
->io_cksum_report
!= NULL
) {
3895 zio_cksum_report_t
*zcr
= zio
->io_cksum_report
;
3896 zio
->io_cksum_report
= zcr
->zcr_next
;
3897 zcr
->zcr_next
= NULL
;
3898 zcr
->zcr_finish(zcr
, NULL
);
3899 zfs_ereport_free_checksum(zcr
);
3902 if (zio
->io_flags
& ZIO_FLAG_FASTWRITE
&& zio
->io_bp
&&
3903 !BP_IS_HOLE(zio
->io_bp
) && !BP_IS_EMBEDDED(zio
->io_bp
) &&
3904 !(zio
->io_flags
& ZIO_FLAG_NOPWRITE
)) {
3905 metaslab_fastwrite_unmark(zio
->io_spa
, zio
->io_bp
);
3909 * It is the responsibility of the done callback to ensure that this
3910 * particular zio is no longer discoverable for adoption, and as
3911 * such, cannot acquire any new parents.
3916 mutex_enter(&zio
->io_lock
);
3917 zio
->io_state
[ZIO_WAIT_DONE
] = 1;
3918 mutex_exit(&zio
->io_lock
);
3921 for (pio
= zio_walk_parents(zio
, &zl
); pio
!= NULL
; pio
= pio_next
) {
3922 zio_link_t
*remove_zl
= zl
;
3923 pio_next
= zio_walk_parents(zio
, &zl
);
3924 zio_remove_child(pio
, zio
, remove_zl
);
3925 zio_notify_parent(pio
, zio
, ZIO_WAIT_DONE
);
3928 if (zio
->io_waiter
!= NULL
) {
3929 mutex_enter(&zio
->io_lock
);
3930 zio
->io_executor
= NULL
;
3931 cv_broadcast(&zio
->io_cv
);
3932 mutex_exit(&zio
->io_lock
);
3937 return (ZIO_PIPELINE_STOP
);
3941 * ==========================================================================
3942 * I/O pipeline definition
3943 * ==========================================================================
3945 static zio_pipe_stage_t
*zio_pipeline
[] = {
3952 zio_checksum_generate
,
3968 zio_checksum_verify
,
3976 * Compare two zbookmark_phys_t's to see which we would reach first in a
3977 * pre-order traversal of the object tree.
3979 * This is simple in every case aside from the meta-dnode object. For all other
3980 * objects, we traverse them in order (object 1 before object 2, and so on).
3981 * However, all of these objects are traversed while traversing object 0, since
3982 * the data it points to is the list of objects. Thus, we need to convert to a
3983 * canonical representation so we can compare meta-dnode bookmarks to
3984 * non-meta-dnode bookmarks.
3986 * We do this by calculating "equivalents" for each field of the zbookmark.
3987 * zbookmarks outside of the meta-dnode use their own object and level, and
3988 * calculate the level 0 equivalent (the first L0 blkid that is contained in the
3989 * blocks this bookmark refers to) by multiplying their blkid by their span
3990 * (the number of L0 blocks contained within one block at their level).
3991 * zbookmarks inside the meta-dnode calculate their object equivalent
3992 * (which is L0equiv * dnodes per data block), use 0 for their L0equiv, and use
3993 * level + 1<<31 (any value larger than a level could ever be) for their level.
3994 * This causes them to always compare before a bookmark in their object
3995 * equivalent, compare appropriately to bookmarks in other objects, and to
3996 * compare appropriately to other bookmarks in the meta-dnode.
3999 zbookmark_compare(uint16_t dbss1
, uint8_t ibs1
, uint16_t dbss2
, uint8_t ibs2
,
4000 const zbookmark_phys_t
*zb1
, const zbookmark_phys_t
*zb2
)
4003 * These variables represent the "equivalent" values for the zbookmark,
4004 * after converting zbookmarks inside the meta dnode to their
4005 * normal-object equivalents.
4007 uint64_t zb1obj
, zb2obj
;
4008 uint64_t zb1L0
, zb2L0
;
4009 uint64_t zb1level
, zb2level
;
4011 if (zb1
->zb_object
== zb2
->zb_object
&&
4012 zb1
->zb_level
== zb2
->zb_level
&&
4013 zb1
->zb_blkid
== zb2
->zb_blkid
)
4017 * BP_SPANB calculates the span in blocks.
4019 zb1L0
= (zb1
->zb_blkid
) * BP_SPANB(ibs1
, zb1
->zb_level
);
4020 zb2L0
= (zb2
->zb_blkid
) * BP_SPANB(ibs2
, zb2
->zb_level
);
4022 if (zb1
->zb_object
== DMU_META_DNODE_OBJECT
) {
4023 zb1obj
= zb1L0
* (dbss1
<< (SPA_MINBLOCKSHIFT
- DNODE_SHIFT
));
4025 zb1level
= zb1
->zb_level
+ COMPARE_META_LEVEL
;
4027 zb1obj
= zb1
->zb_object
;
4028 zb1level
= zb1
->zb_level
;
4031 if (zb2
->zb_object
== DMU_META_DNODE_OBJECT
) {
4032 zb2obj
= zb2L0
* (dbss2
<< (SPA_MINBLOCKSHIFT
- DNODE_SHIFT
));
4034 zb2level
= zb2
->zb_level
+ COMPARE_META_LEVEL
;
4036 zb2obj
= zb2
->zb_object
;
4037 zb2level
= zb2
->zb_level
;
4040 /* Now that we have a canonical representation, do the comparison. */
4041 if (zb1obj
!= zb2obj
)
4042 return (zb1obj
< zb2obj
? -1 : 1);
4043 else if (zb1L0
!= zb2L0
)
4044 return (zb1L0
< zb2L0
? -1 : 1);
4045 else if (zb1level
!= zb2level
)
4046 return (zb1level
> zb2level
? -1 : 1);
4048 * This can (theoretically) happen if the bookmarks have the same object
4049 * and level, but different blkids, if the block sizes are not the same.
4050 * There is presently no way to change the indirect block sizes
4056 * This function checks the following: given that last_block is the place that
4057 * our traversal stopped last time, does that guarantee that we've visited
4058 * every node under subtree_root? Therefore, we can't just use the raw output
4059 * of zbookmark_compare. We have to pass in a modified version of
4060 * subtree_root; by incrementing the block id, and then checking whether
4061 * last_block is before or equal to that, we can tell whether or not having
4062 * visited last_block implies that all of subtree_root's children have been
4066 zbookmark_subtree_completed(const dnode_phys_t
*dnp
,
4067 const zbookmark_phys_t
*subtree_root
, const zbookmark_phys_t
*last_block
)
4069 zbookmark_phys_t mod_zb
= *subtree_root
;
4071 ASSERT(last_block
->zb_level
== 0);
4073 /* The objset_phys_t isn't before anything. */
4078 * We pass in 1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT) for the
4079 * data block size in sectors, because that variable is only used if
4080 * the bookmark refers to a block in the meta-dnode. Since we don't
4081 * know without examining it what object it refers to, and there's no
4082 * harm in passing in this value in other cases, we always pass it in.
4084 * We pass in 0 for the indirect block size shift because zb2 must be
4085 * level 0. The indirect block size is only used to calculate the span
4086 * of the bookmark, but since the bookmark must be level 0, the span is
4087 * always 1, so the math works out.
4089 * If you make changes to how the zbookmark_compare code works, be sure
4090 * to make sure that this code still works afterwards.
4092 return (zbookmark_compare(dnp
->dn_datablkszsec
, dnp
->dn_indblkshift
,
4093 1ULL << (DNODE_BLOCK_SHIFT
- SPA_MINBLOCKSHIFT
), 0, &mod_zb
,
4097 #if defined(_KERNEL) && defined(HAVE_SPL)
4098 EXPORT_SYMBOL(zio_type_name
);
4099 EXPORT_SYMBOL(zio_buf_alloc
);
4100 EXPORT_SYMBOL(zio_data_buf_alloc
);
4101 EXPORT_SYMBOL(zio_buf_alloc_flags
);
4102 EXPORT_SYMBOL(zio_buf_free
);
4103 EXPORT_SYMBOL(zio_data_buf_free
);
4105 module_param(zio_delay_max
, int, 0644);
4106 MODULE_PARM_DESC(zio_delay_max
, "Max zio millisec delay before posting event");
4108 module_param(zio_requeue_io_start_cut_in_line
, int, 0644);
4109 MODULE_PARM_DESC(zio_requeue_io_start_cut_in_line
, "Prioritize requeued I/O");
4111 module_param(zfs_sync_pass_deferred_free
, int, 0644);
4112 MODULE_PARM_DESC(zfs_sync_pass_deferred_free
,
4113 "Defer frees starting in this pass");
4115 module_param(zfs_sync_pass_dont_compress
, int, 0644);
4116 MODULE_PARM_DESC(zfs_sync_pass_dont_compress
,
4117 "Don't compress starting in this pass");
4119 module_param(zfs_sync_pass_rewrite
, int, 0644);
4120 MODULE_PARM_DESC(zfs_sync_pass_rewrite
,
4121 "Rewrite new bps starting in this pass");
4123 module_param(zio_dva_throttle_enabled
, int, 0644);
4124 MODULE_PARM_DESC(zio_dva_throttle_enabled
,
4125 "Throttle block allocations in the ZIO pipeline");