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