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