xref: /illumos-gate/usr/src/uts/common/fs/zfs/dnode_sync.c (revision 0a586cea3ceec7e5e50e7e54c745082a7a333ac2)
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 /*
22*0a586ceaSMark Shellenbaum  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26fa9e4066Sahrens #include <sys/zfs_context.h>
27fa9e4066Sahrens #include <sys/dbuf.h>
28fa9e4066Sahrens #include <sys/dnode.h>
29fa9e4066Sahrens #include <sys/dmu.h>
30fa9e4066Sahrens #include <sys/dmu_tx.h>
31fa9e4066Sahrens #include <sys/dmu_objset.h>
32fa9e4066Sahrens #include <sys/dsl_dataset.h>
33fa9e4066Sahrens #include <sys/spa.h>
34fa9e4066Sahrens 
35fa9e4066Sahrens static void
36fa9e4066Sahrens dnode_increase_indirection(dnode_t *dn, dmu_tx_t *tx)
37fa9e4066Sahrens {
38fa9e4066Sahrens 	dmu_buf_impl_t *db;
39c717a561Smaybee 	int txgoff = tx->tx_txg & TXG_MASK;
40c717a561Smaybee 	int nblkptr = dn->dn_phys->dn_nblkptr;
41c717a561Smaybee 	int old_toplvl = dn->dn_phys->dn_nlevels - 1;
42c717a561Smaybee 	int new_level = dn->dn_next_nlevels[txgoff];
43fa9e4066Sahrens 	int i;
44fa9e4066Sahrens 
45c717a561Smaybee 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
46c717a561Smaybee 
47c717a561Smaybee 	/* this dnode can't be paged out because it's dirty */
48fa9e4066Sahrens 	ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE);
49fa9e4066Sahrens 	ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
50c717a561Smaybee 	ASSERT(new_level > 1 && dn->dn_phys->dn_nlevels > 0);
51fa9e4066Sahrens 
52fa9e4066Sahrens 	db = dbuf_hold_level(dn, dn->dn_phys->dn_nlevels, 0, FTAG);
53ea8dc4b6Seschrock 	ASSERT(db != NULL);
54c717a561Smaybee 
55c717a561Smaybee 	dn->dn_phys->dn_nlevels = new_level;
56f82bfe17Sgw 	dprintf("os=%p obj=%llu, increase to %d\n", dn->dn_objset,
57f82bfe17Sgw 	    dn->dn_object, dn->dn_phys->dn_nlevels);
58c717a561Smaybee 
59c717a561Smaybee 	/* check for existing blkptrs in the dnode */
60c717a561Smaybee 	for (i = 0; i < nblkptr; i++)
61fa9e4066Sahrens 		if (!BP_IS_HOLE(&dn->dn_phys->dn_blkptr[i]))
62fa9e4066Sahrens 			break;
63c717a561Smaybee 	if (i != nblkptr) {
64c717a561Smaybee 		/* transfer dnode's block pointers to new indirect block */
65c717a561Smaybee 		(void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED|DB_RF_HAVESTRUCT);
66c717a561Smaybee 		ASSERT(db->db.db_data);
67c717a561Smaybee 		ASSERT(arc_released(db->db_buf));
68c717a561Smaybee 		ASSERT3U(sizeof (blkptr_t) * nblkptr, <=, db->db.db_size);
69fa9e4066Sahrens 		bcopy(dn->dn_phys->dn_blkptr, db->db.db_data,
70c717a561Smaybee 		    sizeof (blkptr_t) * nblkptr);
716b4acc8bSahrens 		arc_buf_freeze(db->db_buf);
72fa9e4066Sahrens 	}
73fa9e4066Sahrens 
74fa9e4066Sahrens 	/* set dbuf's parent pointers to new indirect buf */
75c717a561Smaybee 	for (i = 0; i < nblkptr; i++) {
76c717a561Smaybee 		dmu_buf_impl_t *child = dbuf_find(dn, old_toplvl, i);
77c717a561Smaybee 
78fa9e4066Sahrens 		if (child == NULL)
79fa9e4066Sahrens 			continue;
80c717a561Smaybee 		ASSERT3P(child->db_dnode, ==, dn);
81c717a561Smaybee 		if (child->db_parent && child->db_parent != dn->dn_dbuf) {
82c717a561Smaybee 			ASSERT(child->db_parent->db_level == db->db_level);
83c717a561Smaybee 			ASSERT(child->db_blkptr !=
84c717a561Smaybee 			    &dn->dn_phys->dn_blkptr[child->db_blkid]);
85fa9e4066Sahrens 			mutex_exit(&child->db_mtx);
86fa9e4066Sahrens 			continue;
87fa9e4066Sahrens 		}
88c717a561Smaybee 		ASSERT(child->db_parent == NULL ||
89c717a561Smaybee 		    child->db_parent == dn->dn_dbuf);
90c717a561Smaybee 
91c717a561Smaybee 		child->db_parent = db;
92c717a561Smaybee 		dbuf_add_ref(db, child);
93c717a561Smaybee 		if (db->db.db_data)
94c717a561Smaybee 			child->db_blkptr = (blkptr_t *)db->db.db_data + i;
95c717a561Smaybee 		else
96c717a561Smaybee 			child->db_blkptr = NULL;
97c717a561Smaybee 		dprintf_dbuf_bp(child, child->db_blkptr,
98c717a561Smaybee 		    "changed db_blkptr to new indirect %s", "");
99fa9e4066Sahrens 
100fa9e4066Sahrens 		mutex_exit(&child->db_mtx);
101fa9e4066Sahrens 	}
102fa9e4066Sahrens 
103c717a561Smaybee 	bzero(dn->dn_phys->dn_blkptr, sizeof (blkptr_t) * nblkptr);
104fa9e4066Sahrens 
105ea8dc4b6Seschrock 	dbuf_rele(db, FTAG);
106c717a561Smaybee 
107c717a561Smaybee 	rw_exit(&dn->dn_struct_rwlock);
108fa9e4066Sahrens }
109fa9e4066Sahrens 
110cdb0ab79Smaybee static int
111fa9e4066Sahrens free_blocks(dnode_t *dn, blkptr_t *bp, int num, dmu_tx_t *tx)
112fa9e4066Sahrens {
113cdb0ab79Smaybee 	dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
114fa9e4066Sahrens 	uint64_t bytesfreed = 0;
115cdb0ab79Smaybee 	int i, blocks_freed = 0;
116fa9e4066Sahrens 
117cdb0ab79Smaybee 	dprintf("ds=%p obj=%llx num=%d\n", ds, dn->dn_object, num);
118fa9e4066Sahrens 
119fa9e4066Sahrens 	for (i = 0; i < num; i++, bp++) {
120fa9e4066Sahrens 		if (BP_IS_HOLE(bp))
121fa9e4066Sahrens 			continue;
122fa9e4066Sahrens 
123b24ab676SJeff Bonwick 		bytesfreed += dsl_dataset_block_kill(ds, bp, tx, B_FALSE);
12499653d4eSeschrock 		ASSERT3U(bytesfreed, <=, DN_USED_BYTES(dn->dn_phys));
125c717a561Smaybee 		bzero(bp, sizeof (blkptr_t));
126cdb0ab79Smaybee 		blocks_freed += 1;
127fa9e4066Sahrens 	}
128fa9e4066Sahrens 	dnode_diduse_space(dn, -bytesfreed);
129cdb0ab79Smaybee 	return (blocks_freed);
130fa9e4066Sahrens }
131fa9e4066Sahrens 
1329c9dc39aSek #ifdef ZFS_DEBUG
133fa9e4066Sahrens static void
134fa9e4066Sahrens free_verify(dmu_buf_impl_t *db, uint64_t start, uint64_t end, dmu_tx_t *tx)
135fa9e4066Sahrens {
136fa9e4066Sahrens 	int off, num;
137fa9e4066Sahrens 	int i, err, epbs;
138fa9e4066Sahrens 	uint64_t txg = tx->tx_txg;
139fa9e4066Sahrens 
140fa9e4066Sahrens 	epbs = db->db_dnode->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
141fa9e4066Sahrens 	off = start - (db->db_blkid * 1<<epbs);
142fa9e4066Sahrens 	num = end - start + 1;
143fa9e4066Sahrens 
144fa9e4066Sahrens 	ASSERT3U(off, >=, 0);
145fa9e4066Sahrens 	ASSERT3U(num, >=, 0);
146fa9e4066Sahrens 	ASSERT3U(db->db_level, >, 0);
147fa9e4066Sahrens 	ASSERT3U(db->db.db_size, ==, 1<<db->db_dnode->dn_phys->dn_indblkshift);
148fa9e4066Sahrens 	ASSERT3U(off+num, <=, db->db.db_size >> SPA_BLKPTRSHIFT);
149fa9e4066Sahrens 	ASSERT(db->db_blkptr != NULL);
150fa9e4066Sahrens 
151fa9e4066Sahrens 	for (i = off; i < off+num; i++) {
152fa9e4066Sahrens 		uint64_t *buf;
153fa9e4066Sahrens 		dmu_buf_impl_t *child;
154c717a561Smaybee 		dbuf_dirty_record_t *dr;
155c717a561Smaybee 		int j;
156fa9e4066Sahrens 
157fa9e4066Sahrens 		ASSERT(db->db_level == 1);
158fa9e4066Sahrens 
159fa9e4066Sahrens 		rw_enter(&db->db_dnode->dn_struct_rwlock, RW_READER);
160fa9e4066Sahrens 		err = dbuf_hold_impl(db->db_dnode, db->db_level-1,
161f82bfe17Sgw 		    (db->db_blkid << epbs) + i, TRUE, FTAG, &child);
162fa9e4066Sahrens 		rw_exit(&db->db_dnode->dn_struct_rwlock);
163fa9e4066Sahrens 		if (err == ENOENT)
164fa9e4066Sahrens 			continue;
165fa9e4066Sahrens 		ASSERT(err == 0);
166fa9e4066Sahrens 		ASSERT(child->db_level == 0);
167c717a561Smaybee 		dr = child->db_last_dirty;
168c717a561Smaybee 		while (dr && dr->dr_txg > txg)
169c717a561Smaybee 			dr = dr->dr_next;
170c717a561Smaybee 		ASSERT(dr == NULL || dr->dr_txg == txg);
171c717a561Smaybee 
172c717a561Smaybee 		/* data_old better be zeroed */
173c717a561Smaybee 		if (dr) {
174c717a561Smaybee 			buf = dr->dt.dl.dr_data->b_data;
175fa9e4066Sahrens 			for (j = 0; j < child->db.db_size >> 3; j++) {
176fa9e4066Sahrens 				if (buf[j] != 0) {
177fa9e4066Sahrens 					panic("freed data not zero: "
178fa9e4066Sahrens 					    "child=%p i=%d off=%d num=%d\n",
179903a11ebSrh 					    (void *)child, i, off, num);
180fa9e4066Sahrens 				}
181fa9e4066Sahrens 			}
182fa9e4066Sahrens 		}
183fa9e4066Sahrens 
184fa9e4066Sahrens 		/*
185fa9e4066Sahrens 		 * db_data better be zeroed unless it's dirty in a
186fa9e4066Sahrens 		 * future txg.
187fa9e4066Sahrens 		 */
188fa9e4066Sahrens 		mutex_enter(&child->db_mtx);
189fa9e4066Sahrens 		buf = child->db.db_data;
190fa9e4066Sahrens 		if (buf != NULL && child->db_state != DB_FILL &&
191c717a561Smaybee 		    child->db_last_dirty == NULL) {
192fa9e4066Sahrens 			for (j = 0; j < child->db.db_size >> 3; j++) {
193fa9e4066Sahrens 				if (buf[j] != 0) {
194fa9e4066Sahrens 					panic("freed data not zero: "
195fa9e4066Sahrens 					    "child=%p i=%d off=%d num=%d\n",
196903a11ebSrh 					    (void *)child, i, off, num);
197fa9e4066Sahrens 				}
198fa9e4066Sahrens 			}
199fa9e4066Sahrens 		}
200fa9e4066Sahrens 		mutex_exit(&child->db_mtx);
201fa9e4066Sahrens 
202ea8dc4b6Seschrock 		dbuf_rele(child, FTAG);
203fa9e4066Sahrens 	}
204fa9e4066Sahrens }
2059c9dc39aSek #endif
206fa9e4066Sahrens 
207cdb0ab79Smaybee #define	ALL -1
208cdb0ab79Smaybee 
209fa9e4066Sahrens static int
210fa9e4066Sahrens free_children(dmu_buf_impl_t *db, uint64_t blkid, uint64_t nblks, int trunc,
211fa9e4066Sahrens     dmu_tx_t *tx)
212fa9e4066Sahrens {
213fa9e4066Sahrens 	dnode_t *dn = db->db_dnode;
214fa9e4066Sahrens 	blkptr_t *bp;
215fa9e4066Sahrens 	dmu_buf_impl_t *subdb;
216fa9e4066Sahrens 	uint64_t start, end, dbstart, dbend, i;
217fa9e4066Sahrens 	int epbs, shift, err;
218fa9e4066Sahrens 	int all = TRUE;
219cdb0ab79Smaybee 	int blocks_freed = 0;
220cdb0ab79Smaybee 
221cdb0ab79Smaybee 	/*
222cdb0ab79Smaybee 	 * There is a small possibility that this block will not be cached:
223cdb0ab79Smaybee 	 *   1 - if level > 1 and there are no children with level <= 1
224cdb0ab79Smaybee 	 *   2 - if we didn't get a dirty hold (because this block had just
225cdb0ab79Smaybee 	 *	 finished being written -- and so had no holds), and then this
226cdb0ab79Smaybee 	 *	 block got evicted before we got here.
227cdb0ab79Smaybee 	 */
228cdb0ab79Smaybee 	if (db->db_state != DB_CACHED)
229cdb0ab79Smaybee 		(void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
230fa9e4066Sahrens 
231fa9e4066Sahrens 	arc_release(db->db_buf, db);
232fa9e4066Sahrens 	bp = (blkptr_t *)db->db.db_data;
233fa9e4066Sahrens 
234fa9e4066Sahrens 	epbs = db->db_dnode->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
235fa9e4066Sahrens 	shift = (db->db_level - 1) * epbs;
236fa9e4066Sahrens 	dbstart = db->db_blkid << epbs;
237fa9e4066Sahrens 	start = blkid >> shift;
238fa9e4066Sahrens 	if (dbstart < start) {
239fa9e4066Sahrens 		bp += start - dbstart;
240fa9e4066Sahrens 		all = FALSE;
241fa9e4066Sahrens 	} else {
242fa9e4066Sahrens 		start = dbstart;
243fa9e4066Sahrens 	}
244fa9e4066Sahrens 	dbend = ((db->db_blkid + 1) << epbs) - 1;
245fa9e4066Sahrens 	end = (blkid + nblks - 1) >> shift;
246fa9e4066Sahrens 	if (dbend <= end)
247fa9e4066Sahrens 		end = dbend;
248fa9e4066Sahrens 	else if (all)
249fa9e4066Sahrens 		all = trunc;
250fa9e4066Sahrens 	ASSERT3U(start, <=, end);
251fa9e4066Sahrens 
252fa9e4066Sahrens 	if (db->db_level == 1) {
2539c9dc39aSek 		FREE_VERIFY(db, start, end, tx);
254cdb0ab79Smaybee 		blocks_freed = free_blocks(dn, bp, end-start+1, tx);
2556b4acc8bSahrens 		arc_buf_freeze(db->db_buf);
256cdb0ab79Smaybee 		ASSERT(all || blocks_freed == 0 || db->db_last_dirty);
257cdb0ab79Smaybee 		return (all ? ALL : blocks_freed);
258fa9e4066Sahrens 	}
259fa9e4066Sahrens 
260fa9e4066Sahrens 	for (i = start; i <= end; i++, bp++) {
261fa9e4066Sahrens 		if (BP_IS_HOLE(bp))
262fa9e4066Sahrens 			continue;
263fa9e4066Sahrens 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
264fa9e4066Sahrens 		err = dbuf_hold_impl(dn, db->db_level-1, i, TRUE, FTAG, &subdb);
265fa9e4066Sahrens 		ASSERT3U(err, ==, 0);
266fa9e4066Sahrens 		rw_exit(&dn->dn_struct_rwlock);
267fa9e4066Sahrens 
268cdb0ab79Smaybee 		if (free_children(subdb, blkid, nblks, trunc, tx) == ALL) {
269fa9e4066Sahrens 			ASSERT3P(subdb->db_blkptr, ==, bp);
270cdb0ab79Smaybee 			blocks_freed += free_blocks(dn, bp, 1, tx);
27123b11526Smaybee 		} else {
27223b11526Smaybee 			all = FALSE;
273fa9e4066Sahrens 		}
274ea8dc4b6Seschrock 		dbuf_rele(subdb, FTAG);
275fa9e4066Sahrens 	}
2766b4acc8bSahrens 	arc_buf_freeze(db->db_buf);
277fa9e4066Sahrens #ifdef ZFS_DEBUG
278fa9e4066Sahrens 	bp -= (end-start)+1;
279fa9e4066Sahrens 	for (i = start; i <= end; i++, bp++) {
280fa9e4066Sahrens 		if (i == start && blkid != 0)
281fa9e4066Sahrens 			continue;
282fa9e4066Sahrens 		else if (i == end && !trunc)
283fa9e4066Sahrens 			continue;
284fa9e4066Sahrens 		ASSERT3U(bp->blk_birth, ==, 0);
285fa9e4066Sahrens 	}
286fa9e4066Sahrens #endif
287cdb0ab79Smaybee 	ASSERT(all || blocks_freed == 0 || db->db_last_dirty);
288cdb0ab79Smaybee 	return (all ? ALL : blocks_freed);
289fa9e4066Sahrens }
290fa9e4066Sahrens 
291fa9e4066Sahrens /*
292fa9e4066Sahrens  * free_range: Traverse the indicated range of the provided file
293fa9e4066Sahrens  * and "free" all the blocks contained there.
294fa9e4066Sahrens  */
295fa9e4066Sahrens static void
296fa9e4066Sahrens dnode_sync_free_range(dnode_t *dn, uint64_t blkid, uint64_t nblks, dmu_tx_t *tx)
297fa9e4066Sahrens {
298fa9e4066Sahrens 	blkptr_t *bp = dn->dn_phys->dn_blkptr;
299fa9e4066Sahrens 	dmu_buf_impl_t *db;
300fa9e4066Sahrens 	int trunc, start, end, shift, i, err;
301fa9e4066Sahrens 	int dnlevel = dn->dn_phys->dn_nlevels;
302fa9e4066Sahrens 
303fa9e4066Sahrens 	if (blkid > dn->dn_phys->dn_maxblkid)
304fa9e4066Sahrens 		return;
305fa9e4066Sahrens 
306fa9e4066Sahrens 	ASSERT(dn->dn_phys->dn_maxblkid < UINT64_MAX);
307fa9e4066Sahrens 	trunc = blkid + nblks > dn->dn_phys->dn_maxblkid;
308fa9e4066Sahrens 	if (trunc)
309fa9e4066Sahrens 		nblks = dn->dn_phys->dn_maxblkid - blkid + 1;
310fa9e4066Sahrens 
311fa9e4066Sahrens 	/* There are no indirect blocks in the object */
312fa9e4066Sahrens 	if (dnlevel == 1) {
313fa9e4066Sahrens 		if (blkid >= dn->dn_phys->dn_nblkptr) {
314fa9e4066Sahrens 			/* this range was never made persistent */
315fa9e4066Sahrens 			return;
316fa9e4066Sahrens 		}
317fa9e4066Sahrens 		ASSERT3U(blkid + nblks, <=, dn->dn_phys->dn_nblkptr);
318cdb0ab79Smaybee 		(void) free_blocks(dn, bp + blkid, nblks, tx);
319fa9e4066Sahrens 		if (trunc) {
320fa9e4066Sahrens 			uint64_t off = (dn->dn_phys->dn_maxblkid + 1) *
321fa9e4066Sahrens 			    (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT);
322fa9e4066Sahrens 			dn->dn_phys->dn_maxblkid = (blkid ? blkid - 1 : 0);
323fa9e4066Sahrens 			ASSERT(off < dn->dn_phys->dn_maxblkid ||
324fa9e4066Sahrens 			    dn->dn_phys->dn_maxblkid == 0 ||
325cdb0ab79Smaybee 			    dnode_next_offset(dn, 0, &off, 1, 1, 0) != 0);
326fa9e4066Sahrens 		}
327fa9e4066Sahrens 		return;
328fa9e4066Sahrens 	}
329fa9e4066Sahrens 
330fa9e4066Sahrens 	shift = (dnlevel - 1) * (dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT);
331fa9e4066Sahrens 	start = blkid >> shift;
332fa9e4066Sahrens 	ASSERT(start < dn->dn_phys->dn_nblkptr);
333fa9e4066Sahrens 	end = (blkid + nblks - 1) >> shift;
334fa9e4066Sahrens 	bp += start;
335fa9e4066Sahrens 	for (i = start; i <= end; i++, bp++) {
336fa9e4066Sahrens 		if (BP_IS_HOLE(bp))
337fa9e4066Sahrens 			continue;
338fa9e4066Sahrens 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
339fa9e4066Sahrens 		err = dbuf_hold_impl(dn, dnlevel-1, i, TRUE, FTAG, &db);
340fa9e4066Sahrens 		ASSERT3U(err, ==, 0);
341fa9e4066Sahrens 		rw_exit(&dn->dn_struct_rwlock);
342fa9e4066Sahrens 
343cdb0ab79Smaybee 		if (free_children(db, blkid, nblks, trunc, tx) == ALL) {
344fa9e4066Sahrens 			ASSERT3P(db->db_blkptr, ==, bp);
345cdb0ab79Smaybee 			(void) free_blocks(dn, bp, 1, tx);
346fa9e4066Sahrens 		}
347ea8dc4b6Seschrock 		dbuf_rele(db, FTAG);
348fa9e4066Sahrens 	}
349fa9e4066Sahrens 	if (trunc) {
350fa9e4066Sahrens 		uint64_t off = (dn->dn_phys->dn_maxblkid + 1) *
351fa9e4066Sahrens 		    (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT);
352fa9e4066Sahrens 		dn->dn_phys->dn_maxblkid = (blkid ? blkid - 1 : 0);
353fa9e4066Sahrens 		ASSERT(off < dn->dn_phys->dn_maxblkid ||
354fa9e4066Sahrens 		    dn->dn_phys->dn_maxblkid == 0 ||
355cdb0ab79Smaybee 		    dnode_next_offset(dn, 0, &off, 1, 1, 0) != 0);
356fa9e4066Sahrens 	}
357fa9e4066Sahrens }
358fa9e4066Sahrens 
359ea8dc4b6Seschrock /*
360ea8dc4b6Seschrock  * Try to kick all the dnodes dbufs out of the cache...
361ea8dc4b6Seschrock  */
3621934e92fSmaybee void
3631934e92fSmaybee dnode_evict_dbufs(dnode_t *dn)
364ea8dc4b6Seschrock {
365c543ec06Sahrens 	int progress;
366c543ec06Sahrens 	int pass = 0;
367c543ec06Sahrens 
368c543ec06Sahrens 	do {
369d90a49d6Smaybee 		dmu_buf_impl_t *db, marker;
370c543ec06Sahrens 		int evicting = FALSE;
371c543ec06Sahrens 
372c543ec06Sahrens 		progress = FALSE;
373c543ec06Sahrens 		mutex_enter(&dn->dn_dbufs_mtx);
374d90a49d6Smaybee 		list_insert_tail(&dn->dn_dbufs, &marker);
375d90a49d6Smaybee 		db = list_head(&dn->dn_dbufs);
376d90a49d6Smaybee 		for (; db != &marker; db = list_head(&dn->dn_dbufs)) {
377d90a49d6Smaybee 			list_remove(&dn->dn_dbufs, db);
378d90a49d6Smaybee 			list_insert_tail(&dn->dn_dbufs, db);
379f82bfe17Sgw 			ASSERT3P(db->db_dnode, ==, dn);
380ea8dc4b6Seschrock 
381ea8dc4b6Seschrock 			mutex_enter(&db->db_mtx);
382c543ec06Sahrens 			if (db->db_state == DB_EVICTING) {
383c543ec06Sahrens 				progress = TRUE;
384c543ec06Sahrens 				evicting = TRUE;
385c543ec06Sahrens 				mutex_exit(&db->db_mtx);
386c543ec06Sahrens 			} else if (refcount_is_zero(&db->db_holds)) {
387c543ec06Sahrens 				progress = TRUE;
388c543ec06Sahrens 				dbuf_clear(db); /* exits db_mtx for us */
389c543ec06Sahrens 			} else {
390c543ec06Sahrens 				mutex_exit(&db->db_mtx);
391c543ec06Sahrens 			}
392c543ec06Sahrens 
393ea8dc4b6Seschrock 		}
394d90a49d6Smaybee 		list_remove(&dn->dn_dbufs, &marker);
395c543ec06Sahrens 		/*
396c543ec06Sahrens 		 * NB: we need to drop dn_dbufs_mtx between passes so
397c543ec06Sahrens 		 * that any DB_EVICTING dbufs can make progress.
398c543ec06Sahrens 		 * Ideally, we would have some cv we could wait on, but
399c543ec06Sahrens 		 * since we don't, just wait a bit to give the other
400c543ec06Sahrens 		 * thread a chance to run.
401c543ec06Sahrens 		 */
402c543ec06Sahrens 		mutex_exit(&dn->dn_dbufs_mtx);
403c543ec06Sahrens 		if (evicting)
404c543ec06Sahrens 			delay(1);
405c543ec06Sahrens 		pass++;
406c543ec06Sahrens 		ASSERT(pass < 100); /* sanity check */
407c543ec06Sahrens 	} while (progress);
408c543ec06Sahrens 
409ea8dc4b6Seschrock 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
410ea8dc4b6Seschrock 	if (dn->dn_bonus && refcount_is_zero(&dn->dn_bonus->db_holds)) {
411ea8dc4b6Seschrock 		mutex_enter(&dn->dn_bonus->db_mtx);
412ea8dc4b6Seschrock 		dbuf_evict(dn->dn_bonus);
413ea8dc4b6Seschrock 		dn->dn_bonus = NULL;
414ea8dc4b6Seschrock 	}
415ea8dc4b6Seschrock 	rw_exit(&dn->dn_struct_rwlock);
416ea8dc4b6Seschrock }
417ea8dc4b6Seschrock 
418c717a561Smaybee static void
419c717a561Smaybee dnode_undirty_dbufs(list_t *list)
420fa9e4066Sahrens {
421c717a561Smaybee 	dbuf_dirty_record_t *dr;
422fa9e4066Sahrens 
423c717a561Smaybee 	while (dr = list_head(list)) {
424c717a561Smaybee 		dmu_buf_impl_t *db = dr->dr_dbuf;
425c717a561Smaybee 		uint64_t txg = dr->dr_txg;
426fa9e4066Sahrens 
427b24ab676SJeff Bonwick 		if (db->db_level != 0)
428b24ab676SJeff Bonwick 			dnode_undirty_dbufs(&dr->dt.di.dr_children);
429b24ab676SJeff Bonwick 
430fa9e4066Sahrens 		mutex_enter(&db->db_mtx);
431fa9e4066Sahrens 		/* XXX - use dbuf_undirty()? */
432c717a561Smaybee 		list_remove(list, dr);
433c717a561Smaybee 		ASSERT(db->db_last_dirty == dr);
434c717a561Smaybee 		db->db_last_dirty = NULL;
435c717a561Smaybee 		db->db_dirtycnt -= 1;
436fa9e4066Sahrens 		if (db->db_level == 0) {
437*0a586ceaSMark Shellenbaum 			ASSERT(db->db_blkid == DMU_BONUS_BLKID ||
438c717a561Smaybee 			    dr->dt.dl.dr_data == db->db_buf);
439c717a561Smaybee 			dbuf_unoverride(dr);
440fa9e4066Sahrens 		}
441c717a561Smaybee 		kmem_free(dr, sizeof (dbuf_dirty_record_t));
442b24ab676SJeff Bonwick 		dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg);
443fa9e4066Sahrens 	}
444c717a561Smaybee }
445fa9e4066Sahrens 
446c717a561Smaybee static void
447c717a561Smaybee dnode_sync_free(dnode_t *dn, dmu_tx_t *tx)
448c717a561Smaybee {
449c717a561Smaybee 	int txgoff = tx->tx_txg & TXG_MASK;
450c717a561Smaybee 
451c717a561Smaybee 	ASSERT(dmu_tx_is_syncing(tx));
452c717a561Smaybee 
453cdb0ab79Smaybee 	/*
454cdb0ab79Smaybee 	 * Our contents should have been freed in dnode_sync() by the
455cdb0ab79Smaybee 	 * free range record inserted by the caller of dnode_free().
456cdb0ab79Smaybee 	 */
457cdb0ab79Smaybee 	ASSERT3U(DN_USED_BYTES(dn->dn_phys), ==, 0);
458cdb0ab79Smaybee 	ASSERT(BP_IS_HOLE(dn->dn_phys->dn_blkptr));
459cdb0ab79Smaybee 
460c717a561Smaybee 	dnode_undirty_dbufs(&dn->dn_dirty_records[txgoff]);
4611934e92fSmaybee 	dnode_evict_dbufs(dn);
462ea8dc4b6Seschrock 	ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL);
463ea8dc4b6Seschrock 
464ea8dc4b6Seschrock 	/*
465ea8dc4b6Seschrock 	 * XXX - It would be nice to assert this, but we may still
466ea8dc4b6Seschrock 	 * have residual holds from async evictions from the arc...
467ea8dc4b6Seschrock 	 *
46855434c77Sek 	 * zfs_obj_to_path() also depends on this being
46955434c77Sek 	 * commented out.
47055434c77Sek 	 *
471ea8dc4b6Seschrock 	 * ASSERT3U(refcount_count(&dn->dn_holds), ==, 1);
472ea8dc4b6Seschrock 	 */
473fa9e4066Sahrens 
474fa9e4066Sahrens 	/* Undirty next bits */
475fa9e4066Sahrens 	dn->dn_next_nlevels[txgoff] = 0;
476fa9e4066Sahrens 	dn->dn_next_indblkshift[txgoff] = 0;
477c543ec06Sahrens 	dn->dn_next_blksz[txgoff] = 0;
478fa9e4066Sahrens 
479fa9e4066Sahrens 	/* ASSERT(blkptrs are zero); */
480fa9e4066Sahrens 	ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE);
481fa9e4066Sahrens 	ASSERT(dn->dn_type != DMU_OT_NONE);
482fa9e4066Sahrens 
483fa9e4066Sahrens 	ASSERT(dn->dn_free_txg > 0);
484fa9e4066Sahrens 	if (dn->dn_allocated_txg != dn->dn_free_txg)
485fa9e4066Sahrens 		dbuf_will_dirty(dn->dn_dbuf, tx);
486fa9e4066Sahrens 	bzero(dn->dn_phys, sizeof (dnode_phys_t));
487fa9e4066Sahrens 
488fa9e4066Sahrens 	mutex_enter(&dn->dn_mtx);
489fa9e4066Sahrens 	dn->dn_type = DMU_OT_NONE;
490fa9e4066Sahrens 	dn->dn_maxblkid = 0;
491fa9e4066Sahrens 	dn->dn_allocated_txg = 0;
492758f6e0bSgw 	dn->dn_free_txg = 0;
493*0a586ceaSMark Shellenbaum 	dn->dn_have_spill = B_FALSE;
494fa9e4066Sahrens 	mutex_exit(&dn->dn_mtx);
495fa9e4066Sahrens 
496ea8dc4b6Seschrock 	ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
497fa9e4066Sahrens 
498fa9e4066Sahrens 	dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg);
499fa9e4066Sahrens 	/*
500fa9e4066Sahrens 	 * Now that we've released our hold, the dnode may
501fa9e4066Sahrens 	 * be evicted, so we musn't access it.
502fa9e4066Sahrens 	 */
503fa9e4066Sahrens }
504fa9e4066Sahrens 
505fa9e4066Sahrens /*
506c717a561Smaybee  * Write out the dnode's dirty buffers.
507fa9e4066Sahrens  */
508c717a561Smaybee void
509c717a561Smaybee dnode_sync(dnode_t *dn, dmu_tx_t *tx)
510fa9e4066Sahrens {
511fa9e4066Sahrens 	free_range_t *rp;
512fa9e4066Sahrens 	dnode_phys_t *dnp = dn->dn_phys;
513c717a561Smaybee 	int txgoff = tx->tx_txg & TXG_MASK;
514c717a561Smaybee 	list_t *list = &dn->dn_dirty_records[txgoff];
51514843421SMatthew Ahrens 	static const dnode_phys_t zerodn = { 0 };
516*0a586ceaSMark Shellenbaum 	boolean_t kill_spill = B_FALSE;
517fa9e4066Sahrens 
518fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
519fa9e4066Sahrens 	ASSERT(dnp->dn_type != DMU_OT_NONE || dn->dn_allocated_txg);
52014843421SMatthew Ahrens 	ASSERT(dnp->dn_type != DMU_OT_NONE ||
52114843421SMatthew Ahrens 	    bcmp(dnp, &zerodn, DNODE_SIZE) == 0);
5229c9dc39aSek 	DNODE_VERIFY(dn);
523c543ec06Sahrens 
524c717a561Smaybee 	ASSERT(dn->dn_dbuf == NULL || arc_released(dn->dn_dbuf->db_buf));
525fa9e4066Sahrens 
52614843421SMatthew Ahrens 	if (dmu_objset_userused_enabled(dn->dn_objset) &&
52714843421SMatthew Ahrens 	    !DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
528*0a586ceaSMark Shellenbaum 		mutex_enter(&dn->dn_mtx);
529*0a586ceaSMark Shellenbaum 		dn->dn_oldused = DN_USED_BYTES(dn->dn_phys);
530*0a586ceaSMark Shellenbaum 		dn->dn_oldflags = dn->dn_phys->dn_flags;
531*0a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_SYNC;
53214843421SMatthew Ahrens 		dn->dn_phys->dn_flags |= DNODE_FLAG_USERUSED_ACCOUNTED;
533*0a586ceaSMark Shellenbaum 		mutex_exit(&dn->dn_mtx);
534*0a586ceaSMark Shellenbaum 		dmu_objset_userquota_get_ids(dn, B_FALSE);
53514843421SMatthew Ahrens 	} else {
53614843421SMatthew Ahrens 		/* Once we account for it, we should always account for it. */
53714843421SMatthew Ahrens 		ASSERT(!(dn->dn_phys->dn_flags &
53814843421SMatthew Ahrens 		    DNODE_FLAG_USERUSED_ACCOUNTED));
53914843421SMatthew Ahrens 	}
54014843421SMatthew Ahrens 
541fa9e4066Sahrens 	mutex_enter(&dn->dn_mtx);
542fa9e4066Sahrens 	if (dn->dn_allocated_txg == tx->tx_txg) {
543fa9e4066Sahrens 		/* The dnode is newly allocated or reallocated */
544fa9e4066Sahrens 		if (dnp->dn_type == DMU_OT_NONE) {
545fa9e4066Sahrens 			/* this is a first alloc, not a realloc */
546fa9e4066Sahrens 			dnp->dn_nlevels = 1;
547da03de99SMark Maybee 			dnp->dn_nblkptr = dn->dn_nblkptr;
548fa9e4066Sahrens 		}
549fa9e4066Sahrens 
550fa9e4066Sahrens 		dnp->dn_type = dn->dn_type;
551fa9e4066Sahrens 		dnp->dn_bonustype = dn->dn_bonustype;
552fa9e4066Sahrens 		dnp->dn_bonuslen = dn->dn_bonuslen;
553fa9e4066Sahrens 	}
554fa9e4066Sahrens 
555c717a561Smaybee 	ASSERT(dnp->dn_nlevels > 1 ||
556f676ed34Sahrens 	    BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
557f676ed34Sahrens 	    BP_GET_LSIZE(&dnp->dn_blkptr[0]) ==
558f676ed34Sahrens 	    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
559f676ed34Sahrens 
560c543ec06Sahrens 	if (dn->dn_next_blksz[txgoff]) {
561c543ec06Sahrens 		ASSERT(P2PHASE(dn->dn_next_blksz[txgoff],
562fa9e4066Sahrens 		    SPA_MINBLOCKSIZE) == 0);
563f676ed34Sahrens 		ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
564cdb0ab79Smaybee 		    dn->dn_maxblkid == 0 || list_head(list) != NULL ||
565347a31bcSahrens 		    dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT ==
566347a31bcSahrens 		    dnp->dn_datablkszsec);
567fa9e4066Sahrens 		dnp->dn_datablkszsec =
568c543ec06Sahrens 		    dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT;
569c543ec06Sahrens 		dn->dn_next_blksz[txgoff] = 0;
570fa9e4066Sahrens 	}
571fa9e4066Sahrens 
5721934e92fSmaybee 	if (dn->dn_next_bonuslen[txgoff]) {
5731934e92fSmaybee 		if (dn->dn_next_bonuslen[txgoff] == DN_ZERO_BONUSLEN)
5741934e92fSmaybee 			dnp->dn_bonuslen = 0;
5751934e92fSmaybee 		else
5761934e92fSmaybee 			dnp->dn_bonuslen = dn->dn_next_bonuslen[txgoff];
5771934e92fSmaybee 		ASSERT(dnp->dn_bonuslen <= DN_MAX_BONUSLEN);
5781934e92fSmaybee 		dn->dn_next_bonuslen[txgoff] = 0;
5791934e92fSmaybee 	}
5801934e92fSmaybee 
581*0a586ceaSMark Shellenbaum 	if (dn->dn_next_bonustype[txgoff]) {
582*0a586ceaSMark Shellenbaum 		ASSERT(dn->dn_next_bonustype[txgoff] < DMU_OT_NUMTYPES);
583*0a586ceaSMark Shellenbaum 		dnp->dn_bonustype = dn->dn_next_bonustype[txgoff];
584*0a586ceaSMark Shellenbaum 		dn->dn_next_bonustype[txgoff] = 0;
585*0a586ceaSMark Shellenbaum 	}
586*0a586ceaSMark Shellenbaum 
587*0a586ceaSMark Shellenbaum 	/*
588*0a586ceaSMark Shellenbaum 	 * We will either remove a spill block when a file is being removed
589*0a586ceaSMark Shellenbaum 	 * or we have been asked to remove it.
590*0a586ceaSMark Shellenbaum 	 */
591*0a586ceaSMark Shellenbaum 	if (dn->dn_rm_spillblk[txgoff] ||
592*0a586ceaSMark Shellenbaum 	    ((dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) &&
593*0a586ceaSMark Shellenbaum 	    dn->dn_free_txg > 0 && dn->dn_free_txg <= tx->tx_txg)) {
594*0a586ceaSMark Shellenbaum 		if ((dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR))
595*0a586ceaSMark Shellenbaum 			kill_spill = B_TRUE;
596*0a586ceaSMark Shellenbaum 		dn->dn_rm_spillblk[txgoff] = 0;
597*0a586ceaSMark Shellenbaum 	}
598*0a586ceaSMark Shellenbaum 
599fa9e4066Sahrens 	if (dn->dn_next_indblkshift[txgoff]) {
600fa9e4066Sahrens 		ASSERT(dnp->dn_nlevels == 1);
601fa9e4066Sahrens 		dnp->dn_indblkshift = dn->dn_next_indblkshift[txgoff];
602fa9e4066Sahrens 		dn->dn_next_indblkshift[txgoff] = 0;
603fa9e4066Sahrens 	}
604fa9e4066Sahrens 
605fa9e4066Sahrens 	/*
606fa9e4066Sahrens 	 * Just take the live (open-context) values for checksum and compress.
607fa9e4066Sahrens 	 * Strictly speaking it's a future leak, but nothing bad happens if we
608fa9e4066Sahrens 	 * start using the new checksum or compress algorithm a little early.
609fa9e4066Sahrens 	 */
610fa9e4066Sahrens 	dnp->dn_checksum = dn->dn_checksum;
611fa9e4066Sahrens 	dnp->dn_compress = dn->dn_compress;
612fa9e4066Sahrens 
613fa9e4066Sahrens 	mutex_exit(&dn->dn_mtx);
614fa9e4066Sahrens 
615*0a586ceaSMark Shellenbaum 	if (kill_spill) {
616*0a586ceaSMark Shellenbaum 		dmu_buf_impl_t *spilldb;
617*0a586ceaSMark Shellenbaum 		(void) free_blocks(dn, &dn->dn_phys->dn_spill, 1, tx);
618*0a586ceaSMark Shellenbaum 		mutex_enter(&dn->dn_mtx);
619*0a586ceaSMark Shellenbaum 		dnp->dn_flags &= ~DNODE_FLAG_SPILL_BLKPTR;
620*0a586ceaSMark Shellenbaum 		mutex_exit(&dn->dn_mtx);
621*0a586ceaSMark Shellenbaum 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
622*0a586ceaSMark Shellenbaum 		spilldb = dbuf_find(dn, 0, DMU_SPILL_BLKID);
623*0a586ceaSMark Shellenbaum 		if (spilldb) {
624*0a586ceaSMark Shellenbaum 			spilldb->db_blkptr = NULL;
625*0a586ceaSMark Shellenbaum 			mutex_exit(&spilldb->db_mtx);
626*0a586ceaSMark Shellenbaum 		}
627*0a586ceaSMark Shellenbaum 		rw_exit(&dn->dn_struct_rwlock);
628*0a586ceaSMark Shellenbaum 	}
629*0a586ceaSMark Shellenbaum 
630fa9e4066Sahrens 	/* process all the "freed" ranges in the file */
631cdb0ab79Smaybee 	while (rp = avl_last(&dn->dn_ranges[txgoff])) {
632cdb0ab79Smaybee 		dnode_sync_free_range(dn, rp->fr_blkid, rp->fr_nblks, tx);
633cdb0ab79Smaybee 		/* grab the mutex so we don't race with dnode_block_freed() */
634cdb0ab79Smaybee 		mutex_enter(&dn->dn_mtx);
635cdb0ab79Smaybee 		avl_remove(&dn->dn_ranges[txgoff], rp);
636cdb0ab79Smaybee 		mutex_exit(&dn->dn_mtx);
637cdb0ab79Smaybee 		kmem_free(rp, sizeof (free_range_t));
638fa9e4066Sahrens 	}
6391934e92fSmaybee 
640fa9e4066Sahrens 	if (dn->dn_free_txg > 0 && dn->dn_free_txg <= tx->tx_txg) {
641c717a561Smaybee 		dnode_sync_free(dn, tx);
642c717a561Smaybee 		return;
643fa9e4066Sahrens 	}
644fa9e4066Sahrens 
645da03de99SMark Maybee 	if (dn->dn_next_nblkptr[txgoff]) {
646da03de99SMark Maybee 		/* this should only happen on a realloc */
647da03de99SMark Maybee 		ASSERT(dn->dn_allocated_txg == tx->tx_txg);
648da03de99SMark Maybee 		if (dn->dn_next_nblkptr[txgoff] > dnp->dn_nblkptr) {
649da03de99SMark Maybee 			/* zero the new blkptrs we are gaining */
650da03de99SMark Maybee 			bzero(dnp->dn_blkptr + dnp->dn_nblkptr,
651da03de99SMark Maybee 			    sizeof (blkptr_t) *
652da03de99SMark Maybee 			    (dn->dn_next_nblkptr[txgoff] - dnp->dn_nblkptr));
653da03de99SMark Maybee #ifdef ZFS_DEBUG
654da03de99SMark Maybee 		} else {
655da03de99SMark Maybee 			int i;
656da03de99SMark Maybee 			ASSERT(dn->dn_next_nblkptr[txgoff] < dnp->dn_nblkptr);
657da03de99SMark Maybee 			/* the blkptrs we are losing better be unallocated */
658da03de99SMark Maybee 			for (i = dn->dn_next_nblkptr[txgoff];
659da03de99SMark Maybee 			    i < dnp->dn_nblkptr; i++)
660da03de99SMark Maybee 				ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[i]));
661da03de99SMark Maybee #endif
662da03de99SMark Maybee 		}
663da03de99SMark Maybee 		mutex_enter(&dn->dn_mtx);
664da03de99SMark Maybee 		dnp->dn_nblkptr = dn->dn_next_nblkptr[txgoff];
665da03de99SMark Maybee 		dn->dn_next_nblkptr[txgoff] = 0;
666da03de99SMark Maybee 		mutex_exit(&dn->dn_mtx);
667da03de99SMark Maybee 	}
668da03de99SMark Maybee 
669fa9e4066Sahrens 	if (dn->dn_next_nlevels[txgoff]) {
670c717a561Smaybee 		dnode_increase_indirection(dn, tx);
671fa9e4066Sahrens 		dn->dn_next_nlevels[txgoff] = 0;
672fa9e4066Sahrens 	}
673fa9e4066Sahrens 
674c717a561Smaybee 	dbuf_sync_list(list, tx);
675fa9e4066Sahrens 
67614843421SMatthew Ahrens 	if (!DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
677c717a561Smaybee 		ASSERT3P(list_head(list), ==, NULL);
678c717a561Smaybee 		dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg);
679fa9e4066Sahrens 	}
680c717a561Smaybee 
681c717a561Smaybee 	/*
682c717a561Smaybee 	 * Although we have dropped our reference to the dnode, it
683c717a561Smaybee 	 * can't be evicted until its written, and we haven't yet
684c717a561Smaybee 	 * initiated the IO for the dnode's dbuf.
685c717a561Smaybee 	 */
686fa9e4066Sahrens }
687