xref: /illumos-gate/usr/src/uts/common/fs/zfs/dnode_sync.c (revision bc9014e6a81272073b9854d9f65dd59e18d18c35)
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.
24bf16b11eSMatthew Ahrens  * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
25*bc9014e6SJustin Gibbs  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
26fa9e4066Sahrens  */
27fa9e4066Sahrens 
28fa9e4066Sahrens #include <sys/zfs_context.h>
29fa9e4066Sahrens #include <sys/dbuf.h>
30fa9e4066Sahrens #include <sys/dnode.h>
31fa9e4066Sahrens #include <sys/dmu.h>
32fa9e4066Sahrens #include <sys/dmu_tx.h>
33fa9e4066Sahrens #include <sys/dmu_objset.h>
34fa9e4066Sahrens #include <sys/dsl_dataset.h>
35fa9e4066Sahrens #include <sys/spa.h>
36bf16b11eSMatthew Ahrens #include <sys/range_tree.h>
3743466aaeSMax Grossman #include <sys/zfeature.h>
38fa9e4066Sahrens 
39fa9e4066Sahrens static void
40fa9e4066Sahrens dnode_increase_indirection(dnode_t *dn, dmu_tx_t *tx)
41fa9e4066Sahrens {
42fa9e4066Sahrens 	dmu_buf_impl_t *db;
43c717a561Smaybee 	int txgoff = tx->tx_txg & TXG_MASK;
44c717a561Smaybee 	int nblkptr = dn->dn_phys->dn_nblkptr;
45c717a561Smaybee 	int old_toplvl = dn->dn_phys->dn_nlevels - 1;
46c717a561Smaybee 	int new_level = dn->dn_next_nlevels[txgoff];
47fa9e4066Sahrens 	int i;
48fa9e4066Sahrens 
49c717a561Smaybee 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
50c717a561Smaybee 
51c717a561Smaybee 	/* this dnode can't be paged out because it's dirty */
52fa9e4066Sahrens 	ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE);
53fa9e4066Sahrens 	ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
54c717a561Smaybee 	ASSERT(new_level > 1 && dn->dn_phys->dn_nlevels > 0);
55fa9e4066Sahrens 
56fa9e4066Sahrens 	db = dbuf_hold_level(dn, dn->dn_phys->dn_nlevels, 0, FTAG);
57ea8dc4b6Seschrock 	ASSERT(db != NULL);
58c717a561Smaybee 
59c717a561Smaybee 	dn->dn_phys->dn_nlevels = new_level;
60f82bfe17Sgw 	dprintf("os=%p obj=%llu, increase to %d\n", dn->dn_objset,
61f82bfe17Sgw 	    dn->dn_object, dn->dn_phys->dn_nlevels);
62c717a561Smaybee 
63c717a561Smaybee 	/* check for existing blkptrs in the dnode */
64c717a561Smaybee 	for (i = 0; i < nblkptr; i++)
65fa9e4066Sahrens 		if (!BP_IS_HOLE(&dn->dn_phys->dn_blkptr[i]))
66fa9e4066Sahrens 			break;
67c717a561Smaybee 	if (i != nblkptr) {
68c717a561Smaybee 		/* transfer dnode's block pointers to new indirect block */
69c717a561Smaybee 		(void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED|DB_RF_HAVESTRUCT);
70c717a561Smaybee 		ASSERT(db->db.db_data);
71c717a561Smaybee 		ASSERT(arc_released(db->db_buf));
72c717a561Smaybee 		ASSERT3U(sizeof (blkptr_t) * nblkptr, <=, db->db.db_size);
73fa9e4066Sahrens 		bcopy(dn->dn_phys->dn_blkptr, db->db.db_data,
74c717a561Smaybee 		    sizeof (blkptr_t) * nblkptr);
756b4acc8bSahrens 		arc_buf_freeze(db->db_buf);
76fa9e4066Sahrens 	}
77fa9e4066Sahrens 
78fa9e4066Sahrens 	/* set dbuf's parent pointers to new indirect buf */
79c717a561Smaybee 	for (i = 0; i < nblkptr; i++) {
80c717a561Smaybee 		dmu_buf_impl_t *child = dbuf_find(dn, old_toplvl, i);
81c717a561Smaybee 
82fa9e4066Sahrens 		if (child == NULL)
83fa9e4066Sahrens 			continue;
84744947dcSTom Erickson #ifdef	DEBUG
85744947dcSTom Erickson 		DB_DNODE_ENTER(child);
86744947dcSTom Erickson 		ASSERT3P(DB_DNODE(child), ==, dn);
87744947dcSTom Erickson 		DB_DNODE_EXIT(child);
88744947dcSTom Erickson #endif	/* DEBUG */
89c717a561Smaybee 		if (child->db_parent && child->db_parent != dn->dn_dbuf) {
90c717a561Smaybee 			ASSERT(child->db_parent->db_level == db->db_level);
91c717a561Smaybee 			ASSERT(child->db_blkptr !=
92c717a561Smaybee 			    &dn->dn_phys->dn_blkptr[child->db_blkid]);
93fa9e4066Sahrens 			mutex_exit(&child->db_mtx);
94fa9e4066Sahrens 			continue;
95fa9e4066Sahrens 		}
96c717a561Smaybee 		ASSERT(child->db_parent == NULL ||
97c717a561Smaybee 		    child->db_parent == dn->dn_dbuf);
98c717a561Smaybee 
99c717a561Smaybee 		child->db_parent = db;
100c717a561Smaybee 		dbuf_add_ref(db, child);
101c717a561Smaybee 		if (db->db.db_data)
102c717a561Smaybee 			child->db_blkptr = (blkptr_t *)db->db.db_data + i;
103c717a561Smaybee 		else
104c717a561Smaybee 			child->db_blkptr = NULL;
105c717a561Smaybee 		dprintf_dbuf_bp(child, child->db_blkptr,
106c717a561Smaybee 		    "changed db_blkptr to new indirect %s", "");
107fa9e4066Sahrens 
108fa9e4066Sahrens 		mutex_exit(&child->db_mtx);
109fa9e4066Sahrens 	}
110fa9e4066Sahrens 
111c717a561Smaybee 	bzero(dn->dn_phys->dn_blkptr, sizeof (blkptr_t) * nblkptr);
112fa9e4066Sahrens 
113ea8dc4b6Seschrock 	dbuf_rele(db, FTAG);
114c717a561Smaybee 
115c717a561Smaybee 	rw_exit(&dn->dn_struct_rwlock);
116fa9e4066Sahrens }
117fa9e4066Sahrens 
11843466aaeSMax Grossman static void
119fa9e4066Sahrens free_blocks(dnode_t *dn, blkptr_t *bp, int num, dmu_tx_t *tx)
120fa9e4066Sahrens {
121cdb0ab79Smaybee 	dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
122fa9e4066Sahrens 	uint64_t bytesfreed = 0;
123fa9e4066Sahrens 
124cdb0ab79Smaybee 	dprintf("ds=%p obj=%llx num=%d\n", ds, dn->dn_object, num);
125fa9e4066Sahrens 
12643466aaeSMax Grossman 	for (int i = 0; i < num; i++, bp++) {
127fa9e4066Sahrens 		if (BP_IS_HOLE(bp))
128fa9e4066Sahrens 			continue;
129fa9e4066Sahrens 
130b24ab676SJeff Bonwick 		bytesfreed += dsl_dataset_block_kill(ds, bp, tx, B_FALSE);
13199653d4eSeschrock 		ASSERT3U(bytesfreed, <=, DN_USED_BYTES(dn->dn_phys));
13243466aaeSMax Grossman 
13343466aaeSMax Grossman 		/*
13443466aaeSMax Grossman 		 * Save some useful information on the holes being
13543466aaeSMax Grossman 		 * punched, including logical size, type, and indirection
13643466aaeSMax Grossman 		 * level. Retaining birth time enables detection of when
13743466aaeSMax Grossman 		 * holes are punched for reducing the number of free
13843466aaeSMax Grossman 		 * records transmitted during a zfs send.
13943466aaeSMax Grossman 		 */
14043466aaeSMax Grossman 
14143466aaeSMax Grossman 		uint64_t lsize = BP_GET_LSIZE(bp);
14243466aaeSMax Grossman 		dmu_object_type_t type = BP_GET_TYPE(bp);
14343466aaeSMax Grossman 		uint64_t lvl = BP_GET_LEVEL(bp);
14443466aaeSMax Grossman 
145c717a561Smaybee 		bzero(bp, sizeof (blkptr_t));
14643466aaeSMax Grossman 
14743466aaeSMax Grossman 		if (spa_feature_is_active(dn->dn_objset->os_spa,
14843466aaeSMax Grossman 		    SPA_FEATURE_HOLE_BIRTH)) {
14943466aaeSMax Grossman 			BP_SET_LSIZE(bp, lsize);
15043466aaeSMax Grossman 			BP_SET_TYPE(bp, type);
15143466aaeSMax Grossman 			BP_SET_LEVEL(bp, lvl);
15243466aaeSMax Grossman 			BP_SET_BIRTH(bp, dmu_tx_get_txg(tx), 0);
15343466aaeSMax Grossman 		}
154fa9e4066Sahrens 	}
155fa9e4066Sahrens 	dnode_diduse_space(dn, -bytesfreed);
156fa9e4066Sahrens }
157fa9e4066Sahrens 
1589c9dc39aSek #ifdef ZFS_DEBUG
159fa9e4066Sahrens static void
160fa9e4066Sahrens free_verify(dmu_buf_impl_t *db, uint64_t start, uint64_t end, dmu_tx_t *tx)
161fa9e4066Sahrens {
162fa9e4066Sahrens 	int off, num;
163fa9e4066Sahrens 	int i, err, epbs;
164fa9e4066Sahrens 	uint64_t txg = tx->tx_txg;
165744947dcSTom Erickson 	dnode_t *dn;
166fa9e4066Sahrens 
167744947dcSTom Erickson 	DB_DNODE_ENTER(db);
168744947dcSTom Erickson 	dn = DB_DNODE(db);
169744947dcSTom Erickson 	epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
170fa9e4066Sahrens 	off = start - (db->db_blkid * 1<<epbs);
171fa9e4066Sahrens 	num = end - start + 1;
172fa9e4066Sahrens 
173fa9e4066Sahrens 	ASSERT3U(off, >=, 0);
174fa9e4066Sahrens 	ASSERT3U(num, >=, 0);
175fa9e4066Sahrens 	ASSERT3U(db->db_level, >, 0);
176744947dcSTom Erickson 	ASSERT3U(db->db.db_size, ==, 1 << dn->dn_phys->dn_indblkshift);
177fa9e4066Sahrens 	ASSERT3U(off+num, <=, db->db.db_size >> SPA_BLKPTRSHIFT);
178fa9e4066Sahrens 	ASSERT(db->db_blkptr != NULL);
179fa9e4066Sahrens 
180fa9e4066Sahrens 	for (i = off; i < off+num; i++) {
181fa9e4066Sahrens 		uint64_t *buf;
182fa9e4066Sahrens 		dmu_buf_impl_t *child;
183c717a561Smaybee 		dbuf_dirty_record_t *dr;
184c717a561Smaybee 		int j;
185fa9e4066Sahrens 
186fa9e4066Sahrens 		ASSERT(db->db_level == 1);
187fa9e4066Sahrens 
188744947dcSTom Erickson 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
189744947dcSTom Erickson 		err = dbuf_hold_impl(dn, db->db_level-1,
190f82bfe17Sgw 		    (db->db_blkid << epbs) + i, TRUE, FTAG, &child);
191744947dcSTom Erickson 		rw_exit(&dn->dn_struct_rwlock);
192fa9e4066Sahrens 		if (err == ENOENT)
193fa9e4066Sahrens 			continue;
194fa9e4066Sahrens 		ASSERT(err == 0);
195fa9e4066Sahrens 		ASSERT(child->db_level == 0);
196c717a561Smaybee 		dr = child->db_last_dirty;
197c717a561Smaybee 		while (dr && dr->dr_txg > txg)
198c717a561Smaybee 			dr = dr->dr_next;
199c717a561Smaybee 		ASSERT(dr == NULL || dr->dr_txg == txg);
200c717a561Smaybee 
201c717a561Smaybee 		/* data_old better be zeroed */
202c717a561Smaybee 		if (dr) {
203c717a561Smaybee 			buf = dr->dt.dl.dr_data->b_data;
204fa9e4066Sahrens 			for (j = 0; j < child->db.db_size >> 3; j++) {
205fa9e4066Sahrens 				if (buf[j] != 0) {
206fa9e4066Sahrens 					panic("freed data not zero: "
207fa9e4066Sahrens 					    "child=%p i=%d off=%d num=%d\n",
208903a11ebSrh 					    (void *)child, i, off, num);
209fa9e4066Sahrens 				}
210fa9e4066Sahrens 			}
211fa9e4066Sahrens 		}
212fa9e4066Sahrens 
213fa9e4066Sahrens 		/*
214fa9e4066Sahrens 		 * db_data better be zeroed unless it's dirty in a
215fa9e4066Sahrens 		 * future txg.
216fa9e4066Sahrens 		 */
217fa9e4066Sahrens 		mutex_enter(&child->db_mtx);
218fa9e4066Sahrens 		buf = child->db.db_data;
219fa9e4066Sahrens 		if (buf != NULL && child->db_state != DB_FILL &&
220c717a561Smaybee 		    child->db_last_dirty == NULL) {
221fa9e4066Sahrens 			for (j = 0; j < child->db.db_size >> 3; j++) {
222fa9e4066Sahrens 				if (buf[j] != 0) {
223fa9e4066Sahrens 					panic("freed data not zero: "
224fa9e4066Sahrens 					    "child=%p i=%d off=%d num=%d\n",
225903a11ebSrh 					    (void *)child, i, off, num);
226fa9e4066Sahrens 				}
227fa9e4066Sahrens 			}
228fa9e4066Sahrens 		}
229fa9e4066Sahrens 		mutex_exit(&child->db_mtx);
230fa9e4066Sahrens 
231ea8dc4b6Seschrock 		dbuf_rele(child, FTAG);
232fa9e4066Sahrens 	}
233744947dcSTom Erickson 	DB_DNODE_EXIT(db);
234fa9e4066Sahrens }
2359c9dc39aSek #endif
236fa9e4066Sahrens 
23743466aaeSMax Grossman static void
23843466aaeSMax Grossman free_children(dmu_buf_impl_t *db, uint64_t blkid, uint64_t nblks,
239fa9e4066Sahrens     dmu_tx_t *tx)
240fa9e4066Sahrens {
241744947dcSTom Erickson 	dnode_t *dn;
242fa9e4066Sahrens 	blkptr_t *bp;
243fa9e4066Sahrens 	dmu_buf_impl_t *subdb;
244fa9e4066Sahrens 	uint64_t start, end, dbstart, dbend, i;
24543466aaeSMax Grossman 	int epbs, shift;
246cdb0ab79Smaybee 
247cdb0ab79Smaybee 	/*
248cdb0ab79Smaybee 	 * There is a small possibility that this block will not be cached:
249cdb0ab79Smaybee 	 *   1 - if level > 1 and there are no children with level <= 1
25043466aaeSMax Grossman 	 *   2 - if this block was evicted since we read it from
25143466aaeSMax Grossman 	 *	 dmu_tx_hold_free().
252cdb0ab79Smaybee 	 */
253cdb0ab79Smaybee 	if (db->db_state != DB_CACHED)
254cdb0ab79Smaybee 		(void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
255fa9e4066Sahrens 
2563f9d6ad7SLin Ling 	dbuf_release_bp(db);
25743466aaeSMax Grossman 	bp = db->db.db_data;
258fa9e4066Sahrens 
259744947dcSTom Erickson 	DB_DNODE_ENTER(db);
260744947dcSTom Erickson 	dn = DB_DNODE(db);
261744947dcSTom Erickson 	epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
262fa9e4066Sahrens 	shift = (db->db_level - 1) * epbs;
263fa9e4066Sahrens 	dbstart = db->db_blkid << epbs;
264fa9e4066Sahrens 	start = blkid >> shift;
265fa9e4066Sahrens 	if (dbstart < start) {
266fa9e4066Sahrens 		bp += start - dbstart;
267fa9e4066Sahrens 	} else {
268fa9e4066Sahrens 		start = dbstart;
269fa9e4066Sahrens 	}
270fa9e4066Sahrens 	dbend = ((db->db_blkid + 1) << epbs) - 1;
271fa9e4066Sahrens 	end = (blkid + nblks - 1) >> shift;
272fa9e4066Sahrens 	if (dbend <= end)
273fa9e4066Sahrens 		end = dbend;
27443466aaeSMax Grossman 
275fa9e4066Sahrens 	ASSERT3U(start, <=, end);
276fa9e4066Sahrens 
277fa9e4066Sahrens 	if (db->db_level == 1) {
2789c9dc39aSek 		FREE_VERIFY(db, start, end, tx);
27943466aaeSMax Grossman 		free_blocks(dn, bp, end-start+1, tx);
28043466aaeSMax Grossman 	} else {
28143466aaeSMax Grossman 		for (i = start; i <= end; i++, bp++) {
28243466aaeSMax Grossman 			if (BP_IS_HOLE(bp))
28343466aaeSMax Grossman 				continue;
28443466aaeSMax Grossman 			rw_enter(&dn->dn_struct_rwlock, RW_READER);
28543466aaeSMax Grossman 			VERIFY0(dbuf_hold_impl(dn, db->db_level - 1,
28643466aaeSMax Grossman 			    i, B_TRUE, FTAG, &subdb));
28743466aaeSMax Grossman 			rw_exit(&dn->dn_struct_rwlock);
28843466aaeSMax Grossman 			ASSERT3P(bp, ==, subdb->db_blkptr);
28943466aaeSMax Grossman 
29043466aaeSMax Grossman 			free_children(subdb, blkid, nblks, tx);
29143466aaeSMax Grossman 			dbuf_rele(subdb, FTAG);
29243466aaeSMax Grossman 		}
293fa9e4066Sahrens 	}
294fa9e4066Sahrens 
29543466aaeSMax Grossman 	/* If this whole block is free, free ourself too. */
29643466aaeSMax Grossman 	for (i = 0, bp = db->db.db_data; i < 1 << epbs; i++, bp++) {
29743466aaeSMax Grossman 		if (!BP_IS_HOLE(bp))
29843466aaeSMax Grossman 			break;
29943466aaeSMax Grossman 	}
30043466aaeSMax Grossman 	if (i == 1 << epbs) {
30143466aaeSMax Grossman 		/* didn't find any non-holes */
30243466aaeSMax Grossman 		bzero(db->db.db_data, db->db.db_size);
30343466aaeSMax Grossman 		free_blocks(dn, db->db_blkptr, 1, tx);
30443466aaeSMax Grossman 	} else {
30543466aaeSMax Grossman 		/*
30643466aaeSMax Grossman 		 * Partial block free; must be marked dirty so that it
30743466aaeSMax Grossman 		 * will be written out.
30843466aaeSMax Grossman 		 */
30943466aaeSMax Grossman 		ASSERT(db->db_dirtycnt > 0);
310fa9e4066Sahrens 	}
31143466aaeSMax Grossman 
312744947dcSTom Erickson 	DB_DNODE_EXIT(db);
3136b4acc8bSahrens 	arc_buf_freeze(db->db_buf);
314fa9e4066Sahrens }
315fa9e4066Sahrens 
316fa9e4066Sahrens /*
317f7170741SWill Andrews  * Traverse the indicated range of the provided file
318fa9e4066Sahrens  * and "free" all the blocks contained there.
319fa9e4066Sahrens  */
320fa9e4066Sahrens static void
321bf16b11eSMatthew Ahrens dnode_sync_free_range_impl(dnode_t *dn, uint64_t blkid, uint64_t nblks,
32243466aaeSMax Grossman     dmu_tx_t *tx)
323fa9e4066Sahrens {
324fa9e4066Sahrens 	blkptr_t *bp = dn->dn_phys->dn_blkptr;
325fa9e4066Sahrens 	int dnlevel = dn->dn_phys->dn_nlevels;
32643466aaeSMax Grossman 	boolean_t trunc = B_FALSE;
327fa9e4066Sahrens 
328fa9e4066Sahrens 	if (blkid > dn->dn_phys->dn_maxblkid)
329fa9e4066Sahrens 		return;
330fa9e4066Sahrens 
331fa9e4066Sahrens 	ASSERT(dn->dn_phys->dn_maxblkid < UINT64_MAX);
33243466aaeSMax Grossman 	if (blkid + nblks > dn->dn_phys->dn_maxblkid) {
333fa9e4066Sahrens 		nblks = dn->dn_phys->dn_maxblkid - blkid + 1;
33443466aaeSMax Grossman 		trunc = B_TRUE;
33543466aaeSMax Grossman 	}
336fa9e4066Sahrens 
337fa9e4066Sahrens 	/* There are no indirect blocks in the object */
338fa9e4066Sahrens 	if (dnlevel == 1) {
339fa9e4066Sahrens 		if (blkid >= dn->dn_phys->dn_nblkptr) {
340fa9e4066Sahrens 			/* this range was never made persistent */
341fa9e4066Sahrens 			return;
342fa9e4066Sahrens 		}
343fa9e4066Sahrens 		ASSERT3U(blkid + nblks, <=, dn->dn_phys->dn_nblkptr);
34443466aaeSMax Grossman 		free_blocks(dn, bp + blkid, nblks, tx);
34543466aaeSMax Grossman 	} else {
34643466aaeSMax Grossman 		int shift = (dnlevel - 1) *
34743466aaeSMax Grossman 		    (dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT);
34843466aaeSMax Grossman 		int start = blkid >> shift;
34943466aaeSMax Grossman 		int end = (blkid + nblks - 1) >> shift;
35043466aaeSMax Grossman 		dmu_buf_impl_t *db;
35143466aaeSMax Grossman 
35243466aaeSMax Grossman 		ASSERT(start < dn->dn_phys->dn_nblkptr);
35343466aaeSMax Grossman 		bp += start;
35443466aaeSMax Grossman 		for (int i = start; i <= end; i++, bp++) {
35543466aaeSMax Grossman 			if (BP_IS_HOLE(bp))
35643466aaeSMax Grossman 				continue;
35743466aaeSMax Grossman 			rw_enter(&dn->dn_struct_rwlock, RW_READER);
35843466aaeSMax Grossman 			VERIFY0(dbuf_hold_impl(dn, dnlevel - 1, i,
35943466aaeSMax Grossman 			    TRUE, FTAG, &db));
36043466aaeSMax Grossman 			rw_exit(&dn->dn_struct_rwlock);
36143466aaeSMax Grossman 
36243466aaeSMax Grossman 			free_children(db, blkid, nblks, tx);
36343466aaeSMax Grossman 			dbuf_rele(db, FTAG);
364fa9e4066Sahrens 		}
365fa9e4066Sahrens 	}
36643466aaeSMax Grossman 
367fa9e4066Sahrens 	if (trunc) {
36843466aaeSMax Grossman 		dn->dn_phys->dn_maxblkid = blkid == 0 ? 0 : blkid - 1;
36943466aaeSMax Grossman 
370fa9e4066Sahrens 		uint64_t off = (dn->dn_phys->dn_maxblkid + 1) *
371fa9e4066Sahrens 		    (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT);
372fa9e4066Sahrens 		ASSERT(off < dn->dn_phys->dn_maxblkid ||
373fa9e4066Sahrens 		    dn->dn_phys->dn_maxblkid == 0 ||
374cdb0ab79Smaybee 		    dnode_next_offset(dn, 0, &off, 1, 1, 0) != 0);
375fa9e4066Sahrens 	}
376fa9e4066Sahrens }
377fa9e4066Sahrens 
378bf16b11eSMatthew Ahrens typedef struct dnode_sync_free_range_arg {
379bf16b11eSMatthew Ahrens 	dnode_t *dsfra_dnode;
380bf16b11eSMatthew Ahrens 	dmu_tx_t *dsfra_tx;
381bf16b11eSMatthew Ahrens } dnode_sync_free_range_arg_t;
382bf16b11eSMatthew Ahrens 
383bf16b11eSMatthew Ahrens static void
384bf16b11eSMatthew Ahrens dnode_sync_free_range(void *arg, uint64_t blkid, uint64_t nblks)
385bf16b11eSMatthew Ahrens {
386bf16b11eSMatthew Ahrens 	dnode_sync_free_range_arg_t *dsfra = arg;
387bf16b11eSMatthew Ahrens 	dnode_t *dn = dsfra->dsfra_dnode;
388bf16b11eSMatthew Ahrens 
389bf16b11eSMatthew Ahrens 	mutex_exit(&dn->dn_mtx);
390bf16b11eSMatthew Ahrens 	dnode_sync_free_range_impl(dn, blkid, nblks, dsfra->dsfra_tx);
391bf16b11eSMatthew Ahrens 	mutex_enter(&dn->dn_mtx);
392bf16b11eSMatthew Ahrens }
393bf16b11eSMatthew Ahrens 
394ea8dc4b6Seschrock /*
395f7170741SWill Andrews  * Try to kick all the dnode's dbufs out of the cache...
396ea8dc4b6Seschrock  */
3971934e92fSmaybee void
3981934e92fSmaybee dnode_evict_dbufs(dnode_t *dn)
399ea8dc4b6Seschrock {
400*bc9014e6SJustin Gibbs 	dmu_buf_impl_t db_marker;
401*bc9014e6SJustin Gibbs 	dmu_buf_impl_t *db, *db_next;
402c543ec06Sahrens 
403*bc9014e6SJustin Gibbs 	mutex_enter(&dn->dn_dbufs_mtx);
404*bc9014e6SJustin Gibbs 	for (db = avl_first(&dn->dn_dbufs); db != NULL; db = db_next) {
405c543ec06Sahrens 
406744947dcSTom Erickson #ifdef	DEBUG
407*bc9014e6SJustin Gibbs 		DB_DNODE_ENTER(db);
408*bc9014e6SJustin Gibbs 		ASSERT3P(DB_DNODE(db), ==, dn);
409*bc9014e6SJustin Gibbs 		DB_DNODE_EXIT(db);
410744947dcSTom Erickson #endif	/* DEBUG */
411ea8dc4b6Seschrock 
412*bc9014e6SJustin Gibbs 		mutex_enter(&db->db_mtx);
413*bc9014e6SJustin Gibbs 		if (db->db_state != DB_EVICTING &&
414*bc9014e6SJustin Gibbs 		    refcount_is_zero(&db->db_holds)) {
415*bc9014e6SJustin Gibbs 			db_marker.db_level = db->db_level;
416*bc9014e6SJustin Gibbs 			db_marker.db_blkid = db->db_blkid;
417*bc9014e6SJustin Gibbs 			db_marker.db_state = DB_SEARCH;
418*bc9014e6SJustin Gibbs 			avl_insert_here(&dn->dn_dbufs, &db_marker, db,
419*bc9014e6SJustin Gibbs 			    AVL_BEFORE);
420*bc9014e6SJustin Gibbs 
421*bc9014e6SJustin Gibbs 			dbuf_clear(db);
422*bc9014e6SJustin Gibbs 
423*bc9014e6SJustin Gibbs 			db_next = AVL_NEXT(&dn->dn_dbufs, &db_marker);
424*bc9014e6SJustin Gibbs 			avl_remove(&dn->dn_dbufs, &db_marker);
425*bc9014e6SJustin Gibbs 		} else {
426*bc9014e6SJustin Gibbs 			mutex_exit(&db->db_mtx);
427*bc9014e6SJustin Gibbs 			db_next = AVL_NEXT(&dn->dn_dbufs, db);
428ea8dc4b6Seschrock 		}
429*bc9014e6SJustin Gibbs 	}
430*bc9014e6SJustin Gibbs 	mutex_exit(&dn->dn_dbufs_mtx);
431c543ec06Sahrens 
432ea8dc4b6Seschrock 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
433ea8dc4b6Seschrock 	if (dn->dn_bonus && refcount_is_zero(&dn->dn_bonus->db_holds)) {
434ea8dc4b6Seschrock 		mutex_enter(&dn->dn_bonus->db_mtx);
435ea8dc4b6Seschrock 		dbuf_evict(dn->dn_bonus);
436ea8dc4b6Seschrock 		dn->dn_bonus = NULL;
437ea8dc4b6Seschrock 	}
438ea8dc4b6Seschrock 	rw_exit(&dn->dn_struct_rwlock);
439ea8dc4b6Seschrock }
440ea8dc4b6Seschrock 
441c717a561Smaybee static void
442c717a561Smaybee dnode_undirty_dbufs(list_t *list)
443fa9e4066Sahrens {
444c717a561Smaybee 	dbuf_dirty_record_t *dr;
445fa9e4066Sahrens 
446c717a561Smaybee 	while (dr = list_head(list)) {
447c717a561Smaybee 		dmu_buf_impl_t *db = dr->dr_dbuf;
448c717a561Smaybee 		uint64_t txg = dr->dr_txg;
449fa9e4066Sahrens 
450b24ab676SJeff Bonwick 		if (db->db_level != 0)
451b24ab676SJeff Bonwick 			dnode_undirty_dbufs(&dr->dt.di.dr_children);
452b24ab676SJeff Bonwick 
453fa9e4066Sahrens 		mutex_enter(&db->db_mtx);
454fa9e4066Sahrens 		/* XXX - use dbuf_undirty()? */
455c717a561Smaybee 		list_remove(list, dr);
456c717a561Smaybee 		ASSERT(db->db_last_dirty == dr);
457c717a561Smaybee 		db->db_last_dirty = NULL;
458c717a561Smaybee 		db->db_dirtycnt -= 1;
459fa9e4066Sahrens 		if (db->db_level == 0) {
4600a586ceaSMark Shellenbaum 			ASSERT(db->db_blkid == DMU_BONUS_BLKID ||
461c717a561Smaybee 			    dr->dt.dl.dr_data == db->db_buf);
462c717a561Smaybee 			dbuf_unoverride(dr);
463d2b3cbbdSJorgen Lundman 		} else {
464d2b3cbbdSJorgen Lundman 			mutex_destroy(&dr->dt.di.dr_mtx);
465d2b3cbbdSJorgen Lundman 			list_destroy(&dr->dt.di.dr_children);
466fa9e4066Sahrens 		}
467c717a561Smaybee 		kmem_free(dr, sizeof (dbuf_dirty_record_t));
468b24ab676SJeff Bonwick 		dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg);
469fa9e4066Sahrens 	}
470c717a561Smaybee }
471fa9e4066Sahrens 
472c717a561Smaybee static void
473c717a561Smaybee dnode_sync_free(dnode_t *dn, dmu_tx_t *tx)
474c717a561Smaybee {
475c717a561Smaybee 	int txgoff = tx->tx_txg & TXG_MASK;
476c717a561Smaybee 
477c717a561Smaybee 	ASSERT(dmu_tx_is_syncing(tx));
478c717a561Smaybee 
479cdb0ab79Smaybee 	/*
480cdb0ab79Smaybee 	 * Our contents should have been freed in dnode_sync() by the
481cdb0ab79Smaybee 	 * free range record inserted by the caller of dnode_free().
482cdb0ab79Smaybee 	 */
483fb09f5aaSMadhav Suresh 	ASSERT0(DN_USED_BYTES(dn->dn_phys));
484cdb0ab79Smaybee 	ASSERT(BP_IS_HOLE(dn->dn_phys->dn_blkptr));
485cdb0ab79Smaybee 
486c717a561Smaybee 	dnode_undirty_dbufs(&dn->dn_dirty_records[txgoff]);
4871934e92fSmaybee 	dnode_evict_dbufs(dn);
4880f6d88adSAlex Reece 	ASSERT(avl_is_empty(&dn->dn_dbufs));
489ea8dc4b6Seschrock 
490ea8dc4b6Seschrock 	/*
491ea8dc4b6Seschrock 	 * XXX - It would be nice to assert this, but we may still
492ea8dc4b6Seschrock 	 * have residual holds from async evictions from the arc...
493ea8dc4b6Seschrock 	 *
49455434c77Sek 	 * zfs_obj_to_path() also depends on this being
49555434c77Sek 	 * commented out.
49655434c77Sek 	 *
497ea8dc4b6Seschrock 	 * ASSERT3U(refcount_count(&dn->dn_holds), ==, 1);
498ea8dc4b6Seschrock 	 */
499fa9e4066Sahrens 
500fa9e4066Sahrens 	/* Undirty next bits */
501fa9e4066Sahrens 	dn->dn_next_nlevels[txgoff] = 0;
502fa9e4066Sahrens 	dn->dn_next_indblkshift[txgoff] = 0;
503c543ec06Sahrens 	dn->dn_next_blksz[txgoff] = 0;
504fa9e4066Sahrens 
505fa9e4066Sahrens 	/* ASSERT(blkptrs are zero); */
506fa9e4066Sahrens 	ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE);
507fa9e4066Sahrens 	ASSERT(dn->dn_type != DMU_OT_NONE);
508fa9e4066Sahrens 
509fa9e4066Sahrens 	ASSERT(dn->dn_free_txg > 0);
510fa9e4066Sahrens 	if (dn->dn_allocated_txg != dn->dn_free_txg)
51143466aaeSMax Grossman 		dmu_buf_will_dirty(&dn->dn_dbuf->db, tx);
512fa9e4066Sahrens 	bzero(dn->dn_phys, sizeof (dnode_phys_t));
513fa9e4066Sahrens 
514fa9e4066Sahrens 	mutex_enter(&dn->dn_mtx);
515fa9e4066Sahrens 	dn->dn_type = DMU_OT_NONE;
516fa9e4066Sahrens 	dn->dn_maxblkid = 0;
517fa9e4066Sahrens 	dn->dn_allocated_txg = 0;
518758f6e0bSgw 	dn->dn_free_txg = 0;
5190a586ceaSMark Shellenbaum 	dn->dn_have_spill = B_FALSE;
520fa9e4066Sahrens 	mutex_exit(&dn->dn_mtx);
521fa9e4066Sahrens 
522ea8dc4b6Seschrock 	ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
523fa9e4066Sahrens 
524fa9e4066Sahrens 	dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg);
525fa9e4066Sahrens 	/*
526fa9e4066Sahrens 	 * Now that we've released our hold, the dnode may
527fa9e4066Sahrens 	 * be evicted, so we musn't access it.
528fa9e4066Sahrens 	 */
529fa9e4066Sahrens }
530fa9e4066Sahrens 
531fa9e4066Sahrens /*
532c717a561Smaybee  * Write out the dnode's dirty buffers.
533fa9e4066Sahrens  */
534c717a561Smaybee void
535c717a561Smaybee dnode_sync(dnode_t *dn, dmu_tx_t *tx)
536fa9e4066Sahrens {
537fa9e4066Sahrens 	dnode_phys_t *dnp = dn->dn_phys;
538c717a561Smaybee 	int txgoff = tx->tx_txg & TXG_MASK;
539c717a561Smaybee 	list_t *list = &dn->dn_dirty_records[txgoff];
54014843421SMatthew Ahrens 	static const dnode_phys_t zerodn = { 0 };
5410a586ceaSMark Shellenbaum 	boolean_t kill_spill = B_FALSE;
542fa9e4066Sahrens 
543fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
544fa9e4066Sahrens 	ASSERT(dnp->dn_type != DMU_OT_NONE || dn->dn_allocated_txg);
54514843421SMatthew Ahrens 	ASSERT(dnp->dn_type != DMU_OT_NONE ||
54614843421SMatthew Ahrens 	    bcmp(dnp, &zerodn, DNODE_SIZE) == 0);
5479c9dc39aSek 	DNODE_VERIFY(dn);
548c543ec06Sahrens 
549c717a561Smaybee 	ASSERT(dn->dn_dbuf == NULL || arc_released(dn->dn_dbuf->db_buf));
550fa9e4066Sahrens 
55114843421SMatthew Ahrens 	if (dmu_objset_userused_enabled(dn->dn_objset) &&
55214843421SMatthew Ahrens 	    !DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
5530a586ceaSMark Shellenbaum 		mutex_enter(&dn->dn_mtx);
5540a586ceaSMark Shellenbaum 		dn->dn_oldused = DN_USED_BYTES(dn->dn_phys);
5550a586ceaSMark Shellenbaum 		dn->dn_oldflags = dn->dn_phys->dn_flags;
55614843421SMatthew Ahrens 		dn->dn_phys->dn_flags |= DNODE_FLAG_USERUSED_ACCOUNTED;
5570a586ceaSMark Shellenbaum 		mutex_exit(&dn->dn_mtx);
55806e0070dSMark Shellenbaum 		dmu_objset_userquota_get_ids(dn, B_FALSE, tx);
55914843421SMatthew Ahrens 	} else {
56014843421SMatthew Ahrens 		/* Once we account for it, we should always account for it. */
56114843421SMatthew Ahrens 		ASSERT(!(dn->dn_phys->dn_flags &
56214843421SMatthew Ahrens 		    DNODE_FLAG_USERUSED_ACCOUNTED));
56314843421SMatthew Ahrens 	}
56414843421SMatthew Ahrens 
565fa9e4066Sahrens 	mutex_enter(&dn->dn_mtx);
566fa9e4066Sahrens 	if (dn->dn_allocated_txg == tx->tx_txg) {
567fa9e4066Sahrens 		/* The dnode is newly allocated or reallocated */
568fa9e4066Sahrens 		if (dnp->dn_type == DMU_OT_NONE) {
569fa9e4066Sahrens 			/* this is a first alloc, not a realloc */
570fa9e4066Sahrens 			dnp->dn_nlevels = 1;
571da03de99SMark Maybee 			dnp->dn_nblkptr = dn->dn_nblkptr;
572fa9e4066Sahrens 		}
573fa9e4066Sahrens 
574fa9e4066Sahrens 		dnp->dn_type = dn->dn_type;
575fa9e4066Sahrens 		dnp->dn_bonustype = dn->dn_bonustype;
576fa9e4066Sahrens 		dnp->dn_bonuslen = dn->dn_bonuslen;
577fa9e4066Sahrens 	}
578c717a561Smaybee 	ASSERT(dnp->dn_nlevels > 1 ||
579f676ed34Sahrens 	    BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
5805d7b4d43SMatthew Ahrens 	    BP_IS_EMBEDDED(&dnp->dn_blkptr[0]) ||
581f676ed34Sahrens 	    BP_GET_LSIZE(&dnp->dn_blkptr[0]) ==
582f676ed34Sahrens 	    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
5835d7b4d43SMatthew Ahrens 	ASSERT(dnp->dn_nlevels < 2 ||
5845d7b4d43SMatthew Ahrens 	    BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
5855d7b4d43SMatthew Ahrens 	    BP_GET_LSIZE(&dnp->dn_blkptr[0]) == 1 << dnp->dn_indblkshift);
586f676ed34Sahrens 
5872acef22dSMatthew Ahrens 	if (dn->dn_next_type[txgoff] != 0) {
5882acef22dSMatthew Ahrens 		dnp->dn_type = dn->dn_type;
5892acef22dSMatthew Ahrens 		dn->dn_next_type[txgoff] = 0;
5902acef22dSMatthew Ahrens 	}
5912acef22dSMatthew Ahrens 
5922acef22dSMatthew Ahrens 	if (dn->dn_next_blksz[txgoff] != 0) {
593c543ec06Sahrens 		ASSERT(P2PHASE(dn->dn_next_blksz[txgoff],
594fa9e4066Sahrens 		    SPA_MINBLOCKSIZE) == 0);
595f676ed34Sahrens 		ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
596cdb0ab79Smaybee 		    dn->dn_maxblkid == 0 || list_head(list) != NULL ||
597347a31bcSahrens 		    dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT ==
598bf16b11eSMatthew Ahrens 		    dnp->dn_datablkszsec ||
599bf16b11eSMatthew Ahrens 		    range_tree_space(dn->dn_free_ranges[txgoff]) != 0);
600fa9e4066Sahrens 		dnp->dn_datablkszsec =
601c543ec06Sahrens 		    dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT;
602c543ec06Sahrens 		dn->dn_next_blksz[txgoff] = 0;
603fa9e4066Sahrens 	}
604fa9e4066Sahrens 
6052acef22dSMatthew Ahrens 	if (dn->dn_next_bonuslen[txgoff] != 0) {
6061934e92fSmaybee 		if (dn->dn_next_bonuslen[txgoff] == DN_ZERO_BONUSLEN)
6071934e92fSmaybee 			dnp->dn_bonuslen = 0;
6081934e92fSmaybee 		else
6091934e92fSmaybee 			dnp->dn_bonuslen = dn->dn_next_bonuslen[txgoff];
6101934e92fSmaybee 		ASSERT(dnp->dn_bonuslen <= DN_MAX_BONUSLEN);
6111934e92fSmaybee 		dn->dn_next_bonuslen[txgoff] = 0;
6121934e92fSmaybee 	}
6131934e92fSmaybee 
6142acef22dSMatthew Ahrens 	if (dn->dn_next_bonustype[txgoff] != 0) {
615ad135b5dSChristopher Siden 		ASSERT(DMU_OT_IS_VALID(dn->dn_next_bonustype[txgoff]));
6160a586ceaSMark Shellenbaum 		dnp->dn_bonustype = dn->dn_next_bonustype[txgoff];
6170a586ceaSMark Shellenbaum 		dn->dn_next_bonustype[txgoff] = 0;
6180a586ceaSMark Shellenbaum 	}
6190a586ceaSMark Shellenbaum 
62043466aaeSMax Grossman 	boolean_t freeing_dnode = dn->dn_free_txg > 0 &&
62143466aaeSMax Grossman 	    dn->dn_free_txg <= tx->tx_txg;
62243466aaeSMax Grossman 
6230a586ceaSMark Shellenbaum 	/*
624e6518318SMatthew Ahrens 	 * Remove the spill block if we have been explicitly asked to
625e6518318SMatthew Ahrens 	 * remove it, or if the object is being removed.
6260a586ceaSMark Shellenbaum 	 */
627e6518318SMatthew Ahrens 	if (dn->dn_rm_spillblk[txgoff] || freeing_dnode) {
628e6518318SMatthew Ahrens 		if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR)
6290a586ceaSMark Shellenbaum 			kill_spill = B_TRUE;
6300a586ceaSMark Shellenbaum 		dn->dn_rm_spillblk[txgoff] = 0;
6310a586ceaSMark Shellenbaum 	}
6320a586ceaSMark Shellenbaum 
6332acef22dSMatthew Ahrens 	if (dn->dn_next_indblkshift[txgoff] != 0) {
634fa9e4066Sahrens 		ASSERT(dnp->dn_nlevels == 1);
635fa9e4066Sahrens 		dnp->dn_indblkshift = dn->dn_next_indblkshift[txgoff];
636fa9e4066Sahrens 		dn->dn_next_indblkshift[txgoff] = 0;
637fa9e4066Sahrens 	}
638fa9e4066Sahrens 
639fa9e4066Sahrens 	/*
640fa9e4066Sahrens 	 * Just take the live (open-context) values for checksum and compress.
641fa9e4066Sahrens 	 * Strictly speaking it's a future leak, but nothing bad happens if we
642fa9e4066Sahrens 	 * start using the new checksum or compress algorithm a little early.
643fa9e4066Sahrens 	 */
644fa9e4066Sahrens 	dnp->dn_checksum = dn->dn_checksum;
645fa9e4066Sahrens 	dnp->dn_compress = dn->dn_compress;
646fa9e4066Sahrens 
647fa9e4066Sahrens 	mutex_exit(&dn->dn_mtx);
648fa9e4066Sahrens 
6490a586ceaSMark Shellenbaum 	if (kill_spill) {
65043466aaeSMax Grossman 		free_blocks(dn, &dn->dn_phys->dn_spill, 1, tx);
6510a586ceaSMark Shellenbaum 		mutex_enter(&dn->dn_mtx);
6520a586ceaSMark Shellenbaum 		dnp->dn_flags &= ~DNODE_FLAG_SPILL_BLKPTR;
6530a586ceaSMark Shellenbaum 		mutex_exit(&dn->dn_mtx);
6540a586ceaSMark Shellenbaum 	}
6550a586ceaSMark Shellenbaum 
656fa9e4066Sahrens 	/* process all the "freed" ranges in the file */
657bf16b11eSMatthew Ahrens 	if (dn->dn_free_ranges[txgoff] != NULL) {
658bf16b11eSMatthew Ahrens 		dnode_sync_free_range_arg_t dsfra;
659bf16b11eSMatthew Ahrens 		dsfra.dsfra_dnode = dn;
660bf16b11eSMatthew Ahrens 		dsfra.dsfra_tx = tx;
661cdb0ab79Smaybee 		mutex_enter(&dn->dn_mtx);
662bf16b11eSMatthew Ahrens 		range_tree_vacate(dn->dn_free_ranges[txgoff],
663bf16b11eSMatthew Ahrens 		    dnode_sync_free_range, &dsfra);
664bf16b11eSMatthew Ahrens 		range_tree_destroy(dn->dn_free_ranges[txgoff]);
665bf16b11eSMatthew Ahrens 		dn->dn_free_ranges[txgoff] = NULL;
666cdb0ab79Smaybee 		mutex_exit(&dn->dn_mtx);
667fa9e4066Sahrens 	}
6681934e92fSmaybee 
66943466aaeSMax Grossman 	if (freeing_dnode) {
670c717a561Smaybee 		dnode_sync_free(dn, tx);
671c717a561Smaybee 		return;
672fa9e4066Sahrens 	}
673fa9e4066Sahrens 
674e503a685SGeorge Wilson 	if (dn->dn_next_nlevels[txgoff]) {
675e503a685SGeorge Wilson 		dnode_increase_indirection(dn, tx);
676e503a685SGeorge Wilson 		dn->dn_next_nlevels[txgoff] = 0;
677e503a685SGeorge Wilson 	}
678e503a685SGeorge Wilson 
679da03de99SMark Maybee 	if (dn->dn_next_nblkptr[txgoff]) {
680da03de99SMark Maybee 		/* this should only happen on a realloc */
681da03de99SMark Maybee 		ASSERT(dn->dn_allocated_txg == tx->tx_txg);
682da03de99SMark Maybee 		if (dn->dn_next_nblkptr[txgoff] > dnp->dn_nblkptr) {
683da03de99SMark Maybee 			/* zero the new blkptrs we are gaining */
684da03de99SMark Maybee 			bzero(dnp->dn_blkptr + dnp->dn_nblkptr,
685da03de99SMark Maybee 			    sizeof (blkptr_t) *
686da03de99SMark Maybee 			    (dn->dn_next_nblkptr[txgoff] - dnp->dn_nblkptr));
687da03de99SMark Maybee #ifdef ZFS_DEBUG
688da03de99SMark Maybee 		} else {
689da03de99SMark Maybee 			int i;
690da03de99SMark Maybee 			ASSERT(dn->dn_next_nblkptr[txgoff] < dnp->dn_nblkptr);
691da03de99SMark Maybee 			/* the blkptrs we are losing better be unallocated */
692da03de99SMark Maybee 			for (i = dn->dn_next_nblkptr[txgoff];
693da03de99SMark Maybee 			    i < dnp->dn_nblkptr; i++)
694da03de99SMark Maybee 				ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[i]));
695da03de99SMark Maybee #endif
696da03de99SMark Maybee 		}
697da03de99SMark Maybee 		mutex_enter(&dn->dn_mtx);
698da03de99SMark Maybee 		dnp->dn_nblkptr = dn->dn_next_nblkptr[txgoff];
699da03de99SMark Maybee 		dn->dn_next_nblkptr[txgoff] = 0;
700da03de99SMark Maybee 		mutex_exit(&dn->dn_mtx);
701da03de99SMark Maybee 	}
702da03de99SMark Maybee 
703c717a561Smaybee 	dbuf_sync_list(list, tx);
704fa9e4066Sahrens 
70514843421SMatthew Ahrens 	if (!DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
706c717a561Smaybee 		ASSERT3P(list_head(list), ==, NULL);
707c717a561Smaybee 		dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg);
708fa9e4066Sahrens 	}
709c717a561Smaybee 
710c717a561Smaybee 	/*
711c717a561Smaybee 	 * Although we have dropped our reference to the dnode, it
712c717a561Smaybee 	 * can't be evicted until its written, and we haven't yet
713c717a561Smaybee 	 * initiated the IO for the dnode's dbuf.
714c717a561Smaybee 	 */
715fa9e4066Sahrens }
716