xref: /illumos-gate/usr/src/uts/common/fs/zfs/sys/zil.h (revision d80c45e0f58fa434ba37259ea2e2b12e0380c19a)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5436b2950Sperrin  * Common Development and Distribution License (the "License").
6436b2950Sperrin  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
22436b2950Sperrin  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26fa9e4066Sahrens #ifndef	_SYS_ZIL_H
27fa9e4066Sahrens #define	_SYS_ZIL_H
28fa9e4066Sahrens 
29fa9e4066Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
30fa9e4066Sahrens 
31fa9e4066Sahrens #include <sys/types.h>
32fa9e4066Sahrens #include <sys/spa.h>
33fa9e4066Sahrens #include <sys/zio.h>
34fa9e4066Sahrens #include <sys/dmu.h>
35fa9e4066Sahrens 
36fa9e4066Sahrens #ifdef	__cplusplus
37fa9e4066Sahrens extern "C" {
38fa9e4066Sahrens #endif
39fa9e4066Sahrens 
40fa9e4066Sahrens /*
41fa9e4066Sahrens  * Intent log format:
42fa9e4066Sahrens  *
43fa9e4066Sahrens  * Each objset has its own intent log.  The log header (zil_header_t)
44fa9e4066Sahrens  * for objset N's intent log is kept in the Nth object of the SPA's
45fa9e4066Sahrens  * intent_log objset.  The log header points to a chain of log blocks,
46fa9e4066Sahrens  * each of which contains log records (i.e., transactions) followed by
47fa9e4066Sahrens  * a log block trailer (zil_trailer_t).  The format of a log record
48fa9e4066Sahrens  * depends on the record (or transaction) type, but all records begin
49fa9e4066Sahrens  * with a common structure that defines the type, length, and txg.
50fa9e4066Sahrens  */
51fa9e4066Sahrens 
52fa9e4066Sahrens /*
53fa9e4066Sahrens  * Intent log header - this on disk structure holds fields to manage
54fa9e4066Sahrens  * the log.  All fields are 64 bit to easily handle cross architectures.
55fa9e4066Sahrens  */
56fa9e4066Sahrens typedef struct zil_header {
57fa9e4066Sahrens 	uint64_t zh_claim_txg;	/* txg in which log blocks were claimed */
58fa9e4066Sahrens 	uint64_t zh_replay_seq;	/* highest replayed sequence number */
59fa9e4066Sahrens 	blkptr_t zh_log;	/* log chain */
60*d80c45e0Sbonwick 	uint64_t zh_claim_seq;	/* highest claimed sequence number */
61*d80c45e0Sbonwick 	uint64_t zh_pad[5];
62fa9e4066Sahrens } zil_header_t;
63fa9e4066Sahrens 
64fa9e4066Sahrens /*
65fa9e4066Sahrens  * Log block trailer - structure at the end of the header and each log block
66fa9e4066Sahrens  *
67fa9e4066Sahrens  * The zit_bt contains a zbt_cksum which for the intent log is
68fa9e4066Sahrens  * the sequence number of this log block. A seq of 0 is invalid.
69fa9e4066Sahrens  * The zbt_cksum is checked by the SPA against the sequence
70fa9e4066Sahrens  * number passed in the blk_cksum field of the blkptr_t
71fa9e4066Sahrens  */
72fa9e4066Sahrens typedef struct zil_trailer {
73fa9e4066Sahrens 	uint64_t zit_pad;
74fa9e4066Sahrens 	blkptr_t zit_next_blk;	/* next block in chain */
75fa9e4066Sahrens 	uint64_t zit_nused;	/* bytes in log block used */
76fa9e4066Sahrens 	zio_block_tail_t zit_bt; /* block trailer */
77fa9e4066Sahrens } zil_trailer_t;
78fa9e4066Sahrens 
79fa9e4066Sahrens #define	ZIL_MIN_BLKSZ	4096
80fa9e4066Sahrens #define	ZIL_MAX_BLKSZ	SPA_MAXBLOCKSIZE
81fa9e4066Sahrens #define	ZIL_BLK_DATA_SZ(lwb)	((lwb)->lwb_sz - sizeof (zil_trailer_t))
82fa9e4066Sahrens 
83*d80c45e0Sbonwick /*
84*d80c45e0Sbonwick  * The words of a log block checksum.
85*d80c45e0Sbonwick  */
86*d80c45e0Sbonwick #define	ZIL_ZC_GUID_0	0
87*d80c45e0Sbonwick #define	ZIL_ZC_GUID_1	1
88*d80c45e0Sbonwick #define	ZIL_ZC_OBJSET	2
89*d80c45e0Sbonwick #define	ZIL_ZC_SEQ	3
90*d80c45e0Sbonwick 
91fa9e4066Sahrens /*
92fa9e4066Sahrens  * Intent log transaction types and record structures
93fa9e4066Sahrens  */
94fa9e4066Sahrens #define	TX_CREATE	1		/* Create file */
95fa9e4066Sahrens #define	TX_MKDIR	2		/* Make directory */
96fa9e4066Sahrens #define	TX_MKXATTR	3		/* Make XATTR directory */
97fa9e4066Sahrens #define	TX_SYMLINK	4		/* Create symbolic link to a file */
98fa9e4066Sahrens #define	TX_REMOVE	5		/* Remove file */
99fa9e4066Sahrens #define	TX_RMDIR	6		/* Remove directory */
100fa9e4066Sahrens #define	TX_LINK		7		/* Create hard link to a file */
101fa9e4066Sahrens #define	TX_RENAME	8		/* Rename a file */
102fa9e4066Sahrens #define	TX_WRITE	9		/* File write */
103fa9e4066Sahrens #define	TX_TRUNCATE	10		/* Truncate a file */
104fa9e4066Sahrens #define	TX_SETATTR	11		/* Set file attributes */
105fa9e4066Sahrens #define	TX_ACL		12		/* Set acl */
106fa9e4066Sahrens #define	TX_MAX_TYPE	13		/* Max transaction type */
107fa9e4066Sahrens 
108fa9e4066Sahrens /*
109fa9e4066Sahrens  * Format of log records.
110fa9e4066Sahrens  * The fields are carefully defined to allow them to be aligned
111fa9e4066Sahrens  * and sized the same on sparc & intel architectures.
112fa9e4066Sahrens  * Each log record has a common structure at the beginning.
113fa9e4066Sahrens  */
114fa9e4066Sahrens typedef struct {			/* common log record header */
115fa9e4066Sahrens 	uint64_t	lrc_txtype;	/* intent log transaction type */
116fa9e4066Sahrens 	uint64_t	lrc_reclen;	/* transaction record length */
117fa9e4066Sahrens 	uint64_t	lrc_txg;	/* dmu transaction group number */
118fa9e4066Sahrens 	uint64_t	lrc_seq;	/* intent log sequence number */
119fa9e4066Sahrens } lr_t;
120fa9e4066Sahrens 
121fa9e4066Sahrens typedef struct {
122fa9e4066Sahrens 	lr_t		lr_common;	/* common portion of log record */
123fa9e4066Sahrens 	uint64_t	lr_doid;	/* object id of directory */
124fa9e4066Sahrens 	uint64_t	lr_foid;	/* object id of created file object */
125fa9e4066Sahrens 	uint64_t	lr_mode;	/* mode of object */
126fa9e4066Sahrens 	uint64_t	lr_uid;		/* uid of object */
127fa9e4066Sahrens 	uint64_t	lr_gid;		/* gid of object */
128fa9e4066Sahrens 	uint64_t	lr_gen;		/* generation (txg of creation) */
129fa9e4066Sahrens 	uint64_t	lr_crtime[2];	/* creation time */
130fa9e4066Sahrens 	uint64_t	lr_rdev;	/* rdev of object to create */
131fa9e4066Sahrens 	/* name of object to create follows this */
132fa9e4066Sahrens 	/* for symlinks, link content follows name */
133fa9e4066Sahrens } lr_create_t;
134fa9e4066Sahrens 
135fa9e4066Sahrens typedef struct {
136fa9e4066Sahrens 	lr_t		lr_common;	/* common portion of log record */
137fa9e4066Sahrens 	uint64_t	lr_doid;	/* obj id of directory */
138fa9e4066Sahrens 	/* name of object to remove follows this */
139fa9e4066Sahrens } lr_remove_t;
140fa9e4066Sahrens 
141fa9e4066Sahrens typedef struct {
142fa9e4066Sahrens 	lr_t		lr_common;	/* common portion of log record */
143fa9e4066Sahrens 	uint64_t	lr_doid;	/* obj id of directory */
144fa9e4066Sahrens 	uint64_t	lr_link_obj;	/* obj id of link */
145fa9e4066Sahrens 	/* name of object to link follows this */
146fa9e4066Sahrens } lr_link_t;
147fa9e4066Sahrens 
148fa9e4066Sahrens typedef struct {
149fa9e4066Sahrens 	lr_t		lr_common;	/* common portion of log record */
150fa9e4066Sahrens 	uint64_t	lr_sdoid;	/* obj id of source directory */
151fa9e4066Sahrens 	uint64_t	lr_tdoid;	/* obj id of target directory */
152fa9e4066Sahrens 	/* 2 strings: names of source and destination follow this */
153fa9e4066Sahrens } lr_rename_t;
154fa9e4066Sahrens 
155fa9e4066Sahrens typedef struct {
156fa9e4066Sahrens 	lr_t		lr_common;	/* common portion of log record */
157fa9e4066Sahrens 	uint64_t	lr_foid;	/* file object to write */
158fa9e4066Sahrens 	uint64_t	lr_offset;	/* offset to write to */
159fa9e4066Sahrens 	uint64_t	lr_length;	/* user data length to write */
160fa9e4066Sahrens 	uint64_t	lr_blkoff;	/* offset represented by lr_blkptr */
161fa9e4066Sahrens 	blkptr_t	lr_blkptr;	/* spa block pointer for replay */
162fa9e4066Sahrens 	/* write data will follow for small writes */
163fa9e4066Sahrens } lr_write_t;
164fa9e4066Sahrens 
165fa9e4066Sahrens typedef struct {
166fa9e4066Sahrens 	lr_t		lr_common;	/* common portion of log record */
167fa9e4066Sahrens 	uint64_t	lr_foid;	/* object id of file to truncate */
168fa9e4066Sahrens 	uint64_t	lr_offset;	/* offset to truncate from */
169fa9e4066Sahrens 	uint64_t	lr_length;	/* length to truncate */
170fa9e4066Sahrens } lr_truncate_t;
171fa9e4066Sahrens 
172fa9e4066Sahrens typedef struct {
173fa9e4066Sahrens 	lr_t		lr_common;	/* common portion of log record */
174fa9e4066Sahrens 	uint64_t	lr_foid;	/* file object to change attributes */
175fa9e4066Sahrens 	uint64_t	lr_mask;	/* mask of attributes to set */
176fa9e4066Sahrens 	uint64_t	lr_mode;	/* mode to set */
177fa9e4066Sahrens 	uint64_t	lr_uid;		/* uid to set */
178fa9e4066Sahrens 	uint64_t	lr_gid;		/* gid to set */
179fa9e4066Sahrens 	uint64_t	lr_size;	/* size to set */
180fa9e4066Sahrens 	uint64_t	lr_atime[2];	/* access time */
181fa9e4066Sahrens 	uint64_t	lr_mtime[2];	/* modification time */
182fa9e4066Sahrens } lr_setattr_t;
183fa9e4066Sahrens 
184fa9e4066Sahrens typedef struct {
185fa9e4066Sahrens 	lr_t		lr_common;	/* common portion of log record */
186fa9e4066Sahrens 	uint64_t	lr_foid;	/* obj id of file */
187fa9e4066Sahrens 	uint64_t	lr_aclcnt;	/* number of acl entries */
188fa9e4066Sahrens 	/* lr_aclcnt number of ace_t entries follow this */
189fa9e4066Sahrens } lr_acl_t;
190fa9e4066Sahrens 
191fa9e4066Sahrens /*
192fa9e4066Sahrens  * ZIL structure definitions, interface function prototype and globals.
193fa9e4066Sahrens  */
194fa9e4066Sahrens 
195fa9e4066Sahrens /*
196fa9e4066Sahrens  * ZFS intent log transaction structure
197fa9e4066Sahrens  */
198104e2ed7Sperrin typedef enum {
199104e2ed7Sperrin 	WR_INDIRECT,	/* indirect - a large write (dmu_sync() data */
200104e2ed7Sperrin 			/* and put blkptr in log, rather than actual data) */
201104e2ed7Sperrin 	WR_COPIED,	/* immediate - data is copied into lr_write_t */
202104e2ed7Sperrin 	WR_NEED_COPY,	/* immediate - data needs to be copied if pushed */
203104e2ed7Sperrin } itx_wr_state_t;
204104e2ed7Sperrin 
205fa9e4066Sahrens typedef struct itx {
206fa9e4066Sahrens 	list_node_t	itx_node;	/* linkage on zl_itx_list */
207fa9e4066Sahrens 	void		*itx_private;	/* type-specific opaque data */
208104e2ed7Sperrin 	itx_wr_state_t	itx_wr_state;	/* write state */
209fa9e4066Sahrens 	lr_t		itx_lr;		/* common part of log record */
210fa9e4066Sahrens 	/* followed by type-specific part of lr_xx_t and its immediate data */
211fa9e4066Sahrens } itx_t;
212fa9e4066Sahrens 
213fa9e4066Sahrens typedef void zil_parse_blk_func_t(zilog_t *zilog, blkptr_t *bp, void *arg,
214fa9e4066Sahrens     uint64_t txg);
215fa9e4066Sahrens typedef void zil_parse_lr_func_t(zilog_t *zilog, lr_t *lr, void *arg,
216fa9e4066Sahrens     uint64_t txg);
217fa9e4066Sahrens typedef int zil_replay_func_t();
218104e2ed7Sperrin typedef int zil_get_data_t(void *arg, lr_write_t *lr, char *dbuf);
219fa9e4066Sahrens 
220*d80c45e0Sbonwick extern uint64_t	zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func,
221fa9e4066Sahrens     zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg);
222fa9e4066Sahrens 
223fa9e4066Sahrens extern void	zil_init(void);
224fa9e4066Sahrens extern void	zil_fini(void);
225fa9e4066Sahrens 
226fa9e4066Sahrens extern zilog_t	*zil_alloc(objset_t *os, zil_header_t *zh_phys);
227fa9e4066Sahrens extern void	zil_free(zilog_t *zilog);
228fa9e4066Sahrens 
229fa9e4066Sahrens extern zilog_t	*zil_open(objset_t *os, zil_get_data_t *get_data);
230fa9e4066Sahrens extern void	zil_close(zilog_t *zilog);
231fa9e4066Sahrens 
232fa9e4066Sahrens extern void	zil_replay(objset_t *os, void *arg, uint64_t *txgp,
233fa9e4066Sahrens     zil_replay_func_t *replay_func[TX_MAX_TYPE], void (*rm_wait)(void *));
234*d80c45e0Sbonwick extern void	zil_destroy(zilog_t *zilog, boolean_t keep_first);
235fa9e4066Sahrens 
236fa9e4066Sahrens extern itx_t	*zil_itx_create(int txtype, size_t lrsize);
237fa9e4066Sahrens extern uint64_t zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx);
238fa9e4066Sahrens 
239fa9e4066Sahrens extern void	zil_commit(zilog_t *zilog, uint64_t seq, int ioflag);
240fa9e4066Sahrens 
241fa9e4066Sahrens extern void	zil_claim(char *osname, void *txarg);
242fa9e4066Sahrens extern void	zil_sync(zilog_t *zilog, dmu_tx_t *tx);
243fa9e4066Sahrens extern void	zil_clean(zilog_t *zilog);
244436b2950Sperrin extern int	zil_is_committed(zilog_t *zilog);
245fa9e4066Sahrens 
246fa9e4066Sahrens extern int	zil_suspend(zilog_t *zilog);
247fa9e4066Sahrens extern void	zil_resume(zilog_t *zilog);
248fa9e4066Sahrens 
249fa9e4066Sahrens extern int zil_disable;
250fa9e4066Sahrens extern int zil_always;
251fa9e4066Sahrens extern int zil_purge;
252fa9e4066Sahrens 
253fa9e4066Sahrens #ifdef	__cplusplus
254fa9e4066Sahrens }
255fa9e4066Sahrens #endif
256fa9e4066Sahrens 
257fa9e4066Sahrens #endif	/* _SYS_ZIL_H */
258