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