xref: /illumos-gate/usr/src/uts/common/fs/zfs/dnode_sync.c (revision 99a19144e82244f3426f055cc73af8a937c0135c)
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.
2494c2d0ebSMatthew Ahrens  * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
25bc9014e6SJustin 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 
638df0bcf0SPaul Dagnelie 	/* transfer dnode's block pointers to new indirect block */
648df0bcf0SPaul Dagnelie 	(void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED|DB_RF_HAVESTRUCT);
658df0bcf0SPaul Dagnelie 	ASSERT(db->db.db_data);
668df0bcf0SPaul Dagnelie 	ASSERT(arc_released(db->db_buf));
678df0bcf0SPaul Dagnelie 	ASSERT3U(sizeof (blkptr_t) * nblkptr, <=, db->db.db_size);
688df0bcf0SPaul Dagnelie 	bcopy(dn->dn_phys->dn_blkptr, db->db.db_data,
698df0bcf0SPaul Dagnelie 	    sizeof (blkptr_t) * nblkptr);
708df0bcf0SPaul Dagnelie 	arc_buf_freeze(db->db_buf);
71fa9e4066Sahrens 
72fa9e4066Sahrens 	/* set dbuf's parent pointers to new indirect buf */
73c717a561Smaybee 	for (i = 0; i < nblkptr; i++) {
74e57a022bSJustin T. Gibbs 		dmu_buf_impl_t *child =
75e57a022bSJustin T. Gibbs 		    dbuf_find(dn->dn_objset, dn->dn_object, old_toplvl, i);
76c717a561Smaybee 
77fa9e4066Sahrens 		if (child == NULL)
78fa9e4066Sahrens 			continue;
79744947dcSTom Erickson #ifdef	DEBUG
80744947dcSTom Erickson 		DB_DNODE_ENTER(child);
81744947dcSTom Erickson 		ASSERT3P(DB_DNODE(child), ==, dn);
82744947dcSTom Erickson 		DB_DNODE_EXIT(child);
83744947dcSTom Erickson #endif	/* DEBUG */
84c717a561Smaybee 		if (child->db_parent && child->db_parent != dn->dn_dbuf) {
85c717a561Smaybee 			ASSERT(child->db_parent->db_level == db->db_level);
86c717a561Smaybee 			ASSERT(child->db_blkptr !=
87c717a561Smaybee 			    &dn->dn_phys->dn_blkptr[child->db_blkid]);
88fa9e4066Sahrens 			mutex_exit(&child->db_mtx);
89fa9e4066Sahrens 			continue;
90fa9e4066Sahrens 		}
91c717a561Smaybee 		ASSERT(child->db_parent == NULL ||
92c717a561Smaybee 		    child->db_parent == dn->dn_dbuf);
93c717a561Smaybee 
94c717a561Smaybee 		child->db_parent = db;
95c717a561Smaybee 		dbuf_add_ref(db, child);
96c717a561Smaybee 		if (db->db.db_data)
97c717a561Smaybee 			child->db_blkptr = (blkptr_t *)db->db.db_data + i;
98c717a561Smaybee 		else
99c717a561Smaybee 			child->db_blkptr = NULL;
100c717a561Smaybee 		dprintf_dbuf_bp(child, child->db_blkptr,
101c717a561Smaybee 		    "changed db_blkptr to new indirect %s", "");
102fa9e4066Sahrens 
103fa9e4066Sahrens 		mutex_exit(&child->db_mtx);
104fa9e4066Sahrens 	}
105fa9e4066Sahrens 
106c717a561Smaybee 	bzero(dn->dn_phys->dn_blkptr, sizeof (blkptr_t) * nblkptr);
107fa9e4066Sahrens 
108ea8dc4b6Seschrock 	dbuf_rele(db, FTAG);
109c717a561Smaybee 
110c717a561Smaybee 	rw_exit(&dn->dn_struct_rwlock);
111fa9e4066Sahrens }
112fa9e4066Sahrens 
11343466aaeSMax Grossman static void
114fa9e4066Sahrens free_blocks(dnode_t *dn, blkptr_t *bp, int num, dmu_tx_t *tx)
115fa9e4066Sahrens {
116cdb0ab79Smaybee 	dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
117fa9e4066Sahrens 	uint64_t bytesfreed = 0;
118fa9e4066Sahrens 
119cdb0ab79Smaybee 	dprintf("ds=%p obj=%llx num=%d\n", ds, dn->dn_object, num);
120fa9e4066Sahrens 
12143466aaeSMax Grossman 	for (int i = 0; i < num; i++, bp++) {
122fa9e4066Sahrens 		if (BP_IS_HOLE(bp))
123fa9e4066Sahrens 			continue;
124fa9e4066Sahrens 
125b24ab676SJeff Bonwick 		bytesfreed += dsl_dataset_block_kill(ds, bp, tx, B_FALSE);
12699653d4eSeschrock 		ASSERT3U(bytesfreed, <=, DN_USED_BYTES(dn->dn_phys));
12743466aaeSMax Grossman 
12843466aaeSMax Grossman 		/*
12943466aaeSMax Grossman 		 * Save some useful information on the holes being
13043466aaeSMax Grossman 		 * punched, including logical size, type, and indirection
13143466aaeSMax Grossman 		 * level. Retaining birth time enables detection of when
13243466aaeSMax Grossman 		 * holes are punched for reducing the number of free
13343466aaeSMax Grossman 		 * records transmitted during a zfs send.
13443466aaeSMax Grossman 		 */
13543466aaeSMax Grossman 
13643466aaeSMax Grossman 		uint64_t lsize = BP_GET_LSIZE(bp);
13743466aaeSMax Grossman 		dmu_object_type_t type = BP_GET_TYPE(bp);
13843466aaeSMax Grossman 		uint64_t lvl = BP_GET_LEVEL(bp);
13943466aaeSMax Grossman 
140c717a561Smaybee 		bzero(bp, sizeof (blkptr_t));
14143466aaeSMax Grossman 
14243466aaeSMax Grossman 		if (spa_feature_is_active(dn->dn_objset->os_spa,
14343466aaeSMax Grossman 		    SPA_FEATURE_HOLE_BIRTH)) {
14443466aaeSMax Grossman 			BP_SET_LSIZE(bp, lsize);
14543466aaeSMax Grossman 			BP_SET_TYPE(bp, type);
14643466aaeSMax Grossman 			BP_SET_LEVEL(bp, lvl);
14743466aaeSMax Grossman 			BP_SET_BIRTH(bp, dmu_tx_get_txg(tx), 0);
14843466aaeSMax Grossman 		}
149fa9e4066Sahrens 	}
150fa9e4066Sahrens 	dnode_diduse_space(dn, -bytesfreed);
151fa9e4066Sahrens }
152fa9e4066Sahrens 
1539c9dc39aSek #ifdef ZFS_DEBUG
154fa9e4066Sahrens static void
155fa9e4066Sahrens free_verify(dmu_buf_impl_t *db, uint64_t start, uint64_t end, dmu_tx_t *tx)
156fa9e4066Sahrens {
157fa9e4066Sahrens 	int off, num;
158fa9e4066Sahrens 	int i, err, epbs;
159fa9e4066Sahrens 	uint64_t txg = tx->tx_txg;
160744947dcSTom Erickson 	dnode_t *dn;
161fa9e4066Sahrens 
162744947dcSTom Erickson 	DB_DNODE_ENTER(db);
163744947dcSTom Erickson 	dn = DB_DNODE(db);
164744947dcSTom Erickson 	epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
165fa9e4066Sahrens 	off = start - (db->db_blkid * 1<<epbs);
166fa9e4066Sahrens 	num = end - start + 1;
167fa9e4066Sahrens 
168fa9e4066Sahrens 	ASSERT3U(off, >=, 0);
169fa9e4066Sahrens 	ASSERT3U(num, >=, 0);
170fa9e4066Sahrens 	ASSERT3U(db->db_level, >, 0);
171744947dcSTom Erickson 	ASSERT3U(db->db.db_size, ==, 1 << dn->dn_phys->dn_indblkshift);
172fa9e4066Sahrens 	ASSERT3U(off+num, <=, db->db.db_size >> SPA_BLKPTRSHIFT);
173fa9e4066Sahrens 	ASSERT(db->db_blkptr != NULL);
174fa9e4066Sahrens 
175fa9e4066Sahrens 	for (i = off; i < off+num; i++) {
176fa9e4066Sahrens 		uint64_t *buf;
177fa9e4066Sahrens 		dmu_buf_impl_t *child;
178c717a561Smaybee 		dbuf_dirty_record_t *dr;
179c717a561Smaybee 		int j;
180fa9e4066Sahrens 
181fa9e4066Sahrens 		ASSERT(db->db_level == 1);
182fa9e4066Sahrens 
183744947dcSTom Erickson 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
184744947dcSTom Erickson 		err = dbuf_hold_impl(dn, db->db_level-1,
185a2cdcdd2SPaul Dagnelie 		    (db->db_blkid << epbs) + i, TRUE, FALSE, FTAG, &child);
186744947dcSTom Erickson 		rw_exit(&dn->dn_struct_rwlock);
187fa9e4066Sahrens 		if (err == ENOENT)
188fa9e4066Sahrens 			continue;
189fa9e4066Sahrens 		ASSERT(err == 0);
190fa9e4066Sahrens 		ASSERT(child->db_level == 0);
191c717a561Smaybee 		dr = child->db_last_dirty;
192c717a561Smaybee 		while (dr && dr->dr_txg > txg)
193c717a561Smaybee 			dr = dr->dr_next;
194c717a561Smaybee 		ASSERT(dr == NULL || dr->dr_txg == txg);
195c717a561Smaybee 
196c717a561Smaybee 		/* data_old better be zeroed */
197c717a561Smaybee 		if (dr) {
198c717a561Smaybee 			buf = dr->dt.dl.dr_data->b_data;
199fa9e4066Sahrens 			for (j = 0; j < child->db.db_size >> 3; j++) {
200fa9e4066Sahrens 				if (buf[j] != 0) {
201fa9e4066Sahrens 					panic("freed data not zero: "
202fa9e4066Sahrens 					    "child=%p i=%d off=%d num=%d\n",
203903a11ebSrh 					    (void *)child, i, off, num);
204fa9e4066Sahrens 				}
205fa9e4066Sahrens 			}
206fa9e4066Sahrens 		}
207fa9e4066Sahrens 
208fa9e4066Sahrens 		/*
209fa9e4066Sahrens 		 * db_data better be zeroed unless it's dirty in a
210fa9e4066Sahrens 		 * future txg.
211fa9e4066Sahrens 		 */
212fa9e4066Sahrens 		mutex_enter(&child->db_mtx);
213fa9e4066Sahrens 		buf = child->db.db_data;
214fa9e4066Sahrens 		if (buf != NULL && child->db_state != DB_FILL &&
215c717a561Smaybee 		    child->db_last_dirty == NULL) {
216fa9e4066Sahrens 			for (j = 0; j < child->db.db_size >> 3; j++) {
217fa9e4066Sahrens 				if (buf[j] != 0) {
218fa9e4066Sahrens 					panic("freed data not zero: "
219fa9e4066Sahrens 					    "child=%p i=%d off=%d num=%d\n",
220903a11ebSrh 					    (void *)child, i, off, num);
221fa9e4066Sahrens 				}
222fa9e4066Sahrens 			}
223fa9e4066Sahrens 		}
224fa9e4066Sahrens 		mutex_exit(&child->db_mtx);
225fa9e4066Sahrens 
226ea8dc4b6Seschrock 		dbuf_rele(child, FTAG);
227fa9e4066Sahrens 	}
228744947dcSTom Erickson 	DB_DNODE_EXIT(db);
229fa9e4066Sahrens }
2309c9dc39aSek #endif
231fa9e4066Sahrens 
232738e2a3cSPaul Dagnelie /*
233738e2a3cSPaul Dagnelie  * We don't usually free the indirect blocks here.  If in one txg we have a
234738e2a3cSPaul Dagnelie  * free_range and a write to the same indirect block, it's important that we
235738e2a3cSPaul Dagnelie  * preserve the hole's birth times. Therefore, we don't free any any indirect
236738e2a3cSPaul Dagnelie  * blocks in free_children().  If an indirect block happens to turn into all
237738e2a3cSPaul Dagnelie  * holes, it will be freed by dbuf_write_children_ready, which happens at a
238738e2a3cSPaul Dagnelie  * point in the syncing process where we know for certain the contents of the
239738e2a3cSPaul Dagnelie  * indirect block.
240738e2a3cSPaul Dagnelie  *
241738e2a3cSPaul Dagnelie  * However, if we're freeing a dnode, its space accounting must go to zero
242738e2a3cSPaul Dagnelie  * before we actually try to free the dnode, or we will trip an assertion. In
243738e2a3cSPaul Dagnelie  * addition, we know the case described above cannot occur, because the dnode is
244738e2a3cSPaul Dagnelie  * being freed.  Therefore, we free the indirect blocks immediately in that
245738e2a3cSPaul Dagnelie  * case.
246738e2a3cSPaul Dagnelie  */
24743466aaeSMax Grossman static void
24843466aaeSMax Grossman free_children(dmu_buf_impl_t *db, uint64_t blkid, uint64_t nblks,
249738e2a3cSPaul Dagnelie     boolean_t free_indirects, dmu_tx_t *tx)
250fa9e4066Sahrens {
251744947dcSTom Erickson 	dnode_t *dn;
252fa9e4066Sahrens 	blkptr_t *bp;
253fa9e4066Sahrens 	dmu_buf_impl_t *subdb;
2541a01181fSGeorge Wilson 	uint64_t start, end, dbstart, dbend;
2551a01181fSGeorge Wilson 	unsigned int epbs, shift, i;
256cdb0ab79Smaybee 
257cdb0ab79Smaybee 	/*
258cdb0ab79Smaybee 	 * There is a small possibility that this block will not be cached:
259cdb0ab79Smaybee 	 *   1 - if level > 1 and there are no children with level <= 1
26043466aaeSMax Grossman 	 *   2 - if this block was evicted since we read it from
26143466aaeSMax Grossman 	 *	 dmu_tx_hold_free().
262cdb0ab79Smaybee 	 */
263cdb0ab79Smaybee 	if (db->db_state != DB_CACHED)
264cdb0ab79Smaybee 		(void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
265fa9e4066Sahrens 
266*99a19144SMatthew Ahrens 	/*
267*99a19144SMatthew Ahrens 	 * If we modify this indirect block, and we are not freeing the
268*99a19144SMatthew Ahrens 	 * dnode (!free_indirects), then this indirect block needs to get
269*99a19144SMatthew Ahrens 	 * written to disk by dbuf_write().  If it is dirty, we know it will
270*99a19144SMatthew Ahrens 	 * be written (otherwise, we would have incorrect on-disk state
271*99a19144SMatthew Ahrens 	 * because the space would be freed but still referenced by the BP
272*99a19144SMatthew Ahrens 	 * in this indirect block).  Therefore we VERIFY that it is
273*99a19144SMatthew Ahrens 	 * dirty.
274*99a19144SMatthew Ahrens 	 *
275*99a19144SMatthew Ahrens 	 * Our VERIFY covers some cases that do not actually have to be
276*99a19144SMatthew Ahrens 	 * dirty, but the open-context code happens to dirty.  E.g. if the
277*99a19144SMatthew Ahrens 	 * blocks we are freeing are all holes, because in that case, we
278*99a19144SMatthew Ahrens 	 * are only freeing part of this indirect block, so it is an
279*99a19144SMatthew Ahrens 	 * ancestor of the first or last block to be freed.  The first and
280*99a19144SMatthew Ahrens 	 * last L1 indirect blocks are always dirtied by dnode_free_range().
281*99a19144SMatthew Ahrens 	 */
282*99a19144SMatthew Ahrens 	VERIFY(BP_GET_FILL(db->db_blkptr) == 0 || db->db_dirtycnt > 0);
283*99a19144SMatthew Ahrens 
2843f9d6ad7SLin Ling 	dbuf_release_bp(db);
28543466aaeSMax Grossman 	bp = db->db.db_data;
286fa9e4066Sahrens 
287744947dcSTom Erickson 	DB_DNODE_ENTER(db);
288744947dcSTom Erickson 	dn = DB_DNODE(db);
289744947dcSTom Erickson 	epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
2901a01181fSGeorge Wilson 	ASSERT3U(epbs, <, 31);
291fa9e4066Sahrens 	shift = (db->db_level - 1) * epbs;
292fa9e4066Sahrens 	dbstart = db->db_blkid << epbs;
293fa9e4066Sahrens 	start = blkid >> shift;
294fa9e4066Sahrens 	if (dbstart < start) {
295fa9e4066Sahrens 		bp += start - dbstart;
296fa9e4066Sahrens 	} else {
297fa9e4066Sahrens 		start = dbstart;
298fa9e4066Sahrens 	}
299fa9e4066Sahrens 	dbend = ((db->db_blkid + 1) << epbs) - 1;
300fa9e4066Sahrens 	end = (blkid + nblks - 1) >> shift;
301fa9e4066Sahrens 	if (dbend <= end)
302fa9e4066Sahrens 		end = dbend;
30343466aaeSMax Grossman 
304fa9e4066Sahrens 	ASSERT3U(start, <=, end);
305fa9e4066Sahrens 
306fa9e4066Sahrens 	if (db->db_level == 1) {
3079c9dc39aSek 		FREE_VERIFY(db, start, end, tx);
30843466aaeSMax Grossman 		free_blocks(dn, bp, end-start+1, tx);
30943466aaeSMax Grossman 	} else {
3101a01181fSGeorge Wilson 		for (uint64_t id = start; id <= end; id++, bp++) {
31143466aaeSMax Grossman 			if (BP_IS_HOLE(bp))
31243466aaeSMax Grossman 				continue;
31343466aaeSMax Grossman 			rw_enter(&dn->dn_struct_rwlock, RW_READER);
31443466aaeSMax Grossman 			VERIFY0(dbuf_hold_impl(dn, db->db_level - 1,
3151a01181fSGeorge Wilson 			    id, TRUE, FALSE, FTAG, &subdb));
31643466aaeSMax Grossman 			rw_exit(&dn->dn_struct_rwlock);
31743466aaeSMax Grossman 			ASSERT3P(bp, ==, subdb->db_blkptr);
31843466aaeSMax Grossman 
319738e2a3cSPaul Dagnelie 			free_children(subdb, blkid, nblks, free_indirects, tx);
32043466aaeSMax Grossman 			dbuf_rele(subdb, FTAG);
32143466aaeSMax Grossman 		}
322fa9e4066Sahrens 	}
323fa9e4066Sahrens 
324738e2a3cSPaul Dagnelie 	if (free_indirects) {
325738e2a3cSPaul Dagnelie 		for (i = 0, bp = db->db.db_data; i < 1 << epbs; i++, bp++)
326738e2a3cSPaul Dagnelie 			ASSERT(BP_IS_HOLE(bp));
32743466aaeSMax Grossman 		bzero(db->db.db_data, db->db.db_size);
32843466aaeSMax Grossman 		free_blocks(dn, db->db_blkptr, 1, tx);
329fa9e4066Sahrens 	}
33043466aaeSMax Grossman 
331744947dcSTom Erickson 	DB_DNODE_EXIT(db);
3326b4acc8bSahrens 	arc_buf_freeze(db->db_buf);
333fa9e4066Sahrens }
334fa9e4066Sahrens 
335fa9e4066Sahrens /*
336f7170741SWill Andrews  * Traverse the indicated range of the provided file
337fa9e4066Sahrens  * and "free" all the blocks contained there.
338fa9e4066Sahrens  */
339fa9e4066Sahrens static void
340bf16b11eSMatthew Ahrens dnode_sync_free_range_impl(dnode_t *dn, uint64_t blkid, uint64_t nblks,
341738e2a3cSPaul Dagnelie     boolean_t free_indirects, dmu_tx_t *tx)
342fa9e4066Sahrens {
343fa9e4066Sahrens 	blkptr_t *bp = dn->dn_phys->dn_blkptr;
344fa9e4066Sahrens 	int dnlevel = dn->dn_phys->dn_nlevels;
34543466aaeSMax Grossman 	boolean_t trunc = B_FALSE;
346fa9e4066Sahrens 
347fa9e4066Sahrens 	if (blkid > dn->dn_phys->dn_maxblkid)
348fa9e4066Sahrens 		return;
349fa9e4066Sahrens 
350fa9e4066Sahrens 	ASSERT(dn->dn_phys->dn_maxblkid < UINT64_MAX);
35143466aaeSMax Grossman 	if (blkid + nblks > dn->dn_phys->dn_maxblkid) {
352fa9e4066Sahrens 		nblks = dn->dn_phys->dn_maxblkid - blkid + 1;
35343466aaeSMax Grossman 		trunc = B_TRUE;
35443466aaeSMax Grossman 	}
355fa9e4066Sahrens 
356fa9e4066Sahrens 	/* There are no indirect blocks in the object */
357fa9e4066Sahrens 	if (dnlevel == 1) {
358fa9e4066Sahrens 		if (blkid >= dn->dn_phys->dn_nblkptr) {
359fa9e4066Sahrens 			/* this range was never made persistent */
360fa9e4066Sahrens 			return;
361fa9e4066Sahrens 		}
362fa9e4066Sahrens 		ASSERT3U(blkid + nblks, <=, dn->dn_phys->dn_nblkptr);
36343466aaeSMax Grossman 		free_blocks(dn, bp + blkid, nblks, tx);
36443466aaeSMax Grossman 	} else {
36543466aaeSMax Grossman 		int shift = (dnlevel - 1) *
36643466aaeSMax Grossman 		    (dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT);
36743466aaeSMax Grossman 		int start = blkid >> shift;
36843466aaeSMax Grossman 		int end = (blkid + nblks - 1) >> shift;
36943466aaeSMax Grossman 		dmu_buf_impl_t *db;
37043466aaeSMax Grossman 
37143466aaeSMax Grossman 		ASSERT(start < dn->dn_phys->dn_nblkptr);
37243466aaeSMax Grossman 		bp += start;
37343466aaeSMax Grossman 		for (int i = start; i <= end; i++, bp++) {
37443466aaeSMax Grossman 			if (BP_IS_HOLE(bp))
37543466aaeSMax Grossman 				continue;
37643466aaeSMax Grossman 			rw_enter(&dn->dn_struct_rwlock, RW_READER);
37743466aaeSMax Grossman 			VERIFY0(dbuf_hold_impl(dn, dnlevel - 1, i,
378a2cdcdd2SPaul Dagnelie 			    TRUE, FALSE, FTAG, &db));
37943466aaeSMax Grossman 			rw_exit(&dn->dn_struct_rwlock);
38043466aaeSMax Grossman 
381738e2a3cSPaul Dagnelie 			free_children(db, blkid, nblks, free_indirects, tx);
38243466aaeSMax Grossman 			dbuf_rele(db, FTAG);
383fa9e4066Sahrens 		}
384fa9e4066Sahrens 	}
38543466aaeSMax Grossman 
386fa9e4066Sahrens 	if (trunc) {
38743466aaeSMax Grossman 		dn->dn_phys->dn_maxblkid = blkid == 0 ? 0 : blkid - 1;
38843466aaeSMax Grossman 
389fa9e4066Sahrens 		uint64_t off = (dn->dn_phys->dn_maxblkid + 1) *
390fa9e4066Sahrens 		    (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT);
391fa9e4066Sahrens 		ASSERT(off < dn->dn_phys->dn_maxblkid ||
392fa9e4066Sahrens 		    dn->dn_phys->dn_maxblkid == 0 ||
393cdb0ab79Smaybee 		    dnode_next_offset(dn, 0, &off, 1, 1, 0) != 0);
394fa9e4066Sahrens 	}
395fa9e4066Sahrens }
396fa9e4066Sahrens 
397bf16b11eSMatthew Ahrens typedef struct dnode_sync_free_range_arg {
398bf16b11eSMatthew Ahrens 	dnode_t *dsfra_dnode;
399bf16b11eSMatthew Ahrens 	dmu_tx_t *dsfra_tx;
400738e2a3cSPaul Dagnelie 	boolean_t dsfra_free_indirects;
401bf16b11eSMatthew Ahrens } dnode_sync_free_range_arg_t;
402bf16b11eSMatthew Ahrens 
403bf16b11eSMatthew Ahrens static void
404bf16b11eSMatthew Ahrens dnode_sync_free_range(void *arg, uint64_t blkid, uint64_t nblks)
405bf16b11eSMatthew Ahrens {
406bf16b11eSMatthew Ahrens 	dnode_sync_free_range_arg_t *dsfra = arg;
407bf16b11eSMatthew Ahrens 	dnode_t *dn = dsfra->dsfra_dnode;
408bf16b11eSMatthew Ahrens 
409bf16b11eSMatthew Ahrens 	mutex_exit(&dn->dn_mtx);
410738e2a3cSPaul Dagnelie 	dnode_sync_free_range_impl(dn, blkid, nblks,
411738e2a3cSPaul Dagnelie 	    dsfra->dsfra_free_indirects, dsfra->dsfra_tx);
412bf16b11eSMatthew Ahrens 	mutex_enter(&dn->dn_mtx);
413bf16b11eSMatthew Ahrens }
414bf16b11eSMatthew Ahrens 
415ea8dc4b6Seschrock /*
416f7170741SWill Andrews  * Try to kick all the dnode's dbufs out of the cache...
417ea8dc4b6Seschrock  */
4181934e92fSmaybee void
4191934e92fSmaybee dnode_evict_dbufs(dnode_t *dn)
420ea8dc4b6Seschrock {
421bc9014e6SJustin Gibbs 	dmu_buf_impl_t db_marker;
422bc9014e6SJustin Gibbs 	dmu_buf_impl_t *db, *db_next;
423c543ec06Sahrens 
424bc9014e6SJustin Gibbs 	mutex_enter(&dn->dn_dbufs_mtx);
425bc9014e6SJustin Gibbs 	for (db = avl_first(&dn->dn_dbufs); db != NULL; db = db_next) {
426c543ec06Sahrens 
427744947dcSTom Erickson #ifdef	DEBUG
428bc9014e6SJustin Gibbs 		DB_DNODE_ENTER(db);
429bc9014e6SJustin Gibbs 		ASSERT3P(DB_DNODE(db), ==, dn);
430bc9014e6SJustin Gibbs 		DB_DNODE_EXIT(db);
431744947dcSTom Erickson #endif	/* DEBUG */
432ea8dc4b6Seschrock 
433bc9014e6SJustin Gibbs 		mutex_enter(&db->db_mtx);
434bc9014e6SJustin Gibbs 		if (db->db_state != DB_EVICTING &&
435bc9014e6SJustin Gibbs 		    refcount_is_zero(&db->db_holds)) {
436bc9014e6SJustin Gibbs 			db_marker.db_level = db->db_level;
437bc9014e6SJustin Gibbs 			db_marker.db_blkid = db->db_blkid;
438bc9014e6SJustin Gibbs 			db_marker.db_state = DB_SEARCH;
439bc9014e6SJustin Gibbs 			avl_insert_here(&dn->dn_dbufs, &db_marker, db,
440bc9014e6SJustin Gibbs 			    AVL_BEFORE);
441bc9014e6SJustin Gibbs 
442dcbf3bd6SGeorge Wilson 			dbuf_destroy(db);
443bc9014e6SJustin Gibbs 
444bc9014e6SJustin Gibbs 			db_next = AVL_NEXT(&dn->dn_dbufs, &db_marker);
445bc9014e6SJustin Gibbs 			avl_remove(&dn->dn_dbufs, &db_marker);
446bc9014e6SJustin Gibbs 		} else {
447d2058105SJustin T. Gibbs 			db->db_pending_evict = TRUE;
448bc9014e6SJustin Gibbs 			mutex_exit(&db->db_mtx);
449bc9014e6SJustin Gibbs 			db_next = AVL_NEXT(&dn->dn_dbufs, db);
450ea8dc4b6Seschrock 		}
451bc9014e6SJustin Gibbs 	}
452bc9014e6SJustin Gibbs 	mutex_exit(&dn->dn_dbufs_mtx);
453c543ec06Sahrens 
454cd485b49SJustin T. Gibbs 	dnode_evict_bonus(dn);
455cd485b49SJustin T. Gibbs }
456cd485b49SJustin T. Gibbs 
457cd485b49SJustin T. Gibbs void
458cd485b49SJustin T. Gibbs dnode_evict_bonus(dnode_t *dn)
459cd485b49SJustin T. Gibbs {
460ea8dc4b6Seschrock 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
461d2058105SJustin T. Gibbs 	if (dn->dn_bonus != NULL) {
462d2058105SJustin T. Gibbs 		if (refcount_is_zero(&dn->dn_bonus->db_holds)) {
463d2058105SJustin T. Gibbs 			mutex_enter(&dn->dn_bonus->db_mtx);
464dcbf3bd6SGeorge Wilson 			dbuf_destroy(dn->dn_bonus);
465d2058105SJustin T. Gibbs 			dn->dn_bonus = NULL;
466d2058105SJustin T. Gibbs 		} else {
467d2058105SJustin T. Gibbs 			dn->dn_bonus->db_pending_evict = TRUE;
468d2058105SJustin T. Gibbs 		}
469ea8dc4b6Seschrock 	}
470ea8dc4b6Seschrock 	rw_exit(&dn->dn_struct_rwlock);
471ea8dc4b6Seschrock }
472ea8dc4b6Seschrock 
473c717a561Smaybee static void
474c717a561Smaybee dnode_undirty_dbufs(list_t *list)
475fa9e4066Sahrens {
476c717a561Smaybee 	dbuf_dirty_record_t *dr;
477fa9e4066Sahrens 
478c717a561Smaybee 	while (dr = list_head(list)) {
479c717a561Smaybee 		dmu_buf_impl_t *db = dr->dr_dbuf;
480c717a561Smaybee 		uint64_t txg = dr->dr_txg;
481fa9e4066Sahrens 
482b24ab676SJeff Bonwick 		if (db->db_level != 0)
483b24ab676SJeff Bonwick 			dnode_undirty_dbufs(&dr->dt.di.dr_children);
484b24ab676SJeff Bonwick 
485fa9e4066Sahrens 		mutex_enter(&db->db_mtx);
486fa9e4066Sahrens 		/* XXX - use dbuf_undirty()? */
487c717a561Smaybee 		list_remove(list, dr);
488c717a561Smaybee 		ASSERT(db->db_last_dirty == dr);
489c717a561Smaybee 		db->db_last_dirty = NULL;
490c717a561Smaybee 		db->db_dirtycnt -= 1;
491fa9e4066Sahrens 		if (db->db_level == 0) {
4920a586ceaSMark Shellenbaum 			ASSERT(db->db_blkid == DMU_BONUS_BLKID ||
493c717a561Smaybee 			    dr->dt.dl.dr_data == db->db_buf);
494c717a561Smaybee 			dbuf_unoverride(dr);
495d2b3cbbdSJorgen Lundman 		} else {
496d2b3cbbdSJorgen Lundman 			mutex_destroy(&dr->dt.di.dr_mtx);
497d2b3cbbdSJorgen Lundman 			list_destroy(&dr->dt.di.dr_children);
498fa9e4066Sahrens 		}
499c717a561Smaybee 		kmem_free(dr, sizeof (dbuf_dirty_record_t));
500b24ab676SJeff Bonwick 		dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg);
501fa9e4066Sahrens 	}
502c717a561Smaybee }
503fa9e4066Sahrens 
504c717a561Smaybee static void
505c717a561Smaybee dnode_sync_free(dnode_t *dn, dmu_tx_t *tx)
506c717a561Smaybee {
507c717a561Smaybee 	int txgoff = tx->tx_txg & TXG_MASK;
508c717a561Smaybee 
509c717a561Smaybee 	ASSERT(dmu_tx_is_syncing(tx));
510c717a561Smaybee 
511cdb0ab79Smaybee 	/*
512cdb0ab79Smaybee 	 * Our contents should have been freed in dnode_sync() by the
513cdb0ab79Smaybee 	 * free range record inserted by the caller of dnode_free().
514cdb0ab79Smaybee 	 */
515fb09f5aaSMadhav Suresh 	ASSERT0(DN_USED_BYTES(dn->dn_phys));
516cdb0ab79Smaybee 	ASSERT(BP_IS_HOLE(dn->dn_phys->dn_blkptr));
517cdb0ab79Smaybee 
518c717a561Smaybee 	dnode_undirty_dbufs(&dn->dn_dirty_records[txgoff]);
5191934e92fSmaybee 	dnode_evict_dbufs(dn);
520ea8dc4b6Seschrock 
521ea8dc4b6Seschrock 	/*
522ea8dc4b6Seschrock 	 * XXX - It would be nice to assert this, but we may still
523ea8dc4b6Seschrock 	 * have residual holds from async evictions from the arc...
524ea8dc4b6Seschrock 	 *
52555434c77Sek 	 * zfs_obj_to_path() also depends on this being
52655434c77Sek 	 * commented out.
52755434c77Sek 	 *
528ea8dc4b6Seschrock 	 * ASSERT3U(refcount_count(&dn->dn_holds), ==, 1);
529ea8dc4b6Seschrock 	 */
530fa9e4066Sahrens 
531fa9e4066Sahrens 	/* Undirty next bits */
532fa9e4066Sahrens 	dn->dn_next_nlevels[txgoff] = 0;
533fa9e4066Sahrens 	dn->dn_next_indblkshift[txgoff] = 0;
534c543ec06Sahrens 	dn->dn_next_blksz[txgoff] = 0;
535fa9e4066Sahrens 
536fa9e4066Sahrens 	/* ASSERT(blkptrs are zero); */
537fa9e4066Sahrens 	ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE);
538fa9e4066Sahrens 	ASSERT(dn->dn_type != DMU_OT_NONE);
539fa9e4066Sahrens 
540fa9e4066Sahrens 	ASSERT(dn->dn_free_txg > 0);
541fa9e4066Sahrens 	if (dn->dn_allocated_txg != dn->dn_free_txg)
54243466aaeSMax Grossman 		dmu_buf_will_dirty(&dn->dn_dbuf->db, tx);
543fa9e4066Sahrens 	bzero(dn->dn_phys, sizeof (dnode_phys_t));
544fa9e4066Sahrens 
545fa9e4066Sahrens 	mutex_enter(&dn->dn_mtx);
546fa9e4066Sahrens 	dn->dn_type = DMU_OT_NONE;
547fa9e4066Sahrens 	dn->dn_maxblkid = 0;
548fa9e4066Sahrens 	dn->dn_allocated_txg = 0;
549758f6e0bSgw 	dn->dn_free_txg = 0;
5500a586ceaSMark Shellenbaum 	dn->dn_have_spill = B_FALSE;
551fa9e4066Sahrens 	mutex_exit(&dn->dn_mtx);
552fa9e4066Sahrens 
553ea8dc4b6Seschrock 	ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
554fa9e4066Sahrens 
555fa9e4066Sahrens 	dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg);
556fa9e4066Sahrens 	/*
557fa9e4066Sahrens 	 * Now that we've released our hold, the dnode may
558fa9e4066Sahrens 	 * be evicted, so we musn't access it.
559fa9e4066Sahrens 	 */
560fa9e4066Sahrens }
561fa9e4066Sahrens 
562fa9e4066Sahrens /*
563c717a561Smaybee  * Write out the dnode's dirty buffers.
564fa9e4066Sahrens  */
565c717a561Smaybee void
566c717a561Smaybee dnode_sync(dnode_t *dn, dmu_tx_t *tx)
567fa9e4066Sahrens {
568fa9e4066Sahrens 	dnode_phys_t *dnp = dn->dn_phys;
569c717a561Smaybee 	int txgoff = tx->tx_txg & TXG_MASK;
570c717a561Smaybee 	list_t *list = &dn->dn_dirty_records[txgoff];
57114843421SMatthew Ahrens 	static const dnode_phys_t zerodn = { 0 };
5720a586ceaSMark Shellenbaum 	boolean_t kill_spill = B_FALSE;
573fa9e4066Sahrens 
574fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
575fa9e4066Sahrens 	ASSERT(dnp->dn_type != DMU_OT_NONE || dn->dn_allocated_txg);
57614843421SMatthew Ahrens 	ASSERT(dnp->dn_type != DMU_OT_NONE ||
57714843421SMatthew Ahrens 	    bcmp(dnp, &zerodn, DNODE_SIZE) == 0);
5789c9dc39aSek 	DNODE_VERIFY(dn);
579c543ec06Sahrens 
580c717a561Smaybee 	ASSERT(dn->dn_dbuf == NULL || arc_released(dn->dn_dbuf->db_buf));
581fa9e4066Sahrens 
58214843421SMatthew Ahrens 	if (dmu_objset_userused_enabled(dn->dn_objset) &&
58314843421SMatthew Ahrens 	    !DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
5840a586ceaSMark Shellenbaum 		mutex_enter(&dn->dn_mtx);
5850a586ceaSMark Shellenbaum 		dn->dn_oldused = DN_USED_BYTES(dn->dn_phys);
5860a586ceaSMark Shellenbaum 		dn->dn_oldflags = dn->dn_phys->dn_flags;
58714843421SMatthew Ahrens 		dn->dn_phys->dn_flags |= DNODE_FLAG_USERUSED_ACCOUNTED;
5880a586ceaSMark Shellenbaum 		mutex_exit(&dn->dn_mtx);
58906e0070dSMark Shellenbaum 		dmu_objset_userquota_get_ids(dn, B_FALSE, tx);
59014843421SMatthew Ahrens 	} else {
59114843421SMatthew Ahrens 		/* Once we account for it, we should always account for it. */
59214843421SMatthew Ahrens 		ASSERT(!(dn->dn_phys->dn_flags &
59314843421SMatthew Ahrens 		    DNODE_FLAG_USERUSED_ACCOUNTED));
59414843421SMatthew Ahrens 	}
59514843421SMatthew Ahrens 
596fa9e4066Sahrens 	mutex_enter(&dn->dn_mtx);
597fa9e4066Sahrens 	if (dn->dn_allocated_txg == tx->tx_txg) {
598fa9e4066Sahrens 		/* The dnode is newly allocated or reallocated */
599fa9e4066Sahrens 		if (dnp->dn_type == DMU_OT_NONE) {
600fa9e4066Sahrens 			/* this is a first alloc, not a realloc */
601fa9e4066Sahrens 			dnp->dn_nlevels = 1;
602da03de99SMark Maybee 			dnp->dn_nblkptr = dn->dn_nblkptr;
603fa9e4066Sahrens 		}
604fa9e4066Sahrens 
605fa9e4066Sahrens 		dnp->dn_type = dn->dn_type;
606fa9e4066Sahrens 		dnp->dn_bonustype = dn->dn_bonustype;
607fa9e4066Sahrens 		dnp->dn_bonuslen = dn->dn_bonuslen;
608fa9e4066Sahrens 	}
609c717a561Smaybee 	ASSERT(dnp->dn_nlevels > 1 ||
610f676ed34Sahrens 	    BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
6115d7b4d43SMatthew Ahrens 	    BP_IS_EMBEDDED(&dnp->dn_blkptr[0]) ||
612f676ed34Sahrens 	    BP_GET_LSIZE(&dnp->dn_blkptr[0]) ==
613f676ed34Sahrens 	    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
6145d7b4d43SMatthew Ahrens 	ASSERT(dnp->dn_nlevels < 2 ||
6155d7b4d43SMatthew Ahrens 	    BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
6165d7b4d43SMatthew Ahrens 	    BP_GET_LSIZE(&dnp->dn_blkptr[0]) == 1 << dnp->dn_indblkshift);
617f676ed34Sahrens 
6182acef22dSMatthew Ahrens 	if (dn->dn_next_type[txgoff] != 0) {
6192acef22dSMatthew Ahrens 		dnp->dn_type = dn->dn_type;
6202acef22dSMatthew Ahrens 		dn->dn_next_type[txgoff] = 0;
6212acef22dSMatthew Ahrens 	}
6222acef22dSMatthew Ahrens 
6232acef22dSMatthew Ahrens 	if (dn->dn_next_blksz[txgoff] != 0) {
624c543ec06Sahrens 		ASSERT(P2PHASE(dn->dn_next_blksz[txgoff],
625fa9e4066Sahrens 		    SPA_MINBLOCKSIZE) == 0);
626f676ed34Sahrens 		ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
627cdb0ab79Smaybee 		    dn->dn_maxblkid == 0 || list_head(list) != NULL ||
628347a31bcSahrens 		    dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT ==
629bf16b11eSMatthew Ahrens 		    dnp->dn_datablkszsec ||
63086714001SSerapheim Dimitropoulos 		    !range_tree_is_empty(dn->dn_free_ranges[txgoff]));
631fa9e4066Sahrens 		dnp->dn_datablkszsec =
632c543ec06Sahrens 		    dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT;
633c543ec06Sahrens 		dn->dn_next_blksz[txgoff] = 0;
634fa9e4066Sahrens 	}
635fa9e4066Sahrens 
6362acef22dSMatthew Ahrens 	if (dn->dn_next_bonuslen[txgoff] != 0) {
6371934e92fSmaybee 		if (dn->dn_next_bonuslen[txgoff] == DN_ZERO_BONUSLEN)
6381934e92fSmaybee 			dnp->dn_bonuslen = 0;
6391934e92fSmaybee 		else
6401934e92fSmaybee 			dnp->dn_bonuslen = dn->dn_next_bonuslen[txgoff];
6411934e92fSmaybee 		ASSERT(dnp->dn_bonuslen <= DN_MAX_BONUSLEN);
6421934e92fSmaybee 		dn->dn_next_bonuslen[txgoff] = 0;
6431934e92fSmaybee 	}
6441934e92fSmaybee 
6452acef22dSMatthew Ahrens 	if (dn->dn_next_bonustype[txgoff] != 0) {
646ad135b5dSChristopher Siden 		ASSERT(DMU_OT_IS_VALID(dn->dn_next_bonustype[txgoff]));
6470a586ceaSMark Shellenbaum 		dnp->dn_bonustype = dn->dn_next_bonustype[txgoff];
6480a586ceaSMark Shellenbaum 		dn->dn_next_bonustype[txgoff] = 0;
6490a586ceaSMark Shellenbaum 	}
6500a586ceaSMark Shellenbaum 
65143466aaeSMax Grossman 	boolean_t freeing_dnode = dn->dn_free_txg > 0 &&
65243466aaeSMax Grossman 	    dn->dn_free_txg <= tx->tx_txg;
65343466aaeSMax Grossman 
6540a586ceaSMark Shellenbaum 	/*
655e6518318SMatthew Ahrens 	 * Remove the spill block if we have been explicitly asked to
656e6518318SMatthew Ahrens 	 * remove it, or if the object is being removed.
6570a586ceaSMark Shellenbaum 	 */
658e6518318SMatthew Ahrens 	if (dn->dn_rm_spillblk[txgoff] || freeing_dnode) {
659e6518318SMatthew Ahrens 		if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR)
6600a586ceaSMark Shellenbaum 			kill_spill = B_TRUE;
6610a586ceaSMark Shellenbaum 		dn->dn_rm_spillblk[txgoff] = 0;
6620a586ceaSMark Shellenbaum 	}
6630a586ceaSMark Shellenbaum 
6642acef22dSMatthew Ahrens 	if (dn->dn_next_indblkshift[txgoff] != 0) {
665fa9e4066Sahrens 		ASSERT(dnp->dn_nlevels == 1);
666fa9e4066Sahrens 		dnp->dn_indblkshift = dn->dn_next_indblkshift[txgoff];
667fa9e4066Sahrens 		dn->dn_next_indblkshift[txgoff] = 0;
668fa9e4066Sahrens 	}
669fa9e4066Sahrens 
670fa9e4066Sahrens 	/*
671fa9e4066Sahrens 	 * Just take the live (open-context) values for checksum and compress.
672fa9e4066Sahrens 	 * Strictly speaking it's a future leak, but nothing bad happens if we
673fa9e4066Sahrens 	 * start using the new checksum or compress algorithm a little early.
674fa9e4066Sahrens 	 */
675fa9e4066Sahrens 	dnp->dn_checksum = dn->dn_checksum;
676fa9e4066Sahrens 	dnp->dn_compress = dn->dn_compress;
677fa9e4066Sahrens 
678fa9e4066Sahrens 	mutex_exit(&dn->dn_mtx);
679fa9e4066Sahrens 
6800a586ceaSMark Shellenbaum 	if (kill_spill) {
68143466aaeSMax Grossman 		free_blocks(dn, &dn->dn_phys->dn_spill, 1, tx);
6820a586ceaSMark Shellenbaum 		mutex_enter(&dn->dn_mtx);
6830a586ceaSMark Shellenbaum 		dnp->dn_flags &= ~DNODE_FLAG_SPILL_BLKPTR;
6840a586ceaSMark Shellenbaum 		mutex_exit(&dn->dn_mtx);
6850a586ceaSMark Shellenbaum 	}
6860a586ceaSMark Shellenbaum 
687fa9e4066Sahrens 	/* process all the "freed" ranges in the file */
688bf16b11eSMatthew Ahrens 	if (dn->dn_free_ranges[txgoff] != NULL) {
689bf16b11eSMatthew Ahrens 		dnode_sync_free_range_arg_t dsfra;
690bf16b11eSMatthew Ahrens 		dsfra.dsfra_dnode = dn;
691bf16b11eSMatthew Ahrens 		dsfra.dsfra_tx = tx;
692738e2a3cSPaul Dagnelie 		dsfra.dsfra_free_indirects = freeing_dnode;
693738e2a3cSPaul Dagnelie 		if (freeing_dnode) {
694738e2a3cSPaul Dagnelie 			ASSERT(range_tree_contains(dn->dn_free_ranges[txgoff],
695738e2a3cSPaul Dagnelie 			    0, dn->dn_maxblkid + 1));
696738e2a3cSPaul Dagnelie 		}
697cdb0ab79Smaybee 		mutex_enter(&dn->dn_mtx);
698bf16b11eSMatthew Ahrens 		range_tree_vacate(dn->dn_free_ranges[txgoff],
699bf16b11eSMatthew Ahrens 		    dnode_sync_free_range, &dsfra);
700bf16b11eSMatthew Ahrens 		range_tree_destroy(dn->dn_free_ranges[txgoff]);
701bf16b11eSMatthew Ahrens 		dn->dn_free_ranges[txgoff] = NULL;
702cdb0ab79Smaybee 		mutex_exit(&dn->dn_mtx);
703fa9e4066Sahrens 	}
7041934e92fSmaybee 
70543466aaeSMax Grossman 	if (freeing_dnode) {
706af346df5SNed Bass 		dn->dn_objset->os_freed_dnodes++;
707c717a561Smaybee 		dnode_sync_free(dn, tx);
708c717a561Smaybee 		return;
709fa9e4066Sahrens 	}
710fa9e4066Sahrens 
711e503a685SGeorge Wilson 	if (dn->dn_next_nlevels[txgoff]) {
712e503a685SGeorge Wilson 		dnode_increase_indirection(dn, tx);
713e503a685SGeorge Wilson 		dn->dn_next_nlevels[txgoff] = 0;
714e503a685SGeorge Wilson 	}
715e503a685SGeorge Wilson 
716da03de99SMark Maybee 	if (dn->dn_next_nblkptr[txgoff]) {
717da03de99SMark Maybee 		/* this should only happen on a realloc */
718da03de99SMark Maybee 		ASSERT(dn->dn_allocated_txg == tx->tx_txg);
719da03de99SMark Maybee 		if (dn->dn_next_nblkptr[txgoff] > dnp->dn_nblkptr) {
720da03de99SMark Maybee 			/* zero the new blkptrs we are gaining */
721da03de99SMark Maybee 			bzero(dnp->dn_blkptr + dnp->dn_nblkptr,
722da03de99SMark Maybee 			    sizeof (blkptr_t) *
723da03de99SMark Maybee 			    (dn->dn_next_nblkptr[txgoff] - dnp->dn_nblkptr));
724da03de99SMark Maybee #ifdef ZFS_DEBUG
725da03de99SMark Maybee 		} else {
726da03de99SMark Maybee 			int i;
727da03de99SMark Maybee 			ASSERT(dn->dn_next_nblkptr[txgoff] < dnp->dn_nblkptr);
728da03de99SMark Maybee 			/* the blkptrs we are losing better be unallocated */
729da03de99SMark Maybee 			for (i = dn->dn_next_nblkptr[txgoff];
730da03de99SMark Maybee 			    i < dnp->dn_nblkptr; i++)
731da03de99SMark Maybee 				ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[i]));
732da03de99SMark Maybee #endif
733da03de99SMark Maybee 		}
734da03de99SMark Maybee 		mutex_enter(&dn->dn_mtx);
735da03de99SMark Maybee 		dnp->dn_nblkptr = dn->dn_next_nblkptr[txgoff];
736da03de99SMark Maybee 		dn->dn_next_nblkptr[txgoff] = 0;
737da03de99SMark Maybee 		mutex_exit(&dn->dn_mtx);
738da03de99SMark Maybee 	}
739da03de99SMark Maybee 
74046e1baa6SMatthew Ahrens 	dbuf_sync_list(list, dn->dn_phys->dn_nlevels - 1, tx);
741fa9e4066Sahrens 
74214843421SMatthew Ahrens 	if (!DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
743c717a561Smaybee 		ASSERT3P(list_head(list), ==, NULL);
744c717a561Smaybee 		dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg);
745fa9e4066Sahrens 	}
746c717a561Smaybee 
747c717a561Smaybee 	/*
748c717a561Smaybee 	 * Although we have dropped our reference to the dnode, it
749c717a561Smaybee 	 * can't be evicted until its written, and we haven't yet
750c717a561Smaybee 	 * initiated the IO for the dnode's dbuf.
751c717a561Smaybee 	 */
752fa9e4066Sahrens }
753