xref: /titanic_41/usr/src/uts/common/fs/zfs/zil.c (revision 445f2479fe3d7435daab18bf2cdc310b86cd6738)
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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
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.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  * These global ZIL switches affect all pools
68  */
69 int zil_disable = 0;	/* disable intent logging */
70 int zil_always = 0;	/* make every transaction synchronous */
71 int zil_purge = 0;	/* at pool open, just throw everything away */
72 int zil_noflush = 0;	/* don't flush write cache buffers on disks */
73 
74 static kmem_cache_t *zil_lwb_cache;
75 
76 static int
77 zil_dva_compare(const void *x1, const void *x2)
78 {
79 	const dva_t *dva1 = x1;
80 	const dva_t *dva2 = x2;
81 
82 	if (DVA_GET_VDEV(dva1) < DVA_GET_VDEV(dva2))
83 		return (-1);
84 	if (DVA_GET_VDEV(dva1) > DVA_GET_VDEV(dva2))
85 		return (1);
86 
87 	if (DVA_GET_OFFSET(dva1) < DVA_GET_OFFSET(dva2))
88 		return (-1);
89 	if (DVA_GET_OFFSET(dva1) > DVA_GET_OFFSET(dva2))
90 		return (1);
91 
92 	return (0);
93 }
94 
95 static void
96 zil_dva_tree_init(avl_tree_t *t)
97 {
98 	avl_create(t, zil_dva_compare, sizeof (zil_dva_node_t),
99 	    offsetof(zil_dva_node_t, zn_node));
100 }
101 
102 static void
103 zil_dva_tree_fini(avl_tree_t *t)
104 {
105 	zil_dva_node_t *zn;
106 	void *cookie = NULL;
107 
108 	while ((zn = avl_destroy_nodes(t, &cookie)) != NULL)
109 		kmem_free(zn, sizeof (zil_dva_node_t));
110 
111 	avl_destroy(t);
112 }
113 
114 static int
115 zil_dva_tree_add(avl_tree_t *t, dva_t *dva)
116 {
117 	zil_dva_node_t *zn;
118 	avl_index_t where;
119 
120 	if (avl_find(t, dva, &where) != NULL)
121 		return (EEXIST);
122 
123 	zn = kmem_alloc(sizeof (zil_dva_node_t), KM_SLEEP);
124 	zn->zn_dva = *dva;
125 	avl_insert(t, zn, where);
126 
127 	return (0);
128 }
129 
130 /*
131  * Read a log block, make sure it's valid, and byteswap it if necessary.
132  */
133 static int
134 zil_read_log_block(zilog_t *zilog, blkptr_t *bp, char *buf)
135 {
136 	uint64_t blksz = BP_GET_LSIZE(bp);
137 	zil_trailer_t *ztp = (zil_trailer_t *)(buf + blksz) - 1;
138 	zio_cksum_t cksum;
139 	zbookmark_t zb;
140 	int error;
141 
142 	zb.zb_objset = bp->blk_cksum.zc_word[2];
143 	zb.zb_object = 0;
144 	zb.zb_level = -1;
145 	zb.zb_blkid = bp->blk_cksum.zc_word[3];
146 
147 	error = zio_wait(zio_read(NULL, zilog->zl_spa, bp, buf, blksz,
148 	    NULL, NULL, ZIO_PRIORITY_SYNC_READ,
149 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, &zb));
150 	if (error) {
151 		dprintf_bp(bp, "zilog %p bp %p read failed, error %d: ",
152 		    zilog, bp, error);
153 		return (error);
154 	}
155 
156 	if (BP_SHOULD_BYTESWAP(bp))
157 		byteswap_uint64_array(buf, blksz);
158 
159 	/*
160 	 * Sequence numbers should be... sequential.  The checksum verifier for
161 	 * the next block should be: <logid[0], logid[1], objset id, seq + 1>.
162 	 */
163 	cksum = bp->blk_cksum;
164 	cksum.zc_word[3]++;
165 	if (bcmp(&cksum, &ztp->zit_next_blk.blk_cksum, sizeof (cksum)) != 0) {
166 		dprintf_bp(bp, "zilog %p bp %p stale pointer: ", zilog, bp);
167 		return (ESTALE);
168 	}
169 
170 	if (BP_IS_HOLE(&ztp->zit_next_blk)) {
171 		dprintf_bp(bp, "zilog %p bp %p hole: ", zilog, bp);
172 		return (ENOENT);
173 	}
174 
175 	if (ztp->zit_nused > (blksz - sizeof (zil_trailer_t))) {
176 		dprintf("zilog %p bp %p nused exceeds blksz\n", zilog, bp);
177 		return (EOVERFLOW);
178 	}
179 
180 	dprintf_bp(bp, "zilog %p bp %p good block: ", zilog, bp);
181 
182 	return (0);
183 }
184 
185 /*
186  * Parse the intent log, and call parse_func for each valid record within.
187  */
188 void
189 zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func,
190     zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg)
191 {
192 	blkptr_t blk;
193 	char *lrbuf, *lrp;
194 	zil_trailer_t *ztp;
195 	int reclen, error;
196 
197 	blk = zilog->zl_header->zh_log;
198 	if (BP_IS_HOLE(&blk))
199 		return;
200 
201 	/*
202 	 * Starting at the block pointed to by zh_log we read the log chain.
203 	 * For each block in the chain we strongly check that block to
204 	 * ensure its validity.  We stop when an invalid block is found.
205 	 * For each block pointer in the chain we call parse_blk_func().
206 	 * For each record in each valid block we call parse_lr_func().
207 	 */
208 	zil_dva_tree_init(&zilog->zl_dva_tree);
209 	lrbuf = zio_buf_alloc(SPA_MAXBLOCKSIZE);
210 	for (;;) {
211 		error = zil_read_log_block(zilog, &blk, lrbuf);
212 
213 		if (parse_blk_func != NULL)
214 			parse_blk_func(zilog, &blk, arg, txg);
215 
216 		if (error)
217 			break;
218 
219 		ztp = (zil_trailer_t *)(lrbuf + BP_GET_LSIZE(&blk)) - 1;
220 		blk = ztp->zit_next_blk;
221 
222 		if (parse_lr_func == NULL)
223 			continue;
224 
225 		for (lrp = lrbuf; lrp < lrbuf + ztp->zit_nused; lrp += reclen) {
226 			lr_t *lr = (lr_t *)lrp;
227 			reclen = lr->lrc_reclen;
228 			ASSERT3U(reclen, >=, sizeof (lr_t));
229 			parse_lr_func(zilog, lr, arg, txg);
230 		}
231 	}
232 	zio_buf_free(lrbuf, SPA_MAXBLOCKSIZE);
233 	zil_dva_tree_fini(&zilog->zl_dva_tree);
234 }
235 
236 /* ARGSUSED */
237 static void
238 zil_claim_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t first_txg)
239 {
240 	spa_t *spa = zilog->zl_spa;
241 	int err;
242 
243 	dprintf_bp(bp, "first_txg %llu: ", first_txg);
244 
245 	/*
246 	 * Claim log block if not already committed and not already claimed.
247 	 */
248 	if (bp->blk_birth >= first_txg &&
249 	    zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp)) == 0) {
250 		err = zio_wait(zio_claim(NULL, spa, first_txg, bp, NULL, NULL));
251 		ASSERT(err == 0);
252 	}
253 }
254 
255 static void
256 zil_claim_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg)
257 {
258 	if (lrc->lrc_txtype == TX_WRITE) {
259 		lr_write_t *lr = (lr_write_t *)lrc;
260 		zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg);
261 	}
262 }
263 
264 /* ARGSUSED */
265 static void
266 zil_free_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t claim_txg)
267 {
268 	zio_free_blk(zilog->zl_spa, bp, dmu_tx_get_txg(tx));
269 }
270 
271 static void
272 zil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg)
273 {
274 	/*
275 	 * If we previously claimed it, we need to free it.
276 	 */
277 	if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE) {
278 		lr_write_t *lr = (lr_write_t *)lrc;
279 		blkptr_t *bp = &lr->lr_blkptr;
280 		if (bp->blk_birth >= claim_txg &&
281 		    !zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp))) {
282 			(void) arc_free(NULL, zilog->zl_spa,
283 			    dmu_tx_get_txg(tx), bp, NULL, NULL, ARC_WAIT);
284 		}
285 	}
286 }
287 
288 /*
289  * Create an on-disk intent log.
290  */
291 static void
292 zil_create(zilog_t *zilog)
293 {
294 	lwb_t *lwb;
295 	uint64_t txg;
296 	dmu_tx_t *tx;
297 	blkptr_t blk;
298 	int error;
299 	int no_blk;
300 
301 	ASSERT(zilog->zl_header->zh_claim_txg == 0);
302 	ASSERT(zilog->zl_header->zh_replay_seq == 0);
303 
304 	/*
305 	 * Initialize the log header block.
306 	 */
307 	tx = dmu_tx_create(zilog->zl_os);
308 	(void) dmu_tx_assign(tx, TXG_WAIT);
309 	dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
310 	txg = dmu_tx_get_txg(tx);
311 
312 	/*
313 	 * If we don't have a log block already then
314 	 * allocate the first log block and assign its checksum verifier.
315 	 */
316 	no_blk = BP_IS_HOLE(&zilog->zl_header->zh_log);
317 	if (no_blk) {
318 		error = zio_alloc_blk(zilog->zl_spa, ZIO_CHECKSUM_ZILOG,
319 		    ZIL_MIN_BLKSZ, &blk, txg);
320 	} else {
321 		blk = zilog->zl_header->zh_log;
322 		error = 0;
323 	}
324 	if (error == 0) {
325 		ZIO_SET_CHECKSUM(&blk.blk_cksum,
326 		    spa_get_random(-1ULL), spa_get_random(-1ULL),
327 		    dmu_objset_id(zilog->zl_os), 1ULL);
328 
329 		/*
330 		 * Allocate a log write buffer (lwb) for the first log block.
331 		 */
332 		lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP);
333 		lwb->lwb_zilog = zilog;
334 		lwb->lwb_blk = blk;
335 		lwb->lwb_nused = 0;
336 		lwb->lwb_sz = BP_GET_LSIZE(&lwb->lwb_blk);
337 		lwb->lwb_buf = zio_buf_alloc(lwb->lwb_sz);
338 		lwb->lwb_max_txg = txg;
339 		lwb->lwb_seq = 0;
340 		lwb->lwb_state = UNWRITTEN;
341 		mutex_enter(&zilog->zl_lock);
342 		list_insert_tail(&zilog->zl_lwb_list, lwb);
343 		mutex_exit(&zilog->zl_lock);
344 	}
345 
346 	dmu_tx_commit(tx);
347 	if (no_blk)
348 		txg_wait_synced(zilog->zl_dmu_pool, txg);
349 }
350 
351 /*
352  * In one tx, free all log blocks and clear the log header.
353  */
354 void
355 zil_destroy(zilog_t *zilog)
356 {
357 	dmu_tx_t *tx;
358 	uint64_t txg;
359 
360 	mutex_enter(&zilog->zl_destroy_lock);
361 
362 	if (BP_IS_HOLE(&zilog->zl_header->zh_log)) {
363 		mutex_exit(&zilog->zl_destroy_lock);
364 		return;
365 	}
366 
367 	tx = dmu_tx_create(zilog->zl_os);
368 	(void) dmu_tx_assign(tx, TXG_WAIT);
369 	dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
370 	txg = dmu_tx_get_txg(tx);
371 
372 	zil_parse(zilog, zil_free_log_block, zil_free_log_record, tx,
373 	    zilog->zl_header->zh_claim_txg);
374 	/*
375 	 * zil_sync clears the zil header as soon as the zl_destroy_txg commits
376 	 */
377 	zilog->zl_destroy_txg = txg;
378 
379 	dmu_tx_commit(tx);
380 	txg_wait_synced(zilog->zl_dmu_pool, txg);
381 
382 	mutex_exit(&zilog->zl_destroy_lock);
383 }
384 
385 void
386 zil_claim(char *osname, void *txarg)
387 {
388 	dmu_tx_t *tx = txarg;
389 	uint64_t first_txg = dmu_tx_get_txg(tx);
390 	zilog_t *zilog;
391 	zil_header_t *zh;
392 	objset_t *os;
393 	int error;
394 
395 	error = dmu_objset_open(osname, DMU_OST_ANY, DS_MODE_STANDARD, &os);
396 	if (error) {
397 		cmn_err(CE_WARN, "can't process intent log for %s", osname);
398 		return;
399 	}
400 
401 	zilog = dmu_objset_zil(os);
402 	zh = zilog->zl_header;
403 
404 	/*
405 	 * Claim all log blocks if we haven't already done so.
406 	 */
407 	ASSERT3U(zh->zh_claim_txg, <=, first_txg);
408 	if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) {
409 		zh->zh_claim_txg = first_txg;
410 		zil_parse(zilog, zil_claim_log_block, zil_claim_log_record,
411 		    tx, first_txg);
412 		dsl_dataset_dirty(dmu_objset_ds(os), tx);
413 	}
414 	ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1));
415 	dmu_objset_close(os);
416 }
417 
418 void
419 zil_add_vdev(zilog_t *zilog, uint64_t vdev, uint64_t seq)
420 {
421 	zil_vdev_t *zv;
422 
423 	if (zil_noflush)
424 		return;
425 
426 	ASSERT(MUTEX_HELD(&zilog->zl_lock));
427 	zv = kmem_alloc(sizeof (zil_vdev_t), KM_SLEEP);
428 	zv->vdev = vdev;
429 	zv->seq = seq;
430 	list_insert_tail(&zilog->zl_vdev_list, zv);
431 }
432 
433 void
434 zil_flush_vdevs(zilog_t *zilog, uint64_t seq)
435 {
436 	vdev_t *vd;
437 	zil_vdev_t *zv, *zv2;
438 	zio_t *zio;
439 	spa_t *spa;
440 	uint64_t vdev;
441 
442 	if (zil_noflush)
443 		return;
444 
445 	ASSERT(MUTEX_HELD(&zilog->zl_lock));
446 
447 	spa = zilog->zl_spa;
448 	zio = NULL;
449 
450 	while ((zv = list_head(&zilog->zl_vdev_list)) != NULL &&
451 	    zv->seq <= seq) {
452 		vdev = zv->vdev;
453 		list_remove(&zilog->zl_vdev_list, zv);
454 		kmem_free(zv, sizeof (zil_vdev_t));
455 
456 		/*
457 		 * remove all chained entries <= seq with same vdev
458 		 */
459 		zv = list_head(&zilog->zl_vdev_list);
460 		while (zv && zv->seq <= seq) {
461 			zv2 = list_next(&zilog->zl_vdev_list, zv);
462 			if (zv->vdev == vdev) {
463 				list_remove(&zilog->zl_vdev_list, zv);
464 				kmem_free(zv, sizeof (zil_vdev_t));
465 			}
466 			zv = zv2;
467 		}
468 
469 		/* flush the write cache for this vdev */
470 		mutex_exit(&zilog->zl_lock);
471 		if (zio == NULL)
472 			zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
473 		vd = vdev_lookup_top(spa, vdev);
474 		ASSERT(vd);
475 		(void) zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE,
476 		    NULL, NULL, ZIO_PRIORITY_NOW,
477 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
478 		mutex_enter(&zilog->zl_lock);
479 	}
480 
481 	/*
482 	 * Wait for all the flushes to complete.  Not all devices actually
483 	 * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails.
484 	 */
485 	if (zio != NULL) {
486 		mutex_exit(&zilog->zl_lock);
487 		(void) zio_wait(zio);
488 		mutex_enter(&zilog->zl_lock);
489 	}
490 }
491 
492 /*
493  * Function called when a log block write completes
494  */
495 static void
496 zil_lwb_write_done(zio_t *zio)
497 {
498 	lwb_t *prev;
499 	lwb_t *lwb = zio->io_private;
500 	zilog_t *zilog = lwb->lwb_zilog;
501 	uint64_t max_seq;
502 
503 	/*
504 	 * Now that we've written this log block, we have a stable pointer
505 	 * to the next block in the chain, so it's OK to let the txg in
506 	 * which we allocated the next block sync.
507 	 */
508 	txg_rele_to_sync(&lwb->lwb_txgh);
509 
510 	zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
511 	mutex_enter(&zilog->zl_lock);
512 	lwb->lwb_buf = NULL;
513 	if (zio->io_error) {
514 		zilog->zl_log_error = B_TRUE;
515 		mutex_exit(&zilog->zl_lock);
516 		cv_broadcast(&zilog->zl_cv_seq);
517 		return;
518 	}
519 
520 	prev = list_prev(&zilog->zl_lwb_list, lwb);
521 	if (prev && prev->lwb_state != SEQ_COMPLETE) {
522 		/* There's an unwritten buffer in the chain before this one */
523 		lwb->lwb_state = SEQ_INCOMPLETE;
524 		mutex_exit(&zilog->zl_lock);
525 		return;
526 	}
527 
528 	max_seq = lwb->lwb_seq;
529 	lwb->lwb_state = SEQ_COMPLETE;
530 	/*
531 	 * We must also follow up the chain for already written buffers
532 	 * to see if we can set zl_ss_seq even higher.
533 	 */
534 	while (lwb = list_next(&zilog->zl_lwb_list, lwb)) {
535 		if (lwb->lwb_state != SEQ_INCOMPLETE)
536 			break;
537 		lwb->lwb_state = SEQ_COMPLETE;
538 		/* lwb_seq will be zero if we've written an empty buffer */
539 		if (lwb->lwb_seq) {
540 			ASSERT3U(max_seq, <, lwb->lwb_seq);
541 			max_seq = lwb->lwb_seq;
542 		}
543 	}
544 	zilog->zl_ss_seq = MAX(max_seq, zilog->zl_ss_seq);
545 	mutex_exit(&zilog->zl_lock);
546 	cv_broadcast(&zilog->zl_cv_seq);
547 }
548 
549 /*
550  * Start a log block write and advance to the next log block.
551  * Calls are serialized.
552  */
553 static lwb_t *
554 zil_lwb_write_start(zilog_t *zilog, lwb_t *lwb)
555 {
556 	lwb_t *nlwb;
557 	zil_trailer_t *ztp = (zil_trailer_t *)(lwb->lwb_buf + lwb->lwb_sz) - 1;
558 	uint64_t txg;
559 	uint64_t zil_blksz;
560 	zbookmark_t zb;
561 	int error;
562 
563 	ASSERT(lwb->lwb_nused <= ZIL_BLK_DATA_SZ(lwb));
564 
565 	/*
566 	 * Allocate the next block and save its address in this block
567 	 * before writing it in order to establish the log chain.
568 	 * Note that if the allocation of nlwb synced before we wrote
569 	 * the block that points at it (lwb), we'd leak it if we crashed.
570 	 * Therefore, we don't do txg_rele_to_sync() until zil_lwb_write_done().
571 	 */
572 	txg = txg_hold_open(zilog->zl_dmu_pool, &lwb->lwb_txgh);
573 	txg_rele_to_quiesce(&lwb->lwb_txgh);
574 
575 	/*
576 	 * Pick a ZIL blocksize. We request a size that is the
577 	 * maximum of the previous used size, the current used size and
578 	 * the amount waiting in the queue.
579 	 */
580 	zil_blksz = MAX(zilog->zl_cur_used, zilog->zl_prev_used);
581 	zil_blksz = MAX(zil_blksz, zilog->zl_itx_list_sz + sizeof (*ztp));
582 	zil_blksz = P2ROUNDUP(zil_blksz, ZIL_MIN_BLKSZ);
583 	if (zil_blksz > ZIL_MAX_BLKSZ)
584 		zil_blksz = ZIL_MAX_BLKSZ;
585 
586 	error = zio_alloc_blk(zilog->zl_spa, ZIO_CHECKSUM_ZILOG,
587 	    zil_blksz, &ztp->zit_next_blk, txg);
588 	if (error) {
589 		/*
590 		 * Reinitialise the lwb.
591 		 * By returning NULL the caller will call tx_wait_synced()
592 		 */
593 		mutex_enter(&zilog->zl_lock);
594 		ASSERT(lwb->lwb_state == UNWRITTEN);
595 		lwb->lwb_nused = 0;
596 		lwb->lwb_seq = 0;
597 		mutex_exit(&zilog->zl_lock);
598 		txg_rele_to_sync(&lwb->lwb_txgh);
599 		return (NULL);
600 	}
601 
602 	ASSERT3U(ztp->zit_next_blk.blk_birth, ==, txg);
603 	ztp->zit_pad = 0;
604 	ztp->zit_nused = lwb->lwb_nused;
605 	ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum;
606 	ztp->zit_next_blk.blk_cksum = lwb->lwb_blk.blk_cksum;
607 	ztp->zit_next_blk.blk_cksum.zc_word[3]++;
608 
609 	/*
610 	 * Allocate a new log write buffer (lwb).
611 	 */
612 	nlwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP);
613 
614 	nlwb->lwb_zilog = zilog;
615 	nlwb->lwb_blk = ztp->zit_next_blk;
616 	nlwb->lwb_nused = 0;
617 	nlwb->lwb_sz = BP_GET_LSIZE(&nlwb->lwb_blk);
618 	nlwb->lwb_buf = zio_buf_alloc(nlwb->lwb_sz);
619 	nlwb->lwb_max_txg = txg;
620 	nlwb->lwb_seq = 0;
621 	nlwb->lwb_state = UNWRITTEN;
622 
623 	/*
624 	 * Put new lwb at the end of the log chain,
625 	 * and record the vdev for later flushing
626 	 */
627 	mutex_enter(&zilog->zl_lock);
628 	list_insert_tail(&zilog->zl_lwb_list, nlwb);
629 	zil_add_vdev(zilog, DVA_GET_VDEV(BP_IDENTITY(&(lwb->lwb_blk))),
630 	    lwb->lwb_seq);
631 	mutex_exit(&zilog->zl_lock);
632 
633 	/*
634 	 * write the old log block
635 	 */
636 	dprintf_bp(&lwb->lwb_blk, "lwb %p txg %llu: ", lwb, txg);
637 
638 	zb.zb_objset = lwb->lwb_blk.blk_cksum.zc_word[2];
639 	zb.zb_object = 0;
640 	zb.zb_level = -1;
641 	zb.zb_blkid = lwb->lwb_blk.blk_cksum.zc_word[3];
642 
643 	zio_nowait(zio_rewrite(NULL, zilog->zl_spa, ZIO_CHECKSUM_ZILOG, 0,
644 	    &lwb->lwb_blk, lwb->lwb_buf, lwb->lwb_sz, zil_lwb_write_done, lwb,
645 	    ZIO_PRIORITY_LOG_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb));
646 
647 	return (nlwb);
648 }
649 
650 static lwb_t *
651 zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb)
652 {
653 	lr_t *lrc = &itx->itx_lr; /* common log record */
654 	uint64_t seq = lrc->lrc_seq;
655 	uint64_t txg = lrc->lrc_txg;
656 	uint64_t reclen = lrc->lrc_reclen;
657 	int error;
658 
659 	if (lwb == NULL)
660 		return (NULL);
661 	ASSERT(lwb->lwb_buf != NULL);
662 
663 	/*
664 	 * If it's a write, fetch the data or get its blkptr as appropriate.
665 	 */
666 	if (lrc->lrc_txtype == TX_WRITE) {
667 		lr_write_t *lr = (lr_write_t *)lrc;
668 		if (txg > spa_freeze_txg(zilog->zl_spa))
669 			txg_wait_synced(zilog->zl_dmu_pool, txg);
670 
671 		if (!itx->itx_data_copied &&
672 		    (error = zilog->zl_get_data(itx->itx_private, lr)) != 0) {
673 			if (error != ENOENT && error != EALREADY) {
674 				txg_wait_synced(zilog->zl_dmu_pool, txg);
675 				mutex_enter(&zilog->zl_lock);
676 				zilog->zl_ss_seq = MAX(seq, zilog->zl_ss_seq);
677 				zil_add_vdev(zilog,
678 				    DVA_GET_VDEV(BP_IDENTITY(&(lr->lr_blkptr))),
679 				    seq);
680 				mutex_exit(&zilog->zl_lock);
681 				return (lwb);
682 			}
683 			mutex_enter(&zilog->zl_lock);
684 			zil_add_vdev(zilog,
685 			    DVA_GET_VDEV(BP_IDENTITY(&(lr->lr_blkptr))), seq);
686 			mutex_exit(&zilog->zl_lock);
687 			return (lwb);
688 		}
689 	}
690 
691 	zilog->zl_cur_used += reclen;
692 
693 	/*
694 	 * If this record won't fit in the current log block, start a new one.
695 	 */
696 	if (lwb->lwb_nused + reclen > ZIL_BLK_DATA_SZ(lwb)) {
697 		lwb = zil_lwb_write_start(zilog, lwb);
698 		if (lwb == NULL)
699 			return (NULL);
700 		ASSERT(lwb->lwb_nused == 0);
701 		if (reclen > ZIL_BLK_DATA_SZ(lwb)) {
702 			txg_wait_synced(zilog->zl_dmu_pool, txg);
703 			mutex_enter(&zilog->zl_lock);
704 			zilog->zl_ss_seq = MAX(seq, zilog->zl_ss_seq);
705 			mutex_exit(&zilog->zl_lock);
706 			return (lwb);
707 		}
708 	}
709 
710 	bcopy(lrc, lwb->lwb_buf + lwb->lwb_nused, reclen);
711 	lwb->lwb_nused += reclen;
712 	lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg);
713 	ASSERT3U(lwb->lwb_seq, <, seq);
714 	lwb->lwb_seq = seq;
715 	ASSERT3U(lwb->lwb_nused, <=, ZIL_BLK_DATA_SZ(lwb));
716 	ASSERT3U(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)), ==, 0);
717 
718 	return (lwb);
719 }
720 
721 itx_t *
722 zil_itx_create(int txtype, size_t lrsize)
723 {
724 	itx_t *itx;
725 
726 	lrsize = P2ROUNDUP(lrsize, sizeof (uint64_t));
727 
728 	itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, KM_SLEEP);
729 	itx->itx_lr.lrc_txtype = txtype;
730 	itx->itx_lr.lrc_reclen = lrsize;
731 	itx->itx_lr.lrc_seq = 0;	/* defensive */
732 
733 	return (itx);
734 }
735 
736 uint64_t
737 zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx)
738 {
739 	uint64_t seq;
740 
741 	ASSERT(itx->itx_lr.lrc_seq == 0);
742 
743 	mutex_enter(&zilog->zl_lock);
744 	list_insert_tail(&zilog->zl_itx_list, itx);
745 	zilog->zl_itx_list_sz += itx->itx_lr.lrc_reclen;
746 	itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx);
747 	itx->itx_lr.lrc_seq = seq = ++zilog->zl_itx_seq;
748 	mutex_exit(&zilog->zl_lock);
749 
750 	return (seq);
751 }
752 
753 /*
754  * Free up all in-memory intent log transactions that have now been synced.
755  */
756 static void
757 zil_itx_clean(zilog_t *zilog)
758 {
759 	uint64_t synced_txg = spa_last_synced_txg(zilog->zl_spa);
760 	uint64_t freeze_txg = spa_freeze_txg(zilog->zl_spa);
761 	uint64_t max_seq = 0;
762 	itx_t *itx;
763 
764 	mutex_enter(&zilog->zl_lock);
765 	while ((itx = list_head(&zilog->zl_itx_list)) != NULL &&
766 	    itx->itx_lr.lrc_txg <= MIN(synced_txg, freeze_txg)) {
767 		list_remove(&zilog->zl_itx_list, itx);
768 		zilog->zl_itx_list_sz -= itx->itx_lr.lrc_reclen;
769 		ASSERT3U(max_seq, <, itx->itx_lr.lrc_seq);
770 		max_seq = itx->itx_lr.lrc_seq;
771 		kmem_free(itx, offsetof(itx_t, itx_lr)
772 		    + itx->itx_lr.lrc_reclen);
773 	}
774 	if (max_seq > zilog->zl_ss_seq) {
775 		zilog->zl_ss_seq = max_seq;
776 		cv_broadcast(&zilog->zl_cv_seq);
777 	}
778 	mutex_exit(&zilog->zl_lock);
779 }
780 
781 void
782 zil_clean(zilog_t *zilog)
783 {
784 	/*
785 	 * Check for any log blocks that can be freed.
786 	 * Log blocks are only freed when the log block allocation and
787 	 * log records contained within are both known to be committed.
788 	 */
789 	mutex_enter(&zilog->zl_lock);
790 	if (list_head(&zilog->zl_itx_list) != NULL)
791 		(void) taskq_dispatch(zilog->zl_clean_taskq,
792 		    (void (*)(void *))zil_itx_clean, zilog, TQ_NOSLEEP);
793 	mutex_exit(&zilog->zl_lock);
794 }
795 
796 /*
797  * Push zfs transactions to stable storage up to the supplied sequence number.
798  */
799 void
800 zil_commit(zilog_t *zilog, uint64_t seq, int ioflag)
801 {
802 	uint64_t txg;
803 	uint64_t max_seq;
804 	uint64_t reclen;
805 	itx_t *itx;
806 	lwb_t *lwb;
807 	spa_t *spa;
808 
809 	if (zilog == NULL || seq == 0 ||
810 	    ((ioflag & (FSYNC | FDSYNC | FRSYNC)) == 0 && !zil_always))
811 		return;
812 
813 	spa = zilog->zl_spa;
814 	mutex_enter(&zilog->zl_lock);
815 
816 	seq = MIN(seq, zilog->zl_itx_seq);	/* cap seq at largest itx seq */
817 
818 	for (;;) {
819 		if (zilog->zl_ss_seq >= seq) {	/* already on stable storage */
820 			mutex_exit(&zilog->zl_lock);
821 			return;
822 		}
823 
824 		if (zilog->zl_writer == B_FALSE) /* no one writing, do it */
825 			break;
826 
827 		cv_wait(&zilog->zl_cv_write, &zilog->zl_lock);
828 	}
829 
830 	zilog->zl_writer = B_TRUE;
831 	max_seq = 0;
832 
833 	if (zilog->zl_suspend) {
834 		lwb = NULL;
835 	} else {
836 		lwb = list_tail(&zilog->zl_lwb_list);
837 		if (lwb == NULL) {
838 			mutex_exit(&zilog->zl_lock);
839 			zil_create(zilog);
840 			mutex_enter(&zilog->zl_lock);
841 			lwb = list_tail(&zilog->zl_lwb_list);
842 		}
843 	}
844 
845 	/*
846 	 * Loop through in-memory log transactions filling log blocks,
847 	 * until we reach the given sequence number and there's no more
848 	 * room in the write buffer.
849 	 */
850 	for (;;) {
851 		itx = list_head(&zilog->zl_itx_list);
852 		if (itx == NULL)
853 			break;
854 
855 		reclen = itx->itx_lr.lrc_reclen;
856 		if ((itx->itx_lr.lrc_seq > seq) &&
857 		    ((lwb == NULL) || (lwb->lwb_nused + reclen >
858 		    ZIL_BLK_DATA_SZ(lwb))))
859 			break;
860 
861 		list_remove(&zilog->zl_itx_list, itx);
862 		txg = itx->itx_lr.lrc_txg;
863 		ASSERT(txg);
864 
865 		mutex_exit(&zilog->zl_lock);
866 		if (txg > spa_last_synced_txg(spa) ||
867 		    txg > spa_freeze_txg(spa))
868 			lwb = zil_lwb_commit(zilog, itx, lwb);
869 		else
870 			max_seq = itx->itx_lr.lrc_seq;
871 		kmem_free(itx, offsetof(itx_t, itx_lr)
872 		    + itx->itx_lr.lrc_reclen);
873 		mutex_enter(&zilog->zl_lock);
874 		zilog->zl_itx_list_sz -= reclen;
875 	}
876 
877 	mutex_exit(&zilog->zl_lock);
878 
879 	/* write the last block out */
880 	if (lwb != NULL && lwb->lwb_nused != 0)
881 		lwb = zil_lwb_write_start(zilog, lwb);
882 
883 	zilog->zl_prev_used = zilog->zl_cur_used;
884 	zilog->zl_cur_used = 0;
885 
886 	mutex_enter(&zilog->zl_lock);
887 	if (max_seq > zilog->zl_ss_seq) {
888 		zilog->zl_ss_seq = max_seq;
889 		cv_broadcast(&zilog->zl_cv_seq);
890 	}
891 	/*
892 	 * Wait if necessary for our seq to be committed.
893 	 */
894 	if (lwb) {
895 		while (zilog->zl_ss_seq < seq && zilog->zl_log_error == 0)
896 			cv_wait(&zilog->zl_cv_seq, &zilog->zl_lock);
897 		zil_flush_vdevs(zilog, seq);
898 	}
899 
900 	if (zilog->zl_log_error || lwb == NULL) {
901 		zilog->zl_log_error = 0;
902 		max_seq = zilog->zl_itx_seq;
903 		mutex_exit(&zilog->zl_lock);
904 		txg_wait_synced(zilog->zl_dmu_pool, 0);
905 		mutex_enter(&zilog->zl_lock);
906 		zilog->zl_ss_seq = MAX(max_seq, zilog->zl_ss_seq);
907 		cv_broadcast(&zilog->zl_cv_seq);
908 	}
909 	/* wake up others waiting to start a write */
910 	zilog->zl_writer = B_FALSE;
911 	mutex_exit(&zilog->zl_lock);
912 	cv_broadcast(&zilog->zl_cv_write);
913 }
914 
915 /*
916  * Called in syncing context to free committed log blocks and update log header.
917  */
918 void
919 zil_sync(zilog_t *zilog, dmu_tx_t *tx)
920 {
921 	uint64_t txg = dmu_tx_get_txg(tx);
922 	spa_t *spa = zilog->zl_spa;
923 	lwb_t *lwb;
924 
925 	ASSERT(zilog->zl_stop_sync == 0);
926 
927 	zilog->zl_header->zh_replay_seq = zilog->zl_replay_seq[txg & TXG_MASK];
928 
929 	if (zilog->zl_destroy_txg == txg) {
930 		bzero(zilog->zl_header, sizeof (zil_header_t));
931 		bzero(zilog->zl_replay_seq, sizeof (zilog->zl_replay_seq));
932 		zilog->zl_destroy_txg = 0;
933 	}
934 
935 	mutex_enter(&zilog->zl_lock);
936 	for (;;) {
937 		lwb = list_head(&zilog->zl_lwb_list);
938 		if (lwb == NULL) {
939 			mutex_exit(&zilog->zl_lock);
940 			return;
941 		}
942 		if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg)
943 			break;
944 		list_remove(&zilog->zl_lwb_list, lwb);
945 		zio_free_blk(spa, &lwb->lwb_blk, txg);
946 		kmem_cache_free(zil_lwb_cache, lwb);
947 	}
948 	zilog->zl_header->zh_log = lwb->lwb_blk;
949 	mutex_exit(&zilog->zl_lock);
950 }
951 
952 void
953 zil_init(void)
954 {
955 	zil_lwb_cache = kmem_cache_create("zil_lwb_cache",
956 	    sizeof (struct lwb), NULL, NULL, NULL, NULL, NULL, NULL, 0);
957 }
958 
959 void
960 zil_fini(void)
961 {
962 	kmem_cache_destroy(zil_lwb_cache);
963 }
964 
965 zilog_t *
966 zil_alloc(objset_t *os, zil_header_t *zh_phys)
967 {
968 	zilog_t *zilog;
969 
970 	zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP);
971 
972 	zilog->zl_header = zh_phys;
973 	zilog->zl_os = os;
974 	zilog->zl_spa = dmu_objset_spa(os);
975 	zilog->zl_dmu_pool = dmu_objset_pool(os);
976 
977 	list_create(&zilog->zl_itx_list, sizeof (itx_t),
978 	    offsetof(itx_t, itx_node));
979 
980 	list_create(&zilog->zl_lwb_list, sizeof (lwb_t),
981 	    offsetof(lwb_t, lwb_node));
982 
983 	list_create(&zilog->zl_vdev_list, sizeof (zil_vdev_t),
984 	    offsetof(zil_vdev_t, vdev_seq_node));
985 
986 	return (zilog);
987 }
988 
989 void
990 zil_free(zilog_t *zilog)
991 {
992 	lwb_t *lwb;
993 	zil_vdev_t *zv;
994 
995 	zilog->zl_stop_sync = 1;
996 
997 	while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
998 		list_remove(&zilog->zl_lwb_list, lwb);
999 		if (lwb->lwb_buf != NULL)
1000 			zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
1001 		kmem_cache_free(zil_lwb_cache, lwb);
1002 	}
1003 	list_destroy(&zilog->zl_lwb_list);
1004 
1005 	while ((zv = list_head(&zilog->zl_vdev_list)) != NULL) {
1006 		list_remove(&zilog->zl_vdev_list, zv);
1007 		kmem_free(zv, sizeof (zil_vdev_t));
1008 	}
1009 	list_destroy(&zilog->zl_vdev_list);
1010 
1011 	ASSERT(list_head(&zilog->zl_itx_list) == NULL);
1012 	list_destroy(&zilog->zl_itx_list);
1013 
1014 	kmem_free(zilog, sizeof (zilog_t));
1015 }
1016 
1017 /*
1018  * return true if the initial log block is not valid
1019  */
1020 static int
1021 zil_empty(zilog_t *zilog)
1022 {
1023 	blkptr_t blk;
1024 	char *lrbuf;
1025 	int error;
1026 
1027 	blk = zilog->zl_header->zh_log;
1028 	if (BP_IS_HOLE(&blk))
1029 		return (1);
1030 
1031 	lrbuf = zio_buf_alloc(SPA_MAXBLOCKSIZE);
1032 	error = zil_read_log_block(zilog, &blk, lrbuf);
1033 	zio_buf_free(lrbuf, SPA_MAXBLOCKSIZE);
1034 	return (error ? 1 : 0);
1035 }
1036 
1037 /*
1038  * Open an intent log.
1039  */
1040 zilog_t *
1041 zil_open(objset_t *os, zil_get_data_t *get_data)
1042 {
1043 	zilog_t *zilog = dmu_objset_zil(os);
1044 
1045 	zilog->zl_get_data = get_data;
1046 	zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri,
1047 	    2, 2, TASKQ_PREPOPULATE);
1048 
1049 	return (zilog);
1050 }
1051 
1052 /*
1053  * Close an intent log.
1054  */
1055 void
1056 zil_close(zilog_t *zilog)
1057 {
1058 	if (!zil_is_committed(zilog))
1059 		txg_wait_synced(zilog->zl_dmu_pool, 0);
1060 	taskq_destroy(zilog->zl_clean_taskq);
1061 	zilog->zl_clean_taskq = NULL;
1062 	zilog->zl_get_data = NULL;
1063 
1064 	zil_itx_clean(zilog);
1065 	ASSERT(list_head(&zilog->zl_itx_list) == NULL);
1066 }
1067 
1068 /*
1069  * Suspend an intent log.  While in suspended mode, we still honor
1070  * synchronous semantics, but we rely on txg_wait_synced() to do it.
1071  * We suspend the log briefly when taking a snapshot so that the snapshot
1072  * contains all the data it's supposed to, and has an empty intent log.
1073  */
1074 int
1075 zil_suspend(zilog_t *zilog)
1076 {
1077 	lwb_t *lwb;
1078 
1079 	mutex_enter(&zilog->zl_lock);
1080 	if (zilog->zl_header->zh_claim_txg != 0) {	/* unplayed log */
1081 		mutex_exit(&zilog->zl_lock);
1082 		return (EBUSY);
1083 	}
1084 	zilog->zl_suspend++;
1085 	mutex_exit(&zilog->zl_lock);
1086 
1087 	zil_commit(zilog, UINT64_MAX, FSYNC);
1088 
1089 	mutex_enter(&zilog->zl_lock);
1090 	while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
1091 		if (lwb->lwb_buf != NULL) {
1092 			/*
1093 			 * Wait for the buffer if it's in the process of
1094 			 * being written.
1095 			 */
1096 			if ((lwb->lwb_seq != 0) &&
1097 			    (lwb->lwb_state != SEQ_COMPLETE)) {
1098 				cv_wait(&zilog->zl_cv_seq, &zilog->zl_lock);
1099 				continue;
1100 			}
1101 			zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
1102 		}
1103 		list_remove(&zilog->zl_lwb_list, lwb);
1104 		kmem_cache_free(zil_lwb_cache, lwb);
1105 	}
1106 	mutex_exit(&zilog->zl_lock);
1107 
1108 	zil_destroy(zilog);
1109 
1110 	return (0);
1111 }
1112 
1113 void
1114 zil_resume(zilog_t *zilog)
1115 {
1116 	mutex_enter(&zilog->zl_lock);
1117 	ASSERT(zilog->zl_suspend != 0);
1118 	zilog->zl_suspend--;
1119 	mutex_exit(&zilog->zl_lock);
1120 }
1121 
1122 typedef struct zil_replay_arg {
1123 	objset_t	*zr_os;
1124 	zil_replay_func_t **zr_replay;
1125 	void		*zr_arg;
1126 	void		(*zr_rm_sync)(void *arg);
1127 	uint64_t	*zr_txgp;
1128 	boolean_t	zr_byteswap;
1129 	char		*zr_lrbuf;
1130 } zil_replay_arg_t;
1131 
1132 static void
1133 zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg)
1134 {
1135 	zil_replay_arg_t *zr = zra;
1136 	zil_header_t *zh = zilog->zl_header;
1137 	uint64_t reclen = lr->lrc_reclen;
1138 	uint64_t txtype = lr->lrc_txtype;
1139 	int pass, error;
1140 
1141 	if (zilog->zl_stop_replay)
1142 		return;
1143 
1144 	if (lr->lrc_txg < claim_txg)		/* already committed */
1145 		return;
1146 
1147 	if (lr->lrc_seq <= zh->zh_replay_seq)	/* already replayed */
1148 		return;
1149 
1150 	/*
1151 	 * Make a copy of the data so we can revise and extend it.
1152 	 */
1153 	bcopy(lr, zr->zr_lrbuf, reclen);
1154 
1155 	/*
1156 	 * The log block containing this lr may have been byteswapped
1157 	 * so that we can easily examine common fields like lrc_txtype.
1158 	 * However, the log is a mix of different data types, and only the
1159 	 * replay vectors know how to byteswap their records.  Therefore, if
1160 	 * the lr was byteswapped, undo it before invoking the replay vector.
1161 	 */
1162 	if (zr->zr_byteswap)
1163 		byteswap_uint64_array(zr->zr_lrbuf, reclen);
1164 
1165 	/*
1166 	 * If this is a TX_WRITE with a blkptr, suck in the data.
1167 	 */
1168 	if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) {
1169 		lr_write_t *lrw = (lr_write_t *)lr;
1170 		blkptr_t *wbp = &lrw->lr_blkptr;
1171 		uint64_t wlen = lrw->lr_length;
1172 		char *wbuf = zr->zr_lrbuf + reclen;
1173 
1174 		if (BP_IS_HOLE(wbp)) {	/* compressed to a hole */
1175 			bzero(wbuf, wlen);
1176 		} else {
1177 			/*
1178 			 * A subsequent write may have overwritten this block,
1179 			 * in which case wbp may have been been freed and
1180 			 * reallocated, and our read of wbp may fail with a
1181 			 * checksum error.  We can safely ignore this because
1182 			 * the later write will provide the correct data.
1183 			 */
1184 			zbookmark_t zb;
1185 
1186 			zb.zb_objset = dmu_objset_id(zilog->zl_os);
1187 			zb.zb_object = lrw->lr_foid;
1188 			zb.zb_level = -1;
1189 			zb.zb_blkid = lrw->lr_offset / BP_GET_LSIZE(wbp);
1190 
1191 			(void) zio_wait(zio_read(NULL, zilog->zl_spa,
1192 			    wbp, wbuf, BP_GET_LSIZE(wbp), NULL, NULL,
1193 			    ZIO_PRIORITY_SYNC_READ,
1194 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, &zb));
1195 			(void) memmove(wbuf, wbuf + lrw->lr_blkoff, wlen);
1196 		}
1197 	}
1198 
1199 	/*
1200 	 * We must now do two things atomically: replay this log record,
1201 	 * and update the log header to reflect the fact that we did so.
1202 	 * We use the DMU's ability to assign into a specific txg to do this.
1203 	 */
1204 	for (pass = 1; /* CONSTANTCONDITION */; pass++) {
1205 		uint64_t replay_txg;
1206 		dmu_tx_t *replay_tx;
1207 
1208 		replay_tx = dmu_tx_create(zr->zr_os);
1209 		error = dmu_tx_assign(replay_tx, TXG_WAIT);
1210 		if (error) {
1211 			dmu_tx_abort(replay_tx);
1212 			break;
1213 		}
1214 
1215 		replay_txg = dmu_tx_get_txg(replay_tx);
1216 
1217 		if (txtype == 0 || txtype >= TX_MAX_TYPE) {
1218 			error = EINVAL;
1219 		} else {
1220 			/*
1221 			 * On the first pass, arrange for the replay vector
1222 			 * to fail its dmu_tx_assign().  That's the only way
1223 			 * to ensure that those code paths remain well tested.
1224 			 */
1225 			*zr->zr_txgp = replay_txg - (pass == 1);
1226 			error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lrbuf,
1227 			    zr->zr_byteswap);
1228 			*zr->zr_txgp = TXG_NOWAIT;
1229 		}
1230 
1231 		if (error == 0) {
1232 			dsl_dataset_dirty(dmu_objset_ds(zr->zr_os), replay_tx);
1233 			zilog->zl_replay_seq[replay_txg & TXG_MASK] =
1234 			    lr->lrc_seq;
1235 		}
1236 
1237 		dmu_tx_commit(replay_tx);
1238 
1239 		if (error != ERESTART)
1240 			break;
1241 
1242 		if (pass != 1)
1243 			txg_wait_open(spa_get_dsl(zilog->zl_spa),
1244 			    replay_txg + 1);
1245 
1246 		dprintf("pass %d, retrying\n", pass);
1247 	}
1248 
1249 	if (error) {
1250 		char *name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1251 		dmu_objset_name(zr->zr_os, name);
1252 		cmn_err(CE_WARN, "ZFS replay transaction error %d, "
1253 		    "dataset %s, seq 0x%llx, txtype %llu\n",
1254 		    error, name,
1255 		    (u_longlong_t)lr->lrc_seq, (u_longlong_t)txtype);
1256 		zilog->zl_stop_replay = 1;
1257 		kmem_free(name, MAXNAMELEN);
1258 	}
1259 
1260 	/*
1261 	 * The DMU's dnode layer doesn't see removes until the txg commits,
1262 	 * so a subsequent claim can spuriously fail with EEXIST.
1263 	 * To prevent this, if we might have removed an object,
1264 	 * wait for the delete thread to delete it, and then
1265 	 * wait for the transaction group to sync.
1266 	 */
1267 	if (txtype == TX_REMOVE || txtype == TX_RMDIR || txtype == TX_RENAME) {
1268 		if (zr->zr_rm_sync != NULL)
1269 			zr->zr_rm_sync(zr->zr_arg);
1270 		txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0);
1271 	}
1272 }
1273 
1274 /*
1275  * If this dataset has a non-empty intent log, replay it and destroy it.
1276  */
1277 void
1278 zil_replay(objset_t *os, void *arg, uint64_t *txgp,
1279 	zil_replay_func_t *replay_func[TX_MAX_TYPE], void (*rm_sync)(void *arg))
1280 {
1281 	zilog_t *zilog = dmu_objset_zil(os);
1282 		zil_replay_arg_t zr;
1283 
1284 	if (zil_empty(zilog)) {
1285 		/*
1286 		 * Initialise the log header but don't free the log block
1287 		 * which will get reused.
1288 		 */
1289 		zilog->zl_header->zh_claim_txg = 0;
1290 		zilog->zl_header->zh_replay_seq = 0;
1291 		return;
1292 	}
1293 
1294 	zr.zr_os = os;
1295 	zr.zr_replay = replay_func;
1296 	zr.zr_arg = arg;
1297 	zr.zr_rm_sync = rm_sync;
1298 	zr.zr_txgp = txgp;
1299 	zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zilog->zl_header->zh_log);
1300 	zr.zr_lrbuf = kmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP);
1301 
1302 	/*
1303 	 * Wait for in-progress removes to sync before starting replay.
1304 	 */
1305 	if (rm_sync != NULL)
1306 		rm_sync(arg);
1307 	txg_wait_synced(zilog->zl_dmu_pool, 0);
1308 
1309 	zilog->zl_stop_replay = 0;
1310 	zil_parse(zilog, NULL, zil_replay_log_record, &zr,
1311 	    zilog->zl_header->zh_claim_txg);
1312 	kmem_free(zr.zr_lrbuf, 2 * SPA_MAXBLOCKSIZE);
1313 
1314 	zil_destroy(zilog);
1315 }
1316 
1317 /*
1318  * Report whether all transactions are committed
1319  */
1320 int
1321 zil_is_committed(zilog_t *zilog)
1322 {
1323 	lwb_t *lwb;
1324 
1325 	if (zilog == NULL || list_head(&zilog->zl_itx_list))
1326 		return (B_FALSE);
1327 
1328 	/*
1329 	 * A log write buffer at the head of the list that is not UNWRITTEN
1330 	 * means there's a lwb yet to be freed after a txg commit
1331 	 */
1332 	lwb = list_head(&zilog->zl_lwb_list);
1333 	if (lwb && lwb->lwb_state != UNWRITTEN)
1334 		return (B_FALSE);
1335 	ASSERT(zil_empty(zilog));
1336 	return (B_TRUE);
1337 }
1338