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