xref: /freebsd/sys/contrib/openzfs/include/sys/zio.h (revision 22649d4dba730d46244fd2dff4fd174903c8379f)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * This file and its contents are supplied under the terms of the
4  * Common Development and Distribution License ("CDDL"), version 1.0.
5  * You may only use this file in accordance with the terms of version
6  * 1.0 of the CDDL.
7  *
8  * A full copy of the text of the CDDL should have accompanied this
9  * source.  A copy of the CDDL is also available via the Internet at
10  * https://opensource.org/license/CDDL-1.0.
11  */
12 
13 /*
14  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
15  * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
16  * Copyright (c) 2012, 2024 by Delphix. All rights reserved.
17  * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
18  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
19  * Copyright 2016 Toomas Soome <tsoome@me.com>
20  * Copyright (c) 2019, Allan Jude
21  * Copyright (c) 2019, 2023, 2024, Klara Inc.
22  * Copyright (c) 2019-2020, Michael Niewöhner
23  * Copyright (c) 2024 by George Melikov. All rights reserved.
24  */
25 
26 #ifndef _ZIO_H
27 #define	_ZIO_H
28 
29 #include <sys/zfs_context.h>
30 #include <sys/spa.h>
31 #include <sys/txg.h>
32 #include <sys/avl.h>
33 #include <sys/fs/zfs.h>
34 #include <sys/zio_impl.h>
35 
36 #ifdef	__cplusplus
37 extern "C" {
38 #endif
39 
40 /*
41  * Embedded checksum
42  */
43 #define	ZEC_MAGIC	0x210da7ab10c7a11ULL
44 
45 typedef struct zio_eck {
46 	uint64_t	zec_magic;	/* for validation, endianness	*/
47 	zio_cksum_t	zec_cksum;	/* 256-bit checksum		*/
48 } zio_eck_t;
49 
50 /*
51  * Gang block headers are self-checksumming and contain an array
52  * of block pointers. The old gang block size has enough room for 3 blkptrs,
53  * while new gang blocks can store more.
54  *
55  * Layout:
56  * +--------+--------+--------+-----+---------+-----------+
57  * |        |        |        |     |         |           |
58  * | blkptr | blkptr | blkptr | ... | padding | zio_eck_t |
59  * |   1    |   2    |   3    |     |         |           |
60  * +--------+--------+--------+-----+---------+-----------+
61  *   128B     128B     128B             88B        40B
62  */
63 #define	SPA_OLD_GANGBLOCKSIZE	SPA_MINBLOCKSIZE
64 typedef void zio_gbh_phys_t;
65 
66 static inline uint64_t
gbh_nblkptrs(uint64_t size)67 gbh_nblkptrs(uint64_t size) {
68 	ASSERT(IS_P2ALIGNED(size, sizeof (blkptr_t)));
69 	return ((size - sizeof (zio_eck_t)) / sizeof (blkptr_t));
70 }
71 
72 static inline zio_eck_t *
gbh_eck(zio_gbh_phys_t * gbh,uint64_t size)73 gbh_eck(zio_gbh_phys_t *gbh, uint64_t size) {
74 	ASSERT(IS_P2ALIGNED(size, sizeof (blkptr_t)));
75 	return ((zio_eck_t *)((uintptr_t)gbh + (size_t)size -
76 	    sizeof (zio_eck_t)));
77 }
78 
79 static inline blkptr_t *
gbh_bp(zio_gbh_phys_t * gbh,int bp)80 gbh_bp(zio_gbh_phys_t *gbh, int bp) {
81 	return (&((blkptr_t *)gbh)[bp]);
82 }
83 
84 enum zio_checksum {
85 	ZIO_CHECKSUM_INHERIT = 0,
86 	ZIO_CHECKSUM_ON,
87 	ZIO_CHECKSUM_OFF,
88 	ZIO_CHECKSUM_LABEL,
89 	ZIO_CHECKSUM_GANG_HEADER,
90 	ZIO_CHECKSUM_ZILOG,
91 	ZIO_CHECKSUM_FLETCHER_2,
92 	ZIO_CHECKSUM_FLETCHER_4,
93 	ZIO_CHECKSUM_SHA256,
94 	ZIO_CHECKSUM_ZILOG2,
95 	ZIO_CHECKSUM_NOPARITY,
96 	ZIO_CHECKSUM_SHA512,
97 	ZIO_CHECKSUM_SKEIN,
98 	ZIO_CHECKSUM_EDONR,
99 	ZIO_CHECKSUM_BLAKE3,
100 	ZIO_CHECKSUM_FUNCTIONS
101 };
102 
103 /*
104  * The number of "legacy" compression functions which can be set on individual
105  * objects.
106  */
107 #define	ZIO_CHECKSUM_LEGACY_FUNCTIONS ZIO_CHECKSUM_ZILOG2
108 
109 #define	ZIO_CHECKSUM_ON_VALUE	ZIO_CHECKSUM_FLETCHER_4
110 #define	ZIO_CHECKSUM_DEFAULT	ZIO_CHECKSUM_ON
111 
112 #define	ZIO_CHECKSUM_MASK	0xffULL
113 #define	ZIO_CHECKSUM_VERIFY	(1U << 8)
114 
115 #define	ZIO_DEDUPCHECKSUM	ZIO_CHECKSUM_SHA256
116 
117 /* macros defining encryption lengths */
118 #define	ZIO_OBJSET_MAC_LEN		32
119 #define	ZIO_DATA_IV_LEN			12
120 #define	ZIO_DATA_SALT_LEN		8
121 #define	ZIO_DATA_MAC_LEN		16
122 
123 /*
124  * The number of "legacy" compression functions which can be set on individual
125  * objects.
126  */
127 #define	ZIO_COMPRESS_LEGACY_FUNCTIONS ZIO_COMPRESS_LZ4
128 
129 /*
130  * The meaning of "compress = on" selected by the compression features enabled
131  * on a given pool.
132  */
133 #define	ZIO_COMPRESS_LEGACY_ON_VALUE	ZIO_COMPRESS_LZJB
134 #define	ZIO_COMPRESS_LZ4_ON_VALUE	ZIO_COMPRESS_LZ4
135 
136 #define	ZIO_COMPRESS_DEFAULT		ZIO_COMPRESS_ON
137 
138 #define	BOOTFS_COMPRESS_VALID(compress)			\
139 	((compress) == ZIO_COMPRESS_LZJB ||		\
140 	(compress) == ZIO_COMPRESS_LZ4 ||		\
141 	(compress) == ZIO_COMPRESS_GZIP_1 ||		\
142 	(compress) == ZIO_COMPRESS_GZIP_2 ||		\
143 	(compress) == ZIO_COMPRESS_GZIP_3 ||		\
144 	(compress) == ZIO_COMPRESS_GZIP_4 ||		\
145 	(compress) == ZIO_COMPRESS_GZIP_5 ||		\
146 	(compress) == ZIO_COMPRESS_GZIP_6 ||		\
147 	(compress) == ZIO_COMPRESS_GZIP_7 ||		\
148 	(compress) == ZIO_COMPRESS_GZIP_8 ||		\
149 	(compress) == ZIO_COMPRESS_GZIP_9 ||		\
150 	(compress) == ZIO_COMPRESS_ZLE ||		\
151 	(compress) == ZIO_COMPRESS_ZSTD ||		\
152 	(compress) == ZIO_COMPRESS_ON ||		\
153 	(compress) == ZIO_COMPRESS_OFF)
154 
155 
156 #define	ZIO_COMPRESS_ALGO(x)	(x & SPA_COMPRESSMASK)
157 #define	ZIO_COMPRESS_LEVEL(x)	((x & ~SPA_COMPRESSMASK) >> SPA_COMPRESSBITS)
158 #define	ZIO_COMPRESS_RAW(type, level)	(type | ((level) << SPA_COMPRESSBITS))
159 
160 #define	ZIO_COMPLEVEL_ZSTD(level)	\
161 	ZIO_COMPRESS_RAW(ZIO_COMPRESS_ZSTD, level)
162 
163 #define	ZIO_FAILURE_MODE_WAIT		0
164 #define	ZIO_FAILURE_MODE_CONTINUE	1
165 #define	ZIO_FAILURE_MODE_PANIC		2
166 
167 typedef enum zio_suspend_reason {
168 	ZIO_SUSPEND_NONE = 0,
169 	ZIO_SUSPEND_IOERR,
170 	ZIO_SUSPEND_MMP,
171 } zio_suspend_reason_t;
172 
173 /*
174  * This was originally an enum type. However, those are 32-bit and there is no
175  * way to make a 64-bit enum type. Since we ran out of bits for flags, we were
176  * forced to upgrade it to a uint64_t.
177  *
178  * NOTE: PLEASE UPDATE THE BITFIELD STRINGS IN zfs_valstr.c IF YOU ADD ANOTHER
179  * FLAG.
180  */
181 typedef uint64_t zio_flag_t;
182 	/*
183 	 * Flags inherited by gang, ddt, and vdev children,
184 	 * and that must be equal for two zios to aggregate
185 	 */
186 #define	ZIO_FLAG_DONT_AGGREGATE	(1ULL << 0)
187 #define	ZIO_FLAG_IO_REPAIR	(1ULL << 1)
188 #define	ZIO_FLAG_SELF_HEAL	(1ULL << 2)
189 #define	ZIO_FLAG_RESILVER	(1ULL << 3)
190 #define	ZIO_FLAG_SCRUB		(1ULL << 4)
191 #define	ZIO_FLAG_SCAN_THREAD	(1ULL << 5)
192 #define	ZIO_FLAG_PHYSICAL	(1ULL << 6)
193 
194 #define	ZIO_FLAG_AGG_INHERIT	(ZIO_FLAG_CANFAIL - 1)
195 
196 	/*
197 	 * Flags inherited by ddt, gang, and vdev children.
198 	 */
199 #define	ZIO_FLAG_CANFAIL	(1ULL << 7)	/* must be first for INHERIT */
200 #define	ZIO_FLAG_SPECULATIVE	(1ULL << 8)
201 #define	ZIO_FLAG_CONFIG_WRITER	(1ULL << 9)
202 #define	ZIO_FLAG_DONT_RETRY	(1ULL << 10)
203 #define	ZIO_FLAG_NODATA		(1ULL << 12)
204 #define	ZIO_FLAG_INDUCE_DAMAGE	(1ULL << 13)
205 #define	ZIO_FLAG_ALLOC_THROTTLED	(1ULL << 14)
206 
207 #define	ZIO_FLAG_DDT_INHERIT	(ZIO_FLAG_IO_RETRY - 1)
208 #define	ZIO_FLAG_GANG_INHERIT	(ZIO_FLAG_IO_RETRY - 1)
209 
210 	/*
211 	 * Flags inherited by vdev children.
212 	 */
213 #define	ZIO_FLAG_IO_RETRY	(1ULL << 15)	/* must be first for INHERIT */
214 #define	ZIO_FLAG_PROBE		(1ULL << 16)
215 #define	ZIO_FLAG_TRYHARD	(1ULL << 17)
216 #define	ZIO_FLAG_OPTIONAL	(1ULL << 18)
217 #define	ZIO_FLAG_DIO_READ	(1ULL << 19)
218 #define	ZIO_FLAG_BYPASSED_QUEUE	(1ULL << 20)
219 #define	ZIO_FLAG_VDEV_INHERIT	(ZIO_FLAG_DONT_QUEUE - 1)
220 
221 	/*
222 	 * Flags not inherited by any children.
223 	 */
224 #define	ZIO_FLAG_DONT_QUEUE	(1ULL << 21)	/* must be first for INHERIT */
225 #define	ZIO_FLAG_DONT_PROPAGATE	(1ULL << 22)
226 #define	ZIO_FLAG_IO_BYPASS	(1ULL << 23)
227 #define	ZIO_FLAG_IO_REWRITE	(1ULL << 24)
228 #define	ZIO_FLAG_RAW_COMPRESS	(1ULL << 25)
229 #define	ZIO_FLAG_RAW_ENCRYPT	(1ULL << 26)
230 #define	ZIO_FLAG_GANG_CHILD	(1ULL << 27)
231 #define	ZIO_FLAG_DDT_CHILD	(1ULL << 28)
232 #define	ZIO_FLAG_GODFATHER	(1ULL << 29)
233 #define	ZIO_FLAG_NOPWRITE	(1ULL << 30)
234 #define	ZIO_FLAG_REEXECUTED	(1ULL << 31)
235 #define	ZIO_FLAG_DELEGATED	(1ULL << 32)
236 #define	ZIO_FLAG_PREALLOCATED	(1ULL << 33)
237 #define	ZIO_FLAG_POSTREAD	(1ULL << 34)
238 
239 #define	ZIO_ALLOCATOR_NONE	(-1)
240 #define	ZIO_HAS_ALLOCATOR(zio)	((zio)->io_allocator != ZIO_ALLOCATOR_NONE)
241 
242 #define	ZIO_FLAG_MUSTSUCCEED		0
243 #define	ZIO_FLAG_RAW	(ZIO_FLAG_RAW_COMPRESS | ZIO_FLAG_RAW_ENCRYPT)
244 
245 #define	ZIO_DDT_CHILD_FLAGS(zio)				\
246 	(((zio)->io_flags & ZIO_FLAG_DDT_INHERIT) |		\
247 	ZIO_FLAG_DDT_CHILD | ZIO_FLAG_CANFAIL)
248 
249 #define	ZIO_GANG_CHILD_FLAGS(zio)				\
250 	(((zio)->io_flags & ZIO_FLAG_GANG_INHERIT) |		\
251 	ZIO_FLAG_GANG_CHILD | ZIO_FLAG_CANFAIL)
252 
253 #define	ZIO_VDEV_CHILD_FLAGS(zio)				\
254 	(((zio)->io_flags & ZIO_FLAG_VDEV_INHERIT) |		\
255 	ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_CANFAIL)
256 
257 #define	ZIO_CHILD_BIT(x)		(1U << (x))
258 #define	ZIO_CHILD_BIT_IS_SET(val, x)	((val) & (1U << (x)))
259 
260 enum zio_child {
261 	ZIO_CHILD_VDEV = 0,
262 	ZIO_CHILD_GANG,
263 	ZIO_CHILD_DDT,
264 	ZIO_CHILD_LOGICAL,
265 	ZIO_CHILD_TYPES
266 };
267 
268 #define	ZIO_CHILD_VDEV_BIT		ZIO_CHILD_BIT(ZIO_CHILD_VDEV)
269 #define	ZIO_CHILD_GANG_BIT		ZIO_CHILD_BIT(ZIO_CHILD_GANG)
270 #define	ZIO_CHILD_DDT_BIT		ZIO_CHILD_BIT(ZIO_CHILD_DDT)
271 #define	ZIO_CHILD_LOGICAL_BIT		ZIO_CHILD_BIT(ZIO_CHILD_LOGICAL)
272 #define	ZIO_CHILD_ALL_BITS					\
273 	(ZIO_CHILD_VDEV_BIT | ZIO_CHILD_GANG_BIT |		\
274 	ZIO_CHILD_DDT_BIT | ZIO_CHILD_LOGICAL_BIT)
275 
276 enum zio_wait_type {
277 	ZIO_WAIT_READY = 0,
278 	ZIO_WAIT_DONE,
279 	ZIO_WAIT_TYPES
280 };
281 
282 typedef void zio_done_func_t(zio_t *zio);
283 
284 extern int zio_exclude_metadata;
285 extern int zio_dva_throttle_enabled;
286 extern const char *const zio_type_name[ZIO_TYPES];
287 
288 /*
289  * A bookmark is a four-tuple <objset, object, level, blkid> that uniquely
290  * identifies any block in the pool.  By convention, the meta-objset (MOS)
291  * is objset 0, and the meta-dnode is object 0.  This covers all blocks
292  * except root blocks and ZIL blocks, which are defined as follows:
293  *
294  * Root blocks (objset_phys_t) are object 0, level -1:  <objset, 0, -1, 0>.
295  * ZIL blocks are bookmarked <objset, 0, -2, blkid == ZIL sequence number>.
296  * dmu_sync()ed ZIL data blocks are bookmarked <objset, object, -2, blkid>.
297  * dnode visit bookmarks are <objset, object id of dnode, -3, 0>.
298  *
299  * Note: this structure is called a bookmark because its original purpose
300  * was to remember where to resume a pool-wide traverse.
301  *
302  * Note: this structure is passed between userland and the kernel, and is
303  * stored on disk (by virtue of being incorporated into other on-disk
304  * structures, e.g. dsl_scan_phys_t).
305  *
306  * If the head_errlog feature is enabled a different on-disk format for error
307  * logs is used. This introduces the use of an error bookmark, a four-tuple
308  * <object, level, blkid, birth> that uniquely identifies any error block
309  * in the pool. The birth transaction group is used to track whether the block
310  * has been overwritten by newer data or added to a snapshot since its marking
311  * as an error.
312  */
313 struct zbookmark_phys {
314 	uint64_t	zb_objset;
315 	uint64_t	zb_object;
316 	int64_t		zb_level;
317 	uint64_t	zb_blkid;
318 };
319 
320 struct zbookmark_err_phys {
321 	uint64_t	zb_object;
322 	int64_t		zb_level;
323 	uint64_t	zb_blkid;
324 	uint64_t	zb_birth;
325 };
326 
327 #define	SET_BOOKMARK(zb, objset, object, level, blkid)  \
328 {                                                       \
329 	(zb)->zb_objset = objset;                       \
330 	(zb)->zb_object = object;                       \
331 	(zb)->zb_level = level;                         \
332 	(zb)->zb_blkid = blkid;                         \
333 }
334 
335 #define	ZB_DESTROYED_OBJSET	(-1ULL)
336 
337 #define	ZB_ROOT_OBJECT		(0ULL)
338 #define	ZB_ROOT_LEVEL		(-1LL)
339 #define	ZB_ROOT_BLKID		(0ULL)
340 
341 #define	ZB_ZIL_OBJECT		(0ULL)
342 #define	ZB_ZIL_LEVEL		(-2LL)
343 
344 #define	ZB_DNODE_LEVEL		(-3LL)
345 #define	ZB_DNODE_BLKID		(0ULL)
346 
347 #define	ZB_IS_ZERO(zb)						\
348 	((zb)->zb_objset == 0 && (zb)->zb_object == 0 &&	\
349 	(zb)->zb_level == 0 && (zb)->zb_blkid == 0)
350 #define	ZB_IS_ROOT(zb)				\
351 	((zb)->zb_object == ZB_ROOT_OBJECT &&	\
352 	(zb)->zb_level == ZB_ROOT_LEVEL &&	\
353 	(zb)->zb_blkid == ZB_ROOT_BLKID)
354 
355 typedef struct zio_prop {
356 	enum zio_checksum	zp_checksum:8;
357 	enum zio_compress	zp_compress:8;
358 	uint8_t			zp_complevel;
359 	uint8_t			zp_level;
360 	uint8_t			zp_copies;
361 	uint8_t			zp_gang_copies;
362 	dmu_object_type_t	zp_type:8;
363 	dmu_object_type_t	zp_storage_type:8;
364 	boolean_t		zp_dedup:1;
365 	boolean_t		zp_dedup_verify:1;
366 	boolean_t		zp_nopwrite:1;
367 	boolean_t		zp_brtwrite:1;
368 	boolean_t		zp_encrypt:1;
369 	boolean_t		zp_byteorder:1;
370 	boolean_t		zp_direct_write:1;
371 	boolean_t		zp_rewrite:1;
372 	uint32_t		zp_zpl_smallblk;
373 	uint8_t			zp_salt[ZIO_DATA_SALT_LEN];
374 	uint8_t			zp_iv[ZIO_DATA_IV_LEN];
375 	uint8_t			zp_mac[ZIO_DATA_MAC_LEN];
376 } zio_prop_t;
377 
378 typedef struct zio_cksum_report zio_cksum_report_t;
379 
380 typedef void zio_cksum_finish_f(zio_cksum_report_t *rep,
381     const abd_t *good_data);
382 typedef void zio_cksum_free_f(void *cbdata, size_t size);
383 
384 struct zio_bad_cksum;				/* defined in zio_checksum.h */
385 struct dnode_phys;
386 struct abd;
387 
388 struct zio_cksum_report {
389 	struct zio_cksum_report *zcr_next;
390 	nvlist_t		*zcr_ereport;
391 	nvlist_t		*zcr_detector;
392 	void			*zcr_cbdata;
393 	size_t			zcr_cbinfo;	/* passed to zcr_free() */
394 	uint64_t		zcr_sector;
395 	uint64_t		zcr_align;
396 	uint64_t		zcr_length;
397 	zio_cksum_finish_f	*zcr_finish;
398 	zio_cksum_free_f	*zcr_free;
399 
400 	/* internal use only */
401 	struct zio_bad_cksum	*zcr_ckinfo;	/* information from failure */
402 };
403 
404 typedef struct zio_vsd_ops {
405 	zio_done_func_t		*vsd_free;
406 } zio_vsd_ops_t;
407 
408 typedef struct zio_gang_node {
409 	zio_gbh_phys_t		*gn_gbh;
410 	uint64_t		gn_gangblocksize;
411 	uint64_t		gn_allocsize;
412 	struct zio_gang_node	*gn_child[];
413 } zio_gang_node_t;
414 
415 typedef zio_t *zio_gang_issue_func_t(zio_t *zio, blkptr_t *bp,
416     zio_gang_node_t *gn, struct abd *data, uint64_t offset);
417 
418 typedef void zio_transform_func_t(zio_t *zio, struct abd *data, uint64_t size);
419 
420 typedef struct zio_transform {
421 	struct abd		*zt_orig_abd;
422 	uint64_t		zt_orig_size;
423 	uint64_t		zt_bufsize;
424 	zio_transform_func_t	*zt_transform;
425 	struct zio_transform	*zt_next;
426 } zio_transform_t;
427 
428 typedef zio_t *zio_pipe_stage_t(zio_t *zio);
429 
430 /*
431  * The io_post flags describe additional actions that a parent IO should
432  * consider or perform on behalf of a child. They are distinct from io_flags
433  * because the child must be able to propagate them to the parent. The normal
434  * io_flags are local to the zio, not protected by any lock, and not modifiable
435  * by children; the reexecute flags are protected by io_lock, modifiable by
436  * children, and always propagated -- even when ZIO_FLAG_DONT_PROPAGATE is set.
437  */
438 #define	ZIO_POST_REEXECUTE	(1 << 0)
439 #define	ZIO_POST_SUSPEND	(1 << 1)
440 #define	ZIO_POST_DIO_CHKSUM_ERR	(1 << 2)
441 
442 /*
443  * The io_trim flags are used to specify the type of TRIM to perform.  They
444  * only apply to ZIO_TYPE_TRIM zios are distinct from io_flags.
445  */
446 enum trim_flag {
447 	ZIO_TRIM_SECURE		= 1U << 0,
448 };
449 
450 #ifdef METASLAB_TRACE
451 typedef struct zio_alloc_list {
452 	list_t  zal_list;
453 	uint64_t zal_size;
454 } zio_alloc_list_t;
455 #define	ZIO_ALLOC_LIST(zio)	(&(zio)->io_alloc_list)
456 #else
457 typedef struct zio_alloc_list {
458 	uint8_t	zal_pad;
459 } zio_alloc_list_t;
460 #define	ZIO_ALLOC_LIST(zio)	NULL
461 #endif
462 
463 typedef struct zio_link {
464 	zio_t		*zl_parent;
465 	zio_t		*zl_child;
466 	list_node_t	zl_parent_node;
467 	list_node_t	zl_child_node;
468 } zio_link_t;
469 
470 enum zio_qstate {
471 	ZIO_QS_NONE = 0,
472 	ZIO_QS_QUEUED,
473 	ZIO_QS_ACTIVE,
474 };
475 
476 struct zio {
477 	/* Core information about this I/O */
478 	zbookmark_phys_t	io_bookmark;
479 	zio_prop_t	io_prop;
480 	zio_type_t	io_type;
481 	enum zio_child	io_child_type;
482 	enum trim_flag	io_trim_flags;
483 	zio_priority_t	io_priority;
484 	uint8_t		io_post;
485 	uint8_t		io_state[ZIO_WAIT_TYPES];
486 	uint64_t	io_txg;
487 	spa_t		*io_spa;
488 	blkptr_t	*io_bp;
489 	blkptr_t	*io_bp_override;
490 	blkptr_t	io_bp_copy;
491 	list_t		io_parent_list;
492 	list_t		io_child_list;
493 	zio_t		*io_logical;
494 	zio_transform_t *io_transform_stack;
495 
496 	/* Callback info */
497 	zio_done_func_t	*io_ready;
498 	zio_done_func_t	*io_children_ready;
499 	zio_done_func_t	*io_done;
500 	void		*io_private;
501 	int64_t		io_prev_space_delta;	/* DMU private */
502 	blkptr_t	io_bp_orig;
503 	/* io_lsize != io_orig_size iff this is a raw write */
504 	uint64_t	io_lsize;
505 
506 	/* Data represented by this I/O */
507 	struct abd	*io_abd;
508 	struct abd	*io_orig_abd;
509 	uint64_t	io_size;
510 	uint64_t	io_orig_size;
511 
512 	/* Stuff for the vdev stack */
513 	vdev_t		*io_vd;
514 	void		*io_vsd;
515 	const zio_vsd_ops_t *io_vsd_ops;
516 	metaslab_class_t *io_metaslab_class;	/* dva throttle class */
517 
518 	enum zio_qstate	io_queue_state;	/* vdev queue state */
519 	union {
520 		list_node_t l;
521 		avl_node_t a;
522 	} io_queue_node ____cacheline_aligned;	/* allocator and vdev queues */
523 	avl_node_t	io_offset_node;	/* vdev offset queues */
524 	uint64_t	io_offset;
525 	hrtime_t	io_timestamp;	/* submitted at */
526 	hrtime_t	io_queued_timestamp;
527 	hrtime_t	io_target_timestamp;
528 	hrtime_t	io_delta;	/* vdev queue service delta */
529 	hrtime_t	io_delay;	/* Device access time (disk or */
530 					/* file). */
531 #ifdef METASLAB_TRACE
532 	zio_alloc_list_t 	io_alloc_list;
533 #endif
534 
535 	/* Internal pipeline state */
536 	zio_flag_t	io_flags;
537 	enum zio_stage	io_stage;
538 	enum zio_stage	io_pipeline;
539 	zio_flag_t	io_orig_flags;
540 	enum zio_stage	io_orig_stage;
541 	enum zio_stage	io_orig_pipeline;
542 	enum zio_stage	io_pipeline_trace;
543 	int		io_error;
544 	int		io_child_error[ZIO_CHILD_TYPES];
545 	uint64_t	io_children[ZIO_CHILD_TYPES][ZIO_WAIT_TYPES];
546 	uint64_t	*io_stall;
547 	zio_t		*io_gang_leader;
548 	zio_gang_node_t	*io_gang_tree;
549 	void		*io_executor;
550 	void		*io_waiter;
551 	void		*io_bio;
552 	kmutex_t	io_lock;
553 	kcondvar_t	io_cv;
554 	int		io_allocator;
555 
556 	/* FMA state */
557 	zio_cksum_report_t *io_cksum_report;
558 	uint64_t	io_ena;
559 
560 	/* Taskq dispatching state */
561 	taskq_ent_t	io_tqent;
562 };
563 
564 enum blk_verify_flag {
565 	BLK_VERIFY_ONLY,
566 	BLK_VERIFY_LOG,
567 	BLK_VERIFY_HALT
568 };
569 
570 enum blk_config_flag {
571 	BLK_CONFIG_HELD,   // SCL_VDEV held for writer
572 	BLK_CONFIG_NEEDED, // SCL_VDEV should be obtained for reader
573 	BLK_CONFIG_NEEDED_TRY, // Try with SCL_VDEV for reader
574 	BLK_CONFIG_SKIP,   // skip checks which require SCL_VDEV
575 };
576 
577 extern int zio_bookmark_compare(const void *, const void *);
578 
579 extern zio_t *zio_null(zio_t *pio, spa_t *spa, vdev_t *vd,
580     zio_done_func_t *done, void *priv, zio_flag_t flags);
581 
582 extern zio_t *zio_root(spa_t *spa,
583     zio_done_func_t *done, void *priv, zio_flag_t flags);
584 
585 extern void zio_destroy(zio_t *zio);
586 
587 extern zio_t *zio_read(zio_t *pio, spa_t *spa, const blkptr_t *bp,
588     struct abd *data, uint64_t lsize, zio_done_func_t *done, void *priv,
589     zio_priority_t priority, zio_flag_t flags, const zbookmark_phys_t *zb);
590 
591 extern zio_t *zio_write(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
592     struct abd *data, uint64_t size, uint64_t psize, const zio_prop_t *zp,
593     zio_done_func_t *ready, zio_done_func_t *children_ready,
594     zio_done_func_t *done, void *priv, zio_priority_t priority,
595     zio_flag_t flags, const zbookmark_phys_t *zb);
596 
597 extern zio_t *zio_rewrite(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
598     struct abd *data, uint64_t size, zio_done_func_t *done, void *priv,
599     zio_priority_t priority, zio_flag_t flags, zbookmark_phys_t *zb);
600 
601 extern void zio_write_override(zio_t *zio, blkptr_t *bp, int copies,
602     int gang_copies, boolean_t nopwrite, boolean_t brtwrite);
603 
604 extern void zio_free(spa_t *spa, uint64_t txg, const blkptr_t *bp);
605 
606 extern zio_t *zio_claim(zio_t *pio, spa_t *spa, uint64_t txg,
607     const blkptr_t *bp,
608     zio_done_func_t *done, void *priv, zio_flag_t flags);
609 
610 extern zio_t *zio_trim(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
611     zio_done_func_t *done, void *priv, zio_priority_t priority,
612     zio_flag_t flags, enum trim_flag trim_flags);
613 
614 extern zio_t *zio_read_phys(zio_t *pio, vdev_t *vd, uint64_t offset,
615     uint64_t size, struct abd *data, int checksum,
616     zio_done_func_t *done, void *priv, zio_priority_t priority,
617     zio_flag_t flags, boolean_t labels);
618 
619 extern zio_t *zio_write_phys(zio_t *pio, vdev_t *vd, uint64_t offset,
620     uint64_t size, struct abd *data, int checksum,
621     zio_done_func_t *done, void *priv, zio_priority_t priority,
622     zio_flag_t flags, boolean_t labels);
623 
624 extern zio_t *zio_free_sync(zio_t *pio, spa_t *spa, uint64_t txg,
625     const blkptr_t *bp, zio_flag_t flags);
626 
627 extern int zio_alloc_zil(spa_t *spa, objset_t *os, uint64_t txg,
628     blkptr_t *new_bp, uint64_t min_size, uint64_t max_size, boolean_t *slog,
629     boolean_t allow_larger);
630 extern void zio_flush(zio_t *zio, vdev_t *vd);
631 extern void zio_shrink(zio_t *zio, uint64_t size);
632 
633 extern size_t zio_get_compression_max_size(enum zio_compress compress,
634     uint64_t gcd_alloc, uint64_t min_alloc, size_t s_len);
635 extern int zio_wait(zio_t *zio);
636 extern void zio_nowait(zio_t *zio);
637 extern void zio_execute(void *zio);
638 extern void zio_interrupt(void *zio);
639 extern void zio_delay_init(zio_t *zio);
640 extern void zio_delay_interrupt(zio_t *zio);
641 extern void zio_deadman(zio_t *zio, const char *tag);
642 
643 extern zio_t *zio_walk_parents(zio_t *cio, zio_link_t **);
644 extern zio_t *zio_walk_children(zio_t *pio, zio_link_t **);
645 extern zio_t *zio_unique_parent(zio_t *cio);
646 extern void zio_add_child(zio_t *pio, zio_t *cio);
647 
648 extern void *zio_buf_alloc(size_t size);
649 extern void zio_buf_free(void *buf, size_t size);
650 extern void *zio_data_buf_alloc(size_t size);
651 extern void zio_data_buf_free(void *buf, size_t size);
652 
653 extern void zio_push_transform(zio_t *zio, struct abd *abd, uint64_t size,
654     uint64_t bufsize, zio_transform_func_t *transform);
655 extern void zio_pop_transforms(zio_t *zio);
656 
657 extern void zio_resubmit_stage_async(void *);
658 
659 extern zio_t *zio_vdev_child_io(zio_t *zio, blkptr_t *bp, vdev_t *vd,
660     uint64_t offset, struct abd *data, uint64_t size, int type,
661     zio_priority_t priority, zio_flag_t flags,
662     zio_done_func_t *done, void *priv);
663 
664 extern zio_t *zio_vdev_delegated_io(vdev_t *vd, uint64_t offset,
665     struct abd *data, uint64_t size, zio_type_t type, zio_priority_t priority,
666     zio_flag_t flags, zio_done_func_t *done, void *priv);
667 
668 extern void zio_vdev_io_bypass(zio_t *zio);
669 extern void zio_vdev_io_reissue(zio_t *zio);
670 extern void zio_vdev_io_redone(zio_t *zio);
671 
672 extern void zio_change_priority(zio_t *pio, zio_priority_t priority);
673 
674 extern void zio_checksum_verified(zio_t *zio);
675 extern void zio_dio_chksum_verify_error_report(zio_t *zio);
676 extern int zio_worst_error(int e1, int e2);
677 
678 extern enum zio_checksum zio_checksum_select(enum zio_checksum child,
679     enum zio_checksum parent);
680 extern enum zio_checksum zio_checksum_dedup_select(spa_t *spa,
681     enum zio_checksum child, enum zio_checksum parent);
682 extern enum zio_compress zio_compress_select(spa_t *spa,
683     enum zio_compress child, enum zio_compress parent);
684 extern uint8_t zio_complevel_select(spa_t *spa, enum zio_compress compress,
685     uint8_t child, uint8_t parent);
686 
687 extern void zio_suspend(spa_t *spa, zio_t *zio, zio_suspend_reason_t);
688 extern int zio_resume(spa_t *spa);
689 extern void zio_resume_wait(spa_t *spa);
690 
691 extern int zfs_blkptr_verify(spa_t *spa, const blkptr_t *bp,
692     enum blk_config_flag blk_config, enum blk_verify_flag blk_verify);
693 
694 /*
695  * Initial setup and teardown.
696  */
697 extern void zio_init(void);
698 extern void zio_fini(void);
699 
700 /*
701  * Fault injection
702  */
703 struct zinject_record;
704 extern uint32_t zio_injection_enabled;
705 extern int zio_inject_fault(char *name, int flags, int *id,
706     struct zinject_record *record);
707 extern int zio_inject_list_next(int *id, char *name, size_t buflen,
708     struct zinject_record *record);
709 extern int zio_clear_fault(int id);
710 extern void zio_handle_panic_injection(spa_t *spa, const char *tag,
711     uint64_t type);
712 extern int zio_handle_decrypt_injection(spa_t *spa, const zbookmark_phys_t *zb,
713     uint64_t type, int error);
714 extern int zio_handle_fault_injection(zio_t *zio, int error);
715 extern int zio_handle_device_injection(vdev_t *vd, zio_t *zio, int error);
716 extern int zio_handle_device_injections(vdev_t *vd, zio_t *zio, int err1,
717     int err2);
718 extern int zio_handle_label_injection(zio_t *zio, int error);
719 extern void zio_handle_ignored_writes(zio_t *zio);
720 extern hrtime_t zio_handle_io_delay(zio_t *zio);
721 extern void zio_handle_import_delay(spa_t *spa, hrtime_t elapsed);
722 extern void zio_handle_export_delay(spa_t *spa, hrtime_t elapsed);
723 extern hrtime_t zio_handle_ready_delay(zio_t *zio);
724 
725 /*
726  * Checksum ereport functions
727  */
728 extern int zfs_ereport_start_checksum(spa_t *spa, vdev_t *vd,
729     const zbookmark_phys_t *zb, struct zio *zio, uint64_t offset,
730     uint64_t length, struct zio_bad_cksum *info);
731 extern void zfs_ereport_finish_checksum(zio_cksum_report_t *report,
732     const abd_t *good_data, const abd_t *bad_data, boolean_t drop_if_identical);
733 
734 extern void zfs_ereport_free_checksum(zio_cksum_report_t *report);
735 
736 /* If we have the good data in hand, this function can be used */
737 extern int zfs_ereport_post_checksum(spa_t *spa, vdev_t *vd,
738     const zbookmark_phys_t *zb, struct zio *zio, uint64_t offset,
739     uint64_t length, const abd_t *good_data, const abd_t *bad_data,
740     struct zio_bad_cksum *info);
741 
742 void zio_vsd_default_cksum_report(zio_t *zio, zio_cksum_report_t *zcr);
743 extern void zfs_ereport_snapshot_post(const char *subclass, spa_t *spa,
744     const char *name);
745 
746 /* Called from spa_sync(), but primarily an injection handler */
747 extern void spa_handle_ignored_writes(spa_t *spa);
748 
749 /* zbookmark_phys functions */
750 boolean_t zbookmark_subtree_completed(const struct dnode_phys *dnp,
751     const zbookmark_phys_t *subtree_root, const zbookmark_phys_t *last_block);
752 boolean_t zbookmark_subtree_tbd(const struct dnode_phys *dnp,
753     const zbookmark_phys_t *subtree_root, const zbookmark_phys_t *last_block);
754 int zbookmark_compare(uint16_t dbss1, uint8_t ibs1, uint16_t dbss2,
755     uint8_t ibs2, const zbookmark_phys_t *zb1, const zbookmark_phys_t *zb2);
756 
757 #ifdef	__cplusplus
758 }
759 #endif
760 
761 #endif	/* _ZIO_H */
762