xref: /onnv-gate/usr/src/uts/common/fs/zfs/dnode_sync.c (revision 6992:20c04e18c58c)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51544Seschrock  * Common Development and Distribution License (the "License").
61544Seschrock  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
22*6992Smaybee  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
27789Sahrens 
28789Sahrens #include <sys/zfs_context.h>
29789Sahrens #include <sys/dbuf.h>
30789Sahrens #include <sys/dnode.h>
31789Sahrens #include <sys/dmu.h>
32789Sahrens #include <sys/dmu_tx.h>
33789Sahrens #include <sys/dmu_objset.h>
34789Sahrens #include <sys/dsl_dataset.h>
35789Sahrens #include <sys/spa.h>
36789Sahrens 
37789Sahrens static void
38789Sahrens dnode_increase_indirection(dnode_t *dn, dmu_tx_t *tx)
39789Sahrens {
40789Sahrens 	dmu_buf_impl_t *db;
413547Smaybee 	int txgoff = tx->tx_txg & TXG_MASK;
423547Smaybee 	int nblkptr = dn->dn_phys->dn_nblkptr;
433547Smaybee 	int old_toplvl = dn->dn_phys->dn_nlevels - 1;
443547Smaybee 	int new_level = dn->dn_next_nlevels[txgoff];
45789Sahrens 	int i;
46789Sahrens 
473547Smaybee 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
483547Smaybee 
493547Smaybee 	/* this dnode can't be paged out because it's dirty */
50789Sahrens 	ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE);
51789Sahrens 	ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
523547Smaybee 	ASSERT(new_level > 1 && dn->dn_phys->dn_nlevels > 0);
53789Sahrens 
54789Sahrens 	db = dbuf_hold_level(dn, dn->dn_phys->dn_nlevels, 0, FTAG);
551544Seschrock 	ASSERT(db != NULL);
56789Sahrens 
573547Smaybee 	dn->dn_phys->dn_nlevels = new_level;
584312Sgw25295 	dprintf("os=%p obj=%llu, increase to %d\n", dn->dn_objset,
594312Sgw25295 	    dn->dn_object, dn->dn_phys->dn_nlevels);
60789Sahrens 
613547Smaybee 	/* check for existing blkptrs in the dnode */
623547Smaybee 	for (i = 0; i < nblkptr; i++)
633547Smaybee 		if (!BP_IS_HOLE(&dn->dn_phys->dn_blkptr[i]))
643547Smaybee 			break;
653547Smaybee 	if (i != nblkptr) {
663547Smaybee 		/* transfer dnode's block pointers to new indirect block */
673547Smaybee 		(void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED|DB_RF_HAVESTRUCT);
683547Smaybee 		ASSERT(db->db.db_data);
693547Smaybee 		ASSERT(arc_released(db->db_buf));
703547Smaybee 		ASSERT3U(sizeof (blkptr_t) * nblkptr, <=, db->db.db_size);
713547Smaybee 		bcopy(dn->dn_phys->dn_blkptr, db->db.db_data,
723547Smaybee 		    sizeof (blkptr_t) * nblkptr);
733547Smaybee 		arc_buf_freeze(db->db_buf);
743547Smaybee 	}
753547Smaybee 
76789Sahrens 	/* set dbuf's parent pointers to new indirect buf */
773547Smaybee 	for (i = 0; i < nblkptr; i++) {
783547Smaybee 		dmu_buf_impl_t *child = dbuf_find(dn, old_toplvl, i);
793547Smaybee 
80789Sahrens 		if (child == NULL)
81789Sahrens 			continue;
823547Smaybee 		ASSERT3P(child->db_dnode, ==, dn);
833547Smaybee 		if (child->db_parent && child->db_parent != dn->dn_dbuf) {
843547Smaybee 			ASSERT(child->db_parent->db_level == db->db_level);
853547Smaybee 			ASSERT(child->db_blkptr !=
863547Smaybee 			    &dn->dn_phys->dn_blkptr[child->db_blkid]);
87789Sahrens 			mutex_exit(&child->db_mtx);
88789Sahrens 			continue;
89789Sahrens 		}
903547Smaybee 		ASSERT(child->db_parent == NULL ||
913547Smaybee 		    child->db_parent == dn->dn_dbuf);
92789Sahrens 
933547Smaybee 		child->db_parent = db;
943547Smaybee 		dbuf_add_ref(db, child);
953547Smaybee 		if (db->db.db_data)
963547Smaybee 			child->db_blkptr = (blkptr_t *)db->db.db_data + i;
973547Smaybee 		else
983547Smaybee 			child->db_blkptr = NULL;
993547Smaybee 		dprintf_dbuf_bp(child, child->db_blkptr,
1003547Smaybee 		    "changed db_blkptr to new indirect %s", "");
101789Sahrens 
102789Sahrens 		mutex_exit(&child->db_mtx);
103789Sahrens 	}
104789Sahrens 
1053547Smaybee 	bzero(dn->dn_phys->dn_blkptr, sizeof (blkptr_t) * nblkptr);
106789Sahrens 
1071544Seschrock 	dbuf_rele(db, FTAG);
1083547Smaybee 
1093547Smaybee 	rw_exit(&dn->dn_struct_rwlock);
110789Sahrens }
111789Sahrens 
112*6992Smaybee static int
113789Sahrens free_blocks(dnode_t *dn, blkptr_t *bp, int num, dmu_tx_t *tx)
114789Sahrens {
115*6992Smaybee 	dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
116789Sahrens 	uint64_t bytesfreed = 0;
117*6992Smaybee 	int i, blocks_freed = 0;
118789Sahrens 
119*6992Smaybee 	dprintf("ds=%p obj=%llx num=%d\n", ds, dn->dn_object, num);
120789Sahrens 
121789Sahrens 	for (i = 0; i < num; i++, bp++) {
122789Sahrens 		if (BP_IS_HOLE(bp))
123789Sahrens 			continue;
124789Sahrens 
125*6992Smaybee 		bytesfreed += dsl_dataset_block_kill(ds, bp, dn->dn_zio, tx);
1262082Seschrock 		ASSERT3U(bytesfreed, <=, DN_USED_BYTES(dn->dn_phys));
1273547Smaybee 		bzero(bp, sizeof (blkptr_t));
128*6992Smaybee 		blocks_freed += 1;
129789Sahrens 	}
130789Sahrens 	dnode_diduse_space(dn, -bytesfreed);
131*6992Smaybee 	return (blocks_freed);
132789Sahrens }
133789Sahrens 
134873Sek110237 #ifdef ZFS_DEBUG
135789Sahrens static void
136789Sahrens free_verify(dmu_buf_impl_t *db, uint64_t start, uint64_t end, dmu_tx_t *tx)
137789Sahrens {
138789Sahrens 	int off, num;
139789Sahrens 	int i, err, epbs;
140789Sahrens 	uint64_t txg = tx->tx_txg;
141789Sahrens 
142789Sahrens 	epbs = db->db_dnode->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
143789Sahrens 	off = start - (db->db_blkid * 1<<epbs);
144789Sahrens 	num = end - start + 1;
145789Sahrens 
146789Sahrens 	ASSERT3U(off, >=, 0);
147789Sahrens 	ASSERT3U(num, >=, 0);
148789Sahrens 	ASSERT3U(db->db_level, >, 0);
149789Sahrens 	ASSERT3U(db->db.db_size, ==, 1<<db->db_dnode->dn_phys->dn_indblkshift);
150789Sahrens 	ASSERT3U(off+num, <=, db->db.db_size >> SPA_BLKPTRSHIFT);
151789Sahrens 	ASSERT(db->db_blkptr != NULL);
152789Sahrens 
153789Sahrens 	for (i = off; i < off+num; i++) {
154789Sahrens 		uint64_t *buf;
1553547Smaybee 		dmu_buf_impl_t *child;
1563547Smaybee 		dbuf_dirty_record_t *dr;
157789Sahrens 		int j;
158789Sahrens 
159789Sahrens 		ASSERT(db->db_level == 1);
160789Sahrens 
161789Sahrens 		rw_enter(&db->db_dnode->dn_struct_rwlock, RW_READER);
162789Sahrens 		err = dbuf_hold_impl(db->db_dnode, db->db_level-1,
1634312Sgw25295 		    (db->db_blkid << epbs) + i, TRUE, FTAG, &child);
164789Sahrens 		rw_exit(&db->db_dnode->dn_struct_rwlock);
165789Sahrens 		if (err == ENOENT)
166789Sahrens 			continue;
167789Sahrens 		ASSERT(err == 0);
168789Sahrens 		ASSERT(child->db_level == 0);
1693547Smaybee 		dr = child->db_last_dirty;
1703547Smaybee 		while (dr && dr->dr_txg > txg)
1713547Smaybee 			dr = dr->dr_next;
1723547Smaybee 		ASSERT(dr == NULL || dr->dr_txg == txg);
173789Sahrens 
1743547Smaybee 		/* data_old better be zeroed */
1753547Smaybee 		if (dr) {
1763547Smaybee 			buf = dr->dt.dl.dr_data->b_data;
177789Sahrens 			for (j = 0; j < child->db.db_size >> 3; j++) {
178789Sahrens 				if (buf[j] != 0) {
179789Sahrens 					panic("freed data not zero: "
180789Sahrens 					    "child=%p i=%d off=%d num=%d\n",
181789Sahrens 					    child, i, off, num);
182789Sahrens 				}
183789Sahrens 			}
184789Sahrens 		}
185789Sahrens 
186789Sahrens 		/*
187789Sahrens 		 * db_data better be zeroed unless it's dirty in a
188789Sahrens 		 * future txg.
189789Sahrens 		 */
190789Sahrens 		mutex_enter(&child->db_mtx);
191789Sahrens 		buf = child->db.db_data;
192789Sahrens 		if (buf != NULL && child->db_state != DB_FILL &&
1933547Smaybee 		    child->db_last_dirty == NULL) {
194789Sahrens 			for (j = 0; j < child->db.db_size >> 3; j++) {
195789Sahrens 				if (buf[j] != 0) {
196789Sahrens 					panic("freed data not zero: "
197789Sahrens 					    "child=%p i=%d off=%d num=%d\n",
198789Sahrens 					    child, i, off, num);
199789Sahrens 				}
200789Sahrens 			}
201789Sahrens 		}
202789Sahrens 		mutex_exit(&child->db_mtx);
203789Sahrens 
2041544Seschrock 		dbuf_rele(child, FTAG);
205789Sahrens 	}
206873Sek110237 }
207789Sahrens #endif
208789Sahrens 
209*6992Smaybee #define	ALL -1
210*6992Smaybee 
211789Sahrens static int
212789Sahrens free_children(dmu_buf_impl_t *db, uint64_t blkid, uint64_t nblks, int trunc,
213789Sahrens     dmu_tx_t *tx)
214789Sahrens {
215789Sahrens 	dnode_t *dn = db->db_dnode;
216789Sahrens 	blkptr_t *bp;
217789Sahrens 	dmu_buf_impl_t *subdb;
218789Sahrens 	uint64_t start, end, dbstart, dbend, i;
219789Sahrens 	int epbs, shift, err;
220789Sahrens 	int all = TRUE;
221*6992Smaybee 	int blocks_freed = 0;
222789Sahrens 
223*6992Smaybee 	/*
224*6992Smaybee 	 * There is a small possibility that this block will not be cached:
225*6992Smaybee 	 *   1 - if level > 1 and there are no children with level <= 1
226*6992Smaybee 	 *   2 - if we didn't get a dirty hold (because this block had just
227*6992Smaybee 	 *	 finished being written -- and so had no holds), and then this
228*6992Smaybee 	 *	 block got evicted before we got here.
229*6992Smaybee 	 */
230*6992Smaybee 	if (db->db_state != DB_CACHED)
231*6992Smaybee 		(void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
232*6992Smaybee 
233789Sahrens 	arc_release(db->db_buf, db);
234789Sahrens 	bp = (blkptr_t *)db->db.db_data;
235789Sahrens 
236789Sahrens 	epbs = db->db_dnode->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
237789Sahrens 	shift = (db->db_level - 1) * epbs;
238789Sahrens 	dbstart = db->db_blkid << epbs;
239789Sahrens 	start = blkid >> shift;
240789Sahrens 	if (dbstart < start) {
241789Sahrens 		bp += start - dbstart;
242789Sahrens 		all = FALSE;
243789Sahrens 	} else {
244789Sahrens 		start = dbstart;
245789Sahrens 	}
246789Sahrens 	dbend = ((db->db_blkid + 1) << epbs) - 1;
247789Sahrens 	end = (blkid + nblks - 1) >> shift;
248789Sahrens 	if (dbend <= end)
249789Sahrens 		end = dbend;
250789Sahrens 	else if (all)
251789Sahrens 		all = trunc;
252789Sahrens 	ASSERT3U(start, <=, end);
253789Sahrens 
254789Sahrens 	if (db->db_level == 1) {
255873Sek110237 		FREE_VERIFY(db, start, end, tx);
256*6992Smaybee 		blocks_freed = free_blocks(dn, bp, end-start+1, tx);
2573093Sahrens 		arc_buf_freeze(db->db_buf);
258*6992Smaybee 		ASSERT(all || blocks_freed == 0 || db->db_last_dirty);
259*6992Smaybee 		return (all ? ALL : blocks_freed);
260789Sahrens 	}
261789Sahrens 
262789Sahrens 	for (i = start; i <= end; i++, bp++) {
263789Sahrens 		if (BP_IS_HOLE(bp))
264789Sahrens 			continue;
265789Sahrens 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
266789Sahrens 		err = dbuf_hold_impl(dn, db->db_level-1, i, TRUE, FTAG, &subdb);
267789Sahrens 		ASSERT3U(err, ==, 0);
268789Sahrens 		rw_exit(&dn->dn_struct_rwlock);
269789Sahrens 
270*6992Smaybee 		if (free_children(subdb, blkid, nblks, trunc, tx) == ALL) {
271789Sahrens 			ASSERT3P(subdb->db_blkptr, ==, bp);
272*6992Smaybee 			blocks_freed += free_blocks(dn, bp, 1, tx);
2731163Smaybee 		} else {
2741163Smaybee 			all = FALSE;
275789Sahrens 		}
2761544Seschrock 		dbuf_rele(subdb, FTAG);
277789Sahrens 	}
2783093Sahrens 	arc_buf_freeze(db->db_buf);
279789Sahrens #ifdef ZFS_DEBUG
280789Sahrens 	bp -= (end-start)+1;
281789Sahrens 	for (i = start; i <= end; i++, bp++) {
282789Sahrens 		if (i == start && blkid != 0)
283789Sahrens 			continue;
284789Sahrens 		else if (i == end && !trunc)
285789Sahrens 			continue;
286789Sahrens 		ASSERT3U(bp->blk_birth, ==, 0);
287789Sahrens 	}
288789Sahrens #endif
289*6992Smaybee 	ASSERT(all || blocks_freed == 0 || db->db_last_dirty);
290*6992Smaybee 	return (all ? ALL : blocks_freed);
291789Sahrens }
292789Sahrens 
293789Sahrens /*
294789Sahrens  * free_range: Traverse the indicated range of the provided file
295789Sahrens  * and "free" all the blocks contained there.
296789Sahrens  */
297789Sahrens static void
298789Sahrens dnode_sync_free_range(dnode_t *dn, uint64_t blkid, uint64_t nblks, dmu_tx_t *tx)
299789Sahrens {
300789Sahrens 	blkptr_t *bp = dn->dn_phys->dn_blkptr;
301789Sahrens 	dmu_buf_impl_t *db;
302789Sahrens 	int trunc, start, end, shift, i, err;
303789Sahrens 	int dnlevel = dn->dn_phys->dn_nlevels;
304789Sahrens 
305789Sahrens 	if (blkid > dn->dn_phys->dn_maxblkid)
306789Sahrens 		return;
307789Sahrens 
308789Sahrens 	ASSERT(dn->dn_phys->dn_maxblkid < UINT64_MAX);
309789Sahrens 	trunc = blkid + nblks > dn->dn_phys->dn_maxblkid;
310789Sahrens 	if (trunc)
311789Sahrens 		nblks = dn->dn_phys->dn_maxblkid - blkid + 1;
312789Sahrens 
313789Sahrens 	/* There are no indirect blocks in the object */
314789Sahrens 	if (dnlevel == 1) {
315789Sahrens 		if (blkid >= dn->dn_phys->dn_nblkptr) {
316789Sahrens 			/* this range was never made persistent */
317789Sahrens 			return;
318789Sahrens 		}
319789Sahrens 		ASSERT3U(blkid + nblks, <=, dn->dn_phys->dn_nblkptr);
320*6992Smaybee 		(void) free_blocks(dn, bp + blkid, nblks, tx);
321789Sahrens 		if (trunc) {
322789Sahrens 			uint64_t off = (dn->dn_phys->dn_maxblkid + 1) *
323789Sahrens 			    (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT);
324789Sahrens 			dn->dn_phys->dn_maxblkid = (blkid ? blkid - 1 : 0);
325789Sahrens 			ASSERT(off < dn->dn_phys->dn_maxblkid ||
326789Sahrens 			    dn->dn_phys->dn_maxblkid == 0 ||
327*6992Smaybee 			    dnode_next_offset(dn, 0, &off, 1, 1, 0) != 0);
328789Sahrens 		}
329789Sahrens 		return;
330789Sahrens 	}
331789Sahrens 
332789Sahrens 	shift = (dnlevel - 1) * (dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT);
333789Sahrens 	start = blkid >> shift;
334789Sahrens 	ASSERT(start < dn->dn_phys->dn_nblkptr);
335789Sahrens 	end = (blkid + nblks - 1) >> shift;
336789Sahrens 	bp += start;
337789Sahrens 	for (i = start; i <= end; i++, bp++) {
338789Sahrens 		if (BP_IS_HOLE(bp))
339789Sahrens 			continue;
340789Sahrens 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
341789Sahrens 		err = dbuf_hold_impl(dn, dnlevel-1, i, TRUE, FTAG, &db);
342789Sahrens 		ASSERT3U(err, ==, 0);
343789Sahrens 		rw_exit(&dn->dn_struct_rwlock);
344789Sahrens 
345*6992Smaybee 		if (free_children(db, blkid, nblks, trunc, tx) == ALL) {
346789Sahrens 			ASSERT3P(db->db_blkptr, ==, bp);
347*6992Smaybee 			(void) free_blocks(dn, bp, 1, tx);
348789Sahrens 		}
3491544Seschrock 		dbuf_rele(db, FTAG);
350789Sahrens 	}
351789Sahrens 	if (trunc) {
352789Sahrens 		uint64_t off = (dn->dn_phys->dn_maxblkid + 1) *
353789Sahrens 		    (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT);
354789Sahrens 		dn->dn_phys->dn_maxblkid = (blkid ? blkid - 1 : 0);
355789Sahrens 		ASSERT(off < dn->dn_phys->dn_maxblkid ||
356789Sahrens 		    dn->dn_phys->dn_maxblkid == 0 ||
357*6992Smaybee 		    dnode_next_offset(dn, 0, &off, 1, 1, 0) != 0);
358789Sahrens 	}
359789Sahrens }
360789Sahrens 
3611544Seschrock /*
3621544Seschrock  * Try to kick all the dnodes dbufs out of the cache...
3631544Seschrock  */
3644944Smaybee void
3654944Smaybee dnode_evict_dbufs(dnode_t *dn)
3661544Seschrock {
3671596Sahrens 	int progress;
3681596Sahrens 	int pass = 0;
3691596Sahrens 
3701596Sahrens 	do {
3711602Smaybee 		dmu_buf_impl_t *db, marker;
3721596Sahrens 		int evicting = FALSE;
3731544Seschrock 
3741596Sahrens 		progress = FALSE;
3751596Sahrens 		mutex_enter(&dn->dn_dbufs_mtx);
3761602Smaybee 		list_insert_tail(&dn->dn_dbufs, &marker);
3771602Smaybee 		db = list_head(&dn->dn_dbufs);
3781602Smaybee 		for (; db != &marker; db = list_head(&dn->dn_dbufs)) {
3791602Smaybee 			list_remove(&dn->dn_dbufs, db);
3801602Smaybee 			list_insert_tail(&dn->dn_dbufs, db);
3814312Sgw25295 			ASSERT3P(db->db_dnode, ==, dn);
3821596Sahrens 
3831544Seschrock 			mutex_enter(&db->db_mtx);
3841596Sahrens 			if (db->db_state == DB_EVICTING) {
3851596Sahrens 				progress = TRUE;
3861596Sahrens 				evicting = TRUE;
3871596Sahrens 				mutex_exit(&db->db_mtx);
3881596Sahrens 			} else if (refcount_is_zero(&db->db_holds)) {
3891596Sahrens 				progress = TRUE;
3901596Sahrens 				ASSERT(!arc_released(db->db_buf));
3911596Sahrens 				dbuf_clear(db); /* exits db_mtx for us */
3921596Sahrens 			} else {
3931596Sahrens 				mutex_exit(&db->db_mtx);
3941596Sahrens 			}
3951596Sahrens 
3961544Seschrock 		}
3971602Smaybee 		list_remove(&dn->dn_dbufs, &marker);
3981596Sahrens 		/*
3991596Sahrens 		 * NB: we need to drop dn_dbufs_mtx between passes so
4001596Sahrens 		 * that any DB_EVICTING dbufs can make progress.
4011596Sahrens 		 * Ideally, we would have some cv we could wait on, but
4021596Sahrens 		 * since we don't, just wait a bit to give the other
4031596Sahrens 		 * thread a chance to run.
4041596Sahrens 		 */
4051596Sahrens 		mutex_exit(&dn->dn_dbufs_mtx);
4061596Sahrens 		if (evicting)
4071596Sahrens 			delay(1);
4081596Sahrens 		pass++;
4091596Sahrens 		ASSERT(pass < 100); /* sanity check */
4101596Sahrens 	} while (progress);
4111596Sahrens 
4121544Seschrock 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
4131544Seschrock 	if (dn->dn_bonus && refcount_is_zero(&dn->dn_bonus->db_holds)) {
4141544Seschrock 		mutex_enter(&dn->dn_bonus->db_mtx);
4151544Seschrock 		dbuf_evict(dn->dn_bonus);
4161544Seschrock 		dn->dn_bonus = NULL;
4171544Seschrock 	}
4181544Seschrock 	rw_exit(&dn->dn_struct_rwlock);
4191544Seschrock }
4201544Seschrock 
4213547Smaybee static void
4223547Smaybee dnode_undirty_dbufs(list_t *list)
4233547Smaybee {
4243547Smaybee 	dbuf_dirty_record_t *dr;
4253547Smaybee 
4263547Smaybee 	while (dr = list_head(list)) {
4273547Smaybee 		dmu_buf_impl_t *db = dr->dr_dbuf;
4283547Smaybee 		uint64_t txg = dr->dr_txg;
4293547Smaybee 
4303547Smaybee 		mutex_enter(&db->db_mtx);
4313547Smaybee 		/* XXX - use dbuf_undirty()? */
4323547Smaybee 		list_remove(list, dr);
4333547Smaybee 		ASSERT(db->db_last_dirty == dr);
4343547Smaybee 		db->db_last_dirty = NULL;
4353547Smaybee 		db->db_dirtycnt -= 1;
4363547Smaybee 		if (db->db_level == 0) {
4373547Smaybee 			ASSERT(db->db_blkid == DB_BONUS_BLKID ||
4383547Smaybee 			    dr->dt.dl.dr_data == db->db_buf);
4393547Smaybee 			dbuf_unoverride(dr);
4403547Smaybee 			mutex_exit(&db->db_mtx);
4413547Smaybee 		} else {
4423547Smaybee 			mutex_exit(&db->db_mtx);
4433547Smaybee 			dnode_undirty_dbufs(&dr->dt.di.dr_children);
4443547Smaybee 		}
4453547Smaybee 		kmem_free(dr, sizeof (dbuf_dirty_record_t));
4463547Smaybee 		dbuf_rele(db, (void *)(uintptr_t)txg);
4473547Smaybee 	}
4483547Smaybee }
4493547Smaybee 
4503547Smaybee static void
451789Sahrens dnode_sync_free(dnode_t *dn, dmu_tx_t *tx)
452789Sahrens {
453789Sahrens 	int txgoff = tx->tx_txg & TXG_MASK;
454789Sahrens 
455789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
456789Sahrens 
457*6992Smaybee 	/*
458*6992Smaybee 	 * Our contents should have been freed in dnode_sync() by the
459*6992Smaybee 	 * free range record inserted by the caller of dnode_free().
460*6992Smaybee 	 */
461*6992Smaybee 	ASSERT3U(DN_USED_BYTES(dn->dn_phys), ==, 0);
462*6992Smaybee 	ASSERT(BP_IS_HOLE(dn->dn_phys->dn_blkptr));
463*6992Smaybee 
4643547Smaybee 	dnode_undirty_dbufs(&dn->dn_dirty_records[txgoff]);
4654944Smaybee 	dnode_evict_dbufs(dn);
4661544Seschrock 	ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL);
4671544Seschrock 
4681544Seschrock 	/*
4691544Seschrock 	 * XXX - It would be nice to assert this, but we may still
4701544Seschrock 	 * have residual holds from async evictions from the arc...
4711544Seschrock 	 *
4723444Sek110237 	 * zfs_obj_to_path() also depends on this being
4733444Sek110237 	 * commented out.
4743444Sek110237 	 *
4751544Seschrock 	 * ASSERT3U(refcount_count(&dn->dn_holds), ==, 1);
4761544Seschrock 	 */
477789Sahrens 
478789Sahrens 	/* Undirty next bits */
479789Sahrens 	dn->dn_next_nlevels[txgoff] = 0;
480789Sahrens 	dn->dn_next_indblkshift[txgoff] = 0;
4811596Sahrens 	dn->dn_next_blksz[txgoff] = 0;
482789Sahrens 
483789Sahrens 	/* ASSERT(blkptrs are zero); */
484789Sahrens 	ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE);
485789Sahrens 	ASSERT(dn->dn_type != DMU_OT_NONE);
486789Sahrens 
487789Sahrens 	ASSERT(dn->dn_free_txg > 0);
488789Sahrens 	if (dn->dn_allocated_txg != dn->dn_free_txg)
489789Sahrens 		dbuf_will_dirty(dn->dn_dbuf, tx);
490789Sahrens 	bzero(dn->dn_phys, sizeof (dnode_phys_t));
491789Sahrens 
492789Sahrens 	mutex_enter(&dn->dn_mtx);
493789Sahrens 	dn->dn_type = DMU_OT_NONE;
494789Sahrens 	dn->dn_maxblkid = 0;
495789Sahrens 	dn->dn_allocated_txg = 0;
4964480Sgw25295 	dn->dn_free_txg = 0;
497789Sahrens 	mutex_exit(&dn->dn_mtx);
498789Sahrens 
4991544Seschrock 	ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
500789Sahrens 
501789Sahrens 	dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg);
502789Sahrens 	/*
503789Sahrens 	 * Now that we've released our hold, the dnode may
504789Sahrens 	 * be evicted, so we musn't access it.
505789Sahrens 	 */
506789Sahrens }
507789Sahrens 
508789Sahrens /*
5093547Smaybee  * Write out the dnode's dirty buffers.
510789Sahrens  *
511789Sahrens  * NOTE: The dnode is kept in memory by being dirty.  Once the
512789Sahrens  * dirty bit is cleared, it may be evicted.  Beware of this!
513789Sahrens  */
5143547Smaybee void
5153547Smaybee dnode_sync(dnode_t *dn, dmu_tx_t *tx)
516789Sahrens {
517789Sahrens 	free_range_t *rp;
5183547Smaybee 	dnode_phys_t *dnp = dn->dn_phys;
519789Sahrens 	int txgoff = tx->tx_txg & TXG_MASK;
5203547Smaybee 	list_t *list = &dn->dn_dirty_records[txgoff];
521789Sahrens 
522789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
523789Sahrens 	ASSERT(dnp->dn_type != DMU_OT_NONE || dn->dn_allocated_txg);
524873Sek110237 	DNODE_VERIFY(dn);
5251596Sahrens 
5263547Smaybee 	ASSERT(dn->dn_dbuf == NULL || arc_released(dn->dn_dbuf->db_buf));
527789Sahrens 
528789Sahrens 	mutex_enter(&dn->dn_mtx);
529789Sahrens 	if (dn->dn_allocated_txg == tx->tx_txg) {
530789Sahrens 		/* The dnode is newly allocated or reallocated */
531789Sahrens 		if (dnp->dn_type == DMU_OT_NONE) {
532789Sahrens 			/* this is a first alloc, not a realloc */
533789Sahrens 			/* XXX shouldn't the phys already be zeroed? */
534789Sahrens 			bzero(dnp, DNODE_CORE_SIZE);
535789Sahrens 			dnp->dn_nlevels = 1;
536789Sahrens 		}
537789Sahrens 
538789Sahrens 		if (dn->dn_nblkptr > dnp->dn_nblkptr) {
539789Sahrens 			/* zero the new blkptrs we are gaining */
540789Sahrens 			bzero(dnp->dn_blkptr + dnp->dn_nblkptr,
541789Sahrens 			    sizeof (blkptr_t) *
542789Sahrens 			    (dn->dn_nblkptr - dnp->dn_nblkptr));
543789Sahrens 		}
544789Sahrens 		dnp->dn_type = dn->dn_type;
545789Sahrens 		dnp->dn_bonustype = dn->dn_bonustype;
546789Sahrens 		dnp->dn_bonuslen = dn->dn_bonuslen;
547789Sahrens 		dnp->dn_nblkptr = dn->dn_nblkptr;
548789Sahrens 	}
549789Sahrens 
5503547Smaybee 	ASSERT(dnp->dn_nlevels > 1 ||
5511599Sahrens 	    BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
5521599Sahrens 	    BP_GET_LSIZE(&dnp->dn_blkptr[0]) ==
5531599Sahrens 	    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
5541599Sahrens 
5551596Sahrens 	if (dn->dn_next_blksz[txgoff]) {
5561596Sahrens 		ASSERT(P2PHASE(dn->dn_next_blksz[txgoff],
557789Sahrens 		    SPA_MINBLOCKSIZE) == 0);
5581599Sahrens 		ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
559*6992Smaybee 		    dn->dn_maxblkid == 0 || list_head(list) != NULL ||
5601600Sahrens 		    dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT ==
5611600Sahrens 		    dnp->dn_datablkszsec);
562789Sahrens 		dnp->dn_datablkszsec =
5631596Sahrens 		    dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT;
5641596Sahrens 		dn->dn_next_blksz[txgoff] = 0;
565789Sahrens 	}
566789Sahrens 
5674944Smaybee 	if (dn->dn_next_bonuslen[txgoff]) {
5684944Smaybee 		if (dn->dn_next_bonuslen[txgoff] == DN_ZERO_BONUSLEN)
5694944Smaybee 			dnp->dn_bonuslen = 0;
5704944Smaybee 		else
5714944Smaybee 			dnp->dn_bonuslen = dn->dn_next_bonuslen[txgoff];
5724944Smaybee 		ASSERT(dnp->dn_bonuslen <= DN_MAX_BONUSLEN);
5734944Smaybee 		dn->dn_next_bonuslen[txgoff] = 0;
5744944Smaybee 	}
5754944Smaybee 
576789Sahrens 	if (dn->dn_next_indblkshift[txgoff]) {
577789Sahrens 		ASSERT(dnp->dn_nlevels == 1);
578789Sahrens 		dnp->dn_indblkshift = dn->dn_next_indblkshift[txgoff];
579789Sahrens 		dn->dn_next_indblkshift[txgoff] = 0;
580789Sahrens 	}
581789Sahrens 
582789Sahrens 	/*
583789Sahrens 	 * Just take the live (open-context) values for checksum and compress.
584789Sahrens 	 * Strictly speaking it's a future leak, but nothing bad happens if we
585789Sahrens 	 * start using the new checksum or compress algorithm a little early.
586789Sahrens 	 */
587789Sahrens 	dnp->dn_checksum = dn->dn_checksum;
588789Sahrens 	dnp->dn_compress = dn->dn_compress;
589789Sahrens 
590789Sahrens 	mutex_exit(&dn->dn_mtx);
591789Sahrens 
592789Sahrens 	/* process all the "freed" ranges in the file */
593*6992Smaybee 	while (rp = avl_last(&dn->dn_ranges[txgoff])) {
594*6992Smaybee 		dnode_sync_free_range(dn, rp->fr_blkid, rp->fr_nblks, tx);
595*6992Smaybee 		/* grab the mutex so we don't race with dnode_block_freed() */
596*6992Smaybee 		mutex_enter(&dn->dn_mtx);
597*6992Smaybee 		avl_remove(&dn->dn_ranges[txgoff], rp);
598*6992Smaybee 		mutex_exit(&dn->dn_mtx);
599*6992Smaybee 		kmem_free(rp, sizeof (free_range_t));
600789Sahrens 	}
6014944Smaybee 
602789Sahrens 	if (dn->dn_free_txg > 0 && dn->dn_free_txg <= tx->tx_txg) {
6033547Smaybee 		dnode_sync_free(dn, tx);
6043547Smaybee 		return;
605789Sahrens 	}
606789Sahrens 
607789Sahrens 	if (dn->dn_next_nlevels[txgoff]) {
6083547Smaybee 		dnode_increase_indirection(dn, tx);
609789Sahrens 		dn->dn_next_nlevels[txgoff] = 0;
610789Sahrens 	}
611789Sahrens 
6123547Smaybee 	dbuf_sync_list(list, tx);
613789Sahrens 
6143547Smaybee 	if (dn->dn_object != DMU_META_DNODE_OBJECT) {
6153547Smaybee 		ASSERT3P(list_head(list), ==, NULL);
6163547Smaybee 		dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg);
6173547Smaybee 	}
6181599Sahrens 
6193547Smaybee 	/*
6203547Smaybee 	 * Although we have dropped our reference to the dnode, it
6213547Smaybee 	 * can't be evicted until its written, and we haven't yet
6223547Smaybee 	 * initiated the IO for the dnode's dbuf.
6233547Smaybee 	 */
624789Sahrens }
625