xref: /illumos-gate/usr/src/uts/common/fs/zfs/sys/zio.h (revision 468c413a79615e77179e8d98f22a7e513a8135bd)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef _ZIO_H
28 #define	_ZIO_H
29 
30 #include <sys/zfs_context.h>
31 #include <sys/spa.h>
32 #include <sys/txg.h>
33 #include <sys/avl.h>
34 #include <sys/fs/zfs.h>
35 #include <sys/zio_impl.h>
36 
37 #ifdef	__cplusplus
38 extern "C" {
39 #endif
40 
41 #define	ZBT_MAGIC	0x210da7ab10c7a11ULL	/* zio data bloc tail */
42 
43 typedef struct zio_block_tail {
44 	uint64_t	zbt_magic;	/* for validation, endianness	*/
45 	zio_cksum_t	zbt_cksum;	/* 256-bit checksum		*/
46 } zio_block_tail_t;
47 
48 /*
49  * Gang block headers are self-checksumming and contain an array
50  * of block pointers.
51  */
52 #define	SPA_GANGBLOCKSIZE	SPA_MINBLOCKSIZE
53 #define	SPA_GBH_NBLKPTRS	((SPA_GANGBLOCKSIZE - \
54 	sizeof (zio_block_tail_t)) / sizeof (blkptr_t))
55 #define	SPA_GBH_FILLER		((SPA_GANGBLOCKSIZE - \
56 	sizeof (zio_block_tail_t) - \
57 	(SPA_GBH_NBLKPTRS * sizeof (blkptr_t))) /\
58 	sizeof (uint64_t))
59 
60 typedef struct zio_gbh {
61 	blkptr_t		zg_blkptr[SPA_GBH_NBLKPTRS];
62 	uint64_t		zg_filler[SPA_GBH_FILLER];
63 	zio_block_tail_t	zg_tail;
64 } zio_gbh_phys_t;
65 
66 enum zio_checksum {
67 	ZIO_CHECKSUM_INHERIT = 0,
68 	ZIO_CHECKSUM_ON,
69 	ZIO_CHECKSUM_OFF,
70 	ZIO_CHECKSUM_LABEL,
71 	ZIO_CHECKSUM_GANG_HEADER,
72 	ZIO_CHECKSUM_ZILOG,
73 	ZIO_CHECKSUM_FLETCHER_2,
74 	ZIO_CHECKSUM_FLETCHER_4,
75 	ZIO_CHECKSUM_SHA256,
76 	ZIO_CHECKSUM_FUNCTIONS
77 };
78 
79 #define	ZIO_CHECKSUM_ON_VALUE	ZIO_CHECKSUM_FLETCHER_4
80 #define	ZIO_CHECKSUM_DEFAULT	ZIO_CHECKSUM_ON
81 
82 enum zio_compress {
83 	ZIO_COMPRESS_INHERIT = 0,
84 	ZIO_COMPRESS_ON,
85 	ZIO_COMPRESS_OFF,
86 	ZIO_COMPRESS_LZJB,
87 	ZIO_COMPRESS_EMPTY,
88 	ZIO_COMPRESS_GZIP_1,
89 	ZIO_COMPRESS_GZIP_2,
90 	ZIO_COMPRESS_GZIP_3,
91 	ZIO_COMPRESS_GZIP_4,
92 	ZIO_COMPRESS_GZIP_5,
93 	ZIO_COMPRESS_GZIP_6,
94 	ZIO_COMPRESS_GZIP_7,
95 	ZIO_COMPRESS_GZIP_8,
96 	ZIO_COMPRESS_GZIP_9,
97 	ZIO_COMPRESS_FUNCTIONS
98 };
99 
100 #define	ZIO_COMPRESS_ON_VALUE	ZIO_COMPRESS_LZJB
101 #define	ZIO_COMPRESS_DEFAULT	ZIO_COMPRESS_OFF
102 
103 #define	ZIO_FAILURE_MODE_WAIT		0
104 #define	ZIO_FAILURE_MODE_CONTINUE	1
105 #define	ZIO_FAILURE_MODE_PANIC		2
106 
107 #define	ZIO_PRIORITY_NOW		(zio_priority_table[0])
108 #define	ZIO_PRIORITY_SYNC_READ		(zio_priority_table[1])
109 #define	ZIO_PRIORITY_SYNC_WRITE		(zio_priority_table[2])
110 #define	ZIO_PRIORITY_ASYNC_READ		(zio_priority_table[3])
111 #define	ZIO_PRIORITY_ASYNC_WRITE	(zio_priority_table[4])
112 #define	ZIO_PRIORITY_FREE		(zio_priority_table[5])
113 #define	ZIO_PRIORITY_CACHE_FILL		(zio_priority_table[6])
114 #define	ZIO_PRIORITY_LOG_WRITE		(zio_priority_table[7])
115 #define	ZIO_PRIORITY_RESILVER		(zio_priority_table[8])
116 #define	ZIO_PRIORITY_SCRUB		(zio_priority_table[9])
117 #define	ZIO_PRIORITY_TABLE_SIZE		10
118 
119 #define	ZIO_FLAG_MUSTSUCCEED		0x000000
120 #define	ZIO_FLAG_CANFAIL		0x000001
121 #define	ZIO_FLAG_SPECULATIVE		0x000002
122 #define	ZIO_FLAG_CONFIG_WRITER		0x000004
123 #define	ZIO_FLAG_DONT_RETRY		0x000008
124 
125 #define	ZIO_FLAG_DONT_CACHE		0x000010
126 #define	ZIO_FLAG_DONT_QUEUE		0x000020
127 #define	ZIO_FLAG_DONT_AGGREGATE		0x000040
128 #define	ZIO_FLAG_DONT_PROPAGATE		0x000080
129 
130 #define	ZIO_FLAG_IO_BYPASS		0x000100
131 #define	ZIO_FLAG_IO_REPAIR		0x000200
132 #define	ZIO_FLAG_IO_RETRY		0x000400
133 #define	ZIO_FLAG_IO_REWRITE		0x000800
134 
135 #define	ZIO_FLAG_SELF_HEAL		0x001000
136 #define	ZIO_FLAG_RESILVER		0x002000
137 #define	ZIO_FLAG_SCRUB			0x004000
138 #define	ZIO_FLAG_SCRUB_THREAD		0x008000
139 
140 #define	ZIO_FLAG_PROBE			0x010000
141 #define	ZIO_FLAG_GANG_CHILD		0x020000
142 #define	ZIO_FLAG_RAW			0x040000
143 #define	ZIO_FLAG_GODFATHER		0x080000
144 
145 #define	ZIO_FLAG_TRYHARD		0x100000
146 #define	ZIO_FLAG_NODATA			0x200000
147 #define	ZIO_FLAG_OPTIONAL		0x400000
148 
149 #define	ZIO_FLAG_GANG_INHERIT		\
150 	(ZIO_FLAG_CANFAIL |		\
151 	ZIO_FLAG_SPECULATIVE |		\
152 	ZIO_FLAG_CONFIG_WRITER |	\
153 	ZIO_FLAG_DONT_RETRY |		\
154 	ZIO_FLAG_DONT_CACHE |		\
155 	ZIO_FLAG_DONT_AGGREGATE |	\
156 	ZIO_FLAG_SELF_HEAL |		\
157 	ZIO_FLAG_RESILVER |		\
158 	ZIO_FLAG_SCRUB |		\
159 	ZIO_FLAG_SCRUB_THREAD)
160 
161 #define	ZIO_FLAG_VDEV_INHERIT		\
162 	(ZIO_FLAG_GANG_INHERIT |	\
163 	ZIO_FLAG_IO_REPAIR |		\
164 	ZIO_FLAG_IO_RETRY |		\
165 	ZIO_FLAG_PROBE |		\
166 	ZIO_FLAG_TRYHARD |		\
167 	ZIO_FLAG_NODATA |		\
168 	ZIO_FLAG_OPTIONAL)
169 
170 #define	ZIO_FLAG_AGG_INHERIT		\
171 	(ZIO_FLAG_DONT_AGGREGATE |	\
172 	ZIO_FLAG_IO_REPAIR |		\
173 	ZIO_FLAG_SELF_HEAL |		\
174 	ZIO_FLAG_RESILVER |		\
175 	ZIO_FLAG_SCRUB |		\
176 	ZIO_FLAG_SCRUB_THREAD)
177 
178 #define	ZIO_PIPELINE_CONTINUE		0x100
179 #define	ZIO_PIPELINE_STOP		0x101
180 
181 #define	ZIO_GANG_CHILD_FLAGS(zio)				\
182 	(((zio)->io_flags & ZIO_FLAG_GANG_INHERIT) |		\
183 	ZIO_FLAG_GANG_CHILD | ZIO_FLAG_CANFAIL)
184 
185 enum zio_child {
186 	ZIO_CHILD_VDEV = 0,
187 	ZIO_CHILD_GANG,
188 	ZIO_CHILD_LOGICAL,
189 	ZIO_CHILD_TYPES
190 };
191 
192 enum zio_wait_type {
193 	ZIO_WAIT_READY = 0,
194 	ZIO_WAIT_DONE,
195 	ZIO_WAIT_TYPES
196 };
197 
198 /*
199  * We'll take the unused errnos, 'EBADE' and 'EBADR' (from the Convergent
200  * graveyard) to indicate checksum errors and fragmentation.
201  */
202 #define	ECKSUM	EBADE
203 #define	EFRAGS	EBADR
204 
205 typedef struct zio zio_t;
206 typedef void zio_done_func_t(zio_t *zio);
207 
208 extern uint8_t zio_priority_table[ZIO_PRIORITY_TABLE_SIZE];
209 extern char *zio_type_name[ZIO_TYPES];
210 
211 /*
212  * A bookmark is a four-tuple <objset, object, level, blkid> that uniquely
213  * identifies any block in the pool.  By convention, the meta-objset (MOS)
214  * is objset 0, the meta-dnode is object 0, the root block (osphys_t) is
215  * level -1 of the meta-dnode, and intent log blocks (which are chained
216  * off the root block) have blkid == sequence number.  In summary:
217  *
218  *	mos is objset 0
219  *	meta-dnode is object 0
220  *	root block is <objset, 0, -1, 0>
221  *	intent log is <objset, 0, -1, ZIL sequence number>
222  *
223  * Note: this structure is called a bookmark because its first purpose was
224  * to remember where to resume a pool-wide traverse.  The absolute ordering
225  * for block visitation during traversal is defined in compare_bookmark().
226  *
227  * Note: this structure is passed between userland and the kernel.
228  * Therefore it must not change size or alignment between 32/64 bit
229  * compilation options.
230  */
231 typedef struct zbookmark {
232 	uint64_t	zb_objset;
233 	uint64_t	zb_object;
234 	int64_t		zb_level;
235 	uint64_t	zb_blkid;
236 } zbookmark_t;
237 
238 typedef struct zio_prop {
239 	enum zio_checksum	zp_checksum;
240 	enum zio_compress	zp_compress;
241 	dmu_object_type_t	zp_type;
242 	uint8_t			zp_level;
243 	uint8_t			zp_ndvas;
244 } zio_prop_t;
245 
246 typedef struct zio_cksum_report zio_cksum_report_t;
247 
248 typedef void zio_cksum_finish_f(zio_cksum_report_t *rep,
249     const void *good_data);
250 typedef void zio_cksum_free_f(void *cbdata, size_t size);
251 
252 struct zio_bad_cksum;				/* defined in zio_checksum.h */
253 
254 struct zio_cksum_report {
255 	struct zio_cksum_report *zcr_next;
256 	nvlist_t		*zcr_ereport;
257 	nvlist_t		*zcr_detector;
258 
259 	void			*zcr_cbdata;
260 	size_t			zcr_cbinfo;	/* passed to zcr_free() */
261 	uint64_t		zcr_length;
262 	zio_cksum_finish_f	*zcr_finish;
263 	zio_cksum_free_f	*zcr_free;
264 
265 	/* internal use only */
266 	struct zio_bad_cksum	*zcr_ckinfo;	/* information from failure */
267 };
268 
269 typedef void zio_vsd_cksum_report_f(zio_t *zio, zio_cksum_report_t *zcr,
270     void *arg);
271 
272 zio_vsd_cksum_report_f	zio_vsd_default_cksum_report;
273 
274 typedef struct zio_vsd_ops {
275 	zio_done_func_t		*vsd_free;
276 	zio_vsd_cksum_report_f	*vsd_cksum_report;
277 } zio_vsd_ops_t;
278 
279 typedef struct zio_gang_node {
280 	zio_gbh_phys_t		*gn_gbh;
281 	struct zio_gang_node	*gn_child[SPA_GBH_NBLKPTRS];
282 } zio_gang_node_t;
283 
284 typedef zio_t *zio_gang_issue_func_t(zio_t *zio, blkptr_t *bp,
285     zio_gang_node_t *gn, void *data);
286 
287 typedef void zio_transform_func_t(zio_t *zio, void *data, uint64_t size);
288 
289 typedef struct zio_transform {
290 	void			*zt_orig_data;
291 	uint64_t		zt_orig_size;
292 	uint64_t		zt_bufsize;
293 	zio_transform_func_t	*zt_transform;
294 	struct zio_transform	*zt_next;
295 } zio_transform_t;
296 
297 typedef int zio_pipe_stage_t(zio_t *zio);
298 
299 /*
300  * The io_reexecute flags are distinct from io_flags because the child must
301  * be able to propagate them to the parent.  The normal io_flags are local
302  * to the zio, not protected by any lock, and not modifiable by children;
303  * the reexecute flags are protected by io_lock, modifiable by children,
304  * and always propagated -- even when ZIO_FLAG_DONT_PROPAGATE is set.
305  */
306 #define	ZIO_REEXECUTE_NOW	0x01
307 #define	ZIO_REEXECUTE_SUSPEND	0x02
308 
309 typedef struct zio_link {
310 	zio_t		*zl_parent;
311 	zio_t		*zl_child;
312 	list_node_t	zl_parent_node;
313 	list_node_t	zl_child_node;
314 } zio_link_t;
315 
316 struct zio {
317 	/* Core information about this I/O */
318 	zbookmark_t	io_bookmark;
319 	zio_prop_t	io_prop;
320 	zio_type_t	io_type;
321 	enum zio_child	io_child_type;
322 	int		io_cmd;
323 	uint8_t		io_priority;
324 	uint8_t		io_reexecute;
325 	uint8_t		io_state[ZIO_WAIT_TYPES];
326 	uint64_t	io_txg;
327 	spa_t		*io_spa;
328 	blkptr_t	*io_bp;
329 	blkptr_t	io_bp_copy;
330 	list_t		io_parent_list;
331 	list_t		io_child_list;
332 	zio_link_t	*io_walk_link;
333 	zio_t		*io_logical;
334 	zio_transform_t *io_transform_stack;
335 
336 	/* Callback info */
337 	zio_done_func_t	*io_ready;
338 	zio_done_func_t	*io_done;
339 	void		*io_private;
340 	blkptr_t	io_bp_orig;
341 
342 	/* Data represented by this I/O */
343 	void		*io_data;
344 	uint64_t	io_size;
345 
346 	/* Stuff for the vdev stack */
347 	vdev_t		*io_vd;
348 	void		*io_vsd;
349 	const zio_vsd_ops_t *io_vsd_ops;
350 
351 	uint64_t	io_offset;
352 	uint64_t	io_deadline;
353 	avl_node_t	io_offset_node;
354 	avl_node_t	io_deadline_node;
355 	avl_tree_t	*io_vdev_tree;
356 
357 	/* Internal pipeline state */
358 	int		io_flags;
359 	zio_stage_t	io_stage;
360 	uint32_t	io_pipeline;
361 	int		io_orig_flags;
362 	zio_stage_t	io_orig_stage;
363 	uint32_t	io_orig_pipeline;
364 	int		io_error;
365 	int		io_child_error[ZIO_CHILD_TYPES];
366 	uint64_t	io_children[ZIO_CHILD_TYPES][ZIO_WAIT_TYPES];
367 	uint64_t	*io_stall;
368 	zio_t		*io_gang_leader;
369 	zio_gang_node_t	*io_gang_tree;
370 	void		*io_executor;
371 	void		*io_waiter;
372 	kmutex_t	io_lock;
373 	kcondvar_t	io_cv;
374 
375 	/* FMA state */
376 	zio_cksum_report_t *io_cksum_report;
377 	uint64_t	io_ena;
378 };
379 
380 extern zio_t *zio_null(zio_t *pio, spa_t *spa, vdev_t *vd,
381     zio_done_func_t *done, void *private, int flags);
382 
383 extern zio_t *zio_root(spa_t *spa,
384     zio_done_func_t *done, void *private, int flags);
385 
386 extern zio_t *zio_read(zio_t *pio, spa_t *spa, const blkptr_t *bp, void *data,
387     uint64_t size, zio_done_func_t *done, void *private,
388     int priority, int flags, const zbookmark_t *zb);
389 
390 extern zio_t *zio_write(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
391     void *data, uint64_t size, zio_prop_t *zp,
392     zio_done_func_t *ready, zio_done_func_t *done, void *private,
393     int priority, int flags, const zbookmark_t *zb);
394 
395 extern zio_t *zio_rewrite(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
396     void *data, uint64_t size, zio_done_func_t *done, void *private,
397     int priority, int flags, zbookmark_t *zb);
398 
399 extern void zio_skip_write(zio_t *zio);
400 
401 extern zio_t *zio_free(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
402     zio_done_func_t *done, void *private, int flags);
403 
404 extern zio_t *zio_claim(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
405     zio_done_func_t *done, void *private, int flags);
406 
407 extern zio_t *zio_ioctl(zio_t *pio, spa_t *spa, vdev_t *vd, int cmd,
408     zio_done_func_t *done, void *private, int priority, int flags);
409 
410 extern zio_t *zio_read_phys(zio_t *pio, vdev_t *vd, uint64_t offset,
411     uint64_t size, void *data, int checksum,
412     zio_done_func_t *done, void *private, int priority, int flags,
413     boolean_t labels);
414 
415 extern zio_t *zio_write_phys(zio_t *pio, vdev_t *vd, uint64_t offset,
416     uint64_t size, void *data, int checksum,
417     zio_done_func_t *done, void *private, int priority, int flags,
418     boolean_t labels);
419 
420 extern int zio_alloc_blk(spa_t *spa, uint64_t size, blkptr_t *new_bp,
421     blkptr_t *old_bp, uint64_t txg, boolean_t use_slog);
422 extern void zio_free_blk(spa_t *spa, blkptr_t *bp, uint64_t txg);
423 extern void zio_flush(zio_t *zio, vdev_t *vd);
424 
425 extern int zio_wait(zio_t *zio);
426 extern void zio_nowait(zio_t *zio);
427 extern void zio_execute(zio_t *zio);
428 extern void zio_interrupt(zio_t *zio);
429 
430 extern zio_t *zio_walk_parents(zio_t *cio);
431 extern zio_t *zio_walk_children(zio_t *pio);
432 extern zio_t *zio_unique_parent(zio_t *cio);
433 extern void zio_add_child(zio_t *pio, zio_t *cio);
434 
435 extern void *zio_buf_alloc(size_t size);
436 extern void zio_buf_free(void *buf, size_t size);
437 extern void *zio_data_buf_alloc(size_t size);
438 extern void zio_data_buf_free(void *buf, size_t size);
439 
440 extern void zio_resubmit_stage_async(void *);
441 
442 extern zio_t *zio_vdev_child_io(zio_t *zio, blkptr_t *bp, vdev_t *vd,
443     uint64_t offset, void *data, uint64_t size, int type, int priority,
444     int flags, zio_done_func_t *done, void *private);
445 
446 extern zio_t *zio_vdev_delegated_io(vdev_t *vd, uint64_t offset,
447     void *data, uint64_t size, int type, int priority,
448     int flags, zio_done_func_t *done, void *private);
449 
450 extern void zio_vdev_io_bypass(zio_t *zio);
451 extern void zio_vdev_io_reissue(zio_t *zio);
452 extern void zio_vdev_io_redone(zio_t *zio);
453 
454 extern void zio_checksum_verified(zio_t *zio);
455 extern int zio_worst_error(int e1, int e2);
456 
457 extern uint8_t zio_checksum_select(uint8_t child, uint8_t parent);
458 extern uint8_t zio_compress_select(uint8_t child, uint8_t parent);
459 
460 extern void zio_suspend(spa_t *spa, zio_t *zio);
461 extern int zio_resume(spa_t *spa);
462 extern void zio_resume_wait(spa_t *spa);
463 
464 /*
465  * Initial setup and teardown.
466  */
467 extern void zio_init(void);
468 extern void zio_fini(void);
469 
470 /*
471  * Fault injection
472  */
473 struct zinject_record;
474 extern uint32_t zio_injection_enabled;
475 extern int zio_inject_fault(char *name, int flags, int *id,
476     struct zinject_record *record);
477 extern int zio_inject_list_next(int *id, char *name, size_t buflen,
478     struct zinject_record *record);
479 extern int zio_clear_fault(int id);
480 extern void zio_handle_panic_injection(spa_t *spa, char *tag);
481 extern int zio_handle_fault_injection(zio_t *zio, int error);
482 extern int zio_handle_device_injection(vdev_t *vd, zio_t *zio, int error);
483 extern int zio_handle_label_injection(zio_t *zio, int error);
484 extern void zio_handle_ignored_writes(zio_t *zio);
485 
486 /*
487  * Checksum ereport functions
488  */
489 extern void zfs_ereport_start_checksum(spa_t *spa, vdev_t *vd, struct zio *zio,
490     uint64_t offset, uint64_t length, void *arg, struct zio_bad_cksum *info);
491 extern void zfs_ereport_finish_checksum(zio_cksum_report_t *report,
492     const void *good_data, const void *bad_data, boolean_t drop_if_identical);
493 
494 extern void zfs_ereport_send_interim_checksum(zio_cksum_report_t *report);
495 extern void zfs_ereport_free_checksum(zio_cksum_report_t *report);
496 
497 /* If we have the good data in hand, this function can be used */
498 extern void zfs_ereport_post_checksum(spa_t *spa, vdev_t *vd,
499     struct zio *zio, uint64_t offset, uint64_t length,
500     const void *good_data, const void *bad_data, struct zio_bad_cksum *info);
501 
502 /* Called from spa_sync(), but primarily an injection handler */
503 extern void spa_handle_ignored_writes(spa_t *spa);
504 
505 #ifdef	__cplusplus
506 }
507 #endif
508 
509 #endif	/* _ZIO_H */
510