xref: /titanic_51/usr/src/uts/common/fs/zfs/dnode_sync.c (revision bfed486ad8de8b8ebc6345a8e10accae08bf2f45)
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  * NOTE: The dnode is kept in memory by being dirty.  Once the
509  * dirty bit is cleared, it may be evicted.  Beware of this!
510  */
511 void
512 dnode_sync(dnode_t *dn, dmu_tx_t *tx)
513 {
514 	free_range_t *rp;
515 	dnode_phys_t *dnp = dn->dn_phys;
516 	int txgoff = tx->tx_txg & TXG_MASK;
517 	list_t *list = &dn->dn_dirty_records[txgoff];
518 
519 	ASSERT(dmu_tx_is_syncing(tx));
520 	ASSERT(dnp->dn_type != DMU_OT_NONE || dn->dn_allocated_txg);
521 	DNODE_VERIFY(dn);
522 
523 	ASSERT(dn->dn_dbuf == NULL || arc_released(dn->dn_dbuf->db_buf));
524 
525 	mutex_enter(&dn->dn_mtx);
526 	if (dn->dn_allocated_txg == tx->tx_txg) {
527 		/* The dnode is newly allocated or reallocated */
528 		if (dnp->dn_type == DMU_OT_NONE) {
529 			/* this is a first alloc, not a realloc */
530 			/* XXX shouldn't the phys already be zeroed? */
531 			bzero(dnp, DNODE_CORE_SIZE);
532 			dnp->dn_nlevels = 1;
533 			dnp->dn_nblkptr = dn->dn_nblkptr;
534 		}
535 
536 		dnp->dn_type = dn->dn_type;
537 		dnp->dn_bonustype = dn->dn_bonustype;
538 		dnp->dn_bonuslen = dn->dn_bonuslen;
539 	}
540 
541 	ASSERT(dnp->dn_nlevels > 1 ||
542 	    BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
543 	    BP_GET_LSIZE(&dnp->dn_blkptr[0]) ==
544 	    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
545 
546 	if (dn->dn_next_blksz[txgoff]) {
547 		ASSERT(P2PHASE(dn->dn_next_blksz[txgoff],
548 		    SPA_MINBLOCKSIZE) == 0);
549 		ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
550 		    dn->dn_maxblkid == 0 || list_head(list) != NULL ||
551 		    dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT ==
552 		    dnp->dn_datablkszsec);
553 		dnp->dn_datablkszsec =
554 		    dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT;
555 		dn->dn_next_blksz[txgoff] = 0;
556 	}
557 
558 	if (dn->dn_next_bonuslen[txgoff]) {
559 		if (dn->dn_next_bonuslen[txgoff] == DN_ZERO_BONUSLEN)
560 			dnp->dn_bonuslen = 0;
561 		else
562 			dnp->dn_bonuslen = dn->dn_next_bonuslen[txgoff];
563 		ASSERT(dnp->dn_bonuslen <= DN_MAX_BONUSLEN);
564 		dn->dn_next_bonuslen[txgoff] = 0;
565 	}
566 
567 	if (dn->dn_next_indblkshift[txgoff]) {
568 		ASSERT(dnp->dn_nlevels == 1);
569 		dnp->dn_indblkshift = dn->dn_next_indblkshift[txgoff];
570 		dn->dn_next_indblkshift[txgoff] = 0;
571 	}
572 
573 	/*
574 	 * Just take the live (open-context) values for checksum and compress.
575 	 * Strictly speaking it's a future leak, but nothing bad happens if we
576 	 * start using the new checksum or compress algorithm a little early.
577 	 */
578 	dnp->dn_checksum = dn->dn_checksum;
579 	dnp->dn_compress = dn->dn_compress;
580 
581 	mutex_exit(&dn->dn_mtx);
582 
583 	/* process all the "freed" ranges in the file */
584 	while (rp = avl_last(&dn->dn_ranges[txgoff])) {
585 		dnode_sync_free_range(dn, rp->fr_blkid, rp->fr_nblks, tx);
586 		/* grab the mutex so we don't race with dnode_block_freed() */
587 		mutex_enter(&dn->dn_mtx);
588 		avl_remove(&dn->dn_ranges[txgoff], rp);
589 		mutex_exit(&dn->dn_mtx);
590 		kmem_free(rp, sizeof (free_range_t));
591 	}
592 
593 	if (dn->dn_free_txg > 0 && dn->dn_free_txg <= tx->tx_txg) {
594 		dnode_sync_free(dn, tx);
595 		return;
596 	}
597 
598 	if (dn->dn_next_nblkptr[txgoff]) {
599 		/* this should only happen on a realloc */
600 		ASSERT(dn->dn_allocated_txg == tx->tx_txg);
601 		if (dn->dn_next_nblkptr[txgoff] > dnp->dn_nblkptr) {
602 			/* zero the new blkptrs we are gaining */
603 			bzero(dnp->dn_blkptr + dnp->dn_nblkptr,
604 			    sizeof (blkptr_t) *
605 			    (dn->dn_next_nblkptr[txgoff] - dnp->dn_nblkptr));
606 #ifdef ZFS_DEBUG
607 		} else {
608 			int i;
609 			ASSERT(dn->dn_next_nblkptr[txgoff] < dnp->dn_nblkptr);
610 			/* the blkptrs we are losing better be unallocated */
611 			for (i = dn->dn_next_nblkptr[txgoff];
612 			    i < dnp->dn_nblkptr; i++)
613 				ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[i]));
614 #endif
615 		}
616 		mutex_enter(&dn->dn_mtx);
617 		dnp->dn_nblkptr = dn->dn_next_nblkptr[txgoff];
618 		dn->dn_next_nblkptr[txgoff] = 0;
619 		mutex_exit(&dn->dn_mtx);
620 	}
621 
622 	if (dn->dn_next_nlevels[txgoff]) {
623 		dnode_increase_indirection(dn, tx);
624 		dn->dn_next_nlevels[txgoff] = 0;
625 	}
626 
627 	dbuf_sync_list(list, tx);
628 
629 	if (dn->dn_object != DMU_META_DNODE_OBJECT) {
630 		ASSERT3P(list_head(list), ==, NULL);
631 		dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg);
632 	}
633 
634 	/*
635 	 * Although we have dropped our reference to the dnode, it
636 	 * can't be evicted until its written, and we haven't yet
637 	 * initiated the IO for the dnode's dbuf.
638 	 */
639 }
640