xref: /freebsd/sys/contrib/openzfs/module/zfs/dnode_sync.c (revision ac0bf12ee5181e3f784b5bb87d60fc5321ebce2d)
1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * CDDL HEADER START
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy  *
8eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9eda14cbcSMatt Macy  * or http://www.opensolaris.org/os/licensing.
10eda14cbcSMatt Macy  * See the License for the specific language governing permissions
11eda14cbcSMatt Macy  * and limitations under the License.
12eda14cbcSMatt Macy  *
13eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy  *
19eda14cbcSMatt Macy  * CDDL HEADER END
20eda14cbcSMatt Macy  */
21eda14cbcSMatt Macy 
22eda14cbcSMatt Macy /*
23eda14cbcSMatt Macy  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24eda14cbcSMatt Macy  * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
25eda14cbcSMatt Macy  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
26*ac0bf12eSMatt Macy  * Copyright 2020 Oxide Computer Company
27eda14cbcSMatt Macy  */
28eda14cbcSMatt Macy 
29eda14cbcSMatt Macy #include <sys/zfs_context.h>
30eda14cbcSMatt Macy #include <sys/dbuf.h>
31eda14cbcSMatt Macy #include <sys/dnode.h>
32eda14cbcSMatt Macy #include <sys/dmu.h>
33eda14cbcSMatt Macy #include <sys/dmu_tx.h>
34eda14cbcSMatt Macy #include <sys/dmu_objset.h>
35eda14cbcSMatt Macy #include <sys/dmu_recv.h>
36eda14cbcSMatt Macy #include <sys/dsl_dataset.h>
37eda14cbcSMatt Macy #include <sys/spa.h>
38eda14cbcSMatt Macy #include <sys/range_tree.h>
39eda14cbcSMatt Macy #include <sys/zfeature.h>
40eda14cbcSMatt Macy 
41eda14cbcSMatt Macy static void
42eda14cbcSMatt Macy dnode_increase_indirection(dnode_t *dn, dmu_tx_t *tx)
43eda14cbcSMatt Macy {
44eda14cbcSMatt Macy 	dmu_buf_impl_t *db;
45eda14cbcSMatt Macy 	int txgoff = tx->tx_txg & TXG_MASK;
46eda14cbcSMatt Macy 	int nblkptr = dn->dn_phys->dn_nblkptr;
47eda14cbcSMatt Macy 	int old_toplvl = dn->dn_phys->dn_nlevels - 1;
48eda14cbcSMatt Macy 	int new_level = dn->dn_next_nlevels[txgoff];
49eda14cbcSMatt Macy 	int i;
50eda14cbcSMatt Macy 
51eda14cbcSMatt Macy 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
52eda14cbcSMatt Macy 
53eda14cbcSMatt Macy 	/* this dnode can't be paged out because it's dirty */
54eda14cbcSMatt Macy 	ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE);
55eda14cbcSMatt Macy 	ASSERT(new_level > 1 && dn->dn_phys->dn_nlevels > 0);
56eda14cbcSMatt Macy 
57eda14cbcSMatt Macy 	db = dbuf_hold_level(dn, dn->dn_phys->dn_nlevels, 0, FTAG);
58eda14cbcSMatt Macy 	ASSERT(db != NULL);
59eda14cbcSMatt Macy 
60eda14cbcSMatt Macy 	dn->dn_phys->dn_nlevels = new_level;
61eda14cbcSMatt Macy 	dprintf("os=%p obj=%llu, increase to %d\n", dn->dn_objset,
62eda14cbcSMatt Macy 	    dn->dn_object, dn->dn_phys->dn_nlevels);
63eda14cbcSMatt Macy 
64eda14cbcSMatt Macy 	/*
65eda14cbcSMatt Macy 	 * Lock ordering requires that we hold the children's db_mutexes (by
66eda14cbcSMatt Macy 	 * calling dbuf_find()) before holding the parent's db_rwlock.  The lock
67eda14cbcSMatt Macy 	 * order is imposed by dbuf_read's steps of "grab the lock to protect
68eda14cbcSMatt Macy 	 * db_parent, get db_parent, hold db_parent's db_rwlock".
69eda14cbcSMatt Macy 	 */
70eda14cbcSMatt Macy 	dmu_buf_impl_t *children[DN_MAX_NBLKPTR];
71eda14cbcSMatt Macy 	ASSERT3U(nblkptr, <=, DN_MAX_NBLKPTR);
72eda14cbcSMatt Macy 	for (i = 0; i < nblkptr; i++) {
73eda14cbcSMatt Macy 		children[i] =
74eda14cbcSMatt Macy 		    dbuf_find(dn->dn_objset, dn->dn_object, old_toplvl, i);
75eda14cbcSMatt Macy 	}
76eda14cbcSMatt Macy 
77eda14cbcSMatt Macy 	/* transfer dnode's block pointers to new indirect block */
78eda14cbcSMatt Macy 	(void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED|DB_RF_HAVESTRUCT);
79eda14cbcSMatt Macy 	if (dn->dn_dbuf != NULL)
80eda14cbcSMatt Macy 		rw_enter(&dn->dn_dbuf->db_rwlock, RW_WRITER);
81eda14cbcSMatt Macy 	rw_enter(&db->db_rwlock, RW_WRITER);
82eda14cbcSMatt Macy 	ASSERT(db->db.db_data);
83eda14cbcSMatt Macy 	ASSERT(arc_released(db->db_buf));
84eda14cbcSMatt Macy 	ASSERT3U(sizeof (blkptr_t) * nblkptr, <=, db->db.db_size);
85eda14cbcSMatt Macy 	bcopy(dn->dn_phys->dn_blkptr, db->db.db_data,
86eda14cbcSMatt Macy 	    sizeof (blkptr_t) * nblkptr);
87eda14cbcSMatt Macy 	arc_buf_freeze(db->db_buf);
88eda14cbcSMatt Macy 
89eda14cbcSMatt Macy 	/* set dbuf's parent pointers to new indirect buf */
90eda14cbcSMatt Macy 	for (i = 0; i < nblkptr; i++) {
91eda14cbcSMatt Macy 		dmu_buf_impl_t *child = children[i];
92eda14cbcSMatt Macy 
93eda14cbcSMatt Macy 		if (child == NULL)
94eda14cbcSMatt Macy 			continue;
95eda14cbcSMatt Macy #ifdef	ZFS_DEBUG
96eda14cbcSMatt Macy 		DB_DNODE_ENTER(child);
97eda14cbcSMatt Macy 		ASSERT3P(DB_DNODE(child), ==, dn);
98eda14cbcSMatt Macy 		DB_DNODE_EXIT(child);
99eda14cbcSMatt Macy #endif	/* DEBUG */
100eda14cbcSMatt Macy 		if (child->db_parent && child->db_parent != dn->dn_dbuf) {
101eda14cbcSMatt Macy 			ASSERT(child->db_parent->db_level == db->db_level);
102eda14cbcSMatt Macy 			ASSERT(child->db_blkptr !=
103eda14cbcSMatt Macy 			    &dn->dn_phys->dn_blkptr[child->db_blkid]);
104eda14cbcSMatt Macy 			mutex_exit(&child->db_mtx);
105eda14cbcSMatt Macy 			continue;
106eda14cbcSMatt Macy 		}
107eda14cbcSMatt Macy 		ASSERT(child->db_parent == NULL ||
108eda14cbcSMatt Macy 		    child->db_parent == dn->dn_dbuf);
109eda14cbcSMatt Macy 
110eda14cbcSMatt Macy 		child->db_parent = db;
111eda14cbcSMatt Macy 		dbuf_add_ref(db, child);
112eda14cbcSMatt Macy 		if (db->db.db_data)
113eda14cbcSMatt Macy 			child->db_blkptr = (blkptr_t *)db->db.db_data + i;
114eda14cbcSMatt Macy 		else
115eda14cbcSMatt Macy 			child->db_blkptr = NULL;
116eda14cbcSMatt Macy 		dprintf_dbuf_bp(child, child->db_blkptr,
117eda14cbcSMatt Macy 		    "changed db_blkptr to new indirect %s", "");
118eda14cbcSMatt Macy 
119eda14cbcSMatt Macy 		mutex_exit(&child->db_mtx);
120eda14cbcSMatt Macy 	}
121eda14cbcSMatt Macy 
122eda14cbcSMatt Macy 	bzero(dn->dn_phys->dn_blkptr, sizeof (blkptr_t) * nblkptr);
123eda14cbcSMatt Macy 
124eda14cbcSMatt Macy 	rw_exit(&db->db_rwlock);
125eda14cbcSMatt Macy 	if (dn->dn_dbuf != NULL)
126eda14cbcSMatt Macy 		rw_exit(&dn->dn_dbuf->db_rwlock);
127eda14cbcSMatt Macy 
128eda14cbcSMatt Macy 	dbuf_rele(db, FTAG);
129eda14cbcSMatt Macy 
130eda14cbcSMatt Macy 	rw_exit(&dn->dn_struct_rwlock);
131eda14cbcSMatt Macy }
132eda14cbcSMatt Macy 
133eda14cbcSMatt Macy static void
134eda14cbcSMatt Macy free_blocks(dnode_t *dn, blkptr_t *bp, int num, dmu_tx_t *tx)
135eda14cbcSMatt Macy {
136eda14cbcSMatt Macy 	dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
137eda14cbcSMatt Macy 	uint64_t bytesfreed = 0;
138eda14cbcSMatt Macy 
139eda14cbcSMatt Macy 	dprintf("ds=%p obj=%llx num=%d\n", ds, dn->dn_object, num);
140eda14cbcSMatt Macy 
141eda14cbcSMatt Macy 	for (int i = 0; i < num; i++, bp++) {
142eda14cbcSMatt Macy 		if (BP_IS_HOLE(bp))
143eda14cbcSMatt Macy 			continue;
144eda14cbcSMatt Macy 
145eda14cbcSMatt Macy 		bytesfreed += dsl_dataset_block_kill(ds, bp, tx, B_FALSE);
146eda14cbcSMatt Macy 		ASSERT3U(bytesfreed, <=, DN_USED_BYTES(dn->dn_phys));
147eda14cbcSMatt Macy 
148eda14cbcSMatt Macy 		/*
149eda14cbcSMatt Macy 		 * Save some useful information on the holes being
150eda14cbcSMatt Macy 		 * punched, including logical size, type, and indirection
151eda14cbcSMatt Macy 		 * level. Retaining birth time enables detection of when
152eda14cbcSMatt Macy 		 * holes are punched for reducing the number of free
153eda14cbcSMatt Macy 		 * records transmitted during a zfs send.
154eda14cbcSMatt Macy 		 */
155eda14cbcSMatt Macy 
156eda14cbcSMatt Macy 		uint64_t lsize = BP_GET_LSIZE(bp);
157eda14cbcSMatt Macy 		dmu_object_type_t type = BP_GET_TYPE(bp);
158eda14cbcSMatt Macy 		uint64_t lvl = BP_GET_LEVEL(bp);
159eda14cbcSMatt Macy 
160eda14cbcSMatt Macy 		bzero(bp, sizeof (blkptr_t));
161eda14cbcSMatt Macy 
162eda14cbcSMatt Macy 		if (spa_feature_is_active(dn->dn_objset->os_spa,
163eda14cbcSMatt Macy 		    SPA_FEATURE_HOLE_BIRTH)) {
164eda14cbcSMatt Macy 			BP_SET_LSIZE(bp, lsize);
165eda14cbcSMatt Macy 			BP_SET_TYPE(bp, type);
166eda14cbcSMatt Macy 			BP_SET_LEVEL(bp, lvl);
167eda14cbcSMatt Macy 			BP_SET_BIRTH(bp, dmu_tx_get_txg(tx), 0);
168eda14cbcSMatt Macy 		}
169eda14cbcSMatt Macy 	}
170eda14cbcSMatt Macy 	dnode_diduse_space(dn, -bytesfreed);
171eda14cbcSMatt Macy }
172eda14cbcSMatt Macy 
173eda14cbcSMatt Macy #ifdef ZFS_DEBUG
174eda14cbcSMatt Macy static void
175eda14cbcSMatt Macy free_verify(dmu_buf_impl_t *db, uint64_t start, uint64_t end, dmu_tx_t *tx)
176eda14cbcSMatt Macy {
177eda14cbcSMatt Macy 	int off, num;
178eda14cbcSMatt Macy 	int i, err, epbs;
179eda14cbcSMatt Macy 	uint64_t txg = tx->tx_txg;
180eda14cbcSMatt Macy 	dnode_t *dn;
181eda14cbcSMatt Macy 
182eda14cbcSMatt Macy 	DB_DNODE_ENTER(db);
183eda14cbcSMatt Macy 	dn = DB_DNODE(db);
184eda14cbcSMatt Macy 	epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
185eda14cbcSMatt Macy 	off = start - (db->db_blkid * 1<<epbs);
186eda14cbcSMatt Macy 	num = end - start + 1;
187eda14cbcSMatt Macy 
188eda14cbcSMatt Macy 	ASSERT3U(off, >=, 0);
189eda14cbcSMatt Macy 	ASSERT3U(num, >=, 0);
190eda14cbcSMatt Macy 	ASSERT3U(db->db_level, >, 0);
191eda14cbcSMatt Macy 	ASSERT3U(db->db.db_size, ==, 1 << dn->dn_phys->dn_indblkshift);
192eda14cbcSMatt Macy 	ASSERT3U(off+num, <=, db->db.db_size >> SPA_BLKPTRSHIFT);
193eda14cbcSMatt Macy 	ASSERT(db->db_blkptr != NULL);
194eda14cbcSMatt Macy 
195eda14cbcSMatt Macy 	for (i = off; i < off+num; i++) {
196eda14cbcSMatt Macy 		uint64_t *buf;
197eda14cbcSMatt Macy 		dmu_buf_impl_t *child;
198eda14cbcSMatt Macy 		dbuf_dirty_record_t *dr;
199eda14cbcSMatt Macy 		int j;
200eda14cbcSMatt Macy 
201eda14cbcSMatt Macy 		ASSERT(db->db_level == 1);
202eda14cbcSMatt Macy 
203eda14cbcSMatt Macy 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
204eda14cbcSMatt Macy 		err = dbuf_hold_impl(dn, db->db_level - 1,
205eda14cbcSMatt Macy 		    (db->db_blkid << epbs) + i, TRUE, FALSE, FTAG, &child);
206eda14cbcSMatt Macy 		rw_exit(&dn->dn_struct_rwlock);
207eda14cbcSMatt Macy 		if (err == ENOENT)
208eda14cbcSMatt Macy 			continue;
209eda14cbcSMatt Macy 		ASSERT(err == 0);
210eda14cbcSMatt Macy 		ASSERT(child->db_level == 0);
211eda14cbcSMatt Macy 		dr = dbuf_find_dirty_eq(child, txg);
212eda14cbcSMatt Macy 
213eda14cbcSMatt Macy 		/* data_old better be zeroed */
214eda14cbcSMatt Macy 		if (dr) {
215eda14cbcSMatt Macy 			buf = dr->dt.dl.dr_data->b_data;
216eda14cbcSMatt Macy 			for (j = 0; j < child->db.db_size >> 3; j++) {
217eda14cbcSMatt Macy 				if (buf[j] != 0) {
218eda14cbcSMatt Macy 					panic("freed data not zero: "
219eda14cbcSMatt Macy 					    "child=%p i=%d off=%d num=%d\n",
220eda14cbcSMatt Macy 					    (void *)child, i, off, num);
221eda14cbcSMatt Macy 				}
222eda14cbcSMatt Macy 			}
223eda14cbcSMatt Macy 		}
224eda14cbcSMatt Macy 
225eda14cbcSMatt Macy 		/*
226eda14cbcSMatt Macy 		 * db_data better be zeroed unless it's dirty in a
227eda14cbcSMatt Macy 		 * future txg.
228eda14cbcSMatt Macy 		 */
229eda14cbcSMatt Macy 		mutex_enter(&child->db_mtx);
230eda14cbcSMatt Macy 		buf = child->db.db_data;
231eda14cbcSMatt Macy 		if (buf != NULL && child->db_state != DB_FILL &&
232eda14cbcSMatt Macy 		    list_is_empty(&child->db_dirty_records)) {
233eda14cbcSMatt Macy 			for (j = 0; j < child->db.db_size >> 3; j++) {
234eda14cbcSMatt Macy 				if (buf[j] != 0) {
235eda14cbcSMatt Macy 					panic("freed data not zero: "
236eda14cbcSMatt Macy 					    "child=%p i=%d off=%d num=%d\n",
237eda14cbcSMatt Macy 					    (void *)child, i, off, num);
238eda14cbcSMatt Macy 				}
239eda14cbcSMatt Macy 			}
240eda14cbcSMatt Macy 		}
241eda14cbcSMatt Macy 		mutex_exit(&child->db_mtx);
242eda14cbcSMatt Macy 
243eda14cbcSMatt Macy 		dbuf_rele(child, FTAG);
244eda14cbcSMatt Macy 	}
245eda14cbcSMatt Macy 	DB_DNODE_EXIT(db);
246eda14cbcSMatt Macy }
247eda14cbcSMatt Macy #endif
248eda14cbcSMatt Macy 
249eda14cbcSMatt Macy /*
250eda14cbcSMatt Macy  * We don't usually free the indirect blocks here.  If in one txg we have a
251eda14cbcSMatt Macy  * free_range and a write to the same indirect block, it's important that we
252eda14cbcSMatt Macy  * preserve the hole's birth times. Therefore, we don't free any any indirect
253eda14cbcSMatt Macy  * blocks in free_children().  If an indirect block happens to turn into all
254eda14cbcSMatt Macy  * holes, it will be freed by dbuf_write_children_ready, which happens at a
255eda14cbcSMatt Macy  * point in the syncing process where we know for certain the contents of the
256eda14cbcSMatt Macy  * indirect block.
257eda14cbcSMatt Macy  *
258eda14cbcSMatt Macy  * However, if we're freeing a dnode, its space accounting must go to zero
259eda14cbcSMatt Macy  * before we actually try to free the dnode, or we will trip an assertion. In
260eda14cbcSMatt Macy  * addition, we know the case described above cannot occur, because the dnode is
261eda14cbcSMatt Macy  * being freed.  Therefore, we free the indirect blocks immediately in that
262eda14cbcSMatt Macy  * case.
263eda14cbcSMatt Macy  */
264eda14cbcSMatt Macy static void
265eda14cbcSMatt Macy free_children(dmu_buf_impl_t *db, uint64_t blkid, uint64_t nblks,
266eda14cbcSMatt Macy     boolean_t free_indirects, dmu_tx_t *tx)
267eda14cbcSMatt Macy {
268eda14cbcSMatt Macy 	dnode_t *dn;
269eda14cbcSMatt Macy 	blkptr_t *bp;
270eda14cbcSMatt Macy 	dmu_buf_impl_t *subdb;
271eda14cbcSMatt Macy 	uint64_t start, end, dbstart, dbend;
272eda14cbcSMatt Macy 	unsigned int epbs, shift, i;
273eda14cbcSMatt Macy 
274eda14cbcSMatt Macy 	/*
275eda14cbcSMatt Macy 	 * There is a small possibility that this block will not be cached:
276eda14cbcSMatt Macy 	 *   1 - if level > 1 and there are no children with level <= 1
277eda14cbcSMatt Macy 	 *   2 - if this block was evicted since we read it from
278eda14cbcSMatt Macy 	 *	 dmu_tx_hold_free().
279eda14cbcSMatt Macy 	 */
280eda14cbcSMatt Macy 	if (db->db_state != DB_CACHED)
281eda14cbcSMatt Macy 		(void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
282eda14cbcSMatt Macy 
283eda14cbcSMatt Macy 	/*
284eda14cbcSMatt Macy 	 * If we modify this indirect block, and we are not freeing the
285eda14cbcSMatt Macy 	 * dnode (!free_indirects), then this indirect block needs to get
286eda14cbcSMatt Macy 	 * written to disk by dbuf_write().  If it is dirty, we know it will
287eda14cbcSMatt Macy 	 * be written (otherwise, we would have incorrect on-disk state
288eda14cbcSMatt Macy 	 * because the space would be freed but still referenced by the BP
289eda14cbcSMatt Macy 	 * in this indirect block).  Therefore we VERIFY that it is
290eda14cbcSMatt Macy 	 * dirty.
291eda14cbcSMatt Macy 	 *
292eda14cbcSMatt Macy 	 * Our VERIFY covers some cases that do not actually have to be
293eda14cbcSMatt Macy 	 * dirty, but the open-context code happens to dirty.  E.g. if the
294eda14cbcSMatt Macy 	 * blocks we are freeing are all holes, because in that case, we
295eda14cbcSMatt Macy 	 * are only freeing part of this indirect block, so it is an
296eda14cbcSMatt Macy 	 * ancestor of the first or last block to be freed.  The first and
297eda14cbcSMatt Macy 	 * last L1 indirect blocks are always dirtied by dnode_free_range().
298eda14cbcSMatt Macy 	 */
299eda14cbcSMatt Macy 	db_lock_type_t dblt = dmu_buf_lock_parent(db, RW_READER, FTAG);
300eda14cbcSMatt Macy 	VERIFY(BP_GET_FILL(db->db_blkptr) == 0 || db->db_dirtycnt > 0);
301eda14cbcSMatt Macy 	dmu_buf_unlock_parent(db, dblt, FTAG);
302eda14cbcSMatt Macy 
303eda14cbcSMatt Macy 	dbuf_release_bp(db);
304eda14cbcSMatt Macy 	bp = db->db.db_data;
305eda14cbcSMatt Macy 
306eda14cbcSMatt Macy 	DB_DNODE_ENTER(db);
307eda14cbcSMatt Macy 	dn = DB_DNODE(db);
308eda14cbcSMatt Macy 	epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
309eda14cbcSMatt Macy 	ASSERT3U(epbs, <, 31);
310eda14cbcSMatt Macy 	shift = (db->db_level - 1) * epbs;
311eda14cbcSMatt Macy 	dbstart = db->db_blkid << epbs;
312eda14cbcSMatt Macy 	start = blkid >> shift;
313eda14cbcSMatt Macy 	if (dbstart < start) {
314eda14cbcSMatt Macy 		bp += start - dbstart;
315eda14cbcSMatt Macy 	} else {
316eda14cbcSMatt Macy 		start = dbstart;
317eda14cbcSMatt Macy 	}
318eda14cbcSMatt Macy 	dbend = ((db->db_blkid + 1) << epbs) - 1;
319eda14cbcSMatt Macy 	end = (blkid + nblks - 1) >> shift;
320eda14cbcSMatt Macy 	if (dbend <= end)
321eda14cbcSMatt Macy 		end = dbend;
322eda14cbcSMatt Macy 
323eda14cbcSMatt Macy 	ASSERT3U(start, <=, end);
324eda14cbcSMatt Macy 
325eda14cbcSMatt Macy 	if (db->db_level == 1) {
326eda14cbcSMatt Macy 		FREE_VERIFY(db, start, end, tx);
327eda14cbcSMatt Macy 		rw_enter(&db->db_rwlock, RW_WRITER);
328eda14cbcSMatt Macy 		free_blocks(dn, bp, end - start + 1, tx);
329eda14cbcSMatt Macy 		rw_exit(&db->db_rwlock);
330eda14cbcSMatt Macy 	} else {
331eda14cbcSMatt Macy 		for (uint64_t id = start; id <= end; id++, bp++) {
332eda14cbcSMatt Macy 			if (BP_IS_HOLE(bp))
333eda14cbcSMatt Macy 				continue;
334eda14cbcSMatt Macy 			rw_enter(&dn->dn_struct_rwlock, RW_READER);
335eda14cbcSMatt Macy 			VERIFY0(dbuf_hold_impl(dn, db->db_level - 1,
336eda14cbcSMatt Macy 			    id, TRUE, FALSE, FTAG, &subdb));
337eda14cbcSMatt Macy 			rw_exit(&dn->dn_struct_rwlock);
338eda14cbcSMatt Macy 			ASSERT3P(bp, ==, subdb->db_blkptr);
339eda14cbcSMatt Macy 
340eda14cbcSMatt Macy 			free_children(subdb, blkid, nblks, free_indirects, tx);
341eda14cbcSMatt Macy 			dbuf_rele(subdb, FTAG);
342eda14cbcSMatt Macy 		}
343eda14cbcSMatt Macy 	}
344eda14cbcSMatt Macy 
345eda14cbcSMatt Macy 	if (free_indirects) {
346eda14cbcSMatt Macy 		rw_enter(&db->db_rwlock, RW_WRITER);
347eda14cbcSMatt Macy 		for (i = 0, bp = db->db.db_data; i < 1 << epbs; i++, bp++)
348eda14cbcSMatt Macy 			ASSERT(BP_IS_HOLE(bp));
349eda14cbcSMatt Macy 		bzero(db->db.db_data, db->db.db_size);
350eda14cbcSMatt Macy 		free_blocks(dn, db->db_blkptr, 1, tx);
351eda14cbcSMatt Macy 		rw_exit(&db->db_rwlock);
352eda14cbcSMatt Macy 	}
353eda14cbcSMatt Macy 
354eda14cbcSMatt Macy 	DB_DNODE_EXIT(db);
355eda14cbcSMatt Macy 	arc_buf_freeze(db->db_buf);
356eda14cbcSMatt Macy }
357eda14cbcSMatt Macy 
358eda14cbcSMatt Macy /*
359eda14cbcSMatt Macy  * Traverse the indicated range of the provided file
360eda14cbcSMatt Macy  * and "free" all the blocks contained there.
361eda14cbcSMatt Macy  */
362eda14cbcSMatt Macy static void
363eda14cbcSMatt Macy dnode_sync_free_range_impl(dnode_t *dn, uint64_t blkid, uint64_t nblks,
364eda14cbcSMatt Macy     boolean_t free_indirects, dmu_tx_t *tx)
365eda14cbcSMatt Macy {
366eda14cbcSMatt Macy 	blkptr_t *bp = dn->dn_phys->dn_blkptr;
367eda14cbcSMatt Macy 	int dnlevel = dn->dn_phys->dn_nlevels;
368eda14cbcSMatt Macy 	boolean_t trunc = B_FALSE;
369eda14cbcSMatt Macy 
370eda14cbcSMatt Macy 	if (blkid > dn->dn_phys->dn_maxblkid)
371eda14cbcSMatt Macy 		return;
372eda14cbcSMatt Macy 
373eda14cbcSMatt Macy 	ASSERT(dn->dn_phys->dn_maxblkid < UINT64_MAX);
374eda14cbcSMatt Macy 	if (blkid + nblks > dn->dn_phys->dn_maxblkid) {
375eda14cbcSMatt Macy 		nblks = dn->dn_phys->dn_maxblkid - blkid + 1;
376eda14cbcSMatt Macy 		trunc = B_TRUE;
377eda14cbcSMatt Macy 	}
378eda14cbcSMatt Macy 
379eda14cbcSMatt Macy 	/* There are no indirect blocks in the object */
380eda14cbcSMatt Macy 	if (dnlevel == 1) {
381eda14cbcSMatt Macy 		if (blkid >= dn->dn_phys->dn_nblkptr) {
382eda14cbcSMatt Macy 			/* this range was never made persistent */
383eda14cbcSMatt Macy 			return;
384eda14cbcSMatt Macy 		}
385eda14cbcSMatt Macy 		ASSERT3U(blkid + nblks, <=, dn->dn_phys->dn_nblkptr);
386eda14cbcSMatt Macy 		free_blocks(dn, bp + blkid, nblks, tx);
387eda14cbcSMatt Macy 	} else {
388eda14cbcSMatt Macy 		int shift = (dnlevel - 1) *
389eda14cbcSMatt Macy 		    (dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT);
390eda14cbcSMatt Macy 		int start = blkid >> shift;
391eda14cbcSMatt Macy 		int end = (blkid + nblks - 1) >> shift;
392eda14cbcSMatt Macy 		dmu_buf_impl_t *db;
393eda14cbcSMatt Macy 
394eda14cbcSMatt Macy 		ASSERT(start < dn->dn_phys->dn_nblkptr);
395eda14cbcSMatt Macy 		bp += start;
396eda14cbcSMatt Macy 		for (int i = start; i <= end; i++, bp++) {
397eda14cbcSMatt Macy 			if (BP_IS_HOLE(bp))
398eda14cbcSMatt Macy 				continue;
399eda14cbcSMatt Macy 			rw_enter(&dn->dn_struct_rwlock, RW_READER);
400eda14cbcSMatt Macy 			VERIFY0(dbuf_hold_impl(dn, dnlevel - 1, i,
401eda14cbcSMatt Macy 			    TRUE, FALSE, FTAG, &db));
402eda14cbcSMatt Macy 			rw_exit(&dn->dn_struct_rwlock);
403eda14cbcSMatt Macy 			free_children(db, blkid, nblks, free_indirects, tx);
404eda14cbcSMatt Macy 			dbuf_rele(db, FTAG);
405eda14cbcSMatt Macy 		}
406eda14cbcSMatt Macy 	}
407eda14cbcSMatt Macy 
408eda14cbcSMatt Macy 	/*
409eda14cbcSMatt Macy 	 * Do not truncate the maxblkid if we are performing a raw
410eda14cbcSMatt Macy 	 * receive. The raw receive sets the maxblkid manually and
411eda14cbcSMatt Macy 	 * must not be overridden. Usually, the last DRR_FREE record
412eda14cbcSMatt Macy 	 * will be at the maxblkid, because the source system sets
413eda14cbcSMatt Macy 	 * the maxblkid when truncating. However, if the last block
414eda14cbcSMatt Macy 	 * was freed by overwriting with zeros and being compressed
415eda14cbcSMatt Macy 	 * away to a hole, the source system will generate a DRR_FREE
416eda14cbcSMatt Macy 	 * record while leaving the maxblkid after the end of that
417eda14cbcSMatt Macy 	 * record. In this case we need to leave the maxblkid as
418eda14cbcSMatt Macy 	 * indicated in the DRR_OBJECT record, so that it matches the
419eda14cbcSMatt Macy 	 * source system, ensuring that the cryptographic hashes will
420eda14cbcSMatt Macy 	 * match.
421eda14cbcSMatt Macy 	 */
422eda14cbcSMatt Macy 	if (trunc && !dn->dn_objset->os_raw_receive) {
423eda14cbcSMatt Macy 		uint64_t off __maybe_unused;
424eda14cbcSMatt Macy 		dn->dn_phys->dn_maxblkid = blkid == 0 ? 0 : blkid - 1;
425eda14cbcSMatt Macy 
426eda14cbcSMatt Macy 		off = (dn->dn_phys->dn_maxblkid + 1) *
427eda14cbcSMatt Macy 		    (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT);
428eda14cbcSMatt Macy 		ASSERT(off < dn->dn_phys->dn_maxblkid ||
429eda14cbcSMatt Macy 		    dn->dn_phys->dn_maxblkid == 0 ||
430eda14cbcSMatt Macy 		    dnode_next_offset(dn, 0, &off, 1, 1, 0) != 0);
431eda14cbcSMatt Macy 	}
432eda14cbcSMatt Macy }
433eda14cbcSMatt Macy 
434eda14cbcSMatt Macy typedef struct dnode_sync_free_range_arg {
435eda14cbcSMatt Macy 	dnode_t *dsfra_dnode;
436eda14cbcSMatt Macy 	dmu_tx_t *dsfra_tx;
437eda14cbcSMatt Macy 	boolean_t dsfra_free_indirects;
438eda14cbcSMatt Macy } dnode_sync_free_range_arg_t;
439eda14cbcSMatt Macy 
440eda14cbcSMatt Macy static void
441eda14cbcSMatt Macy dnode_sync_free_range(void *arg, uint64_t blkid, uint64_t nblks)
442eda14cbcSMatt Macy {
443eda14cbcSMatt Macy 	dnode_sync_free_range_arg_t *dsfra = arg;
444eda14cbcSMatt Macy 	dnode_t *dn = dsfra->dsfra_dnode;
445eda14cbcSMatt Macy 
446eda14cbcSMatt Macy 	mutex_exit(&dn->dn_mtx);
447eda14cbcSMatt Macy 	dnode_sync_free_range_impl(dn, blkid, nblks,
448eda14cbcSMatt Macy 	    dsfra->dsfra_free_indirects, dsfra->dsfra_tx);
449eda14cbcSMatt Macy 	mutex_enter(&dn->dn_mtx);
450eda14cbcSMatt Macy }
451eda14cbcSMatt Macy 
452eda14cbcSMatt Macy /*
453eda14cbcSMatt Macy  * Try to kick all the dnode's dbufs out of the cache...
454eda14cbcSMatt Macy  */
455eda14cbcSMatt Macy void
456eda14cbcSMatt Macy dnode_evict_dbufs(dnode_t *dn)
457eda14cbcSMatt Macy {
458eda14cbcSMatt Macy 	dmu_buf_impl_t *db_marker;
459eda14cbcSMatt Macy 	dmu_buf_impl_t *db, *db_next;
460eda14cbcSMatt Macy 
461eda14cbcSMatt Macy 	db_marker = kmem_alloc(sizeof (dmu_buf_impl_t), KM_SLEEP);
462eda14cbcSMatt Macy 
463eda14cbcSMatt Macy 	mutex_enter(&dn->dn_dbufs_mtx);
464eda14cbcSMatt Macy 	for (db = avl_first(&dn->dn_dbufs); db != NULL; db = db_next) {
465eda14cbcSMatt Macy 
466eda14cbcSMatt Macy #ifdef	ZFS_DEBUG
467eda14cbcSMatt Macy 		DB_DNODE_ENTER(db);
468eda14cbcSMatt Macy 		ASSERT3P(DB_DNODE(db), ==, dn);
469eda14cbcSMatt Macy 		DB_DNODE_EXIT(db);
470eda14cbcSMatt Macy #endif	/* DEBUG */
471eda14cbcSMatt Macy 
472eda14cbcSMatt Macy 		mutex_enter(&db->db_mtx);
473eda14cbcSMatt Macy 		if (db->db_state != DB_EVICTING &&
474eda14cbcSMatt Macy 		    zfs_refcount_is_zero(&db->db_holds)) {
475eda14cbcSMatt Macy 			db_marker->db_level = db->db_level;
476eda14cbcSMatt Macy 			db_marker->db_blkid = db->db_blkid;
477eda14cbcSMatt Macy 			db_marker->db_state = DB_SEARCH;
478eda14cbcSMatt Macy 			avl_insert_here(&dn->dn_dbufs, db_marker, db,
479eda14cbcSMatt Macy 			    AVL_BEFORE);
480eda14cbcSMatt Macy 
481eda14cbcSMatt Macy 			/*
482eda14cbcSMatt Macy 			 * We need to use the "marker" dbuf rather than
483eda14cbcSMatt Macy 			 * simply getting the next dbuf, because
484eda14cbcSMatt Macy 			 * dbuf_destroy() may actually remove multiple dbufs.
485eda14cbcSMatt Macy 			 * It can call itself recursively on the parent dbuf,
486eda14cbcSMatt Macy 			 * which may also be removed from dn_dbufs.  The code
487eda14cbcSMatt Macy 			 * flow would look like:
488eda14cbcSMatt Macy 			 *
489eda14cbcSMatt Macy 			 * dbuf_destroy():
490eda14cbcSMatt Macy 			 *   dnode_rele_and_unlock(parent_dbuf, evicting=TRUE):
491eda14cbcSMatt Macy 			 *	if (!cacheable || pending_evict)
492eda14cbcSMatt Macy 			 *	  dbuf_destroy()
493eda14cbcSMatt Macy 			 */
494eda14cbcSMatt Macy 			dbuf_destroy(db);
495eda14cbcSMatt Macy 
496eda14cbcSMatt Macy 			db_next = AVL_NEXT(&dn->dn_dbufs, db_marker);
497eda14cbcSMatt Macy 			avl_remove(&dn->dn_dbufs, db_marker);
498eda14cbcSMatt Macy 		} else {
499eda14cbcSMatt Macy 			db->db_pending_evict = TRUE;
500eda14cbcSMatt Macy 			mutex_exit(&db->db_mtx);
501eda14cbcSMatt Macy 			db_next = AVL_NEXT(&dn->dn_dbufs, db);
502eda14cbcSMatt Macy 		}
503eda14cbcSMatt Macy 	}
504eda14cbcSMatt Macy 	mutex_exit(&dn->dn_dbufs_mtx);
505eda14cbcSMatt Macy 
506eda14cbcSMatt Macy 	kmem_free(db_marker, sizeof (dmu_buf_impl_t));
507eda14cbcSMatt Macy 
508eda14cbcSMatt Macy 	dnode_evict_bonus(dn);
509eda14cbcSMatt Macy }
510eda14cbcSMatt Macy 
511eda14cbcSMatt Macy void
512eda14cbcSMatt Macy dnode_evict_bonus(dnode_t *dn)
513eda14cbcSMatt Macy {
514eda14cbcSMatt Macy 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
515eda14cbcSMatt Macy 	if (dn->dn_bonus != NULL) {
516eda14cbcSMatt Macy 		if (zfs_refcount_is_zero(&dn->dn_bonus->db_holds)) {
517eda14cbcSMatt Macy 			mutex_enter(&dn->dn_bonus->db_mtx);
518eda14cbcSMatt Macy 			dbuf_destroy(dn->dn_bonus);
519eda14cbcSMatt Macy 			dn->dn_bonus = NULL;
520eda14cbcSMatt Macy 		} else {
521eda14cbcSMatt Macy 			dn->dn_bonus->db_pending_evict = TRUE;
522eda14cbcSMatt Macy 		}
523eda14cbcSMatt Macy 	}
524eda14cbcSMatt Macy 	rw_exit(&dn->dn_struct_rwlock);
525eda14cbcSMatt Macy }
526eda14cbcSMatt Macy 
527eda14cbcSMatt Macy static void
528eda14cbcSMatt Macy dnode_undirty_dbufs(list_t *list)
529eda14cbcSMatt Macy {
530eda14cbcSMatt Macy 	dbuf_dirty_record_t *dr;
531eda14cbcSMatt Macy 
532eda14cbcSMatt Macy 	while ((dr = list_head(list))) {
533eda14cbcSMatt Macy 		dmu_buf_impl_t *db = dr->dr_dbuf;
534eda14cbcSMatt Macy 		uint64_t txg = dr->dr_txg;
535eda14cbcSMatt Macy 
536eda14cbcSMatt Macy 		if (db->db_level != 0)
537eda14cbcSMatt Macy 			dnode_undirty_dbufs(&dr->dt.di.dr_children);
538eda14cbcSMatt Macy 
539eda14cbcSMatt Macy 		mutex_enter(&db->db_mtx);
540eda14cbcSMatt Macy 		/* XXX - use dbuf_undirty()? */
541eda14cbcSMatt Macy 		list_remove(list, dr);
542eda14cbcSMatt Macy 		ASSERT(list_head(&db->db_dirty_records) == dr);
543eda14cbcSMatt Macy 		list_remove_head(&db->db_dirty_records);
544eda14cbcSMatt Macy 		ASSERT(list_is_empty(&db->db_dirty_records));
545eda14cbcSMatt Macy 		db->db_dirtycnt -= 1;
546eda14cbcSMatt Macy 		if (db->db_level == 0) {
547eda14cbcSMatt Macy 			ASSERT(db->db_blkid == DMU_BONUS_BLKID ||
548eda14cbcSMatt Macy 			    dr->dt.dl.dr_data == db->db_buf);
549eda14cbcSMatt Macy 			dbuf_unoverride(dr);
550eda14cbcSMatt Macy 		} else {
551eda14cbcSMatt Macy 			mutex_destroy(&dr->dt.di.dr_mtx);
552eda14cbcSMatt Macy 			list_destroy(&dr->dt.di.dr_children);
553eda14cbcSMatt Macy 		}
554eda14cbcSMatt Macy 		kmem_free(dr, sizeof (dbuf_dirty_record_t));
555eda14cbcSMatt Macy 		dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg, B_FALSE);
556eda14cbcSMatt Macy 	}
557eda14cbcSMatt Macy }
558eda14cbcSMatt Macy 
559eda14cbcSMatt Macy static void
560eda14cbcSMatt Macy dnode_sync_free(dnode_t *dn, dmu_tx_t *tx)
561eda14cbcSMatt Macy {
562eda14cbcSMatt Macy 	int txgoff = tx->tx_txg & TXG_MASK;
563eda14cbcSMatt Macy 
564eda14cbcSMatt Macy 	ASSERT(dmu_tx_is_syncing(tx));
565eda14cbcSMatt Macy 
566eda14cbcSMatt Macy 	/*
567eda14cbcSMatt Macy 	 * Our contents should have been freed in dnode_sync() by the
568eda14cbcSMatt Macy 	 * free range record inserted by the caller of dnode_free().
569eda14cbcSMatt Macy 	 */
570eda14cbcSMatt Macy 	ASSERT0(DN_USED_BYTES(dn->dn_phys));
571eda14cbcSMatt Macy 	ASSERT(BP_IS_HOLE(dn->dn_phys->dn_blkptr));
572eda14cbcSMatt Macy 
573eda14cbcSMatt Macy 	dnode_undirty_dbufs(&dn->dn_dirty_records[txgoff]);
574eda14cbcSMatt Macy 	dnode_evict_dbufs(dn);
575eda14cbcSMatt Macy 
576eda14cbcSMatt Macy 	/*
577eda14cbcSMatt Macy 	 * XXX - It would be nice to assert this, but we may still
578eda14cbcSMatt Macy 	 * have residual holds from async evictions from the arc...
579eda14cbcSMatt Macy 	 *
580eda14cbcSMatt Macy 	 * zfs_obj_to_path() also depends on this being
581eda14cbcSMatt Macy 	 * commented out.
582eda14cbcSMatt Macy 	 *
583eda14cbcSMatt Macy 	 * ASSERT3U(zfs_refcount_count(&dn->dn_holds), ==, 1);
584eda14cbcSMatt Macy 	 */
585eda14cbcSMatt Macy 
586eda14cbcSMatt Macy 	/* Undirty next bits */
587eda14cbcSMatt Macy 	dn->dn_next_nlevels[txgoff] = 0;
588eda14cbcSMatt Macy 	dn->dn_next_indblkshift[txgoff] = 0;
589eda14cbcSMatt Macy 	dn->dn_next_blksz[txgoff] = 0;
590eda14cbcSMatt Macy 	dn->dn_next_maxblkid[txgoff] = 0;
591eda14cbcSMatt Macy 
592eda14cbcSMatt Macy 	/* ASSERT(blkptrs are zero); */
593eda14cbcSMatt Macy 	ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE);
594eda14cbcSMatt Macy 	ASSERT(dn->dn_type != DMU_OT_NONE);
595eda14cbcSMatt Macy 
596eda14cbcSMatt Macy 	ASSERT(dn->dn_free_txg > 0);
597eda14cbcSMatt Macy 	if (dn->dn_allocated_txg != dn->dn_free_txg)
598eda14cbcSMatt Macy 		dmu_buf_will_dirty(&dn->dn_dbuf->db, tx);
599eda14cbcSMatt Macy 	bzero(dn->dn_phys, sizeof (dnode_phys_t) * dn->dn_num_slots);
600eda14cbcSMatt Macy 	dnode_free_interior_slots(dn);
601eda14cbcSMatt Macy 
602eda14cbcSMatt Macy 	mutex_enter(&dn->dn_mtx);
603eda14cbcSMatt Macy 	dn->dn_type = DMU_OT_NONE;
604eda14cbcSMatt Macy 	dn->dn_maxblkid = 0;
605eda14cbcSMatt Macy 	dn->dn_allocated_txg = 0;
606eda14cbcSMatt Macy 	dn->dn_free_txg = 0;
607eda14cbcSMatt Macy 	dn->dn_have_spill = B_FALSE;
608eda14cbcSMatt Macy 	dn->dn_num_slots = 1;
609eda14cbcSMatt Macy 	mutex_exit(&dn->dn_mtx);
610eda14cbcSMatt Macy 
611eda14cbcSMatt Macy 	ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
612eda14cbcSMatt Macy 
613eda14cbcSMatt Macy 	dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg);
614eda14cbcSMatt Macy 	/*
615eda14cbcSMatt Macy 	 * Now that we've released our hold, the dnode may
616eda14cbcSMatt Macy 	 * be evicted, so we mustn't access it.
617eda14cbcSMatt Macy 	 */
618eda14cbcSMatt Macy }
619eda14cbcSMatt Macy 
620eda14cbcSMatt Macy /*
621eda14cbcSMatt Macy  * Write out the dnode's dirty buffers.
622eda14cbcSMatt Macy  */
623eda14cbcSMatt Macy void
624eda14cbcSMatt Macy dnode_sync(dnode_t *dn, dmu_tx_t *tx)
625eda14cbcSMatt Macy {
626eda14cbcSMatt Macy 	objset_t *os = dn->dn_objset;
627eda14cbcSMatt Macy 	dnode_phys_t *dnp = dn->dn_phys;
628eda14cbcSMatt Macy 	int txgoff = tx->tx_txg & TXG_MASK;
629eda14cbcSMatt Macy 	list_t *list = &dn->dn_dirty_records[txgoff];
630eda14cbcSMatt Macy 	static const dnode_phys_t zerodn __maybe_unused = { 0 };
631eda14cbcSMatt Macy 	boolean_t kill_spill = B_FALSE;
632eda14cbcSMatt Macy 
633eda14cbcSMatt Macy 	ASSERT(dmu_tx_is_syncing(tx));
634eda14cbcSMatt Macy 	ASSERT(dnp->dn_type != DMU_OT_NONE || dn->dn_allocated_txg);
635eda14cbcSMatt Macy 	ASSERT(dnp->dn_type != DMU_OT_NONE ||
636eda14cbcSMatt Macy 	    bcmp(dnp, &zerodn, DNODE_MIN_SIZE) == 0);
637eda14cbcSMatt Macy 	DNODE_VERIFY(dn);
638eda14cbcSMatt Macy 
639eda14cbcSMatt Macy 	ASSERT(dn->dn_dbuf == NULL || arc_released(dn->dn_dbuf->db_buf));
640eda14cbcSMatt Macy 
641eda14cbcSMatt Macy 	/*
642eda14cbcSMatt Macy 	 * Do user accounting if it is enabled and this is not
643eda14cbcSMatt Macy 	 * an encrypted receive.
644eda14cbcSMatt Macy 	 */
645eda14cbcSMatt Macy 	if (dmu_objset_userused_enabled(os) &&
646eda14cbcSMatt Macy 	    !DMU_OBJECT_IS_SPECIAL(dn->dn_object) &&
647eda14cbcSMatt Macy 	    (!os->os_encrypted || !dmu_objset_is_receiving(os))) {
648eda14cbcSMatt Macy 		mutex_enter(&dn->dn_mtx);
649eda14cbcSMatt Macy 		dn->dn_oldused = DN_USED_BYTES(dn->dn_phys);
650eda14cbcSMatt Macy 		dn->dn_oldflags = dn->dn_phys->dn_flags;
651eda14cbcSMatt Macy 		dn->dn_phys->dn_flags |= DNODE_FLAG_USERUSED_ACCOUNTED;
652eda14cbcSMatt Macy 		if (dmu_objset_userobjused_enabled(dn->dn_objset))
653eda14cbcSMatt Macy 			dn->dn_phys->dn_flags |=
654eda14cbcSMatt Macy 			    DNODE_FLAG_USEROBJUSED_ACCOUNTED;
655eda14cbcSMatt Macy 		mutex_exit(&dn->dn_mtx);
656eda14cbcSMatt Macy 		dmu_objset_userquota_get_ids(dn, B_FALSE, tx);
657eda14cbcSMatt Macy 	} else {
658eda14cbcSMatt Macy 		/* Once we account for it, we should always account for it */
659eda14cbcSMatt Macy 		ASSERT(!(dn->dn_phys->dn_flags &
660eda14cbcSMatt Macy 		    DNODE_FLAG_USERUSED_ACCOUNTED));
661eda14cbcSMatt Macy 		ASSERT(!(dn->dn_phys->dn_flags &
662eda14cbcSMatt Macy 		    DNODE_FLAG_USEROBJUSED_ACCOUNTED));
663eda14cbcSMatt Macy 	}
664eda14cbcSMatt Macy 
665eda14cbcSMatt Macy 	mutex_enter(&dn->dn_mtx);
666eda14cbcSMatt Macy 	if (dn->dn_allocated_txg == tx->tx_txg) {
667eda14cbcSMatt Macy 		/* The dnode is newly allocated or reallocated */
668eda14cbcSMatt Macy 		if (dnp->dn_type == DMU_OT_NONE) {
669eda14cbcSMatt Macy 			/* this is a first alloc, not a realloc */
670eda14cbcSMatt Macy 			dnp->dn_nlevels = 1;
671eda14cbcSMatt Macy 			dnp->dn_nblkptr = dn->dn_nblkptr;
672eda14cbcSMatt Macy 		}
673eda14cbcSMatt Macy 
674eda14cbcSMatt Macy 		dnp->dn_type = dn->dn_type;
675eda14cbcSMatt Macy 		dnp->dn_bonustype = dn->dn_bonustype;
676eda14cbcSMatt Macy 		dnp->dn_bonuslen = dn->dn_bonuslen;
677eda14cbcSMatt Macy 	}
678eda14cbcSMatt Macy 
679eda14cbcSMatt Macy 	dnp->dn_extra_slots = dn->dn_num_slots - 1;
680eda14cbcSMatt Macy 
681eda14cbcSMatt Macy 	ASSERT(dnp->dn_nlevels > 1 ||
682eda14cbcSMatt Macy 	    BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
683eda14cbcSMatt Macy 	    BP_IS_EMBEDDED(&dnp->dn_blkptr[0]) ||
684eda14cbcSMatt Macy 	    BP_GET_LSIZE(&dnp->dn_blkptr[0]) ==
685eda14cbcSMatt Macy 	    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
686eda14cbcSMatt Macy 	ASSERT(dnp->dn_nlevels < 2 ||
687eda14cbcSMatt Macy 	    BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
688eda14cbcSMatt Macy 	    BP_GET_LSIZE(&dnp->dn_blkptr[0]) == 1 << dnp->dn_indblkshift);
689eda14cbcSMatt Macy 
690eda14cbcSMatt Macy 	if (dn->dn_next_type[txgoff] != 0) {
691eda14cbcSMatt Macy 		dnp->dn_type = dn->dn_type;
692eda14cbcSMatt Macy 		dn->dn_next_type[txgoff] = 0;
693eda14cbcSMatt Macy 	}
694eda14cbcSMatt Macy 
695eda14cbcSMatt Macy 	if (dn->dn_next_blksz[txgoff] != 0) {
696eda14cbcSMatt Macy 		ASSERT(P2PHASE(dn->dn_next_blksz[txgoff],
697eda14cbcSMatt Macy 		    SPA_MINBLOCKSIZE) == 0);
698eda14cbcSMatt Macy 		ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
699eda14cbcSMatt Macy 		    dn->dn_maxblkid == 0 || list_head(list) != NULL ||
700eda14cbcSMatt Macy 		    dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT ==
701eda14cbcSMatt Macy 		    dnp->dn_datablkszsec ||
702eda14cbcSMatt Macy 		    !range_tree_is_empty(dn->dn_free_ranges[txgoff]));
703eda14cbcSMatt Macy 		dnp->dn_datablkszsec =
704eda14cbcSMatt Macy 		    dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT;
705eda14cbcSMatt Macy 		dn->dn_next_blksz[txgoff] = 0;
706eda14cbcSMatt Macy 	}
707eda14cbcSMatt Macy 
708eda14cbcSMatt Macy 	if (dn->dn_next_bonuslen[txgoff] != 0) {
709eda14cbcSMatt Macy 		if (dn->dn_next_bonuslen[txgoff] == DN_ZERO_BONUSLEN)
710eda14cbcSMatt Macy 			dnp->dn_bonuslen = 0;
711eda14cbcSMatt Macy 		else
712eda14cbcSMatt Macy 			dnp->dn_bonuslen = dn->dn_next_bonuslen[txgoff];
713eda14cbcSMatt Macy 		ASSERT(dnp->dn_bonuslen <=
714eda14cbcSMatt Macy 		    DN_SLOTS_TO_BONUSLEN(dnp->dn_extra_slots + 1));
715eda14cbcSMatt Macy 		dn->dn_next_bonuslen[txgoff] = 0;
716eda14cbcSMatt Macy 	}
717eda14cbcSMatt Macy 
718eda14cbcSMatt Macy 	if (dn->dn_next_bonustype[txgoff] != 0) {
719eda14cbcSMatt Macy 		ASSERT(DMU_OT_IS_VALID(dn->dn_next_bonustype[txgoff]));
720eda14cbcSMatt Macy 		dnp->dn_bonustype = dn->dn_next_bonustype[txgoff];
721eda14cbcSMatt Macy 		dn->dn_next_bonustype[txgoff] = 0;
722eda14cbcSMatt Macy 	}
723eda14cbcSMatt Macy 
724eda14cbcSMatt Macy 	boolean_t freeing_dnode = dn->dn_free_txg > 0 &&
725eda14cbcSMatt Macy 	    dn->dn_free_txg <= tx->tx_txg;
726eda14cbcSMatt Macy 
727eda14cbcSMatt Macy 	/*
728eda14cbcSMatt Macy 	 * Remove the spill block if we have been explicitly asked to
729eda14cbcSMatt Macy 	 * remove it, or if the object is being removed.
730eda14cbcSMatt Macy 	 */
731eda14cbcSMatt Macy 	if (dn->dn_rm_spillblk[txgoff] || freeing_dnode) {
732eda14cbcSMatt Macy 		if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR)
733eda14cbcSMatt Macy 			kill_spill = B_TRUE;
734eda14cbcSMatt Macy 		dn->dn_rm_spillblk[txgoff] = 0;
735eda14cbcSMatt Macy 	}
736eda14cbcSMatt Macy 
737eda14cbcSMatt Macy 	if (dn->dn_next_indblkshift[txgoff] != 0) {
738eda14cbcSMatt Macy 		ASSERT(dnp->dn_nlevels == 1);
739eda14cbcSMatt Macy 		dnp->dn_indblkshift = dn->dn_next_indblkshift[txgoff];
740eda14cbcSMatt Macy 		dn->dn_next_indblkshift[txgoff] = 0;
741eda14cbcSMatt Macy 	}
742eda14cbcSMatt Macy 
743eda14cbcSMatt Macy 	/*
744eda14cbcSMatt Macy 	 * Just take the live (open-context) values for checksum and compress.
745eda14cbcSMatt Macy 	 * Strictly speaking it's a future leak, but nothing bad happens if we
746eda14cbcSMatt Macy 	 * start using the new checksum or compress algorithm a little early.
747eda14cbcSMatt Macy 	 */
748eda14cbcSMatt Macy 	dnp->dn_checksum = dn->dn_checksum;
749eda14cbcSMatt Macy 	dnp->dn_compress = dn->dn_compress;
750eda14cbcSMatt Macy 
751eda14cbcSMatt Macy 	mutex_exit(&dn->dn_mtx);
752eda14cbcSMatt Macy 
753eda14cbcSMatt Macy 	if (kill_spill) {
754eda14cbcSMatt Macy 		free_blocks(dn, DN_SPILL_BLKPTR(dn->dn_phys), 1, tx);
755eda14cbcSMatt Macy 		mutex_enter(&dn->dn_mtx);
756eda14cbcSMatt Macy 		dnp->dn_flags &= ~DNODE_FLAG_SPILL_BLKPTR;
757eda14cbcSMatt Macy 		mutex_exit(&dn->dn_mtx);
758eda14cbcSMatt Macy 	}
759eda14cbcSMatt Macy 
760eda14cbcSMatt Macy 	/* process all the "freed" ranges in the file */
761eda14cbcSMatt Macy 	if (dn->dn_free_ranges[txgoff] != NULL) {
762eda14cbcSMatt Macy 		dnode_sync_free_range_arg_t dsfra;
763eda14cbcSMatt Macy 		dsfra.dsfra_dnode = dn;
764eda14cbcSMatt Macy 		dsfra.dsfra_tx = tx;
765eda14cbcSMatt Macy 		dsfra.dsfra_free_indirects = freeing_dnode;
766*ac0bf12eSMatt Macy 		mutex_enter(&dn->dn_mtx);
767eda14cbcSMatt Macy 		if (freeing_dnode) {
768eda14cbcSMatt Macy 			ASSERT(range_tree_contains(dn->dn_free_ranges[txgoff],
769eda14cbcSMatt Macy 			    0, dn->dn_maxblkid + 1));
770eda14cbcSMatt Macy 		}
771*ac0bf12eSMatt Macy 		/*
772*ac0bf12eSMatt Macy 		 * Because dnode_sync_free_range() must drop dn_mtx during its
773*ac0bf12eSMatt Macy 		 * processing, using it as a callback to range_tree_vacate() is
774*ac0bf12eSMatt Macy 		 * not safe.  No other operations (besides destroy) are allowed
775*ac0bf12eSMatt Macy 		 * once range_tree_vacate() has begun, and dropping dn_mtx
776*ac0bf12eSMatt Macy 		 * would leave a window open for another thread to observe that
777*ac0bf12eSMatt Macy 		 * invalid (and unsafe) state.
778*ac0bf12eSMatt Macy 		 */
779*ac0bf12eSMatt Macy 		range_tree_walk(dn->dn_free_ranges[txgoff],
780eda14cbcSMatt Macy 		    dnode_sync_free_range, &dsfra);
781*ac0bf12eSMatt Macy 		range_tree_vacate(dn->dn_free_ranges[txgoff], NULL, NULL);
782eda14cbcSMatt Macy 		range_tree_destroy(dn->dn_free_ranges[txgoff]);
783eda14cbcSMatt Macy 		dn->dn_free_ranges[txgoff] = NULL;
784eda14cbcSMatt Macy 		mutex_exit(&dn->dn_mtx);
785eda14cbcSMatt Macy 	}
786eda14cbcSMatt Macy 
787eda14cbcSMatt Macy 	if (freeing_dnode) {
788eda14cbcSMatt Macy 		dn->dn_objset->os_freed_dnodes++;
789eda14cbcSMatt Macy 		dnode_sync_free(dn, tx);
790eda14cbcSMatt Macy 		return;
791eda14cbcSMatt Macy 	}
792eda14cbcSMatt Macy 
793eda14cbcSMatt Macy 	if (dn->dn_num_slots > DNODE_MIN_SLOTS) {
794eda14cbcSMatt Macy 		dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
795eda14cbcSMatt Macy 		mutex_enter(&ds->ds_lock);
796eda14cbcSMatt Macy 		ds->ds_feature_activation[SPA_FEATURE_LARGE_DNODE] =
797eda14cbcSMatt Macy 		    (void *)B_TRUE;
798eda14cbcSMatt Macy 		mutex_exit(&ds->ds_lock);
799eda14cbcSMatt Macy 	}
800eda14cbcSMatt Macy 
801eda14cbcSMatt Macy 	if (dn->dn_next_nlevels[txgoff]) {
802eda14cbcSMatt Macy 		dnode_increase_indirection(dn, tx);
803eda14cbcSMatt Macy 		dn->dn_next_nlevels[txgoff] = 0;
804eda14cbcSMatt Macy 	}
805eda14cbcSMatt Macy 
806eda14cbcSMatt Macy 	/*
807eda14cbcSMatt Macy 	 * This must be done after dnode_sync_free_range()
808eda14cbcSMatt Macy 	 * and dnode_increase_indirection(). See dnode_new_blkid()
809eda14cbcSMatt Macy 	 * for an explanation of the high bit being set.
810eda14cbcSMatt Macy 	 */
811eda14cbcSMatt Macy 	if (dn->dn_next_maxblkid[txgoff]) {
812eda14cbcSMatt Macy 		mutex_enter(&dn->dn_mtx);
813eda14cbcSMatt Macy 		dnp->dn_maxblkid =
814eda14cbcSMatt Macy 		    dn->dn_next_maxblkid[txgoff] & ~DMU_NEXT_MAXBLKID_SET;
815eda14cbcSMatt Macy 		dn->dn_next_maxblkid[txgoff] = 0;
816eda14cbcSMatt Macy 		mutex_exit(&dn->dn_mtx);
817eda14cbcSMatt Macy 	}
818eda14cbcSMatt Macy 
819eda14cbcSMatt Macy 	if (dn->dn_next_nblkptr[txgoff]) {
820eda14cbcSMatt Macy 		/* this should only happen on a realloc */
821eda14cbcSMatt Macy 		ASSERT(dn->dn_allocated_txg == tx->tx_txg);
822eda14cbcSMatt Macy 		if (dn->dn_next_nblkptr[txgoff] > dnp->dn_nblkptr) {
823eda14cbcSMatt Macy 			/* zero the new blkptrs we are gaining */
824eda14cbcSMatt Macy 			bzero(dnp->dn_blkptr + dnp->dn_nblkptr,
825eda14cbcSMatt Macy 			    sizeof (blkptr_t) *
826eda14cbcSMatt Macy 			    (dn->dn_next_nblkptr[txgoff] - dnp->dn_nblkptr));
827eda14cbcSMatt Macy #ifdef ZFS_DEBUG
828eda14cbcSMatt Macy 		} else {
829eda14cbcSMatt Macy 			int i;
830eda14cbcSMatt Macy 			ASSERT(dn->dn_next_nblkptr[txgoff] < dnp->dn_nblkptr);
831eda14cbcSMatt Macy 			/* the blkptrs we are losing better be unallocated */
832eda14cbcSMatt Macy 			for (i = 0; i < dnp->dn_nblkptr; i++) {
833eda14cbcSMatt Macy 				if (i >= dn->dn_next_nblkptr[txgoff])
834eda14cbcSMatt Macy 					ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[i]));
835eda14cbcSMatt Macy 			}
836eda14cbcSMatt Macy #endif
837eda14cbcSMatt Macy 		}
838eda14cbcSMatt Macy 		mutex_enter(&dn->dn_mtx);
839eda14cbcSMatt Macy 		dnp->dn_nblkptr = dn->dn_next_nblkptr[txgoff];
840eda14cbcSMatt Macy 		dn->dn_next_nblkptr[txgoff] = 0;
841eda14cbcSMatt Macy 		mutex_exit(&dn->dn_mtx);
842eda14cbcSMatt Macy 	}
843eda14cbcSMatt Macy 
844eda14cbcSMatt Macy 	dbuf_sync_list(list, dn->dn_phys->dn_nlevels - 1, tx);
845eda14cbcSMatt Macy 
846eda14cbcSMatt Macy 	if (!DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
847eda14cbcSMatt Macy 		ASSERT3P(list_head(list), ==, NULL);
848eda14cbcSMatt Macy 		dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg);
849eda14cbcSMatt Macy 	}
850eda14cbcSMatt Macy 
851eda14cbcSMatt Macy 	/*
852eda14cbcSMatt Macy 	 * Although we have dropped our reference to the dnode, it
853eda14cbcSMatt Macy 	 * can't be evicted until its written, and we haven't yet
854eda14cbcSMatt Macy 	 * initiated the IO for the dnode's dbuf.
855eda14cbcSMatt Macy 	 */
856eda14cbcSMatt Macy }
857