]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/zil.c
Add zpool_influxdb command
[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.
492f64e9 23 * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
55922e73 24 * Copyright (c) 2014 Integros [integros.com]
2ffd89fc 25 * Copyright (c) 2018 Datto Inc.
34dc7c2f
BB
26 */
27
428870ff
BB
28/* Portions Copyright 2010 Robert Milkowski */
29
34dc7c2f
BB
30#include <sys/zfs_context.h>
31#include <sys/spa.h>
d2734cce 32#include <sys/spa_impl.h>
34dc7c2f
BB
33#include <sys/dmu.h>
34#include <sys/zap.h>
35#include <sys/arc.h>
36#include <sys/stat.h>
34dc7c2f
BB
37#include <sys/zil.h>
38#include <sys/zil_impl.h>
39#include <sys/dsl_dataset.h>
572e2857 40#include <sys/vdev_impl.h>
34dc7c2f 41#include <sys/dmu_tx.h>
428870ff 42#include <sys/dsl_pool.h>
920dd524 43#include <sys/metaslab.h>
e5d1c27e 44#include <sys/trace_zfs.h>
a6255b7f 45#include <sys/abd.h>
34dc7c2f
BB
46
47/*
1ce23dca
PS
48 * The ZFS Intent Log (ZIL) saves "transaction records" (itxs) of system
49 * calls that change the file system. Each itx has enough information to
50 * be able to replay them after a system crash, power loss, or
51 * equivalent failure mode. These are stored in memory until either:
34dc7c2f 52 *
1ce23dca
PS
53 * 1. they are committed to the pool by the DMU transaction group
54 * (txg), at which point they can be discarded; or
55 * 2. they are committed to the on-disk ZIL for the dataset being
56 * modified (e.g. due to an fsync, O_DSYNC, or other synchronous
57 * requirement).
34dc7c2f 58 *
1ce23dca
PS
59 * In the event of a crash or power loss, the itxs contained by each
60 * dataset's on-disk ZIL will be replayed when that dataset is first
e1cfd73f 61 * instantiated (e.g. if the dataset is a normal filesystem, when it is
1ce23dca 62 * first mounted).
34dc7c2f 63 *
1ce23dca
PS
64 * As hinted at above, there is one ZIL per dataset (both the in-memory
65 * representation, and the on-disk representation). The on-disk format
66 * consists of 3 parts:
67 *
68 * - a single, per-dataset, ZIL header; which points to a chain of
69 * - zero or more ZIL blocks; each of which contains
70 * - zero or more ZIL records
71 *
72 * A ZIL record holds the information necessary to replay a single
73 * system call transaction. A ZIL block can hold many ZIL records, and
74 * the blocks are chained together, similarly to a singly linked list.
75 *
76 * Each ZIL block contains a block pointer (blkptr_t) to the next ZIL
77 * block in the chain, and the ZIL header points to the first block in
78 * the chain.
79 *
80 * Note, there is not a fixed place in the pool to hold these ZIL
81 * blocks; they are dynamically allocated and freed as needed from the
82 * blocks available on the pool, though they can be preferentially
83 * allocated from a dedicated "log" vdev.
34dc7c2f
BB
84 */
85
1ce23dca
PS
86/*
87 * This controls the amount of time that a ZIL block (lwb) will remain
88 * "open" when it isn't "full", and it has a thread waiting for it to be
89 * committed to stable storage. Please refer to the zil_commit_waiter()
90 * function (and the comments within it) for more details.
91 */
92int zfs_commit_timeout_pct = 5;
93
b6ad9671
ED
94/*
95 * See zil.h for more information about these fields.
96 */
97zil_stats_t zil_stats = {
d1d7e268
MK
98 { "zil_commit_count", KSTAT_DATA_UINT64 },
99 { "zil_commit_writer_count", KSTAT_DATA_UINT64 },
100 { "zil_itx_count", KSTAT_DATA_UINT64 },
101 { "zil_itx_indirect_count", KSTAT_DATA_UINT64 },
102 { "zil_itx_indirect_bytes", KSTAT_DATA_UINT64 },
103 { "zil_itx_copied_count", KSTAT_DATA_UINT64 },
104 { "zil_itx_copied_bytes", KSTAT_DATA_UINT64 },
105 { "zil_itx_needcopy_count", KSTAT_DATA_UINT64 },
106 { "zil_itx_needcopy_bytes", KSTAT_DATA_UINT64 },
107 { "zil_itx_metaslab_normal_count", KSTAT_DATA_UINT64 },
108 { "zil_itx_metaslab_normal_bytes", KSTAT_DATA_UINT64 },
109 { "zil_itx_metaslab_slog_count", KSTAT_DATA_UINT64 },
110 { "zil_itx_metaslab_slog_bytes", KSTAT_DATA_UINT64 },
b6ad9671
ED
111};
112
113static kstat_t *zil_ksp;
114
34dc7c2f 115/*
d3cc8b15 116 * Disable intent logging replay. This global ZIL switch affects all pools.
34dc7c2f 117 */
d3cc8b15 118int zil_replay_disable = 0;
34dc7c2f
BB
119
120/*
53b1f5ea
PS
121 * Disable the DKIOCFLUSHWRITECACHE commands that are normally sent to
122 * the disk(s) by the ZIL after an LWB write has completed. Setting this
123 * will cause ZIL corruption on power loss if a volatile out-of-order
124 * write cache is enabled.
34dc7c2f 125 */
53b1f5ea 126int zil_nocacheflush = 0;
34dc7c2f 127
1b7c1e5c
GDN
128/*
129 * Limit SLOG write size per commit executed with synchronous priority.
130 * Any writes above that will be executed with lower (asynchronous) priority
131 * to limit potential SLOG device abuse by single active ZIL writer.
132 */
133unsigned long zil_slog_bulk = 768 * 1024;
134
34dc7c2f 135static kmem_cache_t *zil_lwb_cache;
1ce23dca 136static kmem_cache_t *zil_zcw_cache;
34dc7c2f 137
428870ff
BB
138#define LWB_EMPTY(lwb) ((BP_GET_LSIZE(&lwb->lwb_blk) - \
139 sizeof (zil_chain_t)) == (lwb->lwb_sz - lwb->lwb_nused))
140
34dc7c2f 141static int
428870ff 142zil_bp_compare(const void *x1, const void *x2)
34dc7c2f 143{
428870ff
BB
144 const dva_t *dva1 = &((zil_bp_node_t *)x1)->zn_dva;
145 const dva_t *dva2 = &((zil_bp_node_t *)x2)->zn_dva;
34dc7c2f 146
ca577779 147 int cmp = TREE_CMP(DVA_GET_VDEV(dva1), DVA_GET_VDEV(dva2));
ee36c709
GN
148 if (likely(cmp))
149 return (cmp);
34dc7c2f 150
ca577779 151 return (TREE_CMP(DVA_GET_OFFSET(dva1), DVA_GET_OFFSET(dva2)));
34dc7c2f
BB
152}
153
154static void
428870ff 155zil_bp_tree_init(zilog_t *zilog)
34dc7c2f 156{
428870ff
BB
157 avl_create(&zilog->zl_bp_tree, zil_bp_compare,
158 sizeof (zil_bp_node_t), offsetof(zil_bp_node_t, zn_node));
34dc7c2f
BB
159}
160
161static void
428870ff 162zil_bp_tree_fini(zilog_t *zilog)
34dc7c2f 163{
428870ff
BB
164 avl_tree_t *t = &zilog->zl_bp_tree;
165 zil_bp_node_t *zn;
34dc7c2f
BB
166 void *cookie = NULL;
167
168 while ((zn = avl_destroy_nodes(t, &cookie)) != NULL)
428870ff 169 kmem_free(zn, sizeof (zil_bp_node_t));
34dc7c2f
BB
170
171 avl_destroy(t);
172}
173
428870ff
BB
174int
175zil_bp_tree_add(zilog_t *zilog, const blkptr_t *bp)
34dc7c2f 176{
428870ff 177 avl_tree_t *t = &zilog->zl_bp_tree;
9b67f605 178 const dva_t *dva;
428870ff 179 zil_bp_node_t *zn;
34dc7c2f
BB
180 avl_index_t where;
181
9b67f605
MA
182 if (BP_IS_EMBEDDED(bp))
183 return (0);
184
185 dva = BP_IDENTITY(bp);
186
34dc7c2f 187 if (avl_find(t, dva, &where) != NULL)
2e528b49 188 return (SET_ERROR(EEXIST));
34dc7c2f 189
79c76d5b 190 zn = kmem_alloc(sizeof (zil_bp_node_t), KM_SLEEP);
34dc7c2f
BB
191 zn->zn_dva = *dva;
192 avl_insert(t, zn, where);
193
194 return (0);
195}
196
197static zil_header_t *
198zil_header_in_syncing_context(zilog_t *zilog)
199{
200 return ((zil_header_t *)zilog->zl_header);
201}
202
203static void
204zil_init_log_chain(zilog_t *zilog, blkptr_t *bp)
205{
206 zio_cksum_t *zc = &bp->blk_cksum;
207
208 zc->zc_word[ZIL_ZC_GUID_0] = spa_get_random(-1ULL);
209 zc->zc_word[ZIL_ZC_GUID_1] = spa_get_random(-1ULL);
210 zc->zc_word[ZIL_ZC_OBJSET] = dmu_objset_id(zilog->zl_os);
211 zc->zc_word[ZIL_ZC_SEQ] = 1ULL;
212}
213
214/*
428870ff 215 * Read a log block and make sure it's valid.
34dc7c2f
BB
216 */
217static int
b5256303
TC
218zil_read_log_block(zilog_t *zilog, boolean_t decrypt, const blkptr_t *bp,
219 blkptr_t *nbp, void *dst, char **end)
34dc7c2f 220{
428870ff 221 enum zio_flag zio_flags = ZIO_FLAG_CANFAIL;
2a432414 222 arc_flags_t aflags = ARC_FLAG_WAIT;
428870ff 223 arc_buf_t *abuf = NULL;
5dbd68a3 224 zbookmark_phys_t zb;
34dc7c2f
BB
225 int error;
226
428870ff
BB
227 if (zilog->zl_header->zh_claim_txg == 0)
228 zio_flags |= ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB;
34dc7c2f 229
428870ff
BB
230 if (!(zilog->zl_header->zh_flags & ZIL_CLAIM_LR_SEQ_VALID))
231 zio_flags |= ZIO_FLAG_SPECULATIVE;
34dc7c2f 232
b5256303
TC
233 if (!decrypt)
234 zio_flags |= ZIO_FLAG_RAW;
235
428870ff
BB
236 SET_BOOKMARK(&zb, bp->blk_cksum.zc_word[ZIL_ZC_OBJSET],
237 ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, bp->blk_cksum.zc_word[ZIL_ZC_SEQ]);
238
b5256303
TC
239 error = arc_read(NULL, zilog->zl_spa, bp, arc_getbuf_func,
240 &abuf, ZIO_PRIORITY_SYNC_READ, zio_flags, &aflags, &zb);
34dc7c2f
BB
241
242 if (error == 0) {
34dc7c2f
BB
243 zio_cksum_t cksum = bp->blk_cksum;
244
245 /*
b128c09f
BB
246 * Validate the checksummed log block.
247 *
34dc7c2f
BB
248 * Sequence numbers should be... sequential. The checksum
249 * verifier for the next block should be bp's checksum plus 1.
b128c09f
BB
250 *
251 * Also check the log chain linkage and size used.
34dc7c2f
BB
252 */
253 cksum.zc_word[ZIL_ZC_SEQ]++;
254
428870ff
BB
255 if (BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_ZILOG2) {
256 zil_chain_t *zilc = abuf->b_data;
257 char *lr = (char *)(zilc + 1);
258 uint64_t len = zilc->zc_nused - sizeof (zil_chain_t);
34dc7c2f 259
428870ff
BB
260 if (bcmp(&cksum, &zilc->zc_next_blk.blk_cksum,
261 sizeof (cksum)) || BP_IS_HOLE(&zilc->zc_next_blk)) {
2e528b49 262 error = SET_ERROR(ECKSUM);
428870ff 263 } else {
f1512ee6 264 ASSERT3U(len, <=, SPA_OLD_MAXBLOCKSIZE);
428870ff
BB
265 bcopy(lr, dst, len);
266 *end = (char *)dst + len;
267 *nbp = zilc->zc_next_blk;
268 }
269 } else {
270 char *lr = abuf->b_data;
271 uint64_t size = BP_GET_LSIZE(bp);
272 zil_chain_t *zilc = (zil_chain_t *)(lr + size) - 1;
273
274 if (bcmp(&cksum, &zilc->zc_next_blk.blk_cksum,
275 sizeof (cksum)) || BP_IS_HOLE(&zilc->zc_next_blk) ||
276 (zilc->zc_nused > (size - sizeof (*zilc)))) {
2e528b49 277 error = SET_ERROR(ECKSUM);
428870ff 278 } else {
f1512ee6
MA
279 ASSERT3U(zilc->zc_nused, <=,
280 SPA_OLD_MAXBLOCKSIZE);
428870ff
BB
281 bcopy(lr, dst, zilc->zc_nused);
282 *end = (char *)dst + zilc->zc_nused;
283 *nbp = zilc->zc_next_blk;
284 }
34dc7c2f 285 }
428870ff 286
d3c2ae1c 287 arc_buf_destroy(abuf, &abuf);
428870ff
BB
288 }
289
290 return (error);
291}
292
293/*
294 * Read a TX_WRITE log data block.
295 */
296static int
297zil_read_log_data(zilog_t *zilog, const lr_write_t *lr, void *wbuf)
298{
299 enum zio_flag zio_flags = ZIO_FLAG_CANFAIL;
300 const blkptr_t *bp = &lr->lr_blkptr;
2a432414 301 arc_flags_t aflags = ARC_FLAG_WAIT;
428870ff 302 arc_buf_t *abuf = NULL;
5dbd68a3 303 zbookmark_phys_t zb;
428870ff
BB
304 int error;
305
306 if (BP_IS_HOLE(bp)) {
307 if (wbuf != NULL)
308 bzero(wbuf, MAX(BP_GET_LSIZE(bp), lr->lr_length));
309 return (0);
34dc7c2f
BB
310 }
311
428870ff
BB
312 if (zilog->zl_header->zh_claim_txg == 0)
313 zio_flags |= ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB;
314
b5256303
TC
315 /*
316 * If we are not using the resulting data, we are just checking that
317 * it hasn't been corrupted so we don't need to waste CPU time
318 * decompressing and decrypting it.
319 */
320 if (wbuf == NULL)
321 zio_flags |= ZIO_FLAG_RAW;
322
428870ff
BB
323 SET_BOOKMARK(&zb, dmu_objset_id(zilog->zl_os), lr->lr_foid,
324 ZB_ZIL_LEVEL, lr->lr_offset / BP_GET_LSIZE(bp));
325
294f6806 326 error = arc_read(NULL, zilog->zl_spa, bp, arc_getbuf_func, &abuf,
428870ff
BB
327 ZIO_PRIORITY_SYNC_READ, zio_flags, &aflags, &zb);
328
329 if (error == 0) {
330 if (wbuf != NULL)
331 bcopy(abuf->b_data, wbuf, arc_buf_size(abuf));
d3c2ae1c 332 arc_buf_destroy(abuf, &abuf);
428870ff 333 }
34dc7c2f
BB
334
335 return (error);
336}
337
338/*
339 * Parse the intent log, and call parse_func for each valid record within.
34dc7c2f 340 */
428870ff 341int
34dc7c2f 342zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func,
b5256303
TC
343 zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg,
344 boolean_t decrypt)
34dc7c2f
BB
345{
346 const zil_header_t *zh = zilog->zl_header;
428870ff
BB
347 boolean_t claimed = !!zh->zh_claim_txg;
348 uint64_t claim_blk_seq = claimed ? zh->zh_claim_blk_seq : UINT64_MAX;
349 uint64_t claim_lr_seq = claimed ? zh->zh_claim_lr_seq : UINT64_MAX;
350 uint64_t max_blk_seq = 0;
351 uint64_t max_lr_seq = 0;
352 uint64_t blk_count = 0;
353 uint64_t lr_count = 0;
354 blkptr_t blk, next_blk;
34dc7c2f 355 char *lrbuf, *lrp;
428870ff 356 int error = 0;
34dc7c2f 357
d1d7e268 358 bzero(&next_blk, sizeof (blkptr_t));
d4ed6673 359
428870ff
BB
360 /*
361 * Old logs didn't record the maximum zh_claim_lr_seq.
362 */
363 if (!(zh->zh_flags & ZIL_CLAIM_LR_SEQ_VALID))
364 claim_lr_seq = UINT64_MAX;
34dc7c2f
BB
365
366 /*
367 * Starting at the block pointed to by zh_log we read the log chain.
368 * For each block in the chain we strongly check that block to
369 * ensure its validity. We stop when an invalid block is found.
370 * For each block pointer in the chain we call parse_blk_func().
371 * For each record in each valid block we call parse_lr_func().
372 * If the log has been claimed, stop if we encounter a sequence
373 * number greater than the highest claimed sequence number.
374 */
f1512ee6 375 lrbuf = zio_buf_alloc(SPA_OLD_MAXBLOCKSIZE);
428870ff 376 zil_bp_tree_init(zilog);
34dc7c2f 377
428870ff
BB
378 for (blk = zh->zh_log; !BP_IS_HOLE(&blk); blk = next_blk) {
379 uint64_t blk_seq = blk.blk_cksum.zc_word[ZIL_ZC_SEQ];
380 int reclen;
d4ed6673 381 char *end = NULL;
34dc7c2f 382
428870ff
BB
383 if (blk_seq > claim_blk_seq)
384 break;
b5256303
TC
385
386 error = parse_blk_func(zilog, &blk, arg, txg);
387 if (error != 0)
428870ff
BB
388 break;
389 ASSERT3U(max_blk_seq, <, blk_seq);
390 max_blk_seq = blk_seq;
391 blk_count++;
34dc7c2f 392
428870ff
BB
393 if (max_lr_seq == claim_lr_seq && max_blk_seq == claim_blk_seq)
394 break;
34dc7c2f 395
b5256303
TC
396 error = zil_read_log_block(zilog, decrypt, &blk, &next_blk,
397 lrbuf, &end);
13fe0198 398 if (error != 0)
34dc7c2f
BB
399 break;
400
428870ff 401 for (lrp = lrbuf; lrp < end; lrp += reclen) {
34dc7c2f
BB
402 lr_t *lr = (lr_t *)lrp;
403 reclen = lr->lrc_reclen;
404 ASSERT3U(reclen, >=, sizeof (lr_t));
428870ff
BB
405 if (lr->lrc_seq > claim_lr_seq)
406 goto done;
b5256303
TC
407
408 error = parse_lr_func(zilog, lr, arg, txg);
409 if (error != 0)
428870ff
BB
410 goto done;
411 ASSERT3U(max_lr_seq, <, lr->lrc_seq);
412 max_lr_seq = lr->lrc_seq;
413 lr_count++;
34dc7c2f 414 }
34dc7c2f 415 }
428870ff
BB
416done:
417 zilog->zl_parse_error = error;
418 zilog->zl_parse_blk_seq = max_blk_seq;
419 zilog->zl_parse_lr_seq = max_lr_seq;
420 zilog->zl_parse_blk_count = blk_count;
421 zilog->zl_parse_lr_count = lr_count;
422
423 ASSERT(!claimed || !(zh->zh_flags & ZIL_CLAIM_LR_SEQ_VALID) ||
b5256303
TC
424 (max_blk_seq == claim_blk_seq && max_lr_seq == claim_lr_seq) ||
425 (decrypt && error == EIO));
428870ff
BB
426
427 zil_bp_tree_fini(zilog);
f1512ee6 428 zio_buf_free(lrbuf, SPA_OLD_MAXBLOCKSIZE);
34dc7c2f 429
428870ff 430 return (error);
34dc7c2f
BB
431}
432
d2734cce
SD
433/* ARGSUSED */
434static int
435zil_clear_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t first_txg)
436{
437 ASSERT(!BP_IS_HOLE(bp));
438
439 /*
440 * As we call this function from the context of a rewind to a
441 * checkpoint, each ZIL block whose txg is later than the txg
442 * that we rewind to is invalid. Thus, we return -1 so
443 * zil_parse() doesn't attempt to read it.
444 */
445 if (bp->blk_birth >= first_txg)
446 return (-1);
447
448 if (zil_bp_tree_add(zilog, bp) != 0)
449 return (0);
450
451 zio_free(zilog->zl_spa, first_txg, bp);
452 return (0);
453}
454
455/* ARGSUSED */
456static int
457zil_noop_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg)
458{
459 return (0);
460}
461
428870ff 462static int
34dc7c2f
BB
463zil_claim_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t first_txg)
464{
34dc7c2f
BB
465 /*
466 * Claim log block if not already committed and not already claimed.
428870ff 467 * If tx == NULL, just verify that the block is claimable.
34dc7c2f 468 */
b0bc7a84
MG
469 if (BP_IS_HOLE(bp) || bp->blk_birth < first_txg ||
470 zil_bp_tree_add(zilog, bp) != 0)
428870ff
BB
471 return (0);
472
473 return (zio_wait(zio_claim(NULL, zilog->zl_spa,
474 tx == NULL ? 0 : first_txg, bp, spa_claim_notify, NULL,
475 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB)));
34dc7c2f
BB
476}
477
428870ff 478static int
34dc7c2f
BB
479zil_claim_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg)
480{
428870ff
BB
481 lr_write_t *lr = (lr_write_t *)lrc;
482 int error;
483
484 if (lrc->lrc_txtype != TX_WRITE)
485 return (0);
486
487 /*
488 * If the block is not readable, don't claim it. This can happen
489 * in normal operation when a log block is written to disk before
490 * some of the dmu_sync() blocks it points to. In this case, the
491 * transaction cannot have been committed to anyone (we would have
492 * waited for all writes to be stable first), so it is semantically
493 * correct to declare this the end of the log.
494 */
b5256303
TC
495 if (lr->lr_blkptr.blk_birth >= first_txg) {
496 error = zil_read_log_data(zilog, lr, NULL);
497 if (error != 0)
498 return (error);
499 }
500
428870ff 501 return (zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg));
34dc7c2f
BB
502}
503
504/* ARGSUSED */
428870ff 505static int
34dc7c2f
BB
506zil_free_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t claim_txg)
507{
d2734cce 508 zio_free(zilog->zl_spa, dmu_tx_get_txg(tx), bp);
428870ff
BB
509
510 return (0);
34dc7c2f
BB
511}
512
428870ff 513static int
34dc7c2f
BB
514zil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg)
515{
428870ff
BB
516 lr_write_t *lr = (lr_write_t *)lrc;
517 blkptr_t *bp = &lr->lr_blkptr;
518
34dc7c2f
BB
519 /*
520 * If we previously claimed it, we need to free it.
521 */
428870ff 522 if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE &&
b0bc7a84
MG
523 bp->blk_birth >= claim_txg && zil_bp_tree_add(zilog, bp) == 0 &&
524 !BP_IS_HOLE(bp))
428870ff
BB
525 zio_free(zilog->zl_spa, dmu_tx_get_txg(tx), bp);
526
527 return (0);
528}
529
1ce23dca
PS
530static int
531zil_lwb_vdev_compare(const void *x1, const void *x2)
532{
533 const uint64_t v1 = ((zil_vdev_node_t *)x1)->zv_vdev;
534 const uint64_t v2 = ((zil_vdev_node_t *)x2)->zv_vdev;
535
ca577779 536 return (TREE_CMP(v1, v2));
1ce23dca
PS
537}
538
428870ff 539static lwb_t *
1b7c1e5c
GDN
540zil_alloc_lwb(zilog_t *zilog, blkptr_t *bp, boolean_t slog, uint64_t txg,
541 boolean_t fastwrite)
428870ff
BB
542{
543 lwb_t *lwb;
544
79c76d5b 545 lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP);
428870ff
BB
546 lwb->lwb_zilog = zilog;
547 lwb->lwb_blk = *bp;
920dd524 548 lwb->lwb_fastwrite = fastwrite;
1b7c1e5c 549 lwb->lwb_slog = slog;
1ce23dca 550 lwb->lwb_state = LWB_STATE_CLOSED;
428870ff
BB
551 lwb->lwb_buf = zio_buf_alloc(BP_GET_LSIZE(bp));
552 lwb->lwb_max_txg = txg;
1ce23dca
PS
553 lwb->lwb_write_zio = NULL;
554 lwb->lwb_root_zio = NULL;
428870ff 555 lwb->lwb_tx = NULL;
1ce23dca 556 lwb->lwb_issued_timestamp = 0;
428870ff
BB
557 if (BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_ZILOG2) {
558 lwb->lwb_nused = sizeof (zil_chain_t);
559 lwb->lwb_sz = BP_GET_LSIZE(bp);
560 } else {
561 lwb->lwb_nused = 0;
562 lwb->lwb_sz = BP_GET_LSIZE(bp) - sizeof (zil_chain_t);
34dc7c2f 563 }
428870ff
BB
564
565 mutex_enter(&zilog->zl_lock);
566 list_insert_tail(&zilog->zl_lwb_list, lwb);
567 mutex_exit(&zilog->zl_lock);
568
1ce23dca
PS
569 ASSERT(!MUTEX_HELD(&lwb->lwb_vdev_lock));
570 ASSERT(avl_is_empty(&lwb->lwb_vdev_tree));
2fe61a7e
PS
571 VERIFY(list_is_empty(&lwb->lwb_waiters));
572 VERIFY(list_is_empty(&lwb->lwb_itxs));
1ce23dca 573
428870ff 574 return (lwb);
34dc7c2f
BB
575}
576
1ce23dca
PS
577static void
578zil_free_lwb(zilog_t *zilog, lwb_t *lwb)
579{
580 ASSERT(MUTEX_HELD(&zilog->zl_lock));
581 ASSERT(!MUTEX_HELD(&lwb->lwb_vdev_lock));
2fe61a7e
PS
582 VERIFY(list_is_empty(&lwb->lwb_waiters));
583 VERIFY(list_is_empty(&lwb->lwb_itxs));
1ce23dca 584 ASSERT(avl_is_empty(&lwb->lwb_vdev_tree));
1ce23dca
PS
585 ASSERT3P(lwb->lwb_write_zio, ==, NULL);
586 ASSERT3P(lwb->lwb_root_zio, ==, NULL);
2fe61a7e
PS
587 ASSERT3U(lwb->lwb_max_txg, <=, spa_syncing_txg(zilog->zl_spa));
588 ASSERT(lwb->lwb_state == LWB_STATE_CLOSED ||
900d09b2 589 lwb->lwb_state == LWB_STATE_FLUSH_DONE);
1ce23dca
PS
590
591 /*
592 * Clear the zilog's field to indicate this lwb is no longer
593 * valid, and prevent use-after-free errors.
594 */
595 if (zilog->zl_last_lwb_opened == lwb)
596 zilog->zl_last_lwb_opened = NULL;
597
598 kmem_cache_free(zil_lwb_cache, lwb);
599}
600
29809a6c
MA
601/*
602 * Called when we create in-memory log transactions so that we know
603 * to cleanup the itxs at the end of spa_sync().
604 */
65c7cc49 605static void
29809a6c
MA
606zilog_dirty(zilog_t *zilog, uint64_t txg)
607{
608 dsl_pool_t *dp = zilog->zl_dmu_pool;
609 dsl_dataset_t *ds = dmu_objset_ds(zilog->zl_os);
610
1ce23dca
PS
611 ASSERT(spa_writeable(zilog->zl_spa));
612
0c66c32d 613 if (ds->ds_is_snapshot)
29809a6c
MA
614 panic("dirtying snapshot!");
615
13fe0198 616 if (txg_list_add(&dp->dp_dirty_zilogs, zilog, txg)) {
29809a6c
MA
617 /* up the hold count until we can be written out */
618 dmu_buf_add_ref(ds->ds_dbuf, zilog);
1ce23dca
PS
619
620 zilog->zl_dirty_max_txg = MAX(txg, zilog->zl_dirty_max_txg);
29809a6c
MA
621 }
622}
623
55922e73
GW
624/*
625 * Determine if the zil is dirty in the specified txg. Callers wanting to
626 * ensure that the dirty state does not change must hold the itxg_lock for
627 * the specified txg. Holding the lock will ensure that the zil cannot be
628 * dirtied (zil_itx_assign) or cleaned (zil_clean) while we check its current
629 * state.
630 */
65c7cc49 631static boolean_t __maybe_unused
55922e73
GW
632zilog_is_dirty_in_txg(zilog_t *zilog, uint64_t txg)
633{
634 dsl_pool_t *dp = zilog->zl_dmu_pool;
635
636 if (txg_list_member(&dp->dp_dirty_zilogs, zilog, txg & TXG_MASK))
637 return (B_TRUE);
638 return (B_FALSE);
639}
640
641/*
642 * Determine if the zil is dirty. The zil is considered dirty if it has
643 * any pending itx records that have not been cleaned by zil_clean().
644 */
65c7cc49 645static boolean_t
29809a6c
MA
646zilog_is_dirty(zilog_t *zilog)
647{
648 dsl_pool_t *dp = zilog->zl_dmu_pool;
29809a6c 649
1c27024e 650 for (int t = 0; t < TXG_SIZE; t++) {
29809a6c
MA
651 if (txg_list_member(&dp->dp_dirty_zilogs, zilog, t))
652 return (B_TRUE);
653 }
654 return (B_FALSE);
655}
656
34dc7c2f
BB
657/*
658 * Create an on-disk intent log.
659 */
428870ff 660static lwb_t *
34dc7c2f
BB
661zil_create(zilog_t *zilog)
662{
663 const zil_header_t *zh = zilog->zl_header;
428870ff 664 lwb_t *lwb = NULL;
34dc7c2f
BB
665 uint64_t txg = 0;
666 dmu_tx_t *tx = NULL;
667 blkptr_t blk;
668 int error = 0;
920dd524 669 boolean_t fastwrite = FALSE;
1b7c1e5c 670 boolean_t slog = FALSE;
34dc7c2f
BB
671
672 /*
673 * Wait for any previous destroy to complete.
674 */
675 txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
676
677 ASSERT(zh->zh_claim_txg == 0);
678 ASSERT(zh->zh_replay_seq == 0);
679
680 blk = zh->zh_log;
681
682 /*
428870ff
BB
683 * Allocate an initial log block if:
684 * - there isn't one already
4e33ba4c 685 * - the existing block is the wrong endianness
34dc7c2f 686 */
fb5f0bc8 687 if (BP_IS_HOLE(&blk) || BP_SHOULD_BYTESWAP(&blk)) {
34dc7c2f 688 tx = dmu_tx_create(zilog->zl_os);
1ce23dca 689 VERIFY0(dmu_tx_assign(tx, TXG_WAIT));
34dc7c2f
BB
690 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
691 txg = dmu_tx_get_txg(tx);
692
fb5f0bc8 693 if (!BP_IS_HOLE(&blk)) {
d2734cce 694 zio_free(zilog->zl_spa, txg, &blk);
fb5f0bc8
BB
695 BP_ZERO(&blk);
696 }
697
b5256303 698 error = zio_alloc_zil(zilog->zl_spa, zilog->zl_os, txg, &blk,
1b7c1e5c 699 ZIL_MIN_BLKSZ, &slog);
920dd524 700 fastwrite = TRUE;
34dc7c2f
BB
701
702 if (error == 0)
703 zil_init_log_chain(zilog, &blk);
704 }
705
706 /*
1ce23dca 707 * Allocate a log write block (lwb) for the first log block.
34dc7c2f 708 */
428870ff 709 if (error == 0)
1b7c1e5c 710 lwb = zil_alloc_lwb(zilog, &blk, slog, txg, fastwrite);
34dc7c2f
BB
711
712 /*
713 * If we just allocated the first log block, commit our transaction
2fe61a7e 714 * and wait for zil_sync() to stuff the block pointer into zh_log.
34dc7c2f
BB
715 * (zh is part of the MOS, so we cannot modify it in open context.)
716 */
717 if (tx != NULL) {
718 dmu_tx_commit(tx);
719 txg_wait_synced(zilog->zl_dmu_pool, txg);
720 }
721
c04812f9
TC
722 ASSERT(error != 0 || bcmp(&blk, &zh->zh_log, sizeof (blk)) == 0);
723 IMPLY(error == 0, lwb != NULL);
428870ff
BB
724
725 return (lwb);
34dc7c2f
BB
726}
727
728/*
1ce23dca
PS
729 * In one tx, free all log blocks and clear the log header. If keep_first
730 * is set, then we're replaying a log with no content. We want to keep the
731 * first block, however, so that the first synchronous transaction doesn't
732 * require a txg_wait_synced() in zil_create(). We don't need to
733 * txg_wait_synced() here either when keep_first is set, because both
734 * zil_create() and zil_destroy() will wait for any in-progress destroys
735 * to complete.
34dc7c2f
BB
736 */
737void
738zil_destroy(zilog_t *zilog, boolean_t keep_first)
739{
740 const zil_header_t *zh = zilog->zl_header;
741 lwb_t *lwb;
742 dmu_tx_t *tx;
743 uint64_t txg;
744
745 /*
746 * Wait for any previous destroy to complete.
747 */
748 txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
749
428870ff
BB
750 zilog->zl_old_header = *zh; /* debugging aid */
751
34dc7c2f
BB
752 if (BP_IS_HOLE(&zh->zh_log))
753 return;
754
755 tx = dmu_tx_create(zilog->zl_os);
1ce23dca 756 VERIFY0(dmu_tx_assign(tx, TXG_WAIT));
34dc7c2f
BB
757 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
758 txg = dmu_tx_get_txg(tx);
759
760 mutex_enter(&zilog->zl_lock);
761
34dc7c2f
BB
762 ASSERT3U(zilog->zl_destroy_txg, <, txg);
763 zilog->zl_destroy_txg = txg;
764 zilog->zl_keep_first = keep_first;
765
766 if (!list_is_empty(&zilog->zl_lwb_list)) {
767 ASSERT(zh->zh_claim_txg == 0);
3e31d2b0 768 VERIFY(!keep_first);
34dc7c2f 769 while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
920dd524
ED
770 if (lwb->lwb_fastwrite)
771 metaslab_fastwrite_unmark(zilog->zl_spa,
772 &lwb->lwb_blk);
1ce23dca 773
34dc7c2f
BB
774 list_remove(&zilog->zl_lwb_list, lwb);
775 if (lwb->lwb_buf != NULL)
776 zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
1ce23dca
PS
777 zio_free(zilog->zl_spa, txg, &lwb->lwb_blk);
778 zil_free_lwb(zilog, lwb);
34dc7c2f 779 }
428870ff 780 } else if (!keep_first) {
29809a6c 781 zil_destroy_sync(zilog, tx);
34dc7c2f
BB
782 }
783 mutex_exit(&zilog->zl_lock);
784
785 dmu_tx_commit(tx);
786}
787
29809a6c
MA
788void
789zil_destroy_sync(zilog_t *zilog, dmu_tx_t *tx)
790{
791 ASSERT(list_is_empty(&zilog->zl_lwb_list));
792 (void) zil_parse(zilog, zil_free_log_block,
b5256303 793 zil_free_log_record, tx, zilog->zl_header->zh_claim_txg, B_FALSE);
29809a6c
MA
794}
795
34dc7c2f 796int
9c43027b 797zil_claim(dsl_pool_t *dp, dsl_dataset_t *ds, void *txarg)
34dc7c2f
BB
798{
799 dmu_tx_t *tx = txarg;
34dc7c2f 800 zilog_t *zilog;
d2734cce 801 uint64_t first_txg;
34dc7c2f
BB
802 zil_header_t *zh;
803 objset_t *os;
804 int error;
805
9c43027b 806 error = dmu_objset_own_obj(dp, ds->ds_object,
b5256303 807 DMU_OST_ANY, B_FALSE, B_FALSE, FTAG, &os);
13fe0198 808 if (error != 0) {
6d9036f3
MA
809 /*
810 * EBUSY indicates that the objset is inconsistent, in which
811 * case it can not have a ZIL.
812 */
813 if (error != EBUSY) {
9c43027b
AJ
814 cmn_err(CE_WARN, "can't open objset for %llu, error %u",
815 (unsigned long long)ds->ds_object, error);
6d9036f3
MA
816 }
817
34dc7c2f
BB
818 return (0);
819 }
820
821 zilog = dmu_objset_zil(os);
822 zh = zil_header_in_syncing_context(zilog);
d2734cce
SD
823 ASSERT3U(tx->tx_txg, ==, spa_first_txg(zilog->zl_spa));
824 first_txg = spa_min_claim_txg(zilog->zl_spa);
34dc7c2f 825
d2734cce
SD
826 /*
827 * If the spa_log_state is not set to be cleared, check whether
828 * the current uberblock is a checkpoint one and if the current
829 * header has been claimed before moving on.
830 *
831 * If the current uberblock is a checkpointed uberblock then
832 * one of the following scenarios took place:
833 *
834 * 1] We are currently rewinding to the checkpoint of the pool.
835 * 2] We crashed in the middle of a checkpoint rewind but we
836 * did manage to write the checkpointed uberblock to the
837 * vdev labels, so when we tried to import the pool again
838 * the checkpointed uberblock was selected from the import
839 * procedure.
840 *
841 * In both cases we want to zero out all the ZIL blocks, except
842 * the ones that have been claimed at the time of the checkpoint
843 * (their zh_claim_txg != 0). The reason is that these blocks
844 * may be corrupted since we may have reused their locations on
845 * disk after we took the checkpoint.
846 *
847 * We could try to set spa_log_state to SPA_LOG_CLEAR earlier
848 * when we first figure out whether the current uberblock is
849 * checkpointed or not. Unfortunately, that would discard all
850 * the logs, including the ones that are claimed, and we would
851 * leak space.
852 */
853 if (spa_get_log_state(zilog->zl_spa) == SPA_LOG_CLEAR ||
854 (zilog->zl_spa->spa_uberblock.ub_checkpoint_txg != 0 &&
855 zh->zh_claim_txg == 0)) {
856 if (!BP_IS_HOLE(&zh->zh_log)) {
857 (void) zil_parse(zilog, zil_clear_log_block,
858 zil_noop_log_record, tx, first_txg, B_FALSE);
859 }
9babb374 860 BP_ZERO(&zh->zh_log);
b5256303 861 if (os->os_encrypted)
1b66810b 862 os->os_next_write_raw[tx->tx_txg & TXG_MASK] = B_TRUE;
9babb374 863 dsl_dataset_dirty(dmu_objset_ds(os), tx);
b5256303 864 dmu_objset_disown(os, B_FALSE, FTAG);
428870ff 865 return (0);
9babb374
BB
866 }
867
d2734cce
SD
868 /*
869 * If we are not rewinding and opening the pool normally, then
870 * the min_claim_txg should be equal to the first txg of the pool.
871 */
872 ASSERT3U(first_txg, ==, spa_first_txg(zilog->zl_spa));
873
34dc7c2f
BB
874 /*
875 * Claim all log blocks if we haven't already done so, and remember
876 * the highest claimed sequence number. This ensures that if we can
877 * read only part of the log now (e.g. due to a missing device),
878 * but we can read the entire log later, we will not try to replay
879 * or destroy beyond the last block we successfully claimed.
880 */
881 ASSERT3U(zh->zh_claim_txg, <=, first_txg);
882 if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) {
428870ff 883 (void) zil_parse(zilog, zil_claim_log_block,
b5256303 884 zil_claim_log_record, tx, first_txg, B_FALSE);
428870ff
BB
885 zh->zh_claim_txg = first_txg;
886 zh->zh_claim_blk_seq = zilog->zl_parse_blk_seq;
887 zh->zh_claim_lr_seq = zilog->zl_parse_lr_seq;
888 if (zilog->zl_parse_lr_count || zilog->zl_parse_blk_count > 1)
889 zh->zh_flags |= ZIL_REPLAY_NEEDED;
890 zh->zh_flags |= ZIL_CLAIM_LR_SEQ_VALID;
d53bd7f5 891 if (os->os_encrypted)
1b66810b 892 os->os_next_write_raw[tx->tx_txg & TXG_MASK] = B_TRUE;
34dc7c2f
BB
893 dsl_dataset_dirty(dmu_objset_ds(os), tx);
894 }
895
896 ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1));
b5256303 897 dmu_objset_disown(os, B_FALSE, FTAG);
34dc7c2f
BB
898 return (0);
899}
900
b128c09f
BB
901/*
902 * Check the log by walking the log chain.
903 * Checksum errors are ok as they indicate the end of the chain.
904 * Any other error (no device or read failure) returns an error.
905 */
9c43027b 906/* ARGSUSED */
b128c09f 907int
9c43027b 908zil_check_log_chain(dsl_pool_t *dp, dsl_dataset_t *ds, void *tx)
b128c09f
BB
909{
910 zilog_t *zilog;
b128c09f 911 objset_t *os;
572e2857 912 blkptr_t *bp;
b128c09f
BB
913 int error;
914
428870ff
BB
915 ASSERT(tx == NULL);
916
9c43027b 917 error = dmu_objset_from_ds(ds, &os);
13fe0198 918 if (error != 0) {
9c43027b
AJ
919 cmn_err(CE_WARN, "can't open objset %llu, error %d",
920 (unsigned long long)ds->ds_object, error);
b128c09f
BB
921 return (0);
922 }
923
924 zilog = dmu_objset_zil(os);
572e2857
BB
925 bp = (blkptr_t *)&zilog->zl_header->zh_log;
926
572e2857
BB
927 if (!BP_IS_HOLE(bp)) {
928 vdev_t *vd;
929 boolean_t valid = B_TRUE;
930
d2734cce
SD
931 /*
932 * Check the first block and determine if it's on a log device
933 * which may have been removed or faulted prior to loading this
934 * pool. If so, there's no point in checking the rest of the
935 * log as its content should have already been synced to the
936 * pool.
937 */
572e2857
BB
938 spa_config_enter(os->os_spa, SCL_STATE, FTAG, RW_READER);
939 vd = vdev_lookup_top(os->os_spa, DVA_GET_VDEV(&bp->blk_dva[0]));
940 if (vd->vdev_islog && vdev_is_dead(vd))
941 valid = vdev_log_state_valid(vd);
942 spa_config_exit(os->os_spa, SCL_STATE, FTAG);
943
9c43027b 944 if (!valid)
572e2857 945 return (0);
d2734cce
SD
946
947 /*
948 * Check whether the current uberblock is checkpointed (e.g.
949 * we are rewinding) and whether the current header has been
950 * claimed or not. If it hasn't then skip verifying it. We
951 * do this because its ZIL blocks may be part of the pool's
952 * state before the rewind, which is no longer valid.
953 */
954 zil_header_t *zh = zil_header_in_syncing_context(zilog);
955 if (zilog->zl_spa->spa_uberblock.ub_checkpoint_txg != 0 &&
956 zh->zh_claim_txg == 0)
957 return (0);
572e2857 958 }
b128c09f 959
428870ff
BB
960 /*
961 * Because tx == NULL, zil_claim_log_block() will not actually claim
962 * any blocks, but just determine whether it is possible to do so.
963 * In addition to checking the log chain, zil_claim_log_block()
964 * will invoke zio_claim() with a done func of spa_claim_notify(),
965 * which will update spa_max_claim_txg. See spa_load() for details.
966 */
967 error = zil_parse(zilog, zil_claim_log_block, zil_claim_log_record, tx,
d2734cce
SD
968 zilog->zl_header->zh_claim_txg ? -1ULL :
969 spa_min_claim_txg(os->os_spa), B_FALSE);
428870ff 970
428870ff 971 return ((error == ECKSUM || error == ENOENT) ? 0 : error);
b128c09f
BB
972}
973
1ce23dca
PS
974/*
975 * When an itx is "skipped", this function is used to properly mark the
976 * waiter as "done, and signal any thread(s) waiting on it. An itx can
977 * be skipped (and not committed to an lwb) for a variety of reasons,
978 * one of them being that the itx was committed via spa_sync(), prior to
979 * it being committed to an lwb; this can happen if a thread calling
980 * zil_commit() is racing with spa_sync().
981 */
982static void
983zil_commit_waiter_skip(zil_commit_waiter_t *zcw)
34dc7c2f 984{
1ce23dca
PS
985 mutex_enter(&zcw->zcw_lock);
986 ASSERT3B(zcw->zcw_done, ==, B_FALSE);
987 zcw->zcw_done = B_TRUE;
988 cv_broadcast(&zcw->zcw_cv);
989 mutex_exit(&zcw->zcw_lock);
990}
34dc7c2f 991
1ce23dca
PS
992/*
993 * This function is used when the given waiter is to be linked into an
994 * lwb's "lwb_waiter" list; i.e. when the itx is committed to the lwb.
995 * At this point, the waiter will no longer be referenced by the itx,
996 * and instead, will be referenced by the lwb.
997 */
998static void
999zil_commit_waiter_link_lwb(zil_commit_waiter_t *zcw, lwb_t *lwb)
1000{
2fe61a7e
PS
1001 /*
1002 * The lwb_waiters field of the lwb is protected by the zilog's
1003 * zl_lock, thus it must be held when calling this function.
1004 */
1005 ASSERT(MUTEX_HELD(&lwb->lwb_zilog->zl_lock));
1006
1ce23dca
PS
1007 mutex_enter(&zcw->zcw_lock);
1008 ASSERT(!list_link_active(&zcw->zcw_node));
1009 ASSERT3P(zcw->zcw_lwb, ==, NULL);
1010 ASSERT3P(lwb, !=, NULL);
1011 ASSERT(lwb->lwb_state == LWB_STATE_OPENED ||
900d09b2
PS
1012 lwb->lwb_state == LWB_STATE_ISSUED ||
1013 lwb->lwb_state == LWB_STATE_WRITE_DONE);
1ce23dca
PS
1014
1015 list_insert_tail(&lwb->lwb_waiters, zcw);
1016 zcw->zcw_lwb = lwb;
1017 mutex_exit(&zcw->zcw_lock);
1018}
1019
1020/*
1021 * This function is used when zio_alloc_zil() fails to allocate a ZIL
1022 * block, and the given waiter must be linked to the "nolwb waiters"
1023 * list inside of zil_process_commit_list().
1024 */
1025static void
1026zil_commit_waiter_link_nolwb(zil_commit_waiter_t *zcw, list_t *nolwb)
1027{
1028 mutex_enter(&zcw->zcw_lock);
1029 ASSERT(!list_link_active(&zcw->zcw_node));
1030 ASSERT3P(zcw->zcw_lwb, ==, NULL);
1031 list_insert_tail(nolwb, zcw);
1032 mutex_exit(&zcw->zcw_lock);
34dc7c2f
BB
1033}
1034
1035void
1ce23dca 1036zil_lwb_add_block(lwb_t *lwb, const blkptr_t *bp)
34dc7c2f 1037{
1ce23dca 1038 avl_tree_t *t = &lwb->lwb_vdev_tree;
34dc7c2f
BB
1039 avl_index_t where;
1040 zil_vdev_node_t *zv, zvsearch;
1041 int ndvas = BP_GET_NDVAS(bp);
1042 int i;
1043
53b1f5ea 1044 if (zil_nocacheflush)
34dc7c2f
BB
1045 return;
1046
1ce23dca 1047 mutex_enter(&lwb->lwb_vdev_lock);
34dc7c2f
BB
1048 for (i = 0; i < ndvas; i++) {
1049 zvsearch.zv_vdev = DVA_GET_VDEV(&bp->blk_dva[i]);
1050 if (avl_find(t, &zvsearch, &where) == NULL) {
79c76d5b 1051 zv = kmem_alloc(sizeof (*zv), KM_SLEEP);
34dc7c2f
BB
1052 zv->zv_vdev = zvsearch.zv_vdev;
1053 avl_insert(t, zv, where);
1054 }
1055 }
1ce23dca 1056 mutex_exit(&lwb->lwb_vdev_lock);
34dc7c2f
BB
1057}
1058
900d09b2
PS
1059static void
1060zil_lwb_flush_defer(lwb_t *lwb, lwb_t *nlwb)
1061{
1062 avl_tree_t *src = &lwb->lwb_vdev_tree;
1063 avl_tree_t *dst = &nlwb->lwb_vdev_tree;
1064 void *cookie = NULL;
1065 zil_vdev_node_t *zv;
1066
1067 ASSERT3S(lwb->lwb_state, ==, LWB_STATE_WRITE_DONE);
1068 ASSERT3S(nlwb->lwb_state, !=, LWB_STATE_WRITE_DONE);
1069 ASSERT3S(nlwb->lwb_state, !=, LWB_STATE_FLUSH_DONE);
1070
1071 /*
1072 * While 'lwb' is at a point in its lifetime where lwb_vdev_tree does
1073 * not need the protection of lwb_vdev_lock (it will only be modified
1074 * while holding zilog->zl_lock) as its writes and those of its
1075 * children have all completed. The younger 'nlwb' may be waiting on
1076 * future writes to additional vdevs.
1077 */
1078 mutex_enter(&nlwb->lwb_vdev_lock);
1079 /*
1080 * Tear down the 'lwb' vdev tree, ensuring that entries which do not
1081 * exist in 'nlwb' are moved to it, freeing any would-be duplicates.
1082 */
1083 while ((zv = avl_destroy_nodes(src, &cookie)) != NULL) {
1084 avl_index_t where;
1085
1086 if (avl_find(dst, zv, &where) == NULL) {
1087 avl_insert(dst, zv, where);
1088 } else {
1089 kmem_free(zv, sizeof (*zv));
1090 }
1091 }
1092 mutex_exit(&nlwb->lwb_vdev_lock);
1093}
1094
1ce23dca
PS
1095void
1096zil_lwb_add_txg(lwb_t *lwb, uint64_t txg)
1097{
1098 lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg);
1099}
1100
1101/*
900d09b2 1102 * This function is a called after all vdevs associated with a given lwb
1ce23dca 1103 * write have completed their DKIOCFLUSHWRITECACHE command; or as soon
900d09b2
PS
1104 * as the lwb write completes, if "zil_nocacheflush" is set. Further,
1105 * all "previous" lwb's will have completed before this function is
1106 * called; i.e. this function is called for all previous lwbs before
1107 * it's called for "this" lwb (enforced via zio the dependencies
1108 * configured in zil_lwb_set_zio_dependency()).
1ce23dca
PS
1109 *
1110 * The intention is for this function to be called as soon as the
1111 * contents of an lwb are considered "stable" on disk, and will survive
1112 * any sudden loss of power. At this point, any threads waiting for the
1113 * lwb to reach this state are signalled, and the "waiter" structures
1114 * are marked "done".
1115 */
572e2857 1116static void
1ce23dca 1117zil_lwb_flush_vdevs_done(zio_t *zio)
34dc7c2f 1118{
1ce23dca
PS
1119 lwb_t *lwb = zio->io_private;
1120 zilog_t *zilog = lwb->lwb_zilog;
1121 dmu_tx_t *tx = lwb->lwb_tx;
1122 zil_commit_waiter_t *zcw;
1123 itx_t *itx;
1124
1125 spa_config_exit(zilog->zl_spa, SCL_STATE, lwb);
1126
1127 zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
34dc7c2f 1128
1ce23dca 1129 mutex_enter(&zilog->zl_lock);
34dc7c2f
BB
1130
1131 /*
1ce23dca
PS
1132 * Ensure the lwb buffer pointer is cleared before releasing the
1133 * txg. If we have had an allocation failure and the txg is
1134 * waiting to sync then we want zil_sync() to remove the lwb so
1135 * that it's not picked up as the next new one in
1136 * zil_process_commit_list(). zil_sync() will only remove the
1137 * lwb if lwb_buf is null.
34dc7c2f 1138 */
1ce23dca
PS
1139 lwb->lwb_buf = NULL;
1140 lwb->lwb_tx = NULL;
34dc7c2f 1141
1ce23dca
PS
1142 ASSERT3U(lwb->lwb_issued_timestamp, >, 0);
1143 zilog->zl_last_lwb_latency = gethrtime() - lwb->lwb_issued_timestamp;
34dc7c2f 1144
1ce23dca 1145 lwb->lwb_root_zio = NULL;
900d09b2
PS
1146
1147 ASSERT3S(lwb->lwb_state, ==, LWB_STATE_WRITE_DONE);
1148 lwb->lwb_state = LWB_STATE_FLUSH_DONE;
34dc7c2f 1149
1ce23dca
PS
1150 if (zilog->zl_last_lwb_opened == lwb) {
1151 /*
1152 * Remember the highest committed log sequence number
1153 * for ztest. We only update this value when all the log
1154 * writes succeeded, because ztest wants to ASSERT that
1155 * it got the whole log chain.
1156 */
1157 zilog->zl_commit_lr_seq = zilog->zl_lr_seq;
1158 }
1159
1160 while ((itx = list_head(&lwb->lwb_itxs)) != NULL) {
1161 list_remove(&lwb->lwb_itxs, itx);
1162 zil_itx_destroy(itx);
1163 }
1164
1165 while ((zcw = list_head(&lwb->lwb_waiters)) != NULL) {
1166 mutex_enter(&zcw->zcw_lock);
1167
1168 ASSERT(list_link_active(&zcw->zcw_node));
1169 list_remove(&lwb->lwb_waiters, zcw);
1170
1171 ASSERT3P(zcw->zcw_lwb, ==, lwb);
1172 zcw->zcw_lwb = NULL;
1173
1174 zcw->zcw_zio_error = zio->io_error;
1175
1176 ASSERT3B(zcw->zcw_done, ==, B_FALSE);
1177 zcw->zcw_done = B_TRUE;
1178 cv_broadcast(&zcw->zcw_cv);
1179
1180 mutex_exit(&zcw->zcw_lock);
34dc7c2f
BB
1181 }
1182
1ce23dca
PS
1183 mutex_exit(&zilog->zl_lock);
1184
34dc7c2f 1185 /*
1ce23dca
PS
1186 * Now that we've written this log block, we have a stable pointer
1187 * to the next block in the chain, so it's OK to let the txg in
1188 * which we allocated the next block sync.
34dc7c2f 1189 */
1ce23dca 1190 dmu_tx_commit(tx);
34dc7c2f
BB
1191}
1192
1193/*
900d09b2
PS
1194 * This is called when an lwb's write zio completes. The callback's
1195 * purpose is to issue the DKIOCFLUSHWRITECACHE commands for the vdevs
1196 * in the lwb's lwb_vdev_tree. The tree will contain the vdevs involved
1197 * in writing out this specific lwb's data, and in the case that cache
1198 * flushes have been deferred, vdevs involved in writing the data for
1199 * previous lwbs. The writes corresponding to all the vdevs in the
1200 * lwb_vdev_tree will have completed by the time this is called, due to
1201 * the zio dependencies configured in zil_lwb_set_zio_dependency(),
1202 * which takes deferred flushes into account. The lwb will be "done"
1203 * once zil_lwb_flush_vdevs_done() is called, which occurs in the zio
1204 * completion callback for the lwb's root zio.
34dc7c2f
BB
1205 */
1206static void
1207zil_lwb_write_done(zio_t *zio)
1208{
1209 lwb_t *lwb = zio->io_private;
1ce23dca 1210 spa_t *spa = zio->io_spa;
34dc7c2f 1211 zilog_t *zilog = lwb->lwb_zilog;
1ce23dca
PS
1212 avl_tree_t *t = &lwb->lwb_vdev_tree;
1213 void *cookie = NULL;
1214 zil_vdev_node_t *zv;
900d09b2 1215 lwb_t *nlwb;
1ce23dca
PS
1216
1217 ASSERT3S(spa_config_held(spa, SCL_STATE, RW_READER), !=, 0);
34dc7c2f 1218
b128c09f 1219 ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF);
b128c09f
BB
1220 ASSERT(BP_GET_TYPE(zio->io_bp) == DMU_OT_INTENT_LOG);
1221 ASSERT(BP_GET_LEVEL(zio->io_bp) == 0);
1222 ASSERT(BP_GET_BYTEORDER(zio->io_bp) == ZFS_HOST_BYTEORDER);
1223 ASSERT(!BP_IS_GANG(zio->io_bp));
1224 ASSERT(!BP_IS_HOLE(zio->io_bp));
9b67f605 1225 ASSERT(BP_GET_FILL(zio->io_bp) == 0);
b128c09f 1226
a6255b7f 1227 abd_put(zio->io_abd);
1ce23dca 1228
34dc7c2f 1229 mutex_enter(&zilog->zl_lock);
900d09b2
PS
1230 ASSERT3S(lwb->lwb_state, ==, LWB_STATE_ISSUED);
1231 lwb->lwb_state = LWB_STATE_WRITE_DONE;
1ce23dca 1232 lwb->lwb_write_zio = NULL;
920dd524 1233 lwb->lwb_fastwrite = FALSE;
900d09b2 1234 nlwb = list_next(&zilog->zl_lwb_list, lwb);
428870ff 1235 mutex_exit(&zilog->zl_lock);
9babb374 1236
1ce23dca
PS
1237 if (avl_numnodes(t) == 0)
1238 return;
1239
9babb374 1240 /*
1ce23dca
PS
1241 * If there was an IO error, we're not going to call zio_flush()
1242 * on these vdevs, so we simply empty the tree and free the
1243 * nodes. We avoid calling zio_flush() since there isn't any
1244 * good reason for doing so, after the lwb block failed to be
1245 * written out.
9babb374 1246 */
1ce23dca
PS
1247 if (zio->io_error != 0) {
1248 while ((zv = avl_destroy_nodes(t, &cookie)) != NULL)
1249 kmem_free(zv, sizeof (*zv));
1250 return;
1251 }
1252
900d09b2
PS
1253 /*
1254 * If this lwb does not have any threads waiting for it to
1255 * complete, we want to defer issuing the DKIOCFLUSHWRITECACHE
1256 * command to the vdevs written to by "this" lwb, and instead
1257 * rely on the "next" lwb to handle the DKIOCFLUSHWRITECACHE
1258 * command for those vdevs. Thus, we merge the vdev tree of
1259 * "this" lwb with the vdev tree of the "next" lwb in the list,
1260 * and assume the "next" lwb will handle flushing the vdevs (or
1261 * deferring the flush(s) again).
1262 *
1263 * This is a useful performance optimization, especially for
1264 * workloads with lots of async write activity and few sync
1265 * write and/or fsync activity, as it has the potential to
1266 * coalesce multiple flush commands to a vdev into one.
1267 */
1268 if (list_head(&lwb->lwb_waiters) == NULL && nlwb != NULL) {
1269 zil_lwb_flush_defer(lwb, nlwb);
1270 ASSERT(avl_is_empty(&lwb->lwb_vdev_tree));
1271 return;
1272 }
1273
1ce23dca
PS
1274 while ((zv = avl_destroy_nodes(t, &cookie)) != NULL) {
1275 vdev_t *vd = vdev_lookup_top(spa, zv->zv_vdev);
1276 if (vd != NULL)
1277 zio_flush(lwb->lwb_root_zio, vd);
1278 kmem_free(zv, sizeof (*zv));
1279 }
34dc7c2f
BB
1280}
1281
900d09b2
PS
1282static void
1283zil_lwb_set_zio_dependency(zilog_t *zilog, lwb_t *lwb)
1284{
1285 lwb_t *last_lwb_opened = zilog->zl_last_lwb_opened;
1286
1287 ASSERT(MUTEX_HELD(&zilog->zl_issuer_lock));
1288 ASSERT(MUTEX_HELD(&zilog->zl_lock));
1289
1290 /*
1291 * The zilog's "zl_last_lwb_opened" field is used to build the
1292 * lwb/zio dependency chain, which is used to preserve the
1293 * ordering of lwb completions that is required by the semantics
1294 * of the ZIL. Each new lwb zio becomes a parent of the
1295 * "previous" lwb zio, such that the new lwb's zio cannot
1296 * complete until the "previous" lwb's zio completes.
1297 *
1298 * This is required by the semantics of zil_commit(); the commit
1299 * waiters attached to the lwbs will be woken in the lwb zio's
1300 * completion callback, so this zio dependency graph ensures the
1301 * waiters are woken in the correct order (the same order the
1302 * lwbs were created).
1303 */
1304 if (last_lwb_opened != NULL &&
1305 last_lwb_opened->lwb_state != LWB_STATE_FLUSH_DONE) {
1306 ASSERT(last_lwb_opened->lwb_state == LWB_STATE_OPENED ||
1307 last_lwb_opened->lwb_state == LWB_STATE_ISSUED ||
1308 last_lwb_opened->lwb_state == LWB_STATE_WRITE_DONE);
1309
1310 ASSERT3P(last_lwb_opened->lwb_root_zio, !=, NULL);
1311 zio_add_child(lwb->lwb_root_zio,
1312 last_lwb_opened->lwb_root_zio);
1313
1314 /*
1315 * If the previous lwb's write hasn't already completed,
1316 * we also want to order the completion of the lwb write
1317 * zios (above, we only order the completion of the lwb
1318 * root zios). This is required because of how we can
1319 * defer the DKIOCFLUSHWRITECACHE commands for each lwb.
1320 *
612c4930 1321 * When the DKIOCFLUSHWRITECACHE commands are deferred,
900d09b2
PS
1322 * the previous lwb will rely on this lwb to flush the
1323 * vdevs written to by that previous lwb. Thus, we need
1324 * to ensure this lwb doesn't issue the flush until
1325 * after the previous lwb's write completes. We ensure
1326 * this ordering by setting the zio parent/child
1327 * relationship here.
1328 *
1329 * Without this relationship on the lwb's write zio,
1330 * it's possible for this lwb's write to complete prior
1331 * to the previous lwb's write completing; and thus, the
1332 * vdevs for the previous lwb would be flushed prior to
1333 * that lwb's data being written to those vdevs (the
1334 * vdevs are flushed in the lwb write zio's completion
1335 * handler, zil_lwb_write_done()).
1336 */
1337 if (last_lwb_opened->lwb_state != LWB_STATE_WRITE_DONE) {
1338 ASSERT(last_lwb_opened->lwb_state == LWB_STATE_OPENED ||
1339 last_lwb_opened->lwb_state == LWB_STATE_ISSUED);
1340
1341 ASSERT3P(last_lwb_opened->lwb_write_zio, !=, NULL);
1342 zio_add_child(lwb->lwb_write_zio,
1343 last_lwb_opened->lwb_write_zio);
1344 }
1345 }
1346}
1347
1348
34dc7c2f 1349/*
1ce23dca
PS
1350 * This function's purpose is to "open" an lwb such that it is ready to
1351 * accept new itxs being committed to it. To do this, the lwb's zio
1352 * structures are created, and linked to the lwb. This function is
1353 * idempotent; if the passed in lwb has already been opened, this
1354 * function is essentially a no-op.
34dc7c2f
BB
1355 */
1356static void
1ce23dca 1357zil_lwb_write_open(zilog_t *zilog, lwb_t *lwb)
34dc7c2f 1358{
5dbd68a3 1359 zbookmark_phys_t zb;
1b7c1e5c 1360 zio_priority_t prio;
34dc7c2f 1361
1b2b0aca 1362 ASSERT(MUTEX_HELD(&zilog->zl_issuer_lock));
1ce23dca
PS
1363 ASSERT3P(lwb, !=, NULL);
1364 EQUIV(lwb->lwb_root_zio == NULL, lwb->lwb_state == LWB_STATE_CLOSED);
1365 EQUIV(lwb->lwb_root_zio != NULL, lwb->lwb_state == LWB_STATE_OPENED);
1366
428870ff
BB
1367 SET_BOOKMARK(&zb, lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET],
1368 ZB_ZIL_OBJECT, ZB_ZIL_LEVEL,
1369 lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ]);
34dc7c2f 1370
920dd524
ED
1371 /* Lock so zil_sync() doesn't fastwrite_unmark after zio is created */
1372 mutex_enter(&zilog->zl_lock);
1ce23dca 1373 if (lwb->lwb_root_zio == NULL) {
a6255b7f
DQ
1374 abd_t *lwb_abd = abd_get_from_buf(lwb->lwb_buf,
1375 BP_GET_LSIZE(&lwb->lwb_blk));
1ce23dca 1376
920dd524
ED
1377 if (!lwb->lwb_fastwrite) {
1378 metaslab_fastwrite_mark(zilog->zl_spa, &lwb->lwb_blk);
1379 lwb->lwb_fastwrite = 1;
1380 }
1ce23dca 1381
1b7c1e5c
GDN
1382 if (!lwb->lwb_slog || zilog->zl_cur_used <= zil_slog_bulk)
1383 prio = ZIO_PRIORITY_SYNC_WRITE;
1384 else
1385 prio = ZIO_PRIORITY_ASYNC_WRITE;
1ce23dca
PS
1386
1387 lwb->lwb_root_zio = zio_root(zilog->zl_spa,
1388 zil_lwb_flush_vdevs_done, lwb, ZIO_FLAG_CANFAIL);
1389 ASSERT3P(lwb->lwb_root_zio, !=, NULL);
1390
1391 lwb->lwb_write_zio = zio_rewrite(lwb->lwb_root_zio,
1392 zilog->zl_spa, 0, &lwb->lwb_blk, lwb_abd,
1393 BP_GET_LSIZE(&lwb->lwb_blk), zil_lwb_write_done, lwb,
1394 prio, ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE |
920dd524 1395 ZIO_FLAG_FASTWRITE, &zb);
1ce23dca
PS
1396 ASSERT3P(lwb->lwb_write_zio, !=, NULL);
1397
1398 lwb->lwb_state = LWB_STATE_OPENED;
1399
900d09b2 1400 zil_lwb_set_zio_dependency(zilog, lwb);
1ce23dca 1401 zilog->zl_last_lwb_opened = lwb;
34dc7c2f 1402 }
920dd524 1403 mutex_exit(&zilog->zl_lock);
1ce23dca
PS
1404
1405 ASSERT3P(lwb->lwb_root_zio, !=, NULL);
1406 ASSERT3P(lwb->lwb_write_zio, !=, NULL);
1407 ASSERT3S(lwb->lwb_state, ==, LWB_STATE_OPENED);
34dc7c2f
BB
1408}
1409
428870ff
BB
1410/*
1411 * Define a limited set of intent log block sizes.
d3cc8b15 1412 *
428870ff
BB
1413 * These must be a multiple of 4KB. Note only the amount used (again
1414 * aligned to 4KB) actually gets written. However, we can't always just
f1512ee6 1415 * allocate SPA_OLD_MAXBLOCKSIZE as the slog space could be exhausted.
428870ff 1416 */
f15d6a54
AM
1417struct {
1418 uint64_t limit;
1419 uint64_t blksz;
1420} zil_block_buckets[] = {
1421 { 4096, 4096 }, /* non TX_WRITE */
1422 { 8192 + 4096, 8192 + 4096 }, /* database */
1423 { 32768 + 4096, 32768 + 4096 }, /* NFS writes */
1424 { 65536 + 4096, 65536 + 4096 }, /* 64KB writes */
1425 { 131072, 131072 }, /* < 128KB writes */
1426 { 131072 +4096, 65536 + 4096 }, /* 128KB writes */
1427 { UINT64_MAX, SPA_OLD_MAXBLOCKSIZE}, /* > 128KB writes */
428870ff
BB
1428};
1429
b8738257
MA
1430/*
1431 * Maximum block size used by the ZIL. This is picked up when the ZIL is
1432 * initialized. Otherwise this should not be used directly; see
1433 * zl_max_block_size instead.
1434 */
1435int zil_maxblocksize = SPA_OLD_MAXBLOCKSIZE;
1436
34dc7c2f
BB
1437/*
1438 * Start a log block write and advance to the next log block.
1439 * Calls are serialized.
1440 */
1441static lwb_t *
1ce23dca 1442zil_lwb_write_issue(zilog_t *zilog, lwb_t *lwb)
34dc7c2f 1443{
428870ff
BB
1444 lwb_t *nlwb = NULL;
1445 zil_chain_t *zilc;
34dc7c2f 1446 spa_t *spa = zilog->zl_spa;
428870ff
BB
1447 blkptr_t *bp;
1448 dmu_tx_t *tx;
34dc7c2f 1449 uint64_t txg;
428870ff
BB
1450 uint64_t zil_blksz, wsz;
1451 int i, error;
1b7c1e5c 1452 boolean_t slog;
428870ff 1453
1b2b0aca 1454 ASSERT(MUTEX_HELD(&zilog->zl_issuer_lock));
1ce23dca
PS
1455 ASSERT3P(lwb->lwb_root_zio, !=, NULL);
1456 ASSERT3P(lwb->lwb_write_zio, !=, NULL);
1457 ASSERT3S(lwb->lwb_state, ==, LWB_STATE_OPENED);
1458
428870ff
BB
1459 if (BP_GET_CHECKSUM(&lwb->lwb_blk) == ZIO_CHECKSUM_ZILOG2) {
1460 zilc = (zil_chain_t *)lwb->lwb_buf;
1461 bp = &zilc->zc_next_blk;
1462 } else {
1463 zilc = (zil_chain_t *)(lwb->lwb_buf + lwb->lwb_sz);
1464 bp = &zilc->zc_next_blk;
1465 }
34dc7c2f 1466
428870ff 1467 ASSERT(lwb->lwb_nused <= lwb->lwb_sz);
34dc7c2f
BB
1468
1469 /*
1470 * Allocate the next block and save its address in this block
1471 * before writing it in order to establish the log chain.
1472 * Note that if the allocation of nlwb synced before we wrote
1473 * the block that points at it (lwb), we'd leak it if we crashed.
428870ff
BB
1474 * Therefore, we don't do dmu_tx_commit() until zil_lwb_write_done().
1475 * We dirty the dataset to ensure that zil_sync() will be called
1476 * to clean up in the event of allocation failure or I/O failure.
34dc7c2f 1477 */
1ce23dca 1478
428870ff 1479 tx = dmu_tx_create(zilog->zl_os);
e98b6117
AG
1480
1481 /*
0735ecb3
PS
1482 * Since we are not going to create any new dirty data, and we
1483 * can even help with clearing the existing dirty data, we
1484 * should not be subject to the dirty data based delays. We
1485 * use TXG_NOTHROTTLE to bypass the delay mechanism.
e98b6117 1486 */
0735ecb3
PS
1487 VERIFY0(dmu_tx_assign(tx, TXG_WAIT | TXG_NOTHROTTLE));
1488
428870ff
BB
1489 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
1490 txg = dmu_tx_get_txg(tx);
1491
1492 lwb->lwb_tx = tx;
34dc7c2f
BB
1493
1494 /*
428870ff
BB
1495 * Log blocks are pre-allocated. Here we select the size of the next
1496 * block, based on size used in the last block.
1497 * - first find the smallest bucket that will fit the block from a
1498 * limited set of block sizes. This is because it's faster to write
1499 * blocks allocated from the same metaslab as they are adjacent or
1500 * close.
1501 * - next find the maximum from the new suggested size and an array of
1502 * previous sizes. This lessens a picket fence effect of wrongly
2fe61a7e 1503 * guessing the size if we have a stream of say 2k, 64k, 2k, 64k
428870ff
BB
1504 * requests.
1505 *
1506 * Note we only write what is used, but we can't just allocate
1507 * the maximum block size because we can exhaust the available
1508 * pool log space.
34dc7c2f 1509 */
428870ff 1510 zil_blksz = zilog->zl_cur_used + sizeof (zil_chain_t);
f15d6a54 1511 for (i = 0; zil_blksz > zil_block_buckets[i].limit; i++)
428870ff 1512 continue;
f15d6a54 1513 zil_blksz = MIN(zil_block_buckets[i].blksz, zilog->zl_max_block_size);
428870ff
BB
1514 zilog->zl_prev_blks[zilog->zl_prev_rotor] = zil_blksz;
1515 for (i = 0; i < ZIL_PREV_BLKS; i++)
1516 zil_blksz = MAX(zil_blksz, zilog->zl_prev_blks[i]);
1517 zilog->zl_prev_rotor = (zilog->zl_prev_rotor + 1) & (ZIL_PREV_BLKS - 1);
34dc7c2f
BB
1518
1519 BP_ZERO(bp);
b5256303 1520 error = zio_alloc_zil(spa, zilog->zl_os, txg, bp, zil_blksz, &slog);
1b7c1e5c 1521 if (slog) {
b6ad9671
ED
1522 ZIL_STAT_BUMP(zil_itx_metaslab_slog_count);
1523 ZIL_STAT_INCR(zil_itx_metaslab_slog_bytes, lwb->lwb_nused);
d1d7e268 1524 } else {
b6ad9671
ED
1525 ZIL_STAT_BUMP(zil_itx_metaslab_normal_count);
1526 ZIL_STAT_INCR(zil_itx_metaslab_normal_bytes, lwb->lwb_nused);
1527 }
13fe0198 1528 if (error == 0) {
428870ff
BB
1529 ASSERT3U(bp->blk_birth, ==, txg);
1530 bp->blk_cksum = lwb->lwb_blk.blk_cksum;
1531 bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++;
34dc7c2f
BB
1532
1533 /*
1ce23dca 1534 * Allocate a new log write block (lwb).
34dc7c2f 1535 */
1b7c1e5c 1536 nlwb = zil_alloc_lwb(zilog, bp, slog, txg, TRUE);
34dc7c2f
BB
1537 }
1538
428870ff
BB
1539 if (BP_GET_CHECKSUM(&lwb->lwb_blk) == ZIO_CHECKSUM_ZILOG2) {
1540 /* For Slim ZIL only write what is used. */
1541 wsz = P2ROUNDUP_TYPED(lwb->lwb_nused, ZIL_MIN_BLKSZ, uint64_t);
1542 ASSERT3U(wsz, <=, lwb->lwb_sz);
1ce23dca 1543 zio_shrink(lwb->lwb_write_zio, wsz);
34dc7c2f 1544
428870ff
BB
1545 } else {
1546 wsz = lwb->lwb_sz;
1547 }
34dc7c2f 1548
428870ff
BB
1549 zilc->zc_pad = 0;
1550 zilc->zc_nused = lwb->lwb_nused;
1551 zilc->zc_eck.zec_cksum = lwb->lwb_blk.blk_cksum;
34dc7c2f
BB
1552
1553 /*
428870ff 1554 * clear unused data for security
34dc7c2f 1555 */
428870ff 1556 bzero(lwb->lwb_buf + lwb->lwb_nused, wsz - lwb->lwb_nused);
34dc7c2f 1557
1ce23dca
PS
1558 spa_config_enter(zilog->zl_spa, SCL_STATE, lwb, RW_READER);
1559
1560 zil_lwb_add_block(lwb, &lwb->lwb_blk);
1561 lwb->lwb_issued_timestamp = gethrtime();
1562 lwb->lwb_state = LWB_STATE_ISSUED;
1563
1564 zio_nowait(lwb->lwb_root_zio);
1565 zio_nowait(lwb->lwb_write_zio);
34dc7c2f
BB
1566
1567 /*
428870ff
BB
1568 * If there was an allocation failure then nlwb will be null which
1569 * forces a txg_wait_synced().
34dc7c2f 1570 */
34dc7c2f
BB
1571 return (nlwb);
1572}
1573
b8738257
MA
1574/*
1575 * Maximum amount of write data that can be put into single log block.
1576 */
1577uint64_t
1578zil_max_log_data(zilog_t *zilog)
1579{
1580 return (zilog->zl_max_block_size -
1581 sizeof (zil_chain_t) - sizeof (lr_write_t));
1582}
1583
1584/*
1585 * Maximum amount of log space we agree to waste to reduce number of
1586 * WR_NEED_COPY chunks to reduce zl_get_data() overhead (~12%).
1587 */
1588static inline uint64_t
1589zil_max_waste_space(zilog_t *zilog)
1590{
1591 return (zil_max_log_data(zilog) / 8);
1592}
1593
1594/*
1595 * Maximum amount of write data for WR_COPIED. For correctness, consumers
1596 * must fall back to WR_NEED_COPY if we can't fit the entire record into one
1597 * maximum sized log block, because each WR_COPIED record must fit in a
1598 * single log block. For space efficiency, we want to fit two records into a
1599 * max-sized log block.
1600 */
1601uint64_t
1602zil_max_copied_data(zilog_t *zilog)
1603{
1604 return ((zilog->zl_max_block_size - sizeof (zil_chain_t)) / 2 -
1605 sizeof (lr_write_t));
1606}
1607
34dc7c2f
BB
1608static lwb_t *
1609zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb)
1610{
1b7c1e5c
GDN
1611 lr_t *lrcb, *lrc;
1612 lr_write_t *lrwb, *lrw;
428870ff 1613 char *lr_buf;
b8738257 1614 uint64_t dlen, dnow, lwb_sp, reclen, txg, max_log_data;
34dc7c2f 1615
1b2b0aca 1616 ASSERT(MUTEX_HELD(&zilog->zl_issuer_lock));
1ce23dca
PS
1617 ASSERT3P(lwb, !=, NULL);
1618 ASSERT3P(lwb->lwb_buf, !=, NULL);
1619
1620 zil_lwb_write_open(zilog, lwb);
428870ff 1621
1ce23dca
PS
1622 lrc = &itx->itx_lr;
1623 lrw = (lr_write_t *)lrc;
1624
1625 /*
1626 * A commit itx doesn't represent any on-disk state; instead
1627 * it's simply used as a place holder on the commit list, and
1628 * provides a mechanism for attaching a "commit waiter" onto the
1629 * correct lwb (such that the waiter can be signalled upon
1630 * completion of that lwb). Thus, we don't process this itx's
1631 * log record if it's a commit itx (these itx's don't have log
1632 * records), and instead link the itx's waiter onto the lwb's
1633 * list of waiters.
1634 *
1635 * For more details, see the comment above zil_commit().
1636 */
1637 if (lrc->lrc_txtype == TX_COMMIT) {
2fe61a7e 1638 mutex_enter(&zilog->zl_lock);
1ce23dca
PS
1639 zil_commit_waiter_link_lwb(itx->itx_private, lwb);
1640 itx->itx_private = NULL;
2fe61a7e 1641 mutex_exit(&zilog->zl_lock);
1ce23dca
PS
1642 return (lwb);
1643 }
34dc7c2f 1644
1b7c1e5c 1645 if (lrc->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY) {
34dc7c2f 1646 dlen = P2ROUNDUP_TYPED(
428870ff 1647 lrw->lr_length, sizeof (uint64_t), uint64_t);
1b7c1e5c
GDN
1648 } else {
1649 dlen = 0;
1650 }
1651 reclen = lrc->lrc_reclen;
34dc7c2f 1652 zilog->zl_cur_used += (reclen + dlen);
1b7c1e5c 1653 txg = lrc->lrc_txg;
34dc7c2f 1654
1ce23dca 1655 ASSERT3U(zilog->zl_cur_used, <, UINT64_MAX - (reclen + dlen));
34dc7c2f 1656
1b7c1e5c 1657cont:
34dc7c2f
BB
1658 /*
1659 * If this record won't fit in the current log block, start a new one.
1b7c1e5c 1660 * For WR_NEED_COPY optimize layout for minimal number of chunks.
34dc7c2f 1661 */
1b7c1e5c 1662 lwb_sp = lwb->lwb_sz - lwb->lwb_nused;
b8738257 1663 max_log_data = zil_max_log_data(zilog);
1b7c1e5c 1664 if (reclen > lwb_sp || (reclen + dlen > lwb_sp &&
b8738257
MA
1665 lwb_sp < zil_max_waste_space(zilog) &&
1666 (dlen % max_log_data == 0 ||
1667 lwb_sp < reclen + dlen % max_log_data))) {
1ce23dca 1668 lwb = zil_lwb_write_issue(zilog, lwb);
34dc7c2f
BB
1669 if (lwb == NULL)
1670 return (NULL);
1ce23dca 1671 zil_lwb_write_open(zilog, lwb);
428870ff 1672 ASSERT(LWB_EMPTY(lwb));
1b7c1e5c 1673 lwb_sp = lwb->lwb_sz - lwb->lwb_nused;
b8738257
MA
1674
1675 /*
1676 * There must be enough space in the new, empty log block to
1677 * hold reclen. For WR_COPIED, we need to fit the whole
1678 * record in one block, and reclen is the header size + the
1679 * data size. For WR_NEED_COPY, we can create multiple
1680 * records, splitting the data into multiple blocks, so we
1681 * only need to fit one word of data per block; in this case
1682 * reclen is just the header size (no data).
1683 */
1b7c1e5c 1684 ASSERT3U(reclen + MIN(dlen, sizeof (uint64_t)), <=, lwb_sp);
34dc7c2f
BB
1685 }
1686
1b7c1e5c 1687 dnow = MIN(dlen, lwb_sp - reclen);
428870ff
BB
1688 lr_buf = lwb->lwb_buf + lwb->lwb_nused;
1689 bcopy(lrc, lr_buf, reclen);
1b7c1e5c
GDN
1690 lrcb = (lr_t *)lr_buf; /* Like lrc, but inside lwb. */
1691 lrwb = (lr_write_t *)lrcb; /* Like lrw, but inside lwb. */
34dc7c2f 1692
b6ad9671
ED
1693 ZIL_STAT_BUMP(zil_itx_count);
1694
34dc7c2f
BB
1695 /*
1696 * If it's a write, fetch the data or get its blkptr as appropriate.
1697 */
1698 if (lrc->lrc_txtype == TX_WRITE) {
1699 if (txg > spa_freeze_txg(zilog->zl_spa))
1700 txg_wait_synced(zilog->zl_dmu_pool, txg);
b6ad9671
ED
1701 if (itx->itx_wr_state == WR_COPIED) {
1702 ZIL_STAT_BUMP(zil_itx_copied_count);
1703 ZIL_STAT_INCR(zil_itx_copied_bytes, lrw->lr_length);
1704 } else {
34dc7c2f
BB
1705 char *dbuf;
1706 int error;
1707
1b7c1e5c 1708 if (itx->itx_wr_state == WR_NEED_COPY) {
428870ff 1709 dbuf = lr_buf + reclen;
1b7c1e5c
GDN
1710 lrcb->lrc_reclen += dnow;
1711 if (lrwb->lr_length > dnow)
1712 lrwb->lr_length = dnow;
1713 lrw->lr_offset += dnow;
1714 lrw->lr_length -= dnow;
b6ad9671 1715 ZIL_STAT_BUMP(zil_itx_needcopy_count);
5666a994 1716 ZIL_STAT_INCR(zil_itx_needcopy_bytes, dnow);
34dc7c2f 1717 } else {
1ce23dca 1718 ASSERT3S(itx->itx_wr_state, ==, WR_INDIRECT);
34dc7c2f 1719 dbuf = NULL;
b6ad9671 1720 ZIL_STAT_BUMP(zil_itx_indirect_count);
d1d7e268
MK
1721 ZIL_STAT_INCR(zil_itx_indirect_bytes,
1722 lrw->lr_length);
34dc7c2f 1723 }
1ce23dca
PS
1724
1725 /*
1726 * We pass in the "lwb_write_zio" rather than
1727 * "lwb_root_zio" so that the "lwb_write_zio"
1728 * becomes the parent of any zio's created by
1729 * the "zl_get_data" callback. The vdevs are
1730 * flushed after the "lwb_write_zio" completes,
1731 * so we want to make sure that completion
1732 * callback waits for these additional zio's,
1733 * such that the vdevs used by those zio's will
1734 * be included in the lwb's vdev tree, and those
1735 * vdevs will be properly flushed. If we passed
1736 * in "lwb_root_zio" here, then these additional
1737 * vdevs may not be flushed; e.g. if these zio's
1738 * completed after "lwb_write_zio" completed.
1739 */
1740 error = zilog->zl_get_data(itx->itx_private,
1741 lrwb, dbuf, lwb, lwb->lwb_write_zio);
1742
45d1cae3
BB
1743 if (error == EIO) {
1744 txg_wait_synced(zilog->zl_dmu_pool, txg);
1745 return (lwb);
1746 }
13fe0198 1747 if (error != 0) {
34dc7c2f
BB
1748 ASSERT(error == ENOENT || error == EEXIST ||
1749 error == EALREADY);
1750 return (lwb);
1751 }
1752 }
1753 }
1754
428870ff
BB
1755 /*
1756 * We're actually making an entry, so update lrc_seq to be the
1757 * log record sequence number. Note that this is generally not
1758 * equal to the itx sequence number because not all transactions
1759 * are synchronous, and sometimes spa_sync() gets there first.
1760 */
1ce23dca 1761 lrcb->lrc_seq = ++zilog->zl_lr_seq;
1b7c1e5c 1762 lwb->lwb_nused += reclen + dnow;
1ce23dca
PS
1763
1764 zil_lwb_add_txg(lwb, txg);
1765
428870ff 1766 ASSERT3U(lwb->lwb_nused, <=, lwb->lwb_sz);
c99c9001 1767 ASSERT0(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)));
34dc7c2f 1768
1b7c1e5c
GDN
1769 dlen -= dnow;
1770 if (dlen > 0) {
1771 zilog->zl_cur_used += reclen;
1772 goto cont;
1773 }
1774
34dc7c2f
BB
1775 return (lwb);
1776}
1777
1778itx_t *
1779zil_itx_create(uint64_t txtype, size_t lrsize)
1780{
72841b9f 1781 size_t itxsize;
34dc7c2f
BB
1782 itx_t *itx;
1783
1784 lrsize = P2ROUNDUP_TYPED(lrsize, sizeof (uint64_t), size_t);
72841b9f 1785 itxsize = offsetof(itx_t, itx_lr) + lrsize;
34dc7c2f 1786
72841b9f 1787 itx = zio_data_buf_alloc(itxsize);
34dc7c2f
BB
1788 itx->itx_lr.lrc_txtype = txtype;
1789 itx->itx_lr.lrc_reclen = lrsize;
34dc7c2f 1790 itx->itx_lr.lrc_seq = 0; /* defensive */
572e2857 1791 itx->itx_sync = B_TRUE; /* default is synchronous */
119a394a
ED
1792 itx->itx_callback = NULL;
1793 itx->itx_callback_data = NULL;
72841b9f 1794 itx->itx_size = itxsize;
34dc7c2f
BB
1795
1796 return (itx);
1797}
1798
428870ff
BB
1799void
1800zil_itx_destroy(itx_t *itx)
1801{
1ce23dca
PS
1802 IMPLY(itx->itx_lr.lrc_txtype == TX_COMMIT, itx->itx_callback == NULL);
1803 IMPLY(itx->itx_callback != NULL, itx->itx_lr.lrc_txtype != TX_COMMIT);
1804
1805 if (itx->itx_callback != NULL)
1806 itx->itx_callback(itx->itx_callback_data);
1807
72841b9f 1808 zio_data_buf_free(itx, itx->itx_size);
428870ff
BB
1809}
1810
572e2857
BB
1811/*
1812 * Free up the sync and async itxs. The itxs_t has already been detached
1813 * so no locks are needed.
1814 */
1815static void
1816zil_itxg_clean(itxs_t *itxs)
34dc7c2f 1817{
572e2857
BB
1818 itx_t *itx;
1819 list_t *list;
1820 avl_tree_t *t;
1821 void *cookie;
1822 itx_async_node_t *ian;
1823
1824 list = &itxs->i_sync_list;
1825 while ((itx = list_head(list)) != NULL) {
1ce23dca
PS
1826 /*
1827 * In the general case, commit itxs will not be found
1828 * here, as they'll be committed to an lwb via
1829 * zil_lwb_commit(), and free'd in that function. Having
1830 * said that, it is still possible for commit itxs to be
1831 * found here, due to the following race:
1832 *
1833 * - a thread calls zil_commit() which assigns the
1834 * commit itx to a per-txg i_sync_list
1835 * - zil_itxg_clean() is called (e.g. via spa_sync())
1836 * while the waiter is still on the i_sync_list
1837 *
1838 * There's nothing to prevent syncing the txg while the
1839 * waiter is on the i_sync_list. This normally doesn't
1840 * happen because spa_sync() is slower than zil_commit(),
1841 * but if zil_commit() calls txg_wait_synced() (e.g.
1842 * because zil_create() or zil_commit_writer_stall() is
1843 * called) we will hit this case.
1844 */
1845 if (itx->itx_lr.lrc_txtype == TX_COMMIT)
1846 zil_commit_waiter_skip(itx->itx_private);
1847
572e2857 1848 list_remove(list, itx);
19ea3d25 1849 zil_itx_destroy(itx);
572e2857 1850 }
34dc7c2f 1851
572e2857
BB
1852 cookie = NULL;
1853 t = &itxs->i_async_tree;
1854 while ((ian = avl_destroy_nodes(t, &cookie)) != NULL) {
1855 list = &ian->ia_list;
1856 while ((itx = list_head(list)) != NULL) {
1857 list_remove(list, itx);
1ce23dca
PS
1858 /* commit itxs should never be on the async lists. */
1859 ASSERT3U(itx->itx_lr.lrc_txtype, !=, TX_COMMIT);
19ea3d25 1860 zil_itx_destroy(itx);
572e2857
BB
1861 }
1862 list_destroy(list);
1863 kmem_free(ian, sizeof (itx_async_node_t));
1864 }
1865 avl_destroy(t);
34dc7c2f 1866
572e2857
BB
1867 kmem_free(itxs, sizeof (itxs_t));
1868}
34dc7c2f 1869
572e2857
BB
1870static int
1871zil_aitx_compare(const void *x1, const void *x2)
1872{
1873 const uint64_t o1 = ((itx_async_node_t *)x1)->ia_foid;
1874 const uint64_t o2 = ((itx_async_node_t *)x2)->ia_foid;
1875
ca577779 1876 return (TREE_CMP(o1, o2));
34dc7c2f
BB
1877}
1878
1879/*
572e2857 1880 * Remove all async itx with the given oid.
34dc7c2f 1881 */
8e556c5e 1882void
572e2857 1883zil_remove_async(zilog_t *zilog, uint64_t oid)
34dc7c2f 1884{
572e2857
BB
1885 uint64_t otxg, txg;
1886 itx_async_node_t *ian;
1887 avl_tree_t *t;
1888 avl_index_t where;
34dc7c2f
BB
1889 list_t clean_list;
1890 itx_t *itx;
1891
572e2857 1892 ASSERT(oid != 0);
34dc7c2f
BB
1893 list_create(&clean_list, sizeof (itx_t), offsetof(itx_t, itx_node));
1894
572e2857
BB
1895 if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */
1896 otxg = ZILTEST_TXG;
1897 else
1898 otxg = spa_last_synced_txg(zilog->zl_spa) + 1;
34dc7c2f 1899
572e2857
BB
1900 for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) {
1901 itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK];
1902
1903 mutex_enter(&itxg->itxg_lock);
1904 if (itxg->itxg_txg != txg) {
1905 mutex_exit(&itxg->itxg_lock);
1906 continue;
1907 }
34dc7c2f 1908
572e2857
BB
1909 /*
1910 * Locate the object node and append its list.
1911 */
1912 t = &itxg->itxg_itxs->i_async_tree;
1913 ian = avl_find(t, &oid, &where);
1914 if (ian != NULL)
1915 list_move_tail(&clean_list, &ian->ia_list);
1916 mutex_exit(&itxg->itxg_lock);
1917 }
34dc7c2f
BB
1918 while ((itx = list_head(&clean_list)) != NULL) {
1919 list_remove(&clean_list, itx);
1ce23dca
PS
1920 /* commit itxs should never be on the async lists. */
1921 ASSERT3U(itx->itx_lr.lrc_txtype, !=, TX_COMMIT);
19ea3d25 1922 zil_itx_destroy(itx);
34dc7c2f
BB
1923 }
1924 list_destroy(&clean_list);
1925}
1926
572e2857
BB
1927void
1928zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx)
1929{
1930 uint64_t txg;
1931 itxg_t *itxg;
1932 itxs_t *itxs, *clean = NULL;
1933
572e2857
BB
1934 /*
1935 * Ensure the data of a renamed file is committed before the rename.
1936 */
1937 if ((itx->itx_lr.lrc_txtype & ~TX_CI) == TX_RENAME)
1938 zil_async_to_sync(zilog, itx->itx_oid);
1939
29809a6c 1940 if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX)
572e2857
BB
1941 txg = ZILTEST_TXG;
1942 else
1943 txg = dmu_tx_get_txg(tx);
1944
1945 itxg = &zilog->zl_itxg[txg & TXG_MASK];
1946 mutex_enter(&itxg->itxg_lock);
1947 itxs = itxg->itxg_itxs;
1948 if (itxg->itxg_txg != txg) {
1949 if (itxs != NULL) {
1950 /*
1951 * The zil_clean callback hasn't got around to cleaning
1952 * this itxg. Save the itxs for release below.
1953 * This should be rare.
1954 */
55922e73
GW
1955 zfs_dbgmsg("zil_itx_assign: missed itx cleanup for "
1956 "txg %llu", itxg->itxg_txg);
572e2857
BB
1957 clean = itxg->itxg_itxs;
1958 }
572e2857 1959 itxg->itxg_txg = txg;
d1d7e268 1960 itxs = itxg->itxg_itxs = kmem_zalloc(sizeof (itxs_t),
79c76d5b 1961 KM_SLEEP);
572e2857
BB
1962
1963 list_create(&itxs->i_sync_list, sizeof (itx_t),
1964 offsetof(itx_t, itx_node));
1965 avl_create(&itxs->i_async_tree, zil_aitx_compare,
1966 sizeof (itx_async_node_t),
1967 offsetof(itx_async_node_t, ia_node));
1968 }
1969 if (itx->itx_sync) {
1970 list_insert_tail(&itxs->i_sync_list, itx);
572e2857
BB
1971 } else {
1972 avl_tree_t *t = &itxs->i_async_tree;
50c957f7
NB
1973 uint64_t foid =
1974 LR_FOID_GET_OBJ(((lr_ooo_t *)&itx->itx_lr)->lr_foid);
572e2857
BB
1975 itx_async_node_t *ian;
1976 avl_index_t where;
1977
1978 ian = avl_find(t, &foid, &where);
1979 if (ian == NULL) {
d1d7e268 1980 ian = kmem_alloc(sizeof (itx_async_node_t),
79c76d5b 1981 KM_SLEEP);
572e2857
BB
1982 list_create(&ian->ia_list, sizeof (itx_t),
1983 offsetof(itx_t, itx_node));
1984 ian->ia_foid = foid;
1985 avl_insert(t, ian, where);
1986 }
1987 list_insert_tail(&ian->ia_list, itx);
1988 }
1989
1990 itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx);
1ce23dca
PS
1991
1992 /*
1993 * We don't want to dirty the ZIL using ZILTEST_TXG, because
1994 * zil_clean() will never be called using ZILTEST_TXG. Thus, we
1995 * need to be careful to always dirty the ZIL using the "real"
1996 * TXG (not itxg_txg) even when the SPA is frozen.
1997 */
1998 zilog_dirty(zilog, dmu_tx_get_txg(tx));
572e2857
BB
1999 mutex_exit(&itxg->itxg_lock);
2000
2001 /* Release the old itxs now we've dropped the lock */
2002 if (clean != NULL)
2003 zil_itxg_clean(clean);
2004}
2005
34dc7c2f
BB
2006/*
2007 * If there are any in-memory intent log transactions which have now been
29809a6c 2008 * synced then start up a taskq to free them. We should only do this after we
e1cfd73f 2009 * have written out the uberblocks (i.e. txg has been committed) so that
29809a6c
MA
2010 * don't inadvertently clean out in-memory log records that would be required
2011 * by zil_commit().
34dc7c2f
BB
2012 */
2013void
572e2857 2014zil_clean(zilog_t *zilog, uint64_t synced_txg)
34dc7c2f 2015{
572e2857
BB
2016 itxg_t *itxg = &zilog->zl_itxg[synced_txg & TXG_MASK];
2017 itxs_t *clean_me;
34dc7c2f 2018
1ce23dca
PS
2019 ASSERT3U(synced_txg, <, ZILTEST_TXG);
2020
572e2857
BB
2021 mutex_enter(&itxg->itxg_lock);
2022 if (itxg->itxg_itxs == NULL || itxg->itxg_txg == ZILTEST_TXG) {
2023 mutex_exit(&itxg->itxg_lock);
2024 return;
2025 }
2026 ASSERT3U(itxg->itxg_txg, <=, synced_txg);
a032ac4b 2027 ASSERT3U(itxg->itxg_txg, !=, 0);
572e2857
BB
2028 clean_me = itxg->itxg_itxs;
2029 itxg->itxg_itxs = NULL;
2030 itxg->itxg_txg = 0;
2031 mutex_exit(&itxg->itxg_lock);
2032 /*
2033 * Preferably start a task queue to free up the old itxs but
2034 * if taskq_dispatch can't allocate resources to do that then
2035 * free it in-line. This should be rare. Note, using TQ_SLEEP
2036 * created a bad performance problem.
2037 */
a032ac4b
BB
2038 ASSERT3P(zilog->zl_dmu_pool, !=, NULL);
2039 ASSERT3P(zilog->zl_dmu_pool->dp_zil_clean_taskq, !=, NULL);
2040 taskqid_t id = taskq_dispatch(zilog->zl_dmu_pool->dp_zil_clean_taskq,
2041 (void (*)(void *))zil_itxg_clean, clean_me, TQ_NOSLEEP);
2042 if (id == TASKQID_INVALID)
572e2857
BB
2043 zil_itxg_clean(clean_me);
2044}
2045
2046/*
1ce23dca
PS
2047 * This function will traverse the queue of itxs that need to be
2048 * committed, and move them onto the ZIL's zl_itx_commit_list.
572e2857
BB
2049 */
2050static void
2051zil_get_commit_list(zilog_t *zilog)
2052{
2053 uint64_t otxg, txg;
2054 list_t *commit_list = &zilog->zl_itx_commit_list;
572e2857 2055
1b2b0aca 2056 ASSERT(MUTEX_HELD(&zilog->zl_issuer_lock));
1ce23dca 2057
572e2857
BB
2058 if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */
2059 otxg = ZILTEST_TXG;
2060 else
2061 otxg = spa_last_synced_txg(zilog->zl_spa) + 1;
2062
55922e73
GW
2063 /*
2064 * This is inherently racy, since there is nothing to prevent
2065 * the last synced txg from changing. That's okay since we'll
2066 * only commit things in the future.
2067 */
572e2857
BB
2068 for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) {
2069 itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK];
2070
2071 mutex_enter(&itxg->itxg_lock);
2072 if (itxg->itxg_txg != txg) {
2073 mutex_exit(&itxg->itxg_lock);
2074 continue;
2075 }
2076
55922e73
GW
2077 /*
2078 * If we're adding itx records to the zl_itx_commit_list,
2079 * then the zil better be dirty in this "txg". We can assert
2080 * that here since we're holding the itxg_lock which will
2081 * prevent spa_sync from cleaning it. Once we add the itxs
2082 * to the zl_itx_commit_list we must commit it to disk even
2083 * if it's unnecessary (i.e. the txg was synced).
2084 */
2085 ASSERT(zilog_is_dirty_in_txg(zilog, txg) ||
2086 spa_freeze_txg(zilog->zl_spa) != UINT64_MAX);
572e2857 2087 list_move_tail(commit_list, &itxg->itxg_itxs->i_sync_list);
572e2857
BB
2088
2089 mutex_exit(&itxg->itxg_lock);
2090 }
572e2857
BB
2091}
2092
2093/*
2094 * Move the async itxs for a specified object to commit into sync lists.
2095 */
eedb3a62 2096void
572e2857
BB
2097zil_async_to_sync(zilog_t *zilog, uint64_t foid)
2098{
2099 uint64_t otxg, txg;
2100 itx_async_node_t *ian;
2101 avl_tree_t *t;
2102 avl_index_t where;
2103
2104 if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */
2105 otxg = ZILTEST_TXG;
2106 else
2107 otxg = spa_last_synced_txg(zilog->zl_spa) + 1;
2108
55922e73
GW
2109 /*
2110 * This is inherently racy, since there is nothing to prevent
2111 * the last synced txg from changing.
2112 */
572e2857
BB
2113 for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) {
2114 itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK];
2115
2116 mutex_enter(&itxg->itxg_lock);
2117 if (itxg->itxg_txg != txg) {
2118 mutex_exit(&itxg->itxg_lock);
2119 continue;
2120 }
2121
2122 /*
2123 * If a foid is specified then find that node and append its
2124 * list. Otherwise walk the tree appending all the lists
2125 * to the sync list. We add to the end rather than the
2126 * beginning to ensure the create has happened.
2127 */
2128 t = &itxg->itxg_itxs->i_async_tree;
2129 if (foid != 0) {
2130 ian = avl_find(t, &foid, &where);
2131 if (ian != NULL) {
2132 list_move_tail(&itxg->itxg_itxs->i_sync_list,
2133 &ian->ia_list);
2134 }
2135 } else {
2136 void *cookie = NULL;
2137
2138 while ((ian = avl_destroy_nodes(t, &cookie)) != NULL) {
2139 list_move_tail(&itxg->itxg_itxs->i_sync_list,
2140 &ian->ia_list);
2141 list_destroy(&ian->ia_list);
2142 kmem_free(ian, sizeof (itx_async_node_t));
2143 }
2144 }
2145 mutex_exit(&itxg->itxg_lock);
34dc7c2f 2146 }
34dc7c2f
BB
2147}
2148
1ce23dca
PS
2149/*
2150 * This function will prune commit itxs that are at the head of the
2151 * commit list (it won't prune past the first non-commit itx), and
2152 * either: a) attach them to the last lwb that's still pending
2153 * completion, or b) skip them altogether.
2154 *
2155 * This is used as a performance optimization to prevent commit itxs
2156 * from generating new lwbs when it's unnecessary to do so.
2157 */
b128c09f 2158static void
1ce23dca 2159zil_prune_commit_list(zilog_t *zilog)
34dc7c2f 2160{
572e2857 2161 itx_t *itx;
34dc7c2f 2162
1b2b0aca 2163 ASSERT(MUTEX_HELD(&zilog->zl_issuer_lock));
572e2857 2164
1ce23dca
PS
2165 while ((itx = list_head(&zilog->zl_itx_commit_list)) != NULL) {
2166 lr_t *lrc = &itx->itx_lr;
2167 if (lrc->lrc_txtype != TX_COMMIT)
2168 break;
572e2857 2169
1ce23dca
PS
2170 mutex_enter(&zilog->zl_lock);
2171
2172 lwb_t *last_lwb = zilog->zl_last_lwb_opened;
900d09b2
PS
2173 if (last_lwb == NULL ||
2174 last_lwb->lwb_state == LWB_STATE_FLUSH_DONE) {
1ce23dca
PS
2175 /*
2176 * All of the itxs this waiter was waiting on
2177 * must have already completed (or there were
2178 * never any itx's for it to wait on), so it's
2179 * safe to skip this waiter and mark it done.
2180 */
2181 zil_commit_waiter_skip(itx->itx_private);
2182 } else {
2183 zil_commit_waiter_link_lwb(itx->itx_private, last_lwb);
2184 itx->itx_private = NULL;
2185 }
2186
2187 mutex_exit(&zilog->zl_lock);
2188
2189 list_remove(&zilog->zl_itx_commit_list, itx);
2190 zil_itx_destroy(itx);
2191 }
2192
2193 IMPLY(itx != NULL, itx->itx_lr.lrc_txtype != TX_COMMIT);
2194}
2195
2196static void
2197zil_commit_writer_stall(zilog_t *zilog)
2198{
2199 /*
2200 * When zio_alloc_zil() fails to allocate the next lwb block on
2201 * disk, we must call txg_wait_synced() to ensure all of the
2202 * lwbs in the zilog's zl_lwb_list are synced and then freed (in
2203 * zil_sync()), such that any subsequent ZIL writer (i.e. a call
2204 * to zil_process_commit_list()) will have to call zil_create(),
2205 * and start a new ZIL chain.
2206 *
2207 * Since zil_alloc_zil() failed, the lwb that was previously
2208 * issued does not have a pointer to the "next" lwb on disk.
2209 * Thus, if another ZIL writer thread was to allocate the "next"
2210 * on-disk lwb, that block could be leaked in the event of a
2211 * crash (because the previous lwb on-disk would not point to
2212 * it).
2213 *
1b2b0aca 2214 * We must hold the zilog's zl_issuer_lock while we do this, to
1ce23dca
PS
2215 * ensure no new threads enter zil_process_commit_list() until
2216 * all lwb's in the zl_lwb_list have been synced and freed
2217 * (which is achieved via the txg_wait_synced() call).
2218 */
1b2b0aca 2219 ASSERT(MUTEX_HELD(&zilog->zl_issuer_lock));
1ce23dca
PS
2220 txg_wait_synced(zilog->zl_dmu_pool, 0);
2221 ASSERT3P(list_tail(&zilog->zl_lwb_list), ==, NULL);
2222}
2223
2224/*
2225 * This function will traverse the commit list, creating new lwbs as
2226 * needed, and committing the itxs from the commit list to these newly
2227 * created lwbs. Additionally, as a new lwb is created, the previous
2228 * lwb will be issued to the zio layer to be written to disk.
2229 */
2230static void
2231zil_process_commit_list(zilog_t *zilog)
2232{
2233 spa_t *spa = zilog->zl_spa;
2234 list_t nolwb_itxs;
2235 list_t nolwb_waiters;
2236 lwb_t *lwb;
2237 itx_t *itx;
2238
1b2b0aca 2239 ASSERT(MUTEX_HELD(&zilog->zl_issuer_lock));
572e2857
BB
2240
2241 /*
2242 * Return if there's nothing to commit before we dirty the fs by
2243 * calling zil_create().
2244 */
1ce23dca 2245 if (list_head(&zilog->zl_itx_commit_list) == NULL)
572e2857 2246 return;
34dc7c2f 2247
1ce23dca
PS
2248 list_create(&nolwb_itxs, sizeof (itx_t), offsetof(itx_t, itx_node));
2249 list_create(&nolwb_waiters, sizeof (zil_commit_waiter_t),
2250 offsetof(zil_commit_waiter_t, zcw_node));
2251
2252 lwb = list_tail(&zilog->zl_lwb_list);
2253 if (lwb == NULL) {
2254 lwb = zil_create(zilog);
34dc7c2f 2255 } else {
1ce23dca 2256 ASSERT3S(lwb->lwb_state, !=, LWB_STATE_ISSUED);
900d09b2
PS
2257 ASSERT3S(lwb->lwb_state, !=, LWB_STATE_WRITE_DONE);
2258 ASSERT3S(lwb->lwb_state, !=, LWB_STATE_FLUSH_DONE);
34dc7c2f
BB
2259 }
2260
1ce23dca
PS
2261 while ((itx = list_head(&zilog->zl_itx_commit_list)) != NULL) {
2262 lr_t *lrc = &itx->itx_lr;
2263 uint64_t txg = lrc->lrc_txg;
2264
55922e73 2265 ASSERT3U(txg, !=, 0);
34dc7c2f 2266
1ce23dca
PS
2267 if (lrc->lrc_txtype == TX_COMMIT) {
2268 DTRACE_PROBE2(zil__process__commit__itx,
2269 zilog_t *, zilog, itx_t *, itx);
2270 } else {
2271 DTRACE_PROBE2(zil__process__normal__itx,
2272 zilog_t *, zilog, itx_t *, itx);
2273 }
2274
2275 list_remove(&zilog->zl_itx_commit_list, itx);
2276
1ce23dca
PS
2277 boolean_t synced = txg <= spa_last_synced_txg(spa);
2278 boolean_t frozen = txg > spa_freeze_txg(spa);
2279
2fe61a7e
PS
2280 /*
2281 * If the txg of this itx has already been synced out, then
2282 * we don't need to commit this itx to an lwb. This is
2283 * because the data of this itx will have already been
2284 * written to the main pool. This is inherently racy, and
2285 * it's still ok to commit an itx whose txg has already
2286 * been synced; this will result in a write that's
2287 * unnecessary, but will do no harm.
2288 *
2289 * With that said, we always want to commit TX_COMMIT itxs
2290 * to an lwb, regardless of whether or not that itx's txg
2291 * has been synced out. We do this to ensure any OPENED lwb
2292 * will always have at least one zil_commit_waiter_t linked
2293 * to the lwb.
2294 *
2295 * As a counter-example, if we skipped TX_COMMIT itx's
2296 * whose txg had already been synced, the following
2297 * situation could occur if we happened to be racing with
2298 * spa_sync:
2299 *
2300 * 1. We commit a non-TX_COMMIT itx to an lwb, where the
2301 * itx's txg is 10 and the last synced txg is 9.
2302 * 2. spa_sync finishes syncing out txg 10.
2303 * 3. We move to the next itx in the list, it's a TX_COMMIT
2304 * whose txg is 10, so we skip it rather than committing
2305 * it to the lwb used in (1).
2306 *
2307 * If the itx that is skipped in (3) is the last TX_COMMIT
2308 * itx in the commit list, than it's possible for the lwb
2309 * used in (1) to remain in the OPENED state indefinitely.
2310 *
2311 * To prevent the above scenario from occurring, ensuring
2312 * that once an lwb is OPENED it will transition to ISSUED
2313 * and eventually DONE, we always commit TX_COMMIT itx's to
2314 * an lwb here, even if that itx's txg has already been
2315 * synced.
2316 *
2317 * Finally, if the pool is frozen, we _always_ commit the
2318 * itx. The point of freezing the pool is to prevent data
2319 * from being written to the main pool via spa_sync, and
2320 * instead rely solely on the ZIL to persistently store the
2321 * data; i.e. when the pool is frozen, the last synced txg
2322 * value can't be trusted.
2323 */
2324 if (frozen || !synced || lrc->lrc_txtype == TX_COMMIT) {
1ce23dca
PS
2325 if (lwb != NULL) {
2326 lwb = zil_lwb_commit(zilog, itx, lwb);
2327
2328 if (lwb == NULL)
2329 list_insert_tail(&nolwb_itxs, itx);
2330 else
2331 list_insert_tail(&lwb->lwb_itxs, itx);
2332 } else {
2333 if (lrc->lrc_txtype == TX_COMMIT) {
2334 zil_commit_waiter_link_nolwb(
2335 itx->itx_private, &nolwb_waiters);
2336 }
2337
2338 list_insert_tail(&nolwb_itxs, itx);
2339 }
2340 } else {
2fe61a7e 2341 ASSERT3S(lrc->lrc_txtype, !=, TX_COMMIT);
1ce23dca
PS
2342 zil_itx_destroy(itx);
2343 }
34dc7c2f 2344 }
34dc7c2f 2345
1ce23dca
PS
2346 if (lwb == NULL) {
2347 /*
2348 * This indicates zio_alloc_zil() failed to allocate the
2349 * "next" lwb on-disk. When this happens, we must stall
2350 * the ZIL write pipeline; see the comment within
2351 * zil_commit_writer_stall() for more details.
2352 */
2353 zil_commit_writer_stall(zilog);
34dc7c2f 2354
1ce23dca
PS
2355 /*
2356 * Additionally, we have to signal and mark the "nolwb"
2357 * waiters as "done" here, since without an lwb, we
2358 * can't do this via zil_lwb_flush_vdevs_done() like
2359 * normal.
2360 */
2361 zil_commit_waiter_t *zcw;
2362 while ((zcw = list_head(&nolwb_waiters)) != NULL) {
2363 zil_commit_waiter_skip(zcw);
2364 list_remove(&nolwb_waiters, zcw);
2365 }
2366
2367 /*
2368 * And finally, we have to destroy the itx's that
2369 * couldn't be committed to an lwb; this will also call
2370 * the itx's callback if one exists for the itx.
2371 */
2372 while ((itx = list_head(&nolwb_itxs)) != NULL) {
2373 list_remove(&nolwb_itxs, itx);
2374 zil_itx_destroy(itx);
2375 }
2376 } else {
2377 ASSERT(list_is_empty(&nolwb_waiters));
2378 ASSERT3P(lwb, !=, NULL);
2379 ASSERT3S(lwb->lwb_state, !=, LWB_STATE_ISSUED);
900d09b2
PS
2380 ASSERT3S(lwb->lwb_state, !=, LWB_STATE_WRITE_DONE);
2381 ASSERT3S(lwb->lwb_state, !=, LWB_STATE_FLUSH_DONE);
1ce23dca
PS
2382
2383 /*
2384 * At this point, the ZIL block pointed at by the "lwb"
2385 * variable is in one of the following states: "closed"
2386 * or "open".
2387 *
2fe61a7e
PS
2388 * If it's "closed", then no itxs have been committed to
2389 * it, so there's no point in issuing its zio (i.e. it's
2390 * "empty").
1ce23dca 2391 *
2fe61a7e
PS
2392 * If it's "open", then it contains one or more itxs that
2393 * eventually need to be committed to stable storage. In
2394 * this case we intentionally do not issue the lwb's zio
2395 * to disk yet, and instead rely on one of the following
2396 * two mechanisms for issuing the zio:
1ce23dca 2397 *
2fe61a7e 2398 * 1. Ideally, there will be more ZIL activity occurring
1ce23dca 2399 * on the system, such that this function will be
2fe61a7e 2400 * immediately called again (not necessarily by the same
1ce23dca
PS
2401 * thread) and this lwb's zio will be issued via
2402 * zil_lwb_commit(). This way, the lwb is guaranteed to
2403 * be "full" when it is issued to disk, and we'll make
2404 * use of the lwb's size the best we can.
2405 *
2fe61a7e 2406 * 2. If there isn't sufficient ZIL activity occurring on
1ce23dca
PS
2407 * the system, such that this lwb's zio isn't issued via
2408 * zil_lwb_commit(), zil_commit_waiter() will issue the
2409 * lwb's zio. If this occurs, the lwb is not guaranteed
2410 * to be "full" by the time its zio is issued, and means
2411 * the size of the lwb was "too large" given the amount
2fe61a7e 2412 * of ZIL activity occurring on the system at that time.
1ce23dca
PS
2413 *
2414 * We do this for a couple of reasons:
2415 *
2416 * 1. To try and reduce the number of IOPs needed to
2417 * write the same number of itxs. If an lwb has space
2fe61a7e 2418 * available in its buffer for more itxs, and more itxs
1ce23dca
PS
2419 * will be committed relatively soon (relative to the
2420 * latency of performing a write), then it's beneficial
2421 * to wait for these "next" itxs. This way, more itxs
2422 * can be committed to stable storage with fewer writes.
2423 *
2424 * 2. To try and use the largest lwb block size that the
2425 * incoming rate of itxs can support. Again, this is to
2426 * try and pack as many itxs into as few lwbs as
2427 * possible, without significantly impacting the latency
2428 * of each individual itx.
2429 */
2430 }
2431}
2432
2433/*
2434 * This function is responsible for ensuring the passed in commit waiter
2435 * (and associated commit itx) is committed to an lwb. If the waiter is
2436 * not already committed to an lwb, all itxs in the zilog's queue of
2437 * itxs will be processed. The assumption is the passed in waiter's
2438 * commit itx will found in the queue just like the other non-commit
2439 * itxs, such that when the entire queue is processed, the waiter will
2fe61a7e 2440 * have been committed to an lwb.
1ce23dca
PS
2441 *
2442 * The lwb associated with the passed in waiter is not guaranteed to
2443 * have been issued by the time this function completes. If the lwb is
2444 * not issued, we rely on future calls to zil_commit_writer() to issue
2445 * the lwb, or the timeout mechanism found in zil_commit_waiter().
2446 */
2447static void
2448zil_commit_writer(zilog_t *zilog, zil_commit_waiter_t *zcw)
2449{
2450 ASSERT(!MUTEX_HELD(&zilog->zl_lock));
2451 ASSERT(spa_writeable(zilog->zl_spa));
1ce23dca 2452
1b2b0aca 2453 mutex_enter(&zilog->zl_issuer_lock);
1ce23dca
PS
2454
2455 if (zcw->zcw_lwb != NULL || zcw->zcw_done) {
2456 /*
2457 * It's possible that, while we were waiting to acquire
1b2b0aca 2458 * the "zl_issuer_lock", another thread committed this
1ce23dca
PS
2459 * waiter to an lwb. If that occurs, we bail out early,
2460 * without processing any of the zilog's queue of itxs.
2461 *
2462 * On certain workloads and system configurations, the
1b2b0aca 2463 * "zl_issuer_lock" can become highly contended. In an
1ce23dca
PS
2464 * attempt to reduce this contention, we immediately drop
2465 * the lock if the waiter has already been processed.
2466 *
2467 * We've measured this optimization to reduce CPU spent
2468 * contending on this lock by up to 5%, using a system
2469 * with 32 CPUs, low latency storage (~50 usec writes),
2470 * and 1024 threads performing sync writes.
2471 */
2472 goto out;
2473 }
2474
2475 ZIL_STAT_BUMP(zil_commit_writer_count);
2476
2477 zil_get_commit_list(zilog);
2478 zil_prune_commit_list(zilog);
2479 zil_process_commit_list(zilog);
2480
2481out:
1b2b0aca 2482 mutex_exit(&zilog->zl_issuer_lock);
1ce23dca
PS
2483}
2484
2485static void
2486zil_commit_waiter_timeout(zilog_t *zilog, zil_commit_waiter_t *zcw)
2487{
1b2b0aca 2488 ASSERT(!MUTEX_HELD(&zilog->zl_issuer_lock));
1ce23dca
PS
2489 ASSERT(MUTEX_HELD(&zcw->zcw_lock));
2490 ASSERT3B(zcw->zcw_done, ==, B_FALSE);
2491
2492 lwb_t *lwb = zcw->zcw_lwb;
2493 ASSERT3P(lwb, !=, NULL);
2494 ASSERT3S(lwb->lwb_state, !=, LWB_STATE_CLOSED);
34dc7c2f
BB
2495
2496 /*
1ce23dca
PS
2497 * If the lwb has already been issued by another thread, we can
2498 * immediately return since there's no work to be done (the
2499 * point of this function is to issue the lwb). Additionally, we
1b2b0aca 2500 * do this prior to acquiring the zl_issuer_lock, to avoid
1ce23dca 2501 * acquiring it when it's not necessary to do so.
34dc7c2f 2502 */
1ce23dca 2503 if (lwb->lwb_state == LWB_STATE_ISSUED ||
900d09b2
PS
2504 lwb->lwb_state == LWB_STATE_WRITE_DONE ||
2505 lwb->lwb_state == LWB_STATE_FLUSH_DONE)
1ce23dca 2506 return;
34dc7c2f 2507
1ce23dca
PS
2508 /*
2509 * In order to call zil_lwb_write_issue() we must hold the
1b2b0aca 2510 * zilog's "zl_issuer_lock". We can't simply acquire that lock,
1ce23dca 2511 * since we're already holding the commit waiter's "zcw_lock",
2fe61a7e 2512 * and those two locks are acquired in the opposite order
1ce23dca
PS
2513 * elsewhere.
2514 */
2515 mutex_exit(&zcw->zcw_lock);
1b2b0aca 2516 mutex_enter(&zilog->zl_issuer_lock);
1ce23dca 2517 mutex_enter(&zcw->zcw_lock);
34dc7c2f 2518
1ce23dca
PS
2519 /*
2520 * Since we just dropped and re-acquired the commit waiter's
2521 * lock, we have to re-check to see if the waiter was marked
2522 * "done" during that process. If the waiter was marked "done",
2523 * the "lwb" pointer is no longer valid (it can be free'd after
2524 * the waiter is marked "done"), so without this check we could
2525 * wind up with a use-after-free error below.
2526 */
2527 if (zcw->zcw_done)
2528 goto out;
119a394a 2529
1ce23dca
PS
2530 ASSERT3P(lwb, ==, zcw->zcw_lwb);
2531
2532 /*
2fe61a7e
PS
2533 * We've already checked this above, but since we hadn't acquired
2534 * the zilog's zl_issuer_lock, we have to perform this check a
2535 * second time while holding the lock.
2536 *
2537 * We don't need to hold the zl_lock since the lwb cannot transition
2538 * from OPENED to ISSUED while we hold the zl_issuer_lock. The lwb
2539 * _can_ transition from ISSUED to DONE, but it's OK to race with
2540 * that transition since we treat the lwb the same, whether it's in
2541 * the ISSUED or DONE states.
2542 *
2543 * The important thing, is we treat the lwb differently depending on
2544 * if it's ISSUED or OPENED, and block any other threads that might
2545 * attempt to issue this lwb. For that reason we hold the
2546 * zl_issuer_lock when checking the lwb_state; we must not call
1ce23dca 2547 * zil_lwb_write_issue() if the lwb had already been issued.
2fe61a7e
PS
2548 *
2549 * See the comment above the lwb_state_t structure definition for
2550 * more details on the lwb states, and locking requirements.
1ce23dca
PS
2551 */
2552 if (lwb->lwb_state == LWB_STATE_ISSUED ||
900d09b2
PS
2553 lwb->lwb_state == LWB_STATE_WRITE_DONE ||
2554 lwb->lwb_state == LWB_STATE_FLUSH_DONE)
1ce23dca
PS
2555 goto out;
2556
2557 ASSERT3S(lwb->lwb_state, ==, LWB_STATE_OPENED);
2558
2559 /*
2560 * As described in the comments above zil_commit_waiter() and
2561 * zil_process_commit_list(), we need to issue this lwb's zio
2562 * since we've reached the commit waiter's timeout and it still
2563 * hasn't been issued.
2564 */
2565 lwb_t *nlwb = zil_lwb_write_issue(zilog, lwb);
2566
ab119165 2567 IMPLY(nlwb != NULL, lwb->lwb_state != LWB_STATE_OPENED);
1ce23dca
PS
2568
2569 /*
2570 * Since the lwb's zio hadn't been issued by the time this thread
2571 * reached its timeout, we reset the zilog's "zl_cur_used" field
2572 * to influence the zil block size selection algorithm.
2573 *
2574 * By having to issue the lwb's zio here, it means the size of the
2575 * lwb was too large, given the incoming throughput of itxs. By
2576 * setting "zl_cur_used" to zero, we communicate this fact to the
2fe61a7e 2577 * block size selection algorithm, so it can take this information
1ce23dca
PS
2578 * into account, and potentially select a smaller size for the
2579 * next lwb block that is allocated.
2580 */
2581 zilog->zl_cur_used = 0;
2582
2583 if (nlwb == NULL) {
2584 /*
2585 * When zil_lwb_write_issue() returns NULL, this
2586 * indicates zio_alloc_zil() failed to allocate the
2587 * "next" lwb on-disk. When this occurs, the ZIL write
2588 * pipeline must be stalled; see the comment within the
2589 * zil_commit_writer_stall() function for more details.
2590 *
2591 * We must drop the commit waiter's lock prior to
2592 * calling zil_commit_writer_stall() or else we can wind
2593 * up with the following deadlock:
2594 *
2595 * - This thread is waiting for the txg to sync while
2596 * holding the waiter's lock; txg_wait_synced() is
2597 * used within txg_commit_writer_stall().
2598 *
2599 * - The txg can't sync because it is waiting for this
2600 * lwb's zio callback to call dmu_tx_commit().
2601 *
2602 * - The lwb's zio callback can't call dmu_tx_commit()
2603 * because it's blocked trying to acquire the waiter's
2604 * lock, which occurs prior to calling dmu_tx_commit()
2605 */
2606 mutex_exit(&zcw->zcw_lock);
2607 zil_commit_writer_stall(zilog);
2608 mutex_enter(&zcw->zcw_lock);
119a394a
ED
2609 }
2610
1ce23dca 2611out:
1b2b0aca 2612 mutex_exit(&zilog->zl_issuer_lock);
1ce23dca
PS
2613 ASSERT(MUTEX_HELD(&zcw->zcw_lock));
2614}
2615
2616/*
2617 * This function is responsible for performing the following two tasks:
2618 *
2619 * 1. its primary responsibility is to block until the given "commit
2620 * waiter" is considered "done".
2621 *
2622 * 2. its secondary responsibility is to issue the zio for the lwb that
2623 * the given "commit waiter" is waiting on, if this function has
2624 * waited "long enough" and the lwb is still in the "open" state.
2625 *
2626 * Given a sufficient amount of itxs being generated and written using
2627 * the ZIL, the lwb's zio will be issued via the zil_lwb_commit()
2628 * function. If this does not occur, this secondary responsibility will
2629 * ensure the lwb is issued even if there is not other synchronous
2630 * activity on the system.
2631 *
2632 * For more details, see zil_process_commit_list(); more specifically,
2633 * the comment at the bottom of that function.
2634 */
2635static void
2636zil_commit_waiter(zilog_t *zilog, zil_commit_waiter_t *zcw)
2637{
2638 ASSERT(!MUTEX_HELD(&zilog->zl_lock));
1b2b0aca 2639 ASSERT(!MUTEX_HELD(&zilog->zl_issuer_lock));
1ce23dca 2640 ASSERT(spa_writeable(zilog->zl_spa));
1ce23dca
PS
2641
2642 mutex_enter(&zcw->zcw_lock);
428870ff
BB
2643
2644 /*
1ce23dca
PS
2645 * The timeout is scaled based on the lwb latency to avoid
2646 * significantly impacting the latency of each individual itx.
2647 * For more details, see the comment at the bottom of the
2648 * zil_process_commit_list() function.
428870ff 2649 */
1ce23dca
PS
2650 int pct = MAX(zfs_commit_timeout_pct, 1);
2651 hrtime_t sleep = (zilog->zl_last_lwb_latency * pct) / 100;
2652 hrtime_t wakeup = gethrtime() + sleep;
2653 boolean_t timedout = B_FALSE;
2654
2655 while (!zcw->zcw_done) {
2656 ASSERT(MUTEX_HELD(&zcw->zcw_lock));
2657
2658 lwb_t *lwb = zcw->zcw_lwb;
2659
2660 /*
2661 * Usually, the waiter will have a non-NULL lwb field here,
2662 * but it's possible for it to be NULL as a result of
2663 * zil_commit() racing with spa_sync().
2664 *
2665 * When zil_clean() is called, it's possible for the itxg
2666 * list (which may be cleaned via a taskq) to contain
2667 * commit itxs. When this occurs, the commit waiters linked
2668 * off of these commit itxs will not be committed to an
2669 * lwb. Additionally, these commit waiters will not be
2670 * marked done until zil_commit_waiter_skip() is called via
2671 * zil_itxg_clean().
2672 *
2673 * Thus, it's possible for this commit waiter (i.e. the
2674 * "zcw" variable) to be found in this "in between" state;
2675 * where it's "zcw_lwb" field is NULL, and it hasn't yet
2676 * been skipped, so it's "zcw_done" field is still B_FALSE.
2677 */
2678 IMPLY(lwb != NULL, lwb->lwb_state != LWB_STATE_CLOSED);
2679
2680 if (lwb != NULL && lwb->lwb_state == LWB_STATE_OPENED) {
2681 ASSERT3B(timedout, ==, B_FALSE);
2682
2683 /*
2684 * If the lwb hasn't been issued yet, then we
2685 * need to wait with a timeout, in case this
2686 * function needs to issue the lwb after the
2687 * timeout is reached; responsibility (2) from
2688 * the comment above this function.
2689 */
8056a756 2690 int rc = cv_timedwait_hires(&zcw->zcw_cv,
1ce23dca
PS
2691 &zcw->zcw_lock, wakeup, USEC2NSEC(1),
2692 CALLOUT_FLAG_ABSOLUTE);
2693
8056a756 2694 if (rc != -1 || zcw->zcw_done)
1ce23dca
PS
2695 continue;
2696
2697 timedout = B_TRUE;
2698 zil_commit_waiter_timeout(zilog, zcw);
2699
2700 if (!zcw->zcw_done) {
2701 /*
2702 * If the commit waiter has already been
2703 * marked "done", it's possible for the
2704 * waiter's lwb structure to have already
2705 * been freed. Thus, we can only reliably
2706 * make these assertions if the waiter
2707 * isn't done.
2708 */
2709 ASSERT3P(lwb, ==, zcw->zcw_lwb);
2710 ASSERT3S(lwb->lwb_state, !=, LWB_STATE_OPENED);
2711 }
2712 } else {
2713 /*
2714 * If the lwb isn't open, then it must have already
2715 * been issued. In that case, there's no need to
2716 * use a timeout when waiting for the lwb to
2717 * complete.
2718 *
2719 * Additionally, if the lwb is NULL, the waiter
2fe61a7e 2720 * will soon be signaled and marked done via
1ce23dca
PS
2721 * zil_clean() and zil_itxg_clean(), so no timeout
2722 * is required.
2723 */
2724
2725 IMPLY(lwb != NULL,
2726 lwb->lwb_state == LWB_STATE_ISSUED ||
900d09b2
PS
2727 lwb->lwb_state == LWB_STATE_WRITE_DONE ||
2728 lwb->lwb_state == LWB_STATE_FLUSH_DONE);
1ce23dca
PS
2729 cv_wait(&zcw->zcw_cv, &zcw->zcw_lock);
2730 }
2731 }
2732
2733 mutex_exit(&zcw->zcw_lock);
2734}
2735
2736static zil_commit_waiter_t *
2737zil_alloc_commit_waiter(void)
2738{
2739 zil_commit_waiter_t *zcw = kmem_cache_alloc(zil_zcw_cache, KM_SLEEP);
2740
2741 cv_init(&zcw->zcw_cv, NULL, CV_DEFAULT, NULL);
2742 mutex_init(&zcw->zcw_lock, NULL, MUTEX_DEFAULT, NULL);
2743 list_link_init(&zcw->zcw_node);
2744 zcw->zcw_lwb = NULL;
2745 zcw->zcw_done = B_FALSE;
2746 zcw->zcw_zio_error = 0;
2747
2748 return (zcw);
2749}
2750
2751static void
2752zil_free_commit_waiter(zil_commit_waiter_t *zcw)
2753{
2754 ASSERT(!list_link_active(&zcw->zcw_node));
2755 ASSERT3P(zcw->zcw_lwb, ==, NULL);
2756 ASSERT3B(zcw->zcw_done, ==, B_TRUE);
2757 mutex_destroy(&zcw->zcw_lock);
2758 cv_destroy(&zcw->zcw_cv);
2759 kmem_cache_free(zil_zcw_cache, zcw);
34dc7c2f
BB
2760}
2761
2762/*
1ce23dca
PS
2763 * This function is used to create a TX_COMMIT itx and assign it. This
2764 * way, it will be linked into the ZIL's list of synchronous itxs, and
2765 * then later committed to an lwb (or skipped) when
2766 * zil_process_commit_list() is called.
2767 */
2768static void
2769zil_commit_itx_assign(zilog_t *zilog, zil_commit_waiter_t *zcw)
2770{
2771 dmu_tx_t *tx = dmu_tx_create(zilog->zl_os);
2772 VERIFY0(dmu_tx_assign(tx, TXG_WAIT));
2773
2774 itx_t *itx = zil_itx_create(TX_COMMIT, sizeof (lr_t));
2775 itx->itx_sync = B_TRUE;
2776 itx->itx_private = zcw;
2777
2778 zil_itx_assign(zilog, itx, tx);
2779
2780 dmu_tx_commit(tx);
2781}
2782
2783/*
2784 * Commit ZFS Intent Log transactions (itxs) to stable storage.
2785 *
2786 * When writing ZIL transactions to the on-disk representation of the
2787 * ZIL, the itxs are committed to a Log Write Block (lwb). Multiple
2788 * itxs can be committed to a single lwb. Once a lwb is written and
2789 * committed to stable storage (i.e. the lwb is written, and vdevs have
2790 * been flushed), each itx that was committed to that lwb is also
2791 * considered to be committed to stable storage.
2792 *
2793 * When an itx is committed to an lwb, the log record (lr_t) contained
2794 * by the itx is copied into the lwb's zio buffer, and once this buffer
2795 * is written to disk, it becomes an on-disk ZIL block.
2796 *
2797 * As itxs are generated, they're inserted into the ZIL's queue of
2798 * uncommitted itxs. The semantics of zil_commit() are such that it will
2799 * block until all itxs that were in the queue when it was called, are
2800 * committed to stable storage.
2801 *
2802 * If "foid" is zero, this means all "synchronous" and "asynchronous"
2803 * itxs, for all objects in the dataset, will be committed to stable
2804 * storage prior to zil_commit() returning. If "foid" is non-zero, all
2805 * "synchronous" itxs for all objects, but only "asynchronous" itxs
2806 * that correspond to the foid passed in, will be committed to stable
2807 * storage prior to zil_commit() returning.
2808 *
2809 * Generally speaking, when zil_commit() is called, the consumer doesn't
2810 * actually care about _all_ of the uncommitted itxs. Instead, they're
2811 * simply trying to waiting for a specific itx to be committed to disk,
2812 * but the interface(s) for interacting with the ZIL don't allow such
2813 * fine-grained communication. A better interface would allow a consumer
2814 * to create and assign an itx, and then pass a reference to this itx to
2815 * zil_commit(); such that zil_commit() would return as soon as that
2816 * specific itx was committed to disk (instead of waiting for _all_
2817 * itxs to be committed).
2818 *
2819 * When a thread calls zil_commit() a special "commit itx" will be
2820 * generated, along with a corresponding "waiter" for this commit itx.
2821 * zil_commit() will wait on this waiter's CV, such that when the waiter
2fe61a7e 2822 * is marked done, and signaled, zil_commit() will return.
1ce23dca
PS
2823 *
2824 * This commit itx is inserted into the queue of uncommitted itxs. This
2825 * provides an easy mechanism for determining which itxs were in the
2826 * queue prior to zil_commit() having been called, and which itxs were
2827 * added after zil_commit() was called.
2828 *
2829 * The commit it is special; it doesn't have any on-disk representation.
2830 * When a commit itx is "committed" to an lwb, the waiter associated
2831 * with it is linked onto the lwb's list of waiters. Then, when that lwb
2fe61a7e 2832 * completes, each waiter on the lwb's list is marked done and signaled
1ce23dca
PS
2833 * -- allowing the thread waiting on the waiter to return from zil_commit().
2834 *
2835 * It's important to point out a few critical factors that allow us
2836 * to make use of the commit itxs, commit waiters, per-lwb lists of
2837 * commit waiters, and zio completion callbacks like we're doing:
572e2857 2838 *
1ce23dca 2839 * 1. The list of waiters for each lwb is traversed, and each commit
2fe61a7e 2840 * waiter is marked "done" and signaled, in the zio completion
1ce23dca 2841 * callback of the lwb's zio[*].
572e2857 2842 *
2fe61a7e 2843 * * Actually, the waiters are signaled in the zio completion
1ce23dca
PS
2844 * callback of the root zio for the DKIOCFLUSHWRITECACHE commands
2845 * that are sent to the vdevs upon completion of the lwb zio.
572e2857 2846 *
1ce23dca
PS
2847 * 2. When the itxs are inserted into the ZIL's queue of uncommitted
2848 * itxs, the order in which they are inserted is preserved[*]; as
2849 * itxs are added to the queue, they are added to the tail of
2850 * in-memory linked lists.
572e2857 2851 *
1ce23dca
PS
2852 * When committing the itxs to lwbs (to be written to disk), they
2853 * are committed in the same order in which the itxs were added to
2854 * the uncommitted queue's linked list(s); i.e. the linked list of
2855 * itxs to commit is traversed from head to tail, and each itx is
2856 * committed to an lwb in that order.
2857 *
2858 * * To clarify:
2859 *
2860 * - the order of "sync" itxs is preserved w.r.t. other
2861 * "sync" itxs, regardless of the corresponding objects.
2862 * - the order of "async" itxs is preserved w.r.t. other
2863 * "async" itxs corresponding to the same object.
2864 * - the order of "async" itxs is *not* preserved w.r.t. other
2865 * "async" itxs corresponding to different objects.
2866 * - the order of "sync" itxs w.r.t. "async" itxs (or vice
2867 * versa) is *not* preserved, even for itxs that correspond
2868 * to the same object.
2869 *
2870 * For more details, see: zil_itx_assign(), zil_async_to_sync(),
2871 * zil_get_commit_list(), and zil_process_commit_list().
2872 *
2873 * 3. The lwbs represent a linked list of blocks on disk. Thus, any
2874 * lwb cannot be considered committed to stable storage, until its
2875 * "previous" lwb is also committed to stable storage. This fact,
2876 * coupled with the fact described above, means that itxs are
2877 * committed in (roughly) the order in which they were generated.
2878 * This is essential because itxs are dependent on prior itxs.
2879 * Thus, we *must not* deem an itx as being committed to stable
2880 * storage, until *all* prior itxs have also been committed to
2881 * stable storage.
2882 *
2883 * To enforce this ordering of lwb zio's, while still leveraging as
2884 * much of the underlying storage performance as possible, we rely
2885 * on two fundamental concepts:
2886 *
2887 * 1. The creation and issuance of lwb zio's is protected by
1b2b0aca 2888 * the zilog's "zl_issuer_lock", which ensures only a single
1ce23dca
PS
2889 * thread is creating and/or issuing lwb's at a time
2890 * 2. The "previous" lwb is a child of the "current" lwb
2fe61a7e 2891 * (leveraging the zio parent-child dependency graph)
1ce23dca
PS
2892 *
2893 * By relying on this parent-child zio relationship, we can have
2894 * many lwb zio's concurrently issued to the underlying storage,
2895 * but the order in which they complete will be the same order in
2896 * which they were created.
34dc7c2f
BB
2897 */
2898void
572e2857 2899zil_commit(zilog_t *zilog, uint64_t foid)
34dc7c2f 2900{
1ce23dca
PS
2901 /*
2902 * We should never attempt to call zil_commit on a snapshot for
2903 * a couple of reasons:
2904 *
2905 * 1. A snapshot may never be modified, thus it cannot have any
2906 * in-flight itxs that would have modified the dataset.
2907 *
2908 * 2. By design, when zil_commit() is called, a commit itx will
2909 * be assigned to this zilog; as a result, the zilog will be
2910 * dirtied. We must not dirty the zilog of a snapshot; there's
2911 * checks in the code that enforce this invariant, and will
2912 * cause a panic if it's not upheld.
2913 */
2914 ASSERT3B(dmu_objset_is_snapshot(zilog->zl_os), ==, B_FALSE);
34dc7c2f 2915
572e2857
BB
2916 if (zilog->zl_sync == ZFS_SYNC_DISABLED)
2917 return;
34dc7c2f 2918
1ce23dca
PS
2919 if (!spa_writeable(zilog->zl_spa)) {
2920 /*
2921 * If the SPA is not writable, there should never be any
2922 * pending itxs waiting to be committed to disk. If that
2923 * weren't true, we'd skip writing those itxs out, and
2fe61a7e 2924 * would break the semantics of zil_commit(); thus, we're
1ce23dca
PS
2925 * verifying that truth before we return to the caller.
2926 */
2927 ASSERT(list_is_empty(&zilog->zl_lwb_list));
2928 ASSERT3P(zilog->zl_last_lwb_opened, ==, NULL);
2929 for (int i = 0; i < TXG_SIZE; i++)
2930 ASSERT3P(zilog->zl_itxg[i].itxg_itxs, ==, NULL);
2931 return;
2932 }
2933
2934 /*
2935 * If the ZIL is suspended, we don't want to dirty it by calling
2936 * zil_commit_itx_assign() below, nor can we write out
2937 * lwbs like would be done in zil_commit_write(). Thus, we
2938 * simply rely on txg_wait_synced() to maintain the necessary
2939 * semantics, and avoid calling those functions altogether.
2940 */
2941 if (zilog->zl_suspend > 0) {
2942 txg_wait_synced(zilog->zl_dmu_pool, 0);
2943 return;
2944 }
2945
2fe61a7e
PS
2946 zil_commit_impl(zilog, foid);
2947}
2948
2949void
2950zil_commit_impl(zilog_t *zilog, uint64_t foid)
2951{
b6ad9671
ED
2952 ZIL_STAT_BUMP(zil_commit_count);
2953
1ce23dca
PS
2954 /*
2955 * Move the "async" itxs for the specified foid to the "sync"
2956 * queues, such that they will be later committed (or skipped)
2957 * to an lwb when zil_process_commit_list() is called.
2958 *
2959 * Since these "async" itxs must be committed prior to this
2960 * call to zil_commit returning, we must perform this operation
2961 * before we call zil_commit_itx_assign().
2962 */
572e2857 2963 zil_async_to_sync(zilog, foid);
34dc7c2f 2964
1ce23dca
PS
2965 /*
2966 * We allocate a new "waiter" structure which will initially be
2967 * linked to the commit itx using the itx's "itx_private" field.
2968 * Since the commit itx doesn't represent any on-disk state,
2969 * when it's committed to an lwb, rather than copying the its
2970 * lr_t into the lwb's buffer, the commit itx's "waiter" will be
2971 * added to the lwb's list of waiters. Then, when the lwb is
2972 * committed to stable storage, each waiter in the lwb's list of
2973 * waiters will be marked "done", and signalled.
2974 *
2975 * We must create the waiter and assign the commit itx prior to
2976 * calling zil_commit_writer(), or else our specific commit itx
2977 * is not guaranteed to be committed to an lwb prior to calling
2978 * zil_commit_waiter().
2979 */
2980 zil_commit_waiter_t *zcw = zil_alloc_commit_waiter();
2981 zil_commit_itx_assign(zilog, zcw);
428870ff 2982
1ce23dca
PS
2983 zil_commit_writer(zilog, zcw);
2984 zil_commit_waiter(zilog, zcw);
428870ff 2985
1ce23dca
PS
2986 if (zcw->zcw_zio_error != 0) {
2987 /*
2988 * If there was an error writing out the ZIL blocks that
2989 * this thread is waiting on, then we fallback to
2990 * relying on spa_sync() to write out the data this
2991 * thread is waiting on. Obviously this has performance
2992 * implications, but the expectation is for this to be
2993 * an exceptional case, and shouldn't occur often.
2994 */
2995 DTRACE_PROBE2(zil__commit__io__error,
2996 zilog_t *, zilog, zil_commit_waiter_t *, zcw);
2997 txg_wait_synced(zilog->zl_dmu_pool, 0);
2998 }
8c0712fd 2999
1ce23dca 3000 zil_free_commit_waiter(zcw);
428870ff
BB
3001}
3002
34dc7c2f
BB
3003/*
3004 * Called in syncing context to free committed log blocks and update log header.
3005 */
3006void
3007zil_sync(zilog_t *zilog, dmu_tx_t *tx)
3008{
3009 zil_header_t *zh = zil_header_in_syncing_context(zilog);
3010 uint64_t txg = dmu_tx_get_txg(tx);
3011 spa_t *spa = zilog->zl_spa;
428870ff 3012 uint64_t *replayed_seq = &zilog->zl_replayed_seq[txg & TXG_MASK];
34dc7c2f
BB
3013 lwb_t *lwb;
3014
9babb374
BB
3015 /*
3016 * We don't zero out zl_destroy_txg, so make sure we don't try
3017 * to destroy it twice.
3018 */
3019 if (spa_sync_pass(spa) != 1)
3020 return;
3021
34dc7c2f
BB
3022 mutex_enter(&zilog->zl_lock);
3023
3024 ASSERT(zilog->zl_stop_sync == 0);
3025
428870ff
BB
3026 if (*replayed_seq != 0) {
3027 ASSERT(zh->zh_replay_seq < *replayed_seq);
3028 zh->zh_replay_seq = *replayed_seq;
3029 *replayed_seq = 0;
3030 }
34dc7c2f
BB
3031
3032 if (zilog->zl_destroy_txg == txg) {
3033 blkptr_t blk = zh->zh_log;
3034
3035 ASSERT(list_head(&zilog->zl_lwb_list) == NULL);
34dc7c2f
BB
3036
3037 bzero(zh, sizeof (zil_header_t));
fb5f0bc8 3038 bzero(zilog->zl_replayed_seq, sizeof (zilog->zl_replayed_seq));
34dc7c2f
BB
3039
3040 if (zilog->zl_keep_first) {
3041 /*
3042 * If this block was part of log chain that couldn't
3043 * be claimed because a device was missing during
3044 * zil_claim(), but that device later returns,
3045 * then this block could erroneously appear valid.
3046 * To guard against this, assign a new GUID to the new
3047 * log chain so it doesn't matter what blk points to.
3048 */
3049 zil_init_log_chain(zilog, &blk);
3050 zh->zh_log = blk;
3051 }
3052 }
3053
9babb374 3054 while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
34dc7c2f
BB
3055 zh->zh_log = lwb->lwb_blk;
3056 if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg)
3057 break;
3058 list_remove(&zilog->zl_lwb_list, lwb);
1ce23dca
PS
3059 zio_free(spa, txg, &lwb->lwb_blk);
3060 zil_free_lwb(zilog, lwb);
34dc7c2f
BB
3061
3062 /*
3063 * If we don't have anything left in the lwb list then
3064 * we've had an allocation failure and we need to zero
3065 * out the zil_header blkptr so that we don't end
3066 * up freeing the same block twice.
3067 */
3068 if (list_head(&zilog->zl_lwb_list) == NULL)
3069 BP_ZERO(&zh->zh_log);
3070 }
920dd524
ED
3071
3072 /*
3073 * Remove fastwrite on any blocks that have been pre-allocated for
3074 * the next commit. This prevents fastwrite counter pollution by
3075 * unused, long-lived LWBs.
3076 */
3077 for (; lwb != NULL; lwb = list_next(&zilog->zl_lwb_list, lwb)) {
1ce23dca 3078 if (lwb->lwb_fastwrite && !lwb->lwb_write_zio) {
920dd524
ED
3079 metaslab_fastwrite_unmark(zilog->zl_spa, &lwb->lwb_blk);
3080 lwb->lwb_fastwrite = 0;
3081 }
3082 }
3083
34dc7c2f
BB
3084 mutex_exit(&zilog->zl_lock);
3085}
3086
1ce23dca
PS
3087/* ARGSUSED */
3088static int
3089zil_lwb_cons(void *vbuf, void *unused, int kmflag)
3090{
3091 lwb_t *lwb = vbuf;
3092 list_create(&lwb->lwb_itxs, sizeof (itx_t), offsetof(itx_t, itx_node));
3093 list_create(&lwb->lwb_waiters, sizeof (zil_commit_waiter_t),
3094 offsetof(zil_commit_waiter_t, zcw_node));
3095 avl_create(&lwb->lwb_vdev_tree, zil_lwb_vdev_compare,
3096 sizeof (zil_vdev_node_t), offsetof(zil_vdev_node_t, zv_node));
3097 mutex_init(&lwb->lwb_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
3098 return (0);
3099}
3100
3101/* ARGSUSED */
3102static void
3103zil_lwb_dest(void *vbuf, void *unused)
3104{
3105 lwb_t *lwb = vbuf;
3106 mutex_destroy(&lwb->lwb_vdev_lock);
3107 avl_destroy(&lwb->lwb_vdev_tree);
3108 list_destroy(&lwb->lwb_waiters);
3109 list_destroy(&lwb->lwb_itxs);
3110}
3111
34dc7c2f
BB
3112void
3113zil_init(void)
3114{
3115 zil_lwb_cache = kmem_cache_create("zil_lwb_cache",
1ce23dca
PS
3116 sizeof (lwb_t), 0, zil_lwb_cons, zil_lwb_dest, NULL, NULL, NULL, 0);
3117
3118 zil_zcw_cache = kmem_cache_create("zil_zcw_cache",
3119 sizeof (zil_commit_waiter_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
b6ad9671
ED
3120
3121 zil_ksp = kstat_create("zfs", 0, "zil", "misc",
d1d7e268 3122 KSTAT_TYPE_NAMED, sizeof (zil_stats) / sizeof (kstat_named_t),
b6ad9671
ED
3123 KSTAT_FLAG_VIRTUAL);
3124
3125 if (zil_ksp != NULL) {
3126 zil_ksp->ks_data = &zil_stats;
3127 kstat_install(zil_ksp);
3128 }
34dc7c2f
BB
3129}
3130
3131void
3132zil_fini(void)
3133{
1ce23dca 3134 kmem_cache_destroy(zil_zcw_cache);
34dc7c2f 3135 kmem_cache_destroy(zil_lwb_cache);
b6ad9671
ED
3136
3137 if (zil_ksp != NULL) {
3138 kstat_delete(zil_ksp);
3139 zil_ksp = NULL;
3140 }
34dc7c2f
BB
3141}
3142
428870ff
BB
3143void
3144zil_set_sync(zilog_t *zilog, uint64_t sync)
3145{
3146 zilog->zl_sync = sync;
3147}
3148
3149void
3150zil_set_logbias(zilog_t *zilog, uint64_t logbias)
3151{
3152 zilog->zl_logbias = logbias;
3153}
3154
34dc7c2f
BB
3155zilog_t *
3156zil_alloc(objset_t *os, zil_header_t *zh_phys)
3157{
3158 zilog_t *zilog;
3159
79c76d5b 3160 zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP);
34dc7c2f
BB
3161
3162 zilog->zl_header = zh_phys;
3163 zilog->zl_os = os;
3164 zilog->zl_spa = dmu_objset_spa(os);
3165 zilog->zl_dmu_pool = dmu_objset_pool(os);
3166 zilog->zl_destroy_txg = TXG_INITIAL - 1;
428870ff
BB
3167 zilog->zl_logbias = dmu_objset_logbias(os);
3168 zilog->zl_sync = dmu_objset_syncprop(os);
1ce23dca
PS
3169 zilog->zl_dirty_max_txg = 0;
3170 zilog->zl_last_lwb_opened = NULL;
3171 zilog->zl_last_lwb_latency = 0;
b8738257 3172 zilog->zl_max_block_size = zil_maxblocksize;
34dc7c2f
BB
3173
3174 mutex_init(&zilog->zl_lock, NULL, MUTEX_DEFAULT, NULL);
1b2b0aca 3175 mutex_init(&zilog->zl_issuer_lock, NULL, MUTEX_DEFAULT, NULL);
34dc7c2f 3176
1c27024e 3177 for (int i = 0; i < TXG_SIZE; i++) {
572e2857
BB
3178 mutex_init(&zilog->zl_itxg[i].itxg_lock, NULL,
3179 MUTEX_DEFAULT, NULL);
3180 }
34dc7c2f
BB
3181
3182 list_create(&zilog->zl_lwb_list, sizeof (lwb_t),
3183 offsetof(lwb_t, lwb_node));
3184
572e2857
BB
3185 list_create(&zilog->zl_itx_commit_list, sizeof (itx_t),
3186 offsetof(itx_t, itx_node));
3187
34dc7c2f
BB
3188 cv_init(&zilog->zl_cv_suspend, NULL, CV_DEFAULT, NULL);
3189
3190 return (zilog);
3191}
3192
3193void
3194zil_free(zilog_t *zilog)
3195{
d6320ddb 3196 int i;
34dc7c2f
BB
3197
3198 zilog->zl_stop_sync = 1;
3199
13fe0198
MA
3200 ASSERT0(zilog->zl_suspend);
3201 ASSERT0(zilog->zl_suspending);
3202
3e31d2b0 3203 ASSERT(list_is_empty(&zilog->zl_lwb_list));
34dc7c2f
BB
3204 list_destroy(&zilog->zl_lwb_list);
3205
572e2857
BB
3206 ASSERT(list_is_empty(&zilog->zl_itx_commit_list));
3207 list_destroy(&zilog->zl_itx_commit_list);
3208
d6320ddb 3209 for (i = 0; i < TXG_SIZE; i++) {
572e2857
BB
3210 /*
3211 * It's possible for an itx to be generated that doesn't dirty
3212 * a txg (e.g. ztest TX_TRUNCATE). So there's no zil_clean()
3213 * callback to remove the entry. We remove those here.
3214 *
3215 * Also free up the ziltest itxs.
3216 */
3217 if (zilog->zl_itxg[i].itxg_itxs)
3218 zil_itxg_clean(zilog->zl_itxg[i].itxg_itxs);
3219 mutex_destroy(&zilog->zl_itxg[i].itxg_lock);
3220 }
3221
1b2b0aca 3222 mutex_destroy(&zilog->zl_issuer_lock);
34dc7c2f
BB
3223 mutex_destroy(&zilog->zl_lock);
3224
34dc7c2f
BB
3225 cv_destroy(&zilog->zl_cv_suspend);
3226
3227 kmem_free(zilog, sizeof (zilog_t));
3228}
3229
34dc7c2f
BB
3230/*
3231 * Open an intent log.
3232 */
3233zilog_t *
3234zil_open(objset_t *os, zil_get_data_t *get_data)
3235{
3236 zilog_t *zilog = dmu_objset_zil(os);
3237
1ce23dca
PS
3238 ASSERT3P(zilog->zl_get_data, ==, NULL);
3239 ASSERT3P(zilog->zl_last_lwb_opened, ==, NULL);
3e31d2b0
ES
3240 ASSERT(list_is_empty(&zilog->zl_lwb_list));
3241
34dc7c2f 3242 zilog->zl_get_data = get_data;
34dc7c2f
BB
3243
3244 return (zilog);
3245}
3246
3247/*
3248 * Close an intent log.
3249 */
3250void
3251zil_close(zilog_t *zilog)
3252{
3e31d2b0 3253 lwb_t *lwb;
1ce23dca 3254 uint64_t txg;
572e2857 3255
1ce23dca
PS
3256 if (!dmu_objset_is_snapshot(zilog->zl_os)) {
3257 zil_commit(zilog, 0);
3258 } else {
3259 ASSERT3P(list_tail(&zilog->zl_lwb_list), ==, NULL);
3260 ASSERT0(zilog->zl_dirty_max_txg);
3261 ASSERT3B(zilog_is_dirty(zilog), ==, B_FALSE);
3262 }
572e2857 3263
572e2857 3264 mutex_enter(&zilog->zl_lock);
3e31d2b0 3265 lwb = list_tail(&zilog->zl_lwb_list);
1ce23dca
PS
3266 if (lwb == NULL)
3267 txg = zilog->zl_dirty_max_txg;
3268 else
3269 txg = MAX(zilog->zl_dirty_max_txg, lwb->lwb_max_txg);
572e2857 3270 mutex_exit(&zilog->zl_lock);
1ce23dca
PS
3271
3272 /*
3273 * We need to use txg_wait_synced() to wait long enough for the
3274 * ZIL to be clean, and to wait for all pending lwbs to be
3275 * written out.
3276 */
3277 if (txg != 0)
34dc7c2f 3278 txg_wait_synced(zilog->zl_dmu_pool, txg);
55922e73
GW
3279
3280 if (zilog_is_dirty(zilog))
a887d653 3281 zfs_dbgmsg("zil (%px) is dirty, txg %llu", zilog, txg);
50c957f7 3282 if (txg < spa_freeze_txg(zilog->zl_spa))
55922e73 3283 VERIFY(!zilog_is_dirty(zilog));
34dc7c2f 3284
34dc7c2f 3285 zilog->zl_get_data = NULL;
3e31d2b0
ES
3286
3287 /*
1ce23dca 3288 * We should have only one lwb left on the list; remove it now.
3e31d2b0
ES
3289 */
3290 mutex_enter(&zilog->zl_lock);
3291 lwb = list_head(&zilog->zl_lwb_list);
3292 if (lwb != NULL) {
1ce23dca
PS
3293 ASSERT3P(lwb, ==, list_tail(&zilog->zl_lwb_list));
3294 ASSERT3S(lwb->lwb_state, !=, LWB_STATE_ISSUED);
3295
920dd524
ED
3296 if (lwb->lwb_fastwrite)
3297 metaslab_fastwrite_unmark(zilog->zl_spa, &lwb->lwb_blk);
1ce23dca 3298
3e31d2b0
ES
3299 list_remove(&zilog->zl_lwb_list, lwb);
3300 zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
1ce23dca 3301 zil_free_lwb(zilog, lwb);
3e31d2b0
ES
3302 }
3303 mutex_exit(&zilog->zl_lock);
34dc7c2f
BB
3304}
3305
13fe0198
MA
3306static char *suspend_tag = "zil suspending";
3307
34dc7c2f
BB
3308/*
3309 * Suspend an intent log. While in suspended mode, we still honor
3310 * synchronous semantics, but we rely on txg_wait_synced() to do it.
13fe0198
MA
3311 * On old version pools, we suspend the log briefly when taking a
3312 * snapshot so that it will have an empty intent log.
3313 *
3314 * Long holds are not really intended to be used the way we do here --
3315 * held for such a short time. A concurrent caller of dsl_dataset_long_held()
3316 * could fail. Therefore we take pains to only put a long hold if it is
3317 * actually necessary. Fortunately, it will only be necessary if the
3318 * objset is currently mounted (or the ZVOL equivalent). In that case it
3319 * will already have a long hold, so we are not really making things any worse.
3320 *
3321 * Ideally, we would locate the existing long-holder (i.e. the zfsvfs_t or
3322 * zvol_state_t), and use their mechanism to prevent their hold from being
3323 * dropped (e.g. VFS_HOLD()). However, that would be even more pain for
3324 * very little gain.
3325 *
3326 * if cookiep == NULL, this does both the suspend & resume.
3327 * Otherwise, it returns with the dataset "long held", and the cookie
3328 * should be passed into zil_resume().
34dc7c2f
BB
3329 */
3330int
13fe0198 3331zil_suspend(const char *osname, void **cookiep)
34dc7c2f 3332{
13fe0198
MA
3333 objset_t *os;
3334 zilog_t *zilog;
3335 const zil_header_t *zh;
3336 int error;
3337
3338 error = dmu_objset_hold(osname, suspend_tag, &os);
3339 if (error != 0)
3340 return (error);
3341 zilog = dmu_objset_zil(os);
34dc7c2f
BB
3342
3343 mutex_enter(&zilog->zl_lock);
13fe0198
MA
3344 zh = zilog->zl_header;
3345
9babb374 3346 if (zh->zh_flags & ZIL_REPLAY_NEEDED) { /* unplayed log */
34dc7c2f 3347 mutex_exit(&zilog->zl_lock);
13fe0198 3348 dmu_objset_rele(os, suspend_tag);
2e528b49 3349 return (SET_ERROR(EBUSY));
34dc7c2f 3350 }
13fe0198
MA
3351
3352 /*
3353 * Don't put a long hold in the cases where we can avoid it. This
3354 * is when there is no cookie so we are doing a suspend & resume
3355 * (i.e. called from zil_vdev_offline()), and there's nothing to do
3356 * for the suspend because it's already suspended, or there's no ZIL.
3357 */
3358 if (cookiep == NULL && !zilog->zl_suspending &&
3359 (zilog->zl_suspend > 0 || BP_IS_HOLE(&zh->zh_log))) {
3360 mutex_exit(&zilog->zl_lock);
3361 dmu_objset_rele(os, suspend_tag);
3362 return (0);
3363 }
3364
3365 dsl_dataset_long_hold(dmu_objset_ds(os), suspend_tag);
3366 dsl_pool_rele(dmu_objset_pool(os), suspend_tag);
3367
3368 zilog->zl_suspend++;
3369
3370 if (zilog->zl_suspend > 1) {
34dc7c2f 3371 /*
13fe0198 3372 * Someone else is already suspending it.
34dc7c2f
BB
3373 * Just wait for them to finish.
3374 */
13fe0198 3375
34dc7c2f
BB
3376 while (zilog->zl_suspending)
3377 cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock);
34dc7c2f 3378 mutex_exit(&zilog->zl_lock);
13fe0198
MA
3379
3380 if (cookiep == NULL)
3381 zil_resume(os);
3382 else
3383 *cookiep = os;
3384 return (0);
3385 }
3386
3387 /*
3388 * If there is no pointer to an on-disk block, this ZIL must not
3389 * be active (e.g. filesystem not mounted), so there's nothing
3390 * to clean up.
3391 */
3392 if (BP_IS_HOLE(&zh->zh_log)) {
3393 ASSERT(cookiep != NULL); /* fast path already handled */
3394
3395 *cookiep = os;
3396 mutex_exit(&zilog->zl_lock);
34dc7c2f
BB
3397 return (0);
3398 }
13fe0198 3399
4807c0ba
TC
3400 /*
3401 * The ZIL has work to do. Ensure that the associated encryption
3402 * key will remain mapped while we are committing the log by
3403 * grabbing a reference to it. If the key isn't loaded we have no
3404 * choice but to return an error until the wrapping key is loaded.
3405 */
52ce99dd
TC
3406 if (os->os_encrypted &&
3407 dsl_dataset_create_key_mapping(dmu_objset_ds(os)) != 0) {
4807c0ba
TC
3408 zilog->zl_suspend--;
3409 mutex_exit(&zilog->zl_lock);
3410 dsl_dataset_long_rele(dmu_objset_ds(os), suspend_tag);
3411 dsl_dataset_rele(dmu_objset_ds(os), suspend_tag);
2ffd89fc 3412 return (SET_ERROR(EACCES));
4807c0ba
TC
3413 }
3414
34dc7c2f
BB
3415 zilog->zl_suspending = B_TRUE;
3416 mutex_exit(&zilog->zl_lock);
3417
2fe61a7e
PS
3418 /*
3419 * We need to use zil_commit_impl to ensure we wait for all
3420 * LWB_STATE_OPENED and LWB_STATE_ISSUED lwbs to be committed
3421 * to disk before proceeding. If we used zil_commit instead, it
3422 * would just call txg_wait_synced(), because zl_suspend is set.
3423 * txg_wait_synced() doesn't wait for these lwb's to be
900d09b2 3424 * LWB_STATE_FLUSH_DONE before returning.
2fe61a7e
PS
3425 */
3426 zil_commit_impl(zilog, 0);
3427
3428 /*
900d09b2
PS
3429 * Now that we've ensured all lwb's are LWB_STATE_FLUSH_DONE, we
3430 * use txg_wait_synced() to ensure the data from the zilog has
3431 * migrated to the main pool before calling zil_destroy().
2fe61a7e
PS
3432 */
3433 txg_wait_synced(zilog->zl_dmu_pool, 0);
34dc7c2f
BB
3434
3435 zil_destroy(zilog, B_FALSE);
3436
3437 mutex_enter(&zilog->zl_lock);
3438 zilog->zl_suspending = B_FALSE;
3439 cv_broadcast(&zilog->zl_cv_suspend);
3440 mutex_exit(&zilog->zl_lock);
3441
52ce99dd
TC
3442 if (os->os_encrypted)
3443 dsl_dataset_remove_key_mapping(dmu_objset_ds(os));
4807c0ba 3444
13fe0198
MA
3445 if (cookiep == NULL)
3446 zil_resume(os);
3447 else
3448 *cookiep = os;
34dc7c2f
BB
3449 return (0);
3450}
3451
3452void
13fe0198 3453zil_resume(void *cookie)
34dc7c2f 3454{
13fe0198
MA
3455 objset_t *os = cookie;
3456 zilog_t *zilog = dmu_objset_zil(os);
3457
34dc7c2f
BB
3458 mutex_enter(&zilog->zl_lock);
3459 ASSERT(zilog->zl_suspend != 0);
3460 zilog->zl_suspend--;
3461 mutex_exit(&zilog->zl_lock);
13fe0198
MA
3462 dsl_dataset_long_rele(dmu_objset_ds(os), suspend_tag);
3463 dsl_dataset_rele(dmu_objset_ds(os), suspend_tag);
34dc7c2f
BB
3464}
3465
3466typedef struct zil_replay_arg {
867959b5 3467 zil_replay_func_t **zr_replay;
34dc7c2f 3468 void *zr_arg;
34dc7c2f 3469 boolean_t zr_byteswap;
428870ff 3470 char *zr_lr;
34dc7c2f
BB
3471} zil_replay_arg_t;
3472
428870ff
BB
3473static int
3474zil_replay_error(zilog_t *zilog, lr_t *lr, int error)
3475{
eca7b760 3476 char name[ZFS_MAX_DATASET_NAME_LEN];
428870ff
BB
3477
3478 zilog->zl_replaying_seq--; /* didn't actually replay this one */
3479
3480 dmu_objset_name(zilog->zl_os, name);
3481
3482 cmn_err(CE_WARN, "ZFS replay transaction error %d, "
3483 "dataset %s, seq 0x%llx, txtype %llu %s\n", error, name,
3484 (u_longlong_t)lr->lrc_seq,
3485 (u_longlong_t)(lr->lrc_txtype & ~TX_CI),
3486 (lr->lrc_txtype & TX_CI) ? "CI" : "");
3487
3488 return (error);
3489}
3490
3491static int
34dc7c2f
BB
3492zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg)
3493{
3494 zil_replay_arg_t *zr = zra;
3495 const zil_header_t *zh = zilog->zl_header;
3496 uint64_t reclen = lr->lrc_reclen;
3497 uint64_t txtype = lr->lrc_txtype;
428870ff 3498 int error = 0;
34dc7c2f 3499
428870ff 3500 zilog->zl_replaying_seq = lr->lrc_seq;
34dc7c2f
BB
3501
3502 if (lr->lrc_seq <= zh->zh_replay_seq) /* already replayed */
428870ff
BB
3503 return (0);
3504
3505 if (lr->lrc_txg < claim_txg) /* already committed */
3506 return (0);
34dc7c2f
BB
3507
3508 /* Strip case-insensitive bit, still present in log record */
3509 txtype &= ~TX_CI;
3510
428870ff
BB
3511 if (txtype == 0 || txtype >= TX_MAX_TYPE)
3512 return (zil_replay_error(zilog, lr, EINVAL));
3513
3514 /*
3515 * If this record type can be logged out of order, the object
3516 * (lr_foid) may no longer exist. That's legitimate, not an error.
3517 */
3518 if (TX_OOO(txtype)) {
3519 error = dmu_object_info(zilog->zl_os,
50c957f7 3520 LR_FOID_GET_OBJ(((lr_ooo_t *)lr)->lr_foid), NULL);
428870ff
BB
3521 if (error == ENOENT || error == EEXIST)
3522 return (0);
fb5f0bc8
BB
3523 }
3524
34dc7c2f
BB
3525 /*
3526 * Make a copy of the data so we can revise and extend it.
3527 */
428870ff
BB
3528 bcopy(lr, zr->zr_lr, reclen);
3529
3530 /*
3531 * If this is a TX_WRITE with a blkptr, suck in the data.
3532 */
3533 if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) {
3534 error = zil_read_log_data(zilog, (lr_write_t *)lr,
3535 zr->zr_lr + reclen);
13fe0198 3536 if (error != 0)
428870ff
BB
3537 return (zil_replay_error(zilog, lr, error));
3538 }
34dc7c2f
BB
3539
3540 /*
3541 * The log block containing this lr may have been byteswapped
3542 * so that we can easily examine common fields like lrc_txtype.
428870ff 3543 * However, the log is a mix of different record types, and only the
34dc7c2f
BB
3544 * replay vectors know how to byteswap their records. Therefore, if
3545 * the lr was byteswapped, undo it before invoking the replay vector.
3546 */
3547 if (zr->zr_byteswap)
428870ff 3548 byteswap_uint64_array(zr->zr_lr, reclen);
34dc7c2f
BB
3549
3550 /*
3551 * We must now do two things atomically: replay this log record,
fb5f0bc8
BB
3552 * and update the log header sequence number to reflect the fact that
3553 * we did so. At the end of each replay function the sequence number
3554 * is updated if we are in replay mode.
34dc7c2f 3555 */
428870ff 3556 error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lr, zr->zr_byteswap);
13fe0198 3557 if (error != 0) {
34dc7c2f
BB
3558 /*
3559 * The DMU's dnode layer doesn't see removes until the txg
3560 * commits, so a subsequent claim can spuriously fail with
fb5f0bc8 3561 * EEXIST. So if we receive any error we try syncing out
428870ff
BB
3562 * any removes then retry the transaction. Note that we
3563 * specify B_FALSE for byteswap now, so we don't do it twice.
34dc7c2f 3564 */
428870ff
BB
3565 txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0);
3566 error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lr, B_FALSE);
13fe0198 3567 if (error != 0)
428870ff 3568 return (zil_replay_error(zilog, lr, error));
34dc7c2f 3569 }
428870ff 3570 return (0);
34dc7c2f
BB
3571}
3572
3573/* ARGSUSED */
428870ff 3574static int
34dc7c2f
BB
3575zil_incr_blks(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
3576{
3577 zilog->zl_replay_blks++;
428870ff
BB
3578
3579 return (0);
34dc7c2f
BB
3580}
3581
3582/*
3583 * If this dataset has a non-empty intent log, replay it and destroy it.
3584 */
3585void
867959b5 3586zil_replay(objset_t *os, void *arg, zil_replay_func_t *replay_func[TX_MAX_TYPE])
34dc7c2f
BB
3587{
3588 zilog_t *zilog = dmu_objset_zil(os);
3589 const zil_header_t *zh = zilog->zl_header;
3590 zil_replay_arg_t zr;
3591
9babb374 3592 if ((zh->zh_flags & ZIL_REPLAY_NEEDED) == 0) {
34dc7c2f
BB
3593 zil_destroy(zilog, B_TRUE);
3594 return;
3595 }
3596
34dc7c2f
BB
3597 zr.zr_replay = replay_func;
3598 zr.zr_arg = arg;
34dc7c2f 3599 zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log);
79c76d5b 3600 zr.zr_lr = vmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP);
34dc7c2f
BB
3601
3602 /*
3603 * Wait for in-progress removes to sync before starting replay.
3604 */
3605 txg_wait_synced(zilog->zl_dmu_pool, 0);
3606
fb5f0bc8 3607 zilog->zl_replay = B_TRUE;
428870ff 3608 zilog->zl_replay_time = ddi_get_lbolt();
34dc7c2f
BB
3609 ASSERT(zilog->zl_replay_blks == 0);
3610 (void) zil_parse(zilog, zil_incr_blks, zil_replay_log_record, &zr,
b5256303 3611 zh->zh_claim_txg, B_TRUE);
00b46022 3612 vmem_free(zr.zr_lr, 2 * SPA_MAXBLOCKSIZE);
34dc7c2f
BB
3613
3614 zil_destroy(zilog, B_FALSE);
3615 txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
fb5f0bc8 3616 zilog->zl_replay = B_FALSE;
34dc7c2f
BB
3617}
3618
428870ff
BB
3619boolean_t
3620zil_replaying(zilog_t *zilog, dmu_tx_t *tx)
34dc7c2f 3621{
428870ff
BB
3622 if (zilog->zl_sync == ZFS_SYNC_DISABLED)
3623 return (B_TRUE);
34dc7c2f 3624
428870ff
BB
3625 if (zilog->zl_replay) {
3626 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
3627 zilog->zl_replayed_seq[dmu_tx_get_txg(tx) & TXG_MASK] =
3628 zilog->zl_replaying_seq;
3629 return (B_TRUE);
34dc7c2f
BB
3630 }
3631
428870ff 3632 return (B_FALSE);
34dc7c2f 3633}
9babb374
BB
3634
3635/* ARGSUSED */
3636int
a1d477c2 3637zil_reset(const char *osname, void *arg)
9babb374 3638{
9babb374
BB
3639 int error;
3640
13fe0198 3641 error = zil_suspend(osname, NULL);
2ffd89fc
PZ
3642 /* EACCES means crypto key not loaded */
3643 if ((error == EACCES) || (error == EBUSY))
3644 return (SET_ERROR(error));
13fe0198 3645 if (error != 0)
2e528b49 3646 return (SET_ERROR(EEXIST));
13fe0198 3647 return (0);
9babb374 3648}
c409e464 3649
0f699108
AZ
3650EXPORT_SYMBOL(zil_alloc);
3651EXPORT_SYMBOL(zil_free);
3652EXPORT_SYMBOL(zil_open);
3653EXPORT_SYMBOL(zil_close);
3654EXPORT_SYMBOL(zil_replay);
3655EXPORT_SYMBOL(zil_replaying);
3656EXPORT_SYMBOL(zil_destroy);
3657EXPORT_SYMBOL(zil_destroy_sync);
3658EXPORT_SYMBOL(zil_itx_create);
3659EXPORT_SYMBOL(zil_itx_destroy);
3660EXPORT_SYMBOL(zil_itx_assign);
3661EXPORT_SYMBOL(zil_commit);
0f699108
AZ
3662EXPORT_SYMBOL(zil_claim);
3663EXPORT_SYMBOL(zil_check_log_chain);
3664EXPORT_SYMBOL(zil_sync);
3665EXPORT_SYMBOL(zil_clean);
3666EXPORT_SYMBOL(zil_suspend);
3667EXPORT_SYMBOL(zil_resume);
1ce23dca 3668EXPORT_SYMBOL(zil_lwb_add_block);
0f699108
AZ
3669EXPORT_SYMBOL(zil_bp_tree_add);
3670EXPORT_SYMBOL(zil_set_sync);
3671EXPORT_SYMBOL(zil_set_logbias);
3672
1b7c1e5c 3673/* BEGIN CSTYLED */
03fdcb9a
MM
3674ZFS_MODULE_PARAM(zfs, zfs_, commit_timeout_pct, INT, ZMOD_RW,
3675 "ZIL block open timeout percentage");
2fe61a7e 3676
03fdcb9a
MM
3677ZFS_MODULE_PARAM(zfs_zil, zil_, replay_disable, INT, ZMOD_RW,
3678 "Disable intent logging replay");
c409e464 3679
03fdcb9a
MM
3680ZFS_MODULE_PARAM(zfs_zil, zil_, nocacheflush, INT, ZMOD_RW,
3681 "Disable ZIL cache flushes");
ee191e80 3682
03fdcb9a
MM
3683ZFS_MODULE_PARAM(zfs_zil, zil_, slog_bulk, ULONG, ZMOD_RW,
3684 "Limit in bytes slog sync writes per commit");
b8738257 3685
03fdcb9a
MM
3686ZFS_MODULE_PARAM(zfs_zil, zil_, maxblocksize, INT, ZMOD_RW,
3687 "Limit in bytes of ZIL log block size");
1b7c1e5c 3688/* END CSTYLED */