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