Home | History | Annotate | Download | only in zfs
      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 2007 Sun Microsystems, Inc.  All rights reserved.
     23  * Use is subject to license terms.
     24  */
     25 
     26 #pragma ident	"@(#)zil.c	1.28	07/12/18 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 #include <sys/dmu_tx.h>
     40 
     41 /*
     42  * The zfs intent log (ZIL) saves transaction records of system calls
     43  * that change the file system in memory with enough information
     44  * to be able to replay them. These are stored in memory until
     45  * either the DMU transaction group (txg) commits them to the stable pool
     46  * and they can be discarded, or they are flushed to the stable log
     47  * (also in the pool) due to a fsync, O_DSYNC or other synchronous
     48  * requirement. In the event of a panic or power fail then those log
     49  * records (transactions) are replayed.
     50  *
     51  * There is one ZIL per file system. Its on-disk (pool) format consists
     52  * of 3 parts:
     53  *
     54  * 	- ZIL header
     55  * 	- ZIL blocks
     56  * 	- ZIL records
     57  *
     58  * A log record holds a system call transaction. Log blocks can
     59  * hold many log records and the blocks are chained together.
     60  * Each ZIL block contains a block pointer (blkptr_t) to the next
     61  * ZIL block in the chain. The ZIL header points to the first
     62  * block in the chain. Note there is not a fixed place in the pool
     63  * to hold blocks. They are dynamically allocated and freed as
     64  * needed from the blocks available. Figure X shows the ZIL structure:
     65  */
     66 
     67 /*
     68  * This global ZIL switch affects all pools
     69  */
     70 int zil_disable = 0;	/* disable intent logging */
     71 
     72 /*
     73  * Tunable parameter for debugging or performance analysis.  Setting
     74  * zfs_nocacheflush will cause corruption on power loss if a volatile
     75  * out-of-order write cache is enabled.
     76  */
     77 boolean_t zfs_nocacheflush = B_FALSE;
     78 
     79 static kmem_cache_t *zil_lwb_cache;
     80 
     81 static int
     82 zil_dva_compare(const void *x1, const void *x2)
     83 {
     84 	const dva_t *dva1 = x1;
     85 	const dva_t *dva2 = x2;
     86 
     87 	if (DVA_GET_VDEV(dva1) < DVA_GET_VDEV(dva2))
     88 		return (-1);
     89 	if (DVA_GET_VDEV(dva1) > DVA_GET_VDEV(dva2))
     90 		return (1);
     91 
     92 	if (DVA_GET_OFFSET(dva1) < DVA_GET_OFFSET(dva2))
     93 		return (-1);
     94 	if (DVA_GET_OFFSET(dva1) > DVA_GET_OFFSET(dva2))
     95 		return (1);
     96 
     97 	return (0);
     98 }
     99 
    100 static void
    101 zil_dva_tree_init(avl_tree_t *t)
    102 {
    103 	avl_create(t, zil_dva_compare, sizeof (zil_dva_node_t),
    104 	    offsetof(zil_dva_node_t, zn_node));
    105 }
    106 
    107 static void
    108 zil_dva_tree_fini(avl_tree_t *t)
    109 {
    110 	zil_dva_node_t *zn;
    111 	void *cookie = NULL;
    112 
    113 	while ((zn = avl_destroy_nodes(t, &cookie)) != NULL)
    114 		kmem_free(zn, sizeof (zil_dva_node_t));
    115 
    116 	avl_destroy(t);
    117 }
    118 
    119 static int
    120 zil_dva_tree_add(avl_tree_t *t, dva_t *dva)
    121 {
    122 	zil_dva_node_t *zn;
    123 	avl_index_t where;
    124 
    125 	if (avl_find(t, dva, &where) != NULL)
    126 		return (EEXIST);
    127 
    128 	zn = kmem_alloc(sizeof (zil_dva_node_t), KM_SLEEP);
    129 	zn->zn_dva = *dva;
    130 	avl_insert(t, zn, where);
    131 
    132 	return (0);
    133 }
    134 
    135 static zil_header_t *
    136 zil_header_in_syncing_context(zilog_t *zilog)
    137 {
    138 	return ((zil_header_t *)zilog->zl_header);
    139 }
    140 
    141 static void
    142 zil_init_log_chain(zilog_t *zilog, blkptr_t *bp)
    143 {
    144 	zio_cksum_t *zc = &bp->blk_cksum;
    145 
    146 	zc->zc_word[ZIL_ZC_GUID_0] = spa_get_random(-1ULL);
    147 	zc->zc_word[ZIL_ZC_GUID_1] = spa_get_random(-1ULL);
    148 	zc->zc_word[ZIL_ZC_OBJSET] = dmu_objset_id(zilog->zl_os);
    149 	zc->zc_word[ZIL_ZC_SEQ] = 1ULL;
    150 }
    151 
    152 /*
    153  * Read a log block, make sure it's valid, and byteswap it if necessary.
    154  */
    155 static int
    156 zil_read_log_block(zilog_t *zilog, const blkptr_t *bp, arc_buf_t **abufpp)
    157 {
    158 	blkptr_t blk = *bp;
    159 	zbookmark_t zb;
    160 	uint32_t aflags = ARC_WAIT;
    161 	int error;
    162 
    163 	zb.zb_objset = bp->blk_cksum.zc_word[ZIL_ZC_OBJSET];
    164 	zb.zb_object = 0;
    165 	zb.zb_level = -1;
    166 	zb.zb_blkid = bp->blk_cksum.zc_word[ZIL_ZC_SEQ];
    167 
    168 	*abufpp = NULL;
    169 
    170 	error = arc_read(NULL, zilog->zl_spa, &blk, byteswap_uint64_array,
    171 	    arc_getbuf_func, abufpp, ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL |
    172 	    ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB, &aflags, &zb);
    173 
    174 	if (error == 0) {
    175 		char *data = (*abufpp)->b_data;
    176 		uint64_t blksz = BP_GET_LSIZE(bp);
    177 		zil_trailer_t *ztp = (zil_trailer_t *)(data + blksz) - 1;
    178 		zio_cksum_t cksum = bp->blk_cksum;
    179 
    180 		/*
    181 		 * Sequence numbers should be... sequential.  The checksum
    182 		 * verifier for the next block should be bp's checksum plus 1.
    183 		 */
    184 		cksum.zc_word[ZIL_ZC_SEQ]++;
    185 
    186 		if (bcmp(&cksum, &ztp->zit_next_blk.blk_cksum, sizeof (cksum)))
    187 			error = ESTALE;
    188 		else if (BP_IS_HOLE(&ztp->zit_next_blk))
    189 			error = ENOENT;
    190 		else if (ztp->zit_nused > (blksz - sizeof (zil_trailer_t)))
    191 			error = EOVERFLOW;
    192 
    193 		if (error) {
    194 			VERIFY(arc_buf_remove_ref(*abufpp, abufpp) == 1);
    195 			*abufpp = NULL;
    196 		}
    197 	}
    198 
    199 	dprintf("error %d on %llu:%llu\n", error, zb.zb_objset, zb.zb_blkid);
    200 
    201 	return (error);
    202 }
    203 
    204 /*
    205  * Parse the intent log, and call parse_func for each valid record within.
    206  * Return the highest sequence number.
    207  */
    208 uint64_t
    209 zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func,
    210     zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg)
    211 {
    212 	const zil_header_t *zh = zilog->zl_header;
    213 	uint64_t claim_seq = zh->zh_claim_seq;
    214 	uint64_t seq = 0;
    215 	uint64_t max_seq = 0;
    216 	blkptr_t blk = zh->zh_log;
    217 	arc_buf_t *abuf;
    218 	char *lrbuf, *lrp;
    219 	zil_trailer_t *ztp;
    220 	int reclen, error;
    221 
    222 	if (BP_IS_HOLE(&blk))
    223 		return (max_seq);
    224 
    225 	/*
    226 	 * Starting at the block pointed to by zh_log we read the log chain.
    227 	 * For each block in the chain we strongly check that block to
    228 	 * ensure its validity.  We stop when an invalid block is found.
    229 	 * For each block pointer in the chain we call parse_blk_func().
    230 	 * For each record in each valid block we call parse_lr_func().
    231 	 * If the log has been claimed, stop if we encounter a sequence
    232 	 * number greater than the highest claimed sequence number.
    233 	 */
    234 	zil_dva_tree_init(&zilog->zl_dva_tree);
    235 	for (;;) {
    236 		seq = blk.blk_cksum.zc_word[ZIL_ZC_SEQ];
    237 
    238 		if (claim_seq != 0 && seq > claim_seq)
    239 			break;
    240 
    241 		ASSERT(max_seq < seq);
    242 		max_seq = seq;
    243 
    244 		error = zil_read_log_block(zilog, &blk, &abuf);
    245 
    246 		if (parse_blk_func != NULL)
    247 			parse_blk_func(zilog, &blk, arg, txg);
    248 
    249 		if (error)
    250 			break;
    251 
    252 		lrbuf = abuf->b_data;
    253 		ztp = (zil_trailer_t *)(lrbuf + BP_GET_LSIZE(&blk)) - 1;
    254 		blk = ztp->zit_next_blk;
    255 
    256 		if (parse_lr_func == NULL) {
    257 			VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
    258 			continue;
    259 		}
    260 
    261 		for (lrp = lrbuf; lrp < lrbuf + ztp->zit_nused; lrp += reclen) {
    262 			lr_t *lr = (lr_t *)lrp;
    263 			reclen = lr->lrc_reclen;
    264 			ASSERT3U(reclen, >=, sizeof (lr_t));
    265 			parse_lr_func(zilog, lr, arg, txg);
    266 		}
    267 		VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
    268 	}
    269 	zil_dva_tree_fini(&zilog->zl_dva_tree);
    270 
    271 	return (max_seq);
    272 }
    273 
    274 /* ARGSUSED */
    275 static void
    276 zil_claim_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t first_txg)
    277 {
    278 	spa_t *spa = zilog->zl_spa;
    279 	int err;
    280 
    281 	/*
    282 	 * Claim log block if not already committed and not already claimed.
    283 	 */
    284 	if (bp->blk_birth >= first_txg &&
    285 	    zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp)) == 0) {
    286 		err = zio_wait(zio_claim(NULL, spa, first_txg, bp, NULL, NULL));
    287 		ASSERT(err == 0);
    288 	}
    289 }
    290 
    291 static void
    292 zil_claim_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg)
    293 {
    294 	if (lrc->lrc_txtype == TX_WRITE) {
    295 		lr_write_t *lr = (lr_write_t *)lrc;
    296 		zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg);
    297 	}
    298 }
    299 
    300 /* ARGSUSED */
    301 static void
    302 zil_free_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t claim_txg)
    303 {
    304 	zio_free_blk(zilog->zl_spa, bp, dmu_tx_get_txg(tx));
    305 }
    306 
    307 static void
    308 zil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg)
    309 {
    310 	/*
    311 	 * If we previously claimed it, we need to free it.
    312 	 */
    313 	if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE) {
    314 		lr_write_t *lr = (lr_write_t *)lrc;
    315 		blkptr_t *bp = &lr->lr_blkptr;
    316 		if (bp->blk_birth >= claim_txg &&
    317 		    !zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp))) {
    318 			(void) arc_free(NULL, zilog->zl_spa,
    319 			    dmu_tx_get_txg(tx), bp, NULL, NULL, ARC_WAIT);
    320 		}
    321 	}
    322 }
    323 
    324 /*
    325  * Create an on-disk intent log.
    326  */
    327 static void
    328 zil_create(zilog_t *zilog)
    329 {
    330 	const zil_header_t *zh = zilog->zl_header;
    331 	lwb_t *lwb;
    332 	uint64_t txg = 0;
    333 	dmu_tx_t *tx = NULL;
    334 	blkptr_t blk;
    335 	int error = 0;
    336 
    337 	/*
    338 	 * Wait for any previous destroy to complete.
    339 	 */
    340 	txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
    341 
    342 	ASSERT(zh->zh_claim_txg == 0);
    343 	ASSERT(zh->zh_replay_seq == 0);
    344 
    345 	blk = zh->zh_log;
    346 
    347 	/*
    348 	 * If we don't already have an initial log block, allocate one now.
    349 	 */
    350 	if (BP_IS_HOLE(&blk)) {
    351 		tx = dmu_tx_create(zilog->zl_os);
    352 		(void) dmu_tx_assign(tx, TXG_WAIT);
    353 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
    354 		txg = dmu_tx_get_txg(tx);
    355 
    356 		error = zio_alloc_blk(zilog->zl_spa, ZIL_MIN_BLKSZ, &blk,
    357 		    NULL, txg);
    358 
    359 		if (error == 0)
    360 			zil_init_log_chain(zilog, &blk);
    361 	}
    362 
    363 	/*
    364 	 * Allocate a log write buffer (lwb) for the first log block.
    365 	 */
    366 	if (error == 0) {
    367 		lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP);
    368 		lwb->lwb_zilog = zilog;
    369 		lwb->lwb_blk = blk;
    370 		lwb->lwb_nused = 0;
    371 		lwb->lwb_sz = BP_GET_LSIZE(&lwb->lwb_blk);
    372 		lwb->lwb_buf = zio_buf_alloc(lwb->lwb_sz);
    373 		lwb->lwb_max_txg = txg;
    374 		lwb->lwb_zio = NULL;
    375 
    376 		mutex_enter(&zilog->zl_lock);
    377 		list_insert_tail(&zilog->zl_lwb_list, lwb);
    378 		mutex_exit(&zilog->zl_lock);
    379 	}
    380 
    381 	/*
    382 	 * If we just allocated the first log block, commit our transaction
    383 	 * and wait for zil_sync() to stuff the block poiner into zh_log.
    384 	 * (zh is part of the MOS, so we cannot modify it in open context.)
    385 	 */
    386 	if (tx != NULL) {
    387 		dmu_tx_commit(tx);
    388 		txg_wait_synced(zilog->zl_dmu_pool, txg);
    389 	}
    390 
    391 	ASSERT(bcmp(&blk, &zh->zh_log, sizeof (blk)) == 0);
    392 }
    393 
    394 /*
    395  * In one tx, free all log blocks and clear the log header.
    396  * If keep_first is set, then we're replaying a log with no content.
    397  * We want to keep the first block, however, so that the first
    398  * synchronous transaction doesn't require a txg_wait_synced()
    399  * in zil_create().  We don't need to txg_wait_synced() here either
    400  * when keep_first is set, because both zil_create() and zil_destroy()
    401  * will wait for any in-progress destroys to complete.
    402  */
    403 void
    404 zil_destroy(zilog_t *zilog, boolean_t keep_first)
    405 {
    406 	const zil_header_t *zh = zilog->zl_header;
    407 	lwb_t *lwb;
    408 	dmu_tx_t *tx;
    409 	uint64_t txg;
    410 
    411 	/*
    412 	 * Wait for any previous destroy to complete.
    413 	 */
    414 	txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
    415 
    416 	if (BP_IS_HOLE(&zh->zh_log))
    417 		return;
    418 
    419 	tx = dmu_tx_create(zilog->zl_os);
    420 	(void) dmu_tx_assign(tx, TXG_WAIT);
    421 	dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
    422 	txg = dmu_tx_get_txg(tx);
    423 
    424 	mutex_enter(&zilog->zl_lock);
    425 
    426 	/*
    427 	 * It is possible for the ZIL to get the previously mounted zilog
    428 	 * structure of the same dataset if quickly remounted and the dbuf
    429 	 * eviction has not completed. In this case we can see a non
    430 	 * empty lwb list and keep_first will be set. We fix this by
    431 	 * clearing the keep_first. This will be slower but it's very rare.
    432 	 */
    433 	if (!list_is_empty(&zilog->zl_lwb_list) && keep_first)
    434 		keep_first = B_FALSE;
    435 
    436 	ASSERT3U(zilog->zl_destroy_txg, <, txg);
    437 	zilog->zl_destroy_txg = txg;
    438 	zilog->zl_keep_first = keep_first;
    439 
    440 	if (!list_is_empty(&zilog->zl_lwb_list)) {
    441 		ASSERT(zh->zh_claim_txg == 0);
    442 		ASSERT(!keep_first);
    443 		while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
    444 			list_remove(&zilog->zl_lwb_list, lwb);
    445 			if (lwb->lwb_buf != NULL)
    446 				zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
    447 			zio_free_blk(zilog->zl_spa, &lwb->lwb_blk, txg);
    448 			kmem_cache_free(zil_lwb_cache, lwb);
    449 		}
    450 	} else {
    451 		if (!keep_first) {
    452 			(void) zil_parse(zilog, zil_free_log_block,
    453 			    zil_free_log_record, tx, zh->zh_claim_txg);
    454 		}
    455 	}
    456 	mutex_exit(&zilog->zl_lock);
    457 
    458 	dmu_tx_commit(tx);
    459 }
    460 
    461 /*
    462  * zil_rollback_destroy() is only called by the rollback code.
    463  * We already have a syncing tx. Rollback has exclusive access to the
    464  * dataset, so we don't have to worry about concurrent zil access.
    465  * The actual freeing of any log blocks occurs in zil_sync() later in
    466  * this txg syncing phase.
    467  */
    468 void
    469 zil_rollback_destroy(zilog_t *zilog, dmu_tx_t *tx)
    470 {
    471 	const zil_header_t *zh = zilog->zl_header;
    472 	uint64_t txg;
    473 
    474 	if (BP_IS_HOLE(&zh->zh_log))
    475 		return;
    476 
    477 	txg = dmu_tx_get_txg(tx);
    478 	ASSERT3U(zilog->zl_destroy_txg, <, txg);
    479 	zilog->zl_destroy_txg = txg;
    480 	zilog->zl_keep_first = B_FALSE;
    481 
    482 	ASSERT(list_is_empty(&zilog->zl_lwb_list));
    483 	(void) zil_parse(zilog, zil_free_log_block, zil_free_log_record,
    484 	    tx, zh->zh_claim_txg);
    485 }
    486 
    487 int
    488 zil_claim(char *osname, void *txarg)
    489 {
    490 	dmu_tx_t *tx = txarg;
    491 	uint64_t first_txg = dmu_tx_get_txg(tx);
    492 	zilog_t *zilog;
    493 	zil_header_t *zh;
    494 	objset_t *os;
    495 	int error;
    496 
    497 	error = dmu_objset_open(osname, DMU_OST_ANY, DS_MODE_STANDARD, &os);
    498 	if (error) {
    499 		cmn_err(CE_WARN, "can't process intent log for %s", osname);
    500 		return (0);
    501 	}
    502 
    503 	zilog = dmu_objset_zil(os);
    504 	zh = zil_header_in_syncing_context(zilog);
    505 
    506 	/*
    507 	 * Claim all log blocks if we haven't already done so, and remember
    508 	 * the highest claimed sequence number.  This ensures that if we can
    509 	 * read only part of the log now (e.g. due to a missing device),
    510 	 * but we can read the entire log later, we will not try to replay
    511 	 * or destroy beyond the last block we successfully claimed.
    512 	 */
    513 	ASSERT3U(zh->zh_claim_txg, <=, first_txg);
    514 	if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) {
    515 		zh->zh_claim_txg = first_txg;
    516 		zh->zh_claim_seq = zil_parse(zilog, zil_claim_log_block,
    517 		    zil_claim_log_record, tx, first_txg);
    518 		dsl_dataset_dirty(dmu_objset_ds(os), tx);
    519 	}
    520 
    521 	ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1));
    522 	dmu_objset_close(os);
    523 	return (0);
    524 }
    525 
    526 static int
    527 zil_vdev_compare(const void *x1, const void *x2)
    528 {
    529 	const uint64_t *v1 = x1;
    530 	const uint64_t *v2 = x2;
    531 
    532 	if (v1 < v2)
    533 		return (-1);
    534 	if (v1 > v2)
    535 		return (1);
    536 
    537 	return (0);
    538 }
    539 
    540 void
    541 zil_add_block(zilog_t *zilog, blkptr_t *bp)
    542 {
    543 	avl_tree_t *t = &zilog->zl_vdev_tree;
    544 	avl_index_t where;
    545 	zil_vdev_node_t *zv, zvsearch;
    546 	int ndvas = BP_GET_NDVAS(bp);
    547 	int i;
    548 
    549 	if (zfs_nocacheflush)
    550 		return;
    551 
    552 	ASSERT(zilog->zl_writer);
    553 
    554 	/*
    555 	 * Even though we're zl_writer, we still need a lock because the
    556 	 * zl_get_data() callbacks may have dmu_sync() done callbacks
    557 	 * that will run concurrently.
    558 	 */
    559 	mutex_enter(&zilog->zl_vdev_lock);
    560 	for (i = 0; i < ndvas; i++) {
    561 		zvsearch.zv_vdev = DVA_GET_VDEV(&bp->blk_dva[i]);
    562 		if (avl_find(t, &zvsearch, &where) == NULL) {
    563 			zv = kmem_alloc(sizeof (*zv), KM_SLEEP);
    564 			zv->zv_vdev = zvsearch.zv_vdev;
    565 			avl_insert(t, zv, where);
    566 		}
    567 	}
    568 	mutex_exit(&zilog->zl_vdev_lock);
    569 }
    570 
    571 void
    572 zil_flush_vdevs(zilog_t *zilog)
    573 {
    574 	spa_t *spa = zilog->zl_spa;
    575 	avl_tree_t *t = &zilog->zl_vdev_tree;
    576 	void *cookie = NULL;
    577 	zil_vdev_node_t *zv;
    578 	zio_t *zio;
    579 
    580 	ASSERT(zilog->zl_writer);
    581 
    582 	/*
    583 	 * We don't need zl_vdev_lock here because we're the zl_writer,
    584 	 * and all zl_get_data() callbacks are done.
    585 	 */
    586 	if (avl_numnodes(t) == 0)
    587 		return;
    588 
    589 	spa_config_enter(spa, RW_READER, FTAG);
    590 
    591 	zio = zio_root(spa, NULL, NULL,
    592 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
    593 
    594 	while ((zv = avl_destroy_nodes(t, &cookie)) != NULL) {
    595 		vdev_t *vd = vdev_lookup_top(spa, zv->zv_vdev);
    596 		if (vd != NULL)
    597 			zio_flush(zio, vd);
    598 		kmem_free(zv, sizeof (*zv));
    599 	}
    600 
    601 	/*
    602 	 * Wait for all the flushes to complete.  Not all devices actually
    603 	 * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails.
    604 	 */
    605 	(void) zio_wait(zio);
    606 
    607 	spa_config_exit(spa, FTAG);
    608 }
    609 
    610 /*
    611  * Function called when a log block write completes
    612  */
    613 static void
    614 zil_lwb_write_done(zio_t *zio)
    615 {
    616 	lwb_t *lwb = zio->io_private;
    617 	zilog_t *zilog = lwb->lwb_zilog;
    618 
    619 	/*
    620 	 * Now that we've written this log block, we have a stable pointer
    621 	 * to the next block in the chain, so it's OK to let the txg in
    622 	 * which we allocated the next block sync.
    623 	 */
    624 	txg_rele_to_sync(&lwb->lwb_txgh);
    625 
    626 	zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
    627 	mutex_enter(&zilog->zl_lock);
    628 	lwb->lwb_buf = NULL;
    629 	if (zio->io_error)
    630 		zilog->zl_log_error = B_TRUE;
    631 	mutex_exit(&zilog->zl_lock);
    632 }
    633 
    634 /*
    635  * Initialize the io for a log block.
    636  *
    637  * Note, we should not initialize the IO until we are about
    638  * to use it, since zio_rewrite() does a spa_config_enter().
    639  */
    640 static void
    641 zil_lwb_write_init(zilog_t *zilog, lwb_t *lwb)
    642 {
    643 	zbookmark_t zb;
    644 
    645 	zb.zb_objset = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET];
    646 	zb.zb_object = 0;
    647 	zb.zb_level = -1;
    648 	zb.zb_blkid = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ];
    649 
    650 	if (zilog->zl_root_zio == NULL) {
    651 		zilog->zl_root_zio = zio_root(zilog->zl_spa, NULL, NULL,
    652 		    ZIO_FLAG_CANFAIL);
    653 	}
    654 	if (lwb->lwb_zio == NULL) {
    655 		lwb->lwb_zio = zio_rewrite(zilog->zl_root_zio, zilog->zl_spa,
    656 		    ZIO_CHECKSUM_ZILOG, 0, &lwb->lwb_blk, lwb->lwb_buf,
    657 		    lwb->lwb_sz, zil_lwb_write_done, lwb,
    658 		    ZIO_PRIORITY_LOG_WRITE, ZIO_FLAG_CANFAIL, &zb);
    659 	}
    660 }
    661 
    662 /*
    663  * Start a log block write and advance to the next log block.
    664  * Calls are serialized.
    665  */
    666 static lwb_t *
    667 zil_lwb_write_start(zilog_t *zilog, lwb_t *lwb)
    668 {
    669 	lwb_t *nlwb;
    670 	zil_trailer_t *ztp = (zil_trailer_t *)(lwb->lwb_buf + lwb->lwb_sz) - 1;
    671 	spa_t *spa = zilog->zl_spa;
    672 	blkptr_t *bp = &ztp->zit_next_blk;
    673 	uint64_t txg;
    674 	uint64_t zil_blksz;
    675 	int error;
    676 
    677 	ASSERT(lwb->lwb_nused <= ZIL_BLK_DATA_SZ(lwb));
    678 
    679 	/*
    680 	 * Allocate the next block and save its address in this block
    681 	 * before writing it in order to establish the log chain.
    682 	 * Note that if the allocation of nlwb synced before we wrote
    683 	 * the block that points at it (lwb), we'd leak it if we crashed.
    684 	 * Therefore, we don't do txg_rele_to_sync() until zil_lwb_write_done().
    685 	 */
    686 	txg = txg_hold_open(zilog->zl_dmu_pool, &lwb->lwb_txgh);
    687 	txg_rele_to_quiesce(&lwb->lwb_txgh);
    688 
    689 	/*
    690 	 * Pick a ZIL blocksize. We request a size that is the
    691 	 * maximum of the previous used size, the current used size and
    692 	 * the amount waiting in the queue.
    693 	 */
    694 	zil_blksz = MAX(zilog->zl_prev_used,
    695 	    zilog->zl_cur_used + sizeof (*ztp));
    696 	zil_blksz = MAX(zil_blksz, zilog->zl_itx_list_sz + sizeof (*ztp));
    697 	zil_blksz = P2ROUNDUP_TYPED(zil_blksz, ZIL_MIN_BLKSZ, uint64_t);
    698 	if (zil_blksz > ZIL_MAX_BLKSZ)
    699 		zil_blksz = ZIL_MAX_BLKSZ;
    700 
    701 	BP_ZERO(bp);
    702 	/* pass the old blkptr in order to spread log blocks across devs */
    703 	error = zio_alloc_blk(spa, zil_blksz, bp, &lwb->lwb_blk, txg);
    704 	if (error) {
    705 		dmu_tx_t *tx = dmu_tx_create_assigned(zilog->zl_dmu_pool, txg);
    706 
    707 		/*
    708 		 * We dirty the dataset to ensure that zil_sync() will
    709 		 * be called to remove this lwb from our zl_lwb_list.
    710 		 * Failing to do so, may leave an lwb with a NULL lwb_buf
    711 		 * hanging around on the zl_lwb_list.
    712 		 */
    713 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
    714 		dmu_tx_commit(tx);
    715 
    716 		/*
    717 		 * Since we've just experienced an allocation failure so we
    718 		 * terminate the current lwb and send it on its way.
    719 		 */
    720 		ztp->zit_pad = 0;
    721 		ztp->zit_nused = lwb->lwb_nused;
    722 		ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum;
    723 		zio_nowait(lwb->lwb_zio);
    724 
    725 		/*
    726 		 * By returning NULL the caller will call tx_wait_synced()
    727 		 */
    728 		return (NULL);
    729 	}
    730 
    731 	ASSERT3U(bp->blk_birth, ==, txg);
    732 	ztp->zit_pad = 0;
    733 	ztp->zit_nused = lwb->lwb_nused;
    734 	ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum;
    735 	bp->blk_cksum = lwb->lwb_blk.blk_cksum;
    736 	bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++;
    737 
    738 	/*
    739 	 * Allocate a new log write buffer (lwb).
    740 	 */
    741 	nlwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP);
    742 
    743 	nlwb->lwb_zilog = zilog;
    744 	nlwb->lwb_blk = *bp;
    745 	nlwb->lwb_nused = 0;
    746 	nlwb->lwb_sz = BP_GET_LSIZE(&nlwb->lwb_blk);
    747 	nlwb->lwb_buf = zio_buf_alloc(nlwb->lwb_sz);
    748 	nlwb->lwb_max_txg = txg;
    749 	nlwb->lwb_zio = NULL;
    750 
    751 	/*
    752 	 * Put new lwb at the end of the log chain
    753 	 */
    754 	mutex_enter(&zilog->zl_lock);
    755 	list_insert_tail(&zilog->zl_lwb_list, nlwb);
    756 	mutex_exit(&zilog->zl_lock);
    757 
    758 	/* Record the block for later vdev flushing */
    759 	zil_add_block(zilog, &lwb->lwb_blk);
    760 
    761 	/*
    762 	 * kick off the write for the old log block
    763 	 */
    764 	dprintf_bp(&lwb->lwb_blk, "lwb %p txg %llu: ", lwb, txg);
    765 	ASSERT(lwb->lwb_zio);
    766 	zio_nowait(lwb->lwb_zio);
    767 
    768 	return (nlwb);
    769 }
    770 
    771 static lwb_t *
    772 zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb)
    773 {
    774 	lr_t *lrc = &itx->itx_lr; /* common log record */
    775 	lr_write_t *lr = (lr_write_t *)lrc;
    776 	uint64_t txg = lrc->lrc_txg;
    777 	uint64_t reclen = lrc->lrc_reclen;
    778 	uint64_t dlen;
    779 
    780 	if (lwb == NULL)
    781 		return (NULL);
    782 	ASSERT(lwb->lwb_buf != NULL);
    783 
    784 	if (lrc->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY)
    785 		dlen = P2ROUNDUP_TYPED(
    786 		    lr->lr_length, sizeof (uint64_t), uint64_t);
    787 	else
    788 		dlen = 0;
    789 
    790 	zilog->zl_cur_used += (reclen + dlen);
    791 
    792 	zil_lwb_write_init(zilog, lwb);
    793 
    794 	/*
    795 	 * If this record won't fit in the current log block, start a new one.
    796 	 */
    797 	if (lwb->lwb_nused + reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) {
    798 		lwb = zil_lwb_write_start(zilog, lwb);
    799 		if (lwb == NULL)
    800 			return (NULL);
    801 		zil_lwb_write_init(zilog, lwb);
    802 		ASSERT(lwb->lwb_nused == 0);
    803 		if (reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) {
    804 			txg_wait_synced(zilog->zl_dmu_pool, txg);
    805 			return (lwb);
    806 		}
    807 	}
    808 
    809 	/*
    810 	 * Update the lrc_seq, to be log record sequence number. See zil.h
    811 	 * Then copy the record to the log buffer.
    812 	 */
    813 	lrc->lrc_seq = ++zilog->zl_lr_seq; /* we are single threaded */
    814 	bcopy(lrc, lwb->lwb_buf + lwb->lwb_nused, reclen);
    815 
    816 	/*
    817 	 * If it's a write, fetch the data or get its blkptr as appropriate.
    818 	 */
    819 	if (lrc->lrc_txtype == TX_WRITE) {
    820 		if (txg > spa_freeze_txg(zilog->zl_spa))
    821 			txg_wait_synced(zilog->zl_dmu_pool, txg);
    822 		if (itx->itx_wr_state != WR_COPIED) {
    823 			char *dbuf;
    824 			int error;
    825 
    826 			/* alignment is guaranteed */
    827 			lr = (lr_write_t *)(lwb->lwb_buf + lwb->lwb_nused);
    828 			if (dlen) {
    829 				ASSERT(itx->itx_wr_state == WR_NEED_COPY);
    830 				dbuf = lwb->lwb_buf + lwb->lwb_nused + reclen;
    831 				lr->lr_common.lrc_reclen += dlen;
    832 			} else {
    833 				ASSERT(itx->itx_wr_state == WR_INDIRECT);
    834 				dbuf = NULL;
    835 			}
    836 			error = zilog->zl_get_data(
    837 			    itx->itx_private, lr, dbuf, lwb->lwb_zio);
    838 			if (error) {
    839 				ASSERT(error == ENOENT || error == EEXIST ||
    840 				    error == EALREADY);
    841 				return (lwb);
    842 			}
    843 		}
    844 	}
    845 
    846 	lwb->lwb_nused += reclen + dlen;
    847 	lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg);
    848 	ASSERT3U(lwb->lwb_nused, <=, ZIL_BLK_DATA_SZ(lwb));
    849 	ASSERT3U(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)), ==, 0);
    850 
    851 	return (lwb);
    852 }
    853 
    854 itx_t *
    855 zil_itx_create(uint64_t txtype, size_t lrsize)
    856 {
    857 	itx_t *itx;
    858 
    859 	lrsize = P2ROUNDUP_TYPED(lrsize, sizeof (uint64_t), size_t);
    860 
    861 	itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, KM_SLEEP);
    862 	itx->itx_lr.lrc_txtype = txtype;
    863 	itx->itx_lr.lrc_reclen = lrsize;
    864 	itx->itx_lr.lrc_seq = 0;	/* defensive */
    865 
    866 	return (itx);
    867 }
    868 
    869 uint64_t
    870 zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx)
    871 {
    872 	uint64_t seq;
    873 
    874 	ASSERT(itx->itx_lr.lrc_seq == 0);
    875 
    876 	mutex_enter(&zilog->zl_lock);
    877 	list_insert_tail(&zilog->zl_itx_list, itx);
    878 	zilog->zl_itx_list_sz += itx->itx_lr.lrc_reclen;
    879 	itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx);
    880 	itx->itx_lr.lrc_seq = seq = ++zilog->zl_itx_seq;
    881 	mutex_exit(&zilog->zl_lock);
    882 
    883 	return (seq);
    884 }
    885 
    886 /*
    887  * Free up all in-memory intent log transactions that have now been synced.
    888  */
    889 static void
    890 zil_itx_clean(zilog_t *zilog)
    891 {
    892 	uint64_t synced_txg = spa_last_synced_txg(zilog->zl_spa);
    893 	uint64_t freeze_txg = spa_freeze_txg(zilog->zl_spa);
    894 	list_t clean_list;
    895 	itx_t *itx;
    896 
    897 	list_create(&clean_list, sizeof (itx_t), offsetof(itx_t, itx_node));
    898 
    899 	mutex_enter(&zilog->zl_lock);
    900 	/* wait for a log writer to finish walking list */
    901 	while (zilog->zl_writer) {
    902 		cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
    903 	}
    904 
    905 	/*
    906 	 * Move the sync'd log transactions to a separate list so we can call
    907 	 * kmem_free without holding the zl_lock.
    908 	 *
    909 	 * There is no need to set zl_writer as we don't drop zl_lock here
    910 	 */
    911 	while ((itx = list_head(&zilog->zl_itx_list)) != NULL &&
    912 	    itx->itx_lr.lrc_txg <= MIN(synced_txg, freeze_txg)) {
    913 		list_remove(&zilog->zl_itx_list, itx);
    914 		zilog->zl_itx_list_sz -= itx->itx_lr.lrc_reclen;
    915 		list_insert_tail(&clean_list, itx);
    916 	}
    917 	cv_broadcast(&zilog->zl_cv_writer);
    918 	mutex_exit(&zilog->zl_lock);
    919 
    920 	/* destroy sync'd log transactions */
    921 	while ((itx = list_head(&clean_list)) != NULL) {
    922 		list_remove(&clean_list, itx);
    923 		kmem_free(itx, offsetof(itx_t, itx_lr)
    924 		    + itx->itx_lr.lrc_reclen);
    925 	}
    926 	list_destroy(&clean_list);
    927 }
    928 
    929 /*
    930  * If there are any in-memory intent log transactions which have now been
    931  * synced then start up a taskq to free them.
    932  */
    933 void
    934 zil_clean(zilog_t *zilog)
    935 {
    936 	itx_t *itx;
    937 
    938 	mutex_enter(&zilog->zl_lock);
    939 	itx = list_head(&zilog->zl_itx_list);
    940 	if ((itx != NULL) &&
    941 	    (itx->itx_lr.lrc_txg <= spa_last_synced_txg(zilog->zl_spa))) {
    942 		(void) taskq_dispatch(zilog->zl_clean_taskq,
    943 		    (void (*)(void *))zil_itx_clean, zilog, TQ_NOSLEEP);
    944 	}
    945 	mutex_exit(&zilog->zl_lock);
    946 }
    947 
    948 void
    949 zil_commit_writer(zilog_t *zilog, uint64_t seq, uint64_t foid)
    950 {
    951 	uint64_t txg;
    952 	uint64_t reclen;
    953 	uint64_t commit_seq = 0;
    954 	itx_t *itx, *itx_next = (itx_t *)-1;
    955 	lwb_t *lwb;
    956 	spa_t *spa;
    957 
    958 	zilog->zl_writer = B_TRUE;
    959 	zilog->zl_root_zio = NULL;
    960 	spa = zilog->zl_spa;
    961 
    962 	if (zilog->zl_suspend) {
    963 		lwb = NULL;
    964 	} else {
    965 		lwb = list_tail(&zilog->zl_lwb_list);
    966 		if (lwb == NULL) {
    967 			/*
    968 			 * Return if there's nothing to flush before we
    969 			 * dirty the fs by calling zil_create()
    970 			 */
    971 			if (list_is_empty(&zilog->zl_itx_list)) {
    972 				zilog->zl_writer = B_FALSE;
    973 				return;
    974 			}
    975 			mutex_exit(&zilog->zl_lock);
    976 			zil_create(zilog);
    977 			mutex_enter(&zilog->zl_lock);
    978 			lwb = list_tail(&zilog->zl_lwb_list);
    979 		}
    980 	}
    981 
    982 	/* Loop through in-memory log transactions filling log blocks. */
    983 	DTRACE_PROBE1(zil__cw1, zilog_t *, zilog);
    984 	for (;;) {
    985 		/*
    986 		 * Find the next itx to push:
    987 		 * Push all transactions related to specified foid and all
    988 		 * other transactions except TX_WRITE, TX_TRUNCATE,
    989 		 * TX_SETATTR and TX_ACL for all other files.
    990 		 */
    991 		if (itx_next != (itx_t *)-1)
    992 			itx = itx_next;
    993 		else
    994 			itx = list_head(&zilog->zl_itx_list);
    995 		for (; itx != NULL; itx = list_next(&zilog->zl_itx_list, itx)) {
    996 			if (foid == 0) /* push all foids? */
    997 				break;
    998 			if (itx->itx_sync) /* push all O_[D]SYNC */
    999 				break;
   1000 			switch (itx->itx_lr.lrc_txtype) {
   1001 			case TX_SETATTR:
   1002 			case TX_WRITE:
   1003 			case TX_TRUNCATE:
   1004 			case