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   1491    ahrens  * Common Development and Distribution License (the "License").
      6   1491    ahrens  * 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   8582   Brendan  * 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_impl.h>
     31    789    ahrens #include <sys/dmu_tx.h>
     32    789    ahrens #include <sys/dmu_objset.h>
     33    789    ahrens #include <sys/dsl_dir.h>
     34    789    ahrens #include <sys/dsl_dataset.h>
     35    789    ahrens #include <sys/spa.h>
     36    789    ahrens #include <sys/zio.h>
     37    789    ahrens #include <sys/dmu_zfetch.h>
     38    789    ahrens 
     39    789    ahrens static int free_range_compar(const void *node1, const void *node2);
     40    789    ahrens 
     41    789    ahrens static kmem_cache_t *dnode_cache;
     42    789    ahrens 
     43    789    ahrens static dnode_phys_t dnode_phys_zero;
     44    789    ahrens 
     45    789    ahrens int zfs_default_bs = SPA_MINBLOCKSHIFT;
     46    789    ahrens int zfs_default_ibs = DN_MAX_INDBLKSHIFT;
     47    789    ahrens 
     48    789    ahrens /* ARGSUSED */
     49    789    ahrens static int
     50    789    ahrens dnode_cons(void *arg, void *unused, int kmflag)
     51    789    ahrens {
     52    789    ahrens 	int i;
     53    789    ahrens 	dnode_t *dn = arg;
     54    789    ahrens 	bzero(dn, sizeof (dnode_t));
     55    789    ahrens 
     56    789    ahrens 	rw_init(&dn->dn_struct_rwlock, NULL, RW_DEFAULT, NULL);
     57    789    ahrens 	mutex_init(&dn->dn_mtx, NULL, MUTEX_DEFAULT, NULL);
     58    789    ahrens 	mutex_init(&dn->dn_dbufs_mtx, NULL, MUTEX_DEFAULT, NULL);
     59   8214   Ricardo 	cv_init(&dn->dn_notxholds, NULL, CV_DEFAULT, NULL);
     60   8214   Ricardo 
     61    789    ahrens 	refcount_create(&dn->dn_holds);
     62    789    ahrens 	refcount_create(&dn->dn_tx_holds);
     63    789    ahrens 
     64    789    ahrens 	for (i = 0; i < TXG_SIZE; i++) {
     65    789    ahrens 		avl_create(&dn->dn_ranges[i], free_range_compar,
     66    789    ahrens 		    sizeof (free_range_t),
     67    789    ahrens 		    offsetof(struct free_range, fr_node));
     68   3547    maybee 		list_create(&dn->dn_dirty_records[i],
     69   3547    maybee 		    sizeof (dbuf_dirty_record_t),
     70   3547    maybee 		    offsetof(dbuf_dirty_record_t, dr_dirty_node));
     71    789    ahrens 	}
     72    789    ahrens 
     73    789    ahrens 	list_create(&dn->dn_dbufs, sizeof (dmu_buf_impl_t),
     74    789    ahrens 	    offsetof(dmu_buf_impl_t, db_link));
     75    789    ahrens 
     76    789    ahrens 	return (0);
     77    789    ahrens }
     78    789    ahrens 
     79    789    ahrens /* ARGSUSED */
     80    789    ahrens static void
     81    789    ahrens dnode_dest(void *arg, void *unused)
     82    789    ahrens {
     83    789    ahrens 	int i;
     84    789    ahrens 	dnode_t *dn = arg;
     85    789    ahrens 
     86    789    ahrens 	rw_destroy(&dn->dn_struct_rwlock);
     87    789    ahrens 	mutex_destroy(&dn->dn_mtx);
     88    789    ahrens 	mutex_destroy(&dn->dn_dbufs_mtx);
     89   8214   Ricardo 	cv_destroy(&dn->dn_notxholds);
     90    789    ahrens 	refcount_destroy(&dn->dn_holds);
     91    789    ahrens 	refcount_destroy(&dn->dn_tx_holds);
     92    789    ahrens 
     93    789    ahrens 	for (i = 0; i < TXG_SIZE; i++) {
     94    789    ahrens 		avl_destroy(&dn->dn_ranges[i]);
     95   3547    maybee 		list_destroy(&dn->dn_dirty_records[i]);
     96    789    ahrens 	}
     97    789    ahrens 
     98    789    ahrens 	list_destroy(&dn->dn_dbufs);
     99    789    ahrens }
    100    789    ahrens 
    101    789    ahrens void
    102    789    ahrens dnode_init(void)
    103    789    ahrens {
    104    789    ahrens 	dnode_cache = kmem_cache_create("dnode_t",
    105    789    ahrens 	    sizeof (dnode_t),
    106    789    ahrens 	    0, dnode_cons, dnode_dest, NULL, NULL, NULL, 0);
    107    789    ahrens }
    108    789    ahrens 
    109    789    ahrens void
    110    789    ahrens dnode_fini(void)
    111    789    ahrens {
    112    789    ahrens 	kmem_cache_destroy(dnode_cache);
    113    789    ahrens }
    114    789    ahrens 
    115    789    ahrens 
    116    873  ek110237 #ifdef ZFS_DEBUG
    117    789    ahrens void
    118    789    ahrens dnode_verify(dnode_t *dn)
    119    789    ahrens {
    120    789    ahrens 	int drop_struct_lock = FALSE;
    121    789    ahrens 
    122    789    ahrens 	ASSERT(dn->dn_phys);
    123    789    ahrens 	ASSERT(dn->dn_objset);
    124    789    ahrens 
    125    789    ahrens 	ASSERT(dn->dn_phys->dn_type < DMU_OT_NUMTYPES);
    126    789    ahrens 
    127    789    ahrens 	if (!(zfs_flags & ZFS_DEBUG_DNODE_VERIFY))
    128    789    ahrens 		return;
    129    789    ahrens 
    130    789    ahrens 	if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
    131    789    ahrens 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
    132    789    ahrens 		drop_struct_lock = TRUE;
    133    789    ahrens 	}
    134    789    ahrens 	if (dn->dn_phys->dn_type != DMU_OT_NONE || dn->dn_allocated_txg != 0) {
    135    789    ahrens 		int i;
    136    789    ahrens 		ASSERT3U(dn->dn_indblkshift, >=, 0);
    137    789    ahrens 		ASSERT3U(dn->dn_indblkshift, <=, SPA_MAXBLOCKSHIFT);
    138    789    ahrens 		if (dn->dn_datablkshift) {
    139    789    ahrens 			ASSERT3U(dn->dn_datablkshift, >=, SPA_MINBLOCKSHIFT);
    140    789    ahrens 			ASSERT3U(dn->dn_datablkshift, <=, SPA_MAXBLOCKSHIFT);
    141    789    ahrens 			ASSERT3U(1<<dn->dn_datablkshift, ==, dn->dn_datablksz);
    142    789    ahrens 		}
    143    789    ahrens 		ASSERT3U(dn->dn_nlevels, <=, 30);
    144    789    ahrens 		ASSERT3U(dn->dn_type, <=, DMU_OT_NUMTYPES);
    145    789    ahrens 		ASSERT3U(dn->dn_nblkptr, >=, 1);
    146    789    ahrens 		ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
    147    789    ahrens 		ASSERT3U(dn->dn_bonuslen, <=, DN_MAX_BONUSLEN);
    148    789    ahrens 		ASSERT3U(dn->dn_datablksz, ==,
    149    789    ahrens 		    dn->dn_datablkszsec << SPA_MINBLOCKSHIFT);
    150    789    ahrens 		ASSERT3U(ISP2(dn->dn_datablksz), ==, dn->dn_datablkshift != 0);
    151    789    ahrens 		ASSERT3U((dn->dn_nblkptr - 1) * sizeof (blkptr_t) +
    152    789    ahrens 		    dn->dn_bonuslen, <=, DN_MAX_BONUSLEN);
    153    789    ahrens 		for (i = 0; i < TXG_SIZE; i++) {
    154    789    ahrens 			ASSERT3U(dn->dn_next_nlevels[i], <=, dn->dn_nlevels);
    155    789    ahrens 		}
    156    789    ahrens 	}
    157    789    ahrens 	if (dn->dn_phys->dn_type != DMU_OT_NONE)
    158    789    ahrens 		ASSERT3U(dn->dn_phys->dn_nlevels, <=, dn->dn_nlevels);
    159   9396   Matthew 	ASSERT(DMU_OBJECT_IS_SPECIAL(dn->dn_object) || dn->dn_dbuf != NULL);
    160    789    ahrens 	if (dn->dn_dbuf != NULL) {
    161    789    ahrens 		ASSERT3P(dn->dn_phys, ==,
    162    789    ahrens 		    (dnode_phys_t *)dn->dn_dbuf->db.db_data +
    163    789    ahrens 		    (dn->dn_object % (dn->dn_dbuf->db.db_size >> DNODE_SHIFT)));
    164    789    ahrens 	}
    165    789    ahrens 	if (drop_struct_lock)
    166    789    ahrens 		rw_exit(&dn->dn_struct_rwlock);
    167    873  ek110237 }
    168    789    ahrens #endif
    169    789    ahrens 
    170    789    ahrens void
    171    789    ahrens dnode_byteswap(dnode_phys_t *dnp)
    172    789    ahrens {
    173    789    ahrens 	uint64_t *buf64 = (void*)&dnp->dn_blkptr;
    174    789    ahrens 	int i;
    175    789    ahrens 
    176    789    ahrens 	if (dnp->dn_type == DMU_OT_NONE) {
    177    789    ahrens 		bzero(dnp, sizeof (dnode_phys_t));
    178    789    ahrens 		return;
    179    789    ahrens 	}
    180    789    ahrens 
    181    789    ahrens 	dnp->dn_datablkszsec = BSWAP_16(dnp->dn_datablkszsec);
    182    789    ahrens 	dnp->dn_bonuslen = BSWAP_16(dnp->dn_bonuslen);
    183    789    ahrens 	dnp->dn_maxblkid = BSWAP_64(dnp->dn_maxblkid);
    184   2082  eschrock 	dnp->dn_used = BSWAP_64(dnp->dn_used);
    185    789    ahrens 
    186    789    ahrens 	/*
    187    789    ahrens 	 * dn_nblkptr is only one byte, so it's OK to read it in either
    188    789    ahrens 	 * byte order.  We can't read dn_bouslen.
    189    789    ahrens 	 */
    190    789    ahrens 	ASSERT(dnp->dn_indblkshift <= SPA_MAXBLOCKSHIFT);
    191    789    ahrens 	ASSERT(dnp->dn_nblkptr <= DN_MAX_NBLKPTR);
    192    789    ahrens 	for (i = 0; i < dnp->dn_nblkptr * sizeof (blkptr_t)/8; i++)
    193    789    ahrens 		buf64[i] = BSWAP_64(buf64[i]);
    194    789    ahrens 
    195    789    ahrens 	/*
    196    789    ahrens 	 * OK to check dn_bonuslen for zero, because it won't matter if
    197    789    ahrens 	 * we have the wrong byte order.  This is necessary because the
    198    789    ahrens 	 * dnode dnode is smaller than a regular dnode.
    199    789    ahrens 	 */
    200    789    ahrens 	if (dnp->dn_bonuslen != 0) {
    201    789    ahrens 		/*
    202    789    ahrens 		 * Note that the bonus length calculated here may be
    203    789    ahrens 		 * longer than the actual bonus buffer.  This is because
    204    789    ahrens 		 * we always put the bonus buffer after the last block
    205    789    ahrens 		 * pointer (instead of packing it against the end of the
    206    789    ahrens 		 * dnode buffer).
    207    789    ahrens 		 */
    208    789    ahrens 		int off = (dnp->dn_nblkptr-1) * sizeof (blkptr_t);
    209    789    ahrens 		size_t len = DN_MAX_BONUSLEN - off;
    210   3882    ahrens 		ASSERT3U(dnp->dn_bonustype, <, DMU_OT_NUMTYPES);
    211    789    ahrens 		dmu_ot[dnp->dn_bonustype].ot_byteswap(dnp->dn_bonus + off, len);
    212    789    ahrens 	}
    213    789    ahrens }
    214    789    ahrens 
    215    789    ahrens void
    216    789    ahrens dnode_buf_byteswap(void *vbuf, size_t size)
    217    789    ahrens {
    218    789    ahrens 	dnode_phys_t *buf = vbuf;
    219    789    ahrens 	int i;
    220    789    ahrens 
    221    789    ahrens 	ASSERT3U(sizeof (dnode_phys_t), ==, (1<<DNODE_SHIFT));
    222    789    ahrens 	ASSERT((size & (sizeof (dnode_phys_t)-1)) == 0);
    223    789    ahrens 
    224    789    ahrens 	size >>= DNODE_SHIFT;
    225    789    ahrens 	for (i = 0; i < size; i++) {
    226    789    ahrens 		dnode_byteswap(buf);
    227    789    ahrens 		buf++;
    228    789    ahrens 	}
    229    789    ahrens }
    230    789    ahrens 
    231    789    ahrens static int
    232    789    ahrens free_range_compar(const void *node1, const void *node2)
    233    789    ahrens {
    234    789    ahrens 	const free_range_t *rp1 = node1;
    235    789    ahrens 	const free_range_t *rp2 = node2;
    236    789    ahrens 
    237    789    ahrens 	if (rp1->fr_blkid < rp2->fr_blkid)
    238    789    ahrens 		return (-1);
    239    789    ahrens 	else if (rp1->fr_blkid > rp2->fr_blkid)
    240    789    ahrens 		return (1);
    241    789    ahrens 	else return (0);
    242    789    ahrens }
    243    789    ahrens 
    244   4944    maybee void
    245   4944    maybee dnode_setbonuslen(dnode_t *dn, int newsize, dmu_tx_t *tx)
    246   4944    maybee {
    247   4944    maybee 	ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
    248   4944    maybee 
    249   4944    maybee 	dnode_setdirty(dn, tx);
    250   4944    maybee 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
    251   4944    maybee 	ASSERT3U(newsize, <=, DN_MAX_BONUSLEN -
    252   4944    maybee 	    (dn->dn_nblkptr-1) * sizeof (blkptr_t));
    253   4944    maybee 	dn->dn_bonuslen = newsize;
    254   4944    maybee 	if (newsize == 0)
    255   4944    maybee 		dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = DN_ZERO_BONUSLEN;
    256   4944    maybee 	else
    257   4944    maybee 		dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen;
    258   4944    maybee 	rw_exit(&dn->dn_struct_rwlock);
    259   4944    maybee }
    260   4944    maybee 
    261    789    ahrens static void
    262    789    ahrens dnode_setdblksz(dnode_t *dn, int size)
    263    789    ahrens {
    264    789    ahrens 	ASSERT3U(P2PHASE(size, SPA_MINBLOCKSIZE), ==, 0);
    265    789    ahrens 	ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
    266    789    ahrens 	ASSERT3U(size, >=, SPA_MINBLOCKSIZE);
    267    789    ahrens 	ASSERT3U(size >> SPA_MINBLOCKSHIFT, <,
    268    789    ahrens 	    1<<(sizeof (dn->dn_phys->dn_datablkszsec) * 8));
    269    789    ahrens 	dn->dn_datablksz = size;
    270    789    ahrens 	dn->dn_datablkszsec = size >> SPA_MINBLOCKSHIFT;
    271    789    ahrens 	dn->dn_datablkshift = ISP2(size) ? highbit(size - 1) : 0;
    272    789    ahrens }
    273    789    ahrens 
    274    789    ahrens static dnode_t *
    275  10298   Matthew dnode_create(objset_t *os, dnode_phys_t *dnp, dmu_buf_impl_t *db,
    276    789    ahrens     uint64_t object)
    277    789    ahrens {
    278    789    ahrens 	dnode_t *dn = kmem_cache_alloc(dnode_cache, KM_SLEEP);
    279    789    ahrens 	(void) dnode_cons(dn, NULL, 0); /* XXX */
    280    789    ahrens 
    281    789    ahrens 	dn->dn_objset = os;
    282    789    ahrens 	dn->dn_object = object;
    283    789    ahrens 	dn->dn_dbuf = db;
    284    789    ahrens 	dn->dn_phys = dnp;
    285    789    ahrens 
    286    789    ahrens 	if (dnp->dn_datablkszsec)
    287    789    ahrens 		dnode_setdblksz(dn, dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
    288    789    ahrens 	dn->dn_indblkshift = dnp->dn_indblkshift;
    289    789    ahrens 	dn->dn_nlevels = dnp->dn_nlevels;
    290    789    ahrens 	dn->dn_type = dnp->dn_type;
    291    789    ahrens 	dn->dn_nblkptr = dnp->dn_nblkptr;
    292    789    ahrens 	dn->dn_checksum = dnp->dn_checksum;
    293    789    ahrens 	dn->dn_compress = dnp->dn_compress;
    294    789    ahrens 	dn->dn_bonustype = dnp->dn_bonustype;
    295    789    ahrens 	dn->dn_bonuslen = dnp->dn_bonuslen;
    296    789    ahrens 	dn->dn_maxblkid = dnp->dn_maxblkid;
    297    789    ahrens 
    298    789    ahrens 	dmu_zfetch_init(&dn->dn_zfetch, dn);
    299    789    ahrens 
    300    789    ahrens 	ASSERT(dn->dn_phys->dn_type < DMU_OT_NUMTYPES);
    301    789    ahrens 	mutex_enter(&os->os_lock);
    302    789    ahrens 	list_insert_head(&os->os_dnodes, dn);
    303    789    ahrens 	mutex_exit(&os->os_lock);
    304    789    ahrens 
    305   8582   Brendan 	arc_space_consume(sizeof (dnode_t), ARC_SPACE_OTHER);
    306    789    ahrens 	return (dn);
    307    789    ahrens }
    308    789    ahrens 
    309    789    ahrens static void
    310    789    ahrens dnode_destroy(dnode_t *dn)
    311    789    ahrens {
    312  10298   Matthew 	objset_t *os = dn->dn_objset;
    313   2885    ahrens 
    314   2885    ahrens #ifdef ZFS_DEBUG
    315   2885    ahrens 	int i;
    316   2885    ahrens 
    317   2885    ahrens 	for (i = 0; i < TXG_SIZE; i++) {
    318   2885    ahrens 		ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
    319   3547    maybee 		ASSERT(NULL == list_head(&dn->dn_dirty_records[i]));
    320   2885    ahrens 		ASSERT(0 == avl_numnodes(&dn->dn_ranges[i]));
    321   2885    ahrens 	}
    322   2885    ahrens 	ASSERT(NULL == list_head(&dn->dn_dbufs));
    323   2885    ahrens #endif
    324   9396   Matthew 	ASSERT(dn->dn_oldphys == NULL);
    325    789    ahrens 
    326    789    ahrens 	mutex_enter(&os->os_lock);
    327    789    ahrens 	list_remove(&os->os_dnodes, dn);
    328    789    ahrens 	mutex_exit(&os->os_lock);
    329    789    ahrens 
    330    789    ahrens 	if (dn->dn_dirtyctx_firstset) {
    331    789    ahrens 		kmem_free(dn->dn_dirtyctx_firstset, 1);
    332    789    ahrens 		dn->dn_dirtyctx_firstset = NULL;
    333    789    ahrens 	}
    334    789    ahrens 	dmu_zfetch_rele(&dn->dn_zfetch);
    335   1544  eschrock 	if (dn->dn_bonus) {
    336   1544  eschrock 		mutex_enter(&dn->dn_bonus->db_mtx);
    337   1544  eschrock 		dbuf_evict(dn->dn_bonus);
    338   1544  eschrock 		dn->dn_bonus = NULL;
    339   1544  eschrock 	}
    340    789    ahrens 	kmem_cache_free(dnode_cache, dn);
    341   8582   Brendan 	arc_space_return(sizeof (dnode_t), ARC_SPACE_OTHER);
    342    789    ahrens }
    343    789    ahrens 
    344    789    ahrens void
    345    789    ahrens dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs,
    346   1599    ahrens     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
    347    789    ahrens {
    348    789    ahrens 	int i;
    349    789    ahrens 
    350    789    ahrens 	if (blocksize == 0)
    351    789    ahrens 		blocksize = 1 << zfs_default_bs;
    352   1402    ahrens 	else if (blocksize > SPA_MAXBLOCKSIZE)
    353   1402    ahrens 		blocksize = SPA_MAXBLOCKSIZE;
    354   1402    ahrens 	else
    355   1402    ahrens 		blocksize = P2ROUNDUP(blocksize, SPA_MINBLOCKSIZE);
    356    789    ahrens 
    357    789    ahrens 	if (ibs == 0)
    358    789    ahrens 		ibs = zfs_default_ibs;
    359    789    ahrens 
    360    789    ahrens 	ibs = MIN(MAX(ibs, DN_MIN_INDBLKSHIFT), DN_MAX_INDBLKSHIFT);
    361    789    ahrens 
    362    789    ahrens 	dprintf("os=%p obj=%llu txg=%llu blocksize=%d ibs=%d\n", dn->dn_objset,
    363    789    ahrens 	    dn->dn_object, tx->tx_txg, blocksize, ibs);
    364    789    ahrens 
    365    789    ahrens 	ASSERT(dn->dn_type == DMU_OT_NONE);
    366    789    ahrens 	ASSERT(bcmp(dn->dn_phys, &dnode_phys_zero, sizeof (dnode_phys_t)) == 0);
    367    789    ahrens 	ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE);
    368    789    ahrens 	ASSERT(ot != DMU_OT_NONE);
    369    789    ahrens 	ASSERT3U(ot, <, DMU_OT_NUMTYPES);
    370    789    ahrens 	ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
    371    789    ahrens 	    (bonustype != DMU_OT_NONE && bonuslen != 0));
    372    789    ahrens 	ASSERT3U(bonustype, <, DMU_OT_NUMTYPES);
    373    789    ahrens 	ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
    374    789    ahrens 	ASSERT(dn->dn_type == DMU_OT_NONE);
    375    789    ahrens 	ASSERT3U(dn->dn_maxblkid, ==, 0);
    376    789    ahrens 	ASSERT3U(dn->dn_allocated_txg, ==, 0);
    377    789    ahrens 	ASSERT3U(dn->dn_assigned_txg, ==, 0);
    378    789    ahrens 	ASSERT(refcount_is_zero(&dn->dn_tx_holds));
    379    789    ahrens 	ASSERT3U(refcount_count(&dn->dn_holds), <=, 1);
    380    789    ahrens 	ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL);
    381    789    ahrens 
    382    789    ahrens 	for (i = 0; i < TXG_SIZE; i++) {
    383    789    ahrens 		ASSERT3U(dn->dn_next_nlevels[i], ==, 0);
    384    789    ahrens 		ASSERT3U(dn->dn_next_indblkshift[i], ==, 0);
    385   4944    maybee 		ASSERT3U(dn->dn_next_bonuslen[i], ==, 0);
    386   1596    ahrens 		ASSERT3U(dn->dn_next_blksz[i], ==, 0);
    387   1596    ahrens 		ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
    388   3547    maybee 		ASSERT3P(list_head(&dn->dn_dirty_records[i]), ==, NULL);
    389    789    ahrens 		ASSERT3U(avl_numnodes(&dn->dn_ranges[i]), ==, 0);
    390    789    ahrens 	}
    391    789    ahrens 
    392    789    ahrens 	dn->dn_type = ot;
    393    789    ahrens 	dnode_setdblksz(dn, blocksize);
    394    789    ahrens 	dn->dn_indblkshift = ibs;
    395    789    ahrens 	dn->dn_nlevels = 1;
    396    789    ahrens 	dn->dn_nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
    397    789    ahrens 	dn->dn_bonustype = bonustype;
    398    789    ahrens 	dn->dn_bonuslen = bonuslen;
    399    789    ahrens 	dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
    400    789    ahrens 	dn->dn_compress = ZIO_COMPRESS_INHERIT;
    401    789    ahrens 	dn->dn_dirtyctx = 0;
    402    789    ahrens 
    403    789    ahrens 	dn->dn_free_txg = 0;
    404    789    ahrens 	if (dn->dn_dirtyctx_firstset) {
    405    789    ahrens 		kmem_free(dn->dn_dirtyctx_firstset, 1);
    406    789    ahrens 		dn->dn_dirtyctx_firstset = NULL;
    407    789    ahrens 	}
    408    789    ahrens 
    409    789    ahrens 	dn->dn_allocated_txg = tx->tx_txg;
    410   1599    ahrens 
    411    789    ahrens 	dnode_setdirty(dn, tx);
    412   1599    ahrens 	dn->dn_next_indblkshift[tx->tx_txg & TXG_MASK] = ibs;
    413   4944    maybee 	dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen;
    414   1599    ahrens 	dn->dn_next_blksz[tx->tx_txg & TXG_MASK] = dn->dn_datablksz;
    415    789    ahrens }
    416    789    ahrens 
    417    789    ahrens void
    418    789    ahrens dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize,
    419    789    ahrens     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
    420    789    ahrens {
    421   8986      Mark 	int nblkptr;
    422   1596    ahrens 
    423    789    ahrens 	ASSERT3U(blocksize, >=, SPA_MINBLOCKSIZE);
    424    789    ahrens 	ASSERT3U(blocksize, <=, SPA_MAXBLOCKSIZE);
    425    789    ahrens 	ASSERT3U(blocksize % SPA_MINBLOCKSIZE, ==, 0);
    426   1544  eschrock 	ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx));
    427    789    ahrens 	ASSERT(tx->tx_txg != 0);
    428    789    ahrens 	ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
    429    789    ahrens 	    (bonustype != DMU_OT_NONE && bonuslen != 0));
    430    789    ahrens 	ASSERT3U(bonustype, <, DMU_OT_NUMTYPES);
    431    789    ahrens 	ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
    432   1596    ahrens 
    433   1544  eschrock 	/* clean up any unreferenced dbufs */
    434   4944    maybee 	dnode_evict_dbufs(dn);
    435    789    ahrens 
    436   8986      Mark 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
    437   8986      Mark 	dnode_setdirty(dn, tx);
    438   8986      Mark 	if (dn->dn_datablksz != blocksize) {
    439   8986      Mark 		/* change blocksize */
    440   8986      Mark 		ASSERT(dn->dn_maxblkid == 0 &&
    441   8986      Mark 		    (BP_IS_HOLE(&dn->dn_phys->dn_blkptr[0]) ||
    442   8986      Mark 		    dnode_block_freed(dn, 0)));
    443   8986      Mark 		dnode_setdblksz(dn, blocksize);
    444   8986      Mark 		dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = blocksize;
    445    789    ahrens 	}
    446   8986      Mark 	if (dn->dn_bonuslen != bonuslen)
    447   8986      Mark 		dn->dn_next_bonuslen[tx->tx_txg&TXG_MASK] = bonuslen;
    448   8644      Mark 	nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
    449   8644      Mark 	if (dn->dn_nblkptr != nblkptr)
    450   8644      Mark 		dn->dn_next_nblkptr[tx->tx_txg&TXG_MASK] = nblkptr;
    451    789    ahrens 	rw_exit(&dn->dn_struct_rwlock);
    452    789    ahrens 
    453    789    ahrens 	/* change type */
    454    789    ahrens 	dn->dn_type = ot;
    455    789    ahrens 
    456    789    ahrens 	/* change bonus size and type */
    457    789    ahrens 	mutex_enter(&dn->dn_mtx);
    458    789    ahrens 	dn->dn_bonustype = bonustype;
    459    789    ahrens 	dn->dn_bonuslen = bonuslen;
    460   8644      Mark 	dn->dn_nblkptr = nblkptr;
    461    789    ahrens 	dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
    462    789    ahrens 	dn->dn_compress = ZIO_COMPRESS_INHERIT;
    463    789    ahrens 	ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
    464   3087    ahrens 
    465   8644      Mark 	/* fix up the bonus db_size */
    466   8644      Mark 	if (dn->dn_bonus) {
    467   4944    maybee 		dn->dn_bonus->db.db_size =
    468   4944    maybee 		    DN_MAX_BONUSLEN - (dn->dn_nblkptr-1) * sizeof (blkptr_t);
    469   4944    maybee 		ASSERT(dn->dn_bonuslen <= dn->dn_bonus->db.db_size);
    470   4944    maybee 	}
    471    789    ahrens 
    472    789    ahrens 	dn->dn_allocated_txg = tx->tx_txg;
    473    789    ahrens 	mutex_exit(&dn->dn_mtx);
    474    789    ahrens }
    475    789    ahrens 
    476    789    ahrens void
    477    789    ahrens dnode_special_close(dnode_t *dn)
    478    789    ahrens {
    479   1544  eschrock 	/*
    480   1544  eschrock 	 * Wait for final references to the dnode to clear.  This can
    481   1544  eschrock 	 * only happen if the arc is asyncronously evicting state that
    482   1544  eschrock 	 * has a hold on this dnode while we are trying to evict this
    483   1544  eschrock 	 * dnode.
    484   1544  eschrock 	 */
    485   1544  eschrock 	while (refcount_count(&dn->dn_holds) > 0)
    486   1544  eschrock 		delay(1);
    487    789    ahrens 	dnode_destroy(dn);
    488    789    ahrens }
    489    789    ahrens 
    490    789    ahrens dnode_t *
    491  10298   Matthew dnode_special_open(objset_t *os, dnode_phys_t *dnp, uint64_t object)
    492    789    ahrens {
    493    789    ahrens 	dnode_t *dn = dnode_create(os, dnp, NULL, object);
    494    873  ek110237 	DNODE_VERIFY(dn);
    495    789    ahrens 	return (dn);
    496    789    ahrens }
    497    789    ahrens 
    498    789    ahrens static void
    499    789    ahrens dnode_buf_pageout(dmu_buf_t *db, void *arg)
    500    789    ahrens {
    501    789    ahrens 	dnode_t **children_dnodes = arg;
    502    789    ahrens 	int i;
    503    789    ahrens 	int epb = db->db_size >> DNODE_SHIFT;
    504    789    ahrens 
    505    789    ahrens 	for (i = 0; i < epb; i++) {
    506    789    ahrens 		dnode_t *dn = children_dnodes[i];
    507    789    ahrens 		int n;
    508    789    ahrens 
    509    789    ahrens 		if (dn == NULL)
    510    789    ahrens 			continue;
    511    789    ahrens #ifdef ZFS_DEBUG
    512    789    ahrens 		/*
    513    789    ahrens 		 * If there are holds on this dnode, then there should
    514    789    ahrens 		 * be holds on the dnode's containing dbuf as well; thus
    515    789    ahrens 		 * it wouldn't be eligable for eviction and this function
    516    789    ahrens 		 * would not have been called.
    517    789    ahrens 		 */
    518    789    ahrens 		ASSERT(refcount_is_zero(&dn->dn_holds));
    519    789    ahrens 		ASSERT(list_head(&dn->dn_dbufs) == NULL);
    520    789    ahrens 		ASSERT(refcount_is_zero(&dn->dn_tx_holds));
    521    789    ahrens 
    522    789    ahrens 		for (n = 0; n < TXG_SIZE; n++)
    523   1596    ahrens 			ASSERT(!list_link_active(&dn->dn_dirty_link[n]));
    524    789    ahrens #endif
    525    789    ahrens 		children_dnodes[i] = NULL;
    526    789    ahrens 		dnode_destroy(dn);
    527    789    ahrens 	}
    528    789    ahrens 	kmem_free(children_dnodes, epb * sizeof (dnode_t *));
    529    789    ahrens }
    530    789    ahrens 
    531    789    ahrens /*
    532   1544  eschrock  * errors:
    533   1544  eschrock  * EINVAL - invalid object number.
    534   1544  eschrock  * EIO - i/o error.
    535   1544  eschrock  * succeeds even for free dnodes.
    536    789    ahrens  */
    537   1544  eschrock int
    538  10298   Matthew dnode_hold_impl(objset_t *os, uint64_t object, int flag,
    539   1544  eschrock     void *tag, dnode_t **dnp)
    540    789    ahrens {
    541   1544  eschrock 	int epb, idx, err;
    542    789    ahrens 	int drop_struct_lock = FALSE;
    543   1544  eschrock 	int type;
    544    789    ahrens 	uint64_t blk;
    545    789    ahrens 	dnode_t *mdn, *dn;
    546    789    ahrens 	dmu_buf_impl_t *db;
    547    789    ahrens 	dnode_t **children_dnodes;
    548   7754      Jeff 
    549   7754      Jeff 	/*
    550   7754      Jeff 	 * If you are holding the spa config lock as writer, you shouldn't
    551   7754      Jeff 	 * be asking the DMU to do *anything*.
    552   7754      Jeff 	 */
    553   7754      Jeff 	ASSERT(spa_config_held(os->os_spa, SCL_ALL, RW_WRITER) == 0);
    554    789    ahrens 
    555   9396   Matthew 	if (object == DMU_USERUSED_OBJECT || object == DMU_GROUPUSED_OBJECT) {
    556   9396   Matthew 		dn = (object == DMU_USERUSED_OBJECT) ?
    557   9396   Matthew 		    os->os_userused_dnode : os->os_groupused_dnode;
    558   9396   Matthew 		if (dn == NULL)
    559   9396   Matthew 			return (ENOENT);
    560   9396   Matthew 		type = dn->dn_type;
    561   9396   Matthew 		if ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE)
    562   9396   Matthew 			return (ENOENT);
    563   9396   Matthew 		if ((flag & DNODE_MUST_BE_FREE) && type != DMU_OT_NONE)
    564   9396   Matthew 			return (EEXIST);
    565   9396   Matthew 		DNODE_VERIFY(dn);
    566   9396   Matthew 		(void) refcount_add(&dn->dn_holds, tag);
    567   9396   Matthew 		*dnp = dn;
    568   9396   Matthew 		return (0);
    569   9396   Matthew 	}
    570   9396   Matthew 
    571    789    ahrens 	if (object == 0 || object >= DN_MAX_OBJECT)
    572   1544  eschrock 		return (EINVAL);
    573    789    ahrens 
    574    789    ahrens 	mdn = os->os_meta_dnode;
    575    789    ahrens 
    576    873  ek110237 	DNODE_VERIFY(mdn);
    577    789    ahrens 
    578    789    ahrens 	if (!RW_WRITE_HELD(&mdn->dn_struct_rwlock)) {
    579    789    ahrens 		rw_enter(&mdn->dn_struct_rwlock, RW_READER);
    580    789    ahrens 		drop_struct_lock = TRUE;
    581    789    ahrens 	}
    582    789    ahrens 
    583    789    ahrens 	blk = dbuf_whichblock(mdn, object * sizeof (dnode_phys_t));
    584    789    ahrens 
    585   1544  eschrock 	db = dbuf_hold(mdn, blk, FTAG);
    586    789    ahrens 	if (drop_struct_lock)
    587    789    ahrens 		rw_exit(&mdn->dn_struct_rwlock);
    588   1544  eschrock 	if (db == NULL)
    589   1544  eschrock 		return (EIO);
    590   1544  eschrock 	err = dbuf_read(db, NULL, DB_RF_CANFAIL);
    591   1544  eschrock 	if (err) {
    592   1544  eschrock 		dbuf_rele(db, FTAG);
    593   1544  eschrock 		return (err);
    594   1544  eschrock 	}
    595    789    ahrens 
    596    789    ahrens 	ASSERT3U(db->db.db_size, >=, 1<<DNODE_SHIFT);
    597    789    ahrens 	epb = db->db.db_size >> DNODE_SHIFT;
    598    789    ahrens 
    599    789    ahrens 	idx = object & (epb-1);
    600    789    ahrens 
    601    789    ahrens 	children_dnodes = dmu_buf_get_user(&db->db);
    602    789    ahrens 	if (children_dnodes == NULL) {
    603    789    ahrens 		dnode_t **winner;
    604    789    ahrens 		children_dnodes = kmem_zalloc(epb * sizeof (dnode_t *),
    605    789    ahrens 		    KM_SLEEP);
    606    789    ahrens 		if (winner = dmu_buf_set_user(&db->db, children_dnodes, NULL,
    607    789    ahrens 		    dnode_buf_pageout)) {
    608    789    ahrens 			kmem_free(children_dnodes, epb * sizeof (dnode_t *));
    609    789    ahrens 			children_dnodes = winner;
    610    789    ahrens 		}
    611    789    ahrens 	}
    612    789    ahrens 
    613    789    ahrens 	if ((dn = children_dnodes[idx]) == NULL) {
    614   4309    maybee 		dnode_phys_t *dnp = (dnode_phys_t *)db->db.db_data+idx;
    615    789    ahrens 		dnode_t *winner;
    616   4309    maybee 
    617   4309    maybee 		dn = dnode_create(os, dnp, db, object);
    618    789    ahrens 		winner = atomic_cas_ptr(&children_dnodes[idx], NULL, dn);
    619    789    ahrens 		if (winner != NULL) {
    620    789    ahrens 			dnode_destroy(dn);
    621    789    ahrens 			dn = winner;
    622    789    ahrens 		}
    623    789    ahrens 	}
    624    789    ahrens 
    625    789    ahrens 	mutex_enter(&dn->dn_mtx);
    626   1544  eschrock 	type = dn->dn_type;
    627    789    ahrens 	if (dn->dn_free_txg ||
    628   1544  eschrock 	    ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE) ||
    629   9396   Matthew 	    ((flag & DNODE_MUST_BE_FREE) &&
    630   9396   Matthew 	    (type != DMU_OT_NONE || dn->dn_oldphys))) {
    631    789    ahrens 		mutex_exit(&dn->dn_mtx);
    632   1544  eschrock 		dbuf_rele(db, FTAG);
    633   1544  eschrock 		return (type == DMU_OT_NONE ? ENOENT : EEXIST);
    634    789    ahrens 	}
    635    789    ahrens 	mutex_exit(&dn->dn_mtx);
    636    789    ahrens 
    637   1544  eschrock 	if (refcount_add(&dn->dn_holds, tag) == 1)
    638    789    ahrens 		dbuf_add_ref(db, dn);
    639    789    ahrens 
    640    873  ek110237 	DNODE_VERIFY(dn);
    641    789    ahrens 	ASSERT3P(dn->dn_dbuf, ==, db);
    642    789    ahrens 	ASSERT3U(dn->dn_object, ==, object);
    643   1544  eschrock 	dbuf_rele(db, FTAG);
    644    789    ahrens 
    645   1544  eschrock 	*dnp = dn;
    646   1544  eschrock 	return (0);
    647    789    ahrens }
    648    789    ahrens 
    649    789    ahrens /*
    650    789    ahrens  * Return held dnode if the object is allocated, NULL if not.
    651    789    ahrens  */
    652   1544  eschrock int
    653  10298   Matthew dnode_hold(objset_t *os, uint64_t object, void *tag, dnode_t **dnp)
    654    789    ahrens {
    655   1544  eschrock 	return (dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED, tag, dnp));
    656    789    ahrens }
    657    789    ahrens 
    658   4944    maybee /*
    659   4944    maybee  * Can only add a reference if there is already at least one
    660   4944    maybee  * reference on the dnode.  Returns FALSE if unable to add a
    661   4944    maybee  * new reference.
    662   4944    maybee  */
    663   4944    maybee boolean_t
    664   1544  eschrock dnode_add_ref(dnode_t *dn, void *tag)
    665    789    ahrens {
    666   4944    maybee 	mutex_enter(&dn->dn_mtx);
    667   4944    maybee 	if (refcount_is_zero(&dn->dn_holds)) {
    668   4944    maybee 		mutex_exit(&dn->dn_mtx);
    669   4944    maybee 		return (FALSE);
    670   4944    maybee 	}
    671   4944    maybee 	VERIFY(1 < refcount_add(&dn->dn_holds, tag));
    672   4944    maybee 	mutex_exit(&dn->dn_mtx);
    673   4944    maybee 	return (TRUE);
    674    789    ahrens }
    675    789    ahrens 
    676    789    ahrens void
    677   1544  eschrock dnode_rele(dnode_t *dn, void *tag)
    678    789    ahrens {
    679    789    ahrens 	uint64_t refs;
    680    789    ahrens 
    681   4944    maybee 	mutex_enter(&dn->dn_mtx);
    682   1544  eschrock 	refs = refcount_remove(&dn->dn_holds, tag);
    683   4944    maybee 	mutex_exit(&dn->dn_mtx);
    684    789    ahrens 	/* NOTE: the DNODE_DNODE does not have a dn_dbuf */
    685    789    ahrens 	if (refs == 0 && dn->dn_dbuf)
    686   1544  eschrock 		dbuf_rele(dn->dn_dbuf, dn);
    687    789    ahrens }
    688    789    ahrens 
    689    789    ahrens void
    690    789    ahrens dnode_setdirty(dnode_t *dn, dmu_tx_t *tx)
    691    789    ahrens {
    692  10298   Matthew 	objset_t *os = dn->dn_objset;
    693    789    ahrens 	uint64_t txg = tx->tx_txg;
    694    789    ahrens 
    695   9396   Matthew 	if (DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
    696   9396   Matthew 		dsl_dataset_dirty(os->os_dsl_dataset, tx);
    697    789    ahrens 		return;
    698   9396   Matthew 	}
    699    789    ahrens 
    700    873  ek110237 	DNODE_VERIFY(dn);
    701    789    ahrens 
    702    789    ahrens #ifdef ZFS_DEBUG
    703    789    ahrens 	mutex_enter(&dn->dn_mtx);
    704    789    ahrens 	ASSERT(dn->dn_phys->dn_type || dn->dn_allocated_txg);
    705    789    ahrens 	/* ASSERT(dn->dn_free_txg == 0 || dn->dn_free_txg >= txg); */
    706    789    ahrens 	mutex_exit(&dn->dn_mtx);
    707    789    ahrens #endif
    708    789    ahrens 
    709    789    ahrens 	mutex_enter(&os->os_lock);
    710    789    ahrens 
    711    789    ahrens 	/*
    712    789    ahrens 	 * If we are already marked dirty, we're done.
    713    789    ahrens 	 */
    714   1596    ahrens 	if (list_link_active(&dn->dn_dirty_link[txg & TXG_MASK])) {
    715    789    ahrens 		mutex_exit(&os->os_lock);
    716    789    ahrens 		return;
    717    789    ahrens 	}
    718    789    ahrens 
    719    789    ahrens 	ASSERT(!refcount_is_zero(&dn->dn_holds) || list_head(&dn->dn_dbufs));
    720    789    ahrens 	ASSERT(dn->dn_datablksz != 0);
    721   4944    maybee 	ASSERT3U(dn->dn_next_bonuslen[txg&TXG_MASK], ==, 0);
    722   1599    ahrens 	ASSERT3U(dn->dn_next_blksz[txg&TXG_MASK], ==, 0);
    723    789    ahrens 
    724    789    ahrens 	dprintf_ds(os->os_dsl_dataset, "obj=%llu txg=%llu\n",
    725    789    ahrens 	    dn->dn_object, txg);
    726    789    ahrens 
    727    789    ahrens 	if (dn->dn_free_txg > 0 && dn->dn_free_txg <= txg) {
    728    789    ahrens 		list_insert_tail(&os->os_free_dnodes[txg&TXG_MASK], dn);
    729    789    ahrens 	} else {
    730    789    ahrens 		list_insert_tail(&os->os_dirty_dnodes[txg&TXG_MASK], dn);
    731    789    ahrens 	}
    732    789    ahrens 
    733    789    ahrens 	mutex_exit(&os->os_lock);
    734    789    ahrens 
    735    789    ahrens 	/*
    736    789    ahrens 	 * The dnode maintains a hold on its containing dbuf as
    737    789    ahrens 	 * long as there are holds on it.  Each instantiated child
    738    789    ahrens 	 * dbuf maintaines a hold on the dnode.  When the last child
    739    789    ahrens 	 * drops its hold, the dnode will drop its hold on the
    740    789    ahrens 	 * containing dbuf. We add a "dirty hold" here so that the
    741    789    ahrens 	 * dnode will hang around after we finish processing its
    742    789    ahrens 	 * children.
    743    789    ahrens 	 */
    744   4944    maybee 	VERIFY(dnode_add_ref(dn, (void *)(uintptr_t)tx->tx_txg));
    745    789    ahrens 
    746   3547    maybee 	(void) dbuf_dirty(dn->dn_dbuf, tx);
    747    789    ahrens 
    748    789    ahrens 	dsl_dataset_dirty(os->os_dsl_dataset, tx);
    749    789    ahrens }
    750    789    ahrens 
    751    789    ahrens void
    752    789    ahrens dnode_free(dnode_t *dn, dmu_tx_t *tx)
    753    789    ahrens {
    754   1596    ahrens 	int txgoff = tx->tx_txg & TXG_MASK;
    755   1596    ahrens 
    756    789    ahrens 	dprintf("dn=%p txg=%llu\n", dn, tx->tx_txg);
    757    789    ahrens 
    758    789    ahrens 	/* we should be the only holder... hopefully */
    759    789    ahrens 	/* ASSERT3U(refcount_count(&dn->dn_holds), ==, 1); */
    760    789    ahrens 
    761    789    ahrens 	mutex_enter(&dn->dn_mtx);
    762    789    ahrens 	if (dn->dn_type == DMU_OT_NONE || dn->dn_free_txg) {
    763    789    ahrens 		mutex_exit(&dn->dn_mtx);
    764    789    ahrens 		return;
    765    789    ahrens 	}
    766    789    ahrens 	dn->dn_free_txg = tx->tx_txg;
    767    789    ahrens 	mutex_exit(&dn->dn_mtx);
    768    789    ahrens 
    769    789    ahrens 	/*
    770    789    ahrens 	 * If the dnode is already dirty, it needs to be moved from
    771    789    ahrens 	 * the dirty list to the free list.
    772    789    ahrens 	 */
    773    789    ahrens 	mutex_enter(&dn->dn_objset->os_lock);
    774   1596    ahrens 	if (list_link_active(&dn->dn_dirty_link[txgoff])) {
    775   1596    ahrens 		list_remove(&dn->dn_objset->os_dirty_dnodes[txgoff], dn);
    776   1596    ahrens 		list_insert_tail(&dn->dn_objset->os_free_dnodes[txgoff], dn);
    777    789    ahrens 		mutex_exit(&dn->dn_objset->os_lock);
    778    789    ahrens 	} else {
    779    789    ahrens 		mutex_exit(&dn->dn_objset->os_lock);
    780    789    ahrens 		dnode_setdirty(dn, tx);
    781    789    ahrens 	}
    782    789    ahrens }
    783    789    ahrens 
    784    789    ahrens /*
    785    789    ahrens  * Try to change the block size for the indicated dnode.  This can only
    786    789    ahrens  * succeed if there are no blocks allocated or dirty beyond first block
    787    789    ahrens  */
    788    789    ahrens int
    789    789    ahrens dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx)
    790    789    ahrens {
    791    789    ahrens 	dmu_buf_impl_t *db, *db_next;
    792   6992    maybee 	int err;
    793    789    ahrens 
    794    789    ahrens 	if (size == 0)
    795    789    ahrens 		size = SPA_MINBLOCKSIZE;
    796    789    ahrens 	if (size > SPA_MAXBLOCKSIZE)
    797    789    ahrens 		size = SPA_MAXBLOCKSIZE;
    798    789    ahrens 	else
    799    789    ahrens 		size = P2ROUNDUP(size, SPA_MINBLOCKSIZE);
    800    789    ahrens 
    801   2445    ahrens 	if (ibs == dn->dn_indblkshift)
    802   2445    ahrens 		ibs = 0;
    803    789    ahrens 
    804   2445    ahrens 	if (size >> SPA_MINBLOCKSHIFT == dn->dn_datablkszsec && ibs == 0)
    805    789    ahrens 		return (0);
    806    789    ahrens 
    807    789    ahrens 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
    808    789    ahrens 
    809    789    ahrens 	/* Check for any allocated blocks beyond the first */
    810    789    ahrens 	if (dn->dn_phys->dn_maxblkid != 0)
    811   2445    ahrens 		goto fail;
    812    789    ahrens 
    813    789    ahrens 	mutex_enter(&dn->dn_dbufs_mtx);
    814    789    ahrens 	for (db = list_head(&dn->dn_dbufs); db; db = db_next) {
    815    789    ahrens 		db_next = list_next(&dn->dn_dbufs, db);
    816    789    ahrens 
    817   6992    maybee 		if (db->db_blkid != 0 && db->db_blkid != DB_BONUS_BLKID) {
    818    789    ahrens 			mutex_exit(&dn->dn_dbufs_mtx);
    819   2445    ahrens 			goto fail;
    820    789    ahrens 		}
    821    789    ahrens 	}
    822    789    ahrens 	mutex_exit(&dn->dn_dbufs_mtx);
    823   2445    ahrens 
    824   2445    ahrens 	if (ibs && dn->dn_nlevels != 1)
    825   2445    ahrens 		goto fail;
    826    789    ahrens 
    827   6992    maybee 	/* resize the old block */
    828   6992    maybee 	err = dbuf_hold_impl(dn, 0, 0, TRUE, FTAG, &db);
    829   6992    maybee 	if (err == 0)
    830   1596    ahrens 		dbuf_new_size(db, size, tx);
    831   6992    maybee 	else if (err != ENOENT)
    832   6992    maybee 		goto fail;
    833    789    ahrens 
    834    789    ahrens 	dnode_setdblksz(dn, size);
    835   1596    ahrens 	dnode_setdirty(dn, tx);
    836   1596    ahrens 	dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = size;
    837   2445    ahrens 	if (ibs) {
    838   2445    ahrens 		dn->dn_indblkshift = ibs;
    839   2445    ahrens 		dn->dn_next_indblkshift[tx->tx_txg&TXG_MASK] = ibs;
    840   2445    ahrens 	}
    841   6992    maybee 	/* rele after we have fixed the blocksize in the dnode */
    842   1596    ahrens 	if (db)
    843   1596    ahrens 		dbuf_rele(db, FTAG);
    844    789    ahrens 
    845    789    ahrens 	rw_exit(&dn->dn_struct_rwlock);
    846   2445    ahrens 	return (0);
    847   2445    ahrens 
    848   2445    ahrens fail:
    849   2445    ahrens 	rw_exit(&dn->dn_struct_rwlock);
    850   2445    ahrens 	return (ENOTSUP);
    851    789    ahrens }
    852    789    ahrens 
    853   7332  Jonathan /* read-holding callers must not rely on the lock being continuously held */
    854    789    ahrens void
    855   7332  Jonathan dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx, boolean_t have_read)
    856    789    ahrens {
    857    789    ahrens 	uint64_t txgoff = tx->tx_txg & TXG_MASK;
    858   1596    ahrens 	int epbs, new_nlevels;
    859    789    ahrens 	uint64_t sz;
    860    789    ahrens 
    861   1596    ahrens 	ASSERT(blkid != DB_BONUS_BLKID);
    862    789    ahrens 
    863   7332  Jonathan 	ASSERT(have_read ?
    864   7332  Jonathan 	    RW_READ_HELD(&dn->dn_struct_rwlock) :
    865   7332  Jonathan 	    RW_WRITE_HELD(&dn->dn_struct_rwlock));
    866   7332  Jonathan 
    867   7332  Jonathan 	/*
    868   7332  Jonathan 	 * if we have a read-lock, check to see if we need to do any work
    869   7332  Jonathan 	 * before upgrading to a write-lock.
    870   7332  Jonathan 	 */
    871   7332  Jonathan 	if (have_read) {
    872   7332  Jonathan 		if (blkid <= dn->dn_maxblkid)
    873   7332  Jonathan 			return;
    874   7332  Jonathan 
    875   7332  Jonathan 		if (!rw_tryupgrade(&dn->dn_struct_rwlock)) {
    876   7332  Jonathan 			rw_exit(&dn->dn_struct_rwlock);
    877   7332  Jonathan 			rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
    878   7332  Jonathan 		}
    879    789    ahrens 	}
    880    789    ahrens 
    881   1596    ahrens 	if (blkid <= dn->dn_maxblkid)
    882   1596    ahrens 		goto out;
    883   1596    ahrens 
    884   1596    ahrens 	dn->dn_maxblkid = blkid;
    885    789    ahrens 
    886    789    ahrens 	/*
    887   1596    ahrens 	 * Compute the number of levels necessary to support the new maxblkid.
    888    789    ahrens 	 */
    889    789    ahrens 	new_nlevels = 1;
    890    789    ahrens 	epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
    891   1596    ahrens 	for (sz = dn->dn_nblkptr;
    892   1596    ahrens 	    sz <= blkid && sz >= dn->dn_nblkptr; sz <<= epbs)
    893   1596    ahrens 		new_nlevels++;
    894    789    ahrens 
    895   1596    ahrens 	if (new_nlevels > dn->dn_nlevels) {
    896   1596    ahrens 		int old_nlevels = dn->dn_nlevels;
    897   1596    ahrens 		dmu_buf_impl_t *db;
    898   3547    maybee 		list_t *list;
    899   3547    maybee 		dbuf_dirty_record_t *new, *dr, *dr_next;
    900    789    ahrens 
    901   1596    ahrens 		dn->dn_nlevels = new_nlevels;
    902   1596    ahrens 
    903   1596    ahrens 		ASSERT3U(new_nlevels, >, dn->dn_next_nlevels[txgoff]);
    904    789    ahrens 		dn->dn_next_nlevels[txgoff] = new_nlevels;
    905    789    ahrens 
    906   3547    maybee 		/* dirty the left indirects */
    907   1596    ahrens 		db = dbuf_hold_level(dn, old_nlevels, 0, FTAG);
    908   3547    maybee 		new = dbuf_dirty(db, tx);
    909   1544  eschrock 		dbuf_rele(db, FTAG);
    910   1596    ahrens 
    911   3547    maybee 		/* transfer the dirty records to the new indirect */
    912   3547    maybee 		mutex_enter(&dn->dn_mtx);
    913   3547    maybee 		mutex_enter(&new->dt.di.dr_mtx);
    914   3547    maybee 		list = &dn->dn_dirty_records[txgoff];
    915   3547    maybee 		for (dr = list_head(list); dr; dr = dr_next) {
    916   3547    maybee 			dr_next = list_next(&dn->dn_dirty_records[txgoff], dr);
    917   3547    maybee 			if (dr->dr_dbuf->db_level != new_nlevels-1 &&
    918   3547    maybee 			    dr->dr_dbuf->db_blkid != DB_BONUS_BLKID) {
    919   3547    maybee 				ASSERT(dr->dr_dbuf->db_level == old_nlevels-1);
    920   3547    maybee 				list_remove(&dn->dn_dirty_records[txgoff], dr);
    921   3547    maybee 				list_insert_tail(&new->dt.di.dr_children, dr);
    922   3547    maybee 				dr->dr_parent = new;
    923   3547    maybee 			}
    924   3547    maybee 		}
    925   3547    maybee 		mutex_exit(&new->dt.di.dr_mtx);
    926   3547    maybee 		mutex_exit(&dn->dn_mtx);
    927    789    ahrens 	}
    928    789    ahrens 
    929    789    ahrens out:
    930   7332  Jonathan 	if (have_read)
    931   7332  Jonathan 		rw_downgrade(&dn->dn_struct_rwlock);
    932    789    ahrens }
    933    789    ahrens 
    934    789    ahrens void
    935    789    ahrens dnode_clear_range(dnode_t *dn, uint64_t blkid, uint64_t nblks, dmu_tx_t *tx)
    936    789    ahrens {
    937    789    ahrens 	avl_tree_t *tree = &dn->dn_ranges[tx->tx_txg&TXG_MASK];
    938    789    ahrens 	avl_index_t where;
    939    789    ahrens 	free_range_t *rp;
    940    789    ahrens 	free_range_t rp_tofind;
    941    789    ahrens 	uint64_t endblk = blkid + nblks;
    942    789    ahrens 
    943    789    ahrens 	ASSERT(MUTEX_HELD(&dn->dn_mtx));
    944    789    ahrens 	ASSERT(nblks <= UINT64_MAX - blkid); /* no overflow */
    945    789    ahrens 
    946    789    ahrens 	dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n",
    947    789    ahrens 	    blkid, nblks, tx->tx_txg);
    948    789    ahrens 	rp_tofind.fr_blkid = blkid;
    949    789    ahrens 	rp = avl_find(tree, &rp_tofind, &where);
    950    789    ahrens 	if (rp == NULL)
    951    789    ahrens 		rp = avl_nearest(tree, where, AVL_BEFORE);
    952    789    ahrens 	if (rp == NULL)
    953    789    ahrens 		rp = avl_nearest(tree, where, AVL_AFTER);
    954    789    ahrens 
    955    789    ahrens 	while (rp && (rp->fr_blkid <= blkid + nblks)) {
    956    789    ahrens 		uint64_t fr_endblk = rp->fr_blkid + rp->fr_nblks;
    957    789    ahrens 		free_range_t *nrp = AVL_NEXT(tree, rp);
    958    789    ahrens 
    959    789    ahrens 		if (blkid <= rp->fr_blkid && endblk >= fr_endblk) {
    960    789    ahrens 			/* clear this entire range */
    961    789    ahrens 			avl_remove(tree, rp);
    962    789    ahrens 			kmem_free(rp, sizeof (free_range_t));
    963    789    ahrens 		} else if (blkid <= rp->fr_blkid &&
    964    789    ahrens 		    endblk > rp->fr_blkid && endblk < fr_endblk) {
    965    789    ahrens 			/* clear the beginning of this range */
    966    789    ahrens 			rp->fr_blkid = endblk;
    967    789    ahrens 			rp->fr_nblks = fr_endblk - endblk;
    968    789    ahrens 		} else if (blkid > rp->fr_blkid && blkid < fr_endblk &&
    969    789    ahrens 		    endblk >= fr_endblk) {
    970    789    ahrens 			/* clear the end of this range */
    971    789    ahrens 			rp->fr_nblks = blkid - rp->fr_blkid;
    972    789    ahrens 		} else if (blkid > rp->fr_blkid && endblk < fr_endblk) {
    973    789    ahrens 			/* clear a chunk out of this range */
    974    789    ahrens 			free_range_t *new_rp =
    975    789    ahrens 			    kmem_alloc(sizeof (free_range_t), KM_SLEEP);
    976    789    ahrens 
    977    789    ahrens 			new_rp->fr_blkid = endblk;
    978    789    ahrens 			new_rp->fr_nblks = fr_endblk - endblk;
    979    789    ahrens 			avl_insert_here(tree, new_rp, rp, AVL_AFTER);
    980    789    ahrens 			rp->fr_nblks = blkid - rp->fr_blkid;
    981    789    ahrens 		}
    982    789    ahrens 		/* there may be no overlap */
    983    789    ahrens 		rp = nrp;
    984    789    ahrens 	}
    985    789    ahrens }
    986    789    ahrens 
    987    789    ahrens void
    988    789    ahrens dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx)
    989    789    ahrens {
    990    789    ahrens 	dmu_buf_impl_t *db;
    991   2445    ahrens 	uint64_t blkoff, blkid, nblks;
    992   6992    maybee 	int blksz, blkshift, head, tail;
    993    789    ahrens 	int trunc = FALSE;
    994   6992    maybee 	int epbs;
    995    789    ahrens 
    996    789    ahrens 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
    997    789    ahrens 	blksz = dn->dn_datablksz;
    998   6992    maybee 	blkshift = dn->dn_datablkshift;
    999   6992    maybee 	epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
   1000    789    ahrens 
   1001    789    ahrens 	if (len == -1ULL) {
   1002    789    ahrens 		len = UINT64_MAX - off;
   1003    789    ahrens 		trunc = TRUE;
   1004    789    ahrens 	}
   1005    789    ahrens 
   1006    789    ahrens 	/*
   1007    789    ahrens 	 * First, block align the region to free:
   1008    789    ahrens 	 */
   1009   2445    ahrens 	if (ISP2(blksz)) {
   1010   2445    ahrens 		head = P2NPHASE(off, blksz);
   1011   2445    ahrens 		blkoff = P2PHASE(off, blksz);
   1012   6992    maybee 		if ((off >> blkshift) > dn->dn_maxblkid)
   1013   6992    maybee 			goto out;
   1014   2445    ahrens 	} else {
   1015   2445    ahrens 		ASSERT(dn->dn_maxblkid == 0);
   1016   2445    ahrens 		if (off == 0 && len >= blksz) {
   1017   6992    maybee 			/* Freeing the whole block; fast-track this request */
   1018   6992    maybee 			blkid = 0;
   1019   6992    maybee 			nblks = 1;
   1020   6992    maybee 			goto done;
   1021   7385      Mark 		} else if (off >= blksz) {
   1022   6992    maybee 			/* Freeing past end-of-data */
   1023   6992    maybee 			goto out;
   1024    789    ahrens 		} else {
   1025   2445    ahrens 			/* Freeing part of the block. */
   1026    789    ahrens 			head = blksz - off;
   1027    789    ahrens 			ASSERT3U(head, >, 0);
   1028    789    ahrens 		}
   1029   2445    ahrens 		blkoff = off;
   1030    789    ahrens 	}
   1031    789    ahrens 	/* zero out any partial block data at the start of the range */
   1032    789    ahrens 	if (head) {
   1033   2445    ahrens 		ASSERT3U(blkoff + head, ==, blksz);
   1034    789    ahrens 		if (len < head)
   1035    789    ahrens 			head = len;
   1036    789    ahrens 		if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off), TRUE,
   1037    789    ahrens 		    FTAG, &db) == 0) {
   1038    789    ahrens 			caddr_t data;
   1039    789    ahrens 
   1040    789    ahrens 			/* don't dirty if it isn't on disk and isn't dirty */
   1041   3547    maybee 			if (db->db_last_dirty ||
   1042    789    ahrens 			    (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
   1043    789    ahrens 				rw_exit(&dn->dn_struct_rwlock);
   1044    789    ahrens 				dbuf_will_dirty(db, tx);
   1045    789    ahrens 				rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
   1046    789    ahrens 				data = db->db.db_data;
   1047   2445    ahrens 				bzero(data + blkoff, head);
   1048    789    ahrens 			}
   1049   1544  eschrock 			dbuf_rele(db, FTAG);
   1050    789    ahrens 		}
   1051    789    ahrens 		off += head;
   1052    789    ahrens 		len -= head;
   1053    789    ahrens 	}
   1054   2445    ahrens 
   1055   2445    ahrens 	/* If the range was less than one block, we're done */
   1056   6992    maybee 	if (len == 0)
   1057    789    ahrens 		goto out;
   1058    789    ahrens 
   1059   6992    maybee 	/* If the remaining range is past end of file, we're done */
   1060   6992    maybee 	if ((off >> blkshift) > dn->dn_maxblkid)
   1061   6992    maybee 		goto out;
   1062    789    ahrens 
   1063   7385      Mark 	ASSERT(ISP2(blksz));
   1064   6992    maybee 	if (trunc)
   1065   6992    maybee 		tail = 0;
   1066   6992    maybee 	else
   1067   6992    maybee 		tail = P2PHASE(len, blksz);
   1068    789    ahrens 
   1069   6992    maybee 	ASSERT3U(P2PHASE(off, blksz), ==, 0);
   1070   6992    maybee 	/* zero out any partial block data at the end of the range */
   1071   6992    maybee 	if (tail) {
   1072   6992    maybee 		if (len < tail)
   1073   6992    maybee 			tail = len;
   1074   6992    maybee 		if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off+len),
   1075   6992    maybee 		    TRUE, FTAG, &db) == 0) {
   1076   6992    maybee 			/* don't dirty if not on disk and not dirty */
   1077   6992    maybee 			if (db->db_last_dirty ||
   1078   6992    maybee 			    (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
   1079   6992    maybee 				rw_exit(&dn->dn_struct_rwlock);
   1080   6992    maybee 				dbuf_will_dirty(db, tx);
   1081   6992    maybee 				rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
   1082   6992    maybee 				bzero(db->db.db_data, tail);
   1083   6992    maybee 			}
   1084   6992    maybee 			dbuf_rele(db, FTAG);
   1085   6992    maybee 		}
   1086   6992    maybee 		len -= tail;
   1087   6992    maybee 	}
   1088   6992    maybee 
   1089   6992    maybee 	/* If the range did not include a full block, we are done */
   1090   6992    maybee 	if (len == 0)
   1091   6992    maybee 		goto out;
   1092   6992    maybee 
   1093   6992    maybee 	ASSERT(IS_P2ALIGNED(off, blksz));
   1094   6992    maybee 	ASSERT(trunc || IS_P2ALIGNED(len, blksz));
   1095   6992    maybee 	blkid = off >> blkshift;
   1096   6992    maybee 	nblks = len >> blkshift;
   1097   6992    maybee 	if (trunc)
   1098   6992    maybee 		nblks += 1;
   1099   6992    maybee 
   1100   6992    maybee 	/*
   1101   6992    maybee 	 * Read in and mark all the level-1 indirects dirty,
   1102   6992    maybee 	 * so that they will stay in memory until syncing phase.
   1103   7049    maybee 	 * Always dirty the first and last indirect to make sure
   1104   7049    maybee 	 * we dirty all the partial indirects.
   1105   6992    maybee 	 */
   1106   6992    maybee 	if (dn->dn_nlevels > 1) {
   1107   6992    maybee 		uint64_t i, first, last;
   1108   6992    maybee 		int shift = epbs + dn->dn_datablkshift;
   1109   6992    maybee 
   1110   6992    maybee 		first = blkid >> epbs;
   1111   7049    maybee 		if (db = dbuf_hold_level(dn, 1, first, FTAG)) {
   1112   7049    maybee 			dbuf_will_dirty(db, tx);
   1113   7049    maybee 			dbuf_rele(db, FTAG);
   1114   7049    maybee 		}
   1115   6992    maybee 		if (trunc)
   1116   6992    maybee 			last = dn->dn_maxblkid >> epbs;
   1117   2445    ahrens 		else
   1118   6992    maybee 			last = (blkid + nblks - 1) >> epbs;
   1119   7049    maybee 		if (last > first && (db = dbuf_hold_level(dn, 1, last, FTAG))) {
   1120   7049    maybee 			dbuf_will_dirty(db, tx);
   1121   7049    maybee 			dbuf_rele(db, FTAG);
   1122   7049    maybee 		}
   1123   7049    maybee 		for (i = first + 1; i < last; i++) {
   1124   6992    maybee 			uint64_t ibyte = i << shift;
   1125   6992    maybee 			int err;
   1126   2445    ahrens 
   1127   6992    maybee 			err = dnode_next_offset(dn,
   1128   6992    maybee 			    DNODE_FIND_HAVELOCK, &ibyte, 1, 1, 0);
   1129   6992    maybee 			i = ibyte >> shift;
   1130   7049    maybee 			if (err == ESRCH || i >= last)
   1131   6992    maybee 				break;
   1132   6992    maybee 			ASSERT(err == 0);
   1133   6992    maybee 			db = dbuf_hold_level(dn, 1, i, FTAG);
   1134   6992    maybee 			if (db) {
   1135   6992    maybee 				dbuf_will_dirty(db, tx);
   1136   2445    ahrens 				dbuf_rele(db, FTAG);
   1137    789    ahrens 			}
   1138   2445    ahrens 		}
   1139    789    ahrens 	}
   1140   6992    maybee done:
   1141   6992    maybee 	/*
   1142   6992    maybee 	 * Add this range to the dnode range list.
   1143   6992    maybee 	 * We will finish up this free operation in the syncing phase.
   1144   6992    maybee 	 */
   1145    789    ahrens 	mutex_enter(&dn->dn_mtx);
   1146    789    ahrens 	dnode_clear_range(dn, blkid, nblks, tx);
   1147    789    ahrens 	{
   1148    789    ahrens 		free_range_t *rp, *found;
   1149    789    ahrens 		avl_index_t where;
   1150    789    ahrens 		avl_tree_t *tree = &dn->dn_ranges[tx->tx_txg&TXG_MASK];
   1151    789    ahrens 
   1152    789    ahrens 		/* Add new range to dn_ranges */
   1153    789    ahrens 		rp = kmem_alloc(sizeof (free_range_t), KM_SLEEP);
   1154    789    ahrens 		rp->fr_blkid = blkid;
   1155    789    ahrens 		rp->fr_nblks = nblks;
   1156    789    ahrens 		found = avl_find(tree, rp, &where);
   1157    789    ahrens 		ASSERT(found == NULL);
   1158    789    ahrens 		avl_insert(tree, rp, where);
   1159    789    ahrens 		dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n",
   1160    789    ahrens 		    blkid, nblks, tx->tx_txg);
   1161    789    ahrens 	}
   1162    789    ahrens 	mutex_exit(&dn->dn_mtx);
   1163    789    ahrens 
   1164   6992    maybee 	dbuf_free_range(dn, blkid, blkid + nblks - 1, tx);
   1165    789    ahrens 	dnode_setdirty(dn, tx);
   1166    789    ahrens out:
   1167   6992    maybee 	if (trunc && dn->dn_maxblkid >= (off >> blkshift))
   1168   6992    maybee 		dn->dn_maxblkid = (off >> blkshift ? (off >> blkshift) - 1 : 0);
   1169   6992    maybee 
   1170    789    ahrens 	rw_exit(&dn->dn_struct_rwlock);
   1171    789    ahrens }
   1172    789    ahrens 
   1173    789    ahrens /* return TRUE if this blkid was freed in a recent txg, or FALSE if it wasn't */
   1174    789    ahrens uint64_t
   1175    789    ahrens dnode_block_freed(dnode_t *dn, uint64_t blkid)
   1176    789    ahrens {
   1177    789    ahrens 	free_range_t range_tofind;
   1178    789    ahrens 	void *dp = spa_get_dsl(dn->dn_objset->os_spa);
   1179    789    ahrens 	int i;
   1180    789    ahrens 
   1181    789    ahrens 	if (blkid == DB_BONUS_BLKID)
   1182    789    ahrens 		return (FALSE);
   1183    789    ahrens 
   1184    789    ahrens 	/*
   1185    789    ahrens 	 * If we're in the process of opening the pool, dp will not be
   1186    789    ahrens 	 * set yet, but there shouldn't be anything dirty.
   1187    789    ahrens 	 */
   1188    789    ahrens 	if (dp == NULL)
   1189    789    ahrens 		return (FALSE);
   1190    789    ahrens 
   1191    789    ahrens 	if (dn->dn_free_txg)
   1192    789    ahrens 		return (TRUE);
   1193    789    ahrens 
   1194    789    ahrens 	range_tofind.fr_blkid = blkid;
   1195    789    ahrens 	mutex_enter(&dn->dn_mtx);
   1196    789    ahrens 	for (i = 0; i < TXG_SIZE; i++) {
   1197    789    ahrens 		free_range_t *range_found;
   1198    789    ahrens 		avl_index_t idx;
   1199    789    ahrens 
   1200    789    ahrens 		range_found = avl_find(&dn->dn_ranges[i], &range_tofind, &idx);
   1201    789    ahrens 		if (range_found) {
   1202    789    ahrens 			ASSERT(range_found->fr_nblks > 0);
   1203    789    ahrens 			break;
   1204    789    ahrens 		}
   1205    789    ahrens 		range_found = avl_nearest(&dn->dn_ranges[i], idx, AVL_BEFORE);
   1206    789    ahrens 		if (range_found &&
   1207    789    ahrens 		    range_found->fr_blkid + range_found->fr_nblks > blkid)
   1208    789    ahrens 			break;
   1209    789    ahrens 	}
   1210    789    ahrens 	mutex_exit(&dn->dn_mtx);
   1211    789    ahrens 	return (i < TXG_SIZE);
   1212    789    ahrens }
   1213    789    ahrens 
   1214    789    ahrens /* call from syncing context when we actually write/free space for this dnode */
   1215    789    ahrens void
   1216   2082  eschrock dnode_diduse_space(dnode_t *dn, int64_t delta)
   1217    789    ahrens {
   1218   2082  eschrock 	uint64_t space;
   1219   2082  eschrock 	dprintf_dnode(dn, "dn=%p dnp=%p used=%llu delta=%lld\n",
   1220    789    ahrens 	    dn, dn->dn_phys,
   1221   2082  eschrock 	    (u_longlong_t)dn->dn_phys->dn_used,
   1222   2082  eschrock 	    (longlong_t)delta);
   1223    789    ahrens 
   1224    789    ahrens 	mutex_enter(&dn->dn_mtx);
   1225   2082  eschrock 	space = DN_USED_BYTES(dn->dn_phys);
   1226   2082  eschrock 	if (delta > 0) {
   1227   2082  eschrock 		ASSERT3U(space + delta, >=, space); /* no overflow */
   1228    789    ahrens 	} else {
   1229   2082  eschrock 		ASSERT3U(space, >=, -delta); /* no underflow */
   1230   2082  eschrock 	}
   1231   2082  eschrock 	space += delta;
   1232   4577    ahrens 	if (spa_version(dn->dn_objset->os_spa) < SPA_VERSION_DNODE_BYTES) {
   1233   2082  eschrock 		ASSERT((dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) == 0);
   1234   2082  eschrock 		ASSERT3U(P2PHASE(space, 1<<DEV_BSHIFT), ==, 0);
   1235   2082  eschrock 		dn->dn_phys->dn_used = space >> DEV_BSHIFT;
   1236   2082  eschrock 	} else {
   1237   2082  eschrock 		dn->dn_phys->dn_used = space;
   1238   2082  eschrock 		dn->dn_phys->dn_flags |= DNODE_FLAG_USED_BYTES;
   1239    789    ahrens 	}
   1240    789    ahrens 	mutex_exit(&dn->dn_mtx);
   1241    789    ahrens }
   1242    789    ahrens 
   1243    789    ahrens /*
   1244    789    ahrens  * Call when we think we're going to write/free space in open context.
   1245    789    ahrens  * Be conservative (ie. OK to write less than this or free more than
   1246    789    ahrens  * this, but don't write more or free less).
   1247    789    ahrens  */
   1248    789    ahrens void
   1249    789    ahrens dnode_willuse_space(dnode_t *dn, int64_t space, dmu_tx_t *tx)
   1250    789    ahrens {
   1251  10298   Matthew 	objset_t *os = dn->dn_objset;
   1252    789    ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
   1253    789    ahrens 
   1254    789    ahrens 	if (space > 0)
   1255    789    ahrens 		space = spa_get_asize(os->os_spa, space);
   1256    789    ahrens 
   1257    789    ahrens 	if (ds)
   1258    789    ahrens 		dsl_dir_willuse_space(ds->ds_dir, space, tx);
   1259    789    ahrens 
   1260    789    ahrens 	dmu_tx_willuse_space(tx, space);
   1261    789    ahrens }
   1262    789    ahrens 
   1263   9950      Mark /*
   1264   9950      Mark  * This function scans a block at the indicated "level" looking for
   1265   9950      Mark  * a hole or data (depending on 'flags').  If level > 0, then we are
   1266   9950      Mark  * scanning an indirect block looking at its pointers.  If level == 0,
   1267   9950      Mark  * then we are looking at a block of dnodes.  If we don't find what we
   1268   9950      Mark  * are looking for in the block, we return ESRCH.  Otherwise, return
   1269   9950      Mark  * with *offset pointing to the beginning (if searching forwards) or
   1270   9950      Mark  * end (if searching backwards) of the range covered by the block
   1271   9950      Mark  * pointer we matched on (or dnode).
   1272   9950      Mark  *
   1273   9950      Mark  * The basic search algorithm used below by dnode_next_offset() is to
   1274   9950      Mark  * use this function to search up the block tree (widen the search) until
   1275   9950      Mark  * we find something (i.e., we don't return ESRCH) and then search back
   1276   9950      Mark  * down the tree (narrow the search) until we reach our original search
   1277   9950      Mark  * level.
   1278   9950      Mark  */
   1279    789    ahrens static int
   1280   6992    maybee dnode_next_offset_level(dnode_t *dn, int flags, uint64_t *offset,
   1281   3025    ahrens 	int lvl, uint64_t blkfill, uint64_t txg)
   1282    789    ahrens {
   1283    789    ahrens 	dmu_buf_impl_t *db = NULL;
   1284    789    ahrens 	void *data = NULL;
   1285    789    ahrens 	uint64_t epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
   1286    789    ahrens 	uint64_t epb = 1ULL << epbs;
   1287    789    ahrens 	uint64_t minfill, maxfill;
   1288   6992    maybee 	boolean_t hole;
   1289   6992    maybee 	int i, inc, error, span;
   1290    789    ahrens 
   1291    789    ahrens 	dprintf("probing object %llu offset %llx level %d of %u\n",
   1292    789    ahrens 	    dn->dn_object, *offset, lvl, dn->dn_phys->dn_nlevels);
   1293   6992    maybee 
   1294   9396   Matthew 	hole = ((flags & DNODE_FIND_HOLE) != 0);
   1295   6992    maybee 	inc = (flags & DNODE_FIND_BACKWARDS) ? -1 : 1;
   1296   7385      Mark 	ASSERT(txg == 0 || !hole);
   1297    789    ahrens 
   1298    789    ahrens 	if (lvl == dn->dn_phys->dn_nlevels) {
   1299    789    ahrens 		error = 0;
   1300    789    ahrens 		epb = dn->dn_phys->dn_nblkptr;
   1301    789    ahrens 		data = dn->dn_phys->dn_blkptr;
   1302    789    ahrens 	} else {
   1303    789    ahrens 		uint64_t blkid = dbuf_whichblock(dn, *offset) >> (epbs * lvl);
   1304    789    ahrens 		error = dbuf_hold_impl(dn, lvl, blkid, TRUE, FTAG, &db);
   1305    789    ahrens 		if (error) {
   1306   7385      Mark 			if (error != ENOENT)
   1307   7385      Mark 				return (error);
   1308   7385      Mark 			if (hole)
   1309   7385      Mark 				return (0);
   1310   7385      Mark 			/*
   1311   7385      Mark 			 * This can only happen when we are searching up
   1312   7385      Mark 			 * the block tree for data.  We don't really need to
   1313   7385      Mark 			 * adjust the offset, as we will just end up looking
   1314   7385      Mark 			 * at the pointer to this block in its parent, and its
   1315   7385      Mark 			 * going to be unallocated, so we will skip over it.
   1316   7385      Mark 			 */
   1317   7385      Mark 			return (ESRCH);
   1318    789    ahrens 		}
   1319   1793    ahrens 		error = dbuf_read(db, NULL, DB_RF_CANFAIL | DB_RF_HAVESTRUCT);
   1320   1793    ahrens 		if (error) {
   1321   1793    ahrens 			dbuf_rele(db, FTAG);
   1322   1793    ahrens 			return (error);
   1323   1793    ahrens 		}
   1324    789    ahrens 		data = db->db.db_data;
   1325    789    ahrens 	}
   1326    789    ahrens 
   1327   3025    ahrens 	if (db && txg &&
   1328   3025    ahrens 	    (db->db_blkptr == NULL || db->db_blkptr->blk_birth <= txg)) {
   1329   7385      Mark 		/*
   1330   7385      Mark 		 * This can only happen when we are searching up the tree
   1331   7385      Mark 		 * and these conditions mean that we need to keep climbing.
   1332   7385      Mark 		 */
   1333   3025    ahrens 		error = ESRCH;
   1334   3025    ahrens 	} else if (lvl == 0) {
   1335    789    ahrens 		dnode_phys_t *dnp = data;
   1336    789    ahrens 		span = DNODE_SHIFT;
   1337    789    ahrens 		ASSERT(dn->dn_type == DMU_OT_DNODE);
   1338    789    ahrens 
   1339   6992    maybee 		for (i = (*offset >> span) & (blkfill - 1);
   1340   6992    maybee 		    i >= 0 && i < blkfill; i += inc) {
   1341   9409  Jonathan 			if ((dnp[i].dn_type == DMU_OT_NONE) == hole)
   1342    789    ahrens 				break;
   1343   6992    maybee 			*offset += (1ULL << span) * inc;
   1344    789    ahrens 		}
   1345   6992    maybee 		if (i < 0 || i == blkfill)
   1346    789    ahrens 			error = ESRCH;
   1347    789    ahrens 	} else {
   1348    789    ahrens 		blkptr_t *bp = data;
   1349   9950      Mark 		uint64_t start = *offset;
   1350    789    ahrens 		span = (lvl - 1) * epbs + dn->dn_datablkshift;
   1351    789    ahrens 		minfill = 0;
   1352    789    ahrens 		maxfill = blkfill << ((lvl - 1) * epbs);
   1353    789    ahrens 
   1354    789    ahrens 		if (hole)
   1355    789    ahrens 			maxfill--;
   1356    789    ahrens 		else
   1357    789    ahrens 			minfill++;
   1358    789    ahrens 
   1359   9950      Mark 		*offset = *offset >> span;
   1360   9950      Mark 		for (i = BF64_GET(*offset, 0, epbs);
   1361   6992    maybee 		    i >= 0 && i < epb; i += inc) {
   1362    789    ahrens 			if (bp[i].blk_fill >= minfill &&
   1363   3025    ahrens 			    bp[i].blk_fill <= maxfill &&
   1364   7385      Mark 			    (hole || bp[i].blk_birth > txg))
   1365    789    ahrens 				break;
   1366   9950      Mark 			if (inc > 0 || *offset > 0)
   1367   9950      Mark 				*offset += inc;
   1368    789    ahrens 		}
   1369   9950      Mark 		*offset = *offset << span;
   1370   9950      Mark 		if (inc < 0) {
   1371   9950      Mark 			/* traversing backwards; position offset at the end */
   1372   9950      Mark 			ASSERT3U(*offset, <=, start);
   1373   9950      Mark 			*offset = MIN(*offset + (1ULL << span) - 1, start);
   1374   9950      Mark 		} else if (*offset < start) {
   1375   9950      Mark 			*offset = start;
   1376   9950      Mark 		}
   1377   9950      Mark 		if (i < 0 || i >= epb)
   1378    789    ahrens 			error = ESRCH;
   1379    789    ahrens 	}
   1380    789    ahrens 
   1381    789    ahrens 	if (db)
   1382   1544  eschrock 		dbuf_rele(db, FTAG);
   1383    789    ahrens 
   1384    789    ahrens 	return (error);
   1385    789    ahrens }
   1386    789    ahrens 
   1387    789    ahrens /*
   1388    789    ahrens  * Find the next hole, data, or sparse region at or after *offset.
   1389    789    ahrens  * The value 'blkfill' tells us how many items we expect to find
   1390    789    ahrens  * in an L0 data block; this value is 1 for normal objects,
   1391    789    ahrens  * DNODES_PER_BLOCK for the meta dnode, and some fraction of
   1392    789    ahrens  * DNODES_PER_BLOCK when searching for sparse regions thereof.
   1393   3025    ahrens  *
   1394    789    ahrens  * Examples:
   1395    789    ahrens  *
   1396   6992    maybee  * dnode_next_offset(dn, flags, offset, 1, 1, 0);
   1397   6992    maybee  *	Finds the next/previous hole/data in a file.
   1398    789    ahrens  *	Used in dmu_offset_next().
   1399    789    ahrens  *
   1400   6992    maybee  * dnode_next_offset(mdn, flags, offset, 0, DNODES_PER_BLOCK, txg);
   1401    789    ahrens  *	Finds the next free/allocated dnode an objset's meta-dnode.
   1402   3025    ahrens  *	Only finds objects that have new contents since txg (ie.
   1403   3025    ahrens  *	bonus buffer changes and content removal are ignored).
   1404    789    ahrens  *	Used in dmu_object_next().
   1405    789    ahrens  *
   1406   6992    maybee  * dnode_next_offset(mdn, DNODE_FIND_HOLE, offset, 2, DNODES_PER_BLOCK >> 2, 0);
   1407    789    ahrens  *	Finds the next L2 meta-dnode bp that's at most 1/4 full.
   1408    789    ahrens  *	Used in dmu_object_alloc().
   1409    789    ahrens  */
   1410    789    ahrens int
   1411   6992    maybee dnode_next_offset(dnode_t *dn, int flags, uint64_t *offset,
   1412   3025    ahrens     int minlvl, uint64_t blkfill, uint64_t txg)
   1413    789    ahrens {
   1414   6992    maybee 	uint64_t initial_offset = *offset;
   1415    789    ahrens 	int lvl, maxlvl;
   1416    789    ahrens 	int error = 0;
   1417    789    ahrens 
   1418   6992    maybee 	if (!(flags & DNODE_FIND_HAVELOCK))
   1419   6992    maybee 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
   1420    789    ahrens 
   1421    789    ahrens 	if (dn->dn_phys->dn_nlevels == 0) {
   1422   6992    maybee 		error = ESRCH;
   1423   6992    maybee 		goto out;
   1424    789    ahrens 	}
   1425    789    ahrens 
   1426    789    ahrens 	if (dn->dn_datablkshift == 0) {
   1427    789    ahrens 		if (*offset < dn->dn_datablksz) {
   1428   6992    maybee 			if (flags & DNODE_FIND_HOLE)
   1429    789    ahrens 				*offset = dn->dn_datablksz;
   1430    789    ahrens 		} else {
   1431    789    ahrens 			error = ESRCH;
   1432    789    ahrens 		}
   1433   6992    maybee 		goto out;
   1434    789    ahrens 	}
   1435    789    ahrens 
   1436    789    ahrens 	maxlvl = dn->dn_phys->dn_nlevels;
   1437    789    ahrens 
   1438    789    ahrens 	for (lvl = minlvl; lvl <= maxlvl; lvl++) {
   1439   3025    ahrens 		error = dnode_next_offset_level(dn,
   1440   6992    maybee 		    flags, offset, lvl, blkfill, txg);
   1441   1793    ahrens 		if (error != ESRCH)
   1442    789    ahrens 			break;
   1443    789    ahrens 	}
   1444    789    ahrens 
   1445   6992    maybee 	while (error == 0 && --lvl >= minlvl) {
   1446   3025    ahrens 		error = dnode_next_offset_level(dn,
   1447   6992    maybee 		    flags, offset, lvl, blkfill, txg);
   1448   3025    ahrens 	}
   1449    789    ahrens 
   1450   6992    maybee 	if (error == 0 && (flags & DNODE_FIND_BACKWARDS ?
   1451   6992    maybee 	    initial_offset < *offset : initial_offset > *offset))
   1452   1793    ahrens 		error = ESRCH;
   1453   6992    maybee out:
   1454   6992    maybee 	if (!(flags & DNODE_FIND_HAVELOCK))
   1455   6992    maybee 		rw_exit(&dn->dn_struct_rwlock);
   1456    789    ahrens 
   1457    789    ahrens 	return (error);
   1458    789    ahrens }
   1459