xref: /freebsd/sys/contrib/openzfs/include/sys/zil.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  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
14  * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
15  */
16 
17 /* Portions Copyright 2010 Robert Milkowski */
18 
19 #ifndef	_SYS_ZIL_H
20 #define	_SYS_ZIL_H
21 
22 #include <sys/types.h>
23 #include <sys/spa.h>
24 #include <sys/zio.h>
25 #include <sys/dmu.h>
26 #include <sys/zio_crypt.h>
27 #include <sys/wmsum.h>
28 
29 #ifdef	__cplusplus
30 extern "C" {
31 #endif
32 
33 struct dsl_pool;
34 struct dsl_dataset;
35 struct lwb;
36 
37 /*
38  * Intent log format:
39  *
40  * Each objset has its own intent log.  The log header (zil_header_t)
41  * for objset N's intent log is kept in the Nth object of the SPA's
42  * intent_log objset.  The log header points to a chain of log blocks,
43  * each of which contains log records (i.e., transactions) followed by
44  * a log block trailer (zil_trailer_t).  The format of a log record
45  * depends on the record (or transaction) type, but all records begin
46  * with a common structure that defines the type, length, and txg.
47  */
48 
49 /*
50  * Intent log header - this on disk structure holds fields to manage
51  * the log.  All fields are 64 bit to easily handle cross architectures.
52  */
53 typedef struct zil_header {
54 	uint64_t zh_claim_txg;	/* txg in which log blocks were claimed */
55 	uint64_t zh_replay_seq;	/* highest replayed sequence number */
56 	blkptr_t zh_log;	/* log chain */
57 	uint64_t zh_claim_blk_seq; /* highest claimed block sequence number */
58 	uint64_t zh_flags;	/* header flags */
59 	uint64_t zh_claim_lr_seq; /* highest claimed lr sequence number */
60 	uint64_t zh_pad[3];
61 } zil_header_t;
62 
63 /*
64  * zh_flags bit settings
65  */
66 #define	ZIL_REPLAY_NEEDED	0x1	/* replay needed - internal only */
67 #define	ZIL_CLAIM_LR_SEQ_VALID	0x2	/* zh_claim_lr_seq field is valid */
68 
69 /*
70  * Log block chaining.
71  *
72  * Log blocks are chained together. Originally they were chained at the
73  * end of the block. For performance reasons the chain was moved to the
74  * beginning of the block which allows writes for only the data being used.
75  * The older position is supported for backwards compatibility.
76  *
77  * The zio_eck_t contains a zec_cksum which for the intent log is
78  * the sequence number of this log block. A seq of 0 is invalid.
79  * The zec_cksum is checked by the SPA against the sequence
80  * number passed in the blk_cksum field of the blkptr_t
81  */
82 typedef struct zil_chain {
83 	uint64_t zc_pad;
84 	blkptr_t zc_next_blk;	/* next block in chain */
85 	uint64_t zc_nused;	/* bytes in log block used */
86 	zio_eck_t zc_eck;	/* block trailer */
87 } zil_chain_t;
88 
89 #define	ZIL_MIN_BLKSZ	4096ULL
90 
91 /*
92  * ziltest is by and large an ugly hack, but very useful in
93  * checking replay without tedious work.
94  * When running ziltest we want to keep all itx's and so maintain
95  * a single list in the zl_itxg[] that uses a high txg: ZILTEST_TXG
96  * We subtract TXG_CONCURRENT_STATES to allow for common code.
97  */
98 #define	ZILTEST_TXG (UINT64_MAX - TXG_CONCURRENT_STATES)
99 
100 /*
101  * The words of a log block checksum.
102  */
103 #define	ZIL_ZC_GUID_0	0
104 #define	ZIL_ZC_GUID_1	1
105 #define	ZIL_ZC_OBJSET	2
106 #define	ZIL_ZC_SEQ	3
107 
108 typedef enum zil_create {
109 	Z_FILE,
110 	Z_DIR,
111 	Z_XATTRDIR,
112 } zil_create_t;
113 
114 /*
115  * size of xvattr log section.
116  * its composed of lr_attr_t + xvattr bitmap + 2 64 bit timestamps
117  * for create time and a single 64 bit integer for all of the attributes,
118  * and 4 64 bit integers (32 bytes) for the scanstamp.
119  *
120  */
121 
122 #define	ZIL_XVAT_SIZE(mapsize) \
123 	sizeof (lr_attr_t) + (sizeof (uint32_t) * (mapsize - 1)) + \
124 	(sizeof (uint64_t) * 7)
125 
126 /*
127  * Size of ACL in log.  The ACE data is padded out to properly align
128  * on 8 byte boundary.
129  */
130 
131 #define	ZIL_ACE_LENGTH(x)	(roundup(x, sizeof (uint64_t)))
132 
133 /*
134  * Intent log transaction types and record structures
135  */
136 #define	TX_COMMIT		0	/* Commit marker (no on-disk state) */
137 #define	TX_CREATE		1	/* Create file */
138 #define	TX_MKDIR		2	/* Make directory */
139 #define	TX_MKXATTR		3	/* Make XATTR directory */
140 #define	TX_SYMLINK		4	/* Create symbolic link to a file */
141 #define	TX_REMOVE		5	/* Remove file */
142 #define	TX_RMDIR		6	/* Remove directory */
143 #define	TX_LINK			7	/* Create hard link to a file */
144 #define	TX_RENAME		8	/* Rename a file */
145 #define	TX_WRITE		9	/* File write */
146 #define	TX_TRUNCATE		10	/* Truncate a file */
147 #define	TX_SETATTR		11	/* Set file attributes */
148 #define	TX_ACL_V0		12	/* Set old formatted ACL */
149 #define	TX_ACL			13	/* Set ACL */
150 #define	TX_CREATE_ACL		14	/* create with ACL */
151 #define	TX_CREATE_ATTR		15	/* create + attrs */
152 #define	TX_CREATE_ACL_ATTR 	16	/* create with ACL + attrs */
153 #define	TX_MKDIR_ACL		17	/* mkdir with ACL */
154 #define	TX_MKDIR_ATTR		18	/* mkdir with attr */
155 #define	TX_MKDIR_ACL_ATTR	19	/* mkdir with ACL + attrs */
156 #define	TX_WRITE2		20	/* dmu_sync EALREADY write */
157 #define	TX_SETSAXATTR		21	/* Set sa xattrs on file */
158 #define	TX_RENAME_EXCHANGE	22	/* Atomic swap via renameat2 */
159 #define	TX_RENAME_WHITEOUT	23	/* Atomic whiteout via renameat2 */
160 #define	TX_CLONE_RANGE		24	/* Clone a file range */
161 #define	TX_MAX_TYPE		25	/* Max transaction type */
162 
163 /*
164  * The transactions for mkdir, symlink, remove, rmdir, link, and rename
165  * may have the following bit set, indicating the original request
166  * specified case-insensitive handling of names.
167  */
168 #define	TX_CI	((uint64_t)0x1 << 63) /* case-insensitive behavior requested */
169 
170 /*
171  * Transactions for operations below can be logged out of order.
172  * For convenience in the code, all such records must have lr_foid
173  * at the same offset.
174  */
175 #define	TX_OOO(txtype)			\
176 	((txtype) == TX_WRITE ||	\
177 	(txtype) == TX_TRUNCATE ||	\
178 	(txtype) == TX_SETATTR ||	\
179 	(txtype) == TX_ACL_V0 ||	\
180 	(txtype) == TX_ACL ||		\
181 	(txtype) == TX_WRITE2 ||	\
182 	(txtype) == TX_SETSAXATTR ||	\
183 	(txtype) == TX_CLONE_RANGE)
184 
185 /*
186  * The number of dnode slots consumed by the object is stored in the 8
187  * unused upper bits of the object ID. We subtract 1 from the value
188  * stored on disk for compatibility with implementations that don't
189  * support large dnodes. The slot count for a single-slot dnode will
190  * contain 0 for those bits to preserve the log record format for
191  * "small" dnodes.
192  */
193 #define	LR_FOID_GET_SLOTS(oid) (BF64_GET((oid), 56, 8) + 1)
194 #define	LR_FOID_SET_SLOTS(oid, x) BF64_SET((oid), 56, 8, (x) - 1)
195 #define	LR_FOID_GET_OBJ(oid) BF64_GET((oid), 0, DN_MAX_OBJECT_SHIFT)
196 #define	LR_FOID_SET_OBJ(oid, x) BF64_SET((oid), 0, DN_MAX_OBJECT_SHIFT, (x))
197 
198 /*
199  * Format of log records.
200  * The fields are carefully defined to allow them to be aligned
201  * and sized the same on sparc & intel architectures.
202  * Each log record has a common structure at the beginning.
203  *
204  * The log record on disk (lrc_seq) holds the sequence number of all log
205  * records which is used to ensure we don't replay the same record.
206  */
207 typedef struct {			/* common log record header */
208 	uint64_t	lrc_txtype;	/* intent log transaction type */
209 	uint64_t	lrc_reclen;	/* transaction record length */
210 	uint64_t	lrc_txg;	/* dmu transaction group number */
211 	uint64_t	lrc_seq;	/* see comment above */
212 } lr_t;
213 
214 /*
215  * Common start of all out-of-order record types (TX_OOO() above).
216  */
217 typedef struct {
218 	lr_t		lr_common;	/* common portion of log record */
219 	uint64_t	lr_foid;	/* object id */
220 } lr_ooo_t;
221 
222 /*
223  * Additional lr_attr_t fields.
224  */
225 typedef struct {
226 	uint64_t	lr_attr_attrs;		/* all of the attributes */
227 	uint64_t	lr_attr_crtime[2];	/* create time */
228 	uint8_t		lr_attr_scanstamp[32];
229 } lr_attr_end_t;
230 
231 /*
232  * Handle option extended vattr attributes.
233  *
234  * Whenever new attributes are added the version number
235  * will need to be updated as will code in
236  * zfs_log.c and zfs_replay.c
237  */
238 typedef struct {
239 	uint32_t	lr_attr_masksize; /* number of elements in array */
240 	uint32_t	lr_attr_bitmap; /* First entry of array */
241 	/* remainder of array and additional lr_attr_end_t fields */
242 	uint8_t		lr_attr_data[];
243 } lr_attr_t;
244 
245 /*
246  * log record for creates without optional ACL.
247  * This log record does support optional xvattr_t attributes.
248  */
249 typedef struct {
250 	lr_t		lr_common;	/* common portion of log record */
251 	uint64_t	lr_doid;	/* object id of directory */
252 	uint64_t	lr_foid;	/* object id of created file object */
253 	uint64_t	lr_mode;	/* mode of object */
254 	uint64_t	lr_uid;		/* uid of object */
255 	uint64_t	lr_gid;		/* gid of object */
256 	uint64_t	lr_gen;		/* generation (txg of creation) */
257 	uint64_t	lr_crtime[2];	/* creation time */
258 	uint64_t	lr_rdev;	/* rdev of object to create */
259 } _lr_create_t;
260 
261 typedef struct {
262 	_lr_create_t	lr_create;	/* common create portion */
263 	/* name of object to create follows this */
264 	/* for symlinks, link content follows name */
265 	/* for creates with xvattr data, the name follows the xvattr info */
266 	uint8_t		lr_data[];
267 } lr_create_t;
268 
269 /*
270  * FUID ACL record will be an array of ACEs from the original ACL.
271  * If this array includes ephemeral IDs, the record will also include
272  * an array of log-specific FUIDs to replace the ephemeral IDs.
273  * Only one copy of each unique domain will be present, so the log-specific
274  * FUIDs will use an index into a compressed domain table.  On replay this
275  * information will be used to construct real FUIDs (and bypass idmap,
276  * since it may not be available).
277  */
278 
279 /*
280  * Log record for creates with optional ACL
281  * This log record is also used for recording any FUID
282  * information needed for replaying the create.  If the
283  * file doesn't have any actual ACEs then the lr_aclcnt
284  * would be zero.
285  *
286  * After lr_acl_flags, there are a lr_acl_bytes number of variable sized ace's.
287  * If create is also setting xvattr's, then acl data follows xvattr.
288  * If ACE FUIDs are needed then they will follow the xvattr_t.  Following
289  * the FUIDs will be the domain table information.  The FUIDs for the owner
290  * and group will be in lr_create.  Name follows ACL data.
291  */
292 typedef struct {
293 	_lr_create_t	lr_create;	/* common create portion */
294 	uint64_t	lr_aclcnt;	/* number of ACEs in ACL */
295 	uint64_t	lr_domcnt;	/* number of unique domains */
296 	uint64_t	lr_fuidcnt;	/* number of real fuids */
297 	uint64_t	lr_acl_bytes;	/* number of bytes in ACL */
298 	uint64_t	lr_acl_flags;	/* ACL flags */
299 	uint8_t		lr_data[];
300 } lr_acl_create_t;
301 
302 typedef struct {
303 	lr_t		lr_common;	/* common portion of log record */
304 	uint64_t	lr_doid;	/* obj id of directory */
305 	/* name of object to remove follows this */
306 	uint8_t		lr_data[];
307 } lr_remove_t;
308 
309 typedef struct {
310 	lr_t		lr_common;	/* common portion of log record */
311 	uint64_t	lr_doid;	/* obj id of directory */
312 	uint64_t	lr_link_obj;	/* obj id of link */
313 	/* name of object to link follows this */
314 	uint8_t		lr_data[];
315 } lr_link_t;
316 
317 typedef struct {
318 	lr_t		lr_common;	/* common portion of log record */
319 	uint64_t	lr_sdoid;	/* obj id of source directory */
320 	uint64_t	lr_tdoid;	/* obj id of target directory */
321 } _lr_rename_t;
322 
323 typedef struct {
324 	_lr_rename_t	lr_rename;	/* common rename portion */
325 	/* 2 strings: names of source and destination follow this */
326 	uint8_t		lr_data[];
327 } lr_rename_t;
328 
329 typedef struct {
330 	_lr_rename_t	lr_rename;	/* common rename portion */
331 	/* members related to the whiteout file (based on _lr_create_t) */
332 	uint64_t	lr_wfoid;	/* obj id of the new whiteout file */
333 	uint64_t	lr_wmode;	/* mode of object */
334 	uint64_t	lr_wuid;	/* uid of whiteout */
335 	uint64_t	lr_wgid;	/* gid of whiteout */
336 	uint64_t	lr_wgen;	/* generation (txg of creation) */
337 	uint64_t	lr_wcrtime[2];	/* creation time */
338 	uint64_t	lr_wrdev;	/* always makedev(0, 0) */
339 	/* 2 strings: names of source and destination follow this */
340 	uint8_t		lr_data[];
341 } lr_rename_whiteout_t;
342 
343 typedef struct {
344 	lr_t		lr_common;	/* common portion of log record */
345 	uint64_t	lr_foid;	/* file object to write */
346 	uint64_t	lr_offset;	/* offset to write to */
347 	uint64_t	lr_length;	/* user data length to write */
348 	uint64_t	lr_blkoff;	/* no longer used */
349 	blkptr_t	lr_blkptr;	/* spa block pointer for replay */
350 	/* write data will follow for small writes */
351 	uint8_t		lr_data[];
352 } lr_write_t;
353 
354 typedef struct {
355 	lr_t		lr_common;	/* common portion of log record */
356 	uint64_t	lr_foid;	/* object id of file to truncate */
357 	uint64_t	lr_offset;	/* offset to truncate from */
358 	uint64_t	lr_length;	/* length to truncate */
359 } lr_truncate_t;
360 
361 typedef struct {
362 	lr_t		lr_common;	/* common portion of log record */
363 	uint64_t	lr_foid;	/* file object to change attributes */
364 	uint64_t	lr_mask;	/* mask of attributes to set */
365 	uint64_t	lr_mode;	/* mode to set */
366 	uint64_t	lr_uid;		/* uid to set */
367 	uint64_t	lr_gid;		/* gid to set */
368 	uint64_t	lr_size;	/* size to set */
369 	uint64_t	lr_atime[2];	/* access time */
370 	uint64_t	lr_mtime[2];	/* modification time */
371 	/* optional attribute lr_attr_t may be here */
372 	uint8_t		lr_data[];
373 } lr_setattr_t;
374 
375 typedef struct {
376 	lr_t		lr_common;	/* common portion of log record */
377 	uint64_t	lr_foid;	/* file object to change attributes */
378 	uint64_t	lr_size;
379 	/* xattr name and value follows */
380 	uint8_t		lr_data[];
381 } lr_setsaxattr_t;
382 
383 typedef struct {
384 	lr_t		lr_common;	/* common portion of log record */
385 	uint64_t	lr_foid;	/* obj id of file */
386 	uint64_t	lr_aclcnt;	/* number of acl entries */
387 	/* lr_aclcnt number of ace_t entries follow this */
388 	uint8_t		lr_data[];
389 } lr_acl_v0_t;
390 
391 typedef struct {
392 	lr_t		lr_common;	/* common portion of log record */
393 	uint64_t	lr_foid;	/* obj id of file */
394 	uint64_t	lr_aclcnt;	/* number of ACEs in ACL */
395 	uint64_t	lr_domcnt;	/* number of unique domains */
396 	uint64_t	lr_fuidcnt;	/* number of real fuids */
397 	uint64_t	lr_acl_bytes;	/* number of bytes in ACL */
398 	uint64_t	lr_acl_flags;	/* ACL flags */
399 	/* lr_acl_bytes number of variable sized ace's follows */
400 	uint8_t		lr_data[];
401 } lr_acl_t;
402 
403 typedef struct {
404 	lr_t		lr_common;	/* common portion of log record */
405 	uint64_t	lr_foid;	/* file object to clone into */
406 	uint64_t	lr_offset;	/* offset to clone to */
407 	uint64_t	lr_length;	/* length of the blocks to clone */
408 	uint64_t	lr_blksz;	/* file's block size */
409 	uint64_t	lr_nbps;	/* number of block pointers */
410 	/* block pointers of the blocks to clone follows */
411 	blkptr_t	lr_bps[];
412 } lr_clone_range_t;
413 
414 /*
415  * ZIL structure definitions, interface function prototype and globals.
416  */
417 
418 /*
419  * Writes are handled in three different ways:
420  *
421  * WR_INDIRECT:
422  *    In this mode, if we need to commit the write later, then the block
423  *    is immediately written into the file system (using dmu_sync),
424  *    and a pointer to the block is put into the log record.
425  *    When the txg commits the block is linked in.
426  *    This saves additionally writing the data into the log record.
427  *    There are a few requirements for this to occur:
428  *	- write is greater than zfs/zvol_immediate_write_sz
429  *	- not using slogs (as slogs are assumed to always be faster
430  *	  than writing into the main pool)
431  *	- the write occupies only one block
432  * WR_COPIED:
433  *    If we know we'll immediately be committing the
434  *    transaction (O_SYNC or O_DSYNC), then we allocate a larger
435  *    log record here for the data and copy the data in.
436  * WR_NEED_COPY:
437  *    Otherwise we don't allocate a buffer, and *if* we need to
438  *    flush the write later then a buffer is allocated and
439  *    we retrieve the data using the dmu.
440  */
441 typedef enum {
442 	WR_INDIRECT,	/* indirect - a large write (dmu_sync() data */
443 			/* and put blkptr in log, rather than actual data) */
444 	WR_COPIED,	/* immediate - data is copied into lr_write_t */
445 	WR_NEED_COPY,	/* immediate - data needs to be copied if pushed */
446 	WR_NUM_STATES	/* number of states */
447 } itx_wr_state_t;
448 
449 typedef void (*zil_callback_t)(void *data, int err);
450 
451 typedef struct itx {
452 	list_node_t	itx_node;	/* linkage on zl_itx_list */
453 	void		*itx_private;	/* type-specific opaque data */
454 	itx_wr_state_t	itx_wr_state;	/* write state */
455 	uint8_t		itx_sync;	/* synchronous transaction */
456 	zil_callback_t	itx_callback;   /* Called when the itx is persistent */
457 	void		*itx_callback_data; /* User data for the callback */
458 	size_t		itx_size;	/* allocated itx structure size */
459 	uint64_t	itx_oid;	/* object id */
460 	uint64_t	itx_gen;	/* gen number for zfs_get_data */
461 	lr_t		itx_lr;		/* common part of log record */
462 	uint8_t		itx_lr_data[];	/* type-specific part of lr_xx_t */
463 } itx_t;
464 
465 /*
466  * Used for zil kstat.
467  */
468 typedef struct zil_stats {
469 	/*
470 	 * Number of times a ZIL commit (e.g. fsync) has been requested.
471 	 */
472 	kstat_named_t zil_commit_count;
473 
474 	/*
475 	 * Number of times the ZIL has been flushed to stable storage.
476 	 * This is less than zil_commit_count when commits are "merged"
477 	 * (see the documentation above zil_commit()).
478 	 */
479 	kstat_named_t zil_commit_writer_count;
480 
481 	/*
482 	 * Number of times a ZIL commit failed and the ZIL was forced to fall
483 	 * back to txg_wait_synced(). The separate counts are for different
484 	 * reasons:
485 	 * -   error: ZIL IO (write/flush) returned an error
486 	 *            (see zil_commit_impl())
487 	 * -   stall: LWB block allocation failed, ZIL chain abandoned
488 	 *            (see zil_commit_writer_stall())
489 	 * - suspend: ZIL suspended
490 	 *            (see zil_commit(), zil_get_commit_list())
491 	 * -   crash: ZIL crashed
492 	 *            (see zil_crash(), zil_commit(), ...)
493 	 */
494 	kstat_named_t zil_commit_error_count;
495 	kstat_named_t zil_commit_stall_count;
496 	kstat_named_t zil_commit_suspend_count;
497 	kstat_named_t zil_commit_crash_count;
498 
499 	/*
500 	 * Number of transactions (reads, writes, renames, etc.)
501 	 * that have been committed.
502 	 */
503 	kstat_named_t zil_itx_count;
504 
505 	/*
506 	 * See the documentation for itx_wr_state_t above.
507 	 * Note that "bytes" accumulates the length of the transactions
508 	 * (i.e. data), not the actual log record sizes.
509 	 */
510 	kstat_named_t zil_itx_indirect_count;
511 	kstat_named_t zil_itx_indirect_bytes;
512 	kstat_named_t zil_itx_copied_count;
513 	kstat_named_t zil_itx_copied_bytes;
514 	kstat_named_t zil_itx_needcopy_count;
515 	kstat_named_t zil_itx_needcopy_bytes;
516 
517 	/*
518 	 * Transactions which have been allocated to the "normal"
519 	 * (i.e. not slog) storage pool. Note that "bytes" accumulate
520 	 * the actual log record sizes - which do not include the actual
521 	 * data in case of indirect writes.  bytes <= write <= alloc.
522 	 */
523 	kstat_named_t zil_itx_metaslab_normal_count;
524 	kstat_named_t zil_itx_metaslab_normal_bytes;
525 	kstat_named_t zil_itx_metaslab_normal_write;
526 	kstat_named_t zil_itx_metaslab_normal_alloc;
527 
528 	/*
529 	 * Transactions which have been allocated to the "slog" storage pool.
530 	 * If there are no separate log devices, this is the same as the
531 	 * "normal" pool.  bytes <= write <= alloc.
532 	 */
533 	kstat_named_t zil_itx_metaslab_slog_count;
534 	kstat_named_t zil_itx_metaslab_slog_bytes;
535 	kstat_named_t zil_itx_metaslab_slog_write;
536 	kstat_named_t zil_itx_metaslab_slog_alloc;
537 } zil_kstat_values_t;
538 
539 typedef struct zil_sums {
540 	wmsum_t zil_commit_count;
541 	wmsum_t zil_commit_writer_count;
542 	wmsum_t zil_commit_error_count;
543 	wmsum_t zil_commit_stall_count;
544 	wmsum_t zil_commit_suspend_count;
545 	wmsum_t zil_commit_crash_count;
546 	wmsum_t zil_itx_count;
547 	wmsum_t zil_itx_indirect_count;
548 	wmsum_t zil_itx_indirect_bytes;
549 	wmsum_t zil_itx_copied_count;
550 	wmsum_t zil_itx_copied_bytes;
551 	wmsum_t zil_itx_needcopy_count;
552 	wmsum_t zil_itx_needcopy_bytes;
553 	wmsum_t zil_itx_metaslab_normal_count;
554 	wmsum_t zil_itx_metaslab_normal_bytes;
555 	wmsum_t zil_itx_metaslab_normal_write;
556 	wmsum_t zil_itx_metaslab_normal_alloc;
557 	wmsum_t zil_itx_metaslab_slog_count;
558 	wmsum_t zil_itx_metaslab_slog_bytes;
559 	wmsum_t zil_itx_metaslab_slog_write;
560 	wmsum_t zil_itx_metaslab_slog_alloc;
561 } zil_sums_t;
562 
563 #define	ZIL_STAT_INCR(zil, stat, val) \
564 	do { \
565 		int64_t tmpval = (val); \
566 		wmsum_add(&(zil_sums_global.stat), tmpval); \
567 		if ((zil)->zl_sums) \
568 			wmsum_add(&((zil)->zl_sums->stat), tmpval); \
569 	} while (0)
570 
571 #define	ZIL_STAT_BUMP(zil, stat) \
572     ZIL_STAT_INCR(zil, stat, 1);
573 
574 /*
575  * Flags for zil_commit_flags(). zil_commit() is a shortcut for
576  * zil_commit_flags(ZIL_COMMIT_FAILMODE), which is the most common use.
577  */
578 typedef enum {
579 	/*
580 	 * Try to commit the ZIL. If it fails, fall back to txg_wait_synced().
581 	 * If that fails, return EIO.
582 	 */
583 	ZIL_COMMIT_NOW = 0,
584 
585 	/*
586 	 * Like ZIL_COMMIT_NOW, but if the ZIL commit fails because the pool
587 	 * suspended, act according to the pool's failmode= setting (wait for
588 	 * the pool to resume, or return EIO).
589 	 */
590 	ZIL_COMMIT_FAILMODE = (1 << 1),
591 } zil_commit_flag_t;
592 
593 typedef int zil_parse_blk_func_t(zilog_t *zilog, const blkptr_t *bp, void *arg,
594     uint64_t txg);
595 typedef int zil_parse_lr_func_t(zilog_t *zilog, const lr_t *lr, void *arg,
596     uint64_t txg);
597 typedef int zil_replay_func_t(void *arg1, void *arg2, boolean_t byteswap);
598 typedef int zil_get_data_t(void *arg, uint64_t arg2, lr_write_t *lr, char *dbuf,
599     struct lwb *lwb, zio_t *zio);
600 
601 extern int zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func,
602     zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg,
603     boolean_t decrypt);
604 
605 extern void	zil_init(void);
606 extern void	zil_fini(void);
607 
608 extern zilog_t	*zil_alloc(objset_t *os, zil_header_t *zh_phys);
609 extern void	zil_free(zilog_t *zilog);
610 
611 extern zilog_t	*zil_open(objset_t *os, zil_get_data_t *get_data,
612     zil_sums_t *zil_sums);
613 extern void	zil_close(zilog_t *zilog);
614 
615 extern boolean_t zil_replay(objset_t *os, void *arg,
616     zil_replay_func_t *const replay_func[TX_MAX_TYPE]);
617 extern boolean_t zil_replaying(zilog_t *zilog, dmu_tx_t *tx);
618 extern boolean_t zil_destroy(zilog_t *zilog, boolean_t keep_first);
619 extern void	zil_destroy_sync(zilog_t *zilog, dmu_tx_t *tx);
620 
621 extern itx_t	*zil_itx_create(uint64_t txtype, size_t lrsize);
622 extern void	zil_itx_destroy(itx_t *itx, int err);
623 extern void	zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx);
624 
625 extern void	zil_async_to_sync(zilog_t *zilog, uint64_t oid);
626 extern void	zil_remove_async(zilog_t *zilog, uint64_t oid);
627 
628 extern int	zil_commit_flags(zilog_t *zilog, uint64_t oid,
629     zil_commit_flag_t flags);
630 extern int __must_check	zil_commit(zilog_t *zilog, uint64_t oid);
631 
632 extern int	zil_reset(const char *osname, void *txarg);
633 extern int	zil_claim(struct dsl_pool *dp,
634     struct dsl_dataset *ds, void *txarg);
635 extern int 	zil_check_log_chain(struct dsl_pool *dp,
636     struct dsl_dataset *ds, void *tx);
637 extern void	zil_sync(zilog_t *zilog, dmu_tx_t *tx);
638 extern void	zil_clean(zilog_t *zilog, uint64_t synced_txg);
639 
640 extern int	zil_suspend(const char *osname, void **cookiep);
641 extern void	zil_resume(void *cookie);
642 
643 extern void	zil_lwb_add_block(struct lwb *lwb, const blkptr_t *bp);
644 extern void	zil_lwb_add_txg(struct lwb *lwb, uint64_t txg);
645 extern int	zil_bp_tree_add(zilog_t *zilog, const blkptr_t *bp);
646 
647 extern void	zil_set_sync(zilog_t *zilog, uint64_t syncval);
648 
649 extern void	zil_set_logbias(zilog_t *zilog, uint64_t slogval);
650 
651 extern uint64_t	zil_max_copied_data(zilog_t *zilog);
652 extern uint64_t	zil_max_log_data(zilog_t *zilog, size_t hdrsize);
653 extern itx_wr_state_t zil_write_state(zilog_t *zilog, uint64_t size,
654     uint32_t blocksize, boolean_t o_direct, boolean_t commit);
655 
656 extern void zil_sums_init(zil_sums_t *zs);
657 extern void zil_sums_fini(zil_sums_t *zs);
658 extern void zil_kstat_values_update(zil_kstat_values_t *zs,
659     zil_sums_t *zil_sums);
660 
661 extern int zil_replay_disable;
662 extern uint_t zfs_immediate_write_sz;
663 extern int zil_special_is_slog;
664 
665 #ifdef	__cplusplus
666 }
667 #endif
668 
669 #endif	/* _SYS_ZIL_H */
670