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