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, 2018 by Delphix. All rights reserved.
24 * Copyright (c) 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2017, Intel Corporation.
28 #include <sys/sysmacros.h>
29 #include <sys/zfs_context.h>
30 #include <sys/fm/fs/zfs.h>
33 #include <sys/spa_impl.h>
34 #include <sys/vdev_impl.h>
35 #include <sys/zio_impl.h>
36 #include <sys/zio_compress.h>
37 #include <sys/zio_checksum.h>
38 #include <sys/dmu_objset.h>
41 #include <sys/blkptr.h>
42 #include <sys/zfeature.h>
43 #include <sys/dsl_scan.h>
44 #include <sys/metaslab_impl.h>
46 #include <sys/trace_zio.h>
48 #include <sys/dsl_crypt.h>
49 #include <sys/cityhash.h>
52 * ==========================================================================
53 * I/O type descriptions
54 * ==========================================================================
56 const char *zio_type_name
[ZIO_TYPES
] = {
58 * Note: Linux kernel thread name length is limited
59 * so these names will differ from upstream open zfs.
61 "z_null", "z_rd", "z_wr", "z_fr", "z_cl", "z_ioctl"
64 int zio_dva_throttle_enabled
= B_TRUE
;
67 * ==========================================================================
69 * ==========================================================================
71 kmem_cache_t
*zio_cache
;
72 kmem_cache_t
*zio_link_cache
;
73 kmem_cache_t
*zio_buf_cache
[SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
];
74 kmem_cache_t
*zio_data_buf_cache
[SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
];
75 #if defined(ZFS_DEBUG) && !defined(_KERNEL)
76 uint64_t zio_buf_cache_allocs
[SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
];
77 uint64_t zio_buf_cache_frees
[SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
];
80 int zio_delay_max
= ZIO_DELAY_MAX
;
82 #define BP_SPANB(indblkshift, level) \
83 (((uint64_t)1) << ((level) * ((indblkshift) - SPA_BLKPTRSHIFT)))
84 #define COMPARE_META_LEVEL 0x80000000ul
86 * The following actions directly effect the spa's sync-to-convergence logic.
87 * The values below define the sync pass when we start performing the action.
88 * Care should be taken when changing these values as they directly impact
89 * spa_sync() performance. Tuning these values may introduce subtle performance
90 * pathologies and should only be done in the context of performance analysis.
91 * These tunables will eventually be removed and replaced with #defines once
92 * enough analysis has been done to determine optimal values.
94 * The 'zfs_sync_pass_deferred_free' pass must be greater than 1 to ensure that
95 * regular blocks are not deferred.
97 int zfs_sync_pass_deferred_free
= 2; /* defer frees starting in this pass */
98 int zfs_sync_pass_dont_compress
= 5; /* don't compress starting in this pass */
99 int zfs_sync_pass_rewrite
= 2; /* rewrite new bps starting in this pass */
102 * An allocating zio is one that either currently has the DVA allocate
103 * stage set or will have it later in its lifetime.
105 #define IO_IS_ALLOCATING(zio) ((zio)->io_orig_pipeline & ZIO_STAGE_DVA_ALLOCATE)
107 int zio_requeue_io_start_cut_in_line
= 1;
110 int zio_buf_debug_limit
= 16384;
112 int zio_buf_debug_limit
= 0;
115 static inline void __zio_execute(zio_t
*zio
);
117 static void zio_taskq_dispatch(zio_t
*, zio_taskq_type_t
, boolean_t
);
123 vmem_t
*data_alloc_arena
= NULL
;
125 zio_cache
= kmem_cache_create("zio_cache",
126 sizeof (zio_t
), 0, NULL
, NULL
, NULL
, NULL
, NULL
, 0);
127 zio_link_cache
= kmem_cache_create("zio_link_cache",
128 sizeof (zio_link_t
), 0, NULL
, NULL
, NULL
, NULL
, NULL
, 0);
131 * For small buffers, we want a cache for each multiple of
132 * SPA_MINBLOCKSIZE. For larger buffers, we want a cache
133 * for each quarter-power of 2.
135 for (c
= 0; c
< SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
; c
++) {
136 size_t size
= (c
+ 1) << SPA_MINBLOCKSHIFT
;
139 size_t cflags
= (size
> zio_buf_debug_limit
) ? KMC_NODEBUG
: 0;
141 #if defined(_ILP32) && defined(_KERNEL)
143 * Cache size limited to 1M on 32-bit platforms until ARC
144 * buffers no longer require virtual address space.
146 if (size
> zfs_max_recordsize
)
155 * If we are using watchpoints, put each buffer on its own page,
156 * to eliminate the performance overhead of trapping to the
157 * kernel when modifying a non-watched buffer that shares the
158 * page with a watched buffer.
160 if (arc_watch
&& !IS_P2ALIGNED(size
, PAGESIZE
))
163 * Here's the problem - on 4K native devices in userland on
164 * Linux using O_DIRECT, buffers must be 4K aligned or I/O
165 * will fail with EINVAL, causing zdb (and others) to coredump.
166 * Since userland probably doesn't need optimized buffer caches,
167 * we just force 4K alignment on everything.
169 align
= 8 * SPA_MINBLOCKSIZE
;
171 if (size
< PAGESIZE
) {
172 align
= SPA_MINBLOCKSIZE
;
173 } else if (IS_P2ALIGNED(size
, p2
>> 2)) {
180 (void) sprintf(name
, "zio_buf_%lu", (ulong_t
)size
);
181 zio_buf_cache
[c
] = kmem_cache_create(name
, size
,
182 align
, NULL
, NULL
, NULL
, NULL
, NULL
, cflags
);
184 (void) sprintf(name
, "zio_data_buf_%lu", (ulong_t
)size
);
185 zio_data_buf_cache
[c
] = kmem_cache_create(name
, size
,
186 align
, NULL
, NULL
, NULL
, NULL
,
187 data_alloc_arena
, cflags
);
192 ASSERT(zio_buf_cache
[c
] != NULL
);
193 if (zio_buf_cache
[c
- 1] == NULL
)
194 zio_buf_cache
[c
- 1] = zio_buf_cache
[c
];
196 ASSERT(zio_data_buf_cache
[c
] != NULL
);
197 if (zio_data_buf_cache
[c
- 1] == NULL
)
198 zio_data_buf_cache
[c
- 1] = zio_data_buf_cache
[c
];
210 kmem_cache_t
*last_cache
= NULL
;
211 kmem_cache_t
*last_data_cache
= NULL
;
213 for (c
= 0; c
< SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
; c
++) {
216 * Cache size limited to 1M on 32-bit platforms until ARC
217 * buffers no longer require virtual address space.
219 if (((c
+ 1) << SPA_MINBLOCKSHIFT
) > zfs_max_recordsize
)
222 #if defined(ZFS_DEBUG) && !defined(_KERNEL)
223 if (zio_buf_cache_allocs
[c
] != zio_buf_cache_frees
[c
])
224 (void) printf("zio_fini: [%d] %llu != %llu\n",
225 (int)((c
+ 1) << SPA_MINBLOCKSHIFT
),
226 (long long unsigned)zio_buf_cache_allocs
[c
],
227 (long long unsigned)zio_buf_cache_frees
[c
]);
229 if (zio_buf_cache
[c
] != last_cache
) {
230 last_cache
= zio_buf_cache
[c
];
231 kmem_cache_destroy(zio_buf_cache
[c
]);
233 zio_buf_cache
[c
] = NULL
;
235 if (zio_data_buf_cache
[c
] != last_data_cache
) {
236 last_data_cache
= zio_data_buf_cache
[c
];
237 kmem_cache_destroy(zio_data_buf_cache
[c
]);
239 zio_data_buf_cache
[c
] = NULL
;
242 kmem_cache_destroy(zio_link_cache
);
243 kmem_cache_destroy(zio_cache
);
251 * ==========================================================================
252 * Allocate and free I/O buffers
253 * ==========================================================================
257 * Use zio_buf_alloc to allocate ZFS metadata. This data will appear in a
258 * crashdump if the kernel panics, so use it judiciously. Obviously, it's
259 * useful to inspect ZFS metadata, but if possible, we should avoid keeping
260 * excess / transient data in-core during a crashdump.
263 zio_buf_alloc(size_t size
)
265 size_t c
= (size
- 1) >> SPA_MINBLOCKSHIFT
;
267 VERIFY3U(c
, <, SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
);
268 #if defined(ZFS_DEBUG) && !defined(_KERNEL)
269 atomic_add_64(&zio_buf_cache_allocs
[c
], 1);
272 return (kmem_cache_alloc(zio_buf_cache
[c
], KM_PUSHPAGE
));
276 * Use zio_data_buf_alloc to allocate data. The data will not appear in a
277 * crashdump if the kernel panics. This exists so that we will limit the amount
278 * of ZFS data that shows up in a kernel crashdump. (Thus reducing the amount
279 * of kernel heap dumped to disk when the kernel panics)
282 zio_data_buf_alloc(size_t size
)
284 size_t c
= (size
- 1) >> SPA_MINBLOCKSHIFT
;
286 VERIFY3U(c
, <, SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
);
288 return (kmem_cache_alloc(zio_data_buf_cache
[c
], KM_PUSHPAGE
));
292 zio_buf_free(void *buf
, size_t size
)
294 size_t c
= (size
- 1) >> SPA_MINBLOCKSHIFT
;
296 VERIFY3U(c
, <, SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
);
297 #if defined(ZFS_DEBUG) && !defined(_KERNEL)
298 atomic_add_64(&zio_buf_cache_frees
[c
], 1);
301 kmem_cache_free(zio_buf_cache
[c
], buf
);
305 zio_data_buf_free(void *buf
, size_t size
)
307 size_t c
= (size
- 1) >> SPA_MINBLOCKSHIFT
;
309 VERIFY3U(c
, <, SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
);
311 kmem_cache_free(zio_data_buf_cache
[c
], buf
);
315 zio_abd_free(void *abd
, size_t size
)
317 abd_free((abd_t
*)abd
);
321 * ==========================================================================
322 * Push and pop I/O transform buffers
323 * ==========================================================================
326 zio_push_transform(zio_t
*zio
, abd_t
*data
, uint64_t size
, uint64_t bufsize
,
327 zio_transform_func_t
*transform
)
329 zio_transform_t
*zt
= kmem_alloc(sizeof (zio_transform_t
), KM_SLEEP
);
332 * Ensure that anyone expecting this zio to contain a linear ABD isn't
333 * going to get a nasty surprise when they try to access the data.
335 IMPLY(abd_is_linear(zio
->io_abd
), abd_is_linear(data
));
337 zt
->zt_orig_abd
= zio
->io_abd
;
338 zt
->zt_orig_size
= zio
->io_size
;
339 zt
->zt_bufsize
= bufsize
;
340 zt
->zt_transform
= transform
;
342 zt
->zt_next
= zio
->io_transform_stack
;
343 zio
->io_transform_stack
= zt
;
350 zio_pop_transforms(zio_t
*zio
)
354 while ((zt
= zio
->io_transform_stack
) != NULL
) {
355 if (zt
->zt_transform
!= NULL
)
356 zt
->zt_transform(zio
,
357 zt
->zt_orig_abd
, zt
->zt_orig_size
);
359 if (zt
->zt_bufsize
!= 0)
360 abd_free(zio
->io_abd
);
362 zio
->io_abd
= zt
->zt_orig_abd
;
363 zio
->io_size
= zt
->zt_orig_size
;
364 zio
->io_transform_stack
= zt
->zt_next
;
366 kmem_free(zt
, sizeof (zio_transform_t
));
371 * ==========================================================================
372 * I/O transform callbacks for subblocks, decompression, and decryption
373 * ==========================================================================
376 zio_subblock(zio_t
*zio
, abd_t
*data
, uint64_t size
)
378 ASSERT(zio
->io_size
> size
);
380 if (zio
->io_type
== ZIO_TYPE_READ
)
381 abd_copy(data
, zio
->io_abd
, size
);
385 zio_decompress(zio_t
*zio
, abd_t
*data
, uint64_t size
)
387 if (zio
->io_error
== 0) {
388 void *tmp
= abd_borrow_buf(data
, size
);
389 int ret
= zio_decompress_data(BP_GET_COMPRESS(zio
->io_bp
),
390 zio
->io_abd
, tmp
, zio
->io_size
, size
);
391 abd_return_buf_copy(data
, tmp
, size
);
393 if (zio_injection_enabled
&& ret
== 0)
394 ret
= zio_handle_fault_injection(zio
, EINVAL
);
397 zio
->io_error
= SET_ERROR(EIO
);
402 zio_decrypt(zio_t
*zio
, abd_t
*data
, uint64_t size
)
406 blkptr_t
*bp
= zio
->io_bp
;
407 spa_t
*spa
= zio
->io_spa
;
408 uint64_t dsobj
= zio
->io_bookmark
.zb_objset
;
409 uint64_t lsize
= BP_GET_LSIZE(bp
);
410 dmu_object_type_t ot
= BP_GET_TYPE(bp
);
411 uint8_t salt
[ZIO_DATA_SALT_LEN
];
412 uint8_t iv
[ZIO_DATA_IV_LEN
];
413 uint8_t mac
[ZIO_DATA_MAC_LEN
];
414 boolean_t no_crypt
= B_FALSE
;
416 ASSERT(BP_USES_CRYPT(bp
));
417 ASSERT3U(size
, !=, 0);
419 if (zio
->io_error
!= 0)
423 * Verify the cksum of MACs stored in an indirect bp. It will always
424 * be possible to verify this since it does not require an encryption
427 if (BP_HAS_INDIRECT_MAC_CKSUM(bp
)) {
428 zio_crypt_decode_mac_bp(bp
, mac
);
430 if (BP_GET_COMPRESS(bp
) != ZIO_COMPRESS_OFF
) {
432 * We haven't decompressed the data yet, but
433 * zio_crypt_do_indirect_mac_checksum() requires
434 * decompressed data to be able to parse out the MACs
435 * from the indirect block. We decompress it now and
436 * throw away the result after we are finished.
438 tmp
= zio_buf_alloc(lsize
);
439 ret
= zio_decompress_data(BP_GET_COMPRESS(bp
),
440 zio
->io_abd
, tmp
, zio
->io_size
, lsize
);
442 ret
= SET_ERROR(EIO
);
445 ret
= zio_crypt_do_indirect_mac_checksum(B_FALSE
,
446 tmp
, lsize
, BP_SHOULD_BYTESWAP(bp
), mac
);
447 zio_buf_free(tmp
, lsize
);
449 ret
= zio_crypt_do_indirect_mac_checksum_abd(B_FALSE
,
450 zio
->io_abd
, size
, BP_SHOULD_BYTESWAP(bp
), mac
);
452 abd_copy(data
, zio
->io_abd
, size
);
454 if (zio_injection_enabled
&& ot
!= DMU_OT_DNODE
&& ret
== 0) {
455 ret
= zio_handle_decrypt_injection(spa
,
456 &zio
->io_bookmark
, ot
, ECKSUM
);
465 * If this is an authenticated block, just check the MAC. It would be
466 * nice to separate this out into its own flag, but for the moment
467 * enum zio_flag is out of bits.
469 if (BP_IS_AUTHENTICATED(bp
)) {
470 if (ot
== DMU_OT_OBJSET
) {
471 ret
= spa_do_crypt_objset_mac_abd(B_FALSE
, spa
,
472 dsobj
, zio
->io_abd
, size
, BP_SHOULD_BYTESWAP(bp
));
474 zio_crypt_decode_mac_bp(bp
, mac
);
475 ret
= spa_do_crypt_mac_abd(B_FALSE
, spa
, dsobj
,
476 zio
->io_abd
, size
, mac
);
477 if (zio_injection_enabled
&& ret
== 0) {
478 ret
= zio_handle_decrypt_injection(spa
,
479 &zio
->io_bookmark
, ot
, ECKSUM
);
482 abd_copy(data
, zio
->io_abd
, size
);
490 zio_crypt_decode_params_bp(bp
, salt
, iv
);
492 if (ot
== DMU_OT_INTENT_LOG
) {
493 tmp
= abd_borrow_buf_copy(zio
->io_abd
, sizeof (zil_chain_t
));
494 zio_crypt_decode_mac_zil(tmp
, mac
);
495 abd_return_buf(zio
->io_abd
, tmp
, sizeof (zil_chain_t
));
497 zio_crypt_decode_mac_bp(bp
, mac
);
500 ret
= spa_do_crypt_abd(B_FALSE
, spa
, &zio
->io_bookmark
, BP_GET_TYPE(bp
),
501 BP_GET_DEDUP(bp
), BP_SHOULD_BYTESWAP(bp
), salt
, iv
, mac
, size
, data
,
502 zio
->io_abd
, &no_crypt
);
504 abd_copy(data
, zio
->io_abd
, size
);
512 /* assert that the key was found unless this was speculative */
513 ASSERT(ret
!= EACCES
|| (zio
->io_flags
& ZIO_FLAG_SPECULATIVE
));
516 * If there was a decryption / authentication error return EIO as
517 * the io_error. If this was not a speculative zio, create an ereport.
520 zio
->io_error
= SET_ERROR(EIO
);
521 if ((zio
->io_flags
& ZIO_FLAG_SPECULATIVE
) == 0) {
522 spa_log_error(spa
, &zio
->io_bookmark
);
523 zfs_ereport_post(FM_EREPORT_ZFS_AUTHENTICATION
,
524 spa
, NULL
, &zio
->io_bookmark
, zio
, 0, 0);
532 * ==========================================================================
533 * I/O parent/child relationships and pipeline interlocks
534 * ==========================================================================
537 zio_walk_parents(zio_t
*cio
, zio_link_t
**zl
)
539 list_t
*pl
= &cio
->io_parent_list
;
541 *zl
= (*zl
== NULL
) ? list_head(pl
) : list_next(pl
, *zl
);
545 ASSERT((*zl
)->zl_child
== cio
);
546 return ((*zl
)->zl_parent
);
550 zio_walk_children(zio_t
*pio
, zio_link_t
**zl
)
552 list_t
*cl
= &pio
->io_child_list
;
554 ASSERT(MUTEX_HELD(&pio
->io_lock
));
556 *zl
= (*zl
== NULL
) ? list_head(cl
) : list_next(cl
, *zl
);
560 ASSERT((*zl
)->zl_parent
== pio
);
561 return ((*zl
)->zl_child
);
565 zio_unique_parent(zio_t
*cio
)
567 zio_link_t
*zl
= NULL
;
568 zio_t
*pio
= zio_walk_parents(cio
, &zl
);
570 VERIFY3P(zio_walk_parents(cio
, &zl
), ==, NULL
);
575 zio_add_child(zio_t
*pio
, zio_t
*cio
)
577 zio_link_t
*zl
= kmem_cache_alloc(zio_link_cache
, KM_SLEEP
);
580 * Logical I/Os can have logical, gang, or vdev children.
581 * Gang I/Os can have gang or vdev children.
582 * Vdev I/Os can only have vdev children.
583 * The following ASSERT captures all of these constraints.
585 ASSERT3S(cio
->io_child_type
, <=, pio
->io_child_type
);
590 mutex_enter(&pio
->io_lock
);
591 mutex_enter(&cio
->io_lock
);
593 ASSERT(pio
->io_state
[ZIO_WAIT_DONE
] == 0);
595 for (int w
= 0; w
< ZIO_WAIT_TYPES
; w
++)
596 pio
->io_children
[cio
->io_child_type
][w
] += !cio
->io_state
[w
];
598 list_insert_head(&pio
->io_child_list
, zl
);
599 list_insert_head(&cio
->io_parent_list
, zl
);
601 pio
->io_child_count
++;
602 cio
->io_parent_count
++;
604 mutex_exit(&cio
->io_lock
);
605 mutex_exit(&pio
->io_lock
);
609 zio_remove_child(zio_t
*pio
, zio_t
*cio
, zio_link_t
*zl
)
611 ASSERT(zl
->zl_parent
== pio
);
612 ASSERT(zl
->zl_child
== cio
);
614 mutex_enter(&pio
->io_lock
);
615 mutex_enter(&cio
->io_lock
);
617 list_remove(&pio
->io_child_list
, zl
);
618 list_remove(&cio
->io_parent_list
, zl
);
620 pio
->io_child_count
--;
621 cio
->io_parent_count
--;
623 mutex_exit(&cio
->io_lock
);
624 mutex_exit(&pio
->io_lock
);
625 kmem_cache_free(zio_link_cache
, zl
);
629 zio_wait_for_children(zio_t
*zio
, uint8_t childbits
, enum zio_wait_type wait
)
631 boolean_t waiting
= B_FALSE
;
633 mutex_enter(&zio
->io_lock
);
634 ASSERT(zio
->io_stall
== NULL
);
635 for (int c
= 0; c
< ZIO_CHILD_TYPES
; c
++) {
636 if (!(ZIO_CHILD_BIT_IS_SET(childbits
, c
)))
639 uint64_t *countp
= &zio
->io_children
[c
][wait
];
642 ASSERT3U(zio
->io_stage
, !=, ZIO_STAGE_OPEN
);
643 zio
->io_stall
= countp
;
648 mutex_exit(&zio
->io_lock
);
652 __attribute__((always_inline
))
654 zio_notify_parent(zio_t
*pio
, zio_t
*zio
, enum zio_wait_type wait
,
655 zio_t
**next_to_executep
)
657 uint64_t *countp
= &pio
->io_children
[zio
->io_child_type
][wait
];
658 int *errorp
= &pio
->io_child_error
[zio
->io_child_type
];
660 mutex_enter(&pio
->io_lock
);
661 if (zio
->io_error
&& !(zio
->io_flags
& ZIO_FLAG_DONT_PROPAGATE
))
662 *errorp
= zio_worst_error(*errorp
, zio
->io_error
);
663 pio
->io_reexecute
|= zio
->io_reexecute
;
664 ASSERT3U(*countp
, >, 0);
668 if (*countp
== 0 && pio
->io_stall
== countp
) {
669 zio_taskq_type_t type
=
670 pio
->io_stage
< ZIO_STAGE_VDEV_IO_START
? ZIO_TASKQ_ISSUE
:
672 pio
->io_stall
= NULL
;
673 mutex_exit(&pio
->io_lock
);
676 * If we can tell the caller to execute this parent next, do
677 * so. Otherwise dispatch the parent zio as its own task.
679 * Having the caller execute the parent when possible reduces
680 * locking on the zio taskq's, reduces context switch
681 * overhead, and has no recursion penalty. Note that one
682 * read from disk typically causes at least 3 zio's: a
683 * zio_null(), the logical zio_read(), and then a physical
684 * zio. When the physical ZIO completes, we are able to call
685 * zio_done() on all 3 of these zio's from one invocation of
686 * zio_execute() by returning the parent back to
687 * zio_execute(). Since the parent isn't executed until this
688 * thread returns back to zio_execute(), the caller should do
691 * In other cases, dispatching the parent prevents
692 * overflowing the stack when we have deeply nested
693 * parent-child relationships, as we do with the "mega zio"
694 * of writes for spa_sync(), and the chain of ZIL blocks.
696 if (next_to_executep
!= NULL
&& *next_to_executep
== NULL
) {
697 *next_to_executep
= pio
;
699 zio_taskq_dispatch(pio
, type
, B_FALSE
);
702 mutex_exit(&pio
->io_lock
);
707 zio_inherit_child_errors(zio_t
*zio
, enum zio_child c
)
709 if (zio
->io_child_error
[c
] != 0 && zio
->io_error
== 0)
710 zio
->io_error
= zio
->io_child_error
[c
];
714 zio_bookmark_compare(const void *x1
, const void *x2
)
716 const zio_t
*z1
= x1
;
717 const zio_t
*z2
= x2
;
719 if (z1
->io_bookmark
.zb_objset
< z2
->io_bookmark
.zb_objset
)
721 if (z1
->io_bookmark
.zb_objset
> z2
->io_bookmark
.zb_objset
)
724 if (z1
->io_bookmark
.zb_object
< z2
->io_bookmark
.zb_object
)
726 if (z1
->io_bookmark
.zb_object
> z2
->io_bookmark
.zb_object
)
729 if (z1
->io_bookmark
.zb_level
< z2
->io_bookmark
.zb_level
)
731 if (z1
->io_bookmark
.zb_level
> z2
->io_bookmark
.zb_level
)
734 if (z1
->io_bookmark
.zb_blkid
< z2
->io_bookmark
.zb_blkid
)
736 if (z1
->io_bookmark
.zb_blkid
> z2
->io_bookmark
.zb_blkid
)
748 * ==========================================================================
749 * Create the various types of I/O (read, write, free, etc)
750 * ==========================================================================
753 zio_create(zio_t
*pio
, spa_t
*spa
, uint64_t txg
, const blkptr_t
*bp
,
754 abd_t
*data
, uint64_t lsize
, uint64_t psize
, zio_done_func_t
*done
,
755 void *private, zio_type_t type
, zio_priority_t priority
,
756 enum zio_flag flags
, vdev_t
*vd
, uint64_t offset
,
757 const zbookmark_phys_t
*zb
, enum zio_stage stage
,
758 enum zio_stage pipeline
)
762 ASSERT3U(psize
, <=, SPA_MAXBLOCKSIZE
);
763 ASSERT(P2PHASE(psize
, SPA_MINBLOCKSIZE
) == 0);
764 ASSERT(P2PHASE(offset
, SPA_MINBLOCKSIZE
) == 0);
766 ASSERT(!vd
|| spa_config_held(spa
, SCL_STATE_ALL
, RW_READER
));
767 ASSERT(!bp
|| !(flags
& ZIO_FLAG_CONFIG_WRITER
));
768 ASSERT(vd
|| stage
== ZIO_STAGE_OPEN
);
770 IMPLY(lsize
!= psize
, (flags
& ZIO_FLAG_RAW_COMPRESS
) != 0);
772 zio
= kmem_cache_alloc(zio_cache
, KM_SLEEP
);
773 bzero(zio
, sizeof (zio_t
));
775 mutex_init(&zio
->io_lock
, NULL
, MUTEX_NOLOCKDEP
, NULL
);
776 cv_init(&zio
->io_cv
, NULL
, CV_DEFAULT
, NULL
);
778 list_create(&zio
->io_parent_list
, sizeof (zio_link_t
),
779 offsetof(zio_link_t
, zl_parent_node
));
780 list_create(&zio
->io_child_list
, sizeof (zio_link_t
),
781 offsetof(zio_link_t
, zl_child_node
));
782 metaslab_trace_init(&zio
->io_alloc_list
);
785 zio
->io_child_type
= ZIO_CHILD_VDEV
;
786 else if (flags
& ZIO_FLAG_GANG_CHILD
)
787 zio
->io_child_type
= ZIO_CHILD_GANG
;
788 else if (flags
& ZIO_FLAG_DDT_CHILD
)
789 zio
->io_child_type
= ZIO_CHILD_DDT
;
791 zio
->io_child_type
= ZIO_CHILD_LOGICAL
;
794 zio
->io_bp
= (blkptr_t
*)bp
;
795 zio
->io_bp_copy
= *bp
;
796 zio
->io_bp_orig
= *bp
;
797 if (type
!= ZIO_TYPE_WRITE
||
798 zio
->io_child_type
== ZIO_CHILD_DDT
)
799 zio
->io_bp
= &zio
->io_bp_copy
; /* so caller can free */
800 if (zio
->io_child_type
== ZIO_CHILD_LOGICAL
)
801 zio
->io_logical
= zio
;
802 if (zio
->io_child_type
> ZIO_CHILD_GANG
&& BP_IS_GANG(bp
))
803 pipeline
|= ZIO_GANG_STAGES
;
809 zio
->io_private
= private;
811 zio
->io_priority
= priority
;
813 zio
->io_offset
= offset
;
814 zio
->io_orig_abd
= zio
->io_abd
= data
;
815 zio
->io_orig_size
= zio
->io_size
= psize
;
816 zio
->io_lsize
= lsize
;
817 zio
->io_orig_flags
= zio
->io_flags
= flags
;
818 zio
->io_orig_stage
= zio
->io_stage
= stage
;
819 zio
->io_orig_pipeline
= zio
->io_pipeline
= pipeline
;
820 zio
->io_pipeline_trace
= ZIO_STAGE_OPEN
;
822 zio
->io_state
[ZIO_WAIT_READY
] = (stage
>= ZIO_STAGE_READY
);
823 zio
->io_state
[ZIO_WAIT_DONE
] = (stage
>= ZIO_STAGE_DONE
);
826 zio
->io_bookmark
= *zb
;
829 if (zio
->io_metaslab_class
== NULL
)
830 zio
->io_metaslab_class
= pio
->io_metaslab_class
;
831 if (zio
->io_logical
== NULL
)
832 zio
->io_logical
= pio
->io_logical
;
833 if (zio
->io_child_type
== ZIO_CHILD_GANG
)
834 zio
->io_gang_leader
= pio
->io_gang_leader
;
835 zio_add_child(pio
, zio
);
838 taskq_init_ent(&zio
->io_tqent
);
844 zio_destroy(zio_t
*zio
)
846 metaslab_trace_fini(&zio
->io_alloc_list
);
847 list_destroy(&zio
->io_parent_list
);
848 list_destroy(&zio
->io_child_list
);
849 mutex_destroy(&zio
->io_lock
);
850 cv_destroy(&zio
->io_cv
);
851 kmem_cache_free(zio_cache
, zio
);
855 zio_null(zio_t
*pio
, spa_t
*spa
, vdev_t
*vd
, zio_done_func_t
*done
,
856 void *private, enum zio_flag flags
)
860 zio
= zio_create(pio
, spa
, 0, NULL
, NULL
, 0, 0, done
, private,
861 ZIO_TYPE_NULL
, ZIO_PRIORITY_NOW
, flags
, vd
, 0, NULL
,
862 ZIO_STAGE_OPEN
, ZIO_INTERLOCK_PIPELINE
);
868 zio_root(spa_t
*spa
, zio_done_func_t
*done
, void *private, enum zio_flag flags
)
870 return (zio_null(NULL
, spa
, NULL
, done
, private, flags
));
874 zfs_blkptr_verify(spa_t
*spa
, const blkptr_t
*bp
)
876 if (!DMU_OT_IS_VALID(BP_GET_TYPE(bp
))) {
877 zfs_panic_recover("blkptr at %p has invalid TYPE %llu",
878 bp
, (longlong_t
)BP_GET_TYPE(bp
));
880 if (BP_GET_CHECKSUM(bp
) >= ZIO_CHECKSUM_FUNCTIONS
||
881 BP_GET_CHECKSUM(bp
) <= ZIO_CHECKSUM_ON
) {
882 zfs_panic_recover("blkptr at %p has invalid CHECKSUM %llu",
883 bp
, (longlong_t
)BP_GET_CHECKSUM(bp
));
885 if (BP_GET_COMPRESS(bp
) >= ZIO_COMPRESS_FUNCTIONS
||
886 BP_GET_COMPRESS(bp
) <= ZIO_COMPRESS_ON
) {
887 zfs_panic_recover("blkptr at %p has invalid COMPRESS %llu",
888 bp
, (longlong_t
)BP_GET_COMPRESS(bp
));
890 if (BP_GET_LSIZE(bp
) > SPA_MAXBLOCKSIZE
) {
891 zfs_panic_recover("blkptr at %p has invalid LSIZE %llu",
892 bp
, (longlong_t
)BP_GET_LSIZE(bp
));
894 if (BP_GET_PSIZE(bp
) > SPA_MAXBLOCKSIZE
) {
895 zfs_panic_recover("blkptr at %p has invalid PSIZE %llu",
896 bp
, (longlong_t
)BP_GET_PSIZE(bp
));
899 if (BP_IS_EMBEDDED(bp
)) {
900 if (BPE_GET_ETYPE(bp
) > NUM_BP_EMBEDDED_TYPES
) {
901 zfs_panic_recover("blkptr at %p has invalid ETYPE %llu",
902 bp
, (longlong_t
)BPE_GET_ETYPE(bp
));
907 * Do not verify individual DVAs if the config is not trusted. This
908 * will be done once the zio is executed in vdev_mirror_map_alloc.
910 if (!spa
->spa_trust_config
)
914 * Pool-specific checks.
916 * Note: it would be nice to verify that the blk_birth and
917 * BP_PHYSICAL_BIRTH() are not too large. However, spa_freeze()
918 * allows the birth time of log blocks (and dmu_sync()-ed blocks
919 * that are in the log) to be arbitrarily large.
921 for (int i
= 0; i
< BP_GET_NDVAS(bp
); i
++) {
922 uint64_t vdevid
= DVA_GET_VDEV(&bp
->blk_dva
[i
]);
924 if (vdevid
>= spa
->spa_root_vdev
->vdev_children
) {
925 zfs_panic_recover("blkptr at %p DVA %u has invalid "
927 bp
, i
, (longlong_t
)vdevid
);
930 vdev_t
*vd
= spa
->spa_root_vdev
->vdev_child
[vdevid
];
932 zfs_panic_recover("blkptr at %p DVA %u has invalid "
934 bp
, i
, (longlong_t
)vdevid
);
937 if (vd
->vdev_ops
== &vdev_hole_ops
) {
938 zfs_panic_recover("blkptr at %p DVA %u has hole "
940 bp
, i
, (longlong_t
)vdevid
);
943 if (vd
->vdev_ops
== &vdev_missing_ops
) {
945 * "missing" vdevs are valid during import, but we
946 * don't have their detailed info (e.g. asize), so
947 * we can't perform any more checks on them.
951 uint64_t offset
= DVA_GET_OFFSET(&bp
->blk_dva
[i
]);
952 uint64_t asize
= DVA_GET_ASIZE(&bp
->blk_dva
[i
]);
954 asize
= vdev_psize_to_asize(vd
, SPA_GANGBLOCKSIZE
);
955 if (offset
+ asize
> vd
->vdev_asize
) {
956 zfs_panic_recover("blkptr at %p DVA %u has invalid "
958 bp
, i
, (longlong_t
)offset
);
964 zfs_dva_valid(spa_t
*spa
, const dva_t
*dva
, const blkptr_t
*bp
)
966 uint64_t vdevid
= DVA_GET_VDEV(dva
);
968 if (vdevid
>= spa
->spa_root_vdev
->vdev_children
)
971 vdev_t
*vd
= spa
->spa_root_vdev
->vdev_child
[vdevid
];
975 if (vd
->vdev_ops
== &vdev_hole_ops
)
978 if (vd
->vdev_ops
== &vdev_missing_ops
) {
982 uint64_t offset
= DVA_GET_OFFSET(dva
);
983 uint64_t asize
= DVA_GET_ASIZE(dva
);
986 asize
= vdev_psize_to_asize(vd
, SPA_GANGBLOCKSIZE
);
987 if (offset
+ asize
> vd
->vdev_asize
)
994 zio_read(zio_t
*pio
, spa_t
*spa
, const blkptr_t
*bp
,
995 abd_t
*data
, uint64_t size
, zio_done_func_t
*done
, void *private,
996 zio_priority_t priority
, enum zio_flag flags
, const zbookmark_phys_t
*zb
)
1000 zfs_blkptr_verify(spa
, bp
);
1002 zio
= zio_create(pio
, spa
, BP_PHYSICAL_BIRTH(bp
), bp
,
1003 data
, size
, size
, done
, private,
1004 ZIO_TYPE_READ
, priority
, flags
, NULL
, 0, zb
,
1005 ZIO_STAGE_OPEN
, (flags
& ZIO_FLAG_DDT_CHILD
) ?
1006 ZIO_DDT_CHILD_READ_PIPELINE
: ZIO_READ_PIPELINE
);
1012 zio_write(zio_t
*pio
, spa_t
*spa
, uint64_t txg
, blkptr_t
*bp
,
1013 abd_t
*data
, uint64_t lsize
, uint64_t psize
, const zio_prop_t
*zp
,
1014 zio_done_func_t
*ready
, zio_done_func_t
*children_ready
,
1015 zio_done_func_t
*physdone
, zio_done_func_t
*done
,
1016 void *private, zio_priority_t priority
, enum zio_flag flags
,
1017 const zbookmark_phys_t
*zb
)
1021 ASSERT(zp
->zp_checksum
>= ZIO_CHECKSUM_OFF
&&
1022 zp
->zp_checksum
< ZIO_CHECKSUM_FUNCTIONS
&&
1023 zp
->zp_compress
>= ZIO_COMPRESS_OFF
&&
1024 zp
->zp_compress
< ZIO_COMPRESS_FUNCTIONS
&&
1025 DMU_OT_IS_VALID(zp
->zp_type
) &&
1026 zp
->zp_level
< 32 &&
1027 zp
->zp_copies
> 0 &&
1028 zp
->zp_copies
<= spa_max_replication(spa
));
1030 zio
= zio_create(pio
, spa
, txg
, bp
, data
, lsize
, psize
, done
, private,
1031 ZIO_TYPE_WRITE
, priority
, flags
, NULL
, 0, zb
,
1032 ZIO_STAGE_OPEN
, (flags
& ZIO_FLAG_DDT_CHILD
) ?
1033 ZIO_DDT_CHILD_WRITE_PIPELINE
: ZIO_WRITE_PIPELINE
);
1035 zio
->io_ready
= ready
;
1036 zio
->io_children_ready
= children_ready
;
1037 zio
->io_physdone
= physdone
;
1041 * Data can be NULL if we are going to call zio_write_override() to
1042 * provide the already-allocated BP. But we may need the data to
1043 * verify a dedup hit (if requested). In this case, don't try to
1044 * dedup (just take the already-allocated BP verbatim). Encrypted
1045 * dedup blocks need data as well so we also disable dedup in this
1049 (zio
->io_prop
.zp_dedup_verify
|| zio
->io_prop
.zp_encrypt
)) {
1050 zio
->io_prop
.zp_dedup
= zio
->io_prop
.zp_dedup_verify
= B_FALSE
;
1057 zio_rewrite(zio_t
*pio
, spa_t
*spa
, uint64_t txg
, blkptr_t
*bp
, abd_t
*data
,
1058 uint64_t size
, zio_done_func_t
*done
, void *private,
1059 zio_priority_t priority
, enum zio_flag flags
, zbookmark_phys_t
*zb
)
1063 zio
= zio_create(pio
, spa
, txg
, bp
, data
, size
, size
, done
, private,
1064 ZIO_TYPE_WRITE
, priority
, flags
| ZIO_FLAG_IO_REWRITE
, NULL
, 0, zb
,
1065 ZIO_STAGE_OPEN
, ZIO_REWRITE_PIPELINE
);
1071 zio_write_override(zio_t
*zio
, blkptr_t
*bp
, int copies
, boolean_t nopwrite
)
1073 ASSERT(zio
->io_type
== ZIO_TYPE_WRITE
);
1074 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
1075 ASSERT(zio
->io_stage
== ZIO_STAGE_OPEN
);
1076 ASSERT(zio
->io_txg
== spa_syncing_txg(zio
->io_spa
));
1079 * We must reset the io_prop to match the values that existed
1080 * when the bp was first written by dmu_sync() keeping in mind
1081 * that nopwrite and dedup are mutually exclusive.
1083 zio
->io_prop
.zp_dedup
= nopwrite
? B_FALSE
: zio
->io_prop
.zp_dedup
;
1084 zio
->io_prop
.zp_nopwrite
= nopwrite
;
1085 zio
->io_prop
.zp_copies
= copies
;
1086 zio
->io_bp_override
= bp
;
1090 zio_free(spa_t
*spa
, uint64_t txg
, const blkptr_t
*bp
)
1093 zfs_blkptr_verify(spa
, bp
);
1096 * The check for EMBEDDED is a performance optimization. We
1097 * process the free here (by ignoring it) rather than
1098 * putting it on the list and then processing it in zio_free_sync().
1100 if (BP_IS_EMBEDDED(bp
))
1102 metaslab_check_free(spa
, bp
);
1105 * Frees that are for the currently-syncing txg, are not going to be
1106 * deferred, and which will not need to do a read (i.e. not GANG or
1107 * DEDUP), can be processed immediately. Otherwise, put them on the
1108 * in-memory list for later processing.
1110 if (BP_IS_GANG(bp
) || BP_GET_DEDUP(bp
) ||
1111 txg
!= spa
->spa_syncing_txg
||
1112 spa_sync_pass(spa
) >= zfs_sync_pass_deferred_free
) {
1113 bplist_append(&spa
->spa_free_bplist
[txg
& TXG_MASK
], bp
);
1115 VERIFY0(zio_wait(zio_free_sync(NULL
, spa
, txg
, bp
, 0)));
1120 zio_free_sync(zio_t
*pio
, spa_t
*spa
, uint64_t txg
, const blkptr_t
*bp
,
1121 enum zio_flag flags
)
1124 enum zio_stage stage
= ZIO_FREE_PIPELINE
;
1126 ASSERT(!BP_IS_HOLE(bp
));
1127 ASSERT(spa_syncing_txg(spa
) == txg
);
1128 ASSERT(spa_sync_pass(spa
) < zfs_sync_pass_deferred_free
);
1130 if (BP_IS_EMBEDDED(bp
))
1131 return (zio_null(pio
, spa
, NULL
, NULL
, NULL
, 0));
1133 metaslab_check_free(spa
, bp
);
1135 dsl_scan_freed(spa
, bp
);
1138 * GANG and DEDUP blocks can induce a read (for the gang block header,
1139 * or the DDT), so issue them asynchronously so that this thread is
1142 if (BP_IS_GANG(bp
) || BP_GET_DEDUP(bp
))
1143 stage
|= ZIO_STAGE_ISSUE_ASYNC
;
1145 zio
= zio_create(pio
, spa
, txg
, bp
, NULL
, BP_GET_PSIZE(bp
),
1146 BP_GET_PSIZE(bp
), NULL
, NULL
, ZIO_TYPE_FREE
, ZIO_PRIORITY_NOW
,
1147 flags
, NULL
, 0, NULL
, ZIO_STAGE_OPEN
, stage
);
1153 zio_claim(zio_t
*pio
, spa_t
*spa
, uint64_t txg
, const blkptr_t
*bp
,
1154 zio_done_func_t
*done
, void *private, enum zio_flag flags
)
1158 zfs_blkptr_verify(spa
, bp
);
1160 if (BP_IS_EMBEDDED(bp
))
1161 return (zio_null(pio
, spa
, NULL
, NULL
, NULL
, 0));
1164 * A claim is an allocation of a specific block. Claims are needed
1165 * to support immediate writes in the intent log. The issue is that
1166 * immediate writes contain committed data, but in a txg that was
1167 * *not* committed. Upon opening the pool after an unclean shutdown,
1168 * the intent log claims all blocks that contain immediate write data
1169 * so that the SPA knows they're in use.
1171 * All claims *must* be resolved in the first txg -- before the SPA
1172 * starts allocating blocks -- so that nothing is allocated twice.
1173 * If txg == 0 we just verify that the block is claimable.
1175 ASSERT3U(spa
->spa_uberblock
.ub_rootbp
.blk_birth
, <,
1176 spa_min_claim_txg(spa
));
1177 ASSERT(txg
== spa_min_claim_txg(spa
) || txg
== 0);
1178 ASSERT(!BP_GET_DEDUP(bp
) || !spa_writeable(spa
)); /* zdb(1M) */
1180 zio
= zio_create(pio
, spa
, txg
, bp
, NULL
, BP_GET_PSIZE(bp
),
1181 BP_GET_PSIZE(bp
), done
, private, ZIO_TYPE_CLAIM
, ZIO_PRIORITY_NOW
,
1182 flags
, NULL
, 0, NULL
, ZIO_STAGE_OPEN
, ZIO_CLAIM_PIPELINE
);
1183 ASSERT0(zio
->io_queued_timestamp
);
1189 zio_ioctl(zio_t
*pio
, spa_t
*spa
, vdev_t
*vd
, int cmd
,
1190 zio_done_func_t
*done
, void *private, enum zio_flag flags
)
1195 if (vd
->vdev_children
== 0) {
1196 zio
= zio_create(pio
, spa
, 0, NULL
, NULL
, 0, 0, done
, private,
1197 ZIO_TYPE_IOCTL
, ZIO_PRIORITY_NOW
, flags
, vd
, 0, NULL
,
1198 ZIO_STAGE_OPEN
, ZIO_IOCTL_PIPELINE
);
1202 zio
= zio_null(pio
, spa
, NULL
, NULL
, NULL
, flags
);
1204 for (c
= 0; c
< vd
->vdev_children
; c
++)
1205 zio_nowait(zio_ioctl(zio
, spa
, vd
->vdev_child
[c
], cmd
,
1206 done
, private, flags
));
1213 zio_read_phys(zio_t
*pio
, vdev_t
*vd
, uint64_t offset
, uint64_t size
,
1214 abd_t
*data
, int checksum
, zio_done_func_t
*done
, void *private,
1215 zio_priority_t priority
, enum zio_flag flags
, boolean_t labels
)
1219 ASSERT(vd
->vdev_children
== 0);
1220 ASSERT(!labels
|| offset
+ size
<= VDEV_LABEL_START_SIZE
||
1221 offset
>= vd
->vdev_psize
- VDEV_LABEL_END_SIZE
);
1222 ASSERT3U(offset
+ size
, <=, vd
->vdev_psize
);
1224 zio
= zio_create(pio
, vd
->vdev_spa
, 0, NULL
, data
, size
, size
, done
,
1225 private, ZIO_TYPE_READ
, priority
, flags
| ZIO_FLAG_PHYSICAL
, vd
,
1226 offset
, NULL
, ZIO_STAGE_OPEN
, ZIO_READ_PHYS_PIPELINE
);
1228 zio
->io_prop
.zp_checksum
= checksum
;
1234 zio_write_phys(zio_t
*pio
, vdev_t
*vd
, uint64_t offset
, uint64_t size
,
1235 abd_t
*data
, int checksum
, zio_done_func_t
*done
, void *private,
1236 zio_priority_t priority
, enum zio_flag flags
, boolean_t labels
)
1240 ASSERT(vd
->vdev_children
== 0);
1241 ASSERT(!labels
|| offset
+ size
<= VDEV_LABEL_START_SIZE
||
1242 offset
>= vd
->vdev_psize
- VDEV_LABEL_END_SIZE
);
1243 ASSERT3U(offset
+ size
, <=, vd
->vdev_psize
);
1245 zio
= zio_create(pio
, vd
->vdev_spa
, 0, NULL
, data
, size
, size
, done
,
1246 private, ZIO_TYPE_WRITE
, priority
, flags
| ZIO_FLAG_PHYSICAL
, vd
,
1247 offset
, NULL
, ZIO_STAGE_OPEN
, ZIO_WRITE_PHYS_PIPELINE
);
1249 zio
->io_prop
.zp_checksum
= checksum
;
1251 if (zio_checksum_table
[checksum
].ci_flags
& ZCHECKSUM_FLAG_EMBEDDED
) {
1253 * zec checksums are necessarily destructive -- they modify
1254 * the end of the write buffer to hold the verifier/checksum.
1255 * Therefore, we must make a local copy in case the data is
1256 * being written to multiple places in parallel.
1258 abd_t
*wbuf
= abd_alloc_sametype(data
, size
);
1259 abd_copy(wbuf
, data
, size
);
1261 zio_push_transform(zio
, wbuf
, size
, size
, NULL
);
1268 * Create a child I/O to do some work for us.
1271 zio_vdev_child_io(zio_t
*pio
, blkptr_t
*bp
, vdev_t
*vd
, uint64_t offset
,
1272 abd_t
*data
, uint64_t size
, int type
, zio_priority_t priority
,
1273 enum zio_flag flags
, zio_done_func_t
*done
, void *private)
1275 enum zio_stage pipeline
= ZIO_VDEV_CHILD_PIPELINE
;
1279 * vdev child I/Os do not propagate their error to the parent.
1280 * Therefore, for correct operation the caller *must* check for
1281 * and handle the error in the child i/o's done callback.
1282 * The only exceptions are i/os that we don't care about
1283 * (OPTIONAL or REPAIR).
1285 ASSERT((flags
& ZIO_FLAG_OPTIONAL
) || (flags
& ZIO_FLAG_IO_REPAIR
) ||
1288 if (type
== ZIO_TYPE_READ
&& bp
!= NULL
) {
1290 * If we have the bp, then the child should perform the
1291 * checksum and the parent need not. This pushes error
1292 * detection as close to the leaves as possible and
1293 * eliminates redundant checksums in the interior nodes.
1295 pipeline
|= ZIO_STAGE_CHECKSUM_VERIFY
;
1296 pio
->io_pipeline
&= ~ZIO_STAGE_CHECKSUM_VERIFY
;
1299 if (vd
->vdev_ops
->vdev_op_leaf
) {
1300 ASSERT0(vd
->vdev_children
);
1301 offset
+= VDEV_LABEL_START_SIZE
;
1304 flags
|= ZIO_VDEV_CHILD_FLAGS(pio
);
1307 * If we've decided to do a repair, the write is not speculative --
1308 * even if the original read was.
1310 if (flags
& ZIO_FLAG_IO_REPAIR
)
1311 flags
&= ~ZIO_FLAG_SPECULATIVE
;
1314 * If we're creating a child I/O that is not associated with a
1315 * top-level vdev, then the child zio is not an allocating I/O.
1316 * If this is a retried I/O then we ignore it since we will
1317 * have already processed the original allocating I/O.
1319 if (flags
& ZIO_FLAG_IO_ALLOCATING
&&
1320 (vd
!= vd
->vdev_top
|| (flags
& ZIO_FLAG_IO_RETRY
))) {
1321 ASSERT(pio
->io_metaslab_class
!= NULL
);
1322 ASSERT(pio
->io_metaslab_class
->mc_alloc_throttle_enabled
);
1323 ASSERT(type
== ZIO_TYPE_WRITE
);
1324 ASSERT(priority
== ZIO_PRIORITY_ASYNC_WRITE
);
1325 ASSERT(!(flags
& ZIO_FLAG_IO_REPAIR
));
1326 ASSERT(!(pio
->io_flags
& ZIO_FLAG_IO_REWRITE
) ||
1327 pio
->io_child_type
== ZIO_CHILD_GANG
);
1329 flags
&= ~ZIO_FLAG_IO_ALLOCATING
;
1333 zio
= zio_create(pio
, pio
->io_spa
, pio
->io_txg
, bp
, data
, size
, size
,
1334 done
, private, type
, priority
, flags
, vd
, offset
, &pio
->io_bookmark
,
1335 ZIO_STAGE_VDEV_IO_START
>> 1, pipeline
);
1336 ASSERT3U(zio
->io_child_type
, ==, ZIO_CHILD_VDEV
);
1338 zio
->io_physdone
= pio
->io_physdone
;
1339 if (vd
->vdev_ops
->vdev_op_leaf
&& zio
->io_logical
!= NULL
)
1340 zio
->io_logical
->io_phys_children
++;
1346 zio_vdev_delegated_io(vdev_t
*vd
, uint64_t offset
, abd_t
*data
, uint64_t size
,
1347 zio_type_t type
, zio_priority_t priority
, enum zio_flag flags
,
1348 zio_done_func_t
*done
, void *private)
1352 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
1354 zio
= zio_create(NULL
, vd
->vdev_spa
, 0, NULL
,
1355 data
, size
, size
, done
, private, type
, priority
,
1356 flags
| ZIO_FLAG_CANFAIL
| ZIO_FLAG_DONT_RETRY
| ZIO_FLAG_DELEGATED
,
1358 ZIO_STAGE_VDEV_IO_START
>> 1, ZIO_VDEV_CHILD_PIPELINE
);
1364 zio_flush(zio_t
*zio
, vdev_t
*vd
)
1366 zio_nowait(zio_ioctl(zio
, zio
->io_spa
, vd
, DKIOCFLUSHWRITECACHE
,
1368 ZIO_FLAG_CANFAIL
| ZIO_FLAG_DONT_PROPAGATE
| ZIO_FLAG_DONT_RETRY
));
1372 zio_shrink(zio_t
*zio
, uint64_t size
)
1374 ASSERT3P(zio
->io_executor
, ==, NULL
);
1375 ASSERT3U(zio
->io_orig_size
, ==, zio
->io_size
);
1376 ASSERT3U(size
, <=, zio
->io_size
);
1379 * We don't shrink for raidz because of problems with the
1380 * reconstruction when reading back less than the block size.
1381 * Note, BP_IS_RAIDZ() assumes no compression.
1383 ASSERT(BP_GET_COMPRESS(zio
->io_bp
) == ZIO_COMPRESS_OFF
);
1384 if (!BP_IS_RAIDZ(zio
->io_bp
)) {
1385 /* we are not doing a raw write */
1386 ASSERT3U(zio
->io_size
, ==, zio
->io_lsize
);
1387 zio
->io_orig_size
= zio
->io_size
= zio
->io_lsize
= size
;
1392 * ==========================================================================
1393 * Prepare to read and write logical blocks
1394 * ==========================================================================
1398 zio_read_bp_init(zio_t
*zio
)
1400 blkptr_t
*bp
= zio
->io_bp
;
1402 BP_IS_EMBEDDED(bp
) ? BPE_GET_PSIZE(bp
) : BP_GET_PSIZE(bp
);
1404 ASSERT3P(zio
->io_bp
, ==, &zio
->io_bp_copy
);
1406 if (BP_GET_COMPRESS(bp
) != ZIO_COMPRESS_OFF
&&
1407 zio
->io_child_type
== ZIO_CHILD_LOGICAL
&&
1408 !(zio
->io_flags
& ZIO_FLAG_RAW_COMPRESS
)) {
1409 zio_push_transform(zio
, abd_alloc_sametype(zio
->io_abd
, psize
),
1410 psize
, psize
, zio_decompress
);
1413 if (((BP_IS_PROTECTED(bp
) && !(zio
->io_flags
& ZIO_FLAG_RAW_ENCRYPT
)) ||
1414 BP_HAS_INDIRECT_MAC_CKSUM(bp
)) &&
1415 zio
->io_child_type
== ZIO_CHILD_LOGICAL
) {
1416 zio_push_transform(zio
, abd_alloc_sametype(zio
->io_abd
, psize
),
1417 psize
, psize
, zio_decrypt
);
1420 if (BP_IS_EMBEDDED(bp
) && BPE_GET_ETYPE(bp
) == BP_EMBEDDED_TYPE_DATA
) {
1421 int psize
= BPE_GET_PSIZE(bp
);
1422 void *data
= abd_borrow_buf(zio
->io_abd
, psize
);
1424 zio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
1425 decode_embedded_bp_compressed(bp
, data
);
1426 abd_return_buf_copy(zio
->io_abd
, data
, psize
);
1428 ASSERT(!BP_IS_EMBEDDED(bp
));
1429 ASSERT3P(zio
->io_bp
, ==, &zio
->io_bp_copy
);
1432 if (!DMU_OT_IS_METADATA(BP_GET_TYPE(bp
)) && BP_GET_LEVEL(bp
) == 0)
1433 zio
->io_flags
|= ZIO_FLAG_DONT_CACHE
;
1435 if (BP_GET_TYPE(bp
) == DMU_OT_DDT_ZAP
)
1436 zio
->io_flags
|= ZIO_FLAG_DONT_CACHE
;
1438 if (BP_GET_DEDUP(bp
) && zio
->io_child_type
== ZIO_CHILD_LOGICAL
)
1439 zio
->io_pipeline
= ZIO_DDT_READ_PIPELINE
;
1445 zio_write_bp_init(zio_t
*zio
)
1447 if (!IO_IS_ALLOCATING(zio
))
1450 ASSERT(zio
->io_child_type
!= ZIO_CHILD_DDT
);
1452 if (zio
->io_bp_override
) {
1453 blkptr_t
*bp
= zio
->io_bp
;
1454 zio_prop_t
*zp
= &zio
->io_prop
;
1456 ASSERT(bp
->blk_birth
!= zio
->io_txg
);
1457 ASSERT(BP_GET_DEDUP(zio
->io_bp_override
) == 0);
1459 *bp
= *zio
->io_bp_override
;
1460 zio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
1462 if (BP_IS_EMBEDDED(bp
))
1466 * If we've been overridden and nopwrite is set then
1467 * set the flag accordingly to indicate that a nopwrite
1468 * has already occurred.
1470 if (!BP_IS_HOLE(bp
) && zp
->zp_nopwrite
) {
1471 ASSERT(!zp
->zp_dedup
);
1472 ASSERT3U(BP_GET_CHECKSUM(bp
), ==, zp
->zp_checksum
);
1473 zio
->io_flags
|= ZIO_FLAG_NOPWRITE
;
1477 ASSERT(!zp
->zp_nopwrite
);
1479 if (BP_IS_HOLE(bp
) || !zp
->zp_dedup
)
1482 ASSERT((zio_checksum_table
[zp
->zp_checksum
].ci_flags
&
1483 ZCHECKSUM_FLAG_DEDUP
) || zp
->zp_dedup_verify
);
1485 if (BP_GET_CHECKSUM(bp
) == zp
->zp_checksum
&&
1487 BP_SET_DEDUP(bp
, 1);
1488 zio
->io_pipeline
|= ZIO_STAGE_DDT_WRITE
;
1493 * We were unable to handle this as an override bp, treat
1494 * it as a regular write I/O.
1496 zio
->io_bp_override
= NULL
;
1497 *bp
= zio
->io_bp_orig
;
1498 zio
->io_pipeline
= zio
->io_orig_pipeline
;
1505 zio_write_compress(zio_t
*zio
)
1507 spa_t
*spa
= zio
->io_spa
;
1508 zio_prop_t
*zp
= &zio
->io_prop
;
1509 enum zio_compress compress
= zp
->zp_compress
;
1510 blkptr_t
*bp
= zio
->io_bp
;
1511 uint64_t lsize
= zio
->io_lsize
;
1512 uint64_t psize
= zio
->io_size
;
1516 * If our children haven't all reached the ready stage,
1517 * wait for them and then repeat this pipeline stage.
1519 if (zio_wait_for_children(zio
, ZIO_CHILD_LOGICAL_BIT
|
1520 ZIO_CHILD_GANG_BIT
, ZIO_WAIT_READY
)) {
1524 if (!IO_IS_ALLOCATING(zio
))
1527 if (zio
->io_children_ready
!= NULL
) {
1529 * Now that all our children are ready, run the callback
1530 * associated with this zio in case it wants to modify the
1531 * data to be written.
1533 ASSERT3U(zp
->zp_level
, >, 0);
1534 zio
->io_children_ready(zio
);
1537 ASSERT(zio
->io_child_type
!= ZIO_CHILD_DDT
);
1538 ASSERT(zio
->io_bp_override
== NULL
);
1540 if (!BP_IS_HOLE(bp
) && bp
->blk_birth
== zio
->io_txg
) {
1542 * We're rewriting an existing block, which means we're
1543 * working on behalf of spa_sync(). For spa_sync() to
1544 * converge, it must eventually be the case that we don't
1545 * have to allocate new blocks. But compression changes
1546 * the blocksize, which forces a reallocate, and makes
1547 * convergence take longer. Therefore, after the first
1548 * few passes, stop compressing to ensure convergence.
1550 pass
= spa_sync_pass(spa
);
1552 ASSERT(zio
->io_txg
== spa_syncing_txg(spa
));
1553 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
1554 ASSERT(!BP_GET_DEDUP(bp
));
1556 if (pass
>= zfs_sync_pass_dont_compress
)
1557 compress
= ZIO_COMPRESS_OFF
;
1559 /* Make sure someone doesn't change their mind on overwrites */
1560 ASSERT(BP_IS_EMBEDDED(bp
) || MIN(zp
->zp_copies
+ BP_IS_GANG(bp
),
1561 spa_max_replication(spa
)) == BP_GET_NDVAS(bp
));
1564 /* If it's a compressed write that is not raw, compress the buffer. */
1565 if (compress
!= ZIO_COMPRESS_OFF
&&
1566 !(zio
->io_flags
& ZIO_FLAG_RAW_COMPRESS
)) {
1567 void *cbuf
= zio_buf_alloc(lsize
);
1568 psize
= zio_compress_data(compress
, zio
->io_abd
, cbuf
, lsize
);
1569 if (psize
== 0 || psize
== lsize
) {
1570 compress
= ZIO_COMPRESS_OFF
;
1571 zio_buf_free(cbuf
, lsize
);
1572 } else if (!zp
->zp_dedup
&& !zp
->zp_encrypt
&&
1573 psize
<= BPE_PAYLOAD_SIZE
&&
1574 zp
->zp_level
== 0 && !DMU_OT_HAS_FILL(zp
->zp_type
) &&
1575 spa_feature_is_enabled(spa
, SPA_FEATURE_EMBEDDED_DATA
)) {
1576 encode_embedded_bp_compressed(bp
,
1577 cbuf
, compress
, lsize
, psize
);
1578 BPE_SET_ETYPE(bp
, BP_EMBEDDED_TYPE_DATA
);
1579 BP_SET_TYPE(bp
, zio
->io_prop
.zp_type
);
1580 BP_SET_LEVEL(bp
, zio
->io_prop
.zp_level
);
1581 zio_buf_free(cbuf
, lsize
);
1582 bp
->blk_birth
= zio
->io_txg
;
1583 zio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
1584 ASSERT(spa_feature_is_active(spa
,
1585 SPA_FEATURE_EMBEDDED_DATA
));
1589 * Round up compressed size up to the ashift
1590 * of the smallest-ashift device, and zero the tail.
1591 * This ensures that the compressed size of the BP
1592 * (and thus compressratio property) are correct,
1593 * in that we charge for the padding used to fill out
1596 ASSERT3U(spa
->spa_min_ashift
, >=, SPA_MINBLOCKSHIFT
);
1597 size_t rounded
= (size_t)P2ROUNDUP(psize
,
1598 1ULL << spa
->spa_min_ashift
);
1599 if (rounded
>= lsize
) {
1600 compress
= ZIO_COMPRESS_OFF
;
1601 zio_buf_free(cbuf
, lsize
);
1604 abd_t
*cdata
= abd_get_from_buf(cbuf
, lsize
);
1605 abd_take_ownership_of_buf(cdata
, B_TRUE
);
1606 abd_zero_off(cdata
, psize
, rounded
- psize
);
1608 zio_push_transform(zio
, cdata
,
1609 psize
, lsize
, NULL
);
1614 * We were unable to handle this as an override bp, treat
1615 * it as a regular write I/O.
1617 zio
->io_bp_override
= NULL
;
1618 *bp
= zio
->io_bp_orig
;
1619 zio
->io_pipeline
= zio
->io_orig_pipeline
;
1621 } else if ((zio
->io_flags
& ZIO_FLAG_RAW_ENCRYPT
) != 0 &&
1622 zp
->zp_type
== DMU_OT_DNODE
) {
1624 * The DMU actually relies on the zio layer's compression
1625 * to free metadnode blocks that have had all contained
1626 * dnodes freed. As a result, even when doing a raw
1627 * receive, we must check whether the block can be compressed
1630 psize
= zio_compress_data(ZIO_COMPRESS_EMPTY
,
1631 zio
->io_abd
, NULL
, lsize
);
1633 compress
= ZIO_COMPRESS_OFF
;
1635 ASSERT3U(psize
, !=, 0);
1639 * The final pass of spa_sync() must be all rewrites, but the first
1640 * few passes offer a trade-off: allocating blocks defers convergence,
1641 * but newly allocated blocks are sequential, so they can be written
1642 * to disk faster. Therefore, we allow the first few passes of
1643 * spa_sync() to allocate new blocks, but force rewrites after that.
1644 * There should only be a handful of blocks after pass 1 in any case.
1646 if (!BP_IS_HOLE(bp
) && bp
->blk_birth
== zio
->io_txg
&&
1647 BP_GET_PSIZE(bp
) == psize
&&
1648 pass
>= zfs_sync_pass_rewrite
) {
1649 VERIFY3U(psize
, !=, 0);
1650 enum zio_stage gang_stages
= zio
->io_pipeline
& ZIO_GANG_STAGES
;
1652 zio
->io_pipeline
= ZIO_REWRITE_PIPELINE
| gang_stages
;
1653 zio
->io_flags
|= ZIO_FLAG_IO_REWRITE
;
1656 zio
->io_pipeline
= ZIO_WRITE_PIPELINE
;
1660 if (zio
->io_bp_orig
.blk_birth
!= 0 &&
1661 spa_feature_is_active(spa
, SPA_FEATURE_HOLE_BIRTH
)) {
1662 BP_SET_LSIZE(bp
, lsize
);
1663 BP_SET_TYPE(bp
, zp
->zp_type
);
1664 BP_SET_LEVEL(bp
, zp
->zp_level
);
1665 BP_SET_BIRTH(bp
, zio
->io_txg
, 0);
1667 zio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
1669 ASSERT(zp
->zp_checksum
!= ZIO_CHECKSUM_GANG_HEADER
);
1670 BP_SET_LSIZE(bp
, lsize
);
1671 BP_SET_TYPE(bp
, zp
->zp_type
);
1672 BP_SET_LEVEL(bp
, zp
->zp_level
);
1673 BP_SET_PSIZE(bp
, psize
);
1674 BP_SET_COMPRESS(bp
, compress
);
1675 BP_SET_CHECKSUM(bp
, zp
->zp_checksum
);
1676 BP_SET_DEDUP(bp
, zp
->zp_dedup
);
1677 BP_SET_BYTEORDER(bp
, ZFS_HOST_BYTEORDER
);
1679 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
1680 ASSERT(!(zio
->io_flags
& ZIO_FLAG_IO_REWRITE
));
1681 ASSERT(!zp
->zp_encrypt
||
1682 DMU_OT_IS_ENCRYPTED(zp
->zp_type
));
1683 zio
->io_pipeline
= ZIO_DDT_WRITE_PIPELINE
;
1685 if (zp
->zp_nopwrite
) {
1686 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
1687 ASSERT(!(zio
->io_flags
& ZIO_FLAG_IO_REWRITE
));
1688 zio
->io_pipeline
|= ZIO_STAGE_NOP_WRITE
;
1695 zio_free_bp_init(zio_t
*zio
)
1697 blkptr_t
*bp
= zio
->io_bp
;
1699 if (zio
->io_child_type
== ZIO_CHILD_LOGICAL
) {
1700 if (BP_GET_DEDUP(bp
))
1701 zio
->io_pipeline
= ZIO_DDT_FREE_PIPELINE
;
1704 ASSERT3P(zio
->io_bp
, ==, &zio
->io_bp_copy
);
1710 * ==========================================================================
1711 * Execute the I/O pipeline
1712 * ==========================================================================
1716 zio_taskq_dispatch(zio_t
*zio
, zio_taskq_type_t q
, boolean_t cutinline
)
1718 spa_t
*spa
= zio
->io_spa
;
1719 zio_type_t t
= zio
->io_type
;
1720 int flags
= (cutinline
? TQ_FRONT
: 0);
1723 * If we're a config writer or a probe, the normal issue and
1724 * interrupt threads may all be blocked waiting for the config lock.
1725 * In this case, select the otherwise-unused taskq for ZIO_TYPE_NULL.
1727 if (zio
->io_flags
& (ZIO_FLAG_CONFIG_WRITER
| ZIO_FLAG_PROBE
))
1731 * A similar issue exists for the L2ARC write thread until L2ARC 2.0.
1733 if (t
== ZIO_TYPE_WRITE
&& zio
->io_vd
&& zio
->io_vd
->vdev_aux
)
1737 * If this is a high priority I/O, then use the high priority taskq if
1740 if (zio
->io_priority
== ZIO_PRIORITY_NOW
&&
1741 spa
->spa_zio_taskq
[t
][q
+ 1].stqs_count
!= 0)
1744 ASSERT3U(q
, <, ZIO_TASKQ_TYPES
);
1747 * NB: We are assuming that the zio can only be dispatched
1748 * to a single taskq at a time. It would be a grievous error
1749 * to dispatch the zio to another taskq at the same time.
1751 ASSERT(taskq_empty_ent(&zio
->io_tqent
));
1752 spa_taskq_dispatch_ent(spa
, t
, q
, (task_func_t
*)zio_execute
, zio
,
1753 flags
, &zio
->io_tqent
);
1757 zio_taskq_member(zio_t
*zio
, zio_taskq_type_t q
)
1759 kthread_t
*executor
= zio
->io_executor
;
1760 spa_t
*spa
= zio
->io_spa
;
1762 for (zio_type_t t
= 0; t
< ZIO_TYPES
; t
++) {
1763 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1765 for (i
= 0; i
< tqs
->stqs_count
; i
++) {
1766 if (taskq_member(tqs
->stqs_taskq
[i
], executor
))
1775 zio_issue_async(zio_t
*zio
)
1777 zio_taskq_dispatch(zio
, ZIO_TASKQ_ISSUE
, B_FALSE
);
1783 zio_interrupt(zio_t
*zio
)
1785 zio_taskq_dispatch(zio
, ZIO_TASKQ_INTERRUPT
, B_FALSE
);
1789 zio_delay_interrupt(zio_t
*zio
)
1792 * The timeout_generic() function isn't defined in userspace, so
1793 * rather than trying to implement the function, the zio delay
1794 * functionality has been disabled for userspace builds.
1799 * If io_target_timestamp is zero, then no delay has been registered
1800 * for this IO, thus jump to the end of this function and "skip" the
1801 * delay; issuing it directly to the zio layer.
1803 if (zio
->io_target_timestamp
!= 0) {
1804 hrtime_t now
= gethrtime();
1806 if (now
>= zio
->io_target_timestamp
) {
1808 * This IO has already taken longer than the target
1809 * delay to complete, so we don't want to delay it
1810 * any longer; we "miss" the delay and issue it
1811 * directly to the zio layer. This is likely due to
1812 * the target latency being set to a value less than
1813 * the underlying hardware can satisfy (e.g. delay
1814 * set to 1ms, but the disks take 10ms to complete an
1818 DTRACE_PROBE2(zio__delay__miss
, zio_t
*, zio
,
1824 hrtime_t diff
= zio
->io_target_timestamp
- now
;
1825 clock_t expire_at_tick
= ddi_get_lbolt() +
1828 DTRACE_PROBE3(zio__delay__hit
, zio_t
*, zio
,
1829 hrtime_t
, now
, hrtime_t
, diff
);
1831 if (NSEC_TO_TICK(diff
) == 0) {
1832 /* Our delay is less than a jiffy - just spin */
1833 zfs_sleep_until(zio
->io_target_timestamp
);
1836 * Use taskq_dispatch_delay() in the place of
1837 * OpenZFS's timeout_generic().
1839 tid
= taskq_dispatch_delay(system_taskq
,
1840 (task_func_t
*)zio_interrupt
,
1841 zio
, TQ_NOSLEEP
, expire_at_tick
);
1842 if (tid
== TASKQID_INVALID
) {
1844 * Couldn't allocate a task. Just
1845 * finish the zio without a delay.
1854 DTRACE_PROBE1(zio__delay__skip
, zio_t
*, zio
);
1859 zio_deadman_impl(zio_t
*pio
)
1861 zio_t
*cio
, *cio_next
;
1862 zio_link_t
*zl
= NULL
;
1863 vdev_t
*vd
= pio
->io_vd
;
1865 if (vd
!= NULL
&& vd
->vdev_ops
->vdev_op_leaf
) {
1866 vdev_queue_t
*vq
= &vd
->vdev_queue
;
1867 zbookmark_phys_t
*zb
= &pio
->io_bookmark
;
1868 uint64_t delta
= gethrtime() - pio
->io_timestamp
;
1869 uint64_t failmode
= spa_get_deadman_failmode(pio
->io_spa
);
1871 zfs_dbgmsg("slow zio: zio=%p timestamp=%llu "
1872 "delta=%llu queued=%llu io=%llu "
1873 "path=%s last=%llu "
1874 "type=%d priority=%d flags=0x%x "
1875 "stage=0x%x pipeline=0x%x pipeline-trace=0x%x "
1876 "objset=%llu object=%llu level=%llu blkid=%llu "
1877 "offset=%llu size=%llu error=%d",
1878 pio
, pio
->io_timestamp
,
1879 delta
, pio
->io_delta
, pio
->io_delay
,
1880 vd
->vdev_path
, vq
->vq_io_complete_ts
,
1881 pio
->io_type
, pio
->io_priority
, pio
->io_flags
,
1882 pio
->io_state
, pio
->io_pipeline
, pio
->io_pipeline_trace
,
1883 zb
->zb_objset
, zb
->zb_object
, zb
->zb_level
, zb
->zb_blkid
,
1884 pio
->io_offset
, pio
->io_size
, pio
->io_error
);
1885 zfs_ereport_post(FM_EREPORT_ZFS_DEADMAN
,
1886 pio
->io_spa
, vd
, zb
, pio
, 0, 0);
1888 if (failmode
== ZIO_FAILURE_MODE_CONTINUE
&&
1889 taskq_empty_ent(&pio
->io_tqent
)) {
1894 mutex_enter(&pio
->io_lock
);
1895 for (cio
= zio_walk_children(pio
, &zl
); cio
!= NULL
; cio
= cio_next
) {
1896 cio_next
= zio_walk_children(pio
, &zl
);
1897 zio_deadman_impl(cio
);
1899 mutex_exit(&pio
->io_lock
);
1903 * Log the critical information describing this zio and all of its children
1904 * using the zfs_dbgmsg() interface then post deadman event for the ZED.
1907 zio_deadman(zio_t
*pio
, char *tag
)
1909 spa_t
*spa
= pio
->io_spa
;
1910 char *name
= spa_name(spa
);
1912 if (!zfs_deadman_enabled
|| spa_suspended(spa
))
1915 zio_deadman_impl(pio
);
1917 switch (spa_get_deadman_failmode(spa
)) {
1918 case ZIO_FAILURE_MODE_WAIT
:
1919 zfs_dbgmsg("%s waiting for hung I/O to pool '%s'", tag
, name
);
1922 case ZIO_FAILURE_MODE_CONTINUE
:
1923 zfs_dbgmsg("%s restarting hung I/O for pool '%s'", tag
, name
);
1926 case ZIO_FAILURE_MODE_PANIC
:
1927 fm_panic("%s determined I/O to pool '%s' is hung.", tag
, name
);
1933 * Execute the I/O pipeline until one of the following occurs:
1934 * (1) the I/O completes; (2) the pipeline stalls waiting for
1935 * dependent child I/Os; (3) the I/O issues, so we're waiting
1936 * for an I/O completion interrupt; (4) the I/O is delegated by
1937 * vdev-level caching or aggregation; (5) the I/O is deferred
1938 * due to vdev-level queueing; (6) the I/O is handed off to
1939 * another thread. In all cases, the pipeline stops whenever
1940 * there's no CPU work; it never burns a thread in cv_wait_io().
1942 * There's no locking on io_stage because there's no legitimate way
1943 * for multiple threads to be attempting to process the same I/O.
1945 static zio_pipe_stage_t
*zio_pipeline
[];
1948 * zio_execute() is a wrapper around the static function
1949 * __zio_execute() so that we can force __zio_execute() to be
1950 * inlined. This reduces stack overhead which is important
1951 * because __zio_execute() is called recursively in several zio
1952 * code paths. zio_execute() itself cannot be inlined because
1953 * it is externally visible.
1956 zio_execute(zio_t
*zio
)
1958 fstrans_cookie_t cookie
;
1960 cookie
= spl_fstrans_mark();
1962 spl_fstrans_unmark(cookie
);
1966 * Used to determine if in the current context the stack is sized large
1967 * enough to allow zio_execute() to be called recursively. A minimum
1968 * stack size of 16K is required to avoid needing to re-dispatch the zio.
1971 zio_execute_stack_check(zio_t
*zio
)
1973 #if !defined(HAVE_LARGE_STACKS)
1974 dsl_pool_t
*dp
= spa_get_dsl(zio
->io_spa
);
1976 /* Executing in txg_sync_thread() context. */
1977 if (dp
&& curthread
== dp
->dp_tx
.tx_sync_thread
)
1980 /* Pool initialization outside of zio_taskq context. */
1981 if (dp
&& spa_is_initializing(dp
->dp_spa
) &&
1982 !zio_taskq_member(zio
, ZIO_TASKQ_ISSUE
) &&
1983 !zio_taskq_member(zio
, ZIO_TASKQ_ISSUE_HIGH
))
1985 #endif /* HAVE_LARGE_STACKS */
1990 __attribute__((always_inline
))
1992 __zio_execute(zio_t
*zio
)
1994 ASSERT3U(zio
->io_queued_timestamp
, >, 0);
1996 while (zio
->io_stage
< ZIO_STAGE_DONE
) {
1997 enum zio_stage pipeline
= zio
->io_pipeline
;
1998 enum zio_stage stage
= zio
->io_stage
;
2000 zio
->io_executor
= curthread
;
2002 ASSERT(!MUTEX_HELD(&zio
->io_lock
));
2003 ASSERT(ISP2(stage
));
2004 ASSERT(zio
->io_stall
== NULL
);
2008 } while ((stage
& pipeline
) == 0);
2010 ASSERT(stage
<= ZIO_STAGE_DONE
);
2013 * If we are in interrupt context and this pipeline stage
2014 * will grab a config lock that is held across I/O,
2015 * or may wait for an I/O that needs an interrupt thread
2016 * to complete, issue async to avoid deadlock.
2018 * For VDEV_IO_START, we cut in line so that the io will
2019 * be sent to disk promptly.
2021 if ((stage
& ZIO_BLOCKING_STAGES
) && zio
->io_vd
== NULL
&&
2022 zio_taskq_member(zio
, ZIO_TASKQ_INTERRUPT
)) {
2023 boolean_t cut
= (stage
== ZIO_STAGE_VDEV_IO_START
) ?
2024 zio_requeue_io_start_cut_in_line
: B_FALSE
;
2025 zio_taskq_dispatch(zio
, ZIO_TASKQ_ISSUE
, cut
);
2030 * If the current context doesn't have large enough stacks
2031 * the zio must be issued asynchronously to prevent overflow.
2033 if (zio_execute_stack_check(zio
)) {
2034 boolean_t cut
= (stage
== ZIO_STAGE_VDEV_IO_START
) ?
2035 zio_requeue_io_start_cut_in_line
: B_FALSE
;
2036 zio_taskq_dispatch(zio
, ZIO_TASKQ_ISSUE
, cut
);
2040 zio
->io_stage
= stage
;
2041 zio
->io_pipeline_trace
|= zio
->io_stage
;
2044 * The zio pipeline stage returns the next zio to execute
2045 * (typically the same as this one), or NULL if we should
2048 zio
= zio_pipeline
[highbit64(stage
) - 1](zio
);
2057 * ==========================================================================
2058 * Initiate I/O, either sync or async
2059 * ==========================================================================
2062 zio_wait(zio_t
*zio
)
2064 long timeout
= MSEC_TO_TICK(zfs_deadman_ziotime_ms
);
2067 ASSERT3S(zio
->io_stage
, ==, ZIO_STAGE_OPEN
);
2068 ASSERT3P(zio
->io_executor
, ==, NULL
);
2070 zio
->io_waiter
= curthread
;
2071 ASSERT0(zio
->io_queued_timestamp
);
2072 zio
->io_queued_timestamp
= gethrtime();
2076 mutex_enter(&zio
->io_lock
);
2077 while (zio
->io_executor
!= NULL
) {
2078 error
= cv_timedwait_io(&zio
->io_cv
, &zio
->io_lock
,
2079 ddi_get_lbolt() + timeout
);
2081 if (zfs_deadman_enabled
&& error
== -1 &&
2082 gethrtime() - zio
->io_queued_timestamp
>
2083 spa_deadman_ziotime(zio
->io_spa
)) {
2084 mutex_exit(&zio
->io_lock
);
2085 timeout
= MSEC_TO_TICK(zfs_deadman_checktime_ms
);
2086 zio_deadman(zio
, FTAG
);
2087 mutex_enter(&zio
->io_lock
);
2090 mutex_exit(&zio
->io_lock
);
2092 error
= zio
->io_error
;
2099 zio_nowait(zio_t
*zio
)
2101 ASSERT3P(zio
->io_executor
, ==, NULL
);
2103 if (zio
->io_child_type
== ZIO_CHILD_LOGICAL
&&
2104 zio_unique_parent(zio
) == NULL
) {
2108 * This is a logical async I/O with no parent to wait for it.
2109 * We add it to the spa_async_root_zio "Godfather" I/O which
2110 * will ensure they complete prior to unloading the pool.
2112 spa_t
*spa
= zio
->io_spa
;
2114 pio
= spa
->spa_async_zio_root
[CPU_SEQID
];
2117 zio_add_child(pio
, zio
);
2120 ASSERT0(zio
->io_queued_timestamp
);
2121 zio
->io_queued_timestamp
= gethrtime();
2126 * ==========================================================================
2127 * Reexecute, cancel, or suspend/resume failed I/O
2128 * ==========================================================================
2132 zio_reexecute(zio_t
*pio
)
2134 zio_t
*cio
, *cio_next
;
2136 ASSERT(pio
->io_child_type
== ZIO_CHILD_LOGICAL
);
2137 ASSERT(pio
->io_orig_stage
== ZIO_STAGE_OPEN
);
2138 ASSERT(pio
->io_gang_leader
== NULL
);
2139 ASSERT(pio
->io_gang_tree
== NULL
);
2141 pio
->io_flags
= pio
->io_orig_flags
;
2142 pio
->io_stage
= pio
->io_orig_stage
;
2143 pio
->io_pipeline
= pio
->io_orig_pipeline
;
2144 pio
->io_reexecute
= 0;
2145 pio
->io_flags
|= ZIO_FLAG_REEXECUTED
;
2146 pio
->io_pipeline_trace
= 0;
2148 for (int w
= 0; w
< ZIO_WAIT_TYPES
; w
++)
2149 pio
->io_state
[w
] = 0;
2150 for (int c
= 0; c
< ZIO_CHILD_TYPES
; c
++)
2151 pio
->io_child_error
[c
] = 0;
2153 if (IO_IS_ALLOCATING(pio
))
2154 BP_ZERO(pio
->io_bp
);
2157 * As we reexecute pio's children, new children could be created.
2158 * New children go to the head of pio's io_child_list, however,
2159 * so we will (correctly) not reexecute them. The key is that
2160 * the remainder of pio's io_child_list, from 'cio_next' onward,
2161 * cannot be affected by any side effects of reexecuting 'cio'.
2163 zio_link_t
*zl
= NULL
;
2164 mutex_enter(&pio
->io_lock
);
2165 for (cio
= zio_walk_children(pio
, &zl
); cio
!= NULL
; cio
= cio_next
) {
2166 cio_next
= zio_walk_children(pio
, &zl
);
2167 for (int w
= 0; w
< ZIO_WAIT_TYPES
; w
++)
2168 pio
->io_children
[cio
->io_child_type
][w
]++;
2169 mutex_exit(&pio
->io_lock
);
2171 mutex_enter(&pio
->io_lock
);
2173 mutex_exit(&pio
->io_lock
);
2176 * Now that all children have been reexecuted, execute the parent.
2177 * We don't reexecute "The Godfather" I/O here as it's the
2178 * responsibility of the caller to wait on it.
2180 if (!(pio
->io_flags
& ZIO_FLAG_GODFATHER
)) {
2181 pio
->io_queued_timestamp
= gethrtime();
2187 zio_suspend(spa_t
*spa
, zio_t
*zio
, zio_suspend_reason_t reason
)
2189 if (spa_get_failmode(spa
) == ZIO_FAILURE_MODE_PANIC
)
2190 fm_panic("Pool '%s' has encountered an uncorrectable I/O "
2191 "failure and the failure mode property for this pool "
2192 "is set to panic.", spa_name(spa
));
2194 cmn_err(CE_WARN
, "Pool '%s' has encountered an uncorrectable I/O "
2195 "failure and has been suspended.\n", spa_name(spa
));
2197 zfs_ereport_post(FM_EREPORT_ZFS_IO_FAILURE
, spa
, NULL
,
2200 mutex_enter(&spa
->spa_suspend_lock
);
2202 if (spa
->spa_suspend_zio_root
== NULL
)
2203 spa
->spa_suspend_zio_root
= zio_root(spa
, NULL
, NULL
,
2204 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
2205 ZIO_FLAG_GODFATHER
);
2207 spa
->spa_suspended
= reason
;
2210 ASSERT(!(zio
->io_flags
& ZIO_FLAG_GODFATHER
));
2211 ASSERT(zio
!= spa
->spa_suspend_zio_root
);
2212 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
2213 ASSERT(zio_unique_parent(zio
) == NULL
);
2214 ASSERT(zio
->io_stage
== ZIO_STAGE_DONE
);
2215 zio_add_child(spa
->spa_suspend_zio_root
, zio
);
2218 mutex_exit(&spa
->spa_suspend_lock
);
2222 zio_resume(spa_t
*spa
)
2227 * Reexecute all previously suspended i/o.
2229 mutex_enter(&spa
->spa_suspend_lock
);
2230 spa
->spa_suspended
= ZIO_SUSPEND_NONE
;
2231 cv_broadcast(&spa
->spa_suspend_cv
);
2232 pio
= spa
->spa_suspend_zio_root
;
2233 spa
->spa_suspend_zio_root
= NULL
;
2234 mutex_exit(&spa
->spa_suspend_lock
);
2240 return (zio_wait(pio
));
2244 zio_resume_wait(spa_t
*spa
)
2246 mutex_enter(&spa
->spa_suspend_lock
);
2247 while (spa_suspended(spa
))
2248 cv_wait(&spa
->spa_suspend_cv
, &spa
->spa_suspend_lock
);
2249 mutex_exit(&spa
->spa_suspend_lock
);
2253 * ==========================================================================
2256 * A gang block is a collection of small blocks that looks to the DMU
2257 * like one large block. When zio_dva_allocate() cannot find a block
2258 * of the requested size, due to either severe fragmentation or the pool
2259 * being nearly full, it calls zio_write_gang_block() to construct the
2260 * block from smaller fragments.
2262 * A gang block consists of a gang header (zio_gbh_phys_t) and up to
2263 * three (SPA_GBH_NBLKPTRS) gang members. The gang header is just like
2264 * an indirect block: it's an array of block pointers. It consumes
2265 * only one sector and hence is allocatable regardless of fragmentation.
2266 * The gang header's bps point to its gang members, which hold the data.
2268 * Gang blocks are self-checksumming, using the bp's <vdev, offset, txg>
2269 * as the verifier to ensure uniqueness of the SHA256 checksum.
2270 * Critically, the gang block bp's blk_cksum is the checksum of the data,
2271 * not the gang header. This ensures that data block signatures (needed for
2272 * deduplication) are independent of how the block is physically stored.
2274 * Gang blocks can be nested: a gang member may itself be a gang block.
2275 * Thus every gang block is a tree in which root and all interior nodes are
2276 * gang headers, and the leaves are normal blocks that contain user data.
2277 * The root of the gang tree is called the gang leader.
2279 * To perform any operation (read, rewrite, free, claim) on a gang block,
2280 * zio_gang_assemble() first assembles the gang tree (minus data leaves)
2281 * in the io_gang_tree field of the original logical i/o by recursively
2282 * reading the gang leader and all gang headers below it. This yields
2283 * an in-core tree containing the contents of every gang header and the
2284 * bps for every constituent of the gang block.
2286 * With the gang tree now assembled, zio_gang_issue() just walks the gang tree
2287 * and invokes a callback on each bp. To free a gang block, zio_gang_issue()
2288 * calls zio_free_gang() -- a trivial wrapper around zio_free() -- for each bp.
2289 * zio_claim_gang() provides a similarly trivial wrapper for zio_claim().
2290 * zio_read_gang() is a wrapper around zio_read() that omits reading gang
2291 * headers, since we already have those in io_gang_tree. zio_rewrite_gang()
2292 * performs a zio_rewrite() of the data or, for gang headers, a zio_rewrite()
2293 * of the gang header plus zio_checksum_compute() of the data to update the
2294 * gang header's blk_cksum as described above.
2296 * The two-phase assemble/issue model solves the problem of partial failure --
2297 * what if you'd freed part of a gang block but then couldn't read the
2298 * gang header for another part? Assembling the entire gang tree first
2299 * ensures that all the necessary gang header I/O has succeeded before
2300 * starting the actual work of free, claim, or write. Once the gang tree
2301 * is assembled, free and claim are in-memory operations that cannot fail.
2303 * In the event that a gang write fails, zio_dva_unallocate() walks the
2304 * gang tree to immediately free (i.e. insert back into the space map)
2305 * everything we've allocated. This ensures that we don't get ENOSPC
2306 * errors during repeated suspend/resume cycles due to a flaky device.
2308 * Gang rewrites only happen during sync-to-convergence. If we can't assemble
2309 * the gang tree, we won't modify the block, so we can safely defer the free
2310 * (knowing that the block is still intact). If we *can* assemble the gang
2311 * tree, then even if some of the rewrites fail, zio_dva_unallocate() will free
2312 * each constituent bp and we can allocate a new block on the next sync pass.
2314 * In all cases, the gang tree allows complete recovery from partial failure.
2315 * ==========================================================================
2319 zio_gang_issue_func_done(zio_t
*zio
)
2321 abd_put(zio
->io_abd
);
2325 zio_read_gang(zio_t
*pio
, blkptr_t
*bp
, zio_gang_node_t
*gn
, abd_t
*data
,
2331 return (zio_read(pio
, pio
->io_spa
, bp
, abd_get_offset(data
, offset
),
2332 BP_GET_PSIZE(bp
), zio_gang_issue_func_done
,
2333 NULL
, pio
->io_priority
, ZIO_GANG_CHILD_FLAGS(pio
),
2334 &pio
->io_bookmark
));
2338 zio_rewrite_gang(zio_t
*pio
, blkptr_t
*bp
, zio_gang_node_t
*gn
, abd_t
*data
,
2345 abd_get_from_buf(gn
->gn_gbh
, SPA_GANGBLOCKSIZE
);
2346 zio
= zio_rewrite(pio
, pio
->io_spa
, pio
->io_txg
, bp
,
2347 gbh_abd
, SPA_GANGBLOCKSIZE
, zio_gang_issue_func_done
, NULL
,
2348 pio
->io_priority
, ZIO_GANG_CHILD_FLAGS(pio
),
2351 * As we rewrite each gang header, the pipeline will compute
2352 * a new gang block header checksum for it; but no one will
2353 * compute a new data checksum, so we do that here. The one
2354 * exception is the gang leader: the pipeline already computed
2355 * its data checksum because that stage precedes gang assembly.
2356 * (Presently, nothing actually uses interior data checksums;
2357 * this is just good hygiene.)
2359 if (gn
!= pio
->io_gang_leader
->io_gang_tree
) {
2360 abd_t
*buf
= abd_get_offset(data
, offset
);
2362 zio_checksum_compute(zio
, BP_GET_CHECKSUM(bp
),
2363 buf
, BP_GET_PSIZE(bp
));
2368 * If we are here to damage data for testing purposes,
2369 * leave the GBH alone so that we can detect the damage.
2371 if (pio
->io_gang_leader
->io_flags
& ZIO_FLAG_INDUCE_DAMAGE
)
2372 zio
->io_pipeline
&= ~ZIO_VDEV_IO_STAGES
;
2374 zio
= zio_rewrite(pio
, pio
->io_spa
, pio
->io_txg
, bp
,
2375 abd_get_offset(data
, offset
), BP_GET_PSIZE(bp
),
2376 zio_gang_issue_func_done
, NULL
, pio
->io_priority
,
2377 ZIO_GANG_CHILD_FLAGS(pio
), &pio
->io_bookmark
);
2385 zio_free_gang(zio_t
*pio
, blkptr_t
*bp
, zio_gang_node_t
*gn
, abd_t
*data
,
2388 return (zio_free_sync(pio
, pio
->io_spa
, pio
->io_txg
, bp
,
2389 ZIO_GANG_CHILD_FLAGS(pio
)));
2394 zio_claim_gang(zio_t
*pio
, blkptr_t
*bp
, zio_gang_node_t
*gn
, abd_t
*data
,
2397 return (zio_claim(pio
, pio
->io_spa
, pio
->io_txg
, bp
,
2398 NULL
, NULL
, ZIO_GANG_CHILD_FLAGS(pio
)));
2401 static zio_gang_issue_func_t
*zio_gang_issue_func
[ZIO_TYPES
] = {
2410 static void zio_gang_tree_assemble_done(zio_t
*zio
);
2412 static zio_gang_node_t
*
2413 zio_gang_node_alloc(zio_gang_node_t
**gnpp
)
2415 zio_gang_node_t
*gn
;
2417 ASSERT(*gnpp
== NULL
);
2419 gn
= kmem_zalloc(sizeof (*gn
), KM_SLEEP
);
2420 gn
->gn_gbh
= zio_buf_alloc(SPA_GANGBLOCKSIZE
);
2427 zio_gang_node_free(zio_gang_node_t
**gnpp
)
2429 zio_gang_node_t
*gn
= *gnpp
;
2431 for (int g
= 0; g
< SPA_GBH_NBLKPTRS
; g
++)
2432 ASSERT(gn
->gn_child
[g
] == NULL
);
2434 zio_buf_free(gn
->gn_gbh
, SPA_GANGBLOCKSIZE
);
2435 kmem_free(gn
, sizeof (*gn
));
2440 zio_gang_tree_free(zio_gang_node_t
**gnpp
)
2442 zio_gang_node_t
*gn
= *gnpp
;
2447 for (int g
= 0; g
< SPA_GBH_NBLKPTRS
; g
++)
2448 zio_gang_tree_free(&gn
->gn_child
[g
]);
2450 zio_gang_node_free(gnpp
);
2454 zio_gang_tree_assemble(zio_t
*gio
, blkptr_t
*bp
, zio_gang_node_t
**gnpp
)
2456 zio_gang_node_t
*gn
= zio_gang_node_alloc(gnpp
);
2457 abd_t
*gbh_abd
= abd_get_from_buf(gn
->gn_gbh
, SPA_GANGBLOCKSIZE
);
2459 ASSERT(gio
->io_gang_leader
== gio
);
2460 ASSERT(BP_IS_GANG(bp
));
2462 zio_nowait(zio_read(gio
, gio
->io_spa
, bp
, gbh_abd
, SPA_GANGBLOCKSIZE
,
2463 zio_gang_tree_assemble_done
, gn
, gio
->io_priority
,
2464 ZIO_GANG_CHILD_FLAGS(gio
), &gio
->io_bookmark
));
2468 zio_gang_tree_assemble_done(zio_t
*zio
)
2470 zio_t
*gio
= zio
->io_gang_leader
;
2471 zio_gang_node_t
*gn
= zio
->io_private
;
2472 blkptr_t
*bp
= zio
->io_bp
;
2474 ASSERT(gio
== zio_unique_parent(zio
));
2475 ASSERT(zio
->io_child_count
== 0);
2480 /* this ABD was created from a linear buf in zio_gang_tree_assemble */
2481 if (BP_SHOULD_BYTESWAP(bp
))
2482 byteswap_uint64_array(abd_to_buf(zio
->io_abd
), zio
->io_size
);
2484 ASSERT3P(abd_to_buf(zio
->io_abd
), ==, gn
->gn_gbh
);
2485 ASSERT(zio
->io_size
== SPA_GANGBLOCKSIZE
);
2486 ASSERT(gn
->gn_gbh
->zg_tail
.zec_magic
== ZEC_MAGIC
);
2488 abd_put(zio
->io_abd
);
2490 for (int g
= 0; g
< SPA_GBH_NBLKPTRS
; g
++) {
2491 blkptr_t
*gbp
= &gn
->gn_gbh
->zg_blkptr
[g
];
2492 if (!BP_IS_GANG(gbp
))
2494 zio_gang_tree_assemble(gio
, gbp
, &gn
->gn_child
[g
]);
2499 zio_gang_tree_issue(zio_t
*pio
, zio_gang_node_t
*gn
, blkptr_t
*bp
, abd_t
*data
,
2502 zio_t
*gio
= pio
->io_gang_leader
;
2505 ASSERT(BP_IS_GANG(bp
) == !!gn
);
2506 ASSERT(BP_GET_CHECKSUM(bp
) == BP_GET_CHECKSUM(gio
->io_bp
));
2507 ASSERT(BP_GET_LSIZE(bp
) == BP_GET_PSIZE(bp
) || gn
== gio
->io_gang_tree
);
2510 * If you're a gang header, your data is in gn->gn_gbh.
2511 * If you're a gang member, your data is in 'data' and gn == NULL.
2513 zio
= zio_gang_issue_func
[gio
->io_type
](pio
, bp
, gn
, data
, offset
);
2516 ASSERT(gn
->gn_gbh
->zg_tail
.zec_magic
== ZEC_MAGIC
);
2518 for (int g
= 0; g
< SPA_GBH_NBLKPTRS
; g
++) {
2519 blkptr_t
*gbp
= &gn
->gn_gbh
->zg_blkptr
[g
];
2520 if (BP_IS_HOLE(gbp
))
2522 zio_gang_tree_issue(zio
, gn
->gn_child
[g
], gbp
, data
,
2524 offset
+= BP_GET_PSIZE(gbp
);
2528 if (gn
== gio
->io_gang_tree
)
2529 ASSERT3U(gio
->io_size
, ==, offset
);
2536 zio_gang_assemble(zio_t
*zio
)
2538 blkptr_t
*bp
= zio
->io_bp
;
2540 ASSERT(BP_IS_GANG(bp
) && zio
->io_gang_leader
== NULL
);
2541 ASSERT(zio
->io_child_type
> ZIO_CHILD_GANG
);
2543 zio
->io_gang_leader
= zio
;
2545 zio_gang_tree_assemble(zio
, bp
, &zio
->io_gang_tree
);
2551 zio_gang_issue(zio_t
*zio
)
2553 blkptr_t
*bp
= zio
->io_bp
;
2555 if (zio_wait_for_children(zio
, ZIO_CHILD_GANG_BIT
, ZIO_WAIT_DONE
)) {
2559 ASSERT(BP_IS_GANG(bp
) && zio
->io_gang_leader
== zio
);
2560 ASSERT(zio
->io_child_type
> ZIO_CHILD_GANG
);
2562 if (zio
->io_child_error
[ZIO_CHILD_GANG
] == 0)
2563 zio_gang_tree_issue(zio
, zio
->io_gang_tree
, bp
, zio
->io_abd
,
2566 zio_gang_tree_free(&zio
->io_gang_tree
);
2568 zio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
2574 zio_write_gang_member_ready(zio_t
*zio
)
2576 zio_t
*pio
= zio_unique_parent(zio
);
2577 dva_t
*cdva
= zio
->io_bp
->blk_dva
;
2578 dva_t
*pdva
= pio
->io_bp
->blk_dva
;
2580 ASSERTV(zio_t
*gio
= zio
->io_gang_leader
);
2582 if (BP_IS_HOLE(zio
->io_bp
))
2585 ASSERT(BP_IS_HOLE(&zio
->io_bp_orig
));
2587 ASSERT(zio
->io_child_type
== ZIO_CHILD_GANG
);
2588 ASSERT3U(zio
->io_prop
.zp_copies
, ==, gio
->io_prop
.zp_copies
);
2589 ASSERT3U(zio
->io_prop
.zp_copies
, <=, BP_GET_NDVAS(zio
->io_bp
));
2590 ASSERT3U(pio
->io_prop
.zp_copies
, <=, BP_GET_NDVAS(pio
->io_bp
));
2591 ASSERT3U(BP_GET_NDVAS(zio
->io_bp
), <=, BP_GET_NDVAS(pio
->io_bp
));
2593 mutex_enter(&pio
->io_lock
);
2594 for (int d
= 0; d
< BP_GET_NDVAS(zio
->io_bp
); d
++) {
2595 ASSERT(DVA_GET_GANG(&pdva
[d
]));
2596 asize
= DVA_GET_ASIZE(&pdva
[d
]);
2597 asize
+= DVA_GET_ASIZE(&cdva
[d
]);
2598 DVA_SET_ASIZE(&pdva
[d
], asize
);
2600 mutex_exit(&pio
->io_lock
);
2604 zio_write_gang_done(zio_t
*zio
)
2606 abd_put(zio
->io_abd
);
2610 zio_write_gang_block(zio_t
*pio
)
2612 spa_t
*spa
= pio
->io_spa
;
2613 metaslab_class_t
*mc
= spa_normal_class(spa
);
2614 blkptr_t
*bp
= pio
->io_bp
;
2615 zio_t
*gio
= pio
->io_gang_leader
;
2617 zio_gang_node_t
*gn
, **gnpp
;
2618 zio_gbh_phys_t
*gbh
;
2620 uint64_t txg
= pio
->io_txg
;
2621 uint64_t resid
= pio
->io_size
;
2623 int copies
= gio
->io_prop
.zp_copies
;
2629 * encrypted blocks need DVA[2] free so encrypted gang headers can't
2630 * have a third copy.
2632 gbh_copies
= MIN(copies
+ 1, spa_max_replication(spa
));
2633 if (gio
->io_prop
.zp_encrypt
&& gbh_copies
>= SPA_DVAS_PER_BP
)
2634 gbh_copies
= SPA_DVAS_PER_BP
- 1;
2636 int flags
= METASLAB_HINTBP_FAVOR
| METASLAB_GANG_HEADER
;
2637 if (pio
->io_flags
& ZIO_FLAG_IO_ALLOCATING
) {
2638 ASSERT(pio
->io_priority
== ZIO_PRIORITY_ASYNC_WRITE
);
2639 ASSERT(!(pio
->io_flags
& ZIO_FLAG_NODATA
));
2641 flags
|= METASLAB_ASYNC_ALLOC
;
2642 VERIFY(refcount_held(&mc
->mc_alloc_slots
[pio
->io_allocator
],
2646 * The logical zio has already placed a reservation for
2647 * 'copies' allocation slots but gang blocks may require
2648 * additional copies. These additional copies
2649 * (i.e. gbh_copies - copies) are guaranteed to succeed
2650 * since metaslab_class_throttle_reserve() always allows
2651 * additional reservations for gang blocks.
2653 VERIFY(metaslab_class_throttle_reserve(mc
, gbh_copies
- copies
,
2654 pio
->io_allocator
, pio
, flags
));
2657 error
= metaslab_alloc(spa
, mc
, SPA_GANGBLOCKSIZE
,
2658 bp
, gbh_copies
, txg
, pio
== gio
? NULL
: gio
->io_bp
, flags
,
2659 &pio
->io_alloc_list
, pio
, pio
->io_allocator
);
2661 if (pio
->io_flags
& ZIO_FLAG_IO_ALLOCATING
) {
2662 ASSERT(pio
->io_priority
== ZIO_PRIORITY_ASYNC_WRITE
);
2663 ASSERT(!(pio
->io_flags
& ZIO_FLAG_NODATA
));
2666 * If we failed to allocate the gang block header then
2667 * we remove any additional allocation reservations that
2668 * we placed here. The original reservation will
2669 * be removed when the logical I/O goes to the ready
2672 metaslab_class_throttle_unreserve(mc
,
2673 gbh_copies
- copies
, pio
->io_allocator
, pio
);
2676 pio
->io_error
= error
;
2681 gnpp
= &gio
->io_gang_tree
;
2683 gnpp
= pio
->io_private
;
2684 ASSERT(pio
->io_ready
== zio_write_gang_member_ready
);
2687 gn
= zio_gang_node_alloc(gnpp
);
2689 bzero(gbh
, SPA_GANGBLOCKSIZE
);
2690 gbh_abd
= abd_get_from_buf(gbh
, SPA_GANGBLOCKSIZE
);
2693 * Create the gang header.
2695 zio
= zio_rewrite(pio
, spa
, txg
, bp
, gbh_abd
, SPA_GANGBLOCKSIZE
,
2696 zio_write_gang_done
, NULL
, pio
->io_priority
,
2697 ZIO_GANG_CHILD_FLAGS(pio
), &pio
->io_bookmark
);
2700 * Create and nowait the gang children.
2702 for (int g
= 0; resid
!= 0; resid
-= lsize
, g
++) {
2703 lsize
= P2ROUNDUP(resid
/ (SPA_GBH_NBLKPTRS
- g
),
2705 ASSERT(lsize
>= SPA_MINBLOCKSIZE
&& lsize
<= resid
);
2707 zp
.zp_checksum
= gio
->io_prop
.zp_checksum
;
2708 zp
.zp_compress
= ZIO_COMPRESS_OFF
;
2709 zp
.zp_type
= DMU_OT_NONE
;
2711 zp
.zp_copies
= gio
->io_prop
.zp_copies
;
2712 zp
.zp_dedup
= B_FALSE
;
2713 zp
.zp_dedup_verify
= B_FALSE
;
2714 zp
.zp_nopwrite
= B_FALSE
;
2715 zp
.zp_encrypt
= gio
->io_prop
.zp_encrypt
;
2716 zp
.zp_byteorder
= gio
->io_prop
.zp_byteorder
;
2717 bzero(zp
.zp_salt
, ZIO_DATA_SALT_LEN
);
2718 bzero(zp
.zp_iv
, ZIO_DATA_IV_LEN
);
2719 bzero(zp
.zp_mac
, ZIO_DATA_MAC_LEN
);
2721 zio_t
*cio
= zio_write(zio
, spa
, txg
, &gbh
->zg_blkptr
[g
],
2722 abd_get_offset(pio
->io_abd
, pio
->io_size
- resid
), lsize
,
2723 lsize
, &zp
, zio_write_gang_member_ready
, NULL
, NULL
,
2724 zio_write_gang_done
, &gn
->gn_child
[g
], pio
->io_priority
,
2725 ZIO_GANG_CHILD_FLAGS(pio
), &pio
->io_bookmark
);
2727 if (pio
->io_flags
& ZIO_FLAG_IO_ALLOCATING
) {
2728 ASSERT(pio
->io_priority
== ZIO_PRIORITY_ASYNC_WRITE
);
2729 ASSERT(!(pio
->io_flags
& ZIO_FLAG_NODATA
));
2732 * Gang children won't throttle but we should
2733 * account for their work, so reserve an allocation
2734 * slot for them here.
2736 VERIFY(metaslab_class_throttle_reserve(mc
,
2737 zp
.zp_copies
, cio
->io_allocator
, cio
, flags
));
2743 * Set pio's pipeline to just wait for zio to finish.
2745 pio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
2748 * We didn't allocate this bp, so make sure it doesn't get unmarked.
2750 pio
->io_flags
&= ~ZIO_FLAG_FASTWRITE
;
2758 * The zio_nop_write stage in the pipeline determines if allocating a
2759 * new bp is necessary. The nopwrite feature can handle writes in
2760 * either syncing or open context (i.e. zil writes) and as a result is
2761 * mutually exclusive with dedup.
2763 * By leveraging a cryptographically secure checksum, such as SHA256, we
2764 * can compare the checksums of the new data and the old to determine if
2765 * allocating a new block is required. Note that our requirements for
2766 * cryptographic strength are fairly weak: there can't be any accidental
2767 * hash collisions, but we don't need to be secure against intentional
2768 * (malicious) collisions. To trigger a nopwrite, you have to be able
2769 * to write the file to begin with, and triggering an incorrect (hash
2770 * collision) nopwrite is no worse than simply writing to the file.
2771 * That said, there are no known attacks against the checksum algorithms
2772 * used for nopwrite, assuming that the salt and the checksums
2773 * themselves remain secret.
2776 zio_nop_write(zio_t
*zio
)
2778 blkptr_t
*bp
= zio
->io_bp
;
2779 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
2780 zio_prop_t
*zp
= &zio
->io_prop
;
2782 ASSERT(BP_GET_LEVEL(bp
) == 0);
2783 ASSERT(!(zio
->io_flags
& ZIO_FLAG_IO_REWRITE
));
2784 ASSERT(zp
->zp_nopwrite
);
2785 ASSERT(!zp
->zp_dedup
);
2786 ASSERT(zio
->io_bp_override
== NULL
);
2787 ASSERT(IO_IS_ALLOCATING(zio
));
2790 * Check to see if the original bp and the new bp have matching
2791 * characteristics (i.e. same checksum, compression algorithms, etc).
2792 * If they don't then just continue with the pipeline which will
2793 * allocate a new bp.
2795 if (BP_IS_HOLE(bp_orig
) ||
2796 !(zio_checksum_table
[BP_GET_CHECKSUM(bp
)].ci_flags
&
2797 ZCHECKSUM_FLAG_NOPWRITE
) ||
2798 BP_IS_ENCRYPTED(bp
) || BP_IS_ENCRYPTED(bp_orig
) ||
2799 BP_GET_CHECKSUM(bp
) != BP_GET_CHECKSUM(bp_orig
) ||
2800 BP_GET_COMPRESS(bp
) != BP_GET_COMPRESS(bp_orig
) ||
2801 BP_GET_DEDUP(bp
) != BP_GET_DEDUP(bp_orig
) ||
2802 zp
->zp_copies
!= BP_GET_NDVAS(bp_orig
))
2806 * If the checksums match then reset the pipeline so that we
2807 * avoid allocating a new bp and issuing any I/O.
2809 if (ZIO_CHECKSUM_EQUAL(bp
->blk_cksum
, bp_orig
->blk_cksum
)) {
2810 ASSERT(zio_checksum_table
[zp
->zp_checksum
].ci_flags
&
2811 ZCHECKSUM_FLAG_NOPWRITE
);
2812 ASSERT3U(BP_GET_PSIZE(bp
), ==, BP_GET_PSIZE(bp_orig
));
2813 ASSERT3U(BP_GET_LSIZE(bp
), ==, BP_GET_LSIZE(bp_orig
));
2814 ASSERT(zp
->zp_compress
!= ZIO_COMPRESS_OFF
);
2815 ASSERT(bcmp(&bp
->blk_prop
, &bp_orig
->blk_prop
,
2816 sizeof (uint64_t)) == 0);
2819 zio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
2820 zio
->io_flags
|= ZIO_FLAG_NOPWRITE
;
2827 * ==========================================================================
2829 * ==========================================================================
2832 zio_ddt_child_read_done(zio_t
*zio
)
2834 blkptr_t
*bp
= zio
->io_bp
;
2835 ddt_entry_t
*dde
= zio
->io_private
;
2837 zio_t
*pio
= zio_unique_parent(zio
);
2839 mutex_enter(&pio
->io_lock
);
2840 ddp
= ddt_phys_select(dde
, bp
);
2841 if (zio
->io_error
== 0)
2842 ddt_phys_clear(ddp
); /* this ddp doesn't need repair */
2844 if (zio
->io_error
== 0 && dde
->dde_repair_abd
== NULL
)
2845 dde
->dde_repair_abd
= zio
->io_abd
;
2847 abd_free(zio
->io_abd
);
2848 mutex_exit(&pio
->io_lock
);
2852 zio_ddt_read_start(zio_t
*zio
)
2854 blkptr_t
*bp
= zio
->io_bp
;
2856 ASSERT(BP_GET_DEDUP(bp
));
2857 ASSERT(BP_GET_PSIZE(bp
) == zio
->io_size
);
2858 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
2860 if (zio
->io_child_error
[ZIO_CHILD_DDT
]) {
2861 ddt_t
*ddt
= ddt_select(zio
->io_spa
, bp
);
2862 ddt_entry_t
*dde
= ddt_repair_start(ddt
, bp
);
2863 ddt_phys_t
*ddp
= dde
->dde_phys
;
2864 ddt_phys_t
*ddp_self
= ddt_phys_select(dde
, bp
);
2867 ASSERT(zio
->io_vsd
== NULL
);
2870 if (ddp_self
== NULL
)
2873 for (int p
= 0; p
< DDT_PHYS_TYPES
; p
++, ddp
++) {
2874 if (ddp
->ddp_phys_birth
== 0 || ddp
== ddp_self
)
2876 ddt_bp_create(ddt
->ddt_checksum
, &dde
->dde_key
, ddp
,
2878 zio_nowait(zio_read(zio
, zio
->io_spa
, &blk
,
2879 abd_alloc_for_io(zio
->io_size
, B_TRUE
),
2880 zio
->io_size
, zio_ddt_child_read_done
, dde
,
2881 zio
->io_priority
, ZIO_DDT_CHILD_FLAGS(zio
) |
2882 ZIO_FLAG_DONT_PROPAGATE
, &zio
->io_bookmark
));
2887 zio_nowait(zio_read(zio
, zio
->io_spa
, bp
,
2888 zio
->io_abd
, zio
->io_size
, NULL
, NULL
, zio
->io_priority
,
2889 ZIO_DDT_CHILD_FLAGS(zio
), &zio
->io_bookmark
));
2895 zio_ddt_read_done(zio_t
*zio
)
2897 blkptr_t
*bp
= zio
->io_bp
;
2899 if (zio_wait_for_children(zio
, ZIO_CHILD_DDT_BIT
, ZIO_WAIT_DONE
)) {
2903 ASSERT(BP_GET_DEDUP(bp
));
2904 ASSERT(BP_GET_PSIZE(bp
) == zio
->io_size
);
2905 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
2907 if (zio
->io_child_error
[ZIO_CHILD_DDT
]) {
2908 ddt_t
*ddt
= ddt_select(zio
->io_spa
, bp
);
2909 ddt_entry_t
*dde
= zio
->io_vsd
;
2911 ASSERT(spa_load_state(zio
->io_spa
) != SPA_LOAD_NONE
);
2915 zio
->io_stage
= ZIO_STAGE_DDT_READ_START
>> 1;
2916 zio_taskq_dispatch(zio
, ZIO_TASKQ_ISSUE
, B_FALSE
);
2919 if (dde
->dde_repair_abd
!= NULL
) {
2920 abd_copy(zio
->io_abd
, dde
->dde_repair_abd
,
2922 zio
->io_child_error
[ZIO_CHILD_DDT
] = 0;
2924 ddt_repair_done(ddt
, dde
);
2928 ASSERT(zio
->io_vsd
== NULL
);
2934 zio_ddt_collision(zio_t
*zio
, ddt_t
*ddt
, ddt_entry_t
*dde
)
2936 spa_t
*spa
= zio
->io_spa
;
2937 boolean_t do_raw
= !!(zio
->io_flags
& ZIO_FLAG_RAW
);
2939 ASSERT(!(zio
->io_bp_override
&& do_raw
));
2942 * Note: we compare the original data, not the transformed data,
2943 * because when zio->io_bp is an override bp, we will not have
2944 * pushed the I/O transforms. That's an important optimization
2945 * because otherwise we'd compress/encrypt all dmu_sync() data twice.
2946 * However, we should never get a raw, override zio so in these
2947 * cases we can compare the io_abd directly. This is useful because
2948 * it allows us to do dedup verification even if we don't have access
2949 * to the original data (for instance, if the encryption keys aren't
2953 for (int p
= DDT_PHYS_SINGLE
; p
<= DDT_PHYS_TRIPLE
; p
++) {
2954 zio_t
*lio
= dde
->dde_lead_zio
[p
];
2956 if (lio
!= NULL
&& do_raw
) {
2957 return (lio
->io_size
!= zio
->io_size
||
2958 abd_cmp(zio
->io_abd
, lio
->io_abd
) != 0);
2959 } else if (lio
!= NULL
) {
2960 return (lio
->io_orig_size
!= zio
->io_orig_size
||
2961 abd_cmp(zio
->io_orig_abd
, lio
->io_orig_abd
) != 0);
2965 for (int p
= DDT_PHYS_SINGLE
; p
<= DDT_PHYS_TRIPLE
; p
++) {
2966 ddt_phys_t
*ddp
= &dde
->dde_phys
[p
];
2968 if (ddp
->ddp_phys_birth
!= 0 && do_raw
) {
2969 blkptr_t blk
= *zio
->io_bp
;
2974 ddt_bp_fill(ddp
, &blk
, ddp
->ddp_phys_birth
);
2975 psize
= BP_GET_PSIZE(&blk
);
2977 if (psize
!= zio
->io_size
)
2982 tmpabd
= abd_alloc_for_io(psize
, B_TRUE
);
2984 error
= zio_wait(zio_read(NULL
, spa
, &blk
, tmpabd
,
2985 psize
, NULL
, NULL
, ZIO_PRIORITY_SYNC_READ
,
2986 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
2987 ZIO_FLAG_RAW
, &zio
->io_bookmark
));
2990 if (abd_cmp(tmpabd
, zio
->io_abd
) != 0)
2991 error
= SET_ERROR(ENOENT
);
2996 return (error
!= 0);
2997 } else if (ddp
->ddp_phys_birth
!= 0) {
2998 arc_buf_t
*abuf
= NULL
;
2999 arc_flags_t aflags
= ARC_FLAG_WAIT
;
3000 blkptr_t blk
= *zio
->io_bp
;
3003 ddt_bp_fill(ddp
, &blk
, ddp
->ddp_phys_birth
);
3005 if (BP_GET_LSIZE(&blk
) != zio
->io_orig_size
)
3010 error
= arc_read(NULL
, spa
, &blk
,
3011 arc_getbuf_func
, &abuf
, ZIO_PRIORITY_SYNC_READ
,
3012 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
3013 &aflags
, &zio
->io_bookmark
);
3016 if (abd_cmp_buf(zio
->io_orig_abd
, abuf
->b_data
,
3017 zio
->io_orig_size
) != 0)
3018 error
= SET_ERROR(ENOENT
);
3019 arc_buf_destroy(abuf
, &abuf
);
3023 return (error
!= 0);
3031 zio_ddt_child_write_ready(zio_t
*zio
)
3033 int p
= zio
->io_prop
.zp_copies
;
3034 ddt_t
*ddt
= ddt_select(zio
->io_spa
, zio
->io_bp
);
3035 ddt_entry_t
*dde
= zio
->io_private
;
3036 ddt_phys_t
*ddp
= &dde
->dde_phys
[p
];
3044 ASSERT(dde
->dde_lead_zio
[p
] == zio
);
3046 ddt_phys_fill(ddp
, zio
->io_bp
);
3048 zio_link_t
*zl
= NULL
;
3049 while ((pio
= zio_walk_parents(zio
, &zl
)) != NULL
)
3050 ddt_bp_fill(ddp
, pio
->io_bp
, zio
->io_txg
);
3056 zio_ddt_child_write_done(zio_t
*zio
)
3058 int p
= zio
->io_prop
.zp_copies
;
3059 ddt_t
*ddt
= ddt_select(zio
->io_spa
, zio
->io_bp
);
3060 ddt_entry_t
*dde
= zio
->io_private
;
3061 ddt_phys_t
*ddp
= &dde
->dde_phys
[p
];
3065 ASSERT(ddp
->ddp_refcnt
== 0);
3066 ASSERT(dde
->dde_lead_zio
[p
] == zio
);
3067 dde
->dde_lead_zio
[p
] = NULL
;
3069 if (zio
->io_error
== 0) {
3070 zio_link_t
*zl
= NULL
;
3071 while (zio_walk_parents(zio
, &zl
) != NULL
)
3072 ddt_phys_addref(ddp
);
3074 ddt_phys_clear(ddp
);
3081 zio_ddt_ditto_write_done(zio_t
*zio
)
3083 int p
= DDT_PHYS_DITTO
;
3084 ASSERTV(zio_prop_t
*zp
= &zio
->io_prop
);
3085 blkptr_t
*bp
= zio
->io_bp
;
3086 ddt_t
*ddt
= ddt_select(zio
->io_spa
, bp
);
3087 ddt_entry_t
*dde
= zio
->io_private
;
3088 ddt_phys_t
*ddp
= &dde
->dde_phys
[p
];
3089 ddt_key_t
*ddk
= &dde
->dde_key
;
3093 ASSERT(ddp
->ddp_refcnt
== 0);
3094 ASSERT(dde
->dde_lead_zio
[p
] == zio
);
3095 dde
->dde_lead_zio
[p
] = NULL
;
3097 if (zio
->io_error
== 0) {
3098 ASSERT(ZIO_CHECKSUM_EQUAL(bp
->blk_cksum
, ddk
->ddk_cksum
));
3099 ASSERT(zp
->zp_copies
< SPA_DVAS_PER_BP
);
3100 ASSERT(zp
->zp_copies
== BP_GET_NDVAS(bp
) - BP_IS_GANG(bp
));
3101 if (ddp
->ddp_phys_birth
!= 0)
3102 ddt_phys_free(ddt
, ddk
, ddp
, zio
->io_txg
);
3103 ddt_phys_fill(ddp
, bp
);
3110 zio_ddt_write(zio_t
*zio
)
3112 spa_t
*spa
= zio
->io_spa
;
3113 blkptr_t
*bp
= zio
->io_bp
;
3114 uint64_t txg
= zio
->io_txg
;
3115 zio_prop_t
*zp
= &zio
->io_prop
;
3116 int p
= zp
->zp_copies
;
3120 ddt_t
*ddt
= ddt_select(spa
, bp
);
3124 ASSERT(BP_GET_DEDUP(bp
));
3125 ASSERT(BP_GET_CHECKSUM(bp
) == zp
->zp_checksum
);
3126 ASSERT(BP_IS_HOLE(bp
) || zio
->io_bp_override
);
3127 ASSERT(!(zio
->io_bp_override
&& (zio
->io_flags
& ZIO_FLAG_RAW
)));
3130 dde
= ddt_lookup(ddt
, bp
, B_TRUE
);
3131 ddp
= &dde
->dde_phys
[p
];
3133 if (zp
->zp_dedup_verify
&& zio_ddt_collision(zio
, ddt
, dde
)) {
3135 * If we're using a weak checksum, upgrade to a strong checksum
3136 * and try again. If we're already using a strong checksum,
3137 * we can't resolve it, so just convert to an ordinary write.
3138 * (And automatically e-mail a paper to Nature?)
3140 if (!(zio_checksum_table
[zp
->zp_checksum
].ci_flags
&
3141 ZCHECKSUM_FLAG_DEDUP
)) {
3142 zp
->zp_checksum
= spa_dedup_checksum(spa
);
3143 zio_pop_transforms(zio
);
3144 zio
->io_stage
= ZIO_STAGE_OPEN
;
3147 zp
->zp_dedup
= B_FALSE
;
3149 zio
->io_pipeline
= ZIO_WRITE_PIPELINE
;
3154 ditto_copies
= ddt_ditto_copies_needed(ddt
, dde
, ddp
);
3155 ASSERT(ditto_copies
< SPA_DVAS_PER_BP
);
3157 if (ditto_copies
> ddt_ditto_copies_present(dde
) &&
3158 dde
->dde_lead_zio
[DDT_PHYS_DITTO
] == NULL
) {
3159 zio_prop_t czp
= *zp
;
3161 czp
.zp_copies
= ditto_copies
;
3164 * If we arrived here with an override bp, we won't have run
3165 * the transform stack, so we won't have the data we need to
3166 * generate a child i/o. So, toss the override bp and restart.
3167 * This is safe, because using the override bp is just an
3168 * optimization; and it's rare, so the cost doesn't matter.
3170 if (zio
->io_bp_override
) {
3171 zio_pop_transforms(zio
);
3172 zio
->io_stage
= ZIO_STAGE_OPEN
;
3173 zio
->io_pipeline
= ZIO_WRITE_PIPELINE
;
3174 zio
->io_bp_override
= NULL
;
3180 dio
= zio_write(zio
, spa
, txg
, bp
, zio
->io_orig_abd
,
3181 zio
->io_orig_size
, zio
->io_orig_size
, &czp
, NULL
, NULL
,
3182 NULL
, zio_ddt_ditto_write_done
, dde
, zio
->io_priority
,
3183 ZIO_DDT_CHILD_FLAGS(zio
), &zio
->io_bookmark
);
3185 zio_push_transform(dio
, zio
->io_abd
, zio
->io_size
, 0, NULL
);
3186 dde
->dde_lead_zio
[DDT_PHYS_DITTO
] = dio
;
3189 if (ddp
->ddp_phys_birth
!= 0 || dde
->dde_lead_zio
[p
] != NULL
) {
3190 if (ddp
->ddp_phys_birth
!= 0)
3191 ddt_bp_fill(ddp
, bp
, txg
);
3192 if (dde
->dde_lead_zio
[p
] != NULL
)
3193 zio_add_child(zio
, dde
->dde_lead_zio
[p
]);
3195 ddt_phys_addref(ddp
);
3196 } else if (zio
->io_bp_override
) {
3197 ASSERT(bp
->blk_birth
== txg
);
3198 ASSERT(BP_EQUAL(bp
, zio
->io_bp_override
));
3199 ddt_phys_fill(ddp
, bp
);
3200 ddt_phys_addref(ddp
);
3202 cio
= zio_write(zio
, spa
, txg
, bp
, zio
->io_orig_abd
,
3203 zio
->io_orig_size
, zio
->io_orig_size
, zp
,
3204 zio_ddt_child_write_ready
, NULL
, NULL
,
3205 zio_ddt_child_write_done
, dde
, zio
->io_priority
,
3206 ZIO_DDT_CHILD_FLAGS(zio
), &zio
->io_bookmark
);
3208 zio_push_transform(cio
, zio
->io_abd
, zio
->io_size
, 0, NULL
);
3209 dde
->dde_lead_zio
[p
] = cio
;
3222 ddt_entry_t
*freedde
; /* for debugging */
3225 zio_ddt_free(zio_t
*zio
)
3227 spa_t
*spa
= zio
->io_spa
;
3228 blkptr_t
*bp
= zio
->io_bp
;
3229 ddt_t
*ddt
= ddt_select(spa
, bp
);
3233 ASSERT(BP_GET_DEDUP(bp
));
3234 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
3237 freedde
= dde
= ddt_lookup(ddt
, bp
, B_TRUE
);
3239 ddp
= ddt_phys_select(dde
, bp
);
3241 ddt_phys_decref(ddp
);
3249 * ==========================================================================
3250 * Allocate and free blocks
3251 * ==========================================================================
3255 zio_io_to_allocate(spa_t
*spa
, int allocator
)
3259 ASSERT(MUTEX_HELD(&spa
->spa_alloc_locks
[allocator
]));
3261 zio
= avl_first(&spa
->spa_alloc_trees
[allocator
]);
3265 ASSERT(IO_IS_ALLOCATING(zio
));
3268 * Try to place a reservation for this zio. If we're unable to
3269 * reserve then we throttle.
3271 ASSERT3U(zio
->io_allocator
, ==, allocator
);
3272 if (!metaslab_class_throttle_reserve(zio
->io_metaslab_class
,
3273 zio
->io_prop
.zp_copies
, zio
->io_allocator
, zio
, 0)) {
3277 avl_remove(&spa
->spa_alloc_trees
[allocator
], zio
);
3278 ASSERT3U(zio
->io_stage
, <, ZIO_STAGE_DVA_ALLOCATE
);
3284 zio_dva_throttle(zio_t
*zio
)
3286 spa_t
*spa
= zio
->io_spa
;
3288 metaslab_class_t
*mc
;
3290 /* locate an appropriate allocation class */
3291 mc
= spa_preferred_class(spa
, zio
->io_size
, zio
->io_prop
.zp_type
,
3292 zio
->io_prop
.zp_level
, zio
->io_prop
.zp_zpl_smallblk
);
3294 if (zio
->io_priority
== ZIO_PRIORITY_SYNC_WRITE
||
3295 !mc
->mc_alloc_throttle_enabled
||
3296 zio
->io_child_type
== ZIO_CHILD_GANG
||
3297 zio
->io_flags
& ZIO_FLAG_NODATA
) {
3301 ASSERT(zio
->io_child_type
> ZIO_CHILD_GANG
);
3303 ASSERT3U(zio
->io_queued_timestamp
, >, 0);
3304 ASSERT(zio
->io_stage
== ZIO_STAGE_DVA_THROTTLE
);
3306 zbookmark_phys_t
*bm
= &zio
->io_bookmark
;
3308 * We want to try to use as many allocators as possible to help improve
3309 * performance, but we also want logically adjacent IOs to be physically
3310 * adjacent to improve sequential read performance. We chunk each object
3311 * into 2^20 block regions, and then hash based on the objset, object,
3312 * level, and region to accomplish both of these goals.
3314 zio
->io_allocator
= cityhash4(bm
->zb_objset
, bm
->zb_object
,
3315 bm
->zb_level
, bm
->zb_blkid
>> 20) % spa
->spa_alloc_count
;
3316 mutex_enter(&spa
->spa_alloc_locks
[zio
->io_allocator
]);
3317 ASSERT(zio
->io_type
== ZIO_TYPE_WRITE
);
3318 zio
->io_metaslab_class
= mc
;
3319 avl_add(&spa
->spa_alloc_trees
[zio
->io_allocator
], zio
);
3320 nio
= zio_io_to_allocate(spa
, zio
->io_allocator
);
3321 mutex_exit(&spa
->spa_alloc_locks
[zio
->io_allocator
]);
3326 zio_allocate_dispatch(spa_t
*spa
, int allocator
)
3330 mutex_enter(&spa
->spa_alloc_locks
[allocator
]);
3331 zio
= zio_io_to_allocate(spa
, allocator
);
3332 mutex_exit(&spa
->spa_alloc_locks
[allocator
]);
3336 ASSERT3U(zio
->io_stage
, ==, ZIO_STAGE_DVA_THROTTLE
);
3337 ASSERT0(zio
->io_error
);
3338 zio_taskq_dispatch(zio
, ZIO_TASKQ_ISSUE
, B_TRUE
);
3342 zio_dva_allocate(zio_t
*zio
)
3344 spa_t
*spa
= zio
->io_spa
;
3345 metaslab_class_t
*mc
;
3346 blkptr_t
*bp
= zio
->io_bp
;
3350 if (zio
->io_gang_leader
== NULL
) {
3351 ASSERT(zio
->io_child_type
> ZIO_CHILD_GANG
);
3352 zio
->io_gang_leader
= zio
;
3355 ASSERT(BP_IS_HOLE(bp
));
3356 ASSERT0(BP_GET_NDVAS(bp
));
3357 ASSERT3U(zio
->io_prop
.zp_copies
, >, 0);
3358 ASSERT3U(zio
->io_prop
.zp_copies
, <=, spa_max_replication(spa
));
3359 ASSERT3U(zio
->io_size
, ==, BP_GET_PSIZE(bp
));
3361 flags
|= (zio
->io_flags
& ZIO_FLAG_FASTWRITE
) ? METASLAB_FASTWRITE
: 0;
3362 if (zio
->io_flags
& ZIO_FLAG_NODATA
)
3363 flags
|= METASLAB_DONT_THROTTLE
;
3364 if (zio
->io_flags
& ZIO_FLAG_GANG_CHILD
)
3365 flags
|= METASLAB_GANG_CHILD
;
3366 if (zio
->io_priority
== ZIO_PRIORITY_ASYNC_WRITE
)
3367 flags
|= METASLAB_ASYNC_ALLOC
;
3370 * if not already chosen, locate an appropriate allocation class
3372 mc
= zio
->io_metaslab_class
;
3374 mc
= spa_preferred_class(spa
, zio
->io_size
,
3375 zio
->io_prop
.zp_type
, zio
->io_prop
.zp_level
,
3376 zio
->io_prop
.zp_zpl_smallblk
);
3377 zio
->io_metaslab_class
= mc
;
3380 error
= metaslab_alloc(spa
, mc
, zio
->io_size
, bp
,
3381 zio
->io_prop
.zp_copies
, zio
->io_txg
, NULL
, flags
,
3382 &zio
->io_alloc_list
, zio
, zio
->io_allocator
);
3385 * Fallback to normal class when an alloc class is full
3387 if (error
== ENOSPC
&& mc
!= spa_normal_class(spa
)) {
3389 * If throttling, transfer reservation over to normal class.
3390 * The io_allocator slot can remain the same even though we
3391 * are switching classes.
3393 if (mc
->mc_alloc_throttle_enabled
&&
3394 (zio
->io_flags
& ZIO_FLAG_IO_ALLOCATING
)) {
3395 metaslab_class_throttle_unreserve(mc
,
3396 zio
->io_prop
.zp_copies
, zio
->io_allocator
, zio
);
3397 zio
->io_flags
&= ~ZIO_FLAG_IO_ALLOCATING
;
3399 mc
= spa_normal_class(spa
);
3400 VERIFY(metaslab_class_throttle_reserve(mc
,
3401 zio
->io_prop
.zp_copies
, zio
->io_allocator
, zio
,
3402 flags
| METASLAB_MUST_RESERVE
));
3404 mc
= spa_normal_class(spa
);
3406 zio
->io_metaslab_class
= mc
;
3408 error
= metaslab_alloc(spa
, mc
, zio
->io_size
, bp
,
3409 zio
->io_prop
.zp_copies
, zio
->io_txg
, NULL
, flags
,
3410 &zio
->io_alloc_list
, zio
, zio
->io_allocator
);
3414 zfs_dbgmsg("%s: metaslab allocation failure: zio %p, "
3415 "size %llu, error %d", spa_name(spa
), zio
, zio
->io_size
,
3417 if (error
== ENOSPC
&& zio
->io_size
> SPA_MINBLOCKSIZE
)
3418 return (zio_write_gang_block(zio
));
3419 zio
->io_error
= error
;
3426 zio_dva_free(zio_t
*zio
)
3428 metaslab_free(zio
->io_spa
, zio
->io_bp
, zio
->io_txg
, B_FALSE
);
3434 zio_dva_claim(zio_t
*zio
)
3438 error
= metaslab_claim(zio
->io_spa
, zio
->io_bp
, zio
->io_txg
);
3440 zio
->io_error
= error
;
3446 * Undo an allocation. This is used by zio_done() when an I/O fails
3447 * and we want to give back the block we just allocated.
3448 * This handles both normal blocks and gang blocks.
3451 zio_dva_unallocate(zio_t
*zio
, zio_gang_node_t
*gn
, blkptr_t
*bp
)
3453 ASSERT(bp
->blk_birth
== zio
->io_txg
|| BP_IS_HOLE(bp
));
3454 ASSERT(zio
->io_bp_override
== NULL
);
3456 if (!BP_IS_HOLE(bp
))
3457 metaslab_free(zio
->io_spa
, bp
, bp
->blk_birth
, B_TRUE
);
3460 for (int g
= 0; g
< SPA_GBH_NBLKPTRS
; g
++) {
3461 zio_dva_unallocate(zio
, gn
->gn_child
[g
],
3462 &gn
->gn_gbh
->zg_blkptr
[g
]);
3468 * Try to allocate an intent log block. Return 0 on success, errno on failure.
3471 zio_alloc_zil(spa_t
*spa
, objset_t
*os
, uint64_t txg
, blkptr_t
*new_bp
,
3472 uint64_t size
, boolean_t
*slog
)
3475 zio_alloc_list_t io_alloc_list
;
3477 ASSERT(txg
> spa_syncing_txg(spa
));
3479 metaslab_trace_init(&io_alloc_list
);
3482 * Block pointer fields are useful to metaslabs for stats and debugging.
3483 * Fill in the obvious ones before calling into metaslab_alloc().
3485 BP_SET_TYPE(new_bp
, DMU_OT_INTENT_LOG
);
3486 BP_SET_PSIZE(new_bp
, size
);
3487 BP_SET_LEVEL(new_bp
, 0);
3490 * When allocating a zil block, we don't have information about
3491 * the final destination of the block except the objset it's part
3492 * of, so we just hash the objset ID to pick the allocator to get
3495 error
= metaslab_alloc(spa
, spa_log_class(spa
), size
, new_bp
, 1,
3496 txg
, NULL
, METASLAB_FASTWRITE
, &io_alloc_list
, NULL
,
3497 cityhash4(0, 0, 0, os
->os_dsl_dataset
->ds_object
) %
3498 spa
->spa_alloc_count
);
3502 error
= metaslab_alloc(spa
, spa_normal_class(spa
), size
,
3503 new_bp
, 1, txg
, NULL
, METASLAB_FASTWRITE
,
3504 &io_alloc_list
, NULL
, cityhash4(0, 0, 0,
3505 os
->os_dsl_dataset
->ds_object
) % spa
->spa_alloc_count
);
3509 metaslab_trace_fini(&io_alloc_list
);
3512 BP_SET_LSIZE(new_bp
, size
);
3513 BP_SET_PSIZE(new_bp
, size
);
3514 BP_SET_COMPRESS(new_bp
, ZIO_COMPRESS_OFF
);
3515 BP_SET_CHECKSUM(new_bp
,
3516 spa_version(spa
) >= SPA_VERSION_SLIM_ZIL
3517 ? ZIO_CHECKSUM_ZILOG2
: ZIO_CHECKSUM_ZILOG
);
3518 BP_SET_TYPE(new_bp
, DMU_OT_INTENT_LOG
);
3519 BP_SET_LEVEL(new_bp
, 0);
3520 BP_SET_DEDUP(new_bp
, 0);
3521 BP_SET_BYTEORDER(new_bp
, ZFS_HOST_BYTEORDER
);
3524 * encrypted blocks will require an IV and salt. We generate
3525 * these now since we will not be rewriting the bp at
3528 if (os
->os_encrypted
) {
3529 uint8_t iv
[ZIO_DATA_IV_LEN
];
3530 uint8_t salt
[ZIO_DATA_SALT_LEN
];
3532 BP_SET_CRYPT(new_bp
, B_TRUE
);
3533 VERIFY0(spa_crypt_get_salt(spa
,
3534 dmu_objset_id(os
), salt
));
3535 VERIFY0(zio_crypt_generate_iv(iv
));
3537 zio_crypt_encode_params_bp(new_bp
, salt
, iv
);
3540 zfs_dbgmsg("%s: zil block allocation failure: "
3541 "size %llu, error %d", spa_name(spa
), size
, error
);
3548 * ==========================================================================
3549 * Read and write to physical devices
3550 * ==========================================================================
3555 * Issue an I/O to the underlying vdev. Typically the issue pipeline
3556 * stops after this stage and will resume upon I/O completion.
3557 * However, there are instances where the vdev layer may need to
3558 * continue the pipeline when an I/O was not issued. Since the I/O
3559 * that was sent to the vdev layer might be different than the one
3560 * currently active in the pipeline (see vdev_queue_io()), we explicitly
3561 * force the underlying vdev layers to call either zio_execute() or
3562 * zio_interrupt() to ensure that the pipeline continues with the correct I/O.
3565 zio_vdev_io_start(zio_t
*zio
)
3567 vdev_t
*vd
= zio
->io_vd
;
3569 spa_t
*spa
= zio
->io_spa
;
3573 ASSERT(zio
->io_error
== 0);
3574 ASSERT(zio
->io_child_error
[ZIO_CHILD_VDEV
] == 0);
3577 if (!(zio
->io_flags
& ZIO_FLAG_CONFIG_WRITER
))
3578 spa_config_enter(spa
, SCL_ZIO
, zio
, RW_READER
);
3581 * The mirror_ops handle multiple DVAs in a single BP.
3583 vdev_mirror_ops
.vdev_op_io_start(zio
);
3587 ASSERT3P(zio
->io_logical
, !=, zio
);
3588 if (zio
->io_type
== ZIO_TYPE_WRITE
) {
3589 ASSERT(spa
->spa_trust_config
);
3592 * Note: the code can handle other kinds of writes,
3593 * but we don't expect them.
3595 if (zio
->io_vd
->vdev_removing
) {
3596 ASSERT(zio
->io_flags
&
3597 (ZIO_FLAG_PHYSICAL
| ZIO_FLAG_SELF_HEAL
|
3598 ZIO_FLAG_RESILVER
| ZIO_FLAG_INDUCE_DAMAGE
));
3602 align
= 1ULL << vd
->vdev_top
->vdev_ashift
;
3604 if (!(zio
->io_flags
& ZIO_FLAG_PHYSICAL
) &&
3605 P2PHASE(zio
->io_size
, align
) != 0) {
3606 /* Transform logical writes to be a full physical block size. */
3607 uint64_t asize
= P2ROUNDUP(zio
->io_size
, align
);
3608 abd_t
*abuf
= abd_alloc_sametype(zio
->io_abd
, asize
);
3609 ASSERT(vd
== vd
->vdev_top
);
3610 if (zio
->io_type
== ZIO_TYPE_WRITE
) {
3611 abd_copy(abuf
, zio
->io_abd
, zio
->io_size
);
3612 abd_zero_off(abuf
, zio
->io_size
, asize
- zio
->io_size
);
3614 zio_push_transform(zio
, abuf
, asize
, asize
, zio_subblock
);
3618 * If this is not a physical io, make sure that it is properly aligned
3619 * before proceeding.
3621 if (!(zio
->io_flags
& ZIO_FLAG_PHYSICAL
)) {
3622 ASSERT0(P2PHASE(zio
->io_offset
, align
));
3623 ASSERT0(P2PHASE(zio
->io_size
, align
));
3626 * For physical writes, we allow 512b aligned writes and assume
3627 * the device will perform a read-modify-write as necessary.
3629 ASSERT0(P2PHASE(zio
->io_offset
, SPA_MINBLOCKSIZE
));
3630 ASSERT0(P2PHASE(zio
->io_size
, SPA_MINBLOCKSIZE
));
3633 VERIFY(zio
->io_type
!= ZIO_TYPE_WRITE
|| spa_writeable(spa
));
3636 * If this is a repair I/O, and there's no self-healing involved --
3637 * that is, we're just resilvering what we expect to resilver --
3638 * then don't do the I/O unless zio's txg is actually in vd's DTL.
3639 * This prevents spurious resilvering.
3641 * There are a few ways that we can end up creating these spurious
3644 * 1. A resilver i/o will be issued if any DVA in the BP has a
3645 * dirty DTL. The mirror code will issue resilver writes to
3646 * each DVA, including the one(s) that are not on vdevs with dirty
3649 * 2. With nested replication, which happens when we have a
3650 * "replacing" or "spare" vdev that's a child of a mirror or raidz.
3651 * For example, given mirror(replacing(A+B), C), it's likely that
3652 * only A is out of date (it's the new device). In this case, we'll
3653 * read from C, then use the data to resilver A+B -- but we don't
3654 * actually want to resilver B, just A. The top-level mirror has no
3655 * way to know this, so instead we just discard unnecessary repairs
3656 * as we work our way down the vdev tree.
3658 * 3. ZTEST also creates mirrors of mirrors, mirrors of raidz, etc.
3659 * The same logic applies to any form of nested replication: ditto
3660 * + mirror, RAID-Z + replacing, etc.
3662 * However, indirect vdevs point off to other vdevs which may have
3663 * DTL's, so we never bypass them. The child i/os on concrete vdevs
3664 * will be properly bypassed instead.
3666 if ((zio
->io_flags
& ZIO_FLAG_IO_REPAIR
) &&
3667 !(zio
->io_flags
& ZIO_FLAG_SELF_HEAL
) &&
3668 zio
->io_txg
!= 0 && /* not a delegated i/o */
3669 vd
->vdev_ops
!= &vdev_indirect_ops
&&
3670 !vdev_dtl_contains(vd
, DTL_PARTIAL
, zio
->io_txg
, 1)) {
3671 ASSERT(zio
->io_type
== ZIO_TYPE_WRITE
);
3672 zio_vdev_io_bypass(zio
);
3676 if (vd
->vdev_ops
->vdev_op_leaf
&&
3677 (zio
->io_type
== ZIO_TYPE_READ
|| zio
->io_type
== ZIO_TYPE_WRITE
)) {
3679 if (zio
->io_type
== ZIO_TYPE_READ
&& vdev_cache_read(zio
))
3682 if ((zio
= vdev_queue_io(zio
)) == NULL
)
3685 if (!vdev_accessible(vd
, zio
)) {
3686 zio
->io_error
= SET_ERROR(ENXIO
);
3690 zio
->io_delay
= gethrtime();
3693 vd
->vdev_ops
->vdev_op_io_start(zio
);
3698 zio_vdev_io_done(zio_t
*zio
)
3700 vdev_t
*vd
= zio
->io_vd
;
3701 vdev_ops_t
*ops
= vd
? vd
->vdev_ops
: &vdev_mirror_ops
;
3702 boolean_t unexpected_error
= B_FALSE
;
3704 if (zio_wait_for_children(zio
, ZIO_CHILD_VDEV_BIT
, ZIO_WAIT_DONE
)) {
3708 ASSERT(zio
->io_type
== ZIO_TYPE_READ
|| zio
->io_type
== ZIO_TYPE_WRITE
);
3711 zio
->io_delay
= gethrtime() - zio
->io_delay
;
3713 if (vd
!= NULL
&& vd
->vdev_ops
->vdev_op_leaf
) {
3715 vdev_queue_io_done(zio
);
3717 if (zio
->io_type
== ZIO_TYPE_WRITE
)
3718 vdev_cache_write(zio
);
3720 if (zio_injection_enabled
&& zio
->io_error
== 0)
3721 zio
->io_error
= zio_handle_device_injections(vd
, zio
,
3724 if (zio_injection_enabled
&& zio
->io_error
== 0)
3725 zio
->io_error
= zio_handle_label_injection(zio
, EIO
);
3727 if (zio
->io_error
) {
3728 if (!vdev_accessible(vd
, zio
)) {
3729 zio
->io_error
= SET_ERROR(ENXIO
);
3731 unexpected_error
= B_TRUE
;
3736 ops
->vdev_op_io_done(zio
);
3738 if (unexpected_error
)
3739 VERIFY(vdev_probe(vd
, zio
) == NULL
);
3745 * This function is used to change the priority of an existing zio that is
3746 * currently in-flight. This is used by the arc to upgrade priority in the
3747 * event that a demand read is made for a block that is currently queued
3748 * as a scrub or async read IO. Otherwise, the high priority read request
3749 * would end up having to wait for the lower priority IO.
3752 zio_change_priority(zio_t
*pio
, zio_priority_t priority
)
3754 zio_t
*cio
, *cio_next
;
3755 zio_link_t
*zl
= NULL
;
3757 ASSERT3U(priority
, <, ZIO_PRIORITY_NUM_QUEUEABLE
);
3759 if (pio
->io_vd
!= NULL
&& pio
->io_vd
->vdev_ops
->vdev_op_leaf
) {
3760 vdev_queue_change_io_priority(pio
, priority
);
3762 pio
->io_priority
= priority
;
3765 mutex_enter(&pio
->io_lock
);
3766 for (cio
= zio_walk_children(pio
, &zl
); cio
!= NULL
; cio
= cio_next
) {
3767 cio_next
= zio_walk_children(pio
, &zl
);
3768 zio_change_priority(cio
, priority
);
3770 mutex_exit(&pio
->io_lock
);
3774 * For non-raidz ZIOs, we can just copy aside the bad data read from the
3775 * disk, and use that to finish the checksum ereport later.
3778 zio_vsd_default_cksum_finish(zio_cksum_report_t
*zcr
,
3779 const abd_t
*good_buf
)
3781 /* no processing needed */
3782 zfs_ereport_finish_checksum(zcr
, good_buf
, zcr
->zcr_cbdata
, B_FALSE
);
3787 zio_vsd_default_cksum_report(zio_t
*zio
, zio_cksum_report_t
*zcr
, void *ignored
)
3789 void *abd
= abd_alloc_sametype(zio
->io_abd
, zio
->io_size
);
3791 abd_copy(abd
, zio
->io_abd
, zio
->io_size
);
3793 zcr
->zcr_cbinfo
= zio
->io_size
;
3794 zcr
->zcr_cbdata
= abd
;
3795 zcr
->zcr_finish
= zio_vsd_default_cksum_finish
;
3796 zcr
->zcr_free
= zio_abd_free
;
3800 zio_vdev_io_assess(zio_t
*zio
)
3802 vdev_t
*vd
= zio
->io_vd
;
3804 if (zio_wait_for_children(zio
, ZIO_CHILD_VDEV_BIT
, ZIO_WAIT_DONE
)) {
3808 if (vd
== NULL
&& !(zio
->io_flags
& ZIO_FLAG_CONFIG_WRITER
))
3809 spa_config_exit(zio
->io_spa
, SCL_ZIO
, zio
);
3811 if (zio
->io_vsd
!= NULL
) {
3812 zio
->io_vsd_ops
->vsd_free(zio
);
3816 if (zio_injection_enabled
&& zio
->io_error
== 0)
3817 zio
->io_error
= zio_handle_fault_injection(zio
, EIO
);
3820 * If the I/O failed, determine whether we should attempt to retry it.
3822 * On retry, we cut in line in the issue queue, since we don't want
3823 * compression/checksumming/etc. work to prevent our (cheap) IO reissue.
3825 if (zio
->io_error
&& vd
== NULL
&&
3826 !(zio
->io_flags
& (ZIO_FLAG_DONT_RETRY
| ZIO_FLAG_IO_RETRY
))) {
3827 ASSERT(!(zio
->io_flags
& ZIO_FLAG_DONT_QUEUE
)); /* not a leaf */
3828 ASSERT(!(zio
->io_flags
& ZIO_FLAG_IO_BYPASS
)); /* not a leaf */
3830 zio
->io_flags
|= ZIO_FLAG_IO_RETRY
|
3831 ZIO_FLAG_DONT_CACHE
| ZIO_FLAG_DONT_AGGREGATE
;
3832 zio
->io_stage
= ZIO_STAGE_VDEV_IO_START
>> 1;
3833 zio_taskq_dispatch(zio
, ZIO_TASKQ_ISSUE
,
3834 zio_requeue_io_start_cut_in_line
);
3839 * If we got an error on a leaf device, convert it to ENXIO
3840 * if the device is not accessible at all.
3842 if (zio
->io_error
&& vd
!= NULL
&& vd
->vdev_ops
->vdev_op_leaf
&&
3843 !vdev_accessible(vd
, zio
))
3844 zio
->io_error
= SET_ERROR(ENXIO
);
3847 * If we can't write to an interior vdev (mirror or RAID-Z),
3848 * set vdev_cant_write so that we stop trying to allocate from it.
3850 if (zio
->io_error
== ENXIO
&& zio
->io_type
== ZIO_TYPE_WRITE
&&
3851 vd
!= NULL
&& !vd
->vdev_ops
->vdev_op_leaf
) {
3852 vd
->vdev_cant_write
= B_TRUE
;
3856 * If a cache flush returns ENOTSUP or ENOTTY, we know that no future
3857 * attempts will ever succeed. In this case we set a persistent bit so
3858 * that we don't bother with it in the future.
3860 if ((zio
->io_error
== ENOTSUP
|| zio
->io_error
== ENOTTY
) &&
3861 zio
->io_type
== ZIO_TYPE_IOCTL
&&
3862 zio
->io_cmd
== DKIOCFLUSHWRITECACHE
&& vd
!= NULL
)
3863 vd
->vdev_nowritecache
= B_TRUE
;
3866 zio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
3868 if (vd
!= NULL
&& vd
->vdev_ops
->vdev_op_leaf
&&
3869 zio
->io_physdone
!= NULL
) {
3870 ASSERT(!(zio
->io_flags
& ZIO_FLAG_DELEGATED
));
3871 ASSERT(zio
->io_child_type
== ZIO_CHILD_VDEV
);
3872 zio
->io_physdone(zio
->io_logical
);
3879 zio_vdev_io_reissue(zio_t
*zio
)
3881 ASSERT(zio
->io_stage
== ZIO_STAGE_VDEV_IO_START
);
3882 ASSERT(zio
->io_error
== 0);
3884 zio
->io_stage
>>= 1;
3888 zio_vdev_io_redone(zio_t
*zio
)
3890 ASSERT(zio
->io_stage
== ZIO_STAGE_VDEV_IO_DONE
);
3892 zio
->io_stage
>>= 1;
3896 zio_vdev_io_bypass(zio_t
*zio
)
3898 ASSERT(zio
->io_stage
== ZIO_STAGE_VDEV_IO_START
);
3899 ASSERT(zio
->io_error
== 0);
3901 zio
->io_flags
|= ZIO_FLAG_IO_BYPASS
;
3902 zio
->io_stage
= ZIO_STAGE_VDEV_IO_ASSESS
>> 1;
3906 * ==========================================================================
3907 * Encrypt and store encryption parameters
3908 * ==========================================================================
3913 * This function is used for ZIO_STAGE_ENCRYPT. It is responsible for
3914 * managing the storage of encryption parameters and passing them to the
3915 * lower-level encryption functions.
3918 zio_encrypt(zio_t
*zio
)
3920 zio_prop_t
*zp
= &zio
->io_prop
;
3921 spa_t
*spa
= zio
->io_spa
;
3922 blkptr_t
*bp
= zio
->io_bp
;
3923 uint64_t psize
= BP_GET_PSIZE(bp
);
3924 uint64_t dsobj
= zio
->io_bookmark
.zb_objset
;
3925 dmu_object_type_t ot
= BP_GET_TYPE(bp
);
3926 void *enc_buf
= NULL
;
3928 uint8_t salt
[ZIO_DATA_SALT_LEN
];
3929 uint8_t iv
[ZIO_DATA_IV_LEN
];
3930 uint8_t mac
[ZIO_DATA_MAC_LEN
];
3931 boolean_t no_crypt
= B_FALSE
;
3933 /* the root zio already encrypted the data */
3934 if (zio
->io_child_type
== ZIO_CHILD_GANG
)
3937 /* only ZIL blocks are re-encrypted on rewrite */
3938 if (!IO_IS_ALLOCATING(zio
) && ot
!= DMU_OT_INTENT_LOG
)
3941 if (!(zp
->zp_encrypt
|| BP_IS_ENCRYPTED(bp
))) {
3942 BP_SET_CRYPT(bp
, B_FALSE
);
3946 /* if we are doing raw encryption set the provided encryption params */
3947 if (zio
->io_flags
& ZIO_FLAG_RAW_ENCRYPT
) {
3948 ASSERT0(BP_GET_LEVEL(bp
));
3949 BP_SET_CRYPT(bp
, B_TRUE
);
3950 BP_SET_BYTEORDER(bp
, zp
->zp_byteorder
);
3951 if (ot
!= DMU_OT_OBJSET
)
3952 zio_crypt_encode_mac_bp(bp
, zp
->zp_mac
);
3954 /* dnode blocks must be written out in the provided byteorder */
3955 if (zp
->zp_byteorder
!= ZFS_HOST_BYTEORDER
&&
3956 ot
== DMU_OT_DNODE
) {
3957 void *bswap_buf
= zio_buf_alloc(psize
);
3958 abd_t
*babd
= abd_get_from_buf(bswap_buf
, psize
);
3960 ASSERT3U(BP_GET_COMPRESS(bp
), ==, ZIO_COMPRESS_OFF
);
3961 abd_copy_to_buf(bswap_buf
, zio
->io_abd
, psize
);
3962 dmu_ot_byteswap
[DMU_OT_BYTESWAP(ot
)].ob_func(bswap_buf
,
3965 abd_take_ownership_of_buf(babd
, B_TRUE
);
3966 zio_push_transform(zio
, babd
, psize
, psize
, NULL
);
3969 if (DMU_OT_IS_ENCRYPTED(ot
))
3970 zio_crypt_encode_params_bp(bp
, zp
->zp_salt
, zp
->zp_iv
);
3974 /* indirect blocks only maintain a cksum of the lower level MACs */
3975 if (BP_GET_LEVEL(bp
) > 0) {
3976 BP_SET_CRYPT(bp
, B_TRUE
);
3977 VERIFY0(zio_crypt_do_indirect_mac_checksum_abd(B_TRUE
,
3978 zio
->io_orig_abd
, BP_GET_LSIZE(bp
), BP_SHOULD_BYTESWAP(bp
),
3980 zio_crypt_encode_mac_bp(bp
, mac
);
3985 * Objset blocks are a special case since they have 2 256-bit MACs
3986 * embedded within them.
3988 if (ot
== DMU_OT_OBJSET
) {
3989 ASSERT0(DMU_OT_IS_ENCRYPTED(ot
));
3990 ASSERT3U(BP_GET_COMPRESS(bp
), ==, ZIO_COMPRESS_OFF
);
3991 BP_SET_CRYPT(bp
, B_TRUE
);
3992 VERIFY0(spa_do_crypt_objset_mac_abd(B_TRUE
, spa
, dsobj
,
3993 zio
->io_abd
, psize
, BP_SHOULD_BYTESWAP(bp
)));
3997 /* unencrypted object types are only authenticated with a MAC */
3998 if (!DMU_OT_IS_ENCRYPTED(ot
)) {
3999 BP_SET_CRYPT(bp
, B_TRUE
);
4000 VERIFY0(spa_do_crypt_mac_abd(B_TRUE
, spa
, dsobj
,
4001 zio
->io_abd
, psize
, mac
));
4002 zio_crypt_encode_mac_bp(bp
, mac
);
4007 * Later passes of sync-to-convergence may decide to rewrite data
4008 * in place to avoid more disk reallocations. This presents a problem
4009 * for encryption because this consitutes rewriting the new data with
4010 * the same encryption key and IV. However, this only applies to blocks
4011 * in the MOS (particularly the spacemaps) and we do not encrypt the
4012 * MOS. We assert that the zio is allocating or an intent log write
4015 ASSERT(IO_IS_ALLOCATING(zio
) || ot
== DMU_OT_INTENT_LOG
);
4016 ASSERT(BP_GET_LEVEL(bp
) == 0 || ot
== DMU_OT_INTENT_LOG
);
4017 ASSERT(spa_feature_is_active(spa
, SPA_FEATURE_ENCRYPTION
));
4018 ASSERT3U(psize
, !=, 0);
4020 enc_buf
= zio_buf_alloc(psize
);
4021 eabd
= abd_get_from_buf(enc_buf
, psize
);
4022 abd_take_ownership_of_buf(eabd
, B_TRUE
);
4025 * For an explanation of what encryption parameters are stored
4026 * where, see the block comment in zio_crypt.c.
4028 if (ot
== DMU_OT_INTENT_LOG
) {
4029 zio_crypt_decode_params_bp(bp
, salt
, iv
);
4031 BP_SET_CRYPT(bp
, B_TRUE
);
4034 /* Perform the encryption. This should not fail */
4035 VERIFY0(spa_do_crypt_abd(B_TRUE
, spa
, &zio
->io_bookmark
,
4036 BP_GET_TYPE(bp
), BP_GET_DEDUP(bp
), BP_SHOULD_BYTESWAP(bp
),
4037 salt
, iv
, mac
, psize
, zio
->io_abd
, eabd
, &no_crypt
));
4039 /* encode encryption metadata into the bp */
4040 if (ot
== DMU_OT_INTENT_LOG
) {
4042 * ZIL blocks store the MAC in the embedded checksum, so the
4043 * transform must always be applied.
4045 zio_crypt_encode_mac_zil(enc_buf
, mac
);
4046 zio_push_transform(zio
, eabd
, psize
, psize
, NULL
);
4048 BP_SET_CRYPT(bp
, B_TRUE
);
4049 zio_crypt_encode_params_bp(bp
, salt
, iv
);
4050 zio_crypt_encode_mac_bp(bp
, mac
);
4053 ASSERT3U(ot
, ==, DMU_OT_DNODE
);
4056 zio_push_transform(zio
, eabd
, psize
, psize
, NULL
);
4064 * ==========================================================================
4065 * Generate and verify checksums
4066 * ==========================================================================
4069 zio_checksum_generate(zio_t
*zio
)
4071 blkptr_t
*bp
= zio
->io_bp
;
4072 enum zio_checksum checksum
;
4076 * This is zio_write_phys().
4077 * We're either generating a label checksum, or none at all.
4079 checksum
= zio
->io_prop
.zp_checksum
;
4081 if (checksum
== ZIO_CHECKSUM_OFF
)
4084 ASSERT(checksum
== ZIO_CHECKSUM_LABEL
);
4086 if (BP_IS_GANG(bp
) && zio
->io_child_type
== ZIO_CHILD_GANG
) {
4087 ASSERT(!IO_IS_ALLOCATING(zio
));
4088 checksum
= ZIO_CHECKSUM_GANG_HEADER
;
4090 checksum
= BP_GET_CHECKSUM(bp
);
4094 zio_checksum_compute(zio
, checksum
, zio
->io_abd
, zio
->io_size
);
4100 zio_checksum_verify(zio_t
*zio
)
4102 zio_bad_cksum_t info
;
4103 blkptr_t
*bp
= zio
->io_bp
;
4106 ASSERT(zio
->io_vd
!= NULL
);
4110 * This is zio_read_phys().
4111 * We're either verifying a label checksum, or nothing at all.
4113 if (zio
->io_prop
.zp_checksum
== ZIO_CHECKSUM_OFF
)
4116 ASSERT(zio
->io_prop
.zp_checksum
== ZIO_CHECKSUM_LABEL
);
4119 if ((error
= zio_checksum_error(zio
, &info
)) != 0) {
4120 zio
->io_error
= error
;
4121 if (error
== ECKSUM
&&
4122 !(zio
->io_flags
& ZIO_FLAG_SPECULATIVE
)) {
4123 zfs_ereport_start_checksum(zio
->io_spa
,
4124 zio
->io_vd
, &zio
->io_bookmark
, zio
,
4125 zio
->io_offset
, zio
->io_size
, NULL
, &info
);
4133 * Called by RAID-Z to ensure we don't compute the checksum twice.
4136 zio_checksum_verified(zio_t
*zio
)
4138 zio
->io_pipeline
&= ~ZIO_STAGE_CHECKSUM_VERIFY
;
4142 * ==========================================================================
4143 * Error rank. Error are ranked in the order 0, ENXIO, ECKSUM, EIO, other.
4144 * An error of 0 indicates success. ENXIO indicates whole-device failure,
4145 * which may be transient (e.g. unplugged) or permament. ECKSUM and EIO
4146 * indicate errors that are specific to one I/O, and most likely permanent.
4147 * Any other error is presumed to be worse because we weren't expecting it.
4148 * ==========================================================================
4151 zio_worst_error(int e1
, int e2
)
4153 static int zio_error_rank
[] = { 0, ENXIO
, ECKSUM
, EIO
};
4156 for (r1
= 0; r1
< sizeof (zio_error_rank
) / sizeof (int); r1
++)
4157 if (e1
== zio_error_rank
[r1
])
4160 for (r2
= 0; r2
< sizeof (zio_error_rank
) / sizeof (int); r2
++)
4161 if (e2
== zio_error_rank
[r2
])
4164 return (r1
> r2
? e1
: e2
);
4168 * ==========================================================================
4170 * ==========================================================================
4173 zio_ready(zio_t
*zio
)
4175 blkptr_t
*bp
= zio
->io_bp
;
4176 zio_t
*pio
, *pio_next
;
4177 zio_link_t
*zl
= NULL
;
4179 if (zio_wait_for_children(zio
, ZIO_CHILD_GANG_BIT
| ZIO_CHILD_DDT_BIT
,
4184 if (zio
->io_ready
) {
4185 ASSERT(IO_IS_ALLOCATING(zio
));
4186 ASSERT(bp
->blk_birth
== zio
->io_txg
|| BP_IS_HOLE(bp
) ||
4187 (zio
->io_flags
& ZIO_FLAG_NOPWRITE
));
4188 ASSERT(zio
->io_children
[ZIO_CHILD_GANG
][ZIO_WAIT_READY
] == 0);
4193 if (bp
!= NULL
&& bp
!= &zio
->io_bp_copy
)
4194 zio
->io_bp_copy
= *bp
;
4196 if (zio
->io_error
!= 0) {
4197 zio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
4199 if (zio
->io_flags
& ZIO_FLAG_IO_ALLOCATING
) {
4200 ASSERT(IO_IS_ALLOCATING(zio
));
4201 ASSERT(zio
->io_priority
== ZIO_PRIORITY_ASYNC_WRITE
);
4202 ASSERT(zio
->io_metaslab_class
!= NULL
);
4205 * We were unable to allocate anything, unreserve and
4206 * issue the next I/O to allocate.
4208 metaslab_class_throttle_unreserve(
4209 zio
->io_metaslab_class
, zio
->io_prop
.zp_copies
,
4210 zio
->io_allocator
, zio
);
4211 zio_allocate_dispatch(zio
->io_spa
, zio
->io_allocator
);
4215 mutex_enter(&zio
->io_lock
);
4216 zio
->io_state
[ZIO_WAIT_READY
] = 1;
4217 pio
= zio_walk_parents(zio
, &zl
);
4218 mutex_exit(&zio
->io_lock
);
4221 * As we notify zio's parents, new parents could be added.
4222 * New parents go to the head of zio's io_parent_list, however,
4223 * so we will (correctly) not notify them. The remainder of zio's
4224 * io_parent_list, from 'pio_next' onward, cannot change because
4225 * all parents must wait for us to be done before they can be done.
4227 for (; pio
!= NULL
; pio
= pio_next
) {
4228 pio_next
= zio_walk_parents(zio
, &zl
);
4229 zio_notify_parent(pio
, zio
, ZIO_WAIT_READY
, NULL
);
4232 if (zio
->io_flags
& ZIO_FLAG_NODATA
) {
4233 if (BP_IS_GANG(bp
)) {
4234 zio
->io_flags
&= ~ZIO_FLAG_NODATA
;
4236 ASSERT((uintptr_t)zio
->io_abd
< SPA_MAXBLOCKSIZE
);
4237 zio
->io_pipeline
&= ~ZIO_VDEV_IO_STAGES
;
4241 if (zio_injection_enabled
&&
4242 zio
->io_spa
->spa_syncing_txg
== zio
->io_txg
)
4243 zio_handle_ignored_writes(zio
);
4249 * Update the allocation throttle accounting.
4252 zio_dva_throttle_done(zio_t
*zio
)
4254 ASSERTV(zio_t
*lio
= zio
->io_logical
);
4255 zio_t
*pio
= zio_unique_parent(zio
);
4256 vdev_t
*vd
= zio
->io_vd
;
4257 int flags
= METASLAB_ASYNC_ALLOC
;
4259 ASSERT3P(zio
->io_bp
, !=, NULL
);
4260 ASSERT3U(zio
->io_type
, ==, ZIO_TYPE_WRITE
);
4261 ASSERT3U(zio
->io_priority
, ==, ZIO_PRIORITY_ASYNC_WRITE
);
4262 ASSERT3U(zio
->io_child_type
, ==, ZIO_CHILD_VDEV
);
4264 ASSERT3P(vd
, ==, vd
->vdev_top
);
4265 ASSERT(zio_injection_enabled
|| !(zio
->io_flags
& ZIO_FLAG_IO_RETRY
));
4266 ASSERT(!(zio
->io_flags
& ZIO_FLAG_IO_REPAIR
));
4267 ASSERT(zio
->io_flags
& ZIO_FLAG_IO_ALLOCATING
);
4268 ASSERT(!(lio
->io_flags
& ZIO_FLAG_IO_REWRITE
));
4269 ASSERT(!(lio
->io_orig_flags
& ZIO_FLAG_NODATA
));
4272 * Parents of gang children can have two flavors -- ones that
4273 * allocated the gang header (will have ZIO_FLAG_IO_REWRITE set)
4274 * and ones that allocated the constituent blocks. The allocation
4275 * throttle needs to know the allocating parent zio so we must find
4278 if (pio
->io_child_type
== ZIO_CHILD_GANG
) {
4280 * If our parent is a rewrite gang child then our grandparent
4281 * would have been the one that performed the allocation.
4283 if (pio
->io_flags
& ZIO_FLAG_IO_REWRITE
)
4284 pio
= zio_unique_parent(pio
);
4285 flags
|= METASLAB_GANG_CHILD
;
4288 ASSERT(IO_IS_ALLOCATING(pio
));
4289 ASSERT3P(zio
, !=, zio
->io_logical
);
4290 ASSERT(zio
->io_logical
!= NULL
);
4291 ASSERT(!(zio
->io_flags
& ZIO_FLAG_IO_REPAIR
));
4292 ASSERT0(zio
->io_flags
& ZIO_FLAG_NOPWRITE
);
4293 ASSERT(zio
->io_metaslab_class
!= NULL
);
4295 mutex_enter(&pio
->io_lock
);
4296 metaslab_group_alloc_decrement(zio
->io_spa
, vd
->vdev_id
, pio
, flags
,
4297 pio
->io_allocator
, B_TRUE
);
4298 mutex_exit(&pio
->io_lock
);
4300 metaslab_class_throttle_unreserve(zio
->io_metaslab_class
, 1,
4301 pio
->io_allocator
, pio
);
4304 * Call into the pipeline to see if there is more work that
4305 * needs to be done. If there is work to be done it will be
4306 * dispatched to another taskq thread.
4308 zio_allocate_dispatch(zio
->io_spa
, pio
->io_allocator
);
4312 zio_done(zio_t
*zio
)
4315 * Always attempt to keep stack usage minimal here since
4316 * we can be called recurisvely up to 19 levels deep.
4318 const uint64_t psize
= zio
->io_size
;
4319 zio_t
*pio
, *pio_next
;
4320 zio_link_t
*zl
= NULL
;
4323 * If our children haven't all completed,
4324 * wait for them and then repeat this pipeline stage.
4326 if (zio_wait_for_children(zio
, ZIO_CHILD_ALL_BITS
, ZIO_WAIT_DONE
)) {
4331 * If the allocation throttle is enabled, then update the accounting.
4332 * We only track child I/Os that are part of an allocating async
4333 * write. We must do this since the allocation is performed
4334 * by the logical I/O but the actual write is done by child I/Os.
4336 if (zio
->io_flags
& ZIO_FLAG_IO_ALLOCATING
&&
4337 zio
->io_child_type
== ZIO_CHILD_VDEV
) {
4338 ASSERT(zio
->io_metaslab_class
!= NULL
);
4339 ASSERT(zio
->io_metaslab_class
->mc_alloc_throttle_enabled
);
4340 zio_dva_throttle_done(zio
);
4344 * If the allocation throttle is enabled, verify that
4345 * we have decremented the refcounts for every I/O that was throttled.
4347 if (zio
->io_flags
& ZIO_FLAG_IO_ALLOCATING
) {
4348 ASSERT(zio
->io_type
== ZIO_TYPE_WRITE
);
4349 ASSERT(zio
->io_priority
== ZIO_PRIORITY_ASYNC_WRITE
);
4350 ASSERT(zio
->io_bp
!= NULL
);
4352 metaslab_group_alloc_verify(zio
->io_spa
, zio
->io_bp
, zio
,
4354 VERIFY(refcount_not_held(
4355 &zio
->io_metaslab_class
->mc_alloc_slots
[zio
->io_allocator
],
4360 for (int c
= 0; c
< ZIO_CHILD_TYPES
; c
++)
4361 for (int w
= 0; w
< ZIO_WAIT_TYPES
; w
++)
4362 ASSERT(zio
->io_children
[c
][w
] == 0);
4364 if (zio
->io_bp
!= NULL
&& !BP_IS_EMBEDDED(zio
->io_bp
)) {
4365 ASSERT(zio
->io_bp
->blk_pad
[0] == 0);
4366 ASSERT(zio
->io_bp
->blk_pad
[1] == 0);
4367 ASSERT(bcmp(zio
->io_bp
, &zio
->io_bp_copy
,
4368 sizeof (blkptr_t
)) == 0 ||
4369 (zio
->io_bp
== zio_unique_parent(zio
)->io_bp
));
4370 if (zio
->io_type
== ZIO_TYPE_WRITE
&& !BP_IS_HOLE(zio
->io_bp
) &&
4371 zio
->io_bp_override
== NULL
&&
4372 !(zio
->io_flags
& ZIO_FLAG_IO_REPAIR
)) {
4373 ASSERT3U(zio
->io_prop
.zp_copies
, <=,
4374 BP_GET_NDVAS(zio
->io_bp
));
4375 ASSERT(BP_COUNT_GANG(zio
->io_bp
) == 0 ||
4376 (BP_COUNT_GANG(zio
->io_bp
) ==
4377 BP_GET_NDVAS(zio
->io_bp
)));
4379 if (zio
->io_flags
& ZIO_FLAG_NOPWRITE
)
4380 VERIFY(BP_EQUAL(zio
->io_bp
, &zio
->io_bp_orig
));
4384 * If there were child vdev/gang/ddt errors, they apply to us now.
4386 zio_inherit_child_errors(zio
, ZIO_CHILD_VDEV
);
4387 zio_inherit_child_errors(zio
, ZIO_CHILD_GANG
);
4388 zio_inherit_child_errors(zio
, ZIO_CHILD_DDT
);
4391 * If the I/O on the transformed data was successful, generate any
4392 * checksum reports now while we still have the transformed data.
4394 if (zio
->io_error
== 0) {
4395 while (zio
->io_cksum_report
!= NULL
) {
4396 zio_cksum_report_t
*zcr
= zio
->io_cksum_report
;
4397 uint64_t align
= zcr
->zcr_align
;
4398 uint64_t asize
= P2ROUNDUP(psize
, align
);
4399 abd_t
*adata
= zio
->io_abd
;
4401 if (asize
!= psize
) {
4402 adata
= abd_alloc(asize
, B_TRUE
);
4403 abd_copy(adata
, zio
->io_abd
, psize
);
4404 abd_zero_off(adata
, psize
, asize
- psize
);
4407 zio
->io_cksum_report
= zcr
->zcr_next
;
4408 zcr
->zcr_next
= NULL
;
4409 zcr
->zcr_finish(zcr
, adata
);
4410 zfs_ereport_free_checksum(zcr
);
4417 zio_pop_transforms(zio
); /* note: may set zio->io_error */
4419 vdev_stat_update(zio
, psize
);
4422 * If this I/O is attached to a particular vdev is slow, exceeding
4423 * 30 seconds to complete, post an error described the I/O delay.
4424 * We ignore these errors if the device is currently unavailable.
4426 if (zio
->io_delay
>= MSEC2NSEC(zio_delay_max
)) {
4427 if (zio
->io_vd
!= NULL
&& !vdev_is_dead(zio
->io_vd
))
4428 zfs_ereport_post(FM_EREPORT_ZFS_DELAY
, zio
->io_spa
,
4429 zio
->io_vd
, &zio
->io_bookmark
, zio
, 0, 0);
4432 if (zio
->io_error
) {
4434 * If this I/O is attached to a particular vdev,
4435 * generate an error message describing the I/O failure
4436 * at the block level. We ignore these errors if the
4437 * device is currently unavailable.
4439 if (zio
->io_error
!= ECKSUM
&& zio
->io_vd
!= NULL
&&
4440 !vdev_is_dead(zio
->io_vd
))
4441 zfs_ereport_post(FM_EREPORT_ZFS_IO
, zio
->io_spa
,
4442 zio
->io_vd
, &zio
->io_bookmark
, zio
, 0, 0);
4444 if ((zio
->io_error
== EIO
|| !(zio
->io_flags
&
4445 (ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_DONT_PROPAGATE
))) &&
4446 zio
== zio
->io_logical
) {
4448 * For logical I/O requests, tell the SPA to log the
4449 * error and generate a logical data ereport.
4451 spa_log_error(zio
->io_spa
, &zio
->io_bookmark
);
4452 zfs_ereport_post(FM_EREPORT_ZFS_DATA
, zio
->io_spa
,
4453 NULL
, &zio
->io_bookmark
, zio
, 0, 0);
4457 if (zio
->io_error
&& zio
== zio
->io_logical
) {
4459 * Determine whether zio should be reexecuted. This will
4460 * propagate all the way to the root via zio_notify_parent().
4462 ASSERT(zio
->io_vd
== NULL
&& zio
->io_bp
!= NULL
);
4463 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
4465 if (IO_IS_ALLOCATING(zio
) &&
4466 !(zio
->io_flags
& ZIO_FLAG_CANFAIL
)) {
4467 if (zio
->io_error
!= ENOSPC
)
4468 zio
->io_reexecute
|= ZIO_REEXECUTE_NOW
;
4470 zio
->io_reexecute
|= ZIO_REEXECUTE_SUSPEND
;
4473 if ((zio
->io_type
== ZIO_TYPE_READ
||
4474 zio
->io_type
== ZIO_TYPE_FREE
) &&
4475 !(zio
->io_flags
& ZIO_FLAG_SCAN_THREAD
) &&
4476 zio
->io_error
== ENXIO
&&
4477 spa_load_state(zio
->io_spa
) == SPA_LOAD_NONE
&&
4478 spa_get_failmode(zio
->io_spa
) != ZIO_FAILURE_MODE_CONTINUE
)
4479 zio
->io_reexecute
|= ZIO_REEXECUTE_SUSPEND
;
4481 if (!(zio
->io_flags
& ZIO_FLAG_CANFAIL
) && !zio
->io_reexecute
)
4482 zio
->io_reexecute
|= ZIO_REEXECUTE_SUSPEND
;
4485 * Here is a possibly good place to attempt to do
4486 * either combinatorial reconstruction or error correction
4487 * based on checksums. It also might be a good place
4488 * to send out preliminary ereports before we suspend
4494 * If there were logical child errors, they apply to us now.
4495 * We defer this until now to avoid conflating logical child
4496 * errors with errors that happened to the zio itself when
4497 * updating vdev stats and reporting FMA events above.
4499 zio_inherit_child_errors(zio
, ZIO_CHILD_LOGICAL
);
4501 if ((zio
->io_error
|| zio
->io_reexecute
) &&
4502 IO_IS_ALLOCATING(zio
) && zio
->io_gang_leader
== zio
&&
4503 !(zio
->io_flags
& (ZIO_FLAG_IO_REWRITE
| ZIO_FLAG_NOPWRITE
)))
4504 zio_dva_unallocate(zio
, zio
->io_gang_tree
, zio
->io_bp
);
4506 zio_gang_tree_free(&zio
->io_gang_tree
);
4509 * Godfather I/Os should never suspend.
4511 if ((zio
->io_flags
& ZIO_FLAG_GODFATHER
) &&
4512 (zio
->io_reexecute
& ZIO_REEXECUTE_SUSPEND
))
4513 zio
->io_reexecute
&= ~ZIO_REEXECUTE_SUSPEND
;
4515 if (zio
->io_reexecute
) {
4517 * This is a logical I/O that wants to reexecute.
4519 * Reexecute is top-down. When an i/o fails, if it's not
4520 * the root, it simply notifies its parent and sticks around.
4521 * The parent, seeing that it still has children in zio_done(),
4522 * does the same. This percolates all the way up to the root.
4523 * The root i/o will reexecute or suspend the entire tree.
4525 * This approach ensures that zio_reexecute() honors
4526 * all the original i/o dependency relationships, e.g.
4527 * parents not executing until children are ready.
4529 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
4531 zio
->io_gang_leader
= NULL
;
4533 mutex_enter(&zio
->io_lock
);
4534 zio
->io_state
[ZIO_WAIT_DONE
] = 1;
4535 mutex_exit(&zio
->io_lock
);
4538 * "The Godfather" I/O monitors its children but is
4539 * not a true parent to them. It will track them through
4540 * the pipeline but severs its ties whenever they get into
4541 * trouble (e.g. suspended). This allows "The Godfather"
4542 * I/O to return status without blocking.
4545 for (pio
= zio_walk_parents(zio
, &zl
); pio
!= NULL
;
4547 zio_link_t
*remove_zl
= zl
;
4548 pio_next
= zio_walk_parents(zio
, &zl
);
4550 if ((pio
->io_flags
& ZIO_FLAG_GODFATHER
) &&
4551 (zio
->io_reexecute
& ZIO_REEXECUTE_SUSPEND
)) {
4552 zio_remove_child(pio
, zio
, remove_zl
);
4554 * This is a rare code path, so we don't
4555 * bother with "next_to_execute".
4557 zio_notify_parent(pio
, zio
, ZIO_WAIT_DONE
,
4562 if ((pio
= zio_unique_parent(zio
)) != NULL
) {
4564 * We're not a root i/o, so there's nothing to do
4565 * but notify our parent. Don't propagate errors
4566 * upward since we haven't permanently failed yet.
4568 ASSERT(!(zio
->io_flags
& ZIO_FLAG_GODFATHER
));
4569 zio
->io_flags
|= ZIO_FLAG_DONT_PROPAGATE
;
4571 * This is a rare code path, so we don't bother with
4572 * "next_to_execute".
4574 zio_notify_parent(pio
, zio
, ZIO_WAIT_DONE
, NULL
);
4575 } else if (zio
->io_reexecute
& ZIO_REEXECUTE_SUSPEND
) {
4577 * We'd fail again if we reexecuted now, so suspend
4578 * until conditions improve (e.g. device comes online).
4580 zio_suspend(zio
->io_spa
, zio
, ZIO_SUSPEND_IOERR
);
4583 * Reexecution is potentially a huge amount of work.
4584 * Hand it off to the otherwise-unused claim taskq.
4586 ASSERT(taskq_empty_ent(&zio
->io_tqent
));
4587 spa_taskq_dispatch_ent(zio
->io_spa
,
4588 ZIO_TYPE_CLAIM
, ZIO_TASKQ_ISSUE
,
4589 (task_func_t
*)zio_reexecute
, zio
, 0,
4595 ASSERT(zio
->io_child_count
== 0);
4596 ASSERT(zio
->io_reexecute
== 0);
4597 ASSERT(zio
->io_error
== 0 || (zio
->io_flags
& ZIO_FLAG_CANFAIL
));
4600 * Report any checksum errors, since the I/O is complete.
4602 while (zio
->io_cksum_report
!= NULL
) {
4603 zio_cksum_report_t
*zcr
= zio
->io_cksum_report
;
4604 zio
->io_cksum_report
= zcr
->zcr_next
;
4605 zcr
->zcr_next
= NULL
;
4606 zcr
->zcr_finish(zcr
, NULL
);
4607 zfs_ereport_free_checksum(zcr
);
4610 if (zio
->io_flags
& ZIO_FLAG_FASTWRITE
&& zio
->io_bp
&&
4611 !BP_IS_HOLE(zio
->io_bp
) && !BP_IS_EMBEDDED(zio
->io_bp
) &&
4612 !(zio
->io_flags
& ZIO_FLAG_NOPWRITE
)) {
4613 metaslab_fastwrite_unmark(zio
->io_spa
, zio
->io_bp
);
4617 * It is the responsibility of the done callback to ensure that this
4618 * particular zio is no longer discoverable for adoption, and as
4619 * such, cannot acquire any new parents.
4624 mutex_enter(&zio
->io_lock
);
4625 zio
->io_state
[ZIO_WAIT_DONE
] = 1;
4626 mutex_exit(&zio
->io_lock
);
4629 * We are done executing this zio. We may want to execute a parent
4630 * next. See the comment in zio_notify_parent().
4632 zio_t
*next_to_execute
= NULL
;
4634 for (pio
= zio_walk_parents(zio
, &zl
); pio
!= NULL
; pio
= pio_next
) {
4635 zio_link_t
*remove_zl
= zl
;
4636 pio_next
= zio_walk_parents(zio
, &zl
);
4637 zio_remove_child(pio
, zio
, remove_zl
);
4638 zio_notify_parent(pio
, zio
, ZIO_WAIT_DONE
, &next_to_execute
);
4641 if (zio
->io_waiter
!= NULL
) {
4642 mutex_enter(&zio
->io_lock
);
4643 zio
->io_executor
= NULL
;
4644 cv_broadcast(&zio
->io_cv
);
4645 mutex_exit(&zio
->io_lock
);
4650 return (next_to_execute
);
4654 * ==========================================================================
4655 * I/O pipeline definition
4656 * ==========================================================================
4658 static zio_pipe_stage_t
*zio_pipeline
[] = {
4666 zio_checksum_generate
,
4682 zio_checksum_verify
,
4690 * Compare two zbookmark_phys_t's to see which we would reach first in a
4691 * pre-order traversal of the object tree.
4693 * This is simple in every case aside from the meta-dnode object. For all other
4694 * objects, we traverse them in order (object 1 before object 2, and so on).
4695 * However, all of these objects are traversed while traversing object 0, since
4696 * the data it points to is the list of objects. Thus, we need to convert to a
4697 * canonical representation so we can compare meta-dnode bookmarks to
4698 * non-meta-dnode bookmarks.
4700 * We do this by calculating "equivalents" for each field of the zbookmark.
4701 * zbookmarks outside of the meta-dnode use their own object and level, and
4702 * calculate the level 0 equivalent (the first L0 blkid that is contained in the
4703 * blocks this bookmark refers to) by multiplying their blkid by their span
4704 * (the number of L0 blocks contained within one block at their level).
4705 * zbookmarks inside the meta-dnode calculate their object equivalent
4706 * (which is L0equiv * dnodes per data block), use 0 for their L0equiv, and use
4707 * level + 1<<31 (any value larger than a level could ever be) for their level.
4708 * This causes them to always compare before a bookmark in their object
4709 * equivalent, compare appropriately to bookmarks in other objects, and to
4710 * compare appropriately to other bookmarks in the meta-dnode.
4713 zbookmark_compare(uint16_t dbss1
, uint8_t ibs1
, uint16_t dbss2
, uint8_t ibs2
,
4714 const zbookmark_phys_t
*zb1
, const zbookmark_phys_t
*zb2
)
4717 * These variables represent the "equivalent" values for the zbookmark,
4718 * after converting zbookmarks inside the meta dnode to their
4719 * normal-object equivalents.
4721 uint64_t zb1obj
, zb2obj
;
4722 uint64_t zb1L0
, zb2L0
;
4723 uint64_t zb1level
, zb2level
;
4725 if (zb1
->zb_object
== zb2
->zb_object
&&
4726 zb1
->zb_level
== zb2
->zb_level
&&
4727 zb1
->zb_blkid
== zb2
->zb_blkid
)
4731 * BP_SPANB calculates the span in blocks.
4733 zb1L0
= (zb1
->zb_blkid
) * BP_SPANB(ibs1
, zb1
->zb_level
);
4734 zb2L0
= (zb2
->zb_blkid
) * BP_SPANB(ibs2
, zb2
->zb_level
);
4736 if (zb1
->zb_object
== DMU_META_DNODE_OBJECT
) {
4737 zb1obj
= zb1L0
* (dbss1
<< (SPA_MINBLOCKSHIFT
- DNODE_SHIFT
));
4739 zb1level
= zb1
->zb_level
+ COMPARE_META_LEVEL
;
4741 zb1obj
= zb1
->zb_object
;
4742 zb1level
= zb1
->zb_level
;
4745 if (zb2
->zb_object
== DMU_META_DNODE_OBJECT
) {
4746 zb2obj
= zb2L0
* (dbss2
<< (SPA_MINBLOCKSHIFT
- DNODE_SHIFT
));
4748 zb2level
= zb2
->zb_level
+ COMPARE_META_LEVEL
;
4750 zb2obj
= zb2
->zb_object
;
4751 zb2level
= zb2
->zb_level
;
4754 /* Now that we have a canonical representation, do the comparison. */
4755 if (zb1obj
!= zb2obj
)
4756 return (zb1obj
< zb2obj
? -1 : 1);
4757 else if (zb1L0
!= zb2L0
)
4758 return (zb1L0
< zb2L0
? -1 : 1);
4759 else if (zb1level
!= zb2level
)
4760 return (zb1level
> zb2level
? -1 : 1);
4762 * This can (theoretically) happen if the bookmarks have the same object
4763 * and level, but different blkids, if the block sizes are not the same.
4764 * There is presently no way to change the indirect block sizes
4770 * This function checks the following: given that last_block is the place that
4771 * our traversal stopped last time, does that guarantee that we've visited
4772 * every node under subtree_root? Therefore, we can't just use the raw output
4773 * of zbookmark_compare. We have to pass in a modified version of
4774 * subtree_root; by incrementing the block id, and then checking whether
4775 * last_block is before or equal to that, we can tell whether or not having
4776 * visited last_block implies that all of subtree_root's children have been
4780 zbookmark_subtree_completed(const dnode_phys_t
*dnp
,
4781 const zbookmark_phys_t
*subtree_root
, const zbookmark_phys_t
*last_block
)
4783 zbookmark_phys_t mod_zb
= *subtree_root
;
4785 ASSERT(last_block
->zb_level
== 0);
4787 /* The objset_phys_t isn't before anything. */
4792 * We pass in 1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT) for the
4793 * data block size in sectors, because that variable is only used if
4794 * the bookmark refers to a block in the meta-dnode. Since we don't
4795 * know without examining it what object it refers to, and there's no
4796 * harm in passing in this value in other cases, we always pass it in.
4798 * We pass in 0 for the indirect block size shift because zb2 must be
4799 * level 0. The indirect block size is only used to calculate the span
4800 * of the bookmark, but since the bookmark must be level 0, the span is
4801 * always 1, so the math works out.
4803 * If you make changes to how the zbookmark_compare code works, be sure
4804 * to make sure that this code still works afterwards.
4806 return (zbookmark_compare(dnp
->dn_datablkszsec
, dnp
->dn_indblkshift
,
4807 1ULL << (DNODE_BLOCK_SHIFT
- SPA_MINBLOCKSHIFT
), 0, &mod_zb
,
4811 #if defined(_KERNEL)
4812 EXPORT_SYMBOL(zio_type_name
);
4813 EXPORT_SYMBOL(zio_buf_alloc
);
4814 EXPORT_SYMBOL(zio_data_buf_alloc
);
4815 EXPORT_SYMBOL(zio_buf_free
);
4816 EXPORT_SYMBOL(zio_data_buf_free
);
4818 module_param(zio_delay_max
, int, 0644);
4819 MODULE_PARM_DESC(zio_delay_max
, "Max zio millisec delay before posting event");
4821 module_param(zio_requeue_io_start_cut_in_line
, int, 0644);
4822 MODULE_PARM_DESC(zio_requeue_io_start_cut_in_line
, "Prioritize requeued I/O");
4824 module_param(zfs_sync_pass_deferred_free
, int, 0644);
4825 MODULE_PARM_DESC(zfs_sync_pass_deferred_free
,
4826 "Defer frees starting in this pass");
4828 module_param(zfs_sync_pass_dont_compress
, int, 0644);
4829 MODULE_PARM_DESC(zfs_sync_pass_dont_compress
,
4830 "Don't compress starting in this pass");
4832 module_param(zfs_sync_pass_rewrite
, int, 0644);
4833 MODULE_PARM_DESC(zfs_sync_pass_rewrite
,
4834 "Rewrite new bps starting in this pass");
4836 module_param(zio_dva_throttle_enabled
, int, 0644);
4837 MODULE_PARM_DESC(zio_dva_throttle_enabled
,
4838 "Throttle block allocations in the ZIO pipeline");