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