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 2009 Sun Microsystems, Inc.  All rights reserved.
     23  * Use is subject to license terms.
     24  */
     25 
     26 #include <sys/zfs_context.h>
     27 #include <sys/dbuf.h>
     28 #include <sys/dnode.h>
     29 #include <sys/dmu.h>
     30 #include <sys/dmu_tx.h>
     31 #include <sys/dmu_objset.h>
     32 #include <sys/dsl_dataset.h>
     33 #include <sys/spa.h>
     34 
     35 static void
     36 dnode_increase_indirection(dnode_t *dn, dmu_tx_t *tx)
     37 {
     38 	dmu_buf_impl_t *db;
     39 	int txgoff = tx->tx_txg & TXG_MASK;
     40 	int nblkptr = dn->dn_phys->dn_nblkptr;
     41 	int old_toplvl = dn->dn_phys->dn_nlevels - 1;
     42 	int new_level = dn->dn_next_nlevels[txgoff];
     43 	int i;
     44 
     45 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
     46 
     47 	/* this dnode can't be paged out because it's dirty */
     48 	ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE);
     49 	ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
     50 	ASSERT(new_level > 1 && dn->dn_phys->dn_nlevels > 0);
     51 
     52 	db = dbuf_hold_level(dn, dn->dn_phys->dn_nlevels, 0, FTAG);
     53 	ASSERT(db != NULL);
     54 
     55 	dn->dn_phys->dn_nlevels = new_level;
     56 	dprintf("os=%p obj=%llu, increase to %d\n", dn->dn_objset,
     57 	    dn->dn_object, dn->dn_phys->dn_nlevels);
     58 
     59 	/* check for existing blkptrs in the dnode */
     60 	for (i = 0; i < nblkptr; i++)
     61 		if (!BP_IS_HOLE(&dn->dn_phys->dn_blkptr[i]))
     62 			break;
     63 	if (i != nblkptr) {
     64 		/* transfer dnode's block pointers to new indirect block */
     65 		(void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED|DB_RF_HAVESTRUCT);
     66 		ASSERT(db->db.db_data);
     67 		ASSERT(arc_released(db->db_buf));
     68 		ASSERT3U(sizeof (blkptr_t) * nblkptr, <=, db->db.db_size);
     69 		bcopy(dn->dn_phys->dn_blkptr, db->db.db_data,
     70 		    sizeof (blkptr_t) * nblkptr);
     71 		arc_buf_freeze(db->db_buf);
     72 	}
     73 
     74 	/* set dbuf's parent pointers to new indirect buf */
     75 	for (i = 0; i < nblkptr; i++) {
     76 		dmu_buf_impl_t *child = dbuf_find(dn, old_toplvl, i);
     77 
     78 		if (child == NULL)
     79 			continue;
     80 		ASSERT3P(child->db_dnode, ==, dn);
     81 		if (child->db_parent && child->db_parent != dn->dn_dbuf) {
     82 			ASSERT(child->db_parent->db_level == db->db_level);
     83 			ASSERT(child->db_blkptr !=
     84 			    &dn->dn_phys->dn_blkptr[child->db_blkid]);
     85 			mutex_exit(&child->db_mtx);
     86 			continue;
     87 		}
     88 		ASSERT(child->db_parent == NULL ||
     89 		    child->db_parent == dn->dn_dbuf);
     90 
     91 		child->db_parent = db;
     92 		dbuf_add_ref(db, child);
     93 		if (db->db.db_data)
     94 			child->db_blkptr = (blkptr_t *)db->db.db_data + i;
     95 		else
     96 			child->db_blkptr = NULL;
     97 		dprintf_dbuf_bp(child, child->db_blkptr,
     98 		    "changed db_blkptr to new indirect %s", "");
     99 
    100 		mutex_exit(&child->db_mtx);
    101 	}
    102 
    103 	bzero(dn->dn_phys->dn_blkptr, sizeof (blkptr_t) * nblkptr);
    104 
    105 	dbuf_rele(db, FTAG);
    106 
    107 	rw_exit(&dn->dn_struct_rwlock);
    108 }
    109 
    110 static int
    111 free_blocks(dnode_t *dn, blkptr_t *bp, int num, dmu_tx_t *tx)
    112 {
    113 	dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
    114 	uint64_t bytesfreed = 0;
    115 	int i, blocks_freed = 0;
    116 
    117 	dprintf("ds=%p obj=%llx num=%d\n", ds, dn->dn_object, num);
    118 
    119 	for (i = 0; i < num; i++, bp++) {
    120 		if (BP_IS_HOLE(bp))
    121 			continue;
    122 
    123 		bytesfreed += dsl_dataset_block_kill(ds, bp, dn->dn_zio, tx);
    124 		ASSERT3U(bytesfreed, <=, DN_USED_BYTES(dn->dn_phys));
    125 		bzero(bp, sizeof (blkptr_t));
    126 		blocks_freed += 1;
    127 	}
    128 	dnode_diduse_space(dn, -bytesfreed);
    129 	return (blocks_freed);
    130 }
    131 
    132 #ifdef ZFS_DEBUG
    133 static void
    134 free_verify(dmu_buf_impl_t *db, uint64_t start, uint64_t end, dmu_tx_t *tx)
    135 {
    136 	int off, num;
    137 	int i, err, epbs;
    138 	uint64_t txg = tx->tx_txg;
    139 
    140 	epbs = db->db_dnode->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
    141 	off = start - (db->db_blkid * 1<<epbs);
    142 	num = end - start + 1;
    143 
    144 	ASSERT3U(off, >=, 0);
    145 	ASSERT3U(num, >=, 0);
    146 	ASSERT3U(db->db_level, >, 0);
    147 	ASSERT3U(db->db.db_size, ==, 1<<db->db_dnode->dn_phys->dn_indblkshift);
    148 	ASSERT3U(off+num, <=, db->db.db_size >> SPA_BLKPTRSHIFT);
    149 	ASSERT(db->db_blkptr != NULL);
    150 
    151 	for (i = off; i < off+num; i++) {
    152 		uint64_t *buf;
    153 		dmu_buf_impl_t *child;
    154 		dbuf_dirty_record_t *dr;
    155 		int j;
    156 
    157 		ASSERT(db->db_level == 1);
    158 
    159 		rw_enter(&db->db_dnode->dn_struct_rwlock, RW_READER);
    160 		err = dbuf_hold_impl(db->db_dnode, db->db_level-1,
    161 		    (db->db_blkid << epbs) + i, TRUE, FTAG, &child);
    162 		rw_exit(&db->db_dnode->dn_struct_rwlock);
    163 		if (err == ENOENT)
    164 			continue;
    165 		ASSERT(err == 0);
    166 		ASSERT(child->db_level == 0);
    167 		dr = child->db_last_dirty;
    168 		while (dr && dr->dr_txg > txg)
    169 			dr = dr->dr_next;
    170 		ASSERT(dr == NULL || dr->dr_txg == txg);
    171 
    172 		/* data_old better be zeroed */
    173 		if (dr) {
    174 			buf = dr->dt.dl.dr_data->b_data;
    175 			for (j = 0; j < child->db.db_size >> 3; j++) {
    176 				if (buf[j] != 0) {
    177 					panic("freed data not zero: "
    178 					    "child=%p i=%d off=%d num=%d\n",
    179 					    (void *)child, i, off, num);
    180 				}
    181 			}
    182 		}
    183 
    184 		/*
    185 		 * db_data better be zeroed unless it's dirty in a
    186 		 * future txg.
    187 		 */
    188 		mutex_enter(&child->db_mtx);
    189 		buf = child->db.db_data;
    190 		if (buf != NULL && child->db_state != DB_FILL &&
    191 		    child->db_last_dirty == NULL) {
    192 			for (j = 0; j < child->db.db_size >> 3; j++) {
    193 				if (buf[j] != 0) {
    194 					panic("freed data not zero: "
    195 					    "child=%p i=%d off=%d num=%d\n",
    196 					    (void *)child, i, off, num);
    197 				}
    198 			}
    199 		}
    200 		mutex_exit(&child->db_mtx);
    201 
    202 		dbuf_rele(child, FTAG);
    203 	}
    204 }
    205 #endif
    206 
    207 #define	ALL -1
    208 
    209 static int
    210 free_children(dmu_buf_impl_t *db, uint64_t blkid, uint64_t nblks, int trunc,
    211     dmu_tx_t *tx)
    212 {
    213 	dnode_t *dn = db->db_dnode;
    214 	blkptr_t *bp;
    215 	dmu_buf_impl_t *subdb;
    216 	uint64_t start, end, dbstart, dbend, i;
    217 	int epbs, shift, err;
    218 	int all = TRUE;
    219 	int blocks_freed = 0;
    220 
    221 	/*
    222 	 * There is a small possibility that this block will not be cached:
    223 	 *   1 - if level > 1 and there are no children with level <= 1
    224 	 *   2 - if we didn't get a dirty hold (because this block had just
    225 	 *	 finished being written -- and so had no holds), and then this
    226 	 *	 block got evicted before we got here.
    227 	 */
    228 	if (db->db_state != DB_CACHED)
    229 		(void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
    230 
    231 	arc_release(db->db_buf, db);
    232 	bp = (blkptr_t *)db->db.db_data;
    233 
    234 	epbs = db->db_dnode->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
    235 	shift = (db->db_level - 1) * epbs;
    236 	dbstart = db->db_blkid << epbs;
    237 	start = blkid >> shift;
    238 	if (dbstart < start) {
    239 		bp += start - dbstart;
    240 		all = FALSE;
    241 	} else {
    242 		start = dbstart;
    243 	}
    244 	dbend = ((db->db_blkid + 1) << epbs) - 1;
    245 	end = (blkid + nblks - 1) >> shift;
    246 	if (dbend <= end)
    247 		end = dbend;
    248 	else if (all)
    249 		all = trunc;
    250 	ASSERT3U(start, <=, end);
    251 
    252 	if (db->db_level == 1) {
    253 		FREE_VERIFY(db, start, end, tx);
    254 		blocks_freed = free_blocks(dn, bp, end-start+1, tx);
    255 		arc_buf_freeze(db->db_buf);
    256 		ASSERT(all || blocks_freed == 0 || db->db_last_dirty);
    257 		return (all ? ALL : blocks_freed);
    258 	}
    259 
    260 	for (i = start; i <= end; i++, bp++) {
    261 		if (BP_IS_HOLE(bp))
    262 			continue;
    263 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
    264 		err = dbuf_hold_impl(dn, db->db_level-1, i, TRUE, FTAG, &subdb);
    265 		ASSERT3U(err, ==, 0);
    266 		rw_exit(&dn->dn_struct_rwlock);
    267 
    268 		if (free_children(subdb, blkid, nblks, trunc, tx) == ALL) {
    269 			ASSERT3P(subdb->db_blkptr, ==, bp);
    270 			blocks_freed += free_blocks(dn, bp, 1, tx);
    271 		} else {
    272 			all = FALSE;
    273 		}
    274 		dbuf_rele(subdb, FTAG);
    275 	}
    276 	arc_buf_freeze(db->db_buf);
    277 #ifdef ZFS_DEBUG
    278 	bp -= (end-start)+1;
    279 	for (i = start; i <= end; i++, bp++) {
    280 		if (i == start && blkid != 0)
    281 			continue;
    282 		else if (i == end && !trunc)
    283 			continue;
    284 		ASSERT3U(bp->blk_birth, ==, 0);
    285 	}
    286 #endif
    287 	ASSERT(all || blocks_freed == 0 || db->db_last_dirty);
    288 	return (all ? ALL : blocks_freed);
    289 }
    290 
    291 /*
    292  * free_range: Traverse the indicated range of the provided file
    293  * and "free" all the blocks contained there.
    294  */
    295 static void
    296 dnode_sync_free_range(dnode_t *dn, uint64_t blkid, uint64_t nblks, dmu_tx_t *tx)
    297 {
    298 	blkptr_t *bp = dn->dn_phys->dn_blkptr;
    299 	dmu_buf_impl_t *db;
    300 	int trunc, start, end, shift, i, err;
    301 	int dnlevel = dn->dn_phys->dn_nlevels;
    302 
    303 	if (blkid > dn->dn_phys->dn_maxblkid)
    304 		return;
    305 
    306 	ASSERT(dn->dn_phys->dn_maxblkid < UINT64_MAX);
    307 	trunc = blkid + nblks > dn->dn_phys->dn_maxblkid;
    308 	if (trunc)
    309 		nblks = dn->dn_phys->dn_maxblkid - blkid + 1;
    310 
    311 	/* There are no indirect blocks in the object */
    312 	if (dnlevel == 1) {
    313 		if (blkid >= dn->dn_phys->dn_nblkptr) {
    314 			/* this range was never made persistent */
    315 			return;
    316 		}
    317 		ASSERT3U(blkid + nblks, <=, dn->dn_phys->dn_nblkptr);
    318 		(void) free_blocks(dn, bp + blkid, nblks, tx);
    319 		if (trunc) {
    320 			uint64_t off = (dn->dn_phys->dn_maxblkid + 1) *
    321 			    (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT);
    322 			dn->dn_phys->dn_maxblkid = (blkid ? blkid - 1 : 0);
    323 			ASSERT(off < dn->dn_phys->dn_maxblkid ||
    324 			    dn->dn_phys->dn_maxblkid == 0 ||
    325 			    dnode_next_offset(dn, 0, &off, 1, 1, 0) != 0);
    326 		}
    327 		return;
    328 	}
    329 
    330 	shift = (dnlevel - 1) * (dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT);
    331 	start = blkid >> shift;
    332 	ASSERT(start < dn->dn_phys->dn_nblkptr);
    333 	end = (blkid + nblks - 1) >> shift;
    334 	bp += start;
    335 	for (i = start; i <= end; i++, bp++) {
    336 		if (BP_IS_HOLE(bp))
    337 			continue;
    338 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
    339 		err = dbuf_hold_impl(dn, dnlevel-1, i, TRUE, FTAG, &db);
    340 		ASSERT3U(err, ==, 0);
    341 		rw_exit(&dn->dn_struct_rwlock);
    342 
    343 		if (free_children(db, blkid, nblks, trunc, tx) == ALL) {
    344 			ASSERT3P(db->db_blkptr, ==, bp);
    345 			(void) free_blocks(dn, bp, 1, tx);
    346 		}
    347 		dbuf_rele(db, FTAG);
    348 	}
    349 	if (trunc) {
    350 		uint64_t off = (dn->dn_phys->dn_maxblkid + 1) *
    351 		    (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT);
    352 		dn->dn_phys->dn_maxblkid = (blkid ? blkid - 1 : 0);
    353 		ASSERT(off < dn->dn_phys->dn_maxblkid ||
    354 		    dn->dn_phys->dn_maxblkid == 0 ||
    355 		    dnode_next_offset(dn, 0, &off, 1, 1, 0) != 0);
    356 	}
    357 }
    358 
    359 /*
    360  * Try to kick all the dnodes dbufs out of the cache...
    361  */
    362 void
    363 dnode_evict_dbufs(dnode_t *dn)
    364 {
    365 	int progress;
    366 	int pass = 0;
    367 
    368 	do {
    369 		dmu_buf_impl_t *db, marker;
    370 		int evicting = FALSE;
    371 
    372 		progress = FALSE;
    373 		mutex_enter(&dn->dn_dbufs_mtx);
    374 		list_insert_tail(&dn->dn_dbufs, &marker);
    375 		db = list_head(&dn->dn_dbufs);
    376 		for (; db != &marker; db = list_head(&dn->dn_dbufs)) {
    377 			list_remove(&dn->dn_dbufs, db);
    378 			list_insert_tail(&dn->dn_dbufs, db);
    379 			ASSERT3P(db->db_dnode, ==, dn);
    380 
    381 			mutex_enter(&db->db_mtx);
    382 			if (db->db_state == DB_EVICTING) {
    383 				progress = TRUE;
    384 				evicting = TRUE;
    385 				mutex_exit(&db->db_mtx);
    386 			} else if (refcount_is_zero(&db->db_holds)) {
    387 				progress = TRUE;
    388 				dbuf_clear(db); /* exits db_mtx for us */
    389 			} else {
    390 				mutex_exit(&db->db_mtx);
    391 			}
    392 
    393 		}
    394 		list_remove(&dn->dn_dbufs, &marker);
    395 		/*
    396 		 * NB: we need to drop dn_dbufs_mtx between passes so
    397 		 * that any DB_EVICTING dbufs can make progress.
    398 		 * Ideally, we would have some cv we could wait on, but
    399 		 * since we don't, just wait a bit to give the other
    400 		 * thread a chance to run.
    401 		 */
    402 		mutex_exit(&dn->dn_dbufs_mtx);
    403 		if (evicting)
    404 			delay(1);
    405 		pass++;
    406 		ASSERT(pass < 100); /* sanity check */
    407 	} while (progress);
    408 
    409 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
    410 	if (dn->dn_bonus && refcount_is_zero(&dn->dn_bonus->db_holds)) {
    411 		mutex_enter(&dn->dn_bonus->db_mtx);
    412 		dbuf_evict(dn->dn_bonus);
    413 		dn->dn_bonus = NULL;
    414 	}
    415 	rw_exit(&dn->dn_struct_rwlock);
    416 }
    417 
    418 static void
    419 dnode_undirty_dbufs(list_t *list)
    420 {
    421 	dbuf_dirty_record_t *dr;
    422 
    423 	while (dr = list_head(list)) {
    424 		dmu_buf_impl_t *db = dr->dr_dbuf;
    425 		uint64_t txg = dr->dr_txg;
    426 
    427 		mutex_enter(&db->db_mtx);
    428 		/* XXX - use dbuf_undirty()? */
    429 		list_remove(list, dr);
    430 		ASSERT(db->db_last_dirty == dr);
    431 		db->db_last_dirty = NULL;
    432 		db->db_dirtycnt -= 1;
    433 		if (db->db_level == 0) {
    434 			ASSERT(db->db_blkid == DB_BONUS_BLKID ||
    435 			    dr->dt.dl.dr_data == db->db_buf);
    436 			dbuf_unoverride(dr);
    437 			mutex_exit(&db->db_mtx);
    438 		} else {
    439 			mutex_exit(&db->db_mtx);
    440 			dnode_undirty_dbufs(&dr->dt.di.dr_children);
    441 		}
    442 		kmem_free(dr, sizeof (dbuf_dirty_record_t));
    443 		dbuf_rele(db, (void *)(uintptr_t)txg);
    444 	}
    445 }
    446 
    447 static void
    448 dnode_sync_free(dnode_t *dn, dmu_tx_t *tx)
    449 {
    450 	int txgoff = tx->tx_txg & TXG_MASK;
    451 
    452 	ASSERT(dmu_tx_is_syncing(tx));
    453 
    454 	/*
    455 	 * Our contents should have been freed in dnode_sync() by the
    456 	 * free range record inserted by the caller of dnode_free().
    457 	 */
    458 	ASSERT3U(DN_USED_BYTES(dn->dn_phys), ==, 0);
    459 	ASSERT(BP_IS_HOLE(dn->dn_phys->dn_blkptr));
    460 
    461 	dnode_undirty_dbufs(&dn->dn_dirty_records[txgoff]);
    462 	dnode_evict_dbufs(dn);
    463 	ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL);
    464 
    465 	/*
    466 	 * XXX - It would be nice to assert this, but we may still
    467 	 * have residual holds from async evictions from the arc...
    468 	 *
    469 	 * zfs_obj_to_path() also depends on this being
    470 	 * commented out.
    471 	 *
    472 	 * ASSERT3U(refcount_count(&dn->dn_holds), ==, 1);
    473 	 */
    474 
    475 	/* Undirty next bits */
    476 	dn->dn_next_nlevels[txgoff] = 0;
    477 	dn->dn_next_indblkshift[txgoff] = 0;
    478 	dn->dn_next_blksz[txgoff] = 0;
    479 
    480 	/* ASSERT(blkptrs are zero); */
    481 	ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE);
    482 	ASSERT(dn->dn_type != DMU_OT_NONE);
    483 
    484 	ASSERT(dn->dn_free_txg > 0);
    485 	if (dn->dn_allocated_txg != dn->dn_free_txg)
    486 		dbuf_will_dirty(dn->dn_dbuf, tx);
    487 	bzero(dn->dn_phys, sizeof (dnode_phys_t));
    488 
    489 	mutex_enter(&dn->dn_mtx);
    490 	dn->dn_type = DMU_OT_NONE;
    491 	dn->dn_maxblkid = 0;
    492 	dn->dn_allocated_txg = 0;
    493 	dn->dn_free_txg = 0;
    494 	mutex_exit(&dn->dn_mtx);
    495 
    496 	ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
    497 
    498 	dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg);
    499 	/*
    500 	 * Now that we've released our hold, the dnode may
    501 	 * be evicted, so we musn't access it.
    502 	 */
    503 }
    504 
    505 /*
    506  * Write out the dnode's dirty buffers.
    507  */
    508 void
    509 dnode_sync(dnode_t *dn, dmu_tx_t *tx)
    510 {
    511 	free_range_t *rp;
    512 	dnode_phys_t *dnp = dn->dn_phys;
    513 	int txgoff = tx->tx_txg & TXG_MASK;
    514 	list_t *list = &dn->dn_dirty_records[txgoff];
    515 	static const dnode_phys_t zerodn = { 0 };
    516 
    517 	ASSERT(dmu_tx_is_syncing(tx));
    518 	ASSERT(dnp->dn_type != DMU_OT_NONE || dn->dn_allocated_txg);
    519 	ASSERT(dnp->dn_type != DMU_OT_NONE ||
    520 	    bcmp(dnp, &zerodn, DNODE_SIZE) == 0);
    521 	DNODE_VERIFY(dn);
    522 
    523 	ASSERT(dn->dn_dbuf == NULL || arc_released(dn->dn_dbuf->db_buf));
    524 
    525 	if (dmu_objset_userused_enabled(dn->dn_objset) &&
    526 	    !DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
    527 		ASSERT(dn->dn_oldphys == NULL);
    528 		dn->dn_oldphys = zio_buf_alloc(sizeof (dnode_phys_t));
    529 		*dn->dn_oldphys = *dn->dn_phys; /* struct assignment */
    530 		dn->dn_phys->dn_flags |= DNODE_FLAG_USERUSED_ACCOUNTED;
    531 	} else {
    532 		/* Once we account for it, we should always account for it. */
    533 		ASSERT(!(dn->dn_phys->dn_flags &
    534 		    DNODE_FLAG_USERUSED_ACCOUNTED));
    535 	}
    536 
    537 	mutex_enter(&dn->dn_mtx);
    538 	if (dn->dn_allocated_txg == tx->tx_txg) {
    539 		/* The dnode is newly allocated or reallocated */
    540 		if (dnp->dn_type == DMU_OT_NONE) {
    541 			/* this is a first alloc, not a realloc */
    542 			dnp->dn_nlevels = 1;
    543 			dnp->dn_nblkptr = dn->dn_nblkptr;
    544 		}
    545 
    546 		dnp->dn_type = dn->dn_type;
    547 		dnp->dn_bonustype = dn->dn_bonustype;
    548 		dnp->dn_bonuslen = dn->dn_bonuslen;
    549 	}
    550 
    551 	ASSERT(dnp->dn_nlevels > 1 ||
    552 	    BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
    553 	    BP_GET_LSIZE(&dnp->dn_blkptr[0]) ==
    554 	    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
    555 
    556 	if (dn->dn_next_blksz[txgoff]) {
    557 		ASSERT(P2PHASE(dn->dn_next_blksz[txgoff],
    558 		    SPA_MINBLOCKSIZE) == 0);
    559 		ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
    560 		    dn->dn_maxblkid == 0 || list_head(list) != NULL ||
    561 		    dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT ==
    562 		    dnp->dn_datablkszsec);
    563 		dnp->dn_datablkszsec =
    564 		    dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT;
    565 		dn->dn_next_blksz[txgoff] = 0;
    566 	}
    567 
    568 	if (dn->dn_next_bonuslen[txgoff]) {
    569 		if (dn->dn_next_bonuslen[txgoff] == DN_ZERO_BONUSLEN)
    570 			dnp->dn_bonuslen = 0;
    571 		else
    572 			dnp->dn_bonuslen = dn->dn_next_bonuslen[txgoff];
    573 		ASSERT(dnp->dn_bonuslen <= DN_MAX_BONUSLEN);
    574 		dn->dn_next_bonuslen[txgoff] = 0;
    575 	}
    576 
    577 	if (dn->dn_next_indblkshift[txgoff]) {
    578 		ASSERT(dnp->dn_nlevels == 1);
    579 		dnp->dn_indblkshift = dn->dn_next_indblkshift[txgoff];
    580 		dn->dn_next_indblkshift[txgoff] = 0;
    581 	}
    582 
    583 	/*
    584 	 * Just take the live (open-context) values for checksum and compress.
    585 	 * Strictly speaking it's a future leak, but nothing bad happens if we
    586 	 * start using the new checksum or compress algorithm a little early.
    587 	 */
    588 	dnp->dn_checksum = dn->dn_checksum;
    589 	dnp->dn_compress = dn->dn_compress;
    590 
    591 	mutex_exit(&dn->dn_mtx);
    592 
    593 	/* process all the "freed" ranges in the file */
    594 	while (rp = avl_last(&dn->dn_ranges[txgoff])) {
    595 		dnode_sync_free_range(dn, rp->fr_blkid, rp->fr_nblks, tx);
    596 		/* grab the mutex so we don't race with dnode_block_freed() */
    597 		mutex_enter(&dn->dn_mtx);
    598 		avl_remove(&dn->dn_ranges[txgoff], rp);
    599 		mutex_exit(&dn->dn_mtx);
    600 		kmem_free(rp, sizeof (free_range_t));
    601 	}
    602 
    603 	if (dn->dn_free_txg > 0 && dn->dn_free_txg <= tx->tx_txg) {
    604 		dnode_sync_free(dn, tx);
    605 		return;
    606 	}
    607 
    608 	if (dn->dn_next_nblkptr[txgoff]) {
    609 		/* this should only happen on a realloc */
    610 		ASSERT(dn->dn_allocated_txg == tx->tx_txg);
    611 		if (dn->dn_next_nblkptr[txgoff] > dnp->dn_nblkptr) {
    612 			/* zero the new blkptrs we are gaining */
    613 			bzero(dnp->dn_blkptr + dnp->dn_nblkptr,
    614 			    sizeof (blkptr_t) *
    615 			    (dn->dn_next_nblkptr[txgoff] - dnp->dn_nblkptr));
    616 #ifdef ZFS_DEBUG
    617 		} else {
    618 			int i;
    619 			ASSERT(dn->dn_next_nblkptr[txgoff] < dnp->dn_nblkptr);
    620 			/* the blkptrs we are losing better be unallocated */
    621 			for (i = dn->dn_next_nblkptr[txgoff];
    622 			    i < dnp->dn_nblkptr; i++)
    623 				ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[i]));
    624 #endif
    625 		}
    626 		mutex_enter(&dn->dn_mtx);
    627 		dnp->dn_nblkptr = dn->dn_next_nblkptr[txgoff];
    628 		dn->dn_next_nblkptr[txgoff] = 0;
    629 		mutex_exit(&dn->dn_mtx);
    630 	}
    631 
    632 	if (dn->dn_next_nlevels[txgoff]) {
    633 		dnode_increase_indirection(dn, tx);
    634 		dn->dn_next_nlevels[txgoff] = 0;
    635 	}
    636 
    637 	dbuf_sync_list(list, tx);
    638 
    639 	if (!DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
    640 		ASSERT3P(list_head(list), ==, NULL);
    641 		dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg);
    642 	}
    643 
    644 	/*
    645 	 * Although we have dropped our reference to the dnode, it
    646 	 * can't be evicted until its written, and we haven't yet
    647 	 * initiated the IO for the dnode's dbuf.
    648 	 */
    649 }
    650