xref: /illumos-gate/usr/src/uts/common/fs/zfs/dnode_sync.c (revision 1934e92fc930c49429ad71a8ca97340f33227e78)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
2255434c77Sek  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26fa9e4066Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
27fa9e4066Sahrens 
28fa9e4066Sahrens #include <sys/zfs_context.h>
29fa9e4066Sahrens #include <sys/dbuf.h>
30fa9e4066Sahrens #include <sys/dnode.h>
31fa9e4066Sahrens #include <sys/dmu.h>
32fa9e4066Sahrens #include <sys/dmu_tx.h>
33fa9e4066Sahrens #include <sys/dmu_objset.h>
34fa9e4066Sahrens #include <sys/dsl_dataset.h>
35fa9e4066Sahrens #include <sys/spa.h>
36fa9e4066Sahrens 
37fa9e4066Sahrens static void
38fa9e4066Sahrens dnode_increase_indirection(dnode_t *dn, dmu_tx_t *tx)
39fa9e4066Sahrens {
40fa9e4066Sahrens 	dmu_buf_impl_t *db;
41c717a561Smaybee 	int txgoff = tx->tx_txg & TXG_MASK;
42c717a561Smaybee 	int nblkptr = dn->dn_phys->dn_nblkptr;
43c717a561Smaybee 	int old_toplvl = dn->dn_phys->dn_nlevels - 1;
44c717a561Smaybee 	int new_level = dn->dn_next_nlevels[txgoff];
45fa9e4066Sahrens 	int i;
46fa9e4066Sahrens 
47c717a561Smaybee 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
48c717a561Smaybee 
49c717a561Smaybee 	/* this dnode can't be paged out because it's dirty */
50fa9e4066Sahrens 	ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE);
51fa9e4066Sahrens 	ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
52c717a561Smaybee 	ASSERT(new_level > 1 && dn->dn_phys->dn_nlevels > 0);
53fa9e4066Sahrens 
54fa9e4066Sahrens 	db = dbuf_hold_level(dn, dn->dn_phys->dn_nlevels, 0, FTAG);
55ea8dc4b6Seschrock 	ASSERT(db != NULL);
56c717a561Smaybee 
57c717a561Smaybee 	dn->dn_phys->dn_nlevels = new_level;
58f82bfe17Sgw 	dprintf("os=%p obj=%llu, increase to %d\n", dn->dn_objset,
59f82bfe17Sgw 	    dn->dn_object, dn->dn_phys->dn_nlevels);
60c717a561Smaybee 
61c717a561Smaybee 	/* check for existing blkptrs in the dnode */
62c717a561Smaybee 	for (i = 0; i < nblkptr; i++)
63fa9e4066Sahrens 		if (!BP_IS_HOLE(&dn->dn_phys->dn_blkptr[i]))
64fa9e4066Sahrens 			break;
65c717a561Smaybee 	if (i != nblkptr) {
66c717a561Smaybee 		/* transfer dnode's block pointers to new indirect block */
67c717a561Smaybee 		(void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED|DB_RF_HAVESTRUCT);
68c717a561Smaybee 		ASSERT(db->db.db_data);
69c717a561Smaybee 		ASSERT(arc_released(db->db_buf));
70c717a561Smaybee 		ASSERT3U(sizeof (blkptr_t) * nblkptr, <=, db->db.db_size);
71fa9e4066Sahrens 		bcopy(dn->dn_phys->dn_blkptr, db->db.db_data,
72c717a561Smaybee 		    sizeof (blkptr_t) * nblkptr);
736b4acc8bSahrens 		arc_buf_freeze(db->db_buf);
74fa9e4066Sahrens 	}
75fa9e4066Sahrens 
76fa9e4066Sahrens 	/* set dbuf's parent pointers to new indirect buf */
77c717a561Smaybee 	for (i = 0; i < nblkptr; i++) {
78c717a561Smaybee 		dmu_buf_impl_t *child = dbuf_find(dn, old_toplvl, i);
79c717a561Smaybee 
80fa9e4066Sahrens 		if (child == NULL)
81fa9e4066Sahrens 			continue;
82c717a561Smaybee 		ASSERT3P(child->db_dnode, ==, dn);
83c717a561Smaybee 		if (child->db_parent && child->db_parent != dn->dn_dbuf) {
84c717a561Smaybee 			ASSERT(child->db_parent->db_level == db->db_level);
85c717a561Smaybee 			ASSERT(child->db_blkptr !=
86c717a561Smaybee 			    &dn->dn_phys->dn_blkptr[child->db_blkid]);
87fa9e4066Sahrens 			mutex_exit(&child->db_mtx);
88fa9e4066Sahrens 			continue;
89fa9e4066Sahrens 		}
90c717a561Smaybee 		ASSERT(child->db_parent == NULL ||
91c717a561Smaybee 		    child->db_parent == dn->dn_dbuf);
92c717a561Smaybee 
93c717a561Smaybee 		child->db_parent = db;
94c717a561Smaybee 		dbuf_add_ref(db, child);
95c717a561Smaybee 		if (db->db.db_data)
96c717a561Smaybee 			child->db_blkptr = (blkptr_t *)db->db.db_data + i;
97c717a561Smaybee 		else
98c717a561Smaybee 			child->db_blkptr = NULL;
99c717a561Smaybee 		dprintf_dbuf_bp(child, child->db_blkptr,
100c717a561Smaybee 		    "changed db_blkptr to new indirect %s", "");
101fa9e4066Sahrens 
102fa9e4066Sahrens 		mutex_exit(&child->db_mtx);
103fa9e4066Sahrens 	}
104fa9e4066Sahrens 
105c717a561Smaybee 	bzero(dn->dn_phys->dn_blkptr, sizeof (blkptr_t) * nblkptr);
106fa9e4066Sahrens 
107ea8dc4b6Seschrock 	dbuf_rele(db, FTAG);
108c717a561Smaybee 
109c717a561Smaybee 	rw_exit(&dn->dn_struct_rwlock);
110fa9e4066Sahrens }
111fa9e4066Sahrens 
112fa9e4066Sahrens static void
113fa9e4066Sahrens free_blocks(dnode_t *dn, blkptr_t *bp, int num, dmu_tx_t *tx)
114fa9e4066Sahrens {
115fa9e4066Sahrens 	objset_impl_t *os = dn->dn_objset;
116fa9e4066Sahrens 	uint64_t bytesfreed = 0;
117fa9e4066Sahrens 	int i;
118fa9e4066Sahrens 
119fa9e4066Sahrens 	dprintf("os=%p obj=%llx num=%d\n", os, dn->dn_object, num);
120fa9e4066Sahrens 
121fa9e4066Sahrens 	for (i = 0; i < num; i++, bp++) {
122fa9e4066Sahrens 		if (BP_IS_HOLE(bp))
123fa9e4066Sahrens 			continue;
124fa9e4066Sahrens 
12599653d4eSeschrock 		bytesfreed += bp_get_dasize(os->os_spa, bp);
12699653d4eSeschrock 		ASSERT3U(bytesfreed, <=, DN_USED_BYTES(dn->dn_phys));
127c717a561Smaybee 		dsl_dataset_block_kill(os->os_dsl_dataset, bp, dn->dn_zio, tx);
128c717a561Smaybee 		bzero(bp, sizeof (blkptr_t));
129fa9e4066Sahrens 	}
130fa9e4066Sahrens 	dnode_diduse_space(dn, -bytesfreed);
131fa9e4066Sahrens }
132fa9e4066Sahrens 
1339c9dc39aSek #ifdef ZFS_DEBUG
134fa9e4066Sahrens static void
135fa9e4066Sahrens free_verify(dmu_buf_impl_t *db, uint64_t start, uint64_t end, dmu_tx_t *tx)
136fa9e4066Sahrens {
137fa9e4066Sahrens 	int off, num;
138fa9e4066Sahrens 	int i, err, epbs;
139fa9e4066Sahrens 	uint64_t txg = tx->tx_txg;
140fa9e4066Sahrens 
141fa9e4066Sahrens 	epbs = db->db_dnode->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
142fa9e4066Sahrens 	off = start - (db->db_blkid * 1<<epbs);
143fa9e4066Sahrens 	num = end - start + 1;
144fa9e4066Sahrens 
145fa9e4066Sahrens 	ASSERT3U(off, >=, 0);
146fa9e4066Sahrens 	ASSERT3U(num, >=, 0);
147fa9e4066Sahrens 	ASSERT3U(db->db_level, >, 0);
148fa9e4066Sahrens 	ASSERT3U(db->db.db_size, ==, 1<<db->db_dnode->dn_phys->dn_indblkshift);
149fa9e4066Sahrens 	ASSERT3U(off+num, <=, db->db.db_size >> SPA_BLKPTRSHIFT);
150fa9e4066Sahrens 	ASSERT(db->db_blkptr != NULL);
151fa9e4066Sahrens 
152fa9e4066Sahrens 	for (i = off; i < off+num; i++) {
153fa9e4066Sahrens 		uint64_t *buf;
154fa9e4066Sahrens 		dmu_buf_impl_t *child;
155c717a561Smaybee 		dbuf_dirty_record_t *dr;
156c717a561Smaybee 		int j;
157fa9e4066Sahrens 
158fa9e4066Sahrens 		ASSERT(db->db_level == 1);
159fa9e4066Sahrens 
160fa9e4066Sahrens 		rw_enter(&db->db_dnode->dn_struct_rwlock, RW_READER);
161fa9e4066Sahrens 		err = dbuf_hold_impl(db->db_dnode, db->db_level-1,
162f82bfe17Sgw 		    (db->db_blkid << epbs) + i, TRUE, FTAG, &child);
163fa9e4066Sahrens 		rw_exit(&db->db_dnode->dn_struct_rwlock);
164fa9e4066Sahrens 		if (err == ENOENT)
165fa9e4066Sahrens 			continue;
166fa9e4066Sahrens 		ASSERT(err == 0);
167fa9e4066Sahrens 		ASSERT(child->db_level == 0);
168c717a561Smaybee 		dr = child->db_last_dirty;
169c717a561Smaybee 		while (dr && dr->dr_txg > txg)
170c717a561Smaybee 			dr = dr->dr_next;
171c717a561Smaybee 		ASSERT(dr == NULL || dr->dr_txg == txg);
172c717a561Smaybee 
173c717a561Smaybee 		/* data_old better be zeroed */
174c717a561Smaybee 		if (dr) {
175c717a561Smaybee 			buf = dr->dt.dl.dr_data->b_data;
176fa9e4066Sahrens 			for (j = 0; j < child->db.db_size >> 3; j++) {
177fa9e4066Sahrens 				if (buf[j] != 0) {
178fa9e4066Sahrens 					panic("freed data not zero: "
179fa9e4066Sahrens 					    "child=%p i=%d off=%d num=%d\n",
180fa9e4066Sahrens 					    child, i, off, num);
181fa9e4066Sahrens 				}
182fa9e4066Sahrens 			}
183fa9e4066Sahrens 		}
184fa9e4066Sahrens 
185fa9e4066Sahrens 		/*
186fa9e4066Sahrens 		 * db_data better be zeroed unless it's dirty in a
187fa9e4066Sahrens 		 * future txg.
188fa9e4066Sahrens 		 */
189fa9e4066Sahrens 		mutex_enter(&child->db_mtx);
190fa9e4066Sahrens 		buf = child->db.db_data;
191fa9e4066Sahrens 		if (buf != NULL && child->db_state != DB_FILL &&
192c717a561Smaybee 		    child->db_last_dirty == NULL) {
193fa9e4066Sahrens 			for (j = 0; j < child->db.db_size >> 3; j++) {
194fa9e4066Sahrens 				if (buf[j] != 0) {
195fa9e4066Sahrens 					panic("freed data not zero: "
196fa9e4066Sahrens 					    "child=%p i=%d off=%d num=%d\n",
197fa9e4066Sahrens 					    child, i, off, num);
198fa9e4066Sahrens 				}
199fa9e4066Sahrens 			}
200fa9e4066Sahrens 		}
201fa9e4066Sahrens 		mutex_exit(&child->db_mtx);
202fa9e4066Sahrens 
203ea8dc4b6Seschrock 		dbuf_rele(child, FTAG);
204fa9e4066Sahrens 	}
205fa9e4066Sahrens }
2069c9dc39aSek #endif
207fa9e4066Sahrens 
208fa9e4066Sahrens static int
209fa9e4066Sahrens free_children(dmu_buf_impl_t *db, uint64_t blkid, uint64_t nblks, int trunc,
210fa9e4066Sahrens     dmu_tx_t *tx)
211fa9e4066Sahrens {
212fa9e4066Sahrens 	dnode_t *dn = db->db_dnode;
213fa9e4066Sahrens 	blkptr_t *bp;
214fa9e4066Sahrens 	dmu_buf_impl_t *subdb;
215fa9e4066Sahrens 	uint64_t start, end, dbstart, dbend, i;
216fa9e4066Sahrens 	int epbs, shift, err;
217fa9e4066Sahrens 	int all = TRUE;
218fa9e4066Sahrens 
219ea8dc4b6Seschrock 	(void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
220fa9e4066Sahrens 	arc_release(db->db_buf, db);
221fa9e4066Sahrens 	bp = (blkptr_t *)db->db.db_data;
222fa9e4066Sahrens 
223fa9e4066Sahrens 	epbs = db->db_dnode->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
224fa9e4066Sahrens 	shift = (db->db_level - 1) * epbs;
225fa9e4066Sahrens 	dbstart = db->db_blkid << epbs;
226fa9e4066Sahrens 	start = blkid >> shift;
227fa9e4066Sahrens 	if (dbstart < start) {
228fa9e4066Sahrens 		bp += start - dbstart;
229fa9e4066Sahrens 		all = FALSE;
230fa9e4066Sahrens 	} else {
231fa9e4066Sahrens 		start = dbstart;
232fa9e4066Sahrens 	}
233fa9e4066Sahrens 	dbend = ((db->db_blkid + 1) << epbs) - 1;
234fa9e4066Sahrens 	end = (blkid + nblks - 1) >> shift;
235fa9e4066Sahrens 	if (dbend <= end)
236fa9e4066Sahrens 		end = dbend;
237fa9e4066Sahrens 	else if (all)
238fa9e4066Sahrens 		all = trunc;
239fa9e4066Sahrens 	ASSERT3U(start, <=, end);
240fa9e4066Sahrens 
241fa9e4066Sahrens 	if (db->db_level == 1) {
2429c9dc39aSek 		FREE_VERIFY(db, start, end, tx);
243fa9e4066Sahrens 		free_blocks(dn, bp, end-start+1, tx);
2446b4acc8bSahrens 		arc_buf_freeze(db->db_buf);
245c717a561Smaybee 		ASSERT(all || db->db_last_dirty);
246fa9e4066Sahrens 		return (all);
247fa9e4066Sahrens 	}
248fa9e4066Sahrens 
249fa9e4066Sahrens 	for (i = start; i <= end; i++, bp++) {
250fa9e4066Sahrens 		if (BP_IS_HOLE(bp))
251fa9e4066Sahrens 			continue;
252fa9e4066Sahrens 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
253fa9e4066Sahrens 		err = dbuf_hold_impl(dn, db->db_level-1, i, TRUE, FTAG, &subdb);
254fa9e4066Sahrens 		ASSERT3U(err, ==, 0);
255fa9e4066Sahrens 		rw_exit(&dn->dn_struct_rwlock);
256fa9e4066Sahrens 
257fa9e4066Sahrens 		if (free_children(subdb, blkid, nblks, trunc, tx)) {
258fa9e4066Sahrens 			ASSERT3P(subdb->db_blkptr, ==, bp);
259fa9e4066Sahrens 			free_blocks(dn, bp, 1, tx);
26023b11526Smaybee 		} else {
26123b11526Smaybee 			all = FALSE;
262fa9e4066Sahrens 		}
263ea8dc4b6Seschrock 		dbuf_rele(subdb, FTAG);
264fa9e4066Sahrens 	}
2656b4acc8bSahrens 	arc_buf_freeze(db->db_buf);
266fa9e4066Sahrens #ifdef ZFS_DEBUG
267fa9e4066Sahrens 	bp -= (end-start)+1;
268fa9e4066Sahrens 	for (i = start; i <= end; i++, bp++) {
269fa9e4066Sahrens 		if (i == start && blkid != 0)
270fa9e4066Sahrens 			continue;
271fa9e4066Sahrens 		else if (i == end && !trunc)
272fa9e4066Sahrens 			continue;
273fa9e4066Sahrens 		ASSERT3U(bp->blk_birth, ==, 0);
274fa9e4066Sahrens 	}
275fa9e4066Sahrens #endif
276c717a561Smaybee 	ASSERT(all || db->db_last_dirty);
277fa9e4066Sahrens 	return (all);
278fa9e4066Sahrens }
279fa9e4066Sahrens 
280fa9e4066Sahrens /*
281fa9e4066Sahrens  * free_range: Traverse the indicated range of the provided file
282fa9e4066Sahrens  * and "free" all the blocks contained there.
283fa9e4066Sahrens  */
284fa9e4066Sahrens static void
285fa9e4066Sahrens dnode_sync_free_range(dnode_t *dn, uint64_t blkid, uint64_t nblks, dmu_tx_t *tx)
286fa9e4066Sahrens {
287fa9e4066Sahrens 	blkptr_t *bp = dn->dn_phys->dn_blkptr;
288fa9e4066Sahrens 	dmu_buf_impl_t *db;
289fa9e4066Sahrens 	int trunc, start, end, shift, i, err;
290fa9e4066Sahrens 	int dnlevel = dn->dn_phys->dn_nlevels;
291fa9e4066Sahrens 
292fa9e4066Sahrens 	if (blkid > dn->dn_phys->dn_maxblkid)
293fa9e4066Sahrens 		return;
294fa9e4066Sahrens 
295fa9e4066Sahrens 	ASSERT(dn->dn_phys->dn_maxblkid < UINT64_MAX);
296fa9e4066Sahrens 	trunc = blkid + nblks > dn->dn_phys->dn_maxblkid;
297fa9e4066Sahrens 	if (trunc)
298fa9e4066Sahrens 		nblks = dn->dn_phys->dn_maxblkid - blkid + 1;
299fa9e4066Sahrens 
300fa9e4066Sahrens 	/* There are no indirect blocks in the object */
301fa9e4066Sahrens 	if (dnlevel == 1) {
302fa9e4066Sahrens 		if (blkid >= dn->dn_phys->dn_nblkptr) {
303fa9e4066Sahrens 			/* this range was never made persistent */
304fa9e4066Sahrens 			return;
305fa9e4066Sahrens 		}
306fa9e4066Sahrens 		ASSERT3U(blkid + nblks, <=, dn->dn_phys->dn_nblkptr);
307fa9e4066Sahrens 		free_blocks(dn, bp + blkid, nblks, tx);
308fa9e4066Sahrens 		if (trunc) {
309fa9e4066Sahrens 			uint64_t off = (dn->dn_phys->dn_maxblkid + 1) *
310fa9e4066Sahrens 			    (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT);
311fa9e4066Sahrens 			dn->dn_phys->dn_maxblkid = (blkid ? blkid - 1 : 0);
312fa9e4066Sahrens 			ASSERT(off < dn->dn_phys->dn_maxblkid ||
313fa9e4066Sahrens 			    dn->dn_phys->dn_maxblkid == 0 ||
3146754306eSahrens 			    dnode_next_offset(dn, FALSE, &off,
3156754306eSahrens 			    1, 1, 0) != 0);
316fa9e4066Sahrens 		}
317fa9e4066Sahrens 		return;
318fa9e4066Sahrens 	}
319fa9e4066Sahrens 
320fa9e4066Sahrens 	shift = (dnlevel - 1) * (dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT);
321fa9e4066Sahrens 	start = blkid >> shift;
322fa9e4066Sahrens 	ASSERT(start < dn->dn_phys->dn_nblkptr);
323fa9e4066Sahrens 	end = (blkid + nblks - 1) >> shift;
324fa9e4066Sahrens 	bp += start;
325fa9e4066Sahrens 	for (i = start; i <= end; i++, bp++) {
326fa9e4066Sahrens 		if (BP_IS_HOLE(bp))
327fa9e4066Sahrens 			continue;
328fa9e4066Sahrens 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
329fa9e4066Sahrens 		err = dbuf_hold_impl(dn, dnlevel-1, i, TRUE, FTAG, &db);
330fa9e4066Sahrens 		ASSERT3U(err, ==, 0);
331fa9e4066Sahrens 		rw_exit(&dn->dn_struct_rwlock);
332fa9e4066Sahrens 
333fa9e4066Sahrens 		if (free_children(db, blkid, nblks, trunc, tx)) {
334fa9e4066Sahrens 			ASSERT3P(db->db_blkptr, ==, bp);
335fa9e4066Sahrens 			free_blocks(dn, bp, 1, tx);
336fa9e4066Sahrens 		}
337ea8dc4b6Seschrock 		dbuf_rele(db, FTAG);
338fa9e4066Sahrens 	}
339fa9e4066Sahrens 	if (trunc) {
340fa9e4066Sahrens 		uint64_t off = (dn->dn_phys->dn_maxblkid + 1) *
341fa9e4066Sahrens 		    (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT);
342fa9e4066Sahrens 		dn->dn_phys->dn_maxblkid = (blkid ? blkid - 1 : 0);
343fa9e4066Sahrens 		ASSERT(off < dn->dn_phys->dn_maxblkid ||
344fa9e4066Sahrens 		    dn->dn_phys->dn_maxblkid == 0 ||
3456754306eSahrens 		    dnode_next_offset(dn, FALSE, &off, 1, 1, 0) != 0);
346fa9e4066Sahrens 	}
347fa9e4066Sahrens }
348fa9e4066Sahrens 
349ea8dc4b6Seschrock /*
350ea8dc4b6Seschrock  * Try to kick all the dnodes dbufs out of the cache...
351ea8dc4b6Seschrock  */
352*1934e92fSmaybee void
353*1934e92fSmaybee dnode_evict_dbufs(dnode_t *dn)
354ea8dc4b6Seschrock {
355c543ec06Sahrens 	int progress;
356c543ec06Sahrens 	int pass = 0;
357c543ec06Sahrens 
358c543ec06Sahrens 	do {
359d90a49d6Smaybee 		dmu_buf_impl_t *db, marker;
360c543ec06Sahrens 		int evicting = FALSE;
361c543ec06Sahrens 
362c543ec06Sahrens 		progress = FALSE;
363c543ec06Sahrens 		mutex_enter(&dn->dn_dbufs_mtx);
364d90a49d6Smaybee 		list_insert_tail(&dn->dn_dbufs, &marker);
365d90a49d6Smaybee 		db = list_head(&dn->dn_dbufs);
366d90a49d6Smaybee 		for (; db != &marker; db = list_head(&dn->dn_dbufs)) {
367d90a49d6Smaybee 			list_remove(&dn->dn_dbufs, db);
368d90a49d6Smaybee 			list_insert_tail(&dn->dn_dbufs, db);
369f82bfe17Sgw 			ASSERT3P(db->db_dnode, ==, dn);
370ea8dc4b6Seschrock 
371ea8dc4b6Seschrock 			mutex_enter(&db->db_mtx);
372c543ec06Sahrens 			if (db->db_state == DB_EVICTING) {
373c543ec06Sahrens 				progress = TRUE;
374c543ec06Sahrens 				evicting = TRUE;
375c543ec06Sahrens 				mutex_exit(&db->db_mtx);
376c543ec06Sahrens 			} else if (refcount_is_zero(&db->db_holds)) {
377c543ec06Sahrens 				progress = TRUE;
378c543ec06Sahrens 				ASSERT(!arc_released(db->db_buf));
379c543ec06Sahrens 				dbuf_clear(db); /* exits db_mtx for us */
380c543ec06Sahrens 			} else {
381c543ec06Sahrens 				mutex_exit(&db->db_mtx);
382c543ec06Sahrens 			}
383c543ec06Sahrens 
384ea8dc4b6Seschrock 		}
385d90a49d6Smaybee 		list_remove(&dn->dn_dbufs, &marker);
386c543ec06Sahrens 		/*
387c543ec06Sahrens 		 * NB: we need to drop dn_dbufs_mtx between passes so
388c543ec06Sahrens 		 * that any DB_EVICTING dbufs can make progress.
389c543ec06Sahrens 		 * Ideally, we would have some cv we could wait on, but
390c543ec06Sahrens 		 * since we don't, just wait a bit to give the other
391c543ec06Sahrens 		 * thread a chance to run.
392c543ec06Sahrens 		 */
393c543ec06Sahrens 		mutex_exit(&dn->dn_dbufs_mtx);
394c543ec06Sahrens 		if (evicting)
395c543ec06Sahrens 			delay(1);
396c543ec06Sahrens 		pass++;
397c543ec06Sahrens 		ASSERT(pass < 100); /* sanity check */
398c543ec06Sahrens 	} while (progress);
399c543ec06Sahrens 
400ea8dc4b6Seschrock 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
401ea8dc4b6Seschrock 	if (dn->dn_bonus && refcount_is_zero(&dn->dn_bonus->db_holds)) {
402ea8dc4b6Seschrock 		mutex_enter(&dn->dn_bonus->db_mtx);
403ea8dc4b6Seschrock 		dbuf_evict(dn->dn_bonus);
404ea8dc4b6Seschrock 		dn->dn_bonus = NULL;
405ea8dc4b6Seschrock 	}
406ea8dc4b6Seschrock 	rw_exit(&dn->dn_struct_rwlock);
407ea8dc4b6Seschrock }
408ea8dc4b6Seschrock 
409c717a561Smaybee static void
410c717a561Smaybee dnode_undirty_dbufs(list_t *list)
411fa9e4066Sahrens {
412c717a561Smaybee 	dbuf_dirty_record_t *dr;
413fa9e4066Sahrens 
414c717a561Smaybee 	while (dr = list_head(list)) {
415c717a561Smaybee 		dmu_buf_impl_t *db = dr->dr_dbuf;
416c717a561Smaybee 		uint64_t txg = dr->dr_txg;
417fa9e4066Sahrens 
418fa9e4066Sahrens 		mutex_enter(&db->db_mtx);
419fa9e4066Sahrens 		/* XXX - use dbuf_undirty()? */
420c717a561Smaybee 		list_remove(list, dr);
421c717a561Smaybee 		ASSERT(db->db_last_dirty == dr);
422c717a561Smaybee 		db->db_last_dirty = NULL;
423c717a561Smaybee 		db->db_dirtycnt -= 1;
424fa9e4066Sahrens 		if (db->db_level == 0) {
425ea8dc4b6Seschrock 			ASSERT(db->db_blkid == DB_BONUS_BLKID ||
426c717a561Smaybee 			    dr->dt.dl.dr_data == db->db_buf);
427c717a561Smaybee 			dbuf_unoverride(dr);
428c717a561Smaybee 			mutex_exit(&db->db_mtx);
429c717a561Smaybee 		} else {
430c717a561Smaybee 			mutex_exit(&db->db_mtx);
431c717a561Smaybee 			dnode_undirty_dbufs(&dr->dt.di.dr_children);
432fa9e4066Sahrens 		}
433c717a561Smaybee 		kmem_free(dr, sizeof (dbuf_dirty_record_t));
434c717a561Smaybee 		dbuf_rele(db, (void *)(uintptr_t)txg);
435fa9e4066Sahrens 	}
436c717a561Smaybee }
437fa9e4066Sahrens 
438c717a561Smaybee static void
439c717a561Smaybee dnode_sync_free(dnode_t *dn, dmu_tx_t *tx)
440c717a561Smaybee {
441c717a561Smaybee 	int txgoff = tx->tx_txg & TXG_MASK;
442c717a561Smaybee 
443c717a561Smaybee 	ASSERT(dmu_tx_is_syncing(tx));
444c717a561Smaybee 
445c717a561Smaybee 	dnode_undirty_dbufs(&dn->dn_dirty_records[txgoff]);
446*1934e92fSmaybee 	dnode_evict_dbufs(dn);
447ea8dc4b6Seschrock 	ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL);
448ea8dc4b6Seschrock 
449ea8dc4b6Seschrock 	/*
450ea8dc4b6Seschrock 	 * XXX - It would be nice to assert this, but we may still
451ea8dc4b6Seschrock 	 * have residual holds from async evictions from the arc...
452ea8dc4b6Seschrock 	 *
45355434c77Sek 	 * zfs_obj_to_path() also depends on this being
45455434c77Sek 	 * commented out.
45555434c77Sek 	 *
456ea8dc4b6Seschrock 	 * ASSERT3U(refcount_count(&dn->dn_holds), ==, 1);
457ea8dc4b6Seschrock 	 */
458fa9e4066Sahrens 
459fa9e4066Sahrens 	/* Undirty next bits */
460fa9e4066Sahrens 	dn->dn_next_nlevels[txgoff] = 0;
461fa9e4066Sahrens 	dn->dn_next_indblkshift[txgoff] = 0;
462c543ec06Sahrens 	dn->dn_next_blksz[txgoff] = 0;
463fa9e4066Sahrens 
464fa9e4066Sahrens 	/* free up all the blocks in the file. */
465fa9e4066Sahrens 	dnode_sync_free_range(dn, 0, dn->dn_phys->dn_maxblkid+1, tx);
46699653d4eSeschrock 	ASSERT3U(DN_USED_BYTES(dn->dn_phys), ==, 0);
467fa9e4066Sahrens 
468fa9e4066Sahrens 	/* ASSERT(blkptrs are zero); */
469fa9e4066Sahrens 	ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE);
470fa9e4066Sahrens 	ASSERT(dn->dn_type != DMU_OT_NONE);
471fa9e4066Sahrens 
472fa9e4066Sahrens 	ASSERT(dn->dn_free_txg > 0);
473fa9e4066Sahrens 	if (dn->dn_allocated_txg != dn->dn_free_txg)
474fa9e4066Sahrens 		dbuf_will_dirty(dn->dn_dbuf, tx);
475fa9e4066Sahrens 	bzero(dn->dn_phys, sizeof (dnode_phys_t));
476fa9e4066Sahrens 
477fa9e4066Sahrens 	mutex_enter(&dn->dn_mtx);
478fa9e4066Sahrens 	dn->dn_type = DMU_OT_NONE;
479fa9e4066Sahrens 	dn->dn_maxblkid = 0;
480fa9e4066Sahrens 	dn->dn_allocated_txg = 0;
481758f6e0bSgw 	dn->dn_free_txg = 0;
482fa9e4066Sahrens 	mutex_exit(&dn->dn_mtx);
483fa9e4066Sahrens 
484ea8dc4b6Seschrock 	ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
485fa9e4066Sahrens 
486fa9e4066Sahrens 	dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg);
487fa9e4066Sahrens 	/*
488fa9e4066Sahrens 	 * Now that we've released our hold, the dnode may
489fa9e4066Sahrens 	 * be evicted, so we musn't access it.
490fa9e4066Sahrens 	 */
491fa9e4066Sahrens }
492fa9e4066Sahrens 
493fa9e4066Sahrens /*
494c717a561Smaybee  * Write out the dnode's dirty buffers.
495fa9e4066Sahrens  *
496fa9e4066Sahrens  * NOTE: The dnode is kept in memory by being dirty.  Once the
497fa9e4066Sahrens  * dirty bit is cleared, it may be evicted.  Beware of this!
498fa9e4066Sahrens  */
499c717a561Smaybee void
500c717a561Smaybee dnode_sync(dnode_t *dn, dmu_tx_t *tx)
501fa9e4066Sahrens {
502fa9e4066Sahrens 	free_range_t *rp;
503fa9e4066Sahrens 	dnode_phys_t *dnp = dn->dn_phys;
504c717a561Smaybee 	int txgoff = tx->tx_txg & TXG_MASK;
505c717a561Smaybee 	list_t *list = &dn->dn_dirty_records[txgoff];
506fa9e4066Sahrens 
507fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
508fa9e4066Sahrens 	ASSERT(dnp->dn_type != DMU_OT_NONE || dn->dn_allocated_txg);
5099c9dc39aSek 	DNODE_VERIFY(dn);
510c543ec06Sahrens 
511c717a561Smaybee 	ASSERT(dn->dn_dbuf == NULL || arc_released(dn->dn_dbuf->db_buf));
512fa9e4066Sahrens 
513fa9e4066Sahrens 	mutex_enter(&dn->dn_mtx);
514fa9e4066Sahrens 	if (dn->dn_allocated_txg == tx->tx_txg) {
515fa9e4066Sahrens 		/* The dnode is newly allocated or reallocated */
516fa9e4066Sahrens 		if (dnp->dn_type == DMU_OT_NONE) {
517fa9e4066Sahrens 			/* this is a first alloc, not a realloc */
518fa9e4066Sahrens 			/* XXX shouldn't the phys already be zeroed? */
519fa9e4066Sahrens 			bzero(dnp, DNODE_CORE_SIZE);
520fa9e4066Sahrens 			dnp->dn_nlevels = 1;
521fa9e4066Sahrens 		}
522fa9e4066Sahrens 
523fa9e4066Sahrens 		if (dn->dn_nblkptr > dnp->dn_nblkptr) {
524fa9e4066Sahrens 			/* zero the new blkptrs we are gaining */
525fa9e4066Sahrens 			bzero(dnp->dn_blkptr + dnp->dn_nblkptr,
526fa9e4066Sahrens 			    sizeof (blkptr_t) *
527fa9e4066Sahrens 			    (dn->dn_nblkptr - dnp->dn_nblkptr));
528fa9e4066Sahrens 		}
529fa9e4066Sahrens 		dnp->dn_type = dn->dn_type;
530fa9e4066Sahrens 		dnp->dn_bonustype = dn->dn_bonustype;
531fa9e4066Sahrens 		dnp->dn_bonuslen = dn->dn_bonuslen;
532fa9e4066Sahrens 		dnp->dn_nblkptr = dn->dn_nblkptr;
533fa9e4066Sahrens 	}
534fa9e4066Sahrens 
535c717a561Smaybee 	ASSERT(dnp->dn_nlevels > 1 ||
536f676ed34Sahrens 	    BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
537f676ed34Sahrens 	    BP_GET_LSIZE(&dnp->dn_blkptr[0]) ==
538f676ed34Sahrens 	    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
539f676ed34Sahrens 
540c543ec06Sahrens 	if (dn->dn_next_blksz[txgoff]) {
541c543ec06Sahrens 		ASSERT(P2PHASE(dn->dn_next_blksz[txgoff],
542fa9e4066Sahrens 		    SPA_MINBLOCKSIZE) == 0);
543f676ed34Sahrens 		ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
544c717a561Smaybee 		    list_head(list) != NULL ||
545347a31bcSahrens 		    dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT ==
546347a31bcSahrens 		    dnp->dn_datablkszsec);
547fa9e4066Sahrens 		dnp->dn_datablkszsec =
548c543ec06Sahrens 		    dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT;
549c543ec06Sahrens 		dn->dn_next_blksz[txgoff] = 0;
550fa9e4066Sahrens 	}
551fa9e4066Sahrens 
552*1934e92fSmaybee 	if (dn->dn_next_bonuslen[txgoff]) {
553*1934e92fSmaybee 		if (dn->dn_next_bonuslen[txgoff] == DN_ZERO_BONUSLEN)
554*1934e92fSmaybee 			dnp->dn_bonuslen = 0;
555*1934e92fSmaybee 		else
556*1934e92fSmaybee 			dnp->dn_bonuslen = dn->dn_next_bonuslen[txgoff];
557*1934e92fSmaybee 		ASSERT(dnp->dn_bonuslen <= DN_MAX_BONUSLEN);
558*1934e92fSmaybee 		dn->dn_next_bonuslen[txgoff] = 0;
559*1934e92fSmaybee 	}
560*1934e92fSmaybee 
561fa9e4066Sahrens 	if (dn->dn_next_indblkshift[txgoff]) {
562fa9e4066Sahrens 		ASSERT(dnp->dn_nlevels == 1);
563fa9e4066Sahrens 		dnp->dn_indblkshift = dn->dn_next_indblkshift[txgoff];
564fa9e4066Sahrens 		dn->dn_next_indblkshift[txgoff] = 0;
565fa9e4066Sahrens 	}
566fa9e4066Sahrens 
567fa9e4066Sahrens 	/*
568fa9e4066Sahrens 	 * Just take the live (open-context) values for checksum and compress.
569fa9e4066Sahrens 	 * Strictly speaking it's a future leak, but nothing bad happens if we
570fa9e4066Sahrens 	 * start using the new checksum or compress algorithm a little early.
571fa9e4066Sahrens 	 */
572fa9e4066Sahrens 	dnp->dn_checksum = dn->dn_checksum;
573fa9e4066Sahrens 	dnp->dn_compress = dn->dn_compress;
574fa9e4066Sahrens 
575fa9e4066Sahrens 	mutex_exit(&dn->dn_mtx);
576fa9e4066Sahrens 
577fa9e4066Sahrens 	/* process all the "freed" ranges in the file */
578fa9e4066Sahrens 	if (dn->dn_free_txg == 0 || dn->dn_free_txg > tx->tx_txg) {
57923b11526Smaybee 		for (rp = avl_last(&dn->dn_ranges[txgoff]); rp != NULL;
58023b11526Smaybee 		    rp = AVL_PREV(&dn->dn_ranges[txgoff], rp))
581fa9e4066Sahrens 			dnode_sync_free_range(dn,
582fa9e4066Sahrens 			    rp->fr_blkid, rp->fr_nblks, tx);
583fa9e4066Sahrens 	}
584*1934e92fSmaybee 	/* grab the mutex so we don't race with dnode_block_freed() */
585fa9e4066Sahrens 	mutex_enter(&dn->dn_mtx);
586fa9e4066Sahrens 	for (rp = avl_first(&dn->dn_ranges[txgoff]); rp; ) {
587*1934e92fSmaybee 
588fa9e4066Sahrens 		free_range_t *last = rp;
589fa9e4066Sahrens 		rp = AVL_NEXT(&dn->dn_ranges[txgoff], rp);
590fa9e4066Sahrens 		avl_remove(&dn->dn_ranges[txgoff], last);
591fa9e4066Sahrens 		kmem_free(last, sizeof (free_range_t));
592fa9e4066Sahrens 	}
593fa9e4066Sahrens 	mutex_exit(&dn->dn_mtx);
594fa9e4066Sahrens 	if (dn->dn_free_txg > 0 && dn->dn_free_txg <= tx->tx_txg) {
595c717a561Smaybee 		dnode_sync_free(dn, tx);
596c717a561Smaybee 		return;
597fa9e4066Sahrens 	}
598fa9e4066Sahrens 
599fa9e4066Sahrens 	if (dn->dn_next_nlevels[txgoff]) {
600c717a561Smaybee 		dnode_increase_indirection(dn, tx);
601fa9e4066Sahrens 		dn->dn_next_nlevels[txgoff] = 0;
602fa9e4066Sahrens 	}
603fa9e4066Sahrens 
604c717a561Smaybee 	dbuf_sync_list(list, tx);
605fa9e4066Sahrens 
606c717a561Smaybee 	if (dn->dn_object != DMU_META_DNODE_OBJECT) {
607c717a561Smaybee 		ASSERT3P(list_head(list), ==, NULL);
608c717a561Smaybee 		dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg);
609fa9e4066Sahrens 	}
610c717a561Smaybee 
611c717a561Smaybee 	/*
612c717a561Smaybee 	 * Although we have dropped our reference to the dnode, it
613c717a561Smaybee 	 * can't be evicted until its written, and we haven't yet
614c717a561Smaybee 	 * initiated the IO for the dnode's dbuf.
615c717a561Smaybee 	 */
616fa9e4066Sahrens }
617