]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/zil.c
Add CONTRIBUTING information and templates
[mirror_zfs.git] / module / zfs / zil.c
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.
d3c2ae1c 23 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
34dc7c2f
BB
24 */
25
428870ff
BB
26/* Portions Copyright 2010 Robert Milkowski */
27
34dc7c2f
BB
28#include <sys/zfs_context.h>
29#include <sys/spa.h>
30#include <sys/dmu.h>
31#include <sys/zap.h>
32#include <sys/arc.h>
33#include <sys/stat.h>
34#include <sys/resource.h>
35#include <sys/zil.h>
36#include <sys/zil_impl.h>
37#include <sys/dsl_dataset.h>
572e2857 38#include <sys/vdev_impl.h>
34dc7c2f 39#include <sys/dmu_tx.h>
428870ff 40#include <sys/dsl_pool.h>
920dd524 41#include <sys/metaslab.h>
49ee64e5 42#include <sys/trace_zil.h>
a6255b7f 43#include <sys/abd.h>
34dc7c2f
BB
44
45/*
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.
54 *
55 * There is one ZIL per file system. Its on-disk (pool) format consists
56 * of 3 parts:
57 *
58 * - ZIL header
59 * - ZIL blocks
60 * - ZIL records
61 *
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:
69 */
70
b6ad9671
ED
71/*
72 * See zil.h for more information about these fields.
73 */
74zil_stats_t zil_stats = {
d1d7e268
MK
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 },
b6ad9671
ED
88};
89
90static kstat_t *zil_ksp;
91
34dc7c2f 92/*
d3cc8b15 93 * Disable intent logging replay. This global ZIL switch affects all pools.
34dc7c2f 94 */
d3cc8b15 95int zil_replay_disable = 0;
34dc7c2f
BB
96
97/*
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.
101 */
c409e464 102int zfs_nocacheflush = 0;
34dc7c2f
BB
103
104static kmem_cache_t *zil_lwb_cache;
105
572e2857 106static void zil_async_to_sync(zilog_t *zilog, uint64_t foid);
428870ff
BB
107
108#define LWB_EMPTY(lwb) ((BP_GET_LSIZE(&lwb->lwb_blk) - \
109 sizeof (zil_chain_t)) == (lwb->lwb_sz - lwb->lwb_nused))
110
111
572e2857
BB
112/*
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.
118 */
119#define ZILTEST_TXG (UINT64_MAX - TXG_CONCURRENT_STATES)
120
34dc7c2f 121static int
428870ff 122zil_bp_compare(const void *x1, const void *x2)
34dc7c2f 123{
428870ff
BB
124 const dva_t *dva1 = &((zil_bp_node_t *)x1)->zn_dva;
125 const dva_t *dva2 = &((zil_bp_node_t *)x2)->zn_dva;
34dc7c2f 126
ee36c709
GN
127 int cmp = AVL_CMP(DVA_GET_VDEV(dva1), DVA_GET_VDEV(dva2));
128 if (likely(cmp))
129 return (cmp);
34dc7c2f 130
ee36c709 131 return (AVL_CMP(DVA_GET_OFFSET(dva1), DVA_GET_OFFSET(dva2)));
34dc7c2f
BB
132}
133
134static void
428870ff 135zil_bp_tree_init(zilog_t *zilog)
34dc7c2f 136{
428870ff
BB
137 avl_create(&zilog->zl_bp_tree, zil_bp_compare,
138 sizeof (zil_bp_node_t), offsetof(zil_bp_node_t, zn_node));
34dc7c2f
BB
139}
140
141static void
428870ff 142zil_bp_tree_fini(zilog_t *zilog)
34dc7c2f 143{
428870ff
BB
144 avl_tree_t *t = &zilog->zl_bp_tree;
145 zil_bp_node_t *zn;
34dc7c2f
BB
146 void *cookie = NULL;
147
148 while ((zn = avl_destroy_nodes(t, &cookie)) != NULL)
428870ff 149 kmem_free(zn, sizeof (zil_bp_node_t));
34dc7c2f
BB
150
151 avl_destroy(t);
152}
153
428870ff
BB
154int
155zil_bp_tree_add(zilog_t *zilog, const blkptr_t *bp)
34dc7c2f 156{
428870ff 157 avl_tree_t *t = &zilog->zl_bp_tree;
9b67f605 158 const dva_t *dva;
428870ff 159 zil_bp_node_t *zn;
34dc7c2f
BB
160 avl_index_t where;
161
9b67f605
MA
162 if (BP_IS_EMBEDDED(bp))
163 return (0);
164
165 dva = BP_IDENTITY(bp);
166
34dc7c2f 167 if (avl_find(t, dva, &where) != NULL)
2e528b49 168 return (SET_ERROR(EEXIST));
34dc7c2f 169
79c76d5b 170 zn = kmem_alloc(sizeof (zil_bp_node_t), KM_SLEEP);
34dc7c2f
BB
171 zn->zn_dva = *dva;
172 avl_insert(t, zn, where);
173
174 return (0);
175}
176
177static zil_header_t *
178zil_header_in_syncing_context(zilog_t *zilog)
179{
180 return ((zil_header_t *)zilog->zl_header);
181}
182
183static void
184zil_init_log_chain(zilog_t *zilog, blkptr_t *bp)
185{
186 zio_cksum_t *zc = &bp->blk_cksum;
187
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;
192}
193
194/*
428870ff 195 * Read a log block and make sure it's valid.
34dc7c2f
BB
196 */
197static int
428870ff
BB
198zil_read_log_block(zilog_t *zilog, const blkptr_t *bp, blkptr_t *nbp, void *dst,
199 char **end)
34dc7c2f 200{
428870ff 201 enum zio_flag zio_flags = ZIO_FLAG_CANFAIL;
2a432414 202 arc_flags_t aflags = ARC_FLAG_WAIT;
428870ff 203 arc_buf_t *abuf = NULL;
5dbd68a3 204 zbookmark_phys_t zb;
34dc7c2f
BB
205 int error;
206
428870ff
BB
207 if (zilog->zl_header->zh_claim_txg == 0)
208 zio_flags |= ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB;
34dc7c2f 209
428870ff
BB
210 if (!(zilog->zl_header->zh_flags & ZIL_CLAIM_LR_SEQ_VALID))
211 zio_flags |= ZIO_FLAG_SPECULATIVE;
34dc7c2f 212
428870ff
BB
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]);
215
294f6806 216 error = arc_read(NULL, zilog->zl_spa, bp, arc_getbuf_func, &abuf,
428870ff 217 ZIO_PRIORITY_SYNC_READ, zio_flags, &aflags, &zb);
34dc7c2f
BB
218
219 if (error == 0) {
34dc7c2f
BB
220 zio_cksum_t cksum = bp->blk_cksum;
221
222 /*
b128c09f
BB
223 * Validate the checksummed log block.
224 *
34dc7c2f
BB
225 * Sequence numbers should be... sequential. The checksum
226 * verifier for the next block should be bp's checksum plus 1.
b128c09f
BB
227 *
228 * Also check the log chain linkage and size used.
34dc7c2f
BB
229 */
230 cksum.zc_word[ZIL_ZC_SEQ]++;
231
428870ff
BB
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);
34dc7c2f 236
428870ff
BB
237 if (bcmp(&cksum, &zilc->zc_next_blk.blk_cksum,
238 sizeof (cksum)) || BP_IS_HOLE(&zilc->zc_next_blk)) {
2e528b49 239 error = SET_ERROR(ECKSUM);
428870ff 240 } else {
f1512ee6 241 ASSERT3U(len, <=, SPA_OLD_MAXBLOCKSIZE);
428870ff
BB
242 bcopy(lr, dst, len);
243 *end = (char *)dst + len;
244 *nbp = zilc->zc_next_blk;
245 }
246 } else {
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;
250
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)))) {
2e528b49 254 error = SET_ERROR(ECKSUM);
428870ff 255 } else {
f1512ee6
MA
256 ASSERT3U(zilc->zc_nused, <=,
257 SPA_OLD_MAXBLOCKSIZE);
428870ff
BB
258 bcopy(lr, dst, zilc->zc_nused);
259 *end = (char *)dst + zilc->zc_nused;
260 *nbp = zilc->zc_next_blk;
261 }
34dc7c2f 262 }
428870ff 263
d3c2ae1c 264 arc_buf_destroy(abuf, &abuf);
428870ff
BB
265 }
266
267 return (error);
268}
269
270/*
271 * Read a TX_WRITE log data block.
272 */
273static int
274zil_read_log_data(zilog_t *zilog, const lr_write_t *lr, void *wbuf)
275{
276 enum zio_flag zio_flags = ZIO_FLAG_CANFAIL;
277 const blkptr_t *bp = &lr->lr_blkptr;
2a432414 278 arc_flags_t aflags = ARC_FLAG_WAIT;
428870ff 279 arc_buf_t *abuf = NULL;
5dbd68a3 280 zbookmark_phys_t zb;
428870ff
BB
281 int error;
282
283 if (BP_IS_HOLE(bp)) {
284 if (wbuf != NULL)
285 bzero(wbuf, MAX(BP_GET_LSIZE(bp), lr->lr_length));
286 return (0);
34dc7c2f
BB
287 }
288
428870ff
BB
289 if (zilog->zl_header->zh_claim_txg == 0)
290 zio_flags |= ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB;
291
292 SET_BOOKMARK(&zb, dmu_objset_id(zilog->zl_os), lr->lr_foid,
293 ZB_ZIL_LEVEL, lr->lr_offset / BP_GET_LSIZE(bp));
294
294f6806 295 error = arc_read(NULL, zilog->zl_spa, bp, arc_getbuf_func, &abuf,
428870ff
BB
296 ZIO_PRIORITY_SYNC_READ, zio_flags, &aflags, &zb);
297
298 if (error == 0) {
299 if (wbuf != NULL)
300 bcopy(abuf->b_data, wbuf, arc_buf_size(abuf));
d3c2ae1c 301 arc_buf_destroy(abuf, &abuf);
428870ff 302 }
34dc7c2f
BB
303
304 return (error);
305}
306
307/*
308 * Parse the intent log, and call parse_func for each valid record within.
34dc7c2f 309 */
428870ff 310int
34dc7c2f
BB
311zil_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)
313{
314 const zil_header_t *zh = zilog->zl_header;
428870ff
BB
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;
34dc7c2f 323 char *lrbuf, *lrp;
428870ff 324 int error = 0;
34dc7c2f 325
d1d7e268 326 bzero(&next_blk, sizeof (blkptr_t));
d4ed6673 327
428870ff
BB
328 /*
329 * Old logs didn't record the maximum zh_claim_lr_seq.
330 */
331 if (!(zh->zh_flags & ZIL_CLAIM_LR_SEQ_VALID))
332 claim_lr_seq = UINT64_MAX;
34dc7c2f
BB
333
334 /*
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.
342 */
f1512ee6 343 lrbuf = zio_buf_alloc(SPA_OLD_MAXBLOCKSIZE);
428870ff 344 zil_bp_tree_init(zilog);
34dc7c2f 345
428870ff
BB
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];
348 int reclen;
d4ed6673 349 char *end = NULL;
34dc7c2f 350
428870ff
BB
351 if (blk_seq > claim_blk_seq)
352 break;
353 if ((error = parse_blk_func(zilog, &blk, arg, txg)) != 0)
354 break;
355 ASSERT3U(max_blk_seq, <, blk_seq);
356 max_blk_seq = blk_seq;
357 blk_count++;
34dc7c2f 358
428870ff
BB
359 if (max_lr_seq == claim_lr_seq && max_blk_seq == claim_blk_seq)
360 break;
34dc7c2f 361
428870ff 362 error = zil_read_log_block(zilog, &blk, &next_blk, lrbuf, &end);
13fe0198 363 if (error != 0)
34dc7c2f
BB
364 break;
365
428870ff 366 for (lrp = lrbuf; lrp < end; lrp += reclen) {
34dc7c2f
BB
367 lr_t *lr = (lr_t *)lrp;
368 reclen = lr->lrc_reclen;
369 ASSERT3U(reclen, >=, sizeof (lr_t));
428870ff
BB
370 if (lr->lrc_seq > claim_lr_seq)
371 goto done;
372 if ((error = parse_lr_func(zilog, lr, arg, txg)) != 0)
373 goto done;
374 ASSERT3U(max_lr_seq, <, lr->lrc_seq);
375 max_lr_seq = lr->lrc_seq;
376 lr_count++;
34dc7c2f 377 }
34dc7c2f 378 }
428870ff
BB
379done:
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;
385
386 ASSERT(!claimed || !(zh->zh_flags & ZIL_CLAIM_LR_SEQ_VALID) ||
387 (max_blk_seq == claim_blk_seq && max_lr_seq == claim_lr_seq));
388
389 zil_bp_tree_fini(zilog);
f1512ee6 390 zio_buf_free(lrbuf, SPA_OLD_MAXBLOCKSIZE);
34dc7c2f 391
428870ff 392 return (error);
34dc7c2f
BB
393}
394
428870ff 395static int
34dc7c2f
BB
396zil_claim_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t first_txg)
397{
34dc7c2f
BB
398 /*
399 * Claim log block if not already committed and not already claimed.
428870ff 400 * If tx == NULL, just verify that the block is claimable.
34dc7c2f 401 */
b0bc7a84
MG
402 if (BP_IS_HOLE(bp) || bp->blk_birth < first_txg ||
403 zil_bp_tree_add(zilog, bp) != 0)
428870ff
BB
404 return (0);
405
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)));
34dc7c2f
BB
409}
410
428870ff 411static int
34dc7c2f
BB
412zil_claim_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg)
413{
428870ff
BB
414 lr_write_t *lr = (lr_write_t *)lrc;
415 int error;
416
417 if (lrc->lrc_txtype != TX_WRITE)
418 return (0);
419
420 /*
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.
427 */
428 if (lr->lr_blkptr.blk_birth >= first_txg &&
429 (error = zil_read_log_data(zilog, lr, NULL)) != 0)
430 return (error);
431 return (zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg));
34dc7c2f
BB
432}
433
434/* ARGSUSED */
428870ff 435static int
34dc7c2f
BB
436zil_free_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t claim_txg)
437{
428870ff
BB
438 zio_free_zil(zilog->zl_spa, dmu_tx_get_txg(tx), bp);
439
440 return (0);
34dc7c2f
BB
441}
442
428870ff 443static int
34dc7c2f
BB
444zil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg)
445{
428870ff
BB
446 lr_write_t *lr = (lr_write_t *)lrc;
447 blkptr_t *bp = &lr->lr_blkptr;
448
34dc7c2f
BB
449 /*
450 * If we previously claimed it, we need to free it.
451 */
428870ff 452 if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE &&
b0bc7a84
MG
453 bp->blk_birth >= claim_txg && zil_bp_tree_add(zilog, bp) == 0 &&
454 !BP_IS_HOLE(bp))
428870ff
BB
455 zio_free(zilog->zl_spa, dmu_tx_get_txg(tx), bp);
456
457 return (0);
458}
459
460static lwb_t *
920dd524 461zil_alloc_lwb(zilog_t *zilog, blkptr_t *bp, uint64_t txg, boolean_t fastwrite)
428870ff
BB
462{
463 lwb_t *lwb;
464
79c76d5b 465 lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP);
428870ff
BB
466 lwb->lwb_zilog = zilog;
467 lwb->lwb_blk = *bp;
920dd524 468 lwb->lwb_fastwrite = fastwrite;
428870ff
BB
469 lwb->lwb_buf = zio_buf_alloc(BP_GET_LSIZE(bp));
470 lwb->lwb_max_txg = txg;
471 lwb->lwb_zio = NULL;
472 lwb->lwb_tx = NULL;
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);
476 } else {
477 lwb->lwb_nused = 0;
478 lwb->lwb_sz = BP_GET_LSIZE(bp) - sizeof (zil_chain_t);
34dc7c2f 479 }
428870ff
BB
480
481 mutex_enter(&zilog->zl_lock);
482 list_insert_tail(&zilog->zl_lwb_list, lwb);
483 mutex_exit(&zilog->zl_lock);
484
485 return (lwb);
34dc7c2f
BB
486}
487
29809a6c
MA
488/*
489 * Called when we create in-memory log transactions so that we know
490 * to cleanup the itxs at the end of spa_sync().
491 */
492void
493zilog_dirty(zilog_t *zilog, uint64_t txg)
494{
495 dsl_pool_t *dp = zilog->zl_dmu_pool;
496 dsl_dataset_t *ds = dmu_objset_ds(zilog->zl_os);
497
0c66c32d 498 if (ds->ds_is_snapshot)
29809a6c
MA
499 panic("dirtying snapshot!");
500
13fe0198 501 if (txg_list_add(&dp->dp_dirty_zilogs, zilog, txg)) {
29809a6c
MA
502 /* up the hold count until we can be written out */
503 dmu_buf_add_ref(ds->ds_dbuf, zilog);
504 }
505}
506
507boolean_t
508zilog_is_dirty(zilog_t *zilog)
509{
510 dsl_pool_t *dp = zilog->zl_dmu_pool;
511 int t;
512
513 for (t = 0; t < TXG_SIZE; t++) {
514 if (txg_list_member(&dp->dp_dirty_zilogs, zilog, t))
515 return (B_TRUE);
516 }
517 return (B_FALSE);
518}
519
34dc7c2f
BB
520/*
521 * Create an on-disk intent log.
522 */
428870ff 523static lwb_t *
34dc7c2f
BB
524zil_create(zilog_t *zilog)
525{
526 const zil_header_t *zh = zilog->zl_header;
428870ff 527 lwb_t *lwb = NULL;
34dc7c2f
BB
528 uint64_t txg = 0;
529 dmu_tx_t *tx = NULL;
530 blkptr_t blk;
531 int error = 0;
920dd524 532 boolean_t fastwrite = FALSE;
34dc7c2f
BB
533
534 /*
535 * Wait for any previous destroy to complete.
536 */
537 txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
538
539 ASSERT(zh->zh_claim_txg == 0);
540 ASSERT(zh->zh_replay_seq == 0);
541
542 blk = zh->zh_log;
543
544 /*
428870ff
BB
545 * Allocate an initial log block if:
546 * - there isn't one already
547 * - the existing block is the wrong endianess
34dc7c2f 548 */
fb5f0bc8 549 if (BP_IS_HOLE(&blk) || BP_SHOULD_BYTESWAP(&blk)) {
34dc7c2f 550 tx = dmu_tx_create(zilog->zl_os);
428870ff 551 VERIFY(dmu_tx_assign(tx, TXG_WAIT) == 0);
34dc7c2f
BB
552 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
553 txg = dmu_tx_get_txg(tx);
554
fb5f0bc8 555 if (!BP_IS_HOLE(&blk)) {
428870ff 556 zio_free_zil(zilog->zl_spa, txg, &blk);
fb5f0bc8
BB
557 BP_ZERO(&blk);
558 }
559
920dd524 560 error = zio_alloc_zil(zilog->zl_spa, txg, &blk,
5d7a86d1 561 ZIL_MIN_BLKSZ, B_TRUE);
920dd524 562 fastwrite = TRUE;
34dc7c2f
BB
563
564 if (error == 0)
565 zil_init_log_chain(zilog, &blk);
566 }
567
568 /*
569 * Allocate a log write buffer (lwb) for the first log block.
570 */
428870ff 571 if (error == 0)
920dd524 572 lwb = zil_alloc_lwb(zilog, &blk, txg, fastwrite);
34dc7c2f
BB
573
574 /*
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.)
578 */
579 if (tx != NULL) {
580 dmu_tx_commit(tx);
581 txg_wait_synced(zilog->zl_dmu_pool, txg);
582 }
583
584 ASSERT(bcmp(&blk, &zh->zh_log, sizeof (blk)) == 0);
428870ff
BB
585
586 return (lwb);
34dc7c2f
BB
587}
588
589/*
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.
597 */
598void
599zil_destroy(zilog_t *zilog, boolean_t keep_first)
600{
601 const zil_header_t *zh = zilog->zl_header;
602 lwb_t *lwb;
603 dmu_tx_t *tx;
604 uint64_t txg;
605
606 /*
607 * Wait for any previous destroy to complete.
608 */
609 txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
610
428870ff
BB
611 zilog->zl_old_header = *zh; /* debugging aid */
612
34dc7c2f
BB
613 if (BP_IS_HOLE(&zh->zh_log))
614 return;
615
616 tx = dmu_tx_create(zilog->zl_os);
428870ff 617 VERIFY(dmu_tx_assign(tx, TXG_WAIT) == 0);
34dc7c2f
BB
618 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
619 txg = dmu_tx_get_txg(tx);
620
621 mutex_enter(&zilog->zl_lock);
622
34dc7c2f
BB
623 ASSERT3U(zilog->zl_destroy_txg, <, txg);
624 zilog->zl_destroy_txg = txg;
625 zilog->zl_keep_first = keep_first;
626
627 if (!list_is_empty(&zilog->zl_lwb_list)) {
628 ASSERT(zh->zh_claim_txg == 0);
3e31d2b0 629 VERIFY(!keep_first);
34dc7c2f 630 while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
920dd524
ED
631 ASSERT(lwb->lwb_zio == NULL);
632 if (lwb->lwb_fastwrite)
633 metaslab_fastwrite_unmark(zilog->zl_spa,
634 &lwb->lwb_blk);
34dc7c2f
BB
635 list_remove(&zilog->zl_lwb_list, lwb);
636 if (lwb->lwb_buf != NULL)
637 zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
428870ff 638 zio_free_zil(zilog->zl_spa, txg, &lwb->lwb_blk);
34dc7c2f
BB
639 kmem_cache_free(zil_lwb_cache, lwb);
640 }
428870ff 641 } else if (!keep_first) {
29809a6c 642 zil_destroy_sync(zilog, tx);
34dc7c2f
BB
643 }
644 mutex_exit(&zilog->zl_lock);
645
646 dmu_tx_commit(tx);
647}
648
29809a6c
MA
649void
650zil_destroy_sync(zilog_t *zilog, dmu_tx_t *tx)
651{
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);
655}
656
34dc7c2f 657int
9c43027b 658zil_claim(dsl_pool_t *dp, dsl_dataset_t *ds, void *txarg)
34dc7c2f
BB
659{
660 dmu_tx_t *tx = txarg;
661 uint64_t first_txg = dmu_tx_get_txg(tx);
662 zilog_t *zilog;
663 zil_header_t *zh;
664 objset_t *os;
665 int error;
666
9c43027b
AJ
667 error = dmu_objset_own_obj(dp, ds->ds_object,
668 DMU_OST_ANY, B_FALSE, FTAG, &os);
13fe0198 669 if (error != 0) {
6d9036f3
MA
670 /*
671 * EBUSY indicates that the objset is inconsistent, in which
672 * case it can not have a ZIL.
673 */
674 if (error != EBUSY) {
9c43027b
AJ
675 cmn_err(CE_WARN, "can't open objset for %llu, error %u",
676 (unsigned long long)ds->ds_object, error);
6d9036f3
MA
677 }
678
34dc7c2f
BB
679 return (0);
680 }
681
682 zilog = dmu_objset_zil(os);
683 zh = zil_header_in_syncing_context(zilog);
684
428870ff 685 if (spa_get_log_state(zilog->zl_spa) == SPA_LOG_CLEAR) {
9babb374 686 if (!BP_IS_HOLE(&zh->zh_log))
428870ff 687 zio_free_zil(zilog->zl_spa, first_txg, &zh->zh_log);
9babb374
BB
688 BP_ZERO(&zh->zh_log);
689 dsl_dataset_dirty(dmu_objset_ds(os), tx);
13fe0198 690 dmu_objset_disown(os, FTAG);
428870ff 691 return (0);
9babb374
BB
692 }
693
34dc7c2f
BB
694 /*
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.
700 */
701 ASSERT3U(zh->zh_claim_txg, <=, first_txg);
702 if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) {
428870ff 703 (void) zil_parse(zilog, zil_claim_log_block,
34dc7c2f 704 zil_claim_log_record, tx, first_txg);
428870ff
BB
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;
34dc7c2f
BB
711 dsl_dataset_dirty(dmu_objset_ds(os), tx);
712 }
713
714 ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1));
13fe0198 715 dmu_objset_disown(os, FTAG);
34dc7c2f
BB
716 return (0);
717}
718
b128c09f
BB
719/*
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.
723 */
9c43027b 724/* ARGSUSED */
b128c09f 725int
9c43027b 726zil_check_log_chain(dsl_pool_t *dp, dsl_dataset_t *ds, void *tx)
b128c09f
BB
727{
728 zilog_t *zilog;
b128c09f 729 objset_t *os;
572e2857 730 blkptr_t *bp;
b128c09f
BB
731 int error;
732
428870ff
BB
733 ASSERT(tx == NULL);
734
9c43027b 735 error = dmu_objset_from_ds(ds, &os);
13fe0198 736 if (error != 0) {
9c43027b
AJ
737 cmn_err(CE_WARN, "can't open objset %llu, error %d",
738 (unsigned long long)ds->ds_object, error);
b128c09f
BB
739 return (0);
740 }
741
742 zilog = dmu_objset_zil(os);
572e2857
BB
743 bp = (blkptr_t *)&zilog->zl_header->zh_log;
744
745 /*
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.
750 */
751 if (!BP_IS_HOLE(bp)) {
752 vdev_t *vd;
753 boolean_t valid = B_TRUE;
754
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);
760
9c43027b 761 if (!valid)
572e2857 762 return (0);
572e2857 763 }
b128c09f 764
428870ff
BB
765 /*
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.
771 */
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));
774
428870ff 775 return ((error == ECKSUM || error == ENOENT) ? 0 : error);
b128c09f
BB
776}
777
34dc7c2f
BB
778static int
779zil_vdev_compare(const void *x1, const void *x2)
780{
572e2857
BB
781 const uint64_t v1 = ((zil_vdev_node_t *)x1)->zv_vdev;
782 const uint64_t v2 = ((zil_vdev_node_t *)x2)->zv_vdev;
34dc7c2f 783
ee36c709 784 return (AVL_CMP(v1, v2));
34dc7c2f
BB
785}
786
787void
428870ff 788zil_add_block(zilog_t *zilog, const blkptr_t *bp)
34dc7c2f
BB
789{
790 avl_tree_t *t = &zilog->zl_vdev_tree;
791 avl_index_t where;
792 zil_vdev_node_t *zv, zvsearch;
793 int ndvas = BP_GET_NDVAS(bp);
794 int i;
795
796 if (zfs_nocacheflush)
797 return;
798
799 ASSERT(zilog->zl_writer);
800
801 /*
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.
805 */
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) {
79c76d5b 810 zv = kmem_alloc(sizeof (*zv), KM_SLEEP);
34dc7c2f
BB
811 zv->zv_vdev = zvsearch.zv_vdev;
812 avl_insert(t, zv, where);
813 }
814 }
815 mutex_exit(&zilog->zl_vdev_lock);
816}
817
572e2857 818static void
34dc7c2f
BB
819zil_flush_vdevs(zilog_t *zilog)
820{
821 spa_t *spa = zilog->zl_spa;
822 avl_tree_t *t = &zilog->zl_vdev_tree;
823 void *cookie = NULL;
824 zil_vdev_node_t *zv;
825 zio_t *zio;
826
827 ASSERT(zilog->zl_writer);
828
829 /*
830 * We don't need zl_vdev_lock here because we're the zl_writer,
831 * and all zl_get_data() callbacks are done.
832 */
833 if (avl_numnodes(t) == 0)
834 return;
835
b128c09f 836 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
34dc7c2f 837
b128c09f 838 zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
34dc7c2f
BB
839
840 while ((zv = avl_destroy_nodes(t, &cookie)) != NULL) {
841 vdev_t *vd = vdev_lookup_top(spa, zv->zv_vdev);
842 if (vd != NULL)
843 zio_flush(zio, vd);
844 kmem_free(zv, sizeof (*zv));
845 }
846
847 /*
848 * Wait for all the flushes to complete. Not all devices actually
849 * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails.
850 */
851 (void) zio_wait(zio);
852
b128c09f 853 spa_config_exit(spa, SCL_STATE, FTAG);
34dc7c2f
BB
854}
855
856/*
857 * Function called when a log block write completes
858 */
859static void
860zil_lwb_write_done(zio_t *zio)
861{
862 lwb_t *lwb = zio->io_private;
863 zilog_t *zilog = lwb->lwb_zilog;
428870ff 864 dmu_tx_t *tx = lwb->lwb_tx;
34dc7c2f 865
b128c09f 866 ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF);
b128c09f
BB
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));
9b67f605 872 ASSERT(BP_GET_FILL(zio->io_bp) == 0);
b128c09f 873
34dc7c2f 874 /*
9babb374
BB
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.
34dc7c2f 881 */
a6255b7f 882 abd_put(zio->io_abd);
34dc7c2f
BB
883 zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
884 mutex_enter(&zilog->zl_lock);
920dd524
ED
885 lwb->lwb_zio = NULL;
886 lwb->lwb_fastwrite = FALSE;
34dc7c2f 887 lwb->lwb_buf = NULL;
428870ff
BB
888 lwb->lwb_tx = NULL;
889 mutex_exit(&zilog->zl_lock);
9babb374
BB
890
891 /*
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
428870ff 894 * which we allocated the next block sync.
9babb374 895 */
428870ff 896 dmu_tx_commit(tx);
34dc7c2f
BB
897}
898
899/*
900 * Initialize the io for a log block.
34dc7c2f
BB
901 */
902static void
903zil_lwb_write_init(zilog_t *zilog, lwb_t *lwb)
904{
5dbd68a3 905 zbookmark_phys_t zb;
34dc7c2f 906
428870ff
BB
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]);
34dc7c2f
BB
910
911 if (zilog->zl_root_zio == NULL) {
912 zilog->zl_root_zio = zio_root(zilog->zl_spa, NULL, NULL,
913 ZIO_FLAG_CANFAIL);
914 }
920dd524
ED
915
916 /* Lock so zil_sync() doesn't fastwrite_unmark after zio is created */
917 mutex_enter(&zilog->zl_lock);
34dc7c2f 918 if (lwb->lwb_zio == NULL) {
a6255b7f
DQ
919 abd_t *lwb_abd = abd_get_from_buf(lwb->lwb_buf,
920 BP_GET_LSIZE(&lwb->lwb_blk));
920dd524
ED
921 if (!lwb->lwb_fastwrite) {
922 metaslab_fastwrite_mark(zilog->zl_spa, &lwb->lwb_blk);
923 lwb->lwb_fastwrite = 1;
924 }
34dc7c2f 925 lwb->lwb_zio = zio_rewrite(zilog->zl_root_zio, zilog->zl_spa,
a6255b7f 926 0, &lwb->lwb_blk, lwb_abd, BP_GET_LSIZE(&lwb->lwb_blk),
e8b96c60 927 zil_lwb_write_done, lwb, ZIO_PRIORITY_SYNC_WRITE,
920dd524
ED
928 ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE |
929 ZIO_FLAG_FASTWRITE, &zb);
34dc7c2f 930 }
920dd524 931 mutex_exit(&zilog->zl_lock);
34dc7c2f
BB
932}
933
428870ff
BB
934/*
935 * Define a limited set of intent log block sizes.
d3cc8b15 936 *
428870ff
BB
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
f1512ee6 939 * allocate SPA_OLD_MAXBLOCKSIZE as the slog space could be exhausted.
428870ff
BB
940 */
941uint64_t zil_block_buckets[] = {
942 4096, /* non TX_WRITE */
943 8192+4096, /* data base */
944 32*1024 + 4096, /* NFS writes */
945 UINT64_MAX
946};
947
948/*
5d7a86d1
ED
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.
428870ff 952 */
ee191e80 953unsigned long zil_slog_limit = 1024 * 1024;
5d7a86d1
ED
954#define USE_SLOG(zilog) (((zilog)->zl_cur_used < zil_slog_limit) || \
955 ((zilog)->zl_itx_list_sz < (zil_slog_limit << 1)))
428870ff 956
34dc7c2f
BB
957/*
958 * Start a log block write and advance to the next log block.
959 * Calls are serialized.
960 */
961static lwb_t *
962zil_lwb_write_start(zilog_t *zilog, lwb_t *lwb)
963{
428870ff
BB
964 lwb_t *nlwb = NULL;
965 zil_chain_t *zilc;
34dc7c2f 966 spa_t *spa = zilog->zl_spa;
428870ff
BB
967 blkptr_t *bp;
968 dmu_tx_t *tx;
34dc7c2f 969 uint64_t txg;
428870ff
BB
970 uint64_t zil_blksz, wsz;
971 int i, error;
b6ad9671 972 boolean_t use_slog;
428870ff
BB
973
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;
977 } else {
978 zilc = (zil_chain_t *)(lwb->lwb_buf + lwb->lwb_sz);
979 bp = &zilc->zc_next_blk;
980 }
34dc7c2f 981
428870ff 982 ASSERT(lwb->lwb_nused <= lwb->lwb_sz);
34dc7c2f
BB
983
984 /*
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.
428870ff
BB
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.
34dc7c2f 992 */
428870ff
BB
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);
997
998 lwb->lwb_tx = tx;
34dc7c2f
BB
999
1000 /*
428870ff
BB
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
1006 * close.
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
1010 * requests.
1011 *
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
1014 * pool log space.
34dc7c2f 1015 */
428870ff
BB
1016 zil_blksz = zilog->zl_cur_used + sizeof (zil_chain_t);
1017 for (i = 0; zil_blksz > zil_block_buckets[i]; i++)
1018 continue;
1019 zil_blksz = zil_block_buckets[i];
1020 if (zil_blksz == UINT64_MAX)
f1512ee6 1021 zil_blksz = SPA_OLD_MAXBLOCKSIZE;
428870ff
BB
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);
34dc7c2f
BB
1026
1027 BP_ZERO(bp);
b6ad9671 1028 use_slog = USE_SLOG(zilog);
13fe0198
MA
1029 error = zio_alloc_zil(spa, txg, bp, zil_blksz,
1030 USE_SLOG(zilog));
d1d7e268 1031 if (use_slog) {
b6ad9671
ED
1032 ZIL_STAT_BUMP(zil_itx_metaslab_slog_count);
1033 ZIL_STAT_INCR(zil_itx_metaslab_slog_bytes, lwb->lwb_nused);
d1d7e268 1034 } else {
b6ad9671
ED
1035 ZIL_STAT_BUMP(zil_itx_metaslab_normal_count);
1036 ZIL_STAT_INCR(zil_itx_metaslab_normal_bytes, lwb->lwb_nused);
1037 }
13fe0198 1038 if (error == 0) {
428870ff
BB
1039 ASSERT3U(bp->blk_birth, ==, txg);
1040 bp->blk_cksum = lwb->lwb_blk.blk_cksum;
1041 bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++;
34dc7c2f
BB
1042
1043 /*
428870ff 1044 * Allocate a new log write buffer (lwb).
34dc7c2f 1045 */
920dd524 1046 nlwb = zil_alloc_lwb(zilog, bp, txg, TRUE);
34dc7c2f 1047
428870ff
BB
1048 /* Record the block for later vdev flushing */
1049 zil_add_block(zilog, &lwb->lwb_blk);
34dc7c2f
BB
1050 }
1051
428870ff
BB
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);
34dc7c2f 1057
428870ff
BB
1058 } else {
1059 wsz = lwb->lwb_sz;
1060 }
34dc7c2f 1061
428870ff
BB
1062 zilc->zc_pad = 0;
1063 zilc->zc_nused = lwb->lwb_nused;
1064 zilc->zc_eck.zec_cksum = lwb->lwb_blk.blk_cksum;
34dc7c2f
BB
1065
1066 /*
428870ff 1067 * clear unused data for security
34dc7c2f 1068 */
428870ff 1069 bzero(lwb->lwb_buf + lwb->lwb_nused, wsz - lwb->lwb_nused);
34dc7c2f 1070
428870ff 1071 zio_nowait(lwb->lwb_zio); /* Kick off the write for the old log block */
34dc7c2f
BB
1072
1073 /*
428870ff
BB
1074 * If there was an allocation failure then nlwb will be null which
1075 * forces a txg_wait_synced().
34dc7c2f 1076 */
34dc7c2f
BB
1077 return (nlwb);
1078}
1079
1080static lwb_t *
1081zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb)
1082{
1083 lr_t *lrc = &itx->itx_lr; /* common log record */
428870ff
BB
1084 lr_write_t *lrw = (lr_write_t *)lrc;
1085 char *lr_buf;
34dc7c2f
BB
1086 uint64_t txg = lrc->lrc_txg;
1087 uint64_t reclen = lrc->lrc_reclen;
428870ff 1088 uint64_t dlen = 0;
34dc7c2f
BB
1089
1090 if (lwb == NULL)
1091 return (NULL);
428870ff 1092
34dc7c2f 1093 ASSERT(lwb->lwb_buf != NULL);
29809a6c
MA
1094 ASSERT(zilog_is_dirty(zilog) ||
1095 spa_freeze_txg(zilog->zl_spa) != UINT64_MAX);
34dc7c2f
BB
1096
1097 if (lrc->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY)
1098 dlen = P2ROUNDUP_TYPED(
428870ff 1099 lrw->lr_length, sizeof (uint64_t), uint64_t);
34dc7c2f
BB
1100
1101 zilog->zl_cur_used += (reclen + dlen);
1102
1103 zil_lwb_write_init(zilog, lwb);
1104
1105 /*
1106 * If this record won't fit in the current log block, start a new one.
1107 */
428870ff 1108 if (lwb->lwb_nused + reclen + dlen > lwb->lwb_sz) {
34dc7c2f
BB
1109 lwb = zil_lwb_write_start(zilog, lwb);
1110 if (lwb == NULL)
1111 return (NULL);
1112 zil_lwb_write_init(zilog, lwb);
428870ff
BB
1113 ASSERT(LWB_EMPTY(lwb));
1114 if (lwb->lwb_nused + reclen + dlen > lwb->lwb_sz) {
34dc7c2f
BB
1115 txg_wait_synced(zilog->zl_dmu_pool, txg);
1116 return (lwb);
1117 }
1118 }
1119
428870ff
BB
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;
34dc7c2f 1124
b6ad9671
ED
1125 ZIL_STAT_BUMP(zil_itx_count);
1126
34dc7c2f
BB
1127 /*
1128 * If it's a write, fetch the data or get its blkptr as appropriate.
1129 */
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);
b6ad9671
ED
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);
1136 } else {
34dc7c2f
BB
1137 char *dbuf;
1138 int error;
1139
34dc7c2f
BB
1140 if (dlen) {
1141 ASSERT(itx->itx_wr_state == WR_NEED_COPY);
428870ff
BB
1142 dbuf = lr_buf + reclen;
1143 lrw->lr_common.lrc_reclen += dlen;
b6ad9671 1144 ZIL_STAT_BUMP(zil_itx_needcopy_count);
d1d7e268
MK
1145 ZIL_STAT_INCR(zil_itx_needcopy_bytes,
1146 lrw->lr_length);
34dc7c2f
BB
1147 } else {
1148 ASSERT(itx->itx_wr_state == WR_INDIRECT);
1149 dbuf = NULL;
b6ad9671 1150 ZIL_STAT_BUMP(zil_itx_indirect_count);
d1d7e268
MK
1151 ZIL_STAT_INCR(zil_itx_indirect_bytes,
1152 lrw->lr_length);
34dc7c2f
BB
1153 }
1154 error = zilog->zl_get_data(
428870ff 1155 itx->itx_private, lrw, dbuf, lwb->lwb_zio);
45d1cae3
BB
1156 if (error == EIO) {
1157 txg_wait_synced(zilog->zl_dmu_pool, txg);
1158 return (lwb);
1159 }
13fe0198 1160 if (error != 0) {
34dc7c2f
BB
1161 ASSERT(error == ENOENT || error == EEXIST ||
1162 error == EALREADY);
1163 return (lwb);
1164 }
1165 }
1166 }
1167
428870ff
BB
1168 /*
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.
1173 */
1174 lrc->lrc_seq = ++zilog->zl_lr_seq; /* we are single threaded */
34dc7c2f
BB
1175 lwb->lwb_nused += reclen + dlen;
1176 lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg);
428870ff 1177 ASSERT3U(lwb->lwb_nused, <=, lwb->lwb_sz);
c99c9001 1178 ASSERT0(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)));
34dc7c2f
BB
1179
1180 return (lwb);
1181}
1182
1183itx_t *
1184zil_itx_create(uint64_t txtype, size_t lrsize)
1185{
1186 itx_t *itx;
1187
1188 lrsize = P2ROUNDUP_TYPED(lrsize, sizeof (uint64_t), size_t);
1189
19ea3d25 1190 itx = zio_data_buf_alloc(offsetof(itx_t, itx_lr) + lrsize);
34dc7c2f
BB
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 */
572e2857 1195 itx->itx_sync = B_TRUE; /* default is synchronous */
119a394a
ED
1196 itx->itx_callback = NULL;
1197 itx->itx_callback_data = NULL;
34dc7c2f
BB
1198
1199 return (itx);
1200}
1201
428870ff
BB
1202void
1203zil_itx_destroy(itx_t *itx)
1204{
19ea3d25 1205 zio_data_buf_free(itx, offsetof(itx_t, itx_lr)+itx->itx_lr.lrc_reclen);
428870ff
BB
1206}
1207
572e2857
BB
1208/*
1209 * Free up the sync and async itxs. The itxs_t has already been detached
1210 * so no locks are needed.
1211 */
1212static void
1213zil_itxg_clean(itxs_t *itxs)
34dc7c2f 1214{
572e2857
BB
1215 itx_t *itx;
1216 list_t *list;
1217 avl_tree_t *t;
1218 void *cookie;
1219 itx_async_node_t *ian;
1220
1221 list = &itxs->i_sync_list;
1222 while ((itx = list_head(list)) != NULL) {
119a394a
ED
1223 if (itx->itx_callback != NULL)
1224 itx->itx_callback(itx->itx_callback_data);
572e2857 1225 list_remove(list, itx);
19ea3d25 1226 zil_itx_destroy(itx);
572e2857 1227 }
34dc7c2f 1228
572e2857
BB
1229 cookie = NULL;
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) {
119a394a
ED
1234 if (itx->itx_callback != NULL)
1235 itx->itx_callback(itx->itx_callback_data);
572e2857 1236 list_remove(list, itx);
19ea3d25 1237 zil_itx_destroy(itx);
572e2857
BB
1238 }
1239 list_destroy(list);
1240 kmem_free(ian, sizeof (itx_async_node_t));
1241 }
1242 avl_destroy(t);
34dc7c2f 1243
572e2857
BB
1244 kmem_free(itxs, sizeof (itxs_t));
1245}
34dc7c2f 1246
572e2857
BB
1247static int
1248zil_aitx_compare(const void *x1, const void *x2)
1249{
1250 const uint64_t o1 = ((itx_async_node_t *)x1)->ia_foid;
1251 const uint64_t o2 = ((itx_async_node_t *)x2)->ia_foid;
1252
ee36c709 1253 return (AVL_CMP(o1, o2));
34dc7c2f
BB
1254}
1255
1256/*
572e2857 1257 * Remove all async itx with the given oid.
34dc7c2f
BB
1258 */
1259static void
572e2857 1260zil_remove_async(zilog_t *zilog, uint64_t oid)
34dc7c2f 1261{
572e2857
BB
1262 uint64_t otxg, txg;
1263 itx_async_node_t *ian;
1264 avl_tree_t *t;
1265 avl_index_t where;
34dc7c2f
BB
1266 list_t clean_list;
1267 itx_t *itx;
1268
572e2857 1269 ASSERT(oid != 0);
34dc7c2f
BB
1270 list_create(&clean_list, sizeof (itx_t), offsetof(itx_t, itx_node));
1271
572e2857
BB
1272 if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */
1273 otxg = ZILTEST_TXG;
1274 else
1275 otxg = spa_last_synced_txg(zilog->zl_spa) + 1;
34dc7c2f 1276
572e2857
BB
1277 for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) {
1278 itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK];
1279
1280 mutex_enter(&itxg->itxg_lock);
1281 if (itxg->itxg_txg != txg) {
1282 mutex_exit(&itxg->itxg_lock);
1283 continue;
1284 }
34dc7c2f 1285
572e2857
BB
1286 /*
1287 * Locate the object node and append its list.
1288 */
1289 t = &itxg->itxg_itxs->i_async_tree;
1290 ian = avl_find(t, &oid, &where);
1291 if (ian != NULL)
1292 list_move_tail(&clean_list, &ian->ia_list);
1293 mutex_exit(&itxg->itxg_lock);
1294 }
34dc7c2f 1295 while ((itx = list_head(&clean_list)) != NULL) {
119a394a
ED
1296 if (itx->itx_callback != NULL)
1297 itx->itx_callback(itx->itx_callback_data);
34dc7c2f 1298 list_remove(&clean_list, itx);
19ea3d25 1299 zil_itx_destroy(itx);
34dc7c2f
BB
1300 }
1301 list_destroy(&clean_list);
1302}
1303
572e2857
BB
1304void
1305zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx)
1306{
1307 uint64_t txg;
1308 itxg_t *itxg;
1309 itxs_t *itxs, *clean = NULL;
1310
1311 /*
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.
1317 */
1318 if ((itx->itx_lr.lrc_txtype & ~TX_CI) == TX_REMOVE)
1319 zil_remove_async(zilog, itx->itx_oid);
1320
1321 /*
1322 * Ensure the data of a renamed file is committed before the rename.
1323 */
1324 if ((itx->itx_lr.lrc_txtype & ~TX_CI) == TX_RENAME)
1325 zil_async_to_sync(zilog, itx->itx_oid);
1326
29809a6c 1327 if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX)
572e2857
BB
1328 txg = ZILTEST_TXG;
1329 else
1330 txg = dmu_tx_get_txg(tx);
1331
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) {
1336 if (itxs != NULL) {
1337 /*
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.
1341 */
1342 atomic_add_64(&zilog->zl_itx_list_sz, -itxg->itxg_sod);
1343 itxg->itxg_sod = 0;
1344 clean = itxg->itxg_itxs;
1345 }
1346 ASSERT(itxg->itxg_sod == 0);
1347 itxg->itxg_txg = txg;
d1d7e268 1348 itxs = itxg->itxg_itxs = kmem_zalloc(sizeof (itxs_t),
79c76d5b 1349 KM_SLEEP);
572e2857
BB
1350
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));
1356 }
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;
1361 } else {
1362 avl_tree_t *t = &itxs->i_async_tree;
50c957f7
NB
1363 uint64_t foid =
1364 LR_FOID_GET_OBJ(((lr_ooo_t *)&itx->itx_lr)->lr_foid);
572e2857
BB
1365 itx_async_node_t *ian;
1366 avl_index_t where;
1367
1368 ian = avl_find(t, &foid, &where);
1369 if (ian == NULL) {
d1d7e268 1370 ian = kmem_alloc(sizeof (itx_async_node_t),
79c76d5b 1371 KM_SLEEP);
572e2857
BB
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);
1376 }
1377 list_insert_tail(&ian->ia_list, itx);
1378 }
1379
1380 itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx);
29809a6c 1381 zilog_dirty(zilog, txg);
572e2857
BB
1382 mutex_exit(&itxg->itxg_lock);
1383
1384 /* Release the old itxs now we've dropped the lock */
1385 if (clean != NULL)
1386 zil_itxg_clean(clean);
1387}
1388
34dc7c2f
BB
1389/*
1390 * If there are any in-memory intent log transactions which have now been
29809a6c
MA
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
1394 * by zil_commit().
34dc7c2f
BB
1395 */
1396void
572e2857 1397zil_clean(zilog_t *zilog, uint64_t synced_txg)
34dc7c2f 1398{
572e2857
BB
1399 itxg_t *itxg = &zilog->zl_itxg[synced_txg & TXG_MASK];
1400 itxs_t *clean_me;
34dc7c2f 1401
572e2857
BB
1402 mutex_enter(&itxg->itxg_lock);
1403 if (itxg->itxg_itxs == NULL || itxg->itxg_txg == ZILTEST_TXG) {
1404 mutex_exit(&itxg->itxg_lock);
1405 return;
1406 }
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);
1411 itxg->itxg_sod = 0;
1412 clean_me = itxg->itxg_itxs;
1413 itxg->itxg_itxs = NULL;
1414 itxg->itxg_txg = 0;
1415 mutex_exit(&itxg->itxg_lock);
1416 /*
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.
1421 */
1422 if (taskq_dispatch(zilog->zl_clean_taskq,
b8864a23 1423 (void (*)(void *))zil_itxg_clean, clean_me, TQ_NOSLEEP) == 0)
572e2857
BB
1424 zil_itxg_clean(clean_me);
1425}
1426
1427/*
1428 * Get the list of itxs to commit into zl_itx_commit_list.
1429 */
1430static void
1431zil_get_commit_list(zilog_t *zilog)
1432{
1433 uint64_t otxg, txg;
1434 list_t *commit_list = &zilog->zl_itx_commit_list;
1435 uint64_t push_sod = 0;
1436
1437 if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */
1438 otxg = ZILTEST_TXG;
1439 else
1440 otxg = spa_last_synced_txg(zilog->zl_spa) + 1;
1441
1442 for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) {
1443 itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK];
1444
1445 mutex_enter(&itxg->itxg_lock);
1446 if (itxg->itxg_txg != txg) {
1447 mutex_exit(&itxg->itxg_lock);
1448 continue;
1449 }
1450
1451 list_move_tail(commit_list, &itxg->itxg_itxs->i_sync_list);
1452 push_sod += itxg->itxg_sod;
1453 itxg->itxg_sod = 0;
1454
1455 mutex_exit(&itxg->itxg_lock);
1456 }
1457 atomic_add_64(&zilog->zl_itx_list_sz, -push_sod);
1458}
1459
1460/*
1461 * Move the async itxs for a specified object to commit into sync lists.
1462 */
1463static void
1464zil_async_to_sync(zilog_t *zilog, uint64_t foid)
1465{
1466 uint64_t otxg, txg;
1467 itx_async_node_t *ian;
1468 avl_tree_t *t;
1469 avl_index_t where;
1470
1471 if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */
1472 otxg = ZILTEST_TXG;
1473 else
1474 otxg = spa_last_synced_txg(zilog->zl_spa) + 1;
1475
1476 for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) {
1477 itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK];
1478
1479 mutex_enter(&itxg->itxg_lock);
1480 if (itxg->itxg_txg != txg) {
1481 mutex_exit(&itxg->itxg_lock);
1482 continue;
1483 }
1484
1485 /*
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.
1490 */
1491 t = &itxg->itxg_itxs->i_async_tree;
1492 if (foid != 0) {
1493 ian = avl_find(t, &foid, &where);
1494 if (ian != NULL) {
1495 list_move_tail(&itxg->itxg_itxs->i_sync_list,
1496 &ian->ia_list);
1497 }
1498 } else {
1499 void *cookie = NULL;
1500
1501 while ((ian = avl_destroy_nodes(t, &cookie)) != NULL) {
1502 list_move_tail(&itxg->itxg_itxs->i_sync_list,
1503 &ian->ia_list);
1504 list_destroy(&ian->ia_list);
1505 kmem_free(ian, sizeof (itx_async_node_t));
1506 }
1507 }
1508 mutex_exit(&itxg->itxg_lock);
34dc7c2f 1509 }
34dc7c2f
BB
1510}
1511
b128c09f 1512static void
572e2857 1513zil_commit_writer(zilog_t *zilog)
34dc7c2f
BB
1514{
1515 uint64_t txg;
572e2857 1516 itx_t *itx;
34dc7c2f 1517 lwb_t *lwb;
572e2857 1518 spa_t *spa = zilog->zl_spa;
428870ff 1519 int error = 0;
34dc7c2f 1520
b128c09f 1521 ASSERT(zilog->zl_root_zio == NULL);
572e2857
BB
1522
1523 mutex_exit(&zilog->zl_lock);
1524
1525 zil_get_commit_list(zilog);
1526
1527 /*
1528 * Return if there's nothing to commit before we dirty the fs by
1529 * calling zil_create().
1530 */
1531 if (list_head(&zilog->zl_itx_commit_list) == NULL) {
1532 mutex_enter(&zilog->zl_lock);
1533 return;
1534 }
34dc7c2f
BB
1535
1536 if (zilog->zl_suspend) {
1537 lwb = NULL;
1538 } else {
1539 lwb = list_tail(&zilog->zl_lwb_list);
572e2857 1540 if (lwb == NULL)
428870ff 1541 lwb = zil_create(zilog);
34dc7c2f
BB
1542 }
1543
34dc7c2f 1544 DTRACE_PROBE1(zil__cw1, zilog_t *, zilog);
119a394a 1545 for (itx = list_head(&zilog->zl_itx_commit_list); itx != NULL;
d1d7e268 1546 itx = list_next(&zilog->zl_itx_commit_list, itx)) {
34dc7c2f
BB
1547 txg = itx->itx_lr.lrc_txg;
1548 ASSERT(txg);
1549
572e2857 1550 if (txg > spa_last_synced_txg(spa) || txg > spa_freeze_txg(spa))
34dc7c2f 1551 lwb = zil_lwb_commit(zilog, itx, lwb);
34dc7c2f
BB
1552 }
1553 DTRACE_PROBE1(zil__cw2, zilog_t *, zilog);
34dc7c2f
BB
1554
1555 /* write the last block out */
1556 if (lwb != NULL && lwb->lwb_zio != NULL)
1557 lwb = zil_lwb_write_start(zilog, lwb);
1558
34dc7c2f
BB
1559 zilog->zl_cur_used = 0;
1560
1561 /*
1562 * Wait if necessary for the log blocks to be on stable storage.
1563 */
1564 if (zilog->zl_root_zio) {
428870ff 1565 error = zio_wait(zilog->zl_root_zio);
b128c09f 1566 zilog->zl_root_zio = NULL;
34dc7c2f
BB
1567 zil_flush_vdevs(zilog);
1568 }
1569
428870ff 1570 if (error || lwb == NULL)
34dc7c2f 1571 txg_wait_synced(zilog->zl_dmu_pool, 0);
34dc7c2f 1572
119a394a
ED
1573 while ((itx = list_head(&zilog->zl_itx_commit_list))) {
1574 txg = itx->itx_lr.lrc_txg;
1575 ASSERT(txg);
1576
1577 if (itx->itx_callback != NULL)
1578 itx->itx_callback(itx->itx_callback_data);
1579 list_remove(&zilog->zl_itx_commit_list, itx);
19ea3d25 1580 zil_itx_destroy(itx);
119a394a
ED
1581 }
1582
34dc7c2f 1583 mutex_enter(&zilog->zl_lock);
428870ff
BB
1584
1585 /*
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.
1589 */
1590 if (error == 0 && lwb != NULL)
1591 zilog->zl_commit_lr_seq = zilog->zl_lr_seq;
34dc7c2f
BB
1592}
1593
1594/*
572e2857 1595 * Commit zfs transactions to stable storage.
34dc7c2f 1596 * If foid is 0 push out all transactions, otherwise push only those
572e2857
BB
1597 * for that object or might reference that object.
1598 *
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.
1603 *
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.
1609 *
1610 * Only 2 condition variables are needed and there's no transition
1611 * between the two cvs needed. They just flip-flop between qthreads
1612 * and cthreads.
1613 *
1614 * Using this scheme we can efficiently wakeup up only those threads
1615 * that have been committed.
34dc7c2f
BB
1616 */
1617void
572e2857 1618zil_commit(zilog_t *zilog, uint64_t foid)
34dc7c2f 1619{
572e2857 1620 uint64_t mybatch;
34dc7c2f 1621
572e2857
BB
1622 if (zilog->zl_sync == ZFS_SYNC_DISABLED)
1623 return;
34dc7c2f 1624
b6ad9671
ED
1625 ZIL_STAT_BUMP(zil_commit_count);
1626
572e2857
BB
1627 /* move the async itxs for the foid to the sync queues */
1628 zil_async_to_sync(zilog, foid);
34dc7c2f 1629
572e2857
BB
1630 mutex_enter(&zilog->zl_lock);
1631 mybatch = zilog->zl_next_batch;
34dc7c2f 1632 while (zilog->zl_writer) {
572e2857
BB
1633 cv_wait(&zilog->zl_cv_batch[mybatch & 1], &zilog->zl_lock);
1634 if (mybatch <= zilog->zl_com_batch) {
34dc7c2f
BB
1635 mutex_exit(&zilog->zl_lock);
1636 return;
1637 }
1638 }
428870ff 1639
572e2857
BB
1640 zilog->zl_next_batch++;
1641 zilog->zl_writer = B_TRUE;
b6ad9671 1642 ZIL_STAT_BUMP(zil_commit_writer_count);
572e2857
BB
1643 zil_commit_writer(zilog);
1644 zilog->zl_com_batch = mybatch;
1645 zilog->zl_writer = B_FALSE;
428870ff 1646
572e2857
BB
1647 /* wake up one thread to become the next writer */
1648 cv_signal(&zilog->zl_cv_batch[(mybatch+1) & 1]);
428870ff 1649
572e2857
BB
1650 /* wake up all threads waiting for this batch to be committed */
1651 cv_broadcast(&zilog->zl_cv_batch[mybatch & 1]);
8c0712fd
BB
1652
1653 mutex_exit(&zilog->zl_lock);
428870ff
BB
1654}
1655
34dc7c2f
BB
1656/*
1657 * Called in syncing context to free committed log blocks and update log header.
1658 */
1659void
1660zil_sync(zilog_t *zilog, dmu_tx_t *tx)
1661{
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;
428870ff 1665 uint64_t *replayed_seq = &zilog->zl_replayed_seq[txg & TXG_MASK];
34dc7c2f
BB
1666 lwb_t *lwb;
1667
9babb374
BB
1668 /*
1669 * We don't zero out zl_destroy_txg, so make sure we don't try
1670 * to destroy it twice.
1671 */
1672 if (spa_sync_pass(spa) != 1)
1673 return;
1674
34dc7c2f
BB
1675 mutex_enter(&zilog->zl_lock);
1676
1677 ASSERT(zilog->zl_stop_sync == 0);
1678
428870ff
BB
1679 if (*replayed_seq != 0) {
1680 ASSERT(zh->zh_replay_seq < *replayed_seq);
1681 zh->zh_replay_seq = *replayed_seq;
1682 *replayed_seq = 0;
1683 }
34dc7c2f
BB
1684
1685 if (zilog->zl_destroy_txg == txg) {
1686 blkptr_t blk = zh->zh_log;
1687
1688 ASSERT(list_head(&zilog->zl_lwb_list) == NULL);
34dc7c2f
BB
1689
1690 bzero(zh, sizeof (zil_header_t));
fb5f0bc8 1691 bzero(zilog->zl_replayed_seq, sizeof (zilog->zl_replayed_seq));
34dc7c2f
BB
1692
1693 if (zilog->zl_keep_first) {
1694 /*
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.
1701 */
1702 zil_init_log_chain(zilog, &blk);
1703 zh->zh_log = blk;
1704 }
1705 }
1706
9babb374 1707 while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
34dc7c2f
BB
1708 zh->zh_log = lwb->lwb_blk;
1709 if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg)
1710 break;
920dd524
ED
1711
1712 ASSERT(lwb->lwb_zio == NULL);
1713
34dc7c2f 1714 list_remove(&zilog->zl_lwb_list, lwb);
428870ff 1715 zio_free_zil(spa, txg, &lwb->lwb_blk);
34dc7c2f
BB
1716 kmem_cache_free(zil_lwb_cache, lwb);
1717
1718 /*
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.
1723 */
1724 if (list_head(&zilog->zl_lwb_list) == NULL)
1725 BP_ZERO(&zh->zh_log);
1726 }
920dd524
ED
1727
1728 /*
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.
1732 */
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;
1737 }
1738 }
1739
34dc7c2f
BB
1740 mutex_exit(&zilog->zl_lock);
1741}
1742
1743void
1744zil_init(void)
1745{
1746 zil_lwb_cache = kmem_cache_create("zil_lwb_cache",
1747 sizeof (struct lwb), 0, NULL, NULL, NULL, NULL, NULL, 0);
b6ad9671
ED
1748
1749 zil_ksp = kstat_create("zfs", 0, "zil", "misc",
d1d7e268 1750 KSTAT_TYPE_NAMED, sizeof (zil_stats) / sizeof (kstat_named_t),
b6ad9671
ED
1751 KSTAT_FLAG_VIRTUAL);
1752
1753 if (zil_ksp != NULL) {
1754 zil_ksp->ks_data = &zil_stats;
1755 kstat_install(zil_ksp);
1756 }
34dc7c2f
BB
1757}
1758
1759void
1760zil_fini(void)
1761{
1762 kmem_cache_destroy(zil_lwb_cache);
b6ad9671
ED
1763
1764 if (zil_ksp != NULL) {
1765 kstat_delete(zil_ksp);
1766 zil_ksp = NULL;
1767 }
34dc7c2f
BB
1768}
1769
428870ff
BB
1770void
1771zil_set_sync(zilog_t *zilog, uint64_t sync)
1772{
1773 zilog->zl_sync = sync;
1774}
1775
1776void
1777zil_set_logbias(zilog_t *zilog, uint64_t logbias)
1778{
1779 zilog->zl_logbias = logbias;
1780}
1781
34dc7c2f
BB
1782zilog_t *
1783zil_alloc(objset_t *os, zil_header_t *zh_phys)
1784{
1785 zilog_t *zilog;
d6320ddb 1786 int i;
34dc7c2f 1787
79c76d5b 1788 zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP);
34dc7c2f
BB
1789
1790 zilog->zl_header = zh_phys;
1791 zilog->zl_os = os;
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;
428870ff
BB
1795 zilog->zl_logbias = dmu_objset_logbias(os);
1796 zilog->zl_sync = dmu_objset_syncprop(os);
572e2857 1797 zilog->zl_next_batch = 1;
34dc7c2f
BB
1798
1799 mutex_init(&zilog->zl_lock, NULL, MUTEX_DEFAULT, NULL);
1800
d6320ddb 1801 for (i = 0; i < TXG_SIZE; i++) {
572e2857
BB
1802 mutex_init(&zilog->zl_itxg[i].itxg_lock, NULL,
1803 MUTEX_DEFAULT, NULL);
1804 }
34dc7c2f
BB
1805
1806 list_create(&zilog->zl_lwb_list, sizeof (lwb_t),
1807 offsetof(lwb_t, lwb_node));
1808
572e2857
BB
1809 list_create(&zilog->zl_itx_commit_list, sizeof (itx_t),
1810 offsetof(itx_t, itx_node));
1811
34dc7c2f
BB
1812 mutex_init(&zilog->zl_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
1813
1814 avl_create(&zilog->zl_vdev_tree, zil_vdev_compare,
1815 sizeof (zil_vdev_node_t), offsetof(zil_vdev_node_t, zv_node));
1816
1817 cv_init(&zilog->zl_cv_writer, NULL, CV_DEFAULT, NULL);
1818 cv_init(&zilog->zl_cv_suspend, NULL, CV_DEFAULT, NULL);
572e2857
BB
1819 cv_init(&zilog->zl_cv_batch[0], NULL, CV_DEFAULT, NULL);
1820 cv_init(&zilog->zl_cv_batch[1], NULL, CV_DEFAULT, NULL);
34dc7c2f
BB
1821
1822 return (zilog);
1823}
1824
1825void
1826zil_free(zilog_t *zilog)
1827{
d6320ddb 1828 int i;
34dc7c2f
BB
1829
1830 zilog->zl_stop_sync = 1;
1831
13fe0198
MA
1832 ASSERT0(zilog->zl_suspend);
1833 ASSERT0(zilog->zl_suspending);
1834
3e31d2b0 1835 ASSERT(list_is_empty(&zilog->zl_lwb_list));
34dc7c2f
BB
1836 list_destroy(&zilog->zl_lwb_list);
1837
1838 avl_destroy(&zilog->zl_vdev_tree);
1839 mutex_destroy(&zilog->zl_vdev_lock);
1840
572e2857
BB
1841 ASSERT(list_is_empty(&zilog->zl_itx_commit_list));
1842 list_destroy(&zilog->zl_itx_commit_list);
1843
d6320ddb 1844 for (i = 0; i < TXG_SIZE; i++) {
572e2857
BB
1845 /*
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.
1849 *
1850 * Also free up the ziltest itxs.
1851 */
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);
1855 }
1856
34dc7c2f
BB
1857 mutex_destroy(&zilog->zl_lock);
1858
1859 cv_destroy(&zilog->zl_cv_writer);
1860 cv_destroy(&zilog->zl_cv_suspend);
572e2857
BB
1861 cv_destroy(&zilog->zl_cv_batch[0]);
1862 cv_destroy(&zilog->zl_cv_batch[1]);
34dc7c2f
BB
1863
1864 kmem_free(zilog, sizeof (zilog_t));
1865}
1866
34dc7c2f
BB
1867/*
1868 * Open an intent log.
1869 */
1870zilog_t *
1871zil_open(objset_t *os, zil_get_data_t *get_data)
1872{
1873 zilog_t *zilog = dmu_objset_zil(os);
1874
3e31d2b0
ES
1875 ASSERT(zilog->zl_clean_taskq == NULL);
1876 ASSERT(zilog->zl_get_data == NULL);
1877 ASSERT(list_is_empty(&zilog->zl_lwb_list));
1878
34dc7c2f 1879 zilog->zl_get_data = get_data;
1229323d 1880 zilog->zl_clean_taskq = taskq_create("zil_clean", 1, defclsyspri,
34dc7c2f
BB
1881 2, 2, TASKQ_PREPOPULATE);
1882
1883 return (zilog);
1884}
1885
1886/*
1887 * Close an intent log.
1888 */
1889void
1890zil_close(zilog_t *zilog)
1891{
3e31d2b0 1892 lwb_t *lwb;
572e2857
BB
1893 uint64_t txg = 0;
1894
1895 zil_commit(zilog, 0); /* commit all itx */
1896
34dc7c2f 1897 /*
572e2857
BB
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.
34dc7c2f 1902 */
572e2857 1903 mutex_enter(&zilog->zl_lock);
3e31d2b0
ES
1904 lwb = list_tail(&zilog->zl_lwb_list);
1905 if (lwb != NULL)
1906 txg = lwb->lwb_max_txg;
572e2857
BB
1907 mutex_exit(&zilog->zl_lock);
1908 if (txg)
34dc7c2f 1909 txg_wait_synced(zilog->zl_dmu_pool, txg);
50c957f7
NB
1910 if (txg < spa_freeze_txg(zilog->zl_spa))
1911 ASSERT(!zilog_is_dirty(zilog));
34dc7c2f
BB
1912
1913 taskq_destroy(zilog->zl_clean_taskq);
1914 zilog->zl_clean_taskq = NULL;
1915 zilog->zl_get_data = NULL;
3e31d2b0
ES
1916
1917 /*
1918 * We should have only one LWB left on the list; remove it now.
1919 */
1920 mutex_enter(&zilog->zl_lock);
1921 lwb = list_head(&zilog->zl_lwb_list);
1922 if (lwb != NULL) {
1923 ASSERT(lwb == list_tail(&zilog->zl_lwb_list));
920dd524
ED
1924 ASSERT(lwb->lwb_zio == NULL);
1925 if (lwb->lwb_fastwrite)
1926 metaslab_fastwrite_unmark(zilog->zl_spa, &lwb->lwb_blk);
3e31d2b0
ES
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);
1930 }
1931 mutex_exit(&zilog->zl_lock);
34dc7c2f
BB
1932}
1933
13fe0198
MA
1934static char *suspend_tag = "zil suspending";
1935
34dc7c2f
BB
1936/*
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.
13fe0198
MA
1939 * On old version pools, we suspend the log briefly when taking a
1940 * snapshot so that it will have an empty intent log.
1941 *
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.
1948 *
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
1952 * very little gain.
1953 *
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().
34dc7c2f
BB
1957 */
1958int
13fe0198 1959zil_suspend(const char *osname, void **cookiep)
34dc7c2f 1960{
13fe0198
MA
1961 objset_t *os;
1962 zilog_t *zilog;
1963 const zil_header_t *zh;
1964 int error;
1965
1966 error = dmu_objset_hold(osname, suspend_tag, &os);
1967 if (error != 0)
1968 return (error);
1969 zilog = dmu_objset_zil(os);
34dc7c2f
BB
1970
1971 mutex_enter(&zilog->zl_lock);
13fe0198
MA
1972 zh = zilog->zl_header;
1973
9babb374 1974 if (zh->zh_flags & ZIL_REPLAY_NEEDED) { /* unplayed log */
34dc7c2f 1975 mutex_exit(&zilog->zl_lock);
13fe0198 1976 dmu_objset_rele(os, suspend_tag);
2e528b49 1977 return (SET_ERROR(EBUSY));
34dc7c2f 1978 }
13fe0198
MA
1979
1980 /*
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.
1985 */
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);
1990 return (0);
1991 }
1992
1993 dsl_dataset_long_hold(dmu_objset_ds(os), suspend_tag);
1994 dsl_pool_rele(dmu_objset_pool(os), suspend_tag);
1995
1996 zilog->zl_suspend++;
1997
1998 if (zilog->zl_suspend > 1) {
34dc7c2f 1999 /*
13fe0198 2000 * Someone else is already suspending it.
34dc7c2f
BB
2001 * Just wait for them to finish.
2002 */
13fe0198 2003
34dc7c2f
BB
2004 while (zilog->zl_suspending)
2005 cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock);
34dc7c2f 2006 mutex_exit(&zilog->zl_lock);
13fe0198
MA
2007
2008 if (cookiep == NULL)
2009 zil_resume(os);
2010 else
2011 *cookiep = os;
2012 return (0);
2013 }
2014
2015 /*
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
2018 * to clean up.
2019 */
2020 if (BP_IS_HOLE(&zh->zh_log)) {
2021 ASSERT(cookiep != NULL); /* fast path already handled */
2022
2023 *cookiep = os;
2024 mutex_exit(&zilog->zl_lock);
34dc7c2f
BB
2025 return (0);
2026 }
13fe0198 2027
34dc7c2f
BB
2028 zilog->zl_suspending = B_TRUE;
2029 mutex_exit(&zilog->zl_lock);
2030
572e2857 2031 zil_commit(zilog, 0);
34dc7c2f
BB
2032
2033 zil_destroy(zilog, B_FALSE);
2034
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);
2039
13fe0198
MA
2040 if (cookiep == NULL)
2041 zil_resume(os);
2042 else
2043 *cookiep = os;
34dc7c2f
BB
2044 return (0);
2045}
2046
2047void
13fe0198 2048zil_resume(void *cookie)
34dc7c2f 2049{
13fe0198
MA
2050 objset_t *os = cookie;
2051 zilog_t *zilog = dmu_objset_zil(os);
2052
34dc7c2f
BB
2053 mutex_enter(&zilog->zl_lock);
2054 ASSERT(zilog->zl_suspend != 0);
2055 zilog->zl_suspend--;
2056 mutex_exit(&zilog->zl_lock);
13fe0198
MA
2057 dsl_dataset_long_rele(dmu_objset_ds(os), suspend_tag);
2058 dsl_dataset_rele(dmu_objset_ds(os), suspend_tag);
34dc7c2f
BB
2059}
2060
2061typedef struct zil_replay_arg {
b01615d5 2062 zil_replay_func_t *zr_replay;
34dc7c2f 2063 void *zr_arg;
34dc7c2f 2064 boolean_t zr_byteswap;
428870ff 2065 char *zr_lr;
34dc7c2f
BB
2066} zil_replay_arg_t;
2067
428870ff
BB
2068static int
2069zil_replay_error(zilog_t *zilog, lr_t *lr, int error)
2070{
eca7b760 2071 char name[ZFS_MAX_DATASET_NAME_LEN];
428870ff
BB
2072
2073 zilog->zl_replaying_seq--; /* didn't actually replay this one */
2074
2075 dmu_objset_name(zilog->zl_os, name);
2076
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" : "");
2082
2083 return (error);
2084}
2085
2086static int
34dc7c2f
BB
2087zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg)
2088{
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;
428870ff 2093 int error = 0;
34dc7c2f 2094
428870ff 2095 zilog->zl_replaying_seq = lr->lrc_seq;
34dc7c2f
BB
2096
2097 if (lr->lrc_seq <= zh->zh_replay_seq) /* already replayed */
428870ff
BB
2098 return (0);
2099
2100 if (lr->lrc_txg < claim_txg) /* already committed */
2101 return (0);
34dc7c2f
BB
2102
2103 /* Strip case-insensitive bit, still present in log record */
2104 txtype &= ~TX_CI;
2105
428870ff
BB
2106 if (txtype == 0 || txtype >= TX_MAX_TYPE)
2107 return (zil_replay_error(zilog, lr, EINVAL));
2108
2109 /*
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.
2112 */
2113 if (TX_OOO(txtype)) {
2114 error = dmu_object_info(zilog->zl_os,
50c957f7 2115 LR_FOID_GET_OBJ(((lr_ooo_t *)lr)->lr_foid), NULL);
428870ff
BB
2116 if (error == ENOENT || error == EEXIST)
2117 return (0);
fb5f0bc8
BB
2118 }
2119
34dc7c2f
BB
2120 /*
2121 * Make a copy of the data so we can revise and extend it.
2122 */
428870ff
BB
2123 bcopy(lr, zr->zr_lr, reclen);
2124
2125 /*
2126 * If this is a TX_WRITE with a blkptr, suck in the data.
2127 */
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);
13fe0198 2131 if (error != 0)
428870ff
BB
2132 return (zil_replay_error(zilog, lr, error));
2133 }
34dc7c2f
BB
2134
2135 /*
2136 * The log block containing this lr may have been byteswapped
2137 * so that we can easily examine common fields like lrc_txtype.
428870ff 2138 * However, the log is a mix of different record types, and only the
34dc7c2f
BB
2139 * replay vectors know how to byteswap their records. Therefore, if
2140 * the lr was byteswapped, undo it before invoking the replay vector.
2141 */
2142 if (zr->zr_byteswap)
428870ff 2143 byteswap_uint64_array(zr->zr_lr, reclen);
34dc7c2f
BB
2144
2145 /*
2146 * We must now do two things atomically: replay this log record,
fb5f0bc8
BB
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.
34dc7c2f 2150 */
428870ff 2151 error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lr, zr->zr_byteswap);
13fe0198 2152 if (error != 0) {
34dc7c2f
BB
2153 /*
2154 * The DMU's dnode layer doesn't see removes until the txg
2155 * commits, so a subsequent claim can spuriously fail with
fb5f0bc8 2156 * EEXIST. So if we receive any error we try syncing out
428870ff
BB
2157 * any removes then retry the transaction. Note that we
2158 * specify B_FALSE for byteswap now, so we don't do it twice.
34dc7c2f 2159 */
428870ff
BB
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);
13fe0198 2162 if (error != 0)
428870ff 2163 return (zil_replay_error(zilog, lr, error));
34dc7c2f 2164 }
428870ff 2165 return (0);
34dc7c2f
BB
2166}
2167
2168/* ARGSUSED */
428870ff 2169static int
34dc7c2f
BB
2170zil_incr_blks(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
2171{
2172 zilog->zl_replay_blks++;
428870ff
BB
2173
2174 return (0);
34dc7c2f
BB
2175}
2176
2177/*
2178 * If this dataset has a non-empty intent log, replay it and destroy it.
2179 */
2180void
b01615d5 2181zil_replay(objset_t *os, void *arg, zil_replay_func_t replay_func[TX_MAX_TYPE])
34dc7c2f
BB
2182{
2183 zilog_t *zilog = dmu_objset_zil(os);
2184 const zil_header_t *zh = zilog->zl_header;
2185 zil_replay_arg_t zr;
2186
9babb374 2187 if ((zh->zh_flags & ZIL_REPLAY_NEEDED) == 0) {
34dc7c2f
BB
2188 zil_destroy(zilog, B_TRUE);
2189 return;
2190 }
2191
34dc7c2f
BB
2192 zr.zr_replay = replay_func;
2193 zr.zr_arg = arg;
34dc7c2f 2194 zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log);
79c76d5b 2195 zr.zr_lr = vmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP);
34dc7c2f
BB
2196
2197 /*
2198 * Wait for in-progress removes to sync before starting replay.
2199 */
2200 txg_wait_synced(zilog->zl_dmu_pool, 0);
2201
fb5f0bc8 2202 zilog->zl_replay = B_TRUE;
428870ff 2203 zilog->zl_replay_time = ddi_get_lbolt();
34dc7c2f
BB
2204 ASSERT(zilog->zl_replay_blks == 0);
2205 (void) zil_parse(zilog, zil_incr_blks, zil_replay_log_record, &zr,
2206 zh->zh_claim_txg);
00b46022 2207 vmem_free(zr.zr_lr, 2 * SPA_MAXBLOCKSIZE);
34dc7c2f
BB
2208
2209 zil_destroy(zilog, B_FALSE);
2210 txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
fb5f0bc8 2211 zilog->zl_replay = B_FALSE;
34dc7c2f
BB
2212}
2213
428870ff
BB
2214boolean_t
2215zil_replaying(zilog_t *zilog, dmu_tx_t *tx)
34dc7c2f 2216{
428870ff
BB
2217 if (zilog->zl_sync == ZFS_SYNC_DISABLED)
2218 return (B_TRUE);
34dc7c2f 2219
428870ff
BB
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;
2224 return (B_TRUE);
34dc7c2f
BB
2225 }
2226
428870ff 2227 return (B_FALSE);
34dc7c2f 2228}
9babb374
BB
2229
2230/* ARGSUSED */
2231int
428870ff 2232zil_vdev_offline(const char *osname, void *arg)
9babb374 2233{
9babb374
BB
2234 int error;
2235
13fe0198
MA
2236 error = zil_suspend(osname, NULL);
2237 if (error != 0)
2e528b49 2238 return (SET_ERROR(EEXIST));
13fe0198 2239 return (0);
9babb374 2240}
c409e464
BB
2241
2242#if defined(_KERNEL) && defined(HAVE_SPL)
0f699108
AZ
2243EXPORT_SYMBOL(zil_alloc);
2244EXPORT_SYMBOL(zil_free);
2245EXPORT_SYMBOL(zil_open);
2246EXPORT_SYMBOL(zil_close);
2247EXPORT_SYMBOL(zil_replay);
2248EXPORT_SYMBOL(zil_replaying);
2249EXPORT_SYMBOL(zil_destroy);
2250EXPORT_SYMBOL(zil_destroy_sync);
2251EXPORT_SYMBOL(zil_itx_create);
2252EXPORT_SYMBOL(zil_itx_destroy);
2253EXPORT_SYMBOL(zil_itx_assign);
2254EXPORT_SYMBOL(zil_commit);
2255EXPORT_SYMBOL(zil_vdev_offline);
2256EXPORT_SYMBOL(zil_claim);
2257EXPORT_SYMBOL(zil_check_log_chain);
2258EXPORT_SYMBOL(zil_sync);
2259EXPORT_SYMBOL(zil_clean);
2260EXPORT_SYMBOL(zil_suspend);
2261EXPORT_SYMBOL(zil_resume);
2262EXPORT_SYMBOL(zil_add_block);
2263EXPORT_SYMBOL(zil_bp_tree_add);
2264EXPORT_SYMBOL(zil_set_sync);
2265EXPORT_SYMBOL(zil_set_logbias);
2266
c409e464
BB
2267module_param(zil_replay_disable, int, 0644);
2268MODULE_PARM_DESC(zil_replay_disable, "Disable intent logging replay");
2269
2270module_param(zfs_nocacheflush, int, 0644);
2271MODULE_PARM_DESC(zfs_nocacheflush, "Disable cache flushes");
ee191e80
ED
2272
2273module_param(zil_slog_limit, ulong, 0644);
2274MODULE_PARM_DESC(zil_slog_limit, "Max commit bytes to separate log device");
c409e464 2275#endif