xref: /onnv-gate/usr/src/uts/common/fs/zfs/dnode_sync.c (revision 2082:76b439ec3ac1)
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 /*
221199Seschrock  * Copyright 2006 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 #include <sys/zio.h>
37789Sahrens 
38789Sahrens static void
39789Sahrens dnode_increase_indirection(dnode_t *dn, dmu_tx_t *tx)
40789Sahrens {
41789Sahrens 	dmu_buf_impl_t *db;
42789Sahrens 	int i;
43789Sahrens 	uint64_t txg = tx->tx_txg;
44789Sahrens 
45789Sahrens 	ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE);
46789Sahrens 	ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
47789Sahrens 	/* this dnode can't be paged out because it's dirty */
48789Sahrens 
49789Sahrens 	db = dbuf_hold_level(dn, dn->dn_phys->dn_nlevels, 0, FTAG);
501544Seschrock 	ASSERT(db != NULL);
51789Sahrens 	for (i = 0; i < dn->dn_phys->dn_nblkptr; i++)
52789Sahrens 		if (!BP_IS_HOLE(&dn->dn_phys->dn_blkptr[i]))
53789Sahrens 			break;
54789Sahrens 	if (i != dn->dn_phys->dn_nblkptr) {
55789Sahrens 		ASSERT(list_link_active(&db->db_dirty_node[txg&TXG_MASK]));
56789Sahrens 
571544Seschrock 		(void) dbuf_read(db, NULL,
581544Seschrock 		    DB_RF_HAVESTRUCT | DB_RF_MUST_SUCCEED);
59789Sahrens 		arc_release(db->db_buf, db);
60789Sahrens 		/* copy dnode's block pointers to new indirect block */
61789Sahrens 		ASSERT3U(sizeof (blkptr_t) * dn->dn_phys->dn_nblkptr, <=,
62789Sahrens 		    db->db.db_size);
63789Sahrens 		bcopy(dn->dn_phys->dn_blkptr, db->db.db_data,
64789Sahrens 		    sizeof (blkptr_t) * dn->dn_phys->dn_nblkptr);
65789Sahrens 	}
66789Sahrens 
67789Sahrens 	dn->dn_phys->dn_nlevels += 1;
68789Sahrens 	dprintf("os=%p obj=%llu, increase to %d\n",
69789Sahrens 		dn->dn_objset, dn->dn_object,
70789Sahrens 		dn->dn_phys->dn_nlevels);
71789Sahrens 
72789Sahrens 	/* set dbuf's parent pointers to new indirect buf */
73789Sahrens 	for (i = 0; i < dn->dn_phys->dn_nblkptr; i++) {
74789Sahrens 		dmu_buf_impl_t *child =
75789Sahrens 		    dbuf_find(dn, dn->dn_phys->dn_nlevels-2, i);
76789Sahrens 		if (child == NULL)
77789Sahrens 			continue;
78789Sahrens 		if (child->db_dnode == NULL) {
79789Sahrens 			mutex_exit(&child->db_mtx);
80789Sahrens 			continue;
81789Sahrens 		}
82789Sahrens 
83789Sahrens 		if (child->db_parent == NULL ||
84789Sahrens 		    child->db_parent == dn->dn_dbuf) {
85789Sahrens 			dprintf_dbuf_bp(child, child->db_blkptr,
86789Sahrens 			    "changing db_blkptr to new indirect %s", "");
87789Sahrens 			child->db_parent = db;
88789Sahrens 			dbuf_add_ref(db, child);
89789Sahrens 			if (db->db.db_data) {
90789Sahrens 				child->db_blkptr =
91789Sahrens 				    (blkptr_t *)db->db.db_data + i;
92789Sahrens 			} else {
93789Sahrens 				child->db_blkptr = NULL;
94789Sahrens 			}
95789Sahrens 			dprintf_dbuf_bp(child, child->db_blkptr,
96789Sahrens 			    "changed db_blkptr to new indirect %s", "");
97789Sahrens 		}
98789Sahrens 		ASSERT3P(child->db_parent, ==, db);
99789Sahrens 
100789Sahrens 		mutex_exit(&child->db_mtx);
101789Sahrens 	}
102789Sahrens 
103789Sahrens 	bzero(dn->dn_phys->dn_blkptr,
104789Sahrens 		sizeof (blkptr_t) * dn->dn_phys->dn_nblkptr);
105789Sahrens 
1061544Seschrock 	dbuf_rele(db, FTAG);
107789Sahrens }
108789Sahrens 
109789Sahrens static void
110789Sahrens free_blocks(dnode_t *dn, blkptr_t *bp, int num, dmu_tx_t *tx)
111789Sahrens {
112789Sahrens 	objset_impl_t *os = dn->dn_objset;
113789Sahrens 	uint64_t bytesfreed = 0;
114789Sahrens 	int i;
115789Sahrens 
116789Sahrens 	dprintf("os=%p obj=%llx num=%d\n", os, dn->dn_object, num);
117789Sahrens 
118789Sahrens 	for (i = 0; i < num; i++, bp++) {
119789Sahrens 		if (BP_IS_HOLE(bp))
120789Sahrens 			continue;
121789Sahrens 
122*2082Seschrock 		bytesfreed += bp_get_dasize(os->os_spa, bp);
123*2082Seschrock 		ASSERT3U(bytesfreed, <=, DN_USED_BYTES(dn->dn_phys));
124789Sahrens 		dsl_dataset_block_kill(os->os_dsl_dataset, bp, tx);
125789Sahrens 	}
126789Sahrens 	dnode_diduse_space(dn, -bytesfreed);
127789Sahrens }
128789Sahrens 
129873Sek110237 #ifdef ZFS_DEBUG
130789Sahrens static void
131789Sahrens free_verify(dmu_buf_impl_t *db, uint64_t start, uint64_t end, dmu_tx_t *tx)
132789Sahrens {
133789Sahrens 	int off, num;
134789Sahrens 	int i, err, epbs;
135789Sahrens 	uint64_t txg = tx->tx_txg;
136789Sahrens 
137789Sahrens 	epbs = db->db_dnode->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
138789Sahrens 	off = start - (db->db_blkid * 1<<epbs);
139789Sahrens 	num = end - start + 1;
140789Sahrens 
141789Sahrens 	ASSERT3U(off, >=, 0);
142789Sahrens 	ASSERT3U(num, >=, 0);
143789Sahrens 	ASSERT3U(db->db_level, >, 0);
144789Sahrens 	ASSERT3U(db->db.db_size, ==, 1<<db->db_dnode->dn_phys->dn_indblkshift);
145789Sahrens 	ASSERT3U(off+num, <=, db->db.db_size >> SPA_BLKPTRSHIFT);
146789Sahrens 	ASSERT(db->db_blkptr != NULL);
147789Sahrens 
148789Sahrens 	for (i = off; i < off+num; i++) {
149789Sahrens 		uint64_t *buf;
150789Sahrens 		int j;
151789Sahrens 		dmu_buf_impl_t *child;
152789Sahrens 
153789Sahrens 		ASSERT(db->db_level == 1);
154789Sahrens 
155789Sahrens 		rw_enter(&db->db_dnode->dn_struct_rwlock, RW_READER);
156789Sahrens 		err = dbuf_hold_impl(db->db_dnode, db->db_level-1,
157789Sahrens 			(db->db_blkid << epbs) + i, TRUE, FTAG, &child);
158789Sahrens 		rw_exit(&db->db_dnode->dn_struct_rwlock);
159789Sahrens 		if (err == ENOENT)
160789Sahrens 			continue;
161789Sahrens 		ASSERT(err == 0);
162789Sahrens 		ASSERT(child->db_level == 0);
163789Sahrens 		ASSERT(!list_link_active(&child->db_dirty_node[txg&TXG_MASK]));
164789Sahrens 
165789Sahrens 		/* db_data_old better be zeroed */
166789Sahrens 		if (child->db_d.db_data_old[txg & TXG_MASK]) {
1671544Seschrock 			buf = ((arc_buf_t *)child->db_d.db_data_old
1681544Seschrock 			    [txg & TXG_MASK])->b_data;
169789Sahrens 			for (j = 0; j < child->db.db_size >> 3; j++) {
170789Sahrens 				if (buf[j] != 0) {
171789Sahrens 					panic("freed data not zero: "
172789Sahrens 					    "child=%p i=%d off=%d num=%d\n",
173789Sahrens 					    child, i, off, num);
174789Sahrens 				}
175789Sahrens 			}
176789Sahrens 		}
177789Sahrens 
178789Sahrens 		/*
179789Sahrens 		 * db_data better be zeroed unless it's dirty in a
180789Sahrens 		 * future txg.
181789Sahrens 		 */
182789Sahrens 		mutex_enter(&child->db_mtx);
183789Sahrens 		buf = child->db.db_data;
184789Sahrens 		if (buf != NULL && child->db_state != DB_FILL &&
185789Sahrens 		    !list_link_active(&child->db_dirty_node
186789Sahrens 			[(txg+1) & TXG_MASK]) &&
187789Sahrens 		    !list_link_active(&child->db_dirty_node
188789Sahrens 			[(txg+2) & TXG_MASK])) {
189789Sahrens 			for (j = 0; j < child->db.db_size >> 3; j++) {
190789Sahrens 				if (buf[j] != 0) {
191789Sahrens 					panic("freed data not zero: "
192789Sahrens 					    "child=%p i=%d off=%d num=%d\n",
193789Sahrens 					    child, i, off, num);
194789Sahrens 				}
195789Sahrens 			}
196789Sahrens 		}
197789Sahrens 		mutex_exit(&child->db_mtx);
198789Sahrens 
1991544Seschrock 		dbuf_rele(child, FTAG);
200789Sahrens 	}
201873Sek110237 }
202789Sahrens #endif
203789Sahrens 
204789Sahrens static int
205789Sahrens free_children(dmu_buf_impl_t *db, uint64_t blkid, uint64_t nblks, int trunc,
206789Sahrens     dmu_tx_t *tx)
207789Sahrens {
208789Sahrens 	dnode_t *dn = db->db_dnode;
209789Sahrens 	blkptr_t *bp;
210789Sahrens 	dmu_buf_impl_t *subdb;
211789Sahrens 	uint64_t start, end, dbstart, dbend, i;
212789Sahrens 	int epbs, shift, err;
2131163Smaybee 	int txgoff = tx->tx_txg & TXG_MASK;
214789Sahrens 	int all = TRUE;
215789Sahrens 
2161544Seschrock 	(void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
217789Sahrens 	arc_release(db->db_buf, db);
218789Sahrens 	bp = (blkptr_t *)db->db.db_data;
219789Sahrens 
220789Sahrens 	epbs = db->db_dnode->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
221789Sahrens 	shift = (db->db_level - 1) * epbs;
222789Sahrens 	dbstart = db->db_blkid << epbs;
223789Sahrens 	start = blkid >> shift;
224789Sahrens 	if (dbstart < start) {
225789Sahrens 		bp += start - dbstart;
226789Sahrens 		all = FALSE;
227789Sahrens 	} else {
228789Sahrens 		start = dbstart;
229789Sahrens 	}
230789Sahrens 	dbend = ((db->db_blkid + 1) << epbs) - 1;
231789Sahrens 	end = (blkid + nblks - 1) >> shift;
232789Sahrens 	if (dbend <= end)
233789Sahrens 		end = dbend;
234789Sahrens 	else if (all)
235789Sahrens 		all = trunc;
236789Sahrens 	ASSERT3U(start, <=, end);
237789Sahrens 
238789Sahrens 	if (db->db_level == 1) {
239873Sek110237 		FREE_VERIFY(db, start, end, tx);
240789Sahrens 		free_blocks(dn, bp, end-start+1, tx);
2411163Smaybee 		ASSERT(all || list_link_active(&db->db_dirty_node[txgoff]));
242789Sahrens 		return (all);
243789Sahrens 	}
244789Sahrens 
245789Sahrens 	for (i = start; i <= end; i++, bp++) {
246789Sahrens 		if (BP_IS_HOLE(bp))
247789Sahrens 			continue;
248789Sahrens 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
249789Sahrens 		err = dbuf_hold_impl(dn, db->db_level-1, i, TRUE, FTAG, &subdb);
250789Sahrens 		ASSERT3U(err, ==, 0);
251789Sahrens 		rw_exit(&dn->dn_struct_rwlock);
252789Sahrens 
253789Sahrens 		if (free_children(subdb, blkid, nblks, trunc, tx)) {
254789Sahrens 			ASSERT3P(subdb->db_blkptr, ==, bp);
255789Sahrens 			free_blocks(dn, bp, 1, tx);
2561163Smaybee 		} else {
2571163Smaybee 			all = FALSE;
258789Sahrens 		}
2591544Seschrock 		dbuf_rele(subdb, FTAG);
260789Sahrens 	}
261789Sahrens #ifdef ZFS_DEBUG
262789Sahrens 	bp -= (end-start)+1;
263789Sahrens 	for (i = start; i <= end; i++, bp++) {
264789Sahrens 		if (i == start && blkid != 0)
265789Sahrens 			continue;
266789Sahrens 		else if (i == end && !trunc)
267789Sahrens 			continue;
268789Sahrens 		ASSERT3U(bp->blk_birth, ==, 0);
269789Sahrens 	}
270789Sahrens #endif
2711163Smaybee 	ASSERT(all || list_link_active(&db->db_dirty_node[txgoff]));
272789Sahrens 	return (all);
273789Sahrens }
274789Sahrens 
275789Sahrens /*
276789Sahrens  * free_range: Traverse the indicated range of the provided file
277789Sahrens  * and "free" all the blocks contained there.
278789Sahrens  */
279789Sahrens static void
280789Sahrens dnode_sync_free_range(dnode_t *dn, uint64_t blkid, uint64_t nblks, dmu_tx_t *tx)
281789Sahrens {
282789Sahrens 	blkptr_t *bp = dn->dn_phys->dn_blkptr;
283789Sahrens 	dmu_buf_impl_t *db;
284789Sahrens 	int trunc, start, end, shift, i, err;
285789Sahrens 	int dnlevel = dn->dn_phys->dn_nlevels;
286789Sahrens 
287789Sahrens 	if (blkid > dn->dn_phys->dn_maxblkid)
288789Sahrens 		return;
289789Sahrens 
290789Sahrens 	ASSERT(dn->dn_phys->dn_maxblkid < UINT64_MAX);
291789Sahrens 	trunc = blkid + nblks > dn->dn_phys->dn_maxblkid;
292789Sahrens 	if (trunc)
293789Sahrens 		nblks = dn->dn_phys->dn_maxblkid - blkid + 1;
294789Sahrens 
295789Sahrens 	/* There are no indirect blocks in the object */
296789Sahrens 	if (dnlevel == 1) {
297789Sahrens 		if (blkid >= dn->dn_phys->dn_nblkptr) {
298789Sahrens 			/* this range was never made persistent */
299789Sahrens 			return;
300789Sahrens 		}
301789Sahrens 		ASSERT3U(blkid + nblks, <=, dn->dn_phys->dn_nblkptr);
302789Sahrens 		free_blocks(dn, bp + blkid, nblks, tx);
303789Sahrens 		if (trunc) {
304789Sahrens 			uint64_t off = (dn->dn_phys->dn_maxblkid + 1) *
305789Sahrens 			    (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT);
306789Sahrens 			dn->dn_phys->dn_maxblkid = (blkid ? blkid - 1 : 0);
307789Sahrens 			ASSERT(off < dn->dn_phys->dn_maxblkid ||
308789Sahrens 			    dn->dn_phys->dn_maxblkid == 0 ||
309789Sahrens 			    dnode_next_offset(dn, FALSE, &off, 1, 1) == ESRCH);
310789Sahrens 		}
311789Sahrens 		return;
312789Sahrens 	}
313789Sahrens 
314789Sahrens 	shift = (dnlevel - 1) * (dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT);
315789Sahrens 	start = blkid >> shift;
316789Sahrens 	ASSERT(start < dn->dn_phys->dn_nblkptr);
317789Sahrens 	end = (blkid + nblks - 1) >> shift;
318789Sahrens 	bp += start;
319789Sahrens 	for (i = start; i <= end; i++, bp++) {
320789Sahrens 		if (BP_IS_HOLE(bp))
321789Sahrens 			continue;
322789Sahrens 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
323789Sahrens 		err = dbuf_hold_impl(dn, dnlevel-1, i, TRUE, FTAG, &db);
324789Sahrens 		ASSERT3U(err, ==, 0);
325789Sahrens 		rw_exit(&dn->dn_struct_rwlock);
326789Sahrens 
327789Sahrens 		if (free_children(db, blkid, nblks, trunc, tx)) {
328789Sahrens 			ASSERT3P(db->db_blkptr, ==, bp);
329789Sahrens 			free_blocks(dn, bp, 1, tx);
330789Sahrens 		}
3311544Seschrock 		dbuf_rele(db, FTAG);
332789Sahrens 	}
333789Sahrens 	if (trunc) {
334789Sahrens 		uint64_t off = (dn->dn_phys->dn_maxblkid + 1) *
335789Sahrens 		    (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT);
336789Sahrens 		dn->dn_phys->dn_maxblkid = (blkid ? blkid - 1 : 0);
337789Sahrens 		ASSERT(off < dn->dn_phys->dn_maxblkid ||
338789Sahrens 		    dn->dn_phys->dn_maxblkid == 0 ||
339789Sahrens 		    dnode_next_offset(dn, FALSE, &off, 1, 1) == ESRCH);
340789Sahrens 	}
341789Sahrens }
342789Sahrens 
3431544Seschrock /*
3441544Seschrock  * Try to kick all the dnodes dbufs out of the cache...
3451544Seschrock  */
3461646Sperrin int
3471646Sperrin dnode_evict_dbufs(dnode_t *dn, int try)
3481544Seschrock {
3491596Sahrens 	int progress;
3501596Sahrens 	int pass = 0;
3511596Sahrens 
3521596Sahrens 	do {
3531602Smaybee 		dmu_buf_impl_t *db, marker;
3541596Sahrens 		int evicting = FALSE;
3551544Seschrock 
3561596Sahrens 		progress = FALSE;
3571596Sahrens 		mutex_enter(&dn->dn_dbufs_mtx);
3581602Smaybee 		list_insert_tail(&dn->dn_dbufs, &marker);
3591602Smaybee 		db = list_head(&dn->dn_dbufs);
3601602Smaybee 		for (; db != &marker; db = list_head(&dn->dn_dbufs)) {
3611602Smaybee 			list_remove(&dn->dn_dbufs, db);
3621602Smaybee 			list_insert_tail(&dn->dn_dbufs, db);
3631596Sahrens 
3641544Seschrock 			mutex_enter(&db->db_mtx);
3651596Sahrens 			if (db->db_state == DB_EVICTING) {
3661596Sahrens 				progress = TRUE;
3671596Sahrens 				evicting = TRUE;
3681596Sahrens 				mutex_exit(&db->db_mtx);
3691596Sahrens 			} else if (refcount_is_zero(&db->db_holds)) {
3701596Sahrens 				progress = TRUE;
3711596Sahrens 				ASSERT(!arc_released(db->db_buf));
3721596Sahrens 				dbuf_clear(db); /* exits db_mtx for us */
3731596Sahrens 			} else {
3741596Sahrens 				mutex_exit(&db->db_mtx);
3751596Sahrens 			}
3761596Sahrens 
3771544Seschrock 		}
3781602Smaybee 		list_remove(&dn->dn_dbufs, &marker);
3791596Sahrens 		/*
3801596Sahrens 		 * NB: we need to drop dn_dbufs_mtx between passes so
3811596Sahrens 		 * that any DB_EVICTING dbufs can make progress.
3821596Sahrens 		 * Ideally, we would have some cv we could wait on, but
3831596Sahrens 		 * since we don't, just wait a bit to give the other
3841596Sahrens 		 * thread a chance to run.
3851596Sahrens 		 */
3861596Sahrens 		mutex_exit(&dn->dn_dbufs_mtx);
3871596Sahrens 		if (evicting)
3881596Sahrens 			delay(1);
3891596Sahrens 		pass++;
3901596Sahrens 		ASSERT(pass < 100); /* sanity check */
3911596Sahrens 	} while (progress);
3921596Sahrens 
3931596Sahrens 	/*
3941646Sperrin 	 * This function works fine even if it can't evict everything.
3951646Sperrin 	 * If were only asked to try to evict everything then
3961646Sperrin 	 * return an error if we can't. Otherwise panic as the caller
3971646Sperrin 	 * expects total eviction.
3981596Sahrens 	 */
3991596Sahrens 	if (list_head(&dn->dn_dbufs) != NULL) {
4001646Sperrin 		if (try) {
4011646Sperrin 			return (1);
4021646Sperrin 		} else {
4031646Sperrin 			panic("dangling dbufs (dn=%p, dbuf=%p)\n",
4041646Sperrin 			    dn, list_head(&dn->dn_dbufs));
4051646Sperrin 		}
4061544Seschrock 	}
4071596Sahrens 
4081544Seschrock 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
4091544Seschrock 	if (dn->dn_bonus && refcount_is_zero(&dn->dn_bonus->db_holds)) {
4101544Seschrock 		mutex_enter(&dn->dn_bonus->db_mtx);
4111544Seschrock 		dbuf_evict(dn->dn_bonus);
4121544Seschrock 		dn->dn_bonus = NULL;
4131544Seschrock 	}
4141544Seschrock 	rw_exit(&dn->dn_struct_rwlock);
4151646Sperrin 	return (0);
4161544Seschrock }
4171544Seschrock 
418789Sahrens static int
419789Sahrens dnode_sync_free(dnode_t *dn, dmu_tx_t *tx)
420789Sahrens {
421789Sahrens 	dmu_buf_impl_t *db;
422789Sahrens 	int txgoff = tx->tx_txg & TXG_MASK;
423789Sahrens 
424789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
425789Sahrens 
426789Sahrens 	/* Undirty all buffers */
427789Sahrens 	while (db = list_head(&dn->dn_dirty_dbufs[txgoff])) {
428789Sahrens 		mutex_enter(&db->db_mtx);
429789Sahrens 		/* XXX - use dbuf_undirty()? */
430789Sahrens 		list_remove(&dn->dn_dirty_dbufs[txgoff], db);
431789Sahrens 		if (db->db_level == 0) {
4321544Seschrock 			ASSERT(db->db_blkid == DB_BONUS_BLKID ||
4331544Seschrock 			    db->db_d.db_data_old[txgoff] == db->db_buf);
434789Sahrens 			if (db->db_d.db_overridden_by[txgoff])
435789Sahrens 				dbuf_unoverride(db, tx->tx_txg);
436789Sahrens 			db->db_d.db_data_old[txgoff] = NULL;
437789Sahrens 		}
438789Sahrens 		db->db_dirtycnt -= 1;
439789Sahrens 		mutex_exit(&db->db_mtx);
4401544Seschrock 		dbuf_rele(db, (void *)(uintptr_t)tx->tx_txg);
441789Sahrens 	}
442789Sahrens 
4431646Sperrin 	(void) dnode_evict_dbufs(dn, 0);
4441544Seschrock 	ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL);
4451544Seschrock 
4461544Seschrock 	/*
4471544Seschrock 	 * XXX - It would be nice to assert this, but we may still
4481544Seschrock 	 * have residual holds from async evictions from the arc...
4491544Seschrock 	 *
4501544Seschrock 	 * ASSERT3U(refcount_count(&dn->dn_holds), ==, 1);
4511544Seschrock 	 */
452789Sahrens 
453789Sahrens 	/* Undirty next bits */
454789Sahrens 	dn->dn_next_nlevels[txgoff] = 0;
455789Sahrens 	dn->dn_next_indblkshift[txgoff] = 0;
4561596Sahrens 	dn->dn_next_blksz[txgoff] = 0;
457789Sahrens 
458789Sahrens 	/* free up all the blocks in the file. */
459789Sahrens 	dnode_sync_free_range(dn, 0, dn->dn_phys->dn_maxblkid+1, tx);
460*2082Seschrock 	ASSERT3U(DN_USED_BYTES(dn->dn_phys), ==, 0);
461789Sahrens 
462789Sahrens 	/* ASSERT(blkptrs are zero); */
463789Sahrens 	ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE);
464789Sahrens 	ASSERT(dn->dn_type != DMU_OT_NONE);
465789Sahrens 
466789Sahrens 	ASSERT(dn->dn_free_txg > 0);
467789Sahrens 	if (dn->dn_allocated_txg != dn->dn_free_txg)
468789Sahrens 		dbuf_will_dirty(dn->dn_dbuf, tx);
469789Sahrens 	bzero(dn->dn_phys, sizeof (dnode_phys_t));
470789Sahrens 
471789Sahrens 	mutex_enter(&dn->dn_mtx);
472789Sahrens 	dn->dn_type = DMU_OT_NONE;
473789Sahrens 	dn->dn_maxblkid = 0;
474789Sahrens 	dn->dn_allocated_txg = 0;
475789Sahrens 	mutex_exit(&dn->dn_mtx);
476789Sahrens 
4771544Seschrock 	ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
478789Sahrens 
479789Sahrens 	dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg);
480789Sahrens 	/*
481789Sahrens 	 * Now that we've released our hold, the dnode may
482789Sahrens 	 * be evicted, so we musn't access it.
483789Sahrens 	 */
484789Sahrens 	return (1);
485789Sahrens }
486789Sahrens 
487789Sahrens /*
488789Sahrens  * Write out the dnode's dirty buffers at the specified level.
489789Sahrens  * This may create more dirty buffers at the next level up.
490789Sahrens  *
491789Sahrens  * NOTE: The dnode is kept in memory by being dirty.  Once the
492789Sahrens  * dirty bit is cleared, it may be evicted.  Beware of this!
493789Sahrens  */
494789Sahrens int
495789Sahrens dnode_sync(dnode_t *dn, int level, zio_t *zio, dmu_tx_t *tx)
496789Sahrens {
497789Sahrens 	free_range_t *rp;
498789Sahrens 	int txgoff = tx->tx_txg & TXG_MASK;
499789Sahrens 	dnode_phys_t *dnp = dn->dn_phys;
500789Sahrens 
501789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
502789Sahrens 	ASSERT(dnp->dn_type != DMU_OT_NONE || dn->dn_allocated_txg);
503873Sek110237 	DNODE_VERIFY(dn);
5041596Sahrens 
505789Sahrens 	/*
506789Sahrens 	 * Make sure the dbuf for the dn_phys is released before we modify it.
507789Sahrens 	 */
508789Sahrens 	if (dn->dn_dbuf)
509789Sahrens 		arc_release(dn->dn_dbuf->db_buf, dn->dn_dbuf);
510789Sahrens 
511789Sahrens 	mutex_enter(&dn->dn_mtx);
512789Sahrens 	if (dn->dn_allocated_txg == tx->tx_txg) {
513789Sahrens 		/* The dnode is newly allocated or reallocated */
514789Sahrens 		if (dnp->dn_type == DMU_OT_NONE) {
515789Sahrens 			/* this is a first alloc, not a realloc */
516789Sahrens 			/* XXX shouldn't the phys already be zeroed? */
517789Sahrens 			bzero(dnp, DNODE_CORE_SIZE);
518789Sahrens 			dnp->dn_nlevels = 1;
519789Sahrens 		}
520789Sahrens 
521789Sahrens 		if (dn->dn_nblkptr > dnp->dn_nblkptr) {
522789Sahrens 			/* zero the new blkptrs we are gaining */
523789Sahrens 			bzero(dnp->dn_blkptr + dnp->dn_nblkptr,
524789Sahrens 			    sizeof (blkptr_t) *
525789Sahrens 			    (dn->dn_nblkptr - dnp->dn_nblkptr));
526789Sahrens 		}
527789Sahrens 		dnp->dn_type = dn->dn_type;
528789Sahrens 		dnp->dn_bonustype = dn->dn_bonustype;
529789Sahrens 		dnp->dn_bonuslen = dn->dn_bonuslen;
530789Sahrens 		dnp->dn_nblkptr = dn->dn_nblkptr;
531789Sahrens 	}
532789Sahrens 
5331599Sahrens 	ASSERT(level != 0 || dnp->dn_nlevels > 1 ||
5341599Sahrens 	    BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
5351599Sahrens 	    BP_GET_LSIZE(&dnp->dn_blkptr[0]) ==
5361599Sahrens 	    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
5371599Sahrens 
5381596Sahrens 	if (dn->dn_next_blksz[txgoff]) {
5391596Sahrens 		ASSERT(P2PHASE(dn->dn_next_blksz[txgoff],
540789Sahrens 		    SPA_MINBLOCKSIZE) == 0);
5411599Sahrens 		ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
5421600Sahrens 		    list_head(&dn->dn_dirty_dbufs[txgoff]) != NULL ||
5431600Sahrens 		    dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT ==
5441600Sahrens 		    dnp->dn_datablkszsec);
545789Sahrens 		dnp->dn_datablkszsec =
5461596Sahrens 		    dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT;
5471596Sahrens 		dn->dn_next_blksz[txgoff] = 0;
548789Sahrens 	}
549789Sahrens 
550789Sahrens 	if (dn->dn_next_indblkshift[txgoff]) {
551789Sahrens 		ASSERT(dnp->dn_nlevels == 1);
552789Sahrens 		dnp->dn_indblkshift = dn->dn_next_indblkshift[txgoff];
553789Sahrens 		dn->dn_next_indblkshift[txgoff] = 0;
554789Sahrens 	}
555789Sahrens 
556789Sahrens 	/*
557789Sahrens 	 * Just take the live (open-context) values for checksum and compress.
558789Sahrens 	 * Strictly speaking it's a future leak, but nothing bad happens if we
559789Sahrens 	 * start using the new checksum or compress algorithm a little early.
560789Sahrens 	 */
561789Sahrens 	dnp->dn_checksum = dn->dn_checksum;
562789Sahrens 	dnp->dn_compress = dn->dn_compress;
563789Sahrens 
564789Sahrens 	mutex_exit(&dn->dn_mtx);
565789Sahrens 
566789Sahrens 	/* process all the "freed" ranges in the file */
567789Sahrens 	if (dn->dn_free_txg == 0 || dn->dn_free_txg > tx->tx_txg) {
5681163Smaybee 		for (rp = avl_last(&dn->dn_ranges[txgoff]); rp != NULL;
5691163Smaybee 		    rp = AVL_PREV(&dn->dn_ranges[txgoff], rp))
570789Sahrens 			dnode_sync_free_range(dn,
571789Sahrens 			    rp->fr_blkid, rp->fr_nblks, tx);
572789Sahrens 	}
573789Sahrens 	mutex_enter(&dn->dn_mtx);
574789Sahrens 	for (rp = avl_first(&dn->dn_ranges[txgoff]); rp; ) {
575789Sahrens 		free_range_t *last = rp;
576789Sahrens 		rp = AVL_NEXT(&dn->dn_ranges[txgoff], rp);
577789Sahrens 		avl_remove(&dn->dn_ranges[txgoff], last);
578789Sahrens 		kmem_free(last, sizeof (free_range_t));
579789Sahrens 	}
580789Sahrens 	mutex_exit(&dn->dn_mtx);
581789Sahrens 
582789Sahrens 	if (dn->dn_free_txg > 0 && dn->dn_free_txg <= tx->tx_txg) {
583789Sahrens 		ASSERT3U(level, ==, 0);
584789Sahrens 		return (dnode_sync_free(dn, tx));
585789Sahrens 	}
586789Sahrens 
587789Sahrens 	if (dn->dn_next_nlevels[txgoff]) {
588789Sahrens 		int new_lvl = dn->dn_next_nlevels[txgoff];
589789Sahrens 
590789Sahrens 		rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
591789Sahrens 		while (new_lvl > dnp->dn_nlevels)
592789Sahrens 			dnode_increase_indirection(dn, tx);
593789Sahrens 		rw_exit(&dn->dn_struct_rwlock);
594789Sahrens 		dn->dn_next_nlevels[txgoff] = 0;
595789Sahrens 	}
596789Sahrens 
597789Sahrens 	if (level == dnp->dn_nlevels) {
598789Sahrens 		uint64_t off = (dn->dn_phys->dn_maxblkid + 1) *
599789Sahrens 		    (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT);
600789Sahrens 
601789Sahrens 		/* we've already synced out all data and indirect blocks */
602789Sahrens 		/* there are no more dirty dbufs under this dnode */
603789Sahrens 		ASSERT3P(list_head(&dn->dn_dirty_dbufs[txgoff]), ==, NULL);
604789Sahrens 		ASSERT(dn->dn_free_txg == 0 || dn->dn_free_txg >= tx->tx_txg);
605789Sahrens 
606789Sahrens 		/* XXX this is expensive. remove once 6343073 is closed. */
607789Sahrens 		/* NB: the "off < maxblkid" is to catch overflow */
608789Sahrens 		/*
609789Sahrens 		 * NB: if blocksize is changing, we could get confused,
610789Sahrens 		 * so only bother if there are multiple blocks and thus
611789Sahrens 		 * it can't be changing.
612789Sahrens 		 */
6131163Smaybee 		if (!(off < dn->dn_phys->dn_maxblkid ||
614789Sahrens 		    dn->dn_phys->dn_maxblkid == 0 ||
6151163Smaybee 		    dnode_next_offset(dn, FALSE, &off, 1, 1) == ESRCH))
6161199Seschrock 			panic("data after EOF: off=%llu\n", (u_longlong_t)off);
617789Sahrens 
6181599Sahrens 		ASSERT(dnp->dn_nlevels > 1 ||
6191599Sahrens 		    BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
6201599Sahrens 		    BP_GET_LSIZE(&dnp->dn_blkptr[0]) ==
6211599Sahrens 		    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
6221599Sahrens 
6231544Seschrock 		if (dn->dn_object != DMU_META_DNODE_OBJECT) {
624789Sahrens 			dbuf_will_dirty(dn->dn_dbuf, tx);
625789Sahrens 			dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg);
626789Sahrens 		}
627789Sahrens 
628789Sahrens 		/*
629789Sahrens 		 * Now that we've dropped the reference, the dnode may
630789Sahrens 		 * be evicted, so we musn't access it.
631789Sahrens 		 */
632789Sahrens 		return (1);
633789Sahrens 	} else {
634789Sahrens 		dmu_buf_impl_t *db, *db_next;
635789Sahrens 		list_t *list = &dn->dn_dirty_dbufs[txgoff];
636789Sahrens 		/*
637789Sahrens 		 * Iterate over the list, removing and sync'ing dbufs
638789Sahrens 		 * which are on the level we want, and leaving others.
639789Sahrens 		 */
640789Sahrens 		for (db = list_head(list); db; db = db_next) {
641789Sahrens 			db_next = list_next(list, db);
642789Sahrens 			if (db->db_level == level) {
643789Sahrens 				list_remove(list, db);
644789Sahrens 				dbuf_sync(db, zio, tx);
645789Sahrens 			}
646789Sahrens 		}
647789Sahrens 		return (0);
648789Sahrens 	}
649789Sahrens }
650