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