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