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 /* 228582SBrendan.Gregg@Sun.COM * Copyright 2009 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); 598214SRicardo.M.Correia@Sun.COM cv_init(&dn->dn_notxholds, NULL, CV_DEFAULT, NULL); 608214SRicardo.M.Correia@Sun.COM 61789Sahrens refcount_create(&dn->dn_holds); 62789Sahrens refcount_create(&dn->dn_tx_holds); 63789Sahrens 64789Sahrens for (i = 0; i < TXG_SIZE; i++) { 65789Sahrens avl_create(&dn->dn_ranges[i], free_range_compar, 66789Sahrens sizeof (free_range_t), 67789Sahrens offsetof(struct free_range, fr_node)); 683547Smaybee list_create(&dn->dn_dirty_records[i], 693547Smaybee sizeof (dbuf_dirty_record_t), 703547Smaybee offsetof(dbuf_dirty_record_t, dr_dirty_node)); 71789Sahrens } 72789Sahrens 73789Sahrens list_create(&dn->dn_dbufs, sizeof (dmu_buf_impl_t), 74789Sahrens offsetof(dmu_buf_impl_t, db_link)); 75789Sahrens 76789Sahrens return (0); 77789Sahrens } 78789Sahrens 79789Sahrens /* ARGSUSED */ 80789Sahrens static void 81789Sahrens dnode_dest(void *arg, void *unused) 82789Sahrens { 83789Sahrens int i; 84789Sahrens dnode_t *dn = arg; 85789Sahrens 86789Sahrens rw_destroy(&dn->dn_struct_rwlock); 87789Sahrens mutex_destroy(&dn->dn_mtx); 88789Sahrens mutex_destroy(&dn->dn_dbufs_mtx); 898214SRicardo.M.Correia@Sun.COM cv_destroy(&dn->dn_notxholds); 90789Sahrens refcount_destroy(&dn->dn_holds); 91789Sahrens refcount_destroy(&dn->dn_tx_holds); 92789Sahrens 93789Sahrens for (i = 0; i < TXG_SIZE; i++) { 94789Sahrens avl_destroy(&dn->dn_ranges[i]); 953547Smaybee list_destroy(&dn->dn_dirty_records[i]); 96789Sahrens } 97789Sahrens 98789Sahrens list_destroy(&dn->dn_dbufs); 99789Sahrens } 100789Sahrens 101789Sahrens void 102789Sahrens dnode_init(void) 103789Sahrens { 104789Sahrens dnode_cache = kmem_cache_create("dnode_t", 105789Sahrens sizeof (dnode_t), 106789Sahrens 0, dnode_cons, dnode_dest, NULL, NULL, NULL, 0); 107789Sahrens } 108789Sahrens 109789Sahrens void 110789Sahrens dnode_fini(void) 111789Sahrens { 112789Sahrens kmem_cache_destroy(dnode_cache); 113789Sahrens } 114789Sahrens 115789Sahrens 116873Sek110237 #ifdef ZFS_DEBUG 117789Sahrens void 118789Sahrens dnode_verify(dnode_t *dn) 119789Sahrens { 120789Sahrens int drop_struct_lock = FALSE; 121789Sahrens 122789Sahrens ASSERT(dn->dn_phys); 123789Sahrens ASSERT(dn->dn_objset); 124789Sahrens 125789Sahrens ASSERT(dn->dn_phys->dn_type < DMU_OT_NUMTYPES); 126789Sahrens 127789Sahrens if (!(zfs_flags & ZFS_DEBUG_DNODE_VERIFY)) 128789Sahrens return; 129789Sahrens 130789Sahrens if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) { 131789Sahrens rw_enter(&dn->dn_struct_rwlock, RW_READER); 132789Sahrens drop_struct_lock = TRUE; 133789Sahrens } 134789Sahrens if (dn->dn_phys->dn_type != DMU_OT_NONE || dn->dn_allocated_txg != 0) { 135789Sahrens int i; 136789Sahrens ASSERT3U(dn->dn_indblkshift, >=, 0); 137789Sahrens ASSERT3U(dn->dn_indblkshift, <=, SPA_MAXBLOCKSHIFT); 138789Sahrens if (dn->dn_datablkshift) { 139789Sahrens ASSERT3U(dn->dn_datablkshift, >=, SPA_MINBLOCKSHIFT); 140789Sahrens ASSERT3U(dn->dn_datablkshift, <=, SPA_MAXBLOCKSHIFT); 141789Sahrens ASSERT3U(1<<dn->dn_datablkshift, ==, dn->dn_datablksz); 142789Sahrens } 143789Sahrens ASSERT3U(dn->dn_nlevels, <=, 30); 144789Sahrens ASSERT3U(dn->dn_type, <=, DMU_OT_NUMTYPES); 145789Sahrens ASSERT3U(dn->dn_nblkptr, >=, 1); 146789Sahrens ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR); 147789Sahrens ASSERT3U(dn->dn_bonuslen, <=, DN_MAX_BONUSLEN); 148789Sahrens ASSERT3U(dn->dn_datablksz, ==, 149789Sahrens dn->dn_datablkszsec << SPA_MINBLOCKSHIFT); 150789Sahrens ASSERT3U(ISP2(dn->dn_datablksz), ==, dn->dn_datablkshift != 0); 151789Sahrens ASSERT3U((dn->dn_nblkptr - 1) * sizeof (blkptr_t) + 152789Sahrens dn->dn_bonuslen, <=, DN_MAX_BONUSLEN); 153789Sahrens for (i = 0; i < TXG_SIZE; i++) { 154789Sahrens ASSERT3U(dn->dn_next_nlevels[i], <=, dn->dn_nlevels); 155789Sahrens } 156789Sahrens } 157789Sahrens if (dn->dn_phys->dn_type != DMU_OT_NONE) 158789Sahrens ASSERT3U(dn->dn_phys->dn_nlevels, <=, dn->dn_nlevels); 1591544Seschrock ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT || dn->dn_dbuf != NULL); 160789Sahrens if (dn->dn_dbuf != NULL) { 161789Sahrens ASSERT3P(dn->dn_phys, ==, 162789Sahrens (dnode_phys_t *)dn->dn_dbuf->db.db_data + 163789Sahrens (dn->dn_object % (dn->dn_dbuf->db.db_size >> DNODE_SHIFT))); 164789Sahrens } 165789Sahrens if (drop_struct_lock) 166789Sahrens rw_exit(&dn->dn_struct_rwlock); 167873Sek110237 } 168789Sahrens #endif 169789Sahrens 170789Sahrens void 171789Sahrens dnode_byteswap(dnode_phys_t *dnp) 172789Sahrens { 173789Sahrens uint64_t *buf64 = (void*)&dnp->dn_blkptr; 174789Sahrens int i; 175789Sahrens 176789Sahrens if (dnp->dn_type == DMU_OT_NONE) { 177789Sahrens bzero(dnp, sizeof (dnode_phys_t)); 178789Sahrens return; 179789Sahrens } 180789Sahrens 181789Sahrens dnp->dn_datablkszsec = BSWAP_16(dnp->dn_datablkszsec); 182789Sahrens dnp->dn_bonuslen = BSWAP_16(dnp->dn_bonuslen); 183789Sahrens dnp->dn_maxblkid = BSWAP_64(dnp->dn_maxblkid); 1842082Seschrock dnp->dn_used = BSWAP_64(dnp->dn_used); 185789Sahrens 186789Sahrens /* 187789Sahrens * dn_nblkptr is only one byte, so it's OK to read it in either 188789Sahrens * byte order. We can't read dn_bouslen. 189789Sahrens */ 190789Sahrens ASSERT(dnp->dn_indblkshift <= SPA_MAXBLOCKSHIFT); 191789Sahrens ASSERT(dnp->dn_nblkptr <= DN_MAX_NBLKPTR); 192789Sahrens for (i = 0; i < dnp->dn_nblkptr * sizeof (blkptr_t)/8; i++) 193789Sahrens buf64[i] = BSWAP_64(buf64[i]); 194789Sahrens 195789Sahrens /* 196789Sahrens * OK to check dn_bonuslen for zero, because it won't matter if 197789Sahrens * we have the wrong byte order. This is necessary because the 198789Sahrens * dnode dnode is smaller than a regular dnode. 199789Sahrens */ 200789Sahrens if (dnp->dn_bonuslen != 0) { 201789Sahrens /* 202789Sahrens * Note that the bonus length calculated here may be 203789Sahrens * longer than the actual bonus buffer. This is because 204789Sahrens * we always put the bonus buffer after the last block 205789Sahrens * pointer (instead of packing it against the end of the 206789Sahrens * dnode buffer). 207789Sahrens */ 208789Sahrens int off = (dnp->dn_nblkptr-1) * sizeof (blkptr_t); 209789Sahrens size_t len = DN_MAX_BONUSLEN - off; 2103882Sahrens ASSERT3U(dnp->dn_bonustype, <, DMU_OT_NUMTYPES); 211789Sahrens dmu_ot[dnp->dn_bonustype].ot_byteswap(dnp->dn_bonus + off, len); 212789Sahrens } 213789Sahrens } 214789Sahrens 215789Sahrens void 216789Sahrens dnode_buf_byteswap(void *vbuf, size_t size) 217789Sahrens { 218789Sahrens dnode_phys_t *buf = vbuf; 219789Sahrens int i; 220789Sahrens 221789Sahrens ASSERT3U(sizeof (dnode_phys_t), ==, (1<<DNODE_SHIFT)); 222789Sahrens ASSERT((size & (sizeof (dnode_phys_t)-1)) == 0); 223789Sahrens 224789Sahrens size >>= DNODE_SHIFT; 225789Sahrens for (i = 0; i < size; i++) { 226789Sahrens dnode_byteswap(buf); 227789Sahrens buf++; 228789Sahrens } 229789Sahrens } 230789Sahrens 231789Sahrens static int 232789Sahrens free_range_compar(const void *node1, const void *node2) 233789Sahrens { 234789Sahrens const free_range_t *rp1 = node1; 235789Sahrens const free_range_t *rp2 = node2; 236789Sahrens 237789Sahrens if (rp1->fr_blkid < rp2->fr_blkid) 238789Sahrens return (-1); 239789Sahrens else if (rp1->fr_blkid > rp2->fr_blkid) 240789Sahrens return (1); 241789Sahrens else return (0); 242789Sahrens } 243789Sahrens 2444944Smaybee void 2454944Smaybee dnode_setbonuslen(dnode_t *dn, int newsize, dmu_tx_t *tx) 2464944Smaybee { 2474944Smaybee ASSERT3U(refcount_count(&dn->dn_holds), >=, 1); 2484944Smaybee 2494944Smaybee dnode_setdirty(dn, tx); 2504944Smaybee rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 2514944Smaybee ASSERT3U(newsize, <=, DN_MAX_BONUSLEN - 2524944Smaybee (dn->dn_nblkptr-1) * sizeof (blkptr_t)); 2534944Smaybee dn->dn_bonuslen = newsize; 2544944Smaybee if (newsize == 0) 2554944Smaybee dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = DN_ZERO_BONUSLEN; 2564944Smaybee else 2574944Smaybee dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen; 2584944Smaybee rw_exit(&dn->dn_struct_rwlock); 2594944Smaybee } 2604944Smaybee 261789Sahrens static void 262789Sahrens dnode_setdblksz(dnode_t *dn, int size) 263789Sahrens { 264789Sahrens ASSERT3U(P2PHASE(size, SPA_MINBLOCKSIZE), ==, 0); 265789Sahrens ASSERT3U(size, <=, SPA_MAXBLOCKSIZE); 266789Sahrens ASSERT3U(size, >=, SPA_MINBLOCKSIZE); 267789Sahrens ASSERT3U(size >> SPA_MINBLOCKSHIFT, <, 268789Sahrens 1<<(sizeof (dn->dn_phys->dn_datablkszsec) * 8)); 269789Sahrens dn->dn_datablksz = size; 270789Sahrens dn->dn_datablkszsec = size >> SPA_MINBLOCKSHIFT; 271789Sahrens dn->dn_datablkshift = ISP2(size) ? highbit(size - 1) : 0; 272789Sahrens } 273789Sahrens 274789Sahrens static dnode_t * 275789Sahrens dnode_create(objset_impl_t *os, dnode_phys_t *dnp, dmu_buf_impl_t *db, 276789Sahrens uint64_t object) 277789Sahrens { 278789Sahrens dnode_t *dn = kmem_cache_alloc(dnode_cache, KM_SLEEP); 279789Sahrens (void) dnode_cons(dn, NULL, 0); /* XXX */ 280789Sahrens 281789Sahrens dn->dn_objset = os; 282789Sahrens dn->dn_object = object; 283789Sahrens dn->dn_dbuf = db; 284789Sahrens dn->dn_phys = dnp; 285789Sahrens 286789Sahrens if (dnp->dn_datablkszsec) 287789Sahrens dnode_setdblksz(dn, dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT); 288789Sahrens dn->dn_indblkshift = dnp->dn_indblkshift; 289789Sahrens dn->dn_nlevels = dnp->dn_nlevels; 290789Sahrens dn->dn_type = dnp->dn_type; 291789Sahrens dn->dn_nblkptr = dnp->dn_nblkptr; 292789Sahrens dn->dn_checksum = dnp->dn_checksum; 293789Sahrens dn->dn_compress = dnp->dn_compress; 294789Sahrens dn->dn_bonustype = dnp->dn_bonustype; 295789Sahrens dn->dn_bonuslen = dnp->dn_bonuslen; 296789Sahrens dn->dn_maxblkid = dnp->dn_maxblkid; 297789Sahrens 298789Sahrens dmu_zfetch_init(&dn->dn_zfetch, dn); 299789Sahrens 300789Sahrens ASSERT(dn->dn_phys->dn_type < DMU_OT_NUMTYPES); 301789Sahrens mutex_enter(&os->os_lock); 302789Sahrens list_insert_head(&os->os_dnodes, dn); 303789Sahrens mutex_exit(&os->os_lock); 304789Sahrens 3058582SBrendan.Gregg@Sun.COM arc_space_consume(sizeof (dnode_t), ARC_SPACE_OTHER); 306789Sahrens return (dn); 307789Sahrens } 308789Sahrens 309789Sahrens static void 310789Sahrens dnode_destroy(dnode_t *dn) 311789Sahrens { 312789Sahrens objset_impl_t *os = dn->dn_objset; 313789Sahrens 3142885Sahrens #ifdef ZFS_DEBUG 3152885Sahrens int i; 3162885Sahrens 3172885Sahrens for (i = 0; i < TXG_SIZE; i++) { 3182885Sahrens ASSERT(!list_link_active(&dn->dn_dirty_link[i])); 3193547Smaybee ASSERT(NULL == list_head(&dn->dn_dirty_records[i])); 3202885Sahrens ASSERT(0 == avl_numnodes(&dn->dn_ranges[i])); 3212885Sahrens } 3222885Sahrens ASSERT(NULL == list_head(&dn->dn_dbufs)); 3232885Sahrens #endif 3242885Sahrens 325789Sahrens mutex_enter(&os->os_lock); 326789Sahrens list_remove(&os->os_dnodes, dn); 327789Sahrens mutex_exit(&os->os_lock); 328789Sahrens 329789Sahrens if (dn->dn_dirtyctx_firstset) { 330789Sahrens kmem_free(dn->dn_dirtyctx_firstset, 1); 331789Sahrens dn->dn_dirtyctx_firstset = NULL; 332789Sahrens } 333789Sahrens dmu_zfetch_rele(&dn->dn_zfetch); 3341544Seschrock if (dn->dn_bonus) { 3351544Seschrock mutex_enter(&dn->dn_bonus->db_mtx); 3361544Seschrock dbuf_evict(dn->dn_bonus); 3371544Seschrock dn->dn_bonus = NULL; 3381544Seschrock } 339789Sahrens kmem_cache_free(dnode_cache, dn); 3408582SBrendan.Gregg@Sun.COM arc_space_return(sizeof (dnode_t), ARC_SPACE_OTHER); 341789Sahrens } 342789Sahrens 343789Sahrens void 344789Sahrens dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs, 3451599Sahrens dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 346789Sahrens { 347789Sahrens int i; 348789Sahrens 349789Sahrens if (blocksize == 0) 350789Sahrens blocksize = 1 << zfs_default_bs; 3511402Sahrens else if (blocksize > SPA_MAXBLOCKSIZE) 3521402Sahrens blocksize = SPA_MAXBLOCKSIZE; 3531402Sahrens else 3541402Sahrens blocksize = P2ROUNDUP(blocksize, SPA_MINBLOCKSIZE); 355789Sahrens 356789Sahrens if (ibs == 0) 357789Sahrens ibs = zfs_default_ibs; 358789Sahrens 359789Sahrens ibs = MIN(MAX(ibs, DN_MIN_INDBLKSHIFT), DN_MAX_INDBLKSHIFT); 360789Sahrens 361789Sahrens dprintf("os=%p obj=%llu txg=%llu blocksize=%d ibs=%d\n", dn->dn_objset, 362789Sahrens dn->dn_object, tx->tx_txg, blocksize, ibs); 363789Sahrens 364789Sahrens ASSERT(dn->dn_type == DMU_OT_NONE); 365789Sahrens ASSERT(bcmp(dn->dn_phys, &dnode_phys_zero, sizeof (dnode_phys_t)) == 0); 366789Sahrens ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE); 367789Sahrens ASSERT(ot != DMU_OT_NONE); 368789Sahrens ASSERT3U(ot, <, DMU_OT_NUMTYPES); 369789Sahrens ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) || 370789Sahrens (bonustype != DMU_OT_NONE && bonuslen != 0)); 371789Sahrens ASSERT3U(bonustype, <, DMU_OT_NUMTYPES); 372789Sahrens ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN); 373789Sahrens ASSERT(dn->dn_type == DMU_OT_NONE); 374789Sahrens ASSERT3U(dn->dn_maxblkid, ==, 0); 375789Sahrens ASSERT3U(dn->dn_allocated_txg, ==, 0); 376789Sahrens ASSERT3U(dn->dn_assigned_txg, ==, 0); 377789Sahrens ASSERT(refcount_is_zero(&dn->dn_tx_holds)); 378789Sahrens ASSERT3U(refcount_count(&dn->dn_holds), <=, 1); 379789Sahrens ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL); 380789Sahrens 381789Sahrens for (i = 0; i < TXG_SIZE; i++) { 382789Sahrens ASSERT3U(dn->dn_next_nlevels[i], ==, 0); 383789Sahrens ASSERT3U(dn->dn_next_indblkshift[i], ==, 0); 3844944Smaybee ASSERT3U(dn->dn_next_bonuslen[i], ==, 0); 3851596Sahrens ASSERT3U(dn->dn_next_blksz[i], ==, 0); 3861596Sahrens ASSERT(!list_link_active(&dn->dn_dirty_link[i])); 3873547Smaybee ASSERT3P(list_head(&dn->dn_dirty_records[i]), ==, NULL); 388789Sahrens ASSERT3U(avl_numnodes(&dn->dn_ranges[i]), ==, 0); 389789Sahrens } 390789Sahrens 391789Sahrens dn->dn_type = ot; 392789Sahrens dnode_setdblksz(dn, blocksize); 393789Sahrens dn->dn_indblkshift = ibs; 394789Sahrens dn->dn_nlevels = 1; 395789Sahrens dn->dn_nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT); 396789Sahrens dn->dn_bonustype = bonustype; 397789Sahrens dn->dn_bonuslen = bonuslen; 398789Sahrens dn->dn_checksum = ZIO_CHECKSUM_INHERIT; 399789Sahrens dn->dn_compress = ZIO_COMPRESS_INHERIT; 400789Sahrens dn->dn_dirtyctx = 0; 401789Sahrens 402789Sahrens dn->dn_free_txg = 0; 403789Sahrens if (dn->dn_dirtyctx_firstset) { 404789Sahrens kmem_free(dn->dn_dirtyctx_firstset, 1); 405789Sahrens dn->dn_dirtyctx_firstset = NULL; 406789Sahrens } 407789Sahrens 408789Sahrens dn->dn_allocated_txg = tx->tx_txg; 4091599Sahrens 410789Sahrens dnode_setdirty(dn, tx); 4111599Sahrens dn->dn_next_indblkshift[tx->tx_txg & TXG_MASK] = ibs; 4124944Smaybee dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen; 4131599Sahrens dn->dn_next_blksz[tx->tx_txg & TXG_MASK] = dn->dn_datablksz; 414789Sahrens } 415789Sahrens 416789Sahrens void 417789Sahrens dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, 418789Sahrens dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 419789Sahrens { 420*8986SMark.Maybee@Sun.COM int nblkptr; 4211596Sahrens 422789Sahrens ASSERT3U(blocksize, >=, SPA_MINBLOCKSIZE); 423789Sahrens ASSERT3U(blocksize, <=, SPA_MAXBLOCKSIZE); 424789Sahrens ASSERT3U(blocksize % SPA_MINBLOCKSIZE, ==, 0); 4251544Seschrock ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx)); 426789Sahrens ASSERT(tx->tx_txg != 0); 427789Sahrens ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) || 428789Sahrens (bonustype != DMU_OT_NONE && bonuslen != 0)); 429789Sahrens ASSERT3U(bonustype, <, DMU_OT_NUMTYPES); 430789Sahrens ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN); 4311596Sahrens 4321544Seschrock /* clean up any unreferenced dbufs */ 4334944Smaybee dnode_evict_dbufs(dn); 4341544Seschrock 435*8986SMark.Maybee@Sun.COM rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 436*8986SMark.Maybee@Sun.COM dnode_setdirty(dn, tx); 437*8986SMark.Maybee@Sun.COM if (dn->dn_datablksz != blocksize) { 438*8986SMark.Maybee@Sun.COM /* change blocksize */ 439*8986SMark.Maybee@Sun.COM ASSERT(dn->dn_maxblkid == 0 && 440*8986SMark.Maybee@Sun.COM (BP_IS_HOLE(&dn->dn_phys->dn_blkptr[0]) || 441*8986SMark.Maybee@Sun.COM dnode_block_freed(dn, 0))); 442*8986SMark.Maybee@Sun.COM dnode_setdblksz(dn, blocksize); 443*8986SMark.Maybee@Sun.COM dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = blocksize; 444789Sahrens } 445*8986SMark.Maybee@Sun.COM if (dn->dn_bonuslen != bonuslen) 446*8986SMark.Maybee@Sun.COM dn->dn_next_bonuslen[tx->tx_txg&TXG_MASK] = bonuslen; 4478644SMark.Maybee@Sun.COM nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT); 4488644SMark.Maybee@Sun.COM if (dn->dn_nblkptr != nblkptr) 4498644SMark.Maybee@Sun.COM dn->dn_next_nblkptr[tx->tx_txg&TXG_MASK] = nblkptr; 450789Sahrens rw_exit(&dn->dn_struct_rwlock); 451789Sahrens 452789Sahrens /* change type */ 453789Sahrens dn->dn_type = ot; 454789Sahrens 455789Sahrens /* change bonus size and type */ 456789Sahrens mutex_enter(&dn->dn_mtx); 457789Sahrens dn->dn_bonustype = bonustype; 458789Sahrens dn->dn_bonuslen = bonuslen; 4598644SMark.Maybee@Sun.COM dn->dn_nblkptr = nblkptr; 460789Sahrens dn->dn_checksum = ZIO_CHECKSUM_INHERIT; 461789Sahrens dn->dn_compress = ZIO_COMPRESS_INHERIT; 462789Sahrens ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR); 463789Sahrens 4648644SMark.Maybee@Sun.COM /* fix up the bonus db_size */ 4658644SMark.Maybee@Sun.COM if (dn->dn_bonus) { 4664944Smaybee dn->dn_bonus->db.db_size = 4674944Smaybee DN_MAX_BONUSLEN - (dn->dn_nblkptr-1) * sizeof (blkptr_t); 4684944Smaybee ASSERT(dn->dn_bonuslen <= dn->dn_bonus->db.db_size); 4694944Smaybee } 4703087Sahrens 471789Sahrens dn->dn_allocated_txg = tx->tx_txg; 472789Sahrens mutex_exit(&dn->dn_mtx); 473789Sahrens } 474789Sahrens 475789Sahrens void 476789Sahrens dnode_special_close(dnode_t *dn) 477789Sahrens { 4781544Seschrock /* 4791544Seschrock * Wait for final references to the dnode to clear. This can 4801544Seschrock * only happen if the arc is asyncronously evicting state that 4811544Seschrock * has a hold on this dnode while we are trying to evict this 4821544Seschrock * dnode. 4831544Seschrock */ 4841544Seschrock while (refcount_count(&dn->dn_holds) > 0) 4851544Seschrock delay(1); 486789Sahrens dnode_destroy(dn); 487789Sahrens } 488789Sahrens 489789Sahrens dnode_t * 490789Sahrens dnode_special_open(objset_impl_t *os, dnode_phys_t *dnp, uint64_t object) 491789Sahrens { 492789Sahrens dnode_t *dn = dnode_create(os, dnp, NULL, object); 493873Sek110237 DNODE_VERIFY(dn); 494789Sahrens return (dn); 495789Sahrens } 496789Sahrens 497789Sahrens static void 498789Sahrens dnode_buf_pageout(dmu_buf_t *db, void *arg) 499789Sahrens { 500789Sahrens dnode_t **children_dnodes = arg; 501789Sahrens int i; 502789Sahrens int epb = db->db_size >> DNODE_SHIFT; 503789Sahrens 504789Sahrens for (i = 0; i < epb; i++) { 505789Sahrens dnode_t *dn = children_dnodes[i]; 506789Sahrens int n; 507789Sahrens 508789Sahrens if (dn == NULL) 509789Sahrens continue; 510789Sahrens #ifdef ZFS_DEBUG 511789Sahrens /* 512789Sahrens * If there are holds on this dnode, then there should 513789Sahrens * be holds on the dnode's containing dbuf as well; thus 514789Sahrens * it wouldn't be eligable for eviction and this function 515789Sahrens * would not have been called. 516789Sahrens */ 517789Sahrens ASSERT(refcount_is_zero(&dn->dn_holds)); 518789Sahrens ASSERT(list_head(&dn->dn_dbufs) == NULL); 519789Sahrens ASSERT(refcount_is_zero(&dn->dn_tx_holds)); 520789Sahrens 521789Sahrens for (n = 0; n < TXG_SIZE; n++) 5221596Sahrens ASSERT(!list_link_active(&dn->dn_dirty_link[n])); 523789Sahrens #endif 524789Sahrens children_dnodes[i] = NULL; 525789Sahrens dnode_destroy(dn); 526789Sahrens } 527789Sahrens kmem_free(children_dnodes, epb * sizeof (dnode_t *)); 528789Sahrens } 529789Sahrens 530789Sahrens /* 5311544Seschrock * errors: 5321544Seschrock * EINVAL - invalid object number. 5331544Seschrock * EIO - i/o error. 5341544Seschrock * succeeds even for free dnodes. 535789Sahrens */ 5361544Seschrock int 5371544Seschrock dnode_hold_impl(objset_impl_t *os, uint64_t object, int flag, 5381544Seschrock void *tag, dnode_t **dnp) 539789Sahrens { 5401544Seschrock int epb, idx, err; 541789Sahrens int drop_struct_lock = FALSE; 5421544Seschrock int type; 543789Sahrens uint64_t blk; 544789Sahrens dnode_t *mdn, *dn; 545789Sahrens dmu_buf_impl_t *db; 546789Sahrens dnode_t **children_dnodes; 547789Sahrens 5487754SJeff.Bonwick@Sun.COM /* 5497754SJeff.Bonwick@Sun.COM * If you are holding the spa config lock as writer, you shouldn't 5507754SJeff.Bonwick@Sun.COM * be asking the DMU to do *anything*. 5517754SJeff.Bonwick@Sun.COM */ 5527754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(os->os_spa, SCL_ALL, RW_WRITER) == 0); 5537754SJeff.Bonwick@Sun.COM 554789Sahrens if (object == 0 || object >= DN_MAX_OBJECT) 5551544Seschrock return (EINVAL); 556789Sahrens 557789Sahrens mdn = os->os_meta_dnode; 558789Sahrens 559873Sek110237 DNODE_VERIFY(mdn); 560789Sahrens 561789Sahrens if (!RW_WRITE_HELD(&mdn->dn_struct_rwlock)) { 562789Sahrens rw_enter(&mdn->dn_struct_rwlock, RW_READER); 563789Sahrens drop_struct_lock = TRUE; 564789Sahrens } 565789Sahrens 566789Sahrens blk = dbuf_whichblock(mdn, object * sizeof (dnode_phys_t)); 567789Sahrens 5681544Seschrock db = dbuf_hold(mdn, blk, FTAG); 569789Sahrens if (drop_struct_lock) 570789Sahrens rw_exit(&mdn->dn_struct_rwlock); 5711544Seschrock if (db == NULL) 5721544Seschrock return (EIO); 5731544Seschrock err = dbuf_read(db, NULL, DB_RF_CANFAIL); 5741544Seschrock if (err) { 5751544Seschrock dbuf_rele(db, FTAG); 5761544Seschrock return (err); 5771544Seschrock } 578789Sahrens 579789Sahrens ASSERT3U(db->db.db_size, >=, 1<<DNODE_SHIFT); 580789Sahrens epb = db->db.db_size >> DNODE_SHIFT; 581789Sahrens 582789Sahrens idx = object & (epb-1); 583789Sahrens 584789Sahrens children_dnodes = dmu_buf_get_user(&db->db); 585789Sahrens if (children_dnodes == NULL) { 586789Sahrens dnode_t **winner; 587789Sahrens children_dnodes = kmem_zalloc(epb * sizeof (dnode_t *), 588789Sahrens KM_SLEEP); 589789Sahrens if (winner = dmu_buf_set_user(&db->db, children_dnodes, NULL, 590789Sahrens dnode_buf_pageout)) { 591789Sahrens kmem_free(children_dnodes, epb * sizeof (dnode_t *)); 592789Sahrens children_dnodes = winner; 593789Sahrens } 594789Sahrens } 595789Sahrens 596789Sahrens if ((dn = children_dnodes[idx]) == NULL) { 5974309Smaybee dnode_phys_t *dnp = (dnode_phys_t *)db->db.db_data+idx; 598789Sahrens dnode_t *winner; 5994309Smaybee 6004309Smaybee dn = dnode_create(os, dnp, db, object); 601789Sahrens winner = atomic_cas_ptr(&children_dnodes[idx], NULL, dn); 602789Sahrens if (winner != NULL) { 603789Sahrens dnode_destroy(dn); 604789Sahrens dn = winner; 605789Sahrens } 606789Sahrens } 607789Sahrens 608789Sahrens mutex_enter(&dn->dn_mtx); 6091544Seschrock type = dn->dn_type; 610789Sahrens if (dn->dn_free_txg || 6111544Seschrock ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE) || 6121544Seschrock ((flag & DNODE_MUST_BE_FREE) && type != DMU_OT_NONE)) { 613789Sahrens mutex_exit(&dn->dn_mtx); 6141544Seschrock dbuf_rele(db, FTAG); 6151544Seschrock return (type == DMU_OT_NONE ? ENOENT : EEXIST); 616789Sahrens } 617789Sahrens mutex_exit(&dn->dn_mtx); 618789Sahrens 6191544Seschrock if (refcount_add(&dn->dn_holds, tag) == 1) 620789Sahrens dbuf_add_ref(db, dn); 621789Sahrens 622873Sek110237 DNODE_VERIFY(dn); 623789Sahrens ASSERT3P(dn->dn_dbuf, ==, db); 624789Sahrens ASSERT3U(dn->dn_object, ==, object); 6251544Seschrock dbuf_rele(db, FTAG); 626789Sahrens 6271544Seschrock *dnp = dn; 6281544Seschrock return (0); 629789Sahrens } 630789Sahrens 631789Sahrens /* 632789Sahrens * Return held dnode if the object is allocated, NULL if not. 633789Sahrens */ 6341544Seschrock int 6351544Seschrock dnode_hold(objset_impl_t *os, uint64_t object, void *tag, dnode_t **dnp) 636789Sahrens { 6371544Seschrock return (dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED, tag, dnp)); 638789Sahrens } 639789Sahrens 6404944Smaybee /* 6414944Smaybee * Can only add a reference if there is already at least one 6424944Smaybee * reference on the dnode. Returns FALSE if unable to add a 6434944Smaybee * new reference. 6444944Smaybee */ 6454944Smaybee boolean_t 6461544Seschrock dnode_add_ref(dnode_t *dn, void *tag) 647789Sahrens { 6484944Smaybee mutex_enter(&dn->dn_mtx); 6494944Smaybee if (refcount_is_zero(&dn->dn_holds)) { 6504944Smaybee mutex_exit(&dn->dn_mtx); 6514944Smaybee return (FALSE); 6524944Smaybee } 6534944Smaybee VERIFY(1 < refcount_add(&dn->dn_holds, tag)); 6544944Smaybee mutex_exit(&dn->dn_mtx); 6554944Smaybee return (TRUE); 656789Sahrens } 657789Sahrens 658789Sahrens void 6591544Seschrock dnode_rele(dnode_t *dn, void *tag) 660789Sahrens { 661789Sahrens uint64_t refs; 662789Sahrens 6634944Smaybee mutex_enter(&dn->dn_mtx); 6641544Seschrock refs = refcount_remove(&dn->dn_holds, tag); 6654944Smaybee mutex_exit(&dn->dn_mtx); 666789Sahrens /* NOTE: the DNODE_DNODE does not have a dn_dbuf */ 667789Sahrens if (refs == 0 && dn->dn_dbuf) 6681544Seschrock dbuf_rele(dn->dn_dbuf, dn); 669789Sahrens } 670789Sahrens 671789Sahrens void 672789Sahrens dnode_setdirty(dnode_t *dn, dmu_tx_t *tx) 673789Sahrens { 674789Sahrens objset_impl_t *os = dn->dn_objset; 675789Sahrens uint64_t txg = tx->tx_txg; 676789Sahrens 6771544Seschrock if (dn->dn_object == DMU_META_DNODE_OBJECT) 678789Sahrens return; 679789Sahrens 680873Sek110237 DNODE_VERIFY(dn); 681789Sahrens 682789Sahrens #ifdef ZFS_DEBUG 683789Sahrens mutex_enter(&dn->dn_mtx); 684789Sahrens ASSERT(dn->dn_phys->dn_type || dn->dn_allocated_txg); 685789Sahrens /* ASSERT(dn->dn_free_txg == 0 || dn->dn_free_txg >= txg); */ 686789Sahrens mutex_exit(&dn->dn_mtx); 687789Sahrens #endif 688789Sahrens 689789Sahrens mutex_enter(&os->os_lock); 690789Sahrens 691789Sahrens /* 692789Sahrens * If we are already marked dirty, we're done. 693789Sahrens */ 6941596Sahrens if (list_link_active(&dn->dn_dirty_link[txg & TXG_MASK])) { 695789Sahrens mutex_exit(&os->os_lock); 696789Sahrens return; 697789Sahrens } 698789Sahrens 699789Sahrens ASSERT(!refcount_is_zero(&dn->dn_holds) || list_head(&dn->dn_dbufs)); 700789Sahrens ASSERT(dn->dn_datablksz != 0); 7014944Smaybee ASSERT3U(dn->dn_next_bonuslen[txg&TXG_MASK], ==, 0); 7021599Sahrens ASSERT3U(dn->dn_next_blksz[txg&TXG_MASK], ==, 0); 703789Sahrens 704789Sahrens dprintf_ds(os->os_dsl_dataset, "obj=%llu txg=%llu\n", 705789Sahrens dn->dn_object, txg); 706789Sahrens 707789Sahrens if (dn->dn_free_txg > 0 && dn->dn_free_txg <= txg) { 708789Sahrens list_insert_tail(&os->os_free_dnodes[txg&TXG_MASK], dn); 709789Sahrens } else { 710789Sahrens list_insert_tail(&os->os_dirty_dnodes[txg&TXG_MASK], dn); 711789Sahrens } 712789Sahrens 713789Sahrens mutex_exit(&os->os_lock); 714789Sahrens 715789Sahrens /* 716789Sahrens * The dnode maintains a hold on its containing dbuf as 717789Sahrens * long as there are holds on it. Each instantiated child 718789Sahrens * dbuf maintaines a hold on the dnode. When the last child 719789Sahrens * drops its hold, the dnode will drop its hold on the 720789Sahrens * containing dbuf. We add a "dirty hold" here so that the 721789Sahrens * dnode will hang around after we finish processing its 722789Sahrens * children. 723789Sahrens */ 7244944Smaybee VERIFY(dnode_add_ref(dn, (void *)(uintptr_t)tx->tx_txg)); 725789Sahrens 7263547Smaybee (void) dbuf_dirty(dn->dn_dbuf, tx); 727789Sahrens 728789Sahrens dsl_dataset_dirty(os->os_dsl_dataset, tx); 729789Sahrens } 730789Sahrens 731789Sahrens void 732789Sahrens dnode_free(dnode_t *dn, dmu_tx_t *tx) 733789Sahrens { 7341596Sahrens int txgoff = tx->tx_txg & TXG_MASK; 7351596Sahrens 736789Sahrens dprintf("dn=%p txg=%llu\n", dn, tx->tx_txg); 737789Sahrens 738789Sahrens /* we should be the only holder... hopefully */ 739789Sahrens /* ASSERT3U(refcount_count(&dn->dn_holds), ==, 1); */ 740789Sahrens 741789Sahrens mutex_enter(&dn->dn_mtx); 742789Sahrens if (dn->dn_type == DMU_OT_NONE || dn->dn_free_txg) { 743789Sahrens mutex_exit(&dn->dn_mtx); 744789Sahrens return; 745789Sahrens } 746789Sahrens dn->dn_free_txg = tx->tx_txg; 747789Sahrens mutex_exit(&dn->dn_mtx); 748789Sahrens 749789Sahrens /* 750789Sahrens * If the dnode is already dirty, it needs to be moved from 751789Sahrens * the dirty list to the free list. 752789Sahrens */ 753789Sahrens mutex_enter(&dn->dn_objset->os_lock); 7541596Sahrens if (list_link_active(&dn->dn_dirty_link[txgoff])) { 7551596Sahrens list_remove(&dn->dn_objset->os_dirty_dnodes[txgoff], dn); 7561596Sahrens list_insert_tail(&dn->dn_objset->os_free_dnodes[txgoff], dn); 757789Sahrens mutex_exit(&dn->dn_objset->os_lock); 758789Sahrens } else { 759789Sahrens mutex_exit(&dn->dn_objset->os_lock); 760789Sahrens dnode_setdirty(dn, tx); 761789Sahrens } 762789Sahrens } 763789Sahrens 764789Sahrens /* 765789Sahrens * Try to change the block size for the indicated dnode. This can only 766789Sahrens * succeed if there are no blocks allocated or dirty beyond first block 767789Sahrens */ 768789Sahrens int 769789Sahrens dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx) 770789Sahrens { 771789Sahrens dmu_buf_impl_t *db, *db_next; 7726992Smaybee int err; 773789Sahrens 774789Sahrens if (size == 0) 775789Sahrens size = SPA_MINBLOCKSIZE; 776789Sahrens if (size > SPA_MAXBLOCKSIZE) 777789Sahrens size = SPA_MAXBLOCKSIZE; 778789Sahrens else 779789Sahrens size = P2ROUNDUP(size, SPA_MINBLOCKSIZE); 780789Sahrens 7812445Sahrens if (ibs == dn->dn_indblkshift) 7822445Sahrens ibs = 0; 783789Sahrens 7842445Sahrens if (size >> SPA_MINBLOCKSHIFT == dn->dn_datablkszsec && ibs == 0) 785789Sahrens return (0); 786789Sahrens 787789Sahrens rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 788789Sahrens 789789Sahrens /* Check for any allocated blocks beyond the first */ 790789Sahrens if (dn->dn_phys->dn_maxblkid != 0) 7912445Sahrens goto fail; 792789Sahrens 793789Sahrens mutex_enter(&dn->dn_dbufs_mtx); 794789Sahrens for (db = list_head(&dn->dn_dbufs); db; db = db_next) { 795789Sahrens db_next = list_next(&dn->dn_dbufs, db); 796789Sahrens 7976992Smaybee if (db->db_blkid != 0 && db->db_blkid != DB_BONUS_BLKID) { 798789Sahrens mutex_exit(&dn->dn_dbufs_mtx); 7992445Sahrens goto fail; 800789Sahrens } 801789Sahrens } 802789Sahrens mutex_exit(&dn->dn_dbufs_mtx); 803789Sahrens 8042445Sahrens if (ibs && dn->dn_nlevels != 1) 8052445Sahrens goto fail; 8062445Sahrens 8076992Smaybee /* resize the old block */ 8086992Smaybee err = dbuf_hold_impl(dn, 0, 0, TRUE, FTAG, &db); 8096992Smaybee if (err == 0) 8101596Sahrens dbuf_new_size(db, size, tx); 8116992Smaybee else if (err != ENOENT) 8126992Smaybee goto fail; 813789Sahrens 814789Sahrens dnode_setdblksz(dn, size); 8151596Sahrens dnode_setdirty(dn, tx); 8161596Sahrens dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = size; 8172445Sahrens if (ibs) { 8182445Sahrens dn->dn_indblkshift = ibs; 8192445Sahrens dn->dn_next_indblkshift[tx->tx_txg&TXG_MASK] = ibs; 8202445Sahrens } 8216992Smaybee /* rele after we have fixed the blocksize in the dnode */ 8221596Sahrens if (db) 8231596Sahrens dbuf_rele(db, FTAG); 824789Sahrens 825789Sahrens rw_exit(&dn->dn_struct_rwlock); 8262445Sahrens return (0); 8272445Sahrens 8282445Sahrens fail: 8292445Sahrens rw_exit(&dn->dn_struct_rwlock); 8302445Sahrens return (ENOTSUP); 831789Sahrens } 832789Sahrens 8337332SJonathan.Adams@Sun.COM /* read-holding callers must not rely on the lock being continuously held */ 834789Sahrens void 8357332SJonathan.Adams@Sun.COM dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx, boolean_t have_read) 836789Sahrens { 837789Sahrens uint64_t txgoff = tx->tx_txg & TXG_MASK; 8381596Sahrens int epbs, new_nlevels; 839789Sahrens uint64_t sz; 840789Sahrens 8411596Sahrens ASSERT(blkid != DB_BONUS_BLKID); 842789Sahrens 8437332SJonathan.Adams@Sun.COM ASSERT(have_read ? 8447332SJonathan.Adams@Sun.COM RW_READ_HELD(&dn->dn_struct_rwlock) : 8457332SJonathan.Adams@Sun.COM RW_WRITE_HELD(&dn->dn_struct_rwlock)); 8467332SJonathan.Adams@Sun.COM 8477332SJonathan.Adams@Sun.COM /* 8487332SJonathan.Adams@Sun.COM * if we have a read-lock, check to see if we need to do any work 8497332SJonathan.Adams@Sun.COM * before upgrading to a write-lock. 8507332SJonathan.Adams@Sun.COM */ 8517332SJonathan.Adams@Sun.COM if (have_read) { 8527332SJonathan.Adams@Sun.COM if (blkid <= dn->dn_maxblkid) 8537332SJonathan.Adams@Sun.COM return; 8547332SJonathan.Adams@Sun.COM 8557332SJonathan.Adams@Sun.COM if (!rw_tryupgrade(&dn->dn_struct_rwlock)) { 8567332SJonathan.Adams@Sun.COM rw_exit(&dn->dn_struct_rwlock); 8577332SJonathan.Adams@Sun.COM rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 8587332SJonathan.Adams@Sun.COM } 859789Sahrens } 860789Sahrens 8611596Sahrens if (blkid <= dn->dn_maxblkid) 8621596Sahrens goto out; 8631596Sahrens 8641596Sahrens dn->dn_maxblkid = blkid; 865789Sahrens 866789Sahrens /* 8671596Sahrens * Compute the number of levels necessary to support the new maxblkid. 868789Sahrens */ 869789Sahrens new_nlevels = 1; 870789Sahrens epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 8711596Sahrens for (sz = dn->dn_nblkptr; 8721596Sahrens sz <= blkid && sz >= dn->dn_nblkptr; sz <<= epbs) 873789Sahrens new_nlevels++; 874789Sahrens 8751596Sahrens if (new_nlevels > dn->dn_nlevels) { 8761596Sahrens int old_nlevels = dn->dn_nlevels; 8771596Sahrens dmu_buf_impl_t *db; 8783547Smaybee list_t *list; 8793547Smaybee dbuf_dirty_record_t *new, *dr, *dr_next; 8801596Sahrens 8811596Sahrens dn->dn_nlevels = new_nlevels; 8821596Sahrens 8831596Sahrens ASSERT3U(new_nlevels, >, dn->dn_next_nlevels[txgoff]); 884789Sahrens dn->dn_next_nlevels[txgoff] = new_nlevels; 885789Sahrens 8863547Smaybee /* dirty the left indirects */ 8871596Sahrens db = dbuf_hold_level(dn, old_nlevels, 0, FTAG); 8883547Smaybee new = dbuf_dirty(db, tx); 8891544Seschrock dbuf_rele(db, FTAG); 8901596Sahrens 8913547Smaybee /* transfer the dirty records to the new indirect */ 8923547Smaybee mutex_enter(&dn->dn_mtx); 8933547Smaybee mutex_enter(&new->dt.di.dr_mtx); 8943547Smaybee list = &dn->dn_dirty_records[txgoff]; 8953547Smaybee for (dr = list_head(list); dr; dr = dr_next) { 8963547Smaybee dr_next = list_next(&dn->dn_dirty_records[txgoff], dr); 8973547Smaybee if (dr->dr_dbuf->db_level != new_nlevels-1 && 8983547Smaybee dr->dr_dbuf->db_blkid != DB_BONUS_BLKID) { 8993547Smaybee ASSERT(dr->dr_dbuf->db_level == old_nlevels-1); 9003547Smaybee list_remove(&dn->dn_dirty_records[txgoff], dr); 9013547Smaybee list_insert_tail(&new->dt.di.dr_children, dr); 9023547Smaybee dr->dr_parent = new; 9033547Smaybee } 9043547Smaybee } 9053547Smaybee mutex_exit(&new->dt.di.dr_mtx); 9063547Smaybee mutex_exit(&dn->dn_mtx); 907789Sahrens } 908789Sahrens 909789Sahrens out: 9107332SJonathan.Adams@Sun.COM if (have_read) 9117332SJonathan.Adams@Sun.COM rw_downgrade(&dn->dn_struct_rwlock); 912789Sahrens } 913789Sahrens 914789Sahrens void 915789Sahrens dnode_clear_range(dnode_t *dn, uint64_t blkid, uint64_t nblks, dmu_tx_t *tx) 916789Sahrens { 917789Sahrens avl_tree_t *tree = &dn->dn_ranges[tx->tx_txg&TXG_MASK]; 918789Sahrens avl_index_t where; 919789Sahrens free_range_t *rp; 920789Sahrens free_range_t rp_tofind; 921789Sahrens uint64_t endblk = blkid + nblks; 922789Sahrens 923789Sahrens ASSERT(MUTEX_HELD(&dn->dn_mtx)); 924789Sahrens ASSERT(nblks <= UINT64_MAX - blkid); /* no overflow */ 925789Sahrens 926789Sahrens dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n", 927789Sahrens blkid, nblks, tx->tx_txg); 928789Sahrens rp_tofind.fr_blkid = blkid; 929789Sahrens rp = avl_find(tree, &rp_tofind, &where); 930789Sahrens if (rp == NULL) 931789Sahrens rp = avl_nearest(tree, where, AVL_BEFORE); 932789Sahrens if (rp == NULL) 933789Sahrens rp = avl_nearest(tree, where, AVL_AFTER); 934789Sahrens 935789Sahrens while (rp && (rp->fr_blkid <= blkid + nblks)) { 936789Sahrens uint64_t fr_endblk = rp->fr_blkid + rp->fr_nblks; 937789Sahrens free_range_t *nrp = AVL_NEXT(tree, rp); 938789Sahrens 939789Sahrens if (blkid <= rp->fr_blkid && endblk >= fr_endblk) { 940789Sahrens /* clear this entire range */ 941789Sahrens avl_remove(tree, rp); 942789Sahrens kmem_free(rp, sizeof (free_range_t)); 943789Sahrens } else if (blkid <= rp->fr_blkid && 944789Sahrens endblk > rp->fr_blkid && endblk < fr_endblk) { 945789Sahrens /* clear the beginning of this range */ 946789Sahrens rp->fr_blkid = endblk; 947789Sahrens rp->fr_nblks = fr_endblk - endblk; 948789Sahrens } else if (blkid > rp->fr_blkid && blkid < fr_endblk && 949789Sahrens endblk >= fr_endblk) { 950789Sahrens /* clear the end of this range */ 951789Sahrens rp->fr_nblks = blkid - rp->fr_blkid; 952789Sahrens } else if (blkid > rp->fr_blkid && endblk < fr_endblk) { 953789Sahrens /* clear a chunk out of this range */ 954789Sahrens free_range_t *new_rp = 955789Sahrens kmem_alloc(sizeof (free_range_t), KM_SLEEP); 956789Sahrens 957789Sahrens new_rp->fr_blkid = endblk; 958789Sahrens new_rp->fr_nblks = fr_endblk - endblk; 959789Sahrens avl_insert_here(tree, new_rp, rp, AVL_AFTER); 960789Sahrens rp->fr_nblks = blkid - rp->fr_blkid; 961789Sahrens } 962789Sahrens /* there may be no overlap */ 963789Sahrens rp = nrp; 964789Sahrens } 965789Sahrens } 966789Sahrens 967789Sahrens void 968789Sahrens dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx) 969789Sahrens { 970789Sahrens dmu_buf_impl_t *db; 9712445Sahrens uint64_t blkoff, blkid, nblks; 9726992Smaybee int blksz, blkshift, head, tail; 973789Sahrens int trunc = FALSE; 9746992Smaybee int epbs; 975789Sahrens 976789Sahrens rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 977789Sahrens blksz = dn->dn_datablksz; 9786992Smaybee blkshift = dn->dn_datablkshift; 9796992Smaybee epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 980789Sahrens 981789Sahrens if (len == -1ULL) { 982789Sahrens len = UINT64_MAX - off; 983789Sahrens trunc = TRUE; 984789Sahrens } 985789Sahrens 986789Sahrens /* 987789Sahrens * First, block align the region to free: 988789Sahrens */ 9892445Sahrens if (ISP2(blksz)) { 9902445Sahrens head = P2NPHASE(off, blksz); 9912445Sahrens blkoff = P2PHASE(off, blksz); 9926992Smaybee if ((off >> blkshift) > dn->dn_maxblkid) 9936992Smaybee goto out; 9942445Sahrens } else { 9952445Sahrens ASSERT(dn->dn_maxblkid == 0); 9962445Sahrens if (off == 0 && len >= blksz) { 9976992Smaybee /* Freeing the whole block; fast-track this request */ 9986992Smaybee blkid = 0; 9996992Smaybee nblks = 1; 10006992Smaybee goto done; 10017385SMark.Maybee@Sun.COM } else if (off >= blksz) { 10026992Smaybee /* Freeing past end-of-data */ 10036992Smaybee goto out; 1004789Sahrens } else { 10052445Sahrens /* Freeing part of the block. */ 1006789Sahrens head = blksz - off; 1007789Sahrens ASSERT3U(head, >, 0); 1008789Sahrens } 10092445Sahrens blkoff = off; 1010789Sahrens } 1011789Sahrens /* zero out any partial block data at the start of the range */ 1012789Sahrens if (head) { 10132445Sahrens ASSERT3U(blkoff + head, ==, blksz); 1014789Sahrens if (len < head) 1015789Sahrens head = len; 1016789Sahrens if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off), TRUE, 1017789Sahrens FTAG, &db) == 0) { 1018789Sahrens caddr_t data; 1019789Sahrens 1020789Sahrens /* don't dirty if it isn't on disk and isn't dirty */ 10213547Smaybee if (db->db_last_dirty || 1022789Sahrens (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) { 1023789Sahrens rw_exit(&dn->dn_struct_rwlock); 1024789Sahrens dbuf_will_dirty(db, tx); 1025789Sahrens rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 1026789Sahrens data = db->db.db_data; 10272445Sahrens bzero(data + blkoff, head); 1028789Sahrens } 10291544Seschrock dbuf_rele(db, FTAG); 1030789Sahrens } 1031789Sahrens off += head; 1032789Sahrens len -= head; 1033789Sahrens } 1034789Sahrens 10352445Sahrens /* If the range was less than one block, we're done */ 10366992Smaybee if (len == 0) 10376992Smaybee goto out; 10386992Smaybee 10396992Smaybee /* If the remaining range is past end of file, we're done */ 10406992Smaybee if ((off >> blkshift) > dn->dn_maxblkid) 10416992Smaybee goto out; 10426992Smaybee 10437385SMark.Maybee@Sun.COM ASSERT(ISP2(blksz)); 10446992Smaybee if (trunc) 10456992Smaybee tail = 0; 10466992Smaybee else 10476992Smaybee tail = P2PHASE(len, blksz); 10486992Smaybee 10496992Smaybee ASSERT3U(P2PHASE(off, blksz), ==, 0); 10506992Smaybee /* zero out any partial block data at the end of the range */ 10516992Smaybee if (tail) { 10526992Smaybee if (len < tail) 10536992Smaybee tail = len; 10546992Smaybee if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off+len), 10556992Smaybee TRUE, FTAG, &db) == 0) { 10566992Smaybee /* don't dirty if not on disk and not dirty */ 10576992Smaybee if (db->db_last_dirty || 10586992Smaybee (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) { 10596992Smaybee rw_exit(&dn->dn_struct_rwlock); 10606992Smaybee dbuf_will_dirty(db, tx); 10616992Smaybee rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 10626992Smaybee bzero(db->db.db_data, tail); 10636992Smaybee } 10646992Smaybee dbuf_rele(db, FTAG); 10656992Smaybee } 10666992Smaybee len -= tail; 10676992Smaybee } 10686992Smaybee 10696992Smaybee /* If the range did not include a full block, we are done */ 10706992Smaybee if (len == 0) 1071789Sahrens goto out; 1072789Sahrens 10736992Smaybee ASSERT(IS_P2ALIGNED(off, blksz)); 10746992Smaybee ASSERT(trunc || IS_P2ALIGNED(len, blksz)); 10756992Smaybee blkid = off >> blkshift; 10766992Smaybee nblks = len >> blkshift; 10776992Smaybee if (trunc) 10786992Smaybee nblks += 1; 10792445Sahrens 10806992Smaybee /* 10816992Smaybee * Read in and mark all the level-1 indirects dirty, 10826992Smaybee * so that they will stay in memory until syncing phase. 10837049Smaybee * Always dirty the first and last indirect to make sure 10847049Smaybee * we dirty all the partial indirects. 10856992Smaybee */ 10866992Smaybee if (dn->dn_nlevels > 1) { 10876992Smaybee uint64_t i, first, last; 10886992Smaybee int shift = epbs + dn->dn_datablkshift; 10892445Sahrens 10906992Smaybee first = blkid >> epbs; 10917049Smaybee if (db = dbuf_hold_level(dn, 1, first, FTAG)) { 10927049Smaybee dbuf_will_dirty(db, tx); 10937049Smaybee dbuf_rele(db, FTAG); 10947049Smaybee } 10956992Smaybee if (trunc) 10966992Smaybee last = dn->dn_maxblkid >> epbs; 10972445Sahrens else 10986992Smaybee last = (blkid + nblks - 1) >> epbs; 10997049Smaybee if (last > first && (db = dbuf_hold_level(dn, 1, last, FTAG))) { 11007049Smaybee dbuf_will_dirty(db, tx); 11017049Smaybee dbuf_rele(db, FTAG); 11027049Smaybee } 11037049Smaybee for (i = first + 1; i < last; i++) { 11046992Smaybee uint64_t ibyte = i << shift; 11056992Smaybee int err; 1106789Sahrens 11076992Smaybee err = dnode_next_offset(dn, 11086992Smaybee DNODE_FIND_HAVELOCK, &ibyte, 1, 1, 0); 11096992Smaybee i = ibyte >> shift; 11107049Smaybee if (err == ESRCH || i >= last) 11116992Smaybee break; 11126992Smaybee ASSERT(err == 0); 11136992Smaybee db = dbuf_hold_level(dn, 1, i, FTAG); 11146992Smaybee if (db) { 11156992Smaybee dbuf_will_dirty(db, tx); 11162445Sahrens dbuf_rele(db, FTAG); 1117789Sahrens } 11182445Sahrens } 11192445Sahrens } 11206992Smaybee done: 11216992Smaybee /* 11226992Smaybee * Add this range to the dnode range list. 11236992Smaybee * We will finish up this free operation in the syncing phase. 11246992Smaybee */ 1125789Sahrens mutex_enter(&dn->dn_mtx); 1126789Sahrens dnode_clear_range(dn, blkid, nblks, tx); 1127789Sahrens { 1128789Sahrens free_range_t *rp, *found; 1129789Sahrens avl_index_t where; 1130789Sahrens avl_tree_t *tree = &dn->dn_ranges[tx->tx_txg&TXG_MASK]; 1131789Sahrens 1132789Sahrens /* Add new range to dn_ranges */ 1133789Sahrens rp = kmem_alloc(sizeof (free_range_t), KM_SLEEP); 1134789Sahrens rp->fr_blkid = blkid; 1135789Sahrens rp->fr_nblks = nblks; 1136789Sahrens found = avl_find(tree, rp, &where); 1137789Sahrens ASSERT(found == NULL); 1138789Sahrens avl_insert(tree, rp, where); 1139789Sahrens dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n", 1140789Sahrens blkid, nblks, tx->tx_txg); 1141789Sahrens } 1142789Sahrens mutex_exit(&dn->dn_mtx); 1143789Sahrens 11446992Smaybee dbuf_free_range(dn, blkid, blkid + nblks - 1, tx); 1145789Sahrens dnode_setdirty(dn, tx); 1146789Sahrens out: 11476992Smaybee if (trunc && dn->dn_maxblkid >= (off >> blkshift)) 11486992Smaybee dn->dn_maxblkid = (off >> blkshift ? (off >> blkshift) - 1 : 0); 11496992Smaybee 1150789Sahrens rw_exit(&dn->dn_struct_rwlock); 1151789Sahrens } 1152789Sahrens 1153789Sahrens /* return TRUE if this blkid was freed in a recent txg, or FALSE if it wasn't */ 1154789Sahrens uint64_t 1155789Sahrens dnode_block_freed(dnode_t *dn, uint64_t blkid) 1156789Sahrens { 1157789Sahrens free_range_t range_tofind; 1158789Sahrens void *dp = spa_get_dsl(dn->dn_objset->os_spa); 1159789Sahrens int i; 1160789Sahrens 1161789Sahrens if (blkid == DB_BONUS_BLKID) 1162789Sahrens return (FALSE); 1163789Sahrens 1164789Sahrens /* 1165789Sahrens * If we're in the process of opening the pool, dp will not be 1166789Sahrens * set yet, but there shouldn't be anything dirty. 1167789Sahrens */ 1168789Sahrens if (dp == NULL) 1169789Sahrens return (FALSE); 1170789Sahrens 1171789Sahrens if (dn->dn_free_txg) 1172789Sahrens return (TRUE); 1173789Sahrens 1174789Sahrens range_tofind.fr_blkid = blkid; 1175789Sahrens mutex_enter(&dn->dn_mtx); 1176789Sahrens for (i = 0; i < TXG_SIZE; i++) { 1177789Sahrens free_range_t *range_found; 1178789Sahrens avl_index_t idx; 1179789Sahrens 1180789Sahrens range_found = avl_find(&dn->dn_ranges[i], &range_tofind, &idx); 1181789Sahrens if (range_found) { 1182789Sahrens ASSERT(range_found->fr_nblks > 0); 1183789Sahrens break; 1184789Sahrens } 1185789Sahrens range_found = avl_nearest(&dn->dn_ranges[i], idx, AVL_BEFORE); 1186789Sahrens if (range_found && 1187789Sahrens range_found->fr_blkid + range_found->fr_nblks > blkid) 1188789Sahrens break; 1189789Sahrens } 1190789Sahrens mutex_exit(&dn->dn_mtx); 1191789Sahrens return (i < TXG_SIZE); 1192789Sahrens } 1193789Sahrens 1194789Sahrens /* call from syncing context when we actually write/free space for this dnode */ 1195789Sahrens void 11962082Seschrock dnode_diduse_space(dnode_t *dn, int64_t delta) 1197789Sahrens { 11982082Seschrock uint64_t space; 11992082Seschrock dprintf_dnode(dn, "dn=%p dnp=%p used=%llu delta=%lld\n", 1200789Sahrens dn, dn->dn_phys, 12012082Seschrock (u_longlong_t)dn->dn_phys->dn_used, 12022082Seschrock (longlong_t)delta); 1203789Sahrens 1204789Sahrens mutex_enter(&dn->dn_mtx); 12052082Seschrock space = DN_USED_BYTES(dn->dn_phys); 12062082Seschrock if (delta > 0) { 12072082Seschrock ASSERT3U(space + delta, >=, space); /* no overflow */ 1208789Sahrens } else { 12092082Seschrock ASSERT3U(space, >=, -delta); /* no underflow */ 12102082Seschrock } 12112082Seschrock space += delta; 12124577Sahrens if (spa_version(dn->dn_objset->os_spa) < SPA_VERSION_DNODE_BYTES) { 12132082Seschrock ASSERT((dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) == 0); 12142082Seschrock ASSERT3U(P2PHASE(space, 1<<DEV_BSHIFT), ==, 0); 12152082Seschrock dn->dn_phys->dn_used = space >> DEV_BSHIFT; 12162082Seschrock } else { 12172082Seschrock dn->dn_phys->dn_used = space; 12182082Seschrock dn->dn_phys->dn_flags |= DNODE_FLAG_USED_BYTES; 1219789Sahrens } 1220789Sahrens mutex_exit(&dn->dn_mtx); 1221789Sahrens } 1222789Sahrens 1223789Sahrens /* 1224789Sahrens * Call when we think we're going to write/free space in open context. 1225789Sahrens * Be conservative (ie. OK to write less than this or free more than 1226789Sahrens * this, but don't write more or free less). 1227789Sahrens */ 1228789Sahrens void 1229789Sahrens dnode_willuse_space(dnode_t *dn, int64_t space, dmu_tx_t *tx) 1230789Sahrens { 1231789Sahrens objset_impl_t *os = dn->dn_objset; 1232789Sahrens dsl_dataset_t *ds = os->os_dsl_dataset; 1233789Sahrens 1234789Sahrens if (space > 0) 1235789Sahrens space = spa_get_asize(os->os_spa, space); 1236789Sahrens 1237789Sahrens if (ds) 1238789Sahrens dsl_dir_willuse_space(ds->ds_dir, space, tx); 1239789Sahrens 1240789Sahrens dmu_tx_willuse_space(tx, space); 1241789Sahrens } 1242789Sahrens 1243789Sahrens static int 12446992Smaybee dnode_next_offset_level(dnode_t *dn, int flags, uint64_t *offset, 12453025Sahrens int lvl, uint64_t blkfill, uint64_t txg) 1246789Sahrens { 1247789Sahrens dmu_buf_impl_t *db = NULL; 1248789Sahrens void *data = NULL; 1249789Sahrens uint64_t epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; 1250789Sahrens uint64_t epb = 1ULL << epbs; 1251789Sahrens uint64_t minfill, maxfill; 12526992Smaybee boolean_t hole; 12536992Smaybee int i, inc, error, span; 1254789Sahrens 1255789Sahrens dprintf("probing object %llu offset %llx level %d of %u\n", 1256789Sahrens dn->dn_object, *offset, lvl, dn->dn_phys->dn_nlevels); 1257789Sahrens 12586992Smaybee hole = flags & DNODE_FIND_HOLE; 12596992Smaybee inc = (flags & DNODE_FIND_BACKWARDS) ? -1 : 1; 12607385SMark.Maybee@Sun.COM ASSERT(txg == 0 || !hole); 12616992Smaybee 1262789Sahrens if (lvl == dn->dn_phys->dn_nlevels) { 1263789Sahrens error = 0; 1264789Sahrens epb = dn->dn_phys->dn_nblkptr; 1265789Sahrens data = dn->dn_phys->dn_blkptr; 1266789Sahrens } else { 1267789Sahrens uint64_t blkid = dbuf_whichblock(dn, *offset) >> (epbs * lvl); 1268789Sahrens error = dbuf_hold_impl(dn, lvl, blkid, TRUE, FTAG, &db); 1269789Sahrens if (error) { 12707385SMark.Maybee@Sun.COM if (error != ENOENT) 12717385SMark.Maybee@Sun.COM return (error); 12727385SMark.Maybee@Sun.COM if (hole) 12737385SMark.Maybee@Sun.COM return (0); 12747385SMark.Maybee@Sun.COM /* 12757385SMark.Maybee@Sun.COM * This can only happen when we are searching up 12767385SMark.Maybee@Sun.COM * the block tree for data. We don't really need to 12777385SMark.Maybee@Sun.COM * adjust the offset, as we will just end up looking 12787385SMark.Maybee@Sun.COM * at the pointer to this block in its parent, and its 12797385SMark.Maybee@Sun.COM * going to be unallocated, so we will skip over it. 12807385SMark.Maybee@Sun.COM */ 12817385SMark.Maybee@Sun.COM return (ESRCH); 1282789Sahrens } 12831793Sahrens error = dbuf_read(db, NULL, DB_RF_CANFAIL | DB_RF_HAVESTRUCT); 12841793Sahrens if (error) { 12851793Sahrens dbuf_rele(db, FTAG); 12861793Sahrens return (error); 12871793Sahrens } 1288789Sahrens data = db->db.db_data; 1289789Sahrens } 1290789Sahrens 12913025Sahrens if (db && txg && 12923025Sahrens (db->db_blkptr == NULL || db->db_blkptr->blk_birth <= txg)) { 12937385SMark.Maybee@Sun.COM /* 12947385SMark.Maybee@Sun.COM * This can only happen when we are searching up the tree 12957385SMark.Maybee@Sun.COM * and these conditions mean that we need to keep climbing. 12967385SMark.Maybee@Sun.COM */ 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 && 13357385SMark.Maybee@Sun.COM (hole || bp[i].blk_birth > txg)) 1336789Sahrens break; 13377385SMark.Maybee@Sun.COM if (inc < 0 && *offset < (1ULL << span)) 13387385SMark.Maybee@Sun.COM *offset = 0; 13397385SMark.Maybee@Sun.COM else 13407385SMark.Maybee@Sun.COM *offset += (1ULL << span) * inc; 1341789Sahrens } 13426992Smaybee if (i < 0 || i == epb) 1343789Sahrens error = ESRCH; 1344789Sahrens } 1345789Sahrens 1346789Sahrens if (db) 13471544Seschrock dbuf_rele(db, FTAG); 1348789Sahrens 1349789Sahrens return (error); 1350789Sahrens } 1351789Sahrens 1352789Sahrens /* 1353789Sahrens * Find the next hole, data, or sparse region at or after *offset. 1354789Sahrens * The value 'blkfill' tells us how many items we expect to find 1355789Sahrens * in an L0 data block; this value is 1 for normal objects, 1356789Sahrens * DNODES_PER_BLOCK for the meta dnode, and some fraction of 1357789Sahrens * DNODES_PER_BLOCK when searching for sparse regions thereof. 13583025Sahrens * 1359789Sahrens * Examples: 1360789Sahrens * 13616992Smaybee * dnode_next_offset(dn, flags, offset, 1, 1, 0); 13626992Smaybee * Finds the next/previous hole/data in a file. 1363789Sahrens * Used in dmu_offset_next(). 1364789Sahrens * 13656992Smaybee * dnode_next_offset(mdn, flags, offset, 0, DNODES_PER_BLOCK, txg); 1366789Sahrens * Finds the next free/allocated dnode an objset's meta-dnode. 13673025Sahrens * Only finds objects that have new contents since txg (ie. 13683025Sahrens * bonus buffer changes and content removal are ignored). 1369789Sahrens * Used in dmu_object_next(). 1370789Sahrens * 13716992Smaybee * dnode_next_offset(mdn, DNODE_FIND_HOLE, offset, 2, DNODES_PER_BLOCK >> 2, 0); 1372789Sahrens * Finds the next L2 meta-dnode bp that's at most 1/4 full. 1373789Sahrens * Used in dmu_object_alloc(). 1374789Sahrens */ 1375789Sahrens int 13766992Smaybee dnode_next_offset(dnode_t *dn, int flags, uint64_t *offset, 13773025Sahrens int minlvl, uint64_t blkfill, uint64_t txg) 1378789Sahrens { 13796992Smaybee uint64_t initial_offset = *offset; 1380789Sahrens int lvl, maxlvl; 1381789Sahrens int error = 0; 1382789Sahrens 13836992Smaybee if (!(flags & DNODE_FIND_HAVELOCK)) 13846992Smaybee rw_enter(&dn->dn_struct_rwlock, RW_READER); 1385789Sahrens 1386789Sahrens if (dn->dn_phys->dn_nlevels == 0) { 13876992Smaybee error = ESRCH; 13886992Smaybee goto out; 1389789Sahrens } 1390789Sahrens 1391789Sahrens if (dn->dn_datablkshift == 0) { 1392789Sahrens if (*offset < dn->dn_datablksz) { 13936992Smaybee if (flags & DNODE_FIND_HOLE) 1394789Sahrens *offset = dn->dn_datablksz; 1395789Sahrens } else { 1396789Sahrens error = ESRCH; 1397789Sahrens } 13986992Smaybee goto out; 1399789Sahrens } 1400789Sahrens 1401789Sahrens maxlvl = dn->dn_phys->dn_nlevels; 1402789Sahrens 1403789Sahrens for (lvl = minlvl; lvl <= maxlvl; lvl++) { 14043025Sahrens error = dnode_next_offset_level(dn, 14056992Smaybee flags, offset, lvl, blkfill, txg); 14061793Sahrens if (error != ESRCH) 1407789Sahrens break; 1408789Sahrens } 1409789Sahrens 14106992Smaybee while (error == 0 && --lvl >= minlvl) { 14113025Sahrens error = dnode_next_offset_level(dn, 14126992Smaybee flags, offset, lvl, blkfill, txg); 14133025Sahrens } 1414789Sahrens 14156992Smaybee if (error == 0 && (flags & DNODE_FIND_BACKWARDS ? 14166992Smaybee initial_offset < *offset : initial_offset > *offset)) 14171793Sahrens error = ESRCH; 14186992Smaybee out: 14196992Smaybee if (!(flags & DNODE_FIND_HAVELOCK)) 14206992Smaybee rw_exit(&dn->dn_struct_rwlock); 1421789Sahrens 1422789Sahrens return (error); 1423789Sahrens } 1424