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