]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/zil.c
Illumos 4757, 4913
[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.
2e528b49 23 * Copyright (c) 2013 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>
34dc7c2f
BB
42
43/*
44 * The zfs intent log (ZIL) saves transaction records of system calls
45 * that change the file system in memory with enough information
46 * to be able to replay them. These are stored in memory until
47 * either the DMU transaction group (txg) commits them to the stable pool
48 * and they can be discarded, or they are flushed to the stable log
49 * (also in the pool) due to a fsync, O_DSYNC or other synchronous
50 * requirement. In the event of a panic or power fail then those log
51 * records (transactions) are replayed.
52 *
53 * There is one ZIL per file system. Its on-disk (pool) format consists
54 * of 3 parts:
55 *
56 * - ZIL header
57 * - ZIL blocks
58 * - ZIL records
59 *
60 * A log record holds a system call transaction. Log blocks can
61 * hold many log records and the blocks are chained together.
62 * Each ZIL block contains a block pointer (blkptr_t) to the next
63 * ZIL block in the chain. The ZIL header points to the first
64 * block in the chain. Note there is not a fixed place in the pool
65 * to hold blocks. They are dynamically allocated and freed as
66 * needed from the blocks available. Figure X shows the ZIL structure:
67 */
68
b6ad9671
ED
69/*
70 * See zil.h for more information about these fields.
71 */
72zil_stats_t zil_stats = {
d1d7e268
MK
73 { "zil_commit_count", KSTAT_DATA_UINT64 },
74 { "zil_commit_writer_count", KSTAT_DATA_UINT64 },
75 { "zil_itx_count", KSTAT_DATA_UINT64 },
76 { "zil_itx_indirect_count", KSTAT_DATA_UINT64 },
77 { "zil_itx_indirect_bytes", KSTAT_DATA_UINT64 },
78 { "zil_itx_copied_count", KSTAT_DATA_UINT64 },
79 { "zil_itx_copied_bytes", KSTAT_DATA_UINT64 },
80 { "zil_itx_needcopy_count", KSTAT_DATA_UINT64 },
81 { "zil_itx_needcopy_bytes", KSTAT_DATA_UINT64 },
82 { "zil_itx_metaslab_normal_count", KSTAT_DATA_UINT64 },
83 { "zil_itx_metaslab_normal_bytes", KSTAT_DATA_UINT64 },
84 { "zil_itx_metaslab_slog_count", KSTAT_DATA_UINT64 },
85 { "zil_itx_metaslab_slog_bytes", KSTAT_DATA_UINT64 },
b6ad9671
ED
86};
87
88static kstat_t *zil_ksp;
89
34dc7c2f 90/*
d3cc8b15 91 * Disable intent logging replay. This global ZIL switch affects all pools.
34dc7c2f 92 */
d3cc8b15 93int zil_replay_disable = 0;
34dc7c2f
BB
94
95/*
96 * Tunable parameter for debugging or performance analysis. Setting
97 * zfs_nocacheflush will cause corruption on power loss if a volatile
98 * out-of-order write cache is enabled.
99 */
c409e464 100int zfs_nocacheflush = 0;
34dc7c2f
BB
101
102static kmem_cache_t *zil_lwb_cache;
103
572e2857 104static void zil_async_to_sync(zilog_t *zilog, uint64_t foid);
428870ff
BB
105
106#define LWB_EMPTY(lwb) ((BP_GET_LSIZE(&lwb->lwb_blk) - \
107 sizeof (zil_chain_t)) == (lwb->lwb_sz - lwb->lwb_nused))
108
109
572e2857
BB
110/*
111 * ziltest is by and large an ugly hack, but very useful in
112 * checking replay without tedious work.
113 * When running ziltest we want to keep all itx's and so maintain
114 * a single list in the zl_itxg[] that uses a high txg: ZILTEST_TXG
115 * We subtract TXG_CONCURRENT_STATES to allow for common code.
116 */
117#define ZILTEST_TXG (UINT64_MAX - TXG_CONCURRENT_STATES)
118
34dc7c2f 119static int
428870ff 120zil_bp_compare(const void *x1, const void *x2)
34dc7c2f 121{
428870ff
BB
122 const dva_t *dva1 = &((zil_bp_node_t *)x1)->zn_dva;
123 const dva_t *dva2 = &((zil_bp_node_t *)x2)->zn_dva;
34dc7c2f
BB
124
125 if (DVA_GET_VDEV(dva1) < DVA_GET_VDEV(dva2))
126 return (-1);
127 if (DVA_GET_VDEV(dva1) > DVA_GET_VDEV(dva2))
128 return (1);
129
130 if (DVA_GET_OFFSET(dva1) < DVA_GET_OFFSET(dva2))
131 return (-1);
132 if (DVA_GET_OFFSET(dva1) > DVA_GET_OFFSET(dva2))
133 return (1);
134
135 return (0);
136}
137
138static void
428870ff 139zil_bp_tree_init(zilog_t *zilog)
34dc7c2f 140{
428870ff
BB
141 avl_create(&zilog->zl_bp_tree, zil_bp_compare,
142 sizeof (zil_bp_node_t), offsetof(zil_bp_node_t, zn_node));
34dc7c2f
BB
143}
144
145static void
428870ff 146zil_bp_tree_fini(zilog_t *zilog)
34dc7c2f 147{
428870ff
BB
148 avl_tree_t *t = &zilog->zl_bp_tree;
149 zil_bp_node_t *zn;
34dc7c2f
BB
150 void *cookie = NULL;
151
152 while ((zn = avl_destroy_nodes(t, &cookie)) != NULL)
428870ff 153 kmem_free(zn, sizeof (zil_bp_node_t));
34dc7c2f
BB
154
155 avl_destroy(t);
156}
157
428870ff
BB
158int
159zil_bp_tree_add(zilog_t *zilog, const blkptr_t *bp)
34dc7c2f 160{
428870ff 161 avl_tree_t *t = &zilog->zl_bp_tree;
9b67f605 162 const dva_t *dva;
428870ff 163 zil_bp_node_t *zn;
34dc7c2f
BB
164 avl_index_t where;
165
9b67f605
MA
166 if (BP_IS_EMBEDDED(bp))
167 return (0);
168
169 dva = BP_IDENTITY(bp);
170
34dc7c2f 171 if (avl_find(t, dva, &where) != NULL)
2e528b49 172 return (SET_ERROR(EEXIST));
34dc7c2f 173
b8d06fca 174 zn = kmem_alloc(sizeof (zil_bp_node_t), KM_PUSHPAGE);
34dc7c2f
BB
175 zn->zn_dva = *dva;
176 avl_insert(t, zn, where);
177
178 return (0);
179}
180
181static zil_header_t *
182zil_header_in_syncing_context(zilog_t *zilog)
183{
184 return ((zil_header_t *)zilog->zl_header);
185}
186
187static void
188zil_init_log_chain(zilog_t *zilog, blkptr_t *bp)
189{
190 zio_cksum_t *zc = &bp->blk_cksum;
191
192 zc->zc_word[ZIL_ZC_GUID_0] = spa_get_random(-1ULL);
193 zc->zc_word[ZIL_ZC_GUID_1] = spa_get_random(-1ULL);
194 zc->zc_word[ZIL_ZC_OBJSET] = dmu_objset_id(zilog->zl_os);
195 zc->zc_word[ZIL_ZC_SEQ] = 1ULL;
196}
197
198/*
428870ff 199 * Read a log block and make sure it's valid.
34dc7c2f
BB
200 */
201static int
428870ff
BB
202zil_read_log_block(zilog_t *zilog, const blkptr_t *bp, blkptr_t *nbp, void *dst,
203 char **end)
34dc7c2f 204{
428870ff 205 enum zio_flag zio_flags = ZIO_FLAG_CANFAIL;
34dc7c2f 206 uint32_t aflags = ARC_WAIT;
428870ff
BB
207 arc_buf_t *abuf = NULL;
208 zbookmark_t zb;
34dc7c2f
BB
209 int error;
210
428870ff
BB
211 if (zilog->zl_header->zh_claim_txg == 0)
212 zio_flags |= ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB;
34dc7c2f 213
428870ff
BB
214 if (!(zilog->zl_header->zh_flags & ZIL_CLAIM_LR_SEQ_VALID))
215 zio_flags |= ZIO_FLAG_SPECULATIVE;
34dc7c2f 216
428870ff
BB
217 SET_BOOKMARK(&zb, bp->blk_cksum.zc_word[ZIL_ZC_OBJSET],
218 ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, bp->blk_cksum.zc_word[ZIL_ZC_SEQ]);
219
294f6806 220 error = arc_read(NULL, zilog->zl_spa, bp, arc_getbuf_func, &abuf,
428870ff 221 ZIO_PRIORITY_SYNC_READ, zio_flags, &aflags, &zb);
34dc7c2f
BB
222
223 if (error == 0) {
34dc7c2f
BB
224 zio_cksum_t cksum = bp->blk_cksum;
225
226 /*
b128c09f
BB
227 * Validate the checksummed log block.
228 *
34dc7c2f
BB
229 * Sequence numbers should be... sequential. The checksum
230 * verifier for the next block should be bp's checksum plus 1.
b128c09f
BB
231 *
232 * Also check the log chain linkage and size used.
34dc7c2f
BB
233 */
234 cksum.zc_word[ZIL_ZC_SEQ]++;
235
428870ff
BB
236 if (BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_ZILOG2) {
237 zil_chain_t *zilc = abuf->b_data;
238 char *lr = (char *)(zilc + 1);
239 uint64_t len = zilc->zc_nused - sizeof (zil_chain_t);
34dc7c2f 240
428870ff
BB
241 if (bcmp(&cksum, &zilc->zc_next_blk.blk_cksum,
242 sizeof (cksum)) || BP_IS_HOLE(&zilc->zc_next_blk)) {
2e528b49 243 error = SET_ERROR(ECKSUM);
428870ff
BB
244 } else {
245 bcopy(lr, dst, len);
246 *end = (char *)dst + len;
247 *nbp = zilc->zc_next_blk;
248 }
249 } else {
250 char *lr = abuf->b_data;
251 uint64_t size = BP_GET_LSIZE(bp);
252 zil_chain_t *zilc = (zil_chain_t *)(lr + size) - 1;
253
254 if (bcmp(&cksum, &zilc->zc_next_blk.blk_cksum,
255 sizeof (cksum)) || BP_IS_HOLE(&zilc->zc_next_blk) ||
256 (zilc->zc_nused > (size - sizeof (*zilc)))) {
2e528b49 257 error = SET_ERROR(ECKSUM);
428870ff
BB
258 } else {
259 bcopy(lr, dst, zilc->zc_nused);
260 *end = (char *)dst + zilc->zc_nused;
261 *nbp = zilc->zc_next_blk;
262 }
34dc7c2f 263 }
428870ff 264
13fe0198 265 VERIFY(arc_buf_remove_ref(abuf, &abuf));
428870ff
BB
266 }
267
268 return (error);
269}
270
271/*
272 * Read a TX_WRITE log data block.
273 */
274static int
275zil_read_log_data(zilog_t *zilog, const lr_write_t *lr, void *wbuf)
276{
277 enum zio_flag zio_flags = ZIO_FLAG_CANFAIL;
278 const blkptr_t *bp = &lr->lr_blkptr;
279 uint32_t aflags = ARC_WAIT;
280 arc_buf_t *abuf = NULL;
281 zbookmark_t zb;
282 int error;
283
284 if (BP_IS_HOLE(bp)) {
285 if (wbuf != NULL)
286 bzero(wbuf, MAX(BP_GET_LSIZE(bp), lr->lr_length));
287 return (0);
34dc7c2f
BB
288 }
289
428870ff
BB
290 if (zilog->zl_header->zh_claim_txg == 0)
291 zio_flags |= ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB;
292
293 SET_BOOKMARK(&zb, dmu_objset_id(zilog->zl_os), lr->lr_foid,
294 ZB_ZIL_LEVEL, lr->lr_offset / BP_GET_LSIZE(bp));
295
294f6806 296 error = arc_read(NULL, zilog->zl_spa, bp, arc_getbuf_func, &abuf,
428870ff
BB
297 ZIO_PRIORITY_SYNC_READ, zio_flags, &aflags, &zb);
298
299 if (error == 0) {
300 if (wbuf != NULL)
301 bcopy(abuf->b_data, wbuf, arc_buf_size(abuf));
302 (void) arc_buf_remove_ref(abuf, &abuf);
303 }
34dc7c2f
BB
304
305 return (error);
306}
307
308/*
309 * Parse the intent log, and call parse_func for each valid record within.
34dc7c2f 310 */
428870ff 311int
34dc7c2f
BB
312zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func,
313 zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg)
314{
315 const zil_header_t *zh = zilog->zl_header;
428870ff
BB
316 boolean_t claimed = !!zh->zh_claim_txg;
317 uint64_t claim_blk_seq = claimed ? zh->zh_claim_blk_seq : UINT64_MAX;
318 uint64_t claim_lr_seq = claimed ? zh->zh_claim_lr_seq : UINT64_MAX;
319 uint64_t max_blk_seq = 0;
320 uint64_t max_lr_seq = 0;
321 uint64_t blk_count = 0;
322 uint64_t lr_count = 0;
323 blkptr_t blk, next_blk;
34dc7c2f 324 char *lrbuf, *lrp;
428870ff 325 int error = 0;
34dc7c2f 326
d1d7e268 327 bzero(&next_blk, sizeof (blkptr_t));
d4ed6673 328
428870ff
BB
329 /*
330 * Old logs didn't record the maximum zh_claim_lr_seq.
331 */
332 if (!(zh->zh_flags & ZIL_CLAIM_LR_SEQ_VALID))
333 claim_lr_seq = UINT64_MAX;
34dc7c2f
BB
334
335 /*
336 * Starting at the block pointed to by zh_log we read the log chain.
337 * For each block in the chain we strongly check that block to
338 * ensure its validity. We stop when an invalid block is found.
339 * For each block pointer in the chain we call parse_blk_func().
340 * For each record in each valid block we call parse_lr_func().
341 * If the log has been claimed, stop if we encounter a sequence
342 * number greater than the highest claimed sequence number.
343 */
428870ff
BB
344 lrbuf = zio_buf_alloc(SPA_MAXBLOCKSIZE);
345 zil_bp_tree_init(zilog);
34dc7c2f 346
428870ff
BB
347 for (blk = zh->zh_log; !BP_IS_HOLE(&blk); blk = next_blk) {
348 uint64_t blk_seq = blk.blk_cksum.zc_word[ZIL_ZC_SEQ];
349 int reclen;
d4ed6673 350 char *end = NULL;
34dc7c2f 351
428870ff
BB
352 if (blk_seq > claim_blk_seq)
353 break;
354 if ((error = parse_blk_func(zilog, &blk, arg, txg)) != 0)
355 break;
356 ASSERT3U(max_blk_seq, <, blk_seq);
357 max_blk_seq = blk_seq;
358 blk_count++;
34dc7c2f 359
428870ff
BB
360 if (max_lr_seq == claim_lr_seq && max_blk_seq == claim_blk_seq)
361 break;
34dc7c2f 362
428870ff 363 error = zil_read_log_block(zilog, &blk, &next_blk, lrbuf, &end);
13fe0198 364 if (error != 0)
34dc7c2f
BB
365 break;
366
428870ff 367 for (lrp = lrbuf; lrp < end; lrp += reclen) {
34dc7c2f
BB
368 lr_t *lr = (lr_t *)lrp;
369 reclen = lr->lrc_reclen;
370 ASSERT3U(reclen, >=, sizeof (lr_t));
428870ff
BB
371 if (lr->lrc_seq > claim_lr_seq)
372 goto done;
373 if ((error = parse_lr_func(zilog, lr, arg, txg)) != 0)
374 goto done;
375 ASSERT3U(max_lr_seq, <, lr->lrc_seq);
376 max_lr_seq = lr->lrc_seq;
377 lr_count++;
34dc7c2f 378 }
34dc7c2f 379 }
428870ff
BB
380done:
381 zilog->zl_parse_error = error;
382 zilog->zl_parse_blk_seq = max_blk_seq;
383 zilog->zl_parse_lr_seq = max_lr_seq;
384 zilog->zl_parse_blk_count = blk_count;
385 zilog->zl_parse_lr_count = lr_count;
386
387 ASSERT(!claimed || !(zh->zh_flags & ZIL_CLAIM_LR_SEQ_VALID) ||
388 (max_blk_seq == claim_blk_seq && max_lr_seq == claim_lr_seq));
389
390 zil_bp_tree_fini(zilog);
391 zio_buf_free(lrbuf, SPA_MAXBLOCKSIZE);
34dc7c2f 392
428870ff 393 return (error);
34dc7c2f
BB
394}
395
428870ff 396static int
34dc7c2f
BB
397zil_claim_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t first_txg)
398{
34dc7c2f
BB
399 /*
400 * Claim log block if not already committed and not already claimed.
428870ff 401 * If tx == NULL, just verify that the block is claimable.
34dc7c2f 402 */
b0bc7a84
MG
403 if (BP_IS_HOLE(bp) || bp->blk_birth < first_txg ||
404 zil_bp_tree_add(zilog, bp) != 0)
428870ff
BB
405 return (0);
406
407 return (zio_wait(zio_claim(NULL, zilog->zl_spa,
408 tx == NULL ? 0 : first_txg, bp, spa_claim_notify, NULL,
409 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB)));
34dc7c2f
BB
410}
411
428870ff 412static int
34dc7c2f
BB
413zil_claim_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg)
414{
428870ff
BB
415 lr_write_t *lr = (lr_write_t *)lrc;
416 int error;
417
418 if (lrc->lrc_txtype != TX_WRITE)
419 return (0);
420
421 /*
422 * If the block is not readable, don't claim it. This can happen
423 * in normal operation when a log block is written to disk before
424 * some of the dmu_sync() blocks it points to. In this case, the
425 * transaction cannot have been committed to anyone (we would have
426 * waited for all writes to be stable first), so it is semantically
427 * correct to declare this the end of the log.
428 */
429 if (lr->lr_blkptr.blk_birth >= first_txg &&
430 (error = zil_read_log_data(zilog, lr, NULL)) != 0)
431 return (error);
432 return (zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg));
34dc7c2f
BB
433}
434
435/* ARGSUSED */
428870ff 436static int
34dc7c2f
BB
437zil_free_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t claim_txg)
438{
428870ff
BB
439 zio_free_zil(zilog->zl_spa, dmu_tx_get_txg(tx), bp);
440
441 return (0);
34dc7c2f
BB
442}
443
428870ff 444static int
34dc7c2f
BB
445zil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg)
446{
428870ff
BB
447 lr_write_t *lr = (lr_write_t *)lrc;
448 blkptr_t *bp = &lr->lr_blkptr;
449
34dc7c2f
BB
450 /*
451 * If we previously claimed it, we need to free it.
452 */
428870ff 453 if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE &&
b0bc7a84
MG
454 bp->blk_birth >= claim_txg && zil_bp_tree_add(zilog, bp) == 0 &&
455 !BP_IS_HOLE(bp))
428870ff
BB
456 zio_free(zilog->zl_spa, dmu_tx_get_txg(tx), bp);
457
458 return (0);
459}
460
461static lwb_t *
920dd524 462zil_alloc_lwb(zilog_t *zilog, blkptr_t *bp, uint64_t txg, boolean_t fastwrite)
428870ff
BB
463{
464 lwb_t *lwb;
465
b8d06fca 466 lwb = kmem_cache_alloc(zil_lwb_cache, KM_PUSHPAGE);
428870ff
BB
467 lwb->lwb_zilog = zilog;
468 lwb->lwb_blk = *bp;
920dd524 469 lwb->lwb_fastwrite = fastwrite;
428870ff
BB
470 lwb->lwb_buf = zio_buf_alloc(BP_GET_LSIZE(bp));
471 lwb->lwb_max_txg = txg;
472 lwb->lwb_zio = NULL;
473 lwb->lwb_tx = NULL;
474 if (BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_ZILOG2) {
475 lwb->lwb_nused = sizeof (zil_chain_t);
476 lwb->lwb_sz = BP_GET_LSIZE(bp);
477 } else {
478 lwb->lwb_nused = 0;
479 lwb->lwb_sz = BP_GET_LSIZE(bp) - sizeof (zil_chain_t);
34dc7c2f 480 }
428870ff
BB
481
482 mutex_enter(&zilog->zl_lock);
483 list_insert_tail(&zilog->zl_lwb_list, lwb);
484 mutex_exit(&zilog->zl_lock);
485
486 return (lwb);
34dc7c2f
BB
487}
488
29809a6c
MA
489/*
490 * Called when we create in-memory log transactions so that we know
491 * to cleanup the itxs at the end of spa_sync().
492 */
493void
494zilog_dirty(zilog_t *zilog, uint64_t txg)
495{
496 dsl_pool_t *dp = zilog->zl_dmu_pool;
497 dsl_dataset_t *ds = dmu_objset_ds(zilog->zl_os);
498
499 if (dsl_dataset_is_snapshot(ds))
500 panic("dirtying snapshot!");
501
13fe0198 502 if (txg_list_add(&dp->dp_dirty_zilogs, zilog, txg)) {
29809a6c
MA
503 /* up the hold count until we can be written out */
504 dmu_buf_add_ref(ds->ds_dbuf, zilog);
505 }
506}
507
508boolean_t
509zilog_is_dirty(zilog_t *zilog)
510{
511 dsl_pool_t *dp = zilog->zl_dmu_pool;
512 int t;
513
514 for (t = 0; t < TXG_SIZE; t++) {
515 if (txg_list_member(&dp->dp_dirty_zilogs, zilog, t))
516 return (B_TRUE);
517 }
518 return (B_FALSE);
519}
520
34dc7c2f
BB
521/*
522 * Create an on-disk intent log.
523 */
428870ff 524static lwb_t *
34dc7c2f
BB
525zil_create(zilog_t *zilog)
526{
527 const zil_header_t *zh = zilog->zl_header;
428870ff 528 lwb_t *lwb = NULL;
34dc7c2f
BB
529 uint64_t txg = 0;
530 dmu_tx_t *tx = NULL;
531 blkptr_t blk;
532 int error = 0;
920dd524 533 boolean_t fastwrite = FALSE;
34dc7c2f
BB
534
535 /*
536 * Wait for any previous destroy to complete.
537 */
538 txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
539
540 ASSERT(zh->zh_claim_txg == 0);
541 ASSERT(zh->zh_replay_seq == 0);
542
543 blk = zh->zh_log;
544
545 /*
428870ff
BB
546 * Allocate an initial log block if:
547 * - there isn't one already
548 * - the existing block is the wrong endianess
34dc7c2f 549 */
fb5f0bc8 550 if (BP_IS_HOLE(&blk) || BP_SHOULD_BYTESWAP(&blk)) {
34dc7c2f 551 tx = dmu_tx_create(zilog->zl_os);
428870ff 552 VERIFY(dmu_tx_assign(tx, TXG_WAIT) == 0);
34dc7c2f
BB
553 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
554 txg = dmu_tx_get_txg(tx);
555
fb5f0bc8 556 if (!BP_IS_HOLE(&blk)) {
428870ff 557 zio_free_zil(zilog->zl_spa, txg, &blk);
fb5f0bc8
BB
558 BP_ZERO(&blk);
559 }
560
920dd524 561 error = zio_alloc_zil(zilog->zl_spa, txg, &blk,
5d7a86d1 562 ZIL_MIN_BLKSZ, B_TRUE);
920dd524 563 fastwrite = TRUE;
34dc7c2f
BB
564
565 if (error == 0)
566 zil_init_log_chain(zilog, &blk);
567 }
568
569 /*
570 * Allocate a log write buffer (lwb) for the first log block.
571 */
428870ff 572 if (error == 0)
920dd524 573 lwb = zil_alloc_lwb(zilog, &blk, txg, fastwrite);
34dc7c2f
BB
574
575 /*
576 * If we just allocated the first log block, commit our transaction
577 * and wait for zil_sync() to stuff the block poiner into zh_log.
578 * (zh is part of the MOS, so we cannot modify it in open context.)
579 */
580 if (tx != NULL) {
581 dmu_tx_commit(tx);
582 txg_wait_synced(zilog->zl_dmu_pool, txg);
583 }
584
585 ASSERT(bcmp(&blk, &zh->zh_log, sizeof (blk)) == 0);
428870ff
BB
586
587 return (lwb);
34dc7c2f
BB
588}
589
590/*
591 * In one tx, free all log blocks and clear the log header.
592 * If keep_first is set, then we're replaying a log with no content.
593 * We want to keep the first block, however, so that the first
594 * synchronous transaction doesn't require a txg_wait_synced()
595 * in zil_create(). We don't need to txg_wait_synced() here either
596 * when keep_first is set, because both zil_create() and zil_destroy()
597 * will wait for any in-progress destroys to complete.
598 */
599void
600zil_destroy(zilog_t *zilog, boolean_t keep_first)
601{
602 const zil_header_t *zh = zilog->zl_header;
603 lwb_t *lwb;
604 dmu_tx_t *tx;
605 uint64_t txg;
606
607 /*
608 * Wait for any previous destroy to complete.
609 */
610 txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
611
428870ff
BB
612 zilog->zl_old_header = *zh; /* debugging aid */
613
34dc7c2f
BB
614 if (BP_IS_HOLE(&zh->zh_log))
615 return;
616
617 tx = dmu_tx_create(zilog->zl_os);
428870ff 618 VERIFY(dmu_tx_assign(tx, TXG_WAIT) == 0);
34dc7c2f
BB
619 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
620 txg = dmu_tx_get_txg(tx);
621
622 mutex_enter(&zilog->zl_lock);
623
34dc7c2f
BB
624 ASSERT3U(zilog->zl_destroy_txg, <, txg);
625 zilog->zl_destroy_txg = txg;
626 zilog->zl_keep_first = keep_first;
627
628 if (!list_is_empty(&zilog->zl_lwb_list)) {
629 ASSERT(zh->zh_claim_txg == 0);
3e31d2b0 630 VERIFY(!keep_first);
34dc7c2f 631 while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
920dd524
ED
632 ASSERT(lwb->lwb_zio == NULL);
633 if (lwb->lwb_fastwrite)
634 metaslab_fastwrite_unmark(zilog->zl_spa,
635 &lwb->lwb_blk);
34dc7c2f
BB
636 list_remove(&zilog->zl_lwb_list, lwb);
637 if (lwb->lwb_buf != NULL)
638 zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
428870ff 639 zio_free_zil(zilog->zl_spa, txg, &lwb->lwb_blk);
34dc7c2f
BB
640 kmem_cache_free(zil_lwb_cache, lwb);
641 }
428870ff 642 } else if (!keep_first) {
29809a6c 643 zil_destroy_sync(zilog, tx);
34dc7c2f
BB
644 }
645 mutex_exit(&zilog->zl_lock);
646
647 dmu_tx_commit(tx);
648}
649
29809a6c
MA
650void
651zil_destroy_sync(zilog_t *zilog, dmu_tx_t *tx)
652{
653 ASSERT(list_is_empty(&zilog->zl_lwb_list));
654 (void) zil_parse(zilog, zil_free_log_block,
655 zil_free_log_record, tx, zilog->zl_header->zh_claim_txg);
656}
657
34dc7c2f 658int
428870ff 659zil_claim(const char *osname, void *txarg)
34dc7c2f
BB
660{
661 dmu_tx_t *tx = txarg;
662 uint64_t first_txg = dmu_tx_get_txg(tx);
663 zilog_t *zilog;
664 zil_header_t *zh;
665 objset_t *os;
666 int error;
667
13fe0198
MA
668 error = dmu_objset_own(osname, DMU_OST_ANY, B_FALSE, FTAG, &os);
669 if (error != 0) {
b128c09f 670 cmn_err(CE_WARN, "can't open objset for %s", osname);
34dc7c2f
BB
671 return (0);
672 }
673
674 zilog = dmu_objset_zil(os);
675 zh = zil_header_in_syncing_context(zilog);
676
428870ff 677 if (spa_get_log_state(zilog->zl_spa) == SPA_LOG_CLEAR) {
9babb374 678 if (!BP_IS_HOLE(&zh->zh_log))
428870ff 679 zio_free_zil(zilog->zl_spa, first_txg, &zh->zh_log);
9babb374
BB
680 BP_ZERO(&zh->zh_log);
681 dsl_dataset_dirty(dmu_objset_ds(os), tx);
13fe0198 682 dmu_objset_disown(os, FTAG);
428870ff 683 return (0);
9babb374
BB
684 }
685
34dc7c2f
BB
686 /*
687 * Claim all log blocks if we haven't already done so, and remember
688 * the highest claimed sequence number. This ensures that if we can
689 * read only part of the log now (e.g. due to a missing device),
690 * but we can read the entire log later, we will not try to replay
691 * or destroy beyond the last block we successfully claimed.
692 */
693 ASSERT3U(zh->zh_claim_txg, <=, first_txg);
694 if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) {
428870ff 695 (void) zil_parse(zilog, zil_claim_log_block,
34dc7c2f 696 zil_claim_log_record, tx, first_txg);
428870ff
BB
697 zh->zh_claim_txg = first_txg;
698 zh->zh_claim_blk_seq = zilog->zl_parse_blk_seq;
699 zh->zh_claim_lr_seq = zilog->zl_parse_lr_seq;
700 if (zilog->zl_parse_lr_count || zilog->zl_parse_blk_count > 1)
701 zh->zh_flags |= ZIL_REPLAY_NEEDED;
702 zh->zh_flags |= ZIL_CLAIM_LR_SEQ_VALID;
34dc7c2f
BB
703 dsl_dataset_dirty(dmu_objset_ds(os), tx);
704 }
705
706 ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1));
13fe0198 707 dmu_objset_disown(os, FTAG);
34dc7c2f
BB
708 return (0);
709}
710
b128c09f
BB
711/*
712 * Check the log by walking the log chain.
713 * Checksum errors are ok as they indicate the end of the chain.
714 * Any other error (no device or read failure) returns an error.
715 */
b128c09f 716int
428870ff 717zil_check_log_chain(const char *osname, void *tx)
b128c09f
BB
718{
719 zilog_t *zilog;
b128c09f 720 objset_t *os;
572e2857 721 blkptr_t *bp;
b128c09f
BB
722 int error;
723
428870ff
BB
724 ASSERT(tx == NULL);
725
726 error = dmu_objset_hold(osname, FTAG, &os);
13fe0198 727 if (error != 0) {
b128c09f
BB
728 cmn_err(CE_WARN, "can't open objset for %s", osname);
729 return (0);
730 }
731
732 zilog = dmu_objset_zil(os);
572e2857
BB
733 bp = (blkptr_t *)&zilog->zl_header->zh_log;
734
735 /*
736 * Check the first block and determine if it's on a log device
737 * which may have been removed or faulted prior to loading this
738 * pool. If so, there's no point in checking the rest of the log
739 * as its content should have already been synced to the pool.
740 */
741 if (!BP_IS_HOLE(bp)) {
742 vdev_t *vd;
743 boolean_t valid = B_TRUE;
744
745 spa_config_enter(os->os_spa, SCL_STATE, FTAG, RW_READER);
746 vd = vdev_lookup_top(os->os_spa, DVA_GET_VDEV(&bp->blk_dva[0]));
747 if (vd->vdev_islog && vdev_is_dead(vd))
748 valid = vdev_log_state_valid(vd);
749 spa_config_exit(os->os_spa, SCL_STATE, FTAG);
750
751 if (!valid) {
752 dmu_objset_rele(os, FTAG);
753 return (0);
754 }
755 }
b128c09f 756
428870ff
BB
757 /*
758 * Because tx == NULL, zil_claim_log_block() will not actually claim
759 * any blocks, but just determine whether it is possible to do so.
760 * In addition to checking the log chain, zil_claim_log_block()
761 * will invoke zio_claim() with a done func of spa_claim_notify(),
762 * which will update spa_max_claim_txg. See spa_load() for details.
763 */
764 error = zil_parse(zilog, zil_claim_log_block, zil_claim_log_record, tx,
765 zilog->zl_header->zh_claim_txg ? -1ULL : spa_first_txg(os->os_spa));
766
767 dmu_objset_rele(os, FTAG);
768
769 return ((error == ECKSUM || error == ENOENT) ? 0 : error);
b128c09f
BB
770}
771
34dc7c2f
BB
772static int
773zil_vdev_compare(const void *x1, const void *x2)
774{
572e2857
BB
775 const uint64_t v1 = ((zil_vdev_node_t *)x1)->zv_vdev;
776 const uint64_t v2 = ((zil_vdev_node_t *)x2)->zv_vdev;
34dc7c2f
BB
777
778 if (v1 < v2)
779 return (-1);
780 if (v1 > v2)
781 return (1);
782
783 return (0);
784}
785
786void
428870ff 787zil_add_block(zilog_t *zilog, const blkptr_t *bp)
34dc7c2f
BB
788{
789 avl_tree_t *t = &zilog->zl_vdev_tree;
790 avl_index_t where;
791 zil_vdev_node_t *zv, zvsearch;
792 int ndvas = BP_GET_NDVAS(bp);
793 int i;
794
795 if (zfs_nocacheflush)
796 return;
797
798 ASSERT(zilog->zl_writer);
799
800 /*
801 * Even though we're zl_writer, we still need a lock because the
802 * zl_get_data() callbacks may have dmu_sync() done callbacks
803 * that will run concurrently.
804 */
805 mutex_enter(&zilog->zl_vdev_lock);
806 for (i = 0; i < ndvas; i++) {
807 zvsearch.zv_vdev = DVA_GET_VDEV(&bp->blk_dva[i]);
808 if (avl_find(t, &zvsearch, &where) == NULL) {
b8d06fca 809 zv = kmem_alloc(sizeof (*zv), KM_PUSHPAGE);
34dc7c2f
BB
810 zv->zv_vdev = zvsearch.zv_vdev;
811 avl_insert(t, zv, where);
812 }
813 }
814 mutex_exit(&zilog->zl_vdev_lock);
815}
816
572e2857 817static void
34dc7c2f
BB
818zil_flush_vdevs(zilog_t *zilog)
819{
820 spa_t *spa = zilog->zl_spa;
821 avl_tree_t *t = &zilog->zl_vdev_tree;
822 void *cookie = NULL;
823 zil_vdev_node_t *zv;
824 zio_t *zio;
825
826 ASSERT(zilog->zl_writer);
827
828 /*
829 * We don't need zl_vdev_lock here because we're the zl_writer,
830 * and all zl_get_data() callbacks are done.
831 */
832 if (avl_numnodes(t) == 0)
833 return;
834
b128c09f 835 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
34dc7c2f 836
b128c09f 837 zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
34dc7c2f
BB
838
839 while ((zv = avl_destroy_nodes(t, &cookie)) != NULL) {
840 vdev_t *vd = vdev_lookup_top(spa, zv->zv_vdev);
841 if (vd != NULL)
842 zio_flush(zio, vd);
843 kmem_free(zv, sizeof (*zv));
844 }
845
846 /*
847 * Wait for all the flushes to complete. Not all devices actually
848 * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails.
849 */
850 (void) zio_wait(zio);
851
b128c09f 852 spa_config_exit(spa, SCL_STATE, FTAG);
34dc7c2f
BB
853}
854
855/*
856 * Function called when a log block write completes
857 */
858static void
859zil_lwb_write_done(zio_t *zio)
860{
861 lwb_t *lwb = zio->io_private;
862 zilog_t *zilog = lwb->lwb_zilog;
428870ff 863 dmu_tx_t *tx = lwb->lwb_tx;
34dc7c2f 864
b128c09f 865 ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF);
b128c09f
BB
866 ASSERT(BP_GET_TYPE(zio->io_bp) == DMU_OT_INTENT_LOG);
867 ASSERT(BP_GET_LEVEL(zio->io_bp) == 0);
868 ASSERT(BP_GET_BYTEORDER(zio->io_bp) == ZFS_HOST_BYTEORDER);
869 ASSERT(!BP_IS_GANG(zio->io_bp));
870 ASSERT(!BP_IS_HOLE(zio->io_bp));
9b67f605 871 ASSERT(BP_GET_FILL(zio->io_bp) == 0);
b128c09f 872
34dc7c2f 873 /*
9babb374
BB
874 * Ensure the lwb buffer pointer is cleared before releasing
875 * the txg. If we have had an allocation failure and
876 * the txg is waiting to sync then we want want zil_sync()
877 * to remove the lwb so that it's not picked up as the next new
878 * one in zil_commit_writer(). zil_sync() will only remove
879 * the lwb if lwb_buf is null.
34dc7c2f 880 */
34dc7c2f
BB
881 zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
882 mutex_enter(&zilog->zl_lock);
920dd524
ED
883 lwb->lwb_zio = NULL;
884 lwb->lwb_fastwrite = FALSE;
34dc7c2f 885 lwb->lwb_buf = NULL;
428870ff
BB
886 lwb->lwb_tx = NULL;
887 mutex_exit(&zilog->zl_lock);
9babb374
BB
888
889 /*
890 * Now that we've written this log block, we have a stable pointer
891 * to the next block in the chain, so it's OK to let the txg in
428870ff 892 * which we allocated the next block sync.
9babb374 893 */
428870ff 894 dmu_tx_commit(tx);
34dc7c2f
BB
895}
896
897/*
898 * Initialize the io for a log block.
34dc7c2f
BB
899 */
900static void
901zil_lwb_write_init(zilog_t *zilog, lwb_t *lwb)
902{
903 zbookmark_t zb;
904
428870ff
BB
905 SET_BOOKMARK(&zb, lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET],
906 ZB_ZIL_OBJECT, ZB_ZIL_LEVEL,
907 lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ]);
34dc7c2f
BB
908
909 if (zilog->zl_root_zio == NULL) {
910 zilog->zl_root_zio = zio_root(zilog->zl_spa, NULL, NULL,
911 ZIO_FLAG_CANFAIL);
912 }
920dd524
ED
913
914 /* Lock so zil_sync() doesn't fastwrite_unmark after zio is created */
915 mutex_enter(&zilog->zl_lock);
34dc7c2f 916 if (lwb->lwb_zio == NULL) {
920dd524
ED
917 if (!lwb->lwb_fastwrite) {
918 metaslab_fastwrite_mark(zilog->zl_spa, &lwb->lwb_blk);
919 lwb->lwb_fastwrite = 1;
920 }
34dc7c2f 921 lwb->lwb_zio = zio_rewrite(zilog->zl_root_zio, zilog->zl_spa,
428870ff 922 0, &lwb->lwb_blk, lwb->lwb_buf, BP_GET_LSIZE(&lwb->lwb_blk),
e8b96c60 923 zil_lwb_write_done, lwb, ZIO_PRIORITY_SYNC_WRITE,
920dd524
ED
924 ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE |
925 ZIO_FLAG_FASTWRITE, &zb);
34dc7c2f 926 }
920dd524 927 mutex_exit(&zilog->zl_lock);
34dc7c2f
BB
928}
929
428870ff
BB
930/*
931 * Define a limited set of intent log block sizes.
d3cc8b15 932 *
428870ff
BB
933 * These must be a multiple of 4KB. Note only the amount used (again
934 * aligned to 4KB) actually gets written. However, we can't always just
935 * allocate SPA_MAXBLOCKSIZE as the slog space could be exhausted.
936 */
937uint64_t zil_block_buckets[] = {
938 4096, /* non TX_WRITE */
939 8192+4096, /* data base */
940 32*1024 + 4096, /* NFS writes */
941 UINT64_MAX
942};
943
944/*
5d7a86d1
ED
945 * Use the slog as long as the current commit size is less than the
946 * limit or the total list size is less than 2X the limit. Limit
947 * checking is disabled by setting zil_slog_limit to UINT64_MAX.
428870ff 948 */
ee191e80 949unsigned long zil_slog_limit = 1024 * 1024;
5d7a86d1
ED
950#define USE_SLOG(zilog) (((zilog)->zl_cur_used < zil_slog_limit) || \
951 ((zilog)->zl_itx_list_sz < (zil_slog_limit << 1)))
428870ff 952
34dc7c2f
BB
953/*
954 * Start a log block write and advance to the next log block.
955 * Calls are serialized.
956 */
957static lwb_t *
958zil_lwb_write_start(zilog_t *zilog, lwb_t *lwb)
959{
428870ff
BB
960 lwb_t *nlwb = NULL;
961 zil_chain_t *zilc;
34dc7c2f 962 spa_t *spa = zilog->zl_spa;
428870ff
BB
963 blkptr_t *bp;
964 dmu_tx_t *tx;
34dc7c2f 965 uint64_t txg;
428870ff
BB
966 uint64_t zil_blksz, wsz;
967 int i, error;
b6ad9671 968 boolean_t use_slog;
428870ff
BB
969
970 if (BP_GET_CHECKSUM(&lwb->lwb_blk) == ZIO_CHECKSUM_ZILOG2) {
971 zilc = (zil_chain_t *)lwb->lwb_buf;
972 bp = &zilc->zc_next_blk;
973 } else {
974 zilc = (zil_chain_t *)(lwb->lwb_buf + lwb->lwb_sz);
975 bp = &zilc->zc_next_blk;
976 }
34dc7c2f 977
428870ff 978 ASSERT(lwb->lwb_nused <= lwb->lwb_sz);
34dc7c2f
BB
979
980 /*
981 * Allocate the next block and save its address in this block
982 * before writing it in order to establish the log chain.
983 * Note that if the allocation of nlwb synced before we wrote
984 * the block that points at it (lwb), we'd leak it if we crashed.
428870ff
BB
985 * Therefore, we don't do dmu_tx_commit() until zil_lwb_write_done().
986 * We dirty the dataset to ensure that zil_sync() will be called
987 * to clean up in the event of allocation failure or I/O failure.
34dc7c2f 988 */
428870ff
BB
989 tx = dmu_tx_create(zilog->zl_os);
990 VERIFY(dmu_tx_assign(tx, TXG_WAIT) == 0);
991 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
992 txg = dmu_tx_get_txg(tx);
993
994 lwb->lwb_tx = tx;
34dc7c2f
BB
995
996 /*
428870ff
BB
997 * Log blocks are pre-allocated. Here we select the size of the next
998 * block, based on size used in the last block.
999 * - first find the smallest bucket that will fit the block from a
1000 * limited set of block sizes. This is because it's faster to write
1001 * blocks allocated from the same metaslab as they are adjacent or
1002 * close.
1003 * - next find the maximum from the new suggested size and an array of
1004 * previous sizes. This lessens a picket fence effect of wrongly
1005 * guesssing the size if we have a stream of say 2k, 64k, 2k, 64k
1006 * requests.
1007 *
1008 * Note we only write what is used, but we can't just allocate
1009 * the maximum block size because we can exhaust the available
1010 * pool log space.
34dc7c2f 1011 */
428870ff
BB
1012 zil_blksz = zilog->zl_cur_used + sizeof (zil_chain_t);
1013 for (i = 0; zil_blksz > zil_block_buckets[i]; i++)
1014 continue;
1015 zil_blksz = zil_block_buckets[i];
1016 if (zil_blksz == UINT64_MAX)
1017 zil_blksz = SPA_MAXBLOCKSIZE;
1018 zilog->zl_prev_blks[zilog->zl_prev_rotor] = zil_blksz;
1019 for (i = 0; i < ZIL_PREV_BLKS; i++)
1020 zil_blksz = MAX(zil_blksz, zilog->zl_prev_blks[i]);
1021 zilog->zl_prev_rotor = (zilog->zl_prev_rotor + 1) & (ZIL_PREV_BLKS - 1);
34dc7c2f
BB
1022
1023 BP_ZERO(bp);
b6ad9671 1024 use_slog = USE_SLOG(zilog);
13fe0198
MA
1025 error = zio_alloc_zil(spa, txg, bp, zil_blksz,
1026 USE_SLOG(zilog));
d1d7e268 1027 if (use_slog) {
b6ad9671
ED
1028 ZIL_STAT_BUMP(zil_itx_metaslab_slog_count);
1029 ZIL_STAT_INCR(zil_itx_metaslab_slog_bytes, lwb->lwb_nused);
d1d7e268 1030 } else {
b6ad9671
ED
1031 ZIL_STAT_BUMP(zil_itx_metaslab_normal_count);
1032 ZIL_STAT_INCR(zil_itx_metaslab_normal_bytes, lwb->lwb_nused);
1033 }
13fe0198 1034 if (error == 0) {
428870ff
BB
1035 ASSERT3U(bp->blk_birth, ==, txg);
1036 bp->blk_cksum = lwb->lwb_blk.blk_cksum;
1037 bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++;
34dc7c2f
BB
1038
1039 /*
428870ff 1040 * Allocate a new log write buffer (lwb).
34dc7c2f 1041 */
920dd524 1042 nlwb = zil_alloc_lwb(zilog, bp, txg, TRUE);
34dc7c2f 1043
428870ff
BB
1044 /* Record the block for later vdev flushing */
1045 zil_add_block(zilog, &lwb->lwb_blk);
34dc7c2f
BB
1046 }
1047
428870ff
BB
1048 if (BP_GET_CHECKSUM(&lwb->lwb_blk) == ZIO_CHECKSUM_ZILOG2) {
1049 /* For Slim ZIL only write what is used. */
1050 wsz = P2ROUNDUP_TYPED(lwb->lwb_nused, ZIL_MIN_BLKSZ, uint64_t);
1051 ASSERT3U(wsz, <=, lwb->lwb_sz);
1052 zio_shrink(lwb->lwb_zio, wsz);
34dc7c2f 1053
428870ff
BB
1054 } else {
1055 wsz = lwb->lwb_sz;
1056 }
34dc7c2f 1057
428870ff
BB
1058 zilc->zc_pad = 0;
1059 zilc->zc_nused = lwb->lwb_nused;
1060 zilc->zc_eck.zec_cksum = lwb->lwb_blk.blk_cksum;
34dc7c2f
BB
1061
1062 /*
428870ff 1063 * clear unused data for security
34dc7c2f 1064 */
428870ff 1065 bzero(lwb->lwb_buf + lwb->lwb_nused, wsz - lwb->lwb_nused);
34dc7c2f 1066
428870ff 1067 zio_nowait(lwb->lwb_zio); /* Kick off the write for the old log block */
34dc7c2f
BB
1068
1069 /*
428870ff
BB
1070 * If there was an allocation failure then nlwb will be null which
1071 * forces a txg_wait_synced().
34dc7c2f 1072 */
34dc7c2f
BB
1073 return (nlwb);
1074}
1075
1076static lwb_t *
1077zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb)
1078{
1079 lr_t *lrc = &itx->itx_lr; /* common log record */
428870ff
BB
1080 lr_write_t *lrw = (lr_write_t *)lrc;
1081 char *lr_buf;
34dc7c2f
BB
1082 uint64_t txg = lrc->lrc_txg;
1083 uint64_t reclen = lrc->lrc_reclen;
428870ff 1084 uint64_t dlen = 0;
34dc7c2f
BB
1085
1086 if (lwb == NULL)
1087 return (NULL);
428870ff 1088
34dc7c2f 1089 ASSERT(lwb->lwb_buf != NULL);
29809a6c
MA
1090 ASSERT(zilog_is_dirty(zilog) ||
1091 spa_freeze_txg(zilog->zl_spa) != UINT64_MAX);
34dc7c2f
BB
1092
1093 if (lrc->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY)
1094 dlen = P2ROUNDUP_TYPED(
428870ff 1095 lrw->lr_length, sizeof (uint64_t), uint64_t);
34dc7c2f
BB
1096
1097 zilog->zl_cur_used += (reclen + dlen);
1098
1099 zil_lwb_write_init(zilog, lwb);
1100
1101 /*
1102 * If this record won't fit in the current log block, start a new one.
1103 */
428870ff 1104 if (lwb->lwb_nused + reclen + dlen > lwb->lwb_sz) {
34dc7c2f
BB
1105 lwb = zil_lwb_write_start(zilog, lwb);
1106 if (lwb == NULL)
1107 return (NULL);
1108 zil_lwb_write_init(zilog, lwb);
428870ff
BB
1109 ASSERT(LWB_EMPTY(lwb));
1110 if (lwb->lwb_nused + reclen + dlen > lwb->lwb_sz) {
34dc7c2f
BB
1111 txg_wait_synced(zilog->zl_dmu_pool, txg);
1112 return (lwb);
1113 }
1114 }
1115
428870ff
BB
1116 lr_buf = lwb->lwb_buf + lwb->lwb_nused;
1117 bcopy(lrc, lr_buf, reclen);
1118 lrc = (lr_t *)lr_buf;
1119 lrw = (lr_write_t *)lrc;
34dc7c2f 1120
b6ad9671
ED
1121 ZIL_STAT_BUMP(zil_itx_count);
1122
34dc7c2f
BB
1123 /*
1124 * If it's a write, fetch the data or get its blkptr as appropriate.
1125 */
1126 if (lrc->lrc_txtype == TX_WRITE) {
1127 if (txg > spa_freeze_txg(zilog->zl_spa))
1128 txg_wait_synced(zilog->zl_dmu_pool, txg);
b6ad9671
ED
1129 if (itx->itx_wr_state == WR_COPIED) {
1130 ZIL_STAT_BUMP(zil_itx_copied_count);
1131 ZIL_STAT_INCR(zil_itx_copied_bytes, lrw->lr_length);
1132 } else {
34dc7c2f
BB
1133 char *dbuf;
1134 int error;
1135
34dc7c2f
BB
1136 if (dlen) {
1137 ASSERT(itx->itx_wr_state == WR_NEED_COPY);
428870ff
BB
1138 dbuf = lr_buf + reclen;
1139 lrw->lr_common.lrc_reclen += dlen;
b6ad9671 1140 ZIL_STAT_BUMP(zil_itx_needcopy_count);
d1d7e268
MK
1141 ZIL_STAT_INCR(zil_itx_needcopy_bytes,
1142 lrw->lr_length);
34dc7c2f
BB
1143 } else {
1144 ASSERT(itx->itx_wr_state == WR_INDIRECT);
1145 dbuf = NULL;
b6ad9671 1146 ZIL_STAT_BUMP(zil_itx_indirect_count);
d1d7e268
MK
1147 ZIL_STAT_INCR(zil_itx_indirect_bytes,
1148 lrw->lr_length);
34dc7c2f
BB
1149 }
1150 error = zilog->zl_get_data(
428870ff 1151 itx->itx_private, lrw, dbuf, lwb->lwb_zio);
45d1cae3
BB
1152 if (error == EIO) {
1153 txg_wait_synced(zilog->zl_dmu_pool, txg);
1154 return (lwb);
1155 }
13fe0198 1156 if (error != 0) {
34dc7c2f
BB
1157 ASSERT(error == ENOENT || error == EEXIST ||
1158 error == EALREADY);
1159 return (lwb);
1160 }
1161 }
1162 }
1163
428870ff
BB
1164 /*
1165 * We're actually making an entry, so update lrc_seq to be the
1166 * log record sequence number. Note that this is generally not
1167 * equal to the itx sequence number because not all transactions
1168 * are synchronous, and sometimes spa_sync() gets there first.
1169 */
1170 lrc->lrc_seq = ++zilog->zl_lr_seq; /* we are single threaded */
34dc7c2f
BB
1171 lwb->lwb_nused += reclen + dlen;
1172 lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg);
428870ff 1173 ASSERT3U(lwb->lwb_nused, <=, lwb->lwb_sz);
c99c9001 1174 ASSERT0(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)));
34dc7c2f
BB
1175
1176 return (lwb);
1177}
1178
1179itx_t *
1180zil_itx_create(uint64_t txtype, size_t lrsize)
1181{
1182 itx_t *itx;
1183
1184 lrsize = P2ROUNDUP_TYPED(lrsize, sizeof (uint64_t), size_t);
1185
701b1f81
BB
1186 itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize,
1187 KM_PUSHPAGE | KM_NODEBUG);
34dc7c2f
BB
1188 itx->itx_lr.lrc_txtype = txtype;
1189 itx->itx_lr.lrc_reclen = lrsize;
1190 itx->itx_sod = lrsize; /* if write & WR_NEED_COPY will be increased */
1191 itx->itx_lr.lrc_seq = 0; /* defensive */
572e2857 1192 itx->itx_sync = B_TRUE; /* default is synchronous */
119a394a
ED
1193 itx->itx_callback = NULL;
1194 itx->itx_callback_data = NULL;
34dc7c2f
BB
1195
1196 return (itx);
1197}
1198
428870ff
BB
1199void
1200zil_itx_destroy(itx_t *itx)
1201{
1202 kmem_free(itx, offsetof(itx_t, itx_lr) + itx->itx_lr.lrc_reclen);
1203}
1204
572e2857
BB
1205/*
1206 * Free up the sync and async itxs. The itxs_t has already been detached
1207 * so no locks are needed.
1208 */
1209static void
1210zil_itxg_clean(itxs_t *itxs)
34dc7c2f 1211{
572e2857
BB
1212 itx_t *itx;
1213 list_t *list;
1214 avl_tree_t *t;
1215 void *cookie;
1216 itx_async_node_t *ian;
1217
1218 list = &itxs->i_sync_list;
1219 while ((itx = list_head(list)) != NULL) {
119a394a
ED
1220 if (itx->itx_callback != NULL)
1221 itx->itx_callback(itx->itx_callback_data);
572e2857
BB
1222 list_remove(list, itx);
1223 kmem_free(itx, offsetof(itx_t, itx_lr) +
1224 itx->itx_lr.lrc_reclen);
1225 }
34dc7c2f 1226
572e2857
BB
1227 cookie = NULL;
1228 t = &itxs->i_async_tree;
1229 while ((ian = avl_destroy_nodes(t, &cookie)) != NULL) {
1230 list = &ian->ia_list;
1231 while ((itx = list_head(list)) != NULL) {
119a394a
ED
1232 if (itx->itx_callback != NULL)
1233 itx->itx_callback(itx->itx_callback_data);
572e2857
BB
1234 list_remove(list, itx);
1235 kmem_free(itx, offsetof(itx_t, itx_lr) +
1236 itx->itx_lr.lrc_reclen);
1237 }
1238 list_destroy(list);
1239 kmem_free(ian, sizeof (itx_async_node_t));
1240 }
1241 avl_destroy(t);
34dc7c2f 1242
572e2857
BB
1243 kmem_free(itxs, sizeof (itxs_t));
1244}
34dc7c2f 1245
572e2857
BB
1246static int
1247zil_aitx_compare(const void *x1, const void *x2)
1248{
1249 const uint64_t o1 = ((itx_async_node_t *)x1)->ia_foid;
1250 const uint64_t o2 = ((itx_async_node_t *)x2)->ia_foid;
1251
1252 if (o1 < o2)
1253 return (-1);
1254 if (o1 > o2)
1255 return (1);
1256
1257 return (0);
34dc7c2f
BB
1258}
1259
1260/*
572e2857 1261 * Remove all async itx with the given oid.
34dc7c2f
BB
1262 */
1263static void
572e2857 1264zil_remove_async(zilog_t *zilog, uint64_t oid)
34dc7c2f 1265{
572e2857
BB
1266 uint64_t otxg, txg;
1267 itx_async_node_t *ian;
1268 avl_tree_t *t;
1269 avl_index_t where;
34dc7c2f
BB
1270 list_t clean_list;
1271 itx_t *itx;
1272
572e2857 1273 ASSERT(oid != 0);
34dc7c2f
BB
1274 list_create(&clean_list, sizeof (itx_t), offsetof(itx_t, itx_node));
1275
572e2857
BB
1276 if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */
1277 otxg = ZILTEST_TXG;
1278 else
1279 otxg = spa_last_synced_txg(zilog->zl_spa) + 1;
34dc7c2f 1280
572e2857
BB
1281 for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) {
1282 itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK];
1283
1284 mutex_enter(&itxg->itxg_lock);
1285 if (itxg->itxg_txg != txg) {
1286 mutex_exit(&itxg->itxg_lock);
1287 continue;
1288 }
34dc7c2f 1289
572e2857
BB
1290 /*
1291 * Locate the object node and append its list.
1292 */
1293 t = &itxg->itxg_itxs->i_async_tree;
1294 ian = avl_find(t, &oid, &where);
1295 if (ian != NULL)
1296 list_move_tail(&clean_list, &ian->ia_list);
1297 mutex_exit(&itxg->itxg_lock);
1298 }
34dc7c2f 1299 while ((itx = list_head(&clean_list)) != NULL) {
119a394a
ED
1300 if (itx->itx_callback != NULL)
1301 itx->itx_callback(itx->itx_callback_data);
34dc7c2f 1302 list_remove(&clean_list, itx);
572e2857
BB
1303 kmem_free(itx, offsetof(itx_t, itx_lr) +
1304 itx->itx_lr.lrc_reclen);
34dc7c2f
BB
1305 }
1306 list_destroy(&clean_list);
1307}
1308
572e2857
BB
1309void
1310zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx)
1311{
1312 uint64_t txg;
1313 itxg_t *itxg;
1314 itxs_t *itxs, *clean = NULL;
1315
1316 /*
1317 * Object ids can be re-instantiated in the next txg so
1318 * remove any async transactions to avoid future leaks.
1319 * This can happen if a fsync occurs on the re-instantiated
1320 * object for a WR_INDIRECT or WR_NEED_COPY write, which gets
1321 * the new file data and flushes a write record for the old object.
1322 */
1323 if ((itx->itx_lr.lrc_txtype & ~TX_CI) == TX_REMOVE)
1324 zil_remove_async(zilog, itx->itx_oid);
1325
1326 /*
1327 * Ensure the data of a renamed file is committed before the rename.
1328 */
1329 if ((itx->itx_lr.lrc_txtype & ~TX_CI) == TX_RENAME)
1330 zil_async_to_sync(zilog, itx->itx_oid);
1331
29809a6c 1332 if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX)
572e2857
BB
1333 txg = ZILTEST_TXG;
1334 else
1335 txg = dmu_tx_get_txg(tx);
1336
1337 itxg = &zilog->zl_itxg[txg & TXG_MASK];
1338 mutex_enter(&itxg->itxg_lock);
1339 itxs = itxg->itxg_itxs;
1340 if (itxg->itxg_txg != txg) {
1341 if (itxs != NULL) {
1342 /*
1343 * The zil_clean callback hasn't got around to cleaning
1344 * this itxg. Save the itxs for release below.
1345 * This should be rare.
1346 */
1347 atomic_add_64(&zilog->zl_itx_list_sz, -itxg->itxg_sod);
1348 itxg->itxg_sod = 0;
1349 clean = itxg->itxg_itxs;
1350 }
1351 ASSERT(itxg->itxg_sod == 0);
1352 itxg->itxg_txg = txg;
d1d7e268
MK
1353 itxs = itxg->itxg_itxs = kmem_zalloc(sizeof (itxs_t),
1354 KM_PUSHPAGE);
572e2857
BB
1355
1356 list_create(&itxs->i_sync_list, sizeof (itx_t),
1357 offsetof(itx_t, itx_node));
1358 avl_create(&itxs->i_async_tree, zil_aitx_compare,
1359 sizeof (itx_async_node_t),
1360 offsetof(itx_async_node_t, ia_node));
1361 }
1362 if (itx->itx_sync) {
1363 list_insert_tail(&itxs->i_sync_list, itx);
1364 atomic_add_64(&zilog->zl_itx_list_sz, itx->itx_sod);
1365 itxg->itxg_sod += itx->itx_sod;
1366 } else {
1367 avl_tree_t *t = &itxs->i_async_tree;
1368 uint64_t foid = ((lr_ooo_t *)&itx->itx_lr)->lr_foid;
1369 itx_async_node_t *ian;
1370 avl_index_t where;
1371
1372 ian = avl_find(t, &foid, &where);
1373 if (ian == NULL) {
d1d7e268
MK
1374 ian = kmem_alloc(sizeof (itx_async_node_t),
1375 KM_PUSHPAGE);
572e2857
BB
1376 list_create(&ian->ia_list, sizeof (itx_t),
1377 offsetof(itx_t, itx_node));
1378 ian->ia_foid = foid;
1379 avl_insert(t, ian, where);
1380 }
1381 list_insert_tail(&ian->ia_list, itx);
1382 }
1383
1384 itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx);
29809a6c 1385 zilog_dirty(zilog, txg);
572e2857
BB
1386 mutex_exit(&itxg->itxg_lock);
1387
1388 /* Release the old itxs now we've dropped the lock */
1389 if (clean != NULL)
1390 zil_itxg_clean(clean);
1391}
1392
34dc7c2f
BB
1393/*
1394 * If there are any in-memory intent log transactions which have now been
29809a6c
MA
1395 * synced then start up a taskq to free them. We should only do this after we
1396 * have written out the uberblocks (i.e. txg has been comitted) so that
1397 * don't inadvertently clean out in-memory log records that would be required
1398 * by zil_commit().
34dc7c2f
BB
1399 */
1400void
572e2857 1401zil_clean(zilog_t *zilog, uint64_t synced_txg)
34dc7c2f 1402{
572e2857
BB
1403 itxg_t *itxg = &zilog->zl_itxg[synced_txg & TXG_MASK];
1404 itxs_t *clean_me;
34dc7c2f 1405
572e2857
BB
1406 mutex_enter(&itxg->itxg_lock);
1407 if (itxg->itxg_itxs == NULL || itxg->itxg_txg == ZILTEST_TXG) {
1408 mutex_exit(&itxg->itxg_lock);
1409 return;
1410 }
1411 ASSERT3U(itxg->itxg_txg, <=, synced_txg);
1412 ASSERT(itxg->itxg_txg != 0);
1413 ASSERT(zilog->zl_clean_taskq != NULL);
1414 atomic_add_64(&zilog->zl_itx_list_sz, -itxg->itxg_sod);
1415 itxg->itxg_sod = 0;
1416 clean_me = itxg->itxg_itxs;
1417 itxg->itxg_itxs = NULL;
1418 itxg->itxg_txg = 0;
1419 mutex_exit(&itxg->itxg_lock);
1420 /*
1421 * Preferably start a task queue to free up the old itxs but
1422 * if taskq_dispatch can't allocate resources to do that then
1423 * free it in-line. This should be rare. Note, using TQ_SLEEP
1424 * created a bad performance problem.
1425 */
1426 if (taskq_dispatch(zilog->zl_clean_taskq,
b8864a23 1427 (void (*)(void *))zil_itxg_clean, clean_me, TQ_NOSLEEP) == 0)
572e2857
BB
1428 zil_itxg_clean(clean_me);
1429}
1430
1431/*
1432 * Get the list of itxs to commit into zl_itx_commit_list.
1433 */
1434static void
1435zil_get_commit_list(zilog_t *zilog)
1436{
1437 uint64_t otxg, txg;
1438 list_t *commit_list = &zilog->zl_itx_commit_list;
1439 uint64_t push_sod = 0;
1440
1441 if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */
1442 otxg = ZILTEST_TXG;
1443 else
1444 otxg = spa_last_synced_txg(zilog->zl_spa) + 1;
1445
1446 for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) {
1447 itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK];
1448
1449 mutex_enter(&itxg->itxg_lock);
1450 if (itxg->itxg_txg != txg) {
1451 mutex_exit(&itxg->itxg_lock);
1452 continue;
1453 }
1454
1455 list_move_tail(commit_list, &itxg->itxg_itxs->i_sync_list);
1456 push_sod += itxg->itxg_sod;
1457 itxg->itxg_sod = 0;
1458
1459 mutex_exit(&itxg->itxg_lock);
1460 }
1461 atomic_add_64(&zilog->zl_itx_list_sz, -push_sod);
1462}
1463
1464/*
1465 * Move the async itxs for a specified object to commit into sync lists.
1466 */
1467static void
1468zil_async_to_sync(zilog_t *zilog, uint64_t foid)
1469{
1470 uint64_t otxg, txg;
1471 itx_async_node_t *ian;
1472 avl_tree_t *t;
1473 avl_index_t where;
1474
1475 if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */
1476 otxg = ZILTEST_TXG;
1477 else
1478 otxg = spa_last_synced_txg(zilog->zl_spa) + 1;
1479
1480 for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) {
1481 itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK];
1482
1483 mutex_enter(&itxg->itxg_lock);
1484 if (itxg->itxg_txg != txg) {
1485 mutex_exit(&itxg->itxg_lock);
1486 continue;
1487 }
1488
1489 /*
1490 * If a foid is specified then find that node and append its
1491 * list. Otherwise walk the tree appending all the lists
1492 * to the sync list. We add to the end rather than the
1493 * beginning to ensure the create has happened.
1494 */
1495 t = &itxg->itxg_itxs->i_async_tree;
1496 if (foid != 0) {
1497 ian = avl_find(t, &foid, &where);
1498 if (ian != NULL) {
1499 list_move_tail(&itxg->itxg_itxs->i_sync_list,
1500 &ian->ia_list);
1501 }
1502 } else {
1503 void *cookie = NULL;
1504
1505 while ((ian = avl_destroy_nodes(t, &cookie)) != NULL) {
1506 list_move_tail(&itxg->itxg_itxs->i_sync_list,
1507 &ian->ia_list);
1508 list_destroy(&ian->ia_list);
1509 kmem_free(ian, sizeof (itx_async_node_t));
1510 }
1511 }
1512 mutex_exit(&itxg->itxg_lock);
34dc7c2f 1513 }
34dc7c2f
BB
1514}
1515
b128c09f 1516static void
572e2857 1517zil_commit_writer(zilog_t *zilog)
34dc7c2f
BB
1518{
1519 uint64_t txg;
572e2857 1520 itx_t *itx;
34dc7c2f 1521 lwb_t *lwb;
572e2857 1522 spa_t *spa = zilog->zl_spa;
428870ff 1523 int error = 0;
34dc7c2f 1524
b128c09f 1525 ASSERT(zilog->zl_root_zio == NULL);
572e2857
BB
1526
1527 mutex_exit(&zilog->zl_lock);
1528
1529 zil_get_commit_list(zilog);
1530
1531 /*
1532 * Return if there's nothing to commit before we dirty the fs by
1533 * calling zil_create().
1534 */
1535 if (list_head(&zilog->zl_itx_commit_list) == NULL) {
1536 mutex_enter(&zilog->zl_lock);
1537 return;
1538 }
34dc7c2f
BB
1539
1540 if (zilog->zl_suspend) {
1541 lwb = NULL;
1542 } else {
1543 lwb = list_tail(&zilog->zl_lwb_list);
572e2857 1544 if (lwb == NULL)
428870ff 1545 lwb = zil_create(zilog);
34dc7c2f
BB
1546 }
1547
34dc7c2f 1548 DTRACE_PROBE1(zil__cw1, zilog_t *, zilog);
119a394a 1549 for (itx = list_head(&zilog->zl_itx_commit_list); itx != NULL;
d1d7e268 1550 itx = list_next(&zilog->zl_itx_commit_list, itx)) {
34dc7c2f
BB
1551 txg = itx->itx_lr.lrc_txg;
1552 ASSERT(txg);
1553
572e2857 1554 if (txg > spa_last_synced_txg(spa) || txg > spa_freeze_txg(spa))
34dc7c2f 1555 lwb = zil_lwb_commit(zilog, itx, lwb);
34dc7c2f
BB
1556 }
1557 DTRACE_PROBE1(zil__cw2, zilog_t *, zilog);
34dc7c2f
BB
1558
1559 /* write the last block out */
1560 if (lwb != NULL && lwb->lwb_zio != NULL)
1561 lwb = zil_lwb_write_start(zilog, lwb);
1562
34dc7c2f
BB
1563 zilog->zl_cur_used = 0;
1564
1565 /*
1566 * Wait if necessary for the log blocks to be on stable storage.
1567 */
1568 if (zilog->zl_root_zio) {
428870ff 1569 error = zio_wait(zilog->zl_root_zio);
b128c09f 1570 zilog->zl_root_zio = NULL;
34dc7c2f
BB
1571 zil_flush_vdevs(zilog);
1572 }
1573
428870ff 1574 if (error || lwb == NULL)
34dc7c2f 1575 txg_wait_synced(zilog->zl_dmu_pool, 0);
34dc7c2f 1576
119a394a
ED
1577 while ((itx = list_head(&zilog->zl_itx_commit_list))) {
1578 txg = itx->itx_lr.lrc_txg;
1579 ASSERT(txg);
1580
1581 if (itx->itx_callback != NULL)
1582 itx->itx_callback(itx->itx_callback_data);
1583 list_remove(&zilog->zl_itx_commit_list, itx);
1584 kmem_free(itx, offsetof(itx_t, itx_lr)
1585 + itx->itx_lr.lrc_reclen);
1586 }
1587
34dc7c2f 1588 mutex_enter(&zilog->zl_lock);
428870ff
BB
1589
1590 /*
1591 * Remember the highest committed log sequence number for ztest.
1592 * We only update this value when all the log writes succeeded,
1593 * because ztest wants to ASSERT that it got the whole log chain.
1594 */
1595 if (error == 0 && lwb != NULL)
1596 zilog->zl_commit_lr_seq = zilog->zl_lr_seq;
34dc7c2f
BB
1597}
1598
1599/*
572e2857 1600 * Commit zfs transactions to stable storage.
34dc7c2f 1601 * If foid is 0 push out all transactions, otherwise push only those
572e2857
BB
1602 * for that object or might reference that object.
1603 *
1604 * itxs are committed in batches. In a heavily stressed zil there will be
1605 * a commit writer thread who is writing out a bunch of itxs to the log
1606 * for a set of committing threads (cthreads) in the same batch as the writer.
1607 * Those cthreads are all waiting on the same cv for that batch.
1608 *
1609 * There will also be a different and growing batch of threads that are
1610 * waiting to commit (qthreads). When the committing batch completes
1611 * a transition occurs such that the cthreads exit and the qthreads become
1612 * cthreads. One of the new cthreads becomes the writer thread for the
1613 * batch. Any new threads arriving become new qthreads.
1614 *
1615 * Only 2 condition variables are needed and there's no transition
1616 * between the two cvs needed. They just flip-flop between qthreads
1617 * and cthreads.
1618 *
1619 * Using this scheme we can efficiently wakeup up only those threads
1620 * that have been committed.
34dc7c2f
BB
1621 */
1622void
572e2857 1623zil_commit(zilog_t *zilog, uint64_t foid)
34dc7c2f 1624{
572e2857 1625 uint64_t mybatch;
34dc7c2f 1626
572e2857
BB
1627 if (zilog->zl_sync == ZFS_SYNC_DISABLED)
1628 return;
34dc7c2f 1629
b6ad9671
ED
1630 ZIL_STAT_BUMP(zil_commit_count);
1631
572e2857
BB
1632 /* move the async itxs for the foid to the sync queues */
1633 zil_async_to_sync(zilog, foid);
34dc7c2f 1634
572e2857
BB
1635 mutex_enter(&zilog->zl_lock);
1636 mybatch = zilog->zl_next_batch;
34dc7c2f 1637 while (zilog->zl_writer) {
572e2857
BB
1638 cv_wait(&zilog->zl_cv_batch[mybatch & 1], &zilog->zl_lock);
1639 if (mybatch <= zilog->zl_com_batch) {
34dc7c2f
BB
1640 mutex_exit(&zilog->zl_lock);
1641 return;
1642 }
1643 }
428870ff 1644
572e2857
BB
1645 zilog->zl_next_batch++;
1646 zilog->zl_writer = B_TRUE;
b6ad9671 1647 ZIL_STAT_BUMP(zil_commit_writer_count);
572e2857
BB
1648 zil_commit_writer(zilog);
1649 zilog->zl_com_batch = mybatch;
1650 zilog->zl_writer = B_FALSE;
428870ff 1651
572e2857
BB
1652 /* wake up one thread to become the next writer */
1653 cv_signal(&zilog->zl_cv_batch[(mybatch+1) & 1]);
428870ff 1654
572e2857
BB
1655 /* wake up all threads waiting for this batch to be committed */
1656 cv_broadcast(&zilog->zl_cv_batch[mybatch & 1]);
8c0712fd
BB
1657
1658 mutex_exit(&zilog->zl_lock);
428870ff
BB
1659}
1660
34dc7c2f
BB
1661/*
1662 * Called in syncing context to free committed log blocks and update log header.
1663 */
1664void
1665zil_sync(zilog_t *zilog, dmu_tx_t *tx)
1666{
1667 zil_header_t *zh = zil_header_in_syncing_context(zilog);
1668 uint64_t txg = dmu_tx_get_txg(tx);
1669 spa_t *spa = zilog->zl_spa;
428870ff 1670 uint64_t *replayed_seq = &zilog->zl_replayed_seq[txg & TXG_MASK];
34dc7c2f
BB
1671 lwb_t *lwb;
1672
9babb374
BB
1673 /*
1674 * We don't zero out zl_destroy_txg, so make sure we don't try
1675 * to destroy it twice.
1676 */
1677 if (spa_sync_pass(spa) != 1)
1678 return;
1679
34dc7c2f
BB
1680 mutex_enter(&zilog->zl_lock);
1681
1682 ASSERT(zilog->zl_stop_sync == 0);
1683
428870ff
BB
1684 if (*replayed_seq != 0) {
1685 ASSERT(zh->zh_replay_seq < *replayed_seq);
1686 zh->zh_replay_seq = *replayed_seq;
1687 *replayed_seq = 0;
1688 }
34dc7c2f
BB
1689
1690 if (zilog->zl_destroy_txg == txg) {
1691 blkptr_t blk = zh->zh_log;
1692
1693 ASSERT(list_head(&zilog->zl_lwb_list) == NULL);
34dc7c2f
BB
1694
1695 bzero(zh, sizeof (zil_header_t));
fb5f0bc8 1696 bzero(zilog->zl_replayed_seq, sizeof (zilog->zl_replayed_seq));
34dc7c2f
BB
1697
1698 if (zilog->zl_keep_first) {
1699 /*
1700 * If this block was part of log chain that couldn't
1701 * be claimed because a device was missing during
1702 * zil_claim(), but that device later returns,
1703 * then this block could erroneously appear valid.
1704 * To guard against this, assign a new GUID to the new
1705 * log chain so it doesn't matter what blk points to.
1706 */
1707 zil_init_log_chain(zilog, &blk);
1708 zh->zh_log = blk;
1709 }
1710 }
1711
9babb374 1712 while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
34dc7c2f
BB
1713 zh->zh_log = lwb->lwb_blk;
1714 if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg)
1715 break;
920dd524
ED
1716
1717 ASSERT(lwb->lwb_zio == NULL);
1718
34dc7c2f 1719 list_remove(&zilog->zl_lwb_list, lwb);
428870ff 1720 zio_free_zil(spa, txg, &lwb->lwb_blk);
34dc7c2f
BB
1721 kmem_cache_free(zil_lwb_cache, lwb);
1722
1723 /*
1724 * If we don't have anything left in the lwb list then
1725 * we've had an allocation failure and we need to zero
1726 * out the zil_header blkptr so that we don't end
1727 * up freeing the same block twice.
1728 */
1729 if (list_head(&zilog->zl_lwb_list) == NULL)
1730 BP_ZERO(&zh->zh_log);
1731 }
920dd524
ED
1732
1733 /*
1734 * Remove fastwrite on any blocks that have been pre-allocated for
1735 * the next commit. This prevents fastwrite counter pollution by
1736 * unused, long-lived LWBs.
1737 */
1738 for (; lwb != NULL; lwb = list_next(&zilog->zl_lwb_list, lwb)) {
1739 if (lwb->lwb_fastwrite && !lwb->lwb_zio) {
1740 metaslab_fastwrite_unmark(zilog->zl_spa, &lwb->lwb_blk);
1741 lwb->lwb_fastwrite = 0;
1742 }
1743 }
1744
34dc7c2f
BB
1745 mutex_exit(&zilog->zl_lock);
1746}
1747
1748void
1749zil_init(void)
1750{
1751 zil_lwb_cache = kmem_cache_create("zil_lwb_cache",
1752 sizeof (struct lwb), 0, NULL, NULL, NULL, NULL, NULL, 0);
b6ad9671
ED
1753
1754 zil_ksp = kstat_create("zfs", 0, "zil", "misc",
d1d7e268 1755 KSTAT_TYPE_NAMED, sizeof (zil_stats) / sizeof (kstat_named_t),
b6ad9671
ED
1756 KSTAT_FLAG_VIRTUAL);
1757
1758 if (zil_ksp != NULL) {
1759 zil_ksp->ks_data = &zil_stats;
1760 kstat_install(zil_ksp);
1761 }
34dc7c2f
BB
1762}
1763
1764void
1765zil_fini(void)
1766{
1767 kmem_cache_destroy(zil_lwb_cache);
b6ad9671
ED
1768
1769 if (zil_ksp != NULL) {
1770 kstat_delete(zil_ksp);
1771 zil_ksp = NULL;
1772 }
34dc7c2f
BB
1773}
1774
428870ff
BB
1775void
1776zil_set_sync(zilog_t *zilog, uint64_t sync)
1777{
1778 zilog->zl_sync = sync;
1779}
1780
1781void
1782zil_set_logbias(zilog_t *zilog, uint64_t logbias)
1783{
1784 zilog->zl_logbias = logbias;
1785}
1786
34dc7c2f
BB
1787zilog_t *
1788zil_alloc(objset_t *os, zil_header_t *zh_phys)
1789{
1790 zilog_t *zilog;
d6320ddb 1791 int i;
34dc7c2f 1792
b8d06fca 1793 zilog = kmem_zalloc(sizeof (zilog_t), KM_PUSHPAGE);
34dc7c2f
BB
1794
1795 zilog->zl_header = zh_phys;
1796 zilog->zl_os = os;
1797 zilog->zl_spa = dmu_objset_spa(os);
1798 zilog->zl_dmu_pool = dmu_objset_pool(os);
1799 zilog->zl_destroy_txg = TXG_INITIAL - 1;
428870ff
BB
1800 zilog->zl_logbias = dmu_objset_logbias(os);
1801 zilog->zl_sync = dmu_objset_syncprop(os);
572e2857 1802 zilog->zl_next_batch = 1;
34dc7c2f
BB
1803
1804 mutex_init(&zilog->zl_lock, NULL, MUTEX_DEFAULT, NULL);
1805
d6320ddb 1806 for (i = 0; i < TXG_SIZE; i++) {
572e2857
BB
1807 mutex_init(&zilog->zl_itxg[i].itxg_lock, NULL,
1808 MUTEX_DEFAULT, NULL);
1809 }
34dc7c2f
BB
1810
1811 list_create(&zilog->zl_lwb_list, sizeof (lwb_t),
1812 offsetof(lwb_t, lwb_node));
1813
572e2857
BB
1814 list_create(&zilog->zl_itx_commit_list, sizeof (itx_t),
1815 offsetof(itx_t, itx_node));
1816
34dc7c2f
BB
1817 mutex_init(&zilog->zl_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
1818
1819 avl_create(&zilog->zl_vdev_tree, zil_vdev_compare,
1820 sizeof (zil_vdev_node_t), offsetof(zil_vdev_node_t, zv_node));
1821
1822 cv_init(&zilog->zl_cv_writer, NULL, CV_DEFAULT, NULL);
1823 cv_init(&zilog->zl_cv_suspend, NULL, CV_DEFAULT, NULL);
572e2857
BB
1824 cv_init(&zilog->zl_cv_batch[0], NULL, CV_DEFAULT, NULL);
1825 cv_init(&zilog->zl_cv_batch[1], NULL, CV_DEFAULT, NULL);
34dc7c2f
BB
1826
1827 return (zilog);
1828}
1829
1830void
1831zil_free(zilog_t *zilog)
1832{
d6320ddb 1833 int i;
34dc7c2f
BB
1834
1835 zilog->zl_stop_sync = 1;
1836
13fe0198
MA
1837 ASSERT0(zilog->zl_suspend);
1838 ASSERT0(zilog->zl_suspending);
1839
3e31d2b0 1840 ASSERT(list_is_empty(&zilog->zl_lwb_list));
34dc7c2f
BB
1841 list_destroy(&zilog->zl_lwb_list);
1842
1843 avl_destroy(&zilog->zl_vdev_tree);
1844 mutex_destroy(&zilog->zl_vdev_lock);
1845
572e2857
BB
1846 ASSERT(list_is_empty(&zilog->zl_itx_commit_list));
1847 list_destroy(&zilog->zl_itx_commit_list);
1848
d6320ddb 1849 for (i = 0; i < TXG_SIZE; i++) {
572e2857
BB
1850 /*
1851 * It's possible for an itx to be generated that doesn't dirty
1852 * a txg (e.g. ztest TX_TRUNCATE). So there's no zil_clean()
1853 * callback to remove the entry. We remove those here.
1854 *
1855 * Also free up the ziltest itxs.
1856 */
1857 if (zilog->zl_itxg[i].itxg_itxs)
1858 zil_itxg_clean(zilog->zl_itxg[i].itxg_itxs);
1859 mutex_destroy(&zilog->zl_itxg[i].itxg_lock);
1860 }
1861
34dc7c2f
BB
1862 mutex_destroy(&zilog->zl_lock);
1863
1864 cv_destroy(&zilog->zl_cv_writer);
1865 cv_destroy(&zilog->zl_cv_suspend);
572e2857
BB
1866 cv_destroy(&zilog->zl_cv_batch[0]);
1867 cv_destroy(&zilog->zl_cv_batch[1]);
34dc7c2f
BB
1868
1869 kmem_free(zilog, sizeof (zilog_t));
1870}
1871
34dc7c2f
BB
1872/*
1873 * Open an intent log.
1874 */
1875zilog_t *
1876zil_open(objset_t *os, zil_get_data_t *get_data)
1877{
1878 zilog_t *zilog = dmu_objset_zil(os);
1879
3e31d2b0
ES
1880 ASSERT(zilog->zl_clean_taskq == NULL);
1881 ASSERT(zilog->zl_get_data == NULL);
1882 ASSERT(list_is_empty(&zilog->zl_lwb_list));
1883
34dc7c2f
BB
1884 zilog->zl_get_data = get_data;
1885 zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri,
1886 2, 2, TASKQ_PREPOPULATE);
1887
1888 return (zilog);
1889}
1890
1891/*
1892 * Close an intent log.
1893 */
1894void
1895zil_close(zilog_t *zilog)
1896{
3e31d2b0 1897 lwb_t *lwb;
572e2857
BB
1898 uint64_t txg = 0;
1899
1900 zil_commit(zilog, 0); /* commit all itx */
1901
34dc7c2f 1902 /*
572e2857
BB
1903 * The lwb_max_txg for the stubby lwb will reflect the last activity
1904 * for the zil. After a txg_wait_synced() on the txg we know all the
1905 * callbacks have occurred that may clean the zil. Only then can we
1906 * destroy the zl_clean_taskq.
34dc7c2f 1907 */
572e2857 1908 mutex_enter(&zilog->zl_lock);
3e31d2b0
ES
1909 lwb = list_tail(&zilog->zl_lwb_list);
1910 if (lwb != NULL)
1911 txg = lwb->lwb_max_txg;
572e2857
BB
1912 mutex_exit(&zilog->zl_lock);
1913 if (txg)
34dc7c2f 1914 txg_wait_synced(zilog->zl_dmu_pool, txg);
29809a6c 1915 ASSERT(!zilog_is_dirty(zilog));
34dc7c2f
BB
1916
1917 taskq_destroy(zilog->zl_clean_taskq);
1918 zilog->zl_clean_taskq = NULL;
1919 zilog->zl_get_data = NULL;
3e31d2b0
ES
1920
1921 /*
1922 * We should have only one LWB left on the list; remove it now.
1923 */
1924 mutex_enter(&zilog->zl_lock);
1925 lwb = list_head(&zilog->zl_lwb_list);
1926 if (lwb != NULL) {
1927 ASSERT(lwb == list_tail(&zilog->zl_lwb_list));
920dd524
ED
1928 ASSERT(lwb->lwb_zio == NULL);
1929 if (lwb->lwb_fastwrite)
1930 metaslab_fastwrite_unmark(zilog->zl_spa, &lwb->lwb_blk);
3e31d2b0
ES
1931 list_remove(&zilog->zl_lwb_list, lwb);
1932 zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
1933 kmem_cache_free(zil_lwb_cache, lwb);
1934 }
1935 mutex_exit(&zilog->zl_lock);
34dc7c2f
BB
1936}
1937
13fe0198
MA
1938static char *suspend_tag = "zil suspending";
1939
34dc7c2f
BB
1940/*
1941 * Suspend an intent log. While in suspended mode, we still honor
1942 * synchronous semantics, but we rely on txg_wait_synced() to do it.
13fe0198
MA
1943 * On old version pools, we suspend the log briefly when taking a
1944 * snapshot so that it will have an empty intent log.
1945 *
1946 * Long holds are not really intended to be used the way we do here --
1947 * held for such a short time. A concurrent caller of dsl_dataset_long_held()
1948 * could fail. Therefore we take pains to only put a long hold if it is
1949 * actually necessary. Fortunately, it will only be necessary if the
1950 * objset is currently mounted (or the ZVOL equivalent). In that case it
1951 * will already have a long hold, so we are not really making things any worse.
1952 *
1953 * Ideally, we would locate the existing long-holder (i.e. the zfsvfs_t or
1954 * zvol_state_t), and use their mechanism to prevent their hold from being
1955 * dropped (e.g. VFS_HOLD()). However, that would be even more pain for
1956 * very little gain.
1957 *
1958 * if cookiep == NULL, this does both the suspend & resume.
1959 * Otherwise, it returns with the dataset "long held", and the cookie
1960 * should be passed into zil_resume().
34dc7c2f
BB
1961 */
1962int
13fe0198 1963zil_suspend(const char *osname, void **cookiep)
34dc7c2f 1964{
13fe0198
MA
1965 objset_t *os;
1966 zilog_t *zilog;
1967 const zil_header_t *zh;
1968 int error;
1969
1970 error = dmu_objset_hold(osname, suspend_tag, &os);
1971 if (error != 0)
1972 return (error);
1973 zilog = dmu_objset_zil(os);
34dc7c2f
BB
1974
1975 mutex_enter(&zilog->zl_lock);
13fe0198
MA
1976 zh = zilog->zl_header;
1977
9babb374 1978 if (zh->zh_flags & ZIL_REPLAY_NEEDED) { /* unplayed log */
34dc7c2f 1979 mutex_exit(&zilog->zl_lock);
13fe0198 1980 dmu_objset_rele(os, suspend_tag);
2e528b49 1981 return (SET_ERROR(EBUSY));
34dc7c2f 1982 }
13fe0198
MA
1983
1984 /*
1985 * Don't put a long hold in the cases where we can avoid it. This
1986 * is when there is no cookie so we are doing a suspend & resume
1987 * (i.e. called from zil_vdev_offline()), and there's nothing to do
1988 * for the suspend because it's already suspended, or there's no ZIL.
1989 */
1990 if (cookiep == NULL && !zilog->zl_suspending &&
1991 (zilog->zl_suspend > 0 || BP_IS_HOLE(&zh->zh_log))) {
1992 mutex_exit(&zilog->zl_lock);
1993 dmu_objset_rele(os, suspend_tag);
1994 return (0);
1995 }
1996
1997 dsl_dataset_long_hold(dmu_objset_ds(os), suspend_tag);
1998 dsl_pool_rele(dmu_objset_pool(os), suspend_tag);
1999
2000 zilog->zl_suspend++;
2001
2002 if (zilog->zl_suspend > 1) {
34dc7c2f 2003 /*
13fe0198 2004 * Someone else is already suspending it.
34dc7c2f
BB
2005 * Just wait for them to finish.
2006 */
13fe0198 2007
34dc7c2f
BB
2008 while (zilog->zl_suspending)
2009 cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock);
34dc7c2f 2010 mutex_exit(&zilog->zl_lock);
13fe0198
MA
2011
2012 if (cookiep == NULL)
2013 zil_resume(os);
2014 else
2015 *cookiep = os;
2016 return (0);
2017 }
2018
2019 /*
2020 * If there is no pointer to an on-disk block, this ZIL must not
2021 * be active (e.g. filesystem not mounted), so there's nothing
2022 * to clean up.
2023 */
2024 if (BP_IS_HOLE(&zh->zh_log)) {
2025 ASSERT(cookiep != NULL); /* fast path already handled */
2026
2027 *cookiep = os;
2028 mutex_exit(&zilog->zl_lock);
34dc7c2f
BB
2029 return (0);
2030 }
13fe0198 2031
34dc7c2f
BB
2032 zilog->zl_suspending = B_TRUE;
2033 mutex_exit(&zilog->zl_lock);
2034
572e2857 2035 zil_commit(zilog, 0);
34dc7c2f
BB
2036
2037 zil_destroy(zilog, B_FALSE);
2038
2039 mutex_enter(&zilog->zl_lock);
2040 zilog->zl_suspending = B_FALSE;
2041 cv_broadcast(&zilog->zl_cv_suspend);
2042 mutex_exit(&zilog->zl_lock);
2043
13fe0198
MA
2044 if (cookiep == NULL)
2045 zil_resume(os);
2046 else
2047 *cookiep = os;
34dc7c2f
BB
2048 return (0);
2049}
2050
2051void
13fe0198 2052zil_resume(void *cookie)
34dc7c2f 2053{
13fe0198
MA
2054 objset_t *os = cookie;
2055 zilog_t *zilog = dmu_objset_zil(os);
2056
34dc7c2f
BB
2057 mutex_enter(&zilog->zl_lock);
2058 ASSERT(zilog->zl_suspend != 0);
2059 zilog->zl_suspend--;
2060 mutex_exit(&zilog->zl_lock);
13fe0198
MA
2061 dsl_dataset_long_rele(dmu_objset_ds(os), suspend_tag);
2062 dsl_dataset_rele(dmu_objset_ds(os), suspend_tag);
34dc7c2f
BB
2063}
2064
2065typedef struct zil_replay_arg {
b01615d5 2066 zil_replay_func_t *zr_replay;
34dc7c2f 2067 void *zr_arg;
34dc7c2f 2068 boolean_t zr_byteswap;
428870ff 2069 char *zr_lr;
34dc7c2f
BB
2070} zil_replay_arg_t;
2071
428870ff
BB
2072static int
2073zil_replay_error(zilog_t *zilog, lr_t *lr, int error)
2074{
2075 char name[MAXNAMELEN];
2076
2077 zilog->zl_replaying_seq--; /* didn't actually replay this one */
2078
2079 dmu_objset_name(zilog->zl_os, name);
2080
2081 cmn_err(CE_WARN, "ZFS replay transaction error %d, "
2082 "dataset %s, seq 0x%llx, txtype %llu %s\n", error, name,
2083 (u_longlong_t)lr->lrc_seq,
2084 (u_longlong_t)(lr->lrc_txtype & ~TX_CI),
2085 (lr->lrc_txtype & TX_CI) ? "CI" : "");
2086
2087 return (error);
2088}
2089
2090static int
34dc7c2f
BB
2091zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg)
2092{
2093 zil_replay_arg_t *zr = zra;
2094 const zil_header_t *zh = zilog->zl_header;
2095 uint64_t reclen = lr->lrc_reclen;
2096 uint64_t txtype = lr->lrc_txtype;
428870ff 2097 int error = 0;
34dc7c2f 2098
428870ff 2099 zilog->zl_replaying_seq = lr->lrc_seq;
34dc7c2f
BB
2100
2101 if (lr->lrc_seq <= zh->zh_replay_seq) /* already replayed */
428870ff
BB
2102 return (0);
2103
2104 if (lr->lrc_txg < claim_txg) /* already committed */
2105 return (0);
34dc7c2f
BB
2106
2107 /* Strip case-insensitive bit, still present in log record */
2108 txtype &= ~TX_CI;
2109
428870ff
BB
2110 if (txtype == 0 || txtype >= TX_MAX_TYPE)
2111 return (zil_replay_error(zilog, lr, EINVAL));
2112
2113 /*
2114 * If this record type can be logged out of order, the object
2115 * (lr_foid) may no longer exist. That's legitimate, not an error.
2116 */
2117 if (TX_OOO(txtype)) {
2118 error = dmu_object_info(zilog->zl_os,
2119 ((lr_ooo_t *)lr)->lr_foid, NULL);
2120 if (error == ENOENT || error == EEXIST)
2121 return (0);
fb5f0bc8
BB
2122 }
2123
34dc7c2f
BB
2124 /*
2125 * Make a copy of the data so we can revise and extend it.
2126 */
428870ff
BB
2127 bcopy(lr, zr->zr_lr, reclen);
2128
2129 /*
2130 * If this is a TX_WRITE with a blkptr, suck in the data.
2131 */
2132 if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) {
2133 error = zil_read_log_data(zilog, (lr_write_t *)lr,
2134 zr->zr_lr + reclen);
13fe0198 2135 if (error != 0)
428870ff
BB
2136 return (zil_replay_error(zilog, lr, error));
2137 }
34dc7c2f
BB
2138
2139 /*
2140 * The log block containing this lr may have been byteswapped
2141 * so that we can easily examine common fields like lrc_txtype.
428870ff 2142 * However, the log is a mix of different record types, and only the
34dc7c2f
BB
2143 * replay vectors know how to byteswap their records. Therefore, if
2144 * the lr was byteswapped, undo it before invoking the replay vector.
2145 */
2146 if (zr->zr_byteswap)
428870ff 2147 byteswap_uint64_array(zr->zr_lr, reclen);
34dc7c2f
BB
2148
2149 /*
2150 * We must now do two things atomically: replay this log record,
fb5f0bc8
BB
2151 * and update the log header sequence number to reflect the fact that
2152 * we did so. At the end of each replay function the sequence number
2153 * is updated if we are in replay mode.
34dc7c2f 2154 */
428870ff 2155 error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lr, zr->zr_byteswap);
13fe0198 2156 if (error != 0) {
34dc7c2f
BB
2157 /*
2158 * The DMU's dnode layer doesn't see removes until the txg
2159 * commits, so a subsequent claim can spuriously fail with
fb5f0bc8 2160 * EEXIST. So if we receive any error we try syncing out
428870ff
BB
2161 * any removes then retry the transaction. Note that we
2162 * specify B_FALSE for byteswap now, so we don't do it twice.
34dc7c2f 2163 */
428870ff
BB
2164 txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0);
2165 error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lr, B_FALSE);
13fe0198 2166 if (error != 0)
428870ff 2167 return (zil_replay_error(zilog, lr, error));
34dc7c2f 2168 }
428870ff 2169 return (0);
34dc7c2f
BB
2170}
2171
2172/* ARGSUSED */
428870ff 2173static int
34dc7c2f
BB
2174zil_incr_blks(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
2175{
2176 zilog->zl_replay_blks++;
428870ff
BB
2177
2178 return (0);
34dc7c2f
BB
2179}
2180
2181/*
2182 * If this dataset has a non-empty intent log, replay it and destroy it.
2183 */
2184void
b01615d5 2185zil_replay(objset_t *os, void *arg, zil_replay_func_t replay_func[TX_MAX_TYPE])
34dc7c2f
BB
2186{
2187 zilog_t *zilog = dmu_objset_zil(os);
2188 const zil_header_t *zh = zilog->zl_header;
2189 zil_replay_arg_t zr;
2190
9babb374 2191 if ((zh->zh_flags & ZIL_REPLAY_NEEDED) == 0) {
34dc7c2f
BB
2192 zil_destroy(zilog, B_TRUE);
2193 return;
2194 }
2195
34dc7c2f
BB
2196 zr.zr_replay = replay_func;
2197 zr.zr_arg = arg;
34dc7c2f 2198 zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log);
b8d06fca 2199 zr.zr_lr = vmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_PUSHPAGE);
34dc7c2f
BB
2200
2201 /*
2202 * Wait for in-progress removes to sync before starting replay.
2203 */
2204 txg_wait_synced(zilog->zl_dmu_pool, 0);
2205
fb5f0bc8 2206 zilog->zl_replay = B_TRUE;
428870ff 2207 zilog->zl_replay_time = ddi_get_lbolt();
34dc7c2f
BB
2208 ASSERT(zilog->zl_replay_blks == 0);
2209 (void) zil_parse(zilog, zil_incr_blks, zil_replay_log_record, &zr,
2210 zh->zh_claim_txg);
00b46022 2211 vmem_free(zr.zr_lr, 2 * SPA_MAXBLOCKSIZE);
34dc7c2f
BB
2212
2213 zil_destroy(zilog, B_FALSE);
2214 txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
fb5f0bc8 2215 zilog->zl_replay = B_FALSE;
34dc7c2f
BB
2216}
2217
428870ff
BB
2218boolean_t
2219zil_replaying(zilog_t *zilog, dmu_tx_t *tx)
34dc7c2f 2220{
428870ff
BB
2221 if (zilog->zl_sync == ZFS_SYNC_DISABLED)
2222 return (B_TRUE);
34dc7c2f 2223
428870ff
BB
2224 if (zilog->zl_replay) {
2225 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
2226 zilog->zl_replayed_seq[dmu_tx_get_txg(tx) & TXG_MASK] =
2227 zilog->zl_replaying_seq;
2228 return (B_TRUE);
34dc7c2f
BB
2229 }
2230
428870ff 2231 return (B_FALSE);
34dc7c2f 2232}
9babb374
BB
2233
2234/* ARGSUSED */
2235int
428870ff 2236zil_vdev_offline(const char *osname, void *arg)
9babb374 2237{
9babb374
BB
2238 int error;
2239
13fe0198
MA
2240 error = zil_suspend(osname, NULL);
2241 if (error != 0)
2e528b49 2242 return (SET_ERROR(EEXIST));
13fe0198 2243 return (0);
9babb374 2244}
c409e464
BB
2245
2246#if defined(_KERNEL) && defined(HAVE_SPL)
2247module_param(zil_replay_disable, int, 0644);
2248MODULE_PARM_DESC(zil_replay_disable, "Disable intent logging replay");
2249
2250module_param(zfs_nocacheflush, int, 0644);
2251MODULE_PARM_DESC(zfs_nocacheflush, "Disable cache flushes");
ee191e80
ED
2252
2253module_param(zil_slog_limit, ulong, 0644);
2254MODULE_PARM_DESC(zil_slog_limit, "Max commit bytes to separate log device");
c409e464 2255#endif