xref: /illumos-gate/usr/src/uts/common/fs/zfs/dnode_sync.c (revision bf16b11e8deb633dd6c4296d46e92399d1582df4)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21ad135b5dSChristopher Siden 
22fa9e4066Sahrens /*
2306e0070dSMark Shellenbaum  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24*bf16b11eSMatthew Ahrens  * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
25fa9e4066Sahrens  */
26fa9e4066Sahrens 
27fa9e4066Sahrens #include <sys/zfs_context.h>
28fa9e4066Sahrens #include <sys/dbuf.h>
29fa9e4066Sahrens #include <sys/dnode.h>
30fa9e4066Sahrens #include <sys/dmu.h>
31fa9e4066Sahrens #include <sys/dmu_tx.h>
32fa9e4066Sahrens #include <sys/dmu_objset.h>
33fa9e4066Sahrens #include <sys/dsl_dataset.h>
34fa9e4066Sahrens #include <sys/spa.h>
35*bf16b11eSMatthew Ahrens #include <sys/range_tree.h>
3643466aaeSMax Grossman #include <sys/zfeature.h>
37fa9e4066Sahrens 
38fa9e4066Sahrens static void
39fa9e4066Sahrens dnode_increase_indirection(dnode_t *dn, dmu_tx_t *tx)
40fa9e4066Sahrens {
41fa9e4066Sahrens 	dmu_buf_impl_t *db;
42c717a561Smaybee 	int txgoff = tx->tx_txg & TXG_MASK;
43c717a561Smaybee 	int nblkptr = dn->dn_phys->dn_nblkptr;
44c717a561Smaybee 	int old_toplvl = dn->dn_phys->dn_nlevels - 1;
45c717a561Smaybee 	int new_level = dn->dn_next_nlevels[txgoff];
46fa9e4066Sahrens 	int i;
47fa9e4066Sahrens 
48c717a561Smaybee 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
49c717a561Smaybee 
50c717a561Smaybee 	/* this dnode can't be paged out because it's dirty */
51fa9e4066Sahrens 	ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE);
52fa9e4066Sahrens 	ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
53c717a561Smaybee 	ASSERT(new_level > 1 && dn->dn_phys->dn_nlevels > 0);
54fa9e4066Sahrens 
55fa9e4066Sahrens 	db = dbuf_hold_level(dn, dn->dn_phys->dn_nlevels, 0, FTAG);
56ea8dc4b6Seschrock 	ASSERT(db != NULL);
57c717a561Smaybee 
58c717a561Smaybee 	dn->dn_phys->dn_nlevels = new_level;
59f82bfe17Sgw 	dprintf("os=%p obj=%llu, increase to %d\n", dn->dn_objset,
60f82bfe17Sgw 	    dn->dn_object, dn->dn_phys->dn_nlevels);
61c717a561Smaybee 
62c717a561Smaybee 	/* check for existing blkptrs in the dnode */
63c717a561Smaybee 	for (i = 0; i < nblkptr; i++)
64fa9e4066Sahrens 		if (!BP_IS_HOLE(&dn->dn_phys->dn_blkptr[i]))
65fa9e4066Sahrens 			break;
66c717a561Smaybee 	if (i != nblkptr) {
67c717a561Smaybee 		/* transfer dnode's block pointers to new indirect block */
68c717a561Smaybee 		(void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED|DB_RF_HAVESTRUCT);
69c717a561Smaybee 		ASSERT(db->db.db_data);
70c717a561Smaybee 		ASSERT(arc_released(db->db_buf));
71c717a561Smaybee 		ASSERT3U(sizeof (blkptr_t) * nblkptr, <=, db->db.db_size);
72fa9e4066Sahrens 		bcopy(dn->dn_phys->dn_blkptr, db->db.db_data,
73c717a561Smaybee 		    sizeof (blkptr_t) * nblkptr);
746b4acc8bSahrens 		arc_buf_freeze(db->db_buf);
75fa9e4066Sahrens 	}
76fa9e4066Sahrens 
77fa9e4066Sahrens 	/* set dbuf's parent pointers to new indirect buf */
78c717a561Smaybee 	for (i = 0; i < nblkptr; i++) {
79c717a561Smaybee 		dmu_buf_impl_t *child = dbuf_find(dn, old_toplvl, i);
80c717a561Smaybee 
81fa9e4066Sahrens 		if (child == NULL)
82fa9e4066Sahrens 			continue;
83744947dcSTom Erickson #ifdef	DEBUG
84744947dcSTom Erickson 		DB_DNODE_ENTER(child);
85744947dcSTom Erickson 		ASSERT3P(DB_DNODE(child), ==, dn);
86744947dcSTom Erickson 		DB_DNODE_EXIT(child);
87744947dcSTom Erickson #endif	/* DEBUG */
88c717a561Smaybee 		if (child->db_parent && child->db_parent != dn->dn_dbuf) {
89c717a561Smaybee 			ASSERT(child->db_parent->db_level == db->db_level);
90c717a561Smaybee 			ASSERT(child->db_blkptr !=
91c717a561Smaybee 			    &dn->dn_phys->dn_blkptr[child->db_blkid]);
92fa9e4066Sahrens 			mutex_exit(&child->db_mtx);
93fa9e4066Sahrens 			continue;
94fa9e4066Sahrens 		}
95c717a561Smaybee 		ASSERT(child->db_parent == NULL ||
96c717a561Smaybee 		    child->db_parent == dn->dn_dbuf);
97c717a561Smaybee 
98c717a561Smaybee 		child->db_parent = db;
99c717a561Smaybee 		dbuf_add_ref(db, child);
100c717a561Smaybee 		if (db->db.db_data)
101c717a561Smaybee 			child->db_blkptr = (blkptr_t *)db->db.db_data + i;
102c717a561Smaybee 		else
103c717a561Smaybee 			child->db_blkptr = NULL;
104c717a561Smaybee 		dprintf_dbuf_bp(child, child->db_blkptr,
105c717a561Smaybee 		    "changed db_blkptr to new indirect %s", "");
106fa9e4066Sahrens 
107fa9e4066Sahrens 		mutex_exit(&child->db_mtx);
108fa9e4066Sahrens 	}
109fa9e4066Sahrens 
110c717a561Smaybee 	bzero(dn->dn_phys->dn_blkptr, sizeof (blkptr_t) * nblkptr);
111fa9e4066Sahrens 
112ea8dc4b6Seschrock 	dbuf_rele(db, FTAG);
113c717a561Smaybee 
114c717a561Smaybee 	rw_exit(&dn->dn_struct_rwlock);
115fa9e4066Sahrens }
116fa9e4066Sahrens 
11743466aaeSMax Grossman static void
118fa9e4066Sahrens free_blocks(dnode_t *dn, blkptr_t *bp, int num, dmu_tx_t *tx)
119fa9e4066Sahrens {
120cdb0ab79Smaybee 	dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
121fa9e4066Sahrens 	uint64_t bytesfreed = 0;
122fa9e4066Sahrens 
123cdb0ab79Smaybee 	dprintf("ds=%p obj=%llx num=%d\n", ds, dn->dn_object, num);
124fa9e4066Sahrens 
12543466aaeSMax Grossman 	for (int i = 0; i < num; i++, bp++) {
126fa9e4066Sahrens 		if (BP_IS_HOLE(bp))
127fa9e4066Sahrens 			continue;
128fa9e4066Sahrens 
129b24ab676SJeff Bonwick 		bytesfreed += dsl_dataset_block_kill(ds, bp, tx, B_FALSE);
13099653d4eSeschrock 		ASSERT3U(bytesfreed, <=, DN_USED_BYTES(dn->dn_phys));
13143466aaeSMax Grossman 
13243466aaeSMax Grossman 		/*
13343466aaeSMax Grossman 		 * Save some useful information on the holes being
13443466aaeSMax Grossman 		 * punched, including logical size, type, and indirection
13543466aaeSMax Grossman 		 * level. Retaining birth time enables detection of when
13643466aaeSMax Grossman 		 * holes are punched for reducing the number of free
13743466aaeSMax Grossman 		 * records transmitted during a zfs send.
13843466aaeSMax Grossman 		 */
13943466aaeSMax Grossman 
14043466aaeSMax Grossman 		uint64_t lsize = BP_GET_LSIZE(bp);
14143466aaeSMax Grossman 		dmu_object_type_t type = BP_GET_TYPE(bp);
14243466aaeSMax Grossman 		uint64_t lvl = BP_GET_LEVEL(bp);
14343466aaeSMax Grossman 
144c717a561Smaybee 		bzero(bp, sizeof (blkptr_t));
14543466aaeSMax Grossman 
14643466aaeSMax Grossman 		if (spa_feature_is_active(dn->dn_objset->os_spa,
14743466aaeSMax Grossman 		    SPA_FEATURE_HOLE_BIRTH)) {
14843466aaeSMax Grossman 			BP_SET_LSIZE(bp, lsize);
14943466aaeSMax Grossman 			BP_SET_TYPE(bp, type);
15043466aaeSMax Grossman 			BP_SET_LEVEL(bp, lvl);
15143466aaeSMax Grossman 			BP_SET_BIRTH(bp, dmu_tx_get_txg(tx), 0);
15243466aaeSMax Grossman 		}
153fa9e4066Sahrens 	}
154fa9e4066Sahrens 	dnode_diduse_space(dn, -bytesfreed);
155fa9e4066Sahrens }
156fa9e4066Sahrens 
1579c9dc39aSek #ifdef ZFS_DEBUG
158fa9e4066Sahrens static void
159fa9e4066Sahrens free_verify(dmu_buf_impl_t *db, uint64_t start, uint64_t end, dmu_tx_t *tx)
160fa9e4066Sahrens {
161fa9e4066Sahrens 	int off, num;
162fa9e4066Sahrens 	int i, err, epbs;
163fa9e4066Sahrens 	uint64_t txg = tx->tx_txg;
164744947dcSTom Erickson 	dnode_t *dn;
165fa9e4066Sahrens 
166744947dcSTom Erickson 	DB_DNODE_ENTER(db);
167744947dcSTom Erickson 	dn = DB_DNODE(db);
168744947dcSTom Erickson 	epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
169fa9e4066Sahrens 	off = start - (db->db_blkid * 1<<epbs);
170fa9e4066Sahrens 	num = end - start + 1;
171fa9e4066Sahrens 
172fa9e4066Sahrens 	ASSERT3U(off, >=, 0);
173fa9e4066Sahrens 	ASSERT3U(num, >=, 0);
174fa9e4066Sahrens 	ASSERT3U(db->db_level, >, 0);
175744947dcSTom Erickson 	ASSERT3U(db->db.db_size, ==, 1 << dn->dn_phys->dn_indblkshift);
176fa9e4066Sahrens 	ASSERT3U(off+num, <=, db->db.db_size >> SPA_BLKPTRSHIFT);
177fa9e4066Sahrens 	ASSERT(db->db_blkptr != NULL);
178fa9e4066Sahrens 
179fa9e4066Sahrens 	for (i = off; i < off+num; i++) {
180fa9e4066Sahrens 		uint64_t *buf;
181fa9e4066Sahrens 		dmu_buf_impl_t *child;
182c717a561Smaybee 		dbuf_dirty_record_t *dr;
183c717a561Smaybee 		int j;
184fa9e4066Sahrens 
185fa9e4066Sahrens 		ASSERT(db->db_level == 1);
186fa9e4066Sahrens 
187744947dcSTom Erickson 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
188744947dcSTom Erickson 		err = dbuf_hold_impl(dn, db->db_level-1,
189f82bfe17Sgw 		    (db->db_blkid << epbs) + i, TRUE, FTAG, &child);
190744947dcSTom Erickson 		rw_exit(&dn->dn_struct_rwlock);
191fa9e4066Sahrens 		if (err == ENOENT)
192fa9e4066Sahrens 			continue;
193fa9e4066Sahrens 		ASSERT(err == 0);
194fa9e4066Sahrens 		ASSERT(child->db_level == 0);
195c717a561Smaybee 		dr = child->db_last_dirty;
196c717a561Smaybee 		while (dr && dr->dr_txg > txg)
197c717a561Smaybee 			dr = dr->dr_next;
198c717a561Smaybee 		ASSERT(dr == NULL || dr->dr_txg == txg);
199c717a561Smaybee 
200c717a561Smaybee 		/* data_old better be zeroed */
201c717a561Smaybee 		if (dr) {
202c717a561Smaybee 			buf = dr->dt.dl.dr_data->b_data;
203fa9e4066Sahrens 			for (j = 0; j < child->db.db_size >> 3; j++) {
204fa9e4066Sahrens 				if (buf[j] != 0) {
205fa9e4066Sahrens 					panic("freed data not zero: "
206fa9e4066Sahrens 					    "child=%p i=%d off=%d num=%d\n",
207903a11ebSrh 					    (void *)child, i, off, num);
208fa9e4066Sahrens 				}
209fa9e4066Sahrens 			}
210fa9e4066Sahrens 		}
211fa9e4066Sahrens 
212fa9e4066Sahrens 		/*
213fa9e4066Sahrens 		 * db_data better be zeroed unless it's dirty in a
214fa9e4066Sahrens 		 * future txg.
215fa9e4066Sahrens 		 */
216fa9e4066Sahrens 		mutex_enter(&child->db_mtx);
217fa9e4066Sahrens 		buf = child->db.db_data;
218fa9e4066Sahrens 		if (buf != NULL && child->db_state != DB_FILL &&
219c717a561Smaybee 		    child->db_last_dirty == NULL) {
220fa9e4066Sahrens 			for (j = 0; j < child->db.db_size >> 3; j++) {
221fa9e4066Sahrens 				if (buf[j] != 0) {
222fa9e4066Sahrens 					panic("freed data not zero: "
223fa9e4066Sahrens 					    "child=%p i=%d off=%d num=%d\n",
224903a11ebSrh 					    (void *)child, i, off, num);
225fa9e4066Sahrens 				}
226fa9e4066Sahrens 			}
227fa9e4066Sahrens 		}
228fa9e4066Sahrens 		mutex_exit(&child->db_mtx);
229fa9e4066Sahrens 
230ea8dc4b6Seschrock 		dbuf_rele(child, FTAG);
231fa9e4066Sahrens 	}
232744947dcSTom Erickson 	DB_DNODE_EXIT(db);
233fa9e4066Sahrens }
2349c9dc39aSek #endif
235fa9e4066Sahrens 
236cdb0ab79Smaybee #define	ALL -1
237cdb0ab79Smaybee 
23843466aaeSMax Grossman static void
23943466aaeSMax Grossman free_children(dmu_buf_impl_t *db, uint64_t blkid, uint64_t nblks,
240fa9e4066Sahrens     dmu_tx_t *tx)
241fa9e4066Sahrens {
242744947dcSTom Erickson 	dnode_t *dn;
243fa9e4066Sahrens 	blkptr_t *bp;
244fa9e4066Sahrens 	dmu_buf_impl_t *subdb;
245fa9e4066Sahrens 	uint64_t start, end, dbstart, dbend, i;
24643466aaeSMax Grossman 	int epbs, shift;
247cdb0ab79Smaybee 
248cdb0ab79Smaybee 	/*
249cdb0ab79Smaybee 	 * There is a small possibility that this block will not be cached:
250cdb0ab79Smaybee 	 *   1 - if level > 1 and there are no children with level <= 1
25143466aaeSMax Grossman 	 *   2 - if this block was evicted since we read it from
25243466aaeSMax Grossman 	 *	 dmu_tx_hold_free().
253cdb0ab79Smaybee 	 */
254cdb0ab79Smaybee 	if (db->db_state != DB_CACHED)
255cdb0ab79Smaybee 		(void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
256fa9e4066Sahrens 
2573f9d6ad7SLin Ling 	dbuf_release_bp(db);
25843466aaeSMax Grossman 	bp = db->db.db_data;
259fa9e4066Sahrens 
260744947dcSTom Erickson 	DB_DNODE_ENTER(db);
261744947dcSTom Erickson 	dn = DB_DNODE(db);
262744947dcSTom Erickson 	epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
263fa9e4066Sahrens 	shift = (db->db_level - 1) * epbs;
264fa9e4066Sahrens 	dbstart = db->db_blkid << epbs;
265fa9e4066Sahrens 	start = blkid >> shift;
266fa9e4066Sahrens 	if (dbstart < start) {
267fa9e4066Sahrens 		bp += start - dbstart;
268fa9e4066Sahrens 	} else {
269fa9e4066Sahrens 		start = dbstart;
270fa9e4066Sahrens 	}
271fa9e4066Sahrens 	dbend = ((db->db_blkid + 1) << epbs) - 1;
272fa9e4066Sahrens 	end = (blkid + nblks - 1) >> shift;
273fa9e4066Sahrens 	if (dbend <= end)
274fa9e4066Sahrens 		end = dbend;
27543466aaeSMax Grossman 
276fa9e4066Sahrens 	ASSERT3U(start, <=, end);
277fa9e4066Sahrens 
278fa9e4066Sahrens 	if (db->db_level == 1) {
2799c9dc39aSek 		FREE_VERIFY(db, start, end, tx);
28043466aaeSMax Grossman 		free_blocks(dn, bp, end-start+1, tx);
28143466aaeSMax Grossman 	} else {
28243466aaeSMax Grossman 		for (i = start; i <= end; i++, bp++) {
28343466aaeSMax Grossman 			if (BP_IS_HOLE(bp))
28443466aaeSMax Grossman 				continue;
28543466aaeSMax Grossman 			rw_enter(&dn->dn_struct_rwlock, RW_READER);
28643466aaeSMax Grossman 			VERIFY0(dbuf_hold_impl(dn, db->db_level - 1,
28743466aaeSMax Grossman 			    i, B_TRUE, FTAG, &subdb));
28843466aaeSMax Grossman 			rw_exit(&dn->dn_struct_rwlock);
28943466aaeSMax Grossman 			ASSERT3P(bp, ==, subdb->db_blkptr);
29043466aaeSMax Grossman 
29143466aaeSMax Grossman 			free_children(subdb, blkid, nblks, tx);
29243466aaeSMax Grossman 			dbuf_rele(subdb, FTAG);
29343466aaeSMax Grossman 		}
294fa9e4066Sahrens 	}
295fa9e4066Sahrens 
29643466aaeSMax Grossman 	/* If this whole block is free, free ourself too. */
29743466aaeSMax Grossman 	for (i = 0, bp = db->db.db_data; i < 1 << epbs; i++, bp++) {
29843466aaeSMax Grossman 		if (!BP_IS_HOLE(bp))
29943466aaeSMax Grossman 			break;
30043466aaeSMax Grossman 	}
30143466aaeSMax Grossman 	if (i == 1 << epbs) {
30243466aaeSMax Grossman 		/* didn't find any non-holes */
30343466aaeSMax Grossman 		bzero(db->db.db_data, db->db.db_size);
30443466aaeSMax Grossman 		free_blocks(dn, db->db_blkptr, 1, tx);
30543466aaeSMax Grossman 	} else {
30643466aaeSMax Grossman 		/*
30743466aaeSMax Grossman 		 * Partial block free; must be marked dirty so that it
30843466aaeSMax Grossman 		 * will be written out.
30943466aaeSMax Grossman 		 */
31043466aaeSMax Grossman 		ASSERT(db->db_dirtycnt > 0);
311fa9e4066Sahrens 	}
31243466aaeSMax Grossman 
313744947dcSTom Erickson 	DB_DNODE_EXIT(db);
3146b4acc8bSahrens 	arc_buf_freeze(db->db_buf);
315fa9e4066Sahrens }
316fa9e4066Sahrens 
317fa9e4066Sahrens /*
318f7170741SWill Andrews  * Traverse the indicated range of the provided file
319fa9e4066Sahrens  * and "free" all the blocks contained there.
320fa9e4066Sahrens  */
321fa9e4066Sahrens static void
322*bf16b11eSMatthew Ahrens dnode_sync_free_range_impl(dnode_t *dn, uint64_t blkid, uint64_t nblks,
32343466aaeSMax Grossman     dmu_tx_t *tx)
324fa9e4066Sahrens {
325fa9e4066Sahrens 	blkptr_t *bp = dn->dn_phys->dn_blkptr;
326fa9e4066Sahrens 	int dnlevel = dn->dn_phys->dn_nlevels;
32743466aaeSMax Grossman 	boolean_t trunc = B_FALSE;
328fa9e4066Sahrens 
329fa9e4066Sahrens 	if (blkid > dn->dn_phys->dn_maxblkid)
330fa9e4066Sahrens 		return;
331fa9e4066Sahrens 
332fa9e4066Sahrens 	ASSERT(dn->dn_phys->dn_maxblkid < UINT64_MAX);
33343466aaeSMax Grossman 	if (blkid + nblks > dn->dn_phys->dn_maxblkid) {
334fa9e4066Sahrens 		nblks = dn->dn_phys->dn_maxblkid - blkid + 1;
33543466aaeSMax Grossman 		trunc = B_TRUE;
33643466aaeSMax Grossman 	}
337fa9e4066Sahrens 
338fa9e4066Sahrens 	/* There are no indirect blocks in the object */
339fa9e4066Sahrens 	if (dnlevel == 1) {
340fa9e4066Sahrens 		if (blkid >= dn->dn_phys->dn_nblkptr) {
341fa9e4066Sahrens 			/* this range was never made persistent */
342fa9e4066Sahrens 			return;
343fa9e4066Sahrens 		}
344fa9e4066Sahrens 		ASSERT3U(blkid + nblks, <=, dn->dn_phys->dn_nblkptr);
34543466aaeSMax Grossman 		free_blocks(dn, bp + blkid, nblks, tx);
34643466aaeSMax Grossman 	} else {
34743466aaeSMax Grossman 		int shift = (dnlevel - 1) *
34843466aaeSMax Grossman 		    (dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT);
34943466aaeSMax Grossman 		int start = blkid >> shift;
35043466aaeSMax Grossman 		int end = (blkid + nblks - 1) >> shift;
35143466aaeSMax Grossman 		dmu_buf_impl_t *db;
35243466aaeSMax Grossman 
35343466aaeSMax Grossman 		ASSERT(start < dn->dn_phys->dn_nblkptr);
35443466aaeSMax Grossman 		bp += start;
35543466aaeSMax Grossman 		for (int i = start; i <= end; i++, bp++) {
35643466aaeSMax Grossman 			if (BP_IS_HOLE(bp))
35743466aaeSMax Grossman 				continue;
35843466aaeSMax Grossman 			rw_enter(&dn->dn_struct_rwlock, RW_READER);
35943466aaeSMax Grossman 			VERIFY0(dbuf_hold_impl(dn, dnlevel - 1, i,
36043466aaeSMax Grossman 			    TRUE, FTAG, &db));
36143466aaeSMax Grossman 			rw_exit(&dn->dn_struct_rwlock);
36243466aaeSMax Grossman 
36343466aaeSMax Grossman 			free_children(db, blkid, nblks, tx);
36443466aaeSMax Grossman 			dbuf_rele(db, FTAG);
365fa9e4066Sahrens 
366fa9e4066Sahrens 		}
367fa9e4066Sahrens 	}
36843466aaeSMax Grossman 
369fa9e4066Sahrens 	if (trunc) {
37043466aaeSMax Grossman 		dn->dn_phys->dn_maxblkid = blkid == 0 ? 0 : blkid - 1;
37143466aaeSMax Grossman 
372fa9e4066Sahrens 		uint64_t off = (dn->dn_phys->dn_maxblkid + 1) *
373fa9e4066Sahrens 		    (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT);
374fa9e4066Sahrens 		ASSERT(off < dn->dn_phys->dn_maxblkid ||
375fa9e4066Sahrens 		    dn->dn_phys->dn_maxblkid == 0 ||
376cdb0ab79Smaybee 		    dnode_next_offset(dn, 0, &off, 1, 1, 0) != 0);
377fa9e4066Sahrens 	}
378fa9e4066Sahrens }
379fa9e4066Sahrens 
380*bf16b11eSMatthew Ahrens typedef struct dnode_sync_free_range_arg {
381*bf16b11eSMatthew Ahrens 	dnode_t *dsfra_dnode;
382*bf16b11eSMatthew Ahrens 	dmu_tx_t *dsfra_tx;
383*bf16b11eSMatthew Ahrens } dnode_sync_free_range_arg_t;
384*bf16b11eSMatthew Ahrens 
385*bf16b11eSMatthew Ahrens static void
386*bf16b11eSMatthew Ahrens dnode_sync_free_range(void *arg, uint64_t blkid, uint64_t nblks)
387*bf16b11eSMatthew Ahrens {
388*bf16b11eSMatthew Ahrens 	dnode_sync_free_range_arg_t *dsfra = arg;
389*bf16b11eSMatthew Ahrens 	dnode_t *dn = dsfra->dsfra_dnode;
390*bf16b11eSMatthew Ahrens 
391*bf16b11eSMatthew Ahrens 	mutex_exit(&dn->dn_mtx);
392*bf16b11eSMatthew Ahrens 	dnode_sync_free_range_impl(dn, blkid, nblks, dsfra->dsfra_tx);
393*bf16b11eSMatthew Ahrens 	mutex_enter(&dn->dn_mtx);
394*bf16b11eSMatthew Ahrens }
395*bf16b11eSMatthew Ahrens 
396ea8dc4b6Seschrock /*
397f7170741SWill Andrews  * Try to kick all the dnode's dbufs out of the cache...
398ea8dc4b6Seschrock  */
3991934e92fSmaybee void
4001934e92fSmaybee dnode_evict_dbufs(dnode_t *dn)
401ea8dc4b6Seschrock {
402c543ec06Sahrens 	int progress;
403c543ec06Sahrens 	int pass = 0;
404c543ec06Sahrens 
405c543ec06Sahrens 	do {
406d90a49d6Smaybee 		dmu_buf_impl_t *db, marker;
407c543ec06Sahrens 		int evicting = FALSE;
408c543ec06Sahrens 
409c543ec06Sahrens 		progress = FALSE;
410c543ec06Sahrens 		mutex_enter(&dn->dn_dbufs_mtx);
411d90a49d6Smaybee 		list_insert_tail(&dn->dn_dbufs, &marker);
412d90a49d6Smaybee 		db = list_head(&dn->dn_dbufs);
413d90a49d6Smaybee 		for (; db != &marker; db = list_head(&dn->dn_dbufs)) {
414d90a49d6Smaybee 			list_remove(&dn->dn_dbufs, db);
415d90a49d6Smaybee 			list_insert_tail(&dn->dn_dbufs, db);
416744947dcSTom Erickson #ifdef	DEBUG
417744947dcSTom Erickson 			DB_DNODE_ENTER(db);
418744947dcSTom Erickson 			ASSERT3P(DB_DNODE(db), ==, dn);
419744947dcSTom Erickson 			DB_DNODE_EXIT(db);
420744947dcSTom Erickson #endif	/* DEBUG */
421ea8dc4b6Seschrock 
422ea8dc4b6Seschrock 			mutex_enter(&db->db_mtx);
423c543ec06Sahrens 			if (db->db_state == DB_EVICTING) {
424c543ec06Sahrens 				progress = TRUE;
425c543ec06Sahrens 				evicting = TRUE;
426c543ec06Sahrens 				mutex_exit(&db->db_mtx);
427c543ec06Sahrens 			} else if (refcount_is_zero(&db->db_holds)) {
428c543ec06Sahrens 				progress = TRUE;
429c543ec06Sahrens 				dbuf_clear(db); /* exits db_mtx for us */
430c543ec06Sahrens 			} else {
431c543ec06Sahrens 				mutex_exit(&db->db_mtx);
432c543ec06Sahrens 			}
433c543ec06Sahrens 
434ea8dc4b6Seschrock 		}
435d90a49d6Smaybee 		list_remove(&dn->dn_dbufs, &marker);
436c543ec06Sahrens 		/*
437c543ec06Sahrens 		 * NB: we need to drop dn_dbufs_mtx between passes so
438c543ec06Sahrens 		 * that any DB_EVICTING dbufs can make progress.
439c543ec06Sahrens 		 * Ideally, we would have some cv we could wait on, but
440c543ec06Sahrens 		 * since we don't, just wait a bit to give the other
441c543ec06Sahrens 		 * thread a chance to run.
442c543ec06Sahrens 		 */
443c543ec06Sahrens 		mutex_exit(&dn->dn_dbufs_mtx);
444c543ec06Sahrens 		if (evicting)
445c543ec06Sahrens 			delay(1);
446c543ec06Sahrens 		pass++;
447c543ec06Sahrens 		ASSERT(pass < 100); /* sanity check */
448c543ec06Sahrens 	} while (progress);
449c543ec06Sahrens 
450ea8dc4b6Seschrock 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
451ea8dc4b6Seschrock 	if (dn->dn_bonus && refcount_is_zero(&dn->dn_bonus->db_holds)) {
452ea8dc4b6Seschrock 		mutex_enter(&dn->dn_bonus->db_mtx);
453ea8dc4b6Seschrock 		dbuf_evict(dn->dn_bonus);
454ea8dc4b6Seschrock 		dn->dn_bonus = NULL;
455ea8dc4b6Seschrock 	}
456ea8dc4b6Seschrock 	rw_exit(&dn->dn_struct_rwlock);
457ea8dc4b6Seschrock }
458ea8dc4b6Seschrock 
459c717a561Smaybee static void
460c717a561Smaybee dnode_undirty_dbufs(list_t *list)
461fa9e4066Sahrens {
462c717a561Smaybee 	dbuf_dirty_record_t *dr;
463fa9e4066Sahrens 
464c717a561Smaybee 	while (dr = list_head(list)) {
465c717a561Smaybee 		dmu_buf_impl_t *db = dr->dr_dbuf;
466c717a561Smaybee 		uint64_t txg = dr->dr_txg;
467fa9e4066Sahrens 
468b24ab676SJeff Bonwick 		if (db->db_level != 0)
469b24ab676SJeff Bonwick 			dnode_undirty_dbufs(&dr->dt.di.dr_children);
470b24ab676SJeff Bonwick 
471fa9e4066Sahrens 		mutex_enter(&db->db_mtx);
472fa9e4066Sahrens 		/* XXX - use dbuf_undirty()? */
473c717a561Smaybee 		list_remove(list, dr);
474c717a561Smaybee 		ASSERT(db->db_last_dirty == dr);
475c717a561Smaybee 		db->db_last_dirty = NULL;
476c717a561Smaybee 		db->db_dirtycnt -= 1;
477fa9e4066Sahrens 		if (db->db_level == 0) {
4780a586ceaSMark Shellenbaum 			ASSERT(db->db_blkid == DMU_BONUS_BLKID ||
479c717a561Smaybee 			    dr->dt.dl.dr_data == db->db_buf);
480c717a561Smaybee 			dbuf_unoverride(dr);
481fa9e4066Sahrens 		}
482c717a561Smaybee 		kmem_free(dr, sizeof (dbuf_dirty_record_t));
483b24ab676SJeff Bonwick 		dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg);
484fa9e4066Sahrens 	}
485c717a561Smaybee }
486fa9e4066Sahrens 
487c717a561Smaybee static void
488c717a561Smaybee dnode_sync_free(dnode_t *dn, dmu_tx_t *tx)
489c717a561Smaybee {
490c717a561Smaybee 	int txgoff = tx->tx_txg & TXG_MASK;
491c717a561Smaybee 
492c717a561Smaybee 	ASSERT(dmu_tx_is_syncing(tx));
493c717a561Smaybee 
494cdb0ab79Smaybee 	/*
495cdb0ab79Smaybee 	 * Our contents should have been freed in dnode_sync() by the
496cdb0ab79Smaybee 	 * free range record inserted by the caller of dnode_free().
497cdb0ab79Smaybee 	 */
498fb09f5aaSMadhav Suresh 	ASSERT0(DN_USED_BYTES(dn->dn_phys));
499cdb0ab79Smaybee 	ASSERT(BP_IS_HOLE(dn->dn_phys->dn_blkptr));
500cdb0ab79Smaybee 
501c717a561Smaybee 	dnode_undirty_dbufs(&dn->dn_dirty_records[txgoff]);
5021934e92fSmaybee 	dnode_evict_dbufs(dn);
503ea8dc4b6Seschrock 	ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL);
5043b2aab18SMatthew Ahrens 	ASSERT3P(dn->dn_bonus, ==, NULL);
505ea8dc4b6Seschrock 
506ea8dc4b6Seschrock 	/*
507ea8dc4b6Seschrock 	 * XXX - It would be nice to assert this, but we may still
508ea8dc4b6Seschrock 	 * have residual holds from async evictions from the arc...
509ea8dc4b6Seschrock 	 *
51055434c77Sek 	 * zfs_obj_to_path() also depends on this being
51155434c77Sek 	 * commented out.
51255434c77Sek 	 *
513ea8dc4b6Seschrock 	 * ASSERT3U(refcount_count(&dn->dn_holds), ==, 1);
514ea8dc4b6Seschrock 	 */
515fa9e4066Sahrens 
516fa9e4066Sahrens 	/* Undirty next bits */
517fa9e4066Sahrens 	dn->dn_next_nlevels[txgoff] = 0;
518fa9e4066Sahrens 	dn->dn_next_indblkshift[txgoff] = 0;
519c543ec06Sahrens 	dn->dn_next_blksz[txgoff] = 0;
520fa9e4066Sahrens 
521fa9e4066Sahrens 	/* ASSERT(blkptrs are zero); */
522fa9e4066Sahrens 	ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE);
523fa9e4066Sahrens 	ASSERT(dn->dn_type != DMU_OT_NONE);
524fa9e4066Sahrens 
525fa9e4066Sahrens 	ASSERT(dn->dn_free_txg > 0);
526fa9e4066Sahrens 	if (dn->dn_allocated_txg != dn->dn_free_txg)
52743466aaeSMax Grossman 		dmu_buf_will_dirty(&dn->dn_dbuf->db, tx);
528fa9e4066Sahrens 	bzero(dn->dn_phys, sizeof (dnode_phys_t));
529fa9e4066Sahrens 
530fa9e4066Sahrens 	mutex_enter(&dn->dn_mtx);
531fa9e4066Sahrens 	dn->dn_type = DMU_OT_NONE;
532fa9e4066Sahrens 	dn->dn_maxblkid = 0;
533fa9e4066Sahrens 	dn->dn_allocated_txg = 0;
534758f6e0bSgw 	dn->dn_free_txg = 0;
5350a586ceaSMark Shellenbaum 	dn->dn_have_spill = B_FALSE;
536fa9e4066Sahrens 	mutex_exit(&dn->dn_mtx);
537fa9e4066Sahrens 
538ea8dc4b6Seschrock 	ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
539fa9e4066Sahrens 
540fa9e4066Sahrens 	dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg);
541fa9e4066Sahrens 	/*
542fa9e4066Sahrens 	 * Now that we've released our hold, the dnode may
543fa9e4066Sahrens 	 * be evicted, so we musn't access it.
544fa9e4066Sahrens 	 */
545fa9e4066Sahrens }
546fa9e4066Sahrens 
547fa9e4066Sahrens /*
548c717a561Smaybee  * Write out the dnode's dirty buffers.
549fa9e4066Sahrens  */
550c717a561Smaybee void
551c717a561Smaybee dnode_sync(dnode_t *dn, dmu_tx_t *tx)
552fa9e4066Sahrens {
553fa9e4066Sahrens 	dnode_phys_t *dnp = dn->dn_phys;
554c717a561Smaybee 	int txgoff = tx->tx_txg & TXG_MASK;
555c717a561Smaybee 	list_t *list = &dn->dn_dirty_records[txgoff];
55614843421SMatthew Ahrens 	static const dnode_phys_t zerodn = { 0 };
5570a586ceaSMark Shellenbaum 	boolean_t kill_spill = B_FALSE;
558fa9e4066Sahrens 
559fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
560fa9e4066Sahrens 	ASSERT(dnp->dn_type != DMU_OT_NONE || dn->dn_allocated_txg);
56114843421SMatthew Ahrens 	ASSERT(dnp->dn_type != DMU_OT_NONE ||
56214843421SMatthew Ahrens 	    bcmp(dnp, &zerodn, DNODE_SIZE) == 0);
5639c9dc39aSek 	DNODE_VERIFY(dn);
564c543ec06Sahrens 
565c717a561Smaybee 	ASSERT(dn->dn_dbuf == NULL || arc_released(dn->dn_dbuf->db_buf));
566fa9e4066Sahrens 
56714843421SMatthew Ahrens 	if (dmu_objset_userused_enabled(dn->dn_objset) &&
56814843421SMatthew Ahrens 	    !DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
5690a586ceaSMark Shellenbaum 		mutex_enter(&dn->dn_mtx);
5700a586ceaSMark Shellenbaum 		dn->dn_oldused = DN_USED_BYTES(dn->dn_phys);
5710a586ceaSMark Shellenbaum 		dn->dn_oldflags = dn->dn_phys->dn_flags;
57214843421SMatthew Ahrens 		dn->dn_phys->dn_flags |= DNODE_FLAG_USERUSED_ACCOUNTED;
5730a586ceaSMark Shellenbaum 		mutex_exit(&dn->dn_mtx);
57406e0070dSMark Shellenbaum 		dmu_objset_userquota_get_ids(dn, B_FALSE, tx);
57514843421SMatthew Ahrens 	} else {
57614843421SMatthew Ahrens 		/* Once we account for it, we should always account for it. */
57714843421SMatthew Ahrens 		ASSERT(!(dn->dn_phys->dn_flags &
57814843421SMatthew Ahrens 		    DNODE_FLAG_USERUSED_ACCOUNTED));
57914843421SMatthew Ahrens 	}
58014843421SMatthew Ahrens 
581fa9e4066Sahrens 	mutex_enter(&dn->dn_mtx);
582fa9e4066Sahrens 	if (dn->dn_allocated_txg == tx->tx_txg) {
583fa9e4066Sahrens 		/* The dnode is newly allocated or reallocated */
584fa9e4066Sahrens 		if (dnp->dn_type == DMU_OT_NONE) {
585fa9e4066Sahrens 			/* this is a first alloc, not a realloc */
586fa9e4066Sahrens 			dnp->dn_nlevels = 1;
587da03de99SMark Maybee 			dnp->dn_nblkptr = dn->dn_nblkptr;
588fa9e4066Sahrens 		}
589fa9e4066Sahrens 
590fa9e4066Sahrens 		dnp->dn_type = dn->dn_type;
591fa9e4066Sahrens 		dnp->dn_bonustype = dn->dn_bonustype;
592fa9e4066Sahrens 		dnp->dn_bonuslen = dn->dn_bonuslen;
593fa9e4066Sahrens 	}
594fa9e4066Sahrens 
595c717a561Smaybee 	ASSERT(dnp->dn_nlevels > 1 ||
596f676ed34Sahrens 	    BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
597f676ed34Sahrens 	    BP_GET_LSIZE(&dnp->dn_blkptr[0]) ==
598f676ed34Sahrens 	    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
599f676ed34Sahrens 
6002acef22dSMatthew Ahrens 	if (dn->dn_next_type[txgoff] != 0) {
6012acef22dSMatthew Ahrens 		dnp->dn_type = dn->dn_type;
6022acef22dSMatthew Ahrens 		dn->dn_next_type[txgoff] = 0;
6032acef22dSMatthew Ahrens 	}
6042acef22dSMatthew Ahrens 
6052acef22dSMatthew Ahrens 	if (dn->dn_next_blksz[txgoff] != 0) {
606c543ec06Sahrens 		ASSERT(P2PHASE(dn->dn_next_blksz[txgoff],
607fa9e4066Sahrens 		    SPA_MINBLOCKSIZE) == 0);
608f676ed34Sahrens 		ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
609cdb0ab79Smaybee 		    dn->dn_maxblkid == 0 || list_head(list) != NULL ||
610347a31bcSahrens 		    dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT ==
611*bf16b11eSMatthew Ahrens 		    dnp->dn_datablkszsec ||
612*bf16b11eSMatthew Ahrens 		    range_tree_space(dn->dn_free_ranges[txgoff]) != 0);
613fa9e4066Sahrens 		dnp->dn_datablkszsec =
614c543ec06Sahrens 		    dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT;
615c543ec06Sahrens 		dn->dn_next_blksz[txgoff] = 0;
616fa9e4066Sahrens 	}
617fa9e4066Sahrens 
6182acef22dSMatthew Ahrens 	if (dn->dn_next_bonuslen[txgoff] != 0) {
6191934e92fSmaybee 		if (dn->dn_next_bonuslen[txgoff] == DN_ZERO_BONUSLEN)
6201934e92fSmaybee 			dnp->dn_bonuslen = 0;
6211934e92fSmaybee 		else
6221934e92fSmaybee 			dnp->dn_bonuslen = dn->dn_next_bonuslen[txgoff];
6231934e92fSmaybee 		ASSERT(dnp->dn_bonuslen <= DN_MAX_BONUSLEN);
6241934e92fSmaybee 		dn->dn_next_bonuslen[txgoff] = 0;
6251934e92fSmaybee 	}
6261934e92fSmaybee 
6272acef22dSMatthew Ahrens 	if (dn->dn_next_bonustype[txgoff] != 0) {
628ad135b5dSChristopher Siden 		ASSERT(DMU_OT_IS_VALID(dn->dn_next_bonustype[txgoff]));
6290a586ceaSMark Shellenbaum 		dnp->dn_bonustype = dn->dn_next_bonustype[txgoff];
6300a586ceaSMark Shellenbaum 		dn->dn_next_bonustype[txgoff] = 0;
6310a586ceaSMark Shellenbaum 	}
6320a586ceaSMark Shellenbaum 
63343466aaeSMax Grossman 	boolean_t freeing_dnode = dn->dn_free_txg > 0 &&
63443466aaeSMax Grossman 	    dn->dn_free_txg <= tx->tx_txg;
63543466aaeSMax Grossman 
6360a586ceaSMark Shellenbaum 	/*
6370a586ceaSMark Shellenbaum 	 * We will either remove a spill block when a file is being removed
6380a586ceaSMark Shellenbaum 	 * or we have been asked to remove it.
6390a586ceaSMark Shellenbaum 	 */
6400a586ceaSMark Shellenbaum 	if (dn->dn_rm_spillblk[txgoff] ||
64143466aaeSMax Grossman 	    ((dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) && freeing_dnode)) {
6420a586ceaSMark Shellenbaum 		if ((dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR))
6430a586ceaSMark Shellenbaum 			kill_spill = B_TRUE;
6440a586ceaSMark Shellenbaum 		dn->dn_rm_spillblk[txgoff] = 0;
6450a586ceaSMark Shellenbaum 	}
6460a586ceaSMark Shellenbaum 
6472acef22dSMatthew Ahrens 	if (dn->dn_next_indblkshift[txgoff] != 0) {
648fa9e4066Sahrens 		ASSERT(dnp->dn_nlevels == 1);
649fa9e4066Sahrens 		dnp->dn_indblkshift = dn->dn_next_indblkshift[txgoff];
650fa9e4066Sahrens 		dn->dn_next_indblkshift[txgoff] = 0;
651fa9e4066Sahrens 	}
652fa9e4066Sahrens 
653fa9e4066Sahrens 	/*
654fa9e4066Sahrens 	 * Just take the live (open-context) values for checksum and compress.
655fa9e4066Sahrens 	 * Strictly speaking it's a future leak, but nothing bad happens if we
656fa9e4066Sahrens 	 * start using the new checksum or compress algorithm a little early.
657fa9e4066Sahrens 	 */
658fa9e4066Sahrens 	dnp->dn_checksum = dn->dn_checksum;
659fa9e4066Sahrens 	dnp->dn_compress = dn->dn_compress;
660fa9e4066Sahrens 
661fa9e4066Sahrens 	mutex_exit(&dn->dn_mtx);
662fa9e4066Sahrens 
6630a586ceaSMark Shellenbaum 	if (kill_spill) {
66443466aaeSMax Grossman 		free_blocks(dn, &dn->dn_phys->dn_spill, 1, tx);
6650a586ceaSMark Shellenbaum 		mutex_enter(&dn->dn_mtx);
6660a586ceaSMark Shellenbaum 		dnp->dn_flags &= ~DNODE_FLAG_SPILL_BLKPTR;
6670a586ceaSMark Shellenbaum 		mutex_exit(&dn->dn_mtx);
6680a586ceaSMark Shellenbaum 	}
6690a586ceaSMark Shellenbaum 
670fa9e4066Sahrens 	/* process all the "freed" ranges in the file */
671*bf16b11eSMatthew Ahrens 	if (dn->dn_free_ranges[txgoff] != NULL) {
672*bf16b11eSMatthew Ahrens 		dnode_sync_free_range_arg_t dsfra;
673*bf16b11eSMatthew Ahrens 		dsfra.dsfra_dnode = dn;
674*bf16b11eSMatthew Ahrens 		dsfra.dsfra_tx = tx;
675cdb0ab79Smaybee 		mutex_enter(&dn->dn_mtx);
676*bf16b11eSMatthew Ahrens 		range_tree_vacate(dn->dn_free_ranges[txgoff],
677*bf16b11eSMatthew Ahrens 		    dnode_sync_free_range, &dsfra);
678*bf16b11eSMatthew Ahrens 		range_tree_destroy(dn->dn_free_ranges[txgoff]);
679*bf16b11eSMatthew Ahrens 		dn->dn_free_ranges[txgoff] = NULL;
680cdb0ab79Smaybee 		mutex_exit(&dn->dn_mtx);
681fa9e4066Sahrens 	}
6821934e92fSmaybee 
68343466aaeSMax Grossman 	if (freeing_dnode) {
684c717a561Smaybee 		dnode_sync_free(dn, tx);
685c717a561Smaybee 		return;
686fa9e4066Sahrens 	}
687fa9e4066Sahrens 
688da03de99SMark Maybee 	if (dn->dn_next_nblkptr[txgoff]) {
689da03de99SMark Maybee 		/* this should only happen on a realloc */
690da03de99SMark Maybee 		ASSERT(dn->dn_allocated_txg == tx->tx_txg);
691da03de99SMark Maybee 		if (dn->dn_next_nblkptr[txgoff] > dnp->dn_nblkptr) {
692da03de99SMark Maybee 			/* zero the new blkptrs we are gaining */
693da03de99SMark Maybee 			bzero(dnp->dn_blkptr + dnp->dn_nblkptr,
694da03de99SMark Maybee 			    sizeof (blkptr_t) *
695da03de99SMark Maybee 			    (dn->dn_next_nblkptr[txgoff] - dnp->dn_nblkptr));
696da03de99SMark Maybee #ifdef ZFS_DEBUG
697da03de99SMark Maybee 		} else {
698da03de99SMark Maybee 			int i;
699da03de99SMark Maybee 			ASSERT(dn->dn_next_nblkptr[txgoff] < dnp->dn_nblkptr);
700da03de99SMark Maybee 			/* the blkptrs we are losing better be unallocated */
701da03de99SMark Maybee 			for (i = dn->dn_next_nblkptr[txgoff];
702da03de99SMark Maybee 			    i < dnp->dn_nblkptr; i++)
703da03de99SMark Maybee 				ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[i]));
704da03de99SMark Maybee #endif
705da03de99SMark Maybee 		}
706da03de99SMark Maybee 		mutex_enter(&dn->dn_mtx);
707da03de99SMark Maybee 		dnp->dn_nblkptr = dn->dn_next_nblkptr[txgoff];
708da03de99SMark Maybee 		dn->dn_next_nblkptr[txgoff] = 0;
709da03de99SMark Maybee 		mutex_exit(&dn->dn_mtx);
710da03de99SMark Maybee 	}
711da03de99SMark Maybee 
712fa9e4066Sahrens 	if (dn->dn_next_nlevels[txgoff]) {
713c717a561Smaybee 		dnode_increase_indirection(dn, tx);
714fa9e4066Sahrens 		dn->dn_next_nlevels[txgoff] = 0;
715fa9e4066Sahrens 	}
716fa9e4066Sahrens 
717c717a561Smaybee 	dbuf_sync_list(list, tx);
718fa9e4066Sahrens 
71914843421SMatthew Ahrens 	if (!DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
720c717a561Smaybee 		ASSERT3P(list_head(list), ==, NULL);
721c717a561Smaybee 		dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg);
722fa9e4066Sahrens 	}
723c717a561Smaybee 
724c717a561Smaybee 	/*
725c717a561Smaybee 	 * Although we have dropped our reference to the dnode, it
726c717a561Smaybee 	 * can't be evicted until its written, and we haven't yet
727c717a561Smaybee 	 * initiated the IO for the dnode's dbuf.
728c717a561Smaybee 	 */
729fa9e4066Sahrens }
730