xref: /titanic_41/usr/src/uts/common/fs/zfs/zil.c (revision a799b1e741b6f59012a469e6b57c40cb8061127b)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/zfs_context.h>
27 #include <sys/spa.h>
28 #include <sys/spa_impl.h>
29 #include <sys/dmu.h>
30 #include <sys/zap.h>
31 #include <sys/arc.h>
32 #include <sys/stat.h>
33 #include <sys/resource.h>
34 #include <sys/zil.h>
35 #include <sys/zil_impl.h>
36 #include <sys/dsl_dataset.h>
37 #include <sys/vdev.h>
38 #include <sys/dmu_tx.h>
39 
40 /*
41  * The zfs intent log (ZIL) saves transaction records of system calls
42  * that change the file system in memory with enough information
43  * to be able to replay them. These are stored in memory until
44  * either the DMU transaction group (txg) commits them to the stable pool
45  * and they can be discarded, or they are flushed to the stable log
46  * (also in the pool) due to a fsync, O_DSYNC or other synchronous
47  * requirement. In the event of a panic or power fail then those log
48  * records (transactions) are replayed.
49  *
50  * There is one ZIL per file system. Its on-disk (pool) format consists
51  * of 3 parts:
52  *
53  * 	- ZIL header
54  * 	- ZIL blocks
55  * 	- ZIL records
56  *
57  * A log record holds a system call transaction. Log blocks can
58  * hold many log records and the blocks are chained together.
59  * Each ZIL block contains a block pointer (blkptr_t) to the next
60  * ZIL block in the chain. The ZIL header points to the first
61  * block in the chain. Note there is not a fixed place in the pool
62  * to hold blocks. They are dynamically allocated and freed as
63  * needed from the blocks available. Figure X shows the ZIL structure:
64  */
65 
66 /*
67  * This global ZIL switch affects all pools
68  */
69 int zil_disable = 0;	/* disable intent logging */
70 
71 /*
72  * Tunable parameter for debugging or performance analysis.  Setting
73  * zfs_nocacheflush will cause corruption on power loss if a volatile
74  * out-of-order write cache is enabled.
75  */
76 boolean_t zfs_nocacheflush = B_FALSE;
77 
78 static kmem_cache_t *zil_lwb_cache;
79 
80 static int
81 zil_dva_compare(const void *x1, const void *x2)
82 {
83 	const dva_t *dva1 = x1;
84 	const dva_t *dva2 = x2;
85 
86 	if (DVA_GET_VDEV(dva1) < DVA_GET_VDEV(dva2))
87 		return (-1);
88 	if (DVA_GET_VDEV(dva1) > DVA_GET_VDEV(dva2))
89 		return (1);
90 
91 	if (DVA_GET_OFFSET(dva1) < DVA_GET_OFFSET(dva2))
92 		return (-1);
93 	if (DVA_GET_OFFSET(dva1) > DVA_GET_OFFSET(dva2))
94 		return (1);
95 
96 	return (0);
97 }
98 
99 static void
100 zil_dva_tree_init(avl_tree_t *t)
101 {
102 	avl_create(t, zil_dva_compare, sizeof (zil_dva_node_t),
103 	    offsetof(zil_dva_node_t, zn_node));
104 }
105 
106 static void
107 zil_dva_tree_fini(avl_tree_t *t)
108 {
109 	zil_dva_node_t *zn;
110 	void *cookie = NULL;
111 
112 	while ((zn = avl_destroy_nodes(t, &cookie)) != NULL)
113 		kmem_free(zn, sizeof (zil_dva_node_t));
114 
115 	avl_destroy(t);
116 }
117 
118 static int
119 zil_dva_tree_add(avl_tree_t *t, dva_t *dva)
120 {
121 	zil_dva_node_t *zn;
122 	avl_index_t where;
123 
124 	if (avl_find(t, dva, &where) != NULL)
125 		return (EEXIST);
126 
127 	zn = kmem_alloc(sizeof (zil_dva_node_t), KM_SLEEP);
128 	zn->zn_dva = *dva;
129 	avl_insert(t, zn, where);
130 
131 	return (0);
132 }
133 
134 static zil_header_t *
135 zil_header_in_syncing_context(zilog_t *zilog)
136 {
137 	return ((zil_header_t *)zilog->zl_header);
138 }
139 
140 static void
141 zil_init_log_chain(zilog_t *zilog, blkptr_t *bp)
142 {
143 	zio_cksum_t *zc = &bp->blk_cksum;
144 
145 	zc->zc_word[ZIL_ZC_GUID_0] = spa_get_random(-1ULL);
146 	zc->zc_word[ZIL_ZC_GUID_1] = spa_get_random(-1ULL);
147 	zc->zc_word[ZIL_ZC_OBJSET] = dmu_objset_id(zilog->zl_os);
148 	zc->zc_word[ZIL_ZC_SEQ] = 1ULL;
149 }
150 
151 /*
152  * Read a log block, make sure it's valid, and byteswap it if necessary.
153  */
154 static int
155 zil_read_log_block(zilog_t *zilog, const blkptr_t *bp, arc_buf_t **abufpp)
156 {
157 	blkptr_t blk = *bp;
158 	zbookmark_t zb;
159 	uint32_t aflags = ARC_WAIT;
160 	int error;
161 
162 	zb.zb_objset = bp->blk_cksum.zc_word[ZIL_ZC_OBJSET];
163 	zb.zb_object = 0;
164 	zb.zb_level = -1;
165 	zb.zb_blkid = bp->blk_cksum.zc_word[ZIL_ZC_SEQ];
166 
167 	*abufpp = NULL;
168 
169 	/*
170 	 * We shouldn't be doing any scrubbing while we're doing log
171 	 * replay, it's OK to not lock.
172 	 */
173 	error = arc_read_nolock(NULL, zilog->zl_spa, &blk,
174 	    arc_getbuf_func, abufpp, ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL |
175 	    ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB, &aflags, &zb);
176 
177 	if (error == 0) {
178 		char *data = (*abufpp)->b_data;
179 		uint64_t blksz = BP_GET_LSIZE(bp);
180 		zil_trailer_t *ztp = (zil_trailer_t *)(data + blksz) - 1;
181 		zio_cksum_t cksum = bp->blk_cksum;
182 
183 		/*
184 		 * Validate the checksummed log block.
185 		 *
186 		 * Sequence numbers should be... sequential.  The checksum
187 		 * verifier for the next block should be bp's checksum plus 1.
188 		 *
189 		 * Also check the log chain linkage and size used.
190 		 */
191 		cksum.zc_word[ZIL_ZC_SEQ]++;
192 
193 		if (bcmp(&cksum, &ztp->zit_next_blk.blk_cksum,
194 		    sizeof (cksum)) || BP_IS_HOLE(&ztp->zit_next_blk) ||
195 		    (ztp->zit_nused > (blksz - sizeof (zil_trailer_t)))) {
196 			error = ECKSUM;
197 		}
198 
199 		if (error) {
200 			VERIFY(arc_buf_remove_ref(*abufpp, abufpp) == 1);
201 			*abufpp = NULL;
202 		}
203 	}
204 
205 	dprintf("error %d on %llu:%llu\n", error, zb.zb_objset, zb.zb_blkid);
206 
207 	return (error);
208 }
209 
210 /*
211  * Parse the intent log, and call parse_func for each valid record within.
212  * Return the highest sequence number.
213  */
214 uint64_t
215 zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func,
216     zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg)
217 {
218 	const zil_header_t *zh = zilog->zl_header;
219 	uint64_t claim_seq = zh->zh_claim_seq;
220 	uint64_t seq = 0;
221 	uint64_t max_seq = 0;
222 	blkptr_t blk = zh->zh_log;
223 	arc_buf_t *abuf;
224 	char *lrbuf, *lrp;
225 	zil_trailer_t *ztp;
226 	int reclen, error;
227 
228 	if (BP_IS_HOLE(&blk))
229 		return (max_seq);
230 
231 	/*
232 	 * Starting at the block pointed to by zh_log we read the log chain.
233 	 * For each block in the chain we strongly check that block to
234 	 * ensure its validity.  We stop when an invalid block is found.
235 	 * For each block pointer in the chain we call parse_blk_func().
236 	 * For each record in each valid block we call parse_lr_func().
237 	 * If the log has been claimed, stop if we encounter a sequence
238 	 * number greater than the highest claimed sequence number.
239 	 */
240 	zil_dva_tree_init(&zilog->zl_dva_tree);
241 	for (;;) {
242 		seq = blk.blk_cksum.zc_word[ZIL_ZC_SEQ];
243 
244 		if (claim_seq != 0 && seq > claim_seq)
245 			break;
246 
247 		ASSERT(max_seq < seq);
248 		max_seq = seq;
249 
250 		error = zil_read_log_block(zilog, &blk, &abuf);
251 
252 		if (parse_blk_func != NULL)
253 			parse_blk_func(zilog, &blk, arg, txg);
254 
255 		if (error)
256 			break;
257 
258 		lrbuf = abuf->b_data;
259 		ztp = (zil_trailer_t *)(lrbuf + BP_GET_LSIZE(&blk)) - 1;
260 		blk = ztp->zit_next_blk;
261 
262 		if (parse_lr_func == NULL) {
263 			VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
264 			continue;
265 		}
266 
267 		for (lrp = lrbuf; lrp < lrbuf + ztp->zit_nused; lrp += reclen) {
268 			lr_t *lr = (lr_t *)lrp;
269 			reclen = lr->lrc_reclen;
270 			ASSERT3U(reclen, >=, sizeof (lr_t));
271 			parse_lr_func(zilog, lr, arg, txg);
272 		}
273 		VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
274 	}
275 	zil_dva_tree_fini(&zilog->zl_dva_tree);
276 
277 	return (max_seq);
278 }
279 
280 /* ARGSUSED */
281 static void
282 zil_claim_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t first_txg)
283 {
284 	spa_t *spa = zilog->zl_spa;
285 	int err;
286 
287 	/*
288 	 * Claim log block if not already committed and not already claimed.
289 	 */
290 	if (bp->blk_birth >= first_txg &&
291 	    zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp)) == 0) {
292 		err = zio_wait(zio_claim(NULL, spa, first_txg, bp, NULL, NULL,
293 		    ZIO_FLAG_MUSTSUCCEED));
294 		ASSERT(err == 0);
295 	}
296 }
297 
298 static void
299 zil_claim_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg)
300 {
301 	if (lrc->lrc_txtype == TX_WRITE) {
302 		lr_write_t *lr = (lr_write_t *)lrc;
303 		zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg);
304 	}
305 }
306 
307 /* ARGSUSED */
308 static void
309 zil_free_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t claim_txg)
310 {
311 	zio_free_blk(zilog->zl_spa, bp, dmu_tx_get_txg(tx));
312 }
313 
314 static void
315 zil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg)
316 {
317 	/*
318 	 * If we previously claimed it, we need to free it.
319 	 */
320 	if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE) {
321 		lr_write_t *lr = (lr_write_t *)lrc;
322 		blkptr_t *bp = &lr->lr_blkptr;
323 		if (bp->blk_birth >= claim_txg &&
324 		    !zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp))) {
325 			(void) arc_free(NULL, zilog->zl_spa,
326 			    dmu_tx_get_txg(tx), bp, NULL, NULL, ARC_WAIT);
327 		}
328 	}
329 }
330 
331 /*
332  * Create an on-disk intent log.
333  */
334 static void
335 zil_create(zilog_t *zilog)
336 {
337 	const zil_header_t *zh = zilog->zl_header;
338 	lwb_t *lwb;
339 	uint64_t txg = 0;
340 	dmu_tx_t *tx = NULL;
341 	blkptr_t blk;
342 	int error = 0;
343 
344 	/*
345 	 * Wait for any previous destroy to complete.
346 	 */
347 	txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
348 
349 	ASSERT(zh->zh_claim_txg == 0);
350 	ASSERT(zh->zh_replay_seq == 0);
351 
352 	blk = zh->zh_log;
353 
354 	/*
355 	 * If we don't already have an initial log block or we have one
356 	 * but it's the wrong endianness then allocate one.
357 	 */
358 	if (BP_IS_HOLE(&blk) || BP_SHOULD_BYTESWAP(&blk)) {
359 		tx = dmu_tx_create(zilog->zl_os);
360 		(void) dmu_tx_assign(tx, TXG_WAIT);
361 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
362 		txg = dmu_tx_get_txg(tx);
363 
364 		if (!BP_IS_HOLE(&blk)) {
365 			zio_free_blk(zilog->zl_spa, &blk, txg);
366 			BP_ZERO(&blk);
367 		}
368 
369 		error = zio_alloc_blk(zilog->zl_spa, ZIL_MIN_BLKSZ, &blk,
370 		    NULL, txg, zilog->zl_logbias != ZFS_LOGBIAS_LATENCY);
371 
372 		if (error == 0)
373 			zil_init_log_chain(zilog, &blk);
374 	}
375 
376 	/*
377 	 * Allocate a log write buffer (lwb) for the first log block.
378 	 */
379 	if (error == 0) {
380 		lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP);
381 		lwb->lwb_zilog = zilog;
382 		lwb->lwb_blk = blk;
383 		lwb->lwb_nused = 0;
384 		lwb->lwb_sz = BP_GET_LSIZE(&lwb->lwb_blk);
385 		lwb->lwb_buf = zio_buf_alloc(lwb->lwb_sz);
386 		lwb->lwb_max_txg = txg;
387 		lwb->lwb_zio = NULL;
388 
389 		mutex_enter(&zilog->zl_lock);
390 		list_insert_tail(&zilog->zl_lwb_list, lwb);
391 		mutex_exit(&zilog->zl_lock);
392 	}
393 
394 	/*
395 	 * If we just allocated the first log block, commit our transaction
396 	 * and wait for zil_sync() to stuff the block poiner into zh_log.
397 	 * (zh is part of the MOS, so we cannot modify it in open context.)
398 	 */
399 	if (tx != NULL) {
400 		dmu_tx_commit(tx);
401 		txg_wait_synced(zilog->zl_dmu_pool, txg);
402 	}
403 
404 	ASSERT(bcmp(&blk, &zh->zh_log, sizeof (blk)) == 0);
405 }
406 
407 /*
408  * In one tx, free all log blocks and clear the log header.
409  * If keep_first is set, then we're replaying a log with no content.
410  * We want to keep the first block, however, so that the first
411  * synchronous transaction doesn't require a txg_wait_synced()
412  * in zil_create().  We don't need to txg_wait_synced() here either
413  * when keep_first is set, because both zil_create() and zil_destroy()
414  * will wait for any in-progress destroys to complete.
415  */
416 void
417 zil_destroy(zilog_t *zilog, boolean_t keep_first)
418 {
419 	const zil_header_t *zh = zilog->zl_header;
420 	lwb_t *lwb;
421 	dmu_tx_t *tx;
422 	uint64_t txg;
423 
424 	/*
425 	 * Wait for any previous destroy to complete.
426 	 */
427 	txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
428 
429 	if (BP_IS_HOLE(&zh->zh_log))
430 		return;
431 
432 	tx = dmu_tx_create(zilog->zl_os);
433 	(void) dmu_tx_assign(tx, TXG_WAIT);
434 	dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
435 	txg = dmu_tx_get_txg(tx);
436 
437 	mutex_enter(&zilog->zl_lock);
438 
439 	/*
440 	 * It is possible for the ZIL to get the previously mounted zilog
441 	 * structure of the same dataset if quickly remounted and the dbuf
442 	 * eviction has not completed. In this case we can see a non
443 	 * empty lwb list and keep_first will be set. We fix this by
444 	 * clearing the keep_first. This will be slower but it's very rare.
445 	 */
446 	if (!list_is_empty(&zilog->zl_lwb_list) && keep_first)
447 		keep_first = B_FALSE;
448 
449 	ASSERT3U(zilog->zl_destroy_txg, <, txg);
450 	zilog->zl_destroy_txg = txg;
451 	zilog->zl_keep_first = keep_first;
452 
453 	if (!list_is_empty(&zilog->zl_lwb_list)) {
454 		ASSERT(zh->zh_claim_txg == 0);
455 		ASSERT(!keep_first);
456 		while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
457 			list_remove(&zilog->zl_lwb_list, lwb);
458 			if (lwb->lwb_buf != NULL)
459 				zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
460 			zio_free_blk(zilog->zl_spa, &lwb->lwb_blk, txg);
461 			kmem_cache_free(zil_lwb_cache, lwb);
462 		}
463 	} else {
464 		if (!keep_first) {
465 			(void) zil_parse(zilog, zil_free_log_block,
466 			    zil_free_log_record, tx, zh->zh_claim_txg);
467 		}
468 	}
469 	mutex_exit(&zilog->zl_lock);
470 
471 	dmu_tx_commit(tx);
472 }
473 
474 /*
475  * return true if the initial log block is not valid
476  */
477 static boolean_t
478 zil_empty(zilog_t *zilog)
479 {
480 	const zil_header_t *zh = zilog->zl_header;
481 	arc_buf_t *abuf = NULL;
482 
483 	if (BP_IS_HOLE(&zh->zh_log))
484 		return (B_TRUE);
485 
486 	if (zil_read_log_block(zilog, &zh->zh_log, &abuf) != 0)
487 		return (B_TRUE);
488 
489 	VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
490 	return (B_FALSE);
491 }
492 
493 int
494 zil_claim(char *osname, void *txarg)
495 {
496 	dmu_tx_t *tx = txarg;
497 	uint64_t first_txg = dmu_tx_get_txg(tx);
498 	zilog_t *zilog;
499 	zil_header_t *zh;
500 	objset_t *os;
501 	int error;
502 
503 	error = dmu_objset_hold(osname, FTAG, &os);
504 	if (error) {
505 		cmn_err(CE_WARN, "can't open objset for %s", osname);
506 		return (0);
507 	}
508 
509 	zilog = dmu_objset_zil(os);
510 	zh = zil_header_in_syncing_context(zilog);
511 
512 	if (zilog->zl_spa->spa_log_state == SPA_LOG_CLEAR) {
513 		if (!BP_IS_HOLE(&zh->zh_log))
514 			zio_free_blk(zilog->zl_spa, &zh->zh_log, first_txg);
515 		BP_ZERO(&zh->zh_log);
516 		dsl_dataset_dirty(dmu_objset_ds(os), tx);
517 	}
518 
519 	/*
520 	 * Record here whether the zil has any records to replay.
521 	 * If the header block pointer is null or the block points
522 	 * to the stubby then we know there are no valid log records.
523 	 * We use the header to store this state as the the zilog gets
524 	 * freed later in dmu_objset_close().
525 	 * The flags (and the rest of the header fields) are cleared in
526 	 * zil_sync() as a result of a zil_destroy(), after replaying the log.
527 	 *
528 	 * Note, the intent log can be empty but still need the
529 	 * stubby to be claimed.
530 	 */
531 	if (!zil_empty(zilog)) {
532 		zh->zh_flags |= ZIL_REPLAY_NEEDED;
533 		dsl_dataset_dirty(dmu_objset_ds(os), tx);
534 	}
535 
536 	/*
537 	 * Claim all log blocks if we haven't already done so, and remember
538 	 * the highest claimed sequence number.  This ensures that if we can
539 	 * read only part of the log now (e.g. due to a missing device),
540 	 * but we can read the entire log later, we will not try to replay
541 	 * or destroy beyond the last block we successfully claimed.
542 	 */
543 	ASSERT3U(zh->zh_claim_txg, <=, first_txg);
544 	if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) {
545 		zh->zh_claim_txg = first_txg;
546 		zh->zh_claim_seq = zil_parse(zilog, zil_claim_log_block,
547 		    zil_claim_log_record, tx, first_txg);
548 		dsl_dataset_dirty(dmu_objset_ds(os), tx);
549 	}
550 
551 	ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1));
552 	dmu_objset_rele(os, FTAG);
553 	return (0);
554 }
555 
556 /*
557  * Check the log by walking the log chain.
558  * Checksum errors are ok as they indicate the end of the chain.
559  * Any other error (no device or read failure) returns an error.
560  */
561 /* ARGSUSED */
562 int
563 zil_check_log_chain(char *osname, void *txarg)
564 {
565 	zilog_t *zilog;
566 	zil_header_t *zh;
567 	blkptr_t blk;
568 	arc_buf_t *abuf;
569 	objset_t *os;
570 	char *lrbuf;
571 	zil_trailer_t *ztp;
572 	int error;
573 
574 	error = dmu_objset_hold(osname, FTAG, &os);
575 	if (error) {
576 		cmn_err(CE_WARN, "can't open objset for %s", osname);
577 		return (0);
578 	}
579 
580 	zilog = dmu_objset_zil(os);
581 	zh = zil_header_in_syncing_context(zilog);
582 	blk = zh->zh_log;
583 	if (BP_IS_HOLE(&blk)) {
584 		dmu_objset_rele(os, FTAG);
585 		return (0); /* no chain */
586 	}
587 
588 	for (;;) {
589 		error = zil_read_log_block(zilog, &blk, &abuf);
590 		if (error)
591 			break;
592 		lrbuf = abuf->b_data;
593 		ztp = (zil_trailer_t *)(lrbuf + BP_GET_LSIZE(&blk)) - 1;
594 		blk = ztp->zit_next_blk;
595 		VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
596 	}
597 	dmu_objset_rele(os, FTAG);
598 	if (error == ECKSUM)
599 		return (0); /* normal end of chain */
600 	return (error);
601 }
602 
603 static int
604 zil_vdev_compare(const void *x1, const void *x2)
605 {
606 	uint64_t v1 = ((zil_vdev_node_t *)x1)->zv_vdev;
607 	uint64_t v2 = ((zil_vdev_node_t *)x2)->zv_vdev;
608 
609 	if (v1 < v2)
610 		return (-1);
611 	if (v1 > v2)
612 		return (1);
613 
614 	return (0);
615 }
616 
617 void
618 zil_add_block(zilog_t *zilog, blkptr_t *bp)
619 {
620 	avl_tree_t *t = &zilog->zl_vdev_tree;
621 	avl_index_t where;
622 	zil_vdev_node_t *zv, zvsearch;
623 	int ndvas = BP_GET_NDVAS(bp);
624 	int i;
625 
626 	if (zfs_nocacheflush)
627 		return;
628 
629 	ASSERT(zilog->zl_writer);
630 
631 	/*
632 	 * Even though we're zl_writer, we still need a lock because the
633 	 * zl_get_data() callbacks may have dmu_sync() done callbacks
634 	 * that will run concurrently.
635 	 */
636 	mutex_enter(&zilog->zl_vdev_lock);
637 	for (i = 0; i < ndvas; i++) {
638 		zvsearch.zv_vdev = DVA_GET_VDEV(&bp->blk_dva[i]);
639 		if (avl_find(t, &zvsearch, &where) == NULL) {
640 			zv = kmem_alloc(sizeof (*zv), KM_SLEEP);
641 			zv->zv_vdev = zvsearch.zv_vdev;
642 			avl_insert(t, zv, where);
643 		}
644 	}
645 	mutex_exit(&zilog->zl_vdev_lock);
646 }
647 
648 void
649 zil_flush_vdevs(zilog_t *zilog)
650 {
651 	spa_t *spa = zilog->zl_spa;
652 	avl_tree_t *t = &zilog->zl_vdev_tree;
653 	void *cookie = NULL;
654 	zil_vdev_node_t *zv;
655 	zio_t *zio;
656 
657 	ASSERT(zilog->zl_writer);
658 
659 	/*
660 	 * We don't need zl_vdev_lock here because we're the zl_writer,
661 	 * and all zl_get_data() callbacks are done.
662 	 */
663 	if (avl_numnodes(t) == 0)
664 		return;
665 
666 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
667 
668 	zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
669 
670 	while ((zv = avl_destroy_nodes(t, &cookie)) != NULL) {
671 		vdev_t *vd = vdev_lookup_top(spa, zv->zv_vdev);
672 		if (vd != NULL)
673 			zio_flush(zio, vd);
674 		kmem_free(zv, sizeof (*zv));
675 	}
676 
677 	/*
678 	 * Wait for all the flushes to complete.  Not all devices actually
679 	 * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails.
680 	 */
681 	(void) zio_wait(zio);
682 
683 	spa_config_exit(spa, SCL_STATE, FTAG);
684 }
685 
686 /*
687  * Function called when a log block write completes
688  */
689 static void
690 zil_lwb_write_done(zio_t *zio)
691 {
692 	lwb_t *lwb = zio->io_private;
693 	zilog_t *zilog = lwb->lwb_zilog;
694 
695 	ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF);
696 	ASSERT(BP_GET_CHECKSUM(zio->io_bp) == ZIO_CHECKSUM_ZILOG);
697 	ASSERT(BP_GET_TYPE(zio->io_bp) == DMU_OT_INTENT_LOG);
698 	ASSERT(BP_GET_LEVEL(zio->io_bp) == 0);
699 	ASSERT(BP_GET_BYTEORDER(zio->io_bp) == ZFS_HOST_BYTEORDER);
700 	ASSERT(!BP_IS_GANG(zio->io_bp));
701 	ASSERT(!BP_IS_HOLE(zio->io_bp));
702 	ASSERT(zio->io_bp->blk_fill == 0);
703 
704 	/*
705 	 * Ensure the lwb buffer pointer is cleared before releasing
706 	 * the txg. If we have had an allocation failure and
707 	 * the txg is waiting to sync then we want want zil_sync()
708 	 * to remove the lwb so that it's not picked up as the next new
709 	 * one in zil_commit_writer(). zil_sync() will only remove
710 	 * the lwb if lwb_buf is null.
711 	 */
712 	zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
713 	mutex_enter(&zilog->zl_lock);
714 	lwb->lwb_buf = NULL;
715 	if (zio->io_error)
716 		zilog->zl_log_error = B_TRUE;
717 
718 	/*
719 	 * Now that we've written this log block, we have a stable pointer
720 	 * to the next block in the chain, so it's OK to let the txg in
721 	 * which we allocated the next block sync. We still have the
722 	 * zl_lock to ensure zil_sync doesn't kmem free the lwb.
723 	 */
724 	txg_rele_to_sync(&lwb->lwb_txgh);
725 	mutex_exit(&zilog->zl_lock);
726 }
727 
728 /*
729  * Initialize the io for a log block.
730  */
731 static void
732 zil_lwb_write_init(zilog_t *zilog, lwb_t *lwb)
733 {
734 	zbookmark_t zb;
735 
736 	zb.zb_objset = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET];
737 	zb.zb_object = 0;
738 	zb.zb_level = -1;
739 	zb.zb_blkid = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ];
740 
741 	if (zilog->zl_root_zio == NULL) {
742 		zilog->zl_root_zio = zio_root(zilog->zl_spa, NULL, NULL,
743 		    ZIO_FLAG_CANFAIL);
744 	}
745 	if (lwb->lwb_zio == NULL) {
746 		lwb->lwb_zio = zio_rewrite(zilog->zl_root_zio, zilog->zl_spa,
747 		    0, &lwb->lwb_blk, lwb->lwb_buf, lwb->lwb_sz,
748 		    zil_lwb_write_done, lwb, ZIO_PRIORITY_LOG_WRITE,
749 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, &zb);
750 	}
751 }
752 
753 /*
754  * Start a log block write and advance to the next log block.
755  * Calls are serialized.
756  */
757 static lwb_t *
758 zil_lwb_write_start(zilog_t *zilog, lwb_t *lwb)
759 {
760 	lwb_t *nlwb;
761 	zil_trailer_t *ztp = (zil_trailer_t *)(lwb->lwb_buf + lwb->lwb_sz) - 1;
762 	spa_t *spa = zilog->zl_spa;
763 	blkptr_t *bp = &ztp->zit_next_blk;
764 	uint64_t txg;
765 	uint64_t zil_blksz;
766 	int error;
767 
768 	ASSERT(lwb->lwb_nused <= ZIL_BLK_DATA_SZ(lwb));
769 
770 	/*
771 	 * Allocate the next block and save its address in this block
772 	 * before writing it in order to establish the log chain.
773 	 * Note that if the allocation of nlwb synced before we wrote
774 	 * the block that points at it (lwb), we'd leak it if we crashed.
775 	 * Therefore, we don't do txg_rele_to_sync() until zil_lwb_write_done().
776 	 */
777 	txg = txg_hold_open(zilog->zl_dmu_pool, &lwb->lwb_txgh);
778 	txg_rele_to_quiesce(&lwb->lwb_txgh);
779 
780 	/*
781 	 * Pick a ZIL blocksize. We request a size that is the
782 	 * maximum of the previous used size, the current used size and
783 	 * the amount waiting in the queue.
784 	 */
785 	zil_blksz = MAX(zilog->zl_prev_used,
786 	    zilog->zl_cur_used + sizeof (*ztp));
787 	zil_blksz = MAX(zil_blksz, zilog->zl_itx_list_sz + sizeof (*ztp));
788 	zil_blksz = P2ROUNDUP_TYPED(zil_blksz, ZIL_MIN_BLKSZ, uint64_t);
789 	if (zil_blksz > ZIL_MAX_BLKSZ)
790 		zil_blksz = ZIL_MAX_BLKSZ;
791 
792 	BP_ZERO(bp);
793 	/* pass the old blkptr in order to spread log blocks across devs */
794 	error = zio_alloc_blk(spa, zil_blksz, bp, &lwb->lwb_blk, txg,
795 	    zilog->zl_logbias != ZFS_LOGBIAS_LATENCY);
796 	if (error) {
797 		dmu_tx_t *tx = dmu_tx_create_assigned(zilog->zl_dmu_pool, txg);
798 
799 		/*
800 		 * We dirty the dataset to ensure that zil_sync() will
801 		 * be called to remove this lwb from our zl_lwb_list.
802 		 * Failing to do so, may leave an lwb with a NULL lwb_buf
803 		 * hanging around on the zl_lwb_list.
804 		 */
805 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
806 		dmu_tx_commit(tx);
807 
808 		/*
809 		 * Since we've just experienced an allocation failure so we
810 		 * terminate the current lwb and send it on its way.
811 		 */
812 		ztp->zit_pad = 0;
813 		ztp->zit_nused = lwb->lwb_nused;
814 		ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum;
815 		zio_nowait(lwb->lwb_zio);
816 
817 		/*
818 		 * By returning NULL the caller will call tx_wait_synced()
819 		 */
820 		return (NULL);
821 	}
822 
823 	ASSERT3U(bp->blk_birth, ==, txg);
824 	ztp->zit_pad = 0;
825 	ztp->zit_nused = lwb->lwb_nused;
826 	ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum;
827 	bp->blk_cksum = lwb->lwb_blk.blk_cksum;
828 	bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++;
829 
830 	/*
831 	 * Allocate a new log write buffer (lwb).
832 	 */
833 	nlwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP);
834 
835 	nlwb->lwb_zilog = zilog;
836 	nlwb->lwb_blk = *bp;
837 	nlwb->lwb_nused = 0;
838 	nlwb->lwb_sz = BP_GET_LSIZE(&nlwb->lwb_blk);
839 	nlwb->lwb_buf = zio_buf_alloc(nlwb->lwb_sz);
840 	nlwb->lwb_max_txg = txg;
841 	nlwb->lwb_zio = NULL;
842 
843 	/*
844 	 * Put new lwb at the end of the log chain
845 	 */
846 	mutex_enter(&zilog->zl_lock);
847 	list_insert_tail(&zilog->zl_lwb_list, nlwb);
848 	mutex_exit(&zilog->zl_lock);
849 
850 	/* Record the block for later vdev flushing */
851 	zil_add_block(zilog, &lwb->lwb_blk);
852 
853 	/*
854 	 * kick off the write for the old log block
855 	 */
856 	dprintf_bp(&lwb->lwb_blk, "lwb %p txg %llu: ", lwb, txg);
857 	ASSERT(lwb->lwb_zio);
858 	zio_nowait(lwb->lwb_zio);
859 
860 	return (nlwb);
861 }
862 
863 static lwb_t *
864 zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb)
865 {
866 	lr_t *lrc = &itx->itx_lr; /* common log record */
867 	lr_write_t *lr = (lr_write_t *)lrc;
868 	uint64_t txg = lrc->lrc_txg;
869 	uint64_t reclen = lrc->lrc_reclen;
870 	uint64_t dlen;
871 
872 	if (lwb == NULL)
873 		return (NULL);
874 	ASSERT(lwb->lwb_buf != NULL);
875 
876 	if (lrc->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY)
877 		dlen = P2ROUNDUP_TYPED(
878 		    lr->lr_length, sizeof (uint64_t), uint64_t);
879 	else
880 		dlen = 0;
881 
882 	zilog->zl_cur_used += (reclen + dlen);
883 
884 	zil_lwb_write_init(zilog, lwb);
885 
886 	/*
887 	 * If this record won't fit in the current log block, start a new one.
888 	 */
889 	if (lwb->lwb_nused + reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) {
890 		lwb = zil_lwb_write_start(zilog, lwb);
891 		if (lwb == NULL)
892 			return (NULL);
893 		zil_lwb_write_init(zilog, lwb);
894 		ASSERT(lwb->lwb_nused == 0);
895 		if (reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) {
896 			txg_wait_synced(zilog->zl_dmu_pool, txg);
897 			return (lwb);
898 		}
899 	}
900 
901 	/*
902 	 * Update the lrc_seq, to be log record sequence number. See zil.h
903 	 * Then copy the record to the log buffer.
904 	 */
905 	lrc->lrc_seq = ++zilog->zl_lr_seq; /* we are single threaded */
906 	bcopy(lrc, lwb->lwb_buf + lwb->lwb_nused, reclen);
907 
908 	/*
909 	 * If it's a write, fetch the data or get its blkptr as appropriate.
910 	 */
911 	if (lrc->lrc_txtype == TX_WRITE) {
912 		if (txg > spa_freeze_txg(zilog->zl_spa))
913 			txg_wait_synced(zilog->zl_dmu_pool, txg);
914 		if (itx->itx_wr_state != WR_COPIED) {
915 			char *dbuf;
916 			int error;
917 
918 			/* alignment is guaranteed */
919 			lr = (lr_write_t *)(lwb->lwb_buf + lwb->lwb_nused);
920 			if (dlen) {
921 				ASSERT(itx->itx_wr_state == WR_NEED_COPY);
922 				dbuf = lwb->lwb_buf + lwb->lwb_nused + reclen;
923 				lr->lr_common.lrc_reclen += dlen;
924 			} else {
925 				ASSERT(itx->itx_wr_state == WR_INDIRECT);
926 				dbuf = NULL;
927 			}
928 			error = zilog->zl_get_data(
929 			    itx->itx_private, lr, dbuf, lwb->lwb_zio);
930 			if (error == EIO) {
931 				txg_wait_synced(zilog->zl_dmu_pool, txg);
932 				return (lwb);
933 			}
934 			if (error) {
935 				ASSERT(error == ENOENT || error == EEXIST ||
936 				    error == EALREADY);
937 				return (lwb);
938 			}
939 		}
940 	}
941 
942 	lwb->lwb_nused += reclen + dlen;
943 	lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg);
944 	ASSERT3U(lwb->lwb_nused, <=, ZIL_BLK_DATA_SZ(lwb));
945 	ASSERT3U(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)), ==, 0);
946 
947 	return (lwb);
948 }
949 
950 itx_t *
951 zil_itx_create(uint64_t txtype, size_t lrsize)
952 {
953 	itx_t *itx;
954 
955 	lrsize = P2ROUNDUP_TYPED(lrsize, sizeof (uint64_t), size_t);
956 
957 	itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, KM_SLEEP);
958 	itx->itx_lr.lrc_txtype = txtype;
959 	itx->itx_lr.lrc_reclen = lrsize;
960 	itx->itx_sod = lrsize; /* if write & WR_NEED_COPY will be increased */
961 	itx->itx_lr.lrc_seq = 0;	/* defensive */
962 
963 	return (itx);
964 }
965 
966 uint64_t
967 zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx)
968 {
969 	uint64_t seq;
970 
971 	ASSERT(itx->itx_lr.lrc_seq == 0);
972 
973 	mutex_enter(&zilog->zl_lock);
974 	list_insert_tail(&zilog->zl_itx_list, itx);
975 	zilog->zl_itx_list_sz += itx->itx_sod;
976 	itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx);
977 	itx->itx_lr.lrc_seq = seq = ++zilog->zl_itx_seq;
978 	mutex_exit(&zilog->zl_lock);
979 
980 	return (seq);
981 }
982 
983 /*
984  * Free up all in-memory intent log transactions that have now been synced.
985  */
986 static void
987 zil_itx_clean(zilog_t *zilog)
988 {
989 	uint64_t synced_txg = spa_last_synced_txg(zilog->zl_spa);
990 	uint64_t freeze_txg = spa_freeze_txg(zilog->zl_spa);
991 	list_t clean_list;
992 	itx_t *itx;
993 
994 	list_create(&clean_list, sizeof (itx_t), offsetof(itx_t, itx_node));
995 
996 	mutex_enter(&zilog->zl_lock);
997 	/* wait for a log writer to finish walking list */
998 	while (zilog->zl_writer) {
999 		cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
1000 	}
1001 
1002 	/*
1003 	 * Move the sync'd log transactions to a separate list so we can call
1004 	 * kmem_free without holding the zl_lock.
1005 	 *
1006 	 * There is no need to set zl_writer as we don't drop zl_lock here
1007 	 */
1008 	while ((itx = list_head(&zilog->zl_itx_list)) != NULL &&
1009 	    itx->itx_lr.lrc_txg <= MIN(synced_txg, freeze_txg)) {
1010 		list_remove(&zilog->zl_itx_list, itx);
1011 		zilog->zl_itx_list_sz -= itx->itx_sod;
1012 		list_insert_tail(&clean_list, itx);
1013 	}
1014 	cv_broadcast(&zilog->zl_cv_writer);
1015 	mutex_exit(&zilog->zl_lock);
1016 
1017 	/* destroy sync'd log transactions */
1018 	while ((itx = list_head(&clean_list)) != NULL) {
1019 		list_remove(&clean_list, itx);
1020 		kmem_free(itx, offsetof(itx_t, itx_lr)
1021 		    + itx->itx_lr.lrc_reclen);
1022 	}
1023 	list_destroy(&clean_list);
1024 }
1025 
1026 /*
1027  * If there are any in-memory intent log transactions which have now been
1028  * synced then start up a taskq to free them.
1029  */
1030 void
1031 zil_clean(zilog_t *zilog)
1032 {
1033 	itx_t *itx;
1034 
1035 	mutex_enter(&zilog->zl_lock);
1036 	itx = list_head(&zilog->zl_itx_list);
1037 	if ((itx != NULL) &&
1038 	    (itx->itx_lr.lrc_txg <= spa_last_synced_txg(zilog->zl_spa))) {
1039 		(void) taskq_dispatch(zilog->zl_clean_taskq,
1040 		    (task_func_t *)zil_itx_clean, zilog, TQ_SLEEP);
1041 	}
1042 	mutex_exit(&zilog->zl_lock);
1043 }
1044 
1045 static void
1046 zil_commit_writer(zilog_t *zilog, uint64_t seq, uint64_t foid)
1047 {
1048 	uint64_t txg;
1049 	uint64_t commit_seq = 0;
1050 	itx_t *itx, *itx_next = (itx_t *)-1;
1051 	lwb_t *lwb;
1052 	spa_t *spa;
1053 
1054 	zilog->zl_writer = B_TRUE;
1055 	ASSERT(zilog->zl_root_zio == NULL);
1056 	spa = zilog->zl_spa;
1057 
1058 	if (zilog->zl_suspend) {
1059 		lwb = NULL;
1060 	} else {
1061 		lwb = list_tail(&zilog->zl_lwb_list);
1062 		if (lwb == NULL) {
1063 			/*
1064 			 * Return if there's nothing to flush before we
1065 			 * dirty the fs by calling zil_create()
1066 			 */
1067 			if (list_is_empty(&zilog->zl_itx_list)) {
1068 				zilog->zl_writer = B_FALSE;
1069 				return;
1070 			}
1071 			mutex_exit(&zilog->zl_lock);
1072 			zil_create(zilog);
1073 			mutex_enter(&zilog->zl_lock);
1074 			lwb = list_tail(&zilog->zl_lwb_list);
1075 		}
1076 	}
1077 
1078 	/* Loop through in-memory log transactions filling log blocks. */
1079 	DTRACE_PROBE1(zil__cw1, zilog_t *, zilog);
1080 	for (;;) {
1081 		/*
1082 		 * Find the next itx to push:
1083 		 * Push all transactions related to specified foid and all
1084 		 * other transactions except TX_WRITE, TX_TRUNCATE,
1085 		 * TX_SETATTR and TX_ACL for all other files.
1086 		 */
1087 		if (itx_next != (itx_t *)-1)
1088 			itx = itx_next;
1089 		else
1090 			itx = list_head(&zilog->zl_itx_list);
1091 		for (; itx != NULL; itx = list_next(&zilog->zl_itx_list, itx)) {
1092 			if (foid == 0) /* push all foids? */
1093 				break;
1094 			if (itx->itx_sync) /* push all O_[D]SYNC */
1095 				break;
1096 			switch (itx->itx_lr.lrc_txtype) {
1097 			case TX_SETATTR:
1098 			case TX_WRITE:
1099 			case TX_TRUNCATE:
1100 			case TX_ACL:
1101 				/* lr_foid is same offset for these records */
1102 				if (((lr_write_t *)&itx->itx_lr)->lr_foid
1103 				    != foid) {
1104 					continue; /* skip this record */
1105 				}
1106 			}
1107 			break;
1108 		}
1109 		if (itx == NULL)
1110 			break;
1111 
1112 		if ((itx->itx_lr.lrc_seq > seq) &&
1113 		    ((lwb == NULL) || (lwb->lwb_nused == 0) ||
1114 		    (lwb->lwb_nused + itx->itx_sod > ZIL_BLK_DATA_SZ(lwb)))) {
1115 			break;
1116 		}
1117 
1118 		/*
1119 		 * Save the next pointer.  Even though we soon drop
1120 		 * zl_lock all threads that may change the list
1121 		 * (another writer or zil_itx_clean) can't do so until
1122 		 * they have zl_writer.
1123 		 */
1124 		itx_next = list_next(&zilog->zl_itx_list, itx);
1125 		list_remove(&zilog->zl_itx_list, itx);
1126 		zilog->zl_itx_list_sz -= itx->itx_sod;
1127 		mutex_exit(&zilog->zl_lock);
1128 		txg = itx->itx_lr.lrc_txg;
1129 		ASSERT(txg);
1130 
1131 		if (txg > spa_last_synced_txg(spa) ||
1132 		    txg > spa_freeze_txg(spa))
1133 			lwb = zil_lwb_commit(zilog, itx, lwb);
1134 		kmem_free(itx, offsetof(itx_t, itx_lr)
1135 		    + itx->itx_lr.lrc_reclen);
1136 		mutex_enter(&zilog->zl_lock);
1137 	}
1138 	DTRACE_PROBE1(zil__cw2, zilog_t *, zilog);
1139 	/* determine commit sequence number */
1140 	itx = list_head(&zilog->zl_itx_list);
1141 	if (itx)
1142 		commit_seq = itx->itx_lr.lrc_seq;
1143 	else
1144 		commit_seq = zilog->zl_itx_seq;
1145 	mutex_exit(&zilog->zl_lock);
1146 
1147 	/* write the last block out */
1148 	if (lwb != NULL && lwb->lwb_zio != NULL)
1149 		lwb = zil_lwb_write_start(zilog, lwb);
1150 
1151 	zilog->zl_prev_used = zilog->zl_cur_used;
1152 	zilog->zl_cur_used = 0;
1153 
1154 	/*
1155 	 * Wait if necessary for the log blocks to be on stable storage.
1156 	 */
1157 	if (zilog->zl_root_zio) {
1158 		DTRACE_PROBE1(zil__cw3, zilog_t *, zilog);
1159 		(void) zio_wait(zilog->zl_root_zio);
1160 		zilog->zl_root_zio = NULL;
1161 		DTRACE_PROBE1(zil__cw4, zilog_t *, zilog);
1162 		zil_flush_vdevs(zilog);
1163 	}
1164 
1165 	if (zilog->zl_log_error || lwb == NULL) {
1166 		zilog->zl_log_error = 0;
1167 		txg_wait_synced(zilog->zl_dmu_pool, 0);
1168 	}
1169 
1170 	mutex_enter(&zilog->zl_lock);
1171 	zilog->zl_writer = B_FALSE;
1172 
1173 	ASSERT3U(commit_seq, >=, zilog->zl_commit_seq);
1174 	zilog->zl_commit_seq = commit_seq;
1175 }
1176 
1177 /*
1178  * Push zfs transactions to stable storage up to the supplied sequence number.
1179  * If foid is 0 push out all transactions, otherwise push only those
1180  * for that file or might have been used to create that file.
1181  */
1182 void
1183 zil_commit(zilog_t *zilog, uint64_t seq, uint64_t foid)
1184 {
1185 	if (zilog == NULL || seq == 0)
1186 		return;
1187 
1188 	mutex_enter(&zilog->zl_lock);
1189 
1190 	seq = MIN(seq, zilog->zl_itx_seq);	/* cap seq at largest itx seq */
1191 
1192 	while (zilog->zl_writer) {
1193 		cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
1194 		if (seq < zilog->zl_commit_seq) {
1195 			mutex_exit(&zilog->zl_lock);
1196 			return;
1197 		}
1198 	}
1199 	zil_commit_writer(zilog, seq, foid); /* drops zl_lock */
1200 	/* wake up others waiting on the commit */
1201 	cv_broadcast(&zilog->zl_cv_writer);
1202 	mutex_exit(&zilog->zl_lock);
1203 }
1204 
1205 /*
1206  * Called in syncing context to free committed log blocks and update log header.
1207  */
1208 void
1209 zil_sync(zilog_t *zilog, dmu_tx_t *tx)
1210 {
1211 	zil_header_t *zh = zil_header_in_syncing_context(zilog);
1212 	uint64_t txg = dmu_tx_get_txg(tx);
1213 	spa_t *spa = zilog->zl_spa;
1214 	lwb_t *lwb;
1215 
1216 	/*
1217 	 * We don't zero out zl_destroy_txg, so make sure we don't try
1218 	 * to destroy it twice.
1219 	 */
1220 	if (spa_sync_pass(spa) != 1)
1221 		return;
1222 
1223 	mutex_enter(&zilog->zl_lock);
1224 
1225 	ASSERT(zilog->zl_stop_sync == 0);
1226 
1227 	zh->zh_replay_seq = zilog->zl_replayed_seq[txg & TXG_MASK];
1228 
1229 	if (zilog->zl_destroy_txg == txg) {
1230 		blkptr_t blk = zh->zh_log;
1231 
1232 		ASSERT(list_head(&zilog->zl_lwb_list) == NULL);
1233 
1234 		bzero(zh, sizeof (zil_header_t));
1235 		bzero(zilog->zl_replayed_seq, sizeof (zilog->zl_replayed_seq));
1236 
1237 		if (zilog->zl_keep_first) {
1238 			/*
1239 			 * If this block was part of log chain that couldn't
1240 			 * be claimed because a device was missing during
1241 			 * zil_claim(), but that device later returns,
1242 			 * then this block could erroneously appear valid.
1243 			 * To guard against this, assign a new GUID to the new
1244 			 * log chain so it doesn't matter what blk points to.
1245 			 */
1246 			zil_init_log_chain(zilog, &blk);
1247 			zh->zh_log = blk;
1248 		}
1249 	}
1250 
1251 	while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
1252 		zh->zh_log = lwb->lwb_blk;
1253 		if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg)
1254 			break;
1255 		list_remove(&zilog->zl_lwb_list, lwb);
1256 		zio_free_blk(spa, &lwb->lwb_blk, txg);
1257 		kmem_cache_free(zil_lwb_cache, lwb);
1258 
1259 		/*
1260 		 * If we don't have anything left in the lwb list then
1261 		 * we've had an allocation failure and we need to zero
1262 		 * out the zil_header blkptr so that we don't end
1263 		 * up freeing the same block twice.
1264 		 */
1265 		if (list_head(&zilog->zl_lwb_list) == NULL)
1266 			BP_ZERO(&zh->zh_log);
1267 	}
1268 	mutex_exit(&zilog->zl_lock);
1269 }
1270 
1271 void
1272 zil_init(void)
1273 {
1274 	zil_lwb_cache = kmem_cache_create("zil_lwb_cache",
1275 	    sizeof (struct lwb), 0, NULL, NULL, NULL, NULL, NULL, 0);
1276 }
1277 
1278 void
1279 zil_fini(void)
1280 {
1281 	kmem_cache_destroy(zil_lwb_cache);
1282 }
1283 
1284 void
1285 zil_set_logbias(zilog_t *zilog, uint64_t logbias)
1286 {
1287 	zilog->zl_logbias = logbias;
1288 }
1289 
1290 zilog_t *
1291 zil_alloc(objset_t *os, zil_header_t *zh_phys)
1292 {
1293 	zilog_t *zilog;
1294 
1295 	zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP);
1296 
1297 	zilog->zl_header = zh_phys;
1298 	zilog->zl_os = os;
1299 	zilog->zl_spa = dmu_objset_spa(os);
1300 	zilog->zl_dmu_pool = dmu_objset_pool(os);
1301 	zilog->zl_destroy_txg = TXG_INITIAL - 1;
1302 	zilog->zl_logbias = dmu_objset_logbias(os);
1303 
1304 	mutex_init(&zilog->zl_lock, NULL, MUTEX_DEFAULT, NULL);
1305 
1306 	list_create(&zilog->zl_itx_list, sizeof (itx_t),
1307 	    offsetof(itx_t, itx_node));
1308 
1309 	list_create(&zilog->zl_lwb_list, sizeof (lwb_t),
1310 	    offsetof(lwb_t, lwb_node));
1311 
1312 	mutex_init(&zilog->zl_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
1313 
1314 	avl_create(&zilog->zl_vdev_tree, zil_vdev_compare,
1315 	    sizeof (zil_vdev_node_t), offsetof(zil_vdev_node_t, zv_node));
1316 
1317 	cv_init(&zilog->zl_cv_writer, NULL, CV_DEFAULT, NULL);
1318 	cv_init(&zilog->zl_cv_suspend, NULL, CV_DEFAULT, NULL);
1319 
1320 	return (zilog);
1321 }
1322 
1323 void
1324 zil_free(zilog_t *zilog)
1325 {
1326 	lwb_t *lwb;
1327 
1328 	zilog->zl_stop_sync = 1;
1329 
1330 	while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
1331 		list_remove(&zilog->zl_lwb_list, lwb);
1332 		if (lwb->lwb_buf != NULL)
1333 			zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
1334 		kmem_cache_free(zil_lwb_cache, lwb);
1335 	}
1336 	list_destroy(&zilog->zl_lwb_list);
1337 
1338 	avl_destroy(&zilog->zl_vdev_tree);
1339 	mutex_destroy(&zilog->zl_vdev_lock);
1340 
1341 	ASSERT(list_head(&zilog->zl_itx_list) == NULL);
1342 	list_destroy(&zilog->zl_itx_list);
1343 	mutex_destroy(&zilog->zl_lock);
1344 
1345 	cv_destroy(&zilog->zl_cv_writer);
1346 	cv_destroy(&zilog->zl_cv_suspend);
1347 
1348 	kmem_free(zilog, sizeof (zilog_t));
1349 }
1350 
1351 /*
1352  * Open an intent log.
1353  */
1354 zilog_t *
1355 zil_open(objset_t *os, zil_get_data_t *get_data)
1356 {
1357 	zilog_t *zilog = dmu_objset_zil(os);
1358 
1359 	zilog->zl_get_data = get_data;
1360 	zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri,
1361 	    2, 2, TASKQ_PREPOPULATE);
1362 
1363 	return (zilog);
1364 }
1365 
1366 /*
1367  * Close an intent log.
1368  */
1369 void
1370 zil_close(zilog_t *zilog)
1371 {
1372 	/*
1373 	 * If the log isn't already committed, mark the objset dirty
1374 	 * (so zil_sync() will be called) and wait for that txg to sync.
1375 	 */
1376 	if (!zil_is_committed(zilog)) {
1377 		uint64_t txg;
1378 		dmu_tx_t *tx = dmu_tx_create(zilog->zl_os);
1379 		(void) dmu_tx_assign(tx, TXG_WAIT);
1380 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
1381 		txg = dmu_tx_get_txg(tx);
1382 		dmu_tx_commit(tx);
1383 		txg_wait_synced(zilog->zl_dmu_pool, txg);
1384 	}
1385 
1386 	taskq_destroy(zilog->zl_clean_taskq);
1387 	zilog->zl_clean_taskq = NULL;
1388 	zilog->zl_get_data = NULL;
1389 
1390 	zil_itx_clean(zilog);
1391 	ASSERT(list_head(&zilog->zl_itx_list) == NULL);
1392 }
1393 
1394 /*
1395  * Suspend an intent log.  While in suspended mode, we still honor
1396  * synchronous semantics, but we rely on txg_wait_synced() to do it.
1397  * We suspend the log briefly when taking a snapshot so that the snapshot
1398  * contains all the data it's supposed to, and has an empty intent log.
1399  */
1400 int
1401 zil_suspend(zilog_t *zilog)
1402 {
1403 	const zil_header_t *zh = zilog->zl_header;
1404 
1405 	mutex_enter(&zilog->zl_lock);
1406 	if (zh->zh_flags & ZIL_REPLAY_NEEDED) {		/* unplayed log */
1407 		mutex_exit(&zilog->zl_lock);
1408 		return (EBUSY);
1409 	}
1410 	if (zilog->zl_suspend++ != 0) {
1411 		/*
1412 		 * Someone else already began a suspend.
1413 		 * Just wait for them to finish.
1414 		 */
1415 		while (zilog->zl_suspending)
1416 			cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock);
1417 		mutex_exit(&zilog->zl_lock);
1418 		return (0);
1419 	}
1420 	zilog->zl_suspending = B_TRUE;
1421 	mutex_exit(&zilog->zl_lock);
1422 
1423 	zil_commit(zilog, UINT64_MAX, 0);
1424 
1425 	/*
1426 	 * Wait for any in-flight log writes to complete.
1427 	 */
1428 	mutex_enter(&zilog->zl_lock);
1429 	while (zilog->zl_writer)
1430 		cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
1431 	mutex_exit(&zilog->zl_lock);
1432 
1433 	zil_destroy(zilog, B_FALSE);
1434 
1435 	mutex_enter(&zilog->zl_lock);
1436 	zilog->zl_suspending = B_FALSE;
1437 	cv_broadcast(&zilog->zl_cv_suspend);
1438 	mutex_exit(&zilog->zl_lock);
1439 
1440 	return (0);
1441 }
1442 
1443 void
1444 zil_resume(zilog_t *zilog)
1445 {
1446 	mutex_enter(&zilog->zl_lock);
1447 	ASSERT(zilog->zl_suspend != 0);
1448 	zilog->zl_suspend--;
1449 	mutex_exit(&zilog->zl_lock);
1450 }
1451 
1452 typedef struct zil_replay_arg {
1453 	objset_t	*zr_os;
1454 	zil_replay_func_t **zr_replay;
1455 	void		*zr_arg;
1456 	boolean_t	zr_byteswap;
1457 	char		*zr_lrbuf;
1458 } zil_replay_arg_t;
1459 
1460 static void
1461 zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg)
1462 {
1463 	zil_replay_arg_t *zr = zra;
1464 	const zil_header_t *zh = zilog->zl_header;
1465 	uint64_t reclen = lr->lrc_reclen;
1466 	uint64_t txtype = lr->lrc_txtype;
1467 	char *name;
1468 	int pass, error;
1469 
1470 	if (!zilog->zl_replay)			/* giving up */
1471 		return;
1472 
1473 	if (lr->lrc_txg < claim_txg)		/* already committed */
1474 		return;
1475 
1476 	if (lr->lrc_seq <= zh->zh_replay_seq)	/* already replayed */
1477 		return;
1478 
1479 	/* Strip case-insensitive bit, still present in log record */
1480 	txtype &= ~TX_CI;
1481 
1482 	if (txtype == 0 || txtype >= TX_MAX_TYPE) {
1483 		error = EINVAL;
1484 		goto bad;
1485 	}
1486 
1487 	/*
1488 	 * Make a copy of the data so we can revise and extend it.
1489 	 */
1490 	bcopy(lr, zr->zr_lrbuf, reclen);
1491 
1492 	/*
1493 	 * The log block containing this lr may have been byteswapped
1494 	 * so that we can easily examine common fields like lrc_txtype.
1495 	 * However, the log is a mix of different data types, and only the
1496 	 * replay vectors know how to byteswap their records.  Therefore, if
1497 	 * the lr was byteswapped, undo it before invoking the replay vector.
1498 	 */
1499 	if (zr->zr_byteswap)
1500 		byteswap_uint64_array(zr->zr_lrbuf, reclen);
1501 
1502 	/*
1503 	 * If this is a TX_WRITE with a blkptr, suck in the data.
1504 	 */
1505 	if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) {
1506 		lr_write_t *lrw = (lr_write_t *)lr;
1507 		blkptr_t *wbp = &lrw->lr_blkptr;
1508 		uint64_t wlen = lrw->lr_length;
1509 		char *wbuf = zr->zr_lrbuf + reclen;
1510 
1511 		if (BP_IS_HOLE(wbp)) {	/* compressed to a hole */
1512 			bzero(wbuf, wlen);
1513 		} else {
1514 			/*
1515 			 * A subsequent write may have overwritten this block,
1516 			 * in which case wbp may have been been freed and
1517 			 * reallocated, and our read of wbp may fail with a
1518 			 * checksum error.  We can safely ignore this because
1519 			 * the later write will provide the correct data.
1520 			 */
1521 			zbookmark_t zb;
1522 
1523 			zb.zb_objset = dmu_objset_id(zilog->zl_os);
1524 			zb.zb_object = lrw->lr_foid;
1525 			zb.zb_level = -1;
1526 			zb.zb_blkid = lrw->lr_offset / BP_GET_LSIZE(wbp);
1527 
1528 			(void) zio_wait(zio_read(NULL, zilog->zl_spa,
1529 			    wbp, wbuf, BP_GET_LSIZE(wbp), NULL, NULL,
1530 			    ZIO_PRIORITY_SYNC_READ,
1531 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, &zb));
1532 			(void) memmove(wbuf, wbuf + lrw->lr_blkoff, wlen);
1533 		}
1534 	}
1535 
1536 	/*
1537 	 * We must now do two things atomically: replay this log record,
1538 	 * and update the log header sequence number to reflect the fact that
1539 	 * we did so. At the end of each replay function the sequence number
1540 	 * is updated if we are in replay mode.
1541 	 */
1542 	for (pass = 1; pass <= 2; pass++) {
1543 		zilog->zl_replaying_seq = lr->lrc_seq;
1544 		/* Only byteswap (if needed) on the 1st pass.  */
1545 		error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lrbuf,
1546 		    zr->zr_byteswap && pass == 1);
1547 
1548 		if (!error)
1549 			return;
1550 
1551 		/*
1552 		 * The DMU's dnode layer doesn't see removes until the txg
1553 		 * commits, so a subsequent claim can spuriously fail with
1554 		 * EEXIST. So if we receive any error we try syncing out
1555 		 * any removes then retry the transaction.
1556 		 */
1557 		if (pass == 1)
1558 			txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0);
1559 	}
1560 
1561 bad:
1562 	ASSERT(error);
1563 	name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1564 	dmu_objset_name(zr->zr_os, name);
1565 	cmn_err(CE_WARN, "ZFS replay transaction error %d, "
1566 	    "dataset %s, seq 0x%llx, txtype %llu %s\n",
1567 	    error, name, (u_longlong_t)lr->lrc_seq, (u_longlong_t)txtype,
1568 	    (lr->lrc_txtype & TX_CI) ? "CI" : "");
1569 	zilog->zl_replay = B_FALSE;
1570 	kmem_free(name, MAXNAMELEN);
1571 }
1572 
1573 /* ARGSUSED */
1574 static void
1575 zil_incr_blks(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
1576 {
1577 	zilog->zl_replay_blks++;
1578 }
1579 
1580 /*
1581  * If this dataset has a non-empty intent log, replay it and destroy it.
1582  */
1583 void
1584 zil_replay(objset_t *os, void *arg, zil_replay_func_t *replay_func[TX_MAX_TYPE])
1585 {
1586 	zilog_t *zilog = dmu_objset_zil(os);
1587 	const zil_header_t *zh = zilog->zl_header;
1588 	zil_replay_arg_t zr;
1589 
1590 	if ((zh->zh_flags & ZIL_REPLAY_NEEDED) == 0) {
1591 		zil_destroy(zilog, B_TRUE);
1592 		return;
1593 	}
1594 
1595 	zr.zr_os = os;
1596 	zr.zr_replay = replay_func;
1597 	zr.zr_arg = arg;
1598 	zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log);
1599 	zr.zr_lrbuf = kmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP);
1600 
1601 	/*
1602 	 * Wait for in-progress removes to sync before starting replay.
1603 	 */
1604 	txg_wait_synced(zilog->zl_dmu_pool, 0);
1605 
1606 	zilog->zl_replay = B_TRUE;
1607 	zilog->zl_replay_time = lbolt;
1608 	ASSERT(zilog->zl_replay_blks == 0);
1609 	(void) zil_parse(zilog, zil_incr_blks, zil_replay_log_record, &zr,
1610 	    zh->zh_claim_txg);
1611 	kmem_free(zr.zr_lrbuf, 2 * SPA_MAXBLOCKSIZE);
1612 
1613 	zil_destroy(zilog, B_FALSE);
1614 	txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
1615 	zilog->zl_replay = B_FALSE;
1616 }
1617 
1618 /*
1619  * Report whether all transactions are committed
1620  */
1621 int
1622 zil_is_committed(zilog_t *zilog)
1623 {
1624 	lwb_t *lwb;
1625 	int ret;
1626 
1627 	mutex_enter(&zilog->zl_lock);
1628 	while (zilog->zl_writer)
1629 		cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
1630 
1631 	/* recent unpushed intent log transactions? */
1632 	if (!list_is_empty(&zilog->zl_itx_list)) {
1633 		ret = B_FALSE;
1634 		goto out;
1635 	}
1636 
1637 	/* intent log never used? */
1638 	lwb = list_head(&zilog->zl_lwb_list);
1639 	if (lwb == NULL) {
1640 		ret = B_TRUE;
1641 		goto out;
1642 	}
1643 
1644 	/*
1645 	 * more than 1 log buffer means zil_sync() hasn't yet freed
1646 	 * entries after a txg has committed
1647 	 */
1648 	if (list_next(&zilog->zl_lwb_list, lwb)) {
1649 		ret = B_FALSE;
1650 		goto out;
1651 	}
1652 
1653 	ASSERT(zil_empty(zilog));
1654 	ret = B_TRUE;
1655 out:
1656 	cv_broadcast(&zilog->zl_cv_writer);
1657 	mutex_exit(&zilog->zl_lock);
1658 	return (ret);
1659 }
1660 
1661 /* ARGSUSED */
1662 int
1663 zil_vdev_offline(char *osname, void *arg)
1664 {
1665 	objset_t *os;
1666 	zilog_t *zilog;
1667 	int error;
1668 
1669 	error = dmu_objset_hold(osname, FTAG, &os);
1670 	if (error)
1671 		return (error);
1672 
1673 	zilog = dmu_objset_zil(os);
1674 	if (zil_suspend(zilog) != 0)
1675 		error = EEXIST;
1676 	else
1677 		zil_resume(zilog);
1678 	dmu_objset_rele(os, FTAG);
1679 	return (error);
1680 }
1681