xref: /titanic_51/usr/src/uts/common/fs/zfs/dnode_sync.c (revision 1dbc1fed8be6e82e676ff3f124628dc470058bfb)
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, tx, B_FALSE);
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 		if (db->db_level != 0)
428 			dnode_undirty_dbufs(&dr->dt.di.dr_children);
429 
430 		mutex_enter(&db->db_mtx);
431 		/* XXX - use dbuf_undirty()? */
432 		list_remove(list, dr);
433 		ASSERT(db->db_last_dirty == dr);
434 		db->db_last_dirty = NULL;
435 		db->db_dirtycnt -= 1;
436 		if (db->db_level == 0) {
437 			ASSERT(db->db_blkid == DB_BONUS_BLKID ||
438 			    dr->dt.dl.dr_data == db->db_buf);
439 			dbuf_unoverride(dr);
440 		}
441 		kmem_free(dr, sizeof (dbuf_dirty_record_t));
442 		dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg);
443 	}
444 }
445 
446 static void
447 dnode_sync_free(dnode_t *dn, dmu_tx_t *tx)
448 {
449 	int txgoff = tx->tx_txg & TXG_MASK;
450 
451 	ASSERT(dmu_tx_is_syncing(tx));
452 
453 	/*
454 	 * Our contents should have been freed in dnode_sync() by the
455 	 * free range record inserted by the caller of dnode_free().
456 	 */
457 	ASSERT3U(DN_USED_BYTES(dn->dn_phys), ==, 0);
458 	ASSERT(BP_IS_HOLE(dn->dn_phys->dn_blkptr));
459 
460 	dnode_undirty_dbufs(&dn->dn_dirty_records[txgoff]);
461 	dnode_evict_dbufs(dn);
462 	ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL);
463 
464 	/*
465 	 * XXX - It would be nice to assert this, but we may still
466 	 * have residual holds from async evictions from the arc...
467 	 *
468 	 * zfs_obj_to_path() also depends on this being
469 	 * commented out.
470 	 *
471 	 * ASSERT3U(refcount_count(&dn->dn_holds), ==, 1);
472 	 */
473 
474 	/* Undirty next bits */
475 	dn->dn_next_nlevels[txgoff] = 0;
476 	dn->dn_next_indblkshift[txgoff] = 0;
477 	dn->dn_next_blksz[txgoff] = 0;
478 
479 	/* ASSERT(blkptrs are zero); */
480 	ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE);
481 	ASSERT(dn->dn_type != DMU_OT_NONE);
482 
483 	ASSERT(dn->dn_free_txg > 0);
484 	if (dn->dn_allocated_txg != dn->dn_free_txg)
485 		dbuf_will_dirty(dn->dn_dbuf, tx);
486 	bzero(dn->dn_phys, sizeof (dnode_phys_t));
487 
488 	mutex_enter(&dn->dn_mtx);
489 	dn->dn_type = DMU_OT_NONE;
490 	dn->dn_maxblkid = 0;
491 	dn->dn_allocated_txg = 0;
492 	dn->dn_free_txg = 0;
493 	mutex_exit(&dn->dn_mtx);
494 
495 	ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
496 
497 	dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg);
498 	/*
499 	 * Now that we've released our hold, the dnode may
500 	 * be evicted, so we musn't access it.
501 	 */
502 }
503 
504 /*
505  * Write out the dnode's dirty buffers.
506  */
507 void
508 dnode_sync(dnode_t *dn, dmu_tx_t *tx)
509 {
510 	free_range_t *rp;
511 	dnode_phys_t *dnp = dn->dn_phys;
512 	int txgoff = tx->tx_txg & TXG_MASK;
513 	list_t *list = &dn->dn_dirty_records[txgoff];
514 	static const dnode_phys_t zerodn = { 0 };
515 
516 	ASSERT(dmu_tx_is_syncing(tx));
517 	ASSERT(dnp->dn_type != DMU_OT_NONE || dn->dn_allocated_txg);
518 	ASSERT(dnp->dn_type != DMU_OT_NONE ||
519 	    bcmp(dnp, &zerodn, DNODE_SIZE) == 0);
520 	DNODE_VERIFY(dn);
521 
522 	ASSERT(dn->dn_dbuf == NULL || arc_released(dn->dn_dbuf->db_buf));
523 
524 	if (dmu_objset_userused_enabled(dn->dn_objset) &&
525 	    !DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
526 		ASSERT(dn->dn_oldphys == NULL);
527 		dn->dn_oldphys = zio_buf_alloc(sizeof (dnode_phys_t));
528 		*dn->dn_oldphys = *dn->dn_phys; /* struct assignment */
529 		dn->dn_phys->dn_flags |= DNODE_FLAG_USERUSED_ACCOUNTED;
530 	} else {
531 		/* Once we account for it, we should always account for it. */
532 		ASSERT(!(dn->dn_phys->dn_flags &
533 		    DNODE_FLAG_USERUSED_ACCOUNTED));
534 	}
535 
536 	mutex_enter(&dn->dn_mtx);
537 	if (dn->dn_allocated_txg == tx->tx_txg) {
538 		/* The dnode is newly allocated or reallocated */
539 		if (dnp->dn_type == DMU_OT_NONE) {
540 			/* this is a first alloc, not a realloc */
541 			dnp->dn_nlevels = 1;
542 			dnp->dn_nblkptr = dn->dn_nblkptr;
543 		}
544 
545 		dnp->dn_type = dn->dn_type;
546 		dnp->dn_bonustype = dn->dn_bonustype;
547 		dnp->dn_bonuslen = dn->dn_bonuslen;
548 	}
549 
550 	ASSERT(dnp->dn_nlevels > 1 ||
551 	    BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
552 	    BP_GET_LSIZE(&dnp->dn_blkptr[0]) ==
553 	    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
554 
555 	if (dn->dn_next_blksz[txgoff]) {
556 		ASSERT(P2PHASE(dn->dn_next_blksz[txgoff],
557 		    SPA_MINBLOCKSIZE) == 0);
558 		ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
559 		    dn->dn_maxblkid == 0 || list_head(list) != NULL ||
560 		    dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT ==
561 		    dnp->dn_datablkszsec);
562 		dnp->dn_datablkszsec =
563 		    dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT;
564 		dn->dn_next_blksz[txgoff] = 0;
565 	}
566 
567 	if (dn->dn_next_bonuslen[txgoff]) {
568 		if (dn->dn_next_bonuslen[txgoff] == DN_ZERO_BONUSLEN)
569 			dnp->dn_bonuslen = 0;
570 		else
571 			dnp->dn_bonuslen = dn->dn_next_bonuslen[txgoff];
572 		ASSERT(dnp->dn_bonuslen <= DN_MAX_BONUSLEN);
573 		dn->dn_next_bonuslen[txgoff] = 0;
574 	}
575 
576 	if (dn->dn_next_indblkshift[txgoff]) {
577 		ASSERT(dnp->dn_nlevels == 1);
578 		dnp->dn_indblkshift = dn->dn_next_indblkshift[txgoff];
579 		dn->dn_next_indblkshift[txgoff] = 0;
580 	}
581 
582 	/*
583 	 * Just take the live (open-context) values for checksum and compress.
584 	 * Strictly speaking it's a future leak, but nothing bad happens if we
585 	 * start using the new checksum or compress algorithm a little early.
586 	 */
587 	dnp->dn_checksum = dn->dn_checksum;
588 	dnp->dn_compress = dn->dn_compress;
589 
590 	mutex_exit(&dn->dn_mtx);
591 
592 	/* process all the "freed" ranges in the file */
593 	while (rp = avl_last(&dn->dn_ranges[txgoff])) {
594 		dnode_sync_free_range(dn, rp->fr_blkid, rp->fr_nblks, tx);
595 		/* grab the mutex so we don't race with dnode_block_freed() */
596 		mutex_enter(&dn->dn_mtx);
597 		avl_remove(&dn->dn_ranges[txgoff], rp);
598 		mutex_exit(&dn->dn_mtx);
599 		kmem_free(rp, sizeof (free_range_t));
600 	}
601 
602 	if (dn->dn_free_txg > 0 && dn->dn_free_txg <= tx->tx_txg) {
603 		dnode_sync_free(dn, tx);
604 		return;
605 	}
606 
607 	if (dn->dn_next_nblkptr[txgoff]) {
608 		/* this should only happen on a realloc */
609 		ASSERT(dn->dn_allocated_txg == tx->tx_txg);
610 		if (dn->dn_next_nblkptr[txgoff] > dnp->dn_nblkptr) {
611 			/* zero the new blkptrs we are gaining */
612 			bzero(dnp->dn_blkptr + dnp->dn_nblkptr,
613 			    sizeof (blkptr_t) *
614 			    (dn->dn_next_nblkptr[txgoff] - dnp->dn_nblkptr));
615 #ifdef ZFS_DEBUG
616 		} else {
617 			int i;
618 			ASSERT(dn->dn_next_nblkptr[txgoff] < dnp->dn_nblkptr);
619 			/* the blkptrs we are losing better be unallocated */
620 			for (i = dn->dn_next_nblkptr[txgoff];
621 			    i < dnp->dn_nblkptr; i++)
622 				ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[i]));
623 #endif
624 		}
625 		mutex_enter(&dn->dn_mtx);
626 		dnp->dn_nblkptr = dn->dn_next_nblkptr[txgoff];
627 		dn->dn_next_nblkptr[txgoff] = 0;
628 		mutex_exit(&dn->dn_mtx);
629 	}
630 
631 	if (dn->dn_next_nlevels[txgoff]) {
632 		dnode_increase_indirection(dn, tx);
633 		dn->dn_next_nlevels[txgoff] = 0;
634 	}
635 
636 	dbuf_sync_list(list, tx);
637 
638 	if (!DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
639 		ASSERT3P(list_head(list), ==, NULL);
640 		dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg);
641 	}
642 
643 	/*
644 	 * Although we have dropped our reference to the dnode, it
645 	 * can't be evicted until its written, and we haven't yet
646 	 * initiated the IO for the dnode's dbuf.
647 	 */
648 }
649