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) 2011, 2015 by Delphix. All rights reserved.
26 /* Portions Copyright 2010 Robert Milkowski */
28 #include <sys/zfs_context.h>
34 #include <sys/resource.h>
36 #include <sys/zil_impl.h>
37 #include <sys/dsl_dataset.h>
38 #include <sys/vdev_impl.h>
39 #include <sys/dmu_tx.h>
40 #include <sys/dsl_pool.h>
41 #include <sys/metaslab.h>
42 #include <sys/trace_zil.h>
46 * The zfs intent log (ZIL) saves transaction records of system calls
47 * that change the file system in memory with enough information
48 * to be able to replay them. These are stored in memory until
49 * either the DMU transaction group (txg) commits them to the stable pool
50 * and they can be discarded, or they are flushed to the stable log
51 * (also in the pool) due to a fsync, O_DSYNC or other synchronous
52 * requirement. In the event of a panic or power fail then those log
53 * records (transactions) are replayed.
55 * There is one ZIL per file system. Its on-disk (pool) format consists
62 * A log record holds a system call transaction. Log blocks can
63 * hold many log records and the blocks are chained together.
64 * Each ZIL block contains a block pointer (blkptr_t) to the next
65 * ZIL block in the chain. The ZIL header points to the first
66 * block in the chain. Note there is not a fixed place in the pool
67 * to hold blocks. They are dynamically allocated and freed as
68 * needed from the blocks available. Figure X shows the ZIL structure:
72 * See zil.h for more information about these fields.
74 zil_stats_t zil_stats
= {
75 { "zil_commit_count", KSTAT_DATA_UINT64
},
76 { "zil_commit_writer_count", KSTAT_DATA_UINT64
},
77 { "zil_itx_count", KSTAT_DATA_UINT64
},
78 { "zil_itx_indirect_count", KSTAT_DATA_UINT64
},
79 { "zil_itx_indirect_bytes", KSTAT_DATA_UINT64
},
80 { "zil_itx_copied_count", KSTAT_DATA_UINT64
},
81 { "zil_itx_copied_bytes", KSTAT_DATA_UINT64
},
82 { "zil_itx_needcopy_count", KSTAT_DATA_UINT64
},
83 { "zil_itx_needcopy_bytes", KSTAT_DATA_UINT64
},
84 { "zil_itx_metaslab_normal_count", KSTAT_DATA_UINT64
},
85 { "zil_itx_metaslab_normal_bytes", KSTAT_DATA_UINT64
},
86 { "zil_itx_metaslab_slog_count", KSTAT_DATA_UINT64
},
87 { "zil_itx_metaslab_slog_bytes", KSTAT_DATA_UINT64
},
90 static kstat_t
*zil_ksp
;
93 * Disable intent logging replay. This global ZIL switch affects all pools.
95 int zil_replay_disable
= 0;
98 * Tunable parameter for debugging or performance analysis. Setting
99 * zfs_nocacheflush will cause corruption on power loss if a volatile
100 * out-of-order write cache is enabled.
102 int zfs_nocacheflush
= 0;
104 static kmem_cache_t
*zil_lwb_cache
;
106 static void zil_async_to_sync(zilog_t
*zilog
, uint64_t foid
);
108 #define LWB_EMPTY(lwb) ((BP_GET_LSIZE(&lwb->lwb_blk) - \
109 sizeof (zil_chain_t)) == (lwb->lwb_sz - lwb->lwb_nused))
113 * ziltest is by and large an ugly hack, but very useful in
114 * checking replay without tedious work.
115 * When running ziltest we want to keep all itx's and so maintain
116 * a single list in the zl_itxg[] that uses a high txg: ZILTEST_TXG
117 * We subtract TXG_CONCURRENT_STATES to allow for common code.
119 #define ZILTEST_TXG (UINT64_MAX - TXG_CONCURRENT_STATES)
122 zil_bp_compare(const void *x1
, const void *x2
)
124 const dva_t
*dva1
= &((zil_bp_node_t
*)x1
)->zn_dva
;
125 const dva_t
*dva2
= &((zil_bp_node_t
*)x2
)->zn_dva
;
127 int cmp
= AVL_CMP(DVA_GET_VDEV(dva1
), DVA_GET_VDEV(dva2
));
131 return (AVL_CMP(DVA_GET_OFFSET(dva1
), DVA_GET_OFFSET(dva2
)));
135 zil_bp_tree_init(zilog_t
*zilog
)
137 avl_create(&zilog
->zl_bp_tree
, zil_bp_compare
,
138 sizeof (zil_bp_node_t
), offsetof(zil_bp_node_t
, zn_node
));
142 zil_bp_tree_fini(zilog_t
*zilog
)
144 avl_tree_t
*t
= &zilog
->zl_bp_tree
;
148 while ((zn
= avl_destroy_nodes(t
, &cookie
)) != NULL
)
149 kmem_free(zn
, sizeof (zil_bp_node_t
));
155 zil_bp_tree_add(zilog_t
*zilog
, const blkptr_t
*bp
)
157 avl_tree_t
*t
= &zilog
->zl_bp_tree
;
162 if (BP_IS_EMBEDDED(bp
))
165 dva
= BP_IDENTITY(bp
);
167 if (avl_find(t
, dva
, &where
) != NULL
)
168 return (SET_ERROR(EEXIST
));
170 zn
= kmem_alloc(sizeof (zil_bp_node_t
), KM_SLEEP
);
172 avl_insert(t
, zn
, where
);
177 static zil_header_t
*
178 zil_header_in_syncing_context(zilog_t
*zilog
)
180 return ((zil_header_t
*)zilog
->zl_header
);
184 zil_init_log_chain(zilog_t
*zilog
, blkptr_t
*bp
)
186 zio_cksum_t
*zc
= &bp
->blk_cksum
;
188 zc
->zc_word
[ZIL_ZC_GUID_0
] = spa_get_random(-1ULL);
189 zc
->zc_word
[ZIL_ZC_GUID_1
] = spa_get_random(-1ULL);
190 zc
->zc_word
[ZIL_ZC_OBJSET
] = dmu_objset_id(zilog
->zl_os
);
191 zc
->zc_word
[ZIL_ZC_SEQ
] = 1ULL;
195 * Read a log block and make sure it's valid.
198 zil_read_log_block(zilog_t
*zilog
, const blkptr_t
*bp
, blkptr_t
*nbp
, void *dst
,
201 enum zio_flag zio_flags
= ZIO_FLAG_CANFAIL
;
202 arc_flags_t aflags
= ARC_FLAG_WAIT
;
203 arc_buf_t
*abuf
= NULL
;
207 if (zilog
->zl_header
->zh_claim_txg
== 0)
208 zio_flags
|= ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_SCRUB
;
210 if (!(zilog
->zl_header
->zh_flags
& ZIL_CLAIM_LR_SEQ_VALID
))
211 zio_flags
|= ZIO_FLAG_SPECULATIVE
;
213 SET_BOOKMARK(&zb
, bp
->blk_cksum
.zc_word
[ZIL_ZC_OBJSET
],
214 ZB_ZIL_OBJECT
, ZB_ZIL_LEVEL
, bp
->blk_cksum
.zc_word
[ZIL_ZC_SEQ
]);
216 error
= arc_read(NULL
, zilog
->zl_spa
, bp
, arc_getbuf_func
, &abuf
,
217 ZIO_PRIORITY_SYNC_READ
, zio_flags
, &aflags
, &zb
);
220 zio_cksum_t cksum
= bp
->blk_cksum
;
223 * Validate the checksummed log block.
225 * Sequence numbers should be... sequential. The checksum
226 * verifier for the next block should be bp's checksum plus 1.
228 * Also check the log chain linkage and size used.
230 cksum
.zc_word
[ZIL_ZC_SEQ
]++;
232 if (BP_GET_CHECKSUM(bp
) == ZIO_CHECKSUM_ZILOG2
) {
233 zil_chain_t
*zilc
= abuf
->b_data
;
234 char *lr
= (char *)(zilc
+ 1);
235 uint64_t len
= zilc
->zc_nused
- sizeof (zil_chain_t
);
237 if (bcmp(&cksum
, &zilc
->zc_next_blk
.blk_cksum
,
238 sizeof (cksum
)) || BP_IS_HOLE(&zilc
->zc_next_blk
)) {
239 error
= SET_ERROR(ECKSUM
);
241 ASSERT3U(len
, <=, SPA_OLD_MAXBLOCKSIZE
);
243 *end
= (char *)dst
+ len
;
244 *nbp
= zilc
->zc_next_blk
;
247 char *lr
= abuf
->b_data
;
248 uint64_t size
= BP_GET_LSIZE(bp
);
249 zil_chain_t
*zilc
= (zil_chain_t
*)(lr
+ size
) - 1;
251 if (bcmp(&cksum
, &zilc
->zc_next_blk
.blk_cksum
,
252 sizeof (cksum
)) || BP_IS_HOLE(&zilc
->zc_next_blk
) ||
253 (zilc
->zc_nused
> (size
- sizeof (*zilc
)))) {
254 error
= SET_ERROR(ECKSUM
);
256 ASSERT3U(zilc
->zc_nused
, <=,
257 SPA_OLD_MAXBLOCKSIZE
);
258 bcopy(lr
, dst
, zilc
->zc_nused
);
259 *end
= (char *)dst
+ zilc
->zc_nused
;
260 *nbp
= zilc
->zc_next_blk
;
264 arc_buf_destroy(abuf
, &abuf
);
271 * Read a TX_WRITE log data block.
274 zil_read_log_data(zilog_t
*zilog
, const lr_write_t
*lr
, void *wbuf
)
276 enum zio_flag zio_flags
= ZIO_FLAG_CANFAIL
;
277 const blkptr_t
*bp
= &lr
->lr_blkptr
;
278 arc_flags_t aflags
= ARC_FLAG_WAIT
;
279 arc_buf_t
*abuf
= NULL
;
283 if (BP_IS_HOLE(bp
)) {
285 bzero(wbuf
, MAX(BP_GET_LSIZE(bp
), lr
->lr_length
));
289 if (zilog
->zl_header
->zh_claim_txg
== 0)
290 zio_flags
|= ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_SCRUB
;
292 SET_BOOKMARK(&zb
, dmu_objset_id(zilog
->zl_os
), lr
->lr_foid
,
293 ZB_ZIL_LEVEL
, lr
->lr_offset
/ BP_GET_LSIZE(bp
));
295 error
= arc_read(NULL
, zilog
->zl_spa
, bp
, arc_getbuf_func
, &abuf
,
296 ZIO_PRIORITY_SYNC_READ
, zio_flags
, &aflags
, &zb
);
300 bcopy(abuf
->b_data
, wbuf
, arc_buf_size(abuf
));
301 arc_buf_destroy(abuf
, &abuf
);
308 * Parse the intent log, and call parse_func for each valid record within.
311 zil_parse(zilog_t
*zilog
, zil_parse_blk_func_t
*parse_blk_func
,
312 zil_parse_lr_func_t
*parse_lr_func
, void *arg
, uint64_t txg
)
314 const zil_header_t
*zh
= zilog
->zl_header
;
315 boolean_t claimed
= !!zh
->zh_claim_txg
;
316 uint64_t claim_blk_seq
= claimed
? zh
->zh_claim_blk_seq
: UINT64_MAX
;
317 uint64_t claim_lr_seq
= claimed
? zh
->zh_claim_lr_seq
: UINT64_MAX
;
318 uint64_t max_blk_seq
= 0;
319 uint64_t max_lr_seq
= 0;
320 uint64_t blk_count
= 0;
321 uint64_t lr_count
= 0;
322 blkptr_t blk
, next_blk
;
326 bzero(&next_blk
, sizeof (blkptr_t
));
329 * Old logs didn't record the maximum zh_claim_lr_seq.
331 if (!(zh
->zh_flags
& ZIL_CLAIM_LR_SEQ_VALID
))
332 claim_lr_seq
= UINT64_MAX
;
335 * Starting at the block pointed to by zh_log we read the log chain.
336 * For each block in the chain we strongly check that block to
337 * ensure its validity. We stop when an invalid block is found.
338 * For each block pointer in the chain we call parse_blk_func().
339 * For each record in each valid block we call parse_lr_func().
340 * If the log has been claimed, stop if we encounter a sequence
341 * number greater than the highest claimed sequence number.
343 lrbuf
= zio_buf_alloc(SPA_OLD_MAXBLOCKSIZE
);
344 zil_bp_tree_init(zilog
);
346 for (blk
= zh
->zh_log
; !BP_IS_HOLE(&blk
); blk
= next_blk
) {
347 uint64_t blk_seq
= blk
.blk_cksum
.zc_word
[ZIL_ZC_SEQ
];
351 if (blk_seq
> claim_blk_seq
)
353 if ((error
= parse_blk_func(zilog
, &blk
, arg
, txg
)) != 0)
355 ASSERT3U(max_blk_seq
, <, blk_seq
);
356 max_blk_seq
= blk_seq
;
359 if (max_lr_seq
== claim_lr_seq
&& max_blk_seq
== claim_blk_seq
)
362 error
= zil_read_log_block(zilog
, &blk
, &next_blk
, lrbuf
, &end
);
366 for (lrp
= lrbuf
; lrp
< end
; lrp
+= reclen
) {
367 lr_t
*lr
= (lr_t
*)lrp
;
368 reclen
= lr
->lrc_reclen
;
369 ASSERT3U(reclen
, >=, sizeof (lr_t
));
370 if (lr
->lrc_seq
> claim_lr_seq
)
372 if ((error
= parse_lr_func(zilog
, lr
, arg
, txg
)) != 0)
374 ASSERT3U(max_lr_seq
, <, lr
->lrc_seq
);
375 max_lr_seq
= lr
->lrc_seq
;
380 zilog
->zl_parse_error
= error
;
381 zilog
->zl_parse_blk_seq
= max_blk_seq
;
382 zilog
->zl_parse_lr_seq
= max_lr_seq
;
383 zilog
->zl_parse_blk_count
= blk_count
;
384 zilog
->zl_parse_lr_count
= lr_count
;
386 ASSERT(!claimed
|| !(zh
->zh_flags
& ZIL_CLAIM_LR_SEQ_VALID
) ||
387 (max_blk_seq
== claim_blk_seq
&& max_lr_seq
== claim_lr_seq
));
389 zil_bp_tree_fini(zilog
);
390 zio_buf_free(lrbuf
, SPA_OLD_MAXBLOCKSIZE
);
396 zil_claim_log_block(zilog_t
*zilog
, blkptr_t
*bp
, void *tx
, uint64_t first_txg
)
399 * Claim log block if not already committed and not already claimed.
400 * If tx == NULL, just verify that the block is claimable.
402 if (BP_IS_HOLE(bp
) || bp
->blk_birth
< first_txg
||
403 zil_bp_tree_add(zilog
, bp
) != 0)
406 return (zio_wait(zio_claim(NULL
, zilog
->zl_spa
,
407 tx
== NULL
? 0 : first_txg
, bp
, spa_claim_notify
, NULL
,
408 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_SCRUB
)));
412 zil_claim_log_record(zilog_t
*zilog
, lr_t
*lrc
, void *tx
, uint64_t first_txg
)
414 lr_write_t
*lr
= (lr_write_t
*)lrc
;
417 if (lrc
->lrc_txtype
!= TX_WRITE
)
421 * If the block is not readable, don't claim it. This can happen
422 * in normal operation when a log block is written to disk before
423 * some of the dmu_sync() blocks it points to. In this case, the
424 * transaction cannot have been committed to anyone (we would have
425 * waited for all writes to be stable first), so it is semantically
426 * correct to declare this the end of the log.
428 if (lr
->lr_blkptr
.blk_birth
>= first_txg
&&
429 (error
= zil_read_log_data(zilog
, lr
, NULL
)) != 0)
431 return (zil_claim_log_block(zilog
, &lr
->lr_blkptr
, tx
, first_txg
));
436 zil_free_log_block(zilog_t
*zilog
, blkptr_t
*bp
, void *tx
, uint64_t claim_txg
)
438 zio_free_zil(zilog
->zl_spa
, dmu_tx_get_txg(tx
), bp
);
444 zil_free_log_record(zilog_t
*zilog
, lr_t
*lrc
, void *tx
, uint64_t claim_txg
)
446 lr_write_t
*lr
= (lr_write_t
*)lrc
;
447 blkptr_t
*bp
= &lr
->lr_blkptr
;
450 * If we previously claimed it, we need to free it.
452 if (claim_txg
!= 0 && lrc
->lrc_txtype
== TX_WRITE
&&
453 bp
->blk_birth
>= claim_txg
&& zil_bp_tree_add(zilog
, bp
) == 0 &&
455 zio_free(zilog
->zl_spa
, dmu_tx_get_txg(tx
), bp
);
461 zil_alloc_lwb(zilog_t
*zilog
, blkptr_t
*bp
, uint64_t txg
, boolean_t fastwrite
)
465 lwb
= kmem_cache_alloc(zil_lwb_cache
, KM_SLEEP
);
466 lwb
->lwb_zilog
= zilog
;
468 lwb
->lwb_fastwrite
= fastwrite
;
469 lwb
->lwb_buf
= zio_buf_alloc(BP_GET_LSIZE(bp
));
470 lwb
->lwb_max_txg
= txg
;
473 if (BP_GET_CHECKSUM(bp
) == ZIO_CHECKSUM_ZILOG2
) {
474 lwb
->lwb_nused
= sizeof (zil_chain_t
);
475 lwb
->lwb_sz
= BP_GET_LSIZE(bp
);
478 lwb
->lwb_sz
= BP_GET_LSIZE(bp
) - sizeof (zil_chain_t
);
481 mutex_enter(&zilog
->zl_lock
);
482 list_insert_tail(&zilog
->zl_lwb_list
, lwb
);
483 mutex_exit(&zilog
->zl_lock
);
489 * Called when we create in-memory log transactions so that we know
490 * to cleanup the itxs at the end of spa_sync().
493 zilog_dirty(zilog_t
*zilog
, uint64_t txg
)
495 dsl_pool_t
*dp
= zilog
->zl_dmu_pool
;
496 dsl_dataset_t
*ds
= dmu_objset_ds(zilog
->zl_os
);
498 if (ds
->ds_is_snapshot
)
499 panic("dirtying snapshot!");
501 if (txg_list_add(&dp
->dp_dirty_zilogs
, zilog
, txg
)) {
502 /* up the hold count until we can be written out */
503 dmu_buf_add_ref(ds
->ds_dbuf
, zilog
);
508 zilog_is_dirty(zilog_t
*zilog
)
510 dsl_pool_t
*dp
= zilog
->zl_dmu_pool
;
513 for (t
= 0; t
< TXG_SIZE
; t
++) {
514 if (txg_list_member(&dp
->dp_dirty_zilogs
, zilog
, t
))
521 * Create an on-disk intent log.
524 zil_create(zilog_t
*zilog
)
526 const zil_header_t
*zh
= zilog
->zl_header
;
532 boolean_t fastwrite
= FALSE
;
535 * Wait for any previous destroy to complete.
537 txg_wait_synced(zilog
->zl_dmu_pool
, zilog
->zl_destroy_txg
);
539 ASSERT(zh
->zh_claim_txg
== 0);
540 ASSERT(zh
->zh_replay_seq
== 0);
545 * Allocate an initial log block if:
546 * - there isn't one already
547 * - the existing block is the wrong endianess
549 if (BP_IS_HOLE(&blk
) || BP_SHOULD_BYTESWAP(&blk
)) {
550 tx
= dmu_tx_create(zilog
->zl_os
);
551 VERIFY(dmu_tx_assign(tx
, TXG_WAIT
) == 0);
552 dsl_dataset_dirty(dmu_objset_ds(zilog
->zl_os
), tx
);
553 txg
= dmu_tx_get_txg(tx
);
555 if (!BP_IS_HOLE(&blk
)) {
556 zio_free_zil(zilog
->zl_spa
, txg
, &blk
);
560 error
= zio_alloc_zil(zilog
->zl_spa
, txg
, &blk
,
561 ZIL_MIN_BLKSZ
, B_TRUE
);
565 zil_init_log_chain(zilog
, &blk
);
569 * Allocate a log write buffer (lwb) for the first log block.
572 lwb
= zil_alloc_lwb(zilog
, &blk
, txg
, fastwrite
);
575 * If we just allocated the first log block, commit our transaction
576 * and wait for zil_sync() to stuff the block poiner into zh_log.
577 * (zh is part of the MOS, so we cannot modify it in open context.)
581 txg_wait_synced(zilog
->zl_dmu_pool
, txg
);
584 ASSERT(bcmp(&blk
, &zh
->zh_log
, sizeof (blk
)) == 0);
590 * In one tx, free all log blocks and clear the log header.
591 * If keep_first is set, then we're replaying a log with no content.
592 * We want to keep the first block, however, so that the first
593 * synchronous transaction doesn't require a txg_wait_synced()
594 * in zil_create(). We don't need to txg_wait_synced() here either
595 * when keep_first is set, because both zil_create() and zil_destroy()
596 * will wait for any in-progress destroys to complete.
599 zil_destroy(zilog_t
*zilog
, boolean_t keep_first
)
601 const zil_header_t
*zh
= zilog
->zl_header
;
607 * Wait for any previous destroy to complete.
609 txg_wait_synced(zilog
->zl_dmu_pool
, zilog
->zl_destroy_txg
);
611 zilog
->zl_old_header
= *zh
; /* debugging aid */
613 if (BP_IS_HOLE(&zh
->zh_log
))
616 tx
= dmu_tx_create(zilog
->zl_os
);
617 VERIFY(dmu_tx_assign(tx
, TXG_WAIT
) == 0);
618 dsl_dataset_dirty(dmu_objset_ds(zilog
->zl_os
), tx
);
619 txg
= dmu_tx_get_txg(tx
);
621 mutex_enter(&zilog
->zl_lock
);
623 ASSERT3U(zilog
->zl_destroy_txg
, <, txg
);
624 zilog
->zl_destroy_txg
= txg
;
625 zilog
->zl_keep_first
= keep_first
;
627 if (!list_is_empty(&zilog
->zl_lwb_list
)) {
628 ASSERT(zh
->zh_claim_txg
== 0);
630 while ((lwb
= list_head(&zilog
->zl_lwb_list
)) != NULL
) {
631 ASSERT(lwb
->lwb_zio
== NULL
);
632 if (lwb
->lwb_fastwrite
)
633 metaslab_fastwrite_unmark(zilog
->zl_spa
,
635 list_remove(&zilog
->zl_lwb_list
, lwb
);
636 if (lwb
->lwb_buf
!= NULL
)
637 zio_buf_free(lwb
->lwb_buf
, lwb
->lwb_sz
);
638 zio_free_zil(zilog
->zl_spa
, txg
, &lwb
->lwb_blk
);
639 kmem_cache_free(zil_lwb_cache
, lwb
);
641 } else if (!keep_first
) {
642 zil_destroy_sync(zilog
, tx
);
644 mutex_exit(&zilog
->zl_lock
);
650 zil_destroy_sync(zilog_t
*zilog
, dmu_tx_t
*tx
)
652 ASSERT(list_is_empty(&zilog
->zl_lwb_list
));
653 (void) zil_parse(zilog
, zil_free_log_block
,
654 zil_free_log_record
, tx
, zilog
->zl_header
->zh_claim_txg
);
658 zil_claim(dsl_pool_t
*dp
, dsl_dataset_t
*ds
, void *txarg
)
660 dmu_tx_t
*tx
= txarg
;
661 uint64_t first_txg
= dmu_tx_get_txg(tx
);
667 error
= dmu_objset_own_obj(dp
, ds
->ds_object
,
668 DMU_OST_ANY
, B_FALSE
, FTAG
, &os
);
671 * EBUSY indicates that the objset is inconsistent, in which
672 * case it can not have a ZIL.
674 if (error
!= EBUSY
) {
675 cmn_err(CE_WARN
, "can't open objset for %llu, error %u",
676 (unsigned long long)ds
->ds_object
, error
);
682 zilog
= dmu_objset_zil(os
);
683 zh
= zil_header_in_syncing_context(zilog
);
685 if (spa_get_log_state(zilog
->zl_spa
) == SPA_LOG_CLEAR
) {
686 if (!BP_IS_HOLE(&zh
->zh_log
))
687 zio_free_zil(zilog
->zl_spa
, first_txg
, &zh
->zh_log
);
688 BP_ZERO(&zh
->zh_log
);
689 dsl_dataset_dirty(dmu_objset_ds(os
), tx
);
690 dmu_objset_disown(os
, FTAG
);
695 * Claim all log blocks if we haven't already done so, and remember
696 * the highest claimed sequence number. This ensures that if we can
697 * read only part of the log now (e.g. due to a missing device),
698 * but we can read the entire log later, we will not try to replay
699 * or destroy beyond the last block we successfully claimed.
701 ASSERT3U(zh
->zh_claim_txg
, <=, first_txg
);
702 if (zh
->zh_claim_txg
== 0 && !BP_IS_HOLE(&zh
->zh_log
)) {
703 (void) zil_parse(zilog
, zil_claim_log_block
,
704 zil_claim_log_record
, tx
, first_txg
);
705 zh
->zh_claim_txg
= first_txg
;
706 zh
->zh_claim_blk_seq
= zilog
->zl_parse_blk_seq
;
707 zh
->zh_claim_lr_seq
= zilog
->zl_parse_lr_seq
;
708 if (zilog
->zl_parse_lr_count
|| zilog
->zl_parse_blk_count
> 1)
709 zh
->zh_flags
|= ZIL_REPLAY_NEEDED
;
710 zh
->zh_flags
|= ZIL_CLAIM_LR_SEQ_VALID
;
711 dsl_dataset_dirty(dmu_objset_ds(os
), tx
);
714 ASSERT3U(first_txg
, ==, (spa_last_synced_txg(zilog
->zl_spa
) + 1));
715 dmu_objset_disown(os
, FTAG
);
720 * Check the log by walking the log chain.
721 * Checksum errors are ok as they indicate the end of the chain.
722 * Any other error (no device or read failure) returns an error.
726 zil_check_log_chain(dsl_pool_t
*dp
, dsl_dataset_t
*ds
, void *tx
)
735 error
= dmu_objset_from_ds(ds
, &os
);
737 cmn_err(CE_WARN
, "can't open objset %llu, error %d",
738 (unsigned long long)ds
->ds_object
, error
);
742 zilog
= dmu_objset_zil(os
);
743 bp
= (blkptr_t
*)&zilog
->zl_header
->zh_log
;
746 * Check the first block and determine if it's on a log device
747 * which may have been removed or faulted prior to loading this
748 * pool. If so, there's no point in checking the rest of the log
749 * as its content should have already been synced to the pool.
751 if (!BP_IS_HOLE(bp
)) {
753 boolean_t valid
= B_TRUE
;
755 spa_config_enter(os
->os_spa
, SCL_STATE
, FTAG
, RW_READER
);
756 vd
= vdev_lookup_top(os
->os_spa
, DVA_GET_VDEV(&bp
->blk_dva
[0]));
757 if (vd
->vdev_islog
&& vdev_is_dead(vd
))
758 valid
= vdev_log_state_valid(vd
);
759 spa_config_exit(os
->os_spa
, SCL_STATE
, FTAG
);
766 * Because tx == NULL, zil_claim_log_block() will not actually claim
767 * any blocks, but just determine whether it is possible to do so.
768 * In addition to checking the log chain, zil_claim_log_block()
769 * will invoke zio_claim() with a done func of spa_claim_notify(),
770 * which will update spa_max_claim_txg. See spa_load() for details.
772 error
= zil_parse(zilog
, zil_claim_log_block
, zil_claim_log_record
, tx
,
773 zilog
->zl_header
->zh_claim_txg
? -1ULL : spa_first_txg(os
->os_spa
));
775 return ((error
== ECKSUM
|| error
== ENOENT
) ? 0 : error
);
779 zil_vdev_compare(const void *x1
, const void *x2
)
781 const uint64_t v1
= ((zil_vdev_node_t
*)x1
)->zv_vdev
;
782 const uint64_t v2
= ((zil_vdev_node_t
*)x2
)->zv_vdev
;
784 return (AVL_CMP(v1
, v2
));
788 zil_add_block(zilog_t
*zilog
, const blkptr_t
*bp
)
790 avl_tree_t
*t
= &zilog
->zl_vdev_tree
;
792 zil_vdev_node_t
*zv
, zvsearch
;
793 int ndvas
= BP_GET_NDVAS(bp
);
796 if (zfs_nocacheflush
)
799 ASSERT(zilog
->zl_writer
);
802 * Even though we're zl_writer, we still need a lock because the
803 * zl_get_data() callbacks may have dmu_sync() done callbacks
804 * that will run concurrently.
806 mutex_enter(&zilog
->zl_vdev_lock
);
807 for (i
= 0; i
< ndvas
; i
++) {
808 zvsearch
.zv_vdev
= DVA_GET_VDEV(&bp
->blk_dva
[i
]);
809 if (avl_find(t
, &zvsearch
, &where
) == NULL
) {
810 zv
= kmem_alloc(sizeof (*zv
), KM_SLEEP
);
811 zv
->zv_vdev
= zvsearch
.zv_vdev
;
812 avl_insert(t
, zv
, where
);
815 mutex_exit(&zilog
->zl_vdev_lock
);
819 zil_flush_vdevs(zilog_t
*zilog
)
821 spa_t
*spa
= zilog
->zl_spa
;
822 avl_tree_t
*t
= &zilog
->zl_vdev_tree
;
827 ASSERT(zilog
->zl_writer
);
830 * We don't need zl_vdev_lock here because we're the zl_writer,
831 * and all zl_get_data() callbacks are done.
833 if (avl_numnodes(t
) == 0)
836 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
838 zio
= zio_root(spa
, NULL
, NULL
, ZIO_FLAG_CANFAIL
);
840 while ((zv
= avl_destroy_nodes(t
, &cookie
)) != NULL
) {
841 vdev_t
*vd
= vdev_lookup_top(spa
, zv
->zv_vdev
);
844 kmem_free(zv
, sizeof (*zv
));
848 * Wait for all the flushes to complete. Not all devices actually
849 * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails.
851 (void) zio_wait(zio
);
853 spa_config_exit(spa
, SCL_STATE
, FTAG
);
857 * Function called when a log block write completes
860 zil_lwb_write_done(zio_t
*zio
)
862 lwb_t
*lwb
= zio
->io_private
;
863 zilog_t
*zilog
= lwb
->lwb_zilog
;
864 dmu_tx_t
*tx
= lwb
->lwb_tx
;
866 ASSERT(BP_GET_COMPRESS(zio
->io_bp
) == ZIO_COMPRESS_OFF
);
867 ASSERT(BP_GET_TYPE(zio
->io_bp
) == DMU_OT_INTENT_LOG
);
868 ASSERT(BP_GET_LEVEL(zio
->io_bp
) == 0);
869 ASSERT(BP_GET_BYTEORDER(zio
->io_bp
) == ZFS_HOST_BYTEORDER
);
870 ASSERT(!BP_IS_GANG(zio
->io_bp
));
871 ASSERT(!BP_IS_HOLE(zio
->io_bp
));
872 ASSERT(BP_GET_FILL(zio
->io_bp
) == 0);
875 * Ensure the lwb buffer pointer is cleared before releasing
876 * the txg. If we have had an allocation failure and
877 * the txg is waiting to sync then we want want zil_sync()
878 * to remove the lwb so that it's not picked up as the next new
879 * one in zil_commit_writer(). zil_sync() will only remove
880 * the lwb if lwb_buf is null.
882 abd_put(zio
->io_abd
);
883 zio_buf_free(lwb
->lwb_buf
, lwb
->lwb_sz
);
884 mutex_enter(&zilog
->zl_lock
);
886 lwb
->lwb_fastwrite
= FALSE
;
889 mutex_exit(&zilog
->zl_lock
);
892 * Now that we've written this log block, we have a stable pointer
893 * to the next block in the chain, so it's OK to let the txg in
894 * which we allocated the next block sync.
900 * Initialize the io for a log block.
903 zil_lwb_write_init(zilog_t
*zilog
, lwb_t
*lwb
)
907 SET_BOOKMARK(&zb
, lwb
->lwb_blk
.blk_cksum
.zc_word
[ZIL_ZC_OBJSET
],
908 ZB_ZIL_OBJECT
, ZB_ZIL_LEVEL
,
909 lwb
->lwb_blk
.blk_cksum
.zc_word
[ZIL_ZC_SEQ
]);
911 if (zilog
->zl_root_zio
== NULL
) {
912 zilog
->zl_root_zio
= zio_root(zilog
->zl_spa
, NULL
, NULL
,
916 /* Lock so zil_sync() doesn't fastwrite_unmark after zio is created */
917 mutex_enter(&zilog
->zl_lock
);
918 if (lwb
->lwb_zio
== NULL
) {
919 abd_t
*lwb_abd
= abd_get_from_buf(lwb
->lwb_buf
,
920 BP_GET_LSIZE(&lwb
->lwb_blk
));
921 if (!lwb
->lwb_fastwrite
) {
922 metaslab_fastwrite_mark(zilog
->zl_spa
, &lwb
->lwb_blk
);
923 lwb
->lwb_fastwrite
= 1;
925 lwb
->lwb_zio
= zio_rewrite(zilog
->zl_root_zio
, zilog
->zl_spa
,
926 0, &lwb
->lwb_blk
, lwb_abd
, BP_GET_LSIZE(&lwb
->lwb_blk
),
927 zil_lwb_write_done
, lwb
, ZIO_PRIORITY_SYNC_WRITE
,
928 ZIO_FLAG_CANFAIL
| ZIO_FLAG_DONT_PROPAGATE
|
929 ZIO_FLAG_FASTWRITE
, &zb
);
931 mutex_exit(&zilog
->zl_lock
);
935 * Define a limited set of intent log block sizes.
937 * These must be a multiple of 4KB. Note only the amount used (again
938 * aligned to 4KB) actually gets written. However, we can't always just
939 * allocate SPA_OLD_MAXBLOCKSIZE as the slog space could be exhausted.
941 uint64_t zil_block_buckets
[] = {
942 4096, /* non TX_WRITE */
943 8192+4096, /* data base */
944 32*1024 + 4096, /* NFS writes */
949 * Use the slog as long as the current commit size is less than the
950 * limit or the total list size is less than 2X the limit. Limit
951 * checking is disabled by setting zil_slog_limit to UINT64_MAX.
953 unsigned long zil_slog_limit
= 1024 * 1024;
954 #define USE_SLOG(zilog) (((zilog)->zl_cur_used < zil_slog_limit) || \
955 ((zilog)->zl_itx_list_sz < (zil_slog_limit << 1)))
958 * Start a log block write and advance to the next log block.
959 * Calls are serialized.
962 zil_lwb_write_start(zilog_t
*zilog
, lwb_t
*lwb
)
966 spa_t
*spa
= zilog
->zl_spa
;
970 uint64_t zil_blksz
, wsz
;
974 if (BP_GET_CHECKSUM(&lwb
->lwb_blk
) == ZIO_CHECKSUM_ZILOG2
) {
975 zilc
= (zil_chain_t
*)lwb
->lwb_buf
;
976 bp
= &zilc
->zc_next_blk
;
978 zilc
= (zil_chain_t
*)(lwb
->lwb_buf
+ lwb
->lwb_sz
);
979 bp
= &zilc
->zc_next_blk
;
982 ASSERT(lwb
->lwb_nused
<= lwb
->lwb_sz
);
985 * Allocate the next block and save its address in this block
986 * before writing it in order to establish the log chain.
987 * Note that if the allocation of nlwb synced before we wrote
988 * the block that points at it (lwb), we'd leak it if we crashed.
989 * Therefore, we don't do dmu_tx_commit() until zil_lwb_write_done().
990 * We dirty the dataset to ensure that zil_sync() will be called
991 * to clean up in the event of allocation failure or I/O failure.
993 tx
= dmu_tx_create(zilog
->zl_os
);
994 VERIFY(dmu_tx_assign(tx
, TXG_WAIT
) == 0);
995 dsl_dataset_dirty(dmu_objset_ds(zilog
->zl_os
), tx
);
996 txg
= dmu_tx_get_txg(tx
);
1001 * Log blocks are pre-allocated. Here we select the size of the next
1002 * block, based on size used in the last block.
1003 * - first find the smallest bucket that will fit the block from a
1004 * limited set of block sizes. This is because it's faster to write
1005 * blocks allocated from the same metaslab as they are adjacent or
1007 * - next find the maximum from the new suggested size and an array of
1008 * previous sizes. This lessens a picket fence effect of wrongly
1009 * guesssing the size if we have a stream of say 2k, 64k, 2k, 64k
1012 * Note we only write what is used, but we can't just allocate
1013 * the maximum block size because we can exhaust the available
1016 zil_blksz
= zilog
->zl_cur_used
+ sizeof (zil_chain_t
);
1017 for (i
= 0; zil_blksz
> zil_block_buckets
[i
]; i
++)
1019 zil_blksz
= zil_block_buckets
[i
];
1020 if (zil_blksz
== UINT64_MAX
)
1021 zil_blksz
= SPA_OLD_MAXBLOCKSIZE
;
1022 zilog
->zl_prev_blks
[zilog
->zl_prev_rotor
] = zil_blksz
;
1023 for (i
= 0; i
< ZIL_PREV_BLKS
; i
++)
1024 zil_blksz
= MAX(zil_blksz
, zilog
->zl_prev_blks
[i
]);
1025 zilog
->zl_prev_rotor
= (zilog
->zl_prev_rotor
+ 1) & (ZIL_PREV_BLKS
- 1);
1028 use_slog
= USE_SLOG(zilog
);
1029 error
= zio_alloc_zil(spa
, txg
, bp
, zil_blksz
,
1032 ZIL_STAT_BUMP(zil_itx_metaslab_slog_count
);
1033 ZIL_STAT_INCR(zil_itx_metaslab_slog_bytes
, lwb
->lwb_nused
);
1035 ZIL_STAT_BUMP(zil_itx_metaslab_normal_count
);
1036 ZIL_STAT_INCR(zil_itx_metaslab_normal_bytes
, lwb
->lwb_nused
);
1039 ASSERT3U(bp
->blk_birth
, ==, txg
);
1040 bp
->blk_cksum
= lwb
->lwb_blk
.blk_cksum
;
1041 bp
->blk_cksum
.zc_word
[ZIL_ZC_SEQ
]++;
1044 * Allocate a new log write buffer (lwb).
1046 nlwb
= zil_alloc_lwb(zilog
, bp
, txg
, TRUE
);
1048 /* Record the block for later vdev flushing */
1049 zil_add_block(zilog
, &lwb
->lwb_blk
);
1052 if (BP_GET_CHECKSUM(&lwb
->lwb_blk
) == ZIO_CHECKSUM_ZILOG2
) {
1053 /* For Slim ZIL only write what is used. */
1054 wsz
= P2ROUNDUP_TYPED(lwb
->lwb_nused
, ZIL_MIN_BLKSZ
, uint64_t);
1055 ASSERT3U(wsz
, <=, lwb
->lwb_sz
);
1056 zio_shrink(lwb
->lwb_zio
, wsz
);
1063 zilc
->zc_nused
= lwb
->lwb_nused
;
1064 zilc
->zc_eck
.zec_cksum
= lwb
->lwb_blk
.blk_cksum
;
1067 * clear unused data for security
1069 bzero(lwb
->lwb_buf
+ lwb
->lwb_nused
, wsz
- lwb
->lwb_nused
);
1071 zio_nowait(lwb
->lwb_zio
); /* Kick off the write for the old log block */
1074 * If there was an allocation failure then nlwb will be null which
1075 * forces a txg_wait_synced().
1081 zil_lwb_commit(zilog_t
*zilog
, itx_t
*itx
, lwb_t
*lwb
)
1083 lr_t
*lrc
= &itx
->itx_lr
; /* common log record */
1084 lr_write_t
*lrw
= (lr_write_t
*)lrc
;
1086 uint64_t txg
= lrc
->lrc_txg
;
1087 uint64_t reclen
= lrc
->lrc_reclen
;
1093 ASSERT(lwb
->lwb_buf
!= NULL
);
1094 ASSERT(zilog_is_dirty(zilog
) ||
1095 spa_freeze_txg(zilog
->zl_spa
) != UINT64_MAX
);
1097 if (lrc
->lrc_txtype
== TX_WRITE
&& itx
->itx_wr_state
== WR_NEED_COPY
)
1098 dlen
= P2ROUNDUP_TYPED(
1099 lrw
->lr_length
, sizeof (uint64_t), uint64_t);
1101 zilog
->zl_cur_used
+= (reclen
+ dlen
);
1103 zil_lwb_write_init(zilog
, lwb
);
1106 * If this record won't fit in the current log block, start a new one.
1108 if (lwb
->lwb_nused
+ reclen
+ dlen
> lwb
->lwb_sz
) {
1109 lwb
= zil_lwb_write_start(zilog
, lwb
);
1112 zil_lwb_write_init(zilog
, lwb
);
1113 ASSERT(LWB_EMPTY(lwb
));
1114 if (lwb
->lwb_nused
+ reclen
+ dlen
> lwb
->lwb_sz
) {
1115 txg_wait_synced(zilog
->zl_dmu_pool
, txg
);
1120 lr_buf
= lwb
->lwb_buf
+ lwb
->lwb_nused
;
1121 bcopy(lrc
, lr_buf
, reclen
);
1122 lrc
= (lr_t
*)lr_buf
;
1123 lrw
= (lr_write_t
*)lrc
;
1125 ZIL_STAT_BUMP(zil_itx_count
);
1128 * If it's a write, fetch the data or get its blkptr as appropriate.
1130 if (lrc
->lrc_txtype
== TX_WRITE
) {
1131 if (txg
> spa_freeze_txg(zilog
->zl_spa
))
1132 txg_wait_synced(zilog
->zl_dmu_pool
, txg
);
1133 if (itx
->itx_wr_state
== WR_COPIED
) {
1134 ZIL_STAT_BUMP(zil_itx_copied_count
);
1135 ZIL_STAT_INCR(zil_itx_copied_bytes
, lrw
->lr_length
);
1141 ASSERT(itx
->itx_wr_state
== WR_NEED_COPY
);
1142 dbuf
= lr_buf
+ reclen
;
1143 lrw
->lr_common
.lrc_reclen
+= dlen
;
1144 ZIL_STAT_BUMP(zil_itx_needcopy_count
);
1145 ZIL_STAT_INCR(zil_itx_needcopy_bytes
,
1148 ASSERT(itx
->itx_wr_state
== WR_INDIRECT
);
1150 ZIL_STAT_BUMP(zil_itx_indirect_count
);
1151 ZIL_STAT_INCR(zil_itx_indirect_bytes
,
1154 error
= zilog
->zl_get_data(
1155 itx
->itx_private
, lrw
, dbuf
, lwb
->lwb_zio
);
1157 txg_wait_synced(zilog
->zl_dmu_pool
, txg
);
1161 ASSERT(error
== ENOENT
|| error
== EEXIST
||
1169 * We're actually making an entry, so update lrc_seq to be the
1170 * log record sequence number. Note that this is generally not
1171 * equal to the itx sequence number because not all transactions
1172 * are synchronous, and sometimes spa_sync() gets there first.
1174 lrc
->lrc_seq
= ++zilog
->zl_lr_seq
; /* we are single threaded */
1175 lwb
->lwb_nused
+= reclen
+ dlen
;
1176 lwb
->lwb_max_txg
= MAX(lwb
->lwb_max_txg
, txg
);
1177 ASSERT3U(lwb
->lwb_nused
, <=, lwb
->lwb_sz
);
1178 ASSERT0(P2PHASE(lwb
->lwb_nused
, sizeof (uint64_t)));
1184 zil_itx_create(uint64_t txtype
, size_t lrsize
)
1188 lrsize
= P2ROUNDUP_TYPED(lrsize
, sizeof (uint64_t), size_t);
1190 itx
= zio_data_buf_alloc(offsetof(itx_t
, itx_lr
) + lrsize
);
1191 itx
->itx_lr
.lrc_txtype
= txtype
;
1192 itx
->itx_lr
.lrc_reclen
= lrsize
;
1193 itx
->itx_sod
= lrsize
; /* if write & WR_NEED_COPY will be increased */
1194 itx
->itx_lr
.lrc_seq
= 0; /* defensive */
1195 itx
->itx_sync
= B_TRUE
; /* default is synchronous */
1196 itx
->itx_callback
= NULL
;
1197 itx
->itx_callback_data
= NULL
;
1203 zil_itx_destroy(itx_t
*itx
)
1205 zio_data_buf_free(itx
, offsetof(itx_t
, itx_lr
)+itx
->itx_lr
.lrc_reclen
);
1209 * Free up the sync and async itxs. The itxs_t has already been detached
1210 * so no locks are needed.
1213 zil_itxg_clean(itxs_t
*itxs
)
1219 itx_async_node_t
*ian
;
1221 list
= &itxs
->i_sync_list
;
1222 while ((itx
= list_head(list
)) != NULL
) {
1223 if (itx
->itx_callback
!= NULL
)
1224 itx
->itx_callback(itx
->itx_callback_data
);
1225 list_remove(list
, itx
);
1226 zil_itx_destroy(itx
);
1230 t
= &itxs
->i_async_tree
;
1231 while ((ian
= avl_destroy_nodes(t
, &cookie
)) != NULL
) {
1232 list
= &ian
->ia_list
;
1233 while ((itx
= list_head(list
)) != NULL
) {
1234 if (itx
->itx_callback
!= NULL
)
1235 itx
->itx_callback(itx
->itx_callback_data
);
1236 list_remove(list
, itx
);
1237 zil_itx_destroy(itx
);
1240 kmem_free(ian
, sizeof (itx_async_node_t
));
1244 kmem_free(itxs
, sizeof (itxs_t
));
1248 zil_aitx_compare(const void *x1
, const void *x2
)
1250 const uint64_t o1
= ((itx_async_node_t
*)x1
)->ia_foid
;
1251 const uint64_t o2
= ((itx_async_node_t
*)x2
)->ia_foid
;
1253 return (AVL_CMP(o1
, o2
));
1257 * Remove all async itx with the given oid.
1260 zil_remove_async(zilog_t
*zilog
, uint64_t oid
)
1263 itx_async_node_t
*ian
;
1270 list_create(&clean_list
, sizeof (itx_t
), offsetof(itx_t
, itx_node
));
1272 if (spa_freeze_txg(zilog
->zl_spa
) != UINT64_MAX
) /* ziltest support */
1275 otxg
= spa_last_synced_txg(zilog
->zl_spa
) + 1;
1277 for (txg
= otxg
; txg
< (otxg
+ TXG_CONCURRENT_STATES
); txg
++) {
1278 itxg_t
*itxg
= &zilog
->zl_itxg
[txg
& TXG_MASK
];
1280 mutex_enter(&itxg
->itxg_lock
);
1281 if (itxg
->itxg_txg
!= txg
) {
1282 mutex_exit(&itxg
->itxg_lock
);
1287 * Locate the object node and append its list.
1289 t
= &itxg
->itxg_itxs
->i_async_tree
;
1290 ian
= avl_find(t
, &oid
, &where
);
1292 list_move_tail(&clean_list
, &ian
->ia_list
);
1293 mutex_exit(&itxg
->itxg_lock
);
1295 while ((itx
= list_head(&clean_list
)) != NULL
) {
1296 if (itx
->itx_callback
!= NULL
)
1297 itx
->itx_callback(itx
->itx_callback_data
);
1298 list_remove(&clean_list
, itx
);
1299 zil_itx_destroy(itx
);
1301 list_destroy(&clean_list
);
1305 zil_itx_assign(zilog_t
*zilog
, itx_t
*itx
, dmu_tx_t
*tx
)
1309 itxs_t
*itxs
, *clean
= NULL
;
1312 * Object ids can be re-instantiated in the next txg so
1313 * remove any async transactions to avoid future leaks.
1314 * This can happen if a fsync occurs on the re-instantiated
1315 * object for a WR_INDIRECT or WR_NEED_COPY write, which gets
1316 * the new file data and flushes a write record for the old object.
1318 if ((itx
->itx_lr
.lrc_txtype
& ~TX_CI
) == TX_REMOVE
)
1319 zil_remove_async(zilog
, itx
->itx_oid
);
1322 * Ensure the data of a renamed file is committed before the rename.
1324 if ((itx
->itx_lr
.lrc_txtype
& ~TX_CI
) == TX_RENAME
)
1325 zil_async_to_sync(zilog
, itx
->itx_oid
);
1327 if (spa_freeze_txg(zilog
->zl_spa
) != UINT64_MAX
)
1330 txg
= dmu_tx_get_txg(tx
);
1332 itxg
= &zilog
->zl_itxg
[txg
& TXG_MASK
];
1333 mutex_enter(&itxg
->itxg_lock
);
1334 itxs
= itxg
->itxg_itxs
;
1335 if (itxg
->itxg_txg
!= txg
) {
1338 * The zil_clean callback hasn't got around to cleaning
1339 * this itxg. Save the itxs for release below.
1340 * This should be rare.
1342 atomic_add_64(&zilog
->zl_itx_list_sz
, -itxg
->itxg_sod
);
1344 clean
= itxg
->itxg_itxs
;
1346 ASSERT(itxg
->itxg_sod
== 0);
1347 itxg
->itxg_txg
= txg
;
1348 itxs
= itxg
->itxg_itxs
= kmem_zalloc(sizeof (itxs_t
),
1351 list_create(&itxs
->i_sync_list
, sizeof (itx_t
),
1352 offsetof(itx_t
, itx_node
));
1353 avl_create(&itxs
->i_async_tree
, zil_aitx_compare
,
1354 sizeof (itx_async_node_t
),
1355 offsetof(itx_async_node_t
, ia_node
));
1357 if (itx
->itx_sync
) {
1358 list_insert_tail(&itxs
->i_sync_list
, itx
);
1359 atomic_add_64(&zilog
->zl_itx_list_sz
, itx
->itx_sod
);
1360 itxg
->itxg_sod
+= itx
->itx_sod
;
1362 avl_tree_t
*t
= &itxs
->i_async_tree
;
1364 LR_FOID_GET_OBJ(((lr_ooo_t
*)&itx
->itx_lr
)->lr_foid
);
1365 itx_async_node_t
*ian
;
1368 ian
= avl_find(t
, &foid
, &where
);
1370 ian
= kmem_alloc(sizeof (itx_async_node_t
),
1372 list_create(&ian
->ia_list
, sizeof (itx_t
),
1373 offsetof(itx_t
, itx_node
));
1374 ian
->ia_foid
= foid
;
1375 avl_insert(t
, ian
, where
);
1377 list_insert_tail(&ian
->ia_list
, itx
);
1380 itx
->itx_lr
.lrc_txg
= dmu_tx_get_txg(tx
);
1381 zilog_dirty(zilog
, txg
);
1382 mutex_exit(&itxg
->itxg_lock
);
1384 /* Release the old itxs now we've dropped the lock */
1386 zil_itxg_clean(clean
);
1390 * If there are any in-memory intent log transactions which have now been
1391 * synced then start up a taskq to free them. We should only do this after we
1392 * have written out the uberblocks (i.e. txg has been comitted) so that
1393 * don't inadvertently clean out in-memory log records that would be required
1397 zil_clean(zilog_t
*zilog
, uint64_t synced_txg
)
1399 itxg_t
*itxg
= &zilog
->zl_itxg
[synced_txg
& TXG_MASK
];
1402 mutex_enter(&itxg
->itxg_lock
);
1403 if (itxg
->itxg_itxs
== NULL
|| itxg
->itxg_txg
== ZILTEST_TXG
) {
1404 mutex_exit(&itxg
->itxg_lock
);
1407 ASSERT3U(itxg
->itxg_txg
, <=, synced_txg
);
1408 ASSERT(itxg
->itxg_txg
!= 0);
1409 ASSERT(zilog
->zl_clean_taskq
!= NULL
);
1410 atomic_add_64(&zilog
->zl_itx_list_sz
, -itxg
->itxg_sod
);
1412 clean_me
= itxg
->itxg_itxs
;
1413 itxg
->itxg_itxs
= NULL
;
1415 mutex_exit(&itxg
->itxg_lock
);
1417 * Preferably start a task queue to free up the old itxs but
1418 * if taskq_dispatch can't allocate resources to do that then
1419 * free it in-line. This should be rare. Note, using TQ_SLEEP
1420 * created a bad performance problem.
1422 if (taskq_dispatch(zilog
->zl_clean_taskq
,
1423 (void (*)(void *))zil_itxg_clean
, clean_me
, TQ_NOSLEEP
) == 0)
1424 zil_itxg_clean(clean_me
);
1428 * Get the list of itxs to commit into zl_itx_commit_list.
1431 zil_get_commit_list(zilog_t
*zilog
)
1434 list_t
*commit_list
= &zilog
->zl_itx_commit_list
;
1435 uint64_t push_sod
= 0;
1437 if (spa_freeze_txg(zilog
->zl_spa
) != UINT64_MAX
) /* ziltest support */
1440 otxg
= spa_last_synced_txg(zilog
->zl_spa
) + 1;
1442 for (txg
= otxg
; txg
< (otxg
+ TXG_CONCURRENT_STATES
); txg
++) {
1443 itxg_t
*itxg
= &zilog
->zl_itxg
[txg
& TXG_MASK
];
1445 mutex_enter(&itxg
->itxg_lock
);
1446 if (itxg
->itxg_txg
!= txg
) {
1447 mutex_exit(&itxg
->itxg_lock
);
1451 list_move_tail(commit_list
, &itxg
->itxg_itxs
->i_sync_list
);
1452 push_sod
+= itxg
->itxg_sod
;
1455 mutex_exit(&itxg
->itxg_lock
);
1457 atomic_add_64(&zilog
->zl_itx_list_sz
, -push_sod
);
1461 * Move the async itxs for a specified object to commit into sync lists.
1464 zil_async_to_sync(zilog_t
*zilog
, uint64_t foid
)
1467 itx_async_node_t
*ian
;
1471 if (spa_freeze_txg(zilog
->zl_spa
) != UINT64_MAX
) /* ziltest support */
1474 otxg
= spa_last_synced_txg(zilog
->zl_spa
) + 1;
1476 for (txg
= otxg
; txg
< (otxg
+ TXG_CONCURRENT_STATES
); txg
++) {
1477 itxg_t
*itxg
= &zilog
->zl_itxg
[txg
& TXG_MASK
];
1479 mutex_enter(&itxg
->itxg_lock
);
1480 if (itxg
->itxg_txg
!= txg
) {
1481 mutex_exit(&itxg
->itxg_lock
);
1486 * If a foid is specified then find that node and append its
1487 * list. Otherwise walk the tree appending all the lists
1488 * to the sync list. We add to the end rather than the
1489 * beginning to ensure the create has happened.
1491 t
= &itxg
->itxg_itxs
->i_async_tree
;
1493 ian
= avl_find(t
, &foid
, &where
);
1495 list_move_tail(&itxg
->itxg_itxs
->i_sync_list
,
1499 void *cookie
= NULL
;
1501 while ((ian
= avl_destroy_nodes(t
, &cookie
)) != NULL
) {
1502 list_move_tail(&itxg
->itxg_itxs
->i_sync_list
,
1504 list_destroy(&ian
->ia_list
);
1505 kmem_free(ian
, sizeof (itx_async_node_t
));
1508 mutex_exit(&itxg
->itxg_lock
);
1513 zil_commit_writer(zilog_t
*zilog
)
1518 spa_t
*spa
= zilog
->zl_spa
;
1521 ASSERT(zilog
->zl_root_zio
== NULL
);
1523 mutex_exit(&zilog
->zl_lock
);
1525 zil_get_commit_list(zilog
);
1528 * Return if there's nothing to commit before we dirty the fs by
1529 * calling zil_create().
1531 if (list_head(&zilog
->zl_itx_commit_list
) == NULL
) {
1532 mutex_enter(&zilog
->zl_lock
);
1536 if (zilog
->zl_suspend
) {
1539 lwb
= list_tail(&zilog
->zl_lwb_list
);
1541 lwb
= zil_create(zilog
);
1544 DTRACE_PROBE1(zil__cw1
, zilog_t
*, zilog
);
1545 for (itx
= list_head(&zilog
->zl_itx_commit_list
); itx
!= NULL
;
1546 itx
= list_next(&zilog
->zl_itx_commit_list
, itx
)) {
1547 txg
= itx
->itx_lr
.lrc_txg
;
1550 if (txg
> spa_last_synced_txg(spa
) || txg
> spa_freeze_txg(spa
))
1551 lwb
= zil_lwb_commit(zilog
, itx
, lwb
);
1553 DTRACE_PROBE1(zil__cw2
, zilog_t
*, zilog
);
1555 /* write the last block out */
1556 if (lwb
!= NULL
&& lwb
->lwb_zio
!= NULL
)
1557 lwb
= zil_lwb_write_start(zilog
, lwb
);
1559 zilog
->zl_cur_used
= 0;
1562 * Wait if necessary for the log blocks to be on stable storage.
1564 if (zilog
->zl_root_zio
) {
1565 error
= zio_wait(zilog
->zl_root_zio
);
1566 zilog
->zl_root_zio
= NULL
;
1567 zil_flush_vdevs(zilog
);
1570 if (error
|| lwb
== NULL
)
1571 txg_wait_synced(zilog
->zl_dmu_pool
, 0);
1573 while ((itx
= list_head(&zilog
->zl_itx_commit_list
))) {
1574 txg
= itx
->itx_lr
.lrc_txg
;
1577 if (itx
->itx_callback
!= NULL
)
1578 itx
->itx_callback(itx
->itx_callback_data
);
1579 list_remove(&zilog
->zl_itx_commit_list
, itx
);
1580 zil_itx_destroy(itx
);
1583 mutex_enter(&zilog
->zl_lock
);
1586 * Remember the highest committed log sequence number for ztest.
1587 * We only update this value when all the log writes succeeded,
1588 * because ztest wants to ASSERT that it got the whole log chain.
1590 if (error
== 0 && lwb
!= NULL
)
1591 zilog
->zl_commit_lr_seq
= zilog
->zl_lr_seq
;
1595 * Commit zfs transactions to stable storage.
1596 * If foid is 0 push out all transactions, otherwise push only those
1597 * for that object or might reference that object.
1599 * itxs are committed in batches. In a heavily stressed zil there will be
1600 * a commit writer thread who is writing out a bunch of itxs to the log
1601 * for a set of committing threads (cthreads) in the same batch as the writer.
1602 * Those cthreads are all waiting on the same cv for that batch.
1604 * There will also be a different and growing batch of threads that are
1605 * waiting to commit (qthreads). When the committing batch completes
1606 * a transition occurs such that the cthreads exit and the qthreads become
1607 * cthreads. One of the new cthreads becomes the writer thread for the
1608 * batch. Any new threads arriving become new qthreads.
1610 * Only 2 condition variables are needed and there's no transition
1611 * between the two cvs needed. They just flip-flop between qthreads
1614 * Using this scheme we can efficiently wakeup up only those threads
1615 * that have been committed.
1618 zil_commit(zilog_t
*zilog
, uint64_t foid
)
1622 if (zilog
->zl_sync
== ZFS_SYNC_DISABLED
)
1625 ZIL_STAT_BUMP(zil_commit_count
);
1627 /* move the async itxs for the foid to the sync queues */
1628 zil_async_to_sync(zilog
, foid
);
1630 mutex_enter(&zilog
->zl_lock
);
1631 mybatch
= zilog
->zl_next_batch
;
1632 while (zilog
->zl_writer
) {
1633 cv_wait(&zilog
->zl_cv_batch
[mybatch
& 1], &zilog
->zl_lock
);
1634 if (mybatch
<= zilog
->zl_com_batch
) {
1635 mutex_exit(&zilog
->zl_lock
);
1640 zilog
->zl_next_batch
++;
1641 zilog
->zl_writer
= B_TRUE
;
1642 ZIL_STAT_BUMP(zil_commit_writer_count
);
1643 zil_commit_writer(zilog
);
1644 zilog
->zl_com_batch
= mybatch
;
1645 zilog
->zl_writer
= B_FALSE
;
1647 /* wake up one thread to become the next writer */
1648 cv_signal(&zilog
->zl_cv_batch
[(mybatch
+1) & 1]);
1650 /* wake up all threads waiting for this batch to be committed */
1651 cv_broadcast(&zilog
->zl_cv_batch
[mybatch
& 1]);
1653 mutex_exit(&zilog
->zl_lock
);
1657 * Called in syncing context to free committed log blocks and update log header.
1660 zil_sync(zilog_t
*zilog
, dmu_tx_t
*tx
)
1662 zil_header_t
*zh
= zil_header_in_syncing_context(zilog
);
1663 uint64_t txg
= dmu_tx_get_txg(tx
);
1664 spa_t
*spa
= zilog
->zl_spa
;
1665 uint64_t *replayed_seq
= &zilog
->zl_replayed_seq
[txg
& TXG_MASK
];
1669 * We don't zero out zl_destroy_txg, so make sure we don't try
1670 * to destroy it twice.
1672 if (spa_sync_pass(spa
) != 1)
1675 mutex_enter(&zilog
->zl_lock
);
1677 ASSERT(zilog
->zl_stop_sync
== 0);
1679 if (*replayed_seq
!= 0) {
1680 ASSERT(zh
->zh_replay_seq
< *replayed_seq
);
1681 zh
->zh_replay_seq
= *replayed_seq
;
1685 if (zilog
->zl_destroy_txg
== txg
) {
1686 blkptr_t blk
= zh
->zh_log
;
1688 ASSERT(list_head(&zilog
->zl_lwb_list
) == NULL
);
1690 bzero(zh
, sizeof (zil_header_t
));
1691 bzero(zilog
->zl_replayed_seq
, sizeof (zilog
->zl_replayed_seq
));
1693 if (zilog
->zl_keep_first
) {
1695 * If this block was part of log chain that couldn't
1696 * be claimed because a device was missing during
1697 * zil_claim(), but that device later returns,
1698 * then this block could erroneously appear valid.
1699 * To guard against this, assign a new GUID to the new
1700 * log chain so it doesn't matter what blk points to.
1702 zil_init_log_chain(zilog
, &blk
);
1707 while ((lwb
= list_head(&zilog
->zl_lwb_list
)) != NULL
) {
1708 zh
->zh_log
= lwb
->lwb_blk
;
1709 if (lwb
->lwb_buf
!= NULL
|| lwb
->lwb_max_txg
> txg
)
1712 ASSERT(lwb
->lwb_zio
== NULL
);
1714 list_remove(&zilog
->zl_lwb_list
, lwb
);
1715 zio_free_zil(spa
, txg
, &lwb
->lwb_blk
);
1716 kmem_cache_free(zil_lwb_cache
, lwb
);
1719 * If we don't have anything left in the lwb list then
1720 * we've had an allocation failure and we need to zero
1721 * out the zil_header blkptr so that we don't end
1722 * up freeing the same block twice.
1724 if (list_head(&zilog
->zl_lwb_list
) == NULL
)
1725 BP_ZERO(&zh
->zh_log
);
1729 * Remove fastwrite on any blocks that have been pre-allocated for
1730 * the next commit. This prevents fastwrite counter pollution by
1731 * unused, long-lived LWBs.
1733 for (; lwb
!= NULL
; lwb
= list_next(&zilog
->zl_lwb_list
, lwb
)) {
1734 if (lwb
->lwb_fastwrite
&& !lwb
->lwb_zio
) {
1735 metaslab_fastwrite_unmark(zilog
->zl_spa
, &lwb
->lwb_blk
);
1736 lwb
->lwb_fastwrite
= 0;
1740 mutex_exit(&zilog
->zl_lock
);
1746 zil_lwb_cache
= kmem_cache_create("zil_lwb_cache",
1747 sizeof (struct lwb
), 0, NULL
, NULL
, NULL
, NULL
, NULL
, 0);
1749 zil_ksp
= kstat_create("zfs", 0, "zil", "misc",
1750 KSTAT_TYPE_NAMED
, sizeof (zil_stats
) / sizeof (kstat_named_t
),
1751 KSTAT_FLAG_VIRTUAL
);
1753 if (zil_ksp
!= NULL
) {
1754 zil_ksp
->ks_data
= &zil_stats
;
1755 kstat_install(zil_ksp
);
1762 kmem_cache_destroy(zil_lwb_cache
);
1764 if (zil_ksp
!= NULL
) {
1765 kstat_delete(zil_ksp
);
1771 zil_set_sync(zilog_t
*zilog
, uint64_t sync
)
1773 zilog
->zl_sync
= sync
;
1777 zil_set_logbias(zilog_t
*zilog
, uint64_t logbias
)
1779 zilog
->zl_logbias
= logbias
;
1783 zil_alloc(objset_t
*os
, zil_header_t
*zh_phys
)
1788 zilog
= kmem_zalloc(sizeof (zilog_t
), KM_SLEEP
);
1790 zilog
->zl_header
= zh_phys
;
1792 zilog
->zl_spa
= dmu_objset_spa(os
);
1793 zilog
->zl_dmu_pool
= dmu_objset_pool(os
);
1794 zilog
->zl_destroy_txg
= TXG_INITIAL
- 1;
1795 zilog
->zl_logbias
= dmu_objset_logbias(os
);
1796 zilog
->zl_sync
= dmu_objset_syncprop(os
);
1797 zilog
->zl_next_batch
= 1;
1799 mutex_init(&zilog
->zl_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1801 for (i
= 0; i
< TXG_SIZE
; i
++) {
1802 mutex_init(&zilog
->zl_itxg
[i
].itxg_lock
, NULL
,
1803 MUTEX_DEFAULT
, NULL
);
1806 list_create(&zilog
->zl_lwb_list
, sizeof (lwb_t
),
1807 offsetof(lwb_t
, lwb_node
));
1809 list_create(&zilog
->zl_itx_commit_list
, sizeof (itx_t
),
1810 offsetof(itx_t
, itx_node
));
1812 mutex_init(&zilog
->zl_vdev_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1814 avl_create(&zilog
->zl_vdev_tree
, zil_vdev_compare
,
1815 sizeof (zil_vdev_node_t
), offsetof(zil_vdev_node_t
, zv_node
));
1817 cv_init(&zilog
->zl_cv_writer
, NULL
, CV_DEFAULT
, NULL
);
1818 cv_init(&zilog
->zl_cv_suspend
, NULL
, CV_DEFAULT
, NULL
);
1819 cv_init(&zilog
->zl_cv_batch
[0], NULL
, CV_DEFAULT
, NULL
);
1820 cv_init(&zilog
->zl_cv_batch
[1], NULL
, CV_DEFAULT
, NULL
);
1826 zil_free(zilog_t
*zilog
)
1830 zilog
->zl_stop_sync
= 1;
1832 ASSERT0(zilog
->zl_suspend
);
1833 ASSERT0(zilog
->zl_suspending
);
1835 ASSERT(list_is_empty(&zilog
->zl_lwb_list
));
1836 list_destroy(&zilog
->zl_lwb_list
);
1838 avl_destroy(&zilog
->zl_vdev_tree
);
1839 mutex_destroy(&zilog
->zl_vdev_lock
);
1841 ASSERT(list_is_empty(&zilog
->zl_itx_commit_list
));
1842 list_destroy(&zilog
->zl_itx_commit_list
);
1844 for (i
= 0; i
< TXG_SIZE
; i
++) {
1846 * It's possible for an itx to be generated that doesn't dirty
1847 * a txg (e.g. ztest TX_TRUNCATE). So there's no zil_clean()
1848 * callback to remove the entry. We remove those here.
1850 * Also free up the ziltest itxs.
1852 if (zilog
->zl_itxg
[i
].itxg_itxs
)
1853 zil_itxg_clean(zilog
->zl_itxg
[i
].itxg_itxs
);
1854 mutex_destroy(&zilog
->zl_itxg
[i
].itxg_lock
);
1857 mutex_destroy(&zilog
->zl_lock
);
1859 cv_destroy(&zilog
->zl_cv_writer
);
1860 cv_destroy(&zilog
->zl_cv_suspend
);
1861 cv_destroy(&zilog
->zl_cv_batch
[0]);
1862 cv_destroy(&zilog
->zl_cv_batch
[1]);
1864 kmem_free(zilog
, sizeof (zilog_t
));
1868 * Open an intent log.
1871 zil_open(objset_t
*os
, zil_get_data_t
*get_data
)
1873 zilog_t
*zilog
= dmu_objset_zil(os
);
1875 ASSERT(zilog
->zl_clean_taskq
== NULL
);
1876 ASSERT(zilog
->zl_get_data
== NULL
);
1877 ASSERT(list_is_empty(&zilog
->zl_lwb_list
));
1879 zilog
->zl_get_data
= get_data
;
1880 zilog
->zl_clean_taskq
= taskq_create("zil_clean", 1, defclsyspri
,
1881 2, 2, TASKQ_PREPOPULATE
);
1887 * Close an intent log.
1890 zil_close(zilog_t
*zilog
)
1895 zil_commit(zilog
, 0); /* commit all itx */
1898 * The lwb_max_txg for the stubby lwb will reflect the last activity
1899 * for the zil. After a txg_wait_synced() on the txg we know all the
1900 * callbacks have occurred that may clean the zil. Only then can we
1901 * destroy the zl_clean_taskq.
1903 mutex_enter(&zilog
->zl_lock
);
1904 lwb
= list_tail(&zilog
->zl_lwb_list
);
1906 txg
= lwb
->lwb_max_txg
;
1907 mutex_exit(&zilog
->zl_lock
);
1909 txg_wait_synced(zilog
->zl_dmu_pool
, txg
);
1910 if (txg
< spa_freeze_txg(zilog
->zl_spa
))
1911 ASSERT(!zilog_is_dirty(zilog
));
1913 taskq_destroy(zilog
->zl_clean_taskq
);
1914 zilog
->zl_clean_taskq
= NULL
;
1915 zilog
->zl_get_data
= NULL
;
1918 * We should have only one LWB left on the list; remove it now.
1920 mutex_enter(&zilog
->zl_lock
);
1921 lwb
= list_head(&zilog
->zl_lwb_list
);
1923 ASSERT(lwb
== list_tail(&zilog
->zl_lwb_list
));
1924 ASSERT(lwb
->lwb_zio
== NULL
);
1925 if (lwb
->lwb_fastwrite
)
1926 metaslab_fastwrite_unmark(zilog
->zl_spa
, &lwb
->lwb_blk
);
1927 list_remove(&zilog
->zl_lwb_list
, lwb
);
1928 zio_buf_free(lwb
->lwb_buf
, lwb
->lwb_sz
);
1929 kmem_cache_free(zil_lwb_cache
, lwb
);
1931 mutex_exit(&zilog
->zl_lock
);
1934 static char *suspend_tag
= "zil suspending";
1937 * Suspend an intent log. While in suspended mode, we still honor
1938 * synchronous semantics, but we rely on txg_wait_synced() to do it.
1939 * On old version pools, we suspend the log briefly when taking a
1940 * snapshot so that it will have an empty intent log.
1942 * Long holds are not really intended to be used the way we do here --
1943 * held for such a short time. A concurrent caller of dsl_dataset_long_held()
1944 * could fail. Therefore we take pains to only put a long hold if it is
1945 * actually necessary. Fortunately, it will only be necessary if the
1946 * objset is currently mounted (or the ZVOL equivalent). In that case it
1947 * will already have a long hold, so we are not really making things any worse.
1949 * Ideally, we would locate the existing long-holder (i.e. the zfsvfs_t or
1950 * zvol_state_t), and use their mechanism to prevent their hold from being
1951 * dropped (e.g. VFS_HOLD()). However, that would be even more pain for
1954 * if cookiep == NULL, this does both the suspend & resume.
1955 * Otherwise, it returns with the dataset "long held", and the cookie
1956 * should be passed into zil_resume().
1959 zil_suspend(const char *osname
, void **cookiep
)
1963 const zil_header_t
*zh
;
1966 error
= dmu_objset_hold(osname
, suspend_tag
, &os
);
1969 zilog
= dmu_objset_zil(os
);
1971 mutex_enter(&zilog
->zl_lock
);
1972 zh
= zilog
->zl_header
;
1974 if (zh
->zh_flags
& ZIL_REPLAY_NEEDED
) { /* unplayed log */
1975 mutex_exit(&zilog
->zl_lock
);
1976 dmu_objset_rele(os
, suspend_tag
);
1977 return (SET_ERROR(EBUSY
));
1981 * Don't put a long hold in the cases where we can avoid it. This
1982 * is when there is no cookie so we are doing a suspend & resume
1983 * (i.e. called from zil_vdev_offline()), and there's nothing to do
1984 * for the suspend because it's already suspended, or there's no ZIL.
1986 if (cookiep
== NULL
&& !zilog
->zl_suspending
&&
1987 (zilog
->zl_suspend
> 0 || BP_IS_HOLE(&zh
->zh_log
))) {
1988 mutex_exit(&zilog
->zl_lock
);
1989 dmu_objset_rele(os
, suspend_tag
);
1993 dsl_dataset_long_hold(dmu_objset_ds(os
), suspend_tag
);
1994 dsl_pool_rele(dmu_objset_pool(os
), suspend_tag
);
1996 zilog
->zl_suspend
++;
1998 if (zilog
->zl_suspend
> 1) {
2000 * Someone else is already suspending it.
2001 * Just wait for them to finish.
2004 while (zilog
->zl_suspending
)
2005 cv_wait(&zilog
->zl_cv_suspend
, &zilog
->zl_lock
);
2006 mutex_exit(&zilog
->zl_lock
);
2008 if (cookiep
== NULL
)
2016 * If there is no pointer to an on-disk block, this ZIL must not
2017 * be active (e.g. filesystem not mounted), so there's nothing
2020 if (BP_IS_HOLE(&zh
->zh_log
)) {
2021 ASSERT(cookiep
!= NULL
); /* fast path already handled */
2024 mutex_exit(&zilog
->zl_lock
);
2028 zilog
->zl_suspending
= B_TRUE
;
2029 mutex_exit(&zilog
->zl_lock
);
2031 zil_commit(zilog
, 0);
2033 zil_destroy(zilog
, B_FALSE
);
2035 mutex_enter(&zilog
->zl_lock
);
2036 zilog
->zl_suspending
= B_FALSE
;
2037 cv_broadcast(&zilog
->zl_cv_suspend
);
2038 mutex_exit(&zilog
->zl_lock
);
2040 if (cookiep
== NULL
)
2048 zil_resume(void *cookie
)
2050 objset_t
*os
= cookie
;
2051 zilog_t
*zilog
= dmu_objset_zil(os
);
2053 mutex_enter(&zilog
->zl_lock
);
2054 ASSERT(zilog
->zl_suspend
!= 0);
2055 zilog
->zl_suspend
--;
2056 mutex_exit(&zilog
->zl_lock
);
2057 dsl_dataset_long_rele(dmu_objset_ds(os
), suspend_tag
);
2058 dsl_dataset_rele(dmu_objset_ds(os
), suspend_tag
);
2061 typedef struct zil_replay_arg
{
2062 zil_replay_func_t
*zr_replay
;
2064 boolean_t zr_byteswap
;
2069 zil_replay_error(zilog_t
*zilog
, lr_t
*lr
, int error
)
2071 char name
[ZFS_MAX_DATASET_NAME_LEN
];
2073 zilog
->zl_replaying_seq
--; /* didn't actually replay this one */
2075 dmu_objset_name(zilog
->zl_os
, name
);
2077 cmn_err(CE_WARN
, "ZFS replay transaction error %d, "
2078 "dataset %s, seq 0x%llx, txtype %llu %s\n", error
, name
,
2079 (u_longlong_t
)lr
->lrc_seq
,
2080 (u_longlong_t
)(lr
->lrc_txtype
& ~TX_CI
),
2081 (lr
->lrc_txtype
& TX_CI
) ? "CI" : "");
2087 zil_replay_log_record(zilog_t
*zilog
, lr_t
*lr
, void *zra
, uint64_t claim_txg
)
2089 zil_replay_arg_t
*zr
= zra
;
2090 const zil_header_t
*zh
= zilog
->zl_header
;
2091 uint64_t reclen
= lr
->lrc_reclen
;
2092 uint64_t txtype
= lr
->lrc_txtype
;
2095 zilog
->zl_replaying_seq
= lr
->lrc_seq
;
2097 if (lr
->lrc_seq
<= zh
->zh_replay_seq
) /* already replayed */
2100 if (lr
->lrc_txg
< claim_txg
) /* already committed */
2103 /* Strip case-insensitive bit, still present in log record */
2106 if (txtype
== 0 || txtype
>= TX_MAX_TYPE
)
2107 return (zil_replay_error(zilog
, lr
, EINVAL
));
2110 * If this record type can be logged out of order, the object
2111 * (lr_foid) may no longer exist. That's legitimate, not an error.
2113 if (TX_OOO(txtype
)) {
2114 error
= dmu_object_info(zilog
->zl_os
,
2115 LR_FOID_GET_OBJ(((lr_ooo_t
*)lr
)->lr_foid
), NULL
);
2116 if (error
== ENOENT
|| error
== EEXIST
)
2121 * Make a copy of the data so we can revise and extend it.
2123 bcopy(lr
, zr
->zr_lr
, reclen
);
2126 * If this is a TX_WRITE with a blkptr, suck in the data.
2128 if (txtype
== TX_WRITE
&& reclen
== sizeof (lr_write_t
)) {
2129 error
= zil_read_log_data(zilog
, (lr_write_t
*)lr
,
2130 zr
->zr_lr
+ reclen
);
2132 return (zil_replay_error(zilog
, lr
, error
));
2136 * The log block containing this lr may have been byteswapped
2137 * so that we can easily examine common fields like lrc_txtype.
2138 * However, the log is a mix of different record types, and only the
2139 * replay vectors know how to byteswap their records. Therefore, if
2140 * the lr was byteswapped, undo it before invoking the replay vector.
2142 if (zr
->zr_byteswap
)
2143 byteswap_uint64_array(zr
->zr_lr
, reclen
);
2146 * We must now do two things atomically: replay this log record,
2147 * and update the log header sequence number to reflect the fact that
2148 * we did so. At the end of each replay function the sequence number
2149 * is updated if we are in replay mode.
2151 error
= zr
->zr_replay
[txtype
](zr
->zr_arg
, zr
->zr_lr
, zr
->zr_byteswap
);
2154 * The DMU's dnode layer doesn't see removes until the txg
2155 * commits, so a subsequent claim can spuriously fail with
2156 * EEXIST. So if we receive any error we try syncing out
2157 * any removes then retry the transaction. Note that we
2158 * specify B_FALSE for byteswap now, so we don't do it twice.
2160 txg_wait_synced(spa_get_dsl(zilog
->zl_spa
), 0);
2161 error
= zr
->zr_replay
[txtype
](zr
->zr_arg
, zr
->zr_lr
, B_FALSE
);
2163 return (zil_replay_error(zilog
, lr
, error
));
2170 zil_incr_blks(zilog_t
*zilog
, blkptr_t
*bp
, void *arg
, uint64_t claim_txg
)
2172 zilog
->zl_replay_blks
++;
2178 * If this dataset has a non-empty intent log, replay it and destroy it.
2181 zil_replay(objset_t
*os
, void *arg
, zil_replay_func_t replay_func
[TX_MAX_TYPE
])
2183 zilog_t
*zilog
= dmu_objset_zil(os
);
2184 const zil_header_t
*zh
= zilog
->zl_header
;
2185 zil_replay_arg_t zr
;
2187 if ((zh
->zh_flags
& ZIL_REPLAY_NEEDED
) == 0) {
2188 zil_destroy(zilog
, B_TRUE
);
2192 zr
.zr_replay
= replay_func
;
2194 zr
.zr_byteswap
= BP_SHOULD_BYTESWAP(&zh
->zh_log
);
2195 zr
.zr_lr
= vmem_alloc(2 * SPA_MAXBLOCKSIZE
, KM_SLEEP
);
2198 * Wait for in-progress removes to sync before starting replay.
2200 txg_wait_synced(zilog
->zl_dmu_pool
, 0);
2202 zilog
->zl_replay
= B_TRUE
;
2203 zilog
->zl_replay_time
= ddi_get_lbolt();
2204 ASSERT(zilog
->zl_replay_blks
== 0);
2205 (void) zil_parse(zilog
, zil_incr_blks
, zil_replay_log_record
, &zr
,
2207 vmem_free(zr
.zr_lr
, 2 * SPA_MAXBLOCKSIZE
);
2209 zil_destroy(zilog
, B_FALSE
);
2210 txg_wait_synced(zilog
->zl_dmu_pool
, zilog
->zl_destroy_txg
);
2211 zilog
->zl_replay
= B_FALSE
;
2215 zil_replaying(zilog_t
*zilog
, dmu_tx_t
*tx
)
2217 if (zilog
->zl_sync
== ZFS_SYNC_DISABLED
)
2220 if (zilog
->zl_replay
) {
2221 dsl_dataset_dirty(dmu_objset_ds(zilog
->zl_os
), tx
);
2222 zilog
->zl_replayed_seq
[dmu_tx_get_txg(tx
) & TXG_MASK
] =
2223 zilog
->zl_replaying_seq
;
2232 zil_vdev_offline(const char *osname
, void *arg
)
2236 error
= zil_suspend(osname
, NULL
);
2238 return (SET_ERROR(EEXIST
));
2242 #if defined(_KERNEL) && defined(HAVE_SPL)
2243 EXPORT_SYMBOL(zil_alloc
);
2244 EXPORT_SYMBOL(zil_free
);
2245 EXPORT_SYMBOL(zil_open
);
2246 EXPORT_SYMBOL(zil_close
);
2247 EXPORT_SYMBOL(zil_replay
);
2248 EXPORT_SYMBOL(zil_replaying
);
2249 EXPORT_SYMBOL(zil_destroy
);
2250 EXPORT_SYMBOL(zil_destroy_sync
);
2251 EXPORT_SYMBOL(zil_itx_create
);
2252 EXPORT_SYMBOL(zil_itx_destroy
);
2253 EXPORT_SYMBOL(zil_itx_assign
);
2254 EXPORT_SYMBOL(zil_commit
);
2255 EXPORT_SYMBOL(zil_vdev_offline
);
2256 EXPORT_SYMBOL(zil_claim
);
2257 EXPORT_SYMBOL(zil_check_log_chain
);
2258 EXPORT_SYMBOL(zil_sync
);
2259 EXPORT_SYMBOL(zil_clean
);
2260 EXPORT_SYMBOL(zil_suspend
);
2261 EXPORT_SYMBOL(zil_resume
);
2262 EXPORT_SYMBOL(zil_add_block
);
2263 EXPORT_SYMBOL(zil_bp_tree_add
);
2264 EXPORT_SYMBOL(zil_set_sync
);
2265 EXPORT_SYMBOL(zil_set_logbias
);
2267 module_param(zil_replay_disable
, int, 0644);
2268 MODULE_PARM_DESC(zil_replay_disable
, "Disable intent logging replay");
2270 module_param(zfs_nocacheflush
, int, 0644);
2271 MODULE_PARM_DESC(zfs_nocacheflush
, "Disable cache flushes");
2274 module_param(zil_slog_limit
, ulong
, 0644);
2275 MODULE_PARM_DESC(zil_slog_limit
, "Max commit bytes to separate log device");