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.
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.
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]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
26 /* Portions Copyright 2010 Robert Milkowski */
31 #include <sys/types.h>
35 #include <sys/zio_crypt.h>
47 * Each objset has its own intent log. The log header (zil_header_t)
48 * for objset N's intent log is kept in the Nth object of the SPA's
49 * intent_log objset. The log header points to a chain of log blocks,
50 * each of which contains log records (i.e., transactions) followed by
51 * a log block trailer (zil_trailer_t). The format of a log record
52 * depends on the record (or transaction) type, but all records begin
53 * with a common structure that defines the type, length, and txg.
57 * Intent log header - this on disk structure holds fields to manage
58 * the log. All fields are 64 bit to easily handle cross architectures.
60 typedef struct zil_header
{
61 uint64_t zh_claim_txg
; /* txg in which log blocks were claimed */
62 uint64_t zh_replay_seq
; /* highest replayed sequence number */
63 blkptr_t zh_log
; /* log chain */
64 uint64_t zh_claim_blk_seq
; /* highest claimed block sequence number */
65 uint64_t zh_flags
; /* header flags */
66 uint64_t zh_claim_lr_seq
; /* highest claimed lr sequence number */
71 * zh_flags bit settings
73 #define ZIL_REPLAY_NEEDED 0x1 /* replay needed - internal only */
74 #define ZIL_CLAIM_LR_SEQ_VALID 0x2 /* zh_claim_lr_seq field is valid */
79 * Log blocks are chained together. Originally they were chained at the
80 * end of the block. For performance reasons the chain was moved to the
81 * beginning of the block which allows writes for only the data being used.
82 * The older position is supported for backwards compatability.
84 * The zio_eck_t contains a zec_cksum which for the intent log is
85 * the sequence number of this log block. A seq of 0 is invalid.
86 * The zec_cksum is checked by the SPA against the sequence
87 * number passed in the blk_cksum field of the blkptr_t
89 typedef struct zil_chain
{
91 blkptr_t zc_next_blk
; /* next block in chain */
92 uint64_t zc_nused
; /* bytes in log block used */
93 zio_eck_t zc_eck
; /* block trailer */
96 #define ZIL_MIN_BLKSZ 4096ULL
99 * ziltest is by and large an ugly hack, but very useful in
100 * checking replay without tedious work.
101 * When running ziltest we want to keep all itx's and so maintain
102 * a single list in the zl_itxg[] that uses a high txg: ZILTEST_TXG
103 * We subtract TXG_CONCURRENT_STATES to allow for common code.
105 #define ZILTEST_TXG (UINT64_MAX - TXG_CONCURRENT_STATES)
108 * The words of a log block checksum.
110 #define ZIL_ZC_GUID_0 0
111 #define ZIL_ZC_GUID_1 1
112 #define ZIL_ZC_OBJSET 2
115 typedef enum zil_create
{
122 * size of xvattr log section.
123 * its composed of lr_attr_t + xvattr bitmap + 2 64 bit timestamps
124 * for create time and a single 64 bit integer for all of the attributes,
125 * and 4 64 bit integers (32 bytes) for the scanstamp.
129 #define ZIL_XVAT_SIZE(mapsize) \
130 sizeof (lr_attr_t) + (sizeof (uint32_t) * (mapsize - 1)) + \
131 (sizeof (uint64_t) * 7)
134 * Size of ACL in log. The ACE data is padded out to properly align
135 * on 8 byte boundary.
138 #define ZIL_ACE_LENGTH(x) (roundup(x, sizeof (uint64_t)))
141 * Intent log transaction types and record structures
143 #define TX_CREATE 1 /* Create file */
144 #define TX_MKDIR 2 /* Make directory */
145 #define TX_MKXATTR 3 /* Make XATTR directory */
146 #define TX_SYMLINK 4 /* Create symbolic link to a file */
147 #define TX_REMOVE 5 /* Remove file */
148 #define TX_RMDIR 6 /* Remove directory */
149 #define TX_LINK 7 /* Create hard link to a file */
150 #define TX_RENAME 8 /* Rename a file */
151 #define TX_WRITE 9 /* File write */
152 #define TX_TRUNCATE 10 /* Truncate a file */
153 #define TX_SETATTR 11 /* Set file attributes */
154 #define TX_ACL_V0 12 /* Set old formatted ACL */
155 #define TX_ACL 13 /* Set ACL */
156 #define TX_CREATE_ACL 14 /* create with ACL */
157 #define TX_CREATE_ATTR 15 /* create + attrs */
158 #define TX_CREATE_ACL_ATTR 16 /* create with ACL + attrs */
159 #define TX_MKDIR_ACL 17 /* mkdir with ACL */
160 #define TX_MKDIR_ATTR 18 /* mkdir with attr */
161 #define TX_MKDIR_ACL_ATTR 19 /* mkdir with ACL + attrs */
162 #define TX_WRITE2 20 /* dmu_sync EALREADY write */
163 #define TX_MAX_TYPE 21 /* Max transaction type */
166 * The transactions for mkdir, symlink, remove, rmdir, link, and rename
167 * may have the following bit set, indicating the original request
168 * specified case-insensitive handling of names.
170 #define TX_CI ((uint64_t)0x1 << 63) /* case-insensitive behavior requested */
173 * Transactions for write, truncate, setattr, acl_v0, and acl can be logged
174 * out of order. For convenience in the code, all such records must have
175 * lr_foid at the same offset.
177 #define TX_OOO(txtype) \
178 ((txtype) == TX_WRITE || \
179 (txtype) == TX_TRUNCATE || \
180 (txtype) == TX_SETATTR || \
181 (txtype) == TX_ACL_V0 || \
182 (txtype) == TX_ACL || \
183 (txtype) == TX_WRITE2)
186 * The number of dnode slots consumed by the object is stored in the 8
187 * unused upper bits of the object ID. We subtract 1 from the value
188 * stored on disk for compatibility with implementations that don't
189 * support large dnodes. The slot count for a single-slot dnode will
190 * contain 0 for those bits to preserve the log record format for
193 #define LR_FOID_GET_SLOTS(oid) (BF64_GET((oid), 56, 8) + 1)
194 #define LR_FOID_SET_SLOTS(oid, x) BF64_SET((oid), 56, 8, (x) - 1)
195 #define LR_FOID_GET_OBJ(oid) BF64_GET((oid), 0, DN_MAX_OBJECT_SHIFT)
196 #define LR_FOID_SET_OBJ(oid, x) BF64_SET((oid), 0, DN_MAX_OBJECT_SHIFT, (x))
199 * Format of log records.
200 * The fields are carefully defined to allow them to be aligned
201 * and sized the same on sparc & intel architectures.
202 * Each log record has a common structure at the beginning.
204 * The log record on disk (lrc_seq) holds the sequence number of all log
205 * records which is used to ensure we don't replay the same record.
207 typedef struct { /* common log record header */
208 uint64_t lrc_txtype
; /* intent log transaction type */
209 uint64_t lrc_reclen
; /* transaction record length */
210 uint64_t lrc_txg
; /* dmu transaction group number */
211 uint64_t lrc_seq
; /* see comment above */
215 * Common start of all out-of-order record types (TX_OOO() above).
218 lr_t lr_common
; /* common portion of log record */
219 uint64_t lr_foid
; /* object id */
223 * Handle option extended vattr attributes.
225 * Whenever new attributes are added the version number
226 * will need to be updated as will code in
227 * zfs_log.c and zfs_replay.c
230 uint32_t lr_attr_masksize
; /* number of elements in array */
231 uint32_t lr_attr_bitmap
; /* First entry of array */
232 /* remainder of array and any additional fields */
236 * log record for creates without optional ACL.
237 * This log record does support optional xvattr_t attributes.
240 lr_t lr_common
; /* common portion of log record */
241 uint64_t lr_doid
; /* object id of directory */
242 uint64_t lr_foid
; /* object id of created file object */
243 uint64_t lr_mode
; /* mode of object */
244 uint64_t lr_uid
; /* uid of object */
245 uint64_t lr_gid
; /* gid of object */
246 uint64_t lr_gen
; /* generation (txg of creation) */
247 uint64_t lr_crtime
[2]; /* creation time */
248 uint64_t lr_rdev
; /* rdev of object to create */
249 /* name of object to create follows this */
250 /* for symlinks, link content follows name */
251 /* for creates with xvattr data, the name follows the xvattr info */
255 * FUID ACL record will be an array of ACEs from the original ACL.
256 * If this array includes ephemeral IDs, the record will also include
257 * an array of log-specific FUIDs to replace the ephemeral IDs.
258 * Only one copy of each unique domain will be present, so the log-specific
259 * FUIDs will use an index into a compressed domain table. On replay this
260 * information will be used to construct real FUIDs (and bypass idmap,
261 * since it may not be available).
265 * Log record for creates with optional ACL
266 * This log record is also used for recording any FUID
267 * information needed for replaying the create. If the
268 * file doesn't have any actual ACEs then the lr_aclcnt
271 * After lr_acl_flags, there are a lr_acl_bytes number of variable sized ace's.
272 * If create is also setting xvattr's, then acl data follows xvattr.
273 * If ACE FUIDs are needed then they will follow the xvattr_t. Following
274 * the FUIDs will be the domain table information. The FUIDs for the owner
275 * and group will be in lr_create. Name follows ACL data.
278 lr_create_t lr_create
; /* common create portion */
279 uint64_t lr_aclcnt
; /* number of ACEs in ACL */
280 uint64_t lr_domcnt
; /* number of unique domains */
281 uint64_t lr_fuidcnt
; /* number of real fuids */
282 uint64_t lr_acl_bytes
; /* number of bytes in ACL */
283 uint64_t lr_acl_flags
; /* ACL flags */
287 lr_t lr_common
; /* common portion of log record */
288 uint64_t lr_doid
; /* obj id of directory */
289 /* name of object to remove follows this */
293 lr_t lr_common
; /* common portion of log record */
294 uint64_t lr_doid
; /* obj id of directory */
295 uint64_t lr_link_obj
; /* obj id of link */
296 /* name of object to link follows this */
300 lr_t lr_common
; /* common portion of log record */
301 uint64_t lr_sdoid
; /* obj id of source directory */
302 uint64_t lr_tdoid
; /* obj id of target directory */
303 /* 2 strings: names of source and destination follow this */
307 lr_t lr_common
; /* common portion of log record */
308 uint64_t lr_foid
; /* file object to write */
309 uint64_t lr_offset
; /* offset to write to */
310 uint64_t lr_length
; /* user data length to write */
311 uint64_t lr_blkoff
; /* no longer used */
312 blkptr_t lr_blkptr
; /* spa block pointer for replay */
313 /* write data will follow for small writes */
317 lr_t lr_common
; /* common portion of log record */
318 uint64_t lr_foid
; /* object id of file to truncate */
319 uint64_t lr_offset
; /* offset to truncate from */
320 uint64_t lr_length
; /* length to truncate */
324 lr_t lr_common
; /* common portion of log record */
325 uint64_t lr_foid
; /* file object to change attributes */
326 uint64_t lr_mask
; /* mask of attributes to set */
327 uint64_t lr_mode
; /* mode to set */
328 uint64_t lr_uid
; /* uid to set */
329 uint64_t lr_gid
; /* gid to set */
330 uint64_t lr_size
; /* size to set */
331 uint64_t lr_atime
[2]; /* access time */
332 uint64_t lr_mtime
[2]; /* modification time */
333 /* optional attribute lr_attr_t may be here */
337 lr_t lr_common
; /* common portion of log record */
338 uint64_t lr_foid
; /* obj id of file */
339 uint64_t lr_aclcnt
; /* number of acl entries */
340 /* lr_aclcnt number of ace_t entries follow this */
344 lr_t lr_common
; /* common portion of log record */
345 uint64_t lr_foid
; /* obj id of file */
346 uint64_t lr_aclcnt
; /* number of ACEs in ACL */
347 uint64_t lr_domcnt
; /* number of unique domains */
348 uint64_t lr_fuidcnt
; /* number of real fuids */
349 uint64_t lr_acl_bytes
; /* number of bytes in ACL */
350 uint64_t lr_acl_flags
; /* ACL flags */
351 /* lr_acl_bytes number of variable sized ace's follows */
355 * ZIL structure definitions, interface function prototype and globals.
359 * Writes are handled in three different ways:
362 * In this mode, if we need to commit the write later, then the block
363 * is immediately written into the file system (using dmu_sync),
364 * and a pointer to the block is put into the log record.
365 * When the txg commits the block is linked in.
366 * This saves additionally writing the data into the log record.
367 * There are a few requirements for this to occur:
368 * - write is greater than zfs/zvol_immediate_write_sz
369 * - not using slogs (as slogs are assumed to always be faster
370 * than writing into the main pool)
371 * - the write occupies only one block
373 * If we know we'll immediately be committing the
374 * transaction (FSYNC or FDSYNC), then we allocate a larger
375 * log record here for the data and copy the data in.
377 * Otherwise we don't allocate a buffer, and *if* we need to
378 * flush the write later then a buffer is allocated and
379 * we retrieve the data using the dmu.
382 WR_INDIRECT
, /* indirect - a large write (dmu_sync() data */
383 /* and put blkptr in log, rather than actual data) */
384 WR_COPIED
, /* immediate - data is copied into lr_write_t */
385 WR_NEED_COPY
, /* immediate - data needs to be copied if pushed */
386 WR_NUM_STATES
/* number of states */
389 typedef void (*zil_callback_t
)(void *data
);
392 list_node_t itx_node
; /* linkage on zl_itx_list */
393 void *itx_private
; /* type-specific opaque data */
394 itx_wr_state_t itx_wr_state
; /* write state */
395 uint8_t itx_sync
; /* synchronous transaction */
396 zil_callback_t itx_callback
; /* Called when the itx is persistent */
397 void *itx_callback_data
; /* User data for the callback */
398 uint64_t itx_oid
; /* object id */
399 lr_t itx_lr
; /* common part of log record */
400 /* followed by type-specific part of lr_xx_t and its immediate data */
404 * Used for zil kstat.
406 typedef struct zil_stats
{
408 * Number of times a ZIL commit (e.g. fsync) has been requested.
410 kstat_named_t zil_commit_count
;
413 * Number of times the ZIL has been flushed to stable storage.
414 * This is less than zil_commit_count when commits are "merged"
415 * (see the documentation above zil_commit()).
417 kstat_named_t zil_commit_writer_count
;
420 * Number of transactions (reads, writes, renames, etc.)
421 * that have been commited.
423 kstat_named_t zil_itx_count
;
426 * See the documentation for itx_wr_state_t above.
427 * Note that "bytes" accumulates the length of the transactions
428 * (i.e. data), not the actual log record sizes.
430 kstat_named_t zil_itx_indirect_count
;
431 kstat_named_t zil_itx_indirect_bytes
;
432 kstat_named_t zil_itx_copied_count
;
433 kstat_named_t zil_itx_copied_bytes
;
434 kstat_named_t zil_itx_needcopy_count
;
435 kstat_named_t zil_itx_needcopy_bytes
;
438 * Transactions which have been allocated to the "normal"
439 * (i.e. not slog) storage pool. Note that "bytes" accumulate
440 * the actual log record sizes - which do not include the actual
441 * data in case of indirect writes.
443 kstat_named_t zil_itx_metaslab_normal_count
;
444 kstat_named_t zil_itx_metaslab_normal_bytes
;
447 * Transactions which have been allocated to the "slog" storage pool.
448 * If there are no separate log devices, this is the same as the
451 kstat_named_t zil_itx_metaslab_slog_count
;
452 kstat_named_t zil_itx_metaslab_slog_bytes
;
455 extern zil_stats_t zil_stats
;
457 #define ZIL_STAT_INCR(stat, val) \
458 atomic_add_64(&zil_stats.stat.value.ui64, (val));
459 #define ZIL_STAT_BUMP(stat) \
460 ZIL_STAT_INCR(stat, 1);
462 typedef int zil_parse_blk_func_t(zilog_t
*zilog
, blkptr_t
*bp
, void *arg
,
464 typedef int zil_parse_lr_func_t(zilog_t
*zilog
, lr_t
*lr
, void *arg
,
466 typedef int (*const zil_replay_func_t
)(void *, char *, boolean_t
);
467 typedef int zil_get_data_t(void *arg
, lr_write_t
*lr
, char *dbuf
, zio_t
*zio
);
469 extern int zil_parse(zilog_t
*zilog
, zil_parse_blk_func_t
*parse_blk_func
,
470 zil_parse_lr_func_t
*parse_lr_func
, void *arg
, uint64_t txg
,
473 extern void zil_init(void);
474 extern void zil_fini(void);
476 extern zilog_t
*zil_alloc(objset_t
*os
, zil_header_t
*zh_phys
);
477 extern void zil_free(zilog_t
*zilog
);
479 extern zilog_t
*zil_open(objset_t
*os
, zil_get_data_t
*get_data
);
480 extern void zil_close(zilog_t
*zilog
);
482 extern void zil_replay(objset_t
*os
, void *arg
,
483 zil_replay_func_t replay_func
[TX_MAX_TYPE
]);
484 extern boolean_t
zil_replaying(zilog_t
*zilog
, dmu_tx_t
*tx
);
485 extern void zil_destroy(zilog_t
*zilog
, boolean_t keep_first
);
486 extern void zil_destroy_sync(zilog_t
*zilog
, dmu_tx_t
*tx
);
488 extern itx_t
*zil_itx_create(uint64_t txtype
, size_t lrsize
);
489 extern void zil_itx_destroy(itx_t
*itx
);
490 extern void zil_itx_assign(zilog_t
*zilog
, itx_t
*itx
, dmu_tx_t
*tx
);
492 extern void zil_commit(zilog_t
*zilog
, uint64_t oid
);
494 extern int zil_vdev_offline(const char *osname
, void *txarg
);
495 extern int zil_claim(struct dsl_pool
*dp
,
496 struct dsl_dataset
*ds
, void *txarg
);
497 extern int zil_check_log_chain(struct dsl_pool
*dp
,
498 struct dsl_dataset
*ds
, void *tx
);
499 extern void zil_sync(zilog_t
*zilog
, dmu_tx_t
*tx
);
500 extern void zil_clean(zilog_t
*zilog
, uint64_t synced_txg
);
502 extern int zil_suspend(const char *osname
, void **cookiep
);
503 extern void zil_resume(void *cookie
);
505 extern void zil_add_block(zilog_t
*zilog
, const blkptr_t
*bp
);
506 extern int zil_bp_tree_add(zilog_t
*zilog
, const blkptr_t
*bp
);
508 extern void zil_set_sync(zilog_t
*zilog
, uint64_t syncval
);
510 extern void zil_set_logbias(zilog_t
*zilog
, uint64_t slogval
);
512 extern int zil_replay_disable
;
518 #endif /* _SYS_ZIL_H */