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