xref: /illumos-gate/usr/src/uts/common/fs/zfs/sys/zil.h (revision fd1368791be99c4a6354fa81f08408c2dbf4b444)
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 2009 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 #include <sys/types.h>
30 #include <sys/spa.h>
31 #include <sys/zio.h>
32 #include <sys/dmu.h>
33 
34 #ifdef	__cplusplus
35 extern "C" {
36 #endif
37 
38 /*
39  * Intent log format:
40  *
41  * Each objset has its own intent log.  The log header (zil_header_t)
42  * for objset N's intent log is kept in the Nth object of the SPA's
43  * intent_log objset.  The log header points to a chain of log blocks,
44  * each of which contains log records (i.e., transactions) followed by
45  * a log block trailer (zil_trailer_t).  The format of a log record
46  * depends on the record (or transaction) type, but all records begin
47  * with a common structure that defines the type, length, and txg.
48  */
49 
50 /*
51  * Intent log header - this on disk structure holds fields to manage
52  * the log.  All fields are 64 bit to easily handle cross architectures.
53  */
54 typedef struct zil_header {
55 	uint64_t zh_claim_txg;	/* txg in which log blocks were claimed */
56 	uint64_t zh_replay_seq;	/* highest replayed sequence number */
57 	blkptr_t zh_log;	/* log chain */
58 	uint64_t zh_claim_blk_seq; /* highest claimed block sequence number */
59 	uint64_t zh_flags;	/* header flags */
60 	uint64_t zh_claim_lr_seq; /* highest claimed lr sequence number */
61 	uint64_t zh_pad[3];
62 } zil_header_t;
63 
64 /*
65  * zh_flags bit settings
66  */
67 #define	ZIL_REPLAY_NEEDED	0x1	/* replay needed - internal only */
68 #define	ZIL_CLAIM_LR_SEQ_VALID	0x2	/* zh_claim_lr_seq field is valid */
69 
70 /*
71  * Log block trailer - structure at the end of the header and each log block
72  *
73  * The zit_bt contains a zbt_cksum which for the intent log is
74  * the sequence number of this log block. A seq of 0 is invalid.
75  * The zbt_cksum is checked by the SPA against the sequence
76  * number passed in the blk_cksum field of the blkptr_t
77  */
78 typedef struct zil_trailer {
79 	uint64_t zit_pad;
80 	blkptr_t zit_next_blk;	/* next block in chain */
81 	uint64_t zit_nused;	/* bytes in log block used */
82 	zio_block_tail_t zit_bt; /* block trailer */
83 } zil_trailer_t;
84 
85 #define	ZIL_MIN_BLKSZ	4096ULL
86 #define	ZIL_MAX_BLKSZ	SPA_MAXBLOCKSIZE
87 #define	ZIL_BLK_DATA_SZ(lwb)	((lwb)->lwb_sz - sizeof (zil_trailer_t))
88 
89 /*
90  * The words of a log block checksum.
91  */
92 #define	ZIL_ZC_GUID_0	0
93 #define	ZIL_ZC_GUID_1	1
94 #define	ZIL_ZC_OBJSET	2
95 #define	ZIL_ZC_SEQ	3
96 
97 typedef enum zil_create {
98 	Z_FILE,
99 	Z_DIR,
100 	Z_XATTRDIR,
101 } zil_create_t;
102 
103 /*
104  * size of xvattr log section.
105  * its composed of lr_attr_t + xvattr bitmap + 2 64 bit timestamps
106  * for create time and a single 64 bit integer for all of the attributes,
107  * and 4 64 bit integers (32 bytes) for the scanstamp.
108  *
109  */
110 
111 #define	ZIL_XVAT_SIZE(mapsize) \
112 	sizeof (lr_attr_t) + (sizeof (uint32_t) * (mapsize - 1)) + \
113 	(sizeof (uint64_t) * 7)
114 
115 /*
116  * Size of ACL in log.  The ACE data is padded out to properly align
117  * on 8 byte boundary.
118  */
119 
120 #define	ZIL_ACE_LENGTH(x)	(roundup(x, sizeof (uint64_t)))
121 
122 /*
123  * Intent log transaction types and record structures
124  */
125 #define	TX_CREATE		1	/* Create file */
126 #define	TX_MKDIR		2	/* Make directory */
127 #define	TX_MKXATTR		3	/* Make XATTR directory */
128 #define	TX_SYMLINK		4	/* Create symbolic link to a file */
129 #define	TX_REMOVE		5	/* Remove file */
130 #define	TX_RMDIR		6	/* Remove directory */
131 #define	TX_LINK			7	/* Create hard link to a file */
132 #define	TX_RENAME		8	/* Rename a file */
133 #define	TX_WRITE		9	/* File write */
134 #define	TX_TRUNCATE		10	/* Truncate a file */
135 #define	TX_SETATTR		11	/* Set file attributes */
136 #define	TX_ACL_V0		12	/* Set old formatted ACL */
137 #define	TX_ACL			13	/* Set ACL */
138 #define	TX_CREATE_ACL		14	/* create with ACL */
139 #define	TX_CREATE_ATTR		15	/* create + attrs */
140 #define	TX_CREATE_ACL_ATTR 	16	/* create with ACL + attrs */
141 #define	TX_MKDIR_ACL		17	/* mkdir with ACL */
142 #define	TX_MKDIR_ATTR		18	/* mkdir with attr */
143 #define	TX_MKDIR_ACL_ATTR	19	/* mkdir with ACL + attrs */
144 #define	TX_WRITE2		20	/* dmu_sync EALREADY write */
145 #define	TX_MAX_TYPE		21	/* Max transaction type */
146 
147 /*
148  * The transactions for mkdir, symlink, remove, rmdir, link, and rename
149  * may have the following bit set, indicating the original request
150  * specified case-insensitive handling of names.
151  */
152 #define	TX_CI	((uint64_t)0x1 << 63) /* case-insensitive behavior requested */
153 
154 /*
155  * Transactions for write, truncate, setattr, acl_v0, and acl can be logged
156  * out of order.  For convenience in the code, all such records must have
157  * lr_foid at the same offset.
158  */
159 #define	TX_OOO(txtype)			\
160 	((txtype) == TX_WRITE ||	\
161 	(txtype) == TX_TRUNCATE ||	\
162 	(txtype) == TX_SETATTR ||	\
163 	(txtype) == TX_ACL_V0 ||	\
164 	(txtype) == TX_ACL ||		\
165 	(txtype) == TX_WRITE2)
166 
167 
168 /*
169  * Format of log records.
170  * The fields are carefully defined to allow them to be aligned
171  * and sized the same on sparc & intel architectures.
172  * Each log record has a common structure at the beginning.
173  *
174  * Note, lrc_seq holds two different sequence numbers. Whilst in memory
175  * it contains the transaction sequence number.  The log record on
176  * disk holds the sequence number of all log records which is used to
177  * ensure we don't replay the same record.  The two sequence numbers are
178  * different because the transactions can now be pushed out of order.
179  */
180 typedef struct {			/* common log record header */
181 	uint64_t	lrc_txtype;	/* intent log transaction type */
182 	uint64_t	lrc_reclen;	/* transaction record length */
183 	uint64_t	lrc_txg;	/* dmu transaction group number */
184 	uint64_t	lrc_seq;	/* see comment above */
185 } lr_t;
186 
187 /*
188  * Common start of all out-of-order record types (TX_OOO() above).
189  */
190 typedef struct {
191 	lr_t		lr_common;	/* common portion of log record */
192 	uint64_t	lr_foid;	/* object id */
193 } lr_ooo_t;
194 
195 /*
196  * Handle option extended vattr attributes.
197  *
198  * Whenever new attributes are added the version number
199  * will need to be updated as will code in
200  * zfs_log.c and zfs_replay.c
201  */
202 typedef struct {
203 	uint32_t	lr_attr_masksize; /* number of elements in array */
204 	uint32_t	lr_attr_bitmap; /* First entry of array */
205 	/* remainder of array and any additional fields */
206 } lr_attr_t;
207 
208 /*
209  * log record for creates without optional ACL.
210  * This log record does support optional xvattr_t attributes.
211  */
212 typedef struct {
213 	lr_t		lr_common;	/* common portion of log record */
214 	uint64_t	lr_doid;	/* object id of directory */
215 	uint64_t	lr_foid;	/* object id of created file object */
216 	uint64_t	lr_mode;	/* mode of object */
217 	uint64_t	lr_uid;		/* uid of object */
218 	uint64_t	lr_gid;		/* gid of object */
219 	uint64_t	lr_gen;		/* generation (txg of creation) */
220 	uint64_t	lr_crtime[2];	/* creation time */
221 	uint64_t	lr_rdev;	/* rdev of object to create */
222 	/* name of object to create follows this */
223 	/* for symlinks, link content follows name */
224 	/* for creates with xvattr data, the name follows the xvattr info */
225 } lr_create_t;
226 
227 /*
228  * FUID ACL record will be an array of ACEs from the original ACL.
229  * If this array includes ephemeral IDs, the record will also include
230  * an array of log-specific FUIDs to replace the ephemeral IDs.
231  * Only one copy of each unique domain will be present, so the log-specific
232  * FUIDs will use an index into a compressed domain table.  On replay this
233  * information will be used to construct real FUIDs (and bypass idmap,
234  * since it may not be available).
235  */
236 
237 /*
238  * Log record for creates with optional ACL
239  * This log record is also used for recording any FUID
240  * information needed for replaying the create.  If the
241  * file doesn't have any actual ACEs then the lr_aclcnt
242  * would be zero.
243  */
244 typedef struct {
245 	lr_create_t	lr_create;	/* common create portion */
246 	uint64_t	lr_aclcnt;	/* number of ACEs in ACL */
247 	uint64_t	lr_domcnt;	/* number of unique domains */
248 	uint64_t	lr_fuidcnt;	/* number of real fuids */
249 	uint64_t	lr_acl_bytes;	/* number of bytes in ACL */
250 	uint64_t	lr_acl_flags;	/* ACL flags */
251 	/* lr_acl_bytes number of variable sized ace's follows */
252 	/* if create is also setting xvattr's, then acl data follows xvattr */
253 	/* if ACE FUIDs are needed then they will follow the xvattr_t */
254 	/* Following the FUIDs will be the domain table information. */
255 	/* The FUIDs for the owner and group will be in the lr_create */
256 	/* portion of the record. */
257 	/* name follows ACL data */
258 } lr_acl_create_t;
259 
260 typedef struct {
261 	lr_t		lr_common;	/* common portion of log record */
262 	uint64_t	lr_doid;	/* obj id of directory */
263 	/* name of object to remove follows this */
264 } lr_remove_t;
265 
266 typedef struct {
267 	lr_t		lr_common;	/* common portion of log record */
268 	uint64_t	lr_doid;	/* obj id of directory */
269 	uint64_t	lr_link_obj;	/* obj id of link */
270 	/* name of object to link follows this */
271 } lr_link_t;
272 
273 typedef struct {
274 	lr_t		lr_common;	/* common portion of log record */
275 	uint64_t	lr_sdoid;	/* obj id of source directory */
276 	uint64_t	lr_tdoid;	/* obj id of target directory */
277 	/* 2 strings: names of source and destination follow this */
278 } lr_rename_t;
279 
280 typedef struct {
281 	lr_t		lr_common;	/* common portion of log record */
282 	uint64_t	lr_foid;	/* file object to write */
283 	uint64_t	lr_offset;	/* offset to write to */
284 	uint64_t	lr_length;	/* user data length to write */
285 	uint64_t	lr_blkoff;	/* no longer used */
286 	blkptr_t	lr_blkptr;	/* spa block pointer for replay */
287 	/* write data will follow for small writes */
288 } lr_write_t;
289 
290 typedef struct {
291 	lr_t		lr_common;	/* common portion of log record */
292 	uint64_t	lr_foid;	/* object id of file to truncate */
293 	uint64_t	lr_offset;	/* offset to truncate from */
294 	uint64_t	lr_length;	/* length to truncate */
295 } lr_truncate_t;
296 
297 typedef struct {
298 	lr_t		lr_common;	/* common portion of log record */
299 	uint64_t	lr_foid;	/* file object to change attributes */
300 	uint64_t	lr_mask;	/* mask of attributes to set */
301 	uint64_t	lr_mode;	/* mode to set */
302 	uint64_t	lr_uid;		/* uid to set */
303 	uint64_t	lr_gid;		/* gid to set */
304 	uint64_t	lr_size;	/* size to set */
305 	uint64_t	lr_atime[2];	/* access time */
306 	uint64_t	lr_mtime[2];	/* modification time */
307 	/* optional attribute lr_attr_t may be here */
308 } lr_setattr_t;
309 
310 typedef struct {
311 	lr_t		lr_common;	/* common portion of log record */
312 	uint64_t	lr_foid;	/* obj id of file */
313 	uint64_t	lr_aclcnt;	/* number of acl entries */
314 	/* lr_aclcnt number of ace_t entries follow this */
315 } lr_acl_v0_t;
316 
317 typedef struct {
318 	lr_t		lr_common;	/* common portion of log record */
319 	uint64_t	lr_foid;	/* obj id of file */
320 	uint64_t	lr_aclcnt;	/* number of ACEs in ACL */
321 	uint64_t	lr_domcnt;	/* number of unique domains */
322 	uint64_t	lr_fuidcnt;	/* number of real fuids */
323 	uint64_t	lr_acl_bytes;	/* number of bytes in ACL */
324 	uint64_t	lr_acl_flags;	/* ACL flags */
325 	/* lr_acl_bytes number of variable sized ace's follows */
326 } lr_acl_t;
327 
328 /*
329  * ZIL structure definitions, interface function prototype and globals.
330  */
331 
332 /*
333  * Writes are handled in three different ways:
334  *
335  * WR_INDIRECT:
336  *    In this mode, if we need to commit the write later, then the block
337  *    is immediately written into the file system (using dmu_sync),
338  *    and a pointer to the block is put into the log record.
339  *    When the txg commits the block is linked in.
340  *    This saves additionally writing the data into the log record.
341  *    There are a few requirements for this to occur:
342  *	- write is greater than zfs/zvol_immediate_write_sz
343  *	- not using slogs (as slogs are assumed to always be faster
344  *	  than writing into the main pool)
345  *	- the write occupies only one block
346  * WR_COPIED:
347  *    If we know we'll immediately be committing the
348  *    transaction (FSYNC or FDSYNC), the we allocate a larger
349  *    log record here for the data and copy the data in.
350  * WR_NEED_COPY:
351  *    Otherwise we don't allocate a buffer, and *if* we need to
352  *    flush the write later then a buffer is allocated and
353  *    we retrieve the data using the dmu.
354  */
355 typedef enum {
356 	WR_INDIRECT,	/* indirect - a large write (dmu_sync() data */
357 			/* and put blkptr in log, rather than actual data) */
358 	WR_COPIED,	/* immediate - data is copied into lr_write_t */
359 	WR_NEED_COPY,	/* immediate - data needs to be copied if pushed */
360 	WR_NUM_STATES	/* number of states */
361 } itx_wr_state_t;
362 
363 typedef struct itx {
364 	list_node_t	itx_node;	/* linkage on zl_itx_list */
365 	void		*itx_private;	/* type-specific opaque data */
366 	itx_wr_state_t	itx_wr_state;	/* write state */
367 	uint8_t		itx_sync;	/* synchronous transaction */
368 	uint64_t	itx_sod;	/* record size on disk */
369 	lr_t		itx_lr;		/* common part of log record */
370 	/* followed by type-specific part of lr_xx_t and its immediate data */
371 } itx_t;
372 
373 typedef int zil_parse_blk_func_t(zilog_t *zilog, blkptr_t *bp, void *arg,
374     uint64_t txg);
375 typedef int zil_parse_lr_func_t(zilog_t *zilog, lr_t *lr, void *arg,
376     uint64_t txg);
377 typedef int zil_replay_func_t();
378 typedef int zil_get_data_t(void *arg, lr_write_t *lr, char *dbuf, zio_t *zio);
379 
380 extern int zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func,
381     zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg);
382 
383 extern void	zil_init(void);
384 extern void	zil_fini(void);
385 
386 extern zilog_t	*zil_alloc(objset_t *os, zil_header_t *zh_phys);
387 extern void	zil_free(zilog_t *zilog);
388 
389 extern zilog_t	*zil_open(objset_t *os, zil_get_data_t *get_data);
390 extern void	zil_close(zilog_t *zilog);
391 
392 extern void	zil_replay(objset_t *os, void *arg,
393     zil_replay_func_t *replay_func[TX_MAX_TYPE]);
394 extern boolean_t zil_replaying(zilog_t *zilog, dmu_tx_t *tx);
395 extern void	zil_destroy(zilog_t *zilog, boolean_t keep_first);
396 extern void	zil_rollback_destroy(zilog_t *zilog, dmu_tx_t *tx);
397 
398 extern itx_t	*zil_itx_create(uint64_t txtype, size_t lrsize);
399 extern void	zil_itx_destroy(itx_t *itx);
400 extern uint64_t zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx);
401 
402 extern void	zil_commit(zilog_t *zilog, uint64_t seq, uint64_t oid);
403 
404 extern int	zil_vdev_offline(const char *osname, void *txarg);
405 extern int	zil_claim(const char *osname, void *txarg);
406 extern int	zil_check_log_chain(const char *osname, void *txarg);
407 extern void	zil_sync(zilog_t *zilog, dmu_tx_t *tx);
408 extern void	zil_clean(zilog_t *zilog);
409 
410 extern int	zil_suspend(zilog_t *zilog);
411 extern void	zil_resume(zilog_t *zilog);
412 
413 extern void	zil_add_block(zilog_t *zilog, const blkptr_t *bp);
414 extern int	zil_bp_tree_add(zilog_t *zilog, const blkptr_t *bp);
415 
416 extern void	zil_set_logbias(zilog_t *zilog, uint64_t slogval);
417 
418 extern int zil_disable;
419 
420 #ifdef	__cplusplus
421 }
422 #endif
423 
424 #endif	/* _SYS_ZIL_H */
425