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