xref: /onnv-gate/usr/src/uts/common/fs/zfs/dnode_sync.c (revision 12684:397e44ebb8a9)
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 /*
2212178SMark.Shellenbaum@Sun.COM  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23789Sahrens  */
24789Sahrens 
25789Sahrens #include <sys/zfs_context.h>
26789Sahrens #include <sys/dbuf.h>
27789Sahrens #include <sys/dnode.h>
28789Sahrens #include <sys/dmu.h>
29789Sahrens #include <sys/dmu_tx.h>
30789Sahrens #include <sys/dmu_objset.h>
31789Sahrens #include <sys/dsl_dataset.h>
32789Sahrens #include <sys/spa.h>
33789Sahrens 
34789Sahrens static void
dnode_increase_indirection(dnode_t * dn,dmu_tx_t * tx)35789Sahrens dnode_increase_indirection(dnode_t *dn, dmu_tx_t *tx)
36789Sahrens {
37789Sahrens 	dmu_buf_impl_t *db;
383547Smaybee 	int txgoff = tx->tx_txg & TXG_MASK;
393547Smaybee 	int nblkptr = dn->dn_phys->dn_nblkptr;
403547Smaybee 	int old_toplvl = dn->dn_phys->dn_nlevels - 1;
413547Smaybee 	int new_level = dn->dn_next_nlevels[txgoff];
42789Sahrens 	int i;
43789Sahrens 
443547Smaybee 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
453547Smaybee 
463547Smaybee 	/* this dnode can't be paged out because it's dirty */
47789Sahrens 	ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE);
48789Sahrens 	ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
493547Smaybee 	ASSERT(new_level > 1 && dn->dn_phys->dn_nlevels > 0);
50789Sahrens 
51789Sahrens 	db = dbuf_hold_level(dn, dn->dn_phys->dn_nlevels, 0, FTAG);
521544Seschrock 	ASSERT(db != NULL);
53789Sahrens 
543547Smaybee 	dn->dn_phys->dn_nlevels = new_level;
554312Sgw25295 	dprintf("os=%p obj=%llu, increase to %d\n", dn->dn_objset,
564312Sgw25295 	    dn->dn_object, dn->dn_phys->dn_nlevels);
57789Sahrens 
583547Smaybee 	/* check for existing blkptrs in the dnode */
593547Smaybee 	for (i = 0; i < nblkptr; i++)
603547Smaybee 		if (!BP_IS_HOLE(&dn->dn_phys->dn_blkptr[i]))
613547Smaybee 			break;
623547Smaybee 	if (i != nblkptr) {
633547Smaybee 		/* transfer dnode's block pointers to new indirect block */
643547Smaybee 		(void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED|DB_RF_HAVESTRUCT);
653547Smaybee 		ASSERT(db->db.db_data);
663547Smaybee 		ASSERT(arc_released(db->db_buf));
673547Smaybee 		ASSERT3U(sizeof (blkptr_t) * nblkptr, <=, db->db.db_size);
683547Smaybee 		bcopy(dn->dn_phys->dn_blkptr, db->db.db_data,
693547Smaybee 		    sizeof (blkptr_t) * nblkptr);
703547Smaybee 		arc_buf_freeze(db->db_buf);
713547Smaybee 	}
723547Smaybee 
73789Sahrens 	/* set dbuf's parent pointers to new indirect buf */
743547Smaybee 	for (i = 0; i < nblkptr; i++) {
753547Smaybee 		dmu_buf_impl_t *child = dbuf_find(dn, old_toplvl, i);
763547Smaybee 
77789Sahrens 		if (child == NULL)
78789Sahrens 			continue;
79*12684STom.Erickson@Sun.COM #ifdef	DEBUG
80*12684STom.Erickson@Sun.COM 		DB_DNODE_ENTER(child);
81*12684STom.Erickson@Sun.COM 		ASSERT3P(DB_DNODE(child), ==, dn);
82*12684STom.Erickson@Sun.COM 		DB_DNODE_EXIT(child);
83*12684STom.Erickson@Sun.COM #endif	/* DEBUG */
843547Smaybee 		if (child->db_parent && child->db_parent != dn->dn_dbuf) {
853547Smaybee 			ASSERT(child->db_parent->db_level == db->db_level);
863547Smaybee 			ASSERT(child->db_blkptr !=
873547Smaybee 			    &dn->dn_phys->dn_blkptr[child->db_blkid]);
88789Sahrens 			mutex_exit(&child->db_mtx);
89789Sahrens 			continue;
90789Sahrens 		}
913547Smaybee 		ASSERT(child->db_parent == NULL ||
923547Smaybee 		    child->db_parent == dn->dn_dbuf);
93789Sahrens 
943547Smaybee 		child->db_parent = db;
953547Smaybee 		dbuf_add_ref(db, child);
963547Smaybee 		if (db->db.db_data)
973547Smaybee 			child->db_blkptr = (blkptr_t *)db->db.db_data + i;
983547Smaybee 		else
993547Smaybee 			child->db_blkptr = NULL;
1003547Smaybee 		dprintf_dbuf_bp(child, child->db_blkptr,
1013547Smaybee 		    "changed db_blkptr to new indirect %s", "");
102789Sahrens 
103789Sahrens 		mutex_exit(&child->db_mtx);
104789Sahrens 	}
105789Sahrens 
1063547Smaybee 	bzero(dn->dn_phys->dn_blkptr, sizeof (blkptr_t) * nblkptr);
107789Sahrens 
1081544Seschrock 	dbuf_rele(db, FTAG);
1093547Smaybee 
1103547Smaybee 	rw_exit(&dn->dn_struct_rwlock);
111789Sahrens }
112789Sahrens 
1136992Smaybee static int
free_blocks(dnode_t * dn,blkptr_t * bp,int num,dmu_tx_t * tx)114789Sahrens free_blocks(dnode_t *dn, blkptr_t *bp, int num, dmu_tx_t *tx)
115789Sahrens {
1166992Smaybee 	dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
117789Sahrens 	uint64_t bytesfreed = 0;
1186992Smaybee 	int i, blocks_freed = 0;
119789Sahrens 
1206992Smaybee 	dprintf("ds=%p obj=%llx num=%d\n", ds, dn->dn_object, num);
121789Sahrens 
122789Sahrens 	for (i = 0; i < num; i++, bp++) {
123789Sahrens 		if (BP_IS_HOLE(bp))
124789Sahrens 			continue;
125789Sahrens 
12610922SJeff.Bonwick@Sun.COM 		bytesfreed += dsl_dataset_block_kill(ds, bp, tx, B_FALSE);
1272082Seschrock 		ASSERT3U(bytesfreed, <=, DN_USED_BYTES(dn->dn_phys));
1283547Smaybee 		bzero(bp, sizeof (blkptr_t));
1296992Smaybee 		blocks_freed += 1;
130789Sahrens 	}
131789Sahrens 	dnode_diduse_space(dn, -bytesfreed);
1326992Smaybee 	return (blocks_freed);
133789Sahrens }
134789Sahrens 
135873Sek110237 #ifdef ZFS_DEBUG
136789Sahrens static void
free_verify(dmu_buf_impl_t * db,uint64_t start,uint64_t end,dmu_tx_t * tx)137789Sahrens free_verify(dmu_buf_impl_t *db, uint64_t start, uint64_t end, dmu_tx_t *tx)
138789Sahrens {
139789Sahrens 	int off, num;
140789Sahrens 	int i, err, epbs;
141789Sahrens 	uint64_t txg = tx->tx_txg;
142*12684STom.Erickson@Sun.COM 	dnode_t *dn;
143789Sahrens 
144*12684STom.Erickson@Sun.COM 	DB_DNODE_ENTER(db);
145*12684STom.Erickson@Sun.COM 	dn = DB_DNODE(db);
146*12684STom.Erickson@Sun.COM 	epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
147789Sahrens 	off = start - (db->db_blkid * 1<<epbs);
148789Sahrens 	num = end - start + 1;
149789Sahrens 
150789Sahrens 	ASSERT3U(off, >=, 0);
151789Sahrens 	ASSERT3U(num, >=, 0);
152789Sahrens 	ASSERT3U(db->db_level, >, 0);
153*12684STom.Erickson@Sun.COM 	ASSERT3U(db->db.db_size, ==, 1 << dn->dn_phys->dn_indblkshift);
154789Sahrens 	ASSERT3U(off+num, <=, db->db.db_size >> SPA_BLKPTRSHIFT);
155789Sahrens 	ASSERT(db->db_blkptr != NULL);
156789Sahrens 
157789Sahrens 	for (i = off; i < off+num; i++) {
158789Sahrens 		uint64_t *buf;
1593547Smaybee 		dmu_buf_impl_t *child;
1603547Smaybee 		dbuf_dirty_record_t *dr;
161789Sahrens 		int j;
162789Sahrens 
163789Sahrens 		ASSERT(db->db_level == 1);
164789Sahrens 
165*12684STom.Erickson@Sun.COM 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
166*12684STom.Erickson@Sun.COM 		err = dbuf_hold_impl(dn, db->db_level-1,
1674312Sgw25295 		    (db->db_blkid << epbs) + i, TRUE, FTAG, &child);
168*12684STom.Erickson@Sun.COM 		rw_exit(&dn->dn_struct_rwlock);
169789Sahrens 		if (err == ENOENT)
170789Sahrens 			continue;
171789Sahrens 		ASSERT(err == 0);
172789Sahrens 		ASSERT(child->db_level == 0);
1733547Smaybee 		dr = child->db_last_dirty;
1743547Smaybee 		while (dr && dr->dr_txg > txg)
1753547Smaybee 			dr = dr->dr_next;
1763547Smaybee 		ASSERT(dr == NULL || dr->dr_txg == txg);
177789Sahrens 
1783547Smaybee 		/* data_old better be zeroed */
1793547Smaybee 		if (dr) {
1803547Smaybee 			buf = dr->dt.dl.dr_data->b_data;
181789Sahrens 			for (j = 0; j < child->db.db_size >> 3; j++) {
182789Sahrens 				if (buf[j] != 0) {
183789Sahrens 					panic("freed data not zero: "
184789Sahrens 					    "child=%p i=%d off=%d num=%d\n",
1857240Srh87107 					    (void *)child, i, off, num);
186789Sahrens 				}
187789Sahrens 			}
188789Sahrens 		}
189789Sahrens 
190789Sahrens 		/*
191789Sahrens 		 * db_data better be zeroed unless it's dirty in a
192789Sahrens 		 * future txg.
193789Sahrens 		 */
194789Sahrens 		mutex_enter(&child->db_mtx);
195789Sahrens 		buf = child->db.db_data;
196789Sahrens 		if (buf != NULL && child->db_state != DB_FILL &&
1973547Smaybee 		    child->db_last_dirty == NULL) {
198789Sahrens 			for (j = 0; j < child->db.db_size >> 3; j++) {
199789Sahrens 				if (buf[j] != 0) {
200789Sahrens 					panic("freed data not zero: "
201789Sahrens 					    "child=%p i=%d off=%d num=%d\n",
2027240Srh87107 					    (void *)child, i, off, num);
203789Sahrens 				}
204789Sahrens 			}
205789Sahrens 		}
206789Sahrens 		mutex_exit(&child->db_mtx);
207789Sahrens 
2081544Seschrock 		dbuf_rele(child, FTAG);
209789Sahrens 	}
210*12684STom.Erickson@Sun.COM 	DB_DNODE_EXIT(db);
211873Sek110237 }
212789Sahrens #endif
213789Sahrens 
2146992Smaybee #define	ALL -1
2156992Smaybee 
216789Sahrens static int
free_children(dmu_buf_impl_t * db,uint64_t blkid,uint64_t nblks,int trunc,dmu_tx_t * tx)217789Sahrens free_children(dmu_buf_impl_t *db, uint64_t blkid, uint64_t nblks, int trunc,
218789Sahrens     dmu_tx_t *tx)
219789Sahrens {
220*12684STom.Erickson@Sun.COM 	dnode_t *dn;
221789Sahrens 	blkptr_t *bp;
222789Sahrens 	dmu_buf_impl_t *subdb;
223789Sahrens 	uint64_t start, end, dbstart, dbend, i;
224789Sahrens 	int epbs, shift, err;
225789Sahrens 	int all = TRUE;
2266992Smaybee 	int blocks_freed = 0;
227789Sahrens 
2286992Smaybee 	/*
2296992Smaybee 	 * There is a small possibility that this block will not be cached:
2306992Smaybee 	 *   1 - if level > 1 and there are no children with level <= 1
2316992Smaybee 	 *   2 - if we didn't get a dirty hold (because this block had just
2326992Smaybee 	 *	 finished being written -- and so had no holds), and then this
2336992Smaybee 	 *	 block got evicted before we got here.
2346992Smaybee 	 */
2356992Smaybee 	if (db->db_state != DB_CACHED)
2366992Smaybee 		(void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
2376992Smaybee 
23812296SLin.Ling@Sun.COM 	dbuf_release_bp(db);
239789Sahrens 	bp = (blkptr_t *)db->db.db_data;
240789Sahrens 
241*12684STom.Erickson@Sun.COM 	DB_DNODE_ENTER(db);
242*12684STom.Erickson@Sun.COM 	dn = DB_DNODE(db);
243*12684STom.Erickson@Sun.COM 	epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
244789Sahrens 	shift = (db->db_level - 1) * epbs;
245789Sahrens 	dbstart = db->db_blkid << epbs;
246789Sahrens 	start = blkid >> shift;
247789Sahrens 	if (dbstart < start) {
248789Sahrens 		bp += start - dbstart;
249789Sahrens 		all = FALSE;
250789Sahrens 	} else {
251789Sahrens 		start = dbstart;
252789Sahrens 	}
253789Sahrens 	dbend = ((db->db_blkid + 1) << epbs) - 1;
254789Sahrens 	end = (blkid + nblks - 1) >> shift;
255789Sahrens 	if (dbend <= end)
256789Sahrens 		end = dbend;
257789Sahrens 	else if (all)
258789Sahrens 		all = trunc;
259789Sahrens 	ASSERT3U(start, <=, end);
260789Sahrens 
261789Sahrens 	if (db->db_level == 1) {
262873Sek110237 		FREE_VERIFY(db, start, end, tx);
2636992Smaybee 		blocks_freed = free_blocks(dn, bp, end-start+1, tx);
2643093Sahrens 		arc_buf_freeze(db->db_buf);
2656992Smaybee 		ASSERT(all || blocks_freed == 0 || db->db_last_dirty);
266*12684STom.Erickson@Sun.COM 		DB_DNODE_EXIT(db);
2676992Smaybee 		return (all ? ALL : blocks_freed);
268789Sahrens 	}
269789Sahrens 
270789Sahrens 	for (i = start; i <= end; i++, bp++) {
271789Sahrens 		if (BP_IS_HOLE(bp))
272789Sahrens 			continue;
273789Sahrens 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
274789Sahrens 		err = dbuf_hold_impl(dn, db->db_level-1, i, TRUE, FTAG, &subdb);
275789Sahrens 		ASSERT3U(err, ==, 0);
276789Sahrens 		rw_exit(&dn->dn_struct_rwlock);
277789Sahrens 
2786992Smaybee 		if (free_children(subdb, blkid, nblks, trunc, tx) == ALL) {
279789Sahrens 			ASSERT3P(subdb->db_blkptr, ==, bp);
2806992Smaybee 			blocks_freed += free_blocks(dn, bp, 1, tx);
2811163Smaybee 		} else {
2821163Smaybee 			all = FALSE;
283789Sahrens 		}
2841544Seschrock 		dbuf_rele(subdb, FTAG);
285789Sahrens 	}
286*12684STom.Erickson@Sun.COM 	DB_DNODE_EXIT(db);
2873093Sahrens 	arc_buf_freeze(db->db_buf);
288789Sahrens #ifdef ZFS_DEBUG
289789Sahrens 	bp -= (end-start)+1;
290789Sahrens 	for (i = start; i <= end; i++, bp++) {
291789Sahrens 		if (i == start && blkid != 0)
292789Sahrens 			continue;
293789Sahrens 		else if (i == end && !trunc)
294789Sahrens 			continue;
295789Sahrens 		ASSERT3U(bp->blk_birth, ==, 0);
296789Sahrens 	}
297789Sahrens #endif
2986992Smaybee 	ASSERT(all || blocks_freed == 0 || db->db_last_dirty);
2996992Smaybee 	return (all ? ALL : blocks_freed);
300789Sahrens }
301789Sahrens 
302789Sahrens /*
303789Sahrens  * free_range: Traverse the indicated range of the provided file
304789Sahrens  * and "free" all the blocks contained there.
305789Sahrens  */
306789Sahrens static void
dnode_sync_free_range(dnode_t * dn,uint64_t blkid,uint64_t nblks,dmu_tx_t * tx)307789Sahrens dnode_sync_free_range(dnode_t *dn, uint64_t blkid, uint64_t nblks, dmu_tx_t *tx)
308789Sahrens {
309789Sahrens 	blkptr_t *bp = dn->dn_phys->dn_blkptr;
310789Sahrens 	dmu_buf_impl_t *db;
311789Sahrens 	int trunc, start, end, shift, i, err;
312789Sahrens 	int dnlevel = dn->dn_phys->dn_nlevels;
313789Sahrens 
314789Sahrens 	if (blkid > dn->dn_phys->dn_maxblkid)
315789Sahrens 		return;
316789Sahrens 
317789Sahrens 	ASSERT(dn->dn_phys->dn_maxblkid < UINT64_MAX);
318789Sahrens 	trunc = blkid + nblks > dn->dn_phys->dn_maxblkid;
319789Sahrens 	if (trunc)
320789Sahrens 		nblks = dn->dn_phys->dn_maxblkid - blkid + 1;
321789Sahrens 
322789Sahrens 	/* There are no indirect blocks in the object */
323789Sahrens 	if (dnlevel == 1) {
324789Sahrens 		if (blkid >= dn->dn_phys->dn_nblkptr) {
325789Sahrens 			/* this range was never made persistent */
326789Sahrens 			return;
327789Sahrens 		}
328789Sahrens 		ASSERT3U(blkid + nblks, <=, dn->dn_phys->dn_nblkptr);
3296992Smaybee 		(void) free_blocks(dn, bp + blkid, nblks, tx);
330789Sahrens 		if (trunc) {
331789Sahrens 			uint64_t off = (dn->dn_phys->dn_maxblkid + 1) *
332789Sahrens 			    (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT);
333789Sahrens 			dn->dn_phys->dn_maxblkid = (blkid ? blkid - 1 : 0);
334789Sahrens 			ASSERT(off < dn->dn_phys->dn_maxblkid ||
335789Sahrens 			    dn->dn_phys->dn_maxblkid == 0 ||
3366992Smaybee 			    dnode_next_offset(dn, 0, &off, 1, 1, 0) != 0);
337789Sahrens 		}
338789Sahrens 		return;
339789Sahrens 	}
340789Sahrens 
341789Sahrens 	shift = (dnlevel - 1) * (dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT);
342789Sahrens 	start = blkid >> shift;
343789Sahrens 	ASSERT(start < dn->dn_phys->dn_nblkptr);
344789Sahrens 	end = (blkid + nblks - 1) >> shift;
345789Sahrens 	bp += start;
346789Sahrens 	for (i = start; i <= end; i++, bp++) {
347789Sahrens 		if (BP_IS_HOLE(bp))
348789Sahrens 			continue;
349789Sahrens 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
350789Sahrens 		err = dbuf_hold_impl(dn, dnlevel-1, i, TRUE, FTAG, &db);
351789Sahrens 		ASSERT3U(err, ==, 0);
352789Sahrens 		rw_exit(&dn->dn_struct_rwlock);
353789Sahrens 
3546992Smaybee 		if (free_children(db, blkid, nblks, trunc, tx) == ALL) {
355789Sahrens 			ASSERT3P(db->db_blkptr, ==, bp);
3566992Smaybee 			(void) free_blocks(dn, bp, 1, tx);
357789Sahrens 		}
3581544Seschrock 		dbuf_rele(db, FTAG);
359789Sahrens 	}
360789Sahrens 	if (trunc) {
361789Sahrens 		uint64_t off = (dn->dn_phys->dn_maxblkid + 1) *
362789Sahrens 		    (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT);
363789Sahrens 		dn->dn_phys->dn_maxblkid = (blkid ? blkid - 1 : 0);
364789Sahrens 		ASSERT(off < dn->dn_phys->dn_maxblkid ||
365789Sahrens 		    dn->dn_phys->dn_maxblkid == 0 ||
3666992Smaybee 		    dnode_next_offset(dn, 0, &off, 1, 1, 0) != 0);
367789Sahrens 	}
368789Sahrens }
369789Sahrens 
3701544Seschrock /*
3711544Seschrock  * Try to kick all the dnodes dbufs out of the cache...
3721544Seschrock  */
3734944Smaybee void
dnode_evict_dbufs(dnode_t * dn)3744944Smaybee dnode_evict_dbufs(dnode_t *dn)
3751544Seschrock {
3761596Sahrens 	int progress;
3771596Sahrens 	int pass = 0;
3781596Sahrens 
3791596Sahrens 	do {
3801602Smaybee 		dmu_buf_impl_t *db, marker;
3811596Sahrens 		int evicting = FALSE;
3821544Seschrock 
3831596Sahrens 		progress = FALSE;
3841596Sahrens 		mutex_enter(&dn->dn_dbufs_mtx);
3851602Smaybee 		list_insert_tail(&dn->dn_dbufs, &marker);
3861602Smaybee 		db = list_head(&dn->dn_dbufs);
3871602Smaybee 		for (; db != &marker; db = list_head(&dn->dn_dbufs)) {
3881602Smaybee 			list_remove(&dn->dn_dbufs, db);
3891602Smaybee 			list_insert_tail(&dn->dn_dbufs, db);
390*12684STom.Erickson@Sun.COM #ifdef	DEBUG
391*12684STom.Erickson@Sun.COM 			DB_DNODE_ENTER(db);
392*12684STom.Erickson@Sun.COM 			ASSERT3P(DB_DNODE(db), ==, dn);
393*12684STom.Erickson@Sun.COM 			DB_DNODE_EXIT(db);
394*12684STom.Erickson@Sun.COM #endif	/* DEBUG */
3951596Sahrens 
3961544Seschrock 			mutex_enter(&db->db_mtx);
3971596Sahrens 			if (db->db_state == DB_EVICTING) {
3981596Sahrens 				progress = TRUE;
3991596Sahrens 				evicting = TRUE;
4001596Sahrens 				mutex_exit(&db->db_mtx);
4011596Sahrens 			} else if (refcount_is_zero(&db->db_holds)) {
4021596Sahrens 				progress = TRUE;
4031596Sahrens 				dbuf_clear(db); /* exits db_mtx for us */
4041596Sahrens 			} else {
4051596Sahrens 				mutex_exit(&db->db_mtx);
4061596Sahrens 			}
4071596Sahrens 
4081544Seschrock 		}
4091602Smaybee 		list_remove(&dn->dn_dbufs, &marker);
4101596Sahrens 		/*
4111596Sahrens 		 * NB: we need to drop dn_dbufs_mtx between passes so
4121596Sahrens 		 * that any DB_EVICTING dbufs can make progress.
4131596Sahrens 		 * Ideally, we would have some cv we could wait on, but
4141596Sahrens 		 * since we don't, just wait a bit to give the other
4151596Sahrens 		 * thread a chance to run.
4161596Sahrens 		 */
4171596Sahrens 		mutex_exit(&dn->dn_dbufs_mtx);
4181596Sahrens 		if (evicting)
4191596Sahrens 			delay(1);
4201596Sahrens 		pass++;
4211596Sahrens 		ASSERT(pass < 100); /* sanity check */
4221596Sahrens 	} while (progress);
4231596Sahrens 
4241544Seschrock 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
4251544Seschrock 	if (dn->dn_bonus && refcount_is_zero(&dn->dn_bonus->db_holds)) {
4261544Seschrock 		mutex_enter(&dn->dn_bonus->db_mtx);
4271544Seschrock 		dbuf_evict(dn->dn_bonus);
4281544Seschrock 		dn->dn_bonus = NULL;
4291544Seschrock 	}
4301544Seschrock 	rw_exit(&dn->dn_struct_rwlock);
4311544Seschrock }
4321544Seschrock 
4333547Smaybee static void
dnode_undirty_dbufs(list_t * list)4343547Smaybee dnode_undirty_dbufs(list_t *list)
4353547Smaybee {
4363547Smaybee 	dbuf_dirty_record_t *dr;
4373547Smaybee 
4383547Smaybee 	while (dr = list_head(list)) {
4393547Smaybee 		dmu_buf_impl_t *db = dr->dr_dbuf;
4403547Smaybee 		uint64_t txg = dr->dr_txg;
4413547Smaybee 
44210922SJeff.Bonwick@Sun.COM 		if (db->db_level != 0)
44310922SJeff.Bonwick@Sun.COM 			dnode_undirty_dbufs(&dr->dt.di.dr_children);
44410922SJeff.Bonwick@Sun.COM 
4453547Smaybee 		mutex_enter(&db->db_mtx);
4463547Smaybee 		/* XXX - use dbuf_undirty()? */
4473547Smaybee 		list_remove(list, dr);
4483547Smaybee 		ASSERT(db->db_last_dirty == dr);
4493547Smaybee 		db->db_last_dirty = NULL;
4503547Smaybee 		db->db_dirtycnt -= 1;
4513547Smaybee 		if (db->db_level == 0) {
45211935SMark.Shellenbaum@Sun.COM 			ASSERT(db->db_blkid == DMU_BONUS_BLKID ||
4533547Smaybee 			    dr->dt.dl.dr_data == db->db_buf);
4543547Smaybee 			dbuf_unoverride(dr);
4553547Smaybee 		}
4563547Smaybee 		kmem_free(dr, sizeof (dbuf_dirty_record_t));
45710922SJeff.Bonwick@Sun.COM 		dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg);
4583547Smaybee 	}
4593547Smaybee }
4603547Smaybee 
4613547Smaybee static void
dnode_sync_free(dnode_t * dn,dmu_tx_t * tx)462789Sahrens dnode_sync_free(dnode_t *dn, dmu_tx_t *tx)
463789Sahrens {
464789Sahrens 	int txgoff = tx->tx_txg & TXG_MASK;
465789Sahrens 
466789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
467789Sahrens 
4686992Smaybee 	/*
4696992Smaybee 	 * Our contents should have been freed in dnode_sync() by the
4706992Smaybee 	 * free range record inserted by the caller of dnode_free().
4716992Smaybee 	 */
4726992Smaybee 	ASSERT3U(DN_USED_BYTES(dn->dn_phys), ==, 0);
4736992Smaybee 	ASSERT(BP_IS_HOLE(dn->dn_phys->dn_blkptr));
4746992Smaybee 
4753547Smaybee 	dnode_undirty_dbufs(&dn->dn_dirty_records[txgoff]);
4764944Smaybee 	dnode_evict_dbufs(dn);
4771544Seschrock 	ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL);
4781544Seschrock 
4791544Seschrock 	/*
4801544Seschrock 	 * XXX - It would be nice to assert this, but we may still
4811544Seschrock 	 * have residual holds from async evictions from the arc...
4821544Seschrock 	 *
4833444Sek110237 	 * zfs_obj_to_path() also depends on this being
4843444Sek110237 	 * commented out.
4853444Sek110237 	 *
4861544Seschrock 	 * ASSERT3U(refcount_count(&dn->dn_holds), ==, 1);
4871544Seschrock 	 */
488789Sahrens 
489789Sahrens 	/* Undirty next bits */
490789Sahrens 	dn->dn_next_nlevels[txgoff] = 0;
491789Sahrens 	dn->dn_next_indblkshift[txgoff] = 0;
4921596Sahrens 	dn->dn_next_blksz[txgoff] = 0;
493789Sahrens 
494789Sahrens 	/* ASSERT(blkptrs are zero); */
495789Sahrens 	ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE);
496789Sahrens 	ASSERT(dn->dn_type != DMU_OT_NONE);
497789Sahrens 
498789Sahrens 	ASSERT(dn->dn_free_txg > 0);
499789Sahrens 	if (dn->dn_allocated_txg != dn->dn_free_txg)
500789Sahrens 		dbuf_will_dirty(dn->dn_dbuf, tx);
501789Sahrens 	bzero(dn->dn_phys, sizeof (dnode_phys_t));
502789Sahrens 
503789Sahrens 	mutex_enter(&dn->dn_mtx);
504789Sahrens 	dn->dn_type = DMU_OT_NONE;
505789Sahrens 	dn->dn_maxblkid = 0;
506789Sahrens 	dn->dn_allocated_txg = 0;
5074480Sgw25295 	dn->dn_free_txg = 0;
50811935SMark.Shellenbaum@Sun.COM 	dn->dn_have_spill = B_FALSE;
509789Sahrens 	mutex_exit(&dn->dn_mtx);
510789Sahrens 
5111544Seschrock 	ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
512789Sahrens 
513789Sahrens 	dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg);
514789Sahrens 	/*
515789Sahrens 	 * Now that we've released our hold, the dnode may
516789Sahrens 	 * be evicted, so we musn't access it.
517789Sahrens 	 */
518789Sahrens }
519789Sahrens 
520789Sahrens /*
5213547Smaybee  * Write out the dnode's dirty buffers.
522789Sahrens  */
5233547Smaybee void
dnode_sync(dnode_t * dn,dmu_tx_t * tx)5243547Smaybee dnode_sync(dnode_t *dn, dmu_tx_t *tx)
525789Sahrens {
526789Sahrens 	free_range_t *rp;
5273547Smaybee 	dnode_phys_t *dnp = dn->dn_phys;
528789Sahrens 	int txgoff = tx->tx_txg & TXG_MASK;
5293547Smaybee 	list_t *list = &dn->dn_dirty_records[txgoff];
5309396SMatthew.Ahrens@Sun.COM 	static const dnode_phys_t zerodn = { 0 };
53111935SMark.Shellenbaum@Sun.COM 	boolean_t kill_spill = B_FALSE;
532789Sahrens 
533789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
534789Sahrens 	ASSERT(dnp->dn_type != DMU_OT_NONE || dn->dn_allocated_txg);
5359396SMatthew.Ahrens@Sun.COM 	ASSERT(dnp->dn_type != DMU_OT_NONE ||
5369396SMatthew.Ahrens@Sun.COM 	    bcmp(dnp, &zerodn, DNODE_SIZE) == 0);
537873Sek110237 	DNODE_VERIFY(dn);
5381596Sahrens 
5393547Smaybee 	ASSERT(dn->dn_dbuf == NULL || arc_released(dn->dn_dbuf->db_buf));
540789Sahrens 
5419396SMatthew.Ahrens@Sun.COM 	if (dmu_objset_userused_enabled(dn->dn_objset) &&
5429396SMatthew.Ahrens@Sun.COM 	    !DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
54311935SMark.Shellenbaum@Sun.COM 		mutex_enter(&dn->dn_mtx);
54411935SMark.Shellenbaum@Sun.COM 		dn->dn_oldused = DN_USED_BYTES(dn->dn_phys);
54511935SMark.Shellenbaum@Sun.COM 		dn->dn_oldflags = dn->dn_phys->dn_flags;
5469396SMatthew.Ahrens@Sun.COM 		dn->dn_phys->dn_flags |= DNODE_FLAG_USERUSED_ACCOUNTED;
54711935SMark.Shellenbaum@Sun.COM 		mutex_exit(&dn->dn_mtx);
54812178SMark.Shellenbaum@Sun.COM 		dmu_objset_userquota_get_ids(dn, B_FALSE, tx);
5499396SMatthew.Ahrens@Sun.COM 	} else {
5509396SMatthew.Ahrens@Sun.COM 		/* Once we account for it, we should always account for it. */
5519396SMatthew.Ahrens@Sun.COM 		ASSERT(!(dn->dn_phys->dn_flags &
5529396SMatthew.Ahrens@Sun.COM 		    DNODE_FLAG_USERUSED_ACCOUNTED));
5539396SMatthew.Ahrens@Sun.COM 	}
5549396SMatthew.Ahrens@Sun.COM 
555789Sahrens 	mutex_enter(&dn->dn_mtx);
556789Sahrens 	if (dn->dn_allocated_txg == tx->tx_txg) {
557789Sahrens 		/* The dnode is newly allocated or reallocated */
558789Sahrens 		if (dnp->dn_type == DMU_OT_NONE) {
559789Sahrens 			/* this is a first alloc, not a realloc */
560789Sahrens 			dnp->dn_nlevels = 1;
5618644SMark.Maybee@Sun.COM 			dnp->dn_nblkptr = dn->dn_nblkptr;
562789Sahrens 		}
563789Sahrens 
564789Sahrens 		dnp->dn_type = dn->dn_type;
565789Sahrens 		dnp->dn_bonustype = dn->dn_bonustype;
566789Sahrens 		dnp->dn_bonuslen = dn->dn_bonuslen;
567789Sahrens 	}
568789Sahrens 
5693547Smaybee 	ASSERT(dnp->dn_nlevels > 1 ||
5701599Sahrens 	    BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
5711599Sahrens 	    BP_GET_LSIZE(&dnp->dn_blkptr[0]) ==
5721599Sahrens 	    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
5731599Sahrens 
5741596Sahrens 	if (dn->dn_next_blksz[txgoff]) {
5751596Sahrens 		ASSERT(P2PHASE(dn->dn_next_blksz[txgoff],
576789Sahrens 		    SPA_MINBLOCKSIZE) == 0);
5771599Sahrens 		ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
5786992Smaybee 		    dn->dn_maxblkid == 0 || list_head(list) != NULL ||
57912178SMark.Shellenbaum@Sun.COM 		    avl_last(&dn->dn_ranges[txgoff]) ||
5801600Sahrens 		    dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT ==
5811600Sahrens 		    dnp->dn_datablkszsec);
582789Sahrens 		dnp->dn_datablkszsec =
5831596Sahrens 		    dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT;
5841596Sahrens 		dn->dn_next_blksz[txgoff] = 0;
585789Sahrens 	}
586789Sahrens 
5874944Smaybee 	if (dn->dn_next_bonuslen[txgoff]) {
5884944Smaybee 		if (dn->dn_next_bonuslen[txgoff] == DN_ZERO_BONUSLEN)
5894944Smaybee 			dnp->dn_bonuslen = 0;
5904944Smaybee 		else
5914944Smaybee 			dnp->dn_bonuslen = dn->dn_next_bonuslen[txgoff];
5924944Smaybee 		ASSERT(dnp->dn_bonuslen <= DN_MAX_BONUSLEN);
5934944Smaybee 		dn->dn_next_bonuslen[txgoff] = 0;
5944944Smaybee 	}
5954944Smaybee 
59611935SMark.Shellenbaum@Sun.COM 	if (dn->dn_next_bonustype[txgoff]) {
59711935SMark.Shellenbaum@Sun.COM 		ASSERT(dn->dn_next_bonustype[txgoff] < DMU_OT_NUMTYPES);
59811935SMark.Shellenbaum@Sun.COM 		dnp->dn_bonustype = dn->dn_next_bonustype[txgoff];
59911935SMark.Shellenbaum@Sun.COM 		dn->dn_next_bonustype[txgoff] = 0;
60011935SMark.Shellenbaum@Sun.COM 	}
60111935SMark.Shellenbaum@Sun.COM 
60211935SMark.Shellenbaum@Sun.COM 	/*
60311935SMark.Shellenbaum@Sun.COM 	 * We will either remove a spill block when a file is being removed
60411935SMark.Shellenbaum@Sun.COM 	 * or we have been asked to remove it.
60511935SMark.Shellenbaum@Sun.COM 	 */
60611935SMark.Shellenbaum@Sun.COM 	if (dn->dn_rm_spillblk[txgoff] ||
60711935SMark.Shellenbaum@Sun.COM 	    ((dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) &&
60811935SMark.Shellenbaum@Sun.COM 	    dn->dn_free_txg > 0 && dn->dn_free_txg <= tx->tx_txg)) {
60911935SMark.Shellenbaum@Sun.COM 		if ((dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR))
61011935SMark.Shellenbaum@Sun.COM 			kill_spill = B_TRUE;
61111935SMark.Shellenbaum@Sun.COM 		dn->dn_rm_spillblk[txgoff] = 0;
61211935SMark.Shellenbaum@Sun.COM 	}
61311935SMark.Shellenbaum@Sun.COM 
614789Sahrens 	if (dn->dn_next_indblkshift[txgoff]) {
615789Sahrens 		ASSERT(dnp->dn_nlevels == 1);
616789Sahrens 		dnp->dn_indblkshift = dn->dn_next_indblkshift[txgoff];
617789Sahrens 		dn->dn_next_indblkshift[txgoff] = 0;
618789Sahrens 	}
619789Sahrens 
620789Sahrens 	/*
621789Sahrens 	 * Just take the live (open-context) values for checksum and compress.
622789Sahrens 	 * Strictly speaking it's a future leak, but nothing bad happens if we
623789Sahrens 	 * start using the new checksum or compress algorithm a little early.
624789Sahrens 	 */
625789Sahrens 	dnp->dn_checksum = dn->dn_checksum;
626789Sahrens 	dnp->dn_compress = dn->dn_compress;
627789Sahrens 
628789Sahrens 	mutex_exit(&dn->dn_mtx);
629789Sahrens 
63011935SMark.Shellenbaum@Sun.COM 	if (kill_spill) {
63111935SMark.Shellenbaum@Sun.COM 		(void) free_blocks(dn, &dn->dn_phys->dn_spill, 1, tx);
63211935SMark.Shellenbaum@Sun.COM 		mutex_enter(&dn->dn_mtx);
63311935SMark.Shellenbaum@Sun.COM 		dnp->dn_flags &= ~DNODE_FLAG_SPILL_BLKPTR;
63411935SMark.Shellenbaum@Sun.COM 		mutex_exit(&dn->dn_mtx);
63511935SMark.Shellenbaum@Sun.COM 	}
63611935SMark.Shellenbaum@Sun.COM 
637789Sahrens 	/* process all the "freed" ranges in the file */
6386992Smaybee 	while (rp = avl_last(&dn->dn_ranges[txgoff])) {
6396992Smaybee 		dnode_sync_free_range(dn, rp->fr_blkid, rp->fr_nblks, tx);
6406992Smaybee 		/* grab the mutex so we don't race with dnode_block_freed() */
6416992Smaybee 		mutex_enter(&dn->dn_mtx);
6426992Smaybee 		avl_remove(&dn->dn_ranges[txgoff], rp);
6436992Smaybee 		mutex_exit(&dn->dn_mtx);
6446992Smaybee 		kmem_free(rp, sizeof (free_range_t));
645789Sahrens 	}
6464944Smaybee 
647789Sahrens 	if (dn->dn_free_txg > 0 && dn->dn_free_txg <= tx->tx_txg) {
6483547Smaybee 		dnode_sync_free(dn, tx);
6493547Smaybee 		return;
650789Sahrens 	}
651789Sahrens 
6528644SMark.Maybee@Sun.COM 	if (dn->dn_next_nblkptr[txgoff]) {
6538644SMark.Maybee@Sun.COM 		/* this should only happen on a realloc */
6548644SMark.Maybee@Sun.COM 		ASSERT(dn->dn_allocated_txg == tx->tx_txg);
6558644SMark.Maybee@Sun.COM 		if (dn->dn_next_nblkptr[txgoff] > dnp->dn_nblkptr) {
6568644SMark.Maybee@Sun.COM 			/* zero the new blkptrs we are gaining */
6578644SMark.Maybee@Sun.COM 			bzero(dnp->dn_blkptr + dnp->dn_nblkptr,
6588644SMark.Maybee@Sun.COM 			    sizeof (blkptr_t) *
6598644SMark.Maybee@Sun.COM 			    (dn->dn_next_nblkptr[txgoff] - dnp->dn_nblkptr));
6608644SMark.Maybee@Sun.COM #ifdef ZFS_DEBUG
6618644SMark.Maybee@Sun.COM 		} else {
6628644SMark.Maybee@Sun.COM 			int i;
6638644SMark.Maybee@Sun.COM 			ASSERT(dn->dn_next_nblkptr[txgoff] < dnp->dn_nblkptr);
6648644SMark.Maybee@Sun.COM 			/* the blkptrs we are losing better be unallocated */
6658644SMark.Maybee@Sun.COM 			for (i = dn->dn_next_nblkptr[txgoff];
6668644SMark.Maybee@Sun.COM 			    i < dnp->dn_nblkptr; i++)
6678644SMark.Maybee@Sun.COM 				ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[i]));
6688644SMark.Maybee@Sun.COM #endif
6698644SMark.Maybee@Sun.COM 		}
6708644SMark.Maybee@Sun.COM 		mutex_enter(&dn->dn_mtx);
6718644SMark.Maybee@Sun.COM 		dnp->dn_nblkptr = dn->dn_next_nblkptr[txgoff];
6728644SMark.Maybee@Sun.COM 		dn->dn_next_nblkptr[txgoff] = 0;
6738644SMark.Maybee@Sun.COM 		mutex_exit(&dn->dn_mtx);
6748644SMark.Maybee@Sun.COM 	}
6758644SMark.Maybee@Sun.COM 
676789Sahrens 	if (dn->dn_next_nlevels[txgoff]) {
6773547Smaybee 		dnode_increase_indirection(dn, tx);
678789Sahrens 		dn->dn_next_nlevels[txgoff] = 0;
679789Sahrens 	}
680789Sahrens 
6813547Smaybee 	dbuf_sync_list(list, tx);
682789Sahrens 
6839396SMatthew.Ahrens@Sun.COM 	if (!DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
6843547Smaybee 		ASSERT3P(list_head(list), ==, NULL);
6853547Smaybee 		dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg);
6863547Smaybee 	}
6871599Sahrens 
6883547Smaybee 	/*
6893547Smaybee 	 * Although we have dropped our reference to the dnode, it
6903547Smaybee 	 * can't be evicted until its written, and we haven't yet
6913547Smaybee 	 * initiated the IO for the dnode's dbuf.
6923547Smaybee 	 */
693789Sahrens }
694