1fa9e406ahrens/*
2fa9e406ahrens * CDDL HEADER START
3fa9e406ahrens *
4fa9e406ahrens * The contents of this file are subject to the terms of the
5ea8dc4beschrock * Common Development and Distribution License (the "License").
6ea8dc4beschrock * You may not use this file except in compliance with the License.
7fa9e406ahrens *
8fa9e406ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e406ahrens * or http://www.opensolaris.org/os/licensing.
10fa9e406ahrens * See the License for the specific language governing permissions
11fa9e406ahrens * and limitations under the License.
12fa9e406ahrens *
13fa9e406ahrens * When distributing Covered Code, include this CDDL HEADER in each
14fa9e406ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e406ahrens * If applicable, add the following below this CDDL HEADER, with the
16fa9e406ahrens * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e406ahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e406ahrens *
19fa9e406ahrens * CDDL HEADER END
20fa9e406ahrens */
21ad135b5Christopher Siden
22fa9e406ahrens/*
2306e0070Mark Shellenbaum * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24c2919acMatthew Ahrens * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
25bc9014eJustin Gibbs * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
26fa9e406ahrens */
27fa9e406ahrens
28fa9e406ahrens#include <sys/zfs_context.h>
29fa9e406ahrens#include <sys/dbuf.h>
30fa9e406ahrens#include <sys/dnode.h>
31fa9e406ahrens#include <sys/dmu.h>
32fa9e406ahrens#include <sys/dmu_tx.h>
33fa9e406ahrens#include <sys/dmu_objset.h>
34eb63303Tom Caputi#include <sys/dmu_recv.h>
35fa9e406ahrens#include <sys/dsl_dataset.h>
36fa9e406ahrens#include <sys/spa.h>
37bf16b11Matthew Ahrens#include <sys/range_tree.h>
3843466aaMax Grossman#include <sys/zfeature.h>
39fa9e406ahrens
40fa9e406ahrensstatic void
41fa9e406ahrensdnode_increase_indirection(dnode_t *dn, dmu_tx_t *tx)
42fa9e406ahrens{
43fa9e406ahrens	dmu_buf_impl_t *db;
44c717a56maybee	int txgoff = tx->tx_txg & TXG_MASK;
45c717a56maybee	int nblkptr = dn->dn_phys->dn_nblkptr;
46c717a56maybee	int old_toplvl = dn->dn_phys->dn_nlevels - 1;
47c717a56maybee	int new_level = dn->dn_next_nlevels[txgoff];
48fa9e406ahrens	int i;
49fa9e406ahrens
50c717a56maybee	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
51c717a56maybee
52c717a56maybee	/* this dnode can't be paged out because it's dirty */
53fa9e406ahrens	ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE);
54fa9e406ahrens	ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
55c717a56maybee	ASSERT(new_level > 1 && dn->dn_phys->dn_nlevels > 0);
56fa9e406ahrens
57fa9e406ahrens	db = dbuf_hold_level(dn, dn->dn_phys->dn_nlevels, 0, FTAG);
58ea8dc4beschrock	ASSERT(db != NULL);
59c717a56maybee
60c717a56maybee	dn->dn_phys->dn_nlevels = new_level;
61f82bfe1gw	dprintf("os=%p obj=%llu, increase to %d\n", dn->dn_objset,
62f82bfe1gw	    dn->dn_object, dn->dn_phys->dn_nlevels);
63c717a56maybee
648df0bcfPaul Dagnelie	/* transfer dnode's block pointers to new indirect block */
658df0bcfPaul Dagnelie	(void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED|DB_RF_HAVESTRUCT);
668df0bcfPaul Dagnelie	ASSERT(db->db.db_data);
678df0bcfPaul Dagnelie	ASSERT(arc_released(db->db_buf));
688df0bcfPaul Dagnelie	ASSERT3U(sizeof (blkptr_t) * nblkptr, <=, db->db.db_size);
698df0bcfPaul Dagnelie	bcopy(dn->dn_phys->dn_blkptr, db->db.db_data,
708df0bcfPaul Dagnelie	    sizeof (blkptr_t) * nblkptr);
718df0bcfPaul Dagnelie	arc_buf_freeze(db->db_buf);
72fa9e406ahrens
73fa9e406ahrens	/* set dbuf's parent pointers to new indirect buf */
74c717a56maybee	for (i = 0; i < nblkptr; i++) {
75e57a022Justin T. Gibbs		dmu_buf_impl_t *child =
76e57a022Justin T. Gibbs		    dbuf_find(dn->dn_objset, dn->dn_object, old_toplvl, i);
77c717a56maybee
78fa9e406ahrens		if (child == NULL)
79fa9e406ahrens			continue;
80744947dTom Erickson#ifdef	DEBUG
81744947dTom Erickson		DB_DNODE_ENTER(child);
82744947dTom Erickson		ASSERT3P(DB_DNODE(child), ==, dn);
83744947dTom Erickson		DB_DNODE_EXIT(child);
84744947dTom Erickson#endif	/* DEBUG */
85c717a56maybee		if (child->db_parent && child->db_parent != dn->dn_dbuf) {
86c717a56maybee			ASSERT(child->db_parent->db_level == db->db_level);
87c717a56maybee			ASSERT(child->db_blkptr !=
88c717a56maybee			    &dn->dn_phys->dn_blkptr[child->db_blkid]);
89fa9e406ahrens			mutex_exit(&child->db_mtx);
90fa9e406ahrens			continue;
91fa9e406ahrens		}
92c717a56maybee		ASSERT(child->db_parent == NULL ||
93c717a56maybee		    child->db_parent == dn->dn_dbuf);
94c717a56maybee
95c717a56maybee		child->db_parent = db;
96c717a56maybee		dbuf_add_ref(db, child);
97c717a56maybee		if (db->db.db_data)
98c717a56maybee			child->db_blkptr = (blkptr_t *)db->db.db_data + i;
99c717a56maybee		else
100c717a56maybee			child->db_blkptr = NULL;
101c717a56maybee		dprintf_dbuf_bp(child, child->db_blkptr,
102c717a56maybee		    "changed db_blkptr to new indirect %s", "");
103fa9e406ahrens
104fa9e406ahrens		mutex_exit(&child->db_mtx);
105fa9e406ahrens	}
106fa9e406ahrens
107c717a56maybee	bzero(dn->dn_phys->dn_blkptr, sizeof (blkptr_t) * nblkptr);
108fa9e406ahrens
109ea8dc4beschrock	dbuf_rele(db, FTAG);
110c717a56maybee
111c717a56maybee	rw_exit(&dn->dn_struct_rwlock);
112fa9e406ahrens}
113fa9e406ahrens
11443466aaMax Grossmanstatic void
115fa9e406ahrensfree_blocks(dnode_t *dn, blkptr_t *bp, int num, dmu_tx_t *tx)
116fa9e406ahrens{
117cdb0ab7maybee	dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
118fa9e406ahrens	uint64_t bytesfreed = 0;
119fa9e406ahrens
120cdb0ab7maybee	dprintf("ds=%p obj=%llx num=%d\n", ds, dn->dn_object, num);
121fa9e406ahrens
12243466aaMax Grossman	for (int i = 0; i < num; i++, bp++) {
123fa9e406ahrens		if (BP_IS_HOLE(bp))
124fa9e406ahrens			continue;
125fa9e406ahrens
126b24ab67Jeff Bonwick		bytesfreed += dsl_dataset_block_kill(ds, bp, tx, B_FALSE);
12799653d4eschrock		ASSERT3U(bytesfreed, <=, DN_USED_BYTES(dn->dn_phys));
12843466aaMax Grossman
12943466aaMax Grossman		/*
13043466aaMax Grossman		 * Save some useful information on the holes being
13143466aaMax Grossman		 * punched, including logical size, type, and indirection
13243466aaMax Grossman		 * level. Retaining birth time enables detection of when
13343466aaMax Grossman		 * holes are punched for reducing the number of free
13443466aaMax Grossman		 * records transmitted during a zfs send.
13543466aaMax Grossman		 */
13643466aaMax Grossman
13743466aaMax Grossman		uint64_t lsize = BP_GET_LSIZE(bp);
13843466aaMax Grossman		dmu_object_type_t type = BP_GET_TYPE(bp);
13943466aaMax Grossman		uint64_t lvl = BP_GET_LEVEL(bp);
14043466aaMax Grossman
141c717a56maybee		bzero(bp, sizeof (blkptr_t));
14243466aaMax Grossman
14343466aaMax Grossman		if (spa_feature_is_active(dn->dn_objset->os_spa,
14443466aaMax Grossman		    SPA_FEATURE_HOLE_BIRTH)) {
14543466aaMax Grossman			BP_SET_LSIZE(bp, lsize);
14643466aaMax Grossman			BP_SET_TYPE(bp, type);
14743466aaMax Grossman			BP_SET_LEVEL(bp, lvl);
14843466aaMax Grossman			BP_SET_BIRTH(bp, dmu_tx_get_txg(tx), 0);
14943466aaMax Grossman		}
150fa9e406ahrens	}
151fa9e406ahrens	dnode_diduse_space(dn, -bytesfreed);
152fa9e406ahrens}
153fa9e406ahrens
1549c9dc39ek#ifdef ZFS_DEBUG
155fa9e406ahrensstatic void
156fa9e406ahrensfree_verify(dmu_buf_impl_t *db, uint64_t start, uint64_t end, dmu_tx_t *tx)
157fa9e406ahrens{
158fa9e406ahrens	int off, num;
159fa9e406ahrens	int i, err, epbs;
160fa9e406ahrens	uint64_t txg = tx->tx_txg;
161744947dTom Erickson	dnode_t *dn;
162fa9e406ahrens
163744947dTom Erickson	DB_DNODE_ENTER(db);
164744947dTom Erickson	dn = DB_DNODE(db);
165744947dTom Erickson	epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
166fa9e406ahrens	off = start - (db->db_blkid * 1<<epbs);
167fa9e406ahrens	num = end - start + 1;
168fa9e406ahrens
169fa9e406ahrens	ASSERT3U(off, >=, 0);
170fa9e406ahrens	ASSERT3U(num, >=, 0);
171fa9e406ahrens	ASSERT3U(db->db_level, >, 0);
172744947dTom Erickson	ASSERT3U(db->db.db_size, ==, 1 << dn->dn_phys->dn_indblkshift);
173fa9e406ahrens	ASSERT3U(off+num, <=, db->db.db_size >> SPA_BLKPTRSHIFT);
174fa9e406ahrens	ASSERT(db->db_blkptr != NULL);
175fa9e406ahrens
176fa9e406ahrens	for (i = off; i < off+num; i++) {
177fa9e406ahrens		uint64_t *buf;
178fa9e406ahrens		dmu_buf_impl_t *child;
179c717a56maybee		dbuf_dirty_record_t *dr;
180c717a56maybee		int j;
181fa9e406ahrens
182fa9e406ahrens		ASSERT(db->db_level == 1);
183fa9e406ahrens
184744947dTom Erickson		rw_enter(&dn->dn_struct_rwlock, RW_READER);
185744947dTom Erickson		err = dbuf_hold_impl(dn, db->db_level-1,
186a2cdcddPaul Dagnelie		    (db->db_blkid << epbs) + i, TRUE, FALSE, FTAG, &child);
187744947dTom Erickson		rw_exit(&dn->dn_struct_rwlock);
188fa9e406ahrens		if (err == ENOENT)
189fa9e406ahrens			continue;
190fa9e406ahrens		ASSERT(err == 0);
191fa9e406ahrens		ASSERT(child->db_level == 0);
192c717a56maybee		dr = child->db_last_dirty;
193c717a56maybee		while (dr && dr->dr_txg > txg)
194c717a56maybee			dr = dr->dr_next;
195c717a56maybee		ASSERT(dr == NULL || dr->dr_txg == txg);
196c717a56maybee
197c717a56maybee		/* data_old better be zeroed */
198c717a56maybee		if (dr) {
199c717a56maybee			buf = dr->dt.dl.dr_data->b_data;
200fa9e406ahrens			for (j = 0; j < child->db.db_size >> 3; j++) {
201fa9e406ahrens				if (buf[j] != 0) {
202fa9e406ahrens					panic("freed data not zero: "
203fa9e406ahrens					    "child=%p i=%d off=%d num=%d\n",
204903a11erh					    (void *)child, i, off, num);
205fa9e406ahrens				}
206fa9e406ahrens			}
207fa9e406ahrens		}
208fa9e406ahrens
209fa9e406ahrens		/*
210fa9e406ahrens		 * db_data better be zeroed unless it's dirty in a
211fa9e406ahrens		 * future txg.
212fa9e406ahrens		 */
213fa9e406ahrens		mutex_enter(&child->db_mtx);
214fa9e406ahrens		buf = child->db.db_data;
215fa9e406ahrens		if (buf != NULL && child->db_state != DB_FILL &&
216c717a56maybee		    child->db_last_dirty == NULL) {
217fa9e406ahrens			for (j = 0; j < child->db.db_size >> 3; j++) {
218fa9e406ahrens				if (buf[j] != 0) {
219fa9e406ahrens					panic("freed data not zero: "
220fa9e406ahrens					    "child=%p i=%d off=%d num=%d\n",
221903a11erh					    (void *)child, i, off, num);
222fa9e406ahrens				}
223fa9e406ahrens			}
224fa9e406ahrens		}
225fa9e406ahrens		mutex_exit(&child->db_mtx);
226fa9e406ahrens
227ea8dc4beschrock		dbuf_rele(child, FTAG);
228fa9e406ahrens	}
229744947dTom Erickson	DB_DNODE_EXIT(db);
230fa9e406ahrens}
2319c9dc39ek#endif
232fa9e406ahrens
233738e2a3Paul Dagnelie/*
234738e2a3Paul Dagnelie * We don't usually free the indirect blocks here.  If in one txg we have a
235738e2a3Paul Dagnelie * free_range and a write to the same indirect block, it's important that we
236738e2a3Paul Dagnelie * preserve the hole's birth times. Therefore, we don't free any any indirect
237738e2a3Paul Dagnelie * blocks in free_children().  If an indirect block happens to turn into all
238738e2a3Paul Dagnelie * holes, it will be freed by dbuf_write_children_ready, which happens at a
239738e2a3Paul Dagnelie * point in the syncing process where we know for certain the contents of the
240738e2a3Paul Dagnelie * indirect block.
241738e2a3Paul Dagnelie *
242738e2a3Paul Dagnelie * However, if we're freeing a dnode, its space accounting must go to zero
243738e2a3Paul Dagnelie * before we actually try to free the dnode, or we will trip an assertion. In
244738e2a3Paul Dagnelie * addition, we know the case described above cannot occur, because the dnode is
245738e2a3Paul Dagnelie * being freed.  Therefore, we free the indirect blocks immediately in that
246738e2a3Paul Dagnelie * case.
247738e2a3Paul Dagnelie */
24843466aaMax Grossmanstatic void
24943466aaMax Grossmanfree_children(dmu_buf_impl_t *db, uint64_t blkid, uint64_t nblks,
250738e2a3Paul Dagnelie    boolean_t free_indirects, dmu_tx_t *tx)
251fa9e406ahrens{
252744947dTom Erickson	dnode_t *dn;
253fa9e406ahrens	blkptr_t *bp;
254fa9e406ahrens	dmu_buf_impl_t *subdb;
2551a01181George Wilson	uint64_t start, end, dbstart, dbend;
2561a01181George Wilson	unsigned int epbs, shift, i;
257cdb0ab7maybee
258cdb0ab7maybee	/*
259cdb0ab7maybee	 * There is a small possibility that this block will not be cached:
260cdb0ab7maybee	 *   1 - if level > 1 and there are no children with level <= 1
26143466aaMax Grossman	 *   2 - if this block was evicted since we read it from
26243466aaMax Grossman	 *	 dmu_tx_hold_free().
263cdb0ab7maybee	 */
264cdb0ab7maybee	if (db->db_state != DB_CACHED)
265cdb0ab7maybee		(void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
266fa9e406ahrens
26799a1914Matthew Ahrens	/*
26899a1914Matthew Ahrens	 * If we modify this indirect block, and we are not freeing the
26999a1914Matthew Ahrens	 * dnode (!free_indirects), then this indirect block needs to get
27099a1914Matthew Ahrens	 * written to disk by dbuf_write().  If it is dirty, we know it will
27199a1914Matthew Ahrens	 * be written (otherwise, we would have incorrect on-disk state
27299a1914Matthew Ahrens	 * because the space would be freed but still referenced by the BP
27399a1914Matthew Ahrens	 * in this indirect block).  Therefore we VERIFY that it is
27499a1914Matthew Ahrens	 * dirty.
27599a1914Matthew Ahrens	 *
27699a1914Matthew Ahrens	 * Our VERIFY covers some cases that do not actually have to be
27799a1914Matthew Ahrens	 * dirty, but the open-context code happens to dirty.  E.g. if the
27899a1914Matthew Ahrens	 * blocks we are freeing are all holes, because in that case, we
27999a1914Matthew Ahrens	 * are only freeing part of this indirect block, so it is an
28099a1914Matthew Ahrens	 * ancestor of the first or last block to be freed.  The first and
28199a1914Matthew Ahrens	 * last L1 indirect blocks are always dirtied by dnode_free_range().
28299a1914Matthew Ahrens	 */
28399a1914Matthew Ahrens	VERIFY(BP_GET_FILL(db->db_blkptr) == 0 || db->db_dirtycnt > 0);
28499a1914Matthew Ahrens
2853f9d6adLin Ling	dbuf_release_bp(db);
28643466aaMax Grossman	bp = db->db.db_data;
287fa9e406ahrens
288744947dTom Erickson	DB_DNODE_ENTER(db);
289744947dTom Erickson	dn = DB_DNODE(db);
290744947dTom Erickson	epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
2911a01181George Wilson	ASSERT3U(epbs, <, 31);
292fa9e406ahrens	shift = (db->db_level - 1) * epbs;
293fa9e406ahrens	dbstart = db->db_blkid << epbs;
294fa9e406ahrens	start = blkid >> shift;
295fa9e406ahrens	if (dbstart < start) {
296fa9e406ahrens		bp += start - dbstart;
297fa9e406ahrens	} else {
298fa9e406ahrens		start = dbstart;
299fa9e406ahrens	}
300fa9e406ahrens	dbend = ((db->db_blkid + 1) << epbs) - 1;
301fa9e406ahrens	end = (blkid + nblks - 1) >> shift;
302fa9e406ahrens	if (dbend <= end)
303fa9e406ahrens		end = dbend;
30443466aaMax Grossman
305fa9e406ahrens	ASSERT3U(start, <=, end);
306fa9e406ahrens
307fa9e406ahrens	if (db->db_level == 1) {
3089c9dc39ek		FREE_VERIFY(db, start, end, tx);
30943466aaMax Grossman		free_blocks(dn, bp, end-start+1, tx);
31043466aaMax Grossman	} else {
3111a01181George Wilson		for (uint64_t id = start; id <= end; id++, bp++) {
31243466aaMax Grossman			if (BP_IS_HOLE(bp))
31343466aaMax Grossman				continue;
31443466aaMax Grossman			rw_enter(&dn->dn_struct_rwlock, RW_READER);
31543466aaMax Grossman			VERIFY0(dbuf_hold_impl(dn, db->db_level - 1,
3161a01181George Wilson			    id, TRUE, FALSE, FTAG, &subdb));
31743466aaMax Grossman			rw_exit(&dn->dn_struct_rwlock);
31843466aaMax Grossman			ASSERT3P(bp, ==, subdb->db_blkptr);
31943466aaMax Grossman
320738e2a3Paul Dagnelie			free_children(subdb, blkid, nblks, free_indirects, tx);
32143466aaMax Grossman			dbuf_rele(subdb, FTAG);
32243466aaMax Grossman		}
323fa9e406ahrens	}
324fa9e406ahrens
325738e2a3Paul Dagnelie	if (free_indirects) {
326738e2a3Paul Dagnelie		for (i = 0, bp = db->db.db_data; i < 1 << epbs; i++, bp++)
327738e2a3Paul Dagnelie			ASSERT(BP_IS_HOLE(bp));
32843466aaMax Grossman		bzero(db->db.db_data, db->db.db_size);
32943466aaMax Grossman		free_blocks(dn, db->db_blkptr, 1, tx);
330fa9e406ahrens	}
33143466aaMax Grossman
332744947dTom Erickson	DB_DNODE_EXIT(db);
3336b4acc8ahrens	arc_buf_freeze(db->db_buf);
334fa9e406ahrens}
335fa9e406ahrens
336fa9e406ahrens/*
337f717074Will Andrews * Traverse the indicated range of the provided file
338fa9e406ahrens * and "free" all the blocks contained there.
339fa9e406ahrens */
340fa9e406ahrensstatic void
341bf16b11Matthew Ahrensdnode_sync_free_range_impl(dnode_t *dn, uint64_t blkid, uint64_t nblks,
342738e2a3Paul Dagnelie    boolean_t free_indirects, dmu_tx_t *tx)
343fa9e406ahrens{
344fa9e406ahrens	blkptr_t *bp = dn->dn_phys->dn_blkptr;
345fa9e406ahrens	int dnlevel = dn->dn_phys->dn_nlevels;
34643466aaMax Grossman	boolean_t trunc = B_FALSE;
347fa9e406ahrens
348fa9e406ahrens	if (blkid > dn->dn_phys->dn_maxblkid)
349fa9e406ahrens		return;
350fa9e406ahrens
351fa9e406ahrens	ASSERT(dn->dn_phys->dn_maxblkid < UINT64_MAX);
35243466aaMax Grossman	if (blkid + nblks > dn->dn_phys->dn_maxblkid) {
353fa9e406ahrens		nblks = dn->dn_phys->dn_maxblkid - blkid + 1;
35443466aaMax Grossman		trunc = B_TRUE;
35543466aaMax Grossman	}
356fa9e406ahrens
357fa9e406ahrens	/* There are no indirect blocks in the object */
358fa9e406ahrens	if (dnlevel == 1) {
359fa9e406ahrens		if (blkid >= dn->dn_phys->dn_nblkptr) {
360fa9e406ahrens			/* this range was never made persistent */
361fa9e406ahrens			return;
362fa9e406ahrens		}
363fa9e406ahrens		ASSERT3U(blkid + nblks, <=, dn->dn_phys->dn_nblkptr);
36443466aaMax Grossman		free_blocks(dn, bp + blkid, nblks, tx);
36543466aaMax Grossman	} else {
36643466aaMax Grossman		int shift = (dnlevel - 1) *
36743466aaMax Grossman		    (dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT);
36843466aaMax Grossman		int start = blkid >> shift;
36943466aaMax Grossman		int end = (blkid + nblks - 1) >> shift;
37043466aaMax Grossman		dmu_buf_impl_t *db;
37143466aaMax Grossman
37243466aaMax Grossman		ASSERT(start < dn->dn_phys->dn_nblkptr);
37343466aaMax Grossman		bp += start;
37443466aaMax Grossman		for (int i = start; i <= end; i++, bp++) {
37543466aaMax Grossman			if (BP_IS_HOLE(bp))
37643466aaMax Grossman				continue;
37743466aaMax Grossman			rw_enter(&dn->dn_struct_rwlock, RW_READER);
37843466aaMax Grossman			VERIFY0(dbuf_hold_impl(dn, dnlevel - 1, i,
379a2cdcddPaul Dagnelie			    TRUE, FALSE, FTAG, &db));
38043466aaMax Grossman			rw_exit(&dn->dn_struct_rwlock);
38143466aaMax Grossman
382738e2a3Paul Dagnelie			free_children(db, blkid, nblks, free_indirects, tx);
38343466aaMax Grossman			dbuf_rele(db, FTAG);
384fa9e406ahrens		}
385fa9e406ahrens	}
38643466aaMax Grossman
387eb63303Tom Caputi	/*
388eb63303Tom Caputi	 * Do not truncate the maxblkid if we are performing a raw
389eb63303Tom Caputi	 * receive. The raw receive sets the maxblkid manually and
390eb63303Tom Caputi	 * must not be overridden. Usually, the last DRR_FREE record
391eb63303Tom Caputi	 * will be at the maxblkid, because the source system sets
392eb63303Tom Caputi	 * the maxblkid when truncating. However, if the last block
393eb63303Tom Caputi	 * was freed by overwriting with zeros and being compressed
394eb63303Tom Caputi	 * away to a hole, the source system will generate a DRR_FREE
395eb63303Tom Caputi	 * record while leaving the maxblkid after the end of that
396eb63303Tom Caputi	 * record. In this case we need to leave the maxblkid as
397eb63303Tom Caputi	 * indicated in the DRR_OBJECT record, so that it matches the
398eb63303Tom Caputi	 * source system, ensuring that the cryptographic hashes will
399eb63303Tom Caputi	 * match.
400eb63303Tom Caputi	 */
401eb63303Tom Caputi	if (trunc && !dn->dn_objset->os_raw_receive) {
40243466aaMax Grossman		dn->dn_phys->dn_maxblkid = blkid == 0 ? 0 : blkid - 1;
40343466aaMax Grossman
404fa9e406ahrens		uint64_t off = (dn->dn_phys->dn_maxblkid + 1) *
405fa9e406ahrens		    (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT);
406fa9e406ahrens		ASSERT(off < dn->dn_phys->dn_maxblkid ||
407fa9e406ahrens		    dn->dn_phys->dn_maxblkid == 0 ||
408cdb0ab7maybee		    dnode_next_offset(dn, 0, &off, 1, 1, 0) != 0);
409fa9e406ahrens	}
410fa9e406ahrens}
411fa9e406ahrens
412bf16b11Matthew Ahrenstypedef struct dnode_sync_free_range_arg {
413bf16b11Matthew Ahrens	dnode_t *dsfra_dnode;
414bf16b11Matthew Ahrens	dmu_tx_t *dsfra_tx;
415738e2a3Paul Dagnelie	boolean_t dsfra_free_indirects;
416bf16b11Matthew Ahrens} dnode_sync_free_range_arg_t;
417bf16b11Matthew Ahrens
418bf16b11Matthew Ahrensstatic void
419bf16b11Matthew Ahrensdnode_sync_free_range(void *arg, uint64_t blkid, uint64_t nblks)
420bf16b11Matthew Ahrens{
421bf16b11Matthew Ahrens	dnode_sync_free_range_arg_t *dsfra = arg;
422bf16b11Matthew Ahrens	dnode_t *dn = dsfra->dsfra_dnode;
423bf16b11Matthew Ahrens
424bf16b11Matthew Ahrens	mutex_exit(&dn->dn_mtx);
425738e2a3Paul Dagnelie	dnode_sync_free_range_impl(dn, blkid, nblks,
426738e2a3Paul Dagnelie	    dsfra->dsfra_free_indirects, dsfra->dsfra_tx);
427bf16b11Matthew Ahrens	mutex_enter(&dn->dn_mtx);
428bf16b11Matthew Ahrens}
429bf16b11Matthew Ahrens
430ea8dc4beschrock/*
431f717074Will Andrews * Try to kick all the dnode's dbufs out of the cache...
432ea8dc4beschrock */
4331934e92maybeevoid
4341934e92maybeednode_evict_dbufs(dnode_t *dn)
435ea8dc4beschrock{
436bc9014eJustin Gibbs	dmu_buf_impl_t db_marker;
437bc9014eJustin Gibbs	dmu_buf_impl_t *db, *db_next;
438c543ec0ahrens
439bc9014eJustin Gibbs	mutex_enter(&dn->dn_dbufs_mtx);
440bc9014eJustin Gibbs	for (db = avl_first(&dn->dn_dbufs); db != NULL; db = db_next) {
441c543ec0ahrens
442744947dTom Erickson#ifdef	DEBUG
443bc9014eJustin Gibbs		DB_DNODE_ENTER(db);
444bc9014eJustin Gibbs		ASSERT3P(DB_DNODE(db), ==, dn);
445bc9014eJustin Gibbs		DB_DNODE_EXIT(db);
446744947dTom Erickson#endif	/* DEBUG */
447ea8dc4beschrock
448bc9014eJustin Gibbs		mutex_enter(&db->db_mtx);
449bc9014eJustin Gibbs		if (db->db_state != DB_EVICTING &&
450e914aceTim Schumacher		    zfs_refcount_is_zero(&db->db_holds)) {
451bc9014eJustin Gibbs			db_marker.db_level = db->db_level;
452bc9014eJustin Gibbs			db_marker.db_blkid = db->db_blkid;
453bc9014eJustin Gibbs			db_marker.db_state = DB_SEARCH;
454bc9014eJustin Gibbs			avl_insert_here(&dn->dn_dbufs, &db_marker, db,
455bc9014eJustin Gibbs			    AVL_BEFORE);
456bc9014eJustin Gibbs
457c2919acMatthew Ahrens			/*
458c2919acMatthew Ahrens			 * We need to use the "marker" dbuf rather than
459c2919acMatthew Ahrens			 * simply getting the next dbuf, because
460c2919acMatthew Ahrens			 * dbuf_destroy() may actually remove multiple dbufs.
461c2919acMatthew Ahrens			 * It can call itself recursively on the parent dbuf,
462c2919acMatthew Ahrens			 * which may also be removed from dn_dbufs.  The code
463c2919acMatthew Ahrens			 * flow would look like:
464c2919acMatthew Ahrens			 *
465c2919acMatthew Ahrens			 * dbuf_destroy():
466c2919acMatthew Ahrens			 *   dnode_rele_and_unlock(parent_dbuf, evicting=TRUE):
467c2919acMatthew Ahrens			 *	if (!cacheable || pending_evict)
468c2919acMatthew Ahrens			 *	  dbuf_destroy()
469c2919acMatthew Ahrens			 */
470dcbf3bdGeorge Wilson			dbuf_destroy(db);
471bc9014eJustin Gibbs
472bc9014eJustin Gibbs			db_next = AVL_NEXT(&dn->dn_dbufs, &db_marker);
473bc9014eJustin Gibbs			avl_remove(&dn->dn_dbufs, &db_marker);
474bc9014eJustin Gibbs		} else {
475d205810Justin T. Gibbs			db->db_pending_evict = TRUE;
476bc9014eJustin Gibbs			mutex_exit(&db->db_mtx);
477bc9014eJustin Gibbs			db_next = AVL_NEXT(&dn->dn_dbufs, db);
478ea8dc4beschrock		}
479bc9014eJustin Gibbs	}
480bc9014eJustin Gibbs	mutex_exit(&dn->dn_dbufs_mtx);
481c543ec0ahrens
482cd485b4Justin T. Gibbs	dnode_evict_bonus(dn);
483cd485b4Justin T. Gibbs}
484cd485b4Justin T. Gibbs
485cd485b4Justin T. Gibbsvoid
486cd485b4Justin T. Gibbsdnode_evict_bonus(dnode_t *dn)
487cd485b4Justin T. Gibbs{
488ea8dc4beschrock	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
489d205810Justin T. Gibbs	if (dn->dn_bonus != NULL) {
490e914aceTim Schumacher		if (zfs_refcount_is_zero(&dn->dn_bonus->db_holds)) {
491d205810Justin T. Gibbs			mutex_enter(&dn->dn_bonus->db_mtx);
492dcbf3bdGeorge Wilson			dbuf_destroy(dn->dn_bonus);
493d205810Justin T. Gibbs			dn->dn_bonus = NULL;
494d205810Justin T. Gibbs		} else {
495d205810Justin T. Gibbs			dn->dn_bonus->db_pending_evict = TRUE;
496d205810Justin T. Gibbs		}
497ea8dc4beschrock	}
498ea8dc4beschrock	rw_exit(&dn->dn_struct_rwlock);
499ea8dc4beschrock}
500ea8dc4beschrock
501c717a56maybeestatic void
502c717a56maybeednode_undirty_dbufs(list_t *list)
503fa9e406ahrens{
504c717a56maybee	dbuf_dirty_record_t *dr;
505fa9e406ahrens
506c717a56maybee	while (dr = list_head(list)) {
507c717a56maybee		dmu_buf_impl_t *db = dr->dr_dbuf;
508c717a56maybee		uint64_t txg = dr->dr_txg;
509fa9e406ahrens
510b24ab67Jeff Bonwick		if (db->db_level != 0)
511b24ab67Jeff Bonwick			dnode_undirty_dbufs(&dr->dt.di.dr_children);
512b24ab67Jeff Bonwick
513fa9e406ahrens		mutex_enter(&db->db_mtx);
514fa9e406ahrens		/* XXX - use dbuf_undirty()? */
515c717a56maybee		list_remove(list, dr);
516c717a56maybee		ASSERT(db->db_last_dirty == dr);
517c717a56maybee		db->db_last_dirty = NULL;
518c717a56maybee		db->db_dirtycnt -= 1;
519fa9e406ahrens		if (db->db_level == 0) {
5200a586ceMark Shellenbaum			ASSERT(db->db_blkid == DMU_BONUS_BLKID ||
521c717a56maybee			    dr->dt.dl.dr_data == db->db_buf);
522c717a56maybee			dbuf_unoverride(dr);
523d2b3cbbJorgen Lundman		} else {
524d2b3cbbJorgen Lundman			mutex_destroy(&dr->dt.di.dr_mtx);
525d2b3cbbJorgen Lundman			list_destroy(&dr->dt.di.dr_children);
526fa9e406ahrens		}
527c717a56maybee		kmem_free(dr, sizeof (dbuf_dirty_record_t));
528c2919acMatthew Ahrens		dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg, B_FALSE);
529fa9e406ahrens	}
530c717a56maybee}
531fa9e406ahrens
532c717a56maybeestatic void
533c717a56maybeednode_sync_free(dnode_t *dn, dmu_tx_t *tx)
534c717a56maybee{
535c717a56maybee	int txgoff = tx->tx_txg & TXG_MASK;
536c717a56maybee
537c717a56maybee	ASSERT(dmu_tx_is_syncing(tx));
538c717a56maybee
539cdb0ab7maybee	/*
540cdb0ab7maybee	 * Our contents should have been freed in dnode_sync() by the
541cdb0ab7maybee	 * free range record inserted by the caller of dnode_free().
542cdb0ab7maybee	 */
543fb09f5aMadhav Suresh	ASSERT0(DN_USED_BYTES(dn->dn_phys));
544cdb0ab7maybee	ASSERT(BP_IS_HOLE(dn->dn_phys->dn_blkptr));
545cdb0ab7maybee
546c717a56maybee	dnode_undirty_dbufs(&dn->dn_dirty_records[txgoff]);
5471934e92maybee	dnode_evict_dbufs(dn);
548ea8dc4beschrock
549ea8dc4beschrock	/*
550ea8dc4beschrock	 * XXX - It would be nice to assert this, but we may still
551ea8dc4beschrock	 * have residual holds from async evictions from the arc...
552ea8dc4beschrock	 *
55355434c7ek	 * zfs_obj_to_path() also depends on this being
55455434c7ek	 * commented out.
55555434c7ek	 *
556e914aceTim Schumacher	 * ASSERT3U(zfs_refcount_count(&dn->dn_holds), ==, 1);
557ea8dc4beschrock	 */
558fa9e406ahrens
559fa9e406ahrens	/* Undirty next bits */
560fa9e406ahrens	dn->dn_next_nlevels[txgoff] = 0;
561fa9e406ahrens	dn->dn_next_indblkshift[txgoff] = 0;
562c543ec0ahrens	dn->dn_next_blksz[txgoff] = 0;
563eb63303Tom Caputi	dn->dn_next_maxblkid[txgoff] = 0;
564fa9e406ahrens
565fa9e406ahrens	/* ASSERT(blkptrs are zero); */
566fa9e406ahrens	ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE);
567fa9e406ahrens	ASSERT(dn->dn_type != DMU_OT_NONE);
568fa9e406ahrens
569fa9e406ahrens	ASSERT(dn->dn_free_txg > 0);
570fa9e406ahrens	if (dn->dn_allocated_txg != dn->dn_free_txg)
57143466aaMax Grossman		dmu_buf_will_dirty(&dn->dn_dbuf->db, tx);
57254811daToomas Soome	bzero(dn->dn_phys, sizeof (dnode_phys_t) * dn->dn_num_slots);
57354811daToomas Soome	dnode_free_interior_slots(dn);
574fa9e406ahrens
575fa9e406ahrens	mutex_enter(&dn->dn_mtx);
576fa9e406ahrens	dn->dn_type = DMU_OT_NONE;
577fa9e406ahrens	dn->dn_maxblkid = 0;
578fa9e406ahrens	dn->dn_allocated_txg = 0;
579758f6e0gw	dn->dn_free_txg = 0;
5800a586ceMark Shellenbaum	dn->dn_have_spill = B_FALSE;
58154811daToomas Soome	dn->dn_num_slots = 1;
582fa9e406ahrens	mutex_exit(&dn->dn_mtx);
583fa9e406ahrens
584ea8dc4beschrock	ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
585fa9e406ahrens
586fa9e406ahrens	dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg);
587fa9e406ahrens	/*
588fa9e406ahrens	 * Now that we've released our hold, the dnode may
589eb63303Tom Caputi	 * be evicted, so we mustn't access it.
590fa9e406ahrens	 */
591fa9e406ahrens}
592fa9e406ahrens
593fa9e406ahrens/*
594c717a56maybee * Write out the dnode's dirty buffers.
595fa9e406ahrens */
596c717a56maybeevoid
597c717a56maybeednode_sync(dnode_t *dn, dmu_tx_t *tx)
598fa9e406ahrens{
599eb63303Tom Caputi	objset_t *os = dn->dn_objset;
600fa9e406ahrens	dnode_phys_t *dnp = dn->dn_phys;
601c717a56maybee	int txgoff = tx->tx_txg & TXG_MASK;
602c717a56maybee	list_t *list = &dn->dn_dirty_records[txgoff];
6031484342Matthew Ahrens	static const dnode_phys_t zerodn = { 0 };
6040a586ceMark Shellenbaum	boolean_t kill_spill = B_FALSE;
605fa9e406ahrens
606fa9e406ahrens	ASSERT(dmu_tx_is_syncing(tx));
607fa9e406ahrens	ASSERT(dnp->dn_type != DMU_OT_NONE || dn->dn_allocated_txg);
6081484342Matthew Ahrens	ASSERT(dnp->dn_type != DMU_OT_NONE ||
60954811daToomas Soome	    bcmp(dnp, &zerodn, DNODE_MIN_SIZE) == 0);
6109c9dc39ek	DNODE_VERIFY(dn);
611c543ec0ahrens
612c717a56maybee	ASSERT(dn->dn_dbuf == NULL || arc_released(dn->dn_dbuf->db_buf));
613fa9e406ahrens
614eb63303Tom Caputi	/*
615eb63303Tom Caputi	 * Do user accounting if it is enabled and this is not
616eb63303Tom Caputi	 * an encrypted receive.
617eb63303Tom Caputi	 */
618eb63303Tom Caputi	if (dmu_objset_userused_enabled(os) &&
619eb63303Tom Caputi	    !DMU_OBJECT_IS_SPECIAL(dn->dn_object) &&
620eb63303Tom Caputi	    (!os->os_encrypted || !dmu_objset_is_receiving(os))) {
6210a586ceMark Shellenbaum		mutex_enter(&dn->dn_mtx);
6220a586ceMark Shellenbaum		dn->dn_oldused = DN_USED_BYTES(dn->dn_phys);
6230a586ceMark Shellenbaum		dn->dn_oldflags = dn->dn_phys->dn_flags;
6241484342Matthew Ahrens		dn->dn_phys->dn_flags |= DNODE_FLAG_USERUSED_ACCOUNTED;
625f67950bNasf-Fan		if (dmu_objset_userobjused_enabled(dn->dn_objset))
626f67950bNasf-Fan			dn->dn_phys->dn_flags |=
627f67950bNasf-Fan			    DNODE_FLAG_USEROBJUSED_ACCOUNTED;
6280a586ceMark Shellenbaum		mutex_exit(&dn->dn_mtx);
62906e0070Mark Shellenbaum		dmu_objset_userquota_get_ids(dn, B_FALSE, tx);
6301484342Matthew Ahrens	} else {
631eb63303Tom Caputi		/* Once we account for it, we should always account for it */
6321484342Matthew Ahrens		ASSERT(!(dn->dn_phys->dn_flags &
6331484342Matthew Ahrens		    DNODE_FLAG_USERUSED_ACCOUNTED));
634f67950bNasf-Fan		ASSERT(!(dn->dn_phys->dn_flags &
635f67950bNasf-Fan		    DNODE_FLAG_USEROBJUSED_ACCOUNTED));
6361484342Matthew Ahrens	}
6371484342Matthew Ahrens
638fa9e406ahrens	mutex_enter(&dn->dn_mtx);
639fa9e406ahrens	if (dn->dn_allocated_txg == tx->tx_txg) {
640fa9e406ahrens		/* The dnode is newly allocated or reallocated */
641fa9e406ahrens		if (dnp->dn_type == DMU_OT_NONE) {
642fa9e406ahrens			/* this is a first alloc, not a realloc */
643fa9e406ahrens			dnp->dn_nlevels = 1;
644da03de9Mark Maybee			dnp->dn_nblkptr = dn->dn_nblkptr;
645fa9e406ahrens		}
646fa9e406ahrens
647fa9e406ahrens		dnp->dn_type = dn->dn_type;
648fa9e406ahrens		dnp->dn_bonustype = dn->dn_bonustype;
649fa9e406ahrens		dnp->dn_bonuslen = dn->dn_bonuslen;
650fa9e406ahrens	}
65154811daToomas Soome
65254811daToomas Soome	dnp->dn_extra_slots = dn->dn_num_slots - 1;
65354811daToomas Soome
654c717a56maybee	ASSERT(dnp->dn_nlevels > 1 ||
655f676ed3ahrens	    BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
6565d7b4d4Matthew Ahrens	    BP_IS_EMBEDDED(&dnp->dn_blkptr[0]) ||
657f676ed3ahrens	    BP_GET_LSIZE(&dnp->dn_blkptr[0]) ==
658f676ed3ahrens	    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
6595d7b4d4Matthew Ahrens	ASSERT(dnp->dn_nlevels < 2 ||
6605d7b4d4Matthew Ahrens	    BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
6615d7b4d4Matthew Ahrens	    BP_GET_LSIZE(&dnp->dn_blkptr[0]) == 1 << dnp->dn_indblkshift);
662f676ed3ahrens
6632acef22Matthew Ahrens	if (dn->dn_next_type[txgoff] != 0) {
6642acef22Matthew Ahrens		dnp->dn_type = dn->dn_type;
6652acef22Matthew Ahrens		dn->dn_next_type[txgoff] = 0;
6662acef22Matthew Ahrens	}
6672acef22Matthew Ahrens
6682acef22Matthew Ahrens	if (dn->dn_next_blksz[txgoff] != 0) {
669c543ec0ahrens		ASSERT(P2PHASE(dn->dn_next_blksz[txgoff],
670fa9e406ahrens		    SPA_MINBLOCKSIZE) == 0);
671f676ed3ahrens		ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
672cdb0ab7maybee		    dn->dn_maxblkid == 0 || list_head(list) != NULL ||
673347a31bahrens		    dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT ==
674bf16b11Matthew Ahrens		    dnp->dn_datablkszsec ||
6758671400Serapheim Dimitropoulos		    !range_tree_is_empty(dn->dn_free_ranges[txgoff]));
676fa9e406ahrens		dnp->dn_datablkszsec =
677c543ec0ahrens		    dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT;
678c543ec0ahrens		dn->dn_next_blksz[txgoff] = 0;
679fa9e406ahrens	}
680fa9e406ahrens
6812acef22Matthew Ahrens	if (dn->dn_next_bonuslen[txgoff] != 0) {
6821934e92maybee		if (dn->dn_next_bonuslen[txgoff] == DN_ZERO_BONUSLEN)
6831934e92maybee			dnp->dn_bonuslen = 0;
6841934e92maybee		else
6851934e92maybee			dnp->dn_bonuslen = dn->dn_next_bonuslen[txgoff];
68654811daToomas Soome		ASSERT(dnp->dn_bonuslen <=
68754811daToomas Soome		    DN_SLOTS_TO_BONUSLEN(dnp->dn_extra_slots + 1));
6881934e92maybee		dn->dn_next_bonuslen[txgoff] = 0;
6891934e92maybee	}
6901934e92maybee
6912acef22Matthew Ahrens	if (dn->dn_next_bonustype[txgoff] != 0) {
692ad135b5Christopher Siden		ASSERT(DMU_OT_IS_VALID(dn->dn_next_bonustype[txgoff]));
6930a586ceMark Shellenbaum		dnp->dn_bonustype = dn->dn_next_bonustype[txgoff];
6940a586ceMark Shellenbaum		dn->dn_next_bonustype[txgoff] = 0;
6950a586ceMark Shellenbaum	}
6960a586ceMark Shellenbaum
69743466aaMax Grossman	boolean_t freeing_dnode = dn->dn_free_txg > 0 &&
69843466aaMax Grossman	    dn->dn_free_txg <= tx->tx_txg;
69943466aaMax Grossman
7000a586ceMark Shellenbaum	/*
701e651831Matthew Ahrens	 * Remove the spill block if we have been explicitly asked to
702e651831Matthew Ahrens	 * remove it, or if the object is being removed.
7030a586ceMark Shellenbaum	 */
704e651831Matthew Ahrens	if (dn->dn_rm_spillblk[txgoff] || freeing_dnode) {
705e651831Matthew Ahrens		if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR)
7060a586ceMark Shellenbaum			kill_spill = B_TRUE;
7070a586ceMark Shellenbaum		dn->dn_rm_spillblk[txgoff] = 0;
7080a586ceMark Shellenbaum	}
7090a586ceMark Shellenbaum
7102acef22Matthew Ahrens	if (dn->dn_next_indblkshift[txgoff] != 0) {
711fa9e406ahrens		ASSERT(dnp->dn_nlevels == 1);
712fa9e406ahrens		dnp->dn_indblkshift = dn->dn_next_indblkshift[txgoff];
713fa9e406ahrens		dn->dn_next_indblkshift[txgoff] = 0;
714fa9e406ahrens	}
715fa9e406ahrens
716fa9e406ahrens	/*
717fa9e406ahrens	 * Just take the live (open-context) values for checksum and compress.
718fa9e406ahrens	 * Strictly speaking it's a future leak, but nothing bad happens if we
719fa9e406ahrens	 * start using the new checksum or compress algorithm a little early.
720fa9e406ahrens	 */
721fa9e406ahrens	dnp->dn_checksum = dn->dn_checksum;
722fa9e406ahrens	dnp->dn_compress = dn->dn_compress;
723fa9e406ahrens
724fa9e406ahrens	mutex_exit(&dn->dn_mtx);
725fa9e406ahrens
7260a586ceMark Shellenbaum	if (kill_spill) {
72754811daToomas Soome		free_blocks(dn, DN_SPILL_BLKPTR(dn->dn_phys), 1, tx);
7280a586ceMark Shellenbaum		mutex_enter(&dn->dn_mtx);
7290a586ceMark Shellenbaum		dnp->dn_flags &= ~DNODE_FLAG_SPILL_BLKPTR;
7300a586ceMark Shellenbaum		mutex_exit(&dn->dn_mtx);
7310a586ceMark Shellenbaum	}
7320a586ceMark Shellenbaum
733fa9e406ahrens	/* process all the "freed" ranges in the file */
734bf16b11Matthew Ahrens	if (dn->dn_free_ranges[txgoff] != NULL) {
735bf16b11Matthew Ahrens		dnode_sync_free_range_arg_t dsfra;
736bf16b11Matthew Ahrens		dsfra.dsfra_dnode = dn;
737bf16b11Matthew Ahrens		dsfra.dsfra_tx = tx;
738738e2a3Paul Dagnelie		dsfra.dsfra_free_indirects = freeing_dnode;
739738e2a3Paul Dagnelie		if (freeing_dnode) {
740738e2a3Paul Dagnelie			ASSERT(range_tree_contains(dn->dn_free_ranges[txgoff],
741738e2a3Paul Dagnelie			    0, dn->dn_maxblkid + 1));
742738e2a3Paul Dagnelie		}
743cdb0ab7maybee		mutex_enter(&dn->dn_mtx);
744bf16b11Matthew Ahrens		range_tree_vacate(dn->dn_free_ranges[txgoff],
745bf16b11Matthew Ahrens		    dnode_sync_free_range, &dsfra);
746bf16b11Matthew Ahrens		range_tree_destroy(dn->dn_free_ranges[txgoff]);
747bf16b11Matthew Ahrens		dn->dn_free_ranges[txgoff] = NULL;
748cdb0ab7maybee		mutex_exit(&dn->dn_mtx);
749fa9e406ahrens	}
7501934e92maybee
75143466aaMax Grossman	if (freeing_dnode) {
752af346dfNed Bass		dn->dn_objset->os_freed_dnodes++;
753c717a56maybee		dnode_sync_free(dn, tx);
754c717a56maybee		return;
755fa9e406ahrens	}
756fa9e406ahrens
75754811daToomas Soome	if (dn->dn_num_slots > DNODE_MIN_SLOTS) {
75854811daToomas Soome		dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
75954811daToomas Soome		mutex_enter(&ds->ds_lock);
76054811daToomas Soome		ds->ds_feature_activation_needed[SPA_FEATURE_LARGE_DNODE] =
76154811daToomas Soome		    B_TRUE;
76254811daToomas Soome		mutex_exit(&ds->ds_lock);
76354811daToomas Soome	}
76454811daToomas Soome
765e503a68George Wilson	if (dn->dn_next_nlevels[txgoff]) {
766e503a68George Wilson		dnode_increase_indirection(dn, tx);
767e503a68George Wilson		dn->dn_next_nlevels[txgoff] = 0;
768e503a68George Wilson	}
769e503a68George Wilson
770eb63303Tom Caputi	/*
771eb63303Tom Caputi	 * This must be done after dnode_sync_free_range()
772eb63303Tom Caputi	 * and dnode_increase_indirection(). See dnode_new_blkid()
773eb63303Tom Caputi	 * for an explanation of the high bit being set.
774eb63303Tom Caputi	 */
775eb63303Tom Caputi	if (dn->dn_next_maxblkid[txgoff]) {
776eb63303Tom Caputi		mutex_enter(&dn->dn_mtx);
777