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