1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51491Sahrens * Common Development and Distribution License (the "License"). 61491Sahrens * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 21789Sahrens /* 226992Smaybee * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens #include <sys/zfs_context.h> 27789Sahrens #include <sys/dbuf.h> 28789Sahrens #include <sys/dnode.h> 29789Sahrens #include <sys/dmu.h> 30789Sahrens #include <sys/dmu_impl.h> 31789Sahrens #include <sys/dmu_tx.h> 32789Sahrens #include <sys/dmu_objset.h> 33789Sahrens #include <sys/dsl_dir.h> 34789Sahrens #include <sys/dsl_dataset.h> 35789Sahrens #include <sys/spa.h> 36789Sahrens #include <sys/zio.h> 37789Sahrens #include <sys/dmu_zfetch.h> 38789Sahrens 39789Sahrens static int free_range_compar(const void *node1, const void *node2); 40789Sahrens 41789Sahrens static kmem_cache_t *dnode_cache; 42789Sahrens 43789Sahrens static dnode_phys_t dnode_phys_zero; 44789Sahrens 45789Sahrens int zfs_default_bs = SPA_MINBLOCKSHIFT; 46789Sahrens int zfs_default_ibs = DN_MAX_INDBLKSHIFT; 47789Sahrens 48789Sahrens /* ARGSUSED */ 49789Sahrens static int 50789Sahrens dnode_cons(void *arg, void *unused, int kmflag) 51789Sahrens { 52789Sahrens int i; 53789Sahrens dnode_t *dn = arg; 54789Sahrens bzero(dn, sizeof (dnode_t)); 55789Sahrens 56789Sahrens rw_init(&dn->dn_struct_rwlock, NULL, RW_DEFAULT, NULL); 57789Sahrens mutex_init(&dn->dn_mtx, NULL, MUTEX_DEFAULT, NULL); 58789Sahrens mutex_init(&dn->dn_dbufs_mtx, NULL, MUTEX_DEFAULT, NULL); 59789Sahrens refcount_create(&dn->dn_holds); 60789Sahrens refcount_create(&dn->dn_tx_holds); 61789Sahrens 62789Sahrens for (i = 0; i < TXG_SIZE; i++) { 63789Sahrens avl_create(&dn->dn_ranges[i], free_range_compar, 64789Sahrens sizeof (free_range_t), 65789Sahrens offsetof(struct free_range, fr_node)); 663547Smaybee list_create(&dn->dn_dirty_records[i], 673547Smaybee sizeof (dbuf_dirty_record_t), 683547Smaybee offsetof(dbuf_dirty_record_t, dr_dirty_node)); 69789Sahrens } 70789Sahrens 71789Sahrens list_create(&dn->dn_dbufs, sizeof (dmu_buf_impl_t), 72789Sahrens offsetof(dmu_buf_impl_t, db_link)); 73789Sahrens 74789Sahrens return (0); 75789Sahrens } 76789Sahrens 77789Sahrens /* ARGSUSED */ 78789Sahrens static void 79789Sahrens dnode_dest(void *arg, void *unused) 80789Sahrens { 81789Sahrens int i; 82789Sahrens dnode_t *dn = arg; 83789Sahrens 84789Sahrens rw_destroy(&dn->dn_struct_rwlock); 85789Sahrens mutex_destroy(&dn->dn_mtx); 86789Sahrens mutex_destroy(&dn->dn_dbufs_mtx); 87789Sahrens refcount_destroy(&dn->dn_holds); 88789Sahrens refcount_destroy(&dn->dn_tx_holds); 89789Sahrens 90789Sahrens for (i = 0; i < TXG_SIZE; i++) { 91789Sahrens avl_destroy(&dn->dn_ranges[i]); 923547Smaybee list_destroy(&dn->dn_dirty_records[i]); 93789Sahrens } 94789Sahrens 95789Sahrens list_destroy(&dn->dn_dbufs); 96789Sahrens } 97789Sahrens 98789Sahrens void 99789Sahrens dnode_init(void) 100789Sahrens { 101789Sahrens dnode_cache = kmem_cache_create("dnode_t", 102789Sahrens sizeof (dnode_t), 103789Sahrens 0, dnode_cons, dnode_dest, NULL, NULL, NULL, 0); 104789Sahrens } 105789Sahrens 106789Sahrens void 107789Sahrens dnode_fini(void) 108789Sahrens { 109789Sahrens kmem_cache_destroy(dnode_cache); 110789Sahrens } 111789Sahrens 112789Sahrens 113873Sek110237 #ifdef ZFS_DEBUG 114789Sahrens void 115789Sahrens dnode_verify(dnode_t *dn) 116789Sahrens { 117789Sahrens int drop_struct_lock = FALSE; 118789Sahrens 119789Sahrens ASSERT(dn->dn_phys); 120789Sahrens ASSERT(dn->dn_objset); 121789Sahrens 122789Sahrens ASSERT(dn->dn_phys->dn_type < DMU_OT_NUMTYPES); 123789Sahrens 124789Sahrens if (!(zfs_flags & ZFS_DEBUG_DNODE_VERIFY)) 125789Sahrens return; 126789Sahrens 127789Sahrens if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) { 128789Sahrens rw_enter(&dn->dn_struct_rwlock, RW_READER); 129789Sahrens drop_struct_lock = TRUE; 130789Sahrens } 131789Sahrens if (dn->dn_phys->dn_type != DMU_OT_NONE || dn->dn_allocated_txg != 0) { 132789Sahrens int i; 133789Sahrens ASSERT3U(dn->dn_indblkshift, >=, 0); 134789Sahrens ASSERT3U(dn->dn_indblkshift, <=, SPA_MAXBLOCKSHIFT); 135789Sahrens if (dn->dn_datablkshift) { 136789Sahrens ASSERT3U(dn->dn_datablkshift, >=, SPA_MINBLOCKSHIFT); 137789Sahrens ASSERT3U(dn->dn_datablkshift, <=, SPA_MAXBLOCKSHIFT); 138789Sahrens ASSERT3U(1<<dn->dn_datablkshift, ==, dn->dn_datablksz); 139789Sahrens } 140789Sahrens ASSERT3U(dn->dn_nlevels, <=, 30); 141789Sahrens ASSERT3U(dn->dn_type, <=, DMU_OT_NUMTYPES); 142789Sahrens ASSERT3U(dn->dn_nblkptr, >=, 1); 143789Sahrens ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR); 144789Sahrens ASSERT3U(dn->dn_bonuslen, <=, DN_MAX_BONUSLEN); 145789Sahrens ASSERT3U(dn->dn_datablksz, ==, 146789Sahrens dn->dn_datablkszsec << SPA_MINBLOCKSHIFT); 147789Sahrens ASSERT3U(ISP2(dn->dn_datablksz), ==, dn->dn_datablkshift != 0); 148789Sahrens ASSERT3U((dn->dn_nblkptr - 1) * sizeof (blkptr_t) + 149789Sahrens dn->dn_bonuslen, <=, DN_MAX_BONUSLEN); 150789Sahrens for (i = 0; i < TXG_SIZE; i++) { 151789Sahrens ASSERT3U(dn->dn_next_nlevels[i], <=, dn->dn_nlevels); 152789Sahrens } 153789Sahrens } 154789Sahrens if (dn->dn_phys->dn_type != DMU_OT_NONE) 155789Sahrens ASSERT3U(dn->dn_phys->dn_nlevels, <=, dn->dn_nlevels); 1561544Seschrock ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT || dn->dn_dbuf != NULL); 157789Sahrens if (dn->dn_dbuf != NULL) { 158789Sahrens ASSERT3P(dn->dn_phys, ==, 159789Sahrens (dnode_phys_t *)dn->dn_dbuf->db.db_data + 160789Sahrens (dn->dn_object % (dn->dn_dbuf->db.db_size >> DNODE_SHIFT))); 161789Sahrens } 162789Sahrens if (drop_struct_lock) 163789Sahrens rw_exit(&dn->dn_struct_rwlock); 164873Sek110237 } 165789Sahrens #endif 166789Sahrens 167789Sahrens void 168789Sahrens dnode_byteswap(dnode_phys_t *dnp) 169789Sahrens { 170789Sahrens uint64_t *buf64 = (void*)&dnp->dn_blkptr; 171789Sahrens int i; 172789Sahrens 173789Sahrens if (dnp->dn_type == DMU_OT_NONE) { 174789Sahrens bzero(dnp, sizeof (dnode_phys_t)); 175789Sahrens return; 176789Sahrens } 177789Sahrens 178789Sahrens dnp->dn_datablkszsec = BSWAP_16(dnp->dn_datablkszsec); 179789Sahrens dnp->dn_bonuslen = BSWAP_16(dnp->dn_bonuslen); 180789Sahrens dnp->dn_maxblkid = BSWAP_64(dnp->dn_maxblkid); 1812082Seschrock dnp->dn_used = BSWAP_64(dnp->dn_used); 182789Sahrens 183789Sahrens /* 184789Sahrens * dn_nblkptr is only one byte, so it's OK to read it in either 185789Sahrens * byte order. We can't read dn_bouslen. 186789Sahrens */ 187789Sahrens ASSERT(dnp->dn_indblkshift <= SPA_MAXBLOCKSHIFT); 188789Sahrens ASSERT(dnp->dn_nblkptr <= DN_MAX_NBLKPTR); 189789Sahrens for (i = 0; i < dnp->dn_nblkptr * sizeof (blkptr_t)/8; i++) 190789Sahrens buf64[i] = BSWAP_64(buf64[i]); 191789Sahrens 192789Sahrens /* 193789Sahrens * OK to check dn_bonuslen for zero, because it won't matter if 194789Sahrens * we have the wrong byte order. This is necessary because the 195789Sahrens * dnode dnode is smaller than a regular dnode. 196789Sahrens */ 197789Sahrens if (dnp->dn_bonuslen != 0) { 198789Sahrens /* 199789Sahrens * Note that the bonus length calculated here may be 200789Sahrens * longer than the actual bonus buffer. This is because 201789Sahrens * we always put the bonus buffer after the last block 202789Sahrens * pointer (instead of packing it against the end of the 203789Sahrens * dnode buffer). 204789Sahrens */ 205789Sahrens int off = (dnp->dn_nblkptr-1) * sizeof (blkptr_t); 206789Sahrens size_t len = DN_MAX_BONUSLEN - off; 2073882Sahrens ASSERT3U(dnp->dn_bonustype, <, DMU_OT_NUMTYPES); 208789Sahrens dmu_ot[dnp->dn_bonustype].ot_byteswap(dnp->dn_bonus + off, len); 209789Sahrens } 210789Sahrens } 211789Sahrens 212789Sahrens void 213789Sahrens dnode_buf_byteswap(void *vbuf, size_t size) 214789Sahrens { 215789Sahrens dnode_phys_t *buf = vbuf; 216789Sahrens int i; 217789Sahrens 218789Sahrens ASSERT3U(sizeof (dnode_phys_t), ==, (1<<DNODE_SHIFT)); 219789Sahrens ASSERT((size & (sizeof (dnode_phys_t)-1)) == 0); 220789Sahrens 221789Sahrens size >>= DNODE_SHIFT; 222789Sahrens for (i = 0; i < size; i++) { 223789Sahrens dnode_byteswap(buf); 224789Sahrens buf++; 225789Sahrens } 226789Sahrens } 227789Sahrens 228789Sahrens static int 229789Sahrens free_range_compar(const void *node1, const void *node2) 230789Sahrens { 231789Sahrens const free_range_t *rp1 = node1; 232789Sahrens const free_range_t *rp2 = node2; 233789Sahrens 234789Sahrens if (rp1->fr_blkid < rp2->fr_blkid) 235789Sahrens return (-1); 236789Sahrens else if (rp1->fr_blkid > rp2->fr_blkid) 237789Sahrens return (1); 238789Sahrens else return (0); 239789Sahrens } 240789Sahrens 2414944Smaybee void 2424944Smaybee dnode_setbonuslen(dnode_t *dn, int newsize, dmu_tx_t *tx) 2434944Smaybee { 2444944Smaybee ASSERT3U(refcount_count(&dn->dn_holds), >=, 1); 2454944Smaybee 2464944Smaybee dnode_setdirty(dn, tx); 2474944Smaybee rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 2484944Smaybee ASSERT3U(newsize, <=, DN_MAX_BONUSLEN - 2494944Smaybee (dn->dn_nblkptr-1) * sizeof (blkptr_t)); 2504944Smaybee dn->dn_bonuslen = newsize; 2514944Smaybee if (newsize == 0) 2524944Smaybee dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = DN_ZERO_BONUSLEN; 2534944Smaybee else 2544944Smaybee dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen; 2554944Smaybee rw_exit(&dn->dn_struct_rwlock); 2564944Smaybee } 2574944Smaybee 258789Sahrens static void 259789Sahrens dnode_setdblksz(dnode_t *dn, int size) 260789Sahrens { 261789Sahrens ASSERT3U(P2PHASE(size, SPA_MINBLOCKSIZE), ==, 0); 262789Sahrens ASSERT3U(size, <=, SPA_MAXBLOCKSIZE); 263789Sahrens ASSERT3U(size, >=, SPA_MINBLOCKSIZE); 264789Sahrens ASSERT3U(size >> SPA_MINBLOCKSHIFT, <, 265789Sahrens 1<<(sizeof (dn->dn_phys->dn_datablkszsec) * 8)); 266789Sahrens dn->dn_datablksz = size; 267789Sahrens dn->dn_datablkszsec = size >> SPA_MINBLOCKSHIFT; 268789Sahrens dn->dn_datablkshift = ISP2(size) ? highbit(size - 1) : 0; 269789Sahrens } 270789Sahrens 271789Sahrens static dnode_t * 272789Sahrens dnode_create(objset_impl_t *os, dnode_phys_t *dnp, dmu_buf_impl_t *db, 273789Sahrens uint64_t object) 274789Sahrens { 275789Sahrens dnode_t *dn = kmem_cache_alloc(dnode_cache, KM_SLEEP); 276789Sahrens (void) dnode_cons(dn, NULL, 0); /* XXX */ 277789Sahrens 278789Sahrens dn->dn_objset = os; 279789Sahrens dn->dn_object = object; 280789Sahrens dn->dn_dbuf = db; 281789Sahrens dn->dn_phys = dnp; 282789Sahrens 283789Sahrens if (dnp->dn_datablkszsec) 284789Sahrens dnode_setdblksz(dn, dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT); 285789Sahrens dn->dn_indblkshift = dnp->dn_indblkshift; 286789Sahrens dn->dn_nlevels = dnp->dn_nlevels; 287789Sahrens dn->dn_type = dnp->dn_type; 288789Sahrens dn->dn_nblkptr = dnp->dn_nblkptr; 289789Sahrens dn->dn_checksum = dnp->dn_checksum; 290789Sahrens dn->dn_compress = dnp->dn_compress; 291789Sahrens dn->dn_bonustype = dnp->dn_bonustype; 292789Sahrens dn->dn_bonuslen = dnp->dn_bonuslen; 293789Sahrens dn->dn_maxblkid = dnp->dn_maxblkid; 294789Sahrens 295789Sahrens dmu_zfetch_init(&dn->dn_zfetch, dn); 296789Sahrens 297789Sahrens ASSERT(dn->dn_phys->dn_type < DMU_OT_NUMTYPES); 298789Sahrens mutex_enter(&os->os_lock); 299789Sahrens list_insert_head(&os->os_dnodes, dn); 300789Sahrens mutex_exit(&os->os_lock); 301789Sahrens 3024309Smaybee arc_space_consume(sizeof (dnode_t)); 303789Sahrens return (dn); 304789Sahrens } 305789Sahrens 306789Sahrens static void 307789Sahrens dnode_destroy(dnode_t *dn) 308789Sahrens { 309789Sahrens objset_impl_t *os = dn->dn_objset; 310789Sahrens 3112885Sahrens #ifdef ZFS_DEBUG 3122885Sahrens int i; 3132885Sahrens 3142885Sahrens for (i = 0; i < TXG_SIZE; i++) { 3152885Sahrens ASSERT(!list_link_active(&dn->dn_dirty_link[i])); 3163547Smaybee ASSERT(NULL == list_head(&dn->dn_dirty_records[i])); 3172885Sahrens ASSERT(0 == avl_numnodes(&dn->dn_ranges[i])); 3182885Sahrens } 3192885Sahrens ASSERT(NULL == list_head(&dn->dn_dbufs)); 3202885Sahrens #endif 3212885Sahrens 322789Sahrens mutex_enter(&os->os_lock); 323789Sahrens list_remove(&os->os_dnodes, dn); 324789Sahrens mutex_exit(&os->os_lock); 325789Sahrens 326789Sahrens if (dn->dn_dirtyctx_firstset) { 327789Sahrens kmem_free(dn->dn_dirtyctx_firstset, 1); 328789Sahrens dn->dn_dirtyctx_firstset = NULL; 329789Sahrens } 330789Sahrens dmu_zfetch_rele(&dn->dn_zfetch); 3311544Seschrock if (dn->dn_bonus) { 3321544Seschrock mutex_enter(&dn->dn_bonus->db_mtx); 3331544Seschrock dbuf_evict(dn->dn_bonus); 3341544Seschrock dn->dn_bonus = NULL; 3351544Seschrock } 336789Sahrens kmem_cache_free(dnode_cache, dn); 3374309Smaybee arc_space_return(sizeof (dnode_t)); 338789Sahrens } 339789Sahrens 340789Sahrens void 341789Sahrens dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs, 3421599Sahrens dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 343789Sahrens { 344789Sahrens int i; 345789Sahrens 346789Sahrens if (blocksize == 0) 347789Sahrens blocksize = 1 << zfs_default_bs; 3481402Sahrens else if (blocksize > SPA_MAXBLOCKSIZE) 3491402Sahrens blocksize = SPA_MAXBLOCKSIZE; 3501402Sahrens else 3511402Sahrens blocksize = P2ROUNDUP(blocksize, SPA_MINBLOCKSIZE); 352789Sahrens 353789Sahrens if (ibs == 0) 354789Sahrens ibs = zfs_default_ibs; 355789Sahrens 356789Sahrens ibs = MIN(MAX(ibs, DN_MIN_INDBLKSHIFT), DN_MAX_INDBLKSHIFT); 357789Sahrens 358789Sahrens dprintf("os=%p obj=%llu txg=%llu blocksize=%d ibs=%d\n", dn->dn_objset, 359789Sahrens dn->dn_object, tx->tx_txg, blocksize, ibs); 360789Sahrens 361789Sahrens ASSERT(dn->dn_type == DMU_OT_NONE); 362789Sahrens ASSERT(bcmp(dn->dn_phys, &dnode_phys_zero, sizeof (dnode_phys_t)) == 0); 363789Sahrens ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE); 364789Sahrens ASSERT(ot != DMU_OT_NONE); 365789Sahrens ASSERT3U(ot, <, DMU_OT_NUMTYPES); 366789Sahrens ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) || 367789Sahrens (bonustype != DMU_OT_NONE && bonuslen != 0)); 368789Sahrens ASSERT3U(bonustype, <, DMU_OT_NUMTYPES); 369789Sahrens ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN); 370789Sahrens ASSERT(dn->dn_type == DMU_OT_NONE); 371789Sahrens ASSERT3U(dn->dn_maxblkid, ==, 0); 372789Sahrens ASSERT3U(dn->dn_allocated_txg, ==, 0); 373789Sahrens ASSERT3U(dn->dn_assigned_txg, ==, 0); 374789Sahrens ASSERT(refcount_is_zero(&dn->dn_tx_holds)); 375789Sahrens ASSERT3U(refcount_count(&dn->dn_holds), <=, 1); 376789Sahrens ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL); 377789Sahrens 378789Sahrens for (i = 0; i < TXG_SIZE; i++) { 379789Sahrens ASSERT3U(dn->dn_next_nlevels[i], ==, 0); 380789Sahrens ASSERT3U(dn->dn_next_indblkshift[i], ==, 0); 3814944Smaybee ASSERT3U(dn->dn_next_bonuslen[i], ==, 0); 3821596Sahrens ASSERT3U(dn->dn_next_blksz[i], ==, 0); 3831596Sahrens ASSERT(!list_link_active(&dn->dn_dirty_link[i])); 3843547Smaybee ASSERT3P(list_head(&dn->dn_dirty_records[i]), ==, NULL); 385789Sahrens ASSERT3U(avl_numnodes(&dn->dn_ranges[i]), ==, 0); 386789Sahrens } 387789Sahrens 388789Sahrens dn->dn_type = ot; 389789Sahrens dnode_setdblksz(dn, blocksize); 390789Sahrens dn->dn_indblkshift = ibs; 391789Sahrens dn->dn_nlevels = 1; 392789Sahrens dn->dn_nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT); 393789Sahrens dn->dn_bonustype = bonustype; 394789Sahrens dn->dn_bonuslen = bonuslen; 395789Sahrens dn->dn_checksum = ZIO_CHECKSUM_INHERIT; 396789Sahrens dn->dn_compress = ZIO_COMPRESS_INHERIT; 397789Sahrens dn->dn_dirtyctx = 0; 398789Sahrens 399789Sahrens dn->dn_free_txg = 0; 400789Sahrens if (dn->dn_dirtyctx_firstset) { 401789Sahrens kmem_free(dn->dn_dirtyctx_firstset, 1); 402789Sahrens dn->dn_dirtyctx_firstset = NULL; 403789Sahrens } 404789Sahrens 405789Sahrens dn->dn_allocated_txg = tx->tx_txg; 4061599Sahrens 407789Sahrens dnode_setdirty(dn, tx); 4081599Sahrens dn->dn_next_indblkshift[tx->tx_txg & TXG_MASK] = ibs; 4094944Smaybee dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen; 4101599Sahrens dn->dn_next_blksz[tx->tx_txg & TXG_MASK] = dn->dn_datablksz; 411789Sahrens } 412789Sahrens 413789Sahrens void 414789Sahrens dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, 415789Sahrens dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 416789Sahrens { 4174944Smaybee int i, old_nblkptr; 4183087Sahrens dmu_buf_impl_t *db = NULL; 4191596Sahrens 420789Sahrens ASSERT3U(blocksize, >=, SPA_MINBLOCKSIZE); 421789Sahrens ASSERT3U(blocksize, <=, SPA_MAXBLOCKSIZE); 422789Sahrens ASSERT3U(blocksize % SPA_MINBLOCKSIZE, ==, 0); 4231544Seschrock ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx)); 424789Sahrens ASSERT(tx->tx_txg != 0); 425789Sahrens ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) || 426789Sahrens (bonustype != DMU_OT_NONE && bonuslen != 0)); 427789Sahrens ASSERT3U(bonustype, <, DMU_OT_NUMTYPES); 428789Sahrens ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN); 4291596Sahrens 4301596Sahrens for (i = 0; i < TXG_SIZE; i++) 4311596Sahrens ASSERT(!list_link_active(&dn->dn_dirty_link[i])); 432789Sahrens 4331544Seschrock /* clean up any unreferenced dbufs */ 4344944Smaybee dnode_evict_dbufs(dn); 4351544Seschrock ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL); 4361544Seschrock 437789Sahrens /* 438789Sahrens * XXX I should really have a generation number to tell if we 439789Sahrens * need to do this... 440789Sahrens */ 441789Sahrens if (blocksize != dn->dn_datablksz || 442789Sahrens dn->dn_bonustype != bonustype || dn->dn_bonuslen != bonuslen) { 443789Sahrens /* free all old data */ 444789Sahrens dnode_free_range(dn, 0, -1ULL, tx); 445789Sahrens } 446789Sahrens 447789Sahrens /* change blocksize */ 448789Sahrens rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 4493087Sahrens if (blocksize != dn->dn_datablksz && 4503087Sahrens (!BP_IS_HOLE(&dn->dn_phys->dn_blkptr[0]) || 4513087Sahrens list_head(&dn->dn_dbufs) != NULL)) { 4523087Sahrens db = dbuf_hold(dn, 0, FTAG); 4533087Sahrens dbuf_new_size(db, blocksize, tx); 4543087Sahrens } 455789Sahrens dnode_setdblksz(dn, blocksize); 456789Sahrens dnode_setdirty(dn, tx); 4574944Smaybee dn->dn_next_bonuslen[tx->tx_txg&TXG_MASK] = bonuslen; 4581596Sahrens dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = blocksize; 459789Sahrens rw_exit(&dn->dn_struct_rwlock); 4604944Smaybee if (db) 4613087Sahrens dbuf_rele(db, FTAG); 462789Sahrens 463789Sahrens /* change type */ 464789Sahrens dn->dn_type = ot; 465789Sahrens 466789Sahrens /* change bonus size and type */ 467789Sahrens mutex_enter(&dn->dn_mtx); 4684944Smaybee old_nblkptr = dn->dn_nblkptr; 469789Sahrens dn->dn_bonustype = bonustype; 470789Sahrens dn->dn_bonuslen = bonuslen; 471789Sahrens dn->dn_nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT); 472789Sahrens dn->dn_checksum = ZIO_CHECKSUM_INHERIT; 473789Sahrens dn->dn_compress = ZIO_COMPRESS_INHERIT; 474789Sahrens ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR); 475789Sahrens 4764944Smaybee /* XXX - for now, we can't make nblkptr smaller */ 4774944Smaybee ASSERT3U(dn->dn_nblkptr, >=, old_nblkptr); 4784944Smaybee 4794944Smaybee /* fix up the bonus db_size if dn_nblkptr has changed */ 4804944Smaybee if (dn->dn_bonus && dn->dn_bonuslen != old_nblkptr) { 4814944Smaybee dn->dn_bonus->db.db_size = 4824944Smaybee DN_MAX_BONUSLEN - (dn->dn_nblkptr-1) * sizeof (blkptr_t); 4834944Smaybee ASSERT(dn->dn_bonuslen <= dn->dn_bonus->db.db_size); 4844944Smaybee } 4853087Sahrens 486789Sahrens dn->dn_allocated_txg = tx->tx_txg; 487789Sahrens mutex_exit(&dn->dn_mtx); 488789Sahrens } 489789Sahrens 490789Sahrens void 491789Sahrens dnode_special_close(dnode_t *dn) 492789Sahrens { 4931544Seschrock /* 4941544Seschrock * Wait for final references to the dnode to clear. This can 4951544Seschrock * only happen if the arc is asyncronously evicting state that 4961544Seschrock * has a hold on this dnode while we are trying to evict this 4971544Seschrock * dnode. 4981544Seschrock */ 4991544Seschrock while (refcount_count(&dn->dn_holds) > 0) 5001544Seschrock delay(1); 501789Sahrens dnode_destroy(dn); 502789Sahrens } 503789Sahrens 504789Sahrens dnode_t * 505789Sahrens dnode_special_open(objset_impl_t *os, dnode_phys_t *dnp, uint64_t object) 506789Sahrens { 507789Sahrens dnode_t *dn = dnode_create(os, dnp, NULL, object); 508873Sek110237 DNODE_VERIFY(dn); 509789Sahrens return (dn); 510789Sahrens } 511789Sahrens 512789Sahrens static void 513789Sahrens dnode_buf_pageout(dmu_buf_t *db, void *arg) 514789Sahrens { 515789Sahrens dnode_t **children_dnodes = arg; 516789Sahrens int i; 517789Sahrens int epb = db->db_size >> DNODE_SHIFT; 518789Sahrens 519789Sahrens for (i = 0; i < epb; i++) { 520789Sahrens dnode_t *dn = children_dnodes[i]; 521789Sahrens int n; 522789Sahrens 523789Sahrens if (dn == NULL) 524789Sahrens continue; 525789Sahrens #ifdef ZFS_DEBUG 526789Sahrens /* 527789Sahrens * If there are holds on this dnode, then there should 528789Sahrens * be holds on the dnode's containing dbuf as well; thus 529789Sahrens * it wouldn't be eligable for eviction and this function 530789Sahrens * would not have been called. 531789Sahrens */ 532789Sahrens ASSERT(refcount_is_zero(&dn->dn_holds)); 533789Sahrens ASSERT(list_head(&dn->dn_dbufs) == NULL); 534789Sahrens ASSERT(refcount_is_zero(&dn->dn_tx_holds)); 535789Sahrens 536789Sahrens for (n = 0; n < TXG_SIZE; n++) 5371596Sahrens ASSERT(!list_link_active(&dn->dn_dirty_link[n])); 538789Sahrens #endif 539789Sahrens children_dnodes[i] = NULL; 540789Sahrens dnode_destroy(dn); 541789Sahrens } 542789Sahrens kmem_free(children_dnodes, epb * sizeof (dnode_t *)); 543789Sahrens } 544789Sahrens 545789Sahrens /* 5461544Seschrock * errors: 5471544Seschrock * EINVAL - invalid object number. 5481544Seschrock * EIO - i/o error. 5491544Seschrock * succeeds even for free dnodes. 550789Sahrens */ 5511544Seschrock int 5521544Seschrock dnode_hold_impl(objset_impl_t *os, uint64_t object, int flag, 5531544Seschrock void *tag, dnode_t **dnp) 554789Sahrens { 5551544Seschrock int epb, idx, err; 556789Sahrens int drop_struct_lock = FALSE; 5571544Seschrock int type; 558789Sahrens uint64_t blk; 559789Sahrens dnode_t *mdn, *dn; 560789Sahrens dmu_buf_impl_t *db; 561789Sahrens dnode_t **children_dnodes; 562789Sahrens 563789Sahrens if (object == 0 || object >= DN_MAX_OBJECT) 5641544Seschrock return (EINVAL); 565789Sahrens 566789Sahrens mdn = os->os_meta_dnode; 567789Sahrens 568873Sek110237 DNODE_VERIFY(mdn); 569789Sahrens 570789Sahrens if (!RW_WRITE_HELD(&mdn->dn_struct_rwlock)) { 571789Sahrens rw_enter(&mdn->dn_struct_rwlock, RW_READER); 572789Sahrens drop_struct_lock = TRUE; 573789Sahrens } 574789Sahrens 575789Sahrens blk = dbuf_whichblock(mdn, object * sizeof (dnode_phys_t)); 576789Sahrens 5771544Seschrock db = dbuf_hold(mdn, blk, FTAG); 578789Sahrens if (drop_struct_lock) 579789Sahrens rw_exit(&mdn->dn_struct_rwlock); 5801544Seschrock if (db == NULL) 5811544Seschrock return (EIO); 5821544Seschrock err = dbuf_read(db, NULL, DB_RF_CANFAIL); 5831544Seschrock if (err) { 5841544Seschrock dbuf_rele(db, FTAG); 5851544Seschrock return (err); 5861544Seschrock } 587789Sahrens 588789Sahrens ASSERT3U(db->db.db_size, >=, 1<<DNODE_SHIFT); 589789Sahrens epb = db->db.db_size >> DNODE_SHIFT; 590789Sahrens 591789Sahrens idx = object & (epb-1); 592789Sahrens 593789Sahrens children_dnodes = dmu_buf_get_user(&db->db); 594789Sahrens if (children_dnodes == NULL) { 595789Sahrens dnode_t **winner; 596789Sahrens children_dnodes = kmem_zalloc(epb * sizeof (dnode_t *), 597789Sahrens KM_SLEEP); 598789Sahrens if (winner = dmu_buf_set_user(&db->db, children_dnodes, NULL, 599789Sahrens dnode_buf_pageout)) { 600789Sahrens kmem_free(children_dnodes, epb * sizeof (dnode_t *)); 601789Sahrens children_dnodes = winner; 602789Sahrens } 603789Sahrens } 604789Sahrens 605789Sahrens if ((dn = children_dnodes[idx]) == NULL) { 6064309Smaybee dnode_phys_t *dnp = (dnode_phys_t *)db->db.db_data+idx; 607789Sahrens dnode_t *winner; 6084309Smaybee 6094309Smaybee dn = dnode_create(os, dnp, db, object); 610789Sahrens winner = atomic_cas_ptr(&children_dnodes[idx], NULL, dn); 611789Sahrens if (winner != NULL) { 612789Sahrens dnode_destroy(dn); 613789Sahrens dn = winner; 614789Sahrens } 615789Sahrens } 616789Sahrens 617789Sahrens mutex_enter(&dn->dn_mtx); 6181544Seschrock type = dn->dn_type; 619789Sahrens if (dn->dn_free_txg || 6201544Seschrock ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE) || 6211544Seschrock ((flag & DNODE_MUST_BE_FREE) && type != DMU_OT_NONE)) { 622789Sahrens mutex_exit(&dn->dn_mtx); 6231544Seschrock dbuf_rele(db, FTAG); 6241544Seschrock return (type == DMU_OT_NONE ? ENOENT : EEXIST); 625789Sahrens } 626789Sahrens mutex_exit(&dn->dn_mtx); 627789Sahrens 6281544Seschrock if (refcount_add(&dn->dn_holds, tag) == 1) 629789Sahrens dbuf_add_ref(db, dn); 630789Sahrens 631873Sek110237 DNODE_VERIFY(dn); 632789Sahrens ASSERT3P(dn->dn_dbuf, ==, db); 633789Sahrens ASSERT3U(dn->dn_object, ==, object); 6341544Seschrock dbuf_rele(db, FTAG); 635789Sahrens 6361544Seschrock *dnp = dn; 6371544Seschrock return (0); 638789Sahrens } 639789Sahrens 640789Sahrens /* 641789Sahrens * Return held dnode if the object is allocated, NULL if not. 642789Sahrens */ 6431544Seschrock int 6441544Seschrock dnode_hold(objset_impl_t *os, uint64_t object, void *tag, dnode_t **dnp) 645789Sahrens { 6461544Seschrock return (dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED, tag, dnp)); 647789Sahrens } 648789Sahrens 6494944Smaybee /* 6504944Smaybee * Can only add a reference if there is already at least one 6514944Smaybee * reference on the dnode. Returns FALSE if unable to add a 6524944Smaybee * new reference. 6534944Smaybee */ 6544944Smaybee boolean_t 6551544Seschrock dnode_add_ref(dnode_t *dn, void *tag) 656789Sahrens { 6574944Smaybee mutex_enter(&dn->dn_mtx); 6584944Smaybee if (refcount_is_zero(&dn->dn_holds)) { 6594944Smaybee mutex_exit(&dn->dn_mtx); 6604944Smaybee return (FALSE); 6614944Smaybee } 6624944Smaybee VERIFY(1 < refcount_add(&dn->dn_holds, tag)); 6634944Smaybee mutex_exit(&dn->dn_mtx); 6644944Smaybee return (TRUE); 665789Sahrens } 666789Sahrens 667789Sahrens void 6681544Seschrock dnode_rele(dnode_t *dn, void *tag) 669789Sahrens { 670789Sahrens uint64_t refs; 671789Sahrens 6724944Smaybee mutex_enter(&dn->dn_mtx); 6731544Seschrock refs = refcount_remove(&dn->dn_holds, tag); 6744944Smaybee mutex_exit(&dn->dn_mtx); 675789Sahrens /* NOTE: the DNODE_DNODE does not have a dn_dbuf */ 676789Sahrens if (refs == 0 && dn->dn_dbuf) 6771544Seschrock dbuf_rele(dn->dn_dbuf, dn); 678789Sahrens } 679789Sahrens 680789Sahrens void 681789Sahrens dnode_setdirty(dnode_t *dn, dmu_tx_t *tx) 682789Sahrens { 683789Sahrens objset_impl_t *os = dn->dn_objset; 684789Sahrens uint64_t txg = tx->tx_txg; 685789Sahrens 6861544Seschrock if (dn->dn_object == DMU_META_DNODE_OBJECT) 687789Sahrens return; 688789Sahrens 689873Sek110237 DNODE_VERIFY(dn); 690789Sahrens 691789Sahrens #ifdef ZFS_DEBUG 692789Sahrens mutex_enter(&dn->dn_mtx); 693789Sahrens ASSERT(dn->dn_phys->dn_type || dn->dn_allocated_txg); 694789Sahrens /* ASSERT(dn->dn_free_txg == 0 || dn->dn_free_txg >= txg); */ 695789Sahrens mutex_exit(&dn->dn_mtx); 696789Sahrens #endif 697789Sahrens 698789Sahrens mutex_enter(&os->os_lock); 699789Sahrens 700789Sahrens /* 701789Sahrens * If we are already marked dirty, we're done. 702789Sahrens */ 7031596Sahrens if (list_link_active(&dn->dn_dirty_link[txg & TXG_MASK])) { 704789Sahrens mutex_exit(&os->os_lock); 705789Sahrens return; 706789Sahrens } 707789Sahrens 708789Sahrens ASSERT(!refcount_is_zero(&dn->dn_holds) || list_head(&dn->dn_dbufs)); 709789Sahrens ASSERT(dn->dn_datablksz != 0); 7104944Smaybee ASSERT3U(dn->dn_next_bonuslen[txg&TXG_MASK], ==, 0); 7111599Sahrens ASSERT3U(dn->dn_next_blksz[txg&TXG_MASK], ==, 0); 712789Sahrens 713789Sahrens dprintf_ds(os->os_dsl_dataset, "obj=%llu txg=%llu\n", 714789Sahrens dn->dn_object, txg); 715789Sahrens 716789Sahrens if (dn->dn_free_txg > 0 && dn->dn_free_txg <= txg) { 717789Sahrens list_insert_tail(&os->os_free_dnodes[txg&TXG_MASK], dn); 718789Sahrens } else { 719789Sahrens list_insert_tail(&os->os_dirty_dnodes[txg&TXG_MASK], dn); 720789Sahrens } 721789Sahrens 722789Sahrens mutex_exit(&os->os_lock); 723789Sahrens 724789Sahrens /* 725789Sahrens * The dnode maintains a hold on its containing dbuf as 726789Sahrens * long as there are holds on it. Each instantiated child 727789Sahrens * dbuf maintaines a hold on the dnode. When the last child 728789Sahrens * drops its hold, the dnode will drop its hold on the 729789Sahrens * containing dbuf. We add a "dirty hold" here so that the 730789Sahrens * dnode will hang around after we finish processing its 731789Sahrens * children. 732789Sahrens */ 7334944Smaybee VERIFY(dnode_add_ref(dn, (void *)(uintptr_t)tx->tx_txg)); 734789Sahrens 7353547Smaybee (void) dbuf_dirty(dn->dn_dbuf, tx); 736789Sahrens 737789Sahrens dsl_dataset_dirty(os->os_dsl_dataset, tx); 738789Sahrens } 739789Sahrens 740789Sahrens void 741789Sahrens dnode_free(dnode_t *dn, dmu_tx_t *tx) 742789Sahrens { 7431596Sahrens int txgoff = tx->tx_txg & TXG_MASK; 7441596Sahrens 745789Sahrens dprintf("dn=%p txg=%llu\n", dn, tx->tx_txg); 746789Sahrens 747789Sahrens /* we should be the only holder... hopefully */ 748789Sahrens /* ASSERT3U(refcount_count(&dn->dn_holds), ==, 1); */ 749789Sahrens 750789Sahrens mutex_enter(&dn->dn_mtx); 751789Sahrens if (dn->dn_type == DMU_OT_NONE || dn->dn_free_txg) { 752789Sahrens mutex_exit(&dn->dn_mtx); 753789Sahrens return; 754789Sahrens } 755789Sahrens dn->dn_free_txg = tx->tx_txg; 756789Sahrens mutex_exit(&dn->dn_mtx); 757789Sahrens 758789Sahrens /* 759789Sahrens * If the dnode is already dirty, it needs to be moved from 760789Sahrens * the dirty list to the free list. 761789Sahrens */ 762789Sahrens mutex_enter(&dn->dn_objset->os_lock); 7631596Sahrens if (list_link_active(&dn->dn_dirty_link[txgoff])) { 7641596Sahrens list_remove(&dn->dn_objset->os_dirty_dnodes[txgoff], dn); 7651596Sahrens list_insert_tail(&dn->dn_objset->os_free_dnodes[txgoff], dn); 766789Sahrens mutex_exit(&dn->dn_objset->os_lock); 767789Sahrens } else { 768789Sahrens mutex_exit(&dn->dn_objset->os_lock); 769789Sahrens dnode_setdirty(dn, tx); 770789Sahrens } 771789Sahrens } 772789Sahrens 773789Sahrens /* 774789Sahrens * Try to change the block size for the indicated dnode. This can only 775789Sahrens * succeed if there are no blocks allocated or dirty beyond first block 776789Sahrens */ 777789Sahrens int 778789Sahrens dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx) 779789Sahrens { 780789Sahrens dmu_buf_impl_t *db, *db_next; 7816992Smaybee int err; 782789Sahrens 783789Sahrens if (size == 0) 784789Sahrens size = SPA_MINBLOCKSIZE; 785789Sahrens if (size > SPA_MAXBLOCKSIZE) 786789Sahrens size = SPA_MAXBLOCKSIZE; 787789Sahrens else 788789Sahrens size = P2ROUNDUP(size, SPA_MINBLOCKSIZE); 789789Sahrens 7902445Sahrens if (ibs == dn->dn_indblkshift) 7912445Sahrens ibs = 0; 792789Sahrens 7932445Sahrens if (size >> SPA_MINBLOCKSHIFT == dn->dn_datablkszsec && ibs == 0) 794789Sahrens return (0); 795789Sahrens 796789Sahrens rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 797789Sahrens 798789Sahrens /* Check for any allocated blocks beyond the first */ 799789Sahrens if (dn->dn_phys->dn_maxblkid != 0) 8002445Sahrens goto fail; 801789Sahrens 802789Sahrens mutex_enter(&dn->dn_dbufs_mtx); 803789Sahrens for (db = list_head(&dn->dn_dbufs); db; db = db_next) { 804789Sahrens db_next = list_next(&dn->dn_dbufs, db); 805789Sahrens 8066992Smaybee if (db->db_blkid != 0 && db->db_blkid != DB_BONUS_BLKID) { 807789Sahrens mutex_exit(&dn->dn_dbufs_mtx); 8082445Sahrens goto fail; 809789Sahrens } 810789Sahrens } 811789Sahrens mutex_exit(&dn->dn_dbufs_mtx); 812789Sahrens 8132445Sahrens if (ibs && dn->dn_nlevels != 1) 8142445Sahrens goto fail; 8152445Sahrens 8166992Smaybee /* resize the old block */ 8176992Smaybee err = dbuf_hold_impl(dn, 0, 0, TRUE, FTAG, &db); 8186992Smaybee if (err == 0) 8191596Sahrens dbuf_new_size(db, size, tx); 8206992Smaybee else if (err != ENOENT) 8216992Smaybee goto fail; 822789Sahrens 823789Sahrens dnode_setdblksz(dn, size); 8241596Sahrens dnode_setdirty(dn, tx); 8251596Sahrens dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = size; 8262445Sahrens if (ibs) { 8272445Sahrens dn->dn_indblkshift = ibs; 8282445Sahrens dn->dn_next_indblkshift[tx->tx_txg&TXG_MASK] = ibs; 8292445Sahrens } 8306992Smaybee /* rele after we have fixed the blocksize in the dnode */ 8311596Sahrens if (db) 8321596Sahrens dbuf_rele(db, FTAG); 833789Sahrens 834789Sahrens rw_exit(&dn->dn_struct_rwlock); 8352445Sahrens return (0); 8362445Sahrens 8372445Sahrens fail: 8382445Sahrens rw_exit(&dn->dn_struct_rwlock); 8392445Sahrens return (ENOTSUP); 840789Sahrens } 841789Sahrens 842*7332SJonathan.Adams@Sun.COM /* read-holding callers must not rely on the lock being continuously held */ 843789Sahrens void 844*7332SJonathan.Adams@Sun.COM dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx, boolean_t have_read) 845789Sahrens { 846789Sahrens uint64_t txgoff = tx->tx_txg & TXG_MASK; 8471596Sahrens int epbs, new_nlevels; 848789Sahrens uint64_t sz; 849789Sahrens 8501596Sahrens ASSERT(blkid != DB_BONUS_BLKID); 851789Sahrens 852*7332SJonathan.Adams@Sun.COM ASSERT(have_read ? 853*7332SJonathan.Adams@Sun.COM RW_READ_HELD(&dn->dn_struct_rwlock) : 854*7332SJonathan.Adams@Sun.COM RW_WRITE_HELD(&dn->dn_struct_rwlock)); 855*7332SJonathan.Adams@Sun.COM 856*7332SJonathan.Adams@Sun.COM /* 857*7332SJonathan.Adams@Sun.COM * if we have a read-lock, check to see if we need to do any work 858*7332SJonathan.Adams@Sun.COM * before upgrading to a write-lock. 859*7332SJonathan.Adams@Sun.COM */ 860*7332SJonathan.Adams@Sun.COM if (have_read) { 861*7332SJonathan.Adams@Sun.COM if (blkid <= dn->dn_maxblkid) 862*7332SJonathan.Adams@Sun.COM return; 863*7332SJonathan.Adams@Sun.COM 864*7332SJonathan.Adams@Sun.COM if (!rw_tryupgrade(&dn->dn_struct_rwlock)) { 865*7332SJonathan.Adams@Sun.COM rw_exit(&dn->dn_struct_rwlock); 866*7332SJonathan.Adams@Sun.COM rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 867*7332SJonathan.Adams@Sun.COM } 868789Sahrens } 869789Sahrens 8701596Sahrens if (blkid <= dn->dn_maxblkid) 8711596Sahrens goto out; 8721596Sahrens 8731596Sahrens dn->dn_maxblkid = blkid; 874789Sahrens 875789Sahrens /* 8761596Sahrens * Compute the number of levels necessary to support the new maxblkid. 877789Sahrens */ 878789Sahrens new_nlevels = 1; 879789Sahrens epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 8801596Sahrens for (sz = dn->dn_nblkptr; 8811596Sahrens sz <= blkid && sz >= dn->dn_nblkptr; sz <<= epbs) 882789Sahrens new_nlevels++; 883789Sahrens 8841596Sahrens if (new_nlevels > dn->dn_nlevels) { 8851596Sahrens int old_nlevels = dn->dn_nlevels; 8861596Sahrens dmu_buf_impl_t *db; 8873547Smaybee list_t *list; 8883547Smaybee dbuf_dirty_record_t *new, *dr, *dr_next; 8891596Sahrens 8901596Sahrens dn->dn_nlevels = new_nlevels; 8911596Sahrens 8921596Sahrens ASSERT3U(new_nlevels, >, dn->dn_next_nlevels[txgoff]); 893789Sahrens dn->dn_next_nlevels[txgoff] = new_nlevels; 894789Sahrens 8953547Smaybee /* dirty the left indirects */ 8961596Sahrens db = dbuf_hold_level(dn, old_nlevels, 0, FTAG); 8973547Smaybee new = dbuf_dirty(db, tx); 8981544Seschrock dbuf_rele(db, FTAG); 8991596Sahrens 9003547Smaybee /* transfer the dirty records to the new indirect */ 9013547Smaybee mutex_enter(&dn->dn_mtx); 9023547Smaybee mutex_enter(&new->dt.di.dr_mtx); 9033547Smaybee list = &dn->dn_dirty_records[txgoff]; 9043547Smaybee for (dr = list_head(list); dr; dr = dr_next) { 9053547Smaybee dr_next = list_next(&dn->dn_dirty_records[txgoff], dr); 9063547Smaybee if (dr->dr_dbuf->db_level != new_nlevels-1 && 9073547Smaybee dr->dr_dbuf->db_blkid != DB_BONUS_BLKID) { 9083547Smaybee ASSERT(dr->dr_dbuf->db_level == old_nlevels-1); 9093547Smaybee list_remove(&dn->dn_dirty_records[txgoff], dr); 9103547Smaybee list_insert_tail(&new->dt.di.dr_children, dr); 9113547Smaybee dr->dr_parent = new; 9123547Smaybee } 9133547Smaybee } 9143547Smaybee mutex_exit(&new->dt.di.dr_mtx); 9153547Smaybee mutex_exit(&dn->dn_mtx); 916789Sahrens } 917789Sahrens 918789Sahrens out: 919*7332SJonathan.Adams@Sun.COM if (have_read) 920*7332SJonathan.Adams@Sun.COM rw_downgrade(&dn->dn_struct_rwlock); 921789Sahrens } 922789Sahrens 923789Sahrens void 924789Sahrens dnode_clear_range(dnode_t *dn, uint64_t blkid, uint64_t nblks, dmu_tx_t *tx) 925789Sahrens { 926789Sahrens avl_tree_t *tree = &dn->dn_ranges[tx->tx_txg&TXG_MASK]; 927789Sahrens avl_index_t where; 928789Sahrens free_range_t *rp; 929789Sahrens free_range_t rp_tofind; 930789Sahrens uint64_t endblk = blkid + nblks; 931789Sahrens 932789Sahrens ASSERT(MUTEX_HELD(&dn->dn_mtx)); 933789Sahrens ASSERT(nblks <= UINT64_MAX - blkid); /* no overflow */ 934789Sahrens 935789Sahrens dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n", 936789Sahrens blkid, nblks, tx->tx_txg); 937789Sahrens rp_tofind.fr_blkid = blkid; 938789Sahrens rp = avl_find(tree, &rp_tofind, &where); 939789Sahrens if (rp == NULL) 940789Sahrens rp = avl_nearest(tree, where, AVL_BEFORE); 941789Sahrens if (rp == NULL) 942789Sahrens rp = avl_nearest(tree, where, AVL_AFTER); 943789Sahrens 944789Sahrens while (rp && (rp->fr_blkid <= blkid + nblks)) { 945789Sahrens uint64_t fr_endblk = rp->fr_blkid + rp->fr_nblks; 946789Sahrens free_range_t *nrp = AVL_NEXT(tree, rp); 947789Sahrens 948789Sahrens if (blkid <= rp->fr_blkid && endblk >= fr_endblk) { 949789Sahrens /* clear this entire range */ 950789Sahrens avl_remove(tree, rp); 951789Sahrens kmem_free(rp, sizeof (free_range_t)); 952789Sahrens } else if (blkid <= rp->fr_blkid && 953789Sahrens endblk > rp->fr_blkid && endblk < fr_endblk) { 954789Sahrens /* clear the beginning of this range */ 955789Sahrens rp->fr_blkid = endblk; 956789Sahrens rp->fr_nblks = fr_endblk - endblk; 957789Sahrens } else if (blkid > rp->fr_blkid && blkid < fr_endblk && 958789Sahrens endblk >= fr_endblk) { 959789Sahrens /* clear the end of this range */ 960789Sahrens rp->fr_nblks = blkid - rp->fr_blkid; 961789Sahrens } else if (blkid > rp->fr_blkid && endblk < fr_endblk) { 962789Sahrens /* clear a chunk out of this range */ 963789Sahrens free_range_t *new_rp = 964789Sahrens kmem_alloc(sizeof (free_range_t), KM_SLEEP); 965789Sahrens 966789Sahrens new_rp->fr_blkid = endblk; 967789Sahrens new_rp->fr_nblks = fr_endblk - endblk; 968789Sahrens avl_insert_here(tree, new_rp, rp, AVL_AFTER); 969789Sahrens rp->fr_nblks = blkid - rp->fr_blkid; 970789Sahrens } 971789Sahrens /* there may be no overlap */ 972789Sahrens rp = nrp; 973789Sahrens } 974789Sahrens } 975789Sahrens 976789Sahrens void 977789Sahrens dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx) 978789Sahrens { 979789Sahrens dmu_buf_impl_t *db; 9802445Sahrens uint64_t blkoff, blkid, nblks; 9816992Smaybee int blksz, blkshift, head, tail; 982789Sahrens int trunc = FALSE; 9836992Smaybee int epbs; 984789Sahrens 985789Sahrens rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 986789Sahrens blksz = dn->dn_datablksz; 9876992Smaybee blkshift = dn->dn_datablkshift; 9886992Smaybee epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 989789Sahrens 990789Sahrens if (len == -1ULL) { 991789Sahrens len = UINT64_MAX - off; 992789Sahrens trunc = TRUE; 993789Sahrens } 994789Sahrens 995789Sahrens /* 996789Sahrens * First, block align the region to free: 997789Sahrens */ 9982445Sahrens if (ISP2(blksz)) { 9992445Sahrens head = P2NPHASE(off, blksz); 10002445Sahrens blkoff = P2PHASE(off, blksz); 10016992Smaybee if ((off >> blkshift) > dn->dn_maxblkid) 10026992Smaybee goto out; 10032445Sahrens } else { 10042445Sahrens ASSERT(dn->dn_maxblkid == 0); 10052445Sahrens if (off == 0 && len >= blksz) { 10066992Smaybee /* Freeing the whole block; fast-track this request */ 10076992Smaybee blkid = 0; 10086992Smaybee nblks = 1; 10096992Smaybee goto done; 10106992Smaybee } else if (off > blkid) { 10116992Smaybee /* Freeing past end-of-data */ 10126992Smaybee goto out; 1013789Sahrens } else { 10142445Sahrens /* Freeing part of the block. */ 1015789Sahrens head = blksz - off; 1016789Sahrens ASSERT3U(head, >, 0); 1017789Sahrens } 10182445Sahrens blkoff = off; 1019789Sahrens } 1020789Sahrens /* zero out any partial block data at the start of the range */ 1021789Sahrens if (head) { 10222445Sahrens ASSERT3U(blkoff + head, ==, blksz); 1023789Sahrens if (len < head) 1024789Sahrens head = len; 1025789Sahrens if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off), TRUE, 1026789Sahrens FTAG, &db) == 0) { 1027789Sahrens caddr_t data; 1028789Sahrens 1029789Sahrens /* don't dirty if it isn't on disk and isn't dirty */ 10303547Smaybee if (db->db_last_dirty || 1031789Sahrens (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) { 1032789Sahrens rw_exit(&dn->dn_struct_rwlock); 1033789Sahrens dbuf_will_dirty(db, tx); 1034789Sahrens rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 1035789Sahrens data = db->db.db_data; 10362445Sahrens bzero(data + blkoff, head); 1037789Sahrens } 10381544Seschrock dbuf_rele(db, FTAG); 1039789Sahrens } 1040789Sahrens off += head; 1041789Sahrens len -= head; 1042789Sahrens } 1043789Sahrens 10442445Sahrens /* If the range was less than one block, we're done */ 10456992Smaybee if (len == 0) 10466992Smaybee goto out; 10476992Smaybee 10486992Smaybee ASSERT(ISP2(blksz)); 10496992Smaybee /* If the remaining range is past end of file, we're done */ 10506992Smaybee if ((off >> blkshift) > dn->dn_maxblkid) 10516992Smaybee goto out; 10526992Smaybee 10536992Smaybee if (trunc) 10546992Smaybee tail = 0; 10556992Smaybee else 10566992Smaybee tail = P2PHASE(len, blksz); 10576992Smaybee 10586992Smaybee ASSERT3U(P2PHASE(off, blksz), ==, 0); 10596992Smaybee /* zero out any partial block data at the end of the range */ 10606992Smaybee if (tail) { 10616992Smaybee if (len < tail) 10626992Smaybee tail = len; 10636992Smaybee if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off+len), 10646992Smaybee TRUE, FTAG, &db) == 0) { 10656992Smaybee /* don't dirty if not on disk and not dirty */ 10666992Smaybee if (db->db_last_dirty || 10676992Smaybee (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) { 10686992Smaybee rw_exit(&dn->dn_struct_rwlock); 10696992Smaybee dbuf_will_dirty(db, tx); 10706992Smaybee rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 10716992Smaybee bzero(db->db.db_data, tail); 10726992Smaybee } 10736992Smaybee dbuf_rele(db, FTAG); 10746992Smaybee } 10756992Smaybee len -= tail; 10766992Smaybee } 10776992Smaybee 10786992Smaybee /* If the range did not include a full block, we are done */ 10796992Smaybee if (len == 0) 1080789Sahrens goto out; 1081789Sahrens 10826992Smaybee ASSERT(IS_P2ALIGNED(off, blksz)); 10836992Smaybee ASSERT(trunc || IS_P2ALIGNED(len, blksz)); 10846992Smaybee blkid = off >> blkshift; 10856992Smaybee nblks = len >> blkshift; 10866992Smaybee if (trunc) 10876992Smaybee nblks += 1; 10882445Sahrens 10896992Smaybee /* 10906992Smaybee * Read in and mark all the level-1 indirects dirty, 10916992Smaybee * so that they will stay in memory until syncing phase. 10927049Smaybee * Always dirty the first and last indirect to make sure 10937049Smaybee * we dirty all the partial indirects. 10946992Smaybee */ 10956992Smaybee if (dn->dn_nlevels > 1) { 10966992Smaybee uint64_t i, first, last; 10976992Smaybee int shift = epbs + dn->dn_datablkshift; 10982445Sahrens 10996992Smaybee first = blkid >> epbs; 11007049Smaybee if (db = dbuf_hold_level(dn, 1, first, FTAG)) { 11017049Smaybee dbuf_will_dirty(db, tx); 11027049Smaybee dbuf_rele(db, FTAG); 11037049Smaybee } 11046992Smaybee if (trunc) 11056992Smaybee last = dn->dn_maxblkid >> epbs; 11062445Sahrens else 11076992Smaybee last = (blkid + nblks - 1) >> epbs; 11087049Smaybee if (last > first && (db = dbuf_hold_level(dn, 1, last, FTAG))) { 11097049Smaybee dbuf_will_dirty(db, tx); 11107049Smaybee dbuf_rele(db, FTAG); 11117049Smaybee } 11127049Smaybee for (i = first + 1; i < last; i++) { 11136992Smaybee uint64_t ibyte = i << shift; 11146992Smaybee int err; 1115789Sahrens 11166992Smaybee err = dnode_next_offset(dn, 11176992Smaybee DNODE_FIND_HAVELOCK, &ibyte, 1, 1, 0); 11186992Smaybee i = ibyte >> shift; 11197049Smaybee if (err == ESRCH || i >= last) 11206992Smaybee break; 11216992Smaybee ASSERT(err == 0); 11226992Smaybee db = dbuf_hold_level(dn, 1, i, FTAG); 11236992Smaybee if (db) { 11246992Smaybee dbuf_will_dirty(db, tx); 11252445Sahrens dbuf_rele(db, FTAG); 1126789Sahrens } 11272445Sahrens } 11282445Sahrens } 11296992Smaybee done: 11306992Smaybee /* 11316992Smaybee * Add this range to the dnode range list. 11326992Smaybee * We will finish up this free operation in the syncing phase. 11336992Smaybee */ 1134789Sahrens mutex_enter(&dn->dn_mtx); 1135789Sahrens dnode_clear_range(dn, blkid, nblks, tx); 1136789Sahrens { 1137789Sahrens free_range_t *rp, *found; 1138789Sahrens avl_index_t where; 1139789Sahrens avl_tree_t *tree = &dn->dn_ranges[tx->tx_txg&TXG_MASK]; 1140789Sahrens 1141789Sahrens /* Add new range to dn_ranges */ 1142789Sahrens rp = kmem_alloc(sizeof (free_range_t), KM_SLEEP); 1143789Sahrens rp->fr_blkid = blkid; 1144789Sahrens rp->fr_nblks = nblks; 1145789Sahrens found = avl_find(tree, rp, &where); 1146789Sahrens ASSERT(found == NULL); 1147789Sahrens avl_insert(tree, rp, where); 1148789Sahrens dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n", 1149789Sahrens blkid, nblks, tx->tx_txg); 1150789Sahrens } 1151789Sahrens mutex_exit(&dn->dn_mtx); 1152789Sahrens 11536992Smaybee dbuf_free_range(dn, blkid, blkid + nblks - 1, tx); 1154789Sahrens dnode_setdirty(dn, tx); 1155789Sahrens out: 11566992Smaybee if (trunc && dn->dn_maxblkid >= (off >> blkshift)) 11576992Smaybee dn->dn_maxblkid = (off >> blkshift ? (off >> blkshift) - 1 : 0); 11586992Smaybee 1159789Sahrens rw_exit(&dn->dn_struct_rwlock); 1160789Sahrens } 1161789Sahrens 1162789Sahrens /* return TRUE if this blkid was freed in a recent txg, or FALSE if it wasn't */ 1163789Sahrens uint64_t 1164789Sahrens dnode_block_freed(dnode_t *dn, uint64_t blkid) 1165789Sahrens { 1166789Sahrens free_range_t range_tofind; 1167789Sahrens void *dp = spa_get_dsl(dn->dn_objset->os_spa); 1168789Sahrens int i; 1169789Sahrens 1170789Sahrens if (blkid == DB_BONUS_BLKID) 1171789Sahrens return (FALSE); 1172789Sahrens 1173789Sahrens /* 1174789Sahrens * If we're in the process of opening the pool, dp will not be 1175789Sahrens * set yet, but there shouldn't be anything dirty. 1176789Sahrens */ 1177789Sahrens if (dp == NULL) 1178789Sahrens return (FALSE); 1179789Sahrens 1180789Sahrens if (dn->dn_free_txg) 1181789Sahrens return (TRUE); 1182789Sahrens 1183789Sahrens /* 1184789Sahrens * If dn_datablkshift is not set, then there's only a single 1185789Sahrens * block, in which case there will never be a free range so it 1186789Sahrens * won't matter. 1187789Sahrens */ 1188789Sahrens range_tofind.fr_blkid = blkid; 1189789Sahrens mutex_enter(&dn->dn_mtx); 1190789Sahrens for (i = 0; i < TXG_SIZE; i++) { 1191789Sahrens free_range_t *range_found; 1192789Sahrens avl_index_t idx; 1193789Sahrens 1194789Sahrens range_found = avl_find(&dn->dn_ranges[i], &range_tofind, &idx); 1195789Sahrens if (range_found) { 1196789Sahrens ASSERT(range_found->fr_nblks > 0); 1197789Sahrens break; 1198789Sahrens } 1199789Sahrens range_found = avl_nearest(&dn->dn_ranges[i], idx, AVL_BEFORE); 1200789Sahrens if (range_found && 1201789Sahrens range_found->fr_blkid + range_found->fr_nblks > blkid) 1202789Sahrens break; 1203789Sahrens } 1204789Sahrens mutex_exit(&dn->dn_mtx); 1205789Sahrens return (i < TXG_SIZE); 1206789Sahrens } 1207789Sahrens 1208789Sahrens /* call from syncing context when we actually write/free space for this dnode */ 1209789Sahrens void 12102082Seschrock dnode_diduse_space(dnode_t *dn, int64_t delta) 1211789Sahrens { 12122082Seschrock uint64_t space; 12132082Seschrock dprintf_dnode(dn, "dn=%p dnp=%p used=%llu delta=%lld\n", 1214789Sahrens dn, dn->dn_phys, 12152082Seschrock (u_longlong_t)dn->dn_phys->dn_used, 12162082Seschrock (longlong_t)delta); 1217789Sahrens 1218789Sahrens mutex_enter(&dn->dn_mtx); 12192082Seschrock space = DN_USED_BYTES(dn->dn_phys); 12202082Seschrock if (delta > 0) { 12212082Seschrock ASSERT3U(space + delta, >=, space); /* no overflow */ 1222789Sahrens } else { 12232082Seschrock ASSERT3U(space, >=, -delta); /* no underflow */ 12242082Seschrock } 12252082Seschrock space += delta; 12264577Sahrens if (spa_version(dn->dn_objset->os_spa) < SPA_VERSION_DNODE_BYTES) { 12272082Seschrock ASSERT((dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) == 0); 12282082Seschrock ASSERT3U(P2PHASE(space, 1<<DEV_BSHIFT), ==, 0); 12292082Seschrock dn->dn_phys->dn_used = space >> DEV_BSHIFT; 12302082Seschrock } else { 12312082Seschrock dn->dn_phys->dn_used = space; 12322082Seschrock dn->dn_phys->dn_flags |= DNODE_FLAG_USED_BYTES; 1233789Sahrens } 1234789Sahrens mutex_exit(&dn->dn_mtx); 1235789Sahrens } 1236789Sahrens 1237789Sahrens /* 1238789Sahrens * Call when we think we're going to write/free space in open context. 1239789Sahrens * Be conservative (ie. OK to write less than this or free more than 1240789Sahrens * this, but don't write more or free less). 1241789Sahrens */ 1242789Sahrens void 1243789Sahrens dnode_willuse_space(dnode_t *dn, int64_t space, dmu_tx_t *tx) 1244789Sahrens { 1245789Sahrens objset_impl_t *os = dn->dn_objset; 1246789Sahrens dsl_dataset_t *ds = os->os_dsl_dataset; 1247789Sahrens 1248789Sahrens if (space > 0) 1249789Sahrens space = spa_get_asize(os->os_spa, space); 1250789Sahrens 1251789Sahrens if (ds) 1252789Sahrens dsl_dir_willuse_space(ds->ds_dir, space, tx); 1253789Sahrens 1254789Sahrens dmu_tx_willuse_space(tx, space); 1255789Sahrens } 1256789Sahrens 1257789Sahrens static int 12586992Smaybee dnode_next_offset_level(dnode_t *dn, int flags, uint64_t *offset, 12593025Sahrens int lvl, uint64_t blkfill, uint64_t txg) 1260789Sahrens { 1261789Sahrens dmu_buf_impl_t *db = NULL; 1262789Sahrens void *data = NULL; 1263789Sahrens uint64_t epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; 1264789Sahrens uint64_t epb = 1ULL << epbs; 1265789Sahrens uint64_t minfill, maxfill; 12666992Smaybee boolean_t hole; 12676992Smaybee int i, inc, error, span; 1268789Sahrens 1269789Sahrens dprintf("probing object %llu offset %llx level %d of %u\n", 1270789Sahrens dn->dn_object, *offset, lvl, dn->dn_phys->dn_nlevels); 1271789Sahrens 12726992Smaybee hole = flags & DNODE_FIND_HOLE; 12736992Smaybee inc = (flags & DNODE_FIND_BACKWARDS) ? -1 : 1; 12746992Smaybee 1275789Sahrens if (lvl == dn->dn_phys->dn_nlevels) { 1276789Sahrens error = 0; 1277789Sahrens epb = dn->dn_phys->dn_nblkptr; 1278789Sahrens data = dn->dn_phys->dn_blkptr; 1279789Sahrens } else { 1280789Sahrens uint64_t blkid = dbuf_whichblock(dn, *offset) >> (epbs * lvl); 1281789Sahrens error = dbuf_hold_impl(dn, lvl, blkid, TRUE, FTAG, &db); 1282789Sahrens if (error) { 1283789Sahrens if (error == ENOENT) 1284789Sahrens return (hole ? 0 : ESRCH); 1285789Sahrens return (error); 1286789Sahrens } 12871793Sahrens error = dbuf_read(db, NULL, DB_RF_CANFAIL | DB_RF_HAVESTRUCT); 12881793Sahrens if (error) { 12891793Sahrens dbuf_rele(db, FTAG); 12901793Sahrens return (error); 12911793Sahrens } 1292789Sahrens data = db->db.db_data; 1293789Sahrens } 1294789Sahrens 12953025Sahrens if (db && txg && 12963025Sahrens (db->db_blkptr == NULL || db->db_blkptr->blk_birth <= txg)) { 12973025Sahrens error = ESRCH; 12983025Sahrens } else if (lvl == 0) { 1299789Sahrens dnode_phys_t *dnp = data; 1300789Sahrens span = DNODE_SHIFT; 1301789Sahrens ASSERT(dn->dn_type == DMU_OT_DNODE); 1302789Sahrens 13036992Smaybee for (i = (*offset >> span) & (blkfill - 1); 13046992Smaybee i >= 0 && i < blkfill; i += inc) { 13053025Sahrens boolean_t newcontents = B_TRUE; 13063025Sahrens if (txg) { 13073025Sahrens int j; 13083025Sahrens newcontents = B_FALSE; 13093025Sahrens for (j = 0; j < dnp[i].dn_nblkptr; j++) { 13103025Sahrens if (dnp[i].dn_blkptr[j].blk_birth > txg) 13113025Sahrens newcontents = B_TRUE; 13123025Sahrens } 13133025Sahrens } 13143025Sahrens if (!dnp[i].dn_type == hole && newcontents) 1315789Sahrens break; 13166992Smaybee *offset += (1ULL << span) * inc; 1317789Sahrens } 13186992Smaybee if (i < 0 || i == blkfill) 1319789Sahrens error = ESRCH; 1320789Sahrens } else { 1321789Sahrens blkptr_t *bp = data; 1322789Sahrens span = (lvl - 1) * epbs + dn->dn_datablkshift; 1323789Sahrens minfill = 0; 1324789Sahrens maxfill = blkfill << ((lvl - 1) * epbs); 1325789Sahrens 1326789Sahrens if (hole) 1327789Sahrens maxfill--; 1328789Sahrens else 1329789Sahrens minfill++; 1330789Sahrens 1331789Sahrens for (i = (*offset >> span) & ((1ULL << epbs) - 1); 13326992Smaybee i >= 0 && i < epb; i += inc) { 1333789Sahrens if (bp[i].blk_fill >= minfill && 13343025Sahrens bp[i].blk_fill <= maxfill && 13353025Sahrens bp[i].blk_birth > txg) 1336789Sahrens break; 13376992Smaybee *offset += (1ULL << span) * inc; 1338789Sahrens } 13396992Smaybee if (i < 0 || i == epb) 1340789Sahrens error = ESRCH; 1341789Sahrens } 1342789Sahrens 1343789Sahrens if (db) 13441544Seschrock dbuf_rele(db, FTAG); 1345789Sahrens 1346789Sahrens return (error); 1347789Sahrens } 1348789Sahrens 1349789Sahrens /* 1350789Sahrens * Find the next hole, data, or sparse region at or after *offset. 1351789Sahrens * The value 'blkfill' tells us how many items we expect to find 1352789Sahrens * in an L0 data block; this value is 1 for normal objects, 1353789Sahrens * DNODES_PER_BLOCK for the meta dnode, and some fraction of 1354789Sahrens * DNODES_PER_BLOCK when searching for sparse regions thereof. 13553025Sahrens * 1356789Sahrens * Examples: 1357789Sahrens * 13586992Smaybee * dnode_next_offset(dn, flags, offset, 1, 1, 0); 13596992Smaybee * Finds the next/previous hole/data in a file. 1360789Sahrens * Used in dmu_offset_next(). 1361789Sahrens * 13626992Smaybee * dnode_next_offset(mdn, flags, offset, 0, DNODES_PER_BLOCK, txg); 1363789Sahrens * Finds the next free/allocated dnode an objset's meta-dnode. 13643025Sahrens * Only finds objects that have new contents since txg (ie. 13653025Sahrens * bonus buffer changes and content removal are ignored). 1366789Sahrens * Used in dmu_object_next(). 1367789Sahrens * 13686992Smaybee * dnode_next_offset(mdn, DNODE_FIND_HOLE, offset, 2, DNODES_PER_BLOCK >> 2, 0); 1369789Sahrens * Finds the next L2 meta-dnode bp that's at most 1/4 full. 1370789Sahrens * Used in dmu_object_alloc(). 1371789Sahrens */ 1372789Sahrens int 13736992Smaybee dnode_next_offset(dnode_t *dn, int flags, uint64_t *offset, 13743025Sahrens int minlvl, uint64_t blkfill, uint64_t txg) 1375789Sahrens { 13766992Smaybee uint64_t initial_offset = *offset; 1377789Sahrens int lvl, maxlvl; 1378789Sahrens int error = 0; 1379789Sahrens 13806992Smaybee if (!(flags & DNODE_FIND_HAVELOCK)) 13816992Smaybee rw_enter(&dn->dn_struct_rwlock, RW_READER); 1382789Sahrens 1383789Sahrens if (dn->dn_phys->dn_nlevels == 0) { 13846992Smaybee error = ESRCH; 13856992Smaybee goto out; 1386789Sahrens } 1387789Sahrens 1388789Sahrens if (dn->dn_datablkshift == 0) { 1389789Sahrens if (*offset < dn->dn_datablksz) { 13906992Smaybee if (flags & DNODE_FIND_HOLE) 1391789Sahrens *offset = dn->dn_datablksz; 1392789Sahrens } else { 1393789Sahrens error = ESRCH; 1394789Sahrens } 13956992Smaybee goto out; 1396789Sahrens } 1397789Sahrens 1398789Sahrens maxlvl = dn->dn_phys->dn_nlevels; 1399789Sahrens 1400789Sahrens for (lvl = minlvl; lvl <= maxlvl; lvl++) { 14013025Sahrens error = dnode_next_offset_level(dn, 14026992Smaybee flags, offset, lvl, blkfill, txg); 14031793Sahrens if (error != ESRCH) 1404789Sahrens break; 1405789Sahrens } 1406789Sahrens 14076992Smaybee while (error == 0 && --lvl >= minlvl) { 14083025Sahrens error = dnode_next_offset_level(dn, 14096992Smaybee flags, offset, lvl, blkfill, txg); 14103025Sahrens } 1411789Sahrens 14126992Smaybee if (error == 0 && (flags & DNODE_FIND_BACKWARDS ? 14136992Smaybee initial_offset < *offset : initial_offset > *offset)) 14141793Sahrens error = ESRCH; 14156992Smaybee out: 14166992Smaybee if (!(flags & DNODE_FIND_HAVELOCK)) 14176992Smaybee rw_exit(&dn->dn_struct_rwlock); 1418789Sahrens 1419789Sahrens return (error); 1420789Sahrens } 1421